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;
76 self->expression.count = 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->variadic = fromex->variadic;
112 for (i = 0; i < vec_size(fromex->params); ++i) {
113 ast_value *v = ast_value_copy(fromex->params[i]);
115 ast_value_delete(cp);
118 vec_push(selfex->params, v);
123 bool ast_type_adopt_impl(ast_expression *self, const ast_expression *other)
126 const ast_expression_common *fromex;
127 ast_expression_common *selfex;
128 self->expression.vtype = other->expression.vtype;
129 if (other->expression.next) {
130 self->expression.next = (ast_expression*)ast_type_copy(ast_ctx(self), other->expression.next);
131 if (!self->expression.next)
134 fromex = &other->expression;
135 selfex = &self->expression;
136 selfex->variadic = fromex->variadic;
137 for (i = 0; i < vec_size(fromex->params); ++i) {
138 ast_value *v = ast_value_copy(fromex->params[i]);
141 vec_push(selfex->params, v);
146 static ast_expression* ast_shallow_type(lex_ctx ctx, int vtype)
148 ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
149 ast_expression_init(self, NULL);
150 self->expression.codegen = NULL;
151 self->expression.next = NULL;
152 self->expression.vtype = vtype;
156 ast_expression* ast_type_copy(lex_ctx ctx, const ast_expression *ex)
159 const ast_expression_common *fromex;
160 ast_expression_common *selfex;
166 ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
167 ast_expression_init(self, NULL);
169 fromex = &ex->expression;
170 selfex = &self->expression;
172 /* This may never be codegen()d */
173 selfex->codegen = NULL;
175 selfex->vtype = fromex->vtype;
178 selfex->next = ast_type_copy(ctx, fromex->next);
180 ast_expression_delete_full(self);
187 selfex->variadic = fromex->variadic;
188 for (i = 0; i < vec_size(fromex->params); ++i) {
189 ast_value *v = ast_value_copy(fromex->params[i]);
191 ast_expression_delete_full(self);
194 vec_push(selfex->params, v);
201 bool ast_compare_type(ast_expression *a, ast_expression *b)
203 if (a->expression.vtype != b->expression.vtype)
205 if (!a->expression.next != !b->expression.next)
207 if (vec_size(a->expression.params) != vec_size(b->expression.params))
209 if (a->expression.variadic != b->expression.variadic)
211 if (vec_size(a->expression.params)) {
213 for (i = 0; i < vec_size(a->expression.params); ++i) {
214 if (!ast_compare_type((ast_expression*)a->expression.params[i],
215 (ast_expression*)b->expression.params[i]))
219 if (a->expression.next)
220 return ast_compare_type(a->expression.next, b->expression.next);
224 static size_t ast_type_to_string_impl(ast_expression *e, char *buf, size_t bufsize, size_t pos)
231 if (pos + 6 >= bufsize)
233 strcpy(buf + pos, "(null)");
237 if (pos + 1 >= bufsize)
240 switch (e->expression.vtype) {
242 strcpy(buf + pos, "(variant)");
247 return ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
250 if (pos + 3 >= bufsize)
254 pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
255 if (pos + 1 >= bufsize)
261 pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
262 if (pos + 2 >= bufsize)
264 if (!vec_size(e->expression.params)) {
270 pos = ast_type_to_string_impl((ast_expression*)(e->expression.params[0]), buf, bufsize, pos);
271 for (i = 1; i < vec_size(e->expression.params); ++i) {
272 if (pos + 2 >= bufsize)
276 pos = ast_type_to_string_impl((ast_expression*)(e->expression.params[i]), buf, bufsize, pos);
278 if (pos + 1 >= bufsize)
284 pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
285 if (pos + 1 >= bufsize)
288 pos += snprintf(buf + pos, bufsize - pos - 1, "%i", (int)e->expression.count);
289 if (pos + 1 >= bufsize)
295 typestr = type_name[e->expression.vtype];
296 typelen = strlen(typestr);
297 if (pos + typelen >= bufsize)
299 strcpy(buf + pos, typestr);
300 return pos + typelen;
304 buf[bufsize-3] = '.';
305 buf[bufsize-2] = '.';
306 buf[bufsize-1] = '.';
310 void ast_type_to_string(ast_expression *e, char *buf, size_t bufsize)
312 size_t pos = ast_type_to_string_impl(e, buf, bufsize-1, 0);
316 ast_value* ast_value_new(lex_ctx ctx, const char *name, int t)
318 ast_instantiate(ast_value, ctx, ast_value_delete);
319 ast_expression_init((ast_expression*)self,
320 (ast_expression_codegen*)&ast_value_codegen);
321 self->expression.node.keep = true; /* keep */
323 self->name = name ? util_strdup(name) : NULL;
324 self->expression.vtype = t;
325 self->expression.next = NULL;
326 self->isfield = false;
328 self->hasvalue = false;
330 memset(&self->constval, 0, sizeof(self->constval));
333 self->ir_values = NULL;
334 self->ir_value_count = 0;
342 void ast_value_delete(ast_value* self)
345 mem_d((void*)self->name);
346 if (self->hasvalue) {
347 switch (self->expression.vtype)
350 mem_d((void*)self->constval.vstring);
353 /* unlink us from the function node */
354 self->constval.vfunc->vtype = NULL;
356 /* NOTE: delete function? currently collected in
357 * the parser structure
364 mem_d(self->ir_values);
365 ast_expression_delete((ast_expression*)self);
369 void ast_value_params_add(ast_value *self, ast_value *p)
371 vec_push(self->expression.params, p);
374 bool ast_value_set_name(ast_value *self, const char *name)
377 mem_d((void*)self->name);
378 self->name = util_strdup(name);
382 ast_binary* ast_binary_new(lex_ctx ctx, int op,
383 ast_expression* left, ast_expression* right)
385 ast_instantiate(ast_binary, ctx, ast_binary_delete);
386 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binary_codegen);
392 ast_propagate_effects(self, left);
393 ast_propagate_effects(self, right);
395 if (op >= INSTR_EQ_F && op <= INSTR_GT)
396 self->expression.vtype = TYPE_FLOAT;
397 else if (op == INSTR_AND || op == INSTR_OR ||
398 op == INSTR_BITAND || op == INSTR_BITOR)
399 self->expression.vtype = TYPE_FLOAT;
400 else if (op == INSTR_MUL_VF || op == INSTR_MUL_FV)
401 self->expression.vtype = TYPE_VECTOR;
402 else if (op == INSTR_MUL_V)
403 self->expression.vtype = TYPE_FLOAT;
405 self->expression.vtype = left->expression.vtype;
410 void ast_binary_delete(ast_binary *self)
412 ast_unref(self->left);
413 ast_unref(self->right);
414 ast_expression_delete((ast_expression*)self);
418 ast_binstore* ast_binstore_new(lex_ctx ctx, int storop, int op,
419 ast_expression* left, ast_expression* right)
421 ast_instantiate(ast_binstore, ctx, ast_binstore_delete);
422 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binstore_codegen);
424 ast_side_effects(self) = true;
426 self->opstore = storop;
429 self->source = right;
431 self->keep_dest = false;
433 if (!ast_type_adopt(self, left)) {
441 void ast_binstore_delete(ast_binstore *self)
443 if (!self->keep_dest)
444 ast_unref(self->dest);
445 ast_unref(self->source);
446 ast_expression_delete((ast_expression*)self);
450 ast_unary* ast_unary_new(lex_ctx ctx, int op,
451 ast_expression *expr)
453 ast_instantiate(ast_unary, ctx, ast_unary_delete);
454 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_unary_codegen);
457 self->operand = expr;
459 ast_propagate_effects(self, expr);
461 if (op >= INSTR_NOT_F && op <= INSTR_NOT_FNC) {
462 self->expression.vtype = TYPE_FLOAT;
464 compile_error(ctx, "cannot determine type of unary operation %s", asm_instr[op].m);
469 void ast_unary_delete(ast_unary *self)
471 ast_unref(self->operand);
472 ast_expression_delete((ast_expression*)self);
476 ast_return* ast_return_new(lex_ctx ctx, ast_expression *expr)
478 ast_instantiate(ast_return, ctx, ast_return_delete);
479 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_return_codegen);
481 self->operand = expr;
484 ast_propagate_effects(self, expr);
489 void ast_return_delete(ast_return *self)
492 ast_unref(self->operand);
493 ast_expression_delete((ast_expression*)self);
497 ast_entfield* ast_entfield_new(lex_ctx ctx, ast_expression *entity, ast_expression *field)
499 if (field->expression.vtype != TYPE_FIELD) {
500 compile_error(ctx, "ast_entfield_new with expression not of type field");
503 return ast_entfield_new_force(ctx, entity, field, field->expression.next);
506 ast_entfield* ast_entfield_new_force(lex_ctx ctx, ast_expression *entity, ast_expression *field, const ast_expression *outtype)
508 ast_instantiate(ast_entfield, ctx, ast_entfield_delete);
512 /* Error: field has no type... */
516 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_entfield_codegen);
518 self->entity = entity;
520 ast_propagate_effects(self, entity);
521 ast_propagate_effects(self, field);
523 if (!ast_type_adopt(self, outtype)) {
524 ast_entfield_delete(self);
531 void ast_entfield_delete(ast_entfield *self)
533 ast_unref(self->entity);
534 ast_unref(self->field);
535 ast_expression_delete((ast_expression*)self);
539 ast_member* ast_member_new(lex_ctx ctx, ast_expression *owner, unsigned int field, const char *name)
541 ast_instantiate(ast_member, ctx, ast_member_delete);
547 if (owner->expression.vtype != TYPE_VECTOR &&
548 owner->expression.vtype != TYPE_FIELD) {
549 compile_error(ctx, "member-access on an invalid owner of type %s", type_name[owner->expression.vtype]);
554 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_member_codegen);
555 self->expression.node.keep = true; /* keep */
557 if (owner->expression.vtype == TYPE_VECTOR) {
558 self->expression.vtype = TYPE_FLOAT;
559 self->expression.next = NULL;
561 self->expression.vtype = TYPE_FIELD;
562 self->expression.next = ast_shallow_type(ctx, TYPE_FLOAT);
566 ast_propagate_effects(self, owner);
570 self->name = util_strdup(name);
577 void ast_member_delete(ast_member *self)
579 /* The owner is always an ast_value, which has .keep=true,
580 * also: ast_members are usually deleted after the owner, thus
581 * this will cause invalid access
582 ast_unref(self->owner);
583 * once we allow (expression).x to access a vector-member, we need
584 * to change this: preferably by creating an alternate ast node for this
585 * purpose that is not garbage-collected.
587 ast_expression_delete((ast_expression*)self);
591 ast_array_index* ast_array_index_new(lex_ctx ctx, ast_expression *array, ast_expression *index)
593 ast_expression *outtype;
594 ast_instantiate(ast_array_index, ctx, ast_array_index_delete);
596 outtype = array->expression.next;
599 /* Error: field has no type... */
603 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_array_index_codegen);
607 ast_propagate_effects(self, array);
608 ast_propagate_effects(self, index);
610 if (!ast_type_adopt(self, outtype)) {
611 ast_array_index_delete(self);
614 if (array->expression.vtype == TYPE_FIELD && outtype->expression.vtype == TYPE_ARRAY) {
615 if (self->expression.vtype != TYPE_ARRAY) {
616 compile_error(ast_ctx(self), "array_index node on type");
617 ast_array_index_delete(self);
620 self->array = outtype;
621 self->expression.vtype = TYPE_FIELD;
627 void ast_array_index_delete(ast_array_index *self)
629 ast_unref(self->array);
630 ast_unref(self->index);
631 ast_expression_delete((ast_expression*)self);
635 ast_ifthen* ast_ifthen_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
637 ast_instantiate(ast_ifthen, ctx, ast_ifthen_delete);
638 if (!ontrue && !onfalse) {
639 /* because it is invalid */
643 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ifthen_codegen);
646 self->on_true = ontrue;
647 self->on_false = onfalse;
648 ast_propagate_effects(self, cond);
650 ast_propagate_effects(self, ontrue);
652 ast_propagate_effects(self, onfalse);
657 void ast_ifthen_delete(ast_ifthen *self)
659 ast_unref(self->cond);
661 ast_unref(self->on_true);
663 ast_unref(self->on_false);
664 ast_expression_delete((ast_expression*)self);
668 ast_ternary* ast_ternary_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
670 ast_instantiate(ast_ternary, ctx, ast_ternary_delete);
671 /* This time NEITHER must be NULL */
672 if (!ontrue || !onfalse) {
676 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ternary_codegen);
679 self->on_true = ontrue;
680 self->on_false = onfalse;
681 ast_propagate_effects(self, cond);
682 ast_propagate_effects(self, ontrue);
683 ast_propagate_effects(self, onfalse);
685 if (!ast_type_adopt(self, ontrue)) {
686 ast_ternary_delete(self);
693 void ast_ternary_delete(ast_ternary *self)
695 ast_unref(self->cond);
696 ast_unref(self->on_true);
697 ast_unref(self->on_false);
698 ast_expression_delete((ast_expression*)self);
702 ast_loop* ast_loop_new(lex_ctx ctx,
703 ast_expression *initexpr,
704 ast_expression *precond,
705 ast_expression *postcond,
706 ast_expression *increment,
707 ast_expression *body)
709 ast_instantiate(ast_loop, ctx, ast_loop_delete);
710 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_loop_codegen);
712 self->initexpr = initexpr;
713 self->precond = precond;
714 self->postcond = postcond;
715 self->increment = increment;
719 ast_propagate_effects(self, initexpr);
721 ast_propagate_effects(self, precond);
723 ast_propagate_effects(self, postcond);
725 ast_propagate_effects(self, increment);
727 ast_propagate_effects(self, body);
732 void ast_loop_delete(ast_loop *self)
735 ast_unref(self->initexpr);
737 ast_unref(self->precond);
739 ast_unref(self->postcond);
741 ast_unref(self->increment);
743 ast_unref(self->body);
744 ast_expression_delete((ast_expression*)self);
748 ast_breakcont* ast_breakcont_new(lex_ctx ctx, bool iscont)
750 ast_instantiate(ast_breakcont, ctx, ast_breakcont_delete);
751 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_breakcont_codegen);
753 self->is_continue = iscont;
758 void ast_breakcont_delete(ast_breakcont *self)
760 ast_expression_delete((ast_expression*)self);
764 ast_switch* ast_switch_new(lex_ctx ctx, ast_expression *op)
766 ast_instantiate(ast_switch, ctx, ast_switch_delete);
767 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_switch_codegen);
772 ast_propagate_effects(self, op);
777 void ast_switch_delete(ast_switch *self)
780 ast_unref(self->operand);
782 for (i = 0; i < vec_size(self->cases); ++i) {
783 if (self->cases[i].value)
784 ast_unref(self->cases[i].value);
785 ast_unref(self->cases[i].code);
787 vec_free(self->cases);
789 ast_expression_delete((ast_expression*)self);
793 ast_label* ast_label_new(lex_ctx ctx, const char *name)
795 ast_instantiate(ast_label, ctx, ast_label_delete);
796 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_label_codegen);
798 self->name = util_strdup(name);
799 self->irblock = NULL;
805 void ast_label_delete(ast_label *self)
807 mem_d((void*)self->name);
808 vec_free(self->gotos);
809 ast_expression_delete((ast_expression*)self);
813 void ast_label_register_goto(ast_label *self, ast_goto *g)
815 vec_push(self->gotos, g);
818 ast_goto* ast_goto_new(lex_ctx ctx, const char *name)
820 ast_instantiate(ast_goto, ctx, ast_goto_delete);
821 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_goto_codegen);
823 self->name = util_strdup(name);
825 self->irblock_from = NULL;
830 void ast_goto_delete(ast_goto *self)
832 mem_d((void*)self->name);
833 ast_expression_delete((ast_expression*)self);
837 void ast_goto_set_label(ast_goto *self, ast_label *label)
839 self->target = label;
842 ast_call* ast_call_new(lex_ctx ctx,
843 ast_expression *funcexpr)
845 ast_instantiate(ast_call, ctx, ast_call_delete);
846 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_call_codegen);
848 ast_side_effects(self) = true;
851 self->func = funcexpr;
853 ast_type_adopt(self, funcexpr->expression.next);
858 void ast_call_delete(ast_call *self)
861 for (i = 0; i < vec_size(self->params); ++i)
862 ast_unref(self->params[i]);
863 vec_free(self->params);
866 ast_unref(self->func);
868 ast_expression_delete((ast_expression*)self);
872 bool ast_call_check_types(ast_call *self)
876 const ast_expression *func = self->func;
877 size_t count = vec_size(self->params);
878 if (count > vec_size(func->expression.params))
879 count = vec_size(func->expression.params);
881 for (i = 0; i < count; ++i) {
882 if (!ast_compare_type(self->params[i], (ast_expression*)(func->expression.params[i]))) {
885 ast_type_to_string(self->params[i], tgot, sizeof(tgot));
886 ast_type_to_string((ast_expression*)func->expression.params[i], texp, sizeof(texp));
887 compile_error(ast_ctx(self), "invalid type for parameter %u in function call: expected %s, got %s",
888 (unsigned int)(i+1), texp, tgot);
889 /* we don't immediately return */
896 ast_store* ast_store_new(lex_ctx ctx, int op,
897 ast_expression *dest, ast_expression *source)
899 ast_instantiate(ast_store, ctx, ast_store_delete);
900 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_store_codegen);
902 ast_side_effects(self) = true;
906 self->source = source;
908 if (!ast_type_adopt(self, dest)) {
916 void ast_store_delete(ast_store *self)
918 ast_unref(self->dest);
919 ast_unref(self->source);
920 ast_expression_delete((ast_expression*)self);
924 ast_block* ast_block_new(lex_ctx ctx)
926 ast_instantiate(ast_block, ctx, ast_block_delete);
927 ast_expression_init((ast_expression*)self,
928 (ast_expression_codegen*)&ast_block_codegen);
932 self->collect = NULL;
937 bool ast_block_add_expr(ast_block *self, ast_expression *e)
939 ast_propagate_effects(self, e);
940 vec_push(self->exprs, e);
941 if (self->expression.next) {
942 ast_delete(self->expression.next);
943 self->expression.next = NULL;
945 if (!ast_type_adopt(self, e)) {
946 compile_error(ast_ctx(self), "internal error: failed to adopt type");
952 void ast_block_collect(ast_block *self, ast_expression *expr)
954 vec_push(self->collect, expr);
955 expr->expression.node.keep = true;
958 void ast_block_delete(ast_block *self)
961 for (i = 0; i < vec_size(self->exprs); ++i)
962 ast_unref(self->exprs[i]);
963 vec_free(self->exprs);
964 for (i = 0; i < vec_size(self->locals); ++i)
965 ast_delete(self->locals[i]);
966 vec_free(self->locals);
967 for (i = 0; i < vec_size(self->collect); ++i)
968 ast_delete(self->collect[i]);
969 vec_free(self->collect);
970 ast_expression_delete((ast_expression*)self);
974 bool ast_block_set_type(ast_block *self, ast_expression *from)
976 if (self->expression.next)
977 ast_delete(self->expression.next);
978 if (!ast_type_adopt(self, from))
983 ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype)
985 ast_instantiate(ast_function, ctx, ast_function_delete);
989 vtype->expression.vtype != TYPE_FUNCTION)
991 compile_error(ast_ctx(self), "internal error: ast_function_new condition %i %i type=%i (probably 2 bodies?)",
993 (int)vtype->hasvalue,
994 vtype->expression.vtype);
1000 self->name = name ? util_strdup(name) : NULL;
1001 self->blocks = NULL;
1003 self->labelcount = 0;
1006 self->ir_func = NULL;
1007 self->curblock = NULL;
1009 self->breakblock = NULL;
1010 self->continueblock = NULL;
1012 vtype->hasvalue = true;
1013 vtype->constval.vfunc = self;
1018 void ast_function_delete(ast_function *self)
1022 mem_d((void*)self->name);
1024 /* ast_value_delete(self->vtype); */
1025 self->vtype->hasvalue = false;
1026 self->vtype->constval.vfunc = NULL;
1027 /* We use unref - if it was stored in a global table it is supposed
1028 * to be deleted from *there*
1030 ast_unref(self->vtype);
1032 for (i = 0; i < vec_size(self->blocks); ++i)
1033 ast_delete(self->blocks[i]);
1034 vec_free(self->blocks);
1038 const char* ast_function_label(ast_function *self, const char *prefix)
1044 if (!opts.dump && !opts.dumpfin)
1047 id = (self->labelcount++);
1048 len = strlen(prefix);
1050 from = self->labelbuf + sizeof(self->labelbuf)-1;
1053 unsigned int digit = id % 10;
1054 *from = digit + '0';
1057 memcpy(from - len, prefix, len);
1061 /*********************************************************************/
1063 * by convention you must never pass NULL to the 'ir_value **out'
1064 * parameter. If you really don't care about the output, pass a dummy.
1065 * But I can't imagine a pituation where the output is truly unnecessary.
1068 void _ast_codegen_output_type(ast_expression_common *self, ir_value *out)
1070 if (out->vtype == TYPE_FIELD)
1071 out->fieldtype = self->next->expression.vtype;
1072 if (out->vtype == TYPE_FUNCTION)
1073 out->outtype = self->next->expression.vtype;
1076 #define codegen_output_type(a,o) (_ast_codegen_output_type(&((a)->expression),(o)))
1077 #define codegen_output_type_expr(a,o) (_ast_codegen_output_type(a,(o)))
1079 bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out)
1083 /* NOTE: This is the codegen for a variable used in an expression.
1084 * It is not the codegen to generate the value. For this purpose,
1085 * ast_local_codegen and ast_global_codegen are to be used before this
1086 * is executed. ast_function_codegen should take care of its locals,
1087 * and the ast-user should take care of ast_global_codegen to be used
1088 * on all the globals.
1091 char typename[1024];
1092 ast_type_to_string((ast_expression*)self, typename, sizeof(typename));
1093 compile_error(ast_ctx(self), "ast_value used before generated %s %s", typename, self->name);
1100 bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
1104 if (self->hasvalue && self->expression.vtype == TYPE_FUNCTION)
1106 ir_function *func = ir_builder_create_function(ir, self->name, self->expression.next->expression.vtype);
1109 func->context = ast_ctx(self);
1110 func->value->context = ast_ctx(self);
1112 self->constval.vfunc->ir_func = func;
1113 self->ir_v = func->value;
1114 /* The function is filled later on ast_function_codegen... */
1118 if (isfield && self->expression.vtype == TYPE_FIELD) {
1119 ast_expression *fieldtype = self->expression.next;
1121 if (self->hasvalue) {
1122 compile_error(ast_ctx(self), "TODO: constant field pointers with value");
1126 if (fieldtype->expression.vtype == TYPE_ARRAY) {
1131 ast_expression_common *elemtype;
1133 ast_value *array = (ast_value*)fieldtype;
1135 if (!ast_istype(fieldtype, ast_value)) {
1136 compile_error(ast_ctx(self), "internal error: ast_value required");
1140 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1141 if (!array->expression.count || array->expression.count > opts.max_array_size)
1142 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)array->expression.count);
1144 elemtype = &array->expression.next->expression;
1145 vtype = elemtype->vtype;
1147 v = ir_builder_create_field(ir, self->name, vtype);
1149 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", self->name);
1152 v->context = ast_ctx(self);
1153 v->unique_life = true;
1154 array->ir_v = self->ir_v = v;
1156 namelen = strlen(self->name);
1157 name = (char*)mem_a(namelen + 16);
1158 strcpy(name, self->name);
1160 array->ir_values = (ir_value**)mem_a(sizeof(array->ir_values[0]) * array->expression.count);
1161 array->ir_values[0] = v;
1162 for (ai = 1; ai < array->expression.count; ++ai) {
1163 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1164 array->ir_values[ai] = ir_builder_create_field(ir, name, vtype);
1165 if (!array->ir_values[ai]) {
1167 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", name);
1170 array->ir_values[ai]->context = ast_ctx(self);
1171 array->ir_values[ai]->unique_life = true;
1177 v = ir_builder_create_field(ir, self->name, self->expression.next->expression.vtype);
1180 v->context = ast_ctx(self);
1186 if (self->expression.vtype == TYPE_ARRAY) {
1191 ast_expression_common *elemtype = &self->expression.next->expression;
1192 int vtype = elemtype->vtype;
1194 /* same as with field arrays */
1195 if (!self->expression.count || self->expression.count > opts.max_array_size)
1196 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1198 v = ir_builder_create_global(ir, self->name, vtype);
1200 compile_error(ast_ctx(self), "ir_builder_create_global failed `%s`", self->name);
1203 v->context = ast_ctx(self);
1204 v->unique_life = true;
1206 namelen = strlen(self->name);
1207 name = (char*)mem_a(namelen + 16);
1208 strcpy(name, self->name);
1210 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1211 self->ir_values[0] = v;
1212 for (ai = 1; ai < self->expression.count; ++ai) {
1213 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1214 self->ir_values[ai] = ir_builder_create_global(ir, name, vtype);
1215 if (!self->ir_values[ai]) {
1217 compile_error(ast_ctx(self), "ir_builder_create_global failed `%s`", name);
1220 self->ir_values[ai]->context = ast_ctx(self);
1221 self->ir_values[ai]->unique_life = true;
1227 /* Arrays don't do this since there's no "array" value which spans across the
1230 v = ir_builder_create_global(ir, self->name, self->expression.vtype);
1232 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", self->name);
1235 codegen_output_type(self, v);
1236 v->context = ast_ctx(self);
1239 if (self->hasvalue) {
1240 switch (self->expression.vtype)
1243 if (!ir_value_set_float(v, self->constval.vfloat))
1247 if (!ir_value_set_vector(v, self->constval.vvec))
1251 if (!ir_value_set_string(v, self->constval.vstring))
1255 compile_error(ast_ctx(self), "TODO: global constant array");
1258 compile_error(ast_ctx(self), "global of type function not properly generated");
1260 /* Cannot generate an IR value for a function,
1261 * need a pointer pointing to a function rather.
1264 if (!self->constval.vfield) {
1265 compile_error(ast_ctx(self), "field constant without vfield set");
1268 if (!self->constval.vfield->ir_v) {
1269 compile_error(ast_ctx(self), "field constant generated before its field");
1272 if (!ir_value_set_field(v, self->constval.vfield->ir_v))
1276 compile_error(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1281 /* link us to the ir_value */
1286 error: /* clean up */
1291 bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
1294 if (self->hasvalue && self->expression.vtype == TYPE_FUNCTION)
1296 /* Do we allow local functions? I think not...
1297 * this is NOT a function pointer atm.
1302 if (self->expression.vtype == TYPE_ARRAY) {
1307 ast_expression_common *elemtype = &self->expression.next->expression;
1308 int vtype = elemtype->vtype;
1311 compile_error(ast_ctx(self), "array-parameters are not supported");
1315 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1316 if (!self->expression.count || self->expression.count > opts.max_array_size) {
1317 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1320 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1321 if (!self->ir_values) {
1322 compile_error(ast_ctx(self), "failed to allocate array values");
1326 v = ir_function_create_local(func, self->name, vtype, param);
1328 compile_error(ast_ctx(self), "ir_function_create_local failed");
1331 v->context = ast_ctx(self);
1332 v->unique_life = true;
1334 namelen = strlen(self->name);
1335 name = (char*)mem_a(namelen + 16);
1336 strcpy(name, self->name);
1338 self->ir_values[0] = v;
1339 for (ai = 1; ai < self->expression.count; ++ai) {
1340 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1341 self->ir_values[ai] = ir_function_create_local(func, name, vtype, param);
1342 if (!self->ir_values[ai]) {
1343 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", name);
1346 self->ir_values[ai]->context = ast_ctx(self);
1347 self->ir_values[ai]->unique_life = true;
1352 v = ir_function_create_local(func, self->name, self->expression.vtype, param);
1355 codegen_output_type(self, v);
1356 v->context = ast_ctx(self);
1359 /* A constant local... hmmm...
1360 * I suppose the IR will have to deal with this
1362 if (self->hasvalue) {
1363 switch (self->expression.vtype)
1366 if (!ir_value_set_float(v, self->constval.vfloat))
1370 if (!ir_value_set_vector(v, self->constval.vvec))
1374 if (!ir_value_set_string(v, self->constval.vstring))
1378 compile_error(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1383 /* link us to the ir_value */
1387 if (!ast_generate_accessors(self, func->owner))
1391 error: /* clean up */
1396 bool ast_generate_accessors(ast_value *self, ir_builder *ir)
1399 bool warn = OPTS_WARN(WARN_USED_UNINITIALIZED);
1400 if (!self->setter || !self->getter)
1402 for (i = 0; i < self->expression.count; ++i) {
1403 if (!self->ir_values) {
1404 compile_error(ast_ctx(self), "internal error: no array values generated for `%s`", self->name);
1407 if (!self->ir_values[i]) {
1408 compile_error(ast_ctx(self), "internal error: not all array values have been generated for `%s`", self->name);
1411 if (self->ir_values[i]->life) {
1412 compile_error(ast_ctx(self), "internal error: function containing `%s` already generated", self->name);
1417 options_set(opts.warn, WARN_USED_UNINITIALIZED, false);
1419 if (!ast_global_codegen (self->setter, ir, false) ||
1420 !ast_function_codegen(self->setter->constval.vfunc, ir) ||
1421 !ir_function_finalize(self->setter->constval.vfunc->ir_func))
1423 compile_error(ast_ctx(self), "internal error: failed to generate setter for `%s`", self->name);
1424 options_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1429 if (!ast_global_codegen (self->getter, ir, false) ||
1430 !ast_function_codegen(self->getter->constval.vfunc, ir) ||
1431 !ir_function_finalize(self->getter->constval.vfunc->ir_func))
1433 compile_error(ast_ctx(self), "internal error: failed to generate getter for `%s`", self->name);
1434 options_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1438 for (i = 0; i < self->expression.count; ++i) {
1439 vec_free(self->ir_values[i]->life);
1441 options_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1445 bool ast_function_codegen(ast_function *self, ir_builder *ir)
1449 ast_expression_common *ec;
1454 irf = self->ir_func;
1456 compile_error(ast_ctx(self), "ast_function's related ast_value was not generated yet");
1460 /* fill the parameter list */
1461 ec = &self->vtype->expression;
1462 for (i = 0; i < vec_size(ec->params); ++i)
1464 if (ec->params[i]->expression.vtype == TYPE_FIELD)
1465 vec_push(irf->params, ec->params[i]->expression.next->expression.vtype);
1467 vec_push(irf->params, ec->params[i]->expression.vtype);
1468 if (!self->builtin) {
1469 if (!ast_local_codegen(ec->params[i], self->ir_func, true))
1474 if (self->builtin) {
1475 irf->builtin = self->builtin;
1479 if (!vec_size(self->blocks)) {
1480 compile_error(ast_ctx(self), "function `%s` has no body", self->name);
1484 self->curblock = ir_function_create_block(ast_ctx(self), irf, "entry");
1485 if (!self->curblock) {
1486 compile_error(ast_ctx(self), "failed to allocate entry block for `%s`", self->name);
1490 for (i = 0; i < vec_size(self->blocks); ++i) {
1491 ast_expression_codegen *gen = self->blocks[i]->expression.codegen;
1492 if (!(*gen)((ast_expression*)self->blocks[i], self, false, &dummy))
1496 /* TODO: check return types */
1497 if (!self->curblock->is_return)
1499 if (!self->vtype->expression.next ||
1500 self->vtype->expression.next->expression.vtype == TYPE_VOID)
1502 return ir_block_create_return(self->curblock, ast_ctx(self), NULL);
1504 else if (vec_size(self->curblock->entries))
1506 /* error("missing return"); */
1507 if (compile_warning(ast_ctx(self), WARN_MISSING_RETURN_VALUES,
1508 "control reaches end of non-void function (`%s`) via %s",
1509 self->name, self->curblock->label))
1513 return ir_block_create_return(self->curblock, ast_ctx(self), NULL);
1519 /* Note, you will not see ast_block_codegen generate ir_blocks.
1520 * To the AST and the IR, blocks are 2 different things.
1521 * In the AST it represents a block of code, usually enclosed in
1522 * curly braces {...}.
1523 * While in the IR it represents a block in terms of control-flow.
1525 bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_value **out)
1529 /* We don't use this
1530 * Note: an ast-representation using the comma-operator
1531 * of the form: (a, b, c) = x should not assign to c...
1534 compile_error(ast_ctx(self), "not an l-value (code-block)");
1538 if (self->expression.outr) {
1539 *out = self->expression.outr;
1543 /* output is NULL at first, we'll have each expression
1544 * assign to out output, thus, a comma-operator represention
1545 * using an ast_block will return the last generated value,
1546 * so: (b, c) + a executed both b and c, and returns c,
1547 * which is then added to a.
1551 /* generate locals */
1552 for (i = 0; i < vec_size(self->locals); ++i)
1554 if (!ast_local_codegen(self->locals[i], func->ir_func, false)) {
1556 compile_error(ast_ctx(self), "failed to generate local `%s`", self->locals[i]->name);
1561 for (i = 0; i < vec_size(self->exprs); ++i)
1563 ast_expression_codegen *gen = self->exprs[i]->expression.codegen;
1564 if (func->curblock->final && !ast_istype(self->exprs[i], ast_label)) {
1565 compile_error(ast_ctx(self->exprs[i]), "unreachable statement");
1568 if (!(*gen)(self->exprs[i], func, false, out))
1572 self->expression.outr = *out;
1577 bool ast_store_codegen(ast_store *self, ast_function *func, bool lvalue, ir_value **out)
1579 ast_expression_codegen *cgen;
1580 ir_value *left = NULL;
1581 ir_value *right = NULL;
1585 ast_array_index *ai = NULL;
1587 if (lvalue && self->expression.outl) {
1588 *out = self->expression.outl;
1592 if (!lvalue && self->expression.outr) {
1593 *out = self->expression.outr;
1597 if (ast_istype(self->dest, ast_array_index))
1600 ai = (ast_array_index*)self->dest;
1601 idx = (ast_value*)ai->index;
1603 if (ast_istype(ai->index, ast_value) && idx->hasvalue && idx->cvq == CV_CONST)
1608 /* we need to call the setter */
1609 ir_value *iridx, *funval;
1613 compile_error(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1617 arr = (ast_value*)ai->array;
1618 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1619 compile_error(ast_ctx(self), "value has no setter (%s)", arr->name);
1623 cgen = idx->expression.codegen;
1624 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1627 cgen = arr->setter->expression.codegen;
1628 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
1631 cgen = self->source->expression.codegen;
1632 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1635 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "store"), funval);
1638 ir_call_param(call, iridx);
1639 ir_call_param(call, right);
1640 self->expression.outr = right;
1646 cgen = self->dest->expression.codegen;
1648 if (!(*cgen)((ast_expression*)(self->dest), func, true, &left))
1650 self->expression.outl = left;
1652 cgen = self->source->expression.codegen;
1654 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1657 if (!ir_block_create_store_op(func->curblock, ast_ctx(self), self->op, left, right))
1659 self->expression.outr = right;
1662 /* Theoretically, an assinment returns its left side as an
1663 * lvalue, if we don't need an lvalue though, we return
1664 * the right side as an rvalue, otherwise we have to
1665 * somehow know whether or not we need to dereference the pointer
1666 * on the left side - that is: OP_LOAD if it was an address.
1667 * Also: in original QC we cannot OP_LOADP *anyway*.
1669 *out = (lvalue ? left : right);
1674 bool ast_binary_codegen(ast_binary *self, ast_function *func, bool lvalue, ir_value **out)
1676 ast_expression_codegen *cgen;
1677 ir_value *left, *right;
1679 /* A binary operation cannot yield an l-value */
1681 compile_error(ast_ctx(self), "not an l-value (binop)");
1685 if (self->expression.outr) {
1686 *out = self->expression.outr;
1690 if (OPTS_FLAG(SHORT_LOGIC) &&
1691 (self->op == INSTR_AND || self->op == INSTR_OR))
1693 /* short circuit evaluation */
1694 ir_block *other, *merge;
1695 ir_block *from_left, *from_right;
1700 /* Note about casting to true boolean values:
1701 * We use a single NOT for sub expressions, and an
1702 * overall NOT at the end, and for that purpose swap
1703 * all the jump conditions in order for the NOT to get
1705 * ie: (a && b) usually becomes (!!a ? !!b : !!a)
1706 * but we translate this to (!(!a ? !a : !b))
1709 merge_id = vec_size(func->ir_func->blocks);
1710 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "sce_merge"));
1712 cgen = self->left->expression.codegen;
1713 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1715 if (!OPTS_FLAG(PERL_LOGIC)) {
1716 notop = type_not_instr[left->vtype];
1717 if (notop == AINSTR_END) {
1718 compile_error(ast_ctx(self), "don't know how to cast to bool...");
1721 left = ir_block_create_unary(func->curblock, ast_ctx(self),
1722 ast_function_label(func, "sce_not"),
1726 from_left = func->curblock;
1728 other = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "sce_other"));
1729 if ( !(self->op == INSTR_OR) != !OPTS_FLAG(PERL_LOGIC) ) {
1730 if (!ir_block_create_if(func->curblock, ast_ctx(self), left, other, merge))
1733 if (!ir_block_create_if(func->curblock, ast_ctx(self), left, merge, other))
1736 /* use the likely flag */
1737 vec_last(func->curblock->instr)->likely = true;
1739 func->curblock = other;
1740 cgen = self->right->expression.codegen;
1741 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1743 if (!OPTS_FLAG(PERL_LOGIC)) {
1744 notop = type_not_instr[right->vtype];
1745 if (notop == AINSTR_END) {
1746 compile_error(ast_ctx(self), "don't know how to cast to bool...");
1749 right = ir_block_create_unary(func->curblock, ast_ctx(self),
1750 ast_function_label(func, "sce_not"),
1754 from_right = func->curblock;
1756 if (!ir_block_create_jump(func->curblock, ast_ctx(self), merge))
1759 vec_remove(func->ir_func->blocks, merge_id, 1);
1760 vec_push(func->ir_func->blocks, merge);
1762 func->curblock = merge;
1763 phi = ir_block_create_phi(func->curblock, ast_ctx(self), ast_function_label(func, "sce_value"), TYPE_FLOAT);
1764 ir_phi_add(phi, from_left, left);
1765 ir_phi_add(phi, from_right, right);
1766 *out = ir_phi_value(phi);
1767 if (!OPTS_FLAG(PERL_LOGIC)) {
1768 notop = type_not_instr[(*out)->vtype];
1769 if (notop == AINSTR_END) {
1770 compile_error(ast_ctx(self), "don't know how to cast to bool...");
1773 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
1774 ast_function_label(func, "sce_final_not"),
1780 self->expression.outr = *out;
1784 cgen = self->left->expression.codegen;
1785 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1788 cgen = self->right->expression.codegen;
1789 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1792 *out = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "bin"),
1793 self->op, left, right);
1796 self->expression.outr = *out;
1801 bool ast_binstore_codegen(ast_binstore *self, ast_function *func, bool lvalue, ir_value **out)
1803 ast_expression_codegen *cgen;
1804 ir_value *leftl = NULL, *leftr, *right, *bin;
1808 ast_array_index *ai = NULL;
1809 ir_value *iridx = NULL;
1811 if (lvalue && self->expression.outl) {
1812 *out = self->expression.outl;
1816 if (!lvalue && self->expression.outr) {
1817 *out = self->expression.outr;
1821 if (ast_istype(self->dest, ast_array_index))
1824 ai = (ast_array_index*)self->dest;
1825 idx = (ast_value*)ai->index;
1827 if (ast_istype(ai->index, ast_value) && idx->hasvalue && idx->cvq == CV_CONST)
1831 /* for a binstore we need both an lvalue and an rvalue for the left side */
1832 /* rvalue of destination! */
1834 cgen = idx->expression.codegen;
1835 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1838 cgen = self->dest->expression.codegen;
1839 if (!(*cgen)((ast_expression*)(self->dest), func, false, &leftr))
1842 /* source as rvalue only */
1843 cgen = self->source->expression.codegen;
1844 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1847 /* now the binary */
1848 bin = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "binst"),
1849 self->opbin, leftr, right);
1850 self->expression.outr = bin;
1854 /* we need to call the setter */
1859 compile_error(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1863 arr = (ast_value*)ai->array;
1864 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1865 compile_error(ast_ctx(self), "value has no setter (%s)", arr->name);
1869 cgen = arr->setter->expression.codegen;
1870 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
1873 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "store"), funval);
1876 ir_call_param(call, iridx);
1877 ir_call_param(call, bin);
1878 self->expression.outr = bin;
1880 /* now store them */
1881 cgen = self->dest->expression.codegen;
1882 /* lvalue of destination */
1883 if (!(*cgen)((ast_expression*)(self->dest), func, true, &leftl))
1885 self->expression.outl = leftl;
1887 if (!ir_block_create_store_op(func->curblock, ast_ctx(self), self->opstore, leftl, bin))
1889 self->expression.outr = bin;
1892 /* Theoretically, an assinment returns its left side as an
1893 * lvalue, if we don't need an lvalue though, we return
1894 * the right side as an rvalue, otherwise we have to
1895 * somehow know whether or not we need to dereference the pointer
1896 * on the left side - that is: OP_LOAD if it was an address.
1897 * Also: in original QC we cannot OP_LOADP *anyway*.
1899 *out = (lvalue ? leftl : bin);
1904 bool ast_unary_codegen(ast_unary *self, ast_function *func, bool lvalue, ir_value **out)
1906 ast_expression_codegen *cgen;
1909 /* An unary operation cannot yield an l-value */
1911 compile_error(ast_ctx(self), "not an l-value (binop)");
1915 if (self->expression.outr) {
1916 *out = self->expression.outr;
1920 cgen = self->operand->expression.codegen;
1922 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
1925 *out = ir_block_create_unary(func->curblock, ast_ctx(self), ast_function_label(func, "unary"),
1929 self->expression.outr = *out;
1934 bool ast_return_codegen(ast_return *self, ast_function *func, bool lvalue, ir_value **out)
1936 ast_expression_codegen *cgen;
1941 /* In the context of a return operation, we don't actually return
1945 compile_error(ast_ctx(self), "return-expression is not an l-value");
1949 if (self->expression.outr) {
1950 compile_error(ast_ctx(self), "internal error: ast_return cannot be reused, it bears no result!");
1953 self->expression.outr = (ir_value*)1;
1955 if (self->operand) {
1956 cgen = self->operand->expression.codegen;
1958 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
1961 if (!ir_block_create_return(func->curblock, ast_ctx(self), operand))
1964 if (!ir_block_create_return(func->curblock, ast_ctx(self), NULL))
1971 bool ast_entfield_codegen(ast_entfield *self, ast_function *func, bool lvalue, ir_value **out)
1973 ast_expression_codegen *cgen;
1974 ir_value *ent, *field;
1976 /* This function needs to take the 'lvalue' flag into account!
1977 * As lvalue we provide a field-pointer, as rvalue we provide the
1981 if (lvalue && self->expression.outl) {
1982 *out = self->expression.outl;
1986 if (!lvalue && self->expression.outr) {
1987 *out = self->expression.outr;
1991 cgen = self->entity->expression.codegen;
1992 if (!(*cgen)((ast_expression*)(self->entity), func, false, &ent))
1995 cgen = self->field->expression.codegen;
1996 if (!(*cgen)((ast_expression*)(self->field), func, false, &field))
2001 *out = ir_block_create_fieldaddress(func->curblock, ast_ctx(self), ast_function_label(func, "efa"),
2004 *out = ir_block_create_load_from_ent(func->curblock, ast_ctx(self), ast_function_label(func, "efv"),
2005 ent, field, self->expression.vtype);
2006 /* Done AFTER error checking:
2007 codegen_output_type(self, *out);
2011 compile_error(ast_ctx(self), "failed to create %s instruction (output type %s)",
2012 (lvalue ? "ADDRESS" : "FIELD"),
2013 type_name[self->expression.vtype]);
2017 codegen_output_type(self, *out);
2020 self->expression.outl = *out;
2022 self->expression.outr = *out;
2024 /* Hm that should be it... */
2028 bool ast_member_codegen(ast_member *self, ast_function *func, bool lvalue, ir_value **out)
2030 ast_expression_codegen *cgen;
2033 /* in QC this is always an lvalue */
2035 if (self->expression.outl) {
2036 *out = self->expression.outl;
2040 cgen = self->owner->expression.codegen;
2041 if (!(*cgen)((ast_expression*)(self->owner), func, true, &vec))
2044 if (vec->vtype != TYPE_VECTOR &&
2045 !(vec->vtype == TYPE_FIELD && self->owner->expression.next->expression.vtype == TYPE_VECTOR))
2050 *out = ir_value_vector_member(vec, self->field);
2051 self->expression.outl = *out;
2053 return (*out != NULL);
2056 bool ast_array_index_codegen(ast_array_index *self, ast_function *func, bool lvalue, ir_value **out)
2061 if (!lvalue && self->expression.outr) {
2062 *out = self->expression.outr;
2064 if (lvalue && self->expression.outl) {
2065 *out = self->expression.outl;
2068 if (!ast_istype(self->array, ast_value)) {
2069 compile_error(ast_ctx(self), "array indexing this way is not supported");
2070 /* note this would actually be pointer indexing because the left side is
2071 * not an actual array but (hopefully) an indexable expression.
2072 * Once we get integer arithmetic, and GADDRESS/GSTORE/GLOAD instruction
2073 * support this path will be filled.
2078 arr = (ast_value*)self->array;
2079 idx = (ast_value*)self->index;
2081 if (!ast_istype(self->index, ast_value) || !idx->hasvalue || idx->cvq != CV_CONST) {
2082 /* Time to use accessor functions */
2083 ast_expression_codegen *cgen;
2084 ir_value *iridx, *funval;
2088 compile_error(ast_ctx(self), "(.2) array indexing here needs a compile-time constant");
2093 compile_error(ast_ctx(self), "value has no getter, don't know how to index it");
2097 cgen = self->index->expression.codegen;
2098 if (!(*cgen)((ast_expression*)(self->index), func, false, &iridx))
2101 cgen = arr->getter->expression.codegen;
2102 if (!(*cgen)((ast_expression*)(arr->getter), func, true, &funval))
2105 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "fetch"), funval);
2108 ir_call_param(call, iridx);
2110 *out = ir_call_value(call);
2111 self->expression.outr = *out;
2115 if (idx->expression.vtype == TYPE_FLOAT) {
2116 unsigned int arridx = idx->constval.vfloat;
2117 if (arridx >= self->array->expression.count)
2119 compile_error(ast_ctx(self), "array index out of bounds: %i", arridx);
2122 *out = arr->ir_values[arridx];
2124 else if (idx->expression.vtype == TYPE_INTEGER) {
2125 unsigned int arridx = idx->constval.vint;
2126 if (arridx >= self->array->expression.count)
2128 compile_error(ast_ctx(self), "array index out of bounds: %i", arridx);
2131 *out = arr->ir_values[arridx];
2134 compile_error(ast_ctx(self), "array indexing here needs an integer constant");
2140 bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_value **out)
2142 ast_expression_codegen *cgen;
2147 ir_block *cond = func->curblock;
2150 ir_block *ontrue_endblock = NULL;
2151 ir_block *onfalse_endblock = NULL;
2152 ir_block *merge = NULL;
2154 /* We don't output any value, thus also don't care about r/lvalue */
2158 if (self->expression.outr) {
2159 compile_error(ast_ctx(self), "internal error: ast_ifthen cannot be reused, it bears no result!");
2162 self->expression.outr = (ir_value*)1;
2164 /* generate the condition */
2165 cgen = self->cond->expression.codegen;
2166 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2168 /* update the block which will get the jump - because short-logic or ternaries may have changed this */
2169 cond = func->curblock;
2173 if (self->on_true) {
2174 /* create on-true block */
2175 ontrue = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "ontrue"));
2179 /* enter the block */
2180 func->curblock = ontrue;
2183 cgen = self->on_true->expression.codegen;
2184 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &dummy))
2187 /* we now need to work from the current endpoint */
2188 ontrue_endblock = func->curblock;
2193 if (self->on_false) {
2194 /* create on-false block */
2195 onfalse = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "onfalse"));
2199 /* enter the block */
2200 func->curblock = onfalse;
2203 cgen = self->on_false->expression.codegen;
2204 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &dummy))
2207 /* we now need to work from the current endpoint */
2208 onfalse_endblock = func->curblock;
2212 /* Merge block were they all merge in to */
2213 if (!ontrue || !onfalse || !ontrue_endblock->final || !onfalse_endblock->final)
2215 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "endif"));
2218 /* add jumps ot the merge block */
2219 if (ontrue && !ontrue_endblock->final && !ir_block_create_jump(ontrue_endblock, ast_ctx(self), merge))
2221 if (onfalse && !onfalse_endblock->final && !ir_block_create_jump(onfalse_endblock, ast_ctx(self), merge))
2224 /* Now enter the merge block */
2225 func->curblock = merge;
2228 /* we create the if here, that way all blocks are ordered :)
2230 if (!ir_block_create_if(cond, ast_ctx(self), condval,
2231 (ontrue ? ontrue : merge),
2232 (onfalse ? onfalse : merge)))
2240 bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_value **out)
2242 ast_expression_codegen *cgen;
2245 ir_value *trueval, *falseval;
2248 ir_block *cond = func->curblock;
2249 ir_block *cond_out = NULL;
2250 ir_block *ontrue, *ontrue_out = NULL;
2251 ir_block *onfalse, *onfalse_out = NULL;
2254 /* Ternary can never create an lvalue... */
2258 /* In theory it shouldn't be possible to pass through a node twice, but
2259 * in case we add any kind of optimization pass for the AST itself, it
2260 * may still happen, thus we remember a created ir_value and simply return one
2261 * if it already exists.
2263 if (self->expression.outr) {
2264 *out = self->expression.outr;
2268 /* In the following, contraty to ast_ifthen, we assume both paths exist. */
2270 /* generate the condition */
2271 func->curblock = cond;
2272 cgen = self->cond->expression.codegen;
2273 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2275 cond_out = func->curblock;
2277 /* create on-true block */
2278 ontrue = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_T"));
2283 /* enter the block */
2284 func->curblock = ontrue;
2287 cgen = self->on_true->expression.codegen;
2288 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &trueval))
2291 ontrue_out = func->curblock;
2294 /* create on-false block */
2295 onfalse = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_F"));
2300 /* enter the block */
2301 func->curblock = onfalse;
2304 cgen = self->on_false->expression.codegen;
2305 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &falseval))
2308 onfalse_out = func->curblock;
2311 /* create merge block */
2312 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_out"));
2315 /* jump to merge block */
2316 if (!ir_block_create_jump(ontrue_out, ast_ctx(self), merge))
2318 if (!ir_block_create_jump(onfalse_out, ast_ctx(self), merge))
2321 /* create if instruction */
2322 if (!ir_block_create_if(cond_out, ast_ctx(self), condval, ontrue, onfalse))
2325 /* Now enter the merge block */
2326 func->curblock = merge;
2328 /* Here, now, we need a PHI node
2329 * but first some sanity checking...
2331 if (trueval->vtype != falseval->vtype) {
2332 /* error("ternary with different types on the two sides"); */
2337 phi = ir_block_create_phi(merge, ast_ctx(self), ast_function_label(func, "phi"), trueval->vtype);
2340 ir_phi_add(phi, ontrue_out, trueval);
2341 ir_phi_add(phi, onfalse_out, falseval);
2343 self->expression.outr = ir_phi_value(phi);
2344 *out = self->expression.outr;
2346 codegen_output_type(self, *out);
2351 bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value **out)
2353 ast_expression_codegen *cgen;
2355 ir_value *dummy = NULL;
2356 ir_value *precond = NULL;
2357 ir_value *postcond = NULL;
2359 /* Since we insert some jumps "late" so we have blocks
2360 * ordered "nicely", we need to keep track of the actual end-blocks
2361 * of expressions to add the jumps to.
2363 ir_block *bbody = NULL, *end_bbody = NULL;
2364 ir_block *bprecond = NULL, *end_bprecond = NULL;
2365 ir_block *bpostcond = NULL, *end_bpostcond = NULL;
2366 ir_block *bincrement = NULL, *end_bincrement = NULL;
2367 ir_block *bout = NULL, *bin = NULL;
2369 /* let's at least move the outgoing block to the end */
2372 /* 'break' and 'continue' need to be able to find the right blocks */
2373 ir_block *bcontinue = NULL;
2374 ir_block *bbreak = NULL;
2376 ir_block *old_bcontinue = NULL;
2377 ir_block *old_bbreak = NULL;
2379 ir_block *tmpblock = NULL;
2384 if (self->expression.outr) {
2385 compile_error(ast_ctx(self), "internal error: ast_loop cannot be reused, it bears no result!");
2388 self->expression.outr = (ir_value*)1;
2391 * Should we ever need some kind of block ordering, better make this function
2392 * move blocks around than write a block ordering algorithm later... after all
2393 * the ast and ir should work together, not against each other.
2396 /* initexpr doesn't get its own block, it's pointless, it could create more blocks
2397 * anyway if for example it contains a ternary.
2401 cgen = self->initexpr->expression.codegen;
2402 if (!(*cgen)((ast_expression*)(self->initexpr), func, false, &dummy))
2406 /* Store the block from which we enter this chaos */
2407 bin = func->curblock;
2409 /* The pre-loop condition needs its own block since we
2410 * need to be able to jump to the start of that expression.
2414 bprecond = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "pre_loop_cond"));
2418 /* the pre-loop-condition the least important place to 'continue' at */
2419 bcontinue = bprecond;
2422 func->curblock = bprecond;
2425 cgen = self->precond->expression.codegen;
2426 if (!(*cgen)((ast_expression*)(self->precond), func, false, &precond))
2429 end_bprecond = func->curblock;
2431 bprecond = end_bprecond = NULL;
2434 /* Now the next blocks won't be ordered nicely, but we need to
2435 * generate them this early for 'break' and 'continue'.
2437 if (self->increment) {
2438 bincrement = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "loop_increment"));
2441 bcontinue = bincrement; /* increment comes before the pre-loop-condition */
2443 bincrement = end_bincrement = NULL;
2446 if (self->postcond) {
2447 bpostcond = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "post_loop_cond"));
2450 bcontinue = bpostcond; /* postcond comes before the increment */
2452 bpostcond = end_bpostcond = NULL;
2455 bout_id = vec_size(func->ir_func->blocks);
2456 bout = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "after_loop"));
2461 /* The loop body... */
2462 /* if (self->body) */
2464 bbody = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "loop_body"));
2469 func->curblock = bbody;
2471 old_bbreak = func->breakblock;
2472 old_bcontinue = func->continueblock;
2473 func->breakblock = bbreak;
2474 func->continueblock = bcontinue;
2475 if (!func->continueblock)
2476 func->continueblock = bbody;
2480 cgen = self->body->expression.codegen;
2481 if (!(*cgen)((ast_expression*)(self->body), func, false, &dummy))
2485 end_bbody = func->curblock;
2486 func->breakblock = old_bbreak;
2487 func->continueblock = old_bcontinue;
2490 /* post-loop-condition */
2494 func->curblock = bpostcond;
2497 cgen = self->postcond->expression.codegen;
2498 if (!(*cgen)((ast_expression*)(self->postcond), func, false, &postcond))
2501 end_bpostcond = func->curblock;
2504 /* The incrementor */
2505 if (self->increment)
2508 func->curblock = bincrement;
2511 cgen = self->increment->expression.codegen;
2512 if (!(*cgen)((ast_expression*)(self->increment), func, false, &dummy))
2515 end_bincrement = func->curblock;
2518 /* In any case now, we continue from the outgoing block */
2519 func->curblock = bout;
2521 /* Now all blocks are in place */
2522 /* From 'bin' we jump to whatever comes first */
2523 if (bprecond) tmpblock = bprecond;
2524 else if (bbody) tmpblock = bbody;
2525 else if (bpostcond) tmpblock = bpostcond;
2526 else tmpblock = bout;
2527 if (!ir_block_create_jump(bin, ast_ctx(self), tmpblock))
2533 ir_block *ontrue, *onfalse;
2534 if (bbody) ontrue = bbody;
2535 else if (bincrement) ontrue = bincrement;
2536 else if (bpostcond) ontrue = bpostcond;
2537 else ontrue = bprecond;
2539 if (!ir_block_create_if(end_bprecond, ast_ctx(self), precond, ontrue, onfalse))
2546 if (bincrement) tmpblock = bincrement;
2547 else if (bpostcond) tmpblock = bpostcond;
2548 else if (bprecond) tmpblock = bprecond;
2549 else tmpblock = bbody;
2550 if (!end_bbody->final && !ir_block_create_jump(end_bbody, ast_ctx(self), tmpblock))
2554 /* from increment */
2557 if (bpostcond) tmpblock = bpostcond;
2558 else if (bprecond) tmpblock = bprecond;
2559 else if (bbody) tmpblock = bbody;
2560 else tmpblock = bout;
2561 if (!ir_block_create_jump(end_bincrement, ast_ctx(self), tmpblock))
2568 ir_block *ontrue, *onfalse;
2569 if (bprecond) ontrue = bprecond;
2570 else if (bbody) ontrue = bbody;
2571 else if (bincrement) ontrue = bincrement;
2572 else ontrue = bpostcond;
2574 if (!ir_block_create_if(end_bpostcond, ast_ctx(self), postcond, ontrue, onfalse))
2578 /* Move 'bout' to the end */
2579 vec_remove(func->ir_func->blocks, bout_id, 1);
2580 vec_push(func->ir_func->blocks, bout);
2585 bool ast_breakcont_codegen(ast_breakcont *self, ast_function *func, bool lvalue, ir_value **out)
2592 compile_error(ast_ctx(self), "break/continue expression is not an l-value");
2596 if (self->expression.outr) {
2597 compile_error(ast_ctx(self), "internal error: ast_breakcont cannot be reused!");
2600 self->expression.outr = (ir_value*)1;
2602 if (self->is_continue)
2603 target = func->continueblock;
2605 target = func->breakblock;
2608 compile_error(ast_ctx(self), "%s is lacking a target block", (self->is_continue ? "continue" : "break"));
2612 if (!ir_block_create_jump(func->curblock, ast_ctx(self), target))
2617 bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_value **out)
2619 ast_expression_codegen *cgen;
2621 ast_switch_case *def_case = NULL;
2622 ir_block *def_bfall = NULL;
2624 ir_value *dummy = NULL;
2625 ir_value *irop = NULL;
2626 ir_block *old_break = NULL;
2627 ir_block *bout = NULL;
2628 ir_block *bfall = NULL;
2636 compile_error(ast_ctx(self), "switch expression is not an l-value");
2640 if (self->expression.outr) {
2641 compile_error(ast_ctx(self), "internal error: ast_switch cannot be reused!");
2644 self->expression.outr = (ir_value*)1;
2649 cgen = self->operand->expression.codegen;
2650 if (!(*cgen)((ast_expression*)(self->operand), func, false, &irop))
2653 if (!vec_size(self->cases))
2656 cmpinstr = type_eq_instr[irop->vtype];
2657 if (cmpinstr >= AINSTR_END) {
2658 ast_type_to_string(self->operand, typestr, sizeof(typestr));
2659 compile_error(ast_ctx(self), "invalid type to perform a switch on: %s", typestr);
2663 bout_id = vec_size(func->ir_func->blocks);
2664 bout = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "after_switch"));
2668 /* setup the break block */
2669 old_break = func->breakblock;
2670 func->breakblock = bout;
2672 /* Now create all cases */
2673 for (c = 0; c < vec_size(self->cases); ++c) {
2674 ir_value *cond, *val;
2675 ir_block *bcase, *bnot;
2678 ast_switch_case *swcase = &self->cases[c];
2680 if (swcase->value) {
2681 /* A regular case */
2682 /* generate the condition operand */
2683 cgen = swcase->value->expression.codegen;
2684 if (!(*cgen)((ast_expression*)(swcase->value), func, false, &val))
2686 /* generate the condition */
2687 cond = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "switch_eq"), cmpinstr, irop, val);
2691 bcase = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "case"));
2692 bnot_id = vec_size(func->ir_func->blocks);
2693 bnot = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "not_case"));
2694 if (!bcase || !bnot)
2696 if (!ir_block_create_if(func->curblock, ast_ctx(self), cond, bcase, bnot))
2699 /* Make the previous case-end fall through */
2700 if (bfall && !bfall->final) {
2701 if (!ir_block_create_jump(bfall, ast_ctx(self), bcase))
2705 /* enter the case */
2706 func->curblock = bcase;
2707 cgen = swcase->code->expression.codegen;
2708 if (!(*cgen)((ast_expression*)swcase->code, func, false, &dummy))
2711 /* remember this block to fall through from */
2712 bfall = func->curblock;
2714 /* enter the else and move it down */
2715 func->curblock = bnot;
2716 vec_remove(func->ir_func->blocks, bnot_id, 1);
2717 vec_push(func->ir_func->blocks, bnot);
2719 /* The default case */
2720 /* Remember where to fall through from: */
2723 /* remember which case it was */
2728 /* Jump from the last bnot to bout */
2729 if (bfall && !bfall->final && !ir_block_create_jump(bfall, ast_ctx(self), bout)) {
2731 astwarning(ast_ctx(bfall), WARN_???, "missing break after last case");
2736 /* If there was a default case, put it down here */
2740 /* No need to create an extra block */
2741 bcase = func->curblock;
2743 /* Insert the fallthrough jump */
2744 if (def_bfall && !def_bfall->final) {
2745 if (!ir_block_create_jump(def_bfall, ast_ctx(self), bcase))
2749 /* Now generate the default code */
2750 cgen = def_case->code->expression.codegen;
2751 if (!(*cgen)((ast_expression*)def_case->code, func, false, &dummy))
2755 /* Jump from the last bnot to bout */
2756 if (!func->curblock->final && !ir_block_create_jump(func->curblock, ast_ctx(self), bout))
2758 /* enter the outgoing block */
2759 func->curblock = bout;
2761 /* restore the break block */
2762 func->breakblock = old_break;
2764 /* Move 'bout' to the end, it's nicer */
2765 vec_remove(func->ir_func->blocks, bout_id, 1);
2766 vec_push(func->ir_func->blocks, bout);
2771 bool ast_label_codegen(ast_label *self, ast_function *func, bool lvalue, ir_value **out)
2778 compile_error(ast_ctx(self), "internal error: ast_label cannot be an lvalue");
2782 /* simply create a new block and jump to it */
2783 self->irblock = ir_function_create_block(ast_ctx(self), func->ir_func, self->name);
2784 if (!self->irblock) {
2785 compile_error(ast_ctx(self), "failed to allocate label block `%s`", self->name);
2788 if (!func->curblock->final) {
2789 if (!ir_block_create_jump(func->curblock, ast_ctx(self), self->irblock))
2793 /* enter the new block */
2794 func->curblock = self->irblock;
2796 /* Generate all the leftover gotos */
2797 for (i = 0; i < vec_size(self->gotos); ++i) {
2798 if (!ast_goto_codegen(self->gotos[i], func, false, &dummy))
2805 bool ast_goto_codegen(ast_goto *self, ast_function *func, bool lvalue, ir_value **out)
2809 compile_error(ast_ctx(self), "internal error: ast_goto cannot be an lvalue");
2813 if (self->target->irblock) {
2814 if (self->irblock_from) {
2815 /* we already tried once, this is the callback */
2816 self->irblock_from->final = false;
2817 if (!ir_block_create_jump(self->irblock_from, ast_ctx(self), self->target->irblock)) {
2818 compile_error(ast_ctx(self), "failed to generate goto to `%s`", self->name);
2824 if (!ir_block_create_jump(func->curblock, ast_ctx(self), self->target->irblock)) {
2825 compile_error(ast_ctx(self), "failed to generate goto to `%s`", self->name);
2832 /* the target has not yet been created...
2833 * close this block in a sneaky way:
2835 func->curblock->final = true;
2836 self->irblock_from = func->curblock;
2837 ast_label_register_goto(self->target, self);
2843 bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value **out)
2845 ast_expression_codegen *cgen;
2847 ir_instr *callinstr;
2850 ir_value *funval = NULL;
2852 /* return values are never lvalues */
2854 compile_error(ast_ctx(self), "not an l-value (function call)");
2858 if (self->expression.outr) {
2859 *out = self->expression.outr;
2863 cgen = self->func->expression.codegen;
2864 if (!(*cgen)((ast_expression*)(self->func), func, false, &funval))
2872 for (i = 0; i < vec_size(self->params); ++i)
2875 ast_expression *expr = self->params[i];
2877 cgen = expr->expression.codegen;
2878 if (!(*cgen)(expr, func, false, ¶m))
2882 vec_push(params, param);
2885 callinstr = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "call"), funval);
2889 for (i = 0; i < vec_size(params); ++i) {
2890 ir_call_param(callinstr, params[i]);
2893 *out = ir_call_value(callinstr);
2894 self->expression.outr = *out;
2896 codegen_output_type(self, *out);