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 MEM_VEC_FUNCTIONS(ir_value_vector, ir_value*, v)
176 static void irerror(lex_ctx ctx, const char *msg, ...)
180 cvprintmsg(ctx, LVL_ERROR, "internal error", msg, ap);
184 static bool irwarning(lex_ctx ctx, int warntype, const char *fmt, ...)
187 int lvl = LVL_WARNING;
189 if (warntype && !OPTS_WARN(warntype))
196 vprintmsg(lvl, ctx.file, ctx.line, "warning", fmt, ap);
202 /***********************************************************************
206 static void ir_block_delete_quick(ir_block* self);
207 static void ir_instr_delete_quick(ir_instr *self);
208 static void ir_function_delete_quick(ir_function *self);
210 ir_builder* ir_builder_new(const char *modulename)
214 self = (ir_builder*)mem_a(sizeof(*self));
218 MEM_VECTOR_INIT(self, functions);
219 MEM_VECTOR_INIT(self, globals);
220 MEM_VECTOR_INIT(self, fields);
221 MEM_VECTOR_INIT(self, filenames);
222 MEM_VECTOR_INIT(self, filestrings);
223 self->str_immediate = 0;
225 if (!ir_builder_set_name(self, modulename)) {
233 MEM_VEC_FUNCTIONS(ir_builder, ir_value*, globals)
234 MEM_VEC_FUNCTIONS(ir_builder, ir_value*, fields)
235 MEM_VEC_FUNCTIONS(ir_builder, ir_function*, functions)
236 MEM_VEC_FUNCTIONS(ir_builder, const char*, filenames)
237 MEM_VEC_FUNCTIONS(ir_builder, qcint, filestrings)
239 void ir_builder_delete(ir_builder* self)
242 mem_d((void*)self->name);
243 for (i = 0; i != self->functions_count; ++i) {
244 ir_function_delete_quick(self->functions[i]);
246 MEM_VECTOR_CLEAR(self, functions);
247 for (i = 0; i != self->globals_count; ++i) {
248 ir_value_delete(self->globals[i]);
250 MEM_VECTOR_CLEAR(self, globals);
251 for (i = 0; i != self->fields_count; ++i) {
252 ir_value_delete(self->fields[i]);
254 MEM_VECTOR_CLEAR(self, fields);
255 MEM_VECTOR_CLEAR(self, filenames);
256 MEM_VECTOR_CLEAR(self, filestrings);
260 bool ir_builder_set_name(ir_builder *self, const char *name)
263 mem_d((void*)self->name);
264 self->name = util_strdup(name);
268 ir_function* ir_builder_get_function(ir_builder *self, const char *name)
271 for (i = 0; i < self->functions_count; ++i) {
272 if (!strcmp(name, self->functions[i]->name))
273 return self->functions[i];
278 ir_function* ir_builder_create_function(ir_builder *self, const char *name, int outtype)
280 ir_function *fn = ir_builder_get_function(self, name);
285 fn = ir_function_new(self, outtype);
286 if (!ir_function_set_name(fn, name) ||
287 !ir_builder_functions_add(self, fn) )
289 ir_function_delete(fn);
293 fn->value = ir_builder_create_global(self, fn->name, TYPE_FUNCTION);
295 ir_function_delete(fn);
299 fn->value->isconst = true;
300 fn->value->outtype = outtype;
301 fn->value->constval.vfunc = fn;
302 fn->value->context = fn->context;
307 ir_value* ir_builder_get_global(ir_builder *self, const char *name)
310 for (i = 0; i < self->globals_count; ++i) {
311 if (!strcmp(self->globals[i]->name, name))
312 return self->globals[i];
317 ir_value* ir_builder_create_global(ir_builder *self, const char *name, int vtype)
321 if (name && name[0] != '#')
323 ve = ir_builder_get_global(self, name);
329 ve = ir_value_var(name, store_global, vtype);
330 if (!ir_builder_globals_add(self, ve)) {
337 ir_value* ir_builder_get_field(ir_builder *self, const char *name)
340 for (i = 0; i < self->fields_count; ++i) {
341 if (!strcmp(self->fields[i]->name, name))
342 return self->fields[i];
348 ir_value* ir_builder_create_field(ir_builder *self, const char *name, int vtype)
350 ir_value *ve = ir_builder_get_field(self, name);
355 ve = ir_value_var(name, store_global, TYPE_FIELD);
356 ve->fieldtype = vtype;
357 if (!ir_builder_fields_add(self, ve)) {
364 /***********************************************************************
368 bool ir_function_naive_phi(ir_function*);
369 void ir_function_enumerate(ir_function*);
370 bool ir_function_calculate_liferanges(ir_function*);
371 bool ir_function_allocate_locals(ir_function*);
373 ir_function* ir_function_new(ir_builder* owner, int outtype)
376 self = (ir_function*)mem_a(sizeof(*self));
381 memset(self, 0, sizeof(*self));
384 if (!ir_function_set_name(self, "<@unnamed>")) {
389 self->context.file = "<@no context>";
390 self->context.line = 0;
391 self->outtype = outtype;
394 MEM_VECTOR_INIT(self, params);
395 MEM_VECTOR_INIT(self, blocks);
396 MEM_VECTOR_INIT(self, values);
397 MEM_VECTOR_INIT(self, locals);
399 self->code_function_def = -1;
400 self->allocated_locals = 0;
405 MEM_VEC_FUNCTIONS(ir_function, ir_value*, values)
406 MEM_VEC_FUNCTIONS(ir_function, ir_block*, blocks)
407 MEM_VEC_FUNCTIONS(ir_function, ir_value*, locals)
408 MEM_VEC_FUNCTIONS(ir_function, int, params)
410 bool ir_function_set_name(ir_function *self, const char *name)
413 mem_d((void*)self->name);
414 self->name = util_strdup(name);
418 static void ir_function_delete_quick(ir_function *self)
421 mem_d((void*)self->name);
423 for (i = 0; i != self->blocks_count; ++i)
424 ir_block_delete_quick(self->blocks[i]);
425 MEM_VECTOR_CLEAR(self, blocks);
427 MEM_VECTOR_CLEAR(self, params);
429 for (i = 0; i != self->values_count; ++i)
430 ir_value_delete(self->values[i]);
431 MEM_VECTOR_CLEAR(self, values);
433 for (i = 0; i != self->locals_count; ++i)
434 ir_value_delete(self->locals[i]);
435 MEM_VECTOR_CLEAR(self, locals);
437 /* self->value is deleted by the builder */
442 void ir_function_delete(ir_function *self)
445 mem_d((void*)self->name);
447 for (i = 0; i != self->blocks_count; ++i)
448 ir_block_delete(self->blocks[i]);
449 MEM_VECTOR_CLEAR(self, blocks);
451 MEM_VECTOR_CLEAR(self, params);
453 for (i = 0; i != self->values_count; ++i)
454 ir_value_delete(self->values[i]);
455 MEM_VECTOR_CLEAR(self, values);
457 for (i = 0; i != self->locals_count; ++i)
458 ir_value_delete(self->locals[i]);
459 MEM_VECTOR_CLEAR(self, locals);
461 /* self->value is deleted by the builder */
466 bool GMQCC_WARN ir_function_collect_value(ir_function *self, ir_value *v)
468 return ir_function_values_add(self, v);
471 ir_block* ir_function_create_block(ir_function *self, const char *label)
473 ir_block* bn = ir_block_new(self, label);
474 memcpy(&bn->context, &self->context, sizeof(self->context));
475 if (!ir_function_blocks_add(self, bn)) {
482 bool ir_function_finalize(ir_function *self)
487 if (!ir_function_naive_phi(self))
490 ir_function_enumerate(self);
492 if (!ir_function_calculate_liferanges(self))
495 if (!ir_function_allocate_locals(self))
500 ir_value* ir_function_get_local(ir_function *self, const char *name)
503 for (i = 0; i < self->locals_count; ++i) {
504 if (!strcmp(self->locals[i]->name, name))
505 return self->locals[i];
510 ir_value* ir_function_create_local(ir_function *self, const char *name, int vtype, bool param)
515 if (ir_function_get_local(self, name))
520 self->locals_count &&
521 self->locals[self->locals_count-1]->store != store_param) {
522 irerror(self->context, "cannot add parameters after adding locals");
526 ve = ir_value_var(name, (param ? store_param : store_local), vtype);
527 if (!ir_function_locals_add(self, ve)) {
534 /***********************************************************************
538 ir_block* ir_block_new(ir_function* owner, const char *name)
541 self = (ir_block*)mem_a(sizeof(*self));
545 memset(self, 0, sizeof(*self));
548 if (name && !ir_block_set_label(self, name)) {
553 self->context.file = "<@no context>";
554 self->context.line = 0;
556 MEM_VECTOR_INIT(self, instr);
557 MEM_VECTOR_INIT(self, entries);
558 MEM_VECTOR_INIT(self, exits);
561 self->is_return = false;
563 MEM_VECTOR_INIT(self, living);
565 self->generated = false;
569 MEM_VEC_FUNCTIONS(ir_block, ir_instr*, instr)
570 MEM_VEC_FUNCTIONS_ALL(ir_block, ir_block*, entries)
571 MEM_VEC_FUNCTIONS_ALL(ir_block, ir_block*, exits)
572 MEM_VEC_FUNCTIONS_ALL(ir_block, ir_value*, living)
574 static void ir_block_delete_quick(ir_block* self)
577 if (self->label) mem_d(self->label);
578 for (i = 0; i != self->instr_count; ++i)
579 ir_instr_delete_quick(self->instr[i]);
580 MEM_VECTOR_CLEAR(self, instr);
581 MEM_VECTOR_CLEAR(self, entries);
582 MEM_VECTOR_CLEAR(self, exits);
583 MEM_VECTOR_CLEAR(self, living);
587 void ir_block_delete(ir_block* self)
590 if (self->label) mem_d(self->label);
591 for (i = 0; i != self->instr_count; ++i)
592 ir_instr_delete(self->instr[i]);
593 MEM_VECTOR_CLEAR(self, instr);
594 MEM_VECTOR_CLEAR(self, entries);
595 MEM_VECTOR_CLEAR(self, exits);
596 MEM_VECTOR_CLEAR(self, living);
600 bool ir_block_set_label(ir_block *self, const char *name)
603 mem_d((void*)self->label);
604 self->label = util_strdup(name);
605 return !!self->label;
608 /***********************************************************************
612 ir_instr* ir_instr_new(ir_block* owner, int op)
615 self = (ir_instr*)mem_a(sizeof(*self));
620 self->context.file = "<@no context>";
621 self->context.line = 0;
623 self->_ops[0] = NULL;
624 self->_ops[1] = NULL;
625 self->_ops[2] = NULL;
626 self->bops[0] = NULL;
627 self->bops[1] = NULL;
628 MEM_VECTOR_INIT(self, phi);
629 MEM_VECTOR_INIT(self, params);
634 MEM_VEC_FUNCTIONS(ir_instr, ir_phi_entry_t, phi)
635 MEM_VEC_FUNCTIONS(ir_instr, ir_value*, params)
637 static void ir_instr_delete_quick(ir_instr *self)
639 MEM_VECTOR_CLEAR(self, phi);
640 MEM_VECTOR_CLEAR(self, params);
644 void ir_instr_delete(ir_instr *self)
647 /* The following calls can only delete from
648 * vectors, we still want to delete this instruction
649 * so ignore the return value. Since with the warn_unused_result attribute
650 * gcc doesn't care about an explicit: (void)foo(); to ignore the result,
651 * I have to improvise here and use if(foo());
653 for (i = 0; i < self->phi_count; ++i) {
655 if (ir_value_writes_find(self->phi[i].value, self, &idx))
656 if (ir_value_writes_remove(self->phi[i].value, idx)) GMQCC_SUPPRESS_EMPTY_BODY;
657 if (ir_value_reads_find(self->phi[i].value, self, &idx))
658 if (ir_value_reads_remove (self->phi[i].value, idx)) GMQCC_SUPPRESS_EMPTY_BODY;
660 MEM_VECTOR_CLEAR(self, phi);
661 for (i = 0; i < self->params_count; ++i) {
663 if (ir_value_writes_find(self->params[i], self, &idx))
664 if (ir_value_writes_remove(self->params[i], idx)) GMQCC_SUPPRESS_EMPTY_BODY;
665 if (ir_value_reads_find(self->params[i], self, &idx))
666 if (ir_value_reads_remove (self->params[i], idx)) GMQCC_SUPPRESS_EMPTY_BODY;
668 MEM_VECTOR_CLEAR(self, params);
669 if (ir_instr_op(self, 0, NULL, false)) GMQCC_SUPPRESS_EMPTY_BODY;
670 if (ir_instr_op(self, 1, NULL, false)) GMQCC_SUPPRESS_EMPTY_BODY;
671 if (ir_instr_op(self, 2, NULL, false)) GMQCC_SUPPRESS_EMPTY_BODY;
675 bool ir_instr_op(ir_instr *self, int op, ir_value *v, bool writing)
677 if (self->_ops[op]) {
679 if (writing && ir_value_writes_find(self->_ops[op], self, &idx))
681 if (!ir_value_writes_remove(self->_ops[op], idx))
684 else if (ir_value_reads_find(self->_ops[op], self, &idx))
686 if (!ir_value_reads_remove(self->_ops[op], idx))
692 if (!ir_value_writes_add(v, self))
695 if (!ir_value_reads_add(v, self))
703 /***********************************************************************
707 void ir_value_code_setaddr(ir_value *self, int32_t gaddr)
709 self->code.globaladdr = gaddr;
710 if (self->members[0]) self->members[0]->code.globaladdr = gaddr;
711 if (self->members[1]) self->members[1]->code.globaladdr = gaddr;
712 if (self->members[2]) self->members[2]->code.globaladdr = gaddr;
715 int32_t ir_value_code_addr(const ir_value *self)
717 if (self->store == store_return)
718 return OFS_RETURN + self->code.addroffset;
719 return self->code.globaladdr + self->code.addroffset;
722 ir_value* ir_value_var(const char *name, int storetype, int vtype)
725 self = (ir_value*)mem_a(sizeof(*self));
727 self->fieldtype = TYPE_VOID;
728 self->outtype = TYPE_VOID;
729 self->store = storetype;
730 MEM_VECTOR_INIT(self, reads);
731 MEM_VECTOR_INIT(self, writes);
732 self->isconst = false;
733 self->context.file = "<@no context>";
734 self->context.line = 0;
736 if (name && !ir_value_set_name(self, name)) {
737 irerror(self->context, "out of memory");
742 memset(&self->constval, 0, sizeof(self->constval));
743 memset(&self->code, 0, sizeof(self->code));
745 self->members[0] = NULL;
746 self->members[1] = NULL;
747 self->members[2] = NULL;
748 self->memberof = NULL;
750 MEM_VECTOR_INIT(self, life);
754 ir_value* ir_value_vector_member(ir_value *self, unsigned int member)
760 if (self->members[member])
761 return self->members[member];
763 if (self->vtype == TYPE_VECTOR)
765 m = ir_value_var(self->name, self->store, TYPE_FLOAT);
768 m->context = self->context;
770 self->members[member] = m;
771 m->code.addroffset = member;
773 else if (self->vtype == TYPE_FIELD)
775 if (self->fieldtype != TYPE_VECTOR)
777 m = ir_value_var(self->name, self->store, TYPE_FIELD);
780 m->fieldtype = TYPE_FLOAT;
781 m->context = self->context;
783 self->members[member] = m;
784 m->code.addroffset = member;
788 irerror(self->context, "invalid member access on %s", self->name);
796 MEM_VEC_FUNCTIONS(ir_value, ir_life_entry_t, life)
797 MEM_VEC_FUNCTIONS_ALL(ir_value, ir_instr*, reads)
798 MEM_VEC_FUNCTIONS_ALL(ir_value, ir_instr*, writes)
800 ir_value* ir_value_out(ir_function *owner, const char *name, int storetype, int vtype)
802 ir_value *v = ir_value_var(name, storetype, vtype);
805 if (!ir_function_collect_value(owner, v))
813 void ir_value_delete(ir_value* self)
817 mem_d((void*)self->name);
820 if (self->vtype == TYPE_STRING)
821 mem_d((void*)self->constval.vstring);
823 for (i = 0; i < 3; ++i) {
824 if (self->members[i])
825 ir_value_delete(self->members[i]);
827 MEM_VECTOR_CLEAR(self, reads);
828 MEM_VECTOR_CLEAR(self, writes);
829 MEM_VECTOR_CLEAR(self, life);
833 bool ir_value_set_name(ir_value *self, const char *name)
836 mem_d((void*)self->name);
837 self->name = util_strdup(name);
841 bool ir_value_set_float(ir_value *self, float f)
843 if (self->vtype != TYPE_FLOAT)
845 self->constval.vfloat = f;
846 self->isconst = true;
850 bool ir_value_set_func(ir_value *self, int f)
852 if (self->vtype != TYPE_FUNCTION)
854 self->constval.vint = f;
855 self->isconst = true;
859 bool ir_value_set_vector(ir_value *self, vector v)
861 if (self->vtype != TYPE_VECTOR)
863 self->constval.vvec = v;
864 self->isconst = true;
868 bool ir_value_set_field(ir_value *self, ir_value *fld)
870 if (self->vtype != TYPE_FIELD)
872 self->constval.vpointer = fld;
873 self->isconst = true;
877 static char *ir_strdup(const char *str)
880 /* actually dup empty strings */
881 char *out = mem_a(1);
885 return util_strdup(str);
888 bool ir_value_set_string(ir_value *self, const char *str)
890 if (self->vtype != TYPE_STRING)
892 self->constval.vstring = ir_strdup(str);
893 self->isconst = true;
898 bool ir_value_set_int(ir_value *self, int i)
900 if (self->vtype != TYPE_INTEGER)
902 self->constval.vint = i;
903 self->isconst = true;
908 bool ir_value_lives(ir_value *self, size_t at)
911 for (i = 0; i < self->life_count; ++i)
913 ir_life_entry_t *life = &self->life[i];
914 if (life->start <= at && at <= life->end)
916 if (life->start > at) /* since it's ordered */
922 bool ir_value_life_insert(ir_value *self, size_t idx, ir_life_entry_t e)
925 if (!ir_value_life_add(self, e)) /* naive... */
927 for (k = self->life_count-1; k > idx; --k)
928 self->life[k] = self->life[k-1];
933 bool ir_value_life_merge(ir_value *self, size_t s)
936 ir_life_entry_t *life = NULL;
937 ir_life_entry_t *before = NULL;
938 ir_life_entry_t new_entry;
940 /* Find the first range >= s */
941 for (i = 0; i < self->life_count; ++i)
944 life = &self->life[i];
948 /* nothing found? append */
949 if (i == self->life_count) {
951 if (life && life->end+1 == s)
953 /* previous life range can be merged in */
957 if (life && life->end >= s)
960 if (!ir_value_life_add(self, e))
961 return false; /* failing */
967 if (before->end + 1 == s &&
968 life->start - 1 == s)
971 before->end = life->end;
972 if (!ir_value_life_remove(self, i))
973 return false; /* failing */
976 if (before->end + 1 == s)
982 /* already contained */
983 if (before->end >= s)
987 if (life->start - 1 == s)
992 /* insert a new entry */
993 new_entry.start = new_entry.end = s;
994 return ir_value_life_insert(self, i, new_entry);
997 bool ir_value_life_merge_into(ir_value *self, const ir_value *other)
1001 if (!other->life_count)
1004 if (!self->life_count) {
1005 for (i = 0; i < other->life_count; ++i) {
1006 if (!ir_value_life_add(self, other->life[i]))
1013 for (i = 0; i < other->life_count; ++i)
1015 const ir_life_entry_t *life = &other->life[i];
1018 ir_life_entry_t *entry = &self->life[myi];
1020 if (life->end+1 < entry->start)
1022 /* adding an interval before entry */
1023 if (!ir_value_life_insert(self, myi, *life))
1029 if (life->start < entry->start &&
1030 life->end+1 >= entry->start)
1032 /* starts earlier and overlaps */
1033 entry->start = life->start;
1036 if (life->end > entry->end &&
1037 life->start <= entry->end+1)
1039 /* ends later and overlaps */
1040 entry->end = life->end;
1043 /* see if our change combines it with the next ranges */
1044 while (myi+1 < self->life_count &&
1045 entry->end+1 >= self->life[1+myi].start)
1047 /* overlaps with (myi+1) */
1048 if (entry->end < self->life[1+myi].end)
1049 entry->end = self->life[1+myi].end;
1050 if (!ir_value_life_remove(self, myi+1))
1052 entry = &self->life[myi];
1055 /* see if we're after the entry */
1056 if (life->start > entry->end)
1059 /* append if we're at the end */
1060 if (myi >= self->life_count) {
1061 if (!ir_value_life_add(self, *life))
1065 /* otherweise check the next range */
1074 bool ir_values_overlap(const ir_value *a, const ir_value *b)
1076 /* For any life entry in A see if it overlaps with
1077 * any life entry in B.
1078 * Note that the life entries are orderes, so we can make a
1079 * more efficient algorithm there than naively translating the
1083 ir_life_entry_t *la, *lb, *enda, *endb;
1085 /* first of all, if either has no life range, they cannot clash */
1086 if (!a->life_count || !b->life_count)
1091 enda = la + a->life_count;
1092 endb = lb + b->life_count;
1095 /* check if the entries overlap, for that,
1096 * both must start before the other one ends.
1098 if (la->start < lb->end &&
1099 lb->start < la->end)
1104 /* entries are ordered
1105 * one entry is earlier than the other
1106 * that earlier entry will be moved forward
1108 if (la->start < lb->start)
1110 /* order: A B, move A forward
1111 * check if we hit the end with A
1116 else /* if (lb->start < la->start) actually <= */
1118 /* order: B A, move B forward
1119 * check if we hit the end with B
1128 /***********************************************************************
1132 bool ir_block_create_store_op(ir_block *self, int op, ir_value *target, ir_value *what)
1134 ir_instr *in = ir_instr_new(self, op);
1138 if (target->store == store_value &&
1139 (op < INSTR_STOREP_F || op > INSTR_STOREP_FNC))
1141 irerror(self->context, "cannot store to an SSA value");
1142 irerror(self->context, "trying to store: %s <- %s", target->name, what->name);
1143 irerror(self->context, "instruction: %s", asm_instr[op].m);
1147 if (!ir_instr_op(in, 0, target, true) ||
1148 !ir_instr_op(in, 1, what, false) ||
1149 !ir_block_instr_add(self, in) )
1156 bool ir_block_create_store(ir_block *self, ir_value *target, ir_value *what)
1160 if (target->vtype == TYPE_VARIANT)
1161 vtype = what->vtype;
1163 vtype = target->vtype;
1166 if (vtype == TYPE_FLOAT && what->vtype == TYPE_INTEGER)
1167 op = INSTR_CONV_ITOF;
1168 else if (vtype == TYPE_INTEGER && what->vtype == TYPE_FLOAT)
1169 op = INSTR_CONV_FTOI;
1171 op = type_store_instr[vtype];
1173 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS)) {
1174 if (op == INSTR_STORE_FLD && what->fieldtype == TYPE_VECTOR)
1178 return ir_block_create_store_op(self, op, target, what);
1181 bool ir_block_create_storep(ir_block *self, ir_value *target, ir_value *what)
1186 if (target->vtype != TYPE_POINTER)
1189 /* storing using pointer - target is a pointer, type must be
1190 * inferred from source
1192 vtype = what->vtype;
1194 op = type_storep_instr[vtype];
1195 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS)) {
1196 if (op == INSTR_STOREP_FLD && what->fieldtype == TYPE_VECTOR)
1197 op = INSTR_STOREP_V;
1200 return ir_block_create_store_op(self, op, target, what);
1203 bool ir_block_create_return(ir_block *self, ir_value *v)
1207 irerror(self->context, "block already ended (%s)", self->label);
1211 self->is_return = true;
1212 in = ir_instr_new(self, INSTR_RETURN);
1216 if (v && !ir_instr_op(in, 0, v, false))
1219 if (!ir_block_instr_add(self, in))
1224 bool ir_block_create_if(ir_block *self, ir_value *v,
1225 ir_block *ontrue, ir_block *onfalse)
1229 irerror(self->context, "block already ended (%s)", self->label);
1233 /*in = ir_instr_new(self, (v->vtype == TYPE_STRING ? INSTR_IF_S : INSTR_IF_F));*/
1234 in = ir_instr_new(self, VINSTR_COND);
1238 if (!ir_instr_op(in, 0, v, false)) {
1239 ir_instr_delete(in);
1243 in->bops[0] = ontrue;
1244 in->bops[1] = onfalse;
1246 if (!ir_block_instr_add(self, in))
1249 if (!ir_block_exits_add(self, ontrue) ||
1250 !ir_block_exits_add(self, onfalse) ||
1251 !ir_block_entries_add(ontrue, self) ||
1252 !ir_block_entries_add(onfalse, self) )
1259 bool ir_block_create_jump(ir_block *self, ir_block *to)
1263 irerror(self->context, "block already ended (%s)", self->label);
1267 in = ir_instr_new(self, VINSTR_JUMP);
1272 if (!ir_block_instr_add(self, in))
1275 if (!ir_block_exits_add(self, to) ||
1276 !ir_block_entries_add(to, self) )
1283 bool ir_block_create_goto(ir_block *self, ir_block *to)
1287 irerror(self->context, "block already ended (%s)", self->label);
1291 in = ir_instr_new(self, INSTR_GOTO);
1296 if (!ir_block_instr_add(self, in))
1299 if (!ir_block_exits_add(self, to) ||
1300 !ir_block_entries_add(to, self) )
1307 ir_instr* ir_block_create_phi(ir_block *self, const char *label, int ot)
1311 in = ir_instr_new(self, VINSTR_PHI);
1314 out = ir_value_out(self->owner, label, store_value, ot);
1316 ir_instr_delete(in);
1319 if (!ir_instr_op(in, 0, out, true)) {
1320 ir_instr_delete(in);
1321 ir_value_delete(out);
1324 if (!ir_block_instr_add(self, in)) {
1325 ir_instr_delete(in);
1326 ir_value_delete(out);
1332 ir_value* ir_phi_value(ir_instr *self)
1334 return self->_ops[0];
1337 bool ir_phi_add(ir_instr* self, ir_block *b, ir_value *v)
1341 if (!ir_block_entries_find(self->owner, b, NULL)) {
1342 /* Must not be possible to cause this, otherwise the AST
1343 * is doing something wrong.
1345 irerror(self->context, "Invalid entry block for PHI");
1351 if (!ir_value_reads_add(v, self))
1353 return ir_instr_phi_add(self, pe);
1356 /* call related code */
1357 ir_instr* ir_block_create_call(ir_block *self, const char *label, ir_value *func)
1361 in = ir_instr_new(self, INSTR_CALL0);
1364 out = ir_value_out(self->owner, label, (func->outtype == TYPE_VOID) ? store_return : store_value, func->outtype);
1366 ir_instr_delete(in);
1369 if (!ir_instr_op(in, 0, out, true) ||
1370 !ir_instr_op(in, 1, func, false) ||
1371 !ir_block_instr_add(self, in))
1373 ir_instr_delete(in);
1374 ir_value_delete(out);
1380 ir_value* ir_call_value(ir_instr *self)
1382 return self->_ops[0];
1385 bool ir_call_param(ir_instr* self, ir_value *v)
1387 if (!ir_instr_params_add(self, v))
1389 if (!ir_value_reads_add(v, self)) {
1390 if (!ir_instr_params_remove(self, self->params_count-1))
1391 GMQCC_SUPPRESS_EMPTY_BODY;
1397 /* binary op related code */
1399 ir_value* ir_block_create_binop(ir_block *self,
1400 const char *label, int opcode,
1401 ir_value *left, ir_value *right)
1423 case INSTR_SUB_S: /* -- offset of string as float */
1428 case INSTR_BITOR_IF:
1429 case INSTR_BITOR_FI:
1430 case INSTR_BITAND_FI:
1431 case INSTR_BITAND_IF:
1446 case INSTR_BITAND_I:
1449 case INSTR_RSHIFT_I:
1450 case INSTR_LSHIFT_I:
1472 /* boolean operations result in floats */
1473 if (opcode >= INSTR_EQ_F && opcode <= INSTR_GT)
1475 else if (opcode >= INSTR_LE && opcode <= INSTR_GT)
1478 else if (opcode >= INSTR_LE_I && opcode <= INSTR_EQ_FI)
1483 if (ot == TYPE_VOID) {
1484 /* The AST or parser were supposed to check this! */
1488 return ir_block_create_general_instr(self, label, opcode, left, right, ot);
1491 ir_value* ir_block_create_unary(ir_block *self,
1492 const char *label, int opcode,
1495 int ot = TYPE_FLOAT;
1507 /* QC doesn't have other unary operations. We expect extensions to fill
1508 * the above list, otherwise we assume out-type = in-type, eg for an
1512 ot = operand->vtype;
1515 if (ot == TYPE_VOID) {
1516 /* The AST or parser were supposed to check this! */
1520 /* let's use the general instruction creator and pass NULL for OPB */
1521 return ir_block_create_general_instr(self, label, opcode, operand, NULL, ot);
1524 ir_value* ir_block_create_general_instr(ir_block *self, const char *label,
1525 int op, ir_value *a, ir_value *b, int outype)
1530 out = ir_value_out(self->owner, label, store_value, outype);
1534 instr = ir_instr_new(self, op);
1536 ir_value_delete(out);
1540 if (!ir_instr_op(instr, 0, out, true) ||
1541 !ir_instr_op(instr, 1, a, false) ||
1542 !ir_instr_op(instr, 2, b, false) )
1547 if (!ir_block_instr_add(self, instr))
1552 ir_instr_delete(instr);
1553 ir_value_delete(out);
1557 ir_value* ir_block_create_fieldaddress(ir_block *self, const char *label, ir_value *ent, ir_value *field)
1561 /* Support for various pointer types todo if so desired */
1562 if (ent->vtype != TYPE_ENTITY)
1565 if (field->vtype != TYPE_FIELD)
1568 v = ir_block_create_general_instr(self, label, INSTR_ADDRESS, ent, field, TYPE_POINTER);
1569 v->fieldtype = field->fieldtype;
1573 ir_value* ir_block_create_load_from_ent(ir_block *self, const char *label, ir_value *ent, ir_value *field, int outype)
1576 if (ent->vtype != TYPE_ENTITY)
1579 /* at some point we could redirect for TYPE_POINTER... but that could lead to carelessness */
1580 if (field->vtype != TYPE_FIELD)
1585 case TYPE_FLOAT: op = INSTR_LOAD_F; break;
1586 case TYPE_VECTOR: op = INSTR_LOAD_V; break;
1587 case TYPE_STRING: op = INSTR_LOAD_S; break;
1588 case TYPE_FIELD: op = INSTR_LOAD_FLD; break;
1589 case TYPE_ENTITY: op = INSTR_LOAD_ENT; break;
1590 case TYPE_FUNCTION: op = INSTR_LOAD_FNC; break;
1592 case TYPE_POINTER: op = INSTR_LOAD_I; break;
1593 case TYPE_INTEGER: op = INSTR_LOAD_I; break;
1596 irerror(self->context, "invalid type for ir_block_create_load_from_ent: %s", type_name[outype]);
1600 return ir_block_create_general_instr(self, label, op, ent, field, outype);
1603 ir_value* ir_block_create_add(ir_block *self,
1605 ir_value *left, ir_value *right)
1608 int l = left->vtype;
1609 int r = right->vtype;
1613 irerror(self->context, "invalid type for ir_block_create_add: %s", type_name[l]);
1629 if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1631 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1636 irerror(self->context, "invalid type for ir_block_create_add: %s", type_name[l]);
1640 return ir_block_create_binop(self, label, op, left, right);
1643 ir_value* ir_block_create_sub(ir_block *self,
1645 ir_value *left, ir_value *right)
1648 int l = left->vtype;
1649 int r = right->vtype;
1654 irerror(self->context, "invalid type for ir_block_create_sub: %s", type_name[l]);
1670 if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1672 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1677 irerror(self->context, "invalid type for ir_block_create_sub: %s", type_name[l]);
1681 return ir_block_create_binop(self, label, op, left, right);
1684 ir_value* ir_block_create_mul(ir_block *self,
1686 ir_value *left, ir_value *right)
1689 int l = left->vtype;
1690 int r = right->vtype;
1695 irerror(self->context, "invalid type for ir_block_create_mul: %s", type_name[l]);
1710 if ( (l == TYPE_VECTOR && r == TYPE_FLOAT) )
1712 else if ( (l == TYPE_FLOAT && r == TYPE_VECTOR) )
1715 else if ( (l == TYPE_VECTOR && r == TYPE_INTEGER) )
1717 else if ( (l == TYPE_INTEGER && r == TYPE_VECTOR) )
1719 else if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1721 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1725 irerror(self->context, "invalid type for ir_block_create_mul: %s", type_name[l]);
1729 return ir_block_create_binop(self, label, op, left, right);
1732 ir_value* ir_block_create_div(ir_block *self,
1734 ir_value *left, ir_value *right)
1737 int l = left->vtype;
1738 int r = right->vtype;
1743 irerror(self->context, "invalid type for ir_block_create_div: %s", type_name[l]);
1756 if ( (l == TYPE_VECTOR && r == TYPE_FLOAT) )
1758 else if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1760 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1765 irerror(self->context, "invalid type for ir_block_create_div: %s", type_name[l]);
1769 return ir_block_create_binop(self, label, op, left, right);
1772 /* PHI resolving breaks the SSA, and must thus be the last
1773 * step before life-range calculation.
1776 static bool ir_block_naive_phi(ir_block *self);
1777 bool ir_function_naive_phi(ir_function *self)
1781 for (i = 0; i < self->blocks_count; ++i)
1783 if (!ir_block_naive_phi(self->blocks[i]))
1789 static bool ir_naive_phi_emit_store(ir_block *block, size_t iid, ir_value *old, ir_value *what)
1794 /* create a store */
1795 if (!ir_block_create_store(block, old, what))
1798 /* we now move it up */
1799 instr = block->instr[block->instr_count-1];
1800 for (i = block->instr_count; i > iid; --i)
1801 block->instr[i] = block->instr[i-1];
1802 block->instr[i] = instr;
1807 static bool ir_block_naive_phi(ir_block *self)
1810 /* FIXME: optionally, create_phi can add the phis
1811 * to a list so we don't need to loop through blocks
1812 * - anyway: "don't optimize YET"
1814 for (i = 0; i < self->instr_count; ++i)
1816 ir_instr *instr = self->instr[i];
1817 if (instr->opcode != VINSTR_PHI)
1820 if (!ir_block_instr_remove(self, i))
1822 --i; /* NOTE: i+1 below */
1824 for (p = 0; p < instr->phi_count; ++p)
1826 ir_value *v = instr->phi[p].value;
1827 for (w = 0; w < v->writes_count; ++w) {
1830 if (!v->writes[w]->_ops[0])
1833 /* When the write was to a global, we have to emit a mov */
1834 old = v->writes[w]->_ops[0];
1836 /* The original instruction now writes to the PHI target local */
1837 if (v->writes[w]->_ops[0] == v)
1838 v->writes[w]->_ops[0] = instr->_ops[0];
1840 if (old->store != store_value && old->store != store_local && old->store != store_param)
1842 /* If it originally wrote to a global we need to store the value
1845 if (!ir_naive_phi_emit_store(self, i+1, old, v))
1847 if (i+1 < self->instr_count)
1848 instr = self->instr[i+1];
1851 /* In case I forget and access instr later, it'll be NULL
1852 * when it's a problem, to make sure we crash, rather than accessing
1858 /* If it didn't, we can replace all reads by the phi target now. */
1860 for (r = 0; r < old->reads_count; ++r)
1863 ir_instr *ri = old->reads[r];
1864 for (op = 0; op < ri->phi_count; ++op) {
1865 if (ri->phi[op].value == old)
1866 ri->phi[op].value = v;
1868 for (op = 0; op < 3; ++op) {
1869 if (ri->_ops[op] == old)
1876 ir_instr_delete(instr);
1881 /***********************************************************************
1882 *IR Temp allocation code
1883 * Propagating value life ranges by walking through the function backwards
1884 * until no more changes are made.
1885 * In theory this should happen once more than once for every nested loop
1887 * Though this implementation might run an additional time for if nests.
1896 MEM_VEC_FUNCTIONS_ALL(new_reads_t, ir_value*, v)
1898 /* Enumerate instructions used by value's life-ranges
1900 static void ir_block_enumerate(ir_block *self, size_t *_eid)
1904 for (i = 0; i < self->instr_count; ++i)
1906 self->instr[i]->eid = eid++;
1911 /* Enumerate blocks and instructions.
1912 * The block-enumeration is unordered!
1913 * We do not really use the block enumreation, however
1914 * the instruction enumeration is important for life-ranges.
1916 void ir_function_enumerate(ir_function *self)
1919 size_t instruction_id = 0;
1920 for (i = 0; i < self->blocks_count; ++i)
1922 self->blocks[i]->eid = i;
1923 self->blocks[i]->run_id = 0;
1924 ir_block_enumerate(self->blocks[i], &instruction_id);
1928 static bool ir_block_life_propagate(ir_block *b, ir_block *prev, bool *changed);
1929 bool ir_function_calculate_liferanges(ir_function *self)
1937 for (i = 0; i != self->blocks_count; ++i)
1939 if (self->blocks[i]->is_return)
1941 self->blocks[i]->living_count = 0;
1942 if (!ir_block_life_propagate(self->blocks[i], NULL, &changed))
1947 if (self->blocks_count) {
1948 ir_block *block = self->blocks[0];
1949 for (i = 0; i < block->living_count; ++i) {
1950 ir_value *v = block->living[i];
1951 if (v->memberof || v->store != store_local)
1953 if (irwarning(v->context, WARN_USED_UNINITIALIZED,
1954 "variable `%s` may be used uninitialized in this function", v->name))
1963 /* Local-value allocator
1964 * After finishing creating the liferange of all values used in a function
1965 * we can allocate their global-positions.
1966 * This is the counterpart to register-allocation in register machines.
1969 MEM_VECTOR_MAKE(ir_value*, locals);
1970 MEM_VECTOR_MAKE(size_t, sizes);
1971 MEM_VECTOR_MAKE(size_t, positions);
1972 } function_allocator;
1973 MEM_VEC_FUNCTIONS(function_allocator, ir_value*, locals)
1974 MEM_VEC_FUNCTIONS(function_allocator, size_t, sizes)
1975 MEM_VEC_FUNCTIONS(function_allocator, size_t, positions)
1977 static bool function_allocator_alloc(function_allocator *alloc, const ir_value *var)
1980 size_t vsize = type_sizeof[var->vtype];
1982 slot = ir_value_var("reg", store_global, var->vtype);
1986 if (!ir_value_life_merge_into(slot, var))
1989 if (!function_allocator_locals_add(alloc, slot))
1992 if (!function_allocator_sizes_add(alloc, vsize))
1998 ir_value_delete(slot);
2002 bool ir_function_allocate_locals(ir_function *self)
2011 function_allocator alloc;
2013 if (!self->locals_count && !self->values_count)
2016 MEM_VECTOR_INIT(&alloc, locals);
2017 MEM_VECTOR_INIT(&alloc, sizes);
2018 MEM_VECTOR_INIT(&alloc, positions);
2020 for (i = 0; i < self->locals_count; ++i)
2022 if (!function_allocator_alloc(&alloc, self->locals[i]))
2026 /* Allocate a slot for any value that still exists */
2027 for (i = 0; i < self->values_count; ++i)
2029 v = self->values[i];
2034 for (a = 0; a < alloc.locals_count; ++a)
2036 slot = alloc.locals[a];
2038 if (ir_values_overlap(v, slot))
2041 if (!ir_value_life_merge_into(slot, v))
2044 /* adjust size for this slot */
2045 if (alloc.sizes[a] < type_sizeof[v->vtype])
2046 alloc.sizes[a] = type_sizeof[v->vtype];
2048 self->values[i]->code.local = a;
2051 if (a >= alloc.locals_count) {
2052 self->values[i]->code.local = alloc.locals_count;
2053 if (!function_allocator_alloc(&alloc, v))
2062 /* Adjust slot positions based on sizes */
2063 if (!function_allocator_positions_add(&alloc, 0))
2066 if (alloc.sizes_count)
2067 pos = alloc.positions[0] + alloc.sizes[0];
2070 for (i = 1; i < alloc.sizes_count; ++i)
2072 pos = alloc.positions[i-1] + alloc.sizes[i-1];
2073 if (!function_allocator_positions_add(&alloc, pos))
2077 self->allocated_locals = pos + alloc.sizes[alloc.sizes_count-1];
2079 /* Take over the actual slot positions */
2080 for (i = 0; i < self->values_count; ++i) {
2081 self->values[i]->code.local = alloc.positions[self->values[i]->code.local];
2089 for (i = 0; i < alloc.locals_count; ++i)
2090 ir_value_delete(alloc.locals[i]);
2091 MEM_VECTOR_CLEAR(&alloc, locals);
2092 MEM_VECTOR_CLEAR(&alloc, sizes);
2093 MEM_VECTOR_CLEAR(&alloc, positions);
2097 /* Get information about which operand
2098 * is read from, or written to.
2100 static void ir_op_read_write(int op, size_t *read, size_t *write)
2120 case INSTR_STOREP_F:
2121 case INSTR_STOREP_V:
2122 case INSTR_STOREP_S:
2123 case INSTR_STOREP_ENT:
2124 case INSTR_STOREP_FLD:
2125 case INSTR_STOREP_FNC:
2136 static bool ir_block_living_add_instr(ir_block *self, size_t eid)
2139 bool changed = false;
2141 for (i = 0; i != self->living_count; ++i)
2143 tempbool = ir_value_life_merge(self->living[i], eid);
2146 irerror(self->context, "block_living_add_instr() value instruction added %s: %i", self->living[i]->_name, (int)eid);
2148 changed = changed || tempbool;
2153 static bool ir_block_life_prop_previous(ir_block* self, ir_block *prev, bool *changed)
2156 /* values which have been read in a previous iteration are now
2157 * in the "living" array even if the previous block doesn't use them.
2158 * So we have to remove whatever does not exist in the previous block.
2159 * They will be re-added on-read, but the liferange merge won't cause
2162 for (i = 0; i < self->living_count; ++i)
2164 if (!ir_block_living_find(prev, self->living[i], NULL)) {
2165 if (!ir_block_living_remove(self, i))
2171 /* Whatever the previous block still has in its living set
2172 * must now be added to ours as well.
2174 for (i = 0; i < prev->living_count; ++i)
2176 if (ir_block_living_find(self, prev->living[i], NULL))
2178 if (!ir_block_living_add(self, prev->living[i]))
2181 irerror(self->contextt from prev: %s", self->label, prev->living[i]->_name);
2187 static bool ir_block_life_propagate(ir_block *self, ir_block *prev, bool *changed)
2193 /* bitmasks which operands are read from or written to */
2195 char dbg_ind[16] = { '#', '0' };
2200 if (!ir_block_life_prop_previous(self, prev, changed))
2204 i = self->instr_count;
2207 instr = self->instr[i];
2209 /* PHI operands are always read operands */
2210 for (p = 0; p < instr->phi_count; ++p)
2212 value = instr->phi[p].value;
2213 if (value->memberof)
2214 value = value->memberof;
2215 if (!ir_block_living_find(self, value, NULL) &&
2216 !ir_block_living_add(self, value))
2222 /* call params are read operands too */
2223 for (p = 0; p < instr->params_count; ++p)
2225 value = instr->params[p];
2226 if (value->memberof)
2227 value = value->memberof;
2228 if (!ir_block_living_find(self, value, NULL) &&
2229 !ir_block_living_add(self, value))
2235 /* See which operands are read and write operands */
2236 ir_op_read_write(instr->opcode, &read, &write);
2238 if (instr->opcode == INSTR_MUL_VF)
2240 /* the float source will get an additional lifetime */
2241 tempbool = ir_value_life_merge(instr->_ops[2], instr->eid+1);
2242 *changed = *changed || tempbool;
2244 else if (instr->opcode == INSTR_MUL_FV)
2246 /* the float source will get an additional lifetime */
2247 tempbool = ir_value_life_merge(instr->_ops[1], instr->eid+1);
2248 *changed = *changed || tempbool;
2251 /* Go through the 3 main operands */
2252 for (o = 0; o < 3; ++o)
2254 if (!instr->_ops[o]) /* no such operand */
2257 value = instr->_ops[o];
2258 if (value->memberof)
2259 value = value->memberof;
2261 /* We only care about locals */
2262 /* we also calculate parameter liferanges so that locals
2263 * can take up parameter slots */
2264 if (value->store != store_value &&
2265 value->store != store_local &&
2266 value->store != store_param)
2272 if (!ir_block_living_find(self, value, NULL) &&
2273 !ir_block_living_add(self, value))
2279 /* write operands */
2280 /* When we write to a local, we consider it "dead" for the
2281 * remaining upper part of the function, since in SSA a value
2282 * can only be written once (== created)
2287 bool in_living = ir_block_living_find(self, value, &idx);
2290 /* If the value isn't alive it hasn't been read before... */
2291 /* TODO: See if the warning can be emitted during parsing or AST processing
2292 * otherwise have warning printed here.
2293 * IF printing a warning here: include filecontext_t,
2294 * and make sure it's only printed once
2295 * since this function is run multiple times.
2297 /* For now: debug info: */
2298 /* fprintf(stderr, "Value only written %s\n", value->name); */
2299 tempbool = ir_value_life_merge(value, instr->eid);
2300 *changed = *changed || tempbool;
2302 ir_instr_dump(instr, dbg_ind, printf);
2306 /* since 'living' won't contain it
2307 * anymore, merge the value, since
2310 tempbool = ir_value_life_merge(value, instr->eid);
2313 fprintf(stderr, "value added id %s %i\n", value->name, (int)instr->eid);
2315 *changed = *changed || tempbool;
2317 if (!ir_block_living_remove(self, idx))
2323 tempbool = ir_block_living_add_instr(self, instr->eid);
2324 /*fprintf(stderr, "living added values\n");*/
2325 *changed = *changed || tempbool;
2329 if (self->run_id == self->owner->run_id)
2332 self->run_id = self->owner->run_id;
2334 for (i = 0; i < self->entries_count; ++i)
2336 ir_block *entry = self->entries[i];
2337 ir_block_life_propagate(entry, self, changed);
2343 /***********************************************************************
2346 * Since the IR has the convention of putting 'write' operands
2347 * at the beginning, we have to rotate the operands of instructions
2348 * properly in order to generate valid QCVM code.
2350 * Having destinations at a fixed position is more convenient. In QC
2351 * this is *mostly* OPC, but FTE adds at least 2 instructions which
2352 * read from from OPA, and store to OPB rather than OPC. Which is
2353 * partially the reason why the implementation of these instructions
2354 * in darkplaces has been delayed for so long.
2356 * Breaking conventions is annoying...
2358 static bool ir_builder_gen_global(ir_builder *self, ir_value *global, bool islocal);
2360 static bool gen_global_field(ir_value *global)
2362 if (global->isconst)
2364 ir_value *fld = global->constval.vpointer;
2366 irerror(global->context, "Invalid field constant with no field: %s", global->name);
2370 /* Now, in this case, a relocation would be impossible to code
2371 * since it looks like this:
2372 * .vector v = origin; <- parse error, wtf is 'origin'?
2375 * But we will need a general relocation support later anyway
2376 * for functions... might as well support that here.
2378 if (!fld->code.globaladdr) {
2379 irerror(global->context, "FIXME: Relocation support");
2383 /* copy the field's value */
2384 ir_value_code_setaddr(global, code_globals_add(code_globals_data[fld->code.globaladdr]));
2385 if (global->fieldtype == TYPE_VECTOR) {
2386 code_globals_add(code_globals_data[fld->code.globaladdr]+1);
2387 code_globals_add(code_globals_data[fld->code.globaladdr]+2);
2392 ir_value_code_setaddr(global, code_globals_add(0));
2393 if (global->fieldtype == TYPE_VECTOR) {
2394 code_globals_add(0);
2395 code_globals_add(0);
2398 if (global->code.globaladdr < 0)
2403 static bool gen_global_pointer(ir_value *global)
2405 if (global->isconst)
2407 ir_value *target = global->constval.vpointer;
2409 irerror(global->context, "Invalid pointer constant: %s", global->name);
2410 /* NULL pointers are pointing to the NULL constant, which also
2411 * sits at address 0, but still has an ir_value for itself.
2416 /* Here, relocations ARE possible - in fteqcc-enhanced-qc:
2417 * void() foo; <- proto
2418 * void() *fooptr = &foo;
2419 * void() foo = { code }
2421 if (!target->code.globaladdr) {
2422 /* FIXME: Check for the constant nullptr ir_value!
2423 * because then code.globaladdr being 0 is valid.
2425 irerror(global->context, "FIXME: Relocation support");
2429 ir_value_code_setaddr(global, code_globals_add(target->code.globaladdr));
2433 ir_value_code_setaddr(global, code_globals_add(0));
2435 if (global->code.globaladdr < 0)
2440 static bool gen_blocks_recursive(ir_function *func, ir_block *block)
2442 prog_section_statement stmt;
2451 block->generated = true;
2452 block->code_start = code_statements_elements;
2453 for (i = 0; i < block->instr_count; ++i)
2455 instr = block->instr[i];
2457 if (instr->opcode == VINSTR_PHI) {
2458 irerror(block->context, "cannot generate virtual instruction (phi)");
2462 if (instr->opcode == VINSTR_JUMP) {
2463 target = instr->bops[0];
2464 /* for uncoditional jumps, if the target hasn't been generated
2465 * yet, we generate them right here.
2467 if (!target->generated) {
2472 /* otherwise we generate a jump instruction */
2473 stmt.opcode = INSTR_GOTO;
2474 stmt.o1.s1 = (target->code_start) - code_statements_elements;
2477 if (code_statements_add(stmt) < 0)
2480 /* no further instructions can be in this block */
2484 if (instr->opcode == VINSTR_COND) {
2485 ontrue = instr->bops[0];
2486 onfalse = instr->bops[1];
2487 /* TODO: have the AST signal which block should
2488 * come first: eg. optimize IFs without ELSE...
2491 stmt.o1.u1 = ir_value_code_addr(instr->_ops[0]);
2495 if (ontrue->generated) {
2496 stmt.opcode = INSTR_IF;
2497 stmt.o2.s1 = (ontrue->code_start) - code_statements_elements;
2498 if (code_statements_add(stmt) < 0)
2501 if (onfalse->generated) {
2502 stmt.opcode = INSTR_IFNOT;
2503 stmt.o2.s1 = (onfalse->code_start) - code_statements_elements;
2504 if (code_statements_add(stmt) < 0)
2507 if (!ontrue->generated) {
2508 if (onfalse->generated) {
2513 if (!onfalse->generated) {
2514 if (ontrue->generated) {
2519 /* neither ontrue nor onfalse exist */
2520 stmt.opcode = INSTR_IFNOT;
2521 stidx = code_statements_elements;
2522 if (code_statements_add(stmt) < 0)
2524 /* on false we jump, so add ontrue-path */
2525 if (!gen_blocks_recursive(func, ontrue))
2527 /* fixup the jump address */
2528 code_statements_data[stidx].o2.s1 = code_statements_elements - stidx;
2529 /* generate onfalse path */
2530 if (onfalse->generated) {
2531 /* fixup the jump address */
2532 code_statements_data[stidx].o2.s1 = (onfalse->code_start) - (stidx);
2533 /* may have been generated in the previous recursive call */
2534 stmt.opcode = INSTR_GOTO;
2535 stmt.o1.s1 = (onfalse->code_start) - code_statements_elements;
2538 return (code_statements_add(stmt) >= 0);
2540 /* if not, generate now */
2545 if (instr->opcode >= INSTR_CALL0 && instr->opcode <= INSTR_CALL8) {
2546 /* Trivial call translation:
2547 * copy all params to OFS_PARM*
2548 * if the output's storetype is not store_return,
2549 * add append a STORE instruction!
2551 * NOTES on how to do it better without much trouble:
2552 * -) The liferanges!
2553 * Simply check the liferange of all parameters for
2554 * other CALLs. For each param with no CALL in its
2555 * liferange, we can store it in an OFS_PARM at
2556 * generation already. This would even include later
2557 * reuse.... probably... :)
2562 for (p = 0; p < instr->params_count; ++p)
2564 ir_value *param = instr->params[p];
2566 stmt.opcode = INSTR_STORE_F;
2569 if (param->vtype == TYPE_FIELD)
2570 stmt.opcode = field_store_instr[param->fieldtype];
2572 stmt.opcode = type_store_instr[param->vtype];
2573 stmt.o1.u1 = ir_value_code_addr(param);
2574 stmt.o2.u1 = OFS_PARM0 + 3 * p;
2575 if (code_statements_add(stmt) < 0)
2578 stmt.opcode = INSTR_CALL0 + instr->params_count;
2579 if (stmt.opcode > INSTR_CALL8)
2580 stmt.opcode = INSTR_CALL8;
2581 stmt.o1.u1 = ir_value_code_addr(instr->_ops[1]);
2584 if (code_statements_add(stmt) < 0)
2587 retvalue = instr->_ops[0];
2588 if (retvalue && retvalue->store != store_return && retvalue->life_count)
2590 /* not to be kept in OFS_RETURN */
2591 if (retvalue->vtype == TYPE_FIELD)
2592 stmt.opcode = field_store_instr[retvalue->vtype];
2594 stmt.opcode = type_store_instr[retvalue->vtype];
2595 stmt.o1.u1 = OFS_RETURN;
2596 stmt.o2.u1 = ir_value_code_addr(retvalue);
2598 if (code_statements_add(stmt) < 0)
2604 if (instr->opcode == INSTR_STATE) {
2605 irerror(block->context, "TODO: state instruction");
2609 stmt.opcode = instr->opcode;
2614 /* This is the general order of operands */
2616 stmt.o3.u1 = ir_value_code_addr(instr->_ops[0]);
2619 stmt.o1.u1 = ir_value_code_addr(instr->_ops[1]);
2622 stmt.o2.u1 = ir_value_code_addr(instr->_ops[2]);
2624 if (stmt.opcode == INSTR_RETURN || stmt.opcode == INSTR_DONE)
2626 stmt.o1.u1 = stmt.o3.u1;
2629 else if ((stmt.opcode >= INSTR_STORE_F &&
2630 stmt.opcode <= INSTR_STORE_FNC) ||
2631 (stmt.opcode >= INSTR_STOREP_F &&
2632 stmt.opcode <= INSTR_STOREP_FNC))
2634 /* 2-operand instructions with A -> B */
2635 stmt.o2.u1 = stmt.o3.u1;
2639 if (code_statements_add(stmt) < 0)
2645 static bool gen_function_code(ir_function *self)
2648 prog_section_statement stmt;
2650 /* Starting from entry point, we generate blocks "as they come"
2651 * for now. Dead blocks will not be translated obviously.
2653 if (!self->blocks_count) {
2654 irerror(self->context, "Function '%s' declared without body.", self->name);
2658 block = self->blocks[0];
2659 if (block->generated)
2662 if (!gen_blocks_recursive(self, block)) {
2663 irerror(self->context, "failed to generate blocks for '%s'", self->name);
2667 /* otherwise code_write crashes since it debug-prints functions until AINSTR_END */
2668 stmt.opcode = AINSTR_END;
2672 if (code_statements_add(stmt) < 0)
2677 static qcint ir_builder_filestring(ir_builder *ir, const char *filename)
2679 /* NOTE: filename pointers are copied, we never strdup them,
2680 * thus we can use pointer-comparison to find the string.
2685 for (i = 0; i < ir->filenames_count; ++i) {
2686 if (ir->filenames[i] == filename)
2687 return ir->filestrings[i];
2690 str = code_genstring(filename);
2691 if (!ir_builder_filenames_add(ir, filename))
2693 if (!ir_builder_filestrings_add(ir, str))
2694 ir->filenames_count--;
2698 static bool gen_global_function(ir_builder *ir, ir_value *global)
2700 prog_section_function fun;
2704 size_t local_var_end;
2706 if (!global->isconst || (!global->constval.vfunc))
2708 irerror(global->context, "Invalid state of function-global: not constant: %s", global->name);
2712 irfun = global->constval.vfunc;
2714 fun.name = global->code.name;
2715 fun.file = ir_builder_filestring(ir, global->context.file);
2716 fun.profile = 0; /* always 0 */
2717 fun.nargs = irfun->params_count;
2719 for (i = 0;i < 8; ++i) {
2723 fun.argsize[i] = type_sizeof[irfun->params[i]];
2726 fun.firstlocal = code_globals_elements;
2728 local_var_end = fun.firstlocal;
2729 for (i = 0; i < irfun->locals_count; ++i) {
2730 if (!ir_builder_gen_global(ir, irfun->locals[i], true)) {
2731 irerror(irfun->locals[i]->context, "Failed to generate local %s", irfun->locals[i]->name);
2735 if (irfun->locals_count) {
2736 ir_value *last = irfun->locals[irfun->locals_count-1];
2737 local_var_end = last->code.globaladdr;
2738 local_var_end += type_sizeof[last->vtype];
2740 for (i = 0; i < irfun->values_count; ++i)
2742 /* generate code.globaladdr for ssa values */
2743 ir_value *v = irfun->values[i];
2744 ir_value_code_setaddr(v, local_var_end + v->code.local);
2746 for (i = 0; i < irfun->allocated_locals; ++i) {
2747 /* fill the locals with zeros */
2748 code_globals_add(0);
2751 fun.locals = code_globals_elements - fun.firstlocal;
2754 fun.entry = irfun->builtin;
2756 irfun->code_function_def = code_functions_elements;
2757 fun.entry = code_statements_elements;
2760 return (code_functions_add(fun) >= 0);
2763 static bool gen_global_function_code(ir_builder *ir, ir_value *global)
2765 prog_section_function *fundef;
2768 irfun = global->constval.vfunc;
2770 irwarning(global->context, WARN_IMPLICIT_FUNCTION_POINTER,
2771 "function `%s` has no body and in QC implicitly becomes a function-pointer", global->name);
2772 /* this was a function pointer, don't generate code for those */
2779 if (irfun->code_function_def < 0) {
2780 irerror(irfun->context, "`%s`: IR global wasn't generated, failed to access function-def", irfun->name);
2783 fundef = &code_functions_data[irfun->code_function_def];
2785 fundef->entry = code_statements_elements;
2786 if (!gen_function_code(irfun)) {
2787 irerror(irfun->context, "Failed to generate code for function %s", irfun->name);
2793 static bool ir_builder_gen_global(ir_builder *self, ir_value *global, bool islocal)
2797 prog_section_def def;
2799 def.type = global->vtype;
2800 def.offset = code_globals_elements;
2803 if (global->name[0] == '#') {
2804 if (!self->str_immediate)
2805 self->str_immediate = code_genstring("IMMEDIATE");
2806 def.name = global->code.name = self->str_immediate;
2809 def.name = global->code.name = code_genstring(global->name);
2814 switch (global->vtype)
2817 if (!strcmp(global->name, "end_sys_globals")) {
2818 /* TODO: remember this point... all the defs before this one
2819 * should be checksummed and added to progdefs.h when we generate it.
2822 else if (!strcmp(global->name, "end_sys_fields")) {
2823 /* TODO: same as above but for entity-fields rather than globsl
2827 irwarning(global->context, WARN_VOID_VARIABLES, "unrecognized variable of type void `%s`",
2829 /* I'd argue setting it to 0 is sufficient, but maybe some depend on knowing how far
2830 * the system fields actually go? Though the engine knows this anyway...
2831 * Maybe this could be an -foption
2832 * fteqcc creates data for end_sys_* - of size 1, so let's do the same
2834 ir_value_code_setaddr(global, code_globals_add(0));
2836 if (code_defs_add(def) < 0)
2840 if (code_defs_add(def) < 0)
2842 return gen_global_pointer(global);
2844 if (code_defs_add(def) < 0)
2846 return gen_global_field(global);
2851 if (global->isconst) {
2852 iptr = (int32_t*)&global->constval.ivec[0];
2853 ir_value_code_setaddr(global, code_globals_add(*iptr));
2855 ir_value_code_setaddr(global, code_globals_add(0));
2857 def.type |= DEF_SAVEGLOBAL;
2859 if (code_defs_add(def) < 0)
2862 return global->code.globaladdr >= 0;
2866 if (global->isconst)
2867 ir_value_code_setaddr(global, code_globals_add(code_genstring(global->constval.vstring)));
2869 ir_value_code_setaddr(global, code_globals_add(0));
2871 def.type |= DEF_SAVEGLOBAL;
2873 if (code_defs_add(def) < 0)
2875 return global->code.globaladdr >= 0;
2880 if (global->isconst) {
2881 iptr = (int32_t*)&global->constval.ivec[0];
2882 ir_value_code_setaddr(global, code_globals_add(iptr[0]));
2883 if (global->code.globaladdr < 0)
2885 for (d = 1; d < type_sizeof[global->vtype]; ++d)
2887 if (code_globals_add(iptr[d]) < 0)
2891 ir_value_code_setaddr(global, code_globals_add(0));
2892 if (global->code.globaladdr < 0)
2894 for (d = 1; d < type_sizeof[global->vtype]; ++d)
2896 if (code_globals_add(0) < 0)
2900 def.type |= DEF_SAVEGLOBAL;
2903 if (code_defs_add(def) < 0)
2905 return global->code.globaladdr >= 0;
2908 if (!global->isconst) {
2909 ir_value_code_setaddr(global, code_globals_add(0));
2910 if (global->code.globaladdr < 0)
2913 ir_value_code_setaddr(global, code_globals_elements);
2914 code_globals_add(code_functions_elements);
2915 if (!gen_global_function(self, global))
2918 def.type |= DEF_SAVEGLOBAL;
2920 if (code_defs_add(def) < 0)
2924 /* assume biggest type */
2925 ir_value_code_setaddr(global, code_globals_add(0));
2926 for (i = 1; i < type_sizeof[TYPE_VARIANT]; ++i)
2927 code_globals_add(0);
2930 /* refuse to create 'void' type or any other fancy business. */
2931 irerror(global->context, "Invalid type for global variable `%s`: %s",
2932 global->name, type_name[global->vtype]);
2937 static bool ir_builder_gen_field(ir_builder *self, ir_value *field)
2939 prog_section_def def;
2940 prog_section_field fld;
2942 def.type = field->vtype;
2943 def.offset = code_globals_elements;
2945 /* create a global named the same as the field */
2946 if (opts_standard == COMPILER_GMQCC) {
2947 /* in our standard, the global gets a dot prefix */
2948 size_t len = strlen(field->name);
2951 /* we really don't want to have to allocate this, and 1024
2952 * bytes is more than enough for a variable/field name
2954 if (len+2 >= sizeof(name)) {
2955 irerror(field->context, "invalid field name size: %u", (unsigned int)len);
2960 memcpy(name+1, field->name, len); /* no strncpy - we used strlen above */
2963 def.name = code_genstring(name);
2964 fld.name = def.name + 1; /* we reuse that string table entry */
2966 /* in plain QC, there cannot be a global with the same name,
2967 * and so we also name the global the same.
2968 * FIXME: fteqcc should create a global as well
2969 * check if it actually uses the same name. Probably does
2971 def.name = code_genstring(field->name);
2972 fld.name = def.name;
2975 field->code.name = def.name;
2977 if (code_defs_add(def) < 0)
2980 fld.type = field->fieldtype;
2982 if (fld.type == TYPE_VOID) {
2983 irerror(field->context, "field is missing a type: %s - don't know its size", field->name);
2987 fld.offset = code_alloc_field(type_sizeof[field->fieldtype]);
2989 if (code_fields_add(fld) < 0)
2992 ir_value_code_setaddr(field, code_globals_elements);
2993 if (!code_globals_add(fld.offset))
2995 if (fld.type == TYPE_VECTOR) {
2996 if (!code_globals_add(fld.offset+1))
2998 if (!code_globals_add(fld.offset+2))
3002 return field->code.globaladdr >= 0;
3005 bool ir_builder_generate(ir_builder *self, const char *filename)
3007 prog_section_statement stmt;
3012 for (i = 0; i < self->globals_count; ++i)
3014 if (!ir_builder_gen_global(self, self->globals[i], false)) {
3019 for (i = 0; i < self->fields_count; ++i)
3021 if (!ir_builder_gen_field(self, self->fields[i])) {
3026 /* generate function code */
3027 for (i = 0; i < self->globals_count; ++i)
3029 if (self->globals[i]->vtype == TYPE_FUNCTION) {
3030 if (!gen_global_function_code(self, self->globals[i])) {
3036 /* DP errors if the last instruction is not an INSTR_DONE
3037 * and for debugging purposes we add an additional AINSTR_END
3038 * to the end of functions, so here it goes:
3040 stmt.opcode = INSTR_DONE;
3044 if (code_statements_add(stmt) < 0)
3047 printf("writing '%s'...\n", filename);
3048 return code_write(filename);
3051 /***********************************************************************
3052 *IR DEBUG Dump functions...
3055 #define IND_BUFSZ 1024
3058 # define strncat(dst, src, sz) strncat_s(dst, sz, src, _TRUNCATE)
3061 const char *qc_opname(int op)
3063 if (op < 0) return "<INVALID>";
3064 if (op < ( sizeof(asm_instr) / sizeof(asm_instr[0]) ))
3065 return asm_instr[op].m;
3067 case VINSTR_PHI: return "PHI";
3068 case VINSTR_JUMP: return "JUMP";
3069 case VINSTR_COND: return "COND";
3070 default: return "<UNK>";
3074 void ir_builder_dump(ir_builder *b, int (*oprintf)(const char*, ...))
3077 char indent[IND_BUFSZ];
3081 oprintf("module %s\n", b->name);
3082 for (i = 0; i < b->globals_count; ++i)
3085 if (b->globals[i]->isconst)
3086 oprintf("%s = ", b->globals[i]->name);
3087 ir_value_dump(b->globals[i], oprintf);
3090 for (i = 0; i < b->functions_count; ++i)
3091 ir_function_dump(b->functions[i], indent, oprintf);
3092 oprintf("endmodule %s\n", b->name);
3095 void ir_function_dump(ir_function *f, char *ind,
3096 int (*oprintf)(const char*, ...))
3099 if (f->builtin != 0) {
3100 oprintf("%sfunction %s = builtin %i\n", ind, f->name, -f->builtin);
3103 oprintf("%sfunction %s\n", ind, f->name);
3104 strncat(ind, "\t", IND_BUFSZ);
3105 if (f->locals_count)
3107 oprintf("%s%i locals:\n", ind, (int)f->locals_count);
3108 for (i = 0; i < f->locals_count; ++i) {
3109 oprintf("%s\t", ind);
3110 ir_value_dump(f->locals[i], oprintf);
3114 oprintf("%sliferanges:\n", ind);
3115 for (i = 0; i < f->locals_count; ++i) {
3117 ir_value *v = f->locals[i];
3118 oprintf("%s\t%s: unique ", ind, v->name);
3119 for (l = 0; l < v->life_count; ++l) {
3120 oprintf("[%i,%i] ", v->life[l].start, v->life[l].end);
3124 for (i = 0; i < f->values_count; ++i) {
3126 ir_value *v = f->values[i];
3127 oprintf("%s\t%s: @%i ", ind, v->name, (int)v->code.local);
3128 for (l = 0; l < v->life_count; ++l) {
3129 oprintf("[%i,%i] ", v->life[l].start, v->life[l].end);
3133 if (f->blocks_count)
3135 oprintf("%slife passes (check): %i\n", ind, (int)f->run_id);
3136 for (i = 0; i < f->blocks_count; ++i) {
3137 if (f->blocks[i]->run_id != f->run_id) {
3138 oprintf("%slife pass check fail! %i != %i\n", ind, (int)f->blocks[i]->run_id, (int)f->run_id);
3140 ir_block_dump(f->blocks[i], ind, oprintf);
3144 ind[strlen(ind)-1] = 0;
3145 oprintf("%sendfunction %s\n", ind, f->name);
3148 void ir_block_dump(ir_block* b, char *ind,
3149 int (*oprintf)(const char*, ...))
3152 oprintf("%s:%s\n", ind, b->label);
3153 strncat(ind, "\t", IND_BUFSZ);
3155 for (i = 0; i < b->instr_count; ++i)
3156 ir_instr_dump(b->instr[i], ind, oprintf);
3157 ind[strlen(ind)-1] = 0;
3160 void dump_phi(ir_instr *in, char *ind,
3161 int (*oprintf)(const char*, ...))
3164 oprintf("%s <- phi ", in->_ops[0]->name);
3165 for (i = 0; i < in->phi_count; ++i)
3167 oprintf("([%s] : %s) ", in->phi[i].from->label,
3168 in->phi[i].value->name);
3173 void ir_instr_dump(ir_instr *in, char *ind,
3174 int (*oprintf)(const char*, ...))
3177 const char *comma = NULL;
3179 oprintf("%s (%i) ", ind, (int)in->eid);
3181 if (in->opcode == VINSTR_PHI) {
3182 dump_phi(in, ind, oprintf);
3186 strncat(ind, "\t", IND_BUFSZ);
3188 if (in->_ops[0] && (in->_ops[1] || in->_ops[2])) {
3189 ir_value_dump(in->_ops[0], oprintf);
3190 if (in->_ops[1] || in->_ops[2])
3193 if (in->opcode == INSTR_CALL0) {
3194 oprintf("CALL%i\t", in->params_count);
3196 oprintf("%s\t", qc_opname(in->opcode));
3198 if (in->_ops[0] && !(in->_ops[1] || in->_ops[2])) {
3199 ir_value_dump(in->_ops[0], oprintf);
3204 for (i = 1; i != 3; ++i) {
3208 ir_value_dump(in->_ops[i], oprintf);
3216 oprintf("[%s]", in->bops[0]->label);
3220 oprintf("%s[%s]", comma, in->bops[1]->label);
3221 if (in->params_count) {
3222 oprintf("\tparams: ");
3223 for (i = 0; i != in->params_count; ++i) {
3224 oprintf("%s, ", in->params[i]->name);
3228 ind[strlen(ind)-1] = 0;
3231 void ir_value_dump(ir_value* v, int (*oprintf)(const char*, ...))
3240 oprintf("fn:%s", v->name);
3243 oprintf("%g", v->constval.vfloat);
3246 oprintf("'%g %g %g'",
3249 v->constval.vvec.z);
3252 oprintf("(entity)");
3255 oprintf("\"%s\"", v->constval.vstring);
3259 oprintf("%i", v->constval.vint);
3264 v->constval.vpointer->name);
3268 oprintf("%s", v->name);
3272 void ir_value_dump_life(const ir_value *self, int (*oprintf)(const char*,...))
3275 oprintf("Life of %12s:", self->name);
3276 for (i = 0; i < self->life_count; ++i)
3278 oprintf(" + [%i, %i]\n", self->life[i].start, self->life[i].end);