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, unsigned int levels)
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;
773 self->levels = levels;
778 void ast_breakcont_delete(ast_breakcont *self)
780 ast_expression_delete((ast_expression*)self);
784 ast_switch* ast_switch_new(lex_ctx ctx, ast_expression *op)
786 ast_instantiate(ast_switch, ctx, ast_switch_delete);
787 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_switch_codegen);
792 ast_propagate_effects(self, op);
797 void ast_switch_delete(ast_switch *self)
800 ast_unref(self->operand);
802 for (i = 0; i < vec_size(self->cases); ++i) {
803 if (self->cases[i].value)
804 ast_unref(self->cases[i].value);
805 ast_unref(self->cases[i].code);
807 vec_free(self->cases);
809 ast_expression_delete((ast_expression*)self);
813 ast_label* ast_label_new(lex_ctx ctx, const char *name)
815 ast_instantiate(ast_label, ctx, ast_label_delete);
816 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_label_codegen);
818 self->name = util_strdup(name);
819 self->irblock = NULL;
825 void ast_label_delete(ast_label *self)
827 mem_d((void*)self->name);
828 vec_free(self->gotos);
829 ast_expression_delete((ast_expression*)self);
833 void ast_label_register_goto(ast_label *self, ast_goto *g)
835 vec_push(self->gotos, g);
838 ast_goto* ast_goto_new(lex_ctx ctx, const char *name)
840 ast_instantiate(ast_goto, ctx, ast_goto_delete);
841 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_goto_codegen);
843 self->name = util_strdup(name);
845 self->irblock_from = NULL;
850 void ast_goto_delete(ast_goto *self)
852 mem_d((void*)self->name);
853 ast_expression_delete((ast_expression*)self);
857 void ast_goto_set_label(ast_goto *self, ast_label *label)
859 self->target = label;
862 ast_call* ast_call_new(lex_ctx ctx,
863 ast_expression *funcexpr)
865 ast_instantiate(ast_call, ctx, ast_call_delete);
866 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_call_codegen);
868 ast_side_effects(self) = true;
871 self->func = funcexpr;
873 ast_type_adopt(self, funcexpr->expression.next);
878 void ast_call_delete(ast_call *self)
881 for (i = 0; i < vec_size(self->params); ++i)
882 ast_unref(self->params[i]);
883 vec_free(self->params);
886 ast_unref(self->func);
888 ast_expression_delete((ast_expression*)self);
892 bool ast_call_check_types(ast_call *self)
896 const ast_expression *func = self->func;
897 size_t count = vec_size(self->params);
898 if (count > vec_size(func->expression.params))
899 count = vec_size(func->expression.params);
901 for (i = 0; i < count; ++i) {
902 if (!ast_compare_type(self->params[i], (ast_expression*)(func->expression.params[i]))) {
905 ast_type_to_string(self->params[i], tgot, sizeof(tgot));
906 ast_type_to_string((ast_expression*)func->expression.params[i], texp, sizeof(texp));
907 compile_error(ast_ctx(self), "invalid type for parameter %u in function call: expected %s, got %s",
908 (unsigned int)(i+1), texp, tgot);
909 /* we don't immediately return */
916 ast_store* ast_store_new(lex_ctx ctx, int op,
917 ast_expression *dest, ast_expression *source)
919 ast_instantiate(ast_store, ctx, ast_store_delete);
920 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_store_codegen);
922 ast_side_effects(self) = true;
926 self->source = source;
928 if (!ast_type_adopt(self, dest)) {
936 void ast_store_delete(ast_store *self)
938 ast_unref(self->dest);
939 ast_unref(self->source);
940 ast_expression_delete((ast_expression*)self);
944 ast_block* ast_block_new(lex_ctx ctx)
946 ast_instantiate(ast_block, ctx, ast_block_delete);
947 ast_expression_init((ast_expression*)self,
948 (ast_expression_codegen*)&ast_block_codegen);
952 self->collect = NULL;
957 bool ast_block_add_expr(ast_block *self, ast_expression *e)
959 ast_propagate_effects(self, e);
960 vec_push(self->exprs, e);
961 if (self->expression.next) {
962 ast_delete(self->expression.next);
963 self->expression.next = NULL;
965 if (!ast_type_adopt(self, e)) {
966 compile_error(ast_ctx(self), "internal error: failed to adopt type");
972 void ast_block_collect(ast_block *self, ast_expression *expr)
974 vec_push(self->collect, expr);
975 expr->expression.node.keep = true;
978 void ast_block_delete(ast_block *self)
981 for (i = 0; i < vec_size(self->exprs); ++i)
982 ast_unref(self->exprs[i]);
983 vec_free(self->exprs);
984 for (i = 0; i < vec_size(self->locals); ++i)
985 ast_delete(self->locals[i]);
986 vec_free(self->locals);
987 for (i = 0; i < vec_size(self->collect); ++i)
988 ast_delete(self->collect[i]);
989 vec_free(self->collect);
990 ast_expression_delete((ast_expression*)self);
994 bool ast_block_set_type(ast_block *self, ast_expression *from)
996 if (self->expression.next)
997 ast_delete(self->expression.next);
998 if (!ast_type_adopt(self, from))
1003 ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype)
1005 ast_instantiate(ast_function, ctx, ast_function_delete);
1009 vtype->expression.vtype != TYPE_FUNCTION)
1011 compile_error(ast_ctx(self), "internal error: ast_function_new condition %i %i type=%i (probably 2 bodies?)",
1013 (int)vtype->hasvalue,
1014 vtype->expression.vtype);
1019 self->vtype = vtype;
1020 self->name = name ? util_strdup(name) : NULL;
1021 self->blocks = NULL;
1023 self->labelcount = 0;
1026 self->ir_func = NULL;
1027 self->curblock = NULL;
1029 self->breakblocks = NULL;
1030 self->continueblocks = NULL;
1032 vtype->hasvalue = true;
1033 vtype->constval.vfunc = self;
1038 void ast_function_delete(ast_function *self)
1042 mem_d((void*)self->name);
1044 /* ast_value_delete(self->vtype); */
1045 self->vtype->hasvalue = false;
1046 self->vtype->constval.vfunc = NULL;
1047 /* We use unref - if it was stored in a global table it is supposed
1048 * to be deleted from *there*
1050 ast_unref(self->vtype);
1052 for (i = 0; i < vec_size(self->blocks); ++i)
1053 ast_delete(self->blocks[i]);
1054 vec_free(self->blocks);
1055 vec_free(self->breakblocks);
1056 vec_free(self->continueblocks);
1060 const char* ast_function_label(ast_function *self, const char *prefix)
1066 if (!opts.dump && !opts.dumpfin && !opts.debug)
1069 id = (self->labelcount++);
1070 len = strlen(prefix);
1072 from = self->labelbuf + sizeof(self->labelbuf)-1;
1075 *from-- = (id%10) + '0';
1079 memcpy(from - len, prefix, len);
1083 /*********************************************************************/
1085 * by convention you must never pass NULL to the 'ir_value **out'
1086 * parameter. If you really don't care about the output, pass a dummy.
1087 * But I can't imagine a pituation where the output is truly unnecessary.
1090 void _ast_codegen_output_type(ast_expression_common *self, ir_value *out)
1092 if (out->vtype == TYPE_FIELD)
1093 out->fieldtype = self->next->expression.vtype;
1094 if (out->vtype == TYPE_FUNCTION)
1095 out->outtype = self->next->expression.vtype;
1098 #define codegen_output_type(a,o) (_ast_codegen_output_type(&((a)->expression),(o)))
1100 bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out)
1104 /* NOTE: This is the codegen for a variable used in an expression.
1105 * It is not the codegen to generate the value. For this purpose,
1106 * ast_local_codegen and ast_global_codegen are to be used before this
1107 * is executed. ast_function_codegen should take care of its locals,
1108 * and the ast-user should take care of ast_global_codegen to be used
1109 * on all the globals.
1112 char tname[1024]; /* typename is reserved in C++ */
1113 ast_type_to_string((ast_expression*)self, tname, sizeof(tname));
1114 compile_error(ast_ctx(self), "ast_value used before generated %s %s", tname, self->name);
1121 bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
1125 if (self->hasvalue && self->expression.vtype == TYPE_FUNCTION)
1127 ir_function *func = ir_builder_create_function(ir, self->name, self->expression.next->expression.vtype);
1130 func->context = ast_ctx(self);
1131 func->value->context = ast_ctx(self);
1133 self->constval.vfunc->ir_func = func;
1134 self->ir_v = func->value;
1135 /* The function is filled later on ast_function_codegen... */
1139 if (isfield && self->expression.vtype == TYPE_FIELD) {
1140 ast_expression *fieldtype = self->expression.next;
1142 if (self->hasvalue) {
1143 compile_error(ast_ctx(self), "TODO: constant field pointers with value");
1147 if (fieldtype->expression.vtype == TYPE_ARRAY) {
1152 ast_expression_common *elemtype;
1154 ast_value *array = (ast_value*)fieldtype;
1156 if (!ast_istype(fieldtype, ast_value)) {
1157 compile_error(ast_ctx(self), "internal error: ast_value required");
1161 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1162 if (!array->expression.count || array->expression.count > opts.max_array_size)
1163 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)array->expression.count);
1165 elemtype = &array->expression.next->expression;
1166 vtype = elemtype->vtype;
1168 v = ir_builder_create_field(ir, self->name, vtype);
1170 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", self->name);
1173 v->context = ast_ctx(self);
1174 v->unique_life = true;
1176 array->ir_v = self->ir_v = v;
1178 namelen = strlen(self->name);
1179 name = (char*)mem_a(namelen + 16);
1180 strcpy(name, self->name);
1182 array->ir_values = (ir_value**)mem_a(sizeof(array->ir_values[0]) * array->expression.count);
1183 array->ir_values[0] = v;
1184 for (ai = 1; ai < array->expression.count; ++ai) {
1185 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1186 array->ir_values[ai] = ir_builder_create_field(ir, name, vtype);
1187 if (!array->ir_values[ai]) {
1189 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", name);
1192 array->ir_values[ai]->context = ast_ctx(self);
1193 array->ir_values[ai]->unique_life = true;
1194 array->ir_values[ai]->locked = true;
1200 v = ir_builder_create_field(ir, self->name, self->expression.next->expression.vtype);
1203 v->context = ast_ctx(self);
1209 if (self->expression.vtype == TYPE_ARRAY) {
1214 ast_expression_common *elemtype = &self->expression.next->expression;
1215 int vtype = elemtype->vtype;
1217 /* same as with field arrays */
1218 if (!self->expression.count || self->expression.count > opts.max_array_size)
1219 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1221 v = ir_builder_create_global(ir, self->name, vtype);
1223 compile_error(ast_ctx(self), "ir_builder_create_global failed `%s`", self->name);
1226 v->context = ast_ctx(self);
1227 v->unique_life = true;
1230 namelen = strlen(self->name);
1231 name = (char*)mem_a(namelen + 16);
1232 strcpy(name, self->name);
1234 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1235 self->ir_values[0] = v;
1236 for (ai = 1; ai < self->expression.count; ++ai) {
1237 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1238 self->ir_values[ai] = ir_builder_create_global(ir, name, vtype);
1239 if (!self->ir_values[ai]) {
1241 compile_error(ast_ctx(self), "ir_builder_create_global failed `%s`", name);
1244 self->ir_values[ai]->context = ast_ctx(self);
1245 self->ir_values[ai]->unique_life = true;
1246 self->ir_values[ai]->locked = true;
1252 /* Arrays don't do this since there's no "array" value which spans across the
1255 v = ir_builder_create_global(ir, self->name, self->expression.vtype);
1257 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", self->name);
1260 codegen_output_type(self, v);
1261 v->context = ast_ctx(self);
1264 if (self->hasvalue) {
1265 switch (self->expression.vtype)
1268 if (!ir_value_set_float(v, self->constval.vfloat))
1272 if (!ir_value_set_vector(v, self->constval.vvec))
1276 if (!ir_value_set_string(v, self->constval.vstring))
1280 compile_error(ast_ctx(self), "TODO: global constant array");
1283 compile_error(ast_ctx(self), "global of type function not properly generated");
1285 /* Cannot generate an IR value for a function,
1286 * need a pointer pointing to a function rather.
1289 if (!self->constval.vfield) {
1290 compile_error(ast_ctx(self), "field constant without vfield set");
1293 if (!self->constval.vfield->ir_v) {
1294 compile_error(ast_ctx(self), "field constant generated before its field");
1297 if (!ir_value_set_field(v, self->constval.vfield->ir_v))
1301 compile_error(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1306 /* link us to the ir_value */
1311 error: /* clean up */
1316 bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
1319 if (self->hasvalue && self->expression.vtype == TYPE_FUNCTION)
1321 /* Do we allow local functions? I think not...
1322 * this is NOT a function pointer atm.
1327 if (self->expression.vtype == TYPE_ARRAY) {
1332 ast_expression_common *elemtype = &self->expression.next->expression;
1333 int vtype = elemtype->vtype;
1335 func->flags |= IR_FLAG_HAS_ARRAYS;
1338 compile_error(ast_ctx(self), "array-parameters are not supported");
1342 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1343 if (!self->expression.count || self->expression.count > opts.max_array_size) {
1344 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1347 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1348 if (!self->ir_values) {
1349 compile_error(ast_ctx(self), "failed to allocate array values");
1353 v = ir_function_create_local(func, self->name, vtype, param);
1355 compile_error(ast_ctx(self), "ir_function_create_local failed");
1358 v->context = ast_ctx(self);
1359 v->unique_life = true;
1362 namelen = strlen(self->name);
1363 name = (char*)mem_a(namelen + 16);
1364 strcpy(name, self->name);
1366 self->ir_values[0] = v;
1367 for (ai = 1; ai < self->expression.count; ++ai) {
1368 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1369 self->ir_values[ai] = ir_function_create_local(func, name, vtype, param);
1370 if (!self->ir_values[ai]) {
1371 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", name);
1374 self->ir_values[ai]->context = ast_ctx(self);
1375 self->ir_values[ai]->unique_life = true;
1376 self->ir_values[ai]->locked = true;
1381 v = ir_function_create_local(func, self->name, self->expression.vtype, param);
1384 codegen_output_type(self, v);
1385 v->context = ast_ctx(self);
1388 /* A constant local... hmmm...
1389 * I suppose the IR will have to deal with this
1391 if (self->hasvalue) {
1392 switch (self->expression.vtype)
1395 if (!ir_value_set_float(v, self->constval.vfloat))
1399 if (!ir_value_set_vector(v, self->constval.vvec))
1403 if (!ir_value_set_string(v, self->constval.vstring))
1407 compile_error(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1412 /* link us to the ir_value */
1416 if (!ast_generate_accessors(self, func->owner))
1420 error: /* clean up */
1425 bool ast_generate_accessors(ast_value *self, ir_builder *ir)
1428 bool warn = OPTS_WARN(WARN_USED_UNINITIALIZED);
1429 if (!self->setter || !self->getter)
1431 for (i = 0; i < self->expression.count; ++i) {
1432 if (!self->ir_values) {
1433 compile_error(ast_ctx(self), "internal error: no array values generated for `%s`", self->name);
1436 if (!self->ir_values[i]) {
1437 compile_error(ast_ctx(self), "internal error: not all array values have been generated for `%s`", self->name);
1440 if (self->ir_values[i]->life) {
1441 compile_error(ast_ctx(self), "internal error: function containing `%s` already generated", self->name);
1446 opts_set(opts.warn, WARN_USED_UNINITIALIZED, false);
1448 if (!ast_global_codegen (self->setter, ir, false) ||
1449 !ast_function_codegen(self->setter->constval.vfunc, ir) ||
1450 !ir_function_finalize(self->setter->constval.vfunc->ir_func))
1452 compile_error(ast_ctx(self), "internal error: failed to generate setter for `%s`", self->name);
1453 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1458 if (!ast_global_codegen (self->getter, ir, false) ||
1459 !ast_function_codegen(self->getter->constval.vfunc, ir) ||
1460 !ir_function_finalize(self->getter->constval.vfunc->ir_func))
1462 compile_error(ast_ctx(self), "internal error: failed to generate getter for `%s`", self->name);
1463 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1467 for (i = 0; i < self->expression.count; ++i) {
1468 vec_free(self->ir_values[i]->life);
1470 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1474 bool ast_function_codegen(ast_function *self, ir_builder *ir)
1478 ast_expression_common *ec;
1483 irf = self->ir_func;
1485 compile_error(ast_ctx(self), "ast_function's related ast_value was not generated yet");
1489 /* fill the parameter list */
1490 ec = &self->vtype->expression;
1491 for (i = 0; i < vec_size(ec->params); ++i)
1493 if (ec->params[i]->expression.vtype == TYPE_FIELD)
1494 vec_push(irf->params, ec->params[i]->expression.next->expression.vtype);
1496 vec_push(irf->params, ec->params[i]->expression.vtype);
1497 if (!self->builtin) {
1498 if (!ast_local_codegen(ec->params[i], self->ir_func, true))
1503 if (self->builtin) {
1504 irf->builtin = self->builtin;
1508 if (!vec_size(self->blocks)) {
1509 compile_error(ast_ctx(self), "function `%s` has no body", self->name);
1513 self->curblock = ir_function_create_block(ast_ctx(self), irf, "entry");
1514 if (!self->curblock) {
1515 compile_error(ast_ctx(self), "failed to allocate entry block for `%s`", self->name);
1519 for (i = 0; i < vec_size(self->blocks); ++i) {
1520 ast_expression_codegen *gen = self->blocks[i]->expression.codegen;
1521 if (!(*gen)((ast_expression*)self->blocks[i], self, false, &dummy))
1525 /* TODO: check return types */
1526 if (!self->curblock->final)
1528 if (!self->vtype->expression.next ||
1529 self->vtype->expression.next->expression.vtype == TYPE_VOID)
1531 return ir_block_create_return(self->curblock, ast_ctx(self), NULL);
1533 else if (vec_size(self->curblock->entries))
1535 /* error("missing return"); */
1536 if (compile_warning(ast_ctx(self), WARN_MISSING_RETURN_VALUES,
1537 "control reaches end of non-void function (`%s`) via %s",
1538 self->name, self->curblock->label))
1542 return ir_block_create_return(self->curblock, ast_ctx(self), NULL);
1548 /* Note, you will not see ast_block_codegen generate ir_blocks.
1549 * To the AST and the IR, blocks are 2 different things.
1550 * In the AST it represents a block of code, usually enclosed in
1551 * curly braces {...}.
1552 * While in the IR it represents a block in terms of control-flow.
1554 bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_value **out)
1558 /* We don't use this
1559 * Note: an ast-representation using the comma-operator
1560 * of the form: (a, b, c) = x should not assign to c...
1563 compile_error(ast_ctx(self), "not an l-value (code-block)");
1567 if (self->expression.outr) {
1568 *out = self->expression.outr;
1572 /* output is NULL at first, we'll have each expression
1573 * assign to out output, thus, a comma-operator represention
1574 * using an ast_block will return the last generated value,
1575 * so: (b, c) + a executed both b and c, and returns c,
1576 * which is then added to a.
1580 /* generate locals */
1581 for (i = 0; i < vec_size(self->locals); ++i)
1583 if (!ast_local_codegen(self->locals[i], func->ir_func, false)) {
1585 compile_error(ast_ctx(self), "failed to generate local `%s`", self->locals[i]->name);
1590 for (i = 0; i < vec_size(self->exprs); ++i)
1592 ast_expression_codegen *gen;
1593 if (func->curblock->final && !ast_istype(self->exprs[i], ast_label)) {
1594 if (compile_warning(ast_ctx(self->exprs[i]), WARN_UNREACHABLE_CODE, "unreachable statement"))
1598 gen = self->exprs[i]->expression.codegen;
1599 if (!(*gen)(self->exprs[i], func, false, out))
1603 self->expression.outr = *out;
1608 bool ast_store_codegen(ast_store *self, ast_function *func, bool lvalue, ir_value **out)
1610 ast_expression_codegen *cgen;
1611 ir_value *left = NULL;
1612 ir_value *right = NULL;
1616 ast_array_index *ai = NULL;
1618 if (lvalue && self->expression.outl) {
1619 *out = self->expression.outl;
1623 if (!lvalue && self->expression.outr) {
1624 *out = self->expression.outr;
1628 if (ast_istype(self->dest, ast_array_index))
1631 ai = (ast_array_index*)self->dest;
1632 idx = (ast_value*)ai->index;
1634 if (ast_istype(ai->index, ast_value) && idx->hasvalue && idx->cvq == CV_CONST)
1639 /* we need to call the setter */
1640 ir_value *iridx, *funval;
1644 compile_error(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1648 arr = (ast_value*)ai->array;
1649 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1650 compile_error(ast_ctx(self), "value has no setter (%s)", arr->name);
1654 cgen = idx->expression.codegen;
1655 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1658 cgen = arr->setter->expression.codegen;
1659 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
1662 cgen = self->source->expression.codegen;
1663 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1666 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "store"), funval, false);
1669 ir_call_param(call, iridx);
1670 ir_call_param(call, right);
1671 self->expression.outr = right;
1677 cgen = self->dest->expression.codegen;
1679 if (!(*cgen)((ast_expression*)(self->dest), func, true, &left))
1681 self->expression.outl = left;
1683 cgen = self->source->expression.codegen;
1685 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1688 if (!ir_block_create_store_op(func->curblock, ast_ctx(self), self->op, left, right))
1690 self->expression.outr = right;
1693 /* Theoretically, an assinment returns its left side as an
1694 * lvalue, if we don't need an lvalue though, we return
1695 * the right side as an rvalue, otherwise we have to
1696 * somehow know whether or not we need to dereference the pointer
1697 * on the left side - that is: OP_LOAD if it was an address.
1698 * Also: in original QC we cannot OP_LOADP *anyway*.
1700 *out = (lvalue ? left : right);
1705 bool ast_binary_codegen(ast_binary *self, ast_function *func, bool lvalue, ir_value **out)
1707 ast_expression_codegen *cgen;
1708 ir_value *left, *right;
1710 /* A binary operation cannot yield an l-value */
1712 compile_error(ast_ctx(self), "not an l-value (binop)");
1716 if (self->expression.outr) {
1717 *out = self->expression.outr;
1721 if ((OPTS_FLAG(SHORT_LOGIC) || OPTS_FLAG(PERL_LOGIC)) &&
1722 (self->op == INSTR_AND || self->op == INSTR_OR))
1724 /* short circuit evaluation */
1725 ir_block *other, *merge;
1726 ir_block *from_left, *from_right;
1730 /* prepare end-block */
1731 merge_id = vec_size(func->ir_func->blocks);
1732 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "sce_merge"));
1734 /* generate the left expression */
1735 cgen = self->left->expression.codegen;
1736 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1738 /* remember the block */
1739 from_left = func->curblock;
1741 /* create a new block for the right expression */
1742 other = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "sce_other"));
1743 if (self->op == INSTR_AND) {
1744 /* on AND: left==true -> other */
1745 if (!ir_block_create_if(func->curblock, ast_ctx(self), left, other, merge))
1748 /* on OR: left==false -> other */
1749 if (!ir_block_create_if(func->curblock, ast_ctx(self), left, merge, other))
1752 /* use the likely flag */
1753 vec_last(func->curblock->instr)->likely = true;
1755 /* enter the right-expression's block */
1756 func->curblock = other;
1758 cgen = self->right->expression.codegen;
1759 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1761 /* remember block */
1762 from_right = func->curblock;
1764 /* jump to the merge block */
1765 if (!ir_block_create_jump(func->curblock, ast_ctx(self), merge))
1768 vec_remove(func->ir_func->blocks, merge_id, 1);
1769 vec_push(func->ir_func->blocks, merge);
1771 func->curblock = merge;
1772 phi = ir_block_create_phi(func->curblock, ast_ctx(self),
1773 ast_function_label(func, "sce_value"),
1774 self->expression.vtype);
1775 ir_phi_add(phi, from_left, left);
1776 ir_phi_add(phi, from_right, right);
1777 *out = ir_phi_value(phi);
1781 if (!OPTS_FLAG(PERL_LOGIC)) {
1783 if (OPTS_FLAG(CORRECT_LOGIC) && (*out)->vtype == TYPE_VECTOR) {
1784 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
1785 ast_function_label(func, "sce_bool_v"),
1789 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
1790 ast_function_label(func, "sce_bool"),
1795 else if (OPTS_FLAG(FALSE_EMPTY_STRINGS) && (*out)->vtype == TYPE_STRING) {
1796 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
1797 ast_function_label(func, "sce_bool_s"),
1801 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
1802 ast_function_label(func, "sce_bool"),
1808 *out = ir_block_create_binop(func->curblock, ast_ctx(self),
1809 ast_function_label(func, "sce_bool"),
1810 INSTR_AND, *out, *out);
1816 self->expression.outr = *out;
1820 cgen = self->left->expression.codegen;
1821 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1824 cgen = self->right->expression.codegen;
1825 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1828 *out = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "bin"),
1829 self->op, left, right);
1832 self->expression.outr = *out;
1837 bool ast_binstore_codegen(ast_binstore *self, ast_function *func, bool lvalue, ir_value **out)
1839 ast_expression_codegen *cgen;
1840 ir_value *leftl = NULL, *leftr, *right, *bin;
1844 ast_array_index *ai = NULL;
1845 ir_value *iridx = NULL;
1847 if (lvalue && self->expression.outl) {
1848 *out = self->expression.outl;
1852 if (!lvalue && self->expression.outr) {
1853 *out = self->expression.outr;
1857 if (ast_istype(self->dest, ast_array_index))
1860 ai = (ast_array_index*)self->dest;
1861 idx = (ast_value*)ai->index;
1863 if (ast_istype(ai->index, ast_value) && idx->hasvalue && idx->cvq == CV_CONST)
1867 /* for a binstore we need both an lvalue and an rvalue for the left side */
1868 /* rvalue of destination! */
1870 cgen = idx->expression.codegen;
1871 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1874 cgen = self->dest->expression.codegen;
1875 if (!(*cgen)((ast_expression*)(self->dest), func, false, &leftr))
1878 /* source as rvalue only */
1879 cgen = self->source->expression.codegen;
1880 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1883 /* now the binary */
1884 bin = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "binst"),
1885 self->opbin, leftr, right);
1886 self->expression.outr = bin;
1890 /* we need to call the setter */
1895 compile_error(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1899 arr = (ast_value*)ai->array;
1900 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1901 compile_error(ast_ctx(self), "value has no setter (%s)", arr->name);
1905 cgen = arr->setter->expression.codegen;
1906 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
1909 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "store"), funval, false);
1912 ir_call_param(call, iridx);
1913 ir_call_param(call, bin);
1914 self->expression.outr = bin;
1916 /* now store them */
1917 cgen = self->dest->expression.codegen;
1918 /* lvalue of destination */
1919 if (!(*cgen)((ast_expression*)(self->dest), func, true, &leftl))
1921 self->expression.outl = leftl;
1923 if (!ir_block_create_store_op(func->curblock, ast_ctx(self), self->opstore, leftl, bin))
1925 self->expression.outr = bin;
1928 /* Theoretically, an assinment returns its left side as an
1929 * lvalue, if we don't need an lvalue though, we return
1930 * the right side as an rvalue, otherwise we have to
1931 * somehow know whether or not we need to dereference the pointer
1932 * on the left side - that is: OP_LOAD if it was an address.
1933 * Also: in original QC we cannot OP_LOADP *anyway*.
1935 *out = (lvalue ? leftl : bin);
1940 bool ast_unary_codegen(ast_unary *self, ast_function *func, bool lvalue, ir_value **out)
1942 ast_expression_codegen *cgen;
1945 /* An unary operation cannot yield an l-value */
1947 compile_error(ast_ctx(self), "not an l-value (binop)");
1951 if (self->expression.outr) {
1952 *out = self->expression.outr;
1956 cgen = self->operand->expression.codegen;
1958 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
1961 *out = ir_block_create_unary(func->curblock, ast_ctx(self), ast_function_label(func, "unary"),
1965 self->expression.outr = *out;
1970 bool ast_return_codegen(ast_return *self, ast_function *func, bool lvalue, ir_value **out)
1972 ast_expression_codegen *cgen;
1977 /* In the context of a return operation, we don't actually return
1981 compile_error(ast_ctx(self), "return-expression is not an l-value");
1985 if (self->expression.outr) {
1986 compile_error(ast_ctx(self), "internal error: ast_return cannot be reused, it bears no result!");
1989 self->expression.outr = (ir_value*)1;
1991 if (self->operand) {
1992 cgen = self->operand->expression.codegen;
1994 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
1997 if (!ir_block_create_return(func->curblock, ast_ctx(self), operand))
2000 if (!ir_block_create_return(func->curblock, ast_ctx(self), NULL))
2007 bool ast_entfield_codegen(ast_entfield *self, ast_function *func, bool lvalue, ir_value **out)
2009 ast_expression_codegen *cgen;
2010 ir_value *ent, *field;
2012 /* This function needs to take the 'lvalue' flag into account!
2013 * As lvalue we provide a field-pointer, as rvalue we provide the
2017 if (lvalue && self->expression.outl) {
2018 *out = self->expression.outl;
2022 if (!lvalue && self->expression.outr) {
2023 *out = self->expression.outr;
2027 cgen = self->entity->expression.codegen;
2028 if (!(*cgen)((ast_expression*)(self->entity), func, false, &ent))
2031 cgen = self->field->expression.codegen;
2032 if (!(*cgen)((ast_expression*)(self->field), func, false, &field))
2037 *out = ir_block_create_fieldaddress(func->curblock, ast_ctx(self), ast_function_label(func, "efa"),
2040 *out = ir_block_create_load_from_ent(func->curblock, ast_ctx(self), ast_function_label(func, "efv"),
2041 ent, field, self->expression.vtype);
2042 /* Done AFTER error checking:
2043 codegen_output_type(self, *out);
2047 compile_error(ast_ctx(self), "failed to create %s instruction (output type %s)",
2048 (lvalue ? "ADDRESS" : "FIELD"),
2049 type_name[self->expression.vtype]);
2053 codegen_output_type(self, *out);
2056 self->expression.outl = *out;
2058 self->expression.outr = *out;
2060 /* Hm that should be it... */
2064 bool ast_member_codegen(ast_member *self, ast_function *func, bool lvalue, ir_value **out)
2066 ast_expression_codegen *cgen;
2069 /* in QC this is always an lvalue */
2071 if (self->expression.outl) {
2072 *out = self->expression.outl;
2076 cgen = self->owner->expression.codegen;
2077 if (!(*cgen)((ast_expression*)(self->owner), func, true, &vec))
2080 if (vec->vtype != TYPE_VECTOR &&
2081 !(vec->vtype == TYPE_FIELD && self->owner->expression.next->expression.vtype == TYPE_VECTOR))
2086 *out = ir_value_vector_member(vec, self->field);
2087 self->expression.outl = *out;
2089 return (*out != NULL);
2092 bool ast_array_index_codegen(ast_array_index *self, ast_function *func, bool lvalue, ir_value **out)
2097 if (!lvalue && self->expression.outr) {
2098 *out = self->expression.outr;
2100 if (lvalue && self->expression.outl) {
2101 *out = self->expression.outl;
2104 if (!ast_istype(self->array, ast_value)) {
2105 compile_error(ast_ctx(self), "array indexing this way is not supported");
2106 /* note this would actually be pointer indexing because the left side is
2107 * not an actual array but (hopefully) an indexable expression.
2108 * Once we get integer arithmetic, and GADDRESS/GSTORE/GLOAD instruction
2109 * support this path will be filled.
2114 arr = (ast_value*)self->array;
2115 idx = (ast_value*)self->index;
2117 if (!ast_istype(self->index, ast_value) || !idx->hasvalue || idx->cvq != CV_CONST) {
2118 /* Time to use accessor functions */
2119 ast_expression_codegen *cgen;
2120 ir_value *iridx, *funval;
2124 compile_error(ast_ctx(self), "(.2) array indexing here needs a compile-time constant");
2129 compile_error(ast_ctx(self), "value has no getter, don't know how to index it");
2133 cgen = self->index->expression.codegen;
2134 if (!(*cgen)((ast_expression*)(self->index), func, false, &iridx))
2137 cgen = arr->getter->expression.codegen;
2138 if (!(*cgen)((ast_expression*)(arr->getter), func, true, &funval))
2141 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "fetch"), funval, false);
2144 ir_call_param(call, iridx);
2146 *out = ir_call_value(call);
2147 self->expression.outr = *out;
2151 if (idx->expression.vtype == TYPE_FLOAT) {
2152 unsigned int arridx = idx->constval.vfloat;
2153 if (arridx >= self->array->expression.count)
2155 compile_error(ast_ctx(self), "array index out of bounds: %i", arridx);
2158 *out = arr->ir_values[arridx];
2160 else if (idx->expression.vtype == TYPE_INTEGER) {
2161 unsigned int arridx = idx->constval.vint;
2162 if (arridx >= self->array->expression.count)
2164 compile_error(ast_ctx(self), "array index out of bounds: %i", arridx);
2167 *out = arr->ir_values[arridx];
2170 compile_error(ast_ctx(self), "array indexing here needs an integer constant");
2176 bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_value **out)
2178 ast_expression_codegen *cgen;
2186 ir_block *ontrue_endblock = NULL;
2187 ir_block *onfalse_endblock = NULL;
2188 ir_block *merge = NULL;
2190 /* We don't output any value, thus also don't care about r/lvalue */
2194 if (self->expression.outr) {
2195 compile_error(ast_ctx(self), "internal error: ast_ifthen cannot be reused, it bears no result!");
2198 self->expression.outr = (ir_value*)1;
2200 /* generate the condition */
2201 cgen = self->cond->expression.codegen;
2202 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2204 /* update the block which will get the jump - because short-logic or ternaries may have changed this */
2205 cond = func->curblock;
2209 if (self->on_true) {
2210 /* create on-true block */
2211 ontrue = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "ontrue"));
2215 /* enter the block */
2216 func->curblock = ontrue;
2219 cgen = self->on_true->expression.codegen;
2220 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &dummy))
2223 /* we now need to work from the current endpoint */
2224 ontrue_endblock = func->curblock;
2229 if (self->on_false) {
2230 /* create on-false block */
2231 onfalse = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "onfalse"));
2235 /* enter the block */
2236 func->curblock = onfalse;
2239 cgen = self->on_false->expression.codegen;
2240 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &dummy))
2243 /* we now need to work from the current endpoint */
2244 onfalse_endblock = func->curblock;
2248 /* Merge block were they all merge in to */
2249 if (!ontrue || !onfalse || !ontrue_endblock->final || !onfalse_endblock->final)
2251 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "endif"));
2254 /* add jumps ot the merge block */
2255 if (ontrue && !ontrue_endblock->final && !ir_block_create_jump(ontrue_endblock, ast_ctx(self), merge))
2257 if (onfalse && !onfalse_endblock->final && !ir_block_create_jump(onfalse_endblock, ast_ctx(self), merge))
2260 /* Now enter the merge block */
2261 func->curblock = merge;
2264 /* we create the if here, that way all blocks are ordered :)
2266 if (!ir_block_create_if(cond, ast_ctx(self), condval,
2267 (ontrue ? ontrue : merge),
2268 (onfalse ? onfalse : merge)))
2276 bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_value **out)
2278 ast_expression_codegen *cgen;
2281 ir_value *trueval, *falseval;
2284 ir_block *cond = func->curblock;
2285 ir_block *cond_out = NULL;
2286 ir_block *ontrue, *ontrue_out = NULL;
2287 ir_block *onfalse, *onfalse_out = NULL;
2290 /* Ternary can never create an lvalue... */
2294 /* In theory it shouldn't be possible to pass through a node twice, but
2295 * in case we add any kind of optimization pass for the AST itself, it
2296 * may still happen, thus we remember a created ir_value and simply return one
2297 * if it already exists.
2299 if (self->expression.outr) {
2300 *out = self->expression.outr;
2304 /* In the following, contraty to ast_ifthen, we assume both paths exist. */
2306 /* generate the condition */
2307 func->curblock = cond;
2308 cgen = self->cond->expression.codegen;
2309 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2311 cond_out = func->curblock;
2313 /* create on-true block */
2314 ontrue = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_T"));
2319 /* enter the block */
2320 func->curblock = ontrue;
2323 cgen = self->on_true->expression.codegen;
2324 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &trueval))
2327 ontrue_out = func->curblock;
2330 /* create on-false block */
2331 onfalse = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_F"));
2336 /* enter the block */
2337 func->curblock = onfalse;
2340 cgen = self->on_false->expression.codegen;
2341 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &falseval))
2344 onfalse_out = func->curblock;
2347 /* create merge block */
2348 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_out"));
2351 /* jump to merge block */
2352 if (!ir_block_create_jump(ontrue_out, ast_ctx(self), merge))
2354 if (!ir_block_create_jump(onfalse_out, ast_ctx(self), merge))
2357 /* create if instruction */
2358 if (!ir_block_create_if(cond_out, ast_ctx(self), condval, ontrue, onfalse))
2361 /* Now enter the merge block */
2362 func->curblock = merge;
2364 /* Here, now, we need a PHI node
2365 * but first some sanity checking...
2367 if (trueval->vtype != falseval->vtype) {
2368 /* error("ternary with different types on the two sides"); */
2373 phi = ir_block_create_phi(merge, ast_ctx(self), ast_function_label(func, "phi"), trueval->vtype);
2376 ir_phi_add(phi, ontrue_out, trueval);
2377 ir_phi_add(phi, onfalse_out, falseval);
2379 self->expression.outr = ir_phi_value(phi);
2380 *out = self->expression.outr;
2382 codegen_output_type(self, *out);
2387 bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value **out)
2389 ast_expression_codegen *cgen;
2391 ir_value *dummy = NULL;
2392 ir_value *precond = NULL;
2393 ir_value *postcond = NULL;
2395 /* Since we insert some jumps "late" so we have blocks
2396 * ordered "nicely", we need to keep track of the actual end-blocks
2397 * of expressions to add the jumps to.
2399 ir_block *bbody = NULL, *end_bbody = NULL;
2400 ir_block *bprecond = NULL, *end_bprecond = NULL;
2401 ir_block *bpostcond = NULL, *end_bpostcond = NULL;
2402 ir_block *bincrement = NULL, *end_bincrement = NULL;
2403 ir_block *bout = NULL, *bin = NULL;
2405 /* let's at least move the outgoing block to the end */
2408 /* 'break' and 'continue' need to be able to find the right blocks */
2409 ir_block *bcontinue = NULL;
2410 ir_block *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 vec_push(func->breakblocks, bbreak);
2506 vec_push(func->continueblocks, bcontinue);
2508 vec_push(func->continueblocks, bbody);
2512 cgen = self->body->expression.codegen;
2513 if (!(*cgen)((ast_expression*)(self->body), func, false, &dummy))
2517 end_bbody = func->curblock;
2518 vec_pop(func->breakblocks);
2519 vec_pop(func->continueblocks);
2522 /* post-loop-condition */
2526 func->curblock = bpostcond;
2529 cgen = self->postcond->expression.codegen;
2530 if (!(*cgen)((ast_expression*)(self->postcond), func, false, &postcond))
2533 end_bpostcond = func->curblock;
2536 /* The incrementor */
2537 if (self->increment)
2540 func->curblock = bincrement;
2543 cgen = self->increment->expression.codegen;
2544 if (!(*cgen)((ast_expression*)(self->increment), func, false, &dummy))
2547 end_bincrement = func->curblock;
2550 /* In any case now, we continue from the outgoing block */
2551 func->curblock = bout;
2553 /* Now all blocks are in place */
2554 /* From 'bin' we jump to whatever comes first */
2555 if (bprecond) tmpblock = bprecond;
2556 else if (bbody) tmpblock = bbody;
2557 else if (bpostcond) tmpblock = bpostcond;
2558 else tmpblock = bout;
2559 if (!ir_block_create_jump(bin, ast_ctx(self), tmpblock))
2565 ir_block *ontrue, *onfalse;
2566 if (bbody) ontrue = bbody;
2567 else if (bincrement) ontrue = bincrement;
2568 else if (bpostcond) ontrue = bpostcond;
2569 else ontrue = bprecond;
2571 if (self->pre_not) {
2576 if (!ir_block_create_if(end_bprecond, ast_ctx(self), precond, ontrue, onfalse))
2583 if (bincrement) tmpblock = bincrement;
2584 else if (bpostcond) tmpblock = bpostcond;
2585 else if (bprecond) tmpblock = bprecond;
2586 else tmpblock = bbody;
2587 if (!end_bbody->final && !ir_block_create_jump(end_bbody, ast_ctx(self), tmpblock))
2591 /* from increment */
2594 if (bpostcond) tmpblock = bpostcond;
2595 else if (bprecond) tmpblock = bprecond;
2596 else if (bbody) tmpblock = bbody;
2597 else tmpblock = bout;
2598 if (!ir_block_create_jump(end_bincrement, ast_ctx(self), tmpblock))
2605 ir_block *ontrue, *onfalse;
2606 if (bprecond) ontrue = bprecond;
2607 else if (bbody) ontrue = bbody;
2608 else if (bincrement) ontrue = bincrement;
2609 else ontrue = bpostcond;
2611 if (self->post_not) {
2616 if (!ir_block_create_if(end_bpostcond, ast_ctx(self), postcond, ontrue, onfalse))
2620 /* Move 'bout' to the end */
2621 vec_remove(func->ir_func->blocks, bout_id, 1);
2622 vec_push(func->ir_func->blocks, bout);
2627 bool ast_breakcont_codegen(ast_breakcont *self, ast_function *func, bool lvalue, ir_value **out)
2634 compile_error(ast_ctx(self), "break/continue expression is not an l-value");
2638 if (self->expression.outr) {
2639 compile_error(ast_ctx(self), "internal error: ast_breakcont cannot be reused!");
2642 self->expression.outr = (ir_value*)1;
2644 if (self->is_continue)
2645 target = func->continueblocks[vec_size(func->continueblocks)-1-self->levels];
2647 target = func->breakblocks[vec_size(func->breakblocks)-1-self->levels];
2650 compile_error(ast_ctx(self), "%s is lacking a target block", (self->is_continue ? "continue" : "break"));
2654 if (!ir_block_create_jump(func->curblock, ast_ctx(self), target))
2659 bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_value **out)
2661 ast_expression_codegen *cgen;
2663 ast_switch_case *def_case = NULL;
2664 ir_block *def_bfall = NULL;
2665 ir_block *def_bfall_to = NULL;
2666 bool set_def_bfall_to = false;
2668 ir_value *dummy = NULL;
2669 ir_value *irop = NULL;
2670 ir_block *bout = NULL;
2671 ir_block *bfall = NULL;
2679 compile_error(ast_ctx(self), "switch expression is not an l-value");
2683 if (self->expression.outr) {
2684 compile_error(ast_ctx(self), "internal error: ast_switch cannot be reused!");
2687 self->expression.outr = (ir_value*)1;
2692 cgen = self->operand->expression.codegen;
2693 if (!(*cgen)((ast_expression*)(self->operand), func, false, &irop))
2696 if (!vec_size(self->cases))
2699 cmpinstr = type_eq_instr[irop->vtype];
2700 if (cmpinstr >= AINSTR_END) {
2701 ast_type_to_string(self->operand, typestr, sizeof(typestr));
2702 compile_error(ast_ctx(self), "invalid type to perform a switch on: %s", typestr);
2706 bout_id = vec_size(func->ir_func->blocks);
2707 bout = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "after_switch"));
2711 /* setup the break block */
2712 vec_push(func->breakblocks, bout);
2714 /* Now create all cases */
2715 for (c = 0; c < vec_size(self->cases); ++c) {
2716 ir_value *cond, *val;
2717 ir_block *bcase, *bnot;
2720 ast_switch_case *swcase = &self->cases[c];
2722 if (swcase->value) {
2723 /* A regular case */
2724 /* generate the condition operand */
2725 cgen = swcase->value->expression.codegen;
2726 if (!(*cgen)((ast_expression*)(swcase->value), func, false, &val))
2728 /* generate the condition */
2729 cond = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "switch_eq"), cmpinstr, irop, val);
2733 bcase = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "case"));
2734 bnot_id = vec_size(func->ir_func->blocks);
2735 bnot = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "not_case"));
2736 if (!bcase || !bnot)
2738 if (set_def_bfall_to) {
2739 set_def_bfall_to = false;
2740 def_bfall_to = bcase;
2742 if (!ir_block_create_if(func->curblock, ast_ctx(self), cond, bcase, bnot))
2745 /* Make the previous case-end fall through */
2746 if (bfall && !bfall->final) {
2747 if (!ir_block_create_jump(bfall, ast_ctx(self), bcase))
2751 /* enter the case */
2752 func->curblock = bcase;
2753 cgen = swcase->code->expression.codegen;
2754 if (!(*cgen)((ast_expression*)swcase->code, func, false, &dummy))
2757 /* remember this block to fall through from */
2758 bfall = func->curblock;
2760 /* enter the else and move it down */
2761 func->curblock = bnot;
2762 vec_remove(func->ir_func->blocks, bnot_id, 1);
2763 vec_push(func->ir_func->blocks, bnot);
2765 /* The default case */
2766 /* Remember where to fall through from: */
2769 /* remember which case it was */
2771 /* And the next case will be remembered */
2772 set_def_bfall_to = true;
2776 /* Jump from the last bnot to bout */
2777 if (bfall && !bfall->final && !ir_block_create_jump(bfall, ast_ctx(self), bout)) {
2779 astwarning(ast_ctx(bfall), WARN_???, "missing break after last case");
2784 /* If there was a default case, put it down here */
2788 /* No need to create an extra block */
2789 bcase = func->curblock;
2791 /* Insert the fallthrough jump */
2792 if (def_bfall && !def_bfall->final) {
2793 if (!ir_block_create_jump(def_bfall, ast_ctx(self), bcase))
2797 /* Now generate the default code */
2798 cgen = def_case->code->expression.codegen;
2799 if (!(*cgen)((ast_expression*)def_case->code, func, false, &dummy))
2802 /* see if we need to fall through */
2803 if (def_bfall_to && !func->curblock->final)
2805 if (!ir_block_create_jump(func->curblock, ast_ctx(self), def_bfall_to))
2810 /* Jump from the last bnot to bout */
2811 if (!func->curblock->final && !ir_block_create_jump(func->curblock, ast_ctx(self), bout))
2813 /* enter the outgoing block */
2814 func->curblock = bout;
2816 /* restore the break block */
2817 vec_pop(func->breakblocks);
2819 /* Move 'bout' to the end, it's nicer */
2820 vec_remove(func->ir_func->blocks, bout_id, 1);
2821 vec_push(func->ir_func->blocks, bout);
2826 bool ast_label_codegen(ast_label *self, ast_function *func, bool lvalue, ir_value **out)
2833 compile_error(ast_ctx(self), "internal error: ast_label cannot be an lvalue");
2837 /* simply create a new block and jump to it */
2838 self->irblock = ir_function_create_block(ast_ctx(self), func->ir_func, self->name);
2839 if (!self->irblock) {
2840 compile_error(ast_ctx(self), "failed to allocate label block `%s`", self->name);
2843 if (!func->curblock->final) {
2844 if (!ir_block_create_jump(func->curblock, ast_ctx(self), self->irblock))
2848 /* enter the new block */
2849 func->curblock = self->irblock;
2851 /* Generate all the leftover gotos */
2852 for (i = 0; i < vec_size(self->gotos); ++i) {
2853 if (!ast_goto_codegen(self->gotos[i], func, false, &dummy))
2860 bool ast_goto_codegen(ast_goto *self, ast_function *func, bool lvalue, ir_value **out)
2864 compile_error(ast_ctx(self), "internal error: ast_goto cannot be an lvalue");
2868 if (self->target->irblock) {
2869 if (self->irblock_from) {
2870 /* we already tried once, this is the callback */
2871 self->irblock_from->final = false;
2872 if (!ir_block_create_goto(self->irblock_from, ast_ctx(self), self->target->irblock)) {
2873 compile_error(ast_ctx(self), "failed to generate goto to `%s`", self->name);
2879 if (!ir_block_create_goto(func->curblock, ast_ctx(self), self->target->irblock)) {
2880 compile_error(ast_ctx(self), "failed to generate goto to `%s`", self->name);
2887 /* the target has not yet been created...
2888 * close this block in a sneaky way:
2890 func->curblock->final = true;
2891 self->irblock_from = func->curblock;
2892 ast_label_register_goto(self->target, self);
2898 bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value **out)
2900 ast_expression_codegen *cgen;
2902 ir_instr *callinstr;
2905 ir_value *funval = NULL;
2907 /* return values are never lvalues */
2909 compile_error(ast_ctx(self), "not an l-value (function call)");
2913 if (self->expression.outr) {
2914 *out = self->expression.outr;
2918 cgen = self->func->expression.codegen;
2919 if (!(*cgen)((ast_expression*)(self->func), func, false, &funval))
2927 for (i = 0; i < vec_size(self->params); ++i)
2930 ast_expression *expr = self->params[i];
2932 cgen = expr->expression.codegen;
2933 if (!(*cgen)(expr, func, false, ¶m))
2937 vec_push(params, param);
2940 callinstr = ir_block_create_call(func->curblock, ast_ctx(self),
2941 ast_function_label(func, "call"),
2942 funval, !!(self->func->expression.flags & AST_FLAG_NORETURN));
2946 for (i = 0; i < vec_size(params); ++i) {
2947 ir_call_param(callinstr, params[i]);
2950 *out = ir_call_value(callinstr);
2951 self->expression.outr = *out;
2953 codegen_output_type(self, *out);