5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is furnished to do
10 * so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 /***********************************************************************
29 * Type sizes used at multiple points in the IR codegen
32 const char *type_name[TYPE_COUNT] = {
48 size_t type_sizeof[TYPE_COUNT] = {
55 1, /* TYPE_FUNCTION */
64 uint16_t type_store_instr[TYPE_COUNT] = {
65 INSTR_STORE_F, /* should use I when having integer support */
72 INSTR_STORE_ENT, /* should use I */
74 INSTR_STORE_I, /* integer type */
79 INSTR_STORE_V, /* variant, should never be accessed */
81 AINSTR_END, /* struct */
82 AINSTR_END, /* union */
83 AINSTR_END, /* array */
86 uint16_t field_store_instr[TYPE_COUNT] = {
96 INSTR_STORE_FLD, /* integer type */
101 INSTR_STORE_V, /* variant, should never be accessed */
103 AINSTR_END, /* struct */
104 AINSTR_END, /* union */
105 AINSTR_END, /* array */
108 uint16_t type_storep_instr[TYPE_COUNT] = {
109 INSTR_STOREP_F, /* should use I when having integer support */
116 INSTR_STOREP_ENT, /* should use I */
118 INSTR_STOREP_ENT, /* integer type */
123 INSTR_STOREP_V, /* variant, should never be accessed */
125 AINSTR_END, /* struct */
126 AINSTR_END, /* union */
127 AINSTR_END, /* array */
130 uint16_t type_eq_instr[TYPE_COUNT] = {
131 INSTR_EQ_F, /* should use I when having integer support */
136 INSTR_EQ_E, /* FLD has no comparison */
138 INSTR_EQ_E, /* should use I */
145 INSTR_EQ_V, /* variant, should never be accessed */
147 AINSTR_END, /* struct */
148 AINSTR_END, /* union */
149 AINSTR_END, /* array */
152 uint16_t type_ne_instr[TYPE_COUNT] = {
153 INSTR_NE_F, /* should use I when having integer support */
158 INSTR_NE_E, /* FLD has no comparison */
160 INSTR_NE_E, /* should use I */
167 INSTR_NE_V, /* variant, should never be accessed */
169 AINSTR_END, /* struct */
170 AINSTR_END, /* union */
171 AINSTR_END, /* array */
174 static void irerror(lex_ctx ctx, const char *msg, ...)
178 con_cvprintmsg((void*)&ctx, LVL_ERROR, "internal error", msg, ap);
182 static bool irwarning(lex_ctx ctx, int warntype, const char *fmt, ...)
185 int lvl = LVL_WARNING;
187 if (warntype && !OPTS_WARN(warntype))
194 con_vprintmsg(lvl, ctx.file, ctx.line, "warning", fmt, ap);
200 /***********************************************************************
201 * Vector utility functions
204 bool GMQCC_WARN vec_ir_value_find(ir_value **vec, ir_value *what, size_t *idx)
207 size_t len = vec_size(vec);
208 for (i = 0; i < len; ++i) {
209 if (vec[i] == what) {
217 bool GMQCC_WARN vec_ir_block_find(ir_block **vec, ir_block *what, size_t *idx)
220 size_t len = vec_size(vec);
221 for (i = 0; i < len; ++i) {
222 if (vec[i] == what) {
230 bool GMQCC_WARN vec_ir_instr_find(ir_instr **vec, ir_instr *what, size_t *idx)
233 size_t len = vec_size(vec);
234 for (i = 0; i < len; ++i) {
235 if (vec[i] == what) {
243 /***********************************************************************
247 static void ir_block_delete_quick(ir_block* self);
248 static void ir_instr_delete_quick(ir_instr *self);
249 static void ir_function_delete_quick(ir_function *self);
251 ir_builder* ir_builder_new(const char *modulename)
255 self = (ir_builder*)mem_a(sizeof(*self));
259 self->functions = NULL;
260 self->globals = NULL;
262 self->extparams = NULL;
263 self->filenames = NULL;
264 self->filestrings = NULL;
266 self->str_immediate = 0;
268 if (!ir_builder_set_name(self, modulename)) {
276 void ir_builder_delete(ir_builder* self)
279 mem_d((void*)self->name);
280 for (i = 0; i != vec_size(self->functions); ++i) {
281 ir_function_delete_quick(self->functions[i]);
283 vec_free(self->functions);
284 for (i = 0; i != vec_size(self->extparams); ++i) {
285 ir_value_delete(self->extparams[i]);
287 vec_free(self->extparams);
288 for (i = 0; i != vec_size(self->globals); ++i) {
289 ir_value_delete(self->globals[i]);
291 vec_free(self->globals);
292 for (i = 0; i != vec_size(self->fields); ++i) {
293 ir_value_delete(self->fields[i]);
295 vec_free(self->fields);
296 vec_free(self->filenames);
297 vec_free(self->filestrings);
301 bool ir_builder_set_name(ir_builder *self, const char *name)
304 mem_d((void*)self->name);
305 self->name = util_strdup(name);
309 ir_function* ir_builder_get_function(ir_builder *self, const char *name)
312 for (i = 0; i < vec_size(self->functions); ++i) {
313 if (!strcmp(name, self->functions[i]->name))
314 return self->functions[i];
319 ir_function* ir_builder_create_function(ir_builder *self, const char *name, int outtype)
321 ir_function *fn = ir_builder_get_function(self, name);
326 fn = ir_function_new(self, outtype);
327 if (!ir_function_set_name(fn, name))
329 ir_function_delete(fn);
332 vec_push(self->functions, fn);
334 fn->value = ir_builder_create_global(self, fn->name, TYPE_FUNCTION);
336 ir_function_delete(fn);
340 fn->value->isconst = true;
341 fn->value->outtype = outtype;
342 fn->value->constval.vfunc = fn;
343 fn->value->context = fn->context;
348 ir_value* ir_builder_get_global(ir_builder *self, const char *name)
351 for (i = 0; i < vec_size(self->globals); ++i) {
352 if (!strcmp(self->globals[i]->name, name))
353 return self->globals[i];
358 ir_value* ir_builder_create_global(ir_builder *self, const char *name, int vtype)
362 if (name && name[0] != '#')
364 ve = ir_builder_get_global(self, name);
370 ve = ir_value_var(name, store_global, vtype);
371 vec_push(self->globals, ve);
375 ir_value* ir_builder_get_field(ir_builder *self, const char *name)
378 for (i = 0; i < vec_size(self->fields); ++i) {
379 if (!strcmp(self->fields[i]->name, name))
380 return self->fields[i];
386 ir_value* ir_builder_create_field(ir_builder *self, const char *name, int vtype)
388 ir_value *ve = ir_builder_get_field(self, name);
393 ve = ir_value_var(name, store_global, TYPE_FIELD);
394 ve->fieldtype = vtype;
395 vec_push(self->fields, ve);
399 /***********************************************************************
403 bool ir_function_naive_phi(ir_function*);
404 void ir_function_enumerate(ir_function*);
405 bool ir_function_calculate_liferanges(ir_function*);
406 bool ir_function_allocate_locals(ir_function*);
408 ir_function* ir_function_new(ir_builder* owner, int outtype)
411 self = (ir_function*)mem_a(sizeof(*self));
416 memset(self, 0, sizeof(*self));
419 if (!ir_function_set_name(self, "<@unnamed>")) {
424 self->context.file = "<@no context>";
425 self->context.line = 0;
426 self->outtype = outtype;
435 self->code_function_def = -1;
436 self->allocated_locals = 0;
442 bool ir_function_set_name(ir_function *self, const char *name)
445 mem_d((void*)self->name);
446 self->name = util_strdup(name);
450 static void ir_function_delete_quick(ir_function *self)
453 mem_d((void*)self->name);
455 for (i = 0; i != vec_size(self->blocks); ++i)
456 ir_block_delete_quick(self->blocks[i]);
457 vec_free(self->blocks);
459 vec_free(self->params);
461 for (i = 0; i != vec_size(self->values); ++i)
462 ir_value_delete(self->values[i]);
463 vec_free(self->values);
465 for (i = 0; i != vec_size(self->locals); ++i)
466 ir_value_delete(self->locals[i]);
467 vec_free(self->locals);
469 /* self->value is deleted by the builder */
474 void ir_function_delete(ir_function *self)
477 mem_d((void*)self->name);
479 for (i = 0; i != vec_size(self->blocks); ++i)
480 ir_block_delete(self->blocks[i]);
481 vec_free(self->blocks);
483 vec_free(self->params);
485 for (i = 0; i != vec_size(self->values); ++i)
486 ir_value_delete(self->values[i]);
487 vec_free(self->values);
489 for (i = 0; i != vec_size(self->locals); ++i)
490 ir_value_delete(self->locals[i]);
491 vec_free(self->locals);
493 /* self->value is deleted by the builder */
498 void ir_function_collect_value(ir_function *self, ir_value *v)
500 vec_push(self->values, v);
503 ir_block* ir_function_create_block(ir_function *self, const char *label)
505 ir_block* bn = ir_block_new(self, label);
506 memcpy(&bn->context, &self->context, sizeof(self->context));
507 vec_push(self->blocks, bn);
511 bool ir_function_finalize(ir_function *self)
516 if (!ir_function_naive_phi(self))
519 ir_function_enumerate(self);
521 if (!ir_function_calculate_liferanges(self))
524 if (!ir_function_allocate_locals(self))
529 ir_value* ir_function_get_local(ir_function *self, const char *name)
532 for (i = 0; i < vec_size(self->locals); ++i) {
533 if (!strcmp(self->locals[i]->name, name))
534 return self->locals[i];
539 ir_value* ir_function_create_local(ir_function *self, const char *name, int vtype, bool param)
544 if (ir_function_get_local(self, name))
549 vec_size(self->locals) &&
550 self->locals[vec_size(self->locals)-1]->store != store_param) {
551 irerror(self->context, "cannot add parameters after adding locals");
555 ve = ir_value_var(name, (param ? store_param : store_local), vtype);
556 vec_push(self->locals, ve);
560 /***********************************************************************
564 ir_block* ir_block_new(ir_function* owner, const char *name)
567 self = (ir_block*)mem_a(sizeof(*self));
571 memset(self, 0, sizeof(*self));
574 if (name && !ir_block_set_label(self, name)) {
579 self->context.file = "<@no context>";
580 self->context.line = 0;
584 self->entries = NULL;
588 self->is_return = false;
593 self->generated = false;
598 static void ir_block_delete_quick(ir_block* self)
601 if (self->label) mem_d(self->label);
602 for (i = 0; i != vec_size(self->instr); ++i)
603 ir_instr_delete_quick(self->instr[i]);
604 vec_free(self->instr);
605 vec_free(self->entries);
606 vec_free(self->exits);
607 vec_free(self->living);
611 void ir_block_delete(ir_block* self)
614 if (self->label) mem_d(self->label);
615 for (i = 0; i != vec_size(self->instr); ++i)
616 ir_instr_delete(self->instr[i]);
617 vec_free(self->instr);
618 vec_free(self->entries);
619 vec_free(self->exits);
620 vec_free(self->living);
624 bool ir_block_set_label(ir_block *self, const char *name)
627 mem_d((void*)self->label);
628 self->label = util_strdup(name);
629 return !!self->label;
632 /***********************************************************************
636 ir_instr* ir_instr_new(ir_block* owner, int op)
639 self = (ir_instr*)mem_a(sizeof(*self));
644 self->context.file = "<@no context>";
645 self->context.line = 0;
647 self->_ops[0] = NULL;
648 self->_ops[1] = NULL;
649 self->_ops[2] = NULL;
650 self->bops[0] = NULL;
651 self->bops[1] = NULL;
660 static void ir_instr_delete_quick(ir_instr *self)
663 vec_free(self->params);
667 void ir_instr_delete(ir_instr *self)
670 /* The following calls can only delete from
671 * vectors, we still want to delete this instruction
672 * so ignore the return value. Since with the warn_unused_result attribute
673 * gcc doesn't care about an explicit: (void)foo(); to ignore the result,
674 * I have to improvise here and use if(foo());
676 for (i = 0; i < vec_size(self->phi); ++i) {
678 if (vec_ir_instr_find(self->phi[i].value->writes, self, &idx))
679 vec_remove(self->phi[i].value->writes, idx, 1);
680 if (vec_ir_instr_find(self->phi[i].value->reads, self, &idx))
681 vec_remove(self->phi[i].value->reads, idx, 1);
684 for (i = 0; i < vec_size(self->params); ++i) {
686 if (vec_ir_instr_find(self->params[i]->writes, self, &idx))
687 vec_remove(self->params[i]->writes, idx, 1);
688 if (vec_ir_instr_find(self->params[i]->reads, self, &idx))
689 vec_remove(self->params[i]->reads, idx, 1);
691 vec_free(self->params);
692 (void)!ir_instr_op(self, 0, NULL, false);
693 (void)!ir_instr_op(self, 1, NULL, false);
694 (void)!ir_instr_op(self, 2, NULL, false);
698 bool ir_instr_op(ir_instr *self, int op, ir_value *v, bool writing)
700 if (self->_ops[op]) {
702 if (writing && vec_ir_instr_find(self->_ops[op]->writes, self, &idx))
703 vec_remove(self->_ops[op]->writes, idx, 1);
704 else if (vec_ir_instr_find(self->_ops[op]->reads, self, &idx))
705 vec_remove(self->_ops[op]->reads, idx, 1);
709 vec_push(v->writes, self);
711 vec_push(v->reads, self);
717 /***********************************************************************
721 void ir_value_code_setaddr(ir_value *self, int32_t gaddr)
723 self->code.globaladdr = gaddr;
724 if (self->members[0]) self->members[0]->code.globaladdr = gaddr;
725 if (self->members[1]) self->members[1]->code.globaladdr = gaddr;
726 if (self->members[2]) self->members[2]->code.globaladdr = gaddr;
729 int32_t ir_value_code_addr(const ir_value *self)
731 if (self->store == store_return)
732 return OFS_RETURN + self->code.addroffset;
733 return self->code.globaladdr + self->code.addroffset;
736 ir_value* ir_value_var(const char *name, int storetype, int vtype)
739 self = (ir_value*)mem_a(sizeof(*self));
741 self->fieldtype = TYPE_VOID;
742 self->outtype = TYPE_VOID;
743 self->store = storetype;
748 self->isconst = false;
749 self->context.file = "<@no context>";
750 self->context.line = 0;
752 if (name && !ir_value_set_name(self, name)) {
753 irerror(self->context, "out of memory");
758 memset(&self->constval, 0, sizeof(self->constval));
759 memset(&self->code, 0, sizeof(self->code));
761 self->members[0] = NULL;
762 self->members[1] = NULL;
763 self->members[2] = NULL;
764 self->memberof = NULL;
770 ir_value* ir_value_vector_member(ir_value *self, unsigned int member)
776 if (self->members[member])
777 return self->members[member];
779 if (self->vtype == TYPE_VECTOR)
781 m = ir_value_var(self->name, self->store, TYPE_FLOAT);
784 m->context = self->context;
786 self->members[member] = m;
787 m->code.addroffset = member;
789 else if (self->vtype == TYPE_FIELD)
791 if (self->fieldtype != TYPE_VECTOR)
793 m = ir_value_var(self->name, self->store, TYPE_FIELD);
796 m->fieldtype = TYPE_FLOAT;
797 m->context = self->context;
799 self->members[member] = m;
800 m->code.addroffset = member;
804 irerror(self->context, "invalid member access on %s", self->name);
812 ir_value* ir_value_out(ir_function *owner, const char *name, int storetype, int vtype)
814 ir_value *v = ir_value_var(name, storetype, vtype);
817 ir_function_collect_value(owner, v);
821 void ir_value_delete(ir_value* self)
825 mem_d((void*)self->name);
828 if (self->vtype == TYPE_STRING)
829 mem_d((void*)self->constval.vstring);
831 for (i = 0; i < 3; ++i) {
832 if (self->members[i])
833 ir_value_delete(self->members[i]);
835 vec_free(self->reads);
836 vec_free(self->writes);
837 vec_free(self->life);
841 bool ir_value_set_name(ir_value *self, const char *name)
844 mem_d((void*)self->name);
845 self->name = util_strdup(name);
849 bool ir_value_set_float(ir_value *self, float f)
851 if (self->vtype != TYPE_FLOAT)
853 self->constval.vfloat = f;
854 self->isconst = true;
858 bool ir_value_set_func(ir_value *self, int f)
860 if (self->vtype != TYPE_FUNCTION)
862 self->constval.vint = f;
863 self->isconst = true;
867 bool ir_value_set_vector(ir_value *self, vector v)
869 if (self->vtype != TYPE_VECTOR)
871 self->constval.vvec = v;
872 self->isconst = true;
876 bool ir_value_set_field(ir_value *self, ir_value *fld)
878 if (self->vtype != TYPE_FIELD)
880 self->constval.vpointer = fld;
881 self->isconst = true;
885 static char *ir_strdup(const char *str)
888 /* actually dup empty strings */
889 char *out = mem_a(1);
893 return util_strdup(str);
896 bool ir_value_set_string(ir_value *self, const char *str)
898 if (self->vtype != TYPE_STRING)
900 self->constval.vstring = ir_strdup(str);
901 self->isconst = true;
906 bool ir_value_set_int(ir_value *self, int i)
908 if (self->vtype != TYPE_INTEGER)
910 self->constval.vint = i;
911 self->isconst = true;
916 bool ir_value_lives(ir_value *self, size_t at)
919 for (i = 0; i < vec_size(self->life); ++i)
921 ir_life_entry_t *life = &self->life[i];
922 if (life->start <= at && at <= life->end)
924 if (life->start > at) /* since it's ordered */
930 bool ir_value_life_insert(ir_value *self, size_t idx, ir_life_entry_t e)
933 vec_push(self->life, e);
934 for (k = vec_size(self->life)-1; k > idx; --k)
935 self->life[k] = self->life[k-1];
940 bool ir_value_life_merge(ir_value *self, size_t s)
943 ir_life_entry_t *life = NULL;
944 ir_life_entry_t *before = NULL;
945 ir_life_entry_t new_entry;
947 /* Find the first range >= s */
948 for (i = 0; i < vec_size(self->life); ++i)
951 life = &self->life[i];
955 /* nothing found? append */
956 if (i == vec_size(self->life)) {
958 if (life && life->end+1 == s)
960 /* previous life range can be merged in */
964 if (life && life->end >= s)
967 vec_push(self->life, e);
973 if (before->end + 1 == s &&
974 life->start - 1 == s)
977 before->end = life->end;
978 vec_remove(self->life, i, 1);
981 if (before->end + 1 == s)
987 /* already contained */
988 if (before->end >= s)
992 if (life->start - 1 == s)
997 /* insert a new entry */
998 new_entry.start = new_entry.end = s;
999 return ir_value_life_insert(self, i, new_entry);
1002 bool ir_value_life_merge_into(ir_value *self, const ir_value *other)
1006 if (!vec_size(other->life))
1009 if (!vec_size(self->life)) {
1010 size_t count = vec_size(other->life);
1011 ir_life_entry_t *life = vec_add(self->life, count);
1012 memcpy(life, other->life, count * sizeof(*life));
1017 for (i = 0; i < vec_size(other->life); ++i)
1019 const ir_life_entry_t *life = &other->life[i];
1022 ir_life_entry_t *entry = &self->life[myi];
1024 if (life->end+1 < entry->start)
1026 /* adding an interval before entry */
1027 if (!ir_value_life_insert(self, myi, *life))
1033 if (life->start < entry->start &&
1034 life->end+1 >= entry->start)
1036 /* starts earlier and overlaps */
1037 entry->start = life->start;
1040 if (life->end > entry->end &&
1041 life->start <= entry->end+1)
1043 /* ends later and overlaps */
1044 entry->end = life->end;
1047 /* see if our change combines it with the next ranges */
1048 while (myi+1 < vec_size(self->life) &&
1049 entry->end+1 >= self->life[1+myi].start)
1051 /* overlaps with (myi+1) */
1052 if (entry->end < self->life[1+myi].end)
1053 entry->end = self->life[1+myi].end;
1054 vec_remove(self->life, myi+1, 1);
1055 entry = &self->life[myi];
1058 /* see if we're after the entry */
1059 if (life->start > entry->end)
1062 /* append if we're at the end */
1063 if (myi >= vec_size(self->life)) {
1064 vec_push(self->life, *life);
1067 /* otherweise check the next range */
1076 bool ir_values_overlap(const ir_value *a, const ir_value *b)
1078 /* For any life entry in A see if it overlaps with
1079 * any life entry in B.
1080 * Note that the life entries are orderes, so we can make a
1081 * more efficient algorithm there than naively translating the
1085 ir_life_entry_t *la, *lb, *enda, *endb;
1087 /* first of all, if either has no life range, they cannot clash */
1088 if (!vec_size(a->life) || !vec_size(b->life))
1093 enda = la + vec_size(a->life);
1094 endb = lb + vec_size(b->life);
1097 /* check if the entries overlap, for that,
1098 * both must start before the other one ends.
1100 if (la->start < lb->end &&
1101 lb->start < la->end)
1106 /* entries are ordered
1107 * one entry is earlier than the other
1108 * that earlier entry will be moved forward
1110 if (la->start < lb->start)
1112 /* order: A B, move A forward
1113 * check if we hit the end with A
1118 else /* if (lb->start < la->start) actually <= */
1120 /* order: B A, move B forward
1121 * check if we hit the end with B
1130 /***********************************************************************
1134 bool ir_block_create_store_op(ir_block *self, int op, ir_value *target, ir_value *what)
1138 irerror(self->context, "unreachable statement (%s)", self->label);
1141 in = ir_instr_new(self, op);
1145 if (target->store == store_value &&
1146 (op < INSTR_STOREP_F || op > INSTR_STOREP_FNC))
1148 irerror(self->context, "cannot store to an SSA value");
1149 irerror(self->context, "trying to store: %s <- %s", target->name, what->name);
1150 irerror(self->context, "instruction: %s", asm_instr[op].m);
1154 if (!ir_instr_op(in, 0, target, true) ||
1155 !ir_instr_op(in, 1, what, false))
1159 vec_push(self->instr, in);
1163 bool ir_block_create_store(ir_block *self, ir_value *target, ir_value *what)
1167 if (target->vtype == TYPE_VARIANT)
1168 vtype = what->vtype;
1170 vtype = target->vtype;
1173 if (vtype == TYPE_FLOAT && what->vtype == TYPE_INTEGER)
1174 op = INSTR_CONV_ITOF;
1175 else if (vtype == TYPE_INTEGER && what->vtype == TYPE_FLOAT)
1176 op = INSTR_CONV_FTOI;
1178 op = type_store_instr[vtype];
1180 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS)) {
1181 if (op == INSTR_STORE_FLD && what->fieldtype == TYPE_VECTOR)
1185 return ir_block_create_store_op(self, op, target, what);
1188 bool ir_block_create_storep(ir_block *self, ir_value *target, ir_value *what)
1193 if (target->vtype != TYPE_POINTER)
1196 /* storing using pointer - target is a pointer, type must be
1197 * inferred from source
1199 vtype = what->vtype;
1201 op = type_storep_instr[vtype];
1202 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS)) {
1203 if (op == INSTR_STOREP_FLD && what->fieldtype == TYPE_VECTOR)
1204 op = INSTR_STOREP_V;
1207 return ir_block_create_store_op(self, op, target, what);
1210 bool ir_block_create_return(ir_block *self, ir_value *v)
1214 irerror(self->context, "unreachable statement (%s)", self->label);
1218 self->is_return = true;
1219 in = ir_instr_new(self, INSTR_RETURN);
1223 if (v && !ir_instr_op(in, 0, v, false))
1226 vec_push(self->instr, in);
1230 bool ir_block_create_if(ir_block *self, ir_value *v,
1231 ir_block *ontrue, ir_block *onfalse)
1235 irerror(self->context, "unreachable statement (%s)", self->label);
1239 /*in = ir_instr_new(self, (v->vtype == TYPE_STRING ? INSTR_IF_S : INSTR_IF_F));*/
1240 in = ir_instr_new(self, VINSTR_COND);
1244 if (!ir_instr_op(in, 0, v, false)) {
1245 ir_instr_delete(in);
1249 in->bops[0] = ontrue;
1250 in->bops[1] = onfalse;
1252 vec_push(self->instr, in);
1254 vec_push(self->exits, ontrue);
1255 vec_push(self->exits, onfalse);
1256 vec_push(ontrue->entries, self);
1257 vec_push(onfalse->entries, self);
1261 bool ir_block_create_jump(ir_block *self, ir_block *to)
1265 irerror(self->context, "unreachable statement (%s)", self->label);
1269 in = ir_instr_new(self, VINSTR_JUMP);
1274 vec_push(self->instr, in);
1276 vec_push(self->exits, to);
1277 vec_push(to->entries, self);
1281 bool ir_block_create_goto(ir_block *self, ir_block *to)
1285 irerror(self->context, "unreachable statement (%s)", self->label);
1289 in = ir_instr_new(self, INSTR_GOTO);
1294 vec_push(self->instr, in);
1296 vec_push(self->exits, to);
1297 vec_push(to->entries, self);
1301 ir_instr* ir_block_create_phi(ir_block *self, const char *label, int ot)
1305 in = ir_instr_new(self, VINSTR_PHI);
1308 out = ir_value_out(self->owner, label, store_value, ot);
1310 ir_instr_delete(in);
1313 if (!ir_instr_op(in, 0, out, true)) {
1314 ir_instr_delete(in);
1315 ir_value_delete(out);
1318 vec_push(self->instr, in);
1322 ir_value* ir_phi_value(ir_instr *self)
1324 return self->_ops[0];
1327 void ir_phi_add(ir_instr* self, ir_block *b, ir_value *v)
1331 if (!vec_ir_block_find(self->owner->entries, b, NULL)) {
1332 /* Must not be possible to cause this, otherwise the AST
1333 * is doing something wrong.
1335 irerror(self->context, "Invalid entry block for PHI");
1341 vec_push(v->reads, self);
1342 vec_push(self->phi, pe);
1345 /* call related code */
1346 ir_instr* ir_block_create_call(ir_block *self, const char *label, ir_value *func)
1350 in = ir_instr_new(self, INSTR_CALL0);
1353 out = ir_value_out(self->owner, label, (func->outtype == TYPE_VOID) ? store_return : store_value, func->outtype);
1355 ir_instr_delete(in);
1358 if (!ir_instr_op(in, 0, out, true) ||
1359 !ir_instr_op(in, 1, func, false))
1361 ir_instr_delete(in);
1362 ir_value_delete(out);
1365 vec_push(self->instr, in);
1369 ir_value* ir_call_value(ir_instr *self)
1371 return self->_ops[0];
1374 void ir_call_param(ir_instr* self, ir_value *v)
1376 vec_push(self->params, v);
1377 vec_push(v->reads, self);
1380 /* binary op related code */
1382 ir_value* ir_block_create_binop(ir_block *self,
1383 const char *label, int opcode,
1384 ir_value *left, ir_value *right)
1406 case INSTR_SUB_S: /* -- offset of string as float */
1411 case INSTR_BITOR_IF:
1412 case INSTR_BITOR_FI:
1413 case INSTR_BITAND_FI:
1414 case INSTR_BITAND_IF:
1429 case INSTR_BITAND_I:
1432 case INSTR_RSHIFT_I:
1433 case INSTR_LSHIFT_I:
1455 /* boolean operations result in floats */
1456 if (opcode >= INSTR_EQ_F && opcode <= INSTR_GT)
1458 else if (opcode >= INSTR_LE && opcode <= INSTR_GT)
1461 else if (opcode >= INSTR_LE_I && opcode <= INSTR_EQ_FI)
1466 if (ot == TYPE_VOID) {
1467 /* The AST or parser were supposed to check this! */
1471 return ir_block_create_general_instr(self, label, opcode, left, right, ot);
1474 ir_value* ir_block_create_unary(ir_block *self,
1475 const char *label, int opcode,
1478 int ot = TYPE_FLOAT;
1490 /* QC doesn't have other unary operations. We expect extensions to fill
1491 * the above list, otherwise we assume out-type = in-type, eg for an
1495 ot = operand->vtype;
1498 if (ot == TYPE_VOID) {
1499 /* The AST or parser were supposed to check this! */
1503 /* let's use the general instruction creator and pass NULL for OPB */
1504 return ir_block_create_general_instr(self, label, opcode, operand, NULL, ot);
1507 ir_value* ir_block_create_general_instr(ir_block *self, const char *label,
1508 int op, ir_value *a, ir_value *b, int outype)
1513 out = ir_value_out(self->owner, label, store_value, outype);
1517 instr = ir_instr_new(self, op);
1519 ir_value_delete(out);
1523 if (!ir_instr_op(instr, 0, out, true) ||
1524 !ir_instr_op(instr, 1, a, false) ||
1525 !ir_instr_op(instr, 2, b, false) )
1530 vec_push(self->instr, instr);
1534 ir_instr_delete(instr);
1535 ir_value_delete(out);
1539 ir_value* ir_block_create_fieldaddress(ir_block *self, const char *label, ir_value *ent, ir_value *field)
1543 /* Support for various pointer types todo if so desired */
1544 if (ent->vtype != TYPE_ENTITY)
1547 if (field->vtype != TYPE_FIELD)
1550 v = ir_block_create_general_instr(self, label, INSTR_ADDRESS, ent, field, TYPE_POINTER);
1551 v->fieldtype = field->fieldtype;
1555 ir_value* ir_block_create_load_from_ent(ir_block *self, const char *label, ir_value *ent, ir_value *field, int outype)
1558 if (ent->vtype != TYPE_ENTITY)
1561 /* at some point we could redirect for TYPE_POINTER... but that could lead to carelessness */
1562 if (field->vtype != TYPE_FIELD)
1567 case TYPE_FLOAT: op = INSTR_LOAD_F; break;
1568 case TYPE_VECTOR: op = INSTR_LOAD_V; break;
1569 case TYPE_STRING: op = INSTR_LOAD_S; break;
1570 case TYPE_FIELD: op = INSTR_LOAD_FLD; break;
1571 case TYPE_ENTITY: op = INSTR_LOAD_ENT; break;
1572 case TYPE_FUNCTION: op = INSTR_LOAD_FNC; break;
1574 case TYPE_POINTER: op = INSTR_LOAD_I; break;
1575 case TYPE_INTEGER: op = INSTR_LOAD_I; break;
1578 irerror(self->context, "invalid type for ir_block_create_load_from_ent: %s", type_name[outype]);
1582 return ir_block_create_general_instr(self, label, op, ent, field, outype);
1585 ir_value* ir_block_create_add(ir_block *self,
1587 ir_value *left, ir_value *right)
1590 int l = left->vtype;
1591 int r = right->vtype;
1595 irerror(self->context, "invalid type for ir_block_create_add: %s", type_name[l]);
1611 if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1613 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1618 irerror(self->context, "invalid type for ir_block_create_add: %s", type_name[l]);
1622 return ir_block_create_binop(self, label, op, left, right);
1625 ir_value* ir_block_create_sub(ir_block *self,
1627 ir_value *left, ir_value *right)
1630 int l = left->vtype;
1631 int r = right->vtype;
1636 irerror(self->context, "invalid type for ir_block_create_sub: %s", type_name[l]);
1652 if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1654 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1659 irerror(self->context, "invalid type for ir_block_create_sub: %s", type_name[l]);
1663 return ir_block_create_binop(self, label, op, left, right);
1666 ir_value* ir_block_create_mul(ir_block *self,
1668 ir_value *left, ir_value *right)
1671 int l = left->vtype;
1672 int r = right->vtype;
1677 irerror(self->context, "invalid type for ir_block_create_mul: %s", type_name[l]);
1692 if ( (l == TYPE_VECTOR && r == TYPE_FLOAT) )
1694 else if ( (l == TYPE_FLOAT && r == TYPE_VECTOR) )
1697 else if ( (l == TYPE_VECTOR && r == TYPE_INTEGER) )
1699 else if ( (l == TYPE_INTEGER && r == TYPE_VECTOR) )
1701 else if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1703 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1707 irerror(self->context, "invalid type for ir_block_create_mul: %s", type_name[l]);
1711 return ir_block_create_binop(self, label, op, left, right);
1714 ir_value* ir_block_create_div(ir_block *self,
1716 ir_value *left, ir_value *right)
1719 int l = left->vtype;
1720 int r = right->vtype;
1725 irerror(self->context, "invalid type for ir_block_create_div: %s", type_name[l]);
1738 if ( (l == TYPE_VECTOR && r == TYPE_FLOAT) )
1740 else if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1742 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1747 irerror(self->context, "invalid type for ir_block_create_div: %s", type_name[l]);
1751 return ir_block_create_binop(self, label, op, left, right);
1754 /* PHI resolving breaks the SSA, and must thus be the last
1755 * step before life-range calculation.
1758 static bool ir_block_naive_phi(ir_block *self);
1759 bool ir_function_naive_phi(ir_function *self)
1763 for (i = 0; i < vec_size(self->blocks); ++i)
1765 if (!ir_block_naive_phi(self->blocks[i]))
1771 static bool ir_naive_phi_emit_store(ir_block *block, size_t iid, ir_value *old, ir_value *what)
1776 /* create a store */
1777 if (!ir_block_create_store(block, old, what))
1780 /* we now move it up */
1781 instr = vec_last(block->instr);
1782 for (i = vec_size(block->instr)-1; i > iid; --i)
1783 block->instr[i] = block->instr[i-1];
1784 block->instr[i] = instr;
1789 static bool ir_block_naive_phi(ir_block *self)
1792 /* FIXME: optionally, create_phi can add the phis
1793 * to a list so we don't need to loop through blocks
1794 * - anyway: "don't optimize YET"
1796 for (i = 0; i < vec_size(self->instr); ++i)
1798 ir_instr *instr = self->instr[i];
1799 if (instr->opcode != VINSTR_PHI)
1802 vec_remove(self->instr, i, 1);
1803 --i; /* NOTE: i+1 below */
1805 for (p = 0; p < vec_size(instr->phi); ++p)
1807 ir_value *v = instr->phi[p].value;
1808 ir_block *b = instr->phi[p].from;
1810 if (v->store == store_value &&
1811 vec_size(v->reads) == 1 &&
1812 vec_size(v->writes) == 1)
1814 /* replace the value */
1815 ir_instr_op(v->writes[0], 0, instr->_ops[0], true);
1819 /* force a move instruction */
1820 ir_instr *prevjump = vec_last(b->instr);
1823 instr->_ops[0]->store = store_global;
1824 if (!ir_block_create_store(b, instr->_ops[0], v))
1826 instr->_ops[0]->store = store_value;
1827 vec_push(b->instr, prevjump);
1832 ir_value *v = instr->phi[p].value;
1833 for (w = 0; w < vec_size(v->writes); ++w) {
1836 if (!v->writes[w]->_ops[0])
1839 /* When the write was to a global, we have to emit a mov */
1840 old = v->writes[w]->_ops[0];
1842 /* The original instruction now writes to the PHI target local */
1843 if (v->writes[w]->_ops[0] == v)
1844 v->writes[w]->_ops[0] = instr->_ops[0];
1846 if (old->store != store_value && old->store != store_local && old->store != store_param)
1848 /* If it originally wrote to a global we need to store the value
1851 if (!ir_naive_phi_emit_store(self, i+1, old, v))
1853 if (i+1 < vec_size(self->instr))
1854 instr = self->instr[i+1];
1857 /* In case I forget and access instr later, it'll be NULL
1858 * when it's a problem, to make sure we crash, rather than accessing
1864 /* If it didn't, we can replace all reads by the phi target now. */
1866 for (r = 0; r < vec_size(old->reads); ++r)
1869 ir_instr *ri = old->reads[r];
1870 for (op = 0; op < vec_size(ri->phi); ++op) {
1871 if (ri->phi[op].value == old)
1872 ri->phi[op].value = v;
1874 for (op = 0; op < 3; ++op) {
1875 if (ri->_ops[op] == old)
1883 ir_instr_delete(instr);
1888 /***********************************************************************
1889 *IR Temp allocation code
1890 * Propagating value life ranges by walking through the function backwards
1891 * until no more changes are made.
1892 * In theory this should happen once more than once for every nested loop
1894 * Though this implementation might run an additional time for if nests.
1897 /* Enumerate instructions used by value's life-ranges
1899 static void ir_block_enumerate(ir_block *self, size_t *_eid)
1903 for (i = 0; i < vec_size(self->instr); ++i)
1905 self->instr[i]->eid = eid++;
1910 /* Enumerate blocks and instructions.
1911 * The block-enumeration is unordered!
1912 * We do not really use the block enumreation, however
1913 * the instruction enumeration is important for life-ranges.
1915 void ir_function_enumerate(ir_function *self)
1918 size_t instruction_id = 0;
1919 for (i = 0; i < vec_size(self->blocks); ++i)
1921 self->blocks[i]->eid = i;
1922 self->blocks[i]->run_id = 0;
1923 ir_block_enumerate(self->blocks[i], &instruction_id);
1927 static bool ir_block_life_propagate(ir_block *b, ir_block *prev, bool *changed);
1928 bool ir_function_calculate_liferanges(ir_function *self)
1936 for (i = 0; i != vec_size(self->blocks); ++i)
1938 if (self->blocks[i]->is_return)
1940 vec_free(self->blocks[i]->living);
1941 if (!ir_block_life_propagate(self->blocks[i], NULL, &changed))
1946 if (vec_size(self->blocks)) {
1947 ir_block *block = self->blocks[0];
1948 for (i = 0; i < vec_size(block->living); ++i) {
1949 ir_value *v = block->living[i];
1950 if (v->memberof || v->store != store_local)
1952 if (irwarning(v->context, WARN_USED_UNINITIALIZED,
1953 "variable `%s` may be used uninitialized in this function", v->name))
1962 /* Local-value allocator
1963 * After finishing creating the liferange of all values used in a function
1964 * we can allocate their global-positions.
1965 * This is the counterpart to register-allocation in register machines.
1971 } function_allocator;
1973 static bool function_allocator_alloc(function_allocator *alloc, const ir_value *var)
1976 size_t vsize = type_sizeof[var->vtype];
1978 slot = ir_value_var("reg", store_global, var->vtype);
1982 if (!ir_value_life_merge_into(slot, var))
1985 vec_push(alloc->locals, slot);
1986 vec_push(alloc->sizes, vsize);
1991 ir_value_delete(slot);
1995 bool ir_function_allocate_locals(ir_function *self)
2004 function_allocator alloc;
2006 if (!vec_size(self->locals) && !vec_size(self->values))
2009 alloc.locals = NULL;
2011 alloc.positions = NULL;
2013 for (i = 0; i < vec_size(self->locals); ++i)
2015 if (!function_allocator_alloc(&alloc, self->locals[i]))
2019 /* Allocate a slot for any value that still exists */
2020 for (i = 0; i < vec_size(self->values); ++i)
2022 v = self->values[i];
2024 if (!vec_size(v->life))
2027 for (a = 0; a < vec_size(alloc.locals); ++a)
2029 slot = alloc.locals[a];
2031 if (ir_values_overlap(v, slot))
2034 if (!ir_value_life_merge_into(slot, v))
2037 /* adjust size for this slot */
2038 if (alloc.sizes[a] < type_sizeof[v->vtype])
2039 alloc.sizes[a] = type_sizeof[v->vtype];
2041 self->values[i]->code.local = a;
2044 if (a >= vec_size(alloc.locals)) {
2045 self->values[i]->code.local = vec_size(alloc.locals);
2046 if (!function_allocator_alloc(&alloc, v))
2055 /* Adjust slot positions based on sizes */
2056 vec_push(alloc.positions, 0);
2058 if (vec_size(alloc.sizes))
2059 pos = alloc.positions[0] + alloc.sizes[0];
2062 for (i = 1; i < vec_size(alloc.sizes); ++i)
2064 pos = alloc.positions[i-1] + alloc.sizes[i-1];
2065 vec_push(alloc.positions, pos);
2068 self->allocated_locals = pos + vec_last(alloc.sizes);
2070 /* Take over the actual slot positions */
2071 for (i = 0; i < vec_size(self->values); ++i) {
2072 self->values[i]->code.local = alloc.positions[self->values[i]->code.local];
2080 for (i = 0; i < vec_size(alloc.locals); ++i)
2081 ir_value_delete(alloc.locals[i]);
2082 vec_free(alloc.locals);
2083 vec_free(alloc.sizes);
2084 vec_free(alloc.positions);
2088 /* Get information about which operand
2089 * is read from, or written to.
2091 static void ir_op_read_write(int op, size_t *read, size_t *write)
2111 case INSTR_STOREP_F:
2112 case INSTR_STOREP_V:
2113 case INSTR_STOREP_S:
2114 case INSTR_STOREP_ENT:
2115 case INSTR_STOREP_FLD:
2116 case INSTR_STOREP_FNC:
2127 static bool ir_block_living_add_instr(ir_block *self, size_t eid)
2130 bool changed = false;
2132 for (i = 0; i != vec_size(self->living); ++i)
2134 tempbool = ir_value_life_merge(self->living[i], eid);
2137 irerror(self->context, "block_living_add_instr() value instruction added %s: %i", self->living[i]->_name, (int)eid);
2139 changed = changed || tempbool;
2144 static bool ir_block_life_prop_previous(ir_block* self, ir_block *prev, bool *changed)
2147 /* values which have been read in a previous iteration are now
2148 * in the "living" array even if the previous block doesn't use them.
2149 * So we have to remove whatever does not exist in the previous block.
2150 * They will be re-added on-read, but the liferange merge won't cause
2153 for (i = 0; i < vec_size(self->living); ++i)
2155 if (!vec_ir_value_find(prev->living, self->living[i], NULL)) {
2156 vec_remove(self->living, i, 1);
2161 /* Whatever the previous block still has in its living set
2162 * must now be added to ours as well.
2164 for (i = 0; i < vec_size(prev->living); ++i)
2166 if (vec_ir_value_find(self->living, prev->living[i], NULL))
2168 vec_push(self->living, prev->living[i]);
2170 irerror(self->contextt from prev: %s", self->label, prev->living[i]->_name);
2176 static bool ir_block_life_propagate(ir_block *self, ir_block *prev, bool *changed)
2182 /* bitmasks which operands are read from or written to */
2184 char dbg_ind[16] = { '#', '0' };
2189 if (!ir_block_life_prop_previous(self, prev, changed))
2193 i = vec_size(self->instr);
2196 instr = self->instr[i];
2198 /* PHI operands are always read operands */
2199 for (p = 0; p < vec_size(instr->phi); ++p)
2201 value = instr->phi[p].value;
2202 if (value->memberof)
2203 value = value->memberof;
2204 if (!vec_ir_value_find(self->living, value, NULL))
2205 vec_push(self->living, value);
2208 /* call params are read operands too */
2209 for (p = 0; p < vec_size(instr->params); ++p)
2211 value = instr->params[p];
2212 if (value->memberof)
2213 value = value->memberof;
2214 if (!vec_ir_value_find(self->living, value, NULL))
2215 vec_push(self->living, value);
2218 /* See which operands are read and write operands */
2219 ir_op_read_write(instr->opcode, &read, &write);
2221 if (instr->opcode == INSTR_MUL_VF)
2223 /* the float source will get an additional lifetime */
2224 tempbool = ir_value_life_merge(instr->_ops[2], instr->eid+1);
2225 *changed = *changed || tempbool;
2227 else if (instr->opcode == INSTR_MUL_FV)
2229 /* the float source will get an additional lifetime */
2230 tempbool = ir_value_life_merge(instr->_ops[1], instr->eid+1);
2231 *changed = *changed || tempbool;
2234 /* Go through the 3 main operands */
2235 for (o = 0; o < 3; ++o)
2237 if (!instr->_ops[o]) /* no such operand */
2240 value = instr->_ops[o];
2241 if (value->memberof)
2242 value = value->memberof;
2244 /* We only care about locals */
2245 /* we also calculate parameter liferanges so that locals
2246 * can take up parameter slots */
2247 if (value->store != store_value &&
2248 value->store != store_local &&
2249 value->store != store_param)
2255 if (!vec_ir_value_find(self->living, value, NULL))
2256 vec_push(self->living, value);
2259 /* write operands */
2260 /* When we write to a local, we consider it "dead" for the
2261 * remaining upper part of the function, since in SSA a value
2262 * can only be written once (== created)
2267 bool in_living = vec_ir_value_find(self->living, value, &idx);
2270 /* If the value isn't alive it hasn't been read before... */
2271 /* TODO: See if the warning can be emitted during parsing or AST processing
2272 * otherwise have warning printed here.
2273 * IF printing a warning here: include filecontext_t,
2274 * and make sure it's only printed once
2275 * since this function is run multiple times.
2277 /* For now: debug info: */
2278 /* con_err( "Value only written %s\n", value->name); */
2279 tempbool = ir_value_life_merge(value, instr->eid);
2280 *changed = *changed || tempbool;
2282 ir_instr_dump(instr, dbg_ind, printf);
2286 /* since 'living' won't contain it
2287 * anymore, merge the value, since
2290 tempbool = ir_value_life_merge(value, instr->eid);
2293 con_err( "value added id %s %i\n", value->name, (int)instr->eid);
2295 *changed = *changed || tempbool;
2297 vec_remove(self->living, idx, 1);
2302 tempbool = ir_block_living_add_instr(self, instr->eid);
2303 /*con_err( "living added values\n");*/
2304 *changed = *changed || tempbool;
2308 if (self->run_id == self->owner->run_id)
2311 self->run_id = self->owner->run_id;
2313 for (i = 0; i < vec_size(self->entries); ++i)
2315 ir_block *entry = self->entries[i];
2316 ir_block_life_propagate(entry, self, changed);
2322 /***********************************************************************
2325 * Since the IR has the convention of putting 'write' operands
2326 * at the beginning, we have to rotate the operands of instructions
2327 * properly in order to generate valid QCVM code.
2329 * Having destinations at a fixed position is more convenient. In QC
2330 * this is *mostly* OPC, but FTE adds at least 2 instructions which
2331 * read from from OPA, and store to OPB rather than OPC. Which is
2332 * partially the reason why the implementation of these instructions
2333 * in darkplaces has been delayed for so long.
2335 * Breaking conventions is annoying...
2337 static bool ir_builder_gen_global(ir_builder *self, ir_value *global, bool islocal);
2339 static bool gen_global_field(ir_value *global)
2341 if (global->isconst)
2343 ir_value *fld = global->constval.vpointer;
2345 irerror(global->context, "Invalid field constant with no field: %s", global->name);
2349 /* Now, in this case, a relocation would be impossible to code
2350 * since it looks like this:
2351 * .vector v = origin; <- parse error, wtf is 'origin'?
2354 * But we will need a general relocation support later anyway
2355 * for functions... might as well support that here.
2357 if (!fld->code.globaladdr) {
2358 irerror(global->context, "FIXME: Relocation support");
2362 /* copy the field's value */
2363 ir_value_code_setaddr(global, vec_size(code_globals));
2364 vec_push(code_globals, code_globals[fld->code.globaladdr]);
2365 if (global->fieldtype == TYPE_VECTOR) {
2366 vec_push(code_globals, code_globals[fld->code.globaladdr]+1);
2367 vec_push(code_globals, code_globals[fld->code.globaladdr]+2);
2372 ir_value_code_setaddr(global, vec_size(code_globals));
2373 vec_push(code_globals, 0);
2374 if (global->fieldtype == TYPE_VECTOR) {
2375 vec_push(code_globals, 0);
2376 vec_push(code_globals, 0);
2379 if (global->code.globaladdr < 0)
2384 static bool gen_global_pointer(ir_value *global)
2386 if (global->isconst)
2388 ir_value *target = global->constval.vpointer;
2390 irerror(global->context, "Invalid pointer constant: %s", global->name);
2391 /* NULL pointers are pointing to the NULL constant, which also
2392 * sits at address 0, but still has an ir_value for itself.
2397 /* Here, relocations ARE possible - in fteqcc-enhanced-qc:
2398 * void() foo; <- proto
2399 * void() *fooptr = &foo;
2400 * void() foo = { code }
2402 if (!target->code.globaladdr) {
2403 /* FIXME: Check for the constant nullptr ir_value!
2404 * because then code.globaladdr being 0 is valid.
2406 irerror(global->context, "FIXME: Relocation support");
2410 ir_value_code_setaddr(global, vec_size(code_globals));
2411 vec_push(code_globals, target->code.globaladdr);
2415 ir_value_code_setaddr(global, vec_size(code_globals));
2416 vec_push(code_globals, 0);
2418 if (global->code.globaladdr < 0)
2423 static bool gen_blocks_recursive(ir_function *func, ir_block *block)
2425 prog_section_statement stmt;
2434 block->generated = true;
2435 block->code_start = vec_size(code_statements);
2436 for (i = 0; i < vec_size(block->instr); ++i)
2438 instr = block->instr[i];
2440 if (instr->opcode == VINSTR_PHI) {
2441 irerror(block->context, "cannot generate virtual instruction (phi)");
2445 if (instr->opcode == VINSTR_JUMP) {
2446 target = instr->bops[0];
2447 /* for uncoditional jumps, if the target hasn't been generated
2448 * yet, we generate them right here.
2450 if (!target->generated) {
2455 /* otherwise we generate a jump instruction */
2456 stmt.opcode = INSTR_GOTO;
2457 stmt.o1.s1 = (target->code_start) - vec_size(code_statements);
2460 vec_push(code_statements, stmt);
2462 /* no further instructions can be in this block */
2466 if (instr->opcode == VINSTR_COND) {
2467 ontrue = instr->bops[0];
2468 onfalse = instr->bops[1];
2469 /* TODO: have the AST signal which block should
2470 * come first: eg. optimize IFs without ELSE...
2473 stmt.o1.u1 = ir_value_code_addr(instr->_ops[0]);
2477 if (ontrue->generated) {
2478 stmt.opcode = INSTR_IF;
2479 stmt.o2.s1 = (ontrue->code_start) - vec_size(code_statements);
2480 vec_push(code_statements, stmt);
2482 if (onfalse->generated) {
2483 stmt.opcode = INSTR_IFNOT;
2484 stmt.o2.s1 = (onfalse->code_start) - vec_size(code_statements);
2485 vec_push(code_statements, stmt);
2487 if (!ontrue->generated) {
2488 if (onfalse->generated) {
2493 if (!onfalse->generated) {
2494 if (ontrue->generated) {
2499 /* neither ontrue nor onfalse exist */
2500 stmt.opcode = INSTR_IFNOT;
2501 stidx = vec_size(code_statements);
2502 vec_push(code_statements, stmt);
2503 /* on false we jump, so add ontrue-path */
2504 if (!gen_blocks_recursive(func, ontrue))
2506 /* fixup the jump address */
2507 code_statements[stidx].o2.s1 = vec_size(code_statements) - stidx;
2508 /* generate onfalse path */
2509 if (onfalse->generated) {
2510 /* fixup the jump address */
2511 code_statements[stidx].o2.s1 = (onfalse->code_start) - (stidx);
2512 /* may have been generated in the previous recursive call */
2513 stmt.opcode = INSTR_GOTO;
2514 stmt.o1.s1 = (onfalse->code_start) - vec_size(code_statements);
2517 vec_push(code_statements, stmt);
2520 /* if not, generate now */
2525 if (instr->opcode >= INSTR_CALL0 && instr->opcode <= INSTR_CALL8) {
2526 /* Trivial call translation:
2527 * copy all params to OFS_PARM*
2528 * if the output's storetype is not store_return,
2529 * add append a STORE instruction!
2531 * NOTES on how to do it better without much trouble:
2532 * -) The liferanges!
2533 * Simply check the liferange of all parameters for
2534 * other CALLs. For each param with no CALL in its
2535 * liferange, we can store it in an OFS_PARM at
2536 * generation already. This would even include later
2537 * reuse.... probably... :)
2542 first = vec_size(instr->params);
2545 for (p = 0; p < first; ++p)
2547 ir_value *param = instr->params[p];
2549 stmt.opcode = INSTR_STORE_F;
2552 if (param->vtype == TYPE_FIELD)
2553 stmt.opcode = field_store_instr[param->fieldtype];
2555 stmt.opcode = type_store_instr[param->vtype];
2556 stmt.o1.u1 = ir_value_code_addr(param);
2557 stmt.o2.u1 = OFS_PARM0 + 3 * p;
2558 vec_push(code_statements, stmt);
2560 /* No whandle extparams */
2561 first = vec_size(instr->params);
2562 for (; p < first; ++p)
2564 ir_builder *ir = func->owner;
2565 ir_value *param = instr->params[p];
2568 if (p-8 >= vec_size(ir->extparams)) {
2569 irerror(instr->context, "Not enough extparam-globals have been created");
2573 target = ir->extparams[p-8];
2575 stmt.opcode = INSTR_STORE_F;
2578 if (param->vtype == TYPE_FIELD)
2579 stmt.opcode = field_store_instr[param->fieldtype];
2581 stmt.opcode = type_store_instr[param->vtype];
2582 stmt.o1.u1 = ir_value_code_addr(param);
2583 stmt.o2.u1 = ir_value_code_addr(target);
2584 vec_push(code_statements, stmt);
2587 stmt.opcode = INSTR_CALL0 + vec_size(instr->params);
2588 if (stmt.opcode > INSTR_CALL8)
2589 stmt.opcode = INSTR_CALL8;
2590 stmt.o1.u1 = ir_value_code_addr(instr->_ops[1]);
2593 vec_push(code_statements, stmt);
2595 retvalue = instr->_ops[0];
2596 if (retvalue && retvalue->store != store_return && vec_size(retvalue->life))
2598 /* not to be kept in OFS_RETURN */
2599 if (retvalue->vtype == TYPE_FIELD)
2600 stmt.opcode = field_store_instr[retvalue->vtype];
2602 stmt.opcode = type_store_instr[retvalue->vtype];
2603 stmt.o1.u1 = OFS_RETURN;
2604 stmt.o2.u1 = ir_value_code_addr(retvalue);
2606 vec_push(code_statements, stmt);
2611 if (instr->opcode == INSTR_STATE) {
2612 irerror(block->context, "TODO: state instruction");
2616 stmt.opcode = instr->opcode;
2621 /* This is the general order of operands */
2623 stmt.o3.u1 = ir_value_code_addr(instr->_ops[0]);
2626 stmt.o1.u1 = ir_value_code_addr(instr->_ops[1]);
2629 stmt.o2.u1 = ir_value_code_addr(instr->_ops[2]);
2631 if (stmt.opcode == INSTR_RETURN || stmt.opcode == INSTR_DONE)
2633 stmt.o1.u1 = stmt.o3.u1;
2636 else if ((stmt.opcode >= INSTR_STORE_F &&
2637 stmt.opcode <= INSTR_STORE_FNC) ||
2638 (stmt.opcode >= INSTR_STOREP_F &&
2639 stmt.opcode <= INSTR_STOREP_FNC))
2641 /* 2-operand instructions with A -> B */
2642 stmt.o2.u1 = stmt.o3.u1;
2646 vec_push(code_statements, stmt);
2651 static bool gen_function_code(ir_function *self)
2654 prog_section_statement stmt;
2656 /* Starting from entry point, we generate blocks "as they come"
2657 * for now. Dead blocks will not be translated obviously.
2659 if (!vec_size(self->blocks)) {
2660 irerror(self->context, "Function '%s' declared without body.", self->name);
2664 block = self->blocks[0];
2665 if (block->generated)
2668 if (!gen_blocks_recursive(self, block)) {
2669 irerror(self->context, "failed to generate blocks for '%s'", self->name);
2673 /* otherwise code_write crashes since it debug-prints functions until AINSTR_END */
2674 stmt.opcode = AINSTR_END;
2678 vec_push(code_statements, stmt);
2682 static qcint ir_builder_filestring(ir_builder *ir, const char *filename)
2684 /* NOTE: filename pointers are copied, we never strdup them,
2685 * thus we can use pointer-comparison to find the string.
2690 for (i = 0; i < vec_size(ir->filenames); ++i) {
2691 if (ir->filenames[i] == filename)
2692 return ir->filestrings[i];
2695 str = code_genstring(filename);
2696 vec_push(ir->filenames, filename);
2697 vec_push(ir->filestrings, str);
2701 static bool gen_global_function(ir_builder *ir, ir_value *global)
2703 prog_section_function fun;
2707 size_t local_var_end;
2709 if (!global->isconst || (!global->constval.vfunc))
2711 irerror(global->context, "Invalid state of function-global: not constant: %s", global->name);
2715 irfun = global->constval.vfunc;
2717 fun.name = global->code.name;
2718 fun.file = ir_builder_filestring(ir, global->context.file);
2719 fun.profile = 0; /* always 0 */
2720 fun.nargs = vec_size(irfun->params);
2724 for (i = 0;i < 8; ++i) {
2728 fun.argsize[i] = type_sizeof[irfun->params[i]];
2731 fun.firstlocal = vec_size(code_globals);
2733 local_var_end = fun.firstlocal;
2734 for (i = 0; i < vec_size(irfun->locals); ++i) {
2735 if (!ir_builder_gen_global(ir, irfun->locals[i], true)) {
2736 irerror(irfun->locals[i]->context, "Failed to generate local %s", irfun->locals[i]->name);
2740 if (vec_size(irfun->locals)) {
2741 ir_value *last = vec_last(irfun->locals);
2742 local_var_end = last->code.globaladdr;
2743 local_var_end += type_sizeof[last->vtype];
2745 for (i = 0; i < vec_size(irfun->values); ++i)
2747 /* generate code.globaladdr for ssa values */
2748 ir_value *v = irfun->values[i];
2749 ir_value_code_setaddr(v, local_var_end + v->code.local);
2751 for (i = 0; i < irfun->allocated_locals; ++i) {
2752 /* fill the locals with zeros */
2753 vec_push(code_globals, 0);
2756 fun.locals = vec_size(code_globals) - fun.firstlocal;
2759 fun.entry = irfun->builtin;
2761 irfun->code_function_def = vec_size(code_functions);
2762 fun.entry = vec_size(code_statements);
2765 vec_push(code_functions, fun);
2769 static void ir_gen_extparam(ir_builder *ir)
2771 prog_section_def def;
2775 snprintf(name, sizeof(name), "EXTPARM#%i", (int)(vec_size(ir->extparams)+8));
2776 global = ir_value_var(name, store_global, TYPE_VECTOR);
2778 def.name = code_genstring(name);
2779 def.type = TYPE_VECTOR;
2780 def.offset = vec_size(code_globals);
2782 vec_push(code_defs, def);
2783 ir_value_code_setaddr(global, def.offset);
2784 vec_push(code_globals, 0);
2785 vec_push(code_globals, 0);
2786 vec_push(code_globals, 0);
2788 vec_push(ir->extparams, global);
2791 static bool gen_function_extparam_copy(ir_function *self)
2793 size_t i, ext, numparams;
2795 ir_builder *ir = self->owner;
2797 prog_section_statement stmt;
2799 numparams = vec_size(self->params);
2803 stmt.opcode = INSTR_STORE_F;
2805 for (i = 8; i < numparams; ++i) {
2807 if (ext >= vec_size(ir->extparams))
2808 ir_gen_extparam(ir);
2810 ep = ir->extparams[ext];
2812 stmt.opcode = type_store_instr[self->locals[i]->vtype];
2813 if (self->locals[i]->vtype == TYPE_FIELD &&
2814 self->locals[i]->fieldtype == TYPE_VECTOR)
2816 stmt.opcode = INSTR_STORE_V;
2818 stmt.o1.u1 = ir_value_code_addr(ep);
2819 stmt.o2.u1 = ir_value_code_addr(self->locals[i]);
2820 vec_push(code_statements, stmt);
2826 static bool gen_global_function_code(ir_builder *ir, ir_value *global)
2828 prog_section_function *fundef;
2831 irfun = global->constval.vfunc;
2833 irwarning(global->context, WARN_IMPLICIT_FUNCTION_POINTER,
2834 "function `%s` has no body and in QC implicitly becomes a function-pointer", global->name);
2835 /* this was a function pointer, don't generate code for those */
2842 if (irfun->code_function_def < 0) {
2843 irerror(irfun->context, "`%s`: IR global wasn't generated, failed to access function-def", irfun->name);
2846 fundef = &code_functions[irfun->code_function_def];
2848 fundef->entry = vec_size(code_statements);
2849 if (!gen_function_extparam_copy(irfun)) {
2850 irerror(irfun->context, "Failed to generate extparam-copy code for function %s", irfun->name);
2853 if (!gen_function_code(irfun)) {
2854 irerror(irfun->context, "Failed to generate code for function %s", irfun->name);
2860 static bool ir_builder_gen_global(ir_builder *self, ir_value *global, bool islocal)
2864 prog_section_def def;
2866 def.type = global->vtype;
2867 def.offset = vec_size(code_globals);
2870 if (global->name[0] == '#') {
2871 if (!self->str_immediate)
2872 self->str_immediate = code_genstring("IMMEDIATE");
2873 def.name = global->code.name = self->str_immediate;
2876 def.name = global->code.name = code_genstring(global->name);
2881 switch (global->vtype)
2884 if (!strcmp(global->name, "end_sys_globals")) {
2885 /* TODO: remember this point... all the defs before this one
2886 * should be checksummed and added to progdefs.h when we generate it.
2889 else if (!strcmp(global->name, "end_sys_fields")) {
2890 /* TODO: same as above but for entity-fields rather than globsl
2894 irwarning(global->context, WARN_VOID_VARIABLES, "unrecognized variable of type void `%s`",
2896 /* I'd argue setting it to 0 is sufficient, but maybe some depend on knowing how far
2897 * the system fields actually go? Though the engine knows this anyway...
2898 * Maybe this could be an -foption
2899 * fteqcc creates data for end_sys_* - of size 1, so let's do the same
2901 ir_value_code_setaddr(global, vec_size(code_globals));
2902 vec_push(code_globals, 0);
2904 vec_push(code_defs, def);
2907 vec_push(code_defs, def);
2908 return gen_global_pointer(global);
2910 vec_push(code_defs, def);
2911 return gen_global_field(global);
2916 ir_value_code_setaddr(global, vec_size(code_globals));
2917 if (global->isconst) {
2918 iptr = (int32_t*)&global->constval.ivec[0];
2919 vec_push(code_globals, *iptr);
2921 vec_push(code_globals, 0);
2923 def.type |= DEF_SAVEGLOBAL;
2925 vec_push(code_defs, def);
2927 return global->code.globaladdr >= 0;
2931 ir_value_code_setaddr(global, vec_size(code_globals));
2932 if (global->isconst) {
2933 vec_push(code_globals, code_genstring(global->constval.vstring));
2935 vec_push(code_globals, 0);
2937 def.type |= DEF_SAVEGLOBAL;
2939 vec_push(code_defs, def);
2940 return global->code.globaladdr >= 0;
2945 ir_value_code_setaddr(global, vec_size(code_globals));
2946 if (global->isconst) {
2947 iptr = (int32_t*)&global->constval.ivec[0];
2948 vec_push(code_globals, iptr[0]);
2949 if (global->code.globaladdr < 0)
2951 for (d = 1; d < type_sizeof[global->vtype]; ++d)
2953 vec_push(code_globals, iptr[d]);
2956 vec_push(code_globals, 0);
2957 if (global->code.globaladdr < 0)
2959 for (d = 1; d < type_sizeof[global->vtype]; ++d)
2961 vec_push(code_globals, 0);
2964 def.type |= DEF_SAVEGLOBAL;
2967 vec_push(code_defs, def);
2968 return global->code.globaladdr >= 0;
2971 ir_value_code_setaddr(global, vec_size(code_globals));
2972 if (!global->isconst) {
2973 vec_push(code_globals, 0);
2974 if (global->code.globaladdr < 0)
2977 vec_push(code_globals, vec_size(code_functions));
2978 if (!gen_global_function(self, global))
2981 def.type |= DEF_SAVEGLOBAL;
2983 vec_push(code_defs, def);
2986 /* assume biggest type */
2987 ir_value_code_setaddr(global, vec_size(code_globals));
2988 vec_push(code_globals, 0);
2989 for (i = 1; i < type_sizeof[TYPE_VARIANT]; ++i)
2990 vec_push(code_globals, 0);
2993 /* refuse to create 'void' type or any other fancy business. */
2994 irerror(global->context, "Invalid type for global variable `%s`: %s",
2995 global->name, type_name[global->vtype]);
3000 static bool ir_builder_gen_field(ir_builder *self, ir_value *field)
3002 prog_section_def def;
3003 prog_section_field fld;
3005 def.type = field->vtype;
3006 def.offset = vec_size(code_globals);
3008 /* create a global named the same as the field */
3009 if (opts_standard == COMPILER_GMQCC) {
3010 /* in our standard, the global gets a dot prefix */
3011 size_t len = strlen(field->name);
3014 /* we really don't want to have to allocate this, and 1024
3015 * bytes is more than enough for a variable/field name
3017 if (len+2 >= sizeof(name)) {
3018 irerror(field->context, "invalid field name size: %u", (unsigned int)len);
3023 memcpy(name+1, field->name, len); /* no strncpy - we used strlen above */
3026 def.name = code_genstring(name);
3027 fld.name = def.name + 1; /* we reuse that string table entry */
3029 /* in plain QC, there cannot be a global with the same name,
3030 * and so we also name the global the same.
3031 * FIXME: fteqcc should create a global as well
3032 * check if it actually uses the same name. Probably does
3034 def.name = code_genstring(field->name);
3035 fld.name = def.name;
3038 field->code.name = def.name;
3040 vec_push(code_defs, def);
3042 fld.type = field->fieldtype;
3044 if (fld.type == TYPE_VOID) {
3045 irerror(field->context, "field is missing a type: %s - don't know its size", field->name);
3049 fld.offset = code_alloc_field(type_sizeof[field->fieldtype]);
3051 vec_push(code_fields, fld);
3053 ir_value_code_setaddr(field, vec_size(code_globals));
3054 vec_push(code_globals, fld.offset);
3055 if (fld.type == TYPE_VECTOR) {
3056 vec_push(code_globals, fld.offset+1);
3057 vec_push(code_globals, fld.offset+2);
3060 return field->code.globaladdr >= 0;
3063 bool ir_builder_generate(ir_builder *self, const char *filename)
3065 prog_section_statement stmt;
3070 for (i = 0; i < vec_size(self->globals); ++i)
3072 if (!ir_builder_gen_global(self, self->globals[i], false)) {
3077 for (i = 0; i < vec_size(self->fields); ++i)
3079 if (!ir_builder_gen_field(self, self->fields[i])) {
3084 /* generate function code */
3085 for (i = 0; i < vec_size(self->globals); ++i)
3087 if (self->globals[i]->vtype == TYPE_FUNCTION) {
3088 if (!gen_global_function_code(self, self->globals[i])) {
3094 /* DP errors if the last instruction is not an INSTR_DONE
3095 * and for debugging purposes we add an additional AINSTR_END
3096 * to the end of functions, so here it goes:
3098 stmt.opcode = INSTR_DONE;
3102 vec_push(code_statements, stmt);
3105 con_out("writing '%s'...\n", filename);
3106 return code_write(filename);
3109 /***********************************************************************
3110 *IR DEBUG Dump functions...
3113 #define IND_BUFSZ 1024
3116 # define strncat(dst, src, sz) strncat_s(dst, sz, src, _TRUNCATE)
3119 const char *qc_opname(int op)
3121 if (op < 0) return "<INVALID>";
3122 if (op < ( sizeof(asm_instr) / sizeof(asm_instr[0]) ))
3123 return asm_instr[op].m;
3125 case VINSTR_PHI: return "PHI";
3126 case VINSTR_JUMP: return "JUMP";
3127 case VINSTR_COND: return "COND";
3128 default: return "<UNK>";
3132 void ir_builder_dump(ir_builder *b, int (*oprintf)(const char*, ...))
3135 char indent[IND_BUFSZ];
3139 oprintf("module %s\n", b->name);
3140 for (i = 0; i < vec_size(b->globals); ++i)
3143 if (b->globals[i]->isconst)
3144 oprintf("%s = ", b->globals[i]->name);
3145 ir_value_dump(b->globals[i], oprintf);
3148 for (i = 0; i < vec_size(b->functions); ++i)
3149 ir_function_dump(b->functions[i], indent, oprintf);
3150 oprintf("endmodule %s\n", b->name);
3153 void ir_function_dump(ir_function *f, char *ind,
3154 int (*oprintf)(const char*, ...))
3157 if (f->builtin != 0) {
3158 oprintf("%sfunction %s = builtin %i\n", ind, f->name, -f->builtin);
3161 oprintf("%sfunction %s\n", ind, f->name);
3162 strncat(ind, "\t", IND_BUFSZ);
3163 if (vec_size(f->locals))
3165 oprintf("%s%i locals:\n", ind, (int)vec_size(f->locals));
3166 for (i = 0; i < vec_size(f->locals); ++i) {
3167 oprintf("%s\t", ind);
3168 ir_value_dump(f->locals[i], oprintf);
3172 oprintf("%sliferanges:\n", ind);
3173 for (i = 0; i < vec_size(f->locals); ++i) {
3175 ir_value *v = f->locals[i];
3176 oprintf("%s\t%s: unique ", ind, v->name);
3177 for (l = 0; l < vec_size(v->life); ++l) {
3178 oprintf("[%i,%i] ", v->life[l].start, v->life[l].end);
3182 for (i = 0; i < vec_size(f->values); ++i) {
3184 ir_value *v = f->values[i];
3185 oprintf("%s\t%s: @%i ", ind, v->name, (int)v->code.local);
3186 for (l = 0; l < vec_size(v->life); ++l) {
3187 oprintf("[%i,%i] ", v->life[l].start, v->life[l].end);
3191 if (vec_size(f->blocks))
3193 oprintf("%slife passes (check): %i\n", ind, (int)f->run_id);
3194 for (i = 0; i < vec_size(f->blocks); ++i) {
3195 if (f->blocks[i]->run_id != f->run_id) {
3196 oprintf("%slife pass check fail! %i != %i\n", ind, (int)f->blocks[i]->run_id, (int)f->run_id);
3198 ir_block_dump(f->blocks[i], ind, oprintf);
3202 ind[strlen(ind)-1] = 0;
3203 oprintf("%sendfunction %s\n", ind, f->name);
3206 void ir_block_dump(ir_block* b, char *ind,
3207 int (*oprintf)(const char*, ...))
3210 oprintf("%s:%s\n", ind, b->label);
3211 strncat(ind, "\t", IND_BUFSZ);
3213 for (i = 0; i < vec_size(b->instr); ++i)
3214 ir_instr_dump(b->instr[i], ind, oprintf);
3215 ind[strlen(ind)-1] = 0;
3218 void dump_phi(ir_instr *in, char *ind,
3219 int (*oprintf)(const char*, ...))
3222 oprintf("%s <- phi ", in->_ops[0]->name);
3223 for (i = 0; i < vec_size(in->phi); ++i)
3225 oprintf("([%s] : %s) ", in->phi[i].from->label,
3226 in->phi[i].value->name);
3231 void ir_instr_dump(ir_instr *in, char *ind,
3232 int (*oprintf)(const char*, ...))
3235 const char *comma = NULL;
3237 oprintf("%s (%i) ", ind, (int)in->eid);
3239 if (in->opcode == VINSTR_PHI) {
3240 dump_phi(in, ind, oprintf);
3244 strncat(ind, "\t", IND_BUFSZ);
3246 if (in->_ops[0] && (in->_ops[1] || in->_ops[2])) {
3247 ir_value_dump(in->_ops[0], oprintf);
3248 if (in->_ops[1] || in->_ops[2])
3251 if (in->opcode == INSTR_CALL0) {
3252 oprintf("CALL%i\t", vec_size(in->params));
3254 oprintf("%s\t", qc_opname(in->opcode));
3256 if (in->_ops[0] && !(in->_ops[1] || in->_ops[2])) {
3257 ir_value_dump(in->_ops[0], oprintf);
3262 for (i = 1; i != 3; ++i) {
3266 ir_value_dump(in->_ops[i], oprintf);
3274 oprintf("[%s]", in->bops[0]->label);
3278 oprintf("%s[%s]", comma, in->bops[1]->label);
3279 if (vec_size(in->params)) {
3280 oprintf("\tparams: ");
3281 for (i = 0; i != vec_size(in->params); ++i) {
3282 oprintf("%s, ", in->params[i]->name);
3286 ind[strlen(ind)-1] = 0;
3289 void ir_value_dump(ir_value* v, int (*oprintf)(const char*, ...))
3298 oprintf("fn:%s", v->name);
3301 oprintf("%g", v->constval.vfloat);
3304 oprintf("'%g %g %g'",
3307 v->constval.vvec.z);
3310 oprintf("(entity)");
3313 oprintf("\"%s\"", v->constval.vstring);
3317 oprintf("%i", v->constval.vint);
3322 v->constval.vpointer->name);
3326 oprintf("%s", v->name);
3330 void ir_value_dump_life(const ir_value *self, int (*oprintf)(const char*,...))
3333 oprintf("Life of %12s:", self->name);
3334 for (i = 0; i < vec_size(self->life); ++i)
3336 oprintf(" + [%i, %i]\n", self->life[i].start, self->life[i].end);