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