-
Notifications
You must be signed in to change notification settings - Fork 0
/
expression.cpp
1078 lines (933 loc) · 29.1 KB
/
expression.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 "expression.hpp"
#include <algorithm>
#include <cstring>
#include <sstream>
#include <string>
#include "decl_common.hpp"
void expr::convert_to_bool(llvm::Value *&val) {
val = ir_builder.CreateICmpEQ(
val, const_expr::get_val(0, val->getType()).llvm_val);
val = ir_builder.CreateZExtOrBitCast(val,
llvm::IntegerType::get(the_context, 32));
}
void expr::convert_to_type(value &val, type_i rtype) {
auto ltype_llvm = llvm::dyn_cast<llvm::IntegerType>(val.llvm_val->getType());
auto rtype_llvm = llvm::dyn_cast<llvm::IntegerType>(rtype.llvm_type);
if (!ltype_llvm || !rtype_llvm) {
print_warning("Cannot convert non integer type!");
return;
}
int lbits = ltype_llvm->getBitWidth(), rbits = rtype_llvm->getBitWidth();
if (val.is_signed) {
val.llvm_val = ir_builder.CreateSExtOrTrunc(val.llvm_val, rtype_llvm);
} else {
val.llvm_val = ir_builder.CreateZExtOrTrunc(val.llvm_val, rtype_llvm);
}
val.is_signed = rtype.is_signed;
}
void expr::gen_common_type(value &lhs, value &rhs) {
type_i dest_type = get_common_type(lhs.get_type(), rhs.get_type());
// Only one value will be converted but since get_common_type doesn't give any
// info on which value to convert, we have to do this. Relies on the fact that
// CreateSExtOrTrunc will return the value as is if it is already the
// destination type. Could probably use CreateSExt too but the docs don't
// specify that it doesn't generate an op if value already has desired type
if (lhs.is_signed) {
lhs.llvm_val =
ir_builder.CreateSExtOrTrunc(lhs.llvm_val, dest_type.llvm_type);
} else {
lhs.llvm_val =
ir_builder.CreateZExtOrTrunc(lhs.llvm_val, dest_type.llvm_type);
}
if (rhs.is_signed) {
rhs.llvm_val =
ir_builder.CreateSExtOrTrunc(rhs.llvm_val, dest_type.llvm_type);
} else {
rhs.llvm_val =
ir_builder.CreateZExtOrTrunc(rhs.llvm_val, dest_type.llvm_type);
}
}
type_i expr::get_common_type(type_i t1, type_i t2) {
auto ltype = llvm::dyn_cast<llvm::IntegerType>(t1.llvm_type);
auto rtype = llvm::dyn_cast<llvm::IntegerType>(t2.llvm_type);
if (!ltype || !rtype) {
raise_error("Common types for non integer types not defined!");
}
int lbits = ltype->getBitWidth(), rbits = rtype->getBitWidth();
type_i ret = t1;
if (lbits == rbits) {
ret.is_signed = t1.is_signed && t2.is_signed;
return ret;
}
value val;
if (lbits < rbits) {
ret.llvm_type = rtype;
// FIXME: This is slightly more complicated than this. See 6.3.1.8/1
// Basically, if unsigned has greater rank, then result is unsigned
// Else if signed one can represent both signed and unsigned values then
// result is signed, otherwise the result is unsigned
ret.is_signed = t2.is_signed;
} else {
ret.llvm_type = ltype;
ret.is_signed = t1.is_signed;
}
return ret;
}
type_i expr::get_common_type(expr *e1, expr *e2) {
return get_common_type(e1->get_type(), e2->get_type());
}
assign_expr_ops::assign_expr_ops(unary_expr *left_expr, OP op,
assign_expr *right_expr) {
this->left_expr = left_expr;
this->op = op;
this->right_expr = right_expr;
}
std::string assign_expr_ops::op_string(OP op) {
switch (op) {
case ASSIGN:
return "=";
case MUL_ASSIGN:
return "*=";
case DIV_ASSIGN:
return "/=";
case MOD_ASSIGN:
return "%=";
case ADD_ASSIGN:
return "+=";
case SUB_ASSIGN:
return "-=";
case LEFT_ASSIGN:
return "<<=";
case RIGHT_ASSIGN:
return ">>=";
case AND_ASSIGN:
return "&=";
case XOR_ASSIGN:
return "^=";
case OR_ASSIGN:
return "|=";
}
return "unknown";
}
void assign_expr_ops::dump_tree() {
cout << "- (assignment_expression)" << endl;
left_expr->dump_tree();
cout << "- (assignment_operator) " << op_string(op) << endl;
right_expr->dump_tree();
cout.unindent();
}
value assign_expr_ops::codegen() {
value right_val = right_expr->codegen();
value left_val = left_expr->codegen();
binary_expr_ops::OP bin_op;
switch (op) {
case MUL_ASSIGN:
bin_op = binary_expr_ops::MULT;
break;
case DIV_ASSIGN:
bin_op = binary_expr_ops::DIV;
break;
case MOD_ASSIGN:
bin_op = binary_expr_ops::MOD;
break;
case ADD_ASSIGN:
bin_op = binary_expr_ops::PLUS;
break;
case SUB_ASSIGN:
bin_op = binary_expr_ops::MINUS;
break;
case LEFT_ASSIGN:
bin_op = binary_expr_ops::SHIFT_LEFT;
break;
case RIGHT_ASSIGN:
bin_op = binary_expr_ops::SHIFT_RIGHT;
break;
case AND_ASSIGN:
bin_op = binary_expr_ops::BIT_AND;
break;
case XOR_ASSIGN:
bin_op = binary_expr_ops::BIT_XOR;
break;
case OR_ASSIGN:
bin_op = binary_expr_ops::BIT_OR;
break;
case ASSIGN:
break;
default:
raise_error("Undefined operator in assign_expr_ops");
}
if (op != ASSIGN) { // => bin_op was assigned
right_val = binary_expr_ops::codegen(left_val, bin_op, right_val);
}
return left_expr->codegen_store(right_val);
}
type_i assign_expr_ops::get_type() { return left_expr->get_type(); }
cond_expr_ops::cond_expr_ops(binary_expr *cond, expr *true_expr,
cond_expr *false_expr) {
this->cond = cond;
this->true_expr = true_expr;
this->false_expr = false_expr;
}
value cond_expr_ops::codegen() {
value cond_val = cond->codegen();
if (auto *const_val = llvm::dyn_cast<llvm::Constant>(cond_val.llvm_val)) {
if (const_val->isZeroValue()) {
return false_expr->codegen();
} else {
return true_expr->codegen();
}
}
value binary_cond;
{ // New scope
auto s = sym_table.top_func_scope()->new_scope();
binary_cond = sym_table.add_var(ir_builder.getInt1Ty(), "cond");
binary_cond.llvm_val = ir_builder.CreateICmpNE(
cond_val.llvm_val,
const_expr::get_val(0, cond_val.get_type()).llvm_val);
}
return codegen(binary_cond, true_expr, false_expr);
}
value cond_expr_ops::codegen(value cond_val, expr *true_expr,
expr *false_expr) {
type_i res_type;
if (true_expr->get_type().llvm_type->isIntegerTy()) {
res_type = get_common_type(true_expr, false_expr);
} else {
assert(true_expr->get_type().llvm_type ==
false_expr->get_type().llvm_type &&
"Implicit casting is not supported for non-integral types");
res_type = true_expr->get_type();
}
if (auto *const_val = llvm::dyn_cast<llvm::Constant>(cond_val.llvm_val)) {
if (const_val->isZeroValue()) {
return false_expr->codegen();
} else {
return true_expr->codegen();
}
}
llvm::BasicBlock *curr_block, *true_block, *false_block, *next_block;
value result, ret;
{ // New scope
auto s = sym_table.top_func_scope()->new_scope();
result = sym_table.add_var(res_type, "res");
curr_block = ir_builder.GetInsertBlock();
true_block = llvm::BasicBlock::Create(the_context, "true",
sym_table.get_curr_func());
false_block = llvm::BasicBlock::Create(the_context, "false",
sym_table.get_curr_func());
ir_builder.CreateCondBr(cond_val.llvm_val, true_block, false_block);
next_block = llvm::BasicBlock::Create(the_context, "cond_end",
sym_table.get_curr_func());
ir_builder.SetInsertPoint(next_block);
ret = create_load(result, "res");
}
ir_builder.SetInsertPoint(true_block);
value true_val = true_expr->codegen();
convert_to_type(true_val, res_type);
ir_builder.CreateStore(true_val.llvm_val, result.llvm_val);
ir_builder.CreateBr(next_block);
ir_builder.SetInsertPoint(false_block);
value false_val = false_expr->codegen();
convert_to_type(false_val, res_type);
ir_builder.CreateStore(false_val.llvm_val, result.llvm_val);
ir_builder.CreateBr(next_block);
ir_builder.SetInsertPoint(next_block);
// curr_block->print(llvm::outs());
return ret;
}
void cond_expr_ops::dump_tree() {
cout << "- (conditional_expression)" << endl;
cout.indent();
cond->dump_tree();
true_expr->dump_tree();
false_expr->dump_tree();
cout.unindent();
}
type_i cond_expr_ops::get_type() {
return get_common_type(true_expr->get_type(), false_expr->get_type());
}
binary_expr_ops::binary_expr_ops(expr *left, OP op, expr *right) {
this->left_expr = left;
this->op = op;
this->right_expr = right;
}
std::string binary_expr_ops::op_string(binary_expr_ops::OP op) {
switch (op) {
case MULT:
return "*";
case DIV:
return "/";
case MOD:
return "%";
case PLUS:
return "+";
case MINUS:
return "-";
case SHIFT_LEFT:
return "<<";
case SHIFT_RIGHT:
return ">>";
case LT:
return "<";
case GT:
return ">";
case LE:
return "<=";
case GE:
return ">=";
case EQ:
return "==";
case NE:
return "!=";
case BIT_AND:
return "&";
case BIT_XOR:
return "^";
case BIT_OR:
return "|";
case AND:
return "&&";
case OR:
return "||";
}
return "unknown";
}
void binary_expr_ops::dump_tree() {
cout << "- (binary_expression)" << endl;
cout.indent();
left_expr->dump_tree();
cout << "- (operator) " << op_string(op) << endl;
right_expr->dump_tree();
cout.unindent();
}
// Assumes both values have been through proper type conversions
llvm::Instruction::BinaryOps binary_expr_ops::get_arith_op(
value &lhs, value &rhs, binary_expr_ops::OP op) {
bool is_signed = lhs.is_signed && rhs.is_signed;
llvm::Type *common_type = lhs.llvm_val->getType();
typedef llvm::Instruction::BinaryOps llvmOP;
switch (op) {
case MULT:
return llvmOP::Mul;
case DIV:
if (is_signed) {
return llvmOP::SDiv;
} else {
return llvmOP::UDiv;
}
case MOD:
if (is_signed) {
return llvmOP::SRem;
} else {
return llvmOP::URem;
}
case PLUS:
return llvmOP::Add;
case MINUS:
return llvmOP::Sub;
case SHIFT_LEFT:
return llvmOP::Shl;
case SHIFT_RIGHT:
if (lhs.is_signed) {
return llvmOP::AShr;
} else {
return llvmOP::LShr;
}
break;
case BIT_XOR:
return llvmOP::Xor;
case BIT_AND:
return llvmOP::And;
case BIT_OR:
return llvmOP::Or;
}
raise_error("No binary operator found!");
}
// Assumes both values have been through proper type conversions
llvm::CmpInst::Predicate binary_expr_ops::get_cmp_pred(value &lhs, value &rhs,
OP op) {
typedef llvm::CmpInst::Predicate pred;
// FIXME: Assuming both types to be integers
if (lhs.is_signed && rhs.is_signed) {
switch (op) {
case LT:
return pred::ICMP_SLT;
case GT:
return pred::ICMP_SGT;
case LE:
return pred::ICMP_SLE;
case GE:
return pred::ICMP_SGE;
}
} else {
switch (op) {
case LT:
return pred::ICMP_ULT;
case GT:
return pred::ICMP_UGT;
case LE:
return pred::ICMP_ULE;
case GE:
return pred::ICMP_UGE;
}
}
switch (op) {
case EQ:
return pred::ICMP_EQ;
case NE:
return pred::ICMP_NE;
}
raise_error("The operation is not a comparison operator");
}
value binary_expr_ops::codegen() { return codegen(left_expr, op, right_expr); }
value binary_expr_ops::codegen(expr *lhs_expr, OP op, expr *rhs_expr) {
value lhs = lhs_expr->codegen();
// No implicit casting for floats
if (lhs.get_type().llvm_type->isIntegerTy()) {
value zero_val = const_expr::get_val(0, lhs.get_type().llvm_type);
value one_val = const_expr::get_val(1, lhs.get_type().llvm_type);
switch (op) {
case AND: {
lhs.llvm_val = ir_builder.CreateICmpNE(lhs.llvm_val, zero_val.llvm_val);
value_expr false_expr(zero_val);
binary_expr_ops rhs_expr_bool(rhs_expr, binary_expr_ops::NE,
&false_expr);
return cond_expr_ops::codegen(lhs, &rhs_expr_bool, &false_expr);
}
case OR: {
lhs.llvm_val = ir_builder.CreateICmpNE(lhs.llvm_val, zero_val.llvm_val);
value_expr true_expr(one_val);
value_expr false_expr(zero_val);
binary_expr_ops rhs_expr_bool(rhs_expr, binary_expr_ops::NE,
&false_expr);
return cond_expr_ops::codegen(lhs, &true_expr, rhs_expr);
}
}
}
value rhs = rhs_expr->codegen();
return codegen(lhs, op, rhs);
}
value binary_expr_ops::codegen(value lhs, OP op, value rhs) {
// Assume float!
// TODO: Integrate into the rest of the code this last minute change
if (!lhs.get_type().llvm_type->isIntegerTy()) {
value ret_val;
switch (op) {
case LT:
ret_val =
value(ir_builder.CreateFCmpOLT(lhs.llvm_val, rhs.llvm_val), true);
break;
case GT:
ret_val =
value(ir_builder.CreateFCmpOGT(lhs.llvm_val, rhs.llvm_val), true);
break;
case LE:
ret_val =
value(ir_builder.CreateFCmpOLE(lhs.llvm_val, rhs.llvm_val), true);
break;
case GE:
ret_val =
value(ir_builder.CreateFCmpOGE(lhs.llvm_val, rhs.llvm_val), true);
break;
case EQ:
ret_val =
value(ir_builder.CreateFCmpOEQ(lhs.llvm_val, rhs.llvm_val), true);
break;
case NE:
ret_val =
value(ir_builder.CreateFCmpONE(lhs.llvm_val, rhs.llvm_val), true);
break;
case MULT:
return value(ir_builder.CreateFMul(lhs.llvm_val, rhs.llvm_val), true);
case DIV:
return value(ir_builder.CreateFDiv(lhs.llvm_val, rhs.llvm_val), true);
case PLUS:
return value(ir_builder.CreateFAdd(lhs.llvm_val, rhs.llvm_val), true);
case MINUS:
return value(ir_builder.CreateFSub(lhs.llvm_val, rhs.llvm_val), true);
}
if (ret_val.llvm_val) { // => It was set in switch
ret_val.llvm_val = ir_builder.CreateZExtOrBitCast(
ret_val.llvm_val, ir_builder.getInt32Ty());
return ret_val;
}
raise_error("Unsupported operation on floats");
}
gen_common_type(lhs, rhs);
value ret_val;
ret_val.is_signed = lhs.is_signed && rhs.is_signed;
llvm::Type *common_type = lhs.llvm_val->getType();
if (op == AND || op == OR) {
convert_to_bool(lhs.llvm_val);
convert_to_bool(rhs.llvm_val);
if (op == AND) {
op = BIT_AND;
} else {
op = BIT_OR;
}
}
switch (op) {
case LT:
case GT:
case LE:
case GE:
case EQ:
case NE: {
llvm::CmpInst::Predicate pred = get_cmp_pred(lhs, rhs, op);
ret_val.llvm_val =
ir_builder.CreateICmp(pred, lhs.llvm_val, rhs.llvm_val);
ret_val.llvm_val =
ir_builder.CreateZExtOrBitCast(ret_val.llvm_val, common_type);
break;
}
default: {
llvm::Instruction::BinaryOps llvm_op = get_arith_op(lhs, rhs, op);
ret_val.llvm_val =
ir_builder.CreateBinOp(llvm_op, lhs.llvm_val, rhs.llvm_val);
}
}
return ret_val;
}
type_i binary_expr_ops::get_type() {
return get_common_type(left_expr->get_type(), right_expr->get_type());
}
unary_op_expr::unary_op_expr(unary_op_expr::OP op, cast_expr *expression) {
this->op = op;
this->expression = expression;
}
std::string unary_op_expr::op_string(unary_op_expr::OP op) {
switch (op) {
case ADDRESS_OF:
return "&";
case INDIRECTION:
return "*";
case PLUS:
return "+";
case MINUS:
return "-";
case BIT_NOT:
return "~";
case NOT:
return "!";
}
raise_error("Unkown unary operator");
}
void unary_op_expr::dump_tree() {
cout << "- (unary_expression)" << endl;
cout.indent();
cout << "- (operator) " << op_string(op) << endl;
expression->dump_tree();
cout.unindent();
}
value unary_op_expr::codegen() {
value val = expression->codegen();
return codegen(op, val);
}
value unary_op_expr::codegen(OP op, value val) {
typedef binary_expr_ops::OP bin_op;
switch (op) {
case ADDRESS_OF:
case INDIRECTION:
raise_error("& and * operators are not supported!");
case PLUS:
return val; // TODO: Change this when adding support for floating points
case MINUS:
return binary_expr_ops::codegen(const_expr::get_val(0, val.get_type()),
bin_op::MINUS, val);
case BIT_NOT:
return binary_expr_ops::codegen(const_expr::get_val(-1, val.get_type()),
bin_op::BIT_XOR, val);
case NOT:
return binary_expr_ops::codegen(const_expr::get_val(0, val.get_type()),
bin_op::EQ, val);
}
raise_error("Unkown unary operator!");
}
type_i unary_op_expr::get_type() {
switch (op) {
case PLUS:
case MINUS:
case BIT_NOT:
case NOT:
// TODO: Change this when adding support for other types
return expression->get_type();
}
raise_error("Address-of and indirection operators are not supported!");
}
unary_inc_dec_expr::unary_inc_dec_expr(unary_inc_dec_expr::OP op,
unary_expr *expression) {
this->op = op;
this->expression = expression;
}
std::string unary_inc_dec_expr::op_string(unary_inc_dec_expr::OP op) {
switch (op) {
case INC:
return "++";
case DEC:
return "--";
}
raise_error("Unkown unary_inc_dec_expr operator");
}
void unary_inc_dec_expr::dump_tree() {
cout << "- (unary_expression)" << endl;
cout.indent();
cout << "- (unary_operator) " << op_string(op) << endl;
expression->codegen();
cout.unindent();
}
value unary_inc_dec_expr::codegen() {
value val = expression->codegen();
binary_expr_ops::OP bin_op;
switch (op) {
case INC:
bin_op = binary_expr_ops::PLUS;
break;
case DEC:
bin_op = binary_expr_ops::MINUS;
break;
}
value to_store = binary_expr_ops::codegen(
val, bin_op, const_expr::get_val(1, val.get_type()));
expression->codegen_store(to_store);
return to_store;
}
type_i unary_inc_dec_expr::get_type() { return expression->get_type(); }
arg_expr_list *arg_expr_list::add(assign_expr *expr) {
exprs.push_back(expr);
return this;
}
void arg_expr_list::dump_tree() {
cout << "- (argument_expression_list)" << endl;
cout.indent();
for (auto &expr : exprs) {
expr->dump_tree();
}
cout.unindent();
}
std::vector<type_i> arg_expr_list::get_types() {
std::vector<type_i> types;
types.reserve(exprs.size());
std::transform(exprs.begin(), exprs.end(), std::back_inserter(types),
[](expr *e) { return e->get_type(); });
return types;
}
std::vector<value> arg_expr_list::codegen() {
std::vector<value> vals;
vals.reserve(exprs.size());
std::transform(exprs.begin(), exprs.end(), std::back_inserter(vals),
[](expr *e) { return e->codegen(); });
return vals;
}
postfix_inc_dec_expr::postfix_inc_dec_expr(postfix_expr *expr,
postfix_inc_dec_expr::OP op) {
this->expression = expr;
this->op = op;
}
void postfix_inc_dec_expr::dump_tree() {
cout << "- (postfix_expression)" << endl;
cout.indent();
expression->dump_tree();
cout << "- (postfix_operator) " << to_string(op);
cout.unindent();
}
std::string postfix_inc_dec_expr::to_string(postfix_inc_dec_expr::OP op) {
switch (op) {
case INC:
return "++";
case DEC:
return "--";
}
raise_error("Unknown posfix_inc_dec_expr type");
}
value postfix_inc_dec_expr::codegen() {
value val = expression->codegen();
binary_expr_ops::OP bin_op;
switch (op) {
case INC:
bin_op = binary_expr_ops::PLUS;
break;
case DEC:
bin_op = binary_expr_ops::MINUS;
break;
}
value to_store = binary_expr_ops::codegen(
val, bin_op, const_expr::get_val(1, val.get_type()));
expression->codegen_store(to_store);
return val;
}
type_i postfix_inc_dec_expr::get_type() { return expression->get_type(); }
func_call::func_call(postfix_expr *func_expr, arg_expr_list *expr_list) {
this->func_expr = func_expr;
this->arg_list = expr_list;
}
void func_call::dump_tree() {
cout << "- (functional_call)" << endl;
cout.indent();
func_expr->dump_tree();
if (arg_list) {
arg_list->dump_tree();
}
cout.unindent();
}
value func_call::codegen() {
value func = func_expr->codegen();
auto *llvm_func = llvm::dyn_cast<llvm::Function>(func.llvm_val);
if (!llvm_func) {
raise_error("Function value not generated by expression in func_call");
}
std::vector<value> arg_vals;
if (arg_list) {
arg_vals = arg_list->codegen();
}
int i = 0;
for (auto &llvm_arg : llvm_func->args()) {
if (i >= arg_vals.size()) {
// This is necessary because the spec says "empty list in a function
// declarator that is not part of a definition of that function
// specifies that no information about the number or types of
// the parameters is supplied.", so number of arguments can differ from
// number of parameters in declaration and still be valid
break;
}
if (auto int_type = llvm::dyn_cast<llvm::IntegerType>(llvm_arg.getType())) {
// We only want to attempt to convert integer types for now
convert_to_type(arg_vals[i++], type_i(int_type));
}
}
std::vector<llvm::Value *> llvm_arg_vals;
std::transform(arg_vals.begin(), arg_vals.end(),
std::back_inserter(llvm_arg_vals),
[](value val) { return val.llvm_val; });
// TODO: Add type checking for args. Will involve somehow modifying the value
// struct to hold types for the function's return value and parameters.
// Perhaps create a subclass for value. Would require many changes throughout
// since value is being used by-value everywhere
i = 0;
for (auto &arg : llvm_func->args()) {
if (arg.getType() != llvm_arg_vals[i++]->getType()) {
raise_error(
"Given argument does not match type of function parameter. Note that "
"currently arguments given to a function call do not support some"
"implicit type conversions");
}
}
llvm::Value *ret = ir_builder.CreateCall(llvm_func, llvm_arg_vals);
// FIXME: For now, symbol table always makes is_signed true for any functions
// added to it.
return value(ret, func.is_signed); // Assuming func contains signedness info
// about the return type
}
type_i func_call::get_type() {
type_i func_type = func_expr->get_type();
llvm::FunctionType *ll_func_type;
if (auto ll_func_ptr =
llvm::dyn_cast<llvm::PointerType>(func_type.llvm_type)) {
ll_func_type =
llvm::dyn_cast<llvm::FunctionType>(ll_func_ptr->getElementType());
}
if (!ll_func_type) {
raise_error("Type of expression in function call is not function!");
}
return type_i(ll_func_type->getReturnType(), func_type.is_signed);
}
ident_expr::ident_expr(const char *id) { identifier = std::string(id); }
void ident_expr::dump_tree() {
cout << "- (identifier) " << identifier << endl;
}
value ident_expr::codegen() {
value val = sym_table.get_var(identifier);
if (!val.llvm_val) {
raise_error("use of identifier before declaration");
}
return create_load(val, identifier);
}
value ident_expr::codegen_store(value val) {
value var = sym_table.get_var(identifier);
convert_to_type(val, var.get_type());
ir_builder.CreateStore(val.llvm_val, var.llvm_val);
return val;
}
type_i ident_expr::get_type() {
value val = sym_table.get_var(identifier);
if (!val.llvm_val) {
raise_error("use of identifier before declaration");
}
return val.get_type();
}
paren_expr::paren_expr(expr *expression) { this->expression = expression; }
void paren_expr::dump_tree() { expression->dump_tree(); }
value paren_expr::codegen() { return expression->codegen(); }
type_i paren_expr::get_type() { return expression->get_type(); }
const_expr::const_expr(llvm::Constant *data, bool is_signed, std::string str) {
this->data = data;
this->is_signed = is_signed;
this->str = str;
}
// Parse a string representing int, long, long long and their unsigned
// variants into a value and the corresponding type. See
// https://clang.llvm.org/doxygen/LiteralSupport_8cpp_source.html#l00526 for
// clang's implementation
const_expr *const_expr::new_int_expr(const char *s) {
std::string str(s);
free((void *)s);
std::transform(str.begin(), str.end(), str.begin(),
[](unsigned char c) { return std::toupper(c); });
assert(str.length() > 0 && "Need non-empty string as number");
type_specifiers type;
if (str[0] == 'L' || str[0] == 'U' || str[0] == '\'') { // => char
if (str[0] == 'L' || str[0] == 'U') {
raise_error("No support for wchar_t or char16_t or char32_t");
}
type.add_spec(type_specifiers::CHAR);
char c = str[1]; // TODO: Add support for escape sequences etc
llvm::APInt int_val = llvm::APInt(8, c);
return new const_expr(
llvm::ConstantInt::get(type.get_type().llvm_type, int_val), false, str);
}
int start = 0, end = str.length() - 1;
int radix = 10;
if (str[0] == '0') { // => Octal or Hexa
if (str.length() > 1 && str[1] == 'X') { // => Hexa
radix = 16;
start = 2;
} else {
radix = 8;
if (str.length() > 1) start = 1; // Since only 0 might be present
}
}
bool is_unsigned = false, is_long = false, is_longlong = false;
auto it = str.rbegin();
if (*it == 'U') {
is_unsigned = true;
end--;
++it;
}
if (it != str.rend() && *it == 'L') {
++it;
end--;
if (it != str.rend() && *it == 'L') {
is_longlong = true;
++it;
end--;
} else {
is_long = true;
}
}
if (it != str.rend() && *it == 'U') {
is_unsigned = true;
end--;
}
int num_bits;
if (is_long) {
num_bits = 32;
type.add_spec(type_specifiers::LONG);
} else if (is_longlong) {
num_bits = 64;
type.add_spec(type_specifiers::LONG_LONG);
} else {
num_bits = 32;
type.add_spec(type_specifiers::INT);
}
if (is_unsigned) {
type.add_spec(type_specifiers::UNSIGNED);
}
// Not exactly as the spec wants since in the spec a literal with no suffix
// can also have a type long long if it can't be represented by the int type
// See 6.4.4.1/5
llvm::APInt int_val =
llvm::APInt(num_bits, str.substr(start, end - start + 1), radix);
/* llvm::outs() << "Got val ";
int_val.print(llvm::outs(), !is_unsigned);
llvm::outs() << " from string " << str << '\n'; */
return new const_expr(
llvm::ConstantInt::get(type.get_type().llvm_type, int_val), !is_unsigned,
str);
}
// This is a last minute addition, so doesn't support anything other than that
// supported by std::stod
const_expr *const_expr::new_floating_expr(const char *s) {
std::string str(s);
free((void *)s);
// Always make a double
double val = std::stod(str);
type_i type = type_specifiers(type_specifiers::DOUBLE).get_type();
return new const_expr(llvm::ConstantFP::get(type.llvm_type, val), true, str);
}
value const_expr::get_val(int num) {
return get_val(num, ir_builder.getInt32Ty());
}
value const_expr::get_val(int num, type_i type) {
return get_val(num, type.llvm_type);
}
value const_expr::get_val(int num, llvm::Type *type) {
value val;
val.is_signed = true;
auto *int_type = llvm::dyn_cast<llvm::IntegerType>(type);