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
30 #define ast_instantiate(T, ctx, destroyfn) \
31 T* self = (T*)mem_a(sizeof(T)); \
35 ast_node_init((ast_node*)self, ctx, TYPE_##T); \
36 ( (ast_node*)self )->destroy = (ast_node_delete*)destroyfn
39 * forward declarations, these need not be in ast.h for obvious
42 static bool ast_member_codegen(ast_member*, ast_function*, bool lvalue, ir_value**);
43 static void ast_array_index_delete(ast_array_index*);
44 static bool ast_array_index_codegen(ast_array_index*, ast_function*, bool lvalue, ir_value**);
45 static void ast_store_delete(ast_store*);
46 static bool ast_store_codegen(ast_store*, ast_function*, bool lvalue, ir_value**);
47 static void ast_ifthen_delete(ast_ifthen*);
48 static bool ast_ifthen_codegen(ast_ifthen*, ast_function*, bool lvalue, ir_value**);
49 static void ast_ternary_delete(ast_ternary*);
50 static bool ast_ternary_codegen(ast_ternary*, ast_function*, bool lvalue, ir_value**);
51 static void ast_loop_delete(ast_loop*);
52 static bool ast_loop_codegen(ast_loop*, ast_function*, bool lvalue, ir_value**);
53 static void ast_breakcont_delete(ast_breakcont*);
54 static bool ast_breakcont_codegen(ast_breakcont*, ast_function*, bool lvalue, ir_value**);
55 static void ast_switch_delete(ast_switch*);
56 static bool ast_switch_codegen(ast_switch*, ast_function*, bool lvalue, ir_value**);
57 static void ast_label_delete(ast_label*);
58 static void ast_label_register_goto(ast_label*, ast_goto*);
59 static bool ast_label_codegen(ast_label*, ast_function*, bool lvalue, ir_value**);
60 static bool ast_goto_codegen(ast_goto*, ast_function*, bool lvalue, ir_value**);
61 static void ast_goto_delete(ast_goto*);
62 static void ast_call_delete(ast_call*);
63 static bool ast_call_codegen(ast_call*, ast_function*, bool lvalue, ir_value**);
64 static bool ast_block_codegen(ast_block*, ast_function*, bool lvalue, ir_value**);
65 static void ast_unary_delete(ast_unary*);
66 static bool ast_unary_codegen(ast_unary*, ast_function*, bool lvalue, ir_value**);
67 static void ast_entfield_delete(ast_entfield*);
68 static bool ast_entfield_codegen(ast_entfield*, ast_function*, bool lvalue, ir_value**);
69 static void ast_return_delete(ast_return*);
70 static bool ast_return_codegen(ast_return*, ast_function*, bool lvalue, ir_value**);
71 static void ast_binstore_delete(ast_binstore*);
72 static bool ast_binstore_codegen(ast_binstore*, ast_function*, bool lvalue, ir_value**);
73 static void ast_binary_delete(ast_binary*);
74 static bool ast_binary_codegen(ast_binary*, ast_function*, bool lvalue, ir_value**);
76 /* It must not be possible to get here. */
77 static GMQCC_NORETURN void _ast_node_destroy(ast_node *self)
80 con_err("ast node missing destroy()\n");
84 /* Initialize main ast node aprts */
85 static void ast_node_init(ast_node *self, lex_ctx ctx, int nodetype)
88 self->destroy = &_ast_node_destroy;
90 self->nodetype = nodetype;
91 self->side_effects = false;
94 /* weight and side effects */
95 static void _ast_propagate_effects(ast_node *self, ast_node *other)
97 if (ast_side_effects(other))
98 ast_side_effects(self) = true;
100 #define ast_propagate_effects(s,o) _ast_propagate_effects(((ast_node*)(s)), ((ast_node*)(o)))
102 /* General expression initialization */
103 static void ast_expression_init(ast_expression *self,
104 ast_expression_codegen *codegen)
106 self->codegen = codegen;
107 self->vtype = TYPE_VOID;
114 self->varparam = NULL;
117 static void ast_expression_delete(ast_expression *self)
121 ast_delete(self->next);
122 for (i = 0; i < vec_size(self->params); ++i) {
123 ast_delete(self->params[i]);
125 vec_free(self->params);
127 ast_delete(self->varparam);
130 static void ast_expression_delete_full(ast_expression *self)
132 ast_expression_delete(self);
136 ast_value* ast_value_copy(const ast_value *self)
139 const ast_expression *fromex;
140 ast_expression *selfex;
141 ast_value *cp = ast_value_new(self->expression.node.context, self->name, self->expression.vtype);
142 if (self->expression.next) {
143 cp->expression.next = ast_type_copy(self->expression.node.context, self->expression.next);
145 fromex = &self->expression;
146 selfex = &cp->expression;
147 selfex->count = fromex->count;
148 selfex->flags = fromex->flags;
149 for (i = 0; i < vec_size(fromex->params); ++i) {
150 ast_value *v = ast_value_copy(fromex->params[i]);
151 vec_push(selfex->params, v);
156 void ast_type_adopt_impl(ast_expression *self, const ast_expression *other)
159 const ast_expression *fromex;
160 ast_expression *selfex;
161 self->vtype = other->vtype;
163 self->next = (ast_expression*)ast_type_copy(ast_ctx(self), other->next);
167 selfex->count = fromex->count;
168 selfex->flags = fromex->flags;
169 for (i = 0; i < vec_size(fromex->params); ++i) {
170 ast_value *v = ast_value_copy(fromex->params[i]);
171 vec_push(selfex->params, v);
175 static ast_expression* ast_shallow_type(lex_ctx ctx, int vtype)
177 ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
178 ast_expression_init(self, NULL);
179 self->codegen = NULL;
185 ast_expression* ast_type_copy(lex_ctx ctx, const ast_expression *ex)
188 const ast_expression *fromex;
189 ast_expression *selfex;
195 ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
196 ast_expression_init(self, NULL);
201 /* This may never be codegen()d */
202 selfex->codegen = NULL;
204 selfex->vtype = fromex->vtype;
206 selfex->next = ast_type_copy(ctx, fromex->next);
210 selfex->count = fromex->count;
211 selfex->flags = fromex->flags;
212 for (i = 0; i < vec_size(fromex->params); ++i) {
213 ast_value *v = ast_value_copy(fromex->params[i]);
214 vec_push(selfex->params, v);
221 bool ast_compare_type(ast_expression *a, ast_expression *b)
223 if (a->vtype == TYPE_NIL ||
224 b->vtype == TYPE_NIL)
226 if (a->vtype != b->vtype)
228 if (!a->next != !b->next)
230 if (vec_size(a->params) != vec_size(b->params))
232 if ((a->flags & AST_FLAG_TYPE_MASK) !=
233 (b->flags & AST_FLAG_TYPE_MASK) )
237 if (vec_size(a->params)) {
239 for (i = 0; i < vec_size(a->params); ++i) {
240 if (!ast_compare_type((ast_expression*)a->params[i],
241 (ast_expression*)b->params[i]))
246 return ast_compare_type(a->next, b->next);
250 static size_t ast_type_to_string_impl(ast_expression *e, char *buf, size_t bufsize, size_t pos)
257 if (pos + 6 >= bufsize)
259 util_strncpy(buf + pos, "(null)", 6);
263 if (pos + 1 >= bufsize)
268 util_strncpy(buf + pos, "(variant)", 9);
273 return ast_type_to_string_impl(e->next, buf, bufsize, pos);
276 if (pos + 3 >= bufsize)
280 pos = ast_type_to_string_impl(e->next, buf, bufsize, pos);
281 if (pos + 1 >= bufsize)
287 pos = ast_type_to_string_impl(e->next, buf, bufsize, pos);
288 if (pos + 2 >= bufsize)
290 if (!vec_size(e->params)) {
296 pos = ast_type_to_string_impl((ast_expression*)(e->params[0]), buf, bufsize, pos);
297 for (i = 1; i < vec_size(e->params); ++i) {
298 if (pos + 2 >= bufsize)
302 pos = ast_type_to_string_impl((ast_expression*)(e->params[i]), buf, bufsize, pos);
304 if (pos + 1 >= bufsize)
310 pos = ast_type_to_string_impl(e->next, buf, bufsize, pos);
311 if (pos + 1 >= bufsize)
314 pos += util_snprintf(buf + pos, bufsize - pos - 1, "%i", (int)e->count);
315 if (pos + 1 >= bufsize)
321 typestr = type_name[e->vtype];
322 typelen = strlen(typestr);
323 if (pos + typelen >= bufsize)
325 util_strncpy(buf + pos, typestr, typelen);
326 return pos + typelen;
330 buf[bufsize-3] = '.';
331 buf[bufsize-2] = '.';
332 buf[bufsize-1] = '.';
336 void ast_type_to_string(ast_expression *e, char *buf, size_t bufsize)
338 size_t pos = ast_type_to_string_impl(e, buf, bufsize-1, 0);
342 static bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out);
343 ast_value* ast_value_new(lex_ctx ctx, const char *name, int t)
345 ast_instantiate(ast_value, ctx, ast_value_delete);
346 ast_expression_init((ast_expression*)self,
347 (ast_expression_codegen*)&ast_value_codegen);
348 self->expression.node.keep = true; /* keep */
350 self->name = name ? util_strdup(name) : NULL;
351 self->expression.vtype = t;
352 self->expression.next = NULL;
353 self->isfield = false;
355 self->hasvalue = false;
358 memset(&self->constval, 0, sizeof(self->constval));
359 self->initlist = NULL;
362 self->ir_values = NULL;
363 self->ir_value_count = 0;
369 self->argcounter = NULL;
374 void ast_value_delete(ast_value* self)
377 mem_d((void*)self->name);
378 if (self->argcounter)
379 mem_d((void*)self->argcounter);
380 if (self->hasvalue) {
381 switch (self->expression.vtype)
384 mem_d((void*)self->constval.vstring);
387 /* unlink us from the function node */
388 self->constval.vfunc->vtype = NULL;
390 /* NOTE: delete function? currently collected in
391 * the parser structure
398 mem_d(self->ir_values);
403 if (self->initlist) {
404 if (self->expression.next->vtype == TYPE_STRING) {
405 /* strings are allocated, free them */
406 size_t i, len = vec_size(self->initlist);
407 /* in theory, len should be expression.count
408 * but let's not take any chances */
409 for (i = 0; i < len; ++i) {
410 if (self->initlist[i].vstring)
411 mem_d(self->initlist[i].vstring);
414 vec_free(self->initlist);
417 ast_expression_delete((ast_expression*)self);
421 void ast_value_params_add(ast_value *self, ast_value *p)
423 vec_push(self->expression.params, p);
426 bool ast_value_set_name(ast_value *self, const char *name)
429 mem_d((void*)self->name);
430 self->name = util_strdup(name);
434 ast_binary* ast_binary_new(lex_ctx ctx, int op,
435 ast_expression* left, ast_expression* right)
437 ast_instantiate(ast_binary, ctx, ast_binary_delete);
438 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binary_codegen);
444 ast_propagate_effects(self, left);
445 ast_propagate_effects(self, right);
447 if (op >= INSTR_EQ_F && op <= INSTR_GT)
448 self->expression.vtype = TYPE_FLOAT;
449 else if (op == INSTR_AND || op == INSTR_OR) {
450 if (OPTS_FLAG(PERL_LOGIC))
451 ast_type_adopt(self, right);
453 self->expression.vtype = TYPE_FLOAT;
455 else if (op == INSTR_BITAND || op == INSTR_BITOR)
456 self->expression.vtype = TYPE_FLOAT;
457 else if (op == INSTR_MUL_VF || op == INSTR_MUL_FV)
458 self->expression.vtype = TYPE_VECTOR;
459 else if (op == INSTR_MUL_V)
460 self->expression.vtype = TYPE_FLOAT;
462 self->expression.vtype = left->vtype;
465 self->refs = AST_REF_ALL;
470 void ast_binary_delete(ast_binary *self)
472 if (self->refs & AST_REF_LEFT) ast_unref(self->left);
473 if (self->refs & AST_REF_RIGHT) ast_unref(self->right);
475 ast_expression_delete((ast_expression*)self);
479 ast_binstore* ast_binstore_new(lex_ctx ctx, int storop, int op,
480 ast_expression* left, ast_expression* right)
482 ast_instantiate(ast_binstore, ctx, ast_binstore_delete);
483 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binstore_codegen);
485 ast_side_effects(self) = true;
487 self->opstore = storop;
490 self->source = right;
492 self->keep_dest = false;
494 ast_type_adopt(self, left);
498 void ast_binstore_delete(ast_binstore *self)
500 if (!self->keep_dest)
501 ast_unref(self->dest);
502 ast_unref(self->source);
503 ast_expression_delete((ast_expression*)self);
507 ast_unary* ast_unary_new(lex_ctx ctx, int op,
508 ast_expression *expr)
510 ast_instantiate(ast_unary, ctx, ast_unary_delete);
511 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_unary_codegen);
514 self->operand = expr;
516 ast_propagate_effects(self, expr);
518 if (op >= INSTR_NOT_F && op <= INSTR_NOT_FNC) {
519 self->expression.vtype = TYPE_FLOAT;
521 compile_error(ctx, "cannot determine type of unary operation %s", asm_instr[op].m);
526 void ast_unary_delete(ast_unary *self)
528 if (self->operand) ast_unref(self->operand);
529 ast_expression_delete((ast_expression*)self);
533 ast_return* ast_return_new(lex_ctx ctx, ast_expression *expr)
535 ast_instantiate(ast_return, ctx, ast_return_delete);
536 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_return_codegen);
538 self->operand = expr;
541 ast_propagate_effects(self, expr);
546 void ast_return_delete(ast_return *self)
549 ast_unref(self->operand);
550 ast_expression_delete((ast_expression*)self);
554 ast_entfield* ast_entfield_new(lex_ctx ctx, ast_expression *entity, ast_expression *field)
556 if (field->vtype != TYPE_FIELD) {
557 compile_error(ctx, "ast_entfield_new with expression not of type field");
560 return ast_entfield_new_force(ctx, entity, field, field->next);
563 ast_entfield* ast_entfield_new_force(lex_ctx ctx, ast_expression *entity, ast_expression *field, const ast_expression *outtype)
565 ast_instantiate(ast_entfield, ctx, ast_entfield_delete);
569 /* Error: field has no type... */
573 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_entfield_codegen);
575 self->entity = entity;
577 ast_propagate_effects(self, entity);
578 ast_propagate_effects(self, field);
580 ast_type_adopt(self, outtype);
584 void ast_entfield_delete(ast_entfield *self)
586 ast_unref(self->entity);
587 ast_unref(self->field);
588 ast_expression_delete((ast_expression*)self);
592 ast_member* ast_member_new(lex_ctx ctx, ast_expression *owner, unsigned int field, const char *name)
594 ast_instantiate(ast_member, ctx, ast_member_delete);
600 if (owner->vtype != TYPE_VECTOR &&
601 owner->vtype != TYPE_FIELD) {
602 compile_error(ctx, "member-access on an invalid owner of type %s", type_name[owner->vtype]);
607 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_member_codegen);
608 self->expression.node.keep = true; /* keep */
610 if (owner->vtype == TYPE_VECTOR) {
611 self->expression.vtype = TYPE_FLOAT;
612 self->expression.next = NULL;
614 self->expression.vtype = TYPE_FIELD;
615 self->expression.next = ast_shallow_type(ctx, TYPE_FLOAT);
618 self->rvalue = false;
620 ast_propagate_effects(self, owner);
624 self->name = util_strdup(name);
631 void ast_member_delete(ast_member *self)
633 /* The owner is always an ast_value, which has .keep=true,
634 * also: ast_members are usually deleted after the owner, thus
635 * this will cause invalid access
636 ast_unref(self->owner);
637 * once we allow (expression).x to access a vector-member, we need
638 * to change this: preferably by creating an alternate ast node for this
639 * purpose that is not garbage-collected.
641 ast_expression_delete((ast_expression*)self);
646 bool ast_member_set_name(ast_member *self, const char *name)
649 mem_d((void*)self->name);
650 self->name = util_strdup(name);
654 ast_array_index* ast_array_index_new(lex_ctx ctx, ast_expression *array, ast_expression *index)
656 ast_expression *outtype;
657 ast_instantiate(ast_array_index, ctx, ast_array_index_delete);
659 outtype = array->next;
662 /* Error: field has no type... */
666 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_array_index_codegen);
670 ast_propagate_effects(self, array);
671 ast_propagate_effects(self, index);
673 ast_type_adopt(self, outtype);
674 if (array->vtype == TYPE_FIELD && outtype->vtype == TYPE_ARRAY) {
675 if (self->expression.vtype != TYPE_ARRAY) {
676 compile_error(ast_ctx(self), "array_index node on type");
677 ast_array_index_delete(self);
680 self->array = outtype;
681 self->expression.vtype = TYPE_FIELD;
687 void ast_array_index_delete(ast_array_index *self)
690 ast_unref(self->array);
692 ast_unref(self->index);
693 ast_expression_delete((ast_expression*)self);
697 ast_ifthen* ast_ifthen_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
699 ast_instantiate(ast_ifthen, ctx, ast_ifthen_delete);
700 if (!ontrue && !onfalse) {
701 /* because it is invalid */
705 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ifthen_codegen);
708 self->on_true = ontrue;
709 self->on_false = onfalse;
710 ast_propagate_effects(self, cond);
712 ast_propagate_effects(self, ontrue);
714 ast_propagate_effects(self, onfalse);
719 void ast_ifthen_delete(ast_ifthen *self)
721 ast_unref(self->cond);
723 ast_unref(self->on_true);
725 ast_unref(self->on_false);
726 ast_expression_delete((ast_expression*)self);
730 ast_ternary* ast_ternary_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
732 ast_expression *exprtype = ontrue;
733 ast_instantiate(ast_ternary, ctx, ast_ternary_delete);
734 /* This time NEITHER must be NULL */
735 if (!ontrue || !onfalse) {
739 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ternary_codegen);
742 self->on_true = ontrue;
743 self->on_false = onfalse;
744 ast_propagate_effects(self, cond);
745 ast_propagate_effects(self, ontrue);
746 ast_propagate_effects(self, onfalse);
748 if (ontrue->vtype == TYPE_NIL)
750 ast_type_adopt(self, exprtype);
755 void ast_ternary_delete(ast_ternary *self)
757 /* the if()s are only there because computed-gotos can set them
760 if (self->cond) ast_unref(self->cond);
761 if (self->on_true) ast_unref(self->on_true);
762 if (self->on_false) ast_unref(self->on_false);
763 ast_expression_delete((ast_expression*)self);
767 ast_loop* ast_loop_new(lex_ctx ctx,
768 ast_expression *initexpr,
769 ast_expression *precond, bool pre_not,
770 ast_expression *postcond, bool post_not,
771 ast_expression *increment,
772 ast_expression *body)
774 ast_instantiate(ast_loop, ctx, ast_loop_delete);
775 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_loop_codegen);
777 self->initexpr = initexpr;
778 self->precond = precond;
779 self->postcond = postcond;
780 self->increment = increment;
783 self->pre_not = pre_not;
784 self->post_not = post_not;
787 ast_propagate_effects(self, initexpr);
789 ast_propagate_effects(self, precond);
791 ast_propagate_effects(self, postcond);
793 ast_propagate_effects(self, increment);
795 ast_propagate_effects(self, body);
800 void ast_loop_delete(ast_loop *self)
803 ast_unref(self->initexpr);
805 ast_unref(self->precond);
807 ast_unref(self->postcond);
809 ast_unref(self->increment);
811 ast_unref(self->body);
812 ast_expression_delete((ast_expression*)self);
816 ast_breakcont* ast_breakcont_new(lex_ctx ctx, bool iscont, unsigned int levels)
818 ast_instantiate(ast_breakcont, ctx, ast_breakcont_delete);
819 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_breakcont_codegen);
821 self->is_continue = iscont;
822 self->levels = levels;
827 void ast_breakcont_delete(ast_breakcont *self)
829 ast_expression_delete((ast_expression*)self);
833 ast_switch* ast_switch_new(lex_ctx ctx, ast_expression *op)
835 ast_instantiate(ast_switch, ctx, ast_switch_delete);
836 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_switch_codegen);
841 ast_propagate_effects(self, op);
846 void ast_switch_delete(ast_switch *self)
849 ast_unref(self->operand);
851 for (i = 0; i < vec_size(self->cases); ++i) {
852 if (self->cases[i].value)
853 ast_unref(self->cases[i].value);
854 ast_unref(self->cases[i].code);
856 vec_free(self->cases);
858 ast_expression_delete((ast_expression*)self);
862 ast_label* ast_label_new(lex_ctx ctx, const char *name, bool undefined)
864 ast_instantiate(ast_label, ctx, ast_label_delete);
865 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_label_codegen);
867 self->expression.vtype = TYPE_NOEXPR;
869 self->name = util_strdup(name);
870 self->irblock = NULL;
872 self->undefined = undefined;
877 void ast_label_delete(ast_label *self)
879 mem_d((void*)self->name);
880 vec_free(self->gotos);
881 ast_expression_delete((ast_expression*)self);
885 static void ast_label_register_goto(ast_label *self, ast_goto *g)
887 vec_push(self->gotos, g);
890 ast_goto* ast_goto_new(lex_ctx ctx, const char *name)
892 ast_instantiate(ast_goto, ctx, ast_goto_delete);
893 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_goto_codegen);
895 self->name = util_strdup(name);
897 self->irblock_from = NULL;
902 void ast_goto_delete(ast_goto *self)
904 mem_d((void*)self->name);
905 ast_expression_delete((ast_expression*)self);
909 void ast_goto_set_label(ast_goto *self, ast_label *label)
911 self->target = label;
914 ast_call* ast_call_new(lex_ctx ctx,
915 ast_expression *funcexpr)
917 ast_instantiate(ast_call, ctx, ast_call_delete);
918 if (!funcexpr->next) {
919 compile_error(ctx, "not a function");
923 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_call_codegen);
925 ast_side_effects(self) = true;
928 self->func = funcexpr;
929 self->va_count = NULL;
931 ast_type_adopt(self, funcexpr->next);
936 void ast_call_delete(ast_call *self)
939 for (i = 0; i < vec_size(self->params); ++i)
940 ast_unref(self->params[i]);
941 vec_free(self->params);
944 ast_unref(self->func);
947 ast_unref(self->va_count);
949 ast_expression_delete((ast_expression*)self);
953 bool ast_call_check_types(ast_call *self)
959 const ast_expression *func = self->func;
960 size_t count = vec_size(self->params);
961 if (count > vec_size(func->params))
962 count = vec_size(func->params);
964 for (i = 0; i < count; ++i) {
965 if (!ast_compare_type(self->params[i], (ast_expression*)(func->params[i])))
967 ast_type_to_string(self->params[i], tgot, sizeof(tgot));
968 ast_type_to_string((ast_expression*)func->params[i], texp, sizeof(texp));
969 compile_error(ast_ctx(self), "invalid type for parameter %u in function call: expected %s, got %s",
970 (unsigned int)(i+1), texp, tgot);
971 /* we don't immediately return */
975 count = vec_size(self->params);
976 if (count > vec_size(func->params) && func->varparam) {
977 for (; i < count; ++i) {
978 if (!ast_compare_type(self->params[i], func->varparam))
980 ast_type_to_string(self->params[i], tgot, sizeof(tgot));
981 ast_type_to_string(func->varparam, texp, sizeof(texp));
982 compile_error(ast_ctx(self), "invalid type for parameter %u in function call: expected %s, got %s",
983 (unsigned int)(i+1), texp, tgot);
984 /* we don't immediately return */
992 ast_store* ast_store_new(lex_ctx ctx, int op,
993 ast_expression *dest, ast_expression *source)
995 ast_instantiate(ast_store, ctx, ast_store_delete);
996 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_store_codegen);
998 ast_side_effects(self) = true;
1002 self->source = source;
1004 ast_type_adopt(self, dest);
1009 void ast_store_delete(ast_store *self)
1011 ast_unref(self->dest);
1012 ast_unref(self->source);
1013 ast_expression_delete((ast_expression*)self);
1017 ast_block* ast_block_new(lex_ctx ctx)
1019 ast_instantiate(ast_block, ctx, ast_block_delete);
1020 ast_expression_init((ast_expression*)self,
1021 (ast_expression_codegen*)&ast_block_codegen);
1023 self->locals = NULL;
1025 self->collect = NULL;
1030 bool ast_block_add_expr(ast_block *self, ast_expression *e)
1032 ast_propagate_effects(self, e);
1033 vec_push(self->exprs, e);
1034 if (self->expression.next) {
1035 ast_delete(self->expression.next);
1036 self->expression.next = NULL;
1038 ast_type_adopt(self, e);
1042 void ast_block_collect(ast_block *self, ast_expression *expr)
1044 vec_push(self->collect, expr);
1045 expr->node.keep = true;
1048 void ast_block_delete(ast_block *self)
1051 for (i = 0; i < vec_size(self->exprs); ++i)
1052 ast_unref(self->exprs[i]);
1053 vec_free(self->exprs);
1054 for (i = 0; i < vec_size(self->locals); ++i)
1055 ast_delete(self->locals[i]);
1056 vec_free(self->locals);
1057 for (i = 0; i < vec_size(self->collect); ++i)
1058 ast_delete(self->collect[i]);
1059 vec_free(self->collect);
1060 ast_expression_delete((ast_expression*)self);
1064 void ast_block_set_type(ast_block *self, ast_expression *from)
1066 if (self->expression.next)
1067 ast_delete(self->expression.next);
1068 ast_type_adopt(self, from);
1071 ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype)
1073 ast_instantiate(ast_function, ctx, ast_function_delete);
1077 vtype->expression.vtype != TYPE_FUNCTION)
1079 compile_error(ast_ctx(self), "internal error: ast_function_new condition %i %i type=%i (probably 2 bodies?)",
1081 (int)vtype->hasvalue,
1082 vtype->expression.vtype);
1087 self->vtype = vtype;
1088 self->name = name ? util_strdup(name) : NULL;
1089 self->blocks = NULL;
1091 self->labelcount = 0;
1094 self->ir_func = NULL;
1095 self->curblock = NULL;
1097 self->breakblocks = NULL;
1098 self->continueblocks = NULL;
1100 vtype->hasvalue = true;
1101 vtype->constval.vfunc = self;
1103 self->varargs = NULL;
1105 self->fixedparams = NULL;
1106 self->return_value = NULL;
1111 void ast_function_delete(ast_function *self)
1115 mem_d((void*)self->name);
1117 /* ast_value_delete(self->vtype); */
1118 self->vtype->hasvalue = false;
1119 self->vtype->constval.vfunc = NULL;
1120 /* We use unref - if it was stored in a global table it is supposed
1121 * to be deleted from *there*
1123 ast_unref(self->vtype);
1125 for (i = 0; i < vec_size(self->blocks); ++i)
1126 ast_delete(self->blocks[i]);
1127 vec_free(self->blocks);
1128 vec_free(self->breakblocks);
1129 vec_free(self->continueblocks);
1131 ast_delete(self->varargs);
1133 ast_delete(self->argc);
1134 if (self->fixedparams)
1135 ast_unref(self->fixedparams);
1136 if (self->return_value)
1137 ast_unref(self->return_value);
1141 static const char* ast_function_label(ast_function *self, const char *prefix)
1147 if (!OPTS_OPTION_BOOL(OPTION_DUMP) &&
1148 !OPTS_OPTION_BOOL(OPTION_DUMPFIN) &&
1149 !OPTS_OPTION_BOOL(OPTION_DEBUG))
1154 id = (self->labelcount++);
1155 len = strlen(prefix);
1157 from = self->labelbuf + sizeof(self->labelbuf)-1;
1160 *from-- = (id%10) + '0';
1164 memcpy(from - len, prefix, len);
1168 /*********************************************************************/
1170 * by convention you must never pass NULL to the 'ir_value **out'
1171 * parameter. If you really don't care about the output, pass a dummy.
1172 * But I can't imagine a pituation where the output is truly unnecessary.
1175 static void _ast_codegen_output_type(ast_expression *self, ir_value *out)
1177 if (out->vtype == TYPE_FIELD)
1178 out->fieldtype = self->next->vtype;
1179 if (out->vtype == TYPE_FUNCTION)
1180 out->outtype = self->next->vtype;
1183 #define codegen_output_type(a,o) (_ast_codegen_output_type(&((a)->expression),(o)))
1185 bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out)
1189 if (self->expression.vtype == TYPE_NIL) {
1190 *out = func->ir_func->owner->nil;
1193 /* NOTE: This is the codegen for a variable used in an expression.
1194 * It is not the codegen to generate the value. For this purpose,
1195 * ast_local_codegen and ast_global_codegen are to be used before this
1196 * is executed. ast_function_codegen should take care of its locals,
1197 * and the ast-user should take care of ast_global_codegen to be used
1198 * on all the globals.
1201 char tname[1024]; /* typename is reserved in C++ */
1202 ast_type_to_string((ast_expression*)self, tname, sizeof(tname));
1203 compile_error(ast_ctx(self), "ast_value used before generated %s %s", tname, self->name);
1210 static bool ast_global_array_set(ast_value *self)
1212 size_t count = vec_size(self->initlist);
1215 if (count > self->expression.count) {
1216 compile_error(ast_ctx(self), "too many elements in initializer");
1217 count = self->expression.count;
1219 else if (count < self->expression.count) {
1221 compile_warning(ast_ctx(self), "not all elements are initialized");
1225 for (i = 0; i != count; ++i) {
1226 switch (self->expression.next->vtype) {
1228 if (!ir_value_set_float(self->ir_values[i], self->initlist[i].vfloat))
1232 if (!ir_value_set_vector(self->ir_values[i], self->initlist[i].vvec))
1236 if (!ir_value_set_string(self->ir_values[i], self->initlist[i].vstring))
1240 /* we don't support them in any other place yet either */
1241 compile_error(ast_ctx(self), "TODO: nested arrays");
1244 /* this requiers a bit more work - similar to the fields I suppose */
1245 compile_error(ast_ctx(self), "global of type function not properly generated");
1248 if (!self->initlist[i].vfield) {
1249 compile_error(ast_ctx(self), "field constant without vfield set");
1252 if (!self->initlist[i].vfield->ir_v) {
1253 compile_error(ast_ctx(self), "field constant generated before its field");
1256 if (!ir_value_set_field(self->ir_values[i], self->initlist[i].vfield->ir_v))
1260 compile_error(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1267 bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
1271 if (self->expression.vtype == TYPE_NIL) {
1272 compile_error(ast_ctx(self), "internal error: trying to generate a variable of TYPE_NIL");
1276 if (self->hasvalue && self->expression.vtype == TYPE_FUNCTION)
1278 ir_function *func = ir_builder_create_function(ir, self->name, self->expression.next->vtype);
1281 func->context = ast_ctx(self);
1282 func->value->context = ast_ctx(self);
1284 self->constval.vfunc->ir_func = func;
1285 self->ir_v = func->value;
1286 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1287 self->ir_v->flags |= IR_FLAG_INCLUDE_DEF;
1288 /* The function is filled later on ast_function_codegen... */
1292 if (isfield && self->expression.vtype == TYPE_FIELD) {
1293 ast_expression *fieldtype = self->expression.next;
1295 if (self->hasvalue) {
1296 compile_error(ast_ctx(self), "TODO: constant field pointers with value");
1300 if (fieldtype->vtype == TYPE_ARRAY) {
1305 ast_expression *elemtype;
1307 ast_value *array = (ast_value*)fieldtype;
1309 if (!ast_istype(fieldtype, ast_value)) {
1310 compile_error(ast_ctx(self), "internal error: ast_value required");
1314 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1315 if (!array->expression.count || array->expression.count > OPTS_OPTION_U32(OPTION_MAX_ARRAY_SIZE))
1316 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)array->expression.count);
1318 elemtype = array->expression.next;
1319 vtype = elemtype->vtype;
1321 v = ir_builder_create_field(ir, self->name, vtype);
1323 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", self->name);
1326 v->context = ast_ctx(self);
1327 v->unique_life = true;
1329 array->ir_v = self->ir_v = v;
1330 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1331 self->ir_v->flags |= IR_FLAG_INCLUDE_DEF;
1333 namelen = strlen(self->name);
1334 name = (char*)mem_a(namelen + 16);
1335 util_strncpy(name, self->name, namelen);
1337 array->ir_values = (ir_value**)mem_a(sizeof(array->ir_values[0]) * array->expression.count);
1338 array->ir_values[0] = v;
1339 for (ai = 1; ai < array->expression.count; ++ai) {
1340 util_snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1341 array->ir_values[ai] = ir_builder_create_field(ir, name, vtype);
1342 if (!array->ir_values[ai]) {
1344 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", name);
1347 array->ir_values[ai]->context = ast_ctx(self);
1348 array->ir_values[ai]->unique_life = true;
1349 array->ir_values[ai]->locked = true;
1350 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1351 self->ir_values[ai]->flags |= IR_FLAG_INCLUDE_DEF;
1357 v = ir_builder_create_field(ir, self->name, self->expression.next->vtype);
1360 v->context = ast_ctx(self);
1362 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1363 self->ir_v->flags |= IR_FLAG_INCLUDE_DEF;
1368 if (self->expression.vtype == TYPE_ARRAY) {
1373 ast_expression *elemtype = self->expression.next;
1374 int vtype = elemtype->vtype;
1376 /* same as with field arrays */
1377 if (!self->expression.count || self->expression.count > OPTS_OPTION_U32(OPTION_MAX_ARRAY_SIZE))
1378 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1380 v = ir_builder_create_global(ir, self->name, vtype);
1382 compile_error(ast_ctx(self), "ir_builder_create_global failed `%s`", self->name);
1385 v->context = ast_ctx(self);
1386 v->unique_life = true;
1388 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1389 v->flags |= IR_FLAG_INCLUDE_DEF;
1391 namelen = strlen(self->name);
1392 name = (char*)mem_a(namelen + 16);
1393 util_strncpy(name, self->name, namelen);
1395 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1396 self->ir_values[0] = v;
1397 for (ai = 1; ai < self->expression.count; ++ai) {
1398 util_snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1399 self->ir_values[ai] = ir_builder_create_global(ir, name, vtype);
1400 if (!self->ir_values[ai]) {
1402 compile_error(ast_ctx(self), "ir_builder_create_global failed `%s`", name);
1405 self->ir_values[ai]->context = ast_ctx(self);
1406 self->ir_values[ai]->unique_life = true;
1407 self->ir_values[ai]->locked = true;
1408 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1409 self->ir_values[ai]->flags |= IR_FLAG_INCLUDE_DEF;
1415 /* Arrays don't do this since there's no "array" value which spans across the
1418 v = ir_builder_create_global(ir, self->name, self->expression.vtype);
1420 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", self->name);
1423 codegen_output_type(self, v);
1424 v->context = ast_ctx(self);
1427 /* link us to the ir_value */
1430 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1431 self->ir_v->flags |= IR_FLAG_INCLUDE_DEF;
1434 if (self->hasvalue) {
1435 switch (self->expression.vtype)
1438 if (!ir_value_set_float(v, self->constval.vfloat))
1442 if (!ir_value_set_vector(v, self->constval.vvec))
1446 if (!ir_value_set_string(v, self->constval.vstring))
1450 ast_global_array_set(self);
1453 compile_error(ast_ctx(self), "global of type function not properly generated");
1455 /* Cannot generate an IR value for a function,
1456 * need a pointer pointing to a function rather.
1459 if (!self->constval.vfield) {
1460 compile_error(ast_ctx(self), "field constant without vfield set");
1463 if (!self->constval.vfield->ir_v) {
1464 compile_error(ast_ctx(self), "field constant generated before its field");
1467 if (!ir_value_set_field(v, self->constval.vfield->ir_v))
1471 compile_error(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1477 error: /* clean up */
1482 static bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
1486 if (self->expression.vtype == TYPE_NIL) {
1487 compile_error(ast_ctx(self), "internal error: trying to generate a variable of TYPE_NIL");
1491 if (self->hasvalue && self->expression.vtype == TYPE_FUNCTION)
1493 /* Do we allow local functions? I think not...
1494 * this is NOT a function pointer atm.
1499 if (self->expression.vtype == TYPE_ARRAY) {
1504 ast_expression *elemtype = self->expression.next;
1505 int vtype = elemtype->vtype;
1507 func->flags |= IR_FLAG_HAS_ARRAYS;
1509 if (param && !(self->expression.flags & AST_FLAG_IS_VARARG)) {
1510 compile_error(ast_ctx(self), "array-parameters are not supported");
1514 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1515 if (!self->expression.count || self->expression.count > OPTS_OPTION_U32(OPTION_MAX_ARRAY_SIZE)) {
1516 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1519 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1520 if (!self->ir_values) {
1521 compile_error(ast_ctx(self), "failed to allocate array values");
1525 v = ir_function_create_local(func, self->name, vtype, param);
1527 compile_error(ast_ctx(self), "internal error: ir_function_create_local failed");
1530 v->context = ast_ctx(self);
1531 v->unique_life = true;
1534 namelen = strlen(self->name);
1535 name = (char*)mem_a(namelen + 16);
1536 util_strncpy(name, self->name, namelen);
1538 self->ir_values[0] = v;
1539 for (ai = 1; ai < self->expression.count; ++ai) {
1540 util_snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1541 self->ir_values[ai] = ir_function_create_local(func, name, vtype, param);
1542 if (!self->ir_values[ai]) {
1543 compile_error(ast_ctx(self), "internal_error: ir_builder_create_global failed on `%s`", name);
1546 self->ir_values[ai]->context = ast_ctx(self);
1547 self->ir_values[ai]->unique_life = true;
1548 self->ir_values[ai]->locked = true;
1554 v = ir_function_create_local(func, self->name, self->expression.vtype, param);
1557 codegen_output_type(self, v);
1558 v->context = ast_ctx(self);
1561 /* A constant local... hmmm...
1562 * I suppose the IR will have to deal with this
1564 if (self->hasvalue) {
1565 switch (self->expression.vtype)
1568 if (!ir_value_set_float(v, self->constval.vfloat))
1572 if (!ir_value_set_vector(v, self->constval.vvec))
1576 if (!ir_value_set_string(v, self->constval.vstring))
1580 compile_error(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1585 /* link us to the ir_value */
1589 if (!ast_generate_accessors(self, func->owner))
1593 error: /* clean up */
1598 bool ast_generate_accessors(ast_value *self, ir_builder *ir)
1601 bool warn = OPTS_WARN(WARN_USED_UNINITIALIZED);
1602 if (!self->setter || !self->getter)
1604 for (i = 0; i < self->expression.count; ++i) {
1605 if (!self->ir_values) {
1606 compile_error(ast_ctx(self), "internal error: no array values generated for `%s`", self->name);
1609 if (!self->ir_values[i]) {
1610 compile_error(ast_ctx(self), "internal error: not all array values have been generated for `%s`", self->name);
1613 if (self->ir_values[i]->life) {
1614 compile_error(ast_ctx(self), "internal error: function containing `%s` already generated", self->name);
1619 opts_set(opts.warn, WARN_USED_UNINITIALIZED, false);
1621 if (!ast_global_codegen (self->setter, ir, false) ||
1622 !ast_function_codegen(self->setter->constval.vfunc, ir) ||
1623 !ir_function_finalize(self->setter->constval.vfunc->ir_func))
1625 compile_error(ast_ctx(self), "internal error: failed to generate setter for `%s`", self->name);
1626 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1631 if (!ast_global_codegen (self->getter, ir, false) ||
1632 !ast_function_codegen(self->getter->constval.vfunc, ir) ||
1633 !ir_function_finalize(self->getter->constval.vfunc->ir_func))
1635 compile_error(ast_ctx(self), "internal error: failed to generate getter for `%s`", self->name);
1636 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1640 for (i = 0; i < self->expression.count; ++i) {
1641 vec_free(self->ir_values[i]->life);
1643 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1647 bool ast_function_codegen(ast_function *self, ir_builder *ir)
1652 ast_expression_codegen *cgen;
1657 irf = self->ir_func;
1659 compile_error(ast_ctx(self), "internal error: ast_function's related ast_value was not generated yet");
1663 /* fill the parameter list */
1664 ec = &self->vtype->expression;
1665 for (i = 0; i < vec_size(ec->params); ++i)
1667 if (ec->params[i]->expression.vtype == TYPE_FIELD)
1668 vec_push(irf->params, ec->params[i]->expression.next->vtype);
1670 vec_push(irf->params, ec->params[i]->expression.vtype);
1671 if (!self->builtin) {
1672 if (!ast_local_codegen(ec->params[i], self->ir_func, true))
1677 if (self->varargs) {
1678 if (!ast_local_codegen(self->varargs, self->ir_func, true))
1680 irf->max_varargs = self->varargs->expression.count;
1683 if (self->builtin) {
1684 irf->builtin = self->builtin;
1688 /* have a local return value variable? */
1689 if (self->return_value) {
1690 if (!ast_local_codegen(self->return_value, self->ir_func, false))
1694 if (!vec_size(self->blocks)) {
1695 compile_error(ast_ctx(self), "function `%s` has no body", self->name);
1699 irf->first = self->curblock = ir_function_create_block(ast_ctx(self), irf, "entry");
1700 if (!self->curblock) {
1701 compile_error(ast_ctx(self), "failed to allocate entry block for `%s`", self->name);
1709 if (!ast_local_codegen(self->argc, self->ir_func, true))
1711 cgen = self->argc->expression.codegen;
1712 if (!(*cgen)((ast_expression*)(self->argc), self, false, &va_count))
1714 cgen = self->fixedparams->expression.codegen;
1715 if (!(*cgen)((ast_expression*)(self->fixedparams), self, false, &fixed))
1717 sub = ir_block_create_binop(self->curblock, ast_ctx(self),
1718 ast_function_label(self, "va_count"), INSTR_SUB_F,
1719 ir_builder_get_va_count(ir), fixed);
1722 if (!ir_block_create_store_op(self->curblock, ast_ctx(self), INSTR_STORE_F,
1729 for (i = 0; i < vec_size(self->blocks); ++i) {
1730 cgen = self->blocks[i]->expression.codegen;
1731 if (!(*cgen)((ast_expression*)self->blocks[i], self, false, &dummy))
1735 /* TODO: check return types */
1736 if (!self->curblock->final)
1738 if (!self->vtype->expression.next ||
1739 self->vtype->expression.next->vtype == TYPE_VOID)
1741 return ir_block_create_return(self->curblock, ast_ctx(self), NULL);
1743 else if (vec_size(self->curblock->entries) || self->curblock == irf->first)
1745 if (self->return_value) {
1746 cgen = self->return_value->expression.codegen;
1747 if (!(*cgen)((ast_expression*)(self->return_value), self, false, &dummy))
1749 return ir_block_create_return(self->curblock, ast_ctx(self), dummy);
1751 else if (compile_warning(ast_ctx(self), WARN_MISSING_RETURN_VALUES,
1752 "control reaches end of non-void function (`%s`) via %s",
1753 self->name, self->curblock->label))
1757 return ir_block_create_return(self->curblock, ast_ctx(self), NULL);
1763 /* Note, you will not see ast_block_codegen generate ir_blocks.
1764 * To the AST and the IR, blocks are 2 different things.
1765 * In the AST it represents a block of code, usually enclosed in
1766 * curly braces {...}.
1767 * While in the IR it represents a block in terms of control-flow.
1769 bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_value **out)
1773 /* We don't use this
1774 * Note: an ast-representation using the comma-operator
1775 * of the form: (a, b, c) = x should not assign to c...
1778 compile_error(ast_ctx(self), "not an l-value (code-block)");
1782 if (self->expression.outr) {
1783 *out = self->expression.outr;
1787 /* output is NULL at first, we'll have each expression
1788 * assign to out output, thus, a comma-operator represention
1789 * using an ast_block will return the last generated value,
1790 * so: (b, c) + a executed both b and c, and returns c,
1791 * which is then added to a.
1795 /* generate locals */
1796 for (i = 0; i < vec_size(self->locals); ++i)
1798 if (!ast_local_codegen(self->locals[i], func->ir_func, false)) {
1799 if (OPTS_OPTION_BOOL(OPTION_DEBUG))
1800 compile_error(ast_ctx(self), "failed to generate local `%s`", self->locals[i]->name);
1805 for (i = 0; i < vec_size(self->exprs); ++i)
1807 ast_expression_codegen *gen;
1808 if (func->curblock->final && !ast_istype(self->exprs[i], ast_label)) {
1809 if (compile_warning(ast_ctx(self->exprs[i]), WARN_UNREACHABLE_CODE, "unreachable statement"))
1813 gen = self->exprs[i]->codegen;
1814 if (!(*gen)(self->exprs[i], func, false, out))
1818 self->expression.outr = *out;
1823 bool ast_store_codegen(ast_store *self, ast_function *func, bool lvalue, ir_value **out)
1825 ast_expression_codegen *cgen;
1826 ir_value *left = NULL;
1827 ir_value *right = NULL;
1831 ast_array_index *ai = NULL;
1833 if (lvalue && self->expression.outl) {
1834 *out = self->expression.outl;
1838 if (!lvalue && self->expression.outr) {
1839 *out = self->expression.outr;
1843 if (ast_istype(self->dest, ast_array_index))
1846 ai = (ast_array_index*)self->dest;
1847 idx = (ast_value*)ai->index;
1849 if (ast_istype(ai->index, ast_value) && idx->hasvalue && idx->cvq == CV_CONST)
1854 /* we need to call the setter */
1855 ir_value *iridx, *funval;
1859 compile_error(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1863 arr = (ast_value*)ai->array;
1864 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1865 compile_error(ast_ctx(self), "value has no setter (%s)", arr->name);
1869 cgen = idx->expression.codegen;
1870 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1873 cgen = arr->setter->expression.codegen;
1874 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
1877 cgen = self->source->codegen;
1878 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1881 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "store"), funval, false);
1884 ir_call_param(call, iridx);
1885 ir_call_param(call, right);
1886 self->expression.outr = right;
1892 cgen = self->dest->codegen;
1894 if (!(*cgen)((ast_expression*)(self->dest), func, true, &left))
1896 self->expression.outl = left;
1898 cgen = self->source->codegen;
1900 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1903 if (!ir_block_create_store_op(func->curblock, ast_ctx(self), self->op, left, right))
1905 self->expression.outr = right;
1908 /* Theoretically, an assinment returns its left side as an
1909 * lvalue, if we don't need an lvalue though, we return
1910 * the right side as an rvalue, otherwise we have to
1911 * somehow know whether or not we need to dereference the pointer
1912 * on the left side - that is: OP_LOAD if it was an address.
1913 * Also: in original QC we cannot OP_LOADP *anyway*.
1915 *out = (lvalue ? left : right);
1920 bool ast_binary_codegen(ast_binary *self, ast_function *func, bool lvalue, ir_value **out)
1922 ast_expression_codegen *cgen;
1923 ir_value *left, *right;
1925 /* A binary operation cannot yield an l-value */
1927 compile_error(ast_ctx(self), "not an l-value (binop)");
1931 if (self->expression.outr) {
1932 *out = self->expression.outr;
1936 if ((OPTS_FLAG(SHORT_LOGIC) || OPTS_FLAG(PERL_LOGIC)) &&
1937 (self->op == INSTR_AND || self->op == INSTR_OR))
1939 /* short circuit evaluation */
1940 ir_block *other, *merge;
1941 ir_block *from_left, *from_right;
1945 /* prepare end-block */
1946 merge_id = vec_size(func->ir_func->blocks);
1947 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "sce_merge"));
1949 /* generate the left expression */
1950 cgen = self->left->codegen;
1951 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1953 /* remember the block */
1954 from_left = func->curblock;
1956 /* create a new block for the right expression */
1957 other = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "sce_other"));
1958 if (self->op == INSTR_AND) {
1959 /* on AND: left==true -> other */
1960 if (!ir_block_create_if(func->curblock, ast_ctx(self), left, other, merge))
1963 /* on OR: left==false -> other */
1964 if (!ir_block_create_if(func->curblock, ast_ctx(self), left, merge, other))
1967 /* use the likely flag */
1968 vec_last(func->curblock->instr)->likely = true;
1970 /* enter the right-expression's block */
1971 func->curblock = other;
1973 cgen = self->right->codegen;
1974 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1976 /* remember block */
1977 from_right = func->curblock;
1979 /* jump to the merge block */
1980 if (!ir_block_create_jump(func->curblock, ast_ctx(self), merge))
1983 vec_remove(func->ir_func->blocks, merge_id, 1);
1984 vec_push(func->ir_func->blocks, merge);
1986 func->curblock = merge;
1987 phi = ir_block_create_phi(func->curblock, ast_ctx(self),
1988 ast_function_label(func, "sce_value"),
1989 self->expression.vtype);
1990 ir_phi_add(phi, from_left, left);
1991 ir_phi_add(phi, from_right, right);
1992 *out = ir_phi_value(phi);
1996 if (!OPTS_FLAG(PERL_LOGIC)) {
1998 if (OPTS_FLAG(CORRECT_LOGIC) && (*out)->vtype == TYPE_VECTOR) {
1999 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
2000 ast_function_label(func, "sce_bool_v"),
2004 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
2005 ast_function_label(func, "sce_bool"),
2010 else if (OPTS_FLAG(FALSE_EMPTY_STRINGS) && (*out)->vtype == TYPE_STRING) {
2011 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
2012 ast_function_label(func, "sce_bool_s"),
2016 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
2017 ast_function_label(func, "sce_bool"),
2023 *out = ir_block_create_binop(func->curblock, ast_ctx(self),
2024 ast_function_label(func, "sce_bool"),
2025 INSTR_AND, *out, *out);
2031 self->expression.outr = *out;
2032 codegen_output_type(self, *out);
2036 cgen = self->left->codegen;
2037 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
2040 cgen = self->right->codegen;
2041 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
2044 *out = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "bin"),
2045 self->op, left, right);
2048 self->expression.outr = *out;
2049 codegen_output_type(self, *out);
2054 bool ast_binstore_codegen(ast_binstore *self, ast_function *func, bool lvalue, ir_value **out)
2056 ast_expression_codegen *cgen;
2057 ir_value *leftl = NULL, *leftr, *right, *bin;
2061 ast_array_index *ai = NULL;
2062 ir_value *iridx = NULL;
2064 if (lvalue && self->expression.outl) {
2065 *out = self->expression.outl;
2069 if (!lvalue && self->expression.outr) {
2070 *out = self->expression.outr;
2074 if (ast_istype(self->dest, ast_array_index))
2077 ai = (ast_array_index*)self->dest;
2078 idx = (ast_value*)ai->index;
2080 if (ast_istype(ai->index, ast_value) && idx->hasvalue && idx->cvq == CV_CONST)
2084 /* for a binstore we need both an lvalue and an rvalue for the left side */
2085 /* rvalue of destination! */
2087 cgen = idx->expression.codegen;
2088 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
2091 cgen = self->dest->codegen;
2092 if (!(*cgen)((ast_expression*)(self->dest), func, false, &leftr))
2095 /* source as rvalue only */
2096 cgen = self->source->codegen;
2097 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
2100 /* now the binary */
2101 bin = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "binst"),
2102 self->opbin, leftr, right);
2103 self->expression.outr = bin;
2107 /* we need to call the setter */
2112 compile_error(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
2116 arr = (ast_value*)ai->array;
2117 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
2118 compile_error(ast_ctx(self), "value has no setter (%s)", arr->name);
2122 cgen = arr->setter->expression.codegen;
2123 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
2126 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "store"), funval, false);
2129 ir_call_param(call, iridx);
2130 ir_call_param(call, bin);
2131 self->expression.outr = bin;
2133 /* now store them */
2134 cgen = self->dest->codegen;
2135 /* lvalue of destination */
2136 if (!(*cgen)((ast_expression*)(self->dest), func, true, &leftl))
2138 self->expression.outl = leftl;
2140 if (!ir_block_create_store_op(func->curblock, ast_ctx(self), self->opstore, leftl, bin))
2142 self->expression.outr = bin;
2145 /* Theoretically, an assinment returns its left side as an
2146 * lvalue, if we don't need an lvalue though, we return
2147 * the right side as an rvalue, otherwise we have to
2148 * somehow know whether or not we need to dereference the pointer
2149 * on the left side - that is: OP_LOAD if it was an address.
2150 * Also: in original QC we cannot OP_LOADP *anyway*.
2152 *out = (lvalue ? leftl : bin);
2157 bool ast_unary_codegen(ast_unary *self, ast_function *func, bool lvalue, ir_value **out)
2159 ast_expression_codegen *cgen;
2162 /* An unary operation cannot yield an l-value */
2164 compile_error(ast_ctx(self), "not an l-value (binop)");
2168 if (self->expression.outr) {
2169 *out = self->expression.outr;
2173 cgen = self->operand->codegen;
2175 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
2178 *out = ir_block_create_unary(func->curblock, ast_ctx(self), ast_function_label(func, "unary"),
2182 self->expression.outr = *out;
2187 bool ast_return_codegen(ast_return *self, ast_function *func, bool lvalue, ir_value **out)
2189 ast_expression_codegen *cgen;
2194 /* In the context of a return operation, we don't actually return
2198 compile_error(ast_ctx(self), "return-expression is not an l-value");
2202 if (self->expression.outr) {
2203 compile_error(ast_ctx(self), "internal error: ast_return cannot be reused, it bears no result!");
2206 self->expression.outr = (ir_value*)1;
2208 if (self->operand) {
2209 cgen = self->operand->codegen;
2211 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
2214 if (!ir_block_create_return(func->curblock, ast_ctx(self), operand))
2217 if (!ir_block_create_return(func->curblock, ast_ctx(self), NULL))
2224 bool ast_entfield_codegen(ast_entfield *self, ast_function *func, bool lvalue, ir_value **out)
2226 ast_expression_codegen *cgen;
2227 ir_value *ent, *field;
2229 /* This function needs to take the 'lvalue' flag into account!
2230 * As lvalue we provide a field-pointer, as rvalue we provide the
2234 if (lvalue && self->expression.outl) {
2235 *out = self->expression.outl;
2239 if (!lvalue && self->expression.outr) {
2240 *out = self->expression.outr;
2244 cgen = self->entity->codegen;
2245 if (!(*cgen)((ast_expression*)(self->entity), func, false, &ent))
2248 cgen = self->field->codegen;
2249 if (!(*cgen)((ast_expression*)(self->field), func, false, &field))
2254 *out = ir_block_create_fieldaddress(func->curblock, ast_ctx(self), ast_function_label(func, "efa"),
2257 *out = ir_block_create_load_from_ent(func->curblock, ast_ctx(self), ast_function_label(func, "efv"),
2258 ent, field, self->expression.vtype);
2259 /* Done AFTER error checking:
2260 codegen_output_type(self, *out);
2264 compile_error(ast_ctx(self), "failed to create %s instruction (output type %s)",
2265 (lvalue ? "ADDRESS" : "FIELD"),
2266 type_name[self->expression.vtype]);
2270 codegen_output_type(self, *out);
2273 self->expression.outl = *out;
2275 self->expression.outr = *out;
2277 /* Hm that should be it... */
2281 bool ast_member_codegen(ast_member *self, ast_function *func, bool lvalue, ir_value **out)
2283 ast_expression_codegen *cgen;
2286 /* in QC this is always an lvalue */
2287 if (lvalue && self->rvalue) {
2288 compile_error(ast_ctx(self), "not an l-value (member access)");
2291 if (self->expression.outl) {
2292 *out = self->expression.outl;
2296 cgen = self->owner->codegen;
2297 if (!(*cgen)((ast_expression*)(self->owner), func, false, &vec))
2300 if (vec->vtype != TYPE_VECTOR &&
2301 !(vec->vtype == TYPE_FIELD && self->owner->next->vtype == TYPE_VECTOR))
2306 *out = ir_value_vector_member(vec, self->field);
2307 self->expression.outl = *out;
2309 return (*out != NULL);
2312 bool ast_array_index_codegen(ast_array_index *self, ast_function *func, bool lvalue, ir_value **out)
2317 if (!lvalue && self->expression.outr) {
2318 *out = self->expression.outr;
2321 if (lvalue && self->expression.outl) {
2322 *out = self->expression.outl;
2326 if (!ast_istype(self->array, ast_value)) {
2327 compile_error(ast_ctx(self), "array indexing this way is not supported");
2328 /* note this would actually be pointer indexing because the left side is
2329 * not an actual array but (hopefully) an indexable expression.
2330 * Once we get integer arithmetic, and GADDRESS/GSTORE/GLOAD instruction
2331 * support this path will be filled.
2336 arr = (ast_value*)self->array;
2337 idx = (ast_value*)self->index;
2339 if (!ast_istype(self->index, ast_value) || !idx->hasvalue || idx->cvq != CV_CONST) {
2340 /* Time to use accessor functions */
2341 ast_expression_codegen *cgen;
2342 ir_value *iridx, *funval;
2346 compile_error(ast_ctx(self), "(.2) array indexing here needs a compile-time constant");
2351 compile_error(ast_ctx(self), "value has no getter, don't know how to index it");
2355 cgen = self->index->codegen;
2356 if (!(*cgen)((ast_expression*)(self->index), func, false, &iridx))
2359 cgen = arr->getter->expression.codegen;
2360 if (!(*cgen)((ast_expression*)(arr->getter), func, true, &funval))
2363 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "fetch"), funval, false);
2366 ir_call_param(call, iridx);
2368 *out = ir_call_value(call);
2369 self->expression.outr = *out;
2370 (*out)->vtype = self->expression.vtype;
2371 codegen_output_type(self, *out);
2375 if (idx->expression.vtype == TYPE_FLOAT) {
2376 unsigned int arridx = idx->constval.vfloat;
2377 if (arridx >= self->array->count)
2379 compile_error(ast_ctx(self), "array index out of bounds: %i", arridx);
2382 *out = arr->ir_values[arridx];
2384 else if (idx->expression.vtype == TYPE_INTEGER) {
2385 unsigned int arridx = idx->constval.vint;
2386 if (arridx >= self->array->count)
2388 compile_error(ast_ctx(self), "array index out of bounds: %i", arridx);
2391 *out = arr->ir_values[arridx];
2394 compile_error(ast_ctx(self), "array indexing here needs an integer constant");
2397 (*out)->vtype = self->expression.vtype;
2398 codegen_output_type(self, *out);
2402 bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_value **out)
2404 ast_expression_codegen *cgen;
2412 ir_block *ontrue_endblock = NULL;
2413 ir_block *onfalse_endblock = NULL;
2414 ir_block *merge = NULL;
2416 /* We don't output any value, thus also don't care about r/lvalue */
2420 if (self->expression.outr) {
2421 compile_error(ast_ctx(self), "internal error: ast_ifthen cannot be reused, it bears no result!");
2424 self->expression.outr = (ir_value*)1;
2426 /* generate the condition */
2427 cgen = self->cond->codegen;
2428 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2430 /* update the block which will get the jump - because short-logic or ternaries may have changed this */
2431 cond = func->curblock;
2435 if (self->on_true) {
2436 /* create on-true block */
2437 ontrue = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "ontrue"));
2441 /* enter the block */
2442 func->curblock = ontrue;
2445 cgen = self->on_true->codegen;
2446 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &dummy))
2449 /* we now need to work from the current endpoint */
2450 ontrue_endblock = func->curblock;
2455 if (self->on_false) {
2456 /* create on-false block */
2457 onfalse = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "onfalse"));
2461 /* enter the block */
2462 func->curblock = onfalse;
2465 cgen = self->on_false->codegen;
2466 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &dummy))
2469 /* we now need to work from the current endpoint */
2470 onfalse_endblock = func->curblock;
2474 /* Merge block were they all merge in to */
2475 if (!ontrue || !onfalse || !ontrue_endblock->final || !onfalse_endblock->final)
2477 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "endif"));
2480 /* add jumps ot the merge block */
2481 if (ontrue && !ontrue_endblock->final && !ir_block_create_jump(ontrue_endblock, ast_ctx(self), merge))
2483 if (onfalse && !onfalse_endblock->final && !ir_block_create_jump(onfalse_endblock, ast_ctx(self), merge))
2486 /* Now enter the merge block */
2487 func->curblock = merge;
2490 /* we create the if here, that way all blocks are ordered :)
2492 if (!ir_block_create_if(cond, ast_ctx(self), condval,
2493 (ontrue ? ontrue : merge),
2494 (onfalse ? onfalse : merge)))
2502 bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_value **out)
2504 ast_expression_codegen *cgen;
2507 ir_value *trueval, *falseval;
2510 ir_block *cond = func->curblock;
2511 ir_block *cond_out = NULL;
2512 ir_block *ontrue, *ontrue_out = NULL;
2513 ir_block *onfalse, *onfalse_out = NULL;
2516 /* Ternary can never create an lvalue... */
2520 /* In theory it shouldn't be possible to pass through a node twice, but
2521 * in case we add any kind of optimization pass for the AST itself, it
2522 * may still happen, thus we remember a created ir_value and simply return one
2523 * if it already exists.
2525 if (self->expression.outr) {
2526 *out = self->expression.outr;
2530 /* In the following, contraty to ast_ifthen, we assume both paths exist. */
2532 /* generate the condition */
2533 func->curblock = cond;
2534 cgen = self->cond->codegen;
2535 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2537 cond_out = func->curblock;
2539 /* create on-true block */
2540 ontrue = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_T"));
2545 /* enter the block */
2546 func->curblock = ontrue;
2549 cgen = self->on_true->codegen;
2550 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &trueval))
2553 ontrue_out = func->curblock;
2556 /* create on-false block */
2557 onfalse = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_F"));
2562 /* enter the block */
2563 func->curblock = onfalse;
2566 cgen = self->on_false->codegen;
2567 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &falseval))
2570 onfalse_out = func->curblock;
2573 /* create merge block */
2574 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_out"));
2577 /* jump to merge block */
2578 if (!ir_block_create_jump(ontrue_out, ast_ctx(self), merge))
2580 if (!ir_block_create_jump(onfalse_out, ast_ctx(self), merge))
2583 /* create if instruction */
2584 if (!ir_block_create_if(cond_out, ast_ctx(self), condval, ontrue, onfalse))
2587 /* Now enter the merge block */
2588 func->curblock = merge;
2590 /* Here, now, we need a PHI node
2591 * but first some sanity checking...
2593 if (trueval->vtype != falseval->vtype && trueval->vtype != TYPE_NIL && falseval->vtype != TYPE_NIL) {
2594 /* error("ternary with different types on the two sides"); */
2595 compile_error(ast_ctx(self), "internal error: ternary operand types invalid");
2600 phi = ir_block_create_phi(merge, ast_ctx(self), ast_function_label(func, "phi"), self->expression.vtype);
2602 compile_error(ast_ctx(self), "internal error: failed to generate phi node");
2605 ir_phi_add(phi, ontrue_out, trueval);
2606 ir_phi_add(phi, onfalse_out, falseval);
2608 self->expression.outr = ir_phi_value(phi);
2609 *out = self->expression.outr;
2611 codegen_output_type(self, *out);
2616 bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value **out)
2618 ast_expression_codegen *cgen;
2620 ir_value *dummy = NULL;
2621 ir_value *precond = NULL;
2622 ir_value *postcond = NULL;
2624 /* Since we insert some jumps "late" so we have blocks
2625 * ordered "nicely", we need to keep track of the actual end-blocks
2626 * of expressions to add the jumps to.
2628 ir_block *bbody = NULL, *end_bbody = NULL;
2629 ir_block *bprecond = NULL, *end_bprecond = NULL;
2630 ir_block *bpostcond = NULL, *end_bpostcond = NULL;
2631 ir_block *bincrement = NULL, *end_bincrement = NULL;
2632 ir_block *bout = NULL, *bin = NULL;
2634 /* let's at least move the outgoing block to the end */
2637 /* 'break' and 'continue' need to be able to find the right blocks */
2638 ir_block *bcontinue = NULL;
2639 ir_block *bbreak = NULL;
2641 ir_block *tmpblock = NULL;
2646 if (self->expression.outr) {
2647 compile_error(ast_ctx(self), "internal error: ast_loop cannot be reused, it bears no result!");
2650 self->expression.outr = (ir_value*)1;
2653 * Should we ever need some kind of block ordering, better make this function
2654 * move blocks around than write a block ordering algorithm later... after all
2655 * the ast and ir should work together, not against each other.
2658 /* initexpr doesn't get its own block, it's pointless, it could create more blocks
2659 * anyway if for example it contains a ternary.
2663 cgen = self->initexpr->codegen;
2664 if (!(*cgen)((ast_expression*)(self->initexpr), func, false, &dummy))
2668 /* Store the block from which we enter this chaos */
2669 bin = func->curblock;
2671 /* The pre-loop condition needs its own block since we
2672 * need to be able to jump to the start of that expression.
2676 bprecond = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "pre_loop_cond"));
2680 /* the pre-loop-condition the least important place to 'continue' at */
2681 bcontinue = bprecond;
2684 func->curblock = bprecond;
2687 cgen = self->precond->codegen;
2688 if (!(*cgen)((ast_expression*)(self->precond), func, false, &precond))
2691 end_bprecond = func->curblock;
2693 bprecond = end_bprecond = NULL;
2696 /* Now the next blocks won't be ordered nicely, but we need to
2697 * generate them this early for 'break' and 'continue'.
2699 if (self->increment) {
2700 bincrement = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "loop_increment"));
2703 bcontinue = bincrement; /* increment comes before the pre-loop-condition */
2705 bincrement = end_bincrement = NULL;
2708 if (self->postcond) {
2709 bpostcond = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "post_loop_cond"));
2712 bcontinue = bpostcond; /* postcond comes before the increment */
2714 bpostcond = end_bpostcond = NULL;
2717 bout_id = vec_size(func->ir_func->blocks);
2718 bout = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "after_loop"));
2723 /* The loop body... */
2724 /* if (self->body) */
2726 bbody = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "loop_body"));
2731 func->curblock = bbody;
2733 vec_push(func->breakblocks, bbreak);
2735 vec_push(func->continueblocks, bcontinue);
2737 vec_push(func->continueblocks, bbody);
2741 cgen = self->body->codegen;
2742 if (!(*cgen)((ast_expression*)(self->body), func, false, &dummy))
2746 end_bbody = func->curblock;
2747 vec_pop(func->breakblocks);
2748 vec_pop(func->continueblocks);
2751 /* post-loop-condition */
2755 func->curblock = bpostcond;
2758 cgen = self->postcond->codegen;
2759 if (!(*cgen)((ast_expression*)(self->postcond), func, false, &postcond))
2762 end_bpostcond = func->curblock;
2765 /* The incrementor */
2766 if (self->increment)
2769 func->curblock = bincrement;
2772 cgen = self->increment->codegen;
2773 if (!(*cgen)((ast_expression*)(self->increment), func, false, &dummy))
2776 end_bincrement = func->curblock;
2779 /* In any case now, we continue from the outgoing block */
2780 func->curblock = bout;
2782 /* Now all blocks are in place */
2783 /* From 'bin' we jump to whatever comes first */
2784 if (bprecond) tmpblock = bprecond;
2785 else if (bbody) tmpblock = bbody;
2786 else if (bpostcond) tmpblock = bpostcond;
2787 else tmpblock = bout;
2788 if (!ir_block_create_jump(bin, ast_ctx(self), tmpblock))
2794 ir_block *ontrue, *onfalse;
2795 if (bbody) ontrue = bbody;
2796 else if (bincrement) ontrue = bincrement;
2797 else if (bpostcond) ontrue = bpostcond;
2798 else ontrue = bprecond;
2800 if (self->pre_not) {
2805 if (!ir_block_create_if(end_bprecond, ast_ctx(self), precond, ontrue, onfalse))
2812 if (bincrement) tmpblock = bincrement;
2813 else if (bpostcond) tmpblock = bpostcond;
2814 else if (bprecond) tmpblock = bprecond;
2815 else tmpblock = bbody;
2816 if (!end_bbody->final && !ir_block_create_jump(end_bbody, ast_ctx(self), tmpblock))
2820 /* from increment */
2823 if (bpostcond) tmpblock = bpostcond;
2824 else if (bprecond) tmpblock = bprecond;
2825 else if (bbody) tmpblock = bbody;
2826 else tmpblock = bout;
2827 if (!ir_block_create_jump(end_bincrement, ast_ctx(self), tmpblock))
2834 ir_block *ontrue, *onfalse;
2835 if (bprecond) ontrue = bprecond;
2836 else if (bbody) ontrue = bbody;
2837 else if (bincrement) ontrue = bincrement;
2838 else ontrue = bpostcond;
2840 if (self->post_not) {
2845 if (!ir_block_create_if(end_bpostcond, ast_ctx(self), postcond, ontrue, onfalse))
2849 /* Move 'bout' to the end */
2850 vec_remove(func->ir_func->blocks, bout_id, 1);
2851 vec_push(func->ir_func->blocks, bout);
2856 bool ast_breakcont_codegen(ast_breakcont *self, ast_function *func, bool lvalue, ir_value **out)
2863 compile_error(ast_ctx(self), "break/continue expression is not an l-value");
2867 if (self->expression.outr) {
2868 compile_error(ast_ctx(self), "internal error: ast_breakcont cannot be reused!");
2871 self->expression.outr = (ir_value*)1;
2873 if (self->is_continue)
2874 target = func->continueblocks[vec_size(func->continueblocks)-1-self->levels];
2876 target = func->breakblocks[vec_size(func->breakblocks)-1-self->levels];
2879 compile_error(ast_ctx(self), "%s is lacking a target block", (self->is_continue ? "continue" : "break"));
2883 if (!ir_block_create_jump(func->curblock, ast_ctx(self), target))
2888 bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_value **out)
2890 ast_expression_codegen *cgen;
2892 ast_switch_case *def_case = NULL;
2893 ir_block *def_bfall = NULL;
2894 ir_block *def_bfall_to = NULL;
2895 bool set_def_bfall_to = false;
2897 ir_value *dummy = NULL;
2898 ir_value *irop = NULL;
2899 ir_block *bout = NULL;
2900 ir_block *bfall = NULL;
2908 compile_error(ast_ctx(self), "switch expression is not an l-value");
2912 if (self->expression.outr) {
2913 compile_error(ast_ctx(self), "internal error: ast_switch cannot be reused!");
2916 self->expression.outr = (ir_value*)1;
2921 cgen = self->operand->codegen;
2922 if (!(*cgen)((ast_expression*)(self->operand), func, false, &irop))
2925 if (!vec_size(self->cases))
2928 cmpinstr = type_eq_instr[irop->vtype];
2929 if (cmpinstr >= VINSTR_END) {
2930 ast_type_to_string(self->operand, typestr, sizeof(typestr));
2931 compile_error(ast_ctx(self), "invalid type to perform a switch on: %s", typestr);
2935 bout_id = vec_size(func->ir_func->blocks);
2936 bout = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "after_switch"));
2940 /* setup the break block */
2941 vec_push(func->breakblocks, bout);
2943 /* Now create all cases */
2944 for (c = 0; c < vec_size(self->cases); ++c) {
2945 ir_value *cond, *val;
2946 ir_block *bcase, *bnot;
2949 ast_switch_case *swcase = &self->cases[c];
2951 if (swcase->value) {
2952 /* A regular case */
2953 /* generate the condition operand */
2954 cgen = swcase->value->codegen;
2955 if (!(*cgen)((ast_expression*)(swcase->value), func, false, &val))
2957 /* generate the condition */
2958 cond = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "switch_eq"), cmpinstr, irop, val);
2962 bcase = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "case"));
2963 bnot_id = vec_size(func->ir_func->blocks);
2964 bnot = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "not_case"));
2965 if (!bcase || !bnot)
2967 if (set_def_bfall_to) {
2968 set_def_bfall_to = false;
2969 def_bfall_to = bcase;
2971 if (!ir_block_create_if(func->curblock, ast_ctx(self), cond, bcase, bnot))
2974 /* Make the previous case-end fall through */
2975 if (bfall && !bfall->final) {
2976 if (!ir_block_create_jump(bfall, ast_ctx(self), bcase))
2980 /* enter the case */
2981 func->curblock = bcase;
2982 cgen = swcase->code->codegen;
2983 if (!(*cgen)((ast_expression*)swcase->code, func, false, &dummy))
2986 /* remember this block to fall through from */
2987 bfall = func->curblock;
2989 /* enter the else and move it down */
2990 func->curblock = bnot;
2991 vec_remove(func->ir_func->blocks, bnot_id, 1);
2992 vec_push(func->ir_func->blocks, bnot);
2994 /* The default case */
2995 /* Remember where to fall through from: */
2998 /* remember which case it was */
3000 /* And the next case will be remembered */
3001 set_def_bfall_to = true;
3005 /* Jump from the last bnot to bout */
3006 if (bfall && !bfall->final && !ir_block_create_jump(bfall, ast_ctx(self), bout)) {
3008 astwarning(ast_ctx(bfall), WARN_???, "missing break after last case");
3013 /* If there was a default case, put it down here */
3017 /* No need to create an extra block */
3018 bcase = func->curblock;
3020 /* Insert the fallthrough jump */
3021 if (def_bfall && !def_bfall->final) {
3022 if (!ir_block_create_jump(def_bfall, ast_ctx(self), bcase))
3026 /* Now generate the default code */
3027 cgen = def_case->code->codegen;
3028 if (!(*cgen)((ast_expression*)def_case->code, func, false, &dummy))
3031 /* see if we need to fall through */
3032 if (def_bfall_to && !func->curblock->final)
3034 if (!ir_block_create_jump(func->curblock, ast_ctx(self), def_bfall_to))
3039 /* Jump from the last bnot to bout */
3040 if (!func->curblock->final && !ir_block_create_jump(func->curblock, ast_ctx(self), bout))
3042 /* enter the outgoing block */
3043 func->curblock = bout;
3045 /* restore the break block */
3046 vec_pop(func->breakblocks);
3048 /* Move 'bout' to the end, it's nicer */
3049 vec_remove(func->ir_func->blocks, bout_id, 1);
3050 vec_push(func->ir_func->blocks, bout);
3055 bool ast_label_codegen(ast_label *self, ast_function *func, bool lvalue, ir_value **out)
3060 if (self->undefined) {
3061 compile_error(ast_ctx(self), "internal error: ast_label never defined");
3067 compile_error(ast_ctx(self), "internal error: ast_label cannot be an lvalue");
3071 /* simply create a new block and jump to it */
3072 self->irblock = ir_function_create_block(ast_ctx(self), func->ir_func, self->name);
3073 if (!self->irblock) {
3074 compile_error(ast_ctx(self), "failed to allocate label block `%s`", self->name);
3077 if (!func->curblock->final) {
3078 if (!ir_block_create_jump(func->curblock, ast_ctx(self), self->irblock))
3082 /* enter the new block */
3083 func->curblock = self->irblock;
3085 /* Generate all the leftover gotos */
3086 for (i = 0; i < vec_size(self->gotos); ++i) {
3087 if (!ast_goto_codegen(self->gotos[i], func, false, &dummy))
3094 bool ast_goto_codegen(ast_goto *self, ast_function *func, bool lvalue, ir_value **out)
3098 compile_error(ast_ctx(self), "internal error: ast_goto cannot be an lvalue");
3102 if (self->target->irblock) {
3103 if (self->irblock_from) {
3104 /* we already tried once, this is the callback */
3105 self->irblock_from->final = false;
3106 if (!ir_block_create_goto(self->irblock_from, ast_ctx(self), self->target->irblock)) {
3107 compile_error(ast_ctx(self), "failed to generate goto to `%s`", self->name);
3113 if (!ir_block_create_goto(func->curblock, ast_ctx(self), self->target->irblock)) {
3114 compile_error(ast_ctx(self), "failed to generate goto to `%s`", self->name);
3121 /* the target has not yet been created...
3122 * close this block in a sneaky way:
3124 func->curblock->final = true;
3125 self->irblock_from = func->curblock;
3126 ast_label_register_goto(self->target, self);
3132 bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value **out)
3134 ast_expression_codegen *cgen;
3136 ir_instr *callinstr;
3139 ir_value *funval = NULL;
3141 /* return values are never lvalues */
3143 compile_error(ast_ctx(self), "not an l-value (function call)");
3147 if (self->expression.outr) {
3148 *out = self->expression.outr;
3152 cgen = self->func->codegen;
3153 if (!(*cgen)((ast_expression*)(self->func), func, false, &funval))
3161 for (i = 0; i < vec_size(self->params); ++i)
3164 ast_expression *expr = self->params[i];
3166 cgen = expr->codegen;
3167 if (!(*cgen)(expr, func, false, ¶m))
3171 vec_push(params, param);
3174 /* varargs counter */
3175 if (self->va_count) {
3177 ir_builder *builder = func->curblock->owner->owner;
3178 cgen = self->va_count->codegen;
3179 if (!(*cgen)((ast_expression*)(self->va_count), func, false, &va_count))
3181 if (!ir_block_create_store_op(func->curblock, ast_ctx(self), INSTR_STORE_F,
3182 ir_builder_get_va_count(builder), va_count))
3188 callinstr = ir_block_create_call(func->curblock, ast_ctx(self),
3189 ast_function_label(func, "call"),
3190 funval, !!(self->func->flags & AST_FLAG_NORETURN));
3194 for (i = 0; i < vec_size(params); ++i) {
3195 ir_call_param(callinstr, params[i]);
3198 *out = ir_call_value(callinstr);
3199 self->expression.outr = *out;
3201 codegen_output_type(self, *out);