]> git.xonotic.org Git - xonotic/gmqcc.git/blob - ir.h
temp committing major c++ification
[xonotic/gmqcc.git] / ir.h
1 #ifndef GMQCC_IR_HDR
2 #define GMQCC_IR_HDR
3 #include "gmqcc.h"
4
5 /*
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.
8  */
9 typedef uint8_t ir_flag_t;
10
11 struct ir_value;
12 struct ir_instr;
13 struct ir_block;
14 struct ir_function;
15 struct ir_builder;
16
17 struct ir_life_entry_t {
18     /* both inclusive */
19     size_t start;
20     size_t end;
21 };
22
23 enum {
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,
30
31     IR_FLAG_SPLIT_VECTOR            = 1 << 6,
32
33     IR_FLAG_LAST,
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)
36 };
37
38 struct ir_value {
39     ir_value(std::string&& name, store_type storetype, qc_type vtype);
40     ~ir_value();
41
42     void* operator new(std::size_t); // to use mem_a
43     void operator delete(void*); // to use mem_d
44
45     std::string name;
46
47     qc_type vtype;
48     store_type store;
49     lex_ctx_t context;
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
53     ir_flag_t flags;
54
55     std::vector<ir_instr *> reads;
56     std::vector<ir_instr *> writes;
57
58     // constant values
59     bool hasvalue;
60     union {
61         qcfloat_t    vfloat;
62         int          vint;
63         vec3_t       vvec;
64         int32_t      ivec[3];
65         char        *vstring;
66         ir_value    *vpointer;
67         ir_function *vfunc;
68     } constval;
69
70     struct {
71         int32_t globaladdr;
72         int32_t name;
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
76     } code;
77
78     // for accessing vectors
79     ir_value *members[3];
80     ir_value *memberof;
81
82     bool unique_life;      // arrays will never overlap with temps
83     bool locked;           // temps living during a CALL must be locked
84     bool callparam;
85
86     std::vector<ir_life_entry_t> life; // For the temp allocator
87 };
88
89 /*
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
93  */
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*,...));
102
103 /* PHI data */
104 struct ir_phi_entry_t {
105     ir_value *value;
106     ir_block *from;
107 };
108
109 /* instruction */
110 struct ir_instr {
111     int opcode;
112     lex_ctx_t context;
113     ir_value *(_ops[3]);
114     ir_block *(bops[2]);
115
116     std::vector<ir_phi_entry_t> phi;
117     std::vector<ir_value *> params;
118
119     // For the temp-allocation
120     size_t eid;
121
122     // For IFs
123     bool likely;
124
125     ir_block *owner;
126 };
127
128 /* block */
129 struct ir_block {
130     void* operator new(std::size_t);
131     void operator delete(void*);
132
133     ir_block(ir_function *owner, const std::string& name);
134     ~ir_block();
135
136     ir_function *owner;
137     std::string label;
138
139     lex_ctx_t context;
140     bool final = false; /* once a jump is added we're done */
141
142     ir_instr **instr = nullptr;
143     ir_block **entries = nullptr;
144     ir_block **exits = nullptr;
145     std::vector<ir_value *> living;
146
147     /* For the temp-allocation */
148     size_t entry_id  = 0;
149     size_t eid       = 0;
150     bool   is_return = false;
151
152     bool generated = false;
153     size_t code_start = 0;
154 };
155
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);
163
164 /* This is to create an instruction of the form
165  * <outtype>%label := opcode a, b
166  */
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*);
173
174 bool GMQCC_WARN ir_block_create_return(ir_block*, lex_ctx_t, ir_value *opt_value);
175
176 bool GMQCC_WARN ir_block_create_if(ir_block*, lex_ctx_t, ir_value *cond,
177                                    ir_block *ontrue, ir_block *onfalse);
178 /*
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.
184  */
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);
187
188 /* function */
189 struct ir_function {
190     void* operator new(std::size_t);
191     void operator delete(void*);
192
193     ir_function(ir_builder *owner, qc_type returntype);
194     ~ir_function();
195
196     ir_builder *owner;
197
198     std::string name;
199     qc_type     outtype;
200     int        *params  = nullptr;
201     ir_flag_t   flags   = 0;
202     int         builtin = 0;
203
204     std::vector<std::unique_ptr<ir_block>> blocks;
205
206     /*
207      * values generated from operations
208      * which might get optimized away, so anything
209      * in there needs to be deleted in the dtor.
210      */
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;
214
215     size_t allocated_locals = 0;
216     size_t globaltemps      = 0;
217
218     ir_block*  first = nullptr;
219     ir_block*  last  = nullptr;
220
221     lex_ctx_t  context;
222
223     /*
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
227      *
228      * remember the ID:
229      */
230     qcint_t code_function_def = -1;
231
232     /* for temp allocation */
233     size_t run_id = 0;
234
235     /* vararg support: */
236     size_t max_varargs = 0;
237 };
238
239
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);
243
244 /* builder */
245 #define IR_HT_SIZE          1024
246 #define IR_MAX_VINSTR_TEMPS 1
247
248 struct ir_builder {
249     void* operator new(std::size_t);
250     void operator delete(void*);
251     ir_builder(const std::string& modulename);
252     ~ir_builder();
253
254     std::string name;
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;
260
261     ht            htfunctions;
262     ht            htglobals;
263     ht            htfields;
264
265     std::vector<std::unique_ptr<ir_value>> extparams;
266     std::vector<std::unique_ptr<ir_value>> extparam_protos;
267
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;
273
274     std::vector<const char*> filenames;
275     std::vector<qcint_t>     filestrings;
276
277     // we cache the #IMMEDIATE string here
278     qcint_t      str_immediate = 0;
279
280     // there should just be this one nil
281     ir_value    *nil;
282     ir_value    *reserved_va_count = nullptr;
283     ir_value    *coverage_func = nullptr;
284
285     /* some virtual instructions require temps, and their code is isolated
286      * so that we don't need to keep track of their liveness.
287      */
288     ir_value    *vinstr_temp[IR_MAX_VINSTR_TEMPS];
289
290     /* code generator */
291     code_t      *code;
292 };
293
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*, ...));
300
301 /*
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
304  * for some reason :P
305  */
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];
308
309 /*
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
313  * all the flags.
314  */
315 typedef int static_assert_is_ir_flag_safe [((IR_FLAG_LAST) <= (ir_flag_t)(-1)) ? 1 : -1];
316
317 #endif