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