2 * Copyright (C) 2012, 2013
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is furnished to do
10 * so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 /***********************************************************************
29 * Type sizes used at multiple points in the IR codegen
32 const char *type_name[TYPE_COUNT] = {
51 size_t type_sizeof_[TYPE_COUNT] = {
58 1, /* TYPE_FUNCTION */
69 uint16_t type_store_instr[TYPE_COUNT] = {
70 INSTR_STORE_F, /* should use I when having integer support */
77 INSTR_STORE_ENT, /* should use I */
79 INSTR_STORE_I, /* integer type */
84 INSTR_STORE_V, /* variant, should never be accessed */
86 AINSTR_END, /* struct */
87 AINSTR_END, /* union */
88 AINSTR_END, /* array */
90 AINSTR_END, /* noexpr */
93 uint16_t field_store_instr[TYPE_COUNT] = {
103 INSTR_STORE_FLD, /* integer type */
108 INSTR_STORE_V, /* variant, should never be accessed */
110 AINSTR_END, /* struct */
111 AINSTR_END, /* union */
112 AINSTR_END, /* array */
113 AINSTR_END, /* nil */
114 AINSTR_END, /* noexpr */
117 uint16_t type_storep_instr[TYPE_COUNT] = {
118 INSTR_STOREP_F, /* should use I when having integer support */
125 INSTR_STOREP_ENT, /* should use I */
127 INSTR_STOREP_ENT, /* integer type */
132 INSTR_STOREP_V, /* variant, should never be accessed */
134 AINSTR_END, /* struct */
135 AINSTR_END, /* union */
136 AINSTR_END, /* array */
137 AINSTR_END, /* nil */
138 AINSTR_END, /* noexpr */
141 uint16_t type_eq_instr[TYPE_COUNT] = {
142 INSTR_EQ_F, /* should use I when having integer support */
147 INSTR_EQ_E, /* FLD has no comparison */
149 INSTR_EQ_E, /* should use I */
156 INSTR_EQ_V, /* variant, should never be accessed */
158 AINSTR_END, /* struct */
159 AINSTR_END, /* union */
160 AINSTR_END, /* array */
161 AINSTR_END, /* nil */
162 AINSTR_END, /* noexpr */
165 uint16_t type_ne_instr[TYPE_COUNT] = {
166 INSTR_NE_F, /* should use I when having integer support */
171 INSTR_NE_E, /* FLD has no comparison */
173 INSTR_NE_E, /* should use I */
180 INSTR_NE_V, /* variant, should never be accessed */
182 AINSTR_END, /* struct */
183 AINSTR_END, /* union */
184 AINSTR_END, /* array */
185 AINSTR_END, /* nil */
186 AINSTR_END, /* noexpr */
189 uint16_t type_not_instr[TYPE_COUNT] = {
190 INSTR_NOT_F, /* should use I when having integer support */
197 INSTR_NOT_ENT, /* should use I */
199 INSTR_NOT_I, /* integer type */
204 INSTR_NOT_V, /* variant, should never be accessed */
206 AINSTR_END, /* struct */
207 AINSTR_END, /* union */
208 AINSTR_END, /* array */
209 AINSTR_END, /* nil */
210 AINSTR_END, /* noexpr */
214 static ir_value* ir_gen_extparam_proto(ir_builder *ir);
215 static void ir_gen_extparam (ir_builder *ir);
217 /* error functions */
219 static void irerror(lex_ctx ctx, const char *msg, ...)
223 con_cvprintmsg((void*)&ctx, LVL_ERROR, "internal error", msg, ap);
227 static bool irwarning(lex_ctx ctx, int warntype, const char *fmt, ...)
232 r = vcompile_warning(ctx, warntype, fmt, ap);
237 /***********************************************************************
238 * Vector utility functions
241 bool GMQCC_WARN vec_ir_value_find(ir_value **vec, const ir_value *what, size_t *idx)
244 size_t len = vec_size(vec);
245 for (i = 0; i < len; ++i) {
246 if (vec[i] == what) {
254 bool GMQCC_WARN vec_ir_block_find(ir_block **vec, ir_block *what, size_t *idx)
257 size_t len = vec_size(vec);
258 for (i = 0; i < len; ++i) {
259 if (vec[i] == what) {
267 bool GMQCC_WARN vec_ir_instr_find(ir_instr **vec, ir_instr *what, size_t *idx)
270 size_t len = vec_size(vec);
271 for (i = 0; i < len; ++i) {
272 if (vec[i] == what) {
280 /***********************************************************************
284 static void ir_block_delete_quick(ir_block* self);
285 static void ir_instr_delete_quick(ir_instr *self);
286 static void ir_function_delete_quick(ir_function *self);
288 ir_builder* ir_builder_new(const char *modulename)
292 self = (ir_builder*)mem_a(sizeof(*self));
296 self->functions = NULL;
297 self->globals = NULL;
299 self->filenames = NULL;
300 self->filestrings = NULL;
301 self->htglobals = util_htnew(IR_HT_SIZE);
302 self->htfields = util_htnew(IR_HT_SIZE);
303 self->htfunctions = util_htnew(IR_HT_SIZE);
305 self->extparams = NULL;
306 self->extparam_protos = NULL;
308 self->first_common_globaltemp = 0;
309 self->max_globaltemps = 0;
310 self->first_common_local = 0;
311 self->max_locals = 0;
313 self->str_immediate = 0;
315 if (!ir_builder_set_name(self, modulename)) {
320 self->nil = ir_value_var("nil", store_value, TYPE_NIL);
321 self->nil->cvq = CV_CONST;
323 self->reserved_va_count = NULL;
328 void ir_builder_delete(ir_builder* self)
331 util_htdel(self->htglobals);
332 util_htdel(self->htfields);
333 util_htdel(self->htfunctions);
334 mem_d((void*)self->name);
335 for (i = 0; i != vec_size(self->functions); ++i) {
336 ir_function_delete_quick(self->functions[i]);
338 vec_free(self->functions);
339 for (i = 0; i != vec_size(self->extparams); ++i) {
340 ir_value_delete(self->extparams[i]);
342 vec_free(self->extparams);
343 for (i = 0; i != vec_size(self->globals); ++i) {
344 ir_value_delete(self->globals[i]);
346 vec_free(self->globals);
347 for (i = 0; i != vec_size(self->fields); ++i) {
348 ir_value_delete(self->fields[i]);
350 ir_value_delete(self->nil);
351 vec_free(self->fields);
352 vec_free(self->filenames);
353 vec_free(self->filestrings);
357 bool ir_builder_set_name(ir_builder *self, const char *name)
360 mem_d((void*)self->name);
361 self->name = util_strdup(name);
365 ir_function* ir_builder_get_function(ir_builder *self, const char *name)
367 return (ir_function*)util_htget(self->htfunctions, name);
370 ir_function* ir_builder_create_function(ir_builder *self, const char *name, int outtype)
372 ir_function *fn = ir_builder_get_function(self, name);
377 fn = ir_function_new(self, outtype);
378 if (!ir_function_set_name(fn, name))
380 ir_function_delete(fn);
383 vec_push(self->functions, fn);
384 util_htset(self->htfunctions, name, fn);
386 fn->value = ir_builder_create_global(self, fn->name, TYPE_FUNCTION);
388 ir_function_delete(fn);
392 fn->value->hasvalue = true;
393 fn->value->outtype = outtype;
394 fn->value->constval.vfunc = fn;
395 fn->value->context = fn->context;
400 ir_value* ir_builder_get_global(ir_builder *self, const char *name)
402 return (ir_value*)util_htget(self->htglobals, name);
405 ir_value* ir_builder_create_global(ir_builder *self, const char *name, int vtype)
409 if (name && name[0] != '#')
411 ve = ir_builder_get_global(self, name);
417 ve = ir_value_var(name, store_global, vtype);
418 vec_push(self->globals, ve);
419 util_htset(self->htglobals, name, ve);
423 ir_value* ir_builder_get_va_count(ir_builder *self)
425 if (self->reserved_va_count)
426 return self->reserved_va_count;
427 return (self->reserved_va_count = ir_builder_create_global(self, "reserved:va_count", TYPE_FLOAT));
430 ir_value* ir_builder_get_field(ir_builder *self, const char *name)
432 return (ir_value*)util_htget(self->htfields, name);
436 ir_value* ir_builder_create_field(ir_builder *self, const char *name, int vtype)
438 ir_value *ve = ir_builder_get_field(self, name);
443 ve = ir_value_var(name, store_global, TYPE_FIELD);
444 ve->fieldtype = vtype;
445 vec_push(self->fields, ve);
446 util_htset(self->htfields, name, ve);
450 /***********************************************************************
454 bool ir_function_naive_phi(ir_function*);
455 void ir_function_enumerate(ir_function*);
456 bool ir_function_calculate_liferanges(ir_function*);
457 bool ir_function_allocate_locals(ir_function*);
459 ir_function* ir_function_new(ir_builder* owner, int outtype)
462 self = (ir_function*)mem_a(sizeof(*self));
467 memset(self, 0, sizeof(*self));
470 if (!ir_function_set_name(self, "<@unnamed>")) {
477 self->context.file = "<@no context>";
478 self->context.line = 0;
479 self->outtype = outtype;
488 self->max_varargs = NULL;
490 self->code_function_def = -1;
491 self->allocated_locals = 0;
492 self->globaltemps = 0;
498 bool ir_function_set_name(ir_function *self, const char *name)
501 mem_d((void*)self->name);
502 self->name = util_strdup(name);
506 static void ir_function_delete_quick(ir_function *self)
509 mem_d((void*)self->name);
511 for (i = 0; i != vec_size(self->blocks); ++i)
512 ir_block_delete_quick(self->blocks[i]);
513 vec_free(self->blocks);
515 vec_free(self->params);
517 for (i = 0; i != vec_size(self->values); ++i)
518 ir_value_delete(self->values[i]);
519 vec_free(self->values);
521 for (i = 0; i != vec_size(self->locals); ++i)
522 ir_value_delete(self->locals[i]);
523 vec_free(self->locals);
525 /* self->value is deleted by the builder */
530 void ir_function_delete(ir_function *self)
533 mem_d((void*)self->name);
535 for (i = 0; i != vec_size(self->blocks); ++i)
536 ir_block_delete(self->blocks[i]);
537 vec_free(self->blocks);
539 vec_free(self->params);
541 for (i = 0; i != vec_size(self->values); ++i)
542 ir_value_delete(self->values[i]);
543 vec_free(self->values);
545 for (i = 0; i != vec_size(self->locals); ++i)
546 ir_value_delete(self->locals[i]);
547 vec_free(self->locals);
549 /* self->value is deleted by the builder */
554 void ir_function_collect_value(ir_function *self, ir_value *v)
556 vec_push(self->values, v);
559 ir_block* ir_function_create_block(lex_ctx ctx, ir_function *self, const char *label)
561 ir_block* bn = ir_block_new(self, label);
563 vec_push(self->blocks, bn);
567 static bool instr_is_operation(uint16_t op)
569 return ( (op >= INSTR_MUL_F && op <= INSTR_GT) ||
570 (op >= INSTR_LOAD_F && op <= INSTR_LOAD_FNC) ||
571 (op == INSTR_ADDRESS) ||
572 (op >= INSTR_NOT_F && op <= INSTR_NOT_FNC) ||
573 (op >= INSTR_AND && op <= INSTR_BITOR) ||
574 (op >= INSTR_CALL0 && op <= INSTR_CALL8) );
577 bool ir_function_pass_peephole(ir_function *self)
581 for (b = 0; b < vec_size(self->blocks); ++b) {
583 ir_block *block = self->blocks[b];
585 for (i = 0; i < vec_size(block->instr); ++i) {
587 inst = block->instr[i];
590 (inst->opcode >= INSTR_STORE_F &&
591 inst->opcode <= INSTR_STORE_FNC))
599 oper = block->instr[i-1];
600 if (!instr_is_operation(oper->opcode))
603 value = oper->_ops[0];
605 /* only do it for SSA values */
606 if (value->store != store_value)
609 /* don't optimize out the temp if it's used later again */
610 if (vec_size(value->reads) != 1)
613 /* The very next store must use this value */
614 if (value->reads[0] != store)
617 /* And of course the store must _read_ from it, so it's in
619 if (store->_ops[1] != value)
622 ++opts_optimizationcount[OPTIM_PEEPHOLE];
623 (void)!ir_instr_op(oper, 0, store->_ops[0], true);
625 vec_remove(block->instr, i, 1);
626 ir_instr_delete(store);
628 else if (inst->opcode == VINSTR_COND)
630 /* COND on a value resulting from a NOT could
631 * remove the NOT and swap its operands
638 value = inst->_ops[0];
640 if (value->store != store_value ||
641 vec_size(value->reads) != 1 ||
642 value->reads[0] != inst)
647 inot = value->writes[0];
648 if (inot->_ops[0] != value ||
649 inot->opcode < INSTR_NOT_F ||
650 inot->opcode > INSTR_NOT_FNC ||
651 inot->opcode == INSTR_NOT_V || /* can't do these */
652 inot->opcode == INSTR_NOT_S)
658 ++opts_optimizationcount[OPTIM_PEEPHOLE];
660 (void)!ir_instr_op(inst, 0, inot->_ops[1], false);
663 for (inotid = 0; inotid < vec_size(tmp->instr); ++inotid) {
664 if (tmp->instr[inotid] == inot)
667 if (inotid >= vec_size(tmp->instr)) {
668 compile_error(inst->context, "sanity-check failed: failed to find instruction to optimize out");
671 vec_remove(tmp->instr, inotid, 1);
672 ir_instr_delete(inot);
673 /* swap ontrue/onfalse */
675 inst->bops[0] = inst->bops[1];
686 bool ir_function_pass_tailrecursion(ir_function *self)
690 for (b = 0; b < vec_size(self->blocks); ++b) {
692 ir_instr *ret, *call, *store = NULL;
693 ir_block *block = self->blocks[b];
695 if (!block->final || vec_size(block->instr) < 2)
698 ret = block->instr[vec_size(block->instr)-1];
699 if (ret->opcode != INSTR_DONE && ret->opcode != INSTR_RETURN)
702 call = block->instr[vec_size(block->instr)-2];
703 if (call->opcode >= INSTR_STORE_F && call->opcode <= INSTR_STORE_FNC) {
704 /* account for the unoptimized
706 * STORE %return, %tmp
710 if (vec_size(block->instr) < 3)
714 call = block->instr[vec_size(block->instr)-3];
717 if (call->opcode < INSTR_CALL0 || call->opcode > INSTR_CALL8)
721 /* optimize out the STORE */
723 ret->_ops[0] == store->_ops[0] &&
724 store->_ops[1] == call->_ops[0])
726 ++opts_optimizationcount[OPTIM_PEEPHOLE];
727 call->_ops[0] = store->_ops[0];
728 vec_remove(block->instr, vec_size(block->instr) - 2, 1);
729 ir_instr_delete(store);
738 funcval = call->_ops[1];
741 if (funcval->vtype != TYPE_FUNCTION || funcval->constval.vfunc != self)
744 /* now we have a CALL and a RET, check if it's a tailcall */
745 if (ret->_ops[0] && call->_ops[0] != ret->_ops[0])
748 ++opts_optimizationcount[OPTIM_TAIL_RECURSION];
749 vec_shrinkby(block->instr, 2);
751 block->final = false; /* open it back up */
753 /* emite parameter-stores */
754 for (p = 0; p < vec_size(call->params); ++p) {
755 /* assert(call->params_count <= self->locals_count); */
756 if (!ir_block_create_store(block, call->context, self->locals[p], call->params[p])) {
757 irerror(call->context, "failed to create tailcall store instruction for parameter %i", (int)p);
761 if (!ir_block_create_jump(block, call->context, self->blocks[0])) {
762 irerror(call->context, "failed to create tailcall jump");
766 ir_instr_delete(call);
767 ir_instr_delete(ret);
773 bool ir_function_finalize(ir_function *self)
780 if (OPTS_OPTIMIZATION(OPTIM_PEEPHOLE)) {
781 if (!ir_function_pass_peephole(self)) {
782 irerror(self->context, "generic optimization pass broke something in `%s`", self->name);
787 if (OPTS_OPTIMIZATION(OPTIM_TAIL_RECURSION)) {
788 if (!ir_function_pass_tailrecursion(self)) {
789 irerror(self->context, "tail-recursion optimization pass broke something in `%s`", self->name);
794 if (!ir_function_naive_phi(self)) {
795 irerror(self->context, "internal error: ir_function_naive_phi failed");
799 for (i = 0; i < vec_size(self->locals); ++i) {
800 ir_value *v = self->locals[i];
801 if (v->vtype == TYPE_VECTOR ||
802 (v->vtype == TYPE_FIELD && v->outtype == TYPE_VECTOR))
804 ir_value_vector_member(v, 0);
805 ir_value_vector_member(v, 1);
806 ir_value_vector_member(v, 2);
809 for (i = 0; i < vec_size(self->values); ++i) {
810 ir_value *v = self->values[i];
811 if (v->vtype == TYPE_VECTOR ||
812 (v->vtype == TYPE_FIELD && v->outtype == TYPE_VECTOR))
814 ir_value_vector_member(v, 0);
815 ir_value_vector_member(v, 1);
816 ir_value_vector_member(v, 2);
820 ir_function_enumerate(self);
822 if (!ir_function_calculate_liferanges(self))
824 if (!ir_function_allocate_locals(self))
829 ir_value* ir_function_create_local(ir_function *self, const char *name, int vtype, bool param)
834 vec_size(self->locals) &&
835 self->locals[vec_size(self->locals)-1]->store != store_param) {
836 irerror(self->context, "cannot add parameters after adding locals");
840 ve = ir_value_var(name, (param ? store_param : store_local), vtype);
843 vec_push(self->locals, ve);
847 /***********************************************************************
851 ir_block* ir_block_new(ir_function* owner, const char *name)
854 self = (ir_block*)mem_a(sizeof(*self));
858 memset(self, 0, sizeof(*self));
861 if (name && !ir_block_set_label(self, name)) {
866 self->context.file = "<@no context>";
867 self->context.line = 0;
871 self->entries = NULL;
875 self->is_return = false;
880 self->generated = false;
885 static void ir_block_delete_quick(ir_block* self)
888 if (self->label) mem_d(self->label);
889 for (i = 0; i != vec_size(self->instr); ++i)
890 ir_instr_delete_quick(self->instr[i]);
891 vec_free(self->instr);
892 vec_free(self->entries);
893 vec_free(self->exits);
894 vec_free(self->living);
898 void ir_block_delete(ir_block* self)
901 if (self->label) mem_d(self->label);
902 for (i = 0; i != vec_size(self->instr); ++i)
903 ir_instr_delete(self->instr[i]);
904 vec_free(self->instr);
905 vec_free(self->entries);
906 vec_free(self->exits);
907 vec_free(self->living);
911 bool ir_block_set_label(ir_block *self, const char *name)
914 mem_d((void*)self->label);
915 self->label = util_strdup(name);
916 return !!self->label;
919 /***********************************************************************
923 ir_instr* ir_instr_new(lex_ctx ctx, ir_block* owner, int op)
926 self = (ir_instr*)mem_a(sizeof(*self));
933 self->_ops[0] = NULL;
934 self->_ops[1] = NULL;
935 self->_ops[2] = NULL;
936 self->bops[0] = NULL;
937 self->bops[1] = NULL;
948 static void ir_instr_delete_quick(ir_instr *self)
951 vec_free(self->params);
955 void ir_instr_delete(ir_instr *self)
958 /* The following calls can only delete from
959 * vectors, we still want to delete this instruction
960 * so ignore the return value. Since with the warn_unused_result attribute
961 * gcc doesn't care about an explicit: (void)foo(); to ignore the result,
962 * I have to improvise here and use if(foo());
964 for (i = 0; i < vec_size(self->phi); ++i) {
966 if (vec_ir_instr_find(self->phi[i].value->writes, self, &idx))
967 vec_remove(self->phi[i].value->writes, idx, 1);
968 if (vec_ir_instr_find(self->phi[i].value->reads, self, &idx))
969 vec_remove(self->phi[i].value->reads, idx, 1);
972 for (i = 0; i < vec_size(self->params); ++i) {
974 if (vec_ir_instr_find(self->params[i]->writes, self, &idx))
975 vec_remove(self->params[i]->writes, idx, 1);
976 if (vec_ir_instr_find(self->params[i]->reads, self, &idx))
977 vec_remove(self->params[i]->reads, idx, 1);
979 vec_free(self->params);
980 (void)!ir_instr_op(self, 0, NULL, false);
981 (void)!ir_instr_op(self, 1, NULL, false);
982 (void)!ir_instr_op(self, 2, NULL, false);
986 bool ir_instr_op(ir_instr *self, int op, ir_value *v, bool writing)
988 if (self->_ops[op]) {
990 if (writing && vec_ir_instr_find(self->_ops[op]->writes, self, &idx))
991 vec_remove(self->_ops[op]->writes, idx, 1);
992 else if (vec_ir_instr_find(self->_ops[op]->reads, self, &idx))
993 vec_remove(self->_ops[op]->reads, idx, 1);
997 vec_push(v->writes, self);
999 vec_push(v->reads, self);
1005 /***********************************************************************
1009 void ir_value_code_setaddr(ir_value *self, int32_t gaddr)
1011 self->code.globaladdr = gaddr;
1012 if (self->members[0]) self->members[0]->code.globaladdr = gaddr;
1013 if (self->members[1]) self->members[1]->code.globaladdr = gaddr;
1014 if (self->members[2]) self->members[2]->code.globaladdr = gaddr;
1017 int32_t ir_value_code_addr(const ir_value *self)
1019 if (self->store == store_return)
1020 return OFS_RETURN + self->code.addroffset;
1021 return self->code.globaladdr + self->code.addroffset;
1024 ir_value* ir_value_var(const char *name, int storetype, int vtype)
1027 self = (ir_value*)mem_a(sizeof(*self));
1028 self->vtype = vtype;
1029 self->fieldtype = TYPE_VOID;
1030 self->outtype = TYPE_VOID;
1031 self->store = storetype;
1035 self->writes = NULL;
1037 self->cvq = CV_NONE;
1038 self->hasvalue = false;
1039 self->context.file = "<@no context>";
1040 self->context.line = 0;
1042 if (name && !ir_value_set_name(self, name)) {
1043 irerror(self->context, "out of memory");
1048 memset(&self->constval, 0, sizeof(self->constval));
1049 memset(&self->code, 0, sizeof(self->code));
1051 self->members[0] = NULL;
1052 self->members[1] = NULL;
1053 self->members[2] = NULL;
1054 self->memberof = NULL;
1056 self->unique_life = false;
1057 self->locked = false;
1058 self->callparam = false;
1064 ir_value* ir_value_vector_member(ir_value *self, unsigned int member)
1072 if (self->members[member])
1073 return self->members[member];
1076 len = strlen(self->name);
1077 name = (char*)mem_a(len + 3);
1078 memcpy(name, self->name, len);
1080 name[len+1] = 'x' + member;
1086 if (self->vtype == TYPE_VECTOR)
1088 m = ir_value_var(name, self->store, TYPE_FLOAT);
1093 m->context = self->context;
1095 self->members[member] = m;
1096 m->code.addroffset = member;
1098 else if (self->vtype == TYPE_FIELD)
1100 if (self->fieldtype != TYPE_VECTOR)
1102 m = ir_value_var(name, self->store, TYPE_FIELD);
1107 m->fieldtype = TYPE_FLOAT;
1108 m->context = self->context;
1110 self->members[member] = m;
1111 m->code.addroffset = member;
1115 irerror(self->context, "invalid member access on %s", self->name);
1123 static GMQCC_INLINE size_t ir_value_sizeof(const ir_value *self)
1125 if (self->vtype == TYPE_FIELD && self->fieldtype == TYPE_VECTOR)
1126 return type_sizeof_[TYPE_VECTOR];
1127 return type_sizeof_[self->vtype];
1130 ir_value* ir_value_out(ir_function *owner, const char *name, int storetype, int vtype)
1132 ir_value *v = ir_value_var(name, storetype, vtype);
1135 ir_function_collect_value(owner, v);
1139 void ir_value_delete(ir_value* self)
1143 mem_d((void*)self->name);
1146 if (self->vtype == TYPE_STRING)
1147 mem_d((void*)self->constval.vstring);
1149 for (i = 0; i < 3; ++i) {
1150 if (self->members[i])
1151 ir_value_delete(self->members[i]);
1153 vec_free(self->reads);
1154 vec_free(self->writes);
1155 vec_free(self->life);
1159 bool ir_value_set_name(ir_value *self, const char *name)
1162 mem_d((void*)self->name);
1163 self->name = util_strdup(name);
1164 return !!self->name;
1167 bool ir_value_set_float(ir_value *self, float f)
1169 if (self->vtype != TYPE_FLOAT)
1171 self->constval.vfloat = f;
1172 self->hasvalue = true;
1176 bool ir_value_set_func(ir_value *self, int f)
1178 if (self->vtype != TYPE_FUNCTION)
1180 self->constval.vint = f;
1181 self->hasvalue = true;
1185 bool ir_value_set_vector(ir_value *self, vector v)
1187 if (self->vtype != TYPE_VECTOR)
1189 self->constval.vvec = v;
1190 self->hasvalue = true;
1194 bool ir_value_set_field(ir_value *self, ir_value *fld)
1196 if (self->vtype != TYPE_FIELD)
1198 self->constval.vpointer = fld;
1199 self->hasvalue = true;
1203 static char *ir_strdup(const char *str)
1206 /* actually dup empty strings */
1207 char *out = (char*)mem_a(1);
1211 return util_strdup(str);
1214 bool ir_value_set_string(ir_value *self, const char *str)
1216 if (self->vtype != TYPE_STRING)
1218 self->constval.vstring = ir_strdup(str);
1219 self->hasvalue = true;
1224 bool ir_value_set_int(ir_value *self, int i)
1226 if (self->vtype != TYPE_INTEGER)
1228 self->constval.vint = i;
1229 self->hasvalue = true;
1234 bool ir_value_lives(ir_value *self, size_t at)
1237 for (i = 0; i < vec_size(self->life); ++i)
1239 ir_life_entry_t *life = &self->life[i];
1240 if (life->start <= at && at <= life->end)
1242 if (life->start > at) /* since it's ordered */
1248 bool ir_value_life_insert(ir_value *self, size_t idx, ir_life_entry_t e)
1251 vec_push(self->life, e);
1252 for (k = vec_size(self->life)-1; k > idx; --k)
1253 self->life[k] = self->life[k-1];
1254 self->life[idx] = e;
1258 bool ir_value_life_merge(ir_value *self, size_t s)
1261 ir_life_entry_t *life = NULL;
1262 ir_life_entry_t *before = NULL;
1263 ir_life_entry_t new_entry;
1265 /* Find the first range >= s */
1266 for (i = 0; i < vec_size(self->life); ++i)
1269 life = &self->life[i];
1270 if (life->start > s)
1273 /* nothing found? append */
1274 if (i == vec_size(self->life)) {
1276 if (life && life->end+1 == s)
1278 /* previous life range can be merged in */
1282 if (life && life->end >= s)
1284 e.start = e.end = s;
1285 vec_push(self->life, e);
1291 if (before->end + 1 == s &&
1292 life->start - 1 == s)
1295 before->end = life->end;
1296 vec_remove(self->life, i, 1);
1299 if (before->end + 1 == s)
1305 /* already contained */
1306 if (before->end >= s)
1310 if (life->start - 1 == s)
1315 /* insert a new entry */
1316 new_entry.start = new_entry.end = s;
1317 return ir_value_life_insert(self, i, new_entry);
1320 bool ir_value_life_merge_into(ir_value *self, const ir_value *other)
1324 if (!vec_size(other->life))
1327 if (!vec_size(self->life)) {
1328 size_t count = vec_size(other->life);
1329 ir_life_entry_t *life = vec_add(self->life, count);
1330 memcpy(life, other->life, count * sizeof(*life));
1335 for (i = 0; i < vec_size(other->life); ++i)
1337 const ir_life_entry_t *life = &other->life[i];
1340 ir_life_entry_t *entry = &self->life[myi];
1342 if (life->end+1 < entry->start)
1344 /* adding an interval before entry */
1345 if (!ir_value_life_insert(self, myi, *life))
1351 if (life->start < entry->start &&
1352 life->end+1 >= entry->start)
1354 /* starts earlier and overlaps */
1355 entry->start = life->start;
1358 if (life->end > entry->end &&
1359 life->start <= entry->end+1)
1361 /* ends later and overlaps */
1362 entry->end = life->end;
1365 /* see if our change combines it with the next ranges */
1366 while (myi+1 < vec_size(self->life) &&
1367 entry->end+1 >= self->life[1+myi].start)
1369 /* overlaps with (myi+1) */
1370 if (entry->end < self->life[1+myi].end)
1371 entry->end = self->life[1+myi].end;
1372 vec_remove(self->life, myi+1, 1);
1373 entry = &self->life[myi];
1376 /* see if we're after the entry */
1377 if (life->start > entry->end)
1380 /* append if we're at the end */
1381 if (myi >= vec_size(self->life)) {
1382 vec_push(self->life, *life);
1385 /* otherweise check the next range */
1394 bool ir_values_overlap(const ir_value *a, const ir_value *b)
1396 /* For any life entry in A see if it overlaps with
1397 * any life entry in B.
1398 * Note that the life entries are orderes, so we can make a
1399 * more efficient algorithm there than naively translating the
1403 ir_life_entry_t *la, *lb, *enda, *endb;
1405 /* first of all, if either has no life range, they cannot clash */
1406 if (!vec_size(a->life) || !vec_size(b->life))
1411 enda = la + vec_size(a->life);
1412 endb = lb + vec_size(b->life);
1415 /* check if the entries overlap, for that,
1416 * both must start before the other one ends.
1418 if (la->start < lb->end &&
1419 lb->start < la->end)
1424 /* entries are ordered
1425 * one entry is earlier than the other
1426 * that earlier entry will be moved forward
1428 if (la->start < lb->start)
1430 /* order: A B, move A forward
1431 * check if we hit the end with A
1436 else /* if (lb->start < la->start) actually <= */
1438 /* order: B A, move B forward
1439 * check if we hit the end with B
1448 /***********************************************************************
1452 static bool ir_check_unreachable(ir_block *self)
1454 /* The IR should never have to deal with unreachable code */
1455 if (!self->final/* || OPTS_FLAG(ALLOW_UNREACHABLE_CODE)*/)
1457 irerror(self->context, "unreachable statement (%s)", self->label);
1461 bool ir_block_create_store_op(ir_block *self, lex_ctx ctx, int op, ir_value *target, ir_value *what)
1464 if (!ir_check_unreachable(self))
1467 if (target->store == store_value &&
1468 (op < INSTR_STOREP_F || op > INSTR_STOREP_FNC))
1470 irerror(self->context, "cannot store to an SSA value");
1471 irerror(self->context, "trying to store: %s <- %s", target->name, what->name);
1472 irerror(self->context, "instruction: %s", asm_instr[op].m);
1476 in = ir_instr_new(ctx, self, op);
1480 if (!ir_instr_op(in, 0, target, (op < INSTR_STOREP_F || op > INSTR_STOREP_FNC)) ||
1481 !ir_instr_op(in, 1, what, false))
1483 ir_instr_delete(in);
1486 vec_push(self->instr, in);
1490 bool ir_block_create_store(ir_block *self, lex_ctx ctx, ir_value *target, ir_value *what)
1494 if (target->vtype == TYPE_VARIANT)
1495 vtype = what->vtype;
1497 vtype = target->vtype;
1500 if (vtype == TYPE_FLOAT && what->vtype == TYPE_INTEGER)
1501 op = INSTR_CONV_ITOF;
1502 else if (vtype == TYPE_INTEGER && what->vtype == TYPE_FLOAT)
1503 op = INSTR_CONV_FTOI;
1505 op = type_store_instr[vtype];
1507 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS)) {
1508 if (op == INSTR_STORE_FLD && what->fieldtype == TYPE_VECTOR)
1512 return ir_block_create_store_op(self, ctx, op, target, what);
1515 bool ir_block_create_storep(ir_block *self, lex_ctx ctx, ir_value *target, ir_value *what)
1520 if (target->vtype != TYPE_POINTER)
1523 /* storing using pointer - target is a pointer, type must be
1524 * inferred from source
1526 vtype = what->vtype;
1528 op = type_storep_instr[vtype];
1529 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS)) {
1530 if (op == INSTR_STOREP_FLD && what->fieldtype == TYPE_VECTOR)
1531 op = INSTR_STOREP_V;
1534 return ir_block_create_store_op(self, ctx, op, target, what);
1537 bool ir_block_create_return(ir_block *self, lex_ctx ctx, ir_value *v)
1540 if (!ir_check_unreachable(self))
1543 self->is_return = true;
1544 in = ir_instr_new(ctx, self, INSTR_RETURN);
1548 if (v && !ir_instr_op(in, 0, v, false)) {
1549 ir_instr_delete(in);
1553 vec_push(self->instr, in);
1557 bool ir_block_create_if(ir_block *self, lex_ctx ctx, ir_value *v,
1558 ir_block *ontrue, ir_block *onfalse)
1561 if (!ir_check_unreachable(self))
1564 /*in = ir_instr_new(ctx, self, (v->vtype == TYPE_STRING ? INSTR_IF_S : INSTR_IF_F));*/
1565 in = ir_instr_new(ctx, self, VINSTR_COND);
1569 if (!ir_instr_op(in, 0, v, false)) {
1570 ir_instr_delete(in);
1574 in->bops[0] = ontrue;
1575 in->bops[1] = onfalse;
1577 vec_push(self->instr, in);
1579 vec_push(self->exits, ontrue);
1580 vec_push(self->exits, onfalse);
1581 vec_push(ontrue->entries, self);
1582 vec_push(onfalse->entries, self);
1586 bool ir_block_create_jump(ir_block *self, lex_ctx ctx, ir_block *to)
1589 if (!ir_check_unreachable(self))
1592 in = ir_instr_new(ctx, self, VINSTR_JUMP);
1597 vec_push(self->instr, in);
1599 vec_push(self->exits, to);
1600 vec_push(to->entries, self);
1604 bool ir_block_create_goto(ir_block *self, lex_ctx ctx, ir_block *to)
1606 self->owner->flags |= IR_FLAG_HAS_GOTO;
1607 return ir_block_create_jump(self, ctx, to);
1610 ir_instr* ir_block_create_phi(ir_block *self, lex_ctx ctx, const char *label, int ot)
1614 if (!ir_check_unreachable(self))
1616 in = ir_instr_new(ctx, self, VINSTR_PHI);
1619 out = ir_value_out(self->owner, label, store_value, ot);
1621 ir_instr_delete(in);
1624 if (!ir_instr_op(in, 0, out, true)) {
1625 ir_instr_delete(in);
1626 ir_value_delete(out);
1629 vec_push(self->instr, in);
1633 ir_value* ir_phi_value(ir_instr *self)
1635 return self->_ops[0];
1638 void ir_phi_add(ir_instr* self, ir_block *b, ir_value *v)
1642 if (!vec_ir_block_find(self->owner->entries, b, NULL)) {
1643 /* Must not be possible to cause this, otherwise the AST
1644 * is doing something wrong.
1646 irerror(self->context, "Invalid entry block for PHI");
1652 vec_push(v->reads, self);
1653 vec_push(self->phi, pe);
1656 /* call related code */
1657 ir_instr* ir_block_create_call(ir_block *self, lex_ctx ctx, const char *label, ir_value *func, bool noreturn)
1661 if (!ir_check_unreachable(self))
1663 in = ir_instr_new(ctx, self, (noreturn ? VINSTR_NRCALL : INSTR_CALL0));
1668 self->is_return = true;
1670 out = ir_value_out(self->owner, label, (func->outtype == TYPE_VOID) ? store_return : store_value, func->outtype);
1672 ir_instr_delete(in);
1675 if (!ir_instr_op(in, 0, out, true) ||
1676 !ir_instr_op(in, 1, func, false))
1678 ir_instr_delete(in);
1679 ir_value_delete(out);
1682 vec_push(self->instr, in);
1685 if (!ir_block_create_return(self, ctx, NULL)) {
1686 compile_error(ctx, "internal error: failed to generate dummy-return instruction");
1687 ir_instr_delete(in);
1695 ir_value* ir_call_value(ir_instr *self)
1697 return self->_ops[0];
1700 void ir_call_param(ir_instr* self, ir_value *v)
1702 vec_push(self->params, v);
1703 vec_push(v->reads, self);
1706 /* binary op related code */
1708 ir_value* ir_block_create_binop(ir_block *self, lex_ctx ctx,
1709 const char *label, int opcode,
1710 ir_value *left, ir_value *right)
1732 case INSTR_SUB_S: /* -- offset of string as float */
1737 case INSTR_BITOR_IF:
1738 case INSTR_BITOR_FI:
1739 case INSTR_BITAND_FI:
1740 case INSTR_BITAND_IF:
1755 case INSTR_BITAND_I:
1758 case INSTR_RSHIFT_I:
1759 case INSTR_LSHIFT_I:
1781 /* boolean operations result in floats */
1782 if (opcode >= INSTR_EQ_F && opcode <= INSTR_GT)
1784 else if (opcode >= INSTR_LE && opcode <= INSTR_GT)
1787 else if (opcode >= INSTR_LE_I && opcode <= INSTR_EQ_FI)
1792 if (ot == TYPE_VOID) {
1793 /* The AST or parser were supposed to check this! */
1797 return ir_block_create_general_instr(self, ctx, label, opcode, left, right, ot);
1800 ir_value* ir_block_create_unary(ir_block *self, lex_ctx ctx,
1801 const char *label, int opcode,
1804 int ot = TYPE_FLOAT;
1816 /* QC doesn't have other unary operations. We expect extensions to fill
1817 * the above list, otherwise we assume out-type = in-type, eg for an
1821 ot = operand->vtype;
1824 if (ot == TYPE_VOID) {
1825 /* The AST or parser were supposed to check this! */
1829 /* let's use the general instruction creator and pass NULL for OPB */
1830 return ir_block_create_general_instr(self, ctx, label, opcode, operand, NULL, ot);
1833 ir_value* ir_block_create_general_instr(ir_block *self, lex_ctx ctx, const char *label,
1834 int op, ir_value *a, ir_value *b, int outype)
1839 out = ir_value_out(self->owner, label, store_value, outype);
1843 instr = ir_instr_new(ctx, self, op);
1845 ir_value_delete(out);
1849 if (!ir_instr_op(instr, 0, out, true) ||
1850 !ir_instr_op(instr, 1, a, false) ||
1851 !ir_instr_op(instr, 2, b, false) )
1856 vec_push(self->instr, instr);
1860 ir_instr_delete(instr);
1861 ir_value_delete(out);
1865 ir_value* ir_block_create_fieldaddress(ir_block *self, lex_ctx ctx, const char *label, ir_value *ent, ir_value *field)
1869 /* Support for various pointer types todo if so desired */
1870 if (ent->vtype != TYPE_ENTITY)
1873 if (field->vtype != TYPE_FIELD)
1876 v = ir_block_create_general_instr(self, ctx, label, INSTR_ADDRESS, ent, field, TYPE_POINTER);
1877 v->fieldtype = field->fieldtype;
1881 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)
1884 if (ent->vtype != TYPE_ENTITY)
1887 /* at some point we could redirect for TYPE_POINTER... but that could lead to carelessness */
1888 if (field->vtype != TYPE_FIELD)
1893 case TYPE_FLOAT: op = INSTR_LOAD_F; break;
1894 case TYPE_VECTOR: op = INSTR_LOAD_V; break;
1895 case TYPE_STRING: op = INSTR_LOAD_S; break;
1896 case TYPE_FIELD: op = INSTR_LOAD_FLD; break;
1897 case TYPE_ENTITY: op = INSTR_LOAD_ENT; break;
1898 case TYPE_FUNCTION: op = INSTR_LOAD_FNC; break;
1900 case TYPE_POINTER: op = INSTR_LOAD_I; break;
1901 case TYPE_INTEGER: op = INSTR_LOAD_I; break;
1904 irerror(self->context, "invalid type for ir_block_create_load_from_ent: %s", type_name[outype]);
1908 return ir_block_create_general_instr(self, ctx, label, op, ent, field, outype);
1911 /* PHI resolving breaks the SSA, and must thus be the last
1912 * step before life-range calculation.
1915 static bool ir_block_naive_phi(ir_block *self);
1916 bool ir_function_naive_phi(ir_function *self)
1920 for (i = 0; i < vec_size(self->blocks); ++i)
1922 if (!ir_block_naive_phi(self->blocks[i]))
1928 static bool ir_block_naive_phi(ir_block *self)
1930 size_t i, p; /*, w;*/
1931 /* FIXME: optionally, create_phi can add the phis
1932 * to a list so we don't need to loop through blocks
1933 * - anyway: "don't optimize YET"
1935 for (i = 0; i < vec_size(self->instr); ++i)
1937 ir_instr *instr = self->instr[i];
1938 if (instr->opcode != VINSTR_PHI)
1941 vec_remove(self->instr, i, 1);
1942 --i; /* NOTE: i+1 below */
1944 for (p = 0; p < vec_size(instr->phi); ++p)
1946 ir_value *v = instr->phi[p].value;
1947 ir_block *b = instr->phi[p].from;
1949 if (v->store == store_value &&
1950 vec_size(v->reads) == 1 &&
1951 vec_size(v->writes) == 1)
1953 /* replace the value */
1954 if (!ir_instr_op(v->writes[0], 0, instr->_ops[0], true))
1959 /* force a move instruction */
1960 ir_instr *prevjump = vec_last(b->instr);
1963 instr->_ops[0]->store = store_global;
1964 if (!ir_block_create_store(b, instr->context, instr->_ops[0], v))
1966 instr->_ops[0]->store = store_value;
1967 vec_push(b->instr, prevjump);
1971 ir_instr_delete(instr);
1976 /***********************************************************************
1977 *IR Temp allocation code
1978 * Propagating value life ranges by walking through the function backwards
1979 * until no more changes are made.
1980 * In theory this should happen once more than once for every nested loop
1982 * Though this implementation might run an additional time for if nests.
1985 /* Enumerate instructions used by value's life-ranges
1987 static void ir_block_enumerate(ir_block *self, size_t *_eid)
1991 for (i = 0; i < vec_size(self->instr); ++i)
1993 self->instr[i]->eid = eid++;
1998 /* Enumerate blocks and instructions.
1999 * The block-enumeration is unordered!
2000 * We do not really use the block enumreation, however
2001 * the instruction enumeration is important for life-ranges.
2003 void ir_function_enumerate(ir_function *self)
2006 size_t instruction_id = 0;
2007 for (i = 0; i < vec_size(self->blocks); ++i)
2009 /* each block now gets an additional "entry" instruction id
2010 * we can use to avoid point-life issues
2012 self->blocks[i]->entry_id = instruction_id;
2015 self->blocks[i]->eid = i;
2016 self->blocks[i]->run_id = 0;
2017 ir_block_enumerate(self->blocks[i], &instruction_id);
2021 static bool ir_block_life_propagate(ir_block *b, ir_block *prev, bool *changed);
2022 bool ir_function_calculate_liferanges(ir_function *self)
2027 /* parameters live at 0 */
2028 for (i = 0; i < vec_size(self->params); ++i)
2029 ir_value_life_merge(self->locals[i], 0);
2034 for (i = 0; i != vec_size(self->blocks); ++i)
2036 if (self->blocks[i]->is_return)
2038 vec_free(self->blocks[i]->living);
2039 if (!ir_block_life_propagate(self->blocks[i], NULL, &changed))
2044 if (vec_size(self->blocks)) {
2045 ir_block *block = self->blocks[0];
2046 for (i = 0; i < vec_size(block->living); ++i) {
2047 ir_value *v = block->living[i];
2048 if (v->store != store_local)
2050 if (v->vtype == TYPE_VECTOR)
2052 self->flags |= IR_FLAG_HAS_UNINITIALIZED;
2053 /* find the instruction reading from it */
2054 for (s = 0; s < vec_size(v->reads); ++s) {
2055 if (v->reads[s]->eid == v->life[0].end)
2058 if (s < vec_size(v->reads)) {
2059 if (irwarning(v->context, WARN_USED_UNINITIALIZED,
2060 "variable `%s` may be used uninitialized in this function\n"
2063 v->reads[s]->context.file, v->reads[s]->context.line)
2071 ir_value *vec = v->memberof;
2072 for (s = 0; s < vec_size(vec->reads); ++s) {
2073 if (vec->reads[s]->eid == v->life[0].end)
2076 if (s < vec_size(vec->reads)) {
2077 if (irwarning(v->context, WARN_USED_UNINITIALIZED,
2078 "variable `%s` may be used uninitialized in this function\n"
2081 vec->reads[s]->context.file, vec->reads[s]->context.line)
2089 if (irwarning(v->context, WARN_USED_UNINITIALIZED,
2090 "variable `%s` may be used uninitialized in this function", v->name))
2099 /* Local-value allocator
2100 * After finishing creating the liferange of all values used in a function
2101 * we can allocate their global-positions.
2102 * This is the counterpart to register-allocation in register machines.
2109 } function_allocator;
2111 static bool function_allocator_alloc(function_allocator *alloc, ir_value *var)
2114 size_t vsize = ir_value_sizeof(var);
2116 var->code.local = vec_size(alloc->locals);
2118 slot = ir_value_var("reg", store_global, var->vtype);
2122 if (!ir_value_life_merge_into(slot, var))
2125 vec_push(alloc->locals, slot);
2126 vec_push(alloc->sizes, vsize);
2127 vec_push(alloc->unique, var->unique_life);
2132 ir_value_delete(slot);
2136 static bool ir_function_allocator_assign(ir_function *self, function_allocator *alloc, ir_value *v)
2142 return function_allocator_alloc(alloc, v);
2144 for (a = 0; a < vec_size(alloc->locals); ++a)
2146 /* if it's reserved for a unique liferange: skip */
2147 if (alloc->unique[a])
2150 slot = alloc->locals[a];
2152 /* never resize parameters
2153 * will be required later when overlapping temps + locals
2155 if (a < vec_size(self->params) &&
2156 alloc->sizes[a] < ir_value_sizeof(v))
2161 if (ir_values_overlap(v, slot))
2164 if (!ir_value_life_merge_into(slot, v))
2167 /* adjust size for this slot */
2168 if (alloc->sizes[a] < ir_value_sizeof(v))
2169 alloc->sizes[a] = ir_value_sizeof(v);
2174 if (a >= vec_size(alloc->locals)) {
2175 if (!function_allocator_alloc(alloc, v))
2181 bool ir_function_allocate_locals(ir_function *self)
2186 bool opt_gt = OPTS_OPTIMIZATION(OPTIM_GLOBAL_TEMPS);
2190 function_allocator lockalloc, globalloc;
2192 if (!vec_size(self->locals) && !vec_size(self->values))
2195 globalloc.locals = NULL;
2196 globalloc.sizes = NULL;
2197 globalloc.positions = NULL;
2198 globalloc.unique = NULL;
2199 lockalloc.locals = NULL;
2200 lockalloc.sizes = NULL;
2201 lockalloc.positions = NULL;
2202 lockalloc.unique = NULL;
2204 for (i = 0; i < vec_size(self->locals); ++i)
2206 v = self->locals[i];
2207 if ((self->flags & IR_FLAG_MASK_NO_LOCAL_TEMPS) || !OPTS_OPTIMIZATION(OPTIM_LOCAL_TEMPS)) {
2209 v->unique_life = true;
2211 else if (i >= vec_size(self->params))
2214 v->locked = true; /* lock parameters locals */
2215 if (!function_allocator_alloc((v->locked || !opt_gt ? &lockalloc : &globalloc), v))
2218 for (; i < vec_size(self->locals); ++i)
2220 v = self->locals[i];
2221 if (!vec_size(v->life))
2223 if (!ir_function_allocator_assign(self, (v->locked || !opt_gt ? &lockalloc : &globalloc), v))
2227 /* Allocate a slot for any value that still exists */
2228 for (i = 0; i < vec_size(self->values); ++i)
2230 v = self->values[i];
2232 if (!vec_size(v->life))
2235 /* CALL optimization:
2236 * If the value is a parameter-temp: 1 write, 1 read from a CALL
2237 * and it's not "locked", write it to the OFS_PARM directly.
2239 if (OPTS_OPTIMIZATION(OPTIM_CALL_STORES) && !v->locked && !v->unique_life) {
2240 if (vec_size(v->reads) == 1 && vec_size(v->writes) == 1 &&
2241 (v->reads[0]->opcode == VINSTR_NRCALL ||
2242 (v->reads[0]->opcode >= INSTR_CALL0 && v->reads[0]->opcode <= INSTR_CALL8)
2247 ir_instr *call = v->reads[0];
2248 if (!vec_ir_value_find(call->params, v, ¶m)) {
2249 irerror(call->context, "internal error: unlocked parameter %s not found", v->name);
2253 ++opts_optimizationcount[OPTIM_CALL_STORES];
2254 v->callparam = true;
2256 ir_value_code_setaddr(v, OFS_PARM0 + 3*param);
2260 if (vec_size(self->owner->extparam_protos) <= param)
2261 ep = ir_gen_extparam_proto(self->owner);
2263 ep = self->owner->extparam_protos[param];
2264 ir_instr_op(v->writes[0], 0, ep, true);
2265 call->params[param+8] = ep;
2269 if (vec_size(v->writes) == 1 && v->writes[0]->opcode == INSTR_CALL0)
2271 v->store = store_return;
2272 if (v->members[0]) v->members[0]->store = store_return;
2273 if (v->members[1]) v->members[1]->store = store_return;
2274 if (v->members[2]) v->members[2]->store = store_return;
2275 ++opts_optimizationcount[OPTIM_CALL_STORES];
2280 if (!ir_function_allocator_assign(self, (v->locked || !opt_gt ? &lockalloc : &globalloc), v))
2284 if (!lockalloc.sizes && !globalloc.sizes) {
2287 vec_push(lockalloc.positions, 0);
2288 vec_push(globalloc.positions, 0);
2290 /* Adjust slot positions based on sizes */
2291 if (lockalloc.sizes) {
2292 pos = (vec_size(lockalloc.sizes) ? lockalloc.positions[0] : 0);
2293 for (i = 1; i < vec_size(lockalloc.sizes); ++i)
2295 pos = lockalloc.positions[i-1] + lockalloc.sizes[i-1];
2296 vec_push(lockalloc.positions, pos);
2298 self->allocated_locals = pos + vec_last(lockalloc.sizes);
2300 if (globalloc.sizes) {
2301 pos = (vec_size(globalloc.sizes) ? globalloc.positions[0] : 0);
2302 for (i = 1; i < vec_size(globalloc.sizes); ++i)
2304 pos = globalloc.positions[i-1] + globalloc.sizes[i-1];
2305 vec_push(globalloc.positions, pos);
2307 self->globaltemps = pos + vec_last(globalloc.sizes);
2310 /* Locals need to know their new position */
2311 for (i = 0; i < vec_size(self->locals); ++i) {
2312 v = self->locals[i];
2313 if (i >= vec_size(self->params) && !vec_size(v->life))
2315 if (v->locked || !opt_gt)
2316 v->code.local = lockalloc.positions[v->code.local];
2318 v->code.local = globalloc.positions[v->code.local];
2320 /* Take over the actual slot positions on values */
2321 for (i = 0; i < vec_size(self->values); ++i) {
2322 v = self->values[i];
2323 if (!vec_size(v->life))
2325 if (v->locked || !opt_gt)
2326 v->code.local = lockalloc.positions[v->code.local];
2328 v->code.local = globalloc.positions[v->code.local];
2336 for (i = 0; i < vec_size(lockalloc.locals); ++i)
2337 ir_value_delete(lockalloc.locals[i]);
2338 for (i = 0; i < vec_size(globalloc.locals); ++i)
2339 ir_value_delete(globalloc.locals[i]);
2340 vec_free(globalloc.unique);
2341 vec_free(globalloc.locals);
2342 vec_free(globalloc.sizes);
2343 vec_free(globalloc.positions);
2344 vec_free(lockalloc.unique);
2345 vec_free(lockalloc.locals);
2346 vec_free(lockalloc.sizes);
2347 vec_free(lockalloc.positions);
2351 /* Get information about which operand
2352 * is read from, or written to.
2354 static void ir_op_read_write(int op, size_t *read, size_t *write)
2374 case INSTR_STOREP_F:
2375 case INSTR_STOREP_V:
2376 case INSTR_STOREP_S:
2377 case INSTR_STOREP_ENT:
2378 case INSTR_STOREP_FLD:
2379 case INSTR_STOREP_FNC:
2390 static bool ir_block_living_add_instr(ir_block *self, size_t eid)
2393 bool changed = false;
2395 for (i = 0; i != vec_size(self->living); ++i)
2397 tempbool = ir_value_life_merge(self->living[i], eid);
2398 changed = changed || tempbool;
2403 static bool ir_block_living_lock(ir_block *self)
2406 bool changed = false;
2407 for (i = 0; i != vec_size(self->living); ++i)
2409 if (!self->living[i]->locked) {
2410 self->living[i]->locked = true;
2417 static bool ir_block_life_prop_previous(ir_block* self, ir_block *prev, bool *changed)
2423 /* values which have been read in a previous iteration are now
2424 * in the "living" array even if the previous block doesn't use them.
2425 * So we have to remove whatever does not exist in the previous block.
2426 * They will be re-added on-read, but the liferange merge won't cause
2428 for (i = 0; i < vec_size(self->living); ++i)
2430 if (!vec_ir_value_find(prev->living, self->living[i], NULL)) {
2431 vec_remove(self->living, i, 1);
2437 /* Whatever the previous block still has in its living set
2438 * must now be added to ours as well.
2440 for (i = 0; i < vec_size(prev->living); ++i)
2442 if (vec_ir_value_find(self->living, prev->living[i], NULL))
2444 vec_push(self->living, prev->living[i]);
2446 irerror(self->contextt from prev: %s", self->label, prev->living[i]->_name);
2452 static bool ir_block_life_propagate(ir_block *self, ir_block *prev, bool *changed)
2457 size_t i, o, p, mem;
2458 /* bitmasks which operands are read from or written to */
2467 if (!ir_block_life_prop_previous(self, prev, changed))
2471 i = vec_size(self->instr);
2474 instr = self->instr[i];
2476 /* See which operands are read and write operands */
2477 ir_op_read_write(instr->opcode, &read, &write);
2479 /* Go through the 3 main operands
2480 * writes first, then reads
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)
2497 /* write operands */
2498 /* When we write to a local, we consider it "dead" for the
2499 * remaining upper part of the function, since in SSA a value
2500 * can only be written once (== created)
2505 bool in_living = vec_ir_value_find(self->living, value, &idx);
2508 /* If the value isn't alive it hasn't been read before... */
2509 /* TODO: See if the warning can be emitted during parsing or AST processing
2510 * otherwise have warning printed here.
2511 * IF printing a warning here: include filecontext_t,
2512 * and make sure it's only printed once
2513 * since this function is run multiple times.
2515 /* con_err( "Value only written %s\n", value->name); */
2516 tempbool = ir_value_life_merge(value, instr->eid);
2517 *changed = *changed || tempbool;
2519 /* since 'living' won't contain it
2520 * anymore, merge the value, since
2523 tempbool = ir_value_life_merge(value, instr->eid);
2524 *changed = *changed || tempbool;
2526 vec_remove(self->living, idx, 1);
2528 /* Removing a vector removes all members */
2529 for (mem = 0; mem < 3; ++mem) {
2530 if (value->members[mem] && vec_ir_value_find(self->living, value->members[mem], &idx)) {
2531 tempbool = ir_value_life_merge(value->members[mem], instr->eid);
2532 *changed = *changed || tempbool;
2533 vec_remove(self->living, idx, 1);
2536 /* Removing the last member removes the vector */
2537 if (value->memberof) {
2538 value = value->memberof;
2539 for (mem = 0; mem < 3; ++mem) {
2540 if (value->members[mem] && vec_ir_value_find(self->living, value->members[mem], NULL))
2543 if (mem == 3 && vec_ir_value_find(self->living, value, &idx)) {
2544 tempbool = ir_value_life_merge(value, instr->eid);
2545 *changed = *changed || tempbool;
2546 vec_remove(self->living, idx, 1);
2552 if (instr->opcode == INSTR_MUL_VF)
2554 value = instr->_ops[2];
2555 /* the float source will get an additional lifetime */
2556 if (ir_value_life_merge(value, instr->eid+1))
2558 if (value->memberof && ir_value_life_merge(value->memberof, instr->eid+1))
2561 else if (instr->opcode == INSTR_MUL_FV)
2563 value = instr->_ops[1];
2564 /* the float source will get an additional lifetime */
2565 if (ir_value_life_merge(value, instr->eid+1))
2567 if (value->memberof && ir_value_life_merge(value->memberof, instr->eid+1))
2571 for (o = 0; o < 3; ++o)
2573 if (!instr->_ops[o]) /* no such operand */
2576 value = instr->_ops[o];
2578 /* We only care about locals */
2579 /* we also calculate parameter liferanges so that locals
2580 * can take up parameter slots */
2581 if (value->store != store_value &&
2582 value->store != store_local &&
2583 value->store != store_param)
2589 if (!vec_ir_value_find(self->living, value, NULL))
2590 vec_push(self->living, value);
2591 /* reading adds the full vector */
2592 if (value->memberof && !vec_ir_value_find(self->living, value->memberof, NULL))
2593 vec_push(self->living, value->memberof);
2594 for (mem = 0; mem < 3; ++mem) {
2595 if (value->members[mem] && !vec_ir_value_find(self->living, value->members[mem], NULL))
2596 vec_push(self->living, value->members[mem]);
2600 /* PHI operands are always read operands */
2601 for (p = 0; p < vec_size(instr->phi); ++p)
2603 value = instr->phi[p].value;
2604 if (!vec_ir_value_find(self->living, value, NULL))
2605 vec_push(self->living, value);
2606 /* reading adds the full vector */
2607 if (value->memberof && !vec_ir_value_find(self->living, value->memberof, NULL))
2608 vec_push(self->living, value->memberof);
2609 for (mem = 0; mem < 3; ++mem) {
2610 if (value->members[mem] && !vec_ir_value_find(self->living, value->members[mem], NULL))
2611 vec_push(self->living, value->members[mem]);
2615 /* on a call, all these values must be "locked" */
2616 if (instr->opcode >= INSTR_CALL0 && instr->opcode <= INSTR_CALL8) {
2617 if (ir_block_living_lock(self))
2620 /* call params are read operands too */
2621 for (p = 0; p < vec_size(instr->params); ++p)
2623 value = instr->params[p];
2624 if (!vec_ir_value_find(self->living, value, NULL))
2625 vec_push(self->living, value);
2626 /* reading adds the full vector */
2627 if (value->memberof && !vec_ir_value_find(self->living, value->memberof, NULL))
2628 vec_push(self->living, value->memberof);
2629 for (mem = 0; mem < 3; ++mem) {
2630 if (value->members[mem] && !vec_ir_value_find(self->living, value->members[mem], NULL))
2631 vec_push(self->living, value->members[mem]);
2636 tempbool = ir_block_living_add_instr(self, instr->eid);
2637 /*con_err( "living added values\n");*/
2638 *changed = *changed || tempbool;
2640 /* the "entry" instruction ID */
2641 tempbool = ir_block_living_add_instr(self, self->entry_id);
2642 *changed = *changed || tempbool;
2644 if (self->run_id == self->owner->run_id)
2647 self->run_id = self->owner->run_id;
2649 for (i = 0; i < vec_size(self->entries); ++i)
2651 ir_block *entry = self->entries[i];
2652 ir_block_life_propagate(entry, self, changed);
2658 /***********************************************************************
2661 * Since the IR has the convention of putting 'write' operands
2662 * at the beginning, we have to rotate the operands of instructions
2663 * properly in order to generate valid QCVM code.
2665 * Having destinations at a fixed position is more convenient. In QC
2666 * this is *mostly* OPC, but FTE adds at least 2 instructions which
2667 * read from from OPA, and store to OPB rather than OPC. Which is
2668 * partially the reason why the implementation of these instructions
2669 * in darkplaces has been delayed for so long.
2671 * Breaking conventions is annoying...
2673 static bool ir_builder_gen_global(ir_builder *self, ir_value *global, bool islocal);
2675 static bool gen_global_field(ir_value *global)
2677 if (global->hasvalue)
2679 ir_value *fld = global->constval.vpointer;
2681 irerror(global->context, "Invalid field constant with no field: %s", global->name);
2685 /* copy the field's value */
2686 ir_value_code_setaddr(global, vec_size(code_globals));
2687 vec_push(code_globals, fld->code.fieldaddr);
2688 if (global->fieldtype == TYPE_VECTOR) {
2689 vec_push(code_globals, fld->code.fieldaddr+1);
2690 vec_push(code_globals, fld->code.fieldaddr+2);
2695 ir_value_code_setaddr(global, vec_size(code_globals));
2696 vec_push(code_globals, 0);
2697 if (global->fieldtype == TYPE_VECTOR) {
2698 vec_push(code_globals, 0);
2699 vec_push(code_globals, 0);
2702 if (global->code.globaladdr < 0)
2707 static bool gen_global_pointer(ir_value *global)
2709 if (global->hasvalue)
2711 ir_value *target = global->constval.vpointer;
2713 irerror(global->context, "Invalid pointer constant: %s", global->name);
2714 /* NULL pointers are pointing to the NULL constant, which also
2715 * sits at address 0, but still has an ir_value for itself.
2720 /* Here, relocations ARE possible - in fteqcc-enhanced-qc:
2721 * void() foo; <- proto
2722 * void() *fooptr = &foo;
2723 * void() foo = { code }
2725 if (!target->code.globaladdr) {
2726 /* FIXME: Check for the constant nullptr ir_value!
2727 * because then code.globaladdr being 0 is valid.
2729 irerror(global->context, "FIXME: Relocation support");
2733 ir_value_code_setaddr(global, vec_size(code_globals));
2734 vec_push(code_globals, target->code.globaladdr);
2738 ir_value_code_setaddr(global, vec_size(code_globals));
2739 vec_push(code_globals, 0);
2741 if (global->code.globaladdr < 0)
2746 static bool gen_blocks_recursive(ir_function *func, ir_block *block)
2748 prog_section_statement stmt;
2756 block->generated = true;
2757 block->code_start = vec_size(code_statements);
2758 for (i = 0; i < vec_size(block->instr); ++i)
2760 instr = block->instr[i];
2762 if (instr->opcode == VINSTR_PHI) {
2763 irerror(block->context, "cannot generate virtual instruction (phi)");
2767 if (instr->opcode == VINSTR_JUMP) {
2768 target = instr->bops[0];
2769 /* for uncoditional jumps, if the target hasn't been generated
2770 * yet, we generate them right here.
2772 if (!target->generated)
2773 return gen_blocks_recursive(func, target);
2775 /* otherwise we generate a jump instruction */
2776 stmt.opcode = INSTR_GOTO;
2777 stmt.o1.s1 = (target->code_start) - vec_size(code_statements);
2780 if (stmt.o1.s1 != 1)
2781 code_push_statement(&stmt, instr->context.line);
2783 /* no further instructions can be in this block */
2787 if (instr->opcode == VINSTR_COND) {
2788 ontrue = instr->bops[0];
2789 onfalse = instr->bops[1];
2790 /* TODO: have the AST signal which block should
2791 * come first: eg. optimize IFs without ELSE...
2794 stmt.o1.u1 = ir_value_code_addr(instr->_ops[0]);
2798 if (ontrue->generated) {
2799 stmt.opcode = INSTR_IF;
2800 stmt.o2.s1 = (ontrue->code_start) - vec_size(code_statements);
2801 if (stmt.o2.s1 != 1)
2802 code_push_statement(&stmt, instr->context.line);
2804 if (onfalse->generated) {
2805 stmt.opcode = INSTR_IFNOT;
2806 stmt.o2.s1 = (onfalse->code_start) - vec_size(code_statements);
2807 if (stmt.o2.s1 != 1)
2808 code_push_statement(&stmt, instr->context.line);
2810 if (!ontrue->generated) {
2811 if (onfalse->generated)
2812 return gen_blocks_recursive(func, ontrue);
2814 if (!onfalse->generated) {
2815 if (ontrue->generated)
2816 return gen_blocks_recursive(func, onfalse);
2818 /* neither ontrue nor onfalse exist */
2819 stmt.opcode = INSTR_IFNOT;
2820 if (!instr->likely) {
2821 /* Honor the likelyhood hint */
2822 ir_block *tmp = onfalse;
2823 stmt.opcode = INSTR_IF;
2827 stidx = vec_size(code_statements);
2828 code_push_statement(&stmt, instr->context.line);
2829 /* on false we jump, so add ontrue-path */
2830 if (!gen_blocks_recursive(func, ontrue))
2832 /* fixup the jump address */
2833 code_statements[stidx].o2.s1 = vec_size(code_statements) - stidx;
2834 /* generate onfalse path */
2835 if (onfalse->generated) {
2836 /* fixup the jump address */
2837 code_statements[stidx].o2.s1 = (onfalse->code_start) - (stidx);
2838 if (stidx+2 == vec_size(code_statements) && code_statements[stidx].o2.s1 == 1) {
2839 code_statements[stidx] = code_statements[stidx+1];
2840 if (code_statements[stidx].o1.s1 < 0)
2841 code_statements[stidx].o1.s1++;
2842 code_pop_statement();
2844 stmt.opcode = vec_last(code_statements).opcode;
2845 if (stmt.opcode == INSTR_GOTO ||
2846 stmt.opcode == INSTR_IF ||
2847 stmt.opcode == INSTR_IFNOT ||
2848 stmt.opcode == INSTR_RETURN ||
2849 stmt.opcode == INSTR_DONE)
2851 /* no use jumping from here */
2854 /* may have been generated in the previous recursive call */
2855 stmt.opcode = INSTR_GOTO;
2856 stmt.o1.s1 = (onfalse->code_start) - vec_size(code_statements);
2859 if (stmt.o1.s1 != 1)
2860 code_push_statement(&stmt, instr->context.line);
2863 else if (stidx+2 == vec_size(code_statements) && code_statements[stidx].o2.s1 == 1) {
2864 code_statements[stidx] = code_statements[stidx+1];
2865 if (code_statements[stidx].o1.s1 < 0)
2866 code_statements[stidx].o1.s1++;
2867 code_pop_statement();
2869 /* if not, generate now */
2870 return gen_blocks_recursive(func, onfalse);
2873 if ( (instr->opcode >= INSTR_CALL0 && instr->opcode <= INSTR_CALL8)
2874 || instr->opcode == VINSTR_NRCALL)
2879 first = vec_size(instr->params);
2882 for (p = 0; p < first; ++p)
2884 ir_value *param = instr->params[p];
2885 if (param->callparam)
2888 stmt.opcode = INSTR_STORE_F;
2891 if (param->vtype == TYPE_FIELD)
2892 stmt.opcode = field_store_instr[param->fieldtype];
2893 else if (param->vtype == TYPE_NIL)
2894 stmt.opcode = INSTR_STORE_V;
2896 stmt.opcode = type_store_instr[param->vtype];
2897 stmt.o1.u1 = ir_value_code_addr(param);
2898 stmt.o2.u1 = OFS_PARM0 + 3 * p;
2899 code_push_statement(&stmt, instr->context.line);
2901 /* Now handle extparams */
2902 first = vec_size(instr->params);
2903 for (; p < first; ++p)
2905 ir_builder *ir = func->owner;
2906 ir_value *param = instr->params[p];
2907 ir_value *targetparam;
2909 if (param->callparam)
2912 if (p-8 >= vec_size(ir->extparams))
2913 ir_gen_extparam(ir);
2915 targetparam = ir->extparams[p-8];
2917 stmt.opcode = INSTR_STORE_F;
2920 if (param->vtype == TYPE_FIELD)
2921 stmt.opcode = field_store_instr[param->fieldtype];
2922 else if (param->vtype == TYPE_NIL)
2923 stmt.opcode = INSTR_STORE_V;
2925 stmt.opcode = type_store_instr[param->vtype];
2926 stmt.o1.u1 = ir_value_code_addr(param);
2927 stmt.o2.u1 = ir_value_code_addr(targetparam);
2928 code_push_statement(&stmt, instr->context.line);
2931 stmt.opcode = INSTR_CALL0 + vec_size(instr->params);
2932 if (stmt.opcode > INSTR_CALL8)
2933 stmt.opcode = INSTR_CALL8;
2934 stmt.o1.u1 = ir_value_code_addr(instr->_ops[1]);
2937 code_push_statement(&stmt, instr->context.line);
2939 retvalue = instr->_ops[0];
2940 if (retvalue && retvalue->store != store_return &&
2941 (retvalue->store == store_global || vec_size(retvalue->life)))
2943 /* not to be kept in OFS_RETURN */
2944 if (retvalue->vtype == TYPE_FIELD && OPTS_FLAG(ADJUST_VECTOR_FIELDS))
2945 stmt.opcode = field_store_instr[retvalue->fieldtype];
2947 stmt.opcode = type_store_instr[retvalue->vtype];
2948 stmt.o1.u1 = OFS_RETURN;
2949 stmt.o2.u1 = ir_value_code_addr(retvalue);
2951 code_push_statement(&stmt, instr->context.line);
2956 if (instr->opcode == INSTR_STATE) {
2957 irerror(block->context, "TODO: state instruction");
2961 stmt.opcode = instr->opcode;
2966 /* This is the general order of operands */
2968 stmt.o3.u1 = ir_value_code_addr(instr->_ops[0]);
2971 stmt.o1.u1 = ir_value_code_addr(instr->_ops[1]);
2974 stmt.o2.u1 = ir_value_code_addr(instr->_ops[2]);
2976 if (stmt.opcode == INSTR_RETURN || stmt.opcode == INSTR_DONE)
2978 stmt.o1.u1 = stmt.o3.u1;
2981 else if ((stmt.opcode >= INSTR_STORE_F &&
2982 stmt.opcode <= INSTR_STORE_FNC) ||
2983 (stmt.opcode >= INSTR_STOREP_F &&
2984 stmt.opcode <= INSTR_STOREP_FNC))
2986 /* 2-operand instructions with A -> B */
2987 stmt.o2.u1 = stmt.o3.u1;
2990 /* tiny optimization, don't output
2993 if (stmt.o2.u1 == stmt.o1.u1 &&
2994 OPTS_OPTIMIZATION(OPTIM_PEEPHOLE))
2996 ++opts_optimizationcount[OPTIM_PEEPHOLE];
3001 code_push_statement(&stmt, instr->context.line);
3006 static bool gen_function_code(ir_function *self)
3009 prog_section_statement stmt, *retst;
3011 /* Starting from entry point, we generate blocks "as they come"
3012 * for now. Dead blocks will not be translated obviously.
3014 if (!vec_size(self->blocks)) {
3015 irerror(self->context, "Function '%s' declared without body.", self->name);
3019 block = self->blocks[0];
3020 if (block->generated)
3023 if (!gen_blocks_recursive(self, block)) {
3024 irerror(self->context, "failed to generate blocks for '%s'", self->name);
3028 /* code_write and qcvm -disasm need to know that the function ends here */
3029 retst = &vec_last(code_statements);
3030 if (OPTS_OPTIMIZATION(OPTIM_VOID_RETURN) &&
3031 self->outtype == TYPE_VOID &&
3032 retst->opcode == INSTR_RETURN &&
3033 !retst->o1.u1 && !retst->o2.u1 && !retst->o3.u1)
3035 retst->opcode = INSTR_DONE;
3036 ++opts_optimizationcount[OPTIM_VOID_RETURN];
3038 stmt.opcode = INSTR_DONE;
3042 code_push_statement(&stmt, vec_last(code_linenums));
3047 static qcint ir_builder_filestring(ir_builder *ir, const char *filename)
3049 /* NOTE: filename pointers are copied, we never strdup them,
3050 * thus we can use pointer-comparison to find the string.
3055 for (i = 0; i < vec_size(ir->filenames); ++i) {
3056 if (ir->filenames[i] == filename)
3057 return ir->filestrings[i];
3060 str = code_genstring(filename);
3061 vec_push(ir->filenames, filename);
3062 vec_push(ir->filestrings, str);
3066 static bool gen_global_function(ir_builder *ir, ir_value *global)
3068 prog_section_function fun;
3073 if (!global->hasvalue || (!global->constval.vfunc))
3075 irerror(global->context, "Invalid state of function-global: not constant: %s", global->name);
3079 irfun = global->constval.vfunc;
3081 fun.name = global->code.name;
3082 fun.file = ir_builder_filestring(ir, global->context.file);
3083 fun.profile = 0; /* always 0 */
3084 fun.nargs = vec_size(irfun->params);
3088 for (i = 0;i < 8; ++i) {
3089 if ((int32_t)i >= fun.nargs)
3092 fun.argsize[i] = type_sizeof_[irfun->params[i]];
3096 fun.locals = irfun->allocated_locals;
3099 fun.entry = irfun->builtin+1;
3101 irfun->code_function_def = vec_size(code_functions);
3102 fun.entry = vec_size(code_statements);
3105 vec_push(code_functions, fun);
3109 static ir_value* ir_gen_extparam_proto(ir_builder *ir)
3114 snprintf(name, sizeof(name), "EXTPARM#%i", (int)(vec_size(ir->extparam_protos)+8));
3115 global = ir_value_var(name, store_global, TYPE_VECTOR);
3117 vec_push(ir->extparam_protos, global);
3121 static void ir_gen_extparam(ir_builder *ir)
3123 prog_section_def def;
3126 if (vec_size(ir->extparam_protos) < vec_size(ir->extparams)+1)
3127 global = ir_gen_extparam_proto(ir);
3129 global = ir->extparam_protos[vec_size(ir->extparams)];
3131 def.name = code_genstring(global->name);
3132 def.type = TYPE_VECTOR;
3133 def.offset = vec_size(code_globals);
3135 vec_push(code_defs, def);
3136 ir_value_code_setaddr(global, def.offset);
3137 vec_push(code_globals, 0);
3138 vec_push(code_globals, 0);
3139 vec_push(code_globals, 0);
3141 vec_push(ir->extparams, global);
3144 static bool gen_function_extparam_copy(ir_function *self)
3146 size_t i, ext, numparams;
3148 ir_builder *ir = self->owner;
3150 prog_section_statement stmt;
3152 numparams = vec_size(self->params);
3156 stmt.opcode = INSTR_STORE_F;
3158 for (i = 8; i < numparams; ++i) {
3160 if (ext >= vec_size(ir->extparams))
3161 ir_gen_extparam(ir);
3163 ep = ir->extparams[ext];
3165 stmt.opcode = type_store_instr[self->locals[i]->vtype];
3166 if (self->locals[i]->vtype == TYPE_FIELD &&
3167 self->locals[i]->fieldtype == TYPE_VECTOR)
3169 stmt.opcode = INSTR_STORE_V;
3171 stmt.o1.u1 = ir_value_code_addr(ep);
3172 stmt.o2.u1 = ir_value_code_addr(self->locals[i]);
3173 code_push_statement(&stmt, self->context.line);
3179 static bool gen_function_varargs_copy(ir_function *self)
3181 size_t i, ext, numparams, maxparams;
3183 ir_builder *ir = self->owner;
3185 prog_section_statement stmt;
3187 numparams = vec_size(self->params);
3191 stmt.opcode = INSTR_STORE_V;
3193 maxparams = numparams + self->max_varargs;
3194 for (i = numparams; i < maxparams; ++i) {
3196 stmt.o1.u1 = OFS_PARM0 + 3*i;
3197 stmt.o2.u1 = ir_value_code_addr(self->locals[i]);
3198 code_push_statement(&stmt, self->context.line);
3202 if (ext >= vec_size(ir->extparams))
3203 ir_gen_extparam(ir);
3205 ep = ir->extparams[ext];
3207 stmt.o1.u1 = ir_value_code_addr(ep);
3208 stmt.o2.u1 = ir_value_code_addr(self->locals[i]);
3209 code_push_statement(&stmt, self->context.line);
3215 static bool gen_function_locals(ir_builder *ir, ir_value *global)
3217 prog_section_function *def;
3220 uint32_t firstlocal, firstglobal;
3222 irfun = global->constval.vfunc;
3223 def = code_functions + irfun->code_function_def;
3225 if (opts.g || !OPTS_OPTIMIZATION(OPTIM_OVERLAP_LOCALS) || (irfun->flags & IR_FLAG_MASK_NO_OVERLAP))
3226 firstlocal = def->firstlocal = vec_size(code_globals);
3228 firstlocal = def->firstlocal = ir->first_common_local;
3229 ++opts_optimizationcount[OPTIM_OVERLAP_LOCALS];
3232 firstglobal = (OPTS_OPTIMIZATION(OPTIM_GLOBAL_TEMPS) ? ir->first_common_globaltemp : firstlocal);
3234 for (i = vec_size(code_globals); i < firstlocal + irfun->allocated_locals; ++i)
3235 vec_push(code_globals, 0);
3236 for (i = 0; i < vec_size(irfun->locals); ++i) {
3237 ir_value *v = irfun->locals[i];
3238 if (v->locked || !OPTS_OPTIMIZATION(OPTIM_GLOBAL_TEMPS)) {
3239 ir_value_code_setaddr(v, firstlocal + v->code.local);
3240 if (!ir_builder_gen_global(ir, irfun->locals[i], true)) {
3241 irerror(irfun->locals[i]->context, "failed to generate local %s", irfun->locals[i]->name);
3246 ir_value_code_setaddr(v, firstglobal + v->code.local);
3248 for (i = 0; i < vec_size(irfun->values); ++i)
3250 ir_value *v = irfun->values[i];
3254 ir_value_code_setaddr(v, firstlocal + v->code.local);
3256 ir_value_code_setaddr(v, firstglobal + v->code.local);
3261 static bool gen_global_function_code(ir_builder *ir, ir_value *global)
3263 prog_section_function *fundef;
3268 irfun = global->constval.vfunc;
3270 if (global->cvq == CV_NONE) {
3271 irwarning(global->context, WARN_IMPLICIT_FUNCTION_POINTER,
3272 "function `%s` has no body and in QC implicitly becomes a function-pointer", global->name);
3274 /* this was a function pointer, don't generate code for those */
3281 if (irfun->code_function_def < 0) {
3282 irerror(irfun->context, "`%s`: IR global wasn't generated, failed to access function-def", irfun->name);
3285 fundef = &code_functions[irfun->code_function_def];
3287 fundef->entry = vec_size(code_statements);
3288 if (!gen_function_locals(ir, global)) {
3289 irerror(irfun->context, "Failed to generate locals for function %s", irfun->name);
3292 if (!gen_function_extparam_copy(irfun)) {
3293 irerror(irfun->context, "Failed to generate extparam-copy code for function %s", irfun->name);
3296 if (irfun->max_varargs && !gen_function_varargs_copy(irfun)) {
3297 irerror(irfun->context, "Failed to generate vararg-copy code for function %s", irfun->name);
3300 if (!gen_function_code(irfun)) {
3301 irerror(irfun->context, "Failed to generate code for function %s", irfun->name);
3307 static void gen_vector_defs(prog_section_def def, const char *name)
3312 if (!name || name[0] == '#' || OPTS_FLAG(SINGLE_VECTOR_DEFS))
3315 def.type = TYPE_FLOAT;
3319 component = (char*)mem_a(len+3);
3320 memcpy(component, name, len);
3322 component[len-0] = 0;
3323 component[len-2] = '_';
3325 component[len-1] = 'x';
3327 for (i = 0; i < 3; ++i) {
3328 def.name = code_genstring(component);
3329 vec_push(code_defs, def);
3335 static void gen_vector_fields(prog_section_field fld, const char *name)
3340 if (!name || OPTS_FLAG(SINGLE_VECTOR_DEFS))
3343 fld.type = TYPE_FLOAT;
3347 component = (char*)mem_a(len+3);
3348 memcpy(component, name, len);
3350 component[len-0] = 0;
3351 component[len-2] = '_';
3353 component[len-1] = 'x';
3355 for (i = 0; i < 3; ++i) {
3356 fld.name = code_genstring(component);
3357 vec_push(code_fields, fld);
3363 static bool ir_builder_gen_global(ir_builder *self, ir_value *global, bool islocal)
3367 prog_section_def def;
3368 bool pushdef = false;
3370 def.type = global->vtype;
3371 def.offset = vec_size(code_globals);
3373 if (opts.g || !islocal)
3377 if (OPTS_OPTIMIZATION(OPTIM_STRIP_CONSTANT_NAMES) &&
3378 !(global->flags & IR_FLAG_INCLUDE_DEF) &&
3379 (global->name[0] == '#' || global->cvq == CV_CONST))
3384 if (pushdef && global->name) {
3385 if (global->name[0] == '#') {
3386 if (!self->str_immediate)
3387 self->str_immediate = code_genstring("IMMEDIATE");
3388 def.name = global->code.name = self->str_immediate;
3391 def.name = global->code.name = code_genstring(global->name);
3396 def.offset = ir_value_code_addr(global);
3397 vec_push(code_defs, def);
3398 if (global->vtype == TYPE_VECTOR)
3399 gen_vector_defs(def, global->name);
3400 else if (global->vtype == TYPE_FIELD && global->fieldtype == TYPE_VECTOR)
3401 gen_vector_defs(def, global->name);
3408 switch (global->vtype)
3411 if (!strcmp(global->name, "end_sys_globals")) {
3412 /* TODO: remember this point... all the defs before this one
3413 * should be checksummed and added to progdefs.h when we generate it.
3416 else if (!strcmp(global->name, "end_sys_fields")) {
3417 /* TODO: same as above but for entity-fields rather than globsl
3421 irwarning(global->context, WARN_VOID_VARIABLES, "unrecognized variable of type void `%s`",
3423 /* I'd argue setting it to 0 is sufficient, but maybe some depend on knowing how far
3424 * the system fields actually go? Though the engine knows this anyway...
3425 * Maybe this could be an -foption
3426 * fteqcc creates data for end_sys_* - of size 1, so let's do the same
3428 ir_value_code_setaddr(global, vec_size(code_globals));
3429 vec_push(code_globals, 0);
3431 if (pushdef) vec_push(code_defs, def);
3434 if (pushdef) vec_push(code_defs, def);
3435 return gen_global_pointer(global);
3438 vec_push(code_defs, def);
3439 if (global->fieldtype == TYPE_VECTOR)
3440 gen_vector_defs(def, global->name);
3442 return gen_global_field(global);
3447 ir_value_code_setaddr(global, vec_size(code_globals));
3448 if (global->hasvalue) {
3449 iptr = (int32_t*)&global->constval.ivec[0];
3450 vec_push(code_globals, *iptr);
3452 vec_push(code_globals, 0);
3454 if (!islocal && global->cvq != CV_CONST)
3455 def.type |= DEF_SAVEGLOBAL;
3456 if (pushdef) vec_push(code_defs, def);
3458 return global->code.globaladdr >= 0;
3462 ir_value_code_setaddr(global, vec_size(code_globals));
3463 if (global->hasvalue) {
3464 vec_push(code_globals, code_genstring(global->constval.vstring));
3466 vec_push(code_globals, 0);
3468 if (!islocal && global->cvq != CV_CONST)
3469 def.type |= DEF_SAVEGLOBAL;
3470 if (pushdef) vec_push(code_defs, def);
3471 return global->code.globaladdr >= 0;
3476 ir_value_code_setaddr(global, vec_size(code_globals));
3477 if (global->hasvalue) {
3478 iptr = (int32_t*)&global->constval.ivec[0];
3479 vec_push(code_globals, iptr[0]);
3480 if (global->code.globaladdr < 0)
3482 for (d = 1; d < type_sizeof_[global->vtype]; ++d) {
3483 vec_push(code_globals, iptr[d]);
3486 vec_push(code_globals, 0);
3487 if (global->code.globaladdr < 0)
3489 for (d = 1; d < type_sizeof_[global->vtype]; ++d) {
3490 vec_push(code_globals, 0);
3493 if (!islocal && global->cvq != CV_CONST)
3494 def.type |= DEF_SAVEGLOBAL;
3497 vec_push(code_defs, def);
3498 def.type &= ~DEF_SAVEGLOBAL;
3499 gen_vector_defs(def, global->name);
3501 return global->code.globaladdr >= 0;
3504 ir_value_code_setaddr(global, vec_size(code_globals));
3505 if (!global->hasvalue) {
3506 vec_push(code_globals, 0);
3507 if (global->code.globaladdr < 0)
3510 vec_push(code_globals, vec_size(code_functions));
3511 if (!gen_global_function(self, global))
3514 if (!islocal && global->cvq != CV_CONST)
3515 def.type |= DEF_SAVEGLOBAL;
3516 if (pushdef) vec_push(code_defs, def);
3519 /* assume biggest type */
3520 ir_value_code_setaddr(global, vec_size(code_globals));
3521 vec_push(code_globals, 0);
3522 for (i = 1; i < type_sizeof_[TYPE_VARIANT]; ++i)
3523 vec_push(code_globals, 0);
3526 /* refuse to create 'void' type or any other fancy business. */
3527 irerror(global->context, "Invalid type for global variable `%s`: %s",
3528 global->name, type_name[global->vtype]);
3533 static void ir_builder_prepare_field(ir_value *field)
3535 field->code.fieldaddr = code_alloc_field(type_sizeof_[field->fieldtype]);
3538 static bool ir_builder_gen_field(ir_builder *self, ir_value *field)
3540 prog_section_def def;
3541 prog_section_field fld;
3545 def.type = (uint16_t)field->vtype;
3546 def.offset = (uint16_t)vec_size(code_globals);
3548 /* create a global named the same as the field */
3549 if (opts.standard == COMPILER_GMQCC) {
3550 /* in our standard, the global gets a dot prefix */
3551 size_t len = strlen(field->name);
3554 /* we really don't want to have to allocate this, and 1024
3555 * bytes is more than enough for a variable/field name
3557 if (len+2 >= sizeof(name)) {
3558 irerror(field->context, "invalid field name size: %u", (unsigned int)len);
3563 memcpy(name+1, field->name, len); /* no strncpy - we used strlen above */
3566 def.name = code_genstring(name);
3567 fld.name = def.name + 1; /* we reuse that string table entry */
3569 /* in plain QC, there cannot be a global with the same name,
3570 * and so we also name the global the same.
3571 * FIXME: fteqcc should create a global as well
3572 * check if it actually uses the same name. Probably does
3574 def.name = code_genstring(field->name);
3575 fld.name = def.name;
3578 field->code.name = def.name;
3580 vec_push(code_defs, def);
3582 fld.type = field->fieldtype;
3584 if (fld.type == TYPE_VOID) {
3585 irerror(field->context, "field is missing a type: %s - don't know its size", field->name);
3589 fld.offset = field->code.fieldaddr;
3591 vec_push(code_fields, fld);
3593 ir_value_code_setaddr(field, vec_size(code_globals));
3594 vec_push(code_globals, fld.offset);
3595 if (fld.type == TYPE_VECTOR) {
3596 vec_push(code_globals, fld.offset+1);
3597 vec_push(code_globals, fld.offset+2);
3600 if (field->fieldtype == TYPE_VECTOR) {
3601 gen_vector_defs(def, field->name);
3602 gen_vector_fields(fld, field->name);
3605 return field->code.globaladdr >= 0;
3608 bool ir_builder_generate(ir_builder *self, const char *filename)
3610 prog_section_statement stmt;
3612 char *lnofile = NULL;
3616 for (i = 0; i < vec_size(self->fields); ++i)
3618 ir_builder_prepare_field(self->fields[i]);
3621 for (i = 0; i < vec_size(self->globals); ++i)
3623 if (!ir_builder_gen_global(self, self->globals[i], false)) {
3626 if (self->globals[i]->vtype == TYPE_FUNCTION) {
3627 ir_function *func = self->globals[i]->constval.vfunc;
3628 if (func && self->max_locals < func->allocated_locals &&
3629 !(func->flags & IR_FLAG_MASK_NO_OVERLAP))
3631 self->max_locals = func->allocated_locals;
3633 if (func && self->max_globaltemps < func->globaltemps)
3634 self->max_globaltemps = func->globaltemps;
3638 for (i = 0; i < vec_size(self->fields); ++i)
3640 if (!ir_builder_gen_field(self, self->fields[i])) {
3646 ir_value_code_setaddr(self->nil, vec_size(code_globals));
3647 vec_push(code_globals, 0);
3648 vec_push(code_globals, 0);
3649 vec_push(code_globals, 0);
3651 /* generate global temps */
3652 self->first_common_globaltemp = vec_size(code_globals);
3653 for (i = 0; i < self->max_globaltemps; ++i) {
3654 vec_push(code_globals, 0);
3656 /* generate common locals */
3657 self->first_common_local = vec_size(code_globals);
3658 for (i = 0; i < self->max_locals; ++i) {
3659 vec_push(code_globals, 0);
3662 /* generate function code */
3663 for (i = 0; i < vec_size(self->globals); ++i)
3665 if (self->globals[i]->vtype == TYPE_FUNCTION) {
3666 if (!gen_global_function_code(self, self->globals[i])) {
3672 if (vec_size(code_globals) >= 65536) {
3673 irerror(vec_last(self->globals)->context, "This progs file would require more globals than the metadata can handle. Bailing out.");
3677 /* DP errors if the last instruction is not an INSTR_DONE. */
3678 if (vec_last(code_statements).opcode != INSTR_DONE)
3680 stmt.opcode = INSTR_DONE;
3684 code_push_statement(&stmt, vec_last(code_linenums));
3690 if (vec_size(code_statements) != vec_size(code_linenums)) {
3691 con_err("Linecounter wrong: %lu != %lu\n",
3692 (unsigned long)vec_size(code_statements),
3693 (unsigned long)vec_size(code_linenums));
3694 } else if (OPTS_FLAG(LNO)) {
3696 size_t filelen = strlen(filename);
3698 memcpy(vec_add(lnofile, filelen+1), filename, filelen+1);
3699 dot = strrchr(lnofile, '.');
3703 vec_shrinkto(lnofile, dot - lnofile);
3705 memcpy(vec_add(lnofile, 5), ".lno", 5);
3710 con_out("writing '%s' and '%s'...\n", filename, lnofile);
3712 con_out("writing '%s'\n", filename);
3714 if (!code_write(filename, lnofile)) {
3722 /***********************************************************************
3723 *IR DEBUG Dump functions...
3726 #define IND_BUFSZ 1024
3729 # define strncat(dst, src, sz) strncat_s(dst, sz, src, _TRUNCATE)
3732 const char *qc_opname(int op)
3734 if (op < 0) return "<INVALID>";
3735 if (op < (int)( sizeof(asm_instr) / sizeof(asm_instr[0]) ))
3736 return asm_instr[op].m;
3738 case VINSTR_PHI: return "PHI";
3739 case VINSTR_JUMP: return "JUMP";
3740 case VINSTR_COND: return "COND";
3741 default: return "<UNK>";
3745 void ir_builder_dump(ir_builder *b, int (*oprintf)(const char*, ...))
3748 char indent[IND_BUFSZ];
3752 oprintf("module %s\n", b->name);
3753 for (i = 0; i < vec_size(b->globals); ++i)
3756 if (b->globals[i]->hasvalue)
3757 oprintf("%s = ", b->globals[i]->name);
3758 ir_value_dump(b->globals[i], oprintf);
3761 for (i = 0; i < vec_size(b->functions); ++i)
3762 ir_function_dump(b->functions[i], indent, oprintf);
3763 oprintf("endmodule %s\n", b->name);
3766 static const char *storenames[] = {
3767 "[global]", "[local]", "[param]", "[value]", "[return]"
3770 void ir_function_dump(ir_function *f, char *ind,
3771 int (*oprintf)(const char*, ...))
3774 if (f->builtin != 0) {
3775 oprintf("%sfunction %s = builtin %i\n", ind, f->name, -f->builtin);
3778 oprintf("%sfunction %s\n", ind, f->name);
3779 strncat(ind, "\t", IND_BUFSZ);
3780 if (vec_size(f->locals))
3782 oprintf("%s%i locals:\n", ind, (int)vec_size(f->locals));
3783 for (i = 0; i < vec_size(f->locals); ++i) {
3784 oprintf("%s\t", ind);
3785 ir_value_dump(f->locals[i], oprintf);
3789 oprintf("%sliferanges:\n", ind);
3790 for (i = 0; i < vec_size(f->locals); ++i) {
3791 const char *attr = "";
3793 ir_value *v = f->locals[i];
3794 if (v->unique_life && v->locked)
3795 attr = "unique,locked ";
3796 else if (v->unique_life)
3800 oprintf("%s\t%s: %s %s %s%s@%i ", ind, v->name, type_name[v->vtype],
3801 storenames[v->store],
3802 attr, (v->callparam ? "callparam " : ""),
3803 (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 oprintf("%s\t%s: @%i ", ind, vm->name, (int)vm->code.local);
3815 for (l = 0; l < vec_size(vm->life); ++l) {
3816 oprintf("[%i,%i] ", vm->life[l].start, vm->life[l].end);
3821 for (i = 0; i < vec_size(f->values); ++i) {
3822 const char *attr = "";
3824 ir_value *v = f->values[i];
3825 if (v->unique_life && v->locked)
3826 attr = "unique,locked ";
3827 else if (v->unique_life)
3831 oprintf("%s\t%s: %s %s %s%s@%i ", ind, v->name, type_name[v->vtype],
3832 storenames[v->store],
3833 attr, (v->callparam ? "callparam " : ""),
3834 (int)v->code.local);
3837 for (l = 0; l < vec_size(v->life); ++l) {
3838 oprintf("[%i,%i] ", v->life[l].start, v->life[l].end);
3841 for (m = 0; m < 3; ++m) {
3842 ir_value *vm = v->members[m];
3845 if (vm->unique_life && vm->locked)
3846 attr = "unique,locked ";
3847 else if (vm->unique_life)
3849 else if (vm->locked)
3851 oprintf("%s\t%s: %s@%i ", ind, vm->name, attr, (int)vm->code.local);
3852 for (l = 0; l < vec_size(vm->life); ++l) {
3853 oprintf("[%i,%i] ", vm->life[l].start, vm->life[l].end);
3858 if (vec_size(f->blocks))
3860 oprintf("%slife passes (check): %i\n", ind, (int)f->run_id);
3861 for (i = 0; i < vec_size(f->blocks); ++i) {
3862 if (f->blocks[i]->run_id != f->run_id) {
3863 oprintf("%slife pass check fail! %i != %i\n", ind, (int)f->blocks[i]->run_id, (int)f->run_id);
3865 ir_block_dump(f->blocks[i], ind, oprintf);
3869 ind[strlen(ind)-1] = 0;
3870 oprintf("%sendfunction %s\n", ind, f->name);
3873 void ir_block_dump(ir_block* b, char *ind,
3874 int (*oprintf)(const char*, ...))
3877 oprintf("%s:%s\n", ind, b->label);
3878 strncat(ind, "\t", IND_BUFSZ);
3880 if (b->instr && b->instr[0])
3881 oprintf("%s (%i) [entry]\n", ind, (int)(b->instr[0]->eid-1));
3882 for (i = 0; i < vec_size(b->instr); ++i)
3883 ir_instr_dump(b->instr[i], ind, oprintf);
3884 ind[strlen(ind)-1] = 0;
3887 void dump_phi(ir_instr *in, int (*oprintf)(const char*, ...))
3890 oprintf("%s <- phi ", in->_ops[0]->name);
3891 for (i = 0; i < vec_size(in->phi); ++i)
3893 oprintf("([%s] : %s) ", in->phi[i].from->label,
3894 in->phi[i].value->name);
3899 void ir_instr_dump(ir_instr *in, char *ind,
3900 int (*oprintf)(const char*, ...))
3903 const char *comma = NULL;
3905 oprintf("%s (%i) ", ind, (int)in->eid);
3907 if (in->opcode == VINSTR_PHI) {
3908 dump_phi(in, oprintf);
3912 strncat(ind, "\t", IND_BUFSZ);
3914 if (in->_ops[0] && (in->_ops[1] || in->_ops[2])) {
3915 ir_value_dump(in->_ops[0], oprintf);
3916 if (in->_ops[1] || in->_ops[2])
3919 if (in->opcode == INSTR_CALL0 || in->opcode == VINSTR_NRCALL) {
3920 oprintf("CALL%i\t", vec_size(in->params));
3922 oprintf("%s\t", qc_opname(in->opcode));
3924 if (in->_ops[0] && !(in->_ops[1] || in->_ops[2])) {
3925 ir_value_dump(in->_ops[0], oprintf);
3930 for (i = 1; i != 3; ++i) {
3934 ir_value_dump(in->_ops[i], oprintf);
3942 oprintf("[%s]", in->bops[0]->label);
3946 oprintf("%s[%s]", comma, in->bops[1]->label);
3947 if (vec_size(in->params)) {
3948 oprintf("\tparams: ");
3949 for (i = 0; i != vec_size(in->params); ++i) {
3950 oprintf("%s, ", in->params[i]->name);
3954 ind[strlen(ind)-1] = 0;
3957 void ir_value_dump_string(const char *str, int (*oprintf)(const char*, ...))
3960 for (; *str; ++str) {
3962 case '\n': oprintf("\\n"); break;
3963 case '\r': oprintf("\\r"); break;
3964 case '\t': oprintf("\\t"); break;
3965 case '\v': oprintf("\\v"); break;
3966 case '\f': oprintf("\\f"); break;
3967 case '\b': oprintf("\\b"); break;
3968 case '\a': oprintf("\\a"); break;
3969 case '\\': oprintf("\\\\"); break;
3970 case '"': oprintf("\\\""); break;
3971 default: oprintf("%c", *str); break;
3977 void ir_value_dump(ir_value* v, int (*oprintf)(const char*, ...))
3986 oprintf("fn:%s", v->name);
3989 oprintf("%g", v->constval.vfloat);
3992 oprintf("'%g %g %g'",
3995 v->constval.vvec.z);
3998 oprintf("(entity)");
4001 ir_value_dump_string(v->constval.vstring, oprintf);
4005 oprintf("%i", v->constval.vint);
4010 v->constval.vpointer->name);
4014 oprintf("%s", v->name);
4018 void ir_value_dump_life(const ir_value *self, int (*oprintf)(const char*,...))
4021 oprintf("Life of %12s:", self->name);
4022 for (i = 0; i < vec_size(self->life); ++i)
4024 oprintf(" + [%i, %i]\n", self->life[i].start, self->life[i].end);