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