]> git.xonotic.org Git - xonotic/gmqcc.git/blob - ir.c
Merge branch 'master' into ast-and-ir
[xonotic/gmqcc.git] / ir.c
1 /*
2  * Copyright (C) 2012
3  *     Wolfgang Bumiller
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is furnished to do
10  * so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 #include <stdlib.h>
24 #include <string.h>
25 #include "gmqcc.h"
26 #include "ir.h"
27
28 /***********************************************************************
29  *IR Builder
30  */
31
32 ir_builder* ir_builder_new(const char *modulename)
33 {
34     ir_builder* self;
35
36     self = (ir_builder*)mem_a(sizeof(*self));
37     MEM_VECTOR_INIT(self, functions);
38     MEM_VECTOR_INIT(self, globals);
39     self->name = NULL;
40     if (!ir_builder_set_name(self, modulename)) {
41         mem_d(self);
42         return NULL;
43     }
44
45     /* globals which always exist */
46
47     /* for now we give it a vector size */
48     ir_builder_create_global(self, "OFS_RETURN", TYPE_VARIANT);
49
50     return self;
51 }
52
53 MEM_VEC_FUNCTIONS(ir_builder, ir_value*, globals)
54 MEM_VEC_FUNCTIONS(ir_builder, ir_function*, functions)
55
56 void ir_builder_delete(ir_builder* self)
57 {
58     size_t i;
59     mem_d((void*)self->name);
60     for (i = 0; i != self->functions_count; ++i) {
61         ir_function_delete(self->functions[i]);
62     }
63     MEM_VECTOR_CLEAR(self, functions);
64     for (i = 0; i != self->globals_count; ++i) {
65         ir_value_delete(self->globals[i]);
66     }
67     MEM_VECTOR_CLEAR(self, globals);
68     mem_d(self);
69 }
70
71 bool ir_builder_set_name(ir_builder *self, const char *name)
72 {
73     if (self->name)
74         mem_d((void*)self->name);
75     self->name = util_strdup(name);
76     return !!self->name;
77 }
78
79 ir_function* ir_builder_get_function(ir_builder *self, const char *name)
80 {
81     size_t i;
82     for (i = 0; i < self->functions_count; ++i) {
83         if (!strcmp(name, self->functions[i]->name))
84             return self->functions[i];
85     }
86     return NULL;
87 }
88
89 ir_function* ir_builder_create_function(ir_builder *self, const char *name)
90 {
91     ir_function *fn = ir_builder_get_function(self, name);
92     if (fn) {
93         return NULL;
94     }
95
96     fn = ir_function_new(self);
97     if (!ir_function_set_name(fn, name) ||
98         !ir_builder_functions_add(self, fn) )
99     {
100         ir_function_delete(fn);
101         return NULL;
102     }
103     return fn;
104 }
105
106 ir_value* ir_builder_get_global(ir_builder *self, const char *name)
107 {
108     size_t i;
109     for (i = 0; i < self->globals_count; ++i) {
110         if (!strcmp(self->globals[i]->name, name))
111             return self->globals[i];
112     }
113     return NULL;
114 }
115
116 ir_value* ir_builder_create_global(ir_builder *self, const char *name, int vtype)
117 {
118     ir_value *ve = ir_builder_get_global(self, name);
119     if (ve) {
120         return NULL;
121     }
122
123     ve = ir_value_var(name, store_global, vtype);
124     if (!ir_builder_globals_add(self, ve)) {
125         ir_value_delete(ve);
126         return NULL;
127     }
128     return ve;
129 }
130
131 /***********************************************************************
132  *IR Function
133  */
134
135 bool ir_function_naive_phi(ir_function*);
136 void ir_function_enumerate(ir_function*);
137 bool ir_function_calculate_liferanges(ir_function*);
138
139 ir_function* ir_function_new(ir_builder* owner)
140 {
141     ir_function *self;
142     self = (ir_function*)mem_a(sizeof(*self));
143     self->name = NULL;
144     if (!ir_function_set_name(self, "<@unnamed>")) {
145         mem_d(self);
146         return NULL;
147     }
148     self->owner = owner;
149     self->context.file = "<@no context>";
150     self->context.line = 0;
151     self->retype = TYPE_VOID;
152     MEM_VECTOR_INIT(self, params);
153     MEM_VECTOR_INIT(self, blocks);
154     MEM_VECTOR_INIT(self, values);
155     MEM_VECTOR_INIT(self, locals);
156
157     self->run_id = 0;
158     return self;
159 }
160 MEM_VEC_FUNCTIONS(ir_function, ir_value*, values)
161 MEM_VEC_FUNCTIONS(ir_function, ir_block*, blocks)
162 MEM_VEC_FUNCTIONS(ir_function, ir_value*, locals)
163
164 bool ir_function_set_name(ir_function *self, const char *name)
165 {
166     if (self->name)
167         mem_d((void*)self->name);
168     self->name = util_strdup(name);
169     return !!self->name;
170 }
171
172 void ir_function_delete(ir_function *self)
173 {
174     size_t i;
175     mem_d((void*)self->name);
176
177     for (i = 0; i != self->blocks_count; ++i)
178         ir_block_delete(self->blocks[i]);
179     MEM_VECTOR_CLEAR(self, blocks);
180
181     MEM_VECTOR_CLEAR(self, params);
182
183     for (i = 0; i != self->values_count; ++i)
184         ir_value_delete(self->values[i]);
185     MEM_VECTOR_CLEAR(self, values);
186
187     for (i = 0; i != self->locals_count; ++i)
188         ir_value_delete(self->locals[i]);
189     MEM_VECTOR_CLEAR(self, locals);
190
191     mem_d(self);
192 }
193
194 bool GMQCC_WARN ir_function_collect_value(ir_function *self, ir_value *v)
195 {
196     return ir_function_values_add(self, v);
197 }
198
199 ir_block* ir_function_create_block(ir_function *self, const char *label)
200 {
201     ir_block* bn = ir_block_new(self, label);
202     memcpy(&bn->context, &self->context, sizeof(self->context));
203     if (!ir_function_blocks_add(self, bn)) {
204         ir_block_delete(bn);
205         return NULL;
206     }
207     return bn;
208 }
209
210 bool ir_function_finalize(ir_function *self)
211 {
212     if (!ir_function_naive_phi(self))
213         return false;
214
215     ir_function_enumerate(self);
216
217     if (!ir_function_calculate_liferanges(self))
218         return false;
219     return true;
220 }
221
222 ir_value* ir_function_get_local(ir_function *self, const char *name)
223 {
224     size_t i;
225     for (i = 0; i < self->locals_count; ++i) {
226         if (!strcmp(self->locals[i]->name, name))
227             return self->locals[i];
228     }
229     return NULL;
230 }
231
232 ir_value* ir_function_create_local(ir_function *self, const char *name, int vtype)
233 {
234     ir_value *ve = ir_function_get_local(self, name);
235     if (ve) {
236         return NULL;
237     }
238
239     ve = ir_value_var(name, store_local, vtype);
240     if (!ir_function_locals_add(self, ve)) {
241         ir_value_delete(ve);
242         return NULL;
243     }
244     return ve;
245 }
246
247 /***********************************************************************
248  *IR Block
249  */
250
251 ir_block* ir_block_new(ir_function* owner, const char *name)
252 {
253     ir_block *self;
254     self = (ir_block*)mem_a(sizeof(*self));
255     self->label = NULL;
256     if (!ir_block_set_label(self, name)) {
257         mem_d(self);
258         return NULL;
259     }
260     self->owner = owner;
261     self->context.file = "<@no context>";
262     self->context.line = 0;
263     self->final = false;
264     MEM_VECTOR_INIT(self, instr);
265     MEM_VECTOR_INIT(self, entries);
266     MEM_VECTOR_INIT(self, exits);
267
268     self->eid = 0;
269     self->is_return = false;
270     self->run_id = 0;
271     MEM_VECTOR_INIT(self, living);
272     return self;
273 }
274 MEM_VEC_FUNCTIONS(ir_block, ir_instr*, instr)
275 MEM_VEC_FUNCTIONS_ALL(ir_block, ir_block*, entries)
276 MEM_VEC_FUNCTIONS_ALL(ir_block, ir_block*, exits)
277 MEM_VEC_FUNCTIONS_ALL(ir_block, ir_value*, living)
278
279 void ir_block_delete(ir_block* self)
280 {
281     size_t i;
282     mem_d(self->label);
283     for (i = 0; i != self->instr_count; ++i)
284         ir_instr_delete(self->instr[i]);
285     MEM_VECTOR_CLEAR(self, instr);
286     MEM_VECTOR_CLEAR(self, entries);
287     MEM_VECTOR_CLEAR(self, exits);
288     MEM_VECTOR_CLEAR(self, living);
289     mem_d(self);
290 }
291
292 bool ir_block_set_label(ir_block *self, const char *name)
293 {
294     if (self->label)
295         mem_d((void*)self->label);
296     self->label = util_strdup(name);
297     return !!self->label;
298 }
299
300 /***********************************************************************
301  *IR Instructions
302  */
303
304 ir_instr* ir_instr_new(ir_block* owner, int op)
305 {
306     ir_instr *self;
307     self = (ir_instr*)mem_a(sizeof(*self));
308     self->owner = owner;
309     self->context.file = "<@no context>";
310     self->context.line = 0;
311     self->opcode = op;
312     self->_ops[0] = NULL;
313     self->_ops[1] = NULL;
314     self->_ops[2] = NULL;
315     self->bops[0] = NULL;
316     self->bops[1] = NULL;
317     MEM_VECTOR_INIT(self, phi);
318
319     self->eid = 0;
320     return self;
321 }
322 MEM_VEC_FUNCTIONS(ir_instr, ir_phi_entry_t, phi)
323
324 void ir_instr_delete(ir_instr *self)
325 {
326     size_t i;
327     /* The following calls can only delete from
328      * vectors, we still want to delete this instruction
329      * so ignore the return value. Since with the warn_unused_result attribute
330      * gcc doesn't care about an explicit: (void)foo(); to ignore the result,
331      * I have to improvise here and use if(foo());
332      */
333     for (i = 0; i < self->phi_count; ++i) {
334         size_t idx;
335         if (ir_value_writes_find(self->phi[i].value, self, &idx))
336             if (ir_value_writes_remove(self->phi[i].value, idx)) GMQCC_SUPRESS_EMPTY_BODY;
337         if (ir_value_reads_find(self->phi[i].value, self, &idx))
338             if (ir_value_reads_remove (self->phi[i].value, idx)) GMQCC_SUPRESS_EMPTY_BODY;
339     }
340     MEM_VECTOR_CLEAR(self, phi);
341     if (ir_instr_op(self, 0, NULL, false)) GMQCC_SUPRESS_EMPTY_BODY;
342     if (ir_instr_op(self, 1, NULL, false)) GMQCC_SUPRESS_EMPTY_BODY;
343     if (ir_instr_op(self, 2, NULL, false)) GMQCC_SUPRESS_EMPTY_BODY;
344     mem_d(self);
345 }
346
347 bool ir_instr_op(ir_instr *self, int op, ir_value *v, bool writing)
348 {
349     if (self->_ops[op]) {
350         size_t idx;
351         if (writing && ir_value_writes_find(self->_ops[op], self, &idx))
352         {
353             if (!ir_value_writes_remove(self->_ops[op], idx))
354                 return false;
355         }
356         else if (ir_value_reads_find(self->_ops[op], self, &idx))
357         {
358             if (!ir_value_reads_remove(self->_ops[op], idx))
359                 return false;
360         }
361     }
362     if (v) {
363         if (writing) {
364             if (!ir_value_writes_add(v, self))
365                 return false;
366         } else {
367             if (!ir_value_reads_add(v, self))
368                 return false;
369         }
370     }
371     self->_ops[op] = v;
372     return true;
373 }
374
375 /***********************************************************************
376  *IR Value
377  */
378
379 ir_value* ir_value_var(const char *name, int storetype, int vtype)
380 {
381     ir_value *self;
382     self = (ir_value*)mem_a(sizeof(*self));
383     self->vtype = vtype;
384     self->store = storetype;
385     MEM_VECTOR_INIT(self, reads);
386     MEM_VECTOR_INIT(self, writes);
387     self->isconst = false;
388     self->context.file = "<@no context>";
389     self->context.line = 0;
390     self->name = NULL;
391     ir_value_set_name(self, name);
392
393     MEM_VECTOR_INIT(self, life);
394     return self;
395 }
396 MEM_VEC_FUNCTIONS(ir_value, ir_life_entry_t, life)
397 MEM_VEC_FUNCTIONS_ALL(ir_value, ir_instr*, reads)
398 MEM_VEC_FUNCTIONS_ALL(ir_value, ir_instr*, writes)
399
400 ir_value* ir_value_out(ir_function *owner, const char *name, int storetype, int vtype)
401 {
402     ir_value *v = ir_value_var(name, storetype, vtype);
403     if (!v)
404         return NULL;
405     if (!ir_function_collect_value(owner, v))
406     {
407         ir_value_delete(v);
408         return NULL;
409     }
410     return v;
411 }
412
413 void ir_value_delete(ir_value* self)
414 {
415     mem_d((void*)self->name);
416     if (self->isconst)
417     {
418         if (self->vtype == TYPE_STRING)
419             mem_d((void*)self->constval.vstring);
420     }
421     MEM_VECTOR_CLEAR(self, reads);
422     MEM_VECTOR_CLEAR(self, writes);
423     MEM_VECTOR_CLEAR(self, life);
424     mem_d(self);
425 }
426
427 void ir_value_set_name(ir_value *self, const char *name)
428 {
429     if (self->name)
430         mem_d((void*)self->name);
431     self->name = util_strdup(name);
432 }
433
434 bool ir_value_set_float(ir_value *self, float f)
435 {
436     if (self->vtype != TYPE_FLOAT)
437         return false;
438     self->constval.vfloat = f;
439     self->isconst = true;
440     return true;
441 }
442
443 bool ir_value_set_vector(ir_value *self, vector v)
444 {
445     if (self->vtype != TYPE_VECTOR)
446         return false;
447     self->constval.vvec = v;
448     self->isconst = true;
449     return true;
450 }
451
452 bool ir_value_set_string(ir_value *self, const char *str)
453 {
454     if (self->vtype != TYPE_STRING)
455         return false;
456     self->constval.vstring = util_strdup(str);
457     self->isconst = true;
458     return true;
459 }
460
461 #if 0
462 bool ir_value_set_int(ir_value *self, int i)
463 {
464     if (self->vtype != TYPE_INTEGER)
465         return false;
466     self->constval.vint = i;
467     self->isconst = true;
468     return true;
469 }
470 #endif
471
472 bool ir_value_lives(ir_value *self, size_t at)
473 {
474     size_t i;
475     for (i = 0; i < self->life_count; ++i)
476     {
477         ir_life_entry_t *life = &self->life[i];
478         if (life->start <= at && at <= life->end)
479             return true;
480         if (life->start > at) /* since it's ordered */
481             return false;
482     }
483     return false;
484 }
485
486 bool ir_value_life_insert(ir_value *self, size_t idx, ir_life_entry_t e)
487 {
488     size_t k;
489     if (!ir_value_life_add(self, e)) /* naive... */
490         return false;
491     for (k = self->life_count-1; k > idx; --k)
492         self->life[k] = self->life[k-1];
493     self->life[idx] = e;
494     return true;
495 }
496
497 bool ir_value_life_merge(ir_value *self, size_t s)
498 {
499     size_t i;
500     ir_life_entry_t *life = NULL;
501     ir_life_entry_t *before = NULL;
502     ir_life_entry_t new_entry;
503
504     /* Find the first range >= s */
505     for (i = 0; i < self->life_count; ++i)
506     {
507         before = life;
508         life = &self->life[i];
509         if (life->start > s)
510             break;
511     }
512     /* nothing found? append */
513     if (i == self->life_count) {
514         if (life && life->end+1 == s)
515         {
516             /* previous life range can be merged in */
517             life->end++;
518             return true;
519         }
520         if (life && life->end >= s)
521             return false;
522         ir_life_entry_t e;
523         e.start = e.end = s;
524         if (!ir_value_life_add(self, e))
525             return false; /* failing */
526         return true;
527     }
528     /* found */
529     if (before)
530     {
531         if (before->end + 1 == s &&
532             life->start - 1 == s)
533         {
534             /* merge */
535             before->end = life->end;
536             if (!ir_value_life_remove(self, i))
537                 return false; /* failing */
538             return true;
539         }
540         if (before->end + 1 == s)
541         {
542             /* extend before */
543             before->end++;
544             return true;
545         }
546         /* already contained */
547         if (before->end >= s)
548             return false;
549     }
550     /* extend */
551     if (life->start - 1 == s)
552     {
553         life->start--;
554         return true;
555     }
556     /* insert a new entry */
557     new_entry.start = new_entry.end = s;
558     return ir_value_life_insert(self, i, new_entry);
559 }
560
561 /***********************************************************************
562  *IR main operations
563  */
564
565 bool ir_block_create_store_op(ir_block *self, int op, ir_value *target, ir_value *what)
566 {
567     if (target->store == store_value) {
568         fprintf(stderr, "cannot store to an SSA value\n");
569         return false;
570     } else {
571         ir_instr *in = ir_instr_new(self, op);
572         if (!in)
573             return false;
574         if (!ir_instr_op(in, 0, target, true) ||
575             !ir_instr_op(in, 1, what, false)  ||
576             !ir_block_instr_add(self, in) )
577         {
578             return false;
579         }
580         return true;
581     }
582 }
583
584 bool ir_block_create_store(ir_block *self, ir_value *target, ir_value *what)
585 {
586     int op = 0;
587     int vtype;
588     if (target->vtype == TYPE_VARIANT)
589         vtype = what->vtype;
590     else
591         vtype = target->vtype;
592
593     switch (vtype) {
594         case TYPE_FLOAT:
595 #if 0
596             if (what->vtype == TYPE_INTEGER)
597                 op = INSTR_CONV_ITOF;
598             else
599 #endif
600                 op = INSTR_STORE_F;
601             break;
602         case TYPE_VECTOR:
603             op = INSTR_STORE_V;
604             break;
605         case TYPE_ENTITY:
606             op = INSTR_STORE_ENT;
607             break;
608         case TYPE_STRING:
609             op = INSTR_STORE_S;
610             break;
611 #if 0
612         case TYPE_INTEGER:
613             if (what->vtype == TYPE_INTEGER)
614                 op = INSTR_CONV_FTOI;
615             else
616                 op = INSTR_STORE_I;
617             break;
618 #endif
619         case TYPE_POINTER:
620 #if 0
621             op = INSTR_STORE_I;
622 #else
623             op = INSTR_STORE_ENT;
624 #endif
625             break;
626     }
627     return ir_block_create_store_op(self, op, target, what);
628 }
629
630 bool ir_block_create_return(ir_block *self, ir_value *v)
631 {
632     ir_instr *in;
633     if (self->final) {
634         fprintf(stderr, "block already ended (%s)\n", self->label);
635         return false;
636     }
637     self->final = true;
638     self->is_return = true;
639     in = ir_instr_new(self, INSTR_RETURN);
640     if (!in)
641         return false;
642
643     if (!ir_instr_op(in, 0, v, false) ||
644         !ir_block_instr_add(self, in) )
645     {
646         return false;
647     }
648     return true;
649 }
650
651 bool ir_block_create_if(ir_block *self, ir_value *v,
652                         ir_block *ontrue, ir_block *onfalse)
653 {
654     ir_instr *in;
655     if (self->final) {
656         fprintf(stderr, "block already ended (%s)\n", self->label);
657         return false;
658     }
659     self->final = true;
660     //in = ir_instr_new(self, (v->vtype == TYPE_STRING ? INSTR_IF_S : INSTR_IF_F));
661     in = ir_instr_new(self, VINSTR_COND);
662     if (!in)
663         return false;
664
665     if (!ir_instr_op(in, 0, v, false)) {
666         ir_instr_delete(in);
667         return false;
668     }
669
670     in->bops[0] = ontrue;
671     in->bops[1] = onfalse;
672
673     if (!ir_block_instr_add(self, in))
674         return false;
675
676     if (!ir_block_exits_add(self, ontrue)    ||
677         !ir_block_exits_add(self, onfalse)   ||
678         !ir_block_entries_add(ontrue, self)  ||
679         !ir_block_entries_add(onfalse, self) )
680     {
681         return false;
682     }
683     return true;
684 }
685
686 bool ir_block_create_jump(ir_block *self, ir_block *to)
687 {
688     ir_instr *in;
689     if (self->final) {
690         fprintf(stderr, "block already ended (%s)\n", self->label);
691         return false;
692     }
693     self->final = true;
694     in = ir_instr_new(self, VINSTR_JUMP);
695     if (!in)
696         return false;
697
698     in->bops[0] = to;
699     if (!ir_block_instr_add(self, in))
700         return false;
701
702     if (!ir_block_exits_add(self, to) ||
703         !ir_block_entries_add(to, self) )
704     {
705         return false;
706     }
707     return true;
708 }
709
710 bool ir_block_create_goto(ir_block *self, ir_block *to)
711 {
712     ir_instr *in;
713     if (self->final) {
714         fprintf(stderr, "block already ended (%s)\n", self->label);
715         return false;
716     }
717     self->final = true;
718     in = ir_instr_new(self, INSTR_GOTO);
719     if (!in)
720         return false;
721
722     in->bops[0] = to;
723     if (!ir_block_instr_add(self, in))
724         return false;
725
726     if (!ir_block_exits_add(self, to) ||
727         !ir_block_entries_add(to, self) )
728     {
729         return false;
730     }
731     return true;
732 }
733
734 ir_instr* ir_block_create_phi(ir_block *self, const char *label, int ot)
735 {
736     ir_value *out;
737     ir_instr *in;
738     in = ir_instr_new(self, VINSTR_PHI);
739     if (!in)
740         return NULL;
741     out = ir_value_out(self->owner, label, store_local, ot);
742     if (!out) {
743         ir_instr_delete(in);
744         return NULL;
745     }
746     if (!ir_instr_op(in, 0, out, true)) {
747         ir_instr_delete(in);
748         ir_value_delete(out);
749         return NULL;
750     }
751     if (!ir_block_instr_add(self, in)) {
752         ir_instr_delete(in);
753         ir_value_delete(out);
754         return NULL;
755     }
756     return in;
757 }
758
759 ir_value* ir_phi_value(ir_instr *self)
760 {
761     return self->_ops[0];
762 }
763
764 bool ir_phi_add(ir_instr* self, ir_block *b, ir_value *v)
765 {
766     ir_phi_entry_t pe;
767
768     if (!ir_block_entries_find(self->owner, b, NULL)) {
769         /* Must not be possible to cause this, otherwise the AST
770          * is doing something wrong.
771          */
772         fprintf(stderr, "Invalid entry block for PHI\n");
773         abort();
774     }
775
776     pe.value = v;
777     pe.from = b;
778     if (!ir_value_reads_add(v, self))
779         return false;
780     return ir_instr_phi_add(self, pe);
781 }
782
783 /* binary op related code */
784
785 ir_value* ir_block_create_binop(ir_block *self,
786                                 const char *label, int opcode,
787                                 ir_value *left, ir_value *right)
788 {
789     int ot = TYPE_VOID;
790     switch (opcode) {
791         case INSTR_ADD_F:
792         case INSTR_SUB_F:
793         case INSTR_DIV_F:
794         case INSTR_MUL_F:
795         case INSTR_MUL_V:
796         case INSTR_AND:
797         case INSTR_OR:
798 #if 0
799         case INSTR_AND_I:
800         case INSTR_AND_IF:
801         case INSTR_AND_FI:
802         case INSTR_OR_I:
803         case INSTR_OR_IF:
804         case INSTR_OR_FI:
805 #endif
806         case INSTR_BITAND:
807         case INSTR_BITOR:
808 #if 0
809         case INSTR_SUB_S: /* -- offset of string as float */
810         case INSTR_MUL_IF:
811         case INSTR_MUL_FI:
812         case INSTR_DIV_IF:
813         case INSTR_DIV_FI:
814         case INSTR_BITOR_IF:
815         case INSTR_BITOR_FI:
816         case INSTR_BITAND_FI:
817         case INSTR_BITAND_IF:
818         case INSTR_EQ_I:
819         case INSTR_NE_I:
820 #endif
821             ot = TYPE_FLOAT;
822             break;
823 #if 0
824         case INSTR_ADD_I:
825         case INSTR_ADD_IF:
826         case INSTR_ADD_FI:
827         case INSTR_SUB_I:
828         case INSTR_SUB_FI:
829         case INSTR_SUB_IF:
830         case INSTR_MUL_I:
831         case INSTR_DIV_I:
832         case INSTR_BITAND_I:
833         case INSTR_BITOR_I:
834         case INSTR_XOR_I:
835         case INSTR_RSHIFT_I:
836         case INSTR_LSHIFT_I:
837             ot = TYPE_INTEGER;
838             break;
839 #endif
840         case INSTR_ADD_V:
841         case INSTR_SUB_V:
842         case INSTR_MUL_VF:
843         case INSTR_MUL_FV:
844 #if 0
845         case INSTR_DIV_VF:
846         case INSTR_MUL_IV:
847         case INSTR_MUL_VI:
848 #endif
849             ot = TYPE_VECTOR;
850             break;
851 #if 0
852         case INSTR_ADD_SF:
853             ot = TYPE_POINTER;
854             break;
855 #endif
856         default:
857             // ranges:
858             /* boolean operations result in floats */
859             if (opcode >= INSTR_EQ_F && opcode <= INSTR_GT)
860                 ot = TYPE_FLOAT;
861             else if (opcode >= INSTR_LE && opcode <= INSTR_GT)
862                 ot = TYPE_FLOAT;
863 #if 0
864             else if (opcode >= INSTR_LE_I && opcode <= INSTR_EQ_FI)
865                 ot = TYPE_FLOAT;
866 #endif
867             break;
868     };
869     if (ot == TYPE_VOID) {
870         /* The AST or parser were supposed to check this! */
871         return NULL;
872     }
873
874     ir_value *out = ir_value_out(self->owner, label, store_local, ot);
875     if (!out)
876         return NULL;
877
878     ir_instr *in = ir_instr_new(self, opcode);
879     if (!in) {
880         ir_value_delete(out);
881         return NULL;
882     }
883
884     if (!ir_instr_op(in, 0, out, true) ||
885         !ir_instr_op(in, 1, left, false) ||
886         !ir_instr_op(in, 2, right, false) )
887     {
888         goto on_error;
889     }
890
891     if (!ir_block_instr_add(self, in))
892         goto on_error;
893
894     return out;
895 on_error:
896     ir_value_delete(out);
897     ir_instr_delete(in);
898     return NULL;
899 }
900
901 ir_value* ir_block_create_add(ir_block *self,
902                               const char *label,
903                               ir_value *left, ir_value *right)
904 {
905     int op = 0;
906     int l = left->vtype;
907     int r = right->vtype;
908     if (l == r) {
909         switch (l) {
910             default:
911                 return NULL;
912             case TYPE_FLOAT:
913                 op = INSTR_ADD_F;
914                 break;
915 #if 0
916             case TYPE_INTEGER:
917                 op = INSTR_ADD_I;
918                 break;
919 #endif
920             case TYPE_VECTOR:
921                 op = INSTR_ADD_V;
922                 break;
923         }
924     } else {
925 #if 0
926         if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
927             op = INSTR_ADD_FI;
928         else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
929             op = INSTR_ADD_IF;
930         else
931 #endif
932             return NULL;
933     }
934     return ir_block_create_binop(self, label, op, left, right);
935 }
936
937 ir_value* ir_block_create_sub(ir_block *self,
938                               const char *label,
939                               ir_value *left, ir_value *right)
940 {
941     int op = 0;
942     int l = left->vtype;
943     int r = right->vtype;
944     if (l == r) {
945
946         switch (l) {
947             default:
948                 return NULL;
949             case TYPE_FLOAT:
950                 op = INSTR_SUB_F;
951                 break;
952 #if 0
953             case TYPE_INTEGER:
954                 op = INSTR_SUB_I;
955                 break;
956 #endif
957             case TYPE_VECTOR:
958                 op = INSTR_SUB_V;
959                 break;
960         }
961     } else {
962 #if 0
963         if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
964             op = INSTR_SUB_FI;
965         else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
966             op = INSTR_SUB_IF;
967         else
968 #endif
969             return NULL;
970     }
971     return ir_block_create_binop(self, label, op, left, right);
972 }
973
974 ir_value* ir_block_create_mul(ir_block *self,
975                               const char *label,
976                               ir_value *left, ir_value *right)
977 {
978     int op = 0;
979     int l = left->vtype;
980     int r = right->vtype;
981     if (l == r) {
982
983         switch (l) {
984             default:
985                 return NULL;
986             case TYPE_FLOAT:
987                 op = INSTR_MUL_F;
988                 break;
989 #if 0
990             case TYPE_INTEGER:
991                 op = INSTR_MUL_I;
992                 break;
993 #endif
994             case TYPE_VECTOR:
995                 op = INSTR_MUL_V;
996                 break;
997         }
998     } else {
999         if ( (l == TYPE_VECTOR && r == TYPE_FLOAT) )
1000             op = INSTR_MUL_VF;
1001         else if ( (l == TYPE_FLOAT && r == TYPE_VECTOR) )
1002             op = INSTR_MUL_FV;
1003 #if 0
1004         else if ( (l == TYPE_VECTOR && r == TYPE_INTEGER) )
1005             op = INSTR_MUL_VI;
1006         else if ( (l == TYPE_INTEGER && r == TYPE_VECTOR) )
1007             op = INSTR_MUL_IV;
1008         else if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1009             op = INSTR_MUL_FI;
1010         else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1011             op = INSTR_MUL_IF;
1012 #endif
1013         else
1014             return NULL;
1015     }
1016     return ir_block_create_binop(self, label, op, left, right);
1017 }
1018
1019 ir_value* ir_block_create_div(ir_block *self,
1020                               const char *label,
1021                               ir_value *left, ir_value *right)
1022 {
1023     int op = 0;
1024     int l = left->vtype;
1025     int r = right->vtype;
1026     if (l == r) {
1027
1028         switch (l) {
1029             default:
1030                 return NULL;
1031             case TYPE_FLOAT:
1032                 op = INSTR_DIV_F;
1033                 break;
1034 #if 0
1035             case TYPE_INTEGER:
1036                 op = INSTR_DIV_I;
1037                 break;
1038 #endif
1039         }
1040     } else {
1041 #if 0
1042         if ( (l == TYPE_VECTOR && r == TYPE_FLOAT) )
1043             op = INSTR_DIV_VF;
1044         else if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1045             op = INSTR_DIV_FI;
1046         else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1047             op = INSTR_DIV_IF;
1048         else
1049 #endif
1050             return NULL;
1051     }
1052     return ir_block_create_binop(self, label, op, left, right);
1053 }
1054
1055 /* PHI resolving breaks the SSA, and must thus be the last
1056  * step before life-range calculation.
1057  */
1058
1059 static bool ir_block_naive_phi(ir_block *self);
1060 bool ir_function_naive_phi(ir_function *self)
1061 {
1062     size_t i;
1063
1064     for (i = 0; i < self->blocks_count; ++i)
1065     {
1066         if (!ir_block_naive_phi(self->blocks[i]))
1067             return false;
1068     }
1069     return true;
1070 }
1071
1072 static bool ir_naive_phi_emit_store(ir_block *block, size_t iid, ir_value *old, ir_value *what)
1073 {
1074     ir_instr *instr;
1075     size_t i;
1076
1077     /* create a store */
1078     if (!ir_block_create_store(block, old, what))
1079         return false;
1080
1081     /* we now move it up */
1082     instr = block->instr[block->instr_count-1];
1083     for (i = block->instr_count; i > iid; --i)
1084         block->instr[i] = block->instr[i-1];
1085     block->instr[i] = instr;
1086
1087     return true;
1088 }
1089
1090 static bool ir_block_naive_phi(ir_block *self)
1091 {
1092     size_t i, p, w;
1093     /* FIXME: optionally, create_phi can add the phis
1094      * to a list so we don't need to loop through blocks
1095      * - anyway: "don't optimize YET"
1096      */
1097     for (i = 0; i < self->instr_count; ++i)
1098     {
1099         ir_instr *instr = self->instr[i];
1100         if (instr->opcode != VINSTR_PHI)
1101             continue;
1102
1103         if (!ir_block_instr_remove(self, i))
1104             return false;
1105         --i; /* NOTE: i+1 below */
1106
1107         for (p = 0; p < instr->phi_count; ++p)
1108         {
1109             ir_value *v = instr->phi[p].value;
1110             for (w = 0; w < v->writes_count; ++w) {
1111                 ir_value *old;
1112
1113                 if (!v->writes[w]->_ops[0])
1114                     continue;
1115
1116                 /* When the write was to a global, we have to emit a mov */
1117                 old = v->writes[w]->_ops[0];
1118
1119                 /* The original instruction now writes to the PHI target local */
1120                 if (v->writes[w]->_ops[0] == v)
1121                     v->writes[w]->_ops[0] = instr->_ops[0];
1122
1123                 if (old->store != store_local)
1124                 {
1125                     /* If it originally wrote to a global we need to store the value
1126                      * there as welli
1127                      */
1128                     if (!ir_naive_phi_emit_store(self, i+1, old, v))
1129                         return false;
1130                     if (i+1 < self->instr_count)
1131                         instr = self->instr[i+1];
1132                     else
1133                         instr = NULL;
1134                     /* In case I forget and access instr later, it'll be NULL
1135                      * when it's a problem, to make sure we crash, rather than accessing
1136                      * invalid data.
1137                      */
1138                 }
1139                 else
1140                 {
1141                     /* If it didn't, we can replace all reads by the phi target now. */
1142                     size_t r;
1143                     for (r = 0; r < old->reads_count; ++r)
1144                     {
1145                         size_t op;
1146                         ir_instr *ri = old->reads[r];
1147                         for (op = 0; op < ri->phi_count; ++op) {
1148                             if (ri->phi[op].value == old)
1149                                 ri->phi[op].value = v;
1150                         }
1151                         for (op = 0; op < 3; ++op) {
1152                             if (ri->_ops[op] == old)
1153                                 ri->_ops[op] = v;
1154                         }
1155                     }
1156                 }
1157             }
1158         }
1159         ir_instr_delete(instr);
1160     }
1161     return true;
1162 }
1163
1164 /***********************************************************************
1165  *IR Temp allocation code
1166  * Propagating value life ranges by walking through the function backwards
1167  * until no more changes are made.
1168  * In theory this should happen once more than once for every nested loop
1169  * level.
1170  * Though this implementation might run an additional time for if nests.
1171  */
1172
1173 typedef struct
1174 {
1175     ir_value* *v;
1176     size_t    v_count;
1177     size_t    v_alloc;
1178 } new_reads_t;
1179 MEM_VEC_FUNCTIONS_ALL(new_reads_t, ir_value*, v)
1180
1181 /* Enumerate instructions used by value's life-ranges
1182  */
1183 static void ir_block_enumerate(ir_block *self, size_t *_eid)
1184 {
1185     size_t i;
1186     size_t eid = *_eid;
1187     for (i = 0; i < self->instr_count; ++i)
1188     {
1189         self->instr[i]->eid = eid++;
1190     }
1191     *_eid = eid;
1192 }
1193
1194 /* Enumerate blocks and instructions.
1195  * The block-enumeration is unordered!
1196  * We do not really use the block enumreation, however
1197  * the instruction enumeration is important for life-ranges.
1198  */
1199 void ir_function_enumerate(ir_function *self)
1200 {
1201     size_t i;
1202     size_t instruction_id = 0;
1203     for (i = 0; i < self->blocks_count; ++i)
1204     {
1205         self->blocks[i]->eid = i;
1206         self->blocks[i]->run_id = 0;
1207         ir_block_enumerate(self->blocks[i], &instruction_id);
1208     }
1209 }
1210
1211 static bool ir_block_life_propagate(ir_block *b, ir_block *prev, bool *changed);
1212 bool ir_function_calculate_liferanges(ir_function *self)
1213 {
1214     size_t i;
1215     bool changed;
1216
1217     do {
1218         self->run_id++;
1219         changed = false;
1220         for (i = 0; i != self->blocks_count; ++i)
1221         {
1222             if (self->blocks[i]->is_return)
1223             {
1224                 if (!ir_block_life_propagate(self->blocks[i], NULL, &changed))
1225                     return false;
1226             }
1227         }
1228     } while (changed);
1229     return true;
1230 }
1231
1232 /* Get information about which operand
1233  * is read from, or written to.
1234  */
1235 static void ir_op_read_write(int op, size_t *read, size_t *write)
1236 {
1237     switch (op)
1238     {
1239     case VINSTR_JUMP:
1240     case INSTR_GOTO:
1241         *write = 0;
1242         *read = 0;
1243         break;
1244     case INSTR_IF:
1245     case INSTR_IFNOT:
1246 #if 0
1247     case INSTR_IF_S:
1248     case INSTR_IFNOT_S:
1249 #endif
1250     case INSTR_RETURN:
1251     case VINSTR_COND:
1252         *write = 0;
1253         *read = 1;
1254         break;
1255     default:
1256         *write = 1;
1257         *read = 6;
1258         break;
1259     };
1260 }
1261
1262 static bool ir_block_living_add_instr(ir_block *self, size_t eid)
1263 {
1264     size_t i;
1265     bool changed = false;
1266     bool tempbool;
1267     for (i = 0; i != self->living_count; ++i)
1268     {
1269         tempbool = ir_value_life_merge(self->living[i], eid);
1270         /* debug
1271         if (tempbool)
1272             fprintf(stderr, "block_living_add_instr() value instruction added %s: %i\n", self->living[i]->_name, (int)eid);
1273         */
1274         changed = changed || tempbool;
1275     }
1276     return changed;
1277 }
1278
1279 static bool ir_block_life_prop_previous(ir_block* self, ir_block *prev, bool *changed)
1280 {
1281     size_t i;
1282     /* values which have been read in a previous iteration are now
1283      * in the "living" array even if the previous block doesn't use them.
1284      * So we have to remove whatever does not exist in the previous block.
1285      * They will be re-added on-read, but the liferange merge won't cause
1286      * a change.
1287      */
1288     for (i = 0; i < self->living_count; ++i)
1289     {
1290         if (!ir_block_living_find(prev, self->living[i], NULL)) {
1291             if (!ir_block_living_remove(self, i))
1292                 return false;
1293             --i;
1294         }
1295     }
1296
1297     /* Whatever the previous block still has in its living set
1298      * must now be added to ours as well.
1299      */
1300     for (i = 0; i < prev->living_count; ++i)
1301     {
1302         if (ir_block_living_find(self, prev->living[i], NULL))
1303             continue;
1304         if (!ir_block_living_add(self, prev->living[i]))
1305             return false;
1306         /*
1307         printf("%s got from prev: %s\n", self->label, prev->living[i]->_name);
1308         */
1309     }
1310     return true;
1311 }
1312
1313 static bool ir_block_life_propagate(ir_block *self, ir_block *prev, bool *changed)
1314 {
1315     ir_instr *instr;
1316     ir_value *value;
1317     bool  tempbool;
1318     size_t i, o, p, rd;
1319     /* bitmasks which operands are read from or written to */
1320     size_t read, write;
1321     new_reads_t new_reads;
1322     char dbg_ind[16] = { '#', '0' };
1323     (void)dbg_ind;
1324
1325     MEM_VECTOR_INIT(&new_reads, v);
1326
1327     if (prev)
1328     {
1329         if (!ir_block_life_prop_previous(self, prev, changed))
1330             return false;
1331     }
1332
1333     i = self->instr_count;
1334     while (i)
1335     { --i;
1336         instr = self->instr[i];
1337
1338         /* PHI operands are always read operands */
1339         for (p = 0; p < instr->phi_count; ++p)
1340         {
1341             value = instr->phi[p].value;
1342             /* used this before new_reads - puts the last read into the life range as well
1343             if (!ir_block_living_find(self, value, NULL))
1344                 ir_block_living_add(self, value);
1345             */
1346             /* fprintf(stderr, "read: %s\n", value->_name); */
1347             if (!new_reads_t_v_find(&new_reads, value, NULL))
1348             {
1349                 if (!new_reads_t_v_add(&new_reads, value))
1350                     goto on_error;
1351             }
1352         }
1353
1354         /* See which operands are read and write operands */
1355         ir_op_read_write(instr->opcode, &read, &write);
1356
1357         /* Go through the 3 main operands */
1358         for (o = 0; o < 3; ++o)
1359         {
1360             if (!instr->_ops[o]) /* no such operand */
1361                 continue;
1362
1363             value = instr->_ops[o];
1364
1365             /* We only care about locals */
1366             if (value->store != store_value &&
1367                 value->store != store_local)
1368                 continue;
1369
1370             /* read operands */
1371             if (read & (1<<o))
1372             {
1373                 /* used this before new_reads - puts the last read into the life range as well
1374                 if (!ir_block_living_find(self, value, NULL))
1375                     ir_block_living_add(self, value);
1376                 */
1377                 /* fprintf(stderr, "read: %s\n", value->_name); */
1378                 if (!new_reads_t_v_find(&new_reads, value, NULL))
1379                 {
1380                     if (!new_reads_t_v_add(&new_reads, value))
1381                         goto on_error;
1382                 }
1383             }
1384
1385             /* write operands */
1386             /* When we write to a local, we consider it "dead" for the
1387              * remaining upper part of the function, since in SSA a value
1388              * can only be written once (== created)
1389              */
1390             if (write & (1<<o))
1391             {
1392                 size_t idx, readidx;
1393                 bool in_living = ir_block_living_find(self, value, &idx);
1394                 bool in_reads = new_reads_t_v_find(&new_reads, value, &readidx);
1395                 if (!in_living && !in_reads)
1396                 {
1397                     /* If the value isn't alive it hasn't been read before... */
1398                     /* TODO: See if the warning can be emitted during parsing or AST processing
1399                      * otherwise have warning printed here.
1400                      * IF printing a warning here: include filecontext_t,
1401                      * and make sure it's only printed once
1402                      * since this function is run multiple times.
1403                      */
1404                     /* For now: debug info: */
1405                     fprintf(stderr, "Value only written %s\n", value->name);
1406                     tempbool = ir_value_life_merge(value, instr->eid);
1407                     *changed = *changed || tempbool;
1408                     /*
1409                     ir_instr_dump(instr, dbg_ind, printf);
1410                     abort();
1411                     */
1412                 } else {
1413                     /* since 'living' won't contain it
1414                      * anymore, merge the value, since
1415                      * (A) doesn't.
1416                      */
1417                     tempbool = ir_value_life_merge(value, instr->eid);
1418                     /*
1419                     if (tempbool)
1420                         fprintf(stderr, "value added id %s %i\n", value->name, (int)instr->eid);
1421                     */
1422                     *changed = *changed || tempbool;
1423                     /* Then remove */
1424                     if (!ir_block_living_remove(self, idx))
1425                         goto on_error;
1426                     if (in_reads)
1427                     {
1428                         if (!new_reads_t_v_remove(&new_reads, readidx))
1429                             goto on_error;
1430                     }
1431                 }
1432             }
1433         }
1434         /* (A) */
1435         tempbool = ir_block_living_add_instr(self, instr->eid);
1436         //fprintf(stderr, "living added values\n");
1437         *changed = *changed || tempbool;
1438
1439         /* new reads: */
1440         for (rd = 0; rd < new_reads.v_count; ++rd)
1441         {
1442             if (!ir_block_living_find(self, new_reads.v[rd], NULL)) {
1443                 if (!ir_block_living_add(self, new_reads.v[rd]))
1444                     goto on_error;
1445             }
1446             if (!i && !self->entries_count) {
1447                 /* fix the top */
1448                 *changed = *changed || ir_value_life_merge(new_reads.v[rd], instr->eid);
1449             }
1450         }
1451         MEM_VECTOR_CLEAR(&new_reads, v);
1452     }
1453
1454     if (self->run_id == self->owner->run_id)
1455         return true;
1456
1457     self->run_id = self->owner->run_id;
1458
1459     for (i = 0; i < self->entries_count; ++i)
1460     {
1461         ir_block *entry = self->entries[i];
1462         ir_block_life_propagate(entry, self, changed);
1463     }
1464
1465     return true;
1466 on_error:
1467     MEM_VECTOR_CLEAR(&new_reads, v);
1468     return false;
1469 }
1470
1471 /***********************************************************************
1472  *IR DEBUG Dump functions...
1473  */
1474
1475 #define IND_BUFSZ 1024
1476
1477 const char *qc_opname(int op)
1478 {
1479     if (op < 0) return "<INVALID>";
1480     if (op < ( sizeof(asm_instr) / sizeof(asm_instr[0]) ))
1481         return asm_instr[op].m;
1482     switch (op) {
1483         case VINSTR_PHI:  return "PHI";
1484         case VINSTR_JUMP: return "JUMP";
1485         case VINSTR_COND: return "COND";
1486         default:          return "<UNK>";
1487     }
1488 }
1489
1490 void ir_builder_dump(ir_builder *b, int (*oprintf)(const char*, ...))
1491 {
1492         size_t i;
1493         char indent[IND_BUFSZ];
1494         indent[0] = '\t';
1495         indent[1] = 0;
1496
1497         oprintf("module %s\n", b->name);
1498         for (i = 0; i < b->globals_count; ++i)
1499         {
1500                 oprintf("global ");
1501                 if (b->globals[i]->isconst)
1502                         oprintf("%s = ", b->globals[i]->name);
1503                 ir_value_dump(b->globals[i], oprintf);
1504                 oprintf("\n");
1505         }
1506         for (i = 0; i < b->functions_count; ++i)
1507                 ir_function_dump(b->functions[i], indent, oprintf);
1508         oprintf("endmodule %s\n", b->name);
1509 }
1510
1511 void ir_function_dump(ir_function *f, char *ind,
1512                       int (*oprintf)(const char*, ...))
1513 {
1514         size_t i;
1515         oprintf("%sfunction %s\n", ind, f->name);
1516         strncat(ind, "\t", IND_BUFSZ);
1517         if (f->locals_count)
1518         {
1519                 oprintf("%s%i locals:\n", ind, (int)f->locals_count);
1520                 for (i = 0; i < f->locals_count; ++i) {
1521                         oprintf("%s\t", ind);
1522                         ir_value_dump(f->locals[i], oprintf);
1523                         oprintf("\n");
1524                 }
1525         }
1526         if (f->blocks_count)
1527         {
1528
1529                 oprintf("%slife passes: %i\n", ind, (int)f->blocks[0]->run_id);
1530                 for (i = 0; i < f->blocks_count; ++i)
1531                         ir_block_dump(f->blocks[i], ind, oprintf);
1532
1533         }
1534         ind[strlen(ind)-1] = 0;
1535         oprintf("%sendfunction %s\n", ind, f->name);
1536 }
1537
1538 void ir_block_dump(ir_block* b, char *ind,
1539                    int (*oprintf)(const char*, ...))
1540 {
1541         size_t i;
1542         oprintf("%s:%s\n", ind, b->label);
1543         strncat(ind, "\t", IND_BUFSZ);
1544
1545         for (i = 0; i < b->instr_count; ++i)
1546                 ir_instr_dump(b->instr[i], ind, oprintf);
1547         ind[strlen(ind)-1] = 0;
1548 }
1549
1550 void dump_phi(ir_instr *in, char *ind,
1551               int (*oprintf)(const char*, ...))
1552 {
1553         size_t i;
1554         oprintf("%s <- phi ", in->_ops[0]->name);
1555         for (i = 0; i < in->phi_count; ++i)
1556         {
1557                 oprintf("([%s] : %s) ", in->phi[i].from->label,
1558                                         in->phi[i].value->name);
1559         }
1560         oprintf("\n");
1561 }
1562
1563 void ir_instr_dump(ir_instr *in, char *ind,
1564                        int (*oprintf)(const char*, ...))
1565 {
1566         size_t i;
1567         const char *comma = NULL;
1568
1569         oprintf("%s (%i) ", ind, (int)in->eid);
1570
1571         if (in->opcode == VINSTR_PHI) {
1572                 dump_phi(in, ind, oprintf);
1573                 return;
1574         }
1575
1576         strncat(ind, "\t", IND_BUFSZ);
1577
1578         if (in->_ops[0] && (in->_ops[1] || in->_ops[2])) {
1579                 ir_value_dump(in->_ops[0], oprintf);
1580                 if (in->_ops[1] || in->_ops[2])
1581                         oprintf(" <- ");
1582         }
1583         oprintf("%s\t", qc_opname(in->opcode));
1584         if (in->_ops[0] && !(in->_ops[1] || in->_ops[2])) {
1585                 ir_value_dump(in->_ops[0], oprintf);
1586                 comma = ",\t";
1587         }
1588         else
1589         {
1590                 for (i = 1; i != 3; ++i) {
1591                         if (in->_ops[i]) {
1592                                 if (comma)
1593                                         oprintf(comma);
1594                                 ir_value_dump(in->_ops[i], oprintf);
1595                                 comma = ",\t";
1596                         }
1597                 }
1598         }
1599         if (in->bops[0]) {
1600                 if (comma)
1601                         oprintf(comma);
1602                 oprintf("[%s]", in->bops[0]->label);
1603                 comma = ",\t";
1604         }
1605         if (in->bops[1])
1606                 oprintf("%s[%s]", comma, in->bops[1]->label);
1607         oprintf("\n");
1608         ind[strlen(ind)-1] = 0;
1609 }
1610
1611 void ir_value_dump(ir_value* v, int (*oprintf)(const char*, ...))
1612 {
1613         if (v->isconst) {
1614                 switch (v->vtype) {
1615                         case TYPE_VOID:
1616                                 oprintf("(void)");
1617                                 break;
1618                         case TYPE_FLOAT:
1619                                 oprintf("%g", v->constval.vfloat);
1620                                 break;
1621                         case TYPE_VECTOR:
1622                                 oprintf("'%g %g %g'",
1623                                         v->constval.vvec.x,
1624                                         v->constval.vvec.y,
1625                                         v->constval.vvec.z);
1626                                 break;
1627                         case TYPE_ENTITY:
1628                                 oprintf("(entity)");
1629                                 break;
1630                         case TYPE_STRING:
1631                                 oprintf("\"%s\"", v->constval.vstring);
1632                                 break;
1633 #if 0
1634                         case TYPE_INTEGER:
1635                                 oprintf("%i", v->constval.vint);
1636                                 break;
1637 #endif
1638                         case TYPE_POINTER:
1639                                 oprintf("&%s",
1640                                         v->constval.vpointer->name);
1641                                 break;
1642                 }
1643         } else {
1644                 oprintf("%s", v->name);
1645         }
1646 }
1647
1648 void ir_value_dump_life(ir_value *self, int (*oprintf)(const char*,...))
1649 {
1650         size_t i;
1651         oprintf("Life of %s:\n", self->name);
1652         for (i = 0; i < self->life_count; ++i)
1653         {
1654                 oprintf(" + [%i, %i]\n", self->life[i].start, self->life[i].end);
1655         }
1656 }