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.params = NULL;
75 self->expression.count = 0;
76 self->expression.flags = 0;
79 static void ast_expression_delete(ast_expression *self)
82 if (self->expression.next)
83 ast_delete(self->expression.next);
84 for (i = 0; i < vec_size(self->expression.params); ++i) {
85 ast_delete(self->expression.params[i]);
87 vec_free(self->expression.params);
90 static void ast_expression_delete_full(ast_expression *self)
92 ast_expression_delete(self);
96 ast_value* ast_value_copy(const ast_value *self)
99 const ast_expression_common *fromex;
100 ast_expression_common *selfex;
101 ast_value *cp = ast_value_new(self->expression.node.context, self->name, self->expression.vtype);
102 if (self->expression.next) {
103 cp->expression.next = ast_type_copy(self->expression.node.context, self->expression.next);
104 if (!cp->expression.next) {
105 ast_value_delete(cp);
109 fromex = &self->expression;
110 selfex = &cp->expression;
111 selfex->count = fromex->count;
112 selfex->flags = fromex->flags;
113 for (i = 0; i < vec_size(fromex->params); ++i) {
114 ast_value *v = ast_value_copy(fromex->params[i]);
116 ast_value_delete(cp);
119 vec_push(selfex->params, v);
124 bool ast_type_adopt_impl(ast_expression *self, const ast_expression *other)
127 const ast_expression_common *fromex;
128 ast_expression_common *selfex;
129 self->expression.vtype = other->expression.vtype;
130 if (other->expression.next) {
131 self->expression.next = (ast_expression*)ast_type_copy(ast_ctx(self), other->expression.next);
132 if (!self->expression.next)
135 fromex = &other->expression;
136 selfex = &self->expression;
137 selfex->count = fromex->count;
138 selfex->flags = fromex->flags;
139 for (i = 0; i < vec_size(fromex->params); ++i) {
140 ast_value *v = ast_value_copy(fromex->params[i]);
143 vec_push(selfex->params, v);
148 static ast_expression* ast_shallow_type(lex_ctx ctx, int vtype)
150 ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
151 ast_expression_init(self, NULL);
152 self->expression.codegen = NULL;
153 self->expression.next = NULL;
154 self->expression.vtype = vtype;
158 ast_expression* ast_type_copy(lex_ctx ctx, const ast_expression *ex)
161 const ast_expression_common *fromex;
162 ast_expression_common *selfex;
168 ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
169 ast_expression_init(self, NULL);
171 fromex = &ex->expression;
172 selfex = &self->expression;
174 /* This may never be codegen()d */
175 selfex->codegen = NULL;
177 selfex->vtype = fromex->vtype;
180 selfex->next = ast_type_copy(ctx, fromex->next);
182 ast_expression_delete_full(self);
189 selfex->count = fromex->count;
190 selfex->flags = fromex->flags;
191 for (i = 0; i < vec_size(fromex->params); ++i) {
192 ast_value *v = ast_value_copy(fromex->params[i]);
194 ast_expression_delete_full(self);
197 vec_push(selfex->params, v);
204 bool ast_compare_type(ast_expression *a, ast_expression *b)
206 if (a->expression.vtype != b->expression.vtype)
208 if (!a->expression.next != !b->expression.next)
210 if (vec_size(a->expression.params) != vec_size(b->expression.params))
212 if (a->expression.flags != b->expression.flags)
214 if (vec_size(a->expression.params)) {
216 for (i = 0; i < vec_size(a->expression.params); ++i) {
217 if (!ast_compare_type((ast_expression*)a->expression.params[i],
218 (ast_expression*)b->expression.params[i]))
222 if (a->expression.next)
223 return ast_compare_type(a->expression.next, b->expression.next);
227 static size_t ast_type_to_string_impl(ast_expression *e, char *buf, size_t bufsize, size_t pos)
234 if (pos + 6 >= bufsize)
236 strcpy(buf + pos, "(null)");
240 if (pos + 1 >= bufsize)
243 switch (e->expression.vtype) {
245 strcpy(buf + pos, "(variant)");
250 return ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
253 if (pos + 3 >= bufsize)
257 pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
258 if (pos + 1 >= bufsize)
264 pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
265 if (pos + 2 >= bufsize)
267 if (!vec_size(e->expression.params)) {
273 pos = ast_type_to_string_impl((ast_expression*)(e->expression.params[0]), buf, bufsize, pos);
274 for (i = 1; i < vec_size(e->expression.params); ++i) {
275 if (pos + 2 >= bufsize)
279 pos = ast_type_to_string_impl((ast_expression*)(e->expression.params[i]), buf, bufsize, pos);
281 if (pos + 1 >= bufsize)
287 pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
288 if (pos + 1 >= bufsize)
291 pos += snprintf(buf + pos, bufsize - pos - 1, "%i", (int)e->expression.count);
292 if (pos + 1 >= bufsize)
298 typestr = type_name[e->expression.vtype];
299 typelen = strlen(typestr);
300 if (pos + typelen >= bufsize)
302 strcpy(buf + pos, typestr);
303 return pos + typelen;
307 buf[bufsize-3] = '.';
308 buf[bufsize-2] = '.';
309 buf[bufsize-1] = '.';
313 void ast_type_to_string(ast_expression *e, char *buf, size_t bufsize)
315 size_t pos = ast_type_to_string_impl(e, buf, bufsize-1, 0);
319 ast_value* ast_value_new(lex_ctx ctx, const char *name, int t)
321 ast_instantiate(ast_value, ctx, ast_value_delete);
322 ast_expression_init((ast_expression*)self,
323 (ast_expression_codegen*)&ast_value_codegen);
324 self->expression.node.keep = true; /* keep */
326 self->name = name ? util_strdup(name) : NULL;
327 self->expression.vtype = t;
328 self->expression.next = NULL;
329 self->isfield = false;
331 self->hasvalue = false;
333 memset(&self->constval, 0, sizeof(self->constval));
336 self->ir_values = NULL;
337 self->ir_value_count = 0;
345 void ast_value_delete(ast_value* self)
348 mem_d((void*)self->name);
349 if (self->hasvalue) {
350 switch (self->expression.vtype)
353 mem_d((void*)self->constval.vstring);
356 /* unlink us from the function node */
357 self->constval.vfunc->vtype = NULL;
359 /* NOTE: delete function? currently collected in
360 * the parser structure
367 mem_d(self->ir_values);
368 ast_expression_delete((ast_expression*)self);
372 void ast_value_params_add(ast_value *self, ast_value *p)
374 vec_push(self->expression.params, p);
377 bool ast_value_set_name(ast_value *self, const char *name)
380 mem_d((void*)self->name);
381 self->name = util_strdup(name);
385 ast_binary* ast_binary_new(lex_ctx ctx, int op,
386 ast_expression* left, ast_expression* right)
388 ast_instantiate(ast_binary, ctx, ast_binary_delete);
389 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binary_codegen);
395 ast_propagate_effects(self, left);
396 ast_propagate_effects(self, right);
398 if (op >= INSTR_EQ_F && op <= INSTR_GT)
399 self->expression.vtype = TYPE_FLOAT;
400 else if (op == INSTR_AND || op == INSTR_OR) {
401 if (OPTS_FLAG(PERL_LOGIC))
402 ast_type_adopt(self, right);
404 self->expression.vtype = TYPE_FLOAT;
406 else if (op == INSTR_BITAND || op == INSTR_BITOR)
407 self->expression.vtype = TYPE_FLOAT;
408 else if (op == INSTR_MUL_VF || op == INSTR_MUL_FV)
409 self->expression.vtype = TYPE_VECTOR;
410 else if (op == INSTR_MUL_V)
411 self->expression.vtype = TYPE_FLOAT;
413 self->expression.vtype = left->expression.vtype;
418 void ast_binary_delete(ast_binary *self)
420 ast_unref(self->left);
421 ast_unref(self->right);
422 ast_expression_delete((ast_expression*)self);
426 ast_binstore* ast_binstore_new(lex_ctx ctx, int storop, int op,
427 ast_expression* left, ast_expression* right)
429 ast_instantiate(ast_binstore, ctx, ast_binstore_delete);
430 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binstore_codegen);
432 ast_side_effects(self) = true;
434 self->opstore = storop;
437 self->source = right;
439 self->keep_dest = false;
441 if (!ast_type_adopt(self, left)) {
449 void ast_binstore_delete(ast_binstore *self)
451 if (!self->keep_dest)
452 ast_unref(self->dest);
453 ast_unref(self->source);
454 ast_expression_delete((ast_expression*)self);
458 ast_unary* ast_unary_new(lex_ctx ctx, int op,
459 ast_expression *expr)
461 ast_instantiate(ast_unary, ctx, ast_unary_delete);
462 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_unary_codegen);
465 self->operand = expr;
467 ast_propagate_effects(self, expr);
469 if (op >= INSTR_NOT_F && op <= INSTR_NOT_FNC) {
470 self->expression.vtype = TYPE_FLOAT;
472 compile_error(ctx, "cannot determine type of unary operation %s", asm_instr[op].m);
477 void ast_unary_delete(ast_unary *self)
479 if (self->operand) ast_unref(self->operand);
480 ast_expression_delete((ast_expression*)self);
484 ast_return* ast_return_new(lex_ctx ctx, ast_expression *expr)
486 ast_instantiate(ast_return, ctx, ast_return_delete);
487 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_return_codegen);
489 self->operand = expr;
492 ast_propagate_effects(self, expr);
497 void ast_return_delete(ast_return *self)
500 ast_unref(self->operand);
501 ast_expression_delete((ast_expression*)self);
505 ast_entfield* ast_entfield_new(lex_ctx ctx, ast_expression *entity, ast_expression *field)
507 if (field->expression.vtype != TYPE_FIELD) {
508 compile_error(ctx, "ast_entfield_new with expression not of type field");
511 return ast_entfield_new_force(ctx, entity, field, field->expression.next);
514 ast_entfield* ast_entfield_new_force(lex_ctx ctx, ast_expression *entity, ast_expression *field, const ast_expression *outtype)
516 ast_instantiate(ast_entfield, ctx, ast_entfield_delete);
520 /* Error: field has no type... */
524 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_entfield_codegen);
526 self->entity = entity;
528 ast_propagate_effects(self, entity);
529 ast_propagate_effects(self, field);
531 if (!ast_type_adopt(self, outtype)) {
532 ast_entfield_delete(self);
539 void ast_entfield_delete(ast_entfield *self)
541 ast_unref(self->entity);
542 ast_unref(self->field);
543 ast_expression_delete((ast_expression*)self);
547 ast_member* ast_member_new(lex_ctx ctx, ast_expression *owner, unsigned int field, const char *name)
549 ast_instantiate(ast_member, ctx, ast_member_delete);
555 if (owner->expression.vtype != TYPE_VECTOR &&
556 owner->expression.vtype != TYPE_FIELD) {
557 compile_error(ctx, "member-access on an invalid owner of type %s", type_name[owner->expression.vtype]);
562 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_member_codegen);
563 self->expression.node.keep = true; /* keep */
565 if (owner->expression.vtype == TYPE_VECTOR) {
566 self->expression.vtype = TYPE_FLOAT;
567 self->expression.next = NULL;
569 self->expression.vtype = TYPE_FIELD;
570 self->expression.next = ast_shallow_type(ctx, TYPE_FLOAT);
574 ast_propagate_effects(self, owner);
578 self->name = util_strdup(name);
585 void ast_member_delete(ast_member *self)
587 /* The owner is always an ast_value, which has .keep=true,
588 * also: ast_members are usually deleted after the owner, thus
589 * this will cause invalid access
590 ast_unref(self->owner);
591 * once we allow (expression).x to access a vector-member, we need
592 * to change this: preferably by creating an alternate ast node for this
593 * purpose that is not garbage-collected.
595 ast_expression_delete((ast_expression*)self);
599 ast_array_index* ast_array_index_new(lex_ctx ctx, ast_expression *array, ast_expression *index)
601 ast_expression *outtype;
602 ast_instantiate(ast_array_index, ctx, ast_array_index_delete);
604 outtype = array->expression.next;
607 /* Error: field has no type... */
611 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_array_index_codegen);
615 ast_propagate_effects(self, array);
616 ast_propagate_effects(self, index);
618 if (!ast_type_adopt(self, outtype)) {
619 ast_array_index_delete(self);
622 if (array->expression.vtype == TYPE_FIELD && outtype->expression.vtype == TYPE_ARRAY) {
623 if (self->expression.vtype != TYPE_ARRAY) {
624 compile_error(ast_ctx(self), "array_index node on type");
625 ast_array_index_delete(self);
628 self->array = outtype;
629 self->expression.vtype = TYPE_FIELD;
635 void ast_array_index_delete(ast_array_index *self)
637 ast_unref(self->array);
638 ast_unref(self->index);
639 ast_expression_delete((ast_expression*)self);
643 ast_ifthen* ast_ifthen_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
645 ast_instantiate(ast_ifthen, ctx, ast_ifthen_delete);
646 if (!ontrue && !onfalse) {
647 /* because it is invalid */
651 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ifthen_codegen);
654 self->on_true = ontrue;
655 self->on_false = onfalse;
656 ast_propagate_effects(self, cond);
658 ast_propagate_effects(self, ontrue);
660 ast_propagate_effects(self, onfalse);
665 void ast_ifthen_delete(ast_ifthen *self)
667 ast_unref(self->cond);
669 ast_unref(self->on_true);
671 ast_unref(self->on_false);
672 ast_expression_delete((ast_expression*)self);
676 ast_ternary* ast_ternary_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
678 ast_instantiate(ast_ternary, ctx, ast_ternary_delete);
679 /* This time NEITHER must be NULL */
680 if (!ontrue || !onfalse) {
684 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ternary_codegen);
687 self->on_true = ontrue;
688 self->on_false = onfalse;
689 ast_propagate_effects(self, cond);
690 ast_propagate_effects(self, ontrue);
691 ast_propagate_effects(self, onfalse);
693 if (!ast_type_adopt(self, ontrue)) {
694 ast_ternary_delete(self);
701 void ast_ternary_delete(ast_ternary *self)
703 ast_unref(self->cond);
704 ast_unref(self->on_true);
705 ast_unref(self->on_false);
706 ast_expression_delete((ast_expression*)self);
710 ast_loop* ast_loop_new(lex_ctx ctx,
711 ast_expression *initexpr,
712 ast_expression *precond, bool pre_not,
713 ast_expression *postcond, bool post_not,
714 ast_expression *increment,
715 ast_expression *body)
717 ast_instantiate(ast_loop, ctx, ast_loop_delete);
718 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_loop_codegen);
720 self->initexpr = initexpr;
721 self->precond = precond;
722 self->postcond = postcond;
723 self->increment = increment;
726 self->pre_not = pre_not;
727 self->post_not = post_not;
730 ast_propagate_effects(self, initexpr);
732 ast_propagate_effects(self, precond);
734 ast_propagate_effects(self, postcond);
736 ast_propagate_effects(self, increment);
738 ast_propagate_effects(self, body);
743 void ast_loop_delete(ast_loop *self)
746 ast_unref(self->initexpr);
748 ast_unref(self->precond);
750 ast_unref(self->postcond);
752 ast_unref(self->increment);
754 ast_unref(self->body);
755 ast_expression_delete((ast_expression*)self);
759 ast_breakcont* ast_breakcont_new(lex_ctx ctx, bool iscont)
761 ast_instantiate(ast_breakcont, ctx, ast_breakcont_delete);
762 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_breakcont_codegen);
764 self->is_continue = iscont;
769 void ast_breakcont_delete(ast_breakcont *self)
771 ast_expression_delete((ast_expression*)self);
775 ast_switch* ast_switch_new(lex_ctx ctx, ast_expression *op)
777 ast_instantiate(ast_switch, ctx, ast_switch_delete);
778 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_switch_codegen);
783 ast_propagate_effects(self, op);
788 void ast_switch_delete(ast_switch *self)
791 ast_unref(self->operand);
793 for (i = 0; i < vec_size(self->cases); ++i) {
794 if (self->cases[i].value)
795 ast_unref(self->cases[i].value);
796 ast_unref(self->cases[i].code);
798 vec_free(self->cases);
800 ast_expression_delete((ast_expression*)self);
804 ast_label* ast_label_new(lex_ctx ctx, const char *name)
806 ast_instantiate(ast_label, ctx, ast_label_delete);
807 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_label_codegen);
809 self->name = util_strdup(name);
810 self->irblock = NULL;
816 void ast_label_delete(ast_label *self)
818 mem_d((void*)self->name);
819 vec_free(self->gotos);
820 ast_expression_delete((ast_expression*)self);
824 void ast_label_register_goto(ast_label *self, ast_goto *g)
826 vec_push(self->gotos, g);
829 ast_goto* ast_goto_new(lex_ctx ctx, const char *name)
831 ast_instantiate(ast_goto, ctx, ast_goto_delete);
832 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_goto_codegen);
834 self->name = util_strdup(name);
836 self->irblock_from = NULL;
841 void ast_goto_delete(ast_goto *self)
843 mem_d((void*)self->name);
844 ast_expression_delete((ast_expression*)self);
848 void ast_goto_set_label(ast_goto *self, ast_label *label)
850 self->target = label;
853 ast_call* ast_call_new(lex_ctx ctx,
854 ast_expression *funcexpr)
856 ast_instantiate(ast_call, ctx, ast_call_delete);
857 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_call_codegen);
859 ast_side_effects(self) = true;
862 self->func = funcexpr;
864 ast_type_adopt(self, funcexpr->expression.next);
869 void ast_call_delete(ast_call *self)
872 for (i = 0; i < vec_size(self->params); ++i)
873 ast_unref(self->params[i]);
874 vec_free(self->params);
877 ast_unref(self->func);
879 ast_expression_delete((ast_expression*)self);
883 bool ast_call_check_types(ast_call *self)
887 const ast_expression *func = self->func;
888 size_t count = vec_size(self->params);
889 if (count > vec_size(func->expression.params))
890 count = vec_size(func->expression.params);
892 for (i = 0; i < count; ++i) {
893 if (!ast_compare_type(self->params[i], (ast_expression*)(func->expression.params[i]))) {
896 ast_type_to_string(self->params[i], tgot, sizeof(tgot));
897 ast_type_to_string((ast_expression*)func->expression.params[i], texp, sizeof(texp));
898 compile_error(ast_ctx(self), "invalid type for parameter %u in function call: expected %s, got %s",
899 (unsigned int)(i+1), texp, tgot);
900 /* we don't immediately return */
907 ast_store* ast_store_new(lex_ctx ctx, int op,
908 ast_expression *dest, ast_expression *source)
910 ast_instantiate(ast_store, ctx, ast_store_delete);
911 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_store_codegen);
913 ast_side_effects(self) = true;
917 self->source = source;
919 if (!ast_type_adopt(self, dest)) {
927 void ast_store_delete(ast_store *self)
929 ast_unref(self->dest);
930 ast_unref(self->source);
931 ast_expression_delete((ast_expression*)self);
935 ast_block* ast_block_new(lex_ctx ctx)
937 ast_instantiate(ast_block, ctx, ast_block_delete);
938 ast_expression_init((ast_expression*)self,
939 (ast_expression_codegen*)&ast_block_codegen);
943 self->collect = NULL;
948 bool ast_block_add_expr(ast_block *self, ast_expression *e)
950 ast_propagate_effects(self, e);
951 vec_push(self->exprs, e);
952 if (self->expression.next) {
953 ast_delete(self->expression.next);
954 self->expression.next = NULL;
956 if (!ast_type_adopt(self, e)) {
957 compile_error(ast_ctx(self), "internal error: failed to adopt type");
963 void ast_block_collect(ast_block *self, ast_expression *expr)
965 vec_push(self->collect, expr);
966 expr->expression.node.keep = true;
969 void ast_block_delete(ast_block *self)
972 for (i = 0; i < vec_size(self->exprs); ++i)
973 ast_unref(self->exprs[i]);
974 vec_free(self->exprs);
975 for (i = 0; i < vec_size(self->locals); ++i)
976 ast_delete(self->locals[i]);
977 vec_free(self->locals);
978 for (i = 0; i < vec_size(self->collect); ++i)
979 ast_delete(self->collect[i]);
980 vec_free(self->collect);
981 ast_expression_delete((ast_expression*)self);
985 bool ast_block_set_type(ast_block *self, ast_expression *from)
987 if (self->expression.next)
988 ast_delete(self->expression.next);
989 if (!ast_type_adopt(self, from))
994 ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype)
996 ast_instantiate(ast_function, ctx, ast_function_delete);
1000 vtype->expression.vtype != TYPE_FUNCTION)
1002 compile_error(ast_ctx(self), "internal error: ast_function_new condition %i %i type=%i (probably 2 bodies?)",
1004 (int)vtype->hasvalue,
1005 vtype->expression.vtype);
1010 self->vtype = vtype;
1011 self->name = name ? util_strdup(name) : NULL;
1012 self->blocks = NULL;
1014 self->labelcount = 0;
1017 self->ir_func = NULL;
1018 self->curblock = NULL;
1020 self->breakblock = NULL;
1021 self->continueblock = NULL;
1023 vtype->hasvalue = true;
1024 vtype->constval.vfunc = self;
1029 void ast_function_delete(ast_function *self)
1033 mem_d((void*)self->name);
1035 /* ast_value_delete(self->vtype); */
1036 self->vtype->hasvalue = false;
1037 self->vtype->constval.vfunc = NULL;
1038 /* We use unref - if it was stored in a global table it is supposed
1039 * to be deleted from *there*
1041 ast_unref(self->vtype);
1043 for (i = 0; i < vec_size(self->blocks); ++i)
1044 ast_delete(self->blocks[i]);
1045 vec_free(self->blocks);
1049 const char* ast_function_label(ast_function *self, const char *prefix)
1055 if (!opts.dump && !opts.dumpfin && !opts.debug)
1058 id = (self->labelcount++);
1059 len = strlen(prefix);
1061 from = self->labelbuf + sizeof(self->labelbuf)-1;
1064 *from-- = (id%10) + '0';
1068 memcpy(from - len, prefix, len);
1072 /*********************************************************************/
1074 * by convention you must never pass NULL to the 'ir_value **out'
1075 * parameter. If you really don't care about the output, pass a dummy.
1076 * But I can't imagine a pituation where the output is truly unnecessary.
1079 void _ast_codegen_output_type(ast_expression_common *self, ir_value *out)
1081 if (out->vtype == TYPE_FIELD)
1082 out->fieldtype = self->next->expression.vtype;
1083 if (out->vtype == TYPE_FUNCTION)
1084 out->outtype = self->next->expression.vtype;
1087 #define codegen_output_type(a,o) (_ast_codegen_output_type(&((a)->expression),(o)))
1089 bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out)
1093 /* NOTE: This is the codegen for a variable used in an expression.
1094 * It is not the codegen to generate the value. For this purpose,
1095 * ast_local_codegen and ast_global_codegen are to be used before this
1096 * is executed. ast_function_codegen should take care of its locals,
1097 * and the ast-user should take care of ast_global_codegen to be used
1098 * on all the globals.
1101 char typename[1024];
1102 ast_type_to_string((ast_expression*)self, typename, sizeof(typename));
1103 compile_error(ast_ctx(self), "ast_value used before generated %s %s", typename, self->name);
1110 bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
1114 if (self->hasvalue && self->expression.vtype == TYPE_FUNCTION)
1116 ir_function *func = ir_builder_create_function(ir, self->name, self->expression.next->expression.vtype);
1119 func->context = ast_ctx(self);
1120 func->value->context = ast_ctx(self);
1122 self->constval.vfunc->ir_func = func;
1123 self->ir_v = func->value;
1124 /* The function is filled later on ast_function_codegen... */
1128 if (isfield && self->expression.vtype == TYPE_FIELD) {
1129 ast_expression *fieldtype = self->expression.next;
1131 if (self->hasvalue) {
1132 compile_error(ast_ctx(self), "TODO: constant field pointers with value");
1136 if (fieldtype->expression.vtype == TYPE_ARRAY) {
1141 ast_expression_common *elemtype;
1143 ast_value *array = (ast_value*)fieldtype;
1145 if (!ast_istype(fieldtype, ast_value)) {
1146 compile_error(ast_ctx(self), "internal error: ast_value required");
1150 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1151 if (!array->expression.count || array->expression.count > opts.max_array_size)
1152 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)array->expression.count);
1154 elemtype = &array->expression.next->expression;
1155 vtype = elemtype->vtype;
1157 v = ir_builder_create_field(ir, self->name, vtype);
1159 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", self->name);
1162 v->context = ast_ctx(self);
1163 v->unique_life = true;
1164 array->ir_v = self->ir_v = v;
1166 namelen = strlen(self->name);
1167 name = (char*)mem_a(namelen + 16);
1168 strcpy(name, self->name);
1170 array->ir_values = (ir_value**)mem_a(sizeof(array->ir_values[0]) * array->expression.count);
1171 array->ir_values[0] = v;
1172 for (ai = 1; ai < array->expression.count; ++ai) {
1173 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1174 array->ir_values[ai] = ir_builder_create_field(ir, name, vtype);
1175 if (!array->ir_values[ai]) {
1177 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", name);
1180 array->ir_values[ai]->context = ast_ctx(self);
1181 array->ir_values[ai]->unique_life = true;
1187 v = ir_builder_create_field(ir, self->name, self->expression.next->expression.vtype);
1190 v->context = ast_ctx(self);
1196 if (self->expression.vtype == TYPE_ARRAY) {
1201 ast_expression_common *elemtype = &self->expression.next->expression;
1202 int vtype = elemtype->vtype;
1204 /* same as with field arrays */
1205 if (!self->expression.count || self->expression.count > opts.max_array_size)
1206 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1208 v = ir_builder_create_global(ir, self->name, vtype);
1210 compile_error(ast_ctx(self), "ir_builder_create_global failed `%s`", self->name);
1213 v->context = ast_ctx(self);
1214 v->unique_life = true;
1216 namelen = strlen(self->name);
1217 name = (char*)mem_a(namelen + 16);
1218 strcpy(name, self->name);
1220 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1221 self->ir_values[0] = v;
1222 for (ai = 1; ai < self->expression.count; ++ai) {
1223 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1224 self->ir_values[ai] = ir_builder_create_global(ir, name, vtype);
1225 if (!self->ir_values[ai]) {
1227 compile_error(ast_ctx(self), "ir_builder_create_global failed `%s`", name);
1230 self->ir_values[ai]->context = ast_ctx(self);
1231 self->ir_values[ai]->unique_life = true;
1237 /* Arrays don't do this since there's no "array" value which spans across the
1240 v = ir_builder_create_global(ir, self->name, self->expression.vtype);
1242 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", self->name);
1245 codegen_output_type(self, v);
1246 v->context = ast_ctx(self);
1249 if (self->hasvalue) {
1250 switch (self->expression.vtype)
1253 if (!ir_value_set_float(v, self->constval.vfloat))
1257 if (!ir_value_set_vector(v, self->constval.vvec))
1261 if (!ir_value_set_string(v, self->constval.vstring))
1265 compile_error(ast_ctx(self), "TODO: global constant array");
1268 compile_error(ast_ctx(self), "global of type function not properly generated");
1270 /* Cannot generate an IR value for a function,
1271 * need a pointer pointing to a function rather.
1274 if (!self->constval.vfield) {
1275 compile_error(ast_ctx(self), "field constant without vfield set");
1278 if (!self->constval.vfield->ir_v) {
1279 compile_error(ast_ctx(self), "field constant generated before its field");
1282 if (!ir_value_set_field(v, self->constval.vfield->ir_v))
1286 compile_error(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1291 /* link us to the ir_value */
1296 error: /* clean up */
1301 bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
1304 if (self->hasvalue && self->expression.vtype == TYPE_FUNCTION)
1306 /* Do we allow local functions? I think not...
1307 * this is NOT a function pointer atm.
1312 if (self->expression.vtype == TYPE_ARRAY) {
1317 ast_expression_common *elemtype = &self->expression.next->expression;
1318 int vtype = elemtype->vtype;
1321 compile_error(ast_ctx(self), "array-parameters are not supported");
1325 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1326 if (!self->expression.count || self->expression.count > opts.max_array_size) {
1327 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1330 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1331 if (!self->ir_values) {
1332 compile_error(ast_ctx(self), "failed to allocate array values");
1336 v = ir_function_create_local(func, self->name, vtype, param);
1338 compile_error(ast_ctx(self), "ir_function_create_local failed");
1341 v->context = ast_ctx(self);
1342 v->unique_life = true;
1344 namelen = strlen(self->name);
1345 name = (char*)mem_a(namelen + 16);
1346 strcpy(name, self->name);
1348 self->ir_values[0] = v;
1349 for (ai = 1; ai < self->expression.count; ++ai) {
1350 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1351 self->ir_values[ai] = ir_function_create_local(func, name, vtype, param);
1352 if (!self->ir_values[ai]) {
1353 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", name);
1356 self->ir_values[ai]->context = ast_ctx(self);
1357 self->ir_values[ai]->unique_life = true;
1362 v = ir_function_create_local(func, self->name, self->expression.vtype, param);
1365 codegen_output_type(self, v);
1366 v->context = ast_ctx(self);
1369 /* A constant local... hmmm...
1370 * I suppose the IR will have to deal with this
1372 if (self->hasvalue) {
1373 switch (self->expression.vtype)
1376 if (!ir_value_set_float(v, self->constval.vfloat))
1380 if (!ir_value_set_vector(v, self->constval.vvec))
1384 if (!ir_value_set_string(v, self->constval.vstring))
1388 compile_error(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1393 /* link us to the ir_value */
1397 if (!ast_generate_accessors(self, func->owner))
1401 error: /* clean up */
1406 bool ast_generate_accessors(ast_value *self, ir_builder *ir)
1409 bool warn = OPTS_WARN(WARN_USED_UNINITIALIZED);
1410 if (!self->setter || !self->getter)
1412 for (i = 0; i < self->expression.count; ++i) {
1413 if (!self->ir_values) {
1414 compile_error(ast_ctx(self), "internal error: no array values generated for `%s`", self->name);
1417 if (!self->ir_values[i]) {
1418 compile_error(ast_ctx(self), "internal error: not all array values have been generated for `%s`", self->name);
1421 if (self->ir_values[i]->life) {
1422 compile_error(ast_ctx(self), "internal error: function containing `%s` already generated", self->name);
1427 opts_set(opts.warn, WARN_USED_UNINITIALIZED, false);
1429 if (!ast_global_codegen (self->setter, ir, false) ||
1430 !ast_function_codegen(self->setter->constval.vfunc, ir) ||
1431 !ir_function_finalize(self->setter->constval.vfunc->ir_func))
1433 compile_error(ast_ctx(self), "internal error: failed to generate setter for `%s`", self->name);
1434 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1439 if (!ast_global_codegen (self->getter, ir, false) ||
1440 !ast_function_codegen(self->getter->constval.vfunc, ir) ||
1441 !ir_function_finalize(self->getter->constval.vfunc->ir_func))
1443 compile_error(ast_ctx(self), "internal error: failed to generate getter for `%s`", self->name);
1444 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1448 for (i = 0; i < self->expression.count; ++i) {
1449 vec_free(self->ir_values[i]->life);
1451 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1455 bool ast_function_codegen(ast_function *self, ir_builder *ir)
1459 ast_expression_common *ec;
1464 irf = self->ir_func;
1466 compile_error(ast_ctx(self), "ast_function's related ast_value was not generated yet");
1470 /* fill the parameter list */
1471 ec = &self->vtype->expression;
1472 for (i = 0; i < vec_size(ec->params); ++i)
1474 if (ec->params[i]->expression.vtype == TYPE_FIELD)
1475 vec_push(irf->params, ec->params[i]->expression.next->expression.vtype);
1477 vec_push(irf->params, ec->params[i]->expression.vtype);
1478 if (!self->builtin) {
1479 if (!ast_local_codegen(ec->params[i], self->ir_func, true))
1484 if (self->builtin) {
1485 irf->builtin = self->builtin;
1489 if (!vec_size(self->blocks)) {
1490 compile_error(ast_ctx(self), "function `%s` has no body", self->name);
1494 self->curblock = ir_function_create_block(ast_ctx(self), irf, "entry");
1495 if (!self->curblock) {
1496 compile_error(ast_ctx(self), "failed to allocate entry block for `%s`", self->name);
1500 for (i = 0; i < vec_size(self->blocks); ++i) {
1501 ast_expression_codegen *gen = self->blocks[i]->expression.codegen;
1502 if (!(*gen)((ast_expression*)self->blocks[i], self, false, &dummy))
1506 /* TODO: check return types */
1507 if (!self->curblock->final)
1509 if (!self->vtype->expression.next ||
1510 self->vtype->expression.next->expression.vtype == TYPE_VOID)
1512 return ir_block_create_return(self->curblock, ast_ctx(self), NULL);
1514 else if (vec_size(self->curblock->entries))
1516 /* error("missing return"); */
1517 if (compile_warning(ast_ctx(self), WARN_MISSING_RETURN_VALUES,
1518 "control reaches end of non-void function (`%s`) via %s",
1519 self->name, self->curblock->label))
1523 return ir_block_create_return(self->curblock, ast_ctx(self), NULL);
1529 /* Note, you will not see ast_block_codegen generate ir_blocks.
1530 * To the AST and the IR, blocks are 2 different things.
1531 * In the AST it represents a block of code, usually enclosed in
1532 * curly braces {...}.
1533 * While in the IR it represents a block in terms of control-flow.
1535 bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_value **out)
1539 /* We don't use this
1540 * Note: an ast-representation using the comma-operator
1541 * of the form: (a, b, c) = x should not assign to c...
1544 compile_error(ast_ctx(self), "not an l-value (code-block)");
1548 if (self->expression.outr) {
1549 *out = self->expression.outr;
1553 /* output is NULL at first, we'll have each expression
1554 * assign to out output, thus, a comma-operator represention
1555 * using an ast_block will return the last generated value,
1556 * so: (b, c) + a executed both b and c, and returns c,
1557 * which is then added to a.
1561 /* generate locals */
1562 for (i = 0; i < vec_size(self->locals); ++i)
1564 if (!ast_local_codegen(self->locals[i], func->ir_func, false)) {
1566 compile_error(ast_ctx(self), "failed to generate local `%s`", self->locals[i]->name);
1571 for (i = 0; i < vec_size(self->exprs); ++i)
1573 ast_expression_codegen *gen;
1574 if (func->curblock->final && !ast_istype(self->exprs[i], ast_label)) {
1575 if (compile_warning(ast_ctx(self->exprs[i]), WARN_UNREACHABLE_CODE, "unreachable statement"))
1579 gen = self->exprs[i]->expression.codegen;
1580 if (!(*gen)(self->exprs[i], func, false, out))
1584 self->expression.outr = *out;
1589 bool ast_store_codegen(ast_store *self, ast_function *func, bool lvalue, ir_value **out)
1591 ast_expression_codegen *cgen;
1592 ir_value *left = NULL;
1593 ir_value *right = NULL;
1597 ast_array_index *ai = NULL;
1599 if (lvalue && self->expression.outl) {
1600 *out = self->expression.outl;
1604 if (!lvalue && self->expression.outr) {
1605 *out = self->expression.outr;
1609 if (ast_istype(self->dest, ast_array_index))
1612 ai = (ast_array_index*)self->dest;
1613 idx = (ast_value*)ai->index;
1615 if (ast_istype(ai->index, ast_value) && idx->hasvalue && idx->cvq == CV_CONST)
1620 /* we need to call the setter */
1621 ir_value *iridx, *funval;
1625 compile_error(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1629 arr = (ast_value*)ai->array;
1630 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1631 compile_error(ast_ctx(self), "value has no setter (%s)", arr->name);
1635 cgen = idx->expression.codegen;
1636 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1639 cgen = arr->setter->expression.codegen;
1640 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
1643 cgen = self->source->expression.codegen;
1644 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1647 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "store"), funval, false);
1650 ir_call_param(call, iridx);
1651 ir_call_param(call, right);
1652 self->expression.outr = right;
1658 cgen = self->dest->expression.codegen;
1660 if (!(*cgen)((ast_expression*)(self->dest), func, true, &left))
1662 self->expression.outl = left;
1664 cgen = self->source->expression.codegen;
1666 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1669 if (!ir_block_create_store_op(func->curblock, ast_ctx(self), self->op, left, right))
1671 self->expression.outr = right;
1674 /* Theoretically, an assinment returns its left side as an
1675 * lvalue, if we don't need an lvalue though, we return
1676 * the right side as an rvalue, otherwise we have to
1677 * somehow know whether or not we need to dereference the pointer
1678 * on the left side - that is: OP_LOAD if it was an address.
1679 * Also: in original QC we cannot OP_LOADP *anyway*.
1681 *out = (lvalue ? left : right);
1686 bool ast_binary_codegen(ast_binary *self, ast_function *func, bool lvalue, ir_value **out)
1688 ast_expression_codegen *cgen;
1689 ir_value *left, *right;
1691 /* A binary operation cannot yield an l-value */
1693 compile_error(ast_ctx(self), "not an l-value (binop)");
1697 if (self->expression.outr) {
1698 *out = self->expression.outr;
1702 if (OPTS_FLAG(SHORT_LOGIC) &&
1703 (self->op == INSTR_AND || self->op == INSTR_OR))
1705 /* short circuit evaluation */
1706 ir_block *other, *merge;
1707 ir_block *from_left, *from_right;
1712 /* Note about casting to true boolean values:
1713 * We use a single NOT for sub expressions, and an
1714 * overall NOT at the end, and for that purpose swap
1715 * all the jump conditions in order for the NOT to get
1717 * ie: (a && b) usually becomes (!!a ? !!b : !!a)
1718 * but we translate this to (!(!a ? !a : !b))
1721 merge_id = vec_size(func->ir_func->blocks);
1722 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "sce_merge"));
1724 cgen = self->left->expression.codegen;
1725 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1727 if (!OPTS_FLAG(PERL_LOGIC)) {
1728 notop = type_not_instr[left->vtype];
1729 if (notop == AINSTR_END) {
1730 compile_error(ast_ctx(self), "don't know how to cast to bool...");
1733 left = ir_block_create_unary(func->curblock, ast_ctx(self),
1734 ast_function_label(func, "sce_not"),
1738 from_left = func->curblock;
1740 other = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "sce_other"));
1741 if ( !(self->op == INSTR_OR) != !OPTS_FLAG(PERL_LOGIC) ) {
1742 if (!ir_block_create_if(func->curblock, ast_ctx(self), left, other, merge))
1745 if (!ir_block_create_if(func->curblock, ast_ctx(self), left, merge, other))
1748 /* use the likely flag */
1749 vec_last(func->curblock->instr)->likely = true;
1751 func->curblock = other;
1752 cgen = self->right->expression.codegen;
1753 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1755 if (!OPTS_FLAG(PERL_LOGIC)) {
1756 notop = type_not_instr[right->vtype];
1757 if (notop == AINSTR_END) {
1758 compile_error(ast_ctx(self), "don't know how to cast to bool...");
1761 right = ir_block_create_unary(func->curblock, ast_ctx(self),
1762 ast_function_label(func, "sce_not"),
1766 from_right = func->curblock;
1768 if (!ir_block_create_jump(func->curblock, ast_ctx(self), merge))
1771 vec_remove(func->ir_func->blocks, merge_id, 1);
1772 vec_push(func->ir_func->blocks, merge);
1774 func->curblock = merge;
1775 phi = ir_block_create_phi(func->curblock, ast_ctx(self), ast_function_label(func, "sce_value"), TYPE_FLOAT);
1776 ir_phi_add(phi, from_left, left);
1777 ir_phi_add(phi, from_right, right);
1778 *out = ir_phi_value(phi);
1779 if (!OPTS_FLAG(PERL_LOGIC)) {
1780 notop = type_not_instr[(*out)->vtype];
1781 if (notop == AINSTR_END) {
1782 compile_error(ast_ctx(self), "don't know how to cast to bool...");
1785 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
1786 ast_function_label(func, "sce_final_not"),
1792 self->expression.outr = *out;
1796 cgen = self->left->expression.codegen;
1797 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1800 cgen = self->right->expression.codegen;
1801 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1804 *out = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "bin"),
1805 self->op, left, right);
1808 self->expression.outr = *out;
1813 bool ast_binstore_codegen(ast_binstore *self, ast_function *func, bool lvalue, ir_value **out)
1815 ast_expression_codegen *cgen;
1816 ir_value *leftl = NULL, *leftr, *right, *bin;
1820 ast_array_index *ai = NULL;
1821 ir_value *iridx = NULL;
1823 if (lvalue && self->expression.outl) {
1824 *out = self->expression.outl;
1828 if (!lvalue && self->expression.outr) {
1829 *out = self->expression.outr;
1833 if (ast_istype(self->dest, ast_array_index))
1836 ai = (ast_array_index*)self->dest;
1837 idx = (ast_value*)ai->index;
1839 if (ast_istype(ai->index, ast_value) && idx->hasvalue && idx->cvq == CV_CONST)
1843 /* for a binstore we need both an lvalue and an rvalue for the left side */
1844 /* rvalue of destination! */
1846 cgen = idx->expression.codegen;
1847 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1850 cgen = self->dest->expression.codegen;
1851 if (!(*cgen)((ast_expression*)(self->dest), func, false, &leftr))
1854 /* source as rvalue only */
1855 cgen = self->source->expression.codegen;
1856 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1859 /* now the binary */
1860 bin = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "binst"),
1861 self->opbin, leftr, right);
1862 self->expression.outr = bin;
1866 /* we need to call the setter */
1871 compile_error(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1875 arr = (ast_value*)ai->array;
1876 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1877 compile_error(ast_ctx(self), "value has no setter (%s)", arr->name);
1881 cgen = arr->setter->expression.codegen;
1882 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
1885 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "store"), funval, false);
1888 ir_call_param(call, iridx);
1889 ir_call_param(call, bin);
1890 self->expression.outr = bin;
1892 /* now store them */
1893 cgen = self->dest->expression.codegen;
1894 /* lvalue of destination */
1895 if (!(*cgen)((ast_expression*)(self->dest), func, true, &leftl))
1897 self->expression.outl = leftl;
1899 if (!ir_block_create_store_op(func->curblock, ast_ctx(self), self->opstore, leftl, bin))
1901 self->expression.outr = bin;
1904 /* Theoretically, an assinment returns its left side as an
1905 * lvalue, if we don't need an lvalue though, we return
1906 * the right side as an rvalue, otherwise we have to
1907 * somehow know whether or not we need to dereference the pointer
1908 * on the left side - that is: OP_LOAD if it was an address.
1909 * Also: in original QC we cannot OP_LOADP *anyway*.
1911 *out = (lvalue ? leftl : bin);
1916 bool ast_unary_codegen(ast_unary *self, ast_function *func, bool lvalue, ir_value **out)
1918 ast_expression_codegen *cgen;
1921 /* An unary operation cannot yield an l-value */
1923 compile_error(ast_ctx(self), "not an l-value (binop)");
1927 if (self->expression.outr) {
1928 *out = self->expression.outr;
1932 cgen = self->operand->expression.codegen;
1934 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
1937 *out = ir_block_create_unary(func->curblock, ast_ctx(self), ast_function_label(func, "unary"),
1941 self->expression.outr = *out;
1946 bool ast_return_codegen(ast_return *self, ast_function *func, bool lvalue, ir_value **out)
1948 ast_expression_codegen *cgen;
1953 /* In the context of a return operation, we don't actually return
1957 compile_error(ast_ctx(self), "return-expression is not an l-value");
1961 if (self->expression.outr) {
1962 compile_error(ast_ctx(self), "internal error: ast_return cannot be reused, it bears no result!");
1965 self->expression.outr = (ir_value*)1;
1967 if (self->operand) {
1968 cgen = self->operand->expression.codegen;
1970 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
1973 if (!ir_block_create_return(func->curblock, ast_ctx(self), operand))
1976 if (!ir_block_create_return(func->curblock, ast_ctx(self), NULL))
1983 bool ast_entfield_codegen(ast_entfield *self, ast_function *func, bool lvalue, ir_value **out)
1985 ast_expression_codegen *cgen;
1986 ir_value *ent, *field;
1988 /* This function needs to take the 'lvalue' flag into account!
1989 * As lvalue we provide a field-pointer, as rvalue we provide the
1993 if (lvalue && self->expression.outl) {
1994 *out = self->expression.outl;
1998 if (!lvalue && self->expression.outr) {
1999 *out = self->expression.outr;
2003 cgen = self->entity->expression.codegen;
2004 if (!(*cgen)((ast_expression*)(self->entity), func, false, &ent))
2007 cgen = self->field->expression.codegen;
2008 if (!(*cgen)((ast_expression*)(self->field), func, false, &field))
2013 *out = ir_block_create_fieldaddress(func->curblock, ast_ctx(self), ast_function_label(func, "efa"),
2016 *out = ir_block_create_load_from_ent(func->curblock, ast_ctx(self), ast_function_label(func, "efv"),
2017 ent, field, self->expression.vtype);
2018 /* Done AFTER error checking:
2019 codegen_output_type(self, *out);
2023 compile_error(ast_ctx(self), "failed to create %s instruction (output type %s)",
2024 (lvalue ? "ADDRESS" : "FIELD"),
2025 type_name[self->expression.vtype]);
2029 codegen_output_type(self, *out);
2032 self->expression.outl = *out;
2034 self->expression.outr = *out;
2036 /* Hm that should be it... */
2040 bool ast_member_codegen(ast_member *self, ast_function *func, bool lvalue, ir_value **out)
2042 ast_expression_codegen *cgen;
2045 /* in QC this is always an lvalue */
2047 if (self->expression.outl) {
2048 *out = self->expression.outl;
2052 cgen = self->owner->expression.codegen;
2053 if (!(*cgen)((ast_expression*)(self->owner), func, true, &vec))
2056 if (vec->vtype != TYPE_VECTOR &&
2057 !(vec->vtype == TYPE_FIELD && self->owner->expression.next->expression.vtype == TYPE_VECTOR))
2062 *out = ir_value_vector_member(vec, self->field);
2063 self->expression.outl = *out;
2065 return (*out != NULL);
2068 bool ast_array_index_codegen(ast_array_index *self, ast_function *func, bool lvalue, ir_value **out)
2073 if (!lvalue && self->expression.outr) {
2074 *out = self->expression.outr;
2076 if (lvalue && self->expression.outl) {
2077 *out = self->expression.outl;
2080 if (!ast_istype(self->array, ast_value)) {
2081 compile_error(ast_ctx(self), "array indexing this way is not supported");
2082 /* note this would actually be pointer indexing because the left side is
2083 * not an actual array but (hopefully) an indexable expression.
2084 * Once we get integer arithmetic, and GADDRESS/GSTORE/GLOAD instruction
2085 * support this path will be filled.
2090 arr = (ast_value*)self->array;
2091 idx = (ast_value*)self->index;
2093 if (!ast_istype(self->index, ast_value) || !idx->hasvalue || idx->cvq != CV_CONST) {
2094 /* Time to use accessor functions */
2095 ast_expression_codegen *cgen;
2096 ir_value *iridx, *funval;
2100 compile_error(ast_ctx(self), "(.2) array indexing here needs a compile-time constant");
2105 compile_error(ast_ctx(self), "value has no getter, don't know how to index it");
2109 cgen = self->index->expression.codegen;
2110 if (!(*cgen)((ast_expression*)(self->index), func, false, &iridx))
2113 cgen = arr->getter->expression.codegen;
2114 if (!(*cgen)((ast_expression*)(arr->getter), func, true, &funval))
2117 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "fetch"), funval, false);
2120 ir_call_param(call, iridx);
2122 *out = ir_call_value(call);
2123 self->expression.outr = *out;
2127 if (idx->expression.vtype == TYPE_FLOAT) {
2128 unsigned int arridx = idx->constval.vfloat;
2129 if (arridx >= self->array->expression.count)
2131 compile_error(ast_ctx(self), "array index out of bounds: %i", arridx);
2134 *out = arr->ir_values[arridx];
2136 else if (idx->expression.vtype == TYPE_INTEGER) {
2137 unsigned int arridx = idx->constval.vint;
2138 if (arridx >= self->array->expression.count)
2140 compile_error(ast_ctx(self), "array index out of bounds: %i", arridx);
2143 *out = arr->ir_values[arridx];
2146 compile_error(ast_ctx(self), "array indexing here needs an integer constant");
2152 bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_value **out)
2154 ast_expression_codegen *cgen;
2162 ir_block *ontrue_endblock = NULL;
2163 ir_block *onfalse_endblock = NULL;
2164 ir_block *merge = NULL;
2166 /* We don't output any value, thus also don't care about r/lvalue */
2170 if (self->expression.outr) {
2171 compile_error(ast_ctx(self), "internal error: ast_ifthen cannot be reused, it bears no result!");
2174 self->expression.outr = (ir_value*)1;
2176 /* generate the condition */
2177 cgen = self->cond->expression.codegen;
2178 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2180 /* update the block which will get the jump - because short-logic or ternaries may have changed this */
2181 cond = func->curblock;
2185 if (self->on_true) {
2186 /* create on-true block */
2187 ontrue = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "ontrue"));
2191 /* enter the block */
2192 func->curblock = ontrue;
2195 cgen = self->on_true->expression.codegen;
2196 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &dummy))
2199 /* we now need to work from the current endpoint */
2200 ontrue_endblock = func->curblock;
2205 if (self->on_false) {
2206 /* create on-false block */
2207 onfalse = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "onfalse"));
2211 /* enter the block */
2212 func->curblock = onfalse;
2215 cgen = self->on_false->expression.codegen;
2216 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &dummy))
2219 /* we now need to work from the current endpoint */
2220 onfalse_endblock = func->curblock;
2224 /* Merge block were they all merge in to */
2225 if (!ontrue || !onfalse || !ontrue_endblock->final || !onfalse_endblock->final)
2227 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "endif"));
2230 /* add jumps ot the merge block */
2231 if (ontrue && !ontrue_endblock->final && !ir_block_create_jump(ontrue_endblock, ast_ctx(self), merge))
2233 if (onfalse && !onfalse_endblock->final && !ir_block_create_jump(onfalse_endblock, ast_ctx(self), merge))
2236 /* Now enter the merge block */
2237 func->curblock = merge;
2240 /* we create the if here, that way all blocks are ordered :)
2242 if (!ir_block_create_if(cond, ast_ctx(self), condval,
2243 (ontrue ? ontrue : merge),
2244 (onfalse ? onfalse : merge)))
2252 bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_value **out)
2254 ast_expression_codegen *cgen;
2257 ir_value *trueval, *falseval;
2260 ir_block *cond = func->curblock;
2261 ir_block *cond_out = NULL;
2262 ir_block *ontrue, *ontrue_out = NULL;
2263 ir_block *onfalse, *onfalse_out = NULL;
2266 /* Ternary can never create an lvalue... */
2270 /* In theory it shouldn't be possible to pass through a node twice, but
2271 * in case we add any kind of optimization pass for the AST itself, it
2272 * may still happen, thus we remember a created ir_value and simply return one
2273 * if it already exists.
2275 if (self->expression.outr) {
2276 *out = self->expression.outr;
2280 /* In the following, contraty to ast_ifthen, we assume both paths exist. */
2282 /* generate the condition */
2283 func->curblock = cond;
2284 cgen = self->cond->expression.codegen;
2285 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2287 cond_out = func->curblock;
2289 /* create on-true block */
2290 ontrue = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_T"));
2295 /* enter the block */
2296 func->curblock = ontrue;
2299 cgen = self->on_true->expression.codegen;
2300 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &trueval))
2303 ontrue_out = func->curblock;
2306 /* create on-false block */
2307 onfalse = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_F"));
2312 /* enter the block */
2313 func->curblock = onfalse;
2316 cgen = self->on_false->expression.codegen;
2317 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &falseval))
2320 onfalse_out = func->curblock;
2323 /* create merge block */
2324 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_out"));
2327 /* jump to merge block */
2328 if (!ir_block_create_jump(ontrue_out, ast_ctx(self), merge))
2330 if (!ir_block_create_jump(onfalse_out, ast_ctx(self), merge))
2333 /* create if instruction */
2334 if (!ir_block_create_if(cond_out, ast_ctx(self), condval, ontrue, onfalse))
2337 /* Now enter the merge block */
2338 func->curblock = merge;
2340 /* Here, now, we need a PHI node
2341 * but first some sanity checking...
2343 if (trueval->vtype != falseval->vtype) {
2344 /* error("ternary with different types on the two sides"); */
2349 phi = ir_block_create_phi(merge, ast_ctx(self), ast_function_label(func, "phi"), trueval->vtype);
2352 ir_phi_add(phi, ontrue_out, trueval);
2353 ir_phi_add(phi, onfalse_out, falseval);
2355 self->expression.outr = ir_phi_value(phi);
2356 *out = self->expression.outr;
2358 codegen_output_type(self, *out);
2363 bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value **out)
2365 ast_expression_codegen *cgen;
2367 ir_value *dummy = NULL;
2368 ir_value *precond = NULL;
2369 ir_value *postcond = NULL;
2371 /* Since we insert some jumps "late" so we have blocks
2372 * ordered "nicely", we need to keep track of the actual end-blocks
2373 * of expressions to add the jumps to.
2375 ir_block *bbody = NULL, *end_bbody = NULL;
2376 ir_block *bprecond = NULL, *end_bprecond = NULL;
2377 ir_block *bpostcond = NULL, *end_bpostcond = NULL;
2378 ir_block *bincrement = NULL, *end_bincrement = NULL;
2379 ir_block *bout = NULL, *bin = NULL;
2381 /* let's at least move the outgoing block to the end */
2384 /* 'break' and 'continue' need to be able to find the right blocks */
2385 ir_block *bcontinue = NULL;
2386 ir_block *bbreak = NULL;
2388 ir_block *old_bcontinue = NULL;
2389 ir_block *old_bbreak = NULL;
2391 ir_block *tmpblock = NULL;
2396 if (self->expression.outr) {
2397 compile_error(ast_ctx(self), "internal error: ast_loop cannot be reused, it bears no result!");
2400 self->expression.outr = (ir_value*)1;
2403 * Should we ever need some kind of block ordering, better make this function
2404 * move blocks around than write a block ordering algorithm later... after all
2405 * the ast and ir should work together, not against each other.
2408 /* initexpr doesn't get its own block, it's pointless, it could create more blocks
2409 * anyway if for example it contains a ternary.
2413 cgen = self->initexpr->expression.codegen;
2414 if (!(*cgen)((ast_expression*)(self->initexpr), func, false, &dummy))
2418 /* Store the block from which we enter this chaos */
2419 bin = func->curblock;
2421 /* The pre-loop condition needs its own block since we
2422 * need to be able to jump to the start of that expression.
2426 bprecond = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "pre_loop_cond"));
2430 /* the pre-loop-condition the least important place to 'continue' at */
2431 bcontinue = bprecond;
2434 func->curblock = bprecond;
2437 cgen = self->precond->expression.codegen;
2438 if (!(*cgen)((ast_expression*)(self->precond), func, false, &precond))
2441 end_bprecond = func->curblock;
2443 bprecond = end_bprecond = NULL;
2446 /* Now the next blocks won't be ordered nicely, but we need to
2447 * generate them this early for 'break' and 'continue'.
2449 if (self->increment) {
2450 bincrement = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "loop_increment"));
2453 bcontinue = bincrement; /* increment comes before the pre-loop-condition */
2455 bincrement = end_bincrement = NULL;
2458 if (self->postcond) {
2459 bpostcond = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "post_loop_cond"));
2462 bcontinue = bpostcond; /* postcond comes before the increment */
2464 bpostcond = end_bpostcond = NULL;
2467 bout_id = vec_size(func->ir_func->blocks);
2468 bout = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "after_loop"));
2473 /* The loop body... */
2474 /* if (self->body) */
2476 bbody = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "loop_body"));
2481 func->curblock = bbody;
2483 old_bbreak = func->breakblock;
2484 old_bcontinue = func->continueblock;
2485 func->breakblock = bbreak;
2486 func->continueblock = bcontinue;
2487 if (!func->continueblock)
2488 func->continueblock = bbody;
2492 cgen = self->body->expression.codegen;
2493 if (!(*cgen)((ast_expression*)(self->body), func, false, &dummy))
2497 end_bbody = func->curblock;
2498 func->breakblock = old_bbreak;
2499 func->continueblock = old_bcontinue;
2502 /* post-loop-condition */
2506 func->curblock = bpostcond;
2509 cgen = self->postcond->expression.codegen;
2510 if (!(*cgen)((ast_expression*)(self->postcond), func, false, &postcond))
2513 end_bpostcond = func->curblock;
2516 /* The incrementor */
2517 if (self->increment)
2520 func->curblock = bincrement;
2523 cgen = self->increment->expression.codegen;
2524 if (!(*cgen)((ast_expression*)(self->increment), func, false, &dummy))
2527 end_bincrement = func->curblock;
2530 /* In any case now, we continue from the outgoing block */
2531 func->curblock = bout;
2533 /* Now all blocks are in place */
2534 /* From 'bin' we jump to whatever comes first */
2535 if (bprecond) tmpblock = bprecond;
2536 else if (bbody) tmpblock = bbody;
2537 else if (bpostcond) tmpblock = bpostcond;
2538 else tmpblock = bout;
2539 if (!ir_block_create_jump(bin, ast_ctx(self), tmpblock))
2545 ir_block *ontrue, *onfalse;
2546 if (bbody) ontrue = bbody;
2547 else if (bincrement) ontrue = bincrement;
2548 else if (bpostcond) ontrue = bpostcond;
2549 else ontrue = bprecond;
2551 if (self->pre_not) {
2556 if (!ir_block_create_if(end_bprecond, ast_ctx(self), precond, ontrue, onfalse))
2563 if (bincrement) tmpblock = bincrement;
2564 else if (bpostcond) tmpblock = bpostcond;
2565 else if (bprecond) tmpblock = bprecond;
2566 else tmpblock = bbody;
2567 if (!end_bbody->final && !ir_block_create_jump(end_bbody, ast_ctx(self), tmpblock))
2571 /* from increment */
2574 if (bpostcond) tmpblock = bpostcond;
2575 else if (bprecond) tmpblock = bprecond;
2576 else if (bbody) tmpblock = bbody;
2577 else tmpblock = bout;
2578 if (!ir_block_create_jump(end_bincrement, ast_ctx(self), tmpblock))
2585 ir_block *ontrue, *onfalse;
2586 if (bprecond) ontrue = bprecond;
2587 else if (bbody) ontrue = bbody;
2588 else if (bincrement) ontrue = bincrement;
2589 else ontrue = bpostcond;
2591 if (self->post_not) {
2596 if (!ir_block_create_if(end_bpostcond, ast_ctx(self), postcond, ontrue, onfalse))
2600 /* Move 'bout' to the end */
2601 vec_remove(func->ir_func->blocks, bout_id, 1);
2602 vec_push(func->ir_func->blocks, bout);
2607 bool ast_breakcont_codegen(ast_breakcont *self, ast_function *func, bool lvalue, ir_value **out)
2614 compile_error(ast_ctx(self), "break/continue expression is not an l-value");
2618 if (self->expression.outr) {
2619 compile_error(ast_ctx(self), "internal error: ast_breakcont cannot be reused!");
2622 self->expression.outr = (ir_value*)1;
2624 if (self->is_continue)
2625 target = func->continueblock;
2627 target = func->breakblock;
2630 compile_error(ast_ctx(self), "%s is lacking a target block", (self->is_continue ? "continue" : "break"));
2634 if (!ir_block_create_jump(func->curblock, ast_ctx(self), target))
2639 bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_value **out)
2641 ast_expression_codegen *cgen;
2643 ast_switch_case *def_case = NULL;
2644 ir_block *def_bfall = NULL;
2645 ir_block *def_bfall_to = NULL;
2646 bool set_def_bfall_to = false;
2648 ir_value *dummy = NULL;
2649 ir_value *irop = NULL;
2650 ir_block *old_break = NULL;
2651 ir_block *bout = NULL;
2652 ir_block *bfall = NULL;
2660 compile_error(ast_ctx(self), "switch expression is not an l-value");
2664 if (self->expression.outr) {
2665 compile_error(ast_ctx(self), "internal error: ast_switch cannot be reused!");
2668 self->expression.outr = (ir_value*)1;
2673 cgen = self->operand->expression.codegen;
2674 if (!(*cgen)((ast_expression*)(self->operand), func, false, &irop))
2677 if (!vec_size(self->cases))
2680 cmpinstr = type_eq_instr[irop->vtype];
2681 if (cmpinstr >= AINSTR_END) {
2682 ast_type_to_string(self->operand, typestr, sizeof(typestr));
2683 compile_error(ast_ctx(self), "invalid type to perform a switch on: %s", typestr);
2687 bout_id = vec_size(func->ir_func->blocks);
2688 bout = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "after_switch"));
2692 /* setup the break block */
2693 old_break = func->breakblock;
2694 func->breakblock = bout;
2696 /* Now create all cases */
2697 for (c = 0; c < vec_size(self->cases); ++c) {
2698 ir_value *cond, *val;
2699 ir_block *bcase, *bnot;
2702 ast_switch_case *swcase = &self->cases[c];
2704 if (swcase->value) {
2705 /* A regular case */
2706 /* generate the condition operand */
2707 cgen = swcase->value->expression.codegen;
2708 if (!(*cgen)((ast_expression*)(swcase->value), func, false, &val))
2710 /* generate the condition */
2711 cond = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "switch_eq"), cmpinstr, irop, val);
2715 bcase = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "case"));
2716 bnot_id = vec_size(func->ir_func->blocks);
2717 bnot = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "not_case"));
2718 if (!bcase || !bnot)
2720 if (set_def_bfall_to) {
2721 set_def_bfall_to = false;
2722 def_bfall_to = bcase;
2724 if (!ir_block_create_if(func->curblock, ast_ctx(self), cond, bcase, bnot))
2727 /* Make the previous case-end fall through */
2728 if (bfall && !bfall->final) {
2729 if (!ir_block_create_jump(bfall, ast_ctx(self), bcase))
2733 /* enter the case */
2734 func->curblock = bcase;
2735 cgen = swcase->code->expression.codegen;
2736 if (!(*cgen)((ast_expression*)swcase->code, func, false, &dummy))
2739 /* remember this block to fall through from */
2740 bfall = func->curblock;
2742 /* enter the else and move it down */
2743 func->curblock = bnot;
2744 vec_remove(func->ir_func->blocks, bnot_id, 1);
2745 vec_push(func->ir_func->blocks, bnot);
2747 /* The default case */
2748 /* Remember where to fall through from: */
2751 /* remember which case it was */
2753 /* And the next case will be remembered */
2754 set_def_bfall_to = true;
2758 /* Jump from the last bnot to bout */
2759 if (bfall && !bfall->final && !ir_block_create_jump(bfall, ast_ctx(self), bout)) {
2761 astwarning(ast_ctx(bfall), WARN_???, "missing break after last case");
2766 /* If there was a default case, put it down here */
2770 /* No need to create an extra block */
2771 bcase = func->curblock;
2773 /* Insert the fallthrough jump */
2774 if (def_bfall && !def_bfall->final) {
2775 if (!ir_block_create_jump(def_bfall, ast_ctx(self), bcase))
2779 /* Now generate the default code */
2780 cgen = def_case->code->expression.codegen;
2781 if (!(*cgen)((ast_expression*)def_case->code, func, false, &dummy))
2784 /* see if we need to fall through */
2785 if (def_bfall_to && !func->curblock->final)
2787 if (!ir_block_create_jump(func->curblock, ast_ctx(self), def_bfall_to))
2792 /* Jump from the last bnot to bout */
2793 if (!func->curblock->final && !ir_block_create_jump(func->curblock, ast_ctx(self), bout))
2795 /* enter the outgoing block */
2796 func->curblock = bout;
2798 /* restore the break block */
2799 func->breakblock = old_break;
2801 /* Move 'bout' to the end, it's nicer */
2802 vec_remove(func->ir_func->blocks, bout_id, 1);
2803 vec_push(func->ir_func->blocks, bout);
2808 bool ast_label_codegen(ast_label *self, ast_function *func, bool lvalue, ir_value **out)
2815 compile_error(ast_ctx(self), "internal error: ast_label cannot be an lvalue");
2819 /* simply create a new block and jump to it */
2820 self->irblock = ir_function_create_block(ast_ctx(self), func->ir_func, self->name);
2821 if (!self->irblock) {
2822 compile_error(ast_ctx(self), "failed to allocate label block `%s`", self->name);
2825 if (!func->curblock->final) {
2826 if (!ir_block_create_jump(func->curblock, ast_ctx(self), self->irblock))
2830 /* enter the new block */
2831 func->curblock = self->irblock;
2833 /* Generate all the leftover gotos */
2834 for (i = 0; i < vec_size(self->gotos); ++i) {
2835 if (!ast_goto_codegen(self->gotos[i], func, false, &dummy))
2842 bool ast_goto_codegen(ast_goto *self, ast_function *func, bool lvalue, ir_value **out)
2846 compile_error(ast_ctx(self), "internal error: ast_goto cannot be an lvalue");
2850 if (self->target->irblock) {
2851 if (self->irblock_from) {
2852 /* we already tried once, this is the callback */
2853 self->irblock_from->final = false;
2854 if (!ir_block_create_jump(self->irblock_from, ast_ctx(self), self->target->irblock)) {
2855 compile_error(ast_ctx(self), "failed to generate goto to `%s`", self->name);
2861 if (!ir_block_create_jump(func->curblock, ast_ctx(self), self->target->irblock)) {
2862 compile_error(ast_ctx(self), "failed to generate goto to `%s`", self->name);
2869 /* the target has not yet been created...
2870 * close this block in a sneaky way:
2872 func->curblock->final = true;
2873 self->irblock_from = func->curblock;
2874 ast_label_register_goto(self->target, self);
2880 bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value **out)
2882 ast_expression_codegen *cgen;
2884 ir_instr *callinstr;
2887 ir_value *funval = NULL;
2889 /* return values are never lvalues */
2891 compile_error(ast_ctx(self), "not an l-value (function call)");
2895 if (self->expression.outr) {
2896 *out = self->expression.outr;
2900 cgen = self->func->expression.codegen;
2901 if (!(*cgen)((ast_expression*)(self->func), func, false, &funval))
2909 for (i = 0; i < vec_size(self->params); ++i)
2912 ast_expression *expr = self->params[i];
2914 cgen = expr->expression.codegen;
2915 if (!(*cgen)(expr, func, false, ¶m))
2919 vec_push(params, param);
2922 callinstr = ir_block_create_call(func->curblock, ast_ctx(self),
2923 ast_function_label(func, "call"),
2924 funval, !!(self->func->expression.flags & AST_FLAG_NORETURN));
2928 for (i = 0; i < vec_size(params); ++i) {
2929 ir_call_param(callinstr, params[i]);
2932 *out = ir_call_value(callinstr);
2933 self->expression.outr = *out;
2935 codegen_output_type(self, *out);