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