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