2 * Copyright (C) 2012, 2013
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of
7 * this software and associated documentation files (the "Software"), to deal in
8 * the Software without restriction, including without limitation the rights to
9 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10 * of the Software, and to permit persons to whom the Software is furnished to do
11 * so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 /***********************************************************************
32 * Type sizes used at multiple points in the IR codegen
35 const char *type_name[TYPE_COUNT] = {
54 static size_t type_sizeof_[TYPE_COUNT] = {
61 1, /* TYPE_FUNCTION */
72 const uint16_t type_store_instr[TYPE_COUNT] = {
73 INSTR_STORE_F, /* should use I when having integer support */
80 INSTR_STORE_ENT, /* should use I */
82 INSTR_STORE_I, /* integer type */
87 INSTR_STORE_V, /* variant, should never be accessed */
89 VINSTR_END, /* struct */
90 VINSTR_END, /* union */
91 VINSTR_END, /* array */
93 VINSTR_END, /* noexpr */
96 const uint16_t field_store_instr[TYPE_COUNT] = {
106 INSTR_STORE_FLD, /* integer type */
111 INSTR_STORE_V, /* variant, should never be accessed */
113 VINSTR_END, /* struct */
114 VINSTR_END, /* union */
115 VINSTR_END, /* array */
116 VINSTR_END, /* nil */
117 VINSTR_END, /* noexpr */
120 const uint16_t type_storep_instr[TYPE_COUNT] = {
121 INSTR_STOREP_F, /* should use I when having integer support */
128 INSTR_STOREP_ENT, /* should use I */
130 INSTR_STOREP_ENT, /* integer type */
135 INSTR_STOREP_V, /* variant, should never be accessed */
137 VINSTR_END, /* struct */
138 VINSTR_END, /* union */
139 VINSTR_END, /* array */
140 VINSTR_END, /* nil */
141 VINSTR_END, /* noexpr */
144 const uint16_t type_eq_instr[TYPE_COUNT] = {
145 INSTR_EQ_F, /* should use I when having integer support */
150 INSTR_EQ_E, /* FLD has no comparison */
152 INSTR_EQ_E, /* should use I */
159 INSTR_EQ_V, /* variant, should never be accessed */
161 VINSTR_END, /* struct */
162 VINSTR_END, /* union */
163 VINSTR_END, /* array */
164 VINSTR_END, /* nil */
165 VINSTR_END, /* noexpr */
168 const uint16_t type_ne_instr[TYPE_COUNT] = {
169 INSTR_NE_F, /* should use I when having integer support */
174 INSTR_NE_E, /* FLD has no comparison */
176 INSTR_NE_E, /* should use I */
183 INSTR_NE_V, /* variant, should never be accessed */
185 VINSTR_END, /* struct */
186 VINSTR_END, /* union */
187 VINSTR_END, /* array */
188 VINSTR_END, /* nil */
189 VINSTR_END, /* noexpr */
192 const uint16_t type_not_instr[TYPE_COUNT] = {
193 INSTR_NOT_F, /* should use I when having integer support */
194 VINSTR_END, /* not to be used, depends on string related -f flags */
200 INSTR_NOT_ENT, /* should use I */
202 INSTR_NOT_I, /* integer type */
207 INSTR_NOT_V, /* variant, should never be accessed */
209 VINSTR_END, /* struct */
210 VINSTR_END, /* union */
211 VINSTR_END, /* array */
212 VINSTR_END, /* nil */
213 VINSTR_END, /* noexpr */
217 static ir_value* ir_value_var(const char *name, int st, int vtype);
218 static bool ir_value_set_name(ir_value*, const char *name);
219 static void ir_value_dump(ir_value*, int (*oprintf)(const char*,...));
221 static ir_value* ir_gen_extparam_proto(ir_builder *ir);
222 static void ir_gen_extparam (ir_builder *ir);
224 static bool ir_builder_set_name(ir_builder *self, const char *name);
226 static ir_function* ir_function_new(struct ir_builder_s *owner, int returntype);
227 static bool ir_function_set_name(ir_function*, const char *name);
228 static void ir_function_delete(ir_function*);
229 static void ir_function_dump(ir_function*, char *ind, int (*oprintf)(const char*,...));
231 static ir_value* ir_block_create_general_instr(ir_block *self, lex_ctx_t, const char *label,
232 int op, ir_value *a, ir_value *b, int outype);
233 static void ir_block_delete(ir_block*);
234 static ir_block* ir_block_new(struct ir_function_s *owner, const char *label);
235 static bool GMQCC_WARN ir_block_create_store(ir_block*, lex_ctx_t, ir_value *target, ir_value *what);
236 static bool ir_block_set_label(ir_block*, const char *label);
237 static void ir_block_dump(ir_block*, char *ind, int (*oprintf)(const char*,...));
239 static bool ir_instr_op(ir_instr*, int op, ir_value *value, bool writing);
240 static void ir_instr_delete(ir_instr*);
241 static void ir_instr_dump(ir_instr* in, char *ind, int (*oprintf)(const char*,...));
242 /* error functions */
244 static void irerror(lex_ctx_t ctx, const char *msg, ...)
248 con_cvprintmsg(ctx, LVL_ERROR, "internal error", msg, ap);
252 static bool GMQCC_WARN irwarning(lex_ctx_t ctx, int warntype, const char *fmt, ...)
257 r = vcompile_warning(ctx, warntype, fmt, ap);
262 /***********************************************************************
263 * Vector utility functions
266 static bool GMQCC_WARN vec_ir_value_find(ir_value **vec, const ir_value *what, size_t *idx)
269 size_t len = vec_size(vec);
270 for (i = 0; i < len; ++i) {
271 if (vec[i] == what) {
279 static bool GMQCC_WARN vec_ir_block_find(ir_block **vec, ir_block *what, size_t *idx)
282 size_t len = vec_size(vec);
283 for (i = 0; i < len; ++i) {
284 if (vec[i] == what) {
292 static bool GMQCC_WARN vec_ir_instr_find(ir_instr **vec, ir_instr *what, size_t *idx)
295 size_t len = vec_size(vec);
296 for (i = 0; i < len; ++i) {
297 if (vec[i] == what) {
305 /***********************************************************************
309 static void ir_block_delete_quick(ir_block* self);
310 static void ir_instr_delete_quick(ir_instr *self);
311 static void ir_function_delete_quick(ir_function *self);
313 ir_builder* ir_builder_new(const char *modulename)
318 self = (ir_builder*)mem_a(sizeof(*self));
322 self->functions = NULL;
323 self->globals = NULL;
325 self->filenames = NULL;
326 self->filestrings = NULL;
327 self->htglobals = util_htnew(IR_HT_SIZE);
328 self->htfields = util_htnew(IR_HT_SIZE);
329 self->htfunctions = util_htnew(IR_HT_SIZE);
331 self->extparams = NULL;
332 self->extparam_protos = NULL;
334 self->first_common_globaltemp = 0;
335 self->max_globaltemps = 0;
336 self->first_common_local = 0;
337 self->max_locals = 0;
339 self->str_immediate = 0;
341 if (!ir_builder_set_name(self, modulename)) {
346 self->nil = ir_value_var("nil", store_value, TYPE_NIL);
347 self->nil->cvq = CV_CONST;
349 for (i = 0; i != IR_MAX_VINSTR_TEMPS; ++i) {
350 /* we write to them, but they're not supposed to be used outside the IR, so
351 * let's not allow the generation of ir_instrs which use these.
352 * So it's a constant noexpr.
354 self->vinstr_temp[i] = ir_value_var("vinstr_temp", store_value, TYPE_NOEXPR);
355 self->vinstr_temp[i]->cvq = CV_CONST;
358 self->reserved_va_count = NULL;
359 self->code = code_init();
364 void ir_builder_delete(ir_builder* self)
367 util_htdel(self->htglobals);
368 util_htdel(self->htfields);
369 util_htdel(self->htfunctions);
370 mem_d((void*)self->name);
371 for (i = 0; i != vec_size(self->functions); ++i) {
372 ir_function_delete_quick(self->functions[i]);
374 vec_free(self->functions);
375 for (i = 0; i != vec_size(self->extparams); ++i) {
376 ir_value_delete(self->extparams[i]);
378 vec_free(self->extparams);
379 vec_free(self->extparam_protos);
380 for (i = 0; i != vec_size(self->globals); ++i) {
381 ir_value_delete(self->globals[i]);
383 vec_free(self->globals);
384 for (i = 0; i != vec_size(self->fields); ++i) {
385 ir_value_delete(self->fields[i]);
387 ir_value_delete(self->nil);
388 for (i = 0; i != IR_MAX_VINSTR_TEMPS; ++i) {
389 ir_value_delete(self->vinstr_temp[i]);
391 vec_free(self->fields);
392 vec_free(self->filenames);
393 vec_free(self->filestrings);
395 code_cleanup(self->code);
399 bool ir_builder_set_name(ir_builder *self, const char *name)
402 mem_d((void*)self->name);
403 self->name = util_strdup(name);
407 static ir_function* ir_builder_get_function(ir_builder *self, const char *name)
409 return (ir_function*)util_htget(self->htfunctions, name);
412 ir_function* ir_builder_create_function(ir_builder *self, const char *name, int outtype)
414 ir_function *fn = ir_builder_get_function(self, name);
419 fn = ir_function_new(self, outtype);
420 if (!ir_function_set_name(fn, name))
422 ir_function_delete(fn);
425 vec_push(self->functions, fn);
426 util_htset(self->htfunctions, name, fn);
428 fn->value = ir_builder_create_global(self, fn->name, TYPE_FUNCTION);
430 ir_function_delete(fn);
434 fn->value->hasvalue = true;
435 fn->value->outtype = outtype;
436 fn->value->constval.vfunc = fn;
437 fn->value->context = fn->context;
442 static ir_value* ir_builder_get_global(ir_builder *self, const char *name)
444 return (ir_value*)util_htget(self->htglobals, name);
447 ir_value* ir_builder_create_global(ir_builder *self, const char *name, int vtype)
453 ve = ir_builder_get_global(self, name);
459 ve = ir_value_var(name, store_global, vtype);
460 vec_push(self->globals, ve);
461 util_htset(self->htglobals, name, ve);
465 ir_value* ir_builder_get_va_count(ir_builder *self)
467 if (self->reserved_va_count)
468 return self->reserved_va_count;
469 return (self->reserved_va_count = ir_builder_create_global(self, "reserved:va_count", TYPE_FLOAT));
472 static ir_value* ir_builder_get_field(ir_builder *self, const char *name)
474 return (ir_value*)util_htget(self->htfields, name);
478 ir_value* ir_builder_create_field(ir_builder *self, const char *name, int vtype)
480 ir_value *ve = ir_builder_get_field(self, name);
485 ve = ir_value_var(name, store_global, TYPE_FIELD);
486 ve->fieldtype = vtype;
487 vec_push(self->fields, ve);
488 util_htset(self->htfields, name, ve);
492 /***********************************************************************
496 static bool ir_function_naive_phi(ir_function*);
497 static void ir_function_enumerate(ir_function*);
498 static bool ir_function_calculate_liferanges(ir_function*);
499 static bool ir_function_allocate_locals(ir_function*);
501 ir_function* ir_function_new(ir_builder* owner, int outtype)
504 self = (ir_function*)mem_a(sizeof(*self));
509 memset(self, 0, sizeof(*self));
512 if (!ir_function_set_name(self, "<@unnamed>")) {
519 self->context.file = "<@no context>";
520 self->context.line = 0;
521 self->outtype = outtype;
530 self->max_varargs = 0;
532 self->code_function_def = -1;
533 self->allocated_locals = 0;
534 self->globaltemps = 0;
540 bool ir_function_set_name(ir_function *self, const char *name)
543 mem_d((void*)self->name);
544 self->name = util_strdup(name);
548 static void ir_function_delete_quick(ir_function *self)
551 mem_d((void*)self->name);
553 for (i = 0; i != vec_size(self->blocks); ++i)
554 ir_block_delete_quick(self->blocks[i]);
555 vec_free(self->blocks);
557 vec_free(self->params);
559 for (i = 0; i != vec_size(self->values); ++i)
560 ir_value_delete(self->values[i]);
561 vec_free(self->values);
563 for (i = 0; i != vec_size(self->locals); ++i)
564 ir_value_delete(self->locals[i]);
565 vec_free(self->locals);
567 /* self->value is deleted by the builder */
572 void ir_function_delete(ir_function *self)
575 mem_d((void*)self->name);
577 for (i = 0; i != vec_size(self->blocks); ++i)
578 ir_block_delete(self->blocks[i]);
579 vec_free(self->blocks);
581 vec_free(self->params);
583 for (i = 0; i != vec_size(self->values); ++i)
584 ir_value_delete(self->values[i]);
585 vec_free(self->values);
587 for (i = 0; i != vec_size(self->locals); ++i)
588 ir_value_delete(self->locals[i]);
589 vec_free(self->locals);
591 /* self->value is deleted by the builder */
596 static void ir_function_collect_value(ir_function *self, ir_value *v)
598 vec_push(self->values, v);
601 ir_block* ir_function_create_block(lex_ctx_t ctx, ir_function *self, const char *label)
603 ir_block* bn = ir_block_new(self, label);
605 vec_push(self->blocks, bn);
609 static bool instr_is_operation(uint16_t op)
611 return ( (op >= INSTR_MUL_F && op <= INSTR_GT) ||
612 (op >= INSTR_LOAD_F && op <= INSTR_LOAD_FNC) ||
613 (op == INSTR_ADDRESS) ||
614 (op >= INSTR_NOT_F && op <= INSTR_NOT_FNC) ||
615 (op >= INSTR_AND && op <= INSTR_BITOR) ||
616 (op >= INSTR_CALL0 && op <= INSTR_CALL8) ||
617 (op >= VINSTR_BITAND_V && op <= VINSTR_NEG_V) );
620 static bool ir_function_pass_peephole(ir_function *self)
624 for (b = 0; b < vec_size(self->blocks); ++b) {
626 ir_block *block = self->blocks[b];
628 for (i = 0; i < vec_size(block->instr); ++i) {
630 inst = block->instr[i];
633 (inst->opcode >= INSTR_STORE_F &&
634 inst->opcode <= INSTR_STORE_FNC))
642 oper = block->instr[i-1];
643 if (!instr_is_operation(oper->opcode))
646 /* Don't change semantics of MUL_VF in engines where these may not alias. */
647 if (OPTS_FLAG(LEGACY_VECTOR_MATHS)) {
648 if (oper->opcode == INSTR_MUL_VF && oper->_ops[2]->memberof == oper->_ops[1])
650 if (oper->opcode == INSTR_MUL_FV && oper->_ops[1]->memberof == oper->_ops[2])
654 value = oper->_ops[0];
656 /* only do it for SSA values */
657 if (value->store != store_value)
660 /* don't optimize out the temp if it's used later again */
661 if (vec_size(value->reads) != 1)
664 /* The very next store must use this value */
665 if (value->reads[0] != store)
668 /* And of course the store must _read_ from it, so it's in
670 if (store->_ops[1] != value)
673 ++opts_optimizationcount[OPTIM_PEEPHOLE];
674 (void)!ir_instr_op(oper, 0, store->_ops[0], true);
676 vec_remove(block->instr, i, 1);
677 ir_instr_delete(store);
679 else if (inst->opcode == VINSTR_COND)
681 /* COND on a value resulting from a NOT could
682 * remove the NOT and swap its operands
689 value = inst->_ops[0];
691 if (value->store != store_value ||
692 vec_size(value->reads) != 1 ||
693 value->reads[0] != inst)
698 inot = value->writes[0];
699 if (inot->_ops[0] != value ||
700 inot->opcode < INSTR_NOT_F ||
701 inot->opcode > INSTR_NOT_FNC ||
702 inot->opcode == INSTR_NOT_V || /* can't do these */
703 inot->opcode == INSTR_NOT_S)
709 ++opts_optimizationcount[OPTIM_PEEPHOLE];
711 (void)!ir_instr_op(inst, 0, inot->_ops[1], false);
714 for (inotid = 0; inotid < vec_size(tmp->instr); ++inotid) {
715 if (tmp->instr[inotid] == inot)
718 if (inotid >= vec_size(tmp->instr)) {
719 compile_error(inst->context, "sanity-check failed: failed to find instruction to optimize out");
722 vec_remove(tmp->instr, inotid, 1);
723 ir_instr_delete(inot);
724 /* swap ontrue/onfalse */
726 inst->bops[0] = inst->bops[1];
737 static bool ir_function_pass_tailrecursion(ir_function *self)
741 for (b = 0; b < vec_size(self->blocks); ++b) {
743 ir_instr *ret, *call, *store = NULL;
744 ir_block *block = self->blocks[b];
746 if (!block->final || vec_size(block->instr) < 2)
749 ret = block->instr[vec_size(block->instr)-1];
750 if (ret->opcode != INSTR_DONE && ret->opcode != INSTR_RETURN)
753 call = block->instr[vec_size(block->instr)-2];
754 if (call->opcode >= INSTR_STORE_F && call->opcode <= INSTR_STORE_FNC) {
755 /* account for the unoptimized
757 * STORE %return, %tmp
761 if (vec_size(block->instr) < 3)
765 call = block->instr[vec_size(block->instr)-3];
768 if (call->opcode < INSTR_CALL0 || call->opcode > INSTR_CALL8)
772 /* optimize out the STORE */
774 ret->_ops[0] == store->_ops[0] &&
775 store->_ops[1] == call->_ops[0])
777 ++opts_optimizationcount[OPTIM_PEEPHOLE];
778 call->_ops[0] = store->_ops[0];
779 vec_remove(block->instr, vec_size(block->instr) - 2, 1);
780 ir_instr_delete(store);
789 funcval = call->_ops[1];
792 if (funcval->vtype != TYPE_FUNCTION || funcval->constval.vfunc != self)
795 /* now we have a CALL and a RET, check if it's a tailcall */
796 if (ret->_ops[0] && call->_ops[0] != ret->_ops[0])
799 ++opts_optimizationcount[OPTIM_TAIL_RECURSION];
800 vec_shrinkby(block->instr, 2);
802 block->final = false; /* open it back up */
804 /* emite parameter-stores */
805 for (p = 0; p < vec_size(call->params); ++p) {
806 /* assert(call->params_count <= self->locals_count); */
807 if (!ir_block_create_store(block, call->context, self->locals[p], call->params[p])) {
808 irerror(call->context, "failed to create tailcall store instruction for parameter %i", (int)p);
812 if (!ir_block_create_jump(block, call->context, self->blocks[0])) {
813 irerror(call->context, "failed to create tailcall jump");
817 ir_instr_delete(call);
818 ir_instr_delete(ret);
824 bool ir_function_finalize(ir_function *self)
831 if (OPTS_OPTIMIZATION(OPTIM_PEEPHOLE)) {
832 if (!ir_function_pass_peephole(self)) {
833 irerror(self->context, "generic optimization pass broke something in `%s`", self->name);
838 if (OPTS_OPTIMIZATION(OPTIM_TAIL_RECURSION)) {
839 if (!ir_function_pass_tailrecursion(self)) {
840 irerror(self->context, "tail-recursion optimization pass broke something in `%s`", self->name);
845 if (!ir_function_naive_phi(self)) {
846 irerror(self->context, "internal error: ir_function_naive_phi failed");
850 for (i = 0; i < vec_size(self->locals); ++i) {
851 ir_value *v = self->locals[i];
852 if (v->vtype == TYPE_VECTOR ||
853 (v->vtype == TYPE_FIELD && v->outtype == TYPE_VECTOR))
855 ir_value_vector_member(v, 0);
856 ir_value_vector_member(v, 1);
857 ir_value_vector_member(v, 2);
860 for (i = 0; i < vec_size(self->values); ++i) {
861 ir_value *v = self->values[i];
862 if (v->vtype == TYPE_VECTOR ||
863 (v->vtype == TYPE_FIELD && v->outtype == TYPE_VECTOR))
865 ir_value_vector_member(v, 0);
866 ir_value_vector_member(v, 1);
867 ir_value_vector_member(v, 2);
871 ir_function_enumerate(self);
873 if (!ir_function_calculate_liferanges(self))
875 if (!ir_function_allocate_locals(self))
880 ir_value* ir_function_create_local(ir_function *self, const char *name, int vtype, bool param)
885 vec_size(self->locals) &&
886 self->locals[vec_size(self->locals)-1]->store != store_param) {
887 irerror(self->context, "cannot add parameters after adding locals");
891 ve = ir_value_var(name, (param ? store_param : store_local), vtype);
894 vec_push(self->locals, ve);
898 /***********************************************************************
902 ir_block* ir_block_new(ir_function* owner, const char *name)
905 self = (ir_block*)mem_a(sizeof(*self));
909 memset(self, 0, sizeof(*self));
912 if (name && !ir_block_set_label(self, name)) {
917 self->context.file = "<@no context>";
918 self->context.line = 0;
922 self->entries = NULL;
926 self->is_return = false;
930 self->generated = false;
935 static void ir_block_delete_quick(ir_block* self)
938 if (self->label) mem_d(self->label);
939 for (i = 0; i != vec_size(self->instr); ++i)
940 ir_instr_delete_quick(self->instr[i]);
941 vec_free(self->instr);
942 vec_free(self->entries);
943 vec_free(self->exits);
944 vec_free(self->living);
948 void ir_block_delete(ir_block* self)
951 if (self->label) mem_d(self->label);
952 for (i = 0; i != vec_size(self->instr); ++i)
953 ir_instr_delete(self->instr[i]);
954 vec_free(self->instr);
955 vec_free(self->entries);
956 vec_free(self->exits);
957 vec_free(self->living);
961 bool ir_block_set_label(ir_block *self, const char *name)
964 mem_d((void*)self->label);
965 self->label = util_strdup(name);
966 return !!self->label;
969 /***********************************************************************
973 static ir_instr* ir_instr_new(lex_ctx_t ctx, ir_block* owner, int op)
976 self = (ir_instr*)mem_a(sizeof(*self));
983 self->_ops[0] = NULL;
984 self->_ops[1] = NULL;
985 self->_ops[2] = NULL;
986 self->bops[0] = NULL;
987 self->bops[1] = NULL;
998 static void ir_instr_delete_quick(ir_instr *self)
1000 vec_free(self->phi);
1001 vec_free(self->params);
1005 static void ir_instr_delete(ir_instr *self)
1008 /* The following calls can only delete from
1009 * vectors, we still want to delete this instruction
1010 * so ignore the return value. Since with the warn_unused_result attribute
1011 * gcc doesn't care about an explicit: (void)foo(); to ignore the result,
1012 * I have to improvise here and use if(foo());
1014 for (i = 0; i < vec_size(self->phi); ++i) {
1016 if (vec_ir_instr_find(self->phi[i].value->writes, self, &idx))
1017 vec_remove(self->phi[i].value->writes, idx, 1);
1018 if (vec_ir_instr_find(self->phi[i].value->reads, self, &idx))
1019 vec_remove(self->phi[i].value->reads, idx, 1);
1021 vec_free(self->phi);
1022 for (i = 0; i < vec_size(self->params); ++i) {
1024 if (vec_ir_instr_find(self->params[i]->writes, self, &idx))
1025 vec_remove(self->params[i]->writes, idx, 1);
1026 if (vec_ir_instr_find(self->params[i]->reads, self, &idx))
1027 vec_remove(self->params[i]->reads, idx, 1);
1029 vec_free(self->params);
1030 (void)!ir_instr_op(self, 0, NULL, false);
1031 (void)!ir_instr_op(self, 1, NULL, false);
1032 (void)!ir_instr_op(self, 2, NULL, false);
1036 static bool ir_instr_op(ir_instr *self, int op, ir_value *v, bool writing)
1038 if (v && v->vtype == TYPE_NOEXPR) {
1039 irerror(self->context, "tried to use a NOEXPR value");
1043 if (self->_ops[op]) {
1045 if (writing && vec_ir_instr_find(self->_ops[op]->writes, self, &idx))
1046 vec_remove(self->_ops[op]->writes, idx, 1);
1047 else if (vec_ir_instr_find(self->_ops[op]->reads, self, &idx))
1048 vec_remove(self->_ops[op]->reads, idx, 1);
1052 vec_push(v->writes, self);
1054 vec_push(v->reads, self);
1060 /***********************************************************************
1064 static void ir_value_code_setaddr(ir_value *self, int32_t gaddr)
1066 self->code.globaladdr = gaddr;
1067 if (self->members[0]) self->members[0]->code.globaladdr = gaddr;
1068 if (self->members[1]) self->members[1]->code.globaladdr = gaddr;
1069 if (self->members[2]) self->members[2]->code.globaladdr = gaddr;
1072 static int32_t ir_value_code_addr(const ir_value *self)
1074 if (self->store == store_return)
1075 return OFS_RETURN + self->code.addroffset;
1076 return self->code.globaladdr + self->code.addroffset;
1079 ir_value* ir_value_var(const char *name, int storetype, int vtype)
1082 self = (ir_value*)mem_a(sizeof(*self));
1083 self->vtype = vtype;
1084 self->fieldtype = TYPE_VOID;
1085 self->outtype = TYPE_VOID;
1086 self->store = storetype;
1090 self->writes = NULL;
1092 self->cvq = CV_NONE;
1093 self->hasvalue = false;
1094 self->context.file = "<@no context>";
1095 self->context.line = 0;
1097 if (name && !ir_value_set_name(self, name)) {
1098 irerror(self->context, "out of memory");
1103 memset(&self->constval, 0, sizeof(self->constval));
1104 memset(&self->code, 0, sizeof(self->code));
1106 self->members[0] = NULL;
1107 self->members[1] = NULL;
1108 self->members[2] = NULL;
1109 self->memberof = NULL;
1111 self->unique_life = false;
1112 self->locked = false;
1113 self->callparam = false;
1119 ir_value* ir_value_vector_member(ir_value *self, unsigned int member)
1127 if (self->members[member])
1128 return self->members[member];
1131 len = strlen(self->name);
1132 name = (char*)mem_a(len + 3);
1133 memcpy(name, self->name, len);
1135 name[len+1] = 'x' + member;
1141 if (self->vtype == TYPE_VECTOR)
1143 m = ir_value_var(name, self->store, TYPE_FLOAT);
1148 m->context = self->context;
1150 self->members[member] = m;
1151 m->code.addroffset = member;
1153 else if (self->vtype == TYPE_FIELD)
1155 if (self->fieldtype != TYPE_VECTOR)
1157 m = ir_value_var(name, self->store, TYPE_FIELD);
1162 m->fieldtype = TYPE_FLOAT;
1163 m->context = self->context;
1165 self->members[member] = m;
1166 m->code.addroffset = member;
1170 irerror(self->context, "invalid member access on %s", self->name);
1178 static GMQCC_INLINE size_t ir_value_sizeof(const ir_value *self)
1180 if (self->vtype == TYPE_FIELD && self->fieldtype == TYPE_VECTOR)
1181 return type_sizeof_[TYPE_VECTOR];
1182 return type_sizeof_[self->vtype];
1185 static ir_value* ir_value_out(ir_function *owner, const char *name, int storetype, int vtype)
1187 ir_value *v = ir_value_var(name, storetype, vtype);
1190 ir_function_collect_value(owner, v);
1194 void ir_value_delete(ir_value* self)
1198 mem_d((void*)self->name);
1201 if (self->vtype == TYPE_STRING)
1202 mem_d((void*)self->constval.vstring);
1204 for (i = 0; i < 3; ++i) {
1205 if (self->members[i])
1206 ir_value_delete(self->members[i]);
1208 vec_free(self->reads);
1209 vec_free(self->writes);
1210 vec_free(self->life);
1214 bool ir_value_set_name(ir_value *self, const char *name)
1217 mem_d((void*)self->name);
1218 self->name = util_strdup(name);
1219 return !!self->name;
1222 bool ir_value_set_float(ir_value *self, float f)
1224 if (self->vtype != TYPE_FLOAT)
1226 self->constval.vfloat = f;
1227 self->hasvalue = true;
1231 bool ir_value_set_func(ir_value *self, int f)
1233 if (self->vtype != TYPE_FUNCTION)
1235 self->constval.vint = f;
1236 self->hasvalue = true;
1240 bool ir_value_set_vector(ir_value *self, vec3_t v)
1242 if (self->vtype != TYPE_VECTOR)
1244 self->constval.vvec = v;
1245 self->hasvalue = true;
1249 bool ir_value_set_field(ir_value *self, ir_value *fld)
1251 if (self->vtype != TYPE_FIELD)
1253 self->constval.vpointer = fld;
1254 self->hasvalue = true;
1258 bool ir_value_set_string(ir_value *self, const char *str)
1260 if (self->vtype != TYPE_STRING)
1262 self->constval.vstring = util_strdupe(str);
1263 self->hasvalue = true;
1268 bool ir_value_set_int(ir_value *self, int i)
1270 if (self->vtype != TYPE_INTEGER)
1272 self->constval.vint = i;
1273 self->hasvalue = true;
1278 bool ir_value_lives(ir_value *self, size_t at)
1281 for (i = 0; i < vec_size(self->life); ++i)
1283 ir_life_entry_t *life = &self->life[i];
1284 if (life->start <= at && at <= life->end)
1286 if (life->start > at) /* since it's ordered */
1292 static bool ir_value_life_insert(ir_value *self, size_t idx, ir_life_entry_t e)
1295 vec_push(self->life, e);
1296 for (k = vec_size(self->life)-1; k > idx; --k)
1297 self->life[k] = self->life[k-1];
1298 self->life[idx] = e;
1302 static bool ir_value_life_merge(ir_value *self, size_t s)
1305 const size_t vs = vec_size(self->life);
1306 ir_life_entry_t *life = NULL;
1307 ir_life_entry_t *before = NULL;
1308 ir_life_entry_t new_entry;
1310 /* Find the first range >= s */
1311 for (i = 0; i < vs; ++i)
1314 life = &self->life[i];
1315 if (life->start > s)
1318 /* nothing found? append */
1321 if (life && life->end+1 == s)
1323 /* previous life range can be merged in */
1327 if (life && life->end >= s)
1329 e.start = e.end = s;
1330 vec_push(self->life, e);
1336 if (before->end + 1 == s &&
1337 life->start - 1 == s)
1340 before->end = life->end;
1341 vec_remove(self->life, i, 1);
1344 if (before->end + 1 == s)
1350 /* already contained */
1351 if (before->end >= s)
1355 if (life->start - 1 == s)
1360 /* insert a new entry */
1361 new_entry.start = new_entry.end = s;
1362 return ir_value_life_insert(self, i, new_entry);
1365 static bool ir_value_life_merge_into(ir_value *self, const ir_value *other)
1369 if (!vec_size(other->life))
1372 if (!vec_size(self->life)) {
1373 size_t count = vec_size(other->life);
1374 ir_life_entry_t *life = vec_add(self->life, count);
1375 memcpy(life, other->life, count * sizeof(*life));
1380 for (i = 0; i < vec_size(other->life); ++i)
1382 const ir_life_entry_t *life = &other->life[i];
1385 ir_life_entry_t *entry = &self->life[myi];
1387 if (life->end+1 < entry->start)
1389 /* adding an interval before entry */
1390 if (!ir_value_life_insert(self, myi, *life))
1396 if (life->start < entry->start &&
1397 life->end+1 >= entry->start)
1399 /* starts earlier and overlaps */
1400 entry->start = life->start;
1403 if (life->end > entry->end &&
1404 life->start <= entry->end+1)
1406 /* ends later and overlaps */
1407 entry->end = life->end;
1410 /* see if our change combines it with the next ranges */
1411 while (myi+1 < vec_size(self->life) &&
1412 entry->end+1 >= self->life[1+myi].start)
1414 /* overlaps with (myi+1) */
1415 if (entry->end < self->life[1+myi].end)
1416 entry->end = self->life[1+myi].end;
1417 vec_remove(self->life, myi+1, 1);
1418 entry = &self->life[myi];
1421 /* see if we're after the entry */
1422 if (life->start > entry->end)
1425 /* append if we're at the end */
1426 if (myi >= vec_size(self->life)) {
1427 vec_push(self->life, *life);
1430 /* otherweise check the next range */
1439 static bool ir_values_overlap(const ir_value *a, const ir_value *b)
1441 /* For any life entry in A see if it overlaps with
1442 * any life entry in B.
1443 * Note that the life entries are orderes, so we can make a
1444 * more efficient algorithm there than naively translating the
1448 ir_life_entry_t *la, *lb, *enda, *endb;
1450 /* first of all, if either has no life range, they cannot clash */
1451 if (!vec_size(a->life) || !vec_size(b->life))
1456 enda = la + vec_size(a->life);
1457 endb = lb + vec_size(b->life);
1460 /* check if the entries overlap, for that,
1461 * both must start before the other one ends.
1463 if (la->start < lb->end &&
1464 lb->start < la->end)
1469 /* entries are ordered
1470 * one entry is earlier than the other
1471 * that earlier entry will be moved forward
1473 if (la->start < lb->start)
1475 /* order: A B, move A forward
1476 * check if we hit the end with A
1481 else /* if (lb->start < la->start) actually <= */
1483 /* order: B A, move B forward
1484 * check if we hit the end with B
1493 /***********************************************************************
1497 static bool ir_check_unreachable(ir_block *self)
1499 /* The IR should never have to deal with unreachable code */
1500 if (!self->final/* || OPTS_FLAG(ALLOW_UNREACHABLE_CODE)*/)
1502 irerror(self->context, "unreachable statement (%s)", self->label);
1506 bool ir_block_create_store_op(ir_block *self, lex_ctx_t ctx, int op, ir_value *target, ir_value *what)
1509 if (!ir_check_unreachable(self))
1512 if (target->store == store_value &&
1513 (op < INSTR_STOREP_F || op > INSTR_STOREP_FNC))
1515 irerror(self->context, "cannot store to an SSA value");
1516 irerror(self->context, "trying to store: %s <- %s", target->name, what->name);
1517 irerror(self->context, "instruction: %s", util_instr_str[op]);
1521 in = ir_instr_new(ctx, self, op);
1525 if (!ir_instr_op(in, 0, target, (op < INSTR_STOREP_F || op > INSTR_STOREP_FNC)) ||
1526 !ir_instr_op(in, 1, what, false))
1528 ir_instr_delete(in);
1531 vec_push(self->instr, in);
1535 static bool ir_block_create_store(ir_block *self, lex_ctx_t ctx, ir_value *target, ir_value *what)
1539 if (target->vtype == TYPE_VARIANT)
1540 vtype = what->vtype;
1542 vtype = target->vtype;
1545 if (vtype == TYPE_FLOAT && what->vtype == TYPE_INTEGER)
1546 op = INSTR_CONV_ITOF;
1547 else if (vtype == TYPE_INTEGER && what->vtype == TYPE_FLOAT)
1548 op = INSTR_CONV_FTOI;
1550 op = type_store_instr[vtype];
1552 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS)) {
1553 if (op == INSTR_STORE_FLD && what->fieldtype == TYPE_VECTOR)
1557 return ir_block_create_store_op(self, ctx, op, target, what);
1560 bool ir_block_create_storep(ir_block *self, lex_ctx_t ctx, ir_value *target, ir_value *what)
1565 if (target->vtype != TYPE_POINTER)
1568 /* storing using pointer - target is a pointer, type must be
1569 * inferred from source
1571 vtype = what->vtype;
1573 op = type_storep_instr[vtype];
1574 if (OPTS_FLAG(ADJUST_VECTOR_FIELDS)) {
1575 if (op == INSTR_STOREP_FLD && what->fieldtype == TYPE_VECTOR)
1576 op = INSTR_STOREP_V;
1579 return ir_block_create_store_op(self, ctx, op, target, what);
1582 bool ir_block_create_return(ir_block *self, lex_ctx_t ctx, ir_value *v)
1585 if (!ir_check_unreachable(self))
1588 self->is_return = true;
1589 in = ir_instr_new(ctx, self, INSTR_RETURN);
1593 if (v && !ir_instr_op(in, 0, v, false)) {
1594 ir_instr_delete(in);
1598 vec_push(self->instr, in);
1602 bool ir_block_create_if(ir_block *self, lex_ctx_t ctx, ir_value *v,
1603 ir_block *ontrue, ir_block *onfalse)
1606 if (!ir_check_unreachable(self))
1609 /*in = ir_instr_new(ctx, self, (v->vtype == TYPE_STRING ? INSTR_IF_S : INSTR_IF_F));*/
1610 in = ir_instr_new(ctx, self, VINSTR_COND);
1614 if (!ir_instr_op(in, 0, v, false)) {
1615 ir_instr_delete(in);
1619 in->bops[0] = ontrue;
1620 in->bops[1] = onfalse;
1622 vec_push(self->instr, in);
1624 vec_push(self->exits, ontrue);
1625 vec_push(self->exits, onfalse);
1626 vec_push(ontrue->entries, self);
1627 vec_push(onfalse->entries, self);
1631 bool ir_block_create_jump(ir_block *self, lex_ctx_t ctx, ir_block *to)
1634 if (!ir_check_unreachable(self))
1637 in = ir_instr_new(ctx, self, VINSTR_JUMP);
1642 vec_push(self->instr, in);
1644 vec_push(self->exits, to);
1645 vec_push(to->entries, self);
1649 bool ir_block_create_goto(ir_block *self, lex_ctx_t ctx, ir_block *to)
1651 self->owner->flags |= IR_FLAG_HAS_GOTO;
1652 return ir_block_create_jump(self, ctx, to);
1655 ir_instr* ir_block_create_phi(ir_block *self, lex_ctx_t ctx, const char *label, int ot)
1659 if (!ir_check_unreachable(self))
1661 in = ir_instr_new(ctx, self, VINSTR_PHI);
1664 out = ir_value_out(self->owner, label, store_value, ot);
1666 ir_instr_delete(in);
1669 if (!ir_instr_op(in, 0, out, true)) {
1670 ir_instr_delete(in);
1671 ir_value_delete(out);
1674 vec_push(self->instr, in);
1678 ir_value* ir_phi_value(ir_instr *self)
1680 return self->_ops[0];
1683 void ir_phi_add(ir_instr* self, ir_block *b, ir_value *v)
1687 if (!vec_ir_block_find(self->owner->entries, b, NULL)) {
1688 /* Must not be possible to cause this, otherwise the AST
1689 * is doing something wrong.
1691 irerror(self->context, "Invalid entry block for PHI");
1697 vec_push(v->reads, self);
1698 vec_push(self->phi, pe);
1701 /* call related code */
1702 ir_instr* ir_block_create_call(ir_block *self, lex_ctx_t ctx, const char *label, ir_value *func, bool noreturn)
1706 if (!ir_check_unreachable(self))
1708 in = ir_instr_new(ctx, self, (noreturn ? VINSTR_NRCALL : INSTR_CALL0));
1713 self->is_return = true;
1715 out = ir_value_out(self->owner, label, (func->outtype == TYPE_VOID) ? store_return : store_value, func->outtype);
1717 ir_instr_delete(in);
1720 if (!ir_instr_op(in, 0, out, true) ||
1721 !ir_instr_op(in, 1, func, false))
1723 ir_instr_delete(in);
1724 ir_value_delete(out);
1727 vec_push(self->instr, in);
1730 if (!ir_block_create_return(self, ctx, NULL)) {
1731 compile_error(ctx, "internal error: failed to generate dummy-return instruction");
1732 ir_instr_delete(in);
1740 ir_value* ir_call_value(ir_instr *self)
1742 return self->_ops[0];
1745 void ir_call_param(ir_instr* self, ir_value *v)
1747 vec_push(self->params, v);
1748 vec_push(v->reads, self);
1751 /* binary op related code */
1753 ir_value* ir_block_create_binop(ir_block *self, lex_ctx_t ctx,
1754 const char *label, int opcode,
1755 ir_value *left, ir_value *right)
1778 case INSTR_SUB_S: /* -- offset of string as float */
1783 case INSTR_BITOR_IF:
1784 case INSTR_BITOR_FI:
1785 case INSTR_BITAND_FI:
1786 case INSTR_BITAND_IF:
1801 case INSTR_BITAND_I:
1804 case INSTR_RSHIFT_I:
1805 case INSTR_LSHIFT_I:
1813 case VINSTR_BITAND_V:
1814 case VINSTR_BITOR_V:
1815 case VINSTR_BITXOR_V:
1816 case VINSTR_BITAND_VF:
1817 case VINSTR_BITOR_VF:
1818 case VINSTR_BITXOR_VF:
1833 * after the following default case, the value of opcode can never
1834 * be 1, 2, 3, 4, 5, 6, 7, 8, 9, 62, 63, 64, 65
1838 /* boolean operations result in floats */
1841 * opcode >= 10 takes true branch opcode is at least 10
1842 * opcode <= 23 takes false branch opcode is at least 24
1844 if (opcode >= INSTR_EQ_F && opcode <= INSTR_GT)
1848 * At condition "opcode <= 23", the value of "opcode" must be
1850 * At condition "opcode <= 23", the value of "opcode" cannot be
1851 * equal to any of {1, 2, 3, 4, 5, 6, 7, 8, 9, 62, 63, 64, 65}.
1852 * The condition "opcode <= 23" cannot be true.
1854 * Thus ot=2 (TYPE_FLOAT) can never be true
1857 else if (opcode >= INSTR_LE && opcode <= INSTR_GT)
1859 else if (opcode >= INSTR_LE_I && opcode <= INSTR_EQ_FI)
1864 if (ot == TYPE_VOID) {
1865 /* The AST or parser were supposed to check this! */
1869 return ir_block_create_general_instr(self, ctx, label, opcode, left, right, ot);
1872 ir_value* ir_block_create_unary(ir_block *self, lex_ctx_t ctx,
1873 const char *label, int opcode,
1876 int ot = TYPE_FLOAT;
1882 case INSTR_NOT_FNC: /*
1883 case INSTR_NOT_I: */
1888 * Negation for virtual instructions is emulated with 0-value. Thankfully
1889 * the operand for 0 already exists so we just source it from here.
1892 return ir_block_create_general_instr(self, ctx, label, INSTR_SUB_F, NULL, operand, ot);
1894 return ir_block_create_general_instr(self, ctx, label, INSTR_SUB_V, NULL, operand, ot);
1897 ot = operand->vtype;
1900 if (ot == TYPE_VOID) {
1901 /* The AST or parser were supposed to check this! */
1905 /* let's use the general instruction creator and pass NULL for OPB */
1906 return ir_block_create_general_instr(self, ctx, label, opcode, operand, NULL, ot);
1909 static ir_value* ir_block_create_general_instr(ir_block *self, lex_ctx_t ctx, const char *label,
1910 int op, ir_value *a, ir_value *b, int outype)
1915 out = ir_value_out(self->owner, label, store_value, outype);
1919 instr = ir_instr_new(ctx, self, op);
1921 ir_value_delete(out);
1925 if (!ir_instr_op(instr, 0, out, true) ||
1926 !ir_instr_op(instr, 1, a, false) ||
1927 !ir_instr_op(instr, 2, b, false) )
1932 vec_push(self->instr, instr);
1936 ir_instr_delete(instr);
1937 ir_value_delete(out);
1941 ir_value* ir_block_create_fieldaddress(ir_block *self, lex_ctx_t ctx, const char *label, ir_value *ent, ir_value *field)
1945 /* Support for various pointer types todo if so desired */
1946 if (ent->vtype != TYPE_ENTITY)
1949 if (field->vtype != TYPE_FIELD)
1952 v = ir_block_create_general_instr(self, ctx, label, INSTR_ADDRESS, ent, field, TYPE_POINTER);
1953 v->fieldtype = field->fieldtype;
1957 ir_value* ir_block_create_load_from_ent(ir_block *self, lex_ctx_t ctx, const char *label, ir_value *ent, ir_value *field, int outype)
1960 if (ent->vtype != TYPE_ENTITY)
1963 /* at some point we could redirect for TYPE_POINTER... but that could lead to carelessness */
1964 if (field->vtype != TYPE_FIELD)
1969 case TYPE_FLOAT: op = INSTR_LOAD_F; break;
1970 case TYPE_VECTOR: op = INSTR_LOAD_V; break;
1971 case TYPE_STRING: op = INSTR_LOAD_S; break;
1972 case TYPE_FIELD: op = INSTR_LOAD_FLD; break;
1973 case TYPE_ENTITY: op = INSTR_LOAD_ENT; break;
1974 case TYPE_FUNCTION: op = INSTR_LOAD_FNC; break;
1976 case TYPE_POINTER: op = INSTR_LOAD_I; break;
1977 case TYPE_INTEGER: op = INSTR_LOAD_I; break;
1980 irerror(self->context, "invalid type for ir_block_create_load_from_ent: %s", type_name[outype]);
1984 return ir_block_create_general_instr(self, ctx, label, op, ent, field, outype);
1987 /* PHI resolving breaks the SSA, and must thus be the last
1988 * step before life-range calculation.
1991 static bool ir_block_naive_phi(ir_block *self);
1992 bool ir_function_naive_phi(ir_function *self)
1996 for (i = 0; i < vec_size(self->blocks); ++i)
1998 if (!ir_block_naive_phi(self->blocks[i]))
2004 static bool ir_block_naive_phi(ir_block *self)
2006 size_t i, p; /*, w;*/
2007 /* FIXME: optionally, create_phi can add the phis
2008 * to a list so we don't need to loop through blocks
2009 * - anyway: "don't optimize YET"
2011 for (i = 0; i < vec_size(self->instr); ++i)
2013 ir_instr *instr = self->instr[i];
2014 if (instr->opcode != VINSTR_PHI)
2017 vec_remove(self->instr, i, 1);
2018 --i; /* NOTE: i+1 below */
2020 for (p = 0; p < vec_size(instr->phi); ++p)
2022 ir_value *v = instr->phi[p].value;
2023 ir_block *b = instr->phi[p].from;
2025 if (v->store == store_value &&
2026 vec_size(v->reads) == 1 &&
2027 vec_size(v->writes) == 1)
2029 /* replace the value */
2030 if (!ir_instr_op(v->writes[0], 0, instr->_ops[0], true))
2035 /* force a move instruction */
2036 ir_instr *prevjump = vec_last(b->instr);
2039 instr->_ops[0]->store = store_global;
2040 if (!ir_block_create_store(b, instr->context, instr->_ops[0], v))
2042 instr->_ops[0]->store = store_value;
2043 vec_push(b->instr, prevjump);
2047 ir_instr_delete(instr);
2052 /***********************************************************************
2053 *IR Temp allocation code
2054 * Propagating value life ranges by walking through the function backwards
2055 * until no more changes are made.
2056 * In theory this should happen once more than once for every nested loop
2058 * Though this implementation might run an additional time for if nests.
2061 /* Enumerate instructions used by value's life-ranges
2063 static void ir_block_enumerate(ir_block *self, size_t *_eid)
2067 for (i = 0; i < vec_size(self->instr); ++i)
2069 self->instr[i]->eid = eid++;
2074 /* Enumerate blocks and instructions.
2075 * The block-enumeration is unordered!
2076 * We do not really use the block enumreation, however
2077 * the instruction enumeration is important for life-ranges.
2079 void ir_function_enumerate(ir_function *self)
2082 size_t instruction_id = 0;
2083 for (i = 0; i < vec_size(self->blocks); ++i)
2085 /* each block now gets an additional "entry" instruction id
2086 * we can use to avoid point-life issues
2088 self->blocks[i]->entry_id = instruction_id;
2091 self->blocks[i]->eid = i;
2092 ir_block_enumerate(self->blocks[i], &instruction_id);
2096 /* Local-value allocator
2097 * After finishing creating the liferange of all values used in a function
2098 * we can allocate their global-positions.
2099 * This is the counterpart to register-allocation in register machines.
2106 } function_allocator;
2108 static bool function_allocator_alloc(function_allocator *alloc, ir_value *var)
2111 size_t vsize = ir_value_sizeof(var);
2113 var->code.local = vec_size(alloc->locals);
2115 slot = ir_value_var("reg", store_global, var->vtype);
2119 if (!ir_value_life_merge_into(slot, var))
2122 vec_push(alloc->locals, slot);
2123 vec_push(alloc->sizes, vsize);
2124 vec_push(alloc->unique, var->unique_life);
2129 ir_value_delete(slot);
2133 static bool ir_function_allocator_assign(ir_function *self, function_allocator *alloc, ir_value *v)
2139 return function_allocator_alloc(alloc, v);
2141 for (a = 0; a < vec_size(alloc->locals); ++a)
2143 /* if it's reserved for a unique liferange: skip */
2144 if (alloc->unique[a])
2147 slot = alloc->locals[a];
2149 /* never resize parameters
2150 * will be required later when overlapping temps + locals
2152 if (a < vec_size(self->params) &&
2153 alloc->sizes[a] < ir_value_sizeof(v))
2158 if (ir_values_overlap(v, slot))
2161 if (!ir_value_life_merge_into(slot, v))
2164 /* adjust size for this slot */
2165 if (alloc->sizes[a] < ir_value_sizeof(v))
2166 alloc->sizes[a] = ir_value_sizeof(v);
2171 if (a >= vec_size(alloc->locals)) {
2172 if (!function_allocator_alloc(alloc, v))
2178 bool ir_function_allocate_locals(ir_function *self)
2183 bool opt_gt = OPTS_OPTIMIZATION(OPTIM_GLOBAL_TEMPS);
2187 function_allocator lockalloc, globalloc;
2189 if (!vec_size(self->locals) && !vec_size(self->values))
2192 globalloc.locals = NULL;
2193 globalloc.sizes = NULL;
2194 globalloc.positions = NULL;
2195 globalloc.unique = NULL;
2196 lockalloc.locals = NULL;
2197 lockalloc.sizes = NULL;
2198 lockalloc.positions = NULL;
2199 lockalloc.unique = NULL;
2201 for (i = 0; i < vec_size(self->locals); ++i)
2203 v = self->locals[i];
2204 if ((self->flags & IR_FLAG_MASK_NO_LOCAL_TEMPS) || !OPTS_OPTIMIZATION(OPTIM_LOCAL_TEMPS)) {
2206 v->unique_life = true;
2208 else if (i >= vec_size(self->params))
2211 v->locked = true; /* lock parameters locals */
2212 if (!function_allocator_alloc((v->locked || !opt_gt ? &lockalloc : &globalloc), v))
2215 for (; i < vec_size(self->locals); ++i)
2217 v = self->locals[i];
2218 if (!vec_size(v->life))
2220 if (!ir_function_allocator_assign(self, (v->locked || !opt_gt ? &lockalloc : &globalloc), v))
2224 /* Allocate a slot for any value that still exists */
2225 for (i = 0; i < vec_size(self->values); ++i)
2227 v = self->values[i];
2229 if (!vec_size(v->life))
2232 /* CALL optimization:
2233 * If the value is a parameter-temp: 1 write, 1 read from a CALL
2234 * and it's not "locked", write it to the OFS_PARM directly.
2236 if (OPTS_OPTIMIZATION(OPTIM_CALL_STORES) && !v->locked && !v->unique_life) {
2237 if (vec_size(v->reads) == 1 && vec_size(v->writes) == 1 &&
2238 (v->reads[0]->opcode == VINSTR_NRCALL ||
2239 (v->reads[0]->opcode >= INSTR_CALL0 && v->reads[0]->opcode <= INSTR_CALL8)
2244 ir_instr *call = v->reads[0];
2245 if (!vec_ir_value_find(call->params, v, ¶m)) {
2246 irerror(call->context, "internal error: unlocked parameter %s not found", v->name);
2249 ++opts_optimizationcount[OPTIM_CALL_STORES];
2250 v->callparam = true;
2252 ir_value_code_setaddr(v, OFS_PARM0 + 3*param);
2254 size_t nprotos = vec_size(self->owner->extparam_protos);
2257 if (nprotos > param)
2258 ep = self->owner->extparam_protos[param];
2261 ep = ir_gen_extparam_proto(self->owner);
2262 while (++nprotos <= param)
2263 ep = ir_gen_extparam_proto(self->owner);
2265 ir_instr_op(v->writes[0], 0, ep, true);
2266 call->params[param+8] = ep;
2270 if (vec_size(v->writes) == 1 && v->writes[0]->opcode == INSTR_CALL0)
2272 v->store = store_return;
2273 if (v->members[0]) v->members[0]->store = store_return;
2274 if (v->members[1]) v->members[1]->store = store_return;
2275 if (v->members[2]) v->members[2]->store = store_return;
2276 ++opts_optimizationcount[OPTIM_CALL_STORES];
2281 if (!ir_function_allocator_assign(self, (v->locked || !opt_gt ? &lockalloc : &globalloc), v))
2285 if (!lockalloc.sizes && !globalloc.sizes) {
2288 vec_push(lockalloc.positions, 0);
2289 vec_push(globalloc.positions, 0);
2291 /* Adjust slot positions based on sizes */
2292 if (lockalloc.sizes) {
2293 pos = (vec_size(lockalloc.sizes) ? lockalloc.positions[0] : 0);
2294 for (i = 1; i < vec_size(lockalloc.sizes); ++i)
2296 pos = lockalloc.positions[i-1] + lockalloc.sizes[i-1];
2297 vec_push(lockalloc.positions, pos);
2299 self->allocated_locals = pos + vec_last(lockalloc.sizes);
2301 if (globalloc.sizes) {
2302 pos = (vec_size(globalloc.sizes) ? globalloc.positions[0] : 0);
2303 for (i = 1; i < vec_size(globalloc.sizes); ++i)
2305 pos = globalloc.positions[i-1] + globalloc.sizes[i-1];
2306 vec_push(globalloc.positions, pos);
2308 self->globaltemps = pos + vec_last(globalloc.sizes);
2311 /* Locals need to know their new position */
2312 for (i = 0; i < vec_size(self->locals); ++i) {
2313 v = self->locals[i];
2314 if (v->locked || !opt_gt)
2315 v->code.local = lockalloc.positions[v->code.local];
2317 v->code.local = globalloc.positions[v->code.local];
2319 /* Take over the actual slot positions on values */
2320 for (i = 0; i < vec_size(self->values); ++i) {
2321 v = self->values[i];
2322 if (v->locked || !opt_gt)
2323 v->code.local = lockalloc.positions[v->code.local];
2325 v->code.local = globalloc.positions[v->code.local];
2333 for (i = 0; i < vec_size(lockalloc.locals); ++i)
2334 ir_value_delete(lockalloc.locals[i]);
2335 for (i = 0; i < vec_size(globalloc.locals); ++i)
2336 ir_value_delete(globalloc.locals[i]);
2337 vec_free(globalloc.unique);
2338 vec_free(globalloc.locals);
2339 vec_free(globalloc.sizes);
2340 vec_free(globalloc.positions);
2341 vec_free(lockalloc.unique);
2342 vec_free(lockalloc.locals);
2343 vec_free(lockalloc.sizes);
2344 vec_free(lockalloc.positions);
2348 /* Get information about which operand
2349 * is read from, or written to.
2351 static void ir_op_read_write(int op, size_t *read, size_t *write)
2371 case INSTR_STOREP_F:
2372 case INSTR_STOREP_V:
2373 case INSTR_STOREP_S:
2374 case INSTR_STOREP_ENT:
2375 case INSTR_STOREP_FLD:
2376 case INSTR_STOREP_FNC:
2387 static bool ir_block_living_add_instr(ir_block *self, size_t eid)
2390 const size_t vs = vec_size(self->living);
2391 bool changed = false;
2392 for (i = 0; i != vs; ++i)
2394 if (ir_value_life_merge(self->living[i], eid))
2400 static bool ir_block_living_lock(ir_block *self)
2403 bool changed = false;
2404 for (i = 0; i != vec_size(self->living); ++i)
2406 if (!self->living[i]->locked) {
2407 self->living[i]->locked = true;
2414 static bool ir_block_life_propagate(ir_block *self, bool *changed)
2418 size_t i, o, p, mem, cnt;
2419 /* bitmasks which operands are read from or written to */
2426 vec_free(self->living);
2428 p = vec_size(self->exits);
2429 for (i = 0; i < p; ++i) {
2430 ir_block *prev = self->exits[i];
2431 cnt = vec_size(prev->living);
2432 for (o = 0; o < cnt; ++o) {
2433 if (!vec_ir_value_find(self->living, prev->living[o], NULL))
2434 vec_push(self->living, prev->living[o]);
2438 i = vec_size(self->instr);
2441 instr = self->instr[i];
2443 /* See which operands are read and write operands */
2444 ir_op_read_write(instr->opcode, &read, &write);
2446 /* Go through the 3 main operands
2447 * writes first, then reads
2449 for (o = 0; o < 3; ++o)
2451 if (!instr->_ops[o]) /* no such operand */
2454 value = instr->_ops[o];
2456 /* We only care about locals */
2457 /* we also calculate parameter liferanges so that locals
2458 * can take up parameter slots */
2459 if (value->store != store_value &&
2460 value->store != store_local &&
2461 value->store != store_param)
2464 /* write operands */
2465 /* When we write to a local, we consider it "dead" for the
2466 * remaining upper part of the function, since in SSA a value
2467 * can only be written once (== created)
2472 bool in_living = vec_ir_value_find(self->living, value, &idx);
2475 /* If the value isn't alive it hasn't been read before... */
2476 /* TODO: See if the warning can be emitted during parsing or AST processing
2477 * otherwise have warning printed here.
2478 * IF printing a warning here: include filecontext_t,
2479 * and make sure it's only printed once
2480 * since this function is run multiple times.
2482 /* con_err( "Value only written %s\n", value->name); */
2483 if (ir_value_life_merge(value, instr->eid))
2486 /* since 'living' won't contain it
2487 * anymore, merge the value, since
2490 if (ir_value_life_merge(value, instr->eid))
2493 vec_remove(self->living, idx, 1);
2495 /* Removing a vector removes all members */
2496 for (mem = 0; mem < 3; ++mem) {
2497 if (value->members[mem] && vec_ir_value_find(self->living, value->members[mem], &idx)) {
2498 if (ir_value_life_merge(value->members[mem], instr->eid))
2500 vec_remove(self->living, idx, 1);
2503 /* Removing the last member removes the vector */
2504 if (value->memberof) {
2505 value = value->memberof;
2506 for (mem = 0; mem < 3; ++mem) {
2507 if (value->members[mem] && vec_ir_value_find(self->living, value->members[mem], NULL))
2510 if (mem == 3 && vec_ir_value_find(self->living, value, &idx)) {
2511 if (ir_value_life_merge(value, instr->eid))
2513 vec_remove(self->living, idx, 1);
2519 /* These operations need a special case as they can break when using
2520 * same source and destination operand otherwise, as the engine may
2521 * read the source multiple times. */
2522 if (instr->opcode == INSTR_MUL_VF ||
2523 instr->opcode == VINSTR_BITAND_VF ||
2524 instr->opcode == VINSTR_BITOR_VF ||
2525 instr->opcode == VINSTR_BITXOR ||
2526 instr->opcode == VINSTR_BITXOR_VF ||
2527 instr->opcode == VINSTR_BITXOR_V ||
2528 instr->opcode == VINSTR_CROSS)
2530 value = instr->_ops[2];
2531 /* the float source will get an additional lifetime */
2532 if (ir_value_life_merge(value, instr->eid+1))
2534 if (value->memberof && ir_value_life_merge(value->memberof, instr->eid+1))
2538 if (instr->opcode == INSTR_MUL_FV ||
2539 instr->opcode == INSTR_LOAD_V ||
2540 instr->opcode == VINSTR_BITXOR ||
2541 instr->opcode == VINSTR_BITXOR_VF ||
2542 instr->opcode == VINSTR_BITXOR_V ||
2543 instr->opcode == VINSTR_CROSS)
2545 value = instr->_ops[1];
2546 /* the float source will get an additional lifetime */
2547 if (ir_value_life_merge(value, instr->eid+1))
2549 if (value->memberof && ir_value_life_merge(value->memberof, instr->eid+1))
2553 for (o = 0; o < 3; ++o)
2555 if (!instr->_ops[o]) /* no such operand */
2558 value = instr->_ops[o];
2560 /* We only care about locals */
2561 /* we also calculate parameter liferanges so that locals
2562 * can take up parameter slots */
2563 if (value->store != store_value &&
2564 value->store != store_local &&
2565 value->store != store_param)
2571 if (!vec_ir_value_find(self->living, value, NULL))
2572 vec_push(self->living, value);
2573 /* reading adds the full vector */
2574 if (value->memberof && !vec_ir_value_find(self->living, value->memberof, NULL))
2575 vec_push(self->living, value->memberof);
2576 for (mem = 0; mem < 3; ++mem) {
2577 if (value->members[mem] && !vec_ir_value_find(self->living, value->members[mem], NULL))
2578 vec_push(self->living, value->members[mem]);
2582 /* PHI operands are always read operands */
2583 for (p = 0; p < vec_size(instr->phi); ++p)
2585 value = instr->phi[p].value;
2586 if (!vec_ir_value_find(self->living, value, NULL))
2587 vec_push(self->living, value);
2588 /* reading adds the full vector */
2589 if (value->memberof && !vec_ir_value_find(self->living, value->memberof, NULL))
2590 vec_push(self->living, value->memberof);
2591 for (mem = 0; mem < 3; ++mem) {
2592 if (value->members[mem] && !vec_ir_value_find(self->living, value->members[mem], NULL))
2593 vec_push(self->living, value->members[mem]);
2597 /* on a call, all these values must be "locked" */
2598 if (instr->opcode >= INSTR_CALL0 && instr->opcode <= INSTR_CALL8) {
2599 if (ir_block_living_lock(self))
2602 /* call params are read operands too */
2603 for (p = 0; p < vec_size(instr->params); ++p)
2605 value = instr->params[p];
2606 if (!vec_ir_value_find(self->living, value, NULL))
2607 vec_push(self->living, value);
2608 /* reading adds the full vector */
2609 if (value->memberof && !vec_ir_value_find(self->living, value->memberof, NULL))
2610 vec_push(self->living, value->memberof);
2611 for (mem = 0; mem < 3; ++mem) {
2612 if (value->members[mem] && !vec_ir_value_find(self->living, value->members[mem], NULL))
2613 vec_push(self->living, value->members[mem]);
2618 if (ir_block_living_add_instr(self, instr->eid))
2621 /* the "entry" instruction ID */
2622 if (ir_block_living_add_instr(self, self->entry_id))
2628 bool ir_function_calculate_liferanges(ir_function *self)
2633 /* parameters live at 0 */
2634 for (i = 0; i < vec_size(self->params); ++i)
2635 if (!ir_value_life_merge(self->locals[i], 0))
2636 compile_error(self->context, "internal error: failed value-life merging");
2641 i = vec_size(self->blocks);
2643 ir_block_life_propagate(self->blocks[i], &changed);
2647 if (vec_size(self->blocks)) {
2648 ir_block *block = self->blocks[0];
2649 for (i = 0; i < vec_size(block->living); ++i) {
2650 ir_value *v = block->living[i];
2651 if (v->store != store_local)
2653 if (v->vtype == TYPE_VECTOR)
2655 self->flags |= IR_FLAG_HAS_UNINITIALIZED;
2656 /* find the instruction reading from it */
2657 for (s = 0; s < vec_size(v->reads); ++s) {
2658 if (v->reads[s]->eid == v->life[0].end)
2661 if (s < vec_size(v->reads)) {
2662 if (irwarning(v->context, WARN_USED_UNINITIALIZED,
2663 "variable `%s` may be used uninitialized in this function\n"
2666 v->reads[s]->context.file, v->reads[s]->context.line)
2674 ir_value *vec = v->memberof;
2675 for (s = 0; s < vec_size(vec->reads); ++s) {
2676 if (vec->reads[s]->eid == v->life[0].end)
2679 if (s < vec_size(vec->reads)) {
2680 if (irwarning(v->context, WARN_USED_UNINITIALIZED,
2681 "variable `%s` may be used uninitialized in this function\n"
2684 vec->reads[s]->context.file, vec->reads[s]->context.line)
2692 if (irwarning(v->context, WARN_USED_UNINITIALIZED,
2693 "variable `%s` may be used uninitialized in this function", v->name))
2702 /***********************************************************************
2705 * Since the IR has the convention of putting 'write' operands
2706 * at the beginning, we have to rotate the operands of instructions
2707 * properly in order to generate valid QCVM code.
2709 * Having destinations at a fixed position is more convenient. In QC
2710 * this is *mostly* OPC, but FTE adds at least 2 instructions which
2711 * read from from OPA, and store to OPB rather than OPC. Which is
2712 * partially the reason why the implementation of these instructions
2713 * in darkplaces has been delayed for so long.
2715 * Breaking conventions is annoying...
2717 static bool ir_builder_gen_global(ir_builder *self, ir_value *global, bool islocal);
2719 static bool gen_global_field(code_t *code, ir_value *global)
2721 if (global->hasvalue)
2723 ir_value *fld = global->constval.vpointer;
2725 irerror(global->context, "Invalid field constant with no field: %s", global->name);
2729 /* copy the field's value */
2730 ir_value_code_setaddr(global, vec_size(code->globals));
2731 vec_push(code->globals, fld->code.fieldaddr);
2732 if (global->fieldtype == TYPE_VECTOR) {
2733 vec_push(code->globals, fld->code.fieldaddr+1);
2734 vec_push(code->globals, fld->code.fieldaddr+2);
2739 ir_value_code_setaddr(global, vec_size(code->globals));
2740 vec_push(code->globals, 0);
2741 if (global->fieldtype == TYPE_VECTOR) {
2742 vec_push(code->globals, 0);
2743 vec_push(code->globals, 0);
2746 if (global->code.globaladdr < 0)
2751 static bool gen_global_pointer(code_t *code, ir_value *global)
2753 if (global->hasvalue)
2755 ir_value *target = global->constval.vpointer;
2757 irerror(global->context, "Invalid pointer constant: %s", global->name);
2758 /* NULL pointers are pointing to the NULL constant, which also
2759 * sits at address 0, but still has an ir_value for itself.
2764 /* Here, relocations ARE possible - in fteqcc-enhanced-qc:
2765 * void() foo; <- proto
2766 * void() *fooptr = &foo;
2767 * void() foo = { code }
2769 if (!target->code.globaladdr) {
2770 /* FIXME: Check for the constant nullptr ir_value!
2771 * because then code.globaladdr being 0 is valid.
2773 irerror(global->context, "FIXME: Relocation support");
2777 ir_value_code_setaddr(global, vec_size(code->globals));
2778 vec_push(code->globals, target->code.globaladdr);
2782 ir_value_code_setaddr(global, vec_size(code->globals));
2783 vec_push(code->globals, 0);
2785 if (global->code.globaladdr < 0)
2790 static bool gen_blocks_recursive(code_t *code, ir_function *func, ir_block *block)
2792 prog_section_statement_t stmt;
2801 block->generated = true;
2802 block->code_start = vec_size(code->statements);
2803 for (i = 0; i < vec_size(block->instr); ++i)
2805 instr = block->instr[i];
2807 if (instr->opcode == VINSTR_PHI) {
2808 irerror(block->context, "cannot generate virtual instruction (phi)");
2812 if (instr->opcode == VINSTR_JUMP) {
2813 target = instr->bops[0];
2814 /* for uncoditional jumps, if the target hasn't been generated
2815 * yet, we generate them right here.
2817 if (!target->generated)
2818 return gen_blocks_recursive(code, func, target);
2820 /* otherwise we generate a jump instruction */
2821 stmt.opcode = INSTR_GOTO;
2822 stmt.o1.s1 = (target->code_start) - vec_size(code->statements);
2825 if (stmt.o1.s1 != 1)
2826 code_push_statement(code, &stmt, instr->context);
2828 /* no further instructions can be in this block */
2832 if (instr->opcode == VINSTR_BITXOR) {
2833 stmt.opcode = INSTR_BITOR;
2834 stmt.o1.s1 = ir_value_code_addr(instr->_ops[1]);
2835 stmt.o2.s1 = ir_value_code_addr(instr->_ops[2]);
2836 stmt.o3.s1 = ir_value_code_addr(instr->_ops[0]);
2837 code_push_statement(code, &stmt, instr->context);
2838 stmt.opcode = INSTR_BITAND;
2839 stmt.o1.s1 = ir_value_code_addr(instr->_ops[1]);
2840 stmt.o2.s1 = ir_value_code_addr(instr->_ops[2]);
2841 stmt.o3.s1 = ir_value_code_addr(func->owner->vinstr_temp[0]);
2842 code_push_statement(code, &stmt, instr->context);
2843 stmt.opcode = INSTR_SUB_F;
2844 stmt.o1.s1 = ir_value_code_addr(instr->_ops[0]);
2845 stmt.o2.s1 = ir_value_code_addr(func->owner->vinstr_temp[0]);
2846 stmt.o3.s1 = ir_value_code_addr(instr->_ops[0]);
2847 code_push_statement(code, &stmt, instr->context);
2849 /* instruction generated */
2853 if (instr->opcode == VINSTR_BITAND_V) {
2854 stmt.opcode = INSTR_BITAND;
2855 stmt.o1.s1 = ir_value_code_addr(instr->_ops[1]);
2856 stmt.o2.s1 = ir_value_code_addr(instr->_ops[2]);
2857 stmt.o3.s1 = ir_value_code_addr(instr->_ops[0]);
2858 code_push_statement(code, &stmt, instr->context);
2862 code_push_statement(code, &stmt, instr->context);
2866 code_push_statement(code, &stmt, instr->context);
2868 /* instruction generated */
2872 if (instr->opcode == VINSTR_BITOR_V) {
2873 stmt.opcode = INSTR_BITOR;
2874 stmt.o1.s1 = ir_value_code_addr(instr->_ops[1]);
2875 stmt.o2.s1 = ir_value_code_addr(instr->_ops[2]);
2876 stmt.o3.s1 = ir_value_code_addr(instr->_ops[0]);
2877 code_push_statement(code, &stmt, instr->context);
2881 code_push_statement(code, &stmt, instr->context);
2885 code_push_statement(code, &stmt, instr->context);
2887 /* instruction generated */
2891 if (instr->opcode == VINSTR_BITXOR_V) {
2892 for (j = 0; j < 3; ++j) {
2893 stmt.opcode = INSTR_BITOR;
2894 stmt.o1.s1 = ir_value_code_addr(instr->_ops[1]) + j;
2895 stmt.o2.s1 = ir_value_code_addr(instr->_ops[2]) + j;
2896 stmt.o3.s1 = ir_value_code_addr(instr->_ops[0]) + j;
2897 code_push_statement(code, &stmt, instr->context);
2898 stmt.opcode = INSTR_BITAND;
2899 stmt.o1.s1 = ir_value_code_addr(instr->_ops[1]) + j;
2900 stmt.o2.s1 = ir_value_code_addr(instr->_ops[2]) + j;
2901 stmt.o3.s1 = ir_value_code_addr(func->owner->vinstr_temp[0]) + j;
2902 code_push_statement(code, &stmt, instr->context);
2904 stmt.opcode = INSTR_SUB_V;
2905 stmt.o1.s1 = ir_value_code_addr(instr->_ops[0]);
2906 stmt.o2.s1 = ir_value_code_addr(func->owner->vinstr_temp[0]);
2907 stmt.o3.s1 = ir_value_code_addr(instr->_ops[0]);
2908 code_push_statement(code, &stmt, instr->context);
2910 /* instruction generated */
2914 if (instr->opcode == VINSTR_BITAND_VF) {
2915 stmt.opcode = INSTR_BITAND;
2916 stmt.o1.s1 = ir_value_code_addr(instr->_ops[1]);
2917 stmt.o2.s1 = ir_value_code_addr(instr->_ops[2]);
2918 stmt.o3.s1 = ir_value_code_addr(instr->_ops[0]);
2919 code_push_statement(code, &stmt, instr->context);
2922 code_push_statement(code, &stmt, instr->context);
2925 code_push_statement(code, &stmt, instr->context);
2927 /* instruction generated */
2931 if (instr->opcode == VINSTR_BITOR_VF) {
2932 stmt.opcode = INSTR_BITOR;
2933 stmt.o1.s1 = ir_value_code_addr(instr->_ops[1]);
2934 stmt.o2.s1 = ir_value_code_addr(instr->_ops[2]);
2935 stmt.o3.s1 = ir_value_code_addr(instr->_ops[0]);
2936 code_push_statement(code, &stmt, instr->context);
2939 code_push_statement(code, &stmt, instr->context);
2942 code_push_statement(code, &stmt, instr->context);
2944 /* instruction generated */
2948 if (instr->opcode == VINSTR_BITXOR_VF) {
2949 for (j = 0; j < 3; ++j) {
2950 stmt.opcode = INSTR_BITOR;
2951 stmt.o1.s1 = ir_value_code_addr(instr->_ops[1]) + j;
2952 stmt.o2.s1 = ir_value_code_addr(instr->_ops[2]);
2953 stmt.o3.s1 = ir_value_code_addr(instr->_ops[0]) + j;
2954 code_push_statement(code, &stmt, instr->context);
2955 stmt.opcode = INSTR_BITAND;
2956 stmt.o1.s1 = ir_value_code_addr(instr->_ops[1]) + j;
2957 stmt.o2.s1 = ir_value_code_addr(instr->_ops[2]);
2958 stmt.o3.s1 = ir_value_code_addr(func->owner->vinstr_temp[0]) + j;
2959 code_push_statement(code, &stmt, instr->context);
2961 stmt.opcode = INSTR_SUB_V;
2962 stmt.o1.s1 = ir_value_code_addr(instr->_ops[0]);
2963 stmt.o2.s1 = ir_value_code_addr(func->owner->vinstr_temp[0]);
2964 stmt.o3.s1 = ir_value_code_addr(instr->_ops[0]);
2965 code_push_statement(code, &stmt, instr->context);
2967 /* instruction generated */
2971 if (instr->opcode == VINSTR_CROSS) {
2972 stmt.opcode = INSTR_MUL_F;
2973 for (j = 0; j < 3; ++j) {
2974 stmt.o1.s1 = ir_value_code_addr(instr->_ops[1]) + (j + 1) % 3;
2975 stmt.o2.s1 = ir_value_code_addr(instr->_ops[2]) + (j + 2) % 3;
2976 stmt.o3.s1 = ir_value_code_addr(instr->_ops[0]) + j;
2977 code_push_statement(code, &stmt, instr->context);
2978 stmt.o1.s1 = ir_value_code_addr(instr->_ops[1]) + (j + 2) % 3;
2979 stmt.o2.s1 = ir_value_code_addr(instr->_ops[2]) + (j + 1) % 3;
2980 stmt.o3.s1 = ir_value_code_addr(func->owner->vinstr_temp[0]) + j;
2981 code_push_statement(code, &stmt, instr->context);
2983 stmt.opcode = INSTR_SUB_V;
2984 stmt.o1.s1 = ir_value_code_addr(instr->_ops[0]);
2985 stmt.o2.s1 = ir_value_code_addr(func->owner->vinstr_temp[0]);
2986 stmt.o3.s1 = ir_value_code_addr(instr->_ops[0]);
2987 code_push_statement(code, &stmt, instr->context);
2989 /* instruction generated */
2993 if (instr->opcode == VINSTR_COND) {
2994 ontrue = instr->bops[0];
2995 onfalse = instr->bops[1];
2996 /* TODO: have the AST signal which block should
2997 * come first: eg. optimize IFs without ELSE...
3000 stmt.o1.u1 = ir_value_code_addr(instr->_ops[0]);
3004 if (ontrue->generated) {
3005 stmt.opcode = INSTR_IF;
3006 stmt.o2.s1 = (ontrue->code_start) - vec_size(code->statements);
3007 if (stmt.o2.s1 != 1)
3008 code_push_statement(code, &stmt, instr->context);
3010 if (onfalse->generated) {
3011 stmt.opcode = INSTR_IFNOT;
3012 stmt.o2.s1 = (onfalse->code_start) - vec_size(code->statements);
3013 if (stmt.o2.s1 != 1)
3014 code_push_statement(code, &stmt, instr->context);
3016 if (!ontrue->generated) {
3017 if (onfalse->generated)
3018 return gen_blocks_recursive(code, func, ontrue);
3020 if (!onfalse->generated) {
3021 if (ontrue->generated)
3022 return gen_blocks_recursive(code, func, onfalse);
3024 /* neither ontrue nor onfalse exist */
3025 stmt.opcode = INSTR_IFNOT;
3026 if (!instr->likely) {
3027 /* Honor the likelyhood hint */
3028 ir_block *tmp = onfalse;
3029 stmt.opcode = INSTR_IF;
3033 stidx = vec_size(code->statements);
3034 code_push_statement(code, &stmt, instr->context);
3035 /* on false we jump, so add ontrue-path */
3036 if (!gen_blocks_recursive(code, func, ontrue))
3038 /* fixup the jump address */
3039 code->statements[stidx].o2.s1 = vec_size(code->statements) - stidx;
3040 /* generate onfalse path */
3041 if (onfalse->generated) {
3042 /* fixup the jump address */
3043 code->statements[stidx].o2.s1 = (onfalse->code_start) - (stidx);
3044 if (stidx+2 == vec_size(code->statements) && code->statements[stidx].o2.s1 == 1) {
3045 code->statements[stidx] = code->statements[stidx+1];
3046 if (code->statements[stidx].o1.s1 < 0)
3047 code->statements[stidx].o1.s1++;
3048 code_pop_statement(code);
3050 stmt.opcode = vec_last(code->statements).opcode;
3051 if (stmt.opcode == INSTR_GOTO ||
3052 stmt.opcode == INSTR_IF ||
3053 stmt.opcode == INSTR_IFNOT ||
3054 stmt.opcode == INSTR_RETURN ||
3055 stmt.opcode == INSTR_DONE)
3057 /* no use jumping from here */
3060 /* may have been generated in the previous recursive call */
3061 stmt.opcode = INSTR_GOTO;
3062 stmt.o1.s1 = (onfalse->code_start) - vec_size(code->statements);
3065 if (stmt.o1.s1 != 1)
3066 code_push_statement(code, &stmt, instr->context);
3069 else if (stidx+2 == vec_size(code->statements) && code->statements[stidx].o2.s1 == 1) {
3070 code->statements[stidx] = code->statements[stidx+1];
3071 if (code->statements[stidx].o1.s1 < 0)
3072 code->statements[stidx].o1.s1++;
3073 code_pop_statement(code);
3075 /* if not, generate now */
3076 return gen_blocks_recursive(code, func, onfalse);
3079 if ( (instr->opcode >= INSTR_CALL0 && instr->opcode <= INSTR_CALL8)
3080 || instr->opcode == VINSTR_NRCALL)
3085 first = vec_size(instr->params);
3088 for (p = 0; p < first; ++p)
3090 ir_value *param = instr->params[p];
3091 if (param->callparam)
3094 stmt.opcode = INSTR_STORE_F;
3097 if (param->vtype == TYPE_FIELD)
3098 stmt.opcode = field_store_instr[param->fieldtype];
3099 else if (param->vtype == TYPE_NIL)
3100 stmt.opcode = INSTR_STORE_V;
3102 stmt.opcode = type_store_instr[param->vtype];
3103 stmt.o1.u1 = ir_value_code_addr(param);
3104 stmt.o2.u1 = OFS_PARM0 + 3 * p;
3105 code_push_statement(code, &stmt, instr->context);
3107 /* Now handle extparams */
3108 first = vec_size(instr->params);
3109 for (; p < first; ++p)
3111 ir_builder *ir = func->owner;
3112 ir_value *param = instr->params[p];
3113 ir_value *targetparam;
3115 if (param->callparam)
3118 if (p-8 >= vec_size(ir->extparams))
3119 ir_gen_extparam(ir);
3121 targetparam = ir->extparams[p-8];
3123 stmt.opcode = INSTR_STORE_F;
3126 if (param->vtype == TYPE_FIELD)
3127 stmt.opcode = field_store_instr[param->fieldtype];
3128 else if (param->vtype == TYPE_NIL)
3129 stmt.opcode = INSTR_STORE_V;
3131 stmt.opcode = type_store_instr[param->vtype];
3132 stmt.o1.u1 = ir_value_code_addr(param);
3133 stmt.o2.u1 = ir_value_code_addr(targetparam);
3134 code_push_statement(code, &stmt, instr->context);
3137 stmt.opcode = INSTR_CALL0 + vec_size(instr->params);
3138 if (stmt.opcode > INSTR_CALL8)
3139 stmt.opcode = INSTR_CALL8;
3140 stmt.o1.u1 = ir_value_code_addr(instr->_ops[1]);
3143 code_push_statement(code, &stmt, instr->context);
3145 retvalue = instr->_ops[0];
3146 if (retvalue && retvalue->store != store_return &&
3147 (retvalue->store == store_global || vec_size(retvalue->life)))
3149 /* not to be kept in OFS_RETURN */
3150 if (retvalue->vtype == TYPE_FIELD && OPTS_FLAG(ADJUST_VECTOR_FIELDS))
3151 stmt.opcode = field_store_instr[retvalue->fieldtype];
3153 stmt.opcode = type_store_instr[retvalue->vtype];
3154 stmt.o1.u1 = OFS_RETURN;
3155 stmt.o2.u1 = ir_value_code_addr(retvalue);
3157 code_push_statement(code, &stmt, instr->context);
3162 if (instr->opcode == INSTR_STATE) {
3163 irerror(block->context, "TODO: state instruction");
3167 stmt.opcode = instr->opcode;
3172 /* This is the general order of operands */
3174 stmt.o3.u1 = ir_value_code_addr(instr->_ops[0]);
3177 stmt.o1.u1 = ir_value_code_addr(instr->_ops[1]);
3180 stmt.o2.u1 = ir_value_code_addr(instr->_ops[2]);
3182 if (stmt.opcode == INSTR_RETURN || stmt.opcode == INSTR_DONE)
3184 stmt.o1.u1 = stmt.o3.u1;
3187 else if ((stmt.opcode >= INSTR_STORE_F &&
3188 stmt.opcode <= INSTR_STORE_FNC) ||
3189 (stmt.opcode >= INSTR_STOREP_F &&
3190 stmt.opcode <= INSTR_STOREP_FNC))
3192 /* 2-operand instructions with A -> B */
3193 stmt.o2.u1 = stmt.o3.u1;
3196 /* tiny optimization, don't output
3199 if (stmt.o2.u1 == stmt.o1.u1 &&
3200 OPTS_OPTIMIZATION(OPTIM_PEEPHOLE))
3202 ++opts_optimizationcount[OPTIM_PEEPHOLE];
3206 code_push_statement(code, &stmt, instr->context);
3211 static bool gen_function_code(code_t *code, ir_function *self)
3214 prog_section_statement_t stmt, *retst;
3216 /* Starting from entry point, we generate blocks "as they come"
3217 * for now. Dead blocks will not be translated obviously.
3219 if (!vec_size(self->blocks)) {
3220 irerror(self->context, "Function '%s' declared without body.", self->name);
3224 block = self->blocks[0];
3225 if (block->generated)
3228 if (!gen_blocks_recursive(code, self, block)) {
3229 irerror(self->context, "failed to generate blocks for '%s'", self->name);
3233 /* code_write and qcvm -disasm need to know that the function ends here */
3234 retst = &vec_last(code->statements);
3235 if (OPTS_OPTIMIZATION(OPTIM_VOID_RETURN) &&
3236 self->outtype == TYPE_VOID &&
3237 retst->opcode == INSTR_RETURN &&
3238 !retst->o1.u1 && !retst->o2.u1 && !retst->o3.u1)
3240 retst->opcode = INSTR_DONE;
3241 ++opts_optimizationcount[OPTIM_VOID_RETURN];
3245 stmt.opcode = INSTR_DONE;
3249 last.line = vec_last(code->linenums);
3250 last.column = vec_last(code->columnnums);
3252 code_push_statement(code, &stmt, last);
3257 static qcint_t ir_builder_filestring(ir_builder *ir, const char *filename)
3259 /* NOTE: filename pointers are copied, we never strdup them,
3260 * thus we can use pointer-comparison to find the string.
3265 for (i = 0; i < vec_size(ir->filenames); ++i) {
3266 if (ir->filenames[i] == filename)
3267 return ir->filestrings[i];
3270 str = code_genstring(ir->code, filename);
3271 vec_push(ir->filenames, filename);
3272 vec_push(ir->filestrings, str);
3276 static bool gen_global_function(ir_builder *ir, ir_value *global)
3278 prog_section_function_t fun;
3283 if (!global->hasvalue || (!global->constval.vfunc))
3285 irerror(global->context, "Invalid state of function-global: not constant: %s", global->name);
3289 irfun = global->constval.vfunc;
3291 fun.name = global->code.name;
3292 fun.file = ir_builder_filestring(ir, global->context.file);
3293 fun.profile = 0; /* always 0 */
3294 fun.nargs = vec_size(irfun->params);
3298 for (i = 0;i < 8; ++i) {
3299 if ((int32_t)i >= fun.nargs)
3302 fun.argsize[i] = type_sizeof_[irfun->params[i]];
3306 fun.locals = irfun->allocated_locals;
3309 fun.entry = irfun->builtin+1;
3311 irfun->code_function_def = vec_size(ir->code->functions);
3312 fun.entry = vec_size(ir->code->statements);
3315 vec_push(ir->code->functions, fun);
3319 static ir_value* ir_gen_extparam_proto(ir_builder *ir)
3324 platform_snprintf(name, sizeof(name), "EXTPARM#%i", (int)(vec_size(ir->extparam_protos)));
3325 global = ir_value_var(name, store_global, TYPE_VECTOR);
3327 vec_push(ir->extparam_protos, global);
3331 static void ir_gen_extparam(ir_builder *ir)
3333 prog_section_def_t def;
3336 if (vec_size(ir->extparam_protos) < vec_size(ir->extparams)+1)
3337 global = ir_gen_extparam_proto(ir);
3339 global = ir->extparam_protos[vec_size(ir->extparams)];
3341 def.name = code_genstring(ir->code, global->name);
3342 def.type = TYPE_VECTOR;
3343 def.offset = vec_size(ir->code->globals);
3345 vec_push(ir->code->defs, def);
3347 ir_value_code_setaddr(global, def.offset);
3349 vec_push(ir->code->globals, 0);
3350 vec_push(ir->code->globals, 0);
3351 vec_push(ir->code->globals, 0);
3353 vec_push(ir->extparams, global);
3356 static bool gen_function_extparam_copy(code_t *code, ir_function *self)
3358 size_t i, ext, numparams;
3360 ir_builder *ir = self->owner;
3362 prog_section_statement_t stmt;
3364 numparams = vec_size(self->params);
3368 stmt.opcode = INSTR_STORE_F;
3370 for (i = 8; i < numparams; ++i) {
3372 if (ext >= vec_size(ir->extparams))
3373 ir_gen_extparam(ir);
3375 ep = ir->extparams[ext];
3377 stmt.opcode = type_store_instr[self->locals[i]->vtype];
3378 if (self->locals[i]->vtype == TYPE_FIELD &&
3379 self->locals[i]->fieldtype == TYPE_VECTOR)
3381 stmt.opcode = INSTR_STORE_V;
3383 stmt.o1.u1 = ir_value_code_addr(ep);
3384 stmt.o2.u1 = ir_value_code_addr(self->locals[i]);
3385 code_push_statement(code, &stmt, self->context);
3391 static bool gen_function_varargs_copy(code_t *code, ir_function *self)
3393 size_t i, ext, numparams, maxparams;
3395 ir_builder *ir = self->owner;
3397 prog_section_statement_t stmt;
3399 numparams = vec_size(self->params);
3403 stmt.opcode = INSTR_STORE_V;
3405 maxparams = numparams + self->max_varargs;
3406 for (i = numparams; i < maxparams; ++i) {
3408 stmt.o1.u1 = OFS_PARM0 + 3*i;
3409 stmt.o2.u1 = ir_value_code_addr(self->locals[i]);
3410 code_push_statement(code, &stmt, self->context);
3414 while (ext >= vec_size(ir->extparams))
3415 ir_gen_extparam(ir);
3417 ep = ir->extparams[ext];
3419 stmt.o1.u1 = ir_value_code_addr(ep);
3420 stmt.o2.u1 = ir_value_code_addr(self->locals[i]);
3421 code_push_statement(code, &stmt, self->context);
3427 static bool gen_function_locals(ir_builder *ir, ir_value *global)
3429 prog_section_function_t *def;
3432 uint32_t firstlocal, firstglobal;
3434 irfun = global->constval.vfunc;
3435 def = ir->code->functions + irfun->code_function_def;
3437 if (OPTS_OPTION_BOOL(OPTION_G) ||
3438 !OPTS_OPTIMIZATION(OPTIM_OVERLAP_LOCALS) ||
3439 (irfun->flags & IR_FLAG_MASK_NO_OVERLAP))
3441 firstlocal = def->firstlocal = vec_size(ir->code->globals);
3443 firstlocal = def->firstlocal = ir->first_common_local;
3444 ++opts_optimizationcount[OPTIM_OVERLAP_LOCALS];
3447 firstglobal = (OPTS_OPTIMIZATION(OPTIM_GLOBAL_TEMPS) ? ir->first_common_globaltemp : firstlocal);
3449 for (i = vec_size(ir->code->globals); i < firstlocal + irfun->allocated_locals; ++i)
3450 vec_push(ir->code->globals, 0);
3451 for (i = 0; i < vec_size(irfun->locals); ++i) {
3452 ir_value *v = irfun->locals[i];
3453 if (v->locked || !OPTS_OPTIMIZATION(OPTIM_GLOBAL_TEMPS)) {
3454 ir_value_code_setaddr(v, firstlocal + v->code.local);
3455 if (!ir_builder_gen_global(ir, irfun->locals[i], true)) {
3456 irerror(irfun->locals[i]->context, "failed to generate local %s", irfun->locals[i]->name);
3461 ir_value_code_setaddr(v, firstglobal + v->code.local);
3463 for (i = 0; i < vec_size(irfun->values); ++i)
3465 ir_value *v = irfun->values[i];
3469 ir_value_code_setaddr(v, firstlocal + v->code.local);
3471 ir_value_code_setaddr(v, firstglobal + v->code.local);
3476 static bool gen_global_function_code(ir_builder *ir, ir_value *global)
3478 prog_section_function_t *fundef;
3483 irfun = global->constval.vfunc;
3485 if (global->cvq == CV_NONE) {
3486 if (irwarning(global->context, WARN_IMPLICIT_FUNCTION_POINTER,
3487 "function `%s` has no body and in QC implicitly becomes a function-pointer",
3490 /* Not bailing out just now. If this happens a lot you don't want to have
3491 * to rerun gmqcc for each such function.
3497 /* this was a function pointer, don't generate code for those */
3505 * If there is no definition and the thing is eraseable, we can ignore
3506 * outputting the function to begin with.
3508 if (global->flags & IR_FLAG_ERASEABLE && irfun->code_function_def < 0) {
3512 if (irfun->code_function_def < 0) {
3513 irerror(irfun->context, "`%s`: IR global wasn't generated, failed to access function-def", irfun->name);
3516 fundef = &ir->code->functions[irfun->code_function_def];
3518 fundef->entry = vec_size(ir->code->statements);
3519 if (!gen_function_locals(ir, global)) {
3520 irerror(irfun->context, "Failed to generate locals for function %s", irfun->name);
3523 if (!gen_function_extparam_copy(ir->code, irfun)) {
3524 irerror(irfun->context, "Failed to generate extparam-copy code for function %s", irfun->name);
3527 if (irfun->max_varargs && !gen_function_varargs_copy(ir->code, irfun)) {
3528 irerror(irfun->context, "Failed to generate vararg-copy code for function %s", irfun->name);
3531 if (!gen_function_code(ir->code, irfun)) {
3532 irerror(irfun->context, "Failed to generate code for function %s", irfun->name);
3538 static void gen_vector_defs(code_t *code, prog_section_def_t def, const char *name)
3543 if (!name || name[0] == '#' || OPTS_FLAG(SINGLE_VECTOR_DEFS))
3546 def.type = TYPE_FLOAT;
3550 component = (char*)mem_a(len+3);
3551 memcpy(component, name, len);
3553 component[len-0] = 0;
3554 component[len-2] = '_';
3556 component[len-1] = 'x';
3558 for (i = 0; i < 3; ++i) {
3559 def.name = code_genstring(code, component);
3560 vec_push(code->defs, def);
3568 static void gen_vector_fields(code_t *code, prog_section_field_t fld, const char *name)
3573 if (!name || OPTS_FLAG(SINGLE_VECTOR_DEFS))
3576 fld.type = TYPE_FLOAT;
3580 component = (char*)mem_a(len+3);
3581 memcpy(component, name, len);
3583 component[len-0] = 0;
3584 component[len-2] = '_';
3586 component[len-1] = 'x';
3588 for (i = 0; i < 3; ++i) {
3589 fld.name = code_genstring(code, component);
3590 vec_push(code->fields, fld);
3598 static bool ir_builder_gen_global(ir_builder *self, ir_value *global, bool islocal)
3602 prog_section_def_t def;
3603 bool pushdef = opts.optimizeoff;
3605 def.type = global->vtype;
3606 def.offset = vec_size(self->code->globals);
3608 if (OPTS_OPTION_BOOL(OPTION_G) || !islocal)
3613 * if we're eraseable and the function isn't referenced ignore outputting
3616 if (global->flags & IR_FLAG_ERASEABLE && vec_size(global->reads) == 0) {
3620 if (OPTS_OPTIMIZATION(OPTIM_STRIP_CONSTANT_NAMES) &&
3621 !(global->flags & IR_FLAG_INCLUDE_DEF) &&
3622 (global->name[0] == '#' || global->cvq == CV_CONST))
3628 if (global->name[0] == '#') {
3629 if (!self->str_immediate)
3630 self->str_immediate = code_genstring(self->code, "IMMEDIATE");
3631 def.name = global->code.name = self->str_immediate;
3634 def.name = global->code.name = code_genstring(self->code, global->name);
3639 def.offset = ir_value_code_addr(global);
3640 vec_push(self->code->defs, def);
3641 if (global->vtype == TYPE_VECTOR)
3642 gen_vector_defs(self->code, def, global->name);
3643 else if (global->vtype == TYPE_FIELD && global->fieldtype == TYPE_VECTOR)
3644 gen_vector_defs(self->code, def, global->name);
3651 switch (global->vtype)
3654 if (!strcmp(global->name, "end_sys_globals")) {
3655 /* TODO: remember this point... all the defs before this one
3656 * should be checksummed and added to progdefs.h when we generate it.
3659 else if (!strcmp(global->name, "end_sys_fields")) {
3660 /* TODO: same as above but for entity-fields rather than globsl
3663 else if(irwarning(global->context, WARN_VOID_VARIABLES, "unrecognized variable of type void `%s`",
3666 /* Not bailing out */
3669 /* I'd argue setting it to 0 is sufficient, but maybe some depend on knowing how far
3670 * the system fields actually go? Though the engine knows this anyway...
3671 * Maybe this could be an -foption
3672 * fteqcc creates data for end_sys_* - of size 1, so let's do the same
3674 ir_value_code_setaddr(global, vec_size(self->code->globals));
3675 vec_push(self->code->globals, 0);
3677 if (pushdef) vec_push(self->code->defs, def);
3680 if (pushdef) vec_push(self->code->defs, def);
3681 return gen_global_pointer(self->code, global);
3684 vec_push(self->code->defs, def);
3685 if (global->fieldtype == TYPE_VECTOR)
3686 gen_vector_defs(self->code, def, global->name);
3688 return gen_global_field(self->code, global);
3693 ir_value_code_setaddr(global, vec_size(self->code->globals));
3694 if (global->hasvalue) {
3695 iptr = (int32_t*)&global->constval.ivec[0];
3696 vec_push(self->code->globals, *iptr);
3698 vec_push(self->code->globals, 0);
3700 if (!islocal && global->cvq != CV_CONST)
3701 def.type |= DEF_SAVEGLOBAL;
3702 if (pushdef) vec_push(self->code->defs, def);
3704 return global->code.globaladdr >= 0;
3708 ir_value_code_setaddr(global, vec_size(self->code->globals));
3709 if (global->hasvalue) {
3710 uint32_t load = code_genstring(self->code, global->constval.vstring);
3711 vec_push(self->code->globals, load);
3713 vec_push(self->code->globals, 0);
3715 if (!islocal && global->cvq != CV_CONST)
3716 def.type |= DEF_SAVEGLOBAL;
3717 if (pushdef) vec_push(self->code->defs, def);
3718 return global->code.globaladdr >= 0;
3723 ir_value_code_setaddr(global, vec_size(self->code->globals));
3724 if (global->hasvalue) {
3725 iptr = (int32_t*)&global->constval.ivec[0];
3726 vec_push(self->code->globals, iptr[0]);
3727 if (global->code.globaladdr < 0)
3729 for (d = 1; d < type_sizeof_[global->vtype]; ++d) {
3730 vec_push(self->code->globals, iptr[d]);
3733 vec_push(self->code->globals, 0);
3734 if (global->code.globaladdr < 0)
3736 for (d = 1; d < type_sizeof_[global->vtype]; ++d) {
3737 vec_push(self->code->globals, 0);
3740 if (!islocal && global->cvq != CV_CONST)
3741 def.type |= DEF_SAVEGLOBAL;
3744 vec_push(self->code->defs, def);
3745 def.type &= ~DEF_SAVEGLOBAL;
3746 gen_vector_defs(self->code, def, global->name);
3748 return global->code.globaladdr >= 0;
3751 ir_value_code_setaddr(global, vec_size(self->code->globals));
3752 if (!global->hasvalue) {
3753 vec_push(self->code->globals, 0);
3754 if (global->code.globaladdr < 0)
3757 vec_push(self->code->globals, vec_size(self->code->functions));
3758 if (!gen_global_function(self, global))
3761 if (!islocal && global->cvq != CV_CONST)
3762 def.type |= DEF_SAVEGLOBAL;
3763 if (pushdef) vec_push(self->code->defs, def);
3766 /* assume biggest type */
3767 ir_value_code_setaddr(global, vec_size(self->code->globals));
3768 vec_push(self->code->globals, 0);
3769 for (i = 1; i < type_sizeof_[TYPE_VARIANT]; ++i)
3770 vec_push(self->code->globals, 0);
3773 /* refuse to create 'void' type or any other fancy business. */
3774 irerror(global->context, "Invalid type for global variable `%s`: %s",
3775 global->name, type_name[global->vtype]);
3780 static GMQCC_INLINE void ir_builder_prepare_field(code_t *code, ir_value *field)
3782 field->code.fieldaddr = code_alloc_field(code, type_sizeof_[field->fieldtype]);
3785 static bool ir_builder_gen_field(ir_builder *self, ir_value *field)
3787 prog_section_def_t def;
3788 prog_section_field_t fld;
3792 def.type = (uint16_t)field->vtype;
3793 def.offset = (uint16_t)vec_size(self->code->globals);
3795 /* create a global named the same as the field */
3796 if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_GMQCC) {
3797 /* in our standard, the global gets a dot prefix */
3798 size_t len = strlen(field->name);
3801 /* we really don't want to have to allocate this, and 1024
3802 * bytes is more than enough for a variable/field name
3804 if (len+2 >= sizeof(name)) {
3805 irerror(field->context, "invalid field name size: %u", (unsigned int)len);
3810 memcpy(name+1, field->name, len); /* no strncpy - we used strlen above */
3813 def.name = code_genstring(self->code, name);
3814 fld.name = def.name + 1; /* we reuse that string table entry */
3816 /* in plain QC, there cannot be a global with the same name,
3817 * and so we also name the global the same.
3818 * FIXME: fteqcc should create a global as well
3819 * check if it actually uses the same name. Probably does
3821 def.name = code_genstring(self->code, field->name);
3822 fld.name = def.name;
3825 field->code.name = def.name;
3827 vec_push(self->code->defs, def);
3829 fld.type = field->fieldtype;
3831 if (fld.type == TYPE_VOID) {
3832 irerror(field->context, "field is missing a type: %s - don't know its size", field->name);
3836 fld.offset = field->code.fieldaddr;
3838 vec_push(self->code->fields, fld);
3840 ir_value_code_setaddr(field, vec_size(self->code->globals));
3841 vec_push(self->code->globals, fld.offset);
3842 if (fld.type == TYPE_VECTOR) {
3843 vec_push(self->code->globals, fld.offset+1);
3844 vec_push(self->code->globals, fld.offset+2);
3847 if (field->fieldtype == TYPE_VECTOR) {
3848 gen_vector_defs (self->code, def, field->name);
3849 gen_vector_fields(self->code, fld, field->name);
3852 return field->code.globaladdr >= 0;
3855 bool ir_builder_generate(ir_builder *self, const char *filename)
3857 prog_section_statement_t stmt;
3859 char *lnofile = NULL;
3861 for (i = 0; i < vec_size(self->fields); ++i)
3863 ir_builder_prepare_field(self->code, self->fields[i]);
3866 for (i = 0; i < vec_size(self->globals); ++i)
3868 if (!ir_builder_gen_global(self, self->globals[i], false)) {
3871 if (self->globals[i]->vtype == TYPE_FUNCTION) {
3872 ir_function *func = self->globals[i]->constval.vfunc;
3873 if (func && self->max_locals < func->allocated_locals &&
3874 !(func->flags & IR_FLAG_MASK_NO_OVERLAP))
3876 self->max_locals = func->allocated_locals;
3878 if (func && self->max_globaltemps < func->globaltemps)
3879 self->max_globaltemps = func->globaltemps;
3883 for (i = 0; i < vec_size(self->fields); ++i)
3885 if (!ir_builder_gen_field(self, self->fields[i])) {
3891 ir_value_code_setaddr(self->nil, vec_size(self->code->globals));
3892 vec_push(self->code->globals, 0);
3893 vec_push(self->code->globals, 0);
3894 vec_push(self->code->globals, 0);
3896 /* generate virtual-instruction temps */
3897 for (i = 0; i < IR_MAX_VINSTR_TEMPS; ++i) {
3898 ir_value_code_setaddr(self->vinstr_temp[i], vec_size(self->code->globals));
3899 vec_push(self->code->globals, 0);
3900 vec_push(self->code->globals, 0);
3901 vec_push(self->code->globals, 0);
3904 /* generate global temps */
3905 self->first_common_globaltemp = vec_size(self->code->globals);
3906 for (i = 0; i < self->max_globaltemps; ++i) {
3907 vec_push(self->code->globals, 0);
3909 /* generate common locals */
3910 self->first_common_local = vec_size(self->code->globals);
3911 for (i = 0; i < self->max_locals; ++i) {
3912 vec_push(self->code->globals, 0);
3915 /* generate function code */
3916 for (i = 0; i < vec_size(self->globals); ++i)
3918 if (self->globals[i]->vtype == TYPE_FUNCTION) {
3919 if (!gen_global_function_code(self, self->globals[i])) {
3925 if (vec_size(self->code->globals) >= 65536) {
3926 irerror(vec_last(self->globals)->context, "This progs file would require more globals than the metadata can handle. Bailing out.");
3930 /* DP errors if the last instruction is not an INSTR_DONE. */
3931 if (vec_last(self->code->statements).opcode != INSTR_DONE)
3935 stmt.opcode = INSTR_DONE;
3939 last.line = vec_last(self->code->linenums);
3940 last.column = vec_last(self->code->columnnums);
3942 code_push_statement(self->code, &stmt, last);
3945 if (OPTS_OPTION_BOOL(OPTION_PP_ONLY))
3948 if (vec_size(self->code->statements) != vec_size(self->code->linenums)) {
3949 con_err("Linecounter wrong: %lu != %lu\n",
3950 (unsigned long)vec_size(self->code->statements),
3951 (unsigned long)vec_size(self->code->linenums));
3952 } else if (OPTS_FLAG(LNO)) {
3954 size_t filelen = strlen(filename);
3956 memcpy(vec_add(lnofile, filelen+1), filename, filelen+1);
3957 dot = strrchr(lnofile, '.');
3961 vec_shrinkto(lnofile, dot - lnofile);
3963 memcpy(vec_add(lnofile, 5), ".lno", 5);
3966 if (!code_write(self->code, filename, lnofile)) {
3975 /***********************************************************************
3976 *IR DEBUG Dump functions...
3979 #define IND_BUFSZ 1024
3981 static const char *qc_opname(int op)
3983 if (op < 0) return "<INVALID>";
3984 if (op < VINSTR_END)
3985 return util_instr_str[op];
3987 case VINSTR_END: return "END";
3988 case VINSTR_PHI: return "PHI";
3989 case VINSTR_JUMP: return "JUMP";
3990 case VINSTR_COND: return "COND";
3991 case VINSTR_BITXOR: return "BITXOR";
3992 case VINSTR_BITAND_V: return "BITAND_V";
3993 case VINSTR_BITOR_V: return "BITOR_V";
3994 case VINSTR_BITXOR_V: return "BITXOR_V";
3995 case VINSTR_BITAND_VF: return "BITAND_VF";
3996 case VINSTR_BITOR_VF: return "BITOR_VF";
3997 case VINSTR_BITXOR_VF: return "BITXOR_VF";
3998 case VINSTR_CROSS: return "CROSS";
3999 case VINSTR_NEG_F: return "NEG_F";
4000 case VINSTR_NEG_V: return "NEG_V";
4001 default: return "<UNK>";
4005 void ir_builder_dump(ir_builder *b, int (*oprintf)(const char*, ...))
4008 char indent[IND_BUFSZ];
4012 oprintf("module %s\n", b->name);
4013 for (i = 0; i < vec_size(b->globals); ++i)
4016 if (b->globals[i]->hasvalue)
4017 oprintf("%s = ", b->globals[i]->name);
4018 ir_value_dump(b->globals[i], oprintf);
4021 for (i = 0; i < vec_size(b->functions); ++i)
4022 ir_function_dump(b->functions[i], indent, oprintf);
4023 oprintf("endmodule %s\n", b->name);
4026 static const char *storenames[] = {
4027 "[global]", "[local]", "[param]", "[value]", "[return]"
4030 void ir_function_dump(ir_function *f, char *ind,
4031 int (*oprintf)(const char*, ...))
4034 if (f->builtin != 0) {
4035 oprintf("%sfunction %s = builtin %i\n", ind, f->name, -f->builtin);
4038 oprintf("%sfunction %s\n", ind, f->name);
4039 platform_strncat(ind, "\t", IND_BUFSZ-1);
4040 if (vec_size(f->locals))
4042 oprintf("%s%i locals:\n", ind, (int)vec_size(f->locals));
4043 for (i = 0; i < vec_size(f->locals); ++i) {
4044 oprintf("%s\t", ind);
4045 ir_value_dump(f->locals[i], oprintf);
4049 oprintf("%sliferanges:\n", ind);
4050 for (i = 0; i < vec_size(f->locals); ++i) {
4051 const char *attr = "";
4053 ir_value *v = f->locals[i];
4054 if (v->unique_life && v->locked)
4055 attr = "unique,locked ";
4056 else if (v->unique_life)
4060 oprintf("%s\t%s: %s %s %s%s@%i ", ind, v->name, type_name[v->vtype],
4061 storenames[v->store],
4062 attr, (v->callparam ? "callparam " : ""),
4063 (int)v->code.local);
4066 for (l = 0; l < vec_size(v->life); ++l) {
4067 oprintf("[%i,%i] ", v->life[l].start, v->life[l].end);
4070 for (m = 0; m < 3; ++m) {
4071 ir_value *vm = v->members[m];
4074 oprintf("%s\t%s: @%i ", ind, vm->name, (int)vm->code.local);
4075 for (l = 0; l < vec_size(vm->life); ++l) {
4076 oprintf("[%i,%i] ", vm->life[l].start, vm->life[l].end);
4081 for (i = 0; i < vec_size(f->values); ++i) {
4082 const char *attr = "";
4084 ir_value *v = f->values[i];
4085 if (v->unique_life && v->locked)
4086 attr = "unique,locked ";
4087 else if (v->unique_life)
4091 oprintf("%s\t%s: %s %s %s%s@%i ", ind, v->name, type_name[v->vtype],
4092 storenames[v->store],
4093 attr, (v->callparam ? "callparam " : ""),
4094 (int)v->code.local);
4097 for (l = 0; l < vec_size(v->life); ++l) {
4098 oprintf("[%i,%i] ", v->life[l].start, v->life[l].end);
4101 for (m = 0; m < 3; ++m) {
4102 ir_value *vm = v->members[m];
4105 if (vm->unique_life && vm->locked)
4106 attr = "unique,locked ";
4107 else if (vm->unique_life)
4109 else if (vm->locked)
4111 oprintf("%s\t%s: %s@%i ", ind, vm->name, attr, (int)vm->code.local);
4112 for (l = 0; l < vec_size(vm->life); ++l) {
4113 oprintf("[%i,%i] ", vm->life[l].start, vm->life[l].end);
4118 if (vec_size(f->blocks))
4120 oprintf("%slife passes: %i\n", ind, (int)f->run_id);
4121 for (i = 0; i < vec_size(f->blocks); ++i) {
4122 ir_block_dump(f->blocks[i], ind, oprintf);
4126 ind[strlen(ind)-1] = 0;
4127 oprintf("%sendfunction %s\n", ind, f->name);
4130 void ir_block_dump(ir_block* b, char *ind,
4131 int (*oprintf)(const char*, ...))
4134 oprintf("%s:%s\n", ind, b->label);
4135 platform_strncat(ind, "\t", IND_BUFSZ-1);
4137 if (b->instr && b->instr[0])
4138 oprintf("%s (%i) [entry]\n", ind, (int)(b->instr[0]->eid-1));
4139 for (i = 0; i < vec_size(b->instr); ++i)
4140 ir_instr_dump(b->instr[i], ind, oprintf);
4141 ind[strlen(ind)-1] = 0;
4144 static void dump_phi(ir_instr *in, int (*oprintf)(const char*, ...))
4147 oprintf("%s <- phi ", in->_ops[0]->name);
4148 for (i = 0; i < vec_size(in->phi); ++i)
4150 oprintf("([%s] : %s) ", in->phi[i].from->label,
4151 in->phi[i].value->name);
4156 void ir_instr_dump(ir_instr *in, char *ind,
4157 int (*oprintf)(const char*, ...))
4160 const char *comma = NULL;
4162 oprintf("%s (%i) ", ind, (int)in->eid);
4164 if (in->opcode == VINSTR_PHI) {
4165 dump_phi(in, oprintf);
4169 platform_strncat(ind, "\t", IND_BUFSZ-1);
4171 if (in->_ops[0] && (in->_ops[1] || in->_ops[2])) {
4172 ir_value_dump(in->_ops[0], oprintf);
4173 if (in->_ops[1] || in->_ops[2])
4176 if (in->opcode == INSTR_CALL0 || in->opcode == VINSTR_NRCALL) {
4177 oprintf("CALL%i\t", vec_size(in->params));
4179 oprintf("%s\t", qc_opname(in->opcode));
4181 if (in->_ops[0] && !(in->_ops[1] || in->_ops[2])) {
4182 ir_value_dump(in->_ops[0], oprintf);
4187 for (i = 1; i != 3; ++i) {
4191 ir_value_dump(in->_ops[i], oprintf);
4199 oprintf("[%s]", in->bops[0]->label);
4203 oprintf("%s[%s]", comma, in->bops[1]->label);
4204 if (vec_size(in->params)) {
4205 oprintf("\tparams: ");
4206 for (i = 0; i != vec_size(in->params); ++i) {
4207 oprintf("%s, ", in->params[i]->name);
4211 ind[strlen(ind)-1] = 0;
4214 static void ir_value_dump_string(const char *str, int (*oprintf)(const char*, ...))
4217 for (; *str; ++str) {
4219 case '\n': oprintf("\\n"); break;
4220 case '\r': oprintf("\\r"); break;
4221 case '\t': oprintf("\\t"); break;
4222 case '\v': oprintf("\\v"); break;
4223 case '\f': oprintf("\\f"); break;
4224 case '\b': oprintf("\\b"); break;
4225 case '\a': oprintf("\\a"); break;
4226 case '\\': oprintf("\\\\"); break;
4227 case '"': oprintf("\\\""); break;
4228 default: oprintf("%c", *str); break;
4234 void ir_value_dump(ir_value* v, int (*oprintf)(const char*, ...))
4243 oprintf("fn:%s", v->name);
4246 oprintf("%g", v->constval.vfloat);
4249 oprintf("'%g %g %g'",
4252 v->constval.vvec.z);
4255 oprintf("(entity)");
4258 ir_value_dump_string(v->constval.vstring, oprintf);
4262 oprintf("%i", v->constval.vint);
4267 v->constval.vpointer->name);
4271 oprintf("%s", v->name);
4275 void ir_value_dump_life(const ir_value *self, int (*oprintf)(const char*,...))
4278 oprintf("Life of %12s:", self->name);
4279 for (i = 0; i < vec_size(self->life); ++i)
4281 oprintf(" + [%i, %i]\n", self->life[i].start, self->life[i].end);