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)
2173 /* values which have been read in a previous iteration are now
2174 * in the "living" array even if the previous block doesn't use them.
2175 * So we have to remove whatever does not exist in the previous block.
2176 * They will be re-added on-read, but the liferange merge won't cause
2179 for (i = 0; i < vec_size(self->living); ++i)
2181 if (!vec_ir_value_find(prev->living, self->living[i], NULL)) {
2182 vec_remove(self->living, i, 1);
2187 /* Whatever the previous block still has in its living set
2188 * must now be added to ours as well.
2190 for (i = 0; i < vec_size(prev->living); ++i)
2192 if (vec_ir_value_find(self->living, prev->living[i], NULL))
2194 vec_push(self->living, prev->living[i]);
2196 irerror(self->contextt from prev: %s", self->label, prev->living[i]->_name);
2202 static bool ir_block_life_propagate(ir_block *self, ir_block *prev, bool *changed)
2208 /* bitmasks which operands are read from or written to */
2210 char dbg_ind[16] = { '#', '0' };
2215 if (!ir_block_life_prop_previous(self, prev, changed))
2219 i = vec_size(self->instr);
2222 instr = self->instr[i];
2224 /* PHI operands are always read operands */
2225 for (p = 0; p < vec_size(instr->phi); ++p)
2227 value = instr->phi[p].value;
2228 if (value->memberof)
2229 value = value->memberof;
2230 if (!vec_ir_value_find(self->living, value, NULL))
2231 vec_push(self->living, value);
2234 /* call params are read operands too */
2235 for (p = 0; p < vec_size(instr->params); ++p)
2237 value = instr->params[p];
2238 if (value->memberof)
2239 value = value->memberof;
2240 if (!vec_ir_value_find(self->living, value, NULL))
2241 vec_push(self->living, value);
2244 /* See which operands are read and write operands */
2245 ir_op_read_write(instr->opcode, &read, &write);
2247 if (instr->opcode == INSTR_MUL_VF)
2249 /* the float source will get an additional lifetime */
2250 tempbool = ir_value_life_merge(instr->_ops[2], instr->eid+1);
2251 *changed = *changed || tempbool;
2253 else if (instr->opcode == INSTR_MUL_FV)
2255 /* the float source will get an additional lifetime */
2256 tempbool = ir_value_life_merge(instr->_ops[1], instr->eid+1);
2257 *changed = *changed || tempbool;
2260 /* Go through the 3 main operands */
2261 for (o = 0; o < 3; ++o)
2263 if (!instr->_ops[o]) /* no such operand */
2266 value = instr->_ops[o];
2267 if (value->memberof)
2268 value = value->memberof;
2270 /* We only care about locals */
2271 /* we also calculate parameter liferanges so that locals
2272 * can take up parameter slots */
2273 if (value->store != store_value &&
2274 value->store != store_local &&
2275 value->store != store_param)
2281 if (!vec_ir_value_find(self->living, value, NULL))
2282 vec_push(self->living, value);
2285 /* write operands */
2286 /* When we write to a local, we consider it "dead" for the
2287 * remaining upper part of the function, since in SSA a value
2288 * can only be written once (== created)
2293 bool in_living = vec_ir_value_find(self->living, value, &idx);
2296 /* If the value isn't alive it hasn't been read before... */
2297 /* TODO: See if the warning can be emitted during parsing or AST processing
2298 * otherwise have warning printed here.
2299 * IF printing a warning here: include filecontext_t,
2300 * and make sure it's only printed once
2301 * since this function is run multiple times.
2303 /* For now: debug info: */
2304 /* con_err( "Value only written %s\n", value->name); */
2305 tempbool = ir_value_life_merge(value, instr->eid);
2306 *changed = *changed || tempbool;
2308 ir_instr_dump(instr, dbg_ind, printf);
2312 /* since 'living' won't contain it
2313 * anymore, merge the value, since
2316 tempbool = ir_value_life_merge(value, instr->eid);
2319 con_err( "value added id %s %i\n", value->name, (int)instr->eid);
2321 *changed = *changed || tempbool;
2323 vec_remove(self->living, idx, 1);
2328 tempbool = ir_block_living_add_instr(self, instr->eid);
2329 /*con_err( "living added values\n");*/
2330 *changed = *changed || tempbool;
2334 if (self->run_id == self->owner->run_id)
2337 self->run_id = self->owner->run_id;
2339 for (i = 0; i < vec_size(self->entries); ++i)
2341 ir_block *entry = self->entries[i];
2342 ir_block_life_propagate(entry, self, changed);
2348 /***********************************************************************
2351 * Since the IR has the convention of putting 'write' operands
2352 * at the beginning, we have to rotate the operands of instructions
2353 * properly in order to generate valid QCVM code.
2355 * Having destinations at a fixed position is more convenient. In QC
2356 * this is *mostly* OPC, but FTE adds at least 2 instructions which
2357 * read from from OPA, and store to OPB rather than OPC. Which is
2358 * partially the reason why the implementation of these instructions
2359 * in darkplaces has been delayed for so long.
2361 * Breaking conventions is annoying...
2363 static bool ir_builder_gen_global(ir_builder *self, ir_value *global, bool islocal);
2365 static bool gen_global_field(ir_value *global)
2367 if (global->isconst)
2369 ir_value *fld = global->constval.vpointer;
2371 irerror(global->context, "Invalid field constant with no field: %s", global->name);
2375 /* Now, in this case, a relocation would be impossible to code
2376 * since it looks like this:
2377 * .vector v = origin; <- parse error, wtf is 'origin'?
2380 * But we will need a general relocation support later anyway
2381 * for functions... might as well support that here.
2383 if (!fld->code.globaladdr) {
2384 irerror(global->context, "FIXME: Relocation support");
2388 /* copy the field's value */
2389 ir_value_code_setaddr(global, vec_size(code_globals));
2390 vec_push(code_globals, code_globals[fld->code.globaladdr]);
2391 if (global->fieldtype == TYPE_VECTOR) {
2392 vec_push(code_globals, code_globals[fld->code.globaladdr]+1);
2393 vec_push(code_globals, code_globals[fld->code.globaladdr]+2);
2398 ir_value_code_setaddr(global, vec_size(code_globals));
2399 vec_push(code_globals, 0);
2400 if (global->fieldtype == TYPE_VECTOR) {
2401 vec_push(code_globals, 0);
2402 vec_push(code_globals, 0);
2405 if (global->code.globaladdr < 0)
2410 static bool gen_global_pointer(ir_value *global)
2412 if (global->isconst)
2414 ir_value *target = global->constval.vpointer;
2416 irerror(global->context, "Invalid pointer constant: %s", global->name);
2417 /* NULL pointers are pointing to the NULL constant, which also
2418 * sits at address 0, but still has an ir_value for itself.
2423 /* Here, relocations ARE possible - in fteqcc-enhanced-qc:
2424 * void() foo; <- proto
2425 * void() *fooptr = &foo;
2426 * void() foo = { code }
2428 if (!target->code.globaladdr) {
2429 /* FIXME: Check for the constant nullptr ir_value!
2430 * because then code.globaladdr being 0 is valid.
2432 irerror(global->context, "FIXME: Relocation support");
2436 ir_value_code_setaddr(global, vec_size(code_globals));
2437 vec_push(code_globals, target->code.globaladdr);
2441 ir_value_code_setaddr(global, vec_size(code_globals));
2442 vec_push(code_globals, 0);
2444 if (global->code.globaladdr < 0)
2449 static bool gen_blocks_recursive(ir_function *func, ir_block *block)
2451 prog_section_statement stmt;
2460 block->generated = true;
2461 block->code_start = vec_size(code_statements);
2462 for (i = 0; i < vec_size(block->instr); ++i)
2464 instr = block->instr[i];
2466 if (instr->opcode == VINSTR_PHI) {
2467 irerror(block->context, "cannot generate virtual instruction (phi)");
2471 if (instr->opcode == VINSTR_JUMP) {
2472 target = instr->bops[0];
2473 /* for uncoditional jumps, if the target hasn't been generated
2474 * yet, we generate them right here.
2476 if (!target->generated) {
2481 /* otherwise we generate a jump instruction */
2482 stmt.opcode = INSTR_GOTO;
2483 stmt.o1.s1 = (target->code_start) - vec_size(code_statements);
2486 vec_push(code_statements, stmt);
2488 /* no further instructions can be in this block */
2492 if (instr->opcode == VINSTR_COND) {
2493 ontrue = instr->bops[0];
2494 onfalse = instr->bops[1];
2495 /* TODO: have the AST signal which block should
2496 * come first: eg. optimize IFs without ELSE...
2499 stmt.o1.u1 = ir_value_code_addr(instr->_ops[0]);
2503 if (ontrue->generated) {
2504 stmt.opcode = INSTR_IF;
2505 stmt.o2.s1 = (ontrue->code_start) - vec_size(code_statements);
2506 vec_push(code_statements, stmt);
2508 if (onfalse->generated) {
2509 stmt.opcode = INSTR_IFNOT;
2510 stmt.o2.s1 = (onfalse->code_start) - vec_size(code_statements);
2511 vec_push(code_statements, stmt);
2513 if (!ontrue->generated) {
2514 if (onfalse->generated) {
2519 if (!onfalse->generated) {
2520 if (ontrue->generated) {
2525 /* neither ontrue nor onfalse exist */
2526 stmt.opcode = INSTR_IFNOT;
2527 if (!instr->likely) {
2528 /* Honor the likelyhood hint */
2529 ir_block *tmp = onfalse;
2530 stmt.opcode = INSTR_IF;
2534 stidx = vec_size(code_statements);
2535 vec_push(code_statements, stmt);
2536 /* on false we jump, so add ontrue-path */
2537 if (!gen_blocks_recursive(func, ontrue))
2539 /* fixup the jump address */
2540 code_statements[stidx].o2.s1 = vec_size(code_statements) - stidx;
2541 /* generate onfalse path */
2542 if (onfalse->generated) {
2543 /* fixup the jump address */
2544 code_statements[stidx].o2.s1 = (onfalse->code_start) - (stidx);
2545 stmt.opcode = vec_last(code_statements).opcode;
2546 if (stmt.opcode == INSTR_GOTO ||
2547 stmt.opcode == INSTR_IF ||
2548 stmt.opcode == INSTR_IFNOT ||
2549 stmt.opcode == INSTR_RETURN ||
2550 stmt.opcode == INSTR_DONE)
2552 /* no use jumping from here */
2555 /* may have been generated in the previous recursive call */
2556 stmt.opcode = INSTR_GOTO;
2557 stmt.o1.s1 = (onfalse->code_start) - vec_size(code_statements);
2560 vec_push(code_statements, stmt);
2563 /* if not, generate now */
2568 if (instr->opcode >= INSTR_CALL0 && instr->opcode <= INSTR_CALL8) {
2569 /* Trivial call translation:
2570 * copy all params to OFS_PARM*
2571 * if the output's storetype is not store_return,
2572 * add append a STORE instruction!
2574 * NOTES on how to do it better without much trouble:
2575 * -) The liferanges!
2576 * Simply check the liferange of all parameters for
2577 * other CALLs. For each param with no CALL in its
2578 * liferange, we can store it in an OFS_PARM at
2579 * generation already. This would even include later
2580 * reuse.... probably... :)
2585 first = vec_size(instr->params);
2588 for (p = 0; p < first; ++p)
2590 ir_value *param = instr->params[p];
2592 stmt.opcode = INSTR_STORE_F;
2595 if (param->vtype == TYPE_FIELD)
2596 stmt.opcode = field_store_instr[param->fieldtype];
2598 stmt.opcode = type_store_instr[param->vtype];
2599 stmt.o1.u1 = ir_value_code_addr(param);
2600 stmt.o2.u1 = OFS_PARM0 + 3 * p;
2601 vec_push(code_statements, stmt);
2603 /* Now handle extparams */
2604 first = vec_size(instr->params);
2605 for (; p < first; ++p)
2607 ir_builder *ir = func->owner;
2608 ir_value *param = instr->params[p];
2609 ir_value *targetparam;
2611 if (p-8 >= vec_size(ir->extparams)) {
2612 irerror(instr->context, "Not enough extparam-globals have been created");
2616 targetparam = ir->extparams[p-8];
2618 stmt.opcode = INSTR_STORE_F;
2621 if (param->vtype == TYPE_FIELD)
2622 stmt.opcode = field_store_instr[param->fieldtype];
2624 stmt.opcode = type_store_instr[param->vtype];
2625 stmt.o1.u1 = ir_value_code_addr(param);
2626 stmt.o2.u1 = ir_value_code_addr(targetparam);
2627 vec_push(code_statements, stmt);
2630 stmt.opcode = INSTR_CALL0 + vec_size(instr->params);
2631 if (stmt.opcode > INSTR_CALL8)
2632 stmt.opcode = INSTR_CALL8;
2633 stmt.o1.u1 = ir_value_code_addr(instr->_ops[1]);
2636 vec_push(code_statements, stmt);
2638 retvalue = instr->_ops[0];
2639 if (retvalue && retvalue->store != store_return && vec_size(retvalue->life))
2641 /* not to be kept in OFS_RETURN */
2642 if (retvalue->vtype == TYPE_FIELD)
2643 stmt.opcode = field_store_instr[retvalue->vtype];
2645 stmt.opcode = type_store_instr[retvalue->vtype];
2646 stmt.o1.u1 = OFS_RETURN;
2647 stmt.o2.u1 = ir_value_code_addr(retvalue);
2649 vec_push(code_statements, stmt);
2654 if (instr->opcode == INSTR_STATE) {
2655 irerror(block->context, "TODO: state instruction");
2659 stmt.opcode = instr->opcode;
2664 /* This is the general order of operands */
2666 stmt.o3.u1 = ir_value_code_addr(instr->_ops[0]);
2669 stmt.o1.u1 = ir_value_code_addr(instr->_ops[1]);
2672 stmt.o2.u1 = ir_value_code_addr(instr->_ops[2]);
2674 if (stmt.opcode == INSTR_RETURN || stmt.opcode == INSTR_DONE)
2676 stmt.o1.u1 = stmt.o3.u1;
2679 else if ((stmt.opcode >= INSTR_STORE_F &&
2680 stmt.opcode <= INSTR_STORE_FNC) ||
2681 (stmt.opcode >= INSTR_STOREP_F &&
2682 stmt.opcode <= INSTR_STOREP_FNC))
2684 /* 2-operand instructions with A -> B */
2685 stmt.o2.u1 = stmt.o3.u1;
2689 vec_push(code_statements, stmt);
2694 static bool gen_function_code(ir_function *self)
2697 prog_section_statement stmt;
2699 /* Starting from entry point, we generate blocks "as they come"
2700 * for now. Dead blocks will not be translated obviously.
2702 if (!vec_size(self->blocks)) {
2703 irerror(self->context, "Function '%s' declared without body.", self->name);
2707 block = self->blocks[0];
2708 if (block->generated)
2711 if (!gen_blocks_recursive(self, block)) {
2712 irerror(self->context, "failed to generate blocks for '%s'", self->name);
2716 /* otherwise code_write crashes since it debug-prints functions until AINSTR_END */
2717 stmt.opcode = AINSTR_END;
2721 vec_push(code_statements, stmt);
2725 static qcint ir_builder_filestring(ir_builder *ir, const char *filename)
2727 /* NOTE: filename pointers are copied, we never strdup them,
2728 * thus we can use pointer-comparison to find the string.
2733 for (i = 0; i < vec_size(ir->filenames); ++i) {
2734 if (ir->filenames[i] == filename)
2735 return ir->filestrings[i];
2738 str = code_genstring(filename);
2739 vec_push(ir->filenames, filename);
2740 vec_push(ir->filestrings, str);
2744 static bool gen_global_function(ir_builder *ir, ir_value *global)
2746 prog_section_function fun;
2750 size_t local_var_end;
2752 if (!global->isconst || (!global->constval.vfunc))
2754 irerror(global->context, "Invalid state of function-global: not constant: %s", global->name);
2758 irfun = global->constval.vfunc;
2760 fun.name = global->code.name;
2761 fun.file = ir_builder_filestring(ir, global->context.file);
2762 fun.profile = 0; /* always 0 */
2763 fun.nargs = vec_size(irfun->params);
2767 for (i = 0;i < 8; ++i) {
2771 fun.argsize[i] = type_sizeof[irfun->params[i]];
2774 fun.firstlocal = vec_size(code_globals);
2776 local_var_end = fun.firstlocal;
2777 for (i = 0; i < vec_size(irfun->locals); ++i) {
2778 if (!ir_builder_gen_global(ir, irfun->locals[i], true)) {
2779 irerror(irfun->locals[i]->context, "Failed to generate local %s", irfun->locals[i]->name);
2783 if (vec_size(irfun->locals)) {
2784 ir_value *last = vec_last(irfun->locals);
2785 local_var_end = last->code.globaladdr;
2786 local_var_end += type_sizeof[last->vtype];
2788 for (i = 0; i < vec_size(irfun->values); ++i)
2790 /* generate code.globaladdr for ssa values */
2791 ir_value *v = irfun->values[i];
2792 ir_value_code_setaddr(v, local_var_end + v->code.local);
2794 for (i = 0; i < irfun->allocated_locals; ++i) {
2795 /* fill the locals with zeros */
2796 vec_push(code_globals, 0);
2799 fun.locals = vec_size(code_globals) - fun.firstlocal;
2802 fun.entry = irfun->builtin;
2804 irfun->code_function_def = vec_size(code_functions);
2805 fun.entry = vec_size(code_statements);
2808 vec_push(code_functions, fun);
2812 static void ir_gen_extparam(ir_builder *ir)
2814 prog_section_def def;
2818 snprintf(name, sizeof(name), "EXTPARM#%i", (int)(vec_size(ir->extparams)+8));
2819 global = ir_value_var(name, store_global, TYPE_VECTOR);
2821 def.name = code_genstring(name);
2822 def.type = TYPE_VECTOR;
2823 def.offset = vec_size(code_globals);
2825 vec_push(code_defs, def);
2826 ir_value_code_setaddr(global, def.offset);
2827 vec_push(code_globals, 0);
2828 vec_push(code_globals, 0);
2829 vec_push(code_globals, 0);
2831 vec_push(ir->extparams, global);
2834 static bool gen_function_extparam_copy(ir_function *self)
2836 size_t i, ext, numparams;
2838 ir_builder *ir = self->owner;
2840 prog_section_statement stmt;
2842 numparams = vec_size(self->params);
2846 stmt.opcode = INSTR_STORE_F;
2848 for (i = 8; i < numparams; ++i) {
2850 if (ext >= vec_size(ir->extparams))
2851 ir_gen_extparam(ir);
2853 ep = ir->extparams[ext];
2855 stmt.opcode = type_store_instr[self->locals[i]->vtype];
2856 if (self->locals[i]->vtype == TYPE_FIELD &&
2857 self->locals[i]->fieldtype == TYPE_VECTOR)
2859 stmt.opcode = INSTR_STORE_V;
2861 stmt.o1.u1 = ir_value_code_addr(ep);
2862 stmt.o2.u1 = ir_value_code_addr(self->locals[i]);
2863 vec_push(code_statements, stmt);
2869 static bool gen_global_function_code(ir_builder *ir, ir_value *global)
2871 prog_section_function *fundef;
2874 irfun = global->constval.vfunc;
2876 irwarning(global->context, WARN_IMPLICIT_FUNCTION_POINTER,
2877 "function `%s` has no body and in QC implicitly becomes a function-pointer", global->name);
2878 /* this was a function pointer, don't generate code for those */
2885 if (irfun->code_function_def < 0) {
2886 irerror(irfun->context, "`%s`: IR global wasn't generated, failed to access function-def", irfun->name);
2889 fundef = &code_functions[irfun->code_function_def];
2891 fundef->entry = vec_size(code_statements);
2892 if (!gen_function_extparam_copy(irfun)) {
2893 irerror(irfun->context, "Failed to generate extparam-copy code for function %s", irfun->name);
2896 if (!gen_function_code(irfun)) {
2897 irerror(irfun->context, "Failed to generate code for function %s", irfun->name);
2903 static bool ir_builder_gen_global(ir_builder *self, ir_value *global, bool islocal)
2907 prog_section_def def;
2909 def.type = global->vtype;
2910 def.offset = vec_size(code_globals);
2913 if (global->name[0] == '#') {
2914 if (!self->str_immediate)
2915 self->str_immediate = code_genstring("IMMEDIATE");
2916 def.name = global->code.name = self->str_immediate;
2919 def.name = global->code.name = code_genstring(global->name);
2924 switch (global->vtype)
2927 if (!strcmp(global->name, "end_sys_globals")) {
2928 /* TODO: remember this point... all the defs before this one
2929 * should be checksummed and added to progdefs.h when we generate it.
2932 else if (!strcmp(global->name, "end_sys_fields")) {
2933 /* TODO: same as above but for entity-fields rather than globsl
2937 irwarning(global->context, WARN_VOID_VARIABLES, "unrecognized variable of type void `%s`",
2939 /* I'd argue setting it to 0 is sufficient, but maybe some depend on knowing how far
2940 * the system fields actually go? Though the engine knows this anyway...
2941 * Maybe this could be an -foption
2942 * fteqcc creates data for end_sys_* - of size 1, so let's do the same
2944 ir_value_code_setaddr(global, vec_size(code_globals));
2945 vec_push(code_globals, 0);
2947 vec_push(code_defs, def);
2950 vec_push(code_defs, def);
2951 return gen_global_pointer(global);
2953 vec_push(code_defs, def);
2954 return gen_global_field(global);
2959 ir_value_code_setaddr(global, vec_size(code_globals));
2960 if (global->isconst) {
2961 iptr = (int32_t*)&global->constval.ivec[0];
2962 vec_push(code_globals, *iptr);
2964 vec_push(code_globals, 0);
2966 def.type |= DEF_SAVEGLOBAL;
2968 vec_push(code_defs, def);
2970 return global->code.globaladdr >= 0;
2974 ir_value_code_setaddr(global, vec_size(code_globals));
2975 if (global->isconst) {
2976 vec_push(code_globals, code_genstring(global->constval.vstring));
2978 vec_push(code_globals, 0);
2980 def.type |= DEF_SAVEGLOBAL;
2982 vec_push(code_defs, def);
2983 return global->code.globaladdr >= 0;
2988 ir_value_code_setaddr(global, vec_size(code_globals));
2989 if (global->isconst) {
2990 iptr = (int32_t*)&global->constval.ivec[0];
2991 vec_push(code_globals, iptr[0]);
2992 if (global->code.globaladdr < 0)
2994 for (d = 1; d < type_sizeof[global->vtype]; ++d)
2996 vec_push(code_globals, iptr[d]);
2999 vec_push(code_globals, 0);
3000 if (global->code.globaladdr < 0)
3002 for (d = 1; d < type_sizeof[global->vtype]; ++d)
3004 vec_push(code_globals, 0);
3007 def.type |= DEF_SAVEGLOBAL;
3010 vec_push(code_defs, def);
3011 return global->code.globaladdr >= 0;
3014 ir_value_code_setaddr(global, vec_size(code_globals));
3015 if (!global->isconst) {
3016 vec_push(code_globals, 0);
3017 if (global->code.globaladdr < 0)
3020 vec_push(code_globals, vec_size(code_functions));
3021 if (!gen_global_function(self, global))
3024 def.type |= DEF_SAVEGLOBAL;
3026 vec_push(code_defs, def);
3029 /* assume biggest type */
3030 ir_value_code_setaddr(global, vec_size(code_globals));
3031 vec_push(code_globals, 0);
3032 for (i = 1; i < type_sizeof[TYPE_VARIANT]; ++i)
3033 vec_push(code_globals, 0);
3036 /* refuse to create 'void' type or any other fancy business. */
3037 irerror(global->context, "Invalid type for global variable `%s`: %s",
3038 global->name, type_name[global->vtype]);
3043 static bool ir_builder_gen_field(ir_builder *self, ir_value *field)
3045 prog_section_def def;
3046 prog_section_field fld;
3048 def.type = field->vtype;
3049 def.offset = vec_size(code_globals);
3051 /* create a global named the same as the field */
3052 if (opts_standard == COMPILER_GMQCC) {
3053 /* in our standard, the global gets a dot prefix */
3054 size_t len = strlen(field->name);
3057 /* we really don't want to have to allocate this, and 1024
3058 * bytes is more than enough for a variable/field name
3060 if (len+2 >= sizeof(name)) {
3061 irerror(field->context, "invalid field name size: %u", (unsigned int)len);
3066 memcpy(name+1, field->name, len); /* no strncpy - we used strlen above */
3069 def.name = code_genstring(name);
3070 fld.name = def.name + 1; /* we reuse that string table entry */
3072 /* in plain QC, there cannot be a global with the same name,
3073 * and so we also name the global the same.
3074 * FIXME: fteqcc should create a global as well
3075 * check if it actually uses the same name. Probably does
3077 def.name = code_genstring(field->name);
3078 fld.name = def.name;
3081 field->code.name = def.name;
3083 vec_push(code_defs, def);
3085 fld.type = field->fieldtype;
3087 if (fld.type == TYPE_VOID) {
3088 irerror(field->context, "field is missing a type: %s - don't know its size", field->name);
3092 fld.offset = code_alloc_field(type_sizeof[field->fieldtype]);
3094 vec_push(code_fields, fld);
3096 ir_value_code_setaddr(field, vec_size(code_globals));
3097 vec_push(code_globals, fld.offset);
3098 if (fld.type == TYPE_VECTOR) {
3099 vec_push(code_globals, fld.offset+1);
3100 vec_push(code_globals, fld.offset+2);
3103 return field->code.globaladdr >= 0;
3106 bool ir_builder_generate(ir_builder *self, const char *filename)
3108 prog_section_statement stmt;
3113 for (i = 0; i < vec_size(self->globals); ++i)
3115 if (!ir_builder_gen_global(self, self->globals[i], false)) {
3120 for (i = 0; i < vec_size(self->fields); ++i)
3122 if (!ir_builder_gen_field(self, self->fields[i])) {
3127 /* generate function code */
3128 for (i = 0; i < vec_size(self->globals); ++i)
3130 if (self->globals[i]->vtype == TYPE_FUNCTION) {
3131 if (!gen_global_function_code(self, self->globals[i])) {
3137 /* DP errors if the last instruction is not an INSTR_DONE
3138 * and for debugging purposes we add an additional AINSTR_END
3139 * to the end of functions, so here it goes:
3141 stmt.opcode = INSTR_DONE;
3145 vec_push(code_statements, stmt);
3148 con_out("writing '%s'...\n", filename);
3149 return code_write(filename);
3152 /***********************************************************************
3153 *IR DEBUG Dump functions...
3156 #define IND_BUFSZ 1024
3159 # define strncat(dst, src, sz) strncat_s(dst, sz, src, _TRUNCATE)
3162 const char *qc_opname(int op)
3164 if (op < 0) return "<INVALID>";
3165 if (op < ( sizeof(asm_instr) / sizeof(asm_instr[0]) ))
3166 return asm_instr[op].m;
3168 case VINSTR_PHI: return "PHI";
3169 case VINSTR_JUMP: return "JUMP";
3170 case VINSTR_COND: return "COND";
3171 default: return "<UNK>";
3175 void ir_builder_dump(ir_builder *b, int (*oprintf)(const char*, ...))
3178 char indent[IND_BUFSZ];
3182 oprintf("module %s\n", b->name);
3183 for (i = 0; i < vec_size(b->globals); ++i)
3186 if (b->globals[i]->isconst)
3187 oprintf("%s = ", b->globals[i]->name);
3188 ir_value_dump(b->globals[i], oprintf);
3191 for (i = 0; i < vec_size(b->functions); ++i)
3192 ir_function_dump(b->functions[i], indent, oprintf);
3193 oprintf("endmodule %s\n", b->name);
3196 void ir_function_dump(ir_function *f, char *ind,
3197 int (*oprintf)(const char*, ...))
3200 if (f->builtin != 0) {
3201 oprintf("%sfunction %s = builtin %i\n", ind, f->name, -f->builtin);
3204 oprintf("%sfunction %s\n", ind, f->name);
3205 strncat(ind, "\t", IND_BUFSZ);
3206 if (vec_size(f->locals))
3208 oprintf("%s%i locals:\n", ind, (int)vec_size(f->locals));
3209 for (i = 0; i < vec_size(f->locals); ++i) {
3210 oprintf("%s\t", ind);
3211 ir_value_dump(f->locals[i], oprintf);
3215 oprintf("%sliferanges:\n", ind);
3216 for (i = 0; i < vec_size(f->locals); ++i) {
3218 ir_value *v = f->locals[i];
3219 oprintf("%s\t%s: unique ", ind, v->name);
3220 for (l = 0; l < vec_size(v->life); ++l) {
3221 oprintf("[%i,%i] ", v->life[l].start, v->life[l].end);
3225 for (i = 0; i < vec_size(f->values); ++i) {
3227 ir_value *v = f->values[i];
3228 oprintf("%s\t%s: @%i ", ind, v->name, (int)v->code.local);
3229 for (l = 0; l < vec_size(v->life); ++l) {
3230 oprintf("[%i,%i] ", v->life[l].start, v->life[l].end);
3234 if (vec_size(f->blocks))
3236 oprintf("%slife passes (check): %i\n", ind, (int)f->run_id);
3237 for (i = 0; i < vec_size(f->blocks); ++i) {
3238 if (f->blocks[i]->run_id != f->run_id) {
3239 oprintf("%slife pass check fail! %i != %i\n", ind, (int)f->blocks[i]->run_id, (int)f->run_id);
3241 ir_block_dump(f->blocks[i], ind, oprintf);
3245 ind[strlen(ind)-1] = 0;
3246 oprintf("%sendfunction %s\n", ind, f->name);
3249 void ir_block_dump(ir_block* b, char *ind,
3250 int (*oprintf)(const char*, ...))
3253 oprintf("%s:%s\n", ind, b->label);
3254 strncat(ind, "\t", IND_BUFSZ);
3256 for (i = 0; i < vec_size(b->instr); ++i)
3257 ir_instr_dump(b->instr[i], ind, oprintf);
3258 ind[strlen(ind)-1] = 0;
3261 void dump_phi(ir_instr *in, char *ind,
3262 int (*oprintf)(const char*, ...))
3265 oprintf("%s <- phi ", in->_ops[0]->name);
3266 for (i = 0; i < vec_size(in->phi); ++i)
3268 oprintf("([%s] : %s) ", in->phi[i].from->label,
3269 in->phi[i].value->name);
3274 void ir_instr_dump(ir_instr *in, char *ind,
3275 int (*oprintf)(const char*, ...))
3278 const char *comma = NULL;
3280 oprintf("%s (%i) ", ind, (int)in->eid);
3282 if (in->opcode == VINSTR_PHI) {
3283 dump_phi(in, ind, oprintf);
3287 strncat(ind, "\t", IND_BUFSZ);
3289 if (in->_ops[0] && (in->_ops[1] || in->_ops[2])) {
3290 ir_value_dump(in->_ops[0], oprintf);
3291 if (in->_ops[1] || in->_ops[2])
3294 if (in->opcode == INSTR_CALL0) {
3295 oprintf("CALL%i\t", vec_size(in->params));
3297 oprintf("%s\t", qc_opname(in->opcode));
3299 if (in->_ops[0] && !(in->_ops[1] || in->_ops[2])) {
3300 ir_value_dump(in->_ops[0], oprintf);
3305 for (i = 1; i != 3; ++i) {
3309 ir_value_dump(in->_ops[i], oprintf);
3317 oprintf("[%s]", in->bops[0]->label);
3321 oprintf("%s[%s]", comma, in->bops[1]->label);
3322 if (vec_size(in->params)) {
3323 oprintf("\tparams: ");
3324 for (i = 0; i != vec_size(in->params); ++i) {
3325 oprintf("%s, ", in->params[i]->name);
3329 ind[strlen(ind)-1] = 0;
3332 void ir_value_dump_string(const char *str, int (*oprintf)(const char*, ...))
3335 for (; *str; ++str) {
3337 case '\n': oprintf("\\n"); break;
3338 case '\r': oprintf("\\r"); break;
3339 case '\t': oprintf("\\t"); break;
3340 case '\v': oprintf("\\v"); break;
3341 case '\f': oprintf("\\f"); break;
3342 case '\b': oprintf("\\b"); break;
3343 case '\a': oprintf("\\a"); break;
3344 case '\\': oprintf("\\\\"); break;
3345 case '"': oprintf("\\\""); break;
3346 default: oprintf("%c", *str); break;
3352 void ir_value_dump(ir_value* v, int (*oprintf)(const char*, ...))
3361 oprintf("fn:%s", v->name);
3364 oprintf("%g", v->constval.vfloat);
3367 oprintf("'%g %g %g'",
3370 v->constval.vvec.z);
3373 oprintf("(entity)");
3376 ir_value_dump_string(v->constval.vstring, oprintf);
3380 oprintf("%i", v->constval.vint);
3385 v->constval.vpointer->name);
3389 oprintf("%s", v->name);
3393 void ir_value_dump_life(const ir_value *self, int (*oprintf)(const char*,...))
3396 oprintf("Life of %12s:", self->name);
3397 for (i = 0; i < vec_size(self->life); ++i)
3399 oprintf(" + [%i, %i]\n", self->life[i].start, self->life[i].end);