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;
326 void ir_builder_delete(ir_builder* self)
329 util_htdel(self->htglobals);
330 util_htdel(self->htfields);
331 util_htdel(self->htfunctions);
332 mem_d((void*)self->name);
333 for (i = 0; i != vec_size(self->functions); ++i) {
334 ir_function_delete_quick(self->functions[i]);
336 vec_free(self->functions);
337 for (i = 0; i != vec_size(self->extparams); ++i) {
338 ir_value_delete(self->extparams[i]);
340 vec_free(self->extparams);
341 for (i = 0; i != vec_size(self->globals); ++i) {
342 ir_value_delete(self->globals[i]);
344 vec_free(self->globals);
345 for (i = 0; i != vec_size(self->fields); ++i) {
346 ir_value_delete(self->fields[i]);
348 ir_value_delete(self->nil);
349 vec_free(self->fields);
350 vec_free(self->filenames);
351 vec_free(self->filestrings);
355 bool ir_builder_set_name(ir_builder *self, const char *name)
358 mem_d((void*)self->name);
359 self->name = util_strdup(name);
363 ir_function* ir_builder_get_function(ir_builder *self, const char *name)
365 return (ir_function*)util_htget(self->htfunctions, name);
368 ir_function* ir_builder_create_function(ir_builder *self, const char *name, int outtype)
370 ir_function *fn = ir_builder_get_function(self, name);
375 fn = ir_function_new(self, outtype);
376 if (!ir_function_set_name(fn, name))
378 ir_function_delete(fn);
381 vec_push(self->functions, fn);
382 util_htset(self->htfunctions, name, fn);
384 fn->value = ir_builder_create_global(self, fn->name, TYPE_FUNCTION);
386 ir_function_delete(fn);
390 fn->value->hasvalue = true;
391 fn->value->outtype = outtype;
392 fn->value->constval.vfunc = fn;
393 fn->value->context = fn->context;
398 ir_value* ir_builder_get_global(ir_builder *self, const char *name)
400 return (ir_value*)util_htget(self->htglobals, name);
403 ir_value* ir_builder_create_global(ir_builder *self, const char *name, int vtype)
407 if (name && name[0] != '#')
409 ve = ir_builder_get_global(self, name);
415 ve = ir_value_var(name, store_global, vtype);
416 vec_push(self->globals, ve);
417 util_htset(self->htglobals, name, ve);
421 ir_value* ir_builder_get_field(ir_builder *self, const char *name)
423 return (ir_value*)util_htget(self->htfields, name);
427 ir_value* ir_builder_create_field(ir_builder *self, const char *name, int vtype)
429 ir_value *ve = ir_builder_get_field(self, name);
434 ve = ir_value_var(name, store_global, TYPE_FIELD);
435 ve->fieldtype = vtype;
436 vec_push(self->fields, ve);
437 util_htset(self->htfields, name, ve);
441 /***********************************************************************
445 bool ir_function_naive_phi(ir_function*);
446 void ir_function_enumerate(ir_function*);
447 bool ir_function_calculate_liferanges(ir_function*);
448 bool ir_function_allocate_locals(ir_function*);
450 ir_function* ir_function_new(ir_builder* owner, int outtype)
453 self = (ir_function*)mem_a(sizeof(*self));
458 memset(self, 0, sizeof(*self));
461 if (!ir_function_set_name(self, "<@unnamed>")) {
468 self->context.file = "<@no context>";
469 self->context.line = 0;
470 self->outtype = outtype;
479 self->code_function_def = -1;
480 self->allocated_locals = 0;
481 self->globaltemps = 0;
487 bool ir_function_set_name(ir_function *self, const char *name)
490 mem_d((void*)self->name);
491 self->name = util_strdup(name);
495 static void ir_function_delete_quick(ir_function *self)
498 mem_d((void*)self->name);
500 for (i = 0; i != vec_size(self->blocks); ++i)
501 ir_block_delete_quick(self->blocks[i]);
502 vec_free(self->blocks);
504 vec_free(self->params);
506 for (i = 0; i != vec_size(self->values); ++i)
507 ir_value_delete(self->values[i]);
508 vec_free(self->values);
510 for (i = 0; i != vec_size(self->locals); ++i)
511 ir_value_delete(self->locals[i]);
512 vec_free(self->locals);
514 /* self->value is deleted by the builder */
519 void ir_function_delete(ir_function *self)
522 mem_d((void*)self->name);
524 for (i = 0; i != vec_size(self->blocks); ++i)
525 ir_block_delete(self->blocks[i]);
526 vec_free(self->blocks);
528 vec_free(self->params);
530 for (i = 0; i != vec_size(self->values); ++i)
531 ir_value_delete(self->values[i]);
532 vec_free(self->values);
534 for (i = 0; i != vec_size(self->locals); ++i)
535 ir_value_delete(self->locals[i]);
536 vec_free(self->locals);
538 /* self->value is deleted by the builder */
543 void ir_function_collect_value(ir_function *self, ir_value *v)
545 vec_push(self->values, v);
548 ir_block* ir_function_create_block(lex_ctx ctx, ir_function *self, const char *label)
550 ir_block* bn = ir_block_new(self, label);
552 vec_push(self->blocks, bn);
556 static bool instr_is_operation(uint16_t op)
558 return ( (op >= INSTR_MUL_F && op <= INSTR_GT) ||
559 (op >= INSTR_LOAD_F && op <= INSTR_LOAD_FNC) ||
560 (op == INSTR_ADDRESS) ||
561 (op >= INSTR_NOT_F && op <= INSTR_NOT_FNC) ||
562 (op >= INSTR_AND && op <= INSTR_BITOR) ||
563 (op >= INSTR_CALL0 && op <= INSTR_CALL8) );
566 bool ir_function_pass_peephole(ir_function *self)
570 for (b = 0; b < vec_size(self->blocks); ++b) {
572 ir_block *block = self->blocks[b];
574 for (i = 0; i < vec_size(block->instr); ++i) {
576 inst = block->instr[i];
579 (inst->opcode >= INSTR_STORE_F &&
580 inst->opcode <= INSTR_STORE_FNC))
588 oper = block->instr[i-1];
589 if (!instr_is_operation(oper->opcode))
592 value = oper->_ops[0];
594 /* only do it for SSA values */
595 if (value->store != store_value)
598 /* don't optimize out the temp if it's used later again */
599 if (vec_size(value->reads) != 1)
602 /* The very next store must use this value */
603 if (value->reads[0] != store)
606 /* And of course the store must _read_ from it, so it's in
608 if (store->_ops[1] != value)
611 ++opts_optimizationcount[OPTIM_PEEPHOLE];
612 (void)!ir_instr_op(oper, 0, store->_ops[0], true);
614 vec_remove(block->instr, i, 1);
615 ir_instr_delete(store);
617 else if (inst->opcode == VINSTR_COND)
619 /* COND on a value resulting from a NOT could
620 * remove the NOT and swap its operands
627 value = inst->_ops[0];
629 if (value->store != store_value ||
630 vec_size(value->reads) != 1 ||
631 value->reads[0] != inst)
636 inot = value->writes[0];
637 if (inot->_ops[0] != value ||
638 inot->opcode < INSTR_NOT_F ||
639 inot->opcode > INSTR_NOT_FNC ||
640 inot->opcode == INSTR_NOT_V || /* can't do these */
641 inot->opcode == INSTR_NOT_S)
647 ++opts_optimizationcount[OPTIM_PEEPHOLE];
649 (void)!ir_instr_op(inst, 0, inot->_ops[1], false);
652 for (inotid = 0; inotid < vec_size(tmp->instr); ++inotid) {
653 if (tmp->instr[inotid] == inot)
656 if (inotid >= vec_size(tmp->instr)) {
657 compile_error(inst->context, "sanity-check failed: failed to find instruction to optimize out");
660 vec_remove(tmp->instr, inotid, 1);
661 ir_instr_delete(inot);
662 /* swap ontrue/onfalse */
664 inst->bops[0] = inst->bops[1];
675 bool ir_function_pass_tailrecursion(ir_function *self)
679 for (b = 0; b < vec_size(self->blocks); ++b) {
681 ir_instr *ret, *call, *store = NULL;
682 ir_block *block = self->blocks[b];
684 if (!block->final || vec_size(block->instr) < 2)
687 ret = block->instr[vec_size(block->instr)-1];
688 if (ret->opcode != INSTR_DONE && ret->opcode != INSTR_RETURN)
691 call = block->instr[vec_size(block->instr)-2];
692 if (call->opcode >= INSTR_STORE_F && call->opcode <= INSTR_STORE_FNC) {
693 /* account for the unoptimized
695 * STORE %return, %tmp
699 if (vec_size(block->instr) < 3)
703 call = block->instr[vec_size(block->instr)-3];
706 if (call->opcode < INSTR_CALL0 || call->opcode > INSTR_CALL8)
710 /* optimize out the STORE */
712 ret->_ops[0] == store->_ops[0] &&
713 store->_ops[1] == call->_ops[0])
715 ++opts_optimizationcount[OPTIM_PEEPHOLE];
716 call->_ops[0] = store->_ops[0];
717 vec_remove(block->instr, vec_size(block->instr) - 2, 1);
718 ir_instr_delete(store);
727 funcval = call->_ops[1];
730 if (funcval->vtype != TYPE_FUNCTION || funcval->constval.vfunc != self)
733 /* now we have a CALL and a RET, check if it's a tailcall */
734 if (ret->_ops[0] && call->_ops[0] != ret->_ops[0])
737 ++opts_optimizationcount[OPTIM_TAIL_RECURSION];
738 vec_shrinkby(block->instr, 2);
740 block->final = false; /* open it back up */
742 /* emite parameter-stores */
743 for (p = 0; p < vec_size(call->params); ++p) {
744 /* assert(call->params_count <= self->locals_count); */
745 if (!ir_block_create_store(block, call->context, self->locals[p], call->params[p])) {
746 irerror(call->context, "failed to create tailcall store instruction for parameter %i", (int)p);
750 if (!ir_block_create_jump(block, call->context, self->blocks[0])) {
751 irerror(call->context, "failed to create tailcall jump");
755 ir_instr_delete(call);
756 ir_instr_delete(ret);
762 bool ir_function_finalize(ir_function *self)
769 if (OPTS_OPTIMIZATION(OPTIM_PEEPHOLE)) {
770 if (!ir_function_pass_peephole(self)) {
771 irerror(self->context, "generic optimization pass broke something in `%s`", self->name);
776 if (OPTS_OPTIMIZATION(OPTIM_TAIL_RECURSION)) {
777 if (!ir_function_pass_tailrecursion(self)) {
778 irerror(self->context, "tail-recursion optimization pass broke something in `%s`", self->name);
783 if (!ir_function_naive_phi(self)) {
784 irerror(self->context, "internal error: ir_function_naive_phi failed");
788 for (i = 0; i < vec_size(self->locals); ++i) {
789 ir_value *v = self->locals[i];
790 if (v->vtype == TYPE_VECTOR ||
791 (v->vtype == TYPE_FIELD && v->outtype == TYPE_VECTOR))
793 ir_value_vector_member(v, 0);
794 ir_value_vector_member(v, 1);
795 ir_value_vector_member(v, 2);
798 for (i = 0; i < vec_size(self->values); ++i) {
799 ir_value *v = self->values[i];
800 if (v->vtype == TYPE_VECTOR ||
801 (v->vtype == TYPE_FIELD && v->outtype == TYPE_VECTOR))
803 ir_value_vector_member(v, 0);
804 ir_value_vector_member(v, 1);
805 ir_value_vector_member(v, 2);
809 ir_function_enumerate(self);
811 if (!ir_function_calculate_liferanges(self))
813 if (!ir_function_allocate_locals(self))
818 ir_value* ir_function_create_local(ir_function *self, const char *name, int vtype, bool param)
823 vec_size(self->locals) &&
824 self->locals[vec_size(self->locals)-1]->store != store_param) {
825 irerror(self->context, "cannot add parameters after adding locals");
829 ve = ir_value_var(name, (param ? store_param : store_local), vtype);
832 vec_push(self->locals, ve);
836 /***********************************************************************
840 ir_block* ir_block_new(ir_function* owner, const char *name)
843 self = (ir_block*)mem_a(sizeof(*self));
847 memset(self, 0, sizeof(*self));
850 if (name && !ir_block_set_label(self, name)) {
855 self->context.file = "<@no context>";
856 self->context.line = 0;
860 self->entries = NULL;
864 self->is_return = false;
869 self->generated = false;
874 static void ir_block_delete_quick(ir_block* self)
877 if (self->label) mem_d(self->label);
878 for (i = 0; i != vec_size(self->instr); ++i)
879 ir_instr_delete_quick(self->instr[i]);
880 vec_free(self->instr);
881 vec_free(self->entries);
882 vec_free(self->exits);
883 vec_free(self->living);
887 void ir_block_delete(ir_block* self)
890 if (self->label) mem_d(self->label);
891 for (i = 0; i != vec_size(self->instr); ++i)
892 ir_instr_delete(self->instr[i]);
893 vec_free(self->instr);
894 vec_free(self->entries);
895 vec_free(self->exits);
896 vec_free(self->living);
900 bool ir_block_set_label(ir_block *self, const char *name)
903 mem_d((void*)self->label);
904 self->label = util_strdup(name);
905 return !!self->label;
908 /***********************************************************************
912 ir_instr* ir_instr_new(lex_ctx ctx, ir_block* owner, int op)
915 self = (ir_instr*)mem_a(sizeof(*self));
922 self->_ops[0] = NULL;
923 self->_ops[1] = NULL;
924 self->_ops[2] = NULL;
925 self->bops[0] = NULL;
926 self->bops[1] = NULL;
937 static void ir_instr_delete_quick(ir_instr *self)
940 vec_free(self->params);
944 void ir_instr_delete(ir_instr *self)
947 /* The following calls can only delete from
948 * vectors, we still want to delete this instruction
949 * so ignore the return value. Since with the warn_unused_result attribute
950 * gcc doesn't care about an explicit: (void)foo(); to ignore the result,
951 * I have to improvise here and use if(foo());
953 for (i = 0; i < vec_size(self->phi); ++i) {
955 if (vec_ir_instr_find(self->phi[i].value->writes, self, &idx))
956 vec_remove(self->phi[i].value->writes, idx, 1);
957 if (vec_ir_instr_find(self->phi[i].value->reads, self, &idx))
958 vec_remove(self->phi[i].value->reads, idx, 1);
961 for (i = 0; i < vec_size(self->params); ++i) {
963 if (vec_ir_instr_find(self->params[i]->writes, self, &idx))
964 vec_remove(self->params[i]->writes, idx, 1);
965 if (vec_ir_instr_find(self->params[i]->reads, self, &idx))
966 vec_remove(self->params[i]->reads, idx, 1);
968 vec_free(self->params);
969 (void)!ir_instr_op(self, 0, NULL, false);
970 (void)!ir_instr_op(self, 1, NULL, false);
971 (void)!ir_instr_op(self, 2, NULL, false);
975 bool ir_instr_op(ir_instr *self, int op, ir_value *v, bool writing)
977 if (self->_ops[op]) {
979 if (writing && vec_ir_instr_find(self->_ops[op]->writes, self, &idx))
980 vec_remove(self->_ops[op]->writes, idx, 1);
981 else if (vec_ir_instr_find(self->_ops[op]->reads, self, &idx))
982 vec_remove(self->_ops[op]->reads, idx, 1);
986 vec_push(v->writes, self);
988 vec_push(v->reads, self);
994 /***********************************************************************
998 void ir_value_code_setaddr(ir_value *self, int32_t gaddr)
1000 self->code.globaladdr = gaddr;
1001 if (self->members[0]) self->members[0]->code.globaladdr = gaddr;
1002 if (self->members[1]) self->members[1]->code.globaladdr = gaddr;
1003 if (self->members[2]) self->members[2]->code.globaladdr = gaddr;
1006 int32_t ir_value_code_addr(const ir_value *self)
1008 if (self->store == store_return)
1009 return OFS_RETURN + self->code.addroffset;
1010 return self->code.globaladdr + self->code.addroffset;
1013 ir_value* ir_value_var(const char *name, int storetype, int vtype)
1016 self = (ir_value*)mem_a(sizeof(*self));
1017 self->vtype = vtype;
1018 self->fieldtype = TYPE_VOID;
1019 self->outtype = TYPE_VOID;
1020 self->store = storetype;
1023 self->writes = NULL;
1025 self->cvq = CV_NONE;
1026 self->hasvalue = false;
1027 self->context.file = "<@no context>";
1028 self->context.line = 0;
1030 if (name && !ir_value_set_name(self, name)) {
1031 irerror(self->context, "out of memory");
1036 memset(&self->constval, 0, sizeof(self->constval));
1037 memset(&self->code, 0, sizeof(self->code));
1039 self->members[0] = NULL;
1040 self->members[1] = NULL;
1041 self->members[2] = NULL;
1042 self->memberof = NULL;
1044 self->unique_life = false;
1045 self->locked = false;
1046 self->callparam = false;
1052 ir_value* ir_value_vector_member(ir_value *self, unsigned int member)
1060 if (self->members[member])
1061 return self->members[member];
1064 len = strlen(self->name);
1065 name = (char*)mem_a(len + 3);
1066 memcpy(name, self->name, len);
1068 name[len+1] = 'x' + member;
1074 if (self->vtype == TYPE_VECTOR)
1076 m = ir_value_var(name, self->store, TYPE_FLOAT);
1081 m->context = self->context;
1083 self->members[member] = m;
1084 m->code.addroffset = member;
1086 else if (self->vtype == TYPE_FIELD)
1088 if (self->fieldtype != TYPE_VECTOR)
1090 m = ir_value_var(name, self->store, TYPE_FIELD);
1095 m->fieldtype = TYPE_FLOAT;
1096 m->context = self->context;
1098 self->members[member] = m;
1099 m->code.addroffset = member;
1103 irerror(self->context, "invalid member access on %s", self->name);
1111 static GMQCC_INLINE size_t ir_value_sizeof(const ir_value *self)
1113 if (self->vtype == TYPE_FIELD && self->fieldtype == TYPE_VECTOR)
1114 return type_sizeof_[TYPE_VECTOR];
1115 return type_sizeof_[self->vtype];
1118 ir_value* ir_value_out(ir_function *owner, const char *name, int storetype, int vtype)
1120 ir_value *v = ir_value_var(name, storetype, vtype);
1123 ir_function_collect_value(owner, v);
1127 void ir_value_delete(ir_value* self)
1131 mem_d((void*)self->name);
1134 if (self->vtype == TYPE_STRING)
1135 mem_d((void*)self->constval.vstring);
1137 for (i = 0; i < 3; ++i) {
1138 if (self->members[i])
1139 ir_value_delete(self->members[i]);
1141 vec_free(self->reads);
1142 vec_free(self->writes);
1143 vec_free(self->life);
1147 bool ir_value_set_name(ir_value *self, const char *name)
1150 mem_d((void*)self->name);
1151 self->name = util_strdup(name);
1152 return !!self->name;
1155 bool ir_value_set_float(ir_value *self, float f)
1157 if (self->vtype != TYPE_FLOAT)
1159 self->constval.vfloat = f;
1160 self->hasvalue = true;
1164 bool ir_value_set_func(ir_value *self, int f)
1166 if (self->vtype != TYPE_FUNCTION)
1168 self->constval.vint = f;
1169 self->hasvalue = true;
1173 bool ir_value_set_vector(ir_value *self, vector v)
1175 if (self->vtype != TYPE_VECTOR)
1177 self->constval.vvec = v;
1178 self->hasvalue = true;
1182 bool ir_value_set_field(ir_value *self, ir_value *fld)
1184 if (self->vtype != TYPE_FIELD)
1186 self->constval.vpointer = fld;
1187 self->hasvalue = true;
1191 static char *ir_strdup(const char *str)
1194 /* actually dup empty strings */
1195 char *out = (char*)mem_a(1);
1199 return util_strdup(str);
1202 bool ir_value_set_string(ir_value *self, const char *str)
1204 if (self->vtype != TYPE_STRING)
1206 self->constval.vstring = ir_strdup(str);
1207 self->hasvalue = true;
1212 bool ir_value_set_int(ir_value *self, int i)
1214 if (self->vtype != TYPE_INTEGER)
1216 self->constval.vint = i;
1217 self->hasvalue = true;
1222 bool ir_value_lives(ir_value *self, size_t at)
1225 for (i = 0; i < vec_size(self->life); ++i)
1227 ir_life_entry_t *life = &self->life[i];
1228 if (life->start <= at && at <= life->end)
1230 if (life->start > at) /* since it's ordered */
1236 bool ir_value_life_insert(ir_value *self, size_t idx, ir_life_entry_t e)
1239 vec_push(self->life, e);
1240 for (k = vec_size(self->life)-1; k > idx; --k)
1241 self->life[k] = self->life[k-1];
1242 self->life[idx] = e;
1246 bool ir_value_life_merge(ir_value *self, size_t s)
1249 ir_life_entry_t *life = NULL;
1250 ir_life_entry_t *before = NULL;
1251 ir_life_entry_t new_entry;
1253 /* Find the first range >= s */
1254 for (i = 0; i < vec_size(self->life); ++i)
1257 life = &self->life[i];
1258 if (life->start > s)
1261 /* nothing found? append */
1262 if (i == vec_size(self->life)) {
1264 if (life && life->end+1 == s)
1266 /* previous life range can be merged in */
1270 if (life && life->end >= s)
1272 e.start = e.end = s;
1273 vec_push(self->life, e);
1279 if (before->end + 1 == s &&
1280 life->start - 1 == s)
1283 before->end = life->end;
1284 vec_remove(self->life, i, 1);
1287 if (before->end + 1 == s)
1293 /* already contained */
1294 if (before->end >= s)
1298 if (life->start - 1 == s)
1303 /* insert a new entry */
1304 new_entry.start = new_entry.end = s;
1305 return ir_value_life_insert(self, i, new_entry);
1308 bool ir_value_life_merge_into(ir_value *self, const ir_value *other)
1312 if (!vec_size(other->life))
1315 if (!vec_size(self->life)) {
1316 size_t count = vec_size(other->life);
1317 ir_life_entry_t *life = vec_add(self->life, count);
1318 memcpy(life, other->life, count * sizeof(*life));
1323 for (i = 0; i < vec_size(other->life); ++i)
1325 const ir_life_entry_t *life = &other->life[i];
1328 ir_life_entry_t *entry = &self->life[myi];
1330 if (life->end+1 < entry->start)
1332 /* adding an interval before entry */
1333 if (!ir_value_life_insert(self, myi, *life))
1339 if (life->start < entry->start &&
1340 life->end+1 >= entry->start)
1342 /* starts earlier and overlaps */
1343 entry->start = life->start;
1346 if (life->end > entry->end &&
1347 life->start <= entry->end+1)
1349 /* ends later and overlaps */
1350 entry->end = life->end;
1353 /* see if our change combines it with the next ranges */
1354 while (myi+1 < vec_size(self->life) &&
1355 entry->end+1 >= self->life[1+myi].start)
1357 /* overlaps with (myi+1) */
1358 if (entry->end < self->life[1+myi].end)
1359 entry->end = self->life[1+myi].end;
1360 vec_remove(self->life, myi+1, 1);
1361 entry = &self->life[myi];
1364 /* see if we're after the entry */
1365 if (life->start > entry->end)
1368 /* append if we're at the end */
1369 if (myi >= vec_size(self->life)) {
1370 vec_push(self->life, *life);
1373 /* otherweise check the next range */
1382 bool ir_values_overlap(const ir_value *a, const ir_value *b)
1384 /* For any life entry in A see if it overlaps with
1385 * any life entry in B.
1386 * Note that the life entries are orderes, so we can make a
1387 * more efficient algorithm there than naively translating the
1391 ir_life_entry_t *la, *lb, *enda, *endb;
1393 /* first of all, if either has no life range, they cannot clash */
1394 if (!vec_size(a->life) || !vec_size(b->life))
1399 enda = la + vec_size(a->life);
1400 endb = lb + vec_size(b->life);
1403 /* check if the entries overlap, for that,
1404 * both must start before the other one ends.
1406 if (la->start < lb->end &&
1407 lb->start < la->end)
1412 /* entries are ordered
1413 * one entry is earlier than the other
1414 * that earlier entry will be moved forward
1416 if (la->start < lb->start)
1418 /* order: A B, move A forward
1419 * check if we hit the end with A
1424 else /* if (lb->start < la->start) actually <= */
1426 /* order: B A, move B forward
1427 * check if we hit the end with B
1436 /***********************************************************************
1440 static bool ir_check_unreachable(ir_block *self)
1442 /* The IR should never have to deal with unreachable code */
1443 if (!self->final/* || OPTS_FLAG(ALLOW_UNREACHABLE_CODE)*/)
1445 irerror(self->context, "unreachable statement (%s)", self->label);
1449 bool ir_block_create_store_op(ir_block *self, lex_ctx ctx, int op, ir_value *target, ir_value *what)
1452 if (!ir_check_unreachable(self))
1455 if (target->store == store_value &&
1456 (op < INSTR_STOREP_F || op > INSTR_STOREP_FNC))
1458 irerror(self->context, "cannot store to an SSA value");
1459 irerror(self->context, "trying to store: %s <- %s", target->name, what->name);
1460 irerror(self->context, "instruction: %s", asm_instr[op].m);
1464 in = ir_instr_new(ctx, self, op);
1468 if (!ir_instr_op(in, 0, target, (op < INSTR_STOREP_F || op > INSTR_STOREP_FNC)) ||
1469 !ir_instr_op(in, 1, what, false))
1471 ir_instr_delete(in);
1474 vec_push(self->instr, in);
1478 bool ir_block_create_store(ir_block *self, lex_ctx ctx, ir_value *target, ir_value *what)
1482 if (target->vtype == TYPE_VARIANT)
1483 vtype = what->vtype;
1485 vtype = target->vtype;
1488 if (vtype == TYPE_FLOAT && what->vtype == TYPE_INTEGER)
1489 op = INSTR_CONV_ITOF;
1490 else if (vtype == TYPE_INTEGER && what->vtype == TYPE_FLOAT)
1491 op = INSTR_CONV_FTOI;
1493 op = type_store_instr[vtype];
1495 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS)) {
1496 if (op == INSTR_STORE_FLD && what->fieldtype == TYPE_VECTOR)
1500 return ir_block_create_store_op(self, ctx, op, target, what);
1503 bool ir_block_create_storep(ir_block *self, lex_ctx ctx, ir_value *target, ir_value *what)
1508 if (target->vtype != TYPE_POINTER)
1511 /* storing using pointer - target is a pointer, type must be
1512 * inferred from source
1514 vtype = what->vtype;
1516 op = type_storep_instr[vtype];
1517 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS)) {
1518 if (op == INSTR_STOREP_FLD && what->fieldtype == TYPE_VECTOR)
1519 op = INSTR_STOREP_V;
1522 return ir_block_create_store_op(self, ctx, op, target, what);
1525 bool ir_block_create_return(ir_block *self, lex_ctx ctx, ir_value *v)
1528 if (!ir_check_unreachable(self))
1531 self->is_return = true;
1532 in = ir_instr_new(ctx, self, INSTR_RETURN);
1536 if (v && !ir_instr_op(in, 0, v, false)) {
1537 ir_instr_delete(in);
1541 vec_push(self->instr, in);
1545 bool ir_block_create_if(ir_block *self, lex_ctx ctx, ir_value *v,
1546 ir_block *ontrue, ir_block *onfalse)
1549 if (!ir_check_unreachable(self))
1552 /*in = ir_instr_new(ctx, self, (v->vtype == TYPE_STRING ? INSTR_IF_S : INSTR_IF_F));*/
1553 in = ir_instr_new(ctx, self, VINSTR_COND);
1557 if (!ir_instr_op(in, 0, v, false)) {
1558 ir_instr_delete(in);
1562 in->bops[0] = ontrue;
1563 in->bops[1] = onfalse;
1565 vec_push(self->instr, in);
1567 vec_push(self->exits, ontrue);
1568 vec_push(self->exits, onfalse);
1569 vec_push(ontrue->entries, self);
1570 vec_push(onfalse->entries, self);
1574 bool ir_block_create_jump(ir_block *self, lex_ctx ctx, ir_block *to)
1577 if (!ir_check_unreachable(self))
1580 in = ir_instr_new(ctx, self, VINSTR_JUMP);
1585 vec_push(self->instr, in);
1587 vec_push(self->exits, to);
1588 vec_push(to->entries, self);
1592 bool ir_block_create_goto(ir_block *self, lex_ctx ctx, ir_block *to)
1594 self->owner->flags |= IR_FLAG_HAS_GOTO;
1595 return ir_block_create_jump(self, ctx, to);
1598 ir_instr* ir_block_create_phi(ir_block *self, lex_ctx ctx, const char *label, int ot)
1602 if (!ir_check_unreachable(self))
1604 in = ir_instr_new(ctx, self, VINSTR_PHI);
1607 out = ir_value_out(self->owner, label, store_value, ot);
1609 ir_instr_delete(in);
1612 if (!ir_instr_op(in, 0, out, true)) {
1613 ir_instr_delete(in);
1614 ir_value_delete(out);
1617 vec_push(self->instr, in);
1621 ir_value* ir_phi_value(ir_instr *self)
1623 return self->_ops[0];
1626 void ir_phi_add(ir_instr* self, ir_block *b, ir_value *v)
1630 if (!vec_ir_block_find(self->owner->entries, b, NULL)) {
1631 /* Must not be possible to cause this, otherwise the AST
1632 * is doing something wrong.
1634 irerror(self->context, "Invalid entry block for PHI");
1640 vec_push(v->reads, self);
1641 vec_push(self->phi, pe);
1644 /* call related code */
1645 ir_instr* ir_block_create_call(ir_block *self, lex_ctx ctx, const char *label, ir_value *func, bool noreturn)
1649 if (!ir_check_unreachable(self))
1651 in = ir_instr_new(ctx, self, (noreturn ? VINSTR_NRCALL : INSTR_CALL0));
1656 self->is_return = true;
1658 out = ir_value_out(self->owner, label, (func->outtype == TYPE_VOID) ? store_return : store_value, func->outtype);
1660 ir_instr_delete(in);
1663 if (!ir_instr_op(in, 0, out, true) ||
1664 !ir_instr_op(in, 1, func, false))
1666 ir_instr_delete(in);
1667 ir_value_delete(out);
1670 vec_push(self->instr, in);
1673 if (!ir_block_create_return(self, ctx, NULL)) {
1674 compile_error(ctx, "internal error: failed to generate dummy-return instruction");
1675 ir_instr_delete(in);
1683 ir_value* ir_call_value(ir_instr *self)
1685 return self->_ops[0];
1688 void ir_call_param(ir_instr* self, ir_value *v)
1690 vec_push(self->params, v);
1691 vec_push(v->reads, self);
1694 /* binary op related code */
1696 ir_value* ir_block_create_binop(ir_block *self, lex_ctx ctx,
1697 const char *label, int opcode,
1698 ir_value *left, ir_value *right)
1720 case INSTR_SUB_S: /* -- offset of string as float */
1725 case INSTR_BITOR_IF:
1726 case INSTR_BITOR_FI:
1727 case INSTR_BITAND_FI:
1728 case INSTR_BITAND_IF:
1743 case INSTR_BITAND_I:
1746 case INSTR_RSHIFT_I:
1747 case INSTR_LSHIFT_I:
1769 /* boolean operations result in floats */
1770 if (opcode >= INSTR_EQ_F && opcode <= INSTR_GT)
1772 else if (opcode >= INSTR_LE && opcode <= INSTR_GT)
1775 else if (opcode >= INSTR_LE_I && opcode <= INSTR_EQ_FI)
1780 if (ot == TYPE_VOID) {
1781 /* The AST or parser were supposed to check this! */
1785 return ir_block_create_general_instr(self, ctx, label, opcode, left, right, ot);
1788 ir_value* ir_block_create_unary(ir_block *self, lex_ctx ctx,
1789 const char *label, int opcode,
1792 int ot = TYPE_FLOAT;
1804 /* QC doesn't have other unary operations. We expect extensions to fill
1805 * the above list, otherwise we assume out-type = in-type, eg for an
1809 ot = operand->vtype;
1812 if (ot == TYPE_VOID) {
1813 /* The AST or parser were supposed to check this! */
1817 /* let's use the general instruction creator and pass NULL for OPB */
1818 return ir_block_create_general_instr(self, ctx, label, opcode, operand, NULL, ot);
1821 ir_value* ir_block_create_general_instr(ir_block *self, lex_ctx ctx, const char *label,
1822 int op, ir_value *a, ir_value *b, int outype)
1827 out = ir_value_out(self->owner, label, store_value, outype);
1831 instr = ir_instr_new(ctx, self, op);
1833 ir_value_delete(out);
1837 if (!ir_instr_op(instr, 0, out, true) ||
1838 !ir_instr_op(instr, 1, a, false) ||
1839 !ir_instr_op(instr, 2, b, false) )
1844 vec_push(self->instr, instr);
1848 ir_instr_delete(instr);
1849 ir_value_delete(out);
1853 ir_value* ir_block_create_fieldaddress(ir_block *self, lex_ctx ctx, const char *label, ir_value *ent, ir_value *field)
1857 /* Support for various pointer types todo if so desired */
1858 if (ent->vtype != TYPE_ENTITY)
1861 if (field->vtype != TYPE_FIELD)
1864 v = ir_block_create_general_instr(self, ctx, label, INSTR_ADDRESS, ent, field, TYPE_POINTER);
1865 v->fieldtype = field->fieldtype;
1869 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)
1872 if (ent->vtype != TYPE_ENTITY)
1875 /* at some point we could redirect for TYPE_POINTER... but that could lead to carelessness */
1876 if (field->vtype != TYPE_FIELD)
1881 case TYPE_FLOAT: op = INSTR_LOAD_F; break;
1882 case TYPE_VECTOR: op = INSTR_LOAD_V; break;
1883 case TYPE_STRING: op = INSTR_LOAD_S; break;
1884 case TYPE_FIELD: op = INSTR_LOAD_FLD; break;
1885 case TYPE_ENTITY: op = INSTR_LOAD_ENT; break;
1886 case TYPE_FUNCTION: op = INSTR_LOAD_FNC; break;
1888 case TYPE_POINTER: op = INSTR_LOAD_I; break;
1889 case TYPE_INTEGER: op = INSTR_LOAD_I; break;
1892 irerror(self->context, "invalid type for ir_block_create_load_from_ent: %s", type_name[outype]);
1896 return ir_block_create_general_instr(self, ctx, label, op, ent, field, outype);
1899 /* PHI resolving breaks the SSA, and must thus be the last
1900 * step before life-range calculation.
1903 static bool ir_block_naive_phi(ir_block *self);
1904 bool ir_function_naive_phi(ir_function *self)
1908 for (i = 0; i < vec_size(self->blocks); ++i)
1910 if (!ir_block_naive_phi(self->blocks[i]))
1917 static bool ir_naive_phi_emit_store(ir_block *block, size_t iid, ir_value *old, ir_value *what)
1922 /* create a store */
1923 if (!ir_block_create_store(block, old, what))
1926 /* we now move it up */
1927 instr = vec_last(block->instr);
1928 for (i = vec_size(block->instr)-1; i > iid; --i)
1929 block->instr[i] = block->instr[i-1];
1930 block->instr[i] = instr;
1936 static bool ir_block_naive_phi(ir_block *self)
1938 size_t i, p; /*, w;*/
1939 /* FIXME: optionally, create_phi can add the phis
1940 * to a list so we don't need to loop through blocks
1941 * - anyway: "don't optimize YET"
1943 for (i = 0; i < vec_size(self->instr); ++i)
1945 ir_instr *instr = self->instr[i];
1946 if (instr->opcode != VINSTR_PHI)
1949 vec_remove(self->instr, i, 1);
1950 --i; /* NOTE: i+1 below */
1952 for (p = 0; p < vec_size(instr->phi); ++p)
1954 ir_value *v = instr->phi[p].value;
1955 ir_block *b = instr->phi[p].from;
1957 if (v->store == store_value &&
1958 vec_size(v->reads) == 1 &&
1959 vec_size(v->writes) == 1)
1961 /* replace the value */
1962 if (!ir_instr_op(v->writes[0], 0, instr->_ops[0], true))
1967 /* force a move instruction */
1968 ir_instr *prevjump = vec_last(b->instr);
1971 instr->_ops[0]->store = store_global;
1972 if (!ir_block_create_store(b, instr->context, instr->_ops[0], v))
1974 instr->_ops[0]->store = store_value;
1975 vec_push(b->instr, prevjump);
1980 ir_value *v = instr->phi[p].value;
1981 for (w = 0; w < vec_size(v->writes); ++w) {
1984 if (!v->writes[w]->_ops[0])
1987 /* When the write was to a global, we have to emit a mov */
1988 old = v->writes[w]->_ops[0];
1990 /* The original instruction now writes to the PHI target local */
1991 if (v->writes[w]->_ops[0] == v)
1992 v->writes[w]->_ops[0] = instr->_ops[0];
1994 if (old->store != store_value && old->store != store_local && old->store != store_param)
1996 /* If it originally wrote to a global we need to store the value
1999 if (!ir_naive_phi_emit_store(self, i+1, old, v))
2001 if (i+1 < vec_size(self->instr))
2002 instr = self->instr[i+1];
2005 /* In case I forget and access instr later, it'll be NULL
2006 * when it's a problem, to make sure we crash, rather than accessing
2012 /* If it didn't, we can replace all reads by the phi target now. */
2014 for (r = 0; r < vec_size(old->reads); ++r)
2017 ir_instr *ri = old->reads[r];
2018 for (op = 0; op < vec_size(ri->phi); ++op) {
2019 if (ri->phi[op].value == old)
2020 ri->phi[op].value = v;
2022 for (op = 0; op < 3; ++op) {
2023 if (ri->_ops[op] == old)
2031 ir_instr_delete(instr);
2036 /***********************************************************************
2037 *IR Temp allocation code
2038 * Propagating value life ranges by walking through the function backwards
2039 * until no more changes are made.
2040 * In theory this should happen once more than once for every nested loop
2042 * Though this implementation might run an additional time for if nests.
2045 /* Enumerate instructions used by value's life-ranges
2047 static void ir_block_enumerate(ir_block *self, size_t *_eid)
2051 for (i = 0; i < vec_size(self->instr); ++i)
2053 self->instr[i]->eid = eid++;
2058 /* Enumerate blocks and instructions.
2059 * The block-enumeration is unordered!
2060 * We do not really use the block enumreation, however
2061 * the instruction enumeration is important for life-ranges.
2063 void ir_function_enumerate(ir_function *self)
2066 size_t instruction_id = 1;
2067 for (i = 0; i < vec_size(self->blocks); ++i)
2069 self->blocks[i]->eid = i;
2070 self->blocks[i]->run_id = 0;
2071 ir_block_enumerate(self->blocks[i], &instruction_id);
2075 static bool ir_block_life_propagate(ir_block *b, ir_block *prev, bool *changed);
2076 bool ir_function_calculate_liferanges(ir_function *self)
2081 /* parameters live at 0 */
2082 for (i = 0; i < vec_size(self->params); ++i)
2083 ir_value_life_merge(self->locals[i], 0);
2088 for (i = 0; i != vec_size(self->blocks); ++i)
2090 if (self->blocks[i]->is_return)
2092 vec_free(self->blocks[i]->living);
2093 if (!ir_block_life_propagate(self->blocks[i], NULL, &changed))
2098 if (vec_size(self->blocks)) {
2099 ir_block *block = self->blocks[0];
2100 for (i = 0; i < vec_size(block->living); ++i) {
2101 ir_value *v = block->living[i];
2102 if (v->store != store_local)
2104 if (v->vtype == TYPE_VECTOR)
2106 self->flags |= IR_FLAG_HAS_UNINITIALIZED;
2107 /* find the instruction reading from it */
2108 for (s = 0; s < vec_size(v->reads); ++s) {
2109 if (v->reads[s]->eid == v->life[0].end)
2112 if (s < vec_size(v->reads)) {
2113 if (irwarning(v->context, WARN_USED_UNINITIALIZED,
2114 "variable `%s` may be used uninitialized in this function\n"
2117 v->reads[s]->context.file, v->reads[s]->context.line)
2125 ir_value *vec = v->memberof;
2126 for (s = 0; s < vec_size(vec->reads); ++s) {
2127 if (vec->reads[s]->eid == v->life[0].end)
2130 if (s < vec_size(vec->reads)) {
2131 if (irwarning(v->context, WARN_USED_UNINITIALIZED,
2132 "variable `%s` may be used uninitialized in this function\n"
2135 vec->reads[s]->context.file, vec->reads[s]->context.line)
2143 if (irwarning(v->context, WARN_USED_UNINITIALIZED,
2144 "variable `%s` may be used uninitialized in this function", v->name))
2153 /* Local-value allocator
2154 * After finishing creating the liferange of all values used in a function
2155 * we can allocate their global-positions.
2156 * This is the counterpart to register-allocation in register machines.
2163 } function_allocator;
2165 static bool function_allocator_alloc(function_allocator *alloc, ir_value *var)
2168 size_t vsize = ir_value_sizeof(var);
2170 var->code.local = vec_size(alloc->locals);
2172 slot = ir_value_var("reg", store_global, var->vtype);
2176 if (!ir_value_life_merge_into(slot, var))
2179 vec_push(alloc->locals, slot);
2180 vec_push(alloc->sizes, vsize);
2181 vec_push(alloc->unique, var->unique_life);
2186 ir_value_delete(slot);
2190 static bool ir_function_allocator_assign(ir_function *self, function_allocator *alloc, ir_value *v)
2195 for (a = 0; a < vec_size(alloc->locals); ++a)
2197 /* if it's reserved for a unique liferange: skip */
2198 if (alloc->unique[a])
2201 slot = alloc->locals[a];
2203 /* never resize parameters
2204 * will be required later when overlapping temps + locals
2206 if (a < vec_size(self->params) &&
2207 alloc->sizes[a] < ir_value_sizeof(v))
2212 if (ir_values_overlap(v, slot))
2215 if (!ir_value_life_merge_into(slot, v))
2218 /* adjust size for this slot */
2219 if (alloc->sizes[a] < ir_value_sizeof(v))
2220 alloc->sizes[a] = ir_value_sizeof(v);
2225 if (a >= vec_size(alloc->locals)) {
2226 if (!function_allocator_alloc(alloc, v))
2232 bool ir_function_allocate_locals(ir_function *self)
2237 bool opt_gt = OPTS_OPTIMIZATION(OPTIM_GLOBAL_TEMPS);
2241 function_allocator lockalloc, globalloc;
2243 if (!vec_size(self->locals) && !vec_size(self->values))
2246 globalloc.locals = NULL;
2247 globalloc.sizes = NULL;
2248 globalloc.positions = NULL;
2249 globalloc.unique = NULL;
2250 lockalloc.locals = NULL;
2251 lockalloc.sizes = NULL;
2252 lockalloc.positions = NULL;
2253 lockalloc.unique = NULL;
2255 for (i = 0; i < vec_size(self->locals); ++i)
2257 v = self->locals[i];
2258 if (!OPTS_OPTIMIZATION(OPTIM_LOCAL_TEMPS)) {
2260 v->unique_life = true;
2262 else if (i >= vec_size(self->params))
2265 v->locked = true; /* lock parameters locals */
2266 if (!function_allocator_alloc((v->locked || !opt_gt ? &lockalloc : &globalloc), self->locals[i]))
2269 for (; i < vec_size(self->locals); ++i)
2271 v = self->locals[i];
2272 if (!vec_size(v->life))
2274 if (!ir_function_allocator_assign(self, (v->locked || !opt_gt ? &lockalloc : &globalloc), v))
2278 /* Allocate a slot for any value that still exists */
2279 for (i = 0; i < vec_size(self->values); ++i)
2281 v = self->values[i];
2283 if (!vec_size(v->life))
2286 /* CALL optimization:
2287 * If the value is a parameter-temp: 1 write, 1 read from a CALL
2288 * and it's not "locked", write it to the OFS_PARM directly.
2290 if (OPTS_OPTIMIZATION(OPTIM_CALL_STORES) && !v->locked && !v->unique_life) {
2291 if (vec_size(v->reads) == 1 && vec_size(v->writes) == 1 &&
2292 (v->reads[0]->opcode == VINSTR_NRCALL ||
2293 (v->reads[0]->opcode >= INSTR_CALL0 && v->reads[0]->opcode <= INSTR_CALL8)
2298 ir_instr *call = v->reads[0];
2299 if (!vec_ir_value_find(call->params, v, ¶m)) {
2300 irerror(call->context, "internal error: unlocked parameter %s not found", v->name);
2304 ++opts_optimizationcount[OPTIM_CALL_STORES];
2305 v->callparam = true;
2307 ir_value_code_setaddr(v, OFS_PARM0 + 3*param);
2311 if (vec_size(self->owner->extparam_protos) <= param)
2312 ep = ir_gen_extparam_proto(self->owner);
2314 ep = self->owner->extparam_protos[param];
2315 ir_instr_op(v->writes[0], 0, ep, true);
2316 call->params[param+8] = ep;
2320 if (vec_size(v->writes) == 1 && v->writes[0]->opcode == INSTR_CALL0)
2322 v->store = store_return;
2323 if (v->members[0]) v->members[0]->store = store_return;
2324 if (v->members[1]) v->members[1]->store = store_return;
2325 if (v->members[2]) v->members[2]->store = store_return;
2326 ++opts_optimizationcount[OPTIM_CALL_STORES];
2331 if (!ir_function_allocator_assign(self, (v->locked || !opt_gt ? &lockalloc : &globalloc), v))
2335 if (!lockalloc.sizes && !globalloc.sizes) {
2338 vec_push(lockalloc.positions, 0);
2339 vec_push(globalloc.positions, 0);
2341 /* Adjust slot positions based on sizes */
2342 if (lockalloc.sizes) {
2343 pos = (vec_size(lockalloc.sizes) ? lockalloc.positions[0] : 0);
2344 for (i = 1; i < vec_size(lockalloc.sizes); ++i)
2346 pos = lockalloc.positions[i-1] + lockalloc.sizes[i-1];
2347 vec_push(lockalloc.positions, pos);
2349 self->allocated_locals = pos + vec_last(lockalloc.sizes);
2351 if (globalloc.sizes) {
2352 pos = (vec_size(globalloc.sizes) ? globalloc.positions[0] : 0);
2353 for (i = 1; i < vec_size(globalloc.sizes); ++i)
2355 pos = globalloc.positions[i-1] + globalloc.sizes[i-1];
2356 vec_push(globalloc.positions, pos);
2358 self->globaltemps = pos + vec_last(globalloc.sizes);
2361 /* Locals need to know their new position */
2362 for (i = 0; i < vec_size(self->locals); ++i) {
2363 v = self->locals[i];
2364 if (i >= vec_size(self->params) && !vec_size(v->life))
2366 if (v->locked || !opt_gt)
2367 v->code.local = lockalloc.positions[v->code.local];
2369 v->code.local = globalloc.positions[v->code.local];
2371 /* Take over the actual slot positions on values */
2372 for (i = 0; i < vec_size(self->values); ++i) {
2373 v = self->values[i];
2374 if (!vec_size(v->life))
2376 if (v->locked || !opt_gt)
2377 v->code.local = lockalloc.positions[v->code.local];
2379 v->code.local = globalloc.positions[v->code.local];
2387 for (i = 0; i < vec_size(lockalloc.locals); ++i)
2388 ir_value_delete(lockalloc.locals[i]);
2389 for (i = 0; i < vec_size(globalloc.locals); ++i)
2390 ir_value_delete(globalloc.locals[i]);
2391 vec_free(globalloc.unique);
2392 vec_free(globalloc.locals);
2393 vec_free(globalloc.sizes);
2394 vec_free(globalloc.positions);
2395 vec_free(lockalloc.unique);
2396 vec_free(lockalloc.locals);
2397 vec_free(lockalloc.sizes);
2398 vec_free(lockalloc.positions);
2402 /* Get information about which operand
2403 * is read from, or written to.
2405 static void ir_op_read_write(int op, size_t *read, size_t *write)
2425 case INSTR_STOREP_F:
2426 case INSTR_STOREP_V:
2427 case INSTR_STOREP_S:
2428 case INSTR_STOREP_ENT:
2429 case INSTR_STOREP_FLD:
2430 case INSTR_STOREP_FNC:
2441 static bool ir_block_living_add_instr(ir_block *self, size_t eid)
2444 bool changed = false;
2446 for (i = 0; i != vec_size(self->living); ++i)
2448 tempbool = ir_value_life_merge(self->living[i], eid);
2449 changed = changed || tempbool;
2454 static bool ir_block_living_lock(ir_block *self)
2457 bool changed = false;
2458 for (i = 0; i != vec_size(self->living); ++i)
2460 if (!self->living[i]->locked)
2462 self->living[i]->locked = true;
2467 static bool ir_block_life_prop_previous(ir_block* self, ir_block *prev, bool *changed)
2473 /* values which have been read in a previous iteration are now
2474 * in the "living" array even if the previous block doesn't use them.
2475 * So we have to remove whatever does not exist in the previous block.
2476 * They will be re-added on-read, but the liferange merge won't cause
2478 for (i = 0; i < vec_size(self->living); ++i)
2480 if (!vec_ir_value_find(prev->living, self->living[i], NULL)) {
2481 vec_remove(self->living, i, 1);
2487 /* Whatever the previous block still has in its living set
2488 * must now be added to ours as well.
2490 for (i = 0; i < vec_size(prev->living); ++i)
2492 if (vec_ir_value_find(self->living, prev->living[i], NULL))
2494 vec_push(self->living, prev->living[i]);
2496 irerror(self->contextt from prev: %s", self->label, prev->living[i]->_name);
2502 static bool ir_block_life_propagate(ir_block *self, ir_block *prev, bool *changed)
2507 size_t i, o, p, mem;
2508 /* bitmasks which operands are read from or written to */
2517 if (!ir_block_life_prop_previous(self, prev, changed))
2521 i = vec_size(self->instr);
2524 instr = self->instr[i];
2526 /* See which operands are read and write operands */
2527 ir_op_read_write(instr->opcode, &read, &write);
2529 if (instr->opcode == INSTR_MUL_VF)
2531 /* the float source will get an additional lifetime */
2532 tempbool = ir_value_life_merge(instr->_ops[2], instr->eid+1);
2533 *changed = *changed || tempbool;
2535 else if (instr->opcode == INSTR_MUL_FV)
2537 /* the float source will get an additional lifetime */
2538 tempbool = ir_value_life_merge(instr->_ops[1], instr->eid+1);
2539 *changed = *changed || tempbool;
2542 /* Go through the 3 main operands
2543 * writes first, then reads
2545 for (o = 0; o < 3; ++o)
2547 if (!instr->_ops[o]) /* no such operand */
2550 value = instr->_ops[o];
2552 /* We only care about locals */
2553 /* we also calculate parameter liferanges so that locals
2554 * can take up parameter slots */
2555 if (value->store != store_value &&
2556 value->store != store_local &&
2557 value->store != store_param)
2560 /* write operands */
2561 /* When we write to a local, we consider it "dead" for the
2562 * remaining upper part of the function, since in SSA a value
2563 * can only be written once (== created)
2568 bool in_living = vec_ir_value_find(self->living, value, &idx);
2571 /* If the value isn't alive it hasn't been read before... */
2572 /* TODO: See if the warning can be emitted during parsing or AST processing
2573 * otherwise have warning printed here.
2574 * IF printing a warning here: include filecontext_t,
2575 * and make sure it's only printed once
2576 * since this function is run multiple times.
2578 /* con_err( "Value only written %s\n", value->name); */
2579 tempbool = ir_value_life_merge(value, instr->eid);
2580 *changed = *changed || tempbool;
2582 /* since 'living' won't contain it
2583 * anymore, merge the value, since
2586 tempbool = ir_value_life_merge(value, instr->eid);
2587 *changed = *changed || tempbool;
2589 vec_remove(self->living, idx, 1);
2591 /* Removing a vector removes all members */
2592 for (mem = 0; mem < 3; ++mem) {
2593 if (value->members[mem] && vec_ir_value_find(self->living, value->members[mem], &idx)) {
2594 tempbool = ir_value_life_merge(value->members[mem], instr->eid);
2595 *changed = *changed || tempbool;
2596 vec_remove(self->living, idx, 1);
2599 /* Removing the last member removes the vector */
2600 if (value->memberof) {
2601 value = value->memberof;
2602 for (mem = 0; mem < 3; ++mem) {
2603 if (value->members[mem] && vec_ir_value_find(self->living, value->members[mem], NULL))
2606 if (mem == 3 && vec_ir_value_find(self->living, value, &idx)) {
2607 tempbool = ir_value_life_merge(value, instr->eid);
2608 *changed = *changed || tempbool;
2609 vec_remove(self->living, idx, 1);
2615 for (o = 0; o < 3; ++o)
2617 if (!instr->_ops[o]) /* no such operand */
2620 value = instr->_ops[o];
2622 /* We only care about locals */
2623 /* we also calculate parameter liferanges so that locals
2624 * can take up parameter slots */
2625 if (value->store != store_value &&
2626 value->store != store_local &&
2627 value->store != store_param)
2633 if (!vec_ir_value_find(self->living, value, NULL))
2634 vec_push(self->living, value);
2635 /* reading adds the full vector */
2636 if (value->memberof && !vec_ir_value_find(self->living, value->memberof, NULL))
2637 vec_push(self->living, value->memberof);
2638 for (mem = 0; mem < 3; ++mem) {
2639 if (value->members[mem] && !vec_ir_value_find(self->living, value->members[mem], NULL))
2640 vec_push(self->living, value->members[mem]);
2644 /* PHI operands are always read operands */
2645 for (p = 0; p < vec_size(instr->phi); ++p)
2647 value = instr->phi[p].value;
2648 if (!vec_ir_value_find(self->living, value, NULL))
2649 vec_push(self->living, value);
2650 /* reading adds the full vector */
2651 if (value->memberof && !vec_ir_value_find(self->living, value->memberof, NULL))
2652 vec_push(self->living, value->memberof);
2653 for (mem = 0; mem < 3; ++mem) {
2654 if (value->members[mem] && !vec_ir_value_find(self->living, value->members[mem], NULL))
2655 vec_push(self->living, value->members[mem]);
2659 /* on a call, all these values must be "locked" */
2660 if (instr->opcode >= INSTR_CALL0 && instr->opcode <= INSTR_CALL8) {
2661 if (ir_block_living_lock(self))
2664 /* call params are read operands too */
2665 for (p = 0; p < vec_size(instr->params); ++p)
2667 value = instr->params[p];
2668 if (!vec_ir_value_find(self->living, value, NULL))
2669 vec_push(self->living, value);
2670 /* reading adds the full vector */
2671 if (value->memberof && !vec_ir_value_find(self->living, value->memberof, NULL))
2672 vec_push(self->living, value->memberof);
2673 for (mem = 0; mem < 3; ++mem) {
2674 if (value->members[mem] && !vec_ir_value_find(self->living, value->members[mem], NULL))
2675 vec_push(self->living, value->members[mem]);
2680 tempbool = ir_block_living_add_instr(self, instr->eid);
2681 /*con_err( "living added values\n");*/
2682 *changed = *changed || tempbool;
2686 if (self->run_id == self->owner->run_id)
2689 self->run_id = self->owner->run_id;
2691 for (i = 0; i < vec_size(self->entries); ++i)
2693 ir_block *entry = self->entries[i];
2694 ir_block_life_propagate(entry, self, changed);
2700 /***********************************************************************
2703 * Since the IR has the convention of putting 'write' operands
2704 * at the beginning, we have to rotate the operands of instructions
2705 * properly in order to generate valid QCVM code.
2707 * Having destinations at a fixed position is more convenient. In QC
2708 * this is *mostly* OPC, but FTE adds at least 2 instructions which
2709 * read from from OPA, and store to OPB rather than OPC. Which is
2710 * partially the reason why the implementation of these instructions
2711 * in darkplaces has been delayed for so long.
2713 * Breaking conventions is annoying...
2715 static bool ir_builder_gen_global(ir_builder *self, ir_value *global, bool islocal);
2717 static bool gen_global_field(ir_value *global)
2719 if (global->hasvalue)
2721 ir_value *fld = global->constval.vpointer;
2723 irerror(global->context, "Invalid field constant with no field: %s", global->name);
2727 /* copy the field's value */
2728 ir_value_code_setaddr(global, vec_size(code_globals));
2729 vec_push(code_globals, fld->code.fieldaddr);
2730 if (global->fieldtype == TYPE_VECTOR) {
2731 vec_push(code_globals, fld->code.fieldaddr+1);
2732 vec_push(code_globals, fld->code.fieldaddr+2);
2737 ir_value_code_setaddr(global, vec_size(code_globals));
2738 vec_push(code_globals, 0);
2739 if (global->fieldtype == TYPE_VECTOR) {
2740 vec_push(code_globals, 0);
2741 vec_push(code_globals, 0);
2744 if (global->code.globaladdr < 0)
2749 static bool gen_global_pointer(ir_value *global)
2751 if (global->hasvalue)
2753 ir_value *target = global->constval.vpointer;
2755 irerror(global->context, "Invalid pointer constant: %s", global->name);
2756 /* NULL pointers are pointing to the NULL constant, which also
2757 * sits at address 0, but still has an ir_value for itself.
2762 /* Here, relocations ARE possible - in fteqcc-enhanced-qc:
2763 * void() foo; <- proto
2764 * void() *fooptr = &foo;
2765 * void() foo = { code }
2767 if (!target->code.globaladdr) {
2768 /* FIXME: Check for the constant nullptr ir_value!
2769 * because then code.globaladdr being 0 is valid.
2771 irerror(global->context, "FIXME: Relocation support");
2775 ir_value_code_setaddr(global, vec_size(code_globals));
2776 vec_push(code_globals, target->code.globaladdr);
2780 ir_value_code_setaddr(global, vec_size(code_globals));
2781 vec_push(code_globals, 0);
2783 if (global->code.globaladdr < 0)
2788 static bool gen_blocks_recursive(ir_function *func, ir_block *block)
2790 prog_section_statement stmt;
2799 block->generated = true;
2800 block->code_start = vec_size(code_statements);
2801 for (i = 0; i < vec_size(block->instr); ++i)
2803 instr = block->instr[i];
2805 if (instr->opcode == VINSTR_PHI) {
2806 irerror(block->context, "cannot generate virtual instruction (phi)");
2810 if (instr->opcode == VINSTR_JUMP) {
2811 target = instr->bops[0];
2812 /* for uncoditional jumps, if the target hasn't been generated
2813 * yet, we generate them right here.
2815 if (!target->generated) {
2820 /* otherwise we generate a jump instruction */
2821 stmt.opcode = INSTR_GOTO;
2822 stmt.o1.s1 = (target->code_start) - vec_size(code_statements);
2825 if (stmt.o1.s1 != 1)
2826 code_push_statement(&stmt, instr->context.line);
2828 /* no further instructions can be in this block */
2832 if (instr->opcode == VINSTR_COND) {
2833 ontrue = instr->bops[0];
2834 onfalse = instr->bops[1];
2835 /* TODO: have the AST signal which block should
2836 * come first: eg. optimize IFs without ELSE...
2839 stmt.o1.u1 = ir_value_code_addr(instr->_ops[0]);
2843 if (ontrue->generated) {
2844 stmt.opcode = INSTR_IF;
2845 stmt.o2.s1 = (ontrue->code_start) - vec_size(code_statements);
2846 if (stmt.o2.s1 != 1)
2847 code_push_statement(&stmt, instr->context.line);
2849 if (onfalse->generated) {
2850 stmt.opcode = INSTR_IFNOT;
2851 stmt.o2.s1 = (onfalse->code_start) - vec_size(code_statements);
2852 if (stmt.o2.s1 != 1)
2853 code_push_statement(&stmt, instr->context.line);
2855 if (!ontrue->generated) {
2856 if (onfalse->generated) {
2861 if (!onfalse->generated) {
2862 if (ontrue->generated) {
2867 /* neither ontrue nor onfalse exist */
2868 stmt.opcode = INSTR_IFNOT;
2869 if (!instr->likely) {
2870 /* Honor the likelyhood hint */
2871 ir_block *tmp = onfalse;
2872 stmt.opcode = INSTR_IF;
2876 stidx = vec_size(code_statements);
2877 code_push_statement(&stmt, instr->context.line);
2878 /* on false we jump, so add ontrue-path */
2879 if (!gen_blocks_recursive(func, ontrue))
2881 /* fixup the jump address */
2882 code_statements[stidx].o2.s1 = vec_size(code_statements) - stidx;
2883 /* generate onfalse path */
2884 if (onfalse->generated) {
2885 /* fixup the jump address */
2886 code_statements[stidx].o2.s1 = (onfalse->code_start) - (stidx);
2887 if (code_statements[stidx].o2.s1 == 1) {
2888 code_statements[stidx] = code_statements[stidx+1];
2889 if (code_statements[stidx].o1.s1 < 0)
2890 code_statements[stidx].o1.s1++;
2891 code_pop_statement();
2893 stmt.opcode = vec_last(code_statements).opcode;
2894 if (stmt.opcode == INSTR_GOTO ||
2895 stmt.opcode == INSTR_IF ||
2896 stmt.opcode == INSTR_IFNOT ||
2897 stmt.opcode == INSTR_RETURN ||
2898 stmt.opcode == INSTR_DONE)
2900 /* no use jumping from here */
2903 /* may have been generated in the previous recursive call */
2904 stmt.opcode = INSTR_GOTO;
2905 stmt.o1.s1 = (onfalse->code_start) - vec_size(code_statements);
2908 if (stmt.o1.s1 != 1)
2909 code_push_statement(&stmt, instr->context.line);
2912 else if (code_statements[stidx].o2.s1 == 1) {
2913 code_statements[stidx] = code_statements[stidx+1];
2914 if (code_statements[stidx].o1.s1 < 0)
2915 code_statements[stidx].o1.s1++;
2916 code_pop_statement();
2918 /* if not, generate now */
2923 if ( (instr->opcode >= INSTR_CALL0 && instr->opcode <= INSTR_CALL8)
2924 || instr->opcode == VINSTR_NRCALL)
2929 first = vec_size(instr->params);
2932 for (p = 0; p < first; ++p)
2934 ir_value *param = instr->params[p];
2935 if (param->callparam)
2938 stmt.opcode = INSTR_STORE_F;
2941 if (param->vtype == TYPE_FIELD)
2942 stmt.opcode = field_store_instr[param->fieldtype];
2943 else if (param->vtype == TYPE_NIL)
2944 stmt.opcode = INSTR_STORE_V;
2946 stmt.opcode = type_store_instr[param->vtype];
2947 stmt.o1.u1 = ir_value_code_addr(param);
2948 stmt.o2.u1 = OFS_PARM0 + 3 * p;
2949 code_push_statement(&stmt, instr->context.line);
2951 /* Now handle extparams */
2952 first = vec_size(instr->params);
2953 for (; p < first; ++p)
2955 ir_builder *ir = func->owner;
2956 ir_value *param = instr->params[p];
2957 ir_value *targetparam;
2959 if (param->callparam)
2962 if (p-8 >= vec_size(ir->extparams))
2963 ir_gen_extparam(ir);
2965 targetparam = ir->extparams[p-8];
2967 stmt.opcode = INSTR_STORE_F;
2970 if (param->vtype == TYPE_FIELD)
2971 stmt.opcode = field_store_instr[param->fieldtype];
2972 else if (param->vtype == TYPE_NIL)
2973 stmt.opcode = INSTR_STORE_V;
2975 stmt.opcode = type_store_instr[param->vtype];
2976 stmt.o1.u1 = ir_value_code_addr(param);
2977 stmt.o2.u1 = ir_value_code_addr(targetparam);
2978 code_push_statement(&stmt, instr->context.line);
2981 stmt.opcode = INSTR_CALL0 + vec_size(instr->params);
2982 if (stmt.opcode > INSTR_CALL8)
2983 stmt.opcode = INSTR_CALL8;
2984 stmt.o1.u1 = ir_value_code_addr(instr->_ops[1]);
2987 code_push_statement(&stmt, instr->context.line);
2989 retvalue = instr->_ops[0];
2990 if (retvalue && retvalue->store != store_return &&
2991 (retvalue->store == store_global || vec_size(retvalue->life)))
2993 /* not to be kept in OFS_RETURN */
2994 if (retvalue->vtype == TYPE_FIELD && OPTS_FLAG(ADJUST_VECTOR_FIELDS))
2995 stmt.opcode = field_store_instr[retvalue->fieldtype];
2997 stmt.opcode = type_store_instr[retvalue->vtype];
2998 stmt.o1.u1 = OFS_RETURN;
2999 stmt.o2.u1 = ir_value_code_addr(retvalue);
3001 code_push_statement(&stmt, instr->context.line);
3006 if (instr->opcode == INSTR_STATE) {
3007 irerror(block->context, "TODO: state instruction");
3011 stmt.opcode = instr->opcode;
3016 /* This is the general order of operands */
3018 stmt.o3.u1 = ir_value_code_addr(instr->_ops[0]);
3021 stmt.o1.u1 = ir_value_code_addr(instr->_ops[1]);
3024 stmt.o2.u1 = ir_value_code_addr(instr->_ops[2]);
3026 if (stmt.opcode == INSTR_RETURN || stmt.opcode == INSTR_DONE)
3028 stmt.o1.u1 = stmt.o3.u1;
3031 else if ((stmt.opcode >= INSTR_STORE_F &&
3032 stmt.opcode <= INSTR_STORE_FNC) ||
3033 (stmt.opcode >= INSTR_STOREP_F &&
3034 stmt.opcode <= INSTR_STOREP_FNC))
3036 /* 2-operand instructions with A -> B */
3037 stmt.o2.u1 = stmt.o3.u1;
3040 /* tiny optimization, don't output
3043 if (stmt.o2.u1 == stmt.o1.u1 &&
3044 OPTS_OPTIMIZATION(OPTIM_PEEPHOLE))
3046 ++opts_optimizationcount[OPTIM_PEEPHOLE];
3051 code_push_statement(&stmt, instr->context.line);
3056 static bool gen_function_code(ir_function *self)
3059 prog_section_statement stmt, *retst;
3061 /* Starting from entry point, we generate blocks "as they come"
3062 * for now. Dead blocks will not be translated obviously.
3064 if (!vec_size(self->blocks)) {
3065 irerror(self->context, "Function '%s' declared without body.", self->name);
3069 block = self->blocks[0];
3070 if (block->generated)
3073 if (!gen_blocks_recursive(self, block)) {
3074 irerror(self->context, "failed to generate blocks for '%s'", self->name);
3078 /* code_write and qcvm -disasm need to know that the function ends here */
3079 retst = &vec_last(code_statements);
3080 if (OPTS_OPTIMIZATION(OPTIM_VOID_RETURN) &&
3081 self->outtype == TYPE_VOID &&
3082 retst->opcode == INSTR_RETURN &&
3083 !retst->o1.u1 && !retst->o2.u1 && !retst->o3.u1)
3085 retst->opcode = INSTR_DONE;
3086 ++opts_optimizationcount[OPTIM_VOID_RETURN];
3088 stmt.opcode = INSTR_DONE;
3092 code_push_statement(&stmt, vec_last(code_linenums));
3097 static qcint ir_builder_filestring(ir_builder *ir, const char *filename)
3099 /* NOTE: filename pointers are copied, we never strdup them,
3100 * thus we can use pointer-comparison to find the string.
3105 for (i = 0; i < vec_size(ir->filenames); ++i) {
3106 if (ir->filenames[i] == filename)
3107 return ir->filestrings[i];
3110 str = code_genstring(filename);
3111 vec_push(ir->filenames, filename);
3112 vec_push(ir->filestrings, str);
3116 static bool gen_global_function(ir_builder *ir, ir_value *global)
3118 prog_section_function fun;
3123 if (!global->hasvalue || (!global->constval.vfunc))
3125 irerror(global->context, "Invalid state of function-global: not constant: %s", global->name);
3129 irfun = global->constval.vfunc;
3131 fun.name = global->code.name;
3132 fun.file = ir_builder_filestring(ir, global->context.file);
3133 fun.profile = 0; /* always 0 */
3134 fun.nargs = vec_size(irfun->params);
3138 for (i = 0;i < 8; ++i) {
3139 if ((int32_t)i >= fun.nargs)
3142 fun.argsize[i] = type_sizeof_[irfun->params[i]];
3146 fun.locals = irfun->allocated_locals;
3149 fun.entry = irfun->builtin+1;
3151 irfun->code_function_def = vec_size(code_functions);
3152 fun.entry = vec_size(code_statements);
3155 vec_push(code_functions, fun);
3159 static ir_value* ir_gen_extparam_proto(ir_builder *ir)
3164 snprintf(name, sizeof(name), "EXTPARM#%i", (int)(vec_size(ir->extparam_protos)+8));
3165 global = ir_value_var(name, store_global, TYPE_VECTOR);
3167 vec_push(ir->extparam_protos, global);
3171 static void ir_gen_extparam(ir_builder *ir)
3173 prog_section_def def;
3176 if (vec_size(ir->extparam_protos) < vec_size(ir->extparams)+1)
3177 global = ir_gen_extparam_proto(ir);
3179 global = ir->extparam_protos[vec_size(ir->extparams)];
3181 def.name = code_genstring(global->name);
3182 def.type = TYPE_VECTOR;
3183 def.offset = vec_size(code_globals);
3185 vec_push(code_defs, def);
3186 ir_value_code_setaddr(global, def.offset);
3187 vec_push(code_globals, 0);
3188 vec_push(code_globals, 0);
3189 vec_push(code_globals, 0);
3191 vec_push(ir->extparams, global);
3194 static bool gen_function_extparam_copy(ir_function *self)
3196 size_t i, ext, numparams;
3198 ir_builder *ir = self->owner;
3200 prog_section_statement stmt;
3202 numparams = vec_size(self->params);
3206 stmt.opcode = INSTR_STORE_F;
3208 for (i = 8; i < numparams; ++i) {
3210 if (ext >= vec_size(ir->extparams))
3211 ir_gen_extparam(ir);
3213 ep = ir->extparams[ext];
3215 stmt.opcode = type_store_instr[self->locals[i]->vtype];
3216 if (self->locals[i]->vtype == TYPE_FIELD &&
3217 self->locals[i]->fieldtype == TYPE_VECTOR)
3219 stmt.opcode = INSTR_STORE_V;
3221 stmt.o1.u1 = ir_value_code_addr(ep);
3222 stmt.o2.u1 = ir_value_code_addr(self->locals[i]);
3223 code_push_statement(&stmt, self->context.line);
3229 static bool gen_function_locals(ir_builder *ir, ir_value *global)
3231 prog_section_function *def;
3234 uint32_t firstlocal, firstglobal;
3236 irfun = global->constval.vfunc;
3237 def = code_functions + irfun->code_function_def;
3239 if (opts.g || !OPTS_OPTIMIZATION(OPTIM_OVERLAP_LOCALS) || (irfun->flags & IR_FLAG_MASK_NO_OVERLAP))
3240 firstlocal = def->firstlocal = vec_size(code_globals);
3242 firstlocal = def->firstlocal = ir->first_common_local;
3243 ++opts_optimizationcount[OPTIM_OVERLAP_LOCALS];
3246 firstglobal = (OPTS_OPTIMIZATION(OPTIM_GLOBAL_TEMPS) ? ir->first_common_globaltemp : firstlocal);
3248 for (i = vec_size(code_globals); i < firstlocal + irfun->allocated_locals; ++i)
3249 vec_push(code_globals, 0);
3250 for (i = 0; i < vec_size(irfun->locals); ++i) {
3251 ir_value *v = irfun->locals[i];
3252 if (v->locked || !OPTS_OPTIMIZATION(OPTIM_GLOBAL_TEMPS)) {
3253 ir_value_code_setaddr(v, firstlocal + v->code.local);
3254 if (!ir_builder_gen_global(ir, irfun->locals[i], true)) {
3255 irerror(irfun->locals[i]->context, "failed to generate local %s", irfun->locals[i]->name);
3260 ir_value_code_setaddr(v, firstglobal + v->code.local);
3262 for (i = 0; i < vec_size(irfun->values); ++i)
3264 ir_value *v = irfun->values[i];
3268 ir_value_code_setaddr(v, firstlocal + v->code.local);
3270 ir_value_code_setaddr(v, firstglobal + v->code.local);
3275 static bool gen_global_function_code(ir_builder *ir, ir_value *global)
3277 prog_section_function *fundef;
3282 irfun = global->constval.vfunc;
3284 if (global->cvq == CV_NONE) {
3285 irwarning(global->context, WARN_IMPLICIT_FUNCTION_POINTER,
3286 "function `%s` has no body and in QC implicitly becomes a function-pointer", global->name);
3288 /* this was a function pointer, don't generate code for those */
3295 if (irfun->code_function_def < 0) {
3296 irerror(irfun->context, "`%s`: IR global wasn't generated, failed to access function-def", irfun->name);
3299 fundef = &code_functions[irfun->code_function_def];
3301 fundef->entry = vec_size(code_statements);
3302 if (!gen_function_locals(ir, global)) {
3303 irerror(irfun->context, "Failed to generate locals for function %s", irfun->name);
3306 if (!gen_function_extparam_copy(irfun)) {
3307 irerror(irfun->context, "Failed to generate extparam-copy code for function %s", irfun->name);
3310 if (!gen_function_code(irfun)) {
3311 irerror(irfun->context, "Failed to generate code for function %s", irfun->name);
3317 static void gen_vector_defs(prog_section_def def, const char *name)
3322 if (!name || name[0] == '#' || OPTS_FLAG(SINGLE_VECTOR_DEFS))
3325 def.type = TYPE_FLOAT;
3329 component = (char*)mem_a(len+3);
3330 memcpy(component, name, len);
3332 component[len-0] = 0;
3333 component[len-2] = '_';
3335 component[len-1] = 'x';
3337 for (i = 0; i < 3; ++i) {
3338 def.name = code_genstring(component);
3339 vec_push(code_defs, def);
3345 static void gen_vector_fields(prog_section_field fld, const char *name)
3350 if (!name || OPTS_FLAG(SINGLE_VECTOR_DEFS))
3353 fld.type = TYPE_FLOAT;
3357 component = (char*)mem_a(len+3);
3358 memcpy(component, name, len);
3360 component[len-0] = 0;
3361 component[len-2] = '_';
3363 component[len-1] = 'x';
3365 for (i = 0; i < 3; ++i) {
3366 fld.name = code_genstring(component);
3367 vec_push(code_fields, fld);
3373 static bool ir_builder_gen_global(ir_builder *self, ir_value *global, bool islocal)
3377 prog_section_def def;
3378 bool pushdef = false;
3380 def.type = global->vtype;
3381 def.offset = vec_size(code_globals);
3383 if (opts.g || !islocal)
3387 if (OPTS_OPTIMIZATION(OPTIM_STRIP_CONSTANT_NAMES) &&
3388 (global->name[0] == '#' || global->cvq == CV_CONST))
3393 if (pushdef && global->name) {
3394 if (global->name[0] == '#') {
3395 if (!self->str_immediate)
3396 self->str_immediate = code_genstring("IMMEDIATE");
3397 def.name = global->code.name = self->str_immediate;
3400 def.name = global->code.name = code_genstring(global->name);
3405 def.offset = ir_value_code_addr(global);
3406 vec_push(code_defs, def);
3407 if (global->vtype == TYPE_VECTOR)
3408 gen_vector_defs(def, global->name);
3409 else if (global->vtype == TYPE_FIELD && global->fieldtype == TYPE_VECTOR)
3410 gen_vector_defs(def, global->name);
3417 switch (global->vtype)
3420 if (!strcmp(global->name, "end_sys_globals")) {
3421 /* TODO: remember this point... all the defs before this one
3422 * should be checksummed and added to progdefs.h when we generate it.
3425 else if (!strcmp(global->name, "end_sys_fields")) {
3426 /* TODO: same as above but for entity-fields rather than globsl
3430 irwarning(global->context, WARN_VOID_VARIABLES, "unrecognized variable of type void `%s`",
3432 /* I'd argue setting it to 0 is sufficient, but maybe some depend on knowing how far
3433 * the system fields actually go? Though the engine knows this anyway...
3434 * Maybe this could be an -foption
3435 * fteqcc creates data for end_sys_* - of size 1, so let's do the same
3437 ir_value_code_setaddr(global, vec_size(code_globals));
3438 vec_push(code_globals, 0);
3440 if (pushdef) vec_push(code_defs, def);
3443 if (pushdef) vec_push(code_defs, def);
3444 return gen_global_pointer(global);
3447 vec_push(code_defs, def);
3448 if (global->fieldtype == TYPE_VECTOR)
3449 gen_vector_defs(def, global->name);
3451 return gen_global_field(global);
3456 ir_value_code_setaddr(global, vec_size(code_globals));
3457 if (global->hasvalue) {
3458 iptr = (int32_t*)&global->constval.ivec[0];
3459 vec_push(code_globals, *iptr);
3461 vec_push(code_globals, 0);
3463 if (!islocal && global->cvq != CV_CONST)
3464 def.type |= DEF_SAVEGLOBAL;
3465 if (pushdef) vec_push(code_defs, def);
3467 return global->code.globaladdr >= 0;
3471 ir_value_code_setaddr(global, vec_size(code_globals));
3472 if (global->hasvalue) {
3473 vec_push(code_globals, code_genstring(global->constval.vstring));
3475 vec_push(code_globals, 0);
3477 if (!islocal && global->cvq != CV_CONST)
3478 def.type |= DEF_SAVEGLOBAL;
3479 if (pushdef) vec_push(code_defs, def);
3480 return global->code.globaladdr >= 0;
3485 ir_value_code_setaddr(global, vec_size(code_globals));
3486 if (global->hasvalue) {
3487 iptr = (int32_t*)&global->constval.ivec[0];
3488 vec_push(code_globals, iptr[0]);
3489 if (global->code.globaladdr < 0)
3491 for (d = 1; d < type_sizeof_[global->vtype]; ++d) {
3492 vec_push(code_globals, iptr[d]);
3495 vec_push(code_globals, 0);
3496 if (global->code.globaladdr < 0)
3498 for (d = 1; d < type_sizeof_[global->vtype]; ++d) {
3499 vec_push(code_globals, 0);
3502 if (!islocal && global->cvq != CV_CONST)
3503 def.type |= DEF_SAVEGLOBAL;
3506 vec_push(code_defs, def);
3507 def.type &= ~DEF_SAVEGLOBAL;
3508 gen_vector_defs(def, global->name);
3510 return global->code.globaladdr >= 0;
3513 ir_value_code_setaddr(global, vec_size(code_globals));
3514 if (!global->hasvalue) {
3515 vec_push(code_globals, 0);
3516 if (global->code.globaladdr < 0)
3519 vec_push(code_globals, vec_size(code_functions));
3520 if (!gen_global_function(self, global))
3523 if (!islocal && global->cvq != CV_CONST)
3524 def.type |= DEF_SAVEGLOBAL;
3525 if (pushdef) vec_push(code_defs, def);
3528 /* assume biggest type */
3529 ir_value_code_setaddr(global, vec_size(code_globals));
3530 vec_push(code_globals, 0);
3531 for (i = 1; i < type_sizeof_[TYPE_VARIANT]; ++i)
3532 vec_push(code_globals, 0);
3535 /* refuse to create 'void' type or any other fancy business. */
3536 irerror(global->context, "Invalid type for global variable `%s`: %s",
3537 global->name, type_name[global->vtype]);
3542 static void ir_builder_prepare_field(ir_value *field)
3544 field->code.fieldaddr = code_alloc_field(type_sizeof_[field->fieldtype]);
3547 static bool ir_builder_gen_field(ir_builder *self, ir_value *field)
3549 prog_section_def def;
3550 prog_section_field fld;
3554 def.type = (uint16_t)field->vtype;
3555 def.offset = (uint16_t)vec_size(code_globals);
3557 /* create a global named the same as the field */
3558 if (opts.standard == COMPILER_GMQCC) {
3559 /* in our standard, the global gets a dot prefix */
3560 size_t len = strlen(field->name);
3563 /* we really don't want to have to allocate this, and 1024
3564 * bytes is more than enough for a variable/field name
3566 if (len+2 >= sizeof(name)) {
3567 irerror(field->context, "invalid field name size: %u", (unsigned int)len);
3572 memcpy(name+1, field->name, len); /* no strncpy - we used strlen above */
3575 def.name = code_genstring(name);
3576 fld.name = def.name + 1; /* we reuse that string table entry */
3578 /* in plain QC, there cannot be a global with the same name,
3579 * and so we also name the global the same.
3580 * FIXME: fteqcc should create a global as well
3581 * check if it actually uses the same name. Probably does
3583 def.name = code_genstring(field->name);
3584 fld.name = def.name;
3587 field->code.name = def.name;
3589 vec_push(code_defs, def);
3591 fld.type = field->fieldtype;
3593 if (fld.type == TYPE_VOID) {
3594 irerror(field->context, "field is missing a type: %s - don't know its size", field->name);
3598 fld.offset = field->code.fieldaddr;
3600 vec_push(code_fields, fld);
3602 ir_value_code_setaddr(field, vec_size(code_globals));
3603 vec_push(code_globals, fld.offset);
3604 if (fld.type == TYPE_VECTOR) {
3605 vec_push(code_globals, fld.offset+1);
3606 vec_push(code_globals, fld.offset+2);
3609 if (field->fieldtype == TYPE_VECTOR) {
3610 gen_vector_defs(def, field->name);
3611 gen_vector_fields(fld, field->name);
3614 return field->code.globaladdr >= 0;
3617 bool ir_builder_generate(ir_builder *self, const char *filename)
3619 prog_section_statement stmt;
3621 char *lnofile = NULL;
3625 for (i = 0; i < vec_size(self->fields); ++i)
3627 ir_builder_prepare_field(self->fields[i]);
3630 for (i = 0; i < vec_size(self->globals); ++i)
3632 if (!ir_builder_gen_global(self, self->globals[i], false)) {
3635 if (self->globals[i]->vtype == TYPE_FUNCTION) {
3636 ir_function *func = self->globals[i]->constval.vfunc;
3637 if (func && self->max_locals < func->allocated_locals &&
3638 !(func->flags & IR_FLAG_MASK_NO_OVERLAP))
3640 self->max_locals = func->allocated_locals;
3642 if (func && self->max_globaltemps < func->globaltemps)
3643 self->max_globaltemps = func->globaltemps;
3647 for (i = 0; i < vec_size(self->fields); ++i)
3649 if (!ir_builder_gen_field(self, self->fields[i])) {
3655 ir_value_code_setaddr(self->nil, vec_size(code_globals));
3656 vec_push(code_globals, 0);
3657 vec_push(code_globals, 0);
3658 vec_push(code_globals, 0);
3660 /* generate global temps */
3661 self->first_common_globaltemp = vec_size(code_globals);
3662 for (i = 0; i < self->max_globaltemps; ++i) {
3663 vec_push(code_globals, 0);
3665 /* generate common locals */
3666 self->first_common_local = vec_size(code_globals);
3667 for (i = 0; i < self->max_locals; ++i) {
3668 vec_push(code_globals, 0);
3671 /* generate function code */
3672 for (i = 0; i < vec_size(self->globals); ++i)
3674 if (self->globals[i]->vtype == TYPE_FUNCTION) {
3675 if (!gen_global_function_code(self, self->globals[i])) {
3681 if (vec_size(code_globals) >= 65536) {
3682 irerror(vec_last(self->globals)->context, "This progs file would require more globals than the metadata can handle. Bailing out.");
3686 /* DP errors if the last instruction is not an INSTR_DONE. */
3687 if (vec_last(code_statements).opcode != INSTR_DONE)
3689 stmt.opcode = INSTR_DONE;
3693 code_push_statement(&stmt, vec_last(code_linenums));
3699 if (vec_size(code_statements) != vec_size(code_linenums)) {
3700 con_err("Linecounter wrong: %lu != %lu\n",
3701 (unsigned long)vec_size(code_statements),
3702 (unsigned long)vec_size(code_linenums));
3703 } else if (OPTS_FLAG(LNO)) {
3705 size_t filelen = strlen(filename);
3707 memcpy(vec_add(lnofile, filelen+1), filename, filelen+1);
3708 dot = strrchr(lnofile, '.');
3712 vec_shrinkto(lnofile, dot - lnofile);
3714 memcpy(vec_add(lnofile, 5), ".lno", 5);
3719 con_out("writing '%s' and '%s'...\n", filename, lnofile);
3721 con_out("writing '%s'\n", filename);
3723 if (!code_write(filename, lnofile)) {
3731 /***********************************************************************
3732 *IR DEBUG Dump functions...
3735 #define IND_BUFSZ 1024
3738 # define strncat(dst, src, sz) strncat_s(dst, sz, src, _TRUNCATE)
3741 const char *qc_opname(int op)
3743 if (op < 0) return "<INVALID>";
3744 if (op < (int)( sizeof(asm_instr) / sizeof(asm_instr[0]) ))
3745 return asm_instr[op].m;
3747 case VINSTR_PHI: return "PHI";
3748 case VINSTR_JUMP: return "JUMP";
3749 case VINSTR_COND: return "COND";
3750 default: return "<UNK>";
3754 void ir_builder_dump(ir_builder *b, int (*oprintf)(const char*, ...))
3757 char indent[IND_BUFSZ];
3761 oprintf("module %s\n", b->name);
3762 for (i = 0; i < vec_size(b->globals); ++i)
3765 if (b->globals[i]->hasvalue)
3766 oprintf("%s = ", b->globals[i]->name);
3767 ir_value_dump(b->globals[i], oprintf);
3770 for (i = 0; i < vec_size(b->functions); ++i)
3771 ir_function_dump(b->functions[i], indent, oprintf);
3772 oprintf("endmodule %s\n", b->name);
3775 void ir_function_dump(ir_function *f, char *ind,
3776 int (*oprintf)(const char*, ...))
3779 if (f->builtin != 0) {
3780 oprintf("%sfunction %s = builtin %i\n", ind, f->name, -f->builtin);
3783 oprintf("%sfunction %s\n", ind, f->name);
3784 strncat(ind, "\t", IND_BUFSZ);
3785 if (vec_size(f->locals))
3787 oprintf("%s%i locals:\n", ind, (int)vec_size(f->locals));
3788 for (i = 0; i < vec_size(f->locals); ++i) {
3789 oprintf("%s\t", ind);
3790 ir_value_dump(f->locals[i], oprintf);
3794 oprintf("%sliferanges:\n", ind);
3795 for (i = 0; i < vec_size(f->locals); ++i) {
3796 const char *attr = "";
3798 ir_value *v = f->locals[i];
3799 if (v->unique_life && v->locked)
3800 attr = "unique,locked ";
3801 else if (v->unique_life)
3805 oprintf("%s\t%s: %s %s@%i ", ind, v->name, type_name[v->vtype], attr, (int)v->code.local);
3806 for (l = 0; l < vec_size(v->life); ++l) {
3807 oprintf("[%i,%i] ", v->life[l].start, v->life[l].end);
3810 for (m = 0; m < 3; ++m) {
3811 ir_value *vm = v->members[m];
3814 if (vm->unique_life && vm->locked)
3815 attr = "unique,locked ";
3816 else if (vm->unique_life)
3818 else if (vm->locked)
3820 oprintf("%s\t%s: %s@%i ", ind, vm->name, attr, (int)vm->code.local);
3821 for (l = 0; l < vec_size(vm->life); ++l) {
3822 oprintf("[%i,%i] ", vm->life[l].start, vm->life[l].end);
3827 for (i = 0; i < vec_size(f->values); ++i) {
3828 const char *attr = "";
3830 ir_value *v = f->values[i];
3831 if (v->unique_life && v->locked)
3832 attr = "unique,locked ";
3833 else if (v->unique_life)
3837 oprintf("%s\t%s: %s %s@%i ", ind, v->name, type_name[v->vtype], attr, (int)v->code.local);
3838 for (l = 0; l < vec_size(v->life); ++l) {
3839 oprintf("[%i,%i] ", v->life[l].start, v->life[l].end);
3842 for (m = 0; m < 3; ++m) {
3843 ir_value *vm = v->members[m];
3846 if (vm->unique_life && vm->locked)
3847 attr = "unique,locked ";
3848 else if (vm->unique_life)
3850 else if (vm->locked)
3852 oprintf("%s\t%s: %s@%i ", ind, vm->name, attr, (int)vm->code.local);
3853 for (l = 0; l < vec_size(vm->life); ++l) {
3854 oprintf("[%i,%i] ", vm->life[l].start, vm->life[l].end);
3859 if (vec_size(f->blocks))
3861 oprintf("%slife passes (check): %i\n", ind, (int)f->run_id);
3862 for (i = 0; i < vec_size(f->blocks); ++i) {
3863 if (f->blocks[i]->run_id != f->run_id) {
3864 oprintf("%slife pass check fail! %i != %i\n", ind, (int)f->blocks[i]->run_id, (int)f->run_id);
3866 ir_block_dump(f->blocks[i], ind, oprintf);
3870 ind[strlen(ind)-1] = 0;
3871 oprintf("%sendfunction %s\n", ind, f->name);
3874 void ir_block_dump(ir_block* b, char *ind,
3875 int (*oprintf)(const char*, ...))
3878 oprintf("%s:%s\n", ind, b->label);
3879 strncat(ind, "\t", IND_BUFSZ);
3881 for (i = 0; i < vec_size(b->instr); ++i)
3882 ir_instr_dump(b->instr[i], ind, oprintf);
3883 ind[strlen(ind)-1] = 0;
3886 void dump_phi(ir_instr *in, int (*oprintf)(const char*, ...))
3889 oprintf("%s <- phi ", in->_ops[0]->name);
3890 for (i = 0; i < vec_size(in->phi); ++i)
3892 oprintf("([%s] : %s) ", in->phi[i].from->label,
3893 in->phi[i].value->name);
3898 void ir_instr_dump(ir_instr *in, char *ind,
3899 int (*oprintf)(const char*, ...))
3902 const char *comma = NULL;
3904 oprintf("%s (%i) ", ind, (int)in->eid);
3906 if (in->opcode == VINSTR_PHI) {
3907 dump_phi(in, oprintf);
3911 strncat(ind, "\t", IND_BUFSZ);
3913 if (in->_ops[0] && (in->_ops[1] || in->_ops[2])) {
3914 ir_value_dump(in->_ops[0], oprintf);
3915 if (in->_ops[1] || in->_ops[2])
3918 if (in->opcode == INSTR_CALL0 || in->opcode == VINSTR_NRCALL) {
3919 oprintf("CALL%i\t", vec_size(in->params));
3921 oprintf("%s\t", qc_opname(in->opcode));
3923 if (in->_ops[0] && !(in->_ops[1] || in->_ops[2])) {
3924 ir_value_dump(in->_ops[0], oprintf);
3929 for (i = 1; i != 3; ++i) {
3933 ir_value_dump(in->_ops[i], oprintf);
3941 oprintf("[%s]", in->bops[0]->label);
3945 oprintf("%s[%s]", comma, in->bops[1]->label);
3946 if (vec_size(in->params)) {
3947 oprintf("\tparams: ");
3948 for (i = 0; i != vec_size(in->params); ++i) {
3949 oprintf("%s, ", in->params[i]->name);
3953 ind[strlen(ind)-1] = 0;
3956 void ir_value_dump_string(const char *str, int (*oprintf)(const char*, ...))
3959 for (; *str; ++str) {
3961 case '\n': oprintf("\\n"); break;
3962 case '\r': oprintf("\\r"); break;
3963 case '\t': oprintf("\\t"); break;
3964 case '\v': oprintf("\\v"); break;
3965 case '\f': oprintf("\\f"); break;
3966 case '\b': oprintf("\\b"); break;
3967 case '\a': oprintf("\\a"); break;
3968 case '\\': oprintf("\\\\"); break;
3969 case '"': oprintf("\\\""); break;
3970 default: oprintf("%c", *str); break;
3976 void ir_value_dump(ir_value* v, int (*oprintf)(const char*, ...))
3985 oprintf("fn:%s", v->name);
3988 oprintf("%g", v->constval.vfloat);
3991 oprintf("'%g %g %g'",
3994 v->constval.vvec.z);
3997 oprintf("(entity)");
4000 ir_value_dump_string(v->constval.vstring, oprintf);
4004 oprintf("%i", v->constval.vint);
4009 v->constval.vpointer->name);
4013 oprintf("%s", v->name);
4017 void ir_value_dump_life(const ir_value *self, int (*oprintf)(const char*,...))
4020 oprintf("Life of %12s:", self->name);
4021 for (i = 0; i < vec_size(self->life); ++i)
4023 oprintf(" + [%i, %i]\n", self->life[i].start, self->life[i].end);