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 uint16_t type_not_instr[TYPE_COUNT] = {
175 INSTR_NOT_F, /* should use I when having integer support */
182 INSTR_NOT_ENT, /* should use I */
184 INSTR_NOT_I, /* integer type */
189 INSTR_NOT_V, /* variant, should never be accessed */
191 AINSTR_END, /* struct */
192 AINSTR_END, /* union */
193 AINSTR_END, /* array */
196 static void irerror(lex_ctx ctx, const char *msg, ...)
200 con_cvprintmsg((void*)&ctx, LVL_ERROR, "internal error", msg, ap);
204 static bool irwarning(lex_ctx ctx, int warntype, const char *fmt, ...)
207 int lvl = LVL_WARNING;
209 if (warntype && !OPTS_WARN(warntype))
216 con_vprintmsg(lvl, ctx.file, ctx.line, "warning", fmt, ap);
222 /***********************************************************************
223 * Vector utility functions
226 bool GMQCC_WARN vec_ir_value_find(ir_value **vec, ir_value *what, size_t *idx)
229 size_t len = vec_size(vec);
230 for (i = 0; i < len; ++i) {
231 if (vec[i] == what) {
239 bool GMQCC_WARN vec_ir_block_find(ir_block **vec, ir_block *what, size_t *idx)
242 size_t len = vec_size(vec);
243 for (i = 0; i < len; ++i) {
244 if (vec[i] == what) {
252 bool GMQCC_WARN vec_ir_instr_find(ir_instr **vec, ir_instr *what, size_t *idx)
255 size_t len = vec_size(vec);
256 for (i = 0; i < len; ++i) {
257 if (vec[i] == what) {
265 /***********************************************************************
269 static void ir_block_delete_quick(ir_block* self);
270 static void ir_instr_delete_quick(ir_instr *self);
271 static void ir_function_delete_quick(ir_function *self);
273 ir_builder* ir_builder_new(const char *modulename)
277 self = (ir_builder*)mem_a(sizeof(*self));
281 self->functions = NULL;
282 self->globals = NULL;
284 self->extparams = NULL;
285 self->filenames = NULL;
286 self->filestrings = NULL;
288 self->str_immediate = 0;
290 if (!ir_builder_set_name(self, modulename)) {
298 void ir_builder_delete(ir_builder* self)
301 mem_d((void*)self->name);
302 for (i = 0; i != vec_size(self->functions); ++i) {
303 ir_function_delete_quick(self->functions[i]);
305 vec_free(self->functions);
306 for (i = 0; i != vec_size(self->extparams); ++i) {
307 ir_value_delete(self->extparams[i]);
309 vec_free(self->extparams);
310 for (i = 0; i != vec_size(self->globals); ++i) {
311 ir_value_delete(self->globals[i]);
313 vec_free(self->globals);
314 for (i = 0; i != vec_size(self->fields); ++i) {
315 ir_value_delete(self->fields[i]);
317 vec_free(self->fields);
318 vec_free(self->filenames);
319 vec_free(self->filestrings);
323 bool ir_builder_set_name(ir_builder *self, const char *name)
326 mem_d((void*)self->name);
327 self->name = util_strdup(name);
331 ir_function* ir_builder_get_function(ir_builder *self, const char *name)
334 for (i = 0; i < vec_size(self->functions); ++i) {
335 if (!strcmp(name, self->functions[i]->name))
336 return self->functions[i];
341 ir_function* ir_builder_create_function(ir_builder *self, const char *name, int outtype)
343 ir_function *fn = ir_builder_get_function(self, name);
348 fn = ir_function_new(self, outtype);
349 if (!ir_function_set_name(fn, name))
351 ir_function_delete(fn);
354 vec_push(self->functions, fn);
356 fn->value = ir_builder_create_global(self, fn->name, TYPE_FUNCTION);
358 ir_function_delete(fn);
362 fn->value->isconst = true;
363 fn->value->outtype = outtype;
364 fn->value->constval.vfunc = fn;
365 fn->value->context = fn->context;
370 ir_value* ir_builder_get_global(ir_builder *self, const char *name)
373 for (i = 0; i < vec_size(self->globals); ++i) {
374 if (!strcmp(self->globals[i]->name, name))
375 return self->globals[i];
380 ir_value* ir_builder_create_global(ir_builder *self, const char *name, int vtype)
384 if (name && name[0] != '#')
386 ve = ir_builder_get_global(self, name);
392 ve = ir_value_var(name, store_global, vtype);
393 vec_push(self->globals, ve);
397 ir_value* ir_builder_get_field(ir_builder *self, const char *name)
400 for (i = 0; i < vec_size(self->fields); ++i) {
401 if (!strcmp(self->fields[i]->name, name))
402 return self->fields[i];
408 ir_value* ir_builder_create_field(ir_builder *self, const char *name, int vtype)
410 ir_value *ve = ir_builder_get_field(self, name);
415 ve = ir_value_var(name, store_global, TYPE_FIELD);
416 ve->fieldtype = vtype;
417 vec_push(self->fields, ve);
421 /***********************************************************************
425 bool ir_function_naive_phi(ir_function*);
426 void ir_function_enumerate(ir_function*);
427 bool ir_function_calculate_liferanges(ir_function*);
428 bool ir_function_allocate_locals(ir_function*);
430 ir_function* ir_function_new(ir_builder* owner, int outtype)
433 self = (ir_function*)mem_a(sizeof(*self));
438 memset(self, 0, sizeof(*self));
441 if (!ir_function_set_name(self, "<@unnamed>")) {
446 self->context.file = "<@no context>";
447 self->context.line = 0;
448 self->outtype = outtype;
457 self->code_function_def = -1;
458 self->allocated_locals = 0;
464 bool ir_function_set_name(ir_function *self, const char *name)
467 mem_d((void*)self->name);
468 self->name = util_strdup(name);
472 static void ir_function_delete_quick(ir_function *self)
475 mem_d((void*)self->name);
477 for (i = 0; i != vec_size(self->blocks); ++i)
478 ir_block_delete_quick(self->blocks[i]);
479 vec_free(self->blocks);
481 vec_free(self->params);
483 for (i = 0; i != vec_size(self->values); ++i)
484 ir_value_delete(self->values[i]);
485 vec_free(self->values);
487 for (i = 0; i != vec_size(self->locals); ++i)
488 ir_value_delete(self->locals[i]);
489 vec_free(self->locals);
491 /* self->value is deleted by the builder */
496 void ir_function_delete(ir_function *self)
499 mem_d((void*)self->name);
501 for (i = 0; i != vec_size(self->blocks); ++i)
502 ir_block_delete(self->blocks[i]);
503 vec_free(self->blocks);
505 vec_free(self->params);
507 for (i = 0; i != vec_size(self->values); ++i)
508 ir_value_delete(self->values[i]);
509 vec_free(self->values);
511 for (i = 0; i != vec_size(self->locals); ++i)
512 ir_value_delete(self->locals[i]);
513 vec_free(self->locals);
515 /* self->value is deleted by the builder */
520 void ir_function_collect_value(ir_function *self, ir_value *v)
522 vec_push(self->values, v);
525 ir_block* ir_function_create_block(ir_function *self, const char *label)
527 ir_block* bn = ir_block_new(self, label);
528 memcpy(&bn->context, &self->context, sizeof(self->context));
529 vec_push(self->blocks, bn);
533 bool ir_function_finalize(ir_function *self)
538 if (!ir_function_naive_phi(self))
541 ir_function_enumerate(self);
543 if (!ir_function_calculate_liferanges(self))
545 if (!ir_function_allocate_locals(self))
550 ir_value* ir_function_get_local(ir_function *self, const char *name)
553 for (i = 0; i < vec_size(self->locals); ++i) {
554 if (!strcmp(self->locals[i]->name, name))
555 return self->locals[i];
560 ir_value* ir_function_create_local(ir_function *self, const char *name, int vtype, bool param)
565 if (ir_function_get_local(self, name))
570 vec_size(self->locals) &&
571 self->locals[vec_size(self->locals)-1]->store != store_param) {
572 irerror(self->context, "cannot add parameters after adding locals");
576 ve = ir_value_var(name, (param ? store_param : store_local), vtype);
577 vec_push(self->locals, ve);
581 /***********************************************************************
585 ir_block* ir_block_new(ir_function* owner, const char *name)
588 self = (ir_block*)mem_a(sizeof(*self));
592 memset(self, 0, sizeof(*self));
595 if (name && !ir_block_set_label(self, name)) {
600 self->context.file = "<@no context>";
601 self->context.line = 0;
605 self->entries = NULL;
609 self->is_return = false;
614 self->generated = false;
619 static void ir_block_delete_quick(ir_block* self)
622 if (self->label) mem_d(self->label);
623 for (i = 0; i != vec_size(self->instr); ++i)
624 ir_instr_delete_quick(self->instr[i]);
625 vec_free(self->instr);
626 vec_free(self->entries);
627 vec_free(self->exits);
628 vec_free(self->living);
632 void ir_block_delete(ir_block* self)
635 if (self->label) mem_d(self->label);
636 for (i = 0; i != vec_size(self->instr); ++i)
637 ir_instr_delete(self->instr[i]);
638 vec_free(self->instr);
639 vec_free(self->entries);
640 vec_free(self->exits);
641 vec_free(self->living);
645 bool ir_block_set_label(ir_block *self, const char *name)
648 mem_d((void*)self->label);
649 self->label = util_strdup(name);
650 return !!self->label;
653 /***********************************************************************
657 ir_instr* ir_instr_new(ir_block* owner, int op)
660 self = (ir_instr*)mem_a(sizeof(*self));
665 self->context.file = "<@no context>";
666 self->context.line = 0;
668 self->_ops[0] = NULL;
669 self->_ops[1] = NULL;
670 self->_ops[2] = NULL;
671 self->bops[0] = NULL;
672 self->bops[1] = NULL;
683 static void ir_instr_delete_quick(ir_instr *self)
686 vec_free(self->params);
690 void ir_instr_delete(ir_instr *self)
693 /* The following calls can only delete from
694 * vectors, we still want to delete this instruction
695 * so ignore the return value. Since with the warn_unused_result attribute
696 * gcc doesn't care about an explicit: (void)foo(); to ignore the result,
697 * I have to improvise here and use if(foo());
699 for (i = 0; i < vec_size(self->phi); ++i) {
701 if (vec_ir_instr_find(self->phi[i].value->writes, self, &idx))
702 vec_remove(self->phi[i].value->writes, idx, 1);
703 if (vec_ir_instr_find(self->phi[i].value->reads, self, &idx))
704 vec_remove(self->phi[i].value->reads, idx, 1);
707 for (i = 0; i < vec_size(self->params); ++i) {
709 if (vec_ir_instr_find(self->params[i]->writes, self, &idx))
710 vec_remove(self->params[i]->writes, idx, 1);
711 if (vec_ir_instr_find(self->params[i]->reads, self, &idx))
712 vec_remove(self->params[i]->reads, idx, 1);
714 vec_free(self->params);
715 (void)!ir_instr_op(self, 0, NULL, false);
716 (void)!ir_instr_op(self, 1, NULL, false);
717 (void)!ir_instr_op(self, 2, NULL, false);
721 bool ir_instr_op(ir_instr *self, int op, ir_value *v, bool writing)
723 if (self->_ops[op]) {
725 if (writing && vec_ir_instr_find(self->_ops[op]->writes, self, &idx))
726 vec_remove(self->_ops[op]->writes, idx, 1);
727 else if (vec_ir_instr_find(self->_ops[op]->reads, self, &idx))
728 vec_remove(self->_ops[op]->reads, idx, 1);
732 vec_push(v->writes, self);
734 vec_push(v->reads, self);
740 /***********************************************************************
744 void ir_value_code_setaddr(ir_value *self, int32_t gaddr)
746 self->code.globaladdr = gaddr;
747 if (self->members[0]) self->members[0]->code.globaladdr = gaddr;
748 if (self->members[1]) self->members[1]->code.globaladdr = gaddr;
749 if (self->members[2]) self->members[2]->code.globaladdr = gaddr;
752 int32_t ir_value_code_addr(const ir_value *self)
754 if (self->store == store_return)
755 return OFS_RETURN + self->code.addroffset;
756 return self->code.globaladdr + self->code.addroffset;
759 ir_value* ir_value_var(const char *name, int storetype, int vtype)
762 self = (ir_value*)mem_a(sizeof(*self));
764 self->fieldtype = TYPE_VOID;
765 self->outtype = TYPE_VOID;
766 self->store = storetype;
771 self->isconst = false;
772 self->context.file = "<@no context>";
773 self->context.line = 0;
775 if (name && !ir_value_set_name(self, name)) {
776 irerror(self->context, "out of memory");
781 memset(&self->constval, 0, sizeof(self->constval));
782 memset(&self->code, 0, sizeof(self->code));
784 self->members[0] = NULL;
785 self->members[1] = NULL;
786 self->members[2] = NULL;
787 self->memberof = NULL;
793 ir_value* ir_value_vector_member(ir_value *self, unsigned int member)
799 if (self->members[member])
800 return self->members[member];
802 if (self->vtype == TYPE_VECTOR)
804 m = ir_value_var(self->name, self->store, TYPE_FLOAT);
807 m->context = self->context;
809 self->members[member] = m;
810 m->code.addroffset = member;
812 else if (self->vtype == TYPE_FIELD)
814 if (self->fieldtype != TYPE_VECTOR)
816 m = ir_value_var(self->name, self->store, TYPE_FIELD);
819 m->fieldtype = TYPE_FLOAT;
820 m->context = self->context;
822 self->members[member] = m;
823 m->code.addroffset = member;
827 irerror(self->context, "invalid member access on %s", self->name);
835 ir_value* ir_value_out(ir_function *owner, const char *name, int storetype, int vtype)
837 ir_value *v = ir_value_var(name, storetype, vtype);
840 ir_function_collect_value(owner, v);
844 void ir_value_delete(ir_value* self)
848 mem_d((void*)self->name);
851 if (self->vtype == TYPE_STRING)
852 mem_d((void*)self->constval.vstring);
854 for (i = 0; i < 3; ++i) {
855 if (self->members[i])
856 ir_value_delete(self->members[i]);
858 vec_free(self->reads);
859 vec_free(self->writes);
860 vec_free(self->life);
864 bool ir_value_set_name(ir_value *self, const char *name)
867 mem_d((void*)self->name);
868 self->name = util_strdup(name);
872 bool ir_value_set_float(ir_value *self, float f)
874 if (self->vtype != TYPE_FLOAT)
876 self->constval.vfloat = f;
877 self->isconst = true;
881 bool ir_value_set_func(ir_value *self, int f)
883 if (self->vtype != TYPE_FUNCTION)
885 self->constval.vint = f;
886 self->isconst = true;
890 bool ir_value_set_vector(ir_value *self, vector v)
892 if (self->vtype != TYPE_VECTOR)
894 self->constval.vvec = v;
895 self->isconst = true;
899 bool ir_value_set_field(ir_value *self, ir_value *fld)
901 if (self->vtype != TYPE_FIELD)
903 self->constval.vpointer = fld;
904 self->isconst = true;
908 static char *ir_strdup(const char *str)
911 /* actually dup empty strings */
912 char *out = mem_a(1);
916 return util_strdup(str);
919 bool ir_value_set_string(ir_value *self, const char *str)
921 if (self->vtype != TYPE_STRING)
923 self->constval.vstring = ir_strdup(str);
924 self->isconst = true;
929 bool ir_value_set_int(ir_value *self, int i)
931 if (self->vtype != TYPE_INTEGER)
933 self->constval.vint = i;
934 self->isconst = true;
939 bool ir_value_lives(ir_value *self, size_t at)
942 for (i = 0; i < vec_size(self->life); ++i)
944 ir_life_entry_t *life = &self->life[i];
945 if (life->start <= at && at <= life->end)
947 if (life->start > at) /* since it's ordered */
953 bool ir_value_life_insert(ir_value *self, size_t idx, ir_life_entry_t e)
956 vec_push(self->life, e);
957 for (k = vec_size(self->life)-1; k > idx; --k)
958 self->life[k] = self->life[k-1];
963 bool ir_value_life_merge(ir_value *self, size_t s)
966 ir_life_entry_t *life = NULL;
967 ir_life_entry_t *before = NULL;
968 ir_life_entry_t new_entry;
970 /* Find the first range >= s */
971 for (i = 0; i < vec_size(self->life); ++i)
974 life = &self->life[i];
978 /* nothing found? append */
979 if (i == vec_size(self->life)) {
981 if (life && life->end+1 == s)
983 /* previous life range can be merged in */
987 if (life && life->end >= s)
990 vec_push(self->life, e);
996 if (before->end + 1 == s &&
997 life->start - 1 == s)
1000 before->end = life->end;
1001 vec_remove(self->life, i, 1);
1004 if (before->end + 1 == s)
1010 /* already contained */
1011 if (before->end >= s)
1015 if (life->start - 1 == s)
1020 /* insert a new entry */
1021 new_entry.start = new_entry.end = s;
1022 return ir_value_life_insert(self, i, new_entry);
1025 bool ir_value_life_merge_into(ir_value *self, const ir_value *other)
1029 if (!vec_size(other->life))
1032 if (!vec_size(self->life)) {
1033 size_t count = vec_size(other->life);
1034 ir_life_entry_t *life = vec_add(self->life, count);
1035 memcpy(life, other->life, count * sizeof(*life));
1040 for (i = 0; i < vec_size(other->life); ++i)
1042 const ir_life_entry_t *life = &other->life[i];
1045 ir_life_entry_t *entry = &self->life[myi];
1047 if (life->end+1 < entry->start)
1049 /* adding an interval before entry */
1050 if (!ir_value_life_insert(self, myi, *life))
1056 if (life->start < entry->start &&
1057 life->end+1 >= entry->start)
1059 /* starts earlier and overlaps */
1060 entry->start = life->start;
1063 if (life->end > entry->end &&
1064 life->start <= entry->end+1)
1066 /* ends later and overlaps */
1067 entry->end = life->end;
1070 /* see if our change combines it with the next ranges */
1071 while (myi+1 < vec_size(self->life) &&
1072 entry->end+1 >= self->life[1+myi].start)
1074 /* overlaps with (myi+1) */
1075 if (entry->end < self->life[1+myi].end)
1076 entry->end = self->life[1+myi].end;
1077 vec_remove(self->life, myi+1, 1);
1078 entry = &self->life[myi];
1081 /* see if we're after the entry */
1082 if (life->start > entry->end)
1085 /* append if we're at the end */
1086 if (myi >= vec_size(self->life)) {
1087 vec_push(self->life, *life);
1090 /* otherweise check the next range */
1099 bool ir_values_overlap(const ir_value *a, const ir_value *b)
1101 /* For any life entry in A see if it overlaps with
1102 * any life entry in B.
1103 * Note that the life entries are orderes, so we can make a
1104 * more efficient algorithm there than naively translating the
1108 ir_life_entry_t *la, *lb, *enda, *endb;
1110 /* first of all, if either has no life range, they cannot clash */
1111 if (!vec_size(a->life) || !vec_size(b->life))
1116 enda = la + vec_size(a->life);
1117 endb = lb + vec_size(b->life);
1120 /* check if the entries overlap, for that,
1121 * both must start before the other one ends.
1123 if (la->start < lb->end &&
1124 lb->start < la->end)
1129 /* entries are ordered
1130 * one entry is earlier than the other
1131 * that earlier entry will be moved forward
1133 if (la->start < lb->start)
1135 /* order: A B, move A forward
1136 * check if we hit the end with A
1141 else /* if (lb->start < la->start) actually <= */
1143 /* order: B A, move B forward
1144 * check if we hit the end with B
1153 /***********************************************************************
1157 bool ir_block_create_store_op(ir_block *self, int op, ir_value *target, ir_value *what)
1161 irerror(self->context, "unreachable statement (%s)", self->label);
1164 in = ir_instr_new(self, op);
1168 if (target->store == store_value &&
1169 (op < INSTR_STOREP_F || op > INSTR_STOREP_FNC))
1171 irerror(self->context, "cannot store to an SSA value");
1172 irerror(self->context, "trying to store: %s <- %s", target->name, what->name);
1173 irerror(self->context, "instruction: %s", asm_instr[op].m);
1177 if (!ir_instr_op(in, 0, target, true) ||
1178 !ir_instr_op(in, 1, what, false))
1182 vec_push(self->instr, in);
1186 bool ir_block_create_store(ir_block *self, ir_value *target, ir_value *what)
1190 if (target->vtype == TYPE_VARIANT)
1191 vtype = what->vtype;
1193 vtype = target->vtype;
1196 if (vtype == TYPE_FLOAT && what->vtype == TYPE_INTEGER)
1197 op = INSTR_CONV_ITOF;
1198 else if (vtype == TYPE_INTEGER && what->vtype == TYPE_FLOAT)
1199 op = INSTR_CONV_FTOI;
1201 op = type_store_instr[vtype];
1203 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS)) {
1204 if (op == INSTR_STORE_FLD && what->fieldtype == TYPE_VECTOR)
1208 return ir_block_create_store_op(self, op, target, what);
1211 bool ir_block_create_storep(ir_block *self, ir_value *target, ir_value *what)
1216 if (target->vtype != TYPE_POINTER)
1219 /* storing using pointer - target is a pointer, type must be
1220 * inferred from source
1222 vtype = what->vtype;
1224 op = type_storep_instr[vtype];
1225 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS)) {
1226 if (op == INSTR_STOREP_FLD && what->fieldtype == TYPE_VECTOR)
1227 op = INSTR_STOREP_V;
1230 return ir_block_create_store_op(self, op, target, what);
1233 bool ir_block_create_return(ir_block *self, ir_value *v)
1237 irerror(self->context, "unreachable statement (%s)", self->label);
1241 self->is_return = true;
1242 in = ir_instr_new(self, INSTR_RETURN);
1246 if (v && !ir_instr_op(in, 0, v, false))
1249 vec_push(self->instr, in);
1253 bool ir_block_create_if(ir_block *self, ir_value *v,
1254 ir_block *ontrue, ir_block *onfalse)
1258 irerror(self->context, "unreachable statement (%s)", self->label);
1262 /*in = ir_instr_new(self, (v->vtype == TYPE_STRING ? INSTR_IF_S : INSTR_IF_F));*/
1263 in = ir_instr_new(self, VINSTR_COND);
1267 if (!ir_instr_op(in, 0, v, false)) {
1268 ir_instr_delete(in);
1272 in->bops[0] = ontrue;
1273 in->bops[1] = onfalse;
1275 vec_push(self->instr, in);
1277 vec_push(self->exits, ontrue);
1278 vec_push(self->exits, onfalse);
1279 vec_push(ontrue->entries, self);
1280 vec_push(onfalse->entries, self);
1284 bool ir_block_create_jump(ir_block *self, ir_block *to)
1288 irerror(self->context, "unreachable statement (%s)", self->label);
1292 in = ir_instr_new(self, VINSTR_JUMP);
1297 vec_push(self->instr, in);
1299 vec_push(self->exits, to);
1300 vec_push(to->entries, self);
1304 bool ir_block_create_goto(ir_block *self, ir_block *to)
1308 irerror(self->context, "unreachable statement (%s)", self->label);
1312 in = ir_instr_new(self, INSTR_GOTO);
1317 vec_push(self->instr, in);
1319 vec_push(self->exits, to);
1320 vec_push(to->entries, self);
1324 ir_instr* ir_block_create_phi(ir_block *self, const char *label, int ot)
1328 in = ir_instr_new(self, VINSTR_PHI);
1331 out = ir_value_out(self->owner, label, store_value, ot);
1333 ir_instr_delete(in);
1336 if (!ir_instr_op(in, 0, out, true)) {
1337 ir_instr_delete(in);
1338 ir_value_delete(out);
1341 vec_push(self->instr, in);
1345 ir_value* ir_phi_value(ir_instr *self)
1347 return self->_ops[0];
1350 void ir_phi_add(ir_instr* self, ir_block *b, ir_value *v)
1354 if (!vec_ir_block_find(self->owner->entries, b, NULL)) {
1355 /* Must not be possible to cause this, otherwise the AST
1356 * is doing something wrong.
1358 irerror(self->context, "Invalid entry block for PHI");
1364 vec_push(v->reads, self);
1365 vec_push(self->phi, pe);
1368 /* call related code */
1369 ir_instr* ir_block_create_call(ir_block *self, const char *label, ir_value *func)
1373 in = ir_instr_new(self, INSTR_CALL0);
1376 out = ir_value_out(self->owner, label, (func->outtype == TYPE_VOID) ? store_return : store_value, func->outtype);
1378 ir_instr_delete(in);
1381 if (!ir_instr_op(in, 0, out, true) ||
1382 !ir_instr_op(in, 1, func, false))
1384 ir_instr_delete(in);
1385 ir_value_delete(out);
1388 vec_push(self->instr, in);
1392 ir_value* ir_call_value(ir_instr *self)
1394 return self->_ops[0];
1397 void ir_call_param(ir_instr* self, ir_value *v)
1399 vec_push(self->params, v);
1400 vec_push(v->reads, self);
1403 /* binary op related code */
1405 ir_value* ir_block_create_binop(ir_block *self,
1406 const char *label, int opcode,
1407 ir_value *left, ir_value *right)
1429 case INSTR_SUB_S: /* -- offset of string as float */
1434 case INSTR_BITOR_IF:
1435 case INSTR_BITOR_FI:
1436 case INSTR_BITAND_FI:
1437 case INSTR_BITAND_IF:
1452 case INSTR_BITAND_I:
1455 case INSTR_RSHIFT_I:
1456 case INSTR_LSHIFT_I:
1478 /* boolean operations result in floats */
1479 if (opcode >= INSTR_EQ_F && opcode <= INSTR_GT)
1481 else if (opcode >= INSTR_LE && opcode <= INSTR_GT)
1484 else if (opcode >= INSTR_LE_I && opcode <= INSTR_EQ_FI)
1489 if (ot == TYPE_VOID) {
1490 /* The AST or parser were supposed to check this! */
1494 return ir_block_create_general_instr(self, label, opcode, left, right, ot);
1497 ir_value* ir_block_create_unary(ir_block *self,
1498 const char *label, int opcode,
1501 int ot = TYPE_FLOAT;
1513 /* QC doesn't have other unary operations. We expect extensions to fill
1514 * the above list, otherwise we assume out-type = in-type, eg for an
1518 ot = operand->vtype;
1521 if (ot == TYPE_VOID) {
1522 /* The AST or parser were supposed to check this! */
1526 /* let's use the general instruction creator and pass NULL for OPB */
1527 return ir_block_create_general_instr(self, label, opcode, operand, NULL, ot);
1530 ir_value* ir_block_create_general_instr(ir_block *self, const char *label,
1531 int op, ir_value *a, ir_value *b, int outype)
1536 out = ir_value_out(self->owner, label, store_value, outype);
1540 instr = ir_instr_new(self, op);
1542 ir_value_delete(out);
1546 if (!ir_instr_op(instr, 0, out, true) ||
1547 !ir_instr_op(instr, 1, a, false) ||
1548 !ir_instr_op(instr, 2, b, false) )
1553 vec_push(self->instr, instr);
1557 ir_instr_delete(instr);
1558 ir_value_delete(out);
1562 ir_value* ir_block_create_fieldaddress(ir_block *self, const char *label, ir_value *ent, ir_value *field)
1566 /* Support for various pointer types todo if so desired */
1567 if (ent->vtype != TYPE_ENTITY)
1570 if (field->vtype != TYPE_FIELD)
1573 v = ir_block_create_general_instr(self, label, INSTR_ADDRESS, ent, field, TYPE_POINTER);
1574 v->fieldtype = field->fieldtype;
1578 ir_value* ir_block_create_load_from_ent(ir_block *self, const char *label, ir_value *ent, ir_value *field, int outype)
1581 if (ent->vtype != TYPE_ENTITY)
1584 /* at some point we could redirect for TYPE_POINTER... but that could lead to carelessness */
1585 if (field->vtype != TYPE_FIELD)
1590 case TYPE_FLOAT: op = INSTR_LOAD_F; break;
1591 case TYPE_VECTOR: op = INSTR_LOAD_V; break;
1592 case TYPE_STRING: op = INSTR_LOAD_S; break;
1593 case TYPE_FIELD: op = INSTR_LOAD_FLD; break;
1594 case TYPE_ENTITY: op = INSTR_LOAD_ENT; break;
1595 case TYPE_FUNCTION: op = INSTR_LOAD_FNC; break;
1597 case TYPE_POINTER: op = INSTR_LOAD_I; break;
1598 case TYPE_INTEGER: op = INSTR_LOAD_I; break;
1601 irerror(self->context, "invalid type for ir_block_create_load_from_ent: %s", type_name[outype]);
1605 return ir_block_create_general_instr(self, label, op, ent, field, outype);
1608 ir_value* ir_block_create_add(ir_block *self,
1610 ir_value *left, ir_value *right)
1613 int l = left->vtype;
1614 int r = right->vtype;
1618 irerror(self->context, "invalid type for ir_block_create_add: %s", type_name[l]);
1634 if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1636 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1641 irerror(self->context, "invalid type for ir_block_create_add: %s", type_name[l]);
1645 return ir_block_create_binop(self, label, op, left, right);
1648 ir_value* ir_block_create_sub(ir_block *self,
1650 ir_value *left, ir_value *right)
1653 int l = left->vtype;
1654 int r = right->vtype;
1659 irerror(self->context, "invalid type for ir_block_create_sub: %s", type_name[l]);
1675 if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1677 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1682 irerror(self->context, "invalid type for ir_block_create_sub: %s", type_name[l]);
1686 return ir_block_create_binop(self, label, op, left, right);
1689 ir_value* ir_block_create_mul(ir_block *self,
1691 ir_value *left, ir_value *right)
1694 int l = left->vtype;
1695 int r = right->vtype;
1700 irerror(self->context, "invalid type for ir_block_create_mul: %s", type_name[l]);
1715 if ( (l == TYPE_VECTOR && r == TYPE_FLOAT) )
1717 else if ( (l == TYPE_FLOAT && r == TYPE_VECTOR) )
1720 else if ( (l == TYPE_VECTOR && r == TYPE_INTEGER) )
1722 else if ( (l == TYPE_INTEGER && r == TYPE_VECTOR) )
1724 else if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1726 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1730 irerror(self->context, "invalid type for ir_block_create_mul: %s", type_name[l]);
1734 return ir_block_create_binop(self, label, op, left, right);
1737 ir_value* ir_block_create_div(ir_block *self,
1739 ir_value *left, ir_value *right)
1742 int l = left->vtype;
1743 int r = right->vtype;
1748 irerror(self->context, "invalid type for ir_block_create_div: %s", type_name[l]);
1761 if ( (l == TYPE_VECTOR && r == TYPE_FLOAT) )
1763 else if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1765 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1770 irerror(self->context, "invalid type for ir_block_create_div: %s", type_name[l]);
1774 return ir_block_create_binop(self, label, op, left, right);
1777 /* PHI resolving breaks the SSA, and must thus be the last
1778 * step before life-range calculation.
1781 static bool ir_block_naive_phi(ir_block *self);
1782 bool ir_function_naive_phi(ir_function *self)
1786 for (i = 0; i < vec_size(self->blocks); ++i)
1788 if (!ir_block_naive_phi(self->blocks[i]))
1795 static bool ir_naive_phi_emit_store(ir_block *block, size_t iid, ir_value *old, ir_value *what)
1800 /* create a store */
1801 if (!ir_block_create_store(block, old, what))
1804 /* we now move it up */
1805 instr = vec_last(block->instr);
1806 for (i = vec_size(block->instr)-1; i > iid; --i)
1807 block->instr[i] = block->instr[i-1];
1808 block->instr[i] = instr;
1814 static bool ir_block_naive_phi(ir_block *self)
1816 size_t i, p; /*, w;*/
1817 /* FIXME: optionally, create_phi can add the phis
1818 * to a list so we don't need to loop through blocks
1819 * - anyway: "don't optimize YET"
1821 for (i = 0; i < vec_size(self->instr); ++i)
1823 ir_instr *instr = self->instr[i];
1824 if (instr->opcode != VINSTR_PHI)
1827 vec_remove(self->instr, i, 1);
1828 --i; /* NOTE: i+1 below */
1830 for (p = 0; p < vec_size(instr->phi); ++p)
1832 ir_value *v = instr->phi[p].value;
1833 ir_block *b = instr->phi[p].from;
1835 if (v->store == store_value &&
1836 vec_size(v->reads) == 1 &&
1837 vec_size(v->writes) == 1)
1839 /* replace the value */
1840 if (!ir_instr_op(v->writes[0], 0, instr->_ops[0], true))
1845 /* force a move instruction */
1846 ir_instr *prevjump = vec_last(b->instr);
1849 instr->_ops[0]->store = store_global;
1850 if (!ir_block_create_store(b, instr->_ops[0], v))
1852 instr->_ops[0]->store = store_value;
1853 vec_push(b->instr, prevjump);
1858 ir_value *v = instr->phi[p].value;
1859 for (w = 0; w < vec_size(v->writes); ++w) {
1862 if (!v->writes[w]->_ops[0])
1865 /* When the write was to a global, we have to emit a mov */
1866 old = v->writes[w]->_ops[0];
1868 /* The original instruction now writes to the PHI target local */
1869 if (v->writes[w]->_ops[0] == v)
1870 v->writes[w]->_ops[0] = instr->_ops[0];
1872 if (old->store != store_value && old->store != store_local && old->store != store_param)
1874 /* If it originally wrote to a global we need to store the value
1877 if (!ir_naive_phi_emit_store(self, i+1, old, v))
1879 if (i+1 < vec_size(self->instr))
1880 instr = self->instr[i+1];
1883 /* In case I forget and access instr later, it'll be NULL
1884 * when it's a problem, to make sure we crash, rather than accessing
1890 /* If it didn't, we can replace all reads by the phi target now. */
1892 for (r = 0; r < vec_size(old->reads); ++r)
1895 ir_instr *ri = old->reads[r];
1896 for (op = 0; op < vec_size(ri->phi); ++op) {
1897 if (ri->phi[op].value == old)
1898 ri->phi[op].value = v;
1900 for (op = 0; op < 3; ++op) {
1901 if (ri->_ops[op] == old)
1909 ir_instr_delete(instr);
1914 /***********************************************************************
1915 *IR Temp allocation code
1916 * Propagating value life ranges by walking through the function backwards
1917 * until no more changes are made.
1918 * In theory this should happen once more than once for every nested loop
1920 * Though this implementation might run an additional time for if nests.
1923 /* Enumerate instructions used by value's life-ranges
1925 static void ir_block_enumerate(ir_block *self, size_t *_eid)
1929 for (i = 0; i < vec_size(self->instr); ++i)
1931 self->instr[i]->eid = eid++;
1936 /* Enumerate blocks and instructions.
1937 * The block-enumeration is unordered!
1938 * We do not really use the block enumreation, however
1939 * the instruction enumeration is important for life-ranges.
1941 void ir_function_enumerate(ir_function *self)
1944 size_t instruction_id = 0;
1945 for (i = 0; i < vec_size(self->blocks); ++i)
1947 self->blocks[i]->eid = i;
1948 self->blocks[i]->run_id = 0;
1949 ir_block_enumerate(self->blocks[i], &instruction_id);
1953 static bool ir_block_life_propagate(ir_block *b, ir_block *prev, bool *changed);
1954 bool ir_function_calculate_liferanges(ir_function *self)
1962 for (i = 0; i != vec_size(self->blocks); ++i)
1964 if (self->blocks[i]->is_return)
1966 vec_free(self->blocks[i]->living);
1967 if (!ir_block_life_propagate(self->blocks[i], NULL, &changed))
1972 if (vec_size(self->blocks)) {
1973 ir_block *block = self->blocks[0];
1974 for (i = 0; i < vec_size(block->living); ++i) {
1975 ir_value *v = block->living[i];
1976 if (v->memberof || v->store != store_local)
1978 if (irwarning(v->context, WARN_USED_UNINITIALIZED,
1979 "variable `%s` may be used uninitialized in this function", v->name))
1988 /* Local-value allocator
1989 * After finishing creating the liferange of all values used in a function
1990 * we can allocate their global-positions.
1991 * This is the counterpart to register-allocation in register machines.
1997 } function_allocator;
1999 static bool function_allocator_alloc(function_allocator *alloc, const ir_value *var)
2002 size_t vsize = type_sizeof[var->vtype];
2004 slot = ir_value_var("reg", store_global, var->vtype);
2008 if (!ir_value_life_merge_into(slot, var))
2011 vec_push(alloc->locals, slot);
2012 vec_push(alloc->sizes, vsize);
2017 ir_value_delete(slot);
2021 bool ir_function_allocate_locals(ir_function *self)
2030 function_allocator alloc;
2032 if (!vec_size(self->locals) && !vec_size(self->values))
2035 alloc.locals = NULL;
2037 alloc.positions = NULL;
2039 for (i = 0; i < vec_size(self->locals); ++i)
2041 if (!function_allocator_alloc(&alloc, self->locals[i]))
2045 /* Allocate a slot for any value that still exists */
2046 for (i = 0; i < vec_size(self->values); ++i)
2048 v = self->values[i];
2050 if (!vec_size(v->life))
2053 for (a = 0; a < vec_size(alloc.locals); ++a)
2055 slot = alloc.locals[a];
2057 if (ir_values_overlap(v, slot))
2060 if (!ir_value_life_merge_into(slot, v))
2063 /* adjust size for this slot */
2064 if (alloc.sizes[a] < type_sizeof[v->vtype])
2065 alloc.sizes[a] = type_sizeof[v->vtype];
2067 self->values[i]->code.local = a;
2070 if (a >= vec_size(alloc.locals)) {
2071 self->values[i]->code.local = vec_size(alloc.locals);
2072 if (!function_allocator_alloc(&alloc, v))
2081 /* Adjust slot positions based on sizes */
2082 vec_push(alloc.positions, 0);
2084 if (vec_size(alloc.sizes))
2085 pos = alloc.positions[0] + alloc.sizes[0];
2088 for (i = 1; i < vec_size(alloc.sizes); ++i)
2090 pos = alloc.positions[i-1] + alloc.sizes[i-1];
2091 vec_push(alloc.positions, pos);
2094 self->allocated_locals = pos + vec_last(alloc.sizes);
2096 /* Take over the actual slot positions */
2097 for (i = 0; i < vec_size(self->values); ++i) {
2098 self->values[i]->code.local = alloc.positions[self->values[i]->code.local];
2106 for (i = 0; i < vec_size(alloc.locals); ++i)
2107 ir_value_delete(alloc.locals[i]);
2108 vec_free(alloc.locals);
2109 vec_free(alloc.sizes);
2110 vec_free(alloc.positions);
2114 /* Get information about which operand
2115 * is read from, or written to.
2117 static void ir_op_read_write(int op, size_t *read, size_t *write)
2137 case INSTR_STOREP_F:
2138 case INSTR_STOREP_V:
2139 case INSTR_STOREP_S:
2140 case INSTR_STOREP_ENT:
2141 case INSTR_STOREP_FLD:
2142 case INSTR_STOREP_FNC:
2153 static bool ir_block_living_add_instr(ir_block *self, size_t eid)
2156 bool changed = false;
2158 for (i = 0; i != vec_size(self->living); ++i)
2160 tempbool = ir_value_life_merge(self->living[i], eid);
2163 irerror(self->context, "block_living_add_instr() value instruction added %s: %i", self->living[i]->_name, (int)eid);
2165 changed = changed || tempbool;
2170 static bool ir_block_life_prop_previous(ir_block* self, ir_block *prev, bool *changed)
2176 /* values which have been read in a previous iteration are now
2177 * in the "living" array even if the previous block doesn't use them.
2178 * So we have to remove whatever does not exist in the previous block.
2179 * They will be re-added on-read, but the liferange merge won't cause
2182 for (i = 0; i < vec_size(self->living); ++i)
2184 if (!vec_ir_value_find(prev->living, self->living[i], NULL)) {
2185 vec_remove(self->living, i, 1);
2190 /* Whatever the previous block still has in its living set
2191 * must now be added to ours as well.
2193 for (i = 0; i < vec_size(prev->living); ++i)
2195 if (vec_ir_value_find(self->living, prev->living[i], NULL))
2197 vec_push(self->living, prev->living[i]);
2199 irerror(self->contextt from prev: %s", self->label, prev->living[i]->_name);
2205 static bool ir_block_life_propagate(ir_block *self, ir_block *prev, bool *changed)
2211 /* bitmasks which operands are read from or written to */
2213 char dbg_ind[16] = { '#', '0' };
2218 if (!ir_block_life_prop_previous(self, prev, changed))
2222 i = vec_size(self->instr);
2225 instr = self->instr[i];
2227 /* PHI operands are always read operands */
2228 for (p = 0; p < vec_size(instr->phi); ++p)
2230 value = instr->phi[p].value;
2231 if (value->memberof)
2232 value = value->memberof;
2233 if (!vec_ir_value_find(self->living, value, NULL))
2234 vec_push(self->living, value);
2237 /* call params are read operands too */
2238 for (p = 0; p < vec_size(instr->params); ++p)
2240 value = instr->params[p];
2241 if (value->memberof)
2242 value = value->memberof;
2243 if (!vec_ir_value_find(self->living, value, NULL))
2244 vec_push(self->living, value);
2247 /* See which operands are read and write operands */
2248 ir_op_read_write(instr->opcode, &read, &write);
2250 if (instr->opcode == INSTR_MUL_VF)
2252 /* the float source will get an additional lifetime */
2253 tempbool = ir_value_life_merge(instr->_ops[2], instr->eid+1);
2254 *changed = *changed || tempbool;
2256 else if (instr->opcode == INSTR_MUL_FV)
2258 /* the float source will get an additional lifetime */
2259 tempbool = ir_value_life_merge(instr->_ops[1], instr->eid+1);
2260 *changed = *changed || tempbool;
2263 /* Go through the 3 main operands */
2264 for (o = 0; o < 3; ++o)
2266 if (!instr->_ops[o]) /* no such operand */
2269 value = instr->_ops[o];
2270 if (value->memberof)
2271 value = value->memberof;
2273 /* We only care about locals */
2274 /* we also calculate parameter liferanges so that locals
2275 * can take up parameter slots */
2276 if (value->store != store_value &&
2277 value->store != store_local &&
2278 value->store != store_param)
2284 if (!vec_ir_value_find(self->living, value, NULL))
2285 vec_push(self->living, value);
2288 /* write operands */
2289 /* When we write to a local, we consider it "dead" for the
2290 * remaining upper part of the function, since in SSA a value
2291 * can only be written once (== created)
2296 bool in_living = vec_ir_value_find(self->living, value, &idx);
2299 /* If the value isn't alive it hasn't been read before... */
2300 /* TODO: See if the warning can be emitted during parsing or AST processing
2301 * otherwise have warning printed here.
2302 * IF printing a warning here: include filecontext_t,
2303 * and make sure it's only printed once
2304 * since this function is run multiple times.
2306 /* For now: debug info: */
2307 /* con_err( "Value only written %s\n", value->name); */
2308 tempbool = ir_value_life_merge(value, instr->eid);
2309 *changed = *changed || tempbool;
2311 ir_instr_dump(instr, dbg_ind, printf);
2315 /* since 'living' won't contain it
2316 * anymore, merge the value, since
2319 tempbool = ir_value_life_merge(value, instr->eid);
2322 con_err( "value added id %s %i\n", value->name, (int)instr->eid);
2324 *changed = *changed || tempbool;
2326 vec_remove(self->living, idx, 1);
2331 tempbool = ir_block_living_add_instr(self, instr->eid);
2332 /*con_err( "living added values\n");*/
2333 *changed = *changed || tempbool;
2337 if (self->run_id == self->owner->run_id)
2340 self->run_id = self->owner->run_id;
2342 for (i = 0; i < vec_size(self->entries); ++i)
2344 ir_block *entry = self->entries[i];
2345 ir_block_life_propagate(entry, self, changed);
2351 /***********************************************************************
2354 * Since the IR has the convention of putting 'write' operands
2355 * at the beginning, we have to rotate the operands of instructions
2356 * properly in order to generate valid QCVM code.
2358 * Having destinations at a fixed position is more convenient. In QC
2359 * this is *mostly* OPC, but FTE adds at least 2 instructions which
2360 * read from from OPA, and store to OPB rather than OPC. Which is
2361 * partially the reason why the implementation of these instructions
2362 * in darkplaces has been delayed for so long.
2364 * Breaking conventions is annoying...
2366 static bool ir_builder_gen_global(ir_builder *self, ir_value *global, bool islocal);
2368 static bool gen_global_field(ir_value *global)
2370 if (global->isconst)
2372 ir_value *fld = global->constval.vpointer;
2374 irerror(global->context, "Invalid field constant with no field: %s", global->name);
2378 /* Now, in this case, a relocation would be impossible to code
2379 * since it looks like this:
2380 * .vector v = origin; <- parse error, wtf is 'origin'?
2383 * But we will need a general relocation support later anyway
2384 * for functions... might as well support that here.
2386 if (!fld->code.globaladdr) {
2387 irerror(global->context, "FIXME: Relocation support");
2391 /* copy the field's value */
2392 ir_value_code_setaddr(global, vec_size(code_globals));
2393 vec_push(code_globals, code_globals[fld->code.globaladdr]);
2394 if (global->fieldtype == TYPE_VECTOR) {
2395 vec_push(code_globals, code_globals[fld->code.globaladdr]+1);
2396 vec_push(code_globals, code_globals[fld->code.globaladdr]+2);
2401 ir_value_code_setaddr(global, vec_size(code_globals));
2402 vec_push(code_globals, 0);
2403 if (global->fieldtype == TYPE_VECTOR) {
2404 vec_push(code_globals, 0);
2405 vec_push(code_globals, 0);
2408 if (global->code.globaladdr < 0)
2413 static bool gen_global_pointer(ir_value *global)
2415 if (global->isconst)
2417 ir_value *target = global->constval.vpointer;
2419 irerror(global->context, "Invalid pointer constant: %s", global->name);
2420 /* NULL pointers are pointing to the NULL constant, which also
2421 * sits at address 0, but still has an ir_value for itself.
2426 /* Here, relocations ARE possible - in fteqcc-enhanced-qc:
2427 * void() foo; <- proto
2428 * void() *fooptr = &foo;
2429 * void() foo = { code }
2431 if (!target->code.globaladdr) {
2432 /* FIXME: Check for the constant nullptr ir_value!
2433 * because then code.globaladdr being 0 is valid.
2435 irerror(global->context, "FIXME: Relocation support");
2439 ir_value_code_setaddr(global, vec_size(code_globals));
2440 vec_push(code_globals, target->code.globaladdr);
2444 ir_value_code_setaddr(global, vec_size(code_globals));
2445 vec_push(code_globals, 0);
2447 if (global->code.globaladdr < 0)
2452 static bool gen_blocks_recursive(ir_function *func, ir_block *block)
2454 prog_section_statement stmt;
2463 block->generated = true;
2464 block->code_start = vec_size(code_statements);
2465 for (i = 0; i < vec_size(block->instr); ++i)
2467 instr = block->instr[i];
2469 if (instr->opcode == VINSTR_PHI) {
2470 irerror(block->context, "cannot generate virtual instruction (phi)");
2474 if (instr->opcode == VINSTR_JUMP) {
2475 target = instr->bops[0];
2476 /* for uncoditional jumps, if the target hasn't been generated
2477 * yet, we generate them right here.
2479 if (!target->generated) {
2484 /* otherwise we generate a jump instruction */
2485 stmt.opcode = INSTR_GOTO;
2486 stmt.o1.s1 = (target->code_start) - vec_size(code_statements);
2489 vec_push(code_statements, stmt);
2491 /* no further instructions can be in this block */
2495 if (instr->opcode == VINSTR_COND) {
2496 ontrue = instr->bops[0];
2497 onfalse = instr->bops[1];
2498 /* TODO: have the AST signal which block should
2499 * come first: eg. optimize IFs without ELSE...
2502 stmt.o1.u1 = ir_value_code_addr(instr->_ops[0]);
2506 if (ontrue->generated) {
2507 stmt.opcode = INSTR_IF;
2508 stmt.o2.s1 = (ontrue->code_start) - vec_size(code_statements);
2509 vec_push(code_statements, stmt);
2511 if (onfalse->generated) {
2512 stmt.opcode = INSTR_IFNOT;
2513 stmt.o2.s1 = (onfalse->code_start) - vec_size(code_statements);
2514 vec_push(code_statements, stmt);
2516 if (!ontrue->generated) {
2517 if (onfalse->generated) {
2522 if (!onfalse->generated) {
2523 if (ontrue->generated) {
2528 /* neither ontrue nor onfalse exist */
2529 stmt.opcode = INSTR_IFNOT;
2530 if (!instr->likely) {
2531 /* Honor the likelyhood hint */
2532 ir_block *tmp = onfalse;
2533 stmt.opcode = INSTR_IF;
2537 stidx = vec_size(code_statements);
2538 vec_push(code_statements, stmt);
2539 /* on false we jump, so add ontrue-path */
2540 if (!gen_blocks_recursive(func, ontrue))
2542 /* fixup the jump address */
2543 code_statements[stidx].o2.s1 = vec_size(code_statements) - stidx;
2544 /* generate onfalse path */
2545 if (onfalse->generated) {
2546 /* fixup the jump address */
2547 code_statements[stidx].o2.s1 = (onfalse->code_start) - (stidx);
2548 stmt.opcode = vec_last(code_statements).opcode;
2549 if (stmt.opcode == INSTR_GOTO ||
2550 stmt.opcode == INSTR_IF ||
2551 stmt.opcode == INSTR_IFNOT ||
2552 stmt.opcode == INSTR_RETURN ||
2553 stmt.opcode == INSTR_DONE)
2555 /* no use jumping from here */
2558 /* may have been generated in the previous recursive call */
2559 stmt.opcode = INSTR_GOTO;
2560 stmt.o1.s1 = (onfalse->code_start) - vec_size(code_statements);
2563 vec_push(code_statements, stmt);
2566 /* if not, generate now */
2571 if (instr->opcode >= INSTR_CALL0 && instr->opcode <= INSTR_CALL8) {
2572 /* Trivial call translation:
2573 * copy all params to OFS_PARM*
2574 * if the output's storetype is not store_return,
2575 * add append a STORE instruction!
2577 * NOTES on how to do it better without much trouble:
2578 * -) The liferanges!
2579 * Simply check the liferange of all parameters for
2580 * other CALLs. For each param with no CALL in its
2581 * liferange, we can store it in an OFS_PARM at
2582 * generation already. This would even include later
2583 * reuse.... probably... :)
2588 first = vec_size(instr->params);
2591 for (p = 0; p < first; ++p)
2593 ir_value *param = instr->params[p];
2595 stmt.opcode = INSTR_STORE_F;
2598 if (param->vtype == TYPE_FIELD)
2599 stmt.opcode = field_store_instr[param->fieldtype];
2601 stmt.opcode = type_store_instr[param->vtype];
2602 stmt.o1.u1 = ir_value_code_addr(param);
2603 stmt.o2.u1 = OFS_PARM0 + 3 * p;
2604 vec_push(code_statements, stmt);
2606 /* Now handle extparams */
2607 first = vec_size(instr->params);
2608 for (; p < first; ++p)
2610 ir_builder *ir = func->owner;
2611 ir_value *param = instr->params[p];
2612 ir_value *targetparam;
2614 if (p-8 >= vec_size(ir->extparams)) {
2615 irerror(instr->context, "Not enough extparam-globals have been created");
2619 targetparam = ir->extparams[p-8];
2621 stmt.opcode = INSTR_STORE_F;
2624 if (param->vtype == TYPE_FIELD)
2625 stmt.opcode = field_store_instr[param->fieldtype];
2627 stmt.opcode = type_store_instr[param->vtype];
2628 stmt.o1.u1 = ir_value_code_addr(param);
2629 stmt.o2.u1 = ir_value_code_addr(targetparam);
2630 vec_push(code_statements, stmt);
2633 stmt.opcode = INSTR_CALL0 + vec_size(instr->params);
2634 if (stmt.opcode > INSTR_CALL8)
2635 stmt.opcode = INSTR_CALL8;
2636 stmt.o1.u1 = ir_value_code_addr(instr->_ops[1]);
2639 vec_push(code_statements, stmt);
2641 retvalue = instr->_ops[0];
2642 if (retvalue && retvalue->store != store_return && vec_size(retvalue->life))
2644 /* not to be kept in OFS_RETURN */
2645 if (retvalue->vtype == TYPE_FIELD)
2646 stmt.opcode = field_store_instr[retvalue->vtype];
2648 stmt.opcode = type_store_instr[retvalue->vtype];
2649 stmt.o1.u1 = OFS_RETURN;
2650 stmt.o2.u1 = ir_value_code_addr(retvalue);
2652 vec_push(code_statements, stmt);
2657 if (instr->opcode == INSTR_STATE) {
2658 irerror(block->context, "TODO: state instruction");
2662 stmt.opcode = instr->opcode;
2667 /* This is the general order of operands */
2669 stmt.o3.u1 = ir_value_code_addr(instr->_ops[0]);
2672 stmt.o1.u1 = ir_value_code_addr(instr->_ops[1]);
2675 stmt.o2.u1 = ir_value_code_addr(instr->_ops[2]);
2677 if (stmt.opcode == INSTR_RETURN || stmt.opcode == INSTR_DONE)
2679 stmt.o1.u1 = stmt.o3.u1;
2682 else if ((stmt.opcode >= INSTR_STORE_F &&
2683 stmt.opcode <= INSTR_STORE_FNC) ||
2684 (stmt.opcode >= INSTR_STOREP_F &&
2685 stmt.opcode <= INSTR_STOREP_FNC))
2687 /* 2-operand instructions with A -> B */
2688 stmt.o2.u1 = stmt.o3.u1;
2692 vec_push(code_statements, stmt);
2697 static bool gen_function_code(ir_function *self)
2700 prog_section_statement stmt;
2702 /* Starting from entry point, we generate blocks "as they come"
2703 * for now. Dead blocks will not be translated obviously.
2705 if (!vec_size(self->blocks)) {
2706 irerror(self->context, "Function '%s' declared without body.", self->name);
2710 block = self->blocks[0];
2711 if (block->generated)
2714 if (!gen_blocks_recursive(self, block)) {
2715 irerror(self->context, "failed to generate blocks for '%s'", self->name);
2719 /* otherwise code_write crashes since it debug-prints functions until AINSTR_END */
2720 stmt.opcode = AINSTR_END;
2724 vec_push(code_statements, stmt);
2728 static qcint ir_builder_filestring(ir_builder *ir, const char *filename)
2730 /* NOTE: filename pointers are copied, we never strdup them,
2731 * thus we can use pointer-comparison to find the string.
2736 for (i = 0; i < vec_size(ir->filenames); ++i) {
2737 if (ir->filenames[i] == filename)
2738 return ir->filestrings[i];
2741 str = code_genstring(filename);
2742 vec_push(ir->filenames, filename);
2743 vec_push(ir->filestrings, str);
2747 static bool gen_global_function(ir_builder *ir, ir_value *global)
2749 prog_section_function fun;
2753 size_t local_var_end;
2755 if (!global->isconst || (!global->constval.vfunc))
2757 irerror(global->context, "Invalid state of function-global: not constant: %s", global->name);
2761 irfun = global->constval.vfunc;
2763 fun.name = global->code.name;
2764 fun.file = ir_builder_filestring(ir, global->context.file);
2765 fun.profile = 0; /* always 0 */
2766 fun.nargs = vec_size(irfun->params);
2770 for (i = 0;i < 8; ++i) {
2774 fun.argsize[i] = type_sizeof[irfun->params[i]];
2777 fun.firstlocal = vec_size(code_globals);
2779 local_var_end = fun.firstlocal;
2780 for (i = 0; i < vec_size(irfun->locals); ++i) {
2781 if (!ir_builder_gen_global(ir, irfun->locals[i], true)) {
2782 irerror(irfun->locals[i]->context, "Failed to generate local %s", irfun->locals[i]->name);
2786 if (vec_size(irfun->locals)) {
2787 ir_value *last = vec_last(irfun->locals);
2788 local_var_end = last->code.globaladdr;
2789 local_var_end += type_sizeof[last->vtype];
2791 for (i = 0; i < vec_size(irfun->values); ++i)
2793 /* generate code.globaladdr for ssa values */
2794 ir_value *v = irfun->values[i];
2795 ir_value_code_setaddr(v, local_var_end + v->code.local);
2797 for (i = 0; i < irfun->allocated_locals; ++i) {
2798 /* fill the locals with zeros */
2799 vec_push(code_globals, 0);
2802 fun.locals = vec_size(code_globals) - fun.firstlocal;
2805 fun.entry = irfun->builtin;
2807 irfun->code_function_def = vec_size(code_functions);
2808 fun.entry = vec_size(code_statements);
2811 vec_push(code_functions, fun);
2815 static void ir_gen_extparam(ir_builder *ir)
2817 prog_section_def def;
2821 snprintf(name, sizeof(name), "EXTPARM#%i", (int)(vec_size(ir->extparams)+8));
2822 global = ir_value_var(name, store_global, TYPE_VECTOR);
2824 def.name = code_genstring(name);
2825 def.type = TYPE_VECTOR;
2826 def.offset = vec_size(code_globals);
2828 vec_push(code_defs, def);
2829 ir_value_code_setaddr(global, def.offset);
2830 vec_push(code_globals, 0);
2831 vec_push(code_globals, 0);
2832 vec_push(code_globals, 0);
2834 vec_push(ir->extparams, global);
2837 static bool gen_function_extparam_copy(ir_function *self)
2839 size_t i, ext, numparams;
2841 ir_builder *ir = self->owner;
2843 prog_section_statement stmt;
2845 numparams = vec_size(self->params);
2849 stmt.opcode = INSTR_STORE_F;
2851 for (i = 8; i < numparams; ++i) {
2853 if (ext >= vec_size(ir->extparams))
2854 ir_gen_extparam(ir);
2856 ep = ir->extparams[ext];
2858 stmt.opcode = type_store_instr[self->locals[i]->vtype];
2859 if (self->locals[i]->vtype == TYPE_FIELD &&
2860 self->locals[i]->fieldtype == TYPE_VECTOR)
2862 stmt.opcode = INSTR_STORE_V;
2864 stmt.o1.u1 = ir_value_code_addr(ep);
2865 stmt.o2.u1 = ir_value_code_addr(self->locals[i]);
2866 vec_push(code_statements, stmt);
2872 static bool gen_global_function_code(ir_builder *ir, ir_value *global)
2874 prog_section_function *fundef;
2879 irfun = global->constval.vfunc;
2881 irwarning(global->context, WARN_IMPLICIT_FUNCTION_POINTER,
2882 "function `%s` has no body and in QC implicitly becomes a function-pointer", global->name);
2883 /* this was a function pointer, don't generate code for those */
2890 if (irfun->code_function_def < 0) {
2891 irerror(irfun->context, "`%s`: IR global wasn't generated, failed to access function-def", irfun->name);
2894 fundef = &code_functions[irfun->code_function_def];
2896 fundef->entry = vec_size(code_statements);
2897 if (!gen_function_extparam_copy(irfun)) {
2898 irerror(irfun->context, "Failed to generate extparam-copy code for function %s", irfun->name);
2901 if (!gen_function_code(irfun)) {
2902 irerror(irfun->context, "Failed to generate code for function %s", irfun->name);
2908 static bool ir_builder_gen_global(ir_builder *self, ir_value *global, bool islocal)
2912 prog_section_def def;
2914 def.type = global->vtype;
2915 def.offset = vec_size(code_globals);
2918 if (global->name[0] == '#') {
2919 if (!self->str_immediate)
2920 self->str_immediate = code_genstring("IMMEDIATE");
2921 def.name = global->code.name = self->str_immediate;
2924 def.name = global->code.name = code_genstring(global->name);
2929 switch (global->vtype)
2932 if (!strcmp(global->name, "end_sys_globals")) {
2933 /* TODO: remember this point... all the defs before this one
2934 * should be checksummed and added to progdefs.h when we generate it.
2937 else if (!strcmp(global->name, "end_sys_fields")) {
2938 /* TODO: same as above but for entity-fields rather than globsl
2942 irwarning(global->context, WARN_VOID_VARIABLES, "unrecognized variable of type void `%s`",
2944 /* I'd argue setting it to 0 is sufficient, but maybe some depend on knowing how far
2945 * the system fields actually go? Though the engine knows this anyway...
2946 * Maybe this could be an -foption
2947 * fteqcc creates data for end_sys_* - of size 1, so let's do the same
2949 ir_value_code_setaddr(global, vec_size(code_globals));
2950 vec_push(code_globals, 0);
2952 vec_push(code_defs, def);
2955 vec_push(code_defs, def);
2956 return gen_global_pointer(global);
2958 vec_push(code_defs, def);
2959 return gen_global_field(global);
2964 ir_value_code_setaddr(global, vec_size(code_globals));
2965 if (global->isconst) {
2966 iptr = (int32_t*)&global->constval.ivec[0];
2967 vec_push(code_globals, *iptr);
2969 vec_push(code_globals, 0);
2971 def.type |= DEF_SAVEGLOBAL;
2973 vec_push(code_defs, def);
2975 return global->code.globaladdr >= 0;
2979 ir_value_code_setaddr(global, vec_size(code_globals));
2980 if (global->isconst) {
2981 vec_push(code_globals, code_genstring(global->constval.vstring));
2983 vec_push(code_globals, 0);
2985 def.type |= DEF_SAVEGLOBAL;
2987 vec_push(code_defs, def);
2988 return global->code.globaladdr >= 0;
2993 ir_value_code_setaddr(global, vec_size(code_globals));
2994 if (global->isconst) {
2995 iptr = (int32_t*)&global->constval.ivec[0];
2996 vec_push(code_globals, iptr[0]);
2997 if (global->code.globaladdr < 0)
2999 for (d = 1; d < type_sizeof[global->vtype]; ++d)
3001 vec_push(code_globals, iptr[d]);
3004 vec_push(code_globals, 0);
3005 if (global->code.globaladdr < 0)
3007 for (d = 1; d < type_sizeof[global->vtype]; ++d)
3009 vec_push(code_globals, 0);
3012 def.type |= DEF_SAVEGLOBAL;
3015 vec_push(code_defs, def);
3016 return global->code.globaladdr >= 0;
3019 ir_value_code_setaddr(global, vec_size(code_globals));
3020 if (!global->isconst) {
3021 vec_push(code_globals, 0);
3022 if (global->code.globaladdr < 0)
3025 vec_push(code_globals, vec_size(code_functions));
3026 if (!gen_global_function(self, global))
3029 def.type |= DEF_SAVEGLOBAL;
3031 vec_push(code_defs, def);
3034 /* assume biggest type */
3035 ir_value_code_setaddr(global, vec_size(code_globals));
3036 vec_push(code_globals, 0);
3037 for (i = 1; i < type_sizeof[TYPE_VARIANT]; ++i)
3038 vec_push(code_globals, 0);
3041 /* refuse to create 'void' type or any other fancy business. */
3042 irerror(global->context, "Invalid type for global variable `%s`: %s",
3043 global->name, type_name[global->vtype]);
3048 static bool ir_builder_gen_field(ir_builder *self, ir_value *field)
3050 prog_section_def def;
3051 prog_section_field fld;
3055 def.type = (uint16_t)field->vtype;
3056 def.offset = (uint16_t)vec_size(code_globals);
3058 /* create a global named the same as the field */
3059 if (opts_standard == COMPILER_GMQCC) {
3060 /* in our standard, the global gets a dot prefix */
3061 size_t len = strlen(field->name);
3064 /* we really don't want to have to allocate this, and 1024
3065 * bytes is more than enough for a variable/field name
3067 if (len+2 >= sizeof(name)) {
3068 irerror(field->context, "invalid field name size: %u", (unsigned int)len);
3073 memcpy(name+1, field->name, len); /* no strncpy - we used strlen above */
3076 def.name = code_genstring(name);
3077 fld.name = def.name + 1; /* we reuse that string table entry */
3079 /* in plain QC, there cannot be a global with the same name,
3080 * and so we also name the global the same.
3081 * FIXME: fteqcc should create a global as well
3082 * check if it actually uses the same name. Probably does
3084 def.name = code_genstring(field->name);
3085 fld.name = def.name;
3088 field->code.name = def.name;
3090 vec_push(code_defs, def);
3092 fld.type = field->fieldtype;
3094 if (fld.type == TYPE_VOID) {
3095 irerror(field->context, "field is missing a type: %s - don't know its size", field->name);
3099 fld.offset = code_alloc_field(type_sizeof[field->fieldtype]);
3101 vec_push(code_fields, fld);
3103 ir_value_code_setaddr(field, vec_size(code_globals));
3104 vec_push(code_globals, fld.offset);
3105 if (fld.type == TYPE_VECTOR) {
3106 vec_push(code_globals, fld.offset+1);
3107 vec_push(code_globals, fld.offset+2);
3110 return field->code.globaladdr >= 0;
3113 bool ir_builder_generate(ir_builder *self, const char *filename)
3115 prog_section_statement stmt;
3120 for (i = 0; i < vec_size(self->globals); ++i)
3122 if (!ir_builder_gen_global(self, self->globals[i], false)) {
3127 for (i = 0; i < vec_size(self->fields); ++i)
3129 if (!ir_builder_gen_field(self, self->fields[i])) {
3134 /* generate function code */
3135 for (i = 0; i < vec_size(self->globals); ++i)
3137 if (self->globals[i]->vtype == TYPE_FUNCTION) {
3138 if (!gen_global_function_code(self, self->globals[i])) {
3144 if (vec_size(code_globals) >= 65536) {
3145 irerror(vec_last(self->globals)->context, "This progs file would require more globals than the metadata can handle. Bailing out.");
3149 /* DP errors if the last instruction is not an INSTR_DONE
3150 * and for debugging purposes we add an additional AINSTR_END
3151 * to the end of functions, so here it goes:
3153 stmt.opcode = INSTR_DONE;
3157 vec_push(code_statements, stmt);
3160 con_out("writing '%s'...\n", filename);
3161 return code_write(filename);
3164 /***********************************************************************
3165 *IR DEBUG Dump functions...
3168 #define IND_BUFSZ 1024
3171 # define strncat(dst, src, sz) strncat_s(dst, sz, src, _TRUNCATE)
3174 const char *qc_opname(int op)
3176 if (op < 0) return "<INVALID>";
3177 if (op < (int)( sizeof(asm_instr) / sizeof(asm_instr[0]) ))
3178 return asm_instr[op].m;
3180 case VINSTR_PHI: return "PHI";
3181 case VINSTR_JUMP: return "JUMP";
3182 case VINSTR_COND: return "COND";
3183 default: return "<UNK>";
3187 void ir_builder_dump(ir_builder *b, int (*oprintf)(const char*, ...))
3190 char indent[IND_BUFSZ];
3194 oprintf("module %s\n", b->name);
3195 for (i = 0; i < vec_size(b->globals); ++i)
3198 if (b->globals[i]->isconst)
3199 oprintf("%s = ", b->globals[i]->name);
3200 ir_value_dump(b->globals[i], oprintf);
3203 for (i = 0; i < vec_size(b->functions); ++i)
3204 ir_function_dump(b->functions[i], indent, oprintf);
3205 oprintf("endmodule %s\n", b->name);
3208 void ir_function_dump(ir_function *f, char *ind,
3209 int (*oprintf)(const char*, ...))
3212 if (f->builtin != 0) {
3213 oprintf("%sfunction %s = builtin %i\n", ind, f->name, -f->builtin);
3216 oprintf("%sfunction %s\n", ind, f->name);
3217 strncat(ind, "\t", IND_BUFSZ);
3218 if (vec_size(f->locals))
3220 oprintf("%s%i locals:\n", ind, (int)vec_size(f->locals));
3221 for (i = 0; i < vec_size(f->locals); ++i) {
3222 oprintf("%s\t", ind);
3223 ir_value_dump(f->locals[i], oprintf);
3227 oprintf("%sliferanges:\n", ind);
3228 for (i = 0; i < vec_size(f->locals); ++i) {
3230 ir_value *v = f->locals[i];
3231 oprintf("%s\t%s: unique ", ind, v->name);
3232 for (l = 0; l < vec_size(v->life); ++l) {
3233 oprintf("[%i,%i] ", v->life[l].start, v->life[l].end);
3237 for (i = 0; i < vec_size(f->values); ++i) {
3239 ir_value *v = f->values[i];
3240 oprintf("%s\t%s: @%i ", ind, v->name, (int)v->code.local);
3241 for (l = 0; l < vec_size(v->life); ++l) {
3242 oprintf("[%i,%i] ", v->life[l].start, v->life[l].end);
3246 if (vec_size(f->blocks))
3248 oprintf("%slife passes (check): %i\n", ind, (int)f->run_id);
3249 for (i = 0; i < vec_size(f->blocks); ++i) {
3250 if (f->blocks[i]->run_id != f->run_id) {
3251 oprintf("%slife pass check fail! %i != %i\n", ind, (int)f->blocks[i]->run_id, (int)f->run_id);
3253 ir_block_dump(f->blocks[i], ind, oprintf);
3257 ind[strlen(ind)-1] = 0;
3258 oprintf("%sendfunction %s\n", ind, f->name);
3261 void ir_block_dump(ir_block* b, char *ind,
3262 int (*oprintf)(const char*, ...))
3265 oprintf("%s:%s\n", ind, b->label);
3266 strncat(ind, "\t", IND_BUFSZ);
3268 for (i = 0; i < vec_size(b->instr); ++i)
3269 ir_instr_dump(b->instr[i], ind, oprintf);
3270 ind[strlen(ind)-1] = 0;
3273 void dump_phi(ir_instr *in, int (*oprintf)(const char*, ...))
3276 oprintf("%s <- phi ", in->_ops[0]->name);
3277 for (i = 0; i < vec_size(in->phi); ++i)
3279 oprintf("([%s] : %s) ", in->phi[i].from->label,
3280 in->phi[i].value->name);
3285 void ir_instr_dump(ir_instr *in, char *ind,
3286 int (*oprintf)(const char*, ...))
3289 const char *comma = NULL;
3291 oprintf("%s (%i) ", ind, (int)in->eid);
3293 if (in->opcode == VINSTR_PHI) {
3294 dump_phi(in, oprintf);
3298 strncat(ind, "\t", IND_BUFSZ);
3300 if (in->_ops[0] && (in->_ops[1] || in->_ops[2])) {
3301 ir_value_dump(in->_ops[0], oprintf);
3302 if (in->_ops[1] || in->_ops[2])
3305 if (in->opcode == INSTR_CALL0) {
3306 oprintf("CALL%i\t", vec_size(in->params));
3308 oprintf("%s\t", qc_opname(in->opcode));
3310 if (in->_ops[0] && !(in->_ops[1] || in->_ops[2])) {
3311 ir_value_dump(in->_ops[0], oprintf);
3316 for (i = 1; i != 3; ++i) {
3320 ir_value_dump(in->_ops[i], oprintf);
3328 oprintf("[%s]", in->bops[0]->label);
3332 oprintf("%s[%s]", comma, in->bops[1]->label);
3333 if (vec_size(in->params)) {
3334 oprintf("\tparams: ");
3335 for (i = 0; i != vec_size(in->params); ++i) {
3336 oprintf("%s, ", in->params[i]->name);
3340 ind[strlen(ind)-1] = 0;
3343 void ir_value_dump_string(const char *str, int (*oprintf)(const char*, ...))
3346 for (; *str; ++str) {
3348 case '\n': oprintf("\\n"); break;
3349 case '\r': oprintf("\\r"); break;
3350 case '\t': oprintf("\\t"); break;
3351 case '\v': oprintf("\\v"); break;
3352 case '\f': oprintf("\\f"); break;
3353 case '\b': oprintf("\\b"); break;
3354 case '\a': oprintf("\\a"); break;
3355 case '\\': oprintf("\\\\"); break;
3356 case '"': oprintf("\\\""); break;
3357 default: oprintf("%c", *str); break;
3363 void ir_value_dump(ir_value* v, int (*oprintf)(const char*, ...))
3372 oprintf("fn:%s", v->name);
3375 oprintf("%g", v->constval.vfloat);
3378 oprintf("'%g %g %g'",
3381 v->constval.vvec.z);
3384 oprintf("(entity)");
3387 ir_value_dump_string(v->constval.vstring, oprintf);
3391 oprintf("%i", v->constval.vint);
3396 v->constval.vpointer->name);
3400 oprintf("%s", v->name);
3404 void ir_value_dump_life(const ir_value *self, int (*oprintf)(const char*,...))
3407 oprintf("Life of %12s:", self->name);
3408 for (i = 0; i < vec_size(self->life); ++i)
3410 oprintf(" + [%i, %i]\n", self->life[i].start, self->life[i].end);