-
Notifications
You must be signed in to change notification settings - Fork 0
/
codegen.ml
2137 lines (1668 loc) · 94.1 KB
/
codegen.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
(*
codegen.ml
@authors Aidan Barg
Abby Larson
Claudia Aranda Barrios
Steven Oh
*)
(* We'll refer to Llvm and Ast constructs with module names *)
module L = Llvm
module A = Ast
open Sast
module StringMap = Map.Make(String)
exception Unfinished of string
type symbol_table = {
variables : L.llvalue StringMap.t;
parent : symbol_table option;
funcs : (sfunc_decl option * L.llvalue) StringMap.t;
curr_func : string;
global_vars : L.llvalue StringMap.t;
}
(*** Declare all the LLVM types we'll use ***)
let translate decls =
let context = L.global_context () in
let node_struct = Llvm.named_struct_type context "node_struct" in
let _ = Llvm.struct_set_body node_struct
[| Llvm.pointer_type (L.i8_type context);
Llvm.i1_type context;
Llvm.pointer_type (L.i8_type context); |] false
in
let node_t = Llvm.pointer_type node_struct in
let edge_struct = L.named_struct_type context "edge_t" in
let _ = L.struct_set_body edge_struct
[| node_t;
node_t;
L.i32_type context|] false
in
let edge_t = Llvm.pointer_type edge_struct in
(* for linked list of graph *)
let edge_node = L.named_struct_type context "edge_node" in
let _ = L.struct_set_body edge_node
[| edge_t; (*first is our current edge*)
Llvm.pointer_type edge_node |](*second is our next edge pointer *) false
in
let node_node = L.named_struct_type context "node_node" in
let _ = L.struct_set_body node_node
[| node_t; (*first is our current edge*)
Llvm.pointer_type node_node |](*second is our next node pointer *) false
in
(* linked list of graph *)
let graph_t = L.named_struct_type context "graph_tt" in
let _ = Llvm.struct_set_body graph_t
[|
Llvm.pointer_type node_node;
Llvm.pointer_type edge_node;
|] false
in
let graph_ptr = Llvm.pointer_type graph_t in
let list_node = L.named_struct_type context "list_node" in
let _ = L.struct_set_body list_node
[| L.pointer_type (L.i8_type context); L.pointer_type list_node |] false
in
let list_t = L.pointer_type list_node
and i32_t = L.i32_type context
and i8_t = L.i8_type context
and i1_t = L.i1_type context
and string_t = L.pointer_type (L.i8_type context)
and float_t = L.double_type context
and void_t = L.void_type context
and the_module = L.create_module context "Graphite" in
(*** Define Graphite -> LLVM types here ***)
let ltype_of_typ = function
A.Int -> i32_t
| A.Bool -> i1_t
| A.Float -> float_t
| A.Void -> void_t
| A.String -> string_t
| A.Node(typ) -> node_t
| A.Edge(typ) -> edge_t
| A.Graph(typ) -> graph_ptr
| A.List_t -> list_t
| _ -> raise (Unfinished "not all types implemented")
in
(* add to symbol table *)
let bind_var (scope : symbol_table) x v =
if scope.parent = None
then
{ variables = scope.variables;
parent = scope.parent;
funcs = scope.funcs;
curr_func = scope.curr_func;
global_vars = StringMap.add x v scope.global_vars; }
else
{ variables = StringMap.add x v scope.variables;
parent = scope.parent;
funcs = scope.funcs;
curr_func = scope.curr_func;
global_vars = scope.global_vars; }
in
let rec find_variable (scope: symbol_table) (name : string) =
try find_variable_local scope name
with Not_found -> StringMap.find name scope.global_vars
and find_variable_local (scope : symbol_table) (name : string) =
try StringMap.find name scope.variables
with Not_found ->
match scope.parent with
Some p -> find_variable p name
| None -> raise Not_found
in
let rec find_func (scope : symbol_table) (name : string) =
try StringMap.find name scope.funcs
with Not_found ->
match scope.parent with
Some p -> find_func p name
| None -> let _ = L.dump_value (L.const_int i32_t 0) in raise Not_found
in
(* Add function name to symbol table *)
(* func is the llvm func type *)
let add_func s func stable =
{ variables = stable.variables;
parent = stable.parent;
funcs = StringMap.add s func stable.funcs;
curr_func = stable.curr_func;
global_vars = stable.global_vars; }
in
let set_curr_func s stable =
{ variables = stable.variables;
parent = stable.parent;
funcs = stable.funcs;
curr_func = s;
global_vars = stable.global_vars; }
in
let printf_t : L.lltype =
L.var_arg_function_type i32_t [| L.pointer_type i8_t |] in
let printf_func : L.llvalue =
L.declare_function "printf" printf_t the_module in
let strcmp_t : L.lltype =
L.function_type i32_t [| L.pointer_type i8_t; L.pointer_type i8_t |] in
let strcmp_func : L.llvalue =
L.declare_function "strcmp" strcmp_t the_module in
let array_get_t =
L.var_arg_function_type (L.pointer_type i8_t) [| L.pointer_type i8_t; i32_t |] in
let array_get_func =
L.declare_function "array_get" array_get_t the_module in
let array_set_t =
L.var_arg_function_type (L.pointer_type i8_t) [| L.pointer_type i8_t; i32_t; L.pointer_type i8_t |] in
let array_set_func =
L.declare_function "array_set" array_set_t the_module in
let array_len_t =
L.var_arg_function_type (L.pointer_type i8_t) [| L.pointer_type i8_t; |] in
let array_len_func =
L.declare_function "array_len" array_len_t the_module in
let nodeExists_t =
L.var_arg_function_type i8_t [| node_t |] in
let nodeExists_func =
L.declare_function "nodeExists" nodeExists_t the_module in
let array_add_t =
L.var_arg_function_type (L.pointer_type i8_t) [| L.pointer_type i8_t; i32_t; L.pointer_type i8_t |] in
let array_add_func =
L.declare_function "array_add" array_add_t the_module in
let array_del_t =
L.var_arg_function_type (L.pointer_type i8_t) [| L.pointer_type i8_t; i32_t; |] in
let array_del_func =
L.declare_function "array_del" array_del_t the_module in
(* let add_node_t : L.lltype =
L.var_arg_function_type void_t [| L.pointer_type i8_t ; L.pointer_type i8_t |] in *)
(* let add_node_func : L.llvalue =
L.declare_function "add_node" add_node_t the_module in *)
(*** will have to cast the pointer types to the actual types when being called
L.pointer_type i8_t is LLVM's equivalent of void pointers in C ***)
let to_string styp e = match e with
(_, SLiteral i) -> SString((string_of_int i) ^ "\n")
| (_, SString s) -> SString (s ^ "\n")
| (_, SBoolLit b) -> (match b with
true -> SString("1\n")
| _ -> SString("0\n") )
| (_, SFliteral f) -> SString (f ^ "\n")
| _ -> raise (Failure("printf: argument of unsupported type"))
in
let int_format_str = L.build_global_stringptr "%d\n" "fmt"
and float_format_str = L.build_global_stringptr "%g\n" "fmt"
(*** we treat booleans as integers when printing to avoid the
mysterious B that was being printed otherwise ***)
(* and bool_format_str = L.build_global_stringptr "%B\n" "fmt" *)
and string_format_str = L.build_global_stringptr "%s\n" "fmt" in
let add_terminal builder instr =
(* The current block where we're inserting instr *)
match L.block_terminator (L.insertion_block builder) with
Some _ -> ()
| None -> ignore (instr builder)
in
(*** Expressions go here ***)
let rec expr (builder, stable) ((styp, e) : sexpr) = match e with
SLiteral i -> L.const_int i32_t i
| SBoolLit b -> L.const_int i1_t (if b then 1 else 0)
| SFliteral f -> L.const_float_of_string float_t f
| SString s -> L.build_global_stringptr s "" builder
| SNoexpr -> L.const_int i32_t 0
| SBinop (e1, op, e2) ->
let (t, _) = e1
and e1' = expr (builder, stable) e1
and e2' = expr (builder, stable) e2 in
if t = A.Float then (match op with
A.Add -> L.build_fadd
| A.Sub -> L.build_fsub
| A.Mult -> L.build_fmul
| A.Div -> L.build_fdiv
| A.Equal -> L.build_fcmp L.Fcmp.Oeq
| A.Neq -> L.build_fcmp L.Fcmp.One
| A.Less -> L.build_fcmp L.Fcmp.Olt
| A.Leq -> L.build_fcmp L.Fcmp.Ole
| A.Greater -> L.build_fcmp L.Fcmp.Ogt
| A.Geq -> L.build_fcmp L.Fcmp.Oge
| A.And | A.Or ->
raise (Failure "internal error: semant should have rejected and/or on float")
) e1' e2' "tmp" builder
else (match op with
| A.Add -> L.build_add
| A.Sub -> L.build_sub
| A.Mult -> L.build_mul
| A.Div -> L.build_sdiv
| A.And -> L.build_and
| A.Or -> L.build_or
| A.Equal -> L.build_icmp L.Icmp.Eq
| A.Neq -> L.build_icmp L.Icmp.Ne
| A.Less -> L.build_icmp L.Icmp.Slt
| A.Leq -> L.build_icmp L.Icmp.Sle
| A.Greater -> L.build_icmp L.Icmp.Sgt
| A.Geq -> L.build_icmp L.Icmp.Sge
) e1' e2' "tmp" builder
| SUnop (op, e) ->
let (t, _) = e in
let e' = expr (builder, stable) e in
(match op with
A.Neg when t = A.Float -> L.build_fneg
| A.Neg -> L.build_neg
| A.Not -> L.build_not)
e' "tmp" builder
| SId s -> L.build_load (find_variable stable s) s builder
| SAssign (s, (typ, sexp)) ->
(match sexp with
SCall("array_get", _) -> let e' = expr (builder, stable) (typ, sexp) in
let e_cast = L.build_pointercast e' (L.type_of (find_variable stable s)) "li_conv" builder in
let e_cast_val = L.build_load e_cast "val_ptr" builder in
let _ = L.build_store e_cast_val (find_variable stable s) builder in
e'
| SDotCall(_, "get", _) -> let e' = expr (builder, stable) (typ, sexp) in
let e_cast = L.build_pointercast e' (L.type_of (find_variable stable s)) "li_conv" builder in
let e_cast_val = L.build_load e_cast "val_ptr" builder in
let _ = L.build_store e_cast_val (find_variable stable s) builder in
e'
| _ -> let e' = expr (builder, stable) (typ, sexp) in
let _ = L.build_store e' (find_variable stable s) builder in
e')
| SCall ("printf", [e]) ->
(match e with
(Int, SId s) -> L.build_call printf_func [| int_format_str builder ; (expr (builder, stable) e) |] "printf" builder
| (Float, SId s) -> L.build_call printf_func [| float_format_str builder ; (expr (builder, stable) e) |] "printf" builder
| (String, SId s) -> L.build_call printf_func [| string_format_str builder ; (expr (builder, stable) e) |] "printf" builder
| (Bool, SId s) -> L.build_call printf_func [| int_format_str builder ; (expr (builder, stable) e) |] "printf" builder
| _ -> L.build_call printf_func [| (expr (builder, stable) (A.String, (to_string styp e))) |] "printf" builder )
| SCall (name, args) ->
(match name with
"array_get" -> array_get_def (builder, stable) args
| "array_set" -> array_set_def (builder, stable) args
| "array_add" -> array_add_def (builder, stable) args
| "array_len" -> array_len_def (builder, stable) args
| "array_del" -> array_del_def (builder, stable) args
| _ -> let (fdecl_opt, llvm_decl) = find_func stable name in
let sfdecl = (match fdecl_opt with
Some(f) -> f
| _ -> raise (Failure "No function definition found"))
in
let proc_args arg = expr (builder, stable) arg in
let llargs = List.rev (List.map proc_args (List.rev args)) in
let result = (match sfdecl.styp with
A.Void -> ""
| _ -> name ^ "_result") in
L.build_call llvm_decl (Array.of_list llargs) result builder)
| SDotOp(var, field) ->
let lvar = find_variable stable var in
let lvar' = L.build_load lvar "lvar'" builder in
let steven = match field with
"flag" ->
Llvm.build_struct_gep lvar' 1 "temp" builder
| "name" ->
Llvm.build_struct_gep lvar' 0 "temp" builder
| "data" ->
Llvm.build_struct_gep lvar' 2 "temp" builder
| "src" -> L.build_struct_gep lvar' 0 "temp" builder
| "dst" -> L.build_struct_gep lvar' 1 "temp" builder
| "weight" -> L.build_struct_gep lvar' 2 "temp" builder
| _ -> raise (Failure ("syntax error caught post parsing. Nonexistent field " ^ field))
in
let steven' = match styp with
Uninitialized -> raise (Failure ("something went wrong " ^ field))
| _ -> L.build_load steven (var ^ "." ^ field) builder
in
(match field with
"flag" -> L.build_load steven (var ^ "." ^ field) builder
| "weight" -> L.build_load steven (var ^ "." ^ field) builder
| "src" ->
L.build_load steven "srcNode" builder
| "dst" ->
L.build_load steven "srcNode" builder
| "name" ->
L.build_load steven "srcNode" builder
| _ ->
let llvm_ty = ltype_of_typ styp in
let new_ptr = L.build_pointercast steven' (L.pointer_type (llvm_ty)) "new_ptr" builder in
L.build_load new_ptr (var ^ "." ^ field) builder)
| SDotAssign(var, field, e) ->
let e' = expr (builder, stable) e in
let (my_typ, expr) = e in
let lvar = find_variable stable var in
(* bc its a pointer *)
let lvar' = L.build_load lvar "lvar'" builder in
let steven = match field with
"flag" ->
L.build_struct_gep lvar' 1 "temp" builder
| "name" ->
L.build_struct_gep lvar' 0 "temp" builder
| "data" ->
L.build_struct_gep lvar' 2 "temp" builder
| "src" -> L.build_struct_gep lvar' 0 "temp" builder
| "dst" -> L.build_struct_gep lvar' 1 "temp" builder
| "weight" -> L.build_struct_gep lvar' 2 "temp" builder
| _ -> raise (Failure ("syntax error caught post parsing. Nonexistent field " ^ field))
in
let e'' = match field with
"flag" -> e'
| "weight" -> e'
| "src" ->
e'
| "dst" ->
e'
| "name" ->
e'
| _ ->
let styp_ptr = L.build_malloc (ltype_of_typ my_typ) "bruh" builder in
(* put data in *)
let _ = L.build_store e' styp_ptr builder in
let ptr =
match field with
| "src" -> styp_ptr
| "dst" -> styp_ptr (* must fix for data *)
| _ -> L.build_pointercast styp_ptr (L.pointer_type (i8_t)) "name" builder
in
ptr
in
L.build_store e'' steven builder
| SList(ses) ->
let list_head = L.build_malloc list_t "new_list" builder in
let _ = L.build_store (L.const_pointer_null (L.pointer_type list_node)) list_head builder in
let rec link_list idx es prev_node = (match es with
[] -> 0
| (typ, e) :: es -> let llvm_val = expr (builder, stable) (typ, e) in
let llvm_ptr = L.build_malloc (ltype_of_typ typ) "arr_val" builder in
let _ = L.build_store llvm_val llvm_ptr builder in
let array_node = L.const_named_struct list_node [| L.const_pointer_null (L.pointer_type i8_t); L.const_pointer_null (L.pointer_type list_node); |] in
(** insert the llvm value into the node **)
let gen_val = L.build_pointercast llvm_ptr (L.pointer_type i8_t) "i8ptr" builder in
let node_p = L.build_malloc list_node "node_p" builder in
let val_ptr = L.build_struct_gep node_p 0 "valloc" builder in
let _ = L.build_store array_node node_p builder in
let _ = L.build_store gen_val val_ptr builder in
(** store the pointer to the next node **)
let _ = (if (idx = 0)
then
(L.build_store node_p list_head builder)
else let p = L.build_struct_gep prev_node 1 "temp" builder in
L.build_store node_p p builder) in
link_list (idx + 1) es node_p) in
let _ = link_list 0 ses (L.const_pointer_null list_node) in
(* let _ = L.dump_value (L.build_load list_head "temp" builder) in *)
L.build_load list_head "temp" builder
| SDotCall(ds_name, mname, args) ->
(match mname with
"addNode" -> add_node_def (builder, stable) styp ds_name args
| "addEdge" -> add_edge_def (builder, stable) styp ds_name args
| "getByName" -> get_by_name_def (builder, stable) ds_name args
| "nameExists" -> check_name_exists_def (builder, stable) ds_name args
| "getAllEdges" -> get_all_edges_def (builder, stable) ds_name args
| "getAllNodes" -> get_all_nodes_def (builder, stable) ds_name args
| "nodeExists" -> node_exists_def (builder, stable) ds_name args
| "edgeExists" -> edge_exists_def (builder, stable) ds_name args
| "removeNode" -> remove_node_def (builder, stable) ds_name args
| "removeEdge" -> remove_edge_def (builder, stable) ds_name args
| "getEdgesOf" -> get_edges_of_def (builder, stable) ds_name args
| "set" -> array_set_def (builder, stable) ((List_t, SId(ds_name))::args)
| "add" -> array_add_def (builder, stable) ((List_t, SId(ds_name))::args)
| "get" -> array_get_def (builder, stable) ((List_t, SId(ds_name))::args)
| "len" -> array_len_def (builder, stable) ((List_t, SId(ds_name))::args)
| "del" -> array_del_def (builder, stable) ((List_t, SId(ds_name))::args)
| _ -> raise (Failure ("invalid methods")))
| SEdge(n1, n2) ->
let n1' = expr (builder, stable) n1 in
let n2' = expr (builder, stable) n2 in
let edge_struct = L.build_malloc edge_struct "myEdgeStruct" builder in
let src = Llvm.build_struct_gep edge_struct 0 "name'" builder in
let dst = Llvm.build_struct_gep edge_struct 1 "name'" builder in
let weight = Llvm.build_struct_gep edge_struct 2 "name'" builder in
let _ = L.build_store n1' src builder in
let _ = L.build_store n2' dst builder in
let initWeight = L.const_int i32_t 0 in
let _ = L.build_store initWeight weight builder in
edge_struct
(*** begin built-in func defs ***)
(** need to traverse the array for as many steps as indicated in the
LLVM value i. Note the similarity to while loops, but we create
a custom predicate and body instead **)
and traverse_isteps i list (builder, stable) =
(** some constants that will be used to decrement counter i **)
let zeroval = L.const_int i32_t 0 in
let oneval = L.const_int i32_t 1 in
let currnode = L.build_malloc (L.pointer_type list_node) "" builder in
let iter = L.build_malloc i32_t "" builder in
let _ = L.build_store list currnode builder in
let _ = L.build_store i iter builder in
let _ = L.const_pointer_null (L.pointer_type list_node) in
let (_, currLLVMfunc) = find_func stable stable.curr_func in
let pred_bb = L.append_block context "traverse_loop" currLLVMfunc in
let _ = L.build_br pred_bb builder in
let body_bb = L.append_block context "while_body" currLLVMfunc in
(** body of traverse is stepping through linked list **)
let bb = L.builder_at_end context body_bb in
let temp = L.build_load (L.build_struct_gep (L.build_load currnode "" bb) 1 "temp" bb) "temp" bb in
(* let _ = L.build_store temp next_ptr bb in *)
let _ = L.build_store (L.build_sub (L.build_load iter "" bb) oneval "subtract" bb) iter bb in
let _ = L.build_store temp currnode bb in
let () = add_terminal bb (L.build_br pred_bb) in
(** check if i has been brought down to 0, indicating finished traversal **)
let pred_builder = L.builder_at_end context pred_bb in
let bool_val = L.build_icmp L.Icmp.Ne (L.build_load iter "" pred_builder) zeroval "temp" pred_builder in
(* let bool_val = L.build_icmp L.Icmp.Ne oneval zeroval "" pred_builder in *)
let merge_bb = L.append_block context "merge" currLLVMfunc in
(* let _ = L.build_cond_br bool_val body_bb merge_bb pred_builder in *)
let _ = L.build_cond_br bool_val body_bb merge_bb pred_builder in
let _ = L.position_at_end merge_bb builder in
(* L.build_load next_ptr "" builder *)
currnode
and count_steps list (builder, stable) =
let currnode = L.build_malloc (L.pointer_type list_node) "" builder in
let zeroval = L.const_int i32_t 0 in
let oneval = L.const_int i32_t 1 in
let iter = L.build_malloc i32_t "" builder in
let _ = L.build_store (L.const_int i32_t 0) iter builder in
let _ = L.build_store list currnode builder in
let (_, currLLVMfunc) = find_func stable stable.curr_func in
(* let start_bb = L.insertion_block builder in *)
let bool_val_if = L.build_is_null list "" builder in
let merge_bb_if = L.append_block context "merge" currLLVMfunc in
let branch_instr = L.build_br merge_bb_if in
let then_bb_if = L.append_block context "then" currLLVMfunc in
let then_builder_if = L.builder_at_end context then_bb_if in
let _ = L.build_store zeroval iter then_builder_if in
(* let _ = L.build_call printf_func [| int_format_str then_builder_if ; (L.const_int i32_t 69) |] "printf" then_builder_if in *)
(* let _ = L.build_call printf_func [| int_format_str builder ; (L.const_int i32_t 69) |] "printf" in *)
let () = add_terminal then_builder_if branch_instr in
let else_bb_if = L.append_block context "else" currLLVMfunc in
let else_builder_if = L.builder_at_end context else_bb_if in
let pred_bb = L.append_block context "traverse_loop" currLLVMfunc in
let _ = L.build_br pred_bb else_builder_if in
let body_bb = L.append_block context "while_body" currLLVMfunc in
(** body of traverse is stepping through linked list **)
let bb = L.builder_at_end context body_bb in
let temp = L.build_load (L.build_struct_gep (L.build_load currnode "" bb) 1 "temp" bb) "temp" bb in
(* let _ = L.build_store temp next_ptr bb in *)
let _ = L.build_store (L.build_add (L.build_load iter "" bb) oneval "add" bb) iter bb in
let _ = L.build_store temp currnode bb in
let () = add_terminal bb (L.build_br pred_bb) in
(** check if i has been brought down to 0, indicating finished traversal **)
let pred_builder = L.builder_at_end context pred_bb in
let bool_val = L.build_is_not_null (L.build_load currnode "" pred_builder) "" pred_builder in
let merge_bb = L.append_block context "merge" currLLVMfunc in
let m_build = L.builder_at_end context merge_bb in
let () = add_terminal m_build (L.build_br merge_bb_if) in
let _ = L.build_cond_br bool_val body_bb merge_bb pred_builder in
(* let _ = L.position_at_end merge_bb builder in *)
let () = add_terminal else_builder_if branch_instr in
let _ = L.build_cond_br bool_val_if then_bb_if else_bb_if builder in
let _ = L.position_at_end merge_bb_if builder in
iter
and array_del_def (builder, stable) args =
(match args with
(typ, SId(list_id)) :: index :: [] ->
let list_dp = find_variable stable list_id in
let list_p = L.build_load list_dp "list" builder in
let i = expr (builder, stable) index in
let currnode = L.build_malloc (L.pointer_type list_node) "" builder in
let zero = L.const_int i32_t 0 in
let oneval = L.const_int i32_t 1 in
let iter = L.build_malloc i32_t "" builder in
let _ = L.build_store (L.const_int i32_t 0) iter builder in
let _ = L.build_store list_p currnode builder in
let (_, currLLVMfunc) = find_func stable stable.curr_func in
(* let start_bb = L.insertion_block builder in *)
let bool_val_if = L.build_icmp L.Icmp.Eq i zero "equaltozero" builder in
let merge_bb_if = L.append_block context "merge" currLLVMfunc in
let branch_instr = L.build_br merge_bb_if in
let then_bb_if = L.append_block context "then" currLLVMfunc in
let then_builder_if = L.builder_at_end context then_bb_if in
let delcurnextp = L.build_struct_gep (L.build_load currnode "" then_builder_if) 1 "temp" then_builder_if in
let delcurnext = L.build_load delcurnextp "" then_builder_if in
let _ = L.build_store delcurnext list_dp then_builder_if in
let () = add_terminal then_builder_if branch_instr in
let else_bb_if = L.append_block context "else" currLLVMfunc in
let else_builder_if = L.builder_at_end context else_bb_if in
let idxminusone = L.build_malloc i32_t "" else_builder_if in
let _ = L.build_store (L.build_sub i oneval "" else_builder_if) idxminusone else_builder_if in
let idxplusone = L.build_malloc i32_t "" else_builder_if in
let _ = L.build_store (L.build_add i oneval "" else_builder_if) idxplusone else_builder_if in
let beforev = traverse_isteps (L.build_load idxminusone "" else_builder_if) list_p (else_builder_if, stable) in
let before = L.build_load beforev "" else_builder_if in
let beforeptr = L.build_struct_gep before 1 "temp" else_builder_if in
let targetv = traverse_isteps (L.build_load idxplusone "" else_builder_if) list_p (else_builder_if, stable) in
let target = L.build_load targetv "" else_builder_if in
let curnext = L.build_struct_gep target 1 "temp" else_builder_if in
(* let currnextv = L.build_load curnext "" else_builder_if in *)
let _ = L.build_store (L.build_load curnext "" else_builder_if) beforeptr else_builder_if in
let () = add_terminal else_builder_if branch_instr in
let _ = L.build_cond_br bool_val_if then_bb_if else_bb_if builder in
let _ = L.position_at_end merge_bb_if builder in
list_dp
| _ -> raise (Failure "wrong args to array_delete"))
and array_get_def (builder, stable) args =
(match args with
(typ, SId(list_id)) :: index :: [] ->
let list_dp = find_variable stable list_id in
(* let _ = L.dump_value list_dp in *)
let list_p = L.build_load list_dp "list" builder in
let idx = expr (builder, stable) index in
let targetv = traverse_isteps idx list_p (builder, stable) in
let target = L.build_load targetv "" builder in
let targetptr = L.build_struct_gep target 0 "temp" builder in
let value = L.build_load targetptr "retval" builder in
value
| _ -> raise (Failure "wrong args to array_get"))
and array_set_def (builder, stable) args =
match args with
(typ, SId(list_id)) :: index :: (vtyp, exp) :: [] ->
let llvm_val = expr (builder, stable) (vtyp, exp) in
let llvm_ptr = L.build_malloc (ltype_of_typ vtyp) "arr_val" builder in
let _ = L.build_store llvm_val llvm_ptr builder in
let llvm_i8 = L.build_pointercast llvm_ptr (L.pointer_type i8_t) "i8ptr" builder in
let list_dp = find_variable stable list_id in
let list_p = L.build_load list_dp "list" builder in
let idx = expr (builder, stable) index in
let targetv = traverse_isteps idx list_p (builder, stable) in
let target = L.build_load targetv "" builder in
let targetptr = L.build_struct_gep target 0 "temp" builder in
L.build_store llvm_i8 targetptr builder
| _ -> raise (Failure "wrong args to array_set")
and array_len_def (builder, stable) args =
match args with
(typ, SId(list_id)) :: [] ->
let list_dp = find_variable stable list_id in
let list_p = L.build_load list_dp "list" builder in
let counterp = count_steps list_p (builder, stable) in
let counter = L.build_load counterp "" builder in
counter
| _ -> raise (Failure "wrong args to array_set")
and array_add_def (builder, stable) args =
match args with
(typ, SId(list_id)) :: index :: (vtyp, exp) :: [] ->
let llvm_val = expr (builder, stable) (vtyp, exp) in
let llvm_ptr = L.build_malloc (ltype_of_typ vtyp) "arr_val" builder in
let _ = L.build_store llvm_val llvm_ptr builder in
let zeroval = L.const_int i32_t 0 in
let oneval = L.const_int i32_t 1 in
let array_node = L.const_named_struct list_node [| L.const_pointer_null (L.pointer_type i8_t); L.const_pointer_null (L.pointer_type list_node); |] in
(** generalize the value to an i8_t pointer **)
let llvm_i8 = L.build_pointercast llvm_ptr (L.pointer_type i8_t) "i8ptr" builder in
(** create the node variable **)
let node_p = L.build_malloc list_node "node_p" builder in
let val_ptr = L.build_struct_gep node_p 0 "valloc" builder in
let _ = L.build_store array_node node_p builder in
let _ = L.build_store llvm_i8 val_ptr builder in
(** get the list **)
let list_dp = find_variable stable list_id in
let list_p = L.build_load list_dp "list" builder in
let idx = expr (builder, stable) index in
let idxminusone = L.build_malloc i32_t "" builder in
let _ = L.build_store (L.build_sub idx oneval "" builder) idxminusone builder in
(** if you want to add to beginning of list **)
let (_, currLLVMfunc) = find_func stable stable.curr_func in
(* let start_bb = L.insertion_block builder in *)
let bool_val = L.build_icmp L.Icmp.Eq zeroval idx "" builder in
let merge_bb = L.append_block context "merge" currLLVMfunc in
let branch_merge = L.build_br merge_bb in
let then_bb = L.append_block context "then" currLLVMfunc in
let then_builder = L.builder_at_end context then_bb in
let curnextptr = L.build_struct_gep node_p 1 "" then_builder in (**TODO: this line is a guestimate :o*)
let _ = L.build_store list_p curnextptr then_builder in (**TODO: this line is a guestimate :o*)
let _ = L.build_store node_p list_dp then_builder in (**TODO: this line is a guestimate :o*)
(* let (then_builder, _) = stmt ((L.builder_at_end context then_bb), stable) then_stmt in *)
let () = add_terminal then_builder branch_merge in
let else_bb = L.append_block context "else" currLLVMfunc in
let else_builder = L.builder_at_end context else_bb in
let beforev = traverse_isteps (L.build_load idxminusone "" else_builder) list_p (else_builder, stable) in
let before = L.build_load beforev "" else_builder in
let beforeptr = L.build_struct_gep before 1 "temp" else_builder in
let _ = L.build_store node_p beforeptr else_builder in
let targetv = traverse_isteps idx list_p (else_builder, stable) in
let target = L.build_load targetv "" else_builder in
let curnext = L.build_struct_gep node_p 1 "temp" else_builder in
let _ = L.build_store target curnext else_builder in
let () = add_terminal else_builder branch_merge in
let _ = L.build_cond_br bool_val then_bb else_bb builder in
let _ = L.position_at_end merge_bb builder in
list_p
(* let beforev = traverse_isteps idx list_p (builder, stable) in
let before = L.build_load beforev "" builder in
let iplus1 = L.build_malloc i32_t "iter" builder in
let _ = L.build_store (L.build_add idx oneval "temp" builder) iplus1 builder in
let afterv = traverse_isteps (L.build_load iplus1 "" builder) list_p (builder, stable) in
let after = L.build_load afterv "" builder in
let before_next = L.build_struct_gep before 1 "next" builder in
let _ = L.build_store node_p before_next builder in
if (L.is_null after)
then list_dp
else let _ = L.build_store after (L.build_struct_gep node_p 1 "next" builder) builder in list_dp *)
| _ -> raise (Failure "wrong args to array_add")
and get_all_nodes_def (builder, stable) ds_name lst =
let ds = find_variable stable ds_name in
let ds' = L.build_load ds "graph_ptr" builder in
let nodesList = L.define_global "nodesList" (L.const_pointer_null (list_t)) the_module in
let llNodesPtr = L.build_struct_gep ds' 0 "nodes" builder in
let llNodes = L.build_load llNodesPtr "head" builder in
let llNodes' = L.define_global "llNodes" (L.const_pointer_null (L.pointer_type node_node)) the_module in
let _ = L.build_store llNodes llNodes' builder in
let (_, currLLVMfunc) = find_func stable stable.curr_func in
(* is the curr node null? *)
let pred_bb = L.append_block context "while" currLLVMfunc in
let _ = L.builder_at_end context pred_bb in
let pred_builder = L.builder_at_end context pred_bb in
let x = L.build_load llNodes' "putMeHere" pred_builder in
let bool_val = L.build_is_not_null x "curr" pred_builder in
(* body of the while, including the if/else *)
let body_bb = L.append_block context "while_body" currLLVMfunc in
let body_builder = L.builder_at_end context body_bb in
(* conditional for if currNode*)
let load_struct_ptr = L.build_load llNodes' "putMeHere" body_builder in
let currNodePtr = L.build_struct_gep load_struct_ptr 0 "nodes" body_builder in
let currNode = L.build_load currNodePtr "stored_nodeA'" body_builder in
(* labelling the blocks for if else *)
let then_bb = L.append_block context "then" currLLVMfunc in
let else_bb = L.append_block context "else" currLLVMfunc in
let merge_bb = L.append_block context "merge" currLLVMfunc in
let _ = L.build_br then_bb body_builder in
(* then basic block and builder that returns a true if found *)
let then_builder = L.builder_at_end context then_bb in
(*let ret_ptr = L.build_alloca i1_t "ret_true" then_builder in*)
let curr_node = L.build_malloc list_node "node" then_builder in
let curr_node_ptr_ptr = L.build_struct_gep curr_node 0 "nodes" then_builder in
let curr_node_next_ptr = L.build_struct_gep curr_node 1 "nodes" then_builder in
let nodeForList = L.build_malloc node_t "please_work" then_builder in
let _ = L.build_store currNode nodeForList then_builder in
let currNodeVoidPtr = L.build_pointercast nodeForList (L.pointer_type i8_t) "currNodeVoidPtr" then_builder in
let _ = L.build_store currNodeVoidPtr curr_node_ptr_ptr then_builder in
let next = L.build_load nodesList "nextNodes" then_builder in
let _ = L.build_store next curr_node_next_ptr then_builder in
let _ = L.build_store curr_node nodesList then_builder in
let _ = L.build_br else_bb then_builder in
(*replaced return with below *) (*let _ = L.build_ret ret_ptr then_builder in*)
(*hopefully this builds the proper return instruction *)
(* if we haven't found our node *)
let else_builder = L.builder_at_end context else_bb in
(* get the head of our node linked list *)
let load_struct_ptr = L.build_load llNodes' "toBeHere" else_builder in
let llNodesPtr' = L.build_struct_gep load_struct_ptr 1 "llNodesPtr" else_builder in
let load_next_ptr = L.build_load llNodesPtr' "toBeHerenow" else_builder in
let _ = L.build_store load_next_ptr llNodes' else_builder in
let _ = L.build_br pred_bb else_builder in
(* make sure merge bb returns a FALSE if we get to it, i.e. if we did not find the node*)
let _ = L.builder_at_end context merge_bb in
(* go to body if we still have nodes to check out, go to merge cond. if we are out of nodes *)
let _ = L.build_cond_br bool_val body_bb merge_bb pred_builder in
let _ = L.build_br pred_bb builder in
let _ = L.position_at_end merge_bb builder in
(* placeholder *)
L.build_load nodesList "return" builder
and check_name_exists_def (builder, stable) ds_name lst =
let to_find = match lst with
x::[] -> x
| _ -> raise (Failure ("too many arguments"))
in
(* added so we can just return once *)
let ret_ptr = L.build_alloca i1_t "ret_true" builder in
let bool = L.const_int (ltype_of_typ A.Bool) 0 in
let _ = L.build_store bool ret_ptr builder in
(* retrieve pointer of node to find and graph to traverse *)
let to_find' = expr (builder, stable) to_find in
let ds = find_variable stable ds_name in
let ds' = L.build_load ds "graph_ptr" builder in
(* get the head of our node linked list *)
let llNodesPtr = L.build_struct_gep ds' 0 "nodes" builder in
let llNodes = L.build_load llNodesPtr "head" builder in
let llNodes' = L.define_global "llNodes" (L.const_pointer_null (L.pointer_type node_node)) the_module in
let _ = L.build_store llNodes llNodes' builder in
let (_, currLLVMfunc) = find_func stable stable.curr_func in
(* is the curr node null? *)
let pred_bb = L.append_block context "while" currLLVMfunc in
let _ = L.builder_at_end context pred_bb in
let pred_builder = L.builder_at_end context pred_bb in
let x = L.build_load llNodes' "putMeHere" pred_builder in
let bool_val = L.build_is_not_null x "curr" pred_builder in
(* body of the while, including the if/else *)
let body_bb = L.append_block context "while_body" currLLVMfunc in
let body_builder = L.builder_at_end context body_bb in
(* conditional for if currNode*)
let load_struct_ptr = L.build_load llNodes' "putMeHere" body_builder in
let currNodePtr = L.build_struct_gep load_struct_ptr 0 "nodes" body_builder in
let currNode = L.build_load currNodePtr "stored_node'" body_builder in
(* labelling the blocks for if else *)
let if_bb = L.append_block context "if" currLLVMfunc in
let then_bb = L.append_block context "then" currLLVMfunc in
let else_bb = L.append_block context "else" currLLVMfunc in
let merge_bb = L.append_block context "merge" currLLVMfunc in
let _ = L.build_br if_bb body_builder in
let if_builder = L.builder_at_end context if_bb in
let namePtr = L.build_struct_gep currNode 0 "namePtr" if_builder in
let nodeName = L.build_load namePtr "nodeName" if_builder in
let if_found_bool_val = L.build_call strcmp_func [| to_find';
nodeName
|] "strcmp" if_builder in
let zero = L.const_int (ltype_of_typ A.Int) 0 in
let equal = L.build_icmp L.Icmp.Eq if_found_bool_val zero "equal" if_builder in
let _ = L.build_cond_br equal then_bb else_bb if_builder in
(* then basic block and builder that returns a true if found *)
let then_builder = L.builder_at_end context then_bb in
let _ = L.build_store equal ret_ptr then_builder in
(*replaced return with below *) (*let _ = L.build_ret ret_ptr then_builder in*)
let _ = L.build_br merge_bb then_builder in
(*hopefully this builds the proper return instruction *)
(* if we haven't found our node *)
let else_builder = L.builder_at_end context else_bb in
let _ = L.build_store equal ret_ptr else_builder in
(* get the head of our node linked list *)
let load_struct_ptr = L.build_load llNodes' "toBeHere" else_builder in
let llNodesPtr' = L.build_struct_gep load_struct_ptr 1 "llNodesPtr" else_builder in
let load_next_ptr = L.build_load llNodesPtr' "toBeHerenow" else_builder in
let _ = L.build_store load_next_ptr llNodes' else_builder in
let _ = L.build_br pred_bb else_builder in
(* make sure merge bb returns a FALSE if we get to it, i.e. if we did not find the node*)
let _ = L.builder_at_end context merge_bb in
(* go to body if we still have nodes to check out, go to merge cond. if we are out of nodes *)
let _ = L.build_cond_br bool_val body_bb merge_bb pred_builder in
let _ = L.build_br pred_bb builder in
let _ = L.position_at_end merge_bb builder in
L.build_load ret_ptr "toRtrn" builder
and get_by_name_def (builder, stable) ds_name lst =
let to_find = match lst with
x::[] -> x
| _ -> raise (Failure ("too many arguments"))
in
(*let ret_nd = L.build_alloca ()*)
let ret_nd = L.build_malloc (L.pointer_type node_struct) "ret_nd" builder in
let _ = L.build_store (L.const_pointer_null (L.pointer_type node_struct)) ret_nd builder in
(*let loaded_null_ptr = L.build_load nullPtr "null_ptr_loaded" builder in
let _ = L.build_store loaded_null_ptr ret_nd builder in*)
(* retrieve pointer of node to find and graph to traverse *)
let to_find' = expr (builder, stable) to_find in
let ds = find_variable stable ds_name in
let ds' = L.build_load ds "graph_ptr" builder in
(* get the head of our node linked list *)