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