-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCirCode.c
1573 lines (1418 loc) · 51.8 KB
/
CirCode.c
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 "cir_internal.h"
#include <assert.h>
#include <stdlib.h>
#define MAX_CODE (1024 * 1024 * 1)
// data1:
// bits 31 to 29: type
// bit 28: invalidation bit
#define CIRCODE_EXPR 1
#define CIRCODE_COND 2
#define data1ToType(x) (((x) >> 29) & 0x07)
#define typeToData1(x) (((x) & 0x07) << 29)
#define data1ClearType(x) ((x) & (~(0x07 << 29)))
typedef CirArray(CirStmtId) CirStmtIdArray;
typedef struct CirCode {
uint32_t data1;
CirStmtId firstStmt;
CirStmtId lastStmt;
const CirValue *value;
CirArray(CirVarId) vars;
CirStmtIdArray truejumps; // or nextjumps
CirStmtIdArray falsejumps;
} CirCode;
static CirCode codes[MAX_CODE];
static uint32_t numCodes = 1;
static void
backpatch(CirStmtIdArray *arr, CirStmtId stmt_id)
{
for (size_t i = 0; i < arr->len; i++)
CirStmt_setJumpTarget(arr->items[i], stmt_id);
arr->len = 0;
}
static void
CirCode__setType(CirCodeId code_id, uint32_t type)
{
codes[code_id].data1 = data1ClearType(codes[code_id].data1) | typeToData1(type);
}
void
CirCode__setLastStmt(CirCodeId code_id, CirStmtId stmt_id)
{
codes[code_id].lastStmt = stmt_id;
}
void
CirCode__setFirstStmt(CirCodeId code_id, CirStmtId stmt_id)
{
codes[code_id].firstStmt = stmt_id;
}
CirCodeId
CirCode_ofExpr(const CirValue *value)
{
if (numCodes >= MAX_CODE)
cir_bug("CirCode_ofExpr: too many CirCodes");
CirCodeId code_id = numCodes++;
codes[code_id].data1 = typeToData1(CIRCODE_EXPR);
codes[code_id].firstStmt = 0;
codes[code_id].lastStmt = 0;
codes[code_id].value = value;
return code_id;
}
CirCodeId
CirCode_ofCond(void)
{
if (numCodes >= MAX_CODE)
cir_bug("CirCode_ofCond: too many CirCodes");
CirCodeId code_id = numCodes++;
codes[code_id].data1 = typeToData1(CIRCODE_COND);
codes[code_id].firstStmt = 0;
codes[code_id].lastStmt = 0;
codes[code_id].value = NULL;
return code_id;
}
void
CirCode_free(CirCodeId code_id)
{
if (!code_id)
return;
codes[code_id].data1 |= 0x10000000;
CirArray_release(&codes[code_id].vars);
CirArray_release(&codes[code_id].truejumps);
CirArray_release(&codes[code_id].falsejumps);
}
static bool
CirCode_isFreed(CirCodeId code_id)
{
return codes[code_id].data1 & 0x10000000;
}
void
CirCode__addVar(CirCodeId code_id, CirVarId var_id)
{
assert(code_id != 0);
assert(!CirCode_isFreed(code_id));
CirArray_push(&codes[code_id].vars, &var_id);
}
void
CirCode_addTrueJump(CirCodeId code_id, CirStmtId stmt_id)
{
assert(CirCode_isCond(code_id));
CirArray_push(&codes[code_id].truejumps, &stmt_id);
}
void
CirCode_addFalseJump(CirCodeId code_id, CirStmtId stmt_id)
{
assert(CirCode_isCond(code_id));
CirArray_push(&codes[code_id].falsejumps, &stmt_id);
}
CirStmtId
CirCode_appendNewStmt(CirCodeId code_id)
{
assert(code_id != 0);
assert(!CirCode_isFreed(code_id));
CirStmtId last_stmt_id = CirCode_getLastStmt(code_id);
if (last_stmt_id) {
return CirStmt_newAfter(last_stmt_id);
} else {
last_stmt_id = CirStmt__new(code_id);
CirCode__setFirstStmt(code_id, last_stmt_id);
CirCode__setLastStmt(code_id, last_stmt_id);
return last_stmt_id;
}
}
CirStmtId
CirCode_prependNewStmt(CirCodeId codeId)
{
assert(codeId);
assert(!CirCode_isFreed(codeId));
CirStmtId firstStmtId = CirCode_getFirstStmt(codeId);
if (firstStmtId) {
return CirStmt_newBefore(firstStmtId);
} else {
firstStmtId = CirStmt__new(codeId);
CirCode__setFirstStmt(codeId, firstStmtId);
CirCode__setLastStmt(codeId, firstStmtId);
return firstStmtId;
}
}
void
CirCode_appendOrphanStmt(CirCodeId codeId, CirStmtId stmtId)
{
assert(codeId);
assert(!CirCode_isFreed(codeId));
assert(stmtId);
assert(CirStmt_isOrphan(stmtId));
CirStmtId lastStmtId = CirCode_getLastStmt(codeId);
CirStmt__setNextCode(stmtId, codeId);
CirStmt__setPrevCode(stmtId, codeId);
if (lastStmtId) {
CirStmt__setPrevStmt(stmtId, lastStmtId);
CirStmt__setNextStmt(lastStmtId, stmtId);
CirCode__setLastStmt(codeId, stmtId);
} else {
CirCode__setFirstStmt(codeId, stmtId);
CirCode__setLastStmt(codeId, stmtId);
}
assert(!CirStmt_isOrphan(stmtId));
}
bool
CirCode_isExpr(CirCodeId code_id)
{
assert(!CirCode_isFreed(code_id));
return data1ToType(codes[code_id].data1) == CIRCODE_EXPR;
}
bool
CirCode_isCond(CirCodeId code_id)
{
assert(!CirCode_isFreed(code_id));
return data1ToType(codes[code_id].data1) == CIRCODE_COND;
}
const CirValue *
CirCode_getValue(CirCodeId code_id)
{
assert(code_id != 0);
assert(!CirCode_isFreed(code_id));
if (data1ToType(codes[code_id].data1) != CIRCODE_EXPR)
cir_bug("CirCode_getValue: not a expr code");
return codes[code_id].value;
}
void
CirCode_setValue(CirCodeId code_id, const CirValue *value)
{
assert(code_id != 0);
assert(!CirCode_isFreed(code_id));
if (data1ToType(codes[code_id].data1) != CIRCODE_EXPR)
cir_bug("CirCode_setValue: not a expr code");
codes[code_id].value = value;
}
CirStmtId
CirCode_getFirstStmt(CirCodeId code_id)
{
assert(!CirCode_isFreed(code_id));
CirStmtId stmt_id = codes[code_id].firstStmt;
assert(!stmt_id || !CirStmt_getPrev(stmt_id));
assert(stmt_id || !codes[code_id].lastStmt);
return stmt_id;
}
CirStmtId
CirCode_getLastStmt(CirCodeId code_id)
{
assert(!CirCode_isFreed(code_id));
CirStmtId stmt_id = codes[code_id].lastStmt;
assert(!stmt_id || !CirStmt_getNext(stmt_id));
assert(stmt_id || !codes[code_id].firstStmt);
return stmt_id;
}
static void
appendCode(CirCodeId dst, CirCodeId src)
{
assert(!CirCode_isFreed(dst));
assert(!CirCode_isFreed(src));
// Join the code together
CirStmtId srcFirstStmt = CirCode_getFirstStmt(src);
if (srcFirstStmt) {
// Transfer ownership of src's code to dst
CirStmtId srcLastStmt = CirCode_getLastStmt(src);
CirStmt__setNextCode(srcLastStmt, dst);
CirStmt__setPrevCode(srcFirstStmt, dst);
CirStmtId dstLastStmt = CirCode_getLastStmt(dst);
if (dstLastStmt) {
CirStmt__setNextStmt(dstLastStmt, srcFirstStmt);
CirStmt__setPrevStmt(srcFirstStmt, dstLastStmt);
} else {
codes[dst].firstStmt = srcFirstStmt;
}
codes[dst].lastStmt = srcLastStmt;
}
}
static void
appendVars(CirCodeId dst, CirCodeId src)
{
assert(!CirCode_isFreed(dst));
assert(!CirCode_isFreed(src));
// Join vars together
CirArray_grow(&codes[dst].vars, codes[src].vars.len);
for (size_t i = 0; i < codes[src].vars.len; i++) {
CirVarId v = codes[src].vars.items[i];
assert(CirVar_getOwner(v) == src);
codes[dst].vars.items[codes[dst].vars.len + i] = v;
CirVar__setOwner(v, dst);
}
codes[dst].vars.len += codes[src].vars.len;
}
CirCodeId
CirCode_toExpr(CirCodeId code_id, bool dropValue)
{
if (CirCode_isExpr(code_id)) {
if (dropValue)
CirCode_setValue(code_id, NULL);
return code_id;
} else if (CirCode_isCond(code_id)) {
if (dropValue) {
// Only create a dummy statement if needed
if (codes[code_id].truejumps.len || codes[code_id].falsejumps.len) {
CirStmtId stmt_id = CirCode_appendNewStmt(code_id);
backpatch(&codes[code_id].truejumps, stmt_id);
backpatch(&codes[code_id].falsejumps, stmt_id);
}
// Convert to expr code
CirCode__setType(code_id, CIRCODE_EXPR);
CirCode_setValue(code_id, NULL);
return code_id;
}
// In-place conversion
if (codes[code_id].truejumps.len > 0 && codes[code_id].falsejumps.len > 0) {
CirStmtId stmt_id, jump_id;
// True and false jumps
CirVarId result_id = CirVar_new(code_id);
CirVar_setType(result_id, CirType_int(CIR_IINT));
const CirValue *result_val = CirValue_ofVar(result_id);
// Truejump target
stmt_id = CirCode_appendNewStmt(code_id);
CirStmt_toUnOp(stmt_id, result_val, CIR_UNOP_IDENTITY, CirValue_ofI64(CIR_IINT, 1));
backpatch(&codes[code_id].truejumps, stmt_id);
stmt_id = CirCode_appendNewStmt(code_id);
CirStmt_toGoto(stmt_id, 0);
jump_id = stmt_id;
// Falsejump target
stmt_id = CirCode_appendNewStmt(code_id);
CirStmt_toUnOp(stmt_id, result_val, CIR_UNOP_IDENTITY, CirValue_ofI64(CIR_IINT, 0));
backpatch(&codes[code_id].falsejumps, stmt_id);
// Rest target
stmt_id = CirCode_appendNewStmt(code_id);
CirStmt_setJumpTarget(jump_id, stmt_id);
// Convert to expr code
CirCode__setType(code_id, CIRCODE_EXPR);
CirCode_setValue(code_id, result_val);
} else if (codes[code_id].truejumps.len > 0) {
// Always true.
CirStmtId stmt_id = CirCode_appendNewStmt(code_id);
backpatch(&codes[code_id].truejumps, stmt_id);
// Constant is 1
CirCode__setType(code_id, CIRCODE_EXPR);
CirCode_setValue(code_id, CirValue_ofI64(CIR_IINT, 1));
} else if (codes[code_id].falsejumps.len > 0) {
// Always false.
CirStmtId stmt_id = CirCode_appendNewStmt(code_id);
backpatch(&codes[code_id].truejumps, stmt_id);
// Constant is 0
CirCode__setType(code_id, CIRCODE_EXPR);
CirCode_setValue(code_id, CirValue_ofI64(CIR_IINT, 0));
} else {
cir_bug("CirCode cond without truejumps/falsejumps!?");
}
return code_id;
} else {
cir_bug("CirCode_toExpr: unhandled case");
}
}
static void
CirCode__toEmptyCond(CirCodeId code_id)
{
assert(CirCode_isExpr(code_id));
CirCode__setType(code_id, CIRCODE_COND);
codes[code_id].truejumps.len = 0;
codes[code_id].falsejumps.len = 0;
}
const CirType *
CirCode_getType(CirCodeId code_id)
{
assert(code_id != 0);
assert(!CirCode_isFreed(code_id));
if (CirCode_isExpr(code_id)) {
const CirValue *value = CirCode_getValue(code_id);
return value ? CirValue_getType(value) : CirType_void();
} else if (CirCode_isCond(code_id)) {
return CirType_int(CIR_IINT);
} else {
cir_bug("unreachable");
}
}
void
CirCode_append(CirCodeId dst, CirCodeId src)
{
if (!src)
return;
appendCode(dst, src);
appendVars(dst, src);
if (CirCode_isExpr(dst)) {
if (CirCode_isExpr(src)) {
codes[dst].value = codes[src].value;
} else if (CirCode_isCond(src)) {
CirCode__setType(dst, CIRCODE_COND);
// swap truejumps and falsejumps
{
CirStmtIdArray tmp = codes[dst].truejumps;
codes[dst].truejumps = codes[src].truejumps;
codes[src].truejumps = tmp;
}
{
CirStmtIdArray tmp = codes[dst].falsejumps;
codes[dst].falsejumps = codes[src].falsejumps;
codes[src].falsejumps = tmp;
}
} else {
cir_fatal("unreachable");
}
} else if (CirCode_isCond(dst)) {
if (CirCode_isExpr(src)) {
// No need to do anything
} else if (CirCode_isCond(src)) {
// Append truejumps, falsejumps
CirArray_grow(&codes[dst].truejumps, codes[src].truejumps.len);
memcpy(codes[dst].truejumps.items + codes[dst].truejumps.len, codes[src].truejumps.items, sizeof(CirStmtId) * codes[src].truejumps.len);
codes[dst].truejumps.len += codes[src].truejumps.len;
CirArray_grow(&codes[dst].falsejumps, codes[src].falsejumps.len);
memcpy(codes[dst].falsejumps.items + codes[dst].falsejumps.len, codes[src].falsejumps.items, sizeof(CirStmtId) * codes[src].falsejumps.len);
codes[dst].falsejumps.len += codes[src].falsejumps.len;
} else {
cir_fatal("unreachable");
}
} else {
cir_fatal("incompatible");
}
CirCode_free(src);
}
void
CirCode_dump(CirCodeId cid)
{
if (cid == 0) {
CirLog_print("<CirCode 0>\n");
return;
}
// Starting marker
CirLog_printf("/* cid%u start */\n", (unsigned)cid);
// Log vars
for (size_t i = 0; i < codes[cid].vars.len; i++) {
CirVar_logNameAndType(codes[cid].vars.items[i]);
CirLog_print(";\n");
}
// Log stmts
CirStmtId stmt_id = CirCode_getFirstStmt(cid);
while (stmt_id) {
CirStmt_log(stmt_id);
CirLog_print("\n");
stmt_id = CirStmt_getNext(stmt_id);
}
// Ending marker
if (CirCode_isExpr(cid)) {
CirLog_printf("/* cid%u end, value: ", (unsigned)cid);
CirValue_log(CirCode_getValue(cid));
CirLog_print(" */\n");
} else {
CirLog_printf("/* cid%u end */\n", (unsigned)cid);
}
}
size_t
CirCode_getNumVars(CirCodeId code_id)
{
assert(code_id != 0);
assert(!CirCode_isFreed(code_id));
return codes[code_id].vars.len;
}
CirVarId
CirCode_getVar(CirCodeId code_id, size_t i)
{
assert(code_id != 0);
assert(!CirCode_isFreed(code_id));
assert(i < codes[code_id].vars.len);
return codes[code_id].vars.items[i];
}
static int64_t
truncToIkindS(uint32_t ikind, int64_t val, const CirMachine *mach)
{
switch (CirIkind_size(ikind, mach)) {
case 1:
return (int8_t)val;
case 2:
return (int16_t)val;
case 4:
return (int32_t)val;
case 8:
return val;
default:
cir_bug("truncToIkindS fail");
}
}
static uint64_t
truncToIkindU(uint32_t ikind, uint64_t val, const CirMachine *mach)
{
switch (CirIkind_size(ikind, mach)) {
case 1:
return (uint8_t)val;
case 2:
return (uint16_t)val;
case 4:
return (uint32_t)val;
case 8:
return val;
default:
cir_bug("truncToIkindU fail");
}
}
// Performs arithmetic conversion
#define BINARITH_TEMPLATE(name, runtime_op, comptime_expr) \
lhs = CirCode_toExpr(lhs, false); \
rhs = CirCode_toExpr(rhs, false); \
const CirValue *lhs_value = CirCode_getValue(lhs); \
const CirValue *rhs_value = CirCode_getValue(rhs); \
if (!lhs_value) \
cir_fatal(name ": left operand is void"); \
if (!rhs_value) \
cir_fatal(name ": right operand is void"); \
const CirType *lhs_type = CirValue_getType(lhs_value); \
const CirType *rhs_type = CirValue_getType(rhs_value); \
assert(lhs_type); \
assert(rhs_type); \
CirCode_append(lhs, rhs); \
const CirType *dst_type = CirType__arithmeticConversion(lhs_type, rhs_type, mach); \
if (CirValue_isLval(lhs_value) || CirValue_isLval(rhs_value)) { \
CirVarId dst_id = CirVar_new(lhs); \
const CirValue *dst_var = CirValue_ofVar(dst_id); \
CirVar_setType(dst_id, dst_type); \
CirStmtId stmt_id = CirCode_appendNewStmt(lhs); \
CirStmt_toBinOp(stmt_id, dst_var, runtime_op, lhs_value, rhs_value); \
CirCode_setValue(lhs, dst_var); \
return lhs; \
} else { \
uint32_t ikind = CirType_isInt(dst_type); \
const CirValue *dst_value; \
if (ikind) { \
if (CirIkind_isSigned(ikind, mach)) { \
int64_t a = CirValue_getI64(lhs_value); \
int64_t b = CirValue_getI64(rhs_value); \
int64_t c = comptime_expr; \
dst_value = CirValue_ofI64(ikind, truncToIkindS(ikind, c, mach)); \
} else { \
uint64_t a = CirValue_getU64(lhs_value); \
uint64_t b = CirValue_getU64(rhs_value); \
uint64_t c = comptime_expr; \
dst_value = CirValue_ofU64(ikind, truncToIkindU(ikind, c, mach)); \
} \
} else { \
cir_bug("TODO: cannot handle"); \
} \
CirCode_setValue(lhs, dst_value); \
return lhs; \
}
// Performs integral promotions on lhs and rhs, but chooses type of lhs
#define SHIFT_TEMPLATE(name, runtime_op, comptime_expr) \
lhs = CirCode_toExpr(lhs, false); \
rhs = CirCode_toExpr(rhs, false); \
const CirValue *lhs_value = CirCode_getValue(lhs); \
const CirValue *rhs_value = CirCode_getValue(rhs); \
if (!lhs_value) \
cir_fatal(name ": left operand is void"); \
if (!rhs_value) \
cir_fatal(name ": right operand is void"); \
const CirType *lhs_type = CirValue_getType(lhs_value); \
if (lhs_type) \
lhs_type = CirType__integralPromotion(lhs_type, mach); \
const CirType *rhs_type = CirValue_getType(rhs_value); \
if (!lhs_type || !rhs_type || CirValue_isLval(lhs_value) || CirValue_isLval(rhs_value)) { \
CirVarId dst_id = CirVar_new(lhs); \
const CirValue *dst_value = CirValue_ofVar(dst_id); \
CirVar_setType(dst_id, lhs_type); \
CirStmtId stmt_id = CirCode_appendNewStmt(lhs); \
CirStmt_toBinOp(stmt_id, dst_value, runtime_op, lhs_value, rhs_value); \
CirCode_setValue(lhs, dst_value); \
return lhs; \
} else { \
uint32_t ikind = CirType_isInt(lhs_type); \
const CirValue *dst_value; \
if (CirIkind_isSigned(ikind, mach)) { \
int64_t a = CirValue_getI64(lhs_value); \
int64_t b = CirValue_getI64(rhs_value); \
int64_t c = comptime_expr; \
dst_value = CirValue_ofI64(ikind, truncToIkindS(ikind, c, mach)); \
} else { \
uint64_t a = CirValue_getU64(lhs_value); \
uint64_t b = CirValue_getU64(rhs_value); \
uint64_t c = comptime_expr; \
dst_value = CirValue_ofU64(ikind, truncToIkindU(ikind, c, mach)); \
} \
CirCode_setValue(lhs, dst_value); \
return lhs; \
}
#define RELOP_TEMPLATE(name, runtime_op, comptime_expr) \
lhs = CirCode_toExpr(lhs, false); \
rhs = CirCode_toExpr(rhs, false); \
const CirValue *lhs_value = CirCode_getValue(lhs); \
const CirValue *rhs_value = CirCode_getValue(rhs); \
if (!lhs_value) \
cir_fatal(name ": left operand is void"); \
if (!rhs_value) \
cir_fatal(name ": right operand is void"); \
const CirType *lhs_type = CirValue_getType(lhs_value); \
const CirType *rhs_type = CirValue_getType(rhs_value); \
if (!lhs_type || !rhs_type) \
goto fallback; \
CirCode_append(lhs, rhs); \
const CirType *dst_type = CirType__arithmeticConversion(lhs_type, rhs_type, mach); \
if (CirValue_isLval(lhs_value) || CirValue_isLval(rhs_value)) { \
fallback: \
CirCode__toEmptyCond(lhs); \
CirStmtId stmt_id = CirCode_appendNewStmt(lhs); \
CirStmt_toCmp(stmt_id, runtime_op, lhs_value, rhs_value, 0); \
CirCode_addTrueJump(lhs, stmt_id); \
stmt_id = CirCode_appendNewStmt(lhs); \
CirStmt_toGoto(stmt_id, 0); \
CirCode_addFalseJump(lhs, stmt_id); \
return lhs; \
} else { \
uint32_t ikind = CirType_isInt(dst_type); \
const CirValue *dst_value; \
if (ikind) { \
if (CirIkind_isSigned(ikind, mach)) { \
int64_t a = CirValue_getI64(lhs_value); \
int64_t b = CirValue_getI64(rhs_value); \
dst_value = CirValue_ofI64(CIR_IINT, comptime_expr); \
} else { \
uint64_t a = CirValue_getU64(lhs_value); \
uint64_t b = CirValue_getU64(rhs_value); \
dst_value = CirValue_ofI64(CIR_IINT, comptime_expr); \
} \
} else { \
cir_bug("TODO: cannot handle"); \
} \
CirCode_setValue(lhs, dst_value); \
return lhs; \
}
CirCodeId
CirBuild__mul(CirCodeId lhs, CirCodeId rhs, const CirMachine *mach)
{
BINARITH_TEMPLATE("mul", CIR_BINOP_MUL, a * b)
}
CirCodeId
CirBuild__div(CirCodeId lhs, CirCodeId rhs, const CirMachine *mach)
{
BINARITH_TEMPLATE("div", CIR_BINOP_DIV, a / b)
}
CirCodeId
CirBuild__mod(CirCodeId lhs, CirCodeId rhs, const CirMachine *mach)
{
BINARITH_TEMPLATE("mod", CIR_BINOP_MOD, a % b);
}
static CirCodeId
CirBuild__plusA(CirCodeId lhs, CirCodeId rhs, const CirMachine *mach)
{
BINARITH_TEMPLATE("plusA", CIR_BINOP_PLUS, a + b)
}
static CirCodeId
CirBuild__minusA(CirCodeId lhs, CirCodeId rhs, const CirMachine *mach)
{
BINARITH_TEMPLATE("minusA", CIR_BINOP_MINUS, a - b)
}
CirCodeId
CirBuild__simpleAssign(CirCodeId lhs, CirCodeId rhs, const CirMachine *mach)
{
rhs = CirCode_toExpr(rhs, false);
if (!CirCode_isExpr(lhs))
cir_fatal("simple assign: expected code on lhs");
const CirValue *rhs_value = CirCode_getValue(rhs);
if (!rhs_value)
cir_fatal("simple assign: rhs is void");
const CirValue *lhs_value = CirCode_getValue(lhs);
if (!lhs_value)
cir_fatal("simple assign: lhs is void");
CirCode_append(rhs, lhs);
CirStmtId stmt_id = CirCode_appendNewStmt(rhs);
CirStmt_toUnOp(stmt_id, lhs_value, CIR_UNOP_IDENTITY, rhs_value);
CirCode_setValue(rhs, lhs_value);
return rhs;
}
static void
CirBuild__plusPtr(CirCodeId code_id, const CirType *ptrType, const CirValue *lhsValue, const CirValue *rhsValue)
{
// TODO: Support constant ptrs (e.g. NULL ptr)
CirVarId dst_id = CirVar_new(code_id);
const CirValue *dst_var = CirValue_ofVar(dst_id);
CirVar_setType(dst_id, ptrType);
CirStmtId stmt_id = CirCode_appendNewStmt(code_id);
CirStmt_toBinOp(stmt_id, dst_var, CIR_BINOP_PLUS, lhsValue, rhsValue);
CirCode_setValue(code_id, dst_var);
}
// Handles overloaded +
CirCodeId
CirBuild__plus(CirCodeId lhs, CirCodeId rhs, const CirMachine *mach)
{
lhs = CirCode_toExpr(lhs, false);
rhs = CirCode_toExpr(rhs, false);
const CirValue *lhs_value = CirCode_getValue(lhs);
if (!lhs_value)
cir_fatal("plus: lhs has no value");
const CirValue *rhs_value = CirCode_getValue(rhs);
if (!rhs_value)
cir_fatal("plus: rhs has no value");
const CirType *lhs_type = CirValue_getType(lhs_value);
const CirType *rhs_type = CirValue_getType(rhs_value);
if (!lhs_type || !rhs_type)
goto fallback;
lhs_type = CirType_lvalConv(lhs_type);
rhs_type = CirType_lvalConv(rhs_type);
const CirType *lhs_unrolledType = CirType_unroll(lhs_type);
const CirType *rhs_unrolledType = CirType_unroll(rhs_type);
if (CirType_isArithmetic(lhs_unrolledType) && CirType_isArithmetic(rhs_unrolledType)) {
return CirBuild__plusA(lhs, rhs, mach);
} else if (CirType_isPtr(lhs_unrolledType) && CirType_isInt(rhs_unrolledType)) {
CirCode_append(lhs, rhs);
CirBuild__plusPtr(lhs, lhs_type, lhs_value, rhs_value);
return lhs;
} else if (CirType_isInt(lhs_unrolledType) && CirType_isPtr(rhs_unrolledType)) {
CirCode_append(lhs, rhs);
CirBuild__plusPtr(lhs, rhs_type, lhs_value, rhs_value);
return lhs;
}
fallback:
CirCode_append(lhs, rhs);
CirVarId dst_id = CirVar_new(lhs);
const CirValue *dst_var = CirValue_ofVar(dst_id);
CirStmtId stmt_id = CirCode_appendNewStmt(lhs);
CirStmt_toBinOp(stmt_id, dst_var, CIR_BINOP_PLUS, lhs_value, rhs_value);
CirCode_setValue(lhs, dst_var);
return lhs;
}
CirCodeId
CirBuild__arraySubscript(CirCodeId lhs, CirCodeId rhs, const CirMachine *mach)
{
CirCodeId code_id = CirBuild__plus(lhs, rhs, mach);
const CirValue *value = CirCode_getValue(code_id);
assert(value);
assert(CirValue_isVar(value));
assert(!CirValue_getNumFields(value));
CirVarId var_id = CirValue_getVar(value);
CirCode_setValue(code_id, CirValue_ofMem(var_id));
return code_id;
}
static void
CirBuild__minusPtr(CirCodeId code_id, const CirType *ptrType, const CirValue *lhsValue, const CirValue *rhsValue)
{
// TODO: Support constant ptrs (e.g. NULL ptr)
CirVarId dst_id = CirVar_new(code_id);
const CirValue *dst_var = CirValue_ofVar(dst_id);
CirVar_setType(dst_id, ptrType);
CirStmtId stmt_id = CirCode_appendNewStmt(code_id);
CirStmt_toBinOp(stmt_id, dst_var, CIR_BINOP_MINUS, lhsValue, rhsValue);
CirCode_setValue(code_id, dst_var);
}
// Handles overloaded -
CirCodeId
CirBuild__minus(CirCodeId lhs, CirCodeId rhs, const CirMachine *mach)
{
lhs = CirCode_toExpr(lhs, false);
rhs = CirCode_toExpr(rhs, false);
const CirValue *lhs_value = CirCode_getValue(lhs);
if (!lhs_value)
cir_fatal("minus: lhs has no value");
const CirValue *rhs_value = CirCode_getValue(rhs);
if (!rhs_value)
cir_fatal("minus: rhs has no value");
const CirType *lhs_type = CirValue_getType(lhs_value);
const CirType *rhs_type = CirValue_getType(rhs_value);
if (!lhs_type || !rhs_type)
goto fallback;
lhs_type = CirType_lvalConv(lhs_type);
rhs_type = CirType_lvalConv(rhs_type);
const CirType *lhs_unrolledType = CirType_unroll(lhs_type);
const CirType *rhs_unrolledType = CirType_unroll(rhs_type);
if (CirType_isArithmetic(lhs_unrolledType) && CirType_isArithmetic(rhs_unrolledType)) {
return CirBuild__minusA(lhs, rhs, mach);
} else if (CirType_isPtr(lhs_unrolledType) && CirType_isInt(rhs_unrolledType)) {
CirCode_append(lhs, rhs);
CirBuild__minusPtr(lhs, lhs_type, lhs_value, rhs_value);
return lhs;
} else if (CirType_isPtr(lhs_unrolledType) && CirType_isPtr(rhs_unrolledType)) {
cir_bug("TODO: MinusPP");
}
fallback:
CirCode_append(lhs, rhs);
CirVarId dst_id = CirVar_new(lhs);
const CirValue *dst_var = CirValue_ofVar(dst_id);
CirStmtId stmt_id = CirCode_appendNewStmt(lhs);
CirStmt_toBinOp(stmt_id, dst_var, CIR_BINOP_MINUS, lhs_value, rhs_value);
CirCode_setValue(lhs, dst_var);
return lhs;
}
CirCodeId
CirBuild__call(CirCodeId target, const CirCodeId *args, size_t numArgs, const CirMachine *mach)
{
const CirValue *targetValue = CirCode_getValue(target);
if (!targetValue)
cir_fatal("CirBuild__call: target has no value");
// Get the result type
const CirType *targetType = CirCode_getType(target);
const CirType *targetTypeUnrolled = CirType_unroll(targetType);
const CirType *returnType;
if (CirType_isFun(targetTypeUnrolled)) {
returnType = CirType_getBaseType(targetTypeUnrolled);
} else if (CirType_isPtr(targetTypeUnrolled)) {
const CirType *bt = CirType_getBaseType(targetTypeUnrolled);
const CirType *btUnrolled = CirType_unroll(bt);
if (!CirType_isFun(btUnrolled))
goto target_type_fail;
returnType = CirType_getBaseType(btUnrolled);
} else {
target_type_fail:
CirLog_begin(CIRLOG_FATAL);
CirLog_print("CirBuild__call: ");
CirType_log(targetType, NULL);
CirLog_print(" is not callable");
CirLog_end();
exit(1);
}
// Evaluate arguments from right-to-left
CirArray(const CirValue *) argValues = CIRARRAY_INIT;
CirArray_alloc(&argValues, numArgs);
CirCodeId argCode = 0;
for (size_t i = 0; i < numArgs; i++) {
CirCodeId code_id = args[numArgs - i - 1];
argValues.items[numArgs - i - 1] = CirCode_getValue(code_id);
if (!argCode)
argCode = code_id;
else
CirCode_append(argCode, code_id);
}
if (!argCode)
argCode = CirCode_ofExpr(NULL);
const CirValue *dst;
if (CirType_isVoid(returnType)) {
dst = NULL;
CirCode_setValue(argCode, NULL);
} else {
// Create temporary variable for return
CirVarId result_vid = CirVar_new(argCode);
CirVar_setType(result_vid, returnType);
dst = CirValue_ofVar(result_vid);
CirCode_setValue(argCode, dst);
}
CirStmtId callstmt_id = CirCode_appendNewStmt(argCode);
CirStmt_toCall(callstmt_id, dst, targetValue, argValues.items, numArgs);
CirArray_release(&argValues);
return argCode;
}
CirCodeId
CirBuild__lt(CirCodeId lhs, CirCodeId rhs, const CirMachine *mach)
{
RELOP_TEMPLATE("LT", CIR_CONDOP_LT, a < b)
}
CirCodeId
CirBuild__le(CirCodeId lhs, CirCodeId rhs, const CirMachine *mach)
{
RELOP_TEMPLATE("LE", CIR_CONDOP_LE, a <= b)
}
CirCodeId
CirBuild__gt(CirCodeId lhs, CirCodeId rhs, const CirMachine *mach)
{
RELOP_TEMPLATE("GT", CIR_CONDOP_GT, a > b)
}
CirCodeId
CirBuild__ge(CirCodeId lhs, CirCodeId rhs, const CirMachine *mach)
{
RELOP_TEMPLATE("GE", CIR_CONDOP_GE, a >= b)
}
static CirCodeId
CirBuild__eqA(CirCodeId lhs, CirCodeId rhs, const CirMachine *mach)
{
RELOP_TEMPLATE("EQ_A", CIR_CONDOP_EQ, a == b);
}
CirCodeId
CirBuild__eq(CirCodeId lhs, CirCodeId rhs, const CirMachine *mach)
{
lhs = CirCode_toExpr(lhs, false);
rhs = CirCode_toExpr(rhs, false);
const CirValue *lhs_value = CirCode_getValue(lhs);
if (!lhs_value)
cir_fatal("eq: lhs has no value");
const CirValue *rhs_value = CirCode_getValue(rhs);
if (!rhs_value)
cir_fatal("eq: rhs has no value");
const CirType *lhs_type = CirValue_getType(lhs_value);
const CirType *rhs_type = CirValue_getType(rhs_value);
if (lhs_type && rhs_type && CirType_isArithmetic(lhs_type) && CirType_isArithmetic(rhs_type)) {
return CirBuild__eqA(lhs, rhs, mach);
} else {
CirCode_append(lhs, rhs);
CirCode__toEmptyCond(lhs);
CirStmtId stmt_id = CirCode_appendNewStmt(lhs);
CirStmt_toCmp(stmt_id, CIR_CONDOP_EQ, lhs_value, rhs_value, 0);
CirCode_addTrueJump(lhs, stmt_id);
stmt_id = CirCode_appendNewStmt(lhs);
CirStmt_toGoto(stmt_id, 0);
CirCode_addFalseJump(lhs, stmt_id);
return lhs;
}
}
static CirCodeId
CirBuild__neA(CirCodeId lhs, CirCodeId rhs, const CirMachine *mach)
{
RELOP_TEMPLATE("NE_A", CIR_CONDOP_NE, a != b);
}
CirCodeId
CirBuild__ne(CirCodeId lhs, CirCodeId rhs, const CirMachine *mach)
{
lhs = CirCode_toExpr(lhs, false);
rhs = CirCode_toExpr(rhs, false);
const CirValue *lhs_value = CirCode_getValue(lhs);
if (!lhs_value)
cir_fatal("ne: lhs has no value");
const CirValue *rhs_value = CirCode_getValue(rhs);
if (!rhs_value)
cir_fatal("ne: rhs has no value");
const CirType *lhs_type = CirValue_getType(lhs_value);
const CirType *rhs_type = CirValue_getType(rhs_value);
if (lhs_type && rhs_type && CirType_isArithmetic(lhs_type) && CirType_isArithmetic(rhs_type)) {
return CirBuild__neA(lhs, rhs, mach);
} else {
CirCode_append(lhs, rhs);
CirCode__toEmptyCond(lhs);
CirStmtId stmt_id = CirCode_appendNewStmt(lhs);
CirStmt_toCmp(stmt_id, CIR_CONDOP_NE, lhs_value, rhs_value, 0);
CirCode_addTrueJump(lhs, stmt_id);
stmt_id = CirCode_appendNewStmt(lhs);
CirStmt_toGoto(stmt_id, 0);
CirCode_addFalseJump(lhs, stmt_id);
return lhs;
}
}
static void
toCond(CirCodeId code_id, CirStmtId *firstStmt)
{
assert(CirCode_isExpr(code_id));
// Need to generate a runtime cmp
const CirValue *value = CirCode_getValue(code_id);
if (!value)
cir_fatal("toCond: no value");
CirCode__toEmptyCond(code_id);
if (CirValue_isInt(value)) {
// True or false jump only
CirStmtId stmt_id = CirCode_appendNewStmt(code_id);
if (firstStmt)
*firstStmt = stmt_id;
CirStmt_toGoto(stmt_id, 0);
if (CirValue_getU64(value))
CirCode_addTrueJump(code_id, stmt_id);
else
CirCode_addFalseJump(code_id, stmt_id);
} else if (CirValue_isString(value)) {
// True jump only
CirStmtId stmt_id = CirCode_appendNewStmt(code_id);
if (firstStmt)
*firstStmt = stmt_id;
CirStmt_toGoto(stmt_id, 0);
CirCode_addTrueJump(code_id, stmt_id);
} else {
// True jump and false jump
CirStmtId stmt_id = CirCode_appendNewStmt(code_id);
CirStmt_toCmp(stmt_id, CIR_CONDOP_NE, value, CirValue_ofI64(CIR_IINT, 0), 0);
if (firstStmt)
*firstStmt = stmt_id;
CirCode_addTrueJump(code_id, stmt_id);