]> git.xonotic.org Git - xonotic/gmqcc.git/blob - ir.h
Cleanup
[xonotic/gmqcc.git] / ir.h
1 #ifndef GMQCC_IR_HDR
2 #define GMQCC_IR_HDR
3 #include "gmqcc.h"
4
5 /*
6  * Type large enough to hold all the possible IR flags. This should be
7  * changed if the static assertion at the end of this file fails.
8  */
9 typedef uint8_t ir_flag_t;
10
11 struct ir_value;
12 struct ir_instr;
13 struct ir_block;
14 struct ir_function;
15 struct ir_builder;
16
17 struct ir_life_entry_t {
18     /* both inclusive */
19     size_t start;
20     size_t end;
21 };
22
23 enum {
24     IR_FLAG_HAS_ARRAYS              = 1 << 0,
25     IR_FLAG_HAS_UNINITIALIZED       = 1 << 1,
26     IR_FLAG_HAS_GOTO                = 1 << 2,
27     IR_FLAG_INCLUDE_DEF             = 1 << 3,
28     IR_FLAG_ERASABLE                = 1 << 4,
29     IR_FLAG_BLOCK_COVERAGE          = 1 << 5,
30
31     IR_FLAG_SPLIT_VECTOR            = 1 << 6,
32
33     IR_FLAG_LAST,
34     IR_FLAG_MASK_NO_OVERLAP      = (IR_FLAG_HAS_ARRAYS | IR_FLAG_HAS_UNINITIALIZED),
35     IR_FLAG_MASK_NO_LOCAL_TEMPS  = (IR_FLAG_HAS_ARRAYS | IR_FLAG_HAS_UNINITIALIZED)
36 };
37
38 struct ir_value {
39     char *name;
40     int vtype;
41     int store;
42     lex_ctx_t context;
43     int fieldtype; // even the IR knows the subtype of a field
44     int outtype;   // and the output type of a function
45     int cvq;       // 'const' vs 'var' qualifier
46     ir_flag_t flags;
47
48     std::vector<ir_instr *> reads;
49     std::vector<ir_instr *> writes;
50
51     // constant values
52     bool hasvalue;
53     union {
54         qcfloat_t vfloat;
55         int vint;
56         vec3_t vvec;
57         int32_t ivec[3];
58         char *vstring;
59         ir_value *vpointer;
60         ir_function *vfunc;
61     } constval;
62
63     struct {
64         int32_t globaladdr;
65         int32_t name;
66         int32_t local;         // filled by the local-allocator
67         int32_t addroffset;    // added for members
68         int32_t fieldaddr;     // to generate field-addresses early
69     } code;
70
71     // for accessing vectors
72     ir_value *members[3];
73     ir_value *memberof;
74
75     bool unique_life;      // arrays will never overlap with temps
76     bool locked;           // temps living during a CALL must be locked
77     bool callparam;
78
79     ir_life_entry_t *life; // For the temp allocator
80 };
81
82 /*
83  * ir_value can be a variable, or created by an operation
84  * if a result of an operation: the function should store
85  * it to remember to delete it / garbage collect it
86  */
87 void            ir_value_delete(ir_value*);
88 ir_value*       ir_value_vector_member(ir_value*, unsigned int member);
89 bool GMQCC_WARN ir_value_set_float(ir_value*, float f);
90 bool GMQCC_WARN ir_value_set_func(ir_value*, int f);
91 bool GMQCC_WARN ir_value_set_string(ir_value*, const char *s);
92 bool GMQCC_WARN ir_value_set_vector(ir_value*, vec3_t v);
93 bool GMQCC_WARN ir_value_set_field(ir_value*, ir_value *fld);
94 bool            ir_value_lives(ir_value*, size_t);
95 void            ir_value_dump_life(const ir_value *self, int (*oprintf)(const char*,...));
96
97 /* PHI data */
98 struct ir_phi_entry_t {
99     ir_value *value;
100     ir_block *from;
101 };
102
103 /* instruction */
104 struct ir_instr {
105     int opcode;
106     lex_ctx_t context;
107     ir_value *(_ops[3]);
108     ir_block *(bops[2]);
109
110     std::vector<ir_phi_entry_t> phi;
111     std::vector<ir_value *> params;
112
113     // For the temp-allocation
114     size_t eid;
115
116     // For IFs
117     bool likely;
118
119     ir_block *owner;
120 };
121
122 /* block */
123 struct ir_block {
124     char *label;
125     lex_ctx_t context;
126     bool final; /* once a jump is added we're done */
127
128     ir_instr **instr;
129     ir_block **entries;
130     ir_block **exits;
131     std::vector<ir_value *> living;
132
133     /* For the temp-allocation */
134     size_t entry_id;
135     size_t eid;
136     bool is_return;
137
138     ir_function *owner;
139
140     bool generated;
141     size_t code_start;
142 };
143
144 ir_value*       ir_block_create_binop(ir_block*, lex_ctx_t, const char *label, int op, ir_value *left, ir_value *right);
145 ir_value*       ir_block_create_unary(ir_block*, lex_ctx_t, const char *label, int op, ir_value *operand);
146 bool GMQCC_WARN ir_block_create_store_op(ir_block*, lex_ctx_t, int op, ir_value *target, ir_value *what);
147 bool GMQCC_WARN ir_block_create_storep(ir_block*, lex_ctx_t, ir_value *target, ir_value *what);
148 ir_value*       ir_block_create_load_from_ent(ir_block*, lex_ctx_t, const char *label, ir_value *ent, ir_value *field, int outype);
149 ir_value*       ir_block_create_fieldaddress(ir_block*, lex_ctx_t, const char *label, ir_value *entity, ir_value *field);
150 bool GMQCC_WARN ir_block_create_state_op(ir_block*, lex_ctx_t, ir_value *frame, ir_value *think);
151
152 /* This is to create an instruction of the form
153  * <outtype>%label := opcode a, b
154  */
155 ir_instr* ir_block_create_phi(ir_block*, lex_ctx_t, const char *label, int vtype);
156 ir_value* ir_phi_value(ir_instr*);
157 void ir_phi_add(ir_instr*, ir_block *b, ir_value *v);
158 ir_instr* ir_block_create_call(ir_block*, lex_ctx_t, const char *label, ir_value *func, bool noreturn);
159 ir_value* ir_call_value(ir_instr*);
160 void ir_call_param(ir_instr*, ir_value*);
161
162 bool GMQCC_WARN ir_block_create_return(ir_block*, lex_ctx_t, ir_value *opt_value);
163
164 bool GMQCC_WARN ir_block_create_if(ir_block*, lex_ctx_t, ir_value *cond,
165                                    ir_block *ontrue, ir_block *onfalse);
166 /*
167  * A 'goto' is an actual 'goto' coded in QC, whereas
168  * a 'jump' is a virtual construct which simply names the
169  * next block to go to.
170  * A goto usually becomes an OP_GOTO in the resulting code,
171  * whereas a 'jump' usually doesn't add any actual instruction.
172  */
173 bool GMQCC_WARN ir_block_create_jump(ir_block*, lex_ctx_t, ir_block *to);
174 bool GMQCC_WARN ir_block_create_goto(ir_block*, lex_ctx_t, ir_block *to);
175
176 /* function */
177 struct ir_function {
178     char      *name;
179     int        outtype;
180     int       *params;
181     ir_block **blocks;
182     ir_flag_t  flags;
183     int        builtin;
184
185     /*
186      * values generated from operations
187      * which might get optimized away, so anything
188      * in there needs to be deleted in the dtor.
189      */
190     ir_value **values;
191     ir_value **locals;     /* locally defined variables */
192     ir_value *value;
193
194     size_t allocated_locals;
195     size_t globaltemps;
196
197     ir_block*  first;
198     ir_block*  last;
199
200     lex_ctx_t  context;
201
202     /*
203      * for prototypes - first we generate all the
204      * globals, and we remember teh function-defs
205      * so we can later fill in the entry pos
206      *
207      * remember the ID:
208      */
209     qcint_t code_function_def;
210
211     /* for temp allocation */
212     size_t run_id;
213
214     ir_builder *owner;
215
216     /* vararg support: */
217     size_t max_varargs;
218 };
219
220
221 ir_value*       ir_function_create_local(ir_function *self, const char *name, int vtype, bool param);
222 bool GMQCC_WARN ir_function_finalize(ir_function*);
223 ir_block*       ir_function_create_block(lex_ctx_t ctx, ir_function*, const char *label);
224
225 /* builder */
226 #define IR_HT_SIZE          1024
227 #define IR_MAX_VINSTR_TEMPS 1
228
229 struct ir_builder {
230     char *name;
231     ir_function **functions;
232     ir_value    **globals;
233     ir_value    **fields;
234     ir_value    **const_floats; /* for reusing them in vector-splits, TODO: sort this or use a radix-tree */
235
236     ht            htfunctions;
237     ht            htglobals;
238     ht            htfields;
239
240     ir_value    **extparams;
241     ir_value    **extparam_protos;
242
243     /* the highest func->allocated_locals */
244     size_t        max_locals;
245     size_t        max_globaltemps;
246     uint32_t      first_common_local;
247     uint32_t      first_common_globaltemp;
248
249     const char **filenames;
250     qcint_t     *filestrings;
251     /* we cache the #IMMEDIATE string here */
252     qcint_t      str_immediate;
253     /* there should just be this one nil */
254     ir_value    *nil;
255     ir_value    *reserved_va_count;
256     ir_value    *coverage_func;
257     /* some virtual instructions require temps, and their code is isolated
258      * so that we don't need to keep track of their liveness.
259      */
260     ir_value    *vinstr_temp[IR_MAX_VINSTR_TEMPS];
261
262     /* code generator */
263     code_t      *code;
264 };
265
266 ir_builder*  ir_builder_new(const char *modulename);
267 void         ir_builder_delete(ir_builder*);
268 ir_function* ir_builder_create_function(ir_builder*, const char *name, int outtype);
269 ir_value*    ir_builder_create_global(ir_builder*, const char *name, int vtype);
270 ir_value*    ir_builder_create_field(ir_builder*, const char *name, int vtype);
271 ir_value*    ir_builder_get_va_count(ir_builder*);
272 bool         ir_builder_generate(ir_builder *self, const char *filename);
273 void         ir_builder_dump(ir_builder*, int (*oprintf)(const char*, ...));
274
275 /*
276  * This code assumes 32 bit floats while generating binary
277  * Blub: don't use extern here, it's annoying and shows up in nm
278  * for some reason :P
279  */
280 typedef int static_assert_is_32bit_float  [(sizeof(int32_t) == 4)   ? 1 : -1];
281 typedef int static_assert_is_32bit_integer[(sizeof(qcfloat_t) == 4) ? 1 : -1];
282
283 /*
284  * If the condition creates a situation where this becomes -1 size it means there are
285  * more IR_FLAGs than the type ir_flag_t is capable of holding. So either eliminate
286  * the IR flag count or change the ir_flag_t typedef to a type large enough to accomodate
287  * all the flags.
288  */
289 typedef int static_assert_is_ir_flag_safe [((IR_FLAG_LAST) <= (ir_flag_t)(-1)) ? 1 : -1];
290
291 #endif