2 * Copyright (C) 2012, 2013
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of
7 * this software and associated documentation files (the "Software"), to deal in
8 * the Software without restriction, including without limitation the rights to
9 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10 * of the Software, and to permit persons to whom the Software is furnished to do
11 * so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 #define ast_instantiate(T, ctx, destroyfn) \
32 T* self = (T*)mem_a(sizeof(T)); \
36 ast_node_init((ast_node*)self, ctx, TYPE_##T); \
37 ( (ast_node*)self )->node.destroy = (ast_node_delete*)destroyfn
40 /* It must not be possible to get here. */
41 static GMQCC_NORETURN void _ast_node_destroy(ast_node *self)
44 con_err("ast node missing destroy()\n");
48 /* Initialize main ast node aprts */
49 static void ast_node_init(ast_node *self, lex_ctx ctx, int nodetype)
51 self->node.context = ctx;
52 self->node.destroy = &_ast_node_destroy;
53 self->node.keep = false;
54 self->node.nodetype = nodetype;
55 self->node.side_effects = false;
58 /* weight and side effects */
59 static void _ast_propagate_effects(ast_node *self, ast_node *other)
61 if (ast_side_effects(other))
62 ast_side_effects(self) = true;
64 #define ast_propagate_effects(s,o) _ast_propagate_effects(((ast_node*)(s)), ((ast_node*)(o)))
66 /* General expression initialization */
67 static void ast_expression_init(ast_expression *self,
68 ast_expression_codegen *codegen)
70 self->expression.codegen = codegen;
71 self->expression.vtype = TYPE_VOID;
72 self->expression.next = NULL;
73 self->expression.outl = NULL;
74 self->expression.outr = NULL;
75 self->expression.params = NULL;
76 self->expression.count = 0;
77 self->expression.flags = 0;
80 static void ast_expression_delete(ast_expression *self)
83 if (self->expression.next)
84 ast_delete(self->expression.next);
85 for (i = 0; i < vec_size(self->expression.params); ++i) {
86 ast_delete(self->expression.params[i]);
88 vec_free(self->expression.params);
91 static void ast_expression_delete_full(ast_expression *self)
93 ast_expression_delete(self);
97 ast_value* ast_value_copy(const ast_value *self)
100 const ast_expression_common *fromex;
101 ast_expression_common *selfex;
102 ast_value *cp = ast_value_new(self->expression.node.context, self->name, self->expression.vtype);
103 if (self->expression.next) {
104 cp->expression.next = ast_type_copy(self->expression.node.context, self->expression.next);
105 if (!cp->expression.next) {
106 ast_value_delete(cp);
110 fromex = &self->expression;
111 selfex = &cp->expression;
112 selfex->count = fromex->count;
113 selfex->flags = fromex->flags;
114 for (i = 0; i < vec_size(fromex->params); ++i) {
115 ast_value *v = ast_value_copy(fromex->params[i]);
117 ast_value_delete(cp);
120 vec_push(selfex->params, v);
125 bool ast_type_adopt_impl(ast_expression *self, const ast_expression *other)
128 const ast_expression_common *fromex;
129 ast_expression_common *selfex;
130 self->expression.vtype = other->expression.vtype;
131 if (other->expression.next) {
132 self->expression.next = (ast_expression*)ast_type_copy(ast_ctx(self), other->expression.next);
133 if (!self->expression.next)
136 fromex = &other->expression;
137 selfex = &self->expression;
138 selfex->count = fromex->count;
139 selfex->flags = fromex->flags;
140 for (i = 0; i < vec_size(fromex->params); ++i) {
141 ast_value *v = ast_value_copy(fromex->params[i]);
144 vec_push(selfex->params, v);
149 static ast_expression* ast_shallow_type(lex_ctx ctx, int vtype)
151 ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
152 ast_expression_init(self, NULL);
153 self->expression.codegen = NULL;
154 self->expression.next = NULL;
155 self->expression.vtype = vtype;
159 ast_expression* ast_type_copy(lex_ctx ctx, const ast_expression *ex)
162 const ast_expression_common *fromex;
163 ast_expression_common *selfex;
169 ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
170 ast_expression_init(self, NULL);
172 fromex = &ex->expression;
173 selfex = &self->expression;
175 /* This may never be codegen()d */
176 selfex->codegen = NULL;
178 selfex->vtype = fromex->vtype;
181 selfex->next = ast_type_copy(ctx, fromex->next);
183 ast_expression_delete_full(self);
190 selfex->count = fromex->count;
191 selfex->flags = fromex->flags;
192 for (i = 0; i < vec_size(fromex->params); ++i) {
193 ast_value *v = ast_value_copy(fromex->params[i]);
195 ast_expression_delete_full(self);
198 vec_push(selfex->params, v);
205 bool ast_compare_type(ast_expression *a, ast_expression *b)
207 if (a->expression.vtype == TYPE_NIL ||
208 b->expression.vtype == TYPE_NIL)
210 if (a->expression.vtype != b->expression.vtype)
212 if (!a->expression.next != !b->expression.next)
214 if (vec_size(a->expression.params) != vec_size(b->expression.params))
216 if ((a->expression.flags & AST_FLAG_TYPE_MASK) !=
217 (b->expression.flags & AST_FLAG_TYPE_MASK) )
221 if (vec_size(a->expression.params)) {
223 for (i = 0; i < vec_size(a->expression.params); ++i) {
224 if (!ast_compare_type((ast_expression*)a->expression.params[i],
225 (ast_expression*)b->expression.params[i]))
229 if (a->expression.next)
230 return ast_compare_type(a->expression.next, b->expression.next);
234 static size_t ast_type_to_string_impl(ast_expression *e, char *buf, size_t bufsize, size_t pos)
241 if (pos + 6 >= bufsize)
243 strcpy(buf + pos, "(null)");
247 if (pos + 1 >= bufsize)
250 switch (e->expression.vtype) {
252 strcpy(buf + pos, "(variant)");
257 return ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
260 if (pos + 3 >= bufsize)
264 pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
265 if (pos + 1 >= bufsize)
271 pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
272 if (pos + 2 >= bufsize)
274 if (!vec_size(e->expression.params)) {
280 pos = ast_type_to_string_impl((ast_expression*)(e->expression.params[0]), buf, bufsize, pos);
281 for (i = 1; i < vec_size(e->expression.params); ++i) {
282 if (pos + 2 >= bufsize)
286 pos = ast_type_to_string_impl((ast_expression*)(e->expression.params[i]), buf, bufsize, pos);
288 if (pos + 1 >= bufsize)
294 pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
295 if (pos + 1 >= bufsize)
298 pos += snprintf(buf + pos, bufsize - pos - 1, "%i", (int)e->expression.count);
299 if (pos + 1 >= bufsize)
305 typestr = type_name[e->expression.vtype];
306 typelen = strlen(typestr);
307 if (pos + typelen >= bufsize)
309 strcpy(buf + pos, typestr);
310 return pos + typelen;
314 buf[bufsize-3] = '.';
315 buf[bufsize-2] = '.';
316 buf[bufsize-1] = '.';
320 void ast_type_to_string(ast_expression *e, char *buf, size_t bufsize)
322 size_t pos = ast_type_to_string_impl(e, buf, bufsize-1, 0);
326 ast_value* ast_value_new(lex_ctx ctx, const char *name, int t)
328 ast_instantiate(ast_value, ctx, ast_value_delete);
329 ast_expression_init((ast_expression*)self,
330 (ast_expression_codegen*)&ast_value_codegen);
331 self->expression.node.keep = true; /* keep */
333 self->name = name ? util_strdup(name) : NULL;
334 self->expression.vtype = t;
335 self->expression.next = NULL;
336 self->isfield = false;
338 self->hasvalue = false;
340 memset(&self->constval, 0, sizeof(self->constval));
343 self->ir_values = NULL;
344 self->ir_value_count = 0;
353 void ast_value_delete(ast_value* self)
356 mem_d((void*)self->name);
357 if (self->hasvalue) {
358 switch (self->expression.vtype)
361 mem_d((void*)self->constval.vstring);
364 /* unlink us from the function node */
365 self->constval.vfunc->vtype = NULL;
367 /* NOTE: delete function? currently collected in
368 * the parser structure
375 mem_d(self->ir_values);
380 ast_expression_delete((ast_expression*)self);
384 void ast_value_params_add(ast_value *self, ast_value *p)
386 vec_push(self->expression.params, p);
389 bool ast_value_set_name(ast_value *self, const char *name)
392 mem_d((void*)self->name);
393 self->name = util_strdup(name);
397 ast_binary* ast_binary_new(lex_ctx ctx, int op,
398 ast_expression* left, ast_expression* right)
400 ast_instantiate(ast_binary, ctx, ast_binary_delete);
401 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binary_codegen);
407 ast_propagate_effects(self, left);
408 ast_propagate_effects(self, right);
410 if (op >= INSTR_EQ_F && op <= INSTR_GT)
411 self->expression.vtype = TYPE_FLOAT;
412 else if (op == INSTR_AND || op == INSTR_OR) {
413 if (OPTS_FLAG(PERL_LOGIC))
414 ast_type_adopt(self, right);
416 self->expression.vtype = TYPE_FLOAT;
418 else if (op == INSTR_BITAND || op == INSTR_BITOR)
419 self->expression.vtype = TYPE_FLOAT;
420 else if (op == INSTR_MUL_VF || op == INSTR_MUL_FV)
421 self->expression.vtype = TYPE_VECTOR;
422 else if (op == INSTR_MUL_V)
423 self->expression.vtype = TYPE_FLOAT;
425 self->expression.vtype = left->expression.vtype;
430 void ast_binary_delete(ast_binary *self)
432 ast_unref(self->left);
433 ast_unref(self->right);
434 ast_expression_delete((ast_expression*)self);
438 ast_binstore* ast_binstore_new(lex_ctx ctx, int storop, int op,
439 ast_expression* left, ast_expression* right)
441 ast_instantiate(ast_binstore, ctx, ast_binstore_delete);
442 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binstore_codegen);
444 ast_side_effects(self) = true;
446 self->opstore = storop;
449 self->source = right;
451 self->keep_dest = false;
453 if (!ast_type_adopt(self, left)) {
461 void ast_binstore_delete(ast_binstore *self)
463 if (!self->keep_dest)
464 ast_unref(self->dest);
465 ast_unref(self->source);
466 ast_expression_delete((ast_expression*)self);
470 ast_unary* ast_unary_new(lex_ctx ctx, int op,
471 ast_expression *expr)
473 ast_instantiate(ast_unary, ctx, ast_unary_delete);
474 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_unary_codegen);
477 self->operand = expr;
479 ast_propagate_effects(self, expr);
481 if (op >= INSTR_NOT_F && op <= INSTR_NOT_FNC) {
482 self->expression.vtype = TYPE_FLOAT;
484 compile_error(ctx, "cannot determine type of unary operation %s", asm_instr[op].m);
489 void ast_unary_delete(ast_unary *self)
491 if (self->operand) ast_unref(self->operand);
492 ast_expression_delete((ast_expression*)self);
496 ast_return* ast_return_new(lex_ctx ctx, ast_expression *expr)
498 ast_instantiate(ast_return, ctx, ast_return_delete);
499 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_return_codegen);
501 self->operand = expr;
504 ast_propagate_effects(self, expr);
509 void ast_return_delete(ast_return *self)
512 ast_unref(self->operand);
513 ast_expression_delete((ast_expression*)self);
517 ast_entfield* ast_entfield_new(lex_ctx ctx, ast_expression *entity, ast_expression *field)
519 if (field->expression.vtype != TYPE_FIELD) {
520 compile_error(ctx, "ast_entfield_new with expression not of type field");
523 return ast_entfield_new_force(ctx, entity, field, field->expression.next);
526 ast_entfield* ast_entfield_new_force(lex_ctx ctx, ast_expression *entity, ast_expression *field, const ast_expression *outtype)
528 ast_instantiate(ast_entfield, ctx, ast_entfield_delete);
532 /* Error: field has no type... */
536 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_entfield_codegen);
538 self->entity = entity;
540 ast_propagate_effects(self, entity);
541 ast_propagate_effects(self, field);
543 if (!ast_type_adopt(self, outtype)) {
544 ast_entfield_delete(self);
551 void ast_entfield_delete(ast_entfield *self)
553 ast_unref(self->entity);
554 ast_unref(self->field);
555 ast_expression_delete((ast_expression*)self);
559 ast_member* ast_member_new(lex_ctx ctx, ast_expression *owner, unsigned int field, const char *name)
561 ast_instantiate(ast_member, ctx, ast_member_delete);
567 if (owner->expression.vtype != TYPE_VECTOR &&
568 owner->expression.vtype != TYPE_FIELD) {
569 compile_error(ctx, "member-access on an invalid owner of type %s", type_name[owner->expression.vtype]);
574 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_member_codegen);
575 self->expression.node.keep = true; /* keep */
577 if (owner->expression.vtype == TYPE_VECTOR) {
578 self->expression.vtype = TYPE_FLOAT;
579 self->expression.next = NULL;
581 self->expression.vtype = TYPE_FIELD;
582 self->expression.next = ast_shallow_type(ctx, TYPE_FLOAT);
585 self->rvalue = false;
587 ast_propagate_effects(self, owner);
591 self->name = util_strdup(name);
598 void ast_member_delete(ast_member *self)
600 /* The owner is always an ast_value, which has .keep=true,
601 * also: ast_members are usually deleted after the owner, thus
602 * this will cause invalid access
603 ast_unref(self->owner);
604 * once we allow (expression).x to access a vector-member, we need
605 * to change this: preferably by creating an alternate ast node for this
606 * purpose that is not garbage-collected.
608 ast_expression_delete((ast_expression*)self);
612 bool ast_member_set_name(ast_member *self, const char *name)
615 mem_d((void*)self->name);
616 self->name = util_strdup(name);
620 ast_array_index* ast_array_index_new(lex_ctx ctx, ast_expression *array, ast_expression *index)
622 ast_expression *outtype;
623 ast_instantiate(ast_array_index, ctx, ast_array_index_delete);
625 outtype = array->expression.next;
628 /* Error: field has no type... */
632 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_array_index_codegen);
636 ast_propagate_effects(self, array);
637 ast_propagate_effects(self, index);
639 if (!ast_type_adopt(self, outtype)) {
640 ast_array_index_delete(self);
643 if (array->expression.vtype == TYPE_FIELD && outtype->expression.vtype == TYPE_ARRAY) {
644 if (self->expression.vtype != TYPE_ARRAY) {
645 compile_error(ast_ctx(self), "array_index node on type");
646 ast_array_index_delete(self);
649 self->array = outtype;
650 self->expression.vtype = TYPE_FIELD;
656 void ast_array_index_delete(ast_array_index *self)
658 ast_unref(self->array);
659 ast_unref(self->index);
660 ast_expression_delete((ast_expression*)self);
664 ast_ifthen* ast_ifthen_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
666 ast_instantiate(ast_ifthen, ctx, ast_ifthen_delete);
667 if (!ontrue && !onfalse) {
668 /* because it is invalid */
672 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ifthen_codegen);
675 self->on_true = ontrue;
676 self->on_false = onfalse;
677 ast_propagate_effects(self, cond);
679 ast_propagate_effects(self, ontrue);
681 ast_propagate_effects(self, onfalse);
686 void ast_ifthen_delete(ast_ifthen *self)
688 ast_unref(self->cond);
690 ast_unref(self->on_true);
692 ast_unref(self->on_false);
693 ast_expression_delete((ast_expression*)self);
697 ast_ternary* ast_ternary_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
699 ast_expression *exprtype = ontrue;
700 ast_instantiate(ast_ternary, ctx, ast_ternary_delete);
701 /* This time NEITHER must be NULL */
702 if (!ontrue || !onfalse) {
706 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ternary_codegen);
709 self->on_true = ontrue;
710 self->on_false = onfalse;
711 ast_propagate_effects(self, cond);
712 ast_propagate_effects(self, ontrue);
713 ast_propagate_effects(self, onfalse);
715 if (ontrue->expression.vtype == TYPE_NIL)
717 if (!ast_type_adopt(self, exprtype)) {
718 ast_ternary_delete(self);
725 void ast_ternary_delete(ast_ternary *self)
727 /* the if()s are only there because computed-gotos can set them
730 if (self->cond) ast_unref(self->cond);
731 if (self->on_true) ast_unref(self->on_true);
732 if (self->on_false) ast_unref(self->on_false);
733 ast_expression_delete((ast_expression*)self);
737 ast_loop* ast_loop_new(lex_ctx ctx,
738 ast_expression *initexpr,
739 ast_expression *precond, bool pre_not,
740 ast_expression *postcond, bool post_not,
741 ast_expression *increment,
742 ast_expression *body)
744 ast_instantiate(ast_loop, ctx, ast_loop_delete);
745 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_loop_codegen);
747 self->initexpr = initexpr;
748 self->precond = precond;
749 self->postcond = postcond;
750 self->increment = increment;
753 self->pre_not = pre_not;
754 self->post_not = post_not;
757 ast_propagate_effects(self, initexpr);
759 ast_propagate_effects(self, precond);
761 ast_propagate_effects(self, postcond);
763 ast_propagate_effects(self, increment);
765 ast_propagate_effects(self, body);
770 void ast_loop_delete(ast_loop *self)
773 ast_unref(self->initexpr);
775 ast_unref(self->precond);
777 ast_unref(self->postcond);
779 ast_unref(self->increment);
781 ast_unref(self->body);
782 ast_expression_delete((ast_expression*)self);
786 ast_breakcont* ast_breakcont_new(lex_ctx ctx, bool iscont, unsigned int levels)
788 ast_instantiate(ast_breakcont, ctx, ast_breakcont_delete);
789 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_breakcont_codegen);
791 self->is_continue = iscont;
792 self->levels = levels;
797 void ast_breakcont_delete(ast_breakcont *self)
799 ast_expression_delete((ast_expression*)self);
803 ast_switch* ast_switch_new(lex_ctx ctx, ast_expression *op)
805 ast_instantiate(ast_switch, ctx, ast_switch_delete);
806 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_switch_codegen);
811 ast_propagate_effects(self, op);
816 void ast_switch_delete(ast_switch *self)
819 ast_unref(self->operand);
821 for (i = 0; i < vec_size(self->cases); ++i) {
822 if (self->cases[i].value)
823 ast_unref(self->cases[i].value);
824 ast_unref(self->cases[i].code);
826 vec_free(self->cases);
828 ast_expression_delete((ast_expression*)self);
832 ast_label* ast_label_new(lex_ctx ctx, const char *name, bool undefined)
834 ast_instantiate(ast_label, ctx, ast_label_delete);
835 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_label_codegen);
837 self->expression.vtype = TYPE_NOEXPR;
839 self->name = util_strdup(name);
840 self->irblock = NULL;
842 self->undefined = undefined;
847 void ast_label_delete(ast_label *self)
849 mem_d((void*)self->name);
850 vec_free(self->gotos);
851 ast_expression_delete((ast_expression*)self);
855 void ast_label_register_goto(ast_label *self, ast_goto *g)
857 vec_push(self->gotos, g);
860 ast_goto* ast_goto_new(lex_ctx ctx, const char *name)
862 ast_instantiate(ast_goto, ctx, ast_goto_delete);
863 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_goto_codegen);
865 self->name = util_strdup(name);
867 self->irblock_from = NULL;
872 void ast_goto_delete(ast_goto *self)
874 mem_d((void*)self->name);
875 ast_expression_delete((ast_expression*)self);
879 void ast_goto_set_label(ast_goto *self, ast_label *label)
881 self->target = label;
884 ast_call* ast_call_new(lex_ctx ctx,
885 ast_expression *funcexpr)
887 ast_instantiate(ast_call, ctx, ast_call_delete);
888 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_call_codegen);
890 ast_side_effects(self) = true;
893 self->func = funcexpr;
895 ast_type_adopt(self, funcexpr->expression.next);
900 void ast_call_delete(ast_call *self)
903 for (i = 0; i < vec_size(self->params); ++i)
904 ast_unref(self->params[i]);
905 vec_free(self->params);
908 ast_unref(self->func);
910 ast_expression_delete((ast_expression*)self);
914 bool ast_call_check_types(ast_call *self)
918 const ast_expression *func = self->func;
919 size_t count = vec_size(self->params);
920 if (count > vec_size(func->expression.params))
921 count = vec_size(func->expression.params);
923 for (i = 0; i < count; ++i) {
924 if (!ast_compare_type(self->params[i], (ast_expression*)(func->expression.params[i])))
928 ast_type_to_string(self->params[i], tgot, sizeof(tgot));
929 ast_type_to_string((ast_expression*)func->expression.params[i], texp, sizeof(texp));
930 compile_error(ast_ctx(self), "invalid type for parameter %u in function call: expected %s, got %s",
931 (unsigned int)(i+1), texp, tgot);
932 /* we don't immediately return */
939 ast_store* ast_store_new(lex_ctx ctx, int op,
940 ast_expression *dest, ast_expression *source)
942 ast_instantiate(ast_store, ctx, ast_store_delete);
943 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_store_codegen);
945 ast_side_effects(self) = true;
949 self->source = source;
951 if (!ast_type_adopt(self, dest)) {
959 void ast_store_delete(ast_store *self)
961 ast_unref(self->dest);
962 ast_unref(self->source);
963 ast_expression_delete((ast_expression*)self);
967 ast_block* ast_block_new(lex_ctx ctx)
969 ast_instantiate(ast_block, ctx, ast_block_delete);
970 ast_expression_init((ast_expression*)self,
971 (ast_expression_codegen*)&ast_block_codegen);
975 self->collect = NULL;
980 bool ast_block_add_expr(ast_block *self, ast_expression *e)
982 ast_propagate_effects(self, e);
983 vec_push(self->exprs, e);
984 if (self->expression.next) {
985 ast_delete(self->expression.next);
986 self->expression.next = NULL;
988 if (!ast_type_adopt(self, e)) {
989 compile_error(ast_ctx(self), "internal error: failed to adopt type");
995 void ast_block_collect(ast_block *self, ast_expression *expr)
997 vec_push(self->collect, expr);
998 expr->expression.node.keep = true;
1001 void ast_block_delete(ast_block *self)
1004 for (i = 0; i < vec_size(self->exprs); ++i)
1005 ast_unref(self->exprs[i]);
1006 vec_free(self->exprs);
1007 for (i = 0; i < vec_size(self->locals); ++i)
1008 ast_delete(self->locals[i]);
1009 vec_free(self->locals);
1010 for (i = 0; i < vec_size(self->collect); ++i)
1011 ast_delete(self->collect[i]);
1012 vec_free(self->collect);
1013 ast_expression_delete((ast_expression*)self);
1017 bool ast_block_set_type(ast_block *self, ast_expression *from)
1019 if (self->expression.next)
1020 ast_delete(self->expression.next);
1021 if (!ast_type_adopt(self, from))
1026 ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype)
1028 ast_instantiate(ast_function, ctx, ast_function_delete);
1032 vtype->expression.vtype != TYPE_FUNCTION)
1034 compile_error(ast_ctx(self), "internal error: ast_function_new condition %i %i type=%i (probably 2 bodies?)",
1036 (int)vtype->hasvalue,
1037 vtype->expression.vtype);
1042 self->vtype = vtype;
1043 self->name = name ? util_strdup(name) : NULL;
1044 self->blocks = NULL;
1046 self->labelcount = 0;
1049 self->ir_func = NULL;
1050 self->curblock = NULL;
1052 self->breakblocks = NULL;
1053 self->continueblocks = NULL;
1055 vtype->hasvalue = true;
1056 vtype->constval.vfunc = self;
1061 void ast_function_delete(ast_function *self)
1065 mem_d((void*)self->name);
1067 /* ast_value_delete(self->vtype); */
1068 self->vtype->hasvalue = false;
1069 self->vtype->constval.vfunc = NULL;
1070 /* We use unref - if it was stored in a global table it is supposed
1071 * to be deleted from *there*
1073 ast_unref(self->vtype);
1075 for (i = 0; i < vec_size(self->blocks); ++i)
1076 ast_delete(self->blocks[i]);
1077 vec_free(self->blocks);
1078 vec_free(self->breakblocks);
1079 vec_free(self->continueblocks);
1083 const char* ast_function_label(ast_function *self, const char *prefix)
1089 if (!opts.dump && !opts.dumpfin && !opts.debug)
1092 id = (self->labelcount++);
1093 len = strlen(prefix);
1095 from = self->labelbuf + sizeof(self->labelbuf)-1;
1098 *from-- = (id%10) + '0';
1102 memcpy(from - len, prefix, len);
1106 /*********************************************************************/
1108 * by convention you must never pass NULL to the 'ir_value **out'
1109 * parameter. If you really don't care about the output, pass a dummy.
1110 * But I can't imagine a pituation where the output is truly unnecessary.
1113 void _ast_codegen_output_type(ast_expression_common *self, ir_value *out)
1115 if (out->vtype == TYPE_FIELD)
1116 out->fieldtype = self->next->expression.vtype;
1117 if (out->vtype == TYPE_FUNCTION)
1118 out->outtype = self->next->expression.vtype;
1121 #define codegen_output_type(a,o) (_ast_codegen_output_type(&((a)->expression),(o)))
1123 bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out)
1127 if (self->expression.vtype == TYPE_NIL) {
1128 *out = func->ir_func->owner->nil;
1131 /* NOTE: This is the codegen for a variable used in an expression.
1132 * It is not the codegen to generate the value. For this purpose,
1133 * ast_local_codegen and ast_global_codegen are to be used before this
1134 * is executed. ast_function_codegen should take care of its locals,
1135 * and the ast-user should take care of ast_global_codegen to be used
1136 * on all the globals.
1139 char tname[1024]; /* typename is reserved in C++ */
1140 ast_type_to_string((ast_expression*)self, tname, sizeof(tname));
1141 compile_error(ast_ctx(self), "ast_value used before generated %s %s", tname, self->name);
1148 bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
1152 if (self->expression.vtype == TYPE_NIL) {
1153 compile_error(ast_ctx(self), "internal error: trying to generate a variable of TYPE_NIL");
1157 if (self->hasvalue && self->expression.vtype == TYPE_FUNCTION)
1159 ir_function *func = ir_builder_create_function(ir, self->name, self->expression.next->expression.vtype);
1162 func->context = ast_ctx(self);
1163 func->value->context = ast_ctx(self);
1165 self->constval.vfunc->ir_func = func;
1166 self->ir_v = func->value;
1167 /* The function is filled later on ast_function_codegen... */
1171 if (isfield && self->expression.vtype == TYPE_FIELD) {
1172 ast_expression *fieldtype = self->expression.next;
1174 if (self->hasvalue) {
1175 compile_error(ast_ctx(self), "TODO: constant field pointers with value");
1179 if (fieldtype->expression.vtype == TYPE_ARRAY) {
1184 ast_expression_common *elemtype;
1186 ast_value *array = (ast_value*)fieldtype;
1188 if (!ast_istype(fieldtype, ast_value)) {
1189 compile_error(ast_ctx(self), "internal error: ast_value required");
1193 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1194 if (!array->expression.count || array->expression.count > opts.max_array_size)
1195 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)array->expression.count);
1197 elemtype = &array->expression.next->expression;
1198 vtype = elemtype->vtype;
1200 v = ir_builder_create_field(ir, self->name, vtype);
1202 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", self->name);
1205 v->context = ast_ctx(self);
1206 v->unique_life = true;
1208 array->ir_v = self->ir_v = v;
1210 namelen = strlen(self->name);
1211 name = (char*)mem_a(namelen + 16);
1212 strcpy(name, self->name);
1214 array->ir_values = (ir_value**)mem_a(sizeof(array->ir_values[0]) * array->expression.count);
1215 array->ir_values[0] = v;
1216 for (ai = 1; ai < array->expression.count; ++ai) {
1217 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1218 array->ir_values[ai] = ir_builder_create_field(ir, name, vtype);
1219 if (!array->ir_values[ai]) {
1221 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", name);
1224 array->ir_values[ai]->context = ast_ctx(self);
1225 array->ir_values[ai]->unique_life = true;
1226 array->ir_values[ai]->locked = true;
1232 v = ir_builder_create_field(ir, self->name, self->expression.next->expression.vtype);
1235 v->context = ast_ctx(self);
1241 if (self->expression.vtype == TYPE_ARRAY) {
1246 ast_expression_common *elemtype = &self->expression.next->expression;
1247 int vtype = elemtype->vtype;
1249 /* same as with field arrays */
1250 if (!self->expression.count || self->expression.count > opts.max_array_size)
1251 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1253 v = ir_builder_create_global(ir, self->name, vtype);
1255 compile_error(ast_ctx(self), "ir_builder_create_global failed `%s`", self->name);
1258 v->context = ast_ctx(self);
1259 v->unique_life = true;
1262 namelen = strlen(self->name);
1263 name = (char*)mem_a(namelen + 16);
1264 strcpy(name, self->name);
1266 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1267 self->ir_values[0] = v;
1268 for (ai = 1; ai < self->expression.count; ++ai) {
1269 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1270 self->ir_values[ai] = ir_builder_create_global(ir, name, vtype);
1271 if (!self->ir_values[ai]) {
1273 compile_error(ast_ctx(self), "ir_builder_create_global failed `%s`", name);
1276 self->ir_values[ai]->context = ast_ctx(self);
1277 self->ir_values[ai]->unique_life = true;
1278 self->ir_values[ai]->locked = true;
1284 /* Arrays don't do this since there's no "array" value which spans across the
1287 v = ir_builder_create_global(ir, self->name, self->expression.vtype);
1289 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", self->name);
1292 codegen_output_type(self, v);
1293 v->context = ast_ctx(self);
1296 if (self->hasvalue) {
1297 switch (self->expression.vtype)
1300 if (!ir_value_set_float(v, self->constval.vfloat))
1304 if (!ir_value_set_vector(v, self->constval.vvec))
1308 if (!ir_value_set_string(v, self->constval.vstring))
1312 compile_error(ast_ctx(self), "TODO: global constant array");
1315 compile_error(ast_ctx(self), "global of type function not properly generated");
1317 /* Cannot generate an IR value for a function,
1318 * need a pointer pointing to a function rather.
1321 if (!self->constval.vfield) {
1322 compile_error(ast_ctx(self), "field constant without vfield set");
1325 if (!self->constval.vfield->ir_v) {
1326 compile_error(ast_ctx(self), "field constant generated before its field");
1329 if (!ir_value_set_field(v, self->constval.vfield->ir_v))
1333 compile_error(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1338 /* link us to the ir_value */
1343 error: /* clean up */
1348 bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
1352 if (self->expression.vtype == TYPE_NIL) {
1353 compile_error(ast_ctx(self), "internal error: trying to generate a variable of TYPE_NIL");
1357 if (self->hasvalue && self->expression.vtype == TYPE_FUNCTION)
1359 /* Do we allow local functions? I think not...
1360 * this is NOT a function pointer atm.
1365 if (self->expression.vtype == TYPE_ARRAY) {
1370 ast_expression_common *elemtype = &self->expression.next->expression;
1371 int vtype = elemtype->vtype;
1373 func->flags |= IR_FLAG_HAS_ARRAYS;
1376 compile_error(ast_ctx(self), "array-parameters are not supported");
1380 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1381 if (!self->expression.count || self->expression.count > opts.max_array_size) {
1382 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1385 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1386 if (!self->ir_values) {
1387 compile_error(ast_ctx(self), "failed to allocate array values");
1391 v = ir_function_create_local(func, self->name, vtype, param);
1393 compile_error(ast_ctx(self), "ir_function_create_local failed");
1396 v->context = ast_ctx(self);
1397 v->unique_life = true;
1400 namelen = strlen(self->name);
1401 name = (char*)mem_a(namelen + 16);
1402 strcpy(name, self->name);
1404 self->ir_values[0] = v;
1405 for (ai = 1; ai < self->expression.count; ++ai) {
1406 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1407 self->ir_values[ai] = ir_function_create_local(func, name, vtype, param);
1408 if (!self->ir_values[ai]) {
1409 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", name);
1412 self->ir_values[ai]->context = ast_ctx(self);
1413 self->ir_values[ai]->unique_life = true;
1414 self->ir_values[ai]->locked = true;
1419 v = ir_function_create_local(func, self->name, self->expression.vtype, param);
1422 codegen_output_type(self, v);
1423 v->context = ast_ctx(self);
1426 /* A constant local... hmmm...
1427 * I suppose the IR will have to deal with this
1429 if (self->hasvalue) {
1430 switch (self->expression.vtype)
1433 if (!ir_value_set_float(v, self->constval.vfloat))
1437 if (!ir_value_set_vector(v, self->constval.vvec))
1441 if (!ir_value_set_string(v, self->constval.vstring))
1445 compile_error(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1450 /* link us to the ir_value */
1454 if (!ast_generate_accessors(self, func->owner))
1458 error: /* clean up */
1463 bool ast_generate_accessors(ast_value *self, ir_builder *ir)
1466 bool warn = OPTS_WARN(WARN_USED_UNINITIALIZED);
1467 if (!self->setter || !self->getter)
1469 for (i = 0; i < self->expression.count; ++i) {
1470 if (!self->ir_values) {
1471 compile_error(ast_ctx(self), "internal error: no array values generated for `%s`", self->name);
1474 if (!self->ir_values[i]) {
1475 compile_error(ast_ctx(self), "internal error: not all array values have been generated for `%s`", self->name);
1478 if (self->ir_values[i]->life) {
1479 compile_error(ast_ctx(self), "internal error: function containing `%s` already generated", self->name);
1484 opts_set(opts.warn, WARN_USED_UNINITIALIZED, false);
1486 if (!ast_global_codegen (self->setter, ir, false) ||
1487 !ast_function_codegen(self->setter->constval.vfunc, ir) ||
1488 !ir_function_finalize(self->setter->constval.vfunc->ir_func))
1490 compile_error(ast_ctx(self), "internal error: failed to generate setter for `%s`", self->name);
1491 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1496 if (!ast_global_codegen (self->getter, ir, false) ||
1497 !ast_function_codegen(self->getter->constval.vfunc, ir) ||
1498 !ir_function_finalize(self->getter->constval.vfunc->ir_func))
1500 compile_error(ast_ctx(self), "internal error: failed to generate getter for `%s`", self->name);
1501 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1505 for (i = 0; i < self->expression.count; ++i) {
1506 vec_free(self->ir_values[i]->life);
1508 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1512 bool ast_function_codegen(ast_function *self, ir_builder *ir)
1516 ast_expression_common *ec;
1521 irf = self->ir_func;
1523 compile_error(ast_ctx(self), "ast_function's related ast_value was not generated yet");
1527 /* fill the parameter list */
1528 ec = &self->vtype->expression;
1529 for (i = 0; i < vec_size(ec->params); ++i)
1531 if (ec->params[i]->expression.vtype == TYPE_FIELD)
1532 vec_push(irf->params, ec->params[i]->expression.next->expression.vtype);
1534 vec_push(irf->params, ec->params[i]->expression.vtype);
1535 if (!self->builtin) {
1536 if (!ast_local_codegen(ec->params[i], self->ir_func, true))
1541 if (self->builtin) {
1542 irf->builtin = self->builtin;
1546 if (!vec_size(self->blocks)) {
1547 compile_error(ast_ctx(self), "function `%s` has no body", self->name);
1551 self->curblock = ir_function_create_block(ast_ctx(self), irf, "entry");
1552 if (!self->curblock) {
1553 compile_error(ast_ctx(self), "failed to allocate entry block for `%s`", self->name);
1557 for (i = 0; i < vec_size(self->blocks); ++i) {
1558 ast_expression_codegen *gen = self->blocks[i]->expression.codegen;
1559 if (!(*gen)((ast_expression*)self->blocks[i], self, false, &dummy))
1563 /* TODO: check return types */
1564 if (!self->curblock->final)
1566 if (!self->vtype->expression.next ||
1567 self->vtype->expression.next->expression.vtype == TYPE_VOID)
1569 return ir_block_create_return(self->curblock, ast_ctx(self), NULL);
1571 else if (vec_size(self->curblock->entries))
1573 /* error("missing return"); */
1574 if (compile_warning(ast_ctx(self), WARN_MISSING_RETURN_VALUES,
1575 "control reaches end of non-void function (`%s`) via %s",
1576 self->name, self->curblock->label))
1580 return ir_block_create_return(self->curblock, ast_ctx(self), NULL);
1586 /* Note, you will not see ast_block_codegen generate ir_blocks.
1587 * To the AST and the IR, blocks are 2 different things.
1588 * In the AST it represents a block of code, usually enclosed in
1589 * curly braces {...}.
1590 * While in the IR it represents a block in terms of control-flow.
1592 bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_value **out)
1596 /* We don't use this
1597 * Note: an ast-representation using the comma-operator
1598 * of the form: (a, b, c) = x should not assign to c...
1601 compile_error(ast_ctx(self), "not an l-value (code-block)");
1605 if (self->expression.outr) {
1606 *out = self->expression.outr;
1610 /* output is NULL at first, we'll have each expression
1611 * assign to out output, thus, a comma-operator represention
1612 * using an ast_block will return the last generated value,
1613 * so: (b, c) + a executed both b and c, and returns c,
1614 * which is then added to a.
1618 /* generate locals */
1619 for (i = 0; i < vec_size(self->locals); ++i)
1621 if (!ast_local_codegen(self->locals[i], func->ir_func, false)) {
1623 compile_error(ast_ctx(self), "failed to generate local `%s`", self->locals[i]->name);
1628 for (i = 0; i < vec_size(self->exprs); ++i)
1630 ast_expression_codegen *gen;
1631 if (func->curblock->final && !ast_istype(self->exprs[i], ast_label)) {
1632 if (compile_warning(ast_ctx(self->exprs[i]), WARN_UNREACHABLE_CODE, "unreachable statement"))
1636 gen = self->exprs[i]->expression.codegen;
1637 if (!(*gen)(self->exprs[i], func, false, out))
1641 self->expression.outr = *out;
1646 bool ast_store_codegen(ast_store *self, ast_function *func, bool lvalue, ir_value **out)
1648 ast_expression_codegen *cgen;
1649 ir_value *left = NULL;
1650 ir_value *right = NULL;
1654 ast_array_index *ai = NULL;
1656 if (lvalue && self->expression.outl) {
1657 *out = self->expression.outl;
1661 if (!lvalue && self->expression.outr) {
1662 *out = self->expression.outr;
1666 if (ast_istype(self->dest, ast_array_index))
1669 ai = (ast_array_index*)self->dest;
1670 idx = (ast_value*)ai->index;
1672 if (ast_istype(ai->index, ast_value) && idx->hasvalue && idx->cvq == CV_CONST)
1677 /* we need to call the setter */
1678 ir_value *iridx, *funval;
1682 compile_error(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1686 arr = (ast_value*)ai->array;
1687 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1688 compile_error(ast_ctx(self), "value has no setter (%s)", arr->name);
1692 cgen = idx->expression.codegen;
1693 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1696 cgen = arr->setter->expression.codegen;
1697 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
1700 cgen = self->source->expression.codegen;
1701 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1704 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "store"), funval, false);
1707 ir_call_param(call, iridx);
1708 ir_call_param(call, right);
1709 self->expression.outr = right;
1715 cgen = self->dest->expression.codegen;
1717 if (!(*cgen)((ast_expression*)(self->dest), func, true, &left))
1719 self->expression.outl = left;
1721 cgen = self->source->expression.codegen;
1723 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1726 if (!ir_block_create_store_op(func->curblock, ast_ctx(self), self->op, left, right))
1728 self->expression.outr = right;
1731 /* Theoretically, an assinment returns its left side as an
1732 * lvalue, if we don't need an lvalue though, we return
1733 * the right side as an rvalue, otherwise we have to
1734 * somehow know whether or not we need to dereference the pointer
1735 * on the left side - that is: OP_LOAD if it was an address.
1736 * Also: in original QC we cannot OP_LOADP *anyway*.
1738 *out = (lvalue ? left : right);
1743 bool ast_binary_codegen(ast_binary *self, ast_function *func, bool lvalue, ir_value **out)
1745 ast_expression_codegen *cgen;
1746 ir_value *left, *right;
1748 /* A binary operation cannot yield an l-value */
1750 compile_error(ast_ctx(self), "not an l-value (binop)");
1754 if (self->expression.outr) {
1755 *out = self->expression.outr;
1759 if ((OPTS_FLAG(SHORT_LOGIC) || OPTS_FLAG(PERL_LOGIC)) &&
1760 (self->op == INSTR_AND || self->op == INSTR_OR))
1762 /* short circuit evaluation */
1763 ir_block *other, *merge;
1764 ir_block *from_left, *from_right;
1768 /* prepare end-block */
1769 merge_id = vec_size(func->ir_func->blocks);
1770 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "sce_merge"));
1772 /* generate the left expression */
1773 cgen = self->left->expression.codegen;
1774 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1776 /* remember the block */
1777 from_left = func->curblock;
1779 /* create a new block for the right expression */
1780 other = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "sce_other"));
1781 if (self->op == INSTR_AND) {
1782 /* on AND: left==true -> other */
1783 if (!ir_block_create_if(func->curblock, ast_ctx(self), left, other, merge))
1786 /* on OR: left==false -> other */
1787 if (!ir_block_create_if(func->curblock, ast_ctx(self), left, merge, other))
1790 /* use the likely flag */
1791 vec_last(func->curblock->instr)->likely = true;
1793 /* enter the right-expression's block */
1794 func->curblock = other;
1796 cgen = self->right->expression.codegen;
1797 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1799 /* remember block */
1800 from_right = func->curblock;
1802 /* jump to the merge block */
1803 if (!ir_block_create_jump(func->curblock, ast_ctx(self), merge))
1806 vec_remove(func->ir_func->blocks, merge_id, 1);
1807 vec_push(func->ir_func->blocks, merge);
1809 func->curblock = merge;
1810 phi = ir_block_create_phi(func->curblock, ast_ctx(self),
1811 ast_function_label(func, "sce_value"),
1812 self->expression.vtype);
1813 ir_phi_add(phi, from_left, left);
1814 ir_phi_add(phi, from_right, right);
1815 *out = ir_phi_value(phi);
1819 if (!OPTS_FLAG(PERL_LOGIC)) {
1821 if (OPTS_FLAG(CORRECT_LOGIC) && (*out)->vtype == TYPE_VECTOR) {
1822 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
1823 ast_function_label(func, "sce_bool_v"),
1827 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
1828 ast_function_label(func, "sce_bool"),
1833 else if (OPTS_FLAG(FALSE_EMPTY_STRINGS) && (*out)->vtype == TYPE_STRING) {
1834 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
1835 ast_function_label(func, "sce_bool_s"),
1839 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
1840 ast_function_label(func, "sce_bool"),
1846 *out = ir_block_create_binop(func->curblock, ast_ctx(self),
1847 ast_function_label(func, "sce_bool"),
1848 INSTR_AND, *out, *out);
1854 self->expression.outr = *out;
1855 codegen_output_type(self, *out);
1859 cgen = self->left->expression.codegen;
1860 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1863 cgen = self->right->expression.codegen;
1864 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1867 *out = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "bin"),
1868 self->op, left, right);
1871 self->expression.outr = *out;
1872 codegen_output_type(self, *out);
1877 bool ast_binstore_codegen(ast_binstore *self, ast_function *func, bool lvalue, ir_value **out)
1879 ast_expression_codegen *cgen;
1880 ir_value *leftl = NULL, *leftr, *right, *bin;
1884 ast_array_index *ai = NULL;
1885 ir_value *iridx = NULL;
1887 if (lvalue && self->expression.outl) {
1888 *out = self->expression.outl;
1892 if (!lvalue && self->expression.outr) {
1893 *out = self->expression.outr;
1897 if (ast_istype(self->dest, ast_array_index))
1900 ai = (ast_array_index*)self->dest;
1901 idx = (ast_value*)ai->index;
1903 if (ast_istype(ai->index, ast_value) && idx->hasvalue && idx->cvq == CV_CONST)
1907 /* for a binstore we need both an lvalue and an rvalue for the left side */
1908 /* rvalue of destination! */
1910 cgen = idx->expression.codegen;
1911 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1914 cgen = self->dest->expression.codegen;
1915 if (!(*cgen)((ast_expression*)(self->dest), func, false, &leftr))
1918 /* source as rvalue only */
1919 cgen = self->source->expression.codegen;
1920 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1923 /* now the binary */
1924 bin = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "binst"),
1925 self->opbin, leftr, right);
1926 self->expression.outr = bin;
1930 /* we need to call the setter */
1935 compile_error(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1939 arr = (ast_value*)ai->array;
1940 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1941 compile_error(ast_ctx(self), "value has no setter (%s)", arr->name);
1945 cgen = arr->setter->expression.codegen;
1946 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
1949 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "store"), funval, false);
1952 ir_call_param(call, iridx);
1953 ir_call_param(call, bin);
1954 self->expression.outr = bin;
1956 /* now store them */
1957 cgen = self->dest->expression.codegen;
1958 /* lvalue of destination */
1959 if (!(*cgen)((ast_expression*)(self->dest), func, true, &leftl))
1961 self->expression.outl = leftl;
1963 if (!ir_block_create_store_op(func->curblock, ast_ctx(self), self->opstore, leftl, bin))
1965 self->expression.outr = bin;
1968 /* Theoretically, an assinment returns its left side as an
1969 * lvalue, if we don't need an lvalue though, we return
1970 * the right side as an rvalue, otherwise we have to
1971 * somehow know whether or not we need to dereference the pointer
1972 * on the left side - that is: OP_LOAD if it was an address.
1973 * Also: in original QC we cannot OP_LOADP *anyway*.
1975 *out = (lvalue ? leftl : bin);
1980 bool ast_unary_codegen(ast_unary *self, ast_function *func, bool lvalue, ir_value **out)
1982 ast_expression_codegen *cgen;
1985 /* An unary operation cannot yield an l-value */
1987 compile_error(ast_ctx(self), "not an l-value (binop)");
1991 if (self->expression.outr) {
1992 *out = self->expression.outr;
1996 cgen = self->operand->expression.codegen;
1998 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
2001 *out = ir_block_create_unary(func->curblock, ast_ctx(self), ast_function_label(func, "unary"),
2005 self->expression.outr = *out;
2010 bool ast_return_codegen(ast_return *self, ast_function *func, bool lvalue, ir_value **out)
2012 ast_expression_codegen *cgen;
2017 /* In the context of a return operation, we don't actually return
2021 compile_error(ast_ctx(self), "return-expression is not an l-value");
2025 if (self->expression.outr) {
2026 compile_error(ast_ctx(self), "internal error: ast_return cannot be reused, it bears no result!");
2029 self->expression.outr = (ir_value*)1;
2031 if (self->operand) {
2032 cgen = self->operand->expression.codegen;
2034 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
2037 if (!ir_block_create_return(func->curblock, ast_ctx(self), operand))
2040 if (!ir_block_create_return(func->curblock, ast_ctx(self), NULL))
2047 bool ast_entfield_codegen(ast_entfield *self, ast_function *func, bool lvalue, ir_value **out)
2049 ast_expression_codegen *cgen;
2050 ir_value *ent, *field;
2052 /* This function needs to take the 'lvalue' flag into account!
2053 * As lvalue we provide a field-pointer, as rvalue we provide the
2057 if (lvalue && self->expression.outl) {
2058 *out = self->expression.outl;
2062 if (!lvalue && self->expression.outr) {
2063 *out = self->expression.outr;
2067 cgen = self->entity->expression.codegen;
2068 if (!(*cgen)((ast_expression*)(self->entity), func, false, &ent))
2071 cgen = self->field->expression.codegen;
2072 if (!(*cgen)((ast_expression*)(self->field), func, false, &field))
2077 *out = ir_block_create_fieldaddress(func->curblock, ast_ctx(self), ast_function_label(func, "efa"),
2080 *out = ir_block_create_load_from_ent(func->curblock, ast_ctx(self), ast_function_label(func, "efv"),
2081 ent, field, self->expression.vtype);
2082 /* Done AFTER error checking:
2083 codegen_output_type(self, *out);
2087 compile_error(ast_ctx(self), "failed to create %s instruction (output type %s)",
2088 (lvalue ? "ADDRESS" : "FIELD"),
2089 type_name[self->expression.vtype]);
2093 codegen_output_type(self, *out);
2096 self->expression.outl = *out;
2098 self->expression.outr = *out;
2100 /* Hm that should be it... */
2104 bool ast_member_codegen(ast_member *self, ast_function *func, bool lvalue, ir_value **out)
2106 ast_expression_codegen *cgen;
2109 /* in QC this is always an lvalue */
2110 if (lvalue && self->rvalue) {
2111 compile_error(ast_ctx(self), "not an l-value (member access)");
2114 if (self->expression.outl) {
2115 *out = self->expression.outl;
2119 cgen = self->owner->expression.codegen;
2120 if (!(*cgen)((ast_expression*)(self->owner), func, false, &vec))
2123 if (vec->vtype != TYPE_VECTOR &&
2124 !(vec->vtype == TYPE_FIELD && self->owner->expression.next->expression.vtype == TYPE_VECTOR))
2129 *out = ir_value_vector_member(vec, self->field);
2130 self->expression.outl = *out;
2132 return (*out != NULL);
2135 bool ast_array_index_codegen(ast_array_index *self, ast_function *func, bool lvalue, ir_value **out)
2140 if (!lvalue && self->expression.outr) {
2141 *out = self->expression.outr;
2143 if (lvalue && self->expression.outl) {
2144 *out = self->expression.outl;
2147 if (!ast_istype(self->array, ast_value)) {
2148 compile_error(ast_ctx(self), "array indexing this way is not supported");
2149 /* note this would actually be pointer indexing because the left side is
2150 * not an actual array but (hopefully) an indexable expression.
2151 * Once we get integer arithmetic, and GADDRESS/GSTORE/GLOAD instruction
2152 * support this path will be filled.
2157 arr = (ast_value*)self->array;
2158 idx = (ast_value*)self->index;
2160 if (!ast_istype(self->index, ast_value) || !idx->hasvalue || idx->cvq != CV_CONST) {
2161 /* Time to use accessor functions */
2162 ast_expression_codegen *cgen;
2163 ir_value *iridx, *funval;
2167 compile_error(ast_ctx(self), "(.2) array indexing here needs a compile-time constant");
2172 compile_error(ast_ctx(self), "value has no getter, don't know how to index it");
2176 cgen = self->index->expression.codegen;
2177 if (!(*cgen)((ast_expression*)(self->index), func, false, &iridx))
2180 cgen = arr->getter->expression.codegen;
2181 if (!(*cgen)((ast_expression*)(arr->getter), func, true, &funval))
2184 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "fetch"), funval, false);
2187 ir_call_param(call, iridx);
2189 *out = ir_call_value(call);
2190 self->expression.outr = *out;
2194 if (idx->expression.vtype == TYPE_FLOAT) {
2195 unsigned int arridx = idx->constval.vfloat;
2196 if (arridx >= self->array->expression.count)
2198 compile_error(ast_ctx(self), "array index out of bounds: %i", arridx);
2201 *out = arr->ir_values[arridx];
2203 else if (idx->expression.vtype == TYPE_INTEGER) {
2204 unsigned int arridx = idx->constval.vint;
2205 if (arridx >= self->array->expression.count)
2207 compile_error(ast_ctx(self), "array index out of bounds: %i", arridx);
2210 *out = arr->ir_values[arridx];
2213 compile_error(ast_ctx(self), "array indexing here needs an integer constant");
2219 bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_value **out)
2221 ast_expression_codegen *cgen;
2229 ir_block *ontrue_endblock = NULL;
2230 ir_block *onfalse_endblock = NULL;
2231 ir_block *merge = NULL;
2233 /* We don't output any value, thus also don't care about r/lvalue */
2237 if (self->expression.outr) {
2238 compile_error(ast_ctx(self), "internal error: ast_ifthen cannot be reused, it bears no result!");
2241 self->expression.outr = (ir_value*)1;
2243 /* generate the condition */
2244 cgen = self->cond->expression.codegen;
2245 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2247 /* update the block which will get the jump - because short-logic or ternaries may have changed this */
2248 cond = func->curblock;
2252 if (self->on_true) {
2253 /* create on-true block */
2254 ontrue = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "ontrue"));
2258 /* enter the block */
2259 func->curblock = ontrue;
2262 cgen = self->on_true->expression.codegen;
2263 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &dummy))
2266 /* we now need to work from the current endpoint */
2267 ontrue_endblock = func->curblock;
2272 if (self->on_false) {
2273 /* create on-false block */
2274 onfalse = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "onfalse"));
2278 /* enter the block */
2279 func->curblock = onfalse;
2282 cgen = self->on_false->expression.codegen;
2283 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &dummy))
2286 /* we now need to work from the current endpoint */
2287 onfalse_endblock = func->curblock;
2291 /* Merge block were they all merge in to */
2292 if (!ontrue || !onfalse || !ontrue_endblock->final || !onfalse_endblock->final)
2294 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "endif"));
2297 /* add jumps ot the merge block */
2298 if (ontrue && !ontrue_endblock->final && !ir_block_create_jump(ontrue_endblock, ast_ctx(self), merge))
2300 if (onfalse && !onfalse_endblock->final && !ir_block_create_jump(onfalse_endblock, ast_ctx(self), merge))
2303 /* Now enter the merge block */
2304 func->curblock = merge;
2307 /* we create the if here, that way all blocks are ordered :)
2309 if (!ir_block_create_if(cond, ast_ctx(self), condval,
2310 (ontrue ? ontrue : merge),
2311 (onfalse ? onfalse : merge)))
2319 bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_value **out)
2321 ast_expression_codegen *cgen;
2324 ir_value *trueval, *falseval;
2327 ir_block *cond = func->curblock;
2328 ir_block *cond_out = NULL;
2329 ir_block *ontrue, *ontrue_out = NULL;
2330 ir_block *onfalse, *onfalse_out = NULL;
2333 /* Ternary can never create an lvalue... */
2337 /* In theory it shouldn't be possible to pass through a node twice, but
2338 * in case we add any kind of optimization pass for the AST itself, it
2339 * may still happen, thus we remember a created ir_value and simply return one
2340 * if it already exists.
2342 if (self->expression.outr) {
2343 *out = self->expression.outr;
2347 /* In the following, contraty to ast_ifthen, we assume both paths exist. */
2349 /* generate the condition */
2350 func->curblock = cond;
2351 cgen = self->cond->expression.codegen;
2352 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2354 cond_out = func->curblock;
2356 /* create on-true block */
2357 ontrue = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_T"));
2362 /* enter the block */
2363 func->curblock = ontrue;
2366 cgen = self->on_true->expression.codegen;
2367 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &trueval))
2370 ontrue_out = func->curblock;
2373 /* create on-false block */
2374 onfalse = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_F"));
2379 /* enter the block */
2380 func->curblock = onfalse;
2383 cgen = self->on_false->expression.codegen;
2384 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &falseval))
2387 onfalse_out = func->curblock;
2390 /* create merge block */
2391 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_out"));
2394 /* jump to merge block */
2395 if (!ir_block_create_jump(ontrue_out, ast_ctx(self), merge))
2397 if (!ir_block_create_jump(onfalse_out, ast_ctx(self), merge))
2400 /* create if instruction */
2401 if (!ir_block_create_if(cond_out, ast_ctx(self), condval, ontrue, onfalse))
2404 /* Now enter the merge block */
2405 func->curblock = merge;
2407 /* Here, now, we need a PHI node
2408 * but first some sanity checking...
2410 if (trueval->vtype != falseval->vtype && trueval->vtype != TYPE_NIL && falseval->vtype != TYPE_NIL) {
2411 /* error("ternary with different types on the two sides"); */
2412 compile_error(ast_ctx(self), "internal error: ternary operand types invalid");
2417 phi = ir_block_create_phi(merge, ast_ctx(self), ast_function_label(func, "phi"), self->expression.vtype);
2419 compile_error(ast_ctx(self), "internal error: failed to generate phi node");
2422 ir_phi_add(phi, ontrue_out, trueval);
2423 ir_phi_add(phi, onfalse_out, falseval);
2425 self->expression.outr = ir_phi_value(phi);
2426 *out = self->expression.outr;
2428 codegen_output_type(self, *out);
2433 bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value **out)
2435 ast_expression_codegen *cgen;
2437 ir_value *dummy = NULL;
2438 ir_value *precond = NULL;
2439 ir_value *postcond = NULL;
2441 /* Since we insert some jumps "late" so we have blocks
2442 * ordered "nicely", we need to keep track of the actual end-blocks
2443 * of expressions to add the jumps to.
2445 ir_block *bbody = NULL, *end_bbody = NULL;
2446 ir_block *bprecond = NULL, *end_bprecond = NULL;
2447 ir_block *bpostcond = NULL, *end_bpostcond = NULL;
2448 ir_block *bincrement = NULL, *end_bincrement = NULL;
2449 ir_block *bout = NULL, *bin = NULL;
2451 /* let's at least move the outgoing block to the end */
2454 /* 'break' and 'continue' need to be able to find the right blocks */
2455 ir_block *bcontinue = NULL;
2456 ir_block *bbreak = NULL;
2458 ir_block *tmpblock = NULL;
2463 if (self->expression.outr) {
2464 compile_error(ast_ctx(self), "internal error: ast_loop cannot be reused, it bears no result!");
2467 self->expression.outr = (ir_value*)1;
2470 * Should we ever need some kind of block ordering, better make this function
2471 * move blocks around than write a block ordering algorithm later... after all
2472 * the ast and ir should work together, not against each other.
2475 /* initexpr doesn't get its own block, it's pointless, it could create more blocks
2476 * anyway if for example it contains a ternary.
2480 cgen = self->initexpr->expression.codegen;
2481 if (!(*cgen)((ast_expression*)(self->initexpr), func, false, &dummy))
2485 /* Store the block from which we enter this chaos */
2486 bin = func->curblock;
2488 /* The pre-loop condition needs its own block since we
2489 * need to be able to jump to the start of that expression.
2493 bprecond = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "pre_loop_cond"));
2497 /* the pre-loop-condition the least important place to 'continue' at */
2498 bcontinue = bprecond;
2501 func->curblock = bprecond;
2504 cgen = self->precond->expression.codegen;
2505 if (!(*cgen)((ast_expression*)(self->precond), func, false, &precond))
2508 end_bprecond = func->curblock;
2510 bprecond = end_bprecond = NULL;
2513 /* Now the next blocks won't be ordered nicely, but we need to
2514 * generate them this early for 'break' and 'continue'.
2516 if (self->increment) {
2517 bincrement = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "loop_increment"));
2520 bcontinue = bincrement; /* increment comes before the pre-loop-condition */
2522 bincrement = end_bincrement = NULL;
2525 if (self->postcond) {
2526 bpostcond = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "post_loop_cond"));
2529 bcontinue = bpostcond; /* postcond comes before the increment */
2531 bpostcond = end_bpostcond = NULL;
2534 bout_id = vec_size(func->ir_func->blocks);
2535 bout = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "after_loop"));
2540 /* The loop body... */
2541 /* if (self->body) */
2543 bbody = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "loop_body"));
2548 func->curblock = bbody;
2550 vec_push(func->breakblocks, bbreak);
2552 vec_push(func->continueblocks, bcontinue);
2554 vec_push(func->continueblocks, bbody);
2558 cgen = self->body->expression.codegen;
2559 if (!(*cgen)((ast_expression*)(self->body), func, false, &dummy))
2563 end_bbody = func->curblock;
2564 vec_pop(func->breakblocks);
2565 vec_pop(func->continueblocks);
2568 /* post-loop-condition */
2572 func->curblock = bpostcond;
2575 cgen = self->postcond->expression.codegen;
2576 if (!(*cgen)((ast_expression*)(self->postcond), func, false, &postcond))
2579 end_bpostcond = func->curblock;
2582 /* The incrementor */
2583 if (self->increment)
2586 func->curblock = bincrement;
2589 cgen = self->increment->expression.codegen;
2590 if (!(*cgen)((ast_expression*)(self->increment), func, false, &dummy))
2593 end_bincrement = func->curblock;
2596 /* In any case now, we continue from the outgoing block */
2597 func->curblock = bout;
2599 /* Now all blocks are in place */
2600 /* From 'bin' we jump to whatever comes first */
2601 if (bprecond) tmpblock = bprecond;
2602 else if (bbody) tmpblock = bbody;
2603 else if (bpostcond) tmpblock = bpostcond;
2604 else tmpblock = bout;
2605 if (!ir_block_create_jump(bin, ast_ctx(self), tmpblock))
2611 ir_block *ontrue, *onfalse;
2612 if (bbody) ontrue = bbody;
2613 else if (bincrement) ontrue = bincrement;
2614 else if (bpostcond) ontrue = bpostcond;
2615 else ontrue = bprecond;
2617 if (self->pre_not) {
2622 if (!ir_block_create_if(end_bprecond, ast_ctx(self), precond, ontrue, onfalse))
2629 if (bincrement) tmpblock = bincrement;
2630 else if (bpostcond) tmpblock = bpostcond;
2631 else if (bprecond) tmpblock = bprecond;
2632 else tmpblock = bbody;
2633 if (!end_bbody->final && !ir_block_create_jump(end_bbody, ast_ctx(self), tmpblock))
2637 /* from increment */
2640 if (bpostcond) tmpblock = bpostcond;
2641 else if (bprecond) tmpblock = bprecond;
2642 else if (bbody) tmpblock = bbody;
2643 else tmpblock = bout;
2644 if (!ir_block_create_jump(end_bincrement, ast_ctx(self), tmpblock))
2651 ir_block *ontrue, *onfalse;
2652 if (bprecond) ontrue = bprecond;
2653 else if (bbody) ontrue = bbody;
2654 else if (bincrement) ontrue = bincrement;
2655 else ontrue = bpostcond;
2657 if (self->post_not) {
2662 if (!ir_block_create_if(end_bpostcond, ast_ctx(self), postcond, ontrue, onfalse))
2666 /* Move 'bout' to the end */
2667 vec_remove(func->ir_func->blocks, bout_id, 1);
2668 vec_push(func->ir_func->blocks, bout);
2673 bool ast_breakcont_codegen(ast_breakcont *self, ast_function *func, bool lvalue, ir_value **out)
2680 compile_error(ast_ctx(self), "break/continue expression is not an l-value");
2684 if (self->expression.outr) {
2685 compile_error(ast_ctx(self), "internal error: ast_breakcont cannot be reused!");
2688 self->expression.outr = (ir_value*)1;
2690 if (self->is_continue)
2691 target = func->continueblocks[vec_size(func->continueblocks)-1-self->levels];
2693 target = func->breakblocks[vec_size(func->breakblocks)-1-self->levels];
2696 compile_error(ast_ctx(self), "%s is lacking a target block", (self->is_continue ? "continue" : "break"));
2700 if (!ir_block_create_jump(func->curblock, ast_ctx(self), target))
2705 bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_value **out)
2707 ast_expression_codegen *cgen;
2709 ast_switch_case *def_case = NULL;
2710 ir_block *def_bfall = NULL;
2711 ir_block *def_bfall_to = NULL;
2712 bool set_def_bfall_to = false;
2714 ir_value *dummy = NULL;
2715 ir_value *irop = NULL;
2716 ir_block *bout = NULL;
2717 ir_block *bfall = NULL;
2725 compile_error(ast_ctx(self), "switch expression is not an l-value");
2729 if (self->expression.outr) {
2730 compile_error(ast_ctx(self), "internal error: ast_switch cannot be reused!");
2733 self->expression.outr = (ir_value*)1;
2738 cgen = self->operand->expression.codegen;
2739 if (!(*cgen)((ast_expression*)(self->operand), func, false, &irop))
2742 if (!vec_size(self->cases))
2745 cmpinstr = type_eq_instr[irop->vtype];
2746 if (cmpinstr >= AINSTR_END) {
2747 ast_type_to_string(self->operand, typestr, sizeof(typestr));
2748 compile_error(ast_ctx(self), "invalid type to perform a switch on: %s", typestr);
2752 bout_id = vec_size(func->ir_func->blocks);
2753 bout = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "after_switch"));
2757 /* setup the break block */
2758 vec_push(func->breakblocks, bout);
2760 /* Now create all cases */
2761 for (c = 0; c < vec_size(self->cases); ++c) {
2762 ir_value *cond, *val;
2763 ir_block *bcase, *bnot;
2766 ast_switch_case *swcase = &self->cases[c];
2768 if (swcase->value) {
2769 /* A regular case */
2770 /* generate the condition operand */
2771 cgen = swcase->value->expression.codegen;
2772 if (!(*cgen)((ast_expression*)(swcase->value), func, false, &val))
2774 /* generate the condition */
2775 cond = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "switch_eq"), cmpinstr, irop, val);
2779 bcase = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "case"));
2780 bnot_id = vec_size(func->ir_func->blocks);
2781 bnot = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "not_case"));
2782 if (!bcase || !bnot)
2784 if (set_def_bfall_to) {
2785 set_def_bfall_to = false;
2786 def_bfall_to = bcase;
2788 if (!ir_block_create_if(func->curblock, ast_ctx(self), cond, bcase, bnot))
2791 /* Make the previous case-end fall through */
2792 if (bfall && !bfall->final) {
2793 if (!ir_block_create_jump(bfall, ast_ctx(self), bcase))
2797 /* enter the case */
2798 func->curblock = bcase;
2799 cgen = swcase->code->expression.codegen;
2800 if (!(*cgen)((ast_expression*)swcase->code, func, false, &dummy))
2803 /* remember this block to fall through from */
2804 bfall = func->curblock;
2806 /* enter the else and move it down */
2807 func->curblock = bnot;
2808 vec_remove(func->ir_func->blocks, bnot_id, 1);
2809 vec_push(func->ir_func->blocks, bnot);
2811 /* The default case */
2812 /* Remember where to fall through from: */
2815 /* remember which case it was */
2817 /* And the next case will be remembered */
2818 set_def_bfall_to = true;
2822 /* Jump from the last bnot to bout */
2823 if (bfall && !bfall->final && !ir_block_create_jump(bfall, ast_ctx(self), bout)) {
2825 astwarning(ast_ctx(bfall), WARN_???, "missing break after last case");
2830 /* If there was a default case, put it down here */
2834 /* No need to create an extra block */
2835 bcase = func->curblock;
2837 /* Insert the fallthrough jump */
2838 if (def_bfall && !def_bfall->final) {
2839 if (!ir_block_create_jump(def_bfall, ast_ctx(self), bcase))
2843 /* Now generate the default code */
2844 cgen = def_case->code->expression.codegen;
2845 if (!(*cgen)((ast_expression*)def_case->code, func, false, &dummy))
2848 /* see if we need to fall through */
2849 if (def_bfall_to && !func->curblock->final)
2851 if (!ir_block_create_jump(func->curblock, ast_ctx(self), def_bfall_to))
2856 /* Jump from the last bnot to bout */
2857 if (!func->curblock->final && !ir_block_create_jump(func->curblock, ast_ctx(self), bout))
2859 /* enter the outgoing block */
2860 func->curblock = bout;
2862 /* restore the break block */
2863 vec_pop(func->breakblocks);
2865 /* Move 'bout' to the end, it's nicer */
2866 vec_remove(func->ir_func->blocks, bout_id, 1);
2867 vec_push(func->ir_func->blocks, bout);
2872 bool ast_label_codegen(ast_label *self, ast_function *func, bool lvalue, ir_value **out)
2877 if (self->undefined) {
2878 compile_error(ast_ctx(self), "internal error: ast_label never defined");
2884 compile_error(ast_ctx(self), "internal error: ast_label cannot be an lvalue");
2888 /* simply create a new block and jump to it */
2889 self->irblock = ir_function_create_block(ast_ctx(self), func->ir_func, self->name);
2890 if (!self->irblock) {
2891 compile_error(ast_ctx(self), "failed to allocate label block `%s`", self->name);
2894 if (!func->curblock->final) {
2895 if (!ir_block_create_jump(func->curblock, ast_ctx(self), self->irblock))
2899 /* enter the new block */
2900 func->curblock = self->irblock;
2902 /* Generate all the leftover gotos */
2903 for (i = 0; i < vec_size(self->gotos); ++i) {
2904 if (!ast_goto_codegen(self->gotos[i], func, false, &dummy))
2911 bool ast_goto_codegen(ast_goto *self, ast_function *func, bool lvalue, ir_value **out)
2915 compile_error(ast_ctx(self), "internal error: ast_goto cannot be an lvalue");
2919 if (self->target->irblock) {
2920 if (self->irblock_from) {
2921 /* we already tried once, this is the callback */
2922 self->irblock_from->final = false;
2923 if (!ir_block_create_goto(self->irblock_from, ast_ctx(self), self->target->irblock)) {
2924 compile_error(ast_ctx(self), "failed to generate goto to `%s`", self->name);
2930 if (!ir_block_create_goto(func->curblock, ast_ctx(self), self->target->irblock)) {
2931 compile_error(ast_ctx(self), "failed to generate goto to `%s`", self->name);
2938 /* the target has not yet been created...
2939 * close this block in a sneaky way:
2941 func->curblock->final = true;
2942 self->irblock_from = func->curblock;
2943 ast_label_register_goto(self->target, self);
2949 bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value **out)
2951 ast_expression_codegen *cgen;
2953 ir_instr *callinstr;
2956 ir_value *funval = NULL;
2958 /* return values are never lvalues */
2960 compile_error(ast_ctx(self), "not an l-value (function call)");
2964 if (self->expression.outr) {
2965 *out = self->expression.outr;
2969 cgen = self->func->expression.codegen;
2970 if (!(*cgen)((ast_expression*)(self->func), func, false, &funval))
2978 for (i = 0; i < vec_size(self->params); ++i)
2981 ast_expression *expr = self->params[i];
2983 cgen = expr->expression.codegen;
2984 if (!(*cgen)(expr, func, false, ¶m))
2988 vec_push(params, param);
2991 callinstr = ir_block_create_call(func->curblock, ast_ctx(self),
2992 ast_function_label(func, "call"),
2993 funval, !!(self->func->expression.flags & AST_FLAG_NORETURN));
2997 for (i = 0; i < vec_size(params); ++i) {
2998 ir_call_param(callinstr, params[i]);
3001 *out = ir_call_value(callinstr);
3002 self->expression.outr = *out;
3004 codegen_output_type(self, *out);