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;
326 self->hasvalue = false;
328 memset(&self->constval, 0, sizeof(self->constval));
331 self->ir_values = NULL;
332 self->ir_value_count = 0;
340 void ast_value_delete(ast_value* self)
343 mem_d((void*)self->name);
344 if (self->hasvalue) {
345 switch (self->expression.vtype)
348 mem_d((void*)self->constval.vstring);
351 /* unlink us from the function node */
352 self->constval.vfunc->vtype = NULL;
354 /* NOTE: delete function? currently collected in
355 * the parser structure
362 mem_d(self->ir_values);
363 ast_expression_delete((ast_expression*)self);
367 void ast_value_params_add(ast_value *self, ast_value *p)
369 vec_push(self->expression.params, p);
372 bool ast_value_set_name(ast_value *self, const char *name)
375 mem_d((void*)self->name);
376 self->name = util_strdup(name);
380 ast_binary* ast_binary_new(lex_ctx ctx, int op,
381 ast_expression* left, ast_expression* right)
383 ast_instantiate(ast_binary, ctx, ast_binary_delete);
384 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binary_codegen);
390 ast_propagate_effects(self, left);
391 ast_propagate_effects(self, right);
393 if (op >= INSTR_EQ_F && op <= INSTR_GT)
394 self->expression.vtype = TYPE_FLOAT;
395 else if (op == INSTR_AND || op == INSTR_OR ||
396 op == INSTR_BITAND || op == INSTR_BITOR)
397 self->expression.vtype = TYPE_FLOAT;
398 else if (op == INSTR_MUL_VF || op == INSTR_MUL_FV)
399 self->expression.vtype = TYPE_VECTOR;
400 else if (op == INSTR_MUL_V)
401 self->expression.vtype = TYPE_FLOAT;
403 self->expression.vtype = left->expression.vtype;
408 void ast_binary_delete(ast_binary *self)
410 ast_unref(self->left);
411 ast_unref(self->right);
412 ast_expression_delete((ast_expression*)self);
416 ast_binstore* ast_binstore_new(lex_ctx ctx, int storop, int op,
417 ast_expression* left, ast_expression* right)
419 ast_instantiate(ast_binstore, ctx, ast_binstore_delete);
420 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binstore_codegen);
422 ast_side_effects(self) = true;
424 self->opstore = storop;
427 self->source = right;
429 self->keep_dest = false;
431 self->expression.vtype = left->expression.vtype;
432 if (left->expression.next) {
433 self->expression.next = ast_type_copy(ctx, left);
434 if (!self->expression.next) {
440 self->expression.next = NULL;
445 void ast_binstore_delete(ast_binstore *self)
447 if (!self->keep_dest)
448 ast_unref(self->dest);
449 ast_unref(self->source);
450 ast_expression_delete((ast_expression*)self);
454 ast_unary* ast_unary_new(lex_ctx ctx, int op,
455 ast_expression *expr)
457 ast_instantiate(ast_unary, ctx, ast_unary_delete);
458 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_unary_codegen);
461 self->operand = expr;
463 ast_propagate_effects(self, expr);
465 if (op >= INSTR_NOT_F && op <= INSTR_NOT_FNC) {
466 self->expression.vtype = TYPE_FLOAT;
468 compile_error(ctx, "cannot determine type of unary operation %s", asm_instr[op].m);
473 void ast_unary_delete(ast_unary *self)
475 ast_unref(self->operand);
476 ast_expression_delete((ast_expression*)self);
480 ast_return* ast_return_new(lex_ctx ctx, ast_expression *expr)
482 ast_instantiate(ast_return, ctx, ast_return_delete);
483 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_return_codegen);
485 self->operand = expr;
488 ast_propagate_effects(self, expr);
493 void ast_return_delete(ast_return *self)
496 ast_unref(self->operand);
497 ast_expression_delete((ast_expression*)self);
501 ast_entfield* ast_entfield_new(lex_ctx ctx, ast_expression *entity, ast_expression *field)
503 if (field->expression.vtype != TYPE_FIELD) {
504 compile_error(ctx, "ast_entfield_new with expression not of type field");
507 return ast_entfield_new_force(ctx, entity, field, field->expression.next);
510 ast_entfield* ast_entfield_new_force(lex_ctx ctx, ast_expression *entity, ast_expression *field, const ast_expression *outtype)
512 ast_instantiate(ast_entfield, ctx, ast_entfield_delete);
516 /* Error: field has no type... */
520 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_entfield_codegen);
522 self->entity = entity;
524 ast_propagate_effects(self, entity);
525 ast_propagate_effects(self, field);
527 if (!ast_type_adopt(self, outtype)) {
528 ast_entfield_delete(self);
535 void ast_entfield_delete(ast_entfield *self)
537 ast_unref(self->entity);
538 ast_unref(self->field);
539 ast_expression_delete((ast_expression*)self);
543 ast_member* ast_member_new(lex_ctx ctx, ast_expression *owner, unsigned int field, const char *name)
545 ast_instantiate(ast_member, ctx, ast_member_delete);
551 if (owner->expression.vtype != TYPE_VECTOR &&
552 owner->expression.vtype != TYPE_FIELD) {
553 compile_error(ctx, "member-access on an invalid owner of type %s", type_name[owner->expression.vtype]);
558 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_member_codegen);
559 self->expression.node.keep = true; /* keep */
561 if (owner->expression.vtype == TYPE_VECTOR) {
562 self->expression.vtype = TYPE_FLOAT;
563 self->expression.next = NULL;
565 self->expression.vtype = TYPE_FIELD;
566 self->expression.next = ast_shallow_type(ctx, TYPE_FLOAT);
570 ast_propagate_effects(self, owner);
574 self->name = util_strdup(name);
581 void ast_member_delete(ast_member *self)
583 /* The owner is always an ast_value, which has .keep=true,
584 * also: ast_members are usually deleted after the owner, thus
585 * this will cause invalid access
586 ast_unref(self->owner);
587 * once we allow (expression).x to access a vector-member, we need
588 * to change this: preferably by creating an alternate ast node for this
589 * purpose that is not garbage-collected.
591 ast_expression_delete((ast_expression*)self);
595 ast_array_index* ast_array_index_new(lex_ctx ctx, ast_expression *array, ast_expression *index)
597 ast_expression *outtype;
598 ast_instantiate(ast_array_index, ctx, ast_array_index_delete);
600 outtype = array->expression.next;
603 /* Error: field has no type... */
607 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_array_index_codegen);
611 ast_propagate_effects(self, array);
612 ast_propagate_effects(self, index);
614 if (!ast_type_adopt(self, outtype)) {
615 ast_array_index_delete(self);
618 if (array->expression.vtype == TYPE_FIELD && outtype->expression.vtype == TYPE_ARRAY) {
619 if (self->expression.vtype != TYPE_ARRAY) {
620 compile_error(ast_ctx(self), "array_index node on type");
621 ast_array_index_delete(self);
624 self->array = outtype;
625 self->expression.vtype = TYPE_FIELD;
631 void ast_array_index_delete(ast_array_index *self)
633 ast_unref(self->array);
634 ast_unref(self->index);
635 ast_expression_delete((ast_expression*)self);
639 ast_ifthen* ast_ifthen_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
641 ast_instantiate(ast_ifthen, ctx, ast_ifthen_delete);
642 if (!ontrue && !onfalse) {
643 /* because it is invalid */
647 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ifthen_codegen);
650 self->on_true = ontrue;
651 self->on_false = onfalse;
652 ast_propagate_effects(self, cond);
654 ast_propagate_effects(self, ontrue);
656 ast_propagate_effects(self, onfalse);
661 void ast_ifthen_delete(ast_ifthen *self)
663 ast_unref(self->cond);
665 ast_unref(self->on_true);
667 ast_unref(self->on_false);
668 ast_expression_delete((ast_expression*)self);
672 ast_ternary* ast_ternary_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
674 ast_instantiate(ast_ternary, ctx, ast_ternary_delete);
675 /* This time NEITHER must be NULL */
676 if (!ontrue || !onfalse) {
680 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ternary_codegen);
683 self->on_true = ontrue;
684 self->on_false = onfalse;
685 ast_propagate_effects(self, cond);
686 ast_propagate_effects(self, ontrue);
687 ast_propagate_effects(self, onfalse);
689 if (!ast_type_adopt(self, ontrue)) {
690 ast_ternary_delete(self);
697 void ast_ternary_delete(ast_ternary *self)
699 ast_unref(self->cond);
700 ast_unref(self->on_true);
701 ast_unref(self->on_false);
702 ast_expression_delete((ast_expression*)self);
706 ast_loop* ast_loop_new(lex_ctx ctx,
707 ast_expression *initexpr,
708 ast_expression *precond,
709 ast_expression *postcond,
710 ast_expression *increment,
711 ast_expression *body)
713 ast_instantiate(ast_loop, ctx, ast_loop_delete);
714 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_loop_codegen);
716 self->initexpr = initexpr;
717 self->precond = precond;
718 self->postcond = postcond;
719 self->increment = increment;
723 ast_propagate_effects(self, initexpr);
725 ast_propagate_effects(self, precond);
727 ast_propagate_effects(self, postcond);
729 ast_propagate_effects(self, increment);
731 ast_propagate_effects(self, body);
736 void ast_loop_delete(ast_loop *self)
739 ast_unref(self->initexpr);
741 ast_unref(self->precond);
743 ast_unref(self->postcond);
745 ast_unref(self->increment);
747 ast_unref(self->body);
748 ast_expression_delete((ast_expression*)self);
752 ast_breakcont* ast_breakcont_new(lex_ctx ctx, bool iscont)
754 ast_instantiate(ast_breakcont, ctx, ast_breakcont_delete);
755 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_breakcont_codegen);
757 self->is_continue = iscont;
762 void ast_breakcont_delete(ast_breakcont *self)
764 ast_expression_delete((ast_expression*)self);
768 ast_switch* ast_switch_new(lex_ctx ctx, ast_expression *op)
770 ast_instantiate(ast_switch, ctx, ast_switch_delete);
771 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_switch_codegen);
776 ast_propagate_effects(self, op);
781 void ast_switch_delete(ast_switch *self)
784 ast_unref(self->operand);
786 for (i = 0; i < vec_size(self->cases); ++i) {
787 if (self->cases[i].value)
788 ast_unref(self->cases[i].value);
789 ast_unref(self->cases[i].code);
791 vec_free(self->cases);
793 ast_expression_delete((ast_expression*)self);
797 ast_label* ast_label_new(lex_ctx ctx, const char *name)
799 ast_instantiate(ast_label, ctx, ast_label_delete);
800 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_label_codegen);
802 self->name = util_strdup(name);
803 self->irblock = NULL;
809 void ast_label_delete(ast_label *self)
811 mem_d((void*)self->name);
812 vec_free(self->gotos);
813 ast_expression_delete((ast_expression*)self);
817 void ast_label_register_goto(ast_label *self, ast_goto *g)
819 vec_push(self->gotos, g);
822 ast_goto* ast_goto_new(lex_ctx ctx, const char *name)
824 ast_instantiate(ast_goto, ctx, ast_goto_delete);
825 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_goto_codegen);
827 self->name = util_strdup(name);
829 self->irblock_from = NULL;
834 void ast_goto_delete(ast_goto *self)
836 mem_d((void*)self->name);
837 ast_expression_delete((ast_expression*)self);
841 void ast_goto_set_label(ast_goto *self, ast_label *label)
843 self->target = label;
846 ast_call* ast_call_new(lex_ctx ctx,
847 ast_expression *funcexpr)
849 ast_instantiate(ast_call, ctx, ast_call_delete);
850 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_call_codegen);
852 ast_side_effects(self) = true;
855 self->func = funcexpr;
857 self->expression.vtype = funcexpr->expression.next->expression.vtype;
858 if (funcexpr->expression.next->expression.next)
859 self->expression.next = ast_type_copy(ctx, funcexpr->expression.next->expression.next);
864 void ast_call_delete(ast_call *self)
867 for (i = 0; i < vec_size(self->params); ++i)
868 ast_unref(self->params[i]);
869 vec_free(self->params);
872 ast_unref(self->func);
874 ast_expression_delete((ast_expression*)self);
878 bool ast_call_check_types(ast_call *self)
882 const ast_expression *func = self->func;
883 size_t count = vec_size(self->params);
884 if (count > vec_size(func->expression.params))
885 count = vec_size(func->expression.params);
887 for (i = 0; i < count; ++i) {
888 if (!ast_compare_type(self->params[i], (ast_expression*)(func->expression.params[i]))) {
891 ast_type_to_string(self->params[i], tgot, sizeof(tgot));
892 ast_type_to_string((ast_expression*)func->expression.params[i], texp, sizeof(texp));
893 compile_error(ast_ctx(self), "invalid type for parameter %u in function call: expected %s, got %s",
894 (unsigned int)(i+1), texp, tgot);
895 /* we don't immediately return */
902 ast_store* ast_store_new(lex_ctx ctx, int op,
903 ast_expression *dest, ast_expression *source)
905 ast_instantiate(ast_store, ctx, ast_store_delete);
906 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_store_codegen);
908 ast_side_effects(self) = true;
912 self->source = source;
914 self->expression.vtype = dest->expression.vtype;
915 if (dest->expression.next) {
916 self->expression.next = ast_type_copy(ctx, dest);
917 if (!self->expression.next) {
923 self->expression.next = NULL;
928 void ast_store_delete(ast_store *self)
930 ast_unref(self->dest);
931 ast_unref(self->source);
932 ast_expression_delete((ast_expression*)self);
936 ast_block* ast_block_new(lex_ctx ctx)
938 ast_instantiate(ast_block, ctx, ast_block_delete);
939 ast_expression_init((ast_expression*)self,
940 (ast_expression_codegen*)&ast_block_codegen);
944 self->collect = NULL;
949 void ast_block_add_expr(ast_block *self, ast_expression *e)
951 ast_propagate_effects(self, e);
952 vec_push(self->exprs, e);
955 void ast_block_collect(ast_block *self, ast_expression *expr)
957 vec_push(self->collect, expr);
958 expr->expression.node.keep = true;
961 void ast_block_delete(ast_block *self)
964 for (i = 0; i < vec_size(self->exprs); ++i)
965 ast_unref(self->exprs[i]);
966 vec_free(self->exprs);
967 for (i = 0; i < vec_size(self->locals); ++i)
968 ast_delete(self->locals[i]);
969 vec_free(self->locals);
970 for (i = 0; i < vec_size(self->collect); ++i)
971 ast_delete(self->collect[i]);
972 vec_free(self->collect);
973 ast_expression_delete((ast_expression*)self);
977 bool ast_block_set_type(ast_block *self, ast_expression *from)
979 if (self->expression.next)
980 ast_delete(self->expression.next);
981 self->expression.vtype = from->expression.vtype;
982 if (from->expression.next) {
983 self->expression.next = ast_type_copy(self->expression.node.context, from->expression.next);
984 if (!self->expression.next)
988 self->expression.next = NULL;
992 ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype)
994 ast_instantiate(ast_function, ctx, ast_function_delete);
998 vtype->expression.vtype != TYPE_FUNCTION)
1000 compile_error(ast_ctx(self), "internal error: ast_function_new condition %i %i type=%i",
1002 (int)vtype->hasvalue,
1003 vtype->expression.vtype);
1008 self->vtype = vtype;
1009 self->name = name ? util_strdup(name) : NULL;
1010 self->blocks = NULL;
1012 self->labelcount = 0;
1015 self->ir_func = NULL;
1016 self->curblock = NULL;
1018 self->breakblock = NULL;
1019 self->continueblock = NULL;
1021 vtype->hasvalue = true;
1022 vtype->constval.vfunc = self;
1027 void ast_function_delete(ast_function *self)
1031 mem_d((void*)self->name);
1033 /* ast_value_delete(self->vtype); */
1034 self->vtype->hasvalue = false;
1035 self->vtype->constval.vfunc = NULL;
1036 /* We use unref - if it was stored in a global table it is supposed
1037 * to be deleted from *there*
1039 ast_unref(self->vtype);
1041 for (i = 0; i < vec_size(self->blocks); ++i)
1042 ast_delete(self->blocks[i]);
1043 vec_free(self->blocks);
1047 const char* ast_function_label(ast_function *self, const char *prefix)
1053 if (!opts_dump && !opts_dumpfin)
1056 id = (self->labelcount++);
1057 len = strlen(prefix);
1059 from = self->labelbuf + sizeof(self->labelbuf)-1;
1062 unsigned int digit = id % 10;
1063 *from = digit + '0';
1066 memcpy(from - len, prefix, len);
1070 /*********************************************************************/
1072 * by convention you must never pass NULL to the 'ir_value **out'
1073 * parameter. If you really don't care about the output, pass a dummy.
1074 * But I can't imagine a pituation where the output is truly unnecessary.
1077 bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out)
1081 /* NOTE: This is the codegen for a variable used in an expression.
1082 * It is not the codegen to generate the value. For this purpose,
1083 * ast_local_codegen and ast_global_codegen are to be used before this
1084 * is executed. ast_function_codegen should take care of its locals,
1085 * and the ast-user should take care of ast_global_codegen to be used
1086 * on all the globals.
1089 char typename[1024];
1090 ast_type_to_string((ast_expression*)self, typename, sizeof(typename));
1091 compile_error(ast_ctx(self), "ast_value used before generated %s %s", typename, self->name);
1098 bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
1102 if (self->hasvalue && self->expression.vtype == TYPE_FUNCTION)
1104 ir_function *func = ir_builder_create_function(ir, self->name, self->expression.next->expression.vtype);
1107 func->context = ast_ctx(self);
1108 func->value->context = ast_ctx(self);
1110 self->constval.vfunc->ir_func = func;
1111 self->ir_v = func->value;
1112 /* The function is filled later on ast_function_codegen... */
1116 if (isfield && self->expression.vtype == TYPE_FIELD) {
1117 ast_expression *fieldtype = self->expression.next;
1119 if (self->hasvalue) {
1120 compile_error(ast_ctx(self), "TODO: constant field pointers with value");
1124 if (fieldtype->expression.vtype == TYPE_ARRAY) {
1129 ast_expression_common *elemtype;
1131 ast_value *array = (ast_value*)fieldtype;
1133 if (!ast_istype(fieldtype, ast_value)) {
1134 compile_error(ast_ctx(self), "internal error: ast_value required");
1138 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1139 if (!array->expression.count || array->expression.count > opts_max_array_size)
1140 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)array->expression.count);
1142 elemtype = &array->expression.next->expression;
1143 vtype = elemtype->vtype;
1145 v = ir_builder_create_field(ir, self->name, vtype);
1147 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", self->name);
1150 if (vtype == TYPE_FIELD)
1151 v->fieldtype = elemtype->next->expression.vtype;
1152 v->context = ast_ctx(self);
1153 array->ir_v = self->ir_v = v;
1155 namelen = strlen(self->name);
1156 name = (char*)mem_a(namelen + 16);
1157 strcpy(name, self->name);
1159 array->ir_values = (ir_value**)mem_a(sizeof(array->ir_values[0]) * array->expression.count);
1160 array->ir_values[0] = v;
1161 for (ai = 1; ai < array->expression.count; ++ai) {
1162 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1163 array->ir_values[ai] = ir_builder_create_field(ir, name, vtype);
1164 if (!array->ir_values[ai]) {
1166 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", name);
1169 if (vtype == TYPE_FIELD)
1170 array->ir_values[ai]->fieldtype = elemtype->next->expression.vtype;
1171 array->ir_values[ai]->context = ast_ctx(self);
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 if (vtype == TYPE_FIELD)
1204 v->fieldtype = elemtype->next->expression.vtype;
1205 v->context = ast_ctx(self);
1207 namelen = strlen(self->name);
1208 name = (char*)mem_a(namelen + 16);
1209 strcpy(name, self->name);
1211 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1212 self->ir_values[0] = v;
1213 for (ai = 1; ai < self->expression.count; ++ai) {
1214 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1215 self->ir_values[ai] = ir_builder_create_global(ir, name, vtype);
1216 if (!self->ir_values[ai]) {
1218 compile_error(ast_ctx(self), "ir_builder_create_global failed `%s`", name);
1221 if (vtype == TYPE_FIELD)
1222 self->ir_values[ai]->fieldtype = elemtype->next->expression.vtype;
1223 self->ir_values[ai]->context = ast_ctx(self);
1229 /* Arrays don't do this since there's no "array" value which spans across the
1232 v = ir_builder_create_global(ir, self->name, self->expression.vtype);
1234 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", self->name);
1237 if (self->expression.vtype == TYPE_FIELD)
1238 v->fieldtype = self->expression.next->expression.vtype;
1239 v->context = ast_ctx(self);
1242 if (self->hasvalue) {
1243 switch (self->expression.vtype)
1246 if (!ir_value_set_float(v, self->constval.vfloat))
1250 if (!ir_value_set_vector(v, self->constval.vvec))
1254 if (!ir_value_set_string(v, self->constval.vstring))
1258 compile_error(ast_ctx(self), "TODO: global constant array");
1261 compile_error(ast_ctx(self), "global of type function not properly generated");
1263 /* Cannot generate an IR value for a function,
1264 * need a pointer pointing to a function rather.
1267 compile_error(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1272 /* link us to the ir_value */
1277 error: /* clean up */
1282 bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
1285 if (self->hasvalue && self->expression.vtype == TYPE_FUNCTION)
1287 /* Do we allow local functions? I think not...
1288 * this is NOT a function pointer atm.
1293 if (self->expression.vtype == TYPE_ARRAY) {
1298 ast_expression_common *elemtype = &self->expression.next->expression;
1299 int vtype = elemtype->vtype;
1302 compile_error(ast_ctx(self), "array-parameters are not supported");
1306 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1307 if (!self->expression.count || self->expression.count > opts_max_array_size) {
1308 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1311 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1312 if (!self->ir_values) {
1313 compile_error(ast_ctx(self), "failed to allocate array values");
1317 v = ir_function_create_local(func, self->name, vtype, param);
1319 compile_error(ast_ctx(self), "ir_function_create_local failed");
1322 if (vtype == TYPE_FIELD)
1323 v->fieldtype = elemtype->next->expression.vtype;
1324 v->context = ast_ctx(self);
1326 namelen = strlen(self->name);
1327 name = (char*)mem_a(namelen + 16);
1328 strcpy(name, self->name);
1330 self->ir_values[0] = v;
1331 for (ai = 1; ai < self->expression.count; ++ai) {
1332 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1333 self->ir_values[ai] = ir_function_create_local(func, name, vtype, param);
1334 if (!self->ir_values[ai]) {
1335 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", name);
1338 if (vtype == TYPE_FIELD)
1339 self->ir_values[ai]->fieldtype = elemtype->next->expression.vtype;
1340 self->ir_values[ai]->context = ast_ctx(self);
1345 v = ir_function_create_local(func, self->name, self->expression.vtype, param);
1348 if (self->expression.vtype == TYPE_FIELD)
1349 v->fieldtype = self->expression.next->expression.vtype;
1350 v->context = ast_ctx(self);
1353 /* A constant local... hmmm...
1354 * I suppose the IR will have to deal with this
1356 if (self->hasvalue) {
1357 switch (self->expression.vtype)
1360 if (!ir_value_set_float(v, self->constval.vfloat))
1364 if (!ir_value_set_vector(v, self->constval.vvec))
1368 if (!ir_value_set_string(v, self->constval.vstring))
1372 compile_error(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1377 /* link us to the ir_value */
1382 if (!ast_global_codegen(self->setter, func->owner, false) ||
1383 !ast_function_codegen(self->setter->constval.vfunc, func->owner) ||
1384 !ir_function_finalize(self->setter->constval.vfunc->ir_func))
1388 if (!ast_global_codegen(self->getter, func->owner, false) ||
1389 !ast_function_codegen(self->getter->constval.vfunc, func->owner) ||
1390 !ir_function_finalize(self->getter->constval.vfunc->ir_func))
1395 error: /* clean up */
1400 bool ast_function_codegen(ast_function *self, ir_builder *ir)
1404 ast_expression_common *ec;
1409 irf = self->ir_func;
1411 compile_error(ast_ctx(self), "ast_function's related ast_value was not generated yet");
1415 /* fill the parameter list */
1416 ec = &self->vtype->expression;
1417 for (i = 0; i < vec_size(ec->params); ++i)
1419 if (ec->params[i]->expression.vtype == TYPE_FIELD)
1420 vec_push(irf->params, ec->params[i]->expression.next->expression.vtype);
1422 vec_push(irf->params, ec->params[i]->expression.vtype);
1423 if (!self->builtin) {
1424 if (!ast_local_codegen(ec->params[i], self->ir_func, true))
1429 if (self->builtin) {
1430 irf->builtin = self->builtin;
1434 if (!vec_size(self->blocks)) {
1435 compile_error(ast_ctx(self), "function `%s` has no body", self->name);
1439 self->curblock = ir_function_create_block(ast_ctx(self), irf, "entry");
1440 if (!self->curblock) {
1441 compile_error(ast_ctx(self), "failed to allocate entry block for `%s`", self->name);
1445 for (i = 0; i < vec_size(self->blocks); ++i) {
1446 ast_expression_codegen *gen = self->blocks[i]->expression.codegen;
1447 if (!(*gen)((ast_expression*)self->blocks[i], self, false, &dummy))
1451 /* TODO: check return types */
1452 if (!self->curblock->is_return)
1454 if (!self->vtype->expression.next ||
1455 self->vtype->expression.next->expression.vtype == TYPE_VOID)
1457 return ir_block_create_return(self->curblock, ast_ctx(self), NULL);
1459 else if (vec_size(self->curblock->entries))
1461 /* error("missing return"); */
1462 if (compile_warning(ast_ctx(self), WARN_MISSING_RETURN_VALUES,
1463 "control reaches end of non-void function (`%s`) via %s",
1464 self->name, self->curblock->label))
1468 return ir_block_create_return(self->curblock, ast_ctx(self), NULL);
1474 /* Note, you will not see ast_block_codegen generate ir_blocks.
1475 * To the AST and the IR, blocks are 2 different things.
1476 * In the AST it represents a block of code, usually enclosed in
1477 * curly braces {...}.
1478 * While in the IR it represents a block in terms of control-flow.
1480 bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_value **out)
1484 /* We don't use this
1485 * Note: an ast-representation using the comma-operator
1486 * of the form: (a, b, c) = x should not assign to c...
1489 compile_error(ast_ctx(self), "not an l-value (code-block)");
1493 if (self->expression.outr) {
1494 *out = self->expression.outr;
1498 /* output is NULL at first, we'll have each expression
1499 * assign to out output, thus, a comma-operator represention
1500 * using an ast_block will return the last generated value,
1501 * so: (b, c) + a executed both b and c, and returns c,
1502 * which is then added to a.
1506 /* generate locals */
1507 for (i = 0; i < vec_size(self->locals); ++i)
1509 if (!ast_local_codegen(self->locals[i], func->ir_func, false)) {
1511 compile_error(ast_ctx(self), "failed to generate local `%s`", self->locals[i]->name);
1516 for (i = 0; i < vec_size(self->exprs); ++i)
1518 ast_expression_codegen *gen = self->exprs[i]->expression.codegen;
1519 if (func->curblock->final && !ast_istype(self->exprs[i], ast_label)) {
1520 compile_error(ast_ctx(self->exprs[i]), "unreachable statement");
1523 if (!(*gen)(self->exprs[i], func, false, out))
1527 self->expression.outr = *out;
1532 bool ast_store_codegen(ast_store *self, ast_function *func, bool lvalue, ir_value **out)
1534 ast_expression_codegen *cgen;
1535 ir_value *left = NULL;
1536 ir_value *right = NULL;
1540 ast_array_index *ai = NULL;
1542 if (lvalue && self->expression.outl) {
1543 *out = self->expression.outl;
1547 if (!lvalue && self->expression.outr) {
1548 *out = self->expression.outr;
1552 if (ast_istype(self->dest, ast_array_index))
1555 ai = (ast_array_index*)self->dest;
1556 idx = (ast_value*)ai->index;
1558 if (ast_istype(ai->index, ast_value) && idx->hasvalue && idx->cvq == CV_CONST)
1563 /* we need to call the setter */
1564 ir_value *iridx, *funval;
1568 compile_error(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1572 arr = (ast_value*)ai->array;
1573 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1574 compile_error(ast_ctx(self), "value has no setter (%s)", arr->name);
1578 cgen = idx->expression.codegen;
1579 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1582 cgen = arr->setter->expression.codegen;
1583 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
1586 cgen = self->source->expression.codegen;
1587 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1590 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "store"), funval);
1593 ir_call_param(call, iridx);
1594 ir_call_param(call, right);
1595 self->expression.outr = right;
1601 cgen = self->dest->expression.codegen;
1603 if (!(*cgen)((ast_expression*)(self->dest), func, true, &left))
1605 self->expression.outl = left;
1607 cgen = self->source->expression.codegen;
1609 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1612 if (!ir_block_create_store_op(func->curblock, ast_ctx(self), self->op, left, right))
1614 self->expression.outr = right;
1617 /* Theoretically, an assinment returns its left side as an
1618 * lvalue, if we don't need an lvalue though, we return
1619 * the right side as an rvalue, otherwise we have to
1620 * somehow know whether or not we need to dereference the pointer
1621 * on the left side - that is: OP_LOAD if it was an address.
1622 * Also: in original QC we cannot OP_LOADP *anyway*.
1624 *out = (lvalue ? left : right);
1629 bool ast_binary_codegen(ast_binary *self, ast_function *func, bool lvalue, ir_value **out)
1631 ast_expression_codegen *cgen;
1632 ir_value *left, *right;
1634 /* A binary operation cannot yield an l-value */
1636 compile_error(ast_ctx(self), "not an l-value (binop)");
1640 if (self->expression.outr) {
1641 *out = self->expression.outr;
1645 if (OPTS_FLAG(SHORT_LOGIC) &&
1646 (self->op == INSTR_AND || self->op == INSTR_OR))
1648 /* short circuit evaluation */
1649 ir_block *other, *merge;
1650 ir_block *from_left, *from_right;
1655 /* Note about casting to true boolean values:
1656 * We use a single NOT for sub expressions, and an
1657 * overall NOT at the end, and for that purpose swap
1658 * all the jump conditions in order for the NOT to get
1660 * ie: (a && b) usually becomes (!!a ? !!b : !!a)
1661 * but we translate this to (!(!a ? !a : !b))
1664 merge_id = vec_size(func->ir_func->blocks);
1665 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "sce_merge"));
1667 cgen = self->left->expression.codegen;
1668 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1670 if (!OPTS_FLAG(PERL_LOGIC)) {
1671 notop = type_not_instr[left->vtype];
1672 if (notop == AINSTR_END) {
1673 compile_error(ast_ctx(self), "don't know how to cast to bool...");
1676 left = ir_block_create_unary(func->curblock, ast_ctx(self),
1677 ast_function_label(func, "sce_not"),
1681 from_left = func->curblock;
1683 other = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "sce_other"));
1684 if ( !(self->op == INSTR_OR) != !OPTS_FLAG(PERL_LOGIC) ) {
1685 if (!ir_block_create_if(func->curblock, ast_ctx(self), left, other, merge))
1688 if (!ir_block_create_if(func->curblock, ast_ctx(self), left, merge, other))
1691 /* use the likely flag */
1692 vec_last(func->curblock->instr)->likely = true;
1694 func->curblock = other;
1695 cgen = self->right->expression.codegen;
1696 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1698 if (!OPTS_FLAG(PERL_LOGIC)) {
1699 notop = type_not_instr[right->vtype];
1700 if (notop == AINSTR_END) {
1701 compile_error(ast_ctx(self), "don't know how to cast to bool...");
1704 right = ir_block_create_unary(func->curblock, ast_ctx(self),
1705 ast_function_label(func, "sce_not"),
1709 from_right = func->curblock;
1711 if (!ir_block_create_jump(func->curblock, ast_ctx(self), merge))
1714 vec_remove(func->ir_func->blocks, merge_id, 1);
1715 vec_push(func->ir_func->blocks, merge);
1717 func->curblock = merge;
1718 phi = ir_block_create_phi(func->curblock, ast_ctx(self), ast_function_label(func, "sce_value"), TYPE_FLOAT);
1719 ir_phi_add(phi, from_left, left);
1720 ir_phi_add(phi, from_right, right);
1721 *out = ir_phi_value(phi);
1722 if (!OPTS_FLAG(PERL_LOGIC)) {
1723 notop = type_not_instr[(*out)->vtype];
1724 if (notop == AINSTR_END) {
1725 compile_error(ast_ctx(self), "don't know how to cast to bool...");
1728 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
1729 ast_function_label(func, "sce_final_not"),
1735 self->expression.outr = *out;
1739 cgen = self->left->expression.codegen;
1740 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1743 cgen = self->right->expression.codegen;
1744 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1747 *out = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "bin"),
1748 self->op, left, right);
1751 self->expression.outr = *out;
1756 bool ast_binstore_codegen(ast_binstore *self, ast_function *func, bool lvalue, ir_value **out)
1758 ast_expression_codegen *cgen;
1759 ir_value *leftl = NULL, *leftr, *right, *bin;
1763 ast_array_index *ai = NULL;
1764 ir_value *iridx = NULL;
1766 if (lvalue && self->expression.outl) {
1767 *out = self->expression.outl;
1771 if (!lvalue && self->expression.outr) {
1772 *out = self->expression.outr;
1776 if (ast_istype(self->dest, ast_array_index))
1779 ai = (ast_array_index*)self->dest;
1780 idx = (ast_value*)ai->index;
1782 if (ast_istype(ai->index, ast_value) && idx->hasvalue && idx->cvq == CV_CONST)
1786 /* for a binstore we need both an lvalue and an rvalue for the left side */
1787 /* rvalue of destination! */
1789 cgen = idx->expression.codegen;
1790 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1793 cgen = self->dest->expression.codegen;
1794 if (!(*cgen)((ast_expression*)(self->dest), func, false, &leftr))
1797 /* source as rvalue only */
1798 cgen = self->source->expression.codegen;
1799 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1802 /* now the binary */
1803 bin = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "binst"),
1804 self->opbin, leftr, right);
1805 self->expression.outr = bin;
1809 /* we need to call the setter */
1814 compile_error(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1818 arr = (ast_value*)ai->array;
1819 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1820 compile_error(ast_ctx(self), "value has no setter (%s)", arr->name);
1824 cgen = arr->setter->expression.codegen;
1825 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
1828 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "store"), funval);
1831 ir_call_param(call, iridx);
1832 ir_call_param(call, bin);
1833 self->expression.outr = bin;
1835 /* now store them */
1836 cgen = self->dest->expression.codegen;
1837 /* lvalue of destination */
1838 if (!(*cgen)((ast_expression*)(self->dest), func, true, &leftl))
1840 self->expression.outl = leftl;
1842 if (!ir_block_create_store_op(func->curblock, ast_ctx(self), self->opstore, leftl, bin))
1844 self->expression.outr = bin;
1847 /* Theoretically, an assinment returns its left side as an
1848 * lvalue, if we don't need an lvalue though, we return
1849 * the right side as an rvalue, otherwise we have to
1850 * somehow know whether or not we need to dereference the pointer
1851 * on the left side - that is: OP_LOAD if it was an address.
1852 * Also: in original QC we cannot OP_LOADP *anyway*.
1854 *out = (lvalue ? leftl : bin);
1859 bool ast_unary_codegen(ast_unary *self, ast_function *func, bool lvalue, ir_value **out)
1861 ast_expression_codegen *cgen;
1864 /* An unary operation cannot yield an l-value */
1866 compile_error(ast_ctx(self), "not an l-value (binop)");
1870 if (self->expression.outr) {
1871 *out = self->expression.outr;
1875 cgen = self->operand->expression.codegen;
1877 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
1880 *out = ir_block_create_unary(func->curblock, ast_ctx(self), ast_function_label(func, "unary"),
1884 self->expression.outr = *out;
1889 bool ast_return_codegen(ast_return *self, ast_function *func, bool lvalue, ir_value **out)
1891 ast_expression_codegen *cgen;
1896 /* In the context of a return operation, we don't actually return
1900 compile_error(ast_ctx(self), "return-expression is not an l-value");
1904 if (self->expression.outr) {
1905 compile_error(ast_ctx(self), "internal error: ast_return cannot be reused, it bears no result!");
1908 self->expression.outr = (ir_value*)1;
1910 if (self->operand) {
1911 cgen = self->operand->expression.codegen;
1913 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
1916 if (!ir_block_create_return(func->curblock, ast_ctx(self), operand))
1919 if (!ir_block_create_return(func->curblock, ast_ctx(self), NULL))
1926 bool ast_entfield_codegen(ast_entfield *self, ast_function *func, bool lvalue, ir_value **out)
1928 ast_expression_codegen *cgen;
1929 ir_value *ent, *field;
1931 /* This function needs to take the 'lvalue' flag into account!
1932 * As lvalue we provide a field-pointer, as rvalue we provide the
1936 if (lvalue && self->expression.outl) {
1937 *out = self->expression.outl;
1941 if (!lvalue && self->expression.outr) {
1942 *out = self->expression.outr;
1946 cgen = self->entity->expression.codegen;
1947 if (!(*cgen)((ast_expression*)(self->entity), func, false, &ent))
1950 cgen = self->field->expression.codegen;
1951 if (!(*cgen)((ast_expression*)(self->field), func, false, &field))
1956 *out = ir_block_create_fieldaddress(func->curblock, ast_ctx(self), ast_function_label(func, "efa"),
1959 *out = ir_block_create_load_from_ent(func->curblock, ast_ctx(self), ast_function_label(func, "efv"),
1960 ent, field, self->expression.vtype);
1963 compile_error(ast_ctx(self), "failed to create %s instruction (output type %s)",
1964 (lvalue ? "ADDRESS" : "FIELD"),
1965 type_name[self->expression.vtype]);
1970 self->expression.outl = *out;
1972 self->expression.outr = *out;
1974 /* Hm that should be it... */
1978 bool ast_member_codegen(ast_member *self, ast_function *func, bool lvalue, ir_value **out)
1980 ast_expression_codegen *cgen;
1983 /* in QC this is always an lvalue */
1985 if (self->expression.outl) {
1986 *out = self->expression.outl;
1990 cgen = self->owner->expression.codegen;
1991 if (!(*cgen)((ast_expression*)(self->owner), func, true, &vec))
1994 if (vec->vtype != TYPE_VECTOR &&
1995 !(vec->vtype == TYPE_FIELD && self->owner->expression.next->expression.vtype == TYPE_VECTOR))
2000 *out = ir_value_vector_member(vec, self->field);
2001 self->expression.outl = *out;
2003 return (*out != NULL);
2006 bool ast_array_index_codegen(ast_array_index *self, ast_function *func, bool lvalue, ir_value **out)
2011 if (!lvalue && self->expression.outr) {
2012 *out = self->expression.outr;
2014 if (lvalue && self->expression.outl) {
2015 *out = self->expression.outl;
2018 if (!ast_istype(self->array, ast_value)) {
2019 compile_error(ast_ctx(self), "array indexing this way is not supported");
2020 /* note this would actually be pointer indexing because the left side is
2021 * not an actual array but (hopefully) an indexable expression.
2022 * Once we get integer arithmetic, and GADDRESS/GSTORE/GLOAD instruction
2023 * support this path will be filled.
2028 arr = (ast_value*)self->array;
2029 idx = (ast_value*)self->index;
2031 if (!ast_istype(self->index, ast_value) || !idx->hasvalue || idx->cvq != CV_CONST) {
2032 /* Time to use accessor functions */
2033 ast_expression_codegen *cgen;
2034 ir_value *iridx, *funval;
2038 compile_error(ast_ctx(self), "(.2) array indexing here needs a compile-time constant");
2043 compile_error(ast_ctx(self), "value has no getter, don't know how to index it");
2047 cgen = self->index->expression.codegen;
2048 if (!(*cgen)((ast_expression*)(self->index), func, false, &iridx))
2051 cgen = arr->getter->expression.codegen;
2052 if (!(*cgen)((ast_expression*)(arr->getter), func, true, &funval))
2055 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "fetch"), funval);
2058 ir_call_param(call, iridx);
2060 *out = ir_call_value(call);
2061 self->expression.outr = *out;
2065 if (idx->expression.vtype == TYPE_FLOAT) {
2066 unsigned int arridx = idx->constval.vfloat;
2067 if (arridx >= self->array->expression.count)
2069 compile_error(ast_ctx(self), "array index out of bounds: %i", arridx);
2072 *out = arr->ir_values[arridx];
2074 else if (idx->expression.vtype == TYPE_INTEGER) {
2075 unsigned int arridx = idx->constval.vint;
2076 if (arridx >= self->array->expression.count)
2078 compile_error(ast_ctx(self), "array index out of bounds: %i", arridx);
2081 *out = arr->ir_values[arridx];
2084 compile_error(ast_ctx(self), "array indexing here needs an integer constant");
2090 bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_value **out)
2092 ast_expression_codegen *cgen;
2097 ir_block *cond = func->curblock;
2100 ir_block *ontrue_endblock = NULL;
2101 ir_block *onfalse_endblock = NULL;
2102 ir_block *merge = NULL;
2104 /* We don't output any value, thus also don't care about r/lvalue */
2108 if (self->expression.outr) {
2109 compile_error(ast_ctx(self), "internal error: ast_ifthen cannot be reused, it bears no result!");
2112 self->expression.outr = (ir_value*)1;
2114 /* generate the condition */
2115 cgen = self->cond->expression.codegen;
2116 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2118 /* update the block which will get the jump - because short-logic or ternaries may have changed this */
2119 cond = func->curblock;
2123 if (self->on_true) {
2124 /* create on-true block */
2125 ontrue = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "ontrue"));
2129 /* enter the block */
2130 func->curblock = ontrue;
2133 cgen = self->on_true->expression.codegen;
2134 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &dummy))
2137 /* we now need to work from the current endpoint */
2138 ontrue_endblock = func->curblock;
2143 if (self->on_false) {
2144 /* create on-false block */
2145 onfalse = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "onfalse"));
2149 /* enter the block */
2150 func->curblock = onfalse;
2153 cgen = self->on_false->expression.codegen;
2154 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &dummy))
2157 /* we now need to work from the current endpoint */
2158 onfalse_endblock = func->curblock;
2162 /* Merge block were they all merge in to */
2163 if (!ontrue || !onfalse || !ontrue_endblock->final || !onfalse_endblock->final)
2165 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "endif"));
2168 /* add jumps ot the merge block */
2169 if (ontrue && !ontrue_endblock->final && !ir_block_create_jump(ontrue_endblock, ast_ctx(self), merge))
2171 if (onfalse && !onfalse_endblock->final && !ir_block_create_jump(onfalse_endblock, ast_ctx(self), merge))
2174 /* Now enter the merge block */
2175 func->curblock = merge;
2178 /* we create the if here, that way all blocks are ordered :)
2180 if (!ir_block_create_if(cond, ast_ctx(self), condval,
2181 (ontrue ? ontrue : merge),
2182 (onfalse ? onfalse : merge)))
2190 bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_value **out)
2192 ast_expression_codegen *cgen;
2195 ir_value *trueval, *falseval;
2198 ir_block *cond = func->curblock;
2199 ir_block *cond_out = NULL;
2200 ir_block *ontrue, *ontrue_out = NULL;
2201 ir_block *onfalse, *onfalse_out = NULL;
2204 /* Ternary can never create an lvalue... */
2208 /* In theory it shouldn't be possible to pass through a node twice, but
2209 * in case we add any kind of optimization pass for the AST itself, it
2210 * may still happen, thus we remember a created ir_value and simply return one
2211 * if it already exists.
2213 if (self->expression.outr) {
2214 *out = self->expression.outr;
2218 /* In the following, contraty to ast_ifthen, we assume both paths exist. */
2220 /* generate the condition */
2221 func->curblock = cond;
2222 cgen = self->cond->expression.codegen;
2223 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2225 cond_out = func->curblock;
2227 /* create on-true block */
2228 ontrue = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_T"));
2233 /* enter the block */
2234 func->curblock = ontrue;
2237 cgen = self->on_true->expression.codegen;
2238 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &trueval))
2241 ontrue_out = func->curblock;
2244 /* create on-false block */
2245 onfalse = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_F"));
2250 /* enter the block */
2251 func->curblock = onfalse;
2254 cgen = self->on_false->expression.codegen;
2255 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &falseval))
2258 onfalse_out = func->curblock;
2261 /* create merge block */
2262 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_out"));
2265 /* jump to merge block */
2266 if (!ir_block_create_jump(ontrue_out, ast_ctx(self), merge))
2268 if (!ir_block_create_jump(onfalse_out, ast_ctx(self), merge))
2271 /* create if instruction */
2272 if (!ir_block_create_if(cond_out, ast_ctx(self), condval, ontrue, onfalse))
2275 /* Now enter the merge block */
2276 func->curblock = merge;
2278 /* Here, now, we need a PHI node
2279 * but first some sanity checking...
2281 if (trueval->vtype != falseval->vtype) {
2282 /* error("ternary with different types on the two sides"); */
2287 phi = ir_block_create_phi(merge, ast_ctx(self), ast_function_label(func, "phi"), trueval->vtype);
2290 ir_phi_add(phi, ontrue_out, trueval);
2291 ir_phi_add(phi, onfalse_out, falseval);
2293 self->expression.outr = ir_phi_value(phi);
2294 *out = self->expression.outr;
2299 bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value **out)
2301 ast_expression_codegen *cgen;
2303 ir_value *dummy = NULL;
2304 ir_value *precond = NULL;
2305 ir_value *postcond = NULL;
2307 /* Since we insert some jumps "late" so we have blocks
2308 * ordered "nicely", we need to keep track of the actual end-blocks
2309 * of expressions to add the jumps to.
2311 ir_block *bbody = NULL, *end_bbody = NULL;
2312 ir_block *bprecond = NULL, *end_bprecond = NULL;
2313 ir_block *bpostcond = NULL, *end_bpostcond = NULL;
2314 ir_block *bincrement = NULL, *end_bincrement = NULL;
2315 ir_block *bout = NULL, *bin = NULL;
2317 /* let's at least move the outgoing block to the end */
2320 /* 'break' and 'continue' need to be able to find the right blocks */
2321 ir_block *bcontinue = NULL;
2322 ir_block *bbreak = NULL;
2324 ir_block *old_bcontinue = NULL;
2325 ir_block *old_bbreak = NULL;
2327 ir_block *tmpblock = NULL;
2332 if (self->expression.outr) {
2333 compile_error(ast_ctx(self), "internal error: ast_loop cannot be reused, it bears no result!");
2336 self->expression.outr = (ir_value*)1;
2339 * Should we ever need some kind of block ordering, better make this function
2340 * move blocks around than write a block ordering algorithm later... after all
2341 * the ast and ir should work together, not against each other.
2344 /* initexpr doesn't get its own block, it's pointless, it could create more blocks
2345 * anyway if for example it contains a ternary.
2349 cgen = self->initexpr->expression.codegen;
2350 if (!(*cgen)((ast_expression*)(self->initexpr), func, false, &dummy))
2354 /* Store the block from which we enter this chaos */
2355 bin = func->curblock;
2357 /* The pre-loop condition needs its own block since we
2358 * need to be able to jump to the start of that expression.
2362 bprecond = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "pre_loop_cond"));
2366 /* the pre-loop-condition the least important place to 'continue' at */
2367 bcontinue = bprecond;
2370 func->curblock = bprecond;
2373 cgen = self->precond->expression.codegen;
2374 if (!(*cgen)((ast_expression*)(self->precond), func, false, &precond))
2377 end_bprecond = func->curblock;
2379 bprecond = end_bprecond = NULL;
2382 /* Now the next blocks won't be ordered nicely, but we need to
2383 * generate them this early for 'break' and 'continue'.
2385 if (self->increment) {
2386 bincrement = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "loop_increment"));
2389 bcontinue = bincrement; /* increment comes before the pre-loop-condition */
2391 bincrement = end_bincrement = NULL;
2394 if (self->postcond) {
2395 bpostcond = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "post_loop_cond"));
2398 bcontinue = bpostcond; /* postcond comes before the increment */
2400 bpostcond = end_bpostcond = NULL;
2403 bout_id = vec_size(func->ir_func->blocks);
2404 bout = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "after_loop"));
2409 /* The loop body... */
2412 bbody = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "loop_body"));
2417 func->curblock = bbody;
2419 old_bbreak = func->breakblock;
2420 old_bcontinue = func->continueblock;
2421 func->breakblock = bbreak;
2422 func->continueblock = bcontinue;
2423 if (!func->continueblock)
2424 func->continueblock = bbody;
2427 cgen = self->body->expression.codegen;
2428 if (!(*cgen)((ast_expression*)(self->body), func, false, &dummy))
2431 end_bbody = func->curblock;
2432 func->breakblock = old_bbreak;
2433 func->continueblock = old_bcontinue;
2436 /* post-loop-condition */
2440 func->curblock = bpostcond;
2443 cgen = self->postcond->expression.codegen;
2444 if (!(*cgen)((ast_expression*)(self->postcond), func, false, &postcond))
2447 end_bpostcond = func->curblock;
2450 /* The incrementor */
2451 if (self->increment)
2454 func->curblock = bincrement;
2457 cgen = self->increment->expression.codegen;
2458 if (!(*cgen)((ast_expression*)(self->increment), func, false, &dummy))
2461 end_bincrement = func->curblock;
2464 /* In any case now, we continue from the outgoing block */
2465 func->curblock = bout;
2467 /* Now all blocks are in place */
2468 /* From 'bin' we jump to whatever comes first */
2469 if (bprecond) tmpblock = bprecond;
2470 else if (bbody) tmpblock = bbody;
2471 else if (bpostcond) tmpblock = bpostcond;
2472 else tmpblock = bout;
2473 if (!ir_block_create_jump(bin, ast_ctx(self), tmpblock))
2479 ir_block *ontrue, *onfalse;
2480 if (bbody) ontrue = bbody;
2481 else if (bincrement) ontrue = bincrement;
2482 else if (bpostcond) ontrue = bpostcond;
2483 else ontrue = bprecond;
2485 if (!ir_block_create_if(end_bprecond, ast_ctx(self), precond, ontrue, onfalse))
2492 if (bincrement) tmpblock = bincrement;
2493 else if (bpostcond) tmpblock = bpostcond;
2494 else if (bprecond) tmpblock = bprecond;
2495 else tmpblock = bbody;
2496 if (!end_bbody->final && !ir_block_create_jump(end_bbody, ast_ctx(self), tmpblock))
2500 /* from increment */
2503 if (bpostcond) tmpblock = bpostcond;
2504 else if (bprecond) tmpblock = bprecond;
2505 else if (bbody) tmpblock = bbody;
2506 else tmpblock = bout;
2507 if (!ir_block_create_jump(end_bincrement, ast_ctx(self), tmpblock))
2514 ir_block *ontrue, *onfalse;
2515 if (bprecond) ontrue = bprecond;
2516 else if (bbody) ontrue = bbody;
2517 else if (bincrement) ontrue = bincrement;
2518 else ontrue = bpostcond;
2520 if (!ir_block_create_if(end_bpostcond, ast_ctx(self), postcond, ontrue, onfalse))
2524 /* Move 'bout' to the end */
2525 vec_remove(func->ir_func->blocks, bout_id, 1);
2526 vec_push(func->ir_func->blocks, bout);
2531 bool ast_breakcont_codegen(ast_breakcont *self, ast_function *func, bool lvalue, ir_value **out)
2538 compile_error(ast_ctx(self), "break/continue expression is not an l-value");
2542 if (self->expression.outr) {
2543 compile_error(ast_ctx(self), "internal error: ast_breakcont cannot be reused!");
2546 self->expression.outr = (ir_value*)1;
2548 if (self->is_continue)
2549 target = func->continueblock;
2551 target = func->breakblock;
2554 compile_error(ast_ctx(self), "%s is lacking a target block", (self->is_continue ? "continue" : "break"));
2558 if (!ir_block_create_jump(func->curblock, ast_ctx(self), target))
2563 bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_value **out)
2565 ast_expression_codegen *cgen;
2567 ast_switch_case *def_case = NULL;
2568 ir_block *def_bfall = NULL;
2570 ir_value *dummy = NULL;
2571 ir_value *irop = NULL;
2572 ir_block *old_break = NULL;
2573 ir_block *bout = NULL;
2574 ir_block *bfall = NULL;
2582 compile_error(ast_ctx(self), "switch expression is not an l-value");
2586 if (self->expression.outr) {
2587 compile_error(ast_ctx(self), "internal error: ast_switch cannot be reused!");
2590 self->expression.outr = (ir_value*)1;
2595 cgen = self->operand->expression.codegen;
2596 if (!(*cgen)((ast_expression*)(self->operand), func, false, &irop))
2599 if (!vec_size(self->cases))
2602 cmpinstr = type_eq_instr[irop->vtype];
2603 if (cmpinstr >= AINSTR_END) {
2604 ast_type_to_string(self->operand, typestr, sizeof(typestr));
2605 compile_error(ast_ctx(self), "invalid type to perform a switch on: %s", typestr);
2609 bout_id = vec_size(func->ir_func->blocks);
2610 bout = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "after_switch"));
2614 /* setup the break block */
2615 old_break = func->breakblock;
2616 func->breakblock = bout;
2618 /* Now create all cases */
2619 for (c = 0; c < vec_size(self->cases); ++c) {
2620 ir_value *cond, *val;
2621 ir_block *bcase, *bnot;
2624 ast_switch_case *swcase = &self->cases[c];
2626 if (swcase->value) {
2627 /* A regular case */
2628 /* generate the condition operand */
2629 cgen = swcase->value->expression.codegen;
2630 if (!(*cgen)((ast_expression*)(swcase->value), func, false, &val))
2632 /* generate the condition */
2633 cond = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "switch_eq"), cmpinstr, irop, val);
2637 bcase = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "case"));
2638 bnot_id = vec_size(func->ir_func->blocks);
2639 bnot = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "not_case"));
2640 if (!bcase || !bnot)
2642 if (!ir_block_create_if(func->curblock, ast_ctx(self), cond, bcase, bnot))
2645 /* Make the previous case-end fall through */
2646 if (bfall && !bfall->final) {
2647 if (!ir_block_create_jump(bfall, ast_ctx(self), bcase))
2651 /* enter the case */
2652 func->curblock = bcase;
2653 cgen = swcase->code->expression.codegen;
2654 if (!(*cgen)((ast_expression*)swcase->code, func, false, &dummy))
2657 /* remember this block to fall through from */
2658 bfall = func->curblock;
2660 /* enter the else and move it down */
2661 func->curblock = bnot;
2662 vec_remove(func->ir_func->blocks, bnot_id, 1);
2663 vec_push(func->ir_func->blocks, bnot);
2665 /* The default case */
2666 /* Remember where to fall through from: */
2669 /* remember which case it was */
2674 /* Jump from the last bnot to bout */
2675 if (bfall && !bfall->final && !ir_block_create_jump(bfall, ast_ctx(self), bout)) {
2677 astwarning(ast_ctx(bfall), WARN_???, "missing break after last case");
2682 /* If there was a default case, put it down here */
2686 /* No need to create an extra block */
2687 bcase = func->curblock;
2689 /* Insert the fallthrough jump */
2690 if (def_bfall && !def_bfall->final) {
2691 if (!ir_block_create_jump(def_bfall, ast_ctx(self), bcase))
2695 /* Now generate the default code */
2696 cgen = def_case->code->expression.codegen;
2697 if (!(*cgen)((ast_expression*)def_case->code, func, false, &dummy))
2701 /* Jump from the last bnot to bout */
2702 if (!func->curblock->final && !ir_block_create_jump(func->curblock, ast_ctx(self), bout))
2704 /* enter the outgoing block */
2705 func->curblock = bout;
2707 /* restore the break block */
2708 func->breakblock = old_break;
2710 /* Move 'bout' to the end, it's nicer */
2711 vec_remove(func->ir_func->blocks, bout_id, 1);
2712 vec_push(func->ir_func->blocks, bout);
2717 bool ast_label_codegen(ast_label *self, ast_function *func, bool lvalue, ir_value **out)
2724 compile_error(ast_ctx(self), "internal error: ast_label cannot be an lvalue");
2728 /* simply create a new block and jump to it */
2729 self->irblock = ir_function_create_block(ast_ctx(self), func->ir_func, self->name);
2730 if (!self->irblock) {
2731 compile_error(ast_ctx(self), "failed to allocate label block `%s`", self->name);
2734 if (!func->curblock->final) {
2735 if (!ir_block_create_jump(func->curblock, ast_ctx(self), self->irblock))
2739 /* enter the new block */
2740 func->curblock = self->irblock;
2742 /* Generate all the leftover gotos */
2743 for (i = 0; i < vec_size(self->gotos); ++i) {
2744 if (!ast_goto_codegen(self->gotos[i], func, false, &dummy))
2751 bool ast_goto_codegen(ast_goto *self, ast_function *func, bool lvalue, ir_value **out)
2755 compile_error(ast_ctx(self), "internal error: ast_goto cannot be an lvalue");
2759 if (self->target->irblock) {
2760 if (self->irblock_from) {
2761 /* we already tried once, this is the callback */
2762 self->irblock_from->final = false;
2763 if (!ir_block_create_jump(self->irblock_from, ast_ctx(self), self->target->irblock)) {
2764 compile_error(ast_ctx(self), "failed to generate goto to `%s`", self->name);
2770 if (!ir_block_create_jump(func->curblock, ast_ctx(self), self->target->irblock)) {
2771 compile_error(ast_ctx(self), "failed to generate goto to `%s`", self->name);
2778 /* the target has not yet been created...
2779 * close this block in a sneaky way:
2781 func->curblock->final = true;
2782 self->irblock_from = func->curblock;
2783 ast_label_register_goto(self->target, self);
2789 bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value **out)
2791 ast_expression_codegen *cgen;
2793 ir_instr *callinstr;
2796 ir_value *funval = NULL;
2798 /* return values are never lvalues */
2800 compile_error(ast_ctx(self), "not an l-value (function call)");
2804 if (self->expression.outr) {
2805 *out = self->expression.outr;
2809 cgen = self->func->expression.codegen;
2810 if (!(*cgen)((ast_expression*)(self->func), func, false, &funval))
2818 for (i = 0; i < vec_size(self->params); ++i)
2821 ast_expression *expr = self->params[i];
2823 cgen = expr->expression.codegen;
2824 if (!(*cgen)(expr, func, false, ¶m))
2828 vec_push(params, param);
2831 callinstr = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "call"), funval);
2835 for (i = 0; i < vec_size(params); ++i) {
2836 ir_call_param(callinstr, params[i]);
2839 *out = ir_call_value(callinstr);
2840 self->expression.outr = *out;