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 {
111 void* operator new(std::size_t);
112 void operator delete(void*);
114 ir_instr(lex_ctx_t, ir_block *owner, int opcode);
119 ir_value *(_ops[3]) = { nullptr, nullptr, nullptr };
120 ir_block *(bops[2]) = { nullptr, nullptr };
122 std::vector<ir_phi_entry_t> phi;
123 std::vector<ir_value *> params;
125 // For the temp-allocation
136 void* operator new(std::size_t);
137 void operator delete(void*);
139 ir_block(ir_function *owner, const std::string& name);
146 bool final = false; /* once a jump is added we're done */
148 ir_instr **instr = nullptr;
149 ir_block **entries = nullptr;
150 ir_block **exits = nullptr;
151 std::vector<ir_value *> living;
153 /* For the temp-allocation */
156 bool is_return = false;
158 bool generated = false;
159 size_t code_start = 0;
162 ir_value* ir_block_create_binop(ir_block*, lex_ctx_t, const char *label, int op, ir_value *left, ir_value *right);
163 ir_value* ir_block_create_unary(ir_block*, lex_ctx_t, const char *label, int op, ir_value *operand);
164 bool GMQCC_WARN ir_block_create_store_op(ir_block*, lex_ctx_t, int op, ir_value *target, ir_value *what);
165 bool GMQCC_WARN ir_block_create_storep(ir_block*, lex_ctx_t, ir_value *target, ir_value *what);
166 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);
167 ir_value* ir_block_create_fieldaddress(ir_block*, lex_ctx_t, const char *label, ir_value *entity, ir_value *field);
168 bool GMQCC_WARN ir_block_create_state_op(ir_block*, lex_ctx_t, ir_value *frame, ir_value *think);
170 /* This is to create an instruction of the form
171 * <outtype>%label := opcode a, b
173 ir_instr* ir_block_create_phi(ir_block*, lex_ctx_t, const char *label, qc_type vtype);
174 ir_value* ir_phi_value(ir_instr*);
175 void ir_phi_add(ir_instr*, ir_block *b, ir_value *v);
176 ir_instr* ir_block_create_call(ir_block*, lex_ctx_t, const char *label, ir_value *func, bool noreturn);
177 ir_value* ir_call_value(ir_instr*);
178 void ir_call_param(ir_instr*, ir_value*);
180 bool GMQCC_WARN ir_block_create_return(ir_block*, lex_ctx_t, ir_value *opt_value);
182 bool GMQCC_WARN ir_block_create_if(ir_block*, lex_ctx_t, ir_value *cond,
183 ir_block *ontrue, ir_block *onfalse);
185 * A 'goto' is an actual 'goto' coded in QC, whereas
186 * a 'jump' is a virtual construct which simply names the
187 * next block to go to.
188 * A goto usually becomes an OP_GOTO in the resulting code,
189 * whereas a 'jump' usually doesn't add any actual instruction.
191 bool GMQCC_WARN ir_block_create_jump(ir_block*, lex_ctx_t, ir_block *to);
192 bool GMQCC_WARN ir_block_create_goto(ir_block*, lex_ctx_t, ir_block *to);
196 void* operator new(std::size_t);
197 void operator delete(void*);
199 ir_function(ir_builder *owner, qc_type returntype);
206 int *params = nullptr;
210 std::vector<std::unique_ptr<ir_block>> blocks;
213 * values generated from operations
214 * which might get optimized away, so anything
215 * in there needs to be deleted in the dtor.
217 std::vector<std::unique_ptr<ir_value>> values;
218 std::vector<std::unique_ptr<ir_value>> locals; /* locally defined variables */
219 ir_value *value = nullptr;
221 size_t allocated_locals = 0;
222 size_t globaltemps = 0;
224 ir_block* first = nullptr;
225 ir_block* last = nullptr;
230 * for prototypes - first we generate all the
231 * globals, and we remember teh function-defs
232 * so we can later fill in the entry pos
236 qcint_t code_function_def = -1;
238 /* for temp allocation */
241 /* vararg support: */
242 size_t max_varargs = 0;
246 ir_value* ir_function_create_local(ir_function *self, const std::string& name, qc_type vtype, bool param);
247 bool GMQCC_WARN ir_function_finalize(ir_function*);
248 ir_block* ir_function_create_block(lex_ctx_t ctx, ir_function*, const char *label);
251 #define IR_HT_SIZE 1024
252 #define IR_MAX_VINSTR_TEMPS 1
255 void* operator new(std::size_t);
256 void operator delete(void*);
257 ir_builder(const std::string& modulename);
261 std::vector<std::unique_ptr<ir_function>> functions;
262 std::vector<std::unique_ptr<ir_value>> globals;
263 std::vector<std::unique_ptr<ir_value>> fields;
264 // for reusing them in vector-splits, TODO: sort this or use a radix-tree
265 std::vector<ir_value*> const_floats;
271 // extparams' ir_values reference the ones from extparam_protos
272 std::vector<std::unique_ptr<ir_value>> extparam_protos;
273 std::vector<ir_value*> extparams;
275 // the highest func->allocated_locals
276 size_t max_locals = 0;
277 size_t max_globaltemps = 0;
278 uint32_t first_common_local = 0;
279 uint32_t first_common_globaltemp = 0;
281 std::vector<const char*> filenames;
282 std::vector<qcint_t> filestrings;
284 // we cache the #IMMEDIATE string here
285 qcint_t str_immediate = 0;
287 // there should just be this one nil
289 ir_value *reserved_va_count = nullptr;
290 ir_value *coverage_func = nullptr;
292 /* some virtual instructions require temps, and their code is isolated
293 * so that we don't need to keep track of their liveness.
295 ir_value *vinstr_temp[IR_MAX_VINSTR_TEMPS];
298 std::unique_ptr<code_t> code;
301 ir_function* ir_builder_create_function(ir_builder*, const std::string& name, qc_type outtype);
302 ir_value* ir_builder_create_global(ir_builder*, const std::string& name, qc_type vtype);
303 ir_value* ir_builder_create_field(ir_builder*, const std::string& name, qc_type vtype);
304 ir_value* ir_builder_get_va_count(ir_builder*);
305 bool ir_builder_generate(ir_builder *self, const char *filename);
306 void ir_builder_dump(ir_builder*, int (*oprintf)(const char*, ...));
309 * This code assumes 32 bit floats while generating binary
310 * Blub: don't use extern here, it's annoying and shows up in nm
313 typedef int static_assert_is_32bit_float [(sizeof(int32_t) == 4) ? 1 : -1];
314 typedef int static_assert_is_32bit_integer[(sizeof(qcfloat_t) == 4) ? 1 : -1];
317 * If the condition creates a situation where this becomes -1 size it means there are
318 * more IR_FLAGs than the type ir_flag_t is capable of holding. So either eliminate
319 * the IR flag count or change the ir_flag_t typedef to a type large enough to accomodate
322 typedef int static_assert_is_ir_flag_safe [((IR_FLAG_LAST) <= (ir_flag_t)(-1)) ? 1 : -1];