-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodegen.ml
694 lines (612 loc) · 36.8 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
open Ast
open Printf
open Symbol
open Llvm
open Llvm_ipo
open Llvm_vectorize
open Llvm_analysis
open Llvm_scalar_opts
open Llvm_all_backends
(*open Llvm.PassManager*)
type var_type =
| Value
| Pointer (* Ptr_value or Ptr_array_head *)
| Array
| Ptr_array
| Fun
type entry =
| Efun of func_decl * Llvm.llvalue
| Evar of var * (Llvm.llvalue -> Llvm.llvalue) * var_type
type llvm_info = {
context : Llvm.llcontext;
the_module : Llvm.llmodule;
builder : Llvm.llbuilder;
i8 : Llvm.lltype;
i32 : Llvm.lltype;
i64 : Llvm.lltype;
c32 : int -> Llvm.llvalue;
c64 : int -> Llvm.llvalue;
funcs : Llvm.llvalue list ref;
built_in_table : (string, entry) Hashtbl.t;
count_funs : int ref;
count_strings : int ref;
}
let string_table : (string, Llvm.llvalue) Hashtbl.t = Hashtbl.create 10
let lookup_string stringkey =
try Some (Hashtbl.find string_table stringkey)
with Not_found -> None
let insert_string stringkey llval =
if Hashtbl.mem string_table stringkey then
failwith "insert_string"
else
Hashtbl.add string_table stringkey llval
(*
symbol table:
- Key: a string that is the name of the variable being looked up.
- Value: a function that knows which entry of the activation record
corresponds to the identifier that is being looked up. It is
basically a "getter".
*)
let symbol_table : (string, entry) Hashtbl.t list ref = ref []
(*
fun_refs:
- Key: an llvalue that represents a function.
- Value: a list of booleans that corresponds to the function's parameters.
true if the parameter is passed by reference, otherwise false.
*)
let fun_refs : (Llvm.llvalue, var_type list) Hashtbl.t = Hashtbl.create 10
let fun_decls : func_decl list ref = ref []
(*
struct_types:
A list of the structs that have been created in order to
accomodate nested functions. Click on the following link for an explanation:
https://stackoverflow.com/questions/55736390/llvm-how-to-make-a-nested-function-see-an-outside-functions-variables
*)
let struct_types : lltype option list ref = ref []
let activation_records : llvalue option list ref = ref []
(* ------------------------------------------------- *)
let get_head lst who =
try List.hd lst with Failure(str) -> failwith who
let open_scope () =
symbol_table := (Hashtbl.create 10) :: !symbol_table
let close_scope () =
symbol_table := List.tl !symbol_table
let current_scope () =
get_head !symbol_table "current_scope"
(*
Lookup function returns a tuple of (llvalue->llvalue , int) option
The integer represent the depth of the scope that the variable/function was declared
Specific integer values: -1 (means global scope)
*)
let lookup info id =
let rec walk id st n =
match st with
| [] -> raise Not_found
| cs::[] -> (try (* remember that the end of the list/stack is the global scope *)
Some (Hashtbl.find cs id, -1)
with Not_found -> raise Not_found)
| cs::scopes -> (try
Some (Hashtbl.find cs id, n)
with Not_found -> walk id scopes (n+1))
in
try walk id !symbol_table 0
with Not_found -> try Some(Hashtbl.find info.built_in_table id, -1) with Not_found -> None
let lookup_head info id =
try
Some (Hashtbl.find (current_scope ()) id, 0)
with Not_found -> None
(* REMEMBER: check that ids dont confict with fix fun ids eg print *)
let insert id llval =
if Hashtbl.mem (current_scope ()) id then
failwith "insert"
else
Hashtbl.add (current_scope ()) id llval
let remove_head id =
Hashtbl.remove (current_scope ()) id
let rec get_struct_param (f:func_decl) lst =
match !(!(f.depend)), lst with
| Some(1,_), Some(hd)::tl -> hd
| _ , hd::tl -> get_struct_param (match !(!(f.father_func)) with Some(x) -> x | None -> failwith "get_struct_param") tl
| _ , [] -> failwith "get_struct_param"
(* ------------------------------------------------- *)
let get_type atype ref =
match atype, ref with
| EInteger([]) , false -> Value
| EInteger([]) , true -> Pointer
| EInteger(_) , false -> Array
| EInteger(-1::tl) , true -> Ptr_array
| EInteger(_) , true -> Pointer
| ECharacter([]) , false -> Value
| ECharacter([]) , true -> Pointer
| ECharacter(_) , false -> Array
| ECharacter(-1::tl) , true -> Ptr_array
| ECharacter(_) , true -> Pointer
| _ -> failwith "get_type"
(* the following functions are helpers for handling expressions *)
let codegen_int info i = info.c32 i
let codegen_char info c = Llvm.const_int (Llvm.i8_type info.context) (Char.code c)
let codegen_uop info oper expr =
match oper with
| UnopPlus -> expr
| UnopMinus -> Llvm.build_neg expr "negtmp" info.builder
let codegen_bop info oper expr1 expr2 =
match oper with
| BopAdd -> Llvm.build_add expr1 expr2 "addtmp" info.builder
| BopSub -> Llvm.build_sub expr1 expr2 "subtmp" info.builder
| BopMul -> Llvm.build_mul expr1 expr2 "multmp" info.builder
| BopDiv -> Llvm.build_sdiv expr1 expr2 "divtmp" info.builder
| BopMod -> Llvm.build_srem expr1 expr2 "modtmp" info.builder
let codegen_type info atype ref =
let rec walk atype =
match atype with
| EInteger(lst) -> (match lst with
| [] -> info.i32
| hd::tl -> Llvm.array_type (walk (EInteger(tl))) hd)
| ECharacter(lst) -> (match lst with
| [] -> Llvm.i8_type info.context
| hd::tl -> Llvm.array_type (walk (ECharacter(tl))) hd)
| _ -> failwith "codegen_type"
in match atype with
| EInteger(lst) -> (match lst with
| [] -> if ref then Llvm.pointer_type info.i32 else info.i32
| -1::tl -> Llvm.pointer_type (walk (EInteger(tl)))
| hd::tl -> if ref then Llvm.pointer_type (walk (EInteger(lst))) else walk (EInteger(lst)))
| ECharacter(lst) -> (match lst with
| [] -> if ref then Llvm.pointer_type (Llvm.i8_type info.context) else Llvm.i8_type info.context
| -1::tl -> Llvm.pointer_type (walk (ECharacter(tl)))
| hd::tl -> if ref then Llvm.pointer_type (walk (ECharacter(lst))) else walk (ECharacter(lst)))
| EString -> failwith "codegen_type"
| ENothing -> Llvm.void_type info.context
let id_get_llvalue info id =
let rec walk (decl:func_decl) ac_link depth =
let father_decl = match !(!(decl.father_func)) with Some(x) -> x | _ -> failwith "id_get_llvalue"
in match !(!(decl.depend)), depth with
| _ , 1 -> ac_link
| Some(1,_) , _ -> walk father_decl (let prev_ac_rec = Llvm.build_struct_gep ac_link 0 "prev_acc_link" info.builder
in Llvm.build_load prev_ac_rec "prev_ac_rec" info.builder) (depth-1)
| Some(_,_) , _ -> walk father_decl ac_link (depth-1)
| _ , _ -> failwith "id_get_llvalue"
in let func_decl = get_head !fun_decls "id_get_llvalue"
in let parent_acc_link = Llvm.param (get_head !(info.funcs) "id_get_llvalue") 0 (* check code line for potential hazard*)
in match lookup info id with
| Some(Efun(_,llval),i) -> (llval,Fun)
| Some(Evar(_,llval,vtype),-1) -> (llval (info.c32 0),vtype)
| Some(Evar(_,llval,vtype),0) -> (llval (match get_head !activation_records "id_get_llvalue" with Some(x) -> x | None -> (info.c32 0)),vtype)
| Some(Evar(_,llval,vtype),i) -> (llval (walk func_decl parent_acc_link i),vtype)
| _ -> failwith "id_get_llvalue"
let fun_get_struct_ptr info id =
let rec walk (decl:func_decl) ac_link depth =
let father_decl = match !(!(decl.father_func)) with Some(x) -> x | _ -> failwith "fun_get_struct_ptr"
in match !(!(decl.depend)), depth with
| _ , 1 -> ac_link
| Some(1,_) , _ -> walk father_decl (let prev_ac_rec = Llvm.build_struct_gep ac_link 0 "prev_acc_link" info.builder
in Llvm.build_load prev_ac_rec "prev_ac_rec" info.builder) (depth-1)
| Some(_,_) , _ -> walk father_decl ac_link (depth-1)
| _ , _ -> failwith "fun_get_struct_ptr"
in let func_decl = get_head !fun_decls "func_decl"
in let parent_acc_link = Llvm.param (get_head !(info.funcs) "fun_get_struct_ptr") 0
in match lookup info id with
| Some(_,-1) -> (fun x -> x)
| Some(Efun(decl,_),0) -> (match !(!(decl.depend)) with
| None -> (fun x -> x)
| Some(1,_) -> let curr_ac_rec = (match get_head !activation_records "fun_get_struct_ptr" with Some(x) -> x | _ -> failwith "fun_get_struct_ptr")
in (fun x -> curr_ac_rec::x)
| Some(d,_) -> (fun x -> (walk func_decl parent_acc_link (d-1))::x))
| Some(Efun(decl,_),i) -> (match !(!(decl.depend)) with
| None -> (fun x -> x)
| Some(d,_) -> (fun x -> (walk func_decl parent_acc_link (d+i-1))::x))
| _ -> failwith "fun_get_struct_ptr"
let rec codegen_lval_for_array info lval index_expr =
match lval with
| EAssId(id,_) -> let llval,vtype = id_get_llvalue info id in
let index = codegen_expr info index_expr in
if vtype=Array then (Llvm.build_gep llval [| info.c32 0; index |] "pointer" info.builder)
else if vtype=Ptr_array then (let llval = Llvm.build_load llval "lval_tmp" info.builder
in Llvm.build_gep llval [| index |] "pointer" info.builder)
else (let llval = Llvm.build_load llval "lval_tmp" info.builder
in let llval = Llvm.build_gep llval [| info.c32 0 |] "pointer" info.builder
in Llvm.build_gep llval [| info.c32 0; index |] "pointer" info.builder)
| EAssArrEl(lval,expr,_) -> let llval = codegen_lval_for_array info lval expr
in let index = codegen_expr info index_expr
in Llvm.build_gep llval [| info.c32 0; index |] "pointer" info.builder
| EAssString(str,_) -> match lookup_string str with
| Some(str_el_ptr) -> let index = codegen_expr info index_expr in
Llvm.build_gep str_el_ptr [| index |] "pointer" info.builder
| None -> let str_type = Llvm.array_type info.i8 (1 + String.length str) in
let the_str = Llvm.declare_global str_type ("string_$" ^ (string_of_int !(info.count_strings))) info.the_module in
insert_string str the_str; (* insert the string llvalue to the table *)
info.count_strings := !(info.count_strings)+1;
Llvm.set_linkage Llvm.Linkage.Private the_str;
Llvm.set_global_constant true the_str;
Llvm.set_initializer (Llvm.const_stringz info.context str) the_str;
Llvm.set_alignment 1 the_str;
let index = codegen_expr info index_expr in
Llvm.build_gep the_str [| info.c32 0; index |] "" info.builder
and codegen_lval_load info lval =
match lval with
| EAssId(id,_) -> let llval,vtype = id_get_llvalue info id in
let llval = Llvm.build_load llval "lval_tmp" info.builder in
if vtype=Pointer then Llvm.build_load llval "lval_tmp" info.builder else llval
| EAssArrEl(lval,expr,_) -> let llval = codegen_lval_for_array info lval expr
in Llvm.build_load llval "lval_tmp" info.builder
| EAssString(str,_) -> failwith "codegen_lval_load EAssString"
and codegen_expr info ast =
match ast with
| EInt(i, _) -> codegen_int info i
| EChar(c, _) -> codegen_char info c
| ELVal(lval, _) -> codegen_lval_load info lval
| EFuncCall(id, params, _) -> codegen_call_func info id params
| EUnOp(op, expr, _) -> begin
let e = codegen_expr info expr in
codegen_uop info op e
end
| EBinOp(op, expr1, expr2, _) -> begin
let e1 = codegen_expr info expr1
and e2 = codegen_expr info expr2 in
codegen_bop info op e1 e2
end
and codegen_call_func info id params =
let codegen_param_ref info lval target_type =
match lval with
| EAssId(id,_) -> let llval,vtype = id_get_llvalue info id in
if vtype==target_type then Llvm.build_load llval "lval_tmp" info.builder
else if target_type=Pointer then llval
else Llvm.build_gep llval [| info.c32 0; info.c32 0 |] "pointer" info.builder
| EAssString(str,_) -> (match lookup_string str with
| Some(str_el_ptr) -> if target_type==Array then Llvm.build_load str_el_ptr "lval_tmp" info.builder
else Llvm.build_gep str_el_ptr [| info.c32 0; info.c32 0 |] "" info.builder
| None -> let str_type = Llvm.array_type info.i8 (1 + String.length str) in
let the_str = Llvm.declare_global str_type ("string_$" ^ string_of_int !(info.count_strings)) info.the_module in
info.count_strings := !(info.count_strings)+1;
insert_string str the_str; (* insert the string llvalue to the table *)
Llvm.set_linkage Llvm.Linkage.Private the_str;
Llvm.set_global_constant true the_str;
Llvm.set_initializer (Llvm.const_stringz info.context str) the_str;
Llvm.set_alignment 1 the_str;
if target_type==Array then (* ponder whether == or = *)
Llvm.build_load the_str "lval_tmp" info.builder
else Llvm.build_gep the_str [| info.c32 0; info.c32 0 |] "" info.builder)
| EAssArrEl(lval,expr,_) -> let llval = codegen_lval_for_array info lval expr
in if target_type=Pointer then llval
else Llvm.build_gep llval [| info.c32 0; info.c32 0 |] "pointer" info.builder
in let rec walk params ref_lst =
(let get_llval param target_type =
if (target_type = Value) then (codegen_expr info param)
else (match param with
| ELVal(lval, _) -> codegen_param_ref info lval target_type
| _ -> failwith "codegen_call_func") in
match params, ref_lst with
| [], [] -> []
| param::rest_params, ref::rest_refs -> let llval = (get_llval param ref) in llval :: (walk rest_params rest_refs)
| _, _ -> failwith "codegen_call_func") in
let func,_ = id_get_llvalue info id in
let fix = fun_get_struct_ptr info id in
let ref_lst = (try Hashtbl.find fun_refs func
with Not_found -> failwith "codegen_call_func") in
Llvm.build_call func (Array.of_list (fix (walk params ref_lst))) "" info.builder
(* the following functions are helpers for handling statements *)
let codegen_ret info =
ignore (Llvm.build_ret_void info.builder);
let ret_bb = append_block info.context "after_ret" (get_head !(info.funcs) "codegen_ret") in
position_at_end ret_bb info.builder
let codegen_retval info expr =
ignore (Llvm.build_ret (codegen_expr info expr) info.builder);
let ret_bb = append_block info.context "after_ret" (get_head !(info.funcs) "codegen_retval") in
position_at_end ret_bb info.builder
let rec codegen_cond info cond f cond_bb true_bb false_bb =
Llvm.position_at_end cond_bb info.builder;
match cond with
| ELbop(oper, cond1, cond2, _) -> begin
(* binary logical operators are short circuited *)
match oper with
| LbopAnd -> begin
let no_short_bb = Llvm.append_block info.context "and_no_short" f in
Llvm.position_at_end no_short_bb info.builder;
ignore (codegen_cond info cond2 f no_short_bb true_bb false_bb);
Llvm.position_at_end cond_bb info.builder;
codegen_cond info cond1 f cond_bb no_short_bb false_bb
end
| LbopOr -> begin
let no_short_bb = Llvm.append_block info.context "or_no_short" f in
Llvm.position_at_end no_short_bb info.builder;
ignore (codegen_cond info cond2 f no_short_bb true_bb false_bb);
Llvm.position_at_end cond_bb info.builder;
codegen_cond info cond1 f cond_bb true_bb no_short_bb
end
end
| ELuop(oper, cond1, _) -> begin
(codegen_cond info cond1 f cond_bb false_bb true_bb)
end
| EComp(oper, expr1, expr2, _) -> begin
let e1 = codegen_expr info expr1
and e2 = codegen_expr info expr2 in
let c = match oper with
| CompEq -> Llvm.build_icmp Llvm.Icmp.Eq e1 e2 "equaltmp" info.builder
| CompNeq -> Llvm.build_icmp Llvm.Icmp.Ne e1 e2 "nequaltmp" info.builder
| CompLs -> Llvm.build_icmp Llvm.Icmp.Slt e1 e2 "lesstmp" info.builder
| CompGr -> Llvm.build_icmp Llvm.Icmp.Sgt e1 e2 "greatertmp" info.builder
| CompLsEq -> Llvm.build_icmp Llvm.Icmp.Sle e1 e2 "lesseqtmp" info.builder
| CompGrEq -> Llvm.build_icmp Llvm.Icmp.Sge e1 e2 "greateqtmp" info.builder
in
Llvm.build_cond_br c true_bb false_bb info.builder;
end
let codegen_ass info lval store_expr =
match lval with
| EAssId(id,_) -> let llval,vtype = id_get_llvalue info id in
let llval = if vtype=Pointer then Llvm.build_load llval "lval_tmp" info.builder else llval in
ignore (Llvm.build_store (codegen_expr info store_expr) llval info.builder)
| EAssString(str,_) -> failwith "codegen_ass"
| EAssArrEl(lval,expr,_) -> let llval = codegen_lval_for_array info lval expr
in ignore(Llvm.build_store (codegen_expr info store_expr) llval info.builder)
let rec codegen_stmt info statement =
match statement with
| EEmpty(_) -> ((*do nothing*))
| EBlock(block, _) -> codegen_block info block
| ECallFunc(id, params, _) -> ignore(codegen_call_func info id params)
| EAss(lval, expr, _) -> codegen_ass info lval expr
| EIf(cond, stmt, _) -> codegen_if info cond stmt
| EIfElse(cond, stmt1, stmt2, _) -> codegen_ifelse info cond stmt1 stmt2
| EWhile(cond, stmt, _) -> codegen_while info cond stmt
| ERet(_) -> codegen_ret info
| ERetVal(expr, _) -> codegen_retval info expr
and codegen_block info block =
match block with
| EListStmt(stmt_list, _) -> List.iter (codegen_stmt info) stmt_list
and codegen_if info cond stmt =
let bb = Llvm.insertion_block info.builder in
let f = Llvm.block_parent bb in
let cond_bb = Llvm.append_block info.context "cond" f in
let then_bb = Llvm.append_block info.context "then" f in
let after_bb = Llvm.append_block info.context "after" f in
ignore (Llvm.build_br cond_bb info.builder);
Llvm.position_at_end cond_bb info.builder;
ignore (codegen_cond info cond f cond_bb then_bb after_bb);
Llvm.position_at_end then_bb info.builder;
codegen_stmt info stmt;
ignore (Llvm.build_br after_bb info.builder);
Llvm.position_at_end after_bb info.builder
and codegen_ifelse info cond stmt1 stmt2 =
let bb = Llvm.insertion_block info.builder in
let f = Llvm.block_parent bb in
let cond_bb = Llvm.append_block info.context "cond" f in
let then_bb = Llvm.append_block info.context "then" f in
let else_bb = Llvm.append_block info.context "else" f in
let after_bb = Llvm.append_block info.context "after" f in
ignore (Llvm.build_br cond_bb info.builder);
Llvm.position_at_end cond_bb info.builder;
ignore (codegen_cond info cond f cond_bb then_bb else_bb);
Llvm.position_at_end then_bb info.builder;
codegen_stmt info stmt1;
ignore (Llvm.build_br after_bb info.builder);
Llvm.position_at_end else_bb info.builder;
codegen_stmt info stmt2;
ignore (Llvm.build_br after_bb info.builder);
Llvm.position_at_end after_bb info.builder
and codegen_while info cond stmt =
let bb = Llvm.insertion_block info.builder in
let f = Llvm.block_parent bb in
let cond_bb = Llvm.append_block info.context "cond" f in
let body_bb = Llvm.append_block info.context "body" f in
let after_bb = Llvm.append_block info.context "after" f in
ignore (Llvm.build_br cond_bb info.builder);
Llvm.position_at_end cond_bb info.builder;
ignore (codegen_cond info cond f cond_bb body_bb after_bb);
Llvm.position_at_end body_bb info.builder;
codegen_stmt info stmt;
ignore (Llvm.build_br cond_bb info.builder);
Llvm.position_at_end after_bb info.builder
let rec main_codegen_stmt info statement =
match statement with
| EBlock(block, _) -> main_codegen_block info block
| ERet(_) -> codegen_retval info (EInt(0, {line_start=0;line_end=0;char_start=0;char_end=0}))
| _ -> codegen_stmt info statement
and main_codegen_block info block =
match block with
| EListStmt(stmt_list, _) -> List.iter (main_codegen_stmt info) stmt_list
let codegen_param_type info param =
codegen_type info param.atype param.ref
let codegen_fun_array_args info args (f_decl:func_decl) =
let params = List.map (codegen_param_type info) args
in let final_params = if !(!(f_decl.depend)) <> None then (Llvm.pointer_type (get_struct_param f_decl !struct_types)::params) else params
in Array.of_list final_params
let codegen_activation_record info func ffunc =
let rec get_ac_rec_vars local_defs =
match local_defs with
| [] -> []
| EVarDef(x)::tl -> if !(x.to_ac_rec) then x::(get_ac_rec_vars tl)
else let llval = Llvm.build_alloca (codegen_type info x.atype false) x.id info.builder
in let get_var = (fun _ -> llval)
in insert x.id (Evar(x,get_var,get_type x.atype false)); get_ac_rec_vars tl
| hd::tl -> get_ac_rec_vars tl
in let rec get_ac_rec_params local_defs n =
match local_defs with
| [] -> []
| hd::tl -> if !(hd.to_ac_rec) then hd::(get_ac_rec_params tl (n+1))
else let llval = Llvm.build_alloca (codegen_param_type info hd) hd.id info.builder
in let get_param = (fun _ -> llval) in ignore (Llvm.build_store (Llvm.param ffunc n) llval info.builder);
insert hd.id (Evar({id=hd.id;atype=hd.atype;to_ac_rec=hd.to_ac_rec;pos=hd.pos},get_param,get_type hd.atype hd.ref)); get_ac_rec_params tl (n+1)
in let rec insert_activation_record info ac_record (params:func_args list) (vars:var list) n =
match params, vars with
| [], [] -> ()
(* the following getters take an llvalue and return a pointer to its n-th position *)
(* the llvalue that is going to be passed to it will be the current activation record *)
| [], hd::tl -> let get_var = (fun llval -> Llvm.build_struct_gep llval n "ac_record_arg" info.builder)
in (* no need to store the variables, they will be stored when assignments occur. *)
insert hd.id (Evar(hd,get_var,get_type hd.atype false));
(* insert the getter to the current scope. *)
insert_activation_record info ac_record params tl (n+1)
| hd::tl, _ -> let get_param = (fun llval -> Llvm.build_struct_gep llval n "ac_record_var" info.builder)
in (* don't forget to store the function's parameters. *)
ignore (Llvm.build_store (Llvm.param ffunc n) (get_param ac_record) info.builder);
insert hd.id (Evar({id=hd.id;atype=hd.atype;to_ac_rec=hd.to_ac_rec;pos=hd.pos},get_param,get_type hd.atype hd.ref));
(* insert the getter to the current scope. *)
insert_activation_record info ac_record tl vars (n+1)
(* start creating the activation record *)
in let vars = get_ac_rec_vars func.local_defs
in let params = get_ac_rec_params func.args (if !(func.depend) = None then 0 else 1)
in if !(func.gen_acc_link) then (
(* get the types of the function variables and parameters that will be stored in the activation record *)
let vars_types = List.map (fun (x:var) -> codegen_type info x.atype false) vars
in let params_types = List.map (codegen_param_type info) params
(* create the contents of the activation record and its type *)
in let struct_contents = if !(func.pass_acc_link) then (Llvm.pointer_type (get_struct_param (fun_def2decl func) !struct_types))::(List.append params_types vars_types) else List.append params_types vars_types
in let struct_type = Llvm.struct_type info.context (Array.of_list struct_contents)
in let activation_record = Llvm.build_alloca struct_type "activation_record" info.builder
(* if pass_acc_link flag is set to true, then the activation record will contain
an access link for accessing functions' activation records with lower nesting depth. *)
in if !(func.pass_acc_link) then (let struct_first_element = Llvm.build_struct_gep activation_record 0 "prev_ac_record" info.builder;
in ignore (Llvm.build_store (Llvm.param ffunc 0) struct_first_element info.builder));
insert_activation_record info activation_record params vars (if !(func.pass_acc_link) then 1 else 0);
struct_types := Some(struct_type)::(!struct_types);
activation_records := Some(activation_record)::(!activation_records)
) else (struct_types := None::(!struct_types); activation_records := None::(!activation_records))
let rec codegen_localdef info def =
match def with
| EFuncDef(func) -> let ffunc = match lookup_head info func.id with
| None -> let ffunc_type = Llvm.function_type (codegen_type info func.ret false) (codegen_fun_array_args info func.args (fun_def2decl func)) in
let ffunc = Llvm.declare_function (func.id ^ "_$" ^ (string_of_int !(info.count_funs))) ffunc_type info.the_module in
info.count_funs := !(info.count_funs)+1;
insert func.id (Efun(fun_def2decl func,ffunc));
Hashtbl.add fun_refs ffunc (List.map (fun x -> get_type x.atype x.ref) func.args);
fun_decls := (fun_def2decl func)::!fun_decls; ffunc
| Some(Efun(_,ffunc),_) -> ffunc
| _ -> failwith "codegen_localdef"
in let bb = Llvm.append_block info.context "entry" ffunc in
open_scope ();
Llvm.position_at_end bb info.builder;
codegen_activation_record info func ffunc;
info.funcs := ffunc::!(info.funcs);
List.iter (codegen_localdef info) func.local_defs;
Llvm.position_at_end bb info.builder;
codegen_block info func.body;
(match func.ret with
| ENothing -> ignore (Llvm.build_ret_void info.builder)
| _ -> ignore (Llvm.build_ret (info.c32 0) info.builder));
info.funcs := List.tl !(info.funcs);
struct_types := List.tl !struct_types;
activation_records := List.tl !activation_records;
fun_decls := List.tl !fun_decls;
close_scope ()
| EFuncDecl(func_decl) -> let ffunc_type = Llvm.function_type (codegen_type info func_decl.ret false) (codegen_fun_array_args info func_decl.args func_decl) in
let ffunc = Llvm.declare_function (func_decl.id ^ "_$" ^ (string_of_int !(info.count_funs))) ffunc_type info.the_module in
info.count_funs := !(info.count_funs)+1;
insert func_decl.id (Efun(func_decl,ffunc));
Hashtbl.add fun_refs ffunc (List.map (fun x -> get_type x.atype x.ref) func_decl.args);
fun_decls := func_decl::!fun_decls
| EVarDef(var) -> ()
let rec main_codegen_localdef info def =
match def with
| EFuncDef(func) -> codegen_localdef info def
| EFuncDecl(func_decl) -> codegen_localdef info def
| EVarDef(var) -> (* Initialize global variables *)
let ltype = codegen_type info var.atype false in
let llval = Llvm.declare_global ltype var.id info.the_module in
Llvm.set_linkage Llvm.Linkage.Private llval;
Llvm.set_initializer (Llvm.const_null ltype) llval;
insert var.id (Evar(var,(fun _ -> llval),get_type var.atype false))
let codegen_built_in_decl info (entr:Symbol.entry) =
match entr with
| Efuncdef(decl,used) -> if !used then
let ffunc_type = Llvm.function_type (codegen_type info decl.ret false) (codegen_fun_array_args info decl.args decl) in (* fix array *)
let ffunc = Llvm.declare_function decl.id ffunc_type info.the_module in
Hashtbl.add info.built_in_table decl.id (Efun(decl,ffunc));
Hashtbl.add fun_refs ffunc (List.map (fun x -> get_type x.atype x.ref) decl.args)
else ()
| _ -> failwith "codegen_built_in_decl"
(* define main - compile and dump function *)
let llvm_compile_and_dump main_func optimizations_enable temp_file =
(* Initialize *)
Llvm_all_backends.initialize ();
let context = Llvm.global_context () in
let the_module = Llvm.create_module context "grace program" in
let builder = Llvm.builder context in
let pm = Llvm.PassManager.create () in
let optimizations = [
(* Initial Simplification and Canonicalization *)
Llvm_scalar_opts.add_instruction_combination;
Llvm_scalar_opts.add_cfg_simplification;
(* Early Stage Optimizations *)
Llvm_scalar_opts.add_early_cse; (* Early Common Subexpression Elimination *)
Llvm_ipo.add_function_attrs; (* Function attribute inference *)
Llvm_ipo.add_ipsccp; (* Interprocedural Sparse Conditional Constant Propagation *)
(* Memory and Register Promotions *)
Llvm_scalar_opts.add_memory_to_register_promotion;
(* Dead Code Elimination and Aggregation *)
Llvm_ipo.add_dead_arg_elimination;
Llvm_ipo.add_global_dce; (* Global Dead Code Elimination *)
Llvm_scalar_opts.add_dead_store_elimination; (* Dead Store Elimination *)
(* Constant Propagation and Folding *)
Llvm_scalar_opts.add_sccp; (* Sparse Conditional Constant Propagation *)
(* Loop Simplification and Analysis *)
Llvm_scalar_opts.add_loop_rotation;
Llvm_scalar_opts.add_ind_var_simplification; (* Induction Variable Simplification *)
Llvm_scalar_opts.add_loop_idiom; (* Loop Idiom Recognition *)
(* Mid-Stage Optimizations *)
Llvm_scalar_opts.add_instruction_combination;
Llvm_scalar_opts.add_reassociation;
Llvm_scalar_opts.add_gvn; (* Global Value Numbering *)
(* Loop Optimizations *)
Llvm_scalar_opts.add_licm; (* Loop Invariant Code Motion *)
Llvm_scalar_opts.add_loop_unswitch; (* Unswitch loops with loop-invariant conditionals *)
Llvm_scalar_opts.add_loop_unroll; (* Unroll loops to enable further optimizations *)
Llvm_scalar_opts.add_loop_deletion; (* Delete empty loops *)
(* Function Inlining and Tail Calls *)
Llvm_ipo.add_function_inlining; (* Inline small functions *)
Llvm_scalar_opts.add_tail_call_elimination;
(* Advanced Optimizations *)
Llvm_scalar_opts.add_scalar_repl_aggregation;
Llvm_vectorize.add_loop_vectorize; (* Vectorize loops to operate on multiple data in parallel *)
Llvm_vectorize.add_slp_vectorize; (* SLP Vectorization to vectorize straight-line code *)
(* Late Stage Cleanup *)
Llvm_scalar_opts.add_aggressive_dce; (* Aggressive Dead Code Elimination *)
Llvm_ipo.add_strip_dead_prototypes;
Llvm_scalar_opts.add_cfg_simplification;
] in
if optimizations_enable then List.iter (fun f -> f pm) optimizations;
(* Initialize types *)
let i8 = Llvm.i8_type context in
let i32 = Llvm.i32_type context in
let i64 = Llvm.i64_type context in
(* Initialize constant functions *)
let c32 = Llvm.const_int i32 in
let c64 = Llvm.const_int i64 in
(* Create symbol table for build in functions *)
let built_in_table = Hashtbl.create 10 in
open_scope ();
(* Define and start and main function *)
let main_type = Llvm.function_type i32 [| |] in
let main = Llvm.declare_function "main" main_type the_module in
let bb = Llvm.append_block context "entry" main in
Llvm.position_at_end bb builder;
(* Emit the program code *)
let info = {
context = context;
the_module = the_module;
builder = builder;
i8 = i8;
i32 = i32;
i64 = i64;
c32 = c32;
c64 = c64;
funcs = ref [main];
built_in_table = built_in_table;
count_funs = ref 1;
count_strings = ref 1;
} in
fun_decls := [fun_def2decl main_func];
let values_from_hashtable htbl =
Hashtbl.fold (fun _key value acc -> value :: acc) htbl [] in
List.iter (codegen_built_in_decl info) (values_from_hashtable Symbol.built_in_table);
struct_types := [None];
activation_records := [None];
List.iter (main_codegen_localdef info) main_func.local_defs;
Llvm.position_at_end bb builder;
main_codegen_block info main_func.body;
ignore (Llvm.build_ret (c32 0) builder);
close_scope ();
(* Verify *)
Llvm_analysis.assert_valid_module the_module;
(* Optimize *)
ignore (Llvm.PassManager.run_module the_module pm);
(* Print out the IR *)
Llvm.print_module temp_file the_module