5 /***********************************************************************
9 ir_builder* ir_builder_new(const char *modulename)
13 self = (ir_builder*)mem_a(sizeof(*self));
14 MEM_VECTOR_INIT(self, functions);
15 MEM_VECTOR_INIT(self, globals);
17 ir_builder_set_name(self, modulename);
19 /* globals which always exist */
21 /* for now we give it a vector size */
22 ir_builder_create_global(self, "OFS_RETURN", qc_variant);
27 MEM_VEC_FUNCTIONS(ir_builder, ir_value*, globals)
28 MEM_VECTOR_FUNCTIONS(ir_builder, ir_function*, functions)
30 void ir_builder_delete(ir_builder* self)
33 mem_d((void*)self->name);
34 for (i = 0; i != self->functions_count; ++i) {
35 ir_function_delete(self->functions[i]);
37 MEM_VECTOR_CLEAR(self, functions);
38 for (i = 0; i != self->globals_count; ++i) {
39 ir_value_delete(self->globals[i]);
41 MEM_VECTOR_CLEAR(self, globals);
45 void ir_builder_set_name(ir_builder *self, const char *name)
48 mem_d((void*)self->name);
49 self->name = util_strdup(name);
52 ir_function* ir_builder_get_function(ir_builder *self, const char *name)
55 for (i = 0; i < self->functions_count; ++i) {
56 if (!strcmp(name, self->functions[i]->name))
57 return self->functions[i];
62 ir_function* ir_builder_create_function(ir_builder *self, const char *name)
64 ir_function *fn = ir_builder_get_function(self, name);
69 fn = ir_function_new(self);
70 ir_function_set_name(fn, name);
71 ir_builder_functions_add(self, fn);
75 ir_value* ir_builder_get_global(ir_builder *self, const char *name)
78 for (i = 0; i < self->globals_count; ++i) {
79 if (!strcmp(self->globals[i]->name, name))
80 return self->globals[i];
85 ir_value* ir_builder_create_global(ir_builder *self, const char *name, int vtype)
87 ir_value *ve = ir_builder_get_global(self, name);
92 ve = ir_value_var(name, qc_global, vtype);
93 ir_builder_globals_add(self, ve);
97 /***********************************************************************
101 void ir_function_naive_phi(ir_function*);
102 void ir_function_enumerate(ir_function*);
103 void ir_function_calculate_liferanges(ir_function*);
105 ir_function* ir_function_new(ir_builder* owner)
108 self = (ir_function*)mem_a(sizeof(*self));
110 self->context.file = "<@no context>";
111 self->context.line = 0;
112 self->retype = qc_void;
113 MEM_VECTOR_INIT(self, params);
114 MEM_VECTOR_INIT(self, blocks);
115 MEM_VECTOR_INIT(self, values);
116 MEM_VECTOR_INIT(self, locals);
117 ir_function_set_name(self, "<@unnamed>");
122 MEM_VECTOR_FUNCTIONS(ir_function, ir_value*, values)
123 MEM_VECTOR_FUNCTIONS(ir_function, ir_block*, blocks)
124 MEM_VECTOR_FUNCTIONS(ir_function, ir_value*, locals)
126 void ir_function_set_name(ir_function *self, const char *name)
129 mem_d((void*)self->name);
130 self->name = util_strdup(name);
133 void ir_function_delete(ir_function *self)
136 mem_d((void*)self->name);
138 for (i = 0; i != self->blocks_count; ++i)
139 ir_block_delete(self->blocks[i]);
140 MEM_VECTOR_CLEAR(self, blocks);
142 MEM_VECTOR_CLEAR(self, params);
144 for (i = 0; i != self->values_count; ++i)
145 ir_value_delete(self->values[i]);
146 MEM_VECTOR_CLEAR(self, values);
148 for (i = 0; i != self->locals_count; ++i)
149 ir_value_delete(self->locals[i]);
150 MEM_VECTOR_CLEAR(self, locals);
155 void ir_function_collect_value(ir_function *self, ir_value *v)
157 ir_function_values_add(self, v);
160 ir_block* ir_function_create_block(ir_function *self, const char *label)
162 ir_block* bn = ir_block_new(self, label);
163 memcpy(&bn->context, &self->context, sizeof(self->context));
164 ir_function_blocks_add(self, bn);
168 void ir_function_finalize(ir_function *self)
170 ir_function_naive_phi(self);
171 ir_function_enumerate(self);
172 ir_function_calculate_liferanges(self);
175 ir_value* ir_function_get_local(ir_function *self, const char *name)
178 for (i = 0; i < self->locals_count; ++i) {
179 if (!strcmp(self->locals[i]->name, name))
180 return self->locals[i];
185 ir_value* ir_function_create_local(ir_function *self, const char *name, int vtype)
187 ir_value *ve = ir_function_get_local(self, name);
192 ve = ir_value_var(name, qc_localvar, vtype);
193 ir_function_locals_add(self, ve);
197 /***********************************************************************
201 ir_block* ir_block_new(ir_function* owner, const char *name)
204 self = (ir_block*)mem_a(sizeof(*self));
206 self->context.file = "<@no context>";
207 self->context.line = 0;
209 MEM_VECTOR_INIT(self, instr);
210 MEM_VECTOR_INIT(self, entries);
211 MEM_VECTOR_INIT(self, exits);
213 ir_block_set_label(self, name);
216 self->is_return = false;
218 MEM_VECTOR_INIT(self, living);
221 MEM_VECTOR_FUNCTIONS(ir_block, ir_instr*, instr)
222 MEM_VECTOR_FUNCTIONS_ALL(ir_block, ir_block*, entries)
223 MEM_VECTOR_FUNCTIONS_ALL(ir_block, ir_block*, exits)
224 MEM_VECTOR_FUNCTIONS_ALL(ir_block, ir_value*, living)
226 void ir_block_delete(ir_block* self)
229 mem_d((void*)self->label);
230 for (i = 0; i != self->instr_count; ++i)
231 ir_instr_delete(self->instr[i]);
232 MEM_VECTOR_CLEAR(self, instr);
233 MEM_VECTOR_CLEAR(self, entries);
234 MEM_VECTOR_CLEAR(self, exits);
235 MEM_VECTOR_CLEAR(self, living);
239 void ir_block_set_label(ir_block *self, const char *name)
242 mem_d((void*)self->label);
243 self->label = util_strdup(name);
246 /***********************************************************************
250 ir_instr* ir_instr_new(ir_block* owner, int op)
253 self = (ir_instr*)mem_a(sizeof(*self));
255 self->context.file = "<@no context>";
256 self->context.line = 0;
258 self->_ops[0] = NULL;
259 self->_ops[1] = NULL;
260 self->_ops[2] = NULL;
261 self->bops[0] = NULL;
262 self->bops[1] = NULL;
263 MEM_VECTOR_INIT(self, phi);
268 MEM_VECTOR_FUNCTIONS(ir_instr, ir_phi_entry_t, phi)
270 void ir_instr_delete(ir_instr *self)
272 ir_instr_op(self, 0, NULL, false);
273 ir_instr_op(self, 1, NULL, false);
274 ir_instr_op(self, 2, NULL, false);
275 MEM_VECTOR_CLEAR(self, phi);
279 void ir_instr_op(ir_instr *self, int op, ir_value *v, qbool writing)
281 if (self->_ops[op]) {
283 ir_value_writes_add(self->_ops[op], self);
285 ir_value_reads_add(self->_ops[op], self);
289 ir_value_writes_add(v, self);
291 ir_value_reads_add(v, self);
296 /***********************************************************************
300 ir_value* ir_value_var(const char *name, int storetype, int vtype)
303 self = (ir_value*)mem_a(sizeof(*self));
305 self->store = storetype;
306 MEM_VECTOR_INIT(self, reads);
307 MEM_VECTOR_INIT(self, writes);
308 self->has_constval = false;
309 self->context.file = "<@no context>";
310 self->context.line = 0;
312 ir_value_set_name(self, name);
314 MEM_VECTOR_INIT(self, life);
317 MEM_VECTOR_FUNCTIONS(ir_value, ir_life_entry_t, life)
318 MEM_VECTOR_FUNCTIONS(ir_value, ir_instr*, reads)
319 MEM_VECTOR_FUNCTIONS(ir_value, ir_instr*, writes)
321 ir_value* ir_value_out(ir_function *owner, const char *name, int storetype, int vtype)
323 ir_value *v = ir_value_var(name, storetype, vtype);
324 ir_function_collect_value(owner, v);
328 void ir_value_delete(ir_value* self)
330 mem_d((void*)self->name);
331 if (self->has_constval)
333 if (self->vtype == qc_string)
334 mem_d((void*)self->cvalue.vstring);
336 MEM_VECTOR_CLEAR(self, reads);
337 MEM_VECTOR_CLEAR(self, writes);
338 MEM_VECTOR_CLEAR(self, life);
342 void ir_value_set_name(ir_value *self, const char *name)
345 mem_d((void*)self->name);
346 self->name = util_strdup(name);
349 qbool ir_value_set_float(ir_value *self, float f)
351 if (self->vtype != qc_float)
353 self->cvalue.vfloat = f;
354 self->has_constval = true;
358 qbool ir_value_set_vector(ir_value *self, qc_vec_t v)
360 if (self->vtype != qc_vector)
362 self->cvalue.vvec = v;
363 self->has_constval = true;
367 qbool ir_value_set_string(ir_value *self, const char *str)
369 if (self->vtype != qc_string)
371 self->cvalue.vstring = util_strdup(str);
372 self->has_constval = true;
376 qbool ir_value_set_int(ir_value *self, int i)
378 if (self->vtype != qc_int)
380 self->cvalue.vint = i;
381 self->has_constval = true;
385 qbool ir_value_lives(ir_value *self, size_t at)
388 for (i = 0; i < self->life_count; ++i)
390 ir_life_entry_t *life = &self->life[i];
391 if (life->start <= at && at <= life->end)
393 if (life->start > at) /* since it's ordered */
399 void ir_value_life_insert(ir_value *self, size_t idx, ir_life_entry_t e)
402 ir_value_life_add(self, e); /* naive... */
403 for (k = self->life_count-1; k > idx; --k)
404 self->life[k] = self->life[k-1];
408 qbool ir_value_life_merge(ir_value *self, size_t s)
411 ir_life_entry_t *life = NULL;
412 ir_life_entry_t *before = NULL;
413 ir_life_entry_t new_entry;
415 /* Find the first range >= s */
416 for (i = 0; i < self->life_count; ++i)
419 life = &self->life[i];
423 /* nothing found? append */
424 if (i == self->life_count) {
425 if (life && life->end+1 == s)
427 /* previous life range can be merged in */
431 if (life && life->end >= s)
435 ir_value_life_add(self, e);
441 if (before->end + 1 == s &&
442 life->start - 1 == s)
445 before->end = life->end;
446 ir_value_life_remove(self, i);
449 if (before->end + 1 == s)
455 /* already contained */
456 if (before->end >= s)
460 if (life->start - 1 == s)
465 /* insert a new entry */
466 new_entry.start = new_entry.end = s;
467 ir_value_life_insert(self, i, new_entry);
471 /***********************************************************************
475 qbool ir_block_create_store_op(ir_block *self, int op, ir_value *target, ir_value *what)
477 if (target->store == qc_localval) {
478 fprintf(stderr, "cannot store to an SSA value\n");
481 ir_instr *in = ir_instr_new(self, op);
482 ir_instr_op(in, 0, target, true);
483 ir_instr_op(in, 1, what, false);
484 ir_block_instr_add(self, in);
489 qbool ir_block_create_store(ir_block *self, ir_value *target, ir_value *what)
493 if (target->vtype == qc_variant)
496 vtype = target->vtype;
500 if (what->vtype == qc_int)
501 op = INSTR_CONV_ITOF;
509 op = INSTR_STORE_ENT;
515 if (what->vtype == qc_int)
516 op = INSTR_CONV_FTOI;
524 return ir_block_create_store_op(self, op, target, what);
527 void ir_block_create_return(ir_block *self, ir_value *v)
531 fprintf(stderr, "block already ended (%s)\n", self->_label);
535 self->is_return = true;
536 in = ir_instr_new(self, INSTR_RETURN);
537 ir_instr_op(in, 0, v, false);
538 ir_block_instr_add(self, in);
541 void ir_block_create_if(ir_block *self, ir_value *v,
542 ir_block *ontrue, ir_block *onfalse)
546 fprintf(stderr, "block already ended (%s)\n", self->_label);
550 //in = ir_instr_new(self, (v->vtype == qc_string ? INSTR_IF_S : INSTR_IF_F));
551 in = ir_instr_new(self, VINSTR_COND);
552 ir_instr_op(in, 0, v, false);
553 in->bops[0] = ontrue;
554 in->bops[1] = onfalse;
555 ir_block_instr_add(self, in);
557 ir_block_exits_add(self, ontrue);
558 ir_block_exits_add(self, onfalse);
559 ir_block_entries_add(ontrue, self);
560 ir_block_entries_add(onfalse, self);
563 void ir_block_create_jump(ir_block *self, ir_block *to)
567 fprintf(stderr, "block already ended (%s)\n", self->_label);
571 in = ir_instr_new(self, VINSTR_JUMP);
573 ir_block_instr_add(self, in);
575 ir_block_exits_add(self, to);
576 ir_block_entries_add(to, self);
579 void ir_block_create_goto(ir_block *self, ir_block *to)
583 fprintf(stderr, "block already ended (%s)\n", self->_label);
587 in = ir_instr_new(self, INSTR_GOTO);
589 ir_block_instr_add(self, in);
591 ir_block_exits_add(self, to);
592 ir_block_entries_add(to, self);
595 ir_instr* ir_block_create_phi(ir_block *self, const char *label, int ot)
599 in = ir_instr_new(self, VINSTR_PHI);
600 out = ir_value_out(self->owner, label, qc_localval, ot);
601 ir_instr_op(in, 0, out, true);
602 ir_block_instr_add(self, in);
606 ir_value* ir_phi_value(ir_instr *self)
608 return self->_ops[0];
611 void ir_phi_add(ir_instr* self, ir_block *b, ir_value *v)
615 if (!ir_block_entries_find(self->owner, b, NULL)) {
616 /* Must not be possible to cause this, otherwise the AST
617 * is doing something wrong.
619 fprintf(stderr, "Invalid entry block for PHI\n");
625 ir_value_reads_add(v, self);
626 ir_instr_phi_add(self, pe);
629 /* binary op related code */
631 ir_value* ir_block_create_binop(ir_block *self,
632 const char *label, int opcode,
633 ir_value *left, ir_value *right)
652 case INSTR_SUB_S: /* -- offset of string as float */
659 case INSTR_BITAND_FI:
660 case INSTR_BITAND_IF:
694 /* boolean operations result in floats */
695 if (opcode >= INSTR_EQ_F && opcode <= INSTR_GT)
697 else if (opcode >= INSTR_LE && opcode <= INSTR_GT)
699 else if (opcode >= INSTR_LE_I && opcode <= INSTR_EQ_FI)
704 fprintf(stderr, "binop %i (%s)\n", opcode, qc_opname(opcode));
708 ir_value *out = ir_value_out(self->owner, label, qc_localval, ot);
709 ir_instr *in = ir_instr_new(self, opcode);
710 ir_instr_op(in, 0, out, true);
711 ir_instr_op(in, 1, left, false);
712 ir_instr_op(in, 2, right, false);
713 ir_block_instr_add(self, in);
717 ir_value* ir_block_create_add(ir_block *self,
719 ir_value *left, ir_value *right)
723 int r = right->vtype;
739 if ( (l == qc_float && r == qc_int) )
741 else if ( (l == qc_int && r == qc_float) )
746 return ir_block_create_binop(self, label, op, left, right);
749 ir_value* ir_block_create_sub(ir_block *self,
751 ir_value *left, ir_value *right)
755 int r = right->vtype;
772 if ( (l == qc_float && r == qc_int) )
774 else if ( (l == qc_int && r == qc_float) )
779 return ir_block_create_binop(self, label, op, left, right);
782 ir_value* ir_block_create_mul(ir_block *self,
784 ir_value *left, ir_value *right)
788 int r = right->vtype;
805 if ( (l == qc_float && r == qc_int) )
807 else if ( (l == qc_int && r == qc_float) )
809 else if ( (l == qc_vector && r == qc_float) )
811 else if ( (l == qc_float && r == qc_vector) )
813 else if ( (l == qc_vector && r == qc_int) )
815 else if ( (l == qc_int && r == qc_vector) )
820 return ir_block_create_binop(self, label, op, left, right);
823 ir_value* ir_block_create_div(ir_block *self,
825 ir_value *left, ir_value *right)
829 int r = right->vtype;
843 if ( (l == qc_float && r == qc_int) )
845 else if ( (l == qc_int && r == qc_float) )
847 else if ( (l == qc_vector && r == qc_float) )
852 return ir_block_create_binop(self, label, op, left, right);
855 /* PHI resolving breaks the SSA, and must thus be the last
856 * step before life-range calculation.
859 static void ir_block_naive_phi(ir_block *self);
860 void ir_function_naive_phi(ir_function *self)
864 for (i = 0; i < self->blocks_count; ++i)
865 ir_block_naive_phi(self->blocks[i]);
868 static void ir_naive_phi_emit_store(ir_block *block, size_t iid, ir_value *old, ir_value *what)
874 ir_block_create_store(block, old, what);
876 /* we now move it up */
877 instr = block->instr[block->instr_count-1];
878 for (i = block->instr_count; i > iid; --i)
879 block->instr[i] = block->instr[i-1];
880 block->instr[i] = instr;
883 static void ir_block_naive_phi(ir_block *self)
886 /* FIXME: optionally, create_phi can add the phis
887 * to a list so we don't need to loop through blocks
888 * - anyway: "don't optimize YET"
890 for (i = 0; i < self->instr_count; ++i)
892 ir_instr *instr = self->instr[i];
893 if (instr->opcode != VINSTR_PHI)
896 ir_block_instr_remove(self, i);
897 --i; /* NOTE: i+1 below */
899 for (p = 0; p < instr->phi_count; ++p)
901 ir_value *v = instr->phi[p].value;
902 for (w = 0; w < v->writes_count; ++w) {
905 if (!v->writes[w]->_ops[0])
908 /* When the write was to a global, we have to emit a mov */
909 old = v->writes[w]->_ops[0];
911 /* The original instruction now writes to the PHI target local */
912 if (v->writes[w]->_ops[0] == v)
913 v->writes[w]->_ops[0] = instr->_ops[0];
915 if (old->store != qc_localval)
917 /* If it originally wrote to a global we need to store the value
920 ir_naive_phi_emit_store(self, i+1, old, v);
921 if (i+1 < self->instr_count)
922 instr = self->instr[i+1];
925 /* In case I forget and access instr later, it'll be NULL
926 * when it's a problem, to make sure we crash, rather than accessing
932 /* If it didn't, we can replace all reads by the phi target now. */
934 for (r = 0; r < old->reads_count; ++r)
937 ir_instr *ri = old->reads[r];
938 for (op = 0; op < ri->phi_count; ++op) {
939 if (ri->phi[op].value == old)
940 ri->phi[op].value = v;
942 for (op = 0; op < 3; ++op) {
943 if (ri->_ops[op] == old)
950 ir_instr_delete(instr);