]> de.git.xonotic.org Git - xonotic/gmqcc.git/blob - ir.c
Fix another Blub bug .. learn to use comments properly :P
[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];
2485     dbg_ind[0] = '#';
2486     dbg_ind[1] = '0';
2487     (void)dbg_ind;
2488
2489     if (prev)
2490     {
2491         if (!ir_block_life_prop_previous(self, prev, changed))
2492             return false;
2493     }
2494
2495     i = vec_size(self->instr);
2496     while (i)
2497     { --i;
2498         instr = self->instr[i];
2499
2500         /* See which operands are read and write operands */
2501         ir_op_read_write(instr->opcode, &read, &write);
2502
2503         if (instr->opcode == INSTR_MUL_VF)
2504         {
2505             /* the float source will get an additional lifetime */
2506             tempbool = ir_value_life_merge(instr->_ops[2], instr->eid+1);
2507             *changed = *changed || tempbool;
2508         }
2509         else if (instr->opcode == INSTR_MUL_FV)
2510         {
2511             /* the float source will get an additional lifetime */
2512             tempbool = ir_value_life_merge(instr->_ops[1], instr->eid+1);
2513             *changed = *changed || tempbool;
2514         }
2515
2516         /* Go through the 3 main operands
2517          * writes first, then reads
2518          */
2519         for (o = 0; o < 3; ++o)
2520         {
2521             if (!instr->_ops[o]) /* no such operand */
2522                 continue;
2523
2524             value = instr->_ops[o];
2525
2526             /* We only care about locals */
2527             /* we also calculate parameter liferanges so that locals
2528              * can take up parameter slots */
2529             if (value->store != store_value &&
2530                 value->store != store_local &&
2531                 value->store != store_param)
2532                 continue;
2533
2534             /* write operands */
2535             /* When we write to a local, we consider it "dead" for the
2536              * remaining upper part of the function, since in SSA a value
2537              * can only be written once (== created)
2538              */
2539             if (write & (1<<o))
2540             {
2541                 size_t idx;
2542                 bool in_living = vec_ir_value_find(self->living, value, &idx);
2543                 if (!in_living)
2544                 {
2545                     /* If the value isn't alive it hasn't been read before... */
2546                     /* TODO: See if the warning can be emitted during parsing or AST processing
2547                      * otherwise have warning printed here.
2548                      * IF printing a warning here: include filecontext_t,
2549                      * and make sure it's only printed once
2550                      * since this function is run multiple times.
2551                      */
2552                     /* con_err( "Value only written %s\n", value->name); */
2553                     tempbool = ir_value_life_merge(value, instr->eid);
2554                     *changed = *changed || tempbool;
2555                 } else {
2556                     /* since 'living' won't contain it
2557                      * anymore, merge the value, since
2558                      * (A) doesn't.
2559                      */
2560                     tempbool = ir_value_life_merge(value, instr->eid);
2561                     *changed = *changed || tempbool;
2562                     /* Then remove */
2563                     vec_remove(self->living, idx, 1);
2564                 }
2565                 /* Removing a vector removes all members */
2566                 for (mem = 0; mem < 3; ++mem) {
2567                     if (value->members[mem] && vec_ir_value_find(self->living, value->members[mem], &idx)) {
2568                         tempbool = ir_value_life_merge(value->members[mem], instr->eid);
2569                         *changed = *changed || tempbool;
2570                         vec_remove(self->living, idx, 1);
2571                     }
2572                 }
2573                 /* Removing the last member removes the vector */
2574                 if (value->memberof) {
2575                     value = value->memberof;
2576                     for (mem = 0; mem < 3; ++mem) {
2577                         if (value->members[mem] && vec_ir_value_find(self->living, value->members[mem], NULL))
2578                             break;
2579                     }
2580                     if (mem == 3 && vec_ir_value_find(self->living, value, &idx)) {
2581                         tempbool = ir_value_life_merge(value, instr->eid);
2582                         *changed = *changed || tempbool;
2583                         vec_remove(self->living, idx, 1);
2584                     }
2585                 }
2586             }
2587         }
2588
2589         for (o = 0; o < 3; ++o)
2590         {
2591             if (!instr->_ops[o]) /* no such operand */
2592                 continue;
2593
2594             value = instr->_ops[o];
2595
2596             /* We only care about locals */
2597             /* we also calculate parameter liferanges so that locals
2598              * can take up parameter slots */
2599             if (value->store != store_value &&
2600                 value->store != store_local &&
2601                 value->store != store_param)
2602                 continue;
2603
2604             /* read operands */
2605             if (read & (1<<o))
2606             {
2607                 if (!vec_ir_value_find(self->living, value, NULL))
2608                     vec_push(self->living, value);
2609                 /* reading adds the full vector */
2610                 if (value->memberof && !vec_ir_value_find(self->living, value->memberof, NULL))
2611                     vec_push(self->living, value->memberof);
2612                 for (mem = 0; mem < 3; ++mem) {
2613                     if (value->members[mem] && !vec_ir_value_find(self->living, value->members[mem], NULL))
2614                         vec_push(self->living, value->members[mem]);
2615                 }
2616             }
2617         }
2618         /* PHI operands are always read operands */
2619         for (p = 0; p < vec_size(instr->phi); ++p)
2620         {
2621             value = instr->phi[p].value;
2622             if (!vec_ir_value_find(self->living, value, NULL))
2623                 vec_push(self->living, value);
2624             /* reading adds the full vector */
2625             if (value->memberof && !vec_ir_value_find(self->living, value->memberof, NULL))
2626                 vec_push(self->living, value->memberof);
2627             for (mem = 0; mem < 3; ++mem) {
2628                 if (value->members[mem] && !vec_ir_value_find(self->living, value->members[mem], NULL))
2629                     vec_push(self->living, value->members[mem]);
2630             }
2631         }
2632
2633         /* on a call, all these values must be "locked" */
2634         if (instr->opcode >= INSTR_CALL0 && instr->opcode <= INSTR_CALL8) {
2635             if (ir_block_living_lock(self))
2636                 *changed = true;
2637         }
2638         /* call params are read operands too */
2639         for (p = 0; p < vec_size(instr->params); ++p)
2640         {
2641             value = instr->params[p];
2642             if (!vec_ir_value_find(self->living, value, NULL))
2643                 vec_push(self->living, value);
2644             /* reading adds the full vector */
2645             if (value->memberof && !vec_ir_value_find(self->living, value->memberof, NULL))
2646                 vec_push(self->living, value->memberof);
2647             for (mem = 0; mem < 3; ++mem) {
2648                 if (value->members[mem] && !vec_ir_value_find(self->living, value->members[mem], NULL))
2649                     vec_push(self->living, value->members[mem]);
2650             }
2651         }
2652
2653         /* (A) */
2654         tempbool = ir_block_living_add_instr(self, instr->eid);
2655         /*con_err( "living added values\n");*/
2656         *changed = *changed || tempbool;
2657
2658     }
2659
2660     if (self->run_id == self->owner->run_id)
2661         return true;
2662
2663     self->run_id = self->owner->run_id;
2664
2665     for (i = 0; i < vec_size(self->entries); ++i)
2666     {
2667         ir_block *entry = self->entries[i];
2668         ir_block_life_propagate(entry, self, changed);
2669     }
2670
2671     return true;
2672 }
2673
2674 /***********************************************************************
2675  *IR Code-Generation
2676  *
2677  * Since the IR has the convention of putting 'write' operands
2678  * at the beginning, we have to rotate the operands of instructions
2679  * properly in order to generate valid QCVM code.
2680  *
2681  * Having destinations at a fixed position is more convenient. In QC
2682  * this is *mostly* OPC,  but FTE adds at least 2 instructions which
2683  * read from from OPA,  and store to OPB rather than OPC.   Which is
2684  * partially the reason why the implementation of these instructions
2685  * in darkplaces has been delayed for so long.
2686  *
2687  * Breaking conventions is annoying...
2688  */
2689 static bool ir_builder_gen_global(ir_builder *self, ir_value *global, bool islocal);
2690
2691 static bool gen_global_field(ir_value *global)
2692 {
2693     if (global->hasvalue)
2694     {
2695         ir_value *fld = global->constval.vpointer;
2696         if (!fld) {
2697             irerror(global->context, "Invalid field constant with no field: %s", global->name);
2698             return false;
2699         }
2700
2701         /* copy the field's value */
2702         ir_value_code_setaddr(global, vec_size(code_globals));
2703         vec_push(code_globals, fld->code.fieldaddr);
2704         if (global->fieldtype == TYPE_VECTOR) {
2705             vec_push(code_globals, fld->code.fieldaddr+1);
2706             vec_push(code_globals, fld->code.fieldaddr+2);
2707         }
2708     }
2709     else
2710     {
2711         ir_value_code_setaddr(global, vec_size(code_globals));
2712         vec_push(code_globals, 0);
2713         if (global->fieldtype == TYPE_VECTOR) {
2714             vec_push(code_globals, 0);
2715             vec_push(code_globals, 0);
2716         }
2717     }
2718     if (global->code.globaladdr < 0)
2719         return false;
2720     return true;
2721 }
2722
2723 static bool gen_global_pointer(ir_value *global)
2724 {
2725     if (global->hasvalue)
2726     {
2727         ir_value *target = global->constval.vpointer;
2728         if (!target) {
2729             irerror(global->context, "Invalid pointer constant: %s", global->name);
2730             /* NULL pointers are pointing to the NULL constant, which also
2731              * sits at address 0, but still has an ir_value for itself.
2732              */
2733             return false;
2734         }
2735
2736         /* Here, relocations ARE possible - in fteqcc-enhanced-qc:
2737          * void() foo; <- proto
2738          * void() *fooptr = &foo;
2739          * void() foo = { code }
2740          */
2741         if (!target->code.globaladdr) {
2742             /* FIXME: Check for the constant nullptr ir_value!
2743              * because then code.globaladdr being 0 is valid.
2744              */
2745             irerror(global->context, "FIXME: Relocation support");
2746             return false;
2747         }
2748
2749         ir_value_code_setaddr(global, vec_size(code_globals));
2750         vec_push(code_globals, target->code.globaladdr);
2751     }
2752     else
2753     {
2754         ir_value_code_setaddr(global, vec_size(code_globals));
2755         vec_push(code_globals, 0);
2756     }
2757     if (global->code.globaladdr < 0)
2758         return false;
2759     return true;
2760 }
2761
2762 static bool gen_blocks_recursive(ir_function *func, ir_block *block)
2763 {
2764     prog_section_statement stmt;
2765     ir_instr *instr;
2766     ir_block *target;
2767     ir_block *ontrue;
2768     ir_block *onfalse;
2769     size_t    stidx;
2770     size_t    i;
2771
2772 tailcall:
2773     block->generated = true;
2774     block->code_start = vec_size(code_statements);
2775     for (i = 0; i < vec_size(block->instr); ++i)
2776     {
2777         instr = block->instr[i];
2778
2779         if (instr->opcode == VINSTR_PHI) {
2780             irerror(block->context, "cannot generate virtual instruction (phi)");
2781             return false;
2782         }
2783
2784         if (instr->opcode == VINSTR_JUMP) {
2785             target = instr->bops[0];
2786             /* for uncoditional jumps, if the target hasn't been generated
2787              * yet, we generate them right here.
2788              */
2789             if (!target->generated) {
2790                 block = target;
2791                 goto tailcall;
2792             }
2793
2794             /* otherwise we generate a jump instruction */
2795             stmt.opcode = INSTR_GOTO;
2796             stmt.o1.s1 = (target->code_start) - vec_size(code_statements);
2797             stmt.o2.s1 = 0;
2798             stmt.o3.s1 = 0;
2799             if (stmt.o1.s1 != 1)
2800                 code_push_statement(&stmt, instr->context.line);
2801
2802             /* no further instructions can be in this block */
2803             return true;
2804         }
2805
2806         if (instr->opcode == VINSTR_COND) {
2807             ontrue  = instr->bops[0];
2808             onfalse = instr->bops[1];
2809             /* TODO: have the AST signal which block should
2810              * come first: eg. optimize IFs without ELSE...
2811              */
2812
2813             stmt.o1.u1 = ir_value_code_addr(instr->_ops[0]);
2814             stmt.o2.u1 = 0;
2815             stmt.o3.s1 = 0;
2816
2817             if (ontrue->generated) {
2818                 stmt.opcode = INSTR_IF;
2819                 stmt.o2.s1 = (ontrue->code_start) - vec_size(code_statements);
2820                 if (stmt.o2.s1 != 1)
2821                     code_push_statement(&stmt, instr->context.line);
2822             }
2823             if (onfalse->generated) {
2824                 stmt.opcode = INSTR_IFNOT;
2825                 stmt.o2.s1 = (onfalse->code_start) - vec_size(code_statements);
2826                 if (stmt.o2.s1 != 1)
2827                     code_push_statement(&stmt, instr->context.line);
2828             }
2829             if (!ontrue->generated) {
2830                 if (onfalse->generated) {
2831                     block = ontrue;
2832                     goto tailcall;
2833                 }
2834             }
2835             if (!onfalse->generated) {
2836                 if (ontrue->generated) {
2837                     block = onfalse;
2838                     goto tailcall;
2839                 }
2840             }
2841             /* neither ontrue nor onfalse exist */
2842             stmt.opcode = INSTR_IFNOT;
2843             if (!instr->likely) {
2844                 /* Honor the likelyhood hint */
2845                 ir_block *tmp = onfalse;
2846                 stmt.opcode = INSTR_IF;
2847                 onfalse = ontrue;
2848                 ontrue = tmp;
2849             }
2850             stidx = vec_size(code_statements);
2851             code_push_statement(&stmt, instr->context.line);
2852             /* on false we jump, so add ontrue-path */
2853             if (!gen_blocks_recursive(func, ontrue))
2854                 return false;
2855             /* fixup the jump address */
2856             code_statements[stidx].o2.s1 = vec_size(code_statements) - stidx;
2857             /* generate onfalse path */
2858             if (onfalse->generated) {
2859                 /* fixup the jump address */
2860                 code_statements[stidx].o2.s1 = (onfalse->code_start) - (stidx);
2861                 if (code_statements[stidx].o2.s1 == 1) {
2862                     code_statements[stidx] = code_statements[stidx+1];
2863                     if (code_statements[stidx].o1.s1 < 0)
2864                         code_statements[stidx].o1.s1++;
2865                     code_pop_statement();
2866                 }
2867                 stmt.opcode = vec_last(code_statements).opcode;
2868                 if (stmt.opcode == INSTR_GOTO ||
2869                     stmt.opcode == INSTR_IF ||
2870                     stmt.opcode == INSTR_IFNOT ||
2871                     stmt.opcode == INSTR_RETURN ||
2872                     stmt.opcode == INSTR_DONE)
2873                 {
2874                     /* no use jumping from here */
2875                     return true;
2876                 }
2877                 /* may have been generated in the previous recursive call */
2878                 stmt.opcode = INSTR_GOTO;
2879                 stmt.o1.s1 = (onfalse->code_start) - vec_size(code_statements);
2880                 stmt.o2.s1 = 0;
2881                 stmt.o3.s1 = 0;
2882                 if (stmt.o1.s1 != 1)
2883                     code_push_statement(&stmt, instr->context.line);
2884                 return true;
2885             }
2886             else if (code_statements[stidx].o2.s1 == 1) {
2887                 code_statements[stidx] = code_statements[stidx+1];
2888                 if (code_statements[stidx].o1.s1 < 0)
2889                     code_statements[stidx].o1.s1++;
2890                 code_pop_statement();
2891             }
2892             /* if not, generate now */
2893             block = onfalse;
2894             goto tailcall;
2895         }
2896
2897         if ( (instr->opcode >= INSTR_CALL0 && instr->opcode <= INSTR_CALL8)
2898            || instr->opcode == VINSTR_NRCALL)
2899         {
2900             size_t p, first;
2901             ir_value *retvalue;
2902
2903             first = vec_size(instr->params);
2904             if (first > 8)
2905                 first = 8;
2906             for (p = 0; p < first; ++p)
2907             {
2908                 ir_value *param = instr->params[p];
2909                 if (param->callparam)
2910                     continue;
2911
2912                 stmt.opcode = INSTR_STORE_F;
2913                 stmt.o3.u1 = 0;
2914
2915                 if (param->vtype == TYPE_FIELD)
2916                     stmt.opcode = field_store_instr[param->fieldtype];
2917                 else
2918                     stmt.opcode = type_store_instr[param->vtype];
2919                 stmt.o1.u1 = ir_value_code_addr(param);
2920                 stmt.o2.u1 = OFS_PARM0 + 3 * p;
2921                 code_push_statement(&stmt, instr->context.line);
2922             }
2923             /* Now handle extparams */
2924             first = vec_size(instr->params);
2925             for (; p < first; ++p)
2926             {
2927                 ir_builder *ir = func->owner;
2928                 ir_value *param = instr->params[p];
2929                 ir_value *targetparam;
2930
2931                 if (param->callparam)
2932                     continue;
2933
2934                 if (p-8 >= vec_size(ir->extparams))
2935                     ir_gen_extparam(ir);
2936
2937                 targetparam = ir->extparams[p-8];
2938
2939                 stmt.opcode = INSTR_STORE_F;
2940                 stmt.o3.u1 = 0;
2941
2942                 if (param->vtype == TYPE_FIELD)
2943                     stmt.opcode = field_store_instr[param->fieldtype];
2944                 else
2945                     stmt.opcode = type_store_instr[param->vtype];
2946                 stmt.o1.u1 = ir_value_code_addr(param);
2947                 stmt.o2.u1 = ir_value_code_addr(targetparam);
2948                 code_push_statement(&stmt, instr->context.line);
2949             }
2950
2951             stmt.opcode = INSTR_CALL0 + vec_size(instr->params);
2952             if (stmt.opcode > INSTR_CALL8)
2953                 stmt.opcode = INSTR_CALL8;
2954             stmt.o1.u1 = ir_value_code_addr(instr->_ops[1]);
2955             stmt.o2.u1 = 0;
2956             stmt.o3.u1 = 0;
2957             code_push_statement(&stmt, instr->context.line);
2958
2959             retvalue = instr->_ops[0];
2960             if (retvalue && retvalue->store != store_return &&
2961                 (retvalue->store == store_global || vec_size(retvalue->life)))
2962             {
2963                 /* not to be kept in OFS_RETURN */
2964                 if (retvalue->vtype == TYPE_FIELD && OPTS_FLAG(ADJUST_VECTOR_FIELDS))
2965                     stmt.opcode = field_store_instr[retvalue->fieldtype];
2966                 else
2967                     stmt.opcode = type_store_instr[retvalue->vtype];
2968                 stmt.o1.u1 = OFS_RETURN;
2969                 stmt.o2.u1 = ir_value_code_addr(retvalue);
2970                 stmt.o3.u1 = 0;
2971                 code_push_statement(&stmt, instr->context.line);
2972             }
2973             continue;
2974         }
2975
2976         if (instr->opcode == INSTR_STATE) {
2977             irerror(block->context, "TODO: state instruction");
2978             return false;
2979         }
2980
2981         stmt.opcode = instr->opcode;
2982         stmt.o1.u1 = 0;
2983         stmt.o2.u1 = 0;
2984         stmt.o3.u1 = 0;
2985
2986         /* This is the general order of operands */
2987         if (instr->_ops[0])
2988             stmt.o3.u1 = ir_value_code_addr(instr->_ops[0]);
2989
2990         if (instr->_ops[1])
2991             stmt.o1.u1 = ir_value_code_addr(instr->_ops[1]);
2992
2993         if (instr->_ops[2])
2994             stmt.o2.u1 = ir_value_code_addr(instr->_ops[2]);
2995
2996         if (stmt.opcode == INSTR_RETURN || stmt.opcode == INSTR_DONE)
2997         {
2998             stmt.o1.u1 = stmt.o3.u1;
2999             stmt.o3.u1 = 0;
3000         }
3001         else if ((stmt.opcode >= INSTR_STORE_F &&
3002                   stmt.opcode <= INSTR_STORE_FNC) ||
3003                  (stmt.opcode >= INSTR_STOREP_F &&
3004                   stmt.opcode <= INSTR_STOREP_FNC))
3005         {
3006             /* 2-operand instructions with A -> B */
3007             stmt.o2.u1 = stmt.o3.u1;
3008             stmt.o3.u1 = 0;
3009
3010             /* tiny optimization, don't output
3011              * STORE a, a
3012              */
3013             if (stmt.o2.u1 == stmt.o1.u1 &&
3014                 OPTS_OPTIMIZATION(OPTIM_PEEPHOLE))
3015             {
3016                 ++opts_optimizationcount[OPTIM_PEEPHOLE];
3017                 continue;
3018             }
3019         }
3020
3021         code_push_statement(&stmt, instr->context.line);
3022     }
3023     return true;
3024 }
3025
3026 static bool gen_function_code(ir_function *self)
3027 {
3028     ir_block *block;
3029     prog_section_statement stmt, *retst;
3030
3031     /* Starting from entry point, we generate blocks "as they come"
3032      * for now. Dead blocks will not be translated obviously.
3033      */
3034     if (!vec_size(self->blocks)) {
3035         irerror(self->context, "Function '%s' declared without body.", self->name);
3036         return false;
3037     }
3038
3039     block = self->blocks[0];
3040     if (block->generated)
3041         return true;
3042
3043     if (!gen_blocks_recursive(self, block)) {
3044         irerror(self->context, "failed to generate blocks for '%s'", self->name);
3045         return false;
3046     }
3047
3048     /* code_write and qcvm -disasm need to know that the function ends here */
3049     retst = &vec_last(code_statements);
3050     if (OPTS_OPTIMIZATION(OPTIM_VOID_RETURN) &&
3051         self->outtype == TYPE_VOID &&
3052         retst->opcode == INSTR_RETURN &&
3053         !retst->o1.u1 && !retst->o2.u1 && !retst->o3.u1)
3054     {
3055         retst->opcode = INSTR_DONE;
3056         ++opts_optimizationcount[OPTIM_VOID_RETURN];
3057     } else {
3058         stmt.opcode = INSTR_DONE;
3059         stmt.o1.u1 = 0;
3060         stmt.o2.u1 = 0;
3061         stmt.o3.u1 = 0;
3062         code_push_statement(&stmt, vec_last(code_linenums));
3063     }
3064     return true;
3065 }
3066
3067 static qcint ir_builder_filestring(ir_builder *ir, const char *filename)
3068 {
3069     /* NOTE: filename pointers are copied, we never strdup them,
3070      * thus we can use pointer-comparison to find the string.
3071      */
3072     size_t i;
3073     qcint  str;
3074
3075     for (i = 0; i < vec_size(ir->filenames); ++i) {
3076         if (ir->filenames[i] == filename)
3077             return ir->filestrings[i];
3078     }
3079
3080     str = code_genstring(filename);
3081     vec_push(ir->filenames, filename);
3082     vec_push(ir->filestrings, str);
3083     return str;
3084 }
3085
3086 static bool gen_global_function(ir_builder *ir, ir_value *global)
3087 {
3088     prog_section_function fun;
3089     ir_function          *irfun;
3090
3091     size_t i;
3092
3093     if (!global->hasvalue || (!global->constval.vfunc))
3094     {
3095         irerror(global->context, "Invalid state of function-global: not constant: %s", global->name);
3096         return false;
3097     }
3098
3099     irfun = global->constval.vfunc;
3100
3101     fun.name    = global->code.name;
3102     fun.file    = ir_builder_filestring(ir, global->context.file);
3103     fun.profile = 0; /* always 0 */
3104     fun.nargs   = vec_size(irfun->params);
3105     if (fun.nargs > 8)
3106         fun.nargs = 8;
3107
3108     for (i = 0;i < 8; ++i) {
3109         if ((int32_t)i >= fun.nargs)
3110             fun.argsize[i] = 0;
3111         else
3112             fun.argsize[i] = type_sizeof_[irfun->params[i]];
3113     }
3114
3115     fun.firstlocal = 0;
3116     fun.locals     = irfun->allocated_locals;
3117
3118     if (irfun->builtin)
3119         fun.entry = irfun->builtin+1;
3120     else {
3121         irfun->code_function_def = vec_size(code_functions);
3122         fun.entry = vec_size(code_statements);
3123     }
3124
3125     vec_push(code_functions, fun);
3126     return true;
3127 }
3128
3129 static ir_value* ir_gen_extparam_proto(ir_builder *ir)
3130 {
3131     ir_value *global;
3132     char      name[128];
3133
3134     snprintf(name, sizeof(name), "EXTPARM#%i", (int)(vec_size(ir->extparam_protos)+8));
3135     global = ir_value_var(name, store_global, TYPE_VECTOR);
3136
3137     vec_push(ir->extparam_protos, global);
3138     return global;
3139 }
3140
3141 static void ir_gen_extparam(ir_builder *ir)
3142 {
3143     prog_section_def def;
3144     ir_value        *global;
3145
3146     if (vec_size(ir->extparam_protos) < vec_size(ir->extparams)+1)
3147         global = ir_gen_extparam_proto(ir);
3148     else
3149         global = ir->extparam_protos[vec_size(ir->extparams)];
3150
3151     def.name = code_genstring(global->name);
3152     def.type = TYPE_VECTOR;
3153     def.offset = vec_size(code_globals);
3154
3155     vec_push(code_defs, def);
3156     ir_value_code_setaddr(global, def.offset);
3157     vec_push(code_globals, 0);
3158     vec_push(code_globals, 0);
3159     vec_push(code_globals, 0);
3160
3161     vec_push(ir->extparams, global);
3162 }
3163
3164 static bool gen_function_extparam_copy(ir_function *self)
3165 {
3166     size_t i, ext, numparams;
3167
3168     ir_builder *ir = self->owner;
3169     ir_value   *ep;
3170     prog_section_statement stmt;
3171
3172     numparams = vec_size(self->params);
3173     if (!numparams)
3174         return true;
3175
3176     stmt.opcode = INSTR_STORE_F;
3177     stmt.o3.s1 = 0;
3178     for (i = 8; i < numparams; ++i) {
3179         ext = i - 8;
3180         if (ext >= vec_size(ir->extparams))
3181             ir_gen_extparam(ir);
3182
3183         ep = ir->extparams[ext];
3184
3185         stmt.opcode = type_store_instr[self->locals[i]->vtype];
3186         if (self->locals[i]->vtype == TYPE_FIELD &&
3187             self->locals[i]->fieldtype == TYPE_VECTOR)
3188         {
3189             stmt.opcode = INSTR_STORE_V;
3190         }
3191         stmt.o1.u1 = ir_value_code_addr(ep);
3192         stmt.o2.u1 = ir_value_code_addr(self->locals[i]);
3193         code_push_statement(&stmt, self->context.line);
3194     }
3195
3196     return true;
3197 }
3198
3199 static bool gen_function_locals(ir_builder *ir, ir_value *global)
3200 {
3201     prog_section_function *def;
3202     ir_function           *irfun;
3203     size_t                 i;
3204     uint32_t               firstlocal, firstglobal;
3205
3206     irfun = global->constval.vfunc;
3207     def   = code_functions + irfun->code_function_def;
3208
3209     if (opts.g || !OPTS_OPTIMIZATION(OPTIM_OVERLAP_LOCALS) || (irfun->flags & IR_FLAG_MASK_NO_OVERLAP))
3210         firstlocal = def->firstlocal = vec_size(code_globals);
3211     else {
3212         firstlocal = def->firstlocal = ir->first_common_local;
3213         ++opts_optimizationcount[OPTIM_OVERLAP_LOCALS];
3214     }
3215
3216     firstglobal = (OPTS_OPTIMIZATION(OPTIM_GLOBAL_TEMPS) ? ir->first_common_globaltemp : firstlocal);
3217
3218     for (i = vec_size(code_globals); i < firstlocal + irfun->allocated_locals; ++i)
3219         vec_push(code_globals, 0);
3220     for (i = 0; i < vec_size(irfun->locals); ++i) {
3221         ir_value *v = irfun->locals[i];
3222         if (v->locked || !OPTS_OPTIMIZATION(OPTIM_GLOBAL_TEMPS)) {
3223             ir_value_code_setaddr(v, firstlocal + v->code.local);
3224             if (!ir_builder_gen_global(ir, irfun->locals[i], true)) {
3225                 irerror(irfun->locals[i]->context, "failed to generate local %s", irfun->locals[i]->name);
3226                 return false;
3227             }
3228         }
3229         else
3230             ir_value_code_setaddr(v, firstglobal + v->code.local);
3231     }
3232     for (i = 0; i < vec_size(irfun->values); ++i)
3233     {
3234         ir_value *v = irfun->values[i];
3235         if (v->callparam)
3236             continue;
3237         if (v->locked)
3238             ir_value_code_setaddr(v, firstlocal + v->code.local);
3239         else
3240             ir_value_code_setaddr(v, firstglobal + v->code.local);
3241     }
3242     return true;
3243 }
3244
3245 static bool gen_global_function_code(ir_builder *ir, ir_value *global)
3246 {
3247     prog_section_function *fundef;
3248     ir_function           *irfun;
3249
3250     (void)ir;
3251
3252     irfun = global->constval.vfunc;
3253     if (!irfun) {
3254         if (global->cvq == CV_NONE) {
3255             irwarning(global->context, WARN_IMPLICIT_FUNCTION_POINTER,
3256                       "function `%s` has no body and in QC implicitly becomes a function-pointer", global->name);
3257         }
3258         /* this was a function pointer, don't generate code for those */
3259         return true;
3260     }
3261
3262     if (irfun->builtin)
3263         return true;
3264
3265     if (irfun->code_function_def < 0) {
3266         irerror(irfun->context, "`%s`: IR global wasn't generated, failed to access function-def", irfun->name);
3267         return false;
3268     }
3269     fundef = &code_functions[irfun->code_function_def];
3270
3271     fundef->entry = vec_size(code_statements);
3272     if (!gen_function_locals(ir, global)) {
3273         irerror(irfun->context, "Failed to generate locals for function %s", irfun->name);
3274         return false;
3275     }
3276     if (!gen_function_extparam_copy(irfun)) {
3277         irerror(irfun->context, "Failed to generate extparam-copy code for function %s", irfun->name);
3278         return false;
3279     }
3280     if (!gen_function_code(irfun)) {
3281         irerror(irfun->context, "Failed to generate code for function %s", irfun->name);
3282         return false;
3283     }
3284     return true;
3285 }
3286
3287 static void gen_vector_defs(prog_section_def def, const char *name)
3288 {
3289     char  *component;
3290     size_t len, i;
3291
3292     if (!name || name[0] == '#' || OPTS_FLAG(SINGLE_VECTOR_DEFS))
3293         return;
3294
3295     def.type = TYPE_FLOAT;
3296
3297     len = strlen(name);
3298
3299     component = (char*)mem_a(len+3);
3300     memcpy(component, name, len);
3301     len += 2;
3302     component[len-0] = 0;
3303     component[len-2] = '_';
3304
3305     component[len-1] = 'x';
3306
3307     for (i = 0; i < 3; ++i) {
3308         def.name = code_genstring(component);
3309         vec_push(code_defs, def);
3310         def.offset++;
3311         component[len-1]++;
3312     }
3313 }
3314
3315 static void gen_vector_fields(prog_section_field fld, const char *name)
3316 {
3317     char  *component;
3318     size_t len, i;
3319
3320     if (!name || OPTS_FLAG(SINGLE_VECTOR_DEFS))
3321         return;
3322
3323     fld.type = TYPE_FLOAT;
3324
3325     len = strlen(name);
3326
3327     component = (char*)mem_a(len+3);
3328     memcpy(component, name, len);
3329     len += 2;
3330     component[len-0] = 0;
3331     component[len-2] = '_';
3332
3333     component[len-1] = 'x';
3334
3335     for (i = 0; i < 3; ++i) {
3336         fld.name = code_genstring(component);
3337         vec_push(code_fields, fld);
3338         fld.offset++;
3339         component[len-1]++;
3340     }
3341 }
3342
3343 static bool ir_builder_gen_global(ir_builder *self, ir_value *global, bool islocal)
3344 {
3345     size_t           i;
3346     int32_t         *iptr;
3347     prog_section_def def;
3348     bool             pushdef = false;
3349
3350     def.type   = global->vtype;
3351     def.offset = vec_size(code_globals);
3352     def.name   = 0;
3353     if (opts.g || !islocal)
3354     {
3355         pushdef = true;
3356
3357         if (OPTS_OPTIMIZATION(OPTIM_STRIP_CONSTANT_NAMES) &&
3358             (global->name[0] == '#' || global->cvq == CV_CONST))
3359         {
3360             pushdef = false;
3361         }
3362
3363         if (pushdef && global->name) {
3364             if (global->name[0] == '#') {
3365                 if (!self->str_immediate)
3366                     self->str_immediate = code_genstring("IMMEDIATE");
3367                 def.name = global->code.name = self->str_immediate;
3368             }
3369             else
3370                 def.name = global->code.name = code_genstring(global->name);
3371         }
3372         else
3373             def.name   = 0;
3374         if (islocal) {
3375             def.offset = ir_value_code_addr(global);
3376             vec_push(code_defs, def);
3377             if (global->vtype == TYPE_VECTOR)
3378                 gen_vector_defs(def, global->name);
3379             else if (global->vtype == TYPE_FIELD && global->fieldtype == TYPE_VECTOR)
3380                 gen_vector_defs(def, global->name);
3381             return true;
3382         }
3383     }
3384     if (islocal)
3385         return true;
3386
3387     switch (global->vtype)
3388     {
3389     case TYPE_VOID:
3390         if (!strcmp(global->name, "end_sys_globals")) {
3391             /* TODO: remember this point... all the defs before this one
3392              * should be checksummed and added to progdefs.h when we generate it.
3393              */
3394         }
3395         else if (!strcmp(global->name, "end_sys_fields")) {
3396             /* TODO: same as above but for entity-fields rather than globsl
3397              */
3398         }
3399         else
3400             irwarning(global->context, WARN_VOID_VARIABLES, "unrecognized variable of type void `%s`",
3401                       global->name);
3402         /* I'd argue setting it to 0 is sufficient, but maybe some depend on knowing how far
3403          * the system fields actually go? Though the engine knows this anyway...
3404          * Maybe this could be an -foption
3405          * fteqcc creates data for end_sys_* - of size 1, so let's do the same
3406          */
3407         ir_value_code_setaddr(global, vec_size(code_globals));
3408         vec_push(code_globals, 0);
3409         /* Add the def */
3410         if (pushdef) vec_push(code_defs, def);
3411         return true;
3412     case TYPE_POINTER:
3413         if (pushdef) vec_push(code_defs, def);
3414         return gen_global_pointer(global);
3415     case TYPE_FIELD:
3416         if (pushdef) {
3417             vec_push(code_defs, def);
3418             if (global->fieldtype == TYPE_VECTOR)
3419                 gen_vector_defs(def, global->name);
3420         }
3421         return gen_global_field(global);
3422     case TYPE_ENTITY:
3423         /* fall through */
3424     case TYPE_FLOAT:
3425     {
3426         ir_value_code_setaddr(global, vec_size(code_globals));
3427         if (global->hasvalue) {
3428             iptr = (int32_t*)&global->constval.ivec[0];
3429             vec_push(code_globals, *iptr);
3430         } else {
3431             vec_push(code_globals, 0);
3432         }
3433         if (!islocal && global->cvq != CV_CONST)
3434             def.type |= DEF_SAVEGLOBAL;
3435         if (pushdef) vec_push(code_defs, def);
3436
3437         return global->code.globaladdr >= 0;
3438     }
3439     case TYPE_STRING:
3440     {
3441         ir_value_code_setaddr(global, vec_size(code_globals));
3442         if (global->hasvalue) {
3443             vec_push(code_globals, code_genstring(global->constval.vstring));
3444         } else {
3445             vec_push(code_globals, 0);
3446         }
3447         if (!islocal && global->cvq != CV_CONST)
3448             def.type |= DEF_SAVEGLOBAL;
3449         if (pushdef) vec_push(code_defs, def);
3450         return global->code.globaladdr >= 0;
3451     }
3452     case TYPE_VECTOR:
3453     {
3454         size_t d;
3455         ir_value_code_setaddr(global, vec_size(code_globals));
3456         if (global->hasvalue) {
3457             iptr = (int32_t*)&global->constval.ivec[0];
3458             vec_push(code_globals, iptr[0]);
3459             if (global->code.globaladdr < 0)
3460                 return false;
3461             for (d = 1; d < type_sizeof_[global->vtype]; ++d) {
3462                 vec_push(code_globals, iptr[d]);
3463             }
3464         } else {
3465             vec_push(code_globals, 0);
3466             if (global->code.globaladdr < 0)
3467                 return false;
3468             for (d = 1; d < type_sizeof_[global->vtype]; ++d) {
3469                 vec_push(code_globals, 0);
3470             }
3471         }
3472         if (!islocal && global->cvq != CV_CONST)
3473             def.type |= DEF_SAVEGLOBAL;
3474
3475         if (pushdef) {
3476             vec_push(code_defs, def);
3477             def.type &= ~DEF_SAVEGLOBAL;
3478             gen_vector_defs(def, global->name);
3479         }
3480         return global->code.globaladdr >= 0;
3481     }
3482     case TYPE_FUNCTION:
3483         ir_value_code_setaddr(global, vec_size(code_globals));
3484         if (!global->hasvalue) {
3485             vec_push(code_globals, 0);
3486             if (global->code.globaladdr < 0)
3487                 return false;
3488         } else {
3489             vec_push(code_globals, vec_size(code_functions));
3490             if (!gen_global_function(self, global))
3491                 return false;
3492         }
3493         if (!islocal && global->cvq != CV_CONST)
3494             def.type |= DEF_SAVEGLOBAL;
3495         if (pushdef) vec_push(code_defs, def);
3496         return true;
3497     case TYPE_VARIANT:
3498         /* assume biggest type */
3499             ir_value_code_setaddr(global, vec_size(code_globals));
3500             vec_push(code_globals, 0);
3501             for (i = 1; i < type_sizeof_[TYPE_VARIANT]; ++i)
3502                 vec_push(code_globals, 0);
3503             return true;
3504     default:
3505         /* refuse to create 'void' type or any other fancy business. */
3506         irerror(global->context, "Invalid type for global variable `%s`: %s",
3507                 global->name, type_name[global->vtype]);
3508         return false;
3509     }
3510 }
3511
3512 static void ir_builder_prepare_field(ir_value *field)
3513 {
3514     field->code.fieldaddr = code_alloc_field(type_sizeof_[field->fieldtype]);
3515 }
3516
3517 static bool ir_builder_gen_field(ir_builder *self, ir_value *field)
3518 {
3519     prog_section_def def;
3520     prog_section_field fld;
3521
3522     (void)self;
3523
3524     def.type   = (uint16_t)field->vtype;
3525     def.offset = (uint16_t)vec_size(code_globals);
3526
3527     /* create a global named the same as the field */
3528     if (opts.standard == COMPILER_GMQCC) {
3529         /* in our standard, the global gets a dot prefix */
3530         size_t len = strlen(field->name);
3531         char name[1024];
3532
3533         /* we really don't want to have to allocate this, and 1024
3534          * bytes is more than enough for a variable/field name
3535          */
3536         if (len+2 >= sizeof(name)) {
3537             irerror(field->context, "invalid field name size: %u", (unsigned int)len);
3538             return false;
3539         }
3540
3541         name[0] = '.';
3542         memcpy(name+1, field->name, len); /* no strncpy - we used strlen above */
3543         name[len+1] = 0;
3544
3545         def.name = code_genstring(name);
3546         fld.name = def.name + 1; /* we reuse that string table entry */
3547     } else {
3548         /* in plain QC, there cannot be a global with the same name,
3549          * and so we also name the global the same.
3550          * FIXME: fteqcc should create a global as well
3551          * check if it actually uses the same name. Probably does
3552          */
3553         def.name = code_genstring(field->name);
3554         fld.name = def.name;
3555     }
3556
3557     field->code.name = def.name;
3558
3559     vec_push(code_defs, def);
3560
3561     fld.type = field->fieldtype;
3562
3563     if (fld.type == TYPE_VOID) {
3564         irerror(field->context, "field is missing a type: %s - don't know its size", field->name);
3565         return false;
3566     }
3567
3568     fld.offset = field->code.fieldaddr;
3569
3570     vec_push(code_fields, fld);
3571
3572     ir_value_code_setaddr(field, vec_size(code_globals));
3573     vec_push(code_globals, fld.offset);
3574     if (fld.type == TYPE_VECTOR) {
3575         vec_push(code_globals, fld.offset+1);
3576         vec_push(code_globals, fld.offset+2);
3577     }
3578
3579     if (field->fieldtype == TYPE_VECTOR) {
3580         gen_vector_defs(def, field->name);
3581         gen_vector_fields(fld, field->name);
3582     }
3583
3584     return field->code.globaladdr >= 0;
3585 }
3586
3587 bool ir_builder_generate(ir_builder *self, const char *filename)
3588 {
3589     prog_section_statement stmt;
3590     size_t i;
3591     char  *lnofile = NULL;
3592
3593     code_init();
3594
3595     for (i = 0; i < vec_size(self->fields); ++i)
3596     {
3597         ir_builder_prepare_field(self->fields[i]);
3598     }
3599
3600     for (i = 0; i < vec_size(self->globals); ++i)
3601     {
3602         if (!ir_builder_gen_global(self, self->globals[i], false)) {
3603             return false;
3604         }
3605         if (self->globals[i]->vtype == TYPE_FUNCTION) {
3606             ir_function *func = self->globals[i]->constval.vfunc;
3607             if (func && self->max_locals < func->allocated_locals &&
3608                 !(func->flags & IR_FLAG_MASK_NO_OVERLAP))
3609             {
3610                 self->max_locals = func->allocated_locals;
3611             }
3612             if (func && self->max_globaltemps < func->globaltemps)
3613                 self->max_globaltemps = func->globaltemps;
3614         }
3615     }
3616
3617     for (i = 0; i < vec_size(self->fields); ++i)
3618     {
3619         if (!ir_builder_gen_field(self, self->fields[i])) {
3620             return false;
3621         }
3622     }
3623
3624     /* generate global temps */
3625     self->first_common_globaltemp = vec_size(code_globals);
3626     for (i = 0; i < self->max_globaltemps; ++i) {
3627         vec_push(code_globals, 0);
3628     }
3629     /* generate common locals */
3630     self->first_common_local = vec_size(code_globals);
3631     for (i = 0; i < self->max_locals; ++i) {
3632         vec_push(code_globals, 0);
3633     }
3634
3635     /* generate function code */
3636     for (i = 0; i < vec_size(self->globals); ++i)
3637     {
3638         if (self->globals[i]->vtype == TYPE_FUNCTION) {
3639             if (!gen_global_function_code(self, self->globals[i])) {
3640                 return false;
3641             }
3642         }
3643     }
3644
3645     if (vec_size(code_globals) >= 65536) {
3646         irerror(vec_last(self->globals)->context, "This progs file would require more globals than the metadata can handle. Bailing out.");
3647         return false;
3648     }
3649
3650     /* DP errors if the last instruction is not an INSTR_DONE. */
3651     if (vec_last(code_statements).opcode != INSTR_DONE)
3652     {
3653         stmt.opcode = INSTR_DONE;
3654         stmt.o1.u1 = 0;
3655         stmt.o2.u1 = 0;
3656         stmt.o3.u1 = 0;
3657         code_push_statement(&stmt, vec_last(code_linenums));
3658     }
3659
3660     if (opts.pp_only)
3661         return true;
3662
3663     if (vec_size(code_statements) != vec_size(code_linenums)) {
3664         con_err("Linecounter wrong: %lu != %lu\n",
3665                 (unsigned long)vec_size(code_statements),
3666                 (unsigned long)vec_size(code_linenums));
3667     } else if (OPTS_FLAG(LNO)) {
3668         char *dot;
3669         size_t filelen = strlen(filename);
3670
3671         memcpy(vec_add(lnofile, filelen+1), filename, filelen+1);
3672         dot = strrchr(lnofile, '.');
3673         if (!dot) {
3674             vec_pop(lnofile);
3675         } else {
3676             vec_shrinkto(lnofile, dot - lnofile);
3677         }
3678         memcpy(vec_add(lnofile, 5), ".lno", 5);
3679     }
3680
3681     if (!opts.quiet) {
3682         if (lnofile)
3683             con_out("writing '%s' and '%s'...\n", filename, lnofile);
3684         else
3685             con_out("writing '%s'\n", filename);
3686     }
3687     if (!code_write(filename, lnofile)) {
3688         vec_free(lnofile);
3689         return false;
3690     }
3691     vec_free(lnofile);
3692     return true;
3693 }
3694
3695 /***********************************************************************
3696  *IR DEBUG Dump functions...
3697  */
3698
3699 #define IND_BUFSZ 1024
3700
3701 #ifdef _MSC_VER
3702 #   define strncat(dst, src, sz) strncat_s(dst, sz, src, _TRUNCATE)
3703 #endif
3704
3705 const char *qc_opname(int op)
3706 {
3707     if (op < 0) return "<INVALID>";
3708     if (op < (int)( sizeof(asm_instr) / sizeof(asm_instr[0]) ))
3709         return asm_instr[op].m;
3710     switch (op) {
3711         case VINSTR_PHI:  return "PHI";
3712         case VINSTR_JUMP: return "JUMP";
3713         case VINSTR_COND: return "COND";
3714         default:          return "<UNK>";
3715     }
3716 }
3717
3718 void ir_builder_dump(ir_builder *b, int (*oprintf)(const char*, ...))
3719 {
3720     size_t i;
3721     char indent[IND_BUFSZ];
3722     indent[0] = '\t';
3723     indent[1] = 0;
3724
3725     oprintf("module %s\n", b->name);
3726     for (i = 0; i < vec_size(b->globals); ++i)
3727     {
3728         oprintf("global ");
3729         if (b->globals[i]->hasvalue)
3730             oprintf("%s = ", b->globals[i]->name);
3731         ir_value_dump(b->globals[i], oprintf);
3732         oprintf("\n");
3733     }
3734     for (i = 0; i < vec_size(b->functions); ++i)
3735         ir_function_dump(b->functions[i], indent, oprintf);
3736     oprintf("endmodule %s\n", b->name);
3737 }
3738
3739 void ir_function_dump(ir_function *f, char *ind,
3740                       int (*oprintf)(const char*, ...))
3741 {
3742     size_t i;
3743     if (f->builtin != 0) {
3744         oprintf("%sfunction %s = builtin %i\n", ind, f->name, -f->builtin);
3745         return;
3746     }
3747     oprintf("%sfunction %s\n", ind, f->name);
3748     strncat(ind, "\t", IND_BUFSZ);
3749     if (vec_size(f->locals))
3750     {
3751         oprintf("%s%i locals:\n", ind, (int)vec_size(f->locals));
3752         for (i = 0; i < vec_size(f->locals); ++i) {
3753             oprintf("%s\t", ind);
3754             ir_value_dump(f->locals[i], oprintf);
3755             oprintf("\n");
3756         }
3757     }
3758     oprintf("%sliferanges:\n", ind);
3759     for (i = 0; i < vec_size(f->locals); ++i) {
3760         const char *attr = "";
3761         size_t l, m;
3762         ir_value *v = f->locals[i];
3763         if (v->unique_life && v->locked)
3764             attr = "unique,locked ";
3765         else if (v->unique_life)
3766             attr = "unique ";
3767         else if (v->locked)
3768             attr = "locked ";
3769         oprintf("%s\t%s: %s@%i ", ind, v->name, attr, (int)v->code.local);
3770         for (l = 0; l < vec_size(v->life); ++l) {
3771             oprintf("[%i,%i] ", v->life[l].start, v->life[l].end);
3772         }
3773         oprintf("\n");
3774         for (m = 0; m < 3; ++m) {
3775             ir_value *vm = v->members[m];
3776             if (!vm)
3777                 continue;
3778             if (vm->unique_life && vm->locked)
3779                 attr = "unique,locked ";
3780             else if (vm->unique_life)
3781                 attr = "unique ";
3782             else if (vm->locked)
3783                 attr = "locked ";
3784             oprintf("%s\t%s: %s@%i ", ind, vm->name, attr, (int)vm->code.local);
3785             for (l = 0; l < vec_size(vm->life); ++l) {
3786                 oprintf("[%i,%i] ", vm->life[l].start, vm->life[l].end);
3787             }
3788             oprintf("\n");
3789         }
3790     }
3791     for (i = 0; i < vec_size(f->values); ++i) {
3792         size_t l;
3793         ir_value *v = f->values[i];
3794         oprintf("%s\t%s: @%i ", ind, v->name, (int)v->code.local);
3795         for (l = 0; l < vec_size(v->life); ++l) {
3796             oprintf("[%i,%i] ", v->life[l].start, v->life[l].end);
3797         }
3798         oprintf("\n");
3799     }
3800     if (vec_size(f->blocks))
3801     {
3802         oprintf("%slife passes (check): %i\n", ind, (int)f->run_id);
3803         for (i = 0; i < vec_size(f->blocks); ++i) {
3804             if (f->blocks[i]->run_id != f->run_id) {
3805                 oprintf("%slife pass check fail! %i != %i\n", ind, (int)f->blocks[i]->run_id, (int)f->run_id);
3806             }
3807             ir_block_dump(f->blocks[i], ind, oprintf);
3808         }
3809
3810     }
3811     ind[strlen(ind)-1] = 0;
3812     oprintf("%sendfunction %s\n", ind, f->name);
3813 }
3814
3815 void ir_block_dump(ir_block* b, char *ind,
3816                    int (*oprintf)(const char*, ...))
3817 {
3818     size_t i;
3819     oprintf("%s:%s\n", ind, b->label);
3820     strncat(ind, "\t", IND_BUFSZ);
3821
3822     for (i = 0; i < vec_size(b->instr); ++i)
3823         ir_instr_dump(b->instr[i], ind, oprintf);
3824     ind[strlen(ind)-1] = 0;
3825 }
3826
3827 void dump_phi(ir_instr *in, int (*oprintf)(const char*, ...))
3828 {
3829     size_t i;
3830     oprintf("%s <- phi ", in->_ops[0]->name);
3831     for (i = 0; i < vec_size(in->phi); ++i)
3832     {
3833         oprintf("([%s] : %s) ", in->phi[i].from->label,
3834                                 in->phi[i].value->name);
3835     }
3836     oprintf("\n");
3837 }
3838
3839 void ir_instr_dump(ir_instr *in, char *ind,
3840                        int (*oprintf)(const char*, ...))
3841 {
3842     size_t i;
3843     const char *comma = NULL;
3844
3845     oprintf("%s (%i) ", ind, (int)in->eid);
3846
3847     if (in->opcode == VINSTR_PHI) {
3848         dump_phi(in, oprintf);
3849         return;
3850     }
3851
3852     strncat(ind, "\t", IND_BUFSZ);
3853
3854     if (in->_ops[0] && (in->_ops[1] || in->_ops[2])) {
3855         ir_value_dump(in->_ops[0], oprintf);
3856         if (in->_ops[1] || in->_ops[2])
3857             oprintf(" <- ");
3858     }
3859     if (in->opcode == INSTR_CALL0 || in->opcode == VINSTR_NRCALL) {
3860         oprintf("CALL%i\t", vec_size(in->params));
3861     } else
3862         oprintf("%s\t", qc_opname(in->opcode));
3863
3864     if (in->_ops[0] && !(in->_ops[1] || in->_ops[2])) {
3865         ir_value_dump(in->_ops[0], oprintf);
3866         comma = ",\t";
3867     }
3868     else
3869     {
3870         for (i = 1; i != 3; ++i) {
3871             if (in->_ops[i]) {
3872                 if (comma)
3873                     oprintf(comma);
3874                 ir_value_dump(in->_ops[i], oprintf);
3875                 comma = ",\t";
3876             }
3877         }
3878     }
3879     if (in->bops[0]) {
3880         if (comma)
3881             oprintf(comma);
3882         oprintf("[%s]", in->bops[0]->label);
3883         comma = ",\t";
3884     }
3885     if (in->bops[1])
3886         oprintf("%s[%s]", comma, in->bops[1]->label);
3887     if (vec_size(in->params)) {
3888         oprintf("\tparams: ");
3889         for (i = 0; i != vec_size(in->params); ++i) {
3890             oprintf("%s, ", in->params[i]->name);
3891         }
3892     }
3893     oprintf("\n");
3894     ind[strlen(ind)-1] = 0;
3895 }
3896
3897 void ir_value_dump_string(const char *str, int (*oprintf)(const char*, ...))
3898 {
3899     oprintf("\"");
3900     for (; *str; ++str) {
3901         switch (*str) {
3902             case '\n': oprintf("\\n"); break;
3903             case '\r': oprintf("\\r"); break;
3904             case '\t': oprintf("\\t"); break;
3905             case '\v': oprintf("\\v"); break;
3906             case '\f': oprintf("\\f"); break;
3907             case '\b': oprintf("\\b"); break;
3908             case '\a': oprintf("\\a"); break;
3909             case '\\': oprintf("\\\\"); break;
3910             case '"': oprintf("\\\""); break;
3911             default: oprintf("%c", *str); break;
3912         }
3913     }
3914     oprintf("\"");
3915 }
3916
3917 void ir_value_dump(ir_value* v, int (*oprintf)(const char*, ...))
3918 {
3919     if (v->hasvalue) {
3920         switch (v->vtype) {
3921             default:
3922             case TYPE_VOID:
3923                 oprintf("(void)");
3924                 break;
3925             case TYPE_FUNCTION:
3926                 oprintf("fn:%s", v->name);
3927                 break;
3928             case TYPE_FLOAT:
3929                 oprintf("%g", v->constval.vfloat);
3930                 break;
3931             case TYPE_VECTOR:
3932                 oprintf("'%g %g %g'",
3933                         v->constval.vvec.x,
3934                         v->constval.vvec.y,
3935                         v->constval.vvec.z);
3936                 break;
3937             case TYPE_ENTITY:
3938                 oprintf("(entity)");
3939                 break;
3940             case TYPE_STRING:
3941                 ir_value_dump_string(v->constval.vstring, oprintf);
3942                 break;
3943 #if 0
3944             case TYPE_INTEGER:
3945                 oprintf("%i", v->constval.vint);
3946                 break;
3947 #endif
3948             case TYPE_POINTER:
3949                 oprintf("&%s",
3950                     v->constval.vpointer->name);
3951                 break;
3952         }
3953     } else {
3954         oprintf("%s", v->name);
3955     }
3956 }
3957
3958 void ir_value_dump_life(const ir_value *self, int (*oprintf)(const char*,...))
3959 {
3960     size_t i;
3961     oprintf("Life of %12s:", self->name);
3962     for (i = 0; i < vec_size(self->life); ++i)
3963     {
3964         oprintf(" + [%i, %i]\n", self->life[i].start, self->life[i].end);
3965     }
3966 }