5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is furnished to do
10 * so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 #define ast_instantiate(T, ctx, destroyfn) \
31 T* self = (T*)mem_a(sizeof(T)); \
35 ast_node_init((ast_node*)self, ctx, TYPE_##T); \
36 ( (ast_node*)self )->node.destroy = (ast_node_delete*)destroyfn
39 /* It must not be possible to get here. */
40 static GMQCC_NORETURN void _ast_node_destroy(ast_node *self)
43 con_err("ast node missing destroy()\n");
47 /* Initialize main ast node aprts */
48 static void ast_node_init(ast_node *self, lex_ctx ctx, int nodetype)
50 self->node.context = ctx;
51 self->node.destroy = &_ast_node_destroy;
52 self->node.keep = false;
53 self->node.nodetype = nodetype;
54 self->node.side_effects = false;
57 /* weight and side effects */
58 static void _ast_propagate_effects(ast_node *self, ast_node *other)
60 if (ast_side_effects(other))
61 ast_side_effects(self) = true;
63 #define ast_propagate_effects(s,o) _ast_propagate_effects(((ast_node*)(s)), ((ast_node*)(o)))
65 /* General expression initialization */
66 static void ast_expression_init(ast_expression *self,
67 ast_expression_codegen *codegen)
69 self->expression.codegen = codegen;
70 self->expression.vtype = TYPE_VOID;
71 self->expression.next = NULL;
72 self->expression.outl = NULL;
73 self->expression.outr = NULL;
74 self->expression.params = NULL;
75 self->expression.count = 0;
76 self->expression.flags = 0;
79 static void ast_expression_delete(ast_expression *self)
82 if (self->expression.next)
83 ast_delete(self->expression.next);
84 for (i = 0; i < vec_size(self->expression.params); ++i) {
85 ast_delete(self->expression.params[i]);
87 vec_free(self->expression.params);
90 static void ast_expression_delete_full(ast_expression *self)
92 ast_expression_delete(self);
96 ast_value* ast_value_copy(const ast_value *self)
99 const ast_expression_common *fromex;
100 ast_expression_common *selfex;
101 ast_value *cp = ast_value_new(self->expression.node.context, self->name, self->expression.vtype);
102 if (self->expression.next) {
103 cp->expression.next = ast_type_copy(self->expression.node.context, self->expression.next);
104 if (!cp->expression.next) {
105 ast_value_delete(cp);
109 fromex = &self->expression;
110 selfex = &cp->expression;
111 selfex->count = fromex->count;
112 selfex->flags = fromex->flags;
113 for (i = 0; i < vec_size(fromex->params); ++i) {
114 ast_value *v = ast_value_copy(fromex->params[i]);
116 ast_value_delete(cp);
119 vec_push(selfex->params, v);
124 bool ast_type_adopt_impl(ast_expression *self, const ast_expression *other)
127 const ast_expression_common *fromex;
128 ast_expression_common *selfex;
129 self->expression.vtype = other->expression.vtype;
130 if (other->expression.next) {
131 self->expression.next = (ast_expression*)ast_type_copy(ast_ctx(self), other->expression.next);
132 if (!self->expression.next)
135 fromex = &other->expression;
136 selfex = &self->expression;
137 selfex->count = fromex->count;
138 selfex->flags = fromex->flags;
139 for (i = 0; i < vec_size(fromex->params); ++i) {
140 ast_value *v = ast_value_copy(fromex->params[i]);
143 vec_push(selfex->params, v);
148 static ast_expression* ast_shallow_type(lex_ctx ctx, int vtype)
150 ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
151 ast_expression_init(self, NULL);
152 self->expression.codegen = NULL;
153 self->expression.next = NULL;
154 self->expression.vtype = vtype;
158 ast_expression* ast_type_copy(lex_ctx ctx, const ast_expression *ex)
161 const ast_expression_common *fromex;
162 ast_expression_common *selfex;
168 ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
169 ast_expression_init(self, NULL);
171 fromex = &ex->expression;
172 selfex = &self->expression;
174 /* This may never be codegen()d */
175 selfex->codegen = NULL;
177 selfex->vtype = fromex->vtype;
180 selfex->next = ast_type_copy(ctx, fromex->next);
182 ast_expression_delete_full(self);
189 selfex->count = fromex->count;
190 selfex->flags = fromex->flags;
191 for (i = 0; i < vec_size(fromex->params); ++i) {
192 ast_value *v = ast_value_copy(fromex->params[i]);
194 ast_expression_delete_full(self);
197 vec_push(selfex->params, v);
204 bool ast_compare_type(ast_expression *a, ast_expression *b)
206 if (a->expression.vtype != b->expression.vtype)
208 if (!a->expression.next != !b->expression.next)
210 if (vec_size(a->expression.params) != vec_size(b->expression.params))
212 if (a->expression.flags != b->expression.flags)
214 if (vec_size(a->expression.params)) {
216 for (i = 0; i < vec_size(a->expression.params); ++i) {
217 if (!ast_compare_type((ast_expression*)a->expression.params[i],
218 (ast_expression*)b->expression.params[i]))
222 if (a->expression.next)
223 return ast_compare_type(a->expression.next, b->expression.next);
227 static size_t ast_type_to_string_impl(ast_expression *e, char *buf, size_t bufsize, size_t pos)
234 if (pos + 6 >= bufsize)
236 strcpy(buf + pos, "(null)");
240 if (pos + 1 >= bufsize)
243 switch (e->expression.vtype) {
245 strcpy(buf + pos, "(variant)");
250 return ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
253 if (pos + 3 >= bufsize)
257 pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
258 if (pos + 1 >= bufsize)
264 pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
265 if (pos + 2 >= bufsize)
267 if (!vec_size(e->expression.params)) {
273 pos = ast_type_to_string_impl((ast_expression*)(e->expression.params[0]), buf, bufsize, pos);
274 for (i = 1; i < vec_size(e->expression.params); ++i) {
275 if (pos + 2 >= bufsize)
279 pos = ast_type_to_string_impl((ast_expression*)(e->expression.params[i]), buf, bufsize, pos);
281 if (pos + 1 >= bufsize)
287 pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
288 if (pos + 1 >= bufsize)
291 pos += snprintf(buf + pos, bufsize - pos - 1, "%i", (int)e->expression.count);
292 if (pos + 1 >= bufsize)
298 typestr = type_name[e->expression.vtype];
299 typelen = strlen(typestr);
300 if (pos + typelen >= bufsize)
302 strcpy(buf + pos, typestr);
303 return pos + typelen;
307 buf[bufsize-3] = '.';
308 buf[bufsize-2] = '.';
309 buf[bufsize-1] = '.';
313 void ast_type_to_string(ast_expression *e, char *buf, size_t bufsize)
315 size_t pos = ast_type_to_string_impl(e, buf, bufsize-1, 0);
319 ast_value* ast_value_new(lex_ctx ctx, const char *name, int t)
321 ast_instantiate(ast_value, ctx, ast_value_delete);
322 ast_expression_init((ast_expression*)self,
323 (ast_expression_codegen*)&ast_value_codegen);
324 self->expression.node.keep = true; /* keep */
326 self->name = name ? util_strdup(name) : NULL;
327 self->expression.vtype = t;
328 self->expression.next = NULL;
329 self->isfield = false;
331 self->hasvalue = false;
333 memset(&self->constval, 0, sizeof(self->constval));
336 self->ir_values = NULL;
337 self->ir_value_count = 0;
345 void ast_value_delete(ast_value* self)
348 mem_d((void*)self->name);
349 if (self->hasvalue) {
350 switch (self->expression.vtype)
353 mem_d((void*)self->constval.vstring);
356 /* unlink us from the function node */
357 self->constval.vfunc->vtype = NULL;
359 /* NOTE: delete function? currently collected in
360 * the parser structure
367 mem_d(self->ir_values);
368 ast_expression_delete((ast_expression*)self);
372 void ast_value_params_add(ast_value *self, ast_value *p)
374 vec_push(self->expression.params, p);
377 bool ast_value_set_name(ast_value *self, const char *name)
380 mem_d((void*)self->name);
381 self->name = util_strdup(name);
385 ast_binary* ast_binary_new(lex_ctx ctx, int op,
386 ast_expression* left, ast_expression* right)
388 ast_instantiate(ast_binary, ctx, ast_binary_delete);
389 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binary_codegen);
395 ast_propagate_effects(self, left);
396 ast_propagate_effects(self, right);
398 if (op >= INSTR_EQ_F && op <= INSTR_GT)
399 self->expression.vtype = TYPE_FLOAT;
400 else if (op == INSTR_AND || op == INSTR_OR) {
401 if (OPTS_FLAG(PERL_LOGIC))
402 ast_type_adopt(self, right);
404 self->expression.vtype = TYPE_FLOAT;
406 else if (op == INSTR_BITAND || op == INSTR_BITOR)
407 self->expression.vtype = TYPE_FLOAT;
408 else if (op == INSTR_MUL_VF || op == INSTR_MUL_FV)
409 self->expression.vtype = TYPE_VECTOR;
410 else if (op == INSTR_MUL_V)
411 self->expression.vtype = TYPE_FLOAT;
413 self->expression.vtype = left->expression.vtype;
418 void ast_binary_delete(ast_binary *self)
420 ast_unref(self->left);
421 ast_unref(self->right);
422 ast_expression_delete((ast_expression*)self);
426 ast_binstore* ast_binstore_new(lex_ctx ctx, int storop, int op,
427 ast_expression* left, ast_expression* right)
429 ast_instantiate(ast_binstore, ctx, ast_binstore_delete);
430 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binstore_codegen);
432 ast_side_effects(self) = true;
434 self->opstore = storop;
437 self->source = right;
439 self->keep_dest = false;
441 if (!ast_type_adopt(self, left)) {
449 void ast_binstore_delete(ast_binstore *self)
451 if (!self->keep_dest)
452 ast_unref(self->dest);
453 ast_unref(self->source);
454 ast_expression_delete((ast_expression*)self);
458 ast_unary* ast_unary_new(lex_ctx ctx, int op,
459 ast_expression *expr)
461 ast_instantiate(ast_unary, ctx, ast_unary_delete);
462 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_unary_codegen);
465 self->operand = expr;
467 ast_propagate_effects(self, expr);
469 if (op >= INSTR_NOT_F && op <= INSTR_NOT_FNC) {
470 self->expression.vtype = TYPE_FLOAT;
472 compile_error(ctx, "cannot determine type of unary operation %s", asm_instr[op].m);
477 void ast_unary_delete(ast_unary *self)
479 if (self->operand) ast_unref(self->operand);
480 ast_expression_delete((ast_expression*)self);
484 ast_return* ast_return_new(lex_ctx ctx, ast_expression *expr)
486 ast_instantiate(ast_return, ctx, ast_return_delete);
487 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_return_codegen);
489 self->operand = expr;
492 ast_propagate_effects(self, expr);
497 void ast_return_delete(ast_return *self)
500 ast_unref(self->operand);
501 ast_expression_delete((ast_expression*)self);
505 ast_entfield* ast_entfield_new(lex_ctx ctx, ast_expression *entity, ast_expression *field)
507 if (field->expression.vtype != TYPE_FIELD) {
508 compile_error(ctx, "ast_entfield_new with expression not of type field");
511 return ast_entfield_new_force(ctx, entity, field, field->expression.next);
514 ast_entfield* ast_entfield_new_force(lex_ctx ctx, ast_expression *entity, ast_expression *field, const ast_expression *outtype)
516 ast_instantiate(ast_entfield, ctx, ast_entfield_delete);
520 /* Error: field has no type... */
524 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_entfield_codegen);
526 self->entity = entity;
528 ast_propagate_effects(self, entity);
529 ast_propagate_effects(self, field);
531 if (!ast_type_adopt(self, outtype)) {
532 ast_entfield_delete(self);
539 void ast_entfield_delete(ast_entfield *self)
541 ast_unref(self->entity);
542 ast_unref(self->field);
543 ast_expression_delete((ast_expression*)self);
547 ast_member* ast_member_new(lex_ctx ctx, ast_expression *owner, unsigned int field, const char *name)
549 ast_instantiate(ast_member, ctx, ast_member_delete);
555 if (owner->expression.vtype != TYPE_VECTOR &&
556 owner->expression.vtype != TYPE_FIELD) {
557 compile_error(ctx, "member-access on an invalid owner of type %s", type_name[owner->expression.vtype]);
562 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_member_codegen);
563 self->expression.node.keep = true; /* keep */
565 if (owner->expression.vtype == TYPE_VECTOR) {
566 self->expression.vtype = TYPE_FLOAT;
567 self->expression.next = NULL;
569 self->expression.vtype = TYPE_FIELD;
570 self->expression.next = ast_shallow_type(ctx, TYPE_FLOAT);
573 self->rvalue = false;
575 ast_propagate_effects(self, owner);
579 self->name = util_strdup(name);
586 void ast_member_delete(ast_member *self)
588 /* The owner is always an ast_value, which has .keep=true,
589 * also: ast_members are usually deleted after the owner, thus
590 * this will cause invalid access
591 ast_unref(self->owner);
592 * once we allow (expression).x to access a vector-member, we need
593 * to change this: preferably by creating an alternate ast node for this
594 * purpose that is not garbage-collected.
596 ast_expression_delete((ast_expression*)self);
600 bool ast_member_set_name(ast_member *self, const char *name)
603 mem_d((void*)self->name);
604 self->name = util_strdup(name);
608 ast_array_index* ast_array_index_new(lex_ctx ctx, ast_expression *array, ast_expression *index)
610 ast_expression *outtype;
611 ast_instantiate(ast_array_index, ctx, ast_array_index_delete);
613 outtype = array->expression.next;
616 /* Error: field has no type... */
620 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_array_index_codegen);
624 ast_propagate_effects(self, array);
625 ast_propagate_effects(self, index);
627 if (!ast_type_adopt(self, outtype)) {
628 ast_array_index_delete(self);
631 if (array->expression.vtype == TYPE_FIELD && outtype->expression.vtype == TYPE_ARRAY) {
632 if (self->expression.vtype != TYPE_ARRAY) {
633 compile_error(ast_ctx(self), "array_index node on type");
634 ast_array_index_delete(self);
637 self->array = outtype;
638 self->expression.vtype = TYPE_FIELD;
644 void ast_array_index_delete(ast_array_index *self)
646 ast_unref(self->array);
647 ast_unref(self->index);
648 ast_expression_delete((ast_expression*)self);
652 ast_ifthen* ast_ifthen_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
654 ast_instantiate(ast_ifthen, ctx, ast_ifthen_delete);
655 if (!ontrue && !onfalse) {
656 /* because it is invalid */
660 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ifthen_codegen);
663 self->on_true = ontrue;
664 self->on_false = onfalse;
665 ast_propagate_effects(self, cond);
667 ast_propagate_effects(self, ontrue);
669 ast_propagate_effects(self, onfalse);
674 void ast_ifthen_delete(ast_ifthen *self)
676 ast_unref(self->cond);
678 ast_unref(self->on_true);
680 ast_unref(self->on_false);
681 ast_expression_delete((ast_expression*)self);
685 ast_ternary* ast_ternary_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
687 ast_instantiate(ast_ternary, ctx, ast_ternary_delete);
688 /* This time NEITHER must be NULL */
689 if (!ontrue || !onfalse) {
693 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ternary_codegen);
696 self->on_true = ontrue;
697 self->on_false = onfalse;
698 ast_propagate_effects(self, cond);
699 ast_propagate_effects(self, ontrue);
700 ast_propagate_effects(self, onfalse);
702 if (!ast_type_adopt(self, ontrue)) {
703 ast_ternary_delete(self);
710 void ast_ternary_delete(ast_ternary *self)
712 ast_unref(self->cond);
713 ast_unref(self->on_true);
714 ast_unref(self->on_false);
715 ast_expression_delete((ast_expression*)self);
719 ast_loop* ast_loop_new(lex_ctx ctx,
720 ast_expression *initexpr,
721 ast_expression *precond, bool pre_not,
722 ast_expression *postcond, bool post_not,
723 ast_expression *increment,
724 ast_expression *body)
726 ast_instantiate(ast_loop, ctx, ast_loop_delete);
727 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_loop_codegen);
729 self->initexpr = initexpr;
730 self->precond = precond;
731 self->postcond = postcond;
732 self->increment = increment;
735 self->pre_not = pre_not;
736 self->post_not = post_not;
739 ast_propagate_effects(self, initexpr);
741 ast_propagate_effects(self, precond);
743 ast_propagate_effects(self, postcond);
745 ast_propagate_effects(self, increment);
747 ast_propagate_effects(self, body);
752 void ast_loop_delete(ast_loop *self)
755 ast_unref(self->initexpr);
757 ast_unref(self->precond);
759 ast_unref(self->postcond);
761 ast_unref(self->increment);
763 ast_unref(self->body);
764 ast_expression_delete((ast_expression*)self);
768 ast_breakcont* ast_breakcont_new(lex_ctx ctx, bool iscont, unsigned int levels)
770 ast_instantiate(ast_breakcont, ctx, ast_breakcont_delete);
771 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_breakcont_codegen);
773 self->is_continue = iscont;
774 self->levels = levels;
779 void ast_breakcont_delete(ast_breakcont *self)
781 ast_expression_delete((ast_expression*)self);
785 ast_switch* ast_switch_new(lex_ctx ctx, ast_expression *op)
787 ast_instantiate(ast_switch, ctx, ast_switch_delete);
788 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_switch_codegen);
793 ast_propagate_effects(self, op);
798 void ast_switch_delete(ast_switch *self)
801 ast_unref(self->operand);
803 for (i = 0; i < vec_size(self->cases); ++i) {
804 if (self->cases[i].value)
805 ast_unref(self->cases[i].value);
806 ast_unref(self->cases[i].code);
808 vec_free(self->cases);
810 ast_expression_delete((ast_expression*)self);
814 ast_label* ast_label_new(lex_ctx ctx, const char *name)
816 ast_instantiate(ast_label, ctx, ast_label_delete);
817 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_label_codegen);
819 self->name = util_strdup(name);
820 self->irblock = NULL;
826 void ast_label_delete(ast_label *self)
828 mem_d((void*)self->name);
829 vec_free(self->gotos);
830 ast_expression_delete((ast_expression*)self);
834 void ast_label_register_goto(ast_label *self, ast_goto *g)
836 vec_push(self->gotos, g);
839 ast_goto* ast_goto_new(lex_ctx ctx, const char *name)
841 ast_instantiate(ast_goto, ctx, ast_goto_delete);
842 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_goto_codegen);
844 self->name = util_strdup(name);
846 self->irblock_from = NULL;
851 void ast_goto_delete(ast_goto *self)
853 mem_d((void*)self->name);
854 ast_expression_delete((ast_expression*)self);
858 void ast_goto_set_label(ast_goto *self, ast_label *label)
860 self->target = label;
863 ast_call* ast_call_new(lex_ctx ctx,
864 ast_expression *funcexpr)
866 ast_instantiate(ast_call, ctx, ast_call_delete);
867 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_call_codegen);
869 ast_side_effects(self) = true;
872 self->func = funcexpr;
874 ast_type_adopt(self, funcexpr->expression.next);
879 void ast_call_delete(ast_call *self)
882 for (i = 0; i < vec_size(self->params); ++i)
883 ast_unref(self->params[i]);
884 vec_free(self->params);
887 ast_unref(self->func);
889 ast_expression_delete((ast_expression*)self);
893 bool ast_call_check_types(ast_call *self)
897 const ast_expression *func = self->func;
898 size_t count = vec_size(self->params);
899 if (count > vec_size(func->expression.params))
900 count = vec_size(func->expression.params);
902 for (i = 0; i < count; ++i) {
903 if (!ast_compare_type(self->params[i], (ast_expression*)(func->expression.params[i]))) {
906 ast_type_to_string(self->params[i], tgot, sizeof(tgot));
907 ast_type_to_string((ast_expression*)func->expression.params[i], texp, sizeof(texp));
908 compile_error(ast_ctx(self), "invalid type for parameter %u in function call: expected %s, got %s",
909 (unsigned int)(i+1), texp, tgot);
910 /* we don't immediately return */
917 ast_store* ast_store_new(lex_ctx ctx, int op,
918 ast_expression *dest, ast_expression *source)
920 ast_instantiate(ast_store, ctx, ast_store_delete);
921 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_store_codegen);
923 ast_side_effects(self) = true;
927 self->source = source;
929 if (!ast_type_adopt(self, dest)) {
937 void ast_store_delete(ast_store *self)
939 ast_unref(self->dest);
940 ast_unref(self->source);
941 ast_expression_delete((ast_expression*)self);
945 ast_block* ast_block_new(lex_ctx ctx)
947 ast_instantiate(ast_block, ctx, ast_block_delete);
948 ast_expression_init((ast_expression*)self,
949 (ast_expression_codegen*)&ast_block_codegen);
953 self->collect = NULL;
958 bool ast_block_add_expr(ast_block *self, ast_expression *e)
960 ast_propagate_effects(self, e);
961 vec_push(self->exprs, e);
962 if (self->expression.next) {
963 ast_delete(self->expression.next);
964 self->expression.next = NULL;
966 if (!ast_type_adopt(self, e)) {
967 compile_error(ast_ctx(self), "internal error: failed to adopt type");
973 void ast_block_collect(ast_block *self, ast_expression *expr)
975 vec_push(self->collect, expr);
976 expr->expression.node.keep = true;
979 void ast_block_delete(ast_block *self)
982 for (i = 0; i < vec_size(self->exprs); ++i)
983 ast_unref(self->exprs[i]);
984 vec_free(self->exprs);
985 for (i = 0; i < vec_size(self->locals); ++i)
986 ast_delete(self->locals[i]);
987 vec_free(self->locals);
988 for (i = 0; i < vec_size(self->collect); ++i)
989 ast_delete(self->collect[i]);
990 vec_free(self->collect);
991 ast_expression_delete((ast_expression*)self);
995 bool ast_block_set_type(ast_block *self, ast_expression *from)
997 if (self->expression.next)
998 ast_delete(self->expression.next);
999 if (!ast_type_adopt(self, from))
1004 ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype)
1006 ast_instantiate(ast_function, ctx, ast_function_delete);
1010 vtype->expression.vtype != TYPE_FUNCTION)
1012 compile_error(ast_ctx(self), "internal error: ast_function_new condition %i %i type=%i (probably 2 bodies?)",
1014 (int)vtype->hasvalue,
1015 vtype->expression.vtype);
1020 self->vtype = vtype;
1021 self->name = name ? util_strdup(name) : NULL;
1022 self->blocks = NULL;
1024 self->labelcount = 0;
1027 self->ir_func = NULL;
1028 self->curblock = NULL;
1030 self->breakblocks = NULL;
1031 self->continueblocks = NULL;
1033 vtype->hasvalue = true;
1034 vtype->constval.vfunc = self;
1039 void ast_function_delete(ast_function *self)
1043 mem_d((void*)self->name);
1045 /* ast_value_delete(self->vtype); */
1046 self->vtype->hasvalue = false;
1047 self->vtype->constval.vfunc = NULL;
1048 /* We use unref - if it was stored in a global table it is supposed
1049 * to be deleted from *there*
1051 ast_unref(self->vtype);
1053 for (i = 0; i < vec_size(self->blocks); ++i)
1054 ast_delete(self->blocks[i]);
1055 vec_free(self->blocks);
1056 vec_free(self->breakblocks);
1057 vec_free(self->continueblocks);
1061 const char* ast_function_label(ast_function *self, const char *prefix)
1067 if (!opts.dump && !opts.dumpfin && !opts.debug)
1070 id = (self->labelcount++);
1071 len = strlen(prefix);
1073 from = self->labelbuf + sizeof(self->labelbuf)-1;
1076 *from-- = (id%10) + '0';
1080 memcpy(from - len, prefix, len);
1084 /*********************************************************************/
1086 * by convention you must never pass NULL to the 'ir_value **out'
1087 * parameter. If you really don't care about the output, pass a dummy.
1088 * But I can't imagine a pituation where the output is truly unnecessary.
1091 void _ast_codegen_output_type(ast_expression_common *self, ir_value *out)
1093 if (out->vtype == TYPE_FIELD)
1094 out->fieldtype = self->next->expression.vtype;
1095 if (out->vtype == TYPE_FUNCTION)
1096 out->outtype = self->next->expression.vtype;
1099 #define codegen_output_type(a,o) (_ast_codegen_output_type(&((a)->expression),(o)))
1101 bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out)
1105 if (self->expression.vtype == TYPE_NIL) {
1106 *out = func->ir_func->owner->nil;
1109 /* NOTE: This is the codegen for a variable used in an expression.
1110 * It is not the codegen to generate the value. For this purpose,
1111 * ast_local_codegen and ast_global_codegen are to be used before this
1112 * is executed. ast_function_codegen should take care of its locals,
1113 * and the ast-user should take care of ast_global_codegen to be used
1114 * on all the globals.
1117 char tname[1024]; /* typename is reserved in C++ */
1118 ast_type_to_string((ast_expression*)self, tname, sizeof(tname));
1119 compile_error(ast_ctx(self), "ast_value used before generated %s %s", tname, self->name);
1126 bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
1130 if (self->expression.vtype == TYPE_NIL) {
1131 compile_error(ast_ctx(self), "internal error: trying to generate a variable of TYPE_NIL");
1135 if (self->hasvalue && self->expression.vtype == TYPE_FUNCTION)
1137 ir_function *func = ir_builder_create_function(ir, self->name, self->expression.next->expression.vtype);
1140 func->context = ast_ctx(self);
1141 func->value->context = ast_ctx(self);
1143 self->constval.vfunc->ir_func = func;
1144 self->ir_v = func->value;
1145 /* The function is filled later on ast_function_codegen... */
1149 if (isfield && self->expression.vtype == TYPE_FIELD) {
1150 ast_expression *fieldtype = self->expression.next;
1152 if (self->hasvalue) {
1153 compile_error(ast_ctx(self), "TODO: constant field pointers with value");
1157 if (fieldtype->expression.vtype == TYPE_ARRAY) {
1162 ast_expression_common *elemtype;
1164 ast_value *array = (ast_value*)fieldtype;
1166 if (!ast_istype(fieldtype, ast_value)) {
1167 compile_error(ast_ctx(self), "internal error: ast_value required");
1171 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1172 if (!array->expression.count || array->expression.count > opts.max_array_size)
1173 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)array->expression.count);
1175 elemtype = &array->expression.next->expression;
1176 vtype = elemtype->vtype;
1178 v = ir_builder_create_field(ir, self->name, vtype);
1180 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", self->name);
1183 v->context = ast_ctx(self);
1184 v->unique_life = true;
1186 array->ir_v = self->ir_v = v;
1188 namelen = strlen(self->name);
1189 name = (char*)mem_a(namelen + 16);
1190 strcpy(name, self->name);
1192 array->ir_values = (ir_value**)mem_a(sizeof(array->ir_values[0]) * array->expression.count);
1193 array->ir_values[0] = v;
1194 for (ai = 1; ai < array->expression.count; ++ai) {
1195 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1196 array->ir_values[ai] = ir_builder_create_field(ir, name, vtype);
1197 if (!array->ir_values[ai]) {
1199 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", name);
1202 array->ir_values[ai]->context = ast_ctx(self);
1203 array->ir_values[ai]->unique_life = true;
1204 array->ir_values[ai]->locked = true;
1210 v = ir_builder_create_field(ir, self->name, self->expression.next->expression.vtype);
1213 v->context = ast_ctx(self);
1219 if (self->expression.vtype == TYPE_ARRAY) {
1224 ast_expression_common *elemtype = &self->expression.next->expression;
1225 int vtype = elemtype->vtype;
1227 /* same as with field arrays */
1228 if (!self->expression.count || self->expression.count > opts.max_array_size)
1229 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1231 v = ir_builder_create_global(ir, self->name, vtype);
1233 compile_error(ast_ctx(self), "ir_builder_create_global failed `%s`", self->name);
1236 v->context = ast_ctx(self);
1237 v->unique_life = true;
1240 namelen = strlen(self->name);
1241 name = (char*)mem_a(namelen + 16);
1242 strcpy(name, self->name);
1244 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1245 self->ir_values[0] = v;
1246 for (ai = 1; ai < self->expression.count; ++ai) {
1247 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1248 self->ir_values[ai] = ir_builder_create_global(ir, name, vtype);
1249 if (!self->ir_values[ai]) {
1251 compile_error(ast_ctx(self), "ir_builder_create_global failed `%s`", name);
1254 self->ir_values[ai]->context = ast_ctx(self);
1255 self->ir_values[ai]->unique_life = true;
1256 self->ir_values[ai]->locked = true;
1262 /* Arrays don't do this since there's no "array" value which spans across the
1265 v = ir_builder_create_global(ir, self->name, self->expression.vtype);
1267 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", self->name);
1270 codegen_output_type(self, v);
1271 v->context = ast_ctx(self);
1274 if (self->hasvalue) {
1275 switch (self->expression.vtype)
1278 if (!ir_value_set_float(v, self->constval.vfloat))
1282 if (!ir_value_set_vector(v, self->constval.vvec))
1286 if (!ir_value_set_string(v, self->constval.vstring))
1290 compile_error(ast_ctx(self), "TODO: global constant array");
1293 compile_error(ast_ctx(self), "global of type function not properly generated");
1295 /* Cannot generate an IR value for a function,
1296 * need a pointer pointing to a function rather.
1299 if (!self->constval.vfield) {
1300 compile_error(ast_ctx(self), "field constant without vfield set");
1303 if (!self->constval.vfield->ir_v) {
1304 compile_error(ast_ctx(self), "field constant generated before its field");
1307 if (!ir_value_set_field(v, self->constval.vfield->ir_v))
1311 compile_error(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1316 /* link us to the ir_value */
1321 error: /* clean up */
1326 bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
1330 if (self->expression.vtype == TYPE_NIL) {
1331 compile_error(ast_ctx(self), "internal error: trying to generate a variable of TYPE_NIL");
1335 if (self->hasvalue && self->expression.vtype == TYPE_FUNCTION)
1337 /* Do we allow local functions? I think not...
1338 * this is NOT a function pointer atm.
1343 if (self->expression.vtype == TYPE_ARRAY) {
1348 ast_expression_common *elemtype = &self->expression.next->expression;
1349 int vtype = elemtype->vtype;
1351 func->flags |= IR_FLAG_HAS_ARRAYS;
1354 compile_error(ast_ctx(self), "array-parameters are not supported");
1358 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1359 if (!self->expression.count || self->expression.count > opts.max_array_size) {
1360 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1363 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1364 if (!self->ir_values) {
1365 compile_error(ast_ctx(self), "failed to allocate array values");
1369 v = ir_function_create_local(func, self->name, vtype, param);
1371 compile_error(ast_ctx(self), "ir_function_create_local failed");
1374 v->context = ast_ctx(self);
1375 v->unique_life = true;
1378 namelen = strlen(self->name);
1379 name = (char*)mem_a(namelen + 16);
1380 strcpy(name, self->name);
1382 self->ir_values[0] = v;
1383 for (ai = 1; ai < self->expression.count; ++ai) {
1384 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1385 self->ir_values[ai] = ir_function_create_local(func, name, vtype, param);
1386 if (!self->ir_values[ai]) {
1387 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", name);
1390 self->ir_values[ai]->context = ast_ctx(self);
1391 self->ir_values[ai]->unique_life = true;
1392 self->ir_values[ai]->locked = true;
1397 v = ir_function_create_local(func, self->name, self->expression.vtype, param);
1400 codegen_output_type(self, v);
1401 v->context = ast_ctx(self);
1404 /* A constant local... hmmm...
1405 * I suppose the IR will have to deal with this
1407 if (self->hasvalue) {
1408 switch (self->expression.vtype)
1411 if (!ir_value_set_float(v, self->constval.vfloat))
1415 if (!ir_value_set_vector(v, self->constval.vvec))
1419 if (!ir_value_set_string(v, self->constval.vstring))
1423 compile_error(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1428 /* link us to the ir_value */
1432 if (!ast_generate_accessors(self, func->owner))
1436 error: /* clean up */
1441 bool ast_generate_accessors(ast_value *self, ir_builder *ir)
1444 bool warn = OPTS_WARN(WARN_USED_UNINITIALIZED);
1445 if (!self->setter || !self->getter)
1447 for (i = 0; i < self->expression.count; ++i) {
1448 if (!self->ir_values) {
1449 compile_error(ast_ctx(self), "internal error: no array values generated for `%s`", self->name);
1452 if (!self->ir_values[i]) {
1453 compile_error(ast_ctx(self), "internal error: not all array values have been generated for `%s`", self->name);
1456 if (self->ir_values[i]->life) {
1457 compile_error(ast_ctx(self), "internal error: function containing `%s` already generated", self->name);
1462 opts_set(opts.warn, WARN_USED_UNINITIALIZED, false);
1464 if (!ast_global_codegen (self->setter, ir, false) ||
1465 !ast_function_codegen(self->setter->constval.vfunc, ir) ||
1466 !ir_function_finalize(self->setter->constval.vfunc->ir_func))
1468 compile_error(ast_ctx(self), "internal error: failed to generate setter for `%s`", self->name);
1469 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1474 if (!ast_global_codegen (self->getter, ir, false) ||
1475 !ast_function_codegen(self->getter->constval.vfunc, ir) ||
1476 !ir_function_finalize(self->getter->constval.vfunc->ir_func))
1478 compile_error(ast_ctx(self), "internal error: failed to generate getter for `%s`", self->name);
1479 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1483 for (i = 0; i < self->expression.count; ++i) {
1484 vec_free(self->ir_values[i]->life);
1486 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1490 bool ast_function_codegen(ast_function *self, ir_builder *ir)
1494 ast_expression_common *ec;
1499 irf = self->ir_func;
1501 compile_error(ast_ctx(self), "ast_function's related ast_value was not generated yet");
1505 /* fill the parameter list */
1506 ec = &self->vtype->expression;
1507 for (i = 0; i < vec_size(ec->params); ++i)
1509 if (ec->params[i]->expression.vtype == TYPE_FIELD)
1510 vec_push(irf->params, ec->params[i]->expression.next->expression.vtype);
1512 vec_push(irf->params, ec->params[i]->expression.vtype);
1513 if (!self->builtin) {
1514 if (!ast_local_codegen(ec->params[i], self->ir_func, true))
1519 if (self->builtin) {
1520 irf->builtin = self->builtin;
1524 if (!vec_size(self->blocks)) {
1525 compile_error(ast_ctx(self), "function `%s` has no body", self->name);
1529 self->curblock = ir_function_create_block(ast_ctx(self), irf, "entry");
1530 if (!self->curblock) {
1531 compile_error(ast_ctx(self), "failed to allocate entry block for `%s`", self->name);
1535 for (i = 0; i < vec_size(self->blocks); ++i) {
1536 ast_expression_codegen *gen = self->blocks[i]->expression.codegen;
1537 if (!(*gen)((ast_expression*)self->blocks[i], self, false, &dummy))
1541 /* TODO: check return types */
1542 if (!self->curblock->final)
1544 if (!self->vtype->expression.next ||
1545 self->vtype->expression.next->expression.vtype == TYPE_VOID)
1547 return ir_block_create_return(self->curblock, ast_ctx(self), NULL);
1549 else if (vec_size(self->curblock->entries))
1551 /* error("missing return"); */
1552 if (compile_warning(ast_ctx(self), WARN_MISSING_RETURN_VALUES,
1553 "control reaches end of non-void function (`%s`) via %s",
1554 self->name, self->curblock->label))
1558 return ir_block_create_return(self->curblock, ast_ctx(self), NULL);
1564 /* Note, you will not see ast_block_codegen generate ir_blocks.
1565 * To the AST and the IR, blocks are 2 different things.
1566 * In the AST it represents a block of code, usually enclosed in
1567 * curly braces {...}.
1568 * While in the IR it represents a block in terms of control-flow.
1570 bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_value **out)
1574 /* We don't use this
1575 * Note: an ast-representation using the comma-operator
1576 * of the form: (a, b, c) = x should not assign to c...
1579 compile_error(ast_ctx(self), "not an l-value (code-block)");
1583 if (self->expression.outr) {
1584 *out = self->expression.outr;
1588 /* output is NULL at first, we'll have each expression
1589 * assign to out output, thus, a comma-operator represention
1590 * using an ast_block will return the last generated value,
1591 * so: (b, c) + a executed both b and c, and returns c,
1592 * which is then added to a.
1596 /* generate locals */
1597 for (i = 0; i < vec_size(self->locals); ++i)
1599 if (!ast_local_codegen(self->locals[i], func->ir_func, false)) {
1601 compile_error(ast_ctx(self), "failed to generate local `%s`", self->locals[i]->name);
1606 for (i = 0; i < vec_size(self->exprs); ++i)
1608 ast_expression_codegen *gen;
1609 if (func->curblock->final && !ast_istype(self->exprs[i], ast_label)) {
1610 if (compile_warning(ast_ctx(self->exprs[i]), WARN_UNREACHABLE_CODE, "unreachable statement"))
1614 gen = self->exprs[i]->expression.codegen;
1615 if (!(*gen)(self->exprs[i], func, false, out))
1619 self->expression.outr = *out;
1624 bool ast_store_codegen(ast_store *self, ast_function *func, bool lvalue, ir_value **out)
1626 ast_expression_codegen *cgen;
1627 ir_value *left = NULL;
1628 ir_value *right = NULL;
1632 ast_array_index *ai = NULL;
1634 if (lvalue && self->expression.outl) {
1635 *out = self->expression.outl;
1639 if (!lvalue && self->expression.outr) {
1640 *out = self->expression.outr;
1644 if (ast_istype(self->dest, ast_array_index))
1647 ai = (ast_array_index*)self->dest;
1648 idx = (ast_value*)ai->index;
1650 if (ast_istype(ai->index, ast_value) && idx->hasvalue && idx->cvq == CV_CONST)
1655 /* we need to call the setter */
1656 ir_value *iridx, *funval;
1660 compile_error(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1664 arr = (ast_value*)ai->array;
1665 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1666 compile_error(ast_ctx(self), "value has no setter (%s)", arr->name);
1670 cgen = idx->expression.codegen;
1671 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1674 cgen = arr->setter->expression.codegen;
1675 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
1678 cgen = self->source->expression.codegen;
1679 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1682 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "store"), funval, false);
1685 ir_call_param(call, iridx);
1686 ir_call_param(call, right);
1687 self->expression.outr = right;
1693 cgen = self->dest->expression.codegen;
1695 if (!(*cgen)((ast_expression*)(self->dest), func, true, &left))
1697 self->expression.outl = left;
1699 cgen = self->source->expression.codegen;
1701 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1704 if (!ir_block_create_store_op(func->curblock, ast_ctx(self), self->op, left, right))
1706 self->expression.outr = right;
1709 /* Theoretically, an assinment returns its left side as an
1710 * lvalue, if we don't need an lvalue though, we return
1711 * the right side as an rvalue, otherwise we have to
1712 * somehow know whether or not we need to dereference the pointer
1713 * on the left side - that is: OP_LOAD if it was an address.
1714 * Also: in original QC we cannot OP_LOADP *anyway*.
1716 *out = (lvalue ? left : right);
1721 bool ast_binary_codegen(ast_binary *self, ast_function *func, bool lvalue, ir_value **out)
1723 ast_expression_codegen *cgen;
1724 ir_value *left, *right;
1726 /* A binary operation cannot yield an l-value */
1728 compile_error(ast_ctx(self), "not an l-value (binop)");
1732 if (self->expression.outr) {
1733 *out = self->expression.outr;
1737 if ((OPTS_FLAG(SHORT_LOGIC) || OPTS_FLAG(PERL_LOGIC)) &&
1738 (self->op == INSTR_AND || self->op == INSTR_OR))
1740 /* short circuit evaluation */
1741 ir_block *other, *merge;
1742 ir_block *from_left, *from_right;
1746 /* prepare end-block */
1747 merge_id = vec_size(func->ir_func->blocks);
1748 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "sce_merge"));
1750 /* generate the left expression */
1751 cgen = self->left->expression.codegen;
1752 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1754 /* remember the block */
1755 from_left = func->curblock;
1757 /* create a new block for the right expression */
1758 other = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "sce_other"));
1759 if (self->op == INSTR_AND) {
1760 /* on AND: left==true -> other */
1761 if (!ir_block_create_if(func->curblock, ast_ctx(self), left, other, merge))
1764 /* on OR: left==false -> other */
1765 if (!ir_block_create_if(func->curblock, ast_ctx(self), left, merge, other))
1768 /* use the likely flag */
1769 vec_last(func->curblock->instr)->likely = true;
1771 /* enter the right-expression's block */
1772 func->curblock = other;
1774 cgen = self->right->expression.codegen;
1775 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1777 /* remember block */
1778 from_right = func->curblock;
1780 /* jump to the merge block */
1781 if (!ir_block_create_jump(func->curblock, ast_ctx(self), merge))
1784 vec_remove(func->ir_func->blocks, merge_id, 1);
1785 vec_push(func->ir_func->blocks, merge);
1787 func->curblock = merge;
1788 phi = ir_block_create_phi(func->curblock, ast_ctx(self),
1789 ast_function_label(func, "sce_value"),
1790 self->expression.vtype);
1791 ir_phi_add(phi, from_left, left);
1792 ir_phi_add(phi, from_right, right);
1793 *out = ir_phi_value(phi);
1797 if (!OPTS_FLAG(PERL_LOGIC)) {
1799 if (OPTS_FLAG(CORRECT_LOGIC) && (*out)->vtype == TYPE_VECTOR) {
1800 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
1801 ast_function_label(func, "sce_bool_v"),
1805 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
1806 ast_function_label(func, "sce_bool"),
1811 else if (OPTS_FLAG(FALSE_EMPTY_STRINGS) && (*out)->vtype == TYPE_STRING) {
1812 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
1813 ast_function_label(func, "sce_bool_s"),
1817 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
1818 ast_function_label(func, "sce_bool"),
1824 *out = ir_block_create_binop(func->curblock, ast_ctx(self),
1825 ast_function_label(func, "sce_bool"),
1826 INSTR_AND, *out, *out);
1832 self->expression.outr = *out;
1836 cgen = self->left->expression.codegen;
1837 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1840 cgen = self->right->expression.codegen;
1841 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1844 *out = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "bin"),
1845 self->op, left, right);
1848 self->expression.outr = *out;
1853 bool ast_binstore_codegen(ast_binstore *self, ast_function *func, bool lvalue, ir_value **out)
1855 ast_expression_codegen *cgen;
1856 ir_value *leftl = NULL, *leftr, *right, *bin;
1860 ast_array_index *ai = NULL;
1861 ir_value *iridx = NULL;
1863 if (lvalue && self->expression.outl) {
1864 *out = self->expression.outl;
1868 if (!lvalue && self->expression.outr) {
1869 *out = self->expression.outr;
1873 if (ast_istype(self->dest, ast_array_index))
1876 ai = (ast_array_index*)self->dest;
1877 idx = (ast_value*)ai->index;
1879 if (ast_istype(ai->index, ast_value) && idx->hasvalue && idx->cvq == CV_CONST)
1883 /* for a binstore we need both an lvalue and an rvalue for the left side */
1884 /* rvalue of destination! */
1886 cgen = idx->expression.codegen;
1887 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1890 cgen = self->dest->expression.codegen;
1891 if (!(*cgen)((ast_expression*)(self->dest), func, false, &leftr))
1894 /* source as rvalue only */
1895 cgen = self->source->expression.codegen;
1896 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1899 /* now the binary */
1900 bin = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "binst"),
1901 self->opbin, leftr, right);
1902 self->expression.outr = bin;
1906 /* we need to call the setter */
1911 compile_error(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1915 arr = (ast_value*)ai->array;
1916 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1917 compile_error(ast_ctx(self), "value has no setter (%s)", arr->name);
1921 cgen = arr->setter->expression.codegen;
1922 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
1925 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "store"), funval, false);
1928 ir_call_param(call, iridx);
1929 ir_call_param(call, bin);
1930 self->expression.outr = bin;
1932 /* now store them */
1933 cgen = self->dest->expression.codegen;
1934 /* lvalue of destination */
1935 if (!(*cgen)((ast_expression*)(self->dest), func, true, &leftl))
1937 self->expression.outl = leftl;
1939 if (!ir_block_create_store_op(func->curblock, ast_ctx(self), self->opstore, leftl, bin))
1941 self->expression.outr = bin;
1944 /* Theoretically, an assinment returns its left side as an
1945 * lvalue, if we don't need an lvalue though, we return
1946 * the right side as an rvalue, otherwise we have to
1947 * somehow know whether or not we need to dereference the pointer
1948 * on the left side - that is: OP_LOAD if it was an address.
1949 * Also: in original QC we cannot OP_LOADP *anyway*.
1951 *out = (lvalue ? leftl : bin);
1956 bool ast_unary_codegen(ast_unary *self, ast_function *func, bool lvalue, ir_value **out)
1958 ast_expression_codegen *cgen;
1961 /* An unary operation cannot yield an l-value */
1963 compile_error(ast_ctx(self), "not an l-value (binop)");
1967 if (self->expression.outr) {
1968 *out = self->expression.outr;
1972 cgen = self->operand->expression.codegen;
1974 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
1977 *out = ir_block_create_unary(func->curblock, ast_ctx(self), ast_function_label(func, "unary"),
1981 self->expression.outr = *out;
1986 bool ast_return_codegen(ast_return *self, ast_function *func, bool lvalue, ir_value **out)
1988 ast_expression_codegen *cgen;
1993 /* In the context of a return operation, we don't actually return
1997 compile_error(ast_ctx(self), "return-expression is not an l-value");
2001 if (self->expression.outr) {
2002 compile_error(ast_ctx(self), "internal error: ast_return cannot be reused, it bears no result!");
2005 self->expression.outr = (ir_value*)1;
2007 if (self->operand) {
2008 cgen = self->operand->expression.codegen;
2010 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
2013 if (!ir_block_create_return(func->curblock, ast_ctx(self), operand))
2016 if (!ir_block_create_return(func->curblock, ast_ctx(self), NULL))
2023 bool ast_entfield_codegen(ast_entfield *self, ast_function *func, bool lvalue, ir_value **out)
2025 ast_expression_codegen *cgen;
2026 ir_value *ent, *field;
2028 /* This function needs to take the 'lvalue' flag into account!
2029 * As lvalue we provide a field-pointer, as rvalue we provide the
2033 if (lvalue && self->expression.outl) {
2034 *out = self->expression.outl;
2038 if (!lvalue && self->expression.outr) {
2039 *out = self->expression.outr;
2043 cgen = self->entity->expression.codegen;
2044 if (!(*cgen)((ast_expression*)(self->entity), func, false, &ent))
2047 cgen = self->field->expression.codegen;
2048 if (!(*cgen)((ast_expression*)(self->field), func, false, &field))
2053 *out = ir_block_create_fieldaddress(func->curblock, ast_ctx(self), ast_function_label(func, "efa"),
2056 *out = ir_block_create_load_from_ent(func->curblock, ast_ctx(self), ast_function_label(func, "efv"),
2057 ent, field, self->expression.vtype);
2058 /* Done AFTER error checking:
2059 codegen_output_type(self, *out);
2063 compile_error(ast_ctx(self), "failed to create %s instruction (output type %s)",
2064 (lvalue ? "ADDRESS" : "FIELD"),
2065 type_name[self->expression.vtype]);
2069 codegen_output_type(self, *out);
2072 self->expression.outl = *out;
2074 self->expression.outr = *out;
2076 /* Hm that should be it... */
2080 bool ast_member_codegen(ast_member *self, ast_function *func, bool lvalue, ir_value **out)
2082 ast_expression_codegen *cgen;
2085 /* in QC this is always an lvalue */
2086 if (lvalue && self->rvalue) {
2087 compile_error(ast_ctx(self), "not an l-value (member access)");
2090 if (self->expression.outl) {
2091 *out = self->expression.outl;
2095 cgen = self->owner->expression.codegen;
2096 if (!(*cgen)((ast_expression*)(self->owner), func, false, &vec))
2099 if (vec->vtype != TYPE_VECTOR &&
2100 !(vec->vtype == TYPE_FIELD && self->owner->expression.next->expression.vtype == TYPE_VECTOR))
2105 *out = ir_value_vector_member(vec, self->field);
2106 self->expression.outl = *out;
2108 return (*out != NULL);
2111 bool ast_array_index_codegen(ast_array_index *self, ast_function *func, bool lvalue, ir_value **out)
2116 if (!lvalue && self->expression.outr) {
2117 *out = self->expression.outr;
2119 if (lvalue && self->expression.outl) {
2120 *out = self->expression.outl;
2123 if (!ast_istype(self->array, ast_value)) {
2124 compile_error(ast_ctx(self), "array indexing this way is not supported");
2125 /* note this would actually be pointer indexing because the left side is
2126 * not an actual array but (hopefully) an indexable expression.
2127 * Once we get integer arithmetic, and GADDRESS/GSTORE/GLOAD instruction
2128 * support this path will be filled.
2133 arr = (ast_value*)self->array;
2134 idx = (ast_value*)self->index;
2136 if (!ast_istype(self->index, ast_value) || !idx->hasvalue || idx->cvq != CV_CONST) {
2137 /* Time to use accessor functions */
2138 ast_expression_codegen *cgen;
2139 ir_value *iridx, *funval;
2143 compile_error(ast_ctx(self), "(.2) array indexing here needs a compile-time constant");
2148 compile_error(ast_ctx(self), "value has no getter, don't know how to index it");
2152 cgen = self->index->expression.codegen;
2153 if (!(*cgen)((ast_expression*)(self->index), func, false, &iridx))
2156 cgen = arr->getter->expression.codegen;
2157 if (!(*cgen)((ast_expression*)(arr->getter), func, true, &funval))
2160 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "fetch"), funval, false);
2163 ir_call_param(call, iridx);
2165 *out = ir_call_value(call);
2166 self->expression.outr = *out;
2170 if (idx->expression.vtype == TYPE_FLOAT) {
2171 unsigned int arridx = idx->constval.vfloat;
2172 if (arridx >= self->array->expression.count)
2174 compile_error(ast_ctx(self), "array index out of bounds: %i", arridx);
2177 *out = arr->ir_values[arridx];
2179 else if (idx->expression.vtype == TYPE_INTEGER) {
2180 unsigned int arridx = idx->constval.vint;
2181 if (arridx >= self->array->expression.count)
2183 compile_error(ast_ctx(self), "array index out of bounds: %i", arridx);
2186 *out = arr->ir_values[arridx];
2189 compile_error(ast_ctx(self), "array indexing here needs an integer constant");
2195 bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_value **out)
2197 ast_expression_codegen *cgen;
2205 ir_block *ontrue_endblock = NULL;
2206 ir_block *onfalse_endblock = NULL;
2207 ir_block *merge = NULL;
2209 /* We don't output any value, thus also don't care about r/lvalue */
2213 if (self->expression.outr) {
2214 compile_error(ast_ctx(self), "internal error: ast_ifthen cannot be reused, it bears no result!");
2217 self->expression.outr = (ir_value*)1;
2219 /* generate the condition */
2220 cgen = self->cond->expression.codegen;
2221 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2223 /* update the block which will get the jump - because short-logic or ternaries may have changed this */
2224 cond = func->curblock;
2228 if (self->on_true) {
2229 /* create on-true block */
2230 ontrue = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "ontrue"));
2234 /* enter the block */
2235 func->curblock = ontrue;
2238 cgen = self->on_true->expression.codegen;
2239 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &dummy))
2242 /* we now need to work from the current endpoint */
2243 ontrue_endblock = func->curblock;
2248 if (self->on_false) {
2249 /* create on-false block */
2250 onfalse = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "onfalse"));
2254 /* enter the block */
2255 func->curblock = onfalse;
2258 cgen = self->on_false->expression.codegen;
2259 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &dummy))
2262 /* we now need to work from the current endpoint */
2263 onfalse_endblock = func->curblock;
2267 /* Merge block were they all merge in to */
2268 if (!ontrue || !onfalse || !ontrue_endblock->final || !onfalse_endblock->final)
2270 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "endif"));
2273 /* add jumps ot the merge block */
2274 if (ontrue && !ontrue_endblock->final && !ir_block_create_jump(ontrue_endblock, ast_ctx(self), merge))
2276 if (onfalse && !onfalse_endblock->final && !ir_block_create_jump(onfalse_endblock, ast_ctx(self), merge))
2279 /* Now enter the merge block */
2280 func->curblock = merge;
2283 /* we create the if here, that way all blocks are ordered :)
2285 if (!ir_block_create_if(cond, ast_ctx(self), condval,
2286 (ontrue ? ontrue : merge),
2287 (onfalse ? onfalse : merge)))
2295 bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_value **out)
2297 ast_expression_codegen *cgen;
2300 ir_value *trueval, *falseval;
2303 ir_block *cond = func->curblock;
2304 ir_block *cond_out = NULL;
2305 ir_block *ontrue, *ontrue_out = NULL;
2306 ir_block *onfalse, *onfalse_out = NULL;
2309 /* Ternary can never create an lvalue... */
2313 /* In theory it shouldn't be possible to pass through a node twice, but
2314 * in case we add any kind of optimization pass for the AST itself, it
2315 * may still happen, thus we remember a created ir_value and simply return one
2316 * if it already exists.
2318 if (self->expression.outr) {
2319 *out = self->expression.outr;
2323 /* In the following, contraty to ast_ifthen, we assume both paths exist. */
2325 /* generate the condition */
2326 func->curblock = cond;
2327 cgen = self->cond->expression.codegen;
2328 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2330 cond_out = func->curblock;
2332 /* create on-true block */
2333 ontrue = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_T"));
2338 /* enter the block */
2339 func->curblock = ontrue;
2342 cgen = self->on_true->expression.codegen;
2343 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &trueval))
2346 ontrue_out = func->curblock;
2349 /* create on-false block */
2350 onfalse = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_F"));
2355 /* enter the block */
2356 func->curblock = onfalse;
2359 cgen = self->on_false->expression.codegen;
2360 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &falseval))
2363 onfalse_out = func->curblock;
2366 /* create merge block */
2367 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_out"));
2370 /* jump to merge block */
2371 if (!ir_block_create_jump(ontrue_out, ast_ctx(self), merge))
2373 if (!ir_block_create_jump(onfalse_out, ast_ctx(self), merge))
2376 /* create if instruction */
2377 if (!ir_block_create_if(cond_out, ast_ctx(self), condval, ontrue, onfalse))
2380 /* Now enter the merge block */
2381 func->curblock = merge;
2383 /* Here, now, we need a PHI node
2384 * but first some sanity checking...
2386 if (trueval->vtype != falseval->vtype) {
2387 /* error("ternary with different types on the two sides"); */
2392 phi = ir_block_create_phi(merge, ast_ctx(self), ast_function_label(func, "phi"), trueval->vtype);
2395 ir_phi_add(phi, ontrue_out, trueval);
2396 ir_phi_add(phi, onfalse_out, falseval);
2398 self->expression.outr = ir_phi_value(phi);
2399 *out = self->expression.outr;
2401 codegen_output_type(self, *out);
2406 bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value **out)
2408 ast_expression_codegen *cgen;
2410 ir_value *dummy = NULL;
2411 ir_value *precond = NULL;
2412 ir_value *postcond = NULL;
2414 /* Since we insert some jumps "late" so we have blocks
2415 * ordered "nicely", we need to keep track of the actual end-blocks
2416 * of expressions to add the jumps to.
2418 ir_block *bbody = NULL, *end_bbody = NULL;
2419 ir_block *bprecond = NULL, *end_bprecond = NULL;
2420 ir_block *bpostcond = NULL, *end_bpostcond = NULL;
2421 ir_block *bincrement = NULL, *end_bincrement = NULL;
2422 ir_block *bout = NULL, *bin = NULL;
2424 /* let's at least move the outgoing block to the end */
2427 /* 'break' and 'continue' need to be able to find the right blocks */
2428 ir_block *bcontinue = NULL;
2429 ir_block *bbreak = NULL;
2431 ir_block *tmpblock = NULL;
2436 if (self->expression.outr) {
2437 compile_error(ast_ctx(self), "internal error: ast_loop cannot be reused, it bears no result!");
2440 self->expression.outr = (ir_value*)1;
2443 * Should we ever need some kind of block ordering, better make this function
2444 * move blocks around than write a block ordering algorithm later... after all
2445 * the ast and ir should work together, not against each other.
2448 /* initexpr doesn't get its own block, it's pointless, it could create more blocks
2449 * anyway if for example it contains a ternary.
2453 cgen = self->initexpr->expression.codegen;
2454 if (!(*cgen)((ast_expression*)(self->initexpr), func, false, &dummy))
2458 /* Store the block from which we enter this chaos */
2459 bin = func->curblock;
2461 /* The pre-loop condition needs its own block since we
2462 * need to be able to jump to the start of that expression.
2466 bprecond = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "pre_loop_cond"));
2470 /* the pre-loop-condition the least important place to 'continue' at */
2471 bcontinue = bprecond;
2474 func->curblock = bprecond;
2477 cgen = self->precond->expression.codegen;
2478 if (!(*cgen)((ast_expression*)(self->precond), func, false, &precond))
2481 end_bprecond = func->curblock;
2483 bprecond = end_bprecond = NULL;
2486 /* Now the next blocks won't be ordered nicely, but we need to
2487 * generate them this early for 'break' and 'continue'.
2489 if (self->increment) {
2490 bincrement = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "loop_increment"));
2493 bcontinue = bincrement; /* increment comes before the pre-loop-condition */
2495 bincrement = end_bincrement = NULL;
2498 if (self->postcond) {
2499 bpostcond = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "post_loop_cond"));
2502 bcontinue = bpostcond; /* postcond comes before the increment */
2504 bpostcond = end_bpostcond = NULL;
2507 bout_id = vec_size(func->ir_func->blocks);
2508 bout = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "after_loop"));
2513 /* The loop body... */
2514 /* if (self->body) */
2516 bbody = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "loop_body"));
2521 func->curblock = bbody;
2523 vec_push(func->breakblocks, bbreak);
2525 vec_push(func->continueblocks, bcontinue);
2527 vec_push(func->continueblocks, bbody);
2531 cgen = self->body->expression.codegen;
2532 if (!(*cgen)((ast_expression*)(self->body), func, false, &dummy))
2536 end_bbody = func->curblock;
2537 vec_pop(func->breakblocks);
2538 vec_pop(func->continueblocks);
2541 /* post-loop-condition */
2545 func->curblock = bpostcond;
2548 cgen = self->postcond->expression.codegen;
2549 if (!(*cgen)((ast_expression*)(self->postcond), func, false, &postcond))
2552 end_bpostcond = func->curblock;
2555 /* The incrementor */
2556 if (self->increment)
2559 func->curblock = bincrement;
2562 cgen = self->increment->expression.codegen;
2563 if (!(*cgen)((ast_expression*)(self->increment), func, false, &dummy))
2566 end_bincrement = func->curblock;
2569 /* In any case now, we continue from the outgoing block */
2570 func->curblock = bout;
2572 /* Now all blocks are in place */
2573 /* From 'bin' we jump to whatever comes first */
2574 if (bprecond) tmpblock = bprecond;
2575 else if (bbody) tmpblock = bbody;
2576 else if (bpostcond) tmpblock = bpostcond;
2577 else tmpblock = bout;
2578 if (!ir_block_create_jump(bin, ast_ctx(self), tmpblock))
2584 ir_block *ontrue, *onfalse;
2585 if (bbody) ontrue = bbody;
2586 else if (bincrement) ontrue = bincrement;
2587 else if (bpostcond) ontrue = bpostcond;
2588 else ontrue = bprecond;
2590 if (self->pre_not) {
2595 if (!ir_block_create_if(end_bprecond, ast_ctx(self), precond, ontrue, onfalse))
2602 if (bincrement) tmpblock = bincrement;
2603 else if (bpostcond) tmpblock = bpostcond;
2604 else if (bprecond) tmpblock = bprecond;
2605 else tmpblock = bbody;
2606 if (!end_bbody->final && !ir_block_create_jump(end_bbody, ast_ctx(self), tmpblock))
2610 /* from increment */
2613 if (bpostcond) tmpblock = bpostcond;
2614 else if (bprecond) tmpblock = bprecond;
2615 else if (bbody) tmpblock = bbody;
2616 else tmpblock = bout;
2617 if (!ir_block_create_jump(end_bincrement, ast_ctx(self), tmpblock))
2624 ir_block *ontrue, *onfalse;
2625 if (bprecond) ontrue = bprecond;
2626 else if (bbody) ontrue = bbody;
2627 else if (bincrement) ontrue = bincrement;
2628 else ontrue = bpostcond;
2630 if (self->post_not) {
2635 if (!ir_block_create_if(end_bpostcond, ast_ctx(self), postcond, ontrue, onfalse))
2639 /* Move 'bout' to the end */
2640 vec_remove(func->ir_func->blocks, bout_id, 1);
2641 vec_push(func->ir_func->blocks, bout);
2646 bool ast_breakcont_codegen(ast_breakcont *self, ast_function *func, bool lvalue, ir_value **out)
2653 compile_error(ast_ctx(self), "break/continue expression is not an l-value");
2657 if (self->expression.outr) {
2658 compile_error(ast_ctx(self), "internal error: ast_breakcont cannot be reused!");
2661 self->expression.outr = (ir_value*)1;
2663 if (self->is_continue)
2664 target = func->continueblocks[vec_size(func->continueblocks)-1-self->levels];
2666 target = func->breakblocks[vec_size(func->breakblocks)-1-self->levels];
2669 compile_error(ast_ctx(self), "%s is lacking a target block", (self->is_continue ? "continue" : "break"));
2673 if (!ir_block_create_jump(func->curblock, ast_ctx(self), target))
2678 bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_value **out)
2680 ast_expression_codegen *cgen;
2682 ast_switch_case *def_case = NULL;
2683 ir_block *def_bfall = NULL;
2684 ir_block *def_bfall_to = NULL;
2685 bool set_def_bfall_to = false;
2687 ir_value *dummy = NULL;
2688 ir_value *irop = NULL;
2689 ir_block *bout = NULL;
2690 ir_block *bfall = NULL;
2698 compile_error(ast_ctx(self), "switch expression is not an l-value");
2702 if (self->expression.outr) {
2703 compile_error(ast_ctx(self), "internal error: ast_switch cannot be reused!");
2706 self->expression.outr = (ir_value*)1;
2711 cgen = self->operand->expression.codegen;
2712 if (!(*cgen)((ast_expression*)(self->operand), func, false, &irop))
2715 if (!vec_size(self->cases))
2718 cmpinstr = type_eq_instr[irop->vtype];
2719 if (cmpinstr >= AINSTR_END) {
2720 ast_type_to_string(self->operand, typestr, sizeof(typestr));
2721 compile_error(ast_ctx(self), "invalid type to perform a switch on: %s", typestr);
2725 bout_id = vec_size(func->ir_func->blocks);
2726 bout = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "after_switch"));
2730 /* setup the break block */
2731 vec_push(func->breakblocks, bout);
2733 /* Now create all cases */
2734 for (c = 0; c < vec_size(self->cases); ++c) {
2735 ir_value *cond, *val;
2736 ir_block *bcase, *bnot;
2739 ast_switch_case *swcase = &self->cases[c];
2741 if (swcase->value) {
2742 /* A regular case */
2743 /* generate the condition operand */
2744 cgen = swcase->value->expression.codegen;
2745 if (!(*cgen)((ast_expression*)(swcase->value), func, false, &val))
2747 /* generate the condition */
2748 cond = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "switch_eq"), cmpinstr, irop, val);
2752 bcase = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "case"));
2753 bnot_id = vec_size(func->ir_func->blocks);
2754 bnot = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "not_case"));
2755 if (!bcase || !bnot)
2757 if (set_def_bfall_to) {
2758 set_def_bfall_to = false;
2759 def_bfall_to = bcase;
2761 if (!ir_block_create_if(func->curblock, ast_ctx(self), cond, bcase, bnot))
2764 /* Make the previous case-end fall through */
2765 if (bfall && !bfall->final) {
2766 if (!ir_block_create_jump(bfall, ast_ctx(self), bcase))
2770 /* enter the case */
2771 func->curblock = bcase;
2772 cgen = swcase->code->expression.codegen;
2773 if (!(*cgen)((ast_expression*)swcase->code, func, false, &dummy))
2776 /* remember this block to fall through from */
2777 bfall = func->curblock;
2779 /* enter the else and move it down */
2780 func->curblock = bnot;
2781 vec_remove(func->ir_func->blocks, bnot_id, 1);
2782 vec_push(func->ir_func->blocks, bnot);
2784 /* The default case */
2785 /* Remember where to fall through from: */
2788 /* remember which case it was */
2790 /* And the next case will be remembered */
2791 set_def_bfall_to = true;
2795 /* Jump from the last bnot to bout */
2796 if (bfall && !bfall->final && !ir_block_create_jump(bfall, ast_ctx(self), bout)) {
2798 astwarning(ast_ctx(bfall), WARN_???, "missing break after last case");
2803 /* If there was a default case, put it down here */
2807 /* No need to create an extra block */
2808 bcase = func->curblock;
2810 /* Insert the fallthrough jump */
2811 if (def_bfall && !def_bfall->final) {
2812 if (!ir_block_create_jump(def_bfall, ast_ctx(self), bcase))
2816 /* Now generate the default code */
2817 cgen = def_case->code->expression.codegen;
2818 if (!(*cgen)((ast_expression*)def_case->code, func, false, &dummy))
2821 /* see if we need to fall through */
2822 if (def_bfall_to && !func->curblock->final)
2824 if (!ir_block_create_jump(func->curblock, ast_ctx(self), def_bfall_to))
2829 /* Jump from the last bnot to bout */
2830 if (!func->curblock->final && !ir_block_create_jump(func->curblock, ast_ctx(self), bout))
2832 /* enter the outgoing block */
2833 func->curblock = bout;
2835 /* restore the break block */
2836 vec_pop(func->breakblocks);
2838 /* Move 'bout' to the end, it's nicer */
2839 vec_remove(func->ir_func->blocks, bout_id, 1);
2840 vec_push(func->ir_func->blocks, bout);
2845 bool ast_label_codegen(ast_label *self, ast_function *func, bool lvalue, ir_value **out)
2852 compile_error(ast_ctx(self), "internal error: ast_label cannot be an lvalue");
2856 /* simply create a new block and jump to it */
2857 self->irblock = ir_function_create_block(ast_ctx(self), func->ir_func, self->name);
2858 if (!self->irblock) {
2859 compile_error(ast_ctx(self), "failed to allocate label block `%s`", self->name);
2862 if (!func->curblock->final) {
2863 if (!ir_block_create_jump(func->curblock, ast_ctx(self), self->irblock))
2867 /* enter the new block */
2868 func->curblock = self->irblock;
2870 /* Generate all the leftover gotos */
2871 for (i = 0; i < vec_size(self->gotos); ++i) {
2872 if (!ast_goto_codegen(self->gotos[i], func, false, &dummy))
2879 bool ast_goto_codegen(ast_goto *self, ast_function *func, bool lvalue, ir_value **out)
2883 compile_error(ast_ctx(self), "internal error: ast_goto cannot be an lvalue");
2887 if (self->target->irblock) {
2888 if (self->irblock_from) {
2889 /* we already tried once, this is the callback */
2890 self->irblock_from->final = false;
2891 if (!ir_block_create_goto(self->irblock_from, ast_ctx(self), self->target->irblock)) {
2892 compile_error(ast_ctx(self), "failed to generate goto to `%s`", self->name);
2898 if (!ir_block_create_goto(func->curblock, ast_ctx(self), self->target->irblock)) {
2899 compile_error(ast_ctx(self), "failed to generate goto to `%s`", self->name);
2906 /* the target has not yet been created...
2907 * close this block in a sneaky way:
2909 func->curblock->final = true;
2910 self->irblock_from = func->curblock;
2911 ast_label_register_goto(self->target, self);
2917 bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value **out)
2919 ast_expression_codegen *cgen;
2921 ir_instr *callinstr;
2924 ir_value *funval = NULL;
2926 /* return values are never lvalues */
2928 compile_error(ast_ctx(self), "not an l-value (function call)");
2932 if (self->expression.outr) {
2933 *out = self->expression.outr;
2937 cgen = self->func->expression.codegen;
2938 if (!(*cgen)((ast_expression*)(self->func), func, false, &funval))
2946 for (i = 0; i < vec_size(self->params); ++i)
2949 ast_expression *expr = self->params[i];
2951 cgen = expr->expression.codegen;
2952 if (!(*cgen)(expr, func, false, ¶m))
2956 vec_push(params, param);
2959 callinstr = ir_block_create_call(func->curblock, ast_ctx(self),
2960 ast_function_label(func, "call"),
2961 funval, !!(self->func->expression.flags & AST_FLAG_NORETURN));
2965 for (i = 0; i < vec_size(params); ++i) {
2966 ir_call_param(callinstr, params[i]);
2969 *out = ir_call_value(callinstr);
2970 self->expression.outr = *out;
2972 codegen_output_type(self, *out);