2 * Copyright (C) 2012, 2013
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] = {
51 size_t type_sizeof_[TYPE_COUNT] = {
58 1, /* TYPE_FUNCTION */
69 uint16_t type_store_instr[TYPE_COUNT] = {
70 INSTR_STORE_F, /* should use I when having integer support */
77 INSTR_STORE_ENT, /* should use I */
79 INSTR_STORE_I, /* integer type */
84 INSTR_STORE_V, /* variant, should never be accessed */
86 VINSTR_END, /* struct */
87 VINSTR_END, /* union */
88 VINSTR_END, /* array */
90 VINSTR_END, /* noexpr */
93 uint16_t field_store_instr[TYPE_COUNT] = {
103 INSTR_STORE_FLD, /* integer type */
108 INSTR_STORE_V, /* variant, should never be accessed */
110 VINSTR_END, /* struct */
111 VINSTR_END, /* union */
112 VINSTR_END, /* array */
113 VINSTR_END, /* nil */
114 VINSTR_END, /* noexpr */
117 uint16_t type_storep_instr[TYPE_COUNT] = {
118 INSTR_STOREP_F, /* should use I when having integer support */
125 INSTR_STOREP_ENT, /* should use I */
127 INSTR_STOREP_ENT, /* integer type */
132 INSTR_STOREP_V, /* variant, should never be accessed */
134 VINSTR_END, /* struct */
135 VINSTR_END, /* union */
136 VINSTR_END, /* array */
137 VINSTR_END, /* nil */
138 VINSTR_END, /* noexpr */
141 uint16_t type_eq_instr[TYPE_COUNT] = {
142 INSTR_EQ_F, /* should use I when having integer support */
147 INSTR_EQ_E, /* FLD has no comparison */
149 INSTR_EQ_E, /* should use I */
156 INSTR_EQ_V, /* variant, should never be accessed */
158 VINSTR_END, /* struct */
159 VINSTR_END, /* union */
160 VINSTR_END, /* array */
161 VINSTR_END, /* nil */
162 VINSTR_END, /* noexpr */
165 uint16_t type_ne_instr[TYPE_COUNT] = {
166 INSTR_NE_F, /* should use I when having integer support */
171 INSTR_NE_E, /* FLD has no comparison */
173 INSTR_NE_E, /* should use I */
180 INSTR_NE_V, /* variant, should never be accessed */
182 VINSTR_END, /* struct */
183 VINSTR_END, /* union */
184 VINSTR_END, /* array */
185 VINSTR_END, /* nil */
186 VINSTR_END, /* noexpr */
189 uint16_t type_not_instr[TYPE_COUNT] = {
190 INSTR_NOT_F, /* should use I when having integer support */
197 INSTR_NOT_ENT, /* should use I */
199 INSTR_NOT_I, /* integer type */
204 INSTR_NOT_V, /* variant, should never be accessed */
206 VINSTR_END, /* struct */
207 VINSTR_END, /* union */
208 VINSTR_END, /* array */
209 VINSTR_END, /* nil */
210 VINSTR_END, /* noexpr */
214 static ir_value* ir_gen_extparam_proto(ir_builder *ir);
215 static void ir_gen_extparam (ir_builder *ir);
217 /* error functions */
219 static void irerror(lex_ctx ctx, const char *msg, ...)
223 con_cvprintmsg((void*)&ctx, LVL_ERROR, "internal error", msg, ap);
227 static bool irwarning(lex_ctx ctx, int warntype, const char *fmt, ...)
232 r = vcompile_warning(ctx, warntype, fmt, ap);
237 /***********************************************************************
238 * Vector utility functions
241 bool GMQCC_WARN vec_ir_value_find(ir_value **vec, const ir_value *what, size_t *idx)
244 size_t len = vec_size(vec);
245 for (i = 0; i < len; ++i) {
246 if (vec[i] == what) {
254 bool GMQCC_WARN vec_ir_block_find(ir_block **vec, ir_block *what, size_t *idx)
257 size_t len = vec_size(vec);
258 for (i = 0; i < len; ++i) {
259 if (vec[i] == what) {
267 bool GMQCC_WARN vec_ir_instr_find(ir_instr **vec, ir_instr *what, size_t *idx)
270 size_t len = vec_size(vec);
271 for (i = 0; i < len; ++i) {
272 if (vec[i] == what) {
280 /***********************************************************************
284 static void ir_block_delete_quick(ir_block* self);
285 static void ir_instr_delete_quick(ir_instr *self);
286 static void ir_function_delete_quick(ir_function *self);
288 ir_builder* ir_builder_new(const char *modulename)
292 self = (ir_builder*)mem_a(sizeof(*self));
296 self->functions = NULL;
297 self->globals = NULL;
299 self->filenames = NULL;
300 self->filestrings = NULL;
301 self->htglobals = util_htnew(IR_HT_SIZE);
302 self->htfields = util_htnew(IR_HT_SIZE);
303 self->htfunctions = util_htnew(IR_HT_SIZE);
305 self->extparams = NULL;
306 self->extparam_protos = NULL;
308 self->first_common_globaltemp = 0;
309 self->max_globaltemps = 0;
310 self->first_common_local = 0;
311 self->max_locals = 0;
313 self->str_immediate = 0;
315 if (!ir_builder_set_name(self, modulename)) {
320 self->nil = ir_value_var("nil", store_value, TYPE_NIL);
321 self->nil->cvq = CV_CONST;
323 self->reserved_va_count = NULL;
328 void ir_builder_delete(ir_builder* self)
331 util_htdel(self->htglobals);
332 util_htdel(self->htfields);
333 util_htdel(self->htfunctions);
334 mem_d((void*)self->name);
335 for (i = 0; i != vec_size(self->functions); ++i) {
336 ir_function_delete_quick(self->functions[i]);
338 vec_free(self->functions);
339 for (i = 0; i != vec_size(self->extparams); ++i) {
340 ir_value_delete(self->extparams[i]);
342 vec_free(self->extparams);
343 for (i = 0; i != vec_size(self->globals); ++i) {
344 ir_value_delete(self->globals[i]);
346 vec_free(self->globals);
347 for (i = 0; i != vec_size(self->fields); ++i) {
348 ir_value_delete(self->fields[i]);
350 ir_value_delete(self->nil);
351 vec_free(self->fields);
352 vec_free(self->filenames);
353 vec_free(self->filestrings);
357 bool ir_builder_set_name(ir_builder *self, const char *name)
360 mem_d((void*)self->name);
361 self->name = util_strdup(name);
365 ir_function* ir_builder_get_function(ir_builder *self, const char *name)
367 return (ir_function*)util_htget(self->htfunctions, name);
370 ir_function* ir_builder_create_function(ir_builder *self, const char *name, int outtype)
372 ir_function *fn = ir_builder_get_function(self, name);
377 fn = ir_function_new(self, outtype);
378 if (!ir_function_set_name(fn, name))
380 ir_function_delete(fn);
383 vec_push(self->functions, fn);
384 util_htset(self->htfunctions, name, fn);
386 fn->value = ir_builder_create_global(self, fn->name, TYPE_FUNCTION);
388 ir_function_delete(fn);
392 fn->value->hasvalue = true;
393 fn->value->outtype = outtype;
394 fn->value->constval.vfunc = fn;
395 fn->value->context = fn->context;
400 ir_value* ir_builder_get_global(ir_builder *self, const char *name)
402 return (ir_value*)util_htget(self->htglobals, name);
405 ir_value* ir_builder_create_global(ir_builder *self, const char *name, int vtype)
409 if (name && name[0] != '#')
411 ve = ir_builder_get_global(self, name);
417 ve = ir_value_var(name, store_global, vtype);
418 vec_push(self->globals, ve);
419 util_htset(self->htglobals, name, ve);
423 ir_value* ir_builder_get_va_count(ir_builder *self)
425 if (self->reserved_va_count)
426 return self->reserved_va_count;
427 return (self->reserved_va_count = ir_builder_create_global(self, "reserved:va_count", TYPE_FLOAT));
430 ir_value* ir_builder_get_field(ir_builder *self, const char *name)
432 return (ir_value*)util_htget(self->htfields, name);
436 ir_value* ir_builder_create_field(ir_builder *self, const char *name, int vtype)
438 ir_value *ve = ir_builder_get_field(self, name);
443 ve = ir_value_var(name, store_global, TYPE_FIELD);
444 ve->fieldtype = vtype;
445 vec_push(self->fields, ve);
446 util_htset(self->htfields, name, ve);
450 /***********************************************************************
454 bool ir_function_naive_phi(ir_function*);
455 void ir_function_enumerate(ir_function*);
456 bool ir_function_calculate_liferanges(ir_function*);
457 bool ir_function_allocate_locals(ir_function*);
459 ir_function* ir_function_new(ir_builder* owner, int outtype)
462 self = (ir_function*)mem_a(sizeof(*self));
467 memset(self, 0, sizeof(*self));
470 if (!ir_function_set_name(self, "<@unnamed>")) {
477 self->context.file = "<@no context>";
478 self->context.line = 0;
479 self->outtype = outtype;
488 self->max_varargs = 0;
490 self->code_function_def = -1;
491 self->allocated_locals = 0;
492 self->globaltemps = 0;
498 bool ir_function_set_name(ir_function *self, const char *name)
501 mem_d((void*)self->name);
502 self->name = util_strdup(name);
506 static void ir_function_delete_quick(ir_function *self)
509 mem_d((void*)self->name);
511 for (i = 0; i != vec_size(self->blocks); ++i)
512 ir_block_delete_quick(self->blocks[i]);
513 vec_free(self->blocks);
515 vec_free(self->params);
517 for (i = 0; i != vec_size(self->values); ++i)
518 ir_value_delete(self->values[i]);
519 vec_free(self->values);
521 for (i = 0; i != vec_size(self->locals); ++i)
522 ir_value_delete(self->locals[i]);
523 vec_free(self->locals);
525 /* self->value is deleted by the builder */
530 void ir_function_delete(ir_function *self)
533 mem_d((void*)self->name);
535 for (i = 0; i != vec_size(self->blocks); ++i)
536 ir_block_delete(self->blocks[i]);
537 vec_free(self->blocks);
539 vec_free(self->params);
541 for (i = 0; i != vec_size(self->values); ++i)
542 ir_value_delete(self->values[i]);
543 vec_free(self->values);
545 for (i = 0; i != vec_size(self->locals); ++i)
546 ir_value_delete(self->locals[i]);
547 vec_free(self->locals);
549 /* self->value is deleted by the builder */
554 void ir_function_collect_value(ir_function *self, ir_value *v)
556 vec_push(self->values, v);
559 ir_block* ir_function_create_block(lex_ctx ctx, ir_function *self, const char *label)
561 ir_block* bn = ir_block_new(self, label);
563 vec_push(self->blocks, bn);
567 static bool instr_is_operation(uint16_t op)
569 return ( (op >= INSTR_MUL_F && op <= INSTR_GT) ||
570 (op >= INSTR_LOAD_F && op <= INSTR_LOAD_FNC) ||
571 (op == INSTR_ADDRESS) ||
572 (op >= INSTR_NOT_F && op <= INSTR_NOT_FNC) ||
573 (op >= INSTR_AND && op <= INSTR_BITOR) ||
574 (op >= INSTR_CALL0 && op <= INSTR_CALL8) );
577 bool ir_function_pass_peephole(ir_function *self)
581 for (b = 0; b < vec_size(self->blocks); ++b) {
583 ir_block *block = self->blocks[b];
585 for (i = 0; i < vec_size(block->instr); ++i) {
587 inst = block->instr[i];
590 (inst->opcode >= INSTR_STORE_F &&
591 inst->opcode <= INSTR_STORE_FNC))
599 oper = block->instr[i-1];
600 if (!instr_is_operation(oper->opcode))
603 if (OPTS_FLAG(LEGACY_VECTOR_MATHS)) {
604 if (oper->opcode == INSTR_MUL_VF && oper->_ops[2]->memberof == oper->_ops[1])
606 if (oper->opcode == INSTR_MUL_FV && oper->_ops[1]->memberof == oper->_ops[2])
610 value = oper->_ops[0];
612 /* only do it for SSA values */
613 if (value->store != store_value)
616 /* don't optimize out the temp if it's used later again */
617 if (vec_size(value->reads) != 1)
620 /* The very next store must use this value */
621 if (value->reads[0] != store)
624 /* And of course the store must _read_ from it, so it's in
626 if (store->_ops[1] != value)
629 ++opts_optimizationcount[OPTIM_PEEPHOLE];
630 (void)!ir_instr_op(oper, 0, store->_ops[0], true);
632 vec_remove(block->instr, i, 1);
633 ir_instr_delete(store);
635 else if (inst->opcode == VINSTR_COND)
637 /* COND on a value resulting from a NOT could
638 * remove the NOT and swap its operands
645 value = inst->_ops[0];
647 if (value->store != store_value ||
648 vec_size(value->reads) != 1 ||
649 value->reads[0] != inst)
654 inot = value->writes[0];
655 if (inot->_ops[0] != value ||
656 inot->opcode < INSTR_NOT_F ||
657 inot->opcode > INSTR_NOT_FNC ||
658 inot->opcode == INSTR_NOT_V || /* can't do these */
659 inot->opcode == INSTR_NOT_S)
665 ++opts_optimizationcount[OPTIM_PEEPHOLE];
667 (void)!ir_instr_op(inst, 0, inot->_ops[1], false);
670 for (inotid = 0; inotid < vec_size(tmp->instr); ++inotid) {
671 if (tmp->instr[inotid] == inot)
674 if (inotid >= vec_size(tmp->instr)) {
675 compile_error(inst->context, "sanity-check failed: failed to find instruction to optimize out");
678 vec_remove(tmp->instr, inotid, 1);
679 ir_instr_delete(inot);
680 /* swap ontrue/onfalse */
682 inst->bops[0] = inst->bops[1];
693 bool ir_function_pass_tailrecursion(ir_function *self)
697 for (b = 0; b < vec_size(self->blocks); ++b) {
699 ir_instr *ret, *call, *store = NULL;
700 ir_block *block = self->blocks[b];
702 if (!block->final || vec_size(block->instr) < 2)
705 ret = block->instr[vec_size(block->instr)-1];
706 if (ret->opcode != INSTR_DONE && ret->opcode != INSTR_RETURN)
709 call = block->instr[vec_size(block->instr)-2];
710 if (call->opcode >= INSTR_STORE_F && call->opcode <= INSTR_STORE_FNC) {
711 /* account for the unoptimized
713 * STORE %return, %tmp
717 if (vec_size(block->instr) < 3)
721 call = block->instr[vec_size(block->instr)-3];
724 if (call->opcode < INSTR_CALL0 || call->opcode > INSTR_CALL8)
728 /* optimize out the STORE */
730 ret->_ops[0] == store->_ops[0] &&
731 store->_ops[1] == call->_ops[0])
733 ++opts_optimizationcount[OPTIM_PEEPHOLE];
734 call->_ops[0] = store->_ops[0];
735 vec_remove(block->instr, vec_size(block->instr) - 2, 1);
736 ir_instr_delete(store);
745 funcval = call->_ops[1];
748 if (funcval->vtype != TYPE_FUNCTION || funcval->constval.vfunc != self)
751 /* now we have a CALL and a RET, check if it's a tailcall */
752 if (ret->_ops[0] && call->_ops[0] != ret->_ops[0])
755 ++opts_optimizationcount[OPTIM_TAIL_RECURSION];
756 vec_shrinkby(block->instr, 2);
758 block->final = false; /* open it back up */
760 /* emite parameter-stores */
761 for (p = 0; p < vec_size(call->params); ++p) {
762 /* assert(call->params_count <= self->locals_count); */
763 if (!ir_block_create_store(block, call->context, self->locals[p], call->params[p])) {
764 irerror(call->context, "failed to create tailcall store instruction for parameter %i", (int)p);
768 if (!ir_block_create_jump(block, call->context, self->blocks[0])) {
769 irerror(call->context, "failed to create tailcall jump");
773 ir_instr_delete(call);
774 ir_instr_delete(ret);
780 bool ir_function_finalize(ir_function *self)
787 if (OPTS_OPTIMIZATION(OPTIM_PEEPHOLE)) {
788 if (!ir_function_pass_peephole(self)) {
789 irerror(self->context, "generic optimization pass broke something in `%s`", self->name);
794 if (OPTS_OPTIMIZATION(OPTIM_TAIL_RECURSION)) {
795 if (!ir_function_pass_tailrecursion(self)) {
796 irerror(self->context, "tail-recursion optimization pass broke something in `%s`", self->name);
801 if (!ir_function_naive_phi(self)) {
802 irerror(self->context, "internal error: ir_function_naive_phi failed");
806 for (i = 0; i < vec_size(self->locals); ++i) {
807 ir_value *v = self->locals[i];
808 if (v->vtype == TYPE_VECTOR ||
809 (v->vtype == TYPE_FIELD && v->outtype == TYPE_VECTOR))
811 ir_value_vector_member(v, 0);
812 ir_value_vector_member(v, 1);
813 ir_value_vector_member(v, 2);
816 for (i = 0; i < vec_size(self->values); ++i) {
817 ir_value *v = self->values[i];
818 if (v->vtype == TYPE_VECTOR ||
819 (v->vtype == TYPE_FIELD && v->outtype == TYPE_VECTOR))
821 ir_value_vector_member(v, 0);
822 ir_value_vector_member(v, 1);
823 ir_value_vector_member(v, 2);
827 ir_function_enumerate(self);
829 if (!ir_function_calculate_liferanges(self))
831 if (!ir_function_allocate_locals(self))
836 ir_value* ir_function_create_local(ir_function *self, const char *name, int vtype, bool param)
841 vec_size(self->locals) &&
842 self->locals[vec_size(self->locals)-1]->store != store_param) {
843 irerror(self->context, "cannot add parameters after adding locals");
847 ve = ir_value_var(name, (param ? store_param : store_local), vtype);
850 vec_push(self->locals, ve);
854 /***********************************************************************
858 ir_block* ir_block_new(ir_function* owner, const char *name)
861 self = (ir_block*)mem_a(sizeof(*self));
865 memset(self, 0, sizeof(*self));
868 if (name && !ir_block_set_label(self, name)) {
873 self->context.file = "<@no context>";
874 self->context.line = 0;
878 self->entries = NULL;
882 self->is_return = false;
886 self->generated = false;
891 static void ir_block_delete_quick(ir_block* self)
894 if (self->label) mem_d(self->label);
895 for (i = 0; i != vec_size(self->instr); ++i)
896 ir_instr_delete_quick(self->instr[i]);
897 vec_free(self->instr);
898 vec_free(self->entries);
899 vec_free(self->exits);
900 vec_free(self->living);
904 void ir_block_delete(ir_block* self)
907 if (self->label) mem_d(self->label);
908 for (i = 0; i != vec_size(self->instr); ++i)
909 ir_instr_delete(self->instr[i]);
910 vec_free(self->instr);
911 vec_free(self->entries);
912 vec_free(self->exits);
913 vec_free(self->living);
917 bool ir_block_set_label(ir_block *self, const char *name)
920 mem_d((void*)self->label);
921 self->label = util_strdup(name);
922 return !!self->label;
925 /***********************************************************************
929 ir_instr* ir_instr_new(lex_ctx ctx, ir_block* owner, int op)
932 self = (ir_instr*)mem_a(sizeof(*self));
939 self->_ops[0] = NULL;
940 self->_ops[1] = NULL;
941 self->_ops[2] = NULL;
942 self->bops[0] = NULL;
943 self->bops[1] = NULL;
954 static void ir_instr_delete_quick(ir_instr *self)
957 vec_free(self->params);
961 void ir_instr_delete(ir_instr *self)
964 /* The following calls can only delete from
965 * vectors, we still want to delete this instruction
966 * so ignore the return value. Since with the warn_unused_result attribute
967 * gcc doesn't care about an explicit: (void)foo(); to ignore the result,
968 * I have to improvise here and use if(foo());
970 for (i = 0; i < vec_size(self->phi); ++i) {
972 if (vec_ir_instr_find(self->phi[i].value->writes, self, &idx))
973 vec_remove(self->phi[i].value->writes, idx, 1);
974 if (vec_ir_instr_find(self->phi[i].value->reads, self, &idx))
975 vec_remove(self->phi[i].value->reads, idx, 1);
978 for (i = 0; i < vec_size(self->params); ++i) {
980 if (vec_ir_instr_find(self->params[i]->writes, self, &idx))
981 vec_remove(self->params[i]->writes, idx, 1);
982 if (vec_ir_instr_find(self->params[i]->reads, self, &idx))
983 vec_remove(self->params[i]->reads, idx, 1);
985 vec_free(self->params);
986 (void)!ir_instr_op(self, 0, NULL, false);
987 (void)!ir_instr_op(self, 1, NULL, false);
988 (void)!ir_instr_op(self, 2, NULL, false);
992 bool ir_instr_op(ir_instr *self, int op, ir_value *v, bool writing)
994 if (self->_ops[op]) {
996 if (writing && vec_ir_instr_find(self->_ops[op]->writes, self, &idx))
997 vec_remove(self->_ops[op]->writes, idx, 1);
998 else if (vec_ir_instr_find(self->_ops[op]->reads, self, &idx))
999 vec_remove(self->_ops[op]->reads, idx, 1);
1003 vec_push(v->writes, self);
1005 vec_push(v->reads, self);
1011 /***********************************************************************
1015 void ir_value_code_setaddr(ir_value *self, int32_t gaddr)
1017 self->code.globaladdr = gaddr;
1018 if (self->members[0]) self->members[0]->code.globaladdr = gaddr;
1019 if (self->members[1]) self->members[1]->code.globaladdr = gaddr;
1020 if (self->members[2]) self->members[2]->code.globaladdr = gaddr;
1023 int32_t ir_value_code_addr(const ir_value *self)
1025 if (self->store == store_return)
1026 return OFS_RETURN + self->code.addroffset;
1027 return self->code.globaladdr + self->code.addroffset;
1030 ir_value* ir_value_var(const char *name, int storetype, int vtype)
1033 self = (ir_value*)mem_a(sizeof(*self));
1034 self->vtype = vtype;
1035 self->fieldtype = TYPE_VOID;
1036 self->outtype = TYPE_VOID;
1037 self->store = storetype;
1041 self->writes = NULL;
1043 self->cvq = CV_NONE;
1044 self->hasvalue = false;
1045 self->context.file = "<@no context>";
1046 self->context.line = 0;
1048 if (name && !ir_value_set_name(self, name)) {
1049 irerror(self->context, "out of memory");
1054 memset(&self->constval, 0, sizeof(self->constval));
1055 memset(&self->code, 0, sizeof(self->code));
1057 self->members[0] = NULL;
1058 self->members[1] = NULL;
1059 self->members[2] = NULL;
1060 self->memberof = NULL;
1062 self->unique_life = false;
1063 self->locked = false;
1064 self->callparam = false;
1070 ir_value* ir_value_vector_member(ir_value *self, unsigned int member)
1078 if (self->members[member])
1079 return self->members[member];
1082 len = strlen(self->name);
1083 name = (char*)mem_a(len + 3);
1084 memcpy(name, self->name, len);
1086 name[len+1] = 'x' + member;
1092 if (self->vtype == TYPE_VECTOR)
1094 m = ir_value_var(name, self->store, TYPE_FLOAT);
1099 m->context = self->context;
1101 self->members[member] = m;
1102 m->code.addroffset = member;
1104 else if (self->vtype == TYPE_FIELD)
1106 if (self->fieldtype != TYPE_VECTOR)
1108 m = ir_value_var(name, self->store, TYPE_FIELD);
1113 m->fieldtype = TYPE_FLOAT;
1114 m->context = self->context;
1116 self->members[member] = m;
1117 m->code.addroffset = member;
1121 irerror(self->context, "invalid member access on %s", self->name);
1129 static GMQCC_INLINE size_t ir_value_sizeof(const ir_value *self)
1131 if (self->vtype == TYPE_FIELD && self->fieldtype == TYPE_VECTOR)
1132 return type_sizeof_[TYPE_VECTOR];
1133 return type_sizeof_[self->vtype];
1136 ir_value* ir_value_out(ir_function *owner, const char *name, int storetype, int vtype)
1138 ir_value *v = ir_value_var(name, storetype, vtype);
1141 ir_function_collect_value(owner, v);
1145 void ir_value_delete(ir_value* self)
1149 mem_d((void*)self->name);
1152 if (self->vtype == TYPE_STRING)
1153 mem_d((void*)self->constval.vstring);
1155 for (i = 0; i < 3; ++i) {
1156 if (self->members[i])
1157 ir_value_delete(self->members[i]);
1159 vec_free(self->reads);
1160 vec_free(self->writes);
1161 vec_free(self->life);
1165 bool ir_value_set_name(ir_value *self, const char *name)
1168 mem_d((void*)self->name);
1169 self->name = util_strdup(name);
1170 return !!self->name;
1173 bool ir_value_set_float(ir_value *self, float f)
1175 if (self->vtype != TYPE_FLOAT)
1177 self->constval.vfloat = f;
1178 self->hasvalue = true;
1182 bool ir_value_set_func(ir_value *self, int f)
1184 if (self->vtype != TYPE_FUNCTION)
1186 self->constval.vint = f;
1187 self->hasvalue = true;
1191 bool ir_value_set_vector(ir_value *self, vector v)
1193 if (self->vtype != TYPE_VECTOR)
1195 self->constval.vvec = v;
1196 self->hasvalue = true;
1200 bool ir_value_set_field(ir_value *self, ir_value *fld)
1202 if (self->vtype != TYPE_FIELD)
1204 self->constval.vpointer = fld;
1205 self->hasvalue = true;
1209 static char *ir_strdup(const char *str)
1212 /* actually dup empty strings */
1213 char *out = (char*)mem_a(1);
1217 return util_strdup(str);
1220 bool ir_value_set_string(ir_value *self, const char *str)
1222 if (self->vtype != TYPE_STRING)
1224 self->constval.vstring = ir_strdup(str);
1225 self->hasvalue = true;
1230 bool ir_value_set_int(ir_value *self, int i)
1232 if (self->vtype != TYPE_INTEGER)
1234 self->constval.vint = i;
1235 self->hasvalue = true;
1240 bool ir_value_lives(ir_value *self, size_t at)
1243 for (i = 0; i < vec_size(self->life); ++i)
1245 ir_life_entry_t *life = &self->life[i];
1246 if (life->start <= at && at <= life->end)
1248 if (life->start > at) /* since it's ordered */
1254 bool ir_value_life_insert(ir_value *self, size_t idx, ir_life_entry_t e)
1257 vec_push(self->life, e);
1258 for (k = vec_size(self->life)-1; k > idx; --k)
1259 self->life[k] = self->life[k-1];
1260 self->life[idx] = e;
1264 bool ir_value_life_merge(ir_value *self, size_t s)
1267 const size_t vs = vec_size(self->life);
1268 ir_life_entry_t *life = NULL;
1269 ir_life_entry_t *before = NULL;
1270 ir_life_entry_t new_entry;
1272 /* Find the first range >= s */
1273 for (i = 0; i < vs; ++i)
1276 life = &self->life[i];
1277 if (life->start > s)
1280 /* nothing found? append */
1283 if (life && life->end+1 == s)
1285 /* previous life range can be merged in */
1289 if (life && life->end >= s)
1291 e.start = e.end = s;
1292 vec_push(self->life, e);
1298 if (before->end + 1 == s &&
1299 life->start - 1 == s)
1302 before->end = life->end;
1303 vec_remove(self->life, i, 1);
1306 if (before->end + 1 == s)
1312 /* already contained */
1313 if (before->end >= s)
1317 if (life->start - 1 == s)
1322 /* insert a new entry */
1323 new_entry.start = new_entry.end = s;
1324 return ir_value_life_insert(self, i, new_entry);
1327 bool ir_value_life_merge_into(ir_value *self, const ir_value *other)
1331 if (!vec_size(other->life))
1334 if (!vec_size(self->life)) {
1335 size_t count = vec_size(other->life);
1336 ir_life_entry_t *life = vec_add(self->life, count);
1337 memcpy(life, other->life, count * sizeof(*life));
1342 for (i = 0; i < vec_size(other->life); ++i)
1344 const ir_life_entry_t *life = &other->life[i];
1347 ir_life_entry_t *entry = &self->life[myi];
1349 if (life->end+1 < entry->start)
1351 /* adding an interval before entry */
1352 if (!ir_value_life_insert(self, myi, *life))
1358 if (life->start < entry->start &&
1359 life->end+1 >= entry->start)
1361 /* starts earlier and overlaps */
1362 entry->start = life->start;
1365 if (life->end > entry->end &&
1366 life->start <= entry->end+1)
1368 /* ends later and overlaps */
1369 entry->end = life->end;
1372 /* see if our change combines it with the next ranges */
1373 while (myi+1 < vec_size(self->life) &&
1374 entry->end+1 >= self->life[1+myi].start)
1376 /* overlaps with (myi+1) */
1377 if (entry->end < self->life[1+myi].end)
1378 entry->end = self->life[1+myi].end;
1379 vec_remove(self->life, myi+1, 1);
1380 entry = &self->life[myi];
1383 /* see if we're after the entry */
1384 if (life->start > entry->end)
1387 /* append if we're at the end */
1388 if (myi >= vec_size(self->life)) {
1389 vec_push(self->life, *life);
1392 /* otherweise check the next range */
1401 bool ir_values_overlap(const ir_value *a, const ir_value *b)
1403 /* For any life entry in A see if it overlaps with
1404 * any life entry in B.
1405 * Note that the life entries are orderes, so we can make a
1406 * more efficient algorithm there than naively translating the
1410 ir_life_entry_t *la, *lb, *enda, *endb;
1412 /* first of all, if either has no life range, they cannot clash */
1413 if (!vec_size(a->life) || !vec_size(b->life))
1418 enda = la + vec_size(a->life);
1419 endb = lb + vec_size(b->life);
1422 /* check if the entries overlap, for that,
1423 * both must start before the other one ends.
1425 if (la->start < lb->end &&
1426 lb->start < la->end)
1431 /* entries are ordered
1432 * one entry is earlier than the other
1433 * that earlier entry will be moved forward
1435 if (la->start < lb->start)
1437 /* order: A B, move A forward
1438 * check if we hit the end with A
1443 else /* if (lb->start < la->start) actually <= */
1445 /* order: B A, move B forward
1446 * check if we hit the end with B
1455 /***********************************************************************
1459 static bool ir_check_unreachable(ir_block *self)
1461 /* The IR should never have to deal with unreachable code */
1462 if (!self->final/* || OPTS_FLAG(ALLOW_UNREACHABLE_CODE)*/)
1464 irerror(self->context, "unreachable statement (%s)", self->label);
1468 bool ir_block_create_store_op(ir_block *self, lex_ctx ctx, int op, ir_value *target, ir_value *what)
1471 if (!ir_check_unreachable(self))
1474 if (target->store == store_value &&
1475 (op < INSTR_STOREP_F || op > INSTR_STOREP_FNC))
1477 irerror(self->context, "cannot store to an SSA value");
1478 irerror(self->context, "trying to store: %s <- %s", target->name, what->name);
1479 irerror(self->context, "instruction: %s", asm_instr[op].m);
1483 in = ir_instr_new(ctx, self, op);
1487 if (!ir_instr_op(in, 0, target, (op < INSTR_STOREP_F || op > INSTR_STOREP_FNC)) ||
1488 !ir_instr_op(in, 1, what, false))
1490 ir_instr_delete(in);
1493 vec_push(self->instr, in);
1497 bool ir_block_create_store(ir_block *self, lex_ctx ctx, ir_value *target, ir_value *what)
1501 if (target->vtype == TYPE_VARIANT)
1502 vtype = what->vtype;
1504 vtype = target->vtype;
1507 if (vtype == TYPE_FLOAT && what->vtype == TYPE_INTEGER)
1508 op = INSTR_CONV_ITOF;
1509 else if (vtype == TYPE_INTEGER && what->vtype == TYPE_FLOAT)
1510 op = INSTR_CONV_FTOI;
1512 op = type_store_instr[vtype];
1514 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS)) {
1515 if (op == INSTR_STORE_FLD && what->fieldtype == TYPE_VECTOR)
1519 return ir_block_create_store_op(self, ctx, op, target, what);
1522 bool ir_block_create_storep(ir_block *self, lex_ctx ctx, ir_value *target, ir_value *what)
1527 if (target->vtype != TYPE_POINTER)
1530 /* storing using pointer - target is a pointer, type must be
1531 * inferred from source
1533 vtype = what->vtype;
1535 op = type_storep_instr[vtype];
1536 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS)) {
1537 if (op == INSTR_STOREP_FLD && what->fieldtype == TYPE_VECTOR)
1538 op = INSTR_STOREP_V;
1541 return ir_block_create_store_op(self, ctx, op, target, what);
1544 bool ir_block_create_return(ir_block *self, lex_ctx ctx, ir_value *v)
1547 if (!ir_check_unreachable(self))
1550 self->is_return = true;
1551 in = ir_instr_new(ctx, self, INSTR_RETURN);
1555 if (v && !ir_instr_op(in, 0, v, false)) {
1556 ir_instr_delete(in);
1560 vec_push(self->instr, in);
1564 bool ir_block_create_if(ir_block *self, lex_ctx ctx, ir_value *v,
1565 ir_block *ontrue, ir_block *onfalse)
1568 if (!ir_check_unreachable(self))
1571 /*in = ir_instr_new(ctx, self, (v->vtype == TYPE_STRING ? INSTR_IF_S : INSTR_IF_F));*/
1572 in = ir_instr_new(ctx, self, VINSTR_COND);
1576 if (!ir_instr_op(in, 0, v, false)) {
1577 ir_instr_delete(in);
1581 in->bops[0] = ontrue;
1582 in->bops[1] = onfalse;
1584 vec_push(self->instr, in);
1586 vec_push(self->exits, ontrue);
1587 vec_push(self->exits, onfalse);
1588 vec_push(ontrue->entries, self);
1589 vec_push(onfalse->entries, self);
1593 bool ir_block_create_jump(ir_block *self, lex_ctx ctx, ir_block *to)
1596 if (!ir_check_unreachable(self))
1599 in = ir_instr_new(ctx, self, VINSTR_JUMP);
1604 vec_push(self->instr, in);
1606 vec_push(self->exits, to);
1607 vec_push(to->entries, self);
1611 bool ir_block_create_goto(ir_block *self, lex_ctx ctx, ir_block *to)
1613 self->owner->flags |= IR_FLAG_HAS_GOTO;
1614 return ir_block_create_jump(self, ctx, to);
1617 ir_instr* ir_block_create_phi(ir_block *self, lex_ctx ctx, const char *label, int ot)
1621 if (!ir_check_unreachable(self))
1623 in = ir_instr_new(ctx, self, VINSTR_PHI);
1626 out = ir_value_out(self->owner, label, store_value, ot);
1628 ir_instr_delete(in);
1631 if (!ir_instr_op(in, 0, out, true)) {
1632 ir_instr_delete(in);
1633 ir_value_delete(out);
1636 vec_push(self->instr, in);
1640 ir_value* ir_phi_value(ir_instr *self)
1642 return self->_ops[0];
1645 void ir_phi_add(ir_instr* self, ir_block *b, ir_value *v)
1649 if (!vec_ir_block_find(self->owner->entries, b, NULL)) {
1650 /* Must not be possible to cause this, otherwise the AST
1651 * is doing something wrong.
1653 irerror(self->context, "Invalid entry block for PHI");
1659 vec_push(v->reads, self);
1660 vec_push(self->phi, pe);
1663 /* call related code */
1664 ir_instr* ir_block_create_call(ir_block *self, lex_ctx ctx, const char *label, ir_value *func, bool noreturn)
1668 if (!ir_check_unreachable(self))
1670 in = ir_instr_new(ctx, self, (noreturn ? VINSTR_NRCALL : INSTR_CALL0));
1675 self->is_return = true;
1677 out = ir_value_out(self->owner, label, (func->outtype == TYPE_VOID) ? store_return : store_value, func->outtype);
1679 ir_instr_delete(in);
1682 if (!ir_instr_op(in, 0, out, true) ||
1683 !ir_instr_op(in, 1, func, false))
1685 ir_instr_delete(in);
1686 ir_value_delete(out);
1689 vec_push(self->instr, in);
1692 if (!ir_block_create_return(self, ctx, NULL)) {
1693 compile_error(ctx, "internal error: failed to generate dummy-return instruction");
1694 ir_instr_delete(in);
1702 ir_value* ir_call_value(ir_instr *self)
1704 return self->_ops[0];
1707 void ir_call_param(ir_instr* self, ir_value *v)
1709 vec_push(self->params, v);
1710 vec_push(v->reads, self);
1713 /* binary op related code */
1715 ir_value* ir_block_create_binop(ir_block *self, lex_ctx ctx,
1716 const char *label, int opcode,
1717 ir_value *left, ir_value *right)
1739 case INSTR_SUB_S: /* -- offset of string as float */
1744 case INSTR_BITOR_IF:
1745 case INSTR_BITOR_FI:
1746 case INSTR_BITAND_FI:
1747 case INSTR_BITAND_IF:
1762 case INSTR_BITAND_I:
1765 case INSTR_RSHIFT_I:
1766 case INSTR_LSHIFT_I:
1788 /* boolean operations result in floats */
1789 if (opcode >= INSTR_EQ_F && opcode <= INSTR_GT)
1791 else if (opcode >= INSTR_LE && opcode <= INSTR_GT)
1794 else if (opcode >= INSTR_LE_I && opcode <= INSTR_EQ_FI)
1799 if (ot == TYPE_VOID) {
1800 /* The AST or parser were supposed to check this! */
1804 return ir_block_create_general_instr(self, ctx, label, opcode, left, right, ot);
1807 ir_value* ir_block_create_unary(ir_block *self, lex_ctx ctx,
1808 const char *label, int opcode,
1811 int ot = TYPE_FLOAT;
1823 /* QC doesn't have other unary operations. We expect extensions to fill
1824 * the above list, otherwise we assume out-type = in-type, eg for an
1828 ot = operand->vtype;
1831 if (ot == TYPE_VOID) {
1832 /* The AST or parser were supposed to check this! */
1836 /* let's use the general instruction creator and pass NULL for OPB */
1837 return ir_block_create_general_instr(self, ctx, label, opcode, operand, NULL, ot);
1840 ir_value* ir_block_create_general_instr(ir_block *self, lex_ctx ctx, const char *label,
1841 int op, ir_value *a, ir_value *b, int outype)
1846 out = ir_value_out(self->owner, label, store_value, outype);
1850 instr = ir_instr_new(ctx, self, op);
1852 ir_value_delete(out);
1856 if (!ir_instr_op(instr, 0, out, true) ||
1857 !ir_instr_op(instr, 1, a, false) ||
1858 !ir_instr_op(instr, 2, b, false) )
1863 vec_push(self->instr, instr);
1867 ir_instr_delete(instr);
1868 ir_value_delete(out);
1872 ir_value* ir_block_create_fieldaddress(ir_block *self, lex_ctx ctx, const char *label, ir_value *ent, ir_value *field)
1876 /* Support for various pointer types todo if so desired */
1877 if (ent->vtype != TYPE_ENTITY)
1880 if (field->vtype != TYPE_FIELD)
1883 v = ir_block_create_general_instr(self, ctx, label, INSTR_ADDRESS, ent, field, TYPE_POINTER);
1884 v->fieldtype = field->fieldtype;
1888 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)
1891 if (ent->vtype != TYPE_ENTITY)
1894 /* at some point we could redirect for TYPE_POINTER... but that could lead to carelessness */
1895 if (field->vtype != TYPE_FIELD)
1900 case TYPE_FLOAT: op = INSTR_LOAD_F; break;
1901 case TYPE_VECTOR: op = INSTR_LOAD_V; break;
1902 case TYPE_STRING: op = INSTR_LOAD_S; break;
1903 case TYPE_FIELD: op = INSTR_LOAD_FLD; break;
1904 case TYPE_ENTITY: op = INSTR_LOAD_ENT; break;
1905 case TYPE_FUNCTION: op = INSTR_LOAD_FNC; break;
1907 case TYPE_POINTER: op = INSTR_LOAD_I; break;
1908 case TYPE_INTEGER: op = INSTR_LOAD_I; break;
1911 irerror(self->context, "invalid type for ir_block_create_load_from_ent: %s", type_name[outype]);
1915 return ir_block_create_general_instr(self, ctx, label, op, ent, field, outype);
1918 /* PHI resolving breaks the SSA, and must thus be the last
1919 * step before life-range calculation.
1922 static bool ir_block_naive_phi(ir_block *self);
1923 bool ir_function_naive_phi(ir_function *self)
1927 for (i = 0; i < vec_size(self->blocks); ++i)
1929 if (!ir_block_naive_phi(self->blocks[i]))
1935 static bool ir_block_naive_phi(ir_block *self)
1937 size_t i, p; /*, w;*/
1938 /* FIXME: optionally, create_phi can add the phis
1939 * to a list so we don't need to loop through blocks
1940 * - anyway: "don't optimize YET"
1942 for (i = 0; i < vec_size(self->instr); ++i)
1944 ir_instr *instr = self->instr[i];
1945 if (instr->opcode != VINSTR_PHI)
1948 vec_remove(self->instr, i, 1);
1949 --i; /* NOTE: i+1 below */
1951 for (p = 0; p < vec_size(instr->phi); ++p)
1953 ir_value *v = instr->phi[p].value;
1954 ir_block *b = instr->phi[p].from;
1956 if (v->store == store_value &&
1957 vec_size(v->reads) == 1 &&
1958 vec_size(v->writes) == 1)
1960 /* replace the value */
1961 if (!ir_instr_op(v->writes[0], 0, instr->_ops[0], true))
1966 /* force a move instruction */
1967 ir_instr *prevjump = vec_last(b->instr);
1970 instr->_ops[0]->store = store_global;
1971 if (!ir_block_create_store(b, instr->context, instr->_ops[0], v))
1973 instr->_ops[0]->store = store_value;
1974 vec_push(b->instr, prevjump);
1978 ir_instr_delete(instr);
1983 /***********************************************************************
1984 *IR Temp allocation code
1985 * Propagating value life ranges by walking through the function backwards
1986 * until no more changes are made.
1987 * In theory this should happen once more than once for every nested loop
1989 * Though this implementation might run an additional time for if nests.
1992 /* Enumerate instructions used by value's life-ranges
1994 static void ir_block_enumerate(ir_block *self, size_t *_eid)
1998 for (i = 0; i < vec_size(self->instr); ++i)
2000 self->instr[i]->eid = eid++;
2005 /* Enumerate blocks and instructions.
2006 * The block-enumeration is unordered!
2007 * We do not really use the block enumreation, however
2008 * the instruction enumeration is important for life-ranges.
2010 void ir_function_enumerate(ir_function *self)
2013 size_t instruction_id = 0;
2014 for (i = 0; i < vec_size(self->blocks); ++i)
2016 /* each block now gets an additional "entry" instruction id
2017 * we can use to avoid point-life issues
2019 self->blocks[i]->entry_id = instruction_id;
2022 self->blocks[i]->eid = i;
2023 ir_block_enumerate(self->blocks[i], &instruction_id);
2027 /* Local-value allocator
2028 * After finishing creating the liferange of all values used in a function
2029 * we can allocate their global-positions.
2030 * This is the counterpart to register-allocation in register machines.
2037 } function_allocator;
2039 static bool function_allocator_alloc(function_allocator *alloc, ir_value *var)
2042 size_t vsize = ir_value_sizeof(var);
2044 var->code.local = vec_size(alloc->locals);
2046 slot = ir_value_var("reg", store_global, var->vtype);
2050 if (!ir_value_life_merge_into(slot, var))
2053 vec_push(alloc->locals, slot);
2054 vec_push(alloc->sizes, vsize);
2055 vec_push(alloc->unique, var->unique_life);
2060 ir_value_delete(slot);
2064 static bool ir_function_allocator_assign(ir_function *self, function_allocator *alloc, ir_value *v)
2070 return function_allocator_alloc(alloc, v);
2072 for (a = 0; a < vec_size(alloc->locals); ++a)
2074 /* if it's reserved for a unique liferange: skip */
2075 if (alloc->unique[a])
2078 slot = alloc->locals[a];
2080 /* never resize parameters
2081 * will be required later when overlapping temps + locals
2083 if (a < vec_size(self->params) &&
2084 alloc->sizes[a] < ir_value_sizeof(v))
2089 if (ir_values_overlap(v, slot))
2092 if (!ir_value_life_merge_into(slot, v))
2095 /* adjust size for this slot */
2096 if (alloc->sizes[a] < ir_value_sizeof(v))
2097 alloc->sizes[a] = ir_value_sizeof(v);
2102 if (a >= vec_size(alloc->locals)) {
2103 if (!function_allocator_alloc(alloc, v))
2109 bool ir_function_allocate_locals(ir_function *self)
2114 bool opt_gt = OPTS_OPTIMIZATION(OPTIM_GLOBAL_TEMPS);
2118 function_allocator lockalloc, globalloc;
2120 if (!vec_size(self->locals) && !vec_size(self->values))
2123 globalloc.locals = NULL;
2124 globalloc.sizes = NULL;
2125 globalloc.positions = NULL;
2126 globalloc.unique = NULL;
2127 lockalloc.locals = NULL;
2128 lockalloc.sizes = NULL;
2129 lockalloc.positions = NULL;
2130 lockalloc.unique = NULL;
2132 for (i = 0; i < vec_size(self->locals); ++i)
2134 v = self->locals[i];
2135 if ((self->flags & IR_FLAG_MASK_NO_LOCAL_TEMPS) || !OPTS_OPTIMIZATION(OPTIM_LOCAL_TEMPS)) {
2137 v->unique_life = true;
2139 else if (i >= vec_size(self->params))
2142 v->locked = true; /* lock parameters locals */
2143 if (!function_allocator_alloc((v->locked || !opt_gt ? &lockalloc : &globalloc), v))
2146 for (; i < vec_size(self->locals); ++i)
2148 v = self->locals[i];
2149 if (!vec_size(v->life))
2151 if (!ir_function_allocator_assign(self, (v->locked || !opt_gt ? &lockalloc : &globalloc), v))
2155 /* Allocate a slot for any value that still exists */
2156 for (i = 0; i < vec_size(self->values); ++i)
2158 v = self->values[i];
2160 if (!vec_size(v->life))
2163 /* CALL optimization:
2164 * If the value is a parameter-temp: 1 write, 1 read from a CALL
2165 * and it's not "locked", write it to the OFS_PARM directly.
2167 if (OPTS_OPTIMIZATION(OPTIM_CALL_STORES) && !v->locked && !v->unique_life) {
2168 if (vec_size(v->reads) == 1 && vec_size(v->writes) == 1 &&
2169 (v->reads[0]->opcode == VINSTR_NRCALL ||
2170 (v->reads[0]->opcode >= INSTR_CALL0 && v->reads[0]->opcode <= INSTR_CALL8)
2175 ir_instr *call = v->reads[0];
2176 if (!vec_ir_value_find(call->params, v, ¶m)) {
2177 irerror(call->context, "internal error: unlocked parameter %s not found", v->name);
2180 ++opts_optimizationcount[OPTIM_CALL_STORES];
2181 v->callparam = true;
2183 ir_value_code_setaddr(v, OFS_PARM0 + 3*param);
2185 size_t nprotos = vec_size(self->owner->extparam_protos);
2188 if (nprotos > param)
2189 ep = self->owner->extparam_protos[param];
2192 ep = ir_gen_extparam_proto(self->owner);
2193 while (++nprotos <= param)
2194 ep = ir_gen_extparam_proto(self->owner);
2196 ir_instr_op(v->writes[0], 0, ep, true);
2197 call->params[param+8] = ep;
2201 if (vec_size(v->writes) == 1 && v->writes[0]->opcode == INSTR_CALL0)
2203 v->store = store_return;
2204 if (v->members[0]) v->members[0]->store = store_return;
2205 if (v->members[1]) v->members[1]->store = store_return;
2206 if (v->members[2]) v->members[2]->store = store_return;
2207 ++opts_optimizationcount[OPTIM_CALL_STORES];
2212 if (!ir_function_allocator_assign(self, (v->locked || !opt_gt ? &lockalloc : &globalloc), v))
2216 if (!lockalloc.sizes && !globalloc.sizes) {
2219 vec_push(lockalloc.positions, 0);
2220 vec_push(globalloc.positions, 0);
2222 /* Adjust slot positions based on sizes */
2223 if (lockalloc.sizes) {
2224 pos = (vec_size(lockalloc.sizes) ? lockalloc.positions[0] : 0);
2225 for (i = 1; i < vec_size(lockalloc.sizes); ++i)
2227 pos = lockalloc.positions[i-1] + lockalloc.sizes[i-1];
2228 vec_push(lockalloc.positions, pos);
2230 self->allocated_locals = pos + vec_last(lockalloc.sizes);
2232 if (globalloc.sizes) {
2233 pos = (vec_size(globalloc.sizes) ? globalloc.positions[0] : 0);
2234 for (i = 1; i < vec_size(globalloc.sizes); ++i)
2236 pos = globalloc.positions[i-1] + globalloc.sizes[i-1];
2237 vec_push(globalloc.positions, pos);
2239 self->globaltemps = pos + vec_last(globalloc.sizes);
2242 /* Locals need to know their new position */
2243 for (i = 0; i < vec_size(self->locals); ++i) {
2244 v = self->locals[i];
2245 if (v->locked || !opt_gt)
2246 v->code.local = lockalloc.positions[v->code.local];
2248 v->code.local = globalloc.positions[v->code.local];
2250 /* Take over the actual slot positions on values */
2251 for (i = 0; i < vec_size(self->values); ++i) {
2252 v = self->values[i];
2253 if (v->locked || !opt_gt)
2254 v->code.local = lockalloc.positions[v->code.local];
2256 v->code.local = globalloc.positions[v->code.local];
2264 for (i = 0; i < vec_size(lockalloc.locals); ++i)
2265 ir_value_delete(lockalloc.locals[i]);
2266 for (i = 0; i < vec_size(globalloc.locals); ++i)
2267 ir_value_delete(globalloc.locals[i]);
2268 vec_free(globalloc.unique);
2269 vec_free(globalloc.locals);
2270 vec_free(globalloc.sizes);
2271 vec_free(globalloc.positions);
2272 vec_free(lockalloc.unique);
2273 vec_free(lockalloc.locals);
2274 vec_free(lockalloc.sizes);
2275 vec_free(lockalloc.positions);
2279 /* Get information about which operand
2280 * is read from, or written to.
2282 static void ir_op_read_write(int op, size_t *read, size_t *write)
2302 case INSTR_STOREP_F:
2303 case INSTR_STOREP_V:
2304 case INSTR_STOREP_S:
2305 case INSTR_STOREP_ENT:
2306 case INSTR_STOREP_FLD:
2307 case INSTR_STOREP_FNC:
2318 static bool ir_block_living_add_instr(ir_block *self, size_t eid)
2321 const size_t vs = vec_size(self->living);
2322 bool changed = false;
2323 for (i = 0; i != vs; ++i)
2325 if (ir_value_life_merge(self->living[i], eid))
2331 static bool ir_block_living_lock(ir_block *self)
2334 bool changed = false;
2335 for (i = 0; i != vec_size(self->living); ++i)
2337 if (!self->living[i]->locked) {
2338 self->living[i]->locked = true;
2345 static bool ir_block_life_propagate(ir_block *self, bool *changed)
2349 size_t i, o, p, mem, cnt;
2350 /* bitmasks which operands are read from or written to */
2357 vec_free(self->living);
2359 p = vec_size(self->exits);
2360 for (i = 0; i < p; ++i) {
2361 ir_block *prev = self->exits[i];
2362 cnt = vec_size(prev->living);
2363 for (o = 0; o < cnt; ++o) {
2364 if (!vec_ir_value_find(self->living, prev->living[o], NULL))
2365 vec_push(self->living, prev->living[o]);
2369 i = vec_size(self->instr);
2372 instr = self->instr[i];
2374 /* See which operands are read and write operands */
2375 ir_op_read_write(instr->opcode, &read, &write);
2377 /* Go through the 3 main operands
2378 * writes first, then reads
2380 for (o = 0; o < 3; ++o)
2382 if (!instr->_ops[o]) /* no such operand */
2385 value = instr->_ops[o];
2387 /* We only care about locals */
2388 /* we also calculate parameter liferanges so that locals
2389 * can take up parameter slots */
2390 if (value->store != store_value &&
2391 value->store != store_local &&
2392 value->store != store_param)
2395 /* write operands */
2396 /* When we write to a local, we consider it "dead" for the
2397 * remaining upper part of the function, since in SSA a value
2398 * can only be written once (== created)
2403 bool in_living = vec_ir_value_find(self->living, value, &idx);
2406 /* If the value isn't alive it hasn't been read before... */
2407 /* TODO: See if the warning can be emitted during parsing or AST processing
2408 * otherwise have warning printed here.
2409 * IF printing a warning here: include filecontext_t,
2410 * and make sure it's only printed once
2411 * since this function is run multiple times.
2413 /* con_err( "Value only written %s\n", value->name); */
2414 if (ir_value_life_merge(value, instr->eid))
2417 /* since 'living' won't contain it
2418 * anymore, merge the value, since
2421 if (ir_value_life_merge(value, instr->eid))
2424 vec_remove(self->living, idx, 1);
2426 /* Removing a vector removes all members */
2427 for (mem = 0; mem < 3; ++mem) {
2428 if (value->members[mem] && vec_ir_value_find(self->living, value->members[mem], &idx)) {
2429 if (ir_value_life_merge(value->members[mem], instr->eid))
2431 vec_remove(self->living, idx, 1);
2434 /* Removing the last member removes the vector */
2435 if (value->memberof) {
2436 value = value->memberof;
2437 for (mem = 0; mem < 3; ++mem) {
2438 if (value->members[mem] && vec_ir_value_find(self->living, value->members[mem], NULL))
2441 if (mem == 3 && vec_ir_value_find(self->living, value, &idx)) {
2442 if (ir_value_life_merge(value, instr->eid))
2444 vec_remove(self->living, idx, 1);
2450 if (instr->opcode == INSTR_MUL_VF)
2452 value = instr->_ops[2];
2453 /* the float source will get an additional lifetime */
2454 if (ir_value_life_merge(value, instr->eid+1))
2456 if (value->memberof && ir_value_life_merge(value->memberof, instr->eid+1))
2459 else if (instr->opcode == INSTR_MUL_FV || instr->opcode == INSTR_LOAD_V)
2461 value = instr->_ops[1];
2462 /* the float source will get an additional lifetime */
2463 if (ir_value_life_merge(value, instr->eid+1))
2465 if (value->memberof && ir_value_life_merge(value->memberof, instr->eid+1))
2469 for (o = 0; o < 3; ++o)
2471 if (!instr->_ops[o]) /* no such operand */
2474 value = instr->_ops[o];
2476 /* We only care about locals */
2477 /* we also calculate parameter liferanges so that locals
2478 * can take up parameter slots */
2479 if (value->store != store_value &&
2480 value->store != store_local &&
2481 value->store != store_param)
2487 if (!vec_ir_value_find(self->living, value, NULL))
2488 vec_push(self->living, value);
2489 /* reading adds the full vector */
2490 if (value->memberof && !vec_ir_value_find(self->living, value->memberof, NULL))
2491 vec_push(self->living, value->memberof);
2492 for (mem = 0; mem < 3; ++mem) {
2493 if (value->members[mem] && !vec_ir_value_find(self->living, value->members[mem], NULL))
2494 vec_push(self->living, value->members[mem]);
2498 /* PHI operands are always read operands */
2499 for (p = 0; p < vec_size(instr->phi); ++p)
2501 value = instr->phi[p].value;
2502 if (!vec_ir_value_find(self->living, value, NULL))
2503 vec_push(self->living, value);
2504 /* reading adds the full vector */
2505 if (value->memberof && !vec_ir_value_find(self->living, value->memberof, NULL))
2506 vec_push(self->living, value->memberof);
2507 for (mem = 0; mem < 3; ++mem) {
2508 if (value->members[mem] && !vec_ir_value_find(self->living, value->members[mem], NULL))
2509 vec_push(self->living, value->members[mem]);
2513 /* on a call, all these values must be "locked" */
2514 if (instr->opcode >= INSTR_CALL0 && instr->opcode <= INSTR_CALL8) {
2515 if (ir_block_living_lock(self))
2518 /* call params are read operands too */
2519 for (p = 0; p < vec_size(instr->params); ++p)
2521 value = instr->params[p];
2522 if (!vec_ir_value_find(self->living, value, NULL))
2523 vec_push(self->living, value);
2524 /* reading adds the full vector */
2525 if (value->memberof && !vec_ir_value_find(self->living, value->memberof, NULL))
2526 vec_push(self->living, value->memberof);
2527 for (mem = 0; mem < 3; ++mem) {
2528 if (value->members[mem] && !vec_ir_value_find(self->living, value->members[mem], NULL))
2529 vec_push(self->living, value->members[mem]);
2534 if (ir_block_living_add_instr(self, instr->eid))
2537 /* the "entry" instruction ID */
2538 if (ir_block_living_add_instr(self, self->entry_id))
2544 bool ir_function_calculate_liferanges(ir_function *self)
2549 /* parameters live at 0 */
2550 for (i = 0; i < vec_size(self->params); ++i)
2551 ir_value_life_merge(self->locals[i], 0);
2556 i = vec_size(self->blocks);
2558 ir_block_life_propagate(self->blocks[i], &changed);
2562 if (vec_size(self->blocks)) {
2563 ir_block *block = self->blocks[0];
2564 for (i = 0; i < vec_size(block->living); ++i) {
2565 ir_value *v = block->living[i];
2566 if (v->store != store_local)
2568 if (v->vtype == TYPE_VECTOR)
2570 self->flags |= IR_FLAG_HAS_UNINITIALIZED;
2571 /* find the instruction reading from it */
2572 for (s = 0; s < vec_size(v->reads); ++s) {
2573 if (v->reads[s]->eid == v->life[0].end)
2576 if (s < vec_size(v->reads)) {
2577 if (irwarning(v->context, WARN_USED_UNINITIALIZED,
2578 "variable `%s` may be used uninitialized in this function\n"
2581 v->reads[s]->context.file, v->reads[s]->context.line)
2589 ir_value *vec = v->memberof;
2590 for (s = 0; s < vec_size(vec->reads); ++s) {
2591 if (vec->reads[s]->eid == v->life[0].end)
2594 if (s < vec_size(vec->reads)) {
2595 if (irwarning(v->context, WARN_USED_UNINITIALIZED,
2596 "variable `%s` may be used uninitialized in this function\n"
2599 vec->reads[s]->context.file, vec->reads[s]->context.line)
2607 if (irwarning(v->context, WARN_USED_UNINITIALIZED,
2608 "variable `%s` may be used uninitialized in this function", v->name))
2617 /***********************************************************************
2620 * Since the IR has the convention of putting 'write' operands
2621 * at the beginning, we have to rotate the operands of instructions
2622 * properly in order to generate valid QCVM code.
2624 * Having destinations at a fixed position is more convenient. In QC
2625 * this is *mostly* OPC, but FTE adds at least 2 instructions which
2626 * read from from OPA, and store to OPB rather than OPC. Which is
2627 * partially the reason why the implementation of these instructions
2628 * in darkplaces has been delayed for so long.
2630 * Breaking conventions is annoying...
2632 static bool ir_builder_gen_global(ir_builder *self, ir_value *global, bool islocal);
2634 static bool gen_global_field(ir_value *global)
2636 if (global->hasvalue)
2638 ir_value *fld = global->constval.vpointer;
2640 irerror(global->context, "Invalid field constant with no field: %s", global->name);
2644 /* copy the field's value */
2645 ir_value_code_setaddr(global, vec_size(code_globals));
2646 vec_push(code_globals, fld->code.fieldaddr);
2647 if (global->fieldtype == TYPE_VECTOR) {
2648 vec_push(code_globals, fld->code.fieldaddr+1);
2649 vec_push(code_globals, fld->code.fieldaddr+2);
2654 ir_value_code_setaddr(global, vec_size(code_globals));
2655 vec_push(code_globals, 0);
2656 if (global->fieldtype == TYPE_VECTOR) {
2657 vec_push(code_globals, 0);
2658 vec_push(code_globals, 0);
2661 if (global->code.globaladdr < 0)
2666 static bool gen_global_pointer(ir_value *global)
2668 if (global->hasvalue)
2670 ir_value *target = global->constval.vpointer;
2672 irerror(global->context, "Invalid pointer constant: %s", global->name);
2673 /* NULL pointers are pointing to the NULL constant, which also
2674 * sits at address 0, but still has an ir_value for itself.
2679 /* Here, relocations ARE possible - in fteqcc-enhanced-qc:
2680 * void() foo; <- proto
2681 * void() *fooptr = &foo;
2682 * void() foo = { code }
2684 if (!target->code.globaladdr) {
2685 /* FIXME: Check for the constant nullptr ir_value!
2686 * because then code.globaladdr being 0 is valid.
2688 irerror(global->context, "FIXME: Relocation support");
2692 ir_value_code_setaddr(global, vec_size(code_globals));
2693 vec_push(code_globals, target->code.globaladdr);
2697 ir_value_code_setaddr(global, vec_size(code_globals));
2698 vec_push(code_globals, 0);
2700 if (global->code.globaladdr < 0)
2705 static bool gen_blocks_recursive(ir_function *func, ir_block *block)
2707 prog_section_statement stmt;
2715 block->generated = true;
2716 block->code_start = vec_size(code_statements);
2717 for (i = 0; i < vec_size(block->instr); ++i)
2719 instr = block->instr[i];
2721 if (instr->opcode == VINSTR_PHI) {
2722 irerror(block->context, "cannot generate virtual instruction (phi)");
2726 if (instr->opcode == VINSTR_JUMP) {
2727 target = instr->bops[0];
2728 /* for uncoditional jumps, if the target hasn't been generated
2729 * yet, we generate them right here.
2731 if (!target->generated)
2732 return gen_blocks_recursive(func, target);
2734 /* otherwise we generate a jump instruction */
2735 stmt.opcode = INSTR_GOTO;
2736 stmt.o1.s1 = (target->code_start) - vec_size(code_statements);
2739 if (stmt.o1.s1 != 1)
2740 code_push_statement(&stmt, instr->context.line);
2742 /* no further instructions can be in this block */
2746 if (instr->opcode == VINSTR_COND) {
2747 ontrue = instr->bops[0];
2748 onfalse = instr->bops[1];
2749 /* TODO: have the AST signal which block should
2750 * come first: eg. optimize IFs without ELSE...
2753 stmt.o1.u1 = ir_value_code_addr(instr->_ops[0]);
2757 if (ontrue->generated) {
2758 stmt.opcode = INSTR_IF;
2759 stmt.o2.s1 = (ontrue->code_start) - vec_size(code_statements);
2760 if (stmt.o2.s1 != 1)
2761 code_push_statement(&stmt, instr->context.line);
2763 if (onfalse->generated) {
2764 stmt.opcode = INSTR_IFNOT;
2765 stmt.o2.s1 = (onfalse->code_start) - vec_size(code_statements);
2766 if (stmt.o2.s1 != 1)
2767 code_push_statement(&stmt, instr->context.line);
2769 if (!ontrue->generated) {
2770 if (onfalse->generated)
2771 return gen_blocks_recursive(func, ontrue);
2773 if (!onfalse->generated) {
2774 if (ontrue->generated)
2775 return gen_blocks_recursive(func, onfalse);
2777 /* neither ontrue nor onfalse exist */
2778 stmt.opcode = INSTR_IFNOT;
2779 if (!instr->likely) {
2780 /* Honor the likelyhood hint */
2781 ir_block *tmp = onfalse;
2782 stmt.opcode = INSTR_IF;
2786 stidx = vec_size(code_statements);
2787 code_push_statement(&stmt, instr->context.line);
2788 /* on false we jump, so add ontrue-path */
2789 if (!gen_blocks_recursive(func, ontrue))
2791 /* fixup the jump address */
2792 code_statements[stidx].o2.s1 = vec_size(code_statements) - stidx;
2793 /* generate onfalse path */
2794 if (onfalse->generated) {
2795 /* fixup the jump address */
2796 code_statements[stidx].o2.s1 = (onfalse->code_start) - (stidx);
2797 if (stidx+2 == vec_size(code_statements) && code_statements[stidx].o2.s1 == 1) {
2798 code_statements[stidx] = code_statements[stidx+1];
2799 if (code_statements[stidx].o1.s1 < 0)
2800 code_statements[stidx].o1.s1++;
2801 code_pop_statement();
2803 stmt.opcode = vec_last(code_statements).opcode;
2804 if (stmt.opcode == INSTR_GOTO ||
2805 stmt.opcode == INSTR_IF ||
2806 stmt.opcode == INSTR_IFNOT ||
2807 stmt.opcode == INSTR_RETURN ||
2808 stmt.opcode == INSTR_DONE)
2810 /* no use jumping from here */
2813 /* may have been generated in the previous recursive call */
2814 stmt.opcode = INSTR_GOTO;
2815 stmt.o1.s1 = (onfalse->code_start) - vec_size(code_statements);
2818 if (stmt.o1.s1 != 1)
2819 code_push_statement(&stmt, instr->context.line);
2822 else if (stidx+2 == vec_size(code_statements) && code_statements[stidx].o2.s1 == 1) {
2823 code_statements[stidx] = code_statements[stidx+1];
2824 if (code_statements[stidx].o1.s1 < 0)
2825 code_statements[stidx].o1.s1++;
2826 code_pop_statement();
2828 /* if not, generate now */
2829 return gen_blocks_recursive(func, onfalse);
2832 if ( (instr->opcode >= INSTR_CALL0 && instr->opcode <= INSTR_CALL8)
2833 || instr->opcode == VINSTR_NRCALL)
2838 first = vec_size(instr->params);
2841 for (p = 0; p < first; ++p)
2843 ir_value *param = instr->params[p];
2844 if (param->callparam)
2847 stmt.opcode = INSTR_STORE_F;
2850 if (param->vtype == TYPE_FIELD)
2851 stmt.opcode = field_store_instr[param->fieldtype];
2852 else if (param->vtype == TYPE_NIL)
2853 stmt.opcode = INSTR_STORE_V;
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];
2881 else if (param->vtype == TYPE_NIL)
2882 stmt.opcode = INSTR_STORE_V;
2884 stmt.opcode = type_store_instr[param->vtype];
2885 stmt.o1.u1 = ir_value_code_addr(param);
2886 stmt.o2.u1 = ir_value_code_addr(targetparam);
2887 code_push_statement(&stmt, instr->context.line);
2890 stmt.opcode = INSTR_CALL0 + vec_size(instr->params);
2891 if (stmt.opcode > INSTR_CALL8)
2892 stmt.opcode = INSTR_CALL8;
2893 stmt.o1.u1 = ir_value_code_addr(instr->_ops[1]);
2896 code_push_statement(&stmt, instr->context.line);
2898 retvalue = instr->_ops[0];
2899 if (retvalue && retvalue->store != store_return &&
2900 (retvalue->store == store_global || vec_size(retvalue->life)))
2902 /* not to be kept in OFS_RETURN */
2903 if (retvalue->vtype == TYPE_FIELD && OPTS_FLAG(ADJUST_VECTOR_FIELDS))
2904 stmt.opcode = field_store_instr[retvalue->fieldtype];
2906 stmt.opcode = type_store_instr[retvalue->vtype];
2907 stmt.o1.u1 = OFS_RETURN;
2908 stmt.o2.u1 = ir_value_code_addr(retvalue);
2910 code_push_statement(&stmt, instr->context.line);
2915 if (instr->opcode == INSTR_STATE) {
2916 irerror(block->context, "TODO: state instruction");
2920 stmt.opcode = instr->opcode;
2925 /* This is the general order of operands */
2927 stmt.o3.u1 = ir_value_code_addr(instr->_ops[0]);
2930 stmt.o1.u1 = ir_value_code_addr(instr->_ops[1]);
2933 stmt.o2.u1 = ir_value_code_addr(instr->_ops[2]);
2935 if (stmt.opcode == INSTR_RETURN || stmt.opcode == INSTR_DONE)
2937 stmt.o1.u1 = stmt.o3.u1;
2940 else if ((stmt.opcode >= INSTR_STORE_F &&
2941 stmt.opcode <= INSTR_STORE_FNC) ||
2942 (stmt.opcode >= INSTR_STOREP_F &&
2943 stmt.opcode <= INSTR_STOREP_FNC))
2945 /* 2-operand instructions with A -> B */
2946 stmt.o2.u1 = stmt.o3.u1;
2949 /* tiny optimization, don't output
2952 if (stmt.o2.u1 == stmt.o1.u1 &&
2953 OPTS_OPTIMIZATION(OPTIM_PEEPHOLE))
2955 ++opts_optimizationcount[OPTIM_PEEPHOLE];
2960 code_push_statement(&stmt, instr->context.line);
2965 static bool gen_function_code(ir_function *self)
2968 prog_section_statement stmt, *retst;
2970 /* Starting from entry point, we generate blocks "as they come"
2971 * for now. Dead blocks will not be translated obviously.
2973 if (!vec_size(self->blocks)) {
2974 irerror(self->context, "Function '%s' declared without body.", self->name);
2978 block = self->blocks[0];
2979 if (block->generated)
2982 if (!gen_blocks_recursive(self, block)) {
2983 irerror(self->context, "failed to generate blocks for '%s'", self->name);
2987 /* code_write and qcvm -disasm need to know that the function ends here */
2988 retst = &vec_last(code_statements);
2989 if (OPTS_OPTIMIZATION(OPTIM_VOID_RETURN) &&
2990 self->outtype == TYPE_VOID &&
2991 retst->opcode == INSTR_RETURN &&
2992 !retst->o1.u1 && !retst->o2.u1 && !retst->o3.u1)
2994 retst->opcode = INSTR_DONE;
2995 ++opts_optimizationcount[OPTIM_VOID_RETURN];
2997 stmt.opcode = INSTR_DONE;
3001 code_push_statement(&stmt, vec_last(code_linenums));
3006 static qcint ir_builder_filestring(ir_builder *ir, const char *filename)
3008 /* NOTE: filename pointers are copied, we never strdup them,
3009 * thus we can use pointer-comparison to find the string.
3014 for (i = 0; i < vec_size(ir->filenames); ++i) {
3015 if (ir->filenames[i] == filename)
3016 return ir->filestrings[i];
3019 str = code_genstring(filename);
3020 vec_push(ir->filenames, filename);
3021 vec_push(ir->filestrings, str);
3025 static bool gen_global_function(ir_builder *ir, ir_value *global)
3027 prog_section_function fun;
3032 if (!global->hasvalue || (!global->constval.vfunc))
3034 irerror(global->context, "Invalid state of function-global: not constant: %s", global->name);
3038 irfun = global->constval.vfunc;
3040 fun.name = global->code.name;
3041 fun.file = ir_builder_filestring(ir, global->context.file);
3042 fun.profile = 0; /* always 0 */
3043 fun.nargs = vec_size(irfun->params);
3047 for (i = 0;i < 8; ++i) {
3048 if ((int32_t)i >= fun.nargs)
3051 fun.argsize[i] = type_sizeof_[irfun->params[i]];
3055 fun.locals = irfun->allocated_locals;
3058 fun.entry = irfun->builtin+1;
3060 irfun->code_function_def = vec_size(code_functions);
3061 fun.entry = vec_size(code_statements);
3064 vec_push(code_functions, fun);
3068 static ir_value* ir_gen_extparam_proto(ir_builder *ir)
3073 snprintf(name, sizeof(name), "EXTPARM#%i", (int)(vec_size(ir->extparam_protos)));
3074 global = ir_value_var(name, store_global, TYPE_VECTOR);
3076 vec_push(ir->extparam_protos, global);
3080 static void ir_gen_extparam(ir_builder *ir)
3082 prog_section_def def;
3085 if (vec_size(ir->extparam_protos) < vec_size(ir->extparams)+1)
3086 global = ir_gen_extparam_proto(ir);
3088 global = ir->extparam_protos[vec_size(ir->extparams)];
3090 def.name = code_genstring(global->name);
3091 def.type = TYPE_VECTOR;
3092 def.offset = vec_size(code_globals);
3094 vec_push(code_defs, def);
3095 ir_value_code_setaddr(global, def.offset);
3096 vec_push(code_globals, 0);
3097 vec_push(code_globals, 0);
3098 vec_push(code_globals, 0);
3100 vec_push(ir->extparams, global);
3103 static bool gen_function_extparam_copy(ir_function *self)
3105 size_t i, ext, numparams;
3107 ir_builder *ir = self->owner;
3109 prog_section_statement stmt;
3111 numparams = vec_size(self->params);
3115 stmt.opcode = INSTR_STORE_F;
3117 for (i = 8; i < numparams; ++i) {
3119 if (ext >= vec_size(ir->extparams))
3120 ir_gen_extparam(ir);
3122 ep = ir->extparams[ext];
3124 stmt.opcode = type_store_instr[self->locals[i]->vtype];
3125 if (self->locals[i]->vtype == TYPE_FIELD &&
3126 self->locals[i]->fieldtype == TYPE_VECTOR)
3128 stmt.opcode = INSTR_STORE_V;
3130 stmt.o1.u1 = ir_value_code_addr(ep);
3131 stmt.o2.u1 = ir_value_code_addr(self->locals[i]);
3132 code_push_statement(&stmt, self->context.line);
3138 static bool gen_function_varargs_copy(ir_function *self)
3140 size_t i, ext, numparams, maxparams;
3142 ir_builder *ir = self->owner;
3144 prog_section_statement stmt;
3146 numparams = vec_size(self->params);
3150 stmt.opcode = INSTR_STORE_V;
3152 maxparams = numparams + self->max_varargs;
3153 for (i = numparams; i < maxparams; ++i) {
3155 stmt.o1.u1 = OFS_PARM0 + 3*i;
3156 stmt.o2.u1 = ir_value_code_addr(self->locals[i]);
3157 code_push_statement(&stmt, self->context.line);
3161 while (ext >= vec_size(ir->extparams))
3162 ir_gen_extparam(ir);
3164 ep = ir->extparams[ext];
3166 stmt.o1.u1 = ir_value_code_addr(ep);
3167 stmt.o2.u1 = ir_value_code_addr(self->locals[i]);
3168 code_push_statement(&stmt, self->context.line);
3174 static bool gen_function_locals(ir_builder *ir, ir_value *global)
3176 prog_section_function *def;
3179 uint32_t firstlocal, firstglobal;
3181 irfun = global->constval.vfunc;
3182 def = code_functions + irfun->code_function_def;
3184 if (OPTS_OPTION_BOOL(OPTION_G) ||
3185 !OPTS_OPTIMIZATION(OPTIM_OVERLAP_LOCALS) ||
3186 (irfun->flags & IR_FLAG_MASK_NO_OVERLAP))
3188 firstlocal = def->firstlocal = vec_size(code_globals);
3190 firstlocal = def->firstlocal = ir->first_common_local;
3191 ++opts_optimizationcount[OPTIM_OVERLAP_LOCALS];
3194 firstglobal = (OPTS_OPTIMIZATION(OPTIM_GLOBAL_TEMPS) ? ir->first_common_globaltemp : firstlocal);
3196 for (i = vec_size(code_globals); i < firstlocal + irfun->allocated_locals; ++i)
3197 vec_push(code_globals, 0);
3198 for (i = 0; i < vec_size(irfun->locals); ++i) {
3199 ir_value *v = irfun->locals[i];
3200 if (v->locked || !OPTS_OPTIMIZATION(OPTIM_GLOBAL_TEMPS)) {
3201 ir_value_code_setaddr(v, firstlocal + v->code.local);
3202 if (!ir_builder_gen_global(ir, irfun->locals[i], true)) {
3203 irerror(irfun->locals[i]->context, "failed to generate local %s", irfun->locals[i]->name);
3208 ir_value_code_setaddr(v, firstglobal + v->code.local);
3210 for (i = 0; i < vec_size(irfun->values); ++i)
3212 ir_value *v = irfun->values[i];
3216 ir_value_code_setaddr(v, firstlocal + v->code.local);
3218 ir_value_code_setaddr(v, firstglobal + v->code.local);
3223 static bool gen_global_function_code(ir_builder *ir, ir_value *global)
3225 prog_section_function *fundef;
3230 irfun = global->constval.vfunc;
3232 if (global->cvq == CV_NONE) {
3233 irwarning(global->context, WARN_IMPLICIT_FUNCTION_POINTER,
3234 "function `%s` has no body and in QC implicitly becomes a function-pointer", global->name);
3236 /* this was a function pointer, don't generate code for those */
3243 if (irfun->code_function_def < 0) {
3244 irerror(irfun->context, "`%s`: IR global wasn't generated, failed to access function-def", irfun->name);
3247 fundef = &code_functions[irfun->code_function_def];
3249 fundef->entry = vec_size(code_statements);
3250 if (!gen_function_locals(ir, global)) {
3251 irerror(irfun->context, "Failed to generate locals for function %s", irfun->name);
3254 if (!gen_function_extparam_copy(irfun)) {
3255 irerror(irfun->context, "Failed to generate extparam-copy code for function %s", irfun->name);
3258 if (irfun->max_varargs && !gen_function_varargs_copy(irfun)) {
3259 irerror(irfun->context, "Failed to generate vararg-copy code for function %s", irfun->name);
3262 if (!gen_function_code(irfun)) {
3263 irerror(irfun->context, "Failed to generate code for function %s", irfun->name);
3269 static void gen_vector_defs(prog_section_def def, const char *name)
3274 if (!name || name[0] == '#' || OPTS_FLAG(SINGLE_VECTOR_DEFS))
3277 def.type = TYPE_FLOAT;
3281 component = (char*)mem_a(len+3);
3282 memcpy(component, name, len);
3284 component[len-0] = 0;
3285 component[len-2] = '_';
3287 component[len-1] = 'x';
3289 for (i = 0; i < 3; ++i) {
3290 def.name = code_genstring(component);
3291 vec_push(code_defs, def);
3299 static void gen_vector_fields(prog_section_field fld, const char *name)
3304 if (!name || OPTS_FLAG(SINGLE_VECTOR_DEFS))
3307 fld.type = TYPE_FLOAT;
3311 component = (char*)mem_a(len+3);
3312 memcpy(component, name, len);
3314 component[len-0] = 0;
3315 component[len-2] = '_';
3317 component[len-1] = 'x';
3319 for (i = 0; i < 3; ++i) {
3320 fld.name = code_genstring(component);
3321 vec_push(code_fields, fld);
3329 static bool ir_builder_gen_global(ir_builder *self, ir_value *global, bool islocal)
3333 prog_section_def def;
3334 bool pushdef = opts.optimizeoff;
3336 def.type = global->vtype;
3337 def.offset = vec_size(code_globals);
3339 if (OPTS_OPTION_BOOL(OPTION_G) || !islocal)
3343 if (OPTS_OPTIMIZATION(OPTIM_STRIP_CONSTANT_NAMES) &&
3344 !(global->flags & IR_FLAG_INCLUDE_DEF) &&
3345 (global->name[0] == '#' || global->cvq == CV_CONST))
3350 if (pushdef && global->name) {
3351 if (global->name[0] == '#') {
3352 if (!self->str_immediate)
3353 self->str_immediate = code_genstring("IMMEDIATE");
3354 def.name = global->code.name = self->str_immediate;
3357 def.name = global->code.name = code_genstring(global->name);
3362 def.offset = ir_value_code_addr(global);
3363 vec_push(code_defs, def);
3364 if (global->vtype == TYPE_VECTOR)
3365 gen_vector_defs(def, global->name);
3366 else if (global->vtype == TYPE_FIELD && global->fieldtype == TYPE_VECTOR)
3367 gen_vector_defs(def, global->name);
3374 switch (global->vtype)
3377 if (!strcmp(global->name, "end_sys_globals")) {
3378 /* TODO: remember this point... all the defs before this one
3379 * should be checksummed and added to progdefs.h when we generate it.
3382 else if (!strcmp(global->name, "end_sys_fields")) {
3383 /* TODO: same as above but for entity-fields rather than globsl
3387 irwarning(global->context, WARN_VOID_VARIABLES, "unrecognized variable of type void `%s`",
3389 /* I'd argue setting it to 0 is sufficient, but maybe some depend on knowing how far
3390 * the system fields actually go? Though the engine knows this anyway...
3391 * Maybe this could be an -foption
3392 * fteqcc creates data for end_sys_* - of size 1, so let's do the same
3394 ir_value_code_setaddr(global, vec_size(code_globals));
3395 vec_push(code_globals, 0);
3397 if (pushdef) vec_push(code_defs, def);
3400 if (pushdef) vec_push(code_defs, def);
3401 return gen_global_pointer(global);
3404 vec_push(code_defs, def);
3405 if (global->fieldtype == TYPE_VECTOR)
3406 gen_vector_defs(def, global->name);
3408 return gen_global_field(global);
3413 ir_value_code_setaddr(global, vec_size(code_globals));
3414 if (global->hasvalue) {
3415 iptr = (int32_t*)&global->constval.ivec[0];
3416 vec_push(code_globals, *iptr);
3418 vec_push(code_globals, 0);
3420 if (!islocal && global->cvq != CV_CONST)
3421 def.type |= DEF_SAVEGLOBAL;
3422 if (pushdef) vec_push(code_defs, def);
3424 return global->code.globaladdr >= 0;
3428 ir_value_code_setaddr(global, vec_size(code_globals));
3429 if (global->hasvalue) {
3430 vec_push(code_globals, code_genstring(global->constval.vstring));
3432 vec_push(code_globals, 0);
3434 if (!islocal && global->cvq != CV_CONST)
3435 def.type |= DEF_SAVEGLOBAL;
3436 if (pushdef) vec_push(code_defs, def);
3437 return global->code.globaladdr >= 0;
3442 ir_value_code_setaddr(global, vec_size(code_globals));
3443 if (global->hasvalue) {
3444 iptr = (int32_t*)&global->constval.ivec[0];
3445 vec_push(code_globals, iptr[0]);
3446 if (global->code.globaladdr < 0)
3448 for (d = 1; d < type_sizeof_[global->vtype]; ++d) {
3449 vec_push(code_globals, iptr[d]);
3452 vec_push(code_globals, 0);
3453 if (global->code.globaladdr < 0)
3455 for (d = 1; d < type_sizeof_[global->vtype]; ++d) {
3456 vec_push(code_globals, 0);
3459 if (!islocal && global->cvq != CV_CONST)
3460 def.type |= DEF_SAVEGLOBAL;
3463 vec_push(code_defs, def);
3464 def.type &= ~DEF_SAVEGLOBAL;
3465 gen_vector_defs(def, global->name);
3467 return global->code.globaladdr >= 0;
3470 ir_value_code_setaddr(global, vec_size(code_globals));
3471 if (!global->hasvalue) {
3472 vec_push(code_globals, 0);
3473 if (global->code.globaladdr < 0)
3476 vec_push(code_globals, vec_size(code_functions));
3477 if (!gen_global_function(self, global))
3480 if (!islocal && global->cvq != CV_CONST)
3481 def.type |= DEF_SAVEGLOBAL;
3482 if (pushdef) vec_push(code_defs, def);
3485 /* assume biggest type */
3486 ir_value_code_setaddr(global, vec_size(code_globals));
3487 vec_push(code_globals, 0);
3488 for (i = 1; i < type_sizeof_[TYPE_VARIANT]; ++i)
3489 vec_push(code_globals, 0);
3492 /* refuse to create 'void' type or any other fancy business. */
3493 irerror(global->context, "Invalid type for global variable `%s`: %s",
3494 global->name, type_name[global->vtype]);
3499 static void ir_builder_prepare_field(ir_value *field)
3501 field->code.fieldaddr = code_alloc_field(type_sizeof_[field->fieldtype]);
3504 static bool ir_builder_gen_field(ir_builder *self, ir_value *field)
3506 prog_section_def def;
3507 prog_section_field fld;
3511 def.type = (uint16_t)field->vtype;
3512 def.offset = (uint16_t)vec_size(code_globals);
3514 /* create a global named the same as the field */
3515 if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_GMQCC) {
3516 /* in our standard, the global gets a dot prefix */
3517 size_t len = strlen(field->name);
3520 /* we really don't want to have to allocate this, and 1024
3521 * bytes is more than enough for a variable/field name
3523 if (len+2 >= sizeof(name)) {
3524 irerror(field->context, "invalid field name size: %u", (unsigned int)len);
3529 memcpy(name+1, field->name, len); /* no strncpy - we used strlen above */
3532 def.name = code_genstring(name);
3533 fld.name = def.name + 1; /* we reuse that string table entry */
3535 /* in plain QC, there cannot be a global with the same name,
3536 * and so we also name the global the same.
3537 * FIXME: fteqcc should create a global as well
3538 * check if it actually uses the same name. Probably does
3540 def.name = code_genstring(field->name);
3541 fld.name = def.name;
3544 field->code.name = def.name;
3546 vec_push(code_defs, def);
3548 fld.type = field->fieldtype;
3550 if (fld.type == TYPE_VOID) {
3551 irerror(field->context, "field is missing a type: %s - don't know its size", field->name);
3555 fld.offset = field->code.fieldaddr;
3557 vec_push(code_fields, fld);
3559 ir_value_code_setaddr(field, vec_size(code_globals));
3560 vec_push(code_globals, fld.offset);
3561 if (fld.type == TYPE_VECTOR) {
3562 vec_push(code_globals, fld.offset+1);
3563 vec_push(code_globals, fld.offset+2);
3566 if (field->fieldtype == TYPE_VECTOR) {
3567 gen_vector_defs(def, field->name);
3568 gen_vector_fields(fld, field->name);
3571 return field->code.globaladdr >= 0;
3574 bool ir_builder_generate(ir_builder *self, const char *filename)
3576 prog_section_statement stmt;
3578 char *lnofile = NULL;
3582 for (i = 0; i < vec_size(self->fields); ++i)
3584 ir_builder_prepare_field(self->fields[i]);
3587 for (i = 0; i < vec_size(self->globals); ++i)
3589 if (!ir_builder_gen_global(self, self->globals[i], false)) {
3592 if (self->globals[i]->vtype == TYPE_FUNCTION) {
3593 ir_function *func = self->globals[i]->constval.vfunc;
3594 if (func && self->max_locals < func->allocated_locals &&
3595 !(func->flags & IR_FLAG_MASK_NO_OVERLAP))
3597 self->max_locals = func->allocated_locals;
3599 if (func && self->max_globaltemps < func->globaltemps)
3600 self->max_globaltemps = func->globaltemps;
3604 for (i = 0; i < vec_size(self->fields); ++i)
3606 if (!ir_builder_gen_field(self, self->fields[i])) {
3612 ir_value_code_setaddr(self->nil, vec_size(code_globals));
3613 vec_push(code_globals, 0);
3614 vec_push(code_globals, 0);
3615 vec_push(code_globals, 0);
3617 /* generate global temps */
3618 self->first_common_globaltemp = vec_size(code_globals);
3619 for (i = 0; i < self->max_globaltemps; ++i) {
3620 vec_push(code_globals, 0);
3622 /* generate common locals */
3623 self->first_common_local = vec_size(code_globals);
3624 for (i = 0; i < self->max_locals; ++i) {
3625 vec_push(code_globals, 0);
3628 /* generate function code */
3629 for (i = 0; i < vec_size(self->globals); ++i)
3631 if (self->globals[i]->vtype == TYPE_FUNCTION) {
3632 if (!gen_global_function_code(self, self->globals[i])) {
3638 if (vec_size(code_globals) >= 65536) {
3639 irerror(vec_last(self->globals)->context, "This progs file would require more globals than the metadata can handle. Bailing out.");
3643 /* DP errors if the last instruction is not an INSTR_DONE. */
3644 if (vec_last(code_statements).opcode != INSTR_DONE)
3646 stmt.opcode = INSTR_DONE;
3650 code_push_statement(&stmt, vec_last(code_linenums));
3653 if (OPTS_OPTION_BOOL(OPTION_PP_ONLY))
3656 if (vec_size(code_statements) != vec_size(code_linenums)) {
3657 con_err("Linecounter wrong: %lu != %lu\n",
3658 (unsigned long)vec_size(code_statements),
3659 (unsigned long)vec_size(code_linenums));
3660 } else if (OPTS_FLAG(LNO)) {
3662 size_t filelen = strlen(filename);
3664 memcpy(vec_add(lnofile, filelen+1), filename, filelen+1);
3665 dot = strrchr(lnofile, '.');
3669 vec_shrinkto(lnofile, dot - lnofile);
3671 memcpy(vec_add(lnofile, 5), ".lno", 5);
3674 if (!OPTS_OPTION_BOOL(OPTION_QUIET)) {
3676 con_out("writing '%s' and '%s'...\n", filename, lnofile);
3678 con_out("writing '%s'\n", filename);
3680 if (!code_write(filename, lnofile)) {
3688 /***********************************************************************
3689 *IR DEBUG Dump functions...
3692 #define IND_BUFSZ 1024
3695 # define strncat(dst, src, sz) strncat_s(dst, sz, src, _TRUNCATE)
3698 const char *qc_opname(int op)
3700 if (op < 0) return "<INVALID>";
3701 if (op < (int)( sizeof(asm_instr) / sizeof(asm_instr[0]) ))
3702 return asm_instr[op].m;
3704 case VINSTR_PHI: return "PHI";
3705 case VINSTR_JUMP: return "JUMP";
3706 case VINSTR_COND: return "COND";
3707 default: return "<UNK>";
3711 void ir_builder_dump(ir_builder *b, int (*oprintf)(const char*, ...))
3714 char indent[IND_BUFSZ];
3718 oprintf("module %s\n", b->name);
3719 for (i = 0; i < vec_size(b->globals); ++i)
3722 if (b->globals[i]->hasvalue)
3723 oprintf("%s = ", b->globals[i]->name);
3724 ir_value_dump(b->globals[i], oprintf);
3727 for (i = 0; i < vec_size(b->functions); ++i)
3728 ir_function_dump(b->functions[i], indent, oprintf);
3729 oprintf("endmodule %s\n", b->name);
3732 static const char *storenames[] = {
3733 "[global]", "[local]", "[param]", "[value]", "[return]"
3736 void ir_function_dump(ir_function *f, char *ind,
3737 int (*oprintf)(const char*, ...))
3740 if (f->builtin != 0) {
3741 oprintf("%sfunction %s = builtin %i\n", ind, f->name, -f->builtin);
3744 oprintf("%sfunction %s\n", ind, f->name);
3745 strncat(ind, "\t", IND_BUFSZ);
3746 if (vec_size(f->locals))
3748 oprintf("%s%i locals:\n", ind, (int)vec_size(f->locals));
3749 for (i = 0; i < vec_size(f->locals); ++i) {
3750 oprintf("%s\t", ind);
3751 ir_value_dump(f->locals[i], oprintf);
3755 oprintf("%sliferanges:\n", ind);
3756 for (i = 0; i < vec_size(f->locals); ++i) {
3757 const char *attr = "";
3759 ir_value *v = f->locals[i];
3760 if (v->unique_life && v->locked)
3761 attr = "unique,locked ";
3762 else if (v->unique_life)
3766 oprintf("%s\t%s: %s %s %s%s@%i ", ind, v->name, type_name[v->vtype],
3767 storenames[v->store],
3768 attr, (v->callparam ? "callparam " : ""),
3769 (int)v->code.local);
3772 for (l = 0; l < vec_size(v->life); ++l) {
3773 oprintf("[%i,%i] ", v->life[l].start, v->life[l].end);
3776 for (m = 0; m < 3; ++m) {
3777 ir_value *vm = v->members[m];
3780 oprintf("%s\t%s: @%i ", ind, vm->name, (int)vm->code.local);
3781 for (l = 0; l < vec_size(vm->life); ++l) {
3782 oprintf("[%i,%i] ", vm->life[l].start, vm->life[l].end);
3787 for (i = 0; i < vec_size(f->values); ++i) {
3788 const char *attr = "";
3790 ir_value *v = f->values[i];
3791 if (v->unique_life && v->locked)
3792 attr = "unique,locked ";
3793 else if (v->unique_life)
3797 oprintf("%s\t%s: %s %s %s%s@%i ", ind, v->name, type_name[v->vtype],
3798 storenames[v->store],
3799 attr, (v->callparam ? "callparam " : ""),
3800 (int)v->code.local);
3803 for (l = 0; l < vec_size(v->life); ++l) {
3804 oprintf("[%i,%i] ", v->life[l].start, v->life[l].end);
3807 for (m = 0; m < 3; ++m) {
3808 ir_value *vm = v->members[m];
3811 if (vm->unique_life && vm->locked)
3812 attr = "unique,locked ";
3813 else if (vm->unique_life)
3815 else if (vm->locked)
3817 oprintf("%s\t%s: %s@%i ", ind, vm->name, attr, (int)vm->code.local);
3818 for (l = 0; l < vec_size(vm->life); ++l) {
3819 oprintf("[%i,%i] ", vm->life[l].start, vm->life[l].end);
3824 if (vec_size(f->blocks))
3826 oprintf("%slife passes: %i\n", ind, (int)f->run_id);
3827 for (i = 0; i < vec_size(f->blocks); ++i) {
3828 ir_block_dump(f->blocks[i], ind, oprintf);
3832 ind[strlen(ind)-1] = 0;
3833 oprintf("%sendfunction %s\n", ind, f->name);
3836 void ir_block_dump(ir_block* b, char *ind,
3837 int (*oprintf)(const char*, ...))
3840 oprintf("%s:%s\n", ind, b->label);
3841 strncat(ind, "\t", IND_BUFSZ);
3843 if (b->instr && b->instr[0])
3844 oprintf("%s (%i) [entry]\n", ind, (int)(b->instr[0]->eid-1));
3845 for (i = 0; i < vec_size(b->instr); ++i)
3846 ir_instr_dump(b->instr[i], ind, oprintf);
3847 ind[strlen(ind)-1] = 0;
3850 void dump_phi(ir_instr *in, int (*oprintf)(const char*, ...))
3853 oprintf("%s <- phi ", in->_ops[0]->name);
3854 for (i = 0; i < vec_size(in->phi); ++i)
3856 oprintf("([%s] : %s) ", in->phi[i].from->label,
3857 in->phi[i].value->name);
3862 void ir_instr_dump(ir_instr *in, char *ind,
3863 int (*oprintf)(const char*, ...))
3866 const char *comma = NULL;
3868 oprintf("%s (%i) ", ind, (int)in->eid);
3870 if (in->opcode == VINSTR_PHI) {
3871 dump_phi(in, oprintf);
3875 strncat(ind, "\t", IND_BUFSZ);
3877 if (in->_ops[0] && (in->_ops[1] || in->_ops[2])) {
3878 ir_value_dump(in->_ops[0], oprintf);
3879 if (in->_ops[1] || in->_ops[2])
3882 if (in->opcode == INSTR_CALL0 || in->opcode == VINSTR_NRCALL) {
3883 oprintf("CALL%i\t", vec_size(in->params));
3885 oprintf("%s\t", qc_opname(in->opcode));
3887 if (in->_ops[0] && !(in->_ops[1] || in->_ops[2])) {
3888 ir_value_dump(in->_ops[0], oprintf);
3893 for (i = 1; i != 3; ++i) {
3897 ir_value_dump(in->_ops[i], oprintf);
3905 oprintf("[%s]", in->bops[0]->label);
3909 oprintf("%s[%s]", comma, in->bops[1]->label);
3910 if (vec_size(in->params)) {
3911 oprintf("\tparams: ");
3912 for (i = 0; i != vec_size(in->params); ++i) {
3913 oprintf("%s, ", in->params[i]->name);
3917 ind[strlen(ind)-1] = 0;
3920 void ir_value_dump_string(const char *str, int (*oprintf)(const char*, ...))
3923 for (; *str; ++str) {
3925 case '\n': oprintf("\\n"); break;
3926 case '\r': oprintf("\\r"); break;
3927 case '\t': oprintf("\\t"); break;
3928 case '\v': oprintf("\\v"); break;
3929 case '\f': oprintf("\\f"); break;
3930 case '\b': oprintf("\\b"); break;
3931 case '\a': oprintf("\\a"); break;
3932 case '\\': oprintf("\\\\"); break;
3933 case '"': oprintf("\\\""); break;
3934 default: oprintf("%c", *str); break;
3940 void ir_value_dump(ir_value* v, int (*oprintf)(const char*, ...))
3949 oprintf("fn:%s", v->name);
3952 oprintf("%g", v->constval.vfloat);
3955 oprintf("'%g %g %g'",
3958 v->constval.vvec.z);
3961 oprintf("(entity)");
3964 ir_value_dump_string(v->constval.vstring, oprintf);
3968 oprintf("%i", v->constval.vint);
3973 v->constval.vpointer->name);
3977 oprintf("%s", v->name);
3981 void ir_value_dump_life(const ir_value *self, int (*oprintf)(const char*,...))
3984 oprintf("Life of %12s:", self->name);
3985 for (i = 0; i < vec_size(self->life); ++i)
3987 oprintf(" + [%i, %i]\n", self->life[i].start, self->life[i].end);