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