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))
546 if (!ir_function_allocate_locals(self))
551 ir_value* ir_function_get_local(ir_function *self, const char *name)
554 for (i = 0; i < vec_size(self->locals); ++i) {
555 if (!strcmp(self->locals[i]->name, name))
556 return self->locals[i];
561 ir_value* ir_function_create_local(ir_function *self, const char *name, int vtype, bool param)
566 if (ir_function_get_local(self, name))
571 vec_size(self->locals) &&
572 self->locals[vec_size(self->locals)-1]->store != store_param) {
573 irerror(self->context, "cannot add parameters after adding locals");
577 ve = ir_value_var(name, (param ? store_param : store_local), vtype);
578 vec_push(self->locals, ve);
582 /***********************************************************************
586 ir_block* ir_block_new(ir_function* owner, const char *name)
589 self = (ir_block*)mem_a(sizeof(*self));
593 memset(self, 0, sizeof(*self));
596 if (name && !ir_block_set_label(self, name)) {
601 self->context.file = "<@no context>";
602 self->context.line = 0;
606 self->entries = NULL;
610 self->is_return = false;
615 self->generated = false;
620 static void ir_block_delete_quick(ir_block* self)
623 if (self->label) mem_d(self->label);
624 for (i = 0; i != vec_size(self->instr); ++i)
625 ir_instr_delete_quick(self->instr[i]);
626 vec_free(self->instr);
627 vec_free(self->entries);
628 vec_free(self->exits);
629 vec_free(self->living);
633 void ir_block_delete(ir_block* self)
636 if (self->label) mem_d(self->label);
637 for (i = 0; i != vec_size(self->instr); ++i)
638 ir_instr_delete(self->instr[i]);
639 vec_free(self->instr);
640 vec_free(self->entries);
641 vec_free(self->exits);
642 vec_free(self->living);
646 bool ir_block_set_label(ir_block *self, const char *name)
649 mem_d((void*)self->label);
650 self->label = util_strdup(name);
651 return !!self->label;
654 /***********************************************************************
658 ir_instr* ir_instr_new(ir_block* owner, int op)
661 self = (ir_instr*)mem_a(sizeof(*self));
666 self->context.file = "<@no context>";
667 self->context.line = 0;
669 self->_ops[0] = NULL;
670 self->_ops[1] = NULL;
671 self->_ops[2] = NULL;
672 self->bops[0] = NULL;
673 self->bops[1] = NULL;
684 static void ir_instr_delete_quick(ir_instr *self)
687 vec_free(self->params);
691 void ir_instr_delete(ir_instr *self)
694 /* The following calls can only delete from
695 * vectors, we still want to delete this instruction
696 * so ignore the return value. Since with the warn_unused_result attribute
697 * gcc doesn't care about an explicit: (void)foo(); to ignore the result,
698 * I have to improvise here and use if(foo());
700 for (i = 0; i < vec_size(self->phi); ++i) {
702 if (vec_ir_instr_find(self->phi[i].value->writes, self, &idx))
703 vec_remove(self->phi[i].value->writes, idx, 1);
704 if (vec_ir_instr_find(self->phi[i].value->reads, self, &idx))
705 vec_remove(self->phi[i].value->reads, idx, 1);
708 for (i = 0; i < vec_size(self->params); ++i) {
710 if (vec_ir_instr_find(self->params[i]->writes, self, &idx))
711 vec_remove(self->params[i]->writes, idx, 1);
712 if (vec_ir_instr_find(self->params[i]->reads, self, &idx))
713 vec_remove(self->params[i]->reads, idx, 1);
715 vec_free(self->params);
716 (void)!ir_instr_op(self, 0, NULL, false);
717 (void)!ir_instr_op(self, 1, NULL, false);
718 (void)!ir_instr_op(self, 2, NULL, false);
722 bool ir_instr_op(ir_instr *self, int op, ir_value *v, bool writing)
724 if (self->_ops[op]) {
726 if (writing && vec_ir_instr_find(self->_ops[op]->writes, self, &idx))
727 vec_remove(self->_ops[op]->writes, idx, 1);
728 else if (vec_ir_instr_find(self->_ops[op]->reads, self, &idx))
729 vec_remove(self->_ops[op]->reads, idx, 1);
733 vec_push(v->writes, self);
735 vec_push(v->reads, self);
741 /***********************************************************************
745 void ir_value_code_setaddr(ir_value *self, int32_t gaddr)
747 self->code.globaladdr = gaddr;
748 if (self->members[0]) self->members[0]->code.globaladdr = gaddr;
749 if (self->members[1]) self->members[1]->code.globaladdr = gaddr;
750 if (self->members[2]) self->members[2]->code.globaladdr = gaddr;
753 int32_t ir_value_code_addr(const ir_value *self)
755 if (self->store == store_return)
756 return OFS_RETURN + self->code.addroffset;
757 return self->code.globaladdr + self->code.addroffset;
760 ir_value* ir_value_var(const char *name, int storetype, int vtype)
763 self = (ir_value*)mem_a(sizeof(*self));
765 self->fieldtype = TYPE_VOID;
766 self->outtype = TYPE_VOID;
767 self->store = storetype;
772 self->isconst = false;
773 self->context.file = "<@no context>";
774 self->context.line = 0;
776 if (name && !ir_value_set_name(self, name)) {
777 irerror(self->context, "out of memory");
782 memset(&self->constval, 0, sizeof(self->constval));
783 memset(&self->code, 0, sizeof(self->code));
785 self->members[0] = NULL;
786 self->members[1] = NULL;
787 self->members[2] = NULL;
788 self->memberof = NULL;
794 ir_value* ir_value_vector_member(ir_value *self, unsigned int member)
800 if (self->members[member])
801 return self->members[member];
803 if (self->vtype == TYPE_VECTOR)
805 m = ir_value_var(self->name, self->store, TYPE_FLOAT);
808 m->context = self->context;
810 self->members[member] = m;
811 m->code.addroffset = member;
813 else if (self->vtype == TYPE_FIELD)
815 if (self->fieldtype != TYPE_VECTOR)
817 m = ir_value_var(self->name, self->store, TYPE_FIELD);
820 m->fieldtype = TYPE_FLOAT;
821 m->context = self->context;
823 self->members[member] = m;
824 m->code.addroffset = member;
828 irerror(self->context, "invalid member access on %s", self->name);
836 ir_value* ir_value_out(ir_function *owner, const char *name, int storetype, int vtype)
838 ir_value *v = ir_value_var(name, storetype, vtype);
841 ir_function_collect_value(owner, v);
845 void ir_value_delete(ir_value* self)
849 mem_d((void*)self->name);
852 if (self->vtype == TYPE_STRING)
853 mem_d((void*)self->constval.vstring);
855 for (i = 0; i < 3; ++i) {
856 if (self->members[i])
857 ir_value_delete(self->members[i]);
859 vec_free(self->reads);
860 vec_free(self->writes);
861 vec_free(self->life);
865 bool ir_value_set_name(ir_value *self, const char *name)
868 mem_d((void*)self->name);
869 self->name = util_strdup(name);
873 bool ir_value_set_float(ir_value *self, float f)
875 if (self->vtype != TYPE_FLOAT)
877 self->constval.vfloat = f;
878 self->isconst = true;
882 bool ir_value_set_func(ir_value *self, int f)
884 if (self->vtype != TYPE_FUNCTION)
886 self->constval.vint = f;
887 self->isconst = true;
891 bool ir_value_set_vector(ir_value *self, vector v)
893 if (self->vtype != TYPE_VECTOR)
895 self->constval.vvec = v;
896 self->isconst = true;
900 bool ir_value_set_field(ir_value *self, ir_value *fld)
902 if (self->vtype != TYPE_FIELD)
904 self->constval.vpointer = fld;
905 self->isconst = true;
909 static char *ir_strdup(const char *str)
912 /* actually dup empty strings */
913 char *out = mem_a(1);
917 return util_strdup(str);
920 bool ir_value_set_string(ir_value *self, const char *str)
922 if (self->vtype != TYPE_STRING)
924 self->constval.vstring = ir_strdup(str);
925 self->isconst = true;
930 bool ir_value_set_int(ir_value *self, int i)
932 if (self->vtype != TYPE_INTEGER)
934 self->constval.vint = i;
935 self->isconst = true;
940 bool ir_value_lives(ir_value *self, size_t at)
943 for (i = 0; i < vec_size(self->life); ++i)
945 ir_life_entry_t *life = &self->life[i];
946 if (life->start <= at && at <= life->end)
948 if (life->start > at) /* since it's ordered */
954 bool ir_value_life_insert(ir_value *self, size_t idx, ir_life_entry_t e)
957 vec_push(self->life, e);
958 for (k = vec_size(self->life)-1; k > idx; --k)
959 self->life[k] = self->life[k-1];
964 bool ir_value_life_merge(ir_value *self, size_t s)
967 ir_life_entry_t *life = NULL;
968 ir_life_entry_t *before = NULL;
969 ir_life_entry_t new_entry;
971 /* Find the first range >= s */
972 for (i = 0; i < vec_size(self->life); ++i)
975 life = &self->life[i];
979 /* nothing found? append */
980 if (i == vec_size(self->life)) {
982 if (life && life->end+1 == s)
984 /* previous life range can be merged in */
988 if (life && life->end >= s)
991 vec_push(self->life, e);
997 if (before->end + 1 == s &&
998 life->start - 1 == s)
1001 before->end = life->end;
1002 vec_remove(self->life, i, 1);
1005 if (before->end + 1 == s)
1011 /* already contained */
1012 if (before->end >= s)
1016 if (life->start - 1 == s)
1021 /* insert a new entry */
1022 new_entry.start = new_entry.end = s;
1023 return ir_value_life_insert(self, i, new_entry);
1026 bool ir_value_life_merge_into(ir_value *self, const ir_value *other)
1030 if (!vec_size(other->life))
1033 if (!vec_size(self->life)) {
1034 size_t count = vec_size(other->life);
1035 ir_life_entry_t *life = vec_add(self->life, count);
1036 memcpy(life, other->life, count * sizeof(*life));
1041 for (i = 0; i < vec_size(other->life); ++i)
1043 const ir_life_entry_t *life = &other->life[i];
1046 ir_life_entry_t *entry = &self->life[myi];
1048 if (life->end+1 < entry->start)
1050 /* adding an interval before entry */
1051 if (!ir_value_life_insert(self, myi, *life))
1057 if (life->start < entry->start &&
1058 life->end+1 >= entry->start)
1060 /* starts earlier and overlaps */
1061 entry->start = life->start;
1064 if (life->end > entry->end &&
1065 life->start <= entry->end+1)
1067 /* ends later and overlaps */
1068 entry->end = life->end;
1071 /* see if our change combines it with the next ranges */
1072 while (myi+1 < vec_size(self->life) &&
1073 entry->end+1 >= self->life[1+myi].start)
1075 /* overlaps with (myi+1) */
1076 if (entry->end < self->life[1+myi].end)
1077 entry->end = self->life[1+myi].end;
1078 vec_remove(self->life, myi+1, 1);
1079 entry = &self->life[myi];
1082 /* see if we're after the entry */
1083 if (life->start > entry->end)
1086 /* append if we're at the end */
1087 if (myi >= vec_size(self->life)) {
1088 vec_push(self->life, *life);
1091 /* otherweise check the next range */
1100 bool ir_values_overlap(const ir_value *a, const ir_value *b)
1102 /* For any life entry in A see if it overlaps with
1103 * any life entry in B.
1104 * Note that the life entries are orderes, so we can make a
1105 * more efficient algorithm there than naively translating the
1109 ir_life_entry_t *la, *lb, *enda, *endb;
1111 /* first of all, if either has no life range, they cannot clash */
1112 if (!vec_size(a->life) || !vec_size(b->life))
1117 enda = la + vec_size(a->life);
1118 endb = lb + vec_size(b->life);
1121 /* check if the entries overlap, for that,
1122 * both must start before the other one ends.
1124 if (la->start < lb->end &&
1125 lb->start < la->end)
1130 /* entries are ordered
1131 * one entry is earlier than the other
1132 * that earlier entry will be moved forward
1134 if (la->start < lb->start)
1136 /* order: A B, move A forward
1137 * check if we hit the end with A
1142 else /* if (lb->start < la->start) actually <= */
1144 /* order: B A, move B forward
1145 * check if we hit the end with B
1154 /***********************************************************************
1158 bool ir_block_create_store_op(ir_block *self, int op, ir_value *target, ir_value *what)
1162 irerror(self->context, "unreachable statement (%s)", self->label);
1165 in = ir_instr_new(self, op);
1169 if (target->store == store_value &&
1170 (op < INSTR_STOREP_F || op > INSTR_STOREP_FNC))
1172 irerror(self->context, "cannot store to an SSA value");
1173 irerror(self->context, "trying to store: %s <- %s", target->name, what->name);
1174 irerror(self->context, "instruction: %s", asm_instr[op].m);
1178 if (!ir_instr_op(in, 0, target, true) ||
1179 !ir_instr_op(in, 1, what, false))
1183 vec_push(self->instr, in);
1187 bool ir_block_create_store(ir_block *self, ir_value *target, ir_value *what)
1191 if (target->vtype == TYPE_VARIANT)
1192 vtype = what->vtype;
1194 vtype = target->vtype;
1197 if (vtype == TYPE_FLOAT && what->vtype == TYPE_INTEGER)
1198 op = INSTR_CONV_ITOF;
1199 else if (vtype == TYPE_INTEGER && what->vtype == TYPE_FLOAT)
1200 op = INSTR_CONV_FTOI;
1202 op = type_store_instr[vtype];
1204 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS)) {
1205 if (op == INSTR_STORE_FLD && what->fieldtype == TYPE_VECTOR)
1209 return ir_block_create_store_op(self, op, target, what);
1212 bool ir_block_create_storep(ir_block *self, ir_value *target, ir_value *what)
1217 if (target->vtype != TYPE_POINTER)
1220 /* storing using pointer - target is a pointer, type must be
1221 * inferred from source
1223 vtype = what->vtype;
1225 op = type_storep_instr[vtype];
1226 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS)) {
1227 if (op == INSTR_STOREP_FLD && what->fieldtype == TYPE_VECTOR)
1228 op = INSTR_STOREP_V;
1231 return ir_block_create_store_op(self, op, target, what);
1234 bool ir_block_create_return(ir_block *self, ir_value *v)
1238 irerror(self->context, "unreachable statement (%s)", self->label);
1242 self->is_return = true;
1243 in = ir_instr_new(self, INSTR_RETURN);
1247 if (v && !ir_instr_op(in, 0, v, false))
1250 vec_push(self->instr, in);
1254 bool ir_block_create_if(ir_block *self, ir_value *v,
1255 ir_block *ontrue, ir_block *onfalse)
1259 irerror(self->context, "unreachable statement (%s)", self->label);
1263 /*in = ir_instr_new(self, (v->vtype == TYPE_STRING ? INSTR_IF_S : INSTR_IF_F));*/
1264 in = ir_instr_new(self, VINSTR_COND);
1268 if (!ir_instr_op(in, 0, v, false)) {
1269 ir_instr_delete(in);
1273 in->bops[0] = ontrue;
1274 in->bops[1] = onfalse;
1276 vec_push(self->instr, in);
1278 vec_push(self->exits, ontrue);
1279 vec_push(self->exits, onfalse);
1280 vec_push(ontrue->entries, self);
1281 vec_push(onfalse->entries, self);
1285 bool ir_block_create_jump(ir_block *self, ir_block *to)
1289 irerror(self->context, "unreachable statement (%s)", self->label);
1293 in = ir_instr_new(self, VINSTR_JUMP);
1298 vec_push(self->instr, in);
1300 vec_push(self->exits, to);
1301 vec_push(to->entries, self);
1305 bool ir_block_create_goto(ir_block *self, ir_block *to)
1309 irerror(self->context, "unreachable statement (%s)", self->label);
1313 in = ir_instr_new(self, INSTR_GOTO);
1318 vec_push(self->instr, in);
1320 vec_push(self->exits, to);
1321 vec_push(to->entries, self);
1325 ir_instr* ir_block_create_phi(ir_block *self, const char *label, int ot)
1329 in = ir_instr_new(self, VINSTR_PHI);
1332 out = ir_value_out(self->owner, label, store_value, ot);
1334 ir_instr_delete(in);
1337 if (!ir_instr_op(in, 0, out, true)) {
1338 ir_instr_delete(in);
1339 ir_value_delete(out);
1342 vec_push(self->instr, in);
1346 ir_value* ir_phi_value(ir_instr *self)
1348 return self->_ops[0];
1351 void ir_phi_add(ir_instr* self, ir_block *b, ir_value *v)
1355 if (!vec_ir_block_find(self->owner->entries, b, NULL)) {
1356 /* Must not be possible to cause this, otherwise the AST
1357 * is doing something wrong.
1359 irerror(self->context, "Invalid entry block for PHI");
1365 vec_push(v->reads, self);
1366 vec_push(self->phi, pe);
1369 /* call related code */
1370 ir_instr* ir_block_create_call(ir_block *self, const char *label, ir_value *func)
1374 in = ir_instr_new(self, INSTR_CALL0);
1377 out = ir_value_out(self->owner, label, (func->outtype == TYPE_VOID) ? store_return : store_value, func->outtype);
1379 ir_instr_delete(in);
1382 if (!ir_instr_op(in, 0, out, true) ||
1383 !ir_instr_op(in, 1, func, false))
1385 ir_instr_delete(in);
1386 ir_value_delete(out);
1389 vec_push(self->instr, in);
1393 ir_value* ir_call_value(ir_instr *self)
1395 return self->_ops[0];
1398 void ir_call_param(ir_instr* self, ir_value *v)
1400 vec_push(self->params, v);
1401 vec_push(v->reads, self);
1404 /* binary op related code */
1406 ir_value* ir_block_create_binop(ir_block *self,
1407 const char *label, int opcode,
1408 ir_value *left, ir_value *right)
1430 case INSTR_SUB_S: /* -- offset of string as float */
1435 case INSTR_BITOR_IF:
1436 case INSTR_BITOR_FI:
1437 case INSTR_BITAND_FI:
1438 case INSTR_BITAND_IF:
1453 case INSTR_BITAND_I:
1456 case INSTR_RSHIFT_I:
1457 case INSTR_LSHIFT_I:
1479 /* boolean operations result in floats */
1480 if (opcode >= INSTR_EQ_F && opcode <= INSTR_GT)
1482 else if (opcode >= INSTR_LE && opcode <= INSTR_GT)
1485 else if (opcode >= INSTR_LE_I && opcode <= INSTR_EQ_FI)
1490 if (ot == TYPE_VOID) {
1491 /* The AST or parser were supposed to check this! */
1495 return ir_block_create_general_instr(self, label, opcode, left, right, ot);
1498 ir_value* ir_block_create_unary(ir_block *self,
1499 const char *label, int opcode,
1502 int ot = TYPE_FLOAT;
1514 /* QC doesn't have other unary operations. We expect extensions to fill
1515 * the above list, otherwise we assume out-type = in-type, eg for an
1519 ot = operand->vtype;
1522 if (ot == TYPE_VOID) {
1523 /* The AST or parser were supposed to check this! */
1527 /* let's use the general instruction creator and pass NULL for OPB */
1528 return ir_block_create_general_instr(self, label, opcode, operand, NULL, ot);
1531 ir_value* ir_block_create_general_instr(ir_block *self, const char *label,
1532 int op, ir_value *a, ir_value *b, int outype)
1537 out = ir_value_out(self->owner, label, store_value, outype);
1541 instr = ir_instr_new(self, op);
1543 ir_value_delete(out);
1547 if (!ir_instr_op(instr, 0, out, true) ||
1548 !ir_instr_op(instr, 1, a, false) ||
1549 !ir_instr_op(instr, 2, b, false) )
1554 vec_push(self->instr, instr);
1558 ir_instr_delete(instr);
1559 ir_value_delete(out);
1563 ir_value* ir_block_create_fieldaddress(ir_block *self, const char *label, ir_value *ent, ir_value *field)
1567 /* Support for various pointer types todo if so desired */
1568 if (ent->vtype != TYPE_ENTITY)
1571 if (field->vtype != TYPE_FIELD)
1574 v = ir_block_create_general_instr(self, label, INSTR_ADDRESS, ent, field, TYPE_POINTER);
1575 v->fieldtype = field->fieldtype;
1579 ir_value* ir_block_create_load_from_ent(ir_block *self, const char *label, ir_value *ent, ir_value *field, int outype)
1582 if (ent->vtype != TYPE_ENTITY)
1585 /* at some point we could redirect for TYPE_POINTER... but that could lead to carelessness */
1586 if (field->vtype != TYPE_FIELD)
1591 case TYPE_FLOAT: op = INSTR_LOAD_F; break;
1592 case TYPE_VECTOR: op = INSTR_LOAD_V; break;
1593 case TYPE_STRING: op = INSTR_LOAD_S; break;
1594 case TYPE_FIELD: op = INSTR_LOAD_FLD; break;
1595 case TYPE_ENTITY: op = INSTR_LOAD_ENT; break;
1596 case TYPE_FUNCTION: op = INSTR_LOAD_FNC; break;
1598 case TYPE_POINTER: op = INSTR_LOAD_I; break;
1599 case TYPE_INTEGER: op = INSTR_LOAD_I; break;
1602 irerror(self->context, "invalid type for ir_block_create_load_from_ent: %s", type_name[outype]);
1606 return ir_block_create_general_instr(self, label, op, ent, field, outype);
1609 ir_value* ir_block_create_add(ir_block *self,
1611 ir_value *left, ir_value *right)
1614 int l = left->vtype;
1615 int r = right->vtype;
1619 irerror(self->context, "invalid type for ir_block_create_add: %s", type_name[l]);
1635 if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1637 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1642 irerror(self->context, "invalid type for ir_block_create_add: %s", type_name[l]);
1646 return ir_block_create_binop(self, label, op, left, right);
1649 ir_value* ir_block_create_sub(ir_block *self,
1651 ir_value *left, ir_value *right)
1654 int l = left->vtype;
1655 int r = right->vtype;
1660 irerror(self->context, "invalid type for ir_block_create_sub: %s", type_name[l]);
1676 if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1678 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1683 irerror(self->context, "invalid type for ir_block_create_sub: %s", type_name[l]);
1687 return ir_block_create_binop(self, label, op, left, right);
1690 ir_value* ir_block_create_mul(ir_block *self,
1692 ir_value *left, ir_value *right)
1695 int l = left->vtype;
1696 int r = right->vtype;
1701 irerror(self->context, "invalid type for ir_block_create_mul: %s", type_name[l]);
1716 if ( (l == TYPE_VECTOR && r == TYPE_FLOAT) )
1718 else if ( (l == TYPE_FLOAT && r == TYPE_VECTOR) )
1721 else if ( (l == TYPE_VECTOR && r == TYPE_INTEGER) )
1723 else if ( (l == TYPE_INTEGER && r == TYPE_VECTOR) )
1725 else if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1727 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1731 irerror(self->context, "invalid type for ir_block_create_mul: %s", type_name[l]);
1735 return ir_block_create_binop(self, label, op, left, right);
1738 ir_value* ir_block_create_div(ir_block *self,
1740 ir_value *left, ir_value *right)
1743 int l = left->vtype;
1744 int r = right->vtype;
1749 irerror(self->context, "invalid type for ir_block_create_div: %s", type_name[l]);
1762 if ( (l == TYPE_VECTOR && r == TYPE_FLOAT) )
1764 else if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1766 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1771 irerror(self->context, "invalid type for ir_block_create_div: %s", type_name[l]);
1775 return ir_block_create_binop(self, label, op, left, right);
1778 /* PHI resolving breaks the SSA, and must thus be the last
1779 * step before life-range calculation.
1782 static bool ir_block_naive_phi(ir_block *self);
1783 bool ir_function_naive_phi(ir_function *self)
1787 for (i = 0; i < vec_size(self->blocks); ++i)
1789 if (!ir_block_naive_phi(self->blocks[i]))
1796 static bool ir_naive_phi_emit_store(ir_block *block, size_t iid, ir_value *old, ir_value *what)
1801 /* create a store */
1802 if (!ir_block_create_store(block, old, what))
1805 /* we now move it up */
1806 instr = vec_last(block->instr);
1807 for (i = vec_size(block->instr)-1; i > iid; --i)
1808 block->instr[i] = block->instr[i-1];
1809 block->instr[i] = instr;
1815 static bool ir_block_naive_phi(ir_block *self)
1817 size_t i, p; /*, w;*/
1818 /* FIXME: optionally, create_phi can add the phis
1819 * to a list so we don't need to loop through blocks
1820 * - anyway: "don't optimize YET"
1822 for (i = 0; i < vec_size(self->instr); ++i)
1824 ir_instr *instr = self->instr[i];
1825 if (instr->opcode != VINSTR_PHI)
1828 vec_remove(self->instr, i, 1);
1829 --i; /* NOTE: i+1 below */
1831 for (p = 0; p < vec_size(instr->phi); ++p)
1833 ir_value *v = instr->phi[p].value;
1834 ir_block *b = instr->phi[p].from;
1836 if (v->store == store_value &&
1837 vec_size(v->reads) == 1 &&
1838 vec_size(v->writes) == 1)
1840 /* replace the value */
1841 if (!ir_instr_op(v->writes[0], 0, instr->_ops[0], true))
1846 /* force a move instruction */
1847 ir_instr *prevjump = vec_last(b->instr);
1850 instr->_ops[0]->store = store_global;
1851 if (!ir_block_create_store(b, instr->_ops[0], v))
1853 instr->_ops[0]->store = store_value;
1854 vec_push(b->instr, prevjump);
1859 ir_value *v = instr->phi[p].value;
1860 for (w = 0; w < vec_size(v->writes); ++w) {
1863 if (!v->writes[w]->_ops[0])
1866 /* When the write was to a global, we have to emit a mov */
1867 old = v->writes[w]->_ops[0];
1869 /* The original instruction now writes to the PHI target local */
1870 if (v->writes[w]->_ops[0] == v)
1871 v->writes[w]->_ops[0] = instr->_ops[0];
1873 if (old->store != store_value && old->store != store_local && old->store != store_param)
1875 /* If it originally wrote to a global we need to store the value
1878 if (!ir_naive_phi_emit_store(self, i+1, old, v))
1880 if (i+1 < vec_size(self->instr))
1881 instr = self->instr[i+1];
1884 /* In case I forget and access instr later, it'll be NULL
1885 * when it's a problem, to make sure we crash, rather than accessing
1891 /* If it didn't, we can replace all reads by the phi target now. */
1893 for (r = 0; r < vec_size(old->reads); ++r)
1896 ir_instr *ri = old->reads[r];
1897 for (op = 0; op < vec_size(ri->phi); ++op) {
1898 if (ri->phi[op].value == old)
1899 ri->phi[op].value = v;
1901 for (op = 0; op < 3; ++op) {
1902 if (ri->_ops[op] == old)
1910 ir_instr_delete(instr);
1915 /***********************************************************************
1916 *IR Temp allocation code
1917 * Propagating value life ranges by walking through the function backwards
1918 * until no more changes are made.
1919 * In theory this should happen once more than once for every nested loop
1921 * Though this implementation might run an additional time for if nests.
1924 /* Enumerate instructions used by value's life-ranges
1926 static void ir_block_enumerate(ir_block *self, size_t *_eid)
1930 for (i = 0; i < vec_size(self->instr); ++i)
1932 self->instr[i]->eid = eid++;
1937 /* Enumerate blocks and instructions.
1938 * The block-enumeration is unordered!
1939 * We do not really use the block enumreation, however
1940 * the instruction enumeration is important for life-ranges.
1942 void ir_function_enumerate(ir_function *self)
1945 size_t instruction_id = 0;
1946 for (i = 0; i < vec_size(self->blocks); ++i)
1948 self->blocks[i]->eid = i;
1949 self->blocks[i]->run_id = 0;
1950 ir_block_enumerate(self->blocks[i], &instruction_id);
1954 static bool ir_block_life_propagate(ir_block *b, ir_block *prev, bool *changed);
1955 bool ir_function_calculate_liferanges(ir_function *self)
1963 for (i = 0; i != vec_size(self->blocks); ++i)
1965 if (self->blocks[i]->is_return)
1967 vec_free(self->blocks[i]->living);
1968 if (!ir_block_life_propagate(self->blocks[i], NULL, &changed))
1973 if (vec_size(self->blocks)) {
1974 ir_block *block = self->blocks[0];
1975 for (i = 0; i < vec_size(block->living); ++i) {
1976 ir_value *v = block->living[i];
1977 if (v->memberof || v->store != store_local)
1979 if (irwarning(v->context, WARN_USED_UNINITIALIZED,
1980 "variable `%s` may be used uninitialized in this function", v->name))
1989 /* Local-value allocator
1990 * After finishing creating the liferange of all values used in a function
1991 * we can allocate their global-positions.
1992 * This is the counterpart to register-allocation in register machines.
1998 } function_allocator;
2000 static bool function_allocator_alloc(function_allocator *alloc, const ir_value *var)
2003 size_t vsize = type_sizeof[var->vtype];
2005 slot = ir_value_var("reg", store_global, var->vtype);
2009 if (!ir_value_life_merge_into(slot, var))
2012 vec_push(alloc->locals, slot);
2013 vec_push(alloc->sizes, vsize);
2018 ir_value_delete(slot);
2022 bool ir_function_allocate_locals(ir_function *self)
2031 function_allocator alloc;
2033 if (!vec_size(self->locals) && !vec_size(self->values))
2036 alloc.locals = NULL;
2038 alloc.positions = NULL;
2040 for (i = 0; i < vec_size(self->locals); ++i)
2042 if (!function_allocator_alloc(&alloc, self->locals[i]))
2046 /* Allocate a slot for any value that still exists */
2047 for (i = 0; i < vec_size(self->values); ++i)
2049 v = self->values[i];
2051 if (!vec_size(v->life))
2054 for (a = 0; a < vec_size(alloc.locals); ++a)
2056 slot = alloc.locals[a];
2058 if (ir_values_overlap(v, slot))
2061 if (!ir_value_life_merge_into(slot, v))
2064 /* adjust size for this slot */
2065 if (alloc.sizes[a] < type_sizeof[v->vtype])
2066 alloc.sizes[a] = type_sizeof[v->vtype];
2068 self->values[i]->code.local = a;
2071 if (a >= vec_size(alloc.locals)) {
2072 self->values[i]->code.local = vec_size(alloc.locals);
2073 if (!function_allocator_alloc(&alloc, v))
2082 /* Adjust slot positions based on sizes */
2083 vec_push(alloc.positions, 0);
2085 if (vec_size(alloc.sizes))
2086 pos = alloc.positions[0] + alloc.sizes[0];
2089 for (i = 1; i < vec_size(alloc.sizes); ++i)
2091 pos = alloc.positions[i-1] + alloc.sizes[i-1];
2092 vec_push(alloc.positions, pos);
2095 self->allocated_locals = pos + vec_last(alloc.sizes);
2097 /* Take over the actual slot positions */
2098 for (i = 0; i < vec_size(self->values); ++i) {
2099 self->values[i]->code.local = alloc.positions[self->values[i]->code.local];
2107 for (i = 0; i < vec_size(alloc.locals); ++i)
2108 ir_value_delete(alloc.locals[i]);
2109 vec_free(alloc.locals);
2110 vec_free(alloc.sizes);
2111 vec_free(alloc.positions);
2115 /* Get information about which operand
2116 * is read from, or written to.
2118 static void ir_op_read_write(int op, size_t *read, size_t *write)
2138 case INSTR_STOREP_F:
2139 case INSTR_STOREP_V:
2140 case INSTR_STOREP_S:
2141 case INSTR_STOREP_ENT:
2142 case INSTR_STOREP_FLD:
2143 case INSTR_STOREP_FNC:
2154 static bool ir_block_living_add_instr(ir_block *self, size_t eid)
2157 bool changed = false;
2159 for (i = 0; i != vec_size(self->living); ++i)
2161 tempbool = ir_value_life_merge(self->living[i], eid);
2164 irerror(self->context, "block_living_add_instr() value instruction added %s: %i", self->living[i]->_name, (int)eid);
2166 changed = changed || tempbool;
2171 static bool ir_block_life_prop_previous(ir_block* self, ir_block *prev, bool *changed)
2174 /* values which have been read in a previous iteration are now
2175 * in the "living" array even if the previous block doesn't use them.
2176 * So we have to remove whatever does not exist in the previous block.
2177 * They will be re-added on-read, but the liferange merge won't cause
2180 for (i = 0; i < vec_size(self->living); ++i)
2182 if (!vec_ir_value_find(prev->living, self->living[i], NULL)) {
2183 vec_remove(self->living, i, 1);
2188 /* Whatever the previous block still has in its living set
2189 * must now be added to ours as well.
2191 for (i = 0; i < vec_size(prev->living); ++i)
2193 if (vec_ir_value_find(self->living, prev->living[i], NULL))
2195 vec_push(self->living, prev->living[i]);
2197 irerror(self->contextt from prev: %s", self->label, prev->living[i]->_name);
2203 static bool ir_block_life_propagate(ir_block *self, ir_block *prev, bool *changed)
2209 /* bitmasks which operands are read from or written to */
2211 char dbg_ind[16] = { '#', '0' };
2216 if (!ir_block_life_prop_previous(self, prev, changed))
2220 i = vec_size(self->instr);
2223 instr = self->instr[i];
2225 /* PHI operands are always read operands */
2226 for (p = 0; p < vec_size(instr->phi); ++p)
2228 value = instr->phi[p].value;
2229 if (value->memberof)
2230 value = value->memberof;
2231 if (!vec_ir_value_find(self->living, value, NULL))
2232 vec_push(self->living, value);
2235 /* call params are read operands too */
2236 for (p = 0; p < vec_size(instr->params); ++p)
2238 value = instr->params[p];
2239 if (value->memberof)
2240 value = value->memberof;
2241 if (!vec_ir_value_find(self->living, value, NULL))
2242 vec_push(self->living, value);
2245 /* See which operands are read and write operands */
2246 ir_op_read_write(instr->opcode, &read, &write);
2248 if (instr->opcode == INSTR_MUL_VF)
2250 /* the float source will get an additional lifetime */
2251 tempbool = ir_value_life_merge(instr->_ops[2], instr->eid+1);
2252 *changed = *changed || tempbool;
2254 else if (instr->opcode == INSTR_MUL_FV)
2256 /* the float source will get an additional lifetime */
2257 tempbool = ir_value_life_merge(instr->_ops[1], instr->eid+1);
2258 *changed = *changed || tempbool;
2261 /* Go through the 3 main operands */
2262 for (o = 0; o < 3; ++o)
2264 if (!instr->_ops[o]) /* no such operand */
2267 value = instr->_ops[o];
2268 if (value->memberof)
2269 value = value->memberof;
2271 /* We only care about locals */
2272 /* we also calculate parameter liferanges so that locals
2273 * can take up parameter slots */
2274 if (value->store != store_value &&
2275 value->store != store_local &&
2276 value->store != store_param)
2282 if (!vec_ir_value_find(self->living, value, NULL))
2283 vec_push(self->living, value);
2286 /* write operands */
2287 /* When we write to a local, we consider it "dead" for the
2288 * remaining upper part of the function, since in SSA a value
2289 * can only be written once (== created)
2294 bool in_living = vec_ir_value_find(self->living, value, &idx);
2297 /* If the value isn't alive it hasn't been read before... */
2298 /* TODO: See if the warning can be emitted during parsing or AST processing
2299 * otherwise have warning printed here.
2300 * IF printing a warning here: include filecontext_t,
2301 * and make sure it's only printed once
2302 * since this function is run multiple times.
2304 /* For now: debug info: */
2305 /* con_err( "Value only written %s\n", value->name); */
2306 tempbool = ir_value_life_merge(value, instr->eid);
2307 *changed = *changed || tempbool;
2309 ir_instr_dump(instr, dbg_ind, printf);
2313 /* since 'living' won't contain it
2314 * anymore, merge the value, since
2317 tempbool = ir_value_life_merge(value, instr->eid);
2320 con_err( "value added id %s %i\n", value->name, (int)instr->eid);
2322 *changed = *changed || tempbool;
2324 vec_remove(self->living, idx, 1);
2329 tempbool = ir_block_living_add_instr(self, instr->eid);
2330 /*con_err( "living added values\n");*/
2331 *changed = *changed || tempbool;
2335 if (self->run_id == self->owner->run_id)
2338 self->run_id = self->owner->run_id;
2340 for (i = 0; i < vec_size(self->entries); ++i)
2342 ir_block *entry = self->entries[i];
2343 ir_block_life_propagate(entry, self, changed);
2349 /***********************************************************************
2352 * Since the IR has the convention of putting 'write' operands
2353 * at the beginning, we have to rotate the operands of instructions
2354 * properly in order to generate valid QCVM code.
2356 * Having destinations at a fixed position is more convenient. In QC
2357 * this is *mostly* OPC, but FTE adds at least 2 instructions which
2358 * read from from OPA, and store to OPB rather than OPC. Which is
2359 * partially the reason why the implementation of these instructions
2360 * in darkplaces has been delayed for so long.
2362 * Breaking conventions is annoying...
2364 static bool ir_builder_gen_global(ir_builder *self, ir_value *global, bool islocal);
2366 static bool gen_global_field(ir_value *global)
2368 if (global->isconst)
2370 ir_value *fld = global->constval.vpointer;
2372 irerror(global->context, "Invalid field constant with no field: %s", global->name);
2376 /* Now, in this case, a relocation would be impossible to code
2377 * since it looks like this:
2378 * .vector v = origin; <- parse error, wtf is 'origin'?
2381 * But we will need a general relocation support later anyway
2382 * for functions... might as well support that here.
2384 if (!fld->code.globaladdr) {
2385 irerror(global->context, "FIXME: Relocation support");
2389 /* copy the field's value */
2390 ir_value_code_setaddr(global, vec_size(code_globals));
2391 vec_push(code_globals, code_globals[fld->code.globaladdr]);
2392 if (global->fieldtype == TYPE_VECTOR) {
2393 vec_push(code_globals, code_globals[fld->code.globaladdr]+1);
2394 vec_push(code_globals, code_globals[fld->code.globaladdr]+2);
2399 ir_value_code_setaddr(global, vec_size(code_globals));
2400 vec_push(code_globals, 0);
2401 if (global->fieldtype == TYPE_VECTOR) {
2402 vec_push(code_globals, 0);
2403 vec_push(code_globals, 0);
2406 if (global->code.globaladdr < 0)
2411 static bool gen_global_pointer(ir_value *global)
2413 if (global->isconst)
2415 ir_value *target = global->constval.vpointer;
2417 irerror(global->context, "Invalid pointer constant: %s", global->name);
2418 /* NULL pointers are pointing to the NULL constant, which also
2419 * sits at address 0, but still has an ir_value for itself.
2424 /* Here, relocations ARE possible - in fteqcc-enhanced-qc:
2425 * void() foo; <- proto
2426 * void() *fooptr = &foo;
2427 * void() foo = { code }
2429 if (!target->code.globaladdr) {
2430 /* FIXME: Check for the constant nullptr ir_value!
2431 * because then code.globaladdr being 0 is valid.
2433 irerror(global->context, "FIXME: Relocation support");
2437 ir_value_code_setaddr(global, vec_size(code_globals));
2438 vec_push(code_globals, target->code.globaladdr);
2442 ir_value_code_setaddr(global, vec_size(code_globals));
2443 vec_push(code_globals, 0);
2445 if (global->code.globaladdr < 0)
2450 static bool gen_blocks_recursive(ir_function *func, ir_block *block)
2452 prog_section_statement stmt;
2461 block->generated = true;
2462 block->code_start = vec_size(code_statements);
2463 for (i = 0; i < vec_size(block->instr); ++i)
2465 instr = block->instr[i];
2467 if (instr->opcode == VINSTR_PHI) {
2468 irerror(block->context, "cannot generate virtual instruction (phi)");
2472 if (instr->opcode == VINSTR_JUMP) {
2473 target = instr->bops[0];
2474 /* for uncoditional jumps, if the target hasn't been generated
2475 * yet, we generate them right here.
2477 if (!target->generated) {
2482 /* otherwise we generate a jump instruction */
2483 stmt.opcode = INSTR_GOTO;
2484 stmt.o1.s1 = (target->code_start) - vec_size(code_statements);
2487 vec_push(code_statements, stmt);
2489 /* no further instructions can be in this block */
2493 if (instr->opcode == VINSTR_COND) {
2494 ontrue = instr->bops[0];
2495 onfalse = instr->bops[1];
2496 /* TODO: have the AST signal which block should
2497 * come first: eg. optimize IFs without ELSE...
2500 stmt.o1.u1 = ir_value_code_addr(instr->_ops[0]);
2504 if (ontrue->generated) {
2505 stmt.opcode = INSTR_IF;
2506 stmt.o2.s1 = (ontrue->code_start) - vec_size(code_statements);
2507 vec_push(code_statements, stmt);
2509 if (onfalse->generated) {
2510 stmt.opcode = INSTR_IFNOT;
2511 stmt.o2.s1 = (onfalse->code_start) - vec_size(code_statements);
2512 vec_push(code_statements, stmt);
2514 if (!ontrue->generated) {
2515 if (onfalse->generated) {
2520 if (!onfalse->generated) {
2521 if (ontrue->generated) {
2526 /* neither ontrue nor onfalse exist */
2527 stmt.opcode = INSTR_IFNOT;
2528 if (!instr->likely) {
2529 /* Honor the likelyhood hint */
2530 ir_block *tmp = onfalse;
2531 stmt.opcode = INSTR_IF;
2535 stidx = vec_size(code_statements);
2536 vec_push(code_statements, stmt);
2537 /* on false we jump, so add ontrue-path */
2538 if (!gen_blocks_recursive(func, ontrue))
2540 /* fixup the jump address */
2541 code_statements[stidx].o2.s1 = vec_size(code_statements) - stidx;
2542 /* generate onfalse path */
2543 if (onfalse->generated) {
2544 /* fixup the jump address */
2545 code_statements[stidx].o2.s1 = (onfalse->code_start) - (stidx);
2546 stmt.opcode = vec_last(code_statements).opcode;
2547 if (stmt.opcode == INSTR_GOTO ||
2548 stmt.opcode == INSTR_IF ||
2549 stmt.opcode == INSTR_IFNOT ||
2550 stmt.opcode == INSTR_RETURN ||
2551 stmt.opcode == INSTR_DONE)
2553 /* no use jumping from here */
2556 /* may have been generated in the previous recursive call */
2557 stmt.opcode = INSTR_GOTO;
2558 stmt.o1.s1 = (onfalse->code_start) - vec_size(code_statements);
2561 vec_push(code_statements, stmt);
2564 /* if not, generate now */
2569 if (instr->opcode >= INSTR_CALL0 && instr->opcode <= INSTR_CALL8) {
2570 /* Trivial call translation:
2571 * copy all params to OFS_PARM*
2572 * if the output's storetype is not store_return,
2573 * add append a STORE instruction!
2575 * NOTES on how to do it better without much trouble:
2576 * -) The liferanges!
2577 * Simply check the liferange of all parameters for
2578 * other CALLs. For each param with no CALL in its
2579 * liferange, we can store it in an OFS_PARM at
2580 * generation already. This would even include later
2581 * reuse.... probably... :)
2586 first = vec_size(instr->params);
2589 for (p = 0; p < first; ++p)
2591 ir_value *param = instr->params[p];
2593 stmt.opcode = INSTR_STORE_F;
2596 if (param->vtype == TYPE_FIELD)
2597 stmt.opcode = field_store_instr[param->fieldtype];
2599 stmt.opcode = type_store_instr[param->vtype];
2600 stmt.o1.u1 = ir_value_code_addr(param);
2601 stmt.o2.u1 = OFS_PARM0 + 3 * p;
2602 vec_push(code_statements, stmt);
2604 /* No whandle extparams */
2605 first = vec_size(instr->params);
2606 for (; p < first; ++p)
2608 ir_builder *ir = func->owner;
2609 ir_value *param = instr->params[p];
2612 if (p-8 >= vec_size(ir->extparams)) {
2613 irerror(instr->context, "Not enough extparam-globals have been created");
2617 target = ir->extparams[p-8];
2619 stmt.opcode = INSTR_STORE_F;
2622 if (param->vtype == TYPE_FIELD)
2623 stmt.opcode = field_store_instr[param->fieldtype];
2625 stmt.opcode = type_store_instr[param->vtype];
2626 stmt.o1.u1 = ir_value_code_addr(param);
2627 stmt.o2.u1 = ir_value_code_addr(target);
2628 vec_push(code_statements, stmt);
2631 stmt.opcode = INSTR_CALL0 + vec_size(instr->params);
2632 if (stmt.opcode > INSTR_CALL8)
2633 stmt.opcode = INSTR_CALL8;
2634 stmt.o1.u1 = ir_value_code_addr(instr->_ops[1]);
2637 vec_push(code_statements, stmt);
2639 retvalue = instr->_ops[0];
2640 if (retvalue && retvalue->store != store_return && vec_size(retvalue->life))
2642 /* not to be kept in OFS_RETURN */
2643 if (retvalue->vtype == TYPE_FIELD)
2644 stmt.opcode = field_store_instr[retvalue->vtype];
2646 stmt.opcode = type_store_instr[retvalue->vtype];
2647 stmt.o1.u1 = OFS_RETURN;
2648 stmt.o2.u1 = ir_value_code_addr(retvalue);
2650 vec_push(code_statements, stmt);
2655 if (instr->opcode == INSTR_STATE) {
2656 irerror(block->context, "TODO: state instruction");
2660 stmt.opcode = instr->opcode;
2665 /* This is the general order of operands */
2667 stmt.o3.u1 = ir_value_code_addr(instr->_ops[0]);
2670 stmt.o1.u1 = ir_value_code_addr(instr->_ops[1]);
2673 stmt.o2.u1 = ir_value_code_addr(instr->_ops[2]);
2675 if (stmt.opcode == INSTR_RETURN || stmt.opcode == INSTR_DONE)
2677 stmt.o1.u1 = stmt.o3.u1;
2680 else if ((stmt.opcode >= INSTR_STORE_F &&
2681 stmt.opcode <= INSTR_STORE_FNC) ||
2682 (stmt.opcode >= INSTR_STOREP_F &&
2683 stmt.opcode <= INSTR_STOREP_FNC))
2685 /* 2-operand instructions with A -> B */
2686 stmt.o2.u1 = stmt.o3.u1;
2690 vec_push(code_statements, stmt);
2695 static bool gen_function_code(ir_function *self)
2698 prog_section_statement stmt;
2700 /* Starting from entry point, we generate blocks "as they come"
2701 * for now. Dead blocks will not be translated obviously.
2703 if (!vec_size(self->blocks)) {
2704 irerror(self->context, "Function '%s' declared without body.", self->name);
2708 block = self->blocks[0];
2709 if (block->generated)
2712 if (!gen_blocks_recursive(self, block)) {
2713 irerror(self->context, "failed to generate blocks for '%s'", self->name);
2717 /* otherwise code_write crashes since it debug-prints functions until AINSTR_END */
2718 stmt.opcode = AINSTR_END;
2722 vec_push(code_statements, stmt);
2726 static qcint ir_builder_filestring(ir_builder *ir, const char *filename)
2728 /* NOTE: filename pointers are copied, we never strdup them,
2729 * thus we can use pointer-comparison to find the string.
2734 for (i = 0; i < vec_size(ir->filenames); ++i) {
2735 if (ir->filenames[i] == filename)
2736 return ir->filestrings[i];
2739 str = code_genstring(filename);
2740 vec_push(ir->filenames, filename);
2741 vec_push(ir->filestrings, str);
2745 static bool gen_global_function(ir_builder *ir, ir_value *global)
2747 prog_section_function fun;
2751 size_t local_var_end;
2753 if (!global->isconst || (!global->constval.vfunc))
2755 irerror(global->context, "Invalid state of function-global: not constant: %s", global->name);
2759 irfun = global->constval.vfunc;
2761 fun.name = global->code.name;
2762 fun.file = ir_builder_filestring(ir, global->context.file);
2763 fun.profile = 0; /* always 0 */
2764 fun.nargs = vec_size(irfun->params);
2768 for (i = 0;i < 8; ++i) {
2772 fun.argsize[i] = type_sizeof[irfun->params[i]];
2775 fun.firstlocal = vec_size(code_globals);
2777 local_var_end = fun.firstlocal;
2778 for (i = 0; i < vec_size(irfun->locals); ++i) {
2779 if (!ir_builder_gen_global(ir, irfun->locals[i], true)) {
2780 irerror(irfun->locals[i]->context, "Failed to generate local %s", irfun->locals[i]->name);
2784 if (vec_size(irfun->locals)) {
2785 ir_value *last = vec_last(irfun->locals);
2786 local_var_end = last->code.globaladdr;
2787 local_var_end += type_sizeof[last->vtype];
2789 for (i = 0; i < vec_size(irfun->values); ++i)
2791 /* generate code.globaladdr for ssa values */
2792 ir_value *v = irfun->values[i];
2793 ir_value_code_setaddr(v, local_var_end + v->code.local);
2795 for (i = 0; i < irfun->allocated_locals; ++i) {
2796 /* fill the locals with zeros */
2797 vec_push(code_globals, 0);
2800 fun.locals = vec_size(code_globals) - fun.firstlocal;
2803 fun.entry = irfun->builtin;
2805 irfun->code_function_def = vec_size(code_functions);
2806 fun.entry = vec_size(code_statements);
2809 vec_push(code_functions, fun);
2813 static void ir_gen_extparam(ir_builder *ir)
2815 prog_section_def def;
2819 snprintf(name, sizeof(name), "EXTPARM#%i", (int)(vec_size(ir->extparams)+8));
2820 global = ir_value_var(name, store_global, TYPE_VECTOR);
2822 def.name = code_genstring(name);
2823 def.type = TYPE_VECTOR;
2824 def.offset = vec_size(code_globals);
2826 vec_push(code_defs, def);
2827 ir_value_code_setaddr(global, def.offset);
2828 vec_push(code_globals, 0);
2829 vec_push(code_globals, 0);
2830 vec_push(code_globals, 0);
2832 vec_push(ir->extparams, global);
2835 static bool gen_function_extparam_copy(ir_function *self)
2837 size_t i, ext, numparams;
2839 ir_builder *ir = self->owner;
2841 prog_section_statement stmt;
2843 numparams = vec_size(self->params);
2847 stmt.opcode = INSTR_STORE_F;
2849 for (i = 8; i < numparams; ++i) {
2851 if (ext >= vec_size(ir->extparams))
2852 ir_gen_extparam(ir);
2854 ep = ir->extparams[ext];
2856 stmt.opcode = type_store_instr[self->locals[i]->vtype];
2857 if (self->locals[i]->vtype == TYPE_FIELD &&
2858 self->locals[i]->fieldtype == TYPE_VECTOR)
2860 stmt.opcode = INSTR_STORE_V;
2862 stmt.o1.u1 = ir_value_code_addr(ep);
2863 stmt.o2.u1 = ir_value_code_addr(self->locals[i]);
2864 vec_push(code_statements, stmt);
2870 static bool gen_global_function_code(ir_builder *ir, ir_value *global)
2872 prog_section_function *fundef;
2875 irfun = global->constval.vfunc;
2877 irwarning(global->context, WARN_IMPLICIT_FUNCTION_POINTER,
2878 "function `%s` has no body and in QC implicitly becomes a function-pointer", global->name);
2879 /* this was a function pointer, don't generate code for those */
2886 if (irfun->code_function_def < 0) {
2887 irerror(irfun->context, "`%s`: IR global wasn't generated, failed to access function-def", irfun->name);
2890 fundef = &code_functions[irfun->code_function_def];
2892 fundef->entry = vec_size(code_statements);
2893 if (!gen_function_extparam_copy(irfun)) {
2894 irerror(irfun->context, "Failed to generate extparam-copy code for function %s", irfun->name);
2897 if (!gen_function_code(irfun)) {
2898 irerror(irfun->context, "Failed to generate code for function %s", irfun->name);
2904 static bool ir_builder_gen_global(ir_builder *self, ir_value *global, bool islocal)
2908 prog_section_def def;
2910 def.type = global->vtype;
2911 def.offset = vec_size(code_globals);
2914 if (global->name[0] == '#') {
2915 if (!self->str_immediate)
2916 self->str_immediate = code_genstring("IMMEDIATE");
2917 def.name = global->code.name = self->str_immediate;
2920 def.name = global->code.name = code_genstring(global->name);
2925 switch (global->vtype)
2928 if (!strcmp(global->name, "end_sys_globals")) {
2929 /* TODO: remember this point... all the defs before this one
2930 * should be checksummed and added to progdefs.h when we generate it.
2933 else if (!strcmp(global->name, "end_sys_fields")) {
2934 /* TODO: same as above but for entity-fields rather than globsl
2938 irwarning(global->context, WARN_VOID_VARIABLES, "unrecognized variable of type void `%s`",
2940 /* I'd argue setting it to 0 is sufficient, but maybe some depend on knowing how far
2941 * the system fields actually go? Though the engine knows this anyway...
2942 * Maybe this could be an -foption
2943 * fteqcc creates data for end_sys_* - of size 1, so let's do the same
2945 ir_value_code_setaddr(global, vec_size(code_globals));
2946 vec_push(code_globals, 0);
2948 vec_push(code_defs, def);
2951 vec_push(code_defs, def);
2952 return gen_global_pointer(global);
2954 vec_push(code_defs, def);
2955 return gen_global_field(global);
2960 ir_value_code_setaddr(global, vec_size(code_globals));
2961 if (global->isconst) {
2962 iptr = (int32_t*)&global->constval.ivec[0];
2963 vec_push(code_globals, *iptr);
2965 vec_push(code_globals, 0);
2967 def.type |= DEF_SAVEGLOBAL;
2969 vec_push(code_defs, def);
2971 return global->code.globaladdr >= 0;
2975 ir_value_code_setaddr(global, vec_size(code_globals));
2976 if (global->isconst) {
2977 vec_push(code_globals, code_genstring(global->constval.vstring));
2979 vec_push(code_globals, 0);
2981 def.type |= DEF_SAVEGLOBAL;
2983 vec_push(code_defs, def);
2984 return global->code.globaladdr >= 0;
2989 ir_value_code_setaddr(global, vec_size(code_globals));
2990 if (global->isconst) {
2991 iptr = (int32_t*)&global->constval.ivec[0];
2992 vec_push(code_globals, iptr[0]);
2993 if (global->code.globaladdr < 0)
2995 for (d = 1; d < type_sizeof[global->vtype]; ++d)
2997 vec_push(code_globals, iptr[d]);
3000 vec_push(code_globals, 0);
3001 if (global->code.globaladdr < 0)
3003 for (d = 1; d < type_sizeof[global->vtype]; ++d)
3005 vec_push(code_globals, 0);
3008 def.type |= DEF_SAVEGLOBAL;
3011 vec_push(code_defs, def);
3012 return global->code.globaladdr >= 0;
3015 ir_value_code_setaddr(global, vec_size(code_globals));
3016 if (!global->isconst) {
3017 vec_push(code_globals, 0);
3018 if (global->code.globaladdr < 0)
3021 vec_push(code_globals, vec_size(code_functions));
3022 if (!gen_global_function(self, global))
3025 def.type |= DEF_SAVEGLOBAL;
3027 vec_push(code_defs, def);
3030 /* assume biggest type */
3031 ir_value_code_setaddr(global, vec_size(code_globals));
3032 vec_push(code_globals, 0);
3033 for (i = 1; i < type_sizeof[TYPE_VARIANT]; ++i)
3034 vec_push(code_globals, 0);
3037 /* refuse to create 'void' type or any other fancy business. */
3038 irerror(global->context, "Invalid type for global variable `%s`: %s",
3039 global->name, type_name[global->vtype]);
3044 static bool ir_builder_gen_field(ir_builder *self, ir_value *field)
3046 prog_section_def def;
3047 prog_section_field fld;
3049 def.type = field->vtype;
3050 def.offset = vec_size(code_globals);
3052 /* create a global named the same as the field */
3053 if (opts_standard == COMPILER_GMQCC) {
3054 /* in our standard, the global gets a dot prefix */
3055 size_t len = strlen(field->name);
3058 /* we really don't want to have to allocate this, and 1024
3059 * bytes is more than enough for a variable/field name
3061 if (len+2 >= sizeof(name)) {
3062 irerror(field->context, "invalid field name size: %u", (unsigned int)len);
3067 memcpy(name+1, field->name, len); /* no strncpy - we used strlen above */
3070 def.name = code_genstring(name);
3071 fld.name = def.name + 1; /* we reuse that string table entry */
3073 /* in plain QC, there cannot be a global with the same name,
3074 * and so we also name the global the same.
3075 * FIXME: fteqcc should create a global as well
3076 * check if it actually uses the same name. Probably does
3078 def.name = code_genstring(field->name);
3079 fld.name = def.name;
3082 field->code.name = def.name;
3084 vec_push(code_defs, def);
3086 fld.type = field->fieldtype;
3088 if (fld.type == TYPE_VOID) {
3089 irerror(field->context, "field is missing a type: %s - don't know its size", field->name);
3093 fld.offset = code_alloc_field(type_sizeof[field->fieldtype]);
3095 vec_push(code_fields, fld);
3097 ir_value_code_setaddr(field, vec_size(code_globals));
3098 vec_push(code_globals, fld.offset);
3099 if (fld.type == TYPE_VECTOR) {
3100 vec_push(code_globals, fld.offset+1);
3101 vec_push(code_globals, fld.offset+2);
3104 return field->code.globaladdr >= 0;
3107 bool ir_builder_generate(ir_builder *self, const char *filename)
3109 prog_section_statement stmt;
3114 for (i = 0; i < vec_size(self->globals); ++i)
3116 if (!ir_builder_gen_global(self, self->globals[i], false)) {
3121 for (i = 0; i < vec_size(self->fields); ++i)
3123 if (!ir_builder_gen_field(self, self->fields[i])) {
3128 /* generate function code */
3129 for (i = 0; i < vec_size(self->globals); ++i)
3131 if (self->globals[i]->vtype == TYPE_FUNCTION) {
3132 if (!gen_global_function_code(self, self->globals[i])) {
3138 /* DP errors if the last instruction is not an INSTR_DONE
3139 * and for debugging purposes we add an additional AINSTR_END
3140 * to the end of functions, so here it goes:
3142 stmt.opcode = INSTR_DONE;
3146 vec_push(code_statements, stmt);
3149 con_out("writing '%s'...\n", filename);
3150 return code_write(filename);
3153 /***********************************************************************
3154 *IR DEBUG Dump functions...
3157 #define IND_BUFSZ 1024
3160 # define strncat(dst, src, sz) strncat_s(dst, sz, src, _TRUNCATE)
3163 const char *qc_opname(int op)
3165 if (op < 0) return "<INVALID>";
3166 if (op < ( sizeof(asm_instr) / sizeof(asm_instr[0]) ))
3167 return asm_instr[op].m;
3169 case VINSTR_PHI: return "PHI";
3170 case VINSTR_JUMP: return "JUMP";
3171 case VINSTR_COND: return "COND";
3172 default: return "<UNK>";
3176 void ir_builder_dump(ir_builder *b, int (*oprintf)(const char*, ...))
3179 char indent[IND_BUFSZ];
3183 oprintf("module %s\n", b->name);
3184 for (i = 0; i < vec_size(b->globals); ++i)
3187 if (b->globals[i]->isconst)
3188 oprintf("%s = ", b->globals[i]->name);
3189 ir_value_dump(b->globals[i], oprintf);
3192 for (i = 0; i < vec_size(b->functions); ++i)
3193 ir_function_dump(b->functions[i], indent, oprintf);
3194 oprintf("endmodule %s\n", b->name);
3197 void ir_function_dump(ir_function *f, char *ind,
3198 int (*oprintf)(const char*, ...))
3201 if (f->builtin != 0) {
3202 oprintf("%sfunction %s = builtin %i\n", ind, f->name, -f->builtin);
3205 oprintf("%sfunction %s\n", ind, f->name);
3206 strncat(ind, "\t", IND_BUFSZ);
3207 if (vec_size(f->locals))
3209 oprintf("%s%i locals:\n", ind, (int)vec_size(f->locals));
3210 for (i = 0; i < vec_size(f->locals); ++i) {
3211 oprintf("%s\t", ind);
3212 ir_value_dump(f->locals[i], oprintf);
3216 oprintf("%sliferanges:\n", ind);
3217 for (i = 0; i < vec_size(f->locals); ++i) {
3219 ir_value *v = f->locals[i];
3220 oprintf("%s\t%s: unique ", ind, v->name);
3221 for (l = 0; l < vec_size(v->life); ++l) {
3222 oprintf("[%i,%i] ", v->life[l].start, v->life[l].end);
3226 for (i = 0; i < vec_size(f->values); ++i) {
3228 ir_value *v = f->values[i];
3229 oprintf("%s\t%s: @%i ", ind, v->name, (int)v->code.local);
3230 for (l = 0; l < vec_size(v->life); ++l) {
3231 oprintf("[%i,%i] ", v->life[l].start, v->life[l].end);
3235 if (vec_size(f->blocks))
3237 oprintf("%slife passes (check): %i\n", ind, (int)f->run_id);
3238 for (i = 0; i < vec_size(f->blocks); ++i) {
3239 if (f->blocks[i]->run_id != f->run_id) {
3240 oprintf("%slife pass check fail! %i != %i\n", ind, (int)f->blocks[i]->run_id, (int)f->run_id);
3242 ir_block_dump(f->blocks[i], ind, oprintf);
3246 ind[strlen(ind)-1] = 0;
3247 oprintf("%sendfunction %s\n", ind, f->name);
3250 void ir_block_dump(ir_block* b, char *ind,
3251 int (*oprintf)(const char*, ...))
3254 oprintf("%s:%s\n", ind, b->label);
3255 strncat(ind, "\t", IND_BUFSZ);
3257 for (i = 0; i < vec_size(b->instr); ++i)
3258 ir_instr_dump(b->instr[i], ind, oprintf);
3259 ind[strlen(ind)-1] = 0;
3262 void dump_phi(ir_instr *in, char *ind,
3263 int (*oprintf)(const char*, ...))
3266 oprintf("%s <- phi ", in->_ops[0]->name);
3267 for (i = 0; i < vec_size(in->phi); ++i)
3269 oprintf("([%s] : %s) ", in->phi[i].from->label,
3270 in->phi[i].value->name);
3275 void ir_instr_dump(ir_instr *in, char *ind,
3276 int (*oprintf)(const char*, ...))
3279 const char *comma = NULL;
3281 oprintf("%s (%i) ", ind, (int)in->eid);
3283 if (in->opcode == VINSTR_PHI) {
3284 dump_phi(in, ind, oprintf);
3288 strncat(ind, "\t", IND_BUFSZ);
3290 if (in->_ops[0] && (in->_ops[1] || in->_ops[2])) {
3291 ir_value_dump(in->_ops[0], oprintf);
3292 if (in->_ops[1] || in->_ops[2])
3295 if (in->opcode == INSTR_CALL0) {
3296 oprintf("CALL%i\t", vec_size(in->params));
3298 oprintf("%s\t", qc_opname(in->opcode));
3300 if (in->_ops[0] && !(in->_ops[1] || in->_ops[2])) {
3301 ir_value_dump(in->_ops[0], oprintf);
3306 for (i = 1; i != 3; ++i) {
3310 ir_value_dump(in->_ops[i], oprintf);
3318 oprintf("[%s]", in->bops[0]->label);
3322 oprintf("%s[%s]", comma, in->bops[1]->label);
3323 if (vec_size(in->params)) {
3324 oprintf("\tparams: ");
3325 for (i = 0; i != vec_size(in->params); ++i) {
3326 oprintf("%s, ", in->params[i]->name);
3330 ind[strlen(ind)-1] = 0;
3333 void ir_value_dump_string(const char *str, int (*oprintf)(const char*, ...))
3336 for (; *str; ++str) {
3338 case '\n': oprintf("\\n"); break;
3339 case '\r': oprintf("\\r"); break;
3340 case '\t': oprintf("\\t"); break;
3341 case '\v': oprintf("\\v"); break;
3342 case '\f': oprintf("\\f"); break;
3343 case '\b': oprintf("\\b"); break;
3344 case '\a': oprintf("\\a"); break;
3345 case '\\': oprintf("\\\\"); break;
3346 case '"': oprintf("\\\""); break;
3347 default: oprintf("%c", *str); break;
3353 void ir_value_dump(ir_value* v, int (*oprintf)(const char*, ...))
3362 oprintf("fn:%s", v->name);
3365 oprintf("%g", v->constval.vfloat);
3368 oprintf("'%g %g %g'",
3371 v->constval.vvec.z);
3374 oprintf("(entity)");
3377 ir_value_dump_string(v->constval.vstring, oprintf);
3381 oprintf("%i", v->constval.vint);
3386 v->constval.vpointer->name);
3390 oprintf("%s", v->name);
3394 void ir_value_dump_life(const ir_value *self, int (*oprintf)(const char*,...))
3397 oprintf("Life of %12s:", self->name);
3398 for (i = 0; i < vec_size(self->life); ++i)
3400 oprintf(" + [%i, %i]\n", self->life[i].start, self->life[i].end);