]> git.xonotic.org Git - xonotic/gmqcc.git/blob - ir.h
a12827fc36898f8ac90036c4fcff81bff87569c5
[xonotic/gmqcc.git] / ir.h
1 /*
2  * Copyright (C) 2012
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
26 /* ir_value */
27
28 typedef struct
29 {
30     /* both inclusive */
31     size_t start;
32     size_t end;
33 } ir_life_entry_t;
34
35 struct ir_function_s;
36 typedef struct ir_value_s {
37     char      *name;
38     int       vtype;
39     int       store;
40     lex_ctx   context;
41     /* even the IR knows the subtype of a field */
42     int       fieldtype;
43     /* and the output type of a function */
44     int       outtype;
45     /* 'const' vs 'var' qualifier */
46     int       cvq;
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         vector   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     /* For the temp allocator */
79     ir_life_entry_t *life;
80 } ir_value;
81
82 int32_t ir_value_code_addr(const ir_value*);
83
84 /* ir_value can be a variable, or created by an operation */
85 ir_value* ir_value_var(const char *name, int st, int vtype);
86 /* if a result of an operation: the function should store
87  * it to remember to delete it / garbage collect it
88  */
89 ir_value* ir_value_out(struct ir_function_s *owner, const char *name, int st, int vtype);
90 void      ir_value_delete(ir_value*);
91 bool      ir_value_set_name(ir_value*, const char *name);
92 ir_value* ir_value_vector_member(ir_value*, unsigned int member);
93
94 bool GMQCC_WARN vec_ir_value_find(ir_value **vec, ir_value *what, size_t *idx);
95
96 bool GMQCC_WARN ir_value_set_float(ir_value*, float f);
97 bool GMQCC_WARN ir_value_set_func(ir_value*, int f);
98 #if 0
99 bool GMQCC_WARN ir_value_set_int(ir_value*, int i);
100 #endif
101 bool GMQCC_WARN ir_value_set_string(ir_value*, const char *s);
102 bool GMQCC_WARN ir_value_set_vector(ir_value*, vector v);
103 bool GMQCC_WARN ir_value_set_field(ir_value*, ir_value *fld);
104 /*bool   ir_value_set_pointer_v(ir_value*, ir_value* p); */
105 /*bool   ir_value_set_pointer_i(ir_value*, int i);       */
106
107 /* merge an instruction into the life-range */
108 /* returns false if the lifepoint was already known */
109 bool ir_value_life_merge(ir_value*, size_t);
110 bool ir_value_life_merge_into(ir_value*, const ir_value*);
111 /* check if a value lives at a specific point */
112 bool ir_value_lives(ir_value*, size_t);
113 /* check if the life-range of 2 values overlaps */
114 bool ir_values_overlap(const ir_value*, const ir_value*);
115
116 void ir_value_dump(ir_value*, int (*oprintf)(const char*,...));
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 {
122     ir_value          *value;
123     struct ir_block_s *from;
124 } ir_phi_entry_t;
125
126 /* instruction */
127 typedef struct ir_instr_s
128 {
129     int       opcode;
130     lex_ctx   context;
131     ir_value* (_ops[3]);
132     struct ir_block_s* (bops[2]);
133
134     ir_phi_entry_t *phi;
135     ir_value      **params;
136
137     /* For the temp-allocation */
138     size_t eid;
139
140     /* For IFs */
141     bool   likely;
142
143     struct ir_block_s *owner;
144 } ir_instr;
145
146 ir_instr* ir_instr_new(lex_ctx ctx, struct ir_block_s *owner, int opcode);
147 void      ir_instr_delete(ir_instr*);
148
149 bool GMQCC_WARN vec_ir_instr_find(ir_instr **vec, ir_instr *what, size_t *idx);
150
151 bool GMQCC_WARN ir_instr_op(ir_instr*, int op, ir_value *value, bool writing);
152
153 void ir_instr_dump(ir_instr* in, char *ind, int (*oprintf)(const char*,...));
154
155 /* block */
156 typedef struct ir_block_s
157 {
158     char      *label;
159     lex_ctx    context;
160     bool       final; /* once a jump is added we're done */
161
162     ir_instr          **instr;
163     struct ir_block_s **entries;
164     struct ir_block_s **exits;
165     ir_value          **living;
166
167     /* For the temp-allocation */
168     size_t eid;
169     bool   is_return;
170     size_t run_id;
171
172     struct ir_function_s *owner;
173
174     bool   generated;
175     size_t code_start;
176 } ir_block;
177
178 ir_block* ir_block_new(struct ir_function_s *owner, const char *label);
179 void      ir_block_delete(ir_block*);
180
181 bool      ir_block_set_label(ir_block*, const char *label);
182
183 ir_value* ir_block_create_binop(ir_block*, lex_ctx, const char *label, int op,
184                                 ir_value *left, ir_value *right);
185 ir_value* ir_block_create_unary(ir_block*, lex_ctx, const char *label, int op,
186                                 ir_value *operand);
187 bool GMQCC_WARN ir_block_create_store_op(ir_block*, lex_ctx, int op, ir_value *target, ir_value *what);
188 bool GMQCC_WARN ir_block_create_store(ir_block*, lex_ctx, ir_value *target, ir_value *what);
189 bool GMQCC_WARN ir_block_create_storep(ir_block*, lex_ctx, ir_value *target, ir_value *what);
190
191 /* field must be of TYPE_FIELD */
192 ir_value* ir_block_create_load_from_ent(ir_block*, lex_ctx, const char *label, ir_value *ent, ir_value *field, int outype);
193
194 ir_value* ir_block_create_fieldaddress(ir_block*, lex_ctx, const char *label, ir_value *entity, ir_value *field);
195
196 /* This is to create an instruction of the form
197  * <outtype>%label := opcode a, b
198  */
199 ir_value* ir_block_create_general_instr(ir_block *self, lex_ctx, const char *label,
200                                         int op, ir_value *a, ir_value *b, int outype);
201
202 ir_value* ir_block_create_add(ir_block*, lex_ctx, const char *label, ir_value *l, ir_value *r);
203 ir_value* ir_block_create_sub(ir_block*, lex_ctx, const char *label, ir_value *l, ir_value *r);
204 ir_value* ir_block_create_mul(ir_block*, lex_ctx, const char *label, ir_value *l, ir_value *r);
205 ir_value* ir_block_create_div(ir_block*, lex_ctx, const char *label, ir_value *l, ir_value *r);
206 ir_instr* ir_block_create_phi(ir_block*, lex_ctx, const char *label, int vtype);
207 ir_value* ir_phi_value(ir_instr*);
208 void ir_phi_add(ir_instr*, ir_block *b, ir_value *v);
209 ir_instr* ir_block_create_call(ir_block*, lex_ctx, const char *label, ir_value *func);
210 ir_value* ir_call_value(ir_instr*);
211 void ir_call_param(ir_instr*, ir_value*);
212
213 bool GMQCC_WARN ir_block_create_return(ir_block*, lex_ctx, ir_value *opt_value);
214
215 bool GMQCC_WARN ir_block_create_if(ir_block*, lex_ctx, ir_value *cond,
216                                    ir_block *ontrue, ir_block *onfalse);
217 /* A 'goto' is an actual 'goto' coded in QC, whereas
218  * a 'jump' is a virtual construct which simply names the
219  * next block to go to.
220  * A goto usually becomes an OP_GOTO in the resulting code,
221  * whereas a 'jump' usually doesn't add any actual instruction.
222  */
223 bool GMQCC_WARN ir_block_create_jump(ir_block*, lex_ctx, ir_block *to);
224 bool GMQCC_WARN ir_block_create_goto(ir_block*, lex_ctx, ir_block *to);
225
226 void ir_block_dump(ir_block*, char *ind, int (*oprintf)(const char*,...));
227
228 /* function */
229
230 typedef struct ir_function_s
231 {
232     char      *name;
233     int        outtype;
234     int       *params;
235     ir_block **blocks;
236
237     int builtin;
238
239     ir_value *value;
240
241     /* values generated from operations
242      * which might get optimized away, so anything
243      * in there needs to be deleted in the dtor.
244      */
245     ir_value **values;
246
247     /* locally defined variables */
248     ir_value **locals;
249
250     size_t allocated_locals;
251
252     ir_block*     first;
253     ir_block*     last;
254
255     lex_ctx       context;
256
257     /* for prototypes - first we generate all the
258      * globals, and we remember teh function-defs
259      * so we can later fill in the entry pos
260      *
261      * remember the ID:
262      */
263     qcint code_function_def;
264
265     /* for temp allocation */
266     size_t run_id;
267
268     struct ir_builder_s *owner;
269 } ir_function;
270
271 ir_function* ir_function_new(struct ir_builder_s *owner, int returntype);
272 void         ir_function_delete(ir_function*);
273
274 void ir_function_collect_value(ir_function*, ir_value *value);
275
276 bool ir_function_set_name(ir_function*, const char *name);
277
278 ir_value* ir_function_create_local(ir_function *self, const char *name, int vtype, bool param);
279
280 bool GMQCC_WARN ir_function_finalize(ir_function*);
281 /*
282 bool ir_function_naive_phi(ir_function*);
283 bool ir_function_enumerate(ir_function*);
284 bool ir_function_calculate_liferanges(ir_function*);
285 */
286
287 ir_block* ir_function_create_block(lex_ctx ctx, ir_function*, const char *label);
288
289 void ir_function_dump(ir_function*, char *ind, int (*oprintf)(const char*,...));
290
291 /* builder */
292 #define IR_HT_SIZE 1024
293 typedef struct ir_builder_s
294 {
295     char *name;
296     ir_function **functions;
297     ir_value    **globals;
298     ir_value    **fields;
299
300     ht            htfunctions;
301     ht            htglobals;
302     ht            htfields;
303
304     ir_value    **extparams;
305
306     const char **filenames;
307     qcint       *filestrings;
308     /* we cache the #IMMEDIATE string here */
309     qcint str_immediate;
310 } ir_builder;
311
312 ir_builder* ir_builder_new(const char *modulename);
313 void        ir_builder_delete(ir_builder*);
314
315 bool ir_builder_set_name(ir_builder *self, const char *name);
316
317 ir_function* ir_builder_get_function(ir_builder*, const char *fun);
318 ir_function* ir_builder_create_function(ir_builder*, const char *name, int outtype);
319
320 ir_value* ir_builder_get_global(ir_builder*, const char *fun);
321 ir_value* ir_builder_create_global(ir_builder*, const char *name, int vtype);
322 ir_value* ir_builder_get_field(ir_builder*, const char *fun);
323 ir_value* ir_builder_create_field(ir_builder*, const char *name, int vtype);
324
325 bool ir_builder_generate(ir_builder *self, const char *filename);
326
327 void ir_builder_dump(ir_builder*, int (*oprintf)(const char*, ...));
328
329 /* This code assumes 32 bit floats while generating binary */
330 extern int check_int_and_float_size
331 [ (sizeof(int32_t) == sizeof(qcfloat)) ? 1 : -1 ];
332
333 #endif