-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathllvm_test.ml
1575 lines (1357 loc) · 54.5 KB
/
llvm_test.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(**
* LLVM-bindings to subsetphp
*
* @author Olle Harstedt
* @since 2015-07-28
*)
(**
* Does size of executable matter anymore? With FastCGI, it's
* only loaded once. When compiling, prioritize speed instead of
* size. Especially if CPU speed will keep to lag after memory size.
*
* Define one function for each possible combination of optional
* arguments.
*
* Hack in strict mode could theoretically be as fast as Java.
*
* Compile bitcode files with `llc hello.bc`.
* Then you need to link the .s file with `clang -c hello.s`, and
* `clang -o hello hello.o`
*
* Disassemble bitcode with llvm-dis.
*)
open Llvm
open Llvm_scalar_opts
open Typedast
open Printf
exception Llvm_error of string
exception Llvm_not_implemented of string
(** Setting up LLVM globals needed *)
let llctx = global_context ()
let llm = create_module llctx "mymodule"
let double_type = double_type llctx
let i32_t = i32_type llctx
let i32_ptr_t = pointer_type i32_t
let i64_t = i64_type llctx
let i64_ptr_t = pointer_type i64_t
let i8_t = i8_type llctx
let void_t = void_type llctx
let i8_ptr_t = pointer_type i8_t
let ptr_t = pointer_type i8_t
let ptr_ptr_t = pointer_type ptr_t
let llvm_array_t = named_struct_type llctx "dynamicArray"
(* zend_string opaque type *)
let zend_string_type = named_struct_type llctx "zend_string"
let zend_string_ptr_type = pointer_type zend_string_type
(* ocaml value opaque type *)
let caml_value_type = named_struct_type llctx "caml_value"
let caml_value_ptr_type = pointer_type caml_value_type
(* TODO: Should differ between global and local scope?
* Or just clear hash table at new function, as is now. *)
let global_named_values : (string, llvalue) Hashtbl.t = Hashtbl.create 10
let zero = const_int i32_t 0
let structs : (string, lltype) Hashtbl.t = Hashtbl.create 10
(* Information for the GC ob how to collect structs (pointer offsets) *)
let structs_gc : (string, struct_) Hashtbl.t = Hashtbl.create 10
(**
* Get number of field, where 0 is first, 1 second etc
*
* @param struct_ struct_
* @return int
*)
let get_struct_field_number struct_ field_name =
let {struct_name; struct_fields} = struct_ in
(* Has to take into account the struct type-information header *)
(* Header is two i32_t, so start at 1 (no header = start at -1) *)
let i = ref (1) in
let _ = List.find (fun field ->
i := !i + 1;
let (name, _) = field in
name = field_name
) struct_fields
in
!i
(**
* Return LLVM type of typed AST type
*
* @param Typedast.ty ty
* @return lltype
*)
let rec llvm_ty_of_ty ty = match ty with
| TNumber -> double_type
| TInt -> i32_t
| TInt64 -> i64_t
| TString -> i8_ptr_t
| TZend_string_ptr -> zend_string_ptr_type
| TUnit -> void_t
| TWeak_poly {contents = ((Some TNumber))} -> double_type
| TWeak_poly {contents = ((Some TString))} -> zend_string_ptr_type
| TLLVMArray -> llvm_array_t
| TDynamicSizeArray ty ->
let array_struct_type = struct_type llctx [|
i32_t; (* Type header *)
i32_t;
pointer_type (llvm_ty_of_ty ty); (* pointer to array *)
i32_t; (* used *)
i32_t; (* size *)
|] in
pointer_type array_struct_type
| TStruct (class_name, _) ->
let class_ty = try Hashtbl.find structs class_name with
| Not_found -> raise (Llvm_error (sprintf "llvm_ty_of_ty: Class %s was not found" class_name))
in
pointer_type class_ty (* Pointer instead? *)
| Delayed fn ->
llvm_ty_of_ty (fn ())
| _ -> raise (Llvm_not_implemented (sprintf "llvm_ty_of_ty: %s" (show_ty ty)))
(**
* As above, but TString returns zend_string_ptr_type, not i8_ptr_t for string literals
*
* TODO: How to solve this confusion? The inferrer must differ between literals and dynamic strings
*)
let llvm_ty_of_ty_fun ty = match ty with
| TNumber -> double_type
| TInt -> i32_t
| TString -> zend_string_ptr_type
| TString_literal -> i8_ptr_t
| TPtr_ptr -> ptr_ptr_t
| TPtr -> i8_ptr_t
| TZend_string_ptr -> zend_string_ptr_type
| TCaml_value -> caml_value_ptr_type
| TUnit -> void_t
| TInt64 -> i64_t
| TLLVMArray -> llvm_array_t
| TStruct (class_name, fields) ->
let class_ty = try Hashtbl.find structs class_name with
| Not_found -> raise (Llvm_error (sprintf "llvm_ty_of_ty_fun: Class %s was not found" class_name))
in
dump_type class_ty;
pointer_type class_ty (* Pointer instead? *)
| TDynamicSizeArray ty ->
let array_struct_type = struct_type llctx [|
i32_t; (* Type header *)
i32_t;
pointer_type (llvm_ty_of_ty ty); (* pointer to array *)
i32_t; (* used *)
i32_t; (* size *)
|] in
pointer_type array_struct_type
| _ -> raise (Llvm_not_implemented (sprintf "llvm_ty_of_ty_fun: %s" (show_ty ty)))
(**
* Create an alloca instruction in the entry block of the function. This
* is used for mutable variables etc.
*
* From Kaleidoscope tutorial
*)
let create_entry_block_alloca the_function var_name ty =
let builder = builder_at llctx (instr_begin (entry_block the_function)) in
build_alloca ty var_name builder
(**
* Create a new alloca with type ty and make it
* gcroot
*
* @param llbuilder
* @param ty lltype Pointer type, probably
* @return unit
*)
let create_new_gcroot_alloca llbuilder ty : llvalue =
let alloca = build_alloca ty "tmp" llbuilder in
let tmp = build_bitcast alloca ptr_ptr_t "tmp2" llbuilder in
let callee =
match lookup_function "llvm.gcroot" llm with
| Some callee -> callee
| None ->
raise (Llvm_error (sprintf "unknown function referenced: %s" "llvm.gcroot"))
in
let args = [|tmp; const_null i8_ptr_t|] in
build_call callee args "" llbuilder
(**
* Create a new alloca with type ty and size and make it
* gcroot
*
* Add the header of zend_refcounted, which is uint32_t * 2 = 8 bytes.
*
* @param llbuilder
* @param ty lltype Pointer type, probably
* @param size llvalue?
* @return build_call * alloca
*)
let create_new_gcroot_malloc llbuilder ty size : llvalue * llvalue =
let alloca = build_alloca ty "tmp" llbuilder in
(* Zend header - TODO: Correct size? *)
let zend_refcounted_size = const_int i64_t 64 in
(* Add header size *)
let size = const_bitcast size i64_t in
let add_of_size = build_add size zend_refcounted_size "size" llbuilder in
(* Call custom gc malloc *)
let args = [|add_of_size|] in
let malloc =
match lookup_function "llvm_gc_allocate" llm with
| Some callee -> callee
| None ->
raise (Llvm_error (sprintf "unknown function referenced: %s" "llvm_gc_allocate"))
in
let malloc_result = build_call malloc args "tmp" llbuilder in
let malloc_result_casted = build_bitcast malloc_result ty "tmp2" llbuilder in
ignore (build_store malloc_result_casted alloca llbuilder);
(* Call llvm.gcroot *)
let tmp = build_bitcast alloca ptr_ptr_t "tmp2" llbuilder in
let callee =
match lookup_function "llvm.gcroot" llm with
| Some callee -> callee
| None ->
raise (Llvm_error (sprintf "unknown function referenced: %s" "llvm.gcroot"))
in
let args = [|tmp; const_null i8_ptr_t|] in
build_call callee args "" llbuilder, alloca
(**
* Generate code for a function prototype
* From LLVM tutorial
*
* There's no support for this in PHP, but still
* needed for external functions and libraries.
*
* @param def def
* @return llvalue
*)
let codegen_proto (fun_ : fun_) =
match fun_ with
| {f_name = (f_name_pos, name); f_params; f_ret} -> begin
(* Make the function type: double(double,double) etc. *)
let args = List.map (fun param -> match param with {param_id; param_type} -> param_type) f_params in
let args = Array.of_list args in
let llvm_args = Array.map (fun arg_type ->
llvm_ty_of_ty_fun arg_type
) args in
let ft = function_type (llvm_ty_of_ty_fun f_ret) llvm_args in
dump_type ft;
let f = match lookup_function name llm with
| None ->
let name = String.sub name 1 (String.length name - 1) in (* Strip leading \ (namespace thing) *)
let fn = declare_function name ft llm in
set_gc (Some "shadow-stack") fn;
fn
(* If 'f' conflicted, there was already something named 'name'. If it
* has a body, don't allow redefinition or reextern. *)
| Some f ->
(* TODO: Allow this? *)
(* If 'f' already has a body, reject this. *)
if block_begin f <> At_end f then
raise (Llvm_error "redefinition of function");
(* If 'f' took a different number of arguments, reject. *)
if element_type (type_of f ) <> ft then
raise (Llvm_error "redefinition of function with different # args");
f
in
(* Set names for all arguments. *)
Array.iteri (fun i a ->
let ty = args.(i) in
let n = string_of_ty ty in
set_value_name n a;
Hashtbl.add global_named_values n a;
) (params f);
f
end
(* Create an alloca for each argument and register the argument in the symbol
* table so that references to it will succeed. *)
let create_argument_allocas the_function fun_ llbuilder =
let args = List.map (fun param -> match param with {param_id = (pos, var_name); param_type} -> var_name) fun_.f_params in
let llvm_args = List.map (fun param -> match param with
| {param_id; param_type} ->
llvm_ty_of_ty_fun param_type
) fun_.f_params in
let args = Array.of_list args in
Array.iteri (fun i ai ->
let var_name = args.(i) in
(* TODO: Renaming to avoid name collision does not work
let (_, f_name) = fun_.f_name in
let var_name = f_name ^ var_name in
*)
let var_type = List.nth llvm_args i in
(* Create an alloca for this variable. *)
let alloca = create_entry_block_alloca the_function var_name var_type in
(* Call gcroot for arg *)
(* TODO: Only needed for heap allocated args? *)
(*
let tmp = build_bitcast alloca ptr_ptr_t "tmp" llbuilder in
let callee =
match lookup_function "llvm.gcroot" llm with
| Some callee -> callee
| None ->
raise (Llvm_error (sprintf "unknown function referenced: %s" "llvm.gcroot"))
in
let args = [|tmp; const_null i8_ptr_t|] in
ignore (build_call callee args "" llbuilder);
*)
(* Store the initial value into the alloca. *)
ignore(build_store ai alloca llbuilder);
(* Add arguments to variable symbol table. *)
Hashtbl.add global_named_values var_name alloca;
) (params the_function)
(**
* Generate code for function
* From tutorial
*
* @param fun_ fun_
* @param llbuilder
* @return llvalue
*)
let rec codegen_fun (fun_ : fun_) the_fpm =
print_endline "codegen_fun";
(* TODO: This means all function must come before "main" script code? *)
Hashtbl.clear global_named_values;
let the_function = codegen_proto fun_ in
(* Create a new basic block to start insertion into. *)
let bb = append_block llctx "entry" the_function in
let llbuilder = builder_at_end llctx (entry_block the_function) in
position_at_end bb llbuilder;
try
(* Add all arguments to the symbol table and create their allocas. *)
create_argument_allocas the_function fun_ llbuilder;
ignore (codegen_block fun_.f_body llbuilder);
(* Validate the generated code, checking for consistency. *)
Llvm_analysis.assert_valid_function the_function;
(* Optimize the function. *)
(* TODO: Don't do this here, but in the module, for better error messages. *)
(*let _ = PassManager.run_function the_function the_fpm in*)
print_endline "codegen_fun end";
the_function
with e ->
delete_function the_function;
raise e
(**
* Generate LLVM IR for program
*
* All "global" PHP variables are wrapped in the main function.
*
* @param Typedast.program program
* @return ?
*)
and codegen_program program =
let the_fpm = PassManager.create_function llm in
(* Promote allocas to registers. *)
add_memory_to_register_promotion the_fpm;
(* Do simple "peephole" optimizations and bit-twiddling optzn. *)
add_instruction_combination the_fpm;
(* reassociate expressions. *)
add_reassociation the_fpm;
(* Eliminate Common SubExpressions. *)
add_gvn the_fpm;
(* Simplify the control flow graph (deleting unreachable blocks, etc). *)
add_cfg_simplification the_fpm;
ignore (PassManager.initialize the_fpm);
(** Generate classes *)
let rec aux_class program = match program with
| [] ->
()
(*
| Class class_ :: tail ->
let _ = codegen_class fun_ the_fpm in
aux_class tail
*)
| Struct struct_ :: tail ->
codegen_struct struct_ the_fpm;
aux_class tail
| somethingelse :: tail ->
aux_class tail
in
aux_class program;
(** Generate functions *)
let rec aux_fun program = match program with
| [] ->
()
| Fun fun_ :: tail ->
let _ = codegen_fun fun_ the_fpm in
aux_fun tail
| somethingelse :: tail ->
aux_fun tail
in
aux_fun program;
(* Better clear this... *)
Hashtbl.clear global_named_values;
(* New function type for the main function *)
let fty = function_type i32_t [||] in
(* New function definition, main *)
let fn = define_function "main" fty llm in
set_gc (Some "shadow-stack") fn;
(* Create a builder at end of block for function main *)
let llbuilder = builder_at_end llctx (entry_block fn) in
(** Init the GC *)
let callee =
(*match lookup_function "subsetphp_gc_init" llm with*)
match lookup_function "llvm_gc_initialize" llm with
| Some callee -> callee
| None ->
raise (Llvm_error (sprintf "unknown function referenced: %s" "llvm_gc_initialize"))
in
let heapsize = const_int i32_t 100000 in
ignore (build_call callee [|heapsize|] "" llbuilder);
(** Generate list of defs *)
let rec aux program = match program with
| [] ->
()
| Stmt Noop :: tail ->
(* Don't generate anything for Noop *)
aux tail
| Stmt stmt :: tail ->
let _ = codegen_stmt stmt llbuilder in
aux tail
| possibly_fun :: tail ->
aux tail
in
aux program;
let _ = build_ret (const_int i32_t 0) llbuilder in
(** Generate GC runtime type information *)
(*let begin_pos = global_begin llm in*)
(*instr_begin*)
(*position_builder begin_pos llbuilder;*)
generate_gc_runtime_type_information llbuilder;
(*generate_json_gc_type_information ();*)
()
(**
* Second try to generate type-information for the GC
* This time using JSON serialization and store it into
* an external file which is read by the GC at program
* initialisation.
*)
and generate_json_gc_type_information () =
()
(**
* The GC needs to know what pointers in a struct
* to follow and mark/sweep.
*
* { type : int; nr_of_offsets : int; pointer_offsets : int array }
*
* @param llbuilder
* @return unit
*)
and generate_gc_runtime_type_information llbuilder =
print_endline "1";
let nr_of_struct_types = Hashtbl.length structs in
print_endline "2";
printf "nr_of_struct_types = %d\n" nr_of_struct_types;
(*let pointer_offsets_t = array_type i32_t 2 in*)
print_endline "3";
let array_of_ptrs = Array.make nr_of_struct_types (const_pointer_null i8_ptr_t) in
print_endline "6";
let j = ref 0 in
print_endline "7";
(*let t1 = struct_type*)
(**
* Generate a bunch of global structs with
* type information.
*
* @param struct_type ?
* @return ?
*)
let generate_struct_type_info name struct_type_ =
begin match struct_type_ with
| {struct_name = name; struct_fields = fields} ->
(* Number of heap allocated fields that needs to be collected *)
let nr_of_gc_fields = ref 0 in
(* List of offsets for above fields, so the GC knows where to look *)
let list_of_gc_offsets = ref [] in
(* Iterate through fields and catch all heap allocated dito *)
List.iter (fun fi -> match fi with
| (field_name, Typedast.TWeak_poly {contents = Some Typedast.TString}) ->
print_endline ("here " ^ field_name);
list_of_gc_offsets := !list_of_gc_offsets @ [(const_int i32_t !nr_of_gc_fields)];
printf "offset = %d\n" !nr_of_gc_fields;
nr_of_gc_fields := !nr_of_gc_fields + 1;
| (field_name, _) ->
(* Not a heap allocation *)
()
) fields;
let name = String.sub name 1 (String.length name - 1) in (* Strip leading \ (namespace thing) *)
let const = const_struct llctx [|
(* Number of fields that need to be collected = length of array *)
const_int i32_t !nr_of_gc_fields;
(* Array of offsets *)
const_array i32_t (Array.of_list !list_of_gc_offsets)
|] in
let const_ptr = define_global ("structs_gc_info_" ^ name) const llm in
let ptr = const_pointercast const_ptr i8_ptr_t in
let glob_ptr = define_global ("structs_gc_info_ptr_" ^ name) ptr llm in
array_of_ptrs.(!j) <- glob_ptr;
j := !j + 1
end
in
Hashtbl.iter generate_struct_type_info structs_gc;
print_endline "18";
dump_type ptr_ptr_t;
let const = const_array ptr_ptr_t array_of_ptrs in (* struct has type {i32, i32} *)
print_endline "19";
ignore (define_global "structs_gc_info" const llm);
print_endline "20"
(**
* Generate LLVM IR for block (stmt list)
*
* @param block stmt list
* @param llbuilder
* @return llvalue
*)
and codegen_block block llbuilder : llvalue =
let the_block = block_parent (insertion_block llbuilder) in
begin match block with
| Noop :: [] ->
()
| block ->
let stmt_values = ref [] in
for i = 0 to List.length block - 1 do
let stmt = List.nth block i in
let stmt_value = codegen_stmt stmt llbuilder in
stmt_values := !stmt_values @ [stmt_value];
done;
end;
(*stmt_values*)
the_block
(**
* Generate code for struct
* Actually just stores type information for this struct
* type name.
*
* Structs in subsetphp are final classes with only
* public member variables.
*
* @param struct_
* @param llbuilder
* @return llvalue
*)
and codegen_struct struct_ llbuilder : unit =
print_endline "codegen_struct";
match struct_ with
| {struct_name = name; struct_fields = fields} ->
(* Extract llvm types from fields *)
let fields = List.map (fun field ->
match field with
| (field_name, field_ty) -> llvm_ty_of_ty field_ty
) fields in
(* Add header with type-information for GC *)
(* For now just two i32 - how many bytes is that? *)
let fields = i32_t :: fields in
let fields = i32_t :: fields in
let fields = Array.of_list fields in
let name = String.sub name 1 (String.length name - 1) in (* Strip leading \ (namespace thing) *)
let t = struct_type llctx fields in
Hashtbl.add structs name t;
Hashtbl.add structs_gc name struct_;
(**
* Generate LLVM IR for stmt
*
* @param stmt Typedast.stmt
* @param llbuilder
* @return llvalue
*)
and codegen_stmt (stmt : stmt ) llbuilder : llvalue =
match stmt with
| Block block ->
codegen_block block llbuilder
| Expr (ty, expr) ->
codegen_expr expr llbuilder
| Return (pos, expr_opt, ty) ->
begin match expr_opt with
| None ->
build_ret_void llbuilder
| Some expr ->
let expr = codegen_expr expr llbuilder in
build_ret expr llbuilder
end
| If (expr, then_, else_) ->
let expr = codegen_expr expr llbuilder in
(* Convert condition to a bool by comparing equal to 0.0 *)
let zero = const_float double_type 0.0 in
let cond_val = build_fcmp Fcmp.One expr zero "ifcond" llbuilder in
(* Grab the first block so that we might later add the conditional branch
* to it at the end of the function. *)
let start_bb = insertion_block llbuilder in
let the_function = block_parent start_bb in
let then_bb = append_block llctx "then" the_function in
(* Emit 'then' value. *)
position_at_end then_bb llbuilder;
let then_val = codegen_block then_ llbuilder in
(* Codegen of 'then' can change the current block, update then_bb for the
* phi. We create a new name because one is used for the phi node, and the
* other is used for the conditional branch. *)
let new_then_bb = insertion_block llbuilder in
(* Emit 'else' value. *)
let else_bb = append_block llctx "else" the_function in
position_at_end else_bb llbuilder;
let else_val = codegen_block else_ llbuilder in
(* Codegen of 'else' can change the current block, update else_bb for the
* phi. *)
let new_else_bb = insertion_block llbuilder in
(* Emit merge block. *)
let merge_bb = append_block llctx "ifcont" the_function in
position_at_end merge_bb llbuilder;
let incoming = [(then_val, new_then_bb); (else_val, new_else_bb)] in
let phi = build_phi incoming "iftmp" llbuilder in
(* Return to the start block to add the conditional branch. *)
position_at_end start_bb llbuilder;
ignore (build_cond_br cond_val then_bb else_bb llbuilder);
(* Set a unconditional branch at the end of the 'then' block and the
* 'else' block to the 'merge' block. *)
position_at_end new_then_bb llbuilder; ignore (build_br merge_bb llbuilder);
position_at_end new_else_bb llbuilder; ignore (build_br merge_bb llbuilder);
(* Finally, set the llbuilder to the end of the merge block. *)
position_at_end merge_bb llbuilder;
phi
(*
*)
| For ( (p1, Binop ((Eq None), (p2, Lvar ((p3, var_name), TNumber)), (p4, (Lvar _)), TUnit)) as start, end_, step, body)
| For ( (p1, Binop ((Eq None), (p2, Lvar ((p3, var_name), TNumber)), (p4, (Int _)), TUnit)) as start, end_, step, body) ->
(* Output this as:
* var = alloca double
* ...
* start = startexpr
* store start -> var
* goto loop
* loop:
* ...
* bodyexpr
* ...
* loopend:
* step = stepexpr
* endcond = endexpr
*
* curvar = load var
* nextvar = curvar + step
* store nextvar -> var
* br endcond, loop, endloop
* outloop: *)
let the_function = block_parent (insertion_block llbuilder) in
(* Create an alloca for the variable in the entry block. *)
let alloca = create_entry_block_alloca the_function var_name double_type in
(* Emit the start code first, without 'variable' in scope. *)
let start_val = codegen_expr start llbuilder in
(* Store the value into the alloca. *)
ignore (build_store start_val alloca llbuilder);
(* Make the new basic block for the loop header, inserting after current
* block. *)
let loop_bb = append_block llctx "loop" the_function in
(* Insert an explicit fall through from the current block to the
* loop_bb. *)
ignore (build_br loop_bb llbuilder);
(* Start insertion in loop_bb. *)
position_at_end loop_bb llbuilder;
(* Within the loop, the variable is defined equal to the PHI node. If it
* shadows an existing variable, we have to restore it, so save it
* now. *)
let old_val =
try Some (Hashtbl.find global_named_values var_name) with Not_found -> None
in
Hashtbl.add global_named_values var_name alloca;
(* Emit the body of the loop. This, like any other expr, can change the
* current BB. Note that we ignore the value computed by the body, but
* don't allow an error *)
ignore (codegen_block body llbuilder);
(* Emit the step value. *)
let step_val = codegen_expr step llbuilder in
(* Compute the end condition. *)
let end_cond = codegen_expr end_ llbuilder in
(* Reload, increment, and restore the alloca. This handles the case where
* the body of the loop mutates the variable. *)
(*let cur_var = build_load alloca var_name llbuilder in*)
(*let next_var = build_fadd cur_var step_val "nextvar" llbuilder in*)
ignore (build_store step_val alloca llbuilder);
(* Convert condition to a bool by comparing equal to 0.0. *)
let zero = const_float double_type 0.0 in
let end_cond = build_fcmp Fcmp.One end_cond zero "loopcond" llbuilder in
(* Create the "after loop" block and insert it. *)
let after_bb = append_block llctx "afterloop" the_function in
(* Insert the conditional branch into the end of loop_end_bb. *)
ignore (build_cond_br end_cond loop_bb after_bb llbuilder);
(* Any new code will be inserted in after_bb. *)
position_at_end after_bb llbuilder;
(* Restore the unshadowed variable. *)
(* TODO: Not relevant for PHP, fix. *)
begin match old_val with
| Some old_val -> Hashtbl.add global_named_values var_name old_val
| None -> ()
end;
(* for expr always returns 0.0. *)
const_null double_type
| _ -> raise (Llvm_not_implemented (sprintf "codegen_stmt: %s" (show_stmt stmt)))
(**
* Generate LLVM IR for expr
*
* @param expr Typedast.expr
* @param llbuilder
* @return llvalue
*)
and codegen_expr (expr : expr) llbuilder : llvalue =
match expr with
(*
| p, Id (id, ty) ->
()
*)
| p, True ->
const_float double_type 1.0
| p, False ->
const_float double_type 0.0
| p, Lvar ((pos, lvar_name), ty) ->
(*print_endline lvar_name;*)
(*let the_function = block_parent (insertion_block llbuilder) in*)
let variable = try Hashtbl.find global_named_values lvar_name with
| Not_found ->
let line, start, end_ = Pos.info_pos pos in
let pos_info = sprintf "File %S, line %d, characters %d-%d"
(snd Pos.(pos.pos_file)) line start end_ in
raise (Llvm_error (sprintf "Lvar is used on rhs before ever used on the lhs: %s, %s" lvar_name (pos_info)))
in
build_load variable lvar_name llbuilder
| p, Number nr ->
const_float double_type nr;
| p, String (pos, str) ->
(* Create temporary variable to store string in *)
let alloca = build_alloca zend_string_ptr_type "tmp" llbuilder in
let tmp = build_bitcast alloca ptr_ptr_t "tmp2" llbuilder in
let callee =
match lookup_function "llvm.gcroot" llm with
| Some callee -> callee
| None ->
raise (Llvm_error (sprintf "unknown function referenced: %s" "llvm.gcroot"))
in
let args = [|tmp; const_null i8_ptr_t|] in
ignore (build_call callee args "" llbuilder);
let str_ptr = build_global_stringptr str str llbuilder in
let length = const_int i32_t (String.length str) in
let persistent = const_int i32_t 1 in
let args = [|str_ptr; length; persistent|] in
(* Init string with subsetphp_string_init *)
let callee =
match lookup_function "subsetphp_string_init" llm with
| Some callee -> callee
| None ->
raise (Llvm_error (sprintf "unknown function referenced: %s" "subsetphp_string_init"))
in
let result = build_call callee args "str" llbuilder in
ignore(build_store result alloca llbuilder);
result
| p, Int (pos, i)
| p, Float (pos, i) ->
let f = float_of_string i in
const_float double_type f;
(* TODO: Not used? *)
| p, Array (array_values, TFixedSizeArray (array_length, the_array_type)) ->
let llvm_array_type = array_type (llvm_ty_of_ty the_array_type) array_length in
(* An array alloca is released when function returns ("stack allocation") *)
(* Need to use malloc and gc *)
(*build_array_alloca llvm_array_type (const_int i32_t array_length) "arr" llbuilder*)
(*build_alloca llvm_array_type "arr" llbuilder*)
print_endline "type:";
let build, alloca = create_new_gcroot_malloc llbuilder (pointer_type llvm_array_type) (size_of llvm_array_type) in
print_endline "build:";
print_endline "alloca:";
alloca
(* Like $array[1] *)
| p, ArrayFixedSize_get (
(pos1, Lvar ((pos2, array_var_name), var_type)),
(pos3, Number index),
element_type) ->
let arr : llvalue = try Hashtbl.find global_named_values array_var_name with
| Not_found -> raise (Llvm_error (sprintf "Tried to load number from struct variable that doesn't exist: %s" array_var_name))
in
let loaded_array = build_load arr "loaded_arr" llbuilder in
let index = int_of_float index in
let gep = build_struct_gep loaded_array index "gep" llbuilder in
let load = build_load gep "load" llbuilder in
load
(* Like $array[$i] *)
| p, ArrayFixedSize_get (
(pos1, Lvar ((pos2, array_var_name), var_type)),
(pos3, Lvar ((pos4, lvar_name), TNumber)),
element_type) ->
(*let struct_ty = Hashtbl.find structs struct_name in*)
let arr : llvalue = try Hashtbl.find global_named_values array_var_name with
| Not_found -> raise (Llvm_error (sprintf "Tried to load number from struct variable that doesn't exist: %s" array_var_name))
in
let loaded_array = build_load arr "loaded_arr" llbuilder in
let index_expr = codegen_expr (pos3, Lvar ((pos4, lvar_name), TNumber)) llbuilder in
let index = build_fptoui index_expr i32_t "index" llbuilder in
let gep = build_gep loaded_array [|zero; index|] "gep" llbuilder in
let load = build_load gep "load" llbuilder in
load
(* Like $array[0] with dynamically sized array *)
| p, ArrayDynamicSize_get (
(pos1, Lvar ((pos2, array_var_name), var_type)),
(pos3, Number index),
element_type) ->
let arr : llvalue = try Hashtbl.find global_named_values array_var_name with
| Not_found -> raise (Llvm_error (sprintf "Tried to load number from struct variable that doesn't exist: %s" array_var_name))
in
let loaded_array_struct = build_load arr "loaded_array_struct" llbuilder in
(* TODO: Add bounds check Throw exception? PHP notice: Undefined offset x in file y, line z *)
(* Why index 2 here? Because that's the address of the array. See the struct
* def in bindings2.c:
*)
let gep = build_struct_gep loaded_array_struct 2 "gep" llbuilder in
let loaded_array = build_load gep "loaded_array" llbuilder in
let index = int_of_float index in
let llvm_index = const_int i32_t index in
let gep = build_gep loaded_array [|llvm_index|] "gep" llbuilder in
let load = build_load gep "load" llbuilder in
load
| p, Unop (Uminus expr, TNumber) ->
let expr_code = codegen_expr expr llbuilder in
build_fneg expr_code "neg" llbuilder
(* < *)
| p, Binop (Lt, lexpr, rexpr, TBoolean) ->
let lexpr_code = codegen_expr lexpr llbuilder in
let rexpr_code = codegen_expr rexpr llbuilder in
let i = build_fcmp Fcmp.Olt lexpr_code rexpr_code "EQeqeq" llbuilder in
build_uitofp i double_type "booltmp" llbuilder
(* > *)
| p, Binop (Gt, lexpr, rexpr, TBoolean) ->
let lexpr_code = codegen_expr lexpr llbuilder in
let rexpr_code = codegen_expr rexpr llbuilder in
let i = build_fcmp Fcmp.Ogt lexpr_code rexpr_code "EQeqeq" llbuilder in
build_uitofp i double_type "booltmp" llbuilder
(* === on numbers *)
| p, Binop (EQeqeq TNumber, lexpr, rexpr, TBoolean) ->
let lexpr_code = codegen_expr lexpr llbuilder in
let rexpr_code = codegen_expr rexpr llbuilder in
let i = build_fcmp Fcmp.Oeq lexpr_code rexpr_code "EQeqeq" llbuilder in
build_uitofp i double_type "booltmp" llbuilder
(* +=, only allowed on numbers *)
| p, Binop (Eq (Some Plus), (lha_pos, Lvar ((lvar_pos, lvar_name), TNumber)), rexpr, TNumber) ->
(*let the_function = block_parent (insertion_block llbuilder) in*)
let variable = try Hashtbl.find global_named_values lvar_name with
| Not_found ->
(* Should not happen *)
failwith "Tried to use += on variable that is not defined"
in
let rexpr_code = codegen_expr rexpr llbuilder in
let load_code = build_load variable lvar_name llbuilder in
let add_code = build_fadd load_code rexpr_code "addtmp" llbuilder in
ignore (build_store add_code variable llbuilder);
add_code
(* -=, only allowed on numbers *)
| p, Binop (Eq (Some Minus), (lha_pos, Lvar ((lvar_pos, lvar_name), TNumber)), rexpr, TNumber) ->
(*let the_function = block_parent (insertion_block llbuilder) in*)
let variable = try Hashtbl.find global_named_values lvar_name with
| Not_found ->
(* Should not happen *)
failwith "Tried to use -= on variable that is not defined"
in
let rexpr_code = codegen_expr rexpr llbuilder in
let load_code = build_load variable lvar_name llbuilder in
let add_code = build_fsub load_code rexpr_code "addtmp" llbuilder in
ignore (build_store add_code variable llbuilder);
add_code
(* In case type of lvar is delayed (see note in ty_of_ty in infer.ml) *)
| p, Binop (Eq None, (lhs_pos, Lvar (lvar, Delayed fn)), value_expr, binop_ty) ->
let typ = fn () in
(* This new expression is same as above but with delayed type get *)
let new_expr = (p, Binop (Eq None, (lhs_pos, Lvar (lvar, typ)), value_expr, binop_ty)) in
codegen_expr new_expr llbuilder
(* Assign number to variable *)
| p, Binop (Eq None, (lhs_pos, Lvar ((lvar_pos, lvar_name), TNumber)), value_expr, binop_ty) ->
let the_function = block_parent (insertion_block llbuilder) in
let variable = try Hashtbl.find global_named_values lvar_name with
| Not_found ->
print_endline "new variable in scope";
(* If variable is not found in this scope, create a new one *)
let alloca = create_entry_block_alloca the_function lvar_name double_type in