]> git.xonotic.org Git - xonotic/gmqcc.git/blob - ast.c
initialize the ast_expression node in ast_type_copy properly
[xonotic/gmqcc.git] / ast.c
1 /*
2  * Copyright (C) 2012
3  *     Wolfgang Bumiller
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is furnished to do
10  * so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "gmqcc.h"
28 #include "ast.h"
29
30 #define ast_instantiate(T, ctx, destroyfn)                          \
31     T* self = (T*)mem_a(sizeof(T));                                 \
32     if (!self) {                                                    \
33         return NULL;                                                \
34     }                                                               \
35     ast_node_init((ast_node*)self, ctx, TYPE_##T);                  \
36     ( (ast_node*)self )->node.destroy = (ast_node_delete*)destroyfn
37
38 /* error handling */
39 static void asterror(lex_ctx ctx, const char *msg, ...)
40 {
41     va_list ap;
42     va_start(ap, msg);
43     cvprintmsg(ctx, LVL_ERROR, "error", msg, ap);
44     va_end(ap);
45 }
46
47 /* It must not be possible to get here. */
48 static GMQCC_NORETURN void _ast_node_destroy(ast_node *self)
49 {
50     fprintf(stderr, "ast node missing destroy()\n");
51     abort();
52 }
53
54 /* Initialize main ast node aprts */
55 static void ast_node_init(ast_node *self, lex_ctx ctx, int nodetype)
56 {
57     self->node.context = ctx;
58     self->node.destroy = &_ast_node_destroy;
59     self->node.keep    = false;
60     self->node.nodetype = nodetype;
61 }
62
63 /* General expression initialization */
64 static void ast_expression_init(ast_expression *self,
65                                 ast_expression_codegen *codegen)
66 {
67     self->expression.codegen = codegen;
68     self->expression.vtype   = TYPE_VOID;
69     self->expression.next    = NULL;
70     self->expression.outl    = NULL;
71     self->expression.outr    = NULL;
72     MEM_VECTOR_INIT(&self->expression, params);
73 }
74
75 static void ast_expression_delete(ast_expression *self)
76 {
77     size_t i;
78     if (self->expression.next)
79         ast_delete(self->expression.next);
80     for (i = 0; i < self->expression.params_count; ++i) {
81         ast_delete(self->expression.params[i]);
82     }
83     MEM_VECTOR_CLEAR(&self->expression, params);
84 }
85
86 static void ast_expression_delete_full(ast_expression *self)
87 {
88     ast_expression_delete(self);
89     mem_d(self);
90 }
91
92 MEM_VEC_FUNCTIONS(ast_expression_common, ast_value*, params)
93
94 static ast_expression* ast_type_copy(lex_ctx ctx, const ast_expression *ex);
95 static ast_value* ast_value_copy(const ast_value *self)
96 {
97     ast_value *cp = ast_value_new(self->expression.node.context, self->name, self->expression.vtype);
98     if (self->expression.next) {
99         cp->expression.next = ast_type_copy(self->expression.node.context, self->expression.next);
100         if (!cp->expression.next) {
101             ast_value_delete(cp);
102             return NULL;
103         }
104     }
105     return cp;
106 }
107
108 static ast_expression* ast_shallow_type(lex_ctx ctx, int vtype)
109 {
110     ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
111     self->expression.codegen = NULL;
112     self->expression.next    = NULL;
113     self->expression.vtype   = vtype;
114     return self;
115 }
116
117 static ast_expression* ast_type_copy(lex_ctx ctx, const ast_expression *ex)
118 {
119     size_t i;
120     const ast_expression_common *fromex;
121     ast_expression_common *selfex;
122
123     if (!ex)
124         return NULL;
125     else
126     {
127         ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
128         ast_expression_init(self, NULL);
129
130         fromex   = &ex->expression;
131         selfex = &self->expression;
132
133         /* This may never be codegen()d */
134         selfex->codegen = NULL;
135
136         selfex->vtype = fromex->vtype;
137         if (fromex->next)
138         {
139             selfex->next = ast_type_copy(ctx, fromex->next);
140             if (!selfex->next) {
141                 ast_expression_delete_full(self);
142                 return NULL;
143             }
144         }
145         else
146             selfex->next = NULL;
147
148         for (i = 0; i < fromex->params_count; ++i) {
149             ast_value *v = ast_value_copy(fromex->params[i]);
150             if (!v || !ast_expression_common_params_add(selfex, v)) {
151                 ast_expression_delete_full(self);
152                 return NULL;
153             }
154         }
155
156         return self;
157     }
158 }
159
160 bool ast_compare_type(ast_expression *a, ast_expression *b)
161 {
162     if (a->expression.vtype != b->expression.vtype)
163         return false;
164     if (!a->expression.next != !b->expression.next)
165         return false;
166     if (a->expression.params_count != b->expression.params_count)
167         return false;
168     if (a->expression.params_count) {
169         size_t i;
170         for (i = 0; i < a->expression.params_count; ++i) {
171             if (!ast_compare_type((ast_expression*)a->expression.params[i],
172                                   (ast_expression*)b->expression.params[i]))
173                 return false;
174         }
175     }
176     if (a->expression.next)
177         return ast_compare_type(a->expression.next, b->expression.next);
178     return true;
179 }
180
181 ast_value* ast_value_new(lex_ctx ctx, const char *name, int t)
182 {
183     ast_instantiate(ast_value, ctx, ast_value_delete);
184     ast_expression_init((ast_expression*)self,
185                         (ast_expression_codegen*)&ast_value_codegen);
186     self->expression.node.keep = true; /* keep */
187
188     self->name = name ? util_strdup(name) : NULL;
189     self->expression.vtype = t;
190     self->expression.next  = NULL;
191     self->isconst = false;
192     memset(&self->constval, 0, sizeof(self->constval));
193
194     self->ir_v    = NULL;
195
196     return self;
197 }
198
199 void ast_value_delete(ast_value* self)
200 {
201     if (self->name)
202         mem_d((void*)self->name);
203     if (self->isconst) {
204         switch (self->expression.vtype)
205         {
206         case TYPE_STRING:
207             mem_d((void*)self->constval.vstring);
208             break;
209         case TYPE_FUNCTION:
210             /* unlink us from the function node */
211             self->constval.vfunc->vtype = NULL;
212             break;
213         /* NOTE: delete function? currently collected in
214          * the parser structure
215          */
216         default:
217             break;
218         }
219     }
220     ast_expression_delete((ast_expression*)self);
221     mem_d(self);
222 }
223
224 bool GMQCC_WARN ast_value_params_add(ast_value *self, ast_value *p)
225 {
226     return ast_expression_common_params_add(&self->expression, p);
227 }
228
229 bool ast_value_set_name(ast_value *self, const char *name)
230 {
231     if (self->name)
232         mem_d((void*)self->name);
233     self->name = util_strdup(name);
234     return !!self->name;
235 }
236
237 ast_binary* ast_binary_new(lex_ctx ctx, int op,
238                            ast_expression* left, ast_expression* right)
239 {
240     ast_instantiate(ast_binary, ctx, ast_binary_delete);
241     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binary_codegen);
242
243     self->op = op;
244     self->left = left;
245     self->right = right;
246
247     if (op >= INSTR_EQ_F && op <= INSTR_GT)
248         self->expression.vtype = TYPE_FLOAT;
249     else if (op == INSTR_AND || op == INSTR_OR ||
250              op == INSTR_BITAND || op == INSTR_BITOR)
251         self->expression.vtype = TYPE_FLOAT;
252     else if (op == INSTR_MUL_VF || op == INSTR_MUL_FV)
253         self->expression.vtype = TYPE_VECTOR;
254     else if (op == INSTR_MUL_V)
255         self->expression.vtype = TYPE_FLOAT;
256     else
257         self->expression.vtype = left->expression.vtype;
258
259     return self;
260 }
261
262 void ast_binary_delete(ast_binary *self)
263 {
264     ast_unref(self->left);
265     ast_unref(self->right);
266     ast_expression_delete((ast_expression*)self);
267     mem_d(self);
268 }
269
270 ast_binstore* ast_binstore_new(lex_ctx ctx, int storop, int op,
271                                ast_expression* left, ast_expression* right)
272 {
273     ast_instantiate(ast_binstore, ctx, ast_binstore_delete);
274     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binstore_codegen);
275
276     self->opstore = storop;
277     self->opbin   = op;
278     self->dest    = left;
279     self->source  = right;
280
281     self->expression.vtype = left->expression.vtype;
282     if (left->expression.next) {
283         self->expression.next = ast_type_copy(ctx, left);
284         if (!self->expression.next) {
285             ast_delete(self);
286             return NULL;
287         }
288     }
289     else
290         self->expression.next = NULL;
291
292     return self;
293 }
294
295 void ast_binstore_delete(ast_binstore *self)
296 {
297     ast_unref(self->dest);
298     ast_unref(self->source);
299     ast_expression_delete((ast_expression*)self);
300     mem_d(self);
301 }
302
303 ast_unary* ast_unary_new(lex_ctx ctx, int op,
304                          ast_expression *expr)
305 {
306     ast_instantiate(ast_unary, ctx, ast_unary_delete);
307     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_unary_codegen);
308
309     self->op = op;
310     self->operand = expr;
311
312     return self;
313 }
314
315 void ast_unary_delete(ast_unary *self)
316 {
317     ast_unref(self->operand);
318     ast_expression_delete((ast_expression*)self);
319     mem_d(self);
320 }
321
322 ast_return* ast_return_new(lex_ctx ctx, ast_expression *expr)
323 {
324     ast_instantiate(ast_return, ctx, ast_return_delete);
325     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_return_codegen);
326
327     self->operand = expr;
328
329     return self;
330 }
331
332 void ast_return_delete(ast_return *self)
333 {
334     ast_unref(self->operand);
335     ast_expression_delete((ast_expression*)self);
336     mem_d(self);
337 }
338
339 ast_entfield* ast_entfield_new(lex_ctx ctx, ast_expression *entity, ast_expression *field)
340 {
341     const ast_expression *outtype;
342
343     ast_instantiate(ast_entfield, ctx, ast_entfield_delete);
344
345     if (field->expression.vtype != TYPE_FIELD) {
346         mem_d(self);
347         return NULL;
348     }
349
350     outtype = field->expression.next;
351     if (!outtype) {
352         mem_d(self);
353         /* Error: field has no type... */
354         return NULL;
355     }
356
357     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_entfield_codegen);
358
359     self->expression.vtype = outtype->expression.vtype;
360     self->expression.next  = ast_type_copy(ctx, outtype->expression.next);
361
362     self->entity = entity;
363     self->field  = field;
364
365     return self;
366 }
367
368 void ast_entfield_delete(ast_entfield *self)
369 {
370     ast_unref(self->entity);
371     ast_unref(self->field);
372     ast_expression_delete((ast_expression*)self);
373     mem_d(self);
374 }
375
376 ast_member* ast_member_new(lex_ctx ctx, ast_expression *owner, unsigned int field)
377 {
378     ast_instantiate(ast_member, ctx, ast_member_delete);
379     if (field >= 3) {
380         mem_d(self);
381         return NULL;
382     }
383
384     if (owner->expression.vtype != TYPE_VECTOR &&
385         owner->expression.vtype != TYPE_FIELD) {
386         asterror(ctx, "member-access on an invalid owner of type %s\n", type_name[owner->expression.vtype]);
387         mem_d(self);
388         return NULL;
389     }
390
391     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_member_codegen);
392     self->expression.node.keep = true; /* keep */
393
394     if (owner->expression.vtype == TYPE_VECTOR) {
395         self->expression.vtype = TYPE_FLOAT;
396         self->expression.next  = NULL;
397     } else {
398         self->expression.vtype = TYPE_FIELD;
399         self->expression.next = ast_shallow_type(ctx, TYPE_FLOAT);
400     }
401
402     self->owner = owner;
403     self->field = field;
404
405     return self;
406 }
407
408 void ast_member_delete(ast_member *self)
409 {
410     /* The owner is always an ast_value, which has .keep=true,
411      * also: ast_members are usually deleted after the owner, thus
412      * this will cause invalid access
413     ast_unref(self->owner);
414      * once we allow (expression).x to access a vector-member, we need
415      * to change this: preferably by creating an alternate ast node for this
416      * purpose that is not garbage-collected.
417     */
418     ast_expression_delete((ast_expression*)self);
419     mem_d(self);
420 }
421
422 ast_ifthen* ast_ifthen_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
423 {
424     ast_instantiate(ast_ifthen, ctx, ast_ifthen_delete);
425     if (!ontrue && !onfalse) {
426         /* because it is invalid */
427         mem_d(self);
428         return NULL;
429     }
430     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ifthen_codegen);
431
432     self->cond     = cond;
433     self->on_true  = ontrue;
434     self->on_false = onfalse;
435
436     return self;
437 }
438
439 void ast_ifthen_delete(ast_ifthen *self)
440 {
441     ast_unref(self->cond);
442     if (self->on_true)
443         ast_unref(self->on_true);
444     if (self->on_false)
445         ast_unref(self->on_false);
446     ast_expression_delete((ast_expression*)self);
447     mem_d(self);
448 }
449
450 ast_ternary* ast_ternary_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
451 {
452     ast_instantiate(ast_ternary, ctx, ast_ternary_delete);
453     /* This time NEITHER must be NULL */
454     if (!ontrue || !onfalse) {
455         mem_d(self);
456         return NULL;
457     }
458     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ternary_codegen);
459
460     self->cond     = cond;
461     self->on_true  = ontrue;
462     self->on_false = onfalse;
463     self->phi_out  = NULL;
464
465     return self;
466 }
467
468 void ast_ternary_delete(ast_ternary *self)
469 {
470     ast_unref(self->cond);
471     ast_unref(self->on_true);
472     ast_unref(self->on_false);
473     ast_expression_delete((ast_expression*)self);
474     mem_d(self);
475 }
476
477 ast_loop* ast_loop_new(lex_ctx ctx,
478                        ast_expression *initexpr,
479                        ast_expression *precond,
480                        ast_expression *postcond,
481                        ast_expression *increment,
482                        ast_expression *body)
483 {
484     ast_instantiate(ast_loop, ctx, ast_loop_delete);
485     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_loop_codegen);
486
487     self->initexpr  = initexpr;
488     self->precond   = precond;
489     self->postcond  = postcond;
490     self->increment = increment;
491     self->body      = body;
492
493     return self;
494 }
495
496 void ast_loop_delete(ast_loop *self)
497 {
498     if (self->initexpr)
499         ast_unref(self->initexpr);
500     if (self->precond)
501         ast_unref(self->precond);
502     if (self->postcond)
503         ast_unref(self->postcond);
504     if (self->increment)
505         ast_unref(self->increment);
506     if (self->body)
507         ast_unref(self->body);
508     ast_expression_delete((ast_expression*)self);
509     mem_d(self);
510 }
511
512 ast_call* ast_call_new(lex_ctx ctx,
513                        ast_expression *funcexpr)
514 {
515     ast_instantiate(ast_call, ctx, ast_call_delete);
516     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_call_codegen);
517
518     MEM_VECTOR_INIT(self, params);
519
520     self->func = funcexpr;
521
522     return self;
523 }
524 MEM_VEC_FUNCTIONS(ast_call, ast_expression*, params)
525
526 void ast_call_delete(ast_call *self)
527 {
528     size_t i;
529     for (i = 0; i < self->params_count; ++i)
530         ast_unref(self->params[i]);
531     MEM_VECTOR_CLEAR(self, params);
532
533     if (self->func)
534         ast_unref(self->func);
535
536     ast_expression_delete((ast_expression*)self);
537     mem_d(self);
538 }
539
540 ast_store* ast_store_new(lex_ctx ctx, int op,
541                          ast_expression *dest, ast_expression *source)
542 {
543     ast_instantiate(ast_store, ctx, ast_store_delete);
544     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_store_codegen);
545
546     self->op = op;
547     self->dest = dest;
548     self->source = source;
549
550     return self;
551 }
552
553 void ast_store_delete(ast_store *self)
554 {
555     ast_unref(self->dest);
556     ast_unref(self->source);
557     ast_expression_delete((ast_expression*)self);
558     mem_d(self);
559 }
560
561 ast_block* ast_block_new(lex_ctx ctx)
562 {
563     ast_instantiate(ast_block, ctx, ast_block_delete);
564     ast_expression_init((ast_expression*)self,
565                         (ast_expression_codegen*)&ast_block_codegen);
566
567     MEM_VECTOR_INIT(self, locals);
568     MEM_VECTOR_INIT(self, exprs);
569     MEM_VECTOR_INIT(self, collect);
570
571     return self;
572 }
573 MEM_VEC_FUNCTIONS(ast_block, ast_value*, locals)
574 MEM_VEC_FUNCTIONS(ast_block, ast_expression*, exprs)
575 MEM_VEC_FUNCTIONS(ast_block, ast_expression*, collect)
576
577 bool ast_block_collect(ast_block *self, ast_expression *expr)
578 {
579     if (!ast_block_collect_add(self, expr))
580         return false;
581     expr->expression.node.keep = true;
582     return true;
583 }
584
585 void ast_block_delete(ast_block *self)
586 {
587     size_t i;
588     for (i = 0; i < self->exprs_count; ++i)
589         ast_unref(self->exprs[i]);
590     MEM_VECTOR_CLEAR(self, exprs);
591     for (i = 0; i < self->locals_count; ++i)
592         ast_delete(self->locals[i]);
593     MEM_VECTOR_CLEAR(self, locals);
594     for (i = 0; i < self->collect_count; ++i)
595         ast_delete(self->collect[i]);
596     MEM_VECTOR_CLEAR(self, collect);
597     ast_expression_delete((ast_expression*)self);
598     mem_d(self);
599 }
600
601 bool ast_block_set_type(ast_block *self, ast_expression *from)
602 {
603     if (self->expression.next)
604         ast_delete(self->expression.next);
605     self->expression.vtype = from->expression.vtype;
606     if (from->expression.next) {
607         self->expression.next = ast_type_copy(self->expression.node.context, from->expression.next);
608         if (!self->expression.next)
609             return false;
610     }
611     return true;
612 }
613
614 ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype)
615 {
616     ast_instantiate(ast_function, ctx, ast_function_delete);
617
618     if (!vtype ||
619         vtype->isconst ||
620         vtype->expression.vtype != TYPE_FUNCTION)
621     {
622         mem_d(self);
623         return NULL;
624     }
625
626     self->vtype = vtype;
627     self->name = name ? util_strdup(name) : NULL;
628     MEM_VECTOR_INIT(self, blocks);
629
630     self->labelcount = 0;
631     self->builtin = 0;
632
633     self->ir_func = NULL;
634     self->curblock = NULL;
635
636     self->breakblock    = NULL;
637     self->continueblock = NULL;
638
639     vtype->isconst = true;
640     vtype->constval.vfunc = self;
641
642     return self;
643 }
644
645 MEM_VEC_FUNCTIONS(ast_function, ast_block*, blocks)
646
647 void ast_function_delete(ast_function *self)
648 {
649     size_t i;
650     if (self->name)
651         mem_d((void*)self->name);
652     if (self->vtype) {
653         /* ast_value_delete(self->vtype); */
654         self->vtype->isconst = false;
655         self->vtype->constval.vfunc = NULL;
656         /* We use unref - if it was stored in a global table it is supposed
657          * to be deleted from *there*
658          */
659         ast_unref(self->vtype);
660     }
661     for (i = 0; i < self->blocks_count; ++i)
662         ast_delete(self->blocks[i]);
663     MEM_VECTOR_CLEAR(self, blocks);
664     mem_d(self);
665 }
666
667 static void ast_util_hexitoa(char *buf, size_t size, unsigned int num)
668 {
669     unsigned int base = 10;
670 #define checknul() do { if (size == 1) { *buf = 0; return; } } while (0)
671 #define addch(x) do { *buf++ = (x); --size; checknul(); } while (0)
672     if (size < 1)
673         return;
674     checknul();
675     if (!num)
676         addch('0');
677     else {
678         while (num)
679         {
680             int digit = num % base;
681             num /= base;
682             addch('0' + digit);
683         }
684     }
685
686     *buf = 0;
687 #undef addch
688 #undef checknul
689 }
690
691 const char* ast_function_label(ast_function *self, const char *prefix)
692 {
693     size_t id = (self->labelcount++);
694     size_t len = strlen(prefix);
695     strncpy(self->labelbuf, prefix, sizeof(self->labelbuf));
696     ast_util_hexitoa(self->labelbuf + len, sizeof(self->labelbuf)-len, id);
697     return self->labelbuf;
698 }
699
700 /*********************************************************************/
701 /* AST codegen part
702  * by convention you must never pass NULL to the 'ir_value **out'
703  * parameter. If you really don't care about the output, pass a dummy.
704  * But I can't imagine a pituation where the output is truly unnecessary.
705  */
706
707 bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out)
708 {
709     /* NOTE: This is the codegen for a variable used in an expression.
710      * It is not the codegen to generate the value. For this purpose,
711      * ast_local_codegen and ast_global_codegen are to be used before this
712      * is executed. ast_function_codegen should take care of its locals,
713      * and the ast-user should take care of ast_global_codegen to be used
714      * on all the globals.
715      */
716     if (!self->ir_v) {
717         asterror(ast_ctx(self), "ast_value used before generated (%s)\n", self->name);
718         return false;
719     }
720     *out = self->ir_v;
721     return true;
722 }
723
724 bool ast_global_codegen(ast_value *self, ir_builder *ir)
725 {
726     ir_value *v = NULL;
727     if (self->isconst && self->expression.vtype == TYPE_FUNCTION)
728     {
729         ir_function *func = ir_builder_create_function(ir, self->name, self->expression.next->expression.vtype);
730         if (!func)
731             return false;
732
733         self->constval.vfunc->ir_func = func;
734         self->ir_v = func->value;
735         /* The function is filled later on ast_function_codegen... */
736         return true;
737     }
738
739     if (self->expression.vtype == TYPE_FIELD) {
740         v = ir_builder_create_field(ir, self->name, self->expression.next->expression.vtype);
741         if (!v)
742             return false;
743         if (self->isconst) {
744             asterror(ast_ctx(self), "TODO: constant field pointers with value\n");
745             goto error;
746         }
747         self->ir_v = v;
748         return true;
749     }
750
751     v = ir_builder_create_global(ir, self->name, self->expression.vtype);
752     if (!v) {
753         asterror(ast_ctx(self), "ir_builder_create_global failed\n");
754         return false;
755     }
756
757     if (self->isconst) {
758         switch (self->expression.vtype)
759         {
760             case TYPE_FLOAT:
761                 if (!ir_value_set_float(v, self->constval.vfloat))
762                     goto error;
763                 break;
764             case TYPE_VECTOR:
765                 if (!ir_value_set_vector(v, self->constval.vvec))
766                     goto error;
767                 break;
768             case TYPE_STRING:
769                 if (!ir_value_set_string(v, self->constval.vstring))
770                     goto error;
771                 break;
772             case TYPE_FUNCTION:
773                 asterror(ast_ctx(self), "global of type function not properly generated\n");
774                 goto error;
775                 /* Cannot generate an IR value for a function,
776                  * need a pointer pointing to a function rather.
777                  */
778             default:
779                 asterror(ast_ctx(self), "TODO: global constant type %i\n", self->expression.vtype);
780                 break;
781         }
782     }
783
784     /* link us to the ir_value */
785     self->ir_v = v;
786     return true;
787
788 error: /* clean up */
789     ir_value_delete(v);
790     return false;
791 }
792
793 bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
794 {
795     ir_value *v = NULL;
796     if (self->isconst && self->expression.vtype == TYPE_FUNCTION)
797     {
798         /* Do we allow local functions? I think not...
799          * this is NOT a function pointer atm.
800          */
801         return false;
802     }
803
804     v = ir_function_create_local(func, self->name, self->expression.vtype, param);
805     if (!v)
806         return false;
807
808     /* A constant local... hmmm...
809      * I suppose the IR will have to deal with this
810      */
811     if (self->isconst) {
812         switch (self->expression.vtype)
813         {
814             case TYPE_FLOAT:
815                 if (!ir_value_set_float(v, self->constval.vfloat))
816                     goto error;
817                 break;
818             case TYPE_VECTOR:
819                 if (!ir_value_set_vector(v, self->constval.vvec))
820                     goto error;
821                 break;
822             case TYPE_STRING:
823                 if (!ir_value_set_string(v, self->constval.vstring))
824                     goto error;
825                 break;
826             default:
827                 asterror(ast_ctx(self), "TODO: global constant type %i\n", self->expression.vtype);
828                 break;
829         }
830     }
831
832     /* link us to the ir_value */
833     self->ir_v = v;
834     return true;
835
836 error: /* clean up */
837     ir_value_delete(v);
838     return false;
839 }
840
841 bool ast_function_codegen(ast_function *self, ir_builder *ir)
842 {
843     ir_function *irf;
844     ir_value    *dummy;
845     ast_expression_common *ec;
846     size_t    i;
847
848     irf = self->ir_func;
849     if (!irf) {
850         asterror(ast_ctx(self), "ast_function's related ast_value was not generated yet\n");
851         return false;
852     }
853
854     /* fill the parameter list */
855     ec = &self->vtype->expression;
856     for (i = 0; i < ec->params_count; ++i)
857     {
858         if (!ir_function_params_add(irf, ec->params[i]->expression.vtype))
859             return false;
860         if (!self->builtin) {
861             if (!ast_local_codegen(ec->params[i], self->ir_func, true))
862                 return false;
863         }
864     }
865
866     if (self->builtin) {
867         irf->builtin = self->builtin;
868         return true;
869     }
870
871     if (!self->blocks_count) {
872         asterror(ast_ctx(self), "function `%s` has no body", self->name);
873         return false;
874     }
875
876     self->curblock = ir_function_create_block(irf, "entry");
877     if (!self->curblock)
878         return false;
879
880     for (i = 0; i < self->blocks_count; ++i) {
881         ast_expression_codegen *gen = self->blocks[i]->expression.codegen;
882         if (!(*gen)((ast_expression*)self->blocks[i], self, false, &dummy))
883             return false;
884     }
885
886     /* TODO: check return types */
887     if (!self->curblock->is_return)
888     {
889         if (!self->vtype->expression.next ||
890             self->vtype->expression.next->expression.vtype == TYPE_VOID)
891         {
892             return ir_block_create_return(self->curblock, NULL);
893         }
894         else
895         {
896             /* error("missing return"); */
897             asterror(ast_ctx(self), "function `%s` missing return value", self->name);
898             return false;
899         }
900     }
901     return true;
902 }
903
904 /* Note, you will not see ast_block_codegen generate ir_blocks.
905  * To the AST and the IR, blocks are 2 different things.
906  * In the AST it represents a block of code, usually enclosed in
907  * curly braces {...}.
908  * While in the IR it represents a block in terms of control-flow.
909  */
910 bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_value **out)
911 {
912     size_t i;
913
914     /* We don't use this
915      * Note: an ast-representation using the comma-operator
916      * of the form: (a, b, c) = x should not assign to c...
917      */
918     (void)lvalue;
919     if (self->expression.outr) {
920         *out = self->expression.outr;
921         return true;
922     }
923
924     /* output is NULL at first, we'll have each expression
925      * assign to out output, thus, a comma-operator represention
926      * using an ast_block will return the last generated value,
927      * so: (b, c) + a  executed both b and c, and returns c,
928      * which is then added to a.
929      */
930     *out = NULL;
931
932     /* generate locals */
933     for (i = 0; i < self->locals_count; ++i)
934     {
935         if (!ast_local_codegen(self->locals[i], func->ir_func, false))
936             return false;
937     }
938
939     for (i = 0; i < self->exprs_count; ++i)
940     {
941         ast_expression_codegen *gen = self->exprs[i]->expression.codegen;
942         if (!(*gen)(self->exprs[i], func, false, out))
943             return false;
944     }
945
946     self->expression.outr = *out;
947
948     return true;
949 }
950
951 bool ast_store_codegen(ast_store *self, ast_function *func, bool lvalue, ir_value **out)
952 {
953     ast_expression_codegen *cgen;
954     ir_value *left, *right;
955
956     if (lvalue && self->expression.outl) {
957         *out = self->expression.outl;
958         return true;
959     }
960
961     if (!lvalue && self->expression.outr) {
962         *out = self->expression.outr;
963         return true;
964     }
965
966     cgen = self->dest->expression.codegen;
967     /* lvalue! */
968     if (!(*cgen)((ast_expression*)(self->dest), func, true, &left))
969         return false;
970     self->expression.outl = left;
971
972     cgen = self->source->expression.codegen;
973     /* rvalue! */
974     if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
975         return false;
976
977     if (!ir_block_create_store_op(func->curblock, self->op, left, right))
978         return false;
979     self->expression.outr = right;
980
981     /* Theoretically, an assinment returns its left side as an
982      * lvalue, if we don't need an lvalue though, we return
983      * the right side as an rvalue, otherwise we have to
984      * somehow know whether or not we need to dereference the pointer
985      * on the left side - that is: OP_LOAD if it was an address.
986      * Also: in original QC we cannot OP_LOADP *anyway*.
987      */
988     *out = (lvalue ? left : right);
989
990     return true;
991 }
992
993 bool ast_binary_codegen(ast_binary *self, ast_function *func, bool lvalue, ir_value **out)
994 {
995     ast_expression_codegen *cgen;
996     ir_value *left, *right;
997
998     /* In the context of a binary operation, we can disregard
999      * the lvalue flag.
1000      */
1001     (void)lvalue;
1002     if (self->expression.outr) {
1003         *out = self->expression.outr;
1004         return true;
1005     }
1006
1007     cgen = self->left->expression.codegen;
1008     /* lvalue! */
1009     if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1010         return false;
1011
1012     cgen = self->right->expression.codegen;
1013     /* rvalue! */
1014     if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1015         return false;
1016
1017     *out = ir_block_create_binop(func->curblock, ast_function_label(func, "bin"),
1018                                  self->op, left, right);
1019     if (!*out)
1020         return false;
1021     self->expression.outr = *out;
1022
1023     return true;
1024 }
1025
1026 bool ast_binstore_codegen(ast_binstore *self, ast_function *func, bool lvalue, ir_value **out)
1027 {
1028     ast_expression_codegen *cgen;
1029     ir_value *leftl, *leftr, *right, *bin;
1030
1031     if (lvalue && self->expression.outl) {
1032         *out = self->expression.outl;
1033         return true;
1034     }
1035
1036     if (!lvalue && self->expression.outr) {
1037         *out = self->expression.outr;
1038         return true;
1039     }
1040
1041     /* for a binstore we need both an lvalue and an rvalue for the left side */
1042     /* rvalue of destination! */
1043     cgen = self->dest->expression.codegen;
1044     if (!(*cgen)((ast_expression*)(self->dest), func, false, &leftr))
1045         return false;
1046
1047     /* source as rvalue only */
1048     cgen = self->source->expression.codegen;
1049     if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1050         return false;
1051
1052     /* now the binary */
1053     bin = ir_block_create_binop(func->curblock, ast_function_label(func, "binst"),
1054                                 self->opbin, leftr, right);
1055     self->expression.outr = bin;
1056
1057     /* now store them */
1058     cgen = self->dest->expression.codegen;
1059     /* lvalue of destination */
1060     if (!(*cgen)((ast_expression*)(self->dest), func, true, &leftl))
1061         return false;
1062     self->expression.outl = leftl;
1063
1064     if (!ir_block_create_store_op(func->curblock, self->opstore, leftl, bin))
1065         return false;
1066     self->expression.outr = bin;
1067
1068     /* Theoretically, an assinment returns its left side as an
1069      * lvalue, if we don't need an lvalue though, we return
1070      * the right side as an rvalue, otherwise we have to
1071      * somehow know whether or not we need to dereference the pointer
1072      * on the left side - that is: OP_LOAD if it was an address.
1073      * Also: in original QC we cannot OP_LOADP *anyway*.
1074      */
1075     *out = (lvalue ? leftl : bin);
1076
1077     return true;
1078 }
1079
1080 bool ast_unary_codegen(ast_unary *self, ast_function *func, bool lvalue, ir_value **out)
1081 {
1082     ast_expression_codegen *cgen;
1083     ir_value *operand;
1084
1085     /* In the context of a unary operation, we can disregard
1086      * the lvalue flag.
1087      */
1088     (void)lvalue;
1089     if (self->expression.outr) {
1090         *out = self->expression.outr;
1091         return true;
1092     }
1093
1094     cgen = self->operand->expression.codegen;
1095     /* lvalue! */
1096     if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
1097         return false;
1098
1099     *out = ir_block_create_unary(func->curblock, ast_function_label(func, "unary"),
1100                                  self->op, operand);
1101     if (!*out)
1102         return false;
1103     self->expression.outr = *out;
1104
1105     return true;
1106 }
1107
1108 bool ast_return_codegen(ast_return *self, ast_function *func, bool lvalue, ir_value **out)
1109 {
1110     ast_expression_codegen *cgen;
1111     ir_value *operand;
1112
1113     /* In the context of a return operation, we can disregard
1114      * the lvalue flag.
1115      */
1116     (void)lvalue;
1117     if (self->expression.outr) {
1118         asterror(ast_ctx(self), "internal error: ast_return cannot be reused, it bears no result!\n");
1119         return false;
1120     }
1121     self->expression.outr = (ir_value*)1;
1122
1123     cgen = self->operand->expression.codegen;
1124     /* lvalue! */
1125     if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
1126         return false;
1127
1128     if (!ir_block_create_return(func->curblock, operand))
1129         return false;
1130
1131     return true;
1132 }
1133
1134 bool ast_entfield_codegen(ast_entfield *self, ast_function *func, bool lvalue, ir_value **out)
1135 {
1136     ast_expression_codegen *cgen;
1137     ir_value *ent, *field;
1138
1139     /* This function needs to take the 'lvalue' flag into account!
1140      * As lvalue we provide a field-pointer, as rvalue we provide the
1141      * value in a temp.
1142      */
1143
1144     if (lvalue && self->expression.outl) {
1145         *out = self->expression.outl;
1146         return true;
1147     }
1148
1149     if (!lvalue && self->expression.outr) {
1150         *out = self->expression.outr;
1151         return true;
1152     }
1153
1154     cgen = self->entity->expression.codegen;
1155     if (!(*cgen)((ast_expression*)(self->entity), func, false, &ent))
1156         return false;
1157
1158     cgen = self->field->expression.codegen;
1159     if (!(*cgen)((ast_expression*)(self->field), func, false, &field))
1160         return false;
1161
1162     if (lvalue) {
1163         /* address! */
1164         *out = ir_block_create_fieldaddress(func->curblock, ast_function_label(func, "efa"),
1165                                             ent, field);
1166     } else {
1167         *out = ir_block_create_load_from_ent(func->curblock, ast_function_label(func, "efv"),
1168                                              ent, field, self->expression.vtype);
1169     }
1170     if (!*out) {
1171         asterror(ast_ctx(self), "failed to create %s instruction (output type %s)",
1172                  (lvalue ? "ADDRESS" : "FIELD"),
1173                  type_name[self->expression.vtype]);
1174         return false;
1175     }
1176
1177     if (lvalue)
1178         self->expression.outl = *out;
1179     else
1180         self->expression.outr = *out;
1181
1182     /* Hm that should be it... */
1183     return true;
1184 }
1185
1186 bool ast_member_codegen(ast_member *self, ast_function *func, bool lvalue, ir_value **out)
1187 {
1188     ast_expression_codegen *cgen;
1189     ir_value *vec;
1190
1191     /* in QC this is always an lvalue */
1192     (void)lvalue;
1193     if (self->expression.outl) {
1194         *out = self->expression.outl;
1195         return true;
1196     }
1197
1198     cgen = self->owner->expression.codegen;
1199     if (!(*cgen)((ast_expression*)(self->owner), func, true, &vec))
1200         return false;
1201
1202     if (vec->vtype != TYPE_VECTOR &&
1203         !(vec->vtype == TYPE_FIELD && self->owner->expression.next->expression.vtype == TYPE_VECTOR))
1204     {
1205         return false;
1206     }
1207
1208     *out = ir_value_vector_member(vec, self->field);
1209     self->expression.outl = *out;
1210
1211     return (*out != NULL);
1212 }
1213
1214 bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_value **out)
1215 {
1216     ast_expression_codegen *cgen;
1217
1218     ir_value *condval;
1219     ir_value *dummy;
1220
1221     ir_block *cond = func->curblock;
1222     ir_block *ontrue;
1223     ir_block *onfalse;
1224     ir_block *merge;
1225
1226     /* We don't output any value, thus also don't care about r/lvalue */
1227     (void)out;
1228     (void)lvalue;
1229
1230     if (self->expression.outr) {
1231         asterror(ast_ctx(self), "internal error: ast_ifthen cannot be reused, it bears no result!\n");
1232         return false;
1233     }
1234     self->expression.outr = (ir_value*)1;
1235
1236     /* generate the condition */
1237     func->curblock = cond;
1238     cgen = self->cond->expression.codegen;
1239     if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
1240         return false;
1241
1242     /* on-true path */
1243
1244     if (self->on_true) {
1245         /* create on-true block */
1246         ontrue = ir_function_create_block(func->ir_func, ast_function_label(func, "ontrue"));
1247         if (!ontrue)
1248             return false;
1249
1250         /* enter the block */
1251         func->curblock = ontrue;
1252
1253         /* generate */
1254         cgen = self->on_true->expression.codegen;
1255         if (!(*cgen)((ast_expression*)(self->on_true), func, false, &dummy))
1256             return false;
1257     } else
1258         ontrue = NULL;
1259
1260     /* on-false path */
1261     if (self->on_false) {
1262         /* create on-false block */
1263         onfalse = ir_function_create_block(func->ir_func, ast_function_label(func, "onfalse"));
1264         if (!onfalse)
1265             return false;
1266
1267         /* enter the block */
1268         func->curblock = onfalse;
1269
1270         /* generate */
1271         cgen = self->on_false->expression.codegen;
1272         if (!(*cgen)((ast_expression*)(self->on_false), func, false, &dummy))
1273             return false;
1274     } else
1275         onfalse = NULL;
1276
1277     /* Merge block were they all merge in to */
1278     merge = ir_function_create_block(func->ir_func, ast_function_label(func, "endif"));
1279     if (!merge)
1280         return false;
1281
1282     /* add jumps ot the merge block */
1283     if (ontrue && !ir_block_create_jump(ontrue, merge))
1284         return false;
1285     if (onfalse && !ir_block_create_jump(onfalse, merge))
1286         return false;
1287
1288     /* we create the if here, that way all blocks are ordered :)
1289      */
1290     if (!ir_block_create_if(cond, condval,
1291                             (ontrue  ? ontrue  : merge),
1292                             (onfalse ? onfalse : merge)))
1293     {
1294         return false;
1295     }
1296
1297     /* Now enter the merge block */
1298     func->curblock = merge;
1299
1300     return true;
1301 }
1302
1303 bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_value **out)
1304 {
1305     ast_expression_codegen *cgen;
1306
1307     ir_value *condval;
1308     ir_value *trueval, *falseval;
1309     ir_instr *phi;
1310
1311     ir_block *cond = func->curblock;
1312     ir_block *ontrue;
1313     ir_block *onfalse;
1314     ir_block *merge;
1315
1316     /* Ternary can never create an lvalue... */
1317     if (lvalue)
1318         return false;
1319
1320     /* In theory it shouldn't be possible to pass through a node twice, but
1321      * in case we add any kind of optimization pass for the AST itself, it
1322      * may still happen, thus we remember a created ir_value and simply return one
1323      * if it already exists.
1324      */
1325     if (self->phi_out) {
1326         *out = self->phi_out;
1327         return true;
1328     }
1329
1330     /* In the following, contraty to ast_ifthen, we assume both paths exist. */
1331
1332     /* generate the condition */
1333     func->curblock = cond;
1334     cgen = self->cond->expression.codegen;
1335     if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
1336         return false;
1337
1338     /* create on-true block */
1339     ontrue = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_T"));
1340     if (!ontrue)
1341         return false;
1342     else
1343     {
1344         /* enter the block */
1345         func->curblock = ontrue;
1346
1347         /* generate */
1348         cgen = self->on_true->expression.codegen;
1349         if (!(*cgen)((ast_expression*)(self->on_true), func, false, &trueval))
1350             return false;
1351     }
1352
1353     /* create on-false block */
1354     onfalse = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_F"));
1355     if (!onfalse)
1356         return false;
1357     else
1358     {
1359         /* enter the block */
1360         func->curblock = onfalse;
1361
1362         /* generate */
1363         cgen = self->on_false->expression.codegen;
1364         if (!(*cgen)((ast_expression*)(self->on_false), func, false, &falseval))
1365             return false;
1366     }
1367
1368     /* create merge block */
1369     merge = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_out"));
1370     if (!merge)
1371         return false;
1372     /* jump to merge block */
1373     if (!ir_block_create_jump(ontrue, merge))
1374         return false;
1375     if (!ir_block_create_jump(onfalse, merge))
1376         return false;
1377
1378     /* create if instruction */
1379     if (!ir_block_create_if(cond, condval, ontrue, onfalse))
1380         return false;
1381
1382     /* Now enter the merge block */
1383     func->curblock = merge;
1384
1385     /* Here, now, we need a PHI node
1386      * but first some sanity checking...
1387      */
1388     if (trueval->vtype != falseval->vtype) {
1389         /* error("ternary with different types on the two sides"); */
1390         return false;
1391     }
1392
1393     /* create PHI */
1394     phi = ir_block_create_phi(merge, ast_function_label(func, "phi"), trueval->vtype);
1395     if (!phi ||
1396         !ir_phi_add(phi, ontrue,  trueval) ||
1397         !ir_phi_add(phi, onfalse, falseval))
1398     {
1399         return false;
1400     }
1401
1402     self->phi_out = ir_phi_value(phi);
1403     *out = self->phi_out;
1404
1405     return true;
1406 }
1407
1408 bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value **out)
1409 {
1410     ast_expression_codegen *cgen;
1411
1412     ir_value *dummy      = NULL;
1413     ir_value *precond    = NULL;
1414     ir_value *postcond   = NULL;
1415
1416     /* Since we insert some jumps "late" so we have blocks
1417      * ordered "nicely", we need to keep track of the actual end-blocks
1418      * of expressions to add the jumps to.
1419      */
1420     ir_block *bbody      = NULL, *end_bbody      = NULL;
1421     ir_block *bprecond   = NULL, *end_bprecond   = NULL;
1422     ir_block *bpostcond  = NULL, *end_bpostcond  = NULL;
1423     ir_block *bincrement = NULL, *end_bincrement = NULL;
1424     ir_block *bout       = NULL, *bin            = NULL;
1425
1426     /* let's at least move the outgoing block to the end */
1427     size_t    bout_id;
1428
1429     /* 'break' and 'continue' need to be able to find the right blocks */
1430     ir_block *bcontinue     = NULL;
1431     ir_block *bbreak        = NULL;
1432
1433     ir_block *old_bcontinue = NULL;
1434     ir_block *old_bbreak    = NULL;
1435
1436     ir_block *tmpblock      = NULL;
1437
1438     (void)lvalue;
1439     (void)out;
1440
1441     if (self->expression.outr) {
1442         asterror(ast_ctx(self), "internal error: ast_loop cannot be reused, it bears no result!\n");
1443         return false;
1444     }
1445     self->expression.outr = (ir_value*)1;
1446
1447     /* NOTE:
1448      * Should we ever need some kind of block ordering, better make this function
1449      * move blocks around than write a block ordering algorithm later... after all
1450      * the ast and ir should work together, not against each other.
1451      */
1452
1453     /* initexpr doesn't get its own block, it's pointless, it could create more blocks
1454      * anyway if for example it contains a ternary.
1455      */
1456     if (self->initexpr)
1457     {
1458         cgen = self->initexpr->expression.codegen;
1459         if (!(*cgen)((ast_expression*)(self->initexpr), func, false, &dummy))
1460             return false;
1461     }
1462
1463     /* Store the block from which we enter this chaos */
1464     bin = func->curblock;
1465
1466     /* The pre-loop condition needs its own block since we
1467      * need to be able to jump to the start of that expression.
1468      */
1469     if (self->precond)
1470     {
1471         bprecond = ir_function_create_block(func->ir_func, ast_function_label(func, "pre_loop_cond"));
1472         if (!bprecond)
1473             return false;
1474
1475         /* the pre-loop-condition the least important place to 'continue' at */
1476         bcontinue = bprecond;
1477
1478         /* enter */
1479         func->curblock = bprecond;
1480
1481         /* generate */
1482         cgen = self->precond->expression.codegen;
1483         if (!(*cgen)((ast_expression*)(self->precond), func, false, &precond))
1484             return false;
1485
1486         end_bprecond = func->curblock;
1487     } else {
1488         bprecond = end_bprecond = NULL;
1489     }
1490
1491     /* Now the next blocks won't be ordered nicely, but we need to
1492      * generate them this early for 'break' and 'continue'.
1493      */
1494     if (self->increment) {
1495         bincrement = ir_function_create_block(func->ir_func, ast_function_label(func, "loop_increment"));
1496         if (!bincrement)
1497             return false;
1498         bcontinue = bincrement; /* increment comes before the pre-loop-condition */
1499     } else {
1500         bincrement = end_bincrement = NULL;
1501     }
1502
1503     if (self->postcond) {
1504         bpostcond = ir_function_create_block(func->ir_func, ast_function_label(func, "post_loop_cond"));
1505         if (!bpostcond)
1506             return false;
1507         bcontinue = bpostcond; /* postcond comes before the increment */
1508     } else {
1509         bpostcond = end_bpostcond = NULL;
1510     }
1511
1512     bout_id = func->ir_func->blocks_count;
1513     bout = ir_function_create_block(func->ir_func, ast_function_label(func, "after_loop"));
1514     if (!bout)
1515         return false;
1516     bbreak = bout;
1517
1518     /* The loop body... */
1519     if (self->body)
1520     {
1521         bbody = ir_function_create_block(func->ir_func, ast_function_label(func, "loop_body"));
1522         if (!bbody)
1523             return false;
1524
1525         /* enter */
1526         func->curblock = bbody;
1527
1528         old_bbreak          = func->breakblock;
1529         old_bcontinue       = func->continueblock;
1530         func->breakblock    = bbreak;
1531         func->continueblock = bcontinue;
1532
1533         /* generate */
1534         cgen = self->body->expression.codegen;
1535         if (!(*cgen)((ast_expression*)(self->body), func, false, &dummy))
1536             return false;
1537
1538         end_bbody = func->curblock;
1539         func->breakblock    = old_bbreak;
1540         func->continueblock = old_bcontinue;
1541     }
1542
1543     /* post-loop-condition */
1544     if (self->postcond)
1545     {
1546         /* enter */
1547         func->curblock = bpostcond;
1548
1549         /* generate */
1550         cgen = self->postcond->expression.codegen;
1551         if (!(*cgen)((ast_expression*)(self->postcond), func, false, &postcond))
1552             return false;
1553
1554         end_bpostcond = func->curblock;
1555     }
1556
1557     /* The incrementor */
1558     if (self->increment)
1559     {
1560         /* enter */
1561         func->curblock = bincrement;
1562
1563         /* generate */
1564         cgen = self->increment->expression.codegen;
1565         if (!(*cgen)((ast_expression*)(self->increment), func, false, &dummy))
1566             return false;
1567
1568         end_bincrement = func->curblock;
1569     }
1570
1571     /* In any case now, we continue from the outgoing block */
1572     func->curblock = bout;
1573
1574     /* Now all blocks are in place */
1575     /* From 'bin' we jump to whatever comes first */
1576     if      (bprecond)   tmpblock = bprecond;
1577     else if (bbody)      tmpblock = bbody;
1578     else if (bpostcond)  tmpblock = bpostcond;
1579     else                 tmpblock = bout;
1580     if (!ir_block_create_jump(bin, tmpblock))
1581         return false;
1582
1583     /* From precond */
1584     if (bprecond)
1585     {
1586         ir_block *ontrue, *onfalse;
1587         if      (bbody)      ontrue = bbody;
1588         else if (bincrement) ontrue = bincrement;
1589         else if (bpostcond)  ontrue = bpostcond;
1590         else                 ontrue = bprecond;
1591         onfalse = bout;
1592         if (!ir_block_create_if(end_bprecond, precond, ontrue, onfalse))
1593             return false;
1594     }
1595
1596     /* from body */
1597     if (bbody)
1598     {
1599         if      (bincrement) tmpblock = bincrement;
1600         else if (bpostcond)  tmpblock = bpostcond;
1601         else if (bprecond)   tmpblock = bprecond;
1602         else                 tmpblock = bout;
1603         if (!ir_block_create_jump(end_bbody, tmpblock))
1604             return false;
1605     }
1606
1607     /* from increment */
1608     if (bincrement)
1609     {
1610         if      (bpostcond)  tmpblock = bpostcond;
1611         else if (bprecond)   tmpblock = bprecond;
1612         else if (bbody)      tmpblock = bbody;
1613         else                 tmpblock = bout;
1614         if (!ir_block_create_jump(end_bincrement, tmpblock))
1615             return false;
1616     }
1617
1618     /* from postcond */
1619     if (bpostcond)
1620     {
1621         ir_block *ontrue, *onfalse;
1622         if      (bprecond)   ontrue = bprecond;
1623         else if (bbody)      ontrue = bbody;
1624         else if (bincrement) ontrue = bincrement;
1625         else                 ontrue = bpostcond;
1626         onfalse = bout;
1627         if (!ir_block_create_if(end_bpostcond, postcond, ontrue, onfalse))
1628             return false;
1629     }
1630
1631     /* Move 'bout' to the end */
1632     if (!ir_function_blocks_remove(func->ir_func, bout_id) ||
1633         !ir_function_blocks_add(func->ir_func, bout))
1634     {
1635         ir_block_delete(bout);
1636         return false;
1637     }
1638
1639     return true;
1640 }
1641
1642 bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value **out)
1643 {
1644     ast_expression_codegen *cgen;
1645     ir_value_vector         params;
1646     ir_instr               *callinstr;
1647     size_t i;
1648
1649     ir_value *funval = NULL;
1650
1651     /* return values are never lvalues */
1652     (void)lvalue;
1653
1654     if (self->expression.outr) {
1655         *out = self->expression.outr;
1656         return true;
1657     }
1658
1659     cgen = self->func->expression.codegen;
1660     if (!(*cgen)((ast_expression*)(self->func), func, false, &funval))
1661         return false;
1662     if (!funval)
1663         return false;
1664
1665     MEM_VECTOR_INIT(&params, v);
1666
1667     /* parameters */
1668     for (i = 0; i < self->params_count; ++i)
1669     {
1670         ir_value *param;
1671         ast_expression *expr = self->params[i];
1672
1673         cgen = expr->expression.codegen;
1674         if (!(*cgen)(expr, func, false, &param))
1675             goto error;
1676         if (!param)
1677             goto error;
1678         if (!ir_value_vector_v_add(&params, param))
1679             goto error;
1680     }
1681
1682     callinstr = ir_block_create_call(func->curblock, ast_function_label(func, "call"), funval);
1683     if (!callinstr)
1684         goto error;
1685
1686     for (i = 0; i < params.v_count; ++i) {
1687         if (!ir_call_param(callinstr, params.v[i]))
1688             goto error;
1689     }
1690
1691     *out = ir_call_value(callinstr);
1692     self->expression.outr = *out;
1693
1694     MEM_VECTOR_CLEAR(&params, v);
1695     return true;
1696 error:
1697     MEM_VECTOR_CLEAR(&params, v);
1698     return false;
1699 }