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