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