2 * Copyright (C) 2012, 2013
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is furnished to do
10 * so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 /***********************************************************************
29 * Type sizes used at multiple points in the IR codegen
32 const char *type_name[TYPE_COUNT] = {
51 size_t type_sizeof_[TYPE_COUNT] = {
58 1, /* TYPE_FUNCTION */
69 uint16_t type_store_instr[TYPE_COUNT] = {
70 INSTR_STORE_F, /* should use I when having integer support */
77 INSTR_STORE_ENT, /* should use I */
79 INSTR_STORE_I, /* integer type */
84 INSTR_STORE_V, /* variant, should never be accessed */
86 VINSTR_END, /* struct */
87 VINSTR_END, /* union */
88 VINSTR_END, /* array */
90 VINSTR_END, /* noexpr */
93 uint16_t field_store_instr[TYPE_COUNT] = {
103 INSTR_STORE_FLD, /* integer type */
108 INSTR_STORE_V, /* variant, should never be accessed */
110 VINSTR_END, /* struct */
111 VINSTR_END, /* union */
112 VINSTR_END, /* array */
113 VINSTR_END, /* nil */
114 VINSTR_END, /* noexpr */
117 uint16_t type_storep_instr[TYPE_COUNT] = {
118 INSTR_STOREP_F, /* should use I when having integer support */
125 INSTR_STOREP_ENT, /* should use I */
127 INSTR_STOREP_ENT, /* integer type */
132 INSTR_STOREP_V, /* variant, should never be accessed */
134 VINSTR_END, /* struct */
135 VINSTR_END, /* union */
136 VINSTR_END, /* array */
137 VINSTR_END, /* nil */
138 VINSTR_END, /* noexpr */
141 uint16_t type_eq_instr[TYPE_COUNT] = {
142 INSTR_EQ_F, /* should use I when having integer support */
147 INSTR_EQ_E, /* FLD has no comparison */
149 INSTR_EQ_E, /* should use I */
156 INSTR_EQ_V, /* variant, should never be accessed */
158 VINSTR_END, /* struct */
159 VINSTR_END, /* union */
160 VINSTR_END, /* array */
161 VINSTR_END, /* nil */
162 VINSTR_END, /* noexpr */
165 uint16_t type_ne_instr[TYPE_COUNT] = {
166 INSTR_NE_F, /* should use I when having integer support */
171 INSTR_NE_E, /* FLD has no comparison */
173 INSTR_NE_E, /* should use I */
180 INSTR_NE_V, /* variant, should never be accessed */
182 VINSTR_END, /* struct */
183 VINSTR_END, /* union */
184 VINSTR_END, /* array */
185 VINSTR_END, /* nil */
186 VINSTR_END, /* noexpr */
189 uint16_t type_not_instr[TYPE_COUNT] = {
190 INSTR_NOT_F, /* should use I when having integer support */
197 INSTR_NOT_ENT, /* should use I */
199 INSTR_NOT_I, /* integer type */
204 INSTR_NOT_V, /* variant, should never be accessed */
206 VINSTR_END, /* struct */
207 VINSTR_END, /* union */
208 VINSTR_END, /* array */
209 VINSTR_END, /* nil */
210 VINSTR_END, /* noexpr */
214 static void ir_gen_extparam (ir_builder *ir);
216 /* error functions */
218 static void irerror(lex_ctx ctx, const char *msg, ...)
222 con_cvprintmsg((void*)&ctx, LVL_ERROR, "internal error", msg, ap);
226 static bool irwarning(lex_ctx ctx, int warntype, const char *fmt, ...)
231 r = vcompile_warning(ctx, warntype, fmt, ap);
236 /***********************************************************************
237 * Vector utility functions
240 bool GMQCC_WARN vec_ir_value_find(ir_value **vec, const ir_value *what, size_t *idx)
243 size_t len = vec_size(vec);
244 for (i = 0; i < len; ++i) {
245 if (vec[i] == what) {
253 bool GMQCC_WARN vec_ir_block_find(ir_block **vec, ir_block *what, size_t *idx)
256 size_t len = vec_size(vec);
257 for (i = 0; i < len; ++i) {
258 if (vec[i] == what) {
266 bool GMQCC_WARN vec_ir_instr_find(ir_instr **vec, ir_instr *what, size_t *idx)
269 size_t len = vec_size(vec);
270 for (i = 0; i < len; ++i) {
271 if (vec[i] == what) {
279 /***********************************************************************
283 static void ir_block_delete_quick(ir_block* self);
284 static void ir_instr_delete_quick(ir_instr *self);
285 static void ir_function_delete_quick(ir_function *self);
287 ir_builder* ir_builder_new(const char *modulename)
291 self = (ir_builder*)mem_a(sizeof(*self));
295 self->functions = NULL;
296 self->globals = NULL;
298 self->filenames = NULL;
299 self->filestrings = NULL;
300 self->htglobals = util_htnew(IR_HT_SIZE);
301 self->htfields = util_htnew(IR_HT_SIZE);
302 self->htfunctions = util_htnew(IR_HT_SIZE);
304 self->extparams = NULL;
305 self->extparam_protos = NULL;
307 self->first_common_globaltemp = 0;
308 self->max_globaltemps = 0;
309 self->first_common_local = 0;
310 self->max_locals = 0;
311 self->max_used_params = 0;
313 self->str_immediate = 0;
315 if (!ir_builder_set_name(self, modulename)) {
320 self->nil = ir_value_var("nil", store_global, TYPE_NIL);
321 self->nil->cvq = CV_CONST;
322 self->nil->untracked = true;
324 self->reserved_va_count = NULL;
329 void ir_builder_delete(ir_builder* self)
332 util_htdel(self->htglobals);
333 util_htdel(self->htfields);
334 util_htdel(self->htfunctions);
335 mem_d((void*)self->name);
336 for (i = 0; i != vec_size(self->functions); ++i) {
337 ir_function_delete_quick(self->functions[i]);
339 vec_free(self->functions);
340 for (i = 0; i != vec_size(self->extparams); ++i) {
341 ir_value_delete(self->extparams[i]);
343 vec_free(self->extparams);
344 for (i = 0; i != vec_size(self->globals); ++i) {
345 ir_value_delete(self->globals[i]);
347 vec_free(self->globals);
348 for (i = 0; i != vec_size(self->fields); ++i) {
349 ir_value_delete(self->fields[i]);
351 ir_value_delete(self->nil);
352 vec_free(self->fields);
353 vec_free(self->filenames);
354 vec_free(self->filestrings);
358 bool ir_builder_set_name(ir_builder *self, const char *name)
361 mem_d((void*)self->name);
362 self->name = util_strdup(name);
366 ir_function* ir_builder_get_function(ir_builder *self, const char *name)
368 return (ir_function*)util_htget(self->htfunctions, name);
371 ir_function* ir_builder_create_function(ir_builder *self, const char *name, int outtype)
373 ir_function *fn = ir_builder_get_function(self, name);
378 fn = ir_function_new(self, outtype);
379 if (!ir_function_set_name(fn, name))
381 ir_function_delete(fn);
384 vec_push(self->functions, fn);
385 util_htset(self->htfunctions, name, fn);
387 fn->value = ir_builder_create_global(self, fn->name, TYPE_FUNCTION);
389 ir_function_delete(fn);
393 fn->value->hasvalue = true;
394 fn->value->outtype = outtype;
395 fn->value->constval.vfunc = fn;
396 fn->value->context = fn->context;
401 ir_value* ir_builder_get_global(ir_builder *self, const char *name)
403 return (ir_value*)util_htget(self->htglobals, name);
406 ir_value* ir_builder_create_global(ir_builder *self, const char *name, int vtype)
410 if (name && name[0] != '#')
412 ve = ir_builder_get_global(self, name);
418 ve = ir_value_var(name, store_global, vtype);
419 vec_push(self->globals, ve);
420 util_htset(self->htglobals, name, ve);
424 ir_value* ir_builder_get_va_count(ir_builder *self)
426 if (!self->reserved_va_count) {
427 self->reserved_va_count = ir_builder_create_global(self, "reserved:va_count", TYPE_FLOAT);
428 self->reserved_va_count->untracked = true;
430 return self->reserved_va_count;
433 ir_value* ir_builder_get_field(ir_builder *self, const char *name)
435 return (ir_value*)util_htget(self->htfields, name);
439 ir_value* ir_builder_create_field(ir_builder *self, const char *name, int vtype)
441 ir_value *ve = ir_builder_get_field(self, name);
446 ve = ir_value_var(name, store_global, TYPE_FIELD);
447 ve->fieldtype = vtype;
448 vec_push(self->fields, ve);
449 util_htset(self->htfields, name, ve);
453 /***********************************************************************
457 bool ir_function_naive_phi(ir_function*);
458 void ir_function_enumerate(ir_function*);
459 bool ir_function_calculate_liferanges(ir_function*);
460 bool ir_function_allocate_locals(ir_function*);
462 ir_function* ir_function_new(ir_builder* owner, int outtype)
465 self = (ir_function*)mem_a(sizeof(*self));
470 memset(self, 0, sizeof(*self));
473 if (!ir_function_set_name(self, "<@unnamed>")) {
480 self->context.file = "<@no context>";
481 self->context.line = 0;
482 self->outtype = outtype;
491 self->max_varargs = 0;
493 self->code_function_def = -1;
494 self->allocated_locals = 0;
495 self->globaltemps = 0;
501 bool ir_function_set_name(ir_function *self, const char *name)
504 mem_d((void*)self->name);
505 self->name = util_strdup(name);
509 static void ir_function_delete_quick(ir_function *self)
512 mem_d((void*)self->name);
514 for (i = 0; i != vec_size(self->blocks); ++i)
515 ir_block_delete_quick(self->blocks[i]);
516 vec_free(self->blocks);
518 vec_free(self->params);
520 for (i = 0; i != vec_size(self->values); ++i)
521 ir_value_delete(self->values[i]);
522 vec_free(self->values);
524 for (i = 0; i != vec_size(self->locals); ++i)
525 ir_value_delete(self->locals[i]);
526 vec_free(self->locals);
528 /* self->value is deleted by the builder */
533 void ir_function_delete(ir_function *self)
536 mem_d((void*)self->name);
538 for (i = 0; i != vec_size(self->blocks); ++i)
539 ir_block_delete(self->blocks[i]);
540 vec_free(self->blocks);
542 vec_free(self->params);
544 for (i = 0; i != vec_size(self->values); ++i)
545 ir_value_delete(self->values[i]);
546 vec_free(self->values);
548 for (i = 0; i != vec_size(self->locals); ++i)
549 ir_value_delete(self->locals[i]);
550 vec_free(self->locals);
552 /* self->value is deleted by the builder */
557 void ir_function_collect_value(ir_function *self, ir_value *v)
559 vec_push(self->values, v);
562 ir_block* ir_function_create_block(lex_ctx ctx, ir_function *self, const char *label)
564 ir_block* bn = ir_block_new(self, label);
566 vec_push(self->blocks, bn);
570 static bool instr_is_operation(uint16_t op)
572 return ( (op >= INSTR_MUL_F && op <= INSTR_GT) ||
573 (op >= INSTR_LOAD_F && op <= INSTR_LOAD_FNC) ||
574 (op == INSTR_ADDRESS) ||
575 (op >= INSTR_NOT_F && op <= INSTR_NOT_FNC) ||
576 (op >= INSTR_AND && op <= INSTR_BITOR) ||
577 (op >= INSTR_CALL0 && op <= INSTR_CALL8) );
580 bool ir_function_pass_peephole(ir_function *self)
584 for (b = 0; b < vec_size(self->blocks); ++b) {
586 ir_block *block = self->blocks[b];
588 for (i = 0; i < vec_size(block->instr); ++i) {
590 inst = block->instr[i];
593 (inst->opcode >= INSTR_STORE_F &&
594 inst->opcode <= INSTR_STORE_FNC))
602 oper = block->instr[i-1];
603 if (!instr_is_operation(oper->opcode))
606 if (OPTS_FLAG(LEGACY_VECTOR_MATHS)) {
607 if (oper->opcode == INSTR_MUL_VF && oper->_ops[2]->memberof == oper->_ops[1])
609 if (oper->opcode == INSTR_MUL_FV && oper->_ops[1]->memberof == oper->_ops[2])
613 value = oper->_ops[0];
615 /* only do it for SSA values */
616 if (value->store != store_value)
619 /* don't optimize out the temp if it's used later again */
620 if (vec_size(value->reads) != 1)
623 /* The very next store must use this value */
624 if (value->reads[0] != store)
627 /* And of course the store must _read_ from it, so it's in
629 if (store->_ops[1] != value)
632 ++opts_optimizationcount[OPTIM_PEEPHOLE];
633 (void)!ir_instr_op(oper, 0, store->_ops[0], true);
635 vec_remove(block->instr, i, 1);
636 ir_instr_delete(store);
638 else if (inst->opcode == VINSTR_COND)
640 /* COND on a value resulting from a NOT could
641 * remove the NOT and swap its operands
648 value = inst->_ops[0];
650 if (value->store != store_value ||
651 vec_size(value->reads) != 1 ||
652 value->reads[0] != inst)
657 inot = value->writes[0];
658 if (inot->_ops[0] != value ||
659 inot->opcode < INSTR_NOT_F ||
660 inot->opcode > INSTR_NOT_FNC ||
661 inot->opcode == INSTR_NOT_V || /* can't do these */
662 inot->opcode == INSTR_NOT_S)
668 ++opts_optimizationcount[OPTIM_PEEPHOLE];
670 (void)!ir_instr_op(inst, 0, inot->_ops[1], false);
673 for (inotid = 0; inotid < vec_size(tmp->instr); ++inotid) {
674 if (tmp->instr[inotid] == inot)
677 if (inotid >= vec_size(tmp->instr)) {
678 compile_error(inst->context, "sanity-check failed: failed to find instruction to optimize out");
681 vec_remove(tmp->instr, inotid, 1);
682 ir_instr_delete(inot);
683 /* swap ontrue/onfalse */
685 inst->bops[0] = inst->bops[1];
696 bool ir_function_pass_tailrecursion(ir_function *self)
700 for (b = 0; b < vec_size(self->blocks); ++b) {
702 ir_instr *ret, *call, *store = NULL;
703 ir_block *block = self->blocks[b];
705 if (!block->final || vec_size(block->instr) < 2)
708 ret = block->instr[vec_size(block->instr)-1];
709 if (ret->opcode != INSTR_DONE && ret->opcode != INSTR_RETURN)
712 call = block->instr[vec_size(block->instr)-2];
713 if (call->opcode >= INSTR_STORE_F && call->opcode <= INSTR_STORE_FNC) {
714 /* account for the unoptimized
716 * STORE %return, %tmp
720 if (vec_size(block->instr) < 3)
724 call = block->instr[vec_size(block->instr)-3];
727 if (call->opcode < INSTR_CALL0 || call->opcode > INSTR_CALL8)
731 /* optimize out the STORE */
733 ret->_ops[0] == store->_ops[0] &&
734 store->_ops[1] == call->_ops[0])
736 ++opts_optimizationcount[OPTIM_PEEPHOLE];
737 call->_ops[0] = store->_ops[0];
738 vec_remove(block->instr, vec_size(block->instr) - 2, 1);
739 ir_instr_delete(store);
748 funcval = call->_ops[1];
751 if (funcval->vtype != TYPE_FUNCTION || funcval->constval.vfunc != self)
754 /* now we have a CALL and a RET, check if it's a tailcall */
755 if (ret->_ops[0] && call->_ops[0] != ret->_ops[0])
758 ++opts_optimizationcount[OPTIM_TAIL_RECURSION];
759 vec_shrinkby(block->instr, 2);
761 block->final = false; /* open it back up */
763 /* emite parameter-stores */
764 for (p = 0; p < vec_size(call->params); ++p) {
765 /* assert(call->params_count <= self->locals_count); */
766 if (!ir_block_create_store(block, call->context, self->locals[p], call->params[p])) {
767 irerror(call->context, "failed to create tailcall store instruction for parameter %i", (int)p);
771 if (!ir_block_create_jump(block, call->context, self->blocks[0])) {
772 irerror(call->context, "failed to create tailcall jump");
776 ir_instr_delete(call);
777 ir_instr_delete(ret);
783 bool ir_function_optimize(ir_function *self)
788 if (OPTS_OPTIMIZATION(OPTIM_PEEPHOLE)) {
789 if (!ir_function_pass_peephole(self)) {
790 irerror(self->context, "generic optimization pass broke something in `%s`", self->name);
795 if (OPTS_OPTIMIZATION(OPTIM_TAIL_RECURSION)) {
796 if (!ir_function_pass_tailrecursion(self)) {
797 irerror(self->context, "tail-recursion optimization pass broke something in `%s`", self->name);
802 if (!ir_function_naive_phi(self)) {
803 irerror(self->context, "internal error: ir_function_naive_phi failed");
809 bool ir_function_finalize(ir_function *self)
816 for (i = 0; i < vec_size(self->locals); ++i) {
817 ir_value *v = self->locals[i];
818 if (v->vtype == TYPE_VECTOR ||
819 (v->vtype == TYPE_FIELD && v->outtype == TYPE_VECTOR))
821 ir_value_vector_member(v, 0);
822 ir_value_vector_member(v, 1);
823 ir_value_vector_member(v, 2);
826 for (i = 0; i < vec_size(self->values); ++i) {
827 ir_value *v = self->values[i];
828 if (v->vtype == TYPE_VECTOR ||
829 (v->vtype == TYPE_FIELD && v->outtype == TYPE_VECTOR))
831 ir_value_vector_member(v, 0);
832 ir_value_vector_member(v, 1);
833 ir_value_vector_member(v, 2);
837 ir_function_enumerate(self);
839 if (!ir_function_calculate_liferanges(self))
841 if (!ir_function_allocate_locals(self))
846 ir_value* ir_function_create_local(ir_function *self, const char *name, int vtype, bool param)
851 vec_size(self->locals) &&
852 self->locals[vec_size(self->locals)-1]->store != store_param) {
853 irerror(self->context, "cannot add parameters after adding locals");
857 ve = ir_value_var(name, (param ? store_param : store_local), vtype);
860 vec_push(self->locals, ve);
864 /***********************************************************************
868 ir_block* ir_block_new(ir_function* owner, const char *name)
871 self = (ir_block*)mem_a(sizeof(*self));
875 memset(self, 0, sizeof(*self));
878 if (name && !ir_block_set_label(self, name)) {
883 self->context.file = "<@no context>";
884 self->context.line = 0;
888 self->entries = NULL;
892 self->is_return = false;
896 self->generated = false;
901 static void ir_block_delete_quick(ir_block* self)
904 if (self->label) mem_d(self->label);
905 for (i = 0; i != vec_size(self->instr); ++i)
906 ir_instr_delete_quick(self->instr[i]);
907 vec_free(self->instr);
908 vec_free(self->entries);
909 vec_free(self->exits);
910 vec_free(self->living);
914 void ir_block_delete(ir_block* self)
917 if (self->label) mem_d(self->label);
918 for (i = 0; i != vec_size(self->instr); ++i)
919 ir_instr_delete(self->instr[i]);
920 vec_free(self->instr);
921 vec_free(self->entries);
922 vec_free(self->exits);
923 vec_free(self->living);
927 bool ir_block_set_label(ir_block *self, const char *name)
930 mem_d((void*)self->label);
931 self->label = util_strdup(name);
932 return !!self->label;
935 /***********************************************************************
939 ir_instr* ir_instr_new(lex_ctx ctx, ir_block* owner, int op)
942 self = (ir_instr*)mem_a(sizeof(*self));
949 self->_ops[0] = NULL;
950 self->_ops[1] = NULL;
951 self->_ops[2] = NULL;
952 self->bops[0] = NULL;
953 self->bops[1] = NULL;
964 static void ir_instr_delete_quick(ir_instr *self)
967 vec_free(self->params);
971 void ir_instr_delete(ir_instr *self)
974 /* The following calls can only delete from
975 * vectors, we still want to delete this instruction
976 * so ignore the return value. Since with the warn_unused_result attribute
977 * gcc doesn't care about an explicit: (void)foo(); to ignore the result,
978 * I have to improvise here and use if(foo());
980 for (i = 0; i < vec_size(self->phi); ++i) {
982 if (self->phi[i].value->untracked)
984 if (vec_ir_instr_find(self->phi[i].value->writes, self, &idx))
985 vec_remove(self->phi[i].value->writes, idx, 1);
986 if (vec_ir_instr_find(self->phi[i].value->reads, self, &idx))
987 vec_remove(self->phi[i].value->reads, idx, 1);
990 for (i = 0; i < vec_size(self->params); ++i) {
992 if (self->params[i]->untracked)
994 if (vec_ir_instr_find(self->params[i]->writes, self, &idx))
995 vec_remove(self->params[i]->writes, idx, 1);
996 if (vec_ir_instr_find(self->params[i]->reads, self, &idx))
997 vec_remove(self->params[i]->reads, idx, 1);
999 vec_free(self->params);
1000 (void)!ir_instr_op(self, 0, NULL, false);
1001 (void)!ir_instr_op(self, 1, NULL, false);
1002 (void)!ir_instr_op(self, 2, NULL, false);
1006 bool ir_instr_op(ir_instr *self, int op, ir_value *v, bool writing)
1008 ir_value *old = self->_ops[op];
1009 if (old && !old->untracked) {
1011 if (writing && vec_ir_instr_find(old->writes, self, &idx))
1012 vec_remove(old->writes, idx, 1);
1013 else if (vec_ir_instr_find(old->reads, self, &idx))
1014 vec_remove(old->reads, idx, 1);
1016 if (v && !v->untracked) {
1018 vec_push(v->writes, self);
1020 vec_push(v->reads, self);
1026 /***********************************************************************
1030 void ir_value_code_setaddr(ir_value *self, int32_t gaddr)
1032 self->code.globaladdr = gaddr;
1033 if (self->members[0]) self->members[0]->code.globaladdr = gaddr;
1034 if (self->members[1]) self->members[1]->code.globaladdr = gaddr;
1035 if (self->members[2]) self->members[2]->code.globaladdr = gaddr;
1038 int32_t ir_value_code_addr(const ir_value *self)
1040 if (self->store == store_return)
1041 return OFS_RETURN + self->code.addroffset;
1042 return self->code.globaladdr + self->code.addroffset;
1045 ir_value* ir_value_var(const char *name, int storetype, int vtype)
1048 self = (ir_value*)mem_a(sizeof(*self));
1049 self->vtype = vtype;
1050 self->fieldtype = TYPE_VOID;
1051 self->outtype = TYPE_VOID;
1052 self->store = storetype;
1056 self->writes = NULL;
1057 self->untracked = false;
1059 self->cvq = CV_NONE;
1060 self->hasvalue = false;
1061 self->context.file = "<@no context>";
1062 self->context.line = 0;
1064 if (name && !ir_value_set_name(self, name)) {
1065 irerror(self->context, "out of memory");
1070 memset(&self->constval, 0, sizeof(self->constval));
1071 memset(&self->code, 0, sizeof(self->code));
1073 self->members[0] = NULL;
1074 self->members[1] = NULL;
1075 self->members[2] = NULL;
1076 self->memberof = NULL;
1078 self->unique_life = false;
1079 self->locked = false;
1080 self->callparam = false;
1086 ir_value* ir_value_vector_member(ir_value *self, unsigned int member)
1094 if (self->members[member])
1095 return self->members[member];
1098 len = strlen(self->name);
1099 name = (char*)mem_a(len + 3);
1100 memcpy(name, self->name, len);
1102 name[len+1] = 'x' + member;
1108 if (self->vtype == TYPE_VECTOR)
1110 m = ir_value_var(name, self->store, TYPE_FLOAT);
1115 m->context = self->context;
1117 self->members[member] = m;
1118 m->code.addroffset = member;
1120 else if (self->vtype == TYPE_FIELD)
1122 if (self->fieldtype != TYPE_VECTOR)
1124 m = ir_value_var(name, self->store, TYPE_FIELD);
1129 m->fieldtype = TYPE_FLOAT;
1130 m->context = self->context;
1132 self->members[member] = m;
1133 m->code.addroffset = member;
1137 irerror(self->context, "invalid member access on %s", self->name);
1145 static GMQCC_INLINE size_t ir_value_sizeof(const ir_value *self)
1147 if (self->vtype == TYPE_FIELD && self->fieldtype == TYPE_VECTOR)
1148 return type_sizeof_[TYPE_VECTOR];
1149 return type_sizeof_[self->vtype];
1152 ir_value* ir_value_out(ir_function *owner, const char *name, int storetype, int vtype)
1154 ir_value *v = ir_value_var(name, storetype, vtype);
1157 ir_function_collect_value(owner, v);
1161 void ir_value_delete(ir_value* self)
1165 mem_d((void*)self->name);
1168 if (self->vtype == TYPE_STRING)
1169 mem_d((void*)self->constval.vstring);
1171 for (i = 0; i < 3; ++i) {
1172 if (self->members[i])
1173 ir_value_delete(self->members[i]);
1175 vec_free(self->reads);
1176 vec_free(self->writes);
1177 vec_free(self->life);
1181 bool ir_value_set_name(ir_value *self, const char *name)
1184 mem_d((void*)self->name);
1185 self->name = util_strdup(name);
1186 return !!self->name;
1189 bool ir_value_set_float(ir_value *self, float f)
1191 if (self->vtype != TYPE_FLOAT)
1193 self->constval.vfloat = f;
1194 self->hasvalue = true;
1198 bool ir_value_set_func(ir_value *self, int f)
1200 if (self->vtype != TYPE_FUNCTION)
1202 self->constval.vint = f;
1203 self->hasvalue = true;
1207 bool ir_value_set_vector(ir_value *self, vector v)
1209 if (self->vtype != TYPE_VECTOR)
1211 self->constval.vvec = v;
1212 self->hasvalue = true;
1216 bool ir_value_set_field(ir_value *self, ir_value *fld)
1218 if (self->vtype != TYPE_FIELD)
1220 self->constval.vpointer = fld;
1221 self->hasvalue = true;
1225 bool ir_value_set_string(ir_value *self, const char *str)
1227 if (self->vtype != TYPE_STRING)
1229 self->constval.vstring = util_strdupe(str);
1230 self->hasvalue = true;
1235 bool ir_value_set_int(ir_value *self, int i)
1237 if (self->vtype != TYPE_INTEGER)
1239 self->constval.vint = i;
1240 self->hasvalue = true;
1245 bool ir_value_lives(ir_value *self, size_t at)
1248 for (i = 0; i < vec_size(self->life); ++i)
1250 ir_life_entry_t *life = &self->life[i];
1251 if (life->start <= at && at <= life->end)
1253 if (life->start > at) /* since it's ordered */
1259 bool ir_value_life_insert(ir_value *self, size_t idx, ir_life_entry_t e)
1262 vec_push(self->life, e);
1263 for (k = vec_size(self->life)-1; k > idx; --k)
1264 self->life[k] = self->life[k-1];
1265 self->life[idx] = e;
1269 bool ir_value_life_merge(ir_value *self, size_t s)
1272 const size_t vs = vec_size(self->life);
1273 ir_life_entry_t *life = NULL;
1274 ir_life_entry_t *before = NULL;
1275 ir_life_entry_t new_entry;
1277 /* Find the first range >= s */
1278 for (i = 0; i < vs; ++i)
1281 life = &self->life[i];
1282 if (life->start > s)
1285 /* nothing found? append */
1288 if (life && life->end+1 == s)
1290 /* previous life range can be merged in */
1294 if (life && life->end >= s)
1296 e.start = e.end = s;
1297 vec_push(self->life, e);
1303 if (before->end + 1 == s &&
1304 life->start - 1 == s)
1307 before->end = life->end;
1308 vec_remove(self->life, i, 1);
1311 if (before->end + 1 == s)
1317 /* already contained */
1318 if (before->end >= s)
1322 if (life->start - 1 == s)
1327 /* insert a new entry */
1328 new_entry.start = new_entry.end = s;
1329 return ir_value_life_insert(self, i, new_entry);
1332 bool ir_value_life_merge_into(ir_value *self, const ir_value *other)
1336 if (!vec_size(other->life))
1339 if (!vec_size(self->life)) {
1340 size_t count = vec_size(other->life);
1341 ir_life_entry_t *life = vec_add(self->life, count);
1342 memcpy(life, other->life, count * sizeof(*life));
1347 for (i = 0; i < vec_size(other->life); ++i)
1349 const ir_life_entry_t *life = &other->life[i];
1352 ir_life_entry_t *entry = &self->life[myi];
1354 if (life->end+1 < entry->start)
1356 /* adding an interval before entry */
1357 if (!ir_value_life_insert(self, myi, *life))
1363 if (life->start < entry->start &&
1364 life->end+1 >= entry->start)
1366 /* starts earlier and overlaps */
1367 entry->start = life->start;
1370 if (life->end > entry->end &&
1371 life->start <= entry->end+1)
1373 /* ends later and overlaps */
1374 entry->end = life->end;
1377 /* see if our change combines it with the next ranges */
1378 while (myi+1 < vec_size(self->life) &&
1379 entry->end+1 >= self->life[1+myi].start)
1381 /* overlaps with (myi+1) */
1382 if (entry->end < self->life[1+myi].end)
1383 entry->end = self->life[1+myi].end;
1384 vec_remove(self->life, myi+1, 1);
1385 entry = &self->life[myi];
1388 /* see if we're after the entry */
1389 if (life->start > entry->end)
1392 /* append if we're at the end */
1393 if (myi >= vec_size(self->life)) {
1394 vec_push(self->life, *life);
1397 /* otherweise check the next range */
1406 bool ir_values_overlap(const ir_value *a, const ir_value *b)
1408 /* For any life entry in A see if it overlaps with
1409 * any life entry in B.
1410 * Note that the life entries are orderes, so we can make a
1411 * more efficient algorithm there than naively translating the
1415 ir_life_entry_t *la, *lb, *enda, *endb;
1417 /* first of all, if either has no life range, they cannot clash */
1418 if (!vec_size(a->life) || !vec_size(b->life))
1423 enda = la + vec_size(a->life);
1424 endb = lb + vec_size(b->life);
1427 /* check if the entries overlap, for that,
1428 * both must start before the other one ends.
1430 if (la->start < lb->end &&
1431 lb->start < la->end)
1436 /* entries are ordered
1437 * one entry is earlier than the other
1438 * that earlier entry will be moved forward
1440 if (la->start < lb->start)
1442 /* order: A B, move A forward
1443 * check if we hit the end with A
1448 else /* if (lb->start < la->start) actually <= */
1450 /* order: B A, move B forward
1451 * check if we hit the end with B
1460 /***********************************************************************
1464 static bool ir_check_unreachable(ir_block *self)
1466 /* The IR should never have to deal with unreachable code */
1467 if (!self->final/* || OPTS_FLAG(ALLOW_UNREACHABLE_CODE)*/)
1469 irerror(self->context, "unreachable statement (%s)", self->label);
1473 bool ir_block_create_store_op(ir_block *self, lex_ctx ctx, int op, ir_value *target, ir_value *what)
1476 if (!ir_check_unreachable(self))
1479 if (target->store == store_value &&
1480 (op < INSTR_STOREP_F || op > INSTR_STOREP_FNC))
1482 irerror(self->context, "cannot store to an SSA value");
1483 irerror(self->context, "trying to store: %s <- %s", target->name, what->name);
1484 irerror(self->context, "instruction: %s", asm_instr[op].m);
1488 in = ir_instr_new(ctx, self, op);
1492 if (!ir_instr_op(in, 0, target, (op < INSTR_STOREP_F || op > INSTR_STOREP_FNC)) ||
1493 !ir_instr_op(in, 1, what, false))
1495 ir_instr_delete(in);
1498 vec_push(self->instr, in);
1502 bool ir_block_create_store(ir_block *self, lex_ctx ctx, ir_value *target, ir_value *what)
1506 if (target->vtype == TYPE_VARIANT)
1507 vtype = what->vtype;
1509 vtype = target->vtype;
1512 if (vtype == TYPE_FLOAT && what->vtype == TYPE_INTEGER)
1513 op = INSTR_CONV_ITOF;
1514 else if (vtype == TYPE_INTEGER && what->vtype == TYPE_FLOAT)
1515 op = INSTR_CONV_FTOI;
1517 op = type_store_instr[vtype];
1519 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS)) {
1520 if (op == INSTR_STORE_FLD && what->fieldtype == TYPE_VECTOR)
1524 return ir_block_create_store_op(self, ctx, op, target, what);
1527 bool ir_block_create_storep(ir_block *self, lex_ctx ctx, ir_value *target, ir_value *what)
1532 if (target->vtype != TYPE_POINTER)
1535 /* storing using pointer - target is a pointer, type must be
1536 * inferred from source
1538 vtype = what->vtype;
1540 op = type_storep_instr[vtype];
1541 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS)) {
1542 if (op == INSTR_STOREP_FLD && what->fieldtype == TYPE_VECTOR)
1543 op = INSTR_STOREP_V;
1546 return ir_block_create_store_op(self, ctx, op, target, what);
1549 bool ir_block_create_return(ir_block *self, lex_ctx ctx, ir_value *v)
1552 if (!ir_check_unreachable(self))
1555 self->is_return = true;
1556 in = ir_instr_new(ctx, self, INSTR_RETURN);
1560 if (v && !ir_instr_op(in, 0, v, false)) {
1561 ir_instr_delete(in);
1565 vec_push(self->instr, in);
1569 bool ir_block_create_if(ir_block *self, lex_ctx ctx, ir_value *v,
1570 ir_block *ontrue, ir_block *onfalse)
1573 if (!ir_check_unreachable(self))
1576 /*in = ir_instr_new(ctx, self, (v->vtype == TYPE_STRING ? INSTR_IF_S : INSTR_IF_F));*/
1577 in = ir_instr_new(ctx, self, VINSTR_COND);
1581 if (!ir_instr_op(in, 0, v, false)) {
1582 ir_instr_delete(in);
1586 in->bops[0] = ontrue;
1587 in->bops[1] = onfalse;
1589 vec_push(self->instr, in);
1591 vec_push(self->exits, ontrue);
1592 vec_push(self->exits, onfalse);
1593 vec_push(ontrue->entries, self);
1594 vec_push(onfalse->entries, self);
1598 bool ir_block_create_jump(ir_block *self, lex_ctx ctx, ir_block *to)
1601 if (!ir_check_unreachable(self))
1604 in = ir_instr_new(ctx, self, VINSTR_JUMP);
1609 vec_push(self->instr, in);
1611 vec_push(self->exits, to);
1612 vec_push(to->entries, self);
1616 bool ir_block_create_goto(ir_block *self, lex_ctx ctx, ir_block *to)
1618 self->owner->flags |= IR_FLAG_HAS_GOTO;
1619 return ir_block_create_jump(self, ctx, to);
1622 ir_instr* ir_block_create_phi(ir_block *self, lex_ctx ctx, const char *label, int ot)
1626 if (!ir_check_unreachable(self))
1628 in = ir_instr_new(ctx, self, VINSTR_PHI);
1631 out = ir_value_out(self->owner, label, store_value, ot);
1633 ir_instr_delete(in);
1636 if (!ir_instr_op(in, 0, out, true)) {
1637 ir_instr_delete(in);
1638 ir_value_delete(out);
1641 vec_push(self->instr, in);
1645 ir_value* ir_phi_value(ir_instr *self)
1647 return self->_ops[0];
1650 void ir_phi_add(ir_instr* self, ir_block *b, ir_value *v)
1654 if (!vec_ir_block_find(self->owner->entries, b, NULL)) {
1655 /* Must not be possible to cause this, otherwise the AST
1656 * is doing something wrong.
1658 irerror(self->context, "Invalid entry block for PHI");
1665 vec_push(v->reads, self);
1666 vec_push(self->phi, pe);
1669 /* call related code */
1670 ir_instr* ir_block_create_call(ir_block *self, lex_ctx ctx, const char *label, ir_value *func, bool noreturn)
1674 if (!ir_check_unreachable(self))
1676 in = ir_instr_new(ctx, self, (noreturn ? VINSTR_NRCALL : INSTR_CALL0));
1681 self->is_return = true;
1683 out = ir_value_out(self->owner, label, (func->outtype == TYPE_VOID) ? store_return : store_value, func->outtype);
1685 ir_instr_delete(in);
1688 if (!ir_instr_op(in, 0, out, true) ||
1689 !ir_instr_op(in, 1, func, false))
1691 ir_instr_delete(in);
1692 ir_value_delete(out);
1695 vec_push(self->instr, in);
1698 if (!ir_block_create_return(self, ctx, NULL)) {
1699 compile_error(ctx, "internal error: failed to generate dummy-return instruction");
1700 ir_instr_delete(in);
1708 ir_value* ir_call_value(ir_instr *self)
1710 return self->_ops[0];
1713 void ir_call_param(ir_instr* self, ir_value *v)
1715 size_t *maxparams, param;
1716 vec_push(self->params, v);
1718 vec_push(v->reads, self);
1720 param = vec_size(self->params);
1721 maxparams = &self->owner->owner->owner->max_used_params;
1722 if (param > *maxparams)
1726 /* binary op related code */
1728 ir_value* ir_block_create_binop(ir_block *self, lex_ctx ctx,
1729 const char *label, int opcode,
1730 ir_value *left, ir_value *right)
1752 case INSTR_SUB_S: /* -- offset of string as float */
1757 case INSTR_BITOR_IF:
1758 case INSTR_BITOR_FI:
1759 case INSTR_BITAND_FI:
1760 case INSTR_BITAND_IF:
1775 case INSTR_BITAND_I:
1778 case INSTR_RSHIFT_I:
1779 case INSTR_LSHIFT_I:
1801 /* boolean operations result in floats */
1802 if (opcode >= INSTR_EQ_F && opcode <= INSTR_GT)
1804 else if (opcode >= INSTR_LE && opcode <= INSTR_GT)
1807 else if (opcode >= INSTR_LE_I && opcode <= INSTR_EQ_FI)
1812 if (ot == TYPE_VOID) {
1813 /* The AST or parser were supposed to check this! */
1817 return ir_block_create_general_instr(self, ctx, label, opcode, left, right, ot);
1820 ir_value* ir_block_create_unary(ir_block *self, lex_ctx ctx,
1821 const char *label, int opcode,
1824 int ot = TYPE_FLOAT;
1836 /* QC doesn't have other unary operations. We expect extensions to fill
1837 * the above list, otherwise we assume out-type = in-type, eg for an
1841 ot = operand->vtype;
1844 if (ot == TYPE_VOID) {
1845 /* The AST or parser were supposed to check this! */
1849 /* let's use the general instruction creator and pass NULL for OPB */
1850 return ir_block_create_general_instr(self, ctx, label, opcode, operand, NULL, ot);
1853 ir_value* ir_block_create_general_instr(ir_block *self, lex_ctx ctx, const char *label,
1854 int op, ir_value *a, ir_value *b, int outype)
1859 out = ir_value_out(self->owner, label, store_value, outype);
1863 instr = ir_instr_new(ctx, self, op);
1865 ir_value_delete(out);
1869 if (!ir_instr_op(instr, 0, out, true) ||
1870 !ir_instr_op(instr, 1, a, false) ||
1871 !ir_instr_op(instr, 2, b, false) )
1876 vec_push(self->instr, instr);
1880 ir_instr_delete(instr);
1881 ir_value_delete(out);
1885 ir_value* ir_block_create_fieldaddress(ir_block *self, lex_ctx ctx, const char *label, ir_value *ent, ir_value *field)
1889 /* Support for various pointer types todo if so desired */
1890 if (ent->vtype != TYPE_ENTITY)
1893 if (field->vtype != TYPE_FIELD)
1896 v = ir_block_create_general_instr(self, ctx, label, INSTR_ADDRESS, ent, field, TYPE_POINTER);
1897 v->fieldtype = field->fieldtype;
1901 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)
1904 if (ent->vtype != TYPE_ENTITY)
1907 /* at some point we could redirect for TYPE_POINTER... but that could lead to carelessness */
1908 if (field->vtype != TYPE_FIELD)
1913 case TYPE_FLOAT: op = INSTR_LOAD_F; break;
1914 case TYPE_VECTOR: op = INSTR_LOAD_V; break;
1915 case TYPE_STRING: op = INSTR_LOAD_S; break;
1916 case TYPE_FIELD: op = INSTR_LOAD_FLD; break;
1917 case TYPE_ENTITY: op = INSTR_LOAD_ENT; break;
1918 case TYPE_FUNCTION: op = INSTR_LOAD_FNC; break;
1920 case TYPE_POINTER: op = INSTR_LOAD_I; break;
1921 case TYPE_INTEGER: op = INSTR_LOAD_I; break;
1924 irerror(self->context, "invalid type for ir_block_create_load_from_ent: %s", type_name[outype]);
1928 return ir_block_create_general_instr(self, ctx, label, op, ent, field, outype);
1931 /* PHI resolving breaks the SSA, and must thus be the last
1932 * step before life-range calculation.
1935 static bool ir_block_naive_phi(ir_block *self);
1936 bool ir_function_naive_phi(ir_function *self)
1940 for (i = 0; i < vec_size(self->blocks); ++i)
1942 if (!ir_block_naive_phi(self->blocks[i]))
1948 static bool ir_block_naive_phi(ir_block *self)
1950 size_t i, p; /*, w;*/
1951 /* FIXME: optionally, create_phi can add the phis
1952 * to a list so we don't need to loop through blocks
1953 * - anyway: "don't optimize YET"
1955 for (i = 0; i < vec_size(self->instr); ++i)
1957 ir_instr *instr = self->instr[i];
1958 if (instr->opcode != VINSTR_PHI)
1961 vec_remove(self->instr, i, 1);
1962 --i; /* NOTE: i+1 below */
1964 for (p = 0; p < vec_size(instr->phi); ++p)
1966 ir_value *v = instr->phi[p].value;
1967 ir_block *b = instr->phi[p].from;
1969 if (v->store == store_value &&
1970 vec_size(v->reads) == 1 &&
1971 vec_size(v->writes) == 1)
1973 /* replace the value */
1974 if (!ir_instr_op(v->writes[0], 0, instr->_ops[0], true))
1979 /* force a move instruction */
1980 ir_instr *prevjump = vec_last(b->instr);
1983 instr->_ops[0]->store = store_global;
1984 if (!ir_block_create_store(b, instr->context, instr->_ops[0], v))
1986 instr->_ops[0]->store = store_value;
1987 vec_push(b->instr, prevjump);
1991 ir_instr_delete(instr);
1996 /***********************************************************************
1997 *IR Temp allocation code
1998 * Propagating value life ranges by walking through the function backwards
1999 * until no more changes are made.
2000 * In theory this should happen once more than once for every nested loop
2002 * Though this implementation might run an additional time for if nests.
2005 /* Enumerate instructions used by value's life-ranges
2007 static void ir_block_enumerate(ir_block *self, size_t *_eid)
2011 for (i = 0; i < vec_size(self->instr); ++i)
2013 self->instr[i]->eid = eid++;
2018 /* Enumerate blocks and instructions.
2019 * The block-enumeration is unordered!
2020 * We do not really use the block enumreation, however
2021 * the instruction enumeration is important for life-ranges.
2023 void ir_function_enumerate(ir_function *self)
2026 size_t instruction_id = 0;
2027 for (i = 0; i < vec_size(self->blocks); ++i)
2029 /* each block now gets an additional "entry" instruction id
2030 * we can use to avoid point-life issues
2032 self->blocks[i]->entry_id = instruction_id;
2035 self->blocks[i]->eid = i;
2036 ir_block_enumerate(self->blocks[i], &instruction_id);
2040 /* Local-value allocator
2041 * After finishing creating the liferange of all values used in a function
2042 * we can allocate their global-positions.
2043 * This is the counterpart to register-allocation in register machines.
2050 } function_allocator;
2052 static bool function_allocator_alloc(function_allocator *alloc, ir_value *var)
2055 size_t vsize = ir_value_sizeof(var);
2057 var->code.local = vec_size(alloc->locals);
2059 slot = ir_value_var("reg", store_global, var->vtype);
2063 if (!ir_value_life_merge_into(slot, var))
2066 vec_push(alloc->locals, slot);
2067 vec_push(alloc->sizes, vsize);
2068 vec_push(alloc->unique, var->unique_life);
2073 ir_value_delete(slot);
2077 static bool ir_function_allocator_assign(ir_function *self, function_allocator *alloc, ir_value *v)
2083 return function_allocator_alloc(alloc, v);
2085 for (a = 0; a < vec_size(alloc->locals); ++a)
2087 /* if it's reserved for a unique liferange: skip */
2088 if (alloc->unique[a])
2091 slot = alloc->locals[a];
2093 /* never resize parameters
2094 * will be required later when overlapping temps + locals
2096 if (a < vec_size(self->params) &&
2097 alloc->sizes[a] < ir_value_sizeof(v))
2102 if (ir_values_overlap(v, slot))
2105 if (!ir_value_life_merge_into(slot, v))
2108 /* adjust size for this slot */
2109 if (alloc->sizes[a] < ir_value_sizeof(v))
2110 alloc->sizes[a] = ir_value_sizeof(v);
2115 if (a >= vec_size(alloc->locals)) {
2116 if (!function_allocator_alloc(alloc, v))
2122 bool ir_function_allocate_locals(ir_function *self)
2127 bool opt_gt = OPTS_OPTIMIZATION(OPTIM_GLOBAL_TEMPS);
2131 function_allocator lockalloc, globalloc;
2133 if (!vec_size(self->locals) && !vec_size(self->values))
2136 globalloc.locals = NULL;
2137 globalloc.sizes = NULL;
2138 globalloc.positions = NULL;
2139 globalloc.unique = NULL;
2140 lockalloc.locals = NULL;
2141 lockalloc.sizes = NULL;
2142 lockalloc.positions = NULL;
2143 lockalloc.unique = NULL;
2145 for (i = 0; i < vec_size(self->locals); ++i)
2147 v = self->locals[i];
2148 if ((self->flags & IR_FLAG_MASK_NO_LOCAL_TEMPS) || !OPTS_OPTIMIZATION(OPTIM_LOCAL_TEMPS)) {
2150 v->unique_life = true;
2152 else if (i >= vec_size(self->params))
2155 v->locked = true; /* lock parameters locals */
2156 if (!function_allocator_alloc((v->locked || !opt_gt ? &lockalloc : &globalloc), v))
2159 for (; i < vec_size(self->locals); ++i)
2161 v = self->locals[i];
2162 if (!vec_size(v->life))
2164 if (!ir_function_allocator_assign(self, (v->locked || !opt_gt ? &lockalloc : &globalloc), v))
2168 /* Allocate a slot for any value that still exists */
2169 for (i = 0; i < vec_size(self->values); ++i)
2171 v = self->values[i];
2173 if (!vec_size(v->life))
2176 /* CALL optimization:
2177 * If the value is a parameter-temp: 1 write, 1 read from a CALL
2178 * and it's not "locked", write it to the OFS_PARM directly.
2180 if (OPTS_OPTIMIZATION(OPTIM_CALL_STORES) && !v->locked && !v->unique_life) {
2181 if (vec_size(v->reads) == 1 && vec_size(v->writes) == 1 &&
2182 (v->reads[0]->opcode == VINSTR_NRCALL ||
2183 (v->reads[0]->opcode >= INSTR_CALL0 && v->reads[0]->opcode <= INSTR_CALL8)
2188 ir_instr *call = v->reads[0];
2189 if (!vec_ir_value_find(call->params, v, ¶m)) {
2190 irerror(call->context, "internal error: unlocked parameter %s not found", v->name);
2193 ++opts_optimizationcount[OPTIM_CALL_STORES];
2194 v->callparam = true;
2196 ir_value_code_setaddr(v, OFS_PARM0 + 3*param);
2198 ir_value *ep = self->owner->extparam_protos[param-=8];
2199 ir_instr_op(v->writes[0], 0, ep, true);
2200 call->params[param+8] = ep;
2204 if (vec_size(v->writes) == 1 && v->writes[0]->opcode == INSTR_CALL0)
2206 v->store = store_return;
2207 if (v->members[0]) v->members[0]->store = store_return;
2208 if (v->members[1]) v->members[1]->store = store_return;
2209 if (v->members[2]) v->members[2]->store = store_return;
2210 ++opts_optimizationcount[OPTIM_CALL_STORES];
2215 if (!ir_function_allocator_assign(self, (v->locked || !opt_gt ? &lockalloc : &globalloc), v))
2219 if (!lockalloc.sizes && !globalloc.sizes) {
2222 vec_push(lockalloc.positions, 0);
2223 vec_push(globalloc.positions, 0);
2225 /* Adjust slot positions based on sizes */
2226 if (lockalloc.sizes) {
2227 pos = (vec_size(lockalloc.sizes) ? lockalloc.positions[0] : 0);
2228 for (i = 1; i < vec_size(lockalloc.sizes); ++i)
2230 pos = lockalloc.positions[i-1] + lockalloc.sizes[i-1];
2231 vec_push(lockalloc.positions, pos);
2233 self->allocated_locals = pos + vec_last(lockalloc.sizes);
2235 if (globalloc.sizes) {
2236 pos = (vec_size(globalloc.sizes) ? globalloc.positions[0] : 0);
2237 for (i = 1; i < vec_size(globalloc.sizes); ++i)
2239 pos = globalloc.positions[i-1] + globalloc.sizes[i-1];
2240 vec_push(globalloc.positions, pos);
2242 self->globaltemps = pos + vec_last(globalloc.sizes);
2245 /* Locals need to know their new position */
2246 for (i = 0; i < vec_size(self->locals); ++i) {
2247 v = self->locals[i];
2248 if (v->locked || !opt_gt)
2249 v->code.local = lockalloc.positions[v->code.local];
2251 v->code.local = globalloc.positions[v->code.local];
2253 /* Take over the actual slot positions on values */
2254 for (i = 0; i < vec_size(self->values); ++i) {
2255 v = self->values[i];
2256 if (v->locked || !opt_gt)
2257 v->code.local = lockalloc.positions[v->code.local];
2259 v->code.local = globalloc.positions[v->code.local];
2267 for (i = 0; i < vec_size(lockalloc.locals); ++i)
2268 ir_value_delete(lockalloc.locals[i]);
2269 for (i = 0; i < vec_size(globalloc.locals); ++i)
2270 ir_value_delete(globalloc.locals[i]);
2271 vec_free(globalloc.unique);
2272 vec_free(globalloc.locals);
2273 vec_free(globalloc.sizes);
2274 vec_free(globalloc.positions);
2275 vec_free(lockalloc.unique);
2276 vec_free(lockalloc.locals);
2277 vec_free(lockalloc.sizes);
2278 vec_free(lockalloc.positions);
2282 /* Get information about which operand
2283 * is read from, or written to.
2285 static void ir_op_read_write(int op, size_t *read, size_t *write)
2305 case INSTR_STOREP_F:
2306 case INSTR_STOREP_V:
2307 case INSTR_STOREP_S:
2308 case INSTR_STOREP_ENT:
2309 case INSTR_STOREP_FLD:
2310 case INSTR_STOREP_FNC:
2321 static bool ir_block_living_add_instr(ir_block *self, size_t eid)
2324 const size_t vs = vec_size(self->living);
2325 bool changed = false;
2326 for (i = 0; i != vs; ++i)
2328 if (ir_value_life_merge(self->living[i], eid))
2334 static bool ir_block_living_lock(ir_block *self)
2337 bool changed = false;
2338 for (i = 0; i != vec_size(self->living); ++i)
2340 if (!self->living[i]->locked) {
2341 self->living[i]->locked = true;
2348 static bool ir_block_life_propagate(ir_block *self, bool *changed)
2352 size_t i, o, p, mem, cnt;
2353 /* bitmasks which operands are read from or written to */
2360 vec_free(self->living);
2362 p = vec_size(self->exits);
2363 for (i = 0; i < p; ++i) {
2364 ir_block *prev = self->exits[i];
2365 cnt = vec_size(prev->living);
2366 for (o = 0; o < cnt; ++o) {
2367 if (!vec_ir_value_find(self->living, prev->living[o], NULL))
2368 vec_push(self->living, prev->living[o]);
2372 i = vec_size(self->instr);
2375 instr = self->instr[i];
2377 /* See which operands are read and write operands */
2378 ir_op_read_write(instr->opcode, &read, &write);
2380 /* Go through the 3 main operands
2381 * writes first, then reads
2383 for (o = 0; o < 3; ++o)
2385 if (!instr->_ops[o]) /* no such operand */
2388 value = instr->_ops[o];
2390 /* We only care about locals */
2391 /* we also calculate parameter liferanges so that locals
2392 * can take up parameter slots */
2393 if (value->store != store_value &&
2394 value->store != store_local &&
2395 value->store != store_param)
2398 /* write operands */
2399 /* When we write to a local, we consider it "dead" for the
2400 * remaining upper part of the function, since in SSA a value
2401 * can only be written once (== created)
2406 bool in_living = vec_ir_value_find(self->living, value, &idx);
2409 /* If the value isn't alive it hasn't been read before... */
2410 /* TODO: See if the warning can be emitted during parsing or AST processing
2411 * otherwise have warning printed here.
2412 * IF printing a warning here: include filecontext_t,
2413 * and make sure it's only printed once
2414 * since this function is run multiple times.
2416 /* con_err( "Value only written %s\n", value->name); */
2417 if (ir_value_life_merge(value, instr->eid))
2420 /* since 'living' won't contain it
2421 * anymore, merge the value, since
2424 if (ir_value_life_merge(value, instr->eid))
2427 vec_remove(self->living, idx, 1);
2429 /* Removing a vector removes all members */
2430 for (mem = 0; mem < 3; ++mem) {
2431 if (value->members[mem] && vec_ir_value_find(self->living, value->members[mem], &idx)) {
2432 if (ir_value_life_merge(value->members[mem], instr->eid))
2434 vec_remove(self->living, idx, 1);
2437 /* Removing the last member removes the vector */
2438 if (value->memberof) {
2439 value = value->memberof;
2440 for (mem = 0; mem < 3; ++mem) {
2441 if (value->members[mem] && vec_ir_value_find(self->living, value->members[mem], NULL))
2444 if (mem == 3 && vec_ir_value_find(self->living, value, &idx)) {
2445 if (ir_value_life_merge(value, instr->eid))
2447 vec_remove(self->living, idx, 1);
2453 if (instr->opcode == INSTR_MUL_VF)
2455 value = instr->_ops[2];
2456 if (value->store == store_value ||
2457 value->store == store_local ||
2458 value->store == store_param)
2460 /* the float source will get an additional lifetime */
2461 if (ir_value_life_merge(value, instr->eid+1))
2463 if (value->memberof && ir_value_life_merge(value->memberof, instr->eid+1))
2467 else if (instr->opcode == INSTR_MUL_FV || instr->opcode == INSTR_LOAD_V)
2469 value = instr->_ops[1];
2470 if (value->store == store_value ||
2471 value->store == store_local ||
2472 value->store == store_param)
2474 /* the float source will get an additional lifetime */
2475 if (ir_value_life_merge(value, instr->eid+1))
2477 if (value->memberof && ir_value_life_merge(value->memberof, instr->eid+1))
2482 for (o = 0; o < 3; ++o)
2484 if (!instr->_ops[o]) /* no such operand */
2487 value = instr->_ops[o];
2489 /* We only care about locals */
2490 /* we also calculate parameter liferanges so that locals
2491 * can take up parameter slots */
2492 if (value->store != store_value &&
2493 value->store != store_local &&
2494 value->store != store_param)
2500 if (!vec_ir_value_find(self->living, value, NULL))
2501 vec_push(self->living, value);
2502 /* reading adds the full vector */
2503 if (value->memberof && !vec_ir_value_find(self->living, value->memberof, NULL))
2504 vec_push(self->living, value->memberof);
2505 for (mem = 0; mem < 3; ++mem) {
2506 if (value->members[mem] && !vec_ir_value_find(self->living, value->members[mem], NULL))
2507 vec_push(self->living, value->members[mem]);
2511 /* PHI operands are always read operands */
2512 for (p = 0; p < vec_size(instr->phi); ++p)
2514 value = instr->phi[p].value;
2515 if (value->store != store_value &&
2516 value->store != store_local &&
2517 value->store != store_param)
2519 if (!vec_ir_value_find(self->living, value, NULL))
2520 vec_push(self->living, value);
2521 /* reading adds the full vector */
2522 if (value->memberof && !vec_ir_value_find(self->living, value->memberof, NULL))
2523 vec_push(self->living, value->memberof);
2524 for (mem = 0; mem < 3; ++mem) {
2525 if (value->members[mem] && !vec_ir_value_find(self->living, value->members[mem], NULL))
2526 vec_push(self->living, value->members[mem]);
2530 /* on a call, all these values must be "locked" */
2531 if (instr->opcode >= INSTR_CALL0 && instr->opcode <= INSTR_CALL8) {
2532 if (ir_block_living_lock(self))
2535 /* call params are read operands too */
2536 for (p = 0; p < vec_size(instr->params); ++p)
2538 value = instr->params[p];
2539 if (value->store != store_value &&
2540 value->store != store_local &&
2541 value->store != store_param)
2543 if (!vec_ir_value_find(self->living, value, NULL))
2544 vec_push(self->living, value);
2545 /* reading adds the full vector */
2546 if (value->memberof && !vec_ir_value_find(self->living, value->memberof, NULL))
2547 vec_push(self->living, value->memberof);
2548 for (mem = 0; mem < 3; ++mem) {
2549 if (value->members[mem] && !vec_ir_value_find(self->living, value->members[mem], NULL))
2550 vec_push(self->living, value->members[mem]);
2555 if (ir_block_living_add_instr(self, instr->eid))
2558 /* the "entry" instruction ID */
2559 if (ir_block_living_add_instr(self, self->entry_id))
2565 bool ir_function_calculate_liferanges(ir_function *self)
2570 /* parameters live at 0 */
2571 for (i = 0; i < vec_size(self->params); ++i)
2572 ir_value_life_merge(self->locals[i], 0);
2577 i = vec_size(self->blocks);
2579 ir_block_life_propagate(self->blocks[i], &changed);
2583 if (vec_size(self->blocks)) {
2584 ir_block *block = self->blocks[0];
2585 for (i = 0; i < vec_size(block->living); ++i) {
2586 ir_value *v = block->living[i];
2587 if (v->store != store_local)
2589 if (v->vtype == TYPE_VECTOR)
2591 self->flags |= IR_FLAG_HAS_UNINITIALIZED;
2592 /* find the instruction reading from it */
2593 for (s = 0; s < vec_size(v->reads); ++s) {
2594 if (v->reads[s]->eid == v->life[0].end)
2597 if (s < vec_size(v->reads)) {
2598 if (irwarning(v->context, WARN_USED_UNINITIALIZED,
2599 "variable `%s` may be used uninitialized in this function\n"
2602 v->reads[s]->context.file, v->reads[s]->context.line)
2610 ir_value *vec = v->memberof;
2611 for (s = 0; s < vec_size(vec->reads); ++s) {
2612 if (vec->reads[s]->eid == v->life[0].end)
2615 if (s < vec_size(vec->reads)) {
2616 if (irwarning(v->context, WARN_USED_UNINITIALIZED,
2617 "variable `%s` may be used uninitialized in this function\n"
2620 vec->reads[s]->context.file, vec->reads[s]->context.line)
2628 if (irwarning(v->context, WARN_USED_UNINITIALIZED,
2629 "variable `%s` may be used uninitialized in this function", v->name))
2638 /***********************************************************************
2641 * Since the IR has the convention of putting 'write' operands
2642 * at the beginning, we have to rotate the operands of instructions
2643 * properly in order to generate valid QCVM code.
2645 * Having destinations at a fixed position is more convenient. In QC
2646 * this is *mostly* OPC, but FTE adds at least 2 instructions which
2647 * read from from OPA, and store to OPB rather than OPC. Which is
2648 * partially the reason why the implementation of these instructions
2649 * in darkplaces has been delayed for so long.
2651 * Breaking conventions is annoying...
2653 static bool ir_builder_gen_global(ir_builder *self, ir_value *global, bool islocal);
2655 static bool gen_global_field(ir_value *global)
2657 if (global->hasvalue)
2659 ir_value *fld = global->constval.vpointer;
2661 irerror(global->context, "Invalid field constant with no field: %s", global->name);
2665 /* copy the field's value */
2666 ir_value_code_setaddr(global, vec_size(code_globals));
2667 vec_push(code_globals, fld->code.fieldaddr);
2668 if (global->fieldtype == TYPE_VECTOR) {
2669 vec_push(code_globals, fld->code.fieldaddr+1);
2670 vec_push(code_globals, fld->code.fieldaddr+2);
2675 ir_value_code_setaddr(global, vec_size(code_globals));
2676 vec_push(code_globals, 0);
2677 if (global->fieldtype == TYPE_VECTOR) {
2678 vec_push(code_globals, 0);
2679 vec_push(code_globals, 0);
2682 if (global->code.globaladdr < 0)
2687 static bool gen_global_pointer(ir_value *global)
2689 if (global->hasvalue)
2691 ir_value *target = global->constval.vpointer;
2693 irerror(global->context, "Invalid pointer constant: %s", global->name);
2694 /* NULL pointers are pointing to the NULL constant, which also
2695 * sits at address 0, but still has an ir_value for itself.
2700 /* Here, relocations ARE possible - in fteqcc-enhanced-qc:
2701 * void() foo; <- proto
2702 * void() *fooptr = &foo;
2703 * void() foo = { code }
2705 if (!target->code.globaladdr) {
2706 /* FIXME: Check for the constant nullptr ir_value!
2707 * because then code.globaladdr being 0 is valid.
2709 irerror(global->context, "FIXME: Relocation support");
2713 ir_value_code_setaddr(global, vec_size(code_globals));
2714 vec_push(code_globals, target->code.globaladdr);
2718 ir_value_code_setaddr(global, vec_size(code_globals));
2719 vec_push(code_globals, 0);
2721 if (global->code.globaladdr < 0)
2726 static bool gen_blocks_recursive(ir_function *func, ir_block *block)
2728 prog_section_statement stmt;
2736 block->generated = true;
2737 block->code_start = vec_size(code_statements);
2738 for (i = 0; i < vec_size(block->instr); ++i)
2740 instr = block->instr[i];
2742 if (instr->opcode == VINSTR_PHI) {
2743 irerror(block->context, "cannot generate virtual instruction (phi)");
2747 if (instr->opcode == VINSTR_JUMP) {
2748 target = instr->bops[0];
2749 /* for uncoditional jumps, if the target hasn't been generated
2750 * yet, we generate them right here.
2752 if (!target->generated)
2753 return gen_blocks_recursive(func, target);
2755 /* otherwise we generate a jump instruction */
2756 stmt.opcode = INSTR_GOTO;
2757 stmt.o1.s1 = (target->code_start) - vec_size(code_statements);
2760 if (stmt.o1.s1 != 1)
2761 code_push_statement(&stmt, instr->context.line);
2763 /* no further instructions can be in this block */
2767 if (instr->opcode == VINSTR_COND) {
2768 ontrue = instr->bops[0];
2769 onfalse = instr->bops[1];
2770 /* TODO: have the AST signal which block should
2771 * come first: eg. optimize IFs without ELSE...
2774 stmt.o1.u1 = ir_value_code_addr(instr->_ops[0]);
2778 if (ontrue->generated) {
2779 stmt.opcode = INSTR_IF;
2780 stmt.o2.s1 = (ontrue->code_start) - vec_size(code_statements);
2781 if (stmt.o2.s1 != 1)
2782 code_push_statement(&stmt, instr->context.line);
2784 if (onfalse->generated) {
2785 stmt.opcode = INSTR_IFNOT;
2786 stmt.o2.s1 = (onfalse->code_start) - vec_size(code_statements);
2787 if (stmt.o2.s1 != 1)
2788 code_push_statement(&stmt, instr->context.line);
2790 if (!ontrue->generated) {
2791 if (onfalse->generated)
2792 return gen_blocks_recursive(func, ontrue);
2794 if (!onfalse->generated) {
2795 if (ontrue->generated)
2796 return gen_blocks_recursive(func, onfalse);
2798 /* neither ontrue nor onfalse exist */
2799 stmt.opcode = INSTR_IFNOT;
2800 if (!instr->likely) {
2801 /* Honor the likelyhood hint */
2802 ir_block *tmp = onfalse;
2803 stmt.opcode = INSTR_IF;
2807 stidx = vec_size(code_statements);
2808 code_push_statement(&stmt, instr->context.line);
2809 /* on false we jump, so add ontrue-path */
2810 if (!gen_blocks_recursive(func, ontrue))
2812 /* fixup the jump address */
2813 code_statements[stidx].o2.s1 = vec_size(code_statements) - stidx;
2814 /* generate onfalse path */
2815 if (onfalse->generated) {
2816 /* fixup the jump address */
2817 code_statements[stidx].o2.s1 = (onfalse->code_start) - (stidx);
2818 if (stidx+2 == vec_size(code_statements) && code_statements[stidx].o2.s1 == 1) {
2819 code_statements[stidx] = code_statements[stidx+1];
2820 if (code_statements[stidx].o1.s1 < 0)
2821 code_statements[stidx].o1.s1++;
2822 code_pop_statement();
2824 stmt.opcode = vec_last(code_statements).opcode;
2825 if (stmt.opcode == INSTR_GOTO ||
2826 stmt.opcode == INSTR_IF ||
2827 stmt.opcode == INSTR_IFNOT ||
2828 stmt.opcode == INSTR_RETURN ||
2829 stmt.opcode == INSTR_DONE)
2831 /* no use jumping from here */
2834 /* may have been generated in the previous recursive call */
2835 stmt.opcode = INSTR_GOTO;
2836 stmt.o1.s1 = (onfalse->code_start) - vec_size(code_statements);
2839 if (stmt.o1.s1 != 1)
2840 code_push_statement(&stmt, instr->context.line);
2843 else if (stidx+2 == vec_size(code_statements) && code_statements[stidx].o2.s1 == 1) {
2844 code_statements[stidx] = code_statements[stidx+1];
2845 if (code_statements[stidx].o1.s1 < 0)
2846 code_statements[stidx].o1.s1++;
2847 code_pop_statement();
2849 /* if not, generate now */
2850 return gen_blocks_recursive(func, onfalse);
2853 if ( (instr->opcode >= INSTR_CALL0 && instr->opcode <= INSTR_CALL8)
2854 || instr->opcode == VINSTR_NRCALL)
2859 first = vec_size(instr->params);
2862 for (p = 0; p < first; ++p)
2864 ir_value *param = instr->params[p];
2865 if (param->callparam)
2868 stmt.opcode = INSTR_STORE_F;
2871 if (param->vtype == TYPE_FIELD)
2872 stmt.opcode = field_store_instr[param->fieldtype];
2873 else if (param->vtype == TYPE_NIL)
2874 stmt.opcode = INSTR_STORE_V;
2876 stmt.opcode = type_store_instr[param->vtype];
2877 stmt.o1.u1 = ir_value_code_addr(param);
2878 stmt.o2.u1 = OFS_PARM0 + 3 * p;
2879 code_push_statement(&stmt, instr->context.line);
2881 /* Now handle extparams */
2882 first = vec_size(instr->params);
2883 for (; p < first; ++p)
2885 ir_builder *ir = func->owner;
2886 ir_value *param = instr->params[p];
2887 ir_value *targetparam;
2889 if (param->callparam)
2892 if (p-8 >= vec_size(ir->extparams))
2893 ir_gen_extparam(ir);
2895 targetparam = ir->extparams[p-8];
2897 stmt.opcode = INSTR_STORE_F;
2900 if (param->vtype == TYPE_FIELD)
2901 stmt.opcode = field_store_instr[param->fieldtype];
2902 else if (param->vtype == TYPE_NIL)
2903 stmt.opcode = INSTR_STORE_V;
2905 stmt.opcode = type_store_instr[param->vtype];
2906 stmt.o1.u1 = ir_value_code_addr(param);
2907 stmt.o2.u1 = ir_value_code_addr(targetparam);
2908 code_push_statement(&stmt, instr->context.line);
2911 stmt.opcode = INSTR_CALL0 + vec_size(instr->params);
2912 if (stmt.opcode > INSTR_CALL8)
2913 stmt.opcode = INSTR_CALL8;
2914 stmt.o1.u1 = ir_value_code_addr(instr->_ops[1]);
2917 code_push_statement(&stmt, instr->context.line);
2919 retvalue = instr->_ops[0];
2920 if (retvalue && retvalue->store != store_return &&
2921 (retvalue->store == store_global || vec_size(retvalue->life)))
2923 /* not to be kept in OFS_RETURN */
2924 if (retvalue->vtype == TYPE_FIELD && OPTS_FLAG(ADJUST_VECTOR_FIELDS))
2925 stmt.opcode = field_store_instr[retvalue->fieldtype];
2927 stmt.opcode = type_store_instr[retvalue->vtype];
2928 stmt.o1.u1 = OFS_RETURN;
2929 stmt.o2.u1 = ir_value_code_addr(retvalue);
2931 code_push_statement(&stmt, instr->context.line);
2936 if (instr->opcode == INSTR_STATE) {
2937 irerror(block->context, "TODO: state instruction");
2941 stmt.opcode = instr->opcode;
2946 /* This is the general order of operands */
2948 stmt.o3.u1 = ir_value_code_addr(instr->_ops[0]);
2951 stmt.o1.u1 = ir_value_code_addr(instr->_ops[1]);
2954 stmt.o2.u1 = ir_value_code_addr(instr->_ops[2]);
2956 if (stmt.opcode == INSTR_RETURN || stmt.opcode == INSTR_DONE)
2958 stmt.o1.u1 = stmt.o3.u1;
2961 else if ((stmt.opcode >= INSTR_STORE_F &&
2962 stmt.opcode <= INSTR_STORE_FNC) ||
2963 (stmt.opcode >= INSTR_STOREP_F &&
2964 stmt.opcode <= INSTR_STOREP_FNC))
2966 /* 2-operand instructions with A -> B */
2967 stmt.o2.u1 = stmt.o3.u1;
2970 /* tiny optimization, don't output
2973 if (stmt.o2.u1 == stmt.o1.u1 &&
2974 OPTS_OPTIMIZATION(OPTIM_PEEPHOLE))
2976 ++opts_optimizationcount[OPTIM_PEEPHOLE];
2981 code_push_statement(&stmt, instr->context.line);
2986 static bool gen_function_code(ir_function *self)
2989 prog_section_statement stmt, *retst;
2991 /* Starting from entry point, we generate blocks "as they come"
2992 * for now. Dead blocks will not be translated obviously.
2994 if (!vec_size(self->blocks)) {
2995 irerror(self->context, "Function '%s' declared without body.", self->name);
2999 block = self->blocks[0];
3000 if (block->generated)
3003 if (!gen_blocks_recursive(self, block)) {
3004 irerror(self->context, "failed to generate blocks for '%s'", self->name);
3008 /* code_write and qcvm -disasm need to know that the function ends here */
3009 retst = &vec_last(code_statements);
3010 if (OPTS_OPTIMIZATION(OPTIM_VOID_RETURN) &&
3011 self->outtype == TYPE_VOID &&
3012 retst->opcode == INSTR_RETURN &&
3013 !retst->o1.u1 && !retst->o2.u1 && !retst->o3.u1)
3015 retst->opcode = INSTR_DONE;
3016 ++opts_optimizationcount[OPTIM_VOID_RETURN];
3018 stmt.opcode = INSTR_DONE;
3022 code_push_statement(&stmt, vec_last(code_linenums));
3027 static qcint ir_builder_filestring(ir_builder *ir, const char *filename)
3029 /* NOTE: filename pointers are copied, we never strdup them,
3030 * thus we can use pointer-comparison to find the string.
3035 for (i = 0; i < vec_size(ir->filenames); ++i) {
3036 if (ir->filenames[i] == filename)
3037 return ir->filestrings[i];
3040 str = code_genstring(filename);
3041 vec_push(ir->filenames, filename);
3042 vec_push(ir->filestrings, str);
3046 static bool gen_global_function(ir_builder *ir, ir_value *global)
3048 prog_section_function fun;
3053 if (!global->hasvalue || (!global->constval.vfunc))
3055 irerror(global->context, "Invalid state of function-global: not constant: %s", global->name);
3059 irfun = global->constval.vfunc;
3061 fun.name = global->code.name;
3062 fun.file = ir_builder_filestring(ir, global->context.file);
3063 fun.profile = 0; /* always 0 */
3064 fun.nargs = vec_size(irfun->params);
3068 for (i = 0;i < 8; ++i) {
3069 if ((int32_t)i >= fun.nargs)
3072 fun.argsize[i] = type_sizeof_[irfun->params[i]];
3076 fun.locals = irfun->allocated_locals;
3079 fun.entry = irfun->builtin+1;
3081 irfun->code_function_def = vec_size(code_functions);
3082 fun.entry = vec_size(code_statements);
3085 vec_push(code_functions, fun);
3089 static ir_value* ir_gen_extparam_proto(ir_builder *ir)
3094 snprintf(name, sizeof(name), "EXTPARM#%i", (int)(vec_size(ir->extparam_protos)));
3095 global = ir_value_var(name, store_global, TYPE_VECTOR);
3096 global->untracked = true;
3098 vec_push(ir->extparam_protos, global);
3102 static void ir_gen_extparam(ir_builder *ir)
3104 prog_section_def def;
3107 if (vec_size(ir->extparam_protos) < vec_size(ir->extparams)+1)
3108 global = ir_gen_extparam_proto(ir);
3110 global = ir->extparam_protos[vec_size(ir->extparams)];
3112 def.name = code_genstring(global->name);
3113 def.type = TYPE_VECTOR;
3114 def.offset = vec_size(code_globals);
3116 vec_push(code_defs, def);
3117 ir_value_code_setaddr(global, def.offset);
3118 vec_push(code_globals, 0);
3119 vec_push(code_globals, 0);
3120 vec_push(code_globals, 0);
3122 vec_push(ir->extparams, global);
3125 static bool gen_function_extparam_copy(ir_function *self)
3127 size_t i, ext, numparams;
3129 ir_builder *ir = self->owner;
3131 prog_section_statement stmt;
3133 numparams = vec_size(self->params);
3137 stmt.opcode = INSTR_STORE_F;
3139 for (i = 8; i < numparams; ++i) {
3141 if (ext >= vec_size(ir->extparams))
3142 ir_gen_extparam(ir);
3144 ep = ir->extparams[ext];
3146 stmt.opcode = type_store_instr[self->locals[i]->vtype];
3147 if (self->locals[i]->vtype == TYPE_FIELD &&
3148 self->locals[i]->fieldtype == TYPE_VECTOR)
3150 stmt.opcode = INSTR_STORE_V;
3152 stmt.o1.u1 = ir_value_code_addr(ep);
3153 stmt.o2.u1 = ir_value_code_addr(self->locals[i]);
3154 code_push_statement(&stmt, self->context.line);
3160 static bool gen_function_varargs_copy(ir_function *self)
3162 size_t i, ext, numparams, maxparams;
3164 ir_builder *ir = self->owner;
3166 prog_section_statement stmt;
3168 numparams = vec_size(self->params);
3172 stmt.opcode = INSTR_STORE_V;
3174 maxparams = numparams + self->max_varargs;
3175 for (i = numparams; i < maxparams; ++i) {
3177 stmt.o1.u1 = OFS_PARM0 + 3*i;
3178 stmt.o2.u1 = ir_value_code_addr(self->locals[i]);
3179 code_push_statement(&stmt, self->context.line);
3183 while (ext >= vec_size(ir->extparams))
3184 ir_gen_extparam(ir);
3186 ep = ir->extparams[ext];
3188 stmt.o1.u1 = ir_value_code_addr(ep);
3189 stmt.o2.u1 = ir_value_code_addr(self->locals[i]);
3190 code_push_statement(&stmt, self->context.line);
3196 static bool gen_function_locals(ir_builder *ir, ir_value *global)
3198 prog_section_function *def;
3201 uint32_t firstlocal, firstglobal;
3203 irfun = global->constval.vfunc;
3204 def = code_functions + irfun->code_function_def;
3206 if (OPTS_OPTION_BOOL(OPTION_G) ||
3207 !OPTS_OPTIMIZATION(OPTIM_OVERLAP_LOCALS) ||
3208 (irfun->flags & IR_FLAG_MASK_NO_OVERLAP))
3210 firstlocal = def->firstlocal = vec_size(code_globals);
3212 firstlocal = def->firstlocal = ir->first_common_local;
3213 ++opts_optimizationcount[OPTIM_OVERLAP_LOCALS];
3216 firstglobal = (OPTS_OPTIMIZATION(OPTIM_GLOBAL_TEMPS) ? ir->first_common_globaltemp : firstlocal);
3218 for (i = vec_size(code_globals); i < firstlocal + irfun->allocated_locals; ++i)
3219 vec_push(code_globals, 0);
3220 for (i = 0; i < vec_size(irfun->locals); ++i) {
3221 ir_value *v = irfun->locals[i];
3222 if (v->locked || !OPTS_OPTIMIZATION(OPTIM_GLOBAL_TEMPS)) {
3223 ir_value_code_setaddr(v, firstlocal + v->code.local);
3224 if (!ir_builder_gen_global(ir, irfun->locals[i], true)) {
3225 irerror(irfun->locals[i]->context, "failed to generate local %s", irfun->locals[i]->name);
3230 ir_value_code_setaddr(v, firstglobal + v->code.local);
3232 for (i = 0; i < vec_size(irfun->values); ++i)
3234 ir_value *v = irfun->values[i];
3238 ir_value_code_setaddr(v, firstlocal + v->code.local);
3240 ir_value_code_setaddr(v, firstglobal + v->code.local);
3245 static bool gen_global_function_code(ir_builder *ir, ir_value *global)
3247 prog_section_function *fundef;
3252 irfun = global->constval.vfunc;
3254 if (global->cvq == CV_NONE) {
3255 irwarning(global->context, WARN_IMPLICIT_FUNCTION_POINTER,
3256 "function `%s` has no body and in QC implicitly becomes a function-pointer", global->name);
3258 /* this was a function pointer, don't generate code for those */
3265 if (irfun->code_function_def < 0) {
3266 irerror(irfun->context, "`%s`: IR global wasn't generated, failed to access function-def", irfun->name);
3269 fundef = &code_functions[irfun->code_function_def];
3271 fundef->entry = vec_size(code_statements);
3272 if (!gen_function_locals(ir, global)) {
3273 irerror(irfun->context, "Failed to generate locals for function %s", irfun->name);
3276 if (!gen_function_extparam_copy(irfun)) {
3277 irerror(irfun->context, "Failed to generate extparam-copy code for function %s", irfun->name);
3280 if (irfun->max_varargs && !gen_function_varargs_copy(irfun)) {
3281 irerror(irfun->context, "Failed to generate vararg-copy code for function %s", irfun->name);
3284 if (!gen_function_code(irfun)) {
3285 irerror(irfun->context, "Failed to generate code for function %s", irfun->name);
3291 static void gen_vector_defs(prog_section_def def, const char *name)
3296 if (!name || name[0] == '#' || OPTS_FLAG(SINGLE_VECTOR_DEFS))
3299 def.type = TYPE_FLOAT;
3303 component = (char*)mem_a(len+3);
3304 memcpy(component, name, len);
3306 component[len-0] = 0;
3307 component[len-2] = '_';
3309 component[len-1] = 'x';
3311 for (i = 0; i < 3; ++i) {
3312 def.name = code_genstring(component);
3313 vec_push(code_defs, def);
3321 static void gen_vector_fields(prog_section_field fld, const char *name)
3326 if (!name || OPTS_FLAG(SINGLE_VECTOR_DEFS))
3329 fld.type = TYPE_FLOAT;
3333 component = (char*)mem_a(len+3);
3334 memcpy(component, name, len);
3336 component[len-0] = 0;
3337 component[len-2] = '_';
3339 component[len-1] = 'x';
3341 for (i = 0; i < 3; ++i) {
3342 fld.name = code_genstring(component);
3343 vec_push(code_fields, fld);
3351 static bool ir_builder_gen_global(ir_builder *self, ir_value *global, bool islocal)
3355 prog_section_def def;
3356 bool pushdef = opts.optimizeoff;
3358 def.type = global->vtype;
3359 def.offset = vec_size(code_globals);
3361 if (OPTS_OPTION_BOOL(OPTION_G) || !islocal)
3365 if (OPTS_OPTIMIZATION(OPTIM_STRIP_CONSTANT_NAMES) &&
3366 !(global->flags & IR_FLAG_INCLUDE_DEF) &&
3367 (global->name[0] == '#' || global->cvq == CV_CONST))
3372 if (pushdef && global->name) {
3373 if (global->name[0] == '#') {
3374 if (!self->str_immediate)
3375 self->str_immediate = code_genstring("IMMEDIATE");
3376 def.name = global->code.name = self->str_immediate;
3379 def.name = global->code.name = code_genstring(global->name);
3384 def.offset = ir_value_code_addr(global);
3385 vec_push(code_defs, def);
3386 if (global->vtype == TYPE_VECTOR)
3387 gen_vector_defs(def, global->name);
3388 else if (global->vtype == TYPE_FIELD && global->fieldtype == TYPE_VECTOR)
3389 gen_vector_defs(def, global->name);
3396 switch (global->vtype)
3399 if (!strcmp(global->name, "end_sys_globals")) {
3400 /* TODO: remember this point... all the defs before this one
3401 * should be checksummed and added to progdefs.h when we generate it.
3404 else if (!strcmp(global->name, "end_sys_fields")) {
3405 /* TODO: same as above but for entity-fields rather than globsl
3409 irwarning(global->context, WARN_VOID_VARIABLES, "unrecognized variable of type void `%s`",
3411 /* I'd argue setting it to 0 is sufficient, but maybe some depend on knowing how far
3412 * the system fields actually go? Though the engine knows this anyway...
3413 * Maybe this could be an -foption
3414 * fteqcc creates data for end_sys_* - of size 1, so let's do the same
3416 ir_value_code_setaddr(global, vec_size(code_globals));
3417 vec_push(code_globals, 0);
3419 if (pushdef) vec_push(code_defs, def);
3422 if (pushdef) vec_push(code_defs, def);
3423 return gen_global_pointer(global);
3426 vec_push(code_defs, def);
3427 if (global->fieldtype == TYPE_VECTOR)
3428 gen_vector_defs(def, global->name);
3430 return gen_global_field(global);
3435 ir_value_code_setaddr(global, vec_size(code_globals));
3436 if (global->hasvalue) {
3437 iptr = (int32_t*)&global->constval.ivec[0];
3438 vec_push(code_globals, *iptr);
3440 vec_push(code_globals, 0);
3442 if (!islocal && global->cvq != CV_CONST)
3443 def.type |= DEF_SAVEGLOBAL;
3444 if (pushdef) vec_push(code_defs, def);
3446 return global->code.globaladdr >= 0;
3450 ir_value_code_setaddr(global, vec_size(code_globals));
3451 if (global->hasvalue) {
3452 vec_push(code_globals, code_genstring(global->constval.vstring));
3454 vec_push(code_globals, 0);
3456 if (!islocal && global->cvq != CV_CONST)
3457 def.type |= DEF_SAVEGLOBAL;
3458 if (pushdef) vec_push(code_defs, def);
3459 return global->code.globaladdr >= 0;
3464 ir_value_code_setaddr(global, vec_size(code_globals));
3465 if (global->hasvalue) {
3466 iptr = (int32_t*)&global->constval.ivec[0];
3467 vec_push(code_globals, iptr[0]);
3468 if (global->code.globaladdr < 0)
3470 for (d = 1; d < type_sizeof_[global->vtype]; ++d) {
3471 vec_push(code_globals, iptr[d]);
3474 vec_push(code_globals, 0);
3475 if (global->code.globaladdr < 0)
3477 for (d = 1; d < type_sizeof_[global->vtype]; ++d) {
3478 vec_push(code_globals, 0);
3481 if (!islocal && global->cvq != CV_CONST)
3482 def.type |= DEF_SAVEGLOBAL;
3485 vec_push(code_defs, def);
3486 def.type &= ~DEF_SAVEGLOBAL;
3487 gen_vector_defs(def, global->name);
3489 return global->code.globaladdr >= 0;
3492 ir_value_code_setaddr(global, vec_size(code_globals));
3493 if (!global->hasvalue) {
3494 vec_push(code_globals, 0);
3495 if (global->code.globaladdr < 0)
3498 vec_push(code_globals, vec_size(code_functions));
3499 if (!gen_global_function(self, global))
3502 if (!islocal && global->cvq != CV_CONST)
3503 def.type |= DEF_SAVEGLOBAL;
3504 if (pushdef) vec_push(code_defs, def);
3507 /* assume biggest type */
3508 ir_value_code_setaddr(global, vec_size(code_globals));
3509 vec_push(code_globals, 0);
3510 for (i = 1; i < type_sizeof_[TYPE_VARIANT]; ++i)
3511 vec_push(code_globals, 0);
3514 /* refuse to create 'void' type or any other fancy business. */
3515 irerror(global->context, "Invalid type for global variable `%s`: %s",
3516 global->name, type_name[global->vtype]);
3521 static void ir_builder_prepare_field(ir_value *field)
3523 field->code.fieldaddr = code_alloc_field(type_sizeof_[field->fieldtype]);
3526 static bool ir_builder_gen_field(ir_builder *self, ir_value *field)
3528 prog_section_def def;
3529 prog_section_field fld;
3533 def.type = (uint16_t)field->vtype;
3534 def.offset = (uint16_t)vec_size(code_globals);
3536 /* create a global named the same as the field */
3537 if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_GMQCC) {
3538 /* in our standard, the global gets a dot prefix */
3539 size_t len = strlen(field->name);
3542 /* we really don't want to have to allocate this, and 1024
3543 * bytes is more than enough for a variable/field name
3545 if (len+2 >= sizeof(name)) {
3546 irerror(field->context, "invalid field name size: %u", (unsigned int)len);
3551 memcpy(name+1, field->name, len); /* no strncpy - we used strlen above */
3554 def.name = code_genstring(name);
3555 fld.name = def.name + 1; /* we reuse that string table entry */
3557 /* in plain QC, there cannot be a global with the same name,
3558 * and so we also name the global the same.
3559 * FIXME: fteqcc should create a global as well
3560 * check if it actually uses the same name. Probably does
3562 def.name = code_genstring(field->name);
3563 fld.name = def.name;
3566 field->code.name = def.name;
3568 vec_push(code_defs, def);
3570 fld.type = field->fieldtype;
3572 if (fld.type == TYPE_VOID) {
3573 irerror(field->context, "field is missing a type: %s - don't know its size", field->name);
3577 fld.offset = field->code.fieldaddr;
3579 vec_push(code_fields, fld);
3581 ir_value_code_setaddr(field, vec_size(code_globals));
3582 vec_push(code_globals, fld.offset);
3583 if (fld.type == TYPE_VECTOR) {
3584 vec_push(code_globals, fld.offset+1);
3585 vec_push(code_globals, fld.offset+2);
3588 if (field->fieldtype == TYPE_VECTOR) {
3589 gen_vector_defs(def, field->name);
3590 gen_vector_fields(fld, field->name);
3593 return field->code.globaladdr >= 0;
3596 bool ir_builder_prepare(ir_builder *self)
3598 size_t extparams = self->max_used_params;
3599 if (extparams > 8) {
3600 for (extparams -= 8; extparams; --extparams)
3601 ir_gen_extparam_proto(self);
3606 bool ir_builder_generate(ir_builder *self, const char *filename)
3608 prog_section_statement stmt;
3610 char *lnofile = NULL;
3614 for (i = 0; i < vec_size(self->fields); ++i)
3616 ir_builder_prepare_field(self->fields[i]);
3619 for (i = 0; i < vec_size(self->globals); ++i)
3621 if (!ir_builder_gen_global(self, self->globals[i], false)) {
3624 if (self->globals[i]->vtype == TYPE_FUNCTION) {
3625 ir_function *func = self->globals[i]->constval.vfunc;
3626 if (func && self->max_locals < func->allocated_locals &&
3627 !(func->flags & IR_FLAG_MASK_NO_OVERLAP))
3629 self->max_locals = func->allocated_locals;
3631 if (func && self->max_globaltemps < func->globaltemps)
3632 self->max_globaltemps = func->globaltemps;
3636 for (i = 0; i < vec_size(self->fields); ++i)
3638 if (!ir_builder_gen_field(self, self->fields[i])) {
3644 ir_value_code_setaddr(self->nil, vec_size(code_globals));
3645 vec_push(code_globals, 0);
3646 vec_push(code_globals, 0);
3647 vec_push(code_globals, 0);
3649 /* generate global temps */
3650 self->first_common_globaltemp = vec_size(code_globals);
3651 for (i = 0; i < self->max_globaltemps; ++i) {
3652 vec_push(code_globals, 0);
3654 /* generate common locals */
3655 self->first_common_local = vec_size(code_globals);
3656 for (i = 0; i < self->max_locals; ++i) {
3657 vec_push(code_globals, 0);
3660 /* generate function code */
3661 for (i = 0; i < vec_size(self->globals); ++i)
3663 if (self->globals[i]->vtype == TYPE_FUNCTION) {
3664 if (!gen_global_function_code(self, self->globals[i])) {
3670 if (vec_size(code_globals) >= 65536) {
3671 irerror(vec_last(self->globals)->context, "This progs file would require more globals than the metadata can handle. Bailing out.");
3675 /* DP errors if the last instruction is not an INSTR_DONE. */
3676 if (vec_last(code_statements).opcode != INSTR_DONE)
3678 stmt.opcode = INSTR_DONE;
3682 code_push_statement(&stmt, vec_last(code_linenums));
3685 if (OPTS_OPTION_BOOL(OPTION_PP_ONLY))
3688 if (vec_size(code_statements) != vec_size(code_linenums)) {
3689 con_err("Linecounter wrong: %lu != %lu\n",
3690 (unsigned long)vec_size(code_statements),
3691 (unsigned long)vec_size(code_linenums));
3692 } else if (OPTS_FLAG(LNO)) {
3694 size_t filelen = strlen(filename);
3696 memcpy(vec_add(lnofile, filelen+1), filename, filelen+1);
3697 dot = strrchr(lnofile, '.');
3701 vec_shrinkto(lnofile, dot - lnofile);
3703 memcpy(vec_add(lnofile, 5), ".lno", 5);
3706 if (!OPTS_OPTION_BOOL(OPTION_QUIET)) {
3708 con_out("writing '%s' and '%s'...\n", filename, lnofile);
3710 con_out("writing '%s'\n", filename);
3712 if (!code_write(filename, lnofile)) {
3720 /***********************************************************************
3721 *IR DEBUG Dump functions...
3724 #define IND_BUFSZ 1024
3727 # define strncat(dst, src, sz) strncat_s(dst, sz, src, _TRUNCATE)
3730 const char *qc_opname(int op)
3732 if (op < 0) return "<INVALID>";
3733 if (op < (int)( sizeof(asm_instr) / sizeof(asm_instr[0]) ))
3734 return asm_instr[op].m;
3736 case VINSTR_PHI: return "PHI";
3737 case VINSTR_JUMP: return "JUMP";
3738 case VINSTR_COND: return "COND";
3739 default: return "<UNK>";
3743 void ir_builder_dump(ir_builder *b, int (*oprintf)(const char*, ...))
3746 char indent[IND_BUFSZ];
3750 oprintf("module %s\n", b->name);
3751 for (i = 0; i < vec_size(b->globals); ++i)
3754 if (b->globals[i]->hasvalue)
3755 oprintf("%s = ", b->globals[i]->name);
3756 ir_value_dump(b->globals[i], oprintf);
3759 for (i = 0; i < vec_size(b->functions); ++i)
3760 ir_function_dump(b->functions[i], indent, oprintf);
3761 oprintf("endmodule %s\n", b->name);
3764 static const char *storenames[] = {
3765 "[global]", "[local]", "[param]", "[value]", "[return]"
3768 void ir_function_dump(ir_function *f, char *ind,
3769 int (*oprintf)(const char*, ...))
3772 if (f->builtin != 0) {
3773 oprintf("%sfunction %s = builtin %i\n", ind, f->name, -f->builtin);
3776 oprintf("%sfunction %s\n", ind, f->name);
3777 strncat(ind, "\t", IND_BUFSZ);
3778 if (vec_size(f->locals))
3780 oprintf("%s%i locals:\n", ind, (int)vec_size(f->locals));
3781 for (i = 0; i < vec_size(f->locals); ++i) {
3782 oprintf("%s\t", ind);
3783 ir_value_dump(f->locals[i], oprintf);
3787 oprintf("%sliferanges:\n", ind);
3788 for (i = 0; i < vec_size(f->locals); ++i) {
3789 const char *attr = "";
3791 ir_value *v = f->locals[i];
3792 if (v->unique_life && v->locked)
3793 attr = "unique,locked ";
3794 else if (v->unique_life)
3798 oprintf("%s\t%s: %s %s %s%s@%i ", ind, v->name, type_name[v->vtype],
3799 storenames[v->store],
3800 attr, (v->callparam ? "callparam " : ""),
3801 (int)v->code.local);
3804 for (l = 0; l < vec_size(v->life); ++l) {
3805 oprintf("[%i,%i] ", v->life[l].start, v->life[l].end);
3808 for (m = 0; m < 3; ++m) {
3809 ir_value *vm = v->members[m];
3812 oprintf("%s\t%s: @%i ", ind, vm->name, (int)vm->code.local);
3813 for (l = 0; l < vec_size(vm->life); ++l) {
3814 oprintf("[%i,%i] ", vm->life[l].start, vm->life[l].end);
3819 for (i = 0; i < vec_size(f->values); ++i) {
3820 const char *attr = "";
3822 ir_value *v = f->values[i];
3823 if (v->unique_life && v->locked)
3824 attr = "unique,locked ";
3825 else if (v->unique_life)
3829 oprintf("%s\t%s: %s %s %s%s@%i ", ind, v->name, type_name[v->vtype],
3830 storenames[v->store],
3831 attr, (v->callparam ? "callparam " : ""),
3832 (int)v->code.local);
3835 for (l = 0; l < vec_size(v->life); ++l) {
3836 oprintf("[%i,%i] ", v->life[l].start, v->life[l].end);
3839 for (m = 0; m < 3; ++m) {
3840 ir_value *vm = v->members[m];
3843 if (vm->unique_life && vm->locked)
3844 attr = "unique,locked ";
3845 else if (vm->unique_life)
3847 else if (vm->locked)
3849 oprintf("%s\t%s: %s@%i ", ind, vm->name, attr, (int)vm->code.local);
3850 for (l = 0; l < vec_size(vm->life); ++l) {
3851 oprintf("[%i,%i] ", vm->life[l].start, vm->life[l].end);
3856 if (vec_size(f->blocks))
3858 oprintf("%slife passes: %i\n", ind, (int)f->run_id);
3859 for (i = 0; i < vec_size(f->blocks); ++i) {
3860 ir_block_dump(f->blocks[i], ind, oprintf);
3864 ind[strlen(ind)-1] = 0;
3865 oprintf("%sendfunction %s\n", ind, f->name);
3868 void ir_block_dump(ir_block* b, char *ind,
3869 int (*oprintf)(const char*, ...))
3872 oprintf("%s:%s\n", ind, b->label);
3873 strncat(ind, "\t", IND_BUFSZ);
3875 if (b->instr && b->instr[0])
3876 oprintf("%s (%i) [entry]\n", ind, (int)(b->instr[0]->eid-1));
3877 for (i = 0; i < vec_size(b->instr); ++i)
3878 ir_instr_dump(b->instr[i], ind, oprintf);
3879 ind[strlen(ind)-1] = 0;
3882 void dump_phi(ir_instr *in, int (*oprintf)(const char*, ...))
3885 oprintf("%s <- phi ", in->_ops[0]->name);
3886 for (i = 0; i < vec_size(in->phi); ++i)
3888 oprintf("([%s] : %s) ", in->phi[i].from->label,
3889 in->phi[i].value->name);
3894 void ir_instr_dump(ir_instr *in, char *ind,
3895 int (*oprintf)(const char*, ...))
3898 const char *comma = NULL;
3900 oprintf("%s (%i) ", ind, (int)in->eid);
3902 if (in->opcode == VINSTR_PHI) {
3903 dump_phi(in, oprintf);
3907 strncat(ind, "\t", IND_BUFSZ);
3909 if (in->_ops[0] && (in->_ops[1] || in->_ops[2])) {
3910 ir_value_dump(in->_ops[0], oprintf);
3911 if (in->_ops[1] || in->_ops[2])
3914 if (in->opcode == INSTR_CALL0 || in->opcode == VINSTR_NRCALL) {
3915 oprintf("CALL%i\t", vec_size(in->params));
3917 oprintf("%s\t", qc_opname(in->opcode));
3919 if (in->_ops[0] && !(in->_ops[1] || in->_ops[2])) {
3920 ir_value_dump(in->_ops[0], oprintf);
3925 for (i = 1; i != 3; ++i) {
3929 ir_value_dump(in->_ops[i], oprintf);
3937 oprintf("[%s]", in->bops[0]->label);
3941 oprintf("%s[%s]", comma, in->bops[1]->label);
3942 if (vec_size(in->params)) {
3943 oprintf("\tparams: ");
3944 for (i = 0; i != vec_size(in->params); ++i) {
3945 oprintf("%s, ", in->params[i]->name);
3949 ind[strlen(ind)-1] = 0;
3952 void ir_value_dump_string(const char *str, int (*oprintf)(const char*, ...))
3955 for (; *str; ++str) {
3957 case '\n': oprintf("\\n"); break;
3958 case '\r': oprintf("\\r"); break;
3959 case '\t': oprintf("\\t"); break;
3960 case '\v': oprintf("\\v"); break;
3961 case '\f': oprintf("\\f"); break;
3962 case '\b': oprintf("\\b"); break;
3963 case '\a': oprintf("\\a"); break;
3964 case '\\': oprintf("\\\\"); break;
3965 case '"': oprintf("\\\""); break;
3966 default: oprintf("%c", *str); break;
3972 void ir_value_dump(ir_value* v, int (*oprintf)(const char*, ...))
3981 oprintf("fn:%s", v->name);
3984 oprintf("%g", v->constval.vfloat);
3987 oprintf("'%g %g %g'",
3990 v->constval.vvec.z);
3993 oprintf("(entity)");
3996 ir_value_dump_string(v->constval.vstring, oprintf);
4000 oprintf("%i", v->constval.vint);
4005 v->constval.vpointer->name);
4009 oprintf("%s", v->name);
4013 void ir_value_dump_life(const ir_value *self, int (*oprintf)(const char*,...))
4016 oprintf("Life of %12s:", self->name);
4017 for (i = 0; i < vec_size(self->life); ++i)
4019 oprintf(" + [%i, %i]\n", self->life[i].start, self->life[i].end);