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