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 & AST_FLAG_TYPE_MASK) !=
213 (b->expression.flags & AST_FLAG_TYPE_MASK) )
217 if (vec_size(a->expression.params)) {
219 for (i = 0; i < vec_size(a->expression.params); ++i) {
220 if (!ast_compare_type((ast_expression*)a->expression.params[i],
221 (ast_expression*)b->expression.params[i]))
225 if (a->expression.next)
226 return ast_compare_type(a->expression.next, b->expression.next);
230 static size_t ast_type_to_string_impl(ast_expression *e, char *buf, size_t bufsize, size_t pos)
237 if (pos + 6 >= bufsize)
239 strcpy(buf + pos, "(null)");
243 if (pos + 1 >= bufsize)
246 switch (e->expression.vtype) {
248 strcpy(buf + pos, "(variant)");
253 return ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
256 if (pos + 3 >= bufsize)
260 pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
261 if (pos + 1 >= bufsize)
267 pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
268 if (pos + 2 >= bufsize)
270 if (!vec_size(e->expression.params)) {
276 pos = ast_type_to_string_impl((ast_expression*)(e->expression.params[0]), buf, bufsize, pos);
277 for (i = 1; i < vec_size(e->expression.params); ++i) {
278 if (pos + 2 >= bufsize)
282 pos = ast_type_to_string_impl((ast_expression*)(e->expression.params[i]), buf, bufsize, pos);
284 if (pos + 1 >= bufsize)
290 pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
291 if (pos + 1 >= bufsize)
294 pos += snprintf(buf + pos, bufsize - pos - 1, "%i", (int)e->expression.count);
295 if (pos + 1 >= bufsize)
301 typestr = type_name[e->expression.vtype];
302 typelen = strlen(typestr);
303 if (pos + typelen >= bufsize)
305 strcpy(buf + pos, typestr);
306 return pos + typelen;
310 buf[bufsize-3] = '.';
311 buf[bufsize-2] = '.';
312 buf[bufsize-1] = '.';
316 void ast_type_to_string(ast_expression *e, char *buf, size_t bufsize)
318 size_t pos = ast_type_to_string_impl(e, buf, bufsize-1, 0);
322 ast_value* ast_value_new(lex_ctx ctx, const char *name, int t)
324 ast_instantiate(ast_value, ctx, ast_value_delete);
325 ast_expression_init((ast_expression*)self,
326 (ast_expression_codegen*)&ast_value_codegen);
327 self->expression.node.keep = true; /* keep */
329 self->name = name ? util_strdup(name) : NULL;
330 self->expression.vtype = t;
331 self->expression.next = NULL;
332 self->isfield = false;
334 self->hasvalue = false;
336 memset(&self->constval, 0, sizeof(self->constval));
339 self->ir_values = NULL;
340 self->ir_value_count = 0;
348 void ast_value_delete(ast_value* self)
351 mem_d((void*)self->name);
352 if (self->hasvalue) {
353 switch (self->expression.vtype)
356 mem_d((void*)self->constval.vstring);
359 /* unlink us from the function node */
360 self->constval.vfunc->vtype = NULL;
362 /* NOTE: delete function? currently collected in
363 * the parser structure
370 mem_d(self->ir_values);
371 ast_expression_delete((ast_expression*)self);
375 void ast_value_params_add(ast_value *self, ast_value *p)
377 vec_push(self->expression.params, p);
380 bool ast_value_set_name(ast_value *self, const char *name)
383 mem_d((void*)self->name);
384 self->name = util_strdup(name);
388 ast_binary* ast_binary_new(lex_ctx ctx, int op,
389 ast_expression* left, ast_expression* right)
391 ast_instantiate(ast_binary, ctx, ast_binary_delete);
392 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binary_codegen);
398 ast_propagate_effects(self, left);
399 ast_propagate_effects(self, right);
401 if (op >= INSTR_EQ_F && op <= INSTR_GT)
402 self->expression.vtype = TYPE_FLOAT;
403 else if (op == INSTR_AND || op == INSTR_OR) {
404 if (OPTS_FLAG(PERL_LOGIC))
405 ast_type_adopt(self, right);
407 self->expression.vtype = TYPE_FLOAT;
409 else if (op == INSTR_BITAND || op == INSTR_BITOR)
410 self->expression.vtype = TYPE_FLOAT;
411 else if (op == INSTR_MUL_VF || op == INSTR_MUL_FV)
412 self->expression.vtype = TYPE_VECTOR;
413 else if (op == INSTR_MUL_V)
414 self->expression.vtype = TYPE_FLOAT;
416 self->expression.vtype = left->expression.vtype;
421 void ast_binary_delete(ast_binary *self)
423 ast_unref(self->left);
424 ast_unref(self->right);
425 ast_expression_delete((ast_expression*)self);
429 ast_binstore* ast_binstore_new(lex_ctx ctx, int storop, int op,
430 ast_expression* left, ast_expression* right)
432 ast_instantiate(ast_binstore, ctx, ast_binstore_delete);
433 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binstore_codegen);
435 ast_side_effects(self) = true;
437 self->opstore = storop;
440 self->source = right;
442 self->keep_dest = false;
444 if (!ast_type_adopt(self, left)) {
452 void ast_binstore_delete(ast_binstore *self)
454 if (!self->keep_dest)
455 ast_unref(self->dest);
456 ast_unref(self->source);
457 ast_expression_delete((ast_expression*)self);
461 ast_unary* ast_unary_new(lex_ctx ctx, int op,
462 ast_expression *expr)
464 ast_instantiate(ast_unary, ctx, ast_unary_delete);
465 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_unary_codegen);
468 self->operand = expr;
470 ast_propagate_effects(self, expr);
472 if (op >= INSTR_NOT_F && op <= INSTR_NOT_FNC) {
473 self->expression.vtype = TYPE_FLOAT;
475 compile_error(ctx, "cannot determine type of unary operation %s", asm_instr[op].m);
480 void ast_unary_delete(ast_unary *self)
482 if (self->operand) ast_unref(self->operand);
483 ast_expression_delete((ast_expression*)self);
487 ast_return* ast_return_new(lex_ctx ctx, ast_expression *expr)
489 ast_instantiate(ast_return, ctx, ast_return_delete);
490 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_return_codegen);
492 self->operand = expr;
495 ast_propagate_effects(self, expr);
500 void ast_return_delete(ast_return *self)
503 ast_unref(self->operand);
504 ast_expression_delete((ast_expression*)self);
508 ast_entfield* ast_entfield_new(lex_ctx ctx, ast_expression *entity, ast_expression *field)
510 if (field->expression.vtype != TYPE_FIELD) {
511 compile_error(ctx, "ast_entfield_new with expression not of type field");
514 return ast_entfield_new_force(ctx, entity, field, field->expression.next);
517 ast_entfield* ast_entfield_new_force(lex_ctx ctx, ast_expression *entity, ast_expression *field, const ast_expression *outtype)
519 ast_instantiate(ast_entfield, ctx, ast_entfield_delete);
523 /* Error: field has no type... */
527 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_entfield_codegen);
529 self->entity = entity;
531 ast_propagate_effects(self, entity);
532 ast_propagate_effects(self, field);
534 if (!ast_type_adopt(self, outtype)) {
535 ast_entfield_delete(self);
542 void ast_entfield_delete(ast_entfield *self)
544 ast_unref(self->entity);
545 ast_unref(self->field);
546 ast_expression_delete((ast_expression*)self);
550 ast_member* ast_member_new(lex_ctx ctx, ast_expression *owner, unsigned int field, const char *name)
552 ast_instantiate(ast_member, ctx, ast_member_delete);
558 if (owner->expression.vtype != TYPE_VECTOR &&
559 owner->expression.vtype != TYPE_FIELD) {
560 compile_error(ctx, "member-access on an invalid owner of type %s", type_name[owner->expression.vtype]);
565 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_member_codegen);
566 self->expression.node.keep = true; /* keep */
568 if (owner->expression.vtype == TYPE_VECTOR) {
569 self->expression.vtype = TYPE_FLOAT;
570 self->expression.next = NULL;
572 self->expression.vtype = TYPE_FIELD;
573 self->expression.next = ast_shallow_type(ctx, TYPE_FLOAT);
576 self->rvalue = false;
578 ast_propagate_effects(self, owner);
582 self->name = util_strdup(name);
589 void ast_member_delete(ast_member *self)
591 /* The owner is always an ast_value, which has .keep=true,
592 * also: ast_members are usually deleted after the owner, thus
593 * this will cause invalid access
594 ast_unref(self->owner);
595 * once we allow (expression).x to access a vector-member, we need
596 * to change this: preferably by creating an alternate ast node for this
597 * purpose that is not garbage-collected.
599 ast_expression_delete((ast_expression*)self);
603 bool ast_member_set_name(ast_member *self, const char *name)
606 mem_d((void*)self->name);
607 self->name = util_strdup(name);
611 ast_array_index* ast_array_index_new(lex_ctx ctx, ast_expression *array, ast_expression *index)
613 ast_expression *outtype;
614 ast_instantiate(ast_array_index, ctx, ast_array_index_delete);
616 outtype = array->expression.next;
619 /* Error: field has no type... */
623 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_array_index_codegen);
627 ast_propagate_effects(self, array);
628 ast_propagate_effects(self, index);
630 if (!ast_type_adopt(self, outtype)) {
631 ast_array_index_delete(self);
634 if (array->expression.vtype == TYPE_FIELD && outtype->expression.vtype == TYPE_ARRAY) {
635 if (self->expression.vtype != TYPE_ARRAY) {
636 compile_error(ast_ctx(self), "array_index node on type");
637 ast_array_index_delete(self);
640 self->array = outtype;
641 self->expression.vtype = TYPE_FIELD;
647 void ast_array_index_delete(ast_array_index *self)
649 ast_unref(self->array);
650 ast_unref(self->index);
651 ast_expression_delete((ast_expression*)self);
655 ast_ifthen* ast_ifthen_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
657 ast_instantiate(ast_ifthen, ctx, ast_ifthen_delete);
658 if (!ontrue && !onfalse) {
659 /* because it is invalid */
663 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ifthen_codegen);
666 self->on_true = ontrue;
667 self->on_false = onfalse;
668 ast_propagate_effects(self, cond);
670 ast_propagate_effects(self, ontrue);
672 ast_propagate_effects(self, onfalse);
677 void ast_ifthen_delete(ast_ifthen *self)
679 ast_unref(self->cond);
681 ast_unref(self->on_true);
683 ast_unref(self->on_false);
684 ast_expression_delete((ast_expression*)self);
688 ast_ternary* ast_ternary_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
690 ast_instantiate(ast_ternary, ctx, ast_ternary_delete);
691 /* This time NEITHER must be NULL */
692 if (!ontrue || !onfalse) {
696 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ternary_codegen);
699 self->on_true = ontrue;
700 self->on_false = onfalse;
701 ast_propagate_effects(self, cond);
702 ast_propagate_effects(self, ontrue);
703 ast_propagate_effects(self, onfalse);
705 if (!ast_type_adopt(self, ontrue)) {
706 ast_ternary_delete(self);
713 void ast_ternary_delete(ast_ternary *self)
715 ast_unref(self->cond);
716 ast_unref(self->on_true);
717 ast_unref(self->on_false);
718 ast_expression_delete((ast_expression*)self);
722 ast_loop* ast_loop_new(lex_ctx ctx,
723 ast_expression *initexpr,
724 ast_expression *precond, bool pre_not,
725 ast_expression *postcond, bool post_not,
726 ast_expression *increment,
727 ast_expression *body)
729 ast_instantiate(ast_loop, ctx, ast_loop_delete);
730 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_loop_codegen);
732 self->initexpr = initexpr;
733 self->precond = precond;
734 self->postcond = postcond;
735 self->increment = increment;
738 self->pre_not = pre_not;
739 self->post_not = post_not;
742 ast_propagate_effects(self, initexpr);
744 ast_propagate_effects(self, precond);
746 ast_propagate_effects(self, postcond);
748 ast_propagate_effects(self, increment);
750 ast_propagate_effects(self, body);
755 void ast_loop_delete(ast_loop *self)
758 ast_unref(self->initexpr);
760 ast_unref(self->precond);
762 ast_unref(self->postcond);
764 ast_unref(self->increment);
766 ast_unref(self->body);
767 ast_expression_delete((ast_expression*)self);
771 ast_breakcont* ast_breakcont_new(lex_ctx ctx, bool iscont, unsigned int levels)
773 ast_instantiate(ast_breakcont, ctx, ast_breakcont_delete);
774 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_breakcont_codegen);
776 self->is_continue = iscont;
777 self->levels = levels;
782 void ast_breakcont_delete(ast_breakcont *self)
784 ast_expression_delete((ast_expression*)self);
788 ast_switch* ast_switch_new(lex_ctx ctx, ast_expression *op)
790 ast_instantiate(ast_switch, ctx, ast_switch_delete);
791 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_switch_codegen);
796 ast_propagate_effects(self, op);
801 void ast_switch_delete(ast_switch *self)
804 ast_unref(self->operand);
806 for (i = 0; i < vec_size(self->cases); ++i) {
807 if (self->cases[i].value)
808 ast_unref(self->cases[i].value);
809 ast_unref(self->cases[i].code);
811 vec_free(self->cases);
813 ast_expression_delete((ast_expression*)self);
817 ast_label* ast_label_new(lex_ctx ctx, const char *name)
819 ast_instantiate(ast_label, ctx, ast_label_delete);
820 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_label_codegen);
822 self->name = util_strdup(name);
823 self->irblock = NULL;
829 void ast_label_delete(ast_label *self)
831 mem_d((void*)self->name);
832 vec_free(self->gotos);
833 ast_expression_delete((ast_expression*)self);
837 void ast_label_register_goto(ast_label *self, ast_goto *g)
839 vec_push(self->gotos, g);
842 ast_goto* ast_goto_new(lex_ctx ctx, const char *name)
844 ast_instantiate(ast_goto, ctx, ast_goto_delete);
845 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_goto_codegen);
847 self->name = util_strdup(name);
849 self->irblock_from = NULL;
854 void ast_goto_delete(ast_goto *self)
856 mem_d((void*)self->name);
857 ast_expression_delete((ast_expression*)self);
861 void ast_goto_set_label(ast_goto *self, ast_label *label)
863 self->target = label;
866 ast_call* ast_call_new(lex_ctx ctx,
867 ast_expression *funcexpr)
869 ast_instantiate(ast_call, ctx, ast_call_delete);
870 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_call_codegen);
872 ast_side_effects(self) = true;
875 self->func = funcexpr;
877 ast_type_adopt(self, funcexpr->expression.next);
882 void ast_call_delete(ast_call *self)
885 for (i = 0; i < vec_size(self->params); ++i)
886 ast_unref(self->params[i]);
887 vec_free(self->params);
890 ast_unref(self->func);
892 ast_expression_delete((ast_expression*)self);
896 bool ast_call_check_types(ast_call *self)
900 const ast_expression *func = self->func;
901 size_t count = vec_size(self->params);
902 if (count > vec_size(func->expression.params))
903 count = vec_size(func->expression.params);
905 for (i = 0; i < count; ++i) {
906 if (!ast_compare_type(self->params[i], (ast_expression*)(func->expression.params[i]))) {
909 ast_type_to_string(self->params[i], tgot, sizeof(tgot));
910 ast_type_to_string((ast_expression*)func->expression.params[i], texp, sizeof(texp));
911 compile_error(ast_ctx(self), "invalid type for parameter %u in function call: expected %s, got %s",
912 (unsigned int)(i+1), texp, tgot);
913 /* we don't immediately return */
920 ast_store* ast_store_new(lex_ctx ctx, int op,
921 ast_expression *dest, ast_expression *source)
923 ast_instantiate(ast_store, ctx, ast_store_delete);
924 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_store_codegen);
926 ast_side_effects(self) = true;
930 self->source = source;
932 if (!ast_type_adopt(self, dest)) {
940 void ast_store_delete(ast_store *self)
942 ast_unref(self->dest);
943 ast_unref(self->source);
944 ast_expression_delete((ast_expression*)self);
948 ast_block* ast_block_new(lex_ctx ctx)
950 ast_instantiate(ast_block, ctx, ast_block_delete);
951 ast_expression_init((ast_expression*)self,
952 (ast_expression_codegen*)&ast_block_codegen);
956 self->collect = NULL;
961 bool ast_block_add_expr(ast_block *self, ast_expression *e)
963 ast_propagate_effects(self, e);
964 vec_push(self->exprs, e);
965 if (self->expression.next) {
966 ast_delete(self->expression.next);
967 self->expression.next = NULL;
969 if (!ast_type_adopt(self, e)) {
970 compile_error(ast_ctx(self), "internal error: failed to adopt type");
976 void ast_block_collect(ast_block *self, ast_expression *expr)
978 vec_push(self->collect, expr);
979 expr->expression.node.keep = true;
982 void ast_block_delete(ast_block *self)
985 for (i = 0; i < vec_size(self->exprs); ++i)
986 ast_unref(self->exprs[i]);
987 vec_free(self->exprs);
988 for (i = 0; i < vec_size(self->locals); ++i)
989 ast_delete(self->locals[i]);
990 vec_free(self->locals);
991 for (i = 0; i < vec_size(self->collect); ++i)
992 ast_delete(self->collect[i]);
993 vec_free(self->collect);
994 ast_expression_delete((ast_expression*)self);
998 bool ast_block_set_type(ast_block *self, ast_expression *from)
1000 if (self->expression.next)
1001 ast_delete(self->expression.next);
1002 if (!ast_type_adopt(self, from))
1007 ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype)
1009 ast_instantiate(ast_function, ctx, ast_function_delete);
1013 vtype->expression.vtype != TYPE_FUNCTION)
1015 compile_error(ast_ctx(self), "internal error: ast_function_new condition %i %i type=%i (probably 2 bodies?)",
1017 (int)vtype->hasvalue,
1018 vtype->expression.vtype);
1023 self->vtype = vtype;
1024 self->name = name ? util_strdup(name) : NULL;
1025 self->blocks = NULL;
1027 self->labelcount = 0;
1030 self->ir_func = NULL;
1031 self->curblock = NULL;
1033 self->breakblocks = NULL;
1034 self->continueblocks = NULL;
1036 vtype->hasvalue = true;
1037 vtype->constval.vfunc = self;
1042 void ast_function_delete(ast_function *self)
1046 mem_d((void*)self->name);
1048 /* ast_value_delete(self->vtype); */
1049 self->vtype->hasvalue = false;
1050 self->vtype->constval.vfunc = NULL;
1051 /* We use unref - if it was stored in a global table it is supposed
1052 * to be deleted from *there*
1054 ast_unref(self->vtype);
1056 for (i = 0; i < vec_size(self->blocks); ++i)
1057 ast_delete(self->blocks[i]);
1058 vec_free(self->blocks);
1059 vec_free(self->breakblocks);
1060 vec_free(self->continueblocks);
1064 const char* ast_function_label(ast_function *self, const char *prefix)
1070 if (!opts.dump && !opts.dumpfin && !opts.debug)
1073 id = (self->labelcount++);
1074 len = strlen(prefix);
1076 from = self->labelbuf + sizeof(self->labelbuf)-1;
1079 *from-- = (id%10) + '0';
1083 memcpy(from - len, prefix, len);
1087 /*********************************************************************/
1089 * by convention you must never pass NULL to the 'ir_value **out'
1090 * parameter. If you really don't care about the output, pass a dummy.
1091 * But I can't imagine a pituation where the output is truly unnecessary.
1094 void _ast_codegen_output_type(ast_expression_common *self, ir_value *out)
1096 if (out->vtype == TYPE_FIELD)
1097 out->fieldtype = self->next->expression.vtype;
1098 if (out->vtype == TYPE_FUNCTION)
1099 out->outtype = self->next->expression.vtype;
1102 #define codegen_output_type(a,o) (_ast_codegen_output_type(&((a)->expression),(o)))
1104 bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out)
1108 if (self->expression.vtype == TYPE_NIL) {
1109 *out = func->ir_func->owner->nil;
1112 /* NOTE: This is the codegen for a variable used in an expression.
1113 * It is not the codegen to generate the value. For this purpose,
1114 * ast_local_codegen and ast_global_codegen are to be used before this
1115 * is executed. ast_function_codegen should take care of its locals,
1116 * and the ast-user should take care of ast_global_codegen to be used
1117 * on all the globals.
1120 char tname[1024]; /* typename is reserved in C++ */
1121 ast_type_to_string((ast_expression*)self, tname, sizeof(tname));
1122 compile_error(ast_ctx(self), "ast_value used before generated %s %s", tname, self->name);
1129 bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
1133 if (self->expression.vtype == TYPE_NIL) {
1134 compile_error(ast_ctx(self), "internal error: trying to generate a variable of TYPE_NIL");
1138 if (self->hasvalue && self->expression.vtype == TYPE_FUNCTION)
1140 ir_function *func = ir_builder_create_function(ir, self->name, self->expression.next->expression.vtype);
1143 func->context = ast_ctx(self);
1144 func->value->context = ast_ctx(self);
1146 self->constval.vfunc->ir_func = func;
1147 self->ir_v = func->value;
1148 /* The function is filled later on ast_function_codegen... */
1152 if (isfield && self->expression.vtype == TYPE_FIELD) {
1153 ast_expression *fieldtype = self->expression.next;
1155 if (self->hasvalue) {
1156 compile_error(ast_ctx(self), "TODO: constant field pointers with value");
1160 if (fieldtype->expression.vtype == TYPE_ARRAY) {
1165 ast_expression_common *elemtype;
1167 ast_value *array = (ast_value*)fieldtype;
1169 if (!ast_istype(fieldtype, ast_value)) {
1170 compile_error(ast_ctx(self), "internal error: ast_value required");
1174 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1175 if (!array->expression.count || array->expression.count > opts.max_array_size)
1176 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)array->expression.count);
1178 elemtype = &array->expression.next->expression;
1179 vtype = elemtype->vtype;
1181 v = ir_builder_create_field(ir, self->name, vtype);
1183 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", self->name);
1186 v->context = ast_ctx(self);
1187 v->unique_life = true;
1189 array->ir_v = self->ir_v = v;
1191 namelen = strlen(self->name);
1192 name = (char*)mem_a(namelen + 16);
1193 strcpy(name, self->name);
1195 array->ir_values = (ir_value**)mem_a(sizeof(array->ir_values[0]) * array->expression.count);
1196 array->ir_values[0] = v;
1197 for (ai = 1; ai < array->expression.count; ++ai) {
1198 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1199 array->ir_values[ai] = ir_builder_create_field(ir, name, vtype);
1200 if (!array->ir_values[ai]) {
1202 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", name);
1205 array->ir_values[ai]->context = ast_ctx(self);
1206 array->ir_values[ai]->unique_life = true;
1207 array->ir_values[ai]->locked = true;
1213 v = ir_builder_create_field(ir, self->name, self->expression.next->expression.vtype);
1216 v->context = ast_ctx(self);
1222 if (self->expression.vtype == TYPE_ARRAY) {
1227 ast_expression_common *elemtype = &self->expression.next->expression;
1228 int vtype = elemtype->vtype;
1230 /* same as with field arrays */
1231 if (!self->expression.count || self->expression.count > opts.max_array_size)
1232 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1234 v = ir_builder_create_global(ir, self->name, vtype);
1236 compile_error(ast_ctx(self), "ir_builder_create_global failed `%s`", self->name);
1239 v->context = ast_ctx(self);
1240 v->unique_life = true;
1243 namelen = strlen(self->name);
1244 name = (char*)mem_a(namelen + 16);
1245 strcpy(name, self->name);
1247 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1248 self->ir_values[0] = v;
1249 for (ai = 1; ai < self->expression.count; ++ai) {
1250 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1251 self->ir_values[ai] = ir_builder_create_global(ir, name, vtype);
1252 if (!self->ir_values[ai]) {
1254 compile_error(ast_ctx(self), "ir_builder_create_global failed `%s`", name);
1257 self->ir_values[ai]->context = ast_ctx(self);
1258 self->ir_values[ai]->unique_life = true;
1259 self->ir_values[ai]->locked = true;
1265 /* Arrays don't do this since there's no "array" value which spans across the
1268 v = ir_builder_create_global(ir, self->name, self->expression.vtype);
1270 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", self->name);
1273 codegen_output_type(self, v);
1274 v->context = ast_ctx(self);
1277 if (self->hasvalue) {
1278 switch (self->expression.vtype)
1281 if (!ir_value_set_float(v, self->constval.vfloat))
1285 if (!ir_value_set_vector(v, self->constval.vvec))
1289 if (!ir_value_set_string(v, self->constval.vstring))
1293 compile_error(ast_ctx(self), "TODO: global constant array");
1296 compile_error(ast_ctx(self), "global of type function not properly generated");
1298 /* Cannot generate an IR value for a function,
1299 * need a pointer pointing to a function rather.
1302 if (!self->constval.vfield) {
1303 compile_error(ast_ctx(self), "field constant without vfield set");
1306 if (!self->constval.vfield->ir_v) {
1307 compile_error(ast_ctx(self), "field constant generated before its field");
1310 if (!ir_value_set_field(v, self->constval.vfield->ir_v))
1314 compile_error(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1319 /* link us to the ir_value */
1324 error: /* clean up */
1329 bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
1333 if (self->expression.vtype == TYPE_NIL) {
1334 compile_error(ast_ctx(self), "internal error: trying to generate a variable of TYPE_NIL");
1338 if (self->hasvalue && self->expression.vtype == TYPE_FUNCTION)
1340 /* Do we allow local functions? I think not...
1341 * this is NOT a function pointer atm.
1346 if (self->expression.vtype == TYPE_ARRAY) {
1351 ast_expression_common *elemtype = &self->expression.next->expression;
1352 int vtype = elemtype->vtype;
1354 func->flags |= IR_FLAG_HAS_ARRAYS;
1357 compile_error(ast_ctx(self), "array-parameters are not supported");
1361 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1362 if (!self->expression.count || self->expression.count > opts.max_array_size) {
1363 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1366 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1367 if (!self->ir_values) {
1368 compile_error(ast_ctx(self), "failed to allocate array values");
1372 v = ir_function_create_local(func, self->name, vtype, param);
1374 compile_error(ast_ctx(self), "ir_function_create_local failed");
1377 v->context = ast_ctx(self);
1378 v->unique_life = true;
1381 namelen = strlen(self->name);
1382 name = (char*)mem_a(namelen + 16);
1383 strcpy(name, self->name);
1385 self->ir_values[0] = v;
1386 for (ai = 1; ai < self->expression.count; ++ai) {
1387 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1388 self->ir_values[ai] = ir_function_create_local(func, name, vtype, param);
1389 if (!self->ir_values[ai]) {
1390 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", name);
1393 self->ir_values[ai]->context = ast_ctx(self);
1394 self->ir_values[ai]->unique_life = true;
1395 self->ir_values[ai]->locked = true;
1400 v = ir_function_create_local(func, self->name, self->expression.vtype, param);
1403 codegen_output_type(self, v);
1404 v->context = ast_ctx(self);
1407 /* A constant local... hmmm...
1408 * I suppose the IR will have to deal with this
1410 if (self->hasvalue) {
1411 switch (self->expression.vtype)
1414 if (!ir_value_set_float(v, self->constval.vfloat))
1418 if (!ir_value_set_vector(v, self->constval.vvec))
1422 if (!ir_value_set_string(v, self->constval.vstring))
1426 compile_error(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1431 /* link us to the ir_value */
1435 if (!ast_generate_accessors(self, func->owner))
1439 error: /* clean up */
1444 bool ast_generate_accessors(ast_value *self, ir_builder *ir)
1447 bool warn = OPTS_WARN(WARN_USED_UNINITIALIZED);
1448 if (!self->setter || !self->getter)
1450 for (i = 0; i < self->expression.count; ++i) {
1451 if (!self->ir_values) {
1452 compile_error(ast_ctx(self), "internal error: no array values generated for `%s`", self->name);
1455 if (!self->ir_values[i]) {
1456 compile_error(ast_ctx(self), "internal error: not all array values have been generated for `%s`", self->name);
1459 if (self->ir_values[i]->life) {
1460 compile_error(ast_ctx(self), "internal error: function containing `%s` already generated", self->name);
1465 opts_set(opts.warn, WARN_USED_UNINITIALIZED, false);
1467 if (!ast_global_codegen (self->setter, ir, false) ||
1468 !ast_function_codegen(self->setter->constval.vfunc, ir) ||
1469 !ir_function_finalize(self->setter->constval.vfunc->ir_func))
1471 compile_error(ast_ctx(self), "internal error: failed to generate setter for `%s`", self->name);
1472 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1477 if (!ast_global_codegen (self->getter, ir, false) ||
1478 !ast_function_codegen(self->getter->constval.vfunc, ir) ||
1479 !ir_function_finalize(self->getter->constval.vfunc->ir_func))
1481 compile_error(ast_ctx(self), "internal error: failed to generate getter for `%s`", self->name);
1482 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1486 for (i = 0; i < self->expression.count; ++i) {
1487 vec_free(self->ir_values[i]->life);
1489 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1493 bool ast_function_codegen(ast_function *self, ir_builder *ir)
1497 ast_expression_common *ec;
1502 irf = self->ir_func;
1504 compile_error(ast_ctx(self), "ast_function's related ast_value was not generated yet");
1508 /* fill the parameter list */
1509 ec = &self->vtype->expression;
1510 for (i = 0; i < vec_size(ec->params); ++i)
1512 if (ec->params[i]->expression.vtype == TYPE_FIELD)
1513 vec_push(irf->params, ec->params[i]->expression.next->expression.vtype);
1515 vec_push(irf->params, ec->params[i]->expression.vtype);
1516 if (!self->builtin) {
1517 if (!ast_local_codegen(ec->params[i], self->ir_func, true))
1522 if (self->builtin) {
1523 irf->builtin = self->builtin;
1527 if (!vec_size(self->blocks)) {
1528 compile_error(ast_ctx(self), "function `%s` has no body", self->name);
1532 self->curblock = ir_function_create_block(ast_ctx(self), irf, "entry");
1533 if (!self->curblock) {
1534 compile_error(ast_ctx(self), "failed to allocate entry block for `%s`", self->name);
1538 for (i = 0; i < vec_size(self->blocks); ++i) {
1539 ast_expression_codegen *gen = self->blocks[i]->expression.codegen;
1540 if (!(*gen)((ast_expression*)self->blocks[i], self, false, &dummy))
1544 /* TODO: check return types */
1545 if (!self->curblock->final)
1547 if (!self->vtype->expression.next ||
1548 self->vtype->expression.next->expression.vtype == TYPE_VOID)
1550 return ir_block_create_return(self->curblock, ast_ctx(self), NULL);
1552 else if (vec_size(self->curblock->entries))
1554 /* error("missing return"); */
1555 if (compile_warning(ast_ctx(self), WARN_MISSING_RETURN_VALUES,
1556 "control reaches end of non-void function (`%s`) via %s",
1557 self->name, self->curblock->label))
1561 return ir_block_create_return(self->curblock, ast_ctx(self), NULL);
1567 /* Note, you will not see ast_block_codegen generate ir_blocks.
1568 * To the AST and the IR, blocks are 2 different things.
1569 * In the AST it represents a block of code, usually enclosed in
1570 * curly braces {...}.
1571 * While in the IR it represents a block in terms of control-flow.
1573 bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_value **out)
1577 /* We don't use this
1578 * Note: an ast-representation using the comma-operator
1579 * of the form: (a, b, c) = x should not assign to c...
1582 compile_error(ast_ctx(self), "not an l-value (code-block)");
1586 if (self->expression.outr) {
1587 *out = self->expression.outr;
1591 /* output is NULL at first, we'll have each expression
1592 * assign to out output, thus, a comma-operator represention
1593 * using an ast_block will return the last generated value,
1594 * so: (b, c) + a executed both b and c, and returns c,
1595 * which is then added to a.
1599 /* generate locals */
1600 for (i = 0; i < vec_size(self->locals); ++i)
1602 if (!ast_local_codegen(self->locals[i], func->ir_func, false)) {
1604 compile_error(ast_ctx(self), "failed to generate local `%s`", self->locals[i]->name);
1609 for (i = 0; i < vec_size(self->exprs); ++i)
1611 ast_expression_codegen *gen;
1612 if (func->curblock->final && !ast_istype(self->exprs[i], ast_label)) {
1613 if (compile_warning(ast_ctx(self->exprs[i]), WARN_UNREACHABLE_CODE, "unreachable statement"))
1617 gen = self->exprs[i]->expression.codegen;
1618 if (!(*gen)(self->exprs[i], func, false, out))
1622 self->expression.outr = *out;
1627 bool ast_store_codegen(ast_store *self, ast_function *func, bool lvalue, ir_value **out)
1629 ast_expression_codegen *cgen;
1630 ir_value *left = NULL;
1631 ir_value *right = NULL;
1635 ast_array_index *ai = NULL;
1637 if (lvalue && self->expression.outl) {
1638 *out = self->expression.outl;
1642 if (!lvalue && self->expression.outr) {
1643 *out = self->expression.outr;
1647 if (ast_istype(self->dest, ast_array_index))
1650 ai = (ast_array_index*)self->dest;
1651 idx = (ast_value*)ai->index;
1653 if (ast_istype(ai->index, ast_value) && idx->hasvalue && idx->cvq == CV_CONST)
1658 /* we need to call the setter */
1659 ir_value *iridx, *funval;
1663 compile_error(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1667 arr = (ast_value*)ai->array;
1668 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1669 compile_error(ast_ctx(self), "value has no setter (%s)", arr->name);
1673 cgen = idx->expression.codegen;
1674 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1677 cgen = arr->setter->expression.codegen;
1678 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
1681 cgen = self->source->expression.codegen;
1682 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1685 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "store"), funval, false);
1688 ir_call_param(call, iridx);
1689 ir_call_param(call, right);
1690 self->expression.outr = right;
1696 cgen = self->dest->expression.codegen;
1698 if (!(*cgen)((ast_expression*)(self->dest), func, true, &left))
1700 self->expression.outl = left;
1702 cgen = self->source->expression.codegen;
1704 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1707 if (!ir_block_create_store_op(func->curblock, ast_ctx(self), self->op, left, right))
1709 self->expression.outr = right;
1712 /* Theoretically, an assinment returns its left side as an
1713 * lvalue, if we don't need an lvalue though, we return
1714 * the right side as an rvalue, otherwise we have to
1715 * somehow know whether or not we need to dereference the pointer
1716 * on the left side - that is: OP_LOAD if it was an address.
1717 * Also: in original QC we cannot OP_LOADP *anyway*.
1719 *out = (lvalue ? left : right);
1724 bool ast_binary_codegen(ast_binary *self, ast_function *func, bool lvalue, ir_value **out)
1726 ast_expression_codegen *cgen;
1727 ir_value *left, *right;
1729 /* A binary operation cannot yield an l-value */
1731 compile_error(ast_ctx(self), "not an l-value (binop)");
1735 if (self->expression.outr) {
1736 *out = self->expression.outr;
1740 if ((OPTS_FLAG(SHORT_LOGIC) || OPTS_FLAG(PERL_LOGIC)) &&
1741 (self->op == INSTR_AND || self->op == INSTR_OR))
1743 /* short circuit evaluation */
1744 ir_block *other, *merge;
1745 ir_block *from_left, *from_right;
1749 /* prepare end-block */
1750 merge_id = vec_size(func->ir_func->blocks);
1751 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "sce_merge"));
1753 /* generate the left expression */
1754 cgen = self->left->expression.codegen;
1755 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1757 /* remember the block */
1758 from_left = func->curblock;
1760 /* create a new block for the right expression */
1761 other = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "sce_other"));
1762 if (self->op == INSTR_AND) {
1763 /* on AND: left==true -> other */
1764 if (!ir_block_create_if(func->curblock, ast_ctx(self), left, other, merge))
1767 /* on OR: left==false -> other */
1768 if (!ir_block_create_if(func->curblock, ast_ctx(self), left, merge, other))
1771 /* use the likely flag */
1772 vec_last(func->curblock->instr)->likely = true;
1774 /* enter the right-expression's block */
1775 func->curblock = other;
1777 cgen = self->right->expression.codegen;
1778 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1780 /* remember block */
1781 from_right = func->curblock;
1783 /* jump to the merge block */
1784 if (!ir_block_create_jump(func->curblock, ast_ctx(self), merge))
1787 vec_remove(func->ir_func->blocks, merge_id, 1);
1788 vec_push(func->ir_func->blocks, merge);
1790 func->curblock = merge;
1791 phi = ir_block_create_phi(func->curblock, ast_ctx(self),
1792 ast_function_label(func, "sce_value"),
1793 self->expression.vtype);
1794 ir_phi_add(phi, from_left, left);
1795 ir_phi_add(phi, from_right, right);
1796 *out = ir_phi_value(phi);
1800 if (!OPTS_FLAG(PERL_LOGIC)) {
1802 if (OPTS_FLAG(CORRECT_LOGIC) && (*out)->vtype == TYPE_VECTOR) {
1803 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
1804 ast_function_label(func, "sce_bool_v"),
1808 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
1809 ast_function_label(func, "sce_bool"),
1814 else if (OPTS_FLAG(FALSE_EMPTY_STRINGS) && (*out)->vtype == TYPE_STRING) {
1815 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
1816 ast_function_label(func, "sce_bool_s"),
1820 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
1821 ast_function_label(func, "sce_bool"),
1827 *out = ir_block_create_binop(func->curblock, ast_ctx(self),
1828 ast_function_label(func, "sce_bool"),
1829 INSTR_AND, *out, *out);
1835 self->expression.outr = *out;
1839 cgen = self->left->expression.codegen;
1840 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1843 cgen = self->right->expression.codegen;
1844 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1847 *out = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "bin"),
1848 self->op, left, right);
1851 self->expression.outr = *out;
1856 bool ast_binstore_codegen(ast_binstore *self, ast_function *func, bool lvalue, ir_value **out)
1858 ast_expression_codegen *cgen;
1859 ir_value *leftl = NULL, *leftr, *right, *bin;
1863 ast_array_index *ai = NULL;
1864 ir_value *iridx = NULL;
1866 if (lvalue && self->expression.outl) {
1867 *out = self->expression.outl;
1871 if (!lvalue && self->expression.outr) {
1872 *out = self->expression.outr;
1876 if (ast_istype(self->dest, ast_array_index))
1879 ai = (ast_array_index*)self->dest;
1880 idx = (ast_value*)ai->index;
1882 if (ast_istype(ai->index, ast_value) && idx->hasvalue && idx->cvq == CV_CONST)
1886 /* for a binstore we need both an lvalue and an rvalue for the left side */
1887 /* rvalue of destination! */
1889 cgen = idx->expression.codegen;
1890 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1893 cgen = self->dest->expression.codegen;
1894 if (!(*cgen)((ast_expression*)(self->dest), func, false, &leftr))
1897 /* source as rvalue only */
1898 cgen = self->source->expression.codegen;
1899 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1902 /* now the binary */
1903 bin = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "binst"),
1904 self->opbin, leftr, right);
1905 self->expression.outr = bin;
1909 /* we need to call the setter */
1914 compile_error(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1918 arr = (ast_value*)ai->array;
1919 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1920 compile_error(ast_ctx(self), "value has no setter (%s)", arr->name);
1924 cgen = arr->setter->expression.codegen;
1925 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
1928 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "store"), funval, false);
1931 ir_call_param(call, iridx);
1932 ir_call_param(call, bin);
1933 self->expression.outr = bin;
1935 /* now store them */
1936 cgen = self->dest->expression.codegen;
1937 /* lvalue of destination */
1938 if (!(*cgen)((ast_expression*)(self->dest), func, true, &leftl))
1940 self->expression.outl = leftl;
1942 if (!ir_block_create_store_op(func->curblock, ast_ctx(self), self->opstore, leftl, bin))
1944 self->expression.outr = bin;
1947 /* Theoretically, an assinment returns its left side as an
1948 * lvalue, if we don't need an lvalue though, we return
1949 * the right side as an rvalue, otherwise we have to
1950 * somehow know whether or not we need to dereference the pointer
1951 * on the left side - that is: OP_LOAD if it was an address.
1952 * Also: in original QC we cannot OP_LOADP *anyway*.
1954 *out = (lvalue ? leftl : bin);
1959 bool ast_unary_codegen(ast_unary *self, ast_function *func, bool lvalue, ir_value **out)
1961 ast_expression_codegen *cgen;
1964 /* An unary operation cannot yield an l-value */
1966 compile_error(ast_ctx(self), "not an l-value (binop)");
1970 if (self->expression.outr) {
1971 *out = self->expression.outr;
1975 cgen = self->operand->expression.codegen;
1977 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
1980 *out = ir_block_create_unary(func->curblock, ast_ctx(self), ast_function_label(func, "unary"),
1984 self->expression.outr = *out;
1989 bool ast_return_codegen(ast_return *self, ast_function *func, bool lvalue, ir_value **out)
1991 ast_expression_codegen *cgen;
1996 /* In the context of a return operation, we don't actually return
2000 compile_error(ast_ctx(self), "return-expression is not an l-value");
2004 if (self->expression.outr) {
2005 compile_error(ast_ctx(self), "internal error: ast_return cannot be reused, it bears no result!");
2008 self->expression.outr = (ir_value*)1;
2010 if (self->operand) {
2011 cgen = self->operand->expression.codegen;
2013 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
2016 if (!ir_block_create_return(func->curblock, ast_ctx(self), operand))
2019 if (!ir_block_create_return(func->curblock, ast_ctx(self), NULL))
2026 bool ast_entfield_codegen(ast_entfield *self, ast_function *func, bool lvalue, ir_value **out)
2028 ast_expression_codegen *cgen;
2029 ir_value *ent, *field;
2031 /* This function needs to take the 'lvalue' flag into account!
2032 * As lvalue we provide a field-pointer, as rvalue we provide the
2036 if (lvalue && self->expression.outl) {
2037 *out = self->expression.outl;
2041 if (!lvalue && self->expression.outr) {
2042 *out = self->expression.outr;
2046 cgen = self->entity->expression.codegen;
2047 if (!(*cgen)((ast_expression*)(self->entity), func, false, &ent))
2050 cgen = self->field->expression.codegen;
2051 if (!(*cgen)((ast_expression*)(self->field), func, false, &field))
2056 *out = ir_block_create_fieldaddress(func->curblock, ast_ctx(self), ast_function_label(func, "efa"),
2059 *out = ir_block_create_load_from_ent(func->curblock, ast_ctx(self), ast_function_label(func, "efv"),
2060 ent, field, self->expression.vtype);
2061 /* Done AFTER error checking:
2062 codegen_output_type(self, *out);
2066 compile_error(ast_ctx(self), "failed to create %s instruction (output type %s)",
2067 (lvalue ? "ADDRESS" : "FIELD"),
2068 type_name[self->expression.vtype]);
2072 codegen_output_type(self, *out);
2075 self->expression.outl = *out;
2077 self->expression.outr = *out;
2079 /* Hm that should be it... */
2083 bool ast_member_codegen(ast_member *self, ast_function *func, bool lvalue, ir_value **out)
2085 ast_expression_codegen *cgen;
2088 /* in QC this is always an lvalue */
2089 if (lvalue && self->rvalue) {
2090 compile_error(ast_ctx(self), "not an l-value (member access)");
2093 if (self->expression.outl) {
2094 *out = self->expression.outl;
2098 cgen = self->owner->expression.codegen;
2099 if (!(*cgen)((ast_expression*)(self->owner), func, false, &vec))
2102 if (vec->vtype != TYPE_VECTOR &&
2103 !(vec->vtype == TYPE_FIELD && self->owner->expression.next->expression.vtype == TYPE_VECTOR))
2108 *out = ir_value_vector_member(vec, self->field);
2109 self->expression.outl = *out;
2111 return (*out != NULL);
2114 bool ast_array_index_codegen(ast_array_index *self, ast_function *func, bool lvalue, ir_value **out)
2119 if (!lvalue && self->expression.outr) {
2120 *out = self->expression.outr;
2122 if (lvalue && self->expression.outl) {
2123 *out = self->expression.outl;
2126 if (!ast_istype(self->array, ast_value)) {
2127 compile_error(ast_ctx(self), "array indexing this way is not supported");
2128 /* note this would actually be pointer indexing because the left side is
2129 * not an actual array but (hopefully) an indexable expression.
2130 * Once we get integer arithmetic, and GADDRESS/GSTORE/GLOAD instruction
2131 * support this path will be filled.
2136 arr = (ast_value*)self->array;
2137 idx = (ast_value*)self->index;
2139 if (!ast_istype(self->index, ast_value) || !idx->hasvalue || idx->cvq != CV_CONST) {
2140 /* Time to use accessor functions */
2141 ast_expression_codegen *cgen;
2142 ir_value *iridx, *funval;
2146 compile_error(ast_ctx(self), "(.2) array indexing here needs a compile-time constant");
2151 compile_error(ast_ctx(self), "value has no getter, don't know how to index it");
2155 cgen = self->index->expression.codegen;
2156 if (!(*cgen)((ast_expression*)(self->index), func, false, &iridx))
2159 cgen = arr->getter->expression.codegen;
2160 if (!(*cgen)((ast_expression*)(arr->getter), func, true, &funval))
2163 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "fetch"), funval, false);
2166 ir_call_param(call, iridx);
2168 *out = ir_call_value(call);
2169 self->expression.outr = *out;
2173 if (idx->expression.vtype == TYPE_FLOAT) {
2174 unsigned int arridx = idx->constval.vfloat;
2175 if (arridx >= self->array->expression.count)
2177 compile_error(ast_ctx(self), "array index out of bounds: %i", arridx);
2180 *out = arr->ir_values[arridx];
2182 else if (idx->expression.vtype == TYPE_INTEGER) {
2183 unsigned int arridx = idx->constval.vint;
2184 if (arridx >= self->array->expression.count)
2186 compile_error(ast_ctx(self), "array index out of bounds: %i", arridx);
2189 *out = arr->ir_values[arridx];
2192 compile_error(ast_ctx(self), "array indexing here needs an integer constant");
2198 bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_value **out)
2200 ast_expression_codegen *cgen;
2208 ir_block *ontrue_endblock = NULL;
2209 ir_block *onfalse_endblock = NULL;
2210 ir_block *merge = NULL;
2212 /* We don't output any value, thus also don't care about r/lvalue */
2216 if (self->expression.outr) {
2217 compile_error(ast_ctx(self), "internal error: ast_ifthen cannot be reused, it bears no result!");
2220 self->expression.outr = (ir_value*)1;
2222 /* generate the condition */
2223 cgen = self->cond->expression.codegen;
2224 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2226 /* update the block which will get the jump - because short-logic or ternaries may have changed this */
2227 cond = func->curblock;
2231 if (self->on_true) {
2232 /* create on-true block */
2233 ontrue = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "ontrue"));
2237 /* enter the block */
2238 func->curblock = ontrue;
2241 cgen = self->on_true->expression.codegen;
2242 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &dummy))
2245 /* we now need to work from the current endpoint */
2246 ontrue_endblock = func->curblock;
2251 if (self->on_false) {
2252 /* create on-false block */
2253 onfalse = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "onfalse"));
2257 /* enter the block */
2258 func->curblock = onfalse;
2261 cgen = self->on_false->expression.codegen;
2262 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &dummy))
2265 /* we now need to work from the current endpoint */
2266 onfalse_endblock = func->curblock;
2270 /* Merge block were they all merge in to */
2271 if (!ontrue || !onfalse || !ontrue_endblock->final || !onfalse_endblock->final)
2273 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "endif"));
2276 /* add jumps ot the merge block */
2277 if (ontrue && !ontrue_endblock->final && !ir_block_create_jump(ontrue_endblock, ast_ctx(self), merge))
2279 if (onfalse && !onfalse_endblock->final && !ir_block_create_jump(onfalse_endblock, ast_ctx(self), merge))
2282 /* Now enter the merge block */
2283 func->curblock = merge;
2286 /* we create the if here, that way all blocks are ordered :)
2288 if (!ir_block_create_if(cond, ast_ctx(self), condval,
2289 (ontrue ? ontrue : merge),
2290 (onfalse ? onfalse : merge)))
2298 bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_value **out)
2300 ast_expression_codegen *cgen;
2303 ir_value *trueval, *falseval;
2306 ir_block *cond = func->curblock;
2307 ir_block *cond_out = NULL;
2308 ir_block *ontrue, *ontrue_out = NULL;
2309 ir_block *onfalse, *onfalse_out = NULL;
2312 /* Ternary can never create an lvalue... */
2316 /* In theory it shouldn't be possible to pass through a node twice, but
2317 * in case we add any kind of optimization pass for the AST itself, it
2318 * may still happen, thus we remember a created ir_value and simply return one
2319 * if it already exists.
2321 if (self->expression.outr) {
2322 *out = self->expression.outr;
2326 /* In the following, contraty to ast_ifthen, we assume both paths exist. */
2328 /* generate the condition */
2329 func->curblock = cond;
2330 cgen = self->cond->expression.codegen;
2331 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2333 cond_out = func->curblock;
2335 /* create on-true block */
2336 ontrue = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_T"));
2341 /* enter the block */
2342 func->curblock = ontrue;
2345 cgen = self->on_true->expression.codegen;
2346 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &trueval))
2349 ontrue_out = func->curblock;
2352 /* create on-false block */
2353 onfalse = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_F"));
2358 /* enter the block */
2359 func->curblock = onfalse;
2362 cgen = self->on_false->expression.codegen;
2363 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &falseval))
2366 onfalse_out = func->curblock;
2369 /* create merge block */
2370 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_out"));
2373 /* jump to merge block */
2374 if (!ir_block_create_jump(ontrue_out, ast_ctx(self), merge))
2376 if (!ir_block_create_jump(onfalse_out, ast_ctx(self), merge))
2379 /* create if instruction */
2380 if (!ir_block_create_if(cond_out, ast_ctx(self), condval, ontrue, onfalse))
2383 /* Now enter the merge block */
2384 func->curblock = merge;
2386 /* Here, now, we need a PHI node
2387 * but first some sanity checking...
2389 if (trueval->vtype != falseval->vtype) {
2390 /* error("ternary with different types on the two sides"); */
2395 phi = ir_block_create_phi(merge, ast_ctx(self), ast_function_label(func, "phi"), trueval->vtype);
2398 ir_phi_add(phi, ontrue_out, trueval);
2399 ir_phi_add(phi, onfalse_out, falseval);
2401 self->expression.outr = ir_phi_value(phi);
2402 *out = self->expression.outr;
2404 codegen_output_type(self, *out);
2409 bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value **out)
2411 ast_expression_codegen *cgen;
2413 ir_value *dummy = NULL;
2414 ir_value *precond = NULL;
2415 ir_value *postcond = NULL;
2417 /* Since we insert some jumps "late" so we have blocks
2418 * ordered "nicely", we need to keep track of the actual end-blocks
2419 * of expressions to add the jumps to.
2421 ir_block *bbody = NULL, *end_bbody = NULL;
2422 ir_block *bprecond = NULL, *end_bprecond = NULL;
2423 ir_block *bpostcond = NULL, *end_bpostcond = NULL;
2424 ir_block *bincrement = NULL, *end_bincrement = NULL;
2425 ir_block *bout = NULL, *bin = NULL;
2427 /* let's at least move the outgoing block to the end */
2430 /* 'break' and 'continue' need to be able to find the right blocks */
2431 ir_block *bcontinue = NULL;
2432 ir_block *bbreak = NULL;
2434 ir_block *tmpblock = NULL;
2439 if (self->expression.outr) {
2440 compile_error(ast_ctx(self), "internal error: ast_loop cannot be reused, it bears no result!");
2443 self->expression.outr = (ir_value*)1;
2446 * Should we ever need some kind of block ordering, better make this function
2447 * move blocks around than write a block ordering algorithm later... after all
2448 * the ast and ir should work together, not against each other.
2451 /* initexpr doesn't get its own block, it's pointless, it could create more blocks
2452 * anyway if for example it contains a ternary.
2456 cgen = self->initexpr->expression.codegen;
2457 if (!(*cgen)((ast_expression*)(self->initexpr), func, false, &dummy))
2461 /* Store the block from which we enter this chaos */
2462 bin = func->curblock;
2464 /* The pre-loop condition needs its own block since we
2465 * need to be able to jump to the start of that expression.
2469 bprecond = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "pre_loop_cond"));
2473 /* the pre-loop-condition the least important place to 'continue' at */
2474 bcontinue = bprecond;
2477 func->curblock = bprecond;
2480 cgen = self->precond->expression.codegen;
2481 if (!(*cgen)((ast_expression*)(self->precond), func, false, &precond))
2484 end_bprecond = func->curblock;
2486 bprecond = end_bprecond = NULL;
2489 /* Now the next blocks won't be ordered nicely, but we need to
2490 * generate them this early for 'break' and 'continue'.
2492 if (self->increment) {
2493 bincrement = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "loop_increment"));
2496 bcontinue = bincrement; /* increment comes before the pre-loop-condition */
2498 bincrement = end_bincrement = NULL;
2501 if (self->postcond) {
2502 bpostcond = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "post_loop_cond"));
2505 bcontinue = bpostcond; /* postcond comes before the increment */
2507 bpostcond = end_bpostcond = NULL;
2510 bout_id = vec_size(func->ir_func->blocks);
2511 bout = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "after_loop"));
2516 /* The loop body... */
2517 /* if (self->body) */
2519 bbody = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "loop_body"));
2524 func->curblock = bbody;
2526 vec_push(func->breakblocks, bbreak);
2528 vec_push(func->continueblocks, bcontinue);
2530 vec_push(func->continueblocks, bbody);
2534 cgen = self->body->expression.codegen;
2535 if (!(*cgen)((ast_expression*)(self->body), func, false, &dummy))
2539 end_bbody = func->curblock;
2540 vec_pop(func->breakblocks);
2541 vec_pop(func->continueblocks);
2544 /* post-loop-condition */
2548 func->curblock = bpostcond;
2551 cgen = self->postcond->expression.codegen;
2552 if (!(*cgen)((ast_expression*)(self->postcond), func, false, &postcond))
2555 end_bpostcond = func->curblock;
2558 /* The incrementor */
2559 if (self->increment)
2562 func->curblock = bincrement;
2565 cgen = self->increment->expression.codegen;
2566 if (!(*cgen)((ast_expression*)(self->increment), func, false, &dummy))
2569 end_bincrement = func->curblock;
2572 /* In any case now, we continue from the outgoing block */
2573 func->curblock = bout;
2575 /* Now all blocks are in place */
2576 /* From 'bin' we jump to whatever comes first */
2577 if (bprecond) tmpblock = bprecond;
2578 else if (bbody) tmpblock = bbody;
2579 else if (bpostcond) tmpblock = bpostcond;
2580 else tmpblock = bout;
2581 if (!ir_block_create_jump(bin, ast_ctx(self), tmpblock))
2587 ir_block *ontrue, *onfalse;
2588 if (bbody) ontrue = bbody;
2589 else if (bincrement) ontrue = bincrement;
2590 else if (bpostcond) ontrue = bpostcond;
2591 else ontrue = bprecond;
2593 if (self->pre_not) {
2598 if (!ir_block_create_if(end_bprecond, ast_ctx(self), precond, ontrue, onfalse))
2605 if (bincrement) tmpblock = bincrement;
2606 else if (bpostcond) tmpblock = bpostcond;
2607 else if (bprecond) tmpblock = bprecond;
2608 else tmpblock = bbody;
2609 if (!end_bbody->final && !ir_block_create_jump(end_bbody, ast_ctx(self), tmpblock))
2613 /* from increment */
2616 if (bpostcond) tmpblock = bpostcond;
2617 else if (bprecond) tmpblock = bprecond;
2618 else if (bbody) tmpblock = bbody;
2619 else tmpblock = bout;
2620 if (!ir_block_create_jump(end_bincrement, ast_ctx(self), tmpblock))
2627 ir_block *ontrue, *onfalse;
2628 if (bprecond) ontrue = bprecond;
2629 else if (bbody) ontrue = bbody;
2630 else if (bincrement) ontrue = bincrement;
2631 else ontrue = bpostcond;
2633 if (self->post_not) {
2638 if (!ir_block_create_if(end_bpostcond, ast_ctx(self), postcond, ontrue, onfalse))
2642 /* Move 'bout' to the end */
2643 vec_remove(func->ir_func->blocks, bout_id, 1);
2644 vec_push(func->ir_func->blocks, bout);
2649 bool ast_breakcont_codegen(ast_breakcont *self, ast_function *func, bool lvalue, ir_value **out)
2656 compile_error(ast_ctx(self), "break/continue expression is not an l-value");
2660 if (self->expression.outr) {
2661 compile_error(ast_ctx(self), "internal error: ast_breakcont cannot be reused!");
2664 self->expression.outr = (ir_value*)1;
2666 if (self->is_continue)
2667 target = func->continueblocks[vec_size(func->continueblocks)-1-self->levels];
2669 target = func->breakblocks[vec_size(func->breakblocks)-1-self->levels];
2672 compile_error(ast_ctx(self), "%s is lacking a target block", (self->is_continue ? "continue" : "break"));
2676 if (!ir_block_create_jump(func->curblock, ast_ctx(self), target))
2681 bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_value **out)
2683 ast_expression_codegen *cgen;
2685 ast_switch_case *def_case = NULL;
2686 ir_block *def_bfall = NULL;
2687 ir_block *def_bfall_to = NULL;
2688 bool set_def_bfall_to = false;
2690 ir_value *dummy = NULL;
2691 ir_value *irop = NULL;
2692 ir_block *bout = NULL;
2693 ir_block *bfall = NULL;
2701 compile_error(ast_ctx(self), "switch expression is not an l-value");
2705 if (self->expression.outr) {
2706 compile_error(ast_ctx(self), "internal error: ast_switch cannot be reused!");
2709 self->expression.outr = (ir_value*)1;
2714 cgen = self->operand->expression.codegen;
2715 if (!(*cgen)((ast_expression*)(self->operand), func, false, &irop))
2718 if (!vec_size(self->cases))
2721 cmpinstr = type_eq_instr[irop->vtype];
2722 if (cmpinstr >= AINSTR_END) {
2723 ast_type_to_string(self->operand, typestr, sizeof(typestr));
2724 compile_error(ast_ctx(self), "invalid type to perform a switch on: %s", typestr);
2728 bout_id = vec_size(func->ir_func->blocks);
2729 bout = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "after_switch"));
2733 /* setup the break block */
2734 vec_push(func->breakblocks, bout);
2736 /* Now create all cases */
2737 for (c = 0; c < vec_size(self->cases); ++c) {
2738 ir_value *cond, *val;
2739 ir_block *bcase, *bnot;
2742 ast_switch_case *swcase = &self->cases[c];
2744 if (swcase->value) {
2745 /* A regular case */
2746 /* generate the condition operand */
2747 cgen = swcase->value->expression.codegen;
2748 if (!(*cgen)((ast_expression*)(swcase->value), func, false, &val))
2750 /* generate the condition */
2751 cond = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "switch_eq"), cmpinstr, irop, val);
2755 bcase = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "case"));
2756 bnot_id = vec_size(func->ir_func->blocks);
2757 bnot = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "not_case"));
2758 if (!bcase || !bnot)
2760 if (set_def_bfall_to) {
2761 set_def_bfall_to = false;
2762 def_bfall_to = bcase;
2764 if (!ir_block_create_if(func->curblock, ast_ctx(self), cond, bcase, bnot))
2767 /* Make the previous case-end fall through */
2768 if (bfall && !bfall->final) {
2769 if (!ir_block_create_jump(bfall, ast_ctx(self), bcase))
2773 /* enter the case */
2774 func->curblock = bcase;
2775 cgen = swcase->code->expression.codegen;
2776 if (!(*cgen)((ast_expression*)swcase->code, func, false, &dummy))
2779 /* remember this block to fall through from */
2780 bfall = func->curblock;
2782 /* enter the else and move it down */
2783 func->curblock = bnot;
2784 vec_remove(func->ir_func->blocks, bnot_id, 1);
2785 vec_push(func->ir_func->blocks, bnot);
2787 /* The default case */
2788 /* Remember where to fall through from: */
2791 /* remember which case it was */
2793 /* And the next case will be remembered */
2794 set_def_bfall_to = true;
2798 /* Jump from the last bnot to bout */
2799 if (bfall && !bfall->final && !ir_block_create_jump(bfall, ast_ctx(self), bout)) {
2801 astwarning(ast_ctx(bfall), WARN_???, "missing break after last case");
2806 /* If there was a default case, put it down here */
2810 /* No need to create an extra block */
2811 bcase = func->curblock;
2813 /* Insert the fallthrough jump */
2814 if (def_bfall && !def_bfall->final) {
2815 if (!ir_block_create_jump(def_bfall, ast_ctx(self), bcase))
2819 /* Now generate the default code */
2820 cgen = def_case->code->expression.codegen;
2821 if (!(*cgen)((ast_expression*)def_case->code, func, false, &dummy))
2824 /* see if we need to fall through */
2825 if (def_bfall_to && !func->curblock->final)
2827 if (!ir_block_create_jump(func->curblock, ast_ctx(self), def_bfall_to))
2832 /* Jump from the last bnot to bout */
2833 if (!func->curblock->final && !ir_block_create_jump(func->curblock, ast_ctx(self), bout))
2835 /* enter the outgoing block */
2836 func->curblock = bout;
2838 /* restore the break block */
2839 vec_pop(func->breakblocks);
2841 /* Move 'bout' to the end, it's nicer */
2842 vec_remove(func->ir_func->blocks, bout_id, 1);
2843 vec_push(func->ir_func->blocks, bout);
2848 bool ast_label_codegen(ast_label *self, ast_function *func, bool lvalue, ir_value **out)
2855 compile_error(ast_ctx(self), "internal error: ast_label cannot be an lvalue");
2859 /* simply create a new block and jump to it */
2860 self->irblock = ir_function_create_block(ast_ctx(self), func->ir_func, self->name);
2861 if (!self->irblock) {
2862 compile_error(ast_ctx(self), "failed to allocate label block `%s`", self->name);
2865 if (!func->curblock->final) {
2866 if (!ir_block_create_jump(func->curblock, ast_ctx(self), self->irblock))
2870 /* enter the new block */
2871 func->curblock = self->irblock;
2873 /* Generate all the leftover gotos */
2874 for (i = 0; i < vec_size(self->gotos); ++i) {
2875 if (!ast_goto_codegen(self->gotos[i], func, false, &dummy))
2882 bool ast_goto_codegen(ast_goto *self, ast_function *func, bool lvalue, ir_value **out)
2886 compile_error(ast_ctx(self), "internal error: ast_goto cannot be an lvalue");
2890 if (self->target->irblock) {
2891 if (self->irblock_from) {
2892 /* we already tried once, this is the callback */
2893 self->irblock_from->final = false;
2894 if (!ir_block_create_goto(self->irblock_from, ast_ctx(self), self->target->irblock)) {
2895 compile_error(ast_ctx(self), "failed to generate goto to `%s`", self->name);
2901 if (!ir_block_create_goto(func->curblock, ast_ctx(self), self->target->irblock)) {
2902 compile_error(ast_ctx(self), "failed to generate goto to `%s`", self->name);
2909 /* the target has not yet been created...
2910 * close this block in a sneaky way:
2912 func->curblock->final = true;
2913 self->irblock_from = func->curblock;
2914 ast_label_register_goto(self->target, self);
2920 bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value **out)
2922 ast_expression_codegen *cgen;
2924 ir_instr *callinstr;
2927 ir_value *funval = NULL;
2929 /* return values are never lvalues */
2931 compile_error(ast_ctx(self), "not an l-value (function call)");
2935 if (self->expression.outr) {
2936 *out = self->expression.outr;
2940 cgen = self->func->expression.codegen;
2941 if (!(*cgen)((ast_expression*)(self->func), func, false, &funval))
2949 for (i = 0; i < vec_size(self->params); ++i)
2952 ast_expression *expr = self->params[i];
2954 cgen = expr->expression.codegen;
2955 if (!(*cgen)(expr, func, false, ¶m))
2959 vec_push(params, param);
2962 callinstr = ir_block_create_call(func->curblock, ast_ctx(self),
2963 ast_function_label(func, "call"),
2964 funval, !!(self->func->expression.flags & AST_FLAG_NORETURN));
2968 for (i = 0; i < vec_size(params); ++i) {
2969 ir_call_param(callinstr, params[i]);
2972 *out = ir_call_value(callinstr);
2973 self->expression.outr = *out;
2975 codegen_output_type(self, *out);