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