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)
664 ast_unref(self->array);
666 ast_unref(self->index);
667 ast_expression_delete((ast_expression*)self);
671 ast_ifthen* ast_ifthen_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
673 ast_instantiate(ast_ifthen, ctx, ast_ifthen_delete);
674 if (!ontrue && !onfalse) {
675 /* because it is invalid */
679 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ifthen_codegen);
682 self->on_true = ontrue;
683 self->on_false = onfalse;
684 ast_propagate_effects(self, cond);
686 ast_propagate_effects(self, ontrue);
688 ast_propagate_effects(self, onfalse);
693 void ast_ifthen_delete(ast_ifthen *self)
695 ast_unref(self->cond);
697 ast_unref(self->on_true);
699 ast_unref(self->on_false);
700 ast_expression_delete((ast_expression*)self);
704 ast_ternary* ast_ternary_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
706 ast_expression *exprtype = ontrue;
707 ast_instantiate(ast_ternary, ctx, ast_ternary_delete);
708 /* This time NEITHER must be NULL */
709 if (!ontrue || !onfalse) {
713 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ternary_codegen);
716 self->on_true = ontrue;
717 self->on_false = onfalse;
718 ast_propagate_effects(self, cond);
719 ast_propagate_effects(self, ontrue);
720 ast_propagate_effects(self, onfalse);
722 if (ontrue->expression.vtype == TYPE_NIL)
724 if (!ast_type_adopt(self, exprtype)) {
725 ast_ternary_delete(self);
732 void ast_ternary_delete(ast_ternary *self)
734 /* the if()s are only there because computed-gotos can set them
737 if (self->cond) ast_unref(self->cond);
738 if (self->on_true) ast_unref(self->on_true);
739 if (self->on_false) ast_unref(self->on_false);
740 ast_expression_delete((ast_expression*)self);
744 ast_loop* ast_loop_new(lex_ctx ctx,
745 ast_expression *initexpr,
746 ast_expression *precond, bool pre_not,
747 ast_expression *postcond, bool post_not,
748 ast_expression *increment,
749 ast_expression *body)
751 ast_instantiate(ast_loop, ctx, ast_loop_delete);
752 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_loop_codegen);
754 self->initexpr = initexpr;
755 self->precond = precond;
756 self->postcond = postcond;
757 self->increment = increment;
760 self->pre_not = pre_not;
761 self->post_not = post_not;
764 ast_propagate_effects(self, initexpr);
766 ast_propagate_effects(self, precond);
768 ast_propagate_effects(self, postcond);
770 ast_propagate_effects(self, increment);
772 ast_propagate_effects(self, body);
777 void ast_loop_delete(ast_loop *self)
780 ast_unref(self->initexpr);
782 ast_unref(self->precond);
784 ast_unref(self->postcond);
786 ast_unref(self->increment);
788 ast_unref(self->body);
789 ast_expression_delete((ast_expression*)self);
793 ast_breakcont* ast_breakcont_new(lex_ctx ctx, bool iscont, unsigned int levels)
795 ast_instantiate(ast_breakcont, ctx, ast_breakcont_delete);
796 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_breakcont_codegen);
798 self->is_continue = iscont;
799 self->levels = levels;
804 void ast_breakcont_delete(ast_breakcont *self)
806 ast_expression_delete((ast_expression*)self);
810 ast_switch* ast_switch_new(lex_ctx ctx, ast_expression *op)
812 ast_instantiate(ast_switch, ctx, ast_switch_delete);
813 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_switch_codegen);
818 ast_propagate_effects(self, op);
823 void ast_switch_delete(ast_switch *self)
826 ast_unref(self->operand);
828 for (i = 0; i < vec_size(self->cases); ++i) {
829 if (self->cases[i].value)
830 ast_unref(self->cases[i].value);
831 ast_unref(self->cases[i].code);
833 vec_free(self->cases);
835 ast_expression_delete((ast_expression*)self);
839 ast_label* ast_label_new(lex_ctx ctx, const char *name, bool undefined)
841 ast_instantiate(ast_label, ctx, ast_label_delete);
842 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_label_codegen);
844 self->expression.vtype = TYPE_NOEXPR;
846 self->name = util_strdup(name);
847 self->irblock = NULL;
849 self->undefined = undefined;
854 void ast_label_delete(ast_label *self)
856 mem_d((void*)self->name);
857 vec_free(self->gotos);
858 ast_expression_delete((ast_expression*)self);
862 void ast_label_register_goto(ast_label *self, ast_goto *g)
864 vec_push(self->gotos, g);
867 ast_goto* ast_goto_new(lex_ctx ctx, const char *name)
869 ast_instantiate(ast_goto, ctx, ast_goto_delete);
870 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_goto_codegen);
872 self->name = util_strdup(name);
874 self->irblock_from = NULL;
879 void ast_goto_delete(ast_goto *self)
881 mem_d((void*)self->name);
882 ast_expression_delete((ast_expression*)self);
886 void ast_goto_set_label(ast_goto *self, ast_label *label)
888 self->target = label;
891 ast_call* ast_call_new(lex_ctx ctx,
892 ast_expression *funcexpr)
894 ast_instantiate(ast_call, ctx, ast_call_delete);
895 if (!funcexpr->expression.next) {
896 compile_error(ctx, "not a function");
900 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_call_codegen);
902 ast_side_effects(self) = true;
905 self->func = funcexpr;
906 self->va_count = NULL;
908 ast_type_adopt(self, funcexpr->expression.next);
913 void ast_call_delete(ast_call *self)
916 for (i = 0; i < vec_size(self->params); ++i)
917 ast_unref(self->params[i]);
918 vec_free(self->params);
921 ast_unref(self->func);
924 ast_unref(self->va_count);
926 ast_expression_delete((ast_expression*)self);
930 bool ast_call_check_types(ast_call *self)
936 const ast_expression *func = self->func;
937 size_t count = vec_size(self->params);
938 if (count > vec_size(func->expression.params))
939 count = vec_size(func->expression.params);
941 for (i = 0; i < count; ++i) {
942 if (!ast_compare_type(self->params[i], (ast_expression*)(func->expression.params[i])))
944 ast_type_to_string(self->params[i], tgot, sizeof(tgot));
945 ast_type_to_string((ast_expression*)func->expression.params[i], texp, sizeof(texp));
946 compile_error(ast_ctx(self), "invalid type for parameter %u in function call: expected %s, got %s",
947 (unsigned int)(i+1), texp, tgot);
948 /* we don't immediately return */
952 count = vec_size(self->params);
953 if (count > vec_size(func->expression.params) && func->expression.varparam) {
954 for (; i < count; ++i) {
955 if (!ast_compare_type(self->params[i], func->expression.varparam))
957 ast_type_to_string(self->params[i], tgot, sizeof(tgot));
958 ast_type_to_string(func->expression.varparam, texp, sizeof(texp));
959 compile_error(ast_ctx(self), "invalid type for parameter %u in function call: expected %s, got %s",
960 (unsigned int)(i+1), texp, tgot);
961 /* we don't immediately return */
969 ast_store* ast_store_new(lex_ctx ctx, int op,
970 ast_expression *dest, ast_expression *source)
972 ast_instantiate(ast_store, ctx, ast_store_delete);
973 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_store_codegen);
975 ast_side_effects(self) = true;
979 self->source = source;
981 if (!ast_type_adopt(self, dest)) {
989 void ast_store_delete(ast_store *self)
991 ast_unref(self->dest);
992 ast_unref(self->source);
993 ast_expression_delete((ast_expression*)self);
997 ast_block* ast_block_new(lex_ctx ctx)
999 ast_instantiate(ast_block, ctx, ast_block_delete);
1000 ast_expression_init((ast_expression*)self,
1001 (ast_expression_codegen*)&ast_block_codegen);
1003 self->locals = NULL;
1005 self->collect = NULL;
1010 bool ast_block_add_expr(ast_block *self, ast_expression *e)
1012 ast_propagate_effects(self, e);
1013 vec_push(self->exprs, e);
1014 if (self->expression.next) {
1015 ast_delete(self->expression.next);
1016 self->expression.next = NULL;
1018 if (!ast_type_adopt(self, e)) {
1019 compile_error(ast_ctx(self), "internal error: failed to adopt type");
1025 void ast_block_collect(ast_block *self, ast_expression *expr)
1027 vec_push(self->collect, expr);
1028 expr->expression.node.keep = true;
1031 void ast_block_delete(ast_block *self)
1034 for (i = 0; i < vec_size(self->exprs); ++i)
1035 ast_unref(self->exprs[i]);
1036 vec_free(self->exprs);
1037 for (i = 0; i < vec_size(self->locals); ++i)
1038 ast_delete(self->locals[i]);
1039 vec_free(self->locals);
1040 for (i = 0; i < vec_size(self->collect); ++i)
1041 ast_delete(self->collect[i]);
1042 vec_free(self->collect);
1043 ast_expression_delete((ast_expression*)self);
1047 bool ast_block_set_type(ast_block *self, ast_expression *from)
1049 if (self->expression.next)
1050 ast_delete(self->expression.next);
1051 if (!ast_type_adopt(self, from))
1056 ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype)
1058 ast_instantiate(ast_function, ctx, ast_function_delete);
1062 vtype->expression.vtype != TYPE_FUNCTION)
1064 compile_error(ast_ctx(self), "internal error: ast_function_new condition %i %i type=%i (probably 2 bodies?)",
1066 (int)vtype->hasvalue,
1067 vtype->expression.vtype);
1072 self->vtype = vtype;
1073 self->name = name ? util_strdup(name) : NULL;
1074 self->blocks = NULL;
1076 self->labelcount = 0;
1079 self->ir_func = NULL;
1080 self->curblock = NULL;
1082 self->breakblocks = NULL;
1083 self->continueblocks = NULL;
1085 vtype->hasvalue = true;
1086 vtype->constval.vfunc = self;
1088 self->varargs = NULL;
1090 self->fixedparams = NULL;
1095 void ast_function_delete(ast_function *self)
1099 mem_d((void*)self->name);
1101 /* ast_value_delete(self->vtype); */
1102 self->vtype->hasvalue = false;
1103 self->vtype->constval.vfunc = NULL;
1104 /* We use unref - if it was stored in a global table it is supposed
1105 * to be deleted from *there*
1107 ast_unref(self->vtype);
1109 for (i = 0; i < vec_size(self->blocks); ++i)
1110 ast_delete(self->blocks[i]);
1111 vec_free(self->blocks);
1112 vec_free(self->breakblocks);
1113 vec_free(self->continueblocks);
1115 ast_delete(self->varargs);
1117 ast_delete(self->argc);
1118 if (self->fixedparams)
1119 ast_unref(self->fixedparams);
1123 const char* ast_function_label(ast_function *self, const char *prefix)
1129 if (!OPTS_OPTION_BOOL(OPTION_DUMP) &&
1130 !OPTS_OPTION_BOOL(OPTION_DUMPFIN) &&
1131 !OPTS_OPTION_BOOL(OPTION_DEBUG))
1136 id = (self->labelcount++);
1137 len = strlen(prefix);
1139 from = self->labelbuf + sizeof(self->labelbuf)-1;
1142 *from-- = (id%10) + '0';
1146 memcpy(from - len, prefix, len);
1150 /*********************************************************************/
1152 * by convention you must never pass NULL to the 'ir_value **out'
1153 * parameter. If you really don't care about the output, pass a dummy.
1154 * But I can't imagine a pituation where the output is truly unnecessary.
1157 void _ast_codegen_output_type(ast_expression_common *self, ir_value *out)
1159 if (out->vtype == TYPE_FIELD)
1160 out->fieldtype = self->next->expression.vtype;
1161 if (out->vtype == TYPE_FUNCTION)
1162 out->outtype = self->next->expression.vtype;
1165 #define codegen_output_type(a,o) (_ast_codegen_output_type(&((a)->expression),(o)))
1167 bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out)
1171 if (self->expression.vtype == TYPE_NIL) {
1172 *out = func->ir_func->owner->nil;
1175 /* NOTE: This is the codegen for a variable used in an expression.
1176 * It is not the codegen to generate the value. For this purpose,
1177 * ast_local_codegen and ast_global_codegen are to be used before this
1178 * is executed. ast_function_codegen should take care of its locals,
1179 * and the ast-user should take care of ast_global_codegen to be used
1180 * on all the globals.
1183 char tname[1024]; /* typename is reserved in C++ */
1184 ast_type_to_string((ast_expression*)self, tname, sizeof(tname));
1185 compile_error(ast_ctx(self), "ast_value used before generated %s %s", tname, self->name);
1192 bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
1196 if (self->expression.vtype == TYPE_NIL) {
1197 compile_error(ast_ctx(self), "internal error: trying to generate a variable of TYPE_NIL");
1201 if (self->hasvalue && self->expression.vtype == TYPE_FUNCTION)
1203 ir_function *func = ir_builder_create_function(ir, self->name, self->expression.next->expression.vtype);
1206 func->context = ast_ctx(self);
1207 func->value->context = ast_ctx(self);
1209 self->constval.vfunc->ir_func = func;
1210 self->ir_v = func->value;
1211 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1212 self->ir_v->flags |= IR_FLAG_INCLUDE_DEF;
1213 /* The function is filled later on ast_function_codegen... */
1217 if (isfield && self->expression.vtype == TYPE_FIELD) {
1218 ast_expression *fieldtype = self->expression.next;
1220 if (self->hasvalue) {
1221 compile_error(ast_ctx(self), "TODO: constant field pointers with value");
1225 if (fieldtype->expression.vtype == TYPE_ARRAY) {
1230 ast_expression_common *elemtype;
1232 ast_value *array = (ast_value*)fieldtype;
1234 if (!ast_istype(fieldtype, ast_value)) {
1235 compile_error(ast_ctx(self), "internal error: ast_value required");
1239 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1240 if (!array->expression.count || array->expression.count > OPTS_OPTION_U32(OPTION_MAX_ARRAY_SIZE))
1241 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)array->expression.count);
1243 elemtype = &array->expression.next->expression;
1244 vtype = elemtype->vtype;
1246 v = ir_builder_create_field(ir, self->name, vtype);
1248 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", self->name);
1251 v->context = ast_ctx(self);
1252 v->unique_life = true;
1254 array->ir_v = self->ir_v = v;
1255 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1256 self->ir_v->flags |= IR_FLAG_INCLUDE_DEF;
1258 namelen = strlen(self->name);
1259 name = (char*)mem_a(namelen + 16);
1260 strcpy(name, self->name);
1262 array->ir_values = (ir_value**)mem_a(sizeof(array->ir_values[0]) * array->expression.count);
1263 array->ir_values[0] = v;
1264 for (ai = 1; ai < array->expression.count; ++ai) {
1265 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1266 array->ir_values[ai] = ir_builder_create_field(ir, name, vtype);
1267 if (!array->ir_values[ai]) {
1269 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", name);
1272 array->ir_values[ai]->context = ast_ctx(self);
1273 array->ir_values[ai]->unique_life = true;
1274 array->ir_values[ai]->locked = true;
1275 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1276 self->ir_values[ai]->flags |= IR_FLAG_INCLUDE_DEF;
1282 v = ir_builder_create_field(ir, self->name, self->expression.next->expression.vtype);
1285 v->context = ast_ctx(self);
1287 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1288 self->ir_v->flags |= IR_FLAG_INCLUDE_DEF;
1293 if (self->expression.vtype == TYPE_ARRAY) {
1298 ast_expression_common *elemtype = &self->expression.next->expression;
1299 int vtype = elemtype->vtype;
1301 /* same as with field arrays */
1302 if (!self->expression.count || self->expression.count > OPTS_OPTION_U32(OPTION_MAX_ARRAY_SIZE))
1303 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1305 v = ir_builder_create_global(ir, self->name, vtype);
1307 compile_error(ast_ctx(self), "ir_builder_create_global failed `%s`", self->name);
1310 v->context = ast_ctx(self);
1311 v->unique_life = true;
1313 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1314 v->flags |= IR_FLAG_INCLUDE_DEF;
1316 namelen = strlen(self->name);
1317 name = (char*)mem_a(namelen + 16);
1318 strcpy(name, self->name);
1320 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1321 self->ir_values[0] = v;
1322 for (ai = 1; ai < self->expression.count; ++ai) {
1323 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1324 self->ir_values[ai] = ir_builder_create_global(ir, name, vtype);
1325 if (!self->ir_values[ai]) {
1327 compile_error(ast_ctx(self), "ir_builder_create_global failed `%s`", name);
1330 self->ir_values[ai]->context = ast_ctx(self);
1331 self->ir_values[ai]->unique_life = true;
1332 self->ir_values[ai]->locked = true;
1333 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1334 self->ir_values[ai]->flags |= IR_FLAG_INCLUDE_DEF;
1340 /* Arrays don't do this since there's no "array" value which spans across the
1343 v = ir_builder_create_global(ir, self->name, self->expression.vtype);
1345 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", self->name);
1348 codegen_output_type(self, v);
1349 v->context = ast_ctx(self);
1352 if (self->hasvalue) {
1353 switch (self->expression.vtype)
1356 if (!ir_value_set_float(v, self->constval.vfloat))
1360 if (!ir_value_set_vector(v, self->constval.vvec))
1364 if (!ir_value_set_string(v, self->constval.vstring))
1368 compile_error(ast_ctx(self), "TODO: global constant array");
1371 compile_error(ast_ctx(self), "global of type function not properly generated");
1373 /* Cannot generate an IR value for a function,
1374 * need a pointer pointing to a function rather.
1377 if (!self->constval.vfield) {
1378 compile_error(ast_ctx(self), "field constant without vfield set");
1381 if (!self->constval.vfield->ir_v) {
1382 compile_error(ast_ctx(self), "field constant generated before its field");
1385 if (!ir_value_set_field(v, self->constval.vfield->ir_v))
1389 compile_error(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1394 /* link us to the ir_value */
1397 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1398 self->ir_v->flags |= IR_FLAG_INCLUDE_DEF;
1401 error: /* clean up */
1406 bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
1410 if (self->expression.vtype == TYPE_NIL) {
1411 compile_error(ast_ctx(self), "internal error: trying to generate a variable of TYPE_NIL");
1415 if (self->hasvalue && self->expression.vtype == TYPE_FUNCTION)
1417 /* Do we allow local functions? I think not...
1418 * this is NOT a function pointer atm.
1423 if (self->expression.vtype == TYPE_ARRAY) {
1428 ast_expression_common *elemtype = &self->expression.next->expression;
1429 int vtype = elemtype->vtype;
1431 func->flags |= IR_FLAG_HAS_ARRAYS;
1433 if (param && !(self->expression.flags & AST_FLAG_IS_VARARG)) {
1434 compile_error(ast_ctx(self), "array-parameters are not supported");
1438 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1439 if (!self->expression.count || self->expression.count > OPTS_OPTION_U32(OPTION_MAX_ARRAY_SIZE)) {
1440 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1443 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1444 if (!self->ir_values) {
1445 compile_error(ast_ctx(self), "failed to allocate array values");
1449 v = ir_function_create_local(func, self->name, vtype, param);
1451 compile_error(ast_ctx(self), "ir_function_create_local failed");
1454 v->context = ast_ctx(self);
1455 v->unique_life = true;
1458 namelen = strlen(self->name);
1459 name = (char*)mem_a(namelen + 16);
1460 strcpy(name, self->name);
1462 self->ir_values[0] = v;
1463 for (ai = 1; ai < self->expression.count; ++ai) {
1464 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1465 self->ir_values[ai] = ir_function_create_local(func, name, vtype, param);
1466 if (!self->ir_values[ai]) {
1467 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", name);
1470 self->ir_values[ai]->context = ast_ctx(self);
1471 self->ir_values[ai]->unique_life = true;
1472 self->ir_values[ai]->locked = true;
1477 v = ir_function_create_local(func, self->name, self->expression.vtype, param);
1480 codegen_output_type(self, v);
1481 v->context = ast_ctx(self);
1484 /* A constant local... hmmm...
1485 * I suppose the IR will have to deal with this
1487 if (self->hasvalue) {
1488 switch (self->expression.vtype)
1491 if (!ir_value_set_float(v, self->constval.vfloat))
1495 if (!ir_value_set_vector(v, self->constval.vvec))
1499 if (!ir_value_set_string(v, self->constval.vstring))
1503 compile_error(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1508 /* link us to the ir_value */
1512 if (!ast_generate_accessors(self, func->owner))
1516 error: /* clean up */
1521 bool ast_generate_accessors(ast_value *self, ir_builder *ir)
1524 bool warn = OPTS_WARN(WARN_USED_UNINITIALIZED);
1525 if (!self->setter || !self->getter)
1527 for (i = 0; i < self->expression.count; ++i) {
1528 if (!self->ir_values) {
1529 compile_error(ast_ctx(self), "internal error: no array values generated for `%s`", self->name);
1532 if (!self->ir_values[i]) {
1533 compile_error(ast_ctx(self), "internal error: not all array values have been generated for `%s`", self->name);
1536 if (self->ir_values[i]->life) {
1537 compile_error(ast_ctx(self), "internal error: function containing `%s` already generated", self->name);
1542 opts_set(opts.warn, WARN_USED_UNINITIALIZED, false);
1544 if (!ast_global_codegen (self->setter, ir, false) ||
1545 !ast_function_codegen(self->setter->constval.vfunc, ir) ||
1546 !ir_function_finalize(self->setter->constval.vfunc->ir_func))
1548 compile_error(ast_ctx(self), "internal error: failed to generate setter for `%s`", self->name);
1549 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1554 if (!ast_global_codegen (self->getter, ir, false) ||
1555 !ast_function_codegen(self->getter->constval.vfunc, ir) ||
1556 !ir_function_finalize(self->getter->constval.vfunc->ir_func))
1558 compile_error(ast_ctx(self), "internal error: failed to generate getter for `%s`", self->name);
1559 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1563 for (i = 0; i < self->expression.count; ++i) {
1564 vec_free(self->ir_values[i]->life);
1566 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1570 bool ast_function_codegen(ast_function *self, ir_builder *ir)
1574 ast_expression_common *ec;
1575 ast_expression_codegen *cgen;
1580 irf = self->ir_func;
1582 compile_error(ast_ctx(self), "ast_function's related ast_value was not generated yet");
1586 /* fill the parameter list */
1587 ec = &self->vtype->expression;
1588 for (i = 0; i < vec_size(ec->params); ++i)
1590 if (ec->params[i]->expression.vtype == TYPE_FIELD)
1591 vec_push(irf->params, ec->params[i]->expression.next->expression.vtype);
1593 vec_push(irf->params, ec->params[i]->expression.vtype);
1594 if (!self->builtin) {
1595 if (!ast_local_codegen(ec->params[i], self->ir_func, true))
1600 if (self->varargs) {
1601 if (!ast_local_codegen(self->varargs, self->ir_func, true))
1603 irf->max_varargs = self->varargs->expression.count;
1606 if (self->builtin) {
1607 irf->builtin = self->builtin;
1611 if (!vec_size(self->blocks)) {
1612 compile_error(ast_ctx(self), "function `%s` has no body", self->name);
1616 irf->first = self->curblock = ir_function_create_block(ast_ctx(self), irf, "entry");
1617 if (!self->curblock) {
1618 compile_error(ast_ctx(self), "failed to allocate entry block for `%s`", self->name);
1626 if (!ast_local_codegen(self->argc, self->ir_func, true))
1628 cgen = self->argc->expression.codegen;
1629 if (!(*cgen)((ast_expression*)(self->argc), self, false, &va_count))
1631 cgen = self->fixedparams->expression.codegen;
1632 if (!(*cgen)((ast_expression*)(self->fixedparams), self, false, &fixed))
1634 sub = ir_block_create_binop(self->curblock, ast_ctx(self),
1635 ast_function_label(self, "va_count"), INSTR_SUB_F,
1636 ir_builder_get_va_count(ir), fixed);
1639 if (!ir_block_create_store_op(self->curblock, ast_ctx(self), INSTR_STORE_F,
1646 for (i = 0; i < vec_size(self->blocks); ++i) {
1647 cgen = self->blocks[i]->expression.codegen;
1648 if (!(*cgen)((ast_expression*)self->blocks[i], self, false, &dummy))
1652 /* TODO: check return types */
1653 if (!self->curblock->final)
1655 if (!self->vtype->expression.next ||
1656 self->vtype->expression.next->expression.vtype == TYPE_VOID)
1658 return ir_block_create_return(self->curblock, ast_ctx(self), NULL);
1660 else if (vec_size(self->curblock->entries) || self->curblock == irf->first)
1662 /* error("missing return"); */
1663 if (compile_warning(ast_ctx(self), WARN_MISSING_RETURN_VALUES,
1664 "control reaches end of non-void function (`%s`) via %s",
1665 self->name, self->curblock->label))
1669 return ir_block_create_return(self->curblock, ast_ctx(self), NULL);
1675 /* Note, you will not see ast_block_codegen generate ir_blocks.
1676 * To the AST and the IR, blocks are 2 different things.
1677 * In the AST it represents a block of code, usually enclosed in
1678 * curly braces {...}.
1679 * While in the IR it represents a block in terms of control-flow.
1681 bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_value **out)
1685 /* We don't use this
1686 * Note: an ast-representation using the comma-operator
1687 * of the form: (a, b, c) = x should not assign to c...
1690 compile_error(ast_ctx(self), "not an l-value (code-block)");
1694 if (self->expression.outr) {
1695 *out = self->expression.outr;
1699 /* output is NULL at first, we'll have each expression
1700 * assign to out output, thus, a comma-operator represention
1701 * using an ast_block will return the last generated value,
1702 * so: (b, c) + a executed both b and c, and returns c,
1703 * which is then added to a.
1707 /* generate locals */
1708 for (i = 0; i < vec_size(self->locals); ++i)
1710 if (!ast_local_codegen(self->locals[i], func->ir_func, false)) {
1711 if (OPTS_OPTION_BOOL(OPTION_DEBUG))
1712 compile_error(ast_ctx(self), "failed to generate local `%s`", self->locals[i]->name);
1717 for (i = 0; i < vec_size(self->exprs); ++i)
1719 ast_expression_codegen *gen;
1720 if (func->curblock->final && !ast_istype(self->exprs[i], ast_label)) {
1721 if (compile_warning(ast_ctx(self->exprs[i]), WARN_UNREACHABLE_CODE, "unreachable statement"))
1725 gen = self->exprs[i]->expression.codegen;
1726 if (!(*gen)(self->exprs[i], func, false, out))
1730 self->expression.outr = *out;
1735 bool ast_store_codegen(ast_store *self, ast_function *func, bool lvalue, ir_value **out)
1737 ast_expression_codegen *cgen;
1738 ir_value *left = NULL;
1739 ir_value *right = NULL;
1743 ast_array_index *ai = NULL;
1745 if (lvalue && self->expression.outl) {
1746 *out = self->expression.outl;
1750 if (!lvalue && self->expression.outr) {
1751 *out = self->expression.outr;
1755 if (ast_istype(self->dest, ast_array_index))
1758 ai = (ast_array_index*)self->dest;
1759 idx = (ast_value*)ai->index;
1761 if (ast_istype(ai->index, ast_value) && idx->hasvalue && idx->cvq == CV_CONST)
1766 /* we need to call the setter */
1767 ir_value *iridx, *funval;
1771 compile_error(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1775 arr = (ast_value*)ai->array;
1776 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1777 compile_error(ast_ctx(self), "value has no setter (%s)", arr->name);
1781 cgen = idx->expression.codegen;
1782 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1785 cgen = arr->setter->expression.codegen;
1786 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
1789 cgen = self->source->expression.codegen;
1790 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1793 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "store"), funval, false);
1796 ir_call_param(call, iridx);
1797 ir_call_param(call, right);
1798 self->expression.outr = right;
1804 cgen = self->dest->expression.codegen;
1806 if (!(*cgen)((ast_expression*)(self->dest), func, true, &left))
1808 self->expression.outl = left;
1810 cgen = self->source->expression.codegen;
1812 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1815 if (!ir_block_create_store_op(func->curblock, ast_ctx(self), self->op, left, right))
1817 self->expression.outr = right;
1820 /* Theoretically, an assinment returns its left side as an
1821 * lvalue, if we don't need an lvalue though, we return
1822 * the right side as an rvalue, otherwise we have to
1823 * somehow know whether or not we need to dereference the pointer
1824 * on the left side - that is: OP_LOAD if it was an address.
1825 * Also: in original QC we cannot OP_LOADP *anyway*.
1827 *out = (lvalue ? left : right);
1832 bool ast_binary_codegen(ast_binary *self, ast_function *func, bool lvalue, ir_value **out)
1834 ast_expression_codegen *cgen;
1835 ir_value *left, *right;
1837 /* A binary operation cannot yield an l-value */
1839 compile_error(ast_ctx(self), "not an l-value (binop)");
1843 if (self->expression.outr) {
1844 *out = self->expression.outr;
1848 if ((OPTS_FLAG(SHORT_LOGIC) || OPTS_FLAG(PERL_LOGIC)) &&
1849 (self->op == INSTR_AND || self->op == INSTR_OR))
1851 /* short circuit evaluation */
1852 ir_block *other, *merge;
1853 ir_block *from_left, *from_right;
1857 /* prepare end-block */
1858 merge_id = vec_size(func->ir_func->blocks);
1859 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "sce_merge"));
1861 /* generate the left expression */
1862 cgen = self->left->expression.codegen;
1863 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1865 /* remember the block */
1866 from_left = func->curblock;
1868 /* create a new block for the right expression */
1869 other = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "sce_other"));
1870 if (self->op == INSTR_AND) {
1871 /* on AND: left==true -> other */
1872 if (!ir_block_create_if(func->curblock, ast_ctx(self), left, other, merge))
1875 /* on OR: left==false -> other */
1876 if (!ir_block_create_if(func->curblock, ast_ctx(self), left, merge, other))
1879 /* use the likely flag */
1880 vec_last(func->curblock->instr)->likely = true;
1882 /* enter the right-expression's block */
1883 func->curblock = other;
1885 cgen = self->right->expression.codegen;
1886 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1888 /* remember block */
1889 from_right = func->curblock;
1891 /* jump to the merge block */
1892 if (!ir_block_create_jump(func->curblock, ast_ctx(self), merge))
1895 vec_remove(func->ir_func->blocks, merge_id, 1);
1896 vec_push(func->ir_func->blocks, merge);
1898 func->curblock = merge;
1899 phi = ir_block_create_phi(func->curblock, ast_ctx(self),
1900 ast_function_label(func, "sce_value"),
1901 self->expression.vtype);
1902 ir_phi_add(phi, from_left, left);
1903 ir_phi_add(phi, from_right, right);
1904 *out = ir_phi_value(phi);
1908 if (!OPTS_FLAG(PERL_LOGIC)) {
1910 if (OPTS_FLAG(CORRECT_LOGIC) && (*out)->vtype == TYPE_VECTOR) {
1911 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
1912 ast_function_label(func, "sce_bool_v"),
1916 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
1917 ast_function_label(func, "sce_bool"),
1922 else if (OPTS_FLAG(FALSE_EMPTY_STRINGS) && (*out)->vtype == TYPE_STRING) {
1923 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
1924 ast_function_label(func, "sce_bool_s"),
1928 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
1929 ast_function_label(func, "sce_bool"),
1935 *out = ir_block_create_binop(func->curblock, ast_ctx(self),
1936 ast_function_label(func, "sce_bool"),
1937 INSTR_AND, *out, *out);
1943 self->expression.outr = *out;
1944 codegen_output_type(self, *out);
1948 cgen = self->left->expression.codegen;
1949 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1952 cgen = self->right->expression.codegen;
1953 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1956 *out = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "bin"),
1957 self->op, left, right);
1960 self->expression.outr = *out;
1961 codegen_output_type(self, *out);
1966 bool ast_binstore_codegen(ast_binstore *self, ast_function *func, bool lvalue, ir_value **out)
1968 ast_expression_codegen *cgen;
1969 ir_value *leftl = NULL, *leftr, *right, *bin;
1973 ast_array_index *ai = NULL;
1974 ir_value *iridx = NULL;
1976 if (lvalue && self->expression.outl) {
1977 *out = self->expression.outl;
1981 if (!lvalue && self->expression.outr) {
1982 *out = self->expression.outr;
1986 if (ast_istype(self->dest, ast_array_index))
1989 ai = (ast_array_index*)self->dest;
1990 idx = (ast_value*)ai->index;
1992 if (ast_istype(ai->index, ast_value) && idx->hasvalue && idx->cvq == CV_CONST)
1996 /* for a binstore we need both an lvalue and an rvalue for the left side */
1997 /* rvalue of destination! */
1999 cgen = idx->expression.codegen;
2000 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
2003 cgen = self->dest->expression.codegen;
2004 if (!(*cgen)((ast_expression*)(self->dest), func, false, &leftr))
2007 /* source as rvalue only */
2008 cgen = self->source->expression.codegen;
2009 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
2012 /* now the binary */
2013 bin = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "binst"),
2014 self->opbin, leftr, right);
2015 self->expression.outr = bin;
2019 /* we need to call the setter */
2024 compile_error(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
2028 arr = (ast_value*)ai->array;
2029 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
2030 compile_error(ast_ctx(self), "value has no setter (%s)", arr->name);
2034 cgen = arr->setter->expression.codegen;
2035 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
2038 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "store"), funval, false);
2041 ir_call_param(call, iridx);
2042 ir_call_param(call, bin);
2043 self->expression.outr = bin;
2045 /* now store them */
2046 cgen = self->dest->expression.codegen;
2047 /* lvalue of destination */
2048 if (!(*cgen)((ast_expression*)(self->dest), func, true, &leftl))
2050 self->expression.outl = leftl;
2052 if (!ir_block_create_store_op(func->curblock, ast_ctx(self), self->opstore, leftl, bin))
2054 self->expression.outr = bin;
2057 /* Theoretically, an assinment returns its left side as an
2058 * lvalue, if we don't need an lvalue though, we return
2059 * the right side as an rvalue, otherwise we have to
2060 * somehow know whether or not we need to dereference the pointer
2061 * on the left side - that is: OP_LOAD if it was an address.
2062 * Also: in original QC we cannot OP_LOADP *anyway*.
2064 *out = (lvalue ? leftl : bin);
2069 bool ast_unary_codegen(ast_unary *self, ast_function *func, bool lvalue, ir_value **out)
2071 ast_expression_codegen *cgen;
2074 /* An unary operation cannot yield an l-value */
2076 compile_error(ast_ctx(self), "not an l-value (binop)");
2080 if (self->expression.outr) {
2081 *out = self->expression.outr;
2085 cgen = self->operand->expression.codegen;
2087 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
2090 *out = ir_block_create_unary(func->curblock, ast_ctx(self), ast_function_label(func, "unary"),
2094 self->expression.outr = *out;
2099 bool ast_return_codegen(ast_return *self, ast_function *func, bool lvalue, ir_value **out)
2101 ast_expression_codegen *cgen;
2106 /* In the context of a return operation, we don't actually return
2110 compile_error(ast_ctx(self), "return-expression is not an l-value");
2114 if (self->expression.outr) {
2115 compile_error(ast_ctx(self), "internal error: ast_return cannot be reused, it bears no result!");
2118 self->expression.outr = (ir_value*)1;
2120 if (self->operand) {
2121 cgen = self->operand->expression.codegen;
2123 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
2126 if (!ir_block_create_return(func->curblock, ast_ctx(self), operand))
2129 if (!ir_block_create_return(func->curblock, ast_ctx(self), NULL))
2136 bool ast_entfield_codegen(ast_entfield *self, ast_function *func, bool lvalue, ir_value **out)
2138 ast_expression_codegen *cgen;
2139 ir_value *ent, *field;
2141 /* This function needs to take the 'lvalue' flag into account!
2142 * As lvalue we provide a field-pointer, as rvalue we provide the
2146 if (lvalue && self->expression.outl) {
2147 *out = self->expression.outl;
2151 if (!lvalue && self->expression.outr) {
2152 *out = self->expression.outr;
2156 cgen = self->entity->expression.codegen;
2157 if (!(*cgen)((ast_expression*)(self->entity), func, false, &ent))
2160 cgen = self->field->expression.codegen;
2161 if (!(*cgen)((ast_expression*)(self->field), func, false, &field))
2166 *out = ir_block_create_fieldaddress(func->curblock, ast_ctx(self), ast_function_label(func, "efa"),
2169 *out = ir_block_create_load_from_ent(func->curblock, ast_ctx(self), ast_function_label(func, "efv"),
2170 ent, field, self->expression.vtype);
2171 /* Done AFTER error checking:
2172 codegen_output_type(self, *out);
2176 compile_error(ast_ctx(self), "failed to create %s instruction (output type %s)",
2177 (lvalue ? "ADDRESS" : "FIELD"),
2178 type_name[self->expression.vtype]);
2182 codegen_output_type(self, *out);
2185 self->expression.outl = *out;
2187 self->expression.outr = *out;
2189 /* Hm that should be it... */
2193 bool ast_member_codegen(ast_member *self, ast_function *func, bool lvalue, ir_value **out)
2195 ast_expression_codegen *cgen;
2198 /* in QC this is always an lvalue */
2199 if (lvalue && self->rvalue) {
2200 compile_error(ast_ctx(self), "not an l-value (member access)");
2203 if (self->expression.outl) {
2204 *out = self->expression.outl;
2208 cgen = self->owner->expression.codegen;
2209 if (!(*cgen)((ast_expression*)(self->owner), func, false, &vec))
2212 if (vec->vtype != TYPE_VECTOR &&
2213 !(vec->vtype == TYPE_FIELD && self->owner->expression.next->expression.vtype == TYPE_VECTOR))
2218 *out = ir_value_vector_member(vec, self->field);
2219 self->expression.outl = *out;
2221 return (*out != NULL);
2224 bool ast_array_index_codegen(ast_array_index *self, ast_function *func, bool lvalue, ir_value **out)
2229 if (!lvalue && self->expression.outr) {
2230 *out = self->expression.outr;
2232 if (lvalue && self->expression.outl) {
2233 *out = self->expression.outl;
2236 if (!ast_istype(self->array, ast_value)) {
2237 compile_error(ast_ctx(self), "array indexing this way is not supported");
2238 /* note this would actually be pointer indexing because the left side is
2239 * not an actual array but (hopefully) an indexable expression.
2240 * Once we get integer arithmetic, and GADDRESS/GSTORE/GLOAD instruction
2241 * support this path will be filled.
2246 arr = (ast_value*)self->array;
2247 idx = (ast_value*)self->index;
2249 if (!ast_istype(self->index, ast_value) || !idx->hasvalue || idx->cvq != CV_CONST) {
2250 /* Time to use accessor functions */
2251 ast_expression_codegen *cgen;
2252 ir_value *iridx, *funval;
2256 compile_error(ast_ctx(self), "(.2) array indexing here needs a compile-time constant");
2261 compile_error(ast_ctx(self), "value has no getter, don't know how to index it");
2265 cgen = self->index->expression.codegen;
2266 if (!(*cgen)((ast_expression*)(self->index), func, false, &iridx))
2269 cgen = arr->getter->expression.codegen;
2270 if (!(*cgen)((ast_expression*)(arr->getter), func, true, &funval))
2273 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "fetch"), funval, false);
2276 ir_call_param(call, iridx);
2278 *out = ir_call_value(call);
2279 self->expression.outr = *out;
2280 (*out)->vtype = self->expression.vtype;
2281 codegen_output_type(self, *out);
2285 if (idx->expression.vtype == TYPE_FLOAT) {
2286 unsigned int arridx = idx->constval.vfloat;
2287 if (arridx >= self->array->expression.count)
2289 compile_error(ast_ctx(self), "array index out of bounds: %i", arridx);
2292 *out = arr->ir_values[arridx];
2294 else if (idx->expression.vtype == TYPE_INTEGER) {
2295 unsigned int arridx = idx->constval.vint;
2296 if (arridx >= self->array->expression.count)
2298 compile_error(ast_ctx(self), "array index out of bounds: %i", arridx);
2301 *out = arr->ir_values[arridx];
2304 compile_error(ast_ctx(self), "array indexing here needs an integer constant");
2307 (*out)->vtype = self->expression.vtype;
2308 codegen_output_type(self, *out);
2312 bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_value **out)
2314 ast_expression_codegen *cgen;
2322 ir_block *ontrue_endblock = NULL;
2323 ir_block *onfalse_endblock = NULL;
2324 ir_block *merge = NULL;
2326 /* We don't output any value, thus also don't care about r/lvalue */
2330 if (self->expression.outr) {
2331 compile_error(ast_ctx(self), "internal error: ast_ifthen cannot be reused, it bears no result!");
2334 self->expression.outr = (ir_value*)1;
2336 /* generate the condition */
2337 cgen = self->cond->expression.codegen;
2338 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2340 /* update the block which will get the jump - because short-logic or ternaries may have changed this */
2341 cond = func->curblock;
2345 if (self->on_true) {
2346 /* create on-true block */
2347 ontrue = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "ontrue"));
2351 /* enter the block */
2352 func->curblock = ontrue;
2355 cgen = self->on_true->expression.codegen;
2356 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &dummy))
2359 /* we now need to work from the current endpoint */
2360 ontrue_endblock = func->curblock;
2365 if (self->on_false) {
2366 /* create on-false block */
2367 onfalse = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "onfalse"));
2371 /* enter the block */
2372 func->curblock = onfalse;
2375 cgen = self->on_false->expression.codegen;
2376 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &dummy))
2379 /* we now need to work from the current endpoint */
2380 onfalse_endblock = func->curblock;
2384 /* Merge block were they all merge in to */
2385 if (!ontrue || !onfalse || !ontrue_endblock->final || !onfalse_endblock->final)
2387 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "endif"));
2390 /* add jumps ot the merge block */
2391 if (ontrue && !ontrue_endblock->final && !ir_block_create_jump(ontrue_endblock, ast_ctx(self), merge))
2393 if (onfalse && !onfalse_endblock->final && !ir_block_create_jump(onfalse_endblock, ast_ctx(self), merge))
2396 /* Now enter the merge block */
2397 func->curblock = merge;
2400 /* we create the if here, that way all blocks are ordered :)
2402 if (!ir_block_create_if(cond, ast_ctx(self), condval,
2403 (ontrue ? ontrue : merge),
2404 (onfalse ? onfalse : merge)))
2412 bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_value **out)
2414 ast_expression_codegen *cgen;
2417 ir_value *trueval, *falseval;
2420 ir_block *cond = func->curblock;
2421 ir_block *cond_out = NULL;
2422 ir_block *ontrue, *ontrue_out = NULL;
2423 ir_block *onfalse, *onfalse_out = NULL;
2426 /* Ternary can never create an lvalue... */
2430 /* In theory it shouldn't be possible to pass through a node twice, but
2431 * in case we add any kind of optimization pass for the AST itself, it
2432 * may still happen, thus we remember a created ir_value and simply return one
2433 * if it already exists.
2435 if (self->expression.outr) {
2436 *out = self->expression.outr;
2440 /* In the following, contraty to ast_ifthen, we assume both paths exist. */
2442 /* generate the condition */
2443 func->curblock = cond;
2444 cgen = self->cond->expression.codegen;
2445 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2447 cond_out = func->curblock;
2449 /* create on-true block */
2450 ontrue = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_T"));
2455 /* enter the block */
2456 func->curblock = ontrue;
2459 cgen = self->on_true->expression.codegen;
2460 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &trueval))
2463 ontrue_out = func->curblock;
2466 /* create on-false block */
2467 onfalse = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_F"));
2472 /* enter the block */
2473 func->curblock = onfalse;
2476 cgen = self->on_false->expression.codegen;
2477 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &falseval))
2480 onfalse_out = func->curblock;
2483 /* create merge block */
2484 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_out"));
2487 /* jump to merge block */
2488 if (!ir_block_create_jump(ontrue_out, ast_ctx(self), merge))
2490 if (!ir_block_create_jump(onfalse_out, ast_ctx(self), merge))
2493 /* create if instruction */
2494 if (!ir_block_create_if(cond_out, ast_ctx(self), condval, ontrue, onfalse))
2497 /* Now enter the merge block */
2498 func->curblock = merge;
2500 /* Here, now, we need a PHI node
2501 * but first some sanity checking...
2503 if (trueval->vtype != falseval->vtype && trueval->vtype != TYPE_NIL && falseval->vtype != TYPE_NIL) {
2504 /* error("ternary with different types on the two sides"); */
2505 compile_error(ast_ctx(self), "internal error: ternary operand types invalid");
2510 phi = ir_block_create_phi(merge, ast_ctx(self), ast_function_label(func, "phi"), self->expression.vtype);
2512 compile_error(ast_ctx(self), "internal error: failed to generate phi node");
2515 ir_phi_add(phi, ontrue_out, trueval);
2516 ir_phi_add(phi, onfalse_out, falseval);
2518 self->expression.outr = ir_phi_value(phi);
2519 *out = self->expression.outr;
2521 codegen_output_type(self, *out);
2526 bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value **out)
2528 ast_expression_codegen *cgen;
2530 ir_value *dummy = NULL;
2531 ir_value *precond = NULL;
2532 ir_value *postcond = NULL;
2534 /* Since we insert some jumps "late" so we have blocks
2535 * ordered "nicely", we need to keep track of the actual end-blocks
2536 * of expressions to add the jumps to.
2538 ir_block *bbody = NULL, *end_bbody = NULL;
2539 ir_block *bprecond = NULL, *end_bprecond = NULL;
2540 ir_block *bpostcond = NULL, *end_bpostcond = NULL;
2541 ir_block *bincrement = NULL, *end_bincrement = NULL;
2542 ir_block *bout = NULL, *bin = NULL;
2544 /* let's at least move the outgoing block to the end */
2547 /* 'break' and 'continue' need to be able to find the right blocks */
2548 ir_block *bcontinue = NULL;
2549 ir_block *bbreak = NULL;
2551 ir_block *tmpblock = NULL;
2556 if (self->expression.outr) {
2557 compile_error(ast_ctx(self), "internal error: ast_loop cannot be reused, it bears no result!");
2560 self->expression.outr = (ir_value*)1;
2563 * Should we ever need some kind of block ordering, better make this function
2564 * move blocks around than write a block ordering algorithm later... after all
2565 * the ast and ir should work together, not against each other.
2568 /* initexpr doesn't get its own block, it's pointless, it could create more blocks
2569 * anyway if for example it contains a ternary.
2573 cgen = self->initexpr->expression.codegen;
2574 if (!(*cgen)((ast_expression*)(self->initexpr), func, false, &dummy))
2578 /* Store the block from which we enter this chaos */
2579 bin = func->curblock;
2581 /* The pre-loop condition needs its own block since we
2582 * need to be able to jump to the start of that expression.
2586 bprecond = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "pre_loop_cond"));
2590 /* the pre-loop-condition the least important place to 'continue' at */
2591 bcontinue = bprecond;
2594 func->curblock = bprecond;
2597 cgen = self->precond->expression.codegen;
2598 if (!(*cgen)((ast_expression*)(self->precond), func, false, &precond))
2601 end_bprecond = func->curblock;
2603 bprecond = end_bprecond = NULL;
2606 /* Now the next blocks won't be ordered nicely, but we need to
2607 * generate them this early for 'break' and 'continue'.
2609 if (self->increment) {
2610 bincrement = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "loop_increment"));
2613 bcontinue = bincrement; /* increment comes before the pre-loop-condition */
2615 bincrement = end_bincrement = NULL;
2618 if (self->postcond) {
2619 bpostcond = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "post_loop_cond"));
2622 bcontinue = bpostcond; /* postcond comes before the increment */
2624 bpostcond = end_bpostcond = NULL;
2627 bout_id = vec_size(func->ir_func->blocks);
2628 bout = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "after_loop"));
2633 /* The loop body... */
2634 /* if (self->body) */
2636 bbody = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "loop_body"));
2641 func->curblock = bbody;
2643 vec_push(func->breakblocks, bbreak);
2645 vec_push(func->continueblocks, bcontinue);
2647 vec_push(func->continueblocks, bbody);
2651 cgen = self->body->expression.codegen;
2652 if (!(*cgen)((ast_expression*)(self->body), func, false, &dummy))
2656 end_bbody = func->curblock;
2657 vec_pop(func->breakblocks);
2658 vec_pop(func->continueblocks);
2661 /* post-loop-condition */
2665 func->curblock = bpostcond;
2668 cgen = self->postcond->expression.codegen;
2669 if (!(*cgen)((ast_expression*)(self->postcond), func, false, &postcond))
2672 end_bpostcond = func->curblock;
2675 /* The incrementor */
2676 if (self->increment)
2679 func->curblock = bincrement;
2682 cgen = self->increment->expression.codegen;
2683 if (!(*cgen)((ast_expression*)(self->increment), func, false, &dummy))
2686 end_bincrement = func->curblock;
2689 /* In any case now, we continue from the outgoing block */
2690 func->curblock = bout;
2692 /* Now all blocks are in place */
2693 /* From 'bin' we jump to whatever comes first */
2694 if (bprecond) tmpblock = bprecond;
2695 else if (bbody) tmpblock = bbody;
2696 else if (bpostcond) tmpblock = bpostcond;
2697 else tmpblock = bout;
2698 if (!ir_block_create_jump(bin, ast_ctx(self), tmpblock))
2704 ir_block *ontrue, *onfalse;
2705 if (bbody) ontrue = bbody;
2706 else if (bincrement) ontrue = bincrement;
2707 else if (bpostcond) ontrue = bpostcond;
2708 else ontrue = bprecond;
2710 if (self->pre_not) {
2715 if (!ir_block_create_if(end_bprecond, ast_ctx(self), precond, ontrue, onfalse))
2722 if (bincrement) tmpblock = bincrement;
2723 else if (bpostcond) tmpblock = bpostcond;
2724 else if (bprecond) tmpblock = bprecond;
2725 else tmpblock = bbody;
2726 if (!end_bbody->final && !ir_block_create_jump(end_bbody, ast_ctx(self), tmpblock))
2730 /* from increment */
2733 if (bpostcond) tmpblock = bpostcond;
2734 else if (bprecond) tmpblock = bprecond;
2735 else if (bbody) tmpblock = bbody;
2736 else tmpblock = bout;
2737 if (!ir_block_create_jump(end_bincrement, ast_ctx(self), tmpblock))
2744 ir_block *ontrue, *onfalse;
2745 if (bprecond) ontrue = bprecond;
2746 else if (bbody) ontrue = bbody;
2747 else if (bincrement) ontrue = bincrement;
2748 else ontrue = bpostcond;
2750 if (self->post_not) {
2755 if (!ir_block_create_if(end_bpostcond, ast_ctx(self), postcond, ontrue, onfalse))
2759 /* Move 'bout' to the end */
2760 vec_remove(func->ir_func->blocks, bout_id, 1);
2761 vec_push(func->ir_func->blocks, bout);
2766 bool ast_breakcont_codegen(ast_breakcont *self, ast_function *func, bool lvalue, ir_value **out)
2773 compile_error(ast_ctx(self), "break/continue expression is not an l-value");
2777 if (self->expression.outr) {
2778 compile_error(ast_ctx(self), "internal error: ast_breakcont cannot be reused!");
2781 self->expression.outr = (ir_value*)1;
2783 if (self->is_continue)
2784 target = func->continueblocks[vec_size(func->continueblocks)-1-self->levels];
2786 target = func->breakblocks[vec_size(func->breakblocks)-1-self->levels];
2789 compile_error(ast_ctx(self), "%s is lacking a target block", (self->is_continue ? "continue" : "break"));
2793 if (!ir_block_create_jump(func->curblock, ast_ctx(self), target))
2798 bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_value **out)
2800 ast_expression_codegen *cgen;
2802 ast_switch_case *def_case = NULL;
2803 ir_block *def_bfall = NULL;
2804 ir_block *def_bfall_to = NULL;
2805 bool set_def_bfall_to = false;
2807 ir_value *dummy = NULL;
2808 ir_value *irop = NULL;
2809 ir_block *bout = NULL;
2810 ir_block *bfall = NULL;
2818 compile_error(ast_ctx(self), "switch expression is not an l-value");
2822 if (self->expression.outr) {
2823 compile_error(ast_ctx(self), "internal error: ast_switch cannot be reused!");
2826 self->expression.outr = (ir_value*)1;
2831 cgen = self->operand->expression.codegen;
2832 if (!(*cgen)((ast_expression*)(self->operand), func, false, &irop))
2835 if (!vec_size(self->cases))
2838 cmpinstr = type_eq_instr[irop->vtype];
2839 if (cmpinstr >= VINSTR_END) {
2840 ast_type_to_string(self->operand, typestr, sizeof(typestr));
2841 compile_error(ast_ctx(self), "invalid type to perform a switch on: %s", typestr);
2845 bout_id = vec_size(func->ir_func->blocks);
2846 bout = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "after_switch"));
2850 /* setup the break block */
2851 vec_push(func->breakblocks, bout);
2853 /* Now create all cases */
2854 for (c = 0; c < vec_size(self->cases); ++c) {
2855 ir_value *cond, *val;
2856 ir_block *bcase, *bnot;
2859 ast_switch_case *swcase = &self->cases[c];
2861 if (swcase->value) {
2862 /* A regular case */
2863 /* generate the condition operand */
2864 cgen = swcase->value->expression.codegen;
2865 if (!(*cgen)((ast_expression*)(swcase->value), func, false, &val))
2867 /* generate the condition */
2868 cond = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "switch_eq"), cmpinstr, irop, val);
2872 bcase = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "case"));
2873 bnot_id = vec_size(func->ir_func->blocks);
2874 bnot = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "not_case"));
2875 if (!bcase || !bnot)
2877 if (set_def_bfall_to) {
2878 set_def_bfall_to = false;
2879 def_bfall_to = bcase;
2881 if (!ir_block_create_if(func->curblock, ast_ctx(self), cond, bcase, bnot))
2884 /* Make the previous case-end fall through */
2885 if (bfall && !bfall->final) {
2886 if (!ir_block_create_jump(bfall, ast_ctx(self), bcase))
2890 /* enter the case */
2891 func->curblock = bcase;
2892 cgen = swcase->code->expression.codegen;
2893 if (!(*cgen)((ast_expression*)swcase->code, func, false, &dummy))
2896 /* remember this block to fall through from */
2897 bfall = func->curblock;
2899 /* enter the else and move it down */
2900 func->curblock = bnot;
2901 vec_remove(func->ir_func->blocks, bnot_id, 1);
2902 vec_push(func->ir_func->blocks, bnot);
2904 /* The default case */
2905 /* Remember where to fall through from: */
2908 /* remember which case it was */
2910 /* And the next case will be remembered */
2911 set_def_bfall_to = true;
2915 /* Jump from the last bnot to bout */
2916 if (bfall && !bfall->final && !ir_block_create_jump(bfall, ast_ctx(self), bout)) {
2918 astwarning(ast_ctx(bfall), WARN_???, "missing break after last case");
2923 /* If there was a default case, put it down here */
2927 /* No need to create an extra block */
2928 bcase = func->curblock;
2930 /* Insert the fallthrough jump */
2931 if (def_bfall && !def_bfall->final) {
2932 if (!ir_block_create_jump(def_bfall, ast_ctx(self), bcase))
2936 /* Now generate the default code */
2937 cgen = def_case->code->expression.codegen;
2938 if (!(*cgen)((ast_expression*)def_case->code, func, false, &dummy))
2941 /* see if we need to fall through */
2942 if (def_bfall_to && !func->curblock->final)
2944 if (!ir_block_create_jump(func->curblock, ast_ctx(self), def_bfall_to))
2949 /* Jump from the last bnot to bout */
2950 if (!func->curblock->final && !ir_block_create_jump(func->curblock, ast_ctx(self), bout))
2952 /* enter the outgoing block */
2953 func->curblock = bout;
2955 /* restore the break block */
2956 vec_pop(func->breakblocks);
2958 /* Move 'bout' to the end, it's nicer */
2959 vec_remove(func->ir_func->blocks, bout_id, 1);
2960 vec_push(func->ir_func->blocks, bout);
2965 bool ast_label_codegen(ast_label *self, ast_function *func, bool lvalue, ir_value **out)
2970 if (self->undefined) {
2971 compile_error(ast_ctx(self), "internal error: ast_label never defined");
2977 compile_error(ast_ctx(self), "internal error: ast_label cannot be an lvalue");
2981 /* simply create a new block and jump to it */
2982 self->irblock = ir_function_create_block(ast_ctx(self), func->ir_func, self->name);
2983 if (!self->irblock) {
2984 compile_error(ast_ctx(self), "failed to allocate label block `%s`", self->name);
2987 if (!func->curblock->final) {
2988 if (!ir_block_create_jump(func->curblock, ast_ctx(self), self->irblock))
2992 /* enter the new block */
2993 func->curblock = self->irblock;
2995 /* Generate all the leftover gotos */
2996 for (i = 0; i < vec_size(self->gotos); ++i) {
2997 if (!ast_goto_codegen(self->gotos[i], func, false, &dummy))
3004 bool ast_goto_codegen(ast_goto *self, ast_function *func, bool lvalue, ir_value **out)
3008 compile_error(ast_ctx(self), "internal error: ast_goto cannot be an lvalue");
3012 if (self->target->irblock) {
3013 if (self->irblock_from) {
3014 /* we already tried once, this is the callback */
3015 self->irblock_from->final = false;
3016 if (!ir_block_create_goto(self->irblock_from, ast_ctx(self), self->target->irblock)) {
3017 compile_error(ast_ctx(self), "failed to generate goto to `%s`", self->name);
3023 if (!ir_block_create_goto(func->curblock, ast_ctx(self), self->target->irblock)) {
3024 compile_error(ast_ctx(self), "failed to generate goto to `%s`", self->name);
3031 /* the target has not yet been created...
3032 * close this block in a sneaky way:
3034 func->curblock->final = true;
3035 self->irblock_from = func->curblock;
3036 ast_label_register_goto(self->target, self);
3042 bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value **out)
3044 ast_expression_codegen *cgen;
3046 ir_instr *callinstr;
3049 ir_value *funval = NULL;
3051 /* return values are never lvalues */
3053 compile_error(ast_ctx(self), "not an l-value (function call)");
3057 if (self->expression.outr) {
3058 *out = self->expression.outr;
3062 cgen = self->func->expression.codegen;
3063 if (!(*cgen)((ast_expression*)(self->func), func, false, &funval))
3071 for (i = 0; i < vec_size(self->params); ++i)
3074 ast_expression *expr = self->params[i];
3076 cgen = expr->expression.codegen;
3077 if (!(*cgen)(expr, func, false, ¶m))
3081 vec_push(params, param);
3084 /* varargs counter */
3085 if (self->va_count) {
3087 ir_builder *builder = func->curblock->owner->owner;
3088 cgen = self->va_count->expression.codegen;
3089 if (!(*cgen)((ast_expression*)(self->va_count), func, false, &va_count))
3091 if (!ir_block_create_store_op(func->curblock, ast_ctx(self), INSTR_STORE_F,
3092 ir_builder_get_va_count(builder), va_count))
3098 callinstr = ir_block_create_call(func->curblock, ast_ctx(self),
3099 ast_function_label(func, "call"),
3100 funval, !!(self->func->expression.flags & AST_FLAG_NORETURN));
3104 for (i = 0; i < vec_size(params); ++i) {
3105 ir_call_param(callinstr, params[i]);
3108 *out = ir_call_value(callinstr);
3109 self->expression.outr = *out;
3111 codegen_output_type(self, *out);