]> de.git.xonotic.org Git - xonotic/gmqcc.git/blob - ir.c
more info in the IR dump
[xonotic/gmqcc.git] / ir.c
1 /*
2  * Copyright (C) 2012, 2013
3  *     Wolfgang Bumiller
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is furnished to do
10  * so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 #include <stdlib.h>
24 #include <string.h>
25 #include "gmqcc.h"
26 #include "ir.h"
27
28 /***********************************************************************
29  * Type sizes used at multiple points in the IR codegen
30  */
31
32 const char *type_name[TYPE_COUNT] = {
33     "void",
34     "string",
35     "float",
36     "vector",
37     "entity",
38     "field",
39     "function",
40     "pointer",
41     "integer",
42     "variant",
43     "struct",
44     "union",
45     "array",
46
47     "nil",
48     "<no-expression>"
49 };
50
51 size_t type_sizeof_[TYPE_COUNT] = {
52     1, /* TYPE_VOID     */
53     1, /* TYPE_STRING   */
54     1, /* TYPE_FLOAT    */
55     3, /* TYPE_VECTOR   */
56     1, /* TYPE_ENTITY   */
57     1, /* TYPE_FIELD    */
58     1, /* TYPE_FUNCTION */
59     1, /* TYPE_POINTER  */
60     1, /* TYPE_INTEGER  */
61     3, /* TYPE_VARIANT  */
62     0, /* TYPE_STRUCT   */
63     0, /* TYPE_UNION    */
64     0, /* TYPE_ARRAY    */
65     0, /* TYPE_NIL      */
66     0, /* TYPE_NOESPR   */
67 };
68
69 uint16_t type_store_instr[TYPE_COUNT] = {
70     INSTR_STORE_F, /* should use I when having integer support */
71     INSTR_STORE_S,
72     INSTR_STORE_F,
73     INSTR_STORE_V,
74     INSTR_STORE_ENT,
75     INSTR_STORE_FLD,
76     INSTR_STORE_FNC,
77     INSTR_STORE_ENT, /* should use I */
78 #if 0
79     INSTR_STORE_I, /* integer type */
80 #else
81     INSTR_STORE_F,
82 #endif
83
84     INSTR_STORE_V, /* variant, should never be accessed */
85
86     AINSTR_END, /* struct */
87     AINSTR_END, /* union  */
88     AINSTR_END, /* array  */
89     AINSTR_END, /* nil    */
90     AINSTR_END, /* noexpr */
91 };
92
93 uint16_t field_store_instr[TYPE_COUNT] = {
94     INSTR_STORE_FLD,
95     INSTR_STORE_FLD,
96     INSTR_STORE_FLD,
97     INSTR_STORE_V,
98     INSTR_STORE_FLD,
99     INSTR_STORE_FLD,
100     INSTR_STORE_FLD,
101     INSTR_STORE_FLD,
102 #if 0
103     INSTR_STORE_FLD, /* integer type */
104 #else
105     INSTR_STORE_FLD,
106 #endif
107
108     INSTR_STORE_V, /* variant, should never be accessed */
109
110     AINSTR_END, /* struct */
111     AINSTR_END, /* union  */
112     AINSTR_END, /* array  */
113     AINSTR_END, /* nil    */
114     AINSTR_END, /* noexpr */
115 };
116
117 uint16_t type_storep_instr[TYPE_COUNT] = {
118     INSTR_STOREP_F, /* should use I when having integer support */
119     INSTR_STOREP_S,
120     INSTR_STOREP_F,
121     INSTR_STOREP_V,
122     INSTR_STOREP_ENT,
123     INSTR_STOREP_FLD,
124     INSTR_STOREP_FNC,
125     INSTR_STOREP_ENT, /* should use I */
126 #if 0
127     INSTR_STOREP_ENT, /* integer type */
128 #else
129     INSTR_STOREP_F,
130 #endif
131
132     INSTR_STOREP_V, /* variant, should never be accessed */
133
134     AINSTR_END, /* struct */
135     AINSTR_END, /* union  */
136     AINSTR_END, /* array  */
137     AINSTR_END, /* nil    */
138     AINSTR_END, /* noexpr */
139 };
140
141 uint16_t type_eq_instr[TYPE_COUNT] = {
142     INSTR_EQ_F, /* should use I when having integer support */
143     INSTR_EQ_S,
144     INSTR_EQ_F,
145     INSTR_EQ_V,
146     INSTR_EQ_E,
147     INSTR_EQ_E, /* FLD has no comparison */
148     INSTR_EQ_FNC,
149     INSTR_EQ_E, /* should use I */
150 #if 0
151     INSTR_EQ_I,
152 #else
153     INSTR_EQ_F,
154 #endif
155
156     INSTR_EQ_V, /* variant, should never be accessed */
157
158     AINSTR_END, /* struct */
159     AINSTR_END, /* union  */
160     AINSTR_END, /* array  */
161     AINSTR_END, /* nil    */
162     AINSTR_END, /* noexpr */
163 };
164
165 uint16_t type_ne_instr[TYPE_COUNT] = {
166     INSTR_NE_F, /* should use I when having integer support */
167     INSTR_NE_S,
168     INSTR_NE_F,
169     INSTR_NE_V,
170     INSTR_NE_E,
171     INSTR_NE_E, /* FLD has no comparison */
172     INSTR_NE_FNC,
173     INSTR_NE_E, /* should use I */
174 #if 0
175     INSTR_NE_I,
176 #else
177     INSTR_NE_F,
178 #endif
179
180     INSTR_NE_V, /* variant, should never be accessed */
181
182     AINSTR_END, /* struct */
183     AINSTR_END, /* union  */
184     AINSTR_END, /* array  */
185     AINSTR_END, /* nil    */
186     AINSTR_END, /* noexpr */
187 };
188
189 uint16_t type_not_instr[TYPE_COUNT] = {
190     INSTR_NOT_F, /* should use I when having integer support */
191     INSTR_NOT_S,
192     INSTR_NOT_F,
193     INSTR_NOT_V,
194     INSTR_NOT_ENT,
195     INSTR_NOT_ENT,
196     INSTR_NOT_FNC,
197     INSTR_NOT_ENT, /* should use I */
198 #if 0
199     INSTR_NOT_I, /* integer type */
200 #else
201     INSTR_NOT_F,
202 #endif
203
204     INSTR_NOT_V, /* variant, should never be accessed */
205
206     AINSTR_END, /* struct */
207     AINSTR_END, /* union  */
208     AINSTR_END, /* array  */
209     AINSTR_END, /* nil    */
210     AINSTR_END, /* noexpr */
211 };
212
213 /* protos */
214 static ir_value* ir_gen_extparam_proto(ir_builder *ir);
215 static void      ir_gen_extparam      (ir_builder *ir);
216
217 /* error functions */
218
219 static void irerror(lex_ctx ctx, const char *msg, ...)
220 {
221     va_list ap;
222     va_start(ap, msg);
223     con_cvprintmsg((void*)&ctx, LVL_ERROR, "internal error", msg, ap);
224     va_end(ap);
225 }
226
227 static bool irwarning(lex_ctx ctx, int warntype, const char *fmt, ...)
228 {
229     bool    r;
230     va_list ap;
231     va_start(ap, fmt);
232     r = vcompile_warning(ctx, warntype, fmt, ap);
233     va_end(ap);
234     return r;
235 }
236
237 /***********************************************************************
238  * Vector utility functions
239  */
240
241 bool GMQCC_WARN vec_ir_value_find(ir_value **vec, const ir_value *what, size_t *idx)
242 {
243     size_t i;
244     size_t len = vec_size(vec);
245     for (i = 0; i < len; ++i) {
246         if (vec[i] == what) {
247             if (idx) *idx = i;
248             return true;
249         }
250     }
251     return false;
252 }
253
254 bool GMQCC_WARN vec_ir_block_find(ir_block **vec, ir_block *what, size_t *idx)
255 {
256     size_t i;
257     size_t len = vec_size(vec);
258     for (i = 0; i < len; ++i) {
259         if (vec[i] == what) {
260             if (idx) *idx = i;
261             return true;
262         }
263     }
264     return false;
265 }
266
267 bool GMQCC_WARN vec_ir_instr_find(ir_instr **vec, ir_instr *what, size_t *idx)
268 {
269     size_t i;
270     size_t len = vec_size(vec);
271     for (i = 0; i < len; ++i) {
272         if (vec[i] == what) {
273             if (idx) *idx = i;
274             return true;
275         }
276     }
277     return false;
278 }
279
280 /***********************************************************************
281  * IR Builder
282  */
283
284 static void ir_block_delete_quick(ir_block* self);
285 static void ir_instr_delete_quick(ir_instr *self);
286 static void ir_function_delete_quick(ir_function *self);
287
288 ir_builder* ir_builder_new(const char *modulename)
289 {
290     ir_builder* self;
291
292     self = (ir_builder*)mem_a(sizeof(*self));
293     if (!self)
294         return NULL;
295
296     self->functions   = NULL;
297     self->globals     = NULL;
298     self->fields      = NULL;
299     self->filenames   = NULL;
300     self->filestrings = NULL;
301     self->htglobals   = util_htnew(IR_HT_SIZE);
302     self->htfields    = util_htnew(IR_HT_SIZE);
303     self->htfunctions = util_htnew(IR_HT_SIZE);
304
305     self->extparams       = NULL;
306     self->extparam_protos = NULL;
307
308     self->first_common_globaltemp = 0;
309     self->max_globaltemps         = 0;
310     self->first_common_local      = 0;
311     self->max_locals              = 0;
312
313     self->str_immediate = 0;
314     self->name = NULL;
315     if (!ir_builder_set_name(self, modulename)) {
316         mem_d(self);
317         return NULL;
318     }
319
320     self->nil = ir_value_var("nil", store_value, TYPE_NIL);
321     self->nil->cvq = CV_CONST;
322
323     return self;
324 }
325
326 void ir_builder_delete(ir_builder* self)
327 {
328     size_t i;
329     util_htdel(self->htglobals);
330     util_htdel(self->htfields);
331     util_htdel(self->htfunctions);
332     mem_d((void*)self->name);
333     for (i = 0; i != vec_size(self->functions); ++i) {
334         ir_function_delete_quick(self->functions[i]);
335     }
336     vec_free(self->functions);
337     for (i = 0; i != vec_size(self->extparams); ++i) {
338         ir_value_delete(self->extparams[i]);
339     }
340     vec_free(self->extparams);
341     for (i = 0; i != vec_size(self->globals); ++i) {
342         ir_value_delete(self->globals[i]);
343     }
344     vec_free(self->globals);
345     for (i = 0; i != vec_size(self->fields); ++i) {
346         ir_value_delete(self->fields[i]);
347     }
348     ir_value_delete(self->nil);
349     vec_free(self->fields);
350     vec_free(self->filenames);
351     vec_free(self->filestrings);
352     mem_d(self);
353 }
354
355 bool ir_builder_set_name(ir_builder *self, const char *name)
356 {
357     if (self->name)
358         mem_d((void*)self->name);
359     self->name = util_strdup(name);
360     return !!self->name;
361 }
362
363 ir_function* ir_builder_get_function(ir_builder *self, const char *name)
364 {
365     return (ir_function*)util_htget(self->htfunctions, name);
366 }
367
368 ir_function* ir_builder_create_function(ir_builder *self, const char *name, int outtype)
369 {
370     ir_function *fn = ir_builder_get_function(self, name);
371     if (fn) {
372         return NULL;
373     }
374
375     fn = ir_function_new(self, outtype);
376     if (!ir_function_set_name(fn, name))
377     {
378         ir_function_delete(fn);
379         return NULL;
380     }
381     vec_push(self->functions, fn);
382     util_htset(self->htfunctions, name, fn);
383
384     fn->value = ir_builder_create_global(self, fn->name, TYPE_FUNCTION);
385     if (!fn->value) {
386         ir_function_delete(fn);
387         return NULL;
388     }
389
390     fn->value->hasvalue = true;
391     fn->value->outtype = outtype;
392     fn->value->constval.vfunc = fn;
393     fn->value->context = fn->context;
394
395     return fn;
396 }
397
398 ir_value* ir_builder_get_global(ir_builder *self, const char *name)
399 {
400     return (ir_value*)util_htget(self->htglobals, name);
401 }
402
403 ir_value* ir_builder_create_global(ir_builder *self, const char *name, int vtype)
404 {
405     ir_value *ve;
406
407     if (name && name[0] != '#')
408     {
409         ve = ir_builder_get_global(self, name);
410         if (ve) {
411             return NULL;
412         }
413     }
414
415     ve = ir_value_var(name, store_global, vtype);
416     vec_push(self->globals, ve);
417     util_htset(self->htglobals, name, ve);
418     return ve;
419 }
420
421 ir_value* ir_builder_get_field(ir_builder *self, const char *name)
422 {
423     return (ir_value*)util_htget(self->htfields, name);
424 }
425
426
427 ir_value* ir_builder_create_field(ir_builder *self, const char *name, int vtype)
428 {
429     ir_value *ve = ir_builder_get_field(self, name);
430     if (ve) {
431         return NULL;
432     }
433
434     ve = ir_value_var(name, store_global, TYPE_FIELD);
435     ve->fieldtype = vtype;
436     vec_push(self->fields, ve);
437     util_htset(self->htfields, name, ve);
438     return ve;
439 }
440
441 /***********************************************************************
442  *IR Function
443  */
444
445 bool ir_function_naive_phi(ir_function*);
446 void ir_function_enumerate(ir_function*);
447 bool ir_function_calculate_liferanges(ir_function*);
448 bool ir_function_allocate_locals(ir_function*);
449
450 ir_function* ir_function_new(ir_builder* owner, int outtype)
451 {
452     ir_function *self;
453     self = (ir_function*)mem_a(sizeof(*self));
454
455     if (!self)
456         return NULL;
457
458     memset(self, 0, sizeof(*self));
459
460     self->name = NULL;
461     if (!ir_function_set_name(self, "<@unnamed>")) {
462         mem_d(self);
463         return NULL;
464     }
465     self->flags = 0;
466
467     self->owner = owner;
468     self->context.file = "<@no context>";
469     self->context.line = 0;
470     self->outtype = outtype;
471     self->value = NULL;
472     self->builtin = 0;
473
474     self->params = NULL;
475     self->blocks = NULL;
476     self->values = NULL;
477     self->locals = NULL;
478
479     self->code_function_def = -1;
480     self->allocated_locals = 0;
481     self->globaltemps      = 0;
482
483     self->run_id = 0;
484     return self;
485 }
486
487 bool ir_function_set_name(ir_function *self, const char *name)
488 {
489     if (self->name)
490         mem_d((void*)self->name);
491     self->name = util_strdup(name);
492     return !!self->name;
493 }
494
495 static void ir_function_delete_quick(ir_function *self)
496 {
497     size_t i;
498     mem_d((void*)self->name);
499
500     for (i = 0; i != vec_size(self->blocks); ++i)
501         ir_block_delete_quick(self->blocks[i]);
502     vec_free(self->blocks);
503
504     vec_free(self->params);
505
506     for (i = 0; i != vec_size(self->values); ++i)
507         ir_value_delete(self->values[i]);
508     vec_free(self->values);
509
510     for (i = 0; i != vec_size(self->locals); ++i)
511         ir_value_delete(self->locals[i]);
512     vec_free(self->locals);
513
514     /* self->value is deleted by the builder */
515
516     mem_d(self);
517 }
518
519 void ir_function_delete(ir_function *self)
520 {
521     size_t i;
522     mem_d((void*)self->name);
523
524     for (i = 0; i != vec_size(self->blocks); ++i)
525         ir_block_delete(self->blocks[i]);
526     vec_free(self->blocks);
527
528     vec_free(self->params);
529
530     for (i = 0; i != vec_size(self->values); ++i)
531         ir_value_delete(self->values[i]);
532     vec_free(self->values);
533
534     for (i = 0; i != vec_size(self->locals); ++i)
535         ir_value_delete(self->locals[i]);
536     vec_free(self->locals);
537
538     /* self->value is deleted by the builder */
539
540     mem_d(self);
541 }
542
543 void ir_function_collect_value(ir_function *self, ir_value *v)
544 {
545     vec_push(self->values, v);
546 }
547
548 ir_block* ir_function_create_block(lex_ctx ctx, ir_function *self, const char *label)
549 {
550     ir_block* bn = ir_block_new(self, label);
551     bn->context = ctx;
552     vec_push(self->blocks, bn);
553     return bn;
554 }
555
556 static bool instr_is_operation(uint16_t op)
557 {
558     return ( (op >= INSTR_MUL_F  && op <= INSTR_GT) ||
559              (op >= INSTR_LOAD_F && op <= INSTR_LOAD_FNC) ||
560              (op == INSTR_ADDRESS) ||
561              (op >= INSTR_NOT_F  && op <= INSTR_NOT_FNC) ||
562              (op >= INSTR_AND    && op <= INSTR_BITOR) ||
563              (op >= INSTR_CALL0  && op <= INSTR_CALL8) );
564 }
565
566 bool ir_function_pass_peephole(ir_function *self)
567 {
568     size_t b;
569
570     for (b = 0; b < vec_size(self->blocks); ++b) {
571         size_t    i;
572         ir_block *block = self->blocks[b];
573
574         for (i = 0; i < vec_size(block->instr); ++i) {
575             ir_instr *inst;
576             inst = block->instr[i];
577
578             if (i >= 1 &&
579                 (inst->opcode >= INSTR_STORE_F &&
580                  inst->opcode <= INSTR_STORE_FNC))
581             {
582                 ir_instr *store;
583                 ir_instr *oper;
584                 ir_value *value;
585
586                 store = inst;
587
588                 oper  = block->instr[i-1];
589                 if (!instr_is_operation(oper->opcode))
590                     continue;
591
592                 value = oper->_ops[0];
593
594                 /* only do it for SSA values */
595                 if (value->store != store_value)
596                     continue;
597
598                 /* don't optimize out the temp if it's used later again */
599                 if (vec_size(value->reads) != 1)
600                     continue;
601
602                 /* The very next store must use this value */
603                 if (value->reads[0] != store)
604                     continue;
605
606                 /* And of course the store must _read_ from it, so it's in
607                  * OP 1 */
608                 if (store->_ops[1] != value)
609                     continue;
610
611                 ++opts_optimizationcount[OPTIM_PEEPHOLE];
612                 (void)!ir_instr_op(oper, 0, store->_ops[0], true);
613
614                 vec_remove(block->instr, i, 1);
615                 ir_instr_delete(store);
616             }
617             else if (inst->opcode == VINSTR_COND)
618             {
619                 /* COND on a value resulting from a NOT could
620                  * remove the NOT and swap its operands
621                  */
622                 while (true) {
623                     ir_block *tmp;
624                     size_t    inotid;
625                     ir_instr *inot;
626                     ir_value *value;
627                     value = inst->_ops[0];
628
629                     if (value->store != store_value ||
630                         vec_size(value->reads) != 1 ||
631                         value->reads[0] != inst)
632                     {
633                         break;
634                     }
635
636                     inot = value->writes[0];
637                     if (inot->_ops[0] != value ||
638                         inot->opcode < INSTR_NOT_F ||
639                         inot->opcode > INSTR_NOT_FNC ||
640                         inot->opcode == INSTR_NOT_V || /* can't do these */
641                         inot->opcode == INSTR_NOT_S)
642                     {
643                         break;
644                     }
645
646                     /* count */
647                     ++opts_optimizationcount[OPTIM_PEEPHOLE];
648                     /* change operand */
649                     (void)!ir_instr_op(inst, 0, inot->_ops[1], false);
650                     /* remove NOT */
651                     tmp = inot->owner;
652                     for (inotid = 0; inotid < vec_size(tmp->instr); ++inotid) {
653                         if (tmp->instr[inotid] == inot)
654                             break;
655                     }
656                     if (inotid >= vec_size(tmp->instr)) {
657                         compile_error(inst->context, "sanity-check failed: failed to find instruction to optimize out");
658                         return false;
659                     }
660                     vec_remove(tmp->instr, inotid, 1);
661                     ir_instr_delete(inot);
662                     /* swap ontrue/onfalse */
663                     tmp = inst->bops[0];
664                     inst->bops[0] = inst->bops[1];
665                     inst->bops[1] = tmp;
666                 }
667                 continue;
668             }
669         }
670     }
671
672     return true;
673 }
674
675 bool ir_function_pass_tailrecursion(ir_function *self)
676 {
677     size_t b, p;
678
679     for (b = 0; b < vec_size(self->blocks); ++b) {
680         ir_value *funcval;
681         ir_instr *ret, *call, *store = NULL;
682         ir_block *block = self->blocks[b];
683
684         if (!block->final || vec_size(block->instr) < 2)
685             continue;
686
687         ret = block->instr[vec_size(block->instr)-1];
688         if (ret->opcode != INSTR_DONE && ret->opcode != INSTR_RETURN)
689             continue;
690
691         call = block->instr[vec_size(block->instr)-2];
692         if (call->opcode >= INSTR_STORE_F && call->opcode <= INSTR_STORE_FNC) {
693             /* account for the unoptimized
694              * CALL
695              * STORE %return, %tmp
696              * RETURN %tmp
697              * version
698              */
699             if (vec_size(block->instr) < 3)
700                 continue;
701
702             store = call;
703             call = block->instr[vec_size(block->instr)-3];
704         }
705
706         if (call->opcode < INSTR_CALL0 || call->opcode > INSTR_CALL8)
707             continue;
708
709         if (store) {
710             /* optimize out the STORE */
711             if (ret->_ops[0]   &&
712                 ret->_ops[0]   == store->_ops[0] &&
713                 store->_ops[1] == call->_ops[0])
714             {
715                 ++opts_optimizationcount[OPTIM_PEEPHOLE];
716                 call->_ops[0] = store->_ops[0];
717                 vec_remove(block->instr, vec_size(block->instr) - 2, 1);
718                 ir_instr_delete(store);
719             }
720             else
721                 continue;
722         }
723
724         if (!call->_ops[0])
725             continue;
726
727         funcval = call->_ops[1];
728         if (!funcval)
729             continue;
730         if (funcval->vtype != TYPE_FUNCTION || funcval->constval.vfunc != self)
731             continue;
732
733         /* now we have a CALL and a RET, check if it's a tailcall */
734         if (ret->_ops[0] && call->_ops[0] != ret->_ops[0])
735             continue;
736
737         ++opts_optimizationcount[OPTIM_TAIL_RECURSION];
738         vec_shrinkby(block->instr, 2);
739
740         block->final = false; /* open it back up */
741
742         /* emite parameter-stores */
743         for (p = 0; p < vec_size(call->params); ++p) {
744             /* assert(call->params_count <= self->locals_count); */
745             if (!ir_block_create_store(block, call->context, self->locals[p], call->params[p])) {
746                 irerror(call->context, "failed to create tailcall store instruction for parameter %i", (int)p);
747                 return false;
748             }
749         }
750         if (!ir_block_create_jump(block, call->context, self->blocks[0])) {
751             irerror(call->context, "failed to create tailcall jump");
752             return false;
753         }
754
755         ir_instr_delete(call);
756         ir_instr_delete(ret);
757     }
758
759     return true;
760 }
761
762 bool ir_function_finalize(ir_function *self)
763 {
764     size_t i;
765
766     if (self->builtin)
767         return true;
768
769     if (OPTS_OPTIMIZATION(OPTIM_PEEPHOLE)) {
770         if (!ir_function_pass_peephole(self)) {
771             irerror(self->context, "generic optimization pass broke something in `%s`", self->name);
772             return false;
773         }
774     }
775
776     if (OPTS_OPTIMIZATION(OPTIM_TAIL_RECURSION)) {
777         if (!ir_function_pass_tailrecursion(self)) {
778             irerror(self->context, "tail-recursion optimization pass broke something in `%s`", self->name);
779             return false;
780         }
781     }
782
783     if (!ir_function_naive_phi(self)) {
784         irerror(self->context, "internal error: ir_function_naive_phi failed");
785         return false;
786     }
787
788     for (i = 0; i < vec_size(self->locals); ++i) {
789         ir_value *v = self->locals[i];
790         if (v->vtype == TYPE_VECTOR ||
791             (v->vtype == TYPE_FIELD && v->outtype == TYPE_VECTOR))
792         {
793             ir_value_vector_member(v, 0);
794             ir_value_vector_member(v, 1);
795             ir_value_vector_member(v, 2);
796         }
797     }
798     for (i = 0; i < vec_size(self->values); ++i) {
799         ir_value *v = self->values[i];
800         if (v->vtype == TYPE_VECTOR ||
801             (v->vtype == TYPE_FIELD && v->outtype == TYPE_VECTOR))
802         {
803             ir_value_vector_member(v, 0);
804             ir_value_vector_member(v, 1);
805             ir_value_vector_member(v, 2);
806         }
807     }
808
809     ir_function_enumerate(self);
810
811     if (!ir_function_calculate_liferanges(self))
812         return false;
813     if (!ir_function_allocate_locals(self))
814         return false;
815     return true;
816 }
817
818 ir_value* ir_function_create_local(ir_function *self, const char *name, int vtype, bool param)
819 {
820     ir_value *ve;
821
822     if (param &&
823         vec_size(self->locals) &&
824         self->locals[vec_size(self->locals)-1]->store != store_param) {
825         irerror(self->context, "cannot add parameters after adding locals");
826         return NULL;
827     }
828
829     ve = ir_value_var(name, (param ? store_param : store_local), vtype);
830     if (param)
831         ve->locked = true;
832     vec_push(self->locals, ve);
833     return ve;
834 }
835
836 /***********************************************************************
837  *IR Block
838  */
839
840 ir_block* ir_block_new(ir_function* owner, const char *name)
841 {
842     ir_block *self;
843     self = (ir_block*)mem_a(sizeof(*self));
844     if (!self)
845         return NULL;
846
847     memset(self, 0, sizeof(*self));
848
849     self->label = NULL;
850     if (name && !ir_block_set_label(self, name)) {
851         mem_d(self);
852         return NULL;
853     }
854     self->owner = owner;
855     self->context.file = "<@no context>";
856     self->context.line = 0;
857     self->final = false;
858
859     self->instr   = NULL;
860     self->entries = NULL;
861     self->exits   = NULL;
862
863     self->eid = 0;
864     self->is_return = false;
865     self->run_id = 0;
866
867     self->living = NULL;
868
869     self->generated = false;
870
871     return self;
872 }
873
874 static void ir_block_delete_quick(ir_block* self)
875 {
876     size_t i;
877     if (self->label) mem_d(self->label);
878     for (i = 0; i != vec_size(self->instr); ++i)
879         ir_instr_delete_quick(self->instr[i]);
880     vec_free(self->instr);
881     vec_free(self->entries);
882     vec_free(self->exits);
883     vec_free(self->living);
884     mem_d(self);
885 }
886
887 void ir_block_delete(ir_block* self)
888 {
889     size_t i;
890     if (self->label) mem_d(self->label);
891     for (i = 0; i != vec_size(self->instr); ++i)
892         ir_instr_delete(self->instr[i]);
893     vec_free(self->instr);
894     vec_free(self->entries);
895     vec_free(self->exits);
896     vec_free(self->living);
897     mem_d(self);
898 }
899
900 bool ir_block_set_label(ir_block *self, const char *name)
901 {
902     if (self->label)
903         mem_d((void*)self->label);
904     self->label = util_strdup(name);
905     return !!self->label;
906 }
907
908 /***********************************************************************
909  *IR Instructions
910  */
911
912 ir_instr* ir_instr_new(lex_ctx ctx, ir_block* owner, int op)
913 {
914     ir_instr *self;
915     self = (ir_instr*)mem_a(sizeof(*self));
916     if (!self)
917         return NULL;
918
919     self->owner = owner;
920     self->context = ctx;
921     self->opcode = op;
922     self->_ops[0] = NULL;
923     self->_ops[1] = NULL;
924     self->_ops[2] = NULL;
925     self->bops[0] = NULL;
926     self->bops[1] = NULL;
927
928     self->phi    = NULL;
929     self->params = NULL;
930
931     self->eid = 0;
932
933     self->likely = true;
934     return self;
935 }
936
937 static void ir_instr_delete_quick(ir_instr *self)
938 {
939     vec_free(self->phi);
940     vec_free(self->params);
941     mem_d(self);
942 }
943
944 void ir_instr_delete(ir_instr *self)
945 {
946     size_t i;
947     /* The following calls can only delete from
948      * vectors, we still want to delete this instruction
949      * so ignore the return value. Since with the warn_unused_result attribute
950      * gcc doesn't care about an explicit: (void)foo(); to ignore the result,
951      * I have to improvise here and use if(foo());
952      */
953     for (i = 0; i < vec_size(self->phi); ++i) {
954         size_t idx;
955         if (vec_ir_instr_find(self->phi[i].value->writes, self, &idx))
956             vec_remove(self->phi[i].value->writes, idx, 1);
957         if (vec_ir_instr_find(self->phi[i].value->reads, self, &idx))
958             vec_remove(self->phi[i].value->reads, idx, 1);
959     }
960     vec_free(self->phi);
961     for (i = 0; i < vec_size(self->params); ++i) {
962         size_t idx;
963         if (vec_ir_instr_find(self->params[i]->writes, self, &idx))
964             vec_remove(self->params[i]->writes, idx, 1);
965         if (vec_ir_instr_find(self->params[i]->reads, self, &idx))
966             vec_remove(self->params[i]->reads, idx, 1);
967     }
968     vec_free(self->params);
969     (void)!ir_instr_op(self, 0, NULL, false);
970     (void)!ir_instr_op(self, 1, NULL, false);
971     (void)!ir_instr_op(self, 2, NULL, false);
972     mem_d(self);
973 }
974
975 bool ir_instr_op(ir_instr *self, int op, ir_value *v, bool writing)
976 {
977     if (self->_ops[op]) {
978         size_t idx;
979         if (writing && vec_ir_instr_find(self->_ops[op]->writes, self, &idx))
980             vec_remove(self->_ops[op]->writes, idx, 1);
981         else if (vec_ir_instr_find(self->_ops[op]->reads, self, &idx))
982             vec_remove(self->_ops[op]->reads, idx, 1);
983     }
984     if (v) {
985         if (writing)
986             vec_push(v->writes, self);
987         else
988             vec_push(v->reads, self);
989     }
990     self->_ops[op] = v;
991     return true;
992 }
993
994 /***********************************************************************
995  *IR Value
996  */
997
998 void ir_value_code_setaddr(ir_value *self, int32_t gaddr)
999 {
1000     self->code.globaladdr = gaddr;
1001     if (self->members[0]) self->members[0]->code.globaladdr = gaddr;
1002     if (self->members[1]) self->members[1]->code.globaladdr = gaddr;
1003     if (self->members[2]) self->members[2]->code.globaladdr = gaddr;
1004 }
1005
1006 int32_t ir_value_code_addr(const ir_value *self)
1007 {
1008     if (self->store == store_return)
1009         return OFS_RETURN + self->code.addroffset;
1010     return self->code.globaladdr + self->code.addroffset;
1011 }
1012
1013 ir_value* ir_value_var(const char *name, int storetype, int vtype)
1014 {
1015     ir_value *self;
1016     self = (ir_value*)mem_a(sizeof(*self));
1017     self->vtype = vtype;
1018     self->fieldtype = TYPE_VOID;
1019     self->outtype = TYPE_VOID;
1020     self->store = storetype;
1021
1022     self->reads  = NULL;
1023     self->writes = NULL;
1024
1025     self->cvq          = CV_NONE;
1026     self->hasvalue     = false;
1027     self->context.file = "<@no context>";
1028     self->context.line = 0;
1029     self->name = NULL;
1030     if (name && !ir_value_set_name(self, name)) {
1031         irerror(self->context, "out of memory");
1032         mem_d(self);
1033         return NULL;
1034     }
1035
1036     memset(&self->constval, 0, sizeof(self->constval));
1037     memset(&self->code,     0, sizeof(self->code));
1038
1039     self->members[0] = NULL;
1040     self->members[1] = NULL;
1041     self->members[2] = NULL;
1042     self->memberof = NULL;
1043
1044     self->unique_life = false;
1045     self->locked      = false;
1046     self->callparam   = false;
1047
1048     self->life = NULL;
1049     return self;
1050 }
1051
1052 ir_value* ir_value_vector_member(ir_value *self, unsigned int member)
1053 {
1054     char     *name;
1055     size_t    len;
1056     ir_value *m;
1057     if (member >= 3)
1058         return NULL;
1059
1060     if (self->members[member])
1061         return self->members[member];
1062
1063     if (self->name) {
1064         len = strlen(self->name);
1065         name = (char*)mem_a(len + 3);
1066         memcpy(name, self->name, len);
1067         name[len+0] = '_';
1068         name[len+1] = 'x' + member;
1069         name[len+2] = '\0';
1070     }
1071     else
1072         name = NULL;
1073
1074     if (self->vtype == TYPE_VECTOR)
1075     {
1076         m = ir_value_var(name, self->store, TYPE_FLOAT);
1077         if (name)
1078             mem_d(name);
1079         if (!m)
1080             return NULL;
1081         m->context = self->context;
1082
1083         self->members[member] = m;
1084         m->code.addroffset = member;
1085     }
1086     else if (self->vtype == TYPE_FIELD)
1087     {
1088         if (self->fieldtype != TYPE_VECTOR)
1089             return NULL;
1090         m = ir_value_var(name, self->store, TYPE_FIELD);
1091         if (name)
1092             mem_d(name);
1093         if (!m)
1094             return NULL;
1095         m->fieldtype = TYPE_FLOAT;
1096         m->context = self->context;
1097
1098         self->members[member] = m;
1099         m->code.addroffset = member;
1100     }
1101     else
1102     {
1103         irerror(self->context, "invalid member access on %s", self->name);
1104         return NULL;
1105     }
1106
1107     m->memberof = self;
1108     return m;
1109 }
1110
1111 static GMQCC_INLINE size_t ir_value_sizeof(const ir_value *self)
1112 {
1113     if (self->vtype == TYPE_FIELD && self->fieldtype == TYPE_VECTOR)
1114         return type_sizeof_[TYPE_VECTOR];
1115     return type_sizeof_[self->vtype];
1116 }
1117
1118 ir_value* ir_value_out(ir_function *owner, const char *name, int storetype, int vtype)
1119 {
1120     ir_value *v = ir_value_var(name, storetype, vtype);
1121     if (!v)
1122         return NULL;
1123     ir_function_collect_value(owner, v);
1124     return v;
1125 }
1126
1127 void ir_value_delete(ir_value* self)
1128 {
1129     size_t i;
1130     if (self->name)
1131         mem_d((void*)self->name);
1132     if (self->hasvalue)
1133     {
1134         if (self->vtype == TYPE_STRING)
1135             mem_d((void*)self->constval.vstring);
1136     }
1137     for (i = 0; i < 3; ++i) {
1138         if (self->members[i])
1139             ir_value_delete(self->members[i]);
1140     }
1141     vec_free(self->reads);
1142     vec_free(self->writes);
1143     vec_free(self->life);
1144     mem_d(self);
1145 }
1146
1147 bool ir_value_set_name(ir_value *self, const char *name)
1148 {
1149     if (self->name)
1150         mem_d((void*)self->name);
1151     self->name = util_strdup(name);
1152     return !!self->name;
1153 }
1154
1155 bool ir_value_set_float(ir_value *self, float f)
1156 {
1157     if (self->vtype != TYPE_FLOAT)
1158         return false;
1159     self->constval.vfloat = f;
1160     self->hasvalue = true;
1161     return true;
1162 }
1163
1164 bool ir_value_set_func(ir_value *self, int f)
1165 {
1166     if (self->vtype != TYPE_FUNCTION)
1167         return false;
1168     self->constval.vint = f;
1169     self->hasvalue = true;
1170     return true;
1171 }
1172
1173 bool ir_value_set_vector(ir_value *self, vector v)
1174 {
1175     if (self->vtype != TYPE_VECTOR)
1176         return false;
1177     self->constval.vvec = v;
1178     self->hasvalue = true;
1179     return true;
1180 }
1181
1182 bool ir_value_set_field(ir_value *self, ir_value *fld)
1183 {
1184     if (self->vtype != TYPE_FIELD)
1185         return false;
1186     self->constval.vpointer = fld;
1187     self->hasvalue = true;
1188     return true;
1189 }
1190
1191 static char *ir_strdup(const char *str)
1192 {
1193     if (str && !*str) {
1194         /* actually dup empty strings */
1195         char *out = (char*)mem_a(1);
1196         *out = 0;
1197         return out;
1198     }
1199     return util_strdup(str);
1200 }
1201
1202 bool ir_value_set_string(ir_value *self, const char *str)
1203 {
1204     if (self->vtype != TYPE_STRING)
1205         return false;
1206     self->constval.vstring = ir_strdup(str);
1207     self->hasvalue = true;
1208     return true;
1209 }
1210
1211 #if 0
1212 bool ir_value_set_int(ir_value *self, int i)
1213 {
1214     if (self->vtype != TYPE_INTEGER)
1215         return false;
1216     self->constval.vint = i;
1217     self->hasvalue = true;
1218     return true;
1219 }
1220 #endif
1221
1222 bool ir_value_lives(ir_value *self, size_t at)
1223 {
1224     size_t i;
1225     for (i = 0; i < vec_size(self->life); ++i)
1226     {
1227         ir_life_entry_t *life = &self->life[i];
1228         if (life->start <= at && at <= life->end)
1229             return true;
1230         if (life->start > at) /* since it's ordered */
1231             return false;
1232     }
1233     return false;
1234 }
1235
1236 bool ir_value_life_insert(ir_value *self, size_t idx, ir_life_entry_t e)
1237 {
1238     size_t k;
1239     vec_push(self->life, e);
1240     for (k = vec_size(self->life)-1; k > idx; --k)
1241         self->life[k] = self->life[k-1];
1242     self->life[idx] = e;
1243     return true;
1244 }
1245
1246 bool ir_value_life_merge(ir_value *self, size_t s)
1247 {
1248     size_t i;
1249     ir_life_entry_t *life = NULL;
1250     ir_life_entry_t *before = NULL;
1251     ir_life_entry_t new_entry;
1252
1253     /* Find the first range >= s */
1254     for (i = 0; i < vec_size(self->life); ++i)
1255     {
1256         before = life;
1257         life = &self->life[i];
1258         if (life->start > s)
1259             break;
1260     }
1261     /* nothing found? append */
1262     if (i == vec_size(self->life)) {
1263         ir_life_entry_t e;
1264         if (life && life->end+1 == s)
1265         {
1266             /* previous life range can be merged in */
1267             life->end++;
1268             return true;
1269         }
1270         if (life && life->end >= s)
1271             return false;
1272         e.start = e.end = s;
1273         vec_push(self->life, e);
1274         return true;
1275     }
1276     /* found */
1277     if (before)
1278     {
1279         if (before->end + 1 == s &&
1280             life->start - 1 == s)
1281         {
1282             /* merge */
1283             before->end = life->end;
1284             vec_remove(self->life, i, 1);
1285             return true;
1286         }
1287         if (before->end + 1 == s)
1288         {
1289             /* extend before */
1290             before->end++;
1291             return true;
1292         }
1293         /* already contained */
1294         if (before->end >= s)
1295             return false;
1296     }
1297     /* extend */
1298     if (life->start - 1 == s)
1299     {
1300         life->start--;
1301         return true;
1302     }
1303     /* insert a new entry */
1304     new_entry.start = new_entry.end = s;
1305     return ir_value_life_insert(self, i, new_entry);
1306 }
1307
1308 bool ir_value_life_merge_into(ir_value *self, const ir_value *other)
1309 {
1310     size_t i, myi;
1311
1312     if (!vec_size(other->life))
1313         return true;
1314
1315     if (!vec_size(self->life)) {
1316         size_t count = vec_size(other->life);
1317         ir_life_entry_t *life = vec_add(self->life, count);
1318         memcpy(life, other->life, count * sizeof(*life));
1319         return true;
1320     }
1321
1322     myi = 0;
1323     for (i = 0; i < vec_size(other->life); ++i)
1324     {
1325         const ir_life_entry_t *life = &other->life[i];
1326         while (true)
1327         {
1328             ir_life_entry_t *entry = &self->life[myi];
1329
1330             if (life->end+1 < entry->start)
1331             {
1332                 /* adding an interval before entry */
1333                 if (!ir_value_life_insert(self, myi, *life))
1334                     return false;
1335                 ++myi;
1336                 break;
1337             }
1338
1339             if (life->start <  entry->start &&
1340                 life->end+1 >= entry->start)
1341             {
1342                 /* starts earlier and overlaps */
1343                 entry->start = life->start;
1344             }
1345
1346             if (life->end   >  entry->end &&
1347                 life->start <= entry->end+1)
1348             {
1349                 /* ends later and overlaps */
1350                 entry->end = life->end;
1351             }
1352
1353             /* see if our change combines it with the next ranges */
1354             while (myi+1 < vec_size(self->life) &&
1355                    entry->end+1 >= self->life[1+myi].start)
1356             {
1357                 /* overlaps with (myi+1) */
1358                 if (entry->end < self->life[1+myi].end)
1359                     entry->end = self->life[1+myi].end;
1360                 vec_remove(self->life, myi+1, 1);
1361                 entry = &self->life[myi];
1362             }
1363
1364             /* see if we're after the entry */
1365             if (life->start > entry->end)
1366             {
1367                 ++myi;
1368                 /* append if we're at the end */
1369                 if (myi >= vec_size(self->life)) {
1370                     vec_push(self->life, *life);
1371                     break;
1372                 }
1373                 /* otherweise check the next range */
1374                 continue;
1375             }
1376             break;
1377         }
1378     }
1379     return true;
1380 }
1381
1382 bool ir_values_overlap(const ir_value *a, const ir_value *b)
1383 {
1384     /* For any life entry in A see if it overlaps with
1385      * any life entry in B.
1386      * Note that the life entries are orderes, so we can make a
1387      * more efficient algorithm there than naively translating the
1388      * statement above.
1389      */
1390
1391     ir_life_entry_t *la, *lb, *enda, *endb;
1392
1393     /* first of all, if either has no life range, they cannot clash */
1394     if (!vec_size(a->life) || !vec_size(b->life))
1395         return false;
1396
1397     la = a->life;
1398     lb = b->life;
1399     enda = la + vec_size(a->life);
1400     endb = lb + vec_size(b->life);
1401     while (true)
1402     {
1403         /* check if the entries overlap, for that,
1404          * both must start before the other one ends.
1405          */
1406         if (la->start < lb->end &&
1407             lb->start < la->end)
1408         {
1409             return true;
1410         }
1411
1412         /* entries are ordered
1413          * one entry is earlier than the other
1414          * that earlier entry will be moved forward
1415          */
1416         if (la->start < lb->start)
1417         {
1418             /* order: A B, move A forward
1419              * check if we hit the end with A
1420              */
1421             if (++la == enda)
1422                 break;
1423         }
1424         else /* if (lb->start < la->start)  actually <= */
1425         {
1426             /* order: B A, move B forward
1427              * check if we hit the end with B
1428              */
1429             if (++lb == endb)
1430                 break;
1431         }
1432     }
1433     return false;
1434 }
1435
1436 /***********************************************************************
1437  *IR main operations
1438  */
1439
1440 static bool ir_check_unreachable(ir_block *self)
1441 {
1442     /* The IR should never have to deal with unreachable code */
1443     if (!self->final/* || OPTS_FLAG(ALLOW_UNREACHABLE_CODE)*/)
1444         return true;
1445     irerror(self->context, "unreachable statement (%s)", self->label);
1446     return false;
1447 }
1448
1449 bool ir_block_create_store_op(ir_block *self, lex_ctx ctx, int op, ir_value *target, ir_value *what)
1450 {
1451     ir_instr *in;
1452     if (!ir_check_unreachable(self))
1453         return false;
1454
1455     if (target->store == store_value &&
1456         (op < INSTR_STOREP_F || op > INSTR_STOREP_FNC))
1457     {
1458         irerror(self->context, "cannot store to an SSA value");
1459         irerror(self->context, "trying to store: %s <- %s", target->name, what->name);
1460         irerror(self->context, "instruction: %s", asm_instr[op].m);
1461         return false;
1462     }
1463
1464     in = ir_instr_new(ctx, self, op);
1465     if (!in)
1466         return false;
1467
1468     if (!ir_instr_op(in, 0, target, (op < INSTR_STOREP_F || op > INSTR_STOREP_FNC)) ||
1469         !ir_instr_op(in, 1, what, false))
1470     {
1471         ir_instr_delete(in);
1472         return false;
1473     }
1474     vec_push(self->instr, in);
1475     return true;
1476 }
1477
1478 bool ir_block_create_store(ir_block *self, lex_ctx ctx, ir_value *target, ir_value *what)
1479 {
1480     int op = 0;
1481     int vtype;
1482     if (target->vtype == TYPE_VARIANT)
1483         vtype = what->vtype;
1484     else
1485         vtype = target->vtype;
1486
1487 #if 0
1488     if      (vtype == TYPE_FLOAT   && what->vtype == TYPE_INTEGER)
1489         op = INSTR_CONV_ITOF;
1490     else if (vtype == TYPE_INTEGER && what->vtype == TYPE_FLOAT)
1491         op = INSTR_CONV_FTOI;
1492 #endif
1493         op = type_store_instr[vtype];
1494
1495     if (OPTS_FLAG(ADJUST_VECTOR_FIELDS)) {
1496         if (op == INSTR_STORE_FLD && what->fieldtype == TYPE_VECTOR)
1497             op = INSTR_STORE_V;
1498     }
1499
1500     return ir_block_create_store_op(self, ctx, op, target, what);
1501 }
1502
1503 bool ir_block_create_storep(ir_block *self, lex_ctx ctx, ir_value *target, ir_value *what)
1504 {
1505     int op = 0;
1506     int vtype;
1507
1508     if (target->vtype != TYPE_POINTER)
1509         return false;
1510
1511     /* storing using pointer - target is a pointer, type must be
1512      * inferred from source
1513      */
1514     vtype = what->vtype;
1515
1516     op = type_storep_instr[vtype];
1517     if (OPTS_FLAG(ADJUST_VECTOR_FIELDS)) {
1518         if (op == INSTR_STOREP_FLD && what->fieldtype == TYPE_VECTOR)
1519             op = INSTR_STOREP_V;
1520     }
1521
1522     return ir_block_create_store_op(self, ctx, op, target, what);
1523 }
1524
1525 bool ir_block_create_return(ir_block *self, lex_ctx ctx, ir_value *v)
1526 {
1527     ir_instr *in;
1528     if (!ir_check_unreachable(self))
1529         return false;
1530     self->final = true;
1531     self->is_return = true;
1532     in = ir_instr_new(ctx, self, INSTR_RETURN);
1533     if (!in)
1534         return false;
1535
1536     if (v && !ir_instr_op(in, 0, v, false)) {
1537         ir_instr_delete(in);
1538         return false;
1539     }
1540
1541     vec_push(self->instr, in);
1542     return true;
1543 }
1544
1545 bool ir_block_create_if(ir_block *self, lex_ctx ctx, ir_value *v,
1546                         ir_block *ontrue, ir_block *onfalse)
1547 {
1548     ir_instr *in;
1549     if (!ir_check_unreachable(self))
1550         return false;
1551     self->final = true;
1552     /*in = ir_instr_new(ctx, self, (v->vtype == TYPE_STRING ? INSTR_IF_S : INSTR_IF_F));*/
1553     in = ir_instr_new(ctx, self, VINSTR_COND);
1554     if (!in)
1555         return false;
1556
1557     if (!ir_instr_op(in, 0, v, false)) {
1558         ir_instr_delete(in);
1559         return false;
1560     }
1561
1562     in->bops[0] = ontrue;
1563     in->bops[1] = onfalse;
1564
1565     vec_push(self->instr, in);
1566
1567     vec_push(self->exits, ontrue);
1568     vec_push(self->exits, onfalse);
1569     vec_push(ontrue->entries,  self);
1570     vec_push(onfalse->entries, self);
1571     return true;
1572 }
1573
1574 bool ir_block_create_jump(ir_block *self, lex_ctx ctx, ir_block *to)
1575 {
1576     ir_instr *in;
1577     if (!ir_check_unreachable(self))
1578         return false;
1579     self->final = true;
1580     in = ir_instr_new(ctx, self, VINSTR_JUMP);
1581     if (!in)
1582         return false;
1583
1584     in->bops[0] = to;
1585     vec_push(self->instr, in);
1586
1587     vec_push(self->exits, to);
1588     vec_push(to->entries, self);
1589     return true;
1590 }
1591
1592 bool ir_block_create_goto(ir_block *self, lex_ctx ctx, ir_block *to)
1593 {
1594     self->owner->flags |= IR_FLAG_HAS_GOTO;
1595     return ir_block_create_jump(self, ctx, to);
1596 }
1597
1598 ir_instr* ir_block_create_phi(ir_block *self, lex_ctx ctx, const char *label, int ot)
1599 {
1600     ir_value *out;
1601     ir_instr *in;
1602     if (!ir_check_unreachable(self))
1603         return NULL;
1604     in = ir_instr_new(ctx, self, VINSTR_PHI);
1605     if (!in)
1606         return NULL;
1607     out = ir_value_out(self->owner, label, store_value, ot);
1608     if (!out) {
1609         ir_instr_delete(in);
1610         return NULL;
1611     }
1612     if (!ir_instr_op(in, 0, out, true)) {
1613         ir_instr_delete(in);
1614         ir_value_delete(out);
1615         return NULL;
1616     }
1617     vec_push(self->instr, in);
1618     return in;
1619 }
1620
1621 ir_value* ir_phi_value(ir_instr *self)
1622 {
1623     return self->_ops[0];
1624 }
1625
1626 void ir_phi_add(ir_instr* self, ir_block *b, ir_value *v)
1627 {
1628     ir_phi_entry_t pe;
1629
1630     if (!vec_ir_block_find(self->owner->entries, b, NULL)) {
1631         /* Must not be possible to cause this, otherwise the AST
1632          * is doing something wrong.
1633          */
1634         irerror(self->context, "Invalid entry block for PHI");
1635         abort();
1636     }
1637
1638     pe.value = v;
1639     pe.from = b;
1640     vec_push(v->reads, self);
1641     vec_push(self->phi, pe);
1642 }
1643
1644 /* call related code */
1645 ir_instr* ir_block_create_call(ir_block *self, lex_ctx ctx, const char *label, ir_value *func, bool noreturn)
1646 {
1647     ir_value *out;
1648     ir_instr *in;
1649     if (!ir_check_unreachable(self))
1650         return NULL;
1651     in = ir_instr_new(ctx, self, (noreturn ? VINSTR_NRCALL : INSTR_CALL0));
1652     if (!in)
1653         return NULL;
1654     if (noreturn) {
1655         self->final = true;
1656         self->is_return = true;
1657     }
1658     out = ir_value_out(self->owner, label, (func->outtype == TYPE_VOID) ? store_return : store_value, func->outtype);
1659     if (!out) {
1660         ir_instr_delete(in);
1661         return NULL;
1662     }
1663     if (!ir_instr_op(in, 0, out, true) ||
1664         !ir_instr_op(in, 1, func, false))
1665     {
1666         ir_instr_delete(in);
1667         ir_value_delete(out);
1668         return NULL;
1669     }
1670     vec_push(self->instr, in);
1671     /*
1672     if (noreturn) {
1673         if (!ir_block_create_return(self, ctx, NULL)) {
1674             compile_error(ctx, "internal error: failed to generate dummy-return instruction");
1675             ir_instr_delete(in);
1676             return NULL;
1677         }
1678     }
1679     */
1680     return in;
1681 }
1682
1683 ir_value* ir_call_value(ir_instr *self)
1684 {
1685     return self->_ops[0];
1686 }
1687
1688 void ir_call_param(ir_instr* self, ir_value *v)
1689 {
1690     vec_push(self->params, v);
1691     vec_push(v->reads, self);
1692 }
1693
1694 /* binary op related code */
1695
1696 ir_value* ir_block_create_binop(ir_block *self, lex_ctx ctx,
1697                                 const char *label, int opcode,
1698                                 ir_value *left, ir_value *right)
1699 {
1700     int ot = TYPE_VOID;
1701     switch (opcode) {
1702         case INSTR_ADD_F:
1703         case INSTR_SUB_F:
1704         case INSTR_DIV_F:
1705         case INSTR_MUL_F:
1706         case INSTR_MUL_V:
1707         case INSTR_AND:
1708         case INSTR_OR:
1709 #if 0
1710         case INSTR_AND_I:
1711         case INSTR_AND_IF:
1712         case INSTR_AND_FI:
1713         case INSTR_OR_I:
1714         case INSTR_OR_IF:
1715         case INSTR_OR_FI:
1716 #endif
1717         case INSTR_BITAND:
1718         case INSTR_BITOR:
1719 #if 0
1720         case INSTR_SUB_S: /* -- offset of string as float */
1721         case INSTR_MUL_IF:
1722         case INSTR_MUL_FI:
1723         case INSTR_DIV_IF:
1724         case INSTR_DIV_FI:
1725         case INSTR_BITOR_IF:
1726         case INSTR_BITOR_FI:
1727         case INSTR_BITAND_FI:
1728         case INSTR_BITAND_IF:
1729         case INSTR_EQ_I:
1730         case INSTR_NE_I:
1731 #endif
1732             ot = TYPE_FLOAT;
1733             break;
1734 #if 0
1735         case INSTR_ADD_I:
1736         case INSTR_ADD_IF:
1737         case INSTR_ADD_FI:
1738         case INSTR_SUB_I:
1739         case INSTR_SUB_FI:
1740         case INSTR_SUB_IF:
1741         case INSTR_MUL_I:
1742         case INSTR_DIV_I:
1743         case INSTR_BITAND_I:
1744         case INSTR_BITOR_I:
1745         case INSTR_XOR_I:
1746         case INSTR_RSHIFT_I:
1747         case INSTR_LSHIFT_I:
1748             ot = TYPE_INTEGER;
1749             break;
1750 #endif
1751         case INSTR_ADD_V:
1752         case INSTR_SUB_V:
1753         case INSTR_MUL_VF:
1754         case INSTR_MUL_FV:
1755 #if 0
1756         case INSTR_DIV_VF:
1757         case INSTR_MUL_IV:
1758         case INSTR_MUL_VI:
1759 #endif
1760             ot = TYPE_VECTOR;
1761             break;
1762 #if 0
1763         case INSTR_ADD_SF:
1764             ot = TYPE_POINTER;
1765             break;
1766 #endif
1767         default:
1768             /* ranges: */
1769             /* boolean operations result in floats */
1770             if (opcode >= INSTR_EQ_F && opcode <= INSTR_GT)
1771                 ot = TYPE_FLOAT;
1772             else if (opcode >= INSTR_LE && opcode <= INSTR_GT)
1773                 ot = TYPE_FLOAT;
1774 #if 0
1775             else if (opcode >= INSTR_LE_I && opcode <= INSTR_EQ_FI)
1776                 ot = TYPE_FLOAT;
1777 #endif
1778             break;
1779     };
1780     if (ot == TYPE_VOID) {
1781         /* The AST or parser were supposed to check this! */
1782         return NULL;
1783     }
1784
1785     return ir_block_create_general_instr(self, ctx, label, opcode, left, right, ot);
1786 }
1787
1788 ir_value* ir_block_create_unary(ir_block *self, lex_ctx ctx,
1789                                 const char *label, int opcode,
1790                                 ir_value *operand)
1791 {
1792     int ot = TYPE_FLOAT;
1793     switch (opcode) {
1794         case INSTR_NOT_F:
1795         case INSTR_NOT_V:
1796         case INSTR_NOT_S:
1797         case INSTR_NOT_ENT:
1798         case INSTR_NOT_FNC:
1799 #if 0
1800         case INSTR_NOT_I:
1801 #endif
1802             ot = TYPE_FLOAT;
1803             break;
1804         /* QC doesn't have other unary operations. We expect extensions to fill
1805          * the above list, otherwise we assume out-type = in-type, eg for an
1806          * unary minus
1807          */
1808         default:
1809             ot = operand->vtype;
1810             break;
1811     };
1812     if (ot == TYPE_VOID) {
1813         /* The AST or parser were supposed to check this! */
1814         return NULL;
1815     }
1816
1817     /* let's use the general instruction creator and pass NULL for OPB */
1818     return ir_block_create_general_instr(self, ctx, label, opcode, operand, NULL, ot);
1819 }
1820
1821 ir_value* ir_block_create_general_instr(ir_block *self, lex_ctx ctx, const char *label,
1822                                         int op, ir_value *a, ir_value *b, int outype)
1823 {
1824     ir_instr *instr;
1825     ir_value *out;
1826
1827     out = ir_value_out(self->owner, label, store_value, outype);
1828     if (!out)
1829         return NULL;
1830
1831     instr = ir_instr_new(ctx, self, op);
1832     if (!instr) {
1833         ir_value_delete(out);
1834         return NULL;
1835     }
1836
1837     if (!ir_instr_op(instr, 0, out, true) ||
1838         !ir_instr_op(instr, 1, a, false) ||
1839         !ir_instr_op(instr, 2, b, false) )
1840     {
1841         goto on_error;
1842     }
1843
1844     vec_push(self->instr, instr);
1845
1846     return out;
1847 on_error:
1848     ir_instr_delete(instr);
1849     ir_value_delete(out);
1850     return NULL;
1851 }
1852
1853 ir_value* ir_block_create_fieldaddress(ir_block *self, lex_ctx ctx, const char *label, ir_value *ent, ir_value *field)
1854 {
1855     ir_value *v;
1856
1857     /* Support for various pointer types todo if so desired */
1858     if (ent->vtype != TYPE_ENTITY)
1859         return NULL;
1860
1861     if (field->vtype != TYPE_FIELD)
1862         return NULL;
1863
1864     v = ir_block_create_general_instr(self, ctx, label, INSTR_ADDRESS, ent, field, TYPE_POINTER);
1865     v->fieldtype = field->fieldtype;
1866     return v;
1867 }
1868
1869 ir_value* ir_block_create_load_from_ent(ir_block *self, lex_ctx ctx, const char *label, ir_value *ent, ir_value *field, int outype)
1870 {
1871     int op;
1872     if (ent->vtype != TYPE_ENTITY)
1873         return NULL;
1874
1875     /* at some point we could redirect for TYPE_POINTER... but that could lead to carelessness */
1876     if (field->vtype != TYPE_FIELD)
1877         return NULL;
1878
1879     switch (outype)
1880     {
1881         case TYPE_FLOAT:    op = INSTR_LOAD_F;   break;
1882         case TYPE_VECTOR:   op = INSTR_LOAD_V;   break;
1883         case TYPE_STRING:   op = INSTR_LOAD_S;   break;
1884         case TYPE_FIELD:    op = INSTR_LOAD_FLD; break;
1885         case TYPE_ENTITY:   op = INSTR_LOAD_ENT; break;
1886         case TYPE_FUNCTION: op = INSTR_LOAD_FNC; break;
1887 #if 0
1888         case TYPE_POINTER: op = INSTR_LOAD_I;   break;
1889         case TYPE_INTEGER: op = INSTR_LOAD_I;   break;
1890 #endif
1891         default:
1892             irerror(self->context, "invalid type for ir_block_create_load_from_ent: %s", type_name[outype]);
1893             return NULL;
1894     }
1895
1896     return ir_block_create_general_instr(self, ctx, label, op, ent, field, outype);
1897 }
1898
1899 /* PHI resolving breaks the SSA, and must thus be the last
1900  * step before life-range calculation.
1901  */
1902
1903 static bool ir_block_naive_phi(ir_block *self);
1904 bool ir_function_naive_phi(ir_function *self)
1905 {
1906     size_t i;
1907
1908     for (i = 0; i < vec_size(self->blocks); ++i)
1909     {
1910         if (!ir_block_naive_phi(self->blocks[i]))
1911             return false;
1912     }
1913     return true;
1914 }
1915
1916 #if 0
1917 static bool ir_naive_phi_emit_store(ir_block *block, size_t iid, ir_value *old, ir_value *what)
1918 {
1919     ir_instr *instr;
1920     size_t i;
1921
1922     /* create a store */
1923     if (!ir_block_create_store(block, old, what))
1924         return false;
1925
1926     /* we now move it up */
1927     instr = vec_last(block->instr);
1928     for (i = vec_size(block->instr)-1; i > iid; --i)
1929         block->instr[i] = block->instr[i-1];
1930     block->instr[i] = instr;
1931
1932     return true;
1933 }
1934 #endif
1935
1936 static bool ir_block_naive_phi(ir_block *self)
1937 {
1938     size_t i, p; /*, w;*/
1939     /* FIXME: optionally, create_phi can add the phis
1940      * to a list so we don't need to loop through blocks
1941      * - anyway: "don't optimize YET"
1942      */
1943     for (i = 0; i < vec_size(self->instr); ++i)
1944     {
1945         ir_instr *instr = self->instr[i];
1946         if (instr->opcode != VINSTR_PHI)
1947             continue;
1948
1949         vec_remove(self->instr, i, 1);
1950         --i; /* NOTE: i+1 below */
1951
1952         for (p = 0; p < vec_size(instr->phi); ++p)
1953         {
1954             ir_value *v = instr->phi[p].value;
1955             ir_block *b = instr->phi[p].from;
1956
1957             if (v->store == store_value &&
1958                 vec_size(v->reads) == 1 &&
1959                 vec_size(v->writes) == 1)
1960             {
1961                 /* replace the value */
1962                 if (!ir_instr_op(v->writes[0], 0, instr->_ops[0], true))
1963                     return false;
1964             }
1965             else
1966             {
1967                 /* force a move instruction */
1968                 ir_instr *prevjump = vec_last(b->instr);
1969                 vec_pop(b->instr);
1970                 b->final = false;
1971                 instr->_ops[0]->store = store_global;
1972                 if (!ir_block_create_store(b, instr->context, instr->_ops[0], v))
1973                     return false;
1974                 instr->_ops[0]->store = store_value;
1975                 vec_push(b->instr, prevjump);
1976                 b->final = true;
1977             }
1978
1979 #if 0
1980             ir_value *v = instr->phi[p].value;
1981             for (w = 0; w < vec_size(v->writes); ++w) {
1982                 ir_value *old;
1983
1984                 if (!v->writes[w]->_ops[0])
1985                     continue;
1986
1987                 /* When the write was to a global, we have to emit a mov */
1988                 old = v->writes[w]->_ops[0];
1989
1990                 /* The original instruction now writes to the PHI target local */
1991                 if (v->writes[w]->_ops[0] == v)
1992                     v->writes[w]->_ops[0] = instr->_ops[0];
1993
1994                 if (old->store != store_value && old->store != store_local && old->store != store_param)
1995                 {
1996                     /* If it originally wrote to a global we need to store the value
1997                      * there as welli
1998                      */
1999                     if (!ir_naive_phi_emit_store(self, i+1, old, v))
2000                         return false;
2001                     if (i+1 < vec_size(self->instr))
2002                         instr = self->instr[i+1];
2003                     else
2004                         instr = NULL;
2005                     /* In case I forget and access instr later, it'll be NULL
2006                      * when it's a problem, to make sure we crash, rather than accessing
2007                      * invalid data.
2008                      */
2009                 }
2010                 else
2011                 {
2012                     /* If it didn't, we can replace all reads by the phi target now. */
2013                     size_t r;
2014                     for (r = 0; r < vec_size(old->reads); ++r)
2015                     {
2016                         size_t op;
2017                         ir_instr *ri = old->reads[r];
2018                         for (op = 0; op < vec_size(ri->phi); ++op) {
2019                             if (ri->phi[op].value == old)
2020                                 ri->phi[op].value = v;
2021                         }
2022                         for (op = 0; op < 3; ++op) {
2023                             if (ri->_ops[op] == old)
2024                                 ri->_ops[op] = v;
2025                         }
2026                     }
2027                 }
2028             }
2029 #endif
2030         }
2031         ir_instr_delete(instr);
2032     }
2033     return true;
2034 }
2035
2036 /***********************************************************************
2037  *IR Temp allocation code
2038  * Propagating value life ranges by walking through the function backwards
2039  * until no more changes are made.
2040  * In theory this should happen once more than once for every nested loop
2041  * level.
2042  * Though this implementation might run an additional time for if nests.
2043  */
2044
2045 /* Enumerate instructions used by value's life-ranges
2046  */
2047 static void ir_block_enumerate(ir_block *self, size_t *_eid)
2048 {
2049     size_t i;
2050     size_t eid = *_eid;
2051     for (i = 0; i < vec_size(self->instr); ++i)
2052     {
2053         self->instr[i]->eid = eid++;
2054     }
2055     *_eid = eid;
2056 }
2057
2058 /* Enumerate blocks and instructions.
2059  * The block-enumeration is unordered!
2060  * We do not really use the block enumreation, however
2061  * the instruction enumeration is important for life-ranges.
2062  */
2063 void ir_function_enumerate(ir_function *self)
2064 {
2065     size_t i;
2066     size_t instruction_id = 1;
2067     for (i = 0; i < vec_size(self->blocks); ++i)
2068     {
2069         self->blocks[i]->eid = i;
2070         self->blocks[i]->run_id = 0;
2071         ir_block_enumerate(self->blocks[i], &instruction_id);
2072     }
2073 }
2074
2075 static bool ir_block_life_propagate(ir_block *b, ir_block *prev, bool *changed);
2076 bool ir_function_calculate_liferanges(ir_function *self)
2077 {
2078     size_t i, s;
2079     bool changed;
2080
2081     /* parameters live at 0 */
2082     for (i = 0; i < vec_size(self->params); ++i)
2083         ir_value_life_merge(self->locals[i], 0);
2084
2085     do {
2086         self->run_id++;
2087         changed = false;
2088         for (i = 0; i != vec_size(self->blocks); ++i)
2089         {
2090             if (self->blocks[i]->is_return)
2091             {
2092                 vec_free(self->blocks[i]->living);
2093                 if (!ir_block_life_propagate(self->blocks[i], NULL, &changed))
2094                     return false;
2095             }
2096         }
2097     } while (changed);
2098     if (vec_size(self->blocks)) {
2099         ir_block *block = self->blocks[0];
2100         for (i = 0; i < vec_size(block->living); ++i) {
2101             ir_value *v = block->living[i];
2102             if (v->store != store_local)
2103                 continue;
2104             if (v->vtype == TYPE_VECTOR)
2105                 continue;
2106             self->flags |= IR_FLAG_HAS_UNINITIALIZED;
2107             /* find the instruction reading from it */
2108             for (s = 0; s < vec_size(v->reads); ++s) {
2109                 if (v->reads[s]->eid == v->life[0].end)
2110                     break;
2111             }
2112             if (s < vec_size(v->reads)) {
2113                 if (irwarning(v->context, WARN_USED_UNINITIALIZED,
2114                               "variable `%s` may be used uninitialized in this function\n"
2115                               " -> %s:%i",
2116                               v->name,
2117                               v->reads[s]->context.file, v->reads[s]->context.line)
2118                    )
2119                 {
2120                     return false;
2121                 }
2122                 continue;
2123             }
2124             if (v->memberof) {
2125                 ir_value *vec = v->memberof;
2126                 for (s = 0; s < vec_size(vec->reads); ++s) {
2127                     if (vec->reads[s]->eid == v->life[0].end)
2128                         break;
2129                 }
2130                 if (s < vec_size(vec->reads)) {
2131                     if (irwarning(v->context, WARN_USED_UNINITIALIZED,
2132                                   "variable `%s` may be used uninitialized in this function\n"
2133                                   " -> %s:%i",
2134                                   v->name,
2135                                   vec->reads[s]->context.file, vec->reads[s]->context.line)
2136                        )
2137                     {
2138                         return false;
2139                     }
2140                     continue;
2141                 }
2142             }
2143             if (irwarning(v->context, WARN_USED_UNINITIALIZED,
2144                           "variable `%s` may be used uninitialized in this function", v->name))
2145             {
2146                 return false;
2147             }
2148         }
2149     }
2150     return true;
2151 }
2152
2153 /* Local-value allocator
2154  * After finishing creating the liferange of all values used in a function
2155  * we can allocate their global-positions.
2156  * This is the counterpart to register-allocation in register machines.
2157  */
2158 typedef struct {
2159     ir_value **locals;
2160     size_t    *sizes;
2161     size_t    *positions;
2162     bool      *unique;
2163 } function_allocator;
2164
2165 static bool function_allocator_alloc(function_allocator *alloc, ir_value *var)
2166 {
2167     ir_value *slot;
2168     size_t vsize = ir_value_sizeof(var);
2169
2170     var->code.local = vec_size(alloc->locals);
2171
2172     slot = ir_value_var("reg", store_global, var->vtype);
2173     if (!slot)
2174         return false;
2175
2176     if (!ir_value_life_merge_into(slot, var))
2177         goto localerror;
2178
2179     vec_push(alloc->locals, slot);
2180     vec_push(alloc->sizes, vsize);
2181     vec_push(alloc->unique, var->unique_life);
2182
2183     return true;
2184
2185 localerror:
2186     ir_value_delete(slot);
2187     return false;
2188 }
2189
2190 static bool ir_function_allocator_assign(ir_function *self, function_allocator *alloc, ir_value *v)
2191 {
2192     size_t a;
2193     ir_value *slot;
2194
2195     for (a = 0; a < vec_size(alloc->locals); ++a)
2196     {
2197         /* if it's reserved for a unique liferange: skip */
2198         if (alloc->unique[a])
2199             continue;
2200
2201         slot = alloc->locals[a];
2202
2203         /* never resize parameters
2204          * will be required later when overlapping temps + locals
2205          */
2206         if (a < vec_size(self->params) &&
2207             alloc->sizes[a] < ir_value_sizeof(v))
2208         {
2209             continue;
2210         }
2211
2212         if (ir_values_overlap(v, slot))
2213             continue;
2214
2215         if (!ir_value_life_merge_into(slot, v))
2216             return false;
2217
2218         /* adjust size for this slot */
2219         if (alloc->sizes[a] < ir_value_sizeof(v))
2220             alloc->sizes[a] = ir_value_sizeof(v);
2221
2222         v->code.local = a;
2223         return true;
2224     }
2225     if (a >= vec_size(alloc->locals)) {
2226         if (!function_allocator_alloc(alloc, v))
2227             return false;
2228     }
2229     return true;
2230 }
2231
2232 bool ir_function_allocate_locals(ir_function *self)
2233 {
2234     size_t i;
2235     bool   retval = true;
2236     size_t pos;
2237     bool   opt_gt = OPTS_OPTIMIZATION(OPTIM_GLOBAL_TEMPS);
2238
2239     ir_value *v;
2240
2241     function_allocator lockalloc, globalloc;
2242
2243     if (!vec_size(self->locals) && !vec_size(self->values))
2244         return true;
2245
2246     globalloc.locals    = NULL;
2247     globalloc.sizes     = NULL;
2248     globalloc.positions = NULL;
2249     globalloc.unique    = NULL;
2250     lockalloc.locals    = NULL;
2251     lockalloc.sizes     = NULL;
2252     lockalloc.positions = NULL;
2253     lockalloc.unique    = NULL;
2254
2255     for (i = 0; i < vec_size(self->locals); ++i)
2256     {
2257         v = self->locals[i];
2258         if (!OPTS_OPTIMIZATION(OPTIM_LOCAL_TEMPS)) {
2259             v->locked      = true;
2260             v->unique_life = true;
2261         }
2262         else if (i >= vec_size(self->params))
2263             break;
2264         else
2265             v->locked = true; /* lock parameters locals */
2266         if (!function_allocator_alloc((v->locked || !opt_gt ? &lockalloc : &globalloc), v))
2267             goto error;
2268     }
2269     for (; i < vec_size(self->locals); ++i)
2270     {
2271         v = self->locals[i];
2272         if (!vec_size(v->life))
2273             continue;
2274         if (!ir_function_allocator_assign(self, (v->locked || !opt_gt ? &lockalloc : &globalloc), v))
2275             goto error;
2276     }
2277
2278     /* Allocate a slot for any value that still exists */
2279     for (i = 0; i < vec_size(self->values); ++i)
2280     {
2281         v = self->values[i];
2282
2283         if (!vec_size(v->life))
2284             continue;
2285
2286         /* CALL optimization:
2287          * If the value is a parameter-temp: 1 write, 1 read from a CALL
2288          * and it's not "locked", write it to the OFS_PARM directly.
2289          */
2290         if (OPTS_OPTIMIZATION(OPTIM_CALL_STORES) && !v->locked && !v->unique_life) {
2291             if (vec_size(v->reads) == 1 && vec_size(v->writes) == 1 &&
2292                 (v->reads[0]->opcode == VINSTR_NRCALL ||
2293                  (v->reads[0]->opcode >= INSTR_CALL0 && v->reads[0]->opcode <= INSTR_CALL8)
2294                 )
2295                )
2296             {
2297                 size_t    param;
2298                 ir_instr *call = v->reads[0];
2299                 if (!vec_ir_value_find(call->params, v, &param)) {
2300                     irerror(call->context, "internal error: unlocked parameter %s not found", v->name);
2301                     goto error;
2302                 }
2303
2304                 ++opts_optimizationcount[OPTIM_CALL_STORES];
2305                 v->callparam = true;
2306                 if (param < 8)
2307                     ir_value_code_setaddr(v, OFS_PARM0 + 3*param);
2308                 else {
2309                     ir_value *ep;
2310                     param -= 8;
2311                     if (vec_size(self->owner->extparam_protos) <= param)
2312                         ep = ir_gen_extparam_proto(self->owner);
2313                     else
2314                         ep = self->owner->extparam_protos[param];
2315                     ir_instr_op(v->writes[0], 0, ep, true);
2316                     call->params[param+8] = ep;
2317                 }
2318                 continue;
2319             }
2320             if (vec_size(v->writes) == 1 && v->writes[0]->opcode == INSTR_CALL0)
2321             {
2322                 v->store = store_return;
2323                 if (v->members[0]) v->members[0]->store = store_return;
2324                 if (v->members[1]) v->members[1]->store = store_return;
2325                 if (v->members[2]) v->members[2]->store = store_return;
2326                 ++opts_optimizationcount[OPTIM_CALL_STORES];
2327                 continue;
2328             }
2329         }
2330
2331         if (!ir_function_allocator_assign(self, (v->locked || !opt_gt ? &lockalloc : &globalloc), v))
2332             goto error;
2333     }
2334
2335     if (!lockalloc.sizes && !globalloc.sizes) {
2336         goto cleanup;
2337     }
2338     vec_push(lockalloc.positions, 0);
2339     vec_push(globalloc.positions, 0);
2340
2341     /* Adjust slot positions based on sizes */
2342     if (lockalloc.sizes) {
2343         pos = (vec_size(lockalloc.sizes) ? lockalloc.positions[0] : 0);
2344         for (i = 1; i < vec_size(lockalloc.sizes); ++i)
2345         {
2346             pos = lockalloc.positions[i-1] + lockalloc.sizes[i-1];
2347             vec_push(lockalloc.positions, pos);
2348         }
2349         self->allocated_locals = pos + vec_last(lockalloc.sizes);
2350     }
2351     if (globalloc.sizes) {
2352         pos = (vec_size(globalloc.sizes) ? globalloc.positions[0] : 0);
2353         for (i = 1; i < vec_size(globalloc.sizes); ++i)
2354         {
2355             pos = globalloc.positions[i-1] + globalloc.sizes[i-1];
2356             vec_push(globalloc.positions, pos);
2357         }
2358         self->globaltemps = pos + vec_last(globalloc.sizes);
2359     }
2360
2361     /* Locals need to know their new position */
2362     for (i = 0; i < vec_size(self->locals); ++i) {
2363         v = self->locals[i];
2364         if (i >= vec_size(self->params) && !vec_size(v->life))
2365             continue;
2366         if (v->locked || !opt_gt)
2367             v->code.local = lockalloc.positions[v->code.local];
2368         else
2369             v->code.local = globalloc.positions[v->code.local];
2370     }
2371     /* Take over the actual slot positions on values */
2372     for (i = 0; i < vec_size(self->values); ++i) {
2373         v = self->values[i];
2374         if (!vec_size(v->life))
2375             continue;
2376         if (v->locked || !opt_gt)
2377             v->code.local = lockalloc.positions[v->code.local];
2378         else
2379             v->code.local = globalloc.positions[v->code.local];
2380     }
2381
2382     goto cleanup;
2383
2384 error:
2385     retval = false;
2386 cleanup:
2387     for (i = 0; i < vec_size(lockalloc.locals); ++i)
2388         ir_value_delete(lockalloc.locals[i]);
2389     for (i = 0; i < vec_size(globalloc.locals); ++i)
2390         ir_value_delete(globalloc.locals[i]);
2391     vec_free(globalloc.unique);
2392     vec_free(globalloc.locals);
2393     vec_free(globalloc.sizes);
2394     vec_free(globalloc.positions);
2395     vec_free(lockalloc.unique);
2396     vec_free(lockalloc.locals);
2397     vec_free(lockalloc.sizes);
2398     vec_free(lockalloc.positions);
2399     return retval;
2400 }
2401
2402 /* Get information about which operand
2403  * is read from, or written to.
2404  */
2405 static void ir_op_read_write(int op, size_t *read, size_t *write)
2406 {
2407     switch (op)
2408     {
2409     case VINSTR_JUMP:
2410     case INSTR_GOTO:
2411         *write = 0;
2412         *read = 0;
2413         break;
2414     case INSTR_IF:
2415     case INSTR_IFNOT:
2416 #if 0
2417     case INSTR_IF_S:
2418     case INSTR_IFNOT_S:
2419 #endif
2420     case INSTR_RETURN:
2421     case VINSTR_COND:
2422         *write = 0;
2423         *read = 1;
2424         break;
2425     case INSTR_STOREP_F:
2426     case INSTR_STOREP_V:
2427     case INSTR_STOREP_S:
2428     case INSTR_STOREP_ENT:
2429     case INSTR_STOREP_FLD:
2430     case INSTR_STOREP_FNC:
2431         *write = 0;
2432         *read  = 7;
2433         break;
2434     default:
2435         *write = 1;
2436         *read = 6;
2437         break;
2438     };
2439 }
2440
2441 static bool ir_block_living_add_instr(ir_block *self, size_t eid)
2442 {
2443     size_t i;
2444     bool changed = false;
2445     bool tempbool;
2446     for (i = 0; i != vec_size(self->living); ++i)
2447     {
2448         tempbool = ir_value_life_merge(self->living[i], eid);
2449         changed = changed || tempbool;
2450     }
2451     return changed;
2452 }
2453
2454 static bool ir_block_living_lock(ir_block *self)
2455 {
2456     size_t i;
2457     bool changed = false;
2458     for (i = 0; i != vec_size(self->living); ++i)
2459     {
2460         if (!self->living[i]->locked) {
2461             self->living[i]->locked = true;
2462             changed = true;
2463         }
2464     }
2465     return changed;
2466 }
2467
2468 static bool ir_block_life_prop_previous(ir_block* self, ir_block *prev, bool *changed)
2469 {
2470     size_t i;
2471
2472     (void)changed;
2473
2474     /* values which have been read in a previous iteration are now
2475      * in the "living" array even if the previous block doesn't use them.
2476      * So we have to remove whatever does not exist in the previous block.
2477      * They will be re-added on-read, but the liferange merge won't cause
2478      * a change.
2479     for (i = 0; i < vec_size(self->living); ++i)
2480     {
2481         if (!vec_ir_value_find(prev->living, self->living[i], NULL)) {
2482             vec_remove(self->living, i, 1);
2483             --i;
2484         }
2485     }
2486      */
2487
2488     /* Whatever the previous block still has in its living set
2489      * must now be added to ours as well.
2490      */
2491     for (i = 0; i < vec_size(prev->living); ++i)
2492     {
2493         if (vec_ir_value_find(self->living, prev->living[i], NULL))
2494             continue;
2495         vec_push(self->living, prev->living[i]);
2496         /*
2497         irerror(self->contextt from prev: %s", self->label, prev->living[i]->_name);
2498         */
2499     }
2500     return true;
2501 }
2502
2503 static bool ir_block_life_propagate(ir_block *self, ir_block *prev, bool *changed)
2504 {
2505     ir_instr *instr;
2506     ir_value *value;
2507     bool  tempbool;
2508     size_t i, o, p, mem;
2509     /* bitmasks which operands are read from or written to */
2510     size_t read, write;
2511     char dbg_ind[16];
2512     dbg_ind[0] = '#';
2513     dbg_ind[1] = '0';
2514     (void)dbg_ind;
2515
2516     if (prev)
2517     {
2518         if (!ir_block_life_prop_previous(self, prev, changed))
2519             return false;
2520     }
2521
2522     i = vec_size(self->instr);
2523     while (i)
2524     { --i;
2525         instr = self->instr[i];
2526
2527         /* See which operands are read and write operands */
2528         ir_op_read_write(instr->opcode, &read, &write);
2529
2530         if (instr->opcode == INSTR_MUL_VF)
2531         {
2532             /* the float source will get an additional lifetime */
2533             tempbool = ir_value_life_merge(instr->_ops[2], instr->eid+1);
2534             *changed = *changed || tempbool;
2535         }
2536         else if (instr->opcode == INSTR_MUL_FV)
2537         {
2538             /* the float source will get an additional lifetime */
2539             tempbool = ir_value_life_merge(instr->_ops[1], instr->eid+1);
2540             *changed = *changed || tempbool;
2541         }
2542
2543         /* Go through the 3 main operands
2544          * writes first, then reads
2545          */
2546         for (o = 0; o < 3; ++o)
2547         {
2548             if (!instr->_ops[o]) /* no such operand */
2549                 continue;
2550
2551             value = instr->_ops[o];
2552
2553             /* We only care about locals */
2554             /* we also calculate parameter liferanges so that locals
2555              * can take up parameter slots */
2556             if (value->store != store_value &&
2557                 value->store != store_local &&
2558                 value->store != store_param)
2559                 continue;
2560
2561             /* write operands */
2562             /* When we write to a local, we consider it "dead" for the
2563              * remaining upper part of the function, since in SSA a value
2564              * can only be written once (== created)
2565              */
2566             if (write & (1<<o))
2567             {
2568                 size_t idx;
2569                 bool in_living = vec_ir_value_find(self->living, value, &idx);
2570                 if (!in_living)
2571                 {
2572                     /* If the value isn't alive it hasn't been read before... */
2573                     /* TODO: See if the warning can be emitted during parsing or AST processing
2574                      * otherwise have warning printed here.
2575                      * IF printing a warning here: include filecontext_t,
2576                      * and make sure it's only printed once
2577                      * since this function is run multiple times.
2578                      */
2579                     /* con_err( "Value only written %s\n", value->name); */
2580                     tempbool = ir_value_life_merge(value, instr->eid);
2581                     *changed = *changed || tempbool;
2582                 } else {
2583                     /* since 'living' won't contain it
2584                      * anymore, merge the value, since
2585                      * (A) doesn't.
2586                      */
2587                     tempbool = ir_value_life_merge(value, instr->eid);
2588                     *changed = *changed || tempbool;
2589                     /* Then remove */
2590                     vec_remove(self->living, idx, 1);
2591                 }
2592                 /* Removing a vector removes all members */
2593                 for (mem = 0; mem < 3; ++mem) {
2594                     if (value->members[mem] && vec_ir_value_find(self->living, value->members[mem], &idx)) {
2595                         tempbool = ir_value_life_merge(value->members[mem], instr->eid);
2596                         *changed = *changed || tempbool;
2597                         vec_remove(self->living, idx, 1);
2598                     }
2599                 }
2600                 /* Removing the last member removes the vector */
2601                 if (value->memberof) {
2602                     value = value->memberof;
2603                     for (mem = 0; mem < 3; ++mem) {
2604                         if (value->members[mem] && vec_ir_value_find(self->living, value->members[mem], NULL))
2605                             break;
2606                     }
2607                     if (mem == 3 && vec_ir_value_find(self->living, value, &idx)) {
2608                         tempbool = ir_value_life_merge(value, instr->eid);
2609                         *changed = *changed || tempbool;
2610                         vec_remove(self->living, idx, 1);
2611                     }
2612                 }
2613             }
2614         }
2615
2616         for (o = 0; o < 3; ++o)
2617         {
2618             if (!instr->_ops[o]) /* no such operand */
2619                 continue;
2620
2621             value = instr->_ops[o];
2622
2623             /* We only care about locals */
2624             /* we also calculate parameter liferanges so that locals
2625              * can take up parameter slots */
2626             if (value->store != store_value &&
2627                 value->store != store_local &&
2628                 value->store != store_param)
2629                 continue;
2630
2631             /* read operands */
2632             if (read & (1<<o))
2633             {
2634                 if (!vec_ir_value_find(self->living, value, NULL))
2635                     vec_push(self->living, value);
2636                 /* reading adds the full vector */
2637                 if (value->memberof && !vec_ir_value_find(self->living, value->memberof, NULL))
2638                     vec_push(self->living, value->memberof);
2639                 for (mem = 0; mem < 3; ++mem) {
2640                     if (value->members[mem] && !vec_ir_value_find(self->living, value->members[mem], NULL))
2641                         vec_push(self->living, value->members[mem]);
2642                 }
2643             }
2644         }
2645         /* PHI operands are always read operands */
2646         for (p = 0; p < vec_size(instr->phi); ++p)
2647         {
2648             value = instr->phi[p].value;
2649             if (!vec_ir_value_find(self->living, value, NULL))
2650                 vec_push(self->living, value);
2651             /* reading adds the full vector */
2652             if (value->memberof && !vec_ir_value_find(self->living, value->memberof, NULL))
2653                 vec_push(self->living, value->memberof);
2654             for (mem = 0; mem < 3; ++mem) {
2655                 if (value->members[mem] && !vec_ir_value_find(self->living, value->members[mem], NULL))
2656                     vec_push(self->living, value->members[mem]);
2657             }
2658         }
2659
2660         /* on a call, all these values must be "locked" */
2661         if (instr->opcode >= INSTR_CALL0 && instr->opcode <= INSTR_CALL8) {
2662             if (ir_block_living_lock(self))
2663                 *changed = true;
2664         }
2665         /* call params are read operands too */
2666         for (p = 0; p < vec_size(instr->params); ++p)
2667         {
2668             value = instr->params[p];
2669             if (!vec_ir_value_find(self->living, value, NULL))
2670                 vec_push(self->living, value);
2671             /* reading adds the full vector */
2672             if (value->memberof && !vec_ir_value_find(self->living, value->memberof, NULL))
2673                 vec_push(self->living, value->memberof);
2674             for (mem = 0; mem < 3; ++mem) {
2675                 if (value->members[mem] && !vec_ir_value_find(self->living, value->members[mem], NULL))
2676                     vec_push(self->living, value->members[mem]);
2677             }
2678         }
2679
2680         /* (A) */
2681         tempbool = ir_block_living_add_instr(self, instr->eid);
2682         /*con_err( "living added values\n");*/
2683         *changed = *changed || tempbool;
2684
2685     }
2686
2687     if (self->run_id == self->owner->run_id)
2688         return true;
2689
2690     self->run_id = self->owner->run_id;
2691
2692     for (i = 0; i < vec_size(self->entries); ++i)
2693     {
2694         ir_block *entry = self->entries[i];
2695         ir_block_life_propagate(entry, self, changed);
2696     }
2697
2698     return true;
2699 }
2700
2701 /***********************************************************************
2702  *IR Code-Generation
2703  *
2704  * Since the IR has the convention of putting 'write' operands
2705  * at the beginning, we have to rotate the operands of instructions
2706  * properly in order to generate valid QCVM code.
2707  *
2708  * Having destinations at a fixed position is more convenient. In QC
2709  * this is *mostly* OPC,  but FTE adds at least 2 instructions which
2710  * read from from OPA,  and store to OPB rather than OPC.   Which is
2711  * partially the reason why the implementation of these instructions
2712  * in darkplaces has been delayed for so long.
2713  *
2714  * Breaking conventions is annoying...
2715  */
2716 static bool ir_builder_gen_global(ir_builder *self, ir_value *global, bool islocal);
2717
2718 static bool gen_global_field(ir_value *global)
2719 {
2720     if (global->hasvalue)
2721     {
2722         ir_value *fld = global->constval.vpointer;
2723         if (!fld) {
2724             irerror(global->context, "Invalid field constant with no field: %s", global->name);
2725             return false;
2726         }
2727
2728         /* copy the field's value */
2729         ir_value_code_setaddr(global, vec_size(code_globals));
2730         vec_push(code_globals, fld->code.fieldaddr);
2731         if (global->fieldtype == TYPE_VECTOR) {
2732             vec_push(code_globals, fld->code.fieldaddr+1);
2733             vec_push(code_globals, fld->code.fieldaddr+2);
2734         }
2735     }
2736     else
2737     {
2738         ir_value_code_setaddr(global, vec_size(code_globals));
2739         vec_push(code_globals, 0);
2740         if (global->fieldtype == TYPE_VECTOR) {
2741             vec_push(code_globals, 0);
2742             vec_push(code_globals, 0);
2743         }
2744     }
2745     if (global->code.globaladdr < 0)
2746         return false;
2747     return true;
2748 }
2749
2750 static bool gen_global_pointer(ir_value *global)
2751 {
2752     if (global->hasvalue)
2753     {
2754         ir_value *target = global->constval.vpointer;
2755         if (!target) {
2756             irerror(global->context, "Invalid pointer constant: %s", global->name);
2757             /* NULL pointers are pointing to the NULL constant, which also
2758              * sits at address 0, but still has an ir_value for itself.
2759              */
2760             return false;
2761         }
2762
2763         /* Here, relocations ARE possible - in fteqcc-enhanced-qc:
2764          * void() foo; <- proto
2765          * void() *fooptr = &foo;
2766          * void() foo = { code }
2767          */
2768         if (!target->code.globaladdr) {
2769             /* FIXME: Check for the constant nullptr ir_value!
2770              * because then code.globaladdr being 0 is valid.
2771              */
2772             irerror(global->context, "FIXME: Relocation support");
2773             return false;
2774         }
2775
2776         ir_value_code_setaddr(global, vec_size(code_globals));
2777         vec_push(code_globals, target->code.globaladdr);
2778     }
2779     else
2780     {
2781         ir_value_code_setaddr(global, vec_size(code_globals));
2782         vec_push(code_globals, 0);
2783     }
2784     if (global->code.globaladdr < 0)
2785         return false;
2786     return true;
2787 }
2788
2789 static bool gen_blocks_recursive(ir_function *func, ir_block *block)
2790 {
2791     prog_section_statement stmt;
2792     ir_instr *instr;
2793     ir_block *target;
2794     ir_block *ontrue;
2795     ir_block *onfalse;
2796     size_t    stidx;
2797     size_t    i;
2798
2799 tailcall:
2800     block->generated = true;
2801     block->code_start = vec_size(code_statements);
2802     for (i = 0; i < vec_size(block->instr); ++i)
2803     {
2804         instr = block->instr[i];
2805
2806         if (instr->opcode == VINSTR_PHI) {
2807             irerror(block->context, "cannot generate virtual instruction (phi)");
2808             return false;
2809         }
2810
2811         if (instr->opcode == VINSTR_JUMP) {
2812             target = instr->bops[0];
2813             /* for uncoditional jumps, if the target hasn't been generated
2814              * yet, we generate them right here.
2815              */
2816             if (!target->generated) {
2817                 block = target;
2818                 goto tailcall;
2819             }
2820
2821             /* otherwise we generate a jump instruction */
2822             stmt.opcode = INSTR_GOTO;
2823             stmt.o1.s1 = (target->code_start) - vec_size(code_statements);
2824             stmt.o2.s1 = 0;
2825             stmt.o3.s1 = 0;
2826             if (stmt.o1.s1 != 1)
2827                 code_push_statement(&stmt, instr->context.line);
2828
2829             /* no further instructions can be in this block */
2830             return true;
2831         }
2832
2833         if (instr->opcode == VINSTR_COND) {
2834             ontrue  = instr->bops[0];
2835             onfalse = instr->bops[1];
2836             /* TODO: have the AST signal which block should
2837              * come first: eg. optimize IFs without ELSE...
2838              */
2839
2840             stmt.o1.u1 = ir_value_code_addr(instr->_ops[0]);
2841             stmt.o2.u1 = 0;
2842             stmt.o3.s1 = 0;
2843
2844             if (ontrue->generated) {
2845                 stmt.opcode = INSTR_IF;
2846                 stmt.o2.s1 = (ontrue->code_start) - vec_size(code_statements);
2847                 if (stmt.o2.s1 != 1)
2848                     code_push_statement(&stmt, instr->context.line);
2849             }
2850             if (onfalse->generated) {
2851                 stmt.opcode = INSTR_IFNOT;
2852                 stmt.o2.s1 = (onfalse->code_start) - vec_size(code_statements);
2853                 if (stmt.o2.s1 != 1)
2854                     code_push_statement(&stmt, instr->context.line);
2855             }
2856             if (!ontrue->generated) {
2857                 if (onfalse->generated) {
2858                     block = ontrue;
2859                     goto tailcall;
2860                 }
2861             }
2862             if (!onfalse->generated) {
2863                 if (ontrue->generated) {
2864                     block = onfalse;
2865                     goto tailcall;
2866                 }
2867             }
2868             /* neither ontrue nor onfalse exist */
2869             stmt.opcode = INSTR_IFNOT;
2870             if (!instr->likely) {
2871                 /* Honor the likelyhood hint */
2872                 ir_block *tmp = onfalse;
2873                 stmt.opcode = INSTR_IF;
2874                 onfalse = ontrue;
2875                 ontrue = tmp;
2876             }
2877             stidx = vec_size(code_statements);
2878             code_push_statement(&stmt, instr->context.line);
2879             /* on false we jump, so add ontrue-path */
2880             if (!gen_blocks_recursive(func, ontrue))
2881                 return false;
2882             /* fixup the jump address */
2883             code_statements[stidx].o2.s1 = vec_size(code_statements) - stidx;
2884             /* generate onfalse path */
2885             if (onfalse->generated) {
2886                 /* fixup the jump address */
2887                 code_statements[stidx].o2.s1 = (onfalse->code_start) - (stidx);
2888                 if (stidx+2 == vec_size(code_statements) && code_statements[stidx].o2.s1 == 1) {
2889                     code_statements[stidx] = code_statements[stidx+1];
2890                     if (code_statements[stidx].o1.s1 < 0)
2891                         code_statements[stidx].o1.s1++;
2892                     code_pop_statement();
2893                 }
2894                 stmt.opcode = vec_last(code_statements).opcode;
2895                 if (stmt.opcode == INSTR_GOTO ||
2896                     stmt.opcode == INSTR_IF ||
2897                     stmt.opcode == INSTR_IFNOT ||
2898                     stmt.opcode == INSTR_RETURN ||
2899                     stmt.opcode == INSTR_DONE)
2900                 {
2901                     /* no use jumping from here */
2902                     return true;
2903                 }
2904                 /* may have been generated in the previous recursive call */
2905                 stmt.opcode = INSTR_GOTO;
2906                 stmt.o1.s1 = (onfalse->code_start) - vec_size(code_statements);
2907                 stmt.o2.s1 = 0;
2908                 stmt.o3.s1 = 0;
2909                 if (stmt.o1.s1 != 1)
2910                     code_push_statement(&stmt, instr->context.line);
2911                 return true;
2912             }
2913             else if (stidx+2 == vec_size(code_statements) && code_statements[stidx].o2.s1 == 1) {
2914                 code_statements[stidx] = code_statements[stidx+1];
2915                 if (code_statements[stidx].o1.s1 < 0)
2916                     code_statements[stidx].o1.s1++;
2917                 code_pop_statement();
2918             }
2919             /* if not, generate now */
2920             block = onfalse;
2921             goto tailcall;
2922         }
2923
2924         if ( (instr->opcode >= INSTR_CALL0 && instr->opcode <= INSTR_CALL8)
2925            || instr->opcode == VINSTR_NRCALL)
2926         {
2927             size_t p, first;
2928             ir_value *retvalue;
2929
2930             first = vec_size(instr->params);
2931             if (first > 8)
2932                 first = 8;
2933             for (p = 0; p < first; ++p)
2934             {
2935                 ir_value *param = instr->params[p];
2936                 if (param->callparam)
2937                     continue;
2938
2939                 stmt.opcode = INSTR_STORE_F;
2940                 stmt.o3.u1 = 0;
2941
2942                 if (param->vtype == TYPE_FIELD)
2943                     stmt.opcode = field_store_instr[param->fieldtype];
2944                 else if (param->vtype == TYPE_NIL)
2945                     stmt.opcode = INSTR_STORE_V;
2946                 else
2947                     stmt.opcode = type_store_instr[param->vtype];
2948                 stmt.o1.u1 = ir_value_code_addr(param);
2949                 stmt.o2.u1 = OFS_PARM0 + 3 * p;
2950                 code_push_statement(&stmt, instr->context.line);
2951             }
2952             /* Now handle extparams */
2953             first = vec_size(instr->params);
2954             for (; p < first; ++p)
2955             {
2956                 ir_builder *ir = func->owner;
2957                 ir_value *param = instr->params[p];
2958                 ir_value *targetparam;
2959
2960                 if (param->callparam)
2961                     continue;
2962
2963                 if (p-8 >= vec_size(ir->extparams))
2964                     ir_gen_extparam(ir);
2965
2966                 targetparam = ir->extparams[p-8];
2967
2968                 stmt.opcode = INSTR_STORE_F;
2969                 stmt.o3.u1 = 0;
2970
2971                 if (param->vtype == TYPE_FIELD)
2972                     stmt.opcode = field_store_instr[param->fieldtype];
2973                 else if (param->vtype == TYPE_NIL)
2974                     stmt.opcode = INSTR_STORE_V;
2975                 else
2976                     stmt.opcode = type_store_instr[param->vtype];
2977                 stmt.o1.u1 = ir_value_code_addr(param);
2978                 stmt.o2.u1 = ir_value_code_addr(targetparam);
2979                 code_push_statement(&stmt, instr->context.line);
2980             }
2981
2982             stmt.opcode = INSTR_CALL0 + vec_size(instr->params);
2983             if (stmt.opcode > INSTR_CALL8)
2984                 stmt.opcode = INSTR_CALL8;
2985             stmt.o1.u1 = ir_value_code_addr(instr->_ops[1]);
2986             stmt.o2.u1 = 0;
2987             stmt.o3.u1 = 0;
2988             code_push_statement(&stmt, instr->context.line);
2989
2990             retvalue = instr->_ops[0];
2991             if (retvalue && retvalue->store != store_return &&
2992                 (retvalue->store == store_global || vec_size(retvalue->life)))
2993             {
2994                 /* not to be kept in OFS_RETURN */
2995                 if (retvalue->vtype == TYPE_FIELD && OPTS_FLAG(ADJUST_VECTOR_FIELDS))
2996                     stmt.opcode = field_store_instr[retvalue->fieldtype];
2997                 else
2998                     stmt.opcode = type_store_instr[retvalue->vtype];
2999                 stmt.o1.u1 = OFS_RETURN;
3000                 stmt.o2.u1 = ir_value_code_addr(retvalue);
3001                 stmt.o3.u1 = 0;
3002                 code_push_statement(&stmt, instr->context.line);
3003             }
3004             continue;
3005         }
3006
3007         if (instr->opcode == INSTR_STATE) {
3008             irerror(block->context, "TODO: state instruction");
3009             return false;
3010         }
3011
3012         stmt.opcode = instr->opcode;
3013         stmt.o1.u1 = 0;
3014         stmt.o2.u1 = 0;
3015         stmt.o3.u1 = 0;
3016
3017         /* This is the general order of operands */
3018         if (instr->_ops[0])
3019             stmt.o3.u1 = ir_value_code_addr(instr->_ops[0]);
3020
3021         if (instr->_ops[1])
3022             stmt.o1.u1 = ir_value_code_addr(instr->_ops[1]);
3023
3024         if (instr->_ops[2])
3025             stmt.o2.u1 = ir_value_code_addr(instr->_ops[2]);
3026
3027         if (stmt.opcode == INSTR_RETURN || stmt.opcode == INSTR_DONE)
3028         {
3029             stmt.o1.u1 = stmt.o3.u1;
3030             stmt.o3.u1 = 0;
3031         }
3032         else if ((stmt.opcode >= INSTR_STORE_F &&
3033                   stmt.opcode <= INSTR_STORE_FNC) ||
3034                  (stmt.opcode >= INSTR_STOREP_F &&
3035                   stmt.opcode <= INSTR_STOREP_FNC))
3036         {
3037             /* 2-operand instructions with A -> B */
3038             stmt.o2.u1 = stmt.o3.u1;
3039             stmt.o3.u1 = 0;
3040
3041             /* tiny optimization, don't output
3042              * STORE a, a
3043              */
3044             if (stmt.o2.u1 == stmt.o1.u1 &&
3045                 OPTS_OPTIMIZATION(OPTIM_PEEPHOLE))
3046             {
3047                 ++opts_optimizationcount[OPTIM_PEEPHOLE];
3048                 continue;
3049             }
3050         }
3051
3052         code_push_statement(&stmt, instr->context.line);
3053     }
3054     return true;
3055 }
3056
3057 static bool gen_function_code(ir_function *self)
3058 {
3059     ir_block *block;
3060     prog_section_statement stmt, *retst;
3061
3062     /* Starting from entry point, we generate blocks "as they come"
3063      * for now. Dead blocks will not be translated obviously.
3064      */
3065     if (!vec_size(self->blocks)) {
3066         irerror(self->context, "Function '%s' declared without body.", self->name);
3067         return false;
3068     }
3069
3070     block = self->blocks[0];
3071     if (block->generated)
3072         return true;
3073
3074     if (!gen_blocks_recursive(self, block)) {
3075         irerror(self->context, "failed to generate blocks for '%s'", self->name);
3076         return false;
3077     }
3078
3079     /* code_write and qcvm -disasm need to know that the function ends here */
3080     retst = &vec_last(code_statements);
3081     if (OPTS_OPTIMIZATION(OPTIM_VOID_RETURN) &&
3082         self->outtype == TYPE_VOID &&
3083         retst->opcode == INSTR_RETURN &&
3084         !retst->o1.u1 && !retst->o2.u1 && !retst->o3.u1)
3085     {
3086         retst->opcode = INSTR_DONE;
3087         ++opts_optimizationcount[OPTIM_VOID_RETURN];
3088     } else {
3089         stmt.opcode = INSTR_DONE;
3090         stmt.o1.u1 = 0;
3091         stmt.o2.u1 = 0;
3092         stmt.o3.u1 = 0;
3093         code_push_statement(&stmt, vec_last(code_linenums));
3094     }
3095     return true;
3096 }
3097
3098 static qcint ir_builder_filestring(ir_builder *ir, const char *filename)
3099 {
3100     /* NOTE: filename pointers are copied, we never strdup them,
3101      * thus we can use pointer-comparison to find the string.
3102      */
3103     size_t i;
3104     qcint  str;
3105
3106     for (i = 0; i < vec_size(ir->filenames); ++i) {
3107         if (ir->filenames[i] == filename)
3108             return ir->filestrings[i];
3109     }
3110
3111     str = code_genstring(filename);
3112     vec_push(ir->filenames, filename);
3113     vec_push(ir->filestrings, str);
3114     return str;
3115 }
3116
3117 static bool gen_global_function(ir_builder *ir, ir_value *global)
3118 {
3119     prog_section_function fun;
3120     ir_function          *irfun;
3121
3122     size_t i;
3123
3124     if (!global->hasvalue || (!global->constval.vfunc))
3125     {
3126         irerror(global->context, "Invalid state of function-global: not constant: %s", global->name);
3127         return false;
3128     }
3129
3130     irfun = global->constval.vfunc;
3131
3132     fun.name    = global->code.name;
3133     fun.file    = ir_builder_filestring(ir, global->context.file);
3134     fun.profile = 0; /* always 0 */
3135     fun.nargs   = vec_size(irfun->params);
3136     if (fun.nargs > 8)
3137         fun.nargs = 8;
3138
3139     for (i = 0;i < 8; ++i) {
3140         if ((int32_t)i >= fun.nargs)
3141             fun.argsize[i] = 0;
3142         else
3143             fun.argsize[i] = type_sizeof_[irfun->params[i]];
3144     }
3145
3146     fun.firstlocal = 0;
3147     fun.locals     = irfun->allocated_locals;
3148
3149     if (irfun->builtin)
3150         fun.entry = irfun->builtin+1;
3151     else {
3152         irfun->code_function_def = vec_size(code_functions);
3153         fun.entry = vec_size(code_statements);
3154     }
3155
3156     vec_push(code_functions, fun);
3157     return true;
3158 }
3159
3160 static ir_value* ir_gen_extparam_proto(ir_builder *ir)
3161 {
3162     ir_value *global;
3163     char      name[128];
3164
3165     snprintf(name, sizeof(name), "EXTPARM#%i", (int)(vec_size(ir->extparam_protos)+8));
3166     global = ir_value_var(name, store_global, TYPE_VECTOR);
3167
3168     vec_push(ir->extparam_protos, global);
3169     return global;
3170 }
3171
3172 static void ir_gen_extparam(ir_builder *ir)
3173 {
3174     prog_section_def def;
3175     ir_value        *global;
3176
3177     if (vec_size(ir->extparam_protos) < vec_size(ir->extparams)+1)
3178         global = ir_gen_extparam_proto(ir);
3179     else
3180         global = ir->extparam_protos[vec_size(ir->extparams)];
3181
3182     def.name = code_genstring(global->name);
3183     def.type = TYPE_VECTOR;
3184     def.offset = vec_size(code_globals);
3185
3186     vec_push(code_defs, def);
3187     ir_value_code_setaddr(global, def.offset);
3188     vec_push(code_globals, 0);
3189     vec_push(code_globals, 0);
3190     vec_push(code_globals, 0);
3191
3192     vec_push(ir->extparams, global);
3193 }
3194
3195 static bool gen_function_extparam_copy(ir_function *self)
3196 {
3197     size_t i, ext, numparams;
3198
3199     ir_builder *ir = self->owner;
3200     ir_value   *ep;
3201     prog_section_statement stmt;
3202
3203     numparams = vec_size(self->params);
3204     if (!numparams)
3205         return true;
3206
3207     stmt.opcode = INSTR_STORE_F;
3208     stmt.o3.s1 = 0;
3209     for (i = 8; i < numparams; ++i) {
3210         ext = i - 8;
3211         if (ext >= vec_size(ir->extparams))
3212             ir_gen_extparam(ir);
3213
3214         ep = ir->extparams[ext];
3215
3216         stmt.opcode = type_store_instr[self->locals[i]->vtype];
3217         if (self->locals[i]->vtype == TYPE_FIELD &&
3218             self->locals[i]->fieldtype == TYPE_VECTOR)
3219         {
3220             stmt.opcode = INSTR_STORE_V;
3221         }
3222         stmt.o1.u1 = ir_value_code_addr(ep);
3223         stmt.o2.u1 = ir_value_code_addr(self->locals[i]);
3224         code_push_statement(&stmt, self->context.line);
3225     }
3226
3227     return true;
3228 }
3229
3230 static bool gen_function_locals(ir_builder *ir, ir_value *global)
3231 {
3232     prog_section_function *def;
3233     ir_function           *irfun;
3234     size_t                 i;
3235     uint32_t               firstlocal, firstglobal;
3236
3237     irfun = global->constval.vfunc;
3238     def   = code_functions + irfun->code_function_def;
3239
3240     if (opts.g || !OPTS_OPTIMIZATION(OPTIM_OVERLAP_LOCALS) || (irfun->flags & IR_FLAG_MASK_NO_OVERLAP))
3241         firstlocal = def->firstlocal = vec_size(code_globals);
3242     else {
3243         firstlocal = def->firstlocal = ir->first_common_local;
3244         ++opts_optimizationcount[OPTIM_OVERLAP_LOCALS];
3245     }
3246
3247     firstglobal = (OPTS_OPTIMIZATION(OPTIM_GLOBAL_TEMPS) ? ir->first_common_globaltemp : firstlocal);
3248
3249     for (i = vec_size(code_globals); i < firstlocal + irfun->allocated_locals; ++i)
3250         vec_push(code_globals, 0);
3251     for (i = 0; i < vec_size(irfun->locals); ++i) {
3252         ir_value *v = irfun->locals[i];
3253         if (v->locked || !OPTS_OPTIMIZATION(OPTIM_GLOBAL_TEMPS)) {
3254             ir_value_code_setaddr(v, firstlocal + v->code.local);
3255             if (!ir_builder_gen_global(ir, irfun->locals[i], true)) {
3256                 irerror(irfun->locals[i]->context, "failed to generate local %s", irfun->locals[i]->name);
3257                 return false;
3258             }
3259         }
3260         else
3261             ir_value_code_setaddr(v, firstglobal + v->code.local);
3262     }
3263     for (i = 0; i < vec_size(irfun->values); ++i)
3264     {
3265         ir_value *v = irfun->values[i];
3266         if (v->callparam)
3267             continue;
3268         if (v->locked)
3269             ir_value_code_setaddr(v, firstlocal + v->code.local);
3270         else
3271             ir_value_code_setaddr(v, firstglobal + v->code.local);
3272     }
3273     return true;
3274 }
3275
3276 static bool gen_global_function_code(ir_builder *ir, ir_value *global)
3277 {
3278     prog_section_function *fundef;
3279     ir_function           *irfun;
3280
3281     (void)ir;
3282
3283     irfun = global->constval.vfunc;
3284     if (!irfun) {
3285         if (global->cvq == CV_NONE) {
3286             irwarning(global->context, WARN_IMPLICIT_FUNCTION_POINTER,
3287                       "function `%s` has no body and in QC implicitly becomes a function-pointer", global->name);
3288         }
3289         /* this was a function pointer, don't generate code for those */
3290         return true;
3291     }
3292
3293     if (irfun->builtin)
3294         return true;
3295
3296     if (irfun->code_function_def < 0) {
3297         irerror(irfun->context, "`%s`: IR global wasn't generated, failed to access function-def", irfun->name);
3298         return false;
3299     }
3300     fundef = &code_functions[irfun->code_function_def];
3301
3302     fundef->entry = vec_size(code_statements);
3303     if (!gen_function_locals(ir, global)) {
3304         irerror(irfun->context, "Failed to generate locals for function %s", irfun->name);
3305         return false;
3306     }
3307     if (!gen_function_extparam_copy(irfun)) {
3308         irerror(irfun->context, "Failed to generate extparam-copy code for function %s", irfun->name);
3309         return false;
3310     }
3311     if (!gen_function_code(irfun)) {
3312         irerror(irfun->context, "Failed to generate code for function %s", irfun->name);
3313         return false;
3314     }
3315     return true;
3316 }
3317
3318 static void gen_vector_defs(prog_section_def def, const char *name)
3319 {
3320     char  *component;
3321     size_t len, i;
3322
3323     if (!name || name[0] == '#' || OPTS_FLAG(SINGLE_VECTOR_DEFS))
3324         return;
3325
3326     def.type = TYPE_FLOAT;
3327
3328     len = strlen(name);
3329
3330     component = (char*)mem_a(len+3);
3331     memcpy(component, name, len);
3332     len += 2;
3333     component[len-0] = 0;
3334     component[len-2] = '_';
3335
3336     component[len-1] = 'x';
3337
3338     for (i = 0; i < 3; ++i) {
3339         def.name = code_genstring(component);
3340         vec_push(code_defs, def);
3341         def.offset++;
3342         component[len-1]++;
3343     }
3344 }
3345
3346 static void gen_vector_fields(prog_section_field fld, const char *name)
3347 {
3348     char  *component;
3349     size_t len, i;
3350
3351     if (!name || OPTS_FLAG(SINGLE_VECTOR_DEFS))
3352         return;
3353
3354     fld.type = TYPE_FLOAT;
3355
3356     len = strlen(name);
3357
3358     component = (char*)mem_a(len+3);
3359     memcpy(component, name, len);
3360     len += 2;
3361     component[len-0] = 0;
3362     component[len-2] = '_';
3363
3364     component[len-1] = 'x';
3365
3366     for (i = 0; i < 3; ++i) {
3367         fld.name = code_genstring(component);
3368         vec_push(code_fields, fld);
3369         fld.offset++;
3370         component[len-1]++;
3371     }
3372 }
3373
3374 static bool ir_builder_gen_global(ir_builder *self, ir_value *global, bool islocal)
3375 {
3376     size_t           i;
3377     int32_t         *iptr;
3378     prog_section_def def;
3379     bool             pushdef = false;
3380
3381     def.type   = global->vtype;
3382     def.offset = vec_size(code_globals);
3383     def.name   = 0;
3384     if (opts.g || !islocal)
3385     {
3386         pushdef = true;
3387
3388         if (OPTS_OPTIMIZATION(OPTIM_STRIP_CONSTANT_NAMES) &&
3389             (global->name[0] == '#' || global->cvq == CV_CONST))
3390         {
3391             pushdef = false;
3392         }
3393
3394         if (pushdef && global->name) {
3395             if (global->name[0] == '#') {
3396                 if (!self->str_immediate)
3397                     self->str_immediate = code_genstring("IMMEDIATE");
3398                 def.name = global->code.name = self->str_immediate;
3399             }
3400             else
3401                 def.name = global->code.name = code_genstring(global->name);
3402         }
3403         else
3404             def.name   = 0;
3405         if (islocal) {
3406             def.offset = ir_value_code_addr(global);
3407             vec_push(code_defs, def);
3408             if (global->vtype == TYPE_VECTOR)
3409                 gen_vector_defs(def, global->name);
3410             else if (global->vtype == TYPE_FIELD && global->fieldtype == TYPE_VECTOR)
3411                 gen_vector_defs(def, global->name);
3412             return true;
3413         }
3414     }
3415     if (islocal)
3416         return true;
3417
3418     switch (global->vtype)
3419     {
3420     case TYPE_VOID:
3421         if (!strcmp(global->name, "end_sys_globals")) {
3422             /* TODO: remember this point... all the defs before this one
3423              * should be checksummed and added to progdefs.h when we generate it.
3424              */
3425         }
3426         else if (!strcmp(global->name, "end_sys_fields")) {
3427             /* TODO: same as above but for entity-fields rather than globsl
3428              */
3429         }
3430         else
3431             irwarning(global->context, WARN_VOID_VARIABLES, "unrecognized variable of type void `%s`",
3432                       global->name);
3433         /* I'd argue setting it to 0 is sufficient, but maybe some depend on knowing how far
3434          * the system fields actually go? Though the engine knows this anyway...
3435          * Maybe this could be an -foption
3436          * fteqcc creates data for end_sys_* - of size 1, so let's do the same
3437          */
3438         ir_value_code_setaddr(global, vec_size(code_globals));
3439         vec_push(code_globals, 0);
3440         /* Add the def */
3441         if (pushdef) vec_push(code_defs, def);
3442         return true;
3443     case TYPE_POINTER:
3444         if (pushdef) vec_push(code_defs, def);
3445         return gen_global_pointer(global);
3446     case TYPE_FIELD:
3447         if (pushdef) {
3448             vec_push(code_defs, def);
3449             if (global->fieldtype == TYPE_VECTOR)
3450                 gen_vector_defs(def, global->name);
3451         }
3452         return gen_global_field(global);
3453     case TYPE_ENTITY:
3454         /* fall through */
3455     case TYPE_FLOAT:
3456     {
3457         ir_value_code_setaddr(global, vec_size(code_globals));
3458         if (global->hasvalue) {
3459             iptr = (int32_t*)&global->constval.ivec[0];
3460             vec_push(code_globals, *iptr);
3461         } else {
3462             vec_push(code_globals, 0);
3463         }
3464         if (!islocal && global->cvq != CV_CONST)
3465             def.type |= DEF_SAVEGLOBAL;
3466         if (pushdef) vec_push(code_defs, def);
3467
3468         return global->code.globaladdr >= 0;
3469     }
3470     case TYPE_STRING:
3471     {
3472         ir_value_code_setaddr(global, vec_size(code_globals));
3473         if (global->hasvalue) {
3474             vec_push(code_globals, code_genstring(global->constval.vstring));
3475         } else {
3476             vec_push(code_globals, 0);
3477         }
3478         if (!islocal && global->cvq != CV_CONST)
3479             def.type |= DEF_SAVEGLOBAL;
3480         if (pushdef) vec_push(code_defs, def);
3481         return global->code.globaladdr >= 0;
3482     }
3483     case TYPE_VECTOR:
3484     {
3485         size_t d;
3486         ir_value_code_setaddr(global, vec_size(code_globals));
3487         if (global->hasvalue) {
3488             iptr = (int32_t*)&global->constval.ivec[0];
3489             vec_push(code_globals, iptr[0]);
3490             if (global->code.globaladdr < 0)
3491                 return false;
3492             for (d = 1; d < type_sizeof_[global->vtype]; ++d) {
3493                 vec_push(code_globals, iptr[d]);
3494             }
3495         } else {
3496             vec_push(code_globals, 0);
3497             if (global->code.globaladdr < 0)
3498                 return false;
3499             for (d = 1; d < type_sizeof_[global->vtype]; ++d) {
3500                 vec_push(code_globals, 0);
3501             }
3502         }
3503         if (!islocal && global->cvq != CV_CONST)
3504             def.type |= DEF_SAVEGLOBAL;
3505
3506         if (pushdef) {
3507             vec_push(code_defs, def);
3508             def.type &= ~DEF_SAVEGLOBAL;
3509             gen_vector_defs(def, global->name);
3510         }
3511         return global->code.globaladdr >= 0;
3512     }
3513     case TYPE_FUNCTION:
3514         ir_value_code_setaddr(global, vec_size(code_globals));
3515         if (!global->hasvalue) {
3516             vec_push(code_globals, 0);
3517             if (global->code.globaladdr < 0)
3518                 return false;
3519         } else {
3520             vec_push(code_globals, vec_size(code_functions));
3521             if (!gen_global_function(self, global))
3522                 return false;
3523         }
3524         if (!islocal && global->cvq != CV_CONST)
3525             def.type |= DEF_SAVEGLOBAL;
3526         if (pushdef) vec_push(code_defs, def);
3527         return true;
3528     case TYPE_VARIANT:
3529         /* assume biggest type */
3530             ir_value_code_setaddr(global, vec_size(code_globals));
3531             vec_push(code_globals, 0);
3532             for (i = 1; i < type_sizeof_[TYPE_VARIANT]; ++i)
3533                 vec_push(code_globals, 0);
3534             return true;
3535     default:
3536         /* refuse to create 'void' type or any other fancy business. */
3537         irerror(global->context, "Invalid type for global variable `%s`: %s",
3538                 global->name, type_name[global->vtype]);
3539         return false;
3540     }
3541 }
3542
3543 static void ir_builder_prepare_field(ir_value *field)
3544 {
3545     field->code.fieldaddr = code_alloc_field(type_sizeof_[field->fieldtype]);
3546 }
3547
3548 static bool ir_builder_gen_field(ir_builder *self, ir_value *field)
3549 {
3550     prog_section_def def;
3551     prog_section_field fld;
3552
3553     (void)self;
3554
3555     def.type   = (uint16_t)field->vtype;
3556     def.offset = (uint16_t)vec_size(code_globals);
3557
3558     /* create a global named the same as the field */
3559     if (opts.standard == COMPILER_GMQCC) {
3560         /* in our standard, the global gets a dot prefix */
3561         size_t len = strlen(field->name);
3562         char name[1024];
3563
3564         /* we really don't want to have to allocate this, and 1024
3565          * bytes is more than enough for a variable/field name
3566          */
3567         if (len+2 >= sizeof(name)) {
3568             irerror(field->context, "invalid field name size: %u", (unsigned int)len);
3569             return false;
3570         }
3571
3572         name[0] = '.';
3573         memcpy(name+1, field->name, len); /* no strncpy - we used strlen above */
3574         name[len+1] = 0;
3575
3576         def.name = code_genstring(name);
3577         fld.name = def.name + 1; /* we reuse that string table entry */
3578     } else {
3579         /* in plain QC, there cannot be a global with the same name,
3580          * and so we also name the global the same.
3581          * FIXME: fteqcc should create a global as well
3582          * check if it actually uses the same name. Probably does
3583          */
3584         def.name = code_genstring(field->name);
3585         fld.name = def.name;
3586     }
3587
3588     field->code.name = def.name;
3589
3590     vec_push(code_defs, def);
3591
3592     fld.type = field->fieldtype;
3593
3594     if (fld.type == TYPE_VOID) {
3595         irerror(field->context, "field is missing a type: %s - don't know its size", field->name);
3596         return false;
3597     }
3598
3599     fld.offset = field->code.fieldaddr;
3600
3601     vec_push(code_fields, fld);
3602
3603     ir_value_code_setaddr(field, vec_size(code_globals));
3604     vec_push(code_globals, fld.offset);
3605     if (fld.type == TYPE_VECTOR) {
3606         vec_push(code_globals, fld.offset+1);
3607         vec_push(code_globals, fld.offset+2);
3608     }
3609
3610     if (field->fieldtype == TYPE_VECTOR) {
3611         gen_vector_defs(def, field->name);
3612         gen_vector_fields(fld, field->name);
3613     }
3614
3615     return field->code.globaladdr >= 0;
3616 }
3617
3618 bool ir_builder_generate(ir_builder *self, const char *filename)
3619 {
3620     prog_section_statement stmt;
3621     size_t i;
3622     char  *lnofile = NULL;
3623
3624     code_init();
3625
3626     for (i = 0; i < vec_size(self->fields); ++i)
3627     {
3628         ir_builder_prepare_field(self->fields[i]);
3629     }
3630
3631     for (i = 0; i < vec_size(self->globals); ++i)
3632     {
3633         if (!ir_builder_gen_global(self, self->globals[i], false)) {
3634             return false;
3635         }
3636         if (self->globals[i]->vtype == TYPE_FUNCTION) {
3637             ir_function *func = self->globals[i]->constval.vfunc;
3638             if (func && self->max_locals < func->allocated_locals &&
3639                 !(func->flags & IR_FLAG_MASK_NO_OVERLAP))
3640             {
3641                 self->max_locals = func->allocated_locals;
3642             }
3643             if (func && self->max_globaltemps < func->globaltemps)
3644                 self->max_globaltemps = func->globaltemps;
3645         }
3646     }
3647
3648     for (i = 0; i < vec_size(self->fields); ++i)
3649     {
3650         if (!ir_builder_gen_field(self, self->fields[i])) {
3651             return false;
3652         }
3653     }
3654
3655     /* generate nil */
3656     ir_value_code_setaddr(self->nil, vec_size(code_globals));
3657     vec_push(code_globals, 0);
3658     vec_push(code_globals, 0);
3659     vec_push(code_globals, 0);
3660
3661     /* generate global temps */
3662     self->first_common_globaltemp = vec_size(code_globals);
3663     for (i = 0; i < self->max_globaltemps; ++i) {
3664         vec_push(code_globals, 0);
3665     }
3666     /* generate common locals */
3667     self->first_common_local = vec_size(code_globals);
3668     for (i = 0; i < self->max_locals; ++i) {
3669         vec_push(code_globals, 0);
3670     }
3671
3672     /* generate function code */
3673     for (i = 0; i < vec_size(self->globals); ++i)
3674     {
3675         if (self->globals[i]->vtype == TYPE_FUNCTION) {
3676             if (!gen_global_function_code(self, self->globals[i])) {
3677                 return false;
3678             }
3679         }
3680     }
3681
3682     if (vec_size(code_globals) >= 65536) {
3683         irerror(vec_last(self->globals)->context, "This progs file would require more globals than the metadata can handle. Bailing out.");
3684         return false;
3685     }
3686
3687     /* DP errors if the last instruction is not an INSTR_DONE. */
3688     if (vec_last(code_statements).opcode != INSTR_DONE)
3689     {
3690         stmt.opcode = INSTR_DONE;
3691         stmt.o1.u1 = 0;
3692         stmt.o2.u1 = 0;
3693         stmt.o3.u1 = 0;
3694         code_push_statement(&stmt, vec_last(code_linenums));
3695     }
3696
3697     if (opts.pp_only)
3698         return true;
3699
3700     if (vec_size(code_statements) != vec_size(code_linenums)) {
3701         con_err("Linecounter wrong: %lu != %lu\n",
3702                 (unsigned long)vec_size(code_statements),
3703                 (unsigned long)vec_size(code_linenums));
3704     } else if (OPTS_FLAG(LNO)) {
3705         char *dot;
3706         size_t filelen = strlen(filename);
3707
3708         memcpy(vec_add(lnofile, filelen+1), filename, filelen+1);
3709         dot = strrchr(lnofile, '.');
3710         if (!dot) {
3711             vec_pop(lnofile);
3712         } else {
3713             vec_shrinkto(lnofile, dot - lnofile);
3714         }
3715         memcpy(vec_add(lnofile, 5), ".lno", 5);
3716     }
3717
3718     if (!opts.quiet) {
3719         if (lnofile)
3720             con_out("writing '%s' and '%s'...\n", filename, lnofile);
3721         else
3722             con_out("writing '%s'\n", filename);
3723     }
3724     if (!code_write(filename, lnofile)) {
3725         vec_free(lnofile);
3726         return false;
3727     }
3728     vec_free(lnofile);
3729     return true;
3730 }
3731
3732 /***********************************************************************
3733  *IR DEBUG Dump functions...
3734  */
3735
3736 #define IND_BUFSZ 1024
3737
3738 #ifdef _MSC_VER
3739 #   define strncat(dst, src, sz) strncat_s(dst, sz, src, _TRUNCATE)
3740 #endif
3741
3742 const char *qc_opname(int op)
3743 {
3744     if (op < 0) return "<INVALID>";
3745     if (op < (int)( sizeof(asm_instr) / sizeof(asm_instr[0]) ))
3746         return asm_instr[op].m;
3747     switch (op) {
3748         case VINSTR_PHI:  return "PHI";
3749         case VINSTR_JUMP: return "JUMP";
3750         case VINSTR_COND: return "COND";
3751         default:          return "<UNK>";
3752     }
3753 }
3754
3755 void ir_builder_dump(ir_builder *b, int (*oprintf)(const char*, ...))
3756 {
3757     size_t i;
3758     char indent[IND_BUFSZ];
3759     indent[0] = '\t';
3760     indent[1] = 0;
3761
3762     oprintf("module %s\n", b->name);
3763     for (i = 0; i < vec_size(b->globals); ++i)
3764     {
3765         oprintf("global ");
3766         if (b->globals[i]->hasvalue)
3767             oprintf("%s = ", b->globals[i]->name);
3768         ir_value_dump(b->globals[i], oprintf);
3769         oprintf("\n");
3770     }
3771     for (i = 0; i < vec_size(b->functions); ++i)
3772         ir_function_dump(b->functions[i], indent, oprintf);
3773     oprintf("endmodule %s\n", b->name);
3774 }
3775
3776 void ir_function_dump(ir_function *f, char *ind,
3777                       int (*oprintf)(const char*, ...))
3778 {
3779     size_t i;
3780     if (f->builtin != 0) {
3781         oprintf("%sfunction %s = builtin %i\n", ind, f->name, -f->builtin);
3782         return;
3783     }
3784     oprintf("%sfunction %s\n", ind, f->name);
3785     strncat(ind, "\t", IND_BUFSZ);
3786     if (vec_size(f->locals))
3787     {
3788         oprintf("%s%i locals:\n", ind, (int)vec_size(f->locals));
3789         for (i = 0; i < vec_size(f->locals); ++i) {
3790             oprintf("%s\t", ind);
3791             ir_value_dump(f->locals[i], oprintf);
3792             oprintf("\n");
3793         }
3794     }
3795     oprintf("%sliferanges:\n", ind);
3796     for (i = 0; i < vec_size(f->locals); ++i) {
3797         const char *attr = "";
3798         size_t l, m;
3799         ir_value *v = f->locals[i];
3800         if (v->unique_life && v->locked)
3801             attr = "unique,locked ";
3802         else if (v->unique_life)
3803             attr = "unique ";
3804         else if (v->locked)
3805             attr = "locked ";
3806         oprintf("%s\t%s: %s %s%s@%i ", ind, v->name, type_name[v->vtype],
3807                 attr, (v->callparam ? "callparam " : ""),
3808                 (int)v->code.local);
3809         for (l = 0; l < vec_size(v->life); ++l) {
3810             oprintf("[%i,%i] ", v->life[l].start, v->life[l].end);
3811         }
3812         oprintf("\n");
3813         for (m = 0; m < 3; ++m) {
3814             ir_value *vm = v->members[m];
3815             if (!vm)
3816                 continue;
3817             if (vm->unique_life && vm->locked)
3818                 attr = "unique,locked ";
3819             else if (vm->unique_life)
3820                 attr = "unique ";
3821             else if (vm->locked)
3822                 attr = "locked ";
3823             oprintf("%s\t%s: %s@%i ", ind, vm->name, attr, (int)vm->code.local);
3824             for (l = 0; l < vec_size(vm->life); ++l) {
3825                 oprintf("[%i,%i] ", vm->life[l].start, vm->life[l].end);
3826             }
3827             oprintf("\n");
3828         }
3829     }
3830     for (i = 0; i < vec_size(f->values); ++i) {
3831         const char *attr = "";
3832         size_t l, m;
3833         ir_value *v = f->values[i];
3834         if (v->unique_life && v->locked)
3835             attr = "unique,locked ";
3836         else if (v->unique_life)
3837             attr = "unique ";
3838         else if (v->locked)
3839             attr = "locked ";
3840         oprintf("%s\t%s: %s %s%s@%i ", ind, v->name, type_name[v->vtype],
3841                 attr, (v->callparam ? "callparam " : ""),
3842                 (int)v->code.local);
3843         for (l = 0; l < vec_size(v->life); ++l) {
3844             oprintf("[%i,%i] ", v->life[l].start, v->life[l].end);
3845         }
3846         oprintf("\n");
3847         for (m = 0; m < 3; ++m) {
3848             ir_value *vm = v->members[m];
3849             if (!vm)
3850                 continue;
3851             if (vm->unique_life && vm->locked)
3852                 attr = "unique,locked ";
3853             else if (vm->unique_life)
3854                 attr = "unique ";
3855             else if (vm->locked)
3856                 attr = "locked ";
3857             oprintf("%s\t%s: %s@%i ", ind, vm->name, attr, (int)vm->code.local);
3858             for (l = 0; l < vec_size(vm->life); ++l) {
3859                 oprintf("[%i,%i] ", vm->life[l].start, vm->life[l].end);
3860             }
3861             oprintf("\n");
3862         }
3863     }
3864     if (vec_size(f->blocks))
3865     {
3866         oprintf("%slife passes (check): %i\n", ind, (int)f->run_id);
3867         for (i = 0; i < vec_size(f->blocks); ++i) {
3868             if (f->blocks[i]->run_id != f->run_id) {
3869                 oprintf("%slife pass check fail! %i != %i\n", ind, (int)f->blocks[i]->run_id, (int)f->run_id);
3870             }
3871             ir_block_dump(f->blocks[i], ind, oprintf);
3872         }
3873
3874     }
3875     ind[strlen(ind)-1] = 0;
3876     oprintf("%sendfunction %s\n", ind, f->name);
3877 }
3878
3879 void ir_block_dump(ir_block* b, char *ind,
3880                    int (*oprintf)(const char*, ...))
3881 {
3882     size_t i;
3883     oprintf("%s:%s\n", ind, b->label);
3884     strncat(ind, "\t", IND_BUFSZ);
3885
3886     for (i = 0; i < vec_size(b->instr); ++i)
3887         ir_instr_dump(b->instr[i], ind, oprintf);
3888     ind[strlen(ind)-1] = 0;
3889 }
3890
3891 void dump_phi(ir_instr *in, int (*oprintf)(const char*, ...))
3892 {
3893     size_t i;
3894     oprintf("%s <- phi ", in->_ops[0]->name);
3895     for (i = 0; i < vec_size(in->phi); ++i)
3896     {
3897         oprintf("([%s] : %s) ", in->phi[i].from->label,
3898                                 in->phi[i].value->name);
3899     }
3900     oprintf("\n");
3901 }
3902
3903 void ir_instr_dump(ir_instr *in, char *ind,
3904                        int (*oprintf)(const char*, ...))
3905 {
3906     size_t i;
3907     const char *comma = NULL;
3908
3909     oprintf("%s (%i) ", ind, (int)in->eid);
3910
3911     if (in->opcode == VINSTR_PHI) {
3912         dump_phi(in, oprintf);
3913         return;
3914     }
3915
3916     strncat(ind, "\t", IND_BUFSZ);
3917
3918     if (in->_ops[0] && (in->_ops[1] || in->_ops[2])) {
3919         ir_value_dump(in->_ops[0], oprintf);
3920         if (in->_ops[1] || in->_ops[2])
3921             oprintf(" <- ");
3922     }
3923     if (in->opcode == INSTR_CALL0 || in->opcode == VINSTR_NRCALL) {
3924         oprintf("CALL%i\t", vec_size(in->params));
3925     } else
3926         oprintf("%s\t", qc_opname(in->opcode));
3927
3928     if (in->_ops[0] && !(in->_ops[1] || in->_ops[2])) {
3929         ir_value_dump(in->_ops[0], oprintf);
3930         comma = ",\t";
3931     }
3932     else
3933     {
3934         for (i = 1; i != 3; ++i) {
3935             if (in->_ops[i]) {
3936                 if (comma)
3937                     oprintf(comma);
3938                 ir_value_dump(in->_ops[i], oprintf);
3939                 comma = ",\t";
3940             }
3941         }
3942     }
3943     if (in->bops[0]) {
3944         if (comma)
3945             oprintf(comma);
3946         oprintf("[%s]", in->bops[0]->label);
3947         comma = ",\t";
3948     }
3949     if (in->bops[1])
3950         oprintf("%s[%s]", comma, in->bops[1]->label);
3951     if (vec_size(in->params)) {
3952         oprintf("\tparams: ");
3953         for (i = 0; i != vec_size(in->params); ++i) {
3954             oprintf("%s, ", in->params[i]->name);
3955         }
3956     }
3957     oprintf("\n");
3958     ind[strlen(ind)-1] = 0;
3959 }
3960
3961 void ir_value_dump_string(const char *str, int (*oprintf)(const char*, ...))
3962 {
3963     oprintf("\"");
3964     for (; *str; ++str) {
3965         switch (*str) {
3966             case '\n': oprintf("\\n"); break;
3967             case '\r': oprintf("\\r"); break;
3968             case '\t': oprintf("\\t"); break;
3969             case '\v': oprintf("\\v"); break;
3970             case '\f': oprintf("\\f"); break;
3971             case '\b': oprintf("\\b"); break;
3972             case '\a': oprintf("\\a"); break;
3973             case '\\': oprintf("\\\\"); break;
3974             case '"': oprintf("\\\""); break;
3975             default: oprintf("%c", *str); break;
3976         }
3977     }
3978     oprintf("\"");
3979 }
3980
3981 void ir_value_dump(ir_value* v, int (*oprintf)(const char*, ...))
3982 {
3983     if (v->hasvalue) {
3984         switch (v->vtype) {
3985             default:
3986             case TYPE_VOID:
3987                 oprintf("(void)");
3988                 break;
3989             case TYPE_FUNCTION:
3990                 oprintf("fn:%s", v->name);
3991                 break;
3992             case TYPE_FLOAT:
3993                 oprintf("%g", v->constval.vfloat);
3994                 break;
3995             case TYPE_VECTOR:
3996                 oprintf("'%g %g %g'",
3997                         v->constval.vvec.x,
3998                         v->constval.vvec.y,
3999                         v->constval.vvec.z);
4000                 break;
4001             case TYPE_ENTITY:
4002                 oprintf("(entity)");
4003                 break;
4004             case TYPE_STRING:
4005                 ir_value_dump_string(v->constval.vstring, oprintf);
4006                 break;
4007 #if 0
4008             case TYPE_INTEGER:
4009                 oprintf("%i", v->constval.vint);
4010                 break;
4011 #endif
4012             case TYPE_POINTER:
4013                 oprintf("&%s",
4014                     v->constval.vpointer->name);
4015                 break;
4016         }
4017     } else {
4018         oprintf("%s", v->name);
4019     }
4020 }
4021
4022 void ir_value_dump_life(const ir_value *self, int (*oprintf)(const char*,...))
4023 {
4024     size_t i;
4025     oprintf("Life of %12s:", self->name);
4026     for (i = 0; i < vec_size(self->life); ++i)
4027     {
4028         oprintf(" + [%i, %i]\n", self->life[i].start, self->life[i].end);
4029     }
4030 }