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 AINSTR_END, /* struct */
87 AINSTR_END, /* union */
88 AINSTR_END, /* array */
90 AINSTR_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 AINSTR_END, /* struct */
111 AINSTR_END, /* union */
112 AINSTR_END, /* array */
113 AINSTR_END, /* nil */
114 AINSTR_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 AINSTR_END, /* struct */
135 AINSTR_END, /* union */
136 AINSTR_END, /* array */
137 AINSTR_END, /* nil */
138 AINSTR_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 AINSTR_END, /* struct */
159 AINSTR_END, /* union */
160 AINSTR_END, /* array */
161 AINSTR_END, /* nil */
162 AINSTR_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 AINSTR_END, /* struct */
183 AINSTR_END, /* union */
184 AINSTR_END, /* array */
185 AINSTR_END, /* nil */
186 AINSTR_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 AINSTR_END, /* struct */
207 AINSTR_END, /* union */
208 AINSTR_END, /* array */
209 AINSTR_END, /* nil */
210 AINSTR_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->code_function_def = -1;
489 self->allocated_locals = 0;
490 self->globaltemps = 0;
496 bool ir_function_set_name(ir_function *self, const char *name)
499 mem_d((void*)self->name);
500 self->name = util_strdup(name);
504 static void ir_function_delete_quick(ir_function *self)
507 mem_d((void*)self->name);
509 for (i = 0; i != vec_size(self->blocks); ++i)
510 ir_block_delete_quick(self->blocks[i]);
511 vec_free(self->blocks);
513 vec_free(self->params);
515 for (i = 0; i != vec_size(self->values); ++i)
516 ir_value_delete(self->values[i]);
517 vec_free(self->values);
519 for (i = 0; i != vec_size(self->locals); ++i)
520 ir_value_delete(self->locals[i]);
521 vec_free(self->locals);
523 /* self->value is deleted by the builder */
528 void ir_function_delete(ir_function *self)
531 mem_d((void*)self->name);
533 for (i = 0; i != vec_size(self->blocks); ++i)
534 ir_block_delete(self->blocks[i]);
535 vec_free(self->blocks);
537 vec_free(self->params);
539 for (i = 0; i != vec_size(self->values); ++i)
540 ir_value_delete(self->values[i]);
541 vec_free(self->values);
543 for (i = 0; i != vec_size(self->locals); ++i)
544 ir_value_delete(self->locals[i]);
545 vec_free(self->locals);
547 /* self->value is deleted by the builder */
552 void ir_function_collect_value(ir_function *self, ir_value *v)
554 vec_push(self->values, v);
557 ir_block* ir_function_create_block(lex_ctx ctx, ir_function *self, const char *label)
559 ir_block* bn = ir_block_new(self, label);
561 vec_push(self->blocks, bn);
565 static bool instr_is_operation(uint16_t op)
567 return ( (op >= INSTR_MUL_F && op <= INSTR_GT) ||
568 (op >= INSTR_LOAD_F && op <= INSTR_LOAD_FNC) ||
569 (op == INSTR_ADDRESS) ||
570 (op >= INSTR_NOT_F && op <= INSTR_NOT_FNC) ||
571 (op >= INSTR_AND && op <= INSTR_BITOR) ||
572 (op >= INSTR_CALL0 && op <= INSTR_CALL8) );
575 bool ir_function_pass_peephole(ir_function *self)
579 for (b = 0; b < vec_size(self->blocks); ++b) {
581 ir_block *block = self->blocks[b];
583 for (i = 0; i < vec_size(block->instr); ++i) {
585 inst = block->instr[i];
588 (inst->opcode >= INSTR_STORE_F &&
589 inst->opcode <= INSTR_STORE_FNC))
597 oper = block->instr[i-1];
598 if (!instr_is_operation(oper->opcode))
601 value = oper->_ops[0];
603 /* only do it for SSA values */
604 if (value->store != store_value)
607 /* don't optimize out the temp if it's used later again */
608 if (vec_size(value->reads) != 1)
611 /* The very next store must use this value */
612 if (value->reads[0] != store)
615 /* And of course the store must _read_ from it, so it's in
617 if (store->_ops[1] != value)
620 ++opts_optimizationcount[OPTIM_PEEPHOLE];
621 (void)!ir_instr_op(oper, 0, store->_ops[0], true);
623 vec_remove(block->instr, i, 1);
624 ir_instr_delete(store);
626 else if (inst->opcode == VINSTR_COND)
628 /* COND on a value resulting from a NOT could
629 * remove the NOT and swap its operands
636 value = inst->_ops[0];
638 if (value->store != store_value ||
639 vec_size(value->reads) != 1 ||
640 value->reads[0] != inst)
645 inot = value->writes[0];
646 if (inot->_ops[0] != value ||
647 inot->opcode < INSTR_NOT_F ||
648 inot->opcode > INSTR_NOT_FNC ||
649 inot->opcode == INSTR_NOT_V || /* can't do these */
650 inot->opcode == INSTR_NOT_S)
656 ++opts_optimizationcount[OPTIM_PEEPHOLE];
658 (void)!ir_instr_op(inst, 0, inot->_ops[1], false);
661 for (inotid = 0; inotid < vec_size(tmp->instr); ++inotid) {
662 if (tmp->instr[inotid] == inot)
665 if (inotid >= vec_size(tmp->instr)) {
666 compile_error(inst->context, "sanity-check failed: failed to find instruction to optimize out");
669 vec_remove(tmp->instr, inotid, 1);
670 ir_instr_delete(inot);
671 /* swap ontrue/onfalse */
673 inst->bops[0] = inst->bops[1];
684 bool ir_function_pass_tailrecursion(ir_function *self)
688 for (b = 0; b < vec_size(self->blocks); ++b) {
690 ir_instr *ret, *call, *store = NULL;
691 ir_block *block = self->blocks[b];
693 if (!block->final || vec_size(block->instr) < 2)
696 ret = block->instr[vec_size(block->instr)-1];
697 if (ret->opcode != INSTR_DONE && ret->opcode != INSTR_RETURN)
700 call = block->instr[vec_size(block->instr)-2];
701 if (call->opcode >= INSTR_STORE_F && call->opcode <= INSTR_STORE_FNC) {
702 /* account for the unoptimized
704 * STORE %return, %tmp
708 if (vec_size(block->instr) < 3)
712 call = block->instr[vec_size(block->instr)-3];
715 if (call->opcode < INSTR_CALL0 || call->opcode > INSTR_CALL8)
719 /* optimize out the STORE */
721 ret->_ops[0] == store->_ops[0] &&
722 store->_ops[1] == call->_ops[0])
724 ++opts_optimizationcount[OPTIM_PEEPHOLE];
725 call->_ops[0] = store->_ops[0];
726 vec_remove(block->instr, vec_size(block->instr) - 2, 1);
727 ir_instr_delete(store);
736 funcval = call->_ops[1];
739 if (funcval->vtype != TYPE_FUNCTION || funcval->constval.vfunc != self)
742 /* now we have a CALL and a RET, check if it's a tailcall */
743 if (ret->_ops[0] && call->_ops[0] != ret->_ops[0])
746 ++opts_optimizationcount[OPTIM_TAIL_RECURSION];
747 vec_shrinkby(block->instr, 2);
749 block->final = false; /* open it back up */
751 /* emite parameter-stores */
752 for (p = 0; p < vec_size(call->params); ++p) {
753 /* assert(call->params_count <= self->locals_count); */
754 if (!ir_block_create_store(block, call->context, self->locals[p], call->params[p])) {
755 irerror(call->context, "failed to create tailcall store instruction for parameter %i", (int)p);
759 if (!ir_block_create_jump(block, call->context, self->blocks[0])) {
760 irerror(call->context, "failed to create tailcall jump");
764 ir_instr_delete(call);
765 ir_instr_delete(ret);
771 bool ir_function_finalize(ir_function *self)
778 if (OPTS_OPTIMIZATION(OPTIM_PEEPHOLE)) {
779 if (!ir_function_pass_peephole(self)) {
780 irerror(self->context, "generic optimization pass broke something in `%s`", self->name);
785 if (OPTS_OPTIMIZATION(OPTIM_TAIL_RECURSION)) {
786 if (!ir_function_pass_tailrecursion(self)) {
787 irerror(self->context, "tail-recursion optimization pass broke something in `%s`", self->name);
792 if (!ir_function_naive_phi(self)) {
793 irerror(self->context, "internal error: ir_function_naive_phi failed");
797 for (i = 0; i < vec_size(self->locals); ++i) {
798 ir_value *v = self->locals[i];
799 if (v->vtype == TYPE_VECTOR ||
800 (v->vtype == TYPE_FIELD && v->outtype == TYPE_VECTOR))
802 ir_value_vector_member(v, 0);
803 ir_value_vector_member(v, 1);
804 ir_value_vector_member(v, 2);
807 for (i = 0; i < vec_size(self->values); ++i) {
808 ir_value *v = self->values[i];
809 if (v->vtype == TYPE_VECTOR ||
810 (v->vtype == TYPE_FIELD && v->outtype == TYPE_VECTOR))
812 ir_value_vector_member(v, 0);
813 ir_value_vector_member(v, 1);
814 ir_value_vector_member(v, 2);
818 ir_function_enumerate(self);
820 if (!ir_function_calculate_liferanges(self))
822 if (!ir_function_allocate_locals(self))
827 ir_value* ir_function_create_local(ir_function *self, const char *name, int vtype, bool param)
832 vec_size(self->locals) &&
833 self->locals[vec_size(self->locals)-1]->store != store_param) {
834 irerror(self->context, "cannot add parameters after adding locals");
838 ve = ir_value_var(name, (param ? store_param : store_local), vtype);
841 vec_push(self->locals, ve);
845 /***********************************************************************
849 ir_block* ir_block_new(ir_function* owner, const char *name)
852 self = (ir_block*)mem_a(sizeof(*self));
856 memset(self, 0, sizeof(*self));
859 if (name && !ir_block_set_label(self, name)) {
864 self->context.file = "<@no context>";
865 self->context.line = 0;
869 self->entries = NULL;
873 self->is_return = false;
878 self->generated = false;
883 static void ir_block_delete_quick(ir_block* self)
886 if (self->label) mem_d(self->label);
887 for (i = 0; i != vec_size(self->instr); ++i)
888 ir_instr_delete_quick(self->instr[i]);
889 vec_free(self->instr);
890 vec_free(self->entries);
891 vec_free(self->exits);
892 vec_free(self->living);
896 void ir_block_delete(ir_block* self)
899 if (self->label) mem_d(self->label);
900 for (i = 0; i != vec_size(self->instr); ++i)
901 ir_instr_delete(self->instr[i]);
902 vec_free(self->instr);
903 vec_free(self->entries);
904 vec_free(self->exits);
905 vec_free(self->living);
909 bool ir_block_set_label(ir_block *self, const char *name)
912 mem_d((void*)self->label);
913 self->label = util_strdup(name);
914 return !!self->label;
917 /***********************************************************************
921 ir_instr* ir_instr_new(lex_ctx ctx, ir_block* owner, int op)
924 self = (ir_instr*)mem_a(sizeof(*self));
931 self->_ops[0] = NULL;
932 self->_ops[1] = NULL;
933 self->_ops[2] = NULL;
934 self->bops[0] = NULL;
935 self->bops[1] = NULL;
946 static void ir_instr_delete_quick(ir_instr *self)
949 vec_free(self->params);
953 void ir_instr_delete(ir_instr *self)
956 /* The following calls can only delete from
957 * vectors, we still want to delete this instruction
958 * so ignore the return value. Since with the warn_unused_result attribute
959 * gcc doesn't care about an explicit: (void)foo(); to ignore the result,
960 * I have to improvise here and use if(foo());
962 for (i = 0; i < vec_size(self->phi); ++i) {
964 if (vec_ir_instr_find(self->phi[i].value->writes, self, &idx))
965 vec_remove(self->phi[i].value->writes, idx, 1);
966 if (vec_ir_instr_find(self->phi[i].value->reads, self, &idx))
967 vec_remove(self->phi[i].value->reads, idx, 1);
970 for (i = 0; i < vec_size(self->params); ++i) {
972 if (vec_ir_instr_find(self->params[i]->writes, self, &idx))
973 vec_remove(self->params[i]->writes, idx, 1);
974 if (vec_ir_instr_find(self->params[i]->reads, self, &idx))
975 vec_remove(self->params[i]->reads, idx, 1);
977 vec_free(self->params);
978 (void)!ir_instr_op(self, 0, NULL, false);
979 (void)!ir_instr_op(self, 1, NULL, false);
980 (void)!ir_instr_op(self, 2, NULL, false);
984 bool ir_instr_op(ir_instr *self, int op, ir_value *v, bool writing)
986 if (self->_ops[op]) {
988 if (writing && vec_ir_instr_find(self->_ops[op]->writes, self, &idx))
989 vec_remove(self->_ops[op]->writes, idx, 1);
990 else if (vec_ir_instr_find(self->_ops[op]->reads, self, &idx))
991 vec_remove(self->_ops[op]->reads, idx, 1);
995 vec_push(v->writes, self);
997 vec_push(v->reads, self);
1003 /***********************************************************************
1007 void ir_value_code_setaddr(ir_value *self, int32_t gaddr)
1009 self->code.globaladdr = gaddr;
1010 if (self->members[0]) self->members[0]->code.globaladdr = gaddr;
1011 if (self->members[1]) self->members[1]->code.globaladdr = gaddr;
1012 if (self->members[2]) self->members[2]->code.globaladdr = gaddr;
1015 int32_t ir_value_code_addr(const ir_value *self)
1017 if (self->store == store_return)
1018 return OFS_RETURN + self->code.addroffset;
1019 return self->code.globaladdr + self->code.addroffset;
1022 ir_value* ir_value_var(const char *name, int storetype, int vtype)
1025 self = (ir_value*)mem_a(sizeof(*self));
1026 self->vtype = vtype;
1027 self->fieldtype = TYPE_VOID;
1028 self->outtype = TYPE_VOID;
1029 self->store = storetype;
1033 self->writes = NULL;
1035 self->cvq = CV_NONE;
1036 self->hasvalue = false;
1037 self->context.file = "<@no context>";
1038 self->context.line = 0;
1040 if (name && !ir_value_set_name(self, name)) {
1041 irerror(self->context, "out of memory");
1046 memset(&self->constval, 0, sizeof(self->constval));
1047 memset(&self->code, 0, sizeof(self->code));
1049 self->members[0] = NULL;
1050 self->members[1] = NULL;
1051 self->members[2] = NULL;
1052 self->memberof = NULL;
1054 self->unique_life = false;
1055 self->locked = false;
1056 self->callparam = false;
1062 ir_value* ir_value_vector_member(ir_value *self, unsigned int member)
1070 if (self->members[member])
1071 return self->members[member];
1074 len = strlen(self->name);
1075 name = (char*)mem_a(len + 3);
1076 memcpy(name, self->name, len);
1078 name[len+1] = 'x' + member;
1084 if (self->vtype == TYPE_VECTOR)
1086 m = ir_value_var(name, self->store, TYPE_FLOAT);
1091 m->context = self->context;
1093 self->members[member] = m;
1094 m->code.addroffset = member;
1096 else if (self->vtype == TYPE_FIELD)
1098 if (self->fieldtype != TYPE_VECTOR)
1100 m = ir_value_var(name, self->store, TYPE_FIELD);
1105 m->fieldtype = TYPE_FLOAT;
1106 m->context = self->context;
1108 self->members[member] = m;
1109 m->code.addroffset = member;
1113 irerror(self->context, "invalid member access on %s", self->name);
1121 static GMQCC_INLINE size_t ir_value_sizeof(const ir_value *self)
1123 if (self->vtype == TYPE_FIELD && self->fieldtype == TYPE_VECTOR)
1124 return type_sizeof_[TYPE_VECTOR];
1125 return type_sizeof_[self->vtype];
1128 ir_value* ir_value_out(ir_function *owner, const char *name, int storetype, int vtype)
1130 ir_value *v = ir_value_var(name, storetype, vtype);
1133 ir_function_collect_value(owner, v);
1137 void ir_value_delete(ir_value* self)
1141 mem_d((void*)self->name);
1144 if (self->vtype == TYPE_STRING)
1145 mem_d((void*)self->constval.vstring);
1147 for (i = 0; i < 3; ++i) {
1148 if (self->members[i])
1149 ir_value_delete(self->members[i]);
1151 vec_free(self->reads);
1152 vec_free(self->writes);
1153 vec_free(self->life);
1157 bool ir_value_set_name(ir_value *self, const char *name)
1160 mem_d((void*)self->name);
1161 self->name = util_strdup(name);
1162 return !!self->name;
1165 bool ir_value_set_float(ir_value *self, float f)
1167 if (self->vtype != TYPE_FLOAT)
1169 self->constval.vfloat = f;
1170 self->hasvalue = true;
1174 bool ir_value_set_func(ir_value *self, int f)
1176 if (self->vtype != TYPE_FUNCTION)
1178 self->constval.vint = f;
1179 self->hasvalue = true;
1183 bool ir_value_set_vector(ir_value *self, vector v)
1185 if (self->vtype != TYPE_VECTOR)
1187 self->constval.vvec = v;
1188 self->hasvalue = true;
1192 bool ir_value_set_field(ir_value *self, ir_value *fld)
1194 if (self->vtype != TYPE_FIELD)
1196 self->constval.vpointer = fld;
1197 self->hasvalue = true;
1201 static char *ir_strdup(const char *str)
1204 /* actually dup empty strings */
1205 char *out = (char*)mem_a(1);
1209 return util_strdup(str);
1212 bool ir_value_set_string(ir_value *self, const char *str)
1214 if (self->vtype != TYPE_STRING)
1216 self->constval.vstring = ir_strdup(str);
1217 self->hasvalue = true;
1222 bool ir_value_set_int(ir_value *self, int i)
1224 if (self->vtype != TYPE_INTEGER)
1226 self->constval.vint = i;
1227 self->hasvalue = true;
1232 bool ir_value_lives(ir_value *self, size_t at)
1235 for (i = 0; i < vec_size(self->life); ++i)
1237 ir_life_entry_t *life = &self->life[i];
1238 if (life->start <= at && at <= life->end)
1240 if (life->start > at) /* since it's ordered */
1246 bool ir_value_life_insert(ir_value *self, size_t idx, ir_life_entry_t e)
1249 vec_push(self->life, e);
1250 for (k = vec_size(self->life)-1; k > idx; --k)
1251 self->life[k] = self->life[k-1];
1252 self->life[idx] = e;
1256 bool ir_value_life_merge(ir_value *self, size_t s)
1259 ir_life_entry_t *life = NULL;
1260 ir_life_entry_t *before = NULL;
1261 ir_life_entry_t new_entry;
1263 /* Find the first range >= s */
1264 for (i = 0; i < vec_size(self->life); ++i)
1267 life = &self->life[i];
1268 if (life->start > s)
1271 /* nothing found? append */
1272 if (i == vec_size(self->life)) {
1274 if (life && life->end+1 == s)
1276 /* previous life range can be merged in */
1280 if (life && life->end >= s)
1282 e.start = e.end = s;
1283 vec_push(self->life, e);
1289 if (before->end + 1 == s &&
1290 life->start - 1 == s)
1293 before->end = life->end;
1294 vec_remove(self->life, i, 1);
1297 if (before->end + 1 == s)
1303 /* already contained */
1304 if (before->end >= s)
1308 if (life->start - 1 == s)
1313 /* insert a new entry */
1314 new_entry.start = new_entry.end = s;
1315 return ir_value_life_insert(self, i, new_entry);
1318 bool ir_value_life_merge_into(ir_value *self, const ir_value *other)
1322 if (!vec_size(other->life))
1325 if (!vec_size(self->life)) {
1326 size_t count = vec_size(other->life);
1327 ir_life_entry_t *life = vec_add(self->life, count);
1328 memcpy(life, other->life, count * sizeof(*life));
1333 for (i = 0; i < vec_size(other->life); ++i)
1335 const ir_life_entry_t *life = &other->life[i];
1338 ir_life_entry_t *entry = &self->life[myi];
1340 if (life->end+1 < entry->start)
1342 /* adding an interval before entry */
1343 if (!ir_value_life_insert(self, myi, *life))
1349 if (life->start < entry->start &&
1350 life->end+1 >= entry->start)
1352 /* starts earlier and overlaps */
1353 entry->start = life->start;
1356 if (life->end > entry->end &&
1357 life->start <= entry->end+1)
1359 /* ends later and overlaps */
1360 entry->end = life->end;
1363 /* see if our change combines it with the next ranges */
1364 while (myi+1 < vec_size(self->life) &&
1365 entry->end+1 >= self->life[1+myi].start)
1367 /* overlaps with (myi+1) */
1368 if (entry->end < self->life[1+myi].end)
1369 entry->end = self->life[1+myi].end;
1370 vec_remove(self->life, myi+1, 1);
1371 entry = &self->life[myi];
1374 /* see if we're after the entry */
1375 if (life->start > entry->end)
1378 /* append if we're at the end */
1379 if (myi >= vec_size(self->life)) {
1380 vec_push(self->life, *life);
1383 /* otherweise check the next range */
1392 bool ir_values_overlap(const ir_value *a, const ir_value *b)
1394 /* For any life entry in A see if it overlaps with
1395 * any life entry in B.
1396 * Note that the life entries are orderes, so we can make a
1397 * more efficient algorithm there than naively translating the
1401 ir_life_entry_t *la, *lb, *enda, *endb;
1403 /* first of all, if either has no life range, they cannot clash */
1404 if (!vec_size(a->life) || !vec_size(b->life))
1409 enda = la + vec_size(a->life);
1410 endb = lb + vec_size(b->life);
1413 /* check if the entries overlap, for that,
1414 * both must start before the other one ends.
1416 if (la->start < lb->end &&
1417 lb->start < la->end)
1422 /* entries are ordered
1423 * one entry is earlier than the other
1424 * that earlier entry will be moved forward
1426 if (la->start < lb->start)
1428 /* order: A B, move A forward
1429 * check if we hit the end with A
1434 else /* if (lb->start < la->start) actually <= */
1436 /* order: B A, move B forward
1437 * check if we hit the end with B
1446 /***********************************************************************
1450 static bool ir_check_unreachable(ir_block *self)
1452 /* The IR should never have to deal with unreachable code */
1453 if (!self->final/* || OPTS_FLAG(ALLOW_UNREACHABLE_CODE)*/)
1455 irerror(self->context, "unreachable statement (%s)", self->label);
1459 bool ir_block_create_store_op(ir_block *self, lex_ctx ctx, int op, ir_value *target, ir_value *what)
1462 if (!ir_check_unreachable(self))
1465 if (target->store == store_value &&
1466 (op < INSTR_STOREP_F || op > INSTR_STOREP_FNC))
1468 irerror(self->context, "cannot store to an SSA value");
1469 irerror(self->context, "trying to store: %s <- %s", target->name, what->name);
1470 irerror(self->context, "instruction: %s", asm_instr[op].m);
1474 in = ir_instr_new(ctx, self, op);
1478 if (!ir_instr_op(in, 0, target, (op < INSTR_STOREP_F || op > INSTR_STOREP_FNC)) ||
1479 !ir_instr_op(in, 1, what, false))
1481 ir_instr_delete(in);
1484 vec_push(self->instr, in);
1488 bool ir_block_create_store(ir_block *self, lex_ctx ctx, ir_value *target, ir_value *what)
1492 if (target->vtype == TYPE_VARIANT)
1493 vtype = what->vtype;
1495 vtype = target->vtype;
1498 if (vtype == TYPE_FLOAT && what->vtype == TYPE_INTEGER)
1499 op = INSTR_CONV_ITOF;
1500 else if (vtype == TYPE_INTEGER && what->vtype == TYPE_FLOAT)
1501 op = INSTR_CONV_FTOI;
1503 op = type_store_instr[vtype];
1505 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS)) {
1506 if (op == INSTR_STORE_FLD && what->fieldtype == TYPE_VECTOR)
1510 return ir_block_create_store_op(self, ctx, op, target, what);
1513 bool ir_block_create_storep(ir_block *self, lex_ctx ctx, ir_value *target, ir_value *what)
1518 if (target->vtype != TYPE_POINTER)
1521 /* storing using pointer - target is a pointer, type must be
1522 * inferred from source
1524 vtype = what->vtype;
1526 op = type_storep_instr[vtype];
1527 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS)) {
1528 if (op == INSTR_STOREP_FLD && what->fieldtype == TYPE_VECTOR)
1529 op = INSTR_STOREP_V;
1532 return ir_block_create_store_op(self, ctx, op, target, what);
1535 bool ir_block_create_return(ir_block *self, lex_ctx ctx, ir_value *v)
1538 if (!ir_check_unreachable(self))
1541 self->is_return = true;
1542 in = ir_instr_new(ctx, self, INSTR_RETURN);
1546 if (v && !ir_instr_op(in, 0, v, false)) {
1547 ir_instr_delete(in);
1551 vec_push(self->instr, in);
1555 bool ir_block_create_if(ir_block *self, lex_ctx ctx, ir_value *v,
1556 ir_block *ontrue, ir_block *onfalse)
1559 if (!ir_check_unreachable(self))
1562 /*in = ir_instr_new(ctx, self, (v->vtype == TYPE_STRING ? INSTR_IF_S : INSTR_IF_F));*/
1563 in = ir_instr_new(ctx, self, VINSTR_COND);
1567 if (!ir_instr_op(in, 0, v, false)) {
1568 ir_instr_delete(in);
1572 in->bops[0] = ontrue;
1573 in->bops[1] = onfalse;
1575 vec_push(self->instr, in);
1577 vec_push(self->exits, ontrue);
1578 vec_push(self->exits, onfalse);
1579 vec_push(ontrue->entries, self);
1580 vec_push(onfalse->entries, self);
1584 bool ir_block_create_jump(ir_block *self, lex_ctx ctx, ir_block *to)
1587 if (!ir_check_unreachable(self))
1590 in = ir_instr_new(ctx, self, VINSTR_JUMP);
1595 vec_push(self->instr, in);
1597 vec_push(self->exits, to);
1598 vec_push(to->entries, self);
1602 bool ir_block_create_goto(ir_block *self, lex_ctx ctx, ir_block *to)
1604 self->owner->flags |= IR_FLAG_HAS_GOTO;
1605 return ir_block_create_jump(self, ctx, to);
1608 ir_instr* ir_block_create_phi(ir_block *self, lex_ctx ctx, const char *label, int ot)
1612 if (!ir_check_unreachable(self))
1614 in = ir_instr_new(ctx, self, VINSTR_PHI);
1617 out = ir_value_out(self->owner, label, store_value, ot);
1619 ir_instr_delete(in);
1622 if (!ir_instr_op(in, 0, out, true)) {
1623 ir_instr_delete(in);
1624 ir_value_delete(out);
1627 vec_push(self->instr, in);
1631 ir_value* ir_phi_value(ir_instr *self)
1633 return self->_ops[0];
1636 void ir_phi_add(ir_instr* self, ir_block *b, ir_value *v)
1640 if (!vec_ir_block_find(self->owner->entries, b, NULL)) {
1641 /* Must not be possible to cause this, otherwise the AST
1642 * is doing something wrong.
1644 irerror(self->context, "Invalid entry block for PHI");
1650 vec_push(v->reads, self);
1651 vec_push(self->phi, pe);
1654 /* call related code */
1655 ir_instr* ir_block_create_call(ir_block *self, lex_ctx ctx, const char *label, ir_value *func, bool noreturn)
1659 if (!ir_check_unreachable(self))
1661 in = ir_instr_new(ctx, self, (noreturn ? VINSTR_NRCALL : INSTR_CALL0));
1666 self->is_return = true;
1668 out = ir_value_out(self->owner, label, (func->outtype == TYPE_VOID) ? store_return : store_value, func->outtype);
1670 ir_instr_delete(in);
1673 if (!ir_instr_op(in, 0, out, true) ||
1674 !ir_instr_op(in, 1, func, false))
1676 ir_instr_delete(in);
1677 ir_value_delete(out);
1680 vec_push(self->instr, in);
1683 if (!ir_block_create_return(self, ctx, NULL)) {
1684 compile_error(ctx, "internal error: failed to generate dummy-return instruction");
1685 ir_instr_delete(in);
1693 ir_value* ir_call_value(ir_instr *self)
1695 return self->_ops[0];
1698 void ir_call_param(ir_instr* self, ir_value *v)
1700 vec_push(self->params, v);
1701 vec_push(v->reads, self);
1704 /* binary op related code */
1706 ir_value* ir_block_create_binop(ir_block *self, lex_ctx ctx,
1707 const char *label, int opcode,
1708 ir_value *left, ir_value *right)
1730 case INSTR_SUB_S: /* -- offset of string as float */
1735 case INSTR_BITOR_IF:
1736 case INSTR_BITOR_FI:
1737 case INSTR_BITAND_FI:
1738 case INSTR_BITAND_IF:
1753 case INSTR_BITAND_I:
1756 case INSTR_RSHIFT_I:
1757 case INSTR_LSHIFT_I:
1779 /* boolean operations result in floats */
1780 if (opcode >= INSTR_EQ_F && opcode <= INSTR_GT)
1782 else if (opcode >= INSTR_LE && opcode <= INSTR_GT)
1785 else if (opcode >= INSTR_LE_I && opcode <= INSTR_EQ_FI)
1790 if (ot == TYPE_VOID) {
1791 /* The AST or parser were supposed to check this! */
1795 return ir_block_create_general_instr(self, ctx, label, opcode, left, right, ot);
1798 ir_value* ir_block_create_unary(ir_block *self, lex_ctx ctx,
1799 const char *label, int opcode,
1802 int ot = TYPE_FLOAT;
1814 /* QC doesn't have other unary operations. We expect extensions to fill
1815 * the above list, otherwise we assume out-type = in-type, eg for an
1819 ot = operand->vtype;
1822 if (ot == TYPE_VOID) {
1823 /* The AST or parser were supposed to check this! */
1827 /* let's use the general instruction creator and pass NULL for OPB */
1828 return ir_block_create_general_instr(self, ctx, label, opcode, operand, NULL, ot);
1831 ir_value* ir_block_create_general_instr(ir_block *self, lex_ctx ctx, const char *label,
1832 int op, ir_value *a, ir_value *b, int outype)
1837 out = ir_value_out(self->owner, label, store_value, outype);
1841 instr = ir_instr_new(ctx, self, op);
1843 ir_value_delete(out);
1847 if (!ir_instr_op(instr, 0, out, true) ||
1848 !ir_instr_op(instr, 1, a, false) ||
1849 !ir_instr_op(instr, 2, b, false) )
1854 vec_push(self->instr, instr);
1858 ir_instr_delete(instr);
1859 ir_value_delete(out);
1863 ir_value* ir_block_create_fieldaddress(ir_block *self, lex_ctx ctx, const char *label, ir_value *ent, ir_value *field)
1867 /* Support for various pointer types todo if so desired */
1868 if (ent->vtype != TYPE_ENTITY)
1871 if (field->vtype != TYPE_FIELD)
1874 v = ir_block_create_general_instr(self, ctx, label, INSTR_ADDRESS, ent, field, TYPE_POINTER);
1875 v->fieldtype = field->fieldtype;
1879 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)
1882 if (ent->vtype != TYPE_ENTITY)
1885 /* at some point we could redirect for TYPE_POINTER... but that could lead to carelessness */
1886 if (field->vtype != TYPE_FIELD)
1891 case TYPE_FLOAT: op = INSTR_LOAD_F; break;
1892 case TYPE_VECTOR: op = INSTR_LOAD_V; break;
1893 case TYPE_STRING: op = INSTR_LOAD_S; break;
1894 case TYPE_FIELD: op = INSTR_LOAD_FLD; break;
1895 case TYPE_ENTITY: op = INSTR_LOAD_ENT; break;
1896 case TYPE_FUNCTION: op = INSTR_LOAD_FNC; break;
1898 case TYPE_POINTER: op = INSTR_LOAD_I; break;
1899 case TYPE_INTEGER: op = INSTR_LOAD_I; break;
1902 irerror(self->context, "invalid type for ir_block_create_load_from_ent: %s", type_name[outype]);
1906 return ir_block_create_general_instr(self, ctx, label, op, ent, field, outype);
1909 /* PHI resolving breaks the SSA, and must thus be the last
1910 * step before life-range calculation.
1913 static bool ir_block_naive_phi(ir_block *self);
1914 bool ir_function_naive_phi(ir_function *self)
1918 for (i = 0; i < vec_size(self->blocks); ++i)
1920 if (!ir_block_naive_phi(self->blocks[i]))
1926 static bool ir_block_naive_phi(ir_block *self)
1928 size_t i, p; /*, w;*/
1929 /* FIXME: optionally, create_phi can add the phis
1930 * to a list so we don't need to loop through blocks
1931 * - anyway: "don't optimize YET"
1933 for (i = 0; i < vec_size(self->instr); ++i)
1935 ir_instr *instr = self->instr[i];
1936 if (instr->opcode != VINSTR_PHI)
1939 vec_remove(self->instr, i, 1);
1940 --i; /* NOTE: i+1 below */
1942 for (p = 0; p < vec_size(instr->phi); ++p)
1944 ir_value *v = instr->phi[p].value;
1945 ir_block *b = instr->phi[p].from;
1947 if (v->store == store_value &&
1948 vec_size(v->reads) == 1 &&
1949 vec_size(v->writes) == 1)
1951 /* replace the value */
1952 if (!ir_instr_op(v->writes[0], 0, instr->_ops[0], true))
1957 /* force a move instruction */
1958 ir_instr *prevjump = vec_last(b->instr);
1961 instr->_ops[0]->store = store_global;
1962 if (!ir_block_create_store(b, instr->context, instr->_ops[0], v))
1964 instr->_ops[0]->store = store_value;
1965 vec_push(b->instr, prevjump);
1969 ir_instr_delete(instr);
1974 /***********************************************************************
1975 *IR Temp allocation code
1976 * Propagating value life ranges by walking through the function backwards
1977 * until no more changes are made.
1978 * In theory this should happen once more than once for every nested loop
1980 * Though this implementation might run an additional time for if nests.
1983 /* Enumerate instructions used by value's life-ranges
1985 static void ir_block_enumerate(ir_block *self, size_t *_eid)
1989 for (i = 0; i < vec_size(self->instr); ++i)
1991 self->instr[i]->eid = eid++;
1996 /* Enumerate blocks and instructions.
1997 * The block-enumeration is unordered!
1998 * We do not really use the block enumreation, however
1999 * the instruction enumeration is important for life-ranges.
2001 void ir_function_enumerate(ir_function *self)
2004 size_t instruction_id = 0;
2005 for (i = 0; i < vec_size(self->blocks); ++i)
2007 /* each block now gets an additional "entry" instruction id
2008 * we can use to avoid point-life issues
2010 self->blocks[i]->entry_id = instruction_id;
2013 self->blocks[i]->eid = i;
2014 self->blocks[i]->run_id = 0;
2015 ir_block_enumerate(self->blocks[i], &instruction_id);
2019 static bool ir_block_life_propagate(ir_block *b, ir_block *prev, bool *changed);
2020 bool ir_function_calculate_liferanges(ir_function *self)
2025 /* parameters live at 0 */
2026 for (i = 0; i < vec_size(self->params); ++i)
2027 ir_value_life_merge(self->locals[i], 0);
2032 for (i = 0; i != vec_size(self->blocks); ++i)
2034 if (self->blocks[i]->is_return)
2036 vec_free(self->blocks[i]->living);
2037 if (!ir_block_life_propagate(self->blocks[i], NULL, &changed))
2042 if (vec_size(self->blocks)) {
2043 ir_block *block = self->blocks[0];
2044 for (i = 0; i < vec_size(block->living); ++i) {
2045 ir_value *v = block->living[i];
2046 if (v->store != store_local)
2048 if (v->vtype == TYPE_VECTOR)
2050 self->flags |= IR_FLAG_HAS_UNINITIALIZED;
2051 /* find the instruction reading from it */
2052 for (s = 0; s < vec_size(v->reads); ++s) {
2053 if (v->reads[s]->eid == v->life[0].end)
2056 if (s < vec_size(v->reads)) {
2057 if (irwarning(v->context, WARN_USED_UNINITIALIZED,
2058 "variable `%s` may be used uninitialized in this function\n"
2061 v->reads[s]->context.file, v->reads[s]->context.line)
2069 ir_value *vec = v->memberof;
2070 for (s = 0; s < vec_size(vec->reads); ++s) {
2071 if (vec->reads[s]->eid == v->life[0].end)
2074 if (s < vec_size(vec->reads)) {
2075 if (irwarning(v->context, WARN_USED_UNINITIALIZED,
2076 "variable `%s` may be used uninitialized in this function\n"
2079 vec->reads[s]->context.file, vec->reads[s]->context.line)
2087 if (irwarning(v->context, WARN_USED_UNINITIALIZED,
2088 "variable `%s` may be used uninitialized in this function", v->name))
2097 /* Local-value allocator
2098 * After finishing creating the liferange of all values used in a function
2099 * we can allocate their global-positions.
2100 * This is the counterpart to register-allocation in register machines.
2107 } function_allocator;
2109 static bool function_allocator_alloc(function_allocator *alloc, ir_value *var)
2112 size_t vsize = ir_value_sizeof(var);
2114 var->code.local = vec_size(alloc->locals);
2116 slot = ir_value_var("reg", store_global, var->vtype);
2120 if (!ir_value_life_merge_into(slot, var))
2123 vec_push(alloc->locals, slot);
2124 vec_push(alloc->sizes, vsize);
2125 vec_push(alloc->unique, var->unique_life);
2130 ir_value_delete(slot);
2134 static bool ir_function_allocator_assign(ir_function *self, function_allocator *alloc, ir_value *v)
2140 return function_allocator_alloc(alloc, v);
2142 for (a = 0; a < vec_size(alloc->locals); ++a)
2144 /* if it's reserved for a unique liferange: skip */
2145 if (alloc->unique[a])
2148 slot = alloc->locals[a];
2150 /* never resize parameters
2151 * will be required later when overlapping temps + locals
2153 if (a < vec_size(self->params) &&
2154 alloc->sizes[a] < ir_value_sizeof(v))
2159 if (ir_values_overlap(v, slot))
2162 if (!ir_value_life_merge_into(slot, v))
2165 /* adjust size for this slot */
2166 if (alloc->sizes[a] < ir_value_sizeof(v))
2167 alloc->sizes[a] = ir_value_sizeof(v);
2172 if (a >= vec_size(alloc->locals)) {
2173 if (!function_allocator_alloc(alloc, v))
2179 bool ir_function_allocate_locals(ir_function *self)
2184 bool opt_gt = OPTS_OPTIMIZATION(OPTIM_GLOBAL_TEMPS);
2188 function_allocator lockalloc, globalloc;
2190 if (!vec_size(self->locals) && !vec_size(self->values))
2193 globalloc.locals = NULL;
2194 globalloc.sizes = NULL;
2195 globalloc.positions = NULL;
2196 globalloc.unique = NULL;
2197 lockalloc.locals = NULL;
2198 lockalloc.sizes = NULL;
2199 lockalloc.positions = NULL;
2200 lockalloc.unique = NULL;
2202 for (i = 0; i < vec_size(self->locals); ++i)
2204 v = self->locals[i];
2205 if (!OPTS_OPTIMIZATION(OPTIM_LOCAL_TEMPS)) {
2207 v->unique_life = true;
2209 else if (i >= vec_size(self->params))
2212 v->locked = true; /* lock parameters locals */
2213 if (!function_allocator_alloc((v->locked || !opt_gt ? &lockalloc : &globalloc), v))
2216 for (; i < vec_size(self->locals); ++i)
2218 v = self->locals[i];
2219 if (!vec_size(v->life))
2221 if (!ir_function_allocator_assign(self, (v->locked || !opt_gt ? &lockalloc : &globalloc), v))
2225 /* Allocate a slot for any value that still exists */
2226 for (i = 0; i < vec_size(self->values); ++i)
2228 v = self->values[i];
2230 if (!vec_size(v->life))
2233 /* CALL optimization:
2234 * If the value is a parameter-temp: 1 write, 1 read from a CALL
2235 * and it's not "locked", write it to the OFS_PARM directly.
2237 if (OPTS_OPTIMIZATION(OPTIM_CALL_STORES) && !v->locked && !v->unique_life) {
2238 if (vec_size(v->reads) == 1 && vec_size(v->writes) == 1 &&
2239 (v->reads[0]->opcode == VINSTR_NRCALL ||
2240 (v->reads[0]->opcode >= INSTR_CALL0 && v->reads[0]->opcode <= INSTR_CALL8)
2245 ir_instr *call = v->reads[0];
2246 if (!vec_ir_value_find(call->params, v, ¶m)) {
2247 irerror(call->context, "internal error: unlocked parameter %s not found", v->name);
2251 ++opts_optimizationcount[OPTIM_CALL_STORES];
2252 v->callparam = true;
2254 ir_value_code_setaddr(v, OFS_PARM0 + 3*param);
2258 if (vec_size(self->owner->extparam_protos) <= param)
2259 ep = ir_gen_extparam_proto(self->owner);
2261 ep = self->owner->extparam_protos[param];
2262 ir_instr_op(v->writes[0], 0, ep, true);
2263 call->params[param+8] = ep;
2267 if (vec_size(v->writes) == 1 && v->writes[0]->opcode == INSTR_CALL0)
2269 v->store = store_return;
2270 if (v->members[0]) v->members[0]->store = store_return;
2271 if (v->members[1]) v->members[1]->store = store_return;
2272 if (v->members[2]) v->members[2]->store = store_return;
2273 ++opts_optimizationcount[OPTIM_CALL_STORES];
2278 if (!ir_function_allocator_assign(self, (v->locked || !opt_gt ? &lockalloc : &globalloc), v))
2282 if (!lockalloc.sizes && !globalloc.sizes) {
2285 vec_push(lockalloc.positions, 0);
2286 vec_push(globalloc.positions, 0);
2288 /* Adjust slot positions based on sizes */
2289 if (lockalloc.sizes) {
2290 pos = (vec_size(lockalloc.sizes) ? lockalloc.positions[0] : 0);
2291 for (i = 1; i < vec_size(lockalloc.sizes); ++i)
2293 pos = lockalloc.positions[i-1] + lockalloc.sizes[i-1];
2294 vec_push(lockalloc.positions, pos);
2296 self->allocated_locals = pos + vec_last(lockalloc.sizes);
2298 if (globalloc.sizes) {
2299 pos = (vec_size(globalloc.sizes) ? globalloc.positions[0] : 0);
2300 for (i = 1; i < vec_size(globalloc.sizes); ++i)
2302 pos = globalloc.positions[i-1] + globalloc.sizes[i-1];
2303 vec_push(globalloc.positions, pos);
2305 self->globaltemps = pos + vec_last(globalloc.sizes);
2308 /* Locals need to know their new position */
2309 for (i = 0; i < vec_size(self->locals); ++i) {
2310 v = self->locals[i];
2311 if (i >= vec_size(self->params) && !vec_size(v->life))
2313 if (v->locked || !opt_gt)
2314 v->code.local = lockalloc.positions[v->code.local];
2316 v->code.local = globalloc.positions[v->code.local];
2318 /* Take over the actual slot positions on values */
2319 for (i = 0; i < vec_size(self->values); ++i) {
2320 v = self->values[i];
2321 if (!vec_size(v->life))
2323 if (v->locked || !opt_gt)
2324 v->code.local = lockalloc.positions[v->code.local];
2326 v->code.local = globalloc.positions[v->code.local];
2334 for (i = 0; i < vec_size(lockalloc.locals); ++i)
2335 ir_value_delete(lockalloc.locals[i]);
2336 for (i = 0; i < vec_size(globalloc.locals); ++i)
2337 ir_value_delete(globalloc.locals[i]);
2338 vec_free(globalloc.unique);
2339 vec_free(globalloc.locals);
2340 vec_free(globalloc.sizes);
2341 vec_free(globalloc.positions);
2342 vec_free(lockalloc.unique);
2343 vec_free(lockalloc.locals);
2344 vec_free(lockalloc.sizes);
2345 vec_free(lockalloc.positions);
2349 /* Get information about which operand
2350 * is read from, or written to.
2352 static void ir_op_read_write(int op, size_t *read, size_t *write)
2372 case INSTR_STOREP_F:
2373 case INSTR_STOREP_V:
2374 case INSTR_STOREP_S:
2375 case INSTR_STOREP_ENT:
2376 case INSTR_STOREP_FLD:
2377 case INSTR_STOREP_FNC:
2388 static bool ir_block_living_add_instr(ir_block *self, size_t eid)
2391 bool changed = false;
2393 for (i = 0; i != vec_size(self->living); ++i)
2395 tempbool = ir_value_life_merge(self->living[i], eid);
2396 changed = changed || tempbool;
2401 static bool ir_block_living_lock(ir_block *self)
2404 bool changed = false;
2405 for (i = 0; i != vec_size(self->living); ++i)
2407 if (!self->living[i]->locked) {
2408 self->living[i]->locked = true;
2415 static bool ir_block_life_prop_previous(ir_block* self, ir_block *prev, bool *changed)
2421 /* values which have been read in a previous iteration are now
2422 * in the "living" array even if the previous block doesn't use them.
2423 * So we have to remove whatever does not exist in the previous block.
2424 * They will be re-added on-read, but the liferange merge won't cause
2426 for (i = 0; i < vec_size(self->living); ++i)
2428 if (!vec_ir_value_find(prev->living, self->living[i], NULL)) {
2429 vec_remove(self->living, i, 1);
2435 /* Whatever the previous block still has in its living set
2436 * must now be added to ours as well.
2438 for (i = 0; i < vec_size(prev->living); ++i)
2440 if (vec_ir_value_find(self->living, prev->living[i], NULL))
2442 vec_push(self->living, prev->living[i]);
2444 irerror(self->contextt from prev: %s", self->label, prev->living[i]->_name);
2450 static bool ir_block_life_propagate(ir_block *self, ir_block *prev, bool *changed)
2455 size_t i, o, p, mem;
2456 /* bitmasks which operands are read from or written to */
2465 if (!ir_block_life_prop_previous(self, prev, changed))
2469 i = vec_size(self->instr);
2472 instr = self->instr[i];
2474 /* See which operands are read and write operands */
2475 ir_op_read_write(instr->opcode, &read, &write);
2477 /* Go through the 3 main operands
2478 * writes first, then reads
2480 for (o = 0; o < 3; ++o)
2482 if (!instr->_ops[o]) /* no such operand */
2485 value = instr->_ops[o];
2487 /* We only care about locals */
2488 /* we also calculate parameter liferanges so that locals
2489 * can take up parameter slots */
2490 if (value->store != store_value &&
2491 value->store != store_local &&
2492 value->store != store_param)
2495 /* write operands */
2496 /* When we write to a local, we consider it "dead" for the
2497 * remaining upper part of the function, since in SSA a value
2498 * can only be written once (== created)
2503 bool in_living = vec_ir_value_find(self->living, value, &idx);
2506 /* If the value isn't alive it hasn't been read before... */
2507 /* TODO: See if the warning can be emitted during parsing or AST processing
2508 * otherwise have warning printed here.
2509 * IF printing a warning here: include filecontext_t,
2510 * and make sure it's only printed once
2511 * since this function is run multiple times.
2513 /* con_err( "Value only written %s\n", value->name); */
2514 tempbool = ir_value_life_merge(value, instr->eid);
2515 *changed = *changed || tempbool;
2517 /* since 'living' won't contain it
2518 * anymore, merge the value, since
2521 tempbool = ir_value_life_merge(value, instr->eid);
2522 *changed = *changed || tempbool;
2524 vec_remove(self->living, idx, 1);
2526 /* Removing a vector removes all members */
2527 for (mem = 0; mem < 3; ++mem) {
2528 if (value->members[mem] && vec_ir_value_find(self->living, value->members[mem], &idx)) {
2529 tempbool = ir_value_life_merge(value->members[mem], instr->eid);
2530 *changed = *changed || tempbool;
2531 vec_remove(self->living, idx, 1);
2534 /* Removing the last member removes the vector */
2535 if (value->memberof) {
2536 value = value->memberof;
2537 for (mem = 0; mem < 3; ++mem) {
2538 if (value->members[mem] && vec_ir_value_find(self->living, value->members[mem], NULL))
2541 if (mem == 3 && vec_ir_value_find(self->living, value, &idx)) {
2542 tempbool = ir_value_life_merge(value, instr->eid);
2543 *changed = *changed || tempbool;
2544 vec_remove(self->living, idx, 1);
2550 if (instr->opcode == INSTR_MUL_VF)
2552 value = instr->_ops[2];
2553 /* the float source will get an additional lifetime */
2554 if (ir_value_life_merge(value, instr->eid+1))
2556 if (value->memberof && ir_value_life_merge(value->memberof, instr->eid+1))
2559 else if (instr->opcode == INSTR_MUL_FV)
2561 value = instr->_ops[1];
2562 /* the float source will get an additional lifetime */
2563 if (ir_value_life_merge(value, instr->eid+1))
2565 if (value->memberof && ir_value_life_merge(value->memberof, instr->eid+1))
2569 for (o = 0; o < 3; ++o)
2571 if (!instr->_ops[o]) /* no such operand */
2574 value = instr->_ops[o];
2576 /* We only care about locals */
2577 /* we also calculate parameter liferanges so that locals
2578 * can take up parameter slots */
2579 if (value->store != store_value &&
2580 value->store != store_local &&
2581 value->store != store_param)
2587 if (!vec_ir_value_find(self->living, value, NULL))
2588 vec_push(self->living, value);
2589 /* reading adds the full vector */
2590 if (value->memberof && !vec_ir_value_find(self->living, value->memberof, NULL))
2591 vec_push(self->living, value->memberof);
2592 for (mem = 0; mem < 3; ++mem) {
2593 if (value->members[mem] && !vec_ir_value_find(self->living, value->members[mem], NULL))
2594 vec_push(self->living, value->members[mem]);
2598 /* PHI operands are always read operands */
2599 for (p = 0; p < vec_size(instr->phi); ++p)
2601 value = instr->phi[p].value;
2602 if (!vec_ir_value_find(self->living, value, NULL))
2603 vec_push(self->living, value);
2604 /* reading adds the full vector */
2605 if (value->memberof && !vec_ir_value_find(self->living, value->memberof, NULL))
2606 vec_push(self->living, value->memberof);
2607 for (mem = 0; mem < 3; ++mem) {
2608 if (value->members[mem] && !vec_ir_value_find(self->living, value->members[mem], NULL))
2609 vec_push(self->living, value->members[mem]);
2613 /* on a call, all these values must be "locked" */
2614 if (instr->opcode >= INSTR_CALL0 && instr->opcode <= INSTR_CALL8) {
2615 if (ir_block_living_lock(self))
2618 /* call params are read operands too */
2619 for (p = 0; p < vec_size(instr->params); ++p)
2621 value = instr->params[p];
2622 if (!vec_ir_value_find(self->living, value, NULL))
2623 vec_push(self->living, value);
2624 /* reading adds the full vector */
2625 if (value->memberof && !vec_ir_value_find(self->living, value->memberof, NULL))
2626 vec_push(self->living, value->memberof);
2627 for (mem = 0; mem < 3; ++mem) {
2628 if (value->members[mem] && !vec_ir_value_find(self->living, value->members[mem], NULL))
2629 vec_push(self->living, value->members[mem]);
2634 tempbool = ir_block_living_add_instr(self, instr->eid);
2635 /*con_err( "living added values\n");*/
2636 *changed = *changed || tempbool;
2638 /* the "entry" instruction ID */
2639 tempbool = ir_block_living_add_instr(self, self->entry_id);
2640 *changed = *changed || tempbool;
2642 if (self->run_id == self->owner->run_id)
2645 self->run_id = self->owner->run_id;
2647 for (i = 0; i < vec_size(self->entries); ++i)
2649 ir_block *entry = self->entries[i];
2650 ir_block_life_propagate(entry, self, changed);
2656 /***********************************************************************
2659 * Since the IR has the convention of putting 'write' operands
2660 * at the beginning, we have to rotate the operands of instructions
2661 * properly in order to generate valid QCVM code.
2663 * Having destinations at a fixed position is more convenient. In QC
2664 * this is *mostly* OPC, but FTE adds at least 2 instructions which
2665 * read from from OPA, and store to OPB rather than OPC. Which is
2666 * partially the reason why the implementation of these instructions
2667 * in darkplaces has been delayed for so long.
2669 * Breaking conventions is annoying...
2671 static bool ir_builder_gen_global(ir_builder *self, ir_value *global, bool islocal);
2673 static bool gen_global_field(ir_value *global)
2675 if (global->hasvalue)
2677 ir_value *fld = global->constval.vpointer;
2679 irerror(global->context, "Invalid field constant with no field: %s", global->name);
2683 /* copy the field's value */
2684 ir_value_code_setaddr(global, vec_size(code_globals));
2685 vec_push(code_globals, fld->code.fieldaddr);
2686 if (global->fieldtype == TYPE_VECTOR) {
2687 vec_push(code_globals, fld->code.fieldaddr+1);
2688 vec_push(code_globals, fld->code.fieldaddr+2);
2693 ir_value_code_setaddr(global, vec_size(code_globals));
2694 vec_push(code_globals, 0);
2695 if (global->fieldtype == TYPE_VECTOR) {
2696 vec_push(code_globals, 0);
2697 vec_push(code_globals, 0);
2700 if (global->code.globaladdr < 0)
2705 static bool gen_global_pointer(ir_value *global)
2707 if (global->hasvalue)
2709 ir_value *target = global->constval.vpointer;
2711 irerror(global->context, "Invalid pointer constant: %s", global->name);
2712 /* NULL pointers are pointing to the NULL constant, which also
2713 * sits at address 0, but still has an ir_value for itself.
2718 /* Here, relocations ARE possible - in fteqcc-enhanced-qc:
2719 * void() foo; <- proto
2720 * void() *fooptr = &foo;
2721 * void() foo = { code }
2723 if (!target->code.globaladdr) {
2724 /* FIXME: Check for the constant nullptr ir_value!
2725 * because then code.globaladdr being 0 is valid.
2727 irerror(global->context, "FIXME: Relocation support");
2731 ir_value_code_setaddr(global, vec_size(code_globals));
2732 vec_push(code_globals, target->code.globaladdr);
2736 ir_value_code_setaddr(global, vec_size(code_globals));
2737 vec_push(code_globals, 0);
2739 if (global->code.globaladdr < 0)
2744 static bool gen_blocks_recursive(ir_function *func, ir_block *block)
2746 prog_section_statement stmt;
2754 block->generated = true;
2755 block->code_start = vec_size(code_statements);
2756 for (i = 0; i < vec_size(block->instr); ++i)
2758 instr = block->instr[i];
2760 if (instr->opcode == VINSTR_PHI) {
2761 irerror(block->context, "cannot generate virtual instruction (phi)");
2765 if (instr->opcode == VINSTR_JUMP) {
2766 target = instr->bops[0];
2767 /* for uncoditional jumps, if the target hasn't been generated
2768 * yet, we generate them right here.
2770 if (!target->generated)
2771 return gen_blocks_recursive(func, target);
2773 /* otherwise we generate a jump instruction */
2774 stmt.opcode = INSTR_GOTO;
2775 stmt.o1.s1 = (target->code_start) - vec_size(code_statements);
2778 if (stmt.o1.s1 != 1)
2779 code_push_statement(&stmt, instr->context.line);
2781 /* no further instructions can be in this block */
2785 if (instr->opcode == VINSTR_COND) {
2786 ontrue = instr->bops[0];
2787 onfalse = instr->bops[1];
2788 /* TODO: have the AST signal which block should
2789 * come first: eg. optimize IFs without ELSE...
2792 stmt.o1.u1 = ir_value_code_addr(instr->_ops[0]);
2796 if (ontrue->generated) {
2797 stmt.opcode = INSTR_IF;
2798 stmt.o2.s1 = (ontrue->code_start) - vec_size(code_statements);
2799 if (stmt.o2.s1 != 1)
2800 code_push_statement(&stmt, instr->context.line);
2802 if (onfalse->generated) {
2803 stmt.opcode = INSTR_IFNOT;
2804 stmt.o2.s1 = (onfalse->code_start) - vec_size(code_statements);
2805 if (stmt.o2.s1 != 1)
2806 code_push_statement(&stmt, instr->context.line);
2808 if (!ontrue->generated) {
2809 if (onfalse->generated)
2810 return gen_blocks_recursive(func, ontrue);
2812 if (!onfalse->generated) {
2813 if (ontrue->generated)
2814 return gen_blocks_recursive(func, onfalse);
2816 /* neither ontrue nor onfalse exist */
2817 stmt.opcode = INSTR_IFNOT;
2818 if (!instr->likely) {
2819 /* Honor the likelyhood hint */
2820 ir_block *tmp = onfalse;
2821 stmt.opcode = INSTR_IF;
2825 stidx = vec_size(code_statements);
2826 code_push_statement(&stmt, instr->context.line);
2827 /* on false we jump, so add ontrue-path */
2828 if (!gen_blocks_recursive(func, ontrue))
2830 /* fixup the jump address */
2831 code_statements[stidx].o2.s1 = vec_size(code_statements) - stidx;
2832 /* generate onfalse path */
2833 if (onfalse->generated) {
2834 /* fixup the jump address */
2835 code_statements[stidx].o2.s1 = (onfalse->code_start) - (stidx);
2836 if (stidx+2 == vec_size(code_statements) && code_statements[stidx].o2.s1 == 1) {
2837 code_statements[stidx] = code_statements[stidx+1];
2838 if (code_statements[stidx].o1.s1 < 0)
2839 code_statements[stidx].o1.s1++;
2840 code_pop_statement();
2842 stmt.opcode = vec_last(code_statements).opcode;
2843 if (stmt.opcode == INSTR_GOTO ||
2844 stmt.opcode == INSTR_IF ||
2845 stmt.opcode == INSTR_IFNOT ||
2846 stmt.opcode == INSTR_RETURN ||
2847 stmt.opcode == INSTR_DONE)
2849 /* no use jumping from here */
2852 /* may have been generated in the previous recursive call */
2853 stmt.opcode = INSTR_GOTO;
2854 stmt.o1.s1 = (onfalse->code_start) - vec_size(code_statements);
2857 if (stmt.o1.s1 != 1)
2858 code_push_statement(&stmt, instr->context.line);
2861 else if (stidx+2 == vec_size(code_statements) && code_statements[stidx].o2.s1 == 1) {
2862 code_statements[stidx] = code_statements[stidx+1];
2863 if (code_statements[stidx].o1.s1 < 0)
2864 code_statements[stidx].o1.s1++;
2865 code_pop_statement();
2867 /* if not, generate now */
2868 return gen_blocks_recursive(func, onfalse);
2871 if ( (instr->opcode >= INSTR_CALL0 && instr->opcode <= INSTR_CALL8)
2872 || instr->opcode == VINSTR_NRCALL)
2877 first = vec_size(instr->params);
2880 for (p = 0; p < first; ++p)
2882 ir_value *param = instr->params[p];
2883 if (param->callparam)
2886 stmt.opcode = INSTR_STORE_F;
2889 if (param->vtype == TYPE_FIELD)
2890 stmt.opcode = field_store_instr[param->fieldtype];
2891 else if (param->vtype == TYPE_NIL)
2892 stmt.opcode = INSTR_STORE_V;
2894 stmt.opcode = type_store_instr[param->vtype];
2895 stmt.o1.u1 = ir_value_code_addr(param);
2896 stmt.o2.u1 = OFS_PARM0 + 3 * p;
2897 code_push_statement(&stmt, instr->context.line);
2899 /* Now handle extparams */
2900 first = vec_size(instr->params);
2901 for (; p < first; ++p)
2903 ir_builder *ir = func->owner;
2904 ir_value *param = instr->params[p];
2905 ir_value *targetparam;
2907 if (param->callparam)
2910 if (p-8 >= vec_size(ir->extparams))
2911 ir_gen_extparam(ir);
2913 targetparam = ir->extparams[p-8];
2915 stmt.opcode = INSTR_STORE_F;
2918 if (param->vtype == TYPE_FIELD)
2919 stmt.opcode = field_store_instr[param->fieldtype];
2920 else if (param->vtype == TYPE_NIL)
2921 stmt.opcode = INSTR_STORE_V;
2923 stmt.opcode = type_store_instr[param->vtype];
2924 stmt.o1.u1 = ir_value_code_addr(param);
2925 stmt.o2.u1 = ir_value_code_addr(targetparam);
2926 code_push_statement(&stmt, instr->context.line);
2929 stmt.opcode = INSTR_CALL0 + vec_size(instr->params);
2930 if (stmt.opcode > INSTR_CALL8)
2931 stmt.opcode = INSTR_CALL8;
2932 stmt.o1.u1 = ir_value_code_addr(instr->_ops[1]);
2935 code_push_statement(&stmt, instr->context.line);
2937 retvalue = instr->_ops[0];
2938 if (retvalue && retvalue->store != store_return &&
2939 (retvalue->store == store_global || vec_size(retvalue->life)))
2941 /* not to be kept in OFS_RETURN */
2942 if (retvalue->vtype == TYPE_FIELD && OPTS_FLAG(ADJUST_VECTOR_FIELDS))
2943 stmt.opcode = field_store_instr[retvalue->fieldtype];
2945 stmt.opcode = type_store_instr[retvalue->vtype];
2946 stmt.o1.u1 = OFS_RETURN;
2947 stmt.o2.u1 = ir_value_code_addr(retvalue);
2949 code_push_statement(&stmt, instr->context.line);
2954 if (instr->opcode == INSTR_STATE) {
2955 irerror(block->context, "TODO: state instruction");
2959 stmt.opcode = instr->opcode;
2964 /* This is the general order of operands */
2966 stmt.o3.u1 = ir_value_code_addr(instr->_ops[0]);
2969 stmt.o1.u1 = ir_value_code_addr(instr->_ops[1]);
2972 stmt.o2.u1 = ir_value_code_addr(instr->_ops[2]);
2974 if (stmt.opcode == INSTR_RETURN || stmt.opcode == INSTR_DONE)
2976 stmt.o1.u1 = stmt.o3.u1;
2979 else if ((stmt.opcode >= INSTR_STORE_F &&
2980 stmt.opcode <= INSTR_STORE_FNC) ||
2981 (stmt.opcode >= INSTR_STOREP_F &&
2982 stmt.opcode <= INSTR_STOREP_FNC))
2984 /* 2-operand instructions with A -> B */
2985 stmt.o2.u1 = stmt.o3.u1;
2988 /* tiny optimization, don't output
2991 if (stmt.o2.u1 == stmt.o1.u1 &&
2992 OPTS_OPTIMIZATION(OPTIM_PEEPHOLE))
2994 ++opts_optimizationcount[OPTIM_PEEPHOLE];
2999 code_push_statement(&stmt, instr->context.line);
3004 static bool gen_function_code(ir_function *self)
3007 prog_section_statement stmt, *retst;
3009 /* Starting from entry point, we generate blocks "as they come"
3010 * for now. Dead blocks will not be translated obviously.
3012 if (!vec_size(self->blocks)) {
3013 irerror(self->context, "Function '%s' declared without body.", self->name);
3017 block = self->blocks[0];
3018 if (block->generated)
3021 if (!gen_blocks_recursive(self, block)) {
3022 irerror(self->context, "failed to generate blocks for '%s'", self->name);
3026 /* code_write and qcvm -disasm need to know that the function ends here */
3027 retst = &vec_last(code_statements);
3028 if (OPTS_OPTIMIZATION(OPTIM_VOID_RETURN) &&
3029 self->outtype == TYPE_VOID &&
3030 retst->opcode == INSTR_RETURN &&
3031 !retst->o1.u1 && !retst->o2.u1 && !retst->o3.u1)
3033 retst->opcode = INSTR_DONE;
3034 ++opts_optimizationcount[OPTIM_VOID_RETURN];
3036 stmt.opcode = INSTR_DONE;
3040 code_push_statement(&stmt, vec_last(code_linenums));
3045 static qcint ir_builder_filestring(ir_builder *ir, const char *filename)
3047 /* NOTE: filename pointers are copied, we never strdup them,
3048 * thus we can use pointer-comparison to find the string.
3053 for (i = 0; i < vec_size(ir->filenames); ++i) {
3054 if (ir->filenames[i] == filename)
3055 return ir->filestrings[i];
3058 str = code_genstring(filename);
3059 vec_push(ir->filenames, filename);
3060 vec_push(ir->filestrings, str);
3064 static bool gen_global_function(ir_builder *ir, ir_value *global)
3066 prog_section_function fun;
3071 if (!global->hasvalue || (!global->constval.vfunc))
3073 irerror(global->context, "Invalid state of function-global: not constant: %s", global->name);
3077 irfun = global->constval.vfunc;
3079 fun.name = global->code.name;
3080 fun.file = ir_builder_filestring(ir, global->context.file);
3081 fun.profile = 0; /* always 0 */
3082 fun.nargs = vec_size(irfun->params);
3086 for (i = 0;i < 8; ++i) {
3087 if ((int32_t)i >= fun.nargs)
3090 fun.argsize[i] = type_sizeof_[irfun->params[i]];
3094 fun.locals = irfun->allocated_locals;
3097 fun.entry = irfun->builtin+1;
3099 irfun->code_function_def = vec_size(code_functions);
3100 fun.entry = vec_size(code_statements);
3103 vec_push(code_functions, fun);
3107 static ir_value* ir_gen_extparam_proto(ir_builder *ir)
3112 snprintf(name, sizeof(name), "EXTPARM#%i", (int)(vec_size(ir->extparam_protos)+8));
3113 global = ir_value_var(name, store_global, TYPE_VECTOR);
3115 vec_push(ir->extparam_protos, global);
3119 static void ir_gen_extparam(ir_builder *ir)
3121 prog_section_def def;
3124 if (vec_size(ir->extparam_protos) < vec_size(ir->extparams)+1)
3125 global = ir_gen_extparam_proto(ir);
3127 global = ir->extparam_protos[vec_size(ir->extparams)];
3129 def.name = code_genstring(global->name);
3130 def.type = TYPE_VECTOR;
3131 def.offset = vec_size(code_globals);
3133 vec_push(code_defs, def);
3134 ir_value_code_setaddr(global, def.offset);
3135 vec_push(code_globals, 0);
3136 vec_push(code_globals, 0);
3137 vec_push(code_globals, 0);
3139 vec_push(ir->extparams, global);
3142 static bool gen_function_extparam_copy(ir_function *self)
3144 size_t i, ext, numparams;
3146 ir_builder *ir = self->owner;
3148 prog_section_statement stmt;
3150 numparams = vec_size(self->params);
3154 stmt.opcode = INSTR_STORE_F;
3156 for (i = 8; i < numparams; ++i) {
3158 if (ext >= vec_size(ir->extparams))
3159 ir_gen_extparam(ir);
3161 ep = ir->extparams[ext];
3163 stmt.opcode = type_store_instr[self->locals[i]->vtype];
3164 if (self->locals[i]->vtype == TYPE_FIELD &&
3165 self->locals[i]->fieldtype == TYPE_VECTOR)
3167 stmt.opcode = INSTR_STORE_V;
3169 stmt.o1.u1 = ir_value_code_addr(ep);
3170 stmt.o2.u1 = ir_value_code_addr(self->locals[i]);
3171 code_push_statement(&stmt, self->context.line);
3177 static bool gen_function_locals(ir_builder *ir, ir_value *global)
3179 prog_section_function *def;
3182 uint32_t firstlocal, firstglobal;
3184 irfun = global->constval.vfunc;
3185 def = code_functions + irfun->code_function_def;
3187 if (opts.g || !OPTS_OPTIMIZATION(OPTIM_OVERLAP_LOCALS) || (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 (!gen_function_code(irfun)) {
3259 irerror(irfun->context, "Failed to generate code for function %s", irfun->name);
3265 static void gen_vector_defs(prog_section_def def, const char *name)
3270 if (!name || name[0] == '#' || OPTS_FLAG(SINGLE_VECTOR_DEFS))
3273 def.type = TYPE_FLOAT;
3277 component = (char*)mem_a(len+3);
3278 memcpy(component, name, len);
3280 component[len-0] = 0;
3281 component[len-2] = '_';
3283 component[len-1] = 'x';
3285 for (i = 0; i < 3; ++i) {
3286 def.name = code_genstring(component);
3287 vec_push(code_defs, def);
3293 static void gen_vector_fields(prog_section_field fld, const char *name)
3298 if (!name || OPTS_FLAG(SINGLE_VECTOR_DEFS))
3301 fld.type = TYPE_FLOAT;
3305 component = (char*)mem_a(len+3);
3306 memcpy(component, name, len);
3308 component[len-0] = 0;
3309 component[len-2] = '_';
3311 component[len-1] = 'x';
3313 for (i = 0; i < 3; ++i) {
3314 fld.name = code_genstring(component);
3315 vec_push(code_fields, fld);
3321 static bool ir_builder_gen_global(ir_builder *self, ir_value *global, bool islocal)
3325 prog_section_def def;
3326 bool pushdef = false;
3328 def.type = global->vtype;
3329 def.offset = vec_size(code_globals);
3331 if (opts.g || !islocal)
3335 if (OPTS_OPTIMIZATION(OPTIM_STRIP_CONSTANT_NAMES) &&
3336 !(global->flags & IR_FLAG_INCLUDE_DEF) &&
3337 (global->name[0] == '#' || global->cvq == CV_CONST))
3342 if (pushdef && global->name) {
3343 if (global->name[0] == '#') {
3344 if (!self->str_immediate)
3345 self->str_immediate = code_genstring("IMMEDIATE");
3346 def.name = global->code.name = self->str_immediate;
3349 def.name = global->code.name = code_genstring(global->name);
3354 def.offset = ir_value_code_addr(global);
3355 vec_push(code_defs, def);
3356 if (global->vtype == TYPE_VECTOR)
3357 gen_vector_defs(def, global->name);
3358 else if (global->vtype == TYPE_FIELD && global->fieldtype == TYPE_VECTOR)
3359 gen_vector_defs(def, global->name);
3366 switch (global->vtype)
3369 if (!strcmp(global->name, "end_sys_globals")) {
3370 /* TODO: remember this point... all the defs before this one
3371 * should be checksummed and added to progdefs.h when we generate it.
3374 else if (!strcmp(global->name, "end_sys_fields")) {
3375 /* TODO: same as above but for entity-fields rather than globsl
3379 irwarning(global->context, WARN_VOID_VARIABLES, "unrecognized variable of type void `%s`",
3381 /* I'd argue setting it to 0 is sufficient, but maybe some depend on knowing how far
3382 * the system fields actually go? Though the engine knows this anyway...
3383 * Maybe this could be an -foption
3384 * fteqcc creates data for end_sys_* - of size 1, so let's do the same
3386 ir_value_code_setaddr(global, vec_size(code_globals));
3387 vec_push(code_globals, 0);
3389 if (pushdef) vec_push(code_defs, def);
3392 if (pushdef) vec_push(code_defs, def);
3393 return gen_global_pointer(global);
3396 vec_push(code_defs, def);
3397 if (global->fieldtype == TYPE_VECTOR)
3398 gen_vector_defs(def, global->name);
3400 return gen_global_field(global);
3405 ir_value_code_setaddr(global, vec_size(code_globals));
3406 if (global->hasvalue) {
3407 iptr = (int32_t*)&global->constval.ivec[0];
3408 vec_push(code_globals, *iptr);
3410 vec_push(code_globals, 0);
3412 if (!islocal && global->cvq != CV_CONST)
3413 def.type |= DEF_SAVEGLOBAL;
3414 if (pushdef) vec_push(code_defs, def);
3416 return global->code.globaladdr >= 0;
3420 ir_value_code_setaddr(global, vec_size(code_globals));
3421 if (global->hasvalue) {
3422 vec_push(code_globals, code_genstring(global->constval.vstring));
3424 vec_push(code_globals, 0);
3426 if (!islocal && global->cvq != CV_CONST)
3427 def.type |= DEF_SAVEGLOBAL;
3428 if (pushdef) vec_push(code_defs, def);
3429 return global->code.globaladdr >= 0;
3434 ir_value_code_setaddr(global, vec_size(code_globals));
3435 if (global->hasvalue) {
3436 iptr = (int32_t*)&global->constval.ivec[0];
3437 vec_push(code_globals, iptr[0]);
3438 if (global->code.globaladdr < 0)
3440 for (d = 1; d < type_sizeof_[global->vtype]; ++d) {
3441 vec_push(code_globals, iptr[d]);
3444 vec_push(code_globals, 0);
3445 if (global->code.globaladdr < 0)
3447 for (d = 1; d < type_sizeof_[global->vtype]; ++d) {
3448 vec_push(code_globals, 0);
3451 if (!islocal && global->cvq != CV_CONST)
3452 def.type |= DEF_SAVEGLOBAL;
3455 vec_push(code_defs, def);
3456 def.type &= ~DEF_SAVEGLOBAL;
3457 gen_vector_defs(def, global->name);
3459 return global->code.globaladdr >= 0;
3462 ir_value_code_setaddr(global, vec_size(code_globals));
3463 if (!global->hasvalue) {
3464 vec_push(code_globals, 0);
3465 if (global->code.globaladdr < 0)
3468 vec_push(code_globals, vec_size(code_functions));
3469 if (!gen_global_function(self, global))
3472 if (!islocal && global->cvq != CV_CONST)
3473 def.type |= DEF_SAVEGLOBAL;
3474 if (pushdef) vec_push(code_defs, def);
3477 /* assume biggest type */
3478 ir_value_code_setaddr(global, vec_size(code_globals));
3479 vec_push(code_globals, 0);
3480 for (i = 1; i < type_sizeof_[TYPE_VARIANT]; ++i)
3481 vec_push(code_globals, 0);
3484 /* refuse to create 'void' type or any other fancy business. */
3485 irerror(global->context, "Invalid type for global variable `%s`: %s",
3486 global->name, type_name[global->vtype]);
3491 static void ir_builder_prepare_field(ir_value *field)
3493 field->code.fieldaddr = code_alloc_field(type_sizeof_[field->fieldtype]);
3496 static bool ir_builder_gen_field(ir_builder *self, ir_value *field)
3498 prog_section_def def;
3499 prog_section_field fld;
3503 def.type = (uint16_t)field->vtype;
3504 def.offset = (uint16_t)vec_size(code_globals);
3506 /* create a global named the same as the field */
3507 if (opts.standard == COMPILER_GMQCC) {
3508 /* in our standard, the global gets a dot prefix */
3509 size_t len = strlen(field->name);
3512 /* we really don't want to have to allocate this, and 1024
3513 * bytes is more than enough for a variable/field name
3515 if (len+2 >= sizeof(name)) {
3516 irerror(field->context, "invalid field name size: %u", (unsigned int)len);
3521 memcpy(name+1, field->name, len); /* no strncpy - we used strlen above */
3524 def.name = code_genstring(name);
3525 fld.name = def.name + 1; /* we reuse that string table entry */
3527 /* in plain QC, there cannot be a global with the same name,
3528 * and so we also name the global the same.
3529 * FIXME: fteqcc should create a global as well
3530 * check if it actually uses the same name. Probably does
3532 def.name = code_genstring(field->name);
3533 fld.name = def.name;
3536 field->code.name = def.name;
3538 vec_push(code_defs, def);
3540 fld.type = field->fieldtype;
3542 if (fld.type == TYPE_VOID) {
3543 irerror(field->context, "field is missing a type: %s - don't know its size", field->name);
3547 fld.offset = field->code.fieldaddr;
3549 vec_push(code_fields, fld);
3551 ir_value_code_setaddr(field, vec_size(code_globals));
3552 vec_push(code_globals, fld.offset);
3553 if (fld.type == TYPE_VECTOR) {
3554 vec_push(code_globals, fld.offset+1);
3555 vec_push(code_globals, fld.offset+2);
3558 if (field->fieldtype == TYPE_VECTOR) {
3559 gen_vector_defs(def, field->name);
3560 gen_vector_fields(fld, field->name);
3563 return field->code.globaladdr >= 0;
3566 bool ir_builder_generate(ir_builder *self, const char *filename)
3568 prog_section_statement stmt;
3570 char *lnofile = NULL;
3574 for (i = 0; i < vec_size(self->fields); ++i)
3576 ir_builder_prepare_field(self->fields[i]);
3579 for (i = 0; i < vec_size(self->globals); ++i)
3581 if (!ir_builder_gen_global(self, self->globals[i], false)) {
3584 if (self->globals[i]->vtype == TYPE_FUNCTION) {
3585 ir_function *func = self->globals[i]->constval.vfunc;
3586 if (func && self->max_locals < func->allocated_locals &&
3587 !(func->flags & IR_FLAG_MASK_NO_OVERLAP))
3589 self->max_locals = func->allocated_locals;
3591 if (func && self->max_globaltemps < func->globaltemps)
3592 self->max_globaltemps = func->globaltemps;
3596 for (i = 0; i < vec_size(self->fields); ++i)
3598 if (!ir_builder_gen_field(self, self->fields[i])) {
3604 ir_value_code_setaddr(self->nil, vec_size(code_globals));
3605 vec_push(code_globals, 0);
3606 vec_push(code_globals, 0);
3607 vec_push(code_globals, 0);
3609 /* generate global temps */
3610 self->first_common_globaltemp = vec_size(code_globals);
3611 for (i = 0; i < self->max_globaltemps; ++i) {
3612 vec_push(code_globals, 0);
3614 /* generate common locals */
3615 self->first_common_local = vec_size(code_globals);
3616 for (i = 0; i < self->max_locals; ++i) {
3617 vec_push(code_globals, 0);
3620 /* generate function code */
3621 for (i = 0; i < vec_size(self->globals); ++i)
3623 if (self->globals[i]->vtype == TYPE_FUNCTION) {
3624 if (!gen_global_function_code(self, self->globals[i])) {
3630 if (vec_size(code_globals) >= 65536) {
3631 irerror(vec_last(self->globals)->context, "This progs file would require more globals than the metadata can handle. Bailing out.");
3635 /* DP errors if the last instruction is not an INSTR_DONE. */
3636 if (vec_last(code_statements).opcode != INSTR_DONE)
3638 stmt.opcode = INSTR_DONE;
3642 code_push_statement(&stmt, vec_last(code_linenums));
3648 if (vec_size(code_statements) != vec_size(code_linenums)) {
3649 con_err("Linecounter wrong: %lu != %lu\n",
3650 (unsigned long)vec_size(code_statements),
3651 (unsigned long)vec_size(code_linenums));
3652 } else if (OPTS_FLAG(LNO)) {
3654 size_t filelen = strlen(filename);
3656 memcpy(vec_add(lnofile, filelen+1), filename, filelen+1);
3657 dot = strrchr(lnofile, '.');
3661 vec_shrinkto(lnofile, dot - lnofile);
3663 memcpy(vec_add(lnofile, 5), ".lno", 5);
3668 con_out("writing '%s' and '%s'...\n", filename, lnofile);
3670 con_out("writing '%s'\n", filename);
3672 if (!code_write(filename, lnofile)) {
3680 /***********************************************************************
3681 *IR DEBUG Dump functions...
3684 #define IND_BUFSZ 1024
3687 # define strncat(dst, src, sz) strncat_s(dst, sz, src, _TRUNCATE)
3690 const char *qc_opname(int op)
3692 if (op < 0) return "<INVALID>";
3693 if (op < (int)( sizeof(asm_instr) / sizeof(asm_instr[0]) ))
3694 return asm_instr[op].m;
3696 case VINSTR_PHI: return "PHI";
3697 case VINSTR_JUMP: return "JUMP";
3698 case VINSTR_COND: return "COND";
3699 default: return "<UNK>";
3703 void ir_builder_dump(ir_builder *b, int (*oprintf)(const char*, ...))
3706 char indent[IND_BUFSZ];
3710 oprintf("module %s\n", b->name);
3711 for (i = 0; i < vec_size(b->globals); ++i)
3714 if (b->globals[i]->hasvalue)
3715 oprintf("%s = ", b->globals[i]->name);
3716 ir_value_dump(b->globals[i], oprintf);
3719 for (i = 0; i < vec_size(b->functions); ++i)
3720 ir_function_dump(b->functions[i], indent, oprintf);
3721 oprintf("endmodule %s\n", b->name);
3724 static const char *storenames[] = {
3725 "[global]", "[local]", "[param]", "[value]", "[return]"
3728 void ir_function_dump(ir_function *f, char *ind,
3729 int (*oprintf)(const char*, ...))
3732 if (f->builtin != 0) {
3733 oprintf("%sfunction %s = builtin %i\n", ind, f->name, -f->builtin);
3736 oprintf("%sfunction %s\n", ind, f->name);
3737 strncat(ind, "\t", IND_BUFSZ);
3738 if (vec_size(f->locals))
3740 oprintf("%s%i locals:\n", ind, (int)vec_size(f->locals));
3741 for (i = 0; i < vec_size(f->locals); ++i) {
3742 oprintf("%s\t", ind);
3743 ir_value_dump(f->locals[i], oprintf);
3747 oprintf("%sliferanges:\n", ind);
3748 for (i = 0; i < vec_size(f->locals); ++i) {
3749 const char *attr = "";
3751 ir_value *v = f->locals[i];
3752 if (v->unique_life && v->locked)
3753 attr = "unique,locked ";
3754 else if (v->unique_life)
3758 oprintf("%s\t%s: %s %s %s%s@%i ", ind, v->name, type_name[v->vtype],
3759 storenames[v->store],
3760 attr, (v->callparam ? "callparam " : ""),
3761 (int)v->code.local);
3764 for (l = 0; l < vec_size(v->life); ++l) {
3765 oprintf("[%i,%i] ", v->life[l].start, v->life[l].end);
3768 for (m = 0; m < 3; ++m) {
3769 ir_value *vm = v->members[m];
3772 oprintf("%s\t%s: @%i ", ind, vm->name, (int)vm->code.local);
3773 for (l = 0; l < vec_size(vm->life); ++l) {
3774 oprintf("[%i,%i] ", vm->life[l].start, vm->life[l].end);
3779 for (i = 0; i < vec_size(f->values); ++i) {
3780 const char *attr = "";
3782 ir_value *v = f->values[i];
3783 if (v->unique_life && v->locked)
3784 attr = "unique,locked ";
3785 else if (v->unique_life)
3789 oprintf("%s\t%s: %s %s %s%s@%i ", ind, v->name, type_name[v->vtype],
3790 storenames[v->store],
3791 attr, (v->callparam ? "callparam " : ""),
3792 (int)v->code.local);
3795 for (l = 0; l < vec_size(v->life); ++l) {
3796 oprintf("[%i,%i] ", v->life[l].start, v->life[l].end);
3799 for (m = 0; m < 3; ++m) {
3800 ir_value *vm = v->members[m];
3803 if (vm->unique_life && vm->locked)
3804 attr = "unique,locked ";
3805 else if (vm->unique_life)
3807 else if (vm->locked)
3809 oprintf("%s\t%s: %s@%i ", ind, vm->name, attr, (int)vm->code.local);
3810 for (l = 0; l < vec_size(vm->life); ++l) {
3811 oprintf("[%i,%i] ", vm->life[l].start, vm->life[l].end);
3816 if (vec_size(f->blocks))
3818 oprintf("%slife passes (check): %i\n", ind, (int)f->run_id);
3819 for (i = 0; i < vec_size(f->blocks); ++i) {
3820 if (f->blocks[i]->run_id != f->run_id) {
3821 oprintf("%slife pass check fail! %i != %i\n", ind, (int)f->blocks[i]->run_id, (int)f->run_id);
3823 ir_block_dump(f->blocks[i], ind, oprintf);
3827 ind[strlen(ind)-1] = 0;
3828 oprintf("%sendfunction %s\n", ind, f->name);
3831 void ir_block_dump(ir_block* b, char *ind,
3832 int (*oprintf)(const char*, ...))
3835 oprintf("%s:%s\n", ind, b->label);
3836 strncat(ind, "\t", IND_BUFSZ);
3838 if (b->instr && b->instr[0])
3839 oprintf("%s (%i) [entry]\n", ind, (int)(b->instr[0]->eid-1));
3840 for (i = 0; i < vec_size(b->instr); ++i)
3841 ir_instr_dump(b->instr[i], ind, oprintf);
3842 ind[strlen(ind)-1] = 0;
3845 void dump_phi(ir_instr *in, int (*oprintf)(const char*, ...))
3848 oprintf("%s <- phi ", in->_ops[0]->name);
3849 for (i = 0; i < vec_size(in->phi); ++i)
3851 oprintf("([%s] : %s) ", in->phi[i].from->label,
3852 in->phi[i].value->name);
3857 void ir_instr_dump(ir_instr *in, char *ind,
3858 int (*oprintf)(const char*, ...))
3861 const char *comma = NULL;
3863 oprintf("%s (%i) ", ind, (int)in->eid);
3865 if (in->opcode == VINSTR_PHI) {
3866 dump_phi(in, oprintf);
3870 strncat(ind, "\t", IND_BUFSZ);
3872 if (in->_ops[0] && (in->_ops[1] || in->_ops[2])) {
3873 ir_value_dump(in->_ops[0], oprintf);
3874 if (in->_ops[1] || in->_ops[2])
3877 if (in->opcode == INSTR_CALL0 || in->opcode == VINSTR_NRCALL) {
3878 oprintf("CALL%i\t", vec_size(in->params));
3880 oprintf("%s\t", qc_opname(in->opcode));
3882 if (in->_ops[0] && !(in->_ops[1] || in->_ops[2])) {
3883 ir_value_dump(in->_ops[0], oprintf);
3888 for (i = 1; i != 3; ++i) {
3892 ir_value_dump(in->_ops[i], oprintf);
3900 oprintf("[%s]", in->bops[0]->label);
3904 oprintf("%s[%s]", comma, in->bops[1]->label);
3905 if (vec_size(in->params)) {
3906 oprintf("\tparams: ");
3907 for (i = 0; i != vec_size(in->params); ++i) {
3908 oprintf("%s, ", in->params[i]->name);
3912 ind[strlen(ind)-1] = 0;
3915 void ir_value_dump_string(const char *str, int (*oprintf)(const char*, ...))
3918 for (; *str; ++str) {
3920 case '\n': oprintf("\\n"); break;
3921 case '\r': oprintf("\\r"); break;
3922 case '\t': oprintf("\\t"); break;
3923 case '\v': oprintf("\\v"); break;
3924 case '\f': oprintf("\\f"); break;
3925 case '\b': oprintf("\\b"); break;
3926 case '\a': oprintf("\\a"); break;
3927 case '\\': oprintf("\\\\"); break;
3928 case '"': oprintf("\\\""); break;
3929 default: oprintf("%c", *str); break;
3935 void ir_value_dump(ir_value* v, int (*oprintf)(const char*, ...))
3944 oprintf("fn:%s", v->name);
3947 oprintf("%g", v->constval.vfloat);
3950 oprintf("'%g %g %g'",
3953 v->constval.vvec.z);
3956 oprintf("(entity)");
3959 ir_value_dump_string(v->constval.vstring, oprintf);
3963 oprintf("%i", v->constval.vint);
3968 v->constval.vpointer->name);
3972 oprintf("%s", v->name);
3976 void ir_value_dump_life(const ir_value *self, int (*oprintf)(const char*,...))
3979 oprintf("Life of %12s:", self->name);
3980 for (i = 0; i < vec_size(self->life); ++i)
3982 oprintf(" + [%i, %i]\n", self->life[i].start, self->life[i].end);