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