5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is furnished to do
10 * so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 /***********************************************************************
29 * Type sizes used at multiple points in the IR codegen
32 const char *type_name[TYPE_COUNT] = {
48 size_t type_sizeof[TYPE_COUNT] = {
55 1, /* TYPE_FUNCTION */
64 uint16_t type_store_instr[TYPE_COUNT] = {
65 INSTR_STORE_F, /* should use I when having integer support */
72 INSTR_STORE_ENT, /* should use I */
74 INSTR_STORE_I, /* integer type */
79 INSTR_STORE_V, /* variant, should never be accessed */
81 AINSTR_END, /* struct */
82 AINSTR_END, /* union */
83 AINSTR_END, /* array */
86 uint16_t field_store_instr[TYPE_COUNT] = {
96 INSTR_STORE_FLD, /* integer type */
101 INSTR_STORE_V, /* variant, should never be accessed */
103 AINSTR_END, /* struct */
104 AINSTR_END, /* union */
105 AINSTR_END, /* array */
108 uint16_t type_storep_instr[TYPE_COUNT] = {
109 INSTR_STOREP_F, /* should use I when having integer support */
116 INSTR_STOREP_ENT, /* should use I */
118 INSTR_STOREP_ENT, /* integer type */
123 INSTR_STOREP_V, /* variant, should never be accessed */
125 AINSTR_END, /* struct */
126 AINSTR_END, /* union */
127 AINSTR_END, /* array */
130 uint16_t type_eq_instr[TYPE_COUNT] = {
131 INSTR_EQ_F, /* should use I when having integer support */
136 INSTR_EQ_E, /* FLD has no comparison */
138 INSTR_EQ_E, /* should use I */
145 INSTR_EQ_V, /* variant, should never be accessed */
147 AINSTR_END, /* struct */
148 AINSTR_END, /* union */
149 AINSTR_END, /* array */
152 uint16_t type_ne_instr[TYPE_COUNT] = {
153 INSTR_NE_F, /* should use I when having integer support */
158 INSTR_NE_E, /* FLD has no comparison */
160 INSTR_NE_E, /* should use I */
167 INSTR_NE_V, /* variant, should never be accessed */
169 AINSTR_END, /* struct */
170 AINSTR_END, /* union */
171 AINSTR_END, /* array */
174 uint16_t type_not_instr[TYPE_COUNT] = {
175 INSTR_NOT_F, /* should use I when having integer support */
182 INSTR_NOT_ENT, /* should use I */
184 INSTR_NOT_I, /* integer type */
189 INSTR_NOT_V, /* variant, should never be accessed */
191 AINSTR_END, /* struct */
192 AINSTR_END, /* union */
193 AINSTR_END, /* array */
197 static void ir_gen_extparam(ir_builder *ir);
199 /* error functions */
201 static void irerror(lex_ctx ctx, const char *msg, ...)
205 con_cvprintmsg((void*)&ctx, LVL_ERROR, "internal error", msg, ap);
209 static bool irwarning(lex_ctx ctx, int warntype, const char *fmt, ...)
212 int lvl = LVL_WARNING;
214 if (warntype && !OPTS_WARN(warntype))
221 con_vprintmsg(lvl, ctx.file, ctx.line, (opts.werror ? "error" : "warning"), fmt, ap);
227 /***********************************************************************
228 * Vector utility functions
231 bool GMQCC_WARN vec_ir_value_find(ir_value **vec, ir_value *what, size_t *idx)
234 size_t len = vec_size(vec);
235 for (i = 0; i < len; ++i) {
236 if (vec[i] == what) {
244 bool GMQCC_WARN vec_ir_block_find(ir_block **vec, ir_block *what, size_t *idx)
247 size_t len = vec_size(vec);
248 for (i = 0; i < len; ++i) {
249 if (vec[i] == what) {
257 bool GMQCC_WARN vec_ir_instr_find(ir_instr **vec, ir_instr *what, size_t *idx)
260 size_t len = vec_size(vec);
261 for (i = 0; i < len; ++i) {
262 if (vec[i] == what) {
270 /***********************************************************************
274 static void ir_block_delete_quick(ir_block* self);
275 static void ir_instr_delete_quick(ir_instr *self);
276 static void ir_function_delete_quick(ir_function *self);
278 ir_builder* ir_builder_new(const char *modulename)
282 self = (ir_builder*)mem_a(sizeof(*self));
286 self->functions = NULL;
287 self->globals = NULL;
289 self->extparams = NULL;
290 self->filenames = NULL;
291 self->filestrings = NULL;
292 self->htglobals = util_htnew(IR_HT_SIZE);
293 self->htfields = util_htnew(IR_HT_SIZE);
294 self->htfunctions = util_htnew(IR_HT_SIZE);
296 self->str_immediate = 0;
298 if (!ir_builder_set_name(self, modulename)) {
306 void ir_builder_delete(ir_builder* self)
309 util_htdel(self->htglobals);
310 util_htdel(self->htfields);
311 util_htdel(self->htfunctions);
312 mem_d((void*)self->name);
313 for (i = 0; i != vec_size(self->functions); ++i) {
314 ir_function_delete_quick(self->functions[i]);
316 vec_free(self->functions);
317 for (i = 0; i != vec_size(self->extparams); ++i) {
318 ir_value_delete(self->extparams[i]);
320 vec_free(self->extparams);
321 for (i = 0; i != vec_size(self->globals); ++i) {
322 ir_value_delete(self->globals[i]);
324 vec_free(self->globals);
325 for (i = 0; i != vec_size(self->fields); ++i) {
326 ir_value_delete(self->fields[i]);
328 vec_free(self->fields);
329 vec_free(self->filenames);
330 vec_free(self->filestrings);
334 bool ir_builder_set_name(ir_builder *self, const char *name)
337 mem_d((void*)self->name);
338 self->name = util_strdup(name);
342 ir_function* ir_builder_get_function(ir_builder *self, const char *name)
344 return (ir_function*)util_htget(self->htfunctions, name);
347 ir_function* ir_builder_create_function(ir_builder *self, const char *name, int outtype)
349 ir_function *fn = ir_builder_get_function(self, name);
354 fn = ir_function_new(self, outtype);
355 if (!ir_function_set_name(fn, name))
357 ir_function_delete(fn);
360 vec_push(self->functions, fn);
361 util_htset(self->htfunctions, name, fn);
363 fn->value = ir_builder_create_global(self, fn->name, TYPE_FUNCTION);
365 ir_function_delete(fn);
369 fn->value->hasvalue = true;
370 fn->value->outtype = outtype;
371 fn->value->constval.vfunc = fn;
372 fn->value->context = fn->context;
377 ir_value* ir_builder_get_global(ir_builder *self, const char *name)
379 return (ir_value*)util_htget(self->htglobals, name);
382 ir_value* ir_builder_create_global(ir_builder *self, const char *name, int vtype)
386 if (name && name[0] != '#')
388 ve = ir_builder_get_global(self, name);
394 ve = ir_value_var(name, store_global, vtype);
395 vec_push(self->globals, ve);
396 util_htset(self->htglobals, name, ve);
400 ir_value* ir_builder_get_field(ir_builder *self, const char *name)
402 return (ir_value*)util_htget(self->htfields, name);
406 ir_value* ir_builder_create_field(ir_builder *self, const char *name, int vtype)
408 ir_value *ve = ir_builder_get_field(self, name);
413 ve = ir_value_var(name, store_global, TYPE_FIELD);
414 ve->fieldtype = vtype;
415 vec_push(self->fields, ve);
416 util_htset(self->htfields, name, ve);
420 /***********************************************************************
424 bool ir_function_naive_phi(ir_function*);
425 void ir_function_enumerate(ir_function*);
426 bool ir_function_calculate_liferanges(ir_function*);
427 bool ir_function_allocate_locals(ir_function*);
429 ir_function* ir_function_new(ir_builder* owner, int outtype)
432 self = (ir_function*)mem_a(sizeof(*self));
437 memset(self, 0, sizeof(*self));
440 if (!ir_function_set_name(self, "<@unnamed>")) {
445 self->context.file = "<@no context>";
446 self->context.line = 0;
447 self->outtype = outtype;
456 self->code_function_def = -1;
457 self->allocated_locals = 0;
463 bool ir_function_set_name(ir_function *self, const char *name)
466 mem_d((void*)self->name);
467 self->name = util_strdup(name);
471 static void ir_function_delete_quick(ir_function *self)
474 mem_d((void*)self->name);
476 for (i = 0; i != vec_size(self->blocks); ++i)
477 ir_block_delete_quick(self->blocks[i]);
478 vec_free(self->blocks);
480 vec_free(self->params);
482 for (i = 0; i != vec_size(self->values); ++i)
483 ir_value_delete(self->values[i]);
484 vec_free(self->values);
486 for (i = 0; i != vec_size(self->locals); ++i)
487 ir_value_delete(self->locals[i]);
488 vec_free(self->locals);
490 /* self->value is deleted by the builder */
495 void ir_function_delete(ir_function *self)
498 mem_d((void*)self->name);
500 for (i = 0; i != vec_size(self->blocks); ++i)
501 ir_block_delete(self->blocks[i]);
502 vec_free(self->blocks);
504 vec_free(self->params);
506 for (i = 0; i != vec_size(self->values); ++i)
507 ir_value_delete(self->values[i]);
508 vec_free(self->values);
510 for (i = 0; i != vec_size(self->locals); ++i)
511 ir_value_delete(self->locals[i]);
512 vec_free(self->locals);
514 /* self->value is deleted by the builder */
519 void ir_function_collect_value(ir_function *self, ir_value *v)
521 vec_push(self->values, v);
524 ir_block* ir_function_create_block(lex_ctx ctx, ir_function *self, const char *label)
526 ir_block* bn = ir_block_new(self, label);
528 vec_push(self->blocks, bn);
532 static bool instr_is_operation(uint16_t op)
534 return ( (op >= INSTR_MUL_F && op <= INSTR_GT) ||
535 (op >= INSTR_LOAD_F && op <= INSTR_LOAD_FNC) ||
536 (op == INSTR_ADDRESS) ||
537 (op >= INSTR_NOT_F && op <= INSTR_NOT_FNC) ||
538 (op >= INSTR_AND && op <= INSTR_BITOR) );
541 bool ir_function_pass_peephole(ir_function *self)
545 for (b = 0; b < vec_size(self->blocks); ++b) {
547 ir_block *block = self->blocks[b];
549 for (i = 0; i < vec_size(block->instr); ++i) {
551 inst = block->instr[i];
554 (inst->opcode >= INSTR_STORE_F &&
555 inst->opcode <= INSTR_STORE_FNC))
563 oper = block->instr[i-1];
564 if (!instr_is_operation(oper->opcode))
567 value = oper->_ops[0];
569 /* only do it for SSA values */
570 if (value->store != store_value)
573 /* don't optimize out the temp if it's used later again */
574 if (vec_size(value->reads) != 1)
577 /* The very next store must use this value */
578 if (value->reads[0] != store)
581 /* And of course the store must _read_ from it, so it's in
583 if (store->_ops[1] != value)
586 ++optimization_count[OPTIM_PEEPHOLE];
587 (void)!ir_instr_op(oper, 0, store->_ops[0], true);
589 vec_remove(block->instr, i, 1);
590 ir_instr_delete(store);
592 else if (inst->opcode == VINSTR_COND)
594 /* COND on a value resulting from a NOT could
595 * remove the NOT and swap its operands
602 value = inst->_ops[0];
604 if (value->store != store_value ||
605 vec_size(value->reads) != 1 ||
606 value->reads[0] != inst)
611 inot = value->writes[0];
612 if (inot->_ops[0] != value ||
613 inot->opcode < INSTR_NOT_F ||
614 inot->opcode > INSTR_NOT_FNC ||
615 inot->opcode == INSTR_NOT_V) /* can't do this one */
621 ++optimization_count[OPTIM_PEEPHOLE];
623 (void)!ir_instr_op(inst, 0, inot->_ops[1], false);
626 for (inotid = 0; inotid < vec_size(tmp->instr); ++inotid) {
627 if (tmp->instr[inotid] == inot)
630 if (inotid >= vec_size(tmp->instr)) {
631 compile_error(inst->context, "sanity-check failed: failed to find instruction to optimize out");
634 vec_remove(tmp->instr, inotid, 1);
635 ir_instr_delete(inot);
636 /* swap ontrue/onfalse */
638 inst->bops[0] = inst->bops[1];
649 bool ir_function_pass_tailcall(ir_function *self)
653 for (b = 0; b < vec_size(self->blocks); ++b) {
655 ir_instr *ret, *call, *store = NULL;
656 ir_block *block = self->blocks[b];
658 if (!block->final || vec_size(block->instr) < 2)
661 ret = block->instr[vec_size(block->instr)-1];
662 if (ret->opcode != INSTR_DONE && ret->opcode != INSTR_RETURN)
665 call = block->instr[vec_size(block->instr)-2];
666 if (call->opcode >= INSTR_STORE_F && call->opcode <= INSTR_STORE_FNC) {
667 /* account for the unoptimized
669 * STORE %return, %tmp
673 if (vec_size(block->instr) < 3)
677 call = block->instr[vec_size(block->instr)-3];
680 if (call->opcode < INSTR_CALL0 || call->opcode > INSTR_CALL8)
684 /* optimize out the STORE */
686 ret->_ops[0] == store->_ops[0] &&
687 store->_ops[1] == call->_ops[0])
689 ++optimization_count[OPTIM_PEEPHOLE];
690 call->_ops[0] = store->_ops[0];
691 vec_remove(block->instr, vec_size(block->instr) - 2, 1);
692 ir_instr_delete(store);
701 funcval = call->_ops[1];
704 if (funcval->vtype != TYPE_FUNCTION || funcval->constval.vfunc != self)
707 /* now we have a CALL and a RET, check if it's a tailcall */
708 if (ret->_ops[0] && call->_ops[0] != ret->_ops[0])
711 ++optimization_count[OPTIM_TAIL_RECURSION];
712 vec_shrinkby(block->instr, 2);
714 block->final = false; /* open it back up */
716 /* emite parameter-stores */
717 for (p = 0; p < vec_size(call->params); ++p) {
718 /* assert(call->params_count <= self->locals_count); */
719 if (!ir_block_create_store(block, call->context, self->locals[p], call->params[p])) {
720 irerror(call->context, "failed to create tailcall store instruction for parameter %i", (int)p);
724 if (!ir_block_create_jump(block, call->context, self->blocks[0])) {
725 irerror(call->context, "failed to create tailcall jump");
729 ir_instr_delete(call);
730 ir_instr_delete(ret);
736 bool ir_function_finalize(ir_function *self)
741 if (OPTS_OPTIMIZATION(OPTIM_PEEPHOLE)) {
742 if (!ir_function_pass_peephole(self)) {
743 irerror(self->context, "generic optimization pass broke something in `%s`", self->name);
748 if (OPTS_OPTIMIZATION(OPTIM_TAIL_RECURSION)) {
749 if (!ir_function_pass_tailcall(self)) {
750 irerror(self->context, "tailcall optimization pass broke something in `%s`", self->name);
755 if (!ir_function_naive_phi(self))
758 ir_function_enumerate(self);
760 if (!ir_function_calculate_liferanges(self))
762 if (!ir_function_allocate_locals(self))
767 ir_value* ir_function_create_local(ir_function *self, const char *name, int vtype, bool param)
772 vec_size(self->locals) &&
773 self->locals[vec_size(self->locals)-1]->store != store_param) {
774 irerror(self->context, "cannot add parameters after adding locals");
778 ve = ir_value_var(name, (param ? store_param : store_local), vtype);
779 vec_push(self->locals, ve);
783 /***********************************************************************
787 ir_block* ir_block_new(ir_function* owner, const char *name)
790 self = (ir_block*)mem_a(sizeof(*self));
794 memset(self, 0, sizeof(*self));
797 if (name && !ir_block_set_label(self, name)) {
802 self->context.file = "<@no context>";
803 self->context.line = 0;
807 self->entries = NULL;
811 self->is_return = false;
816 self->generated = false;
821 static void ir_block_delete_quick(ir_block* self)
824 if (self->label) mem_d(self->label);
825 for (i = 0; i != vec_size(self->instr); ++i)
826 ir_instr_delete_quick(self->instr[i]);
827 vec_free(self->instr);
828 vec_free(self->entries);
829 vec_free(self->exits);
830 vec_free(self->living);
834 void ir_block_delete(ir_block* self)
837 if (self->label) mem_d(self->label);
838 for (i = 0; i != vec_size(self->instr); ++i)
839 ir_instr_delete(self->instr[i]);
840 vec_free(self->instr);
841 vec_free(self->entries);
842 vec_free(self->exits);
843 vec_free(self->living);
847 bool ir_block_set_label(ir_block *self, const char *name)
850 mem_d((void*)self->label);
851 self->label = util_strdup(name);
852 return !!self->label;
855 /***********************************************************************
859 ir_instr* ir_instr_new(lex_ctx ctx, ir_block* owner, int op)
862 self = (ir_instr*)mem_a(sizeof(*self));
869 self->_ops[0] = NULL;
870 self->_ops[1] = NULL;
871 self->_ops[2] = NULL;
872 self->bops[0] = NULL;
873 self->bops[1] = NULL;
884 static void ir_instr_delete_quick(ir_instr *self)
887 vec_free(self->params);
891 void ir_instr_delete(ir_instr *self)
894 /* The following calls can only delete from
895 * vectors, we still want to delete this instruction
896 * so ignore the return value. Since with the warn_unused_result attribute
897 * gcc doesn't care about an explicit: (void)foo(); to ignore the result,
898 * I have to improvise here and use if(foo());
900 for (i = 0; i < vec_size(self->phi); ++i) {
902 if (vec_ir_instr_find(self->phi[i].value->writes, self, &idx))
903 vec_remove(self->phi[i].value->writes, idx, 1);
904 if (vec_ir_instr_find(self->phi[i].value->reads, self, &idx))
905 vec_remove(self->phi[i].value->reads, idx, 1);
908 for (i = 0; i < vec_size(self->params); ++i) {
910 if (vec_ir_instr_find(self->params[i]->writes, self, &idx))
911 vec_remove(self->params[i]->writes, idx, 1);
912 if (vec_ir_instr_find(self->params[i]->reads, self, &idx))
913 vec_remove(self->params[i]->reads, idx, 1);
915 vec_free(self->params);
916 (void)!ir_instr_op(self, 0, NULL, false);
917 (void)!ir_instr_op(self, 1, NULL, false);
918 (void)!ir_instr_op(self, 2, NULL, false);
922 bool ir_instr_op(ir_instr *self, int op, ir_value *v, bool writing)
924 if (self->_ops[op]) {
926 if (writing && vec_ir_instr_find(self->_ops[op]->writes, self, &idx))
927 vec_remove(self->_ops[op]->writes, idx, 1);
928 else if (vec_ir_instr_find(self->_ops[op]->reads, self, &idx))
929 vec_remove(self->_ops[op]->reads, idx, 1);
933 vec_push(v->writes, self);
935 vec_push(v->reads, self);
941 /***********************************************************************
945 void ir_value_code_setaddr(ir_value *self, int32_t gaddr)
947 self->code.globaladdr = gaddr;
948 if (self->members[0]) self->members[0]->code.globaladdr = gaddr;
949 if (self->members[1]) self->members[1]->code.globaladdr = gaddr;
950 if (self->members[2]) self->members[2]->code.globaladdr = gaddr;
953 int32_t ir_value_code_addr(const ir_value *self)
955 if (self->store == store_return)
956 return OFS_RETURN + self->code.addroffset;
957 return self->code.globaladdr + self->code.addroffset;
960 ir_value* ir_value_var(const char *name, int storetype, int vtype)
963 self = (ir_value*)mem_a(sizeof(*self));
965 self->fieldtype = TYPE_VOID;
966 self->outtype = TYPE_VOID;
967 self->store = storetype;
973 self->hasvalue = false;
974 self->context.file = "<@no context>";
975 self->context.line = 0;
977 if (name && !ir_value_set_name(self, name)) {
978 irerror(self->context, "out of memory");
983 memset(&self->constval, 0, sizeof(self->constval));
984 memset(&self->code, 0, sizeof(self->code));
986 self->members[0] = NULL;
987 self->members[1] = NULL;
988 self->members[2] = NULL;
989 self->memberof = NULL;
991 self->unique_life = false;
997 ir_value* ir_value_vector_member(ir_value *self, unsigned int member)
1003 if (self->members[member])
1004 return self->members[member];
1006 if (self->vtype == TYPE_VECTOR)
1008 m = ir_value_var(self->name, self->store, TYPE_FLOAT);
1011 m->context = self->context;
1013 self->members[member] = m;
1014 m->code.addroffset = member;
1016 else if (self->vtype == TYPE_FIELD)
1018 if (self->fieldtype != TYPE_VECTOR)
1020 m = ir_value_var(self->name, self->store, TYPE_FIELD);
1023 m->fieldtype = TYPE_FLOAT;
1024 m->context = self->context;
1026 self->members[member] = m;
1027 m->code.addroffset = member;
1031 irerror(self->context, "invalid member access on %s", self->name);
1039 ir_value* ir_value_out(ir_function *owner, const char *name, int storetype, int vtype)
1041 ir_value *v = ir_value_var(name, storetype, vtype);
1044 ir_function_collect_value(owner, v);
1048 void ir_value_delete(ir_value* self)
1052 mem_d((void*)self->name);
1055 if (self->vtype == TYPE_STRING)
1056 mem_d((void*)self->constval.vstring);
1058 for (i = 0; i < 3; ++i) {
1059 if (self->members[i])
1060 ir_value_delete(self->members[i]);
1062 vec_free(self->reads);
1063 vec_free(self->writes);
1064 vec_free(self->life);
1068 bool ir_value_set_name(ir_value *self, const char *name)
1071 mem_d((void*)self->name);
1072 self->name = util_strdup(name);
1073 return !!self->name;
1076 bool ir_value_set_float(ir_value *self, float f)
1078 if (self->vtype != TYPE_FLOAT)
1080 self->constval.vfloat = f;
1081 self->hasvalue = true;
1085 bool ir_value_set_func(ir_value *self, int f)
1087 if (self->vtype != TYPE_FUNCTION)
1089 self->constval.vint = f;
1090 self->hasvalue = true;
1094 bool ir_value_set_vector(ir_value *self, vector v)
1096 if (self->vtype != TYPE_VECTOR)
1098 self->constval.vvec = v;
1099 self->hasvalue = true;
1103 bool ir_value_set_field(ir_value *self, ir_value *fld)
1105 if (self->vtype != TYPE_FIELD)
1107 self->constval.vpointer = fld;
1108 self->hasvalue = true;
1112 static char *ir_strdup(const char *str)
1115 /* actually dup empty strings */
1116 char *out = mem_a(1);
1120 return util_strdup(str);
1123 bool ir_value_set_string(ir_value *self, const char *str)
1125 if (self->vtype != TYPE_STRING)
1127 self->constval.vstring = ir_strdup(str);
1128 self->hasvalue = true;
1133 bool ir_value_set_int(ir_value *self, int i)
1135 if (self->vtype != TYPE_INTEGER)
1137 self->constval.vint = i;
1138 self->hasvalue = true;
1143 bool ir_value_lives(ir_value *self, size_t at)
1146 for (i = 0; i < vec_size(self->life); ++i)
1148 ir_life_entry_t *life = &self->life[i];
1149 if (life->start <= at && at <= life->end)
1151 if (life->start > at) /* since it's ordered */
1157 bool ir_value_life_insert(ir_value *self, size_t idx, ir_life_entry_t e)
1160 vec_push(self->life, e);
1161 for (k = vec_size(self->life)-1; k > idx; --k)
1162 self->life[k] = self->life[k-1];
1163 self->life[idx] = e;
1167 bool ir_value_life_merge(ir_value *self, size_t s)
1170 ir_life_entry_t *life = NULL;
1171 ir_life_entry_t *before = NULL;
1172 ir_life_entry_t new_entry;
1174 /* Find the first range >= s */
1175 for (i = 0; i < vec_size(self->life); ++i)
1178 life = &self->life[i];
1179 if (life->start > s)
1182 /* nothing found? append */
1183 if (i == vec_size(self->life)) {
1185 if (life && life->end+1 == s)
1187 /* previous life range can be merged in */
1191 if (life && life->end >= s)
1193 e.start = e.end = s;
1194 vec_push(self->life, e);
1200 if (before->end + 1 == s &&
1201 life->start - 1 == s)
1204 before->end = life->end;
1205 vec_remove(self->life, i, 1);
1208 if (before->end + 1 == s)
1214 /* already contained */
1215 if (before->end >= s)
1219 if (life->start - 1 == s)
1224 /* insert a new entry */
1225 new_entry.start = new_entry.end = s;
1226 return ir_value_life_insert(self, i, new_entry);
1229 bool ir_value_life_merge_into(ir_value *self, const ir_value *other)
1233 if (!vec_size(other->life))
1236 if (!vec_size(self->life)) {
1237 size_t count = vec_size(other->life);
1238 ir_life_entry_t *life = vec_add(self->life, count);
1239 memcpy(life, other->life, count * sizeof(*life));
1244 for (i = 0; i < vec_size(other->life); ++i)
1246 const ir_life_entry_t *life = &other->life[i];
1249 ir_life_entry_t *entry = &self->life[myi];
1251 if (life->end+1 < entry->start)
1253 /* adding an interval before entry */
1254 if (!ir_value_life_insert(self, myi, *life))
1260 if (life->start < entry->start &&
1261 life->end+1 >= entry->start)
1263 /* starts earlier and overlaps */
1264 entry->start = life->start;
1267 if (life->end > entry->end &&
1268 life->start <= entry->end+1)
1270 /* ends later and overlaps */
1271 entry->end = life->end;
1274 /* see if our change combines it with the next ranges */
1275 while (myi+1 < vec_size(self->life) &&
1276 entry->end+1 >= self->life[1+myi].start)
1278 /* overlaps with (myi+1) */
1279 if (entry->end < self->life[1+myi].end)
1280 entry->end = self->life[1+myi].end;
1281 vec_remove(self->life, myi+1, 1);
1282 entry = &self->life[myi];
1285 /* see if we're after the entry */
1286 if (life->start > entry->end)
1289 /* append if we're at the end */
1290 if (myi >= vec_size(self->life)) {
1291 vec_push(self->life, *life);
1294 /* otherweise check the next range */
1303 bool ir_values_overlap(const ir_value *a, const ir_value *b)
1305 /* For any life entry in A see if it overlaps with
1306 * any life entry in B.
1307 * Note that the life entries are orderes, so we can make a
1308 * more efficient algorithm there than naively translating the
1312 ir_life_entry_t *la, *lb, *enda, *endb;
1314 /* first of all, if either has no life range, they cannot clash */
1315 if (!vec_size(a->life) || !vec_size(b->life))
1320 enda = la + vec_size(a->life);
1321 endb = lb + vec_size(b->life);
1324 /* check if the entries overlap, for that,
1325 * both must start before the other one ends.
1327 if (la->start < lb->end &&
1328 lb->start < la->end)
1333 /* entries are ordered
1334 * one entry is earlier than the other
1335 * that earlier entry will be moved forward
1337 if (la->start < lb->start)
1339 /* order: A B, move A forward
1340 * check if we hit the end with A
1345 else /* if (lb->start < la->start) actually <= */
1347 /* order: B A, move B forward
1348 * check if we hit the end with B
1357 /***********************************************************************
1361 bool ir_block_create_store_op(ir_block *self, lex_ctx ctx, int op, ir_value *target, ir_value *what)
1365 irerror(self->context, "unreachable statement (%s)", self->label);
1368 in = ir_instr_new(ctx, self, op);
1372 if (target->store == store_value &&
1373 (op < INSTR_STOREP_F || op > INSTR_STOREP_FNC))
1375 irerror(self->context, "cannot store to an SSA value");
1376 irerror(self->context, "trying to store: %s <- %s", target->name, what->name);
1377 irerror(self->context, "instruction: %s", asm_instr[op].m);
1381 if (!ir_instr_op(in, 0, target, true) ||
1382 !ir_instr_op(in, 1, what, false))
1386 vec_push(self->instr, in);
1390 bool ir_block_create_store(ir_block *self, lex_ctx ctx, ir_value *target, ir_value *what)
1394 if (target->vtype == TYPE_VARIANT)
1395 vtype = what->vtype;
1397 vtype = target->vtype;
1400 if (vtype == TYPE_FLOAT && what->vtype == TYPE_INTEGER)
1401 op = INSTR_CONV_ITOF;
1402 else if (vtype == TYPE_INTEGER && what->vtype == TYPE_FLOAT)
1403 op = INSTR_CONV_FTOI;
1405 op = type_store_instr[vtype];
1407 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS)) {
1408 if (op == INSTR_STORE_FLD && what->fieldtype == TYPE_VECTOR)
1412 return ir_block_create_store_op(self, ctx, op, target, what);
1415 bool ir_block_create_storep(ir_block *self, lex_ctx ctx, ir_value *target, ir_value *what)
1420 if (target->vtype != TYPE_POINTER)
1423 /* storing using pointer - target is a pointer, type must be
1424 * inferred from source
1426 vtype = what->vtype;
1428 op = type_storep_instr[vtype];
1429 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS)) {
1430 if (op == INSTR_STOREP_FLD && what->fieldtype == TYPE_VECTOR)
1431 op = INSTR_STOREP_V;
1434 return ir_block_create_store_op(self, ctx, op, target, what);
1437 bool ir_block_create_return(ir_block *self, lex_ctx ctx, ir_value *v)
1441 irerror(self->context, "unreachable statement (%s)", self->label);
1445 self->is_return = true;
1446 in = ir_instr_new(ctx, self, INSTR_RETURN);
1450 if (v && !ir_instr_op(in, 0, v, false))
1453 vec_push(self->instr, in);
1457 bool ir_block_create_if(ir_block *self, lex_ctx ctx, ir_value *v,
1458 ir_block *ontrue, ir_block *onfalse)
1462 irerror(self->context, "unreachable statement (%s)", self->label);
1466 /*in = ir_instr_new(ctx, self, (v->vtype == TYPE_STRING ? INSTR_IF_S : INSTR_IF_F));*/
1467 in = ir_instr_new(ctx, self, VINSTR_COND);
1471 if (!ir_instr_op(in, 0, v, false)) {
1472 ir_instr_delete(in);
1476 in->bops[0] = ontrue;
1477 in->bops[1] = onfalse;
1479 vec_push(self->instr, in);
1481 vec_push(self->exits, ontrue);
1482 vec_push(self->exits, onfalse);
1483 vec_push(ontrue->entries, self);
1484 vec_push(onfalse->entries, self);
1488 bool ir_block_create_jump(ir_block *self, lex_ctx ctx, ir_block *to)
1492 irerror(self->context, "unreachable statement (%s)", self->label);
1496 in = ir_instr_new(ctx, self, VINSTR_JUMP);
1501 vec_push(self->instr, in);
1503 vec_push(self->exits, to);
1504 vec_push(to->entries, self);
1508 bool ir_block_create_goto(ir_block *self, lex_ctx ctx, ir_block *to)
1512 irerror(self->context, "unreachable statement (%s)", self->label);
1516 in = ir_instr_new(ctx, self, INSTR_GOTO);
1521 vec_push(self->instr, in);
1523 vec_push(self->exits, to);
1524 vec_push(to->entries, self);
1528 ir_instr* ir_block_create_phi(ir_block *self, lex_ctx ctx, const char *label, int ot)
1532 in = ir_instr_new(ctx, self, VINSTR_PHI);
1535 out = ir_value_out(self->owner, label, store_value, ot);
1537 ir_instr_delete(in);
1540 if (!ir_instr_op(in, 0, out, true)) {
1541 ir_instr_delete(in);
1542 ir_value_delete(out);
1545 vec_push(self->instr, in);
1549 ir_value* ir_phi_value(ir_instr *self)
1551 return self->_ops[0];
1554 void ir_phi_add(ir_instr* self, ir_block *b, ir_value *v)
1558 if (!vec_ir_block_find(self->owner->entries, b, NULL)) {
1559 /* Must not be possible to cause this, otherwise the AST
1560 * is doing something wrong.
1562 irerror(self->context, "Invalid entry block for PHI");
1568 vec_push(v->reads, self);
1569 vec_push(self->phi, pe);
1572 /* call related code */
1573 ir_instr* ir_block_create_call(ir_block *self, lex_ctx ctx, const char *label, ir_value *func)
1577 in = ir_instr_new(ctx, self, INSTR_CALL0);
1580 out = ir_value_out(self->owner, label, (func->outtype == TYPE_VOID) ? store_return : store_value, func->outtype);
1582 ir_instr_delete(in);
1585 if (!ir_instr_op(in, 0, out, true) ||
1586 !ir_instr_op(in, 1, func, false))
1588 ir_instr_delete(in);
1589 ir_value_delete(out);
1592 vec_push(self->instr, in);
1596 ir_value* ir_call_value(ir_instr *self)
1598 return self->_ops[0];
1601 void ir_call_param(ir_instr* self, ir_value *v)
1603 vec_push(self->params, v);
1604 vec_push(v->reads, self);
1607 /* binary op related code */
1609 ir_value* ir_block_create_binop(ir_block *self, lex_ctx ctx,
1610 const char *label, int opcode,
1611 ir_value *left, ir_value *right)
1633 case INSTR_SUB_S: /* -- offset of string as float */
1638 case INSTR_BITOR_IF:
1639 case INSTR_BITOR_FI:
1640 case INSTR_BITAND_FI:
1641 case INSTR_BITAND_IF:
1656 case INSTR_BITAND_I:
1659 case INSTR_RSHIFT_I:
1660 case INSTR_LSHIFT_I:
1682 /* boolean operations result in floats */
1683 if (opcode >= INSTR_EQ_F && opcode <= INSTR_GT)
1685 else if (opcode >= INSTR_LE && opcode <= INSTR_GT)
1688 else if (opcode >= INSTR_LE_I && opcode <= INSTR_EQ_FI)
1693 if (ot == TYPE_VOID) {
1694 /* The AST or parser were supposed to check this! */
1698 return ir_block_create_general_instr(self, ctx, label, opcode, left, right, ot);
1701 ir_value* ir_block_create_unary(ir_block *self, lex_ctx ctx,
1702 const char *label, int opcode,
1705 int ot = TYPE_FLOAT;
1717 /* QC doesn't have other unary operations. We expect extensions to fill
1718 * the above list, otherwise we assume out-type = in-type, eg for an
1722 ot = operand->vtype;
1725 if (ot == TYPE_VOID) {
1726 /* The AST or parser were supposed to check this! */
1730 /* let's use the general instruction creator and pass NULL for OPB */
1731 return ir_block_create_general_instr(self, ctx, label, opcode, operand, NULL, ot);
1734 ir_value* ir_block_create_general_instr(ir_block *self, lex_ctx ctx, const char *label,
1735 int op, ir_value *a, ir_value *b, int outype)
1740 out = ir_value_out(self->owner, label, store_value, outype);
1744 instr = ir_instr_new(ctx, self, op);
1746 ir_value_delete(out);
1750 if (!ir_instr_op(instr, 0, out, true) ||
1751 !ir_instr_op(instr, 1, a, false) ||
1752 !ir_instr_op(instr, 2, b, false) )
1757 vec_push(self->instr, instr);
1761 ir_instr_delete(instr);
1762 ir_value_delete(out);
1766 ir_value* ir_block_create_fieldaddress(ir_block *self, lex_ctx ctx, const char *label, ir_value *ent, ir_value *field)
1770 /* Support for various pointer types todo if so desired */
1771 if (ent->vtype != TYPE_ENTITY)
1774 if (field->vtype != TYPE_FIELD)
1777 v = ir_block_create_general_instr(self, ctx, label, INSTR_ADDRESS, ent, field, TYPE_POINTER);
1778 v->fieldtype = field->fieldtype;
1782 ir_value* ir_block_create_load_from_ent(ir_block *self, lex_ctx ctx, const char *label, ir_value *ent, ir_value *field, int outype)
1785 if (ent->vtype != TYPE_ENTITY)
1788 /* at some point we could redirect for TYPE_POINTER... but that could lead to carelessness */
1789 if (field->vtype != TYPE_FIELD)
1794 case TYPE_FLOAT: op = INSTR_LOAD_F; break;
1795 case TYPE_VECTOR: op = INSTR_LOAD_V; break;
1796 case TYPE_STRING: op = INSTR_LOAD_S; break;
1797 case TYPE_FIELD: op = INSTR_LOAD_FLD; break;
1798 case TYPE_ENTITY: op = INSTR_LOAD_ENT; break;
1799 case TYPE_FUNCTION: op = INSTR_LOAD_FNC; break;
1801 case TYPE_POINTER: op = INSTR_LOAD_I; break;
1802 case TYPE_INTEGER: op = INSTR_LOAD_I; break;
1805 irerror(self->context, "invalid type for ir_block_create_load_from_ent: %s", type_name[outype]);
1809 return ir_block_create_general_instr(self, ctx, label, op, ent, field, outype);
1812 ir_value* ir_block_create_add(ir_block *self, lex_ctx ctx,
1814 ir_value *left, ir_value *right)
1817 int l = left->vtype;
1818 int r = right->vtype;
1822 irerror(self->context, "invalid type for ir_block_create_add: %s", type_name[l]);
1838 if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1840 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1845 irerror(self->context, "invalid type for ir_block_create_add: %s", type_name[l]);
1849 return ir_block_create_binop(self, ctx, label, op, left, right);
1852 ir_value* ir_block_create_sub(ir_block *self, lex_ctx ctx,
1854 ir_value *left, ir_value *right)
1857 int l = left->vtype;
1858 int r = right->vtype;
1863 irerror(self->context, "invalid type for ir_block_create_sub: %s", type_name[l]);
1879 if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1881 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1886 irerror(self->context, "invalid type for ir_block_create_sub: %s", type_name[l]);
1890 return ir_block_create_binop(self, ctx, label, op, left, right);
1893 ir_value* ir_block_create_mul(ir_block *self, lex_ctx ctx,
1895 ir_value *left, ir_value *right)
1898 int l = left->vtype;
1899 int r = right->vtype;
1904 irerror(self->context, "invalid type for ir_block_create_mul: %s", type_name[l]);
1919 if ( (l == TYPE_VECTOR && r == TYPE_FLOAT) )
1921 else if ( (l == TYPE_FLOAT && r == TYPE_VECTOR) )
1924 else if ( (l == TYPE_VECTOR && r == TYPE_INTEGER) )
1926 else if ( (l == TYPE_INTEGER && r == TYPE_VECTOR) )
1928 else if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1930 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1934 irerror(self->context, "invalid type for ir_block_create_mul: %s", type_name[l]);
1938 return ir_block_create_binop(self, ctx, label, op, left, right);
1941 ir_value* ir_block_create_div(ir_block *self, lex_ctx ctx,
1943 ir_value *left, ir_value *right)
1946 int l = left->vtype;
1947 int r = right->vtype;
1952 irerror(self->context, "invalid type for ir_block_create_div: %s", type_name[l]);
1965 if ( (l == TYPE_VECTOR && r == TYPE_FLOAT) )
1967 else if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1969 else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1974 irerror(self->context, "invalid type for ir_block_create_div: %s", type_name[l]);
1978 return ir_block_create_binop(self, ctx, label, op, left, right);
1981 /* PHI resolving breaks the SSA, and must thus be the last
1982 * step before life-range calculation.
1985 static bool ir_block_naive_phi(ir_block *self);
1986 bool ir_function_naive_phi(ir_function *self)
1990 for (i = 0; i < vec_size(self->blocks); ++i)
1992 if (!ir_block_naive_phi(self->blocks[i]))
1999 static bool ir_naive_phi_emit_store(ir_block *block, size_t iid, ir_value *old, ir_value *what)
2004 /* create a store */
2005 if (!ir_block_create_store(block, old, what))
2008 /* we now move it up */
2009 instr = vec_last(block->instr);
2010 for (i = vec_size(block->instr)-1; i > iid; --i)
2011 block->instr[i] = block->instr[i-1];
2012 block->instr[i] = instr;
2018 static bool ir_block_naive_phi(ir_block *self)
2020 size_t i, p; /*, w;*/
2021 /* FIXME: optionally, create_phi can add the phis
2022 * to a list so we don't need to loop through blocks
2023 * - anyway: "don't optimize YET"
2025 for (i = 0; i < vec_size(self->instr); ++i)
2027 ir_instr *instr = self->instr[i];
2028 if (instr->opcode != VINSTR_PHI)
2031 vec_remove(self->instr, i, 1);
2032 --i; /* NOTE: i+1 below */
2034 for (p = 0; p < vec_size(instr->phi); ++p)
2036 ir_value *v = instr->phi[p].value;
2037 ir_block *b = instr->phi[p].from;
2039 if (v->store == store_value &&
2040 vec_size(v->reads) == 1 &&
2041 vec_size(v->writes) == 1)
2043 /* replace the value */
2044 if (!ir_instr_op(v->writes[0], 0, instr->_ops[0], true))
2049 /* force a move instruction */
2050 ir_instr *prevjump = vec_last(b->instr);
2053 instr->_ops[0]->store = store_global;
2054 if (!ir_block_create_store(b, instr->context, instr->_ops[0], v))
2056 instr->_ops[0]->store = store_value;
2057 vec_push(b->instr, prevjump);
2062 ir_value *v = instr->phi[p].value;
2063 for (w = 0; w < vec_size(v->writes); ++w) {
2066 if (!v->writes[w]->_ops[0])
2069 /* When the write was to a global, we have to emit a mov */
2070 old = v->writes[w]->_ops[0];
2072 /* The original instruction now writes to the PHI target local */
2073 if (v->writes[w]->_ops[0] == v)
2074 v->writes[w]->_ops[0] = instr->_ops[0];
2076 if (old->store != store_value && old->store != store_local && old->store != store_param)
2078 /* If it originally wrote to a global we need to store the value
2081 if (!ir_naive_phi_emit_store(self, i+1, old, v))
2083 if (i+1 < vec_size(self->instr))
2084 instr = self->instr[i+1];
2087 /* In case I forget and access instr later, it'll be NULL
2088 * when it's a problem, to make sure we crash, rather than accessing
2094 /* If it didn't, we can replace all reads by the phi target now. */
2096 for (r = 0; r < vec_size(old->reads); ++r)
2099 ir_instr *ri = old->reads[r];
2100 for (op = 0; op < vec_size(ri->phi); ++op) {
2101 if (ri->phi[op].value == old)
2102 ri->phi[op].value = v;
2104 for (op = 0; op < 3; ++op) {
2105 if (ri->_ops[op] == old)
2113 ir_instr_delete(instr);
2118 /***********************************************************************
2119 *IR Temp allocation code
2120 * Propagating value life ranges by walking through the function backwards
2121 * until no more changes are made.
2122 * In theory this should happen once more than once for every nested loop
2124 * Though this implementation might run an additional time for if nests.
2127 /* Enumerate instructions used by value's life-ranges
2129 static void ir_block_enumerate(ir_block *self, size_t *_eid)
2133 for (i = 0; i < vec_size(self->instr); ++i)
2135 self->instr[i]->eid = eid++;
2140 /* Enumerate blocks and instructions.
2141 * The block-enumeration is unordered!
2142 * We do not really use the block enumreation, however
2143 * the instruction enumeration is important for life-ranges.
2145 void ir_function_enumerate(ir_function *self)
2148 size_t instruction_id = 0;
2149 for (i = 0; i < vec_size(self->blocks); ++i)
2151 self->blocks[i]->eid = i;
2152 self->blocks[i]->run_id = 0;
2153 ir_block_enumerate(self->blocks[i], &instruction_id);
2157 static bool ir_block_life_propagate(ir_block *b, ir_block *prev, bool *changed);
2158 bool ir_function_calculate_liferanges(ir_function *self)
2166 for (i = 0; i != vec_size(self->blocks); ++i)
2168 if (self->blocks[i]->is_return)
2170 vec_free(self->blocks[i]->living);
2171 if (!ir_block_life_propagate(self->blocks[i], NULL, &changed))
2176 if (vec_size(self->blocks)) {
2177 ir_block *block = self->blocks[0];
2178 for (i = 0; i < vec_size(block->living); ++i) {
2179 ir_value *v = block->living[i];
2180 if (v->memberof || v->store != store_local)
2182 if (irwarning(v->context, WARN_USED_UNINITIALIZED,
2183 "variable `%s` may be used uninitialized in this function", v->name))
2192 /* Local-value allocator
2193 * After finishing creating the liferange of all values used in a function
2194 * we can allocate their global-positions.
2195 * This is the counterpart to register-allocation in register machines.
2202 } function_allocator;
2204 static bool function_allocator_alloc(function_allocator *alloc, const ir_value *var)
2207 size_t vsize = type_sizeof[var->vtype];
2209 slot = ir_value_var("reg", store_global, var->vtype);
2213 if (!ir_value_life_merge_into(slot, var))
2216 vec_push(alloc->locals, slot);
2217 vec_push(alloc->sizes, vsize);
2218 vec_push(alloc->unique, var->unique_life);
2223 ir_value_delete(slot);
2227 bool ir_function_allocate_locals(ir_function *self)
2236 function_allocator alloc;
2238 if (!vec_size(self->locals) && !vec_size(self->values))
2241 alloc.locals = NULL;
2243 alloc.positions = NULL;
2244 alloc.unique = NULL;
2246 for (i = 0; i < vec_size(self->locals); ++i)
2248 if (!OPTS_OPTIMIZATION(OPTIM_LOCALTEMPS))
2249 self->locals[i]->unique_life = true;
2250 if (!function_allocator_alloc(&alloc, self->locals[i]))
2254 /* Allocate a slot for any value that still exists */
2255 for (i = 0; i < vec_size(self->values); ++i)
2257 v = self->values[i];
2259 if (!vec_size(v->life))
2262 for (a = 0; a < vec_size(alloc.locals); ++a)
2264 /* if it's reserved for a unique liferange: skip */
2265 if (alloc.unique[a])
2268 slot = alloc.locals[a];
2270 /* never resize parameters
2271 * will be required later when overlapping temps + locals
2273 if (a < vec_size(self->params) &&
2274 alloc.sizes[a] < type_sizeof[v->vtype])
2279 if (ir_values_overlap(v, slot))
2282 if (!ir_value_life_merge_into(slot, v))
2285 /* adjust size for this slot */
2286 if (alloc.sizes[a] < type_sizeof[v->vtype])
2287 alloc.sizes[a] = type_sizeof[v->vtype];
2289 self->values[i]->code.local = a;
2292 if (a >= vec_size(alloc.locals)) {
2293 self->values[i]->code.local = vec_size(alloc.locals);
2294 if (!function_allocator_alloc(&alloc, v))
2303 /* Adjust slot positions based on sizes */
2304 vec_push(alloc.positions, 0);
2306 if (vec_size(alloc.sizes))
2307 pos = alloc.positions[0] + alloc.sizes[0];
2310 for (i = 1; i < vec_size(alloc.sizes); ++i)
2312 pos = alloc.positions[i-1] + alloc.sizes[i-1];
2313 vec_push(alloc.positions, pos);
2316 self->allocated_locals = pos + vec_last(alloc.sizes);
2318 /* Locals need to know their new position */
2319 for (i = 0; i < vec_size(self->locals); ++i) {
2320 self->locals[i]->code.local = alloc.positions[i];
2322 /* Take over the actual slot positions on values */
2323 for (i = 0; i < vec_size(self->values); ++i) {
2324 self->values[i]->code.local = alloc.positions[self->values[i]->code.local];
2332 for (i = 0; i < vec_size(alloc.locals); ++i)
2333 ir_value_delete(alloc.locals[i]);
2334 vec_free(alloc.locals);
2335 vec_free(alloc.sizes);
2336 vec_free(alloc.positions);
2340 /* Get information about which operand
2341 * is read from, or written to.
2343 static void ir_op_read_write(int op, size_t *read, size_t *write)
2363 case INSTR_STOREP_F:
2364 case INSTR_STOREP_V:
2365 case INSTR_STOREP_S:
2366 case INSTR_STOREP_ENT:
2367 case INSTR_STOREP_FLD:
2368 case INSTR_STOREP_FNC:
2379 static bool ir_block_living_add_instr(ir_block *self, size_t eid)
2382 bool changed = false;
2384 for (i = 0; i != vec_size(self->living); ++i)
2386 tempbool = ir_value_life_merge(self->living[i], eid);
2389 irerror(self->context, "block_living_add_instr() value instruction added %s: %i", self->living[i]->_name, (int)eid);
2391 changed = changed || tempbool;
2396 static bool ir_block_life_prop_previous(ir_block* self, ir_block *prev, bool *changed)
2402 /* values which have been read in a previous iteration are now
2403 * in the "living" array even if the previous block doesn't use them.
2404 * So we have to remove whatever does not exist in the previous block.
2405 * They will be re-added on-read, but the liferange merge won't cause
2407 for (i = 0; i < vec_size(self->living); ++i)
2409 if (!vec_ir_value_find(prev->living, self->living[i], NULL)) {
2410 vec_remove(self->living, i, 1);
2416 /* Whatever the previous block still has in its living set
2417 * must now be added to ours as well.
2419 for (i = 0; i < vec_size(prev->living); ++i)
2421 if (vec_ir_value_find(self->living, prev->living[i], NULL))
2423 vec_push(self->living, prev->living[i]);
2425 irerror(self->contextt from prev: %s", self->label, prev->living[i]->_name);
2431 static bool ir_block_life_propagate(ir_block *self, ir_block *prev, bool *changed)
2437 /* bitmasks which operands are read from or written to */
2439 char dbg_ind[16] = { '#', '0' };
2444 if (!ir_block_life_prop_previous(self, prev, changed))
2448 i = vec_size(self->instr);
2451 instr = self->instr[i];
2453 /* PHI operands are always read operands */
2454 for (p = 0; p < vec_size(instr->phi); ++p)
2456 value = instr->phi[p].value;
2457 if (value->memberof)
2458 value = value->memberof;
2459 if (!vec_ir_value_find(self->living, value, NULL))
2460 vec_push(self->living, value);
2463 /* call params are read operands too */
2464 for (p = 0; p < vec_size(instr->params); ++p)
2466 value = instr->params[p];
2467 if (value->memberof)
2468 value = value->memberof;
2469 if (!vec_ir_value_find(self->living, value, NULL))
2470 vec_push(self->living, value);
2473 /* See which operands are read and write operands */
2474 ir_op_read_write(instr->opcode, &read, &write);
2476 if (instr->opcode == INSTR_MUL_VF)
2478 /* the float source will get an additional lifetime */
2479 tempbool = ir_value_life_merge(instr->_ops[2], instr->eid+1);
2480 *changed = *changed || tempbool;
2482 else if (instr->opcode == INSTR_MUL_FV)
2484 /* the float source will get an additional lifetime */
2485 tempbool = ir_value_life_merge(instr->_ops[1], instr->eid+1);
2486 *changed = *changed || tempbool;
2489 /* Go through the 3 main operands */
2490 for (o = 0; o < 3; ++o)
2492 if (!instr->_ops[o]) /* no such operand */
2495 value = instr->_ops[o];
2496 if (value->memberof)
2497 value = value->memberof;
2499 /* We only care about locals */
2500 /* we also calculate parameter liferanges so that locals
2501 * can take up parameter slots */
2502 if (value->store != store_value &&
2503 value->store != store_local &&
2504 value->store != store_param)
2510 if (!vec_ir_value_find(self->living, value, NULL))
2511 vec_push(self->living, value);
2514 /* write operands */
2515 /* When we write to a local, we consider it "dead" for the
2516 * remaining upper part of the function, since in SSA a value
2517 * can only be written once (== created)
2522 bool in_living = vec_ir_value_find(self->living, value, &idx);
2525 /* If the value isn't alive it hasn't been read before... */
2526 /* TODO: See if the warning can be emitted during parsing or AST processing
2527 * otherwise have warning printed here.
2528 * IF printing a warning here: include filecontext_t,
2529 * and make sure it's only printed once
2530 * since this function is run multiple times.
2532 /* For now: debug info: */
2533 /* con_err( "Value only written %s\n", value->name); */
2534 tempbool = ir_value_life_merge(value, instr->eid);
2535 *changed = *changed || tempbool;
2537 ir_instr_dump(instr, dbg_ind, printf);
2541 /* since 'living' won't contain it
2542 * anymore, merge the value, since
2545 tempbool = ir_value_life_merge(value, instr->eid);
2548 con_err( "value added id %s %i\n", value->name, (int)instr->eid);
2550 *changed = *changed || tempbool;
2552 vec_remove(self->living, idx, 1);
2557 tempbool = ir_block_living_add_instr(self, instr->eid);
2558 /*con_err( "living added values\n");*/
2559 *changed = *changed || tempbool;
2563 if (self->run_id == self->owner->run_id)
2566 self->run_id = self->owner->run_id;
2568 for (i = 0; i < vec_size(self->entries); ++i)
2570 ir_block *entry = self->entries[i];
2571 ir_block_life_propagate(entry, self, changed);
2577 /***********************************************************************
2580 * Since the IR has the convention of putting 'write' operands
2581 * at the beginning, we have to rotate the operands of instructions
2582 * properly in order to generate valid QCVM code.
2584 * Having destinations at a fixed position is more convenient. In QC
2585 * this is *mostly* OPC, but FTE adds at least 2 instructions which
2586 * read from from OPA, and store to OPB rather than OPC. Which is
2587 * partially the reason why the implementation of these instructions
2588 * in darkplaces has been delayed for so long.
2590 * Breaking conventions is annoying...
2592 static bool ir_builder_gen_global(ir_builder *self, ir_value *global, bool islocal);
2594 static bool gen_global_field(ir_value *global)
2596 if (global->hasvalue)
2598 ir_value *fld = global->constval.vpointer;
2600 irerror(global->context, "Invalid field constant with no field: %s", global->name);
2604 /* copy the field's value */
2605 ir_value_code_setaddr(global, vec_size(code_globals));
2606 vec_push(code_globals, fld->code.fieldaddr);
2607 if (global->fieldtype == TYPE_VECTOR) {
2608 vec_push(code_globals, fld->code.fieldaddr+1);
2609 vec_push(code_globals, fld->code.fieldaddr+2);
2614 ir_value_code_setaddr(global, vec_size(code_globals));
2615 vec_push(code_globals, 0);
2616 if (global->fieldtype == TYPE_VECTOR) {
2617 vec_push(code_globals, 0);
2618 vec_push(code_globals, 0);
2621 if (global->code.globaladdr < 0)
2626 static bool gen_global_pointer(ir_value *global)
2628 if (global->hasvalue)
2630 ir_value *target = global->constval.vpointer;
2632 irerror(global->context, "Invalid pointer constant: %s", global->name);
2633 /* NULL pointers are pointing to the NULL constant, which also
2634 * sits at address 0, but still has an ir_value for itself.
2639 /* Here, relocations ARE possible - in fteqcc-enhanced-qc:
2640 * void() foo; <- proto
2641 * void() *fooptr = &foo;
2642 * void() foo = { code }
2644 if (!target->code.globaladdr) {
2645 /* FIXME: Check for the constant nullptr ir_value!
2646 * because then code.globaladdr being 0 is valid.
2648 irerror(global->context, "FIXME: Relocation support");
2652 ir_value_code_setaddr(global, vec_size(code_globals));
2653 vec_push(code_globals, target->code.globaladdr);
2657 ir_value_code_setaddr(global, vec_size(code_globals));
2658 vec_push(code_globals, 0);
2660 if (global->code.globaladdr < 0)
2665 static bool gen_blocks_recursive(ir_function *func, ir_block *block)
2667 prog_section_statement stmt;
2676 block->generated = true;
2677 block->code_start = vec_size(code_statements);
2678 for (i = 0; i < vec_size(block->instr); ++i)
2680 instr = block->instr[i];
2682 if (instr->opcode == VINSTR_PHI) {
2683 irerror(block->context, "cannot generate virtual instruction (phi)");
2687 if (instr->opcode == VINSTR_JUMP) {
2688 target = instr->bops[0];
2689 /* for uncoditional jumps, if the target hasn't been generated
2690 * yet, we generate them right here.
2692 if (!target->generated) {
2697 /* otherwise we generate a jump instruction */
2698 stmt.opcode = INSTR_GOTO;
2699 stmt.o1.s1 = (target->code_start) - vec_size(code_statements);
2702 code_push_statement(&stmt, instr->context.line);
2704 /* no further instructions can be in this block */
2708 if (instr->opcode == VINSTR_COND) {
2709 ontrue = instr->bops[0];
2710 onfalse = instr->bops[1];
2711 /* TODO: have the AST signal which block should
2712 * come first: eg. optimize IFs without ELSE...
2715 stmt.o1.u1 = ir_value_code_addr(instr->_ops[0]);
2719 if (ontrue->generated) {
2720 stmt.opcode = INSTR_IF;
2721 stmt.o2.s1 = (ontrue->code_start) - vec_size(code_statements);
2722 code_push_statement(&stmt, instr->context.line);
2724 if (onfalse->generated) {
2725 stmt.opcode = INSTR_IFNOT;
2726 stmt.o2.s1 = (onfalse->code_start) - vec_size(code_statements);
2727 code_push_statement(&stmt, instr->context.line);
2729 if (!ontrue->generated) {
2730 if (onfalse->generated) {
2735 if (!onfalse->generated) {
2736 if (ontrue->generated) {
2741 /* neither ontrue nor onfalse exist */
2742 stmt.opcode = INSTR_IFNOT;
2743 if (!instr->likely) {
2744 /* Honor the likelyhood hint */
2745 ir_block *tmp = onfalse;
2746 stmt.opcode = INSTR_IF;
2750 stidx = vec_size(code_statements);
2751 code_push_statement(&stmt, instr->context.line);
2752 /* on false we jump, so add ontrue-path */
2753 if (!gen_blocks_recursive(func, ontrue))
2755 /* fixup the jump address */
2756 code_statements[stidx].o2.s1 = vec_size(code_statements) - stidx;
2757 /* generate onfalse path */
2758 if (onfalse->generated) {
2759 /* fixup the jump address */
2760 code_statements[stidx].o2.s1 = (onfalse->code_start) - (stidx);
2761 stmt.opcode = vec_last(code_statements).opcode;
2762 if (stmt.opcode == INSTR_GOTO ||
2763 stmt.opcode == INSTR_IF ||
2764 stmt.opcode == INSTR_IFNOT ||
2765 stmt.opcode == INSTR_RETURN ||
2766 stmt.opcode == INSTR_DONE)
2768 /* no use jumping from here */
2771 /* may have been generated in the previous recursive call */
2772 stmt.opcode = INSTR_GOTO;
2773 stmt.o1.s1 = (onfalse->code_start) - vec_size(code_statements);
2776 code_push_statement(&stmt, instr->context.line);
2779 /* if not, generate now */
2784 if (instr->opcode >= INSTR_CALL0 && instr->opcode <= INSTR_CALL8) {
2785 /* Trivial call translation:
2786 * copy all params to OFS_PARM*
2787 * if the output's storetype is not store_return,
2788 * add append a STORE instruction!
2790 * NOTES on how to do it better without much trouble:
2791 * -) The liferanges!
2792 * Simply check the liferange of all parameters for
2793 * other CALLs. For each param with no CALL in its
2794 * liferange, we can store it in an OFS_PARM at
2795 * generation already. This would even include later
2796 * reuse.... probably... :)
2801 first = vec_size(instr->params);
2804 for (p = 0; p < first; ++p)
2806 ir_value *param = instr->params[p];
2808 stmt.opcode = INSTR_STORE_F;
2811 if (param->vtype == TYPE_FIELD)
2812 stmt.opcode = field_store_instr[param->fieldtype];
2814 stmt.opcode = type_store_instr[param->vtype];
2815 stmt.o1.u1 = ir_value_code_addr(param);
2816 stmt.o2.u1 = OFS_PARM0 + 3 * p;
2817 code_push_statement(&stmt, instr->context.line);
2819 /* Now handle extparams */
2820 first = vec_size(instr->params);
2821 for (; p < first; ++p)
2823 ir_builder *ir = func->owner;
2824 ir_value *param = instr->params[p];
2825 ir_value *targetparam;
2827 if (p-8 >= vec_size(ir->extparams))
2828 ir_gen_extparam(ir);
2830 targetparam = ir->extparams[p-8];
2832 stmt.opcode = INSTR_STORE_F;
2835 if (param->vtype == TYPE_FIELD)
2836 stmt.opcode = field_store_instr[param->fieldtype];
2838 stmt.opcode = type_store_instr[param->vtype];
2839 stmt.o1.u1 = ir_value_code_addr(param);
2840 stmt.o2.u1 = ir_value_code_addr(targetparam);
2841 code_push_statement(&stmt, instr->context.line);
2844 stmt.opcode = INSTR_CALL0 + vec_size(instr->params);
2845 if (stmt.opcode > INSTR_CALL8)
2846 stmt.opcode = INSTR_CALL8;
2847 stmt.o1.u1 = ir_value_code_addr(instr->_ops[1]);
2850 code_push_statement(&stmt, instr->context.line);
2852 retvalue = instr->_ops[0];
2853 if (retvalue && retvalue->store != store_return && vec_size(retvalue->life))
2855 /* not to be kept in OFS_RETURN */
2856 if (retvalue->vtype == TYPE_FIELD)
2857 stmt.opcode = field_store_instr[retvalue->vtype];
2859 stmt.opcode = type_store_instr[retvalue->vtype];
2860 stmt.o1.u1 = OFS_RETURN;
2861 stmt.o2.u1 = ir_value_code_addr(retvalue);
2863 code_push_statement(&stmt, instr->context.line);
2868 if (instr->opcode == INSTR_STATE) {
2869 irerror(block->context, "TODO: state instruction");
2873 stmt.opcode = instr->opcode;
2878 /* This is the general order of operands */
2880 stmt.o3.u1 = ir_value_code_addr(instr->_ops[0]);
2883 stmt.o1.u1 = ir_value_code_addr(instr->_ops[1]);
2886 stmt.o2.u1 = ir_value_code_addr(instr->_ops[2]);
2888 if (stmt.opcode == INSTR_RETURN || stmt.opcode == INSTR_DONE)
2890 stmt.o1.u1 = stmt.o3.u1;
2893 else if ((stmt.opcode >= INSTR_STORE_F &&
2894 stmt.opcode <= INSTR_STORE_FNC) ||
2895 (stmt.opcode >= INSTR_STOREP_F &&
2896 stmt.opcode <= INSTR_STOREP_FNC))
2898 /* 2-operand instructions with A -> B */
2899 stmt.o2.u1 = stmt.o3.u1;
2902 /* tiny optimization, don't output
2905 if (stmt.o2.u1 == stmt.o1.u1 &&
2906 OPTS_OPTIMIZATION(OPTIM_PEEPHOLE))
2908 ++optimization_count[OPTIM_PEEPHOLE];
2913 code_push_statement(&stmt, instr->context.line);
2918 static bool gen_function_code(ir_function *self)
2921 prog_section_statement stmt;
2923 /* Starting from entry point, we generate blocks "as they come"
2924 * for now. Dead blocks will not be translated obviously.
2926 if (!vec_size(self->blocks)) {
2927 irerror(self->context, "Function '%s' declared without body.", self->name);
2931 block = self->blocks[0];
2932 if (block->generated)
2935 if (!gen_blocks_recursive(self, block)) {
2936 irerror(self->context, "failed to generate blocks for '%s'", self->name);
2940 /* otherwise code_write crashes since it debug-prints functions until AINSTR_END */
2941 stmt.opcode = AINSTR_END;
2945 code_push_statement(&stmt, vec_last(code_linenums));
2949 static qcint ir_builder_filestring(ir_builder *ir, const char *filename)
2951 /* NOTE: filename pointers are copied, we never strdup them,
2952 * thus we can use pointer-comparison to find the string.
2957 for (i = 0; i < vec_size(ir->filenames); ++i) {
2958 if (ir->filenames[i] == filename)
2959 return ir->filestrings[i];
2962 str = code_genstring(filename);
2963 vec_push(ir->filenames, filename);
2964 vec_push(ir->filestrings, str);
2968 static bool gen_global_function(ir_builder *ir, ir_value *global)
2970 prog_section_function fun;
2974 #ifndef NEW_ALLOC_STRAT
2975 size_t local_var_end;
2978 if (!global->hasvalue || (!global->constval.vfunc))
2980 irerror(global->context, "Invalid state of function-global: not constant: %s", global->name);
2984 irfun = global->constval.vfunc;
2986 fun.name = global->code.name;
2987 fun.file = ir_builder_filestring(ir, global->context.file);
2988 fun.profile = 0; /* always 0 */
2989 fun.nargs = vec_size(irfun->params);
2993 for (i = 0;i < 8; ++i) {
2994 if ((int32_t)i >= fun.nargs)
2997 fun.argsize[i] = type_sizeof[irfun->params[i]];
3000 fun.firstlocal = vec_size(code_globals);
3002 #ifndef NEW_ALLOC_STRAT
3003 local_var_end = fun.firstlocal;
3004 for (i = 0; i < vec_size(irfun->locals); ++i) {
3005 if (!ir_builder_gen_global(ir, irfun->locals[i], true)) {
3006 irerror(irfun->locals[i]->context, "Failed to generate local %s", irfun->locals[i]->name);
3010 if (vec_size(irfun->locals)) {
3011 ir_value *last = vec_last(irfun->locals);
3012 local_var_end = last->code.globaladdr;
3013 if (last->vtype == TYPE_FIELD && last->fieldtype == TYPE_VECTOR)
3014 local_var_end += type_sizeof[TYPE_VECTOR];
3016 local_var_end += type_sizeof[last->vtype];
3018 for (i = 0; i < vec_size(irfun->values); ++i)
3020 /* generate code.globaladdr for ssa values */
3021 ir_value *v = irfun->values[i];
3022 ir_value_code_setaddr(v, local_var_end + v->code.local);
3024 for (i = 0; i < irfun->allocated_locals; ++i) {
3025 /* fill the locals with zeros */
3026 vec_push(code_globals, 0);
3029 fun.locals = vec_size(code_globals) - fun.firstlocal;
3031 fun.locals = irfun->allocated_locals;
3032 for (i = 0; i < vec_size(irfun->locals); ++i) {
3033 if (!ir_builder_gen_global(ir, irfun->locals[i], true)) {
3034 irerror(irfun->locals[i]->context, "Failed to generate local %s", irfun->locals[i]->name);
3037 ir_value_code_setaddr(irfun->locals[i], fun.firstlocal + irfun->locals[i]->code.local);
3039 for (i = vec_size(code_globals) - fun.firstlocal; i < fun.locals; ++i) {
3040 vec_push(code_globals, 0);
3042 for (i = 0; i < vec_size(irfun->values); ++i)
3044 /* generate code.globaladdr for ssa values */
3045 ir_value *v = irfun->values[i];
3046 ir_value_code_setaddr(v, fun.firstlocal + v->code.local);
3051 fun.entry = irfun->builtin+1;
3053 irfun->code_function_def = vec_size(code_functions);
3054 fun.entry = vec_size(code_statements);
3057 vec_push(code_functions, fun);
3061 static void ir_gen_extparam(ir_builder *ir)
3063 prog_section_def def;
3067 snprintf(name, sizeof(name), "EXTPARM#%i", (int)(vec_size(ir->extparams)+8));
3068 global = ir_value_var(name, store_global, TYPE_VECTOR);
3070 def.name = code_genstring(name);
3071 def.type = TYPE_VECTOR;
3072 def.offset = vec_size(code_globals);
3074 vec_push(code_defs, def);
3075 ir_value_code_setaddr(global, def.offset);
3076 vec_push(code_globals, 0);
3077 vec_push(code_globals, 0);
3078 vec_push(code_globals, 0);
3080 vec_push(ir->extparams, global);
3083 static bool gen_function_extparam_copy(ir_function *self)
3085 size_t i, ext, numparams;
3087 ir_builder *ir = self->owner;
3089 prog_section_statement stmt;
3091 numparams = vec_size(self->params);
3095 stmt.opcode = INSTR_STORE_F;
3097 for (i = 8; i < numparams; ++i) {
3099 if (ext >= vec_size(ir->extparams))
3100 ir_gen_extparam(ir);
3102 ep = ir->extparams[ext];
3104 stmt.opcode = type_store_instr[self->locals[i]->vtype];
3105 if (self->locals[i]->vtype == TYPE_FIELD &&
3106 self->locals[i]->fieldtype == TYPE_VECTOR)
3108 stmt.opcode = INSTR_STORE_V;
3110 stmt.o1.u1 = ir_value_code_addr(ep);
3111 stmt.o2.u1 = ir_value_code_addr(self->locals[i]);
3112 code_push_statement(&stmt, self->context.line);
3118 static bool gen_global_function_code(ir_builder *ir, ir_value *global)
3120 prog_section_function *fundef;
3125 irfun = global->constval.vfunc;
3127 if (global->cvq == CV_NONE) {
3128 irwarning(global->context, WARN_IMPLICIT_FUNCTION_POINTER,
3129 "function `%s` has no body and in QC implicitly becomes a function-pointer", global->name);
3131 /* this was a function pointer, don't generate code for those */
3138 if (irfun->code_function_def < 0) {
3139 irerror(irfun->context, "`%s`: IR global wasn't generated, failed to access function-def", irfun->name);
3142 fundef = &code_functions[irfun->code_function_def];
3144 fundef->entry = vec_size(code_statements);
3145 if (!gen_function_extparam_copy(irfun)) {
3146 irerror(irfun->context, "Failed to generate extparam-copy code for function %s", irfun->name);
3149 if (!gen_function_code(irfun)) {
3150 irerror(irfun->context, "Failed to generate code for function %s", irfun->name);
3156 static bool ir_builder_gen_global(ir_builder *self, ir_value *global, bool islocal)
3160 prog_section_def def;
3162 def.type = global->vtype;
3163 def.offset = vec_size(code_globals);
3166 if (global->name[0] == '#') {
3167 if (!self->str_immediate)
3168 self->str_immediate = code_genstring("IMMEDIATE");
3169 def.name = global->code.name = self->str_immediate;
3172 def.name = global->code.name = code_genstring(global->name);
3177 switch (global->vtype)
3180 if (!strcmp(global->name, "end_sys_globals")) {
3181 /* TODO: remember this point... all the defs before this one
3182 * should be checksummed and added to progdefs.h when we generate it.
3185 else if (!strcmp(global->name, "end_sys_fields")) {
3186 /* TODO: same as above but for entity-fields rather than globsl
3190 irwarning(global->context, WARN_VOID_VARIABLES, "unrecognized variable of type void `%s`",
3192 /* I'd argue setting it to 0 is sufficient, but maybe some depend on knowing how far
3193 * the system fields actually go? Though the engine knows this anyway...
3194 * Maybe this could be an -foption
3195 * fteqcc creates data for end_sys_* - of size 1, so let's do the same
3197 ir_value_code_setaddr(global, vec_size(code_globals));
3198 vec_push(code_globals, 0);
3200 vec_push(code_defs, def);
3203 vec_push(code_defs, def);
3204 return gen_global_pointer(global);
3206 vec_push(code_defs, def);
3207 return gen_global_field(global);
3212 ir_value_code_setaddr(global, vec_size(code_globals));
3213 if (global->hasvalue) {
3214 iptr = (int32_t*)&global->constval.ivec[0];
3215 vec_push(code_globals, *iptr);
3217 vec_push(code_globals, 0);
3219 def.type |= DEF_SAVEGLOBAL;
3221 vec_push(code_defs, def);
3223 return global->code.globaladdr >= 0;
3227 ir_value_code_setaddr(global, vec_size(code_globals));
3228 if (global->hasvalue) {
3229 vec_push(code_globals, code_genstring(global->constval.vstring));
3231 vec_push(code_globals, 0);
3233 def.type |= DEF_SAVEGLOBAL;
3235 vec_push(code_defs, def);
3236 return global->code.globaladdr >= 0;
3241 ir_value_code_setaddr(global, vec_size(code_globals));
3242 if (global->hasvalue) {
3243 iptr = (int32_t*)&global->constval.ivec[0];
3244 vec_push(code_globals, iptr[0]);
3245 if (global->code.globaladdr < 0)
3247 for (d = 1; d < type_sizeof[global->vtype]; ++d)
3249 vec_push(code_globals, iptr[d]);
3252 vec_push(code_globals, 0);
3253 if (global->code.globaladdr < 0)
3255 for (d = 1; d < type_sizeof[global->vtype]; ++d)
3257 vec_push(code_globals, 0);
3260 def.type |= DEF_SAVEGLOBAL;
3263 vec_push(code_defs, def);
3264 return global->code.globaladdr >= 0;
3267 ir_value_code_setaddr(global, vec_size(code_globals));
3268 if (!global->hasvalue) {
3269 vec_push(code_globals, 0);
3270 if (global->code.globaladdr < 0)
3273 vec_push(code_globals, vec_size(code_functions));
3274 if (!gen_global_function(self, global))
3277 def.type |= DEF_SAVEGLOBAL;
3279 vec_push(code_defs, def);
3282 /* assume biggest type */
3283 ir_value_code_setaddr(global, vec_size(code_globals));
3284 vec_push(code_globals, 0);
3285 for (i = 1; i < type_sizeof[TYPE_VARIANT]; ++i)
3286 vec_push(code_globals, 0);
3289 /* refuse to create 'void' type or any other fancy business. */
3290 irerror(global->context, "Invalid type for global variable `%s`: %s",
3291 global->name, type_name[global->vtype]);
3296 static void ir_builder_prepare_field(ir_value *field)
3298 field->code.fieldaddr = code_alloc_field(type_sizeof[field->fieldtype]);
3301 static bool ir_builder_gen_field(ir_builder *self, ir_value *field)
3303 prog_section_def def;
3304 prog_section_field fld;
3308 def.type = (uint16_t)field->vtype;
3309 def.offset = (uint16_t)vec_size(code_globals);
3311 /* create a global named the same as the field */
3312 if (opts.standard == COMPILER_GMQCC) {
3313 /* in our standard, the global gets a dot prefix */
3314 size_t len = strlen(field->name);
3317 /* we really don't want to have to allocate this, and 1024
3318 * bytes is more than enough for a variable/field name
3320 if (len+2 >= sizeof(name)) {
3321 irerror(field->context, "invalid field name size: %u", (unsigned int)len);
3326 memcpy(name+1, field->name, len); /* no strncpy - we used strlen above */
3329 def.name = code_genstring(name);
3330 fld.name = def.name + 1; /* we reuse that string table entry */
3332 /* in plain QC, there cannot be a global with the same name,
3333 * and so we also name the global the same.
3334 * FIXME: fteqcc should create a global as well
3335 * check if it actually uses the same name. Probably does
3337 def.name = code_genstring(field->name);
3338 fld.name = def.name;
3341 field->code.name = def.name;
3343 vec_push(code_defs, def);
3345 fld.type = field->fieldtype;
3347 if (fld.type == TYPE_VOID) {
3348 irerror(field->context, "field is missing a type: %s - don't know its size", field->name);
3352 fld.offset = field->code.fieldaddr;
3354 vec_push(code_fields, fld);
3356 ir_value_code_setaddr(field, vec_size(code_globals));
3357 vec_push(code_globals, fld.offset);
3358 if (fld.type == TYPE_VECTOR) {
3359 vec_push(code_globals, fld.offset+1);
3360 vec_push(code_globals, fld.offset+2);
3363 return field->code.globaladdr >= 0;
3366 bool ir_builder_generate(ir_builder *self, const char *filename)
3368 prog_section_statement stmt;
3370 char *lnofile = NULL;
3374 for (i = 0; i < vec_size(self->fields); ++i)
3376 ir_builder_prepare_field(self->fields[i]);
3379 for (i = 0; i < vec_size(self->globals); ++i)
3381 if (!ir_builder_gen_global(self, self->globals[i], false)) {
3386 for (i = 0; i < vec_size(self->fields); ++i)
3388 if (!ir_builder_gen_field(self, self->fields[i])) {
3393 /* generate function code */
3394 for (i = 0; i < vec_size(self->globals); ++i)
3396 if (self->globals[i]->vtype == TYPE_FUNCTION) {
3397 if (!gen_global_function_code(self, self->globals[i])) {
3403 if (vec_size(code_globals) >= 65536) {
3404 irerror(vec_last(self->globals)->context, "This progs file would require more globals than the metadata can handle. Bailing out.");
3408 /* DP errors if the last instruction is not an INSTR_DONE
3409 * and for debugging purposes we add an additional AINSTR_END
3410 * to the end of functions, so here it goes:
3412 stmt.opcode = INSTR_DONE;
3416 code_push_statement(&stmt, vec_last(code_linenums));
3421 if (vec_size(code_statements) != vec_size(code_linenums)) {
3422 con_err("Linecounter wrong: %lu != %lu\n",
3423 (unsigned long)vec_size(code_statements),
3424 (unsigned long)vec_size(code_linenums));
3425 } else if (OPTS_FLAG(LNO)) {
3427 size_t filelen = strlen(filename);
3429 memcpy(vec_add(lnofile, filelen+1), filename, filelen+1);
3430 dot = strrchr(lnofile, '.');
3434 vec_shrinkto(lnofile, dot - lnofile);
3436 memcpy(vec_add(lnofile, 5), ".lno", 5);
3440 con_out("writing '%s' and '%s'...\n", filename, lnofile);
3442 con_out("writing '%s'\n", filename);
3443 if (!code_write(filename, lnofile)) {
3451 /***********************************************************************
3452 *IR DEBUG Dump functions...
3455 #define IND_BUFSZ 1024
3458 # define strncat(dst, src, sz) strncat_s(dst, sz, src, _TRUNCATE)
3461 const char *qc_opname(int op)
3463 if (op < 0) return "<INVALID>";
3464 if (op < (int)( sizeof(asm_instr) / sizeof(asm_instr[0]) ))
3465 return asm_instr[op].m;
3467 case VINSTR_PHI: return "PHI";
3468 case VINSTR_JUMP: return "JUMP";
3469 case VINSTR_COND: return "COND";
3470 default: return "<UNK>";
3474 void ir_builder_dump(ir_builder *b, int (*oprintf)(const char*, ...))
3477 char indent[IND_BUFSZ];
3481 oprintf("module %s\n", b->name);
3482 for (i = 0; i < vec_size(b->globals); ++i)
3485 if (b->globals[i]->hasvalue)
3486 oprintf("%s = ", b->globals[i]->name);
3487 ir_value_dump(b->globals[i], oprintf);
3490 for (i = 0; i < vec_size(b->functions); ++i)
3491 ir_function_dump(b->functions[i], indent, oprintf);
3492 oprintf("endmodule %s\n", b->name);
3495 void ir_function_dump(ir_function *f, char *ind,
3496 int (*oprintf)(const char*, ...))
3499 if (f->builtin != 0) {
3500 oprintf("%sfunction %s = builtin %i\n", ind, f->name, -f->builtin);
3503 oprintf("%sfunction %s\n", ind, f->name);
3504 strncat(ind, "\t", IND_BUFSZ);
3505 if (vec_size(f->locals))
3507 oprintf("%s%i locals:\n", ind, (int)vec_size(f->locals));
3508 for (i = 0; i < vec_size(f->locals); ++i) {
3509 oprintf("%s\t", ind);
3510 ir_value_dump(f->locals[i], oprintf);
3514 oprintf("%sliferanges:\n", ind);
3515 for (i = 0; i < vec_size(f->locals); ++i) {
3517 ir_value *v = f->locals[i];
3518 oprintf("%s\t%s: %s@%i ", ind, v->name, (v->unique_life ? "unique " : ""), (int)v->code.local);
3519 for (l = 0; l < vec_size(v->life); ++l) {
3520 oprintf("[%i,%i] ", v->life[l].start, v->life[l].end);
3524 for (i = 0; i < vec_size(f->values); ++i) {
3526 ir_value *v = f->values[i];
3527 oprintf("%s\t%s: @%i ", ind, v->name, (int)v->code.local);
3528 for (l = 0; l < vec_size(v->life); ++l) {
3529 oprintf("[%i,%i] ", v->life[l].start, v->life[l].end);
3533 if (vec_size(f->blocks))
3535 oprintf("%slife passes (check): %i\n", ind, (int)f->run_id);
3536 for (i = 0; i < vec_size(f->blocks); ++i) {
3537 if (f->blocks[i]->run_id != f->run_id) {
3538 oprintf("%slife pass check fail! %i != %i\n", ind, (int)f->blocks[i]->run_id, (int)f->run_id);
3540 ir_block_dump(f->blocks[i], ind, oprintf);
3544 ind[strlen(ind)-1] = 0;
3545 oprintf("%sendfunction %s\n", ind, f->name);
3548 void ir_block_dump(ir_block* b, char *ind,
3549 int (*oprintf)(const char*, ...))
3552 oprintf("%s:%s\n", ind, b->label);
3553 strncat(ind, "\t", IND_BUFSZ);
3555 for (i = 0; i < vec_size(b->instr); ++i)
3556 ir_instr_dump(b->instr[i], ind, oprintf);
3557 ind[strlen(ind)-1] = 0;
3560 void dump_phi(ir_instr *in, int (*oprintf)(const char*, ...))
3563 oprintf("%s <- phi ", in->_ops[0]->name);
3564 for (i = 0; i < vec_size(in->phi); ++i)
3566 oprintf("([%s] : %s) ", in->phi[i].from->label,
3567 in->phi[i].value->name);
3572 void ir_instr_dump(ir_instr *in, char *ind,
3573 int (*oprintf)(const char*, ...))
3576 const char *comma = NULL;
3578 oprintf("%s (%i) ", ind, (int)in->eid);
3580 if (in->opcode == VINSTR_PHI) {
3581 dump_phi(in, oprintf);
3585 strncat(ind, "\t", IND_BUFSZ);
3587 if (in->_ops[0] && (in->_ops[1] || in->_ops[2])) {
3588 ir_value_dump(in->_ops[0], oprintf);
3589 if (in->_ops[1] || in->_ops[2])
3592 if (in->opcode == INSTR_CALL0) {
3593 oprintf("CALL%i\t", vec_size(in->params));
3595 oprintf("%s\t", qc_opname(in->opcode));
3597 if (in->_ops[0] && !(in->_ops[1] || in->_ops[2])) {
3598 ir_value_dump(in->_ops[0], oprintf);
3603 for (i = 1; i != 3; ++i) {
3607 ir_value_dump(in->_ops[i], oprintf);
3615 oprintf("[%s]", in->bops[0]->label);
3619 oprintf("%s[%s]", comma, in->bops[1]->label);
3620 if (vec_size(in->params)) {
3621 oprintf("\tparams: ");
3622 for (i = 0; i != vec_size(in->params); ++i) {
3623 oprintf("%s, ", in->params[i]->name);
3627 ind[strlen(ind)-1] = 0;
3630 void ir_value_dump_string(const char *str, int (*oprintf)(const char*, ...))
3633 for (; *str; ++str) {
3635 case '\n': oprintf("\\n"); break;
3636 case '\r': oprintf("\\r"); break;
3637 case '\t': oprintf("\\t"); break;
3638 case '\v': oprintf("\\v"); break;
3639 case '\f': oprintf("\\f"); break;
3640 case '\b': oprintf("\\b"); break;
3641 case '\a': oprintf("\\a"); break;
3642 case '\\': oprintf("\\\\"); break;
3643 case '"': oprintf("\\\""); break;
3644 default: oprintf("%c", *str); break;
3650 void ir_value_dump(ir_value* v, int (*oprintf)(const char*, ...))
3659 oprintf("fn:%s", v->name);
3662 oprintf("%g", v->constval.vfloat);
3665 oprintf("'%g %g %g'",
3668 v->constval.vvec.z);
3671 oprintf("(entity)");
3674 ir_value_dump_string(v->constval.vstring, oprintf);
3678 oprintf("%i", v->constval.vint);
3683 v->constval.vpointer->name);
3687 oprintf("%s", v->name);
3691 void ir_value_dump_life(const ir_value *self, int (*oprintf)(const char*,...))
3694 oprintf("Life of %12s:", self->name);
3695 for (i = 0; i < vec_size(self->life); ++i)
3697 oprintf(" + [%i, %i]\n", self->life[i].start, self->life[i].end);