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