]> git.xonotic.org Git - xonotic/gmqcc.git/blob - ir.h
more c++ migration for ast/ir/code; reached a working condition here
[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     void* operator new(std::size_t);
112     void operator delete(void*);
113
114     ir_instr(lex_ctx_t, ir_block *owner, int opcode);
115     ~ir_instr();
116
117     int opcode;
118     lex_ctx_t context;
119     ir_value *(_ops[3]) = { nullptr, nullptr, nullptr };
120     ir_block *(bops[2]) = { nullptr, nullptr };
121
122     std::vector<ir_phi_entry_t> phi;
123     std::vector<ir_value *> params;
124
125     // For the temp-allocation
126     size_t eid = 0;
127
128     // For IFs
129     bool likely = true;
130
131     ir_block *owner;
132 };
133
134 /* block */
135 struct ir_block {
136     void* operator new(std::size_t);
137     void operator delete(void*);
138
139     ir_block(ir_function *owner, const std::string& name);
140     ~ir_block();
141
142     ir_function *owner;
143     std::string label;
144
145     lex_ctx_t context;
146     bool final = false; /* once a jump is added we're done */
147
148     ir_instr **instr = nullptr;
149     ir_block **entries = nullptr;
150     ir_block **exits = nullptr;
151     std::vector<ir_value *> living;
152
153     /* For the temp-allocation */
154     size_t entry_id  = 0;
155     size_t eid       = 0;
156     bool   is_return = false;
157
158     bool generated = false;
159     size_t code_start = 0;
160 };
161
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);
169
170 /* This is to create an instruction of the form
171  * <outtype>%label := opcode a, b
172  */
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*);
179
180 bool GMQCC_WARN ir_block_create_return(ir_block*, lex_ctx_t, ir_value *opt_value);
181
182 bool GMQCC_WARN ir_block_create_if(ir_block*, lex_ctx_t, ir_value *cond,
183                                    ir_block *ontrue, ir_block *onfalse);
184 /*
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.
190  */
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);
193
194 /* function */
195 struct ir_function {
196     void* operator new(std::size_t);
197     void operator delete(void*);
198
199     ir_function(ir_builder *owner, qc_type returntype);
200     ~ir_function();
201
202     ir_builder *owner;
203
204     std::string name;
205     qc_type     outtype;
206     int        *params  = nullptr;
207     ir_flag_t   flags   = 0;
208     int         builtin = 0;
209
210     std::vector<std::unique_ptr<ir_block>> blocks;
211
212     /*
213      * values generated from operations
214      * which might get optimized away, so anything
215      * in there needs to be deleted in the dtor.
216      */
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;
220
221     size_t allocated_locals = 0;
222     size_t globaltemps      = 0;
223
224     ir_block*  first = nullptr;
225     ir_block*  last  = nullptr;
226
227     lex_ctx_t  context;
228
229     /*
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
233      *
234      * remember the ID:
235      */
236     qcint_t code_function_def = -1;
237
238     /* for temp allocation */
239     size_t run_id = 0;
240
241     /* vararg support: */
242     size_t max_varargs = 0;
243 };
244
245
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);
249
250 /* builder */
251 #define IR_HT_SIZE          1024
252 #define IR_MAX_VINSTR_TEMPS 1
253
254 struct ir_builder {
255     void* operator new(std::size_t);
256     void operator delete(void*);
257     ir_builder(const std::string& modulename);
258     ~ir_builder();
259
260     std::string name;
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;
266
267     ht            htfunctions;
268     ht            htglobals;
269     ht            htfields;
270
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;
274
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;
280
281     std::vector<const char*> filenames;
282     std::vector<qcint_t>     filestrings;
283
284     // we cache the #IMMEDIATE string here
285     qcint_t      str_immediate = 0;
286
287     // there should just be this one nil
288     ir_value    *nil;
289     ir_value    *reserved_va_count = nullptr;
290     ir_value    *coverage_func = nullptr;
291
292     /* some virtual instructions require temps, and their code is isolated
293      * so that we don't need to keep track of their liveness.
294      */
295     ir_value    *vinstr_temp[IR_MAX_VINSTR_TEMPS];
296
297     /* code generator */
298     std::unique_ptr<code_t> code;
299 };
300
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*, ...));
307
308 /*
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
311  * for some reason :P
312  */
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];
315
316 /*
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
320  * all the flags.
321  */
322 typedef int static_assert_is_ir_flag_safe [((IR_FLAG_LAST) <= (ir_flag_t)(-1)) ? 1 : -1];
323
324 #endif