6 * Type large enough to hold all the possible IR flags. This should be
7 * changed if the static assertion at the end of this file fails.
9 typedef uint8_t ir_flag_t;
17 struct ir_life_entry_t {
24 IR_FLAG_HAS_ARRAYS = 1 << 0,
25 IR_FLAG_HAS_UNINITIALIZED = 1 << 1,
26 IR_FLAG_HAS_GOTO = 1 << 2,
27 IR_FLAG_INCLUDE_DEF = 1 << 3,
28 IR_FLAG_ERASABLE = 1 << 4,
29 IR_FLAG_BLOCK_COVERAGE = 1 << 5,
31 IR_FLAG_SPLIT_VECTOR = 1 << 6,
34 IR_FLAG_MASK_NO_OVERLAP = (IR_FLAG_HAS_ARRAYS | IR_FLAG_HAS_UNINITIALIZED),
35 IR_FLAG_MASK_NO_LOCAL_TEMPS = (IR_FLAG_HAS_ARRAYS | IR_FLAG_HAS_UNINITIALIZED)
39 ir_value(std::string&& name, store_type storetype, qc_type vtype);
42 void* operator new(std::size_t); // to use mem_a
43 void operator delete(void*); // to use mem_d
50 qc_type fieldtype; // even the IR knows the subtype of a field
51 qc_type outtype; // and the output type of a function
52 int cvq; // 'const' vs 'var' qualifier
55 std::vector<ir_instr *> reads;
56 std::vector<ir_instr *> writes;
73 int32_t local; // filled by the local-allocator
74 int32_t addroffset; // added for members
75 int32_t fieldaddr; // to generate field-addresses early
78 // for accessing vectors
82 bool unique_life; // arrays will never overlap with temps
83 bool locked; // temps living during a CALL must be locked
86 std::vector<ir_life_entry_t> life; // For the temp allocator
90 * ir_value can be a variable, or created by an operation
91 * if a result of an operation: the function should store
92 * it to remember to delete it / garbage collect it
94 ir_value* ir_value_vector_member(ir_value*, unsigned int member);
95 bool GMQCC_WARN ir_value_set_float(ir_value*, float f);
96 bool GMQCC_WARN ir_value_set_func(ir_value*, int f);
97 bool GMQCC_WARN ir_value_set_string(ir_value*, const char *s);
98 bool GMQCC_WARN ir_value_set_vector(ir_value*, vec3_t v);
99 bool GMQCC_WARN ir_value_set_field(ir_value*, ir_value *fld);
100 bool ir_value_lives(ir_value*, size_t);
101 void ir_value_dump_life(const ir_value *self, int (*oprintf)(const char*,...));
104 struct ir_phi_entry_t {
116 std::vector<ir_phi_entry_t> phi;
117 std::vector<ir_value *> params;
119 // For the temp-allocation
130 void* operator new(std::size_t);
131 void operator delete(void*);
133 ir_block(ir_function *owner, const std::string& name);
140 bool final = false; /* once a jump is added we're done */
142 ir_instr **instr = nullptr;
143 ir_block **entries = nullptr;
144 ir_block **exits = nullptr;
145 std::vector<ir_value *> living;
147 /* For the temp-allocation */
150 bool is_return = false;
152 bool generated = false;
153 size_t code_start = 0;
156 ir_value* ir_block_create_binop(ir_block*, lex_ctx_t, const char *label, int op, ir_value *left, ir_value *right);
157 ir_value* ir_block_create_unary(ir_block*, lex_ctx_t, const char *label, int op, ir_value *operand);
158 bool GMQCC_WARN ir_block_create_store_op(ir_block*, lex_ctx_t, int op, ir_value *target, ir_value *what);
159 bool GMQCC_WARN ir_block_create_storep(ir_block*, lex_ctx_t, ir_value *target, ir_value *what);
160 ir_value* ir_block_create_load_from_ent(ir_block*, lex_ctx_t, const char *label, ir_value *ent, ir_value *field, qc_type outype);
161 ir_value* ir_block_create_fieldaddress(ir_block*, lex_ctx_t, const char *label, ir_value *entity, ir_value *field);
162 bool GMQCC_WARN ir_block_create_state_op(ir_block*, lex_ctx_t, ir_value *frame, ir_value *think);
164 /* This is to create an instruction of the form
165 * <outtype>%label := opcode a, b
167 ir_instr* ir_block_create_phi(ir_block*, lex_ctx_t, const char *label, qc_type vtype);
168 ir_value* ir_phi_value(ir_instr*);
169 void ir_phi_add(ir_instr*, ir_block *b, ir_value *v);
170 ir_instr* ir_block_create_call(ir_block*, lex_ctx_t, const char *label, ir_value *func, bool noreturn);
171 ir_value* ir_call_value(ir_instr*);
172 void ir_call_param(ir_instr*, ir_value*);
174 bool GMQCC_WARN ir_block_create_return(ir_block*, lex_ctx_t, ir_value *opt_value);
176 bool GMQCC_WARN ir_block_create_if(ir_block*, lex_ctx_t, ir_value *cond,
177 ir_block *ontrue, ir_block *onfalse);
179 * A 'goto' is an actual 'goto' coded in QC, whereas
180 * a 'jump' is a virtual construct which simply names the
181 * next block to go to.
182 * A goto usually becomes an OP_GOTO in the resulting code,
183 * whereas a 'jump' usually doesn't add any actual instruction.
185 bool GMQCC_WARN ir_block_create_jump(ir_block*, lex_ctx_t, ir_block *to);
186 bool GMQCC_WARN ir_block_create_goto(ir_block*, lex_ctx_t, ir_block *to);
190 void* operator new(std::size_t);
191 void operator delete(void*);
193 ir_function(ir_builder *owner, qc_type returntype);
200 int *params = nullptr;
204 std::vector<std::unique_ptr<ir_block>> blocks;
207 * values generated from operations
208 * which might get optimized away, so anything
209 * in there needs to be deleted in the dtor.
211 std::vector<std::unique_ptr<ir_value>> values;
212 std::vector<std::unique_ptr<ir_value>> locals; /* locally defined variables */
213 ir_value *value = nullptr;
215 size_t allocated_locals = 0;
216 size_t globaltemps = 0;
218 ir_block* first = nullptr;
219 ir_block* last = nullptr;
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
230 qcint_t code_function_def = -1;
232 /* for temp allocation */
235 /* vararg support: */
236 size_t max_varargs = 0;
240 ir_value* ir_function_create_local(ir_function *self, const std::string& name, qc_type 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);
245 #define IR_HT_SIZE 1024
246 #define IR_MAX_VINSTR_TEMPS 1
249 void* operator new(std::size_t);
250 void operator delete(void*);
251 ir_builder(const std::string& modulename);
255 std::vector<std::unique_ptr<ir_function>> functions;
256 std::vector<std::unique_ptr<ir_value>> globals;
257 std::vector<std::unique_ptr<ir_value>> fields;
258 // for reusing them in vector-splits, TODO: sort this or use a radix-tree
259 std::vector<ir_value*> const_floats;
265 std::vector<std::unique_ptr<ir_value>> extparams;
266 std::vector<std::unique_ptr<ir_value>> extparam_protos;
268 // the highest func->allocated_locals
269 size_t max_locals = 0;
270 size_t max_globaltemps = 0;
271 uint32_t first_common_local = 0;
272 uint32_t first_common_globaltemp = 0;
274 std::vector<const char*> filenames;
275 std::vector<qcint_t> filestrings;
277 // we cache the #IMMEDIATE string here
278 qcint_t str_immediate = 0;
280 // there should just be this one nil
282 ir_value *reserved_va_count = nullptr;
283 ir_value *coverage_func = nullptr;
285 /* some virtual instructions require temps, and their code is isolated
286 * so that we don't need to keep track of their liveness.
288 ir_value *vinstr_temp[IR_MAX_VINSTR_TEMPS];
294 ir_function* ir_builder_create_function(ir_builder*, const std::string& name, qc_type outtype);
295 ir_value* ir_builder_create_global(ir_builder*, const std::string& name, qc_type vtype);
296 ir_value* ir_builder_create_field(ir_builder*, const std::string& name, qc_type vtype);
297 ir_value* ir_builder_get_va_count(ir_builder*);
298 bool ir_builder_generate(ir_builder *self, const char *filename);
299 void ir_builder_dump(ir_builder*, int (*oprintf)(const char*, ...));
302 * This code assumes 32 bit floats while generating binary
303 * Blub: don't use extern here, it's annoying and shows up in nm
306 typedef int static_assert_is_32bit_float [(sizeof(int32_t) == 4) ? 1 : -1];
307 typedef int static_assert_is_32bit_integer[(sizeof(qcfloat_t) == 4) ? 1 : -1];
310 * If the condition creates a situation where this becomes -1 size it means there are
311 * more IR_FLAGs than the type ir_flag_t is capable of holding. So either eliminate
312 * the IR flag count or change the ir_flag_t typedef to a type large enough to accomodate
315 typedef int static_assert_is_ir_flag_safe [((IR_FLAG_LAST) <= (ir_flag_t)(-1)) ? 1 : -1];