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