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 ir_value* ir_gen_extparam_proto(ir_builder *ir);
198 static void ir_gen_extparam (ir_builder *ir);
200 /* error functions */
202 static void irerror(lex_ctx ctx, const char *msg, ...)
206 con_cvprintmsg((void*)&ctx, LVL_ERROR, "internal error", msg, ap);
210 static bool irwarning(lex_ctx ctx, int warntype, const char *fmt, ...)
215 r = vcompile_warning(ctx, warntype, fmt, ap);
220 /***********************************************************************
221 * Vector utility functions
224 bool GMQCC_WARN vec_ir_value_find(ir_value **vec, const ir_value *what, size_t *idx)
227 size_t len = vec_size(vec);
228 for (i = 0; i < len; ++i) {
229 if (vec[i] == what) {
237 bool GMQCC_WARN vec_ir_block_find(ir_block **vec, ir_block *what, size_t *idx)
240 size_t len = vec_size(vec);
241 for (i = 0; i < len; ++i) {
242 if (vec[i] == what) {
250 bool GMQCC_WARN vec_ir_instr_find(ir_instr **vec, ir_instr *what, size_t *idx)
253 size_t len = vec_size(vec);
254 for (i = 0; i < len; ++i) {
255 if (vec[i] == what) {
263 /***********************************************************************
267 static void ir_block_delete_quick(ir_block* self);
268 static void ir_instr_delete_quick(ir_instr *self);
269 static void ir_function_delete_quick(ir_function *self);
271 ir_builder* ir_builder_new(const char *modulename)
275 self = (ir_builder*)mem_a(sizeof(*self));
279 self->functions = NULL;
280 self->globals = NULL;
282 self->filenames = NULL;
283 self->filestrings = NULL;
284 self->htglobals = util_htnew(IR_HT_SIZE);
285 self->htfields = util_htnew(IR_HT_SIZE);
286 self->htfunctions = util_htnew(IR_HT_SIZE);
288 self->extparams = NULL;
289 self->extparam_protos = NULL;
291 self->max_locals = 0;
293 self->str_immediate = 0;
295 if (!ir_builder_set_name(self, modulename)) {
303 void ir_builder_delete(ir_builder* self)
306 util_htdel(self->htglobals);
307 util_htdel(self->htfields);
308 util_htdel(self->htfunctions);
309 mem_d((void*)self->name);
310 for (i = 0; i != vec_size(self->functions); ++i) {
311 ir_function_delete_quick(self->functions[i]);
313 vec_free(self->functions);
314 for (i = 0; i != vec_size(self->extparams); ++i) {
315 ir_value_delete(self->extparams[i]);
317 vec_free(self->extparams);
318 for (i = 0; i != vec_size(self->globals); ++i) {
319 ir_value_delete(self->globals[i]);
321 vec_free(self->globals);
322 for (i = 0; i != vec_size(self->fields); ++i) {
323 ir_value_delete(self->fields[i]);
325 vec_free(self->fields);
326 vec_free(self->filenames);
327 vec_free(self->filestrings);
331 bool ir_builder_set_name(ir_builder *self, const char *name)
334 mem_d((void*)self->name);
335 self->name = util_strdup(name);
339 ir_function* ir_builder_get_function(ir_builder *self, const char *name)
341 return (ir_function*)util_htget(self->htfunctions, name);
344 ir_function* ir_builder_create_function(ir_builder *self, const char *name, int outtype)
346 ir_function *fn = ir_builder_get_function(self, name);
351 fn = ir_function_new(self, outtype);
352 if (!ir_function_set_name(fn, name))
354 ir_function_delete(fn);
357 vec_push(self->functions, fn);
358 util_htset(self->htfunctions, name, fn);
360 fn->value = ir_builder_create_global(self, fn->name, TYPE_FUNCTION);
362 ir_function_delete(fn);
366 fn->value->hasvalue = true;
367 fn->value->outtype = outtype;
368 fn->value->constval.vfunc = fn;
369 fn->value->context = fn->context;
374 ir_value* ir_builder_get_global(ir_builder *self, const char *name)
376 return (ir_value*)util_htget(self->htglobals, name);
379 ir_value* ir_builder_create_global(ir_builder *self, const char *name, int vtype)
383 if (name && name[0] != '#')
385 ve = ir_builder_get_global(self, name);
391 ve = ir_value_var(name, store_global, vtype);
392 vec_push(self->globals, ve);
393 util_htset(self->htglobals, name, ve);
397 ir_value* ir_builder_get_field(ir_builder *self, const char *name)
399 return (ir_value*)util_htget(self->htfields, name);
403 ir_value* ir_builder_create_field(ir_builder *self, const char *name, int vtype)
405 ir_value *ve = ir_builder_get_field(self, name);
410 ve = ir_value_var(name, store_global, TYPE_FIELD);
411 ve->fieldtype = vtype;
412 vec_push(self->fields, ve);
413 util_htset(self->htfields, name, ve);
417 /***********************************************************************
421 bool ir_function_naive_phi(ir_function*);
422 void ir_function_enumerate(ir_function*);
423 bool ir_function_calculate_liferanges(ir_function*);
424 bool ir_function_allocate_locals(ir_function*);
426 ir_function* ir_function_new(ir_builder* owner, int outtype)
429 self = (ir_function*)mem_a(sizeof(*self));
434 memset(self, 0, sizeof(*self));
437 if (!ir_function_set_name(self, "<@unnamed>")) {
444 self->context.file = "<@no context>";
445 self->context.line = 0;
446 self->outtype = outtype;
455 self->code_function_def = -1;
456 self->allocated_locals = 0;
462 bool ir_function_set_name(ir_function *self, const char *name)
465 mem_d((void*)self->name);
466 self->name = util_strdup(name);
470 static void ir_function_delete_quick(ir_function *self)
473 mem_d((void*)self->name);
475 for (i = 0; i != vec_size(self->blocks); ++i)
476 ir_block_delete_quick(self->blocks[i]);
477 vec_free(self->blocks);
479 vec_free(self->params);
481 for (i = 0; i != vec_size(self->values); ++i)
482 ir_value_delete(self->values[i]);
483 vec_free(self->values);
485 for (i = 0; i != vec_size(self->locals); ++i)
486 ir_value_delete(self->locals[i]);
487 vec_free(self->locals);
489 /* self->value is deleted by the builder */
494 void ir_function_delete(ir_function *self)
497 mem_d((void*)self->name);
499 for (i = 0; i != vec_size(self->blocks); ++i)
500 ir_block_delete(self->blocks[i]);
501 vec_free(self->blocks);
503 vec_free(self->params);
505 for (i = 0; i != vec_size(self->values); ++i)
506 ir_value_delete(self->values[i]);
507 vec_free(self->values);
509 for (i = 0; i != vec_size(self->locals); ++i)
510 ir_value_delete(self->locals[i]);
511 vec_free(self->locals);
513 /* self->value is deleted by the builder */
518 void ir_function_collect_value(ir_function *self, ir_value *v)
520 vec_push(self->values, v);
523 ir_block* ir_function_create_block(lex_ctx ctx, ir_function *self, const char *label)
525 ir_block* bn = ir_block_new(self, label);
527 vec_push(self->blocks, bn);
531 static bool instr_is_operation(uint16_t op)
533 return ( (op >= INSTR_MUL_F && op <= INSTR_GT) ||
534 (op >= INSTR_LOAD_F && op <= INSTR_LOAD_FNC) ||
535 (op == INSTR_ADDRESS) ||
536 (op >= INSTR_NOT_F && op <= INSTR_NOT_FNC) ||
537 (op >= INSTR_AND && op <= INSTR_BITOR) ||
538 (op >= INSTR_CALL0 && op <= INSTR_CALL8) );
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 ++opts_optimizationcount[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 these */
616 inot->opcode == INSTR_NOT_S)
622 ++opts_optimizationcount[OPTIM_PEEPHOLE];
624 (void)!ir_instr_op(inst, 0, inot->_ops[1], false);
627 for (inotid = 0; inotid < vec_size(tmp->instr); ++inotid) {
628 if (tmp->instr[inotid] == inot)
631 if (inotid >= vec_size(tmp->instr)) {
632 compile_error(inst->context, "sanity-check failed: failed to find instruction to optimize out");
635 vec_remove(tmp->instr, inotid, 1);
636 ir_instr_delete(inot);
637 /* swap ontrue/onfalse */
639 inst->bops[0] = inst->bops[1];
650 bool ir_function_pass_tailrecursion(ir_function *self)
654 for (b = 0; b < vec_size(self->blocks); ++b) {
656 ir_instr *ret, *call, *store = NULL;
657 ir_block *block = self->blocks[b];
659 if (!block->final || vec_size(block->instr) < 2)
662 ret = block->instr[vec_size(block->instr)-1];
663 if (ret->opcode != INSTR_DONE && ret->opcode != INSTR_RETURN)
666 call = block->instr[vec_size(block->instr)-2];
667 if (call->opcode >= INSTR_STORE_F && call->opcode <= INSTR_STORE_FNC) {
668 /* account for the unoptimized
670 * STORE %return, %tmp
674 if (vec_size(block->instr) < 3)
678 call = block->instr[vec_size(block->instr)-3];
681 if (call->opcode < INSTR_CALL0 || call->opcode > INSTR_CALL8)
685 /* optimize out the STORE */
687 ret->_ops[0] == store->_ops[0] &&
688 store->_ops[1] == call->_ops[0])
690 ++opts_optimizationcount[OPTIM_PEEPHOLE];
691 call->_ops[0] = store->_ops[0];
692 vec_remove(block->instr, vec_size(block->instr) - 2, 1);
693 ir_instr_delete(store);
702 funcval = call->_ops[1];
705 if (funcval->vtype != TYPE_FUNCTION || funcval->constval.vfunc != self)
708 /* now we have a CALL and a RET, check if it's a tailcall */
709 if (ret->_ops[0] && call->_ops[0] != ret->_ops[0])
712 ++opts_optimizationcount[OPTIM_TAIL_RECURSION];
713 vec_shrinkby(block->instr, 2);
715 block->final = false; /* open it back up */
717 /* emite parameter-stores */
718 for (p = 0; p < vec_size(call->params); ++p) {
719 /* assert(call->params_count <= self->locals_count); */
720 if (!ir_block_create_store(block, call->context, self->locals[p], call->params[p])) {
721 irerror(call->context, "failed to create tailcall store instruction for parameter %i", (int)p);
725 if (!ir_block_create_jump(block, call->context, self->blocks[0])) {
726 irerror(call->context, "failed to create tailcall jump");
730 ir_instr_delete(call);
731 ir_instr_delete(ret);
737 bool ir_function_finalize(ir_function *self)
744 if (OPTS_OPTIMIZATION(OPTIM_PEEPHOLE)) {
745 if (!ir_function_pass_peephole(self)) {
746 irerror(self->context, "generic optimization pass broke something in `%s`", self->name);
751 if (OPTS_OPTIMIZATION(OPTIM_TAIL_RECURSION)) {
752 if (!ir_function_pass_tailrecursion(self)) {
753 irerror(self->context, "tail-recursion optimization pass broke something in `%s`", self->name);
758 if (!ir_function_naive_phi(self))
761 for (i = 0; i < vec_size(self->locals); ++i) {
762 ir_value *v = self->locals[i];
763 if (v->vtype == TYPE_VECTOR ||
764 (v->vtype == TYPE_FIELD && v->outtype == TYPE_VECTOR))
766 ir_value_vector_member(v, 0);
767 ir_value_vector_member(v, 1);
768 ir_value_vector_member(v, 2);
771 for (i = 0; i < vec_size(self->values); ++i) {
772 ir_value *v = self->values[i];
773 if (v->vtype == TYPE_VECTOR ||
774 (v->vtype == TYPE_FIELD && v->outtype == TYPE_VECTOR))
776 ir_value_vector_member(v, 0);
777 ir_value_vector_member(v, 1);
778 ir_value_vector_member(v, 2);
782 ir_function_enumerate(self);
784 if (!ir_function_calculate_liferanges(self))
786 if (!ir_function_allocate_locals(self))
791 ir_value* ir_function_create_local(ir_function *self, const char *name, int vtype, bool param)
796 vec_size(self->locals) &&
797 self->locals[vec_size(self->locals)-1]->store != store_param) {
798 irerror(self->context, "cannot add parameters after adding locals");
802 ve = ir_value_var(name, (param ? store_param : store_local), vtype);
803 vec_push(self->locals, ve);
807 /***********************************************************************
811 ir_block* ir_block_new(ir_function* owner, const char *name)
814 self = (ir_block*)mem_a(sizeof(*self));
818 memset(self, 0, sizeof(*self));
821 if (name && !ir_block_set_label(self, name)) {
826 self->context.file = "<@no context>";
827 self->context.line = 0;
831 self->entries = NULL;
835 self->is_return = false;
840 self->generated = false;
845 static void ir_block_delete_quick(ir_block* self)
848 if (self->label) mem_d(self->label);
849 for (i = 0; i != vec_size(self->instr); ++i)
850 ir_instr_delete_quick(self->instr[i]);
851 vec_free(self->instr);
852 vec_free(self->entries);
853 vec_free(self->exits);
854 vec_free(self->living);
858 void ir_block_delete(ir_block* self)
861 if (self->label) mem_d(self->label);
862 for (i = 0; i != vec_size(self->instr); ++i)
863 ir_instr_delete(self->instr[i]);
864 vec_free(self->instr);
865 vec_free(self->entries);
866 vec_free(self->exits);
867 vec_free(self->living);
871 bool ir_block_set_label(ir_block *self, const char *name)
874 mem_d((void*)self->label);
875 self->label = util_strdup(name);
876 return !!self->label;
879 /***********************************************************************
883 ir_instr* ir_instr_new(lex_ctx ctx, ir_block* owner, int op)
886 self = (ir_instr*)mem_a(sizeof(*self));
893 self->_ops[0] = NULL;
894 self->_ops[1] = NULL;
895 self->_ops[2] = NULL;
896 self->bops[0] = NULL;
897 self->bops[1] = NULL;
908 static void ir_instr_delete_quick(ir_instr *self)
911 vec_free(self->params);
915 void ir_instr_delete(ir_instr *self)
918 /* The following calls can only delete from
919 * vectors, we still want to delete this instruction
920 * so ignore the return value. Since with the warn_unused_result attribute
921 * gcc doesn't care about an explicit: (void)foo(); to ignore the result,
922 * I have to improvise here and use if(foo());
924 for (i = 0; i < vec_size(self->phi); ++i) {
926 if (vec_ir_instr_find(self->phi[i].value->writes, self, &idx))
927 vec_remove(self->phi[i].value->writes, idx, 1);
928 if (vec_ir_instr_find(self->phi[i].value->reads, self, &idx))
929 vec_remove(self->phi[i].value->reads, idx, 1);
932 for (i = 0; i < vec_size(self->params); ++i) {
934 if (vec_ir_instr_find(self->params[i]->writes, self, &idx))
935 vec_remove(self->params[i]->writes, idx, 1);
936 if (vec_ir_instr_find(self->params[i]->reads, self, &idx))
937 vec_remove(self->params[i]->reads, idx, 1);
939 vec_free(self->params);
940 (void)!ir_instr_op(self, 0, NULL, false);
941 (void)!ir_instr_op(self, 1, NULL, false);
942 (void)!ir_instr_op(self, 2, NULL, false);
946 bool ir_instr_op(ir_instr *self, int op, ir_value *v, bool writing)
948 if (self->_ops[op]) {
950 if (writing && vec_ir_instr_find(self->_ops[op]->writes, self, &idx))
951 vec_remove(self->_ops[op]->writes, idx, 1);
952 else if (vec_ir_instr_find(self->_ops[op]->reads, self, &idx))
953 vec_remove(self->_ops[op]->reads, idx, 1);
957 vec_push(v->writes, self);
959 vec_push(v->reads, self);
965 /***********************************************************************
969 void ir_value_code_setaddr(ir_value *self, int32_t gaddr)
971 self->code.globaladdr = gaddr;
972 if (self->members[0]) self->members[0]->code.globaladdr = gaddr;
973 if (self->members[1]) self->members[1]->code.globaladdr = gaddr;
974 if (self->members[2]) self->members[2]->code.globaladdr = gaddr;
977 int32_t ir_value_code_addr(const ir_value *self)
979 if (self->store == store_return)
980 return OFS_RETURN + self->code.addroffset;
981 return self->code.globaladdr + self->code.addroffset;
984 ir_value* ir_value_var(const char *name, int storetype, int vtype)
987 self = (ir_value*)mem_a(sizeof(*self));
989 self->fieldtype = TYPE_VOID;
990 self->outtype = TYPE_VOID;
991 self->store = storetype;
997 self->hasvalue = false;
998 self->context.file = "<@no context>";
999 self->context.line = 0;
1001 if (name && !ir_value_set_name(self, name)) {
1002 irerror(self->context, "out of memory");
1007 memset(&self->constval, 0, sizeof(self->constval));
1008 memset(&self->code, 0, sizeof(self->code));
1010 self->members[0] = NULL;
1011 self->members[1] = NULL;
1012 self->members[2] = NULL;
1013 self->memberof = NULL;
1015 self->unique_life = false;
1016 self->locked = false;
1017 self->callparam = false;
1023 ir_value* ir_value_vector_member(ir_value *self, unsigned int member)
1031 if (self->members[member])
1032 return self->members[member];
1035 len = strlen(self->name);
1036 name = (char*)mem_a(len + 3);
1037 memcpy(name, self->name, len);
1039 name[len+1] = 'x' + member;
1045 if (self->vtype == TYPE_VECTOR)
1047 m = ir_value_var(name, self->store, TYPE_FLOAT);
1052 m->context = self->context;
1054 self->members[member] = m;
1055 m->code.addroffset = member;
1057 else if (self->vtype == TYPE_FIELD)
1059 if (self->fieldtype != TYPE_VECTOR)
1061 m = ir_value_var(name, self->store, TYPE_FIELD);
1066 m->fieldtype = TYPE_FLOAT;
1067 m->context = self->context;
1069 self->members[member] = m;
1070 m->code.addroffset = member;
1074 irerror(self->context, "invalid member access on %s", self->name);
1082 static GMQCC_INLINE size_t ir_value_sizeof(const ir_value *self)
1084 if (self->vtype == TYPE_FIELD && self->fieldtype == TYPE_VECTOR)
1085 return type_sizeof_[TYPE_VECTOR];
1086 return type_sizeof_[self->vtype];
1089 ir_value* ir_value_out(ir_function *owner, const char *name, int storetype, int vtype)
1091 ir_value *v = ir_value_var(name, storetype, vtype);
1094 ir_function_collect_value(owner, v);
1098 void ir_value_delete(ir_value* self)
1102 mem_d((void*)self->name);
1105 if (self->vtype == TYPE_STRING)
1106 mem_d((void*)self->constval.vstring);
1108 for (i = 0; i < 3; ++i) {
1109 if (self->members[i])
1110 ir_value_delete(self->members[i]);
1112 vec_free(self->reads);
1113 vec_free(self->writes);
1114 vec_free(self->life);
1118 bool ir_value_set_name(ir_value *self, const char *name)
1121 mem_d((void*)self->name);
1122 self->name = util_strdup(name);
1123 return !!self->name;
1126 bool ir_value_set_float(ir_value *self, float f)
1128 if (self->vtype != TYPE_FLOAT)
1130 self->constval.vfloat = f;
1131 self->hasvalue = true;
1135 bool ir_value_set_func(ir_value *self, int f)
1137 if (self->vtype != TYPE_FUNCTION)
1139 self->constval.vint = f;
1140 self->hasvalue = true;
1144 bool ir_value_set_vector(ir_value *self, vector v)
1146 if (self->vtype != TYPE_VECTOR)
1148 self->constval.vvec = v;
1149 self->hasvalue = true;
1153 bool ir_value_set_field(ir_value *self, ir_value *fld)
1155 if (self->vtype != TYPE_FIELD)
1157 self->constval.vpointer = fld;
1158 self->hasvalue = true;
1162 static char *ir_strdup(const char *str)
1165 /* actually dup empty strings */
1166 char *out = (char*)mem_a(1);
1170 return util_strdup(str);
1173 bool ir_value_set_string(ir_value *self, const char *str)
1175 if (self->vtype != TYPE_STRING)
1177 self->constval.vstring = ir_strdup(str);
1178 self->hasvalue = true;
1183 bool ir_value_set_int(ir_value *self, int i)
1185 if (self->vtype != TYPE_INTEGER)
1187 self->constval.vint = i;
1188 self->hasvalue = true;
1193 bool ir_value_lives(ir_value *self, size_t at)
1196 for (i = 0; i < vec_size(self->life); ++i)
1198 ir_life_entry_t *life = &self->life[i];
1199 if (life->start <= at && at <= life->end)
1201 if (life->start > at) /* since it's ordered */
1207 bool ir_value_life_insert(ir_value *self, size_t idx, ir_life_entry_t e)
1210 vec_push(self->life, e);
1211 for (k = vec_size(self->life)-1; k > idx; --k)
1212 self->life[k] = self->life[k-1];
1213 self->life[idx] = e;
1217 bool ir_value_life_merge(ir_value *self, size_t s)
1220 ir_life_entry_t *life = NULL;
1221 ir_life_entry_t *before = NULL;
1222 ir_life_entry_t new_entry;
1224 /* Find the first range >= s */
1225 for (i = 0; i < vec_size(self->life); ++i)
1228 life = &self->life[i];
1229 if (life->start > s)
1232 /* nothing found? append */
1233 if (i == vec_size(self->life)) {
1235 if (life && life->end+1 == s)
1237 /* previous life range can be merged in */
1241 if (life && life->end >= s)
1243 e.start = e.end = s;
1244 vec_push(self->life, e);
1250 if (before->end + 1 == s &&
1251 life->start - 1 == s)
1254 before->end = life->end;
1255 vec_remove(self->life, i, 1);
1258 if (before->end + 1 == s)
1264 /* already contained */
1265 if (before->end >= s)
1269 if (life->start - 1 == s)
1274 /* insert a new entry */
1275 new_entry.start = new_entry.end = s;
1276 return ir_value_life_insert(self, i, new_entry);
1279 bool ir_value_life_merge_into(ir_value *self, const ir_value *other)
1283 if (!vec_size(other->life))
1286 if (!vec_size(self->life)) {
1287 size_t count = vec_size(other->life);
1288 ir_life_entry_t *life = vec_add(self->life, count);
1289 memcpy(life, other->life, count * sizeof(*life));
1294 for (i = 0; i < vec_size(other->life); ++i)
1296 const ir_life_entry_t *life = &other->life[i];
1299 ir_life_entry_t *entry = &self->life[myi];
1301 if (life->end+1 < entry->start)
1303 /* adding an interval before entry */
1304 if (!ir_value_life_insert(self, myi, *life))
1310 if (life->start < entry->start &&
1311 life->end+1 >= entry->start)
1313 /* starts earlier and overlaps */
1314 entry->start = life->start;
1317 if (life->end > entry->end &&
1318 life->start <= entry->end+1)
1320 /* ends later and overlaps */
1321 entry->end = life->end;
1324 /* see if our change combines it with the next ranges */
1325 while (myi+1 < vec_size(self->life) &&
1326 entry->end+1 >= self->life[1+myi].start)
1328 /* overlaps with (myi+1) */
1329 if (entry->end < self->life[1+myi].end)
1330 entry->end = self->life[1+myi].end;
1331 vec_remove(self->life, myi+1, 1);
1332 entry = &self->life[myi];
1335 /* see if we're after the entry */
1336 if (life->start > entry->end)
1339 /* append if we're at the end */
1340 if (myi >= vec_size(self->life)) {
1341 vec_push(self->life, *life);
1344 /* otherweise check the next range */
1353 bool ir_values_overlap(const ir_value *a, const ir_value *b)
1355 /* For any life entry in A see if it overlaps with
1356 * any life entry in B.
1357 * Note that the life entries are orderes, so we can make a
1358 * more efficient algorithm there than naively translating the
1362 ir_life_entry_t *la, *lb, *enda, *endb;
1364 /* first of all, if either has no life range, they cannot clash */
1365 if (!vec_size(a->life) || !vec_size(b->life))
1370 enda = la + vec_size(a->life);
1371 endb = lb + vec_size(b->life);
1374 /* check if the entries overlap, for that,
1375 * both must start before the other one ends.
1377 if (la->start < lb->end &&
1378 lb->start < la->end)
1383 /* entries are ordered
1384 * one entry is earlier than the other
1385 * that earlier entry will be moved forward
1387 if (la->start < lb->start)
1389 /* order: A B, move A forward
1390 * check if we hit the end with A
1395 else /* if (lb->start < la->start) actually <= */
1397 /* order: B A, move B forward
1398 * check if we hit the end with B
1407 /***********************************************************************
1411 static bool ir_check_unreachable(ir_block *self)
1413 /* The IR should never have to deal with unreachable code */
1414 if (!self->final/* || OPTS_FLAG(ALLOW_UNREACHABLE_CODE)*/)
1416 irerror(self->context, "unreachable statement (%s)", self->label);
1420 bool ir_block_create_store_op(ir_block *self, lex_ctx ctx, int op, ir_value *target, ir_value *what)
1423 if (!ir_check_unreachable(self))
1426 if (target->store == store_value &&
1427 (op < INSTR_STOREP_F || op > INSTR_STOREP_FNC))
1429 irerror(self->context, "cannot store to an SSA value");
1430 irerror(self->context, "trying to store: %s <- %s", target->name, what->name);
1431 irerror(self->context, "instruction: %s", asm_instr[op].m);
1435 in = ir_instr_new(ctx, self, op);
1439 if (!ir_instr_op(in, 0, target, (op < INSTR_STOREP_F || op > INSTR_STOREP_FNC)) ||
1440 !ir_instr_op(in, 1, what, false))
1442 ir_instr_delete(in);
1445 vec_push(self->instr, in);
1449 bool ir_block_create_store(ir_block *self, lex_ctx ctx, ir_value *target, ir_value *what)
1453 if (target->vtype == TYPE_VARIANT)
1454 vtype = what->vtype;
1456 vtype = target->vtype;
1459 if (vtype == TYPE_FLOAT && what->vtype == TYPE_INTEGER)
1460 op = INSTR_CONV_ITOF;
1461 else if (vtype == TYPE_INTEGER && what->vtype == TYPE_FLOAT)
1462 op = INSTR_CONV_FTOI;
1464 op = type_store_instr[vtype];
1466 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS)) {
1467 if (op == INSTR_STORE_FLD && what->fieldtype == TYPE_VECTOR)
1471 return ir_block_create_store_op(self, ctx, op, target, what);
1474 bool ir_block_create_storep(ir_block *self, lex_ctx ctx, ir_value *target, ir_value *what)
1479 if (target->vtype != TYPE_POINTER)
1482 /* storing using pointer - target is a pointer, type must be
1483 * inferred from source
1485 vtype = what->vtype;
1487 op = type_storep_instr[vtype];
1488 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS)) {
1489 if (op == INSTR_STOREP_FLD && what->fieldtype == TYPE_VECTOR)
1490 op = INSTR_STOREP_V;
1493 return ir_block_create_store_op(self, ctx, op, target, what);
1496 bool ir_block_create_return(ir_block *self, lex_ctx ctx, ir_value *v)
1499 if (!ir_check_unreachable(self))
1502 self->is_return = true;
1503 in = ir_instr_new(ctx, self, INSTR_RETURN);
1507 if (v && !ir_instr_op(in, 0, v, false)) {
1508 ir_instr_delete(in);
1512 vec_push(self->instr, in);
1516 bool ir_block_create_if(ir_block *self, lex_ctx ctx, ir_value *v,
1517 ir_block *ontrue, ir_block *onfalse)
1520 if (!ir_check_unreachable(self))
1523 /*in = ir_instr_new(ctx, self, (v->vtype == TYPE_STRING ? INSTR_IF_S : INSTR_IF_F));*/
1524 in = ir_instr_new(ctx, self, VINSTR_COND);
1528 if (!ir_instr_op(in, 0, v, false)) {
1529 ir_instr_delete(in);
1533 in->bops[0] = ontrue;
1534 in->bops[1] = onfalse;
1536 vec_push(self->instr, in);
1538 vec_push(self->exits, ontrue);
1539 vec_push(self->exits, onfalse);
1540 vec_push(ontrue->entries, self);
1541 vec_push(onfalse->entries, self);
1545 bool ir_block_create_jump(ir_block *self, lex_ctx ctx, ir_block *to)
1548 if (!ir_check_unreachable(self))
1551 in = ir_instr_new(ctx, self, VINSTR_JUMP);
1556 vec_push(self->instr, in);
1558 vec_push(self->exits, to);
1559 vec_push(to->entries, self);
1563 bool ir_block_create_goto(ir_block *self, lex_ctx ctx, ir_block *to)
1565 self->owner->flags |= IR_FLAG_HAS_GOTO;
1566 return ir_block_create_jump(self, ctx, to);
1569 ir_instr* ir_block_create_phi(ir_block *self, lex_ctx ctx, const char *label, int ot)
1573 if (!ir_check_unreachable(self))
1575 in = ir_instr_new(ctx, self, VINSTR_PHI);
1578 out = ir_value_out(self->owner, label, store_value, ot);
1580 ir_instr_delete(in);
1583 if (!ir_instr_op(in, 0, out, true)) {
1584 ir_instr_delete(in);
1585 ir_value_delete(out);
1588 vec_push(self->instr, in);
1592 ir_value* ir_phi_value(ir_instr *self)
1594 return self->_ops[0];
1597 void ir_phi_add(ir_instr* self, ir_block *b, ir_value *v)
1601 if (!vec_ir_block_find(self->owner->entries, b, NULL)) {
1602 /* Must not be possible to cause this, otherwise the AST
1603 * is doing something wrong.
1605 irerror(self->context, "Invalid entry block for PHI");
1611 vec_push(v->reads, self);
1612 vec_push(self->phi, pe);
1615 /* call related code */
1616 ir_instr* ir_block_create_call(ir_block *self, lex_ctx ctx, const char *label, ir_value *func, bool noreturn)
1620 if (!ir_check_unreachable(self))
1622 in = ir_instr_new(ctx, self, (noreturn ? VINSTR_NRCALL : INSTR_CALL0));
1627 self->is_return = true;
1629 out = ir_value_out(self->owner, label, (func->outtype == TYPE_VOID) ? store_return : store_value, func->outtype);
1631 ir_instr_delete(in);
1634 if (!ir_instr_op(in, 0, out, true) ||
1635 !ir_instr_op(in, 1, func, false))
1637 ir_instr_delete(in);
1638 ir_value_delete(out);
1641 vec_push(self->instr, in);
1644 if (!ir_block_create_return(self, ctx, NULL)) {
1645 compile_error(ctx, "internal error: failed to generate dummy-return instruction");
1646 ir_instr_delete(in);
1654 ir_value* ir_call_value(ir_instr *self)
1656 return self->_ops[0];
1659 void ir_call_param(ir_instr* self, ir_value *v)
1661 vec_push(self->params, v);
1662 vec_push(v->reads, self);
1665 /* binary op related code */
1667 ir_value* ir_block_create_binop(ir_block *self, lex_ctx ctx,
1668 const char *label, int opcode,
1669 ir_value *left, ir_value *right)
1691 case INSTR_SUB_S: /* -- offset of string as float */
1696 case INSTR_BITOR_IF:
1697 case INSTR_BITOR_FI:
1698 case INSTR_BITAND_FI:
1699 case INSTR_BITAND_IF:
1714 case INSTR_BITAND_I:
1717 case INSTR_RSHIFT_I:
1718 case INSTR_LSHIFT_I:
1740 /* boolean operations result in floats */
1741 if (opcode >= INSTR_EQ_F && opcode <= INSTR_GT)
1743 else if (opcode >= INSTR_LE && opcode <= INSTR_GT)
1746 else if (opcode >= INSTR_LE_I && opcode <= INSTR_EQ_FI)
1751 if (ot == TYPE_VOID) {
1752 /* The AST or parser were supposed to check this! */
1756 return ir_block_create_general_instr(self, ctx, label, opcode, left, right, ot);
1759 ir_value* ir_block_create_unary(ir_block *self, lex_ctx ctx,
1760 const char *label, int opcode,
1763 int ot = TYPE_FLOAT;
1775 /* QC doesn't have other unary operations. We expect extensions to fill
1776 * the above list, otherwise we assume out-type = in-type, eg for an
1780 ot = operand->vtype;
1783 if (ot == TYPE_VOID) {
1784 /* The AST or parser were supposed to check this! */
1788 /* let's use the general instruction creator and pass NULL for OPB */
1789 return ir_block_create_general_instr(self, ctx, label, opcode, operand, NULL, ot);
1792 ir_value* ir_block_create_general_instr(ir_block *self, lex_ctx ctx, const char *label,
1793 int op, ir_value *a, ir_value *b, int outype)
1798 out = ir_value_out(self->owner, label, store_value, outype);
1802 instr = ir_instr_new(ctx, self, op);
1804 ir_value_delete(out);
1808 if (!ir_instr_op(instr, 0, out, true) ||
1809 !ir_instr_op(instr, 1, a, false) ||
1810 !ir_instr_op(instr, 2, b, false) )
1815 vec_push(self->instr, instr);
1819 ir_instr_delete(instr);
1820 ir_value_delete(out);
1824 ir_value* ir_block_create_fieldaddress(ir_block *self, lex_ctx ctx, const char *label, ir_value *ent, ir_value *field)
1828 /* Support for various pointer types todo if so desired */
1829 if (ent->vtype != TYPE_ENTITY)
1832 if (field->vtype != TYPE_FIELD)
1835 v = ir_block_create_general_instr(self, ctx, label, INSTR_ADDRESS, ent, field, TYPE_POINTER);
1836 v->fieldtype = field->fieldtype;
1840 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)
1843 if (ent->vtype != TYPE_ENTITY)
1846 /* at some point we could redirect for TYPE_POINTER... but that could lead to carelessness */
1847 if (field->vtype != TYPE_FIELD)
1852 case TYPE_FLOAT: op = INSTR_LOAD_F; break;
1853 case TYPE_VECTOR: op = INSTR_LOAD_V; break;
1854 case TYPE_STRING: op = INSTR_LOAD_S; break;
1855 case TYPE_FIELD: op = INSTR_LOAD_FLD; break;
1856 case TYPE_ENTITY: op = INSTR_LOAD_ENT; break;
1857 case TYPE_FUNCTION: op = INSTR_LOAD_FNC; break;
1859 case TYPE_POINTER: op = INSTR_LOAD_I; break;
1860 case TYPE_INTEGER: op = INSTR_LOAD_I; break;
1863 irerror(self->context, "invalid type for ir_block_create_load_from_ent: %s", type_name[outype]);
1867 return ir_block_create_general_instr(self, ctx, label, op, ent, field, outype);
1870 /* PHI resolving breaks the SSA, and must thus be the last
1871 * step before life-range calculation.
1874 static bool ir_block_naive_phi(ir_block *self);
1875 bool ir_function_naive_phi(ir_function *self)
1879 for (i = 0; i < vec_size(self->blocks); ++i)
1881 if (!ir_block_naive_phi(self->blocks[i]))
1888 static bool ir_naive_phi_emit_store(ir_block *block, size_t iid, ir_value *old, ir_value *what)
1893 /* create a store */
1894 if (!ir_block_create_store(block, old, what))
1897 /* we now move it up */
1898 instr = vec_last(block->instr);
1899 for (i = vec_size(block->instr)-1; i > iid; --i)
1900 block->instr[i] = block->instr[i-1];
1901 block->instr[i] = instr;
1907 static bool ir_block_naive_phi(ir_block *self)
1909 size_t i, p; /*, w;*/
1910 /* FIXME: optionally, create_phi can add the phis
1911 * to a list so we don't need to loop through blocks
1912 * - anyway: "don't optimize YET"
1914 for (i = 0; i < vec_size(self->instr); ++i)
1916 ir_instr *instr = self->instr[i];
1917 if (instr->opcode != VINSTR_PHI)
1920 vec_remove(self->instr, i, 1);
1921 --i; /* NOTE: i+1 below */
1923 for (p = 0; p < vec_size(instr->phi); ++p)
1925 ir_value *v = instr->phi[p].value;
1926 ir_block *b = instr->phi[p].from;
1928 if (v->store == store_value &&
1929 vec_size(v->reads) == 1 &&
1930 vec_size(v->writes) == 1)
1932 /* replace the value */
1933 if (!ir_instr_op(v->writes[0], 0, instr->_ops[0], true))
1938 /* force a move instruction */
1939 ir_instr *prevjump = vec_last(b->instr);
1942 instr->_ops[0]->store = store_global;
1943 if (!ir_block_create_store(b, instr->context, instr->_ops[0], v))
1945 instr->_ops[0]->store = store_value;
1946 vec_push(b->instr, prevjump);
1951 ir_value *v = instr->phi[p].value;
1952 for (w = 0; w < vec_size(v->writes); ++w) {
1955 if (!v->writes[w]->_ops[0])
1958 /* When the write was to a global, we have to emit a mov */
1959 old = v->writes[w]->_ops[0];
1961 /* The original instruction now writes to the PHI target local */
1962 if (v->writes[w]->_ops[0] == v)
1963 v->writes[w]->_ops[0] = instr->_ops[0];
1965 if (old->store != store_value && old->store != store_local && old->store != store_param)
1967 /* If it originally wrote to a global we need to store the value
1970 if (!ir_naive_phi_emit_store(self, i+1, old, v))
1972 if (i+1 < vec_size(self->instr))
1973 instr = self->instr[i+1];
1976 /* In case I forget and access instr later, it'll be NULL
1977 * when it's a problem, to make sure we crash, rather than accessing
1983 /* If it didn't, we can replace all reads by the phi target now. */
1985 for (r = 0; r < vec_size(old->reads); ++r)
1988 ir_instr *ri = old->reads[r];
1989 for (op = 0; op < vec_size(ri->phi); ++op) {
1990 if (ri->phi[op].value == old)
1991 ri->phi[op].value = v;
1993 for (op = 0; op < 3; ++op) {
1994 if (ri->_ops[op] == old)
2002 ir_instr_delete(instr);
2007 /***********************************************************************
2008 *IR Temp allocation code
2009 * Propagating value life ranges by walking through the function backwards
2010 * until no more changes are made.
2011 * In theory this should happen once more than once for every nested loop
2013 * Though this implementation might run an additional time for if nests.
2016 /* Enumerate instructions used by value's life-ranges
2018 static void ir_block_enumerate(ir_block *self, size_t *_eid)
2022 for (i = 0; i < vec_size(self->instr); ++i)
2024 self->instr[i]->eid = eid++;
2029 /* Enumerate blocks and instructions.
2030 * The block-enumeration is unordered!
2031 * We do not really use the block enumreation, however
2032 * the instruction enumeration is important for life-ranges.
2034 void ir_function_enumerate(ir_function *self)
2037 size_t instruction_id = 1;
2038 for (i = 0; i < vec_size(self->blocks); ++i)
2040 self->blocks[i]->eid = i;
2041 self->blocks[i]->run_id = 0;
2042 ir_block_enumerate(self->blocks[i], &instruction_id);
2046 static bool ir_block_life_propagate(ir_block *b, ir_block *prev, bool *changed);
2047 bool ir_function_calculate_liferanges(ir_function *self)
2052 /* parameters live at 0 */
2053 for (i = 0; i < vec_size(self->params); ++i)
2054 ir_value_life_merge(self->locals[i], 0);
2059 for (i = 0; i != vec_size(self->blocks); ++i)
2061 if (self->blocks[i]->is_return)
2063 vec_free(self->blocks[i]->living);
2064 if (!ir_block_life_propagate(self->blocks[i], NULL, &changed))
2069 if (vec_size(self->blocks)) {
2070 ir_block *block = self->blocks[0];
2071 for (i = 0; i < vec_size(block->living); ++i) {
2072 ir_value *v = block->living[i];
2073 if (v->store != store_local)
2075 if (v->vtype == TYPE_VECTOR)
2077 self->flags |= IR_FLAG_HAS_UNINITIALIZED;
2078 /* find the instruction reading from it */
2079 for (s = 0; s < vec_size(v->reads); ++s) {
2080 if (v->reads[s]->eid == v->life[0].end)
2083 if (s < vec_size(v->reads)) {
2084 if (irwarning(v->context, WARN_USED_UNINITIALIZED,
2085 "variable `%s` may be used uninitialized in this function\n"
2088 v->reads[s]->context.file, v->reads[s]->context.line)
2096 ir_value *vec = v->memberof;
2097 for (s = 0; s < vec_size(vec->reads); ++s) {
2098 if (vec->reads[s]->eid == v->life[0].end)
2101 if (s < vec_size(vec->reads)) {
2102 if (irwarning(v->context, WARN_USED_UNINITIALIZED,
2103 "variable `%s` may be used uninitialized in this function\n"
2106 vec->reads[s]->context.file, vec->reads[s]->context.line)
2114 if (irwarning(v->context, WARN_USED_UNINITIALIZED,
2115 "variable `%s` may be used uninitialized in this function", v->name))
2124 /* Local-value allocator
2125 * After finishing creating the liferange of all values used in a function
2126 * we can allocate their global-positions.
2127 * This is the counterpart to register-allocation in register machines.
2134 } function_allocator;
2136 static bool function_allocator_alloc(function_allocator *alloc, const ir_value *var)
2139 size_t vsize = ir_value_sizeof(var);
2141 slot = ir_value_var("reg", store_global, var->vtype);
2145 if (!ir_value_life_merge_into(slot, var))
2148 vec_push(alloc->locals, slot);
2149 vec_push(alloc->sizes, vsize);
2150 vec_push(alloc->unique, var->unique_life);
2155 ir_value_delete(slot);
2159 bool ir_function_allocate_locals(ir_function *self)
2168 function_allocator alloc;
2170 if (!vec_size(self->locals) && !vec_size(self->values))
2173 alloc.locals = NULL;
2175 alloc.positions = NULL;
2176 alloc.unique = NULL;
2178 for (i = 0; i < vec_size(self->locals); ++i)
2180 if (!OPTS_OPTIMIZATION(OPTIM_LOCAL_TEMPS))
2181 self->locals[i]->unique_life = true;
2182 if (!function_allocator_alloc(&alloc, self->locals[i]))
2186 /* Allocate a slot for any value that still exists */
2187 for (i = 0; i < vec_size(self->values); ++i)
2189 v = self->values[i];
2191 if (!vec_size(v->life))
2194 /* CALL optimization:
2195 * If the value is a parameter-temp: 1 write, 1 read from a CALL
2196 * and it's not "locked", write it to the OFS_PARM directly.
2198 if (OPTS_OPTIMIZATION(OPTIM_CALL_STORES) && !v->locked) {
2199 if (vec_size(v->reads) == 1 && vec_size(v->writes) == 1 &&
2200 (v->reads[0]->opcode == VINSTR_NRCALL ||
2201 (v->reads[0]->opcode >= INSTR_CALL0 && v->reads[0]->opcode <= INSTR_CALL8)
2206 ir_instr *call = v->reads[0];
2207 if (!vec_ir_value_find(call->params, v, ¶m)) {
2208 irerror(call->context, "internal error: unlocked parameter %s not found", v->name);
2212 ++opts_optimizationcount[OPTIM_CALL_STORES];
2213 v->callparam = true;
2215 ir_value_code_setaddr(v, OFS_PARM0 + 3*param);
2219 if (vec_size(self->owner->extparam_protos) <= param)
2220 ep = ir_gen_extparam_proto(self->owner);
2222 ep = self->owner->extparam_protos[param];
2223 ir_instr_op(v->writes[0], 0, ep, true);
2224 call->params[param+8] = ep;
2228 if (vec_size(v->writes) == 1 && v->writes[0]->opcode == INSTR_CALL0)
2230 v->store = store_return;
2231 ++opts_optimizationcount[OPTIM_CALL_STORES];
2236 for (a = 0; a < vec_size(alloc.locals); ++a)
2238 /* if it's reserved for a unique liferange: skip */
2239 if (alloc.unique[a])
2242 slot = alloc.locals[a];
2244 /* never resize parameters
2245 * will be required later when overlapping temps + locals
2247 if (a < vec_size(self->params) &&
2248 alloc.sizes[a] < ir_value_sizeof(v))
2253 if (ir_values_overlap(v, slot))
2256 if (!ir_value_life_merge_into(slot, v))
2259 /* adjust size for this slot */
2260 if (alloc.sizes[a] < ir_value_sizeof(v))
2261 alloc.sizes[a] = ir_value_sizeof(v);
2263 self->values[i]->code.local = a;
2266 if (a >= vec_size(alloc.locals)) {
2267 self->values[i]->code.local = vec_size(alloc.locals);
2268 if (!function_allocator_alloc(&alloc, v))
2277 /* Adjust slot positions based on sizes */
2278 vec_push(alloc.positions, 0);
2280 if (vec_size(alloc.sizes))
2281 pos = alloc.positions[0] + alloc.sizes[0];
2284 for (i = 1; i < vec_size(alloc.sizes); ++i)
2286 pos = alloc.positions[i-1] + alloc.sizes[i-1];
2287 vec_push(alloc.positions, pos);
2290 self->allocated_locals = pos + vec_last(alloc.sizes);
2292 /* Locals need to know their new position */
2293 for (i = 0; i < vec_size(self->locals); ++i) {
2294 self->locals[i]->code.local = alloc.positions[i];
2296 /* Take over the actual slot positions on values */
2297 for (i = 0; i < vec_size(self->values); ++i) {
2298 self->values[i]->code.local = alloc.positions[self->values[i]->code.local];
2306 for (i = 0; i < vec_size(alloc.locals); ++i)
2307 ir_value_delete(alloc.locals[i]);
2308 vec_free(alloc.unique);
2309 vec_free(alloc.locals);
2310 vec_free(alloc.sizes);
2311 vec_free(alloc.positions);
2315 /* Get information about which operand
2316 * is read from, or written to.
2318 static void ir_op_read_write(int op, size_t *read, size_t *write)
2338 case INSTR_STOREP_F:
2339 case INSTR_STOREP_V:
2340 case INSTR_STOREP_S:
2341 case INSTR_STOREP_ENT:
2342 case INSTR_STOREP_FLD:
2343 case INSTR_STOREP_FNC:
2354 static bool ir_block_living_add_instr(ir_block *self, size_t eid)
2357 bool changed = false;
2359 for (i = 0; i != vec_size(self->living); ++i)
2361 tempbool = ir_value_life_merge(self->living[i], eid);
2362 changed = changed || tempbool;
2367 static bool ir_block_living_lock(ir_block *self)
2370 bool changed = false;
2371 for (i = 0; i != vec_size(self->living); ++i)
2373 if (!self->living[i]->locked)
2375 self->living[i]->locked = true;
2380 static bool ir_block_life_prop_previous(ir_block* self, ir_block *prev, bool *changed)
2386 /* values which have been read in a previous iteration are now
2387 * in the "living" array even if the previous block doesn't use them.
2388 * So we have to remove whatever does not exist in the previous block.
2389 * They will be re-added on-read, but the liferange merge won't cause
2391 for (i = 0; i < vec_size(self->living); ++i)
2393 if (!vec_ir_value_find(prev->living, self->living[i], NULL)) {
2394 vec_remove(self->living, i, 1);
2400 /* Whatever the previous block still has in its living set
2401 * must now be added to ours as well.
2403 for (i = 0; i < vec_size(prev->living); ++i)
2405 if (vec_ir_value_find(self->living, prev->living[i], NULL))
2407 vec_push(self->living, prev->living[i]);
2409 irerror(self->contextt from prev: %s", self->label, prev->living[i]->_name);
2415 static bool ir_block_life_propagate(ir_block *self, ir_block *prev, bool *changed)
2420 size_t i, o, p, mem;
2421 /* bitmasks which operands are read from or written to */
2423 char dbg_ind[16] = { '#', '0' };
2428 if (!ir_block_life_prop_previous(self, prev, changed))
2432 i = vec_size(self->instr);
2435 instr = self->instr[i];
2437 /* See which operands are read and write operands */
2438 ir_op_read_write(instr->opcode, &read, &write);
2440 if (instr->opcode == INSTR_MUL_VF)
2442 /* the float source will get an additional lifetime */
2443 tempbool = ir_value_life_merge(instr->_ops[2], instr->eid+1);
2444 *changed = *changed || tempbool;
2446 else if (instr->opcode == INSTR_MUL_FV)
2448 /* the float source will get an additional lifetime */
2449 tempbool = ir_value_life_merge(instr->_ops[1], instr->eid+1);
2450 *changed = *changed || tempbool;
2453 /* Go through the 3 main operands
2454 * writes first, then reads
2456 for (o = 0; o < 3; ++o)
2458 if (!instr->_ops[o]) /* no such operand */
2461 value = instr->_ops[o];
2463 /* We only care about locals */
2464 /* we also calculate parameter liferanges so that locals
2465 * can take up parameter slots */
2466 if (value->store != store_value &&
2467 value->store != store_local &&
2468 value->store != store_param)
2471 /* write operands */
2472 /* When we write to a local, we consider it "dead" for the
2473 * remaining upper part of the function, since in SSA a value
2474 * can only be written once (== created)
2479 bool in_living = vec_ir_value_find(self->living, value, &idx);
2482 /* If the value isn't alive it hasn't been read before... */
2483 /* TODO: See if the warning can be emitted during parsing or AST processing
2484 * otherwise have warning printed here.
2485 * IF printing a warning here: include filecontext_t,
2486 * and make sure it's only printed once
2487 * since this function is run multiple times.
2489 /* con_err( "Value only written %s\n", value->name); */
2490 tempbool = ir_value_life_merge(value, instr->eid);
2491 *changed = *changed || tempbool;
2493 /* since 'living' won't contain it
2494 * anymore, merge the value, since
2497 tempbool = ir_value_life_merge(value, instr->eid);
2498 *changed = *changed || tempbool;
2500 vec_remove(self->living, idx, 1);
2502 /* Removing a vector removes all members */
2503 for (mem = 0; mem < 3; ++mem) {
2504 if (value->members[mem] && vec_ir_value_find(self->living, value->members[mem], &idx)) {
2505 tempbool = ir_value_life_merge(value->members[mem], instr->eid);
2506 *changed = *changed || tempbool;
2507 vec_remove(self->living, idx, 1);
2510 /* Removing the last member removes the vector */
2511 if (value->memberof) {
2512 value = value->memberof;
2513 for (mem = 0; mem < 3; ++mem) {
2514 if (value->members[mem] && vec_ir_value_find(self->living, value->members[mem], NULL))
2517 if (mem == 3 && vec_ir_value_find(self->living, value, &idx)) {
2518 tempbool = ir_value_life_merge(value, instr->eid);
2519 *changed = *changed || tempbool;
2520 vec_remove(self->living, idx, 1);
2526 for (o = 0; o < 3; ++o)
2528 if (!instr->_ops[o]) /* no such operand */
2531 value = instr->_ops[o];
2533 /* We only care about locals */
2534 /* we also calculate parameter liferanges so that locals
2535 * can take up parameter slots */
2536 if (value->store != store_value &&
2537 value->store != store_local &&
2538 value->store != store_param)
2544 if (!vec_ir_value_find(self->living, value, NULL))
2545 vec_push(self->living, value);
2546 /* reading adds the full vector */
2547 if (value->memberof && !vec_ir_value_find(self->living, value->memberof, NULL))
2548 vec_push(self->living, value->memberof);
2549 for (mem = 0; mem < 3; ++mem) {
2550 if (value->members[mem] && !vec_ir_value_find(self->living, value->members[mem], NULL))
2551 vec_push(self->living, value->members[mem]);
2555 /* PHI operands are always read operands */
2556 for (p = 0; p < vec_size(instr->phi); ++p)
2558 value = instr->phi[p].value;
2559 if (!vec_ir_value_find(self->living, value, NULL))
2560 vec_push(self->living, value);
2561 /* reading adds the full vector */
2562 if (value->memberof && !vec_ir_value_find(self->living, value->memberof, NULL))
2563 vec_push(self->living, value->memberof);
2564 for (mem = 0; mem < 3; ++mem) {
2565 if (value->members[mem] && !vec_ir_value_find(self->living, value->members[mem], NULL))
2566 vec_push(self->living, value->members[mem]);
2570 /* on a call, all these values must be "locked" */
2571 if (instr->opcode >= INSTR_CALL0 && instr->opcode <= INSTR_CALL8) {
2572 if (ir_block_living_lock(self))
2575 /* call params are read operands too */
2576 for (p = 0; p < vec_size(instr->params); ++p)
2578 value = instr->params[p];
2579 if (!vec_ir_value_find(self->living, value, NULL))
2580 vec_push(self->living, value);
2581 /* reading adds the full vector */
2582 if (value->memberof && !vec_ir_value_find(self->living, value->memberof, NULL))
2583 vec_push(self->living, value->memberof);
2584 for (mem = 0; mem < 3; ++mem) {
2585 if (value->members[mem] && !vec_ir_value_find(self->living, value->members[mem], NULL))
2586 vec_push(self->living, value->members[mem]);
2591 tempbool = ir_block_living_add_instr(self, instr->eid);
2592 /*con_err( "living added values\n");*/
2593 *changed = *changed || tempbool;
2597 if (self->run_id == self->owner->run_id)
2600 self->run_id = self->owner->run_id;
2602 for (i = 0; i < vec_size(self->entries); ++i)
2604 ir_block *entry = self->entries[i];
2605 ir_block_life_propagate(entry, self, changed);
2611 /***********************************************************************
2614 * Since the IR has the convention of putting 'write' operands
2615 * at the beginning, we have to rotate the operands of instructions
2616 * properly in order to generate valid QCVM code.
2618 * Having destinations at a fixed position is more convenient. In QC
2619 * this is *mostly* OPC, but FTE adds at least 2 instructions which
2620 * read from from OPA, and store to OPB rather than OPC. Which is
2621 * partially the reason why the implementation of these instructions
2622 * in darkplaces has been delayed for so long.
2624 * Breaking conventions is annoying...
2626 static bool ir_builder_gen_global(ir_builder *self, ir_value *global, bool islocal, bool defs_only);
2628 static bool gen_global_field(ir_value *global)
2630 if (global->hasvalue)
2632 ir_value *fld = global->constval.vpointer;
2634 irerror(global->context, "Invalid field constant with no field: %s", global->name);
2638 /* copy the field's value */
2639 ir_value_code_setaddr(global, vec_size(code_globals));
2640 vec_push(code_globals, fld->code.fieldaddr);
2641 if (global->fieldtype == TYPE_VECTOR) {
2642 vec_push(code_globals, fld->code.fieldaddr+1);
2643 vec_push(code_globals, fld->code.fieldaddr+2);
2648 ir_value_code_setaddr(global, vec_size(code_globals));
2649 vec_push(code_globals, 0);
2650 if (global->fieldtype == TYPE_VECTOR) {
2651 vec_push(code_globals, 0);
2652 vec_push(code_globals, 0);
2655 if (global->code.globaladdr < 0)
2660 static bool gen_global_pointer(ir_value *global)
2662 if (global->hasvalue)
2664 ir_value *target = global->constval.vpointer;
2666 irerror(global->context, "Invalid pointer constant: %s", global->name);
2667 /* NULL pointers are pointing to the NULL constant, which also
2668 * sits at address 0, but still has an ir_value for itself.
2673 /* Here, relocations ARE possible - in fteqcc-enhanced-qc:
2674 * void() foo; <- proto
2675 * void() *fooptr = &foo;
2676 * void() foo = { code }
2678 if (!target->code.globaladdr) {
2679 /* FIXME: Check for the constant nullptr ir_value!
2680 * because then code.globaladdr being 0 is valid.
2682 irerror(global->context, "FIXME: Relocation support");
2686 ir_value_code_setaddr(global, vec_size(code_globals));
2687 vec_push(code_globals, target->code.globaladdr);
2691 ir_value_code_setaddr(global, vec_size(code_globals));
2692 vec_push(code_globals, 0);
2694 if (global->code.globaladdr < 0)
2699 static bool gen_blocks_recursive(ir_function *func, ir_block *block)
2701 prog_section_statement stmt;
2710 block->generated = true;
2711 block->code_start = vec_size(code_statements);
2712 for (i = 0; i < vec_size(block->instr); ++i)
2714 instr = block->instr[i];
2716 if (instr->opcode == VINSTR_PHI) {
2717 irerror(block->context, "cannot generate virtual instruction (phi)");
2721 if (instr->opcode == VINSTR_JUMP) {
2722 target = instr->bops[0];
2723 /* for uncoditional jumps, if the target hasn't been generated
2724 * yet, we generate them right here.
2726 if (!target->generated) {
2731 /* otherwise we generate a jump instruction */
2732 stmt.opcode = INSTR_GOTO;
2733 stmt.o1.s1 = (target->code_start) - vec_size(code_statements);
2736 if (stmt.o1.s1 != 1)
2737 code_push_statement(&stmt, instr->context.line);
2739 /* no further instructions can be in this block */
2743 if (instr->opcode == VINSTR_COND) {
2744 ontrue = instr->bops[0];
2745 onfalse = instr->bops[1];
2746 /* TODO: have the AST signal which block should
2747 * come first: eg. optimize IFs without ELSE...
2750 stmt.o1.u1 = ir_value_code_addr(instr->_ops[0]);
2754 if (ontrue->generated) {
2755 stmt.opcode = INSTR_IF;
2756 stmt.o2.s1 = (ontrue->code_start) - vec_size(code_statements);
2757 if (stmt.o2.s1 != 1)
2758 code_push_statement(&stmt, instr->context.line);
2760 if (onfalse->generated) {
2761 stmt.opcode = INSTR_IFNOT;
2762 stmt.o2.s1 = (onfalse->code_start) - vec_size(code_statements);
2763 if (stmt.o2.s1 != 1)
2764 code_push_statement(&stmt, instr->context.line);
2766 if (!ontrue->generated) {
2767 if (onfalse->generated) {
2772 if (!onfalse->generated) {
2773 if (ontrue->generated) {
2778 /* neither ontrue nor onfalse exist */
2779 stmt.opcode = INSTR_IFNOT;
2780 if (!instr->likely) {
2781 /* Honor the likelyhood hint */
2782 ir_block *tmp = onfalse;
2783 stmt.opcode = INSTR_IF;
2787 stidx = vec_size(code_statements);
2788 code_push_statement(&stmt, instr->context.line);
2789 /* on false we jump, so add ontrue-path */
2790 if (!gen_blocks_recursive(func, ontrue))
2792 /* fixup the jump address */
2793 code_statements[stidx].o2.s1 = vec_size(code_statements) - stidx;
2794 /* generate onfalse path */
2795 if (onfalse->generated) {
2796 /* fixup the jump address */
2797 code_statements[stidx].o2.s1 = (onfalse->code_start) - (stidx);
2798 if (code_statements[stidx].o2.s1 == 1) {
2799 code_statements[stidx] = code_statements[stidx+1];
2800 if (code_statements[stidx].o1.s1 < 0)
2801 code_statements[stidx].o1.s1++;
2802 code_pop_statement();
2804 stmt.opcode = vec_last(code_statements).opcode;
2805 if (stmt.opcode == INSTR_GOTO ||
2806 stmt.opcode == INSTR_IF ||
2807 stmt.opcode == INSTR_IFNOT ||
2808 stmt.opcode == INSTR_RETURN ||
2809 stmt.opcode == INSTR_DONE)
2811 /* no use jumping from here */
2814 /* may have been generated in the previous recursive call */
2815 stmt.opcode = INSTR_GOTO;
2816 stmt.o1.s1 = (onfalse->code_start) - vec_size(code_statements);
2819 if (stmt.o1.s1 != 1)
2820 code_push_statement(&stmt, instr->context.line);
2823 else if (code_statements[stidx].o2.s1 == 1) {
2824 code_statements[stidx] = code_statements[stidx+1];
2825 if (code_statements[stidx].o1.s1 < 0)
2826 code_statements[stidx].o1.s1++;
2827 code_pop_statement();
2829 /* if not, generate now */
2834 if ( (instr->opcode >= INSTR_CALL0 && instr->opcode <= INSTR_CALL8)
2835 || instr->opcode == VINSTR_NRCALL)
2840 first = vec_size(instr->params);
2843 for (p = 0; p < first; ++p)
2845 ir_value *param = instr->params[p];
2846 if (param->callparam)
2849 stmt.opcode = INSTR_STORE_F;
2852 if (param->vtype == TYPE_FIELD)
2853 stmt.opcode = field_store_instr[param->fieldtype];
2855 stmt.opcode = type_store_instr[param->vtype];
2856 stmt.o1.u1 = ir_value_code_addr(param);
2857 stmt.o2.u1 = OFS_PARM0 + 3 * p;
2858 code_push_statement(&stmt, instr->context.line);
2860 /* Now handle extparams */
2861 first = vec_size(instr->params);
2862 for (; p < first; ++p)
2864 ir_builder *ir = func->owner;
2865 ir_value *param = instr->params[p];
2866 ir_value *targetparam;
2868 if (param->callparam)
2871 if (p-8 >= vec_size(ir->extparams))
2872 ir_gen_extparam(ir);
2874 targetparam = ir->extparams[p-8];
2876 stmt.opcode = INSTR_STORE_F;
2879 if (param->vtype == TYPE_FIELD)
2880 stmt.opcode = field_store_instr[param->fieldtype];
2882 stmt.opcode = type_store_instr[param->vtype];
2883 stmt.o1.u1 = ir_value_code_addr(param);
2884 stmt.o2.u1 = ir_value_code_addr(targetparam);
2885 code_push_statement(&stmt, instr->context.line);
2888 stmt.opcode = INSTR_CALL0 + vec_size(instr->params);
2889 if (stmt.opcode > INSTR_CALL8)
2890 stmt.opcode = INSTR_CALL8;
2891 stmt.o1.u1 = ir_value_code_addr(instr->_ops[1]);
2894 code_push_statement(&stmt, instr->context.line);
2896 retvalue = instr->_ops[0];
2897 if (retvalue && retvalue->store != store_return &&
2898 (retvalue->store == store_global || vec_size(retvalue->life)))
2900 /* not to be kept in OFS_RETURN */
2901 if (retvalue->vtype == TYPE_FIELD && OPTS_FLAG(ADJUST_VECTOR_FIELDS))
2902 stmt.opcode = field_store_instr[retvalue->fieldtype];
2904 stmt.opcode = type_store_instr[retvalue->vtype];
2905 stmt.o1.u1 = OFS_RETURN;
2906 stmt.o2.u1 = ir_value_code_addr(retvalue);
2908 code_push_statement(&stmt, instr->context.line);
2913 if (instr->opcode == INSTR_STATE) {
2914 irerror(block->context, "TODO: state instruction");
2918 stmt.opcode = instr->opcode;
2923 /* This is the general order of operands */
2925 stmt.o3.u1 = ir_value_code_addr(instr->_ops[0]);
2928 stmt.o1.u1 = ir_value_code_addr(instr->_ops[1]);
2931 stmt.o2.u1 = ir_value_code_addr(instr->_ops[2]);
2933 if (stmt.opcode == INSTR_RETURN || stmt.opcode == INSTR_DONE)
2935 stmt.o1.u1 = stmt.o3.u1;
2938 else if ((stmt.opcode >= INSTR_STORE_F &&
2939 stmt.opcode <= INSTR_STORE_FNC) ||
2940 (stmt.opcode >= INSTR_STOREP_F &&
2941 stmt.opcode <= INSTR_STOREP_FNC))
2943 /* 2-operand instructions with A -> B */
2944 stmt.o2.u1 = stmt.o3.u1;
2947 /* tiny optimization, don't output
2950 if (stmt.o2.u1 == stmt.o1.u1 &&
2951 OPTS_OPTIMIZATION(OPTIM_PEEPHOLE))
2953 ++opts_optimizationcount[OPTIM_PEEPHOLE];
2958 code_push_statement(&stmt, instr->context.line);
2963 static bool gen_function_code(ir_function *self)
2966 prog_section_statement stmt, *retst;
2968 /* Starting from entry point, we generate blocks "as they come"
2969 * for now. Dead blocks will not be translated obviously.
2971 if (!vec_size(self->blocks)) {
2972 irerror(self->context, "Function '%s' declared without body.", self->name);
2976 block = self->blocks[0];
2977 if (block->generated)
2980 if (!gen_blocks_recursive(self, block)) {
2981 irerror(self->context, "failed to generate blocks for '%s'", self->name);
2985 /* code_write and qcvm -disasm need to know that the function ends here */
2986 retst = &vec_last(code_statements);
2987 if (OPTS_OPTIMIZATION(OPTIM_VOID_RETURN) &&
2988 self->outtype == TYPE_VOID &&
2989 retst->opcode == INSTR_RETURN &&
2990 !retst->o1.u1 && !retst->o2.u1 && !retst->o3.u1)
2992 retst->opcode = INSTR_DONE;
2993 ++opts_optimizationcount[OPTIM_VOID_RETURN];
2995 stmt.opcode = INSTR_DONE;
2999 code_push_statement(&stmt, vec_last(code_linenums));
3004 static qcint ir_builder_filestring(ir_builder *ir, const char *filename)
3006 /* NOTE: filename pointers are copied, we never strdup them,
3007 * thus we can use pointer-comparison to find the string.
3012 for (i = 0; i < vec_size(ir->filenames); ++i) {
3013 if (ir->filenames[i] == filename)
3014 return ir->filestrings[i];
3017 str = code_genstring(filename);
3018 vec_push(ir->filenames, filename);
3019 vec_push(ir->filestrings, str);
3023 static bool gen_global_function(ir_builder *ir, ir_value *global)
3025 prog_section_function fun;
3030 if (!global->hasvalue || (!global->constval.vfunc))
3032 irerror(global->context, "Invalid state of function-global: not constant: %s", global->name);
3036 irfun = global->constval.vfunc;
3038 fun.name = global->code.name;
3039 fun.file = ir_builder_filestring(ir, global->context.file);
3040 fun.profile = 0; /* always 0 */
3041 fun.nargs = vec_size(irfun->params);
3045 for (i = 0;i < 8; ++i) {
3046 if ((int32_t)i >= fun.nargs)
3049 fun.argsize[i] = type_sizeof_[irfun->params[i]];
3053 fun.locals = irfun->allocated_locals;
3056 fun.entry = irfun->builtin+1;
3058 irfun->code_function_def = vec_size(code_functions);
3059 fun.entry = vec_size(code_statements);
3062 vec_push(code_functions, fun);
3066 static ir_value* ir_gen_extparam_proto(ir_builder *ir)
3071 snprintf(name, sizeof(name), "EXTPARM#%i", (int)(vec_size(ir->extparam_protos)+8));
3072 global = ir_value_var(name, store_global, TYPE_VECTOR);
3074 vec_push(ir->extparam_protos, global);
3078 static void ir_gen_extparam(ir_builder *ir)
3080 prog_section_def def;
3083 if (vec_size(ir->extparam_protos) < vec_size(ir->extparams)+1)
3084 global = ir_gen_extparam_proto(ir);
3086 global = ir->extparam_protos[vec_size(ir->extparams)];
3088 def.name = code_genstring(global->name);
3089 def.type = TYPE_VECTOR;
3090 def.offset = vec_size(code_globals);
3092 vec_push(code_defs, def);
3093 ir_value_code_setaddr(global, def.offset);
3094 vec_push(code_globals, 0);
3095 vec_push(code_globals, 0);
3096 vec_push(code_globals, 0);
3098 vec_push(ir->extparams, global);
3101 static bool gen_function_extparam_copy(ir_function *self)
3103 size_t i, ext, numparams;
3105 ir_builder *ir = self->owner;
3107 prog_section_statement stmt;
3109 numparams = vec_size(self->params);
3113 stmt.opcode = INSTR_STORE_F;
3115 for (i = 8; i < numparams; ++i) {
3117 if (ext >= vec_size(ir->extparams))
3118 ir_gen_extparam(ir);
3120 ep = ir->extparams[ext];
3122 stmt.opcode = type_store_instr[self->locals[i]->vtype];
3123 if (self->locals[i]->vtype == TYPE_FIELD &&
3124 self->locals[i]->fieldtype == TYPE_VECTOR)
3126 stmt.opcode = INSTR_STORE_V;
3128 stmt.o1.u1 = ir_value_code_addr(ep);
3129 stmt.o2.u1 = ir_value_code_addr(self->locals[i]);
3130 code_push_statement(&stmt, self->context.line);
3136 static bool gen_function_locals(ir_builder *ir, ir_value *global)
3138 prog_section_function *def;
3141 uint32_t firstlocal;
3143 irfun = global->constval.vfunc;
3144 def = code_functions + irfun->code_function_def;
3146 if (opts.g || !OPTS_OPTIMIZATION(OPTIM_OVERLAP_LOCALS) || (irfun->flags & IR_FLAG_MASK_NO_OVERLAP))
3147 firstlocal = def->firstlocal = vec_size(code_globals);
3149 firstlocal = def->firstlocal = ir->first_common_local;
3150 ++opts_optimizationcount[OPTIM_OVERLAP_LOCALS];
3153 for (i = vec_size(code_globals); i < firstlocal + irfun->allocated_locals; ++i)
3154 vec_push(code_globals, 0);
3155 for (i = 0; i < vec_size(irfun->locals); ++i) {
3156 ir_value_code_setaddr(irfun->locals[i], firstlocal + irfun->locals[i]->code.local);
3157 if (!ir_builder_gen_global(ir, irfun->locals[i], true, true)) {
3158 irerror(irfun->locals[i]->context, "failed to generate local %s", irfun->locals[i]->name);
3162 for (i = 0; i < vec_size(irfun->values); ++i)
3164 ir_value *v = irfun->values[i];
3167 ir_value_code_setaddr(v, firstlocal + v->code.local);
3172 static bool gen_global_function_code(ir_builder *ir, ir_value *global)
3174 prog_section_function *fundef;
3179 irfun = global->constval.vfunc;
3181 if (global->cvq == CV_NONE) {
3182 irwarning(global->context, WARN_IMPLICIT_FUNCTION_POINTER,
3183 "function `%s` has no body and in QC implicitly becomes a function-pointer", global->name);
3185 /* this was a function pointer, don't generate code for those */
3192 if (irfun->code_function_def < 0) {
3193 irerror(irfun->context, "`%s`: IR global wasn't generated, failed to access function-def", irfun->name);
3196 fundef = &code_functions[irfun->code_function_def];
3198 fundef->entry = vec_size(code_statements);
3199 if (!gen_function_locals(ir, global)) {
3200 irerror(irfun->context, "Failed to generate locals for function %s", irfun->name);
3203 if (!gen_function_extparam_copy(irfun)) {
3204 irerror(irfun->context, "Failed to generate extparam-copy code for function %s", irfun->name);
3207 if (!gen_function_code(irfun)) {
3208 irerror(irfun->context, "Failed to generate code for function %s", irfun->name);
3214 static void gen_vector_defs(prog_section_def def, const char *name)
3219 if (!name || name[0] == '#' || OPTS_FLAG(SINGLE_VECTOR_DEFS))
3222 def.type = TYPE_FLOAT;
3226 component = (char*)mem_a(len+3);
3227 memcpy(component, name, len);
3229 component[len-0] = 0;
3230 component[len-2] = '_';
3232 component[len-1] = 'x';
3234 for (i = 0; i < 3; ++i) {
3235 def.name = code_genstring(component);
3236 vec_push(code_defs, def);
3242 static void gen_vector_fields(prog_section_field fld, const char *name)
3247 if (!name || OPTS_FLAG(SINGLE_VECTOR_DEFS))
3250 fld.type = TYPE_FLOAT;
3254 component = (char*)mem_a(len+3);
3255 memcpy(component, name, len);
3257 component[len-0] = 0;
3258 component[len-2] = '_';
3260 component[len-1] = 'x';
3262 for (i = 0; i < 3; ++i) {
3263 fld.name = code_genstring(component);
3264 vec_push(code_fields, fld);
3270 static bool ir_builder_gen_global(ir_builder *self, ir_value *global, bool islocal, bool defs_only)
3274 prog_section_def def;
3275 bool pushdef = false;
3277 if (opts.g || !islocal)
3280 def.type = global->vtype;
3281 def.offset = vec_size(code_globals);
3283 if (OPTS_OPTIMIZATION(OPTIM_STRIP_CONSTANT_NAMES) &&
3284 (global->name[0] == '#' || global->cvq == CV_CONST))
3289 if (pushdef && global->name) {
3290 if (global->name[0] == '#') {
3291 if (!self->str_immediate)
3292 self->str_immediate = code_genstring("IMMEDIATE");
3293 def.name = global->code.name = self->str_immediate;
3296 def.name = global->code.name = code_genstring(global->name);
3301 def.offset = ir_value_code_addr(global);
3302 vec_push(code_defs, def);
3303 if (global->vtype == TYPE_VECTOR)
3304 gen_vector_defs(def, global->name);
3305 else if (global->vtype == TYPE_FIELD && global->fieldtype == TYPE_VECTOR)
3306 gen_vector_defs(def, global->name);
3313 switch (global->vtype)
3316 if (!strcmp(global->name, "end_sys_globals")) {
3317 /* TODO: remember this point... all the defs before this one
3318 * should be checksummed and added to progdefs.h when we generate it.
3321 else if (!strcmp(global->name, "end_sys_fields")) {
3322 /* TODO: same as above but for entity-fields rather than globsl
3326 irwarning(global->context, WARN_VOID_VARIABLES, "unrecognized variable of type void `%s`",
3328 /* I'd argue setting it to 0 is sufficient, but maybe some depend on knowing how far
3329 * the system fields actually go? Though the engine knows this anyway...
3330 * Maybe this could be an -foption
3331 * fteqcc creates data for end_sys_* - of size 1, so let's do the same
3333 ir_value_code_setaddr(global, vec_size(code_globals));
3334 vec_push(code_globals, 0);
3336 if (pushdef) vec_push(code_defs, def);
3339 if (pushdef) vec_push(code_defs, def);
3340 return gen_global_pointer(global);
3343 vec_push(code_defs, def);
3344 if (global->fieldtype == TYPE_VECTOR)
3345 gen_vector_defs(def, global->name);
3347 return gen_global_field(global);
3352 ir_value_code_setaddr(global, vec_size(code_globals));
3353 if (global->hasvalue) {
3354 iptr = (int32_t*)&global->constval.ivec[0];
3355 vec_push(code_globals, *iptr);
3357 vec_push(code_globals, 0);
3359 if (!islocal && global->cvq != CV_CONST)
3360 def.type |= DEF_SAVEGLOBAL;
3361 if (pushdef) vec_push(code_defs, def);
3363 return global->code.globaladdr >= 0;
3367 ir_value_code_setaddr(global, vec_size(code_globals));
3368 if (global->hasvalue) {
3369 vec_push(code_globals, code_genstring(global->constval.vstring));
3371 vec_push(code_globals, 0);
3373 if (!islocal && global->cvq != CV_CONST)
3374 def.type |= DEF_SAVEGLOBAL;
3375 if (pushdef) vec_push(code_defs, def);
3376 return global->code.globaladdr >= 0;
3381 ir_value_code_setaddr(global, vec_size(code_globals));
3382 if (global->hasvalue) {
3383 iptr = (int32_t*)&global->constval.ivec[0];
3384 vec_push(code_globals, iptr[0]);
3385 if (global->code.globaladdr < 0)
3387 for (d = 1; d < type_sizeof_[global->vtype]; ++d) {
3388 vec_push(code_globals, iptr[d]);
3391 vec_push(code_globals, 0);
3392 if (global->code.globaladdr < 0)
3394 for (d = 1; d < type_sizeof_[global->vtype]; ++d) {
3395 vec_push(code_globals, 0);
3398 if (!islocal && global->cvq != CV_CONST)
3399 def.type |= DEF_SAVEGLOBAL;
3402 vec_push(code_defs, def);
3403 def.type &= ~DEF_SAVEGLOBAL;
3404 gen_vector_defs(def, global->name);
3406 return global->code.globaladdr >= 0;
3409 ir_value_code_setaddr(global, vec_size(code_globals));
3410 if (!global->hasvalue) {
3411 vec_push(code_globals, 0);
3412 if (global->code.globaladdr < 0)
3415 vec_push(code_globals, vec_size(code_functions));
3416 if (!gen_global_function(self, global))
3419 if (!islocal && global->cvq != CV_CONST)
3420 def.type |= DEF_SAVEGLOBAL;
3421 if (pushdef) vec_push(code_defs, def);
3424 /* assume biggest type */
3425 ir_value_code_setaddr(global, vec_size(code_globals));
3426 vec_push(code_globals, 0);
3427 for (i = 1; i < type_sizeof_[TYPE_VARIANT]; ++i)
3428 vec_push(code_globals, 0);
3431 /* refuse to create 'void' type or any other fancy business. */
3432 irerror(global->context, "Invalid type for global variable `%s`: %s",
3433 global->name, type_name[global->vtype]);
3438 static void ir_builder_prepare_field(ir_value *field)
3440 field->code.fieldaddr = code_alloc_field(type_sizeof_[field->fieldtype]);
3443 static bool ir_builder_gen_field(ir_builder *self, ir_value *field)
3445 prog_section_def def;
3446 prog_section_field fld;
3450 def.type = (uint16_t)field->vtype;
3451 def.offset = (uint16_t)vec_size(code_globals);
3453 /* create a global named the same as the field */
3454 if (opts.standard == COMPILER_GMQCC) {
3455 /* in our standard, the global gets a dot prefix */
3456 size_t len = strlen(field->name);
3459 /* we really don't want to have to allocate this, and 1024
3460 * bytes is more than enough for a variable/field name
3462 if (len+2 >= sizeof(name)) {
3463 irerror(field->context, "invalid field name size: %u", (unsigned int)len);
3468 memcpy(name+1, field->name, len); /* no strncpy - we used strlen above */
3471 def.name = code_genstring(name);
3472 fld.name = def.name + 1; /* we reuse that string table entry */
3474 /* in plain QC, there cannot be a global with the same name,
3475 * and so we also name the global the same.
3476 * FIXME: fteqcc should create a global as well
3477 * check if it actually uses the same name. Probably does
3479 def.name = code_genstring(field->name);
3480 fld.name = def.name;
3483 field->code.name = def.name;
3485 vec_push(code_defs, def);
3487 fld.type = field->fieldtype;
3489 if (fld.type == TYPE_VOID) {
3490 irerror(field->context, "field is missing a type: %s - don't know its size", field->name);
3494 fld.offset = field->code.fieldaddr;
3496 vec_push(code_fields, fld);
3498 ir_value_code_setaddr(field, vec_size(code_globals));
3499 vec_push(code_globals, fld.offset);
3500 if (fld.type == TYPE_VECTOR) {
3501 vec_push(code_globals, fld.offset+1);
3502 vec_push(code_globals, fld.offset+2);
3505 if (field->fieldtype == TYPE_VECTOR) {
3506 gen_vector_defs(def, field->name);
3507 gen_vector_fields(fld, field->name);
3510 return field->code.globaladdr >= 0;
3513 bool ir_builder_generate(ir_builder *self, const char *filename)
3515 prog_section_statement stmt;
3517 char *lnofile = NULL;
3521 for (i = 0; i < vec_size(self->fields); ++i)
3523 ir_builder_prepare_field(self->fields[i]);
3526 for (i = 0; i < vec_size(self->globals); ++i)
3528 if (!ir_builder_gen_global(self, self->globals[i], false, false)) {
3531 if (self->globals[i]->vtype == TYPE_FUNCTION) {
3532 ir_function *func = self->globals[i]->constval.vfunc;
3533 if (func && self->max_locals < func->allocated_locals &&
3534 !(func->flags & IR_FLAG_MASK_NO_OVERLAP))
3536 self->max_locals = func->allocated_locals;
3541 for (i = 0; i < vec_size(self->fields); ++i)
3543 if (!ir_builder_gen_field(self, self->fields[i])) {
3548 /* generate common locals */
3549 self->first_common_local = vec_size(code_globals);
3550 for (i = 0; i < self->max_locals; ++i) {
3551 vec_push(code_globals, 0);
3554 /* generate function code */
3555 for (i = 0; i < vec_size(self->globals); ++i)
3557 if (self->globals[i]->vtype == TYPE_FUNCTION) {
3558 if (!gen_global_function_code(self, self->globals[i])) {
3564 if (vec_size(code_globals) >= 65536) {
3565 irerror(vec_last(self->globals)->context, "This progs file would require more globals than the metadata can handle. Bailing out.");
3569 /* DP errors if the last instruction is not an INSTR_DONE. */
3570 if (vec_last(code_statements).opcode != INSTR_DONE)
3572 stmt.opcode = INSTR_DONE;
3576 code_push_statement(&stmt, vec_last(code_linenums));
3582 if (vec_size(code_statements) != vec_size(code_linenums)) {
3583 con_err("Linecounter wrong: %lu != %lu\n",
3584 (unsigned long)vec_size(code_statements),
3585 (unsigned long)vec_size(code_linenums));
3586 } else if (OPTS_FLAG(LNO)) {
3588 size_t filelen = strlen(filename);
3590 memcpy(vec_add(lnofile, filelen+1), filename, filelen+1);
3591 dot = strrchr(lnofile, '.');
3595 vec_shrinkto(lnofile, dot - lnofile);
3597 memcpy(vec_add(lnofile, 5), ".lno", 5);
3602 con_out("writing '%s' and '%s'...\n", filename, lnofile);
3604 con_out("writing '%s'\n", filename);
3606 if (!code_write(filename, lnofile)) {
3614 /***********************************************************************
3615 *IR DEBUG Dump functions...
3618 #define IND_BUFSZ 1024
3621 # define strncat(dst, src, sz) strncat_s(dst, sz, src, _TRUNCATE)
3624 const char *qc_opname(int op)
3626 if (op < 0) return "<INVALID>";
3627 if (op < (int)( sizeof(asm_instr) / sizeof(asm_instr[0]) ))
3628 return asm_instr[op].m;
3630 case VINSTR_PHI: return "PHI";
3631 case VINSTR_JUMP: return "JUMP";
3632 case VINSTR_COND: return "COND";
3633 default: return "<UNK>";
3637 void ir_builder_dump(ir_builder *b, int (*oprintf)(const char*, ...))
3640 char indent[IND_BUFSZ];
3644 oprintf("module %s\n", b->name);
3645 for (i = 0; i < vec_size(b->globals); ++i)
3648 if (b->globals[i]->hasvalue)
3649 oprintf("%s = ", b->globals[i]->name);
3650 ir_value_dump(b->globals[i], oprintf);
3653 for (i = 0; i < vec_size(b->functions); ++i)
3654 ir_function_dump(b->functions[i], indent, oprintf);
3655 oprintf("endmodule %s\n", b->name);
3658 void ir_function_dump(ir_function *f, char *ind,
3659 int (*oprintf)(const char*, ...))
3662 if (f->builtin != 0) {
3663 oprintf("%sfunction %s = builtin %i\n", ind, f->name, -f->builtin);
3666 oprintf("%sfunction %s\n", ind, f->name);
3667 strncat(ind, "\t", IND_BUFSZ);
3668 if (vec_size(f->locals))
3670 oprintf("%s%i locals:\n", ind, (int)vec_size(f->locals));
3671 for (i = 0; i < vec_size(f->locals); ++i) {
3672 oprintf("%s\t", ind);
3673 ir_value_dump(f->locals[i], oprintf);
3677 oprintf("%sliferanges:\n", ind);
3678 for (i = 0; i < vec_size(f->locals); ++i) {
3680 ir_value *v = f->locals[i];
3681 oprintf("%s\t%s: %s@%i ", ind, v->name, (v->unique_life ? "unique " : ""), (int)v->code.local);
3682 for (l = 0; l < vec_size(v->life); ++l) {
3683 oprintf("[%i,%i] ", v->life[l].start, v->life[l].end);
3686 for (m = 0; m < 3; ++m) {
3687 ir_value *vm = v->members[m];
3690 oprintf("%s\t%s: %s@%i ", ind, vm->name, (vm->unique_life ? "unique " : ""), (int)vm->code.local);
3691 for (l = 0; l < vec_size(vm->life); ++l) {
3692 oprintf("[%i,%i] ", vm->life[l].start, vm->life[l].end);
3697 for (i = 0; i < vec_size(f->values); ++i) {
3699 ir_value *v = f->values[i];
3700 oprintf("%s\t%s: @%i ", ind, v->name, (int)v->code.local);
3701 for (l = 0; l < vec_size(v->life); ++l) {
3702 oprintf("[%i,%i] ", v->life[l].start, v->life[l].end);
3706 if (vec_size(f->blocks))
3708 oprintf("%slife passes (check): %i\n", ind, (int)f->run_id);
3709 for (i = 0; i < vec_size(f->blocks); ++i) {
3710 if (f->blocks[i]->run_id != f->run_id) {
3711 oprintf("%slife pass check fail! %i != %i\n", ind, (int)f->blocks[i]->run_id, (int)f->run_id);
3713 ir_block_dump(f->blocks[i], ind, oprintf);
3717 ind[strlen(ind)-1] = 0;
3718 oprintf("%sendfunction %s\n", ind, f->name);
3721 void ir_block_dump(ir_block* b, char *ind,
3722 int (*oprintf)(const char*, ...))
3725 oprintf("%s:%s\n", ind, b->label);
3726 strncat(ind, "\t", IND_BUFSZ);
3728 for (i = 0; i < vec_size(b->instr); ++i)
3729 ir_instr_dump(b->instr[i], ind, oprintf);
3730 ind[strlen(ind)-1] = 0;
3733 void dump_phi(ir_instr *in, int (*oprintf)(const char*, ...))
3736 oprintf("%s <- phi ", in->_ops[0]->name);
3737 for (i = 0; i < vec_size(in->phi); ++i)
3739 oprintf("([%s] : %s) ", in->phi[i].from->label,
3740 in->phi[i].value->name);
3745 void ir_instr_dump(ir_instr *in, char *ind,
3746 int (*oprintf)(const char*, ...))
3749 const char *comma = NULL;
3751 oprintf("%s (%i) ", ind, (int)in->eid);
3753 if (in->opcode == VINSTR_PHI) {
3754 dump_phi(in, oprintf);
3758 strncat(ind, "\t", IND_BUFSZ);
3760 if (in->_ops[0] && (in->_ops[1] || in->_ops[2])) {
3761 ir_value_dump(in->_ops[0], oprintf);
3762 if (in->_ops[1] || in->_ops[2])
3765 if (in->opcode == INSTR_CALL0 || in->opcode == VINSTR_NRCALL) {
3766 oprintf("CALL%i\t", vec_size(in->params));
3768 oprintf("%s\t", qc_opname(in->opcode));
3770 if (in->_ops[0] && !(in->_ops[1] || in->_ops[2])) {
3771 ir_value_dump(in->_ops[0], oprintf);
3776 for (i = 1; i != 3; ++i) {
3780 ir_value_dump(in->_ops[i], oprintf);
3788 oprintf("[%s]", in->bops[0]->label);
3792 oprintf("%s[%s]", comma, in->bops[1]->label);
3793 if (vec_size(in->params)) {
3794 oprintf("\tparams: ");
3795 for (i = 0; i != vec_size(in->params); ++i) {
3796 oprintf("%s, ", in->params[i]->name);
3800 ind[strlen(ind)-1] = 0;
3803 void ir_value_dump_string(const char *str, int (*oprintf)(const char*, ...))
3806 for (; *str; ++str) {
3808 case '\n': oprintf("\\n"); break;
3809 case '\r': oprintf("\\r"); break;
3810 case '\t': oprintf("\\t"); break;
3811 case '\v': oprintf("\\v"); break;
3812 case '\f': oprintf("\\f"); break;
3813 case '\b': oprintf("\\b"); break;
3814 case '\a': oprintf("\\a"); break;
3815 case '\\': oprintf("\\\\"); break;
3816 case '"': oprintf("\\\""); break;
3817 default: oprintf("%c", *str); break;
3823 void ir_value_dump(ir_value* v, int (*oprintf)(const char*, ...))
3832 oprintf("fn:%s", v->name);
3835 oprintf("%g", v->constval.vfloat);
3838 oprintf("'%g %g %g'",
3841 v->constval.vvec.z);
3844 oprintf("(entity)");
3847 ir_value_dump_string(v->constval.vstring, oprintf);
3851 oprintf("%i", v->constval.vint);
3856 v->constval.vpointer->name);
3860 oprintf("%s", v->name);
3864 void ir_value_dump_life(const ir_value *self, int (*oprintf)(const char*,...))
3867 oprintf("Life of %12s:", self->name);
3868 for (i = 0; i < vec_size(self->life); ++i)
3870 oprintf(" + [%i, %i]\n", self->life[i].start, self->life[i].end);