5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is furnished to do
10 * so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 #define ast_instantiate(T, ctx, destroyfn) \
31 T* self = (T*)mem_a(sizeof(T)); \
35 ast_node_init((ast_node*)self, ctx, TYPE_##T); \
36 ( (ast_node*)self )->node.destroy = (ast_node_delete*)destroyfn
39 /* It must not be possible to get here. */
40 static GMQCC_NORETURN void _ast_node_destroy(ast_node *self)
43 con_err("ast node missing destroy()\n");
47 /* Initialize main ast node aprts */
48 static void ast_node_init(ast_node *self, lex_ctx ctx, int nodetype)
50 self->node.context = ctx;
51 self->node.destroy = &_ast_node_destroy;
52 self->node.keep = false;
53 self->node.nodetype = nodetype;
54 self->node.side_effects = false;
57 /* weight and side effects */
58 static void _ast_propagate_effects(ast_node *self, ast_node *other)
60 if (ast_side_effects(other))
61 ast_side_effects(self) = true;
63 #define ast_propagate_effects(s,o) _ast_propagate_effects(((ast_node*)(s)), ((ast_node*)(o)))
65 /* General expression initialization */
66 static void ast_expression_init(ast_expression *self,
67 ast_expression_codegen *codegen)
69 self->expression.codegen = codegen;
70 self->expression.vtype = TYPE_VOID;
71 self->expression.next = NULL;
72 self->expression.outl = NULL;
73 self->expression.outr = NULL;
74 self->expression.variadic = false;
75 self->expression.params = NULL;
78 static void ast_expression_delete(ast_expression *self)
81 if (self->expression.next)
82 ast_delete(self->expression.next);
83 for (i = 0; i < vec_size(self->expression.params); ++i) {
84 ast_delete(self->expression.params[i]);
86 vec_free(self->expression.params);
89 static void ast_expression_delete_full(ast_expression *self)
91 ast_expression_delete(self);
95 ast_value* ast_value_copy(const ast_value *self)
98 const ast_expression_common *fromex;
99 ast_expression_common *selfex;
100 ast_value *cp = ast_value_new(self->expression.node.context, self->name, self->expression.vtype);
101 if (self->expression.next) {
102 cp->expression.next = ast_type_copy(self->expression.node.context, self->expression.next);
103 if (!cp->expression.next) {
104 ast_value_delete(cp);
108 fromex = &self->expression;
109 selfex = &cp->expression;
110 selfex->variadic = fromex->variadic;
111 for (i = 0; i < vec_size(fromex->params); ++i) {
112 ast_value *v = ast_value_copy(fromex->params[i]);
114 ast_value_delete(cp);
117 vec_push(selfex->params, v);
122 bool ast_type_adopt_impl(ast_expression *self, const ast_expression *other)
125 const ast_expression_common *fromex;
126 ast_expression_common *selfex;
127 self->expression.vtype = other->expression.vtype;
128 if (other->expression.next) {
129 self->expression.next = (ast_expression*)ast_type_copy(ast_ctx(self), other->expression.next);
130 if (!self->expression.next)
133 fromex = &other->expression;
134 selfex = &self->expression;
135 selfex->variadic = fromex->variadic;
136 for (i = 0; i < vec_size(fromex->params); ++i) {
137 ast_value *v = ast_value_copy(fromex->params[i]);
140 vec_push(selfex->params, v);
145 static ast_expression* ast_shallow_type(lex_ctx ctx, int vtype)
147 ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
148 ast_expression_init(self, NULL);
149 self->expression.codegen = NULL;
150 self->expression.next = NULL;
151 self->expression.vtype = vtype;
155 ast_expression* ast_type_copy(lex_ctx ctx, const ast_expression *ex)
158 const ast_expression_common *fromex;
159 ast_expression_common *selfex;
165 ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
166 ast_expression_init(self, NULL);
168 fromex = &ex->expression;
169 selfex = &self->expression;
171 /* This may never be codegen()d */
172 selfex->codegen = NULL;
174 selfex->vtype = fromex->vtype;
177 selfex->next = ast_type_copy(ctx, fromex->next);
179 ast_expression_delete_full(self);
186 selfex->variadic = fromex->variadic;
187 for (i = 0; i < vec_size(fromex->params); ++i) {
188 ast_value *v = ast_value_copy(fromex->params[i]);
190 ast_expression_delete_full(self);
193 vec_push(selfex->params, v);
200 bool ast_compare_type(ast_expression *a, ast_expression *b)
202 if (a->expression.vtype != b->expression.vtype)
204 if (!a->expression.next != !b->expression.next)
206 if (vec_size(a->expression.params) != vec_size(b->expression.params))
208 if (a->expression.variadic != b->expression.variadic)
210 if (vec_size(a->expression.params)) {
212 for (i = 0; i < vec_size(a->expression.params); ++i) {
213 if (!ast_compare_type((ast_expression*)a->expression.params[i],
214 (ast_expression*)b->expression.params[i]))
218 if (a->expression.next)
219 return ast_compare_type(a->expression.next, b->expression.next);
223 static size_t ast_type_to_string_impl(ast_expression *e, char *buf, size_t bufsize, size_t pos)
230 if (pos + 6 >= bufsize)
232 strcpy(buf + pos, "(null)");
236 if (pos + 1 >= bufsize)
239 switch (e->expression.vtype) {
241 strcpy(buf + pos, "(variant)");
246 return ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
249 if (pos + 3 >= bufsize)
253 pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
254 if (pos + 1 >= bufsize)
260 pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
261 if (pos + 2 >= bufsize)
263 if (!vec_size(e->expression.params)) {
269 pos = ast_type_to_string_impl((ast_expression*)(e->expression.params[0]), buf, bufsize, pos);
270 for (i = 1; i < vec_size(e->expression.params); ++i) {
271 if (pos + 2 >= bufsize)
275 pos = ast_type_to_string_impl((ast_expression*)(e->expression.params[i]), buf, bufsize, pos);
277 if (pos + 1 >= bufsize)
283 pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
284 if (pos + 1 >= bufsize)
287 pos += snprintf(buf + pos, bufsize - pos - 1, "%i", (int)e->expression.count);
288 if (pos + 1 >= bufsize)
294 typestr = type_name[e->expression.vtype];
295 typelen = strlen(typestr);
296 if (pos + typelen >= bufsize)
298 strcpy(buf + pos, typestr);
299 return pos + typelen;
303 buf[bufsize-3] = '.';
304 buf[bufsize-2] = '.';
305 buf[bufsize-1] = '.';
309 void ast_type_to_string(ast_expression *e, char *buf, size_t bufsize)
311 size_t pos = ast_type_to_string_impl(e, buf, bufsize-1, 0);
315 ast_value* ast_value_new(lex_ctx ctx, const char *name, int t)
317 ast_instantiate(ast_value, ctx, ast_value_delete);
318 ast_expression_init((ast_expression*)self,
319 (ast_expression_codegen*)&ast_value_codegen);
320 self->expression.node.keep = true; /* keep */
322 self->name = name ? util_strdup(name) : NULL;
323 self->expression.vtype = t;
324 self->expression.next = NULL;
325 self->isfield = false;
327 self->hasvalue = false;
329 memset(&self->constval, 0, sizeof(self->constval));
332 self->ir_values = NULL;
333 self->ir_value_count = 0;
341 void ast_value_delete(ast_value* self)
344 mem_d((void*)self->name);
345 if (self->hasvalue) {
346 switch (self->expression.vtype)
349 mem_d((void*)self->constval.vstring);
352 /* unlink us from the function node */
353 self->constval.vfunc->vtype = NULL;
355 /* NOTE: delete function? currently collected in
356 * the parser structure
363 mem_d(self->ir_values);
364 ast_expression_delete((ast_expression*)self);
368 void ast_value_params_add(ast_value *self, ast_value *p)
370 vec_push(self->expression.params, p);
373 bool ast_value_set_name(ast_value *self, const char *name)
376 mem_d((void*)self->name);
377 self->name = util_strdup(name);
381 ast_binary* ast_binary_new(lex_ctx ctx, int op,
382 ast_expression* left, ast_expression* right)
384 ast_instantiate(ast_binary, ctx, ast_binary_delete);
385 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binary_codegen);
391 ast_propagate_effects(self, left);
392 ast_propagate_effects(self, right);
394 if (op >= INSTR_EQ_F && op <= INSTR_GT)
395 self->expression.vtype = TYPE_FLOAT;
396 else if (op == INSTR_AND || op == INSTR_OR ||
397 op == INSTR_BITAND || op == INSTR_BITOR)
398 self->expression.vtype = TYPE_FLOAT;
399 else if (op == INSTR_MUL_VF || op == INSTR_MUL_FV)
400 self->expression.vtype = TYPE_VECTOR;
401 else if (op == INSTR_MUL_V)
402 self->expression.vtype = TYPE_FLOAT;
404 self->expression.vtype = left->expression.vtype;
409 void ast_binary_delete(ast_binary *self)
411 ast_unref(self->left);
412 ast_unref(self->right);
413 ast_expression_delete((ast_expression*)self);
417 ast_binstore* ast_binstore_new(lex_ctx ctx, int storop, int op,
418 ast_expression* left, ast_expression* right)
420 ast_instantiate(ast_binstore, ctx, ast_binstore_delete);
421 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binstore_codegen);
423 ast_side_effects(self) = true;
425 self->opstore = storop;
428 self->source = right;
430 self->keep_dest = false;
432 self->expression.vtype = left->expression.vtype;
433 if (left->expression.next) {
434 self->expression.next = ast_type_copy(ctx, left);
435 if (!self->expression.next) {
441 self->expression.next = NULL;
446 void ast_binstore_delete(ast_binstore *self)
448 if (!self->keep_dest)
449 ast_unref(self->dest);
450 ast_unref(self->source);
451 ast_expression_delete((ast_expression*)self);
455 ast_unary* ast_unary_new(lex_ctx ctx, int op,
456 ast_expression *expr)
458 ast_instantiate(ast_unary, ctx, ast_unary_delete);
459 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_unary_codegen);
462 self->operand = expr;
464 ast_propagate_effects(self, expr);
466 if (op >= INSTR_NOT_F && op <= INSTR_NOT_FNC) {
467 self->expression.vtype = TYPE_FLOAT;
469 compile_error(ctx, "cannot determine type of unary operation %s", asm_instr[op].m);
474 void ast_unary_delete(ast_unary *self)
476 ast_unref(self->operand);
477 ast_expression_delete((ast_expression*)self);
481 ast_return* ast_return_new(lex_ctx ctx, ast_expression *expr)
483 ast_instantiate(ast_return, ctx, ast_return_delete);
484 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_return_codegen);
486 self->operand = expr;
489 ast_propagate_effects(self, expr);
494 void ast_return_delete(ast_return *self)
497 ast_unref(self->operand);
498 ast_expression_delete((ast_expression*)self);
502 ast_entfield* ast_entfield_new(lex_ctx ctx, ast_expression *entity, ast_expression *field)
504 if (field->expression.vtype != TYPE_FIELD) {
505 compile_error(ctx, "ast_entfield_new with expression not of type field");
508 return ast_entfield_new_force(ctx, entity, field, field->expression.next);
511 ast_entfield* ast_entfield_new_force(lex_ctx ctx, ast_expression *entity, ast_expression *field, const ast_expression *outtype)
513 ast_instantiate(ast_entfield, ctx, ast_entfield_delete);
517 /* Error: field has no type... */
521 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_entfield_codegen);
523 self->entity = entity;
525 ast_propagate_effects(self, entity);
526 ast_propagate_effects(self, field);
528 if (!ast_type_adopt(self, outtype)) {
529 ast_entfield_delete(self);
536 void ast_entfield_delete(ast_entfield *self)
538 ast_unref(self->entity);
539 ast_unref(self->field);
540 ast_expression_delete((ast_expression*)self);
544 ast_member* ast_member_new(lex_ctx ctx, ast_expression *owner, unsigned int field, const char *name)
546 ast_instantiate(ast_member, ctx, ast_member_delete);
552 if (owner->expression.vtype != TYPE_VECTOR &&
553 owner->expression.vtype != TYPE_FIELD) {
554 compile_error(ctx, "member-access on an invalid owner of type %s", type_name[owner->expression.vtype]);
559 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_member_codegen);
560 self->expression.node.keep = true; /* keep */
562 if (owner->expression.vtype == TYPE_VECTOR) {
563 self->expression.vtype = TYPE_FLOAT;
564 self->expression.next = NULL;
566 self->expression.vtype = TYPE_FIELD;
567 self->expression.next = ast_shallow_type(ctx, TYPE_FLOAT);
571 ast_propagate_effects(self, owner);
575 self->name = util_strdup(name);
582 void ast_member_delete(ast_member *self)
584 /* The owner is always an ast_value, which has .keep=true,
585 * also: ast_members are usually deleted after the owner, thus
586 * this will cause invalid access
587 ast_unref(self->owner);
588 * once we allow (expression).x to access a vector-member, we need
589 * to change this: preferably by creating an alternate ast node for this
590 * purpose that is not garbage-collected.
592 ast_expression_delete((ast_expression*)self);
596 ast_array_index* ast_array_index_new(lex_ctx ctx, ast_expression *array, ast_expression *index)
598 ast_expression *outtype;
599 ast_instantiate(ast_array_index, ctx, ast_array_index_delete);
601 outtype = array->expression.next;
604 /* Error: field has no type... */
608 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_array_index_codegen);
612 ast_propagate_effects(self, array);
613 ast_propagate_effects(self, index);
615 if (!ast_type_adopt(self, outtype)) {
616 ast_array_index_delete(self);
619 if (array->expression.vtype == TYPE_FIELD && outtype->expression.vtype == TYPE_ARRAY) {
620 if (self->expression.vtype != TYPE_ARRAY) {
621 compile_error(ast_ctx(self), "array_index node on type");
622 ast_array_index_delete(self);
625 self->array = outtype;
626 self->expression.vtype = TYPE_FIELD;
632 void ast_array_index_delete(ast_array_index *self)
634 ast_unref(self->array);
635 ast_unref(self->index);
636 ast_expression_delete((ast_expression*)self);
640 ast_ifthen* ast_ifthen_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
642 ast_instantiate(ast_ifthen, ctx, ast_ifthen_delete);
643 if (!ontrue && !onfalse) {
644 /* because it is invalid */
648 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ifthen_codegen);
651 self->on_true = ontrue;
652 self->on_false = onfalse;
653 ast_propagate_effects(self, cond);
655 ast_propagate_effects(self, ontrue);
657 ast_propagate_effects(self, onfalse);
662 void ast_ifthen_delete(ast_ifthen *self)
664 ast_unref(self->cond);
666 ast_unref(self->on_true);
668 ast_unref(self->on_false);
669 ast_expression_delete((ast_expression*)self);
673 ast_ternary* ast_ternary_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
675 ast_instantiate(ast_ternary, ctx, ast_ternary_delete);
676 /* This time NEITHER must be NULL */
677 if (!ontrue || !onfalse) {
681 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ternary_codegen);
684 self->on_true = ontrue;
685 self->on_false = onfalse;
686 ast_propagate_effects(self, cond);
687 ast_propagate_effects(self, ontrue);
688 ast_propagate_effects(self, onfalse);
690 if (!ast_type_adopt(self, ontrue)) {
691 ast_ternary_delete(self);
698 void ast_ternary_delete(ast_ternary *self)
700 ast_unref(self->cond);
701 ast_unref(self->on_true);
702 ast_unref(self->on_false);
703 ast_expression_delete((ast_expression*)self);
707 ast_loop* ast_loop_new(lex_ctx ctx,
708 ast_expression *initexpr,
709 ast_expression *precond,
710 ast_expression *postcond,
711 ast_expression *increment,
712 ast_expression *body)
714 ast_instantiate(ast_loop, ctx, ast_loop_delete);
715 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_loop_codegen);
717 self->initexpr = initexpr;
718 self->precond = precond;
719 self->postcond = postcond;
720 self->increment = increment;
724 ast_propagate_effects(self, initexpr);
726 ast_propagate_effects(self, precond);
728 ast_propagate_effects(self, postcond);
730 ast_propagate_effects(self, increment);
732 ast_propagate_effects(self, body);
737 void ast_loop_delete(ast_loop *self)
740 ast_unref(self->initexpr);
742 ast_unref(self->precond);
744 ast_unref(self->postcond);
746 ast_unref(self->increment);
748 ast_unref(self->body);
749 ast_expression_delete((ast_expression*)self);
753 ast_breakcont* ast_breakcont_new(lex_ctx ctx, bool iscont)
755 ast_instantiate(ast_breakcont, ctx, ast_breakcont_delete);
756 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_breakcont_codegen);
758 self->is_continue = iscont;
763 void ast_breakcont_delete(ast_breakcont *self)
765 ast_expression_delete((ast_expression*)self);
769 ast_switch* ast_switch_new(lex_ctx ctx, ast_expression *op)
771 ast_instantiate(ast_switch, ctx, ast_switch_delete);
772 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_switch_codegen);
777 ast_propagate_effects(self, op);
782 void ast_switch_delete(ast_switch *self)
785 ast_unref(self->operand);
787 for (i = 0; i < vec_size(self->cases); ++i) {
788 if (self->cases[i].value)
789 ast_unref(self->cases[i].value);
790 ast_unref(self->cases[i].code);
792 vec_free(self->cases);
794 ast_expression_delete((ast_expression*)self);
798 ast_label* ast_label_new(lex_ctx ctx, const char *name)
800 ast_instantiate(ast_label, ctx, ast_label_delete);
801 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_label_codegen);
803 self->name = util_strdup(name);
804 self->irblock = NULL;
810 void ast_label_delete(ast_label *self)
812 mem_d((void*)self->name);
813 vec_free(self->gotos);
814 ast_expression_delete((ast_expression*)self);
818 void ast_label_register_goto(ast_label *self, ast_goto *g)
820 vec_push(self->gotos, g);
823 ast_goto* ast_goto_new(lex_ctx ctx, const char *name)
825 ast_instantiate(ast_goto, ctx, ast_goto_delete);
826 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_goto_codegen);
828 self->name = util_strdup(name);
830 self->irblock_from = NULL;
835 void ast_goto_delete(ast_goto *self)
837 mem_d((void*)self->name);
838 ast_expression_delete((ast_expression*)self);
842 void ast_goto_set_label(ast_goto *self, ast_label *label)
844 self->target = label;
847 ast_call* ast_call_new(lex_ctx ctx,
848 ast_expression *funcexpr)
850 ast_instantiate(ast_call, ctx, ast_call_delete);
851 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_call_codegen);
853 ast_side_effects(self) = true;
856 self->func = funcexpr;
859 self->expression.vtype = funcexpr->expression.next->expression.vtype;
860 if (funcexpr->expression.next->expression.next)
861 self->expression.next = ast_type_copy(ctx, funcexpr->expression.next->expression.next);
863 ast_type_adopt(self, funcexpr->expression.next);
868 void ast_call_delete(ast_call *self)
871 for (i = 0; i < vec_size(self->params); ++i)
872 ast_unref(self->params[i]);
873 vec_free(self->params);
876 ast_unref(self->func);
878 ast_expression_delete((ast_expression*)self);
882 bool ast_call_check_types(ast_call *self)
886 const ast_expression *func = self->func;
887 size_t count = vec_size(self->params);
888 if (count > vec_size(func->expression.params))
889 count = vec_size(func->expression.params);
891 for (i = 0; i < count; ++i) {
892 if (!ast_compare_type(self->params[i], (ast_expression*)(func->expression.params[i]))) {
895 ast_type_to_string(self->params[i], tgot, sizeof(tgot));
896 ast_type_to_string((ast_expression*)func->expression.params[i], texp, sizeof(texp));
897 compile_error(ast_ctx(self), "invalid type for parameter %u in function call: expected %s, got %s",
898 (unsigned int)(i+1), texp, tgot);
899 /* we don't immediately return */
906 ast_store* ast_store_new(lex_ctx ctx, int op,
907 ast_expression *dest, ast_expression *source)
909 ast_instantiate(ast_store, ctx, ast_store_delete);
910 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_store_codegen);
912 ast_side_effects(self) = true;
916 self->source = source;
918 self->expression.vtype = dest->expression.vtype;
919 if (dest->expression.next) {
920 self->expression.next = ast_type_copy(ctx, dest);
921 if (!self->expression.next) {
927 self->expression.next = NULL;
932 void ast_store_delete(ast_store *self)
934 ast_unref(self->dest);
935 ast_unref(self->source);
936 ast_expression_delete((ast_expression*)self);
940 ast_block* ast_block_new(lex_ctx ctx)
942 ast_instantiate(ast_block, ctx, ast_block_delete);
943 ast_expression_init((ast_expression*)self,
944 (ast_expression_codegen*)&ast_block_codegen);
948 self->collect = NULL;
953 void ast_block_add_expr(ast_block *self, ast_expression *e)
955 ast_propagate_effects(self, e);
956 vec_push(self->exprs, e);
959 void ast_block_collect(ast_block *self, ast_expression *expr)
961 vec_push(self->collect, expr);
962 expr->expression.node.keep = true;
965 void ast_block_delete(ast_block *self)
968 for (i = 0; i < vec_size(self->exprs); ++i)
969 ast_unref(self->exprs[i]);
970 vec_free(self->exprs);
971 for (i = 0; i < vec_size(self->locals); ++i)
972 ast_delete(self->locals[i]);
973 vec_free(self->locals);
974 for (i = 0; i < vec_size(self->collect); ++i)
975 ast_delete(self->collect[i]);
976 vec_free(self->collect);
977 ast_expression_delete((ast_expression*)self);
981 bool ast_block_set_type(ast_block *self, ast_expression *from)
983 if (self->expression.next)
984 ast_delete(self->expression.next);
985 self->expression.vtype = from->expression.vtype;
986 if (from->expression.next) {
987 self->expression.next = ast_type_copy(self->expression.node.context, from->expression.next);
988 if (!self->expression.next)
992 self->expression.next = NULL;
996 ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype)
998 ast_instantiate(ast_function, ctx, ast_function_delete);
1002 vtype->expression.vtype != TYPE_FUNCTION)
1004 compile_error(ast_ctx(self), "internal error: ast_function_new condition %i %i type=%i (probably 2 bodies?)",
1006 (int)vtype->hasvalue,
1007 vtype->expression.vtype);
1012 self->vtype = vtype;
1013 self->name = name ? util_strdup(name) : NULL;
1014 self->blocks = NULL;
1016 self->labelcount = 0;
1019 self->ir_func = NULL;
1020 self->curblock = NULL;
1022 self->breakblock = NULL;
1023 self->continueblock = NULL;
1025 vtype->hasvalue = true;
1026 vtype->constval.vfunc = self;
1031 void ast_function_delete(ast_function *self)
1035 mem_d((void*)self->name);
1037 /* ast_value_delete(self->vtype); */
1038 self->vtype->hasvalue = false;
1039 self->vtype->constval.vfunc = NULL;
1040 /* We use unref - if it was stored in a global table it is supposed
1041 * to be deleted from *there*
1043 ast_unref(self->vtype);
1045 for (i = 0; i < vec_size(self->blocks); ++i)
1046 ast_delete(self->blocks[i]);
1047 vec_free(self->blocks);
1051 const char* ast_function_label(ast_function *self, const char *prefix)
1057 if (!opts_dump && !opts_dumpfin)
1060 id = (self->labelcount++);
1061 len = strlen(prefix);
1063 from = self->labelbuf + sizeof(self->labelbuf)-1;
1066 unsigned int digit = id % 10;
1067 *from = digit + '0';
1070 memcpy(from - len, prefix, len);
1074 /*********************************************************************/
1076 * by convention you must never pass NULL to the 'ir_value **out'
1077 * parameter. If you really don't care about the output, pass a dummy.
1078 * But I can't imagine a pituation where the output is truly unnecessary.
1081 bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out)
1085 /* NOTE: This is the codegen for a variable used in an expression.
1086 * It is not the codegen to generate the value. For this purpose,
1087 * ast_local_codegen and ast_global_codegen are to be used before this
1088 * is executed. ast_function_codegen should take care of its locals,
1089 * and the ast-user should take care of ast_global_codegen to be used
1090 * on all the globals.
1093 char typename[1024];
1094 ast_type_to_string((ast_expression*)self, typename, sizeof(typename));
1095 compile_error(ast_ctx(self), "ast_value used before generated %s %s", typename, self->name);
1102 bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
1106 if (self->hasvalue && self->expression.vtype == TYPE_FUNCTION)
1108 ir_function *func = ir_builder_create_function(ir, self->name, self->expression.next->expression.vtype);
1111 func->context = ast_ctx(self);
1112 func->value->context = ast_ctx(self);
1114 self->constval.vfunc->ir_func = func;
1115 self->ir_v = func->value;
1116 /* The function is filled later on ast_function_codegen... */
1120 if (isfield && self->expression.vtype == TYPE_FIELD) {
1121 ast_expression *fieldtype = self->expression.next;
1123 if (self->hasvalue) {
1124 compile_error(ast_ctx(self), "TODO: constant field pointers with value");
1128 if (fieldtype->expression.vtype == TYPE_ARRAY) {
1133 ast_expression_common *elemtype;
1135 ast_value *array = (ast_value*)fieldtype;
1137 if (!ast_istype(fieldtype, ast_value)) {
1138 compile_error(ast_ctx(self), "internal error: ast_value required");
1142 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1143 if (!array->expression.count || array->expression.count > opts_max_array_size)
1144 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)array->expression.count);
1146 elemtype = &array->expression.next->expression;
1147 vtype = elemtype->vtype;
1149 v = ir_builder_create_field(ir, self->name, vtype);
1151 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", self->name);
1154 if (vtype == TYPE_FIELD)
1155 v->fieldtype = elemtype->next->expression.vtype;
1156 if (vtype == TYPE_FUNCTION)
1157 v->outtype = elemtype->next->expression.vtype;
1158 v->context = ast_ctx(self);
1159 array->ir_v = self->ir_v = v;
1161 namelen = strlen(self->name);
1162 name = (char*)mem_a(namelen + 16);
1163 strcpy(name, self->name);
1165 array->ir_values = (ir_value**)mem_a(sizeof(array->ir_values[0]) * array->expression.count);
1166 array->ir_values[0] = v;
1167 for (ai = 1; ai < array->expression.count; ++ai) {
1168 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1169 array->ir_values[ai] = ir_builder_create_field(ir, name, vtype);
1170 if (!array->ir_values[ai]) {
1172 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", name);
1175 if (vtype == TYPE_FIELD)
1176 array->ir_values[ai]->fieldtype = elemtype->next->expression.vtype;
1177 if (vtype == TYPE_FUNCTION)
1178 array->ir_values[ai]->outtype = elemtype->next->expression.vtype;
1179 array->ir_values[ai]->context = ast_ctx(self);
1185 v = ir_builder_create_field(ir, self->name, self->expression.next->expression.vtype);
1188 v->context = ast_ctx(self);
1194 if (self->expression.vtype == TYPE_ARRAY) {
1199 ast_expression_common *elemtype = &self->expression.next->expression;
1200 int vtype = elemtype->vtype;
1202 /* same as with field arrays */
1203 if (!self->expression.count || self->expression.count > opts_max_array_size)
1204 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1206 v = ir_builder_create_global(ir, self->name, vtype);
1208 compile_error(ast_ctx(self), "ir_builder_create_global failed `%s`", self->name);
1211 if (vtype == TYPE_FIELD)
1212 v->fieldtype = elemtype->next->expression.vtype;
1213 if (vtype == TYPE_FUNCTION)
1214 v->outtype = elemtype->next->expression.vtype;
1215 v->context = ast_ctx(self);
1217 namelen = strlen(self->name);
1218 name = (char*)mem_a(namelen + 16);
1219 strcpy(name, self->name);
1221 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1222 self->ir_values[0] = v;
1223 for (ai = 1; ai < self->expression.count; ++ai) {
1224 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1225 self->ir_values[ai] = ir_builder_create_global(ir, name, vtype);
1226 if (!self->ir_values[ai]) {
1228 compile_error(ast_ctx(self), "ir_builder_create_global failed `%s`", name);
1231 if (vtype == TYPE_FIELD)
1232 self->ir_values[ai]->fieldtype = elemtype->next->expression.vtype;
1233 if (vtype == TYPE_FUNCTION)
1234 self->ir_values[ai]->outtype = elemtype->next->expression.vtype;
1235 self->ir_values[ai]->context = ast_ctx(self);
1241 /* Arrays don't do this since there's no "array" value which spans across the
1244 v = ir_builder_create_global(ir, self->name, self->expression.vtype);
1246 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", self->name);
1249 if (self->expression.vtype == TYPE_FIELD)
1250 v->fieldtype = self->expression.next->expression.vtype;
1251 if (self->expression.vtype == TYPE_FUNCTION)
1252 v->outtype = self->expression.next->expression.vtype;
1253 v->context = ast_ctx(self);
1256 if (self->hasvalue) {
1257 switch (self->expression.vtype)
1260 if (!ir_value_set_float(v, self->constval.vfloat))
1264 if (!ir_value_set_vector(v, self->constval.vvec))
1268 if (!ir_value_set_string(v, self->constval.vstring))
1272 compile_error(ast_ctx(self), "TODO: global constant array");
1275 compile_error(ast_ctx(self), "global of type function not properly generated");
1277 /* Cannot generate an IR value for a function,
1278 * need a pointer pointing to a function rather.
1281 if (!self->constval.vfield) {
1282 compile_error(ast_ctx(self), "field constant without vfield set");
1285 if (!self->constval.vfield->ir_v) {
1286 compile_error(ast_ctx(self), "field constant generated before its field");
1289 if (!ir_value_set_field(v, self->constval.vfield->ir_v))
1293 compile_error(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1298 /* link us to the ir_value */
1303 error: /* clean up */
1308 bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
1311 if (self->hasvalue && self->expression.vtype == TYPE_FUNCTION)
1313 /* Do we allow local functions? I think not...
1314 * this is NOT a function pointer atm.
1319 if (self->expression.vtype == TYPE_ARRAY) {
1324 ast_expression_common *elemtype = &self->expression.next->expression;
1325 int vtype = elemtype->vtype;
1328 compile_error(ast_ctx(self), "array-parameters are not supported");
1332 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1333 if (!self->expression.count || self->expression.count > opts_max_array_size) {
1334 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1337 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1338 if (!self->ir_values) {
1339 compile_error(ast_ctx(self), "failed to allocate array values");
1343 v = ir_function_create_local(func, self->name, vtype, param);
1345 compile_error(ast_ctx(self), "ir_function_create_local failed");
1348 if (vtype == TYPE_FIELD)
1349 v->fieldtype = elemtype->next->expression.vtype;
1350 if (vtype == TYPE_FUNCTION)
1351 v->outtype = elemtype->next->expression.vtype;
1352 v->context = ast_ctx(self);
1354 namelen = strlen(self->name);
1355 name = (char*)mem_a(namelen + 16);
1356 strcpy(name, self->name);
1358 self->ir_values[0] = v;
1359 for (ai = 1; ai < self->expression.count; ++ai) {
1360 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1361 self->ir_values[ai] = ir_function_create_local(func, name, vtype, param);
1362 if (!self->ir_values[ai]) {
1363 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", name);
1366 if (vtype == TYPE_FIELD)
1367 self->ir_values[ai]->fieldtype = elemtype->next->expression.vtype;
1368 if (vtype == TYPE_FUNCTION)
1369 self->ir_values[ai]->outtype = elemtype->next->expression.vtype;
1370 self->ir_values[ai]->context = ast_ctx(self);
1375 v = ir_function_create_local(func, self->name, self->expression.vtype, param);
1378 if (self->expression.vtype == TYPE_FIELD)
1379 v->fieldtype = self->expression.next->expression.vtype;
1380 if (self->expression.vtype == TYPE_FUNCTION)
1381 v->outtype = self->expression.next->expression.vtype;
1382 v->context = ast_ctx(self);
1385 /* A constant local... hmmm...
1386 * I suppose the IR will have to deal with this
1388 if (self->hasvalue) {
1389 switch (self->expression.vtype)
1392 if (!ir_value_set_float(v, self->constval.vfloat))
1396 if (!ir_value_set_vector(v, self->constval.vvec))
1400 if (!ir_value_set_string(v, self->constval.vstring))
1404 compile_error(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1409 /* link us to the ir_value */
1414 if (!ast_global_codegen(self->setter, func->owner, false) ||
1415 !ast_function_codegen(self->setter->constval.vfunc, func->owner) ||
1416 !ir_function_finalize(self->setter->constval.vfunc->ir_func))
1420 if (!ast_global_codegen(self->getter, func->owner, false) ||
1421 !ast_function_codegen(self->getter->constval.vfunc, func->owner) ||
1422 !ir_function_finalize(self->getter->constval.vfunc->ir_func))
1427 error: /* clean up */
1432 bool ast_function_codegen(ast_function *self, ir_builder *ir)
1436 ast_expression_common *ec;
1441 irf = self->ir_func;
1443 compile_error(ast_ctx(self), "ast_function's related ast_value was not generated yet");
1447 /* fill the parameter list */
1448 ec = &self->vtype->expression;
1449 for (i = 0; i < vec_size(ec->params); ++i)
1451 if (ec->params[i]->expression.vtype == TYPE_FIELD)
1452 vec_push(irf->params, ec->params[i]->expression.next->expression.vtype);
1454 vec_push(irf->params, ec->params[i]->expression.vtype);
1455 if (!self->builtin) {
1456 if (!ast_local_codegen(ec->params[i], self->ir_func, true))
1461 if (self->builtin) {
1462 irf->builtin = self->builtin;
1466 if (!vec_size(self->blocks)) {
1467 compile_error(ast_ctx(self), "function `%s` has no body", self->name);
1471 self->curblock = ir_function_create_block(ast_ctx(self), irf, "entry");
1472 if (!self->curblock) {
1473 compile_error(ast_ctx(self), "failed to allocate entry block for `%s`", self->name);
1477 for (i = 0; i < vec_size(self->blocks); ++i) {
1478 ast_expression_codegen *gen = self->blocks[i]->expression.codegen;
1479 if (!(*gen)((ast_expression*)self->blocks[i], self, false, &dummy))
1483 /* TODO: check return types */
1484 if (!self->curblock->is_return)
1486 if (!self->vtype->expression.next ||
1487 self->vtype->expression.next->expression.vtype == TYPE_VOID)
1489 return ir_block_create_return(self->curblock, ast_ctx(self), NULL);
1491 else if (vec_size(self->curblock->entries))
1493 /* error("missing return"); */
1494 if (compile_warning(ast_ctx(self), WARN_MISSING_RETURN_VALUES,
1495 "control reaches end of non-void function (`%s`) via %s",
1496 self->name, self->curblock->label))
1500 return ir_block_create_return(self->curblock, ast_ctx(self), NULL);
1506 /* Note, you will not see ast_block_codegen generate ir_blocks.
1507 * To the AST and the IR, blocks are 2 different things.
1508 * In the AST it represents a block of code, usually enclosed in
1509 * curly braces {...}.
1510 * While in the IR it represents a block in terms of control-flow.
1512 bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_value **out)
1516 /* We don't use this
1517 * Note: an ast-representation using the comma-operator
1518 * of the form: (a, b, c) = x should not assign to c...
1521 compile_error(ast_ctx(self), "not an l-value (code-block)");
1525 if (self->expression.outr) {
1526 *out = self->expression.outr;
1530 /* output is NULL at first, we'll have each expression
1531 * assign to out output, thus, a comma-operator represention
1532 * using an ast_block will return the last generated value,
1533 * so: (b, c) + a executed both b and c, and returns c,
1534 * which is then added to a.
1538 /* generate locals */
1539 for (i = 0; i < vec_size(self->locals); ++i)
1541 if (!ast_local_codegen(self->locals[i], func->ir_func, false)) {
1543 compile_error(ast_ctx(self), "failed to generate local `%s`", self->locals[i]->name);
1548 for (i = 0; i < vec_size(self->exprs); ++i)
1550 ast_expression_codegen *gen = self->exprs[i]->expression.codegen;
1551 if (func->curblock->final && !ast_istype(self->exprs[i], ast_label)) {
1552 compile_error(ast_ctx(self->exprs[i]), "unreachable statement");
1555 if (!(*gen)(self->exprs[i], func, false, out))
1559 self->expression.outr = *out;
1564 bool ast_store_codegen(ast_store *self, ast_function *func, bool lvalue, ir_value **out)
1566 ast_expression_codegen *cgen;
1567 ir_value *left = NULL;
1568 ir_value *right = NULL;
1572 ast_array_index *ai = NULL;
1574 if (lvalue && self->expression.outl) {
1575 *out = self->expression.outl;
1579 if (!lvalue && self->expression.outr) {
1580 *out = self->expression.outr;
1584 if (ast_istype(self->dest, ast_array_index))
1587 ai = (ast_array_index*)self->dest;
1588 idx = (ast_value*)ai->index;
1590 if (ast_istype(ai->index, ast_value) && idx->hasvalue && idx->cvq == CV_CONST)
1595 /* we need to call the setter */
1596 ir_value *iridx, *funval;
1600 compile_error(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1604 arr = (ast_value*)ai->array;
1605 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1606 compile_error(ast_ctx(self), "value has no setter (%s)", arr->name);
1610 cgen = idx->expression.codegen;
1611 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1614 cgen = arr->setter->expression.codegen;
1615 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
1618 cgen = self->source->expression.codegen;
1619 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1622 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "store"), funval);
1625 ir_call_param(call, iridx);
1626 ir_call_param(call, right);
1627 self->expression.outr = right;
1633 cgen = self->dest->expression.codegen;
1635 if (!(*cgen)((ast_expression*)(self->dest), func, true, &left))
1637 self->expression.outl = left;
1639 cgen = self->source->expression.codegen;
1641 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1644 if (!ir_block_create_store_op(func->curblock, ast_ctx(self), self->op, left, right))
1646 self->expression.outr = right;
1649 /* Theoretically, an assinment returns its left side as an
1650 * lvalue, if we don't need an lvalue though, we return
1651 * the right side as an rvalue, otherwise we have to
1652 * somehow know whether or not we need to dereference the pointer
1653 * on the left side - that is: OP_LOAD if it was an address.
1654 * Also: in original QC we cannot OP_LOADP *anyway*.
1656 *out = (lvalue ? left : right);
1661 bool ast_binary_codegen(ast_binary *self, ast_function *func, bool lvalue, ir_value **out)
1663 ast_expression_codegen *cgen;
1664 ir_value *left, *right;
1666 /* A binary operation cannot yield an l-value */
1668 compile_error(ast_ctx(self), "not an l-value (binop)");
1672 if (self->expression.outr) {
1673 *out = self->expression.outr;
1677 if (OPTS_FLAG(SHORT_LOGIC) &&
1678 (self->op == INSTR_AND || self->op == INSTR_OR))
1680 /* short circuit evaluation */
1681 ir_block *other, *merge;
1682 ir_block *from_left, *from_right;
1687 /* Note about casting to true boolean values:
1688 * We use a single NOT for sub expressions, and an
1689 * overall NOT at the end, and for that purpose swap
1690 * all the jump conditions in order for the NOT to get
1692 * ie: (a && b) usually becomes (!!a ? !!b : !!a)
1693 * but we translate this to (!(!a ? !a : !b))
1696 merge_id = vec_size(func->ir_func->blocks);
1697 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "sce_merge"));
1699 cgen = self->left->expression.codegen;
1700 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1702 if (!OPTS_FLAG(PERL_LOGIC)) {
1703 notop = type_not_instr[left->vtype];
1704 if (notop == AINSTR_END) {
1705 compile_error(ast_ctx(self), "don't know how to cast to bool...");
1708 left = ir_block_create_unary(func->curblock, ast_ctx(self),
1709 ast_function_label(func, "sce_not"),
1713 from_left = func->curblock;
1715 other = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "sce_other"));
1716 if ( !(self->op == INSTR_OR) != !OPTS_FLAG(PERL_LOGIC) ) {
1717 if (!ir_block_create_if(func->curblock, ast_ctx(self), left, other, merge))
1720 if (!ir_block_create_if(func->curblock, ast_ctx(self), left, merge, other))
1723 /* use the likely flag */
1724 vec_last(func->curblock->instr)->likely = true;
1726 func->curblock = other;
1727 cgen = self->right->expression.codegen;
1728 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1730 if (!OPTS_FLAG(PERL_LOGIC)) {
1731 notop = type_not_instr[right->vtype];
1732 if (notop == AINSTR_END) {
1733 compile_error(ast_ctx(self), "don't know how to cast to bool...");
1736 right = ir_block_create_unary(func->curblock, ast_ctx(self),
1737 ast_function_label(func, "sce_not"),
1741 from_right = func->curblock;
1743 if (!ir_block_create_jump(func->curblock, ast_ctx(self), merge))
1746 vec_remove(func->ir_func->blocks, merge_id, 1);
1747 vec_push(func->ir_func->blocks, merge);
1749 func->curblock = merge;
1750 phi = ir_block_create_phi(func->curblock, ast_ctx(self), ast_function_label(func, "sce_value"), TYPE_FLOAT);
1751 ir_phi_add(phi, from_left, left);
1752 ir_phi_add(phi, from_right, right);
1753 *out = ir_phi_value(phi);
1754 if (!OPTS_FLAG(PERL_LOGIC)) {
1755 notop = type_not_instr[(*out)->vtype];
1756 if (notop == AINSTR_END) {
1757 compile_error(ast_ctx(self), "don't know how to cast to bool...");
1760 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
1761 ast_function_label(func, "sce_final_not"),
1767 self->expression.outr = *out;
1771 cgen = self->left->expression.codegen;
1772 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1775 cgen = self->right->expression.codegen;
1776 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1779 *out = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "bin"),
1780 self->op, left, right);
1783 self->expression.outr = *out;
1788 bool ast_binstore_codegen(ast_binstore *self, ast_function *func, bool lvalue, ir_value **out)
1790 ast_expression_codegen *cgen;
1791 ir_value *leftl = NULL, *leftr, *right, *bin;
1795 ast_array_index *ai = NULL;
1796 ir_value *iridx = NULL;
1798 if (lvalue && self->expression.outl) {
1799 *out = self->expression.outl;
1803 if (!lvalue && self->expression.outr) {
1804 *out = self->expression.outr;
1808 if (ast_istype(self->dest, ast_array_index))
1811 ai = (ast_array_index*)self->dest;
1812 idx = (ast_value*)ai->index;
1814 if (ast_istype(ai->index, ast_value) && idx->hasvalue && idx->cvq == CV_CONST)
1818 /* for a binstore we need both an lvalue and an rvalue for the left side */
1819 /* rvalue of destination! */
1821 cgen = idx->expression.codegen;
1822 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1825 cgen = self->dest->expression.codegen;
1826 if (!(*cgen)((ast_expression*)(self->dest), func, false, &leftr))
1829 /* source as rvalue only */
1830 cgen = self->source->expression.codegen;
1831 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1834 /* now the binary */
1835 bin = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "binst"),
1836 self->opbin, leftr, right);
1837 self->expression.outr = bin;
1841 /* we need to call the setter */
1846 compile_error(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1850 arr = (ast_value*)ai->array;
1851 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1852 compile_error(ast_ctx(self), "value has no setter (%s)", arr->name);
1856 cgen = arr->setter->expression.codegen;
1857 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
1860 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "store"), funval);
1863 ir_call_param(call, iridx);
1864 ir_call_param(call, bin);
1865 self->expression.outr = bin;
1867 /* now store them */
1868 cgen = self->dest->expression.codegen;
1869 /* lvalue of destination */
1870 if (!(*cgen)((ast_expression*)(self->dest), func, true, &leftl))
1872 self->expression.outl = leftl;
1874 if (!ir_block_create_store_op(func->curblock, ast_ctx(self), self->opstore, leftl, bin))
1876 self->expression.outr = bin;
1879 /* Theoretically, an assinment returns its left side as an
1880 * lvalue, if we don't need an lvalue though, we return
1881 * the right side as an rvalue, otherwise we have to
1882 * somehow know whether or not we need to dereference the pointer
1883 * on the left side - that is: OP_LOAD if it was an address.
1884 * Also: in original QC we cannot OP_LOADP *anyway*.
1886 *out = (lvalue ? leftl : bin);
1891 bool ast_unary_codegen(ast_unary *self, ast_function *func, bool lvalue, ir_value **out)
1893 ast_expression_codegen *cgen;
1896 /* An unary operation cannot yield an l-value */
1898 compile_error(ast_ctx(self), "not an l-value (binop)");
1902 if (self->expression.outr) {
1903 *out = self->expression.outr;
1907 cgen = self->operand->expression.codegen;
1909 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
1912 *out = ir_block_create_unary(func->curblock, ast_ctx(self), ast_function_label(func, "unary"),
1916 self->expression.outr = *out;
1921 bool ast_return_codegen(ast_return *self, ast_function *func, bool lvalue, ir_value **out)
1923 ast_expression_codegen *cgen;
1928 /* In the context of a return operation, we don't actually return
1932 compile_error(ast_ctx(self), "return-expression is not an l-value");
1936 if (self->expression.outr) {
1937 compile_error(ast_ctx(self), "internal error: ast_return cannot be reused, it bears no result!");
1940 self->expression.outr = (ir_value*)1;
1942 if (self->operand) {
1943 cgen = self->operand->expression.codegen;
1945 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
1948 if (!ir_block_create_return(func->curblock, ast_ctx(self), operand))
1951 if (!ir_block_create_return(func->curblock, ast_ctx(self), NULL))
1958 bool ast_entfield_codegen(ast_entfield *self, ast_function *func, bool lvalue, ir_value **out)
1960 ast_expression_codegen *cgen;
1961 ir_value *ent, *field;
1963 /* This function needs to take the 'lvalue' flag into account!
1964 * As lvalue we provide a field-pointer, as rvalue we provide the
1968 if (lvalue && self->expression.outl) {
1969 *out = self->expression.outl;
1973 if (!lvalue && self->expression.outr) {
1974 *out = self->expression.outr;
1978 cgen = self->entity->expression.codegen;
1979 if (!(*cgen)((ast_expression*)(self->entity), func, false, &ent))
1982 cgen = self->field->expression.codegen;
1983 if (!(*cgen)((ast_expression*)(self->field), func, false, &field))
1988 *out = ir_block_create_fieldaddress(func->curblock, ast_ctx(self), ast_function_label(func, "efa"),
1991 *out = ir_block_create_load_from_ent(func->curblock, ast_ctx(self), ast_function_label(func, "efv"),
1992 ent, field, self->expression.vtype);
1993 if ((*out)->vtype == TYPE_FIELD)
1994 (*out)->fieldtype = self->expression.next->expression.vtype;
1995 if ((*out)->vtype == TYPE_FUNCTION)
1996 (*out)->outtype = self->expression.next->expression.vtype;
1999 compile_error(ast_ctx(self), "failed to create %s instruction (output type %s)",
2000 (lvalue ? "ADDRESS" : "FIELD"),
2001 type_name[self->expression.vtype]);
2006 self->expression.outl = *out;
2008 self->expression.outr = *out;
2010 /* Hm that should be it... */
2014 bool ast_member_codegen(ast_member *self, ast_function *func, bool lvalue, ir_value **out)
2016 ast_expression_codegen *cgen;
2019 /* in QC this is always an lvalue */
2021 if (self->expression.outl) {
2022 *out = self->expression.outl;
2026 cgen = self->owner->expression.codegen;
2027 if (!(*cgen)((ast_expression*)(self->owner), func, true, &vec))
2030 if (vec->vtype != TYPE_VECTOR &&
2031 !(vec->vtype == TYPE_FIELD && self->owner->expression.next->expression.vtype == TYPE_VECTOR))
2036 *out = ir_value_vector_member(vec, self->field);
2037 self->expression.outl = *out;
2039 return (*out != NULL);
2042 bool ast_array_index_codegen(ast_array_index *self, ast_function *func, bool lvalue, ir_value **out)
2047 if (!lvalue && self->expression.outr) {
2048 *out = self->expression.outr;
2050 if (lvalue && self->expression.outl) {
2051 *out = self->expression.outl;
2054 if (!ast_istype(self->array, ast_value)) {
2055 compile_error(ast_ctx(self), "array indexing this way is not supported");
2056 /* note this would actually be pointer indexing because the left side is
2057 * not an actual array but (hopefully) an indexable expression.
2058 * Once we get integer arithmetic, and GADDRESS/GSTORE/GLOAD instruction
2059 * support this path will be filled.
2064 arr = (ast_value*)self->array;
2065 idx = (ast_value*)self->index;
2067 if (!ast_istype(self->index, ast_value) || !idx->hasvalue || idx->cvq != CV_CONST) {
2068 /* Time to use accessor functions */
2069 ast_expression_codegen *cgen;
2070 ir_value *iridx, *funval;
2074 compile_error(ast_ctx(self), "(.2) array indexing here needs a compile-time constant");
2079 compile_error(ast_ctx(self), "value has no getter, don't know how to index it");
2083 cgen = self->index->expression.codegen;
2084 if (!(*cgen)((ast_expression*)(self->index), func, false, &iridx))
2087 cgen = arr->getter->expression.codegen;
2088 if (!(*cgen)((ast_expression*)(arr->getter), func, true, &funval))
2091 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "fetch"), funval);
2094 ir_call_param(call, iridx);
2096 *out = ir_call_value(call);
2097 self->expression.outr = *out;
2101 if (idx->expression.vtype == TYPE_FLOAT) {
2102 unsigned int arridx = idx->constval.vfloat;
2103 if (arridx >= self->array->expression.count)
2105 compile_error(ast_ctx(self), "array index out of bounds: %i", arridx);
2108 *out = arr->ir_values[arridx];
2110 else if (idx->expression.vtype == TYPE_INTEGER) {
2111 unsigned int arridx = idx->constval.vint;
2112 if (arridx >= self->array->expression.count)
2114 compile_error(ast_ctx(self), "array index out of bounds: %i", arridx);
2117 *out = arr->ir_values[arridx];
2120 compile_error(ast_ctx(self), "array indexing here needs an integer constant");
2126 bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_value **out)
2128 ast_expression_codegen *cgen;
2133 ir_block *cond = func->curblock;
2136 ir_block *ontrue_endblock = NULL;
2137 ir_block *onfalse_endblock = NULL;
2138 ir_block *merge = NULL;
2140 /* We don't output any value, thus also don't care about r/lvalue */
2144 if (self->expression.outr) {
2145 compile_error(ast_ctx(self), "internal error: ast_ifthen cannot be reused, it bears no result!");
2148 self->expression.outr = (ir_value*)1;
2150 /* generate the condition */
2151 cgen = self->cond->expression.codegen;
2152 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2154 /* update the block which will get the jump - because short-logic or ternaries may have changed this */
2155 cond = func->curblock;
2159 if (self->on_true) {
2160 /* create on-true block */
2161 ontrue = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "ontrue"));
2165 /* enter the block */
2166 func->curblock = ontrue;
2169 cgen = self->on_true->expression.codegen;
2170 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &dummy))
2173 /* we now need to work from the current endpoint */
2174 ontrue_endblock = func->curblock;
2179 if (self->on_false) {
2180 /* create on-false block */
2181 onfalse = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "onfalse"));
2185 /* enter the block */
2186 func->curblock = onfalse;
2189 cgen = self->on_false->expression.codegen;
2190 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &dummy))
2193 /* we now need to work from the current endpoint */
2194 onfalse_endblock = func->curblock;
2198 /* Merge block were they all merge in to */
2199 if (!ontrue || !onfalse || !ontrue_endblock->final || !onfalse_endblock->final)
2201 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "endif"));
2204 /* add jumps ot the merge block */
2205 if (ontrue && !ontrue_endblock->final && !ir_block_create_jump(ontrue_endblock, ast_ctx(self), merge))
2207 if (onfalse && !onfalse_endblock->final && !ir_block_create_jump(onfalse_endblock, ast_ctx(self), merge))
2210 /* Now enter the merge block */
2211 func->curblock = merge;
2214 /* we create the if here, that way all blocks are ordered :)
2216 if (!ir_block_create_if(cond, ast_ctx(self), condval,
2217 (ontrue ? ontrue : merge),
2218 (onfalse ? onfalse : merge)))
2226 bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_value **out)
2228 ast_expression_codegen *cgen;
2231 ir_value *trueval, *falseval;
2234 ir_block *cond = func->curblock;
2235 ir_block *cond_out = NULL;
2236 ir_block *ontrue, *ontrue_out = NULL;
2237 ir_block *onfalse, *onfalse_out = NULL;
2240 /* Ternary can never create an lvalue... */
2244 /* In theory it shouldn't be possible to pass through a node twice, but
2245 * in case we add any kind of optimization pass for the AST itself, it
2246 * may still happen, thus we remember a created ir_value and simply return one
2247 * if it already exists.
2249 if (self->expression.outr) {
2250 *out = self->expression.outr;
2254 /* In the following, contraty to ast_ifthen, we assume both paths exist. */
2256 /* generate the condition */
2257 func->curblock = cond;
2258 cgen = self->cond->expression.codegen;
2259 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2261 cond_out = func->curblock;
2263 /* create on-true block */
2264 ontrue = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_T"));
2269 /* enter the block */
2270 func->curblock = ontrue;
2273 cgen = self->on_true->expression.codegen;
2274 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &trueval))
2277 ontrue_out = func->curblock;
2280 /* create on-false block */
2281 onfalse = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_F"));
2286 /* enter the block */
2287 func->curblock = onfalse;
2290 cgen = self->on_false->expression.codegen;
2291 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &falseval))
2294 onfalse_out = func->curblock;
2297 /* create merge block */
2298 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_out"));
2301 /* jump to merge block */
2302 if (!ir_block_create_jump(ontrue_out, ast_ctx(self), merge))
2304 if (!ir_block_create_jump(onfalse_out, ast_ctx(self), merge))
2307 /* create if instruction */
2308 if (!ir_block_create_if(cond_out, ast_ctx(self), condval, ontrue, onfalse))
2311 /* Now enter the merge block */
2312 func->curblock = merge;
2314 /* Here, now, we need a PHI node
2315 * but first some sanity checking...
2317 if (trueval->vtype != falseval->vtype) {
2318 /* error("ternary with different types on the two sides"); */
2323 phi = ir_block_create_phi(merge, ast_ctx(self), ast_function_label(func, "phi"), trueval->vtype);
2326 ir_phi_add(phi, ontrue_out, trueval);
2327 ir_phi_add(phi, onfalse_out, falseval);
2329 self->expression.outr = ir_phi_value(phi);
2330 *out = self->expression.outr;
2335 bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value **out)
2337 ast_expression_codegen *cgen;
2339 ir_value *dummy = NULL;
2340 ir_value *precond = NULL;
2341 ir_value *postcond = NULL;
2343 /* Since we insert some jumps "late" so we have blocks
2344 * ordered "nicely", we need to keep track of the actual end-blocks
2345 * of expressions to add the jumps to.
2347 ir_block *bbody = NULL, *end_bbody = NULL;
2348 ir_block *bprecond = NULL, *end_bprecond = NULL;
2349 ir_block *bpostcond = NULL, *end_bpostcond = NULL;
2350 ir_block *bincrement = NULL, *end_bincrement = NULL;
2351 ir_block *bout = NULL, *bin = NULL;
2353 /* let's at least move the outgoing block to the end */
2356 /* 'break' and 'continue' need to be able to find the right blocks */
2357 ir_block *bcontinue = NULL;
2358 ir_block *bbreak = NULL;
2360 ir_block *old_bcontinue = NULL;
2361 ir_block *old_bbreak = NULL;
2363 ir_block *tmpblock = NULL;
2368 if (self->expression.outr) {
2369 compile_error(ast_ctx(self), "internal error: ast_loop cannot be reused, it bears no result!");
2372 self->expression.outr = (ir_value*)1;
2375 * Should we ever need some kind of block ordering, better make this function
2376 * move blocks around than write a block ordering algorithm later... after all
2377 * the ast and ir should work together, not against each other.
2380 /* initexpr doesn't get its own block, it's pointless, it could create more blocks
2381 * anyway if for example it contains a ternary.
2385 cgen = self->initexpr->expression.codegen;
2386 if (!(*cgen)((ast_expression*)(self->initexpr), func, false, &dummy))
2390 /* Store the block from which we enter this chaos */
2391 bin = func->curblock;
2393 /* The pre-loop condition needs its own block since we
2394 * need to be able to jump to the start of that expression.
2398 bprecond = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "pre_loop_cond"));
2402 /* the pre-loop-condition the least important place to 'continue' at */
2403 bcontinue = bprecond;
2406 func->curblock = bprecond;
2409 cgen = self->precond->expression.codegen;
2410 if (!(*cgen)((ast_expression*)(self->precond), func, false, &precond))
2413 end_bprecond = func->curblock;
2415 bprecond = end_bprecond = NULL;
2418 /* Now the next blocks won't be ordered nicely, but we need to
2419 * generate them this early for 'break' and 'continue'.
2421 if (self->increment) {
2422 bincrement = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "loop_increment"));
2425 bcontinue = bincrement; /* increment comes before the pre-loop-condition */
2427 bincrement = end_bincrement = NULL;
2430 if (self->postcond) {
2431 bpostcond = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "post_loop_cond"));
2434 bcontinue = bpostcond; /* postcond comes before the increment */
2436 bpostcond = end_bpostcond = NULL;
2439 bout_id = vec_size(func->ir_func->blocks);
2440 bout = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "after_loop"));
2445 /* The loop body... */
2446 /* if (self->body) */
2448 bbody = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "loop_body"));
2453 func->curblock = bbody;
2455 old_bbreak = func->breakblock;
2456 old_bcontinue = func->continueblock;
2457 func->breakblock = bbreak;
2458 func->continueblock = bcontinue;
2459 if (!func->continueblock)
2460 func->continueblock = bbody;
2464 cgen = self->body->expression.codegen;
2465 if (!(*cgen)((ast_expression*)(self->body), func, false, &dummy))
2469 end_bbody = func->curblock;
2470 func->breakblock = old_bbreak;
2471 func->continueblock = old_bcontinue;
2474 /* post-loop-condition */
2478 func->curblock = bpostcond;
2481 cgen = self->postcond->expression.codegen;
2482 if (!(*cgen)((ast_expression*)(self->postcond), func, false, &postcond))
2485 end_bpostcond = func->curblock;
2488 /* The incrementor */
2489 if (self->increment)
2492 func->curblock = bincrement;
2495 cgen = self->increment->expression.codegen;
2496 if (!(*cgen)((ast_expression*)(self->increment), func, false, &dummy))
2499 end_bincrement = func->curblock;
2502 /* In any case now, we continue from the outgoing block */
2503 func->curblock = bout;
2505 /* Now all blocks are in place */
2506 /* From 'bin' we jump to whatever comes first */
2507 if (bprecond) tmpblock = bprecond;
2508 else if (bbody) tmpblock = bbody;
2509 else if (bpostcond) tmpblock = bpostcond;
2510 else tmpblock = bout;
2511 if (!ir_block_create_jump(bin, ast_ctx(self), tmpblock))
2517 ir_block *ontrue, *onfalse;
2518 if (bbody) ontrue = bbody;
2519 else if (bincrement) ontrue = bincrement;
2520 else if (bpostcond) ontrue = bpostcond;
2521 else ontrue = bprecond;
2523 if (!ir_block_create_if(end_bprecond, ast_ctx(self), precond, ontrue, onfalse))
2530 if (bincrement) tmpblock = bincrement;
2531 else if (bpostcond) tmpblock = bpostcond;
2532 else if (bprecond) tmpblock = bprecond;
2533 else tmpblock = bbody;
2534 if (!end_bbody->final && !ir_block_create_jump(end_bbody, ast_ctx(self), tmpblock))
2538 /* from increment */
2541 if (bpostcond) tmpblock = bpostcond;
2542 else if (bprecond) tmpblock = bprecond;
2543 else if (bbody) tmpblock = bbody;
2544 else tmpblock = bout;
2545 if (!ir_block_create_jump(end_bincrement, ast_ctx(self), tmpblock))
2552 ir_block *ontrue, *onfalse;
2553 if (bprecond) ontrue = bprecond;
2554 else if (bbody) ontrue = bbody;
2555 else if (bincrement) ontrue = bincrement;
2556 else ontrue = bpostcond;
2558 if (!ir_block_create_if(end_bpostcond, ast_ctx(self), postcond, ontrue, onfalse))
2562 /* Move 'bout' to the end */
2563 vec_remove(func->ir_func->blocks, bout_id, 1);
2564 vec_push(func->ir_func->blocks, bout);
2569 bool ast_breakcont_codegen(ast_breakcont *self, ast_function *func, bool lvalue, ir_value **out)
2576 compile_error(ast_ctx(self), "break/continue expression is not an l-value");
2580 if (self->expression.outr) {
2581 compile_error(ast_ctx(self), "internal error: ast_breakcont cannot be reused!");
2584 self->expression.outr = (ir_value*)1;
2586 if (self->is_continue)
2587 target = func->continueblock;
2589 target = func->breakblock;
2592 compile_error(ast_ctx(self), "%s is lacking a target block", (self->is_continue ? "continue" : "break"));
2596 if (!ir_block_create_jump(func->curblock, ast_ctx(self), target))
2601 bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_value **out)
2603 ast_expression_codegen *cgen;
2605 ast_switch_case *def_case = NULL;
2606 ir_block *def_bfall = NULL;
2608 ir_value *dummy = NULL;
2609 ir_value *irop = NULL;
2610 ir_block *old_break = NULL;
2611 ir_block *bout = NULL;
2612 ir_block *bfall = NULL;
2620 compile_error(ast_ctx(self), "switch expression is not an l-value");
2624 if (self->expression.outr) {
2625 compile_error(ast_ctx(self), "internal error: ast_switch cannot be reused!");
2628 self->expression.outr = (ir_value*)1;
2633 cgen = self->operand->expression.codegen;
2634 if (!(*cgen)((ast_expression*)(self->operand), func, false, &irop))
2637 if (!vec_size(self->cases))
2640 cmpinstr = type_eq_instr[irop->vtype];
2641 if (cmpinstr >= AINSTR_END) {
2642 ast_type_to_string(self->operand, typestr, sizeof(typestr));
2643 compile_error(ast_ctx(self), "invalid type to perform a switch on: %s", typestr);
2647 bout_id = vec_size(func->ir_func->blocks);
2648 bout = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "after_switch"));
2652 /* setup the break block */
2653 old_break = func->breakblock;
2654 func->breakblock = bout;
2656 /* Now create all cases */
2657 for (c = 0; c < vec_size(self->cases); ++c) {
2658 ir_value *cond, *val;
2659 ir_block *bcase, *bnot;
2662 ast_switch_case *swcase = &self->cases[c];
2664 if (swcase->value) {
2665 /* A regular case */
2666 /* generate the condition operand */
2667 cgen = swcase->value->expression.codegen;
2668 if (!(*cgen)((ast_expression*)(swcase->value), func, false, &val))
2670 /* generate the condition */
2671 cond = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "switch_eq"), cmpinstr, irop, val);
2675 bcase = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "case"));
2676 bnot_id = vec_size(func->ir_func->blocks);
2677 bnot = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "not_case"));
2678 if (!bcase || !bnot)
2680 if (!ir_block_create_if(func->curblock, ast_ctx(self), cond, bcase, bnot))
2683 /* Make the previous case-end fall through */
2684 if (bfall && !bfall->final) {
2685 if (!ir_block_create_jump(bfall, ast_ctx(self), bcase))
2689 /* enter the case */
2690 func->curblock = bcase;
2691 cgen = swcase->code->expression.codegen;
2692 if (!(*cgen)((ast_expression*)swcase->code, func, false, &dummy))
2695 /* remember this block to fall through from */
2696 bfall = func->curblock;
2698 /* enter the else and move it down */
2699 func->curblock = bnot;
2700 vec_remove(func->ir_func->blocks, bnot_id, 1);
2701 vec_push(func->ir_func->blocks, bnot);
2703 /* The default case */
2704 /* Remember where to fall through from: */
2707 /* remember which case it was */
2712 /* Jump from the last bnot to bout */
2713 if (bfall && !bfall->final && !ir_block_create_jump(bfall, ast_ctx(self), bout)) {
2715 astwarning(ast_ctx(bfall), WARN_???, "missing break after last case");
2720 /* If there was a default case, put it down here */
2724 /* No need to create an extra block */
2725 bcase = func->curblock;
2727 /* Insert the fallthrough jump */
2728 if (def_bfall && !def_bfall->final) {
2729 if (!ir_block_create_jump(def_bfall, ast_ctx(self), bcase))
2733 /* Now generate the default code */
2734 cgen = def_case->code->expression.codegen;
2735 if (!(*cgen)((ast_expression*)def_case->code, func, false, &dummy))
2739 /* Jump from the last bnot to bout */
2740 if (!func->curblock->final && !ir_block_create_jump(func->curblock, ast_ctx(self), bout))
2742 /* enter the outgoing block */
2743 func->curblock = bout;
2745 /* restore the break block */
2746 func->breakblock = old_break;
2748 /* Move 'bout' to the end, it's nicer */
2749 vec_remove(func->ir_func->blocks, bout_id, 1);
2750 vec_push(func->ir_func->blocks, bout);
2755 bool ast_label_codegen(ast_label *self, ast_function *func, bool lvalue, ir_value **out)
2762 compile_error(ast_ctx(self), "internal error: ast_label cannot be an lvalue");
2766 /* simply create a new block and jump to it */
2767 self->irblock = ir_function_create_block(ast_ctx(self), func->ir_func, self->name);
2768 if (!self->irblock) {
2769 compile_error(ast_ctx(self), "failed to allocate label block `%s`", self->name);
2772 if (!func->curblock->final) {
2773 if (!ir_block_create_jump(func->curblock, ast_ctx(self), self->irblock))
2777 /* enter the new block */
2778 func->curblock = self->irblock;
2780 /* Generate all the leftover gotos */
2781 for (i = 0; i < vec_size(self->gotos); ++i) {
2782 if (!ast_goto_codegen(self->gotos[i], func, false, &dummy))
2789 bool ast_goto_codegen(ast_goto *self, ast_function *func, bool lvalue, ir_value **out)
2793 compile_error(ast_ctx(self), "internal error: ast_goto cannot be an lvalue");
2797 if (self->target->irblock) {
2798 if (self->irblock_from) {
2799 /* we already tried once, this is the callback */
2800 self->irblock_from->final = false;
2801 if (!ir_block_create_jump(self->irblock_from, ast_ctx(self), self->target->irblock)) {
2802 compile_error(ast_ctx(self), "failed to generate goto to `%s`", self->name);
2808 if (!ir_block_create_jump(func->curblock, ast_ctx(self), self->target->irblock)) {
2809 compile_error(ast_ctx(self), "failed to generate goto to `%s`", self->name);
2816 /* the target has not yet been created...
2817 * close this block in a sneaky way:
2819 func->curblock->final = true;
2820 self->irblock_from = func->curblock;
2821 ast_label_register_goto(self->target, self);
2827 bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value **out)
2829 ast_expression_codegen *cgen;
2831 ir_instr *callinstr;
2834 ir_value *funval = NULL;
2836 /* return values are never lvalues */
2838 compile_error(ast_ctx(self), "not an l-value (function call)");
2842 if (self->expression.outr) {
2843 *out = self->expression.outr;
2847 cgen = self->func->expression.codegen;
2848 if (!(*cgen)((ast_expression*)(self->func), func, false, &funval))
2856 for (i = 0; i < vec_size(self->params); ++i)
2859 ast_expression *expr = self->params[i];
2861 cgen = expr->expression.codegen;
2862 if (!(*cgen)(expr, func, false, ¶m))
2866 vec_push(params, param);
2869 callinstr = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "call"), funval);
2873 for (i = 0; i < vec_size(params); ++i) {
2874 ir_call_param(callinstr, params[i]);
2877 *out = ir_call_value(callinstr);
2878 self->expression.outr = *out;
2880 if ((*out)->vtype == TYPE_FIELD)
2881 (*out)->fieldtype = self->expression.next->expression.vtype;
2882 if ((*out)->vtype == TYPE_FUNCTION)
2883 (*out)->outtype = self->expression.next->expression.vtype;