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 const char *type_name[TYPE_COUNT] = {
48 size_t type_sizeof[TYPE_COUNT] = {
55 1, /* TYPE_FUNCTION */
64 uint16_t type_store_instr[TYPE_COUNT] = {
65 INSTR_STORE_F, /* should use I when having integer support */
72 INSTR_STORE_ENT, /* should use I */
74 INSTR_STORE_I, /* integer type */
79 INSTR_STORE_V, /* variant, should never be accessed */
81 AINSTR_END, /* struct */
82 AINSTR_END, /* union */
83 AINSTR_END, /* array */
86 uint16_t field_store_instr[TYPE_COUNT] = {
96 INSTR_STORE_FLD, /* integer type */
101 INSTR_STORE_V, /* variant, should never be accessed */
103 AINSTR_END, /* struct */
104 AINSTR_END, /* union */
105 AINSTR_END, /* array */
108 uint16_t type_storep_instr[TYPE_COUNT] = {
109 INSTR_STOREP_F, /* should use I when having integer support */
116 INSTR_STOREP_ENT, /* should use I */
118 INSTR_STOREP_ENT, /* integer type */
123 INSTR_STOREP_V, /* variant, should never be accessed */
125 AINSTR_END, /* struct */
126 AINSTR_END, /* union */
127 AINSTR_END, /* array */
130 uint16_t type_eq_instr[TYPE_COUNT] = {
131 INSTR_EQ_F, /* should use I when having integer support */
136 INSTR_EQ_E, /* FLD has no comparison */
138 INSTR_EQ_E, /* should use I */
145 INSTR_EQ_V, /* variant, should never be accessed */
147 AINSTR_END, /* struct */
148 AINSTR_END, /* union */
149 AINSTR_END, /* array */
152 uint16_t type_ne_instr[TYPE_COUNT] = {
153 INSTR_NE_F, /* should use I when having integer support */
158 INSTR_NE_E, /* FLD has no comparison */
160 INSTR_NE_E, /* should use I */
167 INSTR_NE_V, /* variant, should never be accessed */
169 AINSTR_END, /* struct */
170 AINSTR_END, /* union */
171 AINSTR_END, /* array */
174 static void irerror(lex_ctx ctx, const char *msg, ...)
178 con_cvprintmsg((void*)&ctx, LVL_ERROR, "internal error", msg, ap);
182 static bool irwarning(lex_ctx ctx, int warntype, const char *fmt, ...)
185 int lvl = LVL_WARNING;
187 if (warntype && !OPTS_WARN(warntype))
194 con_vprintmsg(lvl, ctx.file, ctx.line, "warning", fmt, ap);
200 /***********************************************************************
201 * Vector utility functions
204 bool GMQCC_WARN vec_ir_value_find(ir_value **vec, ir_value *what, size_t *idx)
207 size_t len = vec_size(vec);
208 for (i = 0; i < len; ++i) {
209 if (vec[i] == what) {
217 bool GMQCC_WARN vec_ir_block_find(ir_block **vec, ir_block *what, size_t *idx)
220 size_t len = vec_size(vec);
221 for (i = 0; i < len; ++i) {
222 if (vec[i] == what) {
230 bool GMQCC_WARN vec_ir_instr_find(ir_instr **vec, ir_instr *what, size_t *idx)
233 size_t len = vec_size(vec);
234 for (i = 0; i < len; ++i) {
235 if (vec[i] == what) {
243 /***********************************************************************
247 static void ir_block_delete_quick(ir_block* self);
248 static void ir_instr_delete_quick(ir_instr *self);
249 static void ir_function_delete_quick(ir_function *self);
251 ir_builder* ir_builder_new(const char *modulename)
255 self = (ir_builder*)mem_a(sizeof(*self));
259 self->functions = NULL;
260 self->globals = NULL;
262 self->extparams = NULL;
263 self->filenames = NULL;
264 self->filestrings = NULL;
266 self->str_immediate = 0;
268 if (!ir_builder_set_name(self, modulename)) {
276 void ir_builder_delete(ir_builder* self)
279 mem_d((void*)self->name);
280 for (i = 0; i != vec_size(self->functions); ++i) {
281 ir_function_delete_quick(self->functions[i]);
283 vec_free(self->functions);
284 for (i = 0; i != vec_size(self->extparams); ++i) {
285 ir_value_delete(self->extparams[i]);
287 vec_free(self->extparams);
288 for (i = 0; i != vec_size(self->globals); ++i) {
289 ir_value_delete(self->globals[i]);
291 vec_free(self->globals);
292 for (i = 0; i != vec_size(self->fields); ++i) {
293 ir_value_delete(self->fields[i]);
295 vec_free(self->fields);
296 vec_free(self->filenames);
297 vec_free(self->filestrings);
301 bool ir_builder_set_name(ir_builder *self, const char *name)
304 mem_d((void*)self->name);
305 self->name = util_strdup(name);
309 ir_function* ir_builder_get_function(ir_builder *self, const char *name)
312 for (i = 0; i < vec_size(self->functions); ++i) {
313 if (!strcmp(name, self->functions[i]->name))
314 return self->functions[i];
319 ir_function* ir_builder_create_function(ir_builder *self, const char *name, int outtype)
321 ir_function *fn = ir_builder_get_function(self, name);
326 fn = ir_function_new(self, outtype);
327 if (!ir_function_set_name(fn, name))
329 ir_function_delete(fn);
332 vec_push(self->functions, fn);
334 fn->value = ir_builder_create_global(self, fn->name, TYPE_FUNCTION);
336 ir_function_delete(fn);
340 fn->value->isconst = true;
341 fn->value->outtype = outtype;
342 fn->value->constval.vfunc = fn;
343 fn->value->context = fn->context;
348 ir_value* ir_builder_get_global(ir_builder *self, const char *name)
351 for (i = 0; i < vec_size(self->globals); ++i) {
352 if (!strcmp(self->globals[i]->name, name))
353 return self->globals[i];
358 ir_value* ir_builder_create_global(ir_builder *self, const char *name, int vtype)
362 if (name && name[0] != '#')
364 ve = ir_builder_get_global(self, name);
370 ve = ir_value_var(name, store_global, vtype);
371 vec_push(self->globals, ve);
375 ir_value* ir_builder_get_field(ir_builder *self, const char *name)
378 for (i = 0; i < vec_size(self->fields); ++i) {
379 if (!strcmp(self->fields[i]->name, name))
380 return self->fields[i];
386 ir_value* ir_builder_create_field(ir_builder *self, const char *name, int vtype)
388 ir_value *ve = ir_builder_get_field(self, name);
393 ve = ir_value_var(name, store_global, TYPE_FIELD);
394 ve->fieldtype = vtype;
395 vec_push(self->fields, ve);
399 /***********************************************************************
403 bool ir_function_naive_phi(ir_function*);
404 void ir_function_enumerate(ir_function*);
405 bool ir_function_calculate_liferanges(ir_function*);
406 bool ir_function_allocate_locals(ir_function*);
408 ir_function* ir_function_new(ir_builder* owner, int outtype)
411 self = (ir_function*)mem_a(sizeof(*self));
416 memset(self, 0, sizeof(*self));
419 if (!ir_function_set_name(self, "<@unnamed>")) {
424 self->context.file = "<@no context>";
425 self->context.line = 0;
426 self->outtype = outtype;
435 self->code_function_def = -1;
436 self->allocated_locals = 0;
442 bool ir_function_set_name(ir_function *self, const char *name)
445 mem_d((void*)self->name);
446 self->name = util_strdup(name);
450 static void ir_function_delete_quick(ir_function *self)
453 mem_d((void*)self->name);
455 for (i = 0; i != vec_size(self->blocks); ++i)
456 ir_block_delete_quick(self->blocks[i]);
457 vec_free(self->blocks);
459 vec_free(self->params);
461 for (i = 0; i != vec_size(self->values); ++i)
462 ir_value_delete(self->values[i]);
463 vec_free(self->values);
465 for (i = 0; i != vec_size(self->locals); ++i)
466 ir_value_delete(self->locals[i]);
467 vec_free(self->locals);
469 /* self->value is deleted by the builder */
474 void ir_function_delete(ir_function *self)
477 mem_d((void*)self->name);
479 for (i = 0; i != vec_size(self->blocks); ++i)
480 ir_block_delete(self->blocks[i]);
481 vec_free(self->blocks);
483 vec_free(self->params);
485 for (i = 0; i != vec_size(self->values); ++i)
486 ir_value_delete(self->values[i]);
487 vec_free(self->values);
489 for (i = 0; i != vec_size(self->locals); ++i)
490 ir_value_delete(self->locals[i]);
491 vec_free(self->locals);
493 /* self->value is deleted by the builder */
498 void ir_function_collect_value(ir_function *self, ir_value *v)
500 vec_push(self->values, v);
503 ir_block* ir_function_create_block(ir_function *self, const char *label)
505 ir_block* bn = ir_block_new(self, label);
506 memcpy(&bn->context, &self->context, sizeof(self->context));
507 vec_push(self->blocks, bn);
511 bool ir_function_finalize(ir_function *self)
516 if (!ir_function_naive_phi(self))
519 ir_function_enumerate(self);
521 if (!ir_function_calculate_liferanges(self))
524 if (!ir_function_allocate_locals(self))
529 ir_value* ir_function_get_local(ir_function *self, const char *name)
532 for (i = 0; i < vec_size(self->locals); ++i) {
533 if (!strcmp(self->locals[i]->name, name))
534 return self->locals[i];
539 ir_value* ir_function_create_local(ir_function *self, const char *name, int vtype, bool param)
544 if (ir_function_get_local(self, name))
549 vec_size(self->locals) &&
550 self->locals[vec_size(self->locals)-1]->store != store_param) {
551 irerror(self->context, "cannot add parameters after adding locals");
555 ve = ir_value_var(name, (param ? store_param : store_local), vtype);
556 vec_push(self->locals, ve);
560 /***********************************************************************
564 ir_block* ir_block_new(ir_function* owner, const char *name)
567 self = (ir_block*)mem_a(sizeof(*self));
571 memset(self, 0, sizeof(*self));
574 if (name && !ir_block_set_label(self, name)) {
579 self->context.file = "<@no context>";
580 self->context.line = 0;
584 self->entries = NULL;
588 self->is_return = false;
593 self->generated = false;
598 static void ir_block_delete_quick(ir_block* self)
601 if (self->label) mem_d(self->label);
602 for (i = 0; i != vec_size(self->instr); ++i)
603 ir_instr_delete_quick(self->instr[i]);
604 vec_free(self->instr);
605 vec_free(self->entries);
606 vec_free(self->exits);
607 vec_free(self->living);
611 void ir_block_delete(ir_block* self)
614 if (self->label) mem_d(self->label);
615 for (i = 0; i != vec_size(self->instr); ++i)
616 ir_instr_delete(self->instr[i]);
617 vec_free(self->instr);
618 vec_free(self->entries);
619 vec_free(self->exits);
620 vec_free(self->living);
624 bool ir_block_set_label(ir_block *self, const char *name)
627 mem_d((void*)self->label);
628 self->label = util_strdup(name);
629 return !!self->label;
632 /***********************************************************************
636 ir_instr* ir_instr_new(ir_block* owner, int op)
639 self = (ir_instr*)mem_a(sizeof(*self));
644 self->context.file = "<@no context>";
645 self->context.line = 0;
647 self->_ops[0] = NULL;
648 self->_ops[1] = NULL;
649 self->_ops[2] = NULL;
650 self->bops[0] = NULL;
651 self->bops[1] = NULL;
662 static void ir_instr_delete_quick(ir_instr *self)
665 vec_free(self->params);
669 void ir_instr_delete(ir_instr *self)
672 /* The following calls can only delete from
673 * vectors, we still want to delete this instruction
674 * so ignore the return value. Since with the warn_unused_result attribute
675 * gcc doesn't care about an explicit: (void)foo(); to ignore the result,
676 * I have to improvise here and use if(foo());
678 for (i = 0; i < vec_size(self->phi); ++i) {
680 if (vec_ir_instr_find(self->phi[i].value->writes, self, &idx))
681 vec_remove(self->phi[i].value->writes, idx, 1);
682 if (vec_ir_instr_find(self->phi[i].value->reads, self, &idx))
683 vec_remove(self->phi[i].value->reads, idx, 1);
686 for (i = 0; i < vec_size(self->params); ++i) {
688 if (vec_ir_instr_find(self->params[i]->writes, self, &idx))
689 vec_remove(self->params[i]->writes, idx, 1);
690 if (vec_ir_instr_find(self->params[i]->reads, self, &idx))
691 vec_remove(self->params[i]->reads, idx, 1);
693 vec_free(self->params);
694 (void)!ir_instr_op(self, 0, NULL, false);
695 (void)!ir_instr_op(self, 1, NULL, false);
696 (void)!ir_instr_op(self, 2, NULL, false);
700 bool ir_instr_op(ir_instr *self, int op, ir_value *v, bool writing)
702 if (self->_ops[op]) {
704 if (writing && vec_ir_instr_find(self->_ops[op]->writes, self, &idx))
705 vec_remove(self->_ops[op]->writes, idx, 1);
706 else if (vec_ir_instr_find(self->_ops[op]->reads, self, &idx))
707 vec_remove(self->_ops[op]->reads, idx, 1);
711 vec_push(v->writes, self);
713 vec_push(v->reads, self);
719 /***********************************************************************
723 void ir_value_code_setaddr(ir_value *self, int32_t gaddr)
725 self->code.globaladdr = gaddr;
726 if (self->members[0]) self->members[0]->code.globaladdr = gaddr;
727 if (self->members[1]) self->members[1]->code.globaladdr = gaddr;
728 if (self->members[2]) self->members[2]->code.globaladdr = gaddr;
731 int32_t ir_value_code_addr(const ir_value *self)
733 if (self->store == store_return)
734 return OFS_RETURN + self->code.addroffset;
735 return self->code.globaladdr + self->code.addroffset;
738 ir_value* ir_value_var(const char *name, int storetype, int vtype)
741 self = (ir_value*)mem_a(sizeof(*self));
743 self->fieldtype = TYPE_VOID;
744 self->outtype = TYPE_VOID;
745 self->store = storetype;
750 self->isconst = false;
751 self->context.file = "<@no context>";
752 self->context.line = 0;
754 if (name && !ir_value_set_name(self, name)) {
755 irerror(self->context, "out of memory");
760 memset(&self->constval, 0, sizeof(self->constval));
761 memset(&self->code, 0, sizeof(self->code));
763 self->members[0] = NULL;
764 self->members[1] = NULL;
765 self->members[2] = NULL;
766 self->memberof = NULL;
772 ir_value* ir_value_vector_member(ir_value *self, unsigned int member)
778 if (self->members[member])
779 return self->members[member];
781 if (self->vtype == TYPE_VECTOR)
783 m = ir_value_var(self->name, self->store, TYPE_FLOAT);
786 m->context = self->context;
788 self->members[member] = m;
789 m->code.addroffset = member;
791 else if (self->vtype == TYPE_FIELD)
793 if (self->fieldtype != TYPE_VECTOR)
795 m = ir_value_var(self->name, self->store, TYPE_FIELD);
798 m->fieldtype = TYPE_FLOAT;
799 m->context = self->context;
801 self->members[member] = m;
802 m->code.addroffset = member;
806 irerror(self->context, "invalid member access on %s", self->name);
814 ir_value* ir_value_out(ir_function *owner, const char *name, int storetype, int vtype)
816 ir_value *v = ir_value_var(name, storetype, vtype);
819 ir_function_collect_value(owner, v);
823 void ir_value_delete(ir_value* self)
827 mem_d((void*)self->name);
830 if (self->vtype == TYPE_STRING)
831 mem_d((void*)self->constval.vstring);
833 for (i = 0; i < 3; ++i) {
834 if (self->members[i])
835 ir_value_delete(self->members[i]);
837 vec_free(self->reads);
838 vec_free(self->writes);
839 vec_free(self->life);
843 bool ir_value_set_name(ir_value *self, const char *name)
846 mem_d((void*)self->name);
847 self->name = util_strdup(name);
851 bool ir_value_set_float(ir_value *self, float f)
853 if (self->vtype != TYPE_FLOAT)
855 self->constval.vfloat = f;
856 self->isconst = true;
860 bool ir_value_set_func(ir_value *self, int f)
862 if (self->vtype != TYPE_FUNCTION)
864 self->constval.vint = f;
865 self->isconst = true;
869 bool ir_value_set_vector(ir_value *self, vector v)
871 if (self->vtype != TYPE_VECTOR)
873 self->constval.vvec = v;
874 self->isconst = true;
878 bool ir_value_set_field(ir_value *self, ir_value *fld)
880 if (self->vtype != TYPE_FIELD)
882 self->constval.vpointer = fld;
883 self->isconst = true;
887 static char *ir_strdup(const char *str)
890 /* actually dup empty strings */
891 char *out = mem_a(1);
895 return util_strdup(str);
898 bool ir_value_set_string(ir_value *self, const char *str)
900 if (self->vtype != TYPE_STRING)
902 self->constval.vstring = ir_strdup(str);
903 self->isconst = true;
908 bool ir_value_set_int(ir_value *self, int i)
910 if (self->vtype != TYPE_INTEGER)
912 self->constval.vint = i;
913 self->isconst = true;
918 bool ir_value_lives(ir_value *self, size_t at)
921 for (i = 0; i < vec_size(self->life); ++i)
923 ir_life_entry_t *life = &self->life[i];
924 if (life->start <= at && at <= life->end)
926 if (life->start > at) /* since it's ordered */
932 bool ir_value_life_insert(ir_value *self, size_t idx, ir_life_entry_t e)
935 vec_push(self->life, e);
936 for (k = vec_size(self->life)-1; k > idx; --k)
937 self->life[k] = self->life[k-1];
942 bool ir_value_life_merge(ir_value *self, size_t s)
945 ir_life_entry_t *life = NULL;
946 ir_life_entry_t *before = NULL;
947 ir_life_entry_t new_entry;
949 /* Find the first range >= s */
950 for (i = 0; i < vec_size(self->life); ++i)
953 life = &self->life[i];
957 /* nothing found? append */
958 if (i == vec_size(self->life)) {
960 if (life && life->end+1 == s)
962 /* previous life range can be merged in */
966 if (life && life->end >= s)
969 vec_push(self->life, e);
975 if (before->end + 1 == s &&
976 life->start - 1 == s)
979 before->end = life->end;
980 vec_remove(self->life, i, 1);
983 if (before->end + 1 == s)
989 /* already contained */
990 if (before->end >= s)
994 if (life->start - 1 == s)
999 /* insert a new entry */
1000 new_entry.start = new_entry.end = s;
1001 return ir_value_life_insert(self, i, new_entry);
1004 bool ir_value_life_merge_into(ir_value *self, const ir_value *other)
1008 if (!vec_size(other->life))
1011 if (!vec_size(self->life)) {
1012 size_t count = vec_size(other->life);
1013 ir_life_entry_t *life = vec_add(self->life, count);
1014 memcpy(life, other->life, count * sizeof(*life));
1019 for (i = 0; i < vec_size(other->life); ++i)
1021 const ir_life_entry_t *life = &other->life[i];
1024 ir_life_entry_t *entry = &self->life[myi];
1026 if (life->end+1 < entry->start)
1028 /* adding an interval before entry */
1029 if (!ir_value_life_insert(self, myi, *life))
1035 if (life->start < entry->start &&
1036 life->end+1 >= entry->start)
1038 /* starts earlier and overlaps */
1039 entry->start = life->start;
1042 if (life->end > entry->end &&
1043 life->start <= entry->end+1)
1045 /* ends later and overlaps */
1046 entry->end = life->end;
1049 /* see if our change combines it with the next ranges */
1050 while (myi+1 < vec_size(self->life) &&
1051 entry->end+1 >= self->life[1+myi].start)
1053 /* overlaps with (myi+1) */
1054 if (entry->end < self->life[1+myi].end)
1055 entry->end = self->life[1+myi].end;
1056 vec_remove(self->life, myi+1, 1);
1057 entry = &self->life[myi];
1060 /* see if we're after the entry */
1061 if (life->start > entry->end)
1064 /* append if we're at the end */
1065 if (myi >= vec_size(self->life)) {
1066 vec_push(self->life, *life);
1069 /* otherweise check the next range */
1078 bool ir_values_overlap(const ir_value *a, const ir_value *b)
1080 /* For any life entry in A see if it overlaps with
1081 * any life entry in B.
1082 * Note that the life entries are orderes, so we can make a
1083 * more efficient algorithm there than naively translating the
1087 ir_life_entry_t *la, *lb, *enda, *endb;
1089 /* first of all, if either has no life range, they cannot clash */
1090 if (!vec_size(a->life) || !vec_size(b->life))
1095 enda = la + vec_size(a->life);
1096 endb = lb + vec_size(b->life);
1099 /* check if the entries overlap, for that,
1100 * both must start before the other one ends.
1102 if (la->start < lb->end &&
1103 lb->start < la->end)
1108 /* entries are ordered
1109 * one entry is earlier than the other
1110 * that earlier entry will be moved forward
1112 if (la->start < lb->start)
1114 /* order: A B, move A forward
1115 * check if we hit the end with A
1120 else /* if (lb->start < la->start) actually <= */
1122 /* order: B A, move B forward
1123 * check if we hit the end with B
1132 /***********************************************************************
1136 bool ir_block_create_store_op(ir_block *self, int op, ir_value *target, ir_value *what)
1140 irerror(self->context, "unreachable statement (%s)", self->label);
1143 in = ir_instr_new(self, op);
1147 if (target->store == store_value &&
1148 (op < INSTR_STOREP_F || op > INSTR_STOREP_FNC))
1150 irerror(self->context, "cannot store to an SSA value");
1151 irerror(self->context, "trying to store: %s <- %s", target->name, what->name);
1152 irerror(self->context, "instruction: %s", asm_instr[op].m);
1156 if (!ir_instr_op(in, 0, target, true) ||
1157 !ir_instr_op(in, 1, what, false))
1161 vec_push(self->instr, in);
1165 bool ir_block_create_store(ir_block *self, ir_value *target, ir_value *what)
1169 if (target->vtype == TYPE_VARIANT)
1170 vtype = what->vtype;
1172 vtype = target->vtype;
1175 if (vtype == TYPE_FLOAT && what->vtype == TYPE_INTEGER)
1176 op = INSTR_CONV_ITOF;
1177 else if (vtype == TYPE_INTEGER && what->vtype == TYPE_FLOAT)
1178 op = INSTR_CONV_FTOI;
1180 op = type_store_instr[vtype];
1182 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS)) {
1183 if (op == INSTR_STORE_FLD && what->fieldtype == TYPE_VECTOR)
1187 return ir_block_create_store_op(self, op, target, what);
1190 bool ir_block_create_storep(ir_block *self, ir_value *target, ir_value *what)
1195 if (target->vtype != TYPE_POINTER)
1198 /* storing using pointer - target is a pointer, type must be
1199 * inferred from source
1201 vtype = what->vtype;
1203 op = type_storep_instr[vtype];
1204 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS)) {
1205 if (op == INSTR_STOREP_FLD && what->fieldtype == TYPE_VECTOR)
1206 op = INSTR_STOREP_V;
1209 return ir_block_create_store_op(self, op, target, what);
1212 bool ir_block_create_return(ir_block *self, ir_value *v)
1216 irerror(self->context, "unreachable statement (%s)", self->label);
1220 self->is_return = true;
1221 in = ir_instr_new(self, INSTR_RETURN);
1225 if (v && !ir_instr_op(in, 0, v, false))
1228 vec_push(self->instr, in);
1232 bool ir_block_create_if(ir_block *self, ir_value *v,
1233 ir_block *ontrue, ir_block *onfalse)
1237 irerror(self->context, "unreachable statement (%s)", self->label);
1241 /*in = ir_instr_new(self, (v->vtype == TYPE_STRING ? INSTR_IF_S : INSTR_IF_F));*/
1242 in = ir_instr_new(self, VINSTR_COND);
1246 if (!ir_instr_op(in, 0, v, false)) {
1247 ir_instr_delete(in);
1251 in->bops[0] = ontrue;
1252 in->bops[1] = onfalse;
1254 vec_push(self->instr, in);
1256 vec_push(self->exits, ontrue);
1257 vec_push(self->exits, onfalse);
1258 vec_push(ontrue->entries, self);
1259 vec_push(onfalse->entries, self);
1263 bool ir_block_create_jump(ir_block *self, ir_block *to)
1267 irerror(self->context, "unreachable statement (%s)", self->label);
1271 in = ir_instr_new(self, VINSTR_JUMP);
1276 vec_push(self->instr, in);
1278 vec_push(self->exits, to);
1279 vec_push(to->entries, self);
1283 bool ir_block_create_goto(ir_block *self, ir_block *to)
1287 irerror(self->context, "unreachable statement (%s)", self->label);
1291 in = ir_instr_new(self, INSTR_GOTO);
1296 vec_push(self->instr, in);
1298 vec_push(self->exits, to);
1299 vec_push(to->entries, self);
1303 ir_instr* ir_block_create_phi(ir_block *self, const char *label, int ot)
1307 in = ir_instr_new(self, VINSTR_PHI);
1310 out = ir_value_out(self->owner, label, store_value, ot);
1312 ir_instr_delete(in);
1315 if (!ir_instr_op(in, 0, out, true)) {
1316 ir_instr_delete(in);
1317 ir_value_delete(out);
1320 vec_push(self->instr, in);
1324 ir_value* ir_phi_value(ir_instr *self)
1326 return self->_ops[0];
1329 void ir_phi_add(ir_instr* self, ir_block *b, ir_value *v)
1333 if (!vec_ir_block_find(self->owner->entries, b, NULL)) {
1334 /* Must not be possible to cause this, otherwise the AST
1335 * is doing something wrong.
1337 irerror(self->context, "Invalid entry block for PHI");
1343 vec_push(v->reads, self);
1344 vec_push(self->phi, pe);
1347 /* call related code */
1348 ir_instr* ir_block_create_call(ir_block *self, const char *label, ir_value *func)
1352 in = ir_instr_new(self, INSTR_CALL0);
1355 out = ir_value_out(self->owner, label, (func->outtype == TYPE_VOID) ? store_return : store_value, func->outtype);
1357 ir_instr_delete(in);
1360 if (!ir_instr_op(in, 0, out, true) ||
1361 !ir_instr_op(in, 1, func, false))
1363 ir_instr_delete(in);
1364 ir_value_delete(out);
1367 vec_push(self->instr, in);
1371 ir_value* ir_call_value(ir_instr *self)
1373 return self->_ops[0];
1376 void ir_call_param(ir_instr* self, ir_value *v)
1378 vec_push(self->params, v);
1379 vec_push(v->reads, self);
1382 /* binary op related code */
1384 ir_value* ir_block_create_binop(ir_block *self,
1385 const char *label, int opcode,
1386 ir_value *left, ir_value *right)
1408 case INSTR_SUB_S: /* -- offset of string as float */
1413 case INSTR_BITOR_IF:
1414 case INSTR_BITOR_FI:
1415 case INSTR_BITAND_FI:
1416 case INSTR_BITAND_IF:
1431 case INSTR_BITAND_I:
1434 case INSTR_RSHIFT_I:
1435 case INSTR_LSHIFT_I:
1457 /* boolean operations result in floats */
1458 if (opcode >= INSTR_EQ_F && opcode <= INSTR_GT)
1460 else if (opcode >= INSTR_LE && opcode <= INSTR_GT)
1463 else if (opcode >= INSTR_LE_I && opcode <= INSTR_EQ_FI)
1468 if (ot == TYPE_VOID) {
1469 /* The AST or parser were supposed to check this! */
1473 return ir_block_create_general_instr(self, label, opcode, left, right, ot);
1476 ir_value* ir_block_create_unary(ir_block *self,
1477 const char *label, int opcode,
1480 int ot = TYPE_FLOAT;
1492 /* QC doesn't have other unary operations. We expect extensions to fill
1493 * the above list, otherwise we assume out-type = in-type, eg for an
1497 ot = operand->vtype;
1500 if (ot == TYPE_VOID) {
1501 /* The AST or parser were supposed to check this! */
1505 /* let's use the general instruction creator and pass NULL for OPB */
1506 return ir_block_create_general_instr(self, label, opcode, operand, NULL, ot);
1509 ir_value* ir_block_create_general_instr(ir_block *self, const char *label,
1510 int op, ir_value *a, ir_value *b, int outype)
1515 out = ir_value_out(self->owner, label, store_value, outype);
1519 instr = ir_instr_new(self, op);
1521 ir_value_delete(out);
1525 if (!ir_instr_op(instr, 0, out, true) ||
1526 !ir_instr_op(instr, 1, a, false) ||
1527 !ir_instr_op(instr, 2, b, false) )
1532 vec_push(self->instr, instr);
1536 ir_instr_delete(instr);
1537 ir_value_delete(out);
1541 ir_value* ir_block_create_fieldaddress(ir_block *self, const char *label, ir_value *ent, ir_value *field)
1545 /* Support for various pointer types todo if so desired */
1546 if (ent->vtype != TYPE_ENTITY)
1549 if (field->vtype != TYPE_FIELD)
1552 v = ir_block_create_general_instr(self, label, INSTR_ADDRESS, ent, field, TYPE_POINTER);
1553 v->fieldtype = field->fieldtype;
1557 ir_value* ir_block_create_load_from_ent(ir_block *self, const char *label, ir_value *ent, ir_value *field, int outype)
1560 if (ent->vtype != TYPE_ENTITY)
1563 /* at some point we could redirect for TYPE_POINTER... but that could lead to carelessness */
1564 if (field->vtype != TYPE_FIELD)
1569 case TYPE_FLOAT: op = INSTR_LOAD_F; break;
1570 case TYPE_VECTOR: op = INSTR_LOAD_V; break;
1571 case TYPE_STRING: op = INSTR_LOAD_S; break;
1572 case TYPE_FIELD: op = INSTR_LOAD_FLD; break;
1573 case TYPE_ENTITY: op = INSTR_LOAD_ENT; break;
1574 case TYPE_FUNCTION: op = INSTR_LOAD_FNC; break;
1576 case TYPE_POINTER: op = INSTR_LOAD_I; break;
1577 case TYPE_INTEGER: op = INSTR_LOAD_I; break;
1580 irerror(self->context, "invalid type for ir_block_create_load_from_ent: %s", type_name[outype]);
1584 return ir_block_create_general_instr(self, label, op, ent, field, outype);
1587 ir_value* ir_block_create_add(ir_block *self,
1589 ir_value *left, ir_value *right)
1592 int l = left->vtype;
1593 int r = right->vtype;
1597 irerror(self->context, "invalid type for ir_block_create_add: %s", type_name[l]);
1613 if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1615 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1620 irerror(self->context, "invalid type for ir_block_create_add: %s", type_name[l]);
1624 return ir_block_create_binop(self, label, op, left, right);
1627 ir_value* ir_block_create_sub(ir_block *self,
1629 ir_value *left, ir_value *right)
1632 int l = left->vtype;
1633 int r = right->vtype;
1638 irerror(self->context, "invalid type for ir_block_create_sub: %s", type_name[l]);
1654 if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1656 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1661 irerror(self->context, "invalid type for ir_block_create_sub: %s", type_name[l]);
1665 return ir_block_create_binop(self, label, op, left, right);
1668 ir_value* ir_block_create_mul(ir_block *self,
1670 ir_value *left, ir_value *right)
1673 int l = left->vtype;
1674 int r = right->vtype;
1679 irerror(self->context, "invalid type for ir_block_create_mul: %s", type_name[l]);
1694 if ( (l == TYPE_VECTOR && r == TYPE_FLOAT) )
1696 else if ( (l == TYPE_FLOAT && r == TYPE_VECTOR) )
1699 else if ( (l == TYPE_VECTOR && r == TYPE_INTEGER) )
1701 else if ( (l == TYPE_INTEGER && r == TYPE_VECTOR) )
1703 else if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1705 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1709 irerror(self->context, "invalid type for ir_block_create_mul: %s", type_name[l]);
1713 return ir_block_create_binop(self, label, op, left, right);
1716 ir_value* ir_block_create_div(ir_block *self,
1718 ir_value *left, ir_value *right)
1721 int l = left->vtype;
1722 int r = right->vtype;
1727 irerror(self->context, "invalid type for ir_block_create_div: %s", type_name[l]);
1740 if ( (l == TYPE_VECTOR && r == TYPE_FLOAT) )
1742 else if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1744 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1749 irerror(self->context, "invalid type for ir_block_create_div: %s", type_name[l]);
1753 return ir_block_create_binop(self, label, op, left, right);
1756 /* PHI resolving breaks the SSA, and must thus be the last
1757 * step before life-range calculation.
1760 static bool ir_block_naive_phi(ir_block *self);
1761 bool ir_function_naive_phi(ir_function *self)
1765 for (i = 0; i < vec_size(self->blocks); ++i)
1767 if (!ir_block_naive_phi(self->blocks[i]))
1774 static bool ir_naive_phi_emit_store(ir_block *block, size_t iid, ir_value *old, ir_value *what)
1779 /* create a store */
1780 if (!ir_block_create_store(block, old, what))
1783 /* we now move it up */
1784 instr = vec_last(block->instr);
1785 for (i = vec_size(block->instr)-1; i > iid; --i)
1786 block->instr[i] = block->instr[i-1];
1787 block->instr[i] = instr;
1793 static bool ir_block_naive_phi(ir_block *self)
1795 size_t i, p; /*, w;*/
1796 /* FIXME: optionally, create_phi can add the phis
1797 * to a list so we don't need to loop through blocks
1798 * - anyway: "don't optimize YET"
1800 for (i = 0; i < vec_size(self->instr); ++i)
1802 ir_instr *instr = self->instr[i];
1803 if (instr->opcode != VINSTR_PHI)
1806 vec_remove(self->instr, i, 1);
1807 --i; /* NOTE: i+1 below */
1809 for (p = 0; p < vec_size(instr->phi); ++p)
1811 ir_value *v = instr->phi[p].value;
1812 ir_block *b = instr->phi[p].from;
1814 if (v->store == store_value &&
1815 vec_size(v->reads) == 1 &&
1816 vec_size(v->writes) == 1)
1818 /* replace the value */
1819 if (!ir_instr_op(v->writes[0], 0, instr->_ops[0], true))
1824 /* force a move instruction */
1825 ir_instr *prevjump = vec_last(b->instr);
1828 instr->_ops[0]->store = store_global;
1829 if (!ir_block_create_store(b, instr->_ops[0], v))
1831 instr->_ops[0]->store = store_value;
1832 vec_push(b->instr, prevjump);
1837 ir_value *v = instr->phi[p].value;
1838 for (w = 0; w < vec_size(v->writes); ++w) {
1841 if (!v->writes[w]->_ops[0])
1844 /* When the write was to a global, we have to emit a mov */
1845 old = v->writes[w]->_ops[0];
1847 /* The original instruction now writes to the PHI target local */
1848 if (v->writes[w]->_ops[0] == v)
1849 v->writes[w]->_ops[0] = instr->_ops[0];
1851 if (old->store != store_value && old->store != store_local && old->store != store_param)
1853 /* If it originally wrote to a global we need to store the value
1856 if (!ir_naive_phi_emit_store(self, i+1, old, v))
1858 if (i+1 < vec_size(self->instr))
1859 instr = self->instr[i+1];
1862 /* In case I forget and access instr later, it'll be NULL
1863 * when it's a problem, to make sure we crash, rather than accessing
1869 /* If it didn't, we can replace all reads by the phi target now. */
1871 for (r = 0; r < vec_size(old->reads); ++r)
1874 ir_instr *ri = old->reads[r];
1875 for (op = 0; op < vec_size(ri->phi); ++op) {
1876 if (ri->phi[op].value == old)
1877 ri->phi[op].value = v;
1879 for (op = 0; op < 3; ++op) {
1880 if (ri->_ops[op] == old)
1888 ir_instr_delete(instr);
1893 /***********************************************************************
1894 *IR Temp allocation code
1895 * Propagating value life ranges by walking through the function backwards
1896 * until no more changes are made.
1897 * In theory this should happen once more than once for every nested loop
1899 * Though this implementation might run an additional time for if nests.
1902 /* Enumerate instructions used by value's life-ranges
1904 static void ir_block_enumerate(ir_block *self, size_t *_eid)
1908 for (i = 0; i < vec_size(self->instr); ++i)
1910 self->instr[i]->eid = eid++;
1915 /* Enumerate blocks and instructions.
1916 * The block-enumeration is unordered!
1917 * We do not really use the block enumreation, however
1918 * the instruction enumeration is important for life-ranges.
1920 void ir_function_enumerate(ir_function *self)
1923 size_t instruction_id = 0;
1924 for (i = 0; i < vec_size(self->blocks); ++i)
1926 self->blocks[i]->eid = i;
1927 self->blocks[i]->run_id = 0;
1928 ir_block_enumerate(self->blocks[i], &instruction_id);
1932 static bool ir_block_life_propagate(ir_block *b, ir_block *prev, bool *changed);
1933 bool ir_function_calculate_liferanges(ir_function *self)
1941 for (i = 0; i != vec_size(self->blocks); ++i)
1943 if (self->blocks[i]->is_return)
1945 vec_free(self->blocks[i]->living);
1946 if (!ir_block_life_propagate(self->blocks[i], NULL, &changed))
1951 if (vec_size(self->blocks)) {
1952 ir_block *block = self->blocks[0];
1953 for (i = 0; i < vec_size(block->living); ++i) {
1954 ir_value *v = block->living[i];
1955 if (v->memberof || v->store != store_local)
1957 if (irwarning(v->context, WARN_USED_UNINITIALIZED,
1958 "variable `%s` may be used uninitialized in this function", v->name))
1967 /* Local-value allocator
1968 * After finishing creating the liferange of all values used in a function
1969 * we can allocate their global-positions.
1970 * This is the counterpart to register-allocation in register machines.
1976 } function_allocator;
1978 static bool function_allocator_alloc(function_allocator *alloc, const ir_value *var)
1981 size_t vsize = type_sizeof[var->vtype];
1983 slot = ir_value_var("reg", store_global, var->vtype);
1987 if (!ir_value_life_merge_into(slot, var))
1990 vec_push(alloc->locals, slot);
1991 vec_push(alloc->sizes, vsize);
1996 ir_value_delete(slot);
2000 bool ir_function_allocate_locals(ir_function *self)
2009 function_allocator alloc;
2011 if (!vec_size(self->locals) && !vec_size(self->values))
2014 alloc.locals = NULL;
2016 alloc.positions = NULL;
2018 for (i = 0; i < vec_size(self->locals); ++i)
2020 if (!function_allocator_alloc(&alloc, self->locals[i]))
2024 /* Allocate a slot for any value that still exists */
2025 for (i = 0; i < vec_size(self->values); ++i)
2027 v = self->values[i];
2029 if (!vec_size(v->life))
2032 for (a = 0; a < vec_size(alloc.locals); ++a)
2034 slot = alloc.locals[a];
2036 if (ir_values_overlap(v, slot))
2039 if (!ir_value_life_merge_into(slot, v))
2042 /* adjust size for this slot */
2043 if (alloc.sizes[a] < type_sizeof[v->vtype])
2044 alloc.sizes[a] = type_sizeof[v->vtype];
2046 self->values[i]->code.local = a;
2049 if (a >= vec_size(alloc.locals)) {
2050 self->values[i]->code.local = vec_size(alloc.locals);
2051 if (!function_allocator_alloc(&alloc, v))
2060 /* Adjust slot positions based on sizes */
2061 vec_push(alloc.positions, 0);
2063 if (vec_size(alloc.sizes))
2064 pos = alloc.positions[0] + alloc.sizes[0];
2067 for (i = 1; i < vec_size(alloc.sizes); ++i)
2069 pos = alloc.positions[i-1] + alloc.sizes[i-1];
2070 vec_push(alloc.positions, pos);
2073 self->allocated_locals = pos + vec_last(alloc.sizes);
2075 /* Take over the actual slot positions */
2076 for (i = 0; i < vec_size(self->values); ++i) {
2077 self->values[i]->code.local = alloc.positions[self->values[i]->code.local];
2085 for (i = 0; i < vec_size(alloc.locals); ++i)
2086 ir_value_delete(alloc.locals[i]);
2087 vec_free(alloc.locals);
2088 vec_free(alloc.sizes);
2089 vec_free(alloc.positions);
2093 /* Get information about which operand
2094 * is read from, or written to.
2096 static void ir_op_read_write(int op, size_t *read, size_t *write)
2116 case INSTR_STOREP_F:
2117 case INSTR_STOREP_V:
2118 case INSTR_STOREP_S:
2119 case INSTR_STOREP_ENT:
2120 case INSTR_STOREP_FLD:
2121 case INSTR_STOREP_FNC:
2132 static bool ir_block_living_add_instr(ir_block *self, size_t eid)
2135 bool changed = false;
2137 for (i = 0; i != vec_size(self->living); ++i)
2139 tempbool = ir_value_life_merge(self->living[i], eid);
2142 irerror(self->context, "block_living_add_instr() value instruction added %s: %i", self->living[i]->_name, (int)eid);
2144 changed = changed || tempbool;
2149 static bool ir_block_life_prop_previous(ir_block* self, ir_block *prev, bool *changed)
2152 /* values which have been read in a previous iteration are now
2153 * in the "living" array even if the previous block doesn't use them.
2154 * So we have to remove whatever does not exist in the previous block.
2155 * They will be re-added on-read, but the liferange merge won't cause
2158 for (i = 0; i < vec_size(self->living); ++i)
2160 if (!vec_ir_value_find(prev->living, self->living[i], NULL)) {
2161 vec_remove(self->living, i, 1);
2166 /* Whatever the previous block still has in its living set
2167 * must now be added to ours as well.
2169 for (i = 0; i < vec_size(prev->living); ++i)
2171 if (vec_ir_value_find(self->living, prev->living[i], NULL))
2173 vec_push(self->living, prev->living[i]);
2175 irerror(self->contextt from prev: %s", self->label, prev->living[i]->_name);
2181 static bool ir_block_life_propagate(ir_block *self, ir_block *prev, bool *changed)
2187 /* bitmasks which operands are read from or written to */
2189 char dbg_ind[16] = { '#', '0' };
2194 if (!ir_block_life_prop_previous(self, prev, changed))
2198 i = vec_size(self->instr);
2201 instr = self->instr[i];
2203 /* PHI operands are always read operands */
2204 for (p = 0; p < vec_size(instr->phi); ++p)
2206 value = instr->phi[p].value;
2207 if (value->memberof)
2208 value = value->memberof;
2209 if (!vec_ir_value_find(self->living, value, NULL))
2210 vec_push(self->living, value);
2213 /* call params are read operands too */
2214 for (p = 0; p < vec_size(instr->params); ++p)
2216 value = instr->params[p];
2217 if (value->memberof)
2218 value = value->memberof;
2219 if (!vec_ir_value_find(self->living, value, NULL))
2220 vec_push(self->living, value);
2223 /* See which operands are read and write operands */
2224 ir_op_read_write(instr->opcode, &read, &write);
2226 if (instr->opcode == INSTR_MUL_VF)
2228 /* the float source will get an additional lifetime */
2229 tempbool = ir_value_life_merge(instr->_ops[2], instr->eid+1);
2230 *changed = *changed || tempbool;
2232 else if (instr->opcode == INSTR_MUL_FV)
2234 /* the float source will get an additional lifetime */
2235 tempbool = ir_value_life_merge(instr->_ops[1], instr->eid+1);
2236 *changed = *changed || tempbool;
2239 /* Go through the 3 main operands */
2240 for (o = 0; o < 3; ++o)
2242 if (!instr->_ops[o]) /* no such operand */
2245 value = instr->_ops[o];
2246 if (value->memberof)
2247 value = value->memberof;
2249 /* We only care about locals */
2250 /* we also calculate parameter liferanges so that locals
2251 * can take up parameter slots */
2252 if (value->store != store_value &&
2253 value->store != store_local &&
2254 value->store != store_param)
2260 if (!vec_ir_value_find(self->living, value, NULL))
2261 vec_push(self->living, value);
2264 /* write operands */
2265 /* When we write to a local, we consider it "dead" for the
2266 * remaining upper part of the function, since in SSA a value
2267 * can only be written once (== created)
2272 bool in_living = vec_ir_value_find(self->living, value, &idx);
2275 /* If the value isn't alive it hasn't been read before... */
2276 /* TODO: See if the warning can be emitted during parsing or AST processing
2277 * otherwise have warning printed here.
2278 * IF printing a warning here: include filecontext_t,
2279 * and make sure it's only printed once
2280 * since this function is run multiple times.
2282 /* For now: debug info: */
2283 /* con_err( "Value only written %s\n", value->name); */
2284 tempbool = ir_value_life_merge(value, instr->eid);
2285 *changed = *changed || tempbool;
2287 ir_instr_dump(instr, dbg_ind, printf);
2291 /* since 'living' won't contain it
2292 * anymore, merge the value, since
2295 tempbool = ir_value_life_merge(value, instr->eid);
2298 con_err( "value added id %s %i\n", value->name, (int)instr->eid);
2300 *changed = *changed || tempbool;
2302 vec_remove(self->living, idx, 1);
2307 tempbool = ir_block_living_add_instr(self, instr->eid);
2308 /*con_err( "living added values\n");*/
2309 *changed = *changed || tempbool;
2313 if (self->run_id == self->owner->run_id)
2316 self->run_id = self->owner->run_id;
2318 for (i = 0; i < vec_size(self->entries); ++i)
2320 ir_block *entry = self->entries[i];
2321 ir_block_life_propagate(entry, self, changed);
2327 /***********************************************************************
2330 * Since the IR has the convention of putting 'write' operands
2331 * at the beginning, we have to rotate the operands of instructions
2332 * properly in order to generate valid QCVM code.
2334 * Having destinations at a fixed position is more convenient. In QC
2335 * this is *mostly* OPC, but FTE adds at least 2 instructions which
2336 * read from from OPA, and store to OPB rather than OPC. Which is
2337 * partially the reason why the implementation of these instructions
2338 * in darkplaces has been delayed for so long.
2340 * Breaking conventions is annoying...
2342 static bool ir_builder_gen_global(ir_builder *self, ir_value *global, bool islocal);
2344 static bool gen_global_field(ir_value *global)
2346 if (global->isconst)
2348 ir_value *fld = global->constval.vpointer;
2350 irerror(global->context, "Invalid field constant with no field: %s", global->name);
2354 /* Now, in this case, a relocation would be impossible to code
2355 * since it looks like this:
2356 * .vector v = origin; <- parse error, wtf is 'origin'?
2359 * But we will need a general relocation support later anyway
2360 * for functions... might as well support that here.
2362 if (!fld->code.globaladdr) {
2363 irerror(global->context, "FIXME: Relocation support");
2367 /* copy the field's value */
2368 ir_value_code_setaddr(global, vec_size(code_globals));
2369 vec_push(code_globals, code_globals[fld->code.globaladdr]);
2370 if (global->fieldtype == TYPE_VECTOR) {
2371 vec_push(code_globals, code_globals[fld->code.globaladdr]+1);
2372 vec_push(code_globals, code_globals[fld->code.globaladdr]+2);
2377 ir_value_code_setaddr(global, vec_size(code_globals));
2378 vec_push(code_globals, 0);
2379 if (global->fieldtype == TYPE_VECTOR) {
2380 vec_push(code_globals, 0);
2381 vec_push(code_globals, 0);
2384 if (global->code.globaladdr < 0)
2389 static bool gen_global_pointer(ir_value *global)
2391 if (global->isconst)
2393 ir_value *target = global->constval.vpointer;
2395 irerror(global->context, "Invalid pointer constant: %s", global->name);
2396 /* NULL pointers are pointing to the NULL constant, which also
2397 * sits at address 0, but still has an ir_value for itself.
2402 /* Here, relocations ARE possible - in fteqcc-enhanced-qc:
2403 * void() foo; <- proto
2404 * void() *fooptr = &foo;
2405 * void() foo = { code }
2407 if (!target->code.globaladdr) {
2408 /* FIXME: Check for the constant nullptr ir_value!
2409 * because then code.globaladdr being 0 is valid.
2411 irerror(global->context, "FIXME: Relocation support");
2415 ir_value_code_setaddr(global, vec_size(code_globals));
2416 vec_push(code_globals, target->code.globaladdr);
2420 ir_value_code_setaddr(global, vec_size(code_globals));
2421 vec_push(code_globals, 0);
2423 if (global->code.globaladdr < 0)
2428 static bool gen_blocks_recursive(ir_function *func, ir_block *block)
2430 prog_section_statement stmt;
2439 block->generated = true;
2440 block->code_start = vec_size(code_statements);
2441 for (i = 0; i < vec_size(block->instr); ++i)
2443 instr = block->instr[i];
2445 if (instr->opcode == VINSTR_PHI) {
2446 irerror(block->context, "cannot generate virtual instruction (phi)");
2450 if (instr->opcode == VINSTR_JUMP) {
2451 target = instr->bops[0];
2452 /* for uncoditional jumps, if the target hasn't been generated
2453 * yet, we generate them right here.
2455 if (!target->generated) {
2460 /* otherwise we generate a jump instruction */
2461 stmt.opcode = INSTR_GOTO;
2462 stmt.o1.s1 = (target->code_start) - vec_size(code_statements);
2465 vec_push(code_statements, stmt);
2467 /* no further instructions can be in this block */
2471 if (instr->opcode == VINSTR_COND) {
2472 ontrue = instr->bops[0];
2473 onfalse = instr->bops[1];
2474 /* TODO: have the AST signal which block should
2475 * come first: eg. optimize IFs without ELSE...
2478 stmt.o1.u1 = ir_value_code_addr(instr->_ops[0]);
2482 if (ontrue->generated) {
2483 stmt.opcode = INSTR_IF;
2484 stmt.o2.s1 = (ontrue->code_start) - vec_size(code_statements);
2485 vec_push(code_statements, stmt);
2487 if (onfalse->generated) {
2488 stmt.opcode = INSTR_IFNOT;
2489 stmt.o2.s1 = (onfalse->code_start) - vec_size(code_statements);
2490 vec_push(code_statements, stmt);
2492 if (!ontrue->generated) {
2493 if (onfalse->generated) {
2498 if (!onfalse->generated) {
2499 if (ontrue->generated) {
2504 /* neither ontrue nor onfalse exist */
2505 stmt.opcode = INSTR_IFNOT;
2506 if (!instr->likely) {
2507 /* Honor the likelyhood hint */
2508 ir_block *tmp = onfalse;
2509 stmt.opcode = INSTR_IF;
2513 stidx = vec_size(code_statements);
2514 vec_push(code_statements, stmt);
2515 /* on false we jump, so add ontrue-path */
2516 if (!gen_blocks_recursive(func, ontrue))
2518 /* fixup the jump address */
2519 code_statements[stidx].o2.s1 = vec_size(code_statements) - stidx;
2520 /* generate onfalse path */
2521 if (onfalse->generated) {
2522 /* fixup the jump address */
2523 code_statements[stidx].o2.s1 = (onfalse->code_start) - (stidx);
2524 stmt.opcode = vec_last(code_statements).opcode;
2525 if (stmt.opcode == INSTR_GOTO ||
2526 stmt.opcode == INSTR_IF ||
2527 stmt.opcode == INSTR_IFNOT ||
2528 stmt.opcode == INSTR_RETURN ||
2529 stmt.opcode == INSTR_DONE)
2531 /* no use jumping from here */
2534 /* may have been generated in the previous recursive call */
2535 stmt.opcode = INSTR_GOTO;
2536 stmt.o1.s1 = (onfalse->code_start) - vec_size(code_statements);
2539 vec_push(code_statements, stmt);
2542 /* if not, generate now */
2547 if (instr->opcode >= INSTR_CALL0 && instr->opcode <= INSTR_CALL8) {
2548 /* Trivial call translation:
2549 * copy all params to OFS_PARM*
2550 * if the output's storetype is not store_return,
2551 * add append a STORE instruction!
2553 * NOTES on how to do it better without much trouble:
2554 * -) The liferanges!
2555 * Simply check the liferange of all parameters for
2556 * other CALLs. For each param with no CALL in its
2557 * liferange, we can store it in an OFS_PARM at
2558 * generation already. This would even include later
2559 * reuse.... probably... :)
2564 first = vec_size(instr->params);
2567 for (p = 0; p < first; ++p)
2569 ir_value *param = instr->params[p];
2571 stmt.opcode = INSTR_STORE_F;
2574 if (param->vtype == TYPE_FIELD)
2575 stmt.opcode = field_store_instr[param->fieldtype];
2577 stmt.opcode = type_store_instr[param->vtype];
2578 stmt.o1.u1 = ir_value_code_addr(param);
2579 stmt.o2.u1 = OFS_PARM0 + 3 * p;
2580 vec_push(code_statements, stmt);
2582 /* No whandle extparams */
2583 first = vec_size(instr->params);
2584 for (; p < first; ++p)
2586 ir_builder *ir = func->owner;
2587 ir_value *param = instr->params[p];
2590 if (p-8 >= vec_size(ir->extparams)) {
2591 irerror(instr->context, "Not enough extparam-globals have been created");
2595 target = ir->extparams[p-8];
2597 stmt.opcode = INSTR_STORE_F;
2600 if (param->vtype == TYPE_FIELD)
2601 stmt.opcode = field_store_instr[param->fieldtype];
2603 stmt.opcode = type_store_instr[param->vtype];
2604 stmt.o1.u1 = ir_value_code_addr(param);
2605 stmt.o2.u1 = ir_value_code_addr(target);
2606 vec_push(code_statements, stmt);
2609 stmt.opcode = INSTR_CALL0 + vec_size(instr->params);
2610 if (stmt.opcode > INSTR_CALL8)
2611 stmt.opcode = INSTR_CALL8;
2612 stmt.o1.u1 = ir_value_code_addr(instr->_ops[1]);
2615 vec_push(code_statements, stmt);
2617 retvalue = instr->_ops[0];
2618 if (retvalue && retvalue->store != store_return && vec_size(retvalue->life))
2620 /* not to be kept in OFS_RETURN */
2621 if (retvalue->vtype == TYPE_FIELD)
2622 stmt.opcode = field_store_instr[retvalue->vtype];
2624 stmt.opcode = type_store_instr[retvalue->vtype];
2625 stmt.o1.u1 = OFS_RETURN;
2626 stmt.o2.u1 = ir_value_code_addr(retvalue);
2628 vec_push(code_statements, stmt);
2633 if (instr->opcode == INSTR_STATE) {
2634 irerror(block->context, "TODO: state instruction");
2638 stmt.opcode = instr->opcode;
2643 /* This is the general order of operands */
2645 stmt.o3.u1 = ir_value_code_addr(instr->_ops[0]);
2648 stmt.o1.u1 = ir_value_code_addr(instr->_ops[1]);
2651 stmt.o2.u1 = ir_value_code_addr(instr->_ops[2]);
2653 if (stmt.opcode == INSTR_RETURN || stmt.opcode == INSTR_DONE)
2655 stmt.o1.u1 = stmt.o3.u1;
2658 else if ((stmt.opcode >= INSTR_STORE_F &&
2659 stmt.opcode <= INSTR_STORE_FNC) ||
2660 (stmt.opcode >= INSTR_STOREP_F &&
2661 stmt.opcode <= INSTR_STOREP_FNC))
2663 /* 2-operand instructions with A -> B */
2664 stmt.o2.u1 = stmt.o3.u1;
2668 vec_push(code_statements, stmt);
2673 static bool gen_function_code(ir_function *self)
2676 prog_section_statement stmt;
2678 /* Starting from entry point, we generate blocks "as they come"
2679 * for now. Dead blocks will not be translated obviously.
2681 if (!vec_size(self->blocks)) {
2682 irerror(self->context, "Function '%s' declared without body.", self->name);
2686 block = self->blocks[0];
2687 if (block->generated)
2690 if (!gen_blocks_recursive(self, block)) {
2691 irerror(self->context, "failed to generate blocks for '%s'", self->name);
2695 /* otherwise code_write crashes since it debug-prints functions until AINSTR_END */
2696 stmt.opcode = AINSTR_END;
2700 vec_push(code_statements, stmt);
2704 static qcint ir_builder_filestring(ir_builder *ir, const char *filename)
2706 /* NOTE: filename pointers are copied, we never strdup them,
2707 * thus we can use pointer-comparison to find the string.
2712 for (i = 0; i < vec_size(ir->filenames); ++i) {
2713 if (ir->filenames[i] == filename)
2714 return ir->filestrings[i];
2717 str = code_genstring(filename);
2718 vec_push(ir->filenames, filename);
2719 vec_push(ir->filestrings, str);
2723 static bool gen_global_function(ir_builder *ir, ir_value *global)
2725 prog_section_function fun;
2729 size_t local_var_end;
2731 if (!global->isconst || (!global->constval.vfunc))
2733 irerror(global->context, "Invalid state of function-global: not constant: %s", global->name);
2737 irfun = global->constval.vfunc;
2739 fun.name = global->code.name;
2740 fun.file = ir_builder_filestring(ir, global->context.file);
2741 fun.profile = 0; /* always 0 */
2742 fun.nargs = vec_size(irfun->params);
2746 for (i = 0;i < 8; ++i) {
2750 fun.argsize[i] = type_sizeof[irfun->params[i]];
2753 fun.firstlocal = vec_size(code_globals);
2755 local_var_end = fun.firstlocal;
2756 for (i = 0; i < vec_size(irfun->locals); ++i) {
2757 if (!ir_builder_gen_global(ir, irfun->locals[i], true)) {
2758 irerror(irfun->locals[i]->context, "Failed to generate local %s", irfun->locals[i]->name);
2762 if (vec_size(irfun->locals)) {
2763 ir_value *last = vec_last(irfun->locals);
2764 local_var_end = last->code.globaladdr;
2765 local_var_end += type_sizeof[last->vtype];
2767 for (i = 0; i < vec_size(irfun->values); ++i)
2769 /* generate code.globaladdr for ssa values */
2770 ir_value *v = irfun->values[i];
2771 ir_value_code_setaddr(v, local_var_end + v->code.local);
2773 for (i = 0; i < irfun->allocated_locals; ++i) {
2774 /* fill the locals with zeros */
2775 vec_push(code_globals, 0);
2778 fun.locals = vec_size(code_globals) - fun.firstlocal;
2781 fun.entry = irfun->builtin;
2783 irfun->code_function_def = vec_size(code_functions);
2784 fun.entry = vec_size(code_statements);
2787 vec_push(code_functions, fun);
2791 static void ir_gen_extparam(ir_builder *ir)
2793 prog_section_def def;
2797 snprintf(name, sizeof(name), "EXTPARM#%i", (int)(vec_size(ir->extparams)+8));
2798 global = ir_value_var(name, store_global, TYPE_VECTOR);
2800 def.name = code_genstring(name);
2801 def.type = TYPE_VECTOR;
2802 def.offset = vec_size(code_globals);
2804 vec_push(code_defs, def);
2805 ir_value_code_setaddr(global, def.offset);
2806 vec_push(code_globals, 0);
2807 vec_push(code_globals, 0);
2808 vec_push(code_globals, 0);
2810 vec_push(ir->extparams, global);
2813 static bool gen_function_extparam_copy(ir_function *self)
2815 size_t i, ext, numparams;
2817 ir_builder *ir = self->owner;
2819 prog_section_statement stmt;
2821 numparams = vec_size(self->params);
2825 stmt.opcode = INSTR_STORE_F;
2827 for (i = 8; i < numparams; ++i) {
2829 if (ext >= vec_size(ir->extparams))
2830 ir_gen_extparam(ir);
2832 ep = ir->extparams[ext];
2834 stmt.opcode = type_store_instr[self->locals[i]->vtype];
2835 if (self->locals[i]->vtype == TYPE_FIELD &&
2836 self->locals[i]->fieldtype == TYPE_VECTOR)
2838 stmt.opcode = INSTR_STORE_V;
2840 stmt.o1.u1 = ir_value_code_addr(ep);
2841 stmt.o2.u1 = ir_value_code_addr(self->locals[i]);
2842 vec_push(code_statements, stmt);
2848 static bool gen_global_function_code(ir_builder *ir, ir_value *global)
2850 prog_section_function *fundef;
2853 irfun = global->constval.vfunc;
2855 irwarning(global->context, WARN_IMPLICIT_FUNCTION_POINTER,
2856 "function `%s` has no body and in QC implicitly becomes a function-pointer", global->name);
2857 /* this was a function pointer, don't generate code for those */
2864 if (irfun->code_function_def < 0) {
2865 irerror(irfun->context, "`%s`: IR global wasn't generated, failed to access function-def", irfun->name);
2868 fundef = &code_functions[irfun->code_function_def];
2870 fundef->entry = vec_size(code_statements);
2871 if (!gen_function_extparam_copy(irfun)) {
2872 irerror(irfun->context, "Failed to generate extparam-copy code for function %s", irfun->name);
2875 if (!gen_function_code(irfun)) {
2876 irerror(irfun->context, "Failed to generate code for function %s", irfun->name);
2882 static bool ir_builder_gen_global(ir_builder *self, ir_value *global, bool islocal)
2886 prog_section_def def;
2888 def.type = global->vtype;
2889 def.offset = vec_size(code_globals);
2892 if (global->name[0] == '#') {
2893 if (!self->str_immediate)
2894 self->str_immediate = code_genstring("IMMEDIATE");
2895 def.name = global->code.name = self->str_immediate;
2898 def.name = global->code.name = code_genstring(global->name);
2903 switch (global->vtype)
2906 if (!strcmp(global->name, "end_sys_globals")) {
2907 /* TODO: remember this point... all the defs before this one
2908 * should be checksummed and added to progdefs.h when we generate it.
2911 else if (!strcmp(global->name, "end_sys_fields")) {
2912 /* TODO: same as above but for entity-fields rather than globsl
2916 irwarning(global->context, WARN_VOID_VARIABLES, "unrecognized variable of type void `%s`",
2918 /* I'd argue setting it to 0 is sufficient, but maybe some depend on knowing how far
2919 * the system fields actually go? Though the engine knows this anyway...
2920 * Maybe this could be an -foption
2921 * fteqcc creates data for end_sys_* - of size 1, so let's do the same
2923 ir_value_code_setaddr(global, vec_size(code_globals));
2924 vec_push(code_globals, 0);
2926 vec_push(code_defs, def);
2929 vec_push(code_defs, def);
2930 return gen_global_pointer(global);
2932 vec_push(code_defs, def);
2933 return gen_global_field(global);
2938 ir_value_code_setaddr(global, vec_size(code_globals));
2939 if (global->isconst) {
2940 iptr = (int32_t*)&global->constval.ivec[0];
2941 vec_push(code_globals, *iptr);
2943 vec_push(code_globals, 0);
2945 def.type |= DEF_SAVEGLOBAL;
2947 vec_push(code_defs, def);
2949 return global->code.globaladdr >= 0;
2953 ir_value_code_setaddr(global, vec_size(code_globals));
2954 if (global->isconst) {
2955 vec_push(code_globals, code_genstring(global->constval.vstring));
2957 vec_push(code_globals, 0);
2959 def.type |= DEF_SAVEGLOBAL;
2961 vec_push(code_defs, def);
2962 return global->code.globaladdr >= 0;
2967 ir_value_code_setaddr(global, vec_size(code_globals));
2968 if (global->isconst) {
2969 iptr = (int32_t*)&global->constval.ivec[0];
2970 vec_push(code_globals, iptr[0]);
2971 if (global->code.globaladdr < 0)
2973 for (d = 1; d < type_sizeof[global->vtype]; ++d)
2975 vec_push(code_globals, iptr[d]);
2978 vec_push(code_globals, 0);
2979 if (global->code.globaladdr < 0)
2981 for (d = 1; d < type_sizeof[global->vtype]; ++d)
2983 vec_push(code_globals, 0);
2986 def.type |= DEF_SAVEGLOBAL;
2989 vec_push(code_defs, def);
2990 return global->code.globaladdr >= 0;
2993 ir_value_code_setaddr(global, vec_size(code_globals));
2994 if (!global->isconst) {
2995 vec_push(code_globals, 0);
2996 if (global->code.globaladdr < 0)
2999 vec_push(code_globals, vec_size(code_functions));
3000 if (!gen_global_function(self, global))
3003 def.type |= DEF_SAVEGLOBAL;
3005 vec_push(code_defs, def);
3008 /* assume biggest type */
3009 ir_value_code_setaddr(global, vec_size(code_globals));
3010 vec_push(code_globals, 0);
3011 for (i = 1; i < type_sizeof[TYPE_VARIANT]; ++i)
3012 vec_push(code_globals, 0);
3015 /* refuse to create 'void' type or any other fancy business. */
3016 irerror(global->context, "Invalid type for global variable `%s`: %s",
3017 global->name, type_name[global->vtype]);
3022 static bool ir_builder_gen_field(ir_builder *self, ir_value *field)
3024 prog_section_def def;
3025 prog_section_field fld;
3027 def.type = field->vtype;
3028 def.offset = vec_size(code_globals);
3030 /* create a global named the same as the field */
3031 if (opts_standard == COMPILER_GMQCC) {
3032 /* in our standard, the global gets a dot prefix */
3033 size_t len = strlen(field->name);
3036 /* we really don't want to have to allocate this, and 1024
3037 * bytes is more than enough for a variable/field name
3039 if (len+2 >= sizeof(name)) {
3040 irerror(field->context, "invalid field name size: %u", (unsigned int)len);
3045 memcpy(name+1, field->name, len); /* no strncpy - we used strlen above */
3048 def.name = code_genstring(name);
3049 fld.name = def.name + 1; /* we reuse that string table entry */
3051 /* in plain QC, there cannot be a global with the same name,
3052 * and so we also name the global the same.
3053 * FIXME: fteqcc should create a global as well
3054 * check if it actually uses the same name. Probably does
3056 def.name = code_genstring(field->name);
3057 fld.name = def.name;
3060 field->code.name = def.name;
3062 vec_push(code_defs, def);
3064 fld.type = field->fieldtype;
3066 if (fld.type == TYPE_VOID) {
3067 irerror(field->context, "field is missing a type: %s - don't know its size", field->name);
3071 fld.offset = code_alloc_field(type_sizeof[field->fieldtype]);
3073 vec_push(code_fields, fld);
3075 ir_value_code_setaddr(field, vec_size(code_globals));
3076 vec_push(code_globals, fld.offset);
3077 if (fld.type == TYPE_VECTOR) {
3078 vec_push(code_globals, fld.offset+1);
3079 vec_push(code_globals, fld.offset+2);
3082 return field->code.globaladdr >= 0;
3085 bool ir_builder_generate(ir_builder *self, const char *filename)
3087 prog_section_statement stmt;
3092 for (i = 0; i < vec_size(self->globals); ++i)
3094 if (!ir_builder_gen_global(self, self->globals[i], false)) {
3099 for (i = 0; i < vec_size(self->fields); ++i)
3101 if (!ir_builder_gen_field(self, self->fields[i])) {
3106 /* generate function code */
3107 for (i = 0; i < vec_size(self->globals); ++i)
3109 if (self->globals[i]->vtype == TYPE_FUNCTION) {
3110 if (!gen_global_function_code(self, self->globals[i])) {
3116 /* DP errors if the last instruction is not an INSTR_DONE
3117 * and for debugging purposes we add an additional AINSTR_END
3118 * to the end of functions, so here it goes:
3120 stmt.opcode = INSTR_DONE;
3124 vec_push(code_statements, stmt);
3127 con_out("writing '%s'...\n", filename);
3128 return code_write(filename);
3131 /***********************************************************************
3132 *IR DEBUG Dump functions...
3135 #define IND_BUFSZ 1024
3138 # define strncat(dst, src, sz) strncat_s(dst, sz, src, _TRUNCATE)
3141 const char *qc_opname(int op)
3143 if (op < 0) return "<INVALID>";
3144 if (op < ( sizeof(asm_instr) / sizeof(asm_instr[0]) ))
3145 return asm_instr[op].m;
3147 case VINSTR_PHI: return "PHI";
3148 case VINSTR_JUMP: return "JUMP";
3149 case VINSTR_COND: return "COND";
3150 default: return "<UNK>";
3154 void ir_builder_dump(ir_builder *b, int (*oprintf)(const char*, ...))
3157 char indent[IND_BUFSZ];
3161 oprintf("module %s\n", b->name);
3162 for (i = 0; i < vec_size(b->globals); ++i)
3165 if (b->globals[i]->isconst)
3166 oprintf("%s = ", b->globals[i]->name);
3167 ir_value_dump(b->globals[i], oprintf);
3170 for (i = 0; i < vec_size(b->functions); ++i)
3171 ir_function_dump(b->functions[i], indent, oprintf);
3172 oprintf("endmodule %s\n", b->name);
3175 void ir_function_dump(ir_function *f, char *ind,
3176 int (*oprintf)(const char*, ...))
3179 if (f->builtin != 0) {
3180 oprintf("%sfunction %s = builtin %i\n", ind, f->name, -f->builtin);
3183 oprintf("%sfunction %s\n", ind, f->name);
3184 strncat(ind, "\t", IND_BUFSZ);
3185 if (vec_size(f->locals))
3187 oprintf("%s%i locals:\n", ind, (int)vec_size(f->locals));
3188 for (i = 0; i < vec_size(f->locals); ++i) {
3189 oprintf("%s\t", ind);
3190 ir_value_dump(f->locals[i], oprintf);
3194 oprintf("%sliferanges:\n", ind);
3195 for (i = 0; i < vec_size(f->locals); ++i) {
3197 ir_value *v = f->locals[i];
3198 oprintf("%s\t%s: unique ", ind, v->name);
3199 for (l = 0; l < vec_size(v->life); ++l) {
3200 oprintf("[%i,%i] ", v->life[l].start, v->life[l].end);
3204 for (i = 0; i < vec_size(f->values); ++i) {
3206 ir_value *v = f->values[i];
3207 oprintf("%s\t%s: @%i ", ind, v->name, (int)v->code.local);
3208 for (l = 0; l < vec_size(v->life); ++l) {
3209 oprintf("[%i,%i] ", v->life[l].start, v->life[l].end);
3213 if (vec_size(f->blocks))
3215 oprintf("%slife passes (check): %i\n", ind, (int)f->run_id);
3216 for (i = 0; i < vec_size(f->blocks); ++i) {
3217 if (f->blocks[i]->run_id != f->run_id) {
3218 oprintf("%slife pass check fail! %i != %i\n", ind, (int)f->blocks[i]->run_id, (int)f->run_id);
3220 ir_block_dump(f->blocks[i], ind, oprintf);
3224 ind[strlen(ind)-1] = 0;
3225 oprintf("%sendfunction %s\n", ind, f->name);
3228 void ir_block_dump(ir_block* b, char *ind,
3229 int (*oprintf)(const char*, ...))
3232 oprintf("%s:%s\n", ind, b->label);
3233 strncat(ind, "\t", IND_BUFSZ);
3235 for (i = 0; i < vec_size(b->instr); ++i)
3236 ir_instr_dump(b->instr[i], ind, oprintf);
3237 ind[strlen(ind)-1] = 0;
3240 void dump_phi(ir_instr *in, char *ind,
3241 int (*oprintf)(const char*, ...))
3244 oprintf("%s <- phi ", in->_ops[0]->name);
3245 for (i = 0; i < vec_size(in->phi); ++i)
3247 oprintf("([%s] : %s) ", in->phi[i].from->label,
3248 in->phi[i].value->name);
3253 void ir_instr_dump(ir_instr *in, char *ind,
3254 int (*oprintf)(const char*, ...))
3257 const char *comma = NULL;
3259 oprintf("%s (%i) ", ind, (int)in->eid);
3261 if (in->opcode == VINSTR_PHI) {
3262 dump_phi(in, ind, oprintf);
3266 strncat(ind, "\t", IND_BUFSZ);
3268 if (in->_ops[0] && (in->_ops[1] || in->_ops[2])) {
3269 ir_value_dump(in->_ops[0], oprintf);
3270 if (in->_ops[1] || in->_ops[2])
3273 if (in->opcode == INSTR_CALL0) {
3274 oprintf("CALL%i\t", vec_size(in->params));
3276 oprintf("%s\t", qc_opname(in->opcode));
3278 if (in->_ops[0] && !(in->_ops[1] || in->_ops[2])) {
3279 ir_value_dump(in->_ops[0], oprintf);
3284 for (i = 1; i != 3; ++i) {
3288 ir_value_dump(in->_ops[i], oprintf);
3296 oprintf("[%s]", in->bops[0]->label);
3300 oprintf("%s[%s]", comma, in->bops[1]->label);
3301 if (vec_size(in->params)) {
3302 oprintf("\tparams: ");
3303 for (i = 0; i != vec_size(in->params); ++i) {
3304 oprintf("%s, ", in->params[i]->name);
3308 ind[strlen(ind)-1] = 0;
3311 void ir_value_dump(ir_value* v, int (*oprintf)(const char*, ...))
3320 oprintf("fn:%s", v->name);
3323 oprintf("%g", v->constval.vfloat);
3326 oprintf("'%g %g %g'",
3329 v->constval.vvec.z);
3332 oprintf("(entity)");
3335 oprintf("\"%s\"", v->constval.vstring);
3339 oprintf("%i", v->constval.vint);
3344 v->constval.vpointer->name);
3348 oprintf("%s", v->name);
3352 void ir_value_dump_life(const ir_value *self, int (*oprintf)(const char*,...))
3355 oprintf("Life of %12s:", self->name);
3356 for (i = 0; i < vec_size(self->life); ++i)
3358 oprintf(" + [%i, %i]\n", self->life[i].start, self->life[i].end);