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