5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is furnished to do
10 * so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * 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 )->node.destroy = (ast_node_delete*)destroyfn
39 /* It must not be possible to get here. */
40 static GMQCC_NORETURN void _ast_node_destroy(ast_node *self)
43 con_err("ast node missing destroy()\n");
47 /* Initialize main ast node aprts */
48 static void ast_node_init(ast_node *self, lex_ctx ctx, int nodetype)
50 self->node.context = ctx;
51 self->node.destroy = &_ast_node_destroy;
52 self->node.keep = false;
53 self->node.nodetype = nodetype;
54 self->node.side_effects = false;
57 /* weight and side effects */
58 static void _ast_propagate_effects(ast_node *self, ast_node *other)
60 if (ast_side_effects(other))
61 ast_side_effects(self) = true;
63 #define ast_propagate_effects(s,o) _ast_propagate_effects(((ast_node*)(s)), ((ast_node*)(o)))
65 /* General expression initialization */
66 static void ast_expression_init(ast_expression *self,
67 ast_expression_codegen *codegen)
69 self->expression.codegen = codegen;
70 self->expression.vtype = TYPE_VOID;
71 self->expression.next = NULL;
72 self->expression.outl = NULL;
73 self->expression.outr = NULL;
74 self->expression.params = NULL;
75 self->expression.count = 0;
76 self->expression.flags = 0;
79 static void ast_expression_delete(ast_expression *self)
82 if (self->expression.next)
83 ast_delete(self->expression.next);
84 for (i = 0; i < vec_size(self->expression.params); ++i) {
85 ast_delete(self->expression.params[i]);
87 vec_free(self->expression.params);
90 static void ast_expression_delete_full(ast_expression *self)
92 ast_expression_delete(self);
96 ast_value* ast_value_copy(const ast_value *self)
99 const ast_expression_common *fromex;
100 ast_expression_common *selfex;
101 ast_value *cp = ast_value_new(self->expression.node.context, self->name, self->expression.vtype);
102 if (self->expression.next) {
103 cp->expression.next = ast_type_copy(self->expression.node.context, self->expression.next);
104 if (!cp->expression.next) {
105 ast_value_delete(cp);
109 fromex = &self->expression;
110 selfex = &cp->expression;
111 selfex->count = fromex->count;
112 selfex->flags = fromex->flags;
113 for (i = 0; i < vec_size(fromex->params); ++i) {
114 ast_value *v = ast_value_copy(fromex->params[i]);
116 ast_value_delete(cp);
119 vec_push(selfex->params, v);
124 bool ast_type_adopt_impl(ast_expression *self, const ast_expression *other)
127 const ast_expression_common *fromex;
128 ast_expression_common *selfex;
129 self->expression.vtype = other->expression.vtype;
130 if (other->expression.next) {
131 self->expression.next = (ast_expression*)ast_type_copy(ast_ctx(self), other->expression.next);
132 if (!self->expression.next)
135 fromex = &other->expression;
136 selfex = &self->expression;
137 selfex->count = fromex->count;
138 selfex->flags = fromex->flags;
139 for (i = 0; i < vec_size(fromex->params); ++i) {
140 ast_value *v = ast_value_copy(fromex->params[i]);
143 vec_push(selfex->params, v);
148 static ast_expression* ast_shallow_type(lex_ctx ctx, int vtype)
150 ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
151 ast_expression_init(self, NULL);
152 self->expression.codegen = NULL;
153 self->expression.next = NULL;
154 self->expression.vtype = vtype;
158 ast_expression* ast_type_copy(lex_ctx ctx, const ast_expression *ex)
161 const ast_expression_common *fromex;
162 ast_expression_common *selfex;
168 ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
169 ast_expression_init(self, NULL);
171 fromex = &ex->expression;
172 selfex = &self->expression;
174 /* This may never be codegen()d */
175 selfex->codegen = NULL;
177 selfex->vtype = fromex->vtype;
180 selfex->next = ast_type_copy(ctx, fromex->next);
182 ast_expression_delete_full(self);
189 selfex->count = fromex->count;
190 selfex->flags = fromex->flags;
191 for (i = 0; i < vec_size(fromex->params); ++i) {
192 ast_value *v = ast_value_copy(fromex->params[i]);
194 ast_expression_delete_full(self);
197 vec_push(selfex->params, v);
204 bool ast_compare_type(ast_expression *a, ast_expression *b)
206 if (a->expression.vtype != b->expression.vtype)
208 if (!a->expression.next != !b->expression.next)
210 if (vec_size(a->expression.params) != vec_size(b->expression.params))
212 if (a->expression.flags != b->expression.flags)
214 if (vec_size(a->expression.params)) {
216 for (i = 0; i < vec_size(a->expression.params); ++i) {
217 if (!ast_compare_type((ast_expression*)a->expression.params[i],
218 (ast_expression*)b->expression.params[i]))
222 if (a->expression.next)
223 return ast_compare_type(a->expression.next, b->expression.next);
227 static size_t ast_type_to_string_impl(ast_expression *e, char *buf, size_t bufsize, size_t pos)
234 if (pos + 6 >= bufsize)
236 strcpy(buf + pos, "(null)");
240 if (pos + 1 >= bufsize)
243 switch (e->expression.vtype) {
245 strcpy(buf + pos, "(variant)");
250 return ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
253 if (pos + 3 >= bufsize)
257 pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
258 if (pos + 1 >= bufsize)
264 pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
265 if (pos + 2 >= bufsize)
267 if (!vec_size(e->expression.params)) {
273 pos = ast_type_to_string_impl((ast_expression*)(e->expression.params[0]), buf, bufsize, pos);
274 for (i = 1; i < vec_size(e->expression.params); ++i) {
275 if (pos + 2 >= bufsize)
279 pos = ast_type_to_string_impl((ast_expression*)(e->expression.params[i]), buf, bufsize, pos);
281 if (pos + 1 >= bufsize)
287 pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
288 if (pos + 1 >= bufsize)
291 pos += snprintf(buf + pos, bufsize - pos - 1, "%i", (int)e->expression.count);
292 if (pos + 1 >= bufsize)
298 typestr = type_name[e->expression.vtype];
299 typelen = strlen(typestr);
300 if (pos + typelen >= bufsize)
302 strcpy(buf + pos, typestr);
303 return pos + typelen;
307 buf[bufsize-3] = '.';
308 buf[bufsize-2] = '.';
309 buf[bufsize-1] = '.';
313 void ast_type_to_string(ast_expression *e, char *buf, size_t bufsize)
315 size_t pos = ast_type_to_string_impl(e, buf, bufsize-1, 0);
319 ast_value* ast_value_new(lex_ctx ctx, const char *name, int t)
321 ast_instantiate(ast_value, ctx, ast_value_delete);
322 ast_expression_init((ast_expression*)self,
323 (ast_expression_codegen*)&ast_value_codegen);
324 self->expression.node.keep = true; /* keep */
326 self->name = name ? util_strdup(name) : NULL;
327 self->expression.vtype = t;
328 self->expression.next = NULL;
329 self->isfield = false;
331 self->hasvalue = false;
333 memset(&self->constval, 0, sizeof(self->constval));
336 self->ir_values = NULL;
337 self->ir_value_count = 0;
345 void ast_value_delete(ast_value* self)
348 mem_d((void*)self->name);
349 if (self->hasvalue) {
350 switch (self->expression.vtype)
353 mem_d((void*)self->constval.vstring);
356 /* unlink us from the function node */
357 self->constval.vfunc->vtype = NULL;
359 /* NOTE: delete function? currently collected in
360 * the parser structure
367 mem_d(self->ir_values);
368 ast_expression_delete((ast_expression*)self);
372 void ast_value_params_add(ast_value *self, ast_value *p)
374 vec_push(self->expression.params, p);
377 bool ast_value_set_name(ast_value *self, const char *name)
380 mem_d((void*)self->name);
381 self->name = util_strdup(name);
385 ast_binary* ast_binary_new(lex_ctx ctx, int op,
386 ast_expression* left, ast_expression* right)
388 ast_instantiate(ast_binary, ctx, ast_binary_delete);
389 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binary_codegen);
395 ast_propagate_effects(self, left);
396 ast_propagate_effects(self, right);
398 if (op >= INSTR_EQ_F && op <= INSTR_GT)
399 self->expression.vtype = TYPE_FLOAT;
400 else if (op == INSTR_AND || op == INSTR_OR) {
401 if (OPTS_FLAG(PERL_LOGIC))
402 ast_type_adopt(self, right);
404 self->expression.vtype = TYPE_FLOAT;
406 else if (op == INSTR_BITAND || op == INSTR_BITOR)
407 self->expression.vtype = TYPE_FLOAT;
408 else if (op == INSTR_MUL_VF || op == INSTR_MUL_FV)
409 self->expression.vtype = TYPE_VECTOR;
410 else if (op == INSTR_MUL_V)
411 self->expression.vtype = TYPE_FLOAT;
413 self->expression.vtype = left->expression.vtype;
418 void ast_binary_delete(ast_binary *self)
420 ast_unref(self->left);
421 ast_unref(self->right);
422 ast_expression_delete((ast_expression*)self);
426 ast_binstore* ast_binstore_new(lex_ctx ctx, int storop, int op,
427 ast_expression* left, ast_expression* right)
429 ast_instantiate(ast_binstore, ctx, ast_binstore_delete);
430 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binstore_codegen);
432 ast_side_effects(self) = true;
434 self->opstore = storop;
437 self->source = right;
439 self->keep_dest = false;
441 if (!ast_type_adopt(self, left)) {
449 void ast_binstore_delete(ast_binstore *self)
451 if (!self->keep_dest)
452 ast_unref(self->dest);
453 ast_unref(self->source);
454 ast_expression_delete((ast_expression*)self);
458 ast_unary* ast_unary_new(lex_ctx ctx, int op,
459 ast_expression *expr)
461 ast_instantiate(ast_unary, ctx, ast_unary_delete);
462 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_unary_codegen);
465 self->operand = expr;
467 ast_propagate_effects(self, expr);
469 if (op >= INSTR_NOT_F && op <= INSTR_NOT_FNC) {
470 self->expression.vtype = TYPE_FLOAT;
472 compile_error(ctx, "cannot determine type of unary operation %s", asm_instr[op].m);
477 void ast_unary_delete(ast_unary *self)
479 if (self->operand) ast_unref(self->operand);
480 ast_expression_delete((ast_expression*)self);
484 ast_return* ast_return_new(lex_ctx ctx, ast_expression *expr)
486 ast_instantiate(ast_return, ctx, ast_return_delete);
487 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_return_codegen);
489 self->operand = expr;
492 ast_propagate_effects(self, expr);
497 void ast_return_delete(ast_return *self)
500 ast_unref(self->operand);
501 ast_expression_delete((ast_expression*)self);
505 ast_entfield* ast_entfield_new(lex_ctx ctx, ast_expression *entity, ast_expression *field)
507 if (field->expression.vtype != TYPE_FIELD) {
508 compile_error(ctx, "ast_entfield_new with expression not of type field");
511 return ast_entfield_new_force(ctx, entity, field, field->expression.next);
514 ast_entfield* ast_entfield_new_force(lex_ctx ctx, ast_expression *entity, ast_expression *field, const ast_expression *outtype)
516 ast_instantiate(ast_entfield, ctx, ast_entfield_delete);
520 /* Error: field has no type... */
524 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_entfield_codegen);
526 self->entity = entity;
528 ast_propagate_effects(self, entity);
529 ast_propagate_effects(self, field);
531 if (!ast_type_adopt(self, outtype)) {
532 ast_entfield_delete(self);
539 void ast_entfield_delete(ast_entfield *self)
541 ast_unref(self->entity);
542 ast_unref(self->field);
543 ast_expression_delete((ast_expression*)self);
547 ast_member* ast_member_new(lex_ctx ctx, ast_expression *owner, unsigned int field, const char *name)
549 ast_instantiate(ast_member, ctx, ast_member_delete);
555 if (owner->expression.vtype != TYPE_VECTOR &&
556 owner->expression.vtype != TYPE_FIELD) {
557 compile_error(ctx, "member-access on an invalid owner of type %s", type_name[owner->expression.vtype]);
562 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_member_codegen);
563 self->expression.node.keep = true; /* keep */
565 if (owner->expression.vtype == TYPE_VECTOR) {
566 self->expression.vtype = TYPE_FLOAT;
567 self->expression.next = NULL;
569 self->expression.vtype = TYPE_FIELD;
570 self->expression.next = ast_shallow_type(ctx, TYPE_FLOAT);
574 ast_propagate_effects(self, owner);
578 self->name = util_strdup(name);
585 void ast_member_delete(ast_member *self)
587 /* The owner is always an ast_value, which has .keep=true,
588 * also: ast_members are usually deleted after the owner, thus
589 * this will cause invalid access
590 ast_unref(self->owner);
591 * once we allow (expression).x to access a vector-member, we need
592 * to change this: preferably by creating an alternate ast node for this
593 * purpose that is not garbage-collected.
595 ast_expression_delete((ast_expression*)self);
599 bool ast_member_set_name(ast_member *self, const char *name)
602 mem_d((void*)self->name);
603 self->name = util_strdup(name);
607 ast_array_index* ast_array_index_new(lex_ctx ctx, ast_expression *array, ast_expression *index)
609 ast_expression *outtype;
610 ast_instantiate(ast_array_index, ctx, ast_array_index_delete);
612 outtype = array->expression.next;
615 /* Error: field has no type... */
619 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_array_index_codegen);
623 ast_propagate_effects(self, array);
624 ast_propagate_effects(self, index);
626 if (!ast_type_adopt(self, outtype)) {
627 ast_array_index_delete(self);
630 if (array->expression.vtype == TYPE_FIELD && outtype->expression.vtype == TYPE_ARRAY) {
631 if (self->expression.vtype != TYPE_ARRAY) {
632 compile_error(ast_ctx(self), "array_index node on type");
633 ast_array_index_delete(self);
636 self->array = outtype;
637 self->expression.vtype = TYPE_FIELD;
643 void ast_array_index_delete(ast_array_index *self)
645 ast_unref(self->array);
646 ast_unref(self->index);
647 ast_expression_delete((ast_expression*)self);
651 ast_ifthen* ast_ifthen_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
653 ast_instantiate(ast_ifthen, ctx, ast_ifthen_delete);
654 if (!ontrue && !onfalse) {
655 /* because it is invalid */
659 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ifthen_codegen);
662 self->on_true = ontrue;
663 self->on_false = onfalse;
664 ast_propagate_effects(self, cond);
666 ast_propagate_effects(self, ontrue);
668 ast_propagate_effects(self, onfalse);
673 void ast_ifthen_delete(ast_ifthen *self)
675 ast_unref(self->cond);
677 ast_unref(self->on_true);
679 ast_unref(self->on_false);
680 ast_expression_delete((ast_expression*)self);
684 ast_ternary* ast_ternary_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
686 ast_instantiate(ast_ternary, ctx, ast_ternary_delete);
687 /* This time NEITHER must be NULL */
688 if (!ontrue || !onfalse) {
692 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ternary_codegen);
695 self->on_true = ontrue;
696 self->on_false = onfalse;
697 ast_propagate_effects(self, cond);
698 ast_propagate_effects(self, ontrue);
699 ast_propagate_effects(self, onfalse);
701 if (!ast_type_adopt(self, ontrue)) {
702 ast_ternary_delete(self);
709 void ast_ternary_delete(ast_ternary *self)
711 ast_unref(self->cond);
712 ast_unref(self->on_true);
713 ast_unref(self->on_false);
714 ast_expression_delete((ast_expression*)self);
718 ast_loop* ast_loop_new(lex_ctx ctx,
719 ast_expression *initexpr,
720 ast_expression *precond, bool pre_not,
721 ast_expression *postcond, bool post_not,
722 ast_expression *increment,
723 ast_expression *body)
725 ast_instantiate(ast_loop, ctx, ast_loop_delete);
726 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_loop_codegen);
728 self->initexpr = initexpr;
729 self->precond = precond;
730 self->postcond = postcond;
731 self->increment = increment;
734 self->pre_not = pre_not;
735 self->post_not = post_not;
738 ast_propagate_effects(self, initexpr);
740 ast_propagate_effects(self, precond);
742 ast_propagate_effects(self, postcond);
744 ast_propagate_effects(self, increment);
746 ast_propagate_effects(self, body);
751 void ast_loop_delete(ast_loop *self)
754 ast_unref(self->initexpr);
756 ast_unref(self->precond);
758 ast_unref(self->postcond);
760 ast_unref(self->increment);
762 ast_unref(self->body);
763 ast_expression_delete((ast_expression*)self);
767 ast_breakcont* ast_breakcont_new(lex_ctx ctx, bool iscont)
769 ast_instantiate(ast_breakcont, ctx, ast_breakcont_delete);
770 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_breakcont_codegen);
772 self->is_continue = iscont;
777 void ast_breakcont_delete(ast_breakcont *self)
779 ast_expression_delete((ast_expression*)self);
783 ast_switch* ast_switch_new(lex_ctx ctx, ast_expression *op)
785 ast_instantiate(ast_switch, ctx, ast_switch_delete);
786 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_switch_codegen);
791 ast_propagate_effects(self, op);
796 void ast_switch_delete(ast_switch *self)
799 ast_unref(self->operand);
801 for (i = 0; i < vec_size(self->cases); ++i) {
802 if (self->cases[i].value)
803 ast_unref(self->cases[i].value);
804 ast_unref(self->cases[i].code);
806 vec_free(self->cases);
808 ast_expression_delete((ast_expression*)self);
812 ast_label* ast_label_new(lex_ctx ctx, const char *name)
814 ast_instantiate(ast_label, ctx, ast_label_delete);
815 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_label_codegen);
817 self->name = util_strdup(name);
818 self->irblock = NULL;
824 void ast_label_delete(ast_label *self)
826 mem_d((void*)self->name);
827 vec_free(self->gotos);
828 ast_expression_delete((ast_expression*)self);
832 void ast_label_register_goto(ast_label *self, ast_goto *g)
834 vec_push(self->gotos, g);
837 ast_goto* ast_goto_new(lex_ctx ctx, const char *name)
839 ast_instantiate(ast_goto, ctx, ast_goto_delete);
840 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_goto_codegen);
842 self->name = util_strdup(name);
844 self->irblock_from = NULL;
849 void ast_goto_delete(ast_goto *self)
851 mem_d((void*)self->name);
852 ast_expression_delete((ast_expression*)self);
856 void ast_goto_set_label(ast_goto *self, ast_label *label)
858 self->target = label;
861 ast_call* ast_call_new(lex_ctx ctx,
862 ast_expression *funcexpr)
864 ast_instantiate(ast_call, ctx, ast_call_delete);
865 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_call_codegen);
867 ast_side_effects(self) = true;
870 self->func = funcexpr;
872 ast_type_adopt(self, funcexpr->expression.next);
877 void ast_call_delete(ast_call *self)
880 for (i = 0; i < vec_size(self->params); ++i)
881 ast_unref(self->params[i]);
882 vec_free(self->params);
885 ast_unref(self->func);
887 ast_expression_delete((ast_expression*)self);
891 bool ast_call_check_types(ast_call *self)
895 const ast_expression *func = self->func;
896 size_t count = vec_size(self->params);
897 if (count > vec_size(func->expression.params))
898 count = vec_size(func->expression.params);
900 for (i = 0; i < count; ++i) {
901 if (!ast_compare_type(self->params[i], (ast_expression*)(func->expression.params[i]))) {
904 ast_type_to_string(self->params[i], tgot, sizeof(tgot));
905 ast_type_to_string((ast_expression*)func->expression.params[i], texp, sizeof(texp));
906 compile_error(ast_ctx(self), "invalid type for parameter %u in function call: expected %s, got %s",
907 (unsigned int)(i+1), texp, tgot);
908 /* we don't immediately return */
915 ast_store* ast_store_new(lex_ctx ctx, int op,
916 ast_expression *dest, ast_expression *source)
918 ast_instantiate(ast_store, ctx, ast_store_delete);
919 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_store_codegen);
921 ast_side_effects(self) = true;
925 self->source = source;
927 if (!ast_type_adopt(self, dest)) {
935 void ast_store_delete(ast_store *self)
937 ast_unref(self->dest);
938 ast_unref(self->source);
939 ast_expression_delete((ast_expression*)self);
943 ast_block* ast_block_new(lex_ctx ctx)
945 ast_instantiate(ast_block, ctx, ast_block_delete);
946 ast_expression_init((ast_expression*)self,
947 (ast_expression_codegen*)&ast_block_codegen);
951 self->collect = NULL;
956 bool ast_block_add_expr(ast_block *self, ast_expression *e)
958 ast_propagate_effects(self, e);
959 vec_push(self->exprs, e);
960 if (self->expression.next) {
961 ast_delete(self->expression.next);
962 self->expression.next = NULL;
964 if (!ast_type_adopt(self, e)) {
965 compile_error(ast_ctx(self), "internal error: failed to adopt type");
971 void ast_block_collect(ast_block *self, ast_expression *expr)
973 vec_push(self->collect, expr);
974 expr->expression.node.keep = true;
977 void ast_block_delete(ast_block *self)
980 for (i = 0; i < vec_size(self->exprs); ++i)
981 ast_unref(self->exprs[i]);
982 vec_free(self->exprs);
983 for (i = 0; i < vec_size(self->locals); ++i)
984 ast_delete(self->locals[i]);
985 vec_free(self->locals);
986 for (i = 0; i < vec_size(self->collect); ++i)
987 ast_delete(self->collect[i]);
988 vec_free(self->collect);
989 ast_expression_delete((ast_expression*)self);
993 bool ast_block_set_type(ast_block *self, ast_expression *from)
995 if (self->expression.next)
996 ast_delete(self->expression.next);
997 if (!ast_type_adopt(self, from))
1002 ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype)
1004 ast_instantiate(ast_function, ctx, ast_function_delete);
1008 vtype->expression.vtype != TYPE_FUNCTION)
1010 compile_error(ast_ctx(self), "internal error: ast_function_new condition %i %i type=%i (probably 2 bodies?)",
1012 (int)vtype->hasvalue,
1013 vtype->expression.vtype);
1018 self->vtype = vtype;
1019 self->name = name ? util_strdup(name) : NULL;
1020 self->blocks = NULL;
1022 self->labelcount = 0;
1025 self->ir_func = NULL;
1026 self->curblock = NULL;
1028 self->breakblock = NULL;
1029 self->continueblock = NULL;
1031 vtype->hasvalue = true;
1032 vtype->constval.vfunc = self;
1037 void ast_function_delete(ast_function *self)
1041 mem_d((void*)self->name);
1043 /* ast_value_delete(self->vtype); */
1044 self->vtype->hasvalue = false;
1045 self->vtype->constval.vfunc = NULL;
1046 /* We use unref - if it was stored in a global table it is supposed
1047 * to be deleted from *there*
1049 ast_unref(self->vtype);
1051 for (i = 0; i < vec_size(self->blocks); ++i)
1052 ast_delete(self->blocks[i]);
1053 vec_free(self->blocks);
1057 const char* ast_function_label(ast_function *self, const char *prefix)
1063 if (!opts.dump && !opts.dumpfin && !opts.debug)
1066 id = (self->labelcount++);
1067 len = strlen(prefix);
1069 from = self->labelbuf + sizeof(self->labelbuf)-1;
1072 *from-- = (id%10) + '0';
1076 memcpy(from - len, prefix, len);
1080 /*********************************************************************/
1082 * by convention you must never pass NULL to the 'ir_value **out'
1083 * parameter. If you really don't care about the output, pass a dummy.
1084 * But I can't imagine a pituation where the output is truly unnecessary.
1087 void _ast_codegen_output_type(ast_expression_common *self, ir_value *out)
1089 if (out->vtype == TYPE_FIELD)
1090 out->fieldtype = self->next->expression.vtype;
1091 if (out->vtype == TYPE_FUNCTION)
1092 out->outtype = self->next->expression.vtype;
1095 #define codegen_output_type(a,o) (_ast_codegen_output_type(&((a)->expression),(o)))
1097 bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out)
1101 /* NOTE: This is the codegen for a variable used in an expression.
1102 * It is not the codegen to generate the value. For this purpose,
1103 * ast_local_codegen and ast_global_codegen are to be used before this
1104 * is executed. ast_function_codegen should take care of its locals,
1105 * and the ast-user should take care of ast_global_codegen to be used
1106 * on all the globals.
1109 char tname[1024]; /* typename is reserved in C++ */
1110 ast_type_to_string((ast_expression*)self, tname, sizeof(tname));
1111 compile_error(ast_ctx(self), "ast_value used before generated %s %s", tname, self->name);
1118 bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
1122 if (self->hasvalue && self->expression.vtype == TYPE_FUNCTION)
1124 ir_function *func = ir_builder_create_function(ir, self->name, self->expression.next->expression.vtype);
1127 func->context = ast_ctx(self);
1128 func->value->context = ast_ctx(self);
1130 self->constval.vfunc->ir_func = func;
1131 self->ir_v = func->value;
1132 /* The function is filled later on ast_function_codegen... */
1136 if (isfield && self->expression.vtype == TYPE_FIELD) {
1137 ast_expression *fieldtype = self->expression.next;
1139 if (self->hasvalue) {
1140 compile_error(ast_ctx(self), "TODO: constant field pointers with value");
1144 if (fieldtype->expression.vtype == TYPE_ARRAY) {
1149 ast_expression_common *elemtype;
1151 ast_value *array = (ast_value*)fieldtype;
1153 if (!ast_istype(fieldtype, ast_value)) {
1154 compile_error(ast_ctx(self), "internal error: ast_value required");
1158 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1159 if (!array->expression.count || array->expression.count > opts.max_array_size)
1160 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)array->expression.count);
1162 elemtype = &array->expression.next->expression;
1163 vtype = elemtype->vtype;
1165 v = ir_builder_create_field(ir, self->name, vtype);
1167 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", self->name);
1170 v->context = ast_ctx(self);
1171 v->unique_life = true;
1173 array->ir_v = self->ir_v = v;
1175 namelen = strlen(self->name);
1176 name = (char*)mem_a(namelen + 16);
1177 strcpy(name, self->name);
1179 array->ir_values = (ir_value**)mem_a(sizeof(array->ir_values[0]) * array->expression.count);
1180 array->ir_values[0] = v;
1181 for (ai = 1; ai < array->expression.count; ++ai) {
1182 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1183 array->ir_values[ai] = ir_builder_create_field(ir, name, vtype);
1184 if (!array->ir_values[ai]) {
1186 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", name);
1189 array->ir_values[ai]->context = ast_ctx(self);
1190 array->ir_values[ai]->unique_life = true;
1191 array->ir_values[ai]->locked = true;
1197 v = ir_builder_create_field(ir, self->name, self->expression.next->expression.vtype);
1200 v->context = ast_ctx(self);
1206 if (self->expression.vtype == TYPE_ARRAY) {
1211 ast_expression_common *elemtype = &self->expression.next->expression;
1212 int vtype = elemtype->vtype;
1214 /* same as with field arrays */
1215 if (!self->expression.count || self->expression.count > opts.max_array_size)
1216 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1218 v = ir_builder_create_global(ir, self->name, vtype);
1220 compile_error(ast_ctx(self), "ir_builder_create_global failed `%s`", self->name);
1223 v->context = ast_ctx(self);
1224 v->unique_life = true;
1227 namelen = strlen(self->name);
1228 name = (char*)mem_a(namelen + 16);
1229 strcpy(name, self->name);
1231 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1232 self->ir_values[0] = v;
1233 for (ai = 1; ai < self->expression.count; ++ai) {
1234 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1235 self->ir_values[ai] = ir_builder_create_global(ir, name, vtype);
1236 if (!self->ir_values[ai]) {
1238 compile_error(ast_ctx(self), "ir_builder_create_global failed `%s`", name);
1241 self->ir_values[ai]->context = ast_ctx(self);
1242 self->ir_values[ai]->unique_life = true;
1243 self->ir_values[ai]->locked = true;
1249 /* Arrays don't do this since there's no "array" value which spans across the
1252 v = ir_builder_create_global(ir, self->name, self->expression.vtype);
1254 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", self->name);
1257 codegen_output_type(self, v);
1258 v->context = ast_ctx(self);
1261 if (self->hasvalue) {
1262 switch (self->expression.vtype)
1265 if (!ir_value_set_float(v, self->constval.vfloat))
1269 if (!ir_value_set_vector(v, self->constval.vvec))
1273 if (!ir_value_set_string(v, self->constval.vstring))
1277 compile_error(ast_ctx(self), "TODO: global constant array");
1280 compile_error(ast_ctx(self), "global of type function not properly generated");
1282 /* Cannot generate an IR value for a function,
1283 * need a pointer pointing to a function rather.
1286 if (!self->constval.vfield) {
1287 compile_error(ast_ctx(self), "field constant without vfield set");
1290 if (!self->constval.vfield->ir_v) {
1291 compile_error(ast_ctx(self), "field constant generated before its field");
1294 if (!ir_value_set_field(v, self->constval.vfield->ir_v))
1298 compile_error(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1303 /* link us to the ir_value */
1308 error: /* clean up */
1313 bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
1316 if (self->hasvalue && self->expression.vtype == TYPE_FUNCTION)
1318 /* Do we allow local functions? I think not...
1319 * this is NOT a function pointer atm.
1324 if (self->expression.vtype == TYPE_ARRAY) {
1329 ast_expression_common *elemtype = &self->expression.next->expression;
1330 int vtype = elemtype->vtype;
1332 func->flags |= IR_FLAG_HAS_ARRAYS;
1335 compile_error(ast_ctx(self), "array-parameters are not supported");
1339 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1340 if (!self->expression.count || self->expression.count > opts.max_array_size) {
1341 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1344 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1345 if (!self->ir_values) {
1346 compile_error(ast_ctx(self), "failed to allocate array values");
1350 v = ir_function_create_local(func, self->name, vtype, param);
1352 compile_error(ast_ctx(self), "ir_function_create_local failed");
1355 v->context = ast_ctx(self);
1356 v->unique_life = true;
1359 namelen = strlen(self->name);
1360 name = (char*)mem_a(namelen + 16);
1361 strcpy(name, self->name);
1363 self->ir_values[0] = v;
1364 for (ai = 1; ai < self->expression.count; ++ai) {
1365 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1366 self->ir_values[ai] = ir_function_create_local(func, name, vtype, param);
1367 if (!self->ir_values[ai]) {
1368 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", name);
1371 self->ir_values[ai]->context = ast_ctx(self);
1372 self->ir_values[ai]->unique_life = true;
1373 self->ir_values[ai]->locked = true;
1378 v = ir_function_create_local(func, self->name, self->expression.vtype, param);
1381 codegen_output_type(self, v);
1382 v->context = ast_ctx(self);
1385 /* A constant local... hmmm...
1386 * I suppose the IR will have to deal with this
1388 if (self->hasvalue) {
1389 switch (self->expression.vtype)
1392 if (!ir_value_set_float(v, self->constval.vfloat))
1396 if (!ir_value_set_vector(v, self->constval.vvec))
1400 if (!ir_value_set_string(v, self->constval.vstring))
1404 compile_error(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1409 /* link us to the ir_value */
1413 if (!ast_generate_accessors(self, func->owner))
1417 error: /* clean up */
1422 bool ast_generate_accessors(ast_value *self, ir_builder *ir)
1425 bool warn = OPTS_WARN(WARN_USED_UNINITIALIZED);
1426 if (!self->setter || !self->getter)
1428 for (i = 0; i < self->expression.count; ++i) {
1429 if (!self->ir_values) {
1430 compile_error(ast_ctx(self), "internal error: no array values generated for `%s`", self->name);
1433 if (!self->ir_values[i]) {
1434 compile_error(ast_ctx(self), "internal error: not all array values have been generated for `%s`", self->name);
1437 if (self->ir_values[i]->life) {
1438 compile_error(ast_ctx(self), "internal error: function containing `%s` already generated", self->name);
1443 opts_set(opts.warn, WARN_USED_UNINITIALIZED, false);
1445 if (!ast_global_codegen (self->setter, ir, false) ||
1446 !ast_function_codegen(self->setter->constval.vfunc, ir) ||
1447 !ir_function_finalize(self->setter->constval.vfunc->ir_func))
1449 compile_error(ast_ctx(self), "internal error: failed to generate setter for `%s`", self->name);
1450 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1455 if (!ast_global_codegen (self->getter, ir, false) ||
1456 !ast_function_codegen(self->getter->constval.vfunc, ir) ||
1457 !ir_function_finalize(self->getter->constval.vfunc->ir_func))
1459 compile_error(ast_ctx(self), "internal error: failed to generate getter for `%s`", self->name);
1460 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1464 for (i = 0; i < self->expression.count; ++i) {
1465 vec_free(self->ir_values[i]->life);
1467 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1471 bool ast_function_codegen(ast_function *self, ir_builder *ir)
1475 ast_expression_common *ec;
1480 irf = self->ir_func;
1482 compile_error(ast_ctx(self), "ast_function's related ast_value was not generated yet");
1486 /* fill the parameter list */
1487 ec = &self->vtype->expression;
1488 for (i = 0; i < vec_size(ec->params); ++i)
1490 if (ec->params[i]->expression.vtype == TYPE_FIELD)
1491 vec_push(irf->params, ec->params[i]->expression.next->expression.vtype);
1493 vec_push(irf->params, ec->params[i]->expression.vtype);
1494 if (!self->builtin) {
1495 if (!ast_local_codegen(ec->params[i], self->ir_func, true))
1500 if (self->builtin) {
1501 irf->builtin = self->builtin;
1505 if (!vec_size(self->blocks)) {
1506 compile_error(ast_ctx(self), "function `%s` has no body", self->name);
1510 self->curblock = ir_function_create_block(ast_ctx(self), irf, "entry");
1511 if (!self->curblock) {
1512 compile_error(ast_ctx(self), "failed to allocate entry block for `%s`", self->name);
1516 for (i = 0; i < vec_size(self->blocks); ++i) {
1517 ast_expression_codegen *gen = self->blocks[i]->expression.codegen;
1518 if (!(*gen)((ast_expression*)self->blocks[i], self, false, &dummy))
1522 /* TODO: check return types */
1523 if (!self->curblock->final)
1525 if (!self->vtype->expression.next ||
1526 self->vtype->expression.next->expression.vtype == TYPE_VOID)
1528 return ir_block_create_return(self->curblock, ast_ctx(self), NULL);
1530 else if (vec_size(self->curblock->entries))
1532 /* error("missing return"); */
1533 if (compile_warning(ast_ctx(self), WARN_MISSING_RETURN_VALUES,
1534 "control reaches end of non-void function (`%s`) via %s",
1535 self->name, self->curblock->label))
1539 return ir_block_create_return(self->curblock, ast_ctx(self), NULL);
1545 /* Note, you will not see ast_block_codegen generate ir_blocks.
1546 * To the AST and the IR, blocks are 2 different things.
1547 * In the AST it represents a block of code, usually enclosed in
1548 * curly braces {...}.
1549 * While in the IR it represents a block in terms of control-flow.
1551 bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_value **out)
1555 /* We don't use this
1556 * Note: an ast-representation using the comma-operator
1557 * of the form: (a, b, c) = x should not assign to c...
1560 compile_error(ast_ctx(self), "not an l-value (code-block)");
1564 if (self->expression.outr) {
1565 *out = self->expression.outr;
1569 /* output is NULL at first, we'll have each expression
1570 * assign to out output, thus, a comma-operator represention
1571 * using an ast_block will return the last generated value,
1572 * so: (b, c) + a executed both b and c, and returns c,
1573 * which is then added to a.
1577 /* generate locals */
1578 for (i = 0; i < vec_size(self->locals); ++i)
1580 if (!ast_local_codegen(self->locals[i], func->ir_func, false)) {
1582 compile_error(ast_ctx(self), "failed to generate local `%s`", self->locals[i]->name);
1587 for (i = 0; i < vec_size(self->exprs); ++i)
1589 ast_expression_codegen *gen;
1590 if (func->curblock->final && !ast_istype(self->exprs[i], ast_label)) {
1591 if (compile_warning(ast_ctx(self->exprs[i]), WARN_UNREACHABLE_CODE, "unreachable statement"))
1595 gen = self->exprs[i]->expression.codegen;
1596 if (!(*gen)(self->exprs[i], func, false, out))
1600 self->expression.outr = *out;
1605 bool ast_store_codegen(ast_store *self, ast_function *func, bool lvalue, ir_value **out)
1607 ast_expression_codegen *cgen;
1608 ir_value *left = NULL;
1609 ir_value *right = NULL;
1613 ast_array_index *ai = NULL;
1615 if (lvalue && self->expression.outl) {
1616 *out = self->expression.outl;
1620 if (!lvalue && self->expression.outr) {
1621 *out = self->expression.outr;
1625 if (ast_istype(self->dest, ast_array_index))
1628 ai = (ast_array_index*)self->dest;
1629 idx = (ast_value*)ai->index;
1631 if (ast_istype(ai->index, ast_value) && idx->hasvalue && idx->cvq == CV_CONST)
1636 /* we need to call the setter */
1637 ir_value *iridx, *funval;
1641 compile_error(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1645 arr = (ast_value*)ai->array;
1646 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1647 compile_error(ast_ctx(self), "value has no setter (%s)", arr->name);
1651 cgen = idx->expression.codegen;
1652 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1655 cgen = arr->setter->expression.codegen;
1656 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
1659 cgen = self->source->expression.codegen;
1660 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1663 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "store"), funval, false);
1666 ir_call_param(call, iridx);
1667 ir_call_param(call, right);
1668 self->expression.outr = right;
1674 cgen = self->dest->expression.codegen;
1676 if (!(*cgen)((ast_expression*)(self->dest), func, true, &left))
1678 self->expression.outl = left;
1680 cgen = self->source->expression.codegen;
1682 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1685 if (!ir_block_create_store_op(func->curblock, ast_ctx(self), self->op, left, right))
1687 self->expression.outr = right;
1690 /* Theoretically, an assinment returns its left side as an
1691 * lvalue, if we don't need an lvalue though, we return
1692 * the right side as an rvalue, otherwise we have to
1693 * somehow know whether or not we need to dereference the pointer
1694 * on the left side - that is: OP_LOAD if it was an address.
1695 * Also: in original QC we cannot OP_LOADP *anyway*.
1697 *out = (lvalue ? left : right);
1702 bool ast_binary_codegen(ast_binary *self, ast_function *func, bool lvalue, ir_value **out)
1704 ast_expression_codegen *cgen;
1705 ir_value *left, *right;
1707 /* A binary operation cannot yield an l-value */
1709 compile_error(ast_ctx(self), "not an l-value (binop)");
1713 if (self->expression.outr) {
1714 *out = self->expression.outr;
1718 if ((OPTS_FLAG(SHORT_LOGIC) || OPTS_FLAG(PERL_LOGIC)) &&
1719 (self->op == INSTR_AND || self->op == INSTR_OR))
1721 /* short circuit evaluation */
1722 ir_block *other, *merge;
1723 ir_block *from_left, *from_right;
1727 /* prepare end-block */
1728 merge_id = vec_size(func->ir_func->blocks);
1729 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "sce_merge"));
1731 /* generate the left expression */
1732 cgen = self->left->expression.codegen;
1733 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1735 /* remember the block */
1736 from_left = func->curblock;
1738 /* create a new block for the right expression */
1739 other = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "sce_other"));
1740 if (self->op == INSTR_AND) {
1741 /* on AND: left==true -> other */
1742 if (!ir_block_create_if(func->curblock, ast_ctx(self), left, other, merge))
1745 /* on OR: left==false -> other */
1746 if (!ir_block_create_if(func->curblock, ast_ctx(self), left, merge, other))
1749 /* use the likely flag */
1750 vec_last(func->curblock->instr)->likely = true;
1752 /* enter the right-expression's block */
1753 func->curblock = other;
1755 cgen = self->right->expression.codegen;
1756 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1758 /* remember block */
1759 from_right = func->curblock;
1761 /* jump to the merge block */
1762 if (!ir_block_create_jump(func->curblock, ast_ctx(self), merge))
1765 vec_remove(func->ir_func->blocks, merge_id, 1);
1766 vec_push(func->ir_func->blocks, merge);
1768 func->curblock = merge;
1769 phi = ir_block_create_phi(func->curblock, ast_ctx(self),
1770 ast_function_label(func, "sce_value"),
1771 self->expression.vtype);
1772 ir_phi_add(phi, from_left, left);
1773 ir_phi_add(phi, from_right, right);
1774 *out = ir_phi_value(phi);
1778 if (!OPTS_FLAG(PERL_LOGIC)) {
1780 if (OPTS_FLAG(CORRECT_LOGIC) && (*out)->vtype == TYPE_VECTOR) {
1781 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
1782 ast_function_label(func, "sce_bool_v"),
1786 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
1787 ast_function_label(func, "sce_bool"),
1792 else if (OPTS_FLAG(FALSE_EMPTY_STRINGS) && (*out)->vtype == TYPE_STRING) {
1793 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
1794 ast_function_label(func, "sce_bool_s"),
1798 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
1799 ast_function_label(func, "sce_bool"),
1805 *out = ir_block_create_binop(func->curblock, ast_ctx(self),
1806 ast_function_label(func, "sce_bool"),
1807 INSTR_AND, *out, *out);
1813 self->expression.outr = *out;
1817 cgen = self->left->expression.codegen;
1818 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1821 cgen = self->right->expression.codegen;
1822 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1825 *out = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "bin"),
1826 self->op, left, right);
1829 self->expression.outr = *out;
1834 bool ast_binstore_codegen(ast_binstore *self, ast_function *func, bool lvalue, ir_value **out)
1836 ast_expression_codegen *cgen;
1837 ir_value *leftl = NULL, *leftr, *right, *bin;
1841 ast_array_index *ai = NULL;
1842 ir_value *iridx = NULL;
1844 if (lvalue && self->expression.outl) {
1845 *out = self->expression.outl;
1849 if (!lvalue && self->expression.outr) {
1850 *out = self->expression.outr;
1854 if (ast_istype(self->dest, ast_array_index))
1857 ai = (ast_array_index*)self->dest;
1858 idx = (ast_value*)ai->index;
1860 if (ast_istype(ai->index, ast_value) && idx->hasvalue && idx->cvq == CV_CONST)
1864 /* for a binstore we need both an lvalue and an rvalue for the left side */
1865 /* rvalue of destination! */
1867 cgen = idx->expression.codegen;
1868 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1871 cgen = self->dest->expression.codegen;
1872 if (!(*cgen)((ast_expression*)(self->dest), func, false, &leftr))
1875 /* source as rvalue only */
1876 cgen = self->source->expression.codegen;
1877 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1880 /* now the binary */
1881 bin = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "binst"),
1882 self->opbin, leftr, right);
1883 self->expression.outr = bin;
1887 /* we need to call the setter */
1892 compile_error(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1896 arr = (ast_value*)ai->array;
1897 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1898 compile_error(ast_ctx(self), "value has no setter (%s)", arr->name);
1902 cgen = arr->setter->expression.codegen;
1903 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
1906 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "store"), funval, false);
1909 ir_call_param(call, iridx);
1910 ir_call_param(call, bin);
1911 self->expression.outr = bin;
1913 /* now store them */
1914 cgen = self->dest->expression.codegen;
1915 /* lvalue of destination */
1916 if (!(*cgen)((ast_expression*)(self->dest), func, true, &leftl))
1918 self->expression.outl = leftl;
1920 if (!ir_block_create_store_op(func->curblock, ast_ctx(self), self->opstore, leftl, bin))
1922 self->expression.outr = bin;
1925 /* Theoretically, an assinment returns its left side as an
1926 * lvalue, if we don't need an lvalue though, we return
1927 * the right side as an rvalue, otherwise we have to
1928 * somehow know whether or not we need to dereference the pointer
1929 * on the left side - that is: OP_LOAD if it was an address.
1930 * Also: in original QC we cannot OP_LOADP *anyway*.
1932 *out = (lvalue ? leftl : bin);
1937 bool ast_unary_codegen(ast_unary *self, ast_function *func, bool lvalue, ir_value **out)
1939 ast_expression_codegen *cgen;
1942 /* An unary operation cannot yield an l-value */
1944 compile_error(ast_ctx(self), "not an l-value (binop)");
1948 if (self->expression.outr) {
1949 *out = self->expression.outr;
1953 cgen = self->operand->expression.codegen;
1955 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
1958 *out = ir_block_create_unary(func->curblock, ast_ctx(self), ast_function_label(func, "unary"),
1962 self->expression.outr = *out;
1967 bool ast_return_codegen(ast_return *self, ast_function *func, bool lvalue, ir_value **out)
1969 ast_expression_codegen *cgen;
1974 /* In the context of a return operation, we don't actually return
1978 compile_error(ast_ctx(self), "return-expression is not an l-value");
1982 if (self->expression.outr) {
1983 compile_error(ast_ctx(self), "internal error: ast_return cannot be reused, it bears no result!");
1986 self->expression.outr = (ir_value*)1;
1988 if (self->operand) {
1989 cgen = self->operand->expression.codegen;
1991 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
1994 if (!ir_block_create_return(func->curblock, ast_ctx(self), operand))
1997 if (!ir_block_create_return(func->curblock, ast_ctx(self), NULL))
2004 bool ast_entfield_codegen(ast_entfield *self, ast_function *func, bool lvalue, ir_value **out)
2006 ast_expression_codegen *cgen;
2007 ir_value *ent, *field;
2009 /* This function needs to take the 'lvalue' flag into account!
2010 * As lvalue we provide a field-pointer, as rvalue we provide the
2014 if (lvalue && self->expression.outl) {
2015 *out = self->expression.outl;
2019 if (!lvalue && self->expression.outr) {
2020 *out = self->expression.outr;
2024 cgen = self->entity->expression.codegen;
2025 if (!(*cgen)((ast_expression*)(self->entity), func, false, &ent))
2028 cgen = self->field->expression.codegen;
2029 if (!(*cgen)((ast_expression*)(self->field), func, false, &field))
2034 *out = ir_block_create_fieldaddress(func->curblock, ast_ctx(self), ast_function_label(func, "efa"),
2037 *out = ir_block_create_load_from_ent(func->curblock, ast_ctx(self), ast_function_label(func, "efv"),
2038 ent, field, self->expression.vtype);
2039 /* Done AFTER error checking:
2040 codegen_output_type(self, *out);
2044 compile_error(ast_ctx(self), "failed to create %s instruction (output type %s)",
2045 (lvalue ? "ADDRESS" : "FIELD"),
2046 type_name[self->expression.vtype]);
2050 codegen_output_type(self, *out);
2053 self->expression.outl = *out;
2055 self->expression.outr = *out;
2057 /* Hm that should be it... */
2061 bool ast_member_codegen(ast_member *self, ast_function *func, bool lvalue, ir_value **out)
2063 ast_expression_codegen *cgen;
2066 /* in QC this is always an lvalue */
2068 if (self->expression.outl) {
2069 *out = self->expression.outl;
2073 cgen = self->owner->expression.codegen;
2074 if (!(*cgen)((ast_expression*)(self->owner), func, true, &vec))
2077 if (vec->vtype != TYPE_VECTOR &&
2078 !(vec->vtype == TYPE_FIELD && self->owner->expression.next->expression.vtype == TYPE_VECTOR))
2083 *out = ir_value_vector_member(vec, self->field);
2084 self->expression.outl = *out;
2086 return (*out != NULL);
2089 bool ast_array_index_codegen(ast_array_index *self, ast_function *func, bool lvalue, ir_value **out)
2094 if (!lvalue && self->expression.outr) {
2095 *out = self->expression.outr;
2097 if (lvalue && self->expression.outl) {
2098 *out = self->expression.outl;
2101 if (!ast_istype(self->array, ast_value)) {
2102 compile_error(ast_ctx(self), "array indexing this way is not supported");
2103 /* note this would actually be pointer indexing because the left side is
2104 * not an actual array but (hopefully) an indexable expression.
2105 * Once we get integer arithmetic, and GADDRESS/GSTORE/GLOAD instruction
2106 * support this path will be filled.
2111 arr = (ast_value*)self->array;
2112 idx = (ast_value*)self->index;
2114 if (!ast_istype(self->index, ast_value) || !idx->hasvalue || idx->cvq != CV_CONST) {
2115 /* Time to use accessor functions */
2116 ast_expression_codegen *cgen;
2117 ir_value *iridx, *funval;
2121 compile_error(ast_ctx(self), "(.2) array indexing here needs a compile-time constant");
2126 compile_error(ast_ctx(self), "value has no getter, don't know how to index it");
2130 cgen = self->index->expression.codegen;
2131 if (!(*cgen)((ast_expression*)(self->index), func, false, &iridx))
2134 cgen = arr->getter->expression.codegen;
2135 if (!(*cgen)((ast_expression*)(arr->getter), func, true, &funval))
2138 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "fetch"), funval, false);
2141 ir_call_param(call, iridx);
2143 *out = ir_call_value(call);
2144 self->expression.outr = *out;
2148 if (idx->expression.vtype == TYPE_FLOAT) {
2149 unsigned int arridx = idx->constval.vfloat;
2150 if (arridx >= self->array->expression.count)
2152 compile_error(ast_ctx(self), "array index out of bounds: %i", arridx);
2155 *out = arr->ir_values[arridx];
2157 else if (idx->expression.vtype == TYPE_INTEGER) {
2158 unsigned int arridx = idx->constval.vint;
2159 if (arridx >= self->array->expression.count)
2161 compile_error(ast_ctx(self), "array index out of bounds: %i", arridx);
2164 *out = arr->ir_values[arridx];
2167 compile_error(ast_ctx(self), "array indexing here needs an integer constant");
2173 bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_value **out)
2175 ast_expression_codegen *cgen;
2183 ir_block *ontrue_endblock = NULL;
2184 ir_block *onfalse_endblock = NULL;
2185 ir_block *merge = NULL;
2187 /* We don't output any value, thus also don't care about r/lvalue */
2191 if (self->expression.outr) {
2192 compile_error(ast_ctx(self), "internal error: ast_ifthen cannot be reused, it bears no result!");
2195 self->expression.outr = (ir_value*)1;
2197 /* generate the condition */
2198 cgen = self->cond->expression.codegen;
2199 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2201 /* update the block which will get the jump - because short-logic or ternaries may have changed this */
2202 cond = func->curblock;
2206 if (self->on_true) {
2207 /* create on-true block */
2208 ontrue = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "ontrue"));
2212 /* enter the block */
2213 func->curblock = ontrue;
2216 cgen = self->on_true->expression.codegen;
2217 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &dummy))
2220 /* we now need to work from the current endpoint */
2221 ontrue_endblock = func->curblock;
2226 if (self->on_false) {
2227 /* create on-false block */
2228 onfalse = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "onfalse"));
2232 /* enter the block */
2233 func->curblock = onfalse;
2236 cgen = self->on_false->expression.codegen;
2237 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &dummy))
2240 /* we now need to work from the current endpoint */
2241 onfalse_endblock = func->curblock;
2245 /* Merge block were they all merge in to */
2246 if (!ontrue || !onfalse || !ontrue_endblock->final || !onfalse_endblock->final)
2248 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "endif"));
2251 /* add jumps ot the merge block */
2252 if (ontrue && !ontrue_endblock->final && !ir_block_create_jump(ontrue_endblock, ast_ctx(self), merge))
2254 if (onfalse && !onfalse_endblock->final && !ir_block_create_jump(onfalse_endblock, ast_ctx(self), merge))
2257 /* Now enter the merge block */
2258 func->curblock = merge;
2261 /* we create the if here, that way all blocks are ordered :)
2263 if (!ir_block_create_if(cond, ast_ctx(self), condval,
2264 (ontrue ? ontrue : merge),
2265 (onfalse ? onfalse : merge)))
2273 bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_value **out)
2275 ast_expression_codegen *cgen;
2278 ir_value *trueval, *falseval;
2281 ir_block *cond = func->curblock;
2282 ir_block *cond_out = NULL;
2283 ir_block *ontrue, *ontrue_out = NULL;
2284 ir_block *onfalse, *onfalse_out = NULL;
2287 /* Ternary can never create an lvalue... */
2291 /* In theory it shouldn't be possible to pass through a node twice, but
2292 * in case we add any kind of optimization pass for the AST itself, it
2293 * may still happen, thus we remember a created ir_value and simply return one
2294 * if it already exists.
2296 if (self->expression.outr) {
2297 *out = self->expression.outr;
2301 /* In the following, contraty to ast_ifthen, we assume both paths exist. */
2303 /* generate the condition */
2304 func->curblock = cond;
2305 cgen = self->cond->expression.codegen;
2306 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2308 cond_out = func->curblock;
2310 /* create on-true block */
2311 ontrue = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_T"));
2316 /* enter the block */
2317 func->curblock = ontrue;
2320 cgen = self->on_true->expression.codegen;
2321 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &trueval))
2324 ontrue_out = func->curblock;
2327 /* create on-false block */
2328 onfalse = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_F"));
2333 /* enter the block */
2334 func->curblock = onfalse;
2337 cgen = self->on_false->expression.codegen;
2338 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &falseval))
2341 onfalse_out = func->curblock;
2344 /* create merge block */
2345 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_out"));
2348 /* jump to merge block */
2349 if (!ir_block_create_jump(ontrue_out, ast_ctx(self), merge))
2351 if (!ir_block_create_jump(onfalse_out, ast_ctx(self), merge))
2354 /* create if instruction */
2355 if (!ir_block_create_if(cond_out, ast_ctx(self), condval, ontrue, onfalse))
2358 /* Now enter the merge block */
2359 func->curblock = merge;
2361 /* Here, now, we need a PHI node
2362 * but first some sanity checking...
2364 if (trueval->vtype != falseval->vtype) {
2365 /* error("ternary with different types on the two sides"); */
2370 phi = ir_block_create_phi(merge, ast_ctx(self), ast_function_label(func, "phi"), trueval->vtype);
2373 ir_phi_add(phi, ontrue_out, trueval);
2374 ir_phi_add(phi, onfalse_out, falseval);
2376 self->expression.outr = ir_phi_value(phi);
2377 *out = self->expression.outr;
2379 codegen_output_type(self, *out);
2384 bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value **out)
2386 ast_expression_codegen *cgen;
2388 ir_value *dummy = NULL;
2389 ir_value *precond = NULL;
2390 ir_value *postcond = NULL;
2392 /* Since we insert some jumps "late" so we have blocks
2393 * ordered "nicely", we need to keep track of the actual end-blocks
2394 * of expressions to add the jumps to.
2396 ir_block *bbody = NULL, *end_bbody = NULL;
2397 ir_block *bprecond = NULL, *end_bprecond = NULL;
2398 ir_block *bpostcond = NULL, *end_bpostcond = NULL;
2399 ir_block *bincrement = NULL, *end_bincrement = NULL;
2400 ir_block *bout = NULL, *bin = NULL;
2402 /* let's at least move the outgoing block to the end */
2405 /* 'break' and 'continue' need to be able to find the right blocks */
2406 ir_block *bcontinue = NULL;
2407 ir_block *bbreak = NULL;
2409 ir_block *old_bcontinue = NULL;
2410 ir_block *old_bbreak = NULL;
2412 ir_block *tmpblock = NULL;
2417 if (self->expression.outr) {
2418 compile_error(ast_ctx(self), "internal error: ast_loop cannot be reused, it bears no result!");
2421 self->expression.outr = (ir_value*)1;
2424 * Should we ever need some kind of block ordering, better make this function
2425 * move blocks around than write a block ordering algorithm later... after all
2426 * the ast and ir should work together, not against each other.
2429 /* initexpr doesn't get its own block, it's pointless, it could create more blocks
2430 * anyway if for example it contains a ternary.
2434 cgen = self->initexpr->expression.codegen;
2435 if (!(*cgen)((ast_expression*)(self->initexpr), func, false, &dummy))
2439 /* Store the block from which we enter this chaos */
2440 bin = func->curblock;
2442 /* The pre-loop condition needs its own block since we
2443 * need to be able to jump to the start of that expression.
2447 bprecond = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "pre_loop_cond"));
2451 /* the pre-loop-condition the least important place to 'continue' at */
2452 bcontinue = bprecond;
2455 func->curblock = bprecond;
2458 cgen = self->precond->expression.codegen;
2459 if (!(*cgen)((ast_expression*)(self->precond), func, false, &precond))
2462 end_bprecond = func->curblock;
2464 bprecond = end_bprecond = NULL;
2467 /* Now the next blocks won't be ordered nicely, but we need to
2468 * generate them this early for 'break' and 'continue'.
2470 if (self->increment) {
2471 bincrement = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "loop_increment"));
2474 bcontinue = bincrement; /* increment comes before the pre-loop-condition */
2476 bincrement = end_bincrement = NULL;
2479 if (self->postcond) {
2480 bpostcond = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "post_loop_cond"));
2483 bcontinue = bpostcond; /* postcond comes before the increment */
2485 bpostcond = end_bpostcond = NULL;
2488 bout_id = vec_size(func->ir_func->blocks);
2489 bout = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "after_loop"));
2494 /* The loop body... */
2495 /* if (self->body) */
2497 bbody = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "loop_body"));
2502 func->curblock = bbody;
2504 old_bbreak = func->breakblock;
2505 old_bcontinue = func->continueblock;
2506 func->breakblock = bbreak;
2507 func->continueblock = bcontinue;
2508 if (!func->continueblock)
2509 func->continueblock = bbody;
2513 cgen = self->body->expression.codegen;
2514 if (!(*cgen)((ast_expression*)(self->body), func, false, &dummy))
2518 end_bbody = func->curblock;
2519 func->breakblock = old_bbreak;
2520 func->continueblock = old_bcontinue;
2523 /* post-loop-condition */
2527 func->curblock = bpostcond;
2530 cgen = self->postcond->expression.codegen;
2531 if (!(*cgen)((ast_expression*)(self->postcond), func, false, &postcond))
2534 end_bpostcond = func->curblock;
2537 /* The incrementor */
2538 if (self->increment)
2541 func->curblock = bincrement;
2544 cgen = self->increment->expression.codegen;
2545 if (!(*cgen)((ast_expression*)(self->increment), func, false, &dummy))
2548 end_bincrement = func->curblock;
2551 /* In any case now, we continue from the outgoing block */
2552 func->curblock = bout;
2554 /* Now all blocks are in place */
2555 /* From 'bin' we jump to whatever comes first */
2556 if (bprecond) tmpblock = bprecond;
2557 else if (bbody) tmpblock = bbody;
2558 else if (bpostcond) tmpblock = bpostcond;
2559 else tmpblock = bout;
2560 if (!ir_block_create_jump(bin, ast_ctx(self), tmpblock))
2566 ir_block *ontrue, *onfalse;
2567 if (bbody) ontrue = bbody;
2568 else if (bincrement) ontrue = bincrement;
2569 else if (bpostcond) ontrue = bpostcond;
2570 else ontrue = bprecond;
2572 if (self->pre_not) {
2577 if (!ir_block_create_if(end_bprecond, ast_ctx(self), precond, ontrue, onfalse))
2584 if (bincrement) tmpblock = bincrement;
2585 else if (bpostcond) tmpblock = bpostcond;
2586 else if (bprecond) tmpblock = bprecond;
2587 else tmpblock = bbody;
2588 if (!end_bbody->final && !ir_block_create_jump(end_bbody, ast_ctx(self), tmpblock))
2592 /* from increment */
2595 if (bpostcond) tmpblock = bpostcond;
2596 else if (bprecond) tmpblock = bprecond;
2597 else if (bbody) tmpblock = bbody;
2598 else tmpblock = bout;
2599 if (!ir_block_create_jump(end_bincrement, ast_ctx(self), tmpblock))
2606 ir_block *ontrue, *onfalse;
2607 if (bprecond) ontrue = bprecond;
2608 else if (bbody) ontrue = bbody;
2609 else if (bincrement) ontrue = bincrement;
2610 else ontrue = bpostcond;
2612 if (self->post_not) {
2617 if (!ir_block_create_if(end_bpostcond, ast_ctx(self), postcond, ontrue, onfalse))
2621 /* Move 'bout' to the end */
2622 vec_remove(func->ir_func->blocks, bout_id, 1);
2623 vec_push(func->ir_func->blocks, bout);
2628 bool ast_breakcont_codegen(ast_breakcont *self, ast_function *func, bool lvalue, ir_value **out)
2635 compile_error(ast_ctx(self), "break/continue expression is not an l-value");
2639 if (self->expression.outr) {
2640 compile_error(ast_ctx(self), "internal error: ast_breakcont cannot be reused!");
2643 self->expression.outr = (ir_value*)1;
2645 if (self->is_continue)
2646 target = func->continueblock;
2648 target = func->breakblock;
2651 compile_error(ast_ctx(self), "%s is lacking a target block", (self->is_continue ? "continue" : "break"));
2655 if (!ir_block_create_jump(func->curblock, ast_ctx(self), target))
2660 bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_value **out)
2662 ast_expression_codegen *cgen;
2664 ast_switch_case *def_case = NULL;
2665 ir_block *def_bfall = NULL;
2666 ir_block *def_bfall_to = NULL;
2667 bool set_def_bfall_to = false;
2669 ir_value *dummy = NULL;
2670 ir_value *irop = NULL;
2671 ir_block *old_break = NULL;
2672 ir_block *bout = NULL;
2673 ir_block *bfall = NULL;
2681 compile_error(ast_ctx(self), "switch expression is not an l-value");
2685 if (self->expression.outr) {
2686 compile_error(ast_ctx(self), "internal error: ast_switch cannot be reused!");
2689 self->expression.outr = (ir_value*)1;
2694 cgen = self->operand->expression.codegen;
2695 if (!(*cgen)((ast_expression*)(self->operand), func, false, &irop))
2698 if (!vec_size(self->cases))
2701 cmpinstr = type_eq_instr[irop->vtype];
2702 if (cmpinstr >= AINSTR_END) {
2703 ast_type_to_string(self->operand, typestr, sizeof(typestr));
2704 compile_error(ast_ctx(self), "invalid type to perform a switch on: %s", typestr);
2708 bout_id = vec_size(func->ir_func->blocks);
2709 bout = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "after_switch"));
2713 /* setup the break block */
2714 old_break = func->breakblock;
2715 func->breakblock = bout;
2717 /* Now create all cases */
2718 for (c = 0; c < vec_size(self->cases); ++c) {
2719 ir_value *cond, *val;
2720 ir_block *bcase, *bnot;
2723 ast_switch_case *swcase = &self->cases[c];
2725 if (swcase->value) {
2726 /* A regular case */
2727 /* generate the condition operand */
2728 cgen = swcase->value->expression.codegen;
2729 if (!(*cgen)((ast_expression*)(swcase->value), func, false, &val))
2731 /* generate the condition */
2732 cond = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "switch_eq"), cmpinstr, irop, val);
2736 bcase = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "case"));
2737 bnot_id = vec_size(func->ir_func->blocks);
2738 bnot = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "not_case"));
2739 if (!bcase || !bnot)
2741 if (set_def_bfall_to) {
2742 set_def_bfall_to = false;
2743 def_bfall_to = bcase;
2745 if (!ir_block_create_if(func->curblock, ast_ctx(self), cond, bcase, bnot))
2748 /* Make the previous case-end fall through */
2749 if (bfall && !bfall->final) {
2750 if (!ir_block_create_jump(bfall, ast_ctx(self), bcase))
2754 /* enter the case */
2755 func->curblock = bcase;
2756 cgen = swcase->code->expression.codegen;
2757 if (!(*cgen)((ast_expression*)swcase->code, func, false, &dummy))
2760 /* remember this block to fall through from */
2761 bfall = func->curblock;
2763 /* enter the else and move it down */
2764 func->curblock = bnot;
2765 vec_remove(func->ir_func->blocks, bnot_id, 1);
2766 vec_push(func->ir_func->blocks, bnot);
2768 /* The default case */
2769 /* Remember where to fall through from: */
2772 /* remember which case it was */
2774 /* And the next case will be remembered */
2775 set_def_bfall_to = true;
2779 /* Jump from the last bnot to bout */
2780 if (bfall && !bfall->final && !ir_block_create_jump(bfall, ast_ctx(self), bout)) {
2782 astwarning(ast_ctx(bfall), WARN_???, "missing break after last case");
2787 /* If there was a default case, put it down here */
2791 /* No need to create an extra block */
2792 bcase = func->curblock;
2794 /* Insert the fallthrough jump */
2795 if (def_bfall && !def_bfall->final) {
2796 if (!ir_block_create_jump(def_bfall, ast_ctx(self), bcase))
2800 /* Now generate the default code */
2801 cgen = def_case->code->expression.codegen;
2802 if (!(*cgen)((ast_expression*)def_case->code, func, false, &dummy))
2805 /* see if we need to fall through */
2806 if (def_bfall_to && !func->curblock->final)
2808 if (!ir_block_create_jump(func->curblock, ast_ctx(self), def_bfall_to))
2813 /* Jump from the last bnot to bout */
2814 if (!func->curblock->final && !ir_block_create_jump(func->curblock, ast_ctx(self), bout))
2816 /* enter the outgoing block */
2817 func->curblock = bout;
2819 /* restore the break block */
2820 func->breakblock = old_break;
2822 /* Move 'bout' to the end, it's nicer */
2823 vec_remove(func->ir_func->blocks, bout_id, 1);
2824 vec_push(func->ir_func->blocks, bout);
2829 bool ast_label_codegen(ast_label *self, ast_function *func, bool lvalue, ir_value **out)
2836 compile_error(ast_ctx(self), "internal error: ast_label cannot be an lvalue");
2840 /* simply create a new block and jump to it */
2841 self->irblock = ir_function_create_block(ast_ctx(self), func->ir_func, self->name);
2842 if (!self->irblock) {
2843 compile_error(ast_ctx(self), "failed to allocate label block `%s`", self->name);
2846 if (!func->curblock->final) {
2847 if (!ir_block_create_jump(func->curblock, ast_ctx(self), self->irblock))
2851 /* enter the new block */
2852 func->curblock = self->irblock;
2854 /* Generate all the leftover gotos */
2855 for (i = 0; i < vec_size(self->gotos); ++i) {
2856 if (!ast_goto_codegen(self->gotos[i], func, false, &dummy))
2863 bool ast_goto_codegen(ast_goto *self, ast_function *func, bool lvalue, ir_value **out)
2867 compile_error(ast_ctx(self), "internal error: ast_goto cannot be an lvalue");
2871 if (self->target->irblock) {
2872 if (self->irblock_from) {
2873 /* we already tried once, this is the callback */
2874 self->irblock_from->final = false;
2875 if (!ir_block_create_goto(self->irblock_from, ast_ctx(self), self->target->irblock)) {
2876 compile_error(ast_ctx(self), "failed to generate goto to `%s`", self->name);
2882 if (!ir_block_create_goto(func->curblock, ast_ctx(self), self->target->irblock)) {
2883 compile_error(ast_ctx(self), "failed to generate goto to `%s`", self->name);
2890 /* the target has not yet been created...
2891 * close this block in a sneaky way:
2893 func->curblock->final = true;
2894 self->irblock_from = func->curblock;
2895 ast_label_register_goto(self->target, self);
2901 bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value **out)
2903 ast_expression_codegen *cgen;
2905 ir_instr *callinstr;
2908 ir_value *funval = NULL;
2910 /* return values are never lvalues */
2912 compile_error(ast_ctx(self), "not an l-value (function call)");
2916 if (self->expression.outr) {
2917 *out = self->expression.outr;
2921 cgen = self->func->expression.codegen;
2922 if (!(*cgen)((ast_expression*)(self->func), func, false, &funval))
2930 for (i = 0; i < vec_size(self->params); ++i)
2933 ast_expression *expr = self->params[i];
2935 cgen = expr->expression.codegen;
2936 if (!(*cgen)(expr, func, false, ¶m))
2940 vec_push(params, param);
2943 callinstr = ir_block_create_call(func->curblock, ast_ctx(self),
2944 ast_function_label(func, "call"),
2945 funval, !!(self->func->expression.flags & AST_FLAG_NORETURN));
2949 for (i = 0; i < vec_size(params); ++i) {
2950 ir_call_param(callinstr, params[i]);
2953 *out = ir_call_value(callinstr);
2954 self->expression.outr = *out;
2956 codegen_output_type(self, *out);