2 * Copyright (C) 2012, 2013, 2014
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:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
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
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.
31 typedef uint8_t ir_flag_t;
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;
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 IR_FLAG_BLOCK_COVERAGE = 1 << 5,
54 IR_FLAG_MASK_NO_OVERLAP = (IR_FLAG_HAS_ARRAYS | IR_FLAG_HAS_UNINITIALIZED),
55 IR_FLAG_MASK_NO_LOCAL_TEMPS = (IR_FLAG_HAS_ARRAYS | IR_FLAG_HAS_UNINITIALIZED)
65 int fieldtype; /* even the IR knows the subtype of a field */
66 int outtype; /* and the output type of a function */
67 int cvq; /* 'const' vs 'var' qualifier */
88 int32_t local; /* filled by the local-allocator */
89 int32_t addroffset; /* added for members */
90 int32_t fieldaddr; /* to generate field-addresses early */
93 /* for acessing vectors */
98 bool unique_life; /* arrays will never overlap with temps */
99 bool locked; /* temps living during a CALL must be locked */
102 ir_life_entry_t *life; /* For the temp allocator */
106 * ir_value can be a variable, or created by an operation
107 * if a result of an operation: the function should store
108 * it to remember to delete it / garbage collect it
110 void ir_value_delete(ir_value*);
111 ir_value* ir_value_vector_member(ir_value*, unsigned int member);
112 bool GMQCC_WARN ir_value_set_float(ir_value*, float f);
113 bool GMQCC_WARN ir_value_set_func(ir_value*, int f);
114 bool GMQCC_WARN ir_value_set_string(ir_value*, const char *s);
115 bool GMQCC_WARN ir_value_set_vector(ir_value*, vec3_t v);
116 bool GMQCC_WARN ir_value_set_field(ir_value*, ir_value *fld);
117 bool ir_value_lives(ir_value*, size_t);
118 void ir_value_dump_life(const ir_value *self, int (*oprintf)(const char*,...));
121 typedef struct ir_phi_entry_s {
136 /* For the temp-allocation */
149 bool final; /* once a jump is added we're done */
156 /* For the temp-allocation */
167 ir_value* ir_block_create_binop(ir_block*, lex_ctx_t, const char *label, int op, ir_value *left, ir_value *right);
168 ir_value* ir_block_create_unary(ir_block*, lex_ctx_t, const char *label, int op, ir_value *operand);
169 bool GMQCC_WARN ir_block_create_store_op(ir_block*, lex_ctx_t, int op, ir_value *target, ir_value *what);
170 bool GMQCC_WARN ir_block_create_storep(ir_block*, lex_ctx_t, ir_value *target, ir_value *what);
171 ir_value* ir_block_create_load_from_ent(ir_block*, lex_ctx_t, const char *label, ir_value *ent, ir_value *field, int outype);
172 ir_value* ir_block_create_fieldaddress(ir_block*, lex_ctx_t, const char *label, ir_value *entity, ir_value *field);
174 /* This is to create an instruction of the form
175 * <outtype>%label := opcode a, b
177 ir_instr* ir_block_create_phi(ir_block*, lex_ctx_t, const char *label, int vtype);
178 ir_value* ir_phi_value(ir_instr*);
179 void ir_phi_add(ir_instr*, ir_block *b, ir_value *v);
180 ir_instr* ir_block_create_call(ir_block*, lex_ctx_t, const char *label, ir_value *func, bool noreturn);
181 ir_value* ir_call_value(ir_instr*);
182 void ir_call_param(ir_instr*, ir_value*);
184 bool GMQCC_WARN ir_block_create_return(ir_block*, lex_ctx_t, ir_value *opt_value);
186 bool GMQCC_WARN ir_block_create_if(ir_block*, lex_ctx_t, ir_value *cond,
187 ir_block *ontrue, ir_block *onfalse);
189 * A 'goto' is an actual 'goto' coded in QC, whereas
190 * a 'jump' is a virtual construct which simply names the
191 * next block to go to.
192 * A goto usually becomes an OP_GOTO in the resulting code,
193 * whereas a 'jump' usually doesn't add any actual instruction.
195 bool GMQCC_WARN ir_block_create_jump(ir_block*, lex_ctx_t, ir_block *to);
196 bool GMQCC_WARN ir_block_create_goto(ir_block*, lex_ctx_t, ir_block *to);
199 struct ir_function_s {
208 * values generated from operations
209 * which might get optimized away, so anything
210 * in there needs to be deleted in the dtor.
213 ir_value **locals; /* locally defined variables */
216 size_t allocated_locals;
225 * for prototypes - first we generate all the
226 * globals, and we remember teh function-defs
227 * so we can later fill in the entry pos
231 qcint_t code_function_def;
233 /* for temp allocation */
238 /* vararg support: */
243 ir_value* ir_function_create_local(ir_function *self, const char *name, int vtype, bool param);
244 bool GMQCC_WARN ir_function_finalize(ir_function*);
245 ir_block* ir_function_create_block(lex_ctx_t ctx, ir_function*, const char *label);
248 #define IR_HT_SIZE 1024
249 #define IR_MAX_VINSTR_TEMPS 1
251 struct ir_builder_s {
253 ir_function **functions;
261 ir_value **extparams;
262 ir_value **extparam_protos;
264 /* the highest func->allocated_locals */
266 size_t max_globaltemps;
267 uint32_t first_common_local;
268 uint32_t first_common_globaltemp;
270 const char **filenames;
271 qcint_t *filestrings;
272 /* we cache the #IMMEDIATE string here */
273 qcint_t str_immediate;
274 /* there should just be this one nil */
276 ir_value *reserved_va_count;
277 ir_value *coverage_func;
278 /* some virtual instructions require temps, and their code is isolated
279 * so that we don't need to keep track of their liveness.
281 ir_value *vinstr_temp[IR_MAX_VINSTR_TEMPS];
287 ir_builder* ir_builder_new(const char *modulename);
288 void ir_builder_delete(ir_builder*);
289 ir_function* ir_builder_create_function(ir_builder*, const char *name, int outtype);
290 ir_value* ir_builder_create_global(ir_builder*, const char *name, int vtype);
291 ir_value* ir_builder_create_field(ir_builder*, const char *name, int vtype);
292 ir_value* ir_builder_get_va_count(ir_builder*);
293 bool ir_builder_generate(ir_builder *self, const char *filename);
294 void ir_builder_dump(ir_builder*, int (*oprintf)(const char*, ...));
297 * This code assumes 32 bit floats while generating binary
298 * Blub: don't use extern here, it's annoying and shows up in nm
301 typedef int static_assert_is_32bit_float [(sizeof(int32_t) == 4) ? 1 : -1];
302 typedef int static_assert_is_32bit_integer[(sizeof(qcfloat_t) == 4) ? 1 : -1];
305 * If the condition creates a situation where this becomes -1 size it means there are
306 * more IR_FLAGs than the type ir_flag_t is capable of holding. So either eliminate
307 * the IR flag count or change the ir_flag_t typedef to a type large enough to accomodate
310 typedef int static_assert_is_ir_flag_safe [((IR_FLAG_LAST) <= (ir_flag_t)(-1)) ? 1 : -1];