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 /***********************************************************************
32 ir_builder* ir_builder_new(const char *modulename)
36 self = (ir_builder*)mem_a(sizeof(*self));
40 MEM_VECTOR_INIT(self, functions);
41 MEM_VECTOR_INIT(self, globals);
43 if (!ir_builder_set_name(self, modulename)) {
48 /* globals which always exist */
50 /* for now we give it a vector size */
51 ir_builder_create_global(self, "OFS_RETURN", TYPE_VARIANT);
56 MEM_VEC_FUNCTIONS(ir_builder, ir_value*, globals)
57 MEM_VEC_FUNCTIONS(ir_builder, ir_function*, functions)
59 void ir_builder_delete(ir_builder* self)
62 mem_d((void*)self->name);
63 for (i = 0; i != self->functions_count; ++i) {
64 ir_function_delete(self->functions[i]);
66 MEM_VECTOR_CLEAR(self, functions);
67 for (i = 0; i != self->globals_count; ++i) {
68 ir_value_delete(self->globals[i]);
70 MEM_VECTOR_CLEAR(self, globals);
74 bool ir_builder_set_name(ir_builder *self, const char *name)
77 mem_d((void*)self->name);
78 self->name = util_strdup(name);
82 ir_function* ir_builder_get_function(ir_builder *self, const char *name)
85 for (i = 0; i < self->functions_count; ++i) {
86 if (!strcmp(name, self->functions[i]->name))
87 return self->functions[i];
92 ir_function* ir_builder_create_function(ir_builder *self, const char *name)
94 ir_function *fn = ir_builder_get_function(self, name);
99 fn = ir_function_new(self);
100 if (!ir_function_set_name(fn, name) ||
101 !ir_builder_functions_add(self, fn) )
103 ir_function_delete(fn);
109 ir_value* ir_builder_get_global(ir_builder *self, const char *name)
112 for (i = 0; i < self->globals_count; ++i) {
113 if (!strcmp(self->globals[i]->name, name))
114 return self->globals[i];
119 ir_value* ir_builder_create_global(ir_builder *self, const char *name, int vtype)
121 ir_value *ve = ir_builder_get_global(self, name);
126 ve = ir_value_var(name, store_global, vtype);
127 if (!ir_builder_globals_add(self, ve)) {
134 /***********************************************************************
138 bool ir_function_naive_phi(ir_function*);
139 void ir_function_enumerate(ir_function*);
140 bool ir_function_calculate_liferanges(ir_function*);
141 bool ir_function_allocate_locals(ir_function*);
143 ir_function* ir_function_new(ir_builder* owner)
146 self = (ir_function*)mem_a(sizeof(*self));
152 if (!ir_function_set_name(self, "<@unnamed>")) {
157 self->context.file = "<@no context>";
158 self->context.line = 0;
159 self->retype = TYPE_VOID;
160 MEM_VECTOR_INIT(self, params);
161 MEM_VECTOR_INIT(self, blocks);
162 MEM_VECTOR_INIT(self, values);
163 MEM_VECTOR_INIT(self, locals);
168 MEM_VEC_FUNCTIONS(ir_function, ir_value*, values)
169 MEM_VEC_FUNCTIONS(ir_function, ir_block*, blocks)
170 MEM_VEC_FUNCTIONS(ir_function, ir_value*, locals)
172 bool ir_function_set_name(ir_function *self, const char *name)
175 mem_d((void*)self->name);
176 self->name = util_strdup(name);
180 void ir_function_delete(ir_function *self)
183 mem_d((void*)self->name);
185 for (i = 0; i != self->blocks_count; ++i)
186 ir_block_delete(self->blocks[i]);
187 MEM_VECTOR_CLEAR(self, blocks);
189 MEM_VECTOR_CLEAR(self, params);
191 for (i = 0; i != self->values_count; ++i)
192 ir_value_delete(self->values[i]);
193 MEM_VECTOR_CLEAR(self, values);
195 for (i = 0; i != self->locals_count; ++i)
196 ir_value_delete(self->locals[i]);
197 MEM_VECTOR_CLEAR(self, locals);
202 bool GMQCC_WARN ir_function_collect_value(ir_function *self, ir_value *v)
204 return ir_function_values_add(self, v);
207 ir_block* ir_function_create_block(ir_function *self, const char *label)
209 ir_block* bn = ir_block_new(self, label);
210 memcpy(&bn->context, &self->context, sizeof(self->context));
211 if (!ir_function_blocks_add(self, bn)) {
218 bool ir_function_finalize(ir_function *self)
220 if (!ir_function_naive_phi(self))
223 ir_function_enumerate(self);
225 if (!ir_function_calculate_liferanges(self))
228 if (!ir_function_allocate_locals(self))
233 ir_value* ir_function_get_local(ir_function *self, const char *name)
236 for (i = 0; i < self->locals_count; ++i) {
237 if (!strcmp(self->locals[i]->name, name))
238 return self->locals[i];
243 ir_value* ir_function_create_local(ir_function *self, const char *name, int vtype)
245 ir_value *ve = ir_function_get_local(self, name);
250 ve = ir_value_var(name, store_local, vtype);
251 if (!ir_function_locals_add(self, ve)) {
258 /***********************************************************************
262 ir_block* ir_block_new(ir_function* owner, const char *name)
265 self = (ir_block*)mem_a(sizeof(*self));
269 memset(self, 0, sizeof(*self));
272 if (!ir_block_set_label(self, name)) {
277 self->context.file = "<@no context>";
278 self->context.line = 0;
280 MEM_VECTOR_INIT(self, instr);
281 MEM_VECTOR_INIT(self, entries);
282 MEM_VECTOR_INIT(self, exits);
285 self->is_return = false;
287 MEM_VECTOR_INIT(self, living);
289 self->generated = false;
293 MEM_VEC_FUNCTIONS(ir_block, ir_instr*, instr)
294 MEM_VEC_FUNCTIONS_ALL(ir_block, ir_block*, entries)
295 MEM_VEC_FUNCTIONS_ALL(ir_block, ir_block*, exits)
296 MEM_VEC_FUNCTIONS_ALL(ir_block, ir_value*, living)
298 void ir_block_delete(ir_block* self)
302 for (i = 0; i != self->instr_count; ++i)
303 ir_instr_delete(self->instr[i]);
304 MEM_VECTOR_CLEAR(self, instr);
305 MEM_VECTOR_CLEAR(self, entries);
306 MEM_VECTOR_CLEAR(self, exits);
307 MEM_VECTOR_CLEAR(self, living);
311 bool ir_block_set_label(ir_block *self, const char *name)
314 mem_d((void*)self->label);
315 self->label = util_strdup(name);
316 return !!self->label;
319 /***********************************************************************
323 ir_instr* ir_instr_new(ir_block* owner, int op)
326 self = (ir_instr*)mem_a(sizeof(*self));
331 self->context.file = "<@no context>";
332 self->context.line = 0;
334 self->_ops[0] = NULL;
335 self->_ops[1] = NULL;
336 self->_ops[2] = NULL;
337 self->bops[0] = NULL;
338 self->bops[1] = NULL;
339 MEM_VECTOR_INIT(self, phi);
344 MEM_VEC_FUNCTIONS(ir_instr, ir_phi_entry_t, phi)
346 void ir_instr_delete(ir_instr *self)
349 /* The following calls can only delete from
350 * vectors, we still want to delete this instruction
351 * so ignore the return value. Since with the warn_unused_result attribute
352 * gcc doesn't care about an explicit: (void)foo(); to ignore the result,
353 * I have to improvise here and use if(foo());
355 for (i = 0; i < self->phi_count; ++i) {
357 if (ir_value_writes_find(self->phi[i].value, self, &idx))
358 if (ir_value_writes_remove(self->phi[i].value, idx)) GMQCC_SUPRESS_EMPTY_BODY;
359 if (ir_value_reads_find(self->phi[i].value, self, &idx))
360 if (ir_value_reads_remove (self->phi[i].value, idx)) GMQCC_SUPRESS_EMPTY_BODY;
362 MEM_VECTOR_CLEAR(self, phi);
363 if (ir_instr_op(self, 0, NULL, false)) GMQCC_SUPRESS_EMPTY_BODY;
364 if (ir_instr_op(self, 1, NULL, false)) GMQCC_SUPRESS_EMPTY_BODY;
365 if (ir_instr_op(self, 2, NULL, false)) GMQCC_SUPRESS_EMPTY_BODY;
369 bool ir_instr_op(ir_instr *self, int op, ir_value *v, bool writing)
371 if (self->_ops[op]) {
373 if (writing && ir_value_writes_find(self->_ops[op], self, &idx))
375 if (!ir_value_writes_remove(self->_ops[op], idx))
378 else if (ir_value_reads_find(self->_ops[op], self, &idx))
380 if (!ir_value_reads_remove(self->_ops[op], idx))
386 if (!ir_value_writes_add(v, self))
389 if (!ir_value_reads_add(v, self))
397 /***********************************************************************
401 ir_value* ir_value_var(const char *name, int storetype, int vtype)
404 self = (ir_value*)mem_a(sizeof(*self));
406 self->fieldtype = TYPE_VOID;
407 self->store = storetype;
408 MEM_VECTOR_INIT(self, reads);
409 MEM_VECTOR_INIT(self, writes);
410 self->isconst = false;
411 self->context.file = "<@no context>";
412 self->context.line = 0;
414 ir_value_set_name(self, name);
416 memset(&self->constval, 0, sizeof(self->constval));
417 memset(&self->code, 0, sizeof(self->code));
419 MEM_VECTOR_INIT(self, life);
422 MEM_VEC_FUNCTIONS(ir_value, ir_life_entry_t, life)
423 MEM_VEC_FUNCTIONS_ALL(ir_value, ir_instr*, reads)
424 MEM_VEC_FUNCTIONS_ALL(ir_value, ir_instr*, writes)
426 ir_value* ir_value_out(ir_function *owner, const char *name, int storetype, int vtype)
428 ir_value *v = ir_value_var(name, storetype, vtype);
431 if (!ir_function_collect_value(owner, v))
439 void ir_value_delete(ir_value* self)
442 mem_d((void*)self->name);
445 if (self->vtype == TYPE_STRING)
446 mem_d((void*)self->constval.vstring);
448 MEM_VECTOR_CLEAR(self, reads);
449 MEM_VECTOR_CLEAR(self, writes);
450 MEM_VECTOR_CLEAR(self, life);
454 void ir_value_set_name(ir_value *self, const char *name)
457 mem_d((void*)self->name);
458 self->name = util_strdup(name);
461 bool ir_value_set_float(ir_value *self, float f)
463 if (self->vtype != TYPE_FLOAT)
465 self->constval.vfloat = f;
466 self->isconst = true;
470 bool ir_value_set_vector(ir_value *self, vector v)
472 if (self->vtype != TYPE_VECTOR)
474 self->constval.vvec = v;
475 self->isconst = true;
479 bool ir_value_set_string(ir_value *self, const char *str)
481 if (self->vtype != TYPE_STRING)
483 self->constval.vstring = util_strdup(str);
484 self->isconst = true;
489 bool ir_value_set_int(ir_value *self, int i)
491 if (self->vtype != TYPE_INTEGER)
493 self->constval.vint = i;
494 self->isconst = true;
499 bool ir_value_lives(ir_value *self, size_t at)
502 for (i = 0; i < self->life_count; ++i)
504 ir_life_entry_t *life = &self->life[i];
505 if (life->start <= at && at <= life->end)
507 if (life->start > at) /* since it's ordered */
513 bool ir_value_life_insert(ir_value *self, size_t idx, ir_life_entry_t e)
516 if (!ir_value_life_add(self, e)) /* naive... */
518 for (k = self->life_count-1; k > idx; --k)
519 self->life[k] = self->life[k-1];
524 bool ir_value_life_merge(ir_value *self, size_t s)
527 ir_life_entry_t *life = NULL;
528 ir_life_entry_t *before = NULL;
529 ir_life_entry_t new_entry;
531 /* Find the first range >= s */
532 for (i = 0; i < self->life_count; ++i)
535 life = &self->life[i];
539 /* nothing found? append */
540 if (i == self->life_count) {
542 if (life && life->end+1 == s)
544 /* previous life range can be merged in */
548 if (life && life->end >= s)
551 if (!ir_value_life_add(self, e))
552 return false; /* failing */
558 if (before->end + 1 == s &&
559 life->start - 1 == s)
562 before->end = life->end;
563 if (!ir_value_life_remove(self, i))
564 return false; /* failing */
567 if (before->end + 1 == s)
573 /* already contained */
574 if (before->end >= s)
578 if (life->start - 1 == s)
583 /* insert a new entry */
584 new_entry.start = new_entry.end = s;
585 return ir_value_life_insert(self, i, new_entry);
588 bool ir_value_life_merge_into(ir_value *self, const ir_value *other)
592 if (!other->life_count)
595 if (!self->life_count) {
596 for (i = 0; i < other->life_count; ++i) {
597 if (!ir_value_life_add(self, other->life[i]))
604 for (i = 0; i < other->life_count; ++i)
606 const ir_life_entry_t *life = &other->life[i];
609 ir_life_entry_t *entry = &self->life[myi];
611 if (life->end+1 < entry->start)
613 /* adding an interval before entry */
614 if (!ir_value_life_insert(self, myi, *life))
620 if (life->start < entry->start &&
621 life->end >= entry->start)
623 /* starts earlier and overlaps */
624 entry->start = life->start;
627 if (life->end > entry->end &&
628 life->start-1 <= entry->end)
630 /* ends later and overlaps */
631 entry->end = life->end;
634 /* see if our change combines it with the next ranges */
635 while (myi+1 < self->life_count &&
636 entry->end+1 >= self->life[1+myi].start)
638 /* overlaps with (myi+1) */
639 if (entry->end < self->life[1+myi].end)
640 entry->end = self->life[1+myi].end;
641 if (!ir_value_life_remove(self, myi+1))
643 entry = &self->life[myi];
646 /* see if we're after the entry */
647 if (life->start > entry->end)
650 /* append if we're at the end */
651 if (myi >= self->life_count) {
652 if (!ir_value_life_add(self, *life))
656 /* otherweise check the next range */
665 bool ir_values_overlap(const ir_value *a, const ir_value *b)
667 /* For any life entry in A see if it overlaps with
668 * any life entry in B.
669 * Note that the life entries are orderes, so we can make a
670 * more efficient algorithm there than naively translating the
674 ir_life_entry_t *la, *lb, *enda, *endb;
676 /* first of all, if either has no life range, they cannot clash */
677 if (!a->life_count || !b->life_count)
682 enda = la + a->life_count;
683 endb = lb + b->life_count;
686 /* check if the entries overlap, for that,
687 * both must start before the other one ends.
689 #if defined(LIFE_RANGE_WITHOUT_LAST_READ)
690 if (la->start <= lb->end &&
691 lb->start <= la->end)
693 if (la->start < lb->end &&
700 /* entries are ordered
701 * one entry is earlier than the other
702 * that earlier entry will be moved forward
704 if (la->start < lb->start)
706 /* order: A B, move A forward
707 * check if we hit the end with A
712 else if (lb->start < la->start)
714 /* order: B A, move B forward
715 * check if we hit the end with B
724 /***********************************************************************
728 bool ir_block_create_store_op(ir_block *self, int op, ir_value *target, ir_value *what)
730 if (target->store == store_value) {
731 fprintf(stderr, "cannot store to an SSA value\n");
732 fprintf(stderr, "trying to store: %s <- %s\n", target->name, what->name);
735 ir_instr *in = ir_instr_new(self, op);
738 if (!ir_instr_op(in, 0, target, true) ||
739 !ir_instr_op(in, 1, what, false) ||
740 !ir_block_instr_add(self, in) )
748 bool ir_block_create_store(ir_block *self, ir_value *target, ir_value *what)
752 if (target->vtype == TYPE_VARIANT)
755 vtype = target->vtype;
760 if (what->vtype == TYPE_INTEGER)
761 op = INSTR_CONV_ITOF;
770 op = INSTR_STORE_ENT;
776 op = INSTR_STORE_FLD;
780 if (what->vtype == TYPE_INTEGER)
781 op = INSTR_CONV_FTOI;
790 op = INSTR_STORE_ENT;
797 return ir_block_create_store_op(self, op, target, what);
800 bool ir_block_create_storep(ir_block *self, ir_value *target, ir_value *what)
805 if (target->vtype != TYPE_POINTER)
808 /* storing using pointer - target is a pointer, type must be
809 * inferred from source
821 op = INSTR_STOREP_ENT;
827 op = INSTR_STOREP_FLD;
838 op = INSTR_STOREP_ENT;
845 return ir_block_create_store_op(self, op, target, what);
848 bool ir_block_create_return(ir_block *self, ir_value *v)
852 fprintf(stderr, "block already ended (%s)\n", self->label);
856 self->is_return = true;
857 in = ir_instr_new(self, INSTR_RETURN);
861 if (!ir_instr_op(in, 0, v, false) ||
862 !ir_block_instr_add(self, in) )
869 bool ir_block_create_if(ir_block *self, ir_value *v,
870 ir_block *ontrue, ir_block *onfalse)
874 fprintf(stderr, "block already ended (%s)\n", self->label);
878 /*in = ir_instr_new(self, (v->vtype == TYPE_STRING ? INSTR_IF_S : INSTR_IF_F));*/
879 in = ir_instr_new(self, VINSTR_COND);
883 if (!ir_instr_op(in, 0, v, false)) {
888 in->bops[0] = ontrue;
889 in->bops[1] = onfalse;
891 if (!ir_block_instr_add(self, in))
894 if (!ir_block_exits_add(self, ontrue) ||
895 !ir_block_exits_add(self, onfalse) ||
896 !ir_block_entries_add(ontrue, self) ||
897 !ir_block_entries_add(onfalse, self) )
904 bool ir_block_create_jump(ir_block *self, ir_block *to)
908 fprintf(stderr, "block already ended (%s)\n", self->label);
912 in = ir_instr_new(self, VINSTR_JUMP);
917 if (!ir_block_instr_add(self, in))
920 if (!ir_block_exits_add(self, to) ||
921 !ir_block_entries_add(to, self) )
928 bool ir_block_create_goto(ir_block *self, ir_block *to)
932 fprintf(stderr, "block already ended (%s)\n", self->label);
936 in = ir_instr_new(self, INSTR_GOTO);
941 if (!ir_block_instr_add(self, in))
944 if (!ir_block_exits_add(self, to) ||
945 !ir_block_entries_add(to, self) )
952 ir_instr* ir_block_create_phi(ir_block *self, const char *label, int ot)
956 in = ir_instr_new(self, VINSTR_PHI);
959 out = ir_value_out(self->owner, label, store_value, ot);
964 if (!ir_instr_op(in, 0, out, true)) {
966 ir_value_delete(out);
969 if (!ir_block_instr_add(self, in)) {
971 ir_value_delete(out);
977 ir_value* ir_phi_value(ir_instr *self)
979 return self->_ops[0];
982 bool ir_phi_add(ir_instr* self, ir_block *b, ir_value *v)
986 if (!ir_block_entries_find(self->owner, b, NULL)) {
987 /* Must not be possible to cause this, otherwise the AST
988 * is doing something wrong.
990 fprintf(stderr, "Invalid entry block for PHI\n");
996 if (!ir_value_reads_add(v, self))
998 return ir_instr_phi_add(self, pe);
1001 /* binary op related code */
1003 ir_value* ir_block_create_binop(ir_block *self,
1004 const char *label, int opcode,
1005 ir_value *left, ir_value *right)
1027 case INSTR_SUB_S: /* -- offset of string as float */
1032 case INSTR_BITOR_IF:
1033 case INSTR_BITOR_FI:
1034 case INSTR_BITAND_FI:
1035 case INSTR_BITAND_IF:
1050 case INSTR_BITAND_I:
1053 case INSTR_RSHIFT_I:
1054 case INSTR_LSHIFT_I:
1076 /* boolean operations result in floats */
1077 if (opcode >= INSTR_EQ_F && opcode <= INSTR_GT)
1079 else if (opcode >= INSTR_LE && opcode <= INSTR_GT)
1082 else if (opcode >= INSTR_LE_I && opcode <= INSTR_EQ_FI)
1087 if (ot == TYPE_VOID) {
1088 /* The AST or parser were supposed to check this! */
1092 return ir_block_create_general_instr(self, label, opcode, left, right, ot);
1095 ir_value* ir_block_create_general_instr(ir_block *self, const char *label,
1096 int op, ir_value *a, ir_value *b, int outype)
1101 out = ir_value_out(self->owner, label, store_value, outype);
1105 instr = ir_instr_new(self, op);
1107 ir_value_delete(out);
1111 if (!ir_instr_op(instr, 0, out, true) ||
1112 !ir_instr_op(instr, 1, a, false) ||
1113 !ir_instr_op(instr, 2, b, false) )
1118 if (!ir_block_instr_add(self, instr))
1123 ir_instr_delete(instr);
1124 ir_value_delete(out);
1128 ir_value* ir_block_create_fieldaddress(ir_block *self, const char *label, ir_value *ent, ir_value *field)
1130 /* Support for various pointer types todo if so desired */
1131 if (ent->vtype != TYPE_ENTITY)
1134 if (field->vtype != TYPE_FIELD)
1137 return ir_block_create_general_instr(self, label, INSTR_ADDRESS, ent, field, TYPE_POINTER);
1140 ir_value* ir_block_create_load_from_ent(ir_block *self, const char *label, ir_value *ent, ir_value *field, int outype)
1143 if (ent->vtype != TYPE_ENTITY)
1146 /* at some point we could redirect for TYPE_POINTER... but that could lead to carelessness */
1147 if (field->vtype != TYPE_FIELD)
1152 case TYPE_FLOAT: op = INSTR_LOAD_F; break;
1153 case TYPE_VECTOR: op = INSTR_LOAD_V; break;
1154 case TYPE_STRING: op = INSTR_LOAD_S; break;
1155 case TYPE_FIELD: op = INSTR_LOAD_FLD; break;
1156 case TYPE_ENTITY: op = INSTR_LOAD_ENT; break;
1158 case TYPE_POINTER: op = INSTR_LOAD_I; break;
1159 case TYPE_INTEGER: op = INSTR_LOAD_I; break;
1165 return ir_block_create_general_instr(self, label, op, ent, field, outype);
1168 ir_value* ir_block_create_add(ir_block *self,
1170 ir_value *left, ir_value *right)
1173 int l = left->vtype;
1174 int r = right->vtype;
1193 if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1195 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1201 return ir_block_create_binop(self, label, op, left, right);
1204 ir_value* ir_block_create_sub(ir_block *self,
1206 ir_value *left, ir_value *right)
1209 int l = left->vtype;
1210 int r = right->vtype;
1230 if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1232 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1238 return ir_block_create_binop(self, label, op, left, right);
1241 ir_value* ir_block_create_mul(ir_block *self,
1243 ir_value *left, ir_value *right)
1246 int l = left->vtype;
1247 int r = right->vtype;
1266 if ( (l == TYPE_VECTOR && r == TYPE_FLOAT) )
1268 else if ( (l == TYPE_FLOAT && r == TYPE_VECTOR) )
1271 else if ( (l == TYPE_VECTOR && r == TYPE_INTEGER) )
1273 else if ( (l == TYPE_INTEGER && r == TYPE_VECTOR) )
1275 else if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1277 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1283 return ir_block_create_binop(self, label, op, left, right);
1286 ir_value* ir_block_create_div(ir_block *self,
1288 ir_value *left, ir_value *right)
1291 int l = left->vtype;
1292 int r = right->vtype;
1309 if ( (l == TYPE_VECTOR && r == TYPE_FLOAT) )
1311 else if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1313 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1319 return ir_block_create_binop(self, label, op, left, right);
1322 /* PHI resolving breaks the SSA, and must thus be the last
1323 * step before life-range calculation.
1326 static bool ir_block_naive_phi(ir_block *self);
1327 bool ir_function_naive_phi(ir_function *self)
1331 for (i = 0; i < self->blocks_count; ++i)
1333 if (!ir_block_naive_phi(self->blocks[i]))
1339 static bool ir_naive_phi_emit_store(ir_block *block, size_t iid, ir_value *old, ir_value *what)
1344 /* create a store */
1345 if (!ir_block_create_store(block, old, what))
1348 /* we now move it up */
1349 instr = block->instr[block->instr_count-1];
1350 for (i = block->instr_count; i > iid; --i)
1351 block->instr[i] = block->instr[i-1];
1352 block->instr[i] = instr;
1357 static bool ir_block_naive_phi(ir_block *self)
1360 /* FIXME: optionally, create_phi can add the phis
1361 * to a list so we don't need to loop through blocks
1362 * - anyway: "don't optimize YET"
1364 for (i = 0; i < self->instr_count; ++i)
1366 ir_instr *instr = self->instr[i];
1367 if (instr->opcode != VINSTR_PHI)
1370 if (!ir_block_instr_remove(self, i))
1372 --i; /* NOTE: i+1 below */
1374 for (p = 0; p < instr->phi_count; ++p)
1376 ir_value *v = instr->phi[p].value;
1377 for (w = 0; w < v->writes_count; ++w) {
1380 if (!v->writes[w]->_ops[0])
1383 /* When the write was to a global, we have to emit a mov */
1384 old = v->writes[w]->_ops[0];
1386 /* The original instruction now writes to the PHI target local */
1387 if (v->writes[w]->_ops[0] == v)
1388 v->writes[w]->_ops[0] = instr->_ops[0];
1390 if (old->store != store_value && old->store != store_local)
1392 /* If it originally wrote to a global we need to store the value
1395 if (!ir_naive_phi_emit_store(self, i+1, old, v))
1397 if (i+1 < self->instr_count)
1398 instr = self->instr[i+1];
1401 /* In case I forget and access instr later, it'll be NULL
1402 * when it's a problem, to make sure we crash, rather than accessing
1408 /* If it didn't, we can replace all reads by the phi target now. */
1410 for (r = 0; r < old->reads_count; ++r)
1413 ir_instr *ri = old->reads[r];
1414 for (op = 0; op < ri->phi_count; ++op) {
1415 if (ri->phi[op].value == old)
1416 ri->phi[op].value = v;
1418 for (op = 0; op < 3; ++op) {
1419 if (ri->_ops[op] == old)
1426 ir_instr_delete(instr);
1431 /***********************************************************************
1432 *IR Temp allocation code
1433 * Propagating value life ranges by walking through the function backwards
1434 * until no more changes are made.
1435 * In theory this should happen once more than once for every nested loop
1437 * Though this implementation might run an additional time for if nests.
1446 MEM_VEC_FUNCTIONS_ALL(new_reads_t, ir_value*, v)
1448 /* Enumerate instructions used by value's life-ranges
1450 static void ir_block_enumerate(ir_block *self, size_t *_eid)
1454 for (i = 0; i < self->instr_count; ++i)
1456 self->instr[i]->eid = eid++;
1461 /* Enumerate blocks and instructions.
1462 * The block-enumeration is unordered!
1463 * We do not really use the block enumreation, however
1464 * the instruction enumeration is important for life-ranges.
1466 void ir_function_enumerate(ir_function *self)
1469 size_t instruction_id = 0;
1470 for (i = 0; i < self->blocks_count; ++i)
1472 self->blocks[i]->eid = i;
1473 self->blocks[i]->run_id = 0;
1474 ir_block_enumerate(self->blocks[i], &instruction_id);
1478 static bool ir_block_life_propagate(ir_block *b, ir_block *prev, bool *changed);
1479 bool ir_function_calculate_liferanges(ir_function *self)
1487 for (i = 0; i != self->blocks_count; ++i)
1489 if (self->blocks[i]->is_return)
1491 if (!ir_block_life_propagate(self->blocks[i], NULL, &changed))
1499 /* Local-value allocator
1500 * After finishing creating the liferange of all values used in a function
1501 * we can allocate their global-positions.
1502 * This is the counterpart to register-allocation in register machines.
1505 MEM_VECTOR_MAKE(ir_value*, locals);
1506 MEM_VECTOR_MAKE(size_t, sizes);
1507 MEM_VECTOR_MAKE(size_t, positions);
1508 } function_allocator;
1509 MEM_VEC_FUNCTIONS(function_allocator, ir_value*, locals)
1510 MEM_VEC_FUNCTIONS(function_allocator, size_t, sizes)
1511 MEM_VEC_FUNCTIONS(function_allocator, size_t, positions)
1513 static bool function_allocator_alloc(function_allocator *alloc, const ir_value *var)
1518 slot = ir_value_var("reg", store_global, var->vtype);
1522 if (slot->vtype == TYPE_VECTOR || slot->vtype == TYPE_VARIANT)
1525 if (!ir_value_life_merge_into(slot, var))
1528 if (!function_allocator_locals_add(alloc, slot))
1531 if (!function_allocator_sizes_add(alloc, vsize))
1537 ir_value_delete(slot);
1541 bool ir_function_allocate_locals(ir_function *self)
1550 function_allocator alloc;
1552 MEM_VECTOR_INIT(&alloc, locals);
1553 MEM_VECTOR_INIT(&alloc, sizes);
1554 MEM_VECTOR_INIT(&alloc, positions);
1556 for (i = 0; i < self->locals_count; ++i)
1558 if (!function_allocator_alloc(&alloc, self->locals[i]))
1562 /* Allocate a slot for any value that still exists */
1563 for (i = 0; i < self->values_count; ++i)
1565 v = self->values[i];
1570 for (a = 0; a < alloc.locals_count; ++a)
1572 slot = alloc.locals[a];
1574 if (ir_values_overlap(v, slot)) {
1580 if (!ir_value_life_merge_into(slot, v))
1583 /* adjust size for this slot */
1584 if (v->vtype == TYPE_VECTOR || v->vtype == TYPE_VARIANT)
1587 self->values[i]->code.slot = a;
1590 if (a >= alloc.locals_count) {
1591 self->values[i]->code.slot = alloc.locals_count;
1592 if (!function_allocator_alloc(&alloc, v))
1597 /* Adjust slot positions based on sizes */
1598 if (!function_allocator_positions_add(&alloc, 0))
1601 for (i = 1; i < alloc.sizes_count; ++i)
1603 pos = alloc.positions[i-1] + alloc.sizes[i-1];
1604 if (!function_allocator_positions_add(&alloc, pos))
1608 /* Take over the actual slot positions */
1609 for (i = 0; i < self->values_count; ++i)
1610 self->values[i]->code.slot = alloc.positions[self->values[i]->code.slot];
1612 for (i = 0; i < self->values_count; ++i)
1613 printf("Value %s at slot %i\n", self->values[i]->name,
1614 self->values[i]->code.slot);
1620 for (i = 0; i < alloc.locals_count; ++i)
1621 ir_value_delete(alloc.locals[i]);
1622 MEM_VECTOR_CLEAR(&alloc, locals);
1623 MEM_VECTOR_CLEAR(&alloc, sizes);
1624 MEM_VECTOR_CLEAR(&alloc, positions);
1628 /* Get information about which operand
1629 * is read from, or written to.
1631 static void ir_op_read_write(int op, size_t *read, size_t *write)
1658 static bool ir_block_living_add_instr(ir_block *self, size_t eid)
1661 bool changed = false;
1663 for (i = 0; i != self->living_count; ++i)
1665 tempbool = ir_value_life_merge(self->living[i], eid);
1668 fprintf(stderr, "block_living_add_instr() value instruction added %s: %i\n", self->living[i]->_name, (int)eid);
1670 changed = changed || tempbool;
1675 static bool ir_block_life_prop_previous(ir_block* self, ir_block *prev, bool *changed)
1678 /* values which have been read in a previous iteration are now
1679 * in the "living" array even if the previous block doesn't use them.
1680 * So we have to remove whatever does not exist in the previous block.
1681 * They will be re-added on-read, but the liferange merge won't cause
1684 for (i = 0; i < self->living_count; ++i)
1686 if (!ir_block_living_find(prev, self->living[i], NULL)) {
1687 if (!ir_block_living_remove(self, i))
1693 /* Whatever the previous block still has in its living set
1694 * must now be added to ours as well.
1696 for (i = 0; i < prev->living_count; ++i)
1698 if (ir_block_living_find(self, prev->living[i], NULL))
1700 if (!ir_block_living_add(self, prev->living[i]))
1703 printf("%s got from prev: %s\n", self->label, prev->living[i]->_name);
1709 static bool ir_block_life_propagate(ir_block *self, ir_block *prev, bool *changed)
1715 /* bitmasks which operands are read from or written to */
1717 #if defined(LIFE_RANGE_WITHOUT_LAST_READ)
1719 new_reads_t new_reads;
1721 char dbg_ind[16] = { '#', '0' };
1724 #if defined(LIFE_RANGE_WITHOUT_LAST_READ)
1725 MEM_VECTOR_INIT(&new_reads, v);
1730 if (!ir_block_life_prop_previous(self, prev, changed))
1734 i = self->instr_count;
1737 instr = self->instr[i];
1739 /* PHI operands are always read operands */
1740 for (p = 0; p < instr->phi_count; ++p)
1742 value = instr->phi[p].value;
1743 #if ! defined(LIFE_RANGE_WITHOUT_LAST_READ)
1744 if (!ir_block_living_find(self, value, NULL) &&
1745 !ir_block_living_add(self, value))
1750 if (!new_reads_t_v_find(&new_reads, value, NULL))
1752 if (!new_reads_t_v_add(&new_reads, value))
1758 /* See which operands are read and write operands */
1759 ir_op_read_write(instr->opcode, &read, &write);
1761 /* Go through the 3 main operands */
1762 for (o = 0; o < 3; ++o)
1764 if (!instr->_ops[o]) /* no such operand */
1767 value = instr->_ops[o];
1769 /* We only care about locals */
1770 if (value->store != store_value &&
1771 value->store != store_local)
1777 #if ! defined(LIFE_RANGE_WITHOUT_LAST_READ)
1778 if (!ir_block_living_find(self, value, NULL) &&
1779 !ir_block_living_add(self, value))
1784 /* fprintf(stderr, "read: %s\n", value->_name); */
1785 if (!new_reads_t_v_find(&new_reads, value, NULL))
1787 if (!new_reads_t_v_add(&new_reads, value))
1793 /* write operands */
1794 /* When we write to a local, we consider it "dead" for the
1795 * remaining upper part of the function, since in SSA a value
1796 * can only be written once (== created)
1801 bool in_living = ir_block_living_find(self, value, &idx);
1802 #if defined(LIFE_RANGE_WITHOUT_LAST_READ)
1804 bool in_reads = new_reads_t_v_find(&new_reads, value, &readidx);
1805 if (!in_living && !in_reads)
1810 /* If the value isn't alive it hasn't been read before... */
1811 /* TODO: See if the warning can be emitted during parsing or AST processing
1812 * otherwise have warning printed here.
1813 * IF printing a warning here: include filecontext_t,
1814 * and make sure it's only printed once
1815 * since this function is run multiple times.
1817 /* For now: debug info: */
1818 fprintf(stderr, "Value only written %s\n", value->name);
1819 tempbool = ir_value_life_merge(value, instr->eid);
1820 *changed = *changed || tempbool;
1822 ir_instr_dump(instr, dbg_ind, printf);
1826 /* since 'living' won't contain it
1827 * anymore, merge the value, since
1830 tempbool = ir_value_life_merge(value, instr->eid);
1833 fprintf(stderr, "value added id %s %i\n", value->name, (int)instr->eid);
1835 *changed = *changed || tempbool;
1837 #if ! defined(LIFE_RANGE_WITHOUT_LAST_READ)
1838 if (!ir_block_living_remove(self, idx))
1843 if (!new_reads_t_v_remove(&new_reads, readidx))
1851 tempbool = ir_block_living_add_instr(self, instr->eid);
1852 /*fprintf(stderr, "living added values\n");*/
1853 *changed = *changed || tempbool;
1855 #if defined(LIFE_RANGE_WITHOUT_LAST_READ)
1857 for (rd = 0; rd < new_reads.v_count; ++rd)
1859 if (!ir_block_living_find(self, new_reads.v[rd], NULL)) {
1860 if (!ir_block_living_add(self, new_reads.v[rd]))
1863 if (!i && !self->entries_count) {
1865 *changed = *changed || ir_value_life_merge(new_reads.v[rd], instr->eid);
1868 MEM_VECTOR_CLEAR(&new_reads, v);
1872 if (self->run_id == self->owner->run_id)
1875 self->run_id = self->owner->run_id;
1877 for (i = 0; i < self->entries_count; ++i)
1879 ir_block *entry = self->entries[i];
1880 ir_block_life_propagate(entry, self, changed);
1885 #if defined(LIFE_RANGE_WITHOUT_LAST_READ)
1886 MEM_VECTOR_CLEAR(&new_reads, v);
1891 /***********************************************************************
1894 * Since the IR has the convention of putting 'write' operands
1895 * at the beginning, we have to rotate the operands of instructions
1896 * properly in order to generate valid QCVM code.
1898 * Having destinations at a fixed position is more convenient. In QC
1899 * this is *mostly* OPC, but FTE adds at least 2 instructions which
1900 * read from from OPA, and store to OPB rather than OPC. Which is
1901 * partially the reason why the implementation of these instructions
1902 * in darkplaces has been delayed for so long.
1904 * Breaking conventions is annoying...
1906 static bool ir_builder_gen_global(ir_builder *self, ir_value *global);
1908 static bool gen_global_field(ir_value *global)
1910 if (global->isconst)
1912 ir_value *fld = global->constval.vpointer;
1914 printf("Invalid field constant with no field: %s\n", global->name);
1918 /* Now, in this case, a relocation would be impossible to code
1919 * since it looks like this:
1920 * .vector v = origin; <- parse error, wtf is 'origin'?
1923 * But we will need a general relocation support later anyway
1924 * for functions... might as well support that here.
1926 if (!fld->code.globaladdr) {
1927 printf("FIXME: Relocation support\n");
1931 /* copy the field's value */
1932 global->code.globaladdr = code_globals_add(code_globals_data[fld->code.globaladdr]);
1936 prog_section_field fld;
1938 fld.name = global->code.name;
1939 fld.offset = code_fields_elements;
1940 fld.type = global->fieldtype;
1942 if (fld.type == TYPE_VOID) {
1943 printf("Field is missing a type: %s\n", global->name);
1947 if (code_fields_add(fld) < 0)
1950 global->code.globaladdr = code_globals_add(fld.offset);
1952 if (global->code.globaladdr < 0)
1957 static bool gen_global_pointer(ir_value *global)
1959 if (global->isconst)
1961 ir_value *target = global->constval.vpointer;
1963 printf("Invalid pointer constant: %s\n", global->name);
1964 /* NULL pointers are pointing to the NULL constant, which also
1965 * sits at address 0, but still has an ir_value for itself.
1970 /* Here, relocations ARE possible - in fteqcc-enhanced-qc:
1971 * void() foo; <- proto
1972 * void() *fooptr = &foo;
1973 * void() foo = { code }
1975 if (!target->code.globaladdr) {
1976 /* FIXME: Check for the constant nullptr ir_value!
1977 * because then code.globaladdr being 0 is valid.
1979 printf("FIXME: Relocation support\n");
1983 global->code.globaladdr = code_globals_add(target->code.globaladdr);
1987 global->code.globaladdr = code_globals_add(0);
1989 if (global->code.globaladdr < 0)
1994 static bool gen_blocks_recursive(ir_function *func, ir_block *block)
1996 prog_section_statement stmt;
1997 prog_section_statement *stptr;
2006 block->generated = true;
2007 block->code_start = code_statements_elements;
2008 for (i = 0; i < block->instr_count; ++i)
2010 instr = block->instr[i];
2012 if (instr->opcode == VINSTR_PHI) {
2013 printf("cannot generate virtual instruction (phi)\n");
2017 if (instr->opcode == VINSTR_JUMP) {
2018 target = instr->bops[0];
2019 /* for uncoditional jumps, if the target hasn't been generated
2020 * yet, we generate them right here.
2022 if (!target->generated) {
2027 /* otherwise we generate a jump instruction */
2028 stmt.opcode = INSTR_GOTO;
2029 stmt.o1.s1 = (target->code_start-1) - code_statements_elements;
2032 if (code_statements_add(stmt) < 0)
2035 /* no further instructions can be in this block */
2039 if (instr->opcode == VINSTR_COND) {
2040 ontrue = instr->bops[0];
2041 onfalse = instr->bops[1];
2042 /* TODO: have the AST signal which block should
2043 * come first: eg. optimize IFs without ELSE...
2046 stmt.o1.u1 = instr->_ops[0]->code.globaladdr;
2049 if (ontrue->generated) {
2050 stmt.opcode = INSTR_IF;
2051 stmt.o2.s1 = (ontrue->code_start-1) - code_statements_elements;
2052 if (code_statements_add(stmt) < 0)
2055 if (onfalse->generated) {
2056 stmt.opcode = INSTR_IFNOT;
2057 stmt.o2.s1 = (onfalse->code_start-1) - code_statements_elements;
2058 if (code_statements_add(stmt) < 0)
2061 if (!ontrue->generated) {
2062 if (onfalse->generated) {
2067 if (!onfalse->generated) {
2068 if (ontrue->generated) {
2073 /* neither ontrue nor onfalse exist */
2074 stmt.opcode = INSTR_IFNOT;
2075 stidx = code_statements_elements - 1;
2076 if (code_statements_add(stmt) < 0)
2078 stptr = &code_statements_data[stidx];
2079 /* on false we jump, so add ontrue-path */
2080 if (!gen_blocks_recursive(func, ontrue))
2082 /* fixup the jump address */
2083 stptr->o2.s1 = (ontrue->code_start-1) - (stidx+1);
2084 /* generate onfalse path */
2085 if (onfalse->generated) {
2086 /* may have been generated in the previous recursive call */
2087 stmt.opcode = INSTR_GOTO;
2090 stmt.o1.s1 = (onfalse->code_start-1) - code_statements_elements;
2091 return (code_statements_add(stmt) >= 0);
2093 /* if not, generate now */
2098 if (instr->opcode >= INSTR_CALL0 && instr->opcode <= INSTR_CALL8) {
2099 printf("TODO: call instruction\n");
2103 if (instr->opcode == INSTR_STATE) {
2104 printf("TODO: state instruction\n");
2108 stmt.opcode = instr->opcode;
2113 /* This is the general order of operands */
2115 stmt.o3.u1 = instr->_ops[0]->code.globaladdr;
2118 stmt.o1.u1 = instr->_ops[1]->code.globaladdr;
2121 stmt.o2.u1 = instr->_ops[2]->code.globaladdr;
2123 if (stmt.opcode == INSTR_RETURN || stmt.opcode == INSTR_DONE)
2125 stmt.o1.u1 = stmt.o3.u1;
2128 else if ((stmt.opcode >= INSTR_STORE_F &&
2129 stmt.opcode <= INSTR_STORE_FNC) ||
2130 (stmt.opcode >= INSTR_NOT_F &&
2131 stmt.opcode <= INSTR_NOT_FNC))
2133 /* 2-operand instructions with A -> B */
2134 stmt.o2.u1 = stmt.o3.u1;
2138 if (code_statements_add(stmt) < 0)
2144 static bool gen_function_code(ir_function *self)
2148 /* Starting from entry point, we generate blocks "as they come"
2149 * for now. Dead blocks will not be translated obviously.
2151 if (!self->blocks_count) {
2152 printf("Function '%s' declared without body.\n", self->name);
2156 block = self->blocks[0];
2157 if (block->generated)
2160 if (!gen_blocks_recursive(self, block)) {
2161 printf("failed to generate blocks for '%s'\n", self->name);
2167 static bool gen_global_function(ir_builder *ir, ir_value *global)
2169 prog_section_function fun;
2174 if (!global->isconst ||
2175 !global->constval.vfunc)
2177 printf("Invalid state of function-global: not constant: %s\n", global->name);
2181 irfun = global->constval.vfunc;
2183 fun.name = global->code.name;
2184 fun.file = code_cachedstring(global->context.file);
2185 fun.profile = 0; /* always 0 */
2186 fun.nargs = irfun->params_count;
2188 for (i = 0;i < 8; ++i) {
2191 else if (irfun->params[i] == TYPE_VECTOR)
2197 fun.firstlocal = code_globals_elements;
2198 fun.locals = irfun->locals_count;
2199 for (i = 0; i < irfun->locals_count; ++i) {
2200 if (!ir_builder_gen_global(ir, irfun->locals[i])) {
2201 printf("Failed to generate global %s\n", irfun->locals[i]->name);
2206 fun.entry = code_statements_elements;
2207 if (!gen_function_code(irfun)) {
2208 printf("Failed to generate code for function %s\n", irfun->name);
2212 return (code_functions_add(fun) >= 0);
2215 static bool ir_builder_gen_global(ir_builder *self, ir_value *global)
2218 prog_section_def def;
2220 def.type = global->vtype;
2221 def.offset = code_globals_elements;
2222 def.name = global->code.name = code_genstring(global->name);
2224 switch (global->vtype)
2227 if (code_defs_add(def) < 0)
2229 return gen_global_pointer(global);
2231 if (code_defs_add(def) < 0)
2233 return gen_global_field(global);
2238 if (code_defs_add(def) < 0)
2241 if (global->isconst) {
2242 iptr = (int32_t*)&global->constval.vfloat;
2243 global->code.globaladdr = code_globals_add(*iptr);
2245 global->code.globaladdr = code_globals_add(0);
2247 return global->code.globaladdr >= 0;
2251 if (code_defs_add(def) < 0)
2253 if (global->isconst)
2254 global->code.globaladdr = code_globals_add(code_cachedstring(global->constval.vstring));
2256 global->code.globaladdr = code_globals_add(0);
2257 return global->code.globaladdr >= 0;
2261 if (code_defs_add(def) < 0)
2264 if (global->isconst) {
2265 iptr = (int32_t*)&global->constval.vvec;
2266 global->code.globaladdr = code_globals_add(iptr[0]);
2267 if (code_globals_add(iptr[1]) < 0 || code_globals_add(iptr[2]) < 0)
2270 global->code.globaladdr = code_globals_add(0);
2271 if (code_globals_add(0) < 0 || code_globals_add(0) < 0)
2274 return global->code.globaladdr >= 0;
2277 if (code_defs_add(def) < 0)
2279 code_globals_add(code_functions_elements);
2280 return gen_global_function(self, global);
2282 /* assume biggest type */
2283 global->code.globaladdr = code_globals_add(0);
2284 code_globals_add(0);
2285 code_globals_add(0);
2288 /* refuse to create 'void' type or any other fancy business. */
2289 printf("Invalid type for global variable %s\n", global->name);
2294 bool ir_builder_generate(ir_builder *self, const char *filename)
2300 /* FIXME: generate TYPE_FUNCTION globals and link them
2301 * to their ir_function.
2304 for (i = 0; i < self->functions_count; ++i)
2307 ir_function *fun = self->functions[i];
2309 funval = ir_builder_create_global(self, fun->name, TYPE_FUNCTION);
2310 funval->isconst = true;
2311 funval->constval.vfunc = fun;
2312 funval->context = fun->context;
2315 for (i = 0; i < self->globals_count; ++i)
2317 if (!ir_builder_gen_global(self, self->globals[i])) {
2322 printf("writing '%s'...\n", filename);
2323 return code_write(filename);
2326 /***********************************************************************
2327 *IR DEBUG Dump functions...
2330 #define IND_BUFSZ 1024
2332 const char *qc_opname(int op)
2334 if (op < 0) return "<INVALID>";
2335 if (op < ( sizeof(asm_instr) / sizeof(asm_instr[0]) ))
2336 return asm_instr[op].m;
2338 case VINSTR_PHI: return "PHI";
2339 case VINSTR_JUMP: return "JUMP";
2340 case VINSTR_COND: return "COND";
2341 default: return "<UNK>";
2345 void ir_builder_dump(ir_builder *b, int (*oprintf)(const char*, ...))
2348 char indent[IND_BUFSZ];
2352 oprintf("module %s\n", b->name);
2353 for (i = 0; i < b->globals_count; ++i)
2356 if (b->globals[i]->isconst)
2357 oprintf("%s = ", b->globals[i]->name);
2358 ir_value_dump(b->globals[i], oprintf);
2361 for (i = 0; i < b->functions_count; ++i)
2362 ir_function_dump(b->functions[i], indent, oprintf);
2363 oprintf("endmodule %s\n", b->name);
2366 void ir_function_dump(ir_function *f, char *ind,
2367 int (*oprintf)(const char*, ...))
2370 oprintf("%sfunction %s\n", ind, f->name);
2371 strncat(ind, "\t", IND_BUFSZ);
2372 if (f->locals_count)
2374 oprintf("%s%i locals:\n", ind, (int)f->locals_count);
2375 for (i = 0; i < f->locals_count; ++i) {
2376 oprintf("%s\t", ind);
2377 ir_value_dump(f->locals[i], oprintf);
2381 if (f->blocks_count)
2383 oprintf("%slife passes (check): %i\n", ind, (int)f->run_id);
2384 for (i = 0; i < f->blocks_count; ++i) {
2385 if (f->blocks[i]->run_id != f->run_id) {
2386 oprintf("%slife pass check fail! %i != %i\n", ind, (int)f->blocks[i]->run_id, (int)f->run_id);
2388 ir_block_dump(f->blocks[i], ind, oprintf);
2392 ind[strlen(ind)-1] = 0;
2393 oprintf("%sendfunction %s\n", ind, f->name);
2396 void ir_block_dump(ir_block* b, char *ind,
2397 int (*oprintf)(const char*, ...))
2400 oprintf("%s:%s\n", ind, b->label);
2401 strncat(ind, "\t", IND_BUFSZ);
2403 for (i = 0; i < b->instr_count; ++i)
2404 ir_instr_dump(b->instr[i], ind, oprintf);
2405 ind[strlen(ind)-1] = 0;
2408 void dump_phi(ir_instr *in, char *ind,
2409 int (*oprintf)(const char*, ...))
2412 oprintf("%s <- phi ", in->_ops[0]->name);
2413 for (i = 0; i < in->phi_count; ++i)
2415 oprintf("([%s] : %s) ", in->phi[i].from->label,
2416 in->phi[i].value->name);
2421 void ir_instr_dump(ir_instr *in, char *ind,
2422 int (*oprintf)(const char*, ...))
2425 const char *comma = NULL;
2427 oprintf("%s (%i) ", ind, (int)in->eid);
2429 if (in->opcode == VINSTR_PHI) {
2430 dump_phi(in, ind, oprintf);
2434 strncat(ind, "\t", IND_BUFSZ);
2436 if (in->_ops[0] && (in->_ops[1] || in->_ops[2])) {
2437 ir_value_dump(in->_ops[0], oprintf);
2438 if (in->_ops[1] || in->_ops[2])
2441 oprintf("%s\t", qc_opname(in->opcode));
2442 if (in->_ops[0] && !(in->_ops[1] || in->_ops[2])) {
2443 ir_value_dump(in->_ops[0], oprintf);
2448 for (i = 1; i != 3; ++i) {
2452 ir_value_dump(in->_ops[i], oprintf);
2460 oprintf("[%s]", in->bops[0]->label);
2464 oprintf("%s[%s]", comma, in->bops[1]->label);
2466 ind[strlen(ind)-1] = 0;
2469 void ir_value_dump(ir_value* v, int (*oprintf)(const char*, ...))
2477 oprintf("%g", v->constval.vfloat);
2480 oprintf("'%g %g %g'",
2483 v->constval.vvec.z);
2486 oprintf("(entity)");
2489 oprintf("\"%s\"", v->constval.vstring);
2493 oprintf("%i", v->constval.vint);
2498 v->constval.vpointer->name);
2502 oprintf("%s", v->name);
2506 void ir_value_dump_life(ir_value *self, int (*oprintf)(const char*,...))
2509 oprintf("Life of %s:\n", self->name);
2510 for (i = 0; i < self->life_count; ++i)
2512 oprintf(" + [%i, %i]\n", self->life[i].start, self->life[i].end);