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:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
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
30 #define ast_instantiate(T, ctx, destroyfn) \
31 T* self = (T*)mem_a(sizeof(T)); \
35 ast_node_init((ast_node*)self, ctx, TYPE_##T); \
36 ( (ast_node*)self )->node.destroy = (ast_node_delete*)destroyfn
39 /* It must not be possible to get here. */
40 static GMQCC_NORETURN void _ast_node_destroy(ast_node *self)
43 con_err("ast node missing destroy()\n");
47 /* Initialize main ast node aprts */
48 static void ast_node_init(ast_node *self, lex_ctx ctx, int nodetype)
50 self->node.context = ctx;
51 self->node.destroy = &_ast_node_destroy;
52 self->node.keep = false;
53 self->node.nodetype = nodetype;
54 self->node.side_effects = false;
57 /* weight and side effects */
58 static void _ast_propagate_effects(ast_node *self, ast_node *other)
60 if (ast_side_effects(other))
61 ast_side_effects(self) = true;
63 #define ast_propagate_effects(s,o) _ast_propagate_effects(((ast_node*)(s)), ((ast_node*)(o)))
65 /* General expression initialization */
66 static void ast_expression_init(ast_expression *self,
67 ast_expression_codegen *codegen)
69 self->expression.codegen = codegen;
70 self->expression.vtype = TYPE_VOID;
71 self->expression.next = NULL;
72 self->expression.outl = NULL;
73 self->expression.outr = NULL;
74 self->expression.variadic = false;
75 self->expression.params = NULL;
78 static void ast_expression_delete(ast_expression *self)
81 if (self->expression.next)
82 ast_delete(self->expression.next);
83 for (i = 0; i < vec_size(self->expression.params); ++i) {
84 ast_delete(self->expression.params[i]);
86 vec_free(self->expression.params);
89 static void ast_expression_delete_full(ast_expression *self)
91 ast_expression_delete(self);
95 ast_value* ast_value_copy(const ast_value *self)
98 const ast_expression_common *fromex;
99 ast_expression_common *selfex;
100 ast_value *cp = ast_value_new(self->expression.node.context, self->name, self->expression.vtype);
101 if (self->expression.next) {
102 cp->expression.next = ast_type_copy(self->expression.node.context, self->expression.next);
103 if (!cp->expression.next) {
104 ast_value_delete(cp);
108 fromex = &self->expression;
109 selfex = &cp->expression;
110 selfex->variadic = fromex->variadic;
111 for (i = 0; i < vec_size(fromex->params); ++i) {
112 ast_value *v = ast_value_copy(fromex->params[i]);
114 ast_value_delete(cp);
117 vec_push(selfex->params, v);
122 bool ast_type_adopt_impl(ast_expression *self, const ast_expression *other)
125 const ast_expression_common *fromex;
126 ast_expression_common *selfex;
127 self->expression.vtype = other->expression.vtype;
128 if (other->expression.next) {
129 self->expression.next = (ast_expression*)ast_type_copy(ast_ctx(self), other->expression.next);
130 if (!self->expression.next)
133 fromex = &other->expression;
134 selfex = &self->expression;
135 selfex->variadic = fromex->variadic;
136 for (i = 0; i < vec_size(fromex->params); ++i) {
137 ast_value *v = ast_value_copy(fromex->params[i]);
140 vec_push(selfex->params, v);
145 static ast_expression* ast_shallow_type(lex_ctx ctx, int vtype)
147 ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
148 ast_expression_init(self, NULL);
149 self->expression.codegen = NULL;
150 self->expression.next = NULL;
151 self->expression.vtype = vtype;
155 ast_expression* ast_type_copy(lex_ctx ctx, const ast_expression *ex)
158 const ast_expression_common *fromex;
159 ast_expression_common *selfex;
165 ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
166 ast_expression_init(self, NULL);
168 fromex = &ex->expression;
169 selfex = &self->expression;
171 /* This may never be codegen()d */
172 selfex->codegen = NULL;
174 selfex->vtype = fromex->vtype;
177 selfex->next = ast_type_copy(ctx, fromex->next);
179 ast_expression_delete_full(self);
186 selfex->variadic = fromex->variadic;
187 for (i = 0; i < vec_size(fromex->params); ++i) {
188 ast_value *v = ast_value_copy(fromex->params[i]);
190 ast_expression_delete_full(self);
193 vec_push(selfex->params, v);
200 bool ast_compare_type(ast_expression *a, ast_expression *b)
202 if (a->expression.vtype != b->expression.vtype)
204 if (!a->expression.next != !b->expression.next)
206 if (vec_size(a->expression.params) != vec_size(b->expression.params))
208 if (a->expression.variadic != b->expression.variadic)
210 if (vec_size(a->expression.params)) {
212 for (i = 0; i < vec_size(a->expression.params); ++i) {
213 if (!ast_compare_type((ast_expression*)a->expression.params[i],
214 (ast_expression*)b->expression.params[i]))
218 if (a->expression.next)
219 return ast_compare_type(a->expression.next, b->expression.next);
223 static size_t ast_type_to_string_impl(ast_expression *e, char *buf, size_t bufsize, size_t pos)
230 if (pos + 6 >= bufsize)
232 strcpy(buf + pos, "(null)");
236 if (pos + 1 >= bufsize)
239 switch (e->expression.vtype) {
241 strcpy(buf + pos, "(variant)");
246 return ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
249 if (pos + 3 >= bufsize)
253 pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
254 if (pos + 1 >= bufsize)
260 pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
261 if (pos + 2 >= bufsize)
263 if (!vec_size(e->expression.params)) {
269 pos = ast_type_to_string_impl((ast_expression*)(e->expression.params[0]), buf, bufsize, pos);
270 for (i = 1; i < vec_size(e->expression.params); ++i) {
271 if (pos + 2 >= bufsize)
275 pos = ast_type_to_string_impl((ast_expression*)(e->expression.params[i]), buf, bufsize, pos);
277 if (pos + 1 >= bufsize)
283 pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
284 if (pos + 1 >= bufsize)
287 pos += snprintf(buf + pos, bufsize - pos - 1, "%i", (int)e->expression.count);
288 if (pos + 1 >= bufsize)
294 typestr = type_name[e->expression.vtype];
295 typelen = strlen(typestr);
296 if (pos + typelen >= bufsize)
298 strcpy(buf + pos, typestr);
299 return pos + typelen;
303 buf[bufsize-3] = '.';
304 buf[bufsize-2] = '.';
305 buf[bufsize-1] = '.';
309 void ast_type_to_string(ast_expression *e, char *buf, size_t bufsize)
311 size_t pos = ast_type_to_string_impl(e, buf, bufsize-1, 0);
315 ast_value* ast_value_new(lex_ctx ctx, const char *name, int t)
317 ast_instantiate(ast_value, ctx, ast_value_delete);
318 ast_expression_init((ast_expression*)self,
319 (ast_expression_codegen*)&ast_value_codegen);
320 self->expression.node.keep = true; /* keep */
322 self->name = name ? util_strdup(name) : NULL;
323 self->expression.vtype = t;
324 self->expression.next = NULL;
325 self->isfield = false;
327 self->hasvalue = false;
329 memset(&self->constval, 0, sizeof(self->constval));
332 self->ir_values = NULL;
333 self->ir_value_count = 0;
341 void ast_value_delete(ast_value* self)
344 mem_d((void*)self->name);
345 if (self->hasvalue) {
346 switch (self->expression.vtype)
349 mem_d((void*)self->constval.vstring);
352 /* unlink us from the function node */
353 self->constval.vfunc->vtype = NULL;
355 /* NOTE: delete function? currently collected in
356 * the parser structure
363 mem_d(self->ir_values);
364 ast_expression_delete((ast_expression*)self);
368 void ast_value_params_add(ast_value *self, ast_value *p)
370 vec_push(self->expression.params, p);
373 bool ast_value_set_name(ast_value *self, const char *name)
376 mem_d((void*)self->name);
377 self->name = util_strdup(name);
381 ast_binary* ast_binary_new(lex_ctx ctx, int op,
382 ast_expression* left, ast_expression* right)
384 ast_instantiate(ast_binary, ctx, ast_binary_delete);
385 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binary_codegen);
391 ast_propagate_effects(self, left);
392 ast_propagate_effects(self, right);
394 if (op >= INSTR_EQ_F && op <= INSTR_GT)
395 self->expression.vtype = TYPE_FLOAT;
396 else if (op == INSTR_AND || op == INSTR_OR ||
397 op == INSTR_BITAND || op == INSTR_BITOR)
398 self->expression.vtype = TYPE_FLOAT;
399 else if (op == INSTR_MUL_VF || op == INSTR_MUL_FV)
400 self->expression.vtype = TYPE_VECTOR;
401 else if (op == INSTR_MUL_V)
402 self->expression.vtype = TYPE_FLOAT;
404 self->expression.vtype = left->expression.vtype;
409 void ast_binary_delete(ast_binary *self)
411 ast_unref(self->left);
412 ast_unref(self->right);
413 ast_expression_delete((ast_expression*)self);
417 ast_binstore* ast_binstore_new(lex_ctx ctx, int storop, int op,
418 ast_expression* left, ast_expression* right)
420 ast_instantiate(ast_binstore, ctx, ast_binstore_delete);
421 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binstore_codegen);
423 ast_side_effects(self) = true;
425 self->opstore = storop;
428 self->source = right;
430 self->keep_dest = false;
432 if (!ast_type_adopt(self, left)) {
440 void ast_binstore_delete(ast_binstore *self)
442 if (!self->keep_dest)
443 ast_unref(self->dest);
444 ast_unref(self->source);
445 ast_expression_delete((ast_expression*)self);
449 ast_unary* ast_unary_new(lex_ctx ctx, int op,
450 ast_expression *expr)
452 ast_instantiate(ast_unary, ctx, ast_unary_delete);
453 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_unary_codegen);
456 self->operand = expr;
458 ast_propagate_effects(self, expr);
460 if (op >= INSTR_NOT_F && op <= INSTR_NOT_FNC) {
461 self->expression.vtype = TYPE_FLOAT;
463 compile_error(ctx, "cannot determine type of unary operation %s", asm_instr[op].m);
468 void ast_unary_delete(ast_unary *self)
470 ast_unref(self->operand);
471 ast_expression_delete((ast_expression*)self);
475 ast_return* ast_return_new(lex_ctx ctx, ast_expression *expr)
477 ast_instantiate(ast_return, ctx, ast_return_delete);
478 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_return_codegen);
480 self->operand = expr;
483 ast_propagate_effects(self, expr);
488 void ast_return_delete(ast_return *self)
491 ast_unref(self->operand);
492 ast_expression_delete((ast_expression*)self);
496 ast_entfield* ast_entfield_new(lex_ctx ctx, ast_expression *entity, ast_expression *field)
498 if (field->expression.vtype != TYPE_FIELD) {
499 compile_error(ctx, "ast_entfield_new with expression not of type field");
502 return ast_entfield_new_force(ctx, entity, field, field->expression.next);
505 ast_entfield* ast_entfield_new_force(lex_ctx ctx, ast_expression *entity, ast_expression *field, const ast_expression *outtype)
507 ast_instantiate(ast_entfield, ctx, ast_entfield_delete);
511 /* Error: field has no type... */
515 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_entfield_codegen);
517 self->entity = entity;
519 ast_propagate_effects(self, entity);
520 ast_propagate_effects(self, field);
522 if (!ast_type_adopt(self, outtype)) {
523 ast_entfield_delete(self);
530 void ast_entfield_delete(ast_entfield *self)
532 ast_unref(self->entity);
533 ast_unref(self->field);
534 ast_expression_delete((ast_expression*)self);
538 ast_member* ast_member_new(lex_ctx ctx, ast_expression *owner, unsigned int field, const char *name)
540 ast_instantiate(ast_member, ctx, ast_member_delete);
546 if (owner->expression.vtype != TYPE_VECTOR &&
547 owner->expression.vtype != TYPE_FIELD) {
548 compile_error(ctx, "member-access on an invalid owner of type %s", type_name[owner->expression.vtype]);
553 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_member_codegen);
554 self->expression.node.keep = true; /* keep */
556 if (owner->expression.vtype == TYPE_VECTOR) {
557 self->expression.vtype = TYPE_FLOAT;
558 self->expression.next = NULL;
560 self->expression.vtype = TYPE_FIELD;
561 self->expression.next = ast_shallow_type(ctx, TYPE_FLOAT);
565 ast_propagate_effects(self, owner);
569 self->name = util_strdup(name);
576 void ast_member_delete(ast_member *self)
578 /* The owner is always an ast_value, which has .keep=true,
579 * also: ast_members are usually deleted after the owner, thus
580 * this will cause invalid access
581 ast_unref(self->owner);
582 * once we allow (expression).x to access a vector-member, we need
583 * to change this: preferably by creating an alternate ast node for this
584 * purpose that is not garbage-collected.
586 ast_expression_delete((ast_expression*)self);
590 ast_array_index* ast_array_index_new(lex_ctx ctx, ast_expression *array, ast_expression *index)
592 ast_expression *outtype;
593 ast_instantiate(ast_array_index, ctx, ast_array_index_delete);
595 outtype = array->expression.next;
598 /* Error: field has no type... */
602 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_array_index_codegen);
606 ast_propagate_effects(self, array);
607 ast_propagate_effects(self, index);
609 if (!ast_type_adopt(self, outtype)) {
610 ast_array_index_delete(self);
613 if (array->expression.vtype == TYPE_FIELD && outtype->expression.vtype == TYPE_ARRAY) {
614 if (self->expression.vtype != TYPE_ARRAY) {
615 compile_error(ast_ctx(self), "array_index node on type");
616 ast_array_index_delete(self);
619 self->array = outtype;
620 self->expression.vtype = TYPE_FIELD;
626 void ast_array_index_delete(ast_array_index *self)
628 ast_unref(self->array);
629 ast_unref(self->index);
630 ast_expression_delete((ast_expression*)self);
634 ast_ifthen* ast_ifthen_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
636 ast_instantiate(ast_ifthen, ctx, ast_ifthen_delete);
637 if (!ontrue && !onfalse) {
638 /* because it is invalid */
642 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ifthen_codegen);
645 self->on_true = ontrue;
646 self->on_false = onfalse;
647 ast_propagate_effects(self, cond);
649 ast_propagate_effects(self, ontrue);
651 ast_propagate_effects(self, onfalse);
656 void ast_ifthen_delete(ast_ifthen *self)
658 ast_unref(self->cond);
660 ast_unref(self->on_true);
662 ast_unref(self->on_false);
663 ast_expression_delete((ast_expression*)self);
667 ast_ternary* ast_ternary_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
669 ast_instantiate(ast_ternary, ctx, ast_ternary_delete);
670 /* This time NEITHER must be NULL */
671 if (!ontrue || !onfalse) {
675 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ternary_codegen);
678 self->on_true = ontrue;
679 self->on_false = onfalse;
680 ast_propagate_effects(self, cond);
681 ast_propagate_effects(self, ontrue);
682 ast_propagate_effects(self, onfalse);
684 if (!ast_type_adopt(self, ontrue)) {
685 ast_ternary_delete(self);
692 void ast_ternary_delete(ast_ternary *self)
694 ast_unref(self->cond);
695 ast_unref(self->on_true);
696 ast_unref(self->on_false);
697 ast_expression_delete((ast_expression*)self);
701 ast_loop* ast_loop_new(lex_ctx ctx,
702 ast_expression *initexpr,
703 ast_expression *precond,
704 ast_expression *postcond,
705 ast_expression *increment,
706 ast_expression *body)
708 ast_instantiate(ast_loop, ctx, ast_loop_delete);
709 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_loop_codegen);
711 self->initexpr = initexpr;
712 self->precond = precond;
713 self->postcond = postcond;
714 self->increment = increment;
718 ast_propagate_effects(self, initexpr);
720 ast_propagate_effects(self, precond);
722 ast_propagate_effects(self, postcond);
724 ast_propagate_effects(self, increment);
726 ast_propagate_effects(self, body);
731 void ast_loop_delete(ast_loop *self)
734 ast_unref(self->initexpr);
736 ast_unref(self->precond);
738 ast_unref(self->postcond);
740 ast_unref(self->increment);
742 ast_unref(self->body);
743 ast_expression_delete((ast_expression*)self);
747 ast_breakcont* ast_breakcont_new(lex_ctx ctx, bool iscont)
749 ast_instantiate(ast_breakcont, ctx, ast_breakcont_delete);
750 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_breakcont_codegen);
752 self->is_continue = iscont;
757 void ast_breakcont_delete(ast_breakcont *self)
759 ast_expression_delete((ast_expression*)self);
763 ast_switch* ast_switch_new(lex_ctx ctx, ast_expression *op)
765 ast_instantiate(ast_switch, ctx, ast_switch_delete);
766 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_switch_codegen);
771 ast_propagate_effects(self, op);
776 void ast_switch_delete(ast_switch *self)
779 ast_unref(self->operand);
781 for (i = 0; i < vec_size(self->cases); ++i) {
782 if (self->cases[i].value)
783 ast_unref(self->cases[i].value);
784 ast_unref(self->cases[i].code);
786 vec_free(self->cases);
788 ast_expression_delete((ast_expression*)self);
792 ast_label* ast_label_new(lex_ctx ctx, const char *name)
794 ast_instantiate(ast_label, ctx, ast_label_delete);
795 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_label_codegen);
797 self->name = util_strdup(name);
798 self->irblock = NULL;
804 void ast_label_delete(ast_label *self)
806 mem_d((void*)self->name);
807 vec_free(self->gotos);
808 ast_expression_delete((ast_expression*)self);
812 void ast_label_register_goto(ast_label *self, ast_goto *g)
814 vec_push(self->gotos, g);
817 ast_goto* ast_goto_new(lex_ctx ctx, const char *name)
819 ast_instantiate(ast_goto, ctx, ast_goto_delete);
820 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_goto_codegen);
822 self->name = util_strdup(name);
824 self->irblock_from = NULL;
829 void ast_goto_delete(ast_goto *self)
831 mem_d((void*)self->name);
832 ast_expression_delete((ast_expression*)self);
836 void ast_goto_set_label(ast_goto *self, ast_label *label)
838 self->target = label;
841 ast_call* ast_call_new(lex_ctx ctx,
842 ast_expression *funcexpr)
844 ast_instantiate(ast_call, ctx, ast_call_delete);
845 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_call_codegen);
847 ast_side_effects(self) = true;
850 self->func = funcexpr;
852 ast_type_adopt(self, funcexpr->expression.next);
857 void ast_call_delete(ast_call *self)
860 for (i = 0; i < vec_size(self->params); ++i)
861 ast_unref(self->params[i]);
862 vec_free(self->params);
865 ast_unref(self->func);
867 ast_expression_delete((ast_expression*)self);
871 bool ast_call_check_types(ast_call *self)
875 const ast_expression *func = self->func;
876 size_t count = vec_size(self->params);
877 if (count > vec_size(func->expression.params))
878 count = vec_size(func->expression.params);
880 for (i = 0; i < count; ++i) {
881 if (!ast_compare_type(self->params[i], (ast_expression*)(func->expression.params[i]))) {
884 ast_type_to_string(self->params[i], tgot, sizeof(tgot));
885 ast_type_to_string((ast_expression*)func->expression.params[i], texp, sizeof(texp));
886 compile_error(ast_ctx(self), "invalid type for parameter %u in function call: expected %s, got %s",
887 (unsigned int)(i+1), texp, tgot);
888 /* we don't immediately return */
895 ast_store* ast_store_new(lex_ctx ctx, int op,
896 ast_expression *dest, ast_expression *source)
898 ast_instantiate(ast_store, ctx, ast_store_delete);
899 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_store_codegen);
901 ast_side_effects(self) = true;
905 self->source = source;
907 if (!ast_type_adopt(self, dest)) {
915 void ast_store_delete(ast_store *self)
917 ast_unref(self->dest);
918 ast_unref(self->source);
919 ast_expression_delete((ast_expression*)self);
923 ast_block* ast_block_new(lex_ctx ctx)
925 ast_instantiate(ast_block, ctx, ast_block_delete);
926 ast_expression_init((ast_expression*)self,
927 (ast_expression_codegen*)&ast_block_codegen);
931 self->collect = NULL;
936 bool ast_block_add_expr(ast_block *self, ast_expression *e)
938 ast_propagate_effects(self, e);
939 vec_push(self->exprs, e);
940 if (self->expression.next) {
941 ast_delete(self->expression.next);
942 self->expression.next = NULL;
944 if (!ast_type_adopt(self, e)) {
945 compile_error(ast_ctx(self), "internal error: failed to adopt type");
951 void ast_block_collect(ast_block *self, ast_expression *expr)
953 vec_push(self->collect, expr);
954 expr->expression.node.keep = true;
957 void ast_block_delete(ast_block *self)
960 for (i = 0; i < vec_size(self->exprs); ++i)
961 ast_unref(self->exprs[i]);
962 vec_free(self->exprs);
963 for (i = 0; i < vec_size(self->locals); ++i)
964 ast_delete(self->locals[i]);
965 vec_free(self->locals);
966 for (i = 0; i < vec_size(self->collect); ++i)
967 ast_delete(self->collect[i]);
968 vec_free(self->collect);
969 ast_expression_delete((ast_expression*)self);
973 bool ast_block_set_type(ast_block *self, ast_expression *from)
975 if (self->expression.next)
976 ast_delete(self->expression.next);
977 if (!ast_type_adopt(self, from))
982 ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype)
984 ast_instantiate(ast_function, ctx, ast_function_delete);
988 vtype->expression.vtype != TYPE_FUNCTION)
990 compile_error(ast_ctx(self), "internal error: ast_function_new condition %i %i type=%i (probably 2 bodies?)",
992 (int)vtype->hasvalue,
993 vtype->expression.vtype);
999 self->name = name ? util_strdup(name) : NULL;
1000 self->blocks = NULL;
1002 self->labelcount = 0;
1005 self->ir_func = NULL;
1006 self->curblock = NULL;
1008 self->breakblock = NULL;
1009 self->continueblock = NULL;
1011 vtype->hasvalue = true;
1012 vtype->constval.vfunc = self;
1017 void ast_function_delete(ast_function *self)
1021 mem_d((void*)self->name);
1023 /* ast_value_delete(self->vtype); */
1024 self->vtype->hasvalue = false;
1025 self->vtype->constval.vfunc = NULL;
1026 /* We use unref - if it was stored in a global table it is supposed
1027 * to be deleted from *there*
1029 ast_unref(self->vtype);
1031 for (i = 0; i < vec_size(self->blocks); ++i)
1032 ast_delete(self->blocks[i]);
1033 vec_free(self->blocks);
1037 const char* ast_function_label(ast_function *self, const char *prefix)
1043 if (!opts_dump && !opts_dumpfin)
1046 id = (self->labelcount++);
1047 len = strlen(prefix);
1049 from = self->labelbuf + sizeof(self->labelbuf)-1;
1052 unsigned int digit = id % 10;
1053 *from = digit + '0';
1056 memcpy(from - len, prefix, len);
1060 /*********************************************************************/
1062 * by convention you must never pass NULL to the 'ir_value **out'
1063 * parameter. If you really don't care about the output, pass a dummy.
1064 * But I can't imagine a pituation where the output is truly unnecessary.
1067 void _ast_codegen_output_type(ast_expression_common *self, ir_value *out)
1069 if (out->vtype == TYPE_FIELD)
1070 out->fieldtype = self->next->expression.vtype;
1071 if (out->vtype == TYPE_FUNCTION)
1072 out->outtype = self->next->expression.vtype;
1075 #define codegen_output_type(a,o) (_ast_codegen_output_type(&((a)->expression),(o)))
1076 #define codegen_output_type_expr(a,o) (_ast_codegen_output_type(a,(o)))
1078 bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out)
1082 /* NOTE: This is the codegen for a variable used in an expression.
1083 * It is not the codegen to generate the value. For this purpose,
1084 * ast_local_codegen and ast_global_codegen are to be used before this
1085 * is executed. ast_function_codegen should take care of its locals,
1086 * and the ast-user should take care of ast_global_codegen to be used
1087 * on all the globals.
1090 char typename[1024];
1091 ast_type_to_string((ast_expression*)self, typename, sizeof(typename));
1092 compile_error(ast_ctx(self), "ast_value used before generated %s %s", typename, self->name);
1099 bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
1103 if (self->hasvalue && self->expression.vtype == TYPE_FUNCTION)
1105 ir_function *func = ir_builder_create_function(ir, self->name, self->expression.next->expression.vtype);
1108 func->context = ast_ctx(self);
1109 func->value->context = ast_ctx(self);
1111 self->constval.vfunc->ir_func = func;
1112 self->ir_v = func->value;
1113 /* The function is filled later on ast_function_codegen... */
1117 if (isfield && self->expression.vtype == TYPE_FIELD) {
1118 ast_expression *fieldtype = self->expression.next;
1120 if (self->hasvalue) {
1121 compile_error(ast_ctx(self), "TODO: constant field pointers with value");
1125 if (fieldtype->expression.vtype == TYPE_ARRAY) {
1130 ast_expression_common *elemtype;
1132 ast_value *array = (ast_value*)fieldtype;
1134 if (!ast_istype(fieldtype, ast_value)) {
1135 compile_error(ast_ctx(self), "internal error: ast_value required");
1139 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1140 if (!array->expression.count || array->expression.count > opts_max_array_size)
1141 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)array->expression.count);
1143 elemtype = &array->expression.next->expression;
1144 vtype = elemtype->vtype;
1146 v = ir_builder_create_field(ir, self->name, vtype);
1148 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", self->name);
1151 v->context = ast_ctx(self);
1152 array->ir_v = self->ir_v = v;
1154 namelen = strlen(self->name);
1155 name = (char*)mem_a(namelen + 16);
1156 strcpy(name, self->name);
1158 array->ir_values = (ir_value**)mem_a(sizeof(array->ir_values[0]) * array->expression.count);
1159 array->ir_values[0] = v;
1160 for (ai = 1; ai < array->expression.count; ++ai) {
1161 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1162 array->ir_values[ai] = ir_builder_create_field(ir, name, vtype);
1163 if (!array->ir_values[ai]) {
1165 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", name);
1168 array->ir_values[ai]->context = ast_ctx(self);
1174 v = ir_builder_create_field(ir, self->name, self->expression.next->expression.vtype);
1177 v->context = ast_ctx(self);
1183 if (self->expression.vtype == TYPE_ARRAY) {
1188 ast_expression_common *elemtype = &self->expression.next->expression;
1189 int vtype = elemtype->vtype;
1191 /* same as with field arrays */
1192 if (!self->expression.count || self->expression.count > opts_max_array_size)
1193 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1195 v = ir_builder_create_global(ir, self->name, vtype);
1197 compile_error(ast_ctx(self), "ir_builder_create_global failed `%s`", self->name);
1200 v->context = ast_ctx(self);
1202 namelen = strlen(self->name);
1203 name = (char*)mem_a(namelen + 16);
1204 strcpy(name, self->name);
1206 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1207 self->ir_values[0] = v;
1208 for (ai = 1; ai < self->expression.count; ++ai) {
1209 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1210 self->ir_values[ai] = ir_builder_create_global(ir, name, vtype);
1211 if (!self->ir_values[ai]) {
1213 compile_error(ast_ctx(self), "ir_builder_create_global failed `%s`", name);
1216 self->ir_values[ai]->context = ast_ctx(self);
1222 /* Arrays don't do this since there's no "array" value which spans across the
1225 v = ir_builder_create_global(ir, self->name, self->expression.vtype);
1227 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", self->name);
1230 codegen_output_type(self, v);
1231 v->context = ast_ctx(self);
1234 if (self->hasvalue) {
1235 switch (self->expression.vtype)
1238 if (!ir_value_set_float(v, self->constval.vfloat))
1242 if (!ir_value_set_vector(v, self->constval.vvec))
1246 if (!ir_value_set_string(v, self->constval.vstring))
1250 compile_error(ast_ctx(self), "TODO: global constant array");
1253 compile_error(ast_ctx(self), "global of type function not properly generated");
1255 /* Cannot generate an IR value for a function,
1256 * need a pointer pointing to a function rather.
1259 if (!self->constval.vfield) {
1260 compile_error(ast_ctx(self), "field constant without vfield set");
1263 if (!self->constval.vfield->ir_v) {
1264 compile_error(ast_ctx(self), "field constant generated before its field");
1267 if (!ir_value_set_field(v, self->constval.vfield->ir_v))
1271 compile_error(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1276 /* link us to the ir_value */
1281 error: /* clean up */
1286 bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
1289 if (self->hasvalue && self->expression.vtype == TYPE_FUNCTION)
1291 /* Do we allow local functions? I think not...
1292 * this is NOT a function pointer atm.
1297 if (self->expression.vtype == TYPE_ARRAY) {
1302 ast_expression_common *elemtype = &self->expression.next->expression;
1303 int vtype = elemtype->vtype;
1306 compile_error(ast_ctx(self), "array-parameters are not supported");
1310 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1311 if (!self->expression.count || self->expression.count > opts_max_array_size) {
1312 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1315 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1316 if (!self->ir_values) {
1317 compile_error(ast_ctx(self), "failed to allocate array values");
1321 v = ir_function_create_local(func, self->name, vtype, param);
1323 compile_error(ast_ctx(self), "ir_function_create_local failed");
1326 v->context = ast_ctx(self);
1328 namelen = strlen(self->name);
1329 name = (char*)mem_a(namelen + 16);
1330 strcpy(name, self->name);
1332 self->ir_values[0] = v;
1333 for (ai = 1; ai < self->expression.count; ++ai) {
1334 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1335 self->ir_values[ai] = ir_function_create_local(func, name, vtype, param);
1336 if (!self->ir_values[ai]) {
1337 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", name);
1340 self->ir_values[ai]->context = ast_ctx(self);
1345 v = ir_function_create_local(func, self->name, self->expression.vtype, param);
1348 codegen_output_type(self, v);
1349 v->context = ast_ctx(self);
1352 /* A constant local... hmmm...
1353 * I suppose the IR will have to deal with this
1355 if (self->hasvalue) {
1356 switch (self->expression.vtype)
1359 if (!ir_value_set_float(v, self->constval.vfloat))
1363 if (!ir_value_set_vector(v, self->constval.vvec))
1367 if (!ir_value_set_string(v, self->constval.vstring))
1371 compile_error(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1376 /* link us to the ir_value */
1380 if (!ast_generate_accessors(self, func->owner))
1384 error: /* clean up */
1389 bool ast_generate_accessors(ast_value *self, ir_builder *ir)
1392 bool warn = OPTS_WARN(WARN_USED_UNINITIALIZED);
1393 if (!self->setter || !self->getter)
1395 for (i = 0; i < self->expression.count; ++i) {
1396 if (!self->ir_values) {
1397 compile_error(ast_ctx(self), "internal error: no array values generated for `%s`", self->name);
1400 if (!self->ir_values[i]) {
1401 compile_error(ast_ctx(self), "internal error: not all array values have been generated for `%s`", self->name);
1404 if (self->ir_values[i]->life) {
1405 compile_error(ast_ctx(self), "internal error: function containing `%s` already generated", self->name);
1410 options_set(opts_warn, WARN_USED_UNINITIALIZED, false);
1412 if (!ast_global_codegen (self->setter, ir, false) ||
1413 !ast_function_codegen(self->setter->constval.vfunc, ir) ||
1414 !ir_function_finalize(self->setter->constval.vfunc->ir_func))
1416 compile_error(ast_ctx(self), "internal error: failed to generate setter for `%s`", self->name);
1417 options_set(opts_warn, WARN_USED_UNINITIALIZED, warn);
1422 if (!ast_global_codegen (self->getter, ir, false) ||
1423 !ast_function_codegen(self->getter->constval.vfunc, ir) ||
1424 !ir_function_finalize(self->getter->constval.vfunc->ir_func))
1426 compile_error(ast_ctx(self), "internal error: failed to generate getter for `%s`", self->name);
1427 options_set(opts_warn, WARN_USED_UNINITIALIZED, warn);
1431 for (i = 0; i < self->expression.count; ++i) {
1432 vec_free(self->ir_values[i]->life);
1434 options_set(opts_warn, WARN_USED_UNINITIALIZED, warn);
1438 bool ast_function_codegen(ast_function *self, ir_builder *ir)
1442 ast_expression_common *ec;
1447 irf = self->ir_func;
1449 compile_error(ast_ctx(self), "ast_function's related ast_value was not generated yet");
1453 /* fill the parameter list */
1454 ec = &self->vtype->expression;
1455 for (i = 0; i < vec_size(ec->params); ++i)
1457 if (ec->params[i]->expression.vtype == TYPE_FIELD)
1458 vec_push(irf->params, ec->params[i]->expression.next->expression.vtype);
1460 vec_push(irf->params, ec->params[i]->expression.vtype);
1461 if (!self->builtin) {
1462 if (!ast_local_codegen(ec->params[i], self->ir_func, true))
1467 if (self->builtin) {
1468 irf->builtin = self->builtin;
1472 if (!vec_size(self->blocks)) {
1473 compile_error(ast_ctx(self), "function `%s` has no body", self->name);
1477 self->curblock = ir_function_create_block(ast_ctx(self), irf, "entry");
1478 if (!self->curblock) {
1479 compile_error(ast_ctx(self), "failed to allocate entry block for `%s`", self->name);
1483 for (i = 0; i < vec_size(self->blocks); ++i) {
1484 ast_expression_codegen *gen = self->blocks[i]->expression.codegen;
1485 if (!(*gen)((ast_expression*)self->blocks[i], self, false, &dummy))
1489 /* TODO: check return types */
1490 if (!self->curblock->is_return)
1492 if (!self->vtype->expression.next ||
1493 self->vtype->expression.next->expression.vtype == TYPE_VOID)
1495 return ir_block_create_return(self->curblock, ast_ctx(self), NULL);
1497 else if (vec_size(self->curblock->entries))
1499 /* error("missing return"); */
1500 if (compile_warning(ast_ctx(self), WARN_MISSING_RETURN_VALUES,
1501 "control reaches end of non-void function (`%s`) via %s",
1502 self->name, self->curblock->label))
1506 return ir_block_create_return(self->curblock, ast_ctx(self), NULL);
1512 /* Note, you will not see ast_block_codegen generate ir_blocks.
1513 * To the AST and the IR, blocks are 2 different things.
1514 * In the AST it represents a block of code, usually enclosed in
1515 * curly braces {...}.
1516 * While in the IR it represents a block in terms of control-flow.
1518 bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_value **out)
1522 /* We don't use this
1523 * Note: an ast-representation using the comma-operator
1524 * of the form: (a, b, c) = x should not assign to c...
1527 compile_error(ast_ctx(self), "not an l-value (code-block)");
1531 if (self->expression.outr) {
1532 *out = self->expression.outr;
1536 /* output is NULL at first, we'll have each expression
1537 * assign to out output, thus, a comma-operator represention
1538 * using an ast_block will return the last generated value,
1539 * so: (b, c) + a executed both b and c, and returns c,
1540 * which is then added to a.
1544 /* generate locals */
1545 for (i = 0; i < vec_size(self->locals); ++i)
1547 if (!ast_local_codegen(self->locals[i], func->ir_func, false)) {
1549 compile_error(ast_ctx(self), "failed to generate local `%s`", self->locals[i]->name);
1554 for (i = 0; i < vec_size(self->exprs); ++i)
1556 ast_expression_codegen *gen = self->exprs[i]->expression.codegen;
1557 if (func->curblock->final && !ast_istype(self->exprs[i], ast_label)) {
1558 compile_error(ast_ctx(self->exprs[i]), "unreachable statement");
1561 if (!(*gen)(self->exprs[i], func, false, out))
1565 self->expression.outr = *out;
1570 bool ast_store_codegen(ast_store *self, ast_function *func, bool lvalue, ir_value **out)
1572 ast_expression_codegen *cgen;
1573 ir_value *left = NULL;
1574 ir_value *right = NULL;
1578 ast_array_index *ai = NULL;
1580 if (lvalue && self->expression.outl) {
1581 *out = self->expression.outl;
1585 if (!lvalue && self->expression.outr) {
1586 *out = self->expression.outr;
1590 if (ast_istype(self->dest, ast_array_index))
1593 ai = (ast_array_index*)self->dest;
1594 idx = (ast_value*)ai->index;
1596 if (ast_istype(ai->index, ast_value) && idx->hasvalue && idx->cvq == CV_CONST)
1601 /* we need to call the setter */
1602 ir_value *iridx, *funval;
1606 compile_error(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1610 arr = (ast_value*)ai->array;
1611 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1612 compile_error(ast_ctx(self), "value has no setter (%s)", arr->name);
1616 cgen = idx->expression.codegen;
1617 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1620 cgen = arr->setter->expression.codegen;
1621 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
1624 cgen = self->source->expression.codegen;
1625 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1628 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "store"), funval);
1631 ir_call_param(call, iridx);
1632 ir_call_param(call, right);
1633 self->expression.outr = right;
1639 cgen = self->dest->expression.codegen;
1641 if (!(*cgen)((ast_expression*)(self->dest), func, true, &left))
1643 self->expression.outl = left;
1645 cgen = self->source->expression.codegen;
1647 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1650 if (!ir_block_create_store_op(func->curblock, ast_ctx(self), self->op, left, right))
1652 self->expression.outr = right;
1655 /* Theoretically, an assinment returns its left side as an
1656 * lvalue, if we don't need an lvalue though, we return
1657 * the right side as an rvalue, otherwise we have to
1658 * somehow know whether or not we need to dereference the pointer
1659 * on the left side - that is: OP_LOAD if it was an address.
1660 * Also: in original QC we cannot OP_LOADP *anyway*.
1662 *out = (lvalue ? left : right);
1667 bool ast_binary_codegen(ast_binary *self, ast_function *func, bool lvalue, ir_value **out)
1669 ast_expression_codegen *cgen;
1670 ir_value *left, *right;
1672 /* A binary operation cannot yield an l-value */
1674 compile_error(ast_ctx(self), "not an l-value (binop)");
1678 if (self->expression.outr) {
1679 *out = self->expression.outr;
1683 if (OPTS_FLAG(SHORT_LOGIC) &&
1684 (self->op == INSTR_AND || self->op == INSTR_OR))
1686 /* short circuit evaluation */
1687 ir_block *other, *merge;
1688 ir_block *from_left, *from_right;
1693 /* Note about casting to true boolean values:
1694 * We use a single NOT for sub expressions, and an
1695 * overall NOT at the end, and for that purpose swap
1696 * all the jump conditions in order for the NOT to get
1698 * ie: (a && b) usually becomes (!!a ? !!b : !!a)
1699 * but we translate this to (!(!a ? !a : !b))
1702 merge_id = vec_size(func->ir_func->blocks);
1703 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "sce_merge"));
1705 cgen = self->left->expression.codegen;
1706 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1708 if (!OPTS_FLAG(PERL_LOGIC)) {
1709 notop = type_not_instr[left->vtype];
1710 if (notop == AINSTR_END) {
1711 compile_error(ast_ctx(self), "don't know how to cast to bool...");
1714 left = ir_block_create_unary(func->curblock, ast_ctx(self),
1715 ast_function_label(func, "sce_not"),
1719 from_left = func->curblock;
1721 other = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "sce_other"));
1722 if ( !(self->op == INSTR_OR) != !OPTS_FLAG(PERL_LOGIC) ) {
1723 if (!ir_block_create_if(func->curblock, ast_ctx(self), left, other, merge))
1726 if (!ir_block_create_if(func->curblock, ast_ctx(self), left, merge, other))
1729 /* use the likely flag */
1730 vec_last(func->curblock->instr)->likely = true;
1732 func->curblock = other;
1733 cgen = self->right->expression.codegen;
1734 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1736 if (!OPTS_FLAG(PERL_LOGIC)) {
1737 notop = type_not_instr[right->vtype];
1738 if (notop == AINSTR_END) {
1739 compile_error(ast_ctx(self), "don't know how to cast to bool...");
1742 right = ir_block_create_unary(func->curblock, ast_ctx(self),
1743 ast_function_label(func, "sce_not"),
1747 from_right = func->curblock;
1749 if (!ir_block_create_jump(func->curblock, ast_ctx(self), merge))
1752 vec_remove(func->ir_func->blocks, merge_id, 1);
1753 vec_push(func->ir_func->blocks, merge);
1755 func->curblock = merge;
1756 phi = ir_block_create_phi(func->curblock, ast_ctx(self), ast_function_label(func, "sce_value"), TYPE_FLOAT);
1757 ir_phi_add(phi, from_left, left);
1758 ir_phi_add(phi, from_right, right);
1759 *out = ir_phi_value(phi);
1760 if (!OPTS_FLAG(PERL_LOGIC)) {
1761 notop = type_not_instr[(*out)->vtype];
1762 if (notop == AINSTR_END) {
1763 compile_error(ast_ctx(self), "don't know how to cast to bool...");
1766 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
1767 ast_function_label(func, "sce_final_not"),
1773 self->expression.outr = *out;
1777 cgen = self->left->expression.codegen;
1778 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1781 cgen = self->right->expression.codegen;
1782 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1785 *out = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "bin"),
1786 self->op, left, right);
1789 self->expression.outr = *out;
1794 bool ast_binstore_codegen(ast_binstore *self, ast_function *func, bool lvalue, ir_value **out)
1796 ast_expression_codegen *cgen;
1797 ir_value *leftl = NULL, *leftr, *right, *bin;
1801 ast_array_index *ai = NULL;
1802 ir_value *iridx = NULL;
1804 if (lvalue && self->expression.outl) {
1805 *out = self->expression.outl;
1809 if (!lvalue && self->expression.outr) {
1810 *out = self->expression.outr;
1814 if (ast_istype(self->dest, ast_array_index))
1817 ai = (ast_array_index*)self->dest;
1818 idx = (ast_value*)ai->index;
1820 if (ast_istype(ai->index, ast_value) && idx->hasvalue && idx->cvq == CV_CONST)
1824 /* for a binstore we need both an lvalue and an rvalue for the left side */
1825 /* rvalue of destination! */
1827 cgen = idx->expression.codegen;
1828 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1831 cgen = self->dest->expression.codegen;
1832 if (!(*cgen)((ast_expression*)(self->dest), func, false, &leftr))
1835 /* source as rvalue only */
1836 cgen = self->source->expression.codegen;
1837 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1840 /* now the binary */
1841 bin = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "binst"),
1842 self->opbin, leftr, right);
1843 self->expression.outr = bin;
1847 /* we need to call the setter */
1852 compile_error(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1856 arr = (ast_value*)ai->array;
1857 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1858 compile_error(ast_ctx(self), "value has no setter (%s)", arr->name);
1862 cgen = arr->setter->expression.codegen;
1863 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
1866 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "store"), funval);
1869 ir_call_param(call, iridx);
1870 ir_call_param(call, bin);
1871 self->expression.outr = bin;
1873 /* now store them */
1874 cgen = self->dest->expression.codegen;
1875 /* lvalue of destination */
1876 if (!(*cgen)((ast_expression*)(self->dest), func, true, &leftl))
1878 self->expression.outl = leftl;
1880 if (!ir_block_create_store_op(func->curblock, ast_ctx(self), self->opstore, leftl, bin))
1882 self->expression.outr = bin;
1885 /* Theoretically, an assinment returns its left side as an
1886 * lvalue, if we don't need an lvalue though, we return
1887 * the right side as an rvalue, otherwise we have to
1888 * somehow know whether or not we need to dereference the pointer
1889 * on the left side - that is: OP_LOAD if it was an address.
1890 * Also: in original QC we cannot OP_LOADP *anyway*.
1892 *out = (lvalue ? leftl : bin);
1897 bool ast_unary_codegen(ast_unary *self, ast_function *func, bool lvalue, ir_value **out)
1899 ast_expression_codegen *cgen;
1902 /* An unary operation cannot yield an l-value */
1904 compile_error(ast_ctx(self), "not an l-value (binop)");
1908 if (self->expression.outr) {
1909 *out = self->expression.outr;
1913 cgen = self->operand->expression.codegen;
1915 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
1918 *out = ir_block_create_unary(func->curblock, ast_ctx(self), ast_function_label(func, "unary"),
1922 self->expression.outr = *out;
1927 bool ast_return_codegen(ast_return *self, ast_function *func, bool lvalue, ir_value **out)
1929 ast_expression_codegen *cgen;
1934 /* In the context of a return operation, we don't actually return
1938 compile_error(ast_ctx(self), "return-expression is not an l-value");
1942 if (self->expression.outr) {
1943 compile_error(ast_ctx(self), "internal error: ast_return cannot be reused, it bears no result!");
1946 self->expression.outr = (ir_value*)1;
1948 if (self->operand) {
1949 cgen = self->operand->expression.codegen;
1951 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
1954 if (!ir_block_create_return(func->curblock, ast_ctx(self), operand))
1957 if (!ir_block_create_return(func->curblock, ast_ctx(self), NULL))
1964 bool ast_entfield_codegen(ast_entfield *self, ast_function *func, bool lvalue, ir_value **out)
1966 ast_expression_codegen *cgen;
1967 ir_value *ent, *field;
1969 /* This function needs to take the 'lvalue' flag into account!
1970 * As lvalue we provide a field-pointer, as rvalue we provide the
1974 if (lvalue && self->expression.outl) {
1975 *out = self->expression.outl;
1979 if (!lvalue && self->expression.outr) {
1980 *out = self->expression.outr;
1984 cgen = self->entity->expression.codegen;
1985 if (!(*cgen)((ast_expression*)(self->entity), func, false, &ent))
1988 cgen = self->field->expression.codegen;
1989 if (!(*cgen)((ast_expression*)(self->field), func, false, &field))
1994 *out = ir_block_create_fieldaddress(func->curblock, ast_ctx(self), ast_function_label(func, "efa"),
1997 *out = ir_block_create_load_from_ent(func->curblock, ast_ctx(self), ast_function_label(func, "efv"),
1998 ent, field, self->expression.vtype);
1999 codegen_output_type(self, *out);
2002 compile_error(ast_ctx(self), "failed to create %s instruction (output type %s)",
2003 (lvalue ? "ADDRESS" : "FIELD"),
2004 type_name[self->expression.vtype]);
2009 self->expression.outl = *out;
2011 self->expression.outr = *out;
2013 /* Hm that should be it... */
2017 bool ast_member_codegen(ast_member *self, ast_function *func, bool lvalue, ir_value **out)
2019 ast_expression_codegen *cgen;
2022 /* in QC this is always an lvalue */
2024 if (self->expression.outl) {
2025 *out = self->expression.outl;
2029 cgen = self->owner->expression.codegen;
2030 if (!(*cgen)((ast_expression*)(self->owner), func, true, &vec))
2033 if (vec->vtype != TYPE_VECTOR &&
2034 !(vec->vtype == TYPE_FIELD && self->owner->expression.next->expression.vtype == TYPE_VECTOR))
2039 *out = ir_value_vector_member(vec, self->field);
2040 self->expression.outl = *out;
2042 return (*out != NULL);
2045 bool ast_array_index_codegen(ast_array_index *self, ast_function *func, bool lvalue, ir_value **out)
2050 if (!lvalue && self->expression.outr) {
2051 *out = self->expression.outr;
2053 if (lvalue && self->expression.outl) {
2054 *out = self->expression.outl;
2057 if (!ast_istype(self->array, ast_value)) {
2058 compile_error(ast_ctx(self), "array indexing this way is not supported");
2059 /* note this would actually be pointer indexing because the left side is
2060 * not an actual array but (hopefully) an indexable expression.
2061 * Once we get integer arithmetic, and GADDRESS/GSTORE/GLOAD instruction
2062 * support this path will be filled.
2067 arr = (ast_value*)self->array;
2068 idx = (ast_value*)self->index;
2070 if (!ast_istype(self->index, ast_value) || !idx->hasvalue || idx->cvq != CV_CONST) {
2071 /* Time to use accessor functions */
2072 ast_expression_codegen *cgen;
2073 ir_value *iridx, *funval;
2077 compile_error(ast_ctx(self), "(.2) array indexing here needs a compile-time constant");
2082 compile_error(ast_ctx(self), "value has no getter, don't know how to index it");
2086 cgen = self->index->expression.codegen;
2087 if (!(*cgen)((ast_expression*)(self->index), func, false, &iridx))
2090 cgen = arr->getter->expression.codegen;
2091 if (!(*cgen)((ast_expression*)(arr->getter), func, true, &funval))
2094 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "fetch"), funval);
2097 ir_call_param(call, iridx);
2099 *out = ir_call_value(call);
2100 self->expression.outr = *out;
2104 if (idx->expression.vtype == TYPE_FLOAT) {
2105 unsigned int arridx = idx->constval.vfloat;
2106 if (arridx >= self->array->expression.count)
2108 compile_error(ast_ctx(self), "array index out of bounds: %i", arridx);
2111 *out = arr->ir_values[arridx];
2113 else if (idx->expression.vtype == TYPE_INTEGER) {
2114 unsigned int arridx = idx->constval.vint;
2115 if (arridx >= self->array->expression.count)
2117 compile_error(ast_ctx(self), "array index out of bounds: %i", arridx);
2120 *out = arr->ir_values[arridx];
2123 compile_error(ast_ctx(self), "array indexing here needs an integer constant");
2129 bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_value **out)
2131 ast_expression_codegen *cgen;
2136 ir_block *cond = func->curblock;
2139 ir_block *ontrue_endblock = NULL;
2140 ir_block *onfalse_endblock = NULL;
2141 ir_block *merge = NULL;
2143 /* We don't output any value, thus also don't care about r/lvalue */
2147 if (self->expression.outr) {
2148 compile_error(ast_ctx(self), "internal error: ast_ifthen cannot be reused, it bears no result!");
2151 self->expression.outr = (ir_value*)1;
2153 /* generate the condition */
2154 cgen = self->cond->expression.codegen;
2155 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2157 /* update the block which will get the jump - because short-logic or ternaries may have changed this */
2158 cond = func->curblock;
2162 if (self->on_true) {
2163 /* create on-true block */
2164 ontrue = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "ontrue"));
2168 /* enter the block */
2169 func->curblock = ontrue;
2172 cgen = self->on_true->expression.codegen;
2173 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &dummy))
2176 /* we now need to work from the current endpoint */
2177 ontrue_endblock = func->curblock;
2182 if (self->on_false) {
2183 /* create on-false block */
2184 onfalse = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "onfalse"));
2188 /* enter the block */
2189 func->curblock = onfalse;
2192 cgen = self->on_false->expression.codegen;
2193 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &dummy))
2196 /* we now need to work from the current endpoint */
2197 onfalse_endblock = func->curblock;
2201 /* Merge block were they all merge in to */
2202 if (!ontrue || !onfalse || !ontrue_endblock->final || !onfalse_endblock->final)
2204 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "endif"));
2207 /* add jumps ot the merge block */
2208 if (ontrue && !ontrue_endblock->final && !ir_block_create_jump(ontrue_endblock, ast_ctx(self), merge))
2210 if (onfalse && !onfalse_endblock->final && !ir_block_create_jump(onfalse_endblock, ast_ctx(self), merge))
2213 /* Now enter the merge block */
2214 func->curblock = merge;
2217 /* we create the if here, that way all blocks are ordered :)
2219 if (!ir_block_create_if(cond, ast_ctx(self), condval,
2220 (ontrue ? ontrue : merge),
2221 (onfalse ? onfalse : merge)))
2229 bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_value **out)
2231 ast_expression_codegen *cgen;
2234 ir_value *trueval, *falseval;
2237 ir_block *cond = func->curblock;
2238 ir_block *cond_out = NULL;
2239 ir_block *ontrue, *ontrue_out = NULL;
2240 ir_block *onfalse, *onfalse_out = NULL;
2243 /* Ternary can never create an lvalue... */
2247 /* In theory it shouldn't be possible to pass through a node twice, but
2248 * in case we add any kind of optimization pass for the AST itself, it
2249 * may still happen, thus we remember a created ir_value and simply return one
2250 * if it already exists.
2252 if (self->expression.outr) {
2253 *out = self->expression.outr;
2257 /* In the following, contraty to ast_ifthen, we assume both paths exist. */
2259 /* generate the condition */
2260 func->curblock = cond;
2261 cgen = self->cond->expression.codegen;
2262 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2264 cond_out = func->curblock;
2266 /* create on-true block */
2267 ontrue = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_T"));
2272 /* enter the block */
2273 func->curblock = ontrue;
2276 cgen = self->on_true->expression.codegen;
2277 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &trueval))
2280 ontrue_out = func->curblock;
2283 /* create on-false block */
2284 onfalse = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_F"));
2289 /* enter the block */
2290 func->curblock = onfalse;
2293 cgen = self->on_false->expression.codegen;
2294 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &falseval))
2297 onfalse_out = func->curblock;
2300 /* create merge block */
2301 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_out"));
2304 /* jump to merge block */
2305 if (!ir_block_create_jump(ontrue_out, ast_ctx(self), merge))
2307 if (!ir_block_create_jump(onfalse_out, ast_ctx(self), merge))
2310 /* create if instruction */
2311 if (!ir_block_create_if(cond_out, ast_ctx(self), condval, ontrue, onfalse))
2314 /* Now enter the merge block */
2315 func->curblock = merge;
2317 /* Here, now, we need a PHI node
2318 * but first some sanity checking...
2320 if (trueval->vtype != falseval->vtype) {
2321 /* error("ternary with different types on the two sides"); */
2326 phi = ir_block_create_phi(merge, ast_ctx(self), ast_function_label(func, "phi"), trueval->vtype);
2329 ir_phi_add(phi, ontrue_out, trueval);
2330 ir_phi_add(phi, onfalse_out, falseval);
2332 self->expression.outr = ir_phi_value(phi);
2333 *out = self->expression.outr;
2335 codegen_output_type(self, *out);
2340 bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value **out)
2342 ast_expression_codegen *cgen;
2344 ir_value *dummy = NULL;
2345 ir_value *precond = NULL;
2346 ir_value *postcond = NULL;
2348 /* Since we insert some jumps "late" so we have blocks
2349 * ordered "nicely", we need to keep track of the actual end-blocks
2350 * of expressions to add the jumps to.
2352 ir_block *bbody = NULL, *end_bbody = NULL;
2353 ir_block *bprecond = NULL, *end_bprecond = NULL;
2354 ir_block *bpostcond = NULL, *end_bpostcond = NULL;
2355 ir_block *bincrement = NULL, *end_bincrement = NULL;
2356 ir_block *bout = NULL, *bin = NULL;
2358 /* let's at least move the outgoing block to the end */
2361 /* 'break' and 'continue' need to be able to find the right blocks */
2362 ir_block *bcontinue = NULL;
2363 ir_block *bbreak = NULL;
2365 ir_block *old_bcontinue = NULL;
2366 ir_block *old_bbreak = NULL;
2368 ir_block *tmpblock = NULL;
2373 if (self->expression.outr) {
2374 compile_error(ast_ctx(self), "internal error: ast_loop cannot be reused, it bears no result!");
2377 self->expression.outr = (ir_value*)1;
2380 * Should we ever need some kind of block ordering, better make this function
2381 * move blocks around than write a block ordering algorithm later... after all
2382 * the ast and ir should work together, not against each other.
2385 /* initexpr doesn't get its own block, it's pointless, it could create more blocks
2386 * anyway if for example it contains a ternary.
2390 cgen = self->initexpr->expression.codegen;
2391 if (!(*cgen)((ast_expression*)(self->initexpr), func, false, &dummy))
2395 /* Store the block from which we enter this chaos */
2396 bin = func->curblock;
2398 /* The pre-loop condition needs its own block since we
2399 * need to be able to jump to the start of that expression.
2403 bprecond = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "pre_loop_cond"));
2407 /* the pre-loop-condition the least important place to 'continue' at */
2408 bcontinue = bprecond;
2411 func->curblock = bprecond;
2414 cgen = self->precond->expression.codegen;
2415 if (!(*cgen)((ast_expression*)(self->precond), func, false, &precond))
2418 end_bprecond = func->curblock;
2420 bprecond = end_bprecond = NULL;
2423 /* Now the next blocks won't be ordered nicely, but we need to
2424 * generate them this early for 'break' and 'continue'.
2426 if (self->increment) {
2427 bincrement = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "loop_increment"));
2430 bcontinue = bincrement; /* increment comes before the pre-loop-condition */
2432 bincrement = end_bincrement = NULL;
2435 if (self->postcond) {
2436 bpostcond = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "post_loop_cond"));
2439 bcontinue = bpostcond; /* postcond comes before the increment */
2441 bpostcond = end_bpostcond = NULL;
2444 bout_id = vec_size(func->ir_func->blocks);
2445 bout = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "after_loop"));
2450 /* The loop body... */
2451 /* if (self->body) */
2453 bbody = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "loop_body"));
2458 func->curblock = bbody;
2460 old_bbreak = func->breakblock;
2461 old_bcontinue = func->continueblock;
2462 func->breakblock = bbreak;
2463 func->continueblock = bcontinue;
2464 if (!func->continueblock)
2465 func->continueblock = bbody;
2469 cgen = self->body->expression.codegen;
2470 if (!(*cgen)((ast_expression*)(self->body), func, false, &dummy))
2474 end_bbody = func->curblock;
2475 func->breakblock = old_bbreak;
2476 func->continueblock = old_bcontinue;
2479 /* post-loop-condition */
2483 func->curblock = bpostcond;
2486 cgen = self->postcond->expression.codegen;
2487 if (!(*cgen)((ast_expression*)(self->postcond), func, false, &postcond))
2490 end_bpostcond = func->curblock;
2493 /* The incrementor */
2494 if (self->increment)
2497 func->curblock = bincrement;
2500 cgen = self->increment->expression.codegen;
2501 if (!(*cgen)((ast_expression*)(self->increment), func, false, &dummy))
2504 end_bincrement = func->curblock;
2507 /* In any case now, we continue from the outgoing block */
2508 func->curblock = bout;
2510 /* Now all blocks are in place */
2511 /* From 'bin' we jump to whatever comes first */
2512 if (bprecond) tmpblock = bprecond;
2513 else if (bbody) tmpblock = bbody;
2514 else if (bpostcond) tmpblock = bpostcond;
2515 else tmpblock = bout;
2516 if (!ir_block_create_jump(bin, ast_ctx(self), tmpblock))
2522 ir_block *ontrue, *onfalse;
2523 if (bbody) ontrue = bbody;
2524 else if (bincrement) ontrue = bincrement;
2525 else if (bpostcond) ontrue = bpostcond;
2526 else ontrue = bprecond;
2528 if (!ir_block_create_if(end_bprecond, ast_ctx(self), precond, ontrue, onfalse))
2535 if (bincrement) tmpblock = bincrement;
2536 else if (bpostcond) tmpblock = bpostcond;
2537 else if (bprecond) tmpblock = bprecond;
2538 else tmpblock = bbody;
2539 if (!end_bbody->final && !ir_block_create_jump(end_bbody, ast_ctx(self), tmpblock))
2543 /* from increment */
2546 if (bpostcond) tmpblock = bpostcond;
2547 else if (bprecond) tmpblock = bprecond;
2548 else if (bbody) tmpblock = bbody;
2549 else tmpblock = bout;
2550 if (!ir_block_create_jump(end_bincrement, ast_ctx(self), tmpblock))
2557 ir_block *ontrue, *onfalse;
2558 if (bprecond) ontrue = bprecond;
2559 else if (bbody) ontrue = bbody;
2560 else if (bincrement) ontrue = bincrement;
2561 else ontrue = bpostcond;
2563 if (!ir_block_create_if(end_bpostcond, ast_ctx(self), postcond, ontrue, onfalse))
2567 /* Move 'bout' to the end */
2568 vec_remove(func->ir_func->blocks, bout_id, 1);
2569 vec_push(func->ir_func->blocks, bout);
2574 bool ast_breakcont_codegen(ast_breakcont *self, ast_function *func, bool lvalue, ir_value **out)
2581 compile_error(ast_ctx(self), "break/continue expression is not an l-value");
2585 if (self->expression.outr) {
2586 compile_error(ast_ctx(self), "internal error: ast_breakcont cannot be reused!");
2589 self->expression.outr = (ir_value*)1;
2591 if (self->is_continue)
2592 target = func->continueblock;
2594 target = func->breakblock;
2597 compile_error(ast_ctx(self), "%s is lacking a target block", (self->is_continue ? "continue" : "break"));
2601 if (!ir_block_create_jump(func->curblock, ast_ctx(self), target))
2606 bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_value **out)
2608 ast_expression_codegen *cgen;
2610 ast_switch_case *def_case = NULL;
2611 ir_block *def_bfall = NULL;
2613 ir_value *dummy = NULL;
2614 ir_value *irop = NULL;
2615 ir_block *old_break = NULL;
2616 ir_block *bout = NULL;
2617 ir_block *bfall = NULL;
2625 compile_error(ast_ctx(self), "switch expression is not an l-value");
2629 if (self->expression.outr) {
2630 compile_error(ast_ctx(self), "internal error: ast_switch cannot be reused!");
2633 self->expression.outr = (ir_value*)1;
2638 cgen = self->operand->expression.codegen;
2639 if (!(*cgen)((ast_expression*)(self->operand), func, false, &irop))
2642 if (!vec_size(self->cases))
2645 cmpinstr = type_eq_instr[irop->vtype];
2646 if (cmpinstr >= AINSTR_END) {
2647 ast_type_to_string(self->operand, typestr, sizeof(typestr));
2648 compile_error(ast_ctx(self), "invalid type to perform a switch on: %s", typestr);
2652 bout_id = vec_size(func->ir_func->blocks);
2653 bout = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "after_switch"));
2657 /* setup the break block */
2658 old_break = func->breakblock;
2659 func->breakblock = bout;
2661 /* Now create all cases */
2662 for (c = 0; c < vec_size(self->cases); ++c) {
2663 ir_value *cond, *val;
2664 ir_block *bcase, *bnot;
2667 ast_switch_case *swcase = &self->cases[c];
2669 if (swcase->value) {
2670 /* A regular case */
2671 /* generate the condition operand */
2672 cgen = swcase->value->expression.codegen;
2673 if (!(*cgen)((ast_expression*)(swcase->value), func, false, &val))
2675 /* generate the condition */
2676 cond = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "switch_eq"), cmpinstr, irop, val);
2680 bcase = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "case"));
2681 bnot_id = vec_size(func->ir_func->blocks);
2682 bnot = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "not_case"));
2683 if (!bcase || !bnot)
2685 if (!ir_block_create_if(func->curblock, ast_ctx(self), cond, bcase, bnot))
2688 /* Make the previous case-end fall through */
2689 if (bfall && !bfall->final) {
2690 if (!ir_block_create_jump(bfall, ast_ctx(self), bcase))
2694 /* enter the case */
2695 func->curblock = bcase;
2696 cgen = swcase->code->expression.codegen;
2697 if (!(*cgen)((ast_expression*)swcase->code, func, false, &dummy))
2700 /* remember this block to fall through from */
2701 bfall = func->curblock;
2703 /* enter the else and move it down */
2704 func->curblock = bnot;
2705 vec_remove(func->ir_func->blocks, bnot_id, 1);
2706 vec_push(func->ir_func->blocks, bnot);
2708 /* The default case */
2709 /* Remember where to fall through from: */
2712 /* remember which case it was */
2717 /* Jump from the last bnot to bout */
2718 if (bfall && !bfall->final && !ir_block_create_jump(bfall, ast_ctx(self), bout)) {
2720 astwarning(ast_ctx(bfall), WARN_???, "missing break after last case");
2725 /* If there was a default case, put it down here */
2729 /* No need to create an extra block */
2730 bcase = func->curblock;
2732 /* Insert the fallthrough jump */
2733 if (def_bfall && !def_bfall->final) {
2734 if (!ir_block_create_jump(def_bfall, ast_ctx(self), bcase))
2738 /* Now generate the default code */
2739 cgen = def_case->code->expression.codegen;
2740 if (!(*cgen)((ast_expression*)def_case->code, func, false, &dummy))
2744 /* Jump from the last bnot to bout */
2745 if (!func->curblock->final && !ir_block_create_jump(func->curblock, ast_ctx(self), bout))
2747 /* enter the outgoing block */
2748 func->curblock = bout;
2750 /* restore the break block */
2751 func->breakblock = old_break;
2753 /* Move 'bout' to the end, it's nicer */
2754 vec_remove(func->ir_func->blocks, bout_id, 1);
2755 vec_push(func->ir_func->blocks, bout);
2760 bool ast_label_codegen(ast_label *self, ast_function *func, bool lvalue, ir_value **out)
2767 compile_error(ast_ctx(self), "internal error: ast_label cannot be an lvalue");
2771 /* simply create a new block and jump to it */
2772 self->irblock = ir_function_create_block(ast_ctx(self), func->ir_func, self->name);
2773 if (!self->irblock) {
2774 compile_error(ast_ctx(self), "failed to allocate label block `%s`", self->name);
2777 if (!func->curblock->final) {
2778 if (!ir_block_create_jump(func->curblock, ast_ctx(self), self->irblock))
2782 /* enter the new block */
2783 func->curblock = self->irblock;
2785 /* Generate all the leftover gotos */
2786 for (i = 0; i < vec_size(self->gotos); ++i) {
2787 if (!ast_goto_codegen(self->gotos[i], func, false, &dummy))
2794 bool ast_goto_codegen(ast_goto *self, ast_function *func, bool lvalue, ir_value **out)
2798 compile_error(ast_ctx(self), "internal error: ast_goto cannot be an lvalue");
2802 if (self->target->irblock) {
2803 if (self->irblock_from) {
2804 /* we already tried once, this is the callback */
2805 self->irblock_from->final = false;
2806 if (!ir_block_create_jump(self->irblock_from, ast_ctx(self), self->target->irblock)) {
2807 compile_error(ast_ctx(self), "failed to generate goto to `%s`", self->name);
2813 if (!ir_block_create_jump(func->curblock, ast_ctx(self), self->target->irblock)) {
2814 compile_error(ast_ctx(self), "failed to generate goto to `%s`", self->name);
2821 /* the target has not yet been created...
2822 * close this block in a sneaky way:
2824 func->curblock->final = true;
2825 self->irblock_from = func->curblock;
2826 ast_label_register_goto(self->target, self);
2832 bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value **out)
2834 ast_expression_codegen *cgen;
2836 ir_instr *callinstr;
2839 ir_value *funval = NULL;
2841 /* return values are never lvalues */
2843 compile_error(ast_ctx(self), "not an l-value (function call)");
2847 if (self->expression.outr) {
2848 *out = self->expression.outr;
2852 cgen = self->func->expression.codegen;
2853 if (!(*cgen)((ast_expression*)(self->func), func, false, &funval))
2861 for (i = 0; i < vec_size(self->params); ++i)
2864 ast_expression *expr = self->params[i];
2866 cgen = expr->expression.codegen;
2867 if (!(*cgen)(expr, func, false, ¶m))
2871 vec_push(params, param);
2874 callinstr = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "call"), funval);
2878 for (i = 0; i < vec_size(params); ++i) {
2879 ir_call_param(callinstr, params[i]);
2882 *out = ir_call_value(callinstr);
2883 self->expression.outr = *out;
2885 codegen_output_type(self, *out);