-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathir.h
1420 lines (1078 loc) · 35.2 KB
/
ir.h
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
#ifndef IR_IR_H
#define IR_IR_H
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#ifndef FLAG_ENUM
#define FLAG_ENUM
#endif
typedef ptrdiff_t ssize_t;
enum ir_op_ty {
IR_OP_TY_UNKNOWN,
IR_OP_TY_PHI,
IR_OP_TY_UNDF,
// only used late in the pipeline for eliminating phi nodes
IR_OP_TY_MOV,
IR_OP_TY_CNST,
IR_OP_TY_BINARY_OP,
IR_OP_TY_UNARY_OP,
IR_OP_TY_CAST_OP,
IR_OP_TY_STORE,
IR_OP_TY_LOAD,
IR_OP_TY_STORE_BITFIELD,
IR_OP_TY_LOAD_BITFIELD,
IR_OP_TY_BITFIELD_EXTRACT,
IR_OP_TY_BITFIELD_INSERT,
IR_OP_TY_MEM_SET,
IR_OP_TY_MEM_COPY,
IR_OP_TY_ADDR,
IR_OP_TY_ADDR_OFFSET,
IR_OP_TY_BR,
IR_OP_TY_BR_COND,
IR_OP_TY_BR_SWITCH,
IR_OP_TY_RET,
IR_OP_TY_CALL,
// gathers a set of SSA values into an aggregate
IR_OP_TY_GATHER,
IR_OP_TY_VA_START,
IR_OP_TY_VA_ARG,
// Low-level operations that only occur after lowering
// IR_OP_TY_STORE_REG,
// IR_OP_TY_LOAD_REG,
// IR_OP_TY_MOV_REG,
};
struct ir_gather_value {
struct ir_op *value;
size_t field_idx;
};
struct ir_op_gather {
struct ir_gather_value *values;
size_t num_values;
};
struct ir_op_mov {
struct ir_op *value;
};
struct ir_phi_entry {
struct ir_basicblock *basicblock;
struct ir_op *value;
};
struct ir_op_phi {
struct ir_phi_entry *values;
size_t num_values;
};
enum ir_op_cnst_ty {
IR_OP_CNST_TY_FLT,
IR_OP_CNST_TY_INT,
};
struct ir_op_cnst {
enum ir_op_cnst_ty ty;
union {
unsigned long long int_value;
long double flt_value;
};
};
struct ir_op_ret {
struct ir_op *value;
};
enum ir_op_cast_op_ty {
IR_OP_CAST_OP_TY_SEXT,
IR_OP_CAST_OP_TY_ZEXT,
IR_OP_CAST_OP_TY_TRUNC,
// convert between float types
IR_OP_CAST_OP_TY_CONV,
// convert float <-> unsigned
IR_OP_CAST_OP_TY_UCONV,
// convert float <-> signed
IR_OP_CAST_OP_TY_SCONV,
};
struct ir_op_cast_op {
enum ir_op_cast_op_ty ty;
struct ir_op *value;
};
enum ir_op_unary_op_ty {
IR_OP_UNARY_OP_TY_FNEG,
IR_OP_UNARY_OP_TY_FSQRT,
IR_OP_UNARY_OP_TY_FABS,
IR_OP_UNARY_OP_TY_NEG,
IR_OP_UNARY_OP_TY_LOGICAL_NOT,
IR_OP_UNARY_OP_TY_NOT,
};
struct ir_op_unary_op {
enum ir_op_unary_op_ty ty;
struct ir_op *value;
};
enum ir_op_binary_op_ty {
IR_OP_BINARY_OP_TY_EQ,
IR_OP_BINARY_OP_TY_NEQ,
IR_OP_BINARY_OP_TY_UGT,
IR_OP_BINARY_OP_TY_SGT,
IR_OP_BINARY_OP_TY_UGTEQ,
IR_OP_BINARY_OP_TY_SGTEQ,
IR_OP_BINARY_OP_TY_ULT,
IR_OP_BINARY_OP_TY_SLT,
IR_OP_BINARY_OP_TY_ULTEQ,
IR_OP_BINARY_OP_TY_SLTEQ,
IR_OP_BINARY_OP_TY_FMAX,
IR_OP_BINARY_OP_TY_FMIN,
IR_OP_BINARY_OP_TY_FEQ,
IR_OP_BINARY_OP_TY_FNEQ,
IR_OP_BINARY_OP_TY_FGT,
IR_OP_BINARY_OP_TY_FGTEQ,
IR_OP_BINARY_OP_TY_FLT,
IR_OP_BINARY_OP_TY_FLTEQ,
IR_OP_BINARY_OP_TY_LSHIFT,
IR_OP_BINARY_OP_TY_SRSHIFT,
IR_OP_BINARY_OP_TY_URSHIFT,
IR_OP_BINARY_OP_TY_AND,
IR_OP_BINARY_OP_TY_OR,
IR_OP_BINARY_OP_TY_XOR,
IR_OP_BINARY_OP_TY_ADD,
IR_OP_BINARY_OP_TY_SUB,
IR_OP_BINARY_OP_TY_MUL,
IR_OP_BINARY_OP_TY_SDIV,
IR_OP_BINARY_OP_TY_UDIV,
IR_OP_BINARY_OP_TY_SMOD,
IR_OP_BINARY_OP_TY_UMOD,
IR_OP_BINARY_OP_TY_FADD,
IR_OP_BINARY_OP_TY_FSUB,
IR_OP_BINARY_OP_TY_FMUL,
IR_OP_BINARY_OP_TY_FDIV,
};
bool ir_binary_op_is_comparison(enum ir_op_binary_op_ty ty);
enum ir_op_binary_op_ty ir_invert_binary_comparison(enum ir_op_binary_op_ty ty);
enum ir_op_binary_op_ty ir_flip_binary_comparison(enum ir_op_binary_op_ty ty);
enum ir_op_sign { IR_OP_SIGN_NA, IR_OP_SIGN_SIGNED, IR_OP_SIGN_UNSIGNED };
enum ir_op_sign ir_binary_op_sign(enum ir_op_binary_op_ty /*ty*/);
struct ir_op_binary_op {
enum ir_op_binary_op_ty ty;
struct ir_op *lhs;
struct ir_op *rhs;
};
// IR does not have sign encoded in type (so `int` and `unsigned` are both
// IR_OP_VAR_TY_32) and instead encodes it in operations (e.g there are
// different IR ops for signed and unsigned division)
enum ir_var_primitive_ty {
IR_VAR_PRIMITIVE_TY_I1,
IR_VAR_PRIMITIVE_TY_I8,
IR_VAR_PRIMITIVE_TY_I16,
IR_VAR_PRIMITIVE_TY_I32,
IR_VAR_PRIMITIVE_TY_I64,
IR_VAR_PRIMITIVE_TY_I128,
IR_VAR_PRIMITIVE_TY_F16,
IR_VAR_PRIMITIVE_TY_F32,
IR_VAR_PRIMITIVE_TY_F64,
};
enum ir_var_ty_ty {
/* Does not produce a value */
IR_VAR_TY_TY_NONE,
/* Primitives - integers, floats, pointers */
IR_VAR_TY_TY_PRIMITIVE,
IR_VAR_TY_TY_FUNC,
IR_VAR_TY_TY_POINTER,
/* Aggregate */
IR_VAR_TY_TY_ARRAY,
IR_VAR_TY_TY_STRUCT,
IR_VAR_TY_TY_UNION,
/* Variadic */
IR_VAR_TY_TY_VARIADIC,
};
enum FLAG_ENUM ir_var_func_ty_flags {
IR_VAR_FUNC_TY_FLAG_NONE = 0,
IR_VAR_FUNC_TY_FLAG_VARIADIC = 1 << 0
};
struct ir_var_func_ty {
struct ir_var_ty *ret_ty;
size_t num_params;
struct ir_var_ty *params;
enum ir_var_func_ty_flags flags;
};
bool ir_is_func_variadic(const struct ir_var_func_ty *ty);
struct ir_var_aggregate_ty {
size_t num_fields;
struct ir_var_ty *fields;
};
struct ir_var_array_ty {
struct ir_var_ty *underlying;
union {
size_t num_elements;
};
};
struct ir_var_ty {
enum ir_var_ty_ty ty;
union {
enum ir_var_primitive_ty primitive;
struct ir_var_func_ty func;
struct ir_var_array_ty array;
struct ir_var_aggregate_ty aggregate;
};
};
extern const struct ir_var_ty IR_VAR_TY_UNKNOWN;
extern const struct ir_var_ty IR_VAR_TY_POINTER;
extern const struct ir_var_ty IR_VAR_TY_NONE;
extern const struct ir_var_ty IR_VAR_TY_I1;
extern const struct ir_var_ty IR_VAR_TY_I8;
extern const struct ir_var_ty IR_VAR_TY_I16;
extern const struct ir_var_ty IR_VAR_TY_I32;
extern const struct ir_var_ty IR_VAR_TY_I64;
extern const struct ir_var_ty IR_VAR_TY_I128;
extern const struct ir_var_ty IR_VAR_TY_F16;
extern const struct ir_var_ty IR_VAR_TY_F32;
extern const struct ir_var_ty IR_VAR_TY_F64;
extern const struct ir_var_ty IR_VAR_TY_VARIADIC;
enum ir_reg_ty {
IR_REG_TY_NONE,
IR_REG_TY_SPILLED,
IR_REG_TY_FLAGS,
IR_REG_TY_INTEGRAL,
IR_REG_TY_FP,
};
struct ir_reg {
enum ir_reg_ty ty;
union {
unsigned long idx;
};
};
enum ir_param_info_ty {
IR_PARAM_INFO_TY_REGISTER,
IR_PARAM_INFO_TY_STACK,
IR_PARAM_INFO_TY_POINTER,
};
struct ir_param_reg {
// e.g `struct { float[3] }` would have size 4, but `struct { char[16] } would
// have size 8`
struct ir_reg reg;
size_t size;
};
struct ir_param_info {
enum ir_param_info_ty ty;
const struct ir_var_ty *var_ty;
// offset within the param
size_t offset;
// union {
// RV32I STRUCK HACK:
bool split;
struct {
struct {
size_t num_regs;
struct ir_param_reg regs[8]; // also used for pointer
};
size_t stack_offset;
};
};
enum FLAG_ENUM ir_call_info_flags {
IR_CALL_INFO_FLAG_NONE = 0,
// place the number of variadic arguments into the specified register
IR_CALL_INFO_FLAG_NUM_VARIADIC = 1,
};
struct ir_call_info {
size_t stack_size;
size_t num_params;
struct ir_param_info *params;
struct ir_param_info *ret;
size_t num_gp_used;
size_t num_fp_used;
size_t num_variadics;
enum ir_call_info_flags flags;
union {
struct ir_reg num_variadics_reg;
};
};
struct ir_func_info {
struct ir_var_func_ty func_ty;
struct ir_call_info call_info;
};
struct ir_op_call {
struct ir_var_ty func_ty;
struct ir_func_info func_info;
struct ir_op *target;
size_t num_args;
struct ir_op **args;
// must be preserved because of unspecified functions e.g `int foo();
// foo(7, 8.3)`
struct ir_var_ty *arg_var_tys;
};
struct ir_op_addr_offset {
// `addr = base + (index * scale) + offset`
struct ir_op *base;
struct ir_op *index;
size_t scale;
size_t offset;
};
struct ir_bitfield {
size_t offset;
size_t width;
};
struct ir_op_bitfield_extract {
struct ir_op *value;
struct ir_bitfield bitfield;
};
struct ir_op_bitfield_insert {
struct ir_op *target;
struct ir_op *value;
struct ir_bitfield bitfield;
};
enum ir_op_store_ty {
IR_OP_STORE_TY_LCL,
IR_OP_STORE_TY_GLB,
IR_OP_STORE_TY_ADDR,
};
struct ir_op_store {
enum ir_op_store_ty ty;
struct ir_op *value;
union {
struct ir_lcl *lcl;
struct ir_glb *glb;
struct ir_op *addr;
};
};
struct ir_op_store_bitfield {
enum ir_op_store_ty ty;
struct ir_op *value;
struct ir_bitfield bitfield;
union {
struct ir_lcl *lcl;
struct ir_glb *glb;
struct ir_op *addr;
};
};
enum ir_op_load_ty {
IR_OP_LOAD_TY_LCL,
IR_OP_LOAD_TY_GLB,
IR_OP_LOAD_TY_ADDR,
};
struct ir_op_load {
enum ir_op_load_ty ty;
union {
struct ir_lcl *lcl;
struct ir_glb *glb;
struct ir_op *addr;
};
};
struct ir_op_load_bitfield {
enum ir_op_load_ty ty;
struct ir_bitfield bitfield;
union {
struct ir_lcl *lcl;
struct ir_glb *glb;
struct ir_op *addr;
};
};
enum ir_op_addr_ty {
IR_OP_ADDR_TY_LCL,
IR_OP_ADDR_TY_GLB,
};
struct ir_op_addr {
enum ir_op_addr_ty ty;
union {
struct ir_lcl *lcl;
struct ir_glb *glb;
};
};
struct ir_op_br_switch {
struct ir_op *value;
// targets on `ir_basicblock`
};
struct ir_op_br_cond {
struct ir_op *cond;
// targets on `ir_basicblock`
};
struct ir_op_mem_set {
struct ir_op *addr;
unsigned char value;
size_t length;
};
// enum FLAG_ENUM ir_mem_copy_flags {
// IR_MEM_COPY_FLAG_NONE = 0,
// // the instruction is free to use any volatile registers
// IR_MEM_COPY_FLAG_VOLATILE = 1
// };
struct ir_op_mem_copy {
struct ir_op *source;
struct ir_op *dest;
size_t length;
// enum ir_mem_copy_flags flags;
};
bool ir_reg_eq(struct ir_reg left, struct ir_reg right);
bool ir_var_ty_has_reg(const struct ir_var_ty var_ty);
enum ir_reg_ty ir_reg_ty_for_var_ty(const struct ir_var_ty var_ty);
bool ir_primitive_ty_is_integral(enum ir_var_primitive_ty ty);
bool ir_primitive_ty_is_fp(enum ir_var_primitive_ty ty);
#define NO_REG \
(struct ir_reg) { .ty = IR_REG_TY_NONE }
#define REG_SPILLED \
(struct ir_reg) { .ty = IR_REG_TY_SPILLED }
#define REG_FLAGS \
(struct ir_reg) { .ty = IR_REG_TY_FLAGS }
enum FLAG_ENUM ir_op_flags {
IR_OP_FLAG_NONE = 0,
// Force this variable to spill
IR_OP_FLAG_MUST_SPILL = 1,
// Indicates this is a magic mov representing a parameter
IR_OP_FLAG_PARAM = 2,
// indicates this value is passed as a variadic
IR_OP_FLAG_VARIADIC_PARAM = 4,
// indicates the op is a load_lcl/store_lcl used for a spill
IR_OP_FLAG_SPILL = 8,
// indicates this op does not generate any instructions and it is encoded in
// its uses
IR_OP_FLAG_CONTAINED = 16,
// reg is assigned and cannot change
IR_OP_FLAG_FIXED_REG = 32,
// op has side effects (e.g is an assignment)
IR_OP_FLAG_SIDE_EFFECTS = 128,
// op has been spilled and all consumers must reload it
IR_OP_FLAG_SPILLED = 256,
// this op is a mov used to eliminate phis
IR_OP_FLAG_PHI_MOV = 512,
// this op reads from dest reg, so it cannot be overwritten
IR_OP_FLAG_READS_DEST = 1024,
// is a promoted value
IR_OP_FLAG_PROMOTED = 2048,
// HACK: makes op live forever
IR_OP_FLAG_ETERNAL = 4096,
};
struct ir_op_write_info {
size_t num_reg_writes;
struct ir_reg writes[4];
};
struct ir_op_va_start {
struct ir_op *list_addr;
};
struct ir_op_va_arg {
struct ir_var_ty arg_ty;
struct ir_op *list_addr;
};
struct ir_op {
size_t id;
enum ir_op_ty ty;
enum ir_op_flags flags;
struct ir_var_ty var_ty;
struct ir_op *pred;
struct ir_op *succ;
struct ir_stmt *stmt;
union {
struct ir_op_cnst cnst;
struct ir_op_call call;
struct ir_op_binary_op binary_op;
struct ir_op_unary_op unary_op;
struct ir_op_cast_op cast_op;
struct ir_op_ret ret;
struct ir_op_mem_set mem_set;
struct ir_op_mem_copy mem_copy;
struct ir_op_store store;
struct ir_op_load load;
struct ir_op_store_bitfield store_bitfield;
struct ir_op_load_bitfield load_bitfield;
struct ir_op_bitfield_extract bitfield_extract;
struct ir_op_bitfield_insert bitfield_insert;
struct ir_op_addr addr;
struct ir_op_addr_offset addr_offset;
struct ir_op_gather gather;
struct ir_op_br_cond br_cond;
struct ir_op_br_switch br_switch;
struct ir_op_va_start va_start;
struct ir_op_va_arg va_arg;
/* br has no entry, as its target is on `ir_basicblock` and it has no
* condition */
struct ir_op_phi phi;
struct ir_op_mov mov;
};
struct ir_lcl *lcl;
// only meaningful post register-allocation
struct ir_reg reg;
void *metadata;
// contains any registers other than `reg` which this instruction writes to
struct ir_op_write_info write_info;
const char *comment;
};
enum FLAG_ENUM ir_stmt_flags {
IR_STMT_FLAG_NONE = 0,
// contains phi and only phi nodes
IR_STMT_FLAG_PHI = 1 << 0,
// contains param and only param nodes
IR_STMT_FLAG_PARAM = 1 << 1,
// phi moves
IR_STMT_FLAG_PHI_MOV = 1 << 2,
};
// set of ops with no SEQ_POINTs
struct ir_stmt {
size_t id;
// a NULL bb means a pruned stmt
struct ir_basicblock *basicblock;
struct ir_stmt *pred;
struct ir_stmt *succ;
enum ir_stmt_flags flags;
// the links between ops (`pred` & `succ`) have no significance to the
// compilation and are just for traversal. meaningful links between operations
// are with in the op data, such as `ir_op->ret.value`, which points to the op
// whos result is returned
struct ir_op *first;
// last is the dominating op of the statement, and can be used as its "root".
// all other ops in the statement are reachable from it
struct ir_op *last;
const char *comment;
};
// ensures the basic block ends with an appropriate branch and does not contain
// any within it
bool ir_valid_basicblock(struct ir_basicblock *basicblock);
enum ir_basicblock_ty {
// a return has no explicit successors
IR_BASICBLOCK_TY_RET,
// a split basicblock has 2 successors and occurs when there is a conditional
// branch
IR_BASICBLOCK_TY_SPLIT,
// a merge basicblock has 1 successor (but its successor will have at least 2
// predecessors)
// and occurs when multiple basicblocks rejoin
IR_BASICBLOCK_TY_MERGE,
// a list of possible values to jump to, and then false branch
IR_BASICBLOCK_TY_SWITCH,
};
struct ir_basicblock_merge {
struct ir_basicblock *target;
};
struct ir_basicblock_split {
struct ir_basicblock *true_target;
struct ir_basicblock *false_target;
};
struct ir_split_case {
unsigned long long value;
struct ir_basicblock *target;
};
struct ir_basicblock_switch {
size_t num_cases;
struct ir_split_case *cases;
struct ir_basicblock *default_target;
};
struct ir_basicblock {
size_t id;
// a NULL irb means a pruned basicblock
struct ir_func *func;
// these are creation order traversal methods, and do not signify edges
// between BBs
struct ir_basicblock *pred;
struct ir_basicblock *succ;
struct ir_stmt *first;
struct ir_stmt *last;
struct ir_basicblock **preds;
size_t num_preds;
enum ir_basicblock_ty ty;
union {
struct ir_basicblock_merge merge;
struct ir_basicblock_split split;
struct ir_basicblock_switch switch_case;
};
struct cg_basicblock *cg_basicblock;
void *metadata;
const char *comment;
};
enum FLAG_ENUM ir_func_flags {
IR_FUNC_FLAG_NONE = 0,
IR_FUNC_FLAG_MAKES_CALL = 1 << 0,
IR_FUNC_FLAG_NEEDS_SSP = 1 << 1,
IR_FUNC_FLAG_USES_VA_ARGS = 1 << 2
};
enum ir_var_data_ty {
IR_VAR_TY_STRING_LITERAL,
IR_VAR_TY_CONST_DATA,
IR_VAR_TY_DATA
};
struct ir_var_addr {
struct ir_glb *glb;
unsigned long long offset;
};
struct ir_var_value_list {
struct ir_var_value *values;
size_t *offsets;
size_t num_values;
};
enum ir_var_value_ty {
IR_VAR_VALUE_TY_ZERO,
IR_VAR_VALUE_TY_INT,
IR_VAR_VALUE_TY_FLT,
IR_VAR_VALUE_TY_STR,
IR_VAR_VALUE_TY_ADDR,
IR_VAR_VALUE_TY_VALUE_LIST,
};
struct ir_var_str {
const void *value;
size_t len;
};
struct ir_var_value {
enum ir_var_value_ty ty;
struct ir_var_ty var_ty;
union {
struct ir_var_str str_value;
unsigned long long int_value;
long double flt_value;
struct ir_var_addr addr;
struct ir_var_value_list value_list;
};
};
struct ir_var {
struct ir_unit *unit;
enum ir_var_data_ty ty;
struct ir_var_ty var_ty;
struct ir_var_value value;
};
enum ir_glb_ty {
IR_GLB_TY_DATA,
IR_GLB_TY_FUNC,
};
enum ir_glb_def_ty {
IR_GLB_DEF_TY_DEFINED,
IR_GLB_DEF_TY_UNDEFINED,
IR_GLB_DEF_TY_TENTATIVE
};
enum FLAG_ENUM ir_glb_flags {
IR_GLB_FLAG_NONE = 0,
IR_GLB_FLAG_WEAK = 1 << 0,
};
enum ir_linkage { IR_LINKAGE_NONE, IR_LINKAGE_INTERNAL, IR_LINKAGE_EXTERNAL };
struct ir_glb {
size_t id;
enum ir_glb_ty ty;
enum ir_glb_def_ty def_ty;
enum ir_linkage linkage;
enum ir_glb_flags flags;
struct ir_glb *pred;
struct ir_glb *succ;
const char *name;
struct ir_var_ty var_ty;
union {
struct ir_var *var;
struct ir_func *func;
};
};
enum FLAG_ENUM ir_lcl_flags {
IR_LCL_FLAG_NONE = 0,
// spill generated by regalloc
IR_LCL_FLAG_SPILL = 1 << 0,
// local has been zeroed
IR_LCL_FLAG_ZEROED = 1 << 1,
// local is actually a param
IR_LCL_FLAG_PARAM = 1 << 2,
// local has been promoted, but still exists as it is a param, and so `lower`
// is free to remove it if the ABI keeps the param in registers
IR_LCL_FLAG_PROMOTED = 1 << 5,
// saving a register across a call boundary
IR_LCL_FLAG_CALL_SAVE = 1 << 6,
};
enum ir_lcl_alloc_ty {
IR_LCL_ALLOC_TY_NONE,
IR_LCL_ALLOC_TY_NORMAL,
// fixed offset from stack pointer
// this is used for stack arguments and return values
IR_LCL_ALLOC_TY_FIXED,
};
struct ir_lcl_alloc {
ssize_t offset;
size_t size;
size_t padding; // padding used before `offset`. this means removing the alloc
// is removing `size` and `padding`
};
struct ir_lcl {
size_t id;
struct ir_func *func;
struct ir_var_ty var_ty;
struct ir_lcl *pred;
struct ir_lcl *succ;
enum ir_lcl_flags flags;
// each local is effectively in SSA, where it is stored to only once
struct ir_op *store;
void *metadata;
// HACK: this sucks, stores the current offset of the local but means they
// cannot be compacted
enum ir_lcl_alloc_ty alloc_ty;
union {
struct ir_lcl_alloc alloc;
};
};
struct ir_reg_info {
struct ir_reg reg;
struct ir_op *live;
};
struct ir_reg_state {
size_t num_gp_registers;
struct ir_reg_info *gp_registers;
size_t num_fp_registers;
struct ir_reg_info *fp_registers;
};
struct ir_reg_usage {
size_t num_nonvolatile_used;
struct ir_reg *nonvolatile_used;
};
struct ir_func {
struct ir_unit *unit;
const char *name;
struct ir_var_func_ty func_ty;
struct ir_call_info call_info;
struct arena_allocator *arena;
struct ir_basicblock *first;
struct ir_basicblock *last;
enum ir_func_flags flags;
size_t basicblock_count;
size_t stmt_count;
size_t op_count;
size_t next_basicblock_id;
size_t next_stmt_id;
size_t next_op_id;
size_t lcl_count;
size_t next_lcl_id;
struct ir_lcl *first_lcl;
struct ir_lcl *last_lcl;
struct ir_reg_usage reg_usage;
// number of stack local variables
size_t total_locals_size;
// amount of stack needed for calls, i.e that can't be taken up by locals
size_t caller_stack_needed;
};
struct ir_unit {
struct arena_allocator *arena;
const struct target *target;
struct ir_glb *first_global;
struct ir_glb *last_global;
size_t num_globals;
struct {
struct ir_glb *memmove;
struct ir_glb *memcpy;
struct ir_glb *memset;
} well_known_glbs;
};
enum ir_well_known_glb {
IR_WELL_KNOWN_GLB_MEMMOVE,
IR_WELL_KNOWN_GLB_MEMCPY,
IR_WELL_KNOWN_GLB_MEMSET,
};
enum FLAG_ENUM ir_func_iter_flags { IR_FUNC_ITER_FLAG_NONE = 0 };
struct ir_func_iter {
struct ir_func *func;
struct ir_op *op;
enum ir_func_iter_flags flags;
};
enum ir_object_ty {
IR_OBJECT_TY_GLB,
IR_OBJECT_TY_LCL,
IR_OBJECT_TY_FUNC,
IR_OBJECT_TY_VAR,
IR_OBJECT_TY_BASICBLOCK,
IR_OBJECT_TY_STMT,
IR_OBJECT_TY_OP,
};