]> git.xonotic.org Git - xonotic/gmqcc.git/blob - ast.c
51912e94ca284356004fb62acfc5f6c115754f8c
[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     con_cvprintmsg((void*)&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     con_err("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     self->expression.variadic = false;
73     self->expression.params   = NULL;
74 }
75
76 static void ast_expression_delete(ast_expression *self)
77 {
78     size_t i;
79     if (self->expression.next)
80         ast_delete(self->expression.next);
81     for (i = 0; i < vec_size(self->expression.params); ++i) {
82         ast_delete(self->expression.params[i]);
83     }
84     vec_free(self->expression.params);
85 }
86
87 static void ast_expression_delete_full(ast_expression *self)
88 {
89     ast_expression_delete(self);
90     mem_d(self);
91 }
92
93 ast_value* ast_value_copy(const ast_value *self)
94 {
95     size_t i;
96     const ast_expression_common *fromex;
97     ast_expression_common *selfex;
98     ast_value *cp = ast_value_new(self->expression.node.context, self->name, self->expression.vtype);
99     if (self->expression.next) {
100         cp->expression.next = ast_type_copy(self->expression.node.context, self->expression.next);
101         if (!cp->expression.next) {
102             ast_value_delete(cp);
103             return NULL;
104         }
105     }
106     fromex   = &self->expression;
107     selfex = &cp->expression;
108     selfex->variadic = fromex->variadic;
109     for (i = 0; i < vec_size(fromex->params); ++i) {
110         ast_value *v = ast_value_copy(fromex->params[i]);
111         if (!v) {
112             ast_value_delete(cp);
113             return NULL;
114         }
115         vec_push(selfex->params, v);
116     }
117     return cp;
118 }
119
120 bool ast_type_adopt_impl(ast_expression *self, const ast_expression *other)
121 {
122     size_t i;
123     const ast_expression_common *fromex;
124     ast_expression_common *selfex;
125     self->expression.vtype = other->expression.vtype;
126     if (other->expression.next) {
127         self->expression.next = (ast_expression*)ast_type_copy(ast_ctx(self), other->expression.next);
128         if (!self->expression.next)
129             return false;
130     }
131     fromex   = &other->expression;
132     selfex = &self->expression;
133     selfex->variadic = fromex->variadic;
134     for (i = 0; i < vec_size(fromex->params); ++i) {
135         ast_value *v = ast_value_copy(fromex->params[i]);
136         if (!v)
137             return false;
138         vec_push(selfex->params, v);
139     }
140     return true;
141 }
142
143 static ast_expression* ast_shallow_type(lex_ctx ctx, int vtype)
144 {
145     ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
146     ast_expression_init(self, NULL);
147     self->expression.codegen = NULL;
148     self->expression.next    = NULL;
149     self->expression.vtype   = vtype;
150     return self;
151 }
152
153 ast_expression* ast_type_copy(lex_ctx ctx, const ast_expression *ex)
154 {
155     size_t i;
156     const ast_expression_common *fromex;
157     ast_expression_common *selfex;
158
159     if (!ex)
160         return NULL;
161     else
162     {
163         ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
164         ast_expression_init(self, NULL);
165
166         fromex   = &ex->expression;
167         selfex = &self->expression;
168
169         /* This may never be codegen()d */
170         selfex->codegen = NULL;
171
172         selfex->vtype = fromex->vtype;
173         if (fromex->next)
174         {
175             selfex->next = ast_type_copy(ctx, fromex->next);
176             if (!selfex->next) {
177                 ast_expression_delete_full(self);
178                 return NULL;
179             }
180         }
181         else
182             selfex->next = NULL;
183
184         selfex->variadic = fromex->variadic;
185         for (i = 0; i < vec_size(fromex->params); ++i) {
186             ast_value *v = ast_value_copy(fromex->params[i]);
187             if (!v) {
188                 ast_expression_delete_full(self);
189                 return NULL;
190             }
191             vec_push(selfex->params, v);
192         }
193
194         return self;
195     }
196 }
197
198 bool ast_compare_type(ast_expression *a, ast_expression *b)
199 {
200     if (a->expression.vtype != b->expression.vtype)
201         return false;
202     if (!a->expression.next != !b->expression.next)
203         return false;
204     if (vec_size(a->expression.params) != vec_size(b->expression.params))
205         return false;
206     if (a->expression.variadic != b->expression.variadic)
207         return false;
208     if (vec_size(a->expression.params)) {
209         size_t i;
210         for (i = 0; i < vec_size(a->expression.params); ++i) {
211             if (!ast_compare_type((ast_expression*)a->expression.params[i],
212                                   (ast_expression*)b->expression.params[i]))
213                 return false;
214         }
215     }
216     if (a->expression.next)
217         return ast_compare_type(a->expression.next, b->expression.next);
218     return true;
219 }
220
221 static size_t ast_type_to_string_impl(ast_expression *e, char *buf, size_t bufsize, size_t pos)
222 {
223     const char *typestr;
224     size_t typelen;
225     size_t i;
226
227     if (!e) {
228         if (pos + 6 >= bufsize)
229             goto full;
230         strcpy(buf + pos, "(null)");
231         return pos + 6;
232     }
233
234     if (pos + 1 >= bufsize)
235         goto full;
236
237     switch (e->expression.vtype) {
238         case TYPE_VARIANT:
239             strcpy(buf + pos, "(variant)");
240             return pos + 9;
241
242         case TYPE_FIELD:
243             buf[pos++] = '.';
244             return ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
245
246         case TYPE_POINTER:
247             if (pos + 3 >= bufsize)
248                 goto full;
249             buf[pos++] = '*';
250             buf[pos++] = '(';
251             pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
252             if (pos + 1 >= bufsize)
253                 goto full;
254             buf[pos++] = ')';
255             return pos;
256
257         case TYPE_FUNCTION:
258             pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
259             if (pos + 2 >= bufsize)
260                 goto full;
261             if (!vec_size(e->expression.params)) {
262                 buf[pos++] = '(';
263                 buf[pos++] = ')';
264                 return pos;
265             }
266             buf[pos++] = '(';
267             pos = ast_type_to_string_impl((ast_expression*)(e->expression.params[0]), buf, bufsize, pos);
268             for (i = 1; i < vec_size(e->expression.params); ++i) {
269                 if (pos + 2 >= bufsize)
270                     goto full;
271                 buf[pos++] = ',';
272                 buf[pos++] = ' ';
273                 pos = ast_type_to_string_impl((ast_expression*)(e->expression.params[i]), buf, bufsize, pos);
274             }
275             if (pos + 1 >= bufsize)
276                 goto full;
277             buf[pos++] = ')';
278             return pos;
279
280         case TYPE_ARRAY:
281             pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
282             if (pos + 1 >= bufsize)
283                 goto full;
284             buf[pos++] = '[';
285             pos += snprintf(buf + pos, bufsize - pos - 1, "%i", (int)e->expression.count);
286             if (pos + 1 >= bufsize)
287                 goto full;
288             buf[pos++] = ']';
289             return pos;
290
291         default:
292             typestr = type_name[e->expression.vtype];
293             typelen = strlen(typestr);
294             if (pos + typelen >= bufsize)
295                 goto full;
296             strcpy(buf + pos, typestr);
297             return pos + typelen;
298     }
299
300 full:
301     buf[bufsize-3] = '.';
302     buf[bufsize-2] = '.';
303     buf[bufsize-1] = '.';
304     return bufsize;
305 }
306
307 void ast_type_to_string(ast_expression *e, char *buf, size_t bufsize)
308 {
309     size_t pos = ast_type_to_string_impl(e, buf, bufsize-1, 0);
310     buf[pos] = 0;
311 }
312
313 ast_value* ast_value_new(lex_ctx ctx, const char *name, int t)
314 {
315     ast_instantiate(ast_value, ctx, ast_value_delete);
316     ast_expression_init((ast_expression*)self,
317                         (ast_expression_codegen*)&ast_value_codegen);
318     self->expression.node.keep = true; /* keep */
319
320     self->name = name ? util_strdup(name) : NULL;
321     self->expression.vtype = t;
322     self->expression.next  = NULL;
323     self->isconst = false;
324     self->uses    = 0;
325     memset(&self->constval, 0, sizeof(self->constval));
326
327     self->ir_v           = NULL;
328     self->ir_values      = NULL;
329     self->ir_value_count = 0;
330
331     self->setter = NULL;
332     self->getter = NULL;
333
334     return self;
335 }
336
337 void ast_value_delete(ast_value* self)
338 {
339     if (self->name)
340         mem_d((void*)self->name);
341     if (self->isconst) {
342         switch (self->expression.vtype)
343         {
344         case TYPE_STRING:
345             mem_d((void*)self->constval.vstring);
346             break;
347         case TYPE_FUNCTION:
348             /* unlink us from the function node */
349             self->constval.vfunc->vtype = NULL;
350             break;
351         /* NOTE: delete function? currently collected in
352          * the parser structure
353          */
354         default:
355             break;
356         }
357     }
358     if (self->ir_values)
359         mem_d(self->ir_values);
360     ast_expression_delete((ast_expression*)self);
361     mem_d(self);
362 }
363
364 void ast_value_params_add(ast_value *self, ast_value *p)
365 {
366     vec_push(self->expression.params, p);
367 }
368
369 bool ast_value_set_name(ast_value *self, const char *name)
370 {
371     if (self->name)
372         mem_d((void*)self->name);
373     self->name = util_strdup(name);
374     return !!self->name;
375 }
376
377 ast_binary* ast_binary_new(lex_ctx ctx, int op,
378                            ast_expression* left, ast_expression* right)
379 {
380     ast_instantiate(ast_binary, ctx, ast_binary_delete);
381     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binary_codegen);
382
383     self->op = op;
384     self->left = left;
385     self->right = right;
386
387     if (op >= INSTR_EQ_F && op <= INSTR_GT)
388         self->expression.vtype = TYPE_FLOAT;
389     else if (op == INSTR_AND || op == INSTR_OR ||
390              op == INSTR_BITAND || op == INSTR_BITOR)
391         self->expression.vtype = TYPE_FLOAT;
392     else if (op == INSTR_MUL_VF || op == INSTR_MUL_FV)
393         self->expression.vtype = TYPE_VECTOR;
394     else if (op == INSTR_MUL_V)
395         self->expression.vtype = TYPE_FLOAT;
396     else
397         self->expression.vtype = left->expression.vtype;
398
399     return self;
400 }
401
402 void ast_binary_delete(ast_binary *self)
403 {
404     ast_unref(self->left);
405     ast_unref(self->right);
406     ast_expression_delete((ast_expression*)self);
407     mem_d(self);
408 }
409
410 ast_binstore* ast_binstore_new(lex_ctx ctx, int storop, int op,
411                                ast_expression* left, ast_expression* right)
412 {
413     ast_instantiate(ast_binstore, ctx, ast_binstore_delete);
414     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binstore_codegen);
415
416     self->opstore = storop;
417     self->opbin   = op;
418     self->dest    = left;
419     self->source  = right;
420
421     self->expression.vtype = left->expression.vtype;
422     if (left->expression.next) {
423         self->expression.next = ast_type_copy(ctx, left);
424         if (!self->expression.next) {
425             ast_delete(self);
426             return NULL;
427         }
428     }
429     else
430         self->expression.next = NULL;
431
432     return self;
433 }
434
435 void ast_binstore_delete(ast_binstore *self)
436 {
437     ast_unref(self->dest);
438     ast_unref(self->source);
439     ast_expression_delete((ast_expression*)self);
440     mem_d(self);
441 }
442
443 ast_unary* ast_unary_new(lex_ctx ctx, int op,
444                          ast_expression *expr)
445 {
446     ast_instantiate(ast_unary, ctx, ast_unary_delete);
447     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_unary_codegen);
448
449     self->op = op;
450     self->operand = expr;
451
452     if (op >= INSTR_NOT_F && op <= INSTR_NOT_FNC) {
453         self->expression.vtype = TYPE_FLOAT;
454     } else
455         asterror(ctx, "cannot determine type of unary operation %s", asm_instr[op].m);
456
457     return self;
458 }
459
460 void ast_unary_delete(ast_unary *self)
461 {
462     ast_unref(self->operand);
463     ast_expression_delete((ast_expression*)self);
464     mem_d(self);
465 }
466
467 ast_return* ast_return_new(lex_ctx ctx, ast_expression *expr)
468 {
469     ast_instantiate(ast_return, ctx, ast_return_delete);
470     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_return_codegen);
471
472     self->operand = expr;
473
474     return self;
475 }
476
477 void ast_return_delete(ast_return *self)
478 {
479     if (self->operand)
480         ast_unref(self->operand);
481     ast_expression_delete((ast_expression*)self);
482     mem_d(self);
483 }
484
485 ast_entfield* ast_entfield_new(lex_ctx ctx, ast_expression *entity, ast_expression *field)
486 {
487     if (field->expression.vtype != TYPE_FIELD) {
488         asterror(ctx, "ast_entfield_new with expression not of type field");
489         return NULL;
490     }
491     return ast_entfield_new_force(ctx, entity, field, field->expression.next);
492 }
493
494 ast_entfield* ast_entfield_new_force(lex_ctx ctx, ast_expression *entity, ast_expression *field, const ast_expression *outtype)
495 {
496     ast_instantiate(ast_entfield, ctx, ast_entfield_delete);
497
498     if (!outtype) {
499         mem_d(self);
500         /* Error: field has no type... */
501         return NULL;
502     }
503
504     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_entfield_codegen);
505
506     self->entity = entity;
507     self->field  = field;
508
509     if (!ast_type_adopt(self, outtype)) {
510         ast_entfield_delete(self);
511         return NULL;
512     }
513
514     return self;
515 }
516
517 void ast_entfield_delete(ast_entfield *self)
518 {
519     ast_unref(self->entity);
520     ast_unref(self->field);
521     ast_expression_delete((ast_expression*)self);
522     mem_d(self);
523 }
524
525 ast_member* ast_member_new(lex_ctx ctx, ast_expression *owner, unsigned int field)
526 {
527     ast_instantiate(ast_member, ctx, ast_member_delete);
528     if (field >= 3) {
529         mem_d(self);
530         return NULL;
531     }
532
533     if (owner->expression.vtype != TYPE_VECTOR &&
534         owner->expression.vtype != TYPE_FIELD) {
535         asterror(ctx, "member-access on an invalid owner of type %s", type_name[owner->expression.vtype]);
536         mem_d(self);
537         return NULL;
538     }
539
540     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_member_codegen);
541     self->expression.node.keep = true; /* keep */
542
543     if (owner->expression.vtype == TYPE_VECTOR) {
544         self->expression.vtype = TYPE_FLOAT;
545         self->expression.next  = NULL;
546     } else {
547         self->expression.vtype = TYPE_FIELD;
548         self->expression.next = ast_shallow_type(ctx, TYPE_FLOAT);
549     }
550
551     self->owner = owner;
552     self->field = field;
553
554     return self;
555 }
556
557 void ast_member_delete(ast_member *self)
558 {
559     /* The owner is always an ast_value, which has .keep=true,
560      * also: ast_members are usually deleted after the owner, thus
561      * this will cause invalid access
562     ast_unref(self->owner);
563      * once we allow (expression).x to access a vector-member, we need
564      * to change this: preferably by creating an alternate ast node for this
565      * purpose that is not garbage-collected.
566     */
567     ast_expression_delete((ast_expression*)self);
568     mem_d(self);
569 }
570
571 ast_array_index* ast_array_index_new(lex_ctx ctx, ast_expression *array, ast_expression *index)
572 {
573     ast_expression *outtype;
574     ast_instantiate(ast_array_index, ctx, ast_array_index_delete);
575
576     outtype = array->expression.next;
577     if (!outtype) {
578         mem_d(self);
579         /* Error: field has no type... */
580         return NULL;
581     }
582
583     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_array_index_codegen);
584
585     self->array = array;
586     self->index = index;
587
588     if (!ast_type_adopt(self, outtype)) {
589         ast_array_index_delete(self);
590         return NULL;
591     }
592     if (array->expression.vtype == TYPE_FIELD && outtype->expression.vtype == TYPE_ARRAY) {
593         if (self->expression.vtype != TYPE_ARRAY) {
594             asterror(ast_ctx(self), "array_index node on type");
595             ast_array_index_delete(self);
596             return NULL;
597         }
598         self->array = outtype;
599         self->expression.vtype = TYPE_FIELD;
600     }
601
602     return self;
603 }
604
605 void ast_array_index_delete(ast_array_index *self)
606 {
607     ast_unref(self->array);
608     ast_unref(self->index);
609     ast_expression_delete((ast_expression*)self);
610     mem_d(self);
611 }
612
613 ast_ifthen* ast_ifthen_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
614 {
615     ast_instantiate(ast_ifthen, ctx, ast_ifthen_delete);
616     if (!ontrue && !onfalse) {
617         /* because it is invalid */
618         mem_d(self);
619         return NULL;
620     }
621     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ifthen_codegen);
622
623     self->cond     = cond;
624     self->on_true  = ontrue;
625     self->on_false = onfalse;
626
627     return self;
628 }
629
630 void ast_ifthen_delete(ast_ifthen *self)
631 {
632     ast_unref(self->cond);
633     if (self->on_true)
634         ast_unref(self->on_true);
635     if (self->on_false)
636         ast_unref(self->on_false);
637     ast_expression_delete((ast_expression*)self);
638     mem_d(self);
639 }
640
641 ast_ternary* ast_ternary_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
642 {
643     ast_instantiate(ast_ternary, ctx, ast_ternary_delete);
644     /* This time NEITHER must be NULL */
645     if (!ontrue || !onfalse) {
646         mem_d(self);
647         return NULL;
648     }
649     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ternary_codegen);
650
651     self->cond     = cond;
652     self->on_true  = ontrue;
653     self->on_false = onfalse;
654     self->phi_out  = NULL;
655
656     return self;
657 }
658
659 void ast_ternary_delete(ast_ternary *self)
660 {
661     ast_unref(self->cond);
662     ast_unref(self->on_true);
663     ast_unref(self->on_false);
664     ast_expression_delete((ast_expression*)self);
665     mem_d(self);
666 }
667
668 ast_loop* ast_loop_new(lex_ctx ctx,
669                        ast_expression *initexpr,
670                        ast_expression *precond,
671                        ast_expression *postcond,
672                        ast_expression *increment,
673                        ast_expression *body)
674 {
675     ast_instantiate(ast_loop, ctx, ast_loop_delete);
676     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_loop_codegen);
677
678     self->initexpr  = initexpr;
679     self->precond   = precond;
680     self->postcond  = postcond;
681     self->increment = increment;
682     self->body      = body;
683
684     return self;
685 }
686
687 void ast_loop_delete(ast_loop *self)
688 {
689     if (self->initexpr)
690         ast_unref(self->initexpr);
691     if (self->precond)
692         ast_unref(self->precond);
693     if (self->postcond)
694         ast_unref(self->postcond);
695     if (self->increment)
696         ast_unref(self->increment);
697     if (self->body)
698         ast_unref(self->body);
699     ast_expression_delete((ast_expression*)self);
700     mem_d(self);
701 }
702
703 ast_breakcont* ast_breakcont_new(lex_ctx ctx, bool iscont)
704 {
705     ast_instantiate(ast_breakcont, ctx, ast_breakcont_delete);
706     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_breakcont_codegen);
707
708     self->is_continue = iscont;
709
710     return self;
711 }
712
713 void ast_breakcont_delete(ast_breakcont *self)
714 {
715     ast_expression_delete((ast_expression*)self);
716     mem_d(self);
717 }
718
719 ast_call* ast_call_new(lex_ctx ctx,
720                        ast_expression *funcexpr)
721 {
722     ast_instantiate(ast_call, ctx, ast_call_delete);
723     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_call_codegen);
724
725     self->params = NULL;
726     self->func   = funcexpr;
727
728     self->expression.vtype = funcexpr->expression.next->expression.vtype;
729     if (funcexpr->expression.next->expression.next)
730         self->expression.next = ast_type_copy(ctx, funcexpr->expression.next->expression.next);
731
732     return self;
733 }
734
735 void ast_call_delete(ast_call *self)
736 {
737     size_t i;
738     for (i = 0; i < vec_size(self->params); ++i)
739         ast_unref(self->params[i]);
740     vec_free(self->params);
741
742     if (self->func)
743         ast_unref(self->func);
744
745     ast_expression_delete((ast_expression*)self);
746     mem_d(self);
747 }
748
749 bool ast_call_check_types(ast_call *self)
750 {
751     size_t i;
752     bool   retval = true;
753     const  ast_expression *func = self->func;
754     size_t count = vec_size(self->params);
755     if (count > vec_size(func->expression.params))
756         count = vec_size(func->expression.params);
757
758     for (i = 0; i < count; ++i) {
759         if (!ast_compare_type(self->params[i], (ast_expression*)(func->expression.params[i]))) {
760             asterror(ast_ctx(self), "invalid type for parameter %u in function call",
761                      (unsigned int)(i+1));
762             /* we don't immediately return */
763             retval = false;
764         }
765     }
766     return retval;
767 }
768
769 ast_store* ast_store_new(lex_ctx ctx, int op,
770                          ast_expression *dest, ast_expression *source)
771 {
772     ast_instantiate(ast_store, ctx, ast_store_delete);
773     ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_store_codegen);
774
775     self->op = op;
776     self->dest = dest;
777     self->source = source;
778
779     self->expression.vtype = dest->expression.vtype;
780     if (dest->expression.next) {
781         self->expression.next = ast_type_copy(ctx, dest);
782         if (!self->expression.next) {
783             ast_delete(self);
784             return NULL;
785         }
786     }
787     else
788         self->expression.next = NULL;
789
790     return self;
791 }
792
793 void ast_store_delete(ast_store *self)
794 {
795     ast_unref(self->dest);
796     ast_unref(self->source);
797     ast_expression_delete((ast_expression*)self);
798     mem_d(self);
799 }
800
801 ast_block* ast_block_new(lex_ctx ctx)
802 {
803     ast_instantiate(ast_block, ctx, ast_block_delete);
804     ast_expression_init((ast_expression*)self,
805                         (ast_expression_codegen*)&ast_block_codegen);
806
807     self->locals  = NULL;
808     self->exprs   = NULL;
809     self->collect = NULL;
810
811     return self;
812 }
813
814 void ast_block_collect(ast_block *self, ast_expression *expr)
815 {
816     vec_push(self->collect, expr);
817     expr->expression.node.keep = true;
818 }
819
820 void ast_block_delete(ast_block *self)
821 {
822     size_t i;
823     for (i = 0; i < vec_size(self->exprs); ++i)
824         ast_unref(self->exprs[i]);
825     vec_free(self->exprs);
826     for (i = 0; i < vec_size(self->locals); ++i)
827         ast_delete(self->locals[i]);
828     vec_free(self->locals);
829     for (i = 0; i < vec_size(self->collect); ++i)
830         ast_delete(self->collect[i]);
831     vec_free(self->collect);
832     ast_expression_delete((ast_expression*)self);
833     mem_d(self);
834 }
835
836 bool ast_block_set_type(ast_block *self, ast_expression *from)
837 {
838     if (self->expression.next)
839         ast_delete(self->expression.next);
840     self->expression.vtype = from->expression.vtype;
841     if (from->expression.next) {
842         self->expression.next = ast_type_copy(self->expression.node.context, from->expression.next);
843         if (!self->expression.next)
844             return false;
845     }
846     else
847         self->expression.next = NULL;
848     return true;
849 }
850
851 ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype)
852 {
853     ast_instantiate(ast_function, ctx, ast_function_delete);
854
855     if (!vtype ||
856         vtype->isconst ||
857         vtype->expression.vtype != TYPE_FUNCTION)
858     {
859         mem_d(self);
860         return NULL;
861     }
862
863     self->vtype  = vtype;
864     self->name   = name ? util_strdup(name) : NULL;
865     self->blocks = NULL;
866
867     self->labelcount = 0;
868     self->builtin = 0;
869
870     self->ir_func = NULL;
871     self->curblock = NULL;
872
873     self->breakblock    = NULL;
874     self->continueblock = NULL;
875
876     vtype->isconst = true;
877     vtype->constval.vfunc = self;
878
879     return self;
880 }
881
882 void ast_function_delete(ast_function *self)
883 {
884     size_t i;
885     if (self->name)
886         mem_d((void*)self->name);
887     if (self->vtype) {
888         /* ast_value_delete(self->vtype); */
889         self->vtype->isconst = false;
890         self->vtype->constval.vfunc = NULL;
891         /* We use unref - if it was stored in a global table it is supposed
892          * to be deleted from *there*
893          */
894         ast_unref(self->vtype);
895     }
896     for (i = 0; i < vec_size(self->blocks); ++i)
897         ast_delete(self->blocks[i]);
898     vec_free(self->blocks);
899     mem_d(self);
900 }
901
902 const char* ast_function_label(ast_function *self, const char *prefix)
903 {
904     size_t id;
905     size_t len;
906     char  *from;
907
908     if (!opts_dump)
909         return NULL;
910
911     id  = (self->labelcount++);
912     len = strlen(prefix);
913
914     from = self->labelbuf + sizeof(self->labelbuf)-1;
915     *from-- = 0;
916     do {
917         unsigned int digit = id % 10;
918         *from = digit + '0';
919         id /= 10;
920     } while (id);
921     memcpy(from - len, prefix, len);
922     return from - len;
923 }
924
925 /*********************************************************************/
926 /* AST codegen part
927  * by convention you must never pass NULL to the 'ir_value **out'
928  * parameter. If you really don't care about the output, pass a dummy.
929  * But I can't imagine a pituation where the output is truly unnecessary.
930  */
931
932 bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out)
933 {
934     /* NOTE: This is the codegen for a variable used in an expression.
935      * It is not the codegen to generate the value. For this purpose,
936      * ast_local_codegen and ast_global_codegen are to be used before this
937      * is executed. ast_function_codegen should take care of its locals,
938      * and the ast-user should take care of ast_global_codegen to be used
939      * on all the globals.
940      */
941     if (!self->ir_v) {
942         char typename[1024];
943         ast_type_to_string((ast_expression*)self, typename, sizeof(typename));
944         asterror(ast_ctx(self), "ast_value used before generated %s %s", typename, self->name);
945         return false;
946     }
947     *out = self->ir_v;
948     return true;
949 }
950
951 bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
952 {
953     ir_value *v = NULL;
954
955     if (self->isconst && self->expression.vtype == TYPE_FUNCTION)
956     {
957         ir_function *func = ir_builder_create_function(ir, self->name, self->expression.next->expression.vtype);
958         if (!func)
959             return false;
960         func->context = ast_ctx(self);
961         func->value->context = ast_ctx(self);
962
963         self->constval.vfunc->ir_func = func;
964         self->ir_v = func->value;
965         /* The function is filled later on ast_function_codegen... */
966         return true;
967     }
968
969     if (isfield && self->expression.vtype == TYPE_FIELD) {
970         ast_expression *fieldtype = self->expression.next;
971
972         if (self->isconst) {
973             asterror(ast_ctx(self), "TODO: constant field pointers with value");
974             goto error;
975         }
976
977         if (fieldtype->expression.vtype == TYPE_ARRAY) {
978             size_t ai;
979             char   *name;
980             size_t  namelen;
981
982             ast_expression_common *elemtype;
983             int                    vtype;
984             ast_value             *array = (ast_value*)fieldtype;
985
986             if (!ast_istype(fieldtype, ast_value)) {
987                 asterror(ast_ctx(self), "internal error: ast_value required");
988                 return false;
989             }
990
991             /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
992             if (!array->expression.count || array->expression.count > opts_max_array_size)
993                 asterror(ast_ctx(self), "Invalid array of size %lu", (unsigned long)array->expression.count);
994
995             elemtype = &array->expression.next->expression;
996             vtype = elemtype->vtype;
997
998             v = ir_builder_create_field(ir, self->name, vtype);
999             if (!v) {
1000                 asterror(ast_ctx(self), "ir_builder_create_global failed");
1001                 return false;
1002             }
1003             if (vtype == TYPE_FIELD)
1004                 v->fieldtype = elemtype->next->expression.vtype;
1005             v->context = ast_ctx(self);
1006             array->ir_v = self->ir_v = v;
1007
1008             namelen = strlen(self->name);
1009             name    = (char*)mem_a(namelen + 16);
1010             strcpy(name, self->name);
1011
1012             array->ir_values = (ir_value**)mem_a(sizeof(array->ir_values[0]) * array->expression.count);
1013             array->ir_values[0] = v;
1014             for (ai = 1; ai < array->expression.count; ++ai) {
1015                 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1016                 array->ir_values[ai] = ir_builder_create_field(ir, name, vtype);
1017                 if (!array->ir_values[ai]) {
1018                     mem_d(name);
1019                     asterror(ast_ctx(self), "ir_builder_create_global failed");
1020                     return false;
1021                 }
1022                 if (vtype == TYPE_FIELD)
1023                     array->ir_values[ai]->fieldtype = elemtype->next->expression.vtype;
1024                 array->ir_values[ai]->context = ast_ctx(self);
1025             }
1026             mem_d(name);
1027         }
1028         else
1029         {
1030             v = ir_builder_create_field(ir, self->name, self->expression.next->expression.vtype);
1031             if (!v)
1032                 return false;
1033             v->context = ast_ctx(self);
1034             self->ir_v = v;
1035         }
1036         return true;
1037     }
1038
1039     if (self->expression.vtype == TYPE_ARRAY) {
1040         size_t ai;
1041         char   *name;
1042         size_t  namelen;
1043
1044         ast_expression_common *elemtype = &self->expression.next->expression;
1045         int vtype = elemtype->vtype;
1046
1047         /* same as with field arrays */
1048         if (!self->expression.count || self->expression.count > opts_max_array_size)
1049             asterror(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1050
1051         v = ir_builder_create_global(ir, self->name, vtype);
1052         if (!v) {
1053             asterror(ast_ctx(self), "ir_builder_create_global failed");
1054             return false;
1055         }
1056         if (vtype == TYPE_FIELD)
1057             v->fieldtype = elemtype->next->expression.vtype;
1058         v->context = ast_ctx(self);
1059
1060         namelen = strlen(self->name);
1061         name    = (char*)mem_a(namelen + 16);
1062         strcpy(name, self->name);
1063
1064         self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1065         self->ir_values[0] = v;
1066         for (ai = 1; ai < self->expression.count; ++ai) {
1067             snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1068             self->ir_values[ai] = ir_builder_create_global(ir, name, vtype);
1069             if (!self->ir_values[ai]) {
1070                 mem_d(name);
1071                 asterror(ast_ctx(self), "ir_builder_create_global failed");
1072                 return false;
1073             }
1074             if (vtype == TYPE_FIELD)
1075                 self->ir_values[ai]->fieldtype = elemtype->next->expression.vtype;
1076             self->ir_values[ai]->context = ast_ctx(self);
1077         }
1078         mem_d(name);
1079     }
1080     else
1081     {
1082         /* Arrays don't do this since there's no "array" value which spans across the
1083          * whole thing.
1084          */
1085         v = ir_builder_create_global(ir, self->name, self->expression.vtype);
1086         if (!v) {
1087             asterror(ast_ctx(self), "ir_builder_create_global failed");
1088             return false;
1089         }
1090         if (self->expression.vtype == TYPE_FIELD)
1091             v->fieldtype = self->expression.next->expression.vtype;
1092         v->context = ast_ctx(self);
1093     }
1094
1095     if (self->isconst) {
1096         switch (self->expression.vtype)
1097         {
1098             case TYPE_FLOAT:
1099                 if (!ir_value_set_float(v, self->constval.vfloat))
1100                     goto error;
1101                 break;
1102             case TYPE_VECTOR:
1103                 if (!ir_value_set_vector(v, self->constval.vvec))
1104                     goto error;
1105                 break;
1106             case TYPE_STRING:
1107                 if (!ir_value_set_string(v, self->constval.vstring))
1108                     goto error;
1109                 break;
1110             case TYPE_ARRAY:
1111                 asterror(ast_ctx(self), "TODO: global constant array");
1112                 break;
1113             case TYPE_FUNCTION:
1114                 asterror(ast_ctx(self), "global of type function not properly generated");
1115                 goto error;
1116                 /* Cannot generate an IR value for a function,
1117                  * need a pointer pointing to a function rather.
1118                  */
1119             default:
1120                 asterror(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1121                 break;
1122         }
1123     }
1124
1125     /* link us to the ir_value */
1126     self->ir_v = v;
1127     return true;
1128
1129 error: /* clean up */
1130     ir_value_delete(v);
1131     return false;
1132 }
1133
1134 bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
1135 {
1136     ir_value *v = NULL;
1137     if (self->isconst && self->expression.vtype == TYPE_FUNCTION)
1138     {
1139         /* Do we allow local functions? I think not...
1140          * this is NOT a function pointer atm.
1141          */
1142         return false;
1143     }
1144
1145     if (self->expression.vtype == TYPE_ARRAY) {
1146         size_t ai;
1147         char   *name;
1148         size_t  namelen;
1149
1150         ast_expression_common *elemtype = &self->expression.next->expression;
1151         int vtype = elemtype->vtype;
1152
1153         if (param) {
1154             asterror(ast_ctx(self), "array-parameters are not supported");
1155             return false;
1156         }
1157
1158         /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1159         if (!self->expression.count || self->expression.count > opts_max_array_size) {
1160             asterror(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1161         }
1162
1163         self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1164         if (!self->ir_values) {
1165             asterror(ast_ctx(self), "failed to allocate array values");
1166             return false;
1167         }
1168
1169         v = ir_function_create_local(func, self->name, vtype, param);
1170         if (!v) {
1171             asterror(ast_ctx(self), "ir_function_create_local failed");
1172             return false;
1173         }
1174         if (vtype == TYPE_FIELD)
1175             v->fieldtype = elemtype->next->expression.vtype;
1176         v->context = ast_ctx(self);
1177
1178         namelen = strlen(self->name);
1179         name    = (char*)mem_a(namelen + 16);
1180         strcpy(name, self->name);
1181
1182         self->ir_values[0] = v;
1183         for (ai = 1; ai < self->expression.count; ++ai) {
1184             snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1185             self->ir_values[ai] = ir_function_create_local(func, name, vtype, param);
1186             if (!self->ir_values[ai]) {
1187                 asterror(ast_ctx(self), "ir_builder_create_global failed");
1188                 return false;
1189             }
1190             if (vtype == TYPE_FIELD)
1191                 self->ir_values[ai]->fieldtype = elemtype->next->expression.vtype;
1192             self->ir_values[ai]->context = ast_ctx(self);
1193         }
1194     }
1195     else
1196     {
1197         v = ir_function_create_local(func, self->name, self->expression.vtype, param);
1198         if (!v)
1199             return false;
1200         if (self->expression.vtype == TYPE_FIELD)
1201             v->fieldtype = self->expression.next->expression.vtype;
1202         v->context = ast_ctx(self);
1203     }
1204
1205     /* A constant local... hmmm...
1206      * I suppose the IR will have to deal with this
1207      */
1208     if (self->isconst) {
1209         switch (self->expression.vtype)
1210         {
1211             case TYPE_FLOAT:
1212                 if (!ir_value_set_float(v, self->constval.vfloat))
1213                     goto error;
1214                 break;
1215             case TYPE_VECTOR:
1216                 if (!ir_value_set_vector(v, self->constval.vvec))
1217                     goto error;
1218                 break;
1219             case TYPE_STRING:
1220                 if (!ir_value_set_string(v, self->constval.vstring))
1221                     goto error;
1222                 break;
1223             default:
1224                 asterror(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1225                 break;
1226         }
1227     }
1228
1229     /* link us to the ir_value */
1230     self->ir_v = v;
1231
1232     if (self->setter) {
1233         if (!ast_global_codegen(self->setter, func->owner, false) ||
1234             !ast_function_codegen(self->setter->constval.vfunc, func->owner) ||
1235             !ir_function_finalize(self->setter->constval.vfunc->ir_func))
1236             return false;
1237     }
1238     if (self->getter) {
1239         if (!ast_global_codegen(self->getter, func->owner, false) ||
1240             !ast_function_codegen(self->getter->constval.vfunc, func->owner) ||
1241             !ir_function_finalize(self->getter->constval.vfunc->ir_func))
1242             return false;
1243     }
1244     return true;
1245
1246 error: /* clean up */
1247     ir_value_delete(v);
1248     return false;
1249 }
1250
1251 bool ast_function_codegen(ast_function *self, ir_builder *ir)
1252 {
1253     ir_function *irf;
1254     ir_value    *dummy;
1255     ast_expression_common *ec;
1256     size_t    i;
1257
1258     irf = self->ir_func;
1259     if (!irf) {
1260         asterror(ast_ctx(self), "ast_function's related ast_value was not generated yet");
1261         return false;
1262     }
1263
1264     /* fill the parameter list */
1265     ec = &self->vtype->expression;
1266     for (i = 0; i < vec_size(ec->params); ++i)
1267     {
1268         vec_push(irf->params, ec->params[i]->expression.vtype);
1269         if (!self->builtin) {
1270             if (!ast_local_codegen(ec->params[i], self->ir_func, true))
1271                 return false;
1272         }
1273     }
1274
1275     if (self->builtin) {
1276         irf->builtin = self->builtin;
1277         return true;
1278     }
1279
1280     if (!vec_size(self->blocks)) {
1281         asterror(ast_ctx(self), "function `%s` has no body", self->name);
1282         return false;
1283     }
1284
1285     self->curblock = ir_function_create_block(irf, "entry");
1286     if (!self->curblock) {
1287         asterror(ast_ctx(self), "failed to allocate entry block for `%s`", self->name);
1288         return false;
1289     }
1290
1291     for (i = 0; i < vec_size(self->blocks); ++i) {
1292         ast_expression_codegen *gen = self->blocks[i]->expression.codegen;
1293         if (!(*gen)((ast_expression*)self->blocks[i], self, false, &dummy))
1294             return false;
1295     }
1296
1297     /* TODO: check return types */
1298     if (!self->curblock->is_return)
1299     {
1300         return ir_block_create_return(self->curblock, NULL);
1301         /* From now on the parser has to handle this situation */
1302 #if 0
1303         if (!self->vtype->expression.next ||
1304             self->vtype->expression.next->expression.vtype == TYPE_VOID)
1305         {
1306             return ir_block_create_return(self->curblock, NULL);
1307         }
1308         else
1309         {
1310             /* error("missing return"); */
1311             asterror(ast_ctx(self), "function `%s` missing return value", self->name);
1312             return false;
1313         }
1314 #endif
1315     }
1316     return true;
1317 }
1318
1319 /* Note, you will not see ast_block_codegen generate ir_blocks.
1320  * To the AST and the IR, blocks are 2 different things.
1321  * In the AST it represents a block of code, usually enclosed in
1322  * curly braces {...}.
1323  * While in the IR it represents a block in terms of control-flow.
1324  */
1325 bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_value **out)
1326 {
1327     size_t i;
1328
1329     /* We don't use this
1330      * Note: an ast-representation using the comma-operator
1331      * of the form: (a, b, c) = x should not assign to c...
1332      */
1333     if (lvalue) {
1334         asterror(ast_ctx(self), "not an l-value (code-block)");
1335         return false;
1336     }
1337
1338     if (self->expression.outr) {
1339         *out = self->expression.outr;
1340         return true;
1341     }
1342
1343     /* output is NULL at first, we'll have each expression
1344      * assign to out output, thus, a comma-operator represention
1345      * using an ast_block will return the last generated value,
1346      * so: (b, c) + a  executed both b and c, and returns c,
1347      * which is then added to a.
1348      */
1349     *out = NULL;
1350
1351     /* generate locals */
1352     for (i = 0; i < vec_size(self->locals); ++i)
1353     {
1354         if (!ast_local_codegen(self->locals[i], func->ir_func, false)) {
1355             if (opts_debug)
1356                 asterror(ast_ctx(self), "failed to generate local `%s`", self->locals[i]->name);
1357             return false;
1358         }
1359     }
1360
1361     for (i = 0; i < vec_size(self->exprs); ++i)
1362     {
1363         ast_expression_codegen *gen = self->exprs[i]->expression.codegen;
1364         if (func->curblock->final) {
1365             asterror(ast_ctx(self->exprs[i]), "unreachable statement");
1366             return false;
1367         }
1368         if (!(*gen)(self->exprs[i], func, false, out))
1369             return false;
1370     }
1371
1372     self->expression.outr = *out;
1373
1374     return true;
1375 }
1376
1377 bool ast_store_codegen(ast_store *self, ast_function *func, bool lvalue, ir_value **out)
1378 {
1379     ast_expression_codegen *cgen;
1380     ir_value *left, *right;
1381
1382     ast_value       *arr;
1383     ast_value       *idx;
1384     ast_array_index *ai = NULL;
1385
1386     if (lvalue && self->expression.outl) {
1387         *out = self->expression.outl;
1388         return true;
1389     }
1390
1391     if (!lvalue && self->expression.outr) {
1392         *out = self->expression.outr;
1393         return true;
1394     }
1395
1396     if (ast_istype(self->dest, ast_array_index))
1397     {
1398
1399         ai = (ast_array_index*)self->dest;
1400         idx = (ast_value*)ai->index;
1401
1402         if (ast_istype(ai->index, ast_value) && idx->isconst)
1403             ai = NULL;
1404     }
1405
1406     if (ai) {
1407         /* we need to call the setter */
1408         ir_value  *iridx, *funval;
1409         ir_instr  *call;
1410
1411         if (lvalue) {
1412             asterror(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1413             return false;
1414         }
1415
1416         arr = (ast_value*)ai->array;
1417         if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1418             asterror(ast_ctx(self), "value has no setter (%s)", arr->name);
1419             return false;
1420         }
1421
1422         cgen = idx->expression.codegen;
1423         if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1424             return false;
1425
1426         cgen = arr->setter->expression.codegen;
1427         if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
1428             return false;
1429
1430         cgen = self->source->expression.codegen;
1431         if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1432             return false;
1433
1434         call = ir_block_create_call(func->curblock, ast_function_label(func, "store"), funval);
1435         if (!call)
1436             return false;
1437         ir_call_param(call, iridx);
1438         ir_call_param(call, right);
1439         self->expression.outr = right;
1440     }
1441     else
1442     {
1443         /* regular code */
1444
1445         cgen = self->dest->expression.codegen;
1446         /* lvalue! */
1447         if (!(*cgen)((ast_expression*)(self->dest), func, true, &left))
1448             return false;
1449         self->expression.outl = left;
1450
1451         cgen = self->source->expression.codegen;
1452         /* rvalue! */
1453         if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1454             return false;
1455
1456         if (!ir_block_create_store_op(func->curblock, self->op, left, right))
1457             return false;
1458         self->expression.outr = right;
1459     }
1460
1461     /* Theoretically, an assinment returns its left side as an
1462      * lvalue, if we don't need an lvalue though, we return
1463      * the right side as an rvalue, otherwise we have to
1464      * somehow know whether or not we need to dereference the pointer
1465      * on the left side - that is: OP_LOAD if it was an address.
1466      * Also: in original QC we cannot OP_LOADP *anyway*.
1467      */
1468     *out = (lvalue ? left : right);
1469
1470     return true;
1471 }
1472
1473 bool ast_binary_codegen(ast_binary *self, ast_function *func, bool lvalue, ir_value **out)
1474 {
1475     ast_expression_codegen *cgen;
1476     ir_value *left, *right;
1477
1478     /* A binary operation cannot yield an l-value */
1479     if (lvalue) {
1480         asterror(ast_ctx(self), "not an l-value (binop)");
1481         return false;
1482     }
1483
1484     if (self->expression.outr) {
1485         *out = self->expression.outr;
1486         return true;
1487     }
1488
1489     cgen = self->left->expression.codegen;
1490     /* lvalue! */
1491     if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1492         return false;
1493
1494     cgen = self->right->expression.codegen;
1495     /* rvalue! */
1496     if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1497         return false;
1498
1499     *out = ir_block_create_binop(func->curblock, ast_function_label(func, "bin"),
1500                                  self->op, left, right);
1501     if (!*out)
1502         return false;
1503     self->expression.outr = *out;
1504
1505     return true;
1506 }
1507
1508 bool ast_binstore_codegen(ast_binstore *self, ast_function *func, bool lvalue, ir_value **out)
1509 {
1510     ast_expression_codegen *cgen;
1511     ir_value *leftl, *leftr, *right, *bin;
1512
1513     if (lvalue && self->expression.outl) {
1514         *out = self->expression.outl;
1515         return true;
1516     }
1517
1518     if (!lvalue && self->expression.outr) {
1519         *out = self->expression.outr;
1520         return true;
1521     }
1522
1523     /* for a binstore we need both an lvalue and an rvalue for the left side */
1524     /* rvalue of destination! */
1525     cgen = self->dest->expression.codegen;
1526     if (!(*cgen)((ast_expression*)(self->dest), func, false, &leftr))
1527         return false;
1528
1529     /* source as rvalue only */
1530     cgen = self->source->expression.codegen;
1531     if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1532         return false;
1533
1534     /* now the binary */
1535     bin = ir_block_create_binop(func->curblock, ast_function_label(func, "binst"),
1536                                 self->opbin, leftr, right);
1537     self->expression.outr = bin;
1538
1539     /* now store them */
1540     cgen = self->dest->expression.codegen;
1541     /* lvalue of destination */
1542     if (!(*cgen)((ast_expression*)(self->dest), func, true, &leftl))
1543         return false;
1544     self->expression.outl = leftl;
1545
1546     if (!ir_block_create_store_op(func->curblock, self->opstore, leftl, bin))
1547         return false;
1548     self->expression.outr = bin;
1549
1550     /* Theoretically, an assinment returns its left side as an
1551      * lvalue, if we don't need an lvalue though, we return
1552      * the right side as an rvalue, otherwise we have to
1553      * somehow know whether or not we need to dereference the pointer
1554      * on the left side - that is: OP_LOAD if it was an address.
1555      * Also: in original QC we cannot OP_LOADP *anyway*.
1556      */
1557     *out = (lvalue ? leftl : bin);
1558
1559     return true;
1560 }
1561
1562 bool ast_unary_codegen(ast_unary *self, ast_function *func, bool lvalue, ir_value **out)
1563 {
1564     ast_expression_codegen *cgen;
1565     ir_value *operand;
1566
1567     /* An unary operation cannot yield an l-value */
1568     if (lvalue) {
1569         asterror(ast_ctx(self), "not an l-value (binop)");
1570         return false;
1571     }
1572
1573     if (self->expression.outr) {
1574         *out = self->expression.outr;
1575         return true;
1576     }
1577
1578     cgen = self->operand->expression.codegen;
1579     /* lvalue! */
1580     if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
1581         return false;
1582
1583     *out = ir_block_create_unary(func->curblock, ast_function_label(func, "unary"),
1584                                  self->op, operand);
1585     if (!*out)
1586         return false;
1587     self->expression.outr = *out;
1588
1589     return true;
1590 }
1591
1592 bool ast_return_codegen(ast_return *self, ast_function *func, bool lvalue, ir_value **out)
1593 {
1594     ast_expression_codegen *cgen;
1595     ir_value *operand;
1596
1597     /* In the context of a return operation, we don't actually return
1598      * anything...
1599      */
1600     if (lvalue) {
1601         asterror(ast_ctx(self), "return-expression is not an l-value");
1602         return false;
1603     }
1604
1605     if (self->expression.outr) {
1606         asterror(ast_ctx(self), "internal error: ast_return cannot be reused, it bears no result!");
1607         return false;
1608     }
1609     self->expression.outr = (ir_value*)1;
1610
1611     if (self->operand) {
1612         cgen = self->operand->expression.codegen;
1613         /* lvalue! */
1614         if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
1615             return false;
1616
1617         if (!ir_block_create_return(func->curblock, operand))
1618             return false;
1619     } else {
1620         if (!ir_block_create_return(func->curblock, NULL))
1621             return false;
1622     }
1623
1624     return true;
1625 }
1626
1627 bool ast_entfield_codegen(ast_entfield *self, ast_function *func, bool lvalue, ir_value **out)
1628 {
1629     ast_expression_codegen *cgen;
1630     ir_value *ent, *field;
1631
1632     /* This function needs to take the 'lvalue' flag into account!
1633      * As lvalue we provide a field-pointer, as rvalue we provide the
1634      * value in a temp.
1635      */
1636
1637     if (lvalue && self->expression.outl) {
1638         *out = self->expression.outl;
1639         return true;
1640     }
1641
1642     if (!lvalue && self->expression.outr) {
1643         *out = self->expression.outr;
1644         return true;
1645     }
1646
1647     cgen = self->entity->expression.codegen;
1648     if (!(*cgen)((ast_expression*)(self->entity), func, false, &ent))
1649         return false;
1650
1651     cgen = self->field->expression.codegen;
1652     if (!(*cgen)((ast_expression*)(self->field), func, false, &field))
1653         return false;
1654
1655     if (lvalue) {
1656         /* address! */
1657         *out = ir_block_create_fieldaddress(func->curblock, ast_function_label(func, "efa"),
1658                                             ent, field);
1659     } else {
1660         *out = ir_block_create_load_from_ent(func->curblock, ast_function_label(func, "efv"),
1661                                              ent, field, self->expression.vtype);
1662     }
1663     if (!*out) {
1664         asterror(ast_ctx(self), "failed to create %s instruction (output type %s)",
1665                  (lvalue ? "ADDRESS" : "FIELD"),
1666                  type_name[self->expression.vtype]);
1667         return false;
1668     }
1669
1670     if (lvalue)
1671         self->expression.outl = *out;
1672     else
1673         self->expression.outr = *out;
1674
1675     /* Hm that should be it... */
1676     return true;
1677 }
1678
1679 bool ast_member_codegen(ast_member *self, ast_function *func, bool lvalue, ir_value **out)
1680 {
1681     ast_expression_codegen *cgen;
1682     ir_value *vec;
1683
1684     /* in QC this is always an lvalue */
1685     (void)lvalue;
1686     if (self->expression.outl) {
1687         *out = self->expression.outl;
1688         return true;
1689     }
1690
1691     cgen = self->owner->expression.codegen;
1692     if (!(*cgen)((ast_expression*)(self->owner), func, true, &vec))
1693         return false;
1694
1695     if (vec->vtype != TYPE_VECTOR &&
1696         !(vec->vtype == TYPE_FIELD && self->owner->expression.next->expression.vtype == TYPE_VECTOR))
1697     {
1698         return false;
1699     }
1700
1701     *out = ir_value_vector_member(vec, self->field);
1702     self->expression.outl = *out;
1703
1704     return (*out != NULL);
1705 }
1706
1707 bool ast_array_index_codegen(ast_array_index *self, ast_function *func, bool lvalue, ir_value **out)
1708 {
1709     ast_value *arr;
1710     ast_value *idx;
1711
1712     if (!lvalue && self->expression.outr) {
1713         *out = self->expression.outr;
1714     }
1715     if (lvalue && self->expression.outl) {
1716         *out = self->expression.outl;
1717     }
1718
1719     if (!ast_istype(self->array, ast_value)) {
1720         asterror(ast_ctx(self), "array indexing this way is not supported");
1721         /* note this would actually be pointer indexing because the left side is
1722          * not an actual array but (hopefully) an indexable expression.
1723          * Once we get integer arithmetic, and GADDRESS/GSTORE/GLOAD instruction
1724          * support this path will be filled.
1725          */
1726         return false;
1727     }
1728
1729     arr = (ast_value*)self->array;
1730     idx = (ast_value*)self->index;
1731
1732     if (!ast_istype(self->index, ast_value) || !idx->isconst) {
1733         /* Time to use accessor functions */
1734         ast_expression_codegen *cgen;
1735         ir_value               *iridx, *funval;
1736         ir_instr               *call;
1737
1738         if (lvalue) {
1739             asterror(ast_ctx(self), "(.2) array indexing here needs a compile-time constant");
1740             return false;
1741         }
1742
1743         if (!arr->getter) {
1744             asterror(ast_ctx(self), "value has no getter, don't know how to index it");
1745             return false;
1746         }
1747
1748         cgen = self->index->expression.codegen;
1749         if (!(*cgen)((ast_expression*)(self->index), func, true, &iridx))
1750             return false;
1751
1752         cgen = arr->getter->expression.codegen;
1753         if (!(*cgen)((ast_expression*)(arr->getter), func, true, &funval))
1754             return false;
1755
1756         call = ir_block_create_call(func->curblock, ast_function_label(func, "fetch"), funval);
1757         if (!call)
1758             return false;
1759         ir_call_param(call, iridx);
1760
1761         *out = ir_call_value(call);
1762         self->expression.outr = *out;
1763         return true;
1764     }
1765
1766     if (idx->expression.vtype == TYPE_FLOAT)
1767         *out = arr->ir_values[(int)idx->constval.vfloat];
1768     else if (idx->expression.vtype == TYPE_INTEGER)
1769         *out = arr->ir_values[idx->constval.vint];
1770     else {
1771         asterror(ast_ctx(self), "array indexing here needs an integer constant");
1772         return false;
1773     }
1774     return true;
1775 }
1776
1777 bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_value **out)
1778 {
1779     ast_expression_codegen *cgen;
1780
1781     ir_value *condval;
1782     ir_value *dummy;
1783
1784     ir_block *cond = func->curblock;
1785     ir_block *ontrue;
1786     ir_block *onfalse;
1787     ir_block *ontrue_endblock = NULL;
1788     ir_block *onfalse_endblock = NULL;
1789     ir_block *merge;
1790
1791     /* We don't output any value, thus also don't care about r/lvalue */
1792     (void)out;
1793     (void)lvalue;
1794
1795     if (self->expression.outr) {
1796         asterror(ast_ctx(self), "internal error: ast_ifthen cannot be reused, it bears no result!");
1797         return false;
1798     }
1799     self->expression.outr = (ir_value*)1;
1800
1801     /* generate the condition */
1802     func->curblock = cond;
1803     cgen = self->cond->expression.codegen;
1804     if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
1805         return false;
1806
1807     /* on-true path */
1808
1809     if (self->on_true) {
1810         /* create on-true block */
1811         ontrue = ir_function_create_block(func->ir_func, ast_function_label(func, "ontrue"));
1812         if (!ontrue)
1813             return false;
1814
1815         /* enter the block */
1816         func->curblock = ontrue;
1817
1818         /* generate */
1819         cgen = self->on_true->expression.codegen;
1820         if (!(*cgen)((ast_expression*)(self->on_true), func, false, &dummy))
1821             return false;
1822
1823         /* we now need to work from the current endpoint */
1824         ontrue_endblock = func->curblock;
1825     } else
1826         ontrue = NULL;
1827
1828     /* on-false path */
1829     if (self->on_false) {
1830         /* create on-false block */
1831         onfalse = ir_function_create_block(func->ir_func, ast_function_label(func, "onfalse"));
1832         if (!onfalse)
1833             return false;
1834
1835         /* enter the block */
1836         func->curblock = onfalse;
1837
1838         /* generate */
1839         cgen = self->on_false->expression.codegen;
1840         if (!(*cgen)((ast_expression*)(self->on_false), func, false, &dummy))
1841             return false;
1842
1843         /* we now need to work from the current endpoint */
1844         onfalse_endblock = func->curblock;
1845     } else
1846         onfalse = NULL;
1847
1848     /* Merge block were they all merge in to */
1849     merge = ir_function_create_block(func->ir_func, ast_function_label(func, "endif"));
1850     if (!merge)
1851         return false;
1852
1853     /* add jumps ot the merge block */
1854     if (ontrue && !ontrue_endblock->final && !ir_block_create_jump(ontrue_endblock, merge))
1855         return false;
1856     if (onfalse && !onfalse_endblock->final && !ir_block_create_jump(onfalse_endblock, merge))
1857         return false;
1858
1859     /* we create the if here, that way all blocks are ordered :)
1860      */
1861     if (!ir_block_create_if(cond, condval,
1862                             (ontrue  ? ontrue  : merge),
1863                             (onfalse ? onfalse : merge)))
1864     {
1865         return false;
1866     }
1867
1868     /* Now enter the merge block */
1869     func->curblock = merge;
1870
1871     return true;
1872 }
1873
1874 bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_value **out)
1875 {
1876     ast_expression_codegen *cgen;
1877
1878     ir_value *condval;
1879     ir_value *trueval, *falseval;
1880     ir_instr *phi;
1881
1882     ir_block *cond = func->curblock;
1883     ir_block *ontrue;
1884     ir_block *onfalse;
1885     ir_block *merge;
1886
1887     /* Ternary can never create an lvalue... */
1888     if (lvalue)
1889         return false;
1890
1891     /* In theory it shouldn't be possible to pass through a node twice, but
1892      * in case we add any kind of optimization pass for the AST itself, it
1893      * may still happen, thus we remember a created ir_value and simply return one
1894      * if it already exists.
1895      */
1896     if (self->phi_out) {
1897         *out = self->phi_out;
1898         return true;
1899     }
1900
1901     /* In the following, contraty to ast_ifthen, we assume both paths exist. */
1902
1903     /* generate the condition */
1904     func->curblock = cond;
1905     cgen = self->cond->expression.codegen;
1906     if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
1907         return false;
1908
1909     /* create on-true block */
1910     ontrue = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_T"));
1911     if (!ontrue)
1912         return false;
1913     else
1914     {
1915         /* enter the block */
1916         func->curblock = ontrue;
1917
1918         /* generate */
1919         cgen = self->on_true->expression.codegen;
1920         if (!(*cgen)((ast_expression*)(self->on_true), func, false, &trueval))
1921             return false;
1922     }
1923
1924     /* create on-false block */
1925     onfalse = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_F"));
1926     if (!onfalse)
1927         return false;
1928     else
1929     {
1930         /* enter the block */
1931         func->curblock = onfalse;
1932
1933         /* generate */
1934         cgen = self->on_false->expression.codegen;
1935         if (!(*cgen)((ast_expression*)(self->on_false), func, false, &falseval))
1936             return false;
1937     }
1938
1939     /* create merge block */
1940     merge = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_out"));
1941     if (!merge)
1942         return false;
1943     /* jump to merge block */
1944     if (!ir_block_create_jump(ontrue, merge))
1945         return false;
1946     if (!ir_block_create_jump(onfalse, merge))
1947         return false;
1948
1949     /* create if instruction */
1950     if (!ir_block_create_if(cond, condval, ontrue, onfalse))
1951         return false;
1952
1953     /* Now enter the merge block */
1954     func->curblock = merge;
1955
1956     /* Here, now, we need a PHI node
1957      * but first some sanity checking...
1958      */
1959     if (trueval->vtype != falseval->vtype) {
1960         /* error("ternary with different types on the two sides"); */
1961         return false;
1962     }
1963
1964     /* create PHI */
1965     phi = ir_block_create_phi(merge, ast_function_label(func, "phi"), trueval->vtype);
1966     if (!phi)
1967         return false;
1968     ir_phi_add(phi, ontrue,  trueval);
1969     ir_phi_add(phi, onfalse, falseval);
1970
1971     self->phi_out = ir_phi_value(phi);
1972     *out = self->phi_out;
1973
1974     return true;
1975 }
1976
1977 bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value **out)
1978 {
1979     ast_expression_codegen *cgen;
1980
1981     ir_value *dummy      = NULL;
1982     ir_value *precond    = NULL;
1983     ir_value *postcond   = NULL;
1984
1985     /* Since we insert some jumps "late" so we have blocks
1986      * ordered "nicely", we need to keep track of the actual end-blocks
1987      * of expressions to add the jumps to.
1988      */
1989     ir_block *bbody      = NULL, *end_bbody      = NULL;
1990     ir_block *bprecond   = NULL, *end_bprecond   = NULL;
1991     ir_block *bpostcond  = NULL, *end_bpostcond  = NULL;
1992     ir_block *bincrement = NULL, *end_bincrement = NULL;
1993     ir_block *bout       = NULL, *bin            = NULL;
1994
1995     /* let's at least move the outgoing block to the end */
1996     size_t    bout_id;
1997
1998     /* 'break' and 'continue' need to be able to find the right blocks */
1999     ir_block *bcontinue     = NULL;
2000     ir_block *bbreak        = NULL;
2001
2002     ir_block *old_bcontinue = NULL;
2003     ir_block *old_bbreak    = NULL;
2004
2005     ir_block *tmpblock      = NULL;
2006
2007     (void)lvalue;
2008     (void)out;
2009
2010     if (self->expression.outr) {
2011         asterror(ast_ctx(self), "internal error: ast_loop cannot be reused, it bears no result!");
2012         return false;
2013     }
2014     self->expression.outr = (ir_value*)1;
2015
2016     /* NOTE:
2017      * Should we ever need some kind of block ordering, better make this function
2018      * move blocks around than write a block ordering algorithm later... after all
2019      * the ast and ir should work together, not against each other.
2020      */
2021
2022     /* initexpr doesn't get its own block, it's pointless, it could create more blocks
2023      * anyway if for example it contains a ternary.
2024      */
2025     if (self->initexpr)
2026     {
2027         cgen = self->initexpr->expression.codegen;
2028         if (!(*cgen)((ast_expression*)(self->initexpr), func, false, &dummy))
2029             return false;
2030     }
2031
2032     /* Store the block from which we enter this chaos */
2033     bin = func->curblock;
2034
2035     /* The pre-loop condition needs its own block since we
2036      * need to be able to jump to the start of that expression.
2037      */
2038     if (self->precond)
2039     {
2040         bprecond = ir_function_create_block(func->ir_func, ast_function_label(func, "pre_loop_cond"));
2041         if (!bprecond)
2042             return false;
2043
2044         /* the pre-loop-condition the least important place to 'continue' at */
2045         bcontinue = bprecond;
2046
2047         /* enter */
2048         func->curblock = bprecond;
2049
2050         /* generate */
2051         cgen = self->precond->expression.codegen;
2052         if (!(*cgen)((ast_expression*)(self->precond), func, false, &precond))
2053             return false;
2054
2055         end_bprecond = func->curblock;
2056     } else {
2057         bprecond = end_bprecond = NULL;
2058     }
2059
2060     /* Now the next blocks won't be ordered nicely, but we need to
2061      * generate them this early for 'break' and 'continue'.
2062      */
2063     if (self->increment) {
2064         bincrement = ir_function_create_block(func->ir_func, ast_function_label(func, "loop_increment"));
2065         if (!bincrement)
2066             return false;
2067         bcontinue = bincrement; /* increment comes before the pre-loop-condition */
2068     } else {
2069         bincrement = end_bincrement = NULL;
2070     }
2071
2072     if (self->postcond) {
2073         bpostcond = ir_function_create_block(func->ir_func, ast_function_label(func, "post_loop_cond"));
2074         if (!bpostcond)
2075             return false;
2076         bcontinue = bpostcond; /* postcond comes before the increment */
2077     } else {
2078         bpostcond = end_bpostcond = NULL;
2079     }
2080
2081     bout_id = vec_size(func->ir_func->blocks);
2082     bout = ir_function_create_block(func->ir_func, ast_function_label(func, "after_loop"));
2083     if (!bout)
2084         return false;
2085     bbreak = bout;
2086
2087     /* The loop body... */
2088     if (self->body)
2089     {
2090         bbody = ir_function_create_block(func->ir_func, ast_function_label(func, "loop_body"));
2091         if (!bbody)
2092             return false;
2093
2094         /* enter */
2095         func->curblock = bbody;
2096
2097         old_bbreak          = func->breakblock;
2098         old_bcontinue       = func->continueblock;
2099         func->breakblock    = bbreak;
2100         func->continueblock = bcontinue;
2101
2102         /* generate */
2103         cgen = self->body->expression.codegen;
2104         if (!(*cgen)((ast_expression*)(self->body), func, false, &dummy))
2105             return false;
2106
2107         end_bbody = func->curblock;
2108         func->breakblock    = old_bbreak;
2109         func->continueblock = old_bcontinue;
2110     }
2111
2112     /* post-loop-condition */
2113     if (self->postcond)
2114     {
2115         /* enter */
2116         func->curblock = bpostcond;
2117
2118         /* generate */
2119         cgen = self->postcond->expression.codegen;
2120         if (!(*cgen)((ast_expression*)(self->postcond), func, false, &postcond))
2121             return false;
2122
2123         end_bpostcond = func->curblock;
2124     }
2125
2126     /* The incrementor */
2127     if (self->increment)
2128     {
2129         /* enter */
2130         func->curblock = bincrement;
2131
2132         /* generate */
2133         cgen = self->increment->expression.codegen;
2134         if (!(*cgen)((ast_expression*)(self->increment), func, false, &dummy))
2135             return false;
2136
2137         end_bincrement = func->curblock;
2138     }
2139
2140     /* In any case now, we continue from the outgoing block */
2141     func->curblock = bout;
2142
2143     /* Now all blocks are in place */
2144     /* From 'bin' we jump to whatever comes first */
2145     if      (bprecond)   tmpblock = bprecond;
2146     else if (bbody)      tmpblock = bbody;
2147     else if (bpostcond)  tmpblock = bpostcond;
2148     else                 tmpblock = bout;
2149     if (!ir_block_create_jump(bin, tmpblock))
2150         return false;
2151
2152     /* From precond */
2153     if (bprecond)
2154     {
2155         ir_block *ontrue, *onfalse;
2156         if      (bbody)      ontrue = bbody;
2157         else if (bincrement) ontrue = bincrement;
2158         else if (bpostcond)  ontrue = bpostcond;
2159         else                 ontrue = bprecond;
2160         onfalse = bout;
2161         if (!ir_block_create_if(end_bprecond, precond, ontrue, onfalse))
2162             return false;
2163     }
2164
2165     /* from body */
2166     if (bbody)
2167     {
2168         if      (bincrement) tmpblock = bincrement;
2169         else if (bpostcond)  tmpblock = bpostcond;
2170         else if (bprecond)   tmpblock = bprecond;
2171         else                 tmpblock = bout;
2172         if (!end_bbody->final && !ir_block_create_jump(end_bbody, tmpblock))
2173             return false;
2174     }
2175
2176     /* from increment */
2177     if (bincrement)
2178     {
2179         if      (bpostcond)  tmpblock = bpostcond;
2180         else if (bprecond)   tmpblock = bprecond;
2181         else if (bbody)      tmpblock = bbody;
2182         else                 tmpblock = bout;
2183         if (!ir_block_create_jump(end_bincrement, tmpblock))
2184             return false;
2185     }
2186
2187     /* from postcond */
2188     if (bpostcond)
2189     {
2190         ir_block *ontrue, *onfalse;
2191         if      (bprecond)   ontrue = bprecond;
2192         else if (bbody)      ontrue = bbody;
2193         else if (bincrement) ontrue = bincrement;
2194         else                 ontrue = bpostcond;
2195         onfalse = bout;
2196         if (!ir_block_create_if(end_bpostcond, postcond, ontrue, onfalse))
2197             return false;
2198     }
2199
2200     /* Move 'bout' to the end */
2201     vec_remove(func->ir_func->blocks, bout_id, 1);
2202     vec_push(func->ir_func->blocks, bout);
2203
2204     return true;
2205 }
2206
2207 bool ast_breakcont_codegen(ast_breakcont *self, ast_function *func, bool lvalue, ir_value **out)
2208 {
2209     ir_block *target;
2210
2211     if (lvalue) {
2212         asterror(ast_ctx(self), "break/continue expression is not an l-value");
2213         return false;
2214     }
2215
2216     if (self->expression.outr) {
2217         asterror(ast_ctx(self), "internal error: ast_breakcont cannot be reused!");
2218         return false;
2219     }
2220     self->expression.outr = (ir_value*)1;
2221
2222     if (self->is_continue)
2223         target = func->continueblock;
2224     else
2225         target = func->breakblock;
2226
2227     if (!ir_block_create_jump(func->curblock, target))
2228         return false;
2229     return true;
2230 }
2231
2232 bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value **out)
2233 {
2234     ast_expression_codegen *cgen;
2235     ir_value              **params;
2236     ir_instr               *callinstr;
2237     size_t i;
2238
2239     ir_value *funval = NULL;
2240
2241     /* return values are never lvalues */
2242     if (lvalue) {
2243         asterror(ast_ctx(self), "not an l-value (function call)");
2244         return false;
2245     }
2246
2247     if (self->expression.outr) {
2248         *out = self->expression.outr;
2249         return true;
2250     }
2251
2252     cgen = self->func->expression.codegen;
2253     if (!(*cgen)((ast_expression*)(self->func), func, false, &funval))
2254         return false;
2255     if (!funval)
2256         return false;
2257
2258     params = NULL;
2259
2260     /* parameters */
2261     for (i = 0; i < vec_size(self->params); ++i)
2262     {
2263         ir_value *param;
2264         ast_expression *expr = self->params[i];
2265
2266         cgen = expr->expression.codegen;
2267         if (!(*cgen)(expr, func, false, &param))
2268             goto error;
2269         if (!param)
2270             goto error;
2271         vec_push(params, param);
2272     }
2273
2274     callinstr = ir_block_create_call(func->curblock, ast_function_label(func, "call"), funval);
2275     if (!callinstr)
2276         goto error;
2277
2278     for (i = 0; i < vec_size(params); ++i) {
2279         ir_call_param(callinstr, params[i]);
2280     }
2281
2282     *out = ir_call_value(callinstr);
2283     self->expression.outr = *out;
2284
2285     vec_free(params);
2286     return true;
2287 error:
2288     vec_free(params);
2289     return false;
2290 }