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 size_t type_sizeof[TYPE_COUNT] = {
39 1, /* TYPE_FUNCTION */
47 /***********************************************************************
51 ir_builder* ir_builder_new(const char *modulename)
55 self = (ir_builder*)mem_a(sizeof(*self));
59 MEM_VECTOR_INIT(self, functions);
60 MEM_VECTOR_INIT(self, globals);
62 if (!ir_builder_set_name(self, modulename)) {
67 /* globals which always exist */
69 /* for now we give it a vector size */
70 ir_builder_create_global(self, "OFS_RETURN", TYPE_VARIANT);
75 MEM_VEC_FUNCTIONS(ir_builder, ir_value*, globals)
76 MEM_VEC_FUNCTIONS(ir_builder, ir_function*, functions)
78 void ir_builder_delete(ir_builder* self)
81 mem_d((void*)self->name);
82 for (i = 0; i != self->functions_count; ++i) {
83 ir_function_delete(self->functions[i]);
85 MEM_VECTOR_CLEAR(self, functions);
86 for (i = 0; i != self->globals_count; ++i) {
87 ir_value_delete(self->globals[i]);
89 MEM_VECTOR_CLEAR(self, globals);
93 bool ir_builder_set_name(ir_builder *self, const char *name)
96 mem_d((void*)self->name);
97 self->name = util_strdup(name);
101 ir_function* ir_builder_get_function(ir_builder *self, const char *name)
104 for (i = 0; i < self->functions_count; ++i) {
105 if (!strcmp(name, self->functions[i]->name))
106 return self->functions[i];
111 ir_function* ir_builder_create_function(ir_builder *self, const char *name)
113 ir_function *fn = ir_builder_get_function(self, name);
118 fn = ir_function_new(self);
119 if (!ir_function_set_name(fn, name) ||
120 !ir_builder_functions_add(self, fn) )
122 ir_function_delete(fn);
128 ir_value* ir_builder_get_global(ir_builder *self, const char *name)
131 for (i = 0; i < self->globals_count; ++i) {
132 if (!strcmp(self->globals[i]->name, name))
133 return self->globals[i];
138 ir_value* ir_builder_create_global(ir_builder *self, const char *name, int vtype)
140 ir_value *ve = ir_builder_get_global(self, name);
145 ve = ir_value_var(name, store_global, vtype);
146 if (!ir_builder_globals_add(self, ve)) {
153 /***********************************************************************
157 bool ir_function_naive_phi(ir_function*);
158 void ir_function_enumerate(ir_function*);
159 bool ir_function_calculate_liferanges(ir_function*);
160 bool ir_function_allocate_locals(ir_function*);
162 ir_function* ir_function_new(ir_builder* owner)
165 self = (ir_function*)mem_a(sizeof(*self));
171 if (!ir_function_set_name(self, "<@unnamed>")) {
176 self->context.file = "<@no context>";
177 self->context.line = 0;
178 self->retype = TYPE_VOID;
179 MEM_VECTOR_INIT(self, params);
180 MEM_VECTOR_INIT(self, blocks);
181 MEM_VECTOR_INIT(self, values);
182 MEM_VECTOR_INIT(self, locals);
187 MEM_VEC_FUNCTIONS(ir_function, ir_value*, values)
188 MEM_VEC_FUNCTIONS(ir_function, ir_block*, blocks)
189 MEM_VEC_FUNCTIONS(ir_function, ir_value*, locals)
191 bool ir_function_set_name(ir_function *self, const char *name)
194 mem_d((void*)self->name);
195 self->name = util_strdup(name);
199 void ir_function_delete(ir_function *self)
202 mem_d((void*)self->name);
204 for (i = 0; i != self->blocks_count; ++i)
205 ir_block_delete(self->blocks[i]);
206 MEM_VECTOR_CLEAR(self, blocks);
208 MEM_VECTOR_CLEAR(self, params);
210 for (i = 0; i != self->values_count; ++i)
211 ir_value_delete(self->values[i]);
212 MEM_VECTOR_CLEAR(self, values);
214 for (i = 0; i != self->locals_count; ++i)
215 ir_value_delete(self->locals[i]);
216 MEM_VECTOR_CLEAR(self, locals);
221 bool GMQCC_WARN ir_function_collect_value(ir_function *self, ir_value *v)
223 return ir_function_values_add(self, v);
226 ir_block* ir_function_create_block(ir_function *self, const char *label)
228 ir_block* bn = ir_block_new(self, label);
229 memcpy(&bn->context, &self->context, sizeof(self->context));
230 if (!ir_function_blocks_add(self, bn)) {
237 bool ir_function_finalize(ir_function *self)
239 if (!ir_function_naive_phi(self))
242 ir_function_enumerate(self);
244 if (!ir_function_calculate_liferanges(self))
247 if (!ir_function_allocate_locals(self))
252 ir_value* ir_function_get_local(ir_function *self, const char *name)
255 for (i = 0; i < self->locals_count; ++i) {
256 if (!strcmp(self->locals[i]->name, name))
257 return self->locals[i];
262 ir_value* ir_function_create_local(ir_function *self, const char *name, int vtype)
264 ir_value *ve = ir_function_get_local(self, name);
269 ve = ir_value_var(name, store_local, vtype);
270 if (!ir_function_locals_add(self, ve)) {
277 /***********************************************************************
281 ir_block* ir_block_new(ir_function* owner, const char *name)
284 self = (ir_block*)mem_a(sizeof(*self));
288 memset(self, 0, sizeof(*self));
291 if (!ir_block_set_label(self, name)) {
296 self->context.file = "<@no context>";
297 self->context.line = 0;
299 MEM_VECTOR_INIT(self, instr);
300 MEM_VECTOR_INIT(self, entries);
301 MEM_VECTOR_INIT(self, exits);
304 self->is_return = false;
306 MEM_VECTOR_INIT(self, living);
308 self->generated = false;
312 MEM_VEC_FUNCTIONS(ir_block, ir_instr*, instr)
313 MEM_VEC_FUNCTIONS_ALL(ir_block, ir_block*, entries)
314 MEM_VEC_FUNCTIONS_ALL(ir_block, ir_block*, exits)
315 MEM_VEC_FUNCTIONS_ALL(ir_block, ir_value*, living)
317 void ir_block_delete(ir_block* self)
321 for (i = 0; i != self->instr_count; ++i)
322 ir_instr_delete(self->instr[i]);
323 MEM_VECTOR_CLEAR(self, instr);
324 MEM_VECTOR_CLEAR(self, entries);
325 MEM_VECTOR_CLEAR(self, exits);
326 MEM_VECTOR_CLEAR(self, living);
330 bool ir_block_set_label(ir_block *self, const char *name)
333 mem_d((void*)self->label);
334 self->label = util_strdup(name);
335 return !!self->label;
338 /***********************************************************************
342 ir_instr* ir_instr_new(ir_block* owner, int op)
345 self = (ir_instr*)mem_a(sizeof(*self));
350 self->context.file = "<@no context>";
351 self->context.line = 0;
353 self->_ops[0] = NULL;
354 self->_ops[1] = NULL;
355 self->_ops[2] = NULL;
356 self->bops[0] = NULL;
357 self->bops[1] = NULL;
358 MEM_VECTOR_INIT(self, phi);
359 MEM_VECTOR_INIT(self, params);
364 MEM_VEC_FUNCTIONS(ir_instr, ir_phi_entry_t, phi)
366 void ir_instr_delete(ir_instr *self)
369 /* The following calls can only delete from
370 * vectors, we still want to delete this instruction
371 * so ignore the return value. Since with the warn_unused_result attribute
372 * gcc doesn't care about an explicit: (void)foo(); to ignore the result,
373 * I have to improvise here and use if(foo());
375 for (i = 0; i < self->phi_count; ++i) {
377 if (ir_value_writes_find(self->phi[i].value, self, &idx))
378 if (ir_value_writes_remove(self->phi[i].value, idx)) GMQCC_SUPRESS_EMPTY_BODY;
379 if (ir_value_reads_find(self->phi[i].value, self, &idx))
380 if (ir_value_reads_remove (self->phi[i].value, idx)) GMQCC_SUPRESS_EMPTY_BODY;
382 MEM_VECTOR_CLEAR(self, phi);
383 for (i = 0; i < self->params_count; ++i) {
385 if (ir_value_writes_find(self->params[i], self, &idx))
386 if (ir_value_writes_remove(self->params[i], idx)) GMQCC_SUPRESS_EMPTY_BODY;
387 if (ir_value_reads_find(self->params[i], self, &idx))
388 if (ir_value_reads_remove (self->params[i], idx)) GMQCC_SUPRESS_EMPTY_BODY;
390 MEM_VECTOR_CLEAR(self, params);
391 if (ir_instr_op(self, 0, NULL, false)) GMQCC_SUPRESS_EMPTY_BODY;
392 if (ir_instr_op(self, 1, NULL, false)) GMQCC_SUPRESS_EMPTY_BODY;
393 if (ir_instr_op(self, 2, NULL, false)) GMQCC_SUPRESS_EMPTY_BODY;
397 bool ir_instr_op(ir_instr *self, int op, ir_value *v, bool writing)
399 if (self->_ops[op]) {
401 if (writing && ir_value_writes_find(self->_ops[op], self, &idx))
403 if (!ir_value_writes_remove(self->_ops[op], idx))
406 else if (ir_value_reads_find(self->_ops[op], self, &idx))
408 if (!ir_value_reads_remove(self->_ops[op], idx))
414 if (!ir_value_writes_add(v, self))
417 if (!ir_value_reads_add(v, self))
425 /***********************************************************************
429 ir_value* ir_value_var(const char *name, int storetype, int vtype)
432 self = (ir_value*)mem_a(sizeof(*self));
434 self->fieldtype = TYPE_VOID;
435 self->store = storetype;
436 MEM_VECTOR_INIT(self, reads);
437 MEM_VECTOR_INIT(self, writes);
438 self->isconst = false;
439 self->context.file = "<@no context>";
440 self->context.line = 0;
442 ir_value_set_name(self, name);
444 memset(&self->constval, 0, sizeof(self->constval));
445 memset(&self->code, 0, sizeof(self->code));
447 MEM_VECTOR_INIT(self, life);
450 MEM_VEC_FUNCTIONS(ir_value, ir_life_entry_t, life)
451 MEM_VEC_FUNCTIONS_ALL(ir_value, ir_instr*, reads)
452 MEM_VEC_FUNCTIONS_ALL(ir_value, ir_instr*, writes)
454 ir_value* ir_value_out(ir_function *owner, const char *name, int storetype, int vtype)
456 ir_value *v = ir_value_var(name, storetype, vtype);
459 if (!ir_function_collect_value(owner, v))
467 void ir_value_delete(ir_value* self)
470 mem_d((void*)self->name);
473 if (self->vtype == TYPE_STRING)
474 mem_d((void*)self->constval.vstring);
476 MEM_VECTOR_CLEAR(self, reads);
477 MEM_VECTOR_CLEAR(self, writes);
478 MEM_VECTOR_CLEAR(self, life);
482 void ir_value_set_name(ir_value *self, const char *name)
485 mem_d((void*)self->name);
486 self->name = util_strdup(name);
489 bool ir_value_set_float(ir_value *self, float f)
491 if (self->vtype != TYPE_FLOAT)
493 self->constval.vfloat = f;
494 self->isconst = true;
498 bool ir_value_set_vector(ir_value *self, vector v)
500 if (self->vtype != TYPE_VECTOR)
502 self->constval.vvec = v;
503 self->isconst = true;
507 bool ir_value_set_string(ir_value *self, const char *str)
509 if (self->vtype != TYPE_STRING)
511 self->constval.vstring = util_strdup(str);
512 self->isconst = true;
517 bool ir_value_set_int(ir_value *self, int i)
519 if (self->vtype != TYPE_INTEGER)
521 self->constval.vint = i;
522 self->isconst = true;
527 bool ir_value_lives(ir_value *self, size_t at)
530 for (i = 0; i < self->life_count; ++i)
532 ir_life_entry_t *life = &self->life[i];
533 if (life->start <= at && at <= life->end)
535 if (life->start > at) /* since it's ordered */
541 bool ir_value_life_insert(ir_value *self, size_t idx, ir_life_entry_t e)
544 if (!ir_value_life_add(self, e)) /* naive... */
546 for (k = self->life_count-1; k > idx; --k)
547 self->life[k] = self->life[k-1];
552 bool ir_value_life_merge(ir_value *self, size_t s)
555 ir_life_entry_t *life = NULL;
556 ir_life_entry_t *before = NULL;
557 ir_life_entry_t new_entry;
559 /* Find the first range >= s */
560 for (i = 0; i < self->life_count; ++i)
563 life = &self->life[i];
567 /* nothing found? append */
568 if (i == self->life_count) {
570 if (life && life->end+1 == s)
572 /* previous life range can be merged in */
576 if (life && life->end >= s)
579 if (!ir_value_life_add(self, e))
580 return false; /* failing */
586 if (before->end + 1 == s &&
587 life->start - 1 == s)
590 before->end = life->end;
591 if (!ir_value_life_remove(self, i))
592 return false; /* failing */
595 if (before->end + 1 == s)
601 /* already contained */
602 if (before->end >= s)
606 if (life->start - 1 == s)
611 /* insert a new entry */
612 new_entry.start = new_entry.end = s;
613 return ir_value_life_insert(self, i, new_entry);
616 bool ir_value_life_merge_into(ir_value *self, const ir_value *other)
620 if (!other->life_count)
623 if (!self->life_count) {
624 for (i = 0; i < other->life_count; ++i) {
625 if (!ir_value_life_add(self, other->life[i]))
632 for (i = 0; i < other->life_count; ++i)
634 const ir_life_entry_t *life = &other->life[i];
637 ir_life_entry_t *entry = &self->life[myi];
639 if (life->end+1 < entry->start)
641 /* adding an interval before entry */
642 if (!ir_value_life_insert(self, myi, *life))
648 if (life->start < entry->start &&
649 life->end >= entry->start)
651 /* starts earlier and overlaps */
652 entry->start = life->start;
655 if (life->end > entry->end &&
656 life->start-1 <= entry->end)
658 /* ends later and overlaps */
659 entry->end = life->end;
662 /* see if our change combines it with the next ranges */
663 while (myi+1 < self->life_count &&
664 entry->end+1 >= self->life[1+myi].start)
666 /* overlaps with (myi+1) */
667 if (entry->end < self->life[1+myi].end)
668 entry->end = self->life[1+myi].end;
669 if (!ir_value_life_remove(self, myi+1))
671 entry = &self->life[myi];
674 /* see if we're after the entry */
675 if (life->start > entry->end)
678 /* append if we're at the end */
679 if (myi >= self->life_count) {
680 if (!ir_value_life_add(self, *life))
684 /* otherweise check the next range */
693 bool ir_values_overlap(const ir_value *a, const ir_value *b)
695 /* For any life entry in A see if it overlaps with
696 * any life entry in B.
697 * Note that the life entries are orderes, so we can make a
698 * more efficient algorithm there than naively translating the
702 ir_life_entry_t *la, *lb, *enda, *endb;
704 /* first of all, if either has no life range, they cannot clash */
705 if (!a->life_count || !b->life_count)
710 enda = la + a->life_count;
711 endb = lb + b->life_count;
714 /* check if the entries overlap, for that,
715 * both must start before the other one ends.
717 #if defined(LIFE_RANGE_WITHOUT_LAST_READ)
718 if (la->start <= lb->end &&
719 lb->start <= la->end)
721 if (la->start < lb->end &&
728 /* entries are ordered
729 * one entry is earlier than the other
730 * that earlier entry will be moved forward
732 if (la->start < lb->start)
734 /* order: A B, move A forward
735 * check if we hit the end with A
740 else if (lb->start < la->start)
742 /* order: B A, move B forward
743 * check if we hit the end with B
752 /***********************************************************************
756 bool ir_block_create_store_op(ir_block *self, int op, ir_value *target, ir_value *what)
758 if (target->store == store_value) {
759 fprintf(stderr, "cannot store to an SSA value\n");
760 fprintf(stderr, "trying to store: %s <- %s\n", target->name, what->name);
763 ir_instr *in = ir_instr_new(self, op);
766 if (!ir_instr_op(in, 0, target, true) ||
767 !ir_instr_op(in, 1, what, false) ||
768 !ir_block_instr_add(self, in) )
776 bool ir_block_create_store(ir_block *self, ir_value *target, ir_value *what)
780 if (target->vtype == TYPE_VARIANT)
783 vtype = target->vtype;
788 if (what->vtype == TYPE_INTEGER)
789 op = INSTR_CONV_ITOF;
798 op = INSTR_STORE_ENT;
804 op = INSTR_STORE_FLD;
808 if (what->vtype == TYPE_INTEGER)
809 op = INSTR_CONV_FTOI;
818 op = INSTR_STORE_ENT;
825 return ir_block_create_store_op(self, op, target, what);
828 bool ir_block_create_storep(ir_block *self, ir_value *target, ir_value *what)
833 if (target->vtype != TYPE_POINTER)
836 /* storing using pointer - target is a pointer, type must be
837 * inferred from source
849 op = INSTR_STOREP_ENT;
855 op = INSTR_STOREP_FLD;
866 op = INSTR_STOREP_ENT;
873 return ir_block_create_store_op(self, op, target, what);
876 bool ir_block_create_return(ir_block *self, ir_value *v)
880 fprintf(stderr, "block already ended (%s)\n", self->label);
884 self->is_return = true;
885 in = ir_instr_new(self, INSTR_RETURN);
889 if (!ir_instr_op(in, 0, v, false) ||
890 !ir_block_instr_add(self, in) )
897 bool ir_block_create_if(ir_block *self, ir_value *v,
898 ir_block *ontrue, ir_block *onfalse)
902 fprintf(stderr, "block already ended (%s)\n", self->label);
906 /*in = ir_instr_new(self, (v->vtype == TYPE_STRING ? INSTR_IF_S : INSTR_IF_F));*/
907 in = ir_instr_new(self, VINSTR_COND);
911 if (!ir_instr_op(in, 0, v, false)) {
916 in->bops[0] = ontrue;
917 in->bops[1] = onfalse;
919 if (!ir_block_instr_add(self, in))
922 if (!ir_block_exits_add(self, ontrue) ||
923 !ir_block_exits_add(self, onfalse) ||
924 !ir_block_entries_add(ontrue, self) ||
925 !ir_block_entries_add(onfalse, self) )
932 bool ir_block_create_jump(ir_block *self, ir_block *to)
936 fprintf(stderr, "block already ended (%s)\n", self->label);
940 in = ir_instr_new(self, VINSTR_JUMP);
945 if (!ir_block_instr_add(self, in))
948 if (!ir_block_exits_add(self, to) ||
949 !ir_block_entries_add(to, self) )
956 bool ir_block_create_goto(ir_block *self, ir_block *to)
960 fprintf(stderr, "block already ended (%s)\n", self->label);
964 in = ir_instr_new(self, INSTR_GOTO);
969 if (!ir_block_instr_add(self, in))
972 if (!ir_block_exits_add(self, to) ||
973 !ir_block_entries_add(to, self) )
980 ir_instr* ir_block_create_phi(ir_block *self, const char *label, int ot)
984 in = ir_instr_new(self, VINSTR_PHI);
987 out = ir_value_out(self->owner, label, store_value, ot);
992 if (!ir_instr_op(in, 0, out, true)) {
994 ir_value_delete(out);
997 if (!ir_block_instr_add(self, in)) {
999 ir_value_delete(out);
1005 ir_value* ir_phi_value(ir_instr *self)
1007 return self->_ops[0];
1010 bool ir_phi_add(ir_instr* self, ir_block *b, ir_value *v)
1014 if (!ir_block_entries_find(self->owner, b, NULL)) {
1015 /* Must not be possible to cause this, otherwise the AST
1016 * is doing something wrong.
1018 fprintf(stderr, "Invalid entry block for PHI\n");
1024 if (!ir_value_reads_add(v, self))
1026 return ir_instr_phi_add(self, pe);
1029 /* binary op related code */
1031 ir_value* ir_block_create_binop(ir_block *self,
1032 const char *label, int opcode,
1033 ir_value *left, ir_value *right)
1055 case INSTR_SUB_S: /* -- offset of string as float */
1060 case INSTR_BITOR_IF:
1061 case INSTR_BITOR_FI:
1062 case INSTR_BITAND_FI:
1063 case INSTR_BITAND_IF:
1078 case INSTR_BITAND_I:
1081 case INSTR_RSHIFT_I:
1082 case INSTR_LSHIFT_I:
1104 /* boolean operations result in floats */
1105 if (opcode >= INSTR_EQ_F && opcode <= INSTR_GT)
1107 else if (opcode >= INSTR_LE && opcode <= INSTR_GT)
1110 else if (opcode >= INSTR_LE_I && opcode <= INSTR_EQ_FI)
1115 if (ot == TYPE_VOID) {
1116 /* The AST or parser were supposed to check this! */
1120 return ir_block_create_general_instr(self, label, opcode, left, right, ot);
1123 ir_value* ir_block_create_general_instr(ir_block *self, const char *label,
1124 int op, ir_value *a, ir_value *b, int outype)
1129 out = ir_value_out(self->owner, label, store_value, outype);
1133 instr = ir_instr_new(self, op);
1135 ir_value_delete(out);
1139 if (!ir_instr_op(instr, 0, out, true) ||
1140 !ir_instr_op(instr, 1, a, false) ||
1141 !ir_instr_op(instr, 2, b, false) )
1146 if (!ir_block_instr_add(self, instr))
1151 ir_instr_delete(instr);
1152 ir_value_delete(out);
1156 ir_value* ir_block_create_fieldaddress(ir_block *self, const char *label, ir_value *ent, ir_value *field)
1158 /* Support for various pointer types todo if so desired */
1159 if (ent->vtype != TYPE_ENTITY)
1162 if (field->vtype != TYPE_FIELD)
1165 return ir_block_create_general_instr(self, label, INSTR_ADDRESS, ent, field, TYPE_POINTER);
1168 ir_value* ir_block_create_load_from_ent(ir_block *self, const char *label, ir_value *ent, ir_value *field, int outype)
1171 if (ent->vtype != TYPE_ENTITY)
1174 /* at some point we could redirect for TYPE_POINTER... but that could lead to carelessness */
1175 if (field->vtype != TYPE_FIELD)
1180 case TYPE_FLOAT: op = INSTR_LOAD_F; break;
1181 case TYPE_VECTOR: op = INSTR_LOAD_V; break;
1182 case TYPE_STRING: op = INSTR_LOAD_S; break;
1183 case TYPE_FIELD: op = INSTR_LOAD_FLD; break;
1184 case TYPE_ENTITY: op = INSTR_LOAD_ENT; break;
1186 case TYPE_POINTER: op = INSTR_LOAD_I; break;
1187 case TYPE_INTEGER: op = INSTR_LOAD_I; break;
1193 return ir_block_create_general_instr(self, label, op, ent, field, outype);
1196 ir_value* ir_block_create_add(ir_block *self,
1198 ir_value *left, ir_value *right)
1201 int l = left->vtype;
1202 int r = right->vtype;
1221 if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1223 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1229 return ir_block_create_binop(self, label, op, left, right);
1232 ir_value* ir_block_create_sub(ir_block *self,
1234 ir_value *left, ir_value *right)
1237 int l = left->vtype;
1238 int r = right->vtype;
1258 if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1260 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1266 return ir_block_create_binop(self, label, op, left, right);
1269 ir_value* ir_block_create_mul(ir_block *self,
1271 ir_value *left, ir_value *right)
1274 int l = left->vtype;
1275 int r = right->vtype;
1294 if ( (l == TYPE_VECTOR && r == TYPE_FLOAT) )
1296 else if ( (l == TYPE_FLOAT && r == TYPE_VECTOR) )
1299 else if ( (l == TYPE_VECTOR && r == TYPE_INTEGER) )
1301 else if ( (l == TYPE_INTEGER && r == TYPE_VECTOR) )
1303 else if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1305 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1311 return ir_block_create_binop(self, label, op, left, right);
1314 ir_value* ir_block_create_div(ir_block *self,
1316 ir_value *left, ir_value *right)
1319 int l = left->vtype;
1320 int r = right->vtype;
1337 if ( (l == TYPE_VECTOR && r == TYPE_FLOAT) )
1339 else if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1341 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1347 return ir_block_create_binop(self, label, op, left, right);
1350 /* PHI resolving breaks the SSA, and must thus be the last
1351 * step before life-range calculation.
1354 static bool ir_block_naive_phi(ir_block *self);
1355 bool ir_function_naive_phi(ir_function *self)
1359 for (i = 0; i < self->blocks_count; ++i)
1361 if (!ir_block_naive_phi(self->blocks[i]))
1367 static bool ir_naive_phi_emit_store(ir_block *block, size_t iid, ir_value *old, ir_value *what)
1372 /* create a store */
1373 if (!ir_block_create_store(block, old, what))
1376 /* we now move it up */
1377 instr = block->instr[block->instr_count-1];
1378 for (i = block->instr_count; i > iid; --i)
1379 block->instr[i] = block->instr[i-1];
1380 block->instr[i] = instr;
1385 static bool ir_block_naive_phi(ir_block *self)
1388 /* FIXME: optionally, create_phi can add the phis
1389 * to a list so we don't need to loop through blocks
1390 * - anyway: "don't optimize YET"
1392 for (i = 0; i < self->instr_count; ++i)
1394 ir_instr *instr = self->instr[i];
1395 if (instr->opcode != VINSTR_PHI)
1398 if (!ir_block_instr_remove(self, i))
1400 --i; /* NOTE: i+1 below */
1402 for (p = 0; p < instr->phi_count; ++p)
1404 ir_value *v = instr->phi[p].value;
1405 for (w = 0; w < v->writes_count; ++w) {
1408 if (!v->writes[w]->_ops[0])
1411 /* When the write was to a global, we have to emit a mov */
1412 old = v->writes[w]->_ops[0];
1414 /* The original instruction now writes to the PHI target local */
1415 if (v->writes[w]->_ops[0] == v)
1416 v->writes[w]->_ops[0] = instr->_ops[0];
1418 if (old->store != store_value && old->store != store_local)
1420 /* If it originally wrote to a global we need to store the value
1423 if (!ir_naive_phi_emit_store(self, i+1, old, v))
1425 if (i+1 < self->instr_count)
1426 instr = self->instr[i+1];
1429 /* In case I forget and access instr later, it'll be NULL
1430 * when it's a problem, to make sure we crash, rather than accessing
1436 /* If it didn't, we can replace all reads by the phi target now. */
1438 for (r = 0; r < old->reads_count; ++r)
1441 ir_instr *ri = old->reads[r];
1442 for (op = 0; op < ri->phi_count; ++op) {
1443 if (ri->phi[op].value == old)
1444 ri->phi[op].value = v;
1446 for (op = 0; op < 3; ++op) {
1447 if (ri->_ops[op] == old)
1454 ir_instr_delete(instr);
1459 /***********************************************************************
1460 *IR Temp allocation code
1461 * Propagating value life ranges by walking through the function backwards
1462 * until no more changes are made.
1463 * In theory this should happen once more than once for every nested loop
1465 * Though this implementation might run an additional time for if nests.
1474 MEM_VEC_FUNCTIONS_ALL(new_reads_t, ir_value*, v)
1476 /* Enumerate instructions used by value's life-ranges
1478 static void ir_block_enumerate(ir_block *self, size_t *_eid)
1482 for (i = 0; i < self->instr_count; ++i)
1484 self->instr[i]->eid = eid++;
1489 /* Enumerate blocks and instructions.
1490 * The block-enumeration is unordered!
1491 * We do not really use the block enumreation, however
1492 * the instruction enumeration is important for life-ranges.
1494 void ir_function_enumerate(ir_function *self)
1497 size_t instruction_id = 0;
1498 for (i = 0; i < self->blocks_count; ++i)
1500 self->blocks[i]->eid = i;
1501 self->blocks[i]->run_id = 0;
1502 ir_block_enumerate(self->blocks[i], &instruction_id);
1506 static bool ir_block_life_propagate(ir_block *b, ir_block *prev, bool *changed);
1507 bool ir_function_calculate_liferanges(ir_function *self)
1515 for (i = 0; i != self->blocks_count; ++i)
1517 if (self->blocks[i]->is_return)
1519 if (!ir_block_life_propagate(self->blocks[i], NULL, &changed))
1527 /* Local-value allocator
1528 * After finishing creating the liferange of all values used in a function
1529 * we can allocate their global-positions.
1530 * This is the counterpart to register-allocation in register machines.
1533 MEM_VECTOR_MAKE(ir_value*, locals);
1534 MEM_VECTOR_MAKE(size_t, sizes);
1535 MEM_VECTOR_MAKE(size_t, positions);
1536 } function_allocator;
1537 MEM_VEC_FUNCTIONS(function_allocator, ir_value*, locals)
1538 MEM_VEC_FUNCTIONS(function_allocator, size_t, sizes)
1539 MEM_VEC_FUNCTIONS(function_allocator, size_t, positions)
1541 static bool function_allocator_alloc(function_allocator *alloc, const ir_value *var)
1544 size_t vsize = type_sizeof[var->vtype];
1546 slot = ir_value_var("reg", store_global, var->vtype);
1550 if (!ir_value_life_merge_into(slot, var))
1553 if (!function_allocator_locals_add(alloc, slot))
1556 if (!function_allocator_sizes_add(alloc, vsize))
1562 ir_value_delete(slot);
1566 bool ir_function_allocate_locals(ir_function *self)
1575 function_allocator alloc;
1577 MEM_VECTOR_INIT(&alloc, locals);
1578 MEM_VECTOR_INIT(&alloc, sizes);
1579 MEM_VECTOR_INIT(&alloc, positions);
1581 for (i = 0; i < self->locals_count; ++i)
1583 if (!function_allocator_alloc(&alloc, self->locals[i]))
1587 /* Allocate a slot for any value that still exists */
1588 for (i = 0; i < self->values_count; ++i)
1590 v = self->values[i];
1595 for (a = 0; a < alloc.locals_count; ++a)
1597 slot = alloc.locals[a];
1599 if (ir_values_overlap(v, slot))
1602 if (!ir_value_life_merge_into(slot, v))
1605 /* adjust size for this slot */
1606 if (alloc.sizes[a] < type_sizeof[v->vtype])
1607 alloc.sizes[a] = type_sizeof[v->vtype];
1609 self->values[i]->code.local = a;
1612 if (a >= alloc.locals_count) {
1613 self->values[i]->code.local = alloc.locals_count;
1614 if (!function_allocator_alloc(&alloc, v))
1619 /* Adjust slot positions based on sizes */
1620 if (!function_allocator_positions_add(&alloc, 0))
1623 for (i = 1; i < alloc.sizes_count; ++i)
1625 pos = alloc.positions[i-1] + alloc.sizes[i-1];
1626 if (!function_allocator_positions_add(&alloc, pos))
1630 self->allocated_locals = pos + alloc.sizes[alloc.sizes_count-1];
1632 /* Take over the actual slot positions */
1633 for (i = 0; i < self->values_count; ++i)
1634 self->values[i]->code.local = alloc.positions[self->values[i]->code.local];
1641 for (i = 0; i < alloc.locals_count; ++i)
1642 ir_value_delete(alloc.locals[i]);
1643 MEM_VECTOR_CLEAR(&alloc, locals);
1644 MEM_VECTOR_CLEAR(&alloc, sizes);
1645 MEM_VECTOR_CLEAR(&alloc, positions);
1649 /* Get information about which operand
1650 * is read from, or written to.
1652 static void ir_op_read_write(int op, size_t *read, size_t *write)
1679 static bool ir_block_living_add_instr(ir_block *self, size_t eid)
1682 bool changed = false;
1684 for (i = 0; i != self->living_count; ++i)
1686 tempbool = ir_value_life_merge(self->living[i], eid);
1689 fprintf(stderr, "block_living_add_instr() value instruction added %s: %i\n", self->living[i]->_name, (int)eid);
1691 changed = changed || tempbool;
1696 static bool ir_block_life_prop_previous(ir_block* self, ir_block *prev, bool *changed)
1699 /* values which have been read in a previous iteration are now
1700 * in the "living" array even if the previous block doesn't use them.
1701 * So we have to remove whatever does not exist in the previous block.
1702 * They will be re-added on-read, but the liferange merge won't cause
1705 for (i = 0; i < self->living_count; ++i)
1707 if (!ir_block_living_find(prev, self->living[i], NULL)) {
1708 if (!ir_block_living_remove(self, i))
1714 /* Whatever the previous block still has in its living set
1715 * must now be added to ours as well.
1717 for (i = 0; i < prev->living_count; ++i)
1719 if (ir_block_living_find(self, prev->living[i], NULL))
1721 if (!ir_block_living_add(self, prev->living[i]))
1724 printf("%s got from prev: %s\n", self->label, prev->living[i]->_name);
1730 static bool ir_block_life_propagate(ir_block *self, ir_block *prev, bool *changed)
1736 /* bitmasks which operands are read from or written to */
1738 #if defined(LIFE_RANGE_WITHOUT_LAST_READ)
1740 new_reads_t new_reads;
1742 char dbg_ind[16] = { '#', '0' };
1745 #if defined(LIFE_RANGE_WITHOUT_LAST_READ)
1746 MEM_VECTOR_INIT(&new_reads, v);
1751 if (!ir_block_life_prop_previous(self, prev, changed))
1755 i = self->instr_count;
1758 instr = self->instr[i];
1760 /* PHI operands are always read operands */
1761 for (p = 0; p < instr->phi_count; ++p)
1763 value = instr->phi[p].value;
1764 #if ! defined(LIFE_RANGE_WITHOUT_LAST_READ)
1765 if (!ir_block_living_find(self, value, NULL) &&
1766 !ir_block_living_add(self, value))
1771 if (!new_reads_t_v_find(&new_reads, value, NULL))
1773 if (!new_reads_t_v_add(&new_reads, value))
1779 /* See which operands are read and write operands */
1780 ir_op_read_write(instr->opcode, &read, &write);
1782 /* Go through the 3 main operands */
1783 for (o = 0; o < 3; ++o)
1785 if (!instr->_ops[o]) /* no such operand */
1788 value = instr->_ops[o];
1790 /* We only care about locals */
1791 if (value->store != store_value &&
1792 value->store != store_local)
1798 #if ! defined(LIFE_RANGE_WITHOUT_LAST_READ)
1799 if (!ir_block_living_find(self, value, NULL) &&
1800 !ir_block_living_add(self, value))
1805 /* fprintf(stderr, "read: %s\n", value->_name); */
1806 if (!new_reads_t_v_find(&new_reads, value, NULL))
1808 if (!new_reads_t_v_add(&new_reads, value))
1814 /* write operands */
1815 /* When we write to a local, we consider it "dead" for the
1816 * remaining upper part of the function, since in SSA a value
1817 * can only be written once (== created)
1822 bool in_living = ir_block_living_find(self, value, &idx);
1823 #if defined(LIFE_RANGE_WITHOUT_LAST_READ)
1825 bool in_reads = new_reads_t_v_find(&new_reads, value, &readidx);
1826 if (!in_living && !in_reads)
1831 /* If the value isn't alive it hasn't been read before... */
1832 /* TODO: See if the warning can be emitted during parsing or AST processing
1833 * otherwise have warning printed here.
1834 * IF printing a warning here: include filecontext_t,
1835 * and make sure it's only printed once
1836 * since this function is run multiple times.
1838 /* For now: debug info: */
1839 fprintf(stderr, "Value only written %s\n", value->name);
1840 tempbool = ir_value_life_merge(value, instr->eid);
1841 *changed = *changed || tempbool;
1843 ir_instr_dump(instr, dbg_ind, printf);
1847 /* since 'living' won't contain it
1848 * anymore, merge the value, since
1851 tempbool = ir_value_life_merge(value, instr->eid);
1854 fprintf(stderr, "value added id %s %i\n", value->name, (int)instr->eid);
1856 *changed = *changed || tempbool;
1858 #if ! defined(LIFE_RANGE_WITHOUT_LAST_READ)
1859 if (!ir_block_living_remove(self, idx))
1864 if (!new_reads_t_v_remove(&new_reads, readidx))
1872 tempbool = ir_block_living_add_instr(self, instr->eid);
1873 /*fprintf(stderr, "living added values\n");*/
1874 *changed = *changed || tempbool;
1876 #if defined(LIFE_RANGE_WITHOUT_LAST_READ)
1878 for (rd = 0; rd < new_reads.v_count; ++rd)
1880 if (!ir_block_living_find(self, new_reads.v[rd], NULL)) {
1881 if (!ir_block_living_add(self, new_reads.v[rd]))
1884 if (!i && !self->entries_count) {
1886 *changed = *changed || ir_value_life_merge(new_reads.v[rd], instr->eid);
1889 MEM_VECTOR_CLEAR(&new_reads, v);
1893 if (self->run_id == self->owner->run_id)
1896 self->run_id = self->owner->run_id;
1898 for (i = 0; i < self->entries_count; ++i)
1900 ir_block *entry = self->entries[i];
1901 ir_block_life_propagate(entry, self, changed);
1906 #if defined(LIFE_RANGE_WITHOUT_LAST_READ)
1907 MEM_VECTOR_CLEAR(&new_reads, v);
1912 /***********************************************************************
1915 * Since the IR has the convention of putting 'write' operands
1916 * at the beginning, we have to rotate the operands of instructions
1917 * properly in order to generate valid QCVM code.
1919 * Having destinations at a fixed position is more convenient. In QC
1920 * this is *mostly* OPC, but FTE adds at least 2 instructions which
1921 * read from from OPA, and store to OPB rather than OPC. Which is
1922 * partially the reason why the implementation of these instructions
1923 * in darkplaces has been delayed for so long.
1925 * Breaking conventions is annoying...
1927 static bool ir_builder_gen_global(ir_builder *self, ir_value *global);
1929 static bool gen_global_field(ir_value *global)
1931 if (global->isconst)
1933 ir_value *fld = global->constval.vpointer;
1935 printf("Invalid field constant with no field: %s\n", global->name);
1939 /* Now, in this case, a relocation would be impossible to code
1940 * since it looks like this:
1941 * .vector v = origin; <- parse error, wtf is 'origin'?
1944 * But we will need a general relocation support later anyway
1945 * for functions... might as well support that here.
1947 if (!fld->code.globaladdr) {
1948 printf("FIXME: Relocation support\n");
1952 /* copy the field's value */
1953 global->code.globaladdr = code_globals_add(code_globals_data[fld->code.globaladdr]);
1957 prog_section_field fld;
1959 fld.name = global->code.name;
1960 fld.offset = code_fields_elements;
1961 fld.type = global->fieldtype;
1963 if (fld.type == TYPE_VOID) {
1964 printf("Field is missing a type: %s\n", global->name);
1968 if (code_fields_add(fld) < 0)
1971 global->code.globaladdr = code_globals_add(fld.offset);
1973 if (global->code.globaladdr < 0)
1978 static bool gen_global_pointer(ir_value *global)
1980 if (global->isconst)
1982 ir_value *target = global->constval.vpointer;
1984 printf("Invalid pointer constant: %s\n", global->name);
1985 /* NULL pointers are pointing to the NULL constant, which also
1986 * sits at address 0, but still has an ir_value for itself.
1991 /* Here, relocations ARE possible - in fteqcc-enhanced-qc:
1992 * void() foo; <- proto
1993 * void() *fooptr = &foo;
1994 * void() foo = { code }
1996 if (!target->code.globaladdr) {
1997 /* FIXME: Check for the constant nullptr ir_value!
1998 * because then code.globaladdr being 0 is valid.
2000 printf("FIXME: Relocation support\n");
2004 global->code.globaladdr = code_globals_add(target->code.globaladdr);
2008 global->code.globaladdr = code_globals_add(0);
2010 if (global->code.globaladdr < 0)
2015 static bool gen_blocks_recursive(ir_function *func, ir_block *block)
2017 prog_section_statement stmt;
2026 block->generated = true;
2027 block->code_start = code_statements_elements;
2028 for (i = 0; i < block->instr_count; ++i)
2030 instr = block->instr[i];
2032 if (instr->opcode == VINSTR_PHI) {
2033 printf("cannot generate virtual instruction (phi)\n");
2037 if (instr->opcode == VINSTR_JUMP) {
2038 target = instr->bops[0];
2039 /* for uncoditional jumps, if the target hasn't been generated
2040 * yet, we generate them right here.
2042 if (!target->generated) {
2047 /* otherwise we generate a jump instruction */
2048 stmt.opcode = INSTR_GOTO;
2049 stmt.o1.s1 = (target->code_start) - code_statements_elements;
2052 if (code_statements_add(stmt) < 0)
2055 /* no further instructions can be in this block */
2059 if (instr->opcode == VINSTR_COND) {
2060 ontrue = instr->bops[0];
2061 onfalse = instr->bops[1];
2062 /* TODO: have the AST signal which block should
2063 * come first: eg. optimize IFs without ELSE...
2066 stmt.o1.u1 = instr->_ops[0]->code.globaladdr;
2070 if (ontrue->generated) {
2071 stmt.opcode = INSTR_IF;
2072 stmt.o2.s1 = (ontrue->code_start-1) - code_statements_elements;
2073 if (code_statements_add(stmt) < 0)
2076 if (onfalse->generated) {
2077 stmt.opcode = INSTR_IFNOT;
2078 stmt.o2.s1 = (onfalse->code_start-1) - code_statements_elements;
2079 if (code_statements_add(stmt) < 0)
2082 if (!ontrue->generated) {
2083 if (onfalse->generated) {
2088 if (!onfalse->generated) {
2089 if (ontrue->generated) {
2094 /* neither ontrue nor onfalse exist */
2095 stmt.opcode = INSTR_IFNOT;
2096 stidx = code_statements_elements;
2097 if (code_statements_add(stmt) < 0)
2099 /* on false we jump, so add ontrue-path */
2100 if (!gen_blocks_recursive(func, ontrue))
2102 /* fixup the jump address */
2103 code_statements_data[stidx].o2.s1 = code_statements_elements - stidx;
2104 /* generate onfalse path */
2105 if (onfalse->generated) {
2106 /* fixup the jump address */
2107 code_statements_data[stidx].o2.s1 = (onfalse->code_start) - (stidx);
2108 /* may have been generated in the previous recursive call */
2109 stmt.opcode = INSTR_GOTO;
2110 stmt.o1.s1 = (onfalse->code_start) - code_statements_elements;
2113 return (code_statements_add(stmt) >= 0);
2115 /* if not, generate now */
2120 if (instr->opcode >= INSTR_CALL0 && instr->opcode <= INSTR_CALL8) {
2121 /* Trivial call translation:
2122 * copy all params to OFS_PARM*
2124 printf("TODO: call instruction\n");
2128 if (instr->opcode == INSTR_STATE) {
2129 printf("TODO: state instruction\n");
2133 stmt.opcode = instr->opcode;
2138 /* This is the general order of operands */
2140 stmt.o3.u1 = instr->_ops[0]->code.globaladdr;
2143 stmt.o1.u1 = instr->_ops[1]->code.globaladdr;
2146 stmt.o2.u1 = instr->_ops[2]->code.globaladdr;
2148 if (stmt.opcode == INSTR_RETURN || stmt.opcode == INSTR_DONE)
2150 stmt.o1.u1 = stmt.o3.u1;
2153 else if ((stmt.opcode >= INSTR_STORE_F &&
2154 stmt.opcode <= INSTR_STORE_FNC) ||
2155 (stmt.opcode >= INSTR_NOT_F &&
2156 stmt.opcode <= INSTR_NOT_FNC))
2158 /* 2-operand instructions with A -> B */
2159 stmt.o2.u1 = stmt.o3.u1;
2163 if (code_statements_add(stmt) < 0)
2169 static bool gen_function_code(ir_function *self)
2173 /* Starting from entry point, we generate blocks "as they come"
2174 * for now. Dead blocks will not be translated obviously.
2176 if (!self->blocks_count) {
2177 printf("Function '%s' declared without body.\n", self->name);
2181 block = self->blocks[0];
2182 if (block->generated)
2185 if (!gen_blocks_recursive(self, block)) {
2186 printf("failed to generate blocks for '%s'\n", self->name);
2192 static bool gen_global_function(ir_builder *ir, ir_value *global)
2194 prog_section_function fun;
2198 size_t local_var_end;
2200 if (!global->isconst ||
2201 !global->constval.vfunc)
2203 printf("Invalid state of function-global: not constant: %s\n", global->name);
2207 irfun = global->constval.vfunc;
2209 fun.name = global->code.name;
2210 fun.file = code_cachedstring(global->context.file);
2211 fun.profile = 0; /* always 0 */
2212 fun.nargs = irfun->params_count;
2214 for (i = 0;i < 8; ++i) {
2217 else if (irfun->params[i] == TYPE_VECTOR)
2223 fun.firstlocal = code_globals_elements;
2224 fun.locals = irfun->allocated_locals + irfun->locals_count;
2227 for (i = 0; i < irfun->locals_count; ++i) {
2228 if (!ir_builder_gen_global(ir, irfun->locals[i])) {
2229 printf("Failed to generate global %s\n", irfun->locals[i]->name);
2233 if (irfun->locals_count) {
2234 ir_value *last = irfun->locals[irfun->locals_count-1];
2235 local_var_end = last->code.globaladdr;
2236 local_var_end += type_sizeof[last->vtype];
2238 for (i = 0; i < irfun->values_count; ++i)
2240 /* generate code.globaladdr for ssa values */
2241 ir_value *v = irfun->values[i];
2242 v->code.globaladdr = local_var_end + v->code.local;
2244 for (i = 0; i < irfun->locals_count; ++i) {
2245 /* fill the locals with zeros */
2246 code_globals_add(0);
2249 fun.entry = code_statements_elements;
2250 if (!gen_function_code(irfun)) {
2251 printf("Failed to generate code for function %s\n", irfun->name);
2255 return (code_functions_add(fun) >= 0);
2258 static bool ir_builder_gen_global(ir_builder *self, ir_value *global)
2261 prog_section_def def;
2263 def.type = global->vtype;
2264 def.offset = code_globals_elements;
2265 def.name = global->code.name = code_genstring(global->name);
2267 switch (global->vtype)
2270 if (code_defs_add(def) < 0)
2272 return gen_global_pointer(global);
2274 if (code_defs_add(def) < 0)
2276 return gen_global_field(global);
2281 if (code_defs_add(def) < 0)
2284 if (global->isconst) {
2285 iptr = (int32_t*)&global->constval.vfloat;
2286 global->code.globaladdr = code_globals_add(*iptr);
2288 global->code.globaladdr = code_globals_add(0);
2290 return global->code.globaladdr >= 0;
2294 if (code_defs_add(def) < 0)
2296 if (global->isconst)
2297 global->code.globaladdr = code_globals_add(code_cachedstring(global->constval.vstring));
2299 global->code.globaladdr = code_globals_add(0);
2300 return global->code.globaladdr >= 0;
2304 if (code_defs_add(def) < 0)
2307 if (global->isconst) {
2308 iptr = (int32_t*)&global->constval.vvec;
2309 global->code.globaladdr = code_globals_add(iptr[0]);
2310 if (code_globals_add(iptr[1]) < 0 || code_globals_add(iptr[2]) < 0)
2313 global->code.globaladdr = code_globals_add(0);
2314 if (code_globals_add(0) < 0 || code_globals_add(0) < 0)
2317 return global->code.globaladdr >= 0;
2320 if (code_defs_add(def) < 0)
2322 code_globals_add(code_functions_elements);
2323 return gen_global_function(self, global);
2325 /* assume biggest type */
2326 global->code.globaladdr = code_globals_add(0);
2327 code_globals_add(0);
2328 code_globals_add(0);
2331 /* refuse to create 'void' type or any other fancy business. */
2332 printf("Invalid type for global variable %s\n", global->name);
2337 bool ir_builder_generate(ir_builder *self, const char *filename)
2343 /* FIXME: generate TYPE_FUNCTION globals and link them
2344 * to their ir_function.
2347 for (i = 0; i < self->functions_count; ++i)
2350 ir_function *fun = self->functions[i];
2352 funval = ir_builder_create_global(self, fun->name, TYPE_FUNCTION);
2353 funval->isconst = true;
2354 funval->constval.vfunc = fun;
2355 funval->context = fun->context;
2358 for (i = 0; i < self->globals_count; ++i)
2360 if (!ir_builder_gen_global(self, self->globals[i])) {
2365 printf("writing '%s'...\n", filename);
2366 return code_write(filename);
2369 /***********************************************************************
2370 *IR DEBUG Dump functions...
2373 #define IND_BUFSZ 1024
2375 const char *qc_opname(int op)
2377 if (op < 0) return "<INVALID>";
2378 if (op < ( sizeof(asm_instr) / sizeof(asm_instr[0]) ))
2379 return asm_instr[op].m;
2381 case VINSTR_PHI: return "PHI";
2382 case VINSTR_JUMP: return "JUMP";
2383 case VINSTR_COND: return "COND";
2384 default: return "<UNK>";
2388 void ir_builder_dump(ir_builder *b, int (*oprintf)(const char*, ...))
2391 char indent[IND_BUFSZ];
2395 oprintf("module %s\n", b->name);
2396 for (i = 0; i < b->globals_count; ++i)
2399 if (b->globals[i]->isconst)
2400 oprintf("%s = ", b->globals[i]->name);
2401 ir_value_dump(b->globals[i], oprintf);
2404 for (i = 0; i < b->functions_count; ++i)
2405 ir_function_dump(b->functions[i], indent, oprintf);
2406 oprintf("endmodule %s\n", b->name);
2409 void ir_function_dump(ir_function *f, char *ind,
2410 int (*oprintf)(const char*, ...))
2413 oprintf("%sfunction %s\n", ind, f->name);
2414 strncat(ind, "\t", IND_BUFSZ);
2415 if (f->locals_count)
2417 oprintf("%s%i locals:\n", ind, (int)f->locals_count);
2418 for (i = 0; i < f->locals_count; ++i) {
2419 oprintf("%s\t", ind);
2420 ir_value_dump(f->locals[i], oprintf);
2424 if (f->blocks_count)
2426 oprintf("%slife passes (check): %i\n", ind, (int)f->run_id);
2427 for (i = 0; i < f->blocks_count; ++i) {
2428 if (f->blocks[i]->run_id != f->run_id) {
2429 oprintf("%slife pass check fail! %i != %i\n", ind, (int)f->blocks[i]->run_id, (int)f->run_id);
2431 ir_block_dump(f->blocks[i], ind, oprintf);
2435 ind[strlen(ind)-1] = 0;
2436 oprintf("%sendfunction %s\n", ind, f->name);
2439 void ir_block_dump(ir_block* b, char *ind,
2440 int (*oprintf)(const char*, ...))
2443 oprintf("%s:%s\n", ind, b->label);
2444 strncat(ind, "\t", IND_BUFSZ);
2446 for (i = 0; i < b->instr_count; ++i)
2447 ir_instr_dump(b->instr[i], ind, oprintf);
2448 ind[strlen(ind)-1] = 0;
2451 void dump_phi(ir_instr *in, char *ind,
2452 int (*oprintf)(const char*, ...))
2455 oprintf("%s <- phi ", in->_ops[0]->name);
2456 for (i = 0; i < in->phi_count; ++i)
2458 oprintf("([%s] : %s) ", in->phi[i].from->label,
2459 in->phi[i].value->name);
2464 void ir_instr_dump(ir_instr *in, char *ind,
2465 int (*oprintf)(const char*, ...))
2468 const char *comma = NULL;
2470 oprintf("%s (%i) ", ind, (int)in->eid);
2472 if (in->opcode == VINSTR_PHI) {
2473 dump_phi(in, ind, oprintf);
2477 strncat(ind, "\t", IND_BUFSZ);
2479 if (in->_ops[0] && (in->_ops[1] || in->_ops[2])) {
2480 ir_value_dump(in->_ops[0], oprintf);
2481 if (in->_ops[1] || in->_ops[2])
2484 oprintf("%s\t", qc_opname(in->opcode));
2485 if (in->_ops[0] && !(in->_ops[1] || in->_ops[2])) {
2486 ir_value_dump(in->_ops[0], oprintf);
2491 for (i = 1; i != 3; ++i) {
2495 ir_value_dump(in->_ops[i], oprintf);
2503 oprintf("[%s]", in->bops[0]->label);
2507 oprintf("%s[%s]", comma, in->bops[1]->label);
2509 ind[strlen(ind)-1] = 0;
2512 void ir_value_dump(ir_value* v, int (*oprintf)(const char*, ...))
2520 oprintf("%g", v->constval.vfloat);
2523 oprintf("'%g %g %g'",
2526 v->constval.vvec.z);
2529 oprintf("(entity)");
2532 oprintf("\"%s\"", v->constval.vstring);
2536 oprintf("%i", v->constval.vint);
2541 v->constval.vpointer->name);
2545 oprintf("%s", v->name);
2549 void ir_value_dump_life(ir_value *self, int (*oprintf)(const char*,...))
2552 oprintf("Life of %s:\n", self->name);
2553 for (i = 0; i < self->life_count; ++i)
2555 oprintf(" + [%i, %i]\n", self->life[i].start, self->life[i].end);