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