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