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;
351 self->argcounter = NULL;
356 void ast_value_delete(ast_value* self)
359 mem_d((void*)self->name);
360 if (self->argcounter)
361 mem_d((void*)self->argcounter);
362 if (self->hasvalue) {
363 switch (self->expression.vtype)
366 mem_d((void*)self->constval.vstring);
369 /* unlink us from the function node */
370 self->constval.vfunc->vtype = NULL;
372 /* NOTE: delete function? currently collected in
373 * the parser structure
380 mem_d(self->ir_values);
385 ast_expression_delete((ast_expression*)self);
389 void ast_value_params_add(ast_value *self, ast_value *p)
391 vec_push(self->expression.params, p);
394 bool ast_value_set_name(ast_value *self, const char *name)
397 mem_d((void*)self->name);
398 self->name = util_strdup(name);
402 ast_binary* ast_binary_new(lex_ctx ctx, int op,
403 ast_expression* left, ast_expression* right)
405 ast_instantiate(ast_binary, ctx, ast_binary_delete);
406 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binary_codegen);
412 ast_propagate_effects(self, left);
413 ast_propagate_effects(self, right);
415 if (op >= INSTR_EQ_F && op <= INSTR_GT)
416 self->expression.vtype = TYPE_FLOAT;
417 else if (op == INSTR_AND || op == INSTR_OR) {
418 if (OPTS_FLAG(PERL_LOGIC))
419 ast_type_adopt(self, right);
421 self->expression.vtype = TYPE_FLOAT;
423 else if (op == INSTR_BITAND || op == INSTR_BITOR)
424 self->expression.vtype = TYPE_FLOAT;
425 else if (op == INSTR_MUL_VF || op == INSTR_MUL_FV)
426 self->expression.vtype = TYPE_VECTOR;
427 else if (op == INSTR_MUL_V)
428 self->expression.vtype = TYPE_FLOAT;
430 self->expression.vtype = left->expression.vtype;
435 void ast_binary_delete(ast_binary *self)
437 ast_unref(self->left);
438 ast_unref(self->right);
439 ast_expression_delete((ast_expression*)self);
443 ast_binstore* ast_binstore_new(lex_ctx ctx, int storop, int op,
444 ast_expression* left, ast_expression* right)
446 ast_instantiate(ast_binstore, ctx, ast_binstore_delete);
447 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binstore_codegen);
449 ast_side_effects(self) = true;
451 self->opstore = storop;
454 self->source = right;
456 self->keep_dest = false;
458 if (!ast_type_adopt(self, left)) {
466 void ast_binstore_delete(ast_binstore *self)
468 if (!self->keep_dest)
469 ast_unref(self->dest);
470 ast_unref(self->source);
471 ast_expression_delete((ast_expression*)self);
475 ast_unary* ast_unary_new(lex_ctx ctx, int op,
476 ast_expression *expr)
478 ast_instantiate(ast_unary, ctx, ast_unary_delete);
479 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_unary_codegen);
482 self->operand = expr;
484 ast_propagate_effects(self, expr);
486 if (op >= INSTR_NOT_F && op <= INSTR_NOT_FNC) {
487 self->expression.vtype = TYPE_FLOAT;
489 compile_error(ctx, "cannot determine type of unary operation %s", asm_instr[op].m);
494 void ast_unary_delete(ast_unary *self)
496 if (self->operand) ast_unref(self->operand);
497 ast_expression_delete((ast_expression*)self);
501 ast_return* ast_return_new(lex_ctx ctx, ast_expression *expr)
503 ast_instantiate(ast_return, ctx, ast_return_delete);
504 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_return_codegen);
506 self->operand = expr;
509 ast_propagate_effects(self, expr);
514 void ast_return_delete(ast_return *self)
517 ast_unref(self->operand);
518 ast_expression_delete((ast_expression*)self);
522 ast_entfield* ast_entfield_new(lex_ctx ctx, ast_expression *entity, ast_expression *field)
524 if (field->expression.vtype != TYPE_FIELD) {
525 compile_error(ctx, "ast_entfield_new with expression not of type field");
528 return ast_entfield_new_force(ctx, entity, field, field->expression.next);
531 ast_entfield* ast_entfield_new_force(lex_ctx ctx, ast_expression *entity, ast_expression *field, const ast_expression *outtype)
533 ast_instantiate(ast_entfield, ctx, ast_entfield_delete);
537 /* Error: field has no type... */
541 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_entfield_codegen);
543 self->entity = entity;
545 ast_propagate_effects(self, entity);
546 ast_propagate_effects(self, field);
548 if (!ast_type_adopt(self, outtype)) {
549 ast_entfield_delete(self);
556 void ast_entfield_delete(ast_entfield *self)
558 ast_unref(self->entity);
559 ast_unref(self->field);
560 ast_expression_delete((ast_expression*)self);
564 ast_member* ast_member_new(lex_ctx ctx, ast_expression *owner, unsigned int field, const char *name)
566 ast_instantiate(ast_member, ctx, ast_member_delete);
572 if (owner->expression.vtype != TYPE_VECTOR &&
573 owner->expression.vtype != TYPE_FIELD) {
574 compile_error(ctx, "member-access on an invalid owner of type %s", type_name[owner->expression.vtype]);
579 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_member_codegen);
580 self->expression.node.keep = true; /* keep */
582 if (owner->expression.vtype == TYPE_VECTOR) {
583 self->expression.vtype = TYPE_FLOAT;
584 self->expression.next = NULL;
586 self->expression.vtype = TYPE_FIELD;
587 self->expression.next = ast_shallow_type(ctx, TYPE_FLOAT);
590 self->rvalue = false;
592 ast_propagate_effects(self, owner);
596 self->name = util_strdup(name);
603 void ast_member_delete(ast_member *self)
605 /* The owner is always an ast_value, which has .keep=true,
606 * also: ast_members are usually deleted after the owner, thus
607 * this will cause invalid access
608 ast_unref(self->owner);
609 * once we allow (expression).x to access a vector-member, we need
610 * to change this: preferably by creating an alternate ast node for this
611 * purpose that is not garbage-collected.
613 ast_expression_delete((ast_expression*)self);
617 bool ast_member_set_name(ast_member *self, const char *name)
620 mem_d((void*)self->name);
621 self->name = util_strdup(name);
625 ast_array_index* ast_array_index_new(lex_ctx ctx, ast_expression *array, ast_expression *index)
627 ast_expression *outtype;
628 ast_instantiate(ast_array_index, ctx, ast_array_index_delete);
630 outtype = array->expression.next;
633 /* Error: field has no type... */
637 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_array_index_codegen);
641 ast_propagate_effects(self, array);
642 ast_propagate_effects(self, index);
644 if (!ast_type_adopt(self, outtype)) {
645 ast_array_index_delete(self);
648 if (array->expression.vtype == TYPE_FIELD && outtype->expression.vtype == TYPE_ARRAY) {
649 if (self->expression.vtype != TYPE_ARRAY) {
650 compile_error(ast_ctx(self), "array_index node on type");
651 ast_array_index_delete(self);
654 self->array = outtype;
655 self->expression.vtype = TYPE_FIELD;
661 void ast_array_index_delete(ast_array_index *self)
663 ast_unref(self->array);
664 ast_unref(self->index);
665 ast_expression_delete((ast_expression*)self);
669 ast_ifthen* ast_ifthen_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
671 ast_instantiate(ast_ifthen, ctx, ast_ifthen_delete);
672 if (!ontrue && !onfalse) {
673 /* because it is invalid */
677 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ifthen_codegen);
680 self->on_true = ontrue;
681 self->on_false = onfalse;
682 ast_propagate_effects(self, cond);
684 ast_propagate_effects(self, ontrue);
686 ast_propagate_effects(self, onfalse);
691 void ast_ifthen_delete(ast_ifthen *self)
693 ast_unref(self->cond);
695 ast_unref(self->on_true);
697 ast_unref(self->on_false);
698 ast_expression_delete((ast_expression*)self);
702 ast_ternary* ast_ternary_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
704 ast_expression *exprtype = ontrue;
705 ast_instantiate(ast_ternary, ctx, ast_ternary_delete);
706 /* This time NEITHER must be NULL */
707 if (!ontrue || !onfalse) {
711 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ternary_codegen);
714 self->on_true = ontrue;
715 self->on_false = onfalse;
716 ast_propagate_effects(self, cond);
717 ast_propagate_effects(self, ontrue);
718 ast_propagate_effects(self, onfalse);
720 if (ontrue->expression.vtype == TYPE_NIL)
722 if (!ast_type_adopt(self, exprtype)) {
723 ast_ternary_delete(self);
730 void ast_ternary_delete(ast_ternary *self)
732 /* the if()s are only there because computed-gotos can set them
735 if (self->cond) ast_unref(self->cond);
736 if (self->on_true) ast_unref(self->on_true);
737 if (self->on_false) ast_unref(self->on_false);
738 ast_expression_delete((ast_expression*)self);
742 ast_loop* ast_loop_new(lex_ctx ctx,
743 ast_expression *initexpr,
744 ast_expression *precond, bool pre_not,
745 ast_expression *postcond, bool post_not,
746 ast_expression *increment,
747 ast_expression *body)
749 ast_instantiate(ast_loop, ctx, ast_loop_delete);
750 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_loop_codegen);
752 self->initexpr = initexpr;
753 self->precond = precond;
754 self->postcond = postcond;
755 self->increment = increment;
758 self->pre_not = pre_not;
759 self->post_not = post_not;
762 ast_propagate_effects(self, initexpr);
764 ast_propagate_effects(self, precond);
766 ast_propagate_effects(self, postcond);
768 ast_propagate_effects(self, increment);
770 ast_propagate_effects(self, body);
775 void ast_loop_delete(ast_loop *self)
778 ast_unref(self->initexpr);
780 ast_unref(self->precond);
782 ast_unref(self->postcond);
784 ast_unref(self->increment);
786 ast_unref(self->body);
787 ast_expression_delete((ast_expression*)self);
791 ast_breakcont* ast_breakcont_new(lex_ctx ctx, bool iscont, unsigned int levels)
793 ast_instantiate(ast_breakcont, ctx, ast_breakcont_delete);
794 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_breakcont_codegen);
796 self->is_continue = iscont;
797 self->levels = levels;
802 void ast_breakcont_delete(ast_breakcont *self)
804 ast_expression_delete((ast_expression*)self);
808 ast_switch* ast_switch_new(lex_ctx ctx, ast_expression *op)
810 ast_instantiate(ast_switch, ctx, ast_switch_delete);
811 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_switch_codegen);
816 ast_propagate_effects(self, op);
821 void ast_switch_delete(ast_switch *self)
824 ast_unref(self->operand);
826 for (i = 0; i < vec_size(self->cases); ++i) {
827 if (self->cases[i].value)
828 ast_unref(self->cases[i].value);
829 ast_unref(self->cases[i].code);
831 vec_free(self->cases);
833 ast_expression_delete((ast_expression*)self);
837 ast_label* ast_label_new(lex_ctx ctx, const char *name, bool undefined)
839 ast_instantiate(ast_label, ctx, ast_label_delete);
840 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_label_codegen);
842 self->expression.vtype = TYPE_NOEXPR;
844 self->name = util_strdup(name);
845 self->irblock = NULL;
847 self->undefined = undefined;
852 void ast_label_delete(ast_label *self)
854 mem_d((void*)self->name);
855 vec_free(self->gotos);
856 ast_expression_delete((ast_expression*)self);
860 void ast_label_register_goto(ast_label *self, ast_goto *g)
862 vec_push(self->gotos, g);
865 ast_goto* ast_goto_new(lex_ctx ctx, const char *name)
867 ast_instantiate(ast_goto, ctx, ast_goto_delete);
868 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_goto_codegen);
870 self->name = util_strdup(name);
872 self->irblock_from = NULL;
877 void ast_goto_delete(ast_goto *self)
879 mem_d((void*)self->name);
880 ast_expression_delete((ast_expression*)self);
884 void ast_goto_set_label(ast_goto *self, ast_label *label)
886 self->target = label;
889 ast_call* ast_call_new(lex_ctx ctx,
890 ast_expression *funcexpr)
892 ast_instantiate(ast_call, ctx, ast_call_delete);
893 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_call_codegen);
895 ast_side_effects(self) = true;
898 self->func = funcexpr;
899 self->va_count = NULL;
901 ast_type_adopt(self, funcexpr->expression.next);
906 void ast_call_delete(ast_call *self)
909 for (i = 0; i < vec_size(self->params); ++i)
910 ast_unref(self->params[i]);
911 vec_free(self->params);
914 ast_unref(self->func);
917 ast_unref(self->va_count);
919 ast_expression_delete((ast_expression*)self);
923 bool ast_call_check_types(ast_call *self)
929 const ast_expression *func = self->func;
930 size_t count = vec_size(self->params);
931 if (count > vec_size(func->expression.params))
932 count = vec_size(func->expression.params);
934 for (i = 0; i < count; ++i) {
935 if (!ast_compare_type(self->params[i], (ast_expression*)(func->expression.params[i])))
937 ast_type_to_string(self->params[i], tgot, sizeof(tgot));
938 ast_type_to_string((ast_expression*)func->expression.params[i], texp, sizeof(texp));
939 compile_error(ast_ctx(self), "invalid type for parameter %u in function call: expected %s, got %s",
940 (unsigned int)(i+1), texp, tgot);
941 /* we don't immediately return */
945 count = vec_size(self->params);
946 if (count > vec_size(func->expression.params) && func->expression.varparam) {
947 for (; i < count; ++i) {
948 if (!ast_compare_type(self->params[i], func->expression.varparam))
950 ast_type_to_string(self->params[i], tgot, sizeof(tgot));
951 ast_type_to_string(func->expression.varparam, texp, sizeof(texp));
952 compile_error(ast_ctx(self), "invalid type for parameter %u in function call: expected %s, got %s",
953 (unsigned int)(i+1), texp, tgot);
954 /* we don't immediately return */
962 ast_store* ast_store_new(lex_ctx ctx, int op,
963 ast_expression *dest, ast_expression *source)
965 ast_instantiate(ast_store, ctx, ast_store_delete);
966 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_store_codegen);
968 ast_side_effects(self) = true;
972 self->source = source;
974 if (!ast_type_adopt(self, dest)) {
982 void ast_store_delete(ast_store *self)
984 ast_unref(self->dest);
985 ast_unref(self->source);
986 ast_expression_delete((ast_expression*)self);
990 ast_block* ast_block_new(lex_ctx ctx)
992 ast_instantiate(ast_block, ctx, ast_block_delete);
993 ast_expression_init((ast_expression*)self,
994 (ast_expression_codegen*)&ast_block_codegen);
998 self->collect = NULL;
1003 bool ast_block_add_expr(ast_block *self, ast_expression *e)
1005 ast_propagate_effects(self, e);
1006 vec_push(self->exprs, e);
1007 if (self->expression.next) {
1008 ast_delete(self->expression.next);
1009 self->expression.next = NULL;
1011 if (!ast_type_adopt(self, e)) {
1012 compile_error(ast_ctx(self), "internal error: failed to adopt type");
1018 void ast_block_collect(ast_block *self, ast_expression *expr)
1020 vec_push(self->collect, expr);
1021 expr->expression.node.keep = true;
1024 void ast_block_delete(ast_block *self)
1027 for (i = 0; i < vec_size(self->exprs); ++i)
1028 ast_unref(self->exprs[i]);
1029 vec_free(self->exprs);
1030 for (i = 0; i < vec_size(self->locals); ++i)
1031 ast_delete(self->locals[i]);
1032 vec_free(self->locals);
1033 for (i = 0; i < vec_size(self->collect); ++i)
1034 ast_delete(self->collect[i]);
1035 vec_free(self->collect);
1036 ast_expression_delete((ast_expression*)self);
1040 bool ast_block_set_type(ast_block *self, ast_expression *from)
1042 if (self->expression.next)
1043 ast_delete(self->expression.next);
1044 if (!ast_type_adopt(self, from))
1049 ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype)
1051 ast_instantiate(ast_function, ctx, ast_function_delete);
1055 vtype->expression.vtype != TYPE_FUNCTION)
1057 compile_error(ast_ctx(self), "internal error: ast_function_new condition %i %i type=%i (probably 2 bodies?)",
1059 (int)vtype->hasvalue,
1060 vtype->expression.vtype);
1065 self->vtype = vtype;
1066 self->name = name ? util_strdup(name) : NULL;
1067 self->blocks = NULL;
1069 self->labelcount = 0;
1072 self->ir_func = NULL;
1073 self->curblock = NULL;
1075 self->breakblocks = NULL;
1076 self->continueblocks = NULL;
1078 vtype->hasvalue = true;
1079 vtype->constval.vfunc = self;
1081 self->varargs = NULL;
1086 void ast_function_delete(ast_function *self)
1090 mem_d((void*)self->name);
1092 /* ast_value_delete(self->vtype); */
1093 self->vtype->hasvalue = false;
1094 self->vtype->constval.vfunc = NULL;
1095 /* We use unref - if it was stored in a global table it is supposed
1096 * to be deleted from *there*
1098 ast_unref(self->vtype);
1100 for (i = 0; i < vec_size(self->blocks); ++i)
1101 ast_delete(self->blocks[i]);
1102 vec_free(self->blocks);
1103 vec_free(self->breakblocks);
1104 vec_free(self->continueblocks);
1106 ast_delete(self->varargs);
1110 const char* ast_function_label(ast_function *self, const char *prefix)
1116 if (!opts.dump && !opts.dumpfin && !opts.debug)
1119 id = (self->labelcount++);
1120 len = strlen(prefix);
1122 from = self->labelbuf + sizeof(self->labelbuf)-1;
1125 *from-- = (id%10) + '0';
1129 memcpy(from - len, prefix, len);
1133 /*********************************************************************/
1135 * by convention you must never pass NULL to the 'ir_value **out'
1136 * parameter. If you really don't care about the output, pass a dummy.
1137 * But I can't imagine a pituation where the output is truly unnecessary.
1140 void _ast_codegen_output_type(ast_expression_common *self, ir_value *out)
1142 if (out->vtype == TYPE_FIELD)
1143 out->fieldtype = self->next->expression.vtype;
1144 if (out->vtype == TYPE_FUNCTION)
1145 out->outtype = self->next->expression.vtype;
1148 #define codegen_output_type(a,o) (_ast_codegen_output_type(&((a)->expression),(o)))
1150 bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out)
1154 if (self->expression.vtype == TYPE_NIL) {
1155 *out = func->ir_func->owner->nil;
1158 /* NOTE: This is the codegen for a variable used in an expression.
1159 * It is not the codegen to generate the value. For this purpose,
1160 * ast_local_codegen and ast_global_codegen are to be used before this
1161 * is executed. ast_function_codegen should take care of its locals,
1162 * and the ast-user should take care of ast_global_codegen to be used
1163 * on all the globals.
1166 char tname[1024]; /* typename is reserved in C++ */
1167 ast_type_to_string((ast_expression*)self, tname, sizeof(tname));
1168 compile_error(ast_ctx(self), "ast_value used before generated %s %s", tname, self->name);
1175 bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
1179 if (self->expression.vtype == TYPE_NIL) {
1180 compile_error(ast_ctx(self), "internal error: trying to generate a variable of TYPE_NIL");
1184 if (self->hasvalue && self->expression.vtype == TYPE_FUNCTION)
1186 ir_function *func = ir_builder_create_function(ir, self->name, self->expression.next->expression.vtype);
1189 func->context = ast_ctx(self);
1190 func->value->context = ast_ctx(self);
1192 self->constval.vfunc->ir_func = func;
1193 self->ir_v = func->value;
1194 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1195 self->ir_v->flags |= IR_FLAG_INCLUDE_DEF;
1196 /* The function is filled later on ast_function_codegen... */
1200 if (isfield && self->expression.vtype == TYPE_FIELD) {
1201 ast_expression *fieldtype = self->expression.next;
1203 if (self->hasvalue) {
1204 compile_error(ast_ctx(self), "TODO: constant field pointers with value");
1208 if (fieldtype->expression.vtype == TYPE_ARRAY) {
1213 ast_expression_common *elemtype;
1215 ast_value *array = (ast_value*)fieldtype;
1217 if (!ast_istype(fieldtype, ast_value)) {
1218 compile_error(ast_ctx(self), "internal error: ast_value required");
1222 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1223 if (!array->expression.count || array->expression.count > opts.max_array_size)
1224 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)array->expression.count);
1226 elemtype = &array->expression.next->expression;
1227 vtype = elemtype->vtype;
1229 v = ir_builder_create_field(ir, self->name, vtype);
1231 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", self->name);
1234 v->context = ast_ctx(self);
1235 v->unique_life = true;
1237 array->ir_v = self->ir_v = v;
1238 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1239 self->ir_v->flags |= IR_FLAG_INCLUDE_DEF;
1241 namelen = strlen(self->name);
1242 name = (char*)mem_a(namelen + 16);
1243 strcpy(name, self->name);
1245 array->ir_values = (ir_value**)mem_a(sizeof(array->ir_values[0]) * array->expression.count);
1246 array->ir_values[0] = v;
1247 for (ai = 1; ai < array->expression.count; ++ai) {
1248 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1249 array->ir_values[ai] = ir_builder_create_field(ir, name, vtype);
1250 if (!array->ir_values[ai]) {
1252 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", name);
1255 array->ir_values[ai]->context = ast_ctx(self);
1256 array->ir_values[ai]->unique_life = true;
1257 array->ir_values[ai]->locked = true;
1258 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1259 self->ir_values[ai]->flags |= IR_FLAG_INCLUDE_DEF;
1265 v = ir_builder_create_field(ir, self->name, self->expression.next->expression.vtype);
1268 v->context = ast_ctx(self);
1270 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1271 self->ir_v->flags |= IR_FLAG_INCLUDE_DEF;
1276 if (self->expression.vtype == TYPE_ARRAY) {
1281 ast_expression_common *elemtype = &self->expression.next->expression;
1282 int vtype = elemtype->vtype;
1284 /* same as with field arrays */
1285 if (!self->expression.count || self->expression.count > opts.max_array_size)
1286 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1288 v = ir_builder_create_global(ir, self->name, vtype);
1290 compile_error(ast_ctx(self), "ir_builder_create_global failed `%s`", self->name);
1293 v->context = ast_ctx(self);
1294 v->unique_life = true;
1296 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1297 v->flags |= IR_FLAG_INCLUDE_DEF;
1299 namelen = strlen(self->name);
1300 name = (char*)mem_a(namelen + 16);
1301 strcpy(name, self->name);
1303 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1304 self->ir_values[0] = v;
1305 for (ai = 1; ai < self->expression.count; ++ai) {
1306 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1307 self->ir_values[ai] = ir_builder_create_global(ir, name, vtype);
1308 if (!self->ir_values[ai]) {
1310 compile_error(ast_ctx(self), "ir_builder_create_global failed `%s`", name);
1313 self->ir_values[ai]->context = ast_ctx(self);
1314 self->ir_values[ai]->unique_life = true;
1315 self->ir_values[ai]->locked = true;
1316 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1317 self->ir_values[ai]->flags |= IR_FLAG_INCLUDE_DEF;
1323 /* Arrays don't do this since there's no "array" value which spans across the
1326 v = ir_builder_create_global(ir, self->name, self->expression.vtype);
1328 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", self->name);
1331 codegen_output_type(self, v);
1332 v->context = ast_ctx(self);
1335 if (self->hasvalue) {
1336 switch (self->expression.vtype)
1339 if (!ir_value_set_float(v, self->constval.vfloat))
1343 if (!ir_value_set_vector(v, self->constval.vvec))
1347 if (!ir_value_set_string(v, self->constval.vstring))
1351 compile_error(ast_ctx(self), "TODO: global constant array");
1354 compile_error(ast_ctx(self), "global of type function not properly generated");
1356 /* Cannot generate an IR value for a function,
1357 * need a pointer pointing to a function rather.
1360 if (!self->constval.vfield) {
1361 compile_error(ast_ctx(self), "field constant without vfield set");
1364 if (!self->constval.vfield->ir_v) {
1365 compile_error(ast_ctx(self), "field constant generated before its field");
1368 if (!ir_value_set_field(v, self->constval.vfield->ir_v))
1372 compile_error(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1377 /* link us to the ir_value */
1380 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1381 self->ir_v->flags |= IR_FLAG_INCLUDE_DEF;
1384 error: /* clean up */
1389 bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
1393 if (self->expression.vtype == TYPE_NIL) {
1394 compile_error(ast_ctx(self), "internal error: trying to generate a variable of TYPE_NIL");
1398 if (self->hasvalue && self->expression.vtype == TYPE_FUNCTION)
1400 /* Do we allow local functions? I think not...
1401 * this is NOT a function pointer atm.
1406 if (self->expression.vtype == TYPE_ARRAY) {
1411 ast_expression_common *elemtype = &self->expression.next->expression;
1412 int vtype = elemtype->vtype;
1414 func->flags |= IR_FLAG_HAS_ARRAYS;
1416 if (param && !(self->expression.flags & AST_FLAG_IS_VARARG)) {
1417 compile_error(ast_ctx(self), "array-parameters are not supported");
1421 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1422 if (!self->expression.count || self->expression.count > opts.max_array_size) {
1423 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1426 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1427 if (!self->ir_values) {
1428 compile_error(ast_ctx(self), "failed to allocate array values");
1432 v = ir_function_create_local(func, self->name, vtype, param);
1434 compile_error(ast_ctx(self), "ir_function_create_local failed");
1437 v->context = ast_ctx(self);
1438 v->unique_life = true;
1441 namelen = strlen(self->name);
1442 name = (char*)mem_a(namelen + 16);
1443 strcpy(name, self->name);
1445 self->ir_values[0] = v;
1446 for (ai = 1; ai < self->expression.count; ++ai) {
1447 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1448 self->ir_values[ai] = ir_function_create_local(func, name, vtype, param);
1449 if (!self->ir_values[ai]) {
1450 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", name);
1453 self->ir_values[ai]->context = ast_ctx(self);
1454 self->ir_values[ai]->unique_life = true;
1455 self->ir_values[ai]->locked = true;
1460 v = ir_function_create_local(func, self->name, self->expression.vtype, param);
1463 codegen_output_type(self, v);
1464 v->context = ast_ctx(self);
1467 /* A constant local... hmmm...
1468 * I suppose the IR will have to deal with this
1470 if (self->hasvalue) {
1471 switch (self->expression.vtype)
1474 if (!ir_value_set_float(v, self->constval.vfloat))
1478 if (!ir_value_set_vector(v, self->constval.vvec))
1482 if (!ir_value_set_string(v, self->constval.vstring))
1486 compile_error(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1491 /* link us to the ir_value */
1495 if (!ast_generate_accessors(self, func->owner))
1499 error: /* clean up */
1504 bool ast_generate_accessors(ast_value *self, ir_builder *ir)
1507 bool warn = OPTS_WARN(WARN_USED_UNINITIALIZED);
1508 if (!self->setter || !self->getter)
1510 for (i = 0; i < self->expression.count; ++i) {
1511 if (!self->ir_values) {
1512 compile_error(ast_ctx(self), "internal error: no array values generated for `%s`", self->name);
1515 if (!self->ir_values[i]) {
1516 compile_error(ast_ctx(self), "internal error: not all array values have been generated for `%s`", self->name);
1519 if (self->ir_values[i]->life) {
1520 compile_error(ast_ctx(self), "internal error: function containing `%s` already generated", self->name);
1525 opts_set(opts.warn, WARN_USED_UNINITIALIZED, false);
1527 if (!ast_global_codegen (self->setter, ir, false) ||
1528 !ast_function_codegen(self->setter->constval.vfunc, ir) ||
1529 !ir_function_finalize(self->setter->constval.vfunc->ir_func))
1531 compile_error(ast_ctx(self), "internal error: failed to generate setter for `%s`", self->name);
1532 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1537 if (!ast_global_codegen (self->getter, ir, false) ||
1538 !ast_function_codegen(self->getter->constval.vfunc, ir) ||
1539 !ir_function_finalize(self->getter->constval.vfunc->ir_func))
1541 compile_error(ast_ctx(self), "internal error: failed to generate getter for `%s`", self->name);
1542 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1546 for (i = 0; i < self->expression.count; ++i) {
1547 vec_free(self->ir_values[i]->life);
1549 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1553 bool ast_function_codegen(ast_function *self, ir_builder *ir)
1557 ast_expression_common *ec;
1562 irf = self->ir_func;
1564 compile_error(ast_ctx(self), "ast_function's related ast_value was not generated yet");
1568 /* fill the parameter list */
1569 ec = &self->vtype->expression;
1570 for (i = 0; i < vec_size(ec->params); ++i)
1572 if (ec->params[i]->expression.vtype == TYPE_FIELD)
1573 vec_push(irf->params, ec->params[i]->expression.next->expression.vtype);
1575 vec_push(irf->params, ec->params[i]->expression.vtype);
1576 if (!self->builtin) {
1577 if (!ast_local_codegen(ec->params[i], self->ir_func, true))
1582 if (self->varargs) {
1583 if (!ast_local_codegen(self->varargs, self->ir_func, true))
1587 if (self->builtin) {
1588 irf->builtin = self->builtin;
1592 if (!vec_size(self->blocks)) {
1593 compile_error(ast_ctx(self), "function `%s` has no body", self->name);
1597 self->curblock = ir_function_create_block(ast_ctx(self), irf, "entry");
1598 if (!self->curblock) {
1599 compile_error(ast_ctx(self), "failed to allocate entry block for `%s`", self->name);
1603 for (i = 0; i < vec_size(self->blocks); ++i) {
1604 ast_expression_codegen *gen = self->blocks[i]->expression.codegen;
1605 if (!(*gen)((ast_expression*)self->blocks[i], self, false, &dummy))
1609 /* TODO: check return types */
1610 if (!self->curblock->final)
1612 if (!self->vtype->expression.next ||
1613 self->vtype->expression.next->expression.vtype == TYPE_VOID)
1615 return ir_block_create_return(self->curblock, ast_ctx(self), NULL);
1617 else if (vec_size(self->curblock->entries))
1619 /* error("missing return"); */
1620 if (compile_warning(ast_ctx(self), WARN_MISSING_RETURN_VALUES,
1621 "control reaches end of non-void function (`%s`) via %s",
1622 self->name, self->curblock->label))
1626 return ir_block_create_return(self->curblock, ast_ctx(self), NULL);
1632 /* Note, you will not see ast_block_codegen generate ir_blocks.
1633 * To the AST and the IR, blocks are 2 different things.
1634 * In the AST it represents a block of code, usually enclosed in
1635 * curly braces {...}.
1636 * While in the IR it represents a block in terms of control-flow.
1638 bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_value **out)
1642 /* We don't use this
1643 * Note: an ast-representation using the comma-operator
1644 * of the form: (a, b, c) = x should not assign to c...
1647 compile_error(ast_ctx(self), "not an l-value (code-block)");
1651 if (self->expression.outr) {
1652 *out = self->expression.outr;
1656 /* output is NULL at first, we'll have each expression
1657 * assign to out output, thus, a comma-operator represention
1658 * using an ast_block will return the last generated value,
1659 * so: (b, c) + a executed both b and c, and returns c,
1660 * which is then added to a.
1664 /* generate locals */
1665 for (i = 0; i < vec_size(self->locals); ++i)
1667 if (!ast_local_codegen(self->locals[i], func->ir_func, false)) {
1669 compile_error(ast_ctx(self), "failed to generate local `%s`", self->locals[i]->name);
1674 for (i = 0; i < vec_size(self->exprs); ++i)
1676 ast_expression_codegen *gen;
1677 if (func->curblock->final && !ast_istype(self->exprs[i], ast_label)) {
1678 if (compile_warning(ast_ctx(self->exprs[i]), WARN_UNREACHABLE_CODE, "unreachable statement"))
1682 gen = self->exprs[i]->expression.codegen;
1683 if (!(*gen)(self->exprs[i], func, false, out))
1687 self->expression.outr = *out;
1692 bool ast_store_codegen(ast_store *self, ast_function *func, bool lvalue, ir_value **out)
1694 ast_expression_codegen *cgen;
1695 ir_value *left = NULL;
1696 ir_value *right = NULL;
1700 ast_array_index *ai = NULL;
1702 if (lvalue && self->expression.outl) {
1703 *out = self->expression.outl;
1707 if (!lvalue && self->expression.outr) {
1708 *out = self->expression.outr;
1712 if (ast_istype(self->dest, ast_array_index))
1715 ai = (ast_array_index*)self->dest;
1716 idx = (ast_value*)ai->index;
1718 if (ast_istype(ai->index, ast_value) && idx->hasvalue && idx->cvq == CV_CONST)
1723 /* we need to call the setter */
1724 ir_value *iridx, *funval;
1728 compile_error(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1732 arr = (ast_value*)ai->array;
1733 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1734 compile_error(ast_ctx(self), "value has no setter (%s)", arr->name);
1738 cgen = idx->expression.codegen;
1739 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1742 cgen = arr->setter->expression.codegen;
1743 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
1746 cgen = self->source->expression.codegen;
1747 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1750 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "store"), funval, false);
1753 ir_call_param(call, iridx);
1754 ir_call_param(call, right);
1755 self->expression.outr = right;
1761 cgen = self->dest->expression.codegen;
1763 if (!(*cgen)((ast_expression*)(self->dest), func, true, &left))
1765 self->expression.outl = left;
1767 cgen = self->source->expression.codegen;
1769 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1772 if (!ir_block_create_store_op(func->curblock, ast_ctx(self), self->op, left, right))
1774 self->expression.outr = right;
1777 /* Theoretically, an assinment returns its left side as an
1778 * lvalue, if we don't need an lvalue though, we return
1779 * the right side as an rvalue, otherwise we have to
1780 * somehow know whether or not we need to dereference the pointer
1781 * on the left side - that is: OP_LOAD if it was an address.
1782 * Also: in original QC we cannot OP_LOADP *anyway*.
1784 *out = (lvalue ? left : right);
1789 bool ast_binary_codegen(ast_binary *self, ast_function *func, bool lvalue, ir_value **out)
1791 ast_expression_codegen *cgen;
1792 ir_value *left, *right;
1794 /* A binary operation cannot yield an l-value */
1796 compile_error(ast_ctx(self), "not an l-value (binop)");
1800 if (self->expression.outr) {
1801 *out = self->expression.outr;
1805 if ((OPTS_FLAG(SHORT_LOGIC) || OPTS_FLAG(PERL_LOGIC)) &&
1806 (self->op == INSTR_AND || self->op == INSTR_OR))
1808 /* short circuit evaluation */
1809 ir_block *other, *merge;
1810 ir_block *from_left, *from_right;
1814 /* prepare end-block */
1815 merge_id = vec_size(func->ir_func->blocks);
1816 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "sce_merge"));
1818 /* generate the left expression */
1819 cgen = self->left->expression.codegen;
1820 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1822 /* remember the block */
1823 from_left = func->curblock;
1825 /* create a new block for the right expression */
1826 other = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "sce_other"));
1827 if (self->op == INSTR_AND) {
1828 /* on AND: left==true -> other */
1829 if (!ir_block_create_if(func->curblock, ast_ctx(self), left, other, merge))
1832 /* on OR: left==false -> other */
1833 if (!ir_block_create_if(func->curblock, ast_ctx(self), left, merge, other))
1836 /* use the likely flag */
1837 vec_last(func->curblock->instr)->likely = true;
1839 /* enter the right-expression's block */
1840 func->curblock = other;
1842 cgen = self->right->expression.codegen;
1843 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1845 /* remember block */
1846 from_right = func->curblock;
1848 /* jump to the merge block */
1849 if (!ir_block_create_jump(func->curblock, ast_ctx(self), merge))
1852 vec_remove(func->ir_func->blocks, merge_id, 1);
1853 vec_push(func->ir_func->blocks, merge);
1855 func->curblock = merge;
1856 phi = ir_block_create_phi(func->curblock, ast_ctx(self),
1857 ast_function_label(func, "sce_value"),
1858 self->expression.vtype);
1859 ir_phi_add(phi, from_left, left);
1860 ir_phi_add(phi, from_right, right);
1861 *out = ir_phi_value(phi);
1865 if (!OPTS_FLAG(PERL_LOGIC)) {
1867 if (OPTS_FLAG(CORRECT_LOGIC) && (*out)->vtype == TYPE_VECTOR) {
1868 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
1869 ast_function_label(func, "sce_bool_v"),
1873 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
1874 ast_function_label(func, "sce_bool"),
1879 else if (OPTS_FLAG(FALSE_EMPTY_STRINGS) && (*out)->vtype == TYPE_STRING) {
1880 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
1881 ast_function_label(func, "sce_bool_s"),
1885 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
1886 ast_function_label(func, "sce_bool"),
1892 *out = ir_block_create_binop(func->curblock, ast_ctx(self),
1893 ast_function_label(func, "sce_bool"),
1894 INSTR_AND, *out, *out);
1900 self->expression.outr = *out;
1901 codegen_output_type(self, *out);
1905 cgen = self->left->expression.codegen;
1906 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1909 cgen = self->right->expression.codegen;
1910 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1913 *out = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "bin"),
1914 self->op, left, right);
1917 self->expression.outr = *out;
1918 codegen_output_type(self, *out);
1923 bool ast_binstore_codegen(ast_binstore *self, ast_function *func, bool lvalue, ir_value **out)
1925 ast_expression_codegen *cgen;
1926 ir_value *leftl = NULL, *leftr, *right, *bin;
1930 ast_array_index *ai = NULL;
1931 ir_value *iridx = NULL;
1933 if (lvalue && self->expression.outl) {
1934 *out = self->expression.outl;
1938 if (!lvalue && self->expression.outr) {
1939 *out = self->expression.outr;
1943 if (ast_istype(self->dest, ast_array_index))
1946 ai = (ast_array_index*)self->dest;
1947 idx = (ast_value*)ai->index;
1949 if (ast_istype(ai->index, ast_value) && idx->hasvalue && idx->cvq == CV_CONST)
1953 /* for a binstore we need both an lvalue and an rvalue for the left side */
1954 /* rvalue of destination! */
1956 cgen = idx->expression.codegen;
1957 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1960 cgen = self->dest->expression.codegen;
1961 if (!(*cgen)((ast_expression*)(self->dest), func, false, &leftr))
1964 /* source as rvalue only */
1965 cgen = self->source->expression.codegen;
1966 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1969 /* now the binary */
1970 bin = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "binst"),
1971 self->opbin, leftr, right);
1972 self->expression.outr = bin;
1976 /* we need to call the setter */
1981 compile_error(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1985 arr = (ast_value*)ai->array;
1986 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1987 compile_error(ast_ctx(self), "value has no setter (%s)", arr->name);
1991 cgen = arr->setter->expression.codegen;
1992 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
1995 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "store"), funval, false);
1998 ir_call_param(call, iridx);
1999 ir_call_param(call, bin);
2000 self->expression.outr = bin;
2002 /* now store them */
2003 cgen = self->dest->expression.codegen;
2004 /* lvalue of destination */
2005 if (!(*cgen)((ast_expression*)(self->dest), func, true, &leftl))
2007 self->expression.outl = leftl;
2009 if (!ir_block_create_store_op(func->curblock, ast_ctx(self), self->opstore, leftl, bin))
2011 self->expression.outr = bin;
2014 /* Theoretically, an assinment returns its left side as an
2015 * lvalue, if we don't need an lvalue though, we return
2016 * the right side as an rvalue, otherwise we have to
2017 * somehow know whether or not we need to dereference the pointer
2018 * on the left side - that is: OP_LOAD if it was an address.
2019 * Also: in original QC we cannot OP_LOADP *anyway*.
2021 *out = (lvalue ? leftl : bin);
2026 bool ast_unary_codegen(ast_unary *self, ast_function *func, bool lvalue, ir_value **out)
2028 ast_expression_codegen *cgen;
2031 /* An unary operation cannot yield an l-value */
2033 compile_error(ast_ctx(self), "not an l-value (binop)");
2037 if (self->expression.outr) {
2038 *out = self->expression.outr;
2042 cgen = self->operand->expression.codegen;
2044 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
2047 *out = ir_block_create_unary(func->curblock, ast_ctx(self), ast_function_label(func, "unary"),
2051 self->expression.outr = *out;
2056 bool ast_return_codegen(ast_return *self, ast_function *func, bool lvalue, ir_value **out)
2058 ast_expression_codegen *cgen;
2063 /* In the context of a return operation, we don't actually return
2067 compile_error(ast_ctx(self), "return-expression is not an l-value");
2071 if (self->expression.outr) {
2072 compile_error(ast_ctx(self), "internal error: ast_return cannot be reused, it bears no result!");
2075 self->expression.outr = (ir_value*)1;
2077 if (self->operand) {
2078 cgen = self->operand->expression.codegen;
2080 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
2083 if (!ir_block_create_return(func->curblock, ast_ctx(self), operand))
2086 if (!ir_block_create_return(func->curblock, ast_ctx(self), NULL))
2093 bool ast_entfield_codegen(ast_entfield *self, ast_function *func, bool lvalue, ir_value **out)
2095 ast_expression_codegen *cgen;
2096 ir_value *ent, *field;
2098 /* This function needs to take the 'lvalue' flag into account!
2099 * As lvalue we provide a field-pointer, as rvalue we provide the
2103 if (lvalue && self->expression.outl) {
2104 *out = self->expression.outl;
2108 if (!lvalue && self->expression.outr) {
2109 *out = self->expression.outr;
2113 cgen = self->entity->expression.codegen;
2114 if (!(*cgen)((ast_expression*)(self->entity), func, false, &ent))
2117 cgen = self->field->expression.codegen;
2118 if (!(*cgen)((ast_expression*)(self->field), func, false, &field))
2123 *out = ir_block_create_fieldaddress(func->curblock, ast_ctx(self), ast_function_label(func, "efa"),
2126 *out = ir_block_create_load_from_ent(func->curblock, ast_ctx(self), ast_function_label(func, "efv"),
2127 ent, field, self->expression.vtype);
2128 /* Done AFTER error checking:
2129 codegen_output_type(self, *out);
2133 compile_error(ast_ctx(self), "failed to create %s instruction (output type %s)",
2134 (lvalue ? "ADDRESS" : "FIELD"),
2135 type_name[self->expression.vtype]);
2139 codegen_output_type(self, *out);
2142 self->expression.outl = *out;
2144 self->expression.outr = *out;
2146 /* Hm that should be it... */
2150 bool ast_member_codegen(ast_member *self, ast_function *func, bool lvalue, ir_value **out)
2152 ast_expression_codegen *cgen;
2155 /* in QC this is always an lvalue */
2156 if (lvalue && self->rvalue) {
2157 compile_error(ast_ctx(self), "not an l-value (member access)");
2160 if (self->expression.outl) {
2161 *out = self->expression.outl;
2165 cgen = self->owner->expression.codegen;
2166 if (!(*cgen)((ast_expression*)(self->owner), func, false, &vec))
2169 if (vec->vtype != TYPE_VECTOR &&
2170 !(vec->vtype == TYPE_FIELD && self->owner->expression.next->expression.vtype == TYPE_VECTOR))
2175 *out = ir_value_vector_member(vec, self->field);
2176 self->expression.outl = *out;
2178 return (*out != NULL);
2181 bool ast_array_index_codegen(ast_array_index *self, ast_function *func, bool lvalue, ir_value **out)
2186 if (!lvalue && self->expression.outr) {
2187 *out = self->expression.outr;
2189 if (lvalue && self->expression.outl) {
2190 *out = self->expression.outl;
2193 if (!ast_istype(self->array, ast_value)) {
2194 compile_error(ast_ctx(self), "array indexing this way is not supported");
2195 /* note this would actually be pointer indexing because the left side is
2196 * not an actual array but (hopefully) an indexable expression.
2197 * Once we get integer arithmetic, and GADDRESS/GSTORE/GLOAD instruction
2198 * support this path will be filled.
2203 arr = (ast_value*)self->array;
2204 idx = (ast_value*)self->index;
2206 if (!ast_istype(self->index, ast_value) || !idx->hasvalue || idx->cvq != CV_CONST) {
2207 /* Time to use accessor functions */
2208 ast_expression_codegen *cgen;
2209 ir_value *iridx, *funval;
2213 compile_error(ast_ctx(self), "(.2) array indexing here needs a compile-time constant");
2218 compile_error(ast_ctx(self), "value has no getter, don't know how to index it");
2222 cgen = self->index->expression.codegen;
2223 if (!(*cgen)((ast_expression*)(self->index), func, false, &iridx))
2226 cgen = arr->getter->expression.codegen;
2227 if (!(*cgen)((ast_expression*)(arr->getter), func, true, &funval))
2230 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "fetch"), funval, false);
2233 ir_call_param(call, iridx);
2235 *out = ir_call_value(call);
2236 self->expression.outr = *out;
2240 if (idx->expression.vtype == TYPE_FLOAT) {
2241 unsigned int arridx = idx->constval.vfloat;
2242 if (arridx >= self->array->expression.count)
2244 compile_error(ast_ctx(self), "array index out of bounds: %i", arridx);
2247 *out = arr->ir_values[arridx];
2249 else if (idx->expression.vtype == TYPE_INTEGER) {
2250 unsigned int arridx = idx->constval.vint;
2251 if (arridx >= self->array->expression.count)
2253 compile_error(ast_ctx(self), "array index out of bounds: %i", arridx);
2256 *out = arr->ir_values[arridx];
2259 compile_error(ast_ctx(self), "array indexing here needs an integer constant");
2265 bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_value **out)
2267 ast_expression_codegen *cgen;
2275 ir_block *ontrue_endblock = NULL;
2276 ir_block *onfalse_endblock = NULL;
2277 ir_block *merge = NULL;
2279 /* We don't output any value, thus also don't care about r/lvalue */
2283 if (self->expression.outr) {
2284 compile_error(ast_ctx(self), "internal error: ast_ifthen cannot be reused, it bears no result!");
2287 self->expression.outr = (ir_value*)1;
2289 /* generate the condition */
2290 cgen = self->cond->expression.codegen;
2291 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2293 /* update the block which will get the jump - because short-logic or ternaries may have changed this */
2294 cond = func->curblock;
2298 if (self->on_true) {
2299 /* create on-true block */
2300 ontrue = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "ontrue"));
2304 /* enter the block */
2305 func->curblock = ontrue;
2308 cgen = self->on_true->expression.codegen;
2309 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &dummy))
2312 /* we now need to work from the current endpoint */
2313 ontrue_endblock = func->curblock;
2318 if (self->on_false) {
2319 /* create on-false block */
2320 onfalse = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "onfalse"));
2324 /* enter the block */
2325 func->curblock = onfalse;
2328 cgen = self->on_false->expression.codegen;
2329 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &dummy))
2332 /* we now need to work from the current endpoint */
2333 onfalse_endblock = func->curblock;
2337 /* Merge block were they all merge in to */
2338 if (!ontrue || !onfalse || !ontrue_endblock->final || !onfalse_endblock->final)
2340 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "endif"));
2343 /* add jumps ot the merge block */
2344 if (ontrue && !ontrue_endblock->final && !ir_block_create_jump(ontrue_endblock, ast_ctx(self), merge))
2346 if (onfalse && !onfalse_endblock->final && !ir_block_create_jump(onfalse_endblock, ast_ctx(self), merge))
2349 /* Now enter the merge block */
2350 func->curblock = merge;
2353 /* we create the if here, that way all blocks are ordered :)
2355 if (!ir_block_create_if(cond, ast_ctx(self), condval,
2356 (ontrue ? ontrue : merge),
2357 (onfalse ? onfalse : merge)))
2365 bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_value **out)
2367 ast_expression_codegen *cgen;
2370 ir_value *trueval, *falseval;
2373 ir_block *cond = func->curblock;
2374 ir_block *cond_out = NULL;
2375 ir_block *ontrue, *ontrue_out = NULL;
2376 ir_block *onfalse, *onfalse_out = NULL;
2379 /* Ternary can never create an lvalue... */
2383 /* In theory it shouldn't be possible to pass through a node twice, but
2384 * in case we add any kind of optimization pass for the AST itself, it
2385 * may still happen, thus we remember a created ir_value and simply return one
2386 * if it already exists.
2388 if (self->expression.outr) {
2389 *out = self->expression.outr;
2393 /* In the following, contraty to ast_ifthen, we assume both paths exist. */
2395 /* generate the condition */
2396 func->curblock = cond;
2397 cgen = self->cond->expression.codegen;
2398 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2400 cond_out = func->curblock;
2402 /* create on-true block */
2403 ontrue = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_T"));
2408 /* enter the block */
2409 func->curblock = ontrue;
2412 cgen = self->on_true->expression.codegen;
2413 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &trueval))
2416 ontrue_out = func->curblock;
2419 /* create on-false block */
2420 onfalse = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_F"));
2425 /* enter the block */
2426 func->curblock = onfalse;
2429 cgen = self->on_false->expression.codegen;
2430 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &falseval))
2433 onfalse_out = func->curblock;
2436 /* create merge block */
2437 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_out"));
2440 /* jump to merge block */
2441 if (!ir_block_create_jump(ontrue_out, ast_ctx(self), merge))
2443 if (!ir_block_create_jump(onfalse_out, ast_ctx(self), merge))
2446 /* create if instruction */
2447 if (!ir_block_create_if(cond_out, ast_ctx(self), condval, ontrue, onfalse))
2450 /* Now enter the merge block */
2451 func->curblock = merge;
2453 /* Here, now, we need a PHI node
2454 * but first some sanity checking...
2456 if (trueval->vtype != falseval->vtype && trueval->vtype != TYPE_NIL && falseval->vtype != TYPE_NIL) {
2457 /* error("ternary with different types on the two sides"); */
2458 compile_error(ast_ctx(self), "internal error: ternary operand types invalid");
2463 phi = ir_block_create_phi(merge, ast_ctx(self), ast_function_label(func, "phi"), self->expression.vtype);
2465 compile_error(ast_ctx(self), "internal error: failed to generate phi node");
2468 ir_phi_add(phi, ontrue_out, trueval);
2469 ir_phi_add(phi, onfalse_out, falseval);
2471 self->expression.outr = ir_phi_value(phi);
2472 *out = self->expression.outr;
2474 codegen_output_type(self, *out);
2479 bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value **out)
2481 ast_expression_codegen *cgen;
2483 ir_value *dummy = NULL;
2484 ir_value *precond = NULL;
2485 ir_value *postcond = NULL;
2487 /* Since we insert some jumps "late" so we have blocks
2488 * ordered "nicely", we need to keep track of the actual end-blocks
2489 * of expressions to add the jumps to.
2491 ir_block *bbody = NULL, *end_bbody = NULL;
2492 ir_block *bprecond = NULL, *end_bprecond = NULL;
2493 ir_block *bpostcond = NULL, *end_bpostcond = NULL;
2494 ir_block *bincrement = NULL, *end_bincrement = NULL;
2495 ir_block *bout = NULL, *bin = NULL;
2497 /* let's at least move the outgoing block to the end */
2500 /* 'break' and 'continue' need to be able to find the right blocks */
2501 ir_block *bcontinue = NULL;
2502 ir_block *bbreak = NULL;
2504 ir_block *tmpblock = NULL;
2509 if (self->expression.outr) {
2510 compile_error(ast_ctx(self), "internal error: ast_loop cannot be reused, it bears no result!");
2513 self->expression.outr = (ir_value*)1;
2516 * Should we ever need some kind of block ordering, better make this function
2517 * move blocks around than write a block ordering algorithm later... after all
2518 * the ast and ir should work together, not against each other.
2521 /* initexpr doesn't get its own block, it's pointless, it could create more blocks
2522 * anyway if for example it contains a ternary.
2526 cgen = self->initexpr->expression.codegen;
2527 if (!(*cgen)((ast_expression*)(self->initexpr), func, false, &dummy))
2531 /* Store the block from which we enter this chaos */
2532 bin = func->curblock;
2534 /* The pre-loop condition needs its own block since we
2535 * need to be able to jump to the start of that expression.
2539 bprecond = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "pre_loop_cond"));
2543 /* the pre-loop-condition the least important place to 'continue' at */
2544 bcontinue = bprecond;
2547 func->curblock = bprecond;
2550 cgen = self->precond->expression.codegen;
2551 if (!(*cgen)((ast_expression*)(self->precond), func, false, &precond))
2554 end_bprecond = func->curblock;
2556 bprecond = end_bprecond = NULL;
2559 /* Now the next blocks won't be ordered nicely, but we need to
2560 * generate them this early for 'break' and 'continue'.
2562 if (self->increment) {
2563 bincrement = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "loop_increment"));
2566 bcontinue = bincrement; /* increment comes before the pre-loop-condition */
2568 bincrement = end_bincrement = NULL;
2571 if (self->postcond) {
2572 bpostcond = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "post_loop_cond"));
2575 bcontinue = bpostcond; /* postcond comes before the increment */
2577 bpostcond = end_bpostcond = NULL;
2580 bout_id = vec_size(func->ir_func->blocks);
2581 bout = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "after_loop"));
2586 /* The loop body... */
2587 /* if (self->body) */
2589 bbody = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "loop_body"));
2594 func->curblock = bbody;
2596 vec_push(func->breakblocks, bbreak);
2598 vec_push(func->continueblocks, bcontinue);
2600 vec_push(func->continueblocks, bbody);
2604 cgen = self->body->expression.codegen;
2605 if (!(*cgen)((ast_expression*)(self->body), func, false, &dummy))
2609 end_bbody = func->curblock;
2610 vec_pop(func->breakblocks);
2611 vec_pop(func->continueblocks);
2614 /* post-loop-condition */
2618 func->curblock = bpostcond;
2621 cgen = self->postcond->expression.codegen;
2622 if (!(*cgen)((ast_expression*)(self->postcond), func, false, &postcond))
2625 end_bpostcond = func->curblock;
2628 /* The incrementor */
2629 if (self->increment)
2632 func->curblock = bincrement;
2635 cgen = self->increment->expression.codegen;
2636 if (!(*cgen)((ast_expression*)(self->increment), func, false, &dummy))
2639 end_bincrement = func->curblock;
2642 /* In any case now, we continue from the outgoing block */
2643 func->curblock = bout;
2645 /* Now all blocks are in place */
2646 /* From 'bin' we jump to whatever comes first */
2647 if (bprecond) tmpblock = bprecond;
2648 else if (bbody) tmpblock = bbody;
2649 else if (bpostcond) tmpblock = bpostcond;
2650 else tmpblock = bout;
2651 if (!ir_block_create_jump(bin, ast_ctx(self), tmpblock))
2657 ir_block *ontrue, *onfalse;
2658 if (bbody) ontrue = bbody;
2659 else if (bincrement) ontrue = bincrement;
2660 else if (bpostcond) ontrue = bpostcond;
2661 else ontrue = bprecond;
2663 if (self->pre_not) {
2668 if (!ir_block_create_if(end_bprecond, ast_ctx(self), precond, ontrue, onfalse))
2675 if (bincrement) tmpblock = bincrement;
2676 else if (bpostcond) tmpblock = bpostcond;
2677 else if (bprecond) tmpblock = bprecond;
2678 else tmpblock = bbody;
2679 if (!end_bbody->final && !ir_block_create_jump(end_bbody, ast_ctx(self), tmpblock))
2683 /* from increment */
2686 if (bpostcond) tmpblock = bpostcond;
2687 else if (bprecond) tmpblock = bprecond;
2688 else if (bbody) tmpblock = bbody;
2689 else tmpblock = bout;
2690 if (!ir_block_create_jump(end_bincrement, ast_ctx(self), tmpblock))
2697 ir_block *ontrue, *onfalse;
2698 if (bprecond) ontrue = bprecond;
2699 else if (bbody) ontrue = bbody;
2700 else if (bincrement) ontrue = bincrement;
2701 else ontrue = bpostcond;
2703 if (self->post_not) {
2708 if (!ir_block_create_if(end_bpostcond, ast_ctx(self), postcond, ontrue, onfalse))
2712 /* Move 'bout' to the end */
2713 vec_remove(func->ir_func->blocks, bout_id, 1);
2714 vec_push(func->ir_func->blocks, bout);
2719 bool ast_breakcont_codegen(ast_breakcont *self, ast_function *func, bool lvalue, ir_value **out)
2726 compile_error(ast_ctx(self), "break/continue expression is not an l-value");
2730 if (self->expression.outr) {
2731 compile_error(ast_ctx(self), "internal error: ast_breakcont cannot be reused!");
2734 self->expression.outr = (ir_value*)1;
2736 if (self->is_continue)
2737 target = func->continueblocks[vec_size(func->continueblocks)-1-self->levels];
2739 target = func->breakblocks[vec_size(func->breakblocks)-1-self->levels];
2742 compile_error(ast_ctx(self), "%s is lacking a target block", (self->is_continue ? "continue" : "break"));
2746 if (!ir_block_create_jump(func->curblock, ast_ctx(self), target))
2751 bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_value **out)
2753 ast_expression_codegen *cgen;
2755 ast_switch_case *def_case = NULL;
2756 ir_block *def_bfall = NULL;
2757 ir_block *def_bfall_to = NULL;
2758 bool set_def_bfall_to = false;
2760 ir_value *dummy = NULL;
2761 ir_value *irop = NULL;
2762 ir_block *bout = NULL;
2763 ir_block *bfall = NULL;
2771 compile_error(ast_ctx(self), "switch expression is not an l-value");
2775 if (self->expression.outr) {
2776 compile_error(ast_ctx(self), "internal error: ast_switch cannot be reused!");
2779 self->expression.outr = (ir_value*)1;
2784 cgen = self->operand->expression.codegen;
2785 if (!(*cgen)((ast_expression*)(self->operand), func, false, &irop))
2788 if (!vec_size(self->cases))
2791 cmpinstr = type_eq_instr[irop->vtype];
2792 if (cmpinstr >= AINSTR_END) {
2793 ast_type_to_string(self->operand, typestr, sizeof(typestr));
2794 compile_error(ast_ctx(self), "invalid type to perform a switch on: %s", typestr);
2798 bout_id = vec_size(func->ir_func->blocks);
2799 bout = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "after_switch"));
2803 /* setup the break block */
2804 vec_push(func->breakblocks, bout);
2806 /* Now create all cases */
2807 for (c = 0; c < vec_size(self->cases); ++c) {
2808 ir_value *cond, *val;
2809 ir_block *bcase, *bnot;
2812 ast_switch_case *swcase = &self->cases[c];
2814 if (swcase->value) {
2815 /* A regular case */
2816 /* generate the condition operand */
2817 cgen = swcase->value->expression.codegen;
2818 if (!(*cgen)((ast_expression*)(swcase->value), func, false, &val))
2820 /* generate the condition */
2821 cond = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "switch_eq"), cmpinstr, irop, val);
2825 bcase = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "case"));
2826 bnot_id = vec_size(func->ir_func->blocks);
2827 bnot = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "not_case"));
2828 if (!bcase || !bnot)
2830 if (set_def_bfall_to) {
2831 set_def_bfall_to = false;
2832 def_bfall_to = bcase;
2834 if (!ir_block_create_if(func->curblock, ast_ctx(self), cond, bcase, bnot))
2837 /* Make the previous case-end fall through */
2838 if (bfall && !bfall->final) {
2839 if (!ir_block_create_jump(bfall, ast_ctx(self), bcase))
2843 /* enter the case */
2844 func->curblock = bcase;
2845 cgen = swcase->code->expression.codegen;
2846 if (!(*cgen)((ast_expression*)swcase->code, func, false, &dummy))
2849 /* remember this block to fall through from */
2850 bfall = func->curblock;
2852 /* enter the else and move it down */
2853 func->curblock = bnot;
2854 vec_remove(func->ir_func->blocks, bnot_id, 1);
2855 vec_push(func->ir_func->blocks, bnot);
2857 /* The default case */
2858 /* Remember where to fall through from: */
2861 /* remember which case it was */
2863 /* And the next case will be remembered */
2864 set_def_bfall_to = true;
2868 /* Jump from the last bnot to bout */
2869 if (bfall && !bfall->final && !ir_block_create_jump(bfall, ast_ctx(self), bout)) {
2871 astwarning(ast_ctx(bfall), WARN_???, "missing break after last case");
2876 /* If there was a default case, put it down here */
2880 /* No need to create an extra block */
2881 bcase = func->curblock;
2883 /* Insert the fallthrough jump */
2884 if (def_bfall && !def_bfall->final) {
2885 if (!ir_block_create_jump(def_bfall, ast_ctx(self), bcase))
2889 /* Now generate the default code */
2890 cgen = def_case->code->expression.codegen;
2891 if (!(*cgen)((ast_expression*)def_case->code, func, false, &dummy))
2894 /* see if we need to fall through */
2895 if (def_bfall_to && !func->curblock->final)
2897 if (!ir_block_create_jump(func->curblock, ast_ctx(self), def_bfall_to))
2902 /* Jump from the last bnot to bout */
2903 if (!func->curblock->final && !ir_block_create_jump(func->curblock, ast_ctx(self), bout))
2905 /* enter the outgoing block */
2906 func->curblock = bout;
2908 /* restore the break block */
2909 vec_pop(func->breakblocks);
2911 /* Move 'bout' to the end, it's nicer */
2912 vec_remove(func->ir_func->blocks, bout_id, 1);
2913 vec_push(func->ir_func->blocks, bout);
2918 bool ast_label_codegen(ast_label *self, ast_function *func, bool lvalue, ir_value **out)
2923 if (self->undefined) {
2924 compile_error(ast_ctx(self), "internal error: ast_label never defined");
2930 compile_error(ast_ctx(self), "internal error: ast_label cannot be an lvalue");
2934 /* simply create a new block and jump to it */
2935 self->irblock = ir_function_create_block(ast_ctx(self), func->ir_func, self->name);
2936 if (!self->irblock) {
2937 compile_error(ast_ctx(self), "failed to allocate label block `%s`", self->name);
2940 if (!func->curblock->final) {
2941 if (!ir_block_create_jump(func->curblock, ast_ctx(self), self->irblock))
2945 /* enter the new block */
2946 func->curblock = self->irblock;
2948 /* Generate all the leftover gotos */
2949 for (i = 0; i < vec_size(self->gotos); ++i) {
2950 if (!ast_goto_codegen(self->gotos[i], func, false, &dummy))
2957 bool ast_goto_codegen(ast_goto *self, ast_function *func, bool lvalue, ir_value **out)
2961 compile_error(ast_ctx(self), "internal error: ast_goto cannot be an lvalue");
2965 if (self->target->irblock) {
2966 if (self->irblock_from) {
2967 /* we already tried once, this is the callback */
2968 self->irblock_from->final = false;
2969 if (!ir_block_create_goto(self->irblock_from, ast_ctx(self), self->target->irblock)) {
2970 compile_error(ast_ctx(self), "failed to generate goto to `%s`", self->name);
2976 if (!ir_block_create_goto(func->curblock, ast_ctx(self), self->target->irblock)) {
2977 compile_error(ast_ctx(self), "failed to generate goto to `%s`", self->name);
2984 /* the target has not yet been created...
2985 * close this block in a sneaky way:
2987 func->curblock->final = true;
2988 self->irblock_from = func->curblock;
2989 ast_label_register_goto(self->target, self);
2995 bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value **out)
2997 ast_expression_codegen *cgen;
2999 ir_instr *callinstr;
3002 ir_value *funval = NULL;
3004 /* return values are never lvalues */
3006 compile_error(ast_ctx(self), "not an l-value (function call)");
3010 if (self->expression.outr) {
3011 *out = self->expression.outr;
3015 cgen = self->func->expression.codegen;
3016 if (!(*cgen)((ast_expression*)(self->func), func, false, &funval))
3024 for (i = 0; i < vec_size(self->params); ++i)
3027 ast_expression *expr = self->params[i];
3029 cgen = expr->expression.codegen;
3030 if (!(*cgen)(expr, func, false, ¶m))
3034 vec_push(params, param);
3037 /* varargs counter */
3038 if (self->va_count) {
3040 ir_builder *builder = func->curblock->owner->owner;
3041 cgen = self->va_count->expression.codegen;
3042 if (!(*cgen)((ast_expression*)(self->va_count), func, false, &va_count))
3044 if (!ir_block_create_store_op(func->curblock, ast_ctx(self), INSTR_STORE_F,
3045 ir_builder_get_va_count(builder), va_count))
3051 callinstr = ir_block_create_call(func->curblock, ast_ctx(self),
3052 ast_function_label(func, "call"),
3053 funval, !!(self->func->expression.flags & AST_FLAG_NORETURN));
3057 for (i = 0; i < vec_size(params); ++i) {
3058 ir_call_param(callinstr, params[i]);
3061 *out = ir_call_value(callinstr);
3062 self->expression.outr = *out;
3064 codegen_output_type(self, *out);