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