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)
441 mem_d((void*)self->name);
444 if (self->vtype == TYPE_STRING)
445 mem_d((void*)self->constval.vstring);
447 MEM_VECTOR_CLEAR(self, reads);
448 MEM_VECTOR_CLEAR(self, writes);
449 MEM_VECTOR_CLEAR(self, life);
453 void ir_value_set_name(ir_value *self, const char *name)
456 mem_d((void*)self->name);
457 self->name = util_strdup(name);
460 bool ir_value_set_float(ir_value *self, float f)
462 if (self->vtype != TYPE_FLOAT)
464 self->constval.vfloat = f;
465 self->isconst = true;
469 bool ir_value_set_vector(ir_value *self, vector v)
471 if (self->vtype != TYPE_VECTOR)
473 self->constval.vvec = v;
474 self->isconst = true;
478 bool ir_value_set_string(ir_value *self, const char *str)
480 if (self->vtype != TYPE_STRING)
482 self->constval.vstring = util_strdup(str);
483 self->isconst = true;
488 bool ir_value_set_int(ir_value *self, int i)
490 if (self->vtype != TYPE_INTEGER)
492 self->constval.vint = i;
493 self->isconst = true;
498 bool ir_value_lives(ir_value *self, size_t at)
501 for (i = 0; i < self->life_count; ++i)
503 ir_life_entry_t *life = &self->life[i];
504 if (life->start <= at && at <= life->end)
506 if (life->start > at) /* since it's ordered */
512 bool ir_value_life_insert(ir_value *self, size_t idx, ir_life_entry_t e)
515 if (!ir_value_life_add(self, e)) /* naive... */
517 for (k = self->life_count-1; k > idx; --k)
518 self->life[k] = self->life[k-1];
523 bool ir_value_life_merge(ir_value *self, size_t s)
526 ir_life_entry_t *life = NULL;
527 ir_life_entry_t *before = NULL;
528 ir_life_entry_t new_entry;
530 /* Find the first range >= s */
531 for (i = 0; i < self->life_count; ++i)
534 life = &self->life[i];
538 /* nothing found? append */
539 if (i == self->life_count) {
541 if (life && life->end+1 == s)
543 /* previous life range can be merged in */
547 if (life && life->end >= s)
550 if (!ir_value_life_add(self, e))
551 return false; /* failing */
557 if (before->end + 1 == s &&
558 life->start - 1 == s)
561 before->end = life->end;
562 if (!ir_value_life_remove(self, i))
563 return false; /* failing */
566 if (before->end + 1 == s)
572 /* already contained */
573 if (before->end >= s)
577 if (life->start - 1 == s)
582 /* insert a new entry */
583 new_entry.start = new_entry.end = s;
584 return ir_value_life_insert(self, i, new_entry);
587 bool ir_value_life_merge_into(ir_value *self, const ir_value *other)
591 if (!other->life_count)
594 if (!self->life_count) {
595 for (i = 0; i < other->life_count; ++i) {
596 if (!ir_value_life_add(self, other->life[i]))
603 for (i = 0; i < other->life_count; ++i)
605 const ir_life_entry_t *life = &other->life[i];
608 ir_life_entry_t *entry = &self->life[myi];
610 if (life->end+1 < entry->start)
612 /* adding an interval before entry */
613 if (!ir_value_life_insert(self, myi, *life))
619 if (life->start < entry->start &&
620 life->end >= entry->start)
622 /* starts earlier and overlaps */
623 entry->start = life->start;
626 if (life->end > entry->end &&
627 life->start-1 <= entry->end)
629 /* ends later and overlaps */
630 entry->end = life->end;
633 /* see if our change combines it with the next ranges */
634 while (myi+1 < self->life_count &&
635 entry->end+1 >= self->life[1+myi].start)
637 /* overlaps with (myi+1) */
638 if (entry->end < self->life[1+myi].end)
639 entry->end = self->life[1+myi].end;
640 if (!ir_value_life_remove(self, myi+1))
642 entry = &self->life[myi];
645 /* see if we're after the entry */
646 if (life->start > entry->end)
649 /* append if we're at the end */
650 if (myi >= self->life_count) {
651 if (!ir_value_life_add(self, *life))
655 /* otherweise check the next range */
664 bool ir_values_overlap(ir_value *a, ir_value *b)
666 /* For any life entry in A see if it overlaps with
667 * any life entry in B.
668 * Note that the life entries are orderes, so we can make a
669 * more efficient algorithm there than naively translating the
673 ir_life_entry_t *la, *lb, *enda, *endb;
675 /* first of all, if either has no life range, they cannot clash */
676 if (!a->life_count || !b->life_count)
681 enda = la + a->life_count;
682 endb = lb + b->life_count;
685 /* check if the entries overlap, for that,
686 * both must start before the other one ends.
688 #if defined(LIFE_RANGE_WITHOUT_LAST_READ)
689 if (la->start <= lb->end &&
690 lb->start <= la->end)
692 if (la->start < lb->end &&
699 /* entries are ordered
700 * one entry is earlier than the other
701 * that earlier entry will be moved forward
703 if (la->end < lb->end)
705 /* order: A B, move A forward
706 * check if we hit the end with A
711 else if (lb->end < la->end)
713 /* order: B A, move B forward
714 * check if we hit the end with B
723 /***********************************************************************
727 bool ir_block_create_store_op(ir_block *self, int op, ir_value *target, ir_value *what)
729 if (target->store == store_value) {
730 fprintf(stderr, "cannot store to an SSA value\n");
731 fprintf(stderr, "trying to store: %s <- %s\n", target->name, what->name);
734 ir_instr *in = ir_instr_new(self, op);
737 if (!ir_instr_op(in, 0, target, true) ||
738 !ir_instr_op(in, 1, what, false) ||
739 !ir_block_instr_add(self, in) )
747 bool ir_block_create_store(ir_block *self, ir_value *target, ir_value *what)
751 if (target->vtype == TYPE_VARIANT)
754 vtype = target->vtype;
759 if (what->vtype == TYPE_INTEGER)
760 op = INSTR_CONV_ITOF;
769 op = INSTR_STORE_ENT;
775 op = INSTR_STORE_FLD;
779 if (what->vtype == TYPE_INTEGER)
780 op = INSTR_CONV_FTOI;
789 op = INSTR_STORE_ENT;
796 return ir_block_create_store_op(self, op, target, what);
799 bool ir_block_create_storep(ir_block *self, ir_value *target, ir_value *what)
804 if (target->vtype != TYPE_POINTER)
807 /* storing using pointer - target is a pointer, type must be
808 * inferred from source
820 op = INSTR_STOREP_ENT;
826 op = INSTR_STOREP_FLD;
837 op = INSTR_STOREP_ENT;
844 return ir_block_create_store_op(self, op, target, what);
847 bool ir_block_create_return(ir_block *self, ir_value *v)
851 fprintf(stderr, "block already ended (%s)\n", self->label);
855 self->is_return = true;
856 in = ir_instr_new(self, INSTR_RETURN);
860 if (!ir_instr_op(in, 0, v, false) ||
861 !ir_block_instr_add(self, in) )
868 bool ir_block_create_if(ir_block *self, ir_value *v,
869 ir_block *ontrue, ir_block *onfalse)
873 fprintf(stderr, "block already ended (%s)\n", self->label);
877 /*in = ir_instr_new(self, (v->vtype == TYPE_STRING ? INSTR_IF_S : INSTR_IF_F));*/
878 in = ir_instr_new(self, VINSTR_COND);
882 if (!ir_instr_op(in, 0, v, false)) {
887 in->bops[0] = ontrue;
888 in->bops[1] = onfalse;
890 if (!ir_block_instr_add(self, in))
893 if (!ir_block_exits_add(self, ontrue) ||
894 !ir_block_exits_add(self, onfalse) ||
895 !ir_block_entries_add(ontrue, self) ||
896 !ir_block_entries_add(onfalse, self) )
903 bool ir_block_create_jump(ir_block *self, ir_block *to)
907 fprintf(stderr, "block already ended (%s)\n", self->label);
911 in = ir_instr_new(self, VINSTR_JUMP);
916 if (!ir_block_instr_add(self, in))
919 if (!ir_block_exits_add(self, to) ||
920 !ir_block_entries_add(to, self) )
927 bool ir_block_create_goto(ir_block *self, ir_block *to)
931 fprintf(stderr, "block already ended (%s)\n", self->label);
935 in = ir_instr_new(self, INSTR_GOTO);
940 if (!ir_block_instr_add(self, in))
943 if (!ir_block_exits_add(self, to) ||
944 !ir_block_entries_add(to, self) )
951 ir_instr* ir_block_create_phi(ir_block *self, const char *label, int ot)
955 in = ir_instr_new(self, VINSTR_PHI);
958 out = ir_value_out(self->owner, label, store_value, ot);
963 if (!ir_instr_op(in, 0, out, true)) {
965 ir_value_delete(out);
968 if (!ir_block_instr_add(self, in)) {
970 ir_value_delete(out);
976 ir_value* ir_phi_value(ir_instr *self)
978 return self->_ops[0];
981 bool ir_phi_add(ir_instr* self, ir_block *b, ir_value *v)
985 if (!ir_block_entries_find(self->owner, b, NULL)) {
986 /* Must not be possible to cause this, otherwise the AST
987 * is doing something wrong.
989 fprintf(stderr, "Invalid entry block for PHI\n");
995 if (!ir_value_reads_add(v, self))
997 return ir_instr_phi_add(self, pe);
1000 /* binary op related code */
1002 ir_value* ir_block_create_binop(ir_block *self,
1003 const char *label, int opcode,
1004 ir_value *left, ir_value *right)
1026 case INSTR_SUB_S: /* -- offset of string as float */
1031 case INSTR_BITOR_IF:
1032 case INSTR_BITOR_FI:
1033 case INSTR_BITAND_FI:
1034 case INSTR_BITAND_IF:
1049 case INSTR_BITAND_I:
1052 case INSTR_RSHIFT_I:
1053 case INSTR_LSHIFT_I:
1075 /* boolean operations result in floats */
1076 if (opcode >= INSTR_EQ_F && opcode <= INSTR_GT)
1078 else if (opcode >= INSTR_LE && opcode <= INSTR_GT)
1081 else if (opcode >= INSTR_LE_I && opcode <= INSTR_EQ_FI)
1086 if (ot == TYPE_VOID) {
1087 /* The AST or parser were supposed to check this! */
1091 return ir_block_create_general_instr(self, label, opcode, left, right, ot);
1094 ir_value* ir_block_create_general_instr(ir_block *self, const char *label,
1095 int op, ir_value *a, ir_value *b, int outype)
1100 out = ir_value_out(self->owner, label, store_value, outype);
1104 instr = ir_instr_new(self, op);
1106 ir_value_delete(out);
1110 if (!ir_instr_op(instr, 0, out, true) ||
1111 !ir_instr_op(instr, 1, a, false) ||
1112 !ir_instr_op(instr, 2, b, false) )
1117 if (!ir_block_instr_add(self, instr))
1122 ir_instr_delete(instr);
1123 ir_value_delete(out);
1127 ir_value* ir_block_create_fieldaddress(ir_block *self, const char *label, ir_value *ent, ir_value *field)
1129 /* Support for various pointer types todo if so desired */
1130 if (ent->vtype != TYPE_ENTITY)
1133 if (field->vtype != TYPE_FIELD)
1136 return ir_block_create_general_instr(self, label, INSTR_ADDRESS, ent, field, TYPE_POINTER);
1139 ir_value* ir_block_create_load_from_ent(ir_block *self, const char *label, ir_value *ent, ir_value *field, int outype)
1142 if (ent->vtype != TYPE_ENTITY)
1145 /* at some point we could redirect for TYPE_POINTER... but that could lead to carelessness */
1146 if (field->vtype != TYPE_FIELD)
1151 case TYPE_FLOAT: op = INSTR_LOAD_F; break;
1152 case TYPE_VECTOR: op = INSTR_LOAD_V; break;
1153 case TYPE_STRING: op = INSTR_LOAD_S; break;
1154 case TYPE_FIELD: op = INSTR_LOAD_FLD; break;
1155 case TYPE_ENTITY: op = INSTR_LOAD_ENT; break;
1157 case TYPE_POINTER: op = INSTR_LOAD_I; break;
1158 case TYPE_INTEGER: op = INSTR_LOAD_I; break;
1164 return ir_block_create_general_instr(self, label, op, ent, field, outype);
1167 ir_value* ir_block_create_add(ir_block *self,
1169 ir_value *left, ir_value *right)
1172 int l = left->vtype;
1173 int r = right->vtype;
1192 if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1194 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1200 return ir_block_create_binop(self, label, op, left, right);
1203 ir_value* ir_block_create_sub(ir_block *self,
1205 ir_value *left, ir_value *right)
1208 int l = left->vtype;
1209 int r = right->vtype;
1229 if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1231 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1237 return ir_block_create_binop(self, label, op, left, right);
1240 ir_value* ir_block_create_mul(ir_block *self,
1242 ir_value *left, ir_value *right)
1245 int l = left->vtype;
1246 int r = right->vtype;
1265 if ( (l == TYPE_VECTOR && r == TYPE_FLOAT) )
1267 else if ( (l == TYPE_FLOAT && r == TYPE_VECTOR) )
1270 else if ( (l == TYPE_VECTOR && r == TYPE_INTEGER) )
1272 else if ( (l == TYPE_INTEGER && r == TYPE_VECTOR) )
1274 else if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1276 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1282 return ir_block_create_binop(self, label, op, left, right);
1285 ir_value* ir_block_create_div(ir_block *self,
1287 ir_value *left, ir_value *right)
1290 int l = left->vtype;
1291 int r = right->vtype;
1308 if ( (l == TYPE_VECTOR && r == TYPE_FLOAT) )
1310 else if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1312 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1318 return ir_block_create_binop(self, label, op, left, right);
1321 /* PHI resolving breaks the SSA, and must thus be the last
1322 * step before life-range calculation.
1325 static bool ir_block_naive_phi(ir_block *self);
1326 bool ir_function_naive_phi(ir_function *self)
1330 for (i = 0; i < self->blocks_count; ++i)
1332 if (!ir_block_naive_phi(self->blocks[i]))
1338 static bool ir_naive_phi_emit_store(ir_block *block, size_t iid, ir_value *old, ir_value *what)
1343 /* create a store */
1344 if (!ir_block_create_store(block, old, what))
1347 /* we now move it up */
1348 instr = block->instr[block->instr_count-1];
1349 for (i = block->instr_count; i > iid; --i)
1350 block->instr[i] = block->instr[i-1];
1351 block->instr[i] = instr;
1356 static bool ir_block_naive_phi(ir_block *self)
1359 /* FIXME: optionally, create_phi can add the phis
1360 * to a list so we don't need to loop through blocks
1361 * - anyway: "don't optimize YET"
1363 for (i = 0; i < self->instr_count; ++i)
1365 ir_instr *instr = self->instr[i];
1366 if (instr->opcode != VINSTR_PHI)
1369 if (!ir_block_instr_remove(self, i))
1371 --i; /* NOTE: i+1 below */
1373 for (p = 0; p < instr->phi_count; ++p)
1375 ir_value *v = instr->phi[p].value;
1376 for (w = 0; w < v->writes_count; ++w) {
1379 if (!v->writes[w]->_ops[0])
1382 /* When the write was to a global, we have to emit a mov */
1383 old = v->writes[w]->_ops[0];
1385 /* The original instruction now writes to the PHI target local */
1386 if (v->writes[w]->_ops[0] == v)
1387 v->writes[w]->_ops[0] = instr->_ops[0];
1389 if (old->store != store_value && old->store != store_local)
1391 /* If it originally wrote to a global we need to store the value
1394 if (!ir_naive_phi_emit_store(self, i+1, old, v))
1396 if (i+1 < self->instr_count)
1397 instr = self->instr[i+1];
1400 /* In case I forget and access instr later, it'll be NULL
1401 * when it's a problem, to make sure we crash, rather than accessing
1407 /* If it didn't, we can replace all reads by the phi target now. */
1409 for (r = 0; r < old->reads_count; ++r)
1412 ir_instr *ri = old->reads[r];
1413 for (op = 0; op < ri->phi_count; ++op) {
1414 if (ri->phi[op].value == old)
1415 ri->phi[op].value = v;
1417 for (op = 0; op < 3; ++op) {
1418 if (ri->_ops[op] == old)
1425 ir_instr_delete(instr);
1430 /***********************************************************************
1431 *IR Temp allocation code
1432 * Propagating value life ranges by walking through the function backwards
1433 * until no more changes are made.
1434 * In theory this should happen once more than once for every nested loop
1436 * Though this implementation might run an additional time for if nests.
1445 MEM_VEC_FUNCTIONS_ALL(new_reads_t, ir_value*, v)
1447 /* Enumerate instructions used by value's life-ranges
1449 static void ir_block_enumerate(ir_block *self, size_t *_eid)
1453 for (i = 0; i < self->instr_count; ++i)
1455 self->instr[i]->eid = eid++;
1460 /* Enumerate blocks and instructions.
1461 * The block-enumeration is unordered!
1462 * We do not really use the block enumreation, however
1463 * the instruction enumeration is important for life-ranges.
1465 void ir_function_enumerate(ir_function *self)
1468 size_t instruction_id = 0;
1469 for (i = 0; i < self->blocks_count; ++i)
1471 self->blocks[i]->eid = i;
1472 self->blocks[i]->run_id = 0;
1473 ir_block_enumerate(self->blocks[i], &instruction_id);
1477 static bool ir_block_life_propagate(ir_block *b, ir_block *prev, bool *changed);
1478 bool ir_function_calculate_liferanges(ir_function *self)
1486 for (i = 0; i != self->blocks_count; ++i)
1488 if (self->blocks[i]->is_return)
1490 if (!ir_block_life_propagate(self->blocks[i], NULL, &changed))
1498 /* Local-value allocator
1499 * After finishing creating the liferange of all values used in a function
1500 * we can allocate their global-positions.
1501 * This is the counterpart to register-allocation in register machines.
1503 bool ir_function_allocate_locals(ir_function *self)
1508 /* Get information about which operand
1509 * is read from, or written to.
1511 static void ir_op_read_write(int op, size_t *read, size_t *write)
1538 static bool ir_block_living_add_instr(ir_block *self, size_t eid)
1541 bool changed = false;
1543 for (i = 0; i != self->living_count; ++i)
1545 tempbool = ir_value_life_merge(self->living[i], eid);
1548 fprintf(stderr, "block_living_add_instr() value instruction added %s: %i\n", self->living[i]->_name, (int)eid);
1550 changed = changed || tempbool;
1555 static bool ir_block_life_prop_previous(ir_block* self, ir_block *prev, bool *changed)
1558 /* values which have been read in a previous iteration are now
1559 * in the "living" array even if the previous block doesn't use them.
1560 * So we have to remove whatever does not exist in the previous block.
1561 * They will be re-added on-read, but the liferange merge won't cause
1564 for (i = 0; i < self->living_count; ++i)
1566 if (!ir_block_living_find(prev, self->living[i], NULL)) {
1567 if (!ir_block_living_remove(self, i))
1573 /* Whatever the previous block still has in its living set
1574 * must now be added to ours as well.
1576 for (i = 0; i < prev->living_count; ++i)
1578 if (ir_block_living_find(self, prev->living[i], NULL))
1580 if (!ir_block_living_add(self, prev->living[i]))
1583 printf("%s got from prev: %s\n", self->label, prev->living[i]->_name);
1589 static bool ir_block_life_propagate(ir_block *self, ir_block *prev, bool *changed)
1595 /* bitmasks which operands are read from or written to */
1597 #if defined(LIFE_RANGE_WITHOUT_LAST_READ)
1599 new_reads_t new_reads;
1601 char dbg_ind[16] = { '#', '0' };
1604 #if defined(LIFE_RANGE_WITHOUT_LAST_READ)
1605 MEM_VECTOR_INIT(&new_reads, v);
1610 if (!ir_block_life_prop_previous(self, prev, changed))
1614 i = self->instr_count;
1617 instr = self->instr[i];
1619 /* PHI operands are always read operands */
1620 for (p = 0; p < instr->phi_count; ++p)
1622 value = instr->phi[p].value;
1623 #if ! defined(LIFE_RANGE_WITHOUT_LAST_READ)
1624 if (!ir_block_living_find(self, value, NULL) &&
1625 !ir_block_living_add(self, value))
1630 if (!new_reads_t_v_find(&new_reads, value, NULL))
1632 if (!new_reads_t_v_add(&new_reads, value))
1638 /* See which operands are read and write operands */
1639 ir_op_read_write(instr->opcode, &read, &write);
1641 /* Go through the 3 main operands */
1642 for (o = 0; o < 3; ++o)
1644 if (!instr->_ops[o]) /* no such operand */
1647 value = instr->_ops[o];
1649 /* We only care about locals */
1650 if (value->store != store_value &&
1651 value->store != store_local)
1657 #if ! defined(LIFE_RANGE_WITHOUT_LAST_READ)
1658 if (!ir_block_living_find(self, value, NULL) &&
1659 !ir_block_living_add(self, value))
1664 /* fprintf(stderr, "read: %s\n", value->_name); */
1665 if (!new_reads_t_v_find(&new_reads, value, NULL))
1667 if (!new_reads_t_v_add(&new_reads, value))
1673 /* write operands */
1674 /* When we write to a local, we consider it "dead" for the
1675 * remaining upper part of the function, since in SSA a value
1676 * can only be written once (== created)
1681 bool in_living = ir_block_living_find(self, value, &idx);
1682 #if defined(LIFE_RANGE_WITHOUT_LAST_READ)
1684 bool in_reads = new_reads_t_v_find(&new_reads, value, &readidx);
1685 if (!in_living && !in_reads)
1690 /* If the value isn't alive it hasn't been read before... */
1691 /* TODO: See if the warning can be emitted during parsing or AST processing
1692 * otherwise have warning printed here.
1693 * IF printing a warning here: include filecontext_t,
1694 * and make sure it's only printed once
1695 * since this function is run multiple times.
1697 /* For now: debug info: */
1698 fprintf(stderr, "Value only written %s\n", value->name);
1699 tempbool = ir_value_life_merge(value, instr->eid);
1700 *changed = *changed || tempbool;
1702 ir_instr_dump(instr, dbg_ind, printf);
1706 /* since 'living' won't contain it
1707 * anymore, merge the value, since
1710 tempbool = ir_value_life_merge(value, instr->eid);
1713 fprintf(stderr, "value added id %s %i\n", value->name, (int)instr->eid);
1715 *changed = *changed || tempbool;
1717 #if ! defined(LIFE_RANGE_WITHOUT_LAST_READ)
1718 if (!ir_block_living_remove(self, idx))
1723 if (!new_reads_t_v_remove(&new_reads, readidx))
1731 tempbool = ir_block_living_add_instr(self, instr->eid);
1732 /*fprintf(stderr, "living added values\n");*/
1733 *changed = *changed || tempbool;
1735 #if defined(LIFE_RANGE_WITHOUT_LAST_READ)
1737 for (rd = 0; rd < new_reads.v_count; ++rd)
1739 if (!ir_block_living_find(self, new_reads.v[rd], NULL)) {
1740 if (!ir_block_living_add(self, new_reads.v[rd]))
1743 if (!i && !self->entries_count) {
1745 *changed = *changed || ir_value_life_merge(new_reads.v[rd], instr->eid);
1748 MEM_VECTOR_CLEAR(&new_reads, v);
1752 if (self->run_id == self->owner->run_id)
1755 self->run_id = self->owner->run_id;
1757 for (i = 0; i < self->entries_count; ++i)
1759 ir_block *entry = self->entries[i];
1760 ir_block_life_propagate(entry, self, changed);
1765 #if defined(LIFE_RANGE_WITHOUT_LAST_READ)
1766 MEM_VECTOR_CLEAR(&new_reads, v);
1771 /***********************************************************************
1774 * Since the IR has the convention of putting 'write' operands
1775 * at the beginning, we have to rotate the operands of instructions
1776 * properly in order to generate valid QCVM code.
1778 * Having destinations at a fixed position is more convenient. In QC
1779 * this is *mostly* OPC, but FTE adds at least 2 instructions which
1780 * read from from OPA, and store to OPB rather than OPC. Which is
1781 * partially the reason why the implementation of these instructions
1782 * in darkplaces has been delayed for so long.
1784 * Breaking conventions is annoying...
1786 static bool ir_builder_gen_global(ir_builder *self, ir_value *global);
1788 static bool gen_global_field(ir_value *global)
1790 if (global->isconst)
1792 ir_value *fld = global->constval.vpointer;
1794 printf("Invalid field constant with no field: %s\n", global->name);
1798 /* Now, in this case, a relocation would be impossible to code
1799 * since it looks like this:
1800 * .vector v = origin; <- parse error, wtf is 'origin'?
1803 * But we will need a general relocation support later anyway
1804 * for functions... might as well support that here.
1806 if (!fld->code.globaladdr) {
1807 printf("FIXME: Relocation support\n");
1811 /* copy the field's value */
1812 global->code.globaladdr = code_globals_add(code_globals_data[fld->code.globaladdr]);
1816 prog_section_field fld;
1818 fld.name = global->code.name;
1819 fld.offset = code_fields_elements;
1820 fld.type = global->fieldtype;
1822 if (fld.type == TYPE_VOID) {
1823 printf("Field is missing a type: %s\n", global->name);
1827 if (code_fields_add(fld) < 0)
1830 global->code.globaladdr = code_globals_add(fld.offset);
1832 if (global->code.globaladdr < 0)
1837 static bool gen_global_pointer(ir_value *global)
1839 if (global->isconst)
1841 ir_value *target = global->constval.vpointer;
1843 printf("Invalid pointer constant: %s\n", global->name);
1844 /* NULL pointers are pointing to the NULL constant, which also
1845 * sits at address 0, but still has an ir_value for itself.
1850 /* Here, relocations ARE possible - in fteqcc-enhanced-qc:
1851 * void() foo; <- proto
1852 * void() *fooptr = &foo;
1853 * void() foo = { code }
1855 if (!target->code.globaladdr) {
1856 /* FIXME: Check for the constant nullptr ir_value!
1857 * because then code.globaladdr being 0 is valid.
1859 printf("FIXME: Relocation support\n");
1863 global->code.globaladdr = code_globals_add(target->code.globaladdr);
1867 global->code.globaladdr = code_globals_add(0);
1869 if (global->code.globaladdr < 0)
1874 static bool gen_blocks_recursive(ir_function *func, ir_block *block)
1876 prog_section_statement stmt;
1877 prog_section_statement *stptr;
1886 block->generated = true;
1887 block->code_start = code_statements_elements;
1888 for (i = 0; i < block->instr_count; ++i)
1890 instr = block->instr[i];
1892 if (instr->opcode == VINSTR_PHI) {
1893 printf("cannot generate virtual instruction (phi)\n");
1897 if (instr->opcode == VINSTR_JUMP) {
1898 target = instr->bops[0];
1899 /* for uncoditional jumps, if the target hasn't been generated
1900 * yet, we generate them right here.
1902 if (!target->generated) {
1907 /* otherwise we generate a jump instruction */
1908 stmt.opcode = INSTR_GOTO;
1909 stmt.o1.s1 = (target->code_start-1) - code_statements_elements;
1912 if (code_statements_add(stmt) < 0)
1915 /* no further instructions can be in this block */
1919 if (instr->opcode == VINSTR_COND) {
1920 ontrue = instr->bops[0];
1921 onfalse = instr->bops[1];
1922 /* TODO: have the AST signal which block should
1923 * come first: eg. optimize IFs without ELSE...
1926 stmt.o1.u1 = instr->_ops[0]->code.globaladdr;
1929 if (ontrue->generated) {
1930 stmt.opcode = INSTR_IF;
1931 stmt.o2.s1 = (ontrue->code_start-1) - code_statements_elements;
1932 if (code_statements_add(stmt) < 0)
1935 if (onfalse->generated) {
1936 stmt.opcode = INSTR_IFNOT;
1937 stmt.o2.s1 = (onfalse->code_start-1) - code_statements_elements;
1938 if (code_statements_add(stmt) < 0)
1941 if (!ontrue->generated) {
1942 if (onfalse->generated) {
1947 if (!onfalse->generated) {
1948 if (ontrue->generated) {
1953 /* neither ontrue nor onfalse exist */
1954 stmt.opcode = INSTR_IFNOT;
1955 stidx = code_statements_elements - 1;
1956 if (code_statements_add(stmt) < 0)
1958 stptr = &code_statements_data[stidx];
1959 /* on false we jump, so add ontrue-path */
1960 if (!gen_blocks_recursive(func, ontrue))
1962 /* fixup the jump address */
1963 stptr->o2.s1 = (ontrue->code_start-1) - (stidx+1);
1964 /* generate onfalse path */
1965 if (onfalse->generated) {
1966 /* may have been generated in the previous recursive call */
1967 stmt.opcode = INSTR_GOTO;
1970 stmt.o1.s1 = (onfalse->code_start-1) - code_statements_elements;
1971 return (code_statements_add(stmt) >= 0);
1973 /* if not, generate now */
1978 if (instr->opcode >= INSTR_CALL0 && instr->opcode <= INSTR_CALL8) {
1979 printf("TODO: call instruction\n");
1983 if (instr->opcode == INSTR_STATE) {
1984 printf("TODO: state instruction\n");
1988 stmt.opcode = instr->opcode;
1993 /* This is the general order of operands */
1995 stmt.o3.u1 = instr->_ops[0]->code.globaladdr;
1998 stmt.o1.u1 = instr->_ops[1]->code.globaladdr;
2001 stmt.o2.u1 = instr->_ops[2]->code.globaladdr;
2003 if (stmt.opcode == INSTR_RETURN || stmt.opcode == INSTR_DONE)
2005 stmt.o1.u1 = stmt.o3.u1;
2008 else if ((stmt.opcode >= INSTR_STORE_F &&
2009 stmt.opcode <= INSTR_STORE_FNC) ||
2010 (stmt.opcode >= INSTR_NOT_F &&
2011 stmt.opcode <= INSTR_NOT_FNC))
2013 /* 2-operand instructions with A -> B */
2014 stmt.o2.u1 = stmt.o3.u1;
2018 if (code_statements_add(stmt) < 0)
2024 static bool gen_function_code(ir_function *self)
2028 /* Starting from entry point, we generate blocks "as they come"
2029 * for now. Dead blocks will not be translated obviously.
2031 if (!self->blocks_count) {
2032 printf("Function '%s' declared without body.\n", self->name);
2036 block = self->blocks[0];
2037 if (block->generated)
2040 if (!gen_blocks_recursive(self, block)) {
2041 printf("failed to generate blocks for '%s'\n", self->name);
2047 static bool gen_global_function(ir_builder *ir, ir_value *global)
2049 prog_section_function fun;
2054 if (!global->isconst ||
2055 !global->constval.vfunc)
2057 printf("Invalid state of function-global: not constant: %s\n", global->name);
2061 irfun = global->constval.vfunc;
2063 fun.name = global->code.name;
2064 fun.file = code_cachedstring(global->context.file);
2065 fun.profile = 0; /* always 0 */
2066 fun.nargs = irfun->params_count;
2068 for (i = 0;i < 8; ++i) {
2071 else if (irfun->params[i] == TYPE_VECTOR)
2077 fun.firstlocal = code_globals_elements;
2078 fun.locals = irfun->locals_count;
2079 for (i = 0; i < irfun->locals_count; ++i) {
2080 if (!ir_builder_gen_global(ir, irfun->locals[i])) {
2081 printf("Failed to generate global %s\n", irfun->locals[i]->name);
2086 fun.entry = code_statements_elements;
2087 if (!gen_function_code(irfun)) {
2088 printf("Failed to generate code for function %s\n", irfun->name);
2092 return (code_functions_add(fun) >= 0);
2095 static bool ir_builder_gen_global(ir_builder *self, ir_value *global)
2098 prog_section_def def;
2100 def.type = global->vtype;
2101 def.offset = code_globals_elements;
2102 def.name = global->code.name = code_genstring(global->name);
2104 switch (global->vtype)
2107 if (code_defs_add(def) < 0)
2109 return gen_global_pointer(global);
2111 if (code_defs_add(def) < 0)
2113 return gen_global_field(global);
2118 if (code_defs_add(def) < 0)
2121 if (global->isconst) {
2122 iptr = (int32_t*)&global->constval.vfloat;
2123 global->code.globaladdr = code_globals_add(*iptr);
2125 global->code.globaladdr = code_globals_add(0);
2127 return global->code.globaladdr >= 0;
2131 if (code_defs_add(def) < 0)
2133 if (global->isconst)
2134 global->code.globaladdr = code_globals_add(code_cachedstring(global->constval.vstring));
2136 global->code.globaladdr = code_globals_add(0);
2137 return global->code.globaladdr >= 0;
2141 if (code_defs_add(def) < 0)
2144 if (global->isconst) {
2145 iptr = (int32_t*)&global->constval.vvec;
2146 global->code.globaladdr = code_globals_add(iptr[0]);
2147 if (code_globals_add(iptr[1]) < 0 || code_globals_add(iptr[2]) < 0)
2150 global->code.globaladdr = code_globals_add(0);
2151 if (code_globals_add(0) < 0 || code_globals_add(0) < 0)
2154 return global->code.globaladdr >= 0;
2157 if (code_defs_add(def) < 0)
2159 code_globals_add(code_functions_elements);
2160 return gen_global_function(self, global);
2162 /* assume biggest type */
2163 global->code.globaladdr = code_globals_add(0);
2164 code_globals_add(0);
2165 code_globals_add(0);
2168 /* refuse to create 'void' type or any other fancy business. */
2169 printf("Invalid type for global variable %s\n", global->name);
2174 bool ir_builder_generate(ir_builder *self, const char *filename)
2180 /* FIXME: generate TYPE_FUNCTION globals and link them
2181 * to their ir_function.
2184 for (i = 0; i < self->functions_count; ++i)
2187 ir_function *fun = self->functions[i];
2189 funval = ir_builder_create_global(self, fun->name, TYPE_FUNCTION);
2190 funval->isconst = true;
2191 funval->constval.vfunc = fun;
2192 funval->context = fun->context;
2195 for (i = 0; i < self->globals_count; ++i)
2197 if (!ir_builder_gen_global(self, self->globals[i])) {
2202 printf("writing '%s'...\n", filename);
2203 return code_write(filename);
2206 /***********************************************************************
2207 *IR DEBUG Dump functions...
2210 #define IND_BUFSZ 1024
2212 const char *qc_opname(int op)
2214 if (op < 0) return "<INVALID>";
2215 if (op < ( sizeof(asm_instr) / sizeof(asm_instr[0]) ))
2216 return asm_instr[op].m;
2218 case VINSTR_PHI: return "PHI";
2219 case VINSTR_JUMP: return "JUMP";
2220 case VINSTR_COND: return "COND";
2221 default: return "<UNK>";
2225 void ir_builder_dump(ir_builder *b, int (*oprintf)(const char*, ...))
2228 char indent[IND_BUFSZ];
2232 oprintf("module %s\n", b->name);
2233 for (i = 0; i < b->globals_count; ++i)
2236 if (b->globals[i]->isconst)
2237 oprintf("%s = ", b->globals[i]->name);
2238 ir_value_dump(b->globals[i], oprintf);
2241 for (i = 0; i < b->functions_count; ++i)
2242 ir_function_dump(b->functions[i], indent, oprintf);
2243 oprintf("endmodule %s\n", b->name);
2246 void ir_function_dump(ir_function *f, char *ind,
2247 int (*oprintf)(const char*, ...))
2250 oprintf("%sfunction %s\n", ind, f->name);
2251 strncat(ind, "\t", IND_BUFSZ);
2252 if (f->locals_count)
2254 oprintf("%s%i locals:\n", ind, (int)f->locals_count);
2255 for (i = 0; i < f->locals_count; ++i) {
2256 oprintf("%s\t", ind);
2257 ir_value_dump(f->locals[i], oprintf);
2261 if (f->blocks_count)
2263 oprintf("%slife passes (check): %i\n", ind, (int)f->run_id);
2264 for (i = 0; i < f->blocks_count; ++i) {
2265 if (f->blocks[i]->run_id != f->run_id) {
2266 oprintf("%slife pass check fail! %i != %i\n", ind, (int)f->blocks[i]->run_id, (int)f->run_id);
2268 ir_block_dump(f->blocks[i], ind, oprintf);
2272 ind[strlen(ind)-1] = 0;
2273 oprintf("%sendfunction %s\n", ind, f->name);
2276 void ir_block_dump(ir_block* b, char *ind,
2277 int (*oprintf)(const char*, ...))
2280 oprintf("%s:%s\n", ind, b->label);
2281 strncat(ind, "\t", IND_BUFSZ);
2283 for (i = 0; i < b->instr_count; ++i)
2284 ir_instr_dump(b->instr[i], ind, oprintf);
2285 ind[strlen(ind)-1] = 0;
2288 void dump_phi(ir_instr *in, char *ind,
2289 int (*oprintf)(const char*, ...))
2292 oprintf("%s <- phi ", in->_ops[0]->name);
2293 for (i = 0; i < in->phi_count; ++i)
2295 oprintf("([%s] : %s) ", in->phi[i].from->label,
2296 in->phi[i].value->name);
2301 void ir_instr_dump(ir_instr *in, char *ind,
2302 int (*oprintf)(const char*, ...))
2305 const char *comma = NULL;
2307 oprintf("%s (%i) ", ind, (int)in->eid);
2309 if (in->opcode == VINSTR_PHI) {
2310 dump_phi(in, ind, oprintf);
2314 strncat(ind, "\t", IND_BUFSZ);
2316 if (in->_ops[0] && (in->_ops[1] || in->_ops[2])) {
2317 ir_value_dump(in->_ops[0], oprintf);
2318 if (in->_ops[1] || in->_ops[2])
2321 oprintf("%s\t", qc_opname(in->opcode));
2322 if (in->_ops[0] && !(in->_ops[1] || in->_ops[2])) {
2323 ir_value_dump(in->_ops[0], oprintf);
2328 for (i = 1; i != 3; ++i) {
2332 ir_value_dump(in->_ops[i], oprintf);
2340 oprintf("[%s]", in->bops[0]->label);
2344 oprintf("%s[%s]", comma, in->bops[1]->label);
2346 ind[strlen(ind)-1] = 0;
2349 void ir_value_dump(ir_value* v, int (*oprintf)(const char*, ...))
2357 oprintf("%g", v->constval.vfloat);
2360 oprintf("'%g %g %g'",
2363 v->constval.vvec.z);
2366 oprintf("(entity)");
2369 oprintf("\"%s\"", v->constval.vstring);
2373 oprintf("%i", v->constval.vint);
2378 v->constval.vpointer->name);
2382 oprintf("%s", v->name);
2386 void ir_value_dump_life(ir_value *self, int (*oprintf)(const char*,...))
2389 oprintf("Life of %s:\n", self->name);
2390 for (i = 0; i < self->life_count; ++i)
2392 oprintf(" + [%i, %i]\n", self->life[i].start, self->life[i].end);