-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcodegen.cpp
1325 lines (1218 loc) · 53.2 KB
/
codegen.cpp
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
#include "codegen.h"
#include "lit.h"
#include "expr.h"
#include "exprtype.h"
#include "util.h"
#include "parse.h"
#include "stdfunc.h"
llvm::LLVMContext &context(llvm::getGlobalContext());
llvm::IRBuilder<> builder(context);
llvm::Module *mod;
struct stdfunc_t {
std::string name;
int args, type; // if args is -1, the function has vector args.
llvm::Function *func;
};
struct func_body_t { // To define the function that is used before defining
Function *info;
std::vector<std::string> arg_names, cur_mod_name;
std::vector<llvm::Type *> arg_types;
ast_vector body;
bool is_template_base;
llvm::Function *func;
llvm::Type *ret_type;
};
std::vector<func_body_t> funcs_body;
std::map<std::string, stdfunc_t> stdfunc;
llvm::AllocaInst *create_entry_alloca(llvm::Function *TheFunction, std::string &VarName, llvm::Type *type = nullptr) {
llvm::IRBuilder<> TmpB(&TheFunction->getEntryBlock(),
TheFunction->getEntryBlock().begin());
return TmpB.CreateAlloca(type == nullptr ? llvm::Type::getInt32Ty(context) : type, 0, VarName.c_str());
}
llvm::Type *type_to_llvmty(Program &f_list, ExprType *ty) {
llvm::Type *llvm_type = Type::type_to_llvmty(ty);
if(!llvm_type) {
ExprType *tt = ty; int arg_count = 0;
while(tt->is_array()) { tt = tt->next; arg_count++; }
llvm_type = f_list.structs.get(tt->get().user_type)
->strct->getPointerTo();
while(arg_count--) llvm_type = llvm_type->getPointerTo();
}
return llvm_type;
}
namespace Codegen {
llvm::Module *codegen(ast_vector &program) {
llvm::InitializeNativeTarget();
mod = new llvm::Module("LIT", context);
Program list;
{ // initialize standard functions
auto make_stdfunc = [&](std::string name, std::string link_func_name, std::vector<ExprType *> args_ty , ExprType *ret_ty) {
Function f = {
.info = {
.name = name,
.mod_name = list.cur_mod_name,
.is_template = false,
.params = args_ty.size(),
.args_type = args_ty,
.func_addr = nullptr,
.type = ret_ty
}
};
Function *function = list.add(f);
std::vector<llvm::Type *> llvm_args_ty;
for(auto &at : args_ty)
llvm_args_ty.push_back(type_to_llvmty(list, at));
llvm::Type *llvm_ret_type = type_to_llvmty(list, ret_ty);
llvm::Function *llvm_func = llvm::Function::Create(
llvm::FunctionType::get(llvm_ret_type, llvm_args_ty, false),
llvm::Function::ExternalLinkage, link_func_name, mod);
function->info.func_addr = llvm_func;
};
stdfunc["create_array"] = {"create_array" , 1 , T_ARRAY};
stdfunc["printf"] = {"printf" , -1 , T_VOID};
stdfunc["puts"] = {"puts" , -1 , T_VOID};
stdfunc["print"] = {"print" , -1 , T_VOID};
stdfunc["strcat"] = {"strcat" , 2 , T_STRING};
stdfunc["concat_char_str"] = {"concat_char_str" , 2 , T_STRING};
stdfunc["str_register_to_memmgr"] = {"str_register_to_memmgr" , 1 , T_STRING};
stdfunc["int_array_push_int"] = {"int_array_push_int" , 2 , T_ARRAY};
stdfunc["str_to_int"] = {"str_to_int" , 1 , T_INT};
stdfunc["str_to_float"] = {"str_to_float" , 1 , T_DOUBLE};
stdfunc["int_to_str"] = {"int_to_str" , 1 , T_STRING};
stdfunc["float_to_str"] = {"float_to_str" , 1 , T_STRING};
stdfunc["builtinlength"] = {"builtinlength" , 1 , T_INT};
stdfunc["str_copy"] = {"str_copy" , 1 , T_STRING};
stdfunc["GC"] = {"GC" , 0 , T_VOID};
// create put_string function
std::vector<llvm::Type *> func_args;
llvm::Function *func;
make_stdfunc("put_string", "put_string", std::vector<ExprType *>{new ExprType(T_STRING)}, new ExprType(T_VOID));
make_stdfunc("put_char", "put_char", std::vector<ExprType *>{new ExprType(T_CHAR)}, new ExprType(T_VOID));
make_stdfunc("put_num", "put_num", std::vector<ExprType *>{new ExprType(T_INT)}, new ExprType(T_VOID));
make_stdfunc("put_num64", "put_num64", std::vector<ExprType *>{new ExprType(T_INT64)}, new ExprType(T_VOID));
make_stdfunc("put_array", "put_array", std::vector<ExprType *>{new ExprType(new ExprType(T_INT), true)}, new ExprType(T_VOID));
make_stdfunc("put_array_float", "put_array_float", std::vector<ExprType *>{new ExprType(new ExprType(T_DOUBLE), true)}, new ExprType(T_VOID));
make_stdfunc("put_array_str", "put_array_str", std::vector<ExprType *>{new ExprType(new ExprType(T_STRING), true)}, new ExprType(T_VOID));
make_stdfunc("put_num_float", "put_num_float", std::vector<ExprType *>{new ExprType(new ExprType(T_DOUBLE))}, new ExprType(T_VOID));
make_stdfunc("put_ln", "put_ln", std::vector<ExprType *>(), new ExprType(T_VOID));
make_stdfunc("substr", "str_substr", std::vector<ExprType *>{
new ExprType(T_STRING), new ExprType(T_INT), new ExprType(T_INT)}, new ExprType(T_STRING));
make_stdfunc("gets", "get_string_stdin", std::vector<ExprType *>(), new ExprType(T_STRING));
make_stdfunc("getc", "getchar", std::vector<ExprType *>(), new ExprType(T_CHAR));
make_stdfunc("strlen", "strlen", std::vector<ExprType *>{new ExprType(T_STRING)}, new ExprType(T_INT));
// create printf function
func_args.push_back(builder.getInt8Ty());
func = llvm::Function::Create(
llvm::FunctionType::get(/*ret*/builder.getInt32Ty(), func_args, true),
llvm::GlobalValue::ExternalLinkage,
"printf", mod);
stdfunc["printf"].func = func;
func_args.clear();
// create strcat function
func_args.push_back(builder.getInt8PtrTy());
func_args.push_back(builder.getInt8PtrTy());
func = llvm::Function::Create(
llvm::FunctionType::get(/*ret*/builder.getInt8PtrTy(), func_args, false),
llvm::GlobalValue::ExternalLinkage,
"str_concat", mod);
stdfunc["strcat"].func = func;
func_args.clear();
// create str_to_int function
func_args.push_back(builder.getInt8PtrTy());
func = llvm::Function::Create(
llvm::FunctionType::get(/*ret*/builder.getInt32Ty(), func_args, false),
llvm::GlobalValue::ExternalLinkage,
"str_to_int", mod);
stdfunc["str_to_int"].func = func;
func_args.clear();
// create str_to_float function
func_args.push_back(builder.getInt8PtrTy());
func = llvm::Function::Create(
llvm::FunctionType::get(/*ret*/builder.getDoubleTy(), func_args, false),
llvm::GlobalValue::ExternalLinkage,
"str_to_float", mod);
stdfunc["str_to_float"].func = func;
func_args.clear();
// create int_to_str function
func_args.push_back(builder.getInt32Ty());
func = llvm::Function::Create(
llvm::FunctionType::get(/*ret*/builder.getInt8Ty()->getPointerTo(), func_args, false),
llvm::GlobalValue::ExternalLinkage,
"int_to_str", mod);
stdfunc["int_to_str"].func = func;
func_args.clear();
// create int64_to_str function
func_args.push_back(builder.getInt64Ty());
func = llvm::Function::Create(
llvm::FunctionType::get(/*ret*/builder.getInt8Ty()->getPointerTo(), func_args, false),
llvm::GlobalValue::ExternalLinkage,
"int64_to_str", mod);
stdfunc["int64_to_str"].func = func;
func_args.clear();
// create float_to_str function
func_args.push_back(builder.getDoubleTy());
func = llvm::Function::Create(
llvm::FunctionType::get(/*ret*/builder.getInt8Ty()->getPointerTo(), func_args, false),
llvm::GlobalValue::ExternalLinkage,
"float_to_str", mod);
stdfunc["float_to_str"].func = func;
func_args.clear();
// create create_array function
func_args.push_back(builder.getInt32Ty());
func_args.push_back(builder.getInt32Ty());
func = llvm::Function::Create(
llvm::FunctionType::get(/*ret*/builder.getVoidTy()->getPointerTo(), func_args, false),
llvm::GlobalValue::ExternalLinkage,
"create_array", mod);
stdfunc["create_array"].func = func;
func_args.clear();
// create gets function
// create len Function
func_args.push_back(builder.getInt32Ty()->getPointerTo());
func = llvm::Function::Create(
llvm::FunctionType::get(/*ret*/builder.getInt32Ty(), func_args, false),
llvm::GlobalValue::ExternalLinkage,
"get_memory_length", mod);
stdfunc["builtinlength"].func = func;
func_args.clear();
// create str_copy Function
func_args.push_back(builder.getInt8Ty()->getPointerTo());
func = llvm::Function::Create(
llvm::FunctionType::get(/*ret*/builder.getInt8PtrTy(), func_args, false),
llvm::GlobalValue::ExternalLinkage,
"str_copy", mod);
stdfunc["str_copy"].func = func;
func_args.clear();
// create str_register_to_memmgr Function
func_args.push_back(builder.getInt8Ty()->getPointerTo());
func = llvm::Function::Create(
llvm::FunctionType::get(/*ret*/builder.getInt8PtrTy(), func_args, false),
llvm::GlobalValue::ExternalLinkage,
"str_register_to_memmgr", mod);
stdfunc["str_register_to_memmgr"].func = func;
func_args.clear();
// create str_concat_char Function
func_args.push_back(builder.getInt8Ty()->getPointerTo());
func_args.push_back(builder.getInt8Ty());
func = llvm::Function::Create(
llvm::FunctionType::get(/*ret*/builder.getInt8PtrTy(), func_args, false),
llvm::GlobalValue::ExternalLinkage,
"str_concat_char", mod);
stdfunc["concat_char"].func = func;
func_args.clear();
// create str_concat_char Function
func_args.push_back(builder.getInt8Ty());
func_args.push_back(builder.getInt8Ty()->getPointerTo());
func = llvm::Function::Create(
llvm::FunctionType::get(/*ret*/builder.getInt8PtrTy(), func_args, false),
llvm::GlobalValue::ExternalLinkage,
"str_concat_char_str", mod);
stdfunc["concat_char_str"].func = func;
func_args.clear();
// create int_array_push_int Function
func_args.push_back(builder.getInt32Ty()->getPointerTo());
func_args.push_back(builder.getInt32Ty());
func = llvm::Function::Create(
llvm::FunctionType::get(/*ret*/builder.getInt32Ty()->getPointerTo(), func_args, false),
llvm::GlobalValue::ExternalLinkage,
"int_array_push_int", mod);
stdfunc["int_array_push_int"].func = func;
func_args.clear();
// create append_addr_for_gc Function
func_args.push_back(builder.getVoidTy()->getPointerTo());
func = llvm::Function::Create(
llvm::FunctionType::get(/*ret*/builder.getVoidTy(), func_args, false),
llvm::GlobalValue::ExternalLinkage,
"append_addr_for_gc", mod);
stdfunc["append_addr_for_gc"].func = func;
func_args.clear();
// create append_addr_for_gc Function
func_args.push_back(builder.getVoidTy()->getPointerTo());
func = llvm::Function::Create(
llvm::FunctionType::get(/*ret*/builder.getVoidTy(), func_args, false),
llvm::GlobalValue::ExternalLinkage,
"delete_addr_for_gc", mod);
stdfunc["delete_addr_for_gc"].func = func;
func_args.clear();
// create GC Function
func = llvm::Function::Create(
llvm::FunctionType::get(/*ret*/builder.getVoidTy(), func_args, false),
llvm::GlobalValue::ExternalLinkage,
"run_gc", mod);
stdfunc["GC"].func = func;
}
Function main;
std::vector<AST *> main_code, other_code, gvar_code;
for(ast_vector::iterator it = program.begin(); it != program.end(); ++it) {
if(
(*it)->get_type() == AST_FUNCTION ||
(*it)->get_type() == AST_LIBRARY ||
(*it)->get_type() == AST_MODULE ||
(*it)->get_type() == AST_STRUCT
) {
other_code.push_back(*it);
} else {
main_code.push_back(*it);
}
}
for(auto code = other_code.begin(); code != other_code.end(); ++code) {
switch((*code)->get_type()) {
case AST_FUNCTION: ((FunctionAST *)*code)->codegen(list); break;
case AST_LIBRARY: ((LibraryAST *)*code)->codegen(list); break;
case AST_MODULE: ((ModuleAST *)*code)->codegen(list); break;
case AST_STRUCT: ((StructAST *)*code)->codegen(list); break;
default: error("what happened?");
}
}
list.add(main);
int count_temp_func = funcs_body.size();
// create main function
llvm::Function *func_main = llvm::Function::Create(
llvm::FunctionType::get(builder.getInt32Ty(), std::vector<llvm::Type *>(), false),
llvm::Function::ExternalLinkage, "main", mod);
// create entry point of main function
llvm::BasicBlock *entry = llvm::BasicBlock::Create(context, "entry", func_main);
builder.SetInsertPoint(entry);
for(auto &stmt : main_code)
expression(main, list, stmt);
builder.CreateRet(llvm::ConstantInt::get(builder.getInt32Ty(), 0));
////////////////////////////// end of creation of main
llvm::verifyModule(*mod);
// create template function
count_temp_func = funcs_body.size() - count_temp_func;
for(auto fn = funcs_body.begin(); fn != funcs_body.end(); ++fn) {
if(fn->is_template_base) continue;
list.cur_mod_name = fn->cur_mod_name;
llvm::BasicBlock *entry = llvm::BasicBlock::Create(context, "entry", fn->func);
builder.SetInsertPoint(entry);
// set argment variable names and store
auto arg_types_it = fn->arg_types.begin();
auto arg_names_it = fn->arg_names.begin();
for(auto arg_it = fn->func->arg_begin(); arg_it != fn->func->arg_end(); ++arg_it) {
arg_it->setName(*arg_names_it);
llvm::AllocaInst *ainst = create_entry_alloca(fn->func, *arg_names_it, (*arg_types_it));
builder.CreateStore(arg_it, ainst);
var_t *v = fn->info->var.get(*arg_names_it);
if(v) v->val = ainst;
arg_types_it++; arg_names_it++;
}
for(auto v = fn->info->var.local.begin(); v != fn->info->var.local.end(); v++) {
if(v->type.is_array() || v->type.eql_type(T_STRING) || v->type.eql_type(T_USER_TYPE)) {
std::vector<llvm::Value*> func_args;
func_args.push_back(builder.CreateBitCast(v->val, builder.getVoidTy()->getPointerTo())); // get address of var
builder.CreateCall(stdfunc["append_addr_for_gc"].func, func_args);
}
}
llvm::Value *ret_value = llvm::ConstantInt::get(builder.getInt32Ty(), 0); // default return code 0
for(ast_vector::iterator it = fn->body.begin(); it != fn->body.end(); ++it) { // function body
ret_value = Codegen::expression(*(fn->info), list, *it);
}
for(auto v = fn->info->var.local.begin(); v != fn->info->var.local.end(); v++) {
if(v->type.is_array() || v->type.eql_type(T_STRING) || v->type.eql_type(T_USER_TYPE)) {
std::vector<llvm::Value*> func_args;
func_args.push_back(builder.CreateBitCast(v->val, builder.getVoidTy()->getPointerTo())); // get address of var
builder.CreateCall(stdfunc["delete_addr_for_gc"].func, func_args);
}
}
if(ret_value) {
if(ret_value->getType()->getTypeID() != fn->ret_type->getTypeID()) {
ret_value = llvm::ConstantInt::get(builder.getInt32Ty(), 0);
// fprintf(stderr, "warning: type of expression that evaluated last is not match\n");
}
} else if(fn->ret_type->getTypeID() == builder.getInt32Ty()->getTypeID()) {
ret_value = llvm::ConstantInt::get(builder.getInt32Ty(), 0);
} else error("error: return code of function is incorrect '%s'", fn->info->info.name.c_str());
builder.CreateRet(ret_value);
}
return mod;
}
int run(llvm::Module *module, bool enable_optimize, bool enable_emit_llvm) {
std::string err;
llvm::ExecutionEngine *exec_engine = llvm::EngineBuilder(module).setErrorStr(&err).create();
if(!exec_engine) error("LitSystemError: LLVMError: %s\n", err.c_str());
{ // optimize
llvm::PassManager pass_mgr;
// target lays out data structures.
mod->setDataLayout(exec_engine->getDataLayout());
pass_mgr.add(new llvm::DataLayoutPass(mod));
// mem2reg
pass_mgr.add(llvm::createPromoteMemoryToRegisterPass());
/// Provide basic AliasAnalysis support for GVN.
pass_mgr.add(llvm::createBasicAliasAnalysisPass());
// Do simple "peephole" optimizations and bit-twiddling optzns.
pass_mgr.add(llvm::createInstructionCombiningPass());
//
pass_mgr.add(llvm::createReassociatePass());
//
pass_mgr.add(llvm::createCFGSimplificationPass());
// Reassociate expressions.
pass_mgr.add(llvm::createReassociatePass());
pass_mgr.run(*module);
}
if(enable_emit_llvm) {
std::string EC;
llvm::raw_fd_ostream out("mod.bc", EC, llvm::sys::fs::OpenFlags::F_RW);
llvm::WriteBitcodeToFile(module, out);
std::cout << "outputed bitcode to mod.bc" << std::endl;
}
void *prog_ptr = exec_engine->getPointerToFunction(module->getFunction("main"));
int (*program_entry)() = (int (*)())(int*)prog_ptr;
program_entry(); // run
return 0;
}
llvm::Value *expression(Function &f, Program &f_list, AST *ast, ExprType *ty) {
ExprType buf; if(ty == nullptr) ty = &buf;
switch(ast->get_type()) {
case AST_NUMBER:
return ((NumberAST *)ast) -> codegen(f, ty);
case AST_NUMBER_FLOAT:
return ((FloatNumberAST *)ast) -> codegen(f, ty);
case AST_STRING:
return ((StringAST *)ast) -> codegen(f, ty);
case AST_CHAR:
return ((CharAST *)ast) -> codegen(f, ty);
case AST_NEW:
return ((NewAllocAST *)ast) -> codegen(f, f_list, ty);
case AST_VARIABLE:
return ((VariableAST *)ast) -> codegen(f, f_list, ty);
case AST_VARIABLE_ASGMT:
return ((VariableAsgmtAST *)ast) -> codegen(f, f_list, ty);
case AST_FUNCTION_CALL:
return ((FunctionCallAST *)ast) -> codegen(f, f_list, ty);
case AST_BINARY:
return ((BinaryAST *)ast) -> codegen(f, f_list, ty);
case AST_ARRAY:
return ((ArrayAST *)ast) -> codegen(f, f_list, ty);
case AST_VARIABLE_INDEX:
return ((VariableIndexAST *)ast) -> codegen(f, f_list, ty);
case AST_IF:
return ((IfAST *)ast) -> codegen(f, f_list, ty);
case AST_WHILE:
return ((WhileAST *)ast) -> codegen(f, f_list);
case AST_FOR:
return ((ForAST *)ast) -> codegen(f, f_list);
case AST_BREAK:
return ((BreakAST *)ast) -> codegen(f, f_list);
case AST_RETURN:
return ((ReturnAST *)ast) -> codegen(f, f_list);
case AST_DOT:
return ((DotOpAST *)ast) -> codegen(f, f_list, ty);
case AST_CAST:
return ((CastAST *)ast) -> codegen(f, f_list, ty);
case AST_MINUS:
return ((UnaryMinusAST *)ast) -> codegen(f, f_list, ty);
}
return nullptr;
}
};
void ModuleAST::codegen(Program &f_list) {
f_list.cur_mod_name.push_back(name);
for(auto &stmt : statement) {
if(stmt->get_type() == AST_FUNCTION) {
((FunctionAST *)stmt)->codegen(f_list);
} else if(stmt->get_type() == AST_MODULE) {
((ModuleAST *)stmt)->codegen(f_list);
}
}
f_list.cur_mod_name.pop_back();
}
llvm::Value * LibraryAST::codegen(Program &f_list) {
llvm::SMDiagnostic smd_err;
llvm::ErrorOr< std::unique_ptr<llvm::MemoryBuffer> > buf = llvm::MemoryBuffer::getFile(("./lib/" + lib_name + ".bc"));
llvm::Module *lib_mod = llvm::parseBitcodeFile(buf.get().get(), context).get();
if(lib_mod == nullptr)
error("LitSystemError: LLVMError: %s", smd_err.getMessage().str().c_str());
std::string msg_err;
if(llvm::Linker::LinkModules(mod, lib_mod, llvm::Linker::DestroySource, &msg_err))
error("LitSystemError: LLVMError: %s", msg_err.c_str());
for(ast_vector::iterator it = proto.begin(); it != proto.end(); ++it) {
((PrototypeAST *)*it)->append(lib_mod, f_list);
}
return nullptr;
}
void PrototypeAST::append(llvm::Module *lib_mod, Program &f_list) {
Function f;
f.info.name = name.empty() ? proto.name : name;
f.info.is_template = false;
f.info.params = args_type.size();
f.info.type = proto.type;
f.info.func_addr = mod->getFunction(proto.name);
std::vector<ExprType *> proto_args_type;
for(auto it = args_type.begin(); it != args_type.end(); ++it) {
AST *ast = (*it);
if(ast->get_type() == AST_VARIABLE)
proto_args_type.push_back(new ExprType(T_INT));
else if(ast->get_type() == AST_VARIABLE_DECL)
proto_args_type.push_back(new ExprType(((VariableDeclAST *)ast)->info.type));
}
f.info.args_type = proto_args_type;
f_list.add(f);
}
Function FunctionAST::codegen(Program &f_list) { // create a prototype of function, its body will create in Codegen
Function f;
f.info.name = info.name;
f.info.mod_name = f_list.cur_mod_name;
f.info.is_template = info.is_template;
f.info.params = args.size();
f.info.func_addr = nullptr;
f.info.type.change(new ExprType(info.type));
// add arguments
std::vector<llvm::Type *> arg_types;
std::vector<std::string> arg_names;
std::vector<ExprType *> args_type_for_overload;
for(ast_vector::iterator it = args.begin(); it != args.end(); ++it) {
if(f.info.is_template) {
var_t *v = ((VariableAST *)*it)->append(f, f_list);
arg_types.push_back(builder.getInt32Ty());
arg_names.push_back(v->name);
args_type_for_overload.push_back(new ExprType(T_TEMPLATE));
} else if((*it)->get_type() == AST_VARIABLE) {
var_t *v = ((VariableAST *)*it)->append(f, f_list);
llvm::Type *llvm_type = builder.getInt32Ty();
ExprType *type = new ExprType(T_INT);
if(v->type.is_ref()) {
llvm_type = llvm_type->getPointerTo();
type->set_ref();
}
arg_types.push_back(llvm_type);
arg_names.push_back(v->name);
args_type_for_overload.push_back(type);
} else if((*it)->get_type() == AST_VARIABLE_DECL) {
var_t *v = ((VariableDeclAST *)*it)->append(f, f_list);
llvm::Type *llvm_type;
llvm_type = type_to_llvmty(f_list, &v->type);
ExprType *type = new ExprType(v->type);
if(v->type.is_ref()) {
llvm_type = llvm_type->getPointerTo();
type->set_ref();
}
arg_types.push_back(llvm_type);
arg_names.push_back(v->name);
args_type_for_overload.push_back(type);
}
}
f.info.args_name = arg_names;
f.info.args_type = args_type_for_overload;
Function *function = f_list.add(f);
// definition the Function
// set function return type
llvm::Type *func_ret_type = nullptr;
if(info.type.get().type == T_USER_TYPE) {
if(info.type.get().user_type == "T")
func_ret_type = builder.getInt32Ty();
else
func_ret_type = f_list.structs.get(info.type.get().user_type)
->strct->getPointerTo();
} else {
func_ret_type = type_to_llvmty(f_list, &info.type);
}
llvm::FunctionType *func_type = llvm::FunctionType::get(func_ret_type, arg_types, false);
llvm::Function *func = llvm::Function::Create(func_type, llvm::Function::ExternalLinkage, f.info.name, mod);
funcs_body.push_back(func_body_t {
.info = function,
.is_template_base = function->info.is_template,
.arg_names = arg_names,
.arg_types = arg_types,
.body = statement,
.func = func,
.cur_mod_name = f_list.cur_mod_name,
.ret_type = func_ret_type,
});
function->info.func_addr = func;
return f;
}
llvm::Value * IfAST::codegen(Function &f, Program &f_list, ExprType *ret_ty) {
llvm::Value *cond_val = Codegen::expression(f, f_list, cond);
cond_val = builder.CreateICmpNE(cond_val, llvm::ConstantInt::get(builder.getInt32Ty(), 0), "if_cond");
llvm::Function *func = builder.GetInsertBlock()->getParent();
llvm::BasicBlock *bb_then = llvm::BasicBlock::Create(context, "then", func);
llvm::BasicBlock *bb_else = llvm::BasicBlock::Create(context, "else");
llvm::BasicBlock *bb_merge= llvm::BasicBlock::Create(context, "merge");
builder.CreateCondBr(cond_val, bb_then, bb_else);
builder.SetInsertPoint(bb_then);
llvm::Value *val_then = nullptr;
bool has_br = false;
f.has_br.push(has_br);
for(auto expr : then_block)
val_then = Codegen::expression(f, f_list, expr, ret_ty);
if(!f.has_br.top()) builder.CreateBr(bb_merge);
else f.has_br.pop();
bb_then = builder.GetInsertBlock();
func->getBasicBlockList().push_back(bb_else);
builder.SetInsertPoint(bb_else);
llvm::Value *val_else = nullptr;
has_br = false;
f.has_br.push(has_br);
for(auto expr : else_block)
val_else = Codegen::expression(f, f_list, expr);
if(!f.has_br.top()) builder.CreateBr(bb_merge);
else f.has_br.pop();
bb_else = builder.GetInsertBlock();
func->getBasicBlockList().push_back(bb_merge);
builder.SetInsertPoint(bb_merge);
if(val_then && val_else && val_then->getType()->getTypeID() == val_else->getType()->getTypeID()) {
llvm::PHINode *pnode = builder.CreatePHI(val_then->getType(), 2, "if_tmp");
pnode->addIncoming(val_then, bb_then);
pnode->addIncoming(val_else, bb_else);
return pnode;
} else if(val_then && val_else == nullptr) {
// only then branch
return val_then;
}
return nullptr;
}
llvm::Value * WhileAST::codegen(Function &f, Program &f_list) {
llvm::Function *func = builder.GetInsertBlock()->getParent();
llvm::BasicBlock *bb_loop = llvm::BasicBlock::Create(context, "loop", func);
llvm::BasicBlock *bb_after_loop = llvm::BasicBlock::Create(context, "after_loop", func);
f.break_br_list.push(bb_after_loop);
llvm::Value *frst_cond_val = builder.CreateICmpNE(
Codegen::expression(f, f_list, cond), llvm::ConstantInt::get(builder.getInt32Ty(), 0 ));
builder.CreateCondBr(frst_cond_val, bb_loop, bb_after_loop);
builder.SetInsertPoint(bb_loop);
f.break_br_list.push(bb_after_loop);
bool has_br = false; f.has_br.push(has_br);
for(auto expr : block) Codegen::expression(f, f_list, expr);
f.has_br.pop();
f.break_br_list.pop();
llvm::Value *cond_val = builder.CreateICmpNE(
Codegen::expression(f, f_list, cond), llvm::ConstantInt::get(builder.getInt32Ty(), 0));
builder.CreateCondBr(cond_val, bb_loop, bb_after_loop);
builder.SetInsertPoint(bb_after_loop);
f.break_br_list.pop();
return nullptr;
}
llvm::Value * ForAST::codegen(Function &f, Program &f_list) {
llvm::Function *func = builder.GetInsertBlock()->getParent();
Codegen::expression(f, f_list, asgmt);
llvm::BasicBlock *bb_loop = llvm::BasicBlock::Create(context, "loop", func);
llvm::BasicBlock *bb_after_loop = llvm::BasicBlock::Create(context, "after_loop", func);
llvm::Value *frst_cond_val = builder.CreateICmpNE(
Codegen::expression(f, f_list, cond), llvm::ConstantInt::get(builder.getInt32Ty(), 0 ));
builder.CreateCondBr(frst_cond_val, bb_loop, bb_after_loop);
builder.SetInsertPoint(bb_loop);
f.break_br_list.push(bb_after_loop);
bool has_br = false; f.has_br.push(has_br);
for(auto expr : block) Codegen::expression(f, f_list, expr);
f.has_br.pop();
f.break_br_list.pop();
Codegen::expression(f, f_list, step);
llvm::Value *cond_val = builder.CreateICmpNE(
Codegen::expression(f, f_list, cond), llvm::ConstantInt::get(builder.getInt32Ty(), 0));
if(!has_br) builder.CreateCondBr(cond_val, bb_loop, bb_after_loop);
builder.SetInsertPoint(bb_after_loop);
return nullptr;
}
llvm::Value * FunctionCallAST::codegen(Function &f, Program &f_list, ExprType *ty) {
if(stdfunc.count(info.name)) {
llvm::Value *stdfunc_ret_value = llvm::ConstantInt::get(builder.getInt32Ty(), 0);
if(info.name == "puts" || info.name == "print") {
for(int n = 0; n < args.size(); n++) {
ExprType ty;
llvm::Value *val = Codegen::expression(f, f_list, args[n], &ty);
std::vector<llvm::Value*> func_args;
func_args.push_back(val);
if(ty.eql_type(T_STRING)) {
builder.CreateCall(
f_list.lookup("put_string", std::vector<std::string>(), std::vector<ExprType*>{new ExprType(T_STRING)})->info.func_addr
, func_args)->setCallingConv(llvm::CallingConv::C);
} else if(ty.eql_type(T_CHAR)) {
builder.CreateCall(
f_list.lookup("put_char", std::vector<std::string>(), std::vector<ExprType*>{new ExprType(T_CHAR)})->info.func_addr
, func_args)->setCallingConv(llvm::CallingConv::C);
} else if(ty.eql_type(T_DOUBLE)) {
builder.CreateCall(
f_list.lookup("put_num_float", std::vector<std::string>(), std::vector<ExprType*>{new ExprType(T_DOUBLE)})->info.func_addr
, func_args)->setCallingConv(llvm::CallingConv::C);
} else if(ty.is_array() && ty.next->eql_type(T_INT)) {
builder.CreateCall(
f_list.lookup("put_array", std::vector<std::string>(), std::vector<ExprType*>{new ExprType(new ExprType(T_INT), true)})->info.func_addr
, func_args)->setCallingConv(llvm::CallingConv::C);
} else if(ty.is_array() && ty.next->eql_type(T_DOUBLE)) {
builder.CreateCall(
f_list.lookup("put_array_float", std::vector<std::string>(), std::vector<ExprType*>{new ExprType(new ExprType(T_DOUBLE), true)})->info.func_addr
, func_args)->setCallingConv(llvm::CallingConv::C);
} else if(ty.is_array() && ty.next->eql_type(T_STRING)) {
builder.CreateCall(
f_list.lookup("put_array_str", std::vector<std::string>(), std::vector<ExprType*>{new ExprType(new ExprType(T_STRING), true)})->info.func_addr
, func_args)->setCallingConv(llvm::CallingConv::C);
} else if(ty.eql_type(T_INT64)) {
builder.CreateCall(
f_list.lookup("put_num64", std::vector<std::string>(), std::vector<ExprType*>{new ExprType(T_INT64)})->info.func_addr
, func_args)->setCallingConv(llvm::CallingConv::C);
} else {
builder.CreateCall(
f_list.lookup("put_num", std::vector<std::string>(), std::vector<ExprType*>{new ExprType(T_INT)})->info.func_addr
, func_args)->setCallingConv(llvm::CallingConv::C);
}
}
if(info.name == "puts")
builder.CreateCall(
f_list.lookup("put_ln", std::vector<std::string>(), std::vector<ExprType*>())->info.func_addr
, std::vector<llvm::Value *>())->setCallingConv(llvm::CallingConv::C);
} else {
if(stdfunc[info.name].args == -1) { // vector
llvm::Value *val = Codegen::expression(f, f_list, args[0]);
std::vector<llvm::Value*> func_args;
func_args.push_back(val);
for(int n = 1; n < args.size(); ++n)
func_args.push_back(Codegen::expression(f, f_list, args[n]));
builder.CreateCall(stdfunc[info.name].func, func_args);
} else { // normal function
std::vector<llvm::Value*> func_args;
for(auto arg : args)
func_args.push_back(Codegen::expression(f, f_list, arg));
stdfunc_ret_value = builder.CreateCall(stdfunc[info.name].func, func_args);
}
}
ty->change(stdfunc[info.name].type);
return stdfunc_ret_value;
}
// user Function
// process args and get args type
std::vector<ExprType *> caller_args_type;
std::vector<llvm::Value *> caller_args;
for(auto arg = args.begin(); arg != args.end(); ++arg) {
ExprType ty;
caller_args.push_back(Codegen::expression(f, f_list, *arg, &ty));
caller_args_type.push_back(new ExprType(ty));
}
Function *function = f_list.lookup(info.name, info.mod_name, caller_args_type);
if(!function) function = f_list.lookup(info.name, f_list.cur_mod_name, caller_args_type);
if(!function) { // not found
std::string caller_args_type_str;
// args to string
for(auto arg : caller_args_type)
caller_args_type_str += arg->to_string() + ", ";
caller_args_type_str.erase(caller_args_type_str.end() - 2, caller_args_type_str.end()); // erase ", "
error("error: undefined function: '%s(%s)'", info.name.c_str(), caller_args_type_str.c_str());
}
if(function->info.is_template) {
auto has_all_eql_type = [&]() -> bool {
int i=0; for(auto caller_args_each_type : caller_args_type) {
if(!function->info.args_type[i++]->eql_type((caller_args_each_type))) return false;
}
return true;
};
// if callee args type is not the same as the caller args type,
// create a function based template with caller args type
if(has_all_eql_type() == false) {
Function f;
f.info.name = function->info.name;
f.info.mod_name = function->info.mod_name;
f.info.params = function->info.params;
f.info.func_addr = nullptr;
f.info.type.change(&function->info.type);
// append arguments
std::vector<llvm::Type *> arg_types;
for(auto &t : caller_args_type)
arg_types.push_back(type_to_llvmty(f_list, t));
std::vector<std::string> arg_names = function->info.args_name;
f.info.args_type = caller_args_type;
f.info.args_name = arg_names;
Function *func_based_tmpl = f_list.add(f);
for(int i = 0; i < arg_names.size(); i++) {
func_based_tmpl->var.append(arg_names[i], caller_args_type[i]);
}
// definition the Function
// set function return type
func_based_tmpl->info.type.change(
(func_based_tmpl->info.type.get().user_type == "T") ?
caller_args_type[0] :
&function->info.type);
llvm::Type *func_ret_type = type_to_llvmty(f_list, &func_based_tmpl->info.type);
llvm::FunctionType *func_type = llvm::FunctionType::get(func_ret_type, arg_types, false);
llvm::Function *func = llvm::Function::Create(func_type, llvm::Function::ExternalLinkage, f.info.name, mod);
std::vector<AST *> fbody;
for(auto &fb : funcs_body) {
if(fb.info->info.name == f.info.name) {
fbody = fb.body;
break;
}
}
funcs_body.push_back(func_body_t {
.info = func_based_tmpl,
.is_template_base = false,
.arg_names = arg_names,
.arg_types = arg_types,
.body = fbody,
.func = func,
.cur_mod_name = f_list.cur_mod_name,
.ret_type = func_ret_type,
});
func_based_tmpl->info.func_addr = func;
function = func_based_tmpl;
}
}
auto func_args = function->info.args_type;
for(auto arg = args.begin(); arg != args.end(); ++arg) { // reference?
int count = arg - args.begin();
if(func_args[count]->is_ref()) {
VariableAST *vast = (VariableAST *)*arg;
var_t *v = vast->get(f, f_list);
if(!v) getchar();
if(v->type.is_ref())
caller_args[count] = builder.CreateLoad(v->val);
else
caller_args[count] = (v->val);
}
}
llvm::Function *callee = (function->info.func_addr) ? function->info.func_addr : mod->getFunction(info.name);
if(!callee) error("no function: %s", info.name.c_str());
ty->change(new ExprType(function->info.type));
return builder.CreateCall(callee, caller_args, "call_tmp");
}
llvm::Value * BinaryAST::codegen(Function &f, Program &f_list, ExprType *ty) {
ExprType ty_l(T_VOID), ty_r(T_VOID);
llvm::Value *lhs = Codegen::expression(f, f_list, left, &ty_l);
llvm::Value *rhs = Codegen::expression(f, f_list, right, &ty_r);
ty->change(new ExprType(ty_l));
std::vector<ExprType *> args_type;
args_type.push_back(&ty_l);
args_type.push_back(&ty_r);
Function *user_op = f_list.lookup("operator"+op, args_type);
if(user_op) {
llvm::Function *callee = user_op->info.func_addr;
ty->change(new ExprType(user_op->info.type));
llvm::Value *callee_args[] = {lhs, rhs};
return builder.CreateCall(callee, callee_args, "call_tmp");
}
{ // cast instructions
if(ty_l.eql_type(T_INT) && ty_r.eql_type(T_DOUBLE)) {
lhs = builder.CreateSIToFP(lhs, builder.getDoubleTy());
ty->change(new ExprType(T_DOUBLE));
ty_l = T_DOUBLE;
} else if(ty_l.eql_type(T_DOUBLE) && ty_r.eql_type(T_INT)) {
rhs = builder.CreateSIToFP(rhs, builder.getDoubleTy());
} else if(ty_l.eql_type(T_INT64) && ty_r.eql_type(T_INT)) {
rhs = builder.CreateSExt(rhs, builder.getInt64Ty());
} else if(ty_l.eql_type(T_INT) && ty_r.eql_type(T_INT64)) {
lhs = builder.CreateSExt(lhs, builder.getInt64Ty());
} else if(ty_l.eql_type(T_INT) && !ty_r.eql_type(T_INT)) {
rhs = builder.CreateZExt(rhs, builder.getInt32Ty());
}
// char is the same as int
if(ty_l.eql_type(T_CHAR))
lhs = builder.CreateZExt(lhs, builder.getInt32Ty());
if(ty_r.eql_type(T_CHAR))
rhs = builder.CreateZExt(rhs, builder.getInt32Ty());
}
// if(!ty_l.eql_type(&ty_r)) {
// std::cout << "warning: different types to each other: " << ty_l.to_string() << ", " << ty_r.to_string() << std::endl;
// }
if(op == "+") {
if(ty_l.eql_type(T_STRING) && ty_r.eql_type(T_STRING)) { // string + string
std::vector<llvm::Value*> func_args;
func_args.push_back(lhs);
func_args.push_back(rhs);
llvm::Value *ret = builder.CreateCall(stdfunc["strcat"].func, func_args);
return ret;
} else if(ty_l.eql_type(T_STRING) && ty_r.eql_type(T_CHAR)) {
std::vector<llvm::Value*> func_args;
func_args.push_back(lhs);
func_args.push_back(rhs);
llvm::Value *ret = builder.CreateCall(stdfunc["concat_char"].func, func_args);
return ret;
} else if(ty_l.eql_type(T_CHAR) && ty_r.eql_type(T_STRING)) {
std::vector<llvm::Value*> func_args;
func_args.push_back(lhs);
func_args.push_back(rhs);
llvm::Value *ret = builder.CreateCall(stdfunc["concat_char_str"].func, func_args);
ty->change(T_STRING);
return ret;
} else if(ty_l.eql_type(T_ARRAY) && ty_r.eql_type(T_INT)) {
std::vector<llvm::Value*> func_args;
func_args.push_back(lhs);
func_args.push_back(rhs);
llvm::Value *ret = builder.CreateCall(stdfunc["int_array_push_int"].func, func_args);
return ret;
} else if(ty_l.eql_type(T_DOUBLE)) {
return builder.CreateFAdd(lhs, rhs, "addtmp");
} else {
return builder.CreateAdd(lhs, rhs, "addtmp");
}
} else if(op == "-") {
if(ty_l.eql_type(T_DOUBLE))
return builder.CreateFSub(lhs, rhs, "subtmp");
else
return builder.CreateSub(lhs, rhs, "subtmp");
} else if(op == "*") {
if(ty_l.eql_type(T_DOUBLE))
return builder.CreateFMul(lhs, rhs, "multmp");
else
return builder.CreateMul(lhs, rhs, "multmp");
} else if(op == "/") {
if(ty_l.eql_type(T_DOUBLE))
return builder.CreateFDiv(lhs, rhs, "divtmp");
else
return builder.CreateSDiv(lhs, rhs, "divtmp");
} else if(op == "%") return builder.CreateSRem(lhs, rhs, "remtmp");
else if(op == "<" || op == ">" || op == "!=" ||
op == "==" || op == "<=" || op == ">=") {
bool lt = op == "<", gt = op == ">", ne = op == "!=", eql = op == "==", fle = op == "<=";
bool str_cmp = false;
std::string tmp_name = "cmp_tmp";
if(op == "<") {
if(ty_l.eql_type(T_DOUBLE))
lhs = builder.CreateFCmpULT(lhs, rhs, tmp_name);
else
lhs = builder.CreateICmpSLT(lhs, rhs, tmp_name);
} else if(op == ">") {
if(ty_l.eql_type(T_DOUBLE))
lhs = builder.CreateFCmpUGT(lhs, rhs, tmp_name);
else
lhs = builder.CreateICmpSGT(lhs, rhs, tmp_name);
} else if(op == "!=") {
lhs = builder.CreateICmpNE(lhs, rhs, tmp_name);
} else if(op == "==") {
lhs = builder.CreateICmpEQ(lhs, rhs, tmp_name);
} else if(op == "<=") {
lhs = builder.CreateICmpSLE(lhs, rhs, tmp_name);
} else if(op == ">=") {
lhs = builder.CreateICmpSGE(lhs, rhs, tmp_name);
}
lhs = builder.CreateZExt(lhs, builder.getInt32Ty());
ty->change(T_INT);
return lhs;
} else if(op == "and" || op == "&" || op == "or" ||
op == "|" || op == "xor" || op == "^") {
bool andop = op == "and" || op == "&", orop = op == "or" || op == "|";
if(andop) {
lhs = builder.CreateAnd(lhs, rhs);
} else if(orop) {
lhs = builder.CreateOr(lhs, rhs);
} else {
lhs = builder.CreateXor(lhs, rhs);
}
lhs = builder.CreateZExt(lhs, builder.getInt32Ty());
return lhs;
}
return nullptr;
}
llvm::Value *CastAST::codegen(Function &f, Program &f_list, ExprType *ret_ty) {
ret_ty->change(Type::str_to_type(type));
llvm::Type *to_type = type_to_llvmty(f_list, ret_ty);
ExprType exp_ty;
llvm::Value *exp = Codegen::expression(f, f_list, expr, &exp_ty);
if(exp_ty.eql_type(T_INT) && ret_ty->eql_type(T_DOUBLE)) {
return builder.CreateSIToFP(exp, builder.getDoubleTy());
} else if(exp_ty.eql_type(T_DOUBLE) && !ret_ty->eql_type(T_DOUBLE)) {
exp = builder.CreateFPToSI(exp, builder.getInt32Ty());
} else if(ret_ty->eql_type(T_INT64)) {
return builder.CreateSExt(exp, builder.getInt64Ty());
} else if(exp_ty.eql_type(T_CHAR) && ret_ty->eql_type(T_INT)) {
return builder.CreateSExt(exp, builder.getInt32Ty());
}
return builder.CreateBitCast(exp, to_type);
}
llvm::Value *UnaryMinusAST::codegen(Function &f, Program &f_list, ExprType *ret_ty) {
llvm::Value *e = Codegen::expression(f, f_list, expr, ret_ty);
if(ret_ty->eql_type(T_DOUBLE)) {
return builder.CreateFSub(
llvm::ConstantFP::get(llvm::Type::getDoubleTy(context), 0.0),
e);