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