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