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);
118 MEM_VECTOR_INIT(self, fields);
120 if (!ir_builder_set_name(self, modulename)) {
125 /* globals which always exist */
127 /* for now we give it a vector size */
128 ir_builder_create_global(self, "OFS_RETURN", TYPE_VARIANT);
133 MEM_VEC_FUNCTIONS(ir_builder, ir_value*, globals)
134 MEM_VEC_FUNCTIONS(ir_builder, ir_value*, fields)
135 MEM_VEC_FUNCTIONS(ir_builder, ir_function*, functions)
137 void ir_builder_delete(ir_builder* self)
140 mem_d((void*)self->name);
141 for (i = 0; i != self->functions_count; ++i) {
142 ir_function_delete(self->functions[i]);
144 MEM_VECTOR_CLEAR(self, functions);
145 for (i = 0; i != self->globals_count; ++i) {
146 ir_value_delete(self->globals[i]);
148 MEM_VECTOR_CLEAR(self, fields);
149 for (i = 0; i != self->fields_count; ++i) {
150 ir_value_delete(self->fields[i]);
152 MEM_VECTOR_CLEAR(self, fields);
156 bool ir_builder_set_name(ir_builder *self, const char *name)
159 mem_d((void*)self->name);
160 self->name = util_strdup(name);
164 ir_function* ir_builder_get_function(ir_builder *self, const char *name)
167 for (i = 0; i < self->functions_count; ++i) {
168 if (!strcmp(name, self->functions[i]->name))
169 return self->functions[i];
174 ir_function* ir_builder_create_function(ir_builder *self, const char *name, int outtype)
176 ir_function *fn = ir_builder_get_function(self, name);
181 fn = ir_function_new(self, outtype);
182 if (!ir_function_set_name(fn, name) ||
183 !ir_builder_functions_add(self, fn) )
185 ir_function_delete(fn);
189 fn->value = ir_builder_create_global(self, fn->name, TYPE_FUNCTION);
191 ir_function_delete(fn);
195 fn->value->isconst = true;
196 fn->value->outtype = outtype;
197 fn->value->constval.vfunc = fn;
198 fn->value->context = fn->context;
203 ir_value* ir_builder_get_global(ir_builder *self, const char *name)
206 for (i = 0; i < self->globals_count; ++i) {
207 if (!strcmp(self->globals[i]->name, name))
208 return self->globals[i];
213 ir_value* ir_builder_create_global(ir_builder *self, const char *name, int vtype)
217 if (name && name[0] != '#')
219 ve = ir_builder_get_global(self, name);
225 ve = ir_value_var(name, store_global, vtype);
226 if (!ir_builder_globals_add(self, ve)) {
233 ir_value* ir_builder_get_field(ir_builder *self, const char *name)
236 for (i = 0; i < self->fields_count; ++i) {
237 if (!strcmp(self->fields[i]->name, name))
238 return self->fields[i];
244 ir_value* ir_builder_create_field(ir_builder *self, const char *name, int vtype)
246 ir_value *ve = ir_builder_get_field(self, name);
251 ve = ir_value_var(name, store_global, TYPE_FIELD);
252 ve->fieldtype = vtype;
253 if (!ir_builder_fields_add(self, ve)) {
260 /***********************************************************************
264 bool ir_function_naive_phi(ir_function*);
265 void ir_function_enumerate(ir_function*);
266 bool ir_function_calculate_liferanges(ir_function*);
267 bool ir_function_allocate_locals(ir_function*);
269 ir_function* ir_function_new(ir_builder* owner, int outtype)
272 self = (ir_function*)mem_a(sizeof(*self));
278 if (!ir_function_set_name(self, "<@unnamed>")) {
283 self->context.file = "<@no context>";
284 self->context.line = 0;
285 self->outtype = outtype;
288 MEM_VECTOR_INIT(self, params);
289 MEM_VECTOR_INIT(self, blocks);
290 MEM_VECTOR_INIT(self, values);
291 MEM_VECTOR_INIT(self, locals);
296 MEM_VEC_FUNCTIONS(ir_function, ir_value*, values)
297 MEM_VEC_FUNCTIONS(ir_function, ir_block*, blocks)
298 MEM_VEC_FUNCTIONS(ir_function, ir_value*, locals)
299 MEM_VEC_FUNCTIONS(ir_function, int, params)
301 bool ir_function_set_name(ir_function *self, const char *name)
304 mem_d((void*)self->name);
305 self->name = util_strdup(name);
309 void ir_function_delete(ir_function *self)
312 mem_d((void*)self->name);
314 for (i = 0; i != self->blocks_count; ++i)
315 ir_block_delete(self->blocks[i]);
316 MEM_VECTOR_CLEAR(self, blocks);
318 MEM_VECTOR_CLEAR(self, params);
320 for (i = 0; i != self->values_count; ++i)
321 ir_value_delete(self->values[i]);
322 MEM_VECTOR_CLEAR(self, values);
324 for (i = 0; i != self->locals_count; ++i)
325 ir_value_delete(self->locals[i]);
326 MEM_VECTOR_CLEAR(self, locals);
328 /* self->value is deleted by the builder */
333 bool GMQCC_WARN ir_function_collect_value(ir_function *self, ir_value *v)
335 return ir_function_values_add(self, v);
338 ir_block* ir_function_create_block(ir_function *self, const char *label)
340 ir_block* bn = ir_block_new(self, label);
341 memcpy(&bn->context, &self->context, sizeof(self->context));
342 if (!ir_function_blocks_add(self, bn)) {
349 bool ir_function_finalize(ir_function *self)
354 if (!ir_function_naive_phi(self))
357 ir_function_enumerate(self);
359 if (!ir_function_calculate_liferanges(self))
362 if (!ir_function_allocate_locals(self))
367 ir_value* ir_function_get_local(ir_function *self, const char *name)
370 for (i = 0; i < self->locals_count; ++i) {
371 if (!strcmp(self->locals[i]->name, name))
372 return self->locals[i];
377 ir_value* ir_function_create_local(ir_function *self, const char *name, int vtype, bool param)
379 ir_value *ve = ir_function_get_local(self, name);
385 self->locals_count &&
386 self->locals[self->locals_count-1]->store != store_param) {
387 printf("cannot add parameters after adding locals\n");
391 ve = ir_value_var(name, (param ? store_param : store_local), vtype);
392 if (!ir_function_locals_add(self, ve)) {
399 /***********************************************************************
403 ir_block* ir_block_new(ir_function* owner, const char *name)
406 self = (ir_block*)mem_a(sizeof(*self));
410 memset(self, 0, sizeof(*self));
413 if (!ir_block_set_label(self, name)) {
418 self->context.file = "<@no context>";
419 self->context.line = 0;
421 MEM_VECTOR_INIT(self, instr);
422 MEM_VECTOR_INIT(self, entries);
423 MEM_VECTOR_INIT(self, exits);
426 self->is_return = false;
428 MEM_VECTOR_INIT(self, living);
430 self->generated = false;
434 MEM_VEC_FUNCTIONS(ir_block, ir_instr*, instr)
435 MEM_VEC_FUNCTIONS_ALL(ir_block, ir_block*, entries)
436 MEM_VEC_FUNCTIONS_ALL(ir_block, ir_block*, exits)
437 MEM_VEC_FUNCTIONS_ALL(ir_block, ir_value*, living)
439 void ir_block_delete(ir_block* self)
443 for (i = 0; i != self->instr_count; ++i)
444 ir_instr_delete(self->instr[i]);
445 MEM_VECTOR_CLEAR(self, instr);
446 MEM_VECTOR_CLEAR(self, entries);
447 MEM_VECTOR_CLEAR(self, exits);
448 MEM_VECTOR_CLEAR(self, living);
452 bool ir_block_set_label(ir_block *self, const char *name)
455 mem_d((void*)self->label);
456 self->label = util_strdup(name);
457 return !!self->label;
460 /***********************************************************************
464 ir_instr* ir_instr_new(ir_block* owner, int op)
467 self = (ir_instr*)mem_a(sizeof(*self));
472 self->context.file = "<@no context>";
473 self->context.line = 0;
475 self->_ops[0] = NULL;
476 self->_ops[1] = NULL;
477 self->_ops[2] = NULL;
478 self->bops[0] = NULL;
479 self->bops[1] = NULL;
480 MEM_VECTOR_INIT(self, phi);
481 MEM_VECTOR_INIT(self, params);
486 MEM_VEC_FUNCTIONS(ir_instr, ir_phi_entry_t, phi)
487 MEM_VEC_FUNCTIONS(ir_instr, ir_value*, params)
489 void ir_instr_delete(ir_instr *self)
492 /* The following calls can only delete from
493 * vectors, we still want to delete this instruction
494 * so ignore the return value. Since with the warn_unused_result attribute
495 * gcc doesn't care about an explicit: (void)foo(); to ignore the result,
496 * I have to improvise here and use if(foo());
498 for (i = 0; i < self->phi_count; ++i) {
500 if (ir_value_writes_find(self->phi[i].value, self, &idx))
501 if (ir_value_writes_remove(self->phi[i].value, idx)) GMQCC_SUPPRESS_EMPTY_BODY;
502 if (ir_value_reads_find(self->phi[i].value, self, &idx))
503 if (ir_value_reads_remove (self->phi[i].value, idx)) GMQCC_SUPPRESS_EMPTY_BODY;
505 MEM_VECTOR_CLEAR(self, phi);
506 for (i = 0; i < self->params_count; ++i) {
508 if (ir_value_writes_find(self->params[i], self, &idx))
509 if (ir_value_writes_remove(self->params[i], idx)) GMQCC_SUPPRESS_EMPTY_BODY;
510 if (ir_value_reads_find(self->params[i], self, &idx))
511 if (ir_value_reads_remove (self->params[i], idx)) GMQCC_SUPPRESS_EMPTY_BODY;
513 MEM_VECTOR_CLEAR(self, params);
514 if (ir_instr_op(self, 0, NULL, false)) GMQCC_SUPPRESS_EMPTY_BODY;
515 if (ir_instr_op(self, 1, NULL, false)) GMQCC_SUPPRESS_EMPTY_BODY;
516 if (ir_instr_op(self, 2, NULL, false)) GMQCC_SUPPRESS_EMPTY_BODY;
520 bool ir_instr_op(ir_instr *self, int op, ir_value *v, bool writing)
522 if (self->_ops[op]) {
524 if (writing && ir_value_writes_find(self->_ops[op], self, &idx))
526 if (!ir_value_writes_remove(self->_ops[op], idx))
529 else if (ir_value_reads_find(self->_ops[op], self, &idx))
531 if (!ir_value_reads_remove(self->_ops[op], idx))
537 if (!ir_value_writes_add(v, self))
540 if (!ir_value_reads_add(v, self))
548 /***********************************************************************
552 ir_value* ir_value_var(const char *name, int storetype, int vtype)
555 self = (ir_value*)mem_a(sizeof(*self));
557 self->fieldtype = TYPE_VOID;
558 self->outtype = TYPE_VOID;
559 self->store = storetype;
560 MEM_VECTOR_INIT(self, reads);
561 MEM_VECTOR_INIT(self, writes);
562 self->isconst = false;
563 self->context.file = "<@no context>";
564 self->context.line = 0;
566 ir_value_set_name(self, name);
568 memset(&self->constval, 0, sizeof(self->constval));
569 memset(&self->code, 0, sizeof(self->code));
571 MEM_VECTOR_INIT(self, life);
574 MEM_VEC_FUNCTIONS(ir_value, ir_life_entry_t, life)
575 MEM_VEC_FUNCTIONS_ALL(ir_value, ir_instr*, reads)
576 MEM_VEC_FUNCTIONS_ALL(ir_value, ir_instr*, writes)
578 ir_value* ir_value_out(ir_function *owner, const char *name, int storetype, int vtype)
580 ir_value *v = ir_value_var(name, storetype, vtype);
583 if (!ir_function_collect_value(owner, v))
591 void ir_value_delete(ir_value* self)
594 mem_d((void*)self->name);
597 if (self->vtype == TYPE_STRING)
598 mem_d((void*)self->constval.vstring);
600 MEM_VECTOR_CLEAR(self, reads);
601 MEM_VECTOR_CLEAR(self, writes);
602 MEM_VECTOR_CLEAR(self, life);
606 void ir_value_set_name(ir_value *self, const char *name)
609 mem_d((void*)self->name);
610 self->name = util_strdup(name);
613 bool ir_value_set_float(ir_value *self, float f)
615 if (self->vtype != TYPE_FLOAT)
617 self->constval.vfloat = f;
618 self->isconst = true;
622 bool ir_value_set_func(ir_value *self, int f)
624 if (self->vtype != TYPE_FUNCTION)
626 self->constval.vint = f;
627 self->isconst = true;
631 bool ir_value_set_vector(ir_value *self, vector v)
633 if (self->vtype != TYPE_VECTOR)
635 self->constval.vvec = v;
636 self->isconst = true;
640 bool ir_value_set_quaternion(ir_value *self, quaternion v)
642 if (self->vtype != TYPE_QUATERNION)
644 memcpy(&self->constval.vquat, v, sizeof(self->constval.vquat));
645 self->isconst = true;
649 bool ir_value_set_matrix(ir_value *self, matrix v)
651 if (self->vtype != TYPE_MATRIX)
653 memcpy(&self->constval.vmat, v, sizeof(self->constval.vmat));
654 self->isconst = true;
658 bool ir_value_set_string(ir_value *self, const char *str)
660 if (self->vtype != TYPE_STRING)
662 self->constval.vstring = util_strdup(str);
663 self->isconst = true;
668 bool ir_value_set_int(ir_value *self, int i)
670 if (self->vtype != TYPE_INTEGER)
672 self->constval.vint = i;
673 self->isconst = true;
678 bool ir_value_lives(ir_value *self, size_t at)
681 for (i = 0; i < self->life_count; ++i)
683 ir_life_entry_t *life = &self->life[i];
684 if (life->start <= at && at <= life->end)
686 if (life->start > at) /* since it's ordered */
692 bool ir_value_life_insert(ir_value *self, size_t idx, ir_life_entry_t e)
695 if (!ir_value_life_add(self, e)) /* naive... */
697 for (k = self->life_count-1; k > idx; --k)
698 self->life[k] = self->life[k-1];
703 bool ir_value_life_merge(ir_value *self, size_t s)
706 ir_life_entry_t *life = NULL;
707 ir_life_entry_t *before = NULL;
708 ir_life_entry_t new_entry;
710 /* Find the first range >= s */
711 for (i = 0; i < self->life_count; ++i)
714 life = &self->life[i];
718 /* nothing found? append */
719 if (i == self->life_count) {
721 if (life && life->end+1 == s)
723 /* previous life range can be merged in */
727 if (life && life->end >= s)
730 if (!ir_value_life_add(self, e))
731 return false; /* failing */
737 if (before->end + 1 == s &&
738 life->start - 1 == s)
741 before->end = life->end;
742 if (!ir_value_life_remove(self, i))
743 return false; /* failing */
746 if (before->end + 1 == s)
752 /* already contained */
753 if (before->end >= s)
757 if (life->start - 1 == s)
762 /* insert a new entry */
763 new_entry.start = new_entry.end = s;
764 return ir_value_life_insert(self, i, new_entry);
767 bool ir_value_life_merge_into(ir_value *self, const ir_value *other)
771 if (!other->life_count)
774 if (!self->life_count) {
775 for (i = 0; i < other->life_count; ++i) {
776 if (!ir_value_life_add(self, other->life[i]))
783 for (i = 0; i < other->life_count; ++i)
785 const ir_life_entry_t *life = &other->life[i];
788 ir_life_entry_t *entry = &self->life[myi];
790 if (life->end+1 < entry->start)
792 /* adding an interval before entry */
793 if (!ir_value_life_insert(self, myi, *life))
799 if (life->start < entry->start &&
800 life->end >= entry->start)
802 /* starts earlier and overlaps */
803 entry->start = life->start;
806 if (life->end > entry->end &&
807 life->start-1 <= entry->end)
809 /* ends later and overlaps */
810 entry->end = life->end;
813 /* see if our change combines it with the next ranges */
814 while (myi+1 < self->life_count &&
815 entry->end+1 >= self->life[1+myi].start)
817 /* overlaps with (myi+1) */
818 if (entry->end < self->life[1+myi].end)
819 entry->end = self->life[1+myi].end;
820 if (!ir_value_life_remove(self, myi+1))
822 entry = &self->life[myi];
825 /* see if we're after the entry */
826 if (life->start > entry->end)
829 /* append if we're at the end */
830 if (myi >= self->life_count) {
831 if (!ir_value_life_add(self, *life))
835 /* otherweise check the next range */
844 bool ir_values_overlap(const ir_value *a, const ir_value *b)
846 /* For any life entry in A see if it overlaps with
847 * any life entry in B.
848 * Note that the life entries are orderes, so we can make a
849 * more efficient algorithm there than naively translating the
853 ir_life_entry_t *la, *lb, *enda, *endb;
855 /* first of all, if either has no life range, they cannot clash */
856 if (!a->life_count || !b->life_count)
861 enda = la + a->life_count;
862 endb = lb + b->life_count;
865 /* check if the entries overlap, for that,
866 * both must start before the other one ends.
868 #if defined(LIFE_RANGE_WITHOUT_LAST_READ)
869 if (la->start <= lb->end &&
870 lb->start <= la->end)
872 if (la->start < lb->end &&
879 /* entries are ordered
880 * one entry is earlier than the other
881 * that earlier entry will be moved forward
883 if (la->start < lb->start)
885 /* order: A B, move A forward
886 * check if we hit the end with A
891 else if (lb->start < la->start)
893 /* order: B A, move B forward
894 * check if we hit the end with B
903 /***********************************************************************
907 bool ir_block_create_store_op(ir_block *self, int op, ir_value *target, ir_value *what)
909 if (target->store == store_value) {
910 fprintf(stderr, "cannot store to an SSA value\n");
911 fprintf(stderr, "trying to store: %s <- %s\n", target->name, what->name);
914 ir_instr *in = ir_instr_new(self, op);
917 if (!ir_instr_op(in, 0, target, true) ||
918 !ir_instr_op(in, 1, what, false) ||
919 !ir_block_instr_add(self, in) )
927 bool ir_block_create_store(ir_block *self, ir_value *target, ir_value *what)
931 if (target->vtype == TYPE_VARIANT)
934 vtype = target->vtype;
937 if (vtype == TYPE_FLOAT && what->vtype == TYPE_INTEGER)
938 op = INSTR_CONV_ITOF;
939 else if (vtype == TYPE_INTEGER && what->vtype == TYPE_FLOAT)
940 op = INSTR_CONV_FTOI;
942 op = type_store_instr[vtype];
944 return ir_block_create_store_op(self, op, target, what);
947 bool ir_block_create_storep(ir_block *self, ir_value *target, ir_value *what)
952 if (target->vtype != TYPE_POINTER)
955 /* storing using pointer - target is a pointer, type must be
956 * inferred from source
960 op = type_storep_instr[vtype];
961 return ir_block_create_store_op(self, op, target, what);
964 bool ir_block_create_return(ir_block *self, ir_value *v)
968 fprintf(stderr, "block already ended (%s)\n", self->label);
972 self->is_return = true;
973 in = ir_instr_new(self, INSTR_RETURN);
977 if (!ir_instr_op(in, 0, v, false) ||
978 !ir_block_instr_add(self, in) )
985 bool ir_block_create_if(ir_block *self, ir_value *v,
986 ir_block *ontrue, ir_block *onfalse)
990 fprintf(stderr, "block already ended (%s)\n", self->label);
994 /*in = ir_instr_new(self, (v->vtype == TYPE_STRING ? INSTR_IF_S : INSTR_IF_F));*/
995 in = ir_instr_new(self, VINSTR_COND);
999 if (!ir_instr_op(in, 0, v, false)) {
1000 ir_instr_delete(in);
1004 in->bops[0] = ontrue;
1005 in->bops[1] = onfalse;
1007 if (!ir_block_instr_add(self, in))
1010 if (!ir_block_exits_add(self, ontrue) ||
1011 !ir_block_exits_add(self, onfalse) ||
1012 !ir_block_entries_add(ontrue, self) ||
1013 !ir_block_entries_add(onfalse, self) )
1020 bool ir_block_create_jump(ir_block *self, ir_block *to)
1024 fprintf(stderr, "block already ended (%s)\n", self->label);
1028 in = ir_instr_new(self, VINSTR_JUMP);
1033 if (!ir_block_instr_add(self, in))
1036 if (!ir_block_exits_add(self, to) ||
1037 !ir_block_entries_add(to, self) )
1044 bool ir_block_create_goto(ir_block *self, ir_block *to)
1048 fprintf(stderr, "block already ended (%s)\n", self->label);
1052 in = ir_instr_new(self, INSTR_GOTO);
1057 if (!ir_block_instr_add(self, in))
1060 if (!ir_block_exits_add(self, to) ||
1061 !ir_block_entries_add(to, self) )
1068 ir_instr* ir_block_create_phi(ir_block *self, const char *label, int ot)
1072 in = ir_instr_new(self, VINSTR_PHI);
1075 out = ir_value_out(self->owner, label, store_value, ot);
1077 ir_instr_delete(in);
1080 if (!ir_instr_op(in, 0, out, true)) {
1081 ir_instr_delete(in);
1082 ir_value_delete(out);
1085 if (!ir_block_instr_add(self, in)) {
1086 ir_instr_delete(in);
1087 ir_value_delete(out);
1093 ir_value* ir_phi_value(ir_instr *self)
1095 return self->_ops[0];
1098 bool ir_phi_add(ir_instr* self, ir_block *b, ir_value *v)
1102 if (!ir_block_entries_find(self->owner, b, NULL)) {
1103 /* Must not be possible to cause this, otherwise the AST
1104 * is doing something wrong.
1106 fprintf(stderr, "Invalid entry block for PHI\n");
1112 if (!ir_value_reads_add(v, self))
1114 return ir_instr_phi_add(self, pe);
1117 /* call related code */
1118 ir_instr* ir_block_create_call(ir_block *self, const char *label, ir_value *func)
1122 in = ir_instr_new(self, INSTR_CALL0);
1125 out = ir_value_out(self->owner, label, store_return, func->outtype);
1127 ir_instr_delete(in);
1130 if (!ir_instr_op(in, 0, out, true) ||
1131 !ir_instr_op(in, 1, func, false) ||
1132 !ir_block_instr_add(self, in))
1134 ir_instr_delete(in);
1135 ir_value_delete(out);
1141 ir_value* ir_call_value(ir_instr *self)
1143 return self->_ops[0];
1146 bool ir_call_param(ir_instr* self, ir_value *v)
1148 if (!ir_instr_params_add(self, v))
1150 if (!ir_value_reads_add(v, self)) {
1151 if (!ir_instr_params_remove(self, self->params_count-1))
1152 GMQCC_SUPPRESS_EMPTY_BODY;
1158 /* binary op related code */
1160 ir_value* ir_block_create_binop(ir_block *self,
1161 const char *label, int opcode,
1162 ir_value *left, ir_value *right)
1184 case INSTR_SUB_S: /* -- offset of string as float */
1189 case INSTR_BITOR_IF:
1190 case INSTR_BITOR_FI:
1191 case INSTR_BITAND_FI:
1192 case INSTR_BITAND_IF:
1207 case INSTR_BITAND_I:
1210 case INSTR_RSHIFT_I:
1211 case INSTR_LSHIFT_I:
1233 /* boolean operations result in floats */
1234 if (opcode >= INSTR_EQ_F && opcode <= INSTR_GT)
1236 else if (opcode >= INSTR_LE && opcode <= INSTR_GT)
1239 else if (opcode >= INSTR_LE_I && opcode <= INSTR_EQ_FI)
1244 if (ot == TYPE_VOID) {
1245 /* The AST or parser were supposed to check this! */
1249 return ir_block_create_general_instr(self, label, opcode, left, right, ot);
1252 ir_value* ir_block_create_unary(ir_block *self,
1253 const char *label, int opcode,
1256 int ot = TYPE_FLOAT;
1268 /* QC doesn't have other unary operations. We expect extensions to fill
1269 * the above list, otherwise we assume out-type = in-type, eg for an
1273 ot = operand->vtype;
1276 if (ot == TYPE_VOID) {
1277 /* The AST or parser were supposed to check this! */
1281 /* let's use the general instruction creator and pass NULL for OPB */
1282 return ir_block_create_general_instr(self, label, opcode, operand, NULL, ot);
1285 ir_value* ir_block_create_general_instr(ir_block *self, const char *label,
1286 int op, ir_value *a, ir_value *b, int outype)
1291 out = ir_value_out(self->owner, label, store_value, outype);
1295 instr = ir_instr_new(self, op);
1297 ir_value_delete(out);
1301 if (!ir_instr_op(instr, 0, out, true) ||
1302 !ir_instr_op(instr, 1, a, false) ||
1303 !ir_instr_op(instr, 2, b, false) )
1308 if (!ir_block_instr_add(self, instr))
1313 ir_instr_delete(instr);
1314 ir_value_delete(out);
1318 ir_value* ir_block_create_fieldaddress(ir_block *self, const char *label, ir_value *ent, ir_value *field)
1322 /* Support for various pointer types todo if so desired */
1323 if (ent->vtype != TYPE_ENTITY)
1326 if (field->vtype != TYPE_FIELD)
1329 v = ir_block_create_general_instr(self, label, INSTR_ADDRESS, ent, field, TYPE_POINTER);
1330 v->fieldtype = field->fieldtype;
1334 ir_value* ir_block_create_load_from_ent(ir_block *self, const char *label, ir_value *ent, ir_value *field, int outype)
1337 if (ent->vtype != TYPE_ENTITY)
1340 /* at some point we could redirect for TYPE_POINTER... but that could lead to carelessness */
1341 if (field->vtype != TYPE_FIELD)
1346 case TYPE_FLOAT: op = INSTR_LOAD_F; break;
1347 case TYPE_VECTOR: op = INSTR_LOAD_V; break;
1348 case TYPE_STRING: op = INSTR_LOAD_S; break;
1349 case TYPE_FIELD: op = INSTR_LOAD_FLD; break;
1350 case TYPE_ENTITY: op = INSTR_LOAD_ENT; break;
1352 case TYPE_POINTER: op = INSTR_LOAD_I; break;
1353 case TYPE_INTEGER: op = INSTR_LOAD_I; break;
1355 case TYPE_QUATERNION: op = INSTR_LOAD_Q; break;
1356 case TYPE_MATRIX: op = INSTR_LOAD_M; break;
1361 return ir_block_create_general_instr(self, label, op, ent, field, outype);
1364 ir_value* ir_block_create_add(ir_block *self,
1366 ir_value *left, ir_value *right)
1369 int l = left->vtype;
1370 int r = right->vtype;
1389 if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1391 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1397 return ir_block_create_binop(self, label, op, left, right);
1400 ir_value* ir_block_create_sub(ir_block *self,
1402 ir_value *left, ir_value *right)
1405 int l = left->vtype;
1406 int r = right->vtype;
1426 if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1428 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1434 return ir_block_create_binop(self, label, op, left, right);
1437 ir_value* ir_block_create_mul(ir_block *self,
1439 ir_value *left, ir_value *right)
1442 int l = left->vtype;
1443 int r = right->vtype;
1460 case TYPE_QUATERNION:
1468 if ( (l == TYPE_VECTOR && r == TYPE_FLOAT) )
1470 else if ( (l == TYPE_FLOAT && r == TYPE_VECTOR) )
1472 else if ( (l == TYPE_QUATERNION && r == TYPE_FLOAT) )
1474 else if ( (l == TYPE_MATRIX && r == TYPE_FLOAT) )
1477 else if ( (l == TYPE_VECTOR && r == TYPE_INTEGER) )
1479 else if ( (l == TYPE_INTEGER && r == TYPE_VECTOR) )
1481 else if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1483 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1489 return ir_block_create_binop(self, label, op, left, right);
1492 ir_value* ir_block_create_div(ir_block *self,
1494 ir_value *left, ir_value *right)
1497 int l = left->vtype;
1498 int r = right->vtype;
1515 if ( (l == TYPE_VECTOR && r == TYPE_FLOAT) )
1517 else if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1519 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1525 return ir_block_create_binop(self, label, op, left, right);
1528 /* PHI resolving breaks the SSA, and must thus be the last
1529 * step before life-range calculation.
1532 static bool ir_block_naive_phi(ir_block *self);
1533 bool ir_function_naive_phi(ir_function *self)
1537 for (i = 0; i < self->blocks_count; ++i)
1539 if (!ir_block_naive_phi(self->blocks[i]))
1545 static bool ir_naive_phi_emit_store(ir_block *block, size_t iid, ir_value *old, ir_value *what)
1550 /* create a store */
1551 if (!ir_block_create_store(block, old, what))
1554 /* we now move it up */
1555 instr = block->instr[block->instr_count-1];
1556 for (i = block->instr_count; i > iid; --i)
1557 block->instr[i] = block->instr[i-1];
1558 block->instr[i] = instr;
1563 static bool ir_block_naive_phi(ir_block *self)
1566 /* FIXME: optionally, create_phi can add the phis
1567 * to a list so we don't need to loop through blocks
1568 * - anyway: "don't optimize YET"
1570 for (i = 0; i < self->instr_count; ++i)
1572 ir_instr *instr = self->instr[i];
1573 if (instr->opcode != VINSTR_PHI)
1576 if (!ir_block_instr_remove(self, i))
1578 --i; /* NOTE: i+1 below */
1580 for (p = 0; p < instr->phi_count; ++p)
1582 ir_value *v = instr->phi[p].value;
1583 for (w = 0; w < v->writes_count; ++w) {
1586 if (!v->writes[w]->_ops[0])
1589 /* When the write was to a global, we have to emit a mov */
1590 old = v->writes[w]->_ops[0];
1592 /* The original instruction now writes to the PHI target local */
1593 if (v->writes[w]->_ops[0] == v)
1594 v->writes[w]->_ops[0] = instr->_ops[0];
1596 if (old->store != store_value && old->store != store_local && old->store != store_param)
1598 /* If it originally wrote to a global we need to store the value
1601 if (!ir_naive_phi_emit_store(self, i+1, old, v))
1603 if (i+1 < self->instr_count)
1604 instr = self->instr[i+1];
1607 /* In case I forget and access instr later, it'll be NULL
1608 * when it's a problem, to make sure we crash, rather than accessing
1614 /* If it didn't, we can replace all reads by the phi target now. */
1616 for (r = 0; r < old->reads_count; ++r)
1619 ir_instr *ri = old->reads[r];
1620 for (op = 0; op < ri->phi_count; ++op) {
1621 if (ri->phi[op].value == old)
1622 ri->phi[op].value = v;
1624 for (op = 0; op < 3; ++op) {
1625 if (ri->_ops[op] == old)
1632 ir_instr_delete(instr);
1637 /***********************************************************************
1638 *IR Temp allocation code
1639 * Propagating value life ranges by walking through the function backwards
1640 * until no more changes are made.
1641 * In theory this should happen once more than once for every nested loop
1643 * Though this implementation might run an additional time for if nests.
1652 MEM_VEC_FUNCTIONS_ALL(new_reads_t, ir_value*, v)
1654 /* Enumerate instructions used by value's life-ranges
1656 static void ir_block_enumerate(ir_block *self, size_t *_eid)
1660 for (i = 0; i < self->instr_count; ++i)
1662 self->instr[i]->eid = eid++;
1667 /* Enumerate blocks and instructions.
1668 * The block-enumeration is unordered!
1669 * We do not really use the block enumreation, however
1670 * the instruction enumeration is important for life-ranges.
1672 void ir_function_enumerate(ir_function *self)
1675 size_t instruction_id = 0;
1676 for (i = 0; i < self->blocks_count; ++i)
1678 self->blocks[i]->eid = i;
1679 self->blocks[i]->run_id = 0;
1680 ir_block_enumerate(self->blocks[i], &instruction_id);
1684 static bool ir_block_life_propagate(ir_block *b, ir_block *prev, bool *changed);
1685 bool ir_function_calculate_liferanges(ir_function *self)
1693 for (i = 0; i != self->blocks_count; ++i)
1695 if (self->blocks[i]->is_return)
1697 if (!ir_block_life_propagate(self->blocks[i], NULL, &changed))
1705 /* Local-value allocator
1706 * After finishing creating the liferange of all values used in a function
1707 * we can allocate their global-positions.
1708 * This is the counterpart to register-allocation in register machines.
1711 MEM_VECTOR_MAKE(ir_value*, locals);
1712 MEM_VECTOR_MAKE(size_t, sizes);
1713 MEM_VECTOR_MAKE(size_t, positions);
1714 } function_allocator;
1715 MEM_VEC_FUNCTIONS(function_allocator, ir_value*, locals)
1716 MEM_VEC_FUNCTIONS(function_allocator, size_t, sizes)
1717 MEM_VEC_FUNCTIONS(function_allocator, size_t, positions)
1719 static bool function_allocator_alloc(function_allocator *alloc, const ir_value *var)
1722 size_t vsize = type_sizeof[var->vtype];
1724 slot = ir_value_var("reg", store_global, var->vtype);
1728 if (!ir_value_life_merge_into(slot, var))
1731 if (!function_allocator_locals_add(alloc, slot))
1734 if (!function_allocator_sizes_add(alloc, vsize))
1740 ir_value_delete(slot);
1744 bool ir_function_allocate_locals(ir_function *self)
1753 function_allocator alloc;
1755 if (!self->locals_count)
1758 MEM_VECTOR_INIT(&alloc, locals);
1759 MEM_VECTOR_INIT(&alloc, sizes);
1760 MEM_VECTOR_INIT(&alloc, positions);
1762 for (i = 0; i < self->locals_count; ++i)
1764 if (!function_allocator_alloc(&alloc, self->locals[i]))
1768 /* Allocate a slot for any value that still exists */
1769 for (i = 0; i < self->values_count; ++i)
1771 v = self->values[i];
1776 for (a = 0; a < alloc.locals_count; ++a)
1778 slot = alloc.locals[a];
1780 if (ir_values_overlap(v, slot))
1783 if (!ir_value_life_merge_into(slot, v))
1786 /* adjust size for this slot */
1787 if (alloc.sizes[a] < type_sizeof[v->vtype])
1788 alloc.sizes[a] = type_sizeof[v->vtype];
1790 self->values[i]->code.local = a;
1793 if (a >= alloc.locals_count) {
1794 self->values[i]->code.local = alloc.locals_count;
1795 if (!function_allocator_alloc(&alloc, v))
1800 /* Adjust slot positions based on sizes */
1801 if (!function_allocator_positions_add(&alloc, 0))
1804 if (alloc.sizes_count)
1805 pos = alloc.positions[0] + alloc.sizes[0];
1808 for (i = 1; i < alloc.sizes_count; ++i)
1810 pos = alloc.positions[i-1] + alloc.sizes[i-1];
1811 if (!function_allocator_positions_add(&alloc, pos))
1815 self->allocated_locals = pos + alloc.sizes[alloc.sizes_count-1];
1817 /* Take over the actual slot positions */
1818 for (i = 0; i < self->values_count; ++i)
1819 self->values[i]->code.local = alloc.positions[self->values[i]->code.local];
1826 for (i = 0; i < alloc.locals_count; ++i)
1827 ir_value_delete(alloc.locals[i]);
1828 MEM_VECTOR_CLEAR(&alloc, locals);
1829 MEM_VECTOR_CLEAR(&alloc, sizes);
1830 MEM_VECTOR_CLEAR(&alloc, positions);
1834 /* Get information about which operand
1835 * is read from, or written to.
1837 static void ir_op_read_write(int op, size_t *read, size_t *write)
1864 static bool ir_block_living_add_instr(ir_block *self, size_t eid)
1867 bool changed = false;
1869 for (i = 0; i != self->living_count; ++i)
1871 tempbool = ir_value_life_merge(self->living[i], eid);
1874 fprintf(stderr, "block_living_add_instr() value instruction added %s: %i\n", self->living[i]->_name, (int)eid);
1876 changed = changed || tempbool;
1881 static bool ir_block_life_prop_previous(ir_block* self, ir_block *prev, bool *changed)
1884 /* values which have been read in a previous iteration are now
1885 * in the "living" array even if the previous block doesn't use them.
1886 * So we have to remove whatever does not exist in the previous block.
1887 * They will be re-added on-read, but the liferange merge won't cause
1890 for (i = 0; i < self->living_count; ++i)
1892 if (!ir_block_living_find(prev, self->living[i], NULL)) {
1893 if (!ir_block_living_remove(self, i))
1899 /* Whatever the previous block still has in its living set
1900 * must now be added to ours as well.
1902 for (i = 0; i < prev->living_count; ++i)
1904 if (ir_block_living_find(self, prev->living[i], NULL))
1906 if (!ir_block_living_add(self, prev->living[i]))
1909 printf("%s got from prev: %s\n", self->label, prev->living[i]->_name);
1915 static bool ir_block_life_propagate(ir_block *self, ir_block *prev, bool *changed)
1921 /* bitmasks which operands are read from or written to */
1923 #if defined(LIFE_RANGE_WITHOUT_LAST_READ)
1925 new_reads_t new_reads;
1927 char dbg_ind[16] = { '#', '0' };
1930 #if defined(LIFE_RANGE_WITHOUT_LAST_READ)
1931 MEM_VECTOR_INIT(&new_reads, v);
1936 if (!ir_block_life_prop_previous(self, prev, changed))
1940 i = self->instr_count;
1943 instr = self->instr[i];
1945 /* PHI operands are always read operands */
1946 for (p = 0; p < instr->phi_count; ++p)
1948 value = instr->phi[p].value;
1949 #if ! defined(LIFE_RANGE_WITHOUT_LAST_READ)
1950 if (!ir_block_living_find(self, value, NULL) &&
1951 !ir_block_living_add(self, value))
1956 if (!new_reads_t_v_find(&new_reads, value, NULL))
1958 if (!new_reads_t_v_add(&new_reads, value))
1964 /* See which operands are read and write operands */
1965 ir_op_read_write(instr->opcode, &read, &write);
1967 /* Go through the 3 main operands */
1968 for (o = 0; o < 3; ++o)
1970 if (!instr->_ops[o]) /* no such operand */
1973 value = instr->_ops[o];
1975 /* We only care about locals */
1976 /* we also calculate parameter liferanges so that locals
1977 * can take up parameter slots */
1978 if (value->store != store_value &&
1979 value->store != store_local &&
1980 value->store != store_param)
1986 #if ! defined(LIFE_RANGE_WITHOUT_LAST_READ)
1987 if (!ir_block_living_find(self, value, NULL) &&
1988 !ir_block_living_add(self, value))
1993 /* fprintf(stderr, "read: %s\n", value->_name); */
1994 if (!new_reads_t_v_find(&new_reads, value, NULL))
1996 if (!new_reads_t_v_add(&new_reads, value))
2002 /* write operands */
2003 /* When we write to a local, we consider it "dead" for the
2004 * remaining upper part of the function, since in SSA a value
2005 * can only be written once (== created)
2010 bool in_living = ir_block_living_find(self, value, &idx);
2011 #if defined(LIFE_RANGE_WITHOUT_LAST_READ)
2013 bool in_reads = new_reads_t_v_find(&new_reads, value, &readidx);
2014 if (!in_living && !in_reads)
2019 /* If the value isn't alive it hasn't been read before... */
2020 /* TODO: See if the warning can be emitted during parsing or AST processing
2021 * otherwise have warning printed here.
2022 * IF printing a warning here: include filecontext_t,
2023 * and make sure it's only printed once
2024 * since this function is run multiple times.
2026 /* For now: debug info: */
2027 fprintf(stderr, "Value only written %s\n", value->name);
2028 tempbool = ir_value_life_merge(value, instr->eid);
2029 *changed = *changed || tempbool;
2031 ir_instr_dump(instr, dbg_ind, printf);
2035 /* since 'living' won't contain it
2036 * anymore, merge the value, since
2039 tempbool = ir_value_life_merge(value, instr->eid);
2042 fprintf(stderr, "value added id %s %i\n", value->name, (int)instr->eid);
2044 *changed = *changed || tempbool;
2046 #if ! defined(LIFE_RANGE_WITHOUT_LAST_READ)
2047 if (!ir_block_living_remove(self, idx))
2052 if (!new_reads_t_v_remove(&new_reads, readidx))
2060 tempbool = ir_block_living_add_instr(self, instr->eid);
2061 /*fprintf(stderr, "living added values\n");*/
2062 *changed = *changed || tempbool;
2064 #if defined(LIFE_RANGE_WITHOUT_LAST_READ)
2066 for (rd = 0; rd < new_reads.v_count; ++rd)
2068 if (!ir_block_living_find(self, new_reads.v[rd], NULL)) {
2069 if (!ir_block_living_add(self, new_reads.v[rd]))
2072 if (!i && !self->entries_count) {
2074 *changed = *changed || ir_value_life_merge(new_reads.v[rd], instr->eid);
2077 MEM_VECTOR_CLEAR(&new_reads, v);
2081 if (self->run_id == self->owner->run_id)
2084 self->run_id = self->owner->run_id;
2086 for (i = 0; i < self->entries_count; ++i)
2088 ir_block *entry = self->entries[i];
2089 ir_block_life_propagate(entry, self, changed);
2094 #if defined(LIFE_RANGE_WITHOUT_LAST_READ)
2095 MEM_VECTOR_CLEAR(&new_reads, v);
2100 /***********************************************************************
2103 * Since the IR has the convention of putting 'write' operands
2104 * at the beginning, we have to rotate the operands of instructions
2105 * properly in order to generate valid QCVM code.
2107 * Having destinations at a fixed position is more convenient. In QC
2108 * this is *mostly* OPC, but FTE adds at least 2 instructions which
2109 * read from from OPA, and store to OPB rather than OPC. Which is
2110 * partially the reason why the implementation of these instructions
2111 * in darkplaces has been delayed for so long.
2113 * Breaking conventions is annoying...
2115 static bool ir_builder_gen_global(ir_builder *self, ir_value *global);
2117 static bool gen_global_field(ir_value *global)
2119 if (global->isconst)
2121 ir_value *fld = global->constval.vpointer;
2123 printf("Invalid field constant with no field: %s\n", global->name);
2127 /* Now, in this case, a relocation would be impossible to code
2128 * since it looks like this:
2129 * .vector v = origin; <- parse error, wtf is 'origin'?
2132 * But we will need a general relocation support later anyway
2133 * for functions... might as well support that here.
2135 if (!fld->code.globaladdr) {
2136 printf("FIXME: Relocation support\n");
2140 /* copy the field's value */
2141 global->code.globaladdr = code_globals_add(code_globals_data[fld->code.globaladdr]);
2145 global->code.globaladdr = code_globals_add(0);
2147 if (global->code.globaladdr < 0)
2152 static bool gen_global_pointer(ir_value *global)
2154 if (global->isconst)
2156 ir_value *target = global->constval.vpointer;
2158 printf("Invalid pointer constant: %s\n", global->name);
2159 /* NULL pointers are pointing to the NULL constant, which also
2160 * sits at address 0, but still has an ir_value for itself.
2165 /* Here, relocations ARE possible - in fteqcc-enhanced-qc:
2166 * void() foo; <- proto
2167 * void() *fooptr = &foo;
2168 * void() foo = { code }
2170 if (!target->code.globaladdr) {
2171 /* FIXME: Check for the constant nullptr ir_value!
2172 * because then code.globaladdr being 0 is valid.
2174 printf("FIXME: Relocation support\n");
2178 global->code.globaladdr = code_globals_add(target->code.globaladdr);
2182 global->code.globaladdr = code_globals_add(0);
2184 if (global->code.globaladdr < 0)
2189 static bool gen_blocks_recursive(ir_function *func, ir_block *block)
2191 prog_section_statement stmt;
2200 block->generated = true;
2201 block->code_start = code_statements_elements;
2202 for (i = 0; i < block->instr_count; ++i)
2204 instr = block->instr[i];
2206 if (instr->opcode == VINSTR_PHI) {
2207 printf("cannot generate virtual instruction (phi)\n");
2211 if (instr->opcode == VINSTR_JUMP) {
2212 target = instr->bops[0];
2213 /* for uncoditional jumps, if the target hasn't been generated
2214 * yet, we generate them right here.
2216 if (!target->generated) {
2221 /* otherwise we generate a jump instruction */
2222 stmt.opcode = INSTR_GOTO;
2223 stmt.o1.s1 = (target->code_start) - code_statements_elements;
2226 if (code_statements_add(stmt) < 0)
2229 /* no further instructions can be in this block */
2233 if (instr->opcode == VINSTR_COND) {
2234 ontrue = instr->bops[0];
2235 onfalse = instr->bops[1];
2236 /* TODO: have the AST signal which block should
2237 * come first: eg. optimize IFs without ELSE...
2240 stmt.o1.u1 = instr->_ops[0]->code.globaladdr;
2244 if (ontrue->generated) {
2245 stmt.opcode = INSTR_IF;
2246 stmt.o2.s1 = (ontrue->code_start-1) - code_statements_elements;
2247 if (code_statements_add(stmt) < 0)
2250 if (onfalse->generated) {
2251 stmt.opcode = INSTR_IFNOT;
2252 stmt.o2.s1 = (onfalse->code_start-1) - code_statements_elements;
2253 if (code_statements_add(stmt) < 0)
2256 if (!ontrue->generated) {
2257 if (onfalse->generated) {
2262 if (!onfalse->generated) {
2263 if (ontrue->generated) {
2268 /* neither ontrue nor onfalse exist */
2269 stmt.opcode = INSTR_IFNOT;
2270 stidx = code_statements_elements;
2271 if (code_statements_add(stmt) < 0)
2273 /* on false we jump, so add ontrue-path */
2274 if (!gen_blocks_recursive(func, ontrue))
2276 /* fixup the jump address */
2277 code_statements_data[stidx].o2.s1 = code_statements_elements - stidx;
2278 /* generate onfalse path */
2279 if (onfalse->generated) {
2280 /* fixup the jump address */
2281 code_statements_data[stidx].o2.s1 = (onfalse->code_start) - (stidx);
2282 /* may have been generated in the previous recursive call */
2283 stmt.opcode = INSTR_GOTO;
2284 stmt.o1.s1 = (onfalse->code_start) - code_statements_elements;
2287 return (code_statements_add(stmt) >= 0);
2289 /* if not, generate now */
2294 if (instr->opcode >= INSTR_CALL0 && instr->opcode <= INSTR_CALL8) {
2295 /* Trivial call translation:
2296 * copy all params to OFS_PARM*
2297 * if the output's storetype is not store_return,
2298 * add append a STORE instruction!
2300 * NOTES on how to do it better without much trouble:
2301 * -) The liferanges!
2302 * Simply check the liferange of all parameters for
2303 * other CALLs. For each param with no CALL in its
2304 * liferange, we can store it in an OFS_PARM at
2305 * generation already. This would even include later
2306 * reuse.... probably... :)
2311 for (p = 0; p < instr->params_count; ++p)
2313 ir_value *param = instr->params[p];
2315 stmt.opcode = INSTR_STORE_F;
2318 stmt.opcode = type_store_instr[param->vtype];
2319 stmt.o1.u1 = param->code.globaladdr;
2320 stmt.o2.u1 = OFS_PARM0 + 3 * p;
2321 if (code_statements_add(stmt) < 0)
2324 stmt.opcode = INSTR_CALL0 + instr->params_count;
2325 if (stmt.opcode > INSTR_CALL8)
2326 stmt.opcode = INSTR_CALL8;
2327 stmt.o1.u1 = instr->_ops[1]->code.globaladdr;
2330 if (code_statements_add(stmt) < 0)
2333 retvalue = instr->_ops[0];
2334 if (retvalue && retvalue->store != store_return && retvalue->life_count)
2336 /* not to be kept in OFS_RETURN */
2337 stmt.opcode = type_store_instr[retvalue->vtype];
2338 stmt.o1.u1 = OFS_RETURN;
2339 stmt.o2.u1 = retvalue->code.globaladdr;
2341 if (code_statements_add(stmt) < 0)
2347 if (instr->opcode == INSTR_STATE) {
2348 printf("TODO: state instruction\n");
2352 stmt.opcode = instr->opcode;
2357 /* This is the general order of operands */
2359 stmt.o3.u1 = instr->_ops[0]->code.globaladdr;
2362 stmt.o1.u1 = instr->_ops[1]->code.globaladdr;
2365 stmt.o2.u1 = instr->_ops[2]->code.globaladdr;
2367 if (stmt.opcode == INSTR_RETURN || stmt.opcode == INSTR_DONE)
2369 stmt.o1.u1 = stmt.o3.u1;
2372 else if (stmt.opcode >= INSTR_STORE_F &&
2373 stmt.opcode <= INSTR_STORE_FNC)
2375 /* 2-operand instructions with A -> B */
2376 stmt.o2.u1 = stmt.o3.u1;
2380 if (code_statements_add(stmt) < 0)
2386 static bool gen_function_code(ir_function *self)
2389 prog_section_statement stmt;
2391 /* Starting from entry point, we generate blocks "as they come"
2392 * for now. Dead blocks will not be translated obviously.
2394 if (!self->blocks_count) {
2395 printf("Function '%s' declared without body.\n", self->name);
2399 block = self->blocks[0];
2400 if (block->generated)
2403 if (!gen_blocks_recursive(self, block)) {
2404 printf("failed to generate blocks for '%s'\n", self->name);
2408 /* otherwise code_write crashes since it debug-prints functions until AINSTR_END */
2409 stmt.opcode = AINSTR_END;
2413 if (code_statements_add(stmt) < 0)
2418 static bool gen_global_function(ir_builder *ir, ir_value *global)
2420 prog_section_function fun;
2424 size_t local_var_end;
2426 if (!global->isconst || (!global->constval.vfunc))
2428 printf("Invalid state of function-global: not constant: %s\n", global->name);
2432 irfun = global->constval.vfunc;
2434 fun.name = global->code.name;
2435 fun.file = code_cachedstring(global->context.file);
2436 fun.profile = 0; /* always 0 */
2437 fun.nargs = irfun->params_count;
2439 for (i = 0;i < 8; ++i) {
2443 fun.argsize[i] = type_sizeof[irfun->params[i]];
2446 fun.firstlocal = code_globals_elements;
2447 fun.locals = irfun->allocated_locals + irfun->locals_count;
2450 for (i = 0; i < irfun->locals_count; ++i) {
2451 if (!ir_builder_gen_global(ir, irfun->locals[i])) {
2452 printf("Failed to generate global %s\n", irfun->locals[i]->name);
2456 if (irfun->locals_count) {
2457 ir_value *last = irfun->locals[irfun->locals_count-1];
2458 local_var_end = last->code.globaladdr;
2459 local_var_end += type_sizeof[last->vtype];
2461 for (i = 0; i < irfun->values_count; ++i)
2463 /* generate code.globaladdr for ssa values */
2464 ir_value *v = irfun->values[i];
2465 v->code.globaladdr = local_var_end + v->code.local;
2467 for (i = 0; i < irfun->locals_count; ++i) {
2468 /* fill the locals with zeros */
2469 code_globals_add(0);
2473 fun.entry = irfun->builtin;
2475 fun.entry = code_statements_elements;
2476 if (!gen_function_code(irfun)) {
2477 printf("Failed to generate code for function %s\n", irfun->name);
2482 return (code_functions_add(fun) >= 0);
2485 static bool ir_builder_gen_global(ir_builder *self, ir_value *global)
2489 prog_section_def def;
2491 def.type = global->vtype;
2492 def.offset = code_globals_elements;
2493 def.name = global->code.name = code_genstring(global->name);
2495 switch (global->vtype)
2498 if (code_defs_add(def) < 0)
2500 return gen_global_pointer(global);
2502 if (code_defs_add(def) < 0)
2504 return gen_global_field(global);
2509 if (code_defs_add(def) < 0)
2512 if (global->isconst) {
2513 iptr = (int32_t*)&global->constval.vfloat;
2514 global->code.globaladdr = code_globals_add(*iptr);
2516 global->code.globaladdr = code_globals_add(0);
2518 return global->code.globaladdr >= 0;
2522 if (code_defs_add(def) < 0)
2524 if (global->isconst)
2525 global->code.globaladdr = code_globals_add(code_cachedstring(global->constval.vstring));
2527 global->code.globaladdr = code_globals_add(0);
2528 return global->code.globaladdr >= 0;
2531 case TYPE_QUATERNION:
2535 if (code_defs_add(def) < 0)
2538 if (global->isconst) {
2539 iptr = (int32_t*)&global->constval.vvec;
2540 global->code.globaladdr = code_globals_add(iptr[0]);
2541 if (global->code.globaladdr < 0)
2543 for (d = 1; d < type_sizeof[global->vtype]; ++d)
2545 if (code_globals_add(iptr[d]) < 0)
2549 global->code.globaladdr = code_globals_add(0);
2550 if (global->code.globaladdr < 0)
2552 for (d = 1; d < type_sizeof[global->vtype]; ++d)
2554 if (code_globals_add(0) < 0)
2558 return global->code.globaladdr >= 0;
2561 if (code_defs_add(def) < 0)
2563 global->code.globaladdr = code_globals_elements;
2564 code_globals_add(code_functions_elements);
2565 return gen_global_function(self, global);
2567 /* assume biggest type */
2568 global->code.globaladdr = code_globals_add(0);
2569 for (i = 1; i < type_sizeof[TYPE_VARIANT]; ++i)
2570 code_globals_add(0);
2573 /* refuse to create 'void' type or any other fancy business. */
2574 printf("Invalid type for global variable %s\n", global->name);
2579 static bool ir_builder_gen_field(ir_builder *self, ir_value *field)
2581 prog_section_def def;
2582 prog_section_field fld;
2584 def.type = field->vtype;
2585 def.offset = code_globals_elements;
2586 def.name = field->code.name = code_genstring(field->name);
2588 if (code_defs_add(def) < 0)
2591 fld.name = def.name;
2592 fld.offset = code_fields_elements;
2593 fld.type = field->fieldtype;
2595 if (fld.type == TYPE_VOID) {
2596 printf("field is missing a type: %s - don't know its size\n", field->name);
2600 if (code_fields_add(fld) < 0)
2603 if (!code_globals_add(code_alloc_field(type_sizeof[field->fieldtype])))
2606 field->code.globaladdr = code_globals_add(fld.offset);
2607 return field->code.globaladdr >= 0;
2610 bool ir_builder_generate(ir_builder *self, const char *filename)
2616 for (i = 0; i < self->fields_count; ++i)
2618 if (!ir_builder_gen_field(self, self->fields[i])) {
2623 for (i = 0; i < self->globals_count; ++i)
2625 if (!ir_builder_gen_global(self, self->globals[i])) {
2630 printf("writing '%s'...\n", filename);
2631 return code_write(filename);
2634 /***********************************************************************
2635 *IR DEBUG Dump functions...
2638 #define IND_BUFSZ 1024
2640 const char *qc_opname(int op)
2642 if (op < 0) return "<INVALID>";
2643 if (op < ( sizeof(asm_instr) / sizeof(asm_instr[0]) ))
2644 return asm_instr[op].m;
2646 case VINSTR_PHI: return "PHI";
2647 case VINSTR_JUMP: return "JUMP";
2648 case VINSTR_COND: return "COND";
2649 default: return "<UNK>";
2653 void ir_builder_dump(ir_builder *b, int (*oprintf)(const char*, ...))
2656 char indent[IND_BUFSZ];
2660 oprintf("module %s\n", b->name);
2661 for (i = 0; i < b->globals_count; ++i)
2664 if (b->globals[i]->isconst)
2665 oprintf("%s = ", b->globals[i]->name);
2666 ir_value_dump(b->globals[i], oprintf);
2669 for (i = 0; i < b->functions_count; ++i)
2670 ir_function_dump(b->functions[i], indent, oprintf);
2671 oprintf("endmodule %s\n", b->name);
2674 void ir_function_dump(ir_function *f, char *ind,
2675 int (*oprintf)(const char*, ...))
2678 if (f->builtin != 0) {
2679 oprintf("%sfunction %s = builtin %i\n", ind, f->name, -f->builtin);
2682 oprintf("%sfunction %s\n", ind, f->name);
2683 strncat(ind, "\t", IND_BUFSZ);
2684 if (f->locals_count)
2686 oprintf("%s%i locals:\n", ind, (int)f->locals_count);
2687 for (i = 0; i < f->locals_count; ++i) {
2688 oprintf("%s\t", ind);
2689 ir_value_dump(f->locals[i], oprintf);
2693 if (f->blocks_count)
2695 oprintf("%slife passes (check): %i\n", ind, (int)f->run_id);
2696 for (i = 0; i < f->blocks_count; ++i) {
2697 if (f->blocks[i]->run_id != f->run_id) {
2698 oprintf("%slife pass check fail! %i != %i\n", ind, (int)f->blocks[i]->run_id, (int)f->run_id);
2700 ir_block_dump(f->blocks[i], ind, oprintf);
2704 ind[strlen(ind)-1] = 0;
2705 oprintf("%sendfunction %s\n", ind, f->name);
2708 void ir_block_dump(ir_block* b, char *ind,
2709 int (*oprintf)(const char*, ...))
2712 oprintf("%s:%s\n", ind, b->label);
2713 strncat(ind, "\t", IND_BUFSZ);
2715 for (i = 0; i < b->instr_count; ++i)
2716 ir_instr_dump(b->instr[i], ind, oprintf);
2717 ind[strlen(ind)-1] = 0;
2720 void dump_phi(ir_instr *in, char *ind,
2721 int (*oprintf)(const char*, ...))
2724 oprintf("%s <- phi ", in->_ops[0]->name);
2725 for (i = 0; i < in->phi_count; ++i)
2727 oprintf("([%s] : %s) ", in->phi[i].from->label,
2728 in->phi[i].value->name);
2733 void ir_instr_dump(ir_instr *in, char *ind,
2734 int (*oprintf)(const char*, ...))
2737 const char *comma = NULL;
2739 oprintf("%s (%i) ", ind, (int)in->eid);
2741 if (in->opcode == VINSTR_PHI) {
2742 dump_phi(in, ind, oprintf);
2746 strncat(ind, "\t", IND_BUFSZ);
2748 if (in->_ops[0] && (in->_ops[1] || in->_ops[2])) {
2749 ir_value_dump(in->_ops[0], oprintf);
2750 if (in->_ops[1] || in->_ops[2])
2753 oprintf("%s\t", qc_opname(in->opcode));
2754 if (in->_ops[0] && !(in->_ops[1] || in->_ops[2])) {
2755 ir_value_dump(in->_ops[0], oprintf);
2760 for (i = 1; i != 3; ++i) {
2764 ir_value_dump(in->_ops[i], oprintf);
2772 oprintf("[%s]", in->bops[0]->label);
2776 oprintf("%s[%s]", comma, in->bops[1]->label);
2778 ind[strlen(ind)-1] = 0;
2781 void ir_value_dump(ir_value* v, int (*oprintf)(const char*, ...))
2790 oprintf("%g", v->constval.vfloat);
2793 oprintf("'%g %g %g'",
2796 v->constval.vvec.z);
2799 oprintf("(entity)");
2802 oprintf("\"%s\"", v->constval.vstring);
2806 oprintf("%i", v->constval.vint);
2811 v->constval.vpointer->name);
2815 oprintf("%s", v->name);
2819 void ir_value_dump_life(ir_value *self, int (*oprintf)(const char*,...))
2822 oprintf("Life of %s:\n", self->name);
2823 for (i = 0; i < self->life_count; ++i)
2825 oprintf(" + [%i, %i]\n", self->life[i].start, self->life[i].end);