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