-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.c
3428 lines (2831 loc) · 110 KB
/
build.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 "build.h"
#include "../alloc.h"
#include "../compiler.h"
#include "../lex.h"
#include "../typechk.h"
#include "../util.h"
#include "../var_table.h"
#include "../vector.h"
#include "ir.h"
#include "prettyprint.h"
#include "var_refs.h"
#include <math.h>
// break/continues will add an entry into the jumps vector
// and then at the end of the loop these will be traversed and fixed to point to
// the correct basicblock the special value IR_JUMP_TY_NEW_LOOP indicates the
// start of a loop
enum ir_jump_ty {
IR_JUMP_TY_NEW_LOOP,
IR_JUMP_TY_BREAK,
IR_JUMP_TY_CONTINUE
};
struct ir_jump {
enum ir_jump_ty ty;
struct ir_basicblock *basicblock;
};
enum ir_case_ty { IR_CASE_TY_CASE, IR_CASE_TY_DEFAULT };
struct ir_case {
enum ir_case_ty ty;
struct ir_split_case split_case;
};
// linked list of label -> bb mappings
struct ir_label {
const char *name;
struct ir_basicblock *basicblock;
struct ir_label *succ;
};
struct ir_func_builder {
struct ir_unit *unit;
struct ir_func *func;
struct typechk *tchk;
struct var_refs *var_refs;
struct var_refs *global_var_refs;
struct ir_label *labels;
struct vector *jumps;
struct vector *switch_cases;
};
static struct ir_label *add_label(struct ir_func_builder *irb, const char *name,
struct ir_basicblock *basicblock) {
struct ir_label *label = arena_alloc(irb->func->arena, sizeof(*label));
label->name = name;
label->basicblock = basicblock;
label->succ = irb->labels;
irb->labels = label;
return label;
}
static struct var_key get_var_key(const struct td_var *var,
struct ir_basicblock *basicblock) {
return (struct var_key){var->identifier, var->scope,
.basicblock = basicblock};
}
static void get_var_ref(struct ir_func_builder *irb,
struct ir_basicblock *basicblock, struct td_var *var,
struct var_key *key, struct var_ref **ref) {
// debug_assert(var->ty != TD_VAR_TY_ENUM_CNST,
// "can't get var ref for enum cnst");
*ref = NULL;
// this is when we are _reading_ from the var
*key = get_var_key(var, basicblock);
*ref = var_refs_get(irb->var_refs, key);
if (*ref) {
return;
}
*ref = var_refs_get(irb->var_refs, key);
if (*ref && (*ref)->op->lcl) {
return;
}
*ref = var_refs_get(irb->global_var_refs, key);
}
static bool var_ty_eq(struct ir_func *irb, const struct ir_var_ty *l,
const struct ir_var_ty *r) {
if (l == r) {
return true;
}
if (l->ty != r->ty) {
return false;
}
switch (l->ty) {
case IR_VAR_TY_TY_NONE:
return r->ty == IR_VAR_TY_TY_NONE;
case IR_VAR_TY_TY_PRIMITIVE:
return l->primitive == r->primitive;
case IR_VAR_TY_TY_VARIADIC:
return r->ty == IR_VAR_TY_TY_VARIADIC;
case IR_VAR_TY_TY_POINTER:
return true;
case IR_VAR_TY_TY_ARRAY:
return l->array.num_elements == r->array.num_elements &&
var_ty_eq(irb, l->array.underlying, r->array.underlying);
case IR_VAR_TY_TY_FUNC:
if (!var_ty_eq(irb, l->func.ret_ty, r->func.ret_ty)) {
return false;
}
if (l->func.num_params != r->func.num_params) {
return false;
}
for (size_t i = 0; i < l->func.num_params; i++) {
if (!var_ty_eq(irb, &l->func.params[i], &r->func.params[i])) {
return false;
}
}
return true;
case IR_VAR_TY_TY_STRUCT: {
if (l->struct_ty.num_fields != r->struct_ty.num_fields) {
return false;
}
struct ir_var_ty_info l_info = var_ty_info(irb->unit, l);
struct ir_var_ty_info r_info = var_ty_info(irb->unit, r);
// currently we do not have custom alignment/size but it is possible
if (l_info.size != r_info.size || l_info.alignment != r_info.alignment) {
return false;
}
for (size_t i = 0; i < l->struct_ty.num_fields; i++) {
if (!var_ty_eq(irb, &l->struct_ty.fields[i], &r->struct_ty.fields[i])) {
return false;
}
}
return true;
}
case IR_VAR_TY_TY_UNION: {
if (l->union_ty.num_fields != r->union_ty.num_fields) {
return false;
}
struct ir_var_ty_info l_info = var_ty_info(irb->unit, l);
struct ir_var_ty_info r_info = var_ty_info(irb->unit, r);
// currently we do not have custom alignment/size but it is possible
if (l_info.size != r_info.size || l_info.alignment != r_info.alignment) {
return false;
}
for (size_t i = 0; i < l->union_ty.num_fields; i++) {
if (!var_ty_eq(irb, &l->union_ty.fields[i], &r->union_ty.fields[i])) {
return false;
}
}
return true;
}
}
unreachable();
}
static bool var_ty_needs_cast_op(struct ir_func_builder *irb,
const struct ir_var_ty *l,
const struct ir_var_ty *r) {
// note: `l` is TO, `r` is FROM, (as this is in the context of `l <- r`)
if (l->ty == IR_VAR_TY_TY_NONE) {
// void casts are nop
return false;
}
if (var_ty_is_aggregate(l) && var_ty_is_aggregate(r)) {
// casting between these could require conversion,
// but never a cast op
return false;
}
if (var_ty_eq(irb->func, l, r)) {
return false;
}
if ((l->ty == IR_VAR_TY_TY_FUNC && r->ty == IR_VAR_TY_TY_POINTER) ||
(r->ty == IR_VAR_TY_TY_FUNC && l->ty == IR_VAR_TY_TY_POINTER)) {
return false;
}
if ((l->ty == IR_VAR_TY_TY_POINTER || l->ty == IR_VAR_TY_TY_ARRAY) &&
(r->ty == IR_VAR_TY_TY_POINTER || r->ty == IR_VAR_TY_TY_ARRAY)) {
// pointers/arrays need no cast instr
return false;
}
// TODO: hardcodes pointer size
if (((l->ty == IR_VAR_TY_TY_PRIMITIVE &&
l->primitive == IR_VAR_PRIMITIVE_TY_I64) ||
l->ty == IR_VAR_TY_TY_POINTER) &&
((r->ty == IR_VAR_TY_TY_PRIMITIVE &&
r->primitive == IR_VAR_PRIMITIVE_TY_I64) ||
r->ty == IR_VAR_TY_TY_POINTER)) {
// same size int -> pointer needs no cast
return false;
}
return true;
}
static enum ir_var_primitive_ty
var_ty_for_well_known_ty(enum well_known_ty wkt) {
switch (wkt) {
case WELL_KNOWN_TY_CHAR:
case WELL_KNOWN_TY_SIGNED_CHAR:
case WELL_KNOWN_TY_UNSIGNED_CHAR:
return IR_VAR_PRIMITIVE_TY_I8;
case WELL_KNOWN_TY_SIGNED_SHORT:
case WELL_KNOWN_TY_UNSIGNED_SHORT:
return IR_VAR_PRIMITIVE_TY_I16;
case WELL_KNOWN_TY_SIGNED_INT:
case WELL_KNOWN_TY_UNSIGNED_INT:
return IR_VAR_PRIMITIVE_TY_I32;
case WELL_KNOWN_TY_SIGNED_LONG:
case WELL_KNOWN_TY_UNSIGNED_LONG:
return IR_VAR_PRIMITIVE_TY_I64;
case WELL_KNOWN_TY_SIGNED_LONG_LONG:
case WELL_KNOWN_TY_UNSIGNED_LONG_LONG:
return IR_VAR_PRIMITIVE_TY_I64;
case WELL_KNOWN_TY_HALF:
return IR_VAR_PRIMITIVE_TY_F16;
case WELL_KNOWN_TY_FLOAT:
return IR_VAR_PRIMITIVE_TY_F32;
case WELL_KNOWN_TY_DOUBLE:
case WELL_KNOWN_TY_LONG_DOUBLE:
return IR_VAR_PRIMITIVE_TY_F64;
}
}
static struct ir_var_ty var_ty_for_td_var_ty(struct ir_unit *iru,
const struct td_var_ty *var_ty) {
switch (var_ty->ty) {
case TD_VAR_TY_TY_UNKNOWN:
case TD_VAR_TY_TY_INCOMPLETE_AGGREGATE:
bug("shouldn't reach IR gen with unresolved type");
case TD_VAR_TY_TY_AGGREGATE: {
struct td_ty_aggregate aggregate = var_ty->aggregate;
struct ir_var_ty ty;
switch (aggregate.ty) {
case TD_TY_AGGREGATE_TY_STRUCT:
ty.ty = IR_VAR_TY_TY_STRUCT;
ty.struct_ty.num_fields = aggregate.num_fields;
ty.struct_ty.fields = arena_alloc(
iru->arena, sizeof(struct ir_var_ty) * ty.struct_ty.num_fields);
for (size_t i = 0; i < ty.struct_ty.num_fields; i++) {
// handle nested types
ty.struct_ty.fields[i] =
var_ty_for_td_var_ty(iru, &aggregate.fields[i].var_ty);
}
break;
case TD_TY_AGGREGATE_TY_UNION:
ty.ty = IR_VAR_TY_TY_UNION;
ty.union_ty.num_fields = aggregate.num_fields;
ty.union_ty.fields = arena_alloc(iru->arena, sizeof(struct ir_var_ty) *
ty.union_ty.num_fields);
for (size_t i = 0; i < ty.union_ty.num_fields; i++) {
// handle nested types
ty.struct_ty.fields[i] =
var_ty_for_td_var_ty(iru, &aggregate.fields[i].var_ty);
}
break;
}
return ty;
}
case TD_VAR_TY_TY_VOID:
return IR_VAR_TY_NONE;
case TD_VAR_TY_TY_VARIADIC:
return IR_VAR_TY_VARIADIC;
case TD_VAR_TY_TY_WELL_KNOWN: {
struct ir_var_ty ty;
ty.ty = IR_VAR_TY_TY_PRIMITIVE;
ty.primitive = var_ty_for_well_known_ty(var_ty->well_known);
return ty;
}
case TD_VAR_TY_TY_FUNC: {
bool variadic = var_ty->func.ty == TD_TY_FUNC_TY_VARIADIC;
struct ir_var_ty ty;
ty.ty = IR_VAR_TY_TY_FUNC;
ty.func.ret_ty = arena_alloc(iru->arena, sizeof(*ty.func.ret_ty));
*ty.func.ret_ty = var_ty_for_td_var_ty(iru, var_ty->func.ret);
// from IR onwards, variadic is no longer a param of the function but
// instead a flag
ty.func.num_params = var_ty->func.num_params;
ty.func.params =
arena_alloc(iru->arena, sizeof(struct ir_op) * ty.func.num_params);
ty.func.flags = IR_VAR_FUNC_TY_FLAG_NONE;
if (variadic) {
ty.func.flags |= IR_VAR_FUNC_TY_FLAG_VARIADIC;
}
for (size_t i = 0; i < ty.func.num_params; i++) {
ty.func.params[i] =
var_ty_for_td_var_ty(iru, &var_ty->func.params[i].var_ty);
}
return ty;
}
case TD_VAR_TY_TY_POINTER: {
return IR_VAR_TY_POINTER;
}
case TD_VAR_TY_TY_ARRAY: {
struct ir_var_ty underlying =
var_ty_for_td_var_ty(iru, var_ty->array.underlying);
return var_ty_make_array(iru, &underlying, var_ty->array.size);
}
}
}
UNUSED struct ir_var_ty static var_ty_return_ty_for_td_var_ty(
struct ir_func_builder *irb, const struct td_var_ty *ty_ref) {
invariant_assert(ty_ref->ty == TD_VAR_TY_TY_FUNC,
"passed non-func to `return_ty_for_td_var_ty`");
struct ir_var_ty func_ty = var_ty_for_td_var_ty(irb->unit, ty_ref);
return *func_ty.func.ret_ty;
}
static enum ir_op_cast_op_ty cast_ty_for_td_var_ty(struct ir_func_builder *irb,
const struct td_var_ty *from,
const struct td_var_ty *to) {
struct ir_var_ty from_var_ty = var_ty_for_td_var_ty(irb->unit, from);
struct ir_var_ty to_var_ty = var_ty_for_td_var_ty(irb->unit, to);
if (from_var_ty.ty == IR_VAR_TY_TY_POINTER &&
to_var_ty.ty == IR_VAR_TY_TY_POINTER) {
bug("cast between pointer types is implicit");
}
if (from_var_ty.ty == IR_VAR_TY_TY_PRIMITIVE &&
to_var_ty.ty == IR_VAR_TY_TY_POINTER) {
// primitive -> pointer
// TODO: hardcodes pointer size
if (from_var_ty.primitive == IR_VAR_PRIMITIVE_TY_I64) {
bug("cast between primitive & pointer type of same size is implicit");
}
if (WKT_IS_SIGNED(from->well_known)) {
return IR_OP_CAST_OP_TY_SEXT;
} else {
return IR_OP_CAST_OP_TY_ZEXT;
}
}
if (from_var_ty.ty == IR_VAR_TY_TY_POINTER &&
to_var_ty.ty == IR_VAR_TY_TY_PRIMITIVE) {
return IR_OP_CAST_OP_TY_TRUNC;
}
if (from_var_ty.ty != IR_VAR_TY_TY_PRIMITIVE ||
to_var_ty.ty != IR_VAR_TY_TY_PRIMITIVE) {
todo("casts for non prims/pointers (from %d -> %d)", from_var_ty.ty,
to_var_ty.ty);
}
if (is_fp_ty(from) && is_fp_ty(to)) {
return IR_OP_CAST_OP_TY_CONV;
}
if (is_fp_ty(from) || is_fp_ty(to)) {
// one (but not both) is fp
// we need to generate `uconv`/`iconv` depending on the sign of the integral
// type
invariant_assert(from->ty == TD_VAR_TY_TY_WELL_KNOWN ||
to->ty == TD_VAR_TY_TY_WELL_KNOWN,
"other type must be an integer for float conversion");
bool is_signed = is_fp_ty(from) ? WKT_IS_SIGNED(to->well_known)
: WKT_IS_SIGNED(from->well_known);
return is_signed ? IR_OP_CAST_OP_TY_SCONV : IR_OP_CAST_OP_TY_UCONV;
}
if (to_var_ty.primitive < from_var_ty.primitive) {
return IR_OP_CAST_OP_TY_TRUNC;
} else {
invariant_assert(from_var_ty.primitive != to_var_ty.primitive,
"cast not needed for types of same size");
if (WKT_IS_SIGNED(from->well_known)) {
return IR_OP_CAST_OP_TY_SEXT;
} else {
return IR_OP_CAST_OP_TY_ZEXT;
}
}
}
static struct ir_op *build_ir_for_expr(struct ir_func_builder *irb,
struct ir_stmt **stmt,
struct td_expr *expr);
static struct ir_op *insert_ir_for_cast(struct ir_func_builder *irb,
struct ir_stmt *stmt, struct ir_op *op,
const struct ir_var_ty *to,
enum ir_op_cast_op_ty ty) {
struct ir_op *cast = alloc_ir_op(irb->func, stmt);
cast->ty = IR_OP_TY_CAST_OP;
cast->var_ty = *to;
cast->cast_op.ty = ty;
cast->cast_op.value = op;
return cast;
}
struct ir_build_binaryop {
enum td_binary_op_ty ty;
struct td_var_ty result_ty;
struct td_var_ty lhs_ty, rhs_ty;
struct ir_op *lhs, *rhs;
};
static struct ir_op *alloc_binaryop(struct ir_func_builder *irb,
struct ir_stmt *stmt,
const struct ir_build_binaryop *args) {
enum td_binary_op_ty ty = args->ty;
struct td_var_ty lhs_ty = args->lhs_ty, rhs_ty = args->rhs_ty;
struct ir_op *lhs = args->lhs, *rhs = args->rhs;
const struct td_var_ty *td_var_ty = &args->result_ty;
invariant_assert(lhs->var_ty.ty != IR_VAR_TY_TY_ARRAY ||
rhs->var_ty.ty != IR_VAR_TY_TY_ARRAY,
"array should have decayed to ptr");
struct ir_var_ty var_ty = var_ty_for_td_var_ty(irb->unit, td_var_ty);
if (!td_binary_op_is_comparison(ty) && (lhs_ty.ty == TD_VAR_TY_TY_POINTER ||
rhs_ty.ty == TD_VAR_TY_TY_POINTER)) {
if (td_var_ty->ty == TD_VAR_TY_TY_WELL_KNOWN) {
struct td_var_ty *pointer_ty =
lhs_ty.ty == TD_VAR_TY_TY_POINTER ? &lhs_ty : &rhs_ty;
// need to multiply rhs by the element size
struct ir_var_ty el_ty =
var_ty_for_td_var_ty(irb->unit, pointer_ty->pointer.underlying);
struct ir_var_ty_info el_info = var_ty_info(irb->unit, &el_ty);
struct ir_op *el_size_op = alloc_ir_op(irb->func, stmt);
make_pointer_constant(irb->unit, el_size_op, el_info.size);
struct ir_op *diff = alloc_ir_op(irb->func, stmt);
diff->ty = IR_OP_TY_BINARY_OP;
diff->var_ty = var_ty;
diff->binary_op.ty = IR_OP_BINARY_OP_TY_SUB;
diff->binary_op.lhs = lhs;
diff->binary_op.rhs = rhs;
struct ir_op *op = alloc_ir_op(irb->func, stmt);
op->ty = IR_OP_TY_BINARY_OP;
op->var_ty = var_ty;
op->binary_op.ty = IR_OP_BINARY_OP_TY_SDIV;
op->binary_op.lhs = diff;
op->binary_op.rhs = el_size_op;
return op;
} else {
debug_assert(td_var_ty->ty == TD_VAR_TY_TY_POINTER, "non pointer");
// need to multiply rhs by the element size
struct ir_var_ty el_ty =
var_ty_for_td_var_ty(irb->unit, td_var_ty->pointer.underlying);
struct ir_var_ty_info el_info = var_ty_info(irb->unit, &el_ty);
struct ir_op *el_size_op = alloc_ir_op(irb->func, stmt);
make_pointer_constant(irb->unit, el_size_op, el_info.size);
struct ir_op *rhs_mul = alloc_ir_op(irb->func, stmt);
rhs_mul->ty = IR_OP_TY_BINARY_OP;
rhs_mul->var_ty = var_ty;
rhs_mul->binary_op.ty = IR_OP_BINARY_OP_TY_MUL;
rhs_mul->binary_op.lhs = el_size_op;
rhs_mul->binary_op.rhs = rhs;
struct ir_op *op = alloc_ir_op(irb->func, stmt);
op->ty = IR_OP_TY_BINARY_OP;
op->var_ty = var_ty;
op->binary_op.ty = ty == TD_BINARY_OP_TY_ADD ? IR_OP_BINARY_OP_TY_ADD
: IR_OP_BINARY_OP_TY_SUB;
op->binary_op.lhs = lhs;
op->binary_op.rhs = rhs_mul;
return op;
}
}
struct ir_op *op = alloc_ir_op(irb->func, stmt);
op->ty = IR_OP_TY_BINARY_OP;
op->var_ty = var_ty;
struct ir_op_binary_op *b = &op->binary_op;
b->lhs = lhs;
b->rhs = rhs;
bool is_fp = var_ty_is_fp(&op->binary_op.lhs->var_ty);
debug_assert(is_fp == var_ty_is_fp(&op->binary_op.rhs->var_ty),
"type mismatch between lhs/rhs");
invariant_assert(
td_var_ty->ty == TD_VAR_TY_TY_WELL_KNOWN ||
td_var_ty->ty == TD_VAR_TY_TY_POINTER,
"non primitives/well-knowns/pointers cannot be used in binary "
"expression by point IR is reached!");
switch (ty) {
case TD_BINARY_OP_TY_LOGICAL_AND:
case TD_BINARY_OP_TY_LOGICAL_OR:
bug("logical and/or must be handled outside (as they need basicblock "
"adjustment)");
case TD_BINARY_OP_TY_EQ:
b->ty = is_fp ? IR_OP_BINARY_OP_TY_FEQ : IR_OP_BINARY_OP_TY_EQ;
break;
case TD_BINARY_OP_TY_NEQ:
b->ty = is_fp ? IR_OP_BINARY_OP_TY_FNEQ : IR_OP_BINARY_OP_TY_NEQ;
break;
case TD_BINARY_OP_TY_GT:
if (is_fp) {
b->ty = IR_OP_BINARY_OP_TY_FGT;
} else if (WKT_IS_SIGNED(td_var_ty->well_known)) {
b->ty = IR_OP_BINARY_OP_TY_SGT;
} else {
b->ty = IR_OP_BINARY_OP_TY_UGT;
}
break;
case TD_BINARY_OP_TY_GTEQ:
if (is_fp) {
b->ty = IR_OP_BINARY_OP_TY_FGTEQ;
} else if (WKT_IS_SIGNED(td_var_ty->well_known)) {
b->ty = IR_OP_BINARY_OP_TY_SGTEQ;
} else {
b->ty = IR_OP_BINARY_OP_TY_UGTEQ;
}
break;
case TD_BINARY_OP_TY_LT:
if (is_fp) {
b->ty = IR_OP_BINARY_OP_TY_FLT;
} else if (WKT_IS_SIGNED(td_var_ty->well_known)) {
b->ty = IR_OP_BINARY_OP_TY_SLT;
} else {
b->ty = IR_OP_BINARY_OP_TY_ULT;
}
break;
case TD_BINARY_OP_TY_LTEQ:
if (is_fp) {
b->ty = IR_OP_BINARY_OP_TY_FLTEQ;
} else if (WKT_IS_SIGNED(td_var_ty->well_known)) {
b->ty = IR_OP_BINARY_OP_TY_SLTEQ;
} else {
b->ty = IR_OP_BINARY_OP_TY_ULTEQ;
}
break;
case TD_BINARY_OP_TY_RSHIFT:
if (WKT_IS_SIGNED(td_var_ty->well_known)) {
b->ty = IR_OP_BINARY_OP_TY_SRSHIFT;
} else {
b->ty = IR_OP_BINARY_OP_TY_URSHIFT;
}
break;
case TD_BINARY_OP_TY_LSHIFT:
b->ty = IR_OP_BINARY_OP_TY_LSHIFT;
break;
case TD_BINARY_OP_TY_AND:
b->ty = IR_OP_BINARY_OP_TY_AND;
break;
case TD_BINARY_OP_TY_OR:
b->ty = IR_OP_BINARY_OP_TY_OR;
break;
case TD_BINARY_OP_TY_XOR:
b->ty = IR_OP_BINARY_OP_TY_XOR;
break;
case TD_BINARY_OP_TY_ADD:
b->ty = is_fp ? IR_OP_BINARY_OP_TY_FADD : IR_OP_BINARY_OP_TY_ADD;
break;
case TD_BINARY_OP_TY_SUB:
b->ty = is_fp ? IR_OP_BINARY_OP_TY_FSUB : IR_OP_BINARY_OP_TY_SUB;
break;
case TD_BINARY_OP_TY_MUL:
b->ty = is_fp ? IR_OP_BINARY_OP_TY_FMUL : IR_OP_BINARY_OP_TY_MUL;
break;
case TD_BINARY_OP_TY_DIV:
if (is_fp) {
b->ty = IR_OP_BINARY_OP_TY_FDIV;
} else if (WKT_IS_SIGNED(td_var_ty->well_known)) {
b->ty = IR_OP_BINARY_OP_TY_SDIV;
} else {
b->ty = IR_OP_BINARY_OP_TY_UDIV;
}
break;
case TD_BINARY_OP_TY_QUOT:
if (WKT_IS_SIGNED(td_var_ty->well_known)) {
b->ty = IR_OP_BINARY_OP_TY_SQUOT;
} else {
b->ty = IR_OP_BINARY_OP_TY_UQUOT;
}
break;
}
return op;
}
static struct ir_op *build_ir_for_array_address(struct ir_func_builder *irb,
struct ir_stmt **stmt,
struct td_expr *lhs_expr,
struct td_expr *rhs_expr);
static struct ir_op *build_ir_for_member_address(struct ir_func_builder *irb,
struct ir_stmt **stmt,
struct td_expr *lhs_expr,
const char *member_name);
static struct ir_op *build_ir_for_pointer_address(struct ir_func_builder *irb,
struct ir_stmt **stmt,
struct td_expr *lhs_expr,
const char *member_name);
static struct ir_op *build_ir_for_addressof_var(struct ir_func_builder *irb,
struct ir_stmt **stmt,
struct td_var *var) {
struct var_key key;
struct var_ref *ref;
get_var_ref(irb, NULL, var, &key, &ref);
struct ir_var_ty var_ty = IR_VAR_TY_POINTER;
struct ir_op *op = alloc_ir_op(irb->func, *stmt);
op->ty = IR_OP_TY_ADDR;
switch (ref->ty) {
case VAR_REF_TY_SSA:
ref->ty = VAR_REF_TY_LCL;
if (ref->op) {
spill_op(irb->func, ref->op);
ref->lcl = ref->op->lcl;
} else {
ref->lcl = add_local(irb->func, &var_ty);
op->lcl = ref->lcl;
}
op->var_ty = var_ty;
op->addr = (struct ir_op_addr){.ty = IR_OP_ADDR_TY_LCL, .lcl = ref->lcl};
break;
case VAR_REF_TY_LCL:
if (!ref->lcl) {
ref->lcl = add_local(irb->func, &var_ty);
}
op->var_ty = var_ty;
op->addr = (struct ir_op_addr){.ty = IR_OP_ADDR_TY_LCL, .lcl = ref->lcl};
break;
case VAR_REF_TY_GLB:
op->var_ty = var_ty;
op->addr = (struct ir_op_addr){.ty = IR_OP_ADDR_TY_GLB, .glb = ref->glb};
break;
}
return op;
}
static struct ir_op *build_ir_for_addressof(struct ir_func_builder *irb,
struct ir_stmt **stmt,
struct td_expr *expr) {
// address of does not actually "read" its underlying expression
// so we do not build the expression
switch (expr->ty) {
case TD_EXPR_TY_ARRAYACCESS: {
return build_ir_for_array_address(irb, stmt, expr->array_access.lhs,
expr->array_access.rhs);
}
case TD_EXPR_TY_MEMBERACCESS: {
return build_ir_for_member_address(irb, stmt, expr->member_access.lhs,
expr->member_access.member);
}
case TD_EXPR_TY_POINTERACCESS: {
return build_ir_for_pointer_address(irb, stmt, expr->pointer_access.lhs,
expr->pointer_access.member);
}
case TD_EXPR_TY_COMPOUNDEXPR: {
return build_ir_for_addressof(
irb, stmt,
&expr->compound_expr.exprs[expr->compound_expr.num_exprs - 1]);
}
default:
break;
}
if (expr->ty == TD_EXPR_TY_UNARY_OP &&
expr->unary_op.ty == TD_UNARY_OP_TY_INDIRECTION) {
// &*, so cancel
return build_ir_for_expr(irb, stmt, expr->unary_op.expr);
}
if (expr->ty != TD_EXPR_TY_VAR) {
todo("unknown type for addressof");
}
return build_ir_for_addressof_var(irb, stmt, &expr->var);
}
static struct ir_op *build_ir_for_assg(struct ir_func_builder *irb,
struct ir_stmt **stmt,
struct td_expr *expr);
static struct ir_op *build_ir_for_unaryop(struct ir_func_builder *irb,
struct ir_stmt **stmt,
struct td_expr *expr) {
struct td_unary_op *unary_op = &expr->unary_op;
struct ir_var_ty var_ty = var_ty_for_td_var_ty(irb->unit, &expr->var_ty);
if (unary_op->ty == TD_UNARY_OP_TY_ADDRESSOF) {
return build_ir_for_addressof(irb, stmt, unary_op->expr);
}
struct ir_op *ir_expr = build_ir_for_expr(irb, stmt, unary_op->expr);
if (unary_op->ty == TD_UNARY_OP_TY_INDIRECTION) {
// does not generate a unary op instead generates a LOAD_ADDR
struct ir_op *op = alloc_ir_op(irb->func, *stmt);
op->ty = IR_OP_TY_LOAD_ADDR;
op->var_ty = var_ty;
op->load_addr = (struct ir_op_load_addr){.addr = ir_expr};
return op;
}
bool is_postfix;
enum td_assg_ty assg_ty;
switch (unary_op->ty) {
case TD_UNARY_OP_TY_PREFIX_DEC:
case TD_UNARY_OP_TY_PREFIX_INC:
is_postfix = false;
assg_ty = unary_op->ty == TD_UNARY_OP_TY_PREFIX_INC ? TD_ASSG_TY_ADD
: TD_ASSG_TY_SUB;
goto inc_dec;
case TD_UNARY_OP_TY_POSTFIX_INC:
case TD_UNARY_OP_TY_POSTFIX_DEC:
is_postfix = true;
assg_ty = unary_op->ty == TD_UNARY_OP_TY_POSTFIX_INC ? TD_ASSG_TY_ADD
: TD_ASSG_TY_SUB;
goto inc_dec;
inc_dec : {
// if we are decrementing a pointer/array, we need to make sure we don't
// build an expr that is PTR - PTR as this will do a "pointer subtract"
// rather than "pointer minus integer" so we give the constant a
// pointer-sized-integer-type, rather than pointer type
struct td_var_ty cnst_ty;
if (unary_op->expr->var_ty.ty == TD_VAR_TY_TY_POINTER ||
unary_op->expr->var_ty.ty == TD_VAR_TY_TY_ARRAY) {
cnst_ty = td_var_ty_pointer_sized_int(irb->tchk, false);
} else {
cnst_ty = unary_op->expr->var_ty;
}
struct td_expr one = {
.ty = TD_EXPR_TY_CNST,
.var_ty = cnst_ty,
.cnst = (struct td_cnst){.ty = TD_CNST_TY_SIGNED_INT, .int_value = 1}};
struct td_assg td_assg = {
.ty = assg_ty,
.expr = &one,
.assignee = unary_op->expr,
};
struct td_expr td_expr = {
.ty = TD_EXPR_TY_ASSG, .var_ty = expr->var_ty, .assg = td_assg};
struct ir_op *assg = build_ir_for_assg(irb, stmt, &td_expr);
if (is_postfix) {
return ir_expr;
} else {
return assg;
}
}
case TD_UNARY_OP_TY_PLUS:
// no work needed, build_expr will handle type conversion
return ir_expr;
case TD_UNARY_OP_TY_SIZEOF:
case TD_UNARY_OP_TY_ALIGNOF:
todo("sizeof/alignof build (will need different node as they take types "
"not exprs)");
case TD_UNARY_OP_TY_CAST:
if (var_ty_needs_cast_op(irb, &var_ty, &ir_expr->var_ty)) {
return insert_ir_for_cast(
irb, *stmt, ir_expr, &var_ty,
cast_ty_for_td_var_ty(irb, &unary_op->expr->var_ty, &expr->var_ty));
} else {
ir_expr->var_ty = var_ty_for_td_var_ty(irb->unit, &expr->var_ty);
return ir_expr;
}
default:
break;
}
enum ir_op_unary_op_ty unary_op_ty;
switch (unary_op->ty) {
case TD_UNARY_OP_TY_MINUS:
unary_op_ty = IR_OP_UNARY_OP_TY_NEG;
break;
case TD_UNARY_OP_TY_LOGICAL_NOT:
unary_op_ty = IR_OP_UNARY_OP_TY_LOGICAL_NOT;
break;
case TD_UNARY_OP_TY_NOT:
unary_op_ty = IR_OP_UNARY_OP_TY_NOT;
break;
default:
bug("unexpected unary_op_ty in `%s`", __func__);
}
struct ir_op *op = alloc_ir_op(irb->func, *stmt);
op->ty = IR_OP_TY_UNARY_OP;
op->var_ty = var_ty;
op->unary_op.ty = unary_op_ty;
op->unary_op.value = ir_expr;
return op;
}
static struct ir_op *build_ir_for_binaryop(struct ir_func_builder *irb,
struct ir_stmt **stmt,
struct td_expr *expr) {
struct td_binary_op *binary_op = &expr->binary_op;
struct ir_var_ty var_ty = var_ty_for_td_var_ty(irb->unit, &expr->var_ty);
struct ir_op *lhs = build_ir_for_expr(irb, stmt, binary_op->lhs);
if (binary_op->ty == TD_BINARY_OP_TY_LOGICAL_AND ||
binary_op->ty == TD_BINARY_OP_TY_LOGICAL_OR) {
struct ir_basicblock *entry_bb = (*stmt)->basicblock;
struct ir_basicblock *rhs_bb = alloc_ir_basicblock(irb->func);
struct ir_basicblock *end_bb = alloc_ir_basicblock(irb->func);
if (binary_op->ty == TD_BINARY_OP_TY_LOGICAL_AND) {
make_basicblock_split(irb->func, entry_bb, rhs_bb, end_bb);
} else {
make_basicblock_split(irb->func, entry_bb, end_bb, rhs_bb);
}
struct ir_stmt *entry_stmt = alloc_ir_stmt(irb->func, entry_bb);
struct ir_op *lhs_br = alloc_ir_op(irb->func, entry_stmt);
lhs_br->ty = IR_OP_TY_BR_COND;
lhs_br->var_ty = IR_VAR_TY_NONE;
lhs_br->br_cond = (struct ir_op_br_cond){.cond = lhs};
struct ir_stmt *rhs_stmt = alloc_ir_stmt(irb->func, rhs_bb);
struct ir_op *rhs = build_ir_for_expr(irb, &rhs_stmt, binary_op->rhs);
struct ir_op *rhs_br = alloc_ir_op(irb->func, rhs_stmt);
rhs_br->ty = IR_OP_TY_BR;
rhs_br->var_ty = IR_VAR_TY_NONE;
make_basicblock_merge(irb->func, rhs_bb, end_bb);
struct ir_stmt *end_stmt = alloc_ir_stmt(irb->func, end_bb);
struct ir_op *phi = alloc_ir_op(irb->func, end_stmt);
phi->ty = IR_OP_TY_PHI;
phi->var_ty = var_ty;
phi->phi = (struct ir_op_phi){
.num_values = 2,
.values = arena_alloc(irb->func->arena, sizeof(struct ir_op *) * 2),
.var = NULL};
phi->phi.values[0] = lhs;
phi->phi.values[1] = rhs;
*stmt = phi->stmt;
return phi;
}
struct ir_op *rhs = build_ir_for_expr(irb, stmt, binary_op->rhs);
struct ir_build_binaryop args = {
.ty = binary_op->ty,
.result_ty = expr->var_ty,
.lhs_ty = binary_op->lhs->var_ty,
.rhs_ty = binary_op->rhs->var_ty,
.lhs = lhs,
.rhs = rhs,
};
return alloc_binaryop(irb, *stmt, &args);
}
static struct ir_op *build_ir_for_sizeof(struct ir_func_builder *irb,
struct ir_stmt **stmt,
struct td_expr *expr) {
struct td_sizeof *size_of = &expr->size_of;
struct ir_var_ty var_ty = var_ty_for_td_var_ty(irb->unit, &expr->var_ty);
struct ir_var_ty size_var_ty;
switch (size_of->ty) {
case TD_SIZEOF_TY_TYPE:
size_var_ty = var_ty_for_td_var_ty(irb->unit, &size_of->var_ty);
break;
case TD_SIZEOF_TY_EXPR:
size_var_ty = var_ty_for_td_var_ty(irb->unit, &size_of->expr->var_ty);
break;
}
struct ir_var_ty_info info = var_ty_info(irb->unit, &size_var_ty);
struct ir_op *op = alloc_ir_op(irb->func, *stmt);
op->ty = IR_OP_TY_CNST;
op->var_ty = var_ty;
op->cnst =
(struct ir_op_cnst){.ty = IR_OP_CNST_TY_INT, .int_value = info.size};
return op;
}
static struct ir_op *build_ir_for_alignof(struct ir_func_builder *irb,
struct ir_stmt **stmt,
struct td_expr *expr) {
struct td_alignof *align_of = &expr->align_of;
struct ir_var_ty var_ty = var_ty_for_td_var_ty(irb->unit, &expr->var_ty);
struct ir_var_ty align_var_ty =
var_ty_for_td_var_ty(irb->unit, &align_of->var_ty);
struct ir_var_ty_info info = var_ty_info(irb->unit, &align_var_ty);
struct ir_op *op = alloc_ir_op(irb->func, *stmt);
op->ty = IR_OP_TY_CNST;
op->var_ty = var_ty;
op->cnst =
(struct ir_op_cnst){.ty = IR_OP_CNST_TY_INT, .int_value = info.alignment};
return op;
}
static struct ir_op *build_ir_for_cnst(struct ir_func_builder *irb,
struct ir_stmt **stmt,
struct ir_var_ty var_ty,
struct td_cnst *cnst) {
struct ir_op *op = alloc_ir_op(irb->func, *stmt);
switch (cnst->ty) {
case TD_CNST_TY_CHAR:
case TD_CNST_TY_WIDE_CHAR:
case TD_CNST_TY_SIGNED_INT:
case TD_CNST_TY_UNSIGNED_INT:
case TD_CNST_TY_SIGNED_LONG:
case TD_CNST_TY_UNSIGNED_LONG:
case TD_CNST_TY_SIGNED_LONG_LONG:
case TD_CNST_TY_UNSIGNED_LONG_LONG:
op->ty = IR_OP_TY_CNST;
op->var_ty = var_ty;
op->cnst.ty = IR_OP_CNST_TY_INT;
op->cnst.int_value = cnst->int_value;