]> de.git.xonotic.org Git - xonotic/gmqcc.git/blob - ir.c
All code is now C89/C90 compat
[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         ir_life_entry_t e;
515         if (life && life->end+1 == s)
516         {
517             /* previous life range can be merged in */
518             life->end++;
519             return true;
520         }
521         if (life && life->end >= s)
522             return false;
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     ir_value *out = NULL;
790     ir_instr *in  = NULL;
791     
792     int ot = TYPE_VOID;
793     switch (opcode) {
794         case INSTR_ADD_F:
795         case INSTR_SUB_F:
796         case INSTR_DIV_F:
797         case INSTR_MUL_F:
798         case INSTR_MUL_V:
799         case INSTR_AND:
800         case INSTR_OR:
801 #if 0
802         case INSTR_AND_I:
803         case INSTR_AND_IF:
804         case INSTR_AND_FI:
805         case INSTR_OR_I:
806         case INSTR_OR_IF:
807         case INSTR_OR_FI:
808 #endif
809         case INSTR_BITAND:
810         case INSTR_BITOR:
811 #if 0
812         case INSTR_SUB_S: /* -- offset of string as float */
813         case INSTR_MUL_IF:
814         case INSTR_MUL_FI:
815         case INSTR_DIV_IF:
816         case INSTR_DIV_FI:
817         case INSTR_BITOR_IF:
818         case INSTR_BITOR_FI:
819         case INSTR_BITAND_FI:
820         case INSTR_BITAND_IF:
821         case INSTR_EQ_I:
822         case INSTR_NE_I:
823 #endif
824             ot = TYPE_FLOAT;
825             break;
826 #if 0
827         case INSTR_ADD_I:
828         case INSTR_ADD_IF:
829         case INSTR_ADD_FI:
830         case INSTR_SUB_I:
831         case INSTR_SUB_FI:
832         case INSTR_SUB_IF:
833         case INSTR_MUL_I:
834         case INSTR_DIV_I:
835         case INSTR_BITAND_I:
836         case INSTR_BITOR_I:
837         case INSTR_XOR_I:
838         case INSTR_RSHIFT_I:
839         case INSTR_LSHIFT_I:
840             ot = TYPE_INTEGER;
841             break;
842 #endif
843         case INSTR_ADD_V:
844         case INSTR_SUB_V:
845         case INSTR_MUL_VF:
846         case INSTR_MUL_FV:
847 #if 0
848         case INSTR_DIV_VF:
849         case INSTR_MUL_IV:
850         case INSTR_MUL_VI:
851 #endif
852             ot = TYPE_VECTOR;
853             break;
854 #if 0
855         case INSTR_ADD_SF:
856             ot = TYPE_POINTER;
857             break;
858 #endif
859         default:
860             /* ranges: */
861             /* boolean operations result in floats */
862             if (opcode >= INSTR_EQ_F && opcode <= INSTR_GT)
863                 ot = TYPE_FLOAT;
864             else if (opcode >= INSTR_LE && opcode <= INSTR_GT)
865                 ot = TYPE_FLOAT;
866 #if 0
867             else if (opcode >= INSTR_LE_I && opcode <= INSTR_EQ_FI)
868                 ot = TYPE_FLOAT;
869 #endif
870             break;
871     };
872     if (ot == TYPE_VOID) {
873         /* The AST or parser were supposed to check this! */
874         return NULL;
875     }
876
877     out = ir_value_out(self->owner, label, store_local, ot);
878     if (!out)
879         return NULL;
880
881     in = ir_instr_new(self, opcode);
882     if (!in) {
883         ir_value_delete(out);
884         return NULL;
885     }
886
887     if (!ir_instr_op(in, 0, out, true) ||
888         !ir_instr_op(in, 1, left, false) ||
889         !ir_instr_op(in, 2, right, false) )
890     {
891         goto on_error;
892     }
893
894     if (!ir_block_instr_add(self, in))
895         goto on_error;
896
897     return out;
898 on_error:
899     ir_value_delete(out);
900     ir_instr_delete(in);
901     return NULL;
902 }
903
904 ir_value* ir_block_create_add(ir_block *self,
905                               const char *label,
906                               ir_value *left, ir_value *right)
907 {
908     int op = 0;
909     int l = left->vtype;
910     int r = right->vtype;
911     if (l == r) {
912         switch (l) {
913             default:
914                 return NULL;
915             case TYPE_FLOAT:
916                 op = INSTR_ADD_F;
917                 break;
918 #if 0
919             case TYPE_INTEGER:
920                 op = INSTR_ADD_I;
921                 break;
922 #endif
923             case TYPE_VECTOR:
924                 op = INSTR_ADD_V;
925                 break;
926         }
927     } else {
928 #if 0
929         if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
930             op = INSTR_ADD_FI;
931         else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
932             op = INSTR_ADD_IF;
933         else
934 #endif
935             return NULL;
936     }
937     return ir_block_create_binop(self, label, op, left, right);
938 }
939
940 ir_value* ir_block_create_sub(ir_block *self,
941                               const char *label,
942                               ir_value *left, ir_value *right)
943 {
944     int op = 0;
945     int l = left->vtype;
946     int r = right->vtype;
947     if (l == r) {
948
949         switch (l) {
950             default:
951                 return NULL;
952             case TYPE_FLOAT:
953                 op = INSTR_SUB_F;
954                 break;
955 #if 0
956             case TYPE_INTEGER:
957                 op = INSTR_SUB_I;
958                 break;
959 #endif
960             case TYPE_VECTOR:
961                 op = INSTR_SUB_V;
962                 break;
963         }
964     } else {
965 #if 0
966         if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
967             op = INSTR_SUB_FI;
968         else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
969             op = INSTR_SUB_IF;
970         else
971 #endif
972             return NULL;
973     }
974     return ir_block_create_binop(self, label, op, left, right);
975 }
976
977 ir_value* ir_block_create_mul(ir_block *self,
978                               const char *label,
979                               ir_value *left, ir_value *right)
980 {
981     int op = 0;
982     int l = left->vtype;
983     int r = right->vtype;
984     if (l == r) {
985
986         switch (l) {
987             default:
988                 return NULL;
989             case TYPE_FLOAT:
990                 op = INSTR_MUL_F;
991                 break;
992 #if 0
993             case TYPE_INTEGER:
994                 op = INSTR_MUL_I;
995                 break;
996 #endif
997             case TYPE_VECTOR:
998                 op = INSTR_MUL_V;
999                 break;
1000         }
1001     } else {
1002         if ( (l == TYPE_VECTOR && r == TYPE_FLOAT) )
1003             op = INSTR_MUL_VF;
1004         else if ( (l == TYPE_FLOAT && r == TYPE_VECTOR) )
1005             op = INSTR_MUL_FV;
1006 #if 0
1007         else if ( (l == TYPE_VECTOR && r == TYPE_INTEGER) )
1008             op = INSTR_MUL_VI;
1009         else if ( (l == TYPE_INTEGER && r == TYPE_VECTOR) )
1010             op = INSTR_MUL_IV;
1011         else if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1012             op = INSTR_MUL_FI;
1013         else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1014             op = INSTR_MUL_IF;
1015 #endif
1016         else
1017             return NULL;
1018     }
1019     return ir_block_create_binop(self, label, op, left, right);
1020 }
1021
1022 ir_value* ir_block_create_div(ir_block *self,
1023                               const char *label,
1024                               ir_value *left, ir_value *right)
1025 {
1026     int op = 0;
1027     int l = left->vtype;
1028     int r = right->vtype;
1029     if (l == r) {
1030
1031         switch (l) {
1032             default:
1033                 return NULL;
1034             case TYPE_FLOAT:
1035                 op = INSTR_DIV_F;
1036                 break;
1037 #if 0
1038             case TYPE_INTEGER:
1039                 op = INSTR_DIV_I;
1040                 break;
1041 #endif
1042         }
1043     } else {
1044 #if 0
1045         if ( (l == TYPE_VECTOR && r == TYPE_FLOAT) )
1046             op = INSTR_DIV_VF;
1047         else if ( (l == TYPE_FLOAT && r == TYPE_INTEGER) )
1048             op = INSTR_DIV_FI;
1049         else if ( (l == TYPE_INTEGER && r == TYPE_FLOAT) )
1050             op = INSTR_DIV_IF;
1051         else
1052 #endif
1053             return NULL;
1054     }
1055     return ir_block_create_binop(self, label, op, left, right);
1056 }
1057
1058 /* PHI resolving breaks the SSA, and must thus be the last
1059  * step before life-range calculation.
1060  */
1061
1062 static bool ir_block_naive_phi(ir_block *self);
1063 bool ir_function_naive_phi(ir_function *self)
1064 {
1065     size_t i;
1066
1067     for (i = 0; i < self->blocks_count; ++i)
1068     {
1069         if (!ir_block_naive_phi(self->blocks[i]))
1070             return false;
1071     }
1072     return true;
1073 }
1074
1075 static bool ir_naive_phi_emit_store(ir_block *block, size_t iid, ir_value *old, ir_value *what)
1076 {
1077     ir_instr *instr;
1078     size_t i;
1079
1080     /* create a store */
1081     if (!ir_block_create_store(block, old, what))
1082         return false;
1083
1084     /* we now move it up */
1085     instr = block->instr[block->instr_count-1];
1086     for (i = block->instr_count; i > iid; --i)
1087         block->instr[i] = block->instr[i-1];
1088     block->instr[i] = instr;
1089
1090     return true;
1091 }
1092
1093 static bool ir_block_naive_phi(ir_block *self)
1094 {
1095     size_t i, p, w;
1096     /* FIXME: optionally, create_phi can add the phis
1097      * to a list so we don't need to loop through blocks
1098      * - anyway: "don't optimize YET"
1099      */
1100     for (i = 0; i < self->instr_count; ++i)
1101     {
1102         ir_instr *instr = self->instr[i];
1103         if (instr->opcode != VINSTR_PHI)
1104             continue;
1105
1106         if (!ir_block_instr_remove(self, i))
1107             return false;
1108         --i; /* NOTE: i+1 below */
1109
1110         for (p = 0; p < instr->phi_count; ++p)
1111         {
1112             ir_value *v = instr->phi[p].value;
1113             for (w = 0; w < v->writes_count; ++w) {
1114                 ir_value *old;
1115
1116                 if (!v->writes[w]->_ops[0])
1117                     continue;
1118
1119                 /* When the write was to a global, we have to emit a mov */
1120                 old = v->writes[w]->_ops[0];
1121
1122                 /* The original instruction now writes to the PHI target local */
1123                 if (v->writes[w]->_ops[0] == v)
1124                     v->writes[w]->_ops[0] = instr->_ops[0];
1125
1126                 if (old->store != store_local)
1127                 {
1128                     /* If it originally wrote to a global we need to store the value
1129                      * there as welli
1130                      */
1131                     if (!ir_naive_phi_emit_store(self, i+1, old, v))
1132                         return false;
1133                     if (i+1 < self->instr_count)
1134                         instr = self->instr[i+1];
1135                     else
1136                         instr = NULL;
1137                     /* In case I forget and access instr later, it'll be NULL
1138                      * when it's a problem, to make sure we crash, rather than accessing
1139                      * invalid data.
1140                      */
1141                 }
1142                 else
1143                 {
1144                     /* If it didn't, we can replace all reads by the phi target now. */
1145                     size_t r;
1146                     for (r = 0; r < old->reads_count; ++r)
1147                     {
1148                         size_t op;
1149                         ir_instr *ri = old->reads[r];
1150                         for (op = 0; op < ri->phi_count; ++op) {
1151                             if (ri->phi[op].value == old)
1152                                 ri->phi[op].value = v;
1153                         }
1154                         for (op = 0; op < 3; ++op) {
1155                             if (ri->_ops[op] == old)
1156                                 ri->_ops[op] = v;
1157                         }
1158                     }
1159                 }
1160             }
1161         }
1162         ir_instr_delete(instr);
1163     }
1164     return true;
1165 }
1166
1167 /***********************************************************************
1168  *IR Temp allocation code
1169  * Propagating value life ranges by walking through the function backwards
1170  * until no more changes are made.
1171  * In theory this should happen once more than once for every nested loop
1172  * level.
1173  * Though this implementation might run an additional time for if nests.
1174  */
1175
1176 typedef struct
1177 {
1178     ir_value* *v;
1179     size_t    v_count;
1180     size_t    v_alloc;
1181 } new_reads_t;
1182 MEM_VEC_FUNCTIONS_ALL(new_reads_t, ir_value*, v)
1183
1184 /* Enumerate instructions used by value's life-ranges
1185  */
1186 static void ir_block_enumerate(ir_block *self, size_t *_eid)
1187 {
1188     size_t i;
1189     size_t eid = *_eid;
1190     for (i = 0; i < self->instr_count; ++i)
1191     {
1192         self->instr[i]->eid = eid++;
1193     }
1194     *_eid = eid;
1195 }
1196
1197 /* Enumerate blocks and instructions.
1198  * The block-enumeration is unordered!
1199  * We do not really use the block enumreation, however
1200  * the instruction enumeration is important for life-ranges.
1201  */
1202 void ir_function_enumerate(ir_function *self)
1203 {
1204     size_t i;
1205     size_t instruction_id = 0;
1206     for (i = 0; i < self->blocks_count; ++i)
1207     {
1208         self->blocks[i]->eid = i;
1209         self->blocks[i]->run_id = 0;
1210         ir_block_enumerate(self->blocks[i], &instruction_id);
1211     }
1212 }
1213
1214 static bool ir_block_life_propagate(ir_block *b, ir_block *prev, bool *changed);
1215 bool ir_function_calculate_liferanges(ir_function *self)
1216 {
1217     size_t i;
1218     bool changed;
1219
1220     do {
1221         self->run_id++;
1222         changed = false;
1223         for (i = 0; i != self->blocks_count; ++i)
1224         {
1225             if (self->blocks[i]->is_return)
1226             {
1227                 if (!ir_block_life_propagate(self->blocks[i], NULL, &changed))
1228                     return false;
1229             }
1230         }
1231     } while (changed);
1232     return true;
1233 }
1234
1235 /* Get information about which operand
1236  * is read from, or written to.
1237  */
1238 static void ir_op_read_write(int op, size_t *read, size_t *write)
1239 {
1240     switch (op)
1241     {
1242     case VINSTR_JUMP:
1243     case INSTR_GOTO:
1244         *write = 0;
1245         *read = 0;
1246         break;
1247     case INSTR_IF:
1248     case INSTR_IFNOT:
1249 #if 0
1250     case INSTR_IF_S:
1251     case INSTR_IFNOT_S:
1252 #endif
1253     case INSTR_RETURN:
1254     case VINSTR_COND:
1255         *write = 0;
1256         *read = 1;
1257         break;
1258     default:
1259         *write = 1;
1260         *read = 6;
1261         break;
1262     };
1263 }
1264
1265 static bool ir_block_living_add_instr(ir_block *self, size_t eid)
1266 {
1267     size_t i;
1268     bool changed = false;
1269     bool tempbool;
1270     for (i = 0; i != self->living_count; ++i)
1271     {
1272         tempbool = ir_value_life_merge(self->living[i], eid);
1273         /* debug
1274         if (tempbool)
1275             fprintf(stderr, "block_living_add_instr() value instruction added %s: %i\n", self->living[i]->_name, (int)eid);
1276         */
1277         changed = changed || tempbool;
1278     }
1279     return changed;
1280 }
1281
1282 static bool ir_block_life_prop_previous(ir_block* self, ir_block *prev, bool *changed)
1283 {
1284     size_t i;
1285     /* values which have been read in a previous iteration are now
1286      * in the "living" array even if the previous block doesn't use them.
1287      * So we have to remove whatever does not exist in the previous block.
1288      * They will be re-added on-read, but the liferange merge won't cause
1289      * a change.
1290      */
1291     for (i = 0; i < self->living_count; ++i)
1292     {
1293         if (!ir_block_living_find(prev, self->living[i], NULL)) {
1294             if (!ir_block_living_remove(self, i))
1295                 return false;
1296             --i;
1297         }
1298     }
1299
1300     /* Whatever the previous block still has in its living set
1301      * must now be added to ours as well.
1302      */
1303     for (i = 0; i < prev->living_count; ++i)
1304     {
1305         if (ir_block_living_find(self, prev->living[i], NULL))
1306             continue;
1307         if (!ir_block_living_add(self, prev->living[i]))
1308             return false;
1309         /*
1310         printf("%s got from prev: %s\n", self->label, prev->living[i]->_name);
1311         */
1312     }
1313     return true;
1314 }
1315
1316 static bool ir_block_life_propagate(ir_block *self, ir_block *prev, bool *changed)
1317 {
1318     ir_instr *instr;
1319     ir_value *value;
1320     bool  tempbool;
1321     size_t i, o, p, rd;
1322     /* bitmasks which operands are read from or written to */
1323     size_t read, write;
1324     new_reads_t new_reads;
1325     char dbg_ind[16] = { '#', '0' };
1326     (void)dbg_ind;
1327
1328     MEM_VECTOR_INIT(&new_reads, v);
1329
1330     if (prev)
1331     {
1332         if (!ir_block_life_prop_previous(self, prev, changed))
1333             return false;
1334     }
1335
1336     i = self->instr_count;
1337     while (i)
1338     { --i;
1339         instr = self->instr[i];
1340
1341         /* PHI operands are always read operands */
1342         for (p = 0; p < instr->phi_count; ++p)
1343         {
1344             value = instr->phi[p].value;
1345             /* used this before new_reads - puts the last read into the life range as well
1346             if (!ir_block_living_find(self, value, NULL))
1347                 ir_block_living_add(self, value);
1348             */
1349             /* fprintf(stderr, "read: %s\n", value->_name); */
1350             if (!new_reads_t_v_find(&new_reads, value, NULL))
1351             {
1352                 if (!new_reads_t_v_add(&new_reads, value))
1353                     goto on_error;
1354             }
1355         }
1356
1357         /* See which operands are read and write operands */
1358         ir_op_read_write(instr->opcode, &read, &write);
1359
1360         /* Go through the 3 main operands */
1361         for (o = 0; o < 3; ++o)
1362         {
1363             if (!instr->_ops[o]) /* no such operand */
1364                 continue;
1365
1366             value = instr->_ops[o];
1367
1368             /* We only care about locals */
1369             if (value->store != store_value &&
1370                 value->store != store_local)
1371                 continue;
1372
1373             /* read operands */
1374             if (read & (1<<o))
1375             {
1376                 /* used this before new_reads - puts the last read into the life range as well
1377                 if (!ir_block_living_find(self, value, NULL))
1378                     ir_block_living_add(self, value);
1379                 */
1380                 /* fprintf(stderr, "read: %s\n", value->_name); */
1381                 if (!new_reads_t_v_find(&new_reads, value, NULL))
1382                 {
1383                     if (!new_reads_t_v_add(&new_reads, value))
1384                         goto on_error;
1385                 }
1386             }
1387
1388             /* write operands */
1389             /* When we write to a local, we consider it "dead" for the
1390              * remaining upper part of the function, since in SSA a value
1391              * can only be written once (== created)
1392              */
1393             if (write & (1<<o))
1394             {
1395                 size_t idx, readidx;
1396                 bool in_living = ir_block_living_find(self, value, &idx);
1397                 bool in_reads = new_reads_t_v_find(&new_reads, value, &readidx);
1398                 if (!in_living && !in_reads)
1399                 {
1400                     /* If the value isn't alive it hasn't been read before... */
1401                     /* TODO: See if the warning can be emitted during parsing or AST processing
1402                      * otherwise have warning printed here.
1403                      * IF printing a warning here: include filecontext_t,
1404                      * and make sure it's only printed once
1405                      * since this function is run multiple times.
1406                      */
1407                     /* For now: debug info: */
1408                     fprintf(stderr, "Value only written %s\n", value->name);
1409                     tempbool = ir_value_life_merge(value, instr->eid);
1410                     *changed = *changed || tempbool;
1411                     /*
1412                     ir_instr_dump(instr, dbg_ind, printf);
1413                     abort();
1414                     */
1415                 } else {
1416                     /* since 'living' won't contain it
1417                      * anymore, merge the value, since
1418                      * (A) doesn't.
1419                      */
1420                     tempbool = ir_value_life_merge(value, instr->eid);
1421                     /*
1422                     if (tempbool)
1423                         fprintf(stderr, "value added id %s %i\n", value->name, (int)instr->eid);
1424                     */
1425                     *changed = *changed || tempbool;
1426                     /* Then remove */
1427                     if (!ir_block_living_remove(self, idx))
1428                         goto on_error;
1429                     if (in_reads)
1430                     {
1431                         if (!new_reads_t_v_remove(&new_reads, readidx))
1432                             goto on_error;
1433                     }
1434                 }
1435             }
1436         }
1437         /* (A) */
1438         tempbool = ir_block_living_add_instr(self, instr->eid);
1439         /*fprintf(stderr, "living added values\n");*/
1440         *changed = *changed || tempbool;
1441
1442         /* new reads: */
1443         for (rd = 0; rd < new_reads.v_count; ++rd)
1444         {
1445             if (!ir_block_living_find(self, new_reads.v[rd], NULL)) {
1446                 if (!ir_block_living_add(self, new_reads.v[rd]))
1447                     goto on_error;
1448             }
1449             if (!i && !self->entries_count) {
1450                 /* fix the top */
1451                 *changed = *changed || ir_value_life_merge(new_reads.v[rd], instr->eid);
1452             }
1453         }
1454         MEM_VECTOR_CLEAR(&new_reads, v);
1455     }
1456
1457     if (self->run_id == self->owner->run_id)
1458         return true;
1459
1460     self->run_id = self->owner->run_id;
1461
1462     for (i = 0; i < self->entries_count; ++i)
1463     {
1464         ir_block *entry = self->entries[i];
1465         ir_block_life_propagate(entry, self, changed);
1466     }
1467
1468     return true;
1469 on_error:
1470     MEM_VECTOR_CLEAR(&new_reads, v);
1471     return false;
1472 }
1473
1474 /***********************************************************************
1475  *IR DEBUG Dump functions...
1476  */
1477
1478 #define IND_BUFSZ 1024
1479
1480 const char *qc_opname(int op)
1481 {
1482     if (op < 0) return "<INVALID>";
1483     if (op < ( sizeof(asm_instr) / sizeof(asm_instr[0]) ))
1484         return asm_instr[op].m;
1485     switch (op) {
1486         case VINSTR_PHI:  return "PHI";
1487         case VINSTR_JUMP: return "JUMP";
1488         case VINSTR_COND: return "COND";
1489         default:          return "<UNK>";
1490     }
1491 }
1492
1493 void ir_builder_dump(ir_builder *b, int (*oprintf)(const char*, ...))
1494 {
1495         size_t i;
1496         char indent[IND_BUFSZ];
1497         indent[0] = '\t';
1498         indent[1] = 0;
1499
1500         oprintf("module %s\n", b->name);
1501         for (i = 0; i < b->globals_count; ++i)
1502         {
1503                 oprintf("global ");
1504                 if (b->globals[i]->isconst)
1505                         oprintf("%s = ", b->globals[i]->name);
1506                 ir_value_dump(b->globals[i], oprintf);
1507                 oprintf("\n");
1508         }
1509         for (i = 0; i < b->functions_count; ++i)
1510                 ir_function_dump(b->functions[i], indent, oprintf);
1511         oprintf("endmodule %s\n", b->name);
1512 }
1513
1514 void ir_function_dump(ir_function *f, char *ind,
1515                       int (*oprintf)(const char*, ...))
1516 {
1517         size_t i;
1518         oprintf("%sfunction %s\n", ind, f->name);
1519         strncat(ind, "\t", IND_BUFSZ);
1520         if (f->locals_count)
1521         {
1522                 oprintf("%s%i locals:\n", ind, (int)f->locals_count);
1523                 for (i = 0; i < f->locals_count; ++i) {
1524                         oprintf("%s\t", ind);
1525                         ir_value_dump(f->locals[i], oprintf);
1526                         oprintf("\n");
1527                 }
1528         }
1529         if (f->blocks_count)
1530         {
1531
1532                 oprintf("%slife passes: %i\n", ind, (int)f->blocks[0]->run_id);
1533                 for (i = 0; i < f->blocks_count; ++i)
1534                         ir_block_dump(f->blocks[i], ind, oprintf);
1535
1536         }
1537         ind[strlen(ind)-1] = 0;
1538         oprintf("%sendfunction %s\n", ind, f->name);
1539 }
1540
1541 void ir_block_dump(ir_block* b, char *ind,
1542                    int (*oprintf)(const char*, ...))
1543 {
1544         size_t i;
1545         oprintf("%s:%s\n", ind, b->label);
1546         strncat(ind, "\t", IND_BUFSZ);
1547
1548         for (i = 0; i < b->instr_count; ++i)
1549                 ir_instr_dump(b->instr[i], ind, oprintf);
1550         ind[strlen(ind)-1] = 0;
1551 }
1552
1553 void dump_phi(ir_instr *in, char *ind,
1554               int (*oprintf)(const char*, ...))
1555 {
1556         size_t i;
1557         oprintf("%s <- phi ", in->_ops[0]->name);
1558         for (i = 0; i < in->phi_count; ++i)
1559         {
1560                 oprintf("([%s] : %s) ", in->phi[i].from->label,
1561                                         in->phi[i].value->name);
1562         }
1563         oprintf("\n");
1564 }
1565
1566 void ir_instr_dump(ir_instr *in, char *ind,
1567                        int (*oprintf)(const char*, ...))
1568 {
1569         size_t i;
1570         const char *comma = NULL;
1571
1572         oprintf("%s (%i) ", ind, (int)in->eid);
1573
1574         if (in->opcode == VINSTR_PHI) {
1575                 dump_phi(in, ind, oprintf);
1576                 return;
1577         }
1578
1579         strncat(ind, "\t", IND_BUFSZ);
1580
1581         if (in->_ops[0] && (in->_ops[1] || in->_ops[2])) {
1582                 ir_value_dump(in->_ops[0], oprintf);
1583                 if (in->_ops[1] || in->_ops[2])
1584                         oprintf(" <- ");
1585         }
1586         oprintf("%s\t", qc_opname(in->opcode));
1587         if (in->_ops[0] && !(in->_ops[1] || in->_ops[2])) {
1588                 ir_value_dump(in->_ops[0], oprintf);
1589                 comma = ",\t";
1590         }
1591         else
1592         {
1593                 for (i = 1; i != 3; ++i) {
1594                         if (in->_ops[i]) {
1595                                 if (comma)
1596                                         oprintf(comma);
1597                                 ir_value_dump(in->_ops[i], oprintf);
1598                                 comma = ",\t";
1599                         }
1600                 }
1601         }
1602         if (in->bops[0]) {
1603                 if (comma)
1604                         oprintf(comma);
1605                 oprintf("[%s]", in->bops[0]->label);
1606                 comma = ",\t";
1607         }
1608         if (in->bops[1])
1609                 oprintf("%s[%s]", comma, in->bops[1]->label);
1610         oprintf("\n");
1611         ind[strlen(ind)-1] = 0;
1612 }
1613
1614 void ir_value_dump(ir_value* v, int (*oprintf)(const char*, ...))
1615 {
1616         if (v->isconst) {
1617                 switch (v->vtype) {
1618                         case TYPE_VOID:
1619                                 oprintf("(void)");
1620                                 break;
1621                         case TYPE_FLOAT:
1622                                 oprintf("%g", v->constval.vfloat);
1623                                 break;
1624                         case TYPE_VECTOR:
1625                                 oprintf("'%g %g %g'",
1626                                         v->constval.vvec.x,
1627                                         v->constval.vvec.y,
1628                                         v->constval.vvec.z);
1629                                 break;
1630                         case TYPE_ENTITY:
1631                                 oprintf("(entity)");
1632                                 break;
1633                         case TYPE_STRING:
1634                                 oprintf("\"%s\"", v->constval.vstring);
1635                                 break;
1636 #if 0
1637                         case TYPE_INTEGER:
1638                                 oprintf("%i", v->constval.vint);
1639                                 break;
1640 #endif
1641                         case TYPE_POINTER:
1642                                 oprintf("&%s",
1643                                         v->constval.vpointer->name);
1644                                 break;
1645                 }
1646         } else {
1647                 oprintf("%s", v->name);
1648         }
1649 }
1650
1651 void ir_value_dump_life(ir_value *self, int (*oprintf)(const char*,...))
1652 {
1653         size_t i;
1654         oprintf("Life of %s:\n", self->name);
1655         for (i = 0; i < self->life_count; ++i)
1656         {
1657                 oprintf(" + [%i, %i]\n", self->life[i].start, self->life[i].end);
1658         }
1659 }