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