]> git.xonotic.org Git - xonotic/gmqcc.git/blob - ast.c
Added test for variadic arguments
[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 /* error handling */
39 static void asterror(lex_ctx ctx, const char *msg, ...)
40 {
41     va_list ap;
42     va_start(ap, msg);
43     con_cvprintmsg((void*)&ctx, LVL_ERROR, "error", msg, ap);
44     va_end(ap);
45 }
46
47 /* It must not be possible to get here. */
48 static GMQCC_NORETURN void _ast_node_destroy(ast_node *self)
49 {
50     con_err("ast node missing destroy()\n");
51     abort();
52 }
53
54 /* Initialize main ast node aprts */
55 static void ast_node_init(ast_node *self, lex_ctx ctx, int nodetype)
56 {
57     self->node.context = ctx;
58     self->node.destroy = &_ast_node_destroy;
59     self->node.keep    = false;
60     self->node.nodetype = nodetype;
61 }
62
63 /* General expression initialization */
64 static void ast_expression_init(ast_expression *self,
65                                 ast_expression_codegen *codegen)
66 {
67     self->expression.codegen  = codegen;
68     self->expression.vtype    = TYPE_VOID;
69     self->expression.next     = NULL;
70     self->expression.outl     = NULL;
71     self->expression.outr     = NULL;
72     self->expression.variadic = false;
73     self->expression.params   = NULL;
74 }
75
76 static void ast_expression_delete(ast_expression *self)
77 {
78     size_t i;
79     if (self->expression.next)
80         ast_delete(self->expression.next);
81     for (i = 0; i < vec_size(self->expression.params); ++i) {
82         ast_delete(self->expression.params[i]);
83     }
84     vec_free(self->expression.params);
85 }
86
87 static void ast_expression_delete_full(ast_expression *self)
88 {
89     ast_expression_delete(self);
90     mem_d(self);
91 }
92
93 ast_value* ast_value_copy(const ast_value *self)
94 {
95     size_t i;
96     const ast_expression_common *fromex;
97     ast_expression_common *selfex;
98     ast_value *cp = ast_value_new(self->expression.node.context, self->name, self->expression.vtype);
99     if (self->expression.next) {
100         cp->expression.next = ast_type_copy(self->expression.node.context, self->expression.next);
101         if (!cp->expression.next) {
102             ast_value_delete(cp);
103             return NULL;
104         }
105     }
106     fromex   = &self->expression;
107     selfex = &cp->expression;
108     selfex->variadic = fromex->variadic;
109     for (i = 0; i < vec_size(fromex->params); ++i) {
110         ast_value *v = ast_value_copy(fromex->params[i]);
111         if (!v) {
112             ast_value_delete(cp);
113             return NULL;
114         }
115         vec_push(selfex->params, v);
116     }
117     return cp;
118 }
119
120 bool ast_type_adopt_impl(ast_expression *self, const ast_expression *other)
121 {
122     size_t i;
123     const ast_expression_common *fromex;
124     ast_expression_common *selfex;
125     self->expression.vtype = other->expression.vtype;
126     if (other->expression.next) {
127         self->expression.next = (ast_expression*)ast_type_copy(ast_ctx(self), other->expression.next);
128         if (!self->expression.next)
129             return false;
130     }
131     fromex   = &other->expression;
132     selfex = &self->expression;
133     selfex->variadic = fromex->variadic;
134     for (i = 0; i < vec_size(fromex->params); ++i) {
135         ast_value *v = ast_value_copy(fromex->params[i]);
136         if (!v)
137             return false;
138         vec_push(selfex->params, v);
139     }
140     return true;
141 }
142
143 static ast_expression* ast_shallow_type(lex_ctx ctx, int vtype)
144 {
145     ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
146     ast_expression_init(self, NULL);
147     self->expression.codegen = NULL;
148     self->expression.next    = NULL;
149     self->expression.vtype   = vtype;
150     return self;
151 }
152
153 ast_expression* ast_type_copy(lex_ctx ctx, const ast_expression *ex)
154 {
155     size_t i;
156     const ast_expression_common *fromex;
157     ast_expression_common *selfex;
158
159     if (!ex)
160         return NULL;
161     else
162     {
163         ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
164         ast_expression_init(self, NULL);
165
166         fromex   = &ex->expression;
167         selfex = &self->expression;
168
169         /* This may never be codegen()d */
170         selfex->codegen = NULL;
171
172         selfex->vtype = fromex->vtype;
173         if (fromex->next)
174         {
175             selfex->next = ast_type_copy(ctx, fromex->next);
176             if (!selfex->next) {
177                 ast_expression_delete_full(self);
178                 return NULL;
179             }
180         }
181         else
182             selfex->next = NULL;
183
184         selfex->variadic = fromex->variadic;
185         for (i = 0; i < vec_size(fromex->params); ++i) {
186             ast_value *v = ast_value_copy(fromex->params[i]);
187             if (!v) {
188                 ast_expression_delete_full(self);
189                 return NULL;
190             }
191             vec_push(selfex->params, v);
192         }
193
194         return self;
195     }
196 }
197
198 bool ast_compare_type(ast_expression *a, ast_expression *b)
199 {
200     if (a->expression.vtype != b->expression.vtype)
201         return false;
202     if (!a->expression.next != !b->expression.next)
203         return false;
204     if (vec_size(a->expression.params) != vec_size(b->expression.params))
205         return false;
206     if (a->expression.variadic != b->expression.variadic)
207         return false;
208     if (vec_size(a->expression.params)) {
209         size_t i;
210         for (i = 0; i < vec_size(a->expression.params); ++i) {
211             if (!ast_compare_type((ast_expression*)a->expression.params[i],
212                                   (ast_expression*)b->expression.params[i]))
213                 return false;
214         }
215     }
216     if (a->expression.next)
217         return ast_compare_type(a->expression.next, b->expression.next);
218     return true;
219 }
220
221 static size_t ast_type_to_string_impl(ast_expression *e, char *buf, size_t bufsize, size_t pos)
222 {
223     const char *typestr;
224     size_t typelen;
225     size_t i;
226
227     if (!e) {
228         if (pos + 6 >= bufsize)
229             goto full;
230         strcpy(buf + pos, "(null)");
231         return pos + 6;
232     }
233
234     if (pos + 1 >= bufsize)
235         goto full;
236
237     switch (e->expression.vtype) {
238         case TYPE_VARIANT:
239             strcpy(buf + pos, "(variant)");
240             return pos + 9;
241
242         case TYPE_FIELD:
243             buf[pos++] = '.';
244             return ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
245
246         case TYPE_POINTER:
247             if (pos + 3 >= bufsize)
248                 goto full;
249             buf[pos++] = '*';
250             buf[pos++] = '(';
251             pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
252             if (pos + 1 >= bufsize)
253                 goto full;
254             buf[pos++] = ')';
255             return pos;
256
257         case TYPE_FUNCTION:
258             pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
259             if (pos + 2 >= bufsize)
260                 goto full;
261             if (!vec_size(e->expression.params)) {
262                 buf[pos++] = '(';
263                 buf[pos++] = ')';
264                 return pos;
265             }
266             buf[pos++] = '(';
267             pos = ast_type_to_string_impl((ast_expression*)(e->expression.params[0]), buf, bufsize, pos);
268             for (i = 1; i < vec_size(e->expression.params); ++i) {
269                 if (pos + 2 >= bufsize)
270                     goto full;
271                 buf[pos++] = ',';
272                 buf[pos++] = ' ';
273                 pos = ast_type_to_string_impl((ast_expression*)(e->expression.params[i]), buf, bufsize, pos);
274             }
275             if (pos + 1 >= bufsize)
276                 goto full;
277             buf[pos++] = ')';
278             return pos;
279
280         case TYPE_ARRAY:
281             pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
282             if (pos + 1 >= bufsize)
283                 goto full;
284             buf[pos++] = '[';
285             pos += snprintf(buf + pos, bufsize - pos - 1, "%i", (int)e->expression.count);
286             if (pos + 1 >= bufsize)
287                 goto full;
288             buf[pos++] = ']';
289             return pos;
290
291         default:
292             typestr = type_name[e->expression.vtype];
293             typelen = strlen(typestr);
294             if (pos + typelen >= bufsize)
295                 goto full;
296             strcpy(buf + pos, typestr);
297             return pos + typelen;
298     }
299
300 full:
301     buf[bufsize-3] = '.';
302     buf[bufsize-2] = '.';
303     buf[bufsize-1] = '.';
304     return bufsize;
305 }
306
307 void ast_type_to_string(ast_expression *e, char *buf, size_t bufsize)
308 {
309     size_t pos = ast_type_to_string_impl(e, buf, bufsize-1, 0);
310     buf[pos] = 0;
311 }
312
313 ast_value* ast_value_new(lex_ctx ctx, const char *name, int t)
314 {
315     ast_instantiate(ast_value, ctx, ast_value_delete);
316     ast_expression_init((ast_expression*)self,
317                         (ast_expression_codegen*)&ast_value_codegen);
318     self->expression.node.keep = true; /* keep */
319
320     self->name = name ? util_strdup(name) : NULL;
321     self->expression.vtype = t;
322     self->expression.next  = NULL;
323     self->isconst = false;
324     self->uses    = 0;
325     memset(&self->constval, 0, sizeof(self->constval));
326
327     self->ir_v           = NULL;
328     self->ir_values      = NULL;
329     self->ir_value_count = 0;
330
331     self->setter = NULL;
332     self->getter = NULL;
333
334     return self;
335 }
336
337 void ast_value_delete(ast_value* self)
338 {
339     if (self->name)
340         mem_d((void*)self->name);
341     if (self->isconst) {
342         switch (self->expression.vtype)
343         {
344         case TYPE_STRING:
345             mem_d((void*)self->constval.vstring);
346             break;
347         case TYPE_FUNCTION:
348             /* unlink us from the function node */
349             self->constval.vfunc->vtype = NULL;
350             break;
351         /* NOTE: delete function? currently collected in
352          * the parser structure
353          */
354         default:
355             break;
356         }
357     }
358     if (self->ir_values)
359         mem_d(self->ir_values);
360     ast_expression_delete((ast_expression*)self);
361     mem_d(self);
362 }
363
364 void ast_value_params_add(ast_value *self, ast_value *p)
365 {
366     vec_push(self->expression.params, p);
367 }
368
369 bool ast_value_set_name(ast_value *self, const char *name)
370 {
371     if (self->name)
372         mem_d((void*)self->name);
373     self->name = util_strdup(name);
374     return !!self->name;
375 }
376
377 ast_binary* ast_binary_new(lex_ctx ctx, int op,
378                            ast_expression* left, ast_expression* right)
379 {
380     ast_instantiate(ast_binary, ctx, ast_binary_delete);
381     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binary_codegen);
382
383     self->op = op;
384     self->left = left;
385     self->right = right;
386
387     if (op >= INSTR_EQ_F && op <= INSTR_GT)
388         self->expression.vtype = TYPE_FLOAT;
389     else if (op == INSTR_AND || op == INSTR_OR ||
390              op == INSTR_BITAND || op == INSTR_BITOR)
391         self->expression.vtype = TYPE_FLOAT;
392     else if (op == INSTR_MUL_VF || op == INSTR_MUL_FV)
393         self->expression.vtype = TYPE_VECTOR;
394     else if (op == INSTR_MUL_V)
395         self->expression.vtype = TYPE_FLOAT;
396     else
397         self->expression.vtype = left->expression.vtype;
398
399     return self;
400 }
401
402 void ast_binary_delete(ast_binary *self)
403 {
404     ast_unref(self->left);
405     ast_unref(self->right);
406     ast_expression_delete((ast_expression*)self);
407     mem_d(self);
408 }
409
410 ast_binstore* ast_binstore_new(lex_ctx ctx, int storop, int op,
411                                ast_expression* left, ast_expression* right)
412 {
413     ast_instantiate(ast_binstore, ctx, ast_binstore_delete);
414     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binstore_codegen);
415
416     self->opstore = storop;
417     self->opbin   = op;
418     self->dest    = left;
419     self->source  = right;
420
421     self->expression.vtype = left->expression.vtype;
422     if (left->expression.next) {
423         self->expression.next = ast_type_copy(ctx, left);
424         if (!self->expression.next) {
425             ast_delete(self);
426             return NULL;
427         }
428     }
429     else
430         self->expression.next = NULL;
431
432     return self;
433 }
434
435 void ast_binstore_delete(ast_binstore *self)
436 {
437     ast_unref(self->dest);
438     ast_unref(self->source);
439     ast_expression_delete((ast_expression*)self);
440     mem_d(self);
441 }
442
443 ast_unary* ast_unary_new(lex_ctx ctx, int op,
444                          ast_expression *expr)
445 {
446     ast_instantiate(ast_unary, ctx, ast_unary_delete);
447     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_unary_codegen);
448
449     self->op = op;
450     self->operand = expr;
451
452     if (op >= INSTR_NOT_F && op <= INSTR_NOT_FNC) {
453         self->expression.vtype = TYPE_FLOAT;
454     } else
455         asterror(ctx, "cannot determine type of unary operation %s", asm_instr[op].m);
456
457     return self;
458 }
459
460 void ast_unary_delete(ast_unary *self)
461 {
462     ast_unref(self->operand);
463     ast_expression_delete((ast_expression*)self);
464     mem_d(self);
465 }
466
467 ast_return* ast_return_new(lex_ctx ctx, ast_expression *expr)
468 {
469     ast_instantiate(ast_return, ctx, ast_return_delete);
470     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_return_codegen);
471
472     self->operand = expr;
473
474     return self;
475 }
476
477 void ast_return_delete(ast_return *self)
478 {
479     if (self->operand)
480         ast_unref(self->operand);
481     ast_expression_delete((ast_expression*)self);
482     mem_d(self);
483 }
484
485 ast_entfield* ast_entfield_new(lex_ctx ctx, ast_expression *entity, ast_expression *field)
486 {
487     if (field->expression.vtype != TYPE_FIELD) {
488         asterror(ctx, "ast_entfield_new with expression not of type field");
489         return NULL;
490     }
491     return ast_entfield_new_force(ctx, entity, field, field->expression.next);
492 }
493
494 ast_entfield* ast_entfield_new_force(lex_ctx ctx, ast_expression *entity, ast_expression *field, const ast_expression *outtype)
495 {
496     ast_instantiate(ast_entfield, ctx, ast_entfield_delete);
497
498     if (!outtype) {
499         mem_d(self);
500         /* Error: field has no type... */
501         return NULL;
502     }
503
504     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_entfield_codegen);
505
506     self->entity = entity;
507     self->field  = field;
508
509     if (!ast_type_adopt(self, outtype)) {
510         ast_entfield_delete(self);
511         return NULL;
512     }
513
514     return self;
515 }
516
517 void ast_entfield_delete(ast_entfield *self)
518 {
519     ast_unref(self->entity);
520     ast_unref(self->field);
521     ast_expression_delete((ast_expression*)self);
522     mem_d(self);
523 }
524
525 ast_member* ast_member_new(lex_ctx ctx, ast_expression *owner, unsigned int field)
526 {
527     ast_instantiate(ast_member, ctx, ast_member_delete);
528     if (field >= 3) {
529         mem_d(self);
530         return NULL;
531     }
532
533     if (owner->expression.vtype != TYPE_VECTOR &&
534         owner->expression.vtype != TYPE_FIELD) {
535         asterror(ctx, "member-access on an invalid owner of type %s", type_name[owner->expression.vtype]);
536         mem_d(self);
537         return NULL;
538     }
539
540     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_member_codegen);
541     self->expression.node.keep = true; /* keep */
542
543     if (owner->expression.vtype == TYPE_VECTOR) {
544         self->expression.vtype = TYPE_FLOAT;
545         self->expression.next  = NULL;
546     } else {
547         self->expression.vtype = TYPE_FIELD;
548         self->expression.next = ast_shallow_type(ctx, TYPE_FLOAT);
549     }
550
551     self->owner = owner;
552     self->field = field;
553
554     return self;
555 }
556
557 void ast_member_delete(ast_member *self)
558 {
559     /* The owner is always an ast_value, which has .keep=true,
560      * also: ast_members are usually deleted after the owner, thus
561      * this will cause invalid access
562     ast_unref(self->owner);
563      * once we allow (expression).x to access a vector-member, we need
564      * to change this: preferably by creating an alternate ast node for this
565      * purpose that is not garbage-collected.
566     */
567     ast_expression_delete((ast_expression*)self);
568     mem_d(self);
569 }
570
571 ast_array_index* ast_array_index_new(lex_ctx ctx, ast_expression *array, ast_expression *index)
572 {
573     ast_expression *outtype;
574     ast_instantiate(ast_array_index, ctx, ast_array_index_delete);
575
576     outtype = array->expression.next;
577     if (!outtype) {
578         mem_d(self);
579         /* Error: field has no type... */
580         return NULL;
581     }
582
583     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_array_index_codegen);
584
585     self->array = array;
586     self->index = index;
587
588     if (!ast_type_adopt(self, outtype)) {
589         ast_array_index_delete(self);
590         return NULL;
591     }
592     if (array->expression.vtype == TYPE_FIELD && outtype->expression.vtype == TYPE_ARRAY) {
593         if (self->expression.vtype != TYPE_ARRAY) {
594             asterror(ast_ctx(self), "array_index node on type");
595             ast_array_index_delete(self);
596             return NULL;
597         }
598         self->array = outtype;
599         self->expression.vtype = TYPE_FIELD;
600     }
601
602     return self;
603 }
604
605 void ast_array_index_delete(ast_array_index *self)
606 {
607     ast_unref(self->array);
608     ast_unref(self->index);
609     ast_expression_delete((ast_expression*)self);
610     mem_d(self);
611 }
612
613 ast_ifthen* ast_ifthen_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
614 {
615     ast_instantiate(ast_ifthen, ctx, ast_ifthen_delete);
616     if (!ontrue && !onfalse) {
617         /* because it is invalid */
618         mem_d(self);
619         return NULL;
620     }
621     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ifthen_codegen);
622
623     self->cond     = cond;
624     self->on_true  = ontrue;
625     self->on_false = onfalse;
626
627     return self;
628 }
629
630 void ast_ifthen_delete(ast_ifthen *self)
631 {
632     ast_unref(self->cond);
633     if (self->on_true)
634         ast_unref(self->on_true);
635     if (self->on_false)
636         ast_unref(self->on_false);
637     ast_expression_delete((ast_expression*)self);
638     mem_d(self);
639 }
640
641 ast_ternary* ast_ternary_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
642 {
643     ast_instantiate(ast_ternary, ctx, ast_ternary_delete);
644     /* This time NEITHER must be NULL */
645     if (!ontrue || !onfalse) {
646         mem_d(self);
647         return NULL;
648     }
649     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ternary_codegen);
650
651     self->cond     = cond;
652     self->on_true  = ontrue;
653     self->on_false = onfalse;
654     self->phi_out  = NULL;
655
656     return self;
657 }
658
659 void ast_ternary_delete(ast_ternary *self)
660 {
661     ast_unref(self->cond);
662     ast_unref(self->on_true);
663     ast_unref(self->on_false);
664     ast_expression_delete((ast_expression*)self);
665     mem_d(self);
666 }
667
668 ast_loop* ast_loop_new(lex_ctx ctx,
669                        ast_expression *initexpr,
670                        ast_expression *precond,
671                        ast_expression *postcond,
672                        ast_expression *increment,
673                        ast_expression *body)
674 {
675     ast_instantiate(ast_loop, ctx, ast_loop_delete);
676     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_loop_codegen);
677
678     self->initexpr  = initexpr;
679     self->precond   = precond;
680     self->postcond  = postcond;
681     self->increment = increment;
682     self->body      = body;
683
684     return self;
685 }
686
687 void ast_loop_delete(ast_loop *self)
688 {
689     if (self->initexpr)
690         ast_unref(self->initexpr);
691     if (self->precond)
692         ast_unref(self->precond);
693     if (self->postcond)
694         ast_unref(self->postcond);
695     if (self->increment)
696         ast_unref(self->increment);
697     if (self->body)
698         ast_unref(self->body);
699     ast_expression_delete((ast_expression*)self);
700     mem_d(self);
701 }
702
703 ast_call* ast_call_new(lex_ctx ctx,
704                        ast_expression *funcexpr)
705 {
706     ast_instantiate(ast_call, ctx, ast_call_delete);
707     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_call_codegen);
708
709     self->params = NULL;
710     self->func   = funcexpr;
711
712     self->expression.vtype = funcexpr->expression.next->expression.vtype;
713     if (funcexpr->expression.next->expression.next)
714         self->expression.next = ast_type_copy(ctx, funcexpr->expression.next->expression.next);
715
716     return self;
717 }
718
719 void ast_call_delete(ast_call *self)
720 {
721     size_t i;
722     for (i = 0; i < vec_size(self->params); ++i)
723         ast_unref(self->params[i]);
724     vec_free(self->params);
725
726     if (self->func)
727         ast_unref(self->func);
728
729     ast_expression_delete((ast_expression*)self);
730     mem_d(self);
731 }
732
733 bool ast_call_check_types(ast_call *self)
734 {
735     size_t i;
736     bool   retval = true;
737     const  ast_expression *func = self->func;
738     size_t count = vec_size(self->params);
739     if (count > vec_size(func->expression.params))
740         count = vec_size(func->expression.params);
741
742     for (i = 0; i < count; ++i) {
743         if (!ast_compare_type(self->params[i], (ast_expression*)(func->expression.params[i]))) {
744             asterror(ast_ctx(self), "invalid type for parameter %u in function call",
745                      (unsigned int)(i+1));
746             /* we don't immediately return */
747             retval = false;
748         }
749     }
750     return retval;
751 }
752
753 ast_store* ast_store_new(lex_ctx ctx, int op,
754                          ast_expression *dest, ast_expression *source)
755 {
756     ast_instantiate(ast_store, ctx, ast_store_delete);
757     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_store_codegen);
758
759     self->op = op;
760     self->dest = dest;
761     self->source = source;
762
763     self->expression.vtype = dest->expression.vtype;
764     if (dest->expression.next) {
765         self->expression.next = ast_type_copy(ctx, dest);
766         if (!self->expression.next) {
767             ast_delete(self);
768             return NULL;
769         }
770     }
771     else
772         self->expression.next = NULL;
773
774     return self;
775 }
776
777 void ast_store_delete(ast_store *self)
778 {
779     ast_unref(self->dest);
780     ast_unref(self->source);
781     ast_expression_delete((ast_expression*)self);
782     mem_d(self);
783 }
784
785 ast_block* ast_block_new(lex_ctx ctx)
786 {
787     ast_instantiate(ast_block, ctx, ast_block_delete);
788     ast_expression_init((ast_expression*)self,
789                         (ast_expression_codegen*)&ast_block_codegen);
790
791     self->locals  = NULL;
792     self->exprs   = NULL;
793     self->collect = NULL;
794
795     return self;
796 }
797
798 void ast_block_collect(ast_block *self, ast_expression *expr)
799 {
800     vec_push(self->collect, expr);
801     expr->expression.node.keep = true;
802 }
803
804 void ast_block_delete(ast_block *self)
805 {
806     size_t i;
807     for (i = 0; i < vec_size(self->exprs); ++i)
808         ast_unref(self->exprs[i]);
809     vec_free(self->exprs);
810     for (i = 0; i < vec_size(self->locals); ++i)
811         ast_delete(self->locals[i]);
812     vec_free(self->locals);
813     for (i = 0; i < vec_size(self->collect); ++i)
814         ast_delete(self->collect[i]);
815     vec_free(self->collect);
816     ast_expression_delete((ast_expression*)self);
817     mem_d(self);
818 }
819
820 bool ast_block_set_type(ast_block *self, ast_expression *from)
821 {
822     if (self->expression.next)
823         ast_delete(self->expression.next);
824     self->expression.vtype = from->expression.vtype;
825     if (from->expression.next) {
826         self->expression.next = ast_type_copy(self->expression.node.context, from->expression.next);
827         if (!self->expression.next)
828             return false;
829     }
830     else
831         self->expression.next = NULL;
832     return true;
833 }
834
835 ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype)
836 {
837     ast_instantiate(ast_function, ctx, ast_function_delete);
838
839     if (!vtype ||
840         vtype->isconst ||
841         vtype->expression.vtype != TYPE_FUNCTION)
842     {
843         mem_d(self);
844         return NULL;
845     }
846
847     self->vtype  = vtype;
848     self->name   = name ? util_strdup(name) : NULL;
849     self->blocks = NULL;
850
851     self->labelcount = 0;
852     self->builtin = 0;
853
854     self->ir_func = NULL;
855     self->curblock = NULL;
856
857     self->breakblock    = NULL;
858     self->continueblock = NULL;
859
860     vtype->isconst = true;
861     vtype->constval.vfunc = self;
862
863     return self;
864 }
865
866 void ast_function_delete(ast_function *self)
867 {
868     size_t i;
869     if (self->name)
870         mem_d((void*)self->name);
871     if (self->vtype) {
872         /* ast_value_delete(self->vtype); */
873         self->vtype->isconst = false;
874         self->vtype->constval.vfunc = NULL;
875         /* We use unref - if it was stored in a global table it is supposed
876          * to be deleted from *there*
877          */
878         ast_unref(self->vtype);
879     }
880     for (i = 0; i < vec_size(self->blocks); ++i)
881         ast_delete(self->blocks[i]);
882     vec_free(self->blocks);
883     mem_d(self);
884 }
885
886 const char* ast_function_label(ast_function *self, const char *prefix)
887 {
888     size_t id;
889     size_t len;
890     char  *from;
891
892     if (!opts_dump)
893         return NULL;
894
895     id  = (self->labelcount++);
896     len = strlen(prefix);
897
898     from = self->labelbuf + sizeof(self->labelbuf)-1;
899     *from-- = 0;
900     do {
901         unsigned int digit = id % 10;
902         *from = digit + '0';
903         id /= 10;
904     } while (id);
905     memcpy(from - len, prefix, len);
906     return from - len;
907 }
908
909 /*********************************************************************/
910 /* AST codegen part
911  * by convention you must never pass NULL to the 'ir_value **out'
912  * parameter. If you really don't care about the output, pass a dummy.
913  * But I can't imagine a pituation where the output is truly unnecessary.
914  */
915
916 bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out)
917 {
918     /* NOTE: This is the codegen for a variable used in an expression.
919      * It is not the codegen to generate the value. For this purpose,
920      * ast_local_codegen and ast_global_codegen are to be used before this
921      * is executed. ast_function_codegen should take care of its locals,
922      * and the ast-user should take care of ast_global_codegen to be used
923      * on all the globals.
924      */
925     if (!self->ir_v) {
926         char typename[1024];
927         ast_type_to_string((ast_expression*)self, typename, sizeof(typename));
928         asterror(ast_ctx(self), "ast_value used before generated %s %s", typename, self->name);
929         return false;
930     }
931     *out = self->ir_v;
932     return true;
933 }
934
935 bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
936 {
937     ir_value *v = NULL;
938
939     if (self->isconst && self->expression.vtype == TYPE_FUNCTION)
940     {
941         ir_function *func = ir_builder_create_function(ir, self->name, self->expression.next->expression.vtype);
942         if (!func)
943             return false;
944         func->context = ast_ctx(self);
945         func->value->context = ast_ctx(self);
946
947         self->constval.vfunc->ir_func = func;
948         self->ir_v = func->value;
949         /* The function is filled later on ast_function_codegen... */
950         return true;
951     }
952
953     if (isfield && self->expression.vtype == TYPE_FIELD) {
954         ast_expression *fieldtype = self->expression.next;
955
956         if (self->isconst) {
957             asterror(ast_ctx(self), "TODO: constant field pointers with value");
958             goto error;
959         }
960
961         if (fieldtype->expression.vtype == TYPE_ARRAY) {
962             size_t ai;
963             char   *name;
964             size_t  namelen;
965
966             ast_expression_common *elemtype;
967             int                    vtype;
968             ast_value             *array = (ast_value*)fieldtype;
969
970             if (!ast_istype(fieldtype, ast_value)) {
971                 asterror(ast_ctx(self), "internal error: ast_value required");
972                 return false;
973             }
974
975             /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
976             if (!array->expression.count || array->expression.count > opts_max_array_size)
977                 asterror(ast_ctx(self), "Invalid array of size %lu", (unsigned long)array->expression.count);
978
979             elemtype = &array->expression.next->expression;
980             vtype = elemtype->vtype;
981
982             v = ir_builder_create_field(ir, self->name, vtype);
983             if (!v) {
984                 asterror(ast_ctx(self), "ir_builder_create_global failed");
985                 return false;
986             }
987             if (vtype == TYPE_FIELD)
988                 v->fieldtype = elemtype->next->expression.vtype;
989             v->context = ast_ctx(self);
990             array->ir_v = self->ir_v = v;
991
992             namelen = strlen(self->name);
993             name    = (char*)mem_a(namelen + 16);
994             strcpy(name, self->name);
995
996             array->ir_values = (ir_value**)mem_a(sizeof(array->ir_values[0]) * array->expression.count);
997             array->ir_values[0] = v;
998             for (ai = 1; ai < array->expression.count; ++ai) {
999                 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1000                 array->ir_values[ai] = ir_builder_create_field(ir, name, vtype);
1001                 if (!array->ir_values[ai]) {
1002                     mem_d(name);
1003                     asterror(ast_ctx(self), "ir_builder_create_global failed");
1004                     return false;
1005                 }
1006                 if (vtype == TYPE_FIELD)
1007                     array->ir_values[ai]->fieldtype = elemtype->next->expression.vtype;
1008                 array->ir_values[ai]->context = ast_ctx(self);
1009             }
1010             mem_d(name);
1011         }
1012         else
1013         {
1014             v = ir_builder_create_field(ir, self->name, self->expression.next->expression.vtype);
1015             if (!v)
1016                 return false;
1017             v->context = ast_ctx(self);
1018             self->ir_v = v;
1019         }
1020         return true;
1021     }
1022
1023     if (self->expression.vtype == TYPE_ARRAY) {
1024         size_t ai;
1025         char   *name;
1026         size_t  namelen;
1027
1028         ast_expression_common *elemtype = &self->expression.next->expression;
1029         int vtype = elemtype->vtype;
1030
1031         /* same as with field arrays */
1032         if (!self->expression.count || self->expression.count > opts_max_array_size)
1033             asterror(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1034
1035         v = ir_builder_create_global(ir, self->name, vtype);
1036         if (!v) {
1037             asterror(ast_ctx(self), "ir_builder_create_global failed");
1038             return false;
1039         }
1040         if (vtype == TYPE_FIELD)
1041             v->fieldtype = elemtype->next->expression.vtype;
1042         v->context = ast_ctx(self);
1043
1044         namelen = strlen(self->name);
1045         name    = (char*)mem_a(namelen + 16);
1046         strcpy(name, self->name);
1047
1048         self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1049         self->ir_values[0] = v;
1050         for (ai = 1; ai < self->expression.count; ++ai) {
1051             snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1052             self->ir_values[ai] = ir_builder_create_global(ir, name, vtype);
1053             if (!self->ir_values[ai]) {
1054                 mem_d(name);
1055                 asterror(ast_ctx(self), "ir_builder_create_global failed");
1056                 return false;
1057             }
1058             if (vtype == TYPE_FIELD)
1059                 self->ir_values[ai]->fieldtype = elemtype->next->expression.vtype;
1060             self->ir_values[ai]->context = ast_ctx(self);
1061         }
1062         mem_d(name);
1063     }
1064     else
1065     {
1066         /* Arrays don't do this since there's no "array" value which spans across the
1067          * whole thing.
1068          */
1069         v = ir_builder_create_global(ir, self->name, self->expression.vtype);
1070         if (!v) {
1071             asterror(ast_ctx(self), "ir_builder_create_global failed");
1072             return false;
1073         }
1074         if (self->expression.vtype == TYPE_FIELD)
1075             v->fieldtype = self->expression.next->expression.vtype;
1076         v->context = ast_ctx(self);
1077     }
1078
1079     if (self->isconst) {
1080         switch (self->expression.vtype)
1081         {
1082             case TYPE_FLOAT:
1083                 if (!ir_value_set_float(v, self->constval.vfloat))
1084                     goto error;
1085                 break;
1086             case TYPE_VECTOR:
1087                 if (!ir_value_set_vector(v, self->constval.vvec))
1088                     goto error;
1089                 break;
1090             case TYPE_STRING:
1091                 if (!ir_value_set_string(v, self->constval.vstring))
1092                     goto error;
1093                 break;
1094             case TYPE_ARRAY:
1095                 asterror(ast_ctx(self), "TODO: global constant array");
1096                 break;
1097             case TYPE_FUNCTION:
1098                 asterror(ast_ctx(self), "global of type function not properly generated");
1099                 goto error;
1100                 /* Cannot generate an IR value for a function,
1101                  * need a pointer pointing to a function rather.
1102                  */
1103             default:
1104                 asterror(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1105                 break;
1106         }
1107     }
1108
1109     /* link us to the ir_value */
1110     self->ir_v = v;
1111     return true;
1112
1113 error: /* clean up */
1114     ir_value_delete(v);
1115     return false;
1116 }
1117
1118 bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
1119 {
1120     ir_value *v = NULL;
1121     if (self->isconst && self->expression.vtype == TYPE_FUNCTION)
1122     {
1123         /* Do we allow local functions? I think not...
1124          * this is NOT a function pointer atm.
1125          */
1126         return false;
1127     }
1128
1129     if (self->expression.vtype == TYPE_ARRAY) {
1130         size_t ai;
1131         char   *name;
1132         size_t  namelen;
1133
1134         ast_expression_common *elemtype = &self->expression.next->expression;
1135         int vtype = elemtype->vtype;
1136
1137         if (param) {
1138             asterror(ast_ctx(self), "array-parameters are not supported");
1139             return false;
1140         }
1141
1142         /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1143         if (!self->expression.count || self->expression.count > opts_max_array_size) {
1144             asterror(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1145         }
1146
1147         self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1148         if (!self->ir_values) {
1149             asterror(ast_ctx(self), "failed to allocate array values");
1150             return false;
1151         }
1152
1153         v = ir_function_create_local(func, self->name, vtype, param);
1154         if (!v) {
1155             asterror(ast_ctx(self), "ir_function_create_local failed");
1156             return false;
1157         }
1158         if (vtype == TYPE_FIELD)
1159             v->fieldtype = elemtype->next->expression.vtype;
1160         v->context = ast_ctx(self);
1161
1162         namelen = strlen(self->name);
1163         name    = (char*)mem_a(namelen + 16);
1164         strcpy(name, self->name);
1165
1166         self->ir_values[0] = v;
1167         for (ai = 1; ai < self->expression.count; ++ai) {
1168             snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1169             self->ir_values[ai] = ir_function_create_local(func, name, vtype, param);
1170             if (!self->ir_values[ai]) {
1171                 asterror(ast_ctx(self), "ir_builder_create_global failed");
1172                 return false;
1173             }
1174             if (vtype == TYPE_FIELD)
1175                 self->ir_values[ai]->fieldtype = elemtype->next->expression.vtype;
1176             self->ir_values[ai]->context = ast_ctx(self);
1177         }
1178     }
1179     else
1180     {
1181         v = ir_function_create_local(func, self->name, self->expression.vtype, param);
1182         if (!v)
1183             return false;
1184         if (self->expression.vtype == TYPE_FIELD)
1185             v->fieldtype = self->expression.next->expression.vtype;
1186         v->context = ast_ctx(self);
1187     }
1188
1189     /* A constant local... hmmm...
1190      * I suppose the IR will have to deal with this
1191      */
1192     if (self->isconst) {
1193         switch (self->expression.vtype)
1194         {
1195             case TYPE_FLOAT:
1196                 if (!ir_value_set_float(v, self->constval.vfloat))
1197                     goto error;
1198                 break;
1199             case TYPE_VECTOR:
1200                 if (!ir_value_set_vector(v, self->constval.vvec))
1201                     goto error;
1202                 break;
1203             case TYPE_STRING:
1204                 if (!ir_value_set_string(v, self->constval.vstring))
1205                     goto error;
1206                 break;
1207             default:
1208                 asterror(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1209                 break;
1210         }
1211     }
1212
1213     /* link us to the ir_value */
1214     self->ir_v = v;
1215
1216     if (self->setter) {
1217         if (!ast_global_codegen(self->setter, func->owner, false) ||
1218             !ast_function_codegen(self->setter->constval.vfunc, func->owner) ||
1219             !ir_function_finalize(self->setter->constval.vfunc->ir_func))
1220             return false;
1221     }
1222     if (self->getter) {
1223         if (!ast_global_codegen(self->getter, func->owner, false) ||
1224             !ast_function_codegen(self->getter->constval.vfunc, func->owner) ||
1225             !ir_function_finalize(self->getter->constval.vfunc->ir_func))
1226             return false;
1227     }
1228     return true;
1229
1230 error: /* clean up */
1231     ir_value_delete(v);
1232     return false;
1233 }
1234
1235 bool ast_function_codegen(ast_function *self, ir_builder *ir)
1236 {
1237     ir_function *irf;
1238     ir_value    *dummy;
1239     ast_expression_common *ec;
1240     size_t    i;
1241
1242     irf = self->ir_func;
1243     if (!irf) {
1244         asterror(ast_ctx(self), "ast_function's related ast_value was not generated yet");
1245         return false;
1246     }
1247
1248     /* fill the parameter list */
1249     ec = &self->vtype->expression;
1250     for (i = 0; i < vec_size(ec->params); ++i)
1251     {
1252         vec_push(irf->params, ec->params[i]->expression.vtype);
1253         if (!self->builtin) {
1254             if (!ast_local_codegen(ec->params[i], self->ir_func, true))
1255                 return false;
1256         }
1257     }
1258
1259     if (self->builtin) {
1260         irf->builtin = self->builtin;
1261         return true;
1262     }
1263
1264     if (!vec_size(self->blocks)) {
1265         asterror(ast_ctx(self), "function `%s` has no body", self->name);
1266         return false;
1267     }
1268
1269     self->curblock = ir_function_create_block(irf, "entry");
1270     if (!self->curblock) {
1271         asterror(ast_ctx(self), "failed to allocate entry block for `%s`", self->name);
1272         return false;
1273     }
1274
1275     for (i = 0; i < vec_size(self->blocks); ++i) {
1276         ast_expression_codegen *gen = self->blocks[i]->expression.codegen;
1277         if (!(*gen)((ast_expression*)self->blocks[i], self, false, &dummy))
1278             return false;
1279     }
1280
1281     /* TODO: check return types */
1282     if (!self->curblock->is_return)
1283     {
1284         return ir_block_create_return(self->curblock, NULL);
1285         /* From now on the parser has to handle this situation */
1286 #if 0
1287         if (!self->vtype->expression.next ||
1288             self->vtype->expression.next->expression.vtype == TYPE_VOID)
1289         {
1290             return ir_block_create_return(self->curblock, NULL);
1291         }
1292         else
1293         {
1294             /* error("missing return"); */
1295             asterror(ast_ctx(self), "function `%s` missing return value", self->name);
1296             return false;
1297         }
1298 #endif
1299     }
1300     return true;
1301 }
1302
1303 /* Note, you will not see ast_block_codegen generate ir_blocks.
1304  * To the AST and the IR, blocks are 2 different things.
1305  * In the AST it represents a block of code, usually enclosed in
1306  * curly braces {...}.
1307  * While in the IR it represents a block in terms of control-flow.
1308  */
1309 bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_value **out)
1310 {
1311     size_t i;
1312
1313     /* We don't use this
1314      * Note: an ast-representation using the comma-operator
1315      * of the form: (a, b, c) = x should not assign to c...
1316      */
1317     if (lvalue) {
1318         asterror(ast_ctx(self), "not an l-value (code-block)");
1319         return false;
1320     }
1321
1322     if (self->expression.outr) {
1323         *out = self->expression.outr;
1324         return true;
1325     }
1326
1327     /* output is NULL at first, we'll have each expression
1328      * assign to out output, thus, a comma-operator represention
1329      * using an ast_block will return the last generated value,
1330      * so: (b, c) + a  executed both b and c, and returns c,
1331      * which is then added to a.
1332      */
1333     *out = NULL;
1334
1335     /* generate locals */
1336     for (i = 0; i < vec_size(self->locals); ++i)
1337     {
1338         if (!ast_local_codegen(self->locals[i], func->ir_func, false)) {
1339             if (opts_debug)
1340                 asterror(ast_ctx(self), "failed to generate local `%s`", self->locals[i]->name);
1341             return false;
1342         }
1343     }
1344
1345     for (i = 0; i < vec_size(self->exprs); ++i)
1346     {
1347         ast_expression_codegen *gen = self->exprs[i]->expression.codegen;
1348         if (!(*gen)(self->exprs[i], func, false, out))
1349             return false;
1350     }
1351
1352     self->expression.outr = *out;
1353
1354     return true;
1355 }
1356
1357 bool ast_store_codegen(ast_store *self, ast_function *func, bool lvalue, ir_value **out)
1358 {
1359     ast_expression_codegen *cgen;
1360     ir_value *left, *right;
1361
1362     ast_value       *arr;
1363     ast_value       *idx;
1364     ast_array_index *ai = NULL;
1365
1366     if (lvalue && self->expression.outl) {
1367         *out = self->expression.outl;
1368         return true;
1369     }
1370
1371     if (!lvalue && self->expression.outr) {
1372         *out = self->expression.outr;
1373         return true;
1374     }
1375
1376     if (ast_istype(self->dest, ast_array_index))
1377     {
1378
1379         ai = (ast_array_index*)self->dest;
1380         idx = (ast_value*)ai->index;
1381
1382         if (ast_istype(ai->index, ast_value) && idx->isconst)
1383             ai = NULL;
1384     }
1385
1386     if (ai) {
1387         /* we need to call the setter */
1388         ir_value  *iridx, *funval;
1389         ir_instr  *call;
1390
1391         if (lvalue) {
1392             asterror(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1393             return false;
1394         }
1395
1396         arr = (ast_value*)ai->array;
1397         if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1398             asterror(ast_ctx(self), "value has no setter (%s)", arr->name);
1399             return false;
1400         }
1401
1402         cgen = idx->expression.codegen;
1403         if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1404             return false;
1405
1406         cgen = arr->setter->expression.codegen;
1407         if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
1408             return false;
1409
1410         cgen = self->source->expression.codegen;
1411         if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1412             return false;
1413
1414         call = ir_block_create_call(func->curblock, ast_function_label(func, "store"), funval);
1415         if (!call)
1416             return false;
1417         ir_call_param(call, iridx);
1418         ir_call_param(call, right);
1419         self->expression.outr = right;
1420     }
1421     else
1422     {
1423         /* regular code */
1424
1425         cgen = self->dest->expression.codegen;
1426         /* lvalue! */
1427         if (!(*cgen)((ast_expression*)(self->dest), func, true, &left))
1428             return false;
1429         self->expression.outl = left;
1430
1431         cgen = self->source->expression.codegen;
1432         /* rvalue! */
1433         if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1434             return false;
1435
1436         if (!ir_block_create_store_op(func->curblock, self->op, left, right))
1437             return false;
1438         self->expression.outr = right;
1439     }
1440
1441     /* Theoretically, an assinment returns its left side as an
1442      * lvalue, if we don't need an lvalue though, we return
1443      * the right side as an rvalue, otherwise we have to
1444      * somehow know whether or not we need to dereference the pointer
1445      * on the left side - that is: OP_LOAD if it was an address.
1446      * Also: in original QC we cannot OP_LOADP *anyway*.
1447      */
1448     *out = (lvalue ? left : right);
1449
1450     return true;
1451 }
1452
1453 bool ast_binary_codegen(ast_binary *self, ast_function *func, bool lvalue, ir_value **out)
1454 {
1455     ast_expression_codegen *cgen;
1456     ir_value *left, *right;
1457
1458     /* A binary operation cannot yield an l-value */
1459     if (lvalue) {
1460         asterror(ast_ctx(self), "not an l-value (binop)");
1461         return false;
1462     }
1463
1464     if (self->expression.outr) {
1465         *out = self->expression.outr;
1466         return true;
1467     }
1468
1469     cgen = self->left->expression.codegen;
1470     /* lvalue! */
1471     if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1472         return false;
1473
1474     cgen = self->right->expression.codegen;
1475     /* rvalue! */
1476     if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1477         return false;
1478
1479     *out = ir_block_create_binop(func->curblock, ast_function_label(func, "bin"),
1480                                  self->op, left, right);
1481     if (!*out)
1482         return false;
1483     self->expression.outr = *out;
1484
1485     return true;
1486 }
1487
1488 bool ast_binstore_codegen(ast_binstore *self, ast_function *func, bool lvalue, ir_value **out)
1489 {
1490     ast_expression_codegen *cgen;
1491     ir_value *leftl, *leftr, *right, *bin;
1492
1493     if (lvalue && self->expression.outl) {
1494         *out = self->expression.outl;
1495         return true;
1496     }
1497
1498     if (!lvalue && self->expression.outr) {
1499         *out = self->expression.outr;
1500         return true;
1501     }
1502
1503     /* for a binstore we need both an lvalue and an rvalue for the left side */
1504     /* rvalue of destination! */
1505     cgen = self->dest->expression.codegen;
1506     if (!(*cgen)((ast_expression*)(self->dest), func, false, &leftr))
1507         return false;
1508
1509     /* source as rvalue only */
1510     cgen = self->source->expression.codegen;
1511     if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1512         return false;
1513
1514     /* now the binary */
1515     bin = ir_block_create_binop(func->curblock, ast_function_label(func, "binst"),
1516                                 self->opbin, leftr, right);
1517     self->expression.outr = bin;
1518
1519     /* now store them */
1520     cgen = self->dest->expression.codegen;
1521     /* lvalue of destination */
1522     if (!(*cgen)((ast_expression*)(self->dest), func, true, &leftl))
1523         return false;
1524     self->expression.outl = leftl;
1525
1526     if (!ir_block_create_store_op(func->curblock, self->opstore, leftl, bin))
1527         return false;
1528     self->expression.outr = bin;
1529
1530     /* Theoretically, an assinment returns its left side as an
1531      * lvalue, if we don't need an lvalue though, we return
1532      * the right side as an rvalue, otherwise we have to
1533      * somehow know whether or not we need to dereference the pointer
1534      * on the left side - that is: OP_LOAD if it was an address.
1535      * Also: in original QC we cannot OP_LOADP *anyway*.
1536      */
1537     *out = (lvalue ? leftl : bin);
1538
1539     return true;
1540 }
1541
1542 bool ast_unary_codegen(ast_unary *self, ast_function *func, bool lvalue, ir_value **out)
1543 {
1544     ast_expression_codegen *cgen;
1545     ir_value *operand;
1546
1547     /* An unary operation cannot yield an l-value */
1548     if (lvalue) {
1549         asterror(ast_ctx(self), "not an l-value (binop)");
1550         return false;
1551     }
1552
1553     if (self->expression.outr) {
1554         *out = self->expression.outr;
1555         return true;
1556     }
1557
1558     cgen = self->operand->expression.codegen;
1559     /* lvalue! */
1560     if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
1561         return false;
1562
1563     *out = ir_block_create_unary(func->curblock, ast_function_label(func, "unary"),
1564                                  self->op, operand);
1565     if (!*out)
1566         return false;
1567     self->expression.outr = *out;
1568
1569     return true;
1570 }
1571
1572 bool ast_return_codegen(ast_return *self, ast_function *func, bool lvalue, ir_value **out)
1573 {
1574     ast_expression_codegen *cgen;
1575     ir_value *operand;
1576
1577     /* In the context of a return operation, we don't actually return
1578      * anything...
1579      */
1580     if (lvalue) {
1581         asterror(ast_ctx(self), "return-expression is not an l-value");
1582         return false;
1583     }
1584
1585     if (self->expression.outr) {
1586         asterror(ast_ctx(self), "internal error: ast_return cannot be reused, it bears no result!");
1587         return false;
1588     }
1589     self->expression.outr = (ir_value*)1;
1590
1591     if (self->operand) {
1592         cgen = self->operand->expression.codegen;
1593         /* lvalue! */
1594         if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
1595             return false;
1596
1597         if (!ir_block_create_return(func->curblock, operand))
1598             return false;
1599     } else {
1600         if (!ir_block_create_return(func->curblock, NULL))
1601             return false;
1602     }
1603
1604     return true;
1605 }
1606
1607 bool ast_entfield_codegen(ast_entfield *self, ast_function *func, bool lvalue, ir_value **out)
1608 {
1609     ast_expression_codegen *cgen;
1610     ir_value *ent, *field;
1611
1612     /* This function needs to take the 'lvalue' flag into account!
1613      * As lvalue we provide a field-pointer, as rvalue we provide the
1614      * value in a temp.
1615      */
1616
1617     if (lvalue && self->expression.outl) {
1618         *out = self->expression.outl;
1619         return true;
1620     }
1621
1622     if (!lvalue && self->expression.outr) {
1623         *out = self->expression.outr;
1624         return true;
1625     }
1626
1627     cgen = self->entity->expression.codegen;
1628     if (!(*cgen)((ast_expression*)(self->entity), func, false, &ent))
1629         return false;
1630
1631     cgen = self->field->expression.codegen;
1632     if (!(*cgen)((ast_expression*)(self->field), func, false, &field))
1633         return false;
1634
1635     if (lvalue) {
1636         /* address! */
1637         *out = ir_block_create_fieldaddress(func->curblock, ast_function_label(func, "efa"),
1638                                             ent, field);
1639     } else {
1640         *out = ir_block_create_load_from_ent(func->curblock, ast_function_label(func, "efv"),
1641                                              ent, field, self->expression.vtype);
1642     }
1643     if (!*out) {
1644         asterror(ast_ctx(self), "failed to create %s instruction (output type %s)",
1645                  (lvalue ? "ADDRESS" : "FIELD"),
1646                  type_name[self->expression.vtype]);
1647         return false;
1648     }
1649
1650     if (lvalue)
1651         self->expression.outl = *out;
1652     else
1653         self->expression.outr = *out;
1654
1655     /* Hm that should be it... */
1656     return true;
1657 }
1658
1659 bool ast_member_codegen(ast_member *self, ast_function *func, bool lvalue, ir_value **out)
1660 {
1661     ast_expression_codegen *cgen;
1662     ir_value *vec;
1663
1664     /* in QC this is always an lvalue */
1665     (void)lvalue;
1666     if (self->expression.outl) {
1667         *out = self->expression.outl;
1668         return true;
1669     }
1670
1671     cgen = self->owner->expression.codegen;
1672     if (!(*cgen)((ast_expression*)(self->owner), func, true, &vec))
1673         return false;
1674
1675     if (vec->vtype != TYPE_VECTOR &&
1676         !(vec->vtype == TYPE_FIELD && self->owner->expression.next->expression.vtype == TYPE_VECTOR))
1677     {
1678         return false;
1679     }
1680
1681     *out = ir_value_vector_member(vec, self->field);
1682     self->expression.outl = *out;
1683
1684     return (*out != NULL);
1685 }
1686
1687 bool ast_array_index_codegen(ast_array_index *self, ast_function *func, bool lvalue, ir_value **out)
1688 {
1689     ast_value *arr;
1690     ast_value *idx;
1691
1692     if (!lvalue && self->expression.outr) {
1693         *out = self->expression.outr;
1694     }
1695     if (lvalue && self->expression.outl) {
1696         *out = self->expression.outl;
1697     }
1698
1699     if (!ast_istype(self->array, ast_value)) {
1700         asterror(ast_ctx(self), "array indexing this way is not supported");
1701         /* note this would actually be pointer indexing because the left side is
1702          * not an actual array but (hopefully) an indexable expression.
1703          * Once we get integer arithmetic, and GADDRESS/GSTORE/GLOAD instruction
1704          * support this path will be filled.
1705          */
1706         return false;
1707     }
1708
1709     arr = (ast_value*)self->array;
1710     idx = (ast_value*)self->index;
1711
1712     if (!ast_istype(self->index, ast_value) || !idx->isconst) {
1713         /* Time to use accessor functions */
1714         ast_expression_codegen *cgen;
1715         ir_value               *iridx, *funval;
1716         ir_instr               *call;
1717
1718         if (lvalue) {
1719             asterror(ast_ctx(self), "(.2) array indexing here needs a compile-time constant");
1720             return false;
1721         }
1722
1723         if (!arr->getter) {
1724             asterror(ast_ctx(self), "value has no getter, don't know how to index it");
1725             return false;
1726         }
1727
1728         cgen = self->index->expression.codegen;
1729         if (!(*cgen)((ast_expression*)(self->index), func, true, &iridx))
1730             return false;
1731
1732         cgen = arr->getter->expression.codegen;
1733         if (!(*cgen)((ast_expression*)(arr->getter), func, true, &funval))
1734             return false;
1735
1736         call = ir_block_create_call(func->curblock, ast_function_label(func, "fetch"), funval);
1737         if (!call)
1738             return false;
1739         ir_call_param(call, iridx);
1740
1741         *out = ir_call_value(call);
1742         self->expression.outr = *out;
1743         return true;
1744     }
1745
1746     if (idx->expression.vtype == TYPE_FLOAT)
1747         *out = arr->ir_values[(int)idx->constval.vfloat];
1748     else if (idx->expression.vtype == TYPE_INTEGER)
1749         *out = arr->ir_values[idx->constval.vint];
1750     else {
1751         asterror(ast_ctx(self), "array indexing here needs an integer constant");
1752         return false;
1753     }
1754     return true;
1755 }
1756
1757 bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_value **out)
1758 {
1759     ast_expression_codegen *cgen;
1760
1761     ir_value *condval;
1762     ir_value *dummy;
1763
1764     ir_block *cond = func->curblock;
1765     ir_block *ontrue;
1766     ir_block *onfalse;
1767     ir_block *ontrue_endblock = NULL;
1768     ir_block *onfalse_endblock = NULL;
1769     ir_block *merge;
1770
1771     /* We don't output any value, thus also don't care about r/lvalue */
1772     (void)out;
1773     (void)lvalue;
1774
1775     if (self->expression.outr) {
1776         asterror(ast_ctx(self), "internal error: ast_ifthen cannot be reused, it bears no result!");
1777         return false;
1778     }
1779     self->expression.outr = (ir_value*)1;
1780
1781     /* generate the condition */
1782     func->curblock = cond;
1783     cgen = self->cond->expression.codegen;
1784     if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
1785         return false;
1786
1787     /* on-true path */
1788
1789     if (self->on_true) {
1790         /* create on-true block */
1791         ontrue = ir_function_create_block(func->ir_func, ast_function_label(func, "ontrue"));
1792         if (!ontrue)
1793             return false;
1794
1795         /* enter the block */
1796         func->curblock = ontrue;
1797
1798         /* generate */
1799         cgen = self->on_true->expression.codegen;
1800         if (!(*cgen)((ast_expression*)(self->on_true), func, false, &dummy))
1801             return false;
1802
1803         /* we now need to work from the current endpoint */
1804         ontrue_endblock = func->curblock;
1805     } else
1806         ontrue = NULL;
1807
1808     /* on-false path */
1809     if (self->on_false) {
1810         /* create on-false block */
1811         onfalse = ir_function_create_block(func->ir_func, ast_function_label(func, "onfalse"));
1812         if (!onfalse)
1813             return false;
1814
1815         /* enter the block */
1816         func->curblock = onfalse;
1817
1818         /* generate */
1819         cgen = self->on_false->expression.codegen;
1820         if (!(*cgen)((ast_expression*)(self->on_false), func, false, &dummy))
1821             return false;
1822
1823         /* we now need to work from the current endpoint */
1824         onfalse_endblock = func->curblock;
1825     } else
1826         onfalse = NULL;
1827
1828     /* Merge block were they all merge in to */
1829     merge = ir_function_create_block(func->ir_func, ast_function_label(func, "endif"));
1830     if (!merge)
1831         return false;
1832
1833     /* add jumps ot the merge block */
1834     if (ontrue && !ontrue_endblock->final && !ir_block_create_jump(ontrue_endblock, merge))
1835         return false;
1836     if (onfalse && !onfalse_endblock->final && !ir_block_create_jump(onfalse_endblock, merge))
1837         return false;
1838
1839     /* we create the if here, that way all blocks are ordered :)
1840      */
1841     if (!ir_block_create_if(cond, condval,
1842                             (ontrue  ? ontrue  : merge),
1843                             (onfalse ? onfalse : merge)))
1844     {
1845         return false;
1846     }
1847
1848     /* Now enter the merge block */
1849     func->curblock = merge;
1850
1851     return true;
1852 }
1853
1854 bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_value **out)
1855 {
1856     ast_expression_codegen *cgen;
1857
1858     ir_value *condval;
1859     ir_value *trueval, *falseval;
1860     ir_instr *phi;
1861
1862     ir_block *cond = func->curblock;
1863     ir_block *ontrue;
1864     ir_block *onfalse;
1865     ir_block *merge;
1866
1867     /* Ternary can never create an lvalue... */
1868     if (lvalue)
1869         return false;
1870
1871     /* In theory it shouldn't be possible to pass through a node twice, but
1872      * in case we add any kind of optimization pass for the AST itself, it
1873      * may still happen, thus we remember a created ir_value and simply return one
1874      * if it already exists.
1875      */
1876     if (self->phi_out) {
1877         *out = self->phi_out;
1878         return true;
1879     }
1880
1881     /* In the following, contraty to ast_ifthen, we assume both paths exist. */
1882
1883     /* generate the condition */
1884     func->curblock = cond;
1885     cgen = self->cond->expression.codegen;
1886     if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
1887         return false;
1888
1889     /* create on-true block */
1890     ontrue = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_T"));
1891     if (!ontrue)
1892         return false;
1893     else
1894     {
1895         /* enter the block */
1896         func->curblock = ontrue;
1897
1898         /* generate */
1899         cgen = self->on_true->expression.codegen;
1900         if (!(*cgen)((ast_expression*)(self->on_true), func, false, &trueval))
1901             return false;
1902     }
1903
1904     /* create on-false block */
1905     onfalse = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_F"));
1906     if (!onfalse)
1907         return false;
1908     else
1909     {
1910         /* enter the block */
1911         func->curblock = onfalse;
1912
1913         /* generate */
1914         cgen = self->on_false->expression.codegen;
1915         if (!(*cgen)((ast_expression*)(self->on_false), func, false, &falseval))
1916             return false;
1917     }
1918
1919     /* create merge block */
1920     merge = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_out"));
1921     if (!merge)
1922         return false;
1923     /* jump to merge block */
1924     if (!ir_block_create_jump(ontrue, merge))
1925         return false;
1926     if (!ir_block_create_jump(onfalse, merge))
1927         return false;
1928
1929     /* create if instruction */
1930     if (!ir_block_create_if(cond, condval, ontrue, onfalse))
1931         return false;
1932
1933     /* Now enter the merge block */
1934     func->curblock = merge;
1935
1936     /* Here, now, we need a PHI node
1937      * but first some sanity checking...
1938      */
1939     if (trueval->vtype != falseval->vtype) {
1940         /* error("ternary with different types on the two sides"); */
1941         return false;
1942     }
1943
1944     /* create PHI */
1945     phi = ir_block_create_phi(merge, ast_function_label(func, "phi"), trueval->vtype);
1946     if (!phi)
1947         return false;
1948     ir_phi_add(phi, ontrue,  trueval);
1949     ir_phi_add(phi, onfalse, falseval);
1950
1951     self->phi_out = ir_phi_value(phi);
1952     *out = self->phi_out;
1953
1954     return true;
1955 }
1956
1957 bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value **out)
1958 {
1959     ast_expression_codegen *cgen;
1960
1961     ir_value *dummy      = NULL;
1962     ir_value *precond    = NULL;
1963     ir_value *postcond   = NULL;
1964
1965     /* Since we insert some jumps "late" so we have blocks
1966      * ordered "nicely", we need to keep track of the actual end-blocks
1967      * of expressions to add the jumps to.
1968      */
1969     ir_block *bbody      = NULL, *end_bbody      = NULL;
1970     ir_block *bprecond   = NULL, *end_bprecond   = NULL;
1971     ir_block *bpostcond  = NULL, *end_bpostcond  = NULL;
1972     ir_block *bincrement = NULL, *end_bincrement = NULL;
1973     ir_block *bout       = NULL, *bin            = NULL;
1974
1975     /* let's at least move the outgoing block to the end */
1976     size_t    bout_id;
1977
1978     /* 'break' and 'continue' need to be able to find the right blocks */
1979     ir_block *bcontinue     = NULL;
1980     ir_block *bbreak        = NULL;
1981
1982     ir_block *old_bcontinue = NULL;
1983     ir_block *old_bbreak    = NULL;
1984
1985     ir_block *tmpblock      = NULL;
1986
1987     (void)lvalue;
1988     (void)out;
1989
1990     if (self->expression.outr) {
1991         asterror(ast_ctx(self), "internal error: ast_loop cannot be reused, it bears no result!");
1992         return false;
1993     }
1994     self->expression.outr = (ir_value*)1;
1995
1996     /* NOTE:
1997      * Should we ever need some kind of block ordering, better make this function
1998      * move blocks around than write a block ordering algorithm later... after all
1999      * the ast and ir should work together, not against each other.
2000      */
2001
2002     /* initexpr doesn't get its own block, it's pointless, it could create more blocks
2003      * anyway if for example it contains a ternary.
2004      */
2005     if (self->initexpr)
2006     {
2007         cgen = self->initexpr->expression.codegen;
2008         if (!(*cgen)((ast_expression*)(self->initexpr), func, false, &dummy))
2009             return false;
2010     }
2011
2012     /* Store the block from which we enter this chaos */
2013     bin = func->curblock;
2014
2015     /* The pre-loop condition needs its own block since we
2016      * need to be able to jump to the start of that expression.
2017      */
2018     if (self->precond)
2019     {
2020         bprecond = ir_function_create_block(func->ir_func, ast_function_label(func, "pre_loop_cond"));
2021         if (!bprecond)
2022             return false;
2023
2024         /* the pre-loop-condition the least important place to 'continue' at */
2025         bcontinue = bprecond;
2026
2027         /* enter */
2028         func->curblock = bprecond;
2029
2030         /* generate */
2031         cgen = self->precond->expression.codegen;
2032         if (!(*cgen)((ast_expression*)(self->precond), func, false, &precond))
2033             return false;
2034
2035         end_bprecond = func->curblock;
2036     } else {
2037         bprecond = end_bprecond = NULL;
2038     }
2039
2040     /* Now the next blocks won't be ordered nicely, but we need to
2041      * generate them this early for 'break' and 'continue'.
2042      */
2043     if (self->increment) {
2044         bincrement = ir_function_create_block(func->ir_func, ast_function_label(func, "loop_increment"));
2045         if (!bincrement)
2046             return false;
2047         bcontinue = bincrement; /* increment comes before the pre-loop-condition */
2048     } else {
2049         bincrement = end_bincrement = NULL;
2050     }
2051
2052     if (self->postcond) {
2053         bpostcond = ir_function_create_block(func->ir_func, ast_function_label(func, "post_loop_cond"));
2054         if (!bpostcond)
2055             return false;
2056         bcontinue = bpostcond; /* postcond comes before the increment */
2057     } else {
2058         bpostcond = end_bpostcond = NULL;
2059     }
2060
2061     bout_id = vec_size(func->ir_func->blocks);
2062     bout = ir_function_create_block(func->ir_func, ast_function_label(func, "after_loop"));
2063     if (!bout)
2064         return false;
2065     bbreak = bout;
2066
2067     /* The loop body... */
2068     if (self->body)
2069     {
2070         bbody = ir_function_create_block(func->ir_func, ast_function_label(func, "loop_body"));
2071         if (!bbody)
2072             return false;
2073
2074         /* enter */
2075         func->curblock = bbody;
2076
2077         old_bbreak          = func->breakblock;
2078         old_bcontinue       = func->continueblock;
2079         func->breakblock    = bbreak;
2080         func->continueblock = bcontinue;
2081
2082         /* generate */
2083         cgen = self->body->expression.codegen;
2084         if (!(*cgen)((ast_expression*)(self->body), func, false, &dummy))
2085             return false;
2086
2087         end_bbody = func->curblock;
2088         func->breakblock    = old_bbreak;
2089         func->continueblock = old_bcontinue;
2090     }
2091
2092     /* post-loop-condition */
2093     if (self->postcond)
2094     {
2095         /* enter */
2096         func->curblock = bpostcond;
2097
2098         /* generate */
2099         cgen = self->postcond->expression.codegen;
2100         if (!(*cgen)((ast_expression*)(self->postcond), func, false, &postcond))
2101             return false;
2102
2103         end_bpostcond = func->curblock;
2104     }
2105
2106     /* The incrementor */
2107     if (self->increment)
2108     {
2109         /* enter */
2110         func->curblock = bincrement;
2111
2112         /* generate */
2113         cgen = self->increment->expression.codegen;
2114         if (!(*cgen)((ast_expression*)(self->increment), func, false, &dummy))
2115             return false;
2116
2117         end_bincrement = func->curblock;
2118     }
2119
2120     /* In any case now, we continue from the outgoing block */
2121     func->curblock = bout;
2122
2123     /* Now all blocks are in place */
2124     /* From 'bin' we jump to whatever comes first */
2125     if      (bprecond)   tmpblock = bprecond;
2126     else if (bbody)      tmpblock = bbody;
2127     else if (bpostcond)  tmpblock = bpostcond;
2128     else                 tmpblock = bout;
2129     if (!ir_block_create_jump(bin, tmpblock))
2130         return false;
2131
2132     /* From precond */
2133     if (bprecond)
2134     {
2135         ir_block *ontrue, *onfalse;
2136         if      (bbody)      ontrue = bbody;
2137         else if (bincrement) ontrue = bincrement;
2138         else if (bpostcond)  ontrue = bpostcond;
2139         else                 ontrue = bprecond;
2140         onfalse = bout;
2141         if (!ir_block_create_if(end_bprecond, precond, ontrue, onfalse))
2142             return false;
2143     }
2144
2145     /* from body */
2146     if (bbody)
2147     {
2148         if      (bincrement) tmpblock = bincrement;
2149         else if (bpostcond)  tmpblock = bpostcond;
2150         else if (bprecond)   tmpblock = bprecond;
2151         else                 tmpblock = bout;
2152         if (!end_bbody->final && !ir_block_create_jump(end_bbody, tmpblock))
2153             return false;
2154     }
2155
2156     /* from increment */
2157     if (bincrement)
2158     {
2159         if      (bpostcond)  tmpblock = bpostcond;
2160         else if (bprecond)   tmpblock = bprecond;
2161         else if (bbody)      tmpblock = bbody;
2162         else                 tmpblock = bout;
2163         if (!ir_block_create_jump(end_bincrement, tmpblock))
2164             return false;
2165     }
2166
2167     /* from postcond */
2168     if (bpostcond)
2169     {
2170         ir_block *ontrue, *onfalse;
2171         if      (bprecond)   ontrue = bprecond;
2172         else if (bbody)      ontrue = bbody;
2173         else if (bincrement) ontrue = bincrement;
2174         else                 ontrue = bpostcond;
2175         onfalse = bout;
2176         if (!ir_block_create_if(end_bpostcond, postcond, ontrue, onfalse))
2177             return false;
2178     }
2179
2180     /* Move 'bout' to the end */
2181     vec_remove(func->ir_func->blocks, bout_id, 1);
2182     vec_push(func->ir_func->blocks, bout);
2183
2184     return true;
2185 }
2186
2187 bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value **out)
2188 {
2189     ast_expression_codegen *cgen;
2190     ir_value              **params;
2191     ir_instr               *callinstr;
2192     size_t i;
2193
2194     ir_value *funval = NULL;
2195
2196     /* return values are never lvalues */
2197     if (lvalue) {
2198         asterror(ast_ctx(self), "not an l-value (function call)");
2199         return false;
2200     }
2201
2202     if (self->expression.outr) {
2203         *out = self->expression.outr;
2204         return true;
2205     }
2206
2207     cgen = self->func->expression.codegen;
2208     if (!(*cgen)((ast_expression*)(self->func), func, false, &funval))
2209         return false;
2210     if (!funval)
2211         return false;
2212
2213     params = NULL;
2214
2215     /* parameters */
2216     for (i = 0; i < vec_size(self->params); ++i)
2217     {
2218         ir_value *param;
2219         ast_expression *expr = self->params[i];
2220
2221         cgen = expr->expression.codegen;
2222         if (!(*cgen)(expr, func, false, &param))
2223             goto error;
2224         if (!param)
2225             goto error;
2226         vec_push(params, param);
2227     }
2228
2229     callinstr = ir_block_create_call(func->curblock, ast_function_label(func, "call"), funval);
2230     if (!callinstr)
2231         goto error;
2232
2233     for (i = 0; i < vec_size(params); ++i) {
2234         ir_call_param(callinstr, params[i]);
2235     }
2236
2237     *out = ir_call_value(callinstr);
2238     self->expression.outr = *out;
2239
2240     vec_free(params);
2241     return true;
2242 error:
2243     vec_free(params);
2244     return false;
2245 }