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);
90 if (self->expression.varparam)
91 ast_delete(self->expression.varparam);
94 static void ast_expression_delete_full(ast_expression *self)
96 ast_expression_delete(self);
100 ast_value* ast_value_copy(const ast_value *self)
103 const ast_expression_common *fromex;
104 ast_expression_common *selfex;
105 ast_value *cp = ast_value_new(self->expression.node.context, self->name, self->expression.vtype);
106 if (self->expression.next) {
107 cp->expression.next = ast_type_copy(self->expression.node.context, self->expression.next);
109 fromex = &self->expression;
110 selfex = &cp->expression;
111 selfex->count = fromex->count;
112 selfex->flags = fromex->flags;
113 for (i = 0; i < vec_size(fromex->params); ++i) {
114 ast_value *v = ast_value_copy(fromex->params[i]);
115 vec_push(selfex->params, v);
120 void ast_type_adopt_impl(ast_expression *self, const ast_expression *other)
123 const ast_expression_common *fromex;
124 ast_expression_common *selfex;
125 self->expression.vtype = other->expression.vtype;
126 if (other->expression.next) {
127 self->expression.next = (ast_expression*)ast_type_copy(ast_ctx(self), other->expression.next);
129 fromex = &other->expression;
130 selfex = &self->expression;
131 selfex->count = fromex->count;
132 selfex->flags = fromex->flags;
133 for (i = 0; i < vec_size(fromex->params); ++i) {
134 ast_value *v = ast_value_copy(fromex->params[i]);
135 vec_push(selfex->params, v);
139 static ast_expression* ast_shallow_type(lex_ctx ctx, int vtype)
141 ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
142 ast_expression_init(self, NULL);
143 self->expression.codegen = NULL;
144 self->expression.next = NULL;
145 self->expression.vtype = vtype;
149 ast_expression* ast_type_copy(lex_ctx ctx, const ast_expression *ex)
152 const ast_expression_common *fromex;
153 ast_expression_common *selfex;
159 ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
160 ast_expression_init(self, NULL);
162 fromex = &ex->expression;
163 selfex = &self->expression;
165 /* This may never be codegen()d */
166 selfex->codegen = NULL;
168 selfex->vtype = fromex->vtype;
170 selfex->next = ast_type_copy(ctx, fromex->next);
174 selfex->count = fromex->count;
175 selfex->flags = fromex->flags;
176 for (i = 0; i < vec_size(fromex->params); ++i) {
177 ast_value *v = ast_value_copy(fromex->params[i]);
178 vec_push(selfex->params, v);
185 bool ast_compare_type(ast_expression *a, ast_expression *b)
187 if (a->expression.vtype == TYPE_NIL ||
188 b->expression.vtype == TYPE_NIL)
190 if (a->expression.vtype != b->expression.vtype)
192 if (!a->expression.next != !b->expression.next)
194 if (vec_size(a->expression.params) != vec_size(b->expression.params))
196 if ((a->expression.flags & AST_FLAG_TYPE_MASK) !=
197 (b->expression.flags & AST_FLAG_TYPE_MASK) )
201 if (vec_size(a->expression.params)) {
203 for (i = 0; i < vec_size(a->expression.params); ++i) {
204 if (!ast_compare_type((ast_expression*)a->expression.params[i],
205 (ast_expression*)b->expression.params[i]))
209 if (a->expression.next)
210 return ast_compare_type(a->expression.next, b->expression.next);
214 static size_t ast_type_to_string_impl(ast_expression *e, char *buf, size_t bufsize, size_t pos)
221 if (pos + 6 >= bufsize)
223 strncpy(buf + pos, "(null)", 6);
227 if (pos + 1 >= bufsize)
230 switch (e->expression.vtype) {
232 strncpy(buf + pos, "(variant)", 9);
237 return ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
240 if (pos + 3 >= bufsize)
244 pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
245 if (pos + 1 >= bufsize)
251 pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
252 if (pos + 2 >= bufsize)
254 if (!vec_size(e->expression.params)) {
260 pos = ast_type_to_string_impl((ast_expression*)(e->expression.params[0]), buf, bufsize, pos);
261 for (i = 1; i < vec_size(e->expression.params); ++i) {
262 if (pos + 2 >= bufsize)
266 pos = ast_type_to_string_impl((ast_expression*)(e->expression.params[i]), buf, bufsize, pos);
268 if (pos + 1 >= bufsize)
274 pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
275 if (pos + 1 >= bufsize)
278 pos += snprintf(buf + pos, bufsize - pos - 1, "%i", (int)e->expression.count);
279 if (pos + 1 >= bufsize)
285 typestr = type_name[e->expression.vtype];
286 typelen = strlen(typestr);
287 if (pos + typelen >= bufsize)
289 strncpy(buf + pos, typestr, typelen);
290 return pos + typelen;
294 buf[bufsize-3] = '.';
295 buf[bufsize-2] = '.';
296 buf[bufsize-1] = '.';
300 void ast_type_to_string(ast_expression *e, char *buf, size_t bufsize)
302 size_t pos = ast_type_to_string_impl(e, buf, bufsize-1, 0);
306 ast_value* ast_value_new(lex_ctx ctx, const char *name, int t)
308 ast_instantiate(ast_value, ctx, ast_value_delete);
309 ast_expression_init((ast_expression*)self,
310 (ast_expression_codegen*)&ast_value_codegen);
311 self->expression.node.keep = true; /* keep */
313 self->name = name ? util_strdup(name) : NULL;
314 self->expression.vtype = t;
315 self->expression.next = NULL;
316 self->isfield = false;
318 self->hasvalue = false;
320 memset(&self->constval, 0, sizeof(self->constval));
323 self->ir_values = NULL;
324 self->ir_value_count = 0;
330 self->argcounter = NULL;
335 void ast_value_delete(ast_value* self)
338 mem_d((void*)self->name);
339 if (self->argcounter)
340 mem_d((void*)self->argcounter);
341 if (self->hasvalue) {
342 switch (self->expression.vtype)
345 mem_d((void*)self->constval.vstring);
348 /* unlink us from the function node */
349 self->constval.vfunc->vtype = NULL;
351 /* NOTE: delete function? currently collected in
352 * the parser structure
359 mem_d(self->ir_values);
364 ast_expression_delete((ast_expression*)self);
368 void ast_value_params_add(ast_value *self, ast_value *p)
370 vec_push(self->expression.params, p);
373 bool ast_value_set_name(ast_value *self, const char *name)
376 mem_d((void*)self->name);
377 self->name = util_strdup(name);
381 ast_binary* ast_binary_new(lex_ctx ctx, int op,
382 ast_expression* left, ast_expression* right)
384 ast_instantiate(ast_binary, ctx, ast_binary_delete);
385 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binary_codegen);
391 ast_propagate_effects(self, left);
392 ast_propagate_effects(self, right);
394 if (op >= INSTR_EQ_F && op <= INSTR_GT)
395 self->expression.vtype = TYPE_FLOAT;
396 else if (op == INSTR_AND || op == INSTR_OR) {
397 if (OPTS_FLAG(PERL_LOGIC))
398 ast_type_adopt(self, right);
400 self->expression.vtype = TYPE_FLOAT;
402 else if (op == INSTR_BITAND || op == INSTR_BITOR)
403 self->expression.vtype = TYPE_FLOAT;
404 else if (op == INSTR_MUL_VF || op == INSTR_MUL_FV)
405 self->expression.vtype = TYPE_VECTOR;
406 else if (op == INSTR_MUL_V)
407 self->expression.vtype = TYPE_FLOAT;
409 self->expression.vtype = left->expression.vtype;
412 self->refs = AST_REF_ALL;
417 void ast_binary_delete(ast_binary *self)
419 if (self->refs & AST_REF_LEFT) ast_unref(self->left);
420 if (self->refs & AST_REF_RIGHT) ast_unref(self->right);
422 ast_expression_delete((ast_expression*)self);
426 ast_binstore* ast_binstore_new(lex_ctx ctx, int storop, int op,
427 ast_expression* left, ast_expression* right)
429 ast_instantiate(ast_binstore, ctx, ast_binstore_delete);
430 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binstore_codegen);
432 ast_side_effects(self) = true;
434 self->opstore = storop;
437 self->source = right;
439 self->keep_dest = false;
441 ast_type_adopt(self, left);
445 void ast_binstore_delete(ast_binstore *self)
447 if (!self->keep_dest)
448 ast_unref(self->dest);
449 ast_unref(self->source);
450 ast_expression_delete((ast_expression*)self);
454 ast_unary* ast_unary_new(lex_ctx ctx, int op,
455 ast_expression *expr)
457 ast_instantiate(ast_unary, ctx, ast_unary_delete);
458 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_unary_codegen);
461 self->operand = expr;
463 ast_propagate_effects(self, expr);
465 if (op >= INSTR_NOT_F && op <= INSTR_NOT_FNC) {
466 self->expression.vtype = TYPE_FLOAT;
468 compile_error(ctx, "cannot determine type of unary operation %s", asm_instr[op].m);
473 void ast_unary_delete(ast_unary *self)
475 if (self->operand) ast_unref(self->operand);
476 ast_expression_delete((ast_expression*)self);
480 ast_return* ast_return_new(lex_ctx ctx, ast_expression *expr)
482 ast_instantiate(ast_return, ctx, ast_return_delete);
483 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_return_codegen);
485 self->operand = expr;
488 ast_propagate_effects(self, expr);
493 void ast_return_delete(ast_return *self)
496 ast_unref(self->operand);
497 ast_expression_delete((ast_expression*)self);
501 ast_entfield* ast_entfield_new(lex_ctx ctx, ast_expression *entity, ast_expression *field)
503 if (field->expression.vtype != TYPE_FIELD) {
504 compile_error(ctx, "ast_entfield_new with expression not of type field");
507 return ast_entfield_new_force(ctx, entity, field, field->expression.next);
510 ast_entfield* ast_entfield_new_force(lex_ctx ctx, ast_expression *entity, ast_expression *field, const ast_expression *outtype)
512 ast_instantiate(ast_entfield, ctx, ast_entfield_delete);
516 /* Error: field has no type... */
520 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_entfield_codegen);
522 self->entity = entity;
524 ast_propagate_effects(self, entity);
525 ast_propagate_effects(self, field);
527 ast_type_adopt(self, outtype);
531 void ast_entfield_delete(ast_entfield *self)
533 ast_unref(self->entity);
534 ast_unref(self->field);
535 ast_expression_delete((ast_expression*)self);
539 ast_member* ast_member_new(lex_ctx ctx, ast_expression *owner, unsigned int field, const char *name)
541 ast_instantiate(ast_member, ctx, ast_member_delete);
547 if (owner->expression.vtype != TYPE_VECTOR &&
548 owner->expression.vtype != TYPE_FIELD) {
549 compile_error(ctx, "member-access on an invalid owner of type %s", type_name[owner->expression.vtype]);
554 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_member_codegen);
555 self->expression.node.keep = true; /* keep */
557 if (owner->expression.vtype == TYPE_VECTOR) {
558 self->expression.vtype = TYPE_FLOAT;
559 self->expression.next = NULL;
561 self->expression.vtype = TYPE_FIELD;
562 self->expression.next = ast_shallow_type(ctx, TYPE_FLOAT);
565 self->rvalue = false;
567 ast_propagate_effects(self, owner);
571 self->name = util_strdup(name);
578 void ast_member_delete(ast_member *self)
580 /* The owner is always an ast_value, which has .keep=true,
581 * also: ast_members are usually deleted after the owner, thus
582 * this will cause invalid access
583 ast_unref(self->owner);
584 * once we allow (expression).x to access a vector-member, we need
585 * to change this: preferably by creating an alternate ast node for this
586 * purpose that is not garbage-collected.
588 ast_expression_delete((ast_expression*)self);
593 bool ast_member_set_name(ast_member *self, const char *name)
596 mem_d((void*)self->name);
597 self->name = util_strdup(name);
601 ast_array_index* ast_array_index_new(lex_ctx ctx, ast_expression *array, ast_expression *index)
603 ast_expression *outtype;
604 ast_instantiate(ast_array_index, ctx, ast_array_index_delete);
606 outtype = array->expression.next;
609 /* Error: field has no type... */
613 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_array_index_codegen);
617 ast_propagate_effects(self, array);
618 ast_propagate_effects(self, index);
620 ast_type_adopt(self, outtype);
621 if (array->expression.vtype == TYPE_FIELD && outtype->expression.vtype == TYPE_ARRAY) {
622 if (self->expression.vtype != TYPE_ARRAY) {
623 compile_error(ast_ctx(self), "array_index node on type");
624 ast_array_index_delete(self);
627 self->array = outtype;
628 self->expression.vtype = TYPE_FIELD;
634 void ast_array_index_delete(ast_array_index *self)
637 ast_unref(self->array);
639 ast_unref(self->index);
640 ast_expression_delete((ast_expression*)self);
644 ast_ifthen* ast_ifthen_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
646 ast_instantiate(ast_ifthen, ctx, ast_ifthen_delete);
647 if (!ontrue && !onfalse) {
648 /* because it is invalid */
652 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ifthen_codegen);
655 self->on_true = ontrue;
656 self->on_false = onfalse;
657 ast_propagate_effects(self, cond);
659 ast_propagate_effects(self, ontrue);
661 ast_propagate_effects(self, onfalse);
666 void ast_ifthen_delete(ast_ifthen *self)
668 ast_unref(self->cond);
670 ast_unref(self->on_true);
672 ast_unref(self->on_false);
673 ast_expression_delete((ast_expression*)self);
677 ast_ternary* ast_ternary_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
679 ast_expression *exprtype = ontrue;
680 ast_instantiate(ast_ternary, ctx, ast_ternary_delete);
681 /* This time NEITHER must be NULL */
682 if (!ontrue || !onfalse) {
686 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ternary_codegen);
689 self->on_true = ontrue;
690 self->on_false = onfalse;
691 ast_propagate_effects(self, cond);
692 ast_propagate_effects(self, ontrue);
693 ast_propagate_effects(self, onfalse);
695 if (ontrue->expression.vtype == TYPE_NIL)
697 ast_type_adopt(self, exprtype);
702 void ast_ternary_delete(ast_ternary *self)
704 /* the if()s are only there because computed-gotos can set them
707 if (self->cond) ast_unref(self->cond);
708 if (self->on_true) ast_unref(self->on_true);
709 if (self->on_false) ast_unref(self->on_false);
710 ast_expression_delete((ast_expression*)self);
714 ast_loop* ast_loop_new(lex_ctx ctx,
715 ast_expression *initexpr,
716 ast_expression *precond, bool pre_not,
717 ast_expression *postcond, bool post_not,
718 ast_expression *increment,
719 ast_expression *body)
721 ast_instantiate(ast_loop, ctx, ast_loop_delete);
722 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_loop_codegen);
724 self->initexpr = initexpr;
725 self->precond = precond;
726 self->postcond = postcond;
727 self->increment = increment;
730 self->pre_not = pre_not;
731 self->post_not = post_not;
734 ast_propagate_effects(self, initexpr);
736 ast_propagate_effects(self, precond);
738 ast_propagate_effects(self, postcond);
740 ast_propagate_effects(self, increment);
742 ast_propagate_effects(self, body);
747 void ast_loop_delete(ast_loop *self)
750 ast_unref(self->initexpr);
752 ast_unref(self->precond);
754 ast_unref(self->postcond);
756 ast_unref(self->increment);
758 ast_unref(self->body);
759 ast_expression_delete((ast_expression*)self);
763 ast_breakcont* ast_breakcont_new(lex_ctx ctx, bool iscont, unsigned int levels)
765 ast_instantiate(ast_breakcont, ctx, ast_breakcont_delete);
766 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_breakcont_codegen);
768 self->is_continue = iscont;
769 self->levels = levels;
774 void ast_breakcont_delete(ast_breakcont *self)
776 ast_expression_delete((ast_expression*)self);
780 ast_switch* ast_switch_new(lex_ctx ctx, ast_expression *op)
782 ast_instantiate(ast_switch, ctx, ast_switch_delete);
783 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_switch_codegen);
788 ast_propagate_effects(self, op);
793 void ast_switch_delete(ast_switch *self)
796 ast_unref(self->operand);
798 for (i = 0; i < vec_size(self->cases); ++i) {
799 if (self->cases[i].value)
800 ast_unref(self->cases[i].value);
801 ast_unref(self->cases[i].code);
803 vec_free(self->cases);
805 ast_expression_delete((ast_expression*)self);
809 ast_label* ast_label_new(lex_ctx ctx, const char *name, bool undefined)
811 ast_instantiate(ast_label, ctx, ast_label_delete);
812 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_label_codegen);
814 self->expression.vtype = TYPE_NOEXPR;
816 self->name = util_strdup(name);
817 self->irblock = NULL;
819 self->undefined = undefined;
824 void ast_label_delete(ast_label *self)
826 mem_d((void*)self->name);
827 vec_free(self->gotos);
828 ast_expression_delete((ast_expression*)self);
832 void ast_label_register_goto(ast_label *self, ast_goto *g)
834 vec_push(self->gotos, g);
837 ast_goto* ast_goto_new(lex_ctx ctx, const char *name)
839 ast_instantiate(ast_goto, ctx, ast_goto_delete);
840 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_goto_codegen);
842 self->name = util_strdup(name);
844 self->irblock_from = NULL;
849 void ast_goto_delete(ast_goto *self)
851 mem_d((void*)self->name);
852 ast_expression_delete((ast_expression*)self);
856 void ast_goto_set_label(ast_goto *self, ast_label *label)
858 self->target = label;
861 ast_call* ast_call_new(lex_ctx ctx,
862 ast_expression *funcexpr)
864 ast_instantiate(ast_call, ctx, ast_call_delete);
865 if (!funcexpr->expression.next) {
866 compile_error(ctx, "not a function");
870 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_call_codegen);
872 ast_side_effects(self) = true;
875 self->func = funcexpr;
876 self->va_count = NULL;
878 ast_type_adopt(self, funcexpr->expression.next);
883 void ast_call_delete(ast_call *self)
886 for (i = 0; i < vec_size(self->params); ++i)
887 ast_unref(self->params[i]);
888 vec_free(self->params);
891 ast_unref(self->func);
894 ast_unref(self->va_count);
896 ast_expression_delete((ast_expression*)self);
900 bool ast_call_check_types(ast_call *self)
906 const ast_expression *func = self->func;
907 size_t count = vec_size(self->params);
908 if (count > vec_size(func->expression.params))
909 count = vec_size(func->expression.params);
911 for (i = 0; i < count; ++i) {
912 if (!ast_compare_type(self->params[i], (ast_expression*)(func->expression.params[i])))
914 ast_type_to_string(self->params[i], tgot, sizeof(tgot));
915 ast_type_to_string((ast_expression*)func->expression.params[i], texp, sizeof(texp));
916 compile_error(ast_ctx(self), "invalid type for parameter %u in function call: expected %s, got %s",
917 (unsigned int)(i+1), texp, tgot);
918 /* we don't immediately return */
922 count = vec_size(self->params);
923 if (count > vec_size(func->expression.params) && func->expression.varparam) {
924 for (; i < count; ++i) {
925 if (!ast_compare_type(self->params[i], func->expression.varparam))
927 ast_type_to_string(self->params[i], tgot, sizeof(tgot));
928 ast_type_to_string(func->expression.varparam, texp, sizeof(texp));
929 compile_error(ast_ctx(self), "invalid type for parameter %u in function call: expected %s, got %s",
930 (unsigned int)(i+1), texp, tgot);
931 /* 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 ast_type_adopt(self, dest);
956 void ast_store_delete(ast_store *self)
958 ast_unref(self->dest);
959 ast_unref(self->source);
960 ast_expression_delete((ast_expression*)self);
964 ast_block* ast_block_new(lex_ctx ctx)
966 ast_instantiate(ast_block, ctx, ast_block_delete);
967 ast_expression_init((ast_expression*)self,
968 (ast_expression_codegen*)&ast_block_codegen);
972 self->collect = NULL;
977 bool ast_block_add_expr(ast_block *self, ast_expression *e)
979 ast_propagate_effects(self, e);
980 vec_push(self->exprs, e);
981 if (self->expression.next) {
982 ast_delete(self->expression.next);
983 self->expression.next = NULL;
985 ast_type_adopt(self, e);
989 void ast_block_collect(ast_block *self, ast_expression *expr)
991 vec_push(self->collect, expr);
992 expr->expression.node.keep = true;
995 void ast_block_delete(ast_block *self)
998 for (i = 0; i < vec_size(self->exprs); ++i)
999 ast_unref(self->exprs[i]);
1000 vec_free(self->exprs);
1001 for (i = 0; i < vec_size(self->locals); ++i)
1002 ast_delete(self->locals[i]);
1003 vec_free(self->locals);
1004 for (i = 0; i < vec_size(self->collect); ++i)
1005 ast_delete(self->collect[i]);
1006 vec_free(self->collect);
1007 ast_expression_delete((ast_expression*)self);
1011 void ast_block_set_type(ast_block *self, ast_expression *from)
1013 if (self->expression.next)
1014 ast_delete(self->expression.next);
1015 ast_type_adopt(self, from);
1018 ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype)
1020 ast_instantiate(ast_function, ctx, ast_function_delete);
1024 vtype->expression.vtype != TYPE_FUNCTION)
1026 compile_error(ast_ctx(self), "internal error: ast_function_new condition %i %i type=%i (probably 2 bodies?)",
1028 (int)vtype->hasvalue,
1029 vtype->expression.vtype);
1034 self->vtype = vtype;
1035 self->name = name ? util_strdup(name) : NULL;
1036 self->blocks = NULL;
1038 self->labelcount = 0;
1041 self->ir_func = NULL;
1042 self->curblock = NULL;
1044 self->breakblocks = NULL;
1045 self->continueblocks = NULL;
1047 vtype->hasvalue = true;
1048 vtype->constval.vfunc = self;
1050 self->varargs = NULL;
1052 self->fixedparams = NULL;
1057 void ast_function_delete(ast_function *self)
1061 mem_d((void*)self->name);
1063 /* ast_value_delete(self->vtype); */
1064 self->vtype->hasvalue = false;
1065 self->vtype->constval.vfunc = NULL;
1066 /* We use unref - if it was stored in a global table it is supposed
1067 * to be deleted from *there*
1069 ast_unref(self->vtype);
1071 for (i = 0; i < vec_size(self->blocks); ++i)
1072 ast_delete(self->blocks[i]);
1073 vec_free(self->blocks);
1074 vec_free(self->breakblocks);
1075 vec_free(self->continueblocks);
1077 ast_delete(self->varargs);
1079 ast_delete(self->argc);
1080 if (self->fixedparams)
1081 ast_unref(self->fixedparams);
1085 const char* ast_function_label(ast_function *self, const char *prefix)
1091 if (!OPTS_OPTION_BOOL(OPTION_DUMP) &&
1092 !OPTS_OPTION_BOOL(OPTION_DUMPFIN) &&
1093 !OPTS_OPTION_BOOL(OPTION_DEBUG))
1098 id = (self->labelcount++);
1099 len = strlen(prefix);
1101 from = self->labelbuf + sizeof(self->labelbuf)-1;
1104 *from-- = (id%10) + '0';
1108 memcpy(from - len, prefix, len);
1112 /*********************************************************************/
1114 * by convention you must never pass NULL to the 'ir_value **out'
1115 * parameter. If you really don't care about the output, pass a dummy.
1116 * But I can't imagine a pituation where the output is truly unnecessary.
1119 void _ast_codegen_output_type(ast_expression_common *self, ir_value *out)
1121 if (out->vtype == TYPE_FIELD)
1122 out->fieldtype = self->next->expression.vtype;
1123 if (out->vtype == TYPE_FUNCTION)
1124 out->outtype = self->next->expression.vtype;
1127 #define codegen_output_type(a,o) (_ast_codegen_output_type(&((a)->expression),(o)))
1129 bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out)
1133 if (self->expression.vtype == TYPE_NIL) {
1134 *out = func->ir_func->owner->nil;
1137 /* NOTE: This is the codegen for a variable used in an expression.
1138 * It is not the codegen to generate the value. For this purpose,
1139 * ast_local_codegen and ast_global_codegen are to be used before this
1140 * is executed. ast_function_codegen should take care of its locals,
1141 * and the ast-user should take care of ast_global_codegen to be used
1142 * on all the globals.
1145 char tname[1024]; /* typename is reserved in C++ */
1146 ast_type_to_string((ast_expression*)self, tname, sizeof(tname));
1147 compile_error(ast_ctx(self), "ast_value used before generated %s %s", tname, self->name);
1154 bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
1158 if (self->expression.vtype == TYPE_NIL) {
1159 compile_error(ast_ctx(self), "internal error: trying to generate a variable of TYPE_NIL");
1163 if (self->hasvalue && self->expression.vtype == TYPE_FUNCTION)
1165 ir_function *func = ir_builder_create_function(ir, self->name, self->expression.next->expression.vtype);
1168 func->context = ast_ctx(self);
1169 func->value->context = ast_ctx(self);
1171 self->constval.vfunc->ir_func = func;
1172 self->ir_v = func->value;
1173 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1174 self->ir_v->flags |= IR_FLAG_INCLUDE_DEF;
1175 /* The function is filled later on ast_function_codegen... */
1179 if (isfield && self->expression.vtype == TYPE_FIELD) {
1180 ast_expression *fieldtype = self->expression.next;
1182 if (self->hasvalue) {
1183 compile_error(ast_ctx(self), "TODO: constant field pointers with value");
1187 if (fieldtype->expression.vtype == TYPE_ARRAY) {
1192 ast_expression_common *elemtype;
1194 ast_value *array = (ast_value*)fieldtype;
1196 if (!ast_istype(fieldtype, ast_value)) {
1197 compile_error(ast_ctx(self), "internal error: ast_value required");
1201 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1202 if (!array->expression.count || array->expression.count > OPTS_OPTION_U32(OPTION_MAX_ARRAY_SIZE))
1203 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)array->expression.count);
1205 elemtype = &array->expression.next->expression;
1206 vtype = elemtype->vtype;
1208 v = ir_builder_create_field(ir, self->name, vtype);
1210 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", self->name);
1213 v->context = ast_ctx(self);
1214 v->unique_life = true;
1216 array->ir_v = self->ir_v = v;
1217 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1218 self->ir_v->flags |= IR_FLAG_INCLUDE_DEF;
1220 namelen = strlen(self->name);
1221 name = (char*)mem_a(namelen + 16);
1222 strncpy(name, self->name, namelen);
1224 array->ir_values = (ir_value**)mem_a(sizeof(array->ir_values[0]) * array->expression.count);
1225 array->ir_values[0] = v;
1226 for (ai = 1; ai < array->expression.count; ++ai) {
1227 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1228 array->ir_values[ai] = ir_builder_create_field(ir, name, vtype);
1229 if (!array->ir_values[ai]) {
1231 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", name);
1234 array->ir_values[ai]->context = ast_ctx(self);
1235 array->ir_values[ai]->unique_life = true;
1236 array->ir_values[ai]->locked = true;
1237 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1238 self->ir_values[ai]->flags |= IR_FLAG_INCLUDE_DEF;
1244 v = ir_builder_create_field(ir, self->name, self->expression.next->expression.vtype);
1247 v->context = ast_ctx(self);
1249 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1250 self->ir_v->flags |= IR_FLAG_INCLUDE_DEF;
1255 if (self->expression.vtype == TYPE_ARRAY) {
1260 ast_expression_common *elemtype = &self->expression.next->expression;
1261 int vtype = elemtype->vtype;
1263 /* same as with field arrays */
1264 if (!self->expression.count || self->expression.count > OPTS_OPTION_U32(OPTION_MAX_ARRAY_SIZE))
1265 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1267 v = ir_builder_create_global(ir, self->name, vtype);
1269 compile_error(ast_ctx(self), "ir_builder_create_global failed `%s`", self->name);
1272 v->context = ast_ctx(self);
1273 v->unique_life = true;
1275 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1276 v->flags |= IR_FLAG_INCLUDE_DEF;
1278 namelen = strlen(self->name);
1279 name = (char*)mem_a(namelen + 16);
1280 strncpy(name, self->name, namelen);
1282 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1283 self->ir_values[0] = v;
1284 for (ai = 1; ai < self->expression.count; ++ai) {
1285 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1286 self->ir_values[ai] = ir_builder_create_global(ir, name, vtype);
1287 if (!self->ir_values[ai]) {
1289 compile_error(ast_ctx(self), "ir_builder_create_global failed `%s`", name);
1292 self->ir_values[ai]->context = ast_ctx(self);
1293 self->ir_values[ai]->unique_life = true;
1294 self->ir_values[ai]->locked = true;
1295 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1296 self->ir_values[ai]->flags |= IR_FLAG_INCLUDE_DEF;
1302 /* Arrays don't do this since there's no "array" value which spans across the
1305 v = ir_builder_create_global(ir, self->name, self->expression.vtype);
1307 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", self->name);
1310 codegen_output_type(self, v);
1311 v->context = ast_ctx(self);
1314 if (self->hasvalue) {
1315 switch (self->expression.vtype)
1318 if (!ir_value_set_float(v, self->constval.vfloat))
1322 if (!ir_value_set_vector(v, self->constval.vvec))
1326 if (!ir_value_set_string(v, self->constval.vstring))
1330 compile_error(ast_ctx(self), "TODO: global constant array");
1333 compile_error(ast_ctx(self), "global of type function not properly generated");
1335 /* Cannot generate an IR value for a function,
1336 * need a pointer pointing to a function rather.
1339 if (!self->constval.vfield) {
1340 compile_error(ast_ctx(self), "field constant without vfield set");
1343 if (!self->constval.vfield->ir_v) {
1344 compile_error(ast_ctx(self), "field constant generated before its field");
1347 if (!ir_value_set_field(v, self->constval.vfield->ir_v))
1351 compile_error(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1356 /* link us to the ir_value */
1359 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1360 self->ir_v->flags |= IR_FLAG_INCLUDE_DEF;
1363 error: /* clean up */
1368 bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
1372 if (self->expression.vtype == TYPE_NIL) {
1373 compile_error(ast_ctx(self), "internal error: trying to generate a variable of TYPE_NIL");
1377 if (self->hasvalue && self->expression.vtype == TYPE_FUNCTION)
1379 /* Do we allow local functions? I think not...
1380 * this is NOT a function pointer atm.
1385 if (self->expression.vtype == TYPE_ARRAY) {
1390 ast_expression_common *elemtype = &self->expression.next->expression;
1391 int vtype = elemtype->vtype;
1393 func->flags |= IR_FLAG_HAS_ARRAYS;
1395 if (param && !(self->expression.flags & AST_FLAG_IS_VARARG)) {
1396 compile_error(ast_ctx(self), "array-parameters are not supported");
1400 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1401 if (!self->expression.count || self->expression.count > OPTS_OPTION_U32(OPTION_MAX_ARRAY_SIZE)) {
1402 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1405 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1406 if (!self->ir_values) {
1407 compile_error(ast_ctx(self), "failed to allocate array values");
1411 v = ir_function_create_local(func, self->name, vtype, param);
1413 compile_error(ast_ctx(self), "internal error: ir_function_create_local failed");
1416 v->context = ast_ctx(self);
1417 v->unique_life = true;
1420 namelen = strlen(self->name);
1421 name = (char*)mem_a(namelen + 16);
1422 strncpy(name, self->name, namelen);
1424 self->ir_values[0] = v;
1425 for (ai = 1; ai < self->expression.count; ++ai) {
1426 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1427 self->ir_values[ai] = ir_function_create_local(func, name, vtype, param);
1428 if (!self->ir_values[ai]) {
1429 compile_error(ast_ctx(self), "internal_error: ir_builder_create_global failed on `%s`", name);
1432 self->ir_values[ai]->context = ast_ctx(self);
1433 self->ir_values[ai]->unique_life = true;
1434 self->ir_values[ai]->locked = true;
1440 v = ir_function_create_local(func, self->name, self->expression.vtype, param);
1443 codegen_output_type(self, v);
1444 v->context = ast_ctx(self);
1447 /* A constant local... hmmm...
1448 * I suppose the IR will have to deal with this
1450 if (self->hasvalue) {
1451 switch (self->expression.vtype)
1454 if (!ir_value_set_float(v, self->constval.vfloat))
1458 if (!ir_value_set_vector(v, self->constval.vvec))
1462 if (!ir_value_set_string(v, self->constval.vstring))
1466 compile_error(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1471 /* link us to the ir_value */
1475 if (!ast_generate_accessors(self, func->owner))
1479 error: /* clean up */
1484 bool ast_generate_accessors(ast_value *self, ir_builder *ir)
1487 bool warn = OPTS_WARN(WARN_USED_UNINITIALIZED);
1488 if (!self->setter || !self->getter)
1490 for (i = 0; i < self->expression.count; ++i) {
1491 if (!self->ir_values) {
1492 compile_error(ast_ctx(self), "internal error: no array values generated for `%s`", self->name);
1495 if (!self->ir_values[i]) {
1496 compile_error(ast_ctx(self), "internal error: not all array values have been generated for `%s`", self->name);
1499 if (self->ir_values[i]->life) {
1500 compile_error(ast_ctx(self), "internal error: function containing `%s` already generated", self->name);
1505 opts_set(opts.warn, WARN_USED_UNINITIALIZED, false);
1507 if (!ast_global_codegen (self->setter, ir, false) ||
1508 !ast_function_codegen(self->setter->constval.vfunc, ir) ||
1509 !ir_function_finalize(self->setter->constval.vfunc->ir_func))
1511 compile_error(ast_ctx(self), "internal error: failed to generate setter for `%s`", self->name);
1512 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1517 if (!ast_global_codegen (self->getter, ir, false) ||
1518 !ast_function_codegen(self->getter->constval.vfunc, ir) ||
1519 !ir_function_finalize(self->getter->constval.vfunc->ir_func))
1521 compile_error(ast_ctx(self), "internal error: failed to generate getter for `%s`", self->name);
1522 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1526 for (i = 0; i < self->expression.count; ++i) {
1527 vec_free(self->ir_values[i]->life);
1529 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1533 bool ast_function_codegen(ast_function *self, ir_builder *ir)
1537 ast_expression_common *ec;
1538 ast_expression_codegen *cgen;
1543 irf = self->ir_func;
1545 compile_error(ast_ctx(self), "internal error: ast_function's related ast_value was not generated yet");
1549 /* fill the parameter list */
1550 ec = &self->vtype->expression;
1551 for (i = 0; i < vec_size(ec->params); ++i)
1553 if (ec->params[i]->expression.vtype == TYPE_FIELD)
1554 vec_push(irf->params, ec->params[i]->expression.next->expression.vtype);
1556 vec_push(irf->params, ec->params[i]->expression.vtype);
1557 if (!self->builtin) {
1558 if (!ast_local_codegen(ec->params[i], self->ir_func, true))
1563 if (self->varargs) {
1564 if (!ast_local_codegen(self->varargs, self->ir_func, true))
1566 irf->max_varargs = self->varargs->expression.count;
1569 if (self->builtin) {
1570 irf->builtin = self->builtin;
1574 if (!vec_size(self->blocks)) {
1575 compile_error(ast_ctx(self), "function `%s` has no body", self->name);
1579 irf->first = self->curblock = ir_function_create_block(ast_ctx(self), irf, "entry");
1580 if (!self->curblock) {
1581 compile_error(ast_ctx(self), "failed to allocate entry block for `%s`", self->name);
1589 if (!ast_local_codegen(self->argc, self->ir_func, true))
1591 cgen = self->argc->expression.codegen;
1592 if (!(*cgen)((ast_expression*)(self->argc), self, false, &va_count))
1594 cgen = self->fixedparams->expression.codegen;
1595 if (!(*cgen)((ast_expression*)(self->fixedparams), self, false, &fixed))
1597 sub = ir_block_create_binop(self->curblock, ast_ctx(self),
1598 ast_function_label(self, "va_count"), INSTR_SUB_F,
1599 ir_builder_get_va_count(ir), fixed);
1602 if (!ir_block_create_store_op(self->curblock, ast_ctx(self), INSTR_STORE_F,
1609 for (i = 0; i < vec_size(self->blocks); ++i) {
1610 cgen = self->blocks[i]->expression.codegen;
1611 if (!(*cgen)((ast_expression*)self->blocks[i], self, false, &dummy))
1615 /* TODO: check return types */
1616 if (!self->curblock->final)
1618 if (!self->vtype->expression.next ||
1619 self->vtype->expression.next->expression.vtype == TYPE_VOID)
1621 return ir_block_create_return(self->curblock, ast_ctx(self), NULL);
1623 else if (vec_size(self->curblock->entries) || self->curblock == irf->first)
1625 /* error("missing return"); */
1626 if (compile_warning(ast_ctx(self), WARN_MISSING_RETURN_VALUES,
1627 "control reaches end of non-void function (`%s`) via %s",
1628 self->name, self->curblock->label))
1632 return ir_block_create_return(self->curblock, ast_ctx(self), NULL);
1638 /* Note, you will not see ast_block_codegen generate ir_blocks.
1639 * To the AST and the IR, blocks are 2 different things.
1640 * In the AST it represents a block of code, usually enclosed in
1641 * curly braces {...}.
1642 * While in the IR it represents a block in terms of control-flow.
1644 bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_value **out)
1648 /* We don't use this
1649 * Note: an ast-representation using the comma-operator
1650 * of the form: (a, b, c) = x should not assign to c...
1653 compile_error(ast_ctx(self), "not an l-value (code-block)");
1657 if (self->expression.outr) {
1658 *out = self->expression.outr;
1662 /* output is NULL at first, we'll have each expression
1663 * assign to out output, thus, a comma-operator represention
1664 * using an ast_block will return the last generated value,
1665 * so: (b, c) + a executed both b and c, and returns c,
1666 * which is then added to a.
1670 /* generate locals */
1671 for (i = 0; i < vec_size(self->locals); ++i)
1673 if (!ast_local_codegen(self->locals[i], func->ir_func, false)) {
1674 if (OPTS_OPTION_BOOL(OPTION_DEBUG))
1675 compile_error(ast_ctx(self), "failed to generate local `%s`", self->locals[i]->name);
1680 for (i = 0; i < vec_size(self->exprs); ++i)
1682 ast_expression_codegen *gen;
1683 if (func->curblock->final && !ast_istype(self->exprs[i], ast_label)) {
1684 if (compile_warning(ast_ctx(self->exprs[i]), WARN_UNREACHABLE_CODE, "unreachable statement"))
1688 gen = self->exprs[i]->expression.codegen;
1689 if (!(*gen)(self->exprs[i], func, false, out))
1693 self->expression.outr = *out;
1698 bool ast_store_codegen(ast_store *self, ast_function *func, bool lvalue, ir_value **out)
1700 ast_expression_codegen *cgen;
1701 ir_value *left = NULL;
1702 ir_value *right = NULL;
1706 ast_array_index *ai = NULL;
1708 if (lvalue && self->expression.outl) {
1709 *out = self->expression.outl;
1713 if (!lvalue && self->expression.outr) {
1714 *out = self->expression.outr;
1718 if (ast_istype(self->dest, ast_array_index))
1721 ai = (ast_array_index*)self->dest;
1722 idx = (ast_value*)ai->index;
1724 if (ast_istype(ai->index, ast_value) && idx->hasvalue && idx->cvq == CV_CONST)
1729 /* we need to call the setter */
1730 ir_value *iridx, *funval;
1734 compile_error(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1738 arr = (ast_value*)ai->array;
1739 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1740 compile_error(ast_ctx(self), "value has no setter (%s)", arr->name);
1744 cgen = idx->expression.codegen;
1745 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1748 cgen = arr->setter->expression.codegen;
1749 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
1752 cgen = self->source->expression.codegen;
1753 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1756 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "store"), funval, false);
1759 ir_call_param(call, iridx);
1760 ir_call_param(call, right);
1761 self->expression.outr = right;
1767 cgen = self->dest->expression.codegen;
1769 if (!(*cgen)((ast_expression*)(self->dest), func, true, &left))
1771 self->expression.outl = left;
1773 cgen = self->source->expression.codegen;
1775 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1778 if (!ir_block_create_store_op(func->curblock, ast_ctx(self), self->op, left, right))
1780 self->expression.outr = right;
1783 /* Theoretically, an assinment returns its left side as an
1784 * lvalue, if we don't need an lvalue though, we return
1785 * the right side as an rvalue, otherwise we have to
1786 * somehow know whether or not we need to dereference the pointer
1787 * on the left side - that is: OP_LOAD if it was an address.
1788 * Also: in original QC we cannot OP_LOADP *anyway*.
1790 *out = (lvalue ? left : right);
1795 bool ast_binary_codegen(ast_binary *self, ast_function *func, bool lvalue, ir_value **out)
1797 ast_expression_codegen *cgen;
1798 ir_value *left, *right;
1800 /* A binary operation cannot yield an l-value */
1802 compile_error(ast_ctx(self), "not an l-value (binop)");
1806 if (self->expression.outr) {
1807 *out = self->expression.outr;
1811 if ((OPTS_FLAG(SHORT_LOGIC) || OPTS_FLAG(PERL_LOGIC)) &&
1812 (self->op == INSTR_AND || self->op == INSTR_OR))
1814 /* short circuit evaluation */
1815 ir_block *other, *merge;
1816 ir_block *from_left, *from_right;
1820 /* prepare end-block */
1821 merge_id = vec_size(func->ir_func->blocks);
1822 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "sce_merge"));
1824 /* generate the left expression */
1825 cgen = self->left->expression.codegen;
1826 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1828 /* remember the block */
1829 from_left = func->curblock;
1831 /* create a new block for the right expression */
1832 other = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "sce_other"));
1833 if (self->op == INSTR_AND) {
1834 /* on AND: left==true -> other */
1835 if (!ir_block_create_if(func->curblock, ast_ctx(self), left, other, merge))
1838 /* on OR: left==false -> other */
1839 if (!ir_block_create_if(func->curblock, ast_ctx(self), left, merge, other))
1842 /* use the likely flag */
1843 vec_last(func->curblock->instr)->likely = true;
1845 /* enter the right-expression's block */
1846 func->curblock = other;
1848 cgen = self->right->expression.codegen;
1849 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1851 /* remember block */
1852 from_right = func->curblock;
1854 /* jump to the merge block */
1855 if (!ir_block_create_jump(func->curblock, ast_ctx(self), merge))
1858 vec_remove(func->ir_func->blocks, merge_id, 1);
1859 vec_push(func->ir_func->blocks, merge);
1861 func->curblock = merge;
1862 phi = ir_block_create_phi(func->curblock, ast_ctx(self),
1863 ast_function_label(func, "sce_value"),
1864 self->expression.vtype);
1865 ir_phi_add(phi, from_left, left);
1866 ir_phi_add(phi, from_right, right);
1867 *out = ir_phi_value(phi);
1871 if (!OPTS_FLAG(PERL_LOGIC)) {
1873 if (OPTS_FLAG(CORRECT_LOGIC) && (*out)->vtype == TYPE_VECTOR) {
1874 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
1875 ast_function_label(func, "sce_bool_v"),
1879 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
1880 ast_function_label(func, "sce_bool"),
1885 else if (OPTS_FLAG(FALSE_EMPTY_STRINGS) && (*out)->vtype == TYPE_STRING) {
1886 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
1887 ast_function_label(func, "sce_bool_s"),
1891 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
1892 ast_function_label(func, "sce_bool"),
1898 *out = ir_block_create_binop(func->curblock, ast_ctx(self),
1899 ast_function_label(func, "sce_bool"),
1900 INSTR_AND, *out, *out);
1906 self->expression.outr = *out;
1907 codegen_output_type(self, *out);
1911 cgen = self->left->expression.codegen;
1912 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1915 cgen = self->right->expression.codegen;
1916 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1919 *out = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "bin"),
1920 self->op, left, right);
1923 self->expression.outr = *out;
1924 codegen_output_type(self, *out);
1929 bool ast_binstore_codegen(ast_binstore *self, ast_function *func, bool lvalue, ir_value **out)
1931 ast_expression_codegen *cgen;
1932 ir_value *leftl = NULL, *leftr, *right, *bin;
1936 ast_array_index *ai = NULL;
1937 ir_value *iridx = NULL;
1939 if (lvalue && self->expression.outl) {
1940 *out = self->expression.outl;
1944 if (!lvalue && self->expression.outr) {
1945 *out = self->expression.outr;
1949 if (ast_istype(self->dest, ast_array_index))
1952 ai = (ast_array_index*)self->dest;
1953 idx = (ast_value*)ai->index;
1955 if (ast_istype(ai->index, ast_value) && idx->hasvalue && idx->cvq == CV_CONST)
1959 /* for a binstore we need both an lvalue and an rvalue for the left side */
1960 /* rvalue of destination! */
1962 cgen = idx->expression.codegen;
1963 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1966 cgen = self->dest->expression.codegen;
1967 if (!(*cgen)((ast_expression*)(self->dest), func, false, &leftr))
1970 /* source as rvalue only */
1971 cgen = self->source->expression.codegen;
1972 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1975 /* now the binary */
1976 bin = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "binst"),
1977 self->opbin, leftr, right);
1978 self->expression.outr = bin;
1982 /* we need to call the setter */
1987 compile_error(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1991 arr = (ast_value*)ai->array;
1992 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1993 compile_error(ast_ctx(self), "value has no setter (%s)", arr->name);
1997 cgen = arr->setter->expression.codegen;
1998 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
2001 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "store"), funval, false);
2004 ir_call_param(call, iridx);
2005 ir_call_param(call, bin);
2006 self->expression.outr = bin;
2008 /* now store them */
2009 cgen = self->dest->expression.codegen;
2010 /* lvalue of destination */
2011 if (!(*cgen)((ast_expression*)(self->dest), func, true, &leftl))
2013 self->expression.outl = leftl;
2015 if (!ir_block_create_store_op(func->curblock, ast_ctx(self), self->opstore, leftl, bin))
2017 self->expression.outr = bin;
2020 /* Theoretically, an assinment returns its left side as an
2021 * lvalue, if we don't need an lvalue though, we return
2022 * the right side as an rvalue, otherwise we have to
2023 * somehow know whether or not we need to dereference the pointer
2024 * on the left side - that is: OP_LOAD if it was an address.
2025 * Also: in original QC we cannot OP_LOADP *anyway*.
2027 *out = (lvalue ? leftl : bin);
2032 bool ast_unary_codegen(ast_unary *self, ast_function *func, bool lvalue, ir_value **out)
2034 ast_expression_codegen *cgen;
2037 /* An unary operation cannot yield an l-value */
2039 compile_error(ast_ctx(self), "not an l-value (binop)");
2043 if (self->expression.outr) {
2044 *out = self->expression.outr;
2048 cgen = self->operand->expression.codegen;
2050 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
2053 *out = ir_block_create_unary(func->curblock, ast_ctx(self), ast_function_label(func, "unary"),
2057 self->expression.outr = *out;
2062 bool ast_return_codegen(ast_return *self, ast_function *func, bool lvalue, ir_value **out)
2064 ast_expression_codegen *cgen;
2069 /* In the context of a return operation, we don't actually return
2073 compile_error(ast_ctx(self), "return-expression is not an l-value");
2077 if (self->expression.outr) {
2078 compile_error(ast_ctx(self), "internal error: ast_return cannot be reused, it bears no result!");
2081 self->expression.outr = (ir_value*)1;
2083 if (self->operand) {
2084 cgen = self->operand->expression.codegen;
2086 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
2089 if (!ir_block_create_return(func->curblock, ast_ctx(self), operand))
2092 if (!ir_block_create_return(func->curblock, ast_ctx(self), NULL))
2099 bool ast_entfield_codegen(ast_entfield *self, ast_function *func, bool lvalue, ir_value **out)
2101 ast_expression_codegen *cgen;
2102 ir_value *ent, *field;
2104 /* This function needs to take the 'lvalue' flag into account!
2105 * As lvalue we provide a field-pointer, as rvalue we provide the
2109 if (lvalue && self->expression.outl) {
2110 *out = self->expression.outl;
2114 if (!lvalue && self->expression.outr) {
2115 *out = self->expression.outr;
2119 cgen = self->entity->expression.codegen;
2120 if (!(*cgen)((ast_expression*)(self->entity), func, false, &ent))
2123 cgen = self->field->expression.codegen;
2124 if (!(*cgen)((ast_expression*)(self->field), func, false, &field))
2129 *out = ir_block_create_fieldaddress(func->curblock, ast_ctx(self), ast_function_label(func, "efa"),
2132 *out = ir_block_create_load_from_ent(func->curblock, ast_ctx(self), ast_function_label(func, "efv"),
2133 ent, field, self->expression.vtype);
2134 /* Done AFTER error checking:
2135 codegen_output_type(self, *out);
2139 compile_error(ast_ctx(self), "failed to create %s instruction (output type %s)",
2140 (lvalue ? "ADDRESS" : "FIELD"),
2141 type_name[self->expression.vtype]);
2145 codegen_output_type(self, *out);
2148 self->expression.outl = *out;
2150 self->expression.outr = *out;
2152 /* Hm that should be it... */
2156 bool ast_member_codegen(ast_member *self, ast_function *func, bool lvalue, ir_value **out)
2158 ast_expression_codegen *cgen;
2161 /* in QC this is always an lvalue */
2162 if (lvalue && self->rvalue) {
2163 compile_error(ast_ctx(self), "not an l-value (member access)");
2166 if (self->expression.outl) {
2167 *out = self->expression.outl;
2171 cgen = self->owner->expression.codegen;
2172 if (!(*cgen)((ast_expression*)(self->owner), func, false, &vec))
2175 if (vec->vtype != TYPE_VECTOR &&
2176 !(vec->vtype == TYPE_FIELD && self->owner->expression.next->expression.vtype == TYPE_VECTOR))
2181 *out = ir_value_vector_member(vec, self->field);
2182 self->expression.outl = *out;
2184 return (*out != NULL);
2187 bool ast_array_index_codegen(ast_array_index *self, ast_function *func, bool lvalue, ir_value **out)
2192 if (!lvalue && self->expression.outr) {
2193 *out = self->expression.outr;
2196 if (lvalue && self->expression.outl) {
2197 *out = self->expression.outl;
2201 if (!ast_istype(self->array, ast_value)) {
2202 compile_error(ast_ctx(self), "array indexing this way is not supported");
2203 /* note this would actually be pointer indexing because the left side is
2204 * not an actual array but (hopefully) an indexable expression.
2205 * Once we get integer arithmetic, and GADDRESS/GSTORE/GLOAD instruction
2206 * support this path will be filled.
2211 arr = (ast_value*)self->array;
2212 idx = (ast_value*)self->index;
2214 if (!ast_istype(self->index, ast_value) || !idx->hasvalue || idx->cvq != CV_CONST) {
2215 /* Time to use accessor functions */
2216 ast_expression_codegen *cgen;
2217 ir_value *iridx, *funval;
2221 compile_error(ast_ctx(self), "(.2) array indexing here needs a compile-time constant");
2226 compile_error(ast_ctx(self), "value has no getter, don't know how to index it");
2230 cgen = self->index->expression.codegen;
2231 if (!(*cgen)((ast_expression*)(self->index), func, false, &iridx))
2234 cgen = arr->getter->expression.codegen;
2235 if (!(*cgen)((ast_expression*)(arr->getter), func, true, &funval))
2238 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "fetch"), funval, false);
2241 ir_call_param(call, iridx);
2243 *out = ir_call_value(call);
2244 self->expression.outr = *out;
2245 (*out)->vtype = self->expression.vtype;
2246 codegen_output_type(self, *out);
2250 if (idx->expression.vtype == TYPE_FLOAT) {
2251 unsigned int arridx = idx->constval.vfloat;
2252 if (arridx >= self->array->expression.count)
2254 compile_error(ast_ctx(self), "array index out of bounds: %i", arridx);
2257 *out = arr->ir_values[arridx];
2259 else if (idx->expression.vtype == TYPE_INTEGER) {
2260 unsigned int arridx = idx->constval.vint;
2261 if (arridx >= self->array->expression.count)
2263 compile_error(ast_ctx(self), "array index out of bounds: %i", arridx);
2266 *out = arr->ir_values[arridx];
2269 compile_error(ast_ctx(self), "array indexing here needs an integer constant");
2272 (*out)->vtype = self->expression.vtype;
2273 codegen_output_type(self, *out);
2277 bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_value **out)
2279 ast_expression_codegen *cgen;
2287 ir_block *ontrue_endblock = NULL;
2288 ir_block *onfalse_endblock = NULL;
2289 ir_block *merge = NULL;
2291 /* We don't output any value, thus also don't care about r/lvalue */
2295 if (self->expression.outr) {
2296 compile_error(ast_ctx(self), "internal error: ast_ifthen cannot be reused, it bears no result!");
2299 self->expression.outr = (ir_value*)1;
2301 /* generate the condition */
2302 cgen = self->cond->expression.codegen;
2303 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2305 /* update the block which will get the jump - because short-logic or ternaries may have changed this */
2306 cond = func->curblock;
2310 if (self->on_true) {
2311 /* create on-true block */
2312 ontrue = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "ontrue"));
2316 /* enter the block */
2317 func->curblock = ontrue;
2320 cgen = self->on_true->expression.codegen;
2321 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &dummy))
2324 /* we now need to work from the current endpoint */
2325 ontrue_endblock = func->curblock;
2330 if (self->on_false) {
2331 /* create on-false block */
2332 onfalse = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "onfalse"));
2336 /* enter the block */
2337 func->curblock = onfalse;
2340 cgen = self->on_false->expression.codegen;
2341 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &dummy))
2344 /* we now need to work from the current endpoint */
2345 onfalse_endblock = func->curblock;
2349 /* Merge block were they all merge in to */
2350 if (!ontrue || !onfalse || !ontrue_endblock->final || !onfalse_endblock->final)
2352 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "endif"));
2355 /* add jumps ot the merge block */
2356 if (ontrue && !ontrue_endblock->final && !ir_block_create_jump(ontrue_endblock, ast_ctx(self), merge))
2358 if (onfalse && !onfalse_endblock->final && !ir_block_create_jump(onfalse_endblock, ast_ctx(self), merge))
2361 /* Now enter the merge block */
2362 func->curblock = merge;
2365 /* we create the if here, that way all blocks are ordered :)
2367 if (!ir_block_create_if(cond, ast_ctx(self), condval,
2368 (ontrue ? ontrue : merge),
2369 (onfalse ? onfalse : merge)))
2377 bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_value **out)
2379 ast_expression_codegen *cgen;
2382 ir_value *trueval, *falseval;
2385 ir_block *cond = func->curblock;
2386 ir_block *cond_out = NULL;
2387 ir_block *ontrue, *ontrue_out = NULL;
2388 ir_block *onfalse, *onfalse_out = NULL;
2391 /* Ternary can never create an lvalue... */
2395 /* In theory it shouldn't be possible to pass through a node twice, but
2396 * in case we add any kind of optimization pass for the AST itself, it
2397 * may still happen, thus we remember a created ir_value and simply return one
2398 * if it already exists.
2400 if (self->expression.outr) {
2401 *out = self->expression.outr;
2405 /* In the following, contraty to ast_ifthen, we assume both paths exist. */
2407 /* generate the condition */
2408 func->curblock = cond;
2409 cgen = self->cond->expression.codegen;
2410 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2412 cond_out = func->curblock;
2414 /* create on-true block */
2415 ontrue = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_T"));
2420 /* enter the block */
2421 func->curblock = ontrue;
2424 cgen = self->on_true->expression.codegen;
2425 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &trueval))
2428 ontrue_out = func->curblock;
2431 /* create on-false block */
2432 onfalse = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_F"));
2437 /* enter the block */
2438 func->curblock = onfalse;
2441 cgen = self->on_false->expression.codegen;
2442 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &falseval))
2445 onfalse_out = func->curblock;
2448 /* create merge block */
2449 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_out"));
2452 /* jump to merge block */
2453 if (!ir_block_create_jump(ontrue_out, ast_ctx(self), merge))
2455 if (!ir_block_create_jump(onfalse_out, ast_ctx(self), merge))
2458 /* create if instruction */
2459 if (!ir_block_create_if(cond_out, ast_ctx(self), condval, ontrue, onfalse))
2462 /* Now enter the merge block */
2463 func->curblock = merge;
2465 /* Here, now, we need a PHI node
2466 * but first some sanity checking...
2468 if (trueval->vtype != falseval->vtype && trueval->vtype != TYPE_NIL && falseval->vtype != TYPE_NIL) {
2469 /* error("ternary with different types on the two sides"); */
2470 compile_error(ast_ctx(self), "internal error: ternary operand types invalid");
2475 phi = ir_block_create_phi(merge, ast_ctx(self), ast_function_label(func, "phi"), self->expression.vtype);
2477 compile_error(ast_ctx(self), "internal error: failed to generate phi node");
2480 ir_phi_add(phi, ontrue_out, trueval);
2481 ir_phi_add(phi, onfalse_out, falseval);
2483 self->expression.outr = ir_phi_value(phi);
2484 *out = self->expression.outr;
2486 codegen_output_type(self, *out);
2491 bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value **out)
2493 ast_expression_codegen *cgen;
2495 ir_value *dummy = NULL;
2496 ir_value *precond = NULL;
2497 ir_value *postcond = NULL;
2499 /* Since we insert some jumps "late" so we have blocks
2500 * ordered "nicely", we need to keep track of the actual end-blocks
2501 * of expressions to add the jumps to.
2503 ir_block *bbody = NULL, *end_bbody = NULL;
2504 ir_block *bprecond = NULL, *end_bprecond = NULL;
2505 ir_block *bpostcond = NULL, *end_bpostcond = NULL;
2506 ir_block *bincrement = NULL, *end_bincrement = NULL;
2507 ir_block *bout = NULL, *bin = NULL;
2509 /* let's at least move the outgoing block to the end */
2512 /* 'break' and 'continue' need to be able to find the right blocks */
2513 ir_block *bcontinue = NULL;
2514 ir_block *bbreak = NULL;
2516 ir_block *tmpblock = NULL;
2521 if (self->expression.outr) {
2522 compile_error(ast_ctx(self), "internal error: ast_loop cannot be reused, it bears no result!");
2525 self->expression.outr = (ir_value*)1;
2528 * Should we ever need some kind of block ordering, better make this function
2529 * move blocks around than write a block ordering algorithm later... after all
2530 * the ast and ir should work together, not against each other.
2533 /* initexpr doesn't get its own block, it's pointless, it could create more blocks
2534 * anyway if for example it contains a ternary.
2538 cgen = self->initexpr->expression.codegen;
2539 if (!(*cgen)((ast_expression*)(self->initexpr), func, false, &dummy))
2543 /* Store the block from which we enter this chaos */
2544 bin = func->curblock;
2546 /* The pre-loop condition needs its own block since we
2547 * need to be able to jump to the start of that expression.
2551 bprecond = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "pre_loop_cond"));
2555 /* the pre-loop-condition the least important place to 'continue' at */
2556 bcontinue = bprecond;
2559 func->curblock = bprecond;
2562 cgen = self->precond->expression.codegen;
2563 if (!(*cgen)((ast_expression*)(self->precond), func, false, &precond))
2566 end_bprecond = func->curblock;
2568 bprecond = end_bprecond = NULL;
2571 /* Now the next blocks won't be ordered nicely, but we need to
2572 * generate them this early for 'break' and 'continue'.
2574 if (self->increment) {
2575 bincrement = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "loop_increment"));
2578 bcontinue = bincrement; /* increment comes before the pre-loop-condition */
2580 bincrement = end_bincrement = NULL;
2583 if (self->postcond) {
2584 bpostcond = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "post_loop_cond"));
2587 bcontinue = bpostcond; /* postcond comes before the increment */
2589 bpostcond = end_bpostcond = NULL;
2592 bout_id = vec_size(func->ir_func->blocks);
2593 bout = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "after_loop"));
2598 /* The loop body... */
2599 /* if (self->body) */
2601 bbody = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "loop_body"));
2606 func->curblock = bbody;
2608 vec_push(func->breakblocks, bbreak);
2610 vec_push(func->continueblocks, bcontinue);
2612 vec_push(func->continueblocks, bbody);
2616 cgen = self->body->expression.codegen;
2617 if (!(*cgen)((ast_expression*)(self->body), func, false, &dummy))
2621 end_bbody = func->curblock;
2622 vec_pop(func->breakblocks);
2623 vec_pop(func->continueblocks);
2626 /* post-loop-condition */
2630 func->curblock = bpostcond;
2633 cgen = self->postcond->expression.codegen;
2634 if (!(*cgen)((ast_expression*)(self->postcond), func, false, &postcond))
2637 end_bpostcond = func->curblock;
2640 /* The incrementor */
2641 if (self->increment)
2644 func->curblock = bincrement;
2647 cgen = self->increment->expression.codegen;
2648 if (!(*cgen)((ast_expression*)(self->increment), func, false, &dummy))
2651 end_bincrement = func->curblock;
2654 /* In any case now, we continue from the outgoing block */
2655 func->curblock = bout;
2657 /* Now all blocks are in place */
2658 /* From 'bin' we jump to whatever comes first */
2659 if (bprecond) tmpblock = bprecond;
2660 else if (bbody) tmpblock = bbody;
2661 else if (bpostcond) tmpblock = bpostcond;
2662 else tmpblock = bout;
2663 if (!ir_block_create_jump(bin, ast_ctx(self), tmpblock))
2669 ir_block *ontrue, *onfalse;
2670 if (bbody) ontrue = bbody;
2671 else if (bincrement) ontrue = bincrement;
2672 else if (bpostcond) ontrue = bpostcond;
2673 else ontrue = bprecond;
2675 if (self->pre_not) {
2680 if (!ir_block_create_if(end_bprecond, ast_ctx(self), precond, ontrue, onfalse))
2687 if (bincrement) tmpblock = bincrement;
2688 else if (bpostcond) tmpblock = bpostcond;
2689 else if (bprecond) tmpblock = bprecond;
2690 else tmpblock = bbody;
2691 if (!end_bbody->final && !ir_block_create_jump(end_bbody, ast_ctx(self), tmpblock))
2695 /* from increment */
2698 if (bpostcond) tmpblock = bpostcond;
2699 else if (bprecond) tmpblock = bprecond;
2700 else if (bbody) tmpblock = bbody;
2701 else tmpblock = bout;
2702 if (!ir_block_create_jump(end_bincrement, ast_ctx(self), tmpblock))
2709 ir_block *ontrue, *onfalse;
2710 if (bprecond) ontrue = bprecond;
2711 else if (bbody) ontrue = bbody;
2712 else if (bincrement) ontrue = bincrement;
2713 else ontrue = bpostcond;
2715 if (self->post_not) {
2720 if (!ir_block_create_if(end_bpostcond, ast_ctx(self), postcond, ontrue, onfalse))
2724 /* Move 'bout' to the end */
2725 vec_remove(func->ir_func->blocks, bout_id, 1);
2726 vec_push(func->ir_func->blocks, bout);
2731 bool ast_breakcont_codegen(ast_breakcont *self, ast_function *func, bool lvalue, ir_value **out)
2738 compile_error(ast_ctx(self), "break/continue expression is not an l-value");
2742 if (self->expression.outr) {
2743 compile_error(ast_ctx(self), "internal error: ast_breakcont cannot be reused!");
2746 self->expression.outr = (ir_value*)1;
2748 if (self->is_continue)
2749 target = func->continueblocks[vec_size(func->continueblocks)-1-self->levels];
2751 target = func->breakblocks[vec_size(func->breakblocks)-1-self->levels];
2754 compile_error(ast_ctx(self), "%s is lacking a target block", (self->is_continue ? "continue" : "break"));
2758 if (!ir_block_create_jump(func->curblock, ast_ctx(self), target))
2763 bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_value **out)
2765 ast_expression_codegen *cgen;
2767 ast_switch_case *def_case = NULL;
2768 ir_block *def_bfall = NULL;
2769 ir_block *def_bfall_to = NULL;
2770 bool set_def_bfall_to = false;
2772 ir_value *dummy = NULL;
2773 ir_value *irop = NULL;
2774 ir_block *bout = NULL;
2775 ir_block *bfall = NULL;
2783 compile_error(ast_ctx(self), "switch expression is not an l-value");
2787 if (self->expression.outr) {
2788 compile_error(ast_ctx(self), "internal error: ast_switch cannot be reused!");
2791 self->expression.outr = (ir_value*)1;
2796 cgen = self->operand->expression.codegen;
2797 if (!(*cgen)((ast_expression*)(self->operand), func, false, &irop))
2800 if (!vec_size(self->cases))
2803 cmpinstr = type_eq_instr[irop->vtype];
2804 if (cmpinstr >= VINSTR_END) {
2805 ast_type_to_string(self->operand, typestr, sizeof(typestr));
2806 compile_error(ast_ctx(self), "invalid type to perform a switch on: %s", typestr);
2810 bout_id = vec_size(func->ir_func->blocks);
2811 bout = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "after_switch"));
2815 /* setup the break block */
2816 vec_push(func->breakblocks, bout);
2818 /* Now create all cases */
2819 for (c = 0; c < vec_size(self->cases); ++c) {
2820 ir_value *cond, *val;
2821 ir_block *bcase, *bnot;
2824 ast_switch_case *swcase = &self->cases[c];
2826 if (swcase->value) {
2827 /* A regular case */
2828 /* generate the condition operand */
2829 cgen = swcase->value->expression.codegen;
2830 if (!(*cgen)((ast_expression*)(swcase->value), func, false, &val))
2832 /* generate the condition */
2833 cond = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "switch_eq"), cmpinstr, irop, val);
2837 bcase = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "case"));
2838 bnot_id = vec_size(func->ir_func->blocks);
2839 bnot = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "not_case"));
2840 if (!bcase || !bnot)
2842 if (set_def_bfall_to) {
2843 set_def_bfall_to = false;
2844 def_bfall_to = bcase;
2846 if (!ir_block_create_if(func->curblock, ast_ctx(self), cond, bcase, bnot))
2849 /* Make the previous case-end fall through */
2850 if (bfall && !bfall->final) {
2851 if (!ir_block_create_jump(bfall, ast_ctx(self), bcase))
2855 /* enter the case */
2856 func->curblock = bcase;
2857 cgen = swcase->code->expression.codegen;
2858 if (!(*cgen)((ast_expression*)swcase->code, func, false, &dummy))
2861 /* remember this block to fall through from */
2862 bfall = func->curblock;
2864 /* enter the else and move it down */
2865 func->curblock = bnot;
2866 vec_remove(func->ir_func->blocks, bnot_id, 1);
2867 vec_push(func->ir_func->blocks, bnot);
2869 /* The default case */
2870 /* Remember where to fall through from: */
2873 /* remember which case it was */
2875 /* And the next case will be remembered */
2876 set_def_bfall_to = true;
2880 /* Jump from the last bnot to bout */
2881 if (bfall && !bfall->final && !ir_block_create_jump(bfall, ast_ctx(self), bout)) {
2883 astwarning(ast_ctx(bfall), WARN_???, "missing break after last case");
2888 /* If there was a default case, put it down here */
2892 /* No need to create an extra block */
2893 bcase = func->curblock;
2895 /* Insert the fallthrough jump */
2896 if (def_bfall && !def_bfall->final) {
2897 if (!ir_block_create_jump(def_bfall, ast_ctx(self), bcase))
2901 /* Now generate the default code */
2902 cgen = def_case->code->expression.codegen;
2903 if (!(*cgen)((ast_expression*)def_case->code, func, false, &dummy))
2906 /* see if we need to fall through */
2907 if (def_bfall_to && !func->curblock->final)
2909 if (!ir_block_create_jump(func->curblock, ast_ctx(self), def_bfall_to))
2914 /* Jump from the last bnot to bout */
2915 if (!func->curblock->final && !ir_block_create_jump(func->curblock, ast_ctx(self), bout))
2917 /* enter the outgoing block */
2918 func->curblock = bout;
2920 /* restore the break block */
2921 vec_pop(func->breakblocks);
2923 /* Move 'bout' to the end, it's nicer */
2924 vec_remove(func->ir_func->blocks, bout_id, 1);
2925 vec_push(func->ir_func->blocks, bout);
2930 bool ast_label_codegen(ast_label *self, ast_function *func, bool lvalue, ir_value **out)
2935 if (self->undefined) {
2936 compile_error(ast_ctx(self), "internal error: ast_label never defined");
2942 compile_error(ast_ctx(self), "internal error: ast_label cannot be an lvalue");
2946 /* simply create a new block and jump to it */
2947 self->irblock = ir_function_create_block(ast_ctx(self), func->ir_func, self->name);
2948 if (!self->irblock) {
2949 compile_error(ast_ctx(self), "failed to allocate label block `%s`", self->name);
2952 if (!func->curblock->final) {
2953 if (!ir_block_create_jump(func->curblock, ast_ctx(self), self->irblock))
2957 /* enter the new block */
2958 func->curblock = self->irblock;
2960 /* Generate all the leftover gotos */
2961 for (i = 0; i < vec_size(self->gotos); ++i) {
2962 if (!ast_goto_codegen(self->gotos[i], func, false, &dummy))
2969 bool ast_goto_codegen(ast_goto *self, ast_function *func, bool lvalue, ir_value **out)
2973 compile_error(ast_ctx(self), "internal error: ast_goto cannot be an lvalue");
2977 if (self->target->irblock) {
2978 if (self->irblock_from) {
2979 /* we already tried once, this is the callback */
2980 self->irblock_from->final = false;
2981 if (!ir_block_create_goto(self->irblock_from, ast_ctx(self), self->target->irblock)) {
2982 compile_error(ast_ctx(self), "failed to generate goto to `%s`", self->name);
2988 if (!ir_block_create_goto(func->curblock, ast_ctx(self), self->target->irblock)) {
2989 compile_error(ast_ctx(self), "failed to generate goto to `%s`", self->name);
2996 /* the target has not yet been created...
2997 * close this block in a sneaky way:
2999 func->curblock->final = true;
3000 self->irblock_from = func->curblock;
3001 ast_label_register_goto(self->target, self);
3007 bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value **out)
3009 ast_expression_codegen *cgen;
3011 ir_instr *callinstr;
3014 ir_value *funval = NULL;
3016 /* return values are never lvalues */
3018 compile_error(ast_ctx(self), "not an l-value (function call)");
3022 if (self->expression.outr) {
3023 *out = self->expression.outr;
3027 cgen = self->func->expression.codegen;
3028 if (!(*cgen)((ast_expression*)(self->func), func, false, &funval))
3036 for (i = 0; i < vec_size(self->params); ++i)
3039 ast_expression *expr = self->params[i];
3041 cgen = expr->expression.codegen;
3042 if (!(*cgen)(expr, func, false, ¶m))
3046 vec_push(params, param);
3049 /* varargs counter */
3050 if (self->va_count) {
3052 ir_builder *builder = func->curblock->owner->owner;
3053 cgen = self->va_count->expression.codegen;
3054 if (!(*cgen)((ast_expression*)(self->va_count), func, false, &va_count))
3056 if (!ir_block_create_store_op(func->curblock, ast_ctx(self), INSTR_STORE_F,
3057 ir_builder_get_va_count(builder), va_count))
3063 callinstr = ir_block_create_call(func->curblock, ast_ctx(self),
3064 ast_function_label(func, "call"),
3065 funval, !!(self->func->expression.flags & AST_FLAG_NORETURN));
3069 for (i = 0; i < vec_size(params); ++i) {
3070 ir_call_param(callinstr, params[i]);
3073 *out = ir_call_value(callinstr);
3074 self->expression.outr = *out;
3076 codegen_output_type(self, *out);