5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is furnished to do
10 * so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 /***********************************************************************
29 * Type sizes used at multiple points in the IR codegen
32 size_t type_sizeof[TYPE_COUNT] = {
39 1, /* TYPE_FUNCTION */
47 /***********************************************************************
51 ir_builder* ir_builder_new(const char *modulename)
55 self = (ir_builder*)mem_a(sizeof(*self));
59 MEM_VECTOR_INIT(self, functions);
60 MEM_VECTOR_INIT(self, globals);
62 if (!ir_builder_set_name(self, modulename)) {
67 /* globals which always exist */
69 /* for now we give it a vector size */
70 ir_builder_create_global(self, "OFS_RETURN", TYPE_VARIANT);
75 MEM_VEC_FUNCTIONS(ir_builder, ir_value*, globals)
76 MEM_VEC_FUNCTIONS(ir_builder, ir_function*, functions)
78 void ir_builder_delete(ir_builder* self)
81 mem_d((void*)self->name);
82 for (i = 0; i != self->functions_count; ++i) {
83 ir_function_delete(self->functions[i]);
85 MEM_VECTOR_CLEAR(self, functions);
86 for (i = 0; i != self->globals_count; ++i) {
87 ir_value_delete(self->globals[i]);
89 MEM_VECTOR_CLEAR(self, globals);
93 bool ir_builder_set_name(ir_builder *self, const char *name)
96 mem_d((void*)self->name);
97 self->name = util_strdup(name);
101 ir_function* ir_builder_get_function(ir_builder *self, const char *name)
104 for (i = 0; i < self->functions_count; ++i) {
105 if (!strcmp(name, self->functions[i]->name))
106 return self->functions[i];
111 ir_function* ir_builder_create_function(ir_builder *self, const char *name)
113 ir_function *fn = ir_builder_get_function(self, name);
118 fn = ir_function_new(self);
119 if (!ir_function_set_name(fn, name) ||
120 !ir_builder_functions_add(self, fn) )
122 ir_function_delete(fn);
128 ir_value* ir_builder_get_global(ir_builder *self, const char *name)
131 for (i = 0; i < self->globals_count; ++i) {
132 if (!strcmp(self->globals[i]->name, name))
133 return self->globals[i];
138 ir_value* ir_builder_create_global(ir_builder *self, const char *name, int vtype)
140 ir_value *ve = ir_builder_get_global(self, name);
145 ve = ir_value_var(name, store_global, vtype);
146 if (!ir_builder_globals_add(self, ve)) {
153 /***********************************************************************
157 bool ir_function_naive_phi(ir_function*);
158 void ir_function_enumerate(ir_function*);
159 bool ir_function_calculate_liferanges(ir_function*);
160 bool ir_function_allocate_locals(ir_function*);
162 ir_function* ir_function_new(ir_builder* owner)
165 self = (ir_function*)mem_a(sizeof(*self));
171 if (!ir_function_set_name(self, "<@unnamed>")) {
176 self->context.file = "<@no context>";
177 self->context.line = 0;
178 self->retype = TYPE_VOID;
179 MEM_VECTOR_INIT(self, params);
180 MEM_VECTOR_INIT(self, blocks);
181 MEM_VECTOR_INIT(self, values);
182 MEM_VECTOR_INIT(self, locals);
187 MEM_VEC_FUNCTIONS(ir_function, ir_value*, values)
188 MEM_VEC_FUNCTIONS(ir_function, ir_block*, blocks)
189 MEM_VEC_FUNCTIONS(ir_function, ir_value*, locals)
191 bool ir_function_set_name(ir_function *self, const char *name)
194 mem_d((void*)self->name);
195 self->name = util_strdup(name);
199 void ir_function_delete(ir_function *self)
202 mem_d((void*)self->name);
204 for (i = 0; i != self->blocks_count; ++i)
205 ir_block_delete(self->blocks[i]);
206 MEM_VECTOR_CLEAR(self, blocks);
208 MEM_VECTOR_CLEAR(self, params);
210 for (i = 0; i != self->values_count; ++i)
211 ir_value_delete(self->values[i]);
212 MEM_VECTOR_CLEAR(self, values);
214 for (i = 0; i != self->locals_count; ++i)
215 ir_value_delete(self->locals[i]);
216 MEM_VECTOR_CLEAR(self, locals);
221 bool GMQCC_WARN ir_function_collect_value(ir_function *self, ir_value *v)
223 return ir_function_values_add(self, v);
226 ir_block* ir_function_create_block(ir_function *self, const char *label)
228 ir_block* bn = ir_block_new(self, label);
229 memcpy(&bn->context, &self->context, sizeof(self->context));
230 if (!ir_function_blocks_add(self, bn)) {
237 bool ir_function_finalize(ir_function *self)
239 if (!ir_function_naive_phi(self))
242 ir_function_enumerate(self);
244 if (!ir_function_calculate_liferanges(self))
247 if (!ir_function_allocate_locals(self))
252 ir_value* ir_function_get_local(ir_function *self, const char *name)
255 for (i = 0; i < self->locals_count; ++i) {
256 if (!strcmp(self->locals[i]->name, name))
257 return self->locals[i];
262 ir_value* ir_function_create_local(ir_function *self, const char *name, int vtype)
264 ir_value *ve = ir_function_get_local(self, name);
269 ve = ir_value_var(name, store_local, vtype);
270 if (!ir_function_locals_add(self, ve)) {
277 /***********************************************************************
281 ir_block* ir_block_new(ir_function* owner, const char *name)
284 self = (ir_block*)mem_a(sizeof(*self));
288 memset(self, 0, sizeof(*self));
291 if (!ir_block_set_label(self, name)) {
296 self->context.file = "<@no context>";
297 self->context.line = 0;
299 MEM_VECTOR_INIT(self, instr);
300 MEM_VECTOR_INIT(self, entries);
301 MEM_VECTOR_INIT(self, exits);
304 self->is_return = false;
306 MEM_VECTOR_INIT(self, living);
308 self->generated = false;
312 MEM_VEC_FUNCTIONS(ir_block, ir_instr*, instr)
313 MEM_VEC_FUNCTIONS_ALL(ir_block, ir_block*, entries)
314 MEM_VEC_FUNCTIONS_ALL(ir_block, ir_block*, exits)
315 MEM_VEC_FUNCTIONS_ALL(ir_block, ir_value*, living)
317 void ir_block_delete(ir_block* self)
321 for (i = 0; i != self->instr_count; ++i)
322 ir_instr_delete(self->instr[i]);
323 MEM_VECTOR_CLEAR(self, instr);
324 MEM_VECTOR_CLEAR(self, entries);
325 MEM_VECTOR_CLEAR(self, exits);
326 MEM_VECTOR_CLEAR(self, living);
330 bool ir_block_set_label(ir_block *self, const char *name)
333 mem_d((void*)self->label);
334 self->label = util_strdup(name);
335 return !!self->label;
338 /***********************************************************************
342 ir_instr* ir_instr_new(ir_block* owner, int op)
345 self = (ir_instr*)mem_a(sizeof(*self));
350 self->context.file = "<@no context>";
351 self->context.line = 0;
353 self->_ops[0] = NULL;
354 self->_ops[1] = NULL;
355 self->_ops[2] = NULL;
356 self->bops[0] = NULL;
357 self->bops[1] = NULL;
358 MEM_VECTOR_INIT(self, phi);
363 MEM_VEC_FUNCTIONS(ir_instr, ir_phi_entry_t, phi)
365 void ir_instr_delete(ir_instr *self)
368 /* The following calls can only delete from
369 * vectors, we still want to delete this instruction
370 * so ignore the return value. Since with the warn_unused_result attribute
371 * gcc doesn't care about an explicit: (void)foo(); to ignore the result,
372 * I have to improvise here and use if(foo());
374 for (i = 0; i < self->phi_count; ++i) {
376 if (ir_value_writes_find(self->phi[i].value, self, &idx))
377 if (ir_value_writes_remove(self->phi[i].value, idx)) GMQCC_SUPRESS_EMPTY_BODY;
378 if (ir_value_reads_find(self->phi[i].value, self, &idx))
379 if (ir_value_reads_remove (self->phi[i].value, idx)) GMQCC_SUPRESS_EMPTY_BODY;
381 MEM_VECTOR_CLEAR(self, phi);
382 if (ir_instr_op(self, 0, NULL, false)) GMQCC_SUPRESS_EMPTY_BODY;
383 if (ir_instr_op(self, 1, NULL, false)) GMQCC_SUPRESS_EMPTY_BODY;
384 if (ir_instr_op(self, 2, NULL, false)) GMQCC_SUPRESS_EMPTY_BODY;
388 bool ir_instr_op(ir_instr *self, int op, ir_value *v, bool writing)
390 if (self->_ops[op]) {
392 if (writing && ir_value_writes_find(self->_ops[op], self, &idx))
394 if (!ir_value_writes_remove(self->_ops[op], idx))
397 else if (ir_value_reads_find(self->_ops[op], self, &idx))
399 if (!ir_value_reads_remove(self->_ops[op], idx))
405 if (!ir_value_writes_add(v, self))
408 if (!ir_value_reads_add(v, self))
416 /***********************************************************************
420 ir_value* ir_value_var(const char *name, int storetype, int vtype)
423 self = (ir_value*)mem_a(sizeof(*self));
425 self->fieldtype = TYPE_VOID;
426 self->store = storetype;
427 MEM_VECTOR_INIT(self, reads);
428 MEM_VECTOR_INIT(self, writes);
429 self->isconst = false;
430 self->context.file = "<@no context>";
431 self->context.line = 0;
433 ir_value_set_name(self, name);
435 memset(&self->constval, 0, sizeof(self->constval));
436 memset(&self->code, 0, sizeof(self->code));
438 MEM_VECTOR_INIT(self, life);
441 MEM_VEC_FUNCTIONS(ir_value, ir_life_entry_t, life)
442 MEM_VEC_FUNCTIONS_ALL(ir_value, ir_instr*, reads)
443 MEM_VEC_FUNCTIONS_ALL(ir_value, ir_instr*, writes)
445 ir_value* ir_value_out(ir_function *owner, const char *name, int storetype, int vtype)
447 ir_value *v = ir_value_var(name, storetype, vtype);
450 if (!ir_function_collect_value(owner, v))
458 void ir_value_delete(ir_value* self)
461 mem_d((void*)self->name);
464 if (self->vtype == TYPE_STRING)
465 mem_d((void*)self->constval.vstring);
467 MEM_VECTOR_CLEAR(self, reads);
468 MEM_VECTOR_CLEAR(self, writes);
469 MEM_VECTOR_CLEAR(self, life);
473 void ir_value_set_name(ir_value *self, const char *name)
476 mem_d((void*)self->name);
477 self->name = util_strdup(name);
480 bool ir_value_set_float(ir_value *self, float f)
482 if (self->vtype != TYPE_FLOAT)
484 self->constval.vfloat = f;
485 self->isconst = true;
489 bool ir_value_set_vector(ir_value *self, vector v)
491 if (self->vtype != TYPE_VECTOR)
493 self->constval.vvec = v;
494 self->isconst = true;
498 bool ir_value_set_string(ir_value *self, const char *str)
500 if (self->vtype != TYPE_STRING)
502 self->constval.vstring = util_strdup(str);
503 self->isconst = true;
508 bool ir_value_set_int(ir_value *self, int i)
510 if (self->vtype != TYPE_INTEGER)
512 self->constval.vint = i;
513 self->isconst = true;
518 bool ir_value_lives(ir_value *self, size_t at)
521 for (i = 0; i < self->life_count; ++i)
523 ir_life_entry_t *life = &self->life[i];
524 if (life->start <= at && at <= life->end)
526 if (life->start > at) /* since it's ordered */
532 bool ir_value_life_insert(ir_value *self, size_t idx, ir_life_entry_t e)
535 if (!ir_value_life_add(self, e)) /* naive... */
537 for (k = self->life_count-1; k > idx; --k)
538 self->life[k] = self->life[k-1];
543 bool ir_value_life_merge(ir_value *self, size_t s)
546 ir_life_entry_t *life = NULL;
547 ir_life_entry_t *before = NULL;
548 ir_life_entry_t new_entry;
550 /* Find the first range >= s */
551 for (i = 0; i < self->life_count; ++i)
554 life = &self->life[i];
558 /* nothing found? append */
559 if (i == self->life_count) {
561 if (life && life->end+1 == s)
563 /* previous life range can be merged in */
567 if (life && life->end >= s)
570 if (!ir_value_life_add(self, e))
571 return false; /* failing */
577 if (before->end + 1 == s &&
578 life->start - 1 == s)
581 before->end = life->end;
582 if (!ir_value_life_remove(self, i))
583 return false; /* failing */
586 if (before->end + 1 == s)
592 /* already contained */
593 if (before->end >= s)
597 if (life->start - 1 == s)
602 /* insert a new entry */
603 new_entry.start = new_entry.end = s;
604 return ir_value_life_insert(self, i, new_entry);
607 bool ir_value_life_merge_into(ir_value *self, const ir_value *other)
611 if (!other->life_count)
614 if (!self->life_count) {
615 for (i = 0; i < other->life_count; ++i) {
616 if (!ir_value_life_add(self, other->life[i]))
623 for (i = 0; i < other->life_count; ++i)
625 const ir_life_entry_t *life = &other->life[i];
628 ir_life_entry_t *entry = &self->life[myi];
630 if (life->end+1 < entry->start)
632 /* adding an interval before entry */
633 if (!ir_value_life_insert(self, myi, *life))
639 if (life->start < entry->start &&
640 life->end >= entry->start)
642 /* starts earlier and overlaps */
643 entry->start = life->start;
646 if (life->end > entry->end &&
647 life->start-1 <= entry->end)
649 /* ends later and overlaps */
650 entry->end = life->end;
653 /* see if our change combines it with the next ranges */
654 while (myi+1 < self->life_count &&
655 entry->end+1 >= self->life[1+myi].start)
657 /* overlaps with (myi+1) */
658 if (entry->end < self->life[1+myi].end)
659 entry->end = self->life[1+myi].end;
660 if (!ir_value_life_remove(self, myi+1))
662 entry = &self->life[myi];
665 /* see if we're after the entry */
666 if (life->start > entry->end)
669 /* append if we're at the end */
670 if (myi >= self->life_count) {
671 if (!ir_value_life_add(self, *life))
675 /* otherweise check the next range */
684 bool ir_values_overlap(const ir_value *a, const ir_value *b)
686 /* For any life entry in A see if it overlaps with
687 * any life entry in B.
688 * Note that the life entries are orderes, so we can make a
689 * more efficient algorithm there than naively translating the
693 ir_life_entry_t *la, *lb, *enda, *endb;
695 /* first of all, if either has no life range, they cannot clash */
696 if (!a->life_count || !b->life_count)
701 enda = la + a->life_count;
702 endb = lb + b->life_count;
705 /* check if the entries overlap, for that,
706 * both must start before the other one ends.
708 #if defined(LIFE_RANGE_WITHOUT_LAST_READ)
709 if (la->start <= lb->end &&
710 lb->start <= la->end)
712 if (la->start < lb->end &&
719 /* entries are ordered
720 * one entry is earlier than the other
721 * that earlier entry will be moved forward
723 if (la->start < lb->start)
725 /* order: A B, move A forward
726 * check if we hit the end with A
731 else if (lb->start < la->start)
733 /* order: B A, move B forward
734 * check if we hit the end with B
743 /***********************************************************************
747 bool ir_block_create_store_op(ir_block *self, int op, ir_value *target, ir_value *what)
749 if (target->store == store_value) {
750 fprintf(stderr, "cannot store to an SSA value\n");
751 fprintf(stderr, "trying to store: %s <- %s\n", target->name, what->name);
754 ir_instr *in = ir_instr_new(self, op);
757 if (!ir_instr_op(in, 0, target, true) ||
758 !ir_instr_op(in, 1, what, false) ||
759 !ir_block_instr_add(self, in) )
767 bool ir_block_create_store(ir_block *self, ir_value *target, ir_value *what)
771 if (target->vtype == TYPE_VARIANT)
774 vtype = target->vtype;
779 if (what->vtype == TYPE_INTEGER)
780 op = INSTR_CONV_ITOF;
789 op = INSTR_STORE_ENT;
795 op = INSTR_STORE_FLD;
799 if (what->vtype == TYPE_INTEGER)
800 op = INSTR_CONV_FTOI;
809 op = INSTR_STORE_ENT;
816 return ir_block_create_store_op(self, op, target, what);
819 bool ir_block_create_storep(ir_block *self, ir_value *target, ir_value *what)
824 if (target->vtype != TYPE_POINTER)
827 /* storing using pointer - target is a pointer, type must be
828 * inferred from source
840 op = INSTR_STOREP_ENT;
846 op = INSTR_STOREP_FLD;
857 op = INSTR_STOREP_ENT;
864 return ir_block_create_store_op(self, op, target, what);
867 bool ir_block_create_return(ir_block *self, ir_value *v)
871 fprintf(stderr, "block already ended (%s)\n", self->label);
875 self->is_return = true;
876 in = ir_instr_new(self, INSTR_RETURN);
880 if (!ir_instr_op(in, 0, v, false) ||
881 !ir_block_instr_add(self, in) )
888 bool ir_block_create_if(ir_block *self, ir_value *v,
889 ir_block *ontrue, ir_block *onfalse)
893 fprintf(stderr, "block already ended (%s)\n", self->label);
897 /*in = ir_instr_new(self, (v->vtype == TYPE_STRING ? INSTR_IF_S : INSTR_IF_F));*/
898 in = ir_instr_new(self, VINSTR_COND);
902 if (!ir_instr_op(in, 0, v, false)) {
907 in->bops[0] = ontrue;
908 in->bops[1] = onfalse;
910 if (!ir_block_instr_add(self, in))
913 if (!ir_block_exits_add(self, ontrue) ||
914 !ir_block_exits_add(self, onfalse) ||
915 !ir_block_entries_add(ontrue, self) ||
916 !ir_block_entries_add(onfalse, self) )
923 bool ir_block_create_jump(ir_block *self, ir_block *to)
927 fprintf(stderr, "block already ended (%s)\n", self->label);
931 in = ir_instr_new(self, VINSTR_JUMP);
936 if (!ir_block_instr_add(self, in))
939 if (!ir_block_exits_add(self, to) ||
940 !ir_block_entries_add(to, self) )
947 bool ir_block_create_goto(ir_block *self, ir_block *to)
951 fprintf(stderr, "block already ended (%s)\n", self->label);
955 in = ir_instr_new(self, INSTR_GOTO);
960 if (!ir_block_instr_add(self, in))
963 if (!ir_block_exits_add(self, to) ||
964 !ir_block_entries_add(to, self) )
971 ir_instr* ir_block_create_phi(ir_block *self, const char *label, int ot)
975 in = ir_instr_new(self, VINSTR_PHI);
978 out = ir_value_out(self->owner, label, store_value, ot);
983 if (!ir_instr_op(in, 0, out, true)) {
985 ir_value_delete(out);
988 if (!ir_block_instr_add(self, in)) {
990 ir_value_delete(out);
996 ir_value* ir_phi_value(ir_instr *self)
998 return self->_ops[0];
1001 bool ir_phi_add(ir_instr* self, ir_block *b, ir_value *v)
1005 if (!ir_block_entries_find(self->owner, b, NULL)) {
1006 /* Must not be possible to cause this, otherwise the AST
1007 * is doing something wrong.
1009 fprintf(stderr, "Invalid entry block for PHI\n");
1015 if (!ir_value_reads_add(v, self))
1017 return ir_instr_phi_add(self, pe);
1020 /* binary op related code */
1022 ir_value* ir_block_create_binop(ir_block *self,
1023 const char *label, int opcode,
1024 ir_value *left, ir_value *right)
1046 case INSTR_SUB_S: /* -- offset of string as float */
1051 case INSTR_BITOR_IF:
1052 case INSTR_BITOR_FI:
1053 case INSTR_BITAND_FI:
1054 case INSTR_BITAND_IF:
1069 case INSTR_BITAND_I:
1072 case INSTR_RSHIFT_I:
1073 case INSTR_LSHIFT_I:
1095 /* boolean operations result in floats */
1096 if (opcode >= INSTR_EQ_F && opcode <= INSTR_GT)
1098 else if (opcode >= INSTR_LE && opcode <= INSTR_GT)
1101 else if (opcode >= INSTR_LE_I && opcode <= INSTR_EQ_FI)
1106 if (ot == TYPE_VOID) {
1107 /* The AST or parser were supposed to check this! */
1111 return ir_block_create_general_instr(self, label, opcode, left, right, ot);
1114 ir_value* ir_block_create_general_instr(ir_block *self, const char *label,
1115 int op, ir_value *a, ir_value *b, int outype)
1120 out = ir_value_out(self->owner, label, store_value, outype);
1124 instr = ir_instr_new(self, op);
1126 ir_value_delete(out);
1130 if (!ir_instr_op(instr, 0, out, true) ||
1131 !ir_instr_op(instr, 1, a, false) ||
1132 !ir_instr_op(instr, 2, b, false) )
1137 if (!ir_block_instr_add(self, instr))
1142 ir_instr_delete(instr);
1143 ir_value_delete(out);
1147 ir_value* ir_block_create_fieldaddress(ir_block *self, const char *label, ir_value *ent, ir_value *field)
1149 /* Support for various pointer types todo if so desired */
1150 if (ent->vtype != TYPE_ENTITY)
1153 if (field->vtype != TYPE_FIELD)
1156 return ir_block_create_general_instr(self, label, INSTR_ADDRESS, ent, field, TYPE_POINTER);
1159 ir_value* ir_block_create_load_from_ent(ir_block *self, const char *label, ir_value *ent, ir_value *field, int outype)
1162 if (ent->vtype != TYPE_ENTITY)
1165 /* at some point we could redirect for TYPE_POINTER... but that could lead to carelessness */
1166 if (field->vtype != TYPE_FIELD)
1171 case TYPE_FLOAT: op = INSTR_LOAD_F; break;
1172 case TYPE_VECTOR: op = INSTR_LOAD_V; break;
1173 case TYPE_STRING: op = INSTR_LOAD_S; break;
1174 case TYPE_FIELD: op = INSTR_LOAD_FLD; break;
1175 case TYPE_ENTITY: op = INSTR_LOAD_ENT; break;
1177 case TYPE_POINTER: op = INSTR_LOAD_I; break;
1178 case TYPE_INTEGER: op = INSTR_LOAD_I; break;
1184 return ir_block_create_general_instr(self, label, op, ent, field, outype);
1187 ir_value* ir_block_create_add(ir_block *self,
1189 ir_value *left, ir_value *right)
1192 int l = left->vtype;
1193 int r = right->vtype;
1212 if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1214 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1220 return ir_block_create_binop(self, label, op, left, right);
1223 ir_value* ir_block_create_sub(ir_block *self,
1225 ir_value *left, ir_value *right)
1228 int l = left->vtype;
1229 int r = right->vtype;
1249 if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1251 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1257 return ir_block_create_binop(self, label, op, left, right);
1260 ir_value* ir_block_create_mul(ir_block *self,
1262 ir_value *left, ir_value *right)
1265 int l = left->vtype;
1266 int r = right->vtype;
1285 if ( (l == TYPE_VECTOR && r == TYPE_FLOAT) )
1287 else if ( (l == TYPE_FLOAT && r == TYPE_VECTOR) )
1290 else if ( (l == TYPE_VECTOR && r == TYPE_INTEGER) )
1292 else if ( (l == TYPE_INTEGER && r == TYPE_VECTOR) )
1294 else if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1296 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1302 return ir_block_create_binop(self, label, op, left, right);
1305 ir_value* ir_block_create_div(ir_block *self,
1307 ir_value *left, ir_value *right)
1310 int l = left->vtype;
1311 int r = right->vtype;
1328 if ( (l == TYPE_VECTOR && r == TYPE_FLOAT) )
1330 else if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1332 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1338 return ir_block_create_binop(self, label, op, left, right);
1341 /* PHI resolving breaks the SSA, and must thus be the last
1342 * step before life-range calculation.
1345 static bool ir_block_naive_phi(ir_block *self);
1346 bool ir_function_naive_phi(ir_function *self)
1350 for (i = 0; i < self->blocks_count; ++i)
1352 if (!ir_block_naive_phi(self->blocks[i]))
1358 static bool ir_naive_phi_emit_store(ir_block *block, size_t iid, ir_value *old, ir_value *what)
1363 /* create a store */
1364 if (!ir_block_create_store(block, old, what))
1367 /* we now move it up */
1368 instr = block->instr[block->instr_count-1];
1369 for (i = block->instr_count; i > iid; --i)
1370 block->instr[i] = block->instr[i-1];
1371 block->instr[i] = instr;
1376 static bool ir_block_naive_phi(ir_block *self)
1379 /* FIXME: optionally, create_phi can add the phis
1380 * to a list so we don't need to loop through blocks
1381 * - anyway: "don't optimize YET"
1383 for (i = 0; i < self->instr_count; ++i)
1385 ir_instr *instr = self->instr[i];
1386 if (instr->opcode != VINSTR_PHI)
1389 if (!ir_block_instr_remove(self, i))
1391 --i; /* NOTE: i+1 below */
1393 for (p = 0; p < instr->phi_count; ++p)
1395 ir_value *v = instr->phi[p].value;
1396 for (w = 0; w < v->writes_count; ++w) {
1399 if (!v->writes[w]->_ops[0])
1402 /* When the write was to a global, we have to emit a mov */
1403 old = v->writes[w]->_ops[0];
1405 /* The original instruction now writes to the PHI target local */
1406 if (v->writes[w]->_ops[0] == v)
1407 v->writes[w]->_ops[0] = instr->_ops[0];
1409 if (old->store != store_value && old->store != store_local)
1411 /* If it originally wrote to a global we need to store the value
1414 if (!ir_naive_phi_emit_store(self, i+1, old, v))
1416 if (i+1 < self->instr_count)
1417 instr = self->instr[i+1];
1420 /* In case I forget and access instr later, it'll be NULL
1421 * when it's a problem, to make sure we crash, rather than accessing
1427 /* If it didn't, we can replace all reads by the phi target now. */
1429 for (r = 0; r < old->reads_count; ++r)
1432 ir_instr *ri = old->reads[r];
1433 for (op = 0; op < ri->phi_count; ++op) {
1434 if (ri->phi[op].value == old)
1435 ri->phi[op].value = v;
1437 for (op = 0; op < 3; ++op) {
1438 if (ri->_ops[op] == old)
1445 ir_instr_delete(instr);
1450 /***********************************************************************
1451 *IR Temp allocation code
1452 * Propagating value life ranges by walking through the function backwards
1453 * until no more changes are made.
1454 * In theory this should happen once more than once for every nested loop
1456 * Though this implementation might run an additional time for if nests.
1465 MEM_VEC_FUNCTIONS_ALL(new_reads_t, ir_value*, v)
1467 /* Enumerate instructions used by value's life-ranges
1469 static void ir_block_enumerate(ir_block *self, size_t *_eid)
1473 for (i = 0; i < self->instr_count; ++i)
1475 self->instr[i]->eid = eid++;
1480 /* Enumerate blocks and instructions.
1481 * The block-enumeration is unordered!
1482 * We do not really use the block enumreation, however
1483 * the instruction enumeration is important for life-ranges.
1485 void ir_function_enumerate(ir_function *self)
1488 size_t instruction_id = 0;
1489 for (i = 0; i < self->blocks_count; ++i)
1491 self->blocks[i]->eid = i;
1492 self->blocks[i]->run_id = 0;
1493 ir_block_enumerate(self->blocks[i], &instruction_id);
1497 static bool ir_block_life_propagate(ir_block *b, ir_block *prev, bool *changed);
1498 bool ir_function_calculate_liferanges(ir_function *self)
1506 for (i = 0; i != self->blocks_count; ++i)
1508 if (self->blocks[i]->is_return)
1510 if (!ir_block_life_propagate(self->blocks[i], NULL, &changed))
1518 /* Local-value allocator
1519 * After finishing creating the liferange of all values used in a function
1520 * we can allocate their global-positions.
1521 * This is the counterpart to register-allocation in register machines.
1524 MEM_VECTOR_MAKE(ir_value*, locals);
1525 MEM_VECTOR_MAKE(size_t, sizes);
1526 MEM_VECTOR_MAKE(size_t, positions);
1527 } function_allocator;
1528 MEM_VEC_FUNCTIONS(function_allocator, ir_value*, locals)
1529 MEM_VEC_FUNCTIONS(function_allocator, size_t, sizes)
1530 MEM_VEC_FUNCTIONS(function_allocator, size_t, positions)
1532 static bool function_allocator_alloc(function_allocator *alloc, const ir_value *var)
1535 size_t vsize = type_sizeof[var->vtype];
1537 slot = ir_value_var("reg", store_global, var->vtype);
1541 if (!ir_value_life_merge_into(slot, var))
1544 if (!function_allocator_locals_add(alloc, slot))
1547 if (!function_allocator_sizes_add(alloc, vsize))
1553 ir_value_delete(slot);
1557 bool ir_function_allocate_locals(ir_function *self)
1566 function_allocator alloc;
1568 MEM_VECTOR_INIT(&alloc, locals);
1569 MEM_VECTOR_INIT(&alloc, sizes);
1570 MEM_VECTOR_INIT(&alloc, positions);
1572 for (i = 0; i < self->locals_count; ++i)
1574 if (!function_allocator_alloc(&alloc, self->locals[i]))
1578 /* Allocate a slot for any value that still exists */
1579 for (i = 0; i < self->values_count; ++i)
1581 v = self->values[i];
1586 for (a = 0; a < alloc.locals_count; ++a)
1588 slot = alloc.locals[a];
1590 if (ir_values_overlap(v, slot))
1593 if (!ir_value_life_merge_into(slot, v))
1596 /* adjust size for this slot */
1597 if (alloc.sizes[a] < type_sizeof[v->vtype])
1598 alloc.sizes[a] = type_sizeof[v->vtype];
1600 self->values[i]->code.local = a;
1603 if (a >= alloc.locals_count) {
1604 self->values[i]->code.local = alloc.locals_count;
1605 if (!function_allocator_alloc(&alloc, v))
1610 /* Adjust slot positions based on sizes */
1611 if (!function_allocator_positions_add(&alloc, 0))
1614 for (i = 1; i < alloc.sizes_count; ++i)
1616 pos = alloc.positions[i-1] + alloc.sizes[i-1];
1617 if (!function_allocator_positions_add(&alloc, pos))
1621 self->allocated_locals = pos + alloc.sizes[alloc.sizes_count-1];
1623 /* Take over the actual slot positions */
1624 for (i = 0; i < self->values_count; ++i)
1625 self->values[i]->code.local = alloc.positions[self->values[i]->code.local];
1632 for (i = 0; i < alloc.locals_count; ++i)
1633 ir_value_delete(alloc.locals[i]);
1634 MEM_VECTOR_CLEAR(&alloc, locals);
1635 MEM_VECTOR_CLEAR(&alloc, sizes);
1636 MEM_VECTOR_CLEAR(&alloc, positions);
1640 /* Get information about which operand
1641 * is read from, or written to.
1643 static void ir_op_read_write(int op, size_t *read, size_t *write)
1670 static bool ir_block_living_add_instr(ir_block *self, size_t eid)
1673 bool changed = false;
1675 for (i = 0; i != self->living_count; ++i)
1677 tempbool = ir_value_life_merge(self->living[i], eid);
1680 fprintf(stderr, "block_living_add_instr() value instruction added %s: %i\n", self->living[i]->_name, (int)eid);
1682 changed = changed || tempbool;
1687 static bool ir_block_life_prop_previous(ir_block* self, ir_block *prev, bool *changed)
1690 /* values which have been read in a previous iteration are now
1691 * in the "living" array even if the previous block doesn't use them.
1692 * So we have to remove whatever does not exist in the previous block.
1693 * They will be re-added on-read, but the liferange merge won't cause
1696 for (i = 0; i < self->living_count; ++i)
1698 if (!ir_block_living_find(prev, self->living[i], NULL)) {
1699 if (!ir_block_living_remove(self, i))
1705 /* Whatever the previous block still has in its living set
1706 * must now be added to ours as well.
1708 for (i = 0; i < prev->living_count; ++i)
1710 if (ir_block_living_find(self, prev->living[i], NULL))
1712 if (!ir_block_living_add(self, prev->living[i]))
1715 printf("%s got from prev: %s\n", self->label, prev->living[i]->_name);
1721 static bool ir_block_life_propagate(ir_block *self, ir_block *prev, bool *changed)
1727 /* bitmasks which operands are read from or written to */
1729 #if defined(LIFE_RANGE_WITHOUT_LAST_READ)
1731 new_reads_t new_reads;
1733 char dbg_ind[16] = { '#', '0' };
1736 #if defined(LIFE_RANGE_WITHOUT_LAST_READ)
1737 MEM_VECTOR_INIT(&new_reads, v);
1742 if (!ir_block_life_prop_previous(self, prev, changed))
1746 i = self->instr_count;
1749 instr = self->instr[i];
1751 /* PHI operands are always read operands */
1752 for (p = 0; p < instr->phi_count; ++p)
1754 value = instr->phi[p].value;
1755 #if ! defined(LIFE_RANGE_WITHOUT_LAST_READ)
1756 if (!ir_block_living_find(self, value, NULL) &&
1757 !ir_block_living_add(self, value))
1762 if (!new_reads_t_v_find(&new_reads, value, NULL))
1764 if (!new_reads_t_v_add(&new_reads, value))
1770 /* See which operands are read and write operands */
1771 ir_op_read_write(instr->opcode, &read, &write);
1773 /* Go through the 3 main operands */
1774 for (o = 0; o < 3; ++o)
1776 if (!instr->_ops[o]) /* no such operand */
1779 value = instr->_ops[o];
1781 /* We only care about locals */
1782 if (value->store != store_value &&
1783 value->store != store_local)
1789 #if ! defined(LIFE_RANGE_WITHOUT_LAST_READ)
1790 if (!ir_block_living_find(self, value, NULL) &&
1791 !ir_block_living_add(self, value))
1796 /* fprintf(stderr, "read: %s\n", value->_name); */
1797 if (!new_reads_t_v_find(&new_reads, value, NULL))
1799 if (!new_reads_t_v_add(&new_reads, value))
1805 /* write operands */
1806 /* When we write to a local, we consider it "dead" for the
1807 * remaining upper part of the function, since in SSA a value
1808 * can only be written once (== created)
1813 bool in_living = ir_block_living_find(self, value, &idx);
1814 #if defined(LIFE_RANGE_WITHOUT_LAST_READ)
1816 bool in_reads = new_reads_t_v_find(&new_reads, value, &readidx);
1817 if (!in_living && !in_reads)
1822 /* If the value isn't alive it hasn't been read before... */
1823 /* TODO: See if the warning can be emitted during parsing or AST processing
1824 * otherwise have warning printed here.
1825 * IF printing a warning here: include filecontext_t,
1826 * and make sure it's only printed once
1827 * since this function is run multiple times.
1829 /* For now: debug info: */
1830 fprintf(stderr, "Value only written %s\n", value->name);
1831 tempbool = ir_value_life_merge(value, instr->eid);
1832 *changed = *changed || tempbool;
1834 ir_instr_dump(instr, dbg_ind, printf);
1838 /* since 'living' won't contain it
1839 * anymore, merge the value, since
1842 tempbool = ir_value_life_merge(value, instr->eid);
1845 fprintf(stderr, "value added id %s %i\n", value->name, (int)instr->eid);
1847 *changed = *changed || tempbool;
1849 #if ! defined(LIFE_RANGE_WITHOUT_LAST_READ)
1850 if (!ir_block_living_remove(self, idx))
1855 if (!new_reads_t_v_remove(&new_reads, readidx))
1863 tempbool = ir_block_living_add_instr(self, instr->eid);
1864 /*fprintf(stderr, "living added values\n");*/
1865 *changed = *changed || tempbool;
1867 #if defined(LIFE_RANGE_WITHOUT_LAST_READ)
1869 for (rd = 0; rd < new_reads.v_count; ++rd)
1871 if (!ir_block_living_find(self, new_reads.v[rd], NULL)) {
1872 if (!ir_block_living_add(self, new_reads.v[rd]))
1875 if (!i && !self->entries_count) {
1877 *changed = *changed || ir_value_life_merge(new_reads.v[rd], instr->eid);
1880 MEM_VECTOR_CLEAR(&new_reads, v);
1884 if (self->run_id == self->owner->run_id)
1887 self->run_id = self->owner->run_id;
1889 for (i = 0; i < self->entries_count; ++i)
1891 ir_block *entry = self->entries[i];
1892 ir_block_life_propagate(entry, self, changed);
1897 #if defined(LIFE_RANGE_WITHOUT_LAST_READ)
1898 MEM_VECTOR_CLEAR(&new_reads, v);
1903 /***********************************************************************
1906 * Since the IR has the convention of putting 'write' operands
1907 * at the beginning, we have to rotate the operands of instructions
1908 * properly in order to generate valid QCVM code.
1910 * Having destinations at a fixed position is more convenient. In QC
1911 * this is *mostly* OPC, but FTE adds at least 2 instructions which
1912 * read from from OPA, and store to OPB rather than OPC. Which is
1913 * partially the reason why the implementation of these instructions
1914 * in darkplaces has been delayed for so long.
1916 * Breaking conventions is annoying...
1918 static bool ir_builder_gen_global(ir_builder *self, ir_value *global);
1920 static bool gen_global_field(ir_value *global)
1922 if (global->isconst)
1924 ir_value *fld = global->constval.vpointer;
1926 printf("Invalid field constant with no field: %s\n", global->name);
1930 /* Now, in this case, a relocation would be impossible to code
1931 * since it looks like this:
1932 * .vector v = origin; <- parse error, wtf is 'origin'?
1935 * But we will need a general relocation support later anyway
1936 * for functions... might as well support that here.
1938 if (!fld->code.globaladdr) {
1939 printf("FIXME: Relocation support\n");
1943 /* copy the field's value */
1944 global->code.globaladdr = code_globals_add(code_globals_data[fld->code.globaladdr]);
1948 prog_section_field fld;
1950 fld.name = global->code.name;
1951 fld.offset = code_fields_elements;
1952 fld.type = global->fieldtype;
1954 if (fld.type == TYPE_VOID) {
1955 printf("Field is missing a type: %s\n", global->name);
1959 if (code_fields_add(fld) < 0)
1962 global->code.globaladdr = code_globals_add(fld.offset);
1964 if (global->code.globaladdr < 0)
1969 static bool gen_global_pointer(ir_value *global)
1971 if (global->isconst)
1973 ir_value *target = global->constval.vpointer;
1975 printf("Invalid pointer constant: %s\n", global->name);
1976 /* NULL pointers are pointing to the NULL constant, which also
1977 * sits at address 0, but still has an ir_value for itself.
1982 /* Here, relocations ARE possible - in fteqcc-enhanced-qc:
1983 * void() foo; <- proto
1984 * void() *fooptr = &foo;
1985 * void() foo = { code }
1987 if (!target->code.globaladdr) {
1988 /* FIXME: Check for the constant nullptr ir_value!
1989 * because then code.globaladdr being 0 is valid.
1991 printf("FIXME: Relocation support\n");
1995 global->code.globaladdr = code_globals_add(target->code.globaladdr);
1999 global->code.globaladdr = code_globals_add(0);
2001 if (global->code.globaladdr < 0)
2006 static bool gen_blocks_recursive(ir_function *func, ir_block *block)
2008 prog_section_statement stmt;
2017 block->generated = true;
2018 block->code_start = code_statements_elements;
2019 for (i = 0; i < block->instr_count; ++i)
2021 instr = block->instr[i];
2023 if (instr->opcode == VINSTR_PHI) {
2024 printf("cannot generate virtual instruction (phi)\n");
2028 if (instr->opcode == VINSTR_JUMP) {
2029 target = instr->bops[0];
2030 /* for uncoditional jumps, if the target hasn't been generated
2031 * yet, we generate them right here.
2033 if (!target->generated) {
2038 /* otherwise we generate a jump instruction */
2039 stmt.opcode = INSTR_GOTO;
2040 stmt.o1.s1 = (target->code_start) - code_statements_elements;
2043 if (code_statements_add(stmt) < 0)
2046 /* no further instructions can be in this block */
2050 if (instr->opcode == VINSTR_COND) {
2051 ontrue = instr->bops[0];
2052 onfalse = instr->bops[1];
2053 /* TODO: have the AST signal which block should
2054 * come first: eg. optimize IFs without ELSE...
2057 stmt.o1.u1 = instr->_ops[0]->code.globaladdr;
2061 if (ontrue->generated) {
2062 stmt.opcode = INSTR_IF;
2063 stmt.o2.s1 = (ontrue->code_start-1) - code_statements_elements;
2064 if (code_statements_add(stmt) < 0)
2067 if (onfalse->generated) {
2068 stmt.opcode = INSTR_IFNOT;
2069 stmt.o2.s1 = (onfalse->code_start-1) - code_statements_elements;
2070 if (code_statements_add(stmt) < 0)
2073 if (!ontrue->generated) {
2074 if (onfalse->generated) {
2079 if (!onfalse->generated) {
2080 if (ontrue->generated) {
2085 /* neither ontrue nor onfalse exist */
2086 stmt.opcode = INSTR_IFNOT;
2087 stidx = code_statements_elements;
2088 if (code_statements_add(stmt) < 0)
2090 /* on false we jump, so add ontrue-path */
2091 if (!gen_blocks_recursive(func, ontrue))
2093 /* fixup the jump address */
2094 code_statements_data[stidx].o2.s1 = code_statements_elements - stidx;
2095 /* generate onfalse path */
2096 if (onfalse->generated) {
2097 /* fixup the jump address */
2098 code_statements_data[stidx].o2.s1 = (onfalse->code_start) - (stidx);
2099 /* may have been generated in the previous recursive call */
2100 stmt.opcode = INSTR_GOTO;
2101 stmt.o1.s1 = (onfalse->code_start) - code_statements_elements;
2104 return (code_statements_add(stmt) >= 0);
2106 /* if not, generate now */
2111 if (instr->opcode >= INSTR_CALL0 && instr->opcode <= INSTR_CALL8) {
2112 printf("TODO: call instruction\n");
2116 if (instr->opcode == INSTR_STATE) {
2117 printf("TODO: state instruction\n");
2121 stmt.opcode = instr->opcode;
2126 /* This is the general order of operands */
2128 stmt.o3.u1 = instr->_ops[0]->code.globaladdr;
2131 stmt.o1.u1 = instr->_ops[1]->code.globaladdr;
2134 stmt.o2.u1 = instr->_ops[2]->code.globaladdr;
2136 if (stmt.opcode == INSTR_RETURN || stmt.opcode == INSTR_DONE)
2138 stmt.o1.u1 = stmt.o3.u1;
2141 else if ((stmt.opcode >= INSTR_STORE_F &&
2142 stmt.opcode <= INSTR_STORE_FNC) ||
2143 (stmt.opcode >= INSTR_NOT_F &&
2144 stmt.opcode <= INSTR_NOT_FNC))
2146 /* 2-operand instructions with A -> B */
2147 stmt.o2.u1 = stmt.o3.u1;
2151 if (code_statements_add(stmt) < 0)
2157 static bool gen_function_code(ir_function *self)
2161 /* Starting from entry point, we generate blocks "as they come"
2162 * for now. Dead blocks will not be translated obviously.
2164 if (!self->blocks_count) {
2165 printf("Function '%s' declared without body.\n", self->name);
2169 block = self->blocks[0];
2170 if (block->generated)
2173 if (!gen_blocks_recursive(self, block)) {
2174 printf("failed to generate blocks for '%s'\n", self->name);
2180 static bool gen_global_function(ir_builder *ir, ir_value *global)
2182 prog_section_function fun;
2186 size_t local_var_end;
2188 if (!global->isconst ||
2189 !global->constval.vfunc)
2191 printf("Invalid state of function-global: not constant: %s\n", global->name);
2195 irfun = global->constval.vfunc;
2197 fun.name = global->code.name;
2198 fun.file = code_cachedstring(global->context.file);
2199 fun.profile = 0; /* always 0 */
2200 fun.nargs = irfun->params_count;
2202 for (i = 0;i < 8; ++i) {
2205 else if (irfun->params[i] == TYPE_VECTOR)
2211 fun.firstlocal = code_globals_elements;
2212 fun.locals = irfun->allocated_locals + irfun->locals_count;
2215 for (i = 0; i < irfun->locals_count; ++i) {
2216 if (!ir_builder_gen_global(ir, irfun->locals[i])) {
2217 printf("Failed to generate global %s\n", irfun->locals[i]->name);
2221 if (irfun->locals_count) {
2222 ir_value *last = irfun->locals[irfun->locals_count-1];
2223 local_var_end = last->code.globaladdr;
2224 local_var_end += type_sizeof[last->vtype];
2226 for (i = 0; i < irfun->values_count; ++i)
2228 /* generate code.globaladdr for ssa values */
2229 ir_value *v = irfun->values[i];
2230 v->code.globaladdr = local_var_end + v->code.local;
2232 for (i = 0; i < irfun->locals_count; ++i) {
2233 /* fill the locals with zeros */
2234 code_globals_add(0);
2237 fun.entry = code_statements_elements;
2238 if (!gen_function_code(irfun)) {
2239 printf("Failed to generate code for function %s\n", irfun->name);
2243 return (code_functions_add(fun) >= 0);
2246 static bool ir_builder_gen_global(ir_builder *self, ir_value *global)
2249 prog_section_def def;
2251 def.type = global->vtype;
2252 def.offset = code_globals_elements;
2253 def.name = global->code.name = code_genstring(global->name);
2255 switch (global->vtype)
2258 if (code_defs_add(def) < 0)
2260 return gen_global_pointer(global);
2262 if (code_defs_add(def) < 0)
2264 return gen_global_field(global);
2269 if (code_defs_add(def) < 0)
2272 if (global->isconst) {
2273 iptr = (int32_t*)&global->constval.vfloat;
2274 global->code.globaladdr = code_globals_add(*iptr);
2276 global->code.globaladdr = code_globals_add(0);
2278 return global->code.globaladdr >= 0;
2282 if (code_defs_add(def) < 0)
2284 if (global->isconst)
2285 global->code.globaladdr = code_globals_add(code_cachedstring(global->constval.vstring));
2287 global->code.globaladdr = code_globals_add(0);
2288 return global->code.globaladdr >= 0;
2292 if (code_defs_add(def) < 0)
2295 if (global->isconst) {
2296 iptr = (int32_t*)&global->constval.vvec;
2297 global->code.globaladdr = code_globals_add(iptr[0]);
2298 if (code_globals_add(iptr[1]) < 0 || code_globals_add(iptr[2]) < 0)
2301 global->code.globaladdr = code_globals_add(0);
2302 if (code_globals_add(0) < 0 || code_globals_add(0) < 0)
2305 return global->code.globaladdr >= 0;
2308 if (code_defs_add(def) < 0)
2310 code_globals_add(code_functions_elements);
2311 return gen_global_function(self, global);
2313 /* assume biggest type */
2314 global->code.globaladdr = code_globals_add(0);
2315 code_globals_add(0);
2316 code_globals_add(0);
2319 /* refuse to create 'void' type or any other fancy business. */
2320 printf("Invalid type for global variable %s\n", global->name);
2325 bool ir_builder_generate(ir_builder *self, const char *filename)
2331 /* FIXME: generate TYPE_FUNCTION globals and link them
2332 * to their ir_function.
2335 for (i = 0; i < self->functions_count; ++i)
2338 ir_function *fun = self->functions[i];
2340 funval = ir_builder_create_global(self, fun->name, TYPE_FUNCTION);
2341 funval->isconst = true;
2342 funval->constval.vfunc = fun;
2343 funval->context = fun->context;
2346 for (i = 0; i < self->globals_count; ++i)
2348 if (!ir_builder_gen_global(self, self->globals[i])) {
2353 printf("writing '%s'...\n", filename);
2354 return code_write(filename);
2357 /***********************************************************************
2358 *IR DEBUG Dump functions...
2361 #define IND_BUFSZ 1024
2363 const char *qc_opname(int op)
2365 if (op < 0) return "<INVALID>";
2366 if (op < ( sizeof(asm_instr) / sizeof(asm_instr[0]) ))
2367 return asm_instr[op].m;
2369 case VINSTR_PHI: return "PHI";
2370 case VINSTR_JUMP: return "JUMP";
2371 case VINSTR_COND: return "COND";
2372 default: return "<UNK>";
2376 void ir_builder_dump(ir_builder *b, int (*oprintf)(const char*, ...))
2379 char indent[IND_BUFSZ];
2383 oprintf("module %s\n", b->name);
2384 for (i = 0; i < b->globals_count; ++i)
2387 if (b->globals[i]->isconst)
2388 oprintf("%s = ", b->globals[i]->name);
2389 ir_value_dump(b->globals[i], oprintf);
2392 for (i = 0; i < b->functions_count; ++i)
2393 ir_function_dump(b->functions[i], indent, oprintf);
2394 oprintf("endmodule %s\n", b->name);
2397 void ir_function_dump(ir_function *f, char *ind,
2398 int (*oprintf)(const char*, ...))
2401 oprintf("%sfunction %s\n", ind, f->name);
2402 strncat(ind, "\t", IND_BUFSZ);
2403 if (f->locals_count)
2405 oprintf("%s%i locals:\n", ind, (int)f->locals_count);
2406 for (i = 0; i < f->locals_count; ++i) {
2407 oprintf("%s\t", ind);
2408 ir_value_dump(f->locals[i], oprintf);
2412 if (f->blocks_count)
2414 oprintf("%slife passes (check): %i\n", ind, (int)f->run_id);
2415 for (i = 0; i < f->blocks_count; ++i) {
2416 if (f->blocks[i]->run_id != f->run_id) {
2417 oprintf("%slife pass check fail! %i != %i\n", ind, (int)f->blocks[i]->run_id, (int)f->run_id);
2419 ir_block_dump(f->blocks[i], ind, oprintf);
2423 ind[strlen(ind)-1] = 0;
2424 oprintf("%sendfunction %s\n", ind, f->name);
2427 void ir_block_dump(ir_block* b, char *ind,
2428 int (*oprintf)(const char*, ...))
2431 oprintf("%s:%s\n", ind, b->label);
2432 strncat(ind, "\t", IND_BUFSZ);
2434 for (i = 0; i < b->instr_count; ++i)
2435 ir_instr_dump(b->instr[i], ind, oprintf);
2436 ind[strlen(ind)-1] = 0;
2439 void dump_phi(ir_instr *in, char *ind,
2440 int (*oprintf)(const char*, ...))
2443 oprintf("%s <- phi ", in->_ops[0]->name);
2444 for (i = 0; i < in->phi_count; ++i)
2446 oprintf("([%s] : %s) ", in->phi[i].from->label,
2447 in->phi[i].value->name);
2452 void ir_instr_dump(ir_instr *in, char *ind,
2453 int (*oprintf)(const char*, ...))
2456 const char *comma = NULL;
2458 oprintf("%s (%i) ", ind, (int)in->eid);
2460 if (in->opcode == VINSTR_PHI) {
2461 dump_phi(in, ind, oprintf);
2465 strncat(ind, "\t", IND_BUFSZ);
2467 if (in->_ops[0] && (in->_ops[1] || in->_ops[2])) {
2468 ir_value_dump(in->_ops[0], oprintf);
2469 if (in->_ops[1] || in->_ops[2])
2472 oprintf("%s\t", qc_opname(in->opcode));
2473 if (in->_ops[0] && !(in->_ops[1] || in->_ops[2])) {
2474 ir_value_dump(in->_ops[0], oprintf);
2479 for (i = 1; i != 3; ++i) {
2483 ir_value_dump(in->_ops[i], oprintf);
2491 oprintf("[%s]", in->bops[0]->label);
2495 oprintf("%s[%s]", comma, in->bops[1]->label);
2497 ind[strlen(ind)-1] = 0;
2500 void ir_value_dump(ir_value* v, int (*oprintf)(const char*, ...))
2508 oprintf("%g", v->constval.vfloat);
2511 oprintf("'%g %g %g'",
2514 v->constval.vvec.z);
2517 oprintf("(entity)");
2520 oprintf("\"%s\"", v->constval.vstring);
2524 oprintf("%i", v->constval.vint);
2529 v->constval.vpointer->name);
2533 oprintf("%s", v->name);
2537 void ir_value_dump_life(ir_value *self, int (*oprintf)(const char*,...))
2540 oprintf("Life of %s:\n", self->name);
2541 for (i = 0; i < self->life_count; ++i)
2543 oprintf(" + [%i, %i]\n", self->life[i].start, self->life[i].end);