]> de.git.xonotic.org Git - xonotic/gmqcc.git/blob - ast.c
Fixed a bug in the generation of default switch cases: they can now fall through...
[xonotic/gmqcc.git] / ast.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 <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "gmqcc.h"
28 #include "ast.h"
29
30 #define ast_instantiate(T, ctx, destroyfn)                          \
31     T* self = (T*)mem_a(sizeof(T));                                 \
32     if (!self) {                                                    \
33         return NULL;                                                \
34     }                                                               \
35     ast_node_init((ast_node*)self, ctx, TYPE_##T);                  \
36     ( (ast_node*)self )->node.destroy = (ast_node_delete*)destroyfn
37
38
39 /* It must not be possible to get here. */
40 static GMQCC_NORETURN void _ast_node_destroy(ast_node *self)
41 {
42     (void)self;
43     con_err("ast node missing destroy()\n");
44     abort();
45 }
46
47 /* Initialize main ast node aprts */
48 static void ast_node_init(ast_node *self, lex_ctx ctx, int nodetype)
49 {
50     self->node.context = ctx;
51     self->node.destroy = &_ast_node_destroy;
52     self->node.keep    = false;
53     self->node.nodetype = nodetype;
54     self->node.side_effects = false;
55 }
56
57 /* weight and side effects */
58 static void _ast_propagate_effects(ast_node *self, ast_node *other)
59 {
60     if (ast_side_effects(other))
61         ast_side_effects(self) = true;
62 }
63 #define ast_propagate_effects(s,o) _ast_propagate_effects(((ast_node*)(s)), ((ast_node*)(o)))
64
65 /* General expression initialization */
66 static void ast_expression_init(ast_expression *self,
67                                 ast_expression_codegen *codegen)
68 {
69     self->expression.codegen  = codegen;
70     self->expression.vtype    = TYPE_VOID;
71     self->expression.next     = NULL;
72     self->expression.outl     = NULL;
73     self->expression.outr     = NULL;
74     self->expression.variadic = false;
75     self->expression.params   = NULL;
76     self->expression.count    = 0;
77 }
78
79 static void ast_expression_delete(ast_expression *self)
80 {
81     size_t i;
82     if (self->expression.next)
83         ast_delete(self->expression.next);
84     for (i = 0; i < vec_size(self->expression.params); ++i) {
85         ast_delete(self->expression.params[i]);
86     }
87     vec_free(self->expression.params);
88 }
89
90 static void ast_expression_delete_full(ast_expression *self)
91 {
92     ast_expression_delete(self);
93     mem_d(self);
94 }
95
96 ast_value* ast_value_copy(const ast_value *self)
97 {
98     size_t i;
99     const ast_expression_common *fromex;
100     ast_expression_common *selfex;
101     ast_value *cp = ast_value_new(self->expression.node.context, self->name, self->expression.vtype);
102     if (self->expression.next) {
103         cp->expression.next = ast_type_copy(self->expression.node.context, self->expression.next);
104         if (!cp->expression.next) {
105             ast_value_delete(cp);
106             return NULL;
107         }
108     }
109     fromex   = &self->expression;
110     selfex = &cp->expression;
111     selfex->variadic = fromex->variadic;
112     selfex->count    = fromex->count;
113     for (i = 0; i < vec_size(fromex->params); ++i) {
114         ast_value *v = ast_value_copy(fromex->params[i]);
115         if (!v) {
116             ast_value_delete(cp);
117             return NULL;
118         }
119         vec_push(selfex->params, v);
120     }
121     return cp;
122 }
123
124 bool ast_type_adopt_impl(ast_expression *self, const ast_expression *other)
125 {
126     size_t i;
127     const ast_expression_common *fromex;
128     ast_expression_common *selfex;
129     self->expression.vtype = other->expression.vtype;
130     if (other->expression.next) {
131         self->expression.next = (ast_expression*)ast_type_copy(ast_ctx(self), other->expression.next);
132         if (!self->expression.next)
133             return false;
134     }
135     fromex   = &other->expression;
136     selfex = &self->expression;
137     selfex->variadic = fromex->variadic;
138     selfex->count    = fromex->count;
139     for (i = 0; i < vec_size(fromex->params); ++i) {
140         ast_value *v = ast_value_copy(fromex->params[i]);
141         if (!v)
142             return false;
143         vec_push(selfex->params, v);
144     }
145     return true;
146 }
147
148 static ast_expression* ast_shallow_type(lex_ctx ctx, int vtype)
149 {
150     ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
151     ast_expression_init(self, NULL);
152     self->expression.codegen = NULL;
153     self->expression.next    = NULL;
154     self->expression.vtype   = vtype;
155     return self;
156 }
157
158 ast_expression* ast_type_copy(lex_ctx ctx, const ast_expression *ex)
159 {
160     size_t i;
161     const ast_expression_common *fromex;
162     ast_expression_common *selfex;
163
164     if (!ex)
165         return NULL;
166     else
167     {
168         ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
169         ast_expression_init(self, NULL);
170
171         fromex   = &ex->expression;
172         selfex = &self->expression;
173
174         /* This may never be codegen()d */
175         selfex->codegen = NULL;
176
177         selfex->vtype = fromex->vtype;
178         if (fromex->next)
179         {
180             selfex->next = ast_type_copy(ctx, fromex->next);
181             if (!selfex->next) {
182                 ast_expression_delete_full(self);
183                 return NULL;
184             }
185         }
186         else
187             selfex->next = NULL;
188
189         selfex->variadic = fromex->variadic;
190         selfex->count    = fromex->count;
191         for (i = 0; i < vec_size(fromex->params); ++i) {
192             ast_value *v = ast_value_copy(fromex->params[i]);
193             if (!v) {
194                 ast_expression_delete_full(self);
195                 return NULL;
196             }
197             vec_push(selfex->params, v);
198         }
199
200         return self;
201     }
202 }
203
204 bool ast_compare_type(ast_expression *a, ast_expression *b)
205 {
206     if (a->expression.vtype != b->expression.vtype)
207         return false;
208     if (!a->expression.next != !b->expression.next)
209         return false;
210     if (vec_size(a->expression.params) != vec_size(b->expression.params))
211         return false;
212     if (a->expression.variadic != b->expression.variadic)
213         return false;
214     if (vec_size(a->expression.params)) {
215         size_t i;
216         for (i = 0; i < vec_size(a->expression.params); ++i) {
217             if (!ast_compare_type((ast_expression*)a->expression.params[i],
218                                   (ast_expression*)b->expression.params[i]))
219                 return false;
220         }
221     }
222     if (a->expression.next)
223         return ast_compare_type(a->expression.next, b->expression.next);
224     return true;
225 }
226
227 static size_t ast_type_to_string_impl(ast_expression *e, char *buf, size_t bufsize, size_t pos)
228 {
229     const char *typestr;
230     size_t typelen;
231     size_t i;
232
233     if (!e) {
234         if (pos + 6 >= bufsize)
235             goto full;
236         strcpy(buf + pos, "(null)");
237         return pos + 6;
238     }
239
240     if (pos + 1 >= bufsize)
241         goto full;
242
243     switch (e->expression.vtype) {
244         case TYPE_VARIANT:
245             strcpy(buf + pos, "(variant)");
246             return pos + 9;
247
248         case TYPE_FIELD:
249             buf[pos++] = '.';
250             return ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
251
252         case TYPE_POINTER:
253             if (pos + 3 >= bufsize)
254                 goto full;
255             buf[pos++] = '*';
256             buf[pos++] = '(';
257             pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
258             if (pos + 1 >= bufsize)
259                 goto full;
260             buf[pos++] = ')';
261             return pos;
262
263         case TYPE_FUNCTION:
264             pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
265             if (pos + 2 >= bufsize)
266                 goto full;
267             if (!vec_size(e->expression.params)) {
268                 buf[pos++] = '(';
269                 buf[pos++] = ')';
270                 return pos;
271             }
272             buf[pos++] = '(';
273             pos = ast_type_to_string_impl((ast_expression*)(e->expression.params[0]), buf, bufsize, pos);
274             for (i = 1; i < vec_size(e->expression.params); ++i) {
275                 if (pos + 2 >= bufsize)
276                     goto full;
277                 buf[pos++] = ',';
278                 buf[pos++] = ' ';
279                 pos = ast_type_to_string_impl((ast_expression*)(e->expression.params[i]), buf, bufsize, pos);
280             }
281             if (pos + 1 >= bufsize)
282                 goto full;
283             buf[pos++] = ')';
284             return pos;
285
286         case TYPE_ARRAY:
287             pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
288             if (pos + 1 >= bufsize)
289                 goto full;
290             buf[pos++] = '[';
291             pos += snprintf(buf + pos, bufsize - pos - 1, "%i", (int)e->expression.count);
292             if (pos + 1 >= bufsize)
293                 goto full;
294             buf[pos++] = ']';
295             return pos;
296
297         default:
298             typestr = type_name[e->expression.vtype];
299             typelen = strlen(typestr);
300             if (pos + typelen >= bufsize)
301                 goto full;
302             strcpy(buf + pos, typestr);
303             return pos + typelen;
304     }
305
306 full:
307     buf[bufsize-3] = '.';
308     buf[bufsize-2] = '.';
309     buf[bufsize-1] = '.';
310     return bufsize;
311 }
312
313 void ast_type_to_string(ast_expression *e, char *buf, size_t bufsize)
314 {
315     size_t pos = ast_type_to_string_impl(e, buf, bufsize-1, 0);
316     buf[pos] = 0;
317 }
318
319 ast_value* ast_value_new(lex_ctx ctx, const char *name, int t)
320 {
321     ast_instantiate(ast_value, ctx, ast_value_delete);
322     ast_expression_init((ast_expression*)self,
323                         (ast_expression_codegen*)&ast_value_codegen);
324     self->expression.node.keep = true; /* keep */
325
326     self->name = name ? util_strdup(name) : NULL;
327     self->expression.vtype = t;
328     self->expression.next  = NULL;
329     self->isfield  = false;
330     self->cvq      = CV_NONE;
331     self->hasvalue = false;
332     self->uses    = 0;
333     memset(&self->constval, 0, sizeof(self->constval));
334
335     self->ir_v           = NULL;
336     self->ir_values      = NULL;
337     self->ir_value_count = 0;
338
339     self->setter = NULL;
340     self->getter = NULL;
341
342     return self;
343 }
344
345 void ast_value_delete(ast_value* self)
346 {
347     if (self->name)
348         mem_d((void*)self->name);
349     if (self->hasvalue) {
350         switch (self->expression.vtype)
351         {
352         case TYPE_STRING:
353             mem_d((void*)self->constval.vstring);
354             break;
355         case TYPE_FUNCTION:
356             /* unlink us from the function node */
357             self->constval.vfunc->vtype = NULL;
358             break;
359         /* NOTE: delete function? currently collected in
360          * the parser structure
361          */
362         default:
363             break;
364         }
365     }
366     if (self->ir_values)
367         mem_d(self->ir_values);
368     ast_expression_delete((ast_expression*)self);
369     mem_d(self);
370 }
371
372 void ast_value_params_add(ast_value *self, ast_value *p)
373 {
374     vec_push(self->expression.params, p);
375 }
376
377 bool ast_value_set_name(ast_value *self, const char *name)
378 {
379     if (self->name)
380         mem_d((void*)self->name);
381     self->name = util_strdup(name);
382     return !!self->name;
383 }
384
385 ast_binary* ast_binary_new(lex_ctx ctx, int op,
386                            ast_expression* left, ast_expression* right)
387 {
388     ast_instantiate(ast_binary, ctx, ast_binary_delete);
389     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binary_codegen);
390
391     self->op = op;
392     self->left = left;
393     self->right = right;
394
395     ast_propagate_effects(self, left);
396     ast_propagate_effects(self, right);
397
398     if (op >= INSTR_EQ_F && op <= INSTR_GT)
399         self->expression.vtype = TYPE_FLOAT;
400     else if (op == INSTR_AND || op == INSTR_OR ||
401              op == INSTR_BITAND || op == INSTR_BITOR)
402         self->expression.vtype = TYPE_FLOAT;
403     else if (op == INSTR_MUL_VF || op == INSTR_MUL_FV)
404         self->expression.vtype = TYPE_VECTOR;
405     else if (op == INSTR_MUL_V)
406         self->expression.vtype = TYPE_FLOAT;
407     else
408         self->expression.vtype = left->expression.vtype;
409
410     return self;
411 }
412
413 void ast_binary_delete(ast_binary *self)
414 {
415     ast_unref(self->left);
416     ast_unref(self->right);
417     ast_expression_delete((ast_expression*)self);
418     mem_d(self);
419 }
420
421 ast_binstore* ast_binstore_new(lex_ctx ctx, int storop, int op,
422                                ast_expression* left, ast_expression* right)
423 {
424     ast_instantiate(ast_binstore, ctx, ast_binstore_delete);
425     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binstore_codegen);
426
427     ast_side_effects(self) = true;
428
429     self->opstore = storop;
430     self->opbin   = op;
431     self->dest    = left;
432     self->source  = right;
433
434     self->keep_dest = false;
435
436     if (!ast_type_adopt(self, left)) {
437         ast_delete(self);
438         return NULL;
439     }
440
441     return self;
442 }
443
444 void ast_binstore_delete(ast_binstore *self)
445 {
446     if (!self->keep_dest)
447         ast_unref(self->dest);
448     ast_unref(self->source);
449     ast_expression_delete((ast_expression*)self);
450     mem_d(self);
451 }
452
453 ast_unary* ast_unary_new(lex_ctx ctx, int op,
454                          ast_expression *expr)
455 {
456     ast_instantiate(ast_unary, ctx, ast_unary_delete);
457     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_unary_codegen);
458
459     self->op = op;
460     self->operand = expr;
461
462     ast_propagate_effects(self, expr);
463
464     if (op >= INSTR_NOT_F && op <= INSTR_NOT_FNC) {
465         self->expression.vtype = TYPE_FLOAT;
466     } else
467         compile_error(ctx, "cannot determine type of unary operation %s", asm_instr[op].m);
468
469     return self;
470 }
471
472 void ast_unary_delete(ast_unary *self)
473 {
474     ast_unref(self->operand);
475     ast_expression_delete((ast_expression*)self);
476     mem_d(self);
477 }
478
479 ast_return* ast_return_new(lex_ctx ctx, ast_expression *expr)
480 {
481     ast_instantiate(ast_return, ctx, ast_return_delete);
482     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_return_codegen);
483
484     self->operand = expr;
485
486     if (expr)
487         ast_propagate_effects(self, expr);
488
489     return self;
490 }
491
492 void ast_return_delete(ast_return *self)
493 {
494     if (self->operand)
495         ast_unref(self->operand);
496     ast_expression_delete((ast_expression*)self);
497     mem_d(self);
498 }
499
500 ast_entfield* ast_entfield_new(lex_ctx ctx, ast_expression *entity, ast_expression *field)
501 {
502     if (field->expression.vtype != TYPE_FIELD) {
503         compile_error(ctx, "ast_entfield_new with expression not of type field");
504         return NULL;
505     }
506     return ast_entfield_new_force(ctx, entity, field, field->expression.next);
507 }
508
509 ast_entfield* ast_entfield_new_force(lex_ctx ctx, ast_expression *entity, ast_expression *field, const ast_expression *outtype)
510 {
511     ast_instantiate(ast_entfield, ctx, ast_entfield_delete);
512
513     if (!outtype) {
514         mem_d(self);
515         /* Error: field has no type... */
516         return NULL;
517     }
518
519     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_entfield_codegen);
520
521     self->entity = entity;
522     self->field  = field;
523     ast_propagate_effects(self, entity);
524     ast_propagate_effects(self, field);
525
526     if (!ast_type_adopt(self, outtype)) {
527         ast_entfield_delete(self);
528         return NULL;
529     }
530
531     return self;
532 }
533
534 void ast_entfield_delete(ast_entfield *self)
535 {
536     ast_unref(self->entity);
537     ast_unref(self->field);
538     ast_expression_delete((ast_expression*)self);
539     mem_d(self);
540 }
541
542 ast_member* ast_member_new(lex_ctx ctx, ast_expression *owner, unsigned int field, const char *name)
543 {
544     ast_instantiate(ast_member, ctx, ast_member_delete);
545     if (field >= 3) {
546         mem_d(self);
547         return NULL;
548     }
549
550     if (owner->expression.vtype != TYPE_VECTOR &&
551         owner->expression.vtype != TYPE_FIELD) {
552         compile_error(ctx, "member-access on an invalid owner of type %s", type_name[owner->expression.vtype]);
553         mem_d(self);
554         return NULL;
555     }
556
557     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_member_codegen);
558     self->expression.node.keep = true; /* keep */
559
560     if (owner->expression.vtype == TYPE_VECTOR) {
561         self->expression.vtype = TYPE_FLOAT;
562         self->expression.next  = NULL;
563     } else {
564         self->expression.vtype = TYPE_FIELD;
565         self->expression.next = ast_shallow_type(ctx, TYPE_FLOAT);
566     }
567
568     self->owner = owner;
569     ast_propagate_effects(self, owner);
570
571     self->field = field;
572     if (name)
573         self->name = util_strdup(name);
574     else
575         self->name = NULL;
576
577     return self;
578 }
579
580 void ast_member_delete(ast_member *self)
581 {
582     /* The owner is always an ast_value, which has .keep=true,
583      * also: ast_members are usually deleted after the owner, thus
584      * this will cause invalid access
585     ast_unref(self->owner);
586      * once we allow (expression).x to access a vector-member, we need
587      * to change this: preferably by creating an alternate ast node for this
588      * purpose that is not garbage-collected.
589     */
590     ast_expression_delete((ast_expression*)self);
591     mem_d(self);
592 }
593
594 ast_array_index* ast_array_index_new(lex_ctx ctx, ast_expression *array, ast_expression *index)
595 {
596     ast_expression *outtype;
597     ast_instantiate(ast_array_index, ctx, ast_array_index_delete);
598
599     outtype = array->expression.next;
600     if (!outtype) {
601         mem_d(self);
602         /* Error: field has no type... */
603         return NULL;
604     }
605
606     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_array_index_codegen);
607
608     self->array = array;
609     self->index = index;
610     ast_propagate_effects(self, array);
611     ast_propagate_effects(self, index);
612
613     if (!ast_type_adopt(self, outtype)) {
614         ast_array_index_delete(self);
615         return NULL;
616     }
617     if (array->expression.vtype == TYPE_FIELD && outtype->expression.vtype == TYPE_ARRAY) {
618         if (self->expression.vtype != TYPE_ARRAY) {
619             compile_error(ast_ctx(self), "array_index node on type");
620             ast_array_index_delete(self);
621             return NULL;
622         }
623         self->array = outtype;
624         self->expression.vtype = TYPE_FIELD;
625     }
626
627     return self;
628 }
629
630 void ast_array_index_delete(ast_array_index *self)
631 {
632     ast_unref(self->array);
633     ast_unref(self->index);
634     ast_expression_delete((ast_expression*)self);
635     mem_d(self);
636 }
637
638 ast_ifthen* ast_ifthen_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
639 {
640     ast_instantiate(ast_ifthen, ctx, ast_ifthen_delete);
641     if (!ontrue && !onfalse) {
642         /* because it is invalid */
643         mem_d(self);
644         return NULL;
645     }
646     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ifthen_codegen);
647
648     self->cond     = cond;
649     self->on_true  = ontrue;
650     self->on_false = onfalse;
651     ast_propagate_effects(self, cond);
652     if (ontrue)
653         ast_propagate_effects(self, ontrue);
654     if (onfalse)
655         ast_propagate_effects(self, onfalse);
656
657     return self;
658 }
659
660 void ast_ifthen_delete(ast_ifthen *self)
661 {
662     ast_unref(self->cond);
663     if (self->on_true)
664         ast_unref(self->on_true);
665     if (self->on_false)
666         ast_unref(self->on_false);
667     ast_expression_delete((ast_expression*)self);
668     mem_d(self);
669 }
670
671 ast_ternary* ast_ternary_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
672 {
673     ast_instantiate(ast_ternary, ctx, ast_ternary_delete);
674     /* This time NEITHER must be NULL */
675     if (!ontrue || !onfalse) {
676         mem_d(self);
677         return NULL;
678     }
679     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ternary_codegen);
680
681     self->cond     = cond;
682     self->on_true  = ontrue;
683     self->on_false = onfalse;
684     ast_propagate_effects(self, cond);
685     ast_propagate_effects(self, ontrue);
686     ast_propagate_effects(self, onfalse);
687
688     if (!ast_type_adopt(self, ontrue)) {
689         ast_ternary_delete(self);
690         return NULL;
691     }
692
693     return self;
694 }
695
696 void ast_ternary_delete(ast_ternary *self)
697 {
698     ast_unref(self->cond);
699     ast_unref(self->on_true);
700     ast_unref(self->on_false);
701     ast_expression_delete((ast_expression*)self);
702     mem_d(self);
703 }
704
705 ast_loop* ast_loop_new(lex_ctx ctx,
706                        ast_expression *initexpr,
707                        ast_expression *precond,
708                        ast_expression *postcond,
709                        ast_expression *increment,
710                        ast_expression *body)
711 {
712     ast_instantiate(ast_loop, ctx, ast_loop_delete);
713     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_loop_codegen);
714
715     self->initexpr  = initexpr;
716     self->precond   = precond;
717     self->postcond  = postcond;
718     self->increment = increment;
719     self->body      = body;
720
721     if (initexpr)
722         ast_propagate_effects(self, initexpr);
723     if (precond)
724         ast_propagate_effects(self, precond);
725     if (postcond)
726         ast_propagate_effects(self, postcond);
727     if (increment)
728         ast_propagate_effects(self, increment);
729     if (body)
730         ast_propagate_effects(self, body);
731
732     return self;
733 }
734
735 void ast_loop_delete(ast_loop *self)
736 {
737     if (self->initexpr)
738         ast_unref(self->initexpr);
739     if (self->precond)
740         ast_unref(self->precond);
741     if (self->postcond)
742         ast_unref(self->postcond);
743     if (self->increment)
744         ast_unref(self->increment);
745     if (self->body)
746         ast_unref(self->body);
747     ast_expression_delete((ast_expression*)self);
748     mem_d(self);
749 }
750
751 ast_breakcont* ast_breakcont_new(lex_ctx ctx, bool iscont)
752 {
753     ast_instantiate(ast_breakcont, ctx, ast_breakcont_delete);
754     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_breakcont_codegen);
755
756     self->is_continue = iscont;
757
758     return self;
759 }
760
761 void ast_breakcont_delete(ast_breakcont *self)
762 {
763     ast_expression_delete((ast_expression*)self);
764     mem_d(self);
765 }
766
767 ast_switch* ast_switch_new(lex_ctx ctx, ast_expression *op)
768 {
769     ast_instantiate(ast_switch, ctx, ast_switch_delete);
770     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_switch_codegen);
771
772     self->operand = op;
773     self->cases   = NULL;
774
775     ast_propagate_effects(self, op);
776
777     return self;
778 }
779
780 void ast_switch_delete(ast_switch *self)
781 {
782     size_t i;
783     ast_unref(self->operand);
784
785     for (i = 0; i < vec_size(self->cases); ++i) {
786         if (self->cases[i].value)
787             ast_unref(self->cases[i].value);
788         ast_unref(self->cases[i].code);
789     }
790     vec_free(self->cases);
791
792     ast_expression_delete((ast_expression*)self);
793     mem_d(self);
794 }
795
796 ast_label* ast_label_new(lex_ctx ctx, const char *name)
797 {
798     ast_instantiate(ast_label, ctx, ast_label_delete);
799     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_label_codegen);
800
801     self->name    = util_strdup(name);
802     self->irblock = NULL;
803     self->gotos   = NULL;
804
805     return self;
806 }
807
808 void ast_label_delete(ast_label *self)
809 {
810     mem_d((void*)self->name);
811     vec_free(self->gotos);
812     ast_expression_delete((ast_expression*)self);
813     mem_d(self);
814 }
815
816 void ast_label_register_goto(ast_label *self, ast_goto *g)
817 {
818     vec_push(self->gotos, g);
819 }
820
821 ast_goto* ast_goto_new(lex_ctx ctx, const char *name)
822 {
823     ast_instantiate(ast_goto, ctx, ast_goto_delete);
824     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_goto_codegen);
825
826     self->name    = util_strdup(name);
827     self->target  = NULL;
828     self->irblock_from = NULL;
829
830     return self;
831 }
832
833 void ast_goto_delete(ast_goto *self)
834 {
835     mem_d((void*)self->name);
836     ast_expression_delete((ast_expression*)self);
837     mem_d(self);
838 }
839
840 void ast_goto_set_label(ast_goto *self, ast_label *label)
841 {
842     self->target = label;
843 }
844
845 ast_call* ast_call_new(lex_ctx ctx,
846                        ast_expression *funcexpr)
847 {
848     ast_instantiate(ast_call, ctx, ast_call_delete);
849     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_call_codegen);
850
851     ast_side_effects(self) = true;
852
853     self->params = NULL;
854     self->func   = funcexpr;
855
856     ast_type_adopt(self, funcexpr->expression.next);
857
858     return self;
859 }
860
861 void ast_call_delete(ast_call *self)
862 {
863     size_t i;
864     for (i = 0; i < vec_size(self->params); ++i)
865         ast_unref(self->params[i]);
866     vec_free(self->params);
867
868     if (self->func)
869         ast_unref(self->func);
870
871     ast_expression_delete((ast_expression*)self);
872     mem_d(self);
873 }
874
875 bool ast_call_check_types(ast_call *self)
876 {
877     size_t i;
878     bool   retval = true;
879     const  ast_expression *func = self->func;
880     size_t count = vec_size(self->params);
881     if (count > vec_size(func->expression.params))
882         count = vec_size(func->expression.params);
883
884     for (i = 0; i < count; ++i) {
885         if (!ast_compare_type(self->params[i], (ast_expression*)(func->expression.params[i]))) {
886             char texp[1024];
887             char tgot[1024];
888             ast_type_to_string(self->params[i], tgot, sizeof(tgot));
889             ast_type_to_string((ast_expression*)func->expression.params[i], texp, sizeof(texp));
890             compile_error(ast_ctx(self), "invalid type for parameter %u in function call: expected %s, got %s",
891                      (unsigned int)(i+1), texp, tgot);
892             /* we don't immediately return */
893             retval = false;
894         }
895     }
896     return retval;
897 }
898
899 ast_store* ast_store_new(lex_ctx ctx, int op,
900                          ast_expression *dest, ast_expression *source)
901 {
902     ast_instantiate(ast_store, ctx, ast_store_delete);
903     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_store_codegen);
904
905     ast_side_effects(self) = true;
906
907     self->op = op;
908     self->dest = dest;
909     self->source = source;
910
911     if (!ast_type_adopt(self, dest)) {
912         ast_delete(self);
913         return NULL;
914     }
915
916     return self;
917 }
918
919 void ast_store_delete(ast_store *self)
920 {
921     ast_unref(self->dest);
922     ast_unref(self->source);
923     ast_expression_delete((ast_expression*)self);
924     mem_d(self);
925 }
926
927 ast_block* ast_block_new(lex_ctx ctx)
928 {
929     ast_instantiate(ast_block, ctx, ast_block_delete);
930     ast_expression_init((ast_expression*)self,
931                         (ast_expression_codegen*)&ast_block_codegen);
932
933     self->locals  = NULL;
934     self->exprs   = NULL;
935     self->collect = NULL;
936
937     return self;
938 }
939
940 bool ast_block_add_expr(ast_block *self, ast_expression *e)
941 {
942     ast_propagate_effects(self, e);
943     vec_push(self->exprs, e);
944     if (self->expression.next) {
945         ast_delete(self->expression.next);
946         self->expression.next = NULL;
947     }
948     if (!ast_type_adopt(self, e)) {
949         compile_error(ast_ctx(self), "internal error: failed to adopt type");
950         return false;
951     }
952     return true;
953 }
954
955 void ast_block_collect(ast_block *self, ast_expression *expr)
956 {
957     vec_push(self->collect, expr);
958     expr->expression.node.keep = true;
959 }
960
961 void ast_block_delete(ast_block *self)
962 {
963     size_t i;
964     for (i = 0; i < vec_size(self->exprs); ++i)
965         ast_unref(self->exprs[i]);
966     vec_free(self->exprs);
967     for (i = 0; i < vec_size(self->locals); ++i)
968         ast_delete(self->locals[i]);
969     vec_free(self->locals);
970     for (i = 0; i < vec_size(self->collect); ++i)
971         ast_delete(self->collect[i]);
972     vec_free(self->collect);
973     ast_expression_delete((ast_expression*)self);
974     mem_d(self);
975 }
976
977 bool ast_block_set_type(ast_block *self, ast_expression *from)
978 {
979     if (self->expression.next)
980         ast_delete(self->expression.next);
981     if (!ast_type_adopt(self, from))
982         return false;
983     return true;
984 }
985
986 ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype)
987 {
988     ast_instantiate(ast_function, ctx, ast_function_delete);
989
990     if (!vtype ||
991         vtype->hasvalue ||
992         vtype->expression.vtype != TYPE_FUNCTION)
993     {
994         compile_error(ast_ctx(self), "internal error: ast_function_new condition %i %i type=%i (probably 2 bodies?)",
995                  (int)!vtype,
996                  (int)vtype->hasvalue,
997                  vtype->expression.vtype);
998         mem_d(self);
999         return NULL;
1000     }
1001
1002     self->vtype  = vtype;
1003     self->name   = name ? util_strdup(name) : NULL;
1004     self->blocks = NULL;
1005
1006     self->labelcount = 0;
1007     self->builtin = 0;
1008
1009     self->ir_func = NULL;
1010     self->curblock = NULL;
1011
1012     self->breakblock    = NULL;
1013     self->continueblock = NULL;
1014
1015     vtype->hasvalue = true;
1016     vtype->constval.vfunc = self;
1017
1018     return self;
1019 }
1020
1021 void ast_function_delete(ast_function *self)
1022 {
1023     size_t i;
1024     if (self->name)
1025         mem_d((void*)self->name);
1026     if (self->vtype) {
1027         /* ast_value_delete(self->vtype); */
1028         self->vtype->hasvalue = false;
1029         self->vtype->constval.vfunc = NULL;
1030         /* We use unref - if it was stored in a global table it is supposed
1031          * to be deleted from *there*
1032          */
1033         ast_unref(self->vtype);
1034     }
1035     for (i = 0; i < vec_size(self->blocks); ++i)
1036         ast_delete(self->blocks[i]);
1037     vec_free(self->blocks);
1038     mem_d(self);
1039 }
1040
1041 const char* ast_function_label(ast_function *self, const char *prefix)
1042 {
1043     size_t id;
1044     size_t len;
1045     char  *from;
1046
1047     if (!opts.dump && !opts.dumpfin)
1048         return NULL;
1049
1050     id  = (self->labelcount++);
1051     len = strlen(prefix);
1052
1053     from = self->labelbuf + sizeof(self->labelbuf)-1;
1054     *from-- = 0;
1055     do {
1056         unsigned int digit = id % 10;
1057         *from = digit + '0';
1058         id /= 10;
1059     } while (id);
1060     memcpy(from - len, prefix, len);
1061     return from - len;
1062 }
1063
1064 /*********************************************************************/
1065 /* AST codegen part
1066  * by convention you must never pass NULL to the 'ir_value **out'
1067  * parameter. If you really don't care about the output, pass a dummy.
1068  * But I can't imagine a pituation where the output is truly unnecessary.
1069  */
1070
1071 void _ast_codegen_output_type(ast_expression_common *self, ir_value *out)
1072 {
1073     if (out->vtype == TYPE_FIELD)
1074         out->fieldtype = self->next->expression.vtype;
1075     if (out->vtype == TYPE_FUNCTION)
1076         out->outtype = self->next->expression.vtype;
1077 }
1078
1079 #define codegen_output_type(a,o) (_ast_codegen_output_type(&((a)->expression),(o)))
1080 #define codegen_output_type_expr(a,o) (_ast_codegen_output_type(a,(o)))
1081
1082 bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out)
1083 {
1084     (void)func;
1085     (void)lvalue;
1086     /* NOTE: This is the codegen for a variable used in an expression.
1087      * It is not the codegen to generate the value. For this purpose,
1088      * ast_local_codegen and ast_global_codegen are to be used before this
1089      * is executed. ast_function_codegen should take care of its locals,
1090      * and the ast-user should take care of ast_global_codegen to be used
1091      * on all the globals.
1092      */
1093     if (!self->ir_v) {
1094         char typename[1024];
1095         ast_type_to_string((ast_expression*)self, typename, sizeof(typename));
1096         compile_error(ast_ctx(self), "ast_value used before generated %s %s", typename, self->name);
1097         return false;
1098     }
1099     *out = self->ir_v;
1100     return true;
1101 }
1102
1103 bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
1104 {
1105     ir_value *v = NULL;
1106
1107     if (self->hasvalue && self->expression.vtype == TYPE_FUNCTION)
1108     {
1109         ir_function *func = ir_builder_create_function(ir, self->name, self->expression.next->expression.vtype);
1110         if (!func)
1111             return false;
1112         func->context = ast_ctx(self);
1113         func->value->context = ast_ctx(self);
1114
1115         self->constval.vfunc->ir_func = func;
1116         self->ir_v = func->value;
1117         /* The function is filled later on ast_function_codegen... */
1118         return true;
1119     }
1120
1121     if (isfield && self->expression.vtype == TYPE_FIELD) {
1122         ast_expression *fieldtype = self->expression.next;
1123
1124         if (self->hasvalue) {
1125             compile_error(ast_ctx(self), "TODO: constant field pointers with value");
1126             goto error;
1127         }
1128
1129         if (fieldtype->expression.vtype == TYPE_ARRAY) {
1130             size_t ai;
1131             char   *name;
1132             size_t  namelen;
1133
1134             ast_expression_common *elemtype;
1135             int                    vtype;
1136             ast_value             *array = (ast_value*)fieldtype;
1137
1138             if (!ast_istype(fieldtype, ast_value)) {
1139                 compile_error(ast_ctx(self), "internal error: ast_value required");
1140                 return false;
1141             }
1142
1143             /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1144             if (!array->expression.count || array->expression.count > opts.max_array_size)
1145                 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)array->expression.count);
1146
1147             elemtype = &array->expression.next->expression;
1148             vtype = elemtype->vtype;
1149
1150             v = ir_builder_create_field(ir, self->name, vtype);
1151             if (!v) {
1152                 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", self->name);
1153                 return false;
1154             }
1155             v->context = ast_ctx(self);
1156             v->unique_life = true;
1157             array->ir_v = self->ir_v = v;
1158
1159             namelen = strlen(self->name);
1160             name    = (char*)mem_a(namelen + 16);
1161             strcpy(name, self->name);
1162
1163             array->ir_values = (ir_value**)mem_a(sizeof(array->ir_values[0]) * array->expression.count);
1164             array->ir_values[0] = v;
1165             for (ai = 1; ai < array->expression.count; ++ai) {
1166                 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1167                 array->ir_values[ai] = ir_builder_create_field(ir, name, vtype);
1168                 if (!array->ir_values[ai]) {
1169                     mem_d(name);
1170                     compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", name);
1171                     return false;
1172                 }
1173                 array->ir_values[ai]->context = ast_ctx(self);
1174                 array->ir_values[ai]->unique_life = true;
1175             }
1176             mem_d(name);
1177         }
1178         else
1179         {
1180             v = ir_builder_create_field(ir, self->name, self->expression.next->expression.vtype);
1181             if (!v)
1182                 return false;
1183             v->context = ast_ctx(self);
1184             self->ir_v = v;
1185         }
1186         return true;
1187     }
1188
1189     if (self->expression.vtype == TYPE_ARRAY) {
1190         size_t ai;
1191         char   *name;
1192         size_t  namelen;
1193
1194         ast_expression_common *elemtype = &self->expression.next->expression;
1195         int vtype = elemtype->vtype;
1196
1197         /* same as with field arrays */
1198         if (!self->expression.count || self->expression.count > opts.max_array_size)
1199             compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1200
1201         v = ir_builder_create_global(ir, self->name, vtype);
1202         if (!v) {
1203             compile_error(ast_ctx(self), "ir_builder_create_global failed `%s`", self->name);
1204             return false;
1205         }
1206         v->context = ast_ctx(self);
1207         v->unique_life = true;
1208
1209         namelen = strlen(self->name);
1210         name    = (char*)mem_a(namelen + 16);
1211         strcpy(name, self->name);
1212
1213         self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1214         self->ir_values[0] = v;
1215         for (ai = 1; ai < self->expression.count; ++ai) {
1216             snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1217             self->ir_values[ai] = ir_builder_create_global(ir, name, vtype);
1218             if (!self->ir_values[ai]) {
1219                 mem_d(name);
1220                 compile_error(ast_ctx(self), "ir_builder_create_global failed `%s`", name);
1221                 return false;
1222             }
1223             self->ir_values[ai]->context = ast_ctx(self);
1224             self->ir_values[ai]->unique_life = true;
1225         }
1226         mem_d(name);
1227     }
1228     else
1229     {
1230         /* Arrays don't do this since there's no "array" value which spans across the
1231          * whole thing.
1232          */
1233         v = ir_builder_create_global(ir, self->name, self->expression.vtype);
1234         if (!v) {
1235             compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", self->name);
1236             return false;
1237         }
1238         codegen_output_type(self, v);
1239         v->context = ast_ctx(self);
1240     }
1241
1242     if (self->hasvalue) {
1243         switch (self->expression.vtype)
1244         {
1245             case TYPE_FLOAT:
1246                 if (!ir_value_set_float(v, self->constval.vfloat))
1247                     goto error;
1248                 break;
1249             case TYPE_VECTOR:
1250                 if (!ir_value_set_vector(v, self->constval.vvec))
1251                     goto error;
1252                 break;
1253             case TYPE_STRING:
1254                 if (!ir_value_set_string(v, self->constval.vstring))
1255                     goto error;
1256                 break;
1257             case TYPE_ARRAY:
1258                 compile_error(ast_ctx(self), "TODO: global constant array");
1259                 break;
1260             case TYPE_FUNCTION:
1261                 compile_error(ast_ctx(self), "global of type function not properly generated");
1262                 goto error;
1263                 /* Cannot generate an IR value for a function,
1264                  * need a pointer pointing to a function rather.
1265                  */
1266             case TYPE_FIELD:
1267                 if (!self->constval.vfield) {
1268                     compile_error(ast_ctx(self), "field constant without vfield set");
1269                     goto error;
1270                 }
1271                 if (!self->constval.vfield->ir_v) {
1272                     compile_error(ast_ctx(self), "field constant generated before its field");
1273                     goto error;
1274                 }
1275                 if (!ir_value_set_field(v, self->constval.vfield->ir_v))
1276                     goto error;
1277                 break;
1278             default:
1279                 compile_error(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1280                 break;
1281         }
1282     }
1283
1284     /* link us to the ir_value */
1285     v->cvq = self->cvq;
1286     self->ir_v = v;
1287     return true;
1288
1289 error: /* clean up */
1290     ir_value_delete(v);
1291     return false;
1292 }
1293
1294 bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
1295 {
1296     ir_value *v = NULL;
1297     if (self->hasvalue && self->expression.vtype == TYPE_FUNCTION)
1298     {
1299         /* Do we allow local functions? I think not...
1300          * this is NOT a function pointer atm.
1301          */
1302         return false;
1303     }
1304
1305     if (self->expression.vtype == TYPE_ARRAY) {
1306         size_t ai;
1307         char   *name;
1308         size_t  namelen;
1309
1310         ast_expression_common *elemtype = &self->expression.next->expression;
1311         int vtype = elemtype->vtype;
1312
1313         if (param) {
1314             compile_error(ast_ctx(self), "array-parameters are not supported");
1315             return false;
1316         }
1317
1318         /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1319         if (!self->expression.count || self->expression.count > opts.max_array_size) {
1320             compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1321         }
1322
1323         self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1324         if (!self->ir_values) {
1325             compile_error(ast_ctx(self), "failed to allocate array values");
1326             return false;
1327         }
1328
1329         v = ir_function_create_local(func, self->name, vtype, param);
1330         if (!v) {
1331             compile_error(ast_ctx(self), "ir_function_create_local failed");
1332             return false;
1333         }
1334         v->context = ast_ctx(self);
1335         v->unique_life = true;
1336
1337         namelen = strlen(self->name);
1338         name    = (char*)mem_a(namelen + 16);
1339         strcpy(name, self->name);
1340
1341         self->ir_values[0] = v;
1342         for (ai = 1; ai < self->expression.count; ++ai) {
1343             snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1344             self->ir_values[ai] = ir_function_create_local(func, name, vtype, param);
1345             if (!self->ir_values[ai]) {
1346                 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", name);
1347                 return false;
1348             }
1349             self->ir_values[ai]->context = ast_ctx(self);
1350             self->ir_values[ai]->unique_life = true;
1351         }
1352     }
1353     else
1354     {
1355         v = ir_function_create_local(func, self->name, self->expression.vtype, param);
1356         if (!v)
1357             return false;
1358         codegen_output_type(self, v);
1359         v->context = ast_ctx(self);
1360     }
1361
1362     /* A constant local... hmmm...
1363      * I suppose the IR will have to deal with this
1364      */
1365     if (self->hasvalue) {
1366         switch (self->expression.vtype)
1367         {
1368             case TYPE_FLOAT:
1369                 if (!ir_value_set_float(v, self->constval.vfloat))
1370                     goto error;
1371                 break;
1372             case TYPE_VECTOR:
1373                 if (!ir_value_set_vector(v, self->constval.vvec))
1374                     goto error;
1375                 break;
1376             case TYPE_STRING:
1377                 if (!ir_value_set_string(v, self->constval.vstring))
1378                     goto error;
1379                 break;
1380             default:
1381                 compile_error(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1382                 break;
1383         }
1384     }
1385
1386     /* link us to the ir_value */
1387     v->cvq = self->cvq;
1388     self->ir_v = v;
1389
1390     if (!ast_generate_accessors(self, func->owner))
1391         return false;
1392     return true;
1393
1394 error: /* clean up */
1395     ir_value_delete(v);
1396     return false;
1397 }
1398
1399 bool ast_generate_accessors(ast_value *self, ir_builder *ir)
1400 {
1401     size_t i;
1402     bool warn = OPTS_WARN(WARN_USED_UNINITIALIZED);
1403     if (!self->setter || !self->getter)
1404         return true;
1405     for (i = 0; i < self->expression.count; ++i) {
1406         if (!self->ir_values) {
1407             compile_error(ast_ctx(self), "internal error: no array values generated for `%s`", self->name);
1408             return false;
1409         }
1410         if (!self->ir_values[i]) {
1411             compile_error(ast_ctx(self), "internal error: not all array values have been generated for `%s`", self->name);
1412             return false;
1413         }
1414         if (self->ir_values[i]->life) {
1415             compile_error(ast_ctx(self), "internal error: function containing `%s` already generated", self->name);
1416             return false;
1417         }
1418     }
1419
1420     options_set(opts.warn, WARN_USED_UNINITIALIZED, false);
1421     if (self->setter) {
1422         if (!ast_global_codegen  (self->setter, ir, false) ||
1423             !ast_function_codegen(self->setter->constval.vfunc, ir) ||
1424             !ir_function_finalize(self->setter->constval.vfunc->ir_func))
1425         {
1426             compile_error(ast_ctx(self), "internal error: failed to generate setter for `%s`", self->name);
1427             options_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1428             return false;
1429         }
1430     }
1431     if (self->getter) {
1432         if (!ast_global_codegen  (self->getter, ir, false) ||
1433             !ast_function_codegen(self->getter->constval.vfunc, ir) ||
1434             !ir_function_finalize(self->getter->constval.vfunc->ir_func))
1435         {
1436             compile_error(ast_ctx(self), "internal error: failed to generate getter for `%s`", self->name);
1437             options_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1438             return false;
1439         }
1440     }
1441     for (i = 0; i < self->expression.count; ++i) {
1442         vec_free(self->ir_values[i]->life);
1443     }
1444     options_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1445     return true;
1446 }
1447
1448 bool ast_function_codegen(ast_function *self, ir_builder *ir)
1449 {
1450     ir_function *irf;
1451     ir_value    *dummy;
1452     ast_expression_common *ec;
1453     size_t    i;
1454
1455     (void)ir;
1456
1457     irf = self->ir_func;
1458     if (!irf) {
1459         compile_error(ast_ctx(self), "ast_function's related ast_value was not generated yet");
1460         return false;
1461     }
1462
1463     /* fill the parameter list */
1464     ec = &self->vtype->expression;
1465     for (i = 0; i < vec_size(ec->params); ++i)
1466     {
1467         if (ec->params[i]->expression.vtype == TYPE_FIELD)
1468             vec_push(irf->params, ec->params[i]->expression.next->expression.vtype);
1469         else
1470             vec_push(irf->params, ec->params[i]->expression.vtype);
1471         if (!self->builtin) {
1472             if (!ast_local_codegen(ec->params[i], self->ir_func, true))
1473                 return false;
1474         }
1475     }
1476
1477     if (self->builtin) {
1478         irf->builtin = self->builtin;
1479         return true;
1480     }
1481
1482     if (!vec_size(self->blocks)) {
1483         compile_error(ast_ctx(self), "function `%s` has no body", self->name);
1484         return false;
1485     }
1486
1487     self->curblock = ir_function_create_block(ast_ctx(self), irf, "entry");
1488     if (!self->curblock) {
1489         compile_error(ast_ctx(self), "failed to allocate entry block for `%s`", self->name);
1490         return false;
1491     }
1492
1493     for (i = 0; i < vec_size(self->blocks); ++i) {
1494         ast_expression_codegen *gen = self->blocks[i]->expression.codegen;
1495         if (!(*gen)((ast_expression*)self->blocks[i], self, false, &dummy))
1496             return false;
1497     }
1498
1499     /* TODO: check return types */
1500     if (!self->curblock->is_return)
1501     {
1502         if (!self->vtype->expression.next ||
1503             self->vtype->expression.next->expression.vtype == TYPE_VOID)
1504         {
1505             return ir_block_create_return(self->curblock, ast_ctx(self), NULL);
1506         }
1507         else if (vec_size(self->curblock->entries))
1508         {
1509             /* error("missing return"); */
1510             if (compile_warning(ast_ctx(self), WARN_MISSING_RETURN_VALUES,
1511                                 "control reaches end of non-void function (`%s`) via %s",
1512                                 self->name, self->curblock->label))
1513             {
1514                 return false;
1515             }
1516             return ir_block_create_return(self->curblock, ast_ctx(self), NULL);
1517         }
1518     }
1519     return true;
1520 }
1521
1522 /* Note, you will not see ast_block_codegen generate ir_blocks.
1523  * To the AST and the IR, blocks are 2 different things.
1524  * In the AST it represents a block of code, usually enclosed in
1525  * curly braces {...}.
1526  * While in the IR it represents a block in terms of control-flow.
1527  */
1528 bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_value **out)
1529 {
1530     size_t i;
1531
1532     /* We don't use this
1533      * Note: an ast-representation using the comma-operator
1534      * of the form: (a, b, c) = x should not assign to c...
1535      */
1536     if (lvalue) {
1537         compile_error(ast_ctx(self), "not an l-value (code-block)");
1538         return false;
1539     }
1540
1541     if (self->expression.outr) {
1542         *out = self->expression.outr;
1543         return true;
1544     }
1545
1546     /* output is NULL at first, we'll have each expression
1547      * assign to out output, thus, a comma-operator represention
1548      * using an ast_block will return the last generated value,
1549      * so: (b, c) + a  executed both b and c, and returns c,
1550      * which is then added to a.
1551      */
1552     *out = NULL;
1553
1554     /* generate locals */
1555     for (i = 0; i < vec_size(self->locals); ++i)
1556     {
1557         if (!ast_local_codegen(self->locals[i], func->ir_func, false)) {
1558             if (opts.debug)
1559                 compile_error(ast_ctx(self), "failed to generate local `%s`", self->locals[i]->name);
1560             return false;
1561         }
1562     }
1563
1564     for (i = 0; i < vec_size(self->exprs); ++i)
1565     {
1566         ast_expression_codegen *gen = self->exprs[i]->expression.codegen;
1567         if (func->curblock->final && !ast_istype(self->exprs[i], ast_label)) {
1568             compile_error(ast_ctx(self->exprs[i]), "unreachable statement");
1569             return false;
1570         }
1571         if (!(*gen)(self->exprs[i], func, false, out))
1572             return false;
1573     }
1574
1575     self->expression.outr = *out;
1576
1577     return true;
1578 }
1579
1580 bool ast_store_codegen(ast_store *self, ast_function *func, bool lvalue, ir_value **out)
1581 {
1582     ast_expression_codegen *cgen;
1583     ir_value *left  = NULL;
1584     ir_value *right = NULL;
1585
1586     ast_value       *arr;
1587     ast_value       *idx = 0;
1588     ast_array_index *ai = NULL;
1589
1590     if (lvalue && self->expression.outl) {
1591         *out = self->expression.outl;
1592         return true;
1593     }
1594
1595     if (!lvalue && self->expression.outr) {
1596         *out = self->expression.outr;
1597         return true;
1598     }
1599
1600     if (ast_istype(self->dest, ast_array_index))
1601     {
1602
1603         ai = (ast_array_index*)self->dest;
1604         idx = (ast_value*)ai->index;
1605
1606         if (ast_istype(ai->index, ast_value) && idx->hasvalue && idx->cvq == CV_CONST)
1607             ai = NULL;
1608     }
1609
1610     if (ai) {
1611         /* we need to call the setter */
1612         ir_value  *iridx, *funval;
1613         ir_instr  *call;
1614
1615         if (lvalue) {
1616             compile_error(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1617             return false;
1618         }
1619
1620         arr = (ast_value*)ai->array;
1621         if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1622             compile_error(ast_ctx(self), "value has no setter (%s)", arr->name);
1623             return false;
1624         }
1625
1626         cgen = idx->expression.codegen;
1627         if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1628             return false;
1629
1630         cgen = arr->setter->expression.codegen;
1631         if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
1632             return false;
1633
1634         cgen = self->source->expression.codegen;
1635         if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1636             return false;
1637
1638         call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "store"), funval);
1639         if (!call)
1640             return false;
1641         ir_call_param(call, iridx);
1642         ir_call_param(call, right);
1643         self->expression.outr = right;
1644     }
1645     else
1646     {
1647         /* regular code */
1648
1649         cgen = self->dest->expression.codegen;
1650         /* lvalue! */
1651         if (!(*cgen)((ast_expression*)(self->dest), func, true, &left))
1652             return false;
1653         self->expression.outl = left;
1654
1655         cgen = self->source->expression.codegen;
1656         /* rvalue! */
1657         if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1658             return false;
1659
1660         if (!ir_block_create_store_op(func->curblock, ast_ctx(self), self->op, left, right))
1661             return false;
1662         self->expression.outr = right;
1663     }
1664
1665     /* Theoretically, an assinment returns its left side as an
1666      * lvalue, if we don't need an lvalue though, we return
1667      * the right side as an rvalue, otherwise we have to
1668      * somehow know whether or not we need to dereference the pointer
1669      * on the left side - that is: OP_LOAD if it was an address.
1670      * Also: in original QC we cannot OP_LOADP *anyway*.
1671      */
1672     *out = (lvalue ? left : right);
1673
1674     return true;
1675 }
1676
1677 bool ast_binary_codegen(ast_binary *self, ast_function *func, bool lvalue, ir_value **out)
1678 {
1679     ast_expression_codegen *cgen;
1680     ir_value *left, *right;
1681
1682     /* A binary operation cannot yield an l-value */
1683     if (lvalue) {
1684         compile_error(ast_ctx(self), "not an l-value (binop)");
1685         return false;
1686     }
1687
1688     if (self->expression.outr) {
1689         *out = self->expression.outr;
1690         return true;
1691     }
1692
1693     if (OPTS_FLAG(SHORT_LOGIC) &&
1694         (self->op == INSTR_AND || self->op == INSTR_OR))
1695     {
1696         /* short circuit evaluation */
1697         ir_block *other, *merge;
1698         ir_block *from_left, *from_right;
1699         ir_instr *phi;
1700         size_t    merge_id;
1701         uint16_t  notop;
1702
1703         /* Note about casting to true boolean values:
1704          * We use a single NOT for sub expressions, and an
1705          * overall NOT at the end, and for that purpose swap
1706          * all the jump conditions in order for the NOT to get
1707          * doubled.
1708          * ie: (a && b) usually becomes (!!a ? !!b : !!a)
1709          * but we translate this to (!(!a ? !a : !b))
1710          */
1711
1712         merge_id = vec_size(func->ir_func->blocks);
1713         merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "sce_merge"));
1714
1715         cgen = self->left->expression.codegen;
1716         if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1717             return false;
1718         if (!OPTS_FLAG(PERL_LOGIC)) {
1719             notop = type_not_instr[left->vtype];
1720             if (notop == AINSTR_END) {
1721                 compile_error(ast_ctx(self), "don't know how to cast to bool...");
1722                 return false;
1723             }
1724             left = ir_block_create_unary(func->curblock, ast_ctx(self),
1725                                          ast_function_label(func, "sce_not"),
1726                                          notop,
1727                                          left);
1728         }
1729         from_left = func->curblock;
1730
1731         other = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "sce_other"));
1732         if ( !(self->op == INSTR_OR) != !OPTS_FLAG(PERL_LOGIC) ) {
1733             if (!ir_block_create_if(func->curblock, ast_ctx(self), left, other, merge))
1734                 return false;
1735         } else {
1736             if (!ir_block_create_if(func->curblock, ast_ctx(self), left, merge, other))
1737                 return false;
1738         }
1739         /* use the likely flag */
1740         vec_last(func->curblock->instr)->likely = true;
1741
1742         func->curblock = other;
1743         cgen = self->right->expression.codegen;
1744         if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1745             return false;
1746         if (!OPTS_FLAG(PERL_LOGIC)) {
1747             notop = type_not_instr[right->vtype];
1748             if (notop == AINSTR_END) {
1749                 compile_error(ast_ctx(self), "don't know how to cast to bool...");
1750                 return false;
1751             }
1752             right = ir_block_create_unary(func->curblock, ast_ctx(self),
1753                                           ast_function_label(func, "sce_not"),
1754                                           notop,
1755                                           right);
1756         }
1757         from_right = func->curblock;
1758
1759         if (!ir_block_create_jump(func->curblock, ast_ctx(self), merge))
1760             return false;
1761
1762         vec_remove(func->ir_func->blocks, merge_id, 1);
1763         vec_push(func->ir_func->blocks, merge);
1764
1765         func->curblock = merge;
1766         phi = ir_block_create_phi(func->curblock, ast_ctx(self), ast_function_label(func, "sce_value"), TYPE_FLOAT);
1767         ir_phi_add(phi, from_left, left);
1768         ir_phi_add(phi, from_right, right);
1769         *out = ir_phi_value(phi);
1770         if (!OPTS_FLAG(PERL_LOGIC)) {
1771             notop = type_not_instr[(*out)->vtype];
1772             if (notop == AINSTR_END) {
1773                 compile_error(ast_ctx(self), "don't know how to cast to bool...");
1774                 return false;
1775             }
1776             *out = ir_block_create_unary(func->curblock, ast_ctx(self),
1777                                          ast_function_label(func, "sce_final_not"),
1778                                          notop,
1779                                          *out);
1780         }
1781         if (!*out)
1782             return false;
1783         self->expression.outr = *out;
1784         return true;
1785     }
1786
1787     cgen = self->left->expression.codegen;
1788     if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1789         return false;
1790
1791     cgen = self->right->expression.codegen;
1792     if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1793         return false;
1794
1795     *out = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "bin"),
1796                                  self->op, left, right);
1797     if (!*out)
1798         return false;
1799     self->expression.outr = *out;
1800
1801     return true;
1802 }
1803
1804 bool ast_binstore_codegen(ast_binstore *self, ast_function *func, bool lvalue, ir_value **out)
1805 {
1806     ast_expression_codegen *cgen;
1807     ir_value *leftl = NULL, *leftr, *right, *bin;
1808
1809     ast_value       *arr;
1810     ast_value       *idx = 0;
1811     ast_array_index *ai = NULL;
1812     ir_value        *iridx = NULL;
1813
1814     if (lvalue && self->expression.outl) {
1815         *out = self->expression.outl;
1816         return true;
1817     }
1818
1819     if (!lvalue && self->expression.outr) {
1820         *out = self->expression.outr;
1821         return true;
1822     }
1823
1824     if (ast_istype(self->dest, ast_array_index))
1825     {
1826
1827         ai = (ast_array_index*)self->dest;
1828         idx = (ast_value*)ai->index;
1829
1830         if (ast_istype(ai->index, ast_value) && idx->hasvalue && idx->cvq == CV_CONST)
1831             ai = NULL;
1832     }
1833
1834     /* for a binstore we need both an lvalue and an rvalue for the left side */
1835     /* rvalue of destination! */
1836     if (ai) {
1837         cgen = idx->expression.codegen;
1838         if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1839             return false;
1840     }
1841     cgen = self->dest->expression.codegen;
1842     if (!(*cgen)((ast_expression*)(self->dest), func, false, &leftr))
1843         return false;
1844
1845     /* source as rvalue only */
1846     cgen = self->source->expression.codegen;
1847     if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1848         return false;
1849
1850     /* now the binary */
1851     bin = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "binst"),
1852                                 self->opbin, leftr, right);
1853     self->expression.outr = bin;
1854
1855
1856     if (ai) {
1857         /* we need to call the setter */
1858         ir_value  *funval;
1859         ir_instr  *call;
1860
1861         if (lvalue) {
1862             compile_error(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1863             return false;
1864         }
1865
1866         arr = (ast_value*)ai->array;
1867         if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1868             compile_error(ast_ctx(self), "value has no setter (%s)", arr->name);
1869             return false;
1870         }
1871
1872         cgen = arr->setter->expression.codegen;
1873         if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
1874             return false;
1875
1876         call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "store"), funval);
1877         if (!call)
1878             return false;
1879         ir_call_param(call, iridx);
1880         ir_call_param(call, bin);
1881         self->expression.outr = bin;
1882     } else {
1883         /* now store them */
1884         cgen = self->dest->expression.codegen;
1885         /* lvalue of destination */
1886         if (!(*cgen)((ast_expression*)(self->dest), func, true, &leftl))
1887             return false;
1888         self->expression.outl = leftl;
1889
1890         if (!ir_block_create_store_op(func->curblock, ast_ctx(self), self->opstore, leftl, bin))
1891             return false;
1892         self->expression.outr = bin;
1893     }
1894
1895     /* Theoretically, an assinment returns its left side as an
1896      * lvalue, if we don't need an lvalue though, we return
1897      * the right side as an rvalue, otherwise we have to
1898      * somehow know whether or not we need to dereference the pointer
1899      * on the left side - that is: OP_LOAD if it was an address.
1900      * Also: in original QC we cannot OP_LOADP *anyway*.
1901      */
1902     *out = (lvalue ? leftl : bin);
1903
1904     return true;
1905 }
1906
1907 bool ast_unary_codegen(ast_unary *self, ast_function *func, bool lvalue, ir_value **out)
1908 {
1909     ast_expression_codegen *cgen;
1910     ir_value *operand;
1911
1912     /* An unary operation cannot yield an l-value */
1913     if (lvalue) {
1914         compile_error(ast_ctx(self), "not an l-value (binop)");
1915         return false;
1916     }
1917
1918     if (self->expression.outr) {
1919         *out = self->expression.outr;
1920         return true;
1921     }
1922
1923     cgen = self->operand->expression.codegen;
1924     /* lvalue! */
1925     if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
1926         return false;
1927
1928     *out = ir_block_create_unary(func->curblock, ast_ctx(self), ast_function_label(func, "unary"),
1929                                  self->op, operand);
1930     if (!*out)
1931         return false;
1932     self->expression.outr = *out;
1933
1934     return true;
1935 }
1936
1937 bool ast_return_codegen(ast_return *self, ast_function *func, bool lvalue, ir_value **out)
1938 {
1939     ast_expression_codegen *cgen;
1940     ir_value *operand;
1941
1942     *out = NULL;
1943
1944     /* In the context of a return operation, we don't actually return
1945      * anything...
1946      */
1947     if (lvalue) {
1948         compile_error(ast_ctx(self), "return-expression is not an l-value");
1949         return false;
1950     }
1951
1952     if (self->expression.outr) {
1953         compile_error(ast_ctx(self), "internal error: ast_return cannot be reused, it bears no result!");
1954         return false;
1955     }
1956     self->expression.outr = (ir_value*)1;
1957
1958     if (self->operand) {
1959         cgen = self->operand->expression.codegen;
1960         /* lvalue! */
1961         if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
1962             return false;
1963
1964         if (!ir_block_create_return(func->curblock, ast_ctx(self), operand))
1965             return false;
1966     } else {
1967         if (!ir_block_create_return(func->curblock, ast_ctx(self), NULL))
1968             return false;
1969     }
1970
1971     return true;
1972 }
1973
1974 bool ast_entfield_codegen(ast_entfield *self, ast_function *func, bool lvalue, ir_value **out)
1975 {
1976     ast_expression_codegen *cgen;
1977     ir_value *ent, *field;
1978
1979     /* This function needs to take the 'lvalue' flag into account!
1980      * As lvalue we provide a field-pointer, as rvalue we provide the
1981      * value in a temp.
1982      */
1983
1984     if (lvalue && self->expression.outl) {
1985         *out = self->expression.outl;
1986         return true;
1987     }
1988
1989     if (!lvalue && self->expression.outr) {
1990         *out = self->expression.outr;
1991         return true;
1992     }
1993
1994     cgen = self->entity->expression.codegen;
1995     if (!(*cgen)((ast_expression*)(self->entity), func, false, &ent))
1996         return false;
1997
1998     cgen = self->field->expression.codegen;
1999     if (!(*cgen)((ast_expression*)(self->field), func, false, &field))
2000         return false;
2001
2002     if (lvalue) {
2003         /* address! */
2004         *out = ir_block_create_fieldaddress(func->curblock, ast_ctx(self), ast_function_label(func, "efa"),
2005                                             ent, field);
2006     } else {
2007         *out = ir_block_create_load_from_ent(func->curblock, ast_ctx(self), ast_function_label(func, "efv"),
2008                                              ent, field, self->expression.vtype);
2009         /* Done AFTER error checking: 
2010         codegen_output_type(self, *out);
2011         */
2012     }
2013     if (!*out) {
2014         compile_error(ast_ctx(self), "failed to create %s instruction (output type %s)",
2015                  (lvalue ? "ADDRESS" : "FIELD"),
2016                  type_name[self->expression.vtype]);
2017         return false;
2018     }
2019     if (!lvalue)
2020         codegen_output_type(self, *out);
2021
2022     if (lvalue)
2023         self->expression.outl = *out;
2024     else
2025         self->expression.outr = *out;
2026
2027     /* Hm that should be it... */
2028     return true;
2029 }
2030
2031 bool ast_member_codegen(ast_member *self, ast_function *func, bool lvalue, ir_value **out)
2032 {
2033     ast_expression_codegen *cgen;
2034     ir_value *vec;
2035
2036     /* in QC this is always an lvalue */
2037     (void)lvalue;
2038     if (self->expression.outl) {
2039         *out = self->expression.outl;
2040         return true;
2041     }
2042
2043     cgen = self->owner->expression.codegen;
2044     if (!(*cgen)((ast_expression*)(self->owner), func, true, &vec))
2045         return false;
2046
2047     if (vec->vtype != TYPE_VECTOR &&
2048         !(vec->vtype == TYPE_FIELD && self->owner->expression.next->expression.vtype == TYPE_VECTOR))
2049     {
2050         return false;
2051     }
2052
2053     *out = ir_value_vector_member(vec, self->field);
2054     self->expression.outl = *out;
2055
2056     return (*out != NULL);
2057 }
2058
2059 bool ast_array_index_codegen(ast_array_index *self, ast_function *func, bool lvalue, ir_value **out)
2060 {
2061     ast_value *arr;
2062     ast_value *idx;
2063
2064     if (!lvalue && self->expression.outr) {
2065         *out = self->expression.outr;
2066     }
2067     if (lvalue && self->expression.outl) {
2068         *out = self->expression.outl;
2069     }
2070
2071     if (!ast_istype(self->array, ast_value)) {
2072         compile_error(ast_ctx(self), "array indexing this way is not supported");
2073         /* note this would actually be pointer indexing because the left side is
2074          * not an actual array but (hopefully) an indexable expression.
2075          * Once we get integer arithmetic, and GADDRESS/GSTORE/GLOAD instruction
2076          * support this path will be filled.
2077          */
2078         return false;
2079     }
2080
2081     arr = (ast_value*)self->array;
2082     idx = (ast_value*)self->index;
2083
2084     if (!ast_istype(self->index, ast_value) || !idx->hasvalue || idx->cvq != CV_CONST) {
2085         /* Time to use accessor functions */
2086         ast_expression_codegen *cgen;
2087         ir_value               *iridx, *funval;
2088         ir_instr               *call;
2089
2090         if (lvalue) {
2091             compile_error(ast_ctx(self), "(.2) array indexing here needs a compile-time constant");
2092             return false;
2093         }
2094
2095         if (!arr->getter) {
2096             compile_error(ast_ctx(self), "value has no getter, don't know how to index it");
2097             return false;
2098         }
2099
2100         cgen = self->index->expression.codegen;
2101         if (!(*cgen)((ast_expression*)(self->index), func, false, &iridx))
2102             return false;
2103
2104         cgen = arr->getter->expression.codegen;
2105         if (!(*cgen)((ast_expression*)(arr->getter), func, true, &funval))
2106             return false;
2107
2108         call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "fetch"), funval);
2109         if (!call)
2110             return false;
2111         ir_call_param(call, iridx);
2112
2113         *out = ir_call_value(call);
2114         self->expression.outr = *out;
2115         return true;
2116     }
2117
2118     if (idx->expression.vtype == TYPE_FLOAT) {
2119         unsigned int arridx = idx->constval.vfloat;
2120         if (arridx >= self->array->expression.count)
2121         {
2122             compile_error(ast_ctx(self), "array index out of bounds: %i", arridx);
2123             return false;
2124         }
2125         *out = arr->ir_values[arridx];
2126     }
2127     else if (idx->expression.vtype == TYPE_INTEGER) {
2128         unsigned int arridx = idx->constval.vint;
2129         if (arridx >= self->array->expression.count)
2130         {
2131             compile_error(ast_ctx(self), "array index out of bounds: %i", arridx);
2132             return false;
2133         }
2134         *out = arr->ir_values[arridx];
2135     }
2136     else {
2137         compile_error(ast_ctx(self), "array indexing here needs an integer constant");
2138         return false;
2139     }
2140     return true;
2141 }
2142
2143 bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_value **out)
2144 {
2145     ast_expression_codegen *cgen;
2146
2147     ir_value *condval;
2148     ir_value *dummy;
2149
2150     ir_block *cond = func->curblock;
2151     ir_block *ontrue;
2152     ir_block *onfalse;
2153     ir_block *ontrue_endblock = NULL;
2154     ir_block *onfalse_endblock = NULL;
2155     ir_block *merge = NULL;
2156
2157     /* We don't output any value, thus also don't care about r/lvalue */
2158     (void)out;
2159     (void)lvalue;
2160
2161     if (self->expression.outr) {
2162         compile_error(ast_ctx(self), "internal error: ast_ifthen cannot be reused, it bears no result!");
2163         return false;
2164     }
2165     self->expression.outr = (ir_value*)1;
2166
2167     /* generate the condition */
2168     cgen = self->cond->expression.codegen;
2169     if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2170         return false;
2171     /* update the block which will get the jump - because short-logic or ternaries may have changed this */
2172     cond = func->curblock;
2173
2174     /* on-true path */
2175
2176     if (self->on_true) {
2177         /* create on-true block */
2178         ontrue = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "ontrue"));
2179         if (!ontrue)
2180             return false;
2181
2182         /* enter the block */
2183         func->curblock = ontrue;
2184
2185         /* generate */
2186         cgen = self->on_true->expression.codegen;
2187         if (!(*cgen)((ast_expression*)(self->on_true), func, false, &dummy))
2188             return false;
2189
2190         /* we now need to work from the current endpoint */
2191         ontrue_endblock = func->curblock;
2192     } else
2193         ontrue = NULL;
2194
2195     /* on-false path */
2196     if (self->on_false) {
2197         /* create on-false block */
2198         onfalse = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "onfalse"));
2199         if (!onfalse)
2200             return false;
2201
2202         /* enter the block */
2203         func->curblock = onfalse;
2204
2205         /* generate */
2206         cgen = self->on_false->expression.codegen;
2207         if (!(*cgen)((ast_expression*)(self->on_false), func, false, &dummy))
2208             return false;
2209
2210         /* we now need to work from the current endpoint */
2211         onfalse_endblock = func->curblock;
2212     } else
2213         onfalse = NULL;
2214
2215     /* Merge block were they all merge in to */
2216     if (!ontrue || !onfalse || !ontrue_endblock->final || !onfalse_endblock->final)
2217     {
2218         merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "endif"));
2219         if (!merge)
2220             return false;
2221         /* add jumps ot the merge block */
2222         if (ontrue && !ontrue_endblock->final && !ir_block_create_jump(ontrue_endblock, ast_ctx(self), merge))
2223             return false;
2224         if (onfalse && !onfalse_endblock->final && !ir_block_create_jump(onfalse_endblock, ast_ctx(self), merge))
2225             return false;
2226
2227         /* Now enter the merge block */
2228         func->curblock = merge;
2229     }
2230
2231     /* we create the if here, that way all blocks are ordered :)
2232      */
2233     if (!ir_block_create_if(cond, ast_ctx(self), condval,
2234                             (ontrue  ? ontrue  : merge),
2235                             (onfalse ? onfalse : merge)))
2236     {
2237         return false;
2238     }
2239
2240     return true;
2241 }
2242
2243 bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_value **out)
2244 {
2245     ast_expression_codegen *cgen;
2246
2247     ir_value *condval;
2248     ir_value *trueval, *falseval;
2249     ir_instr *phi;
2250
2251     ir_block *cond = func->curblock;
2252     ir_block *cond_out = NULL;
2253     ir_block *ontrue, *ontrue_out = NULL;
2254     ir_block *onfalse, *onfalse_out = NULL;
2255     ir_block *merge;
2256
2257     /* Ternary can never create an lvalue... */
2258     if (lvalue)
2259         return false;
2260
2261     /* In theory it shouldn't be possible to pass through a node twice, but
2262      * in case we add any kind of optimization pass for the AST itself, it
2263      * may still happen, thus we remember a created ir_value and simply return one
2264      * if it already exists.
2265      */
2266     if (self->expression.outr) {
2267         *out = self->expression.outr;
2268         return true;
2269     }
2270
2271     /* In the following, contraty to ast_ifthen, we assume both paths exist. */
2272
2273     /* generate the condition */
2274     func->curblock = cond;
2275     cgen = self->cond->expression.codegen;
2276     if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2277         return false;
2278     cond_out = func->curblock;
2279
2280     /* create on-true block */
2281     ontrue = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_T"));
2282     if (!ontrue)
2283         return false;
2284     else
2285     {
2286         /* enter the block */
2287         func->curblock = ontrue;
2288
2289         /* generate */
2290         cgen = self->on_true->expression.codegen;
2291         if (!(*cgen)((ast_expression*)(self->on_true), func, false, &trueval))
2292             return false;
2293
2294         ontrue_out = func->curblock;
2295     }
2296
2297     /* create on-false block */
2298     onfalse = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_F"));
2299     if (!onfalse)
2300         return false;
2301     else
2302     {
2303         /* enter the block */
2304         func->curblock = onfalse;
2305
2306         /* generate */
2307         cgen = self->on_false->expression.codegen;
2308         if (!(*cgen)((ast_expression*)(self->on_false), func, false, &falseval))
2309             return false;
2310
2311         onfalse_out = func->curblock;
2312     }
2313
2314     /* create merge block */
2315     merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_out"));
2316     if (!merge)
2317         return false;
2318     /* jump to merge block */
2319     if (!ir_block_create_jump(ontrue_out, ast_ctx(self), merge))
2320         return false;
2321     if (!ir_block_create_jump(onfalse_out, ast_ctx(self), merge))
2322         return false;
2323
2324     /* create if instruction */
2325     if (!ir_block_create_if(cond_out, ast_ctx(self), condval, ontrue, onfalse))
2326         return false;
2327
2328     /* Now enter the merge block */
2329     func->curblock = merge;
2330
2331     /* Here, now, we need a PHI node
2332      * but first some sanity checking...
2333      */
2334     if (trueval->vtype != falseval->vtype) {
2335         /* error("ternary with different types on the two sides"); */
2336         return false;
2337     }
2338
2339     /* create PHI */
2340     phi = ir_block_create_phi(merge, ast_ctx(self), ast_function_label(func, "phi"), trueval->vtype);
2341     if (!phi)
2342         return false;
2343     ir_phi_add(phi, ontrue_out,  trueval);
2344     ir_phi_add(phi, onfalse_out, falseval);
2345
2346     self->expression.outr = ir_phi_value(phi);
2347     *out = self->expression.outr;
2348
2349     codegen_output_type(self, *out);
2350
2351     return true;
2352 }
2353
2354 bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value **out)
2355 {
2356     ast_expression_codegen *cgen;
2357
2358     ir_value *dummy      = NULL;
2359     ir_value *precond    = NULL;
2360     ir_value *postcond   = NULL;
2361
2362     /* Since we insert some jumps "late" so we have blocks
2363      * ordered "nicely", we need to keep track of the actual end-blocks
2364      * of expressions to add the jumps to.
2365      */
2366     ir_block *bbody      = NULL, *end_bbody      = NULL;
2367     ir_block *bprecond   = NULL, *end_bprecond   = NULL;
2368     ir_block *bpostcond  = NULL, *end_bpostcond  = NULL;
2369     ir_block *bincrement = NULL, *end_bincrement = NULL;
2370     ir_block *bout       = NULL, *bin            = NULL;
2371
2372     /* let's at least move the outgoing block to the end */
2373     size_t    bout_id;
2374
2375     /* 'break' and 'continue' need to be able to find the right blocks */
2376     ir_block *bcontinue     = NULL;
2377     ir_block *bbreak        = NULL;
2378
2379     ir_block *old_bcontinue = NULL;
2380     ir_block *old_bbreak    = NULL;
2381
2382     ir_block *tmpblock      = NULL;
2383
2384     (void)lvalue;
2385     (void)out;
2386
2387     if (self->expression.outr) {
2388         compile_error(ast_ctx(self), "internal error: ast_loop cannot be reused, it bears no result!");
2389         return false;
2390     }
2391     self->expression.outr = (ir_value*)1;
2392
2393     /* NOTE:
2394      * Should we ever need some kind of block ordering, better make this function
2395      * move blocks around than write a block ordering algorithm later... after all
2396      * the ast and ir should work together, not against each other.
2397      */
2398
2399     /* initexpr doesn't get its own block, it's pointless, it could create more blocks
2400      * anyway if for example it contains a ternary.
2401      */
2402     if (self->initexpr)
2403     {
2404         cgen = self->initexpr->expression.codegen;
2405         if (!(*cgen)((ast_expression*)(self->initexpr), func, false, &dummy))
2406             return false;
2407     }
2408
2409     /* Store the block from which we enter this chaos */
2410     bin = func->curblock;
2411
2412     /* The pre-loop condition needs its own block since we
2413      * need to be able to jump to the start of that expression.
2414      */
2415     if (self->precond)
2416     {
2417         bprecond = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "pre_loop_cond"));
2418         if (!bprecond)
2419             return false;
2420
2421         /* the pre-loop-condition the least important place to 'continue' at */
2422         bcontinue = bprecond;
2423
2424         /* enter */
2425         func->curblock = bprecond;
2426
2427         /* generate */
2428         cgen = self->precond->expression.codegen;
2429         if (!(*cgen)((ast_expression*)(self->precond), func, false, &precond))
2430             return false;
2431
2432         end_bprecond = func->curblock;
2433     } else {
2434         bprecond = end_bprecond = NULL;
2435     }
2436
2437     /* Now the next blocks won't be ordered nicely, but we need to
2438      * generate them this early for 'break' and 'continue'.
2439      */
2440     if (self->increment) {
2441         bincrement = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "loop_increment"));
2442         if (!bincrement)
2443             return false;
2444         bcontinue = bincrement; /* increment comes before the pre-loop-condition */
2445     } else {
2446         bincrement = end_bincrement = NULL;
2447     }
2448
2449     if (self->postcond) {
2450         bpostcond = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "post_loop_cond"));
2451         if (!bpostcond)
2452             return false;
2453         bcontinue = bpostcond; /* postcond comes before the increment */
2454     } else {
2455         bpostcond = end_bpostcond = NULL;
2456     }
2457
2458     bout_id = vec_size(func->ir_func->blocks);
2459     bout = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "after_loop"));
2460     if (!bout)
2461         return false;
2462     bbreak = bout;
2463
2464     /* The loop body... */
2465     /* if (self->body) */
2466     {
2467         bbody = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "loop_body"));
2468         if (!bbody)
2469             return false;
2470
2471         /* enter */
2472         func->curblock = bbody;
2473
2474         old_bbreak          = func->breakblock;
2475         old_bcontinue       = func->continueblock;
2476         func->breakblock    = bbreak;
2477         func->continueblock = bcontinue;
2478         if (!func->continueblock)
2479             func->continueblock = bbody;
2480
2481         /* generate */
2482         if (self->body) {
2483             cgen = self->body->expression.codegen;
2484             if (!(*cgen)((ast_expression*)(self->body), func, false, &dummy))
2485                 return false;
2486         }
2487
2488         end_bbody = func->curblock;
2489         func->breakblock    = old_bbreak;
2490         func->continueblock = old_bcontinue;
2491     }
2492
2493     /* post-loop-condition */
2494     if (self->postcond)
2495     {
2496         /* enter */
2497         func->curblock = bpostcond;
2498
2499         /* generate */
2500         cgen = self->postcond->expression.codegen;
2501         if (!(*cgen)((ast_expression*)(self->postcond), func, false, &postcond))
2502             return false;
2503
2504         end_bpostcond = func->curblock;
2505     }
2506
2507     /* The incrementor */
2508     if (self->increment)
2509     {
2510         /* enter */
2511         func->curblock = bincrement;
2512
2513         /* generate */
2514         cgen = self->increment->expression.codegen;
2515         if (!(*cgen)((ast_expression*)(self->increment), func, false, &dummy))
2516             return false;
2517
2518         end_bincrement = func->curblock;
2519     }
2520
2521     /* In any case now, we continue from the outgoing block */
2522     func->curblock = bout;
2523
2524     /* Now all blocks are in place */
2525     /* From 'bin' we jump to whatever comes first */
2526     if      (bprecond)   tmpblock = bprecond;
2527     else if (bbody)      tmpblock = bbody;
2528     else if (bpostcond)  tmpblock = bpostcond;
2529     else                 tmpblock = bout;
2530     if (!ir_block_create_jump(bin, ast_ctx(self), tmpblock))
2531         return false;
2532
2533     /* From precond */
2534     if (bprecond)
2535     {
2536         ir_block *ontrue, *onfalse;
2537         if      (bbody)      ontrue = bbody;
2538         else if (bincrement) ontrue = bincrement;
2539         else if (bpostcond)  ontrue = bpostcond;
2540         else                 ontrue = bprecond;
2541         onfalse = bout;
2542         if (!ir_block_create_if(end_bprecond, ast_ctx(self), precond, ontrue, onfalse))
2543             return false;
2544     }
2545
2546     /* from body */
2547     if (bbody)
2548     {
2549         if      (bincrement) tmpblock = bincrement;
2550         else if (bpostcond)  tmpblock = bpostcond;
2551         else if (bprecond)   tmpblock = bprecond;
2552         else                 tmpblock = bbody;
2553         if (!end_bbody->final && !ir_block_create_jump(end_bbody, ast_ctx(self), tmpblock))
2554             return false;
2555     }
2556
2557     /* from increment */
2558     if (bincrement)
2559     {
2560         if      (bpostcond)  tmpblock = bpostcond;
2561         else if (bprecond)   tmpblock = bprecond;
2562         else if (bbody)      tmpblock = bbody;
2563         else                 tmpblock = bout;
2564         if (!ir_block_create_jump(end_bincrement, ast_ctx(self), tmpblock))
2565             return false;
2566     }
2567
2568     /* from postcond */
2569     if (bpostcond)
2570     {
2571         ir_block *ontrue, *onfalse;
2572         if      (bprecond)   ontrue = bprecond;
2573         else if (bbody)      ontrue = bbody;
2574         else if (bincrement) ontrue = bincrement;
2575         else                 ontrue = bpostcond;
2576         onfalse = bout;
2577         if (!ir_block_create_if(end_bpostcond, ast_ctx(self), postcond, ontrue, onfalse))
2578             return false;
2579     }
2580
2581     /* Move 'bout' to the end */
2582     vec_remove(func->ir_func->blocks, bout_id, 1);
2583     vec_push(func->ir_func->blocks, bout);
2584
2585     return true;
2586 }
2587
2588 bool ast_breakcont_codegen(ast_breakcont *self, ast_function *func, bool lvalue, ir_value **out)
2589 {
2590     ir_block *target;
2591
2592     *out = NULL;
2593
2594     if (lvalue) {
2595         compile_error(ast_ctx(self), "break/continue expression is not an l-value");
2596         return false;
2597     }
2598
2599     if (self->expression.outr) {
2600         compile_error(ast_ctx(self), "internal error: ast_breakcont cannot be reused!");
2601         return false;
2602     }
2603     self->expression.outr = (ir_value*)1;
2604
2605     if (self->is_continue)
2606         target = func->continueblock;
2607     else
2608         target = func->breakblock;
2609
2610     if (!target) {
2611         compile_error(ast_ctx(self), "%s is lacking a target block", (self->is_continue ? "continue" : "break"));
2612         return false;
2613     }
2614
2615     if (!ir_block_create_jump(func->curblock, ast_ctx(self), target))
2616         return false;
2617     return true;
2618 }
2619
2620 bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_value **out)
2621 {
2622     ast_expression_codegen *cgen;
2623
2624     ast_switch_case *def_case     = NULL;
2625     ir_block        *def_bfall    = NULL;
2626     ir_block        *def_bfall_to = NULL;
2627     bool set_def_bfall_to = false;
2628
2629     ir_value *dummy     = NULL;
2630     ir_value *irop      = NULL;
2631     ir_block *old_break = NULL;
2632     ir_block *bout      = NULL;
2633     ir_block *bfall     = NULL;
2634     size_t    bout_id;
2635     size_t    c;
2636
2637     char      typestr[1024];
2638     uint16_t  cmpinstr;
2639
2640     if (lvalue) {
2641         compile_error(ast_ctx(self), "switch expression is not an l-value");
2642         return false;
2643     }
2644
2645     if (self->expression.outr) {
2646         compile_error(ast_ctx(self), "internal error: ast_switch cannot be reused!");
2647         return false;
2648     }
2649     self->expression.outr = (ir_value*)1;
2650
2651     (void)lvalue;
2652     (void)out;
2653
2654     cgen = self->operand->expression.codegen;
2655     if (!(*cgen)((ast_expression*)(self->operand), func, false, &irop))
2656         return false;
2657
2658     if (!vec_size(self->cases))
2659         return true;
2660
2661     cmpinstr = type_eq_instr[irop->vtype];
2662     if (cmpinstr >= AINSTR_END) {
2663         ast_type_to_string(self->operand, typestr, sizeof(typestr));
2664         compile_error(ast_ctx(self), "invalid type to perform a switch on: %s", typestr);
2665         return false;
2666     }
2667
2668     bout_id = vec_size(func->ir_func->blocks);
2669     bout = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "after_switch"));
2670     if (!bout)
2671         return false;
2672
2673     /* setup the break block */
2674     old_break        = func->breakblock;
2675     func->breakblock = bout;
2676
2677     /* Now create all cases */
2678     for (c = 0; c < vec_size(self->cases); ++c) {
2679         ir_value *cond, *val;
2680         ir_block *bcase, *bnot;
2681         size_t bnot_id;
2682
2683         ast_switch_case *swcase = &self->cases[c];
2684
2685         if (swcase->value) {
2686             /* A regular case */
2687             /* generate the condition operand */
2688             cgen = swcase->value->expression.codegen;
2689             if (!(*cgen)((ast_expression*)(swcase->value), func, false, &val))
2690                 return false;
2691             /* generate the condition */
2692             cond = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "switch_eq"), cmpinstr, irop, val);
2693             if (!cond)
2694                 return false;
2695
2696             bcase = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "case"));
2697             bnot_id = vec_size(func->ir_func->blocks);
2698             bnot = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "not_case"));
2699             if (!bcase || !bnot)
2700                 return false;
2701             if (set_def_bfall_to) {
2702                 set_def_bfall_to = false;
2703                 def_bfall_to = bcase;
2704             }
2705             if (!ir_block_create_if(func->curblock, ast_ctx(self), cond, bcase, bnot))
2706                 return false;
2707
2708             /* Make the previous case-end fall through */
2709             if (bfall && !bfall->final) {
2710                 if (!ir_block_create_jump(bfall, ast_ctx(self), bcase))
2711                     return false;
2712             }
2713
2714             /* enter the case */
2715             func->curblock = bcase;
2716             cgen = swcase->code->expression.codegen;
2717             if (!(*cgen)((ast_expression*)swcase->code, func, false, &dummy))
2718                 return false;
2719
2720             /* remember this block to fall through from */
2721             bfall = func->curblock;
2722
2723             /* enter the else and move it down */
2724             func->curblock = bnot;
2725             vec_remove(func->ir_func->blocks, bnot_id, 1);
2726             vec_push(func->ir_func->blocks, bnot);
2727         } else {
2728             /* The default case */
2729             /* Remember where to fall through from: */
2730             def_bfall = bfall;
2731             bfall     = NULL;
2732             /* remember which case it was */
2733             def_case  = swcase;
2734             /* And the next case will be remembered */
2735             set_def_bfall_to = true;
2736         }
2737     }
2738
2739     /* Jump from the last bnot to bout */
2740     if (bfall && !bfall->final && !ir_block_create_jump(bfall, ast_ctx(self), bout)) {
2741         /*
2742         astwarning(ast_ctx(bfall), WARN_???, "missing break after last case");
2743         */
2744         return false;
2745     }
2746
2747     /* If there was a default case, put it down here */
2748     if (def_case) {
2749         ir_block *bcase;
2750
2751         /* No need to create an extra block */
2752         bcase = func->curblock;
2753
2754         /* Insert the fallthrough jump */
2755         if (def_bfall && !def_bfall->final) {
2756             if (!ir_block_create_jump(def_bfall, ast_ctx(self), bcase))
2757                 return false;
2758         }
2759
2760         /* Now generate the default code */
2761         cgen = def_case->code->expression.codegen;
2762         if (!(*cgen)((ast_expression*)def_case->code, func, false, &dummy))
2763             return false;
2764
2765         /* see if we need to fall through */
2766         if (def_bfall_to && !func->curblock->final)
2767         {
2768             if (!ir_block_create_jump(func->curblock, ast_ctx(self), def_bfall_to))
2769                 return false;
2770         }
2771     }
2772
2773     /* Jump from the last bnot to bout */
2774     if (!func->curblock->final && !ir_block_create_jump(func->curblock, ast_ctx(self), bout))
2775         return false;
2776     /* enter the outgoing block */
2777     func->curblock = bout;
2778
2779     /* restore the break block */
2780     func->breakblock = old_break;
2781
2782     /* Move 'bout' to the end, it's nicer */
2783     vec_remove(func->ir_func->blocks, bout_id, 1);
2784     vec_push(func->ir_func->blocks, bout);
2785
2786     return true;
2787 }
2788
2789 bool ast_label_codegen(ast_label *self, ast_function *func, bool lvalue, ir_value **out)
2790 {
2791     size_t i;
2792     ir_value *dummy;
2793
2794     *out = NULL;
2795     if (lvalue) {
2796         compile_error(ast_ctx(self), "internal error: ast_label cannot be an lvalue");
2797         return false;
2798     }
2799
2800     /* simply create a new block and jump to it */
2801     self->irblock = ir_function_create_block(ast_ctx(self), func->ir_func, self->name);
2802     if (!self->irblock) {
2803         compile_error(ast_ctx(self), "failed to allocate label block `%s`", self->name);
2804         return false;
2805     }
2806     if (!func->curblock->final) {
2807         if (!ir_block_create_jump(func->curblock, ast_ctx(self), self->irblock))
2808             return false;
2809     }
2810
2811     /* enter the new block */
2812     func->curblock = self->irblock;
2813
2814     /* Generate all the leftover gotos */
2815     for (i = 0; i < vec_size(self->gotos); ++i) {
2816         if (!ast_goto_codegen(self->gotos[i], func, false, &dummy))
2817             return false;
2818     }
2819
2820     return true;
2821 }
2822
2823 bool ast_goto_codegen(ast_goto *self, ast_function *func, bool lvalue, ir_value **out)
2824 {
2825     *out = NULL;
2826     if (lvalue) {
2827         compile_error(ast_ctx(self), "internal error: ast_goto cannot be an lvalue");
2828         return false;
2829     }
2830
2831     if (self->target->irblock) {
2832         if (self->irblock_from) {
2833             /* we already tried once, this is the callback */
2834             self->irblock_from->final = false;
2835             if (!ir_block_create_jump(self->irblock_from, ast_ctx(self), self->target->irblock)) {
2836                 compile_error(ast_ctx(self), "failed to generate goto to `%s`", self->name);
2837                 return false;
2838             }
2839         }
2840         else
2841         {
2842             if (!ir_block_create_jump(func->curblock, ast_ctx(self), self->target->irblock)) {
2843                 compile_error(ast_ctx(self), "failed to generate goto to `%s`", self->name);
2844                 return false;
2845             }
2846         }
2847     }
2848     else
2849     {
2850         /* the target has not yet been created...
2851          * close this block in a sneaky way:
2852          */
2853         func->curblock->final = true;
2854         self->irblock_from = func->curblock;
2855         ast_label_register_goto(self->target, self);
2856     }
2857
2858     return true;
2859 }
2860
2861 bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value **out)
2862 {
2863     ast_expression_codegen *cgen;
2864     ir_value              **params;
2865     ir_instr               *callinstr;
2866     size_t i;
2867
2868     ir_value *funval = NULL;
2869
2870     /* return values are never lvalues */
2871     if (lvalue) {
2872         compile_error(ast_ctx(self), "not an l-value (function call)");
2873         return false;
2874     }
2875
2876     if (self->expression.outr) {
2877         *out = self->expression.outr;
2878         return true;
2879     }
2880
2881     cgen = self->func->expression.codegen;
2882     if (!(*cgen)((ast_expression*)(self->func), func, false, &funval))
2883         return false;
2884     if (!funval)
2885         return false;
2886
2887     params = NULL;
2888
2889     /* parameters */
2890     for (i = 0; i < vec_size(self->params); ++i)
2891     {
2892         ir_value *param;
2893         ast_expression *expr = self->params[i];
2894
2895         cgen = expr->expression.codegen;
2896         if (!(*cgen)(expr, func, false, &param))
2897             goto error;
2898         if (!param)
2899             goto error;
2900         vec_push(params, param);
2901     }
2902
2903     callinstr = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "call"), funval);
2904     if (!callinstr)
2905         goto error;
2906
2907     for (i = 0; i < vec_size(params); ++i) {
2908         ir_call_param(callinstr, params[i]);
2909     }
2910
2911     *out = ir_call_value(callinstr);
2912     self->expression.outr = *out;
2913
2914     codegen_output_type(self, *out);
2915
2916     vec_free(params);
2917     return true;
2918 error:
2919     vec_free(params);
2920     return false;
2921 }