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;
78 self->expression.varparam = NULL;
81 static void ast_expression_delete(ast_expression *self)
84 if (self->expression.next)
85 ast_delete(self->expression.next);
86 for (i = 0; i < vec_size(self->expression.params); ++i) {
87 ast_delete(self->expression.params[i]);
89 vec_free(self->expression.params);
92 static void ast_expression_delete_full(ast_expression *self)
94 ast_expression_delete(self);
98 ast_value* ast_value_copy(const ast_value *self)
101 const ast_expression_common *fromex;
102 ast_expression_common *selfex;
103 ast_value *cp = ast_value_new(self->expression.node.context, self->name, self->expression.vtype);
104 if (self->expression.next) {
105 cp->expression.next = ast_type_copy(self->expression.node.context, self->expression.next);
106 if (!cp->expression.next) {
107 ast_value_delete(cp);
111 fromex = &self->expression;
112 selfex = &cp->expression;
113 selfex->count = fromex->count;
114 selfex->flags = fromex->flags;
115 for (i = 0; i < vec_size(fromex->params); ++i) {
116 ast_value *v = ast_value_copy(fromex->params[i]);
118 ast_value_delete(cp);
121 vec_push(selfex->params, v);
126 bool ast_type_adopt_impl(ast_expression *self, const ast_expression *other)
129 const ast_expression_common *fromex;
130 ast_expression_common *selfex;
131 self->expression.vtype = other->expression.vtype;
132 if (other->expression.next) {
133 self->expression.next = (ast_expression*)ast_type_copy(ast_ctx(self), other->expression.next);
134 if (!self->expression.next)
137 fromex = &other->expression;
138 selfex = &self->expression;
139 selfex->count = fromex->count;
140 selfex->flags = fromex->flags;
141 for (i = 0; i < vec_size(fromex->params); ++i) {
142 ast_value *v = ast_value_copy(fromex->params[i]);
145 vec_push(selfex->params, v);
150 static ast_expression* ast_shallow_type(lex_ctx ctx, int vtype)
152 ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
153 ast_expression_init(self, NULL);
154 self->expression.codegen = NULL;
155 self->expression.next = NULL;
156 self->expression.vtype = vtype;
160 ast_expression* ast_type_copy(lex_ctx ctx, const ast_expression *ex)
163 const ast_expression_common *fromex;
164 ast_expression_common *selfex;
170 ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
171 ast_expression_init(self, NULL);
173 fromex = &ex->expression;
174 selfex = &self->expression;
176 /* This may never be codegen()d */
177 selfex->codegen = NULL;
179 selfex->vtype = fromex->vtype;
182 selfex->next = ast_type_copy(ctx, fromex->next);
184 ast_expression_delete_full(self);
191 selfex->count = fromex->count;
192 selfex->flags = fromex->flags;
193 for (i = 0; i < vec_size(fromex->params); ++i) {
194 ast_value *v = ast_value_copy(fromex->params[i]);
196 ast_expression_delete_full(self);
199 vec_push(selfex->params, v);
206 bool ast_compare_type(ast_expression *a, ast_expression *b)
208 if (a->expression.vtype == TYPE_NIL ||
209 b->expression.vtype == TYPE_NIL)
211 if (a->expression.vtype != b->expression.vtype)
213 if (!a->expression.next != !b->expression.next)
215 if (vec_size(a->expression.params) != vec_size(b->expression.params))
217 if ((a->expression.flags & AST_FLAG_TYPE_MASK) !=
218 (b->expression.flags & AST_FLAG_TYPE_MASK) )
222 if (vec_size(a->expression.params)) {
224 for (i = 0; i < vec_size(a->expression.params); ++i) {
225 if (!ast_compare_type((ast_expression*)a->expression.params[i],
226 (ast_expression*)b->expression.params[i]))
230 if (a->expression.next)
231 return ast_compare_type(a->expression.next, b->expression.next);
235 static size_t ast_type_to_string_impl(ast_expression *e, char *buf, size_t bufsize, size_t pos)
242 if (pos + 6 >= bufsize)
244 strcpy(buf + pos, "(null)");
248 if (pos + 1 >= bufsize)
251 switch (e->expression.vtype) {
253 strcpy(buf + pos, "(variant)");
258 return ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
261 if (pos + 3 >= bufsize)
265 pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
266 if (pos + 1 >= bufsize)
272 pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
273 if (pos + 2 >= bufsize)
275 if (!vec_size(e->expression.params)) {
281 pos = ast_type_to_string_impl((ast_expression*)(e->expression.params[0]), buf, bufsize, pos);
282 for (i = 1; i < vec_size(e->expression.params); ++i) {
283 if (pos + 2 >= bufsize)
287 pos = ast_type_to_string_impl((ast_expression*)(e->expression.params[i]), buf, bufsize, pos);
289 if (pos + 1 >= bufsize)
295 pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
296 if (pos + 1 >= bufsize)
299 pos += snprintf(buf + pos, bufsize - pos - 1, "%i", (int)e->expression.count);
300 if (pos + 1 >= bufsize)
306 typestr = type_name[e->expression.vtype];
307 typelen = strlen(typestr);
308 if (pos + typelen >= bufsize)
310 strcpy(buf + pos, typestr);
311 return pos + typelen;
315 buf[bufsize-3] = '.';
316 buf[bufsize-2] = '.';
317 buf[bufsize-1] = '.';
321 void ast_type_to_string(ast_expression *e, char *buf, size_t bufsize)
323 size_t pos = ast_type_to_string_impl(e, buf, bufsize-1, 0);
327 ast_value* ast_value_new(lex_ctx ctx, const char *name, int t)
329 ast_instantiate(ast_value, ctx, ast_value_delete);
330 ast_expression_init((ast_expression*)self,
331 (ast_expression_codegen*)&ast_value_codegen);
332 self->expression.node.keep = true; /* keep */
334 self->name = name ? util_strdup(name) : NULL;
335 self->expression.vtype = t;
336 self->expression.next = NULL;
337 self->isfield = false;
339 self->hasvalue = false;
341 memset(&self->constval, 0, sizeof(self->constval));
344 self->ir_values = NULL;
345 self->ir_value_count = 0;
354 void ast_value_delete(ast_value* self)
357 mem_d((void*)self->name);
358 if (self->hasvalue) {
359 switch (self->expression.vtype)
362 mem_d((void*)self->constval.vstring);
365 /* unlink us from the function node */
366 self->constval.vfunc->vtype = NULL;
368 /* NOTE: delete function? currently collected in
369 * the parser structure
376 mem_d(self->ir_values);
381 ast_expression_delete((ast_expression*)self);
385 void ast_value_params_add(ast_value *self, ast_value *p)
387 vec_push(self->expression.params, p);
390 bool ast_value_set_name(ast_value *self, const char *name)
393 mem_d((void*)self->name);
394 self->name = util_strdup(name);
398 ast_binary* ast_binary_new(lex_ctx ctx, int op,
399 ast_expression* left, ast_expression* right)
401 ast_instantiate(ast_binary, ctx, ast_binary_delete);
402 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binary_codegen);
408 ast_propagate_effects(self, left);
409 ast_propagate_effects(self, right);
411 if (op >= INSTR_EQ_F && op <= INSTR_GT)
412 self->expression.vtype = TYPE_FLOAT;
413 else if (op == INSTR_AND || op == INSTR_OR) {
414 if (OPTS_FLAG(PERL_LOGIC))
415 ast_type_adopt(self, right);
417 self->expression.vtype = TYPE_FLOAT;
419 else if (op == INSTR_BITAND || op == INSTR_BITOR)
420 self->expression.vtype = TYPE_FLOAT;
421 else if (op == INSTR_MUL_VF || op == INSTR_MUL_FV)
422 self->expression.vtype = TYPE_VECTOR;
423 else if (op == INSTR_MUL_V)
424 self->expression.vtype = TYPE_FLOAT;
426 self->expression.vtype = left->expression.vtype;
431 void ast_binary_delete(ast_binary *self)
433 ast_unref(self->left);
434 ast_unref(self->right);
435 ast_expression_delete((ast_expression*)self);
439 ast_binstore* ast_binstore_new(lex_ctx ctx, int storop, int op,
440 ast_expression* left, ast_expression* right)
442 ast_instantiate(ast_binstore, ctx, ast_binstore_delete);
443 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binstore_codegen);
445 ast_side_effects(self) = true;
447 self->opstore = storop;
450 self->source = right;
452 self->keep_dest = false;
454 if (!ast_type_adopt(self, left)) {
462 void ast_binstore_delete(ast_binstore *self)
464 if (!self->keep_dest)
465 ast_unref(self->dest);
466 ast_unref(self->source);
467 ast_expression_delete((ast_expression*)self);
471 ast_unary* ast_unary_new(lex_ctx ctx, int op,
472 ast_expression *expr)
474 ast_instantiate(ast_unary, ctx, ast_unary_delete);
475 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_unary_codegen);
478 self->operand = expr;
480 ast_propagate_effects(self, expr);
482 if (op >= INSTR_NOT_F && op <= INSTR_NOT_FNC) {
483 self->expression.vtype = TYPE_FLOAT;
485 compile_error(ctx, "cannot determine type of unary operation %s", asm_instr[op].m);
490 void ast_unary_delete(ast_unary *self)
492 if (self->operand) ast_unref(self->operand);
493 ast_expression_delete((ast_expression*)self);
497 ast_return* ast_return_new(lex_ctx ctx, ast_expression *expr)
499 ast_instantiate(ast_return, ctx, ast_return_delete);
500 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_return_codegen);
502 self->operand = expr;
505 ast_propagate_effects(self, expr);
510 void ast_return_delete(ast_return *self)
513 ast_unref(self->operand);
514 ast_expression_delete((ast_expression*)self);
518 ast_entfield* ast_entfield_new(lex_ctx ctx, ast_expression *entity, ast_expression *field)
520 if (field->expression.vtype != TYPE_FIELD) {
521 compile_error(ctx, "ast_entfield_new with expression not of type field");
524 return ast_entfield_new_force(ctx, entity, field, field->expression.next);
527 ast_entfield* ast_entfield_new_force(lex_ctx ctx, ast_expression *entity, ast_expression *field, const ast_expression *outtype)
529 ast_instantiate(ast_entfield, ctx, ast_entfield_delete);
533 /* Error: field has no type... */
537 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_entfield_codegen);
539 self->entity = entity;
541 ast_propagate_effects(self, entity);
542 ast_propagate_effects(self, field);
544 if (!ast_type_adopt(self, outtype)) {
545 ast_entfield_delete(self);
552 void ast_entfield_delete(ast_entfield *self)
554 ast_unref(self->entity);
555 ast_unref(self->field);
556 ast_expression_delete((ast_expression*)self);
560 ast_member* ast_member_new(lex_ctx ctx, ast_expression *owner, unsigned int field, const char *name)
562 ast_instantiate(ast_member, ctx, ast_member_delete);
568 if (owner->expression.vtype != TYPE_VECTOR &&
569 owner->expression.vtype != TYPE_FIELD) {
570 compile_error(ctx, "member-access on an invalid owner of type %s", type_name[owner->expression.vtype]);
575 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_member_codegen);
576 self->expression.node.keep = true; /* keep */
578 if (owner->expression.vtype == TYPE_VECTOR) {
579 self->expression.vtype = TYPE_FLOAT;
580 self->expression.next = NULL;
582 self->expression.vtype = TYPE_FIELD;
583 self->expression.next = ast_shallow_type(ctx, TYPE_FLOAT);
586 self->rvalue = false;
588 ast_propagate_effects(self, owner);
592 self->name = util_strdup(name);
599 void ast_member_delete(ast_member *self)
601 /* The owner is always an ast_value, which has .keep=true,
602 * also: ast_members are usually deleted after the owner, thus
603 * this will cause invalid access
604 ast_unref(self->owner);
605 * once we allow (expression).x to access a vector-member, we need
606 * to change this: preferably by creating an alternate ast node for this
607 * purpose that is not garbage-collected.
609 ast_expression_delete((ast_expression*)self);
613 bool ast_member_set_name(ast_member *self, const char *name)
616 mem_d((void*)self->name);
617 self->name = util_strdup(name);
621 ast_array_index* ast_array_index_new(lex_ctx ctx, ast_expression *array, ast_expression *index)
623 ast_expression *outtype;
624 ast_instantiate(ast_array_index, ctx, ast_array_index_delete);
626 outtype = array->expression.next;
629 /* Error: field has no type... */
633 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_array_index_codegen);
637 ast_propagate_effects(self, array);
638 ast_propagate_effects(self, index);
640 if (!ast_type_adopt(self, outtype)) {
641 ast_array_index_delete(self);
644 if (array->expression.vtype == TYPE_FIELD && outtype->expression.vtype == TYPE_ARRAY) {
645 if (self->expression.vtype != TYPE_ARRAY) {
646 compile_error(ast_ctx(self), "array_index node on type");
647 ast_array_index_delete(self);
650 self->array = outtype;
651 self->expression.vtype = TYPE_FIELD;
657 void ast_array_index_delete(ast_array_index *self)
659 ast_unref(self->array);
660 ast_unref(self->index);
661 ast_expression_delete((ast_expression*)self);
665 ast_ifthen* ast_ifthen_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
667 ast_instantiate(ast_ifthen, ctx, ast_ifthen_delete);
668 if (!ontrue && !onfalse) {
669 /* because it is invalid */
673 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ifthen_codegen);
676 self->on_true = ontrue;
677 self->on_false = onfalse;
678 ast_propagate_effects(self, cond);
680 ast_propagate_effects(self, ontrue);
682 ast_propagate_effects(self, onfalse);
687 void ast_ifthen_delete(ast_ifthen *self)
689 ast_unref(self->cond);
691 ast_unref(self->on_true);
693 ast_unref(self->on_false);
694 ast_expression_delete((ast_expression*)self);
698 ast_ternary* ast_ternary_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
700 ast_expression *exprtype = ontrue;
701 ast_instantiate(ast_ternary, ctx, ast_ternary_delete);
702 /* This time NEITHER must be NULL */
703 if (!ontrue || !onfalse) {
707 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ternary_codegen);
710 self->on_true = ontrue;
711 self->on_false = onfalse;
712 ast_propagate_effects(self, cond);
713 ast_propagate_effects(self, ontrue);
714 ast_propagate_effects(self, onfalse);
716 if (ontrue->expression.vtype == TYPE_NIL)
718 if (!ast_type_adopt(self, exprtype)) {
719 ast_ternary_delete(self);
726 void ast_ternary_delete(ast_ternary *self)
728 /* the if()s are only there because computed-gotos can set them
731 if (self->cond) ast_unref(self->cond);
732 if (self->on_true) ast_unref(self->on_true);
733 if (self->on_false) ast_unref(self->on_false);
734 ast_expression_delete((ast_expression*)self);
738 ast_loop* ast_loop_new(lex_ctx ctx,
739 ast_expression *initexpr,
740 ast_expression *precond, bool pre_not,
741 ast_expression *postcond, bool post_not,
742 ast_expression *increment,
743 ast_expression *body)
745 ast_instantiate(ast_loop, ctx, ast_loop_delete);
746 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_loop_codegen);
748 self->initexpr = initexpr;
749 self->precond = precond;
750 self->postcond = postcond;
751 self->increment = increment;
754 self->pre_not = pre_not;
755 self->post_not = post_not;
758 ast_propagate_effects(self, initexpr);
760 ast_propagate_effects(self, precond);
762 ast_propagate_effects(self, postcond);
764 ast_propagate_effects(self, increment);
766 ast_propagate_effects(self, body);
771 void ast_loop_delete(ast_loop *self)
774 ast_unref(self->initexpr);
776 ast_unref(self->precond);
778 ast_unref(self->postcond);
780 ast_unref(self->increment);
782 ast_unref(self->body);
783 ast_expression_delete((ast_expression*)self);
787 ast_breakcont* ast_breakcont_new(lex_ctx ctx, bool iscont, unsigned int levels)
789 ast_instantiate(ast_breakcont, ctx, ast_breakcont_delete);
790 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_breakcont_codegen);
792 self->is_continue = iscont;
793 self->levels = levels;
798 void ast_breakcont_delete(ast_breakcont *self)
800 ast_expression_delete((ast_expression*)self);
804 ast_switch* ast_switch_new(lex_ctx ctx, ast_expression *op)
806 ast_instantiate(ast_switch, ctx, ast_switch_delete);
807 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_switch_codegen);
812 ast_propagate_effects(self, op);
817 void ast_switch_delete(ast_switch *self)
820 ast_unref(self->operand);
822 for (i = 0; i < vec_size(self->cases); ++i) {
823 if (self->cases[i].value)
824 ast_unref(self->cases[i].value);
825 ast_unref(self->cases[i].code);
827 vec_free(self->cases);
829 ast_expression_delete((ast_expression*)self);
833 ast_label* ast_label_new(lex_ctx ctx, const char *name, bool undefined)
835 ast_instantiate(ast_label, ctx, ast_label_delete);
836 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_label_codegen);
838 self->expression.vtype = TYPE_NOEXPR;
840 self->name = util_strdup(name);
841 self->irblock = NULL;
843 self->undefined = undefined;
848 void ast_label_delete(ast_label *self)
850 mem_d((void*)self->name);
851 vec_free(self->gotos);
852 ast_expression_delete((ast_expression*)self);
856 void ast_label_register_goto(ast_label *self, ast_goto *g)
858 vec_push(self->gotos, g);
861 ast_goto* ast_goto_new(lex_ctx ctx, const char *name)
863 ast_instantiate(ast_goto, ctx, ast_goto_delete);
864 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_goto_codegen);
866 self->name = util_strdup(name);
868 self->irblock_from = NULL;
873 void ast_goto_delete(ast_goto *self)
875 mem_d((void*)self->name);
876 ast_expression_delete((ast_expression*)self);
880 void ast_goto_set_label(ast_goto *self, ast_label *label)
882 self->target = label;
885 ast_call* ast_call_new(lex_ctx ctx,
886 ast_expression *funcexpr)
888 ast_instantiate(ast_call, ctx, ast_call_delete);
889 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_call_codegen);
891 ast_side_effects(self) = true;
894 self->func = funcexpr;
896 ast_type_adopt(self, funcexpr->expression.next);
901 void ast_call_delete(ast_call *self)
904 for (i = 0; i < vec_size(self->params); ++i)
905 ast_unref(self->params[i]);
906 vec_free(self->params);
909 ast_unref(self->func);
911 ast_expression_delete((ast_expression*)self);
915 bool ast_call_check_types(ast_call *self)
921 const ast_expression *func = self->func;
922 size_t count = vec_size(self->params);
923 if (count > vec_size(func->expression.params))
924 count = vec_size(func->expression.params);
926 for (i = 0; i < count; ++i) {
927 if (!ast_compare_type(self->params[i], (ast_expression*)(func->expression.params[i])))
929 ast_type_to_string(self->params[i], tgot, sizeof(tgot));
930 ast_type_to_string((ast_expression*)func->expression.params[i], texp, sizeof(texp));
931 compile_error(ast_ctx(self), "invalid type for parameter %u in function call: expected %s, got %s",
932 (unsigned int)(i+1), texp, tgot);
933 /* we don't immediately return */
937 count = vec_size(self->params);
938 if (count > vec_size(func->expression.params) && func->expression.varparam) {
939 for (; i < count; ++i) {
940 if (!ast_compare_type(self->params[i], func->expression.varparam))
942 ast_type_to_string(self->params[i], tgot, sizeof(tgot));
943 ast_type_to_string(func->expression.varparam, texp, sizeof(texp));
944 compile_error(ast_ctx(self), "invalid type for parameter %u in function call: expected %s, got %s",
945 (unsigned int)(i+1), texp, tgot);
946 /* we don't immediately return */
954 ast_store* ast_store_new(lex_ctx ctx, int op,
955 ast_expression *dest, ast_expression *source)
957 ast_instantiate(ast_store, ctx, ast_store_delete);
958 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_store_codegen);
960 ast_side_effects(self) = true;
964 self->source = source;
966 if (!ast_type_adopt(self, dest)) {
974 void ast_store_delete(ast_store *self)
976 ast_unref(self->dest);
977 ast_unref(self->source);
978 ast_expression_delete((ast_expression*)self);
982 ast_block* ast_block_new(lex_ctx ctx)
984 ast_instantiate(ast_block, ctx, ast_block_delete);
985 ast_expression_init((ast_expression*)self,
986 (ast_expression_codegen*)&ast_block_codegen);
990 self->collect = NULL;
995 bool ast_block_add_expr(ast_block *self, ast_expression *e)
997 ast_propagate_effects(self, e);
998 vec_push(self->exprs, e);
999 if (self->expression.next) {
1000 ast_delete(self->expression.next);
1001 self->expression.next = NULL;
1003 if (!ast_type_adopt(self, e)) {
1004 compile_error(ast_ctx(self), "internal error: failed to adopt type");
1010 void ast_block_collect(ast_block *self, ast_expression *expr)
1012 vec_push(self->collect, expr);
1013 expr->expression.node.keep = true;
1016 void ast_block_delete(ast_block *self)
1019 for (i = 0; i < vec_size(self->exprs); ++i)
1020 ast_unref(self->exprs[i]);
1021 vec_free(self->exprs);
1022 for (i = 0; i < vec_size(self->locals); ++i)
1023 ast_delete(self->locals[i]);
1024 vec_free(self->locals);
1025 for (i = 0; i < vec_size(self->collect); ++i)
1026 ast_delete(self->collect[i]);
1027 vec_free(self->collect);
1028 ast_expression_delete((ast_expression*)self);
1032 bool ast_block_set_type(ast_block *self, ast_expression *from)
1034 if (self->expression.next)
1035 ast_delete(self->expression.next);
1036 if (!ast_type_adopt(self, from))
1041 ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype)
1043 ast_instantiate(ast_function, ctx, ast_function_delete);
1047 vtype->expression.vtype != TYPE_FUNCTION)
1049 compile_error(ast_ctx(self), "internal error: ast_function_new condition %i %i type=%i (probably 2 bodies?)",
1051 (int)vtype->hasvalue,
1052 vtype->expression.vtype);
1057 self->vtype = vtype;
1058 self->name = name ? util_strdup(name) : NULL;
1059 self->blocks = NULL;
1061 self->labelcount = 0;
1064 self->ir_func = NULL;
1065 self->curblock = NULL;
1067 self->breakblocks = NULL;
1068 self->continueblocks = NULL;
1070 vtype->hasvalue = true;
1071 vtype->constval.vfunc = self;
1076 void ast_function_delete(ast_function *self)
1080 mem_d((void*)self->name);
1082 /* ast_value_delete(self->vtype); */
1083 self->vtype->hasvalue = false;
1084 self->vtype->constval.vfunc = NULL;
1085 /* We use unref - if it was stored in a global table it is supposed
1086 * to be deleted from *there*
1088 ast_unref(self->vtype);
1090 for (i = 0; i < vec_size(self->blocks); ++i)
1091 ast_delete(self->blocks[i]);
1092 vec_free(self->blocks);
1093 vec_free(self->breakblocks);
1094 vec_free(self->continueblocks);
1098 const char* ast_function_label(ast_function *self, const char *prefix)
1104 if (!opts.dump && !opts.dumpfin && !opts.debug)
1107 id = (self->labelcount++);
1108 len = strlen(prefix);
1110 from = self->labelbuf + sizeof(self->labelbuf)-1;
1113 *from-- = (id%10) + '0';
1117 memcpy(from - len, prefix, len);
1121 /*********************************************************************/
1123 * by convention you must never pass NULL to the 'ir_value **out'
1124 * parameter. If you really don't care about the output, pass a dummy.
1125 * But I can't imagine a pituation where the output is truly unnecessary.
1128 void _ast_codegen_output_type(ast_expression_common *self, ir_value *out)
1130 if (out->vtype == TYPE_FIELD)
1131 out->fieldtype = self->next->expression.vtype;
1132 if (out->vtype == TYPE_FUNCTION)
1133 out->outtype = self->next->expression.vtype;
1136 #define codegen_output_type(a,o) (_ast_codegen_output_type(&((a)->expression),(o)))
1138 bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out)
1142 if (self->expression.vtype == TYPE_NIL) {
1143 *out = func->ir_func->owner->nil;
1146 /* NOTE: This is the codegen for a variable used in an expression.
1147 * It is not the codegen to generate the value. For this purpose,
1148 * ast_local_codegen and ast_global_codegen are to be used before this
1149 * is executed. ast_function_codegen should take care of its locals,
1150 * and the ast-user should take care of ast_global_codegen to be used
1151 * on all the globals.
1154 char tname[1024]; /* typename is reserved in C++ */
1155 ast_type_to_string((ast_expression*)self, tname, sizeof(tname));
1156 compile_error(ast_ctx(self), "ast_value used before generated %s %s", tname, self->name);
1163 bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
1167 if (self->expression.vtype == TYPE_NIL) {
1168 compile_error(ast_ctx(self), "internal error: trying to generate a variable of TYPE_NIL");
1172 if (self->hasvalue && self->expression.vtype == TYPE_FUNCTION)
1174 ir_function *func = ir_builder_create_function(ir, self->name, self->expression.next->expression.vtype);
1177 func->context = ast_ctx(self);
1178 func->value->context = ast_ctx(self);
1180 self->constval.vfunc->ir_func = func;
1181 self->ir_v = func->value;
1182 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1183 self->ir_v->flags |= IR_FLAG_INCLUDE_DEF;
1184 /* The function is filled later on ast_function_codegen... */
1188 if (isfield && self->expression.vtype == TYPE_FIELD) {
1189 ast_expression *fieldtype = self->expression.next;
1191 if (self->hasvalue) {
1192 compile_error(ast_ctx(self), "TODO: constant field pointers with value");
1196 if (fieldtype->expression.vtype == TYPE_ARRAY) {
1201 ast_expression_common *elemtype;
1203 ast_value *array = (ast_value*)fieldtype;
1205 if (!ast_istype(fieldtype, ast_value)) {
1206 compile_error(ast_ctx(self), "internal error: ast_value required");
1210 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1211 if (!array->expression.count || array->expression.count > opts.max_array_size)
1212 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)array->expression.count);
1214 elemtype = &array->expression.next->expression;
1215 vtype = elemtype->vtype;
1217 v = ir_builder_create_field(ir, self->name, vtype);
1219 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", self->name);
1222 v->context = ast_ctx(self);
1223 v->unique_life = true;
1225 array->ir_v = self->ir_v = v;
1226 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1227 self->ir_v->flags |= IR_FLAG_INCLUDE_DEF;
1229 namelen = strlen(self->name);
1230 name = (char*)mem_a(namelen + 16);
1231 strcpy(name, self->name);
1233 array->ir_values = (ir_value**)mem_a(sizeof(array->ir_values[0]) * array->expression.count);
1234 array->ir_values[0] = v;
1235 for (ai = 1; ai < array->expression.count; ++ai) {
1236 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1237 array->ir_values[ai] = ir_builder_create_field(ir, name, vtype);
1238 if (!array->ir_values[ai]) {
1240 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", name);
1243 array->ir_values[ai]->context = ast_ctx(self);
1244 array->ir_values[ai]->unique_life = true;
1245 array->ir_values[ai]->locked = true;
1246 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1247 self->ir_values[ai]->flags |= IR_FLAG_INCLUDE_DEF;
1253 v = ir_builder_create_field(ir, self->name, self->expression.next->expression.vtype);
1256 v->context = ast_ctx(self);
1258 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1259 self->ir_v->flags |= IR_FLAG_INCLUDE_DEF;
1264 if (self->expression.vtype == TYPE_ARRAY) {
1269 ast_expression_common *elemtype = &self->expression.next->expression;
1270 int vtype = elemtype->vtype;
1272 /* same as with field arrays */
1273 if (!self->expression.count || self->expression.count > opts.max_array_size)
1274 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1276 v = ir_builder_create_global(ir, self->name, vtype);
1278 compile_error(ast_ctx(self), "ir_builder_create_global failed `%s`", self->name);
1281 v->context = ast_ctx(self);
1282 v->unique_life = true;
1284 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1285 v->flags |= IR_FLAG_INCLUDE_DEF;
1287 namelen = strlen(self->name);
1288 name = (char*)mem_a(namelen + 16);
1289 strcpy(name, self->name);
1291 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1292 self->ir_values[0] = v;
1293 for (ai = 1; ai < self->expression.count; ++ai) {
1294 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1295 self->ir_values[ai] = ir_builder_create_global(ir, name, vtype);
1296 if (!self->ir_values[ai]) {
1298 compile_error(ast_ctx(self), "ir_builder_create_global failed `%s`", name);
1301 self->ir_values[ai]->context = ast_ctx(self);
1302 self->ir_values[ai]->unique_life = true;
1303 self->ir_values[ai]->locked = true;
1304 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1305 self->ir_values[ai]->flags |= IR_FLAG_INCLUDE_DEF;
1311 /* Arrays don't do this since there's no "array" value which spans across the
1314 v = ir_builder_create_global(ir, self->name, self->expression.vtype);
1316 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", self->name);
1319 codegen_output_type(self, v);
1320 v->context = ast_ctx(self);
1323 if (self->hasvalue) {
1324 switch (self->expression.vtype)
1327 if (!ir_value_set_float(v, self->constval.vfloat))
1331 if (!ir_value_set_vector(v, self->constval.vvec))
1335 if (!ir_value_set_string(v, self->constval.vstring))
1339 compile_error(ast_ctx(self), "TODO: global constant array");
1342 compile_error(ast_ctx(self), "global of type function not properly generated");
1344 /* Cannot generate an IR value for a function,
1345 * need a pointer pointing to a function rather.
1348 if (!self->constval.vfield) {
1349 compile_error(ast_ctx(self), "field constant without vfield set");
1352 if (!self->constval.vfield->ir_v) {
1353 compile_error(ast_ctx(self), "field constant generated before its field");
1356 if (!ir_value_set_field(v, self->constval.vfield->ir_v))
1360 compile_error(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1365 /* link us to the ir_value */
1368 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1369 self->ir_v->flags |= IR_FLAG_INCLUDE_DEF;
1372 error: /* clean up */
1377 bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
1381 if (self->expression.vtype == TYPE_NIL) {
1382 compile_error(ast_ctx(self), "internal error: trying to generate a variable of TYPE_NIL");
1386 if (self->hasvalue && self->expression.vtype == TYPE_FUNCTION)
1388 /* Do we allow local functions? I think not...
1389 * this is NOT a function pointer atm.
1394 if (self->expression.vtype == TYPE_ARRAY) {
1399 ast_expression_common *elemtype = &self->expression.next->expression;
1400 int vtype = elemtype->vtype;
1402 func->flags |= IR_FLAG_HAS_ARRAYS;
1405 compile_error(ast_ctx(self), "array-parameters are not supported");
1409 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1410 if (!self->expression.count || self->expression.count > opts.max_array_size) {
1411 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1414 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1415 if (!self->ir_values) {
1416 compile_error(ast_ctx(self), "failed to allocate array values");
1420 v = ir_function_create_local(func, self->name, vtype, param);
1422 compile_error(ast_ctx(self), "ir_function_create_local failed");
1425 v->context = ast_ctx(self);
1426 v->unique_life = true;
1429 namelen = strlen(self->name);
1430 name = (char*)mem_a(namelen + 16);
1431 strcpy(name, self->name);
1433 self->ir_values[0] = v;
1434 for (ai = 1; ai < self->expression.count; ++ai) {
1435 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1436 self->ir_values[ai] = ir_function_create_local(func, name, vtype, param);
1437 if (!self->ir_values[ai]) {
1438 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", name);
1441 self->ir_values[ai]->context = ast_ctx(self);
1442 self->ir_values[ai]->unique_life = true;
1443 self->ir_values[ai]->locked = true;
1448 v = ir_function_create_local(func, self->name, self->expression.vtype, param);
1451 codegen_output_type(self, v);
1452 v->context = ast_ctx(self);
1455 /* A constant local... hmmm...
1456 * I suppose the IR will have to deal with this
1458 if (self->hasvalue) {
1459 switch (self->expression.vtype)
1462 if (!ir_value_set_float(v, self->constval.vfloat))
1466 if (!ir_value_set_vector(v, self->constval.vvec))
1470 if (!ir_value_set_string(v, self->constval.vstring))
1474 compile_error(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1479 /* link us to the ir_value */
1483 if (!ast_generate_accessors(self, func->owner))
1487 error: /* clean up */
1492 bool ast_generate_accessors(ast_value *self, ir_builder *ir)
1495 bool warn = OPTS_WARN(WARN_USED_UNINITIALIZED);
1496 if (!self->setter || !self->getter)
1498 for (i = 0; i < self->expression.count; ++i) {
1499 if (!self->ir_values) {
1500 compile_error(ast_ctx(self), "internal error: no array values generated for `%s`", self->name);
1503 if (!self->ir_values[i]) {
1504 compile_error(ast_ctx(self), "internal error: not all array values have been generated for `%s`", self->name);
1507 if (self->ir_values[i]->life) {
1508 compile_error(ast_ctx(self), "internal error: function containing `%s` already generated", self->name);
1513 opts_set(opts.warn, WARN_USED_UNINITIALIZED, false);
1515 if (!ast_global_codegen (self->setter, ir, false) ||
1516 !ast_function_codegen(self->setter->constval.vfunc, ir) ||
1517 !ir_function_finalize(self->setter->constval.vfunc->ir_func))
1519 compile_error(ast_ctx(self), "internal error: failed to generate setter for `%s`", self->name);
1520 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1525 if (!ast_global_codegen (self->getter, ir, false) ||
1526 !ast_function_codegen(self->getter->constval.vfunc, ir) ||
1527 !ir_function_finalize(self->getter->constval.vfunc->ir_func))
1529 compile_error(ast_ctx(self), "internal error: failed to generate getter for `%s`", self->name);
1530 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1534 for (i = 0; i < self->expression.count; ++i) {
1535 vec_free(self->ir_values[i]->life);
1537 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1541 bool ast_function_codegen(ast_function *self, ir_builder *ir)
1545 ast_expression_common *ec;
1550 irf = self->ir_func;
1552 compile_error(ast_ctx(self), "ast_function's related ast_value was not generated yet");
1556 /* fill the parameter list */
1557 ec = &self->vtype->expression;
1558 for (i = 0; i < vec_size(ec->params); ++i)
1560 if (ec->params[i]->expression.vtype == TYPE_FIELD)
1561 vec_push(irf->params, ec->params[i]->expression.next->expression.vtype);
1563 vec_push(irf->params, ec->params[i]->expression.vtype);
1564 if (!self->builtin) {
1565 if (!ast_local_codegen(ec->params[i], self->ir_func, true))
1570 if (self->builtin) {
1571 irf->builtin = self->builtin;
1575 if (!vec_size(self->blocks)) {
1576 compile_error(ast_ctx(self), "function `%s` has no body", self->name);
1580 self->curblock = ir_function_create_block(ast_ctx(self), irf, "entry");
1581 if (!self->curblock) {
1582 compile_error(ast_ctx(self), "failed to allocate entry block for `%s`", self->name);
1586 for (i = 0; i < vec_size(self->blocks); ++i) {
1587 ast_expression_codegen *gen = self->blocks[i]->expression.codegen;
1588 if (!(*gen)((ast_expression*)self->blocks[i], self, false, &dummy))
1592 /* TODO: check return types */
1593 if (!self->curblock->final)
1595 if (!self->vtype->expression.next ||
1596 self->vtype->expression.next->expression.vtype == TYPE_VOID)
1598 return ir_block_create_return(self->curblock, ast_ctx(self), NULL);
1600 else if (vec_size(self->curblock->entries))
1602 /* error("missing return"); */
1603 if (compile_warning(ast_ctx(self), WARN_MISSING_RETURN_VALUES,
1604 "control reaches end of non-void function (`%s`) via %s",
1605 self->name, self->curblock->label))
1609 return ir_block_create_return(self->curblock, ast_ctx(self), NULL);
1615 /* Note, you will not see ast_block_codegen generate ir_blocks.
1616 * To the AST and the IR, blocks are 2 different things.
1617 * In the AST it represents a block of code, usually enclosed in
1618 * curly braces {...}.
1619 * While in the IR it represents a block in terms of control-flow.
1621 bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_value **out)
1625 /* We don't use this
1626 * Note: an ast-representation using the comma-operator
1627 * of the form: (a, b, c) = x should not assign to c...
1630 compile_error(ast_ctx(self), "not an l-value (code-block)");
1634 if (self->expression.outr) {
1635 *out = self->expression.outr;
1639 /* output is NULL at first, we'll have each expression
1640 * assign to out output, thus, a comma-operator represention
1641 * using an ast_block will return the last generated value,
1642 * so: (b, c) + a executed both b and c, and returns c,
1643 * which is then added to a.
1647 /* generate locals */
1648 for (i = 0; i < vec_size(self->locals); ++i)
1650 if (!ast_local_codegen(self->locals[i], func->ir_func, false)) {
1652 compile_error(ast_ctx(self), "failed to generate local `%s`", self->locals[i]->name);
1657 for (i = 0; i < vec_size(self->exprs); ++i)
1659 ast_expression_codegen *gen;
1660 if (func->curblock->final && !ast_istype(self->exprs[i], ast_label)) {
1661 if (compile_warning(ast_ctx(self->exprs[i]), WARN_UNREACHABLE_CODE, "unreachable statement"))
1665 gen = self->exprs[i]->expression.codegen;
1666 if (!(*gen)(self->exprs[i], func, false, out))
1670 self->expression.outr = *out;
1675 bool ast_store_codegen(ast_store *self, ast_function *func, bool lvalue, ir_value **out)
1677 ast_expression_codegen *cgen;
1678 ir_value *left = NULL;
1679 ir_value *right = NULL;
1683 ast_array_index *ai = NULL;
1685 if (lvalue && self->expression.outl) {
1686 *out = self->expression.outl;
1690 if (!lvalue && self->expression.outr) {
1691 *out = self->expression.outr;
1695 if (ast_istype(self->dest, ast_array_index))
1698 ai = (ast_array_index*)self->dest;
1699 idx = (ast_value*)ai->index;
1701 if (ast_istype(ai->index, ast_value) && idx->hasvalue && idx->cvq == CV_CONST)
1706 /* we need to call the setter */
1707 ir_value *iridx, *funval;
1711 compile_error(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1715 arr = (ast_value*)ai->array;
1716 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1717 compile_error(ast_ctx(self), "value has no setter (%s)", arr->name);
1721 cgen = idx->expression.codegen;
1722 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1725 cgen = arr->setter->expression.codegen;
1726 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
1729 cgen = self->source->expression.codegen;
1730 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1733 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "store"), funval, false);
1736 ir_call_param(call, iridx);
1737 ir_call_param(call, right);
1738 self->expression.outr = right;
1744 cgen = self->dest->expression.codegen;
1746 if (!(*cgen)((ast_expression*)(self->dest), func, true, &left))
1748 self->expression.outl = left;
1750 cgen = self->source->expression.codegen;
1752 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1755 if (!ir_block_create_store_op(func->curblock, ast_ctx(self), self->op, left, right))
1757 self->expression.outr = right;
1760 /* Theoretically, an assinment returns its left side as an
1761 * lvalue, if we don't need an lvalue though, we return
1762 * the right side as an rvalue, otherwise we have to
1763 * somehow know whether or not we need to dereference the pointer
1764 * on the left side - that is: OP_LOAD if it was an address.
1765 * Also: in original QC we cannot OP_LOADP *anyway*.
1767 *out = (lvalue ? left : right);
1772 bool ast_binary_codegen(ast_binary *self, ast_function *func, bool lvalue, ir_value **out)
1774 ast_expression_codegen *cgen;
1775 ir_value *left, *right;
1777 /* A binary operation cannot yield an l-value */
1779 compile_error(ast_ctx(self), "not an l-value (binop)");
1783 if (self->expression.outr) {
1784 *out = self->expression.outr;
1788 if ((OPTS_FLAG(SHORT_LOGIC) || OPTS_FLAG(PERL_LOGIC)) &&
1789 (self->op == INSTR_AND || self->op == INSTR_OR))
1791 /* short circuit evaluation */
1792 ir_block *other, *merge;
1793 ir_block *from_left, *from_right;
1797 /* prepare end-block */
1798 merge_id = vec_size(func->ir_func->blocks);
1799 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "sce_merge"));
1801 /* generate the left expression */
1802 cgen = self->left->expression.codegen;
1803 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1805 /* remember the block */
1806 from_left = func->curblock;
1808 /* create a new block for the right expression */
1809 other = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "sce_other"));
1810 if (self->op == INSTR_AND) {
1811 /* on AND: left==true -> other */
1812 if (!ir_block_create_if(func->curblock, ast_ctx(self), left, other, merge))
1815 /* on OR: left==false -> other */
1816 if (!ir_block_create_if(func->curblock, ast_ctx(self), left, merge, other))
1819 /* use the likely flag */
1820 vec_last(func->curblock->instr)->likely = true;
1822 /* enter the right-expression's block */
1823 func->curblock = other;
1825 cgen = self->right->expression.codegen;
1826 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1828 /* remember block */
1829 from_right = func->curblock;
1831 /* jump to the merge block */
1832 if (!ir_block_create_jump(func->curblock, ast_ctx(self), merge))
1835 vec_remove(func->ir_func->blocks, merge_id, 1);
1836 vec_push(func->ir_func->blocks, merge);
1838 func->curblock = merge;
1839 phi = ir_block_create_phi(func->curblock, ast_ctx(self),
1840 ast_function_label(func, "sce_value"),
1841 self->expression.vtype);
1842 ir_phi_add(phi, from_left, left);
1843 ir_phi_add(phi, from_right, right);
1844 *out = ir_phi_value(phi);
1848 if (!OPTS_FLAG(PERL_LOGIC)) {
1850 if (OPTS_FLAG(CORRECT_LOGIC) && (*out)->vtype == TYPE_VECTOR) {
1851 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
1852 ast_function_label(func, "sce_bool_v"),
1856 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
1857 ast_function_label(func, "sce_bool"),
1862 else if (OPTS_FLAG(FALSE_EMPTY_STRINGS) && (*out)->vtype == TYPE_STRING) {
1863 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
1864 ast_function_label(func, "sce_bool_s"),
1868 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
1869 ast_function_label(func, "sce_bool"),
1875 *out = ir_block_create_binop(func->curblock, ast_ctx(self),
1876 ast_function_label(func, "sce_bool"),
1877 INSTR_AND, *out, *out);
1883 self->expression.outr = *out;
1884 codegen_output_type(self, *out);
1888 cgen = self->left->expression.codegen;
1889 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1892 cgen = self->right->expression.codegen;
1893 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1896 *out = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "bin"),
1897 self->op, left, right);
1900 self->expression.outr = *out;
1901 codegen_output_type(self, *out);
1906 bool ast_binstore_codegen(ast_binstore *self, ast_function *func, bool lvalue, ir_value **out)
1908 ast_expression_codegen *cgen;
1909 ir_value *leftl = NULL, *leftr, *right, *bin;
1913 ast_array_index *ai = NULL;
1914 ir_value *iridx = NULL;
1916 if (lvalue && self->expression.outl) {
1917 *out = self->expression.outl;
1921 if (!lvalue && self->expression.outr) {
1922 *out = self->expression.outr;
1926 if (ast_istype(self->dest, ast_array_index))
1929 ai = (ast_array_index*)self->dest;
1930 idx = (ast_value*)ai->index;
1932 if (ast_istype(ai->index, ast_value) && idx->hasvalue && idx->cvq == CV_CONST)
1936 /* for a binstore we need both an lvalue and an rvalue for the left side */
1937 /* rvalue of destination! */
1939 cgen = idx->expression.codegen;
1940 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1943 cgen = self->dest->expression.codegen;
1944 if (!(*cgen)((ast_expression*)(self->dest), func, false, &leftr))
1947 /* source as rvalue only */
1948 cgen = self->source->expression.codegen;
1949 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1952 /* now the binary */
1953 bin = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "binst"),
1954 self->opbin, leftr, right);
1955 self->expression.outr = bin;
1959 /* we need to call the setter */
1964 compile_error(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1968 arr = (ast_value*)ai->array;
1969 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1970 compile_error(ast_ctx(self), "value has no setter (%s)", arr->name);
1974 cgen = arr->setter->expression.codegen;
1975 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
1978 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "store"), funval, false);
1981 ir_call_param(call, iridx);
1982 ir_call_param(call, bin);
1983 self->expression.outr = bin;
1985 /* now store them */
1986 cgen = self->dest->expression.codegen;
1987 /* lvalue of destination */
1988 if (!(*cgen)((ast_expression*)(self->dest), func, true, &leftl))
1990 self->expression.outl = leftl;
1992 if (!ir_block_create_store_op(func->curblock, ast_ctx(self), self->opstore, leftl, bin))
1994 self->expression.outr = bin;
1997 /* Theoretically, an assinment returns its left side as an
1998 * lvalue, if we don't need an lvalue though, we return
1999 * the right side as an rvalue, otherwise we have to
2000 * somehow know whether or not we need to dereference the pointer
2001 * on the left side - that is: OP_LOAD if it was an address.
2002 * Also: in original QC we cannot OP_LOADP *anyway*.
2004 *out = (lvalue ? leftl : bin);
2009 bool ast_unary_codegen(ast_unary *self, ast_function *func, bool lvalue, ir_value **out)
2011 ast_expression_codegen *cgen;
2014 /* An unary operation cannot yield an l-value */
2016 compile_error(ast_ctx(self), "not an l-value (binop)");
2020 if (self->expression.outr) {
2021 *out = self->expression.outr;
2025 cgen = self->operand->expression.codegen;
2027 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
2030 *out = ir_block_create_unary(func->curblock, ast_ctx(self), ast_function_label(func, "unary"),
2034 self->expression.outr = *out;
2039 bool ast_return_codegen(ast_return *self, ast_function *func, bool lvalue, ir_value **out)
2041 ast_expression_codegen *cgen;
2046 /* In the context of a return operation, we don't actually return
2050 compile_error(ast_ctx(self), "return-expression is not an l-value");
2054 if (self->expression.outr) {
2055 compile_error(ast_ctx(self), "internal error: ast_return cannot be reused, it bears no result!");
2058 self->expression.outr = (ir_value*)1;
2060 if (self->operand) {
2061 cgen = self->operand->expression.codegen;
2063 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
2066 if (!ir_block_create_return(func->curblock, ast_ctx(self), operand))
2069 if (!ir_block_create_return(func->curblock, ast_ctx(self), NULL))
2076 bool ast_entfield_codegen(ast_entfield *self, ast_function *func, bool lvalue, ir_value **out)
2078 ast_expression_codegen *cgen;
2079 ir_value *ent, *field;
2081 /* This function needs to take the 'lvalue' flag into account!
2082 * As lvalue we provide a field-pointer, as rvalue we provide the
2086 if (lvalue && self->expression.outl) {
2087 *out = self->expression.outl;
2091 if (!lvalue && self->expression.outr) {
2092 *out = self->expression.outr;
2096 cgen = self->entity->expression.codegen;
2097 if (!(*cgen)((ast_expression*)(self->entity), func, false, &ent))
2100 cgen = self->field->expression.codegen;
2101 if (!(*cgen)((ast_expression*)(self->field), func, false, &field))
2106 *out = ir_block_create_fieldaddress(func->curblock, ast_ctx(self), ast_function_label(func, "efa"),
2109 *out = ir_block_create_load_from_ent(func->curblock, ast_ctx(self), ast_function_label(func, "efv"),
2110 ent, field, self->expression.vtype);
2111 /* Done AFTER error checking:
2112 codegen_output_type(self, *out);
2116 compile_error(ast_ctx(self), "failed to create %s instruction (output type %s)",
2117 (lvalue ? "ADDRESS" : "FIELD"),
2118 type_name[self->expression.vtype]);
2122 codegen_output_type(self, *out);
2125 self->expression.outl = *out;
2127 self->expression.outr = *out;
2129 /* Hm that should be it... */
2133 bool ast_member_codegen(ast_member *self, ast_function *func, bool lvalue, ir_value **out)
2135 ast_expression_codegen *cgen;
2138 /* in QC this is always an lvalue */
2139 if (lvalue && self->rvalue) {
2140 compile_error(ast_ctx(self), "not an l-value (member access)");
2143 if (self->expression.outl) {
2144 *out = self->expression.outl;
2148 cgen = self->owner->expression.codegen;
2149 if (!(*cgen)((ast_expression*)(self->owner), func, false, &vec))
2152 if (vec->vtype != TYPE_VECTOR &&
2153 !(vec->vtype == TYPE_FIELD && self->owner->expression.next->expression.vtype == TYPE_VECTOR))
2158 *out = ir_value_vector_member(vec, self->field);
2159 self->expression.outl = *out;
2161 return (*out != NULL);
2164 bool ast_array_index_codegen(ast_array_index *self, ast_function *func, bool lvalue, ir_value **out)
2169 if (!lvalue && self->expression.outr) {
2170 *out = self->expression.outr;
2172 if (lvalue && self->expression.outl) {
2173 *out = self->expression.outl;
2176 if (!ast_istype(self->array, ast_value)) {
2177 compile_error(ast_ctx(self), "array indexing this way is not supported");
2178 /* note this would actually be pointer indexing because the left side is
2179 * not an actual array but (hopefully) an indexable expression.
2180 * Once we get integer arithmetic, and GADDRESS/GSTORE/GLOAD instruction
2181 * support this path will be filled.
2186 arr = (ast_value*)self->array;
2187 idx = (ast_value*)self->index;
2189 if (!ast_istype(self->index, ast_value) || !idx->hasvalue || idx->cvq != CV_CONST) {
2190 /* Time to use accessor functions */
2191 ast_expression_codegen *cgen;
2192 ir_value *iridx, *funval;
2196 compile_error(ast_ctx(self), "(.2) array indexing here needs a compile-time constant");
2201 compile_error(ast_ctx(self), "value has no getter, don't know how to index it");
2205 cgen = self->index->expression.codegen;
2206 if (!(*cgen)((ast_expression*)(self->index), func, false, &iridx))
2209 cgen = arr->getter->expression.codegen;
2210 if (!(*cgen)((ast_expression*)(arr->getter), func, true, &funval))
2213 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "fetch"), funval, false);
2216 ir_call_param(call, iridx);
2218 *out = ir_call_value(call);
2219 self->expression.outr = *out;
2223 if (idx->expression.vtype == TYPE_FLOAT) {
2224 unsigned int arridx = idx->constval.vfloat;
2225 if (arridx >= self->array->expression.count)
2227 compile_error(ast_ctx(self), "array index out of bounds: %i", arridx);
2230 *out = arr->ir_values[arridx];
2232 else if (idx->expression.vtype == TYPE_INTEGER) {
2233 unsigned int arridx = idx->constval.vint;
2234 if (arridx >= self->array->expression.count)
2236 compile_error(ast_ctx(self), "array index out of bounds: %i", arridx);
2239 *out = arr->ir_values[arridx];
2242 compile_error(ast_ctx(self), "array indexing here needs an integer constant");
2248 bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_value **out)
2250 ast_expression_codegen *cgen;
2258 ir_block *ontrue_endblock = NULL;
2259 ir_block *onfalse_endblock = NULL;
2260 ir_block *merge = NULL;
2262 /* We don't output any value, thus also don't care about r/lvalue */
2266 if (self->expression.outr) {
2267 compile_error(ast_ctx(self), "internal error: ast_ifthen cannot be reused, it bears no result!");
2270 self->expression.outr = (ir_value*)1;
2272 /* generate the condition */
2273 cgen = self->cond->expression.codegen;
2274 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2276 /* update the block which will get the jump - because short-logic or ternaries may have changed this */
2277 cond = func->curblock;
2281 if (self->on_true) {
2282 /* create on-true block */
2283 ontrue = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "ontrue"));
2287 /* enter the block */
2288 func->curblock = ontrue;
2291 cgen = self->on_true->expression.codegen;
2292 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &dummy))
2295 /* we now need to work from the current endpoint */
2296 ontrue_endblock = func->curblock;
2301 if (self->on_false) {
2302 /* create on-false block */
2303 onfalse = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "onfalse"));
2307 /* enter the block */
2308 func->curblock = onfalse;
2311 cgen = self->on_false->expression.codegen;
2312 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &dummy))
2315 /* we now need to work from the current endpoint */
2316 onfalse_endblock = func->curblock;
2320 /* Merge block were they all merge in to */
2321 if (!ontrue || !onfalse || !ontrue_endblock->final || !onfalse_endblock->final)
2323 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "endif"));
2326 /* add jumps ot the merge block */
2327 if (ontrue && !ontrue_endblock->final && !ir_block_create_jump(ontrue_endblock, ast_ctx(self), merge))
2329 if (onfalse && !onfalse_endblock->final && !ir_block_create_jump(onfalse_endblock, ast_ctx(self), merge))
2332 /* Now enter the merge block */
2333 func->curblock = merge;
2336 /* we create the if here, that way all blocks are ordered :)
2338 if (!ir_block_create_if(cond, ast_ctx(self), condval,
2339 (ontrue ? ontrue : merge),
2340 (onfalse ? onfalse : merge)))
2348 bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_value **out)
2350 ast_expression_codegen *cgen;
2353 ir_value *trueval, *falseval;
2356 ir_block *cond = func->curblock;
2357 ir_block *cond_out = NULL;
2358 ir_block *ontrue, *ontrue_out = NULL;
2359 ir_block *onfalse, *onfalse_out = NULL;
2362 /* Ternary can never create an lvalue... */
2366 /* In theory it shouldn't be possible to pass through a node twice, but
2367 * in case we add any kind of optimization pass for the AST itself, it
2368 * may still happen, thus we remember a created ir_value and simply return one
2369 * if it already exists.
2371 if (self->expression.outr) {
2372 *out = self->expression.outr;
2376 /* In the following, contraty to ast_ifthen, we assume both paths exist. */
2378 /* generate the condition */
2379 func->curblock = cond;
2380 cgen = self->cond->expression.codegen;
2381 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2383 cond_out = func->curblock;
2385 /* create on-true block */
2386 ontrue = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_T"));
2391 /* enter the block */
2392 func->curblock = ontrue;
2395 cgen = self->on_true->expression.codegen;
2396 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &trueval))
2399 ontrue_out = func->curblock;
2402 /* create on-false block */
2403 onfalse = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_F"));
2408 /* enter the block */
2409 func->curblock = onfalse;
2412 cgen = self->on_false->expression.codegen;
2413 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &falseval))
2416 onfalse_out = func->curblock;
2419 /* create merge block */
2420 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_out"));
2423 /* jump to merge block */
2424 if (!ir_block_create_jump(ontrue_out, ast_ctx(self), merge))
2426 if (!ir_block_create_jump(onfalse_out, ast_ctx(self), merge))
2429 /* create if instruction */
2430 if (!ir_block_create_if(cond_out, ast_ctx(self), condval, ontrue, onfalse))
2433 /* Now enter the merge block */
2434 func->curblock = merge;
2436 /* Here, now, we need a PHI node
2437 * but first some sanity checking...
2439 if (trueval->vtype != falseval->vtype && trueval->vtype != TYPE_NIL && falseval->vtype != TYPE_NIL) {
2440 /* error("ternary with different types on the two sides"); */
2441 compile_error(ast_ctx(self), "internal error: ternary operand types invalid");
2446 phi = ir_block_create_phi(merge, ast_ctx(self), ast_function_label(func, "phi"), self->expression.vtype);
2448 compile_error(ast_ctx(self), "internal error: failed to generate phi node");
2451 ir_phi_add(phi, ontrue_out, trueval);
2452 ir_phi_add(phi, onfalse_out, falseval);
2454 self->expression.outr = ir_phi_value(phi);
2455 *out = self->expression.outr;
2457 codegen_output_type(self, *out);
2462 bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value **out)
2464 ast_expression_codegen *cgen;
2466 ir_value *dummy = NULL;
2467 ir_value *precond = NULL;
2468 ir_value *postcond = NULL;
2470 /* Since we insert some jumps "late" so we have blocks
2471 * ordered "nicely", we need to keep track of the actual end-blocks
2472 * of expressions to add the jumps to.
2474 ir_block *bbody = NULL, *end_bbody = NULL;
2475 ir_block *bprecond = NULL, *end_bprecond = NULL;
2476 ir_block *bpostcond = NULL, *end_bpostcond = NULL;
2477 ir_block *bincrement = NULL, *end_bincrement = NULL;
2478 ir_block *bout = NULL, *bin = NULL;
2480 /* let's at least move the outgoing block to the end */
2483 /* 'break' and 'continue' need to be able to find the right blocks */
2484 ir_block *bcontinue = NULL;
2485 ir_block *bbreak = NULL;
2487 ir_block *tmpblock = NULL;
2492 if (self->expression.outr) {
2493 compile_error(ast_ctx(self), "internal error: ast_loop cannot be reused, it bears no result!");
2496 self->expression.outr = (ir_value*)1;
2499 * Should we ever need some kind of block ordering, better make this function
2500 * move blocks around than write a block ordering algorithm later... after all
2501 * the ast and ir should work together, not against each other.
2504 /* initexpr doesn't get its own block, it's pointless, it could create more blocks
2505 * anyway if for example it contains a ternary.
2509 cgen = self->initexpr->expression.codegen;
2510 if (!(*cgen)((ast_expression*)(self->initexpr), func, false, &dummy))
2514 /* Store the block from which we enter this chaos */
2515 bin = func->curblock;
2517 /* The pre-loop condition needs its own block since we
2518 * need to be able to jump to the start of that expression.
2522 bprecond = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "pre_loop_cond"));
2526 /* the pre-loop-condition the least important place to 'continue' at */
2527 bcontinue = bprecond;
2530 func->curblock = bprecond;
2533 cgen = self->precond->expression.codegen;
2534 if (!(*cgen)((ast_expression*)(self->precond), func, false, &precond))
2537 end_bprecond = func->curblock;
2539 bprecond = end_bprecond = NULL;
2542 /* Now the next blocks won't be ordered nicely, but we need to
2543 * generate them this early for 'break' and 'continue'.
2545 if (self->increment) {
2546 bincrement = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "loop_increment"));
2549 bcontinue = bincrement; /* increment comes before the pre-loop-condition */
2551 bincrement = end_bincrement = NULL;
2554 if (self->postcond) {
2555 bpostcond = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "post_loop_cond"));
2558 bcontinue = bpostcond; /* postcond comes before the increment */
2560 bpostcond = end_bpostcond = NULL;
2563 bout_id = vec_size(func->ir_func->blocks);
2564 bout = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "after_loop"));
2569 /* The loop body... */
2570 /* if (self->body) */
2572 bbody = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "loop_body"));
2577 func->curblock = bbody;
2579 vec_push(func->breakblocks, bbreak);
2581 vec_push(func->continueblocks, bcontinue);
2583 vec_push(func->continueblocks, bbody);
2587 cgen = self->body->expression.codegen;
2588 if (!(*cgen)((ast_expression*)(self->body), func, false, &dummy))
2592 end_bbody = func->curblock;
2593 vec_pop(func->breakblocks);
2594 vec_pop(func->continueblocks);
2597 /* post-loop-condition */
2601 func->curblock = bpostcond;
2604 cgen = self->postcond->expression.codegen;
2605 if (!(*cgen)((ast_expression*)(self->postcond), func, false, &postcond))
2608 end_bpostcond = func->curblock;
2611 /* The incrementor */
2612 if (self->increment)
2615 func->curblock = bincrement;
2618 cgen = self->increment->expression.codegen;
2619 if (!(*cgen)((ast_expression*)(self->increment), func, false, &dummy))
2622 end_bincrement = func->curblock;
2625 /* In any case now, we continue from the outgoing block */
2626 func->curblock = bout;
2628 /* Now all blocks are in place */
2629 /* From 'bin' we jump to whatever comes first */
2630 if (bprecond) tmpblock = bprecond;
2631 else if (bbody) tmpblock = bbody;
2632 else if (bpostcond) tmpblock = bpostcond;
2633 else tmpblock = bout;
2634 if (!ir_block_create_jump(bin, ast_ctx(self), tmpblock))
2640 ir_block *ontrue, *onfalse;
2641 if (bbody) ontrue = bbody;
2642 else if (bincrement) ontrue = bincrement;
2643 else if (bpostcond) ontrue = bpostcond;
2644 else ontrue = bprecond;
2646 if (self->pre_not) {
2651 if (!ir_block_create_if(end_bprecond, ast_ctx(self), precond, ontrue, onfalse))
2658 if (bincrement) tmpblock = bincrement;
2659 else if (bpostcond) tmpblock = bpostcond;
2660 else if (bprecond) tmpblock = bprecond;
2661 else tmpblock = bbody;
2662 if (!end_bbody->final && !ir_block_create_jump(end_bbody, ast_ctx(self), tmpblock))
2666 /* from increment */
2669 if (bpostcond) tmpblock = bpostcond;
2670 else if (bprecond) tmpblock = bprecond;
2671 else if (bbody) tmpblock = bbody;
2672 else tmpblock = bout;
2673 if (!ir_block_create_jump(end_bincrement, ast_ctx(self), tmpblock))
2680 ir_block *ontrue, *onfalse;
2681 if (bprecond) ontrue = bprecond;
2682 else if (bbody) ontrue = bbody;
2683 else if (bincrement) ontrue = bincrement;
2684 else ontrue = bpostcond;
2686 if (self->post_not) {
2691 if (!ir_block_create_if(end_bpostcond, ast_ctx(self), postcond, ontrue, onfalse))
2695 /* Move 'bout' to the end */
2696 vec_remove(func->ir_func->blocks, bout_id, 1);
2697 vec_push(func->ir_func->blocks, bout);
2702 bool ast_breakcont_codegen(ast_breakcont *self, ast_function *func, bool lvalue, ir_value **out)
2709 compile_error(ast_ctx(self), "break/continue expression is not an l-value");
2713 if (self->expression.outr) {
2714 compile_error(ast_ctx(self), "internal error: ast_breakcont cannot be reused!");
2717 self->expression.outr = (ir_value*)1;
2719 if (self->is_continue)
2720 target = func->continueblocks[vec_size(func->continueblocks)-1-self->levels];
2722 target = func->breakblocks[vec_size(func->breakblocks)-1-self->levels];
2725 compile_error(ast_ctx(self), "%s is lacking a target block", (self->is_continue ? "continue" : "break"));
2729 if (!ir_block_create_jump(func->curblock, ast_ctx(self), target))
2734 bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_value **out)
2736 ast_expression_codegen *cgen;
2738 ast_switch_case *def_case = NULL;
2739 ir_block *def_bfall = NULL;
2740 ir_block *def_bfall_to = NULL;
2741 bool set_def_bfall_to = false;
2743 ir_value *dummy = NULL;
2744 ir_value *irop = NULL;
2745 ir_block *bout = NULL;
2746 ir_block *bfall = NULL;
2754 compile_error(ast_ctx(self), "switch expression is not an l-value");
2758 if (self->expression.outr) {
2759 compile_error(ast_ctx(self), "internal error: ast_switch cannot be reused!");
2762 self->expression.outr = (ir_value*)1;
2767 cgen = self->operand->expression.codegen;
2768 if (!(*cgen)((ast_expression*)(self->operand), func, false, &irop))
2771 if (!vec_size(self->cases))
2774 cmpinstr = type_eq_instr[irop->vtype];
2775 if (cmpinstr >= AINSTR_END) {
2776 ast_type_to_string(self->operand, typestr, sizeof(typestr));
2777 compile_error(ast_ctx(self), "invalid type to perform a switch on: %s", typestr);
2781 bout_id = vec_size(func->ir_func->blocks);
2782 bout = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "after_switch"));
2786 /* setup the break block */
2787 vec_push(func->breakblocks, bout);
2789 /* Now create all cases */
2790 for (c = 0; c < vec_size(self->cases); ++c) {
2791 ir_value *cond, *val;
2792 ir_block *bcase, *bnot;
2795 ast_switch_case *swcase = &self->cases[c];
2797 if (swcase->value) {
2798 /* A regular case */
2799 /* generate the condition operand */
2800 cgen = swcase->value->expression.codegen;
2801 if (!(*cgen)((ast_expression*)(swcase->value), func, false, &val))
2803 /* generate the condition */
2804 cond = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "switch_eq"), cmpinstr, irop, val);
2808 bcase = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "case"));
2809 bnot_id = vec_size(func->ir_func->blocks);
2810 bnot = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "not_case"));
2811 if (!bcase || !bnot)
2813 if (set_def_bfall_to) {
2814 set_def_bfall_to = false;
2815 def_bfall_to = bcase;
2817 if (!ir_block_create_if(func->curblock, ast_ctx(self), cond, bcase, bnot))
2820 /* Make the previous case-end fall through */
2821 if (bfall && !bfall->final) {
2822 if (!ir_block_create_jump(bfall, ast_ctx(self), bcase))
2826 /* enter the case */
2827 func->curblock = bcase;
2828 cgen = swcase->code->expression.codegen;
2829 if (!(*cgen)((ast_expression*)swcase->code, func, false, &dummy))
2832 /* remember this block to fall through from */
2833 bfall = func->curblock;
2835 /* enter the else and move it down */
2836 func->curblock = bnot;
2837 vec_remove(func->ir_func->blocks, bnot_id, 1);
2838 vec_push(func->ir_func->blocks, bnot);
2840 /* The default case */
2841 /* Remember where to fall through from: */
2844 /* remember which case it was */
2846 /* And the next case will be remembered */
2847 set_def_bfall_to = true;
2851 /* Jump from the last bnot to bout */
2852 if (bfall && !bfall->final && !ir_block_create_jump(bfall, ast_ctx(self), bout)) {
2854 astwarning(ast_ctx(bfall), WARN_???, "missing break after last case");
2859 /* If there was a default case, put it down here */
2863 /* No need to create an extra block */
2864 bcase = func->curblock;
2866 /* Insert the fallthrough jump */
2867 if (def_bfall && !def_bfall->final) {
2868 if (!ir_block_create_jump(def_bfall, ast_ctx(self), bcase))
2872 /* Now generate the default code */
2873 cgen = def_case->code->expression.codegen;
2874 if (!(*cgen)((ast_expression*)def_case->code, func, false, &dummy))
2877 /* see if we need to fall through */
2878 if (def_bfall_to && !func->curblock->final)
2880 if (!ir_block_create_jump(func->curblock, ast_ctx(self), def_bfall_to))
2885 /* Jump from the last bnot to bout */
2886 if (!func->curblock->final && !ir_block_create_jump(func->curblock, ast_ctx(self), bout))
2888 /* enter the outgoing block */
2889 func->curblock = bout;
2891 /* restore the break block */
2892 vec_pop(func->breakblocks);
2894 /* Move 'bout' to the end, it's nicer */
2895 vec_remove(func->ir_func->blocks, bout_id, 1);
2896 vec_push(func->ir_func->blocks, bout);
2901 bool ast_label_codegen(ast_label *self, ast_function *func, bool lvalue, ir_value **out)
2906 if (self->undefined) {
2907 compile_error(ast_ctx(self), "internal error: ast_label never defined");
2913 compile_error(ast_ctx(self), "internal error: ast_label cannot be an lvalue");
2917 /* simply create a new block and jump to it */
2918 self->irblock = ir_function_create_block(ast_ctx(self), func->ir_func, self->name);
2919 if (!self->irblock) {
2920 compile_error(ast_ctx(self), "failed to allocate label block `%s`", self->name);
2923 if (!func->curblock->final) {
2924 if (!ir_block_create_jump(func->curblock, ast_ctx(self), self->irblock))
2928 /* enter the new block */
2929 func->curblock = self->irblock;
2931 /* Generate all the leftover gotos */
2932 for (i = 0; i < vec_size(self->gotos); ++i) {
2933 if (!ast_goto_codegen(self->gotos[i], func, false, &dummy))
2940 bool ast_goto_codegen(ast_goto *self, ast_function *func, bool lvalue, ir_value **out)
2944 compile_error(ast_ctx(self), "internal error: ast_goto cannot be an lvalue");
2948 if (self->target->irblock) {
2949 if (self->irblock_from) {
2950 /* we already tried once, this is the callback */
2951 self->irblock_from->final = false;
2952 if (!ir_block_create_goto(self->irblock_from, ast_ctx(self), self->target->irblock)) {
2953 compile_error(ast_ctx(self), "failed to generate goto to `%s`", self->name);
2959 if (!ir_block_create_goto(func->curblock, ast_ctx(self), self->target->irblock)) {
2960 compile_error(ast_ctx(self), "failed to generate goto to `%s`", self->name);
2967 /* the target has not yet been created...
2968 * close this block in a sneaky way:
2970 func->curblock->final = true;
2971 self->irblock_from = func->curblock;
2972 ast_label_register_goto(self->target, self);
2978 bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value **out)
2980 ast_expression_codegen *cgen;
2982 ir_instr *callinstr;
2985 ir_value *funval = NULL;
2987 /* return values are never lvalues */
2989 compile_error(ast_ctx(self), "not an l-value (function call)");
2993 if (self->expression.outr) {
2994 *out = self->expression.outr;
2998 cgen = self->func->expression.codegen;
2999 if (!(*cgen)((ast_expression*)(self->func), func, false, &funval))
3007 for (i = 0; i < vec_size(self->params); ++i)
3010 ast_expression *expr = self->params[i];
3012 cgen = expr->expression.codegen;
3013 if (!(*cgen)(expr, func, false, ¶m))
3017 vec_push(params, param);
3020 callinstr = ir_block_create_call(func->curblock, ast_ctx(self),
3021 ast_function_label(func, "call"),
3022 funval, !!(self->func->expression.flags & AST_FLAG_NORETURN));
3026 for (i = 0; i < vec_size(params); ++i) {
3027 ir_call_param(callinstr, params[i]);
3030 *out = ir_call_value(callinstr);
3031 self->expression.outr = *out;
3033 codegen_output_type(self, *out);