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] = {
49 size_t type_sizeof[TYPE_COUNT] = {
56 1, /* TYPE_FUNCTION */
61 4, /* TYPE_QUATERNION */
63 16, /* TYPE_VARIANT */
66 uint16_t type_store_instr[TYPE_COUNT] = {
67 INSTR_STORE_F, /* should use I when having integer support */
74 INSTR_STORE_ENT, /* should use I */
76 INSTR_STORE_I, /* integer type */
81 INSTR_STORE_M, /* variant, should never be accessed */
84 uint16_t type_storep_instr[TYPE_COUNT] = {
85 INSTR_STOREP_F, /* should use I when having integer support */
92 INSTR_STOREP_ENT, /* should use I */
94 INSTR_STOREP_ENT, /* integer type */
99 INSTR_STOREP_M, /* variant, should never be accessed */
102 MEM_VEC_FUNCTIONS(ir_value_vector, ir_value*, v)
104 /***********************************************************************
108 ir_builder* ir_builder_new(const char *modulename)
112 self = (ir_builder*)mem_a(sizeof(*self));
116 MEM_VECTOR_INIT(self, functions);
117 MEM_VECTOR_INIT(self, globals);
119 if (!ir_builder_set_name(self, modulename)) {
124 /* globals which always exist */
126 /* for now we give it a vector size */
127 ir_builder_create_global(self, "OFS_RETURN", TYPE_VARIANT);
132 MEM_VEC_FUNCTIONS(ir_builder, ir_value*, globals)
133 MEM_VEC_FUNCTIONS(ir_builder, ir_function*, functions)
135 void ir_builder_delete(ir_builder* self)
138 mem_d((void*)self->name);
139 for (i = 0; i != self->functions_count; ++i) {
140 ir_function_delete(self->functions[i]);
142 MEM_VECTOR_CLEAR(self, functions);
143 for (i = 0; i != self->globals_count; ++i) {
144 ir_value_delete(self->globals[i]);
146 MEM_VECTOR_CLEAR(self, globals);
150 bool ir_builder_set_name(ir_builder *self, const char *name)
153 mem_d((void*)self->name);
154 self->name = util_strdup(name);
158 ir_function* ir_builder_get_function(ir_builder *self, const char *name)
161 for (i = 0; i < self->functions_count; ++i) {
162 if (!strcmp(name, self->functions[i]->name))
163 return self->functions[i];
168 ir_function* ir_builder_create_function(ir_builder *self, const char *name, int outtype)
170 ir_function *fn = ir_builder_get_function(self, name);
175 fn = ir_function_new(self, outtype);
176 if (!ir_function_set_name(fn, name) ||
177 !ir_builder_functions_add(self, fn) )
179 ir_function_delete(fn);
183 fn->value = ir_builder_create_global(self, fn->name, TYPE_FUNCTION);
185 ir_function_delete(fn);
189 fn->value->isconst = true;
190 fn->value->outtype = outtype;
191 fn->value->constval.vfunc = fn;
192 fn->value->context = fn->context;
197 ir_value* ir_builder_get_global(ir_builder *self, const char *name)
200 for (i = 0; i < self->globals_count; ++i) {
201 if (!strcmp(self->globals[i]->name, name))
202 return self->globals[i];
207 ir_value* ir_builder_create_global(ir_builder *self, const char *name, int vtype)
211 if (name && name[0] != '#')
213 ve = ir_builder_get_global(self, name);
219 ve = ir_value_var(name, store_global, vtype);
220 if (!ir_builder_globals_add(self, ve)) {
227 /***********************************************************************
231 bool ir_function_naive_phi(ir_function*);
232 void ir_function_enumerate(ir_function*);
233 bool ir_function_calculate_liferanges(ir_function*);
234 bool ir_function_allocate_locals(ir_function*);
236 ir_function* ir_function_new(ir_builder* owner, int outtype)
239 self = (ir_function*)mem_a(sizeof(*self));
245 if (!ir_function_set_name(self, "<@unnamed>")) {
250 self->context.file = "<@no context>";
251 self->context.line = 0;
252 self->outtype = outtype;
255 MEM_VECTOR_INIT(self, params);
256 MEM_VECTOR_INIT(self, blocks);
257 MEM_VECTOR_INIT(self, values);
258 MEM_VECTOR_INIT(self, locals);
263 MEM_VEC_FUNCTIONS(ir_function, ir_value*, values)
264 MEM_VEC_FUNCTIONS(ir_function, ir_block*, blocks)
265 MEM_VEC_FUNCTIONS(ir_function, ir_value*, locals)
266 MEM_VEC_FUNCTIONS(ir_function, int, params)
268 bool ir_function_set_name(ir_function *self, const char *name)
271 mem_d((void*)self->name);
272 self->name = util_strdup(name);
276 void ir_function_delete(ir_function *self)
279 mem_d((void*)self->name);
281 for (i = 0; i != self->blocks_count; ++i)
282 ir_block_delete(self->blocks[i]);
283 MEM_VECTOR_CLEAR(self, blocks);
285 MEM_VECTOR_CLEAR(self, params);
287 for (i = 0; i != self->values_count; ++i)
288 ir_value_delete(self->values[i]);
289 MEM_VECTOR_CLEAR(self, values);
291 for (i = 0; i != self->locals_count; ++i)
292 ir_value_delete(self->locals[i]);
293 MEM_VECTOR_CLEAR(self, locals);
295 /* self->value is deleted by the builder */
300 bool GMQCC_WARN ir_function_collect_value(ir_function *self, ir_value *v)
302 return ir_function_values_add(self, v);
305 ir_block* ir_function_create_block(ir_function *self, const char *label)
307 ir_block* bn = ir_block_new(self, label);
308 memcpy(&bn->context, &self->context, sizeof(self->context));
309 if (!ir_function_blocks_add(self, bn)) {
316 bool ir_function_finalize(ir_function *self)
321 if (!ir_function_naive_phi(self))
324 ir_function_enumerate(self);
326 if (!ir_function_calculate_liferanges(self))
329 if (!ir_function_allocate_locals(self))
334 ir_value* ir_function_get_local(ir_function *self, const char *name)
337 for (i = 0; i < self->locals_count; ++i) {
338 if (!strcmp(self->locals[i]->name, name))
339 return self->locals[i];
344 ir_value* ir_function_create_local(ir_function *self, const char *name, int vtype, bool param)
346 ir_value *ve = ir_function_get_local(self, name);
352 self->locals_count &&
353 self->locals[self->locals_count-1]->store != store_param) {
354 printf("cannot add parameters after adding locals\n");
358 ve = ir_value_var(name, (param ? store_param : store_local), vtype);
359 if (!ir_function_locals_add(self, ve)) {
366 /***********************************************************************
370 ir_block* ir_block_new(ir_function* owner, const char *name)
373 self = (ir_block*)mem_a(sizeof(*self));
377 memset(self, 0, sizeof(*self));
380 if (!ir_block_set_label(self, name)) {
385 self->context.file = "<@no context>";
386 self->context.line = 0;
388 MEM_VECTOR_INIT(self, instr);
389 MEM_VECTOR_INIT(self, entries);
390 MEM_VECTOR_INIT(self, exits);
393 self->is_return = false;
395 MEM_VECTOR_INIT(self, living);
397 self->generated = false;
401 MEM_VEC_FUNCTIONS(ir_block, ir_instr*, instr)
402 MEM_VEC_FUNCTIONS_ALL(ir_block, ir_block*, entries)
403 MEM_VEC_FUNCTIONS_ALL(ir_block, ir_block*, exits)
404 MEM_VEC_FUNCTIONS_ALL(ir_block, ir_value*, living)
406 void ir_block_delete(ir_block* self)
410 for (i = 0; i != self->instr_count; ++i)
411 ir_instr_delete(self->instr[i]);
412 MEM_VECTOR_CLEAR(self, instr);
413 MEM_VECTOR_CLEAR(self, entries);
414 MEM_VECTOR_CLEAR(self, exits);
415 MEM_VECTOR_CLEAR(self, living);
419 bool ir_block_set_label(ir_block *self, const char *name)
422 mem_d((void*)self->label);
423 self->label = util_strdup(name);
424 return !!self->label;
427 /***********************************************************************
431 ir_instr* ir_instr_new(ir_block* owner, int op)
434 self = (ir_instr*)mem_a(sizeof(*self));
439 self->context.file = "<@no context>";
440 self->context.line = 0;
442 self->_ops[0] = NULL;
443 self->_ops[1] = NULL;
444 self->_ops[2] = NULL;
445 self->bops[0] = NULL;
446 self->bops[1] = NULL;
447 MEM_VECTOR_INIT(self, phi);
448 MEM_VECTOR_INIT(self, params);
453 MEM_VEC_FUNCTIONS(ir_instr, ir_phi_entry_t, phi)
454 MEM_VEC_FUNCTIONS(ir_instr, ir_value*, params)
456 void ir_instr_delete(ir_instr *self)
459 /* The following calls can only delete from
460 * vectors, we still want to delete this instruction
461 * so ignore the return value. Since with the warn_unused_result attribute
462 * gcc doesn't care about an explicit: (void)foo(); to ignore the result,
463 * I have to improvise here and use if(foo());
465 for (i = 0; i < self->phi_count; ++i) {
467 if (ir_value_writes_find(self->phi[i].value, self, &idx))
468 if (ir_value_writes_remove(self->phi[i].value, idx)) GMQCC_SUPPRESS_EMPTY_BODY;
469 if (ir_value_reads_find(self->phi[i].value, self, &idx))
470 if (ir_value_reads_remove (self->phi[i].value, idx)) GMQCC_SUPPRESS_EMPTY_BODY;
472 MEM_VECTOR_CLEAR(self, phi);
473 for (i = 0; i < self->params_count; ++i) {
475 if (ir_value_writes_find(self->params[i], self, &idx))
476 if (ir_value_writes_remove(self->params[i], idx)) GMQCC_SUPPRESS_EMPTY_BODY;
477 if (ir_value_reads_find(self->params[i], self, &idx))
478 if (ir_value_reads_remove (self->params[i], idx)) GMQCC_SUPPRESS_EMPTY_BODY;
480 MEM_VECTOR_CLEAR(self, params);
481 if (ir_instr_op(self, 0, NULL, false)) GMQCC_SUPPRESS_EMPTY_BODY;
482 if (ir_instr_op(self, 1, NULL, false)) GMQCC_SUPPRESS_EMPTY_BODY;
483 if (ir_instr_op(self, 2, NULL, false)) GMQCC_SUPPRESS_EMPTY_BODY;
487 bool ir_instr_op(ir_instr *self, int op, ir_value *v, bool writing)
489 if (self->_ops[op]) {
491 if (writing && ir_value_writes_find(self->_ops[op], self, &idx))
493 if (!ir_value_writes_remove(self->_ops[op], idx))
496 else if (ir_value_reads_find(self->_ops[op], self, &idx))
498 if (!ir_value_reads_remove(self->_ops[op], idx))
504 if (!ir_value_writes_add(v, self))
507 if (!ir_value_reads_add(v, self))
515 /***********************************************************************
519 ir_value* ir_value_var(const char *name, int storetype, int vtype)
522 self = (ir_value*)mem_a(sizeof(*self));
524 self->fieldtype = TYPE_VOID;
525 self->outtype = TYPE_VOID;
526 self->store = storetype;
527 MEM_VECTOR_INIT(self, reads);
528 MEM_VECTOR_INIT(self, writes);
529 self->isconst = false;
530 self->context.file = "<@no context>";
531 self->context.line = 0;
533 ir_value_set_name(self, name);
535 memset(&self->constval, 0, sizeof(self->constval));
536 memset(&self->code, 0, sizeof(self->code));
538 MEM_VECTOR_INIT(self, life);
541 MEM_VEC_FUNCTIONS(ir_value, ir_life_entry_t, life)
542 MEM_VEC_FUNCTIONS_ALL(ir_value, ir_instr*, reads)
543 MEM_VEC_FUNCTIONS_ALL(ir_value, ir_instr*, writes)
545 ir_value* ir_value_out(ir_function *owner, const char *name, int storetype, int vtype)
547 ir_value *v = ir_value_var(name, storetype, vtype);
550 if (!ir_function_collect_value(owner, v))
558 void ir_value_delete(ir_value* self)
561 mem_d((void*)self->name);
564 if (self->vtype == TYPE_STRING)
565 mem_d((void*)self->constval.vstring);
567 MEM_VECTOR_CLEAR(self, reads);
568 MEM_VECTOR_CLEAR(self, writes);
569 MEM_VECTOR_CLEAR(self, life);
573 void ir_value_set_name(ir_value *self, const char *name)
576 mem_d((void*)self->name);
577 self->name = util_strdup(name);
580 bool ir_value_set_float(ir_value *self, float f)
582 if (self->vtype != TYPE_FLOAT)
584 self->constval.vfloat = f;
585 self->isconst = true;
589 bool ir_value_set_func(ir_value *self, int f)
591 if (self->vtype != TYPE_FUNCTION)
593 self->constval.vint = f;
594 self->isconst = true;
598 bool ir_value_set_vector(ir_value *self, vector v)
600 if (self->vtype != TYPE_VECTOR)
602 self->constval.vvec = v;
603 self->isconst = true;
607 bool ir_value_set_quaternion(ir_value *self, quaternion v)
609 if (self->vtype != TYPE_QUATERNION)
611 memcpy(&self->constval.vquat, v, sizeof(self->constval.vquat));
612 self->isconst = true;
616 bool ir_value_set_matrix(ir_value *self, matrix v)
618 if (self->vtype != TYPE_MATRIX)
620 memcpy(&self->constval.vmat, v, sizeof(self->constval.vmat));
621 self->isconst = true;
625 bool ir_value_set_string(ir_value *self, const char *str)
627 if (self->vtype != TYPE_STRING)
629 self->constval.vstring = util_strdup(str);
630 self->isconst = true;
635 bool ir_value_set_int(ir_value *self, int i)
637 if (self->vtype != TYPE_INTEGER)
639 self->constval.vint = i;
640 self->isconst = true;
645 bool ir_value_lives(ir_value *self, size_t at)
648 for (i = 0; i < self->life_count; ++i)
650 ir_life_entry_t *life = &self->life[i];
651 if (life->start <= at && at <= life->end)
653 if (life->start > at) /* since it's ordered */
659 bool ir_value_life_insert(ir_value *self, size_t idx, ir_life_entry_t e)
662 if (!ir_value_life_add(self, e)) /* naive... */
664 for (k = self->life_count-1; k > idx; --k)
665 self->life[k] = self->life[k-1];
670 bool ir_value_life_merge(ir_value *self, size_t s)
673 ir_life_entry_t *life = NULL;
674 ir_life_entry_t *before = NULL;
675 ir_life_entry_t new_entry;
677 /* Find the first range >= s */
678 for (i = 0; i < self->life_count; ++i)
681 life = &self->life[i];
685 /* nothing found? append */
686 if (i == self->life_count) {
688 if (life && life->end+1 == s)
690 /* previous life range can be merged in */
694 if (life && life->end >= s)
697 if (!ir_value_life_add(self, e))
698 return false; /* failing */
704 if (before->end + 1 == s &&
705 life->start - 1 == s)
708 before->end = life->end;
709 if (!ir_value_life_remove(self, i))
710 return false; /* failing */
713 if (before->end + 1 == s)
719 /* already contained */
720 if (before->end >= s)
724 if (life->start - 1 == s)
729 /* insert a new entry */
730 new_entry.start = new_entry.end = s;
731 return ir_value_life_insert(self, i, new_entry);
734 bool ir_value_life_merge_into(ir_value *self, const ir_value *other)
738 if (!other->life_count)
741 if (!self->life_count) {
742 for (i = 0; i < other->life_count; ++i) {
743 if (!ir_value_life_add(self, other->life[i]))
750 for (i = 0; i < other->life_count; ++i)
752 const ir_life_entry_t *life = &other->life[i];
755 ir_life_entry_t *entry = &self->life[myi];
757 if (life->end+1 < entry->start)
759 /* adding an interval before entry */
760 if (!ir_value_life_insert(self, myi, *life))
766 if (life->start < entry->start &&
767 life->end >= entry->start)
769 /* starts earlier and overlaps */
770 entry->start = life->start;
773 if (life->end > entry->end &&
774 life->start-1 <= entry->end)
776 /* ends later and overlaps */
777 entry->end = life->end;
780 /* see if our change combines it with the next ranges */
781 while (myi+1 < self->life_count &&
782 entry->end+1 >= self->life[1+myi].start)
784 /* overlaps with (myi+1) */
785 if (entry->end < self->life[1+myi].end)
786 entry->end = self->life[1+myi].end;
787 if (!ir_value_life_remove(self, myi+1))
789 entry = &self->life[myi];
792 /* see if we're after the entry */
793 if (life->start > entry->end)
796 /* append if we're at the end */
797 if (myi >= self->life_count) {
798 if (!ir_value_life_add(self, *life))
802 /* otherweise check the next range */
811 bool ir_values_overlap(const ir_value *a, const ir_value *b)
813 /* For any life entry in A see if it overlaps with
814 * any life entry in B.
815 * Note that the life entries are orderes, so we can make a
816 * more efficient algorithm there than naively translating the
820 ir_life_entry_t *la, *lb, *enda, *endb;
822 /* first of all, if either has no life range, they cannot clash */
823 if (!a->life_count || !b->life_count)
828 enda = la + a->life_count;
829 endb = lb + b->life_count;
832 /* check if the entries overlap, for that,
833 * both must start before the other one ends.
835 #if defined(LIFE_RANGE_WITHOUT_LAST_READ)
836 if (la->start <= lb->end &&
837 lb->start <= la->end)
839 if (la->start < lb->end &&
846 /* entries are ordered
847 * one entry is earlier than the other
848 * that earlier entry will be moved forward
850 if (la->start < lb->start)
852 /* order: A B, move A forward
853 * check if we hit the end with A
858 else if (lb->start < la->start)
860 /* order: B A, move B forward
861 * check if we hit the end with B
870 /***********************************************************************
874 bool ir_block_create_store_op(ir_block *self, int op, ir_value *target, ir_value *what)
876 if (target->store == store_value) {
877 fprintf(stderr, "cannot store to an SSA value\n");
878 fprintf(stderr, "trying to store: %s <- %s\n", target->name, what->name);
881 ir_instr *in = ir_instr_new(self, op);
884 if (!ir_instr_op(in, 0, target, true) ||
885 !ir_instr_op(in, 1, what, false) ||
886 !ir_block_instr_add(self, in) )
894 bool ir_block_create_store(ir_block *self, ir_value *target, ir_value *what)
898 if (target->vtype == TYPE_VARIANT)
901 vtype = target->vtype;
904 if (vtype == TYPE_FLOAT && what->vtype == TYPE_INTEGER)
905 op = INSTR_CONV_ITOF;
906 else if (vtype == TYPE_INTEGER && what->vtype == TYPE_FLOAT)
907 op = INSTR_CONV_FTOI;
909 op = type_store_instr[vtype];
911 return ir_block_create_store_op(self, op, target, what);
914 bool ir_block_create_storep(ir_block *self, ir_value *target, ir_value *what)
919 if (target->vtype != TYPE_POINTER)
922 /* storing using pointer - target is a pointer, type must be
923 * inferred from source
927 op = type_storep_instr[vtype];
928 return ir_block_create_store_op(self, op, target, what);
931 bool ir_block_create_return(ir_block *self, ir_value *v)
935 fprintf(stderr, "block already ended (%s)\n", self->label);
939 self->is_return = true;
940 in = ir_instr_new(self, INSTR_RETURN);
944 if (!ir_instr_op(in, 0, v, false) ||
945 !ir_block_instr_add(self, in) )
952 bool ir_block_create_if(ir_block *self, ir_value *v,
953 ir_block *ontrue, ir_block *onfalse)
957 fprintf(stderr, "block already ended (%s)\n", self->label);
961 /*in = ir_instr_new(self, (v->vtype == TYPE_STRING ? INSTR_IF_S : INSTR_IF_F));*/
962 in = ir_instr_new(self, VINSTR_COND);
966 if (!ir_instr_op(in, 0, v, false)) {
971 in->bops[0] = ontrue;
972 in->bops[1] = onfalse;
974 if (!ir_block_instr_add(self, in))
977 if (!ir_block_exits_add(self, ontrue) ||
978 !ir_block_exits_add(self, onfalse) ||
979 !ir_block_entries_add(ontrue, self) ||
980 !ir_block_entries_add(onfalse, self) )
987 bool ir_block_create_jump(ir_block *self, ir_block *to)
991 fprintf(stderr, "block already ended (%s)\n", self->label);
995 in = ir_instr_new(self, VINSTR_JUMP);
1000 if (!ir_block_instr_add(self, in))
1003 if (!ir_block_exits_add(self, to) ||
1004 !ir_block_entries_add(to, self) )
1011 bool ir_block_create_goto(ir_block *self, ir_block *to)
1015 fprintf(stderr, "block already ended (%s)\n", self->label);
1019 in = ir_instr_new(self, INSTR_GOTO);
1024 if (!ir_block_instr_add(self, in))
1027 if (!ir_block_exits_add(self, to) ||
1028 !ir_block_entries_add(to, self) )
1035 ir_instr* ir_block_create_phi(ir_block *self, const char *label, int ot)
1039 in = ir_instr_new(self, VINSTR_PHI);
1042 out = ir_value_out(self->owner, label, store_value, ot);
1044 ir_instr_delete(in);
1047 if (!ir_instr_op(in, 0, out, true)) {
1048 ir_instr_delete(in);
1049 ir_value_delete(out);
1052 if (!ir_block_instr_add(self, in)) {
1053 ir_instr_delete(in);
1054 ir_value_delete(out);
1060 ir_value* ir_phi_value(ir_instr *self)
1062 return self->_ops[0];
1065 bool ir_phi_add(ir_instr* self, ir_block *b, ir_value *v)
1069 if (!ir_block_entries_find(self->owner, b, NULL)) {
1070 /* Must not be possible to cause this, otherwise the AST
1071 * is doing something wrong.
1073 fprintf(stderr, "Invalid entry block for PHI\n");
1079 if (!ir_value_reads_add(v, self))
1081 return ir_instr_phi_add(self, pe);
1084 /* call related code */
1085 ir_instr* ir_block_create_call(ir_block *self, const char *label, ir_value *func)
1089 in = ir_instr_new(self, INSTR_CALL0);
1092 out = ir_value_out(self->owner, label, store_return, func->outtype);
1094 ir_instr_delete(in);
1097 if (!ir_instr_op(in, 0, out, true) ||
1098 !ir_instr_op(in, 1, func, false) ||
1099 !ir_block_instr_add(self, in))
1101 ir_instr_delete(in);
1102 ir_value_delete(out);
1108 ir_value* ir_call_value(ir_instr *self)
1110 return self->_ops[0];
1113 bool ir_call_param(ir_instr* self, ir_value *v)
1115 if (!ir_instr_params_add(self, v))
1117 if (!ir_value_reads_add(v, self)) {
1118 if (!ir_instr_params_remove(self, self->params_count-1))
1119 GMQCC_SUPPRESS_EMPTY_BODY;
1125 /* binary op related code */
1127 ir_value* ir_block_create_binop(ir_block *self,
1128 const char *label, int opcode,
1129 ir_value *left, ir_value *right)
1151 case INSTR_SUB_S: /* -- offset of string as float */
1156 case INSTR_BITOR_IF:
1157 case INSTR_BITOR_FI:
1158 case INSTR_BITAND_FI:
1159 case INSTR_BITAND_IF:
1174 case INSTR_BITAND_I:
1177 case INSTR_RSHIFT_I:
1178 case INSTR_LSHIFT_I:
1200 /* boolean operations result in floats */
1201 if (opcode >= INSTR_EQ_F && opcode <= INSTR_GT)
1203 else if (opcode >= INSTR_LE && opcode <= INSTR_GT)
1206 else if (opcode >= INSTR_LE_I && opcode <= INSTR_EQ_FI)
1211 if (ot == TYPE_VOID) {
1212 /* The AST or parser were supposed to check this! */
1216 return ir_block_create_general_instr(self, label, opcode, left, right, ot);
1219 ir_value* ir_block_create_general_instr(ir_block *self, const char *label,
1220 int op, ir_value *a, ir_value *b, int outype)
1225 out = ir_value_out(self->owner, label, store_value, outype);
1229 instr = ir_instr_new(self, op);
1231 ir_value_delete(out);
1235 if (!ir_instr_op(instr, 0, out, true) ||
1236 !ir_instr_op(instr, 1, a, false) ||
1237 !ir_instr_op(instr, 2, b, false) )
1242 if (!ir_block_instr_add(self, instr))
1247 ir_instr_delete(instr);
1248 ir_value_delete(out);
1252 ir_value* ir_block_create_fieldaddress(ir_block *self, const char *label, ir_value *ent, ir_value *field)
1254 /* Support for various pointer types todo if so desired */
1255 if (ent->vtype != TYPE_ENTITY)
1258 if (field->vtype != TYPE_FIELD)
1261 return ir_block_create_general_instr(self, label, INSTR_ADDRESS, ent, field, TYPE_POINTER);
1264 ir_value* ir_block_create_load_from_ent(ir_block *self, const char *label, ir_value *ent, ir_value *field, int outype)
1267 if (ent->vtype != TYPE_ENTITY)
1270 /* at some point we could redirect for TYPE_POINTER... but that could lead to carelessness */
1271 if (field->vtype != TYPE_FIELD)
1276 case TYPE_FLOAT: op = INSTR_LOAD_F; break;
1277 case TYPE_VECTOR: op = INSTR_LOAD_V; break;
1278 case TYPE_STRING: op = INSTR_LOAD_S; break;
1279 case TYPE_FIELD: op = INSTR_LOAD_FLD; break;
1280 case TYPE_ENTITY: op = INSTR_LOAD_ENT; break;
1282 case TYPE_POINTER: op = INSTR_LOAD_I; break;
1283 case TYPE_INTEGER: op = INSTR_LOAD_I; break;
1285 case TYPE_QUATERNION: op = INSTR_LOAD_Q; break;
1286 case TYPE_MATRIX: op = INSTR_LOAD_M; break;
1291 return ir_block_create_general_instr(self, label, op, ent, field, outype);
1294 ir_value* ir_block_create_add(ir_block *self,
1296 ir_value *left, ir_value *right)
1299 int l = left->vtype;
1300 int r = right->vtype;
1319 if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1321 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1327 return ir_block_create_binop(self, label, op, left, right);
1330 ir_value* ir_block_create_sub(ir_block *self,
1332 ir_value *left, ir_value *right)
1335 int l = left->vtype;
1336 int r = right->vtype;
1356 if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1358 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1364 return ir_block_create_binop(self, label, op, left, right);
1367 ir_value* ir_block_create_mul(ir_block *self,
1369 ir_value *left, ir_value *right)
1372 int l = left->vtype;
1373 int r = right->vtype;
1390 case TYPE_QUATERNION:
1398 if ( (l == TYPE_VECTOR && r == TYPE_FLOAT) )
1400 else if ( (l == TYPE_FLOAT && r == TYPE_VECTOR) )
1402 else if ( (l == TYPE_QUATERNION && r == TYPE_FLOAT) )
1404 else if ( (l == TYPE_MATRIX && r == TYPE_FLOAT) )
1407 else if ( (l == TYPE_VECTOR && r == TYPE_INTEGER) )
1409 else if ( (l == TYPE_INTEGER && r == TYPE_VECTOR) )
1411 else if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1413 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1419 return ir_block_create_binop(self, label, op, left, right);
1422 ir_value* ir_block_create_div(ir_block *self,
1424 ir_value *left, ir_value *right)
1427 int l = left->vtype;
1428 int r = right->vtype;
1445 if ( (l == TYPE_VECTOR && r == TYPE_FLOAT) )
1447 else if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1449 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1455 return ir_block_create_binop(self, label, op, left, right);
1458 /* PHI resolving breaks the SSA, and must thus be the last
1459 * step before life-range calculation.
1462 static bool ir_block_naive_phi(ir_block *self);
1463 bool ir_function_naive_phi(ir_function *self)
1467 for (i = 0; i < self->blocks_count; ++i)
1469 if (!ir_block_naive_phi(self->blocks[i]))
1475 static bool ir_naive_phi_emit_store(ir_block *block, size_t iid, ir_value *old, ir_value *what)
1480 /* create a store */
1481 if (!ir_block_create_store(block, old, what))
1484 /* we now move it up */
1485 instr = block->instr[block->instr_count-1];
1486 for (i = block->instr_count; i > iid; --i)
1487 block->instr[i] = block->instr[i-1];
1488 block->instr[i] = instr;
1493 static bool ir_block_naive_phi(ir_block *self)
1496 /* FIXME: optionally, create_phi can add the phis
1497 * to a list so we don't need to loop through blocks
1498 * - anyway: "don't optimize YET"
1500 for (i = 0; i < self->instr_count; ++i)
1502 ir_instr *instr = self->instr[i];
1503 if (instr->opcode != VINSTR_PHI)
1506 if (!ir_block_instr_remove(self, i))
1508 --i; /* NOTE: i+1 below */
1510 for (p = 0; p < instr->phi_count; ++p)
1512 ir_value *v = instr->phi[p].value;
1513 for (w = 0; w < v->writes_count; ++w) {
1516 if (!v->writes[w]->_ops[0])
1519 /* When the write was to a global, we have to emit a mov */
1520 old = v->writes[w]->_ops[0];
1522 /* The original instruction now writes to the PHI target local */
1523 if (v->writes[w]->_ops[0] == v)
1524 v->writes[w]->_ops[0] = instr->_ops[0];
1526 if (old->store != store_value && old->store != store_local && old->store != store_param)
1528 /* If it originally wrote to a global we need to store the value
1531 if (!ir_naive_phi_emit_store(self, i+1, old, v))
1533 if (i+1 < self->instr_count)
1534 instr = self->instr[i+1];
1537 /* In case I forget and access instr later, it'll be NULL
1538 * when it's a problem, to make sure we crash, rather than accessing
1544 /* If it didn't, we can replace all reads by the phi target now. */
1546 for (r = 0; r < old->reads_count; ++r)
1549 ir_instr *ri = old->reads[r];
1550 for (op = 0; op < ri->phi_count; ++op) {
1551 if (ri->phi[op].value == old)
1552 ri->phi[op].value = v;
1554 for (op = 0; op < 3; ++op) {
1555 if (ri->_ops[op] == old)
1562 ir_instr_delete(instr);
1567 /***********************************************************************
1568 *IR Temp allocation code
1569 * Propagating value life ranges by walking through the function backwards
1570 * until no more changes are made.
1571 * In theory this should happen once more than once for every nested loop
1573 * Though this implementation might run an additional time for if nests.
1582 MEM_VEC_FUNCTIONS_ALL(new_reads_t, ir_value*, v)
1584 /* Enumerate instructions used by value's life-ranges
1586 static void ir_block_enumerate(ir_block *self, size_t *_eid)
1590 for (i = 0; i < self->instr_count; ++i)
1592 self->instr[i]->eid = eid++;
1597 /* Enumerate blocks and instructions.
1598 * The block-enumeration is unordered!
1599 * We do not really use the block enumreation, however
1600 * the instruction enumeration is important for life-ranges.
1602 void ir_function_enumerate(ir_function *self)
1605 size_t instruction_id = 0;
1606 for (i = 0; i < self->blocks_count; ++i)
1608 self->blocks[i]->eid = i;
1609 self->blocks[i]->run_id = 0;
1610 ir_block_enumerate(self->blocks[i], &instruction_id);
1614 static bool ir_block_life_propagate(ir_block *b, ir_block *prev, bool *changed);
1615 bool ir_function_calculate_liferanges(ir_function *self)
1623 for (i = 0; i != self->blocks_count; ++i)
1625 if (self->blocks[i]->is_return)
1627 if (!ir_block_life_propagate(self->blocks[i], NULL, &changed))
1635 /* Local-value allocator
1636 * After finishing creating the liferange of all values used in a function
1637 * we can allocate their global-positions.
1638 * This is the counterpart to register-allocation in register machines.
1641 MEM_VECTOR_MAKE(ir_value*, locals);
1642 MEM_VECTOR_MAKE(size_t, sizes);
1643 MEM_VECTOR_MAKE(size_t, positions);
1644 } function_allocator;
1645 MEM_VEC_FUNCTIONS(function_allocator, ir_value*, locals)
1646 MEM_VEC_FUNCTIONS(function_allocator, size_t, sizes)
1647 MEM_VEC_FUNCTIONS(function_allocator, size_t, positions)
1649 static bool function_allocator_alloc(function_allocator *alloc, const ir_value *var)
1652 size_t vsize = type_sizeof[var->vtype];
1654 slot = ir_value_var("reg", store_global, var->vtype);
1658 if (!ir_value_life_merge_into(slot, var))
1661 if (!function_allocator_locals_add(alloc, slot))
1664 if (!function_allocator_sizes_add(alloc, vsize))
1670 ir_value_delete(slot);
1674 bool ir_function_allocate_locals(ir_function *self)
1683 function_allocator alloc;
1685 if (!self->locals_count)
1688 MEM_VECTOR_INIT(&alloc, locals);
1689 MEM_VECTOR_INIT(&alloc, sizes);
1690 MEM_VECTOR_INIT(&alloc, positions);
1692 for (i = 0; i < self->locals_count; ++i)
1694 if (!function_allocator_alloc(&alloc, self->locals[i]))
1698 /* Allocate a slot for any value that still exists */
1699 for (i = 0; i < self->values_count; ++i)
1701 v = self->values[i];
1706 for (a = 0; a < alloc.locals_count; ++a)
1708 slot = alloc.locals[a];
1710 if (ir_values_overlap(v, slot))
1713 if (!ir_value_life_merge_into(slot, v))
1716 /* adjust size for this slot */
1717 if (alloc.sizes[a] < type_sizeof[v->vtype])
1718 alloc.sizes[a] = type_sizeof[v->vtype];
1720 self->values[i]->code.local = a;
1723 if (a >= alloc.locals_count) {
1724 self->values[i]->code.local = alloc.locals_count;
1725 if (!function_allocator_alloc(&alloc, v))
1730 /* Adjust slot positions based on sizes */
1731 if (!function_allocator_positions_add(&alloc, 0))
1734 if (alloc.sizes_count)
1735 pos = alloc.positions[0] + alloc.sizes[0];
1738 for (i = 1; i < alloc.sizes_count; ++i)
1740 pos = alloc.positions[i-1] + alloc.sizes[i-1];
1741 if (!function_allocator_positions_add(&alloc, pos))
1745 self->allocated_locals = pos + alloc.sizes[alloc.sizes_count-1];
1747 /* Take over the actual slot positions */
1748 for (i = 0; i < self->values_count; ++i)
1749 self->values[i]->code.local = alloc.positions[self->values[i]->code.local];
1756 for (i = 0; i < alloc.locals_count; ++i)
1757 ir_value_delete(alloc.locals[i]);
1758 MEM_VECTOR_CLEAR(&alloc, locals);
1759 MEM_VECTOR_CLEAR(&alloc, sizes);
1760 MEM_VECTOR_CLEAR(&alloc, positions);
1764 /* Get information about which operand
1765 * is read from, or written to.
1767 static void ir_op_read_write(int op, size_t *read, size_t *write)
1794 static bool ir_block_living_add_instr(ir_block *self, size_t eid)
1797 bool changed = false;
1799 for (i = 0; i != self->living_count; ++i)
1801 tempbool = ir_value_life_merge(self->living[i], eid);
1804 fprintf(stderr, "block_living_add_instr() value instruction added %s: %i\n", self->living[i]->_name, (int)eid);
1806 changed = changed || tempbool;
1811 static bool ir_block_life_prop_previous(ir_block* self, ir_block *prev, bool *changed)
1814 /* values which have been read in a previous iteration are now
1815 * in the "living" array even if the previous block doesn't use them.
1816 * So we have to remove whatever does not exist in the previous block.
1817 * They will be re-added on-read, but the liferange merge won't cause
1820 for (i = 0; i < self->living_count; ++i)
1822 if (!ir_block_living_find(prev, self->living[i], NULL)) {
1823 if (!ir_block_living_remove(self, i))
1829 /* Whatever the previous block still has in its living set
1830 * must now be added to ours as well.
1832 for (i = 0; i < prev->living_count; ++i)
1834 if (ir_block_living_find(self, prev->living[i], NULL))
1836 if (!ir_block_living_add(self, prev->living[i]))
1839 printf("%s got from prev: %s\n", self->label, prev->living[i]->_name);
1845 static bool ir_block_life_propagate(ir_block *self, ir_block *prev, bool *changed)
1851 /* bitmasks which operands are read from or written to */
1853 #if defined(LIFE_RANGE_WITHOUT_LAST_READ)
1855 new_reads_t new_reads;
1857 char dbg_ind[16] = { '#', '0' };
1860 #if defined(LIFE_RANGE_WITHOUT_LAST_READ)
1861 MEM_VECTOR_INIT(&new_reads, v);
1866 if (!ir_block_life_prop_previous(self, prev, changed))
1870 i = self->instr_count;
1873 instr = self->instr[i];
1875 /* PHI operands are always read operands */
1876 for (p = 0; p < instr->phi_count; ++p)
1878 value = instr->phi[p].value;
1879 #if ! defined(LIFE_RANGE_WITHOUT_LAST_READ)
1880 if (!ir_block_living_find(self, value, NULL) &&
1881 !ir_block_living_add(self, value))
1886 if (!new_reads_t_v_find(&new_reads, value, NULL))
1888 if (!new_reads_t_v_add(&new_reads, value))
1894 /* See which operands are read and write operands */
1895 ir_op_read_write(instr->opcode, &read, &write);
1897 /* Go through the 3 main operands */
1898 for (o = 0; o < 3; ++o)
1900 if (!instr->_ops[o]) /* no such operand */
1903 value = instr->_ops[o];
1905 /* We only care about locals */
1906 /* we also calculate parameter liferanges so that locals
1907 * can take up parameter slots */
1908 if (value->store != store_value &&
1909 value->store != store_local &&
1910 value->store != store_param)
1916 #if ! defined(LIFE_RANGE_WITHOUT_LAST_READ)
1917 if (!ir_block_living_find(self, value, NULL) &&
1918 !ir_block_living_add(self, value))
1923 /* fprintf(stderr, "read: %s\n", value->_name); */
1924 if (!new_reads_t_v_find(&new_reads, value, NULL))
1926 if (!new_reads_t_v_add(&new_reads, value))
1932 /* write operands */
1933 /* When we write to a local, we consider it "dead" for the
1934 * remaining upper part of the function, since in SSA a value
1935 * can only be written once (== created)
1940 bool in_living = ir_block_living_find(self, value, &idx);
1941 #if defined(LIFE_RANGE_WITHOUT_LAST_READ)
1943 bool in_reads = new_reads_t_v_find(&new_reads, value, &readidx);
1944 if (!in_living && !in_reads)
1949 /* If the value isn't alive it hasn't been read before... */
1950 /* TODO: See if the warning can be emitted during parsing or AST processing
1951 * otherwise have warning printed here.
1952 * IF printing a warning here: include filecontext_t,
1953 * and make sure it's only printed once
1954 * since this function is run multiple times.
1956 /* For now: debug info: */
1957 fprintf(stderr, "Value only written %s\n", value->name);
1958 tempbool = ir_value_life_merge(value, instr->eid);
1959 *changed = *changed || tempbool;
1961 ir_instr_dump(instr, dbg_ind, printf);
1965 /* since 'living' won't contain it
1966 * anymore, merge the value, since
1969 tempbool = ir_value_life_merge(value, instr->eid);
1972 fprintf(stderr, "value added id %s %i\n", value->name, (int)instr->eid);
1974 *changed = *changed || tempbool;
1976 #if ! defined(LIFE_RANGE_WITHOUT_LAST_READ)
1977 if (!ir_block_living_remove(self, idx))
1982 if (!new_reads_t_v_remove(&new_reads, readidx))
1990 tempbool = ir_block_living_add_instr(self, instr->eid);
1991 /*fprintf(stderr, "living added values\n");*/
1992 *changed = *changed || tempbool;
1994 #if defined(LIFE_RANGE_WITHOUT_LAST_READ)
1996 for (rd = 0; rd < new_reads.v_count; ++rd)
1998 if (!ir_block_living_find(self, new_reads.v[rd], NULL)) {
1999 if (!ir_block_living_add(self, new_reads.v[rd]))
2002 if (!i && !self->entries_count) {
2004 *changed = *changed || ir_value_life_merge(new_reads.v[rd], instr->eid);
2007 MEM_VECTOR_CLEAR(&new_reads, v);
2011 if (self->run_id == self->owner->run_id)
2014 self->run_id = self->owner->run_id;
2016 for (i = 0; i < self->entries_count; ++i)
2018 ir_block *entry = self->entries[i];
2019 ir_block_life_propagate(entry, self, changed);
2024 #if defined(LIFE_RANGE_WITHOUT_LAST_READ)
2025 MEM_VECTOR_CLEAR(&new_reads, v);
2030 /***********************************************************************
2033 * Since the IR has the convention of putting 'write' operands
2034 * at the beginning, we have to rotate the operands of instructions
2035 * properly in order to generate valid QCVM code.
2037 * Having destinations at a fixed position is more convenient. In QC
2038 * this is *mostly* OPC, but FTE adds at least 2 instructions which
2039 * read from from OPA, and store to OPB rather than OPC. Which is
2040 * partially the reason why the implementation of these instructions
2041 * in darkplaces has been delayed for so long.
2043 * Breaking conventions is annoying...
2045 static bool ir_builder_gen_global(ir_builder *self, ir_value *global);
2047 static bool gen_global_field(ir_value *global)
2049 if (global->isconst)
2051 ir_value *fld = global->constval.vpointer;
2053 printf("Invalid field constant with no field: %s\n", global->name);
2057 /* Now, in this case, a relocation would be impossible to code
2058 * since it looks like this:
2059 * .vector v = origin; <- parse error, wtf is 'origin'?
2062 * But we will need a general relocation support later anyway
2063 * for functions... might as well support that here.
2065 if (!fld->code.globaladdr) {
2066 printf("FIXME: Relocation support\n");
2070 /* copy the field's value */
2071 global->code.globaladdr = code_globals_add(code_globals_data[fld->code.globaladdr]);
2075 prog_section_field fld;
2077 fld.name = global->code.name;
2078 fld.offset = code_fields_elements;
2079 fld.type = global->fieldtype;
2081 if (fld.type == TYPE_VOID) {
2082 printf("Field is missing a type: %s\n", global->name);
2086 if (code_fields_add(fld) < 0)
2089 global->code.globaladdr = code_globals_add(fld.offset);
2091 if (global->code.globaladdr < 0)
2096 static bool gen_global_pointer(ir_value *global)
2098 if (global->isconst)
2100 ir_value *target = global->constval.vpointer;
2102 printf("Invalid pointer constant: %s\n", global->name);
2103 /* NULL pointers are pointing to the NULL constant, which also
2104 * sits at address 0, but still has an ir_value for itself.
2109 /* Here, relocations ARE possible - in fteqcc-enhanced-qc:
2110 * void() foo; <- proto
2111 * void() *fooptr = &foo;
2112 * void() foo = { code }
2114 if (!target->code.globaladdr) {
2115 /* FIXME: Check for the constant nullptr ir_value!
2116 * because then code.globaladdr being 0 is valid.
2118 printf("FIXME: Relocation support\n");
2122 global->code.globaladdr = code_globals_add(target->code.globaladdr);
2126 global->code.globaladdr = code_globals_add(0);
2128 if (global->code.globaladdr < 0)
2133 static bool gen_blocks_recursive(ir_function *func, ir_block *block)
2135 prog_section_statement stmt;
2144 block->generated = true;
2145 block->code_start = code_statements_elements;
2146 for (i = 0; i < block->instr_count; ++i)
2148 instr = block->instr[i];
2150 if (instr->opcode == VINSTR_PHI) {
2151 printf("cannot generate virtual instruction (phi)\n");
2155 if (instr->opcode == VINSTR_JUMP) {
2156 target = instr->bops[0];
2157 /* for uncoditional jumps, if the target hasn't been generated
2158 * yet, we generate them right here.
2160 if (!target->generated) {
2165 /* otherwise we generate a jump instruction */
2166 stmt.opcode = INSTR_GOTO;
2167 stmt.o1.s1 = (target->code_start) - code_statements_elements;
2170 if (code_statements_add(stmt) < 0)
2173 /* no further instructions can be in this block */
2177 if (instr->opcode == VINSTR_COND) {
2178 ontrue = instr->bops[0];
2179 onfalse = instr->bops[1];
2180 /* TODO: have the AST signal which block should
2181 * come first: eg. optimize IFs without ELSE...
2184 stmt.o1.u1 = instr->_ops[0]->code.globaladdr;
2188 if (ontrue->generated) {
2189 stmt.opcode = INSTR_IF;
2190 stmt.o2.s1 = (ontrue->code_start-1) - code_statements_elements;
2191 if (code_statements_add(stmt) < 0)
2194 if (onfalse->generated) {
2195 stmt.opcode = INSTR_IFNOT;
2196 stmt.o2.s1 = (onfalse->code_start-1) - code_statements_elements;
2197 if (code_statements_add(stmt) < 0)
2200 if (!ontrue->generated) {
2201 if (onfalse->generated) {
2206 if (!onfalse->generated) {
2207 if (ontrue->generated) {
2212 /* neither ontrue nor onfalse exist */
2213 stmt.opcode = INSTR_IFNOT;
2214 stidx = code_statements_elements;
2215 if (code_statements_add(stmt) < 0)
2217 /* on false we jump, so add ontrue-path */
2218 if (!gen_blocks_recursive(func, ontrue))
2220 /* fixup the jump address */
2221 code_statements_data[stidx].o2.s1 = code_statements_elements - stidx;
2222 /* generate onfalse path */
2223 if (onfalse->generated) {
2224 /* fixup the jump address */
2225 code_statements_data[stidx].o2.s1 = (onfalse->code_start) - (stidx);
2226 /* may have been generated in the previous recursive call */
2227 stmt.opcode = INSTR_GOTO;
2228 stmt.o1.s1 = (onfalse->code_start) - code_statements_elements;
2231 return (code_statements_add(stmt) >= 0);
2233 /* if not, generate now */
2238 if (instr->opcode >= INSTR_CALL0 && instr->opcode <= INSTR_CALL8) {
2239 /* Trivial call translation:
2240 * copy all params to OFS_PARM*
2241 * if the output's storetype is not store_return,
2242 * add append a STORE instruction!
2244 * NOTES on how to do it better without much trouble:
2245 * -) The liferanges!
2246 * Simply check the liferange of all parameters for
2247 * other CALLs. For each param with no CALL in its
2248 * liferange, we can store it in an OFS_PARM at
2249 * generation already. This would even include later
2250 * reuse.... probably... :)
2255 for (p = 0; p < instr->params_count; ++p)
2257 ir_value *param = instr->params[p];
2259 stmt.opcode = INSTR_STORE_F;
2262 stmt.opcode = type_store_instr[param->vtype];
2263 stmt.o1.u1 = param->code.globaladdr;
2264 stmt.o2.u1 = OFS_PARM0 + 3 * p;
2265 if (code_statements_add(stmt) < 0)
2268 stmt.opcode = INSTR_CALL0 + instr->params_count;
2269 if (stmt.opcode > INSTR_CALL8)
2270 stmt.opcode = INSTR_CALL8;
2271 stmt.o1.u1 = instr->_ops[1]->code.globaladdr;
2274 if (code_statements_add(stmt) < 0)
2277 retvalue = instr->_ops[0];
2278 if (retvalue && retvalue->store != store_return && retvalue->life_count)
2280 /* not to be kept in OFS_RETURN */
2281 stmt.opcode = type_store_instr[retvalue->vtype];
2282 stmt.o1.u1 = OFS_RETURN;
2283 stmt.o2.u1 = retvalue->code.globaladdr;
2285 if (code_statements_add(stmt) < 0)
2291 if (instr->opcode == INSTR_STATE) {
2292 printf("TODO: state instruction\n");
2296 stmt.opcode = instr->opcode;
2301 /* This is the general order of operands */
2303 stmt.o3.u1 = instr->_ops[0]->code.globaladdr;
2306 stmt.o1.u1 = instr->_ops[1]->code.globaladdr;
2309 stmt.o2.u1 = instr->_ops[2]->code.globaladdr;
2311 if (stmt.opcode == INSTR_RETURN || stmt.opcode == INSTR_DONE)
2313 stmt.o1.u1 = stmt.o3.u1;
2316 else if ((stmt.opcode >= INSTR_STORE_F &&
2317 stmt.opcode <= INSTR_STORE_FNC) ||
2318 (stmt.opcode >= INSTR_NOT_F &&
2319 stmt.opcode <= INSTR_NOT_FNC))
2321 /* 2-operand instructions with A -> B */
2322 stmt.o2.u1 = stmt.o3.u1;
2326 if (code_statements_add(stmt) < 0)
2332 static bool gen_function_code(ir_function *self)
2335 prog_section_statement stmt;
2337 /* Starting from entry point, we generate blocks "as they come"
2338 * for now. Dead blocks will not be translated obviously.
2340 if (!self->blocks_count) {
2341 printf("Function '%s' declared without body.\n", self->name);
2345 block = self->blocks[0];
2346 if (block->generated)
2349 if (!gen_blocks_recursive(self, block)) {
2350 printf("failed to generate blocks for '%s'\n", self->name);
2354 /* otherwise code_write crashes since it debug-prints functions until AINSTR_END */
2355 stmt.opcode = AINSTR_END;
2359 if (code_statements_add(stmt) < 0)
2364 static bool gen_global_function(ir_builder *ir, ir_value *global)
2366 prog_section_function fun;
2370 size_t local_var_end;
2372 if (!global->isconst || (!global->constval.vfunc))
2374 printf("Invalid state of function-global: not constant: %s\n", global->name);
2378 irfun = global->constval.vfunc;
2380 fun.name = global->code.name;
2381 fun.file = code_cachedstring(global->context.file);
2382 fun.profile = 0; /* always 0 */
2383 fun.nargs = irfun->params_count;
2385 for (i = 0;i < 8; ++i) {
2389 fun.argsize[i] = type_sizeof[irfun->params[i]];
2392 fun.firstlocal = code_globals_elements;
2393 fun.locals = irfun->allocated_locals + irfun->locals_count;
2396 for (i = 0; i < irfun->locals_count; ++i) {
2397 if (!ir_builder_gen_global(ir, irfun->locals[i])) {
2398 printf("Failed to generate global %s\n", irfun->locals[i]->name);
2402 if (irfun->locals_count) {
2403 ir_value *last = irfun->locals[irfun->locals_count-1];
2404 local_var_end = last->code.globaladdr;
2405 local_var_end += type_sizeof[last->vtype];
2407 for (i = 0; i < irfun->values_count; ++i)
2409 /* generate code.globaladdr for ssa values */
2410 ir_value *v = irfun->values[i];
2411 v->code.globaladdr = local_var_end + v->code.local;
2413 for (i = 0; i < irfun->locals_count; ++i) {
2414 /* fill the locals with zeros */
2415 code_globals_add(0);
2419 fun.entry = irfun->builtin;
2421 fun.entry = code_statements_elements;
2422 if (!gen_function_code(irfun)) {
2423 printf("Failed to generate code for function %s\n", irfun->name);
2428 return (code_functions_add(fun) >= 0);
2431 static bool ir_builder_gen_global(ir_builder *self, ir_value *global)
2435 prog_section_def def;
2437 def.type = global->vtype;
2438 def.offset = code_globals_elements;
2439 def.name = global->code.name = code_genstring(global->name);
2441 switch (global->vtype)
2444 if (code_defs_add(def) < 0)
2446 return gen_global_pointer(global);
2448 if (code_defs_add(def) < 0)
2450 return gen_global_field(global);
2455 if (code_defs_add(def) < 0)
2458 if (global->isconst) {
2459 iptr = (int32_t*)&global->constval.vfloat;
2460 global->code.globaladdr = code_globals_add(*iptr);
2462 global->code.globaladdr = code_globals_add(0);
2464 return global->code.globaladdr >= 0;
2468 if (code_defs_add(def) < 0)
2470 if (global->isconst)
2471 global->code.globaladdr = code_globals_add(code_cachedstring(global->constval.vstring));
2473 global->code.globaladdr = code_globals_add(0);
2474 return global->code.globaladdr >= 0;
2477 case TYPE_QUATERNION:
2481 if (code_defs_add(def) < 0)
2484 if (global->isconst) {
2485 iptr = (int32_t*)&global->constval.vvec;
2486 global->code.globaladdr = code_globals_add(iptr[0]);
2487 if (global->code.globaladdr < 0)
2489 for (d = 1; d < type_sizeof[global->vtype]; ++d)
2491 if (code_globals_add(iptr[d]) < 0)
2495 global->code.globaladdr = code_globals_add(0);
2496 if (global->code.globaladdr < 0)
2498 for (d = 1; d < type_sizeof[global->vtype]; ++d)
2500 if (code_globals_add(0) < 0)
2504 return global->code.globaladdr >= 0;
2507 if (code_defs_add(def) < 0)
2509 global->code.globaladdr = code_globals_elements;
2510 code_globals_add(code_functions_elements);
2511 return gen_global_function(self, global);
2513 /* assume biggest type */
2514 global->code.globaladdr = code_globals_add(0);
2515 for (i = 1; i < type_sizeof[TYPE_VARIANT]; ++i)
2516 code_globals_add(0);
2519 /* refuse to create 'void' type or any other fancy business. */
2520 printf("Invalid type for global variable %s\n", global->name);
2525 bool ir_builder_generate(ir_builder *self, const char *filename)
2531 for (i = 0; i < self->globals_count; ++i)
2533 if (!ir_builder_gen_global(self, self->globals[i])) {
2538 printf("writing '%s'...\n", filename);
2539 return code_write(filename);
2542 /***********************************************************************
2543 *IR DEBUG Dump functions...
2546 #define IND_BUFSZ 1024
2548 const char *qc_opname(int op)
2550 if (op < 0) return "<INVALID>";
2551 if (op < ( sizeof(asm_instr) / sizeof(asm_instr[0]) ))
2552 return asm_instr[op].m;
2554 case VINSTR_PHI: return "PHI";
2555 case VINSTR_JUMP: return "JUMP";
2556 case VINSTR_COND: return "COND";
2557 default: return "<UNK>";
2561 void ir_builder_dump(ir_builder *b, int (*oprintf)(const char*, ...))
2564 char indent[IND_BUFSZ];
2568 oprintf("module %s\n", b->name);
2569 for (i = 0; i < b->globals_count; ++i)
2572 if (b->globals[i]->isconst)
2573 oprintf("%s = ", b->globals[i]->name);
2574 ir_value_dump(b->globals[i], oprintf);
2577 for (i = 0; i < b->functions_count; ++i)
2578 ir_function_dump(b->functions[i], indent, oprintf);
2579 oprintf("endmodule %s\n", b->name);
2582 void ir_function_dump(ir_function *f, char *ind,
2583 int (*oprintf)(const char*, ...))
2586 if (f->builtin != 0) {
2587 oprintf("%sfunction %s = builtin %i\n", ind, f->name, -f->builtin);
2590 oprintf("%sfunction %s\n", ind, f->name);
2591 strncat(ind, "\t", IND_BUFSZ);
2592 if (f->locals_count)
2594 oprintf("%s%i locals:\n", ind, (int)f->locals_count);
2595 for (i = 0; i < f->locals_count; ++i) {
2596 oprintf("%s\t", ind);
2597 ir_value_dump(f->locals[i], oprintf);
2601 if (f->blocks_count)
2603 oprintf("%slife passes (check): %i\n", ind, (int)f->run_id);
2604 for (i = 0; i < f->blocks_count; ++i) {
2605 if (f->blocks[i]->run_id != f->run_id) {
2606 oprintf("%slife pass check fail! %i != %i\n", ind, (int)f->blocks[i]->run_id, (int)f->run_id);
2608 ir_block_dump(f->blocks[i], ind, oprintf);
2612 ind[strlen(ind)-1] = 0;
2613 oprintf("%sendfunction %s\n", ind, f->name);
2616 void ir_block_dump(ir_block* b, char *ind,
2617 int (*oprintf)(const char*, ...))
2620 oprintf("%s:%s\n", ind, b->label);
2621 strncat(ind, "\t", IND_BUFSZ);
2623 for (i = 0; i < b->instr_count; ++i)
2624 ir_instr_dump(b->instr[i], ind, oprintf);
2625 ind[strlen(ind)-1] = 0;
2628 void dump_phi(ir_instr *in, char *ind,
2629 int (*oprintf)(const char*, ...))
2632 oprintf("%s <- phi ", in->_ops[0]->name);
2633 for (i = 0; i < in->phi_count; ++i)
2635 oprintf("([%s] : %s) ", in->phi[i].from->label,
2636 in->phi[i].value->name);
2641 void ir_instr_dump(ir_instr *in, char *ind,
2642 int (*oprintf)(const char*, ...))
2645 const char *comma = NULL;
2647 oprintf("%s (%i) ", ind, (int)in->eid);
2649 if (in->opcode == VINSTR_PHI) {
2650 dump_phi(in, ind, oprintf);
2654 strncat(ind, "\t", IND_BUFSZ);
2656 if (in->_ops[0] && (in->_ops[1] || in->_ops[2])) {
2657 ir_value_dump(in->_ops[0], oprintf);
2658 if (in->_ops[1] || in->_ops[2])
2661 oprintf("%s\t", qc_opname(in->opcode));
2662 if (in->_ops[0] && !(in->_ops[1] || in->_ops[2])) {
2663 ir_value_dump(in->_ops[0], oprintf);
2668 for (i = 1; i != 3; ++i) {
2672 ir_value_dump(in->_ops[i], oprintf);
2680 oprintf("[%s]", in->bops[0]->label);
2684 oprintf("%s[%s]", comma, in->bops[1]->label);
2686 ind[strlen(ind)-1] = 0;
2689 void ir_value_dump(ir_value* v, int (*oprintf)(const char*, ...))
2698 oprintf("%g", v->constval.vfloat);
2701 oprintf("'%g %g %g'",
2704 v->constval.vvec.z);
2707 oprintf("(entity)");
2710 oprintf("\"%s\"", v->constval.vstring);
2714 oprintf("%i", v->constval.vint);
2719 v->constval.vpointer->name);
2723 oprintf("%s", v->name);
2727 void ir_value_dump_life(ir_value *self, int (*oprintf)(const char*,...))
2730 oprintf("Life of %s:\n", self->name);
2731 for (i = 0; i < self->life_count; ++i)
2733 oprintf(" + [%i, %i]\n", self->life[i].start, self->life[i].end);