2 * Copyright (C) 2012, 2013
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of
7 * this software and associated documentation files (the "Software"), to deal in
8 * the Software without restriction, including without limitation the rights to
9 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10 * of the Software, and to permit persons to whom the Software is furnished to do
11 * so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 #define ast_instantiate(T, ctx, destroyfn, iterfn) \
32 T* self = (T*)mem_a(sizeof(T)); \
36 ast_node_init((ast_node*)self, ctx, TYPE_##T); \
37 ( (ast_node*)self )->destroy = (ast_node_delete*)destroyfn; \
39 ( (ast_node*)self )->next_child = (ast_node_next_child*)iterfn; \
43 * forward declarations, these need not be in ast.h for obvious
46 static bool ast_member_codegen(ast_member*, ast_function*, bool lvalue, ir_value**);
47 static ast_node* ast_member_next_child(ast_member*,ast_node*);
48 static void ast_array_index_delete(ast_array_index*);
49 static bool ast_array_index_codegen(ast_array_index*, ast_function*, bool lvalue, ir_value**);
50 static ast_node* ast_array_index_next_child(ast_array_index*,ast_node*);
51 static void ast_argpipe_delete(ast_argpipe*);
52 static bool ast_argpipe_codegen(ast_argpipe*, ast_function*, bool lvalue, ir_value**);
53 static ast_node* ast_argpipe_next_child(ast_argpipe*,ast_node*);
54 static void ast_store_delete(ast_store*);
55 static bool ast_store_codegen(ast_store*, ast_function*, bool lvalue, ir_value**);
56 static ast_node* ast_store_next_child(ast_store*,ast_node*);
57 static void ast_ifthen_delete(ast_ifthen*);
58 static bool ast_ifthen_codegen(ast_ifthen*, ast_function*, bool lvalue, ir_value**);
59 static ast_node* ast_ifthen_next_child(ast_ifthen*,ast_node*);
60 static void ast_ternary_delete(ast_ternary*);
61 static bool ast_ternary_codegen(ast_ternary*, ast_function*, bool lvalue, ir_value**);
62 static ast_node* ast_ternary_next_child(ast_ternary*,ast_node*);
63 static void ast_loop_delete(ast_loop*);
64 static bool ast_loop_codegen(ast_loop*, ast_function*, bool lvalue, ir_value**);
65 static ast_node* ast_loop_next_child(ast_loop*,ast_node*);
66 static void ast_breakcont_delete(ast_breakcont*);
67 static bool ast_breakcont_codegen(ast_breakcont*, ast_function*, bool lvalue, ir_value**);
68 static ast_node* ast_breakcont_next_child(ast_breakcont*,ast_node*);
69 static void ast_switch_delete(ast_switch*);
70 static bool ast_switch_codegen(ast_switch*, ast_function*, bool lvalue, ir_value**);
71 static ast_node* ast_switch_next_child(ast_switch*,ast_node*);
72 static void ast_label_delete(ast_label*);
73 static void ast_label_register_goto(ast_label*, ast_goto*);
74 static bool ast_label_codegen(ast_label*, ast_function*, bool lvalue, ir_value**);
75 static ast_node* ast_label_next_child(ast_label*,ast_node*);
76 static bool ast_goto_codegen(ast_goto*, ast_function*, bool lvalue, ir_value**);
77 static void ast_goto_delete(ast_goto*);
78 static ast_node* ast_goto_next_child(ast_goto*,ast_node*);
79 static void ast_call_delete(ast_call*);
80 static bool ast_call_codegen(ast_call*, ast_function*, bool lvalue, ir_value**);
81 static ast_node* ast_call_next_child(ast_call*,ast_node*);
82 static bool ast_block_codegen(ast_block*, ast_function*, bool lvalue, ir_value**);
83 static ast_node* ast_block_next_child(ast_block*,ast_node*);
84 static void ast_unary_delete(ast_unary*);
85 static bool ast_unary_codegen(ast_unary*, ast_function*, bool lvalue, ir_value**);
86 static ast_node* ast_unary_next_child(ast_unary*,ast_node*);
87 static void ast_entfield_delete(ast_entfield*);
88 static bool ast_entfield_codegen(ast_entfield*, ast_function*, bool lvalue, ir_value**);
89 static ast_node* ast_entfield_next_child(ast_entfield*,ast_node*);
90 static void ast_return_delete(ast_return*);
91 static bool ast_return_codegen(ast_return*, ast_function*, bool lvalue, ir_value**);
92 static ast_node* ast_return_next_child(ast_return*,ast_node*);
93 static void ast_binstore_delete(ast_binstore*);
94 static bool ast_binstore_codegen(ast_binstore*, ast_function*, bool lvalue, ir_value**);
95 static ast_node* ast_binstore_next_child(ast_binstore*,ast_node*);
96 static void ast_binary_delete(ast_binary*);
97 static bool ast_binary_codegen(ast_binary*, ast_function*, bool lvalue, ir_value**);
98 static ast_node* ast_binary_next_child(ast_binary*,ast_node*);
99 static ast_node* ast_value_next_child(ast_value*,ast_node*);
100 static ast_node* ast_function_next_child(ast_function*,ast_node*);
102 /* It must not be possible to get here. */
103 static GMQCC_NORETURN void _ast_node_destroy(ast_node *self)
106 con_err("ast node missing destroy()\n");
110 static ast_node* _ast_node_next_child(ast_node *self, ast_node *prev)
117 /* Initialize main ast node aprts */
118 static void ast_node_init(ast_node *self, lex_ctx_t ctx, int nodetype)
121 self->destroy = &_ast_node_destroy;
122 self->next_child = &_ast_node_next_child;
124 self->nodetype = nodetype;
125 self->side_effects = false;
128 /* weight and side effects */
129 static void _ast_propagate_effects(ast_node *self, ast_node *other)
131 if (ast_side_effects(other))
132 ast_side_effects(self) = true;
134 #define ast_propagate_effects(s,o) _ast_propagate_effects(((ast_node*)(s)), ((ast_node*)(o)))
136 /* General expression initialization */
137 static void ast_expression_init(ast_expression *self,
138 ast_expression_codegen *codegen)
140 self->codegen = codegen;
141 self->vtype = TYPE_VOID;
148 self->varparam = NULL;
151 static void ast_expression_delete(ast_expression *self)
155 ast_delete(self->next);
156 for (i = 0; i < vec_size(self->params); ++i) {
157 ast_delete(self->params[i]);
159 vec_free(self->params);
161 ast_delete(self->varparam);
164 static void ast_expression_delete_full(ast_expression *self)
166 ast_expression_delete(self);
170 ast_value* ast_value_copy(const ast_value *self)
173 const ast_expression *fromex;
174 ast_expression *selfex;
175 ast_value *cp = ast_value_new(self->expression.node.context, self->name, self->expression.vtype);
176 if (self->expression.next) {
177 cp->expression.next = ast_type_copy(self->expression.node.context, self->expression.next);
179 fromex = &self->expression;
180 selfex = &cp->expression;
181 selfex->count = fromex->count;
182 selfex->flags = fromex->flags;
183 for (i = 0; i < vec_size(fromex->params); ++i) {
184 ast_value *v = ast_value_copy(fromex->params[i]);
185 vec_push(selfex->params, v);
190 void ast_type_adopt_impl(ast_expression *self, const ast_expression *other)
193 const ast_expression *fromex;
194 ast_expression *selfex;
195 self->vtype = other->vtype;
197 self->next = (ast_expression*)ast_type_copy(ast_ctx(self), other->next);
201 selfex->count = fromex->count;
202 selfex->flags = fromex->flags;
203 for (i = 0; i < vec_size(fromex->params); ++i) {
204 ast_value *v = ast_value_copy(fromex->params[i]);
205 vec_push(selfex->params, v);
209 static ast_expression* ast_shallow_type(lex_ctx_t ctx, int vtype)
211 ast_instantiate(ast_expression, ctx, ast_expression_delete_full, NULL);
212 ast_expression_init(self, NULL);
213 self->codegen = NULL;
219 ast_expression* ast_type_copy(lex_ctx_t ctx, const ast_expression *ex)
222 const ast_expression *fromex;
223 ast_expression *selfex;
229 ast_instantiate(ast_expression, ctx, ast_expression_delete_full, NULL);
230 ast_expression_init(self, NULL);
235 /* This may never be codegen()d */
236 selfex->codegen = NULL;
238 selfex->vtype = fromex->vtype;
240 selfex->next = ast_type_copy(ctx, fromex->next);
244 selfex->count = fromex->count;
245 selfex->flags = fromex->flags;
246 for (i = 0; i < vec_size(fromex->params); ++i) {
247 ast_value *v = ast_value_copy(fromex->params[i]);
248 vec_push(selfex->params, v);
255 bool ast_compare_type(ast_expression *a, ast_expression *b)
257 if (a->vtype == TYPE_NIL ||
258 b->vtype == TYPE_NIL)
260 if (a->vtype != b->vtype)
262 if (!a->next != !b->next)
264 if (vec_size(a->params) != vec_size(b->params))
266 if ((a->flags & AST_FLAG_TYPE_MASK) !=
267 (b->flags & AST_FLAG_TYPE_MASK) )
271 if (vec_size(a->params)) {
273 for (i = 0; i < vec_size(a->params); ++i) {
274 if (!ast_compare_type((ast_expression*)a->params[i],
275 (ast_expression*)b->params[i]))
280 return ast_compare_type(a->next, b->next);
284 static size_t ast_type_to_string_impl(ast_expression *e, char *buf, size_t bufsize, size_t pos)
291 if (pos + 6 >= bufsize)
293 util_strncpy(buf + pos, "(null)", 6);
297 if (pos + 1 >= bufsize)
302 util_strncpy(buf + pos, "(variant)", 9);
307 return ast_type_to_string_impl(e->next, buf, bufsize, pos);
310 if (pos + 3 >= bufsize)
314 pos = ast_type_to_string_impl(e->next, buf, bufsize, pos);
315 if (pos + 1 >= bufsize)
321 pos = ast_type_to_string_impl(e->next, buf, bufsize, pos);
322 if (pos + 2 >= bufsize)
324 if (!vec_size(e->params)) {
330 pos = ast_type_to_string_impl((ast_expression*)(e->params[0]), buf, bufsize, pos);
331 for (i = 1; i < vec_size(e->params); ++i) {
332 if (pos + 2 >= bufsize)
336 pos = ast_type_to_string_impl((ast_expression*)(e->params[i]), buf, bufsize, pos);
338 if (pos + 1 >= bufsize)
344 pos = ast_type_to_string_impl(e->next, buf, bufsize, pos);
345 if (pos + 1 >= bufsize)
348 pos += util_snprintf(buf + pos, bufsize - pos - 1, "%i", (int)e->count);
349 if (pos + 1 >= bufsize)
355 typestr = type_name[e->vtype];
356 typelen = strlen(typestr);
357 if (pos + typelen >= bufsize)
359 util_strncpy(buf + pos, typestr, typelen);
360 return pos + typelen;
364 buf[bufsize-3] = '.';
365 buf[bufsize-2] = '.';
366 buf[bufsize-1] = '.';
370 void ast_type_to_string(ast_expression *e, char *buf, size_t bufsize)
372 size_t pos = ast_type_to_string_impl(e, buf, bufsize-1, 0);
376 static bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out);
377 ast_value* ast_value_new(lex_ctx_t ctx, const char *name, int t)
379 ast_instantiate(ast_value, ctx, ast_value_delete, ast_value_next_child);
380 ast_expression_init((ast_expression*)self,
381 (ast_expression_codegen*)&ast_value_codegen);
382 self->expression.node.keep = true; /* keep */
384 self->name = name ? util_strdup(name) : NULL;
385 self->expression.vtype = t;
386 self->expression.next = NULL;
387 self->isfield = false;
389 self->hasvalue = false;
392 memset(&self->constval, 0, sizeof(self->constval));
393 self->initlist = NULL;
396 self->ir_values = NULL;
397 self->ir_value_count = 0;
403 self->argcounter = NULL;
404 self->intrinsic = false;
409 void ast_value_delete(ast_value* self)
412 mem_d((void*)self->name);
413 if (self->argcounter)
414 mem_d((void*)self->argcounter);
415 if (self->hasvalue) {
416 switch (self->expression.vtype)
419 mem_d((void*)self->constval.vstring);
422 /* unlink us from the function node */
423 self->constval.vfunc->vtype = NULL;
425 /* NOTE: delete function? currently collected in
426 * the parser structure
433 mem_d(self->ir_values);
438 if (self->initlist) {
439 if (self->expression.next->vtype == TYPE_STRING) {
440 /* strings are allocated, free them */
441 size_t i, len = vec_size(self->initlist);
442 /* in theory, len should be expression.count
443 * but let's not take any chances */
444 for (i = 0; i < len; ++i) {
445 if (self->initlist[i].vstring)
446 mem_d(self->initlist[i].vstring);
449 vec_free(self->initlist);
452 ast_expression_delete((ast_expression*)self);
456 void ast_value_params_add(ast_value *self, ast_value *p)
458 vec_push(self->expression.params, p);
461 bool ast_value_set_name(ast_value *self, const char *name)
464 mem_d((void*)self->name);
465 self->name = util_strdup(name);
469 ast_binary* ast_binary_new(lex_ctx_t ctx, int op,
470 ast_expression* left, ast_expression* right)
472 ast_instantiate(ast_binary, ctx, ast_binary_delete, ast_binary_next_child);
473 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binary_codegen);
478 self->right_first = false;
480 ast_propagate_effects(self, left);
481 ast_propagate_effects(self, right);
483 if (op >= INSTR_EQ_F && op <= INSTR_GT)
484 self->expression.vtype = TYPE_FLOAT;
485 else if (op == INSTR_AND || op == INSTR_OR) {
486 if (OPTS_FLAG(PERL_LOGIC))
487 ast_type_adopt(self, right);
489 self->expression.vtype = TYPE_FLOAT;
491 else if (op == INSTR_BITAND || op == INSTR_BITOR)
492 self->expression.vtype = TYPE_FLOAT;
493 else if (op == INSTR_MUL_VF || op == INSTR_MUL_FV)
494 self->expression.vtype = TYPE_VECTOR;
495 else if (op == INSTR_MUL_V)
496 self->expression.vtype = TYPE_FLOAT;
498 self->expression.vtype = left->vtype;
501 self->refs = AST_REF_ALL;
506 void ast_binary_delete(ast_binary *self)
508 if (self->refs & AST_REF_LEFT) ast_unref(self->left);
509 if (self->refs & AST_REF_RIGHT) ast_unref(self->right);
511 ast_expression_delete((ast_expression*)self);
515 ast_binstore* ast_binstore_new(lex_ctx_t ctx, int storop, int op,
516 ast_expression* left, ast_expression* right)
518 ast_instantiate(ast_binstore, ctx, ast_binstore_delete, ast_binstore_next_child);
519 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binstore_codegen);
521 ast_side_effects(self) = true;
523 self->opstore = storop;
526 self->source = right;
528 self->keep_dest = false;
530 ast_type_adopt(self, left);
534 void ast_binstore_delete(ast_binstore *self)
536 if (!self->keep_dest)
537 ast_unref(self->dest);
538 ast_unref(self->source);
539 ast_expression_delete((ast_expression*)self);
543 ast_unary* ast_unary_new(lex_ctx_t ctx, int op,
544 ast_expression *expr)
546 ast_instantiate(ast_unary, ctx, ast_unary_delete, ast_unary_next_child);
547 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_unary_codegen);
550 self->operand = expr;
553 if (ast_istype(expr, ast_unary) && OPTS_OPTIMIZATION(OPTIM_PEEPHOLE)) {
554 ast_unary *prev = (ast_unary*)((ast_unary*)expr)->operand;
556 /* Handle for double negation */
557 if (((ast_unary*)expr)->op == op)
558 prev = (ast_unary*)((ast_unary*)expr)->operand;
560 if (ast_istype(prev, ast_unary)) {
561 ast_expression_delete((ast_expression*)self);
563 ++opts_optimizationcount[OPTIM_PEEPHOLE];
568 ast_propagate_effects(self, expr);
570 if ((op >= INSTR_NOT_F && op <= INSTR_NOT_FNC) || op == VINSTR_NEG_F) {
571 self->expression.vtype = TYPE_FLOAT;
572 } else if (op == VINSTR_NEG_V) {
573 self->expression.vtype = TYPE_VECTOR;
575 compile_error(ctx, "cannot determine type of unary operation %s", util_instr_str[op]);
581 void ast_unary_delete(ast_unary *self)
583 if (self->operand) ast_unref(self->operand);
584 ast_expression_delete((ast_expression*)self);
588 ast_return* ast_return_new(lex_ctx_t ctx, ast_expression *expr)
590 ast_instantiate(ast_return, ctx, ast_return_delete, ast_return_next_child);
591 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_return_codegen);
593 self->operand = expr;
596 ast_propagate_effects(self, expr);
601 void ast_return_delete(ast_return *self)
604 ast_unref(self->operand);
605 ast_expression_delete((ast_expression*)self);
609 ast_entfield* ast_entfield_new(lex_ctx_t ctx, ast_expression *entity, ast_expression *field)
611 if (field->vtype != TYPE_FIELD) {
612 compile_error(ctx, "ast_entfield_new with expression not of type field");
615 return ast_entfield_new_force(ctx, entity, field, field->next);
618 ast_entfield* ast_entfield_new_force(lex_ctx_t ctx, ast_expression *entity, ast_expression *field, const ast_expression *outtype)
620 ast_instantiate(ast_entfield, ctx, ast_entfield_delete, ast_entfield_next_child);
624 /* Error: field has no type... */
628 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_entfield_codegen);
630 self->entity = entity;
632 ast_propagate_effects(self, entity);
633 ast_propagate_effects(self, field);
635 ast_type_adopt(self, outtype);
639 void ast_entfield_delete(ast_entfield *self)
641 ast_unref(self->entity);
642 ast_unref(self->field);
643 ast_expression_delete((ast_expression*)self);
647 ast_member* ast_member_new(lex_ctx_t ctx, ast_expression *owner, unsigned int field, const char *name)
649 ast_instantiate(ast_member, ctx, ast_member_delete, ast_member_next_child);
655 if (owner->vtype != TYPE_VECTOR &&
656 owner->vtype != TYPE_FIELD) {
657 compile_error(ctx, "member-access on an invalid owner of type %s", type_name[owner->vtype]);
662 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_member_codegen);
663 self->expression.node.keep = true; /* keep */
665 if (owner->vtype == TYPE_VECTOR) {
666 self->expression.vtype = TYPE_FLOAT;
667 self->expression.next = NULL;
669 self->expression.vtype = TYPE_FIELD;
670 self->expression.next = ast_shallow_type(ctx, TYPE_FLOAT);
673 self->rvalue = false;
675 ast_propagate_effects(self, owner);
679 self->name = util_strdup(name);
686 void ast_member_delete(ast_member *self)
688 /* The owner is always an ast_value, which has .keep=true,
689 * also: ast_members are usually deleted after the owner, thus
690 * this will cause invalid access
691 ast_unref(self->owner);
692 * once we allow (expression).x to access a vector-member, we need
693 * to change this: preferably by creating an alternate ast node for this
694 * purpose that is not garbage-collected.
696 ast_expression_delete((ast_expression*)self);
701 bool ast_member_set_name(ast_member *self, const char *name)
704 mem_d((void*)self->name);
705 self->name = util_strdup(name);
709 ast_array_index* ast_array_index_new(lex_ctx_t ctx, ast_expression *array, ast_expression *index)
711 ast_expression *outtype;
712 ast_instantiate(ast_array_index, ctx, ast_array_index_delete, ast_array_index_next_child);
714 outtype = array->next;
717 /* Error: field has no type... */
721 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_array_index_codegen);
725 ast_propagate_effects(self, array);
726 ast_propagate_effects(self, index);
728 ast_type_adopt(self, outtype);
729 if (array->vtype == TYPE_FIELD && outtype->vtype == TYPE_ARRAY) {
730 if (self->expression.vtype != TYPE_ARRAY) {
731 compile_error(ast_ctx(self), "array_index node on type");
732 ast_array_index_delete(self);
735 self->array = outtype;
736 self->expression.vtype = TYPE_FIELD;
742 void ast_array_index_delete(ast_array_index *self)
745 ast_unref(self->array);
747 ast_unref(self->index);
748 ast_expression_delete((ast_expression*)self);
752 ast_argpipe* ast_argpipe_new(lex_ctx_t ctx, ast_expression *index)
754 ast_instantiate(ast_argpipe, ctx, ast_argpipe_delete, ast_argpipe_next_child);
755 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_argpipe_codegen);
757 self->expression.vtype = TYPE_NOEXPR;
761 void ast_argpipe_delete(ast_argpipe *self)
764 ast_unref(self->index);
765 ast_expression_delete((ast_expression*)self);
769 ast_ifthen* ast_ifthen_new(lex_ctx_t ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
771 ast_instantiate(ast_ifthen, ctx, ast_ifthen_delete, ast_ifthen_next_child);
772 if (!ontrue && !onfalse) {
773 /* because it is invalid */
777 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ifthen_codegen);
780 self->on_true = ontrue;
781 self->on_false = onfalse;
782 ast_propagate_effects(self, cond);
784 ast_propagate_effects(self, ontrue);
786 ast_propagate_effects(self, onfalse);
791 void ast_ifthen_delete(ast_ifthen *self)
793 ast_unref(self->cond);
795 ast_unref(self->on_true);
797 ast_unref(self->on_false);
798 ast_expression_delete((ast_expression*)self);
802 ast_ternary* ast_ternary_new(lex_ctx_t ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
804 ast_expression *exprtype = ontrue;
805 ast_instantiate(ast_ternary, ctx, ast_ternary_delete, ast_ternary_next_child);
806 /* This time NEITHER must be NULL */
807 if (!ontrue || !onfalse) {
811 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ternary_codegen);
814 self->on_true = ontrue;
815 self->on_false = onfalse;
816 ast_propagate_effects(self, cond);
817 ast_propagate_effects(self, ontrue);
818 ast_propagate_effects(self, onfalse);
820 if (ontrue->vtype == TYPE_NIL)
822 ast_type_adopt(self, exprtype);
827 void ast_ternary_delete(ast_ternary *self)
829 /* the if()s are only there because computed-gotos can set them
832 if (self->cond) ast_unref(self->cond);
833 if (self->on_true) ast_unref(self->on_true);
834 if (self->on_false) ast_unref(self->on_false);
835 ast_expression_delete((ast_expression*)self);
839 ast_loop* ast_loop_new(lex_ctx_t ctx,
840 ast_expression *initexpr,
841 ast_expression *precond, bool pre_not,
842 ast_expression *postcond, bool post_not,
843 ast_expression *increment,
844 ast_expression *body)
846 ast_instantiate(ast_loop, ctx, ast_loop_delete, ast_loop_next_child);
847 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_loop_codegen);
849 self->initexpr = initexpr;
850 self->precond = precond;
851 self->postcond = postcond;
852 self->increment = increment;
855 self->pre_not = pre_not;
856 self->post_not = post_not;
859 ast_propagate_effects(self, initexpr);
861 ast_propagate_effects(self, precond);
863 ast_propagate_effects(self, postcond);
865 ast_propagate_effects(self, increment);
867 ast_propagate_effects(self, body);
872 void ast_loop_delete(ast_loop *self)
875 ast_unref(self->initexpr);
877 ast_unref(self->precond);
879 ast_unref(self->postcond);
881 ast_unref(self->increment);
883 ast_unref(self->body);
884 ast_expression_delete((ast_expression*)self);
888 ast_breakcont* ast_breakcont_new(lex_ctx_t ctx, bool iscont, unsigned int levels)
890 ast_instantiate(ast_breakcont, ctx, ast_breakcont_delete, ast_breakcont_next_child);
891 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_breakcont_codegen);
893 self->is_continue = iscont;
894 self->levels = levels;
899 void ast_breakcont_delete(ast_breakcont *self)
901 ast_expression_delete((ast_expression*)self);
905 ast_switch* ast_switch_new(lex_ctx_t ctx, ast_expression *op)
907 ast_instantiate(ast_switch, ctx, ast_switch_delete, ast_switch_next_child);
908 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_switch_codegen);
913 ast_propagate_effects(self, op);
918 void ast_switch_delete(ast_switch *self)
921 ast_unref(self->operand);
923 for (i = 0; i < vec_size(self->cases); ++i) {
924 if (self->cases[i].value)
925 ast_unref(self->cases[i].value);
926 ast_unref(self->cases[i].code);
928 vec_free(self->cases);
930 ast_expression_delete((ast_expression*)self);
934 ast_label* ast_label_new(lex_ctx_t ctx, const char *name, bool undefined)
936 ast_instantiate(ast_label, ctx, ast_label_delete, ast_label_next_child);
937 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_label_codegen);
939 self->expression.vtype = TYPE_NOEXPR;
941 self->name = util_strdup(name);
942 self->irblock = NULL;
944 self->undefined = undefined;
949 void ast_label_delete(ast_label *self)
951 mem_d((void*)self->name);
952 vec_free(self->gotos);
953 ast_expression_delete((ast_expression*)self);
957 static void ast_label_register_goto(ast_label *self, ast_goto *g)
959 vec_push(self->gotos, g);
962 ast_goto* ast_goto_new(lex_ctx_t ctx, const char *name)
964 ast_instantiate(ast_goto, ctx, ast_goto_delete, ast_goto_next_child);
965 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_goto_codegen);
967 self->name = util_strdup(name);
969 self->irblock_from = NULL;
974 void ast_goto_delete(ast_goto *self)
976 mem_d((void*)self->name);
977 ast_expression_delete((ast_expression*)self);
981 void ast_goto_set_label(ast_goto *self, ast_label *label)
983 self->target = label;
986 ast_call* ast_call_new(lex_ctx_t ctx,
987 ast_expression *funcexpr)
989 ast_instantiate(ast_call, ctx, ast_call_delete, ast_call_next_child);
990 if (!funcexpr->next) {
991 compile_error(ctx, "not a function");
995 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_call_codegen);
997 ast_side_effects(self) = true;
1000 self->func = funcexpr;
1001 self->va_count = NULL;
1003 ast_type_adopt(self, funcexpr->next);
1008 void ast_call_delete(ast_call *self)
1011 for (i = 0; i < vec_size(self->params); ++i)
1012 ast_unref(self->params[i]);
1013 vec_free(self->params);
1016 ast_unref(self->func);
1019 ast_unref(self->va_count);
1021 ast_expression_delete((ast_expression*)self);
1025 static bool ast_call_check_vararg(ast_call *self, ast_expression *va_type, ast_expression *exp_type)
1031 if (!va_type || !ast_compare_type(va_type, exp_type))
1033 if (va_type && exp_type)
1035 ast_type_to_string(va_type, tgot, sizeof(tgot));
1036 ast_type_to_string(exp_type, texp, sizeof(texp));
1037 if (OPTS_FLAG(UNSAFE_VARARGS)) {
1038 if (compile_warning(ast_ctx(self), WARN_UNSAFE_TYPES,
1039 "piped variadic argument differs in type: constrained to type %s, expected type %s",
1043 compile_error(ast_ctx(self),
1044 "piped variadic argument differs in type: constrained to type %s, expected type %s",
1051 ast_type_to_string(exp_type, texp, sizeof(texp));
1052 if (OPTS_FLAG(UNSAFE_VARARGS)) {
1053 if (compile_warning(ast_ctx(self), WARN_UNSAFE_TYPES,
1054 "piped variadic argument may differ in type: expected type %s",
1058 compile_error(ast_ctx(self),
1059 "piped variadic argument may differ in type: expected type %s",
1068 bool ast_call_check_types(ast_call *self, ast_expression *va_type)
1074 const ast_expression *func = self->func;
1075 size_t count = vec_size(self->params);
1076 if (count > vec_size(func->params))
1077 count = vec_size(func->params);
1079 for (i = 0; i < count; ++i) {
1080 if (ast_istype(self->params[i], ast_argpipe)) {
1081 /* warn about type safety instead */
1083 compile_error(ast_ctx(self), "argpipe must be the last parameter to a function call");
1086 if (!ast_call_check_vararg(self, va_type, (ast_expression*)func->params[i]))
1089 else if (!ast_compare_type(self->params[i], (ast_expression*)(func->params[i])))
1091 ast_type_to_string(self->params[i], tgot, sizeof(tgot));
1092 ast_type_to_string((ast_expression*)func->params[i], texp, sizeof(texp));
1093 compile_error(ast_ctx(self), "invalid type for parameter %u in function call: expected %s, got %s",
1094 (unsigned int)(i+1), texp, tgot);
1095 /* we don't immediately return */
1099 count = vec_size(self->params);
1100 if (count > vec_size(func->params) && func->varparam) {
1101 for (; i < count; ++i) {
1102 if (ast_istype(self->params[i], ast_argpipe)) {
1103 /* warn about type safety instead */
1105 compile_error(ast_ctx(self), "argpipe must be the last parameter to a function call");
1108 if (!ast_call_check_vararg(self, va_type, func->varparam))
1111 else if (!ast_compare_type(self->params[i], func->varparam))
1113 ast_type_to_string(self->params[i], tgot, sizeof(tgot));
1114 ast_type_to_string(func->varparam, texp, sizeof(texp));
1115 compile_error(ast_ctx(self), "invalid type for variadic parameter %u in function call: expected %s, got %s",
1116 (unsigned int)(i+1), texp, tgot);
1117 /* we don't immediately return */
1125 ast_store* ast_store_new(lex_ctx_t ctx, int op,
1126 ast_expression *dest, ast_expression *source)
1128 ast_instantiate(ast_store, ctx, ast_store_delete, ast_store_next_child);
1129 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_store_codegen);
1131 ast_side_effects(self) = true;
1135 self->source = source;
1137 ast_type_adopt(self, dest);
1142 void ast_store_delete(ast_store *self)
1144 ast_unref(self->dest);
1145 ast_unref(self->source);
1146 ast_expression_delete((ast_expression*)self);
1150 ast_block* ast_block_new(lex_ctx_t ctx)
1152 ast_instantiate(ast_block, ctx, ast_block_delete, ast_block_next_child);
1153 ast_expression_init((ast_expression*)self,
1154 (ast_expression_codegen*)&ast_block_codegen);
1156 self->locals = NULL;
1158 self->collect = NULL;
1163 bool ast_block_add_expr(ast_block *self, ast_expression *e)
1165 ast_propagate_effects(self, e);
1166 vec_push(self->exprs, e);
1167 if (self->expression.next) {
1168 ast_delete(self->expression.next);
1169 self->expression.next = NULL;
1171 ast_type_adopt(self, e);
1175 void ast_block_collect(ast_block *self, ast_expression *expr)
1177 vec_push(self->collect, expr);
1178 expr->node.keep = true;
1181 void ast_block_delete(ast_block *self)
1184 for (i = 0; i < vec_size(self->exprs); ++i)
1185 ast_unref(self->exprs[i]);
1186 vec_free(self->exprs);
1187 for (i = 0; i < vec_size(self->locals); ++i)
1188 ast_delete(self->locals[i]);
1189 vec_free(self->locals);
1190 for (i = 0; i < vec_size(self->collect); ++i)
1191 ast_delete(self->collect[i]);
1192 vec_free(self->collect);
1193 ast_expression_delete((ast_expression*)self);
1197 void ast_block_set_type(ast_block *self, ast_expression *from)
1199 if (self->expression.next)
1200 ast_delete(self->expression.next);
1201 ast_type_adopt(self, from);
1204 ast_function* ast_function_new(lex_ctx_t ctx, const char *name, ast_value *vtype)
1206 ast_instantiate(ast_function, ctx, ast_function_delete, ast_function_next_child);
1209 compile_error(ast_ctx(self), "internal error: ast_function_new condition 0");
1211 } else if (vtype->hasvalue || vtype->expression.vtype != TYPE_FUNCTION) {
1212 } else if (vtype->hasvalue || vtype->expression.vtype != TYPE_FUNCTION) {
1213 compile_error(ast_ctx(self), "internal error: ast_function_new condition %i %i type=%i (probably 2 bodies?)",
1215 (int)vtype->hasvalue,
1216 vtype->expression.vtype);
1220 self->vtype = vtype;
1221 self->name = name ? util_strdup(name) : NULL;
1222 self->blocks = NULL;
1224 self->labelcount = 0;
1227 self->ir_func = NULL;
1228 self->curblock = NULL;
1230 self->breakblocks = NULL;
1231 self->continueblocks = NULL;
1233 vtype->hasvalue = true;
1234 vtype->constval.vfunc = self;
1236 self->varargs = NULL;
1238 self->fixedparams = NULL;
1239 self->return_value = NULL;
1248 void ast_function_delete(ast_function *self)
1252 mem_d((void*)self->name);
1254 /* ast_value_delete(self->vtype); */
1255 self->vtype->hasvalue = false;
1256 self->vtype->constval.vfunc = NULL;
1257 /* We use unref - if it was stored in a global table it is supposed
1258 * to be deleted from *there*
1260 ast_unref(self->vtype);
1262 for (i = 0; i < vec_size(self->blocks); ++i)
1263 ast_delete(self->blocks[i]);
1264 vec_free(self->blocks);
1265 vec_free(self->breakblocks);
1266 vec_free(self->continueblocks);
1268 ast_delete(self->varargs);
1270 ast_delete(self->argc);
1271 if (self->fixedparams)
1272 ast_unref(self->fixedparams);
1273 if (self->return_value)
1274 ast_unref(self->return_value);
1278 const char* ast_function_label(ast_function *self, const char *prefix)
1284 if (!OPTS_OPTION_BOOL(OPTION_DUMP) &&
1285 !OPTS_OPTION_BOOL(OPTION_DUMPFIN) &&
1286 !OPTS_OPTION_BOOL(OPTION_DEBUG))
1291 id = (self->labelcount++);
1292 len = strlen(prefix);
1294 from = self->labelbuf + sizeof(self->labelbuf)-1;
1297 *from-- = (id%10) + '0';
1301 memcpy(from - len, prefix, len);
1305 /*********************************************************************/
1307 * by convention you must never pass NULL to the 'ir_value **out'
1308 * parameter. If you really don't care about the output, pass a dummy.
1309 * But I can't imagine a pituation where the output is truly unnecessary.
1312 static void _ast_codegen_output_type(ast_expression *self, ir_value *out)
1314 if (out->vtype == TYPE_FIELD)
1315 out->fieldtype = self->next->vtype;
1316 if (out->vtype == TYPE_FUNCTION)
1317 out->outtype = self->next->vtype;
1320 #define codegen_output_type(a,o) (_ast_codegen_output_type(&((a)->expression),(o)))
1322 bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out)
1326 if (self->expression.vtype == TYPE_NIL) {
1327 *out = func->ir_func->owner->nil;
1330 /* NOTE: This is the codegen for a variable used in an expression.
1331 * It is not the codegen to generate the value. For this purpose,
1332 * ast_local_codegen and ast_global_codegen are to be used before this
1333 * is executed. ast_function_codegen should take care of its locals,
1334 * and the ast-user should take care of ast_global_codegen to be used
1335 * on all the globals.
1338 char tname[1024]; /* typename is reserved in C++ */
1339 ast_type_to_string((ast_expression*)self, tname, sizeof(tname));
1340 compile_error(ast_ctx(self), "ast_value used before generated %s %s", tname, self->name);
1347 static bool ast_global_array_set(ast_value *self)
1349 size_t count = vec_size(self->initlist);
1352 if (count > self->expression.count) {
1353 compile_error(ast_ctx(self), "too many elements in initializer");
1354 count = self->expression.count;
1356 else if (count < self->expression.count) {
1358 compile_warning(ast_ctx(self), "not all elements are initialized");
1362 for (i = 0; i != count; ++i) {
1363 switch (self->expression.next->vtype) {
1365 if (!ir_value_set_float(self->ir_values[i], self->initlist[i].vfloat))
1369 if (!ir_value_set_vector(self->ir_values[i], self->initlist[i].vvec))
1373 if (!ir_value_set_string(self->ir_values[i], self->initlist[i].vstring))
1377 /* we don't support them in any other place yet either */
1378 compile_error(ast_ctx(self), "TODO: nested arrays");
1381 /* this requiers a bit more work - similar to the fields I suppose */
1382 compile_error(ast_ctx(self), "global of type function not properly generated");
1385 if (!self->initlist[i].vfield) {
1386 compile_error(ast_ctx(self), "field constant without vfield set");
1389 if (!self->initlist[i].vfield->ir_v) {
1390 compile_error(ast_ctx(self), "field constant generated before its field");
1393 if (!ir_value_set_field(self->ir_values[i], self->initlist[i].vfield->ir_v))
1397 compile_error(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1404 static bool check_array(ast_value *self, ast_value *array)
1406 if (array->expression.flags & AST_FLAG_ARRAY_INIT && !array->initlist) {
1407 compile_error(ast_ctx(self), "array without size: %s", self->name);
1410 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1411 if (!array->expression.count || array->expression.count > OPTS_OPTION_U32(OPTION_MAX_ARRAY_SIZE)) {
1412 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)array->expression.count);
1418 bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
1422 if (self->expression.vtype == TYPE_NIL) {
1423 compile_error(ast_ctx(self), "internal error: trying to generate a variable of TYPE_NIL");
1427 if (self->hasvalue && self->expression.vtype == TYPE_FUNCTION)
1429 ir_function *func = ir_builder_create_function(ir, self->name, self->expression.next->vtype);
1432 func->context = ast_ctx(self);
1433 func->value->context = ast_ctx(self);
1435 self->constval.vfunc->ir_func = func;
1436 self->ir_v = func->value;
1437 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1438 self->ir_v->flags |= IR_FLAG_INCLUDE_DEF;
1439 if (self->expression.flags & AST_FLAG_ERASEABLE)
1440 self->ir_v->flags |= IR_FLAG_ERASEABLE;
1441 /* The function is filled later on ast_function_codegen... */
1445 if (isfield && self->expression.vtype == TYPE_FIELD) {
1446 ast_expression *fieldtype = self->expression.next;
1448 if (self->hasvalue) {
1449 compile_error(ast_ctx(self), "TODO: constant field pointers with value");
1453 if (fieldtype->vtype == TYPE_ARRAY) {
1458 ast_expression *elemtype;
1460 ast_value *array = (ast_value*)fieldtype;
1462 if (!ast_istype(fieldtype, ast_value)) {
1463 compile_error(ast_ctx(self), "internal error: ast_value required");
1467 if (!check_array(self, array))
1470 elemtype = array->expression.next;
1471 vtype = elemtype->vtype;
1473 v = ir_builder_create_field(ir, self->name, vtype);
1475 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", self->name);
1478 v->context = ast_ctx(self);
1479 v->unique_life = true;
1481 array->ir_v = self->ir_v = v;
1483 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1484 self->ir_v->flags |= IR_FLAG_INCLUDE_DEF;
1485 if (self->expression.flags & AST_FLAG_ERASEABLE)
1486 self->ir_v->flags |= IR_FLAG_ERASEABLE;
1488 namelen = strlen(self->name);
1489 name = (char*)mem_a(namelen + 16);
1490 util_strncpy(name, self->name, namelen);
1492 array->ir_values = (ir_value**)mem_a(sizeof(array->ir_values[0]) * array->expression.count);
1493 array->ir_values[0] = v;
1494 for (ai = 1; ai < array->expression.count; ++ai) {
1495 util_snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1496 array->ir_values[ai] = ir_builder_create_field(ir, name, vtype);
1497 if (!array->ir_values[ai]) {
1499 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", name);
1502 array->ir_values[ai]->context = ast_ctx(self);
1503 array->ir_values[ai]->unique_life = true;
1504 array->ir_values[ai]->locked = true;
1505 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1506 self->ir_values[ai]->flags |= IR_FLAG_INCLUDE_DEF;
1512 v = ir_builder_create_field(ir, self->name, self->expression.next->vtype);
1515 v->context = ast_ctx(self);
1517 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1518 self->ir_v->flags |= IR_FLAG_INCLUDE_DEF;
1520 if (self->expression.flags & AST_FLAG_ERASEABLE)
1521 self->ir_v->flags |= IR_FLAG_ERASEABLE;
1526 if (self->expression.vtype == TYPE_ARRAY) {
1531 ast_expression *elemtype = self->expression.next;
1532 int vtype = elemtype->vtype;
1534 if (self->expression.flags & AST_FLAG_ARRAY_INIT && !self->expression.count) {
1535 compile_error(ast_ctx(self), "array `%s' has no size", self->name);
1539 /* same as with field arrays */
1540 if (!check_array(self, self))
1543 v = ir_builder_create_global(ir, self->name, vtype);
1545 compile_error(ast_ctx(self), "ir_builder_create_global failed `%s`", self->name);
1548 v->context = ast_ctx(self);
1549 v->unique_life = true;
1552 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1553 v->flags |= IR_FLAG_INCLUDE_DEF;
1554 if (self->expression.flags & AST_FLAG_ERASEABLE)
1555 self->ir_v->flags |= IR_FLAG_ERASEABLE;
1557 namelen = strlen(self->name);
1558 name = (char*)mem_a(namelen + 16);
1559 util_strncpy(name, self->name, namelen);
1561 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1562 self->ir_values[0] = v;
1563 for (ai = 1; ai < self->expression.count; ++ai) {
1564 util_snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1565 self->ir_values[ai] = ir_builder_create_global(ir, name, vtype);
1566 if (!self->ir_values[ai]) {
1568 compile_error(ast_ctx(self), "ir_builder_create_global failed `%s`", name);
1571 self->ir_values[ai]->context = ast_ctx(self);
1572 self->ir_values[ai]->unique_life = true;
1573 self->ir_values[ai]->locked = true;
1574 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1575 self->ir_values[ai]->flags |= IR_FLAG_INCLUDE_DEF;
1581 /* Arrays don't do this since there's no "array" value which spans across the
1584 v = ir_builder_create_global(ir, self->name, self->expression.vtype);
1586 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", self->name);
1589 codegen_output_type(self, v);
1590 v->context = ast_ctx(self);
1593 /* link us to the ir_value */
1597 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1598 self->ir_v->flags |= IR_FLAG_INCLUDE_DEF;
1599 if (self->expression.flags & AST_FLAG_ERASEABLE)
1600 self->ir_v->flags |= IR_FLAG_ERASEABLE;
1603 if (self->hasvalue) {
1604 switch (self->expression.vtype)
1607 if (!ir_value_set_float(v, self->constval.vfloat))
1611 if (!ir_value_set_vector(v, self->constval.vvec))
1615 if (!ir_value_set_string(v, self->constval.vstring))
1619 ast_global_array_set(self);
1622 compile_error(ast_ctx(self), "global of type function not properly generated");
1624 /* Cannot generate an IR value for a function,
1625 * need a pointer pointing to a function rather.
1628 if (!self->constval.vfield) {
1629 compile_error(ast_ctx(self), "field constant without vfield set");
1632 if (!self->constval.vfield->ir_v) {
1633 compile_error(ast_ctx(self), "field constant generated before its field");
1636 if (!ir_value_set_field(v, self->constval.vfield->ir_v))
1640 compile_error(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1646 error: /* clean up */
1647 if(v) ir_value_delete(v);
1651 static bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
1655 if (self->expression.vtype == TYPE_NIL) {
1656 compile_error(ast_ctx(self), "internal error: trying to generate a variable of TYPE_NIL");
1660 if (self->hasvalue && self->expression.vtype == TYPE_FUNCTION)
1662 /* Do we allow local functions? I think not...
1663 * this is NOT a function pointer atm.
1668 if (self->expression.vtype == TYPE_ARRAY) {
1673 ast_expression *elemtype = self->expression.next;
1674 int vtype = elemtype->vtype;
1676 func->flags |= IR_FLAG_HAS_ARRAYS;
1678 if (param && !(self->expression.flags & AST_FLAG_IS_VARARG)) {
1679 compile_error(ast_ctx(self), "array-parameters are not supported");
1683 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1684 if (!check_array(self, self))
1687 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1688 if (!self->ir_values) {
1689 compile_error(ast_ctx(self), "failed to allocate array values");
1693 v = ir_function_create_local(func, self->name, vtype, param);
1695 compile_error(ast_ctx(self), "internal error: ir_function_create_local failed");
1698 v->context = ast_ctx(self);
1699 v->unique_life = true;
1702 namelen = strlen(self->name);
1703 name = (char*)mem_a(namelen + 16);
1704 util_strncpy(name, self->name, namelen);
1706 self->ir_values[0] = v;
1707 for (ai = 1; ai < self->expression.count; ++ai) {
1708 util_snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1709 self->ir_values[ai] = ir_function_create_local(func, name, vtype, param);
1710 if (!self->ir_values[ai]) {
1711 compile_error(ast_ctx(self), "internal_error: ir_builder_create_global failed on `%s`", name);
1714 self->ir_values[ai]->context = ast_ctx(self);
1715 self->ir_values[ai]->unique_life = true;
1716 self->ir_values[ai]->locked = true;
1722 v = ir_function_create_local(func, self->name, self->expression.vtype, param);
1725 codegen_output_type(self, v);
1726 v->context = ast_ctx(self);
1729 /* A constant local... hmmm...
1730 * I suppose the IR will have to deal with this
1732 if (self->hasvalue) {
1733 switch (self->expression.vtype)
1736 if (!ir_value_set_float(v, self->constval.vfloat))
1740 if (!ir_value_set_vector(v, self->constval.vvec))
1744 if (!ir_value_set_string(v, self->constval.vstring))
1748 compile_error(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1753 /* link us to the ir_value */
1757 if (!ast_generate_accessors(self, func->owner))
1761 error: /* clean up */
1766 bool ast_generate_accessors(ast_value *self, ir_builder *ir)
1769 bool warn = OPTS_WARN(WARN_USED_UNINITIALIZED);
1770 if (!self->setter || !self->getter)
1772 for (i = 0; i < self->expression.count; ++i) {
1773 if (!self->ir_values) {
1774 compile_error(ast_ctx(self), "internal error: no array values generated for `%s`", self->name);
1777 if (!self->ir_values[i]) {
1778 compile_error(ast_ctx(self), "internal error: not all array values have been generated for `%s`", self->name);
1781 if (self->ir_values[i]->life) {
1782 compile_error(ast_ctx(self), "internal error: function containing `%s` already generated", self->name);
1787 opts_set(opts.warn, WARN_USED_UNINITIALIZED, false);
1789 if (!ast_global_codegen (self->setter, ir, false) ||
1790 !ast_function_codegen(self->setter->constval.vfunc, ir) ||
1791 !ir_function_finalize(self->setter->constval.vfunc->ir_func))
1793 compile_error(ast_ctx(self), "internal error: failed to generate setter for `%s`", self->name);
1794 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1799 if (!ast_global_codegen (self->getter, ir, false) ||
1800 !ast_function_codegen(self->getter->constval.vfunc, ir) ||
1801 !ir_function_finalize(self->getter->constval.vfunc->ir_func))
1803 compile_error(ast_ctx(self), "internal error: failed to generate getter for `%s`", self->name);
1804 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1808 for (i = 0; i < self->expression.count; ++i) {
1809 vec_free(self->ir_values[i]->life);
1811 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1815 bool ast_function_codegen(ast_function *self, ir_builder *ir)
1820 ast_expression_codegen *cgen;
1826 irf = self->ir_func;
1828 compile_error(ast_ctx(self), "internal error: ast_function's related ast_value was not generated yet");
1832 /* fill the parameter list */
1833 ec = &self->vtype->expression;
1834 for (i = 0; i < vec_size(ec->params); ++i)
1836 if (ec->params[i]->expression.vtype == TYPE_FIELD)
1837 vec_push(irf->params, ec->params[i]->expression.next->vtype);
1839 vec_push(irf->params, ec->params[i]->expression.vtype);
1840 if (!self->builtin) {
1841 if (!ast_local_codegen(ec->params[i], self->ir_func, true))
1846 if (self->varargs) {
1847 if (!ast_local_codegen(self->varargs, self->ir_func, true))
1849 irf->max_varargs = self->varargs->expression.count;
1852 if (self->builtin) {
1853 irf->builtin = self->builtin;
1857 /* have a local return value variable? */
1858 if (self->return_value) {
1859 if (!ast_local_codegen(self->return_value, self->ir_func, false))
1863 if (!vec_size(self->blocks)) {
1864 compile_error(ast_ctx(self), "function `%s` has no body", self->name);
1868 irf->first = self->curblock = ir_function_create_block(ast_ctx(self), irf, "entry");
1869 if (!self->curblock) {
1870 compile_error(ast_ctx(self), "failed to allocate entry block for `%s`", self->name);
1878 if (!ast_local_codegen(self->argc, self->ir_func, true))
1880 cgen = self->argc->expression.codegen;
1881 if (!(*cgen)((ast_expression*)(self->argc), self, false, &va_count))
1883 cgen = self->fixedparams->expression.codegen;
1884 if (!(*cgen)((ast_expression*)(self->fixedparams), self, false, &fixed))
1886 sub = ir_block_create_binop(self->curblock, ast_ctx(self),
1887 ast_function_label(self, "va_count"), INSTR_SUB_F,
1888 ir_builder_get_va_count(ir), fixed);
1891 if (!ir_block_create_store_op(self->curblock, ast_ctx(self), INSTR_STORE_F,
1898 for (i = 0; i < vec_size(self->blocks); ++i) {
1899 cgen = self->blocks[i]->expression.codegen;
1900 if (!(*cgen)((ast_expression*)self->blocks[i], self, false, &dummy))
1904 /* TODO: check return types */
1905 if (!self->curblock->final)
1907 if (!self->vtype->expression.next ||
1908 self->vtype->expression.next->vtype == TYPE_VOID)
1910 return ir_block_create_return(self->curblock, ast_ctx(self), NULL);
1912 else if (vec_size(self->curblock->entries) || self->curblock == irf->first)
1914 if (self->return_value) {
1915 cgen = self->return_value->expression.codegen;
1916 if (!(*cgen)((ast_expression*)(self->return_value), self, false, &dummy))
1918 return ir_block_create_return(self->curblock, ast_ctx(self), dummy);
1920 else if (compile_warning(ast_ctx(self), WARN_MISSING_RETURN_VALUES,
1921 "control reaches end of non-void function (`%s`) via %s",
1922 self->name, self->curblock->label))
1926 return ir_block_create_return(self->curblock, ast_ctx(self), NULL);
1932 static bool starts_a_label(ast_expression *ex)
1934 while (ex && ast_istype(ex, ast_block)) {
1935 ast_block *b = (ast_block*)ex;
1940 return ast_istype(ex, ast_label);
1943 /* Note, you will not see ast_block_codegen generate ir_blocks.
1944 * To the AST and the IR, blocks are 2 different things.
1945 * In the AST it represents a block of code, usually enclosed in
1946 * curly braces {...}.
1947 * While in the IR it represents a block in terms of control-flow.
1949 bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_value **out)
1953 /* We don't use this
1954 * Note: an ast-representation using the comma-operator
1955 * of the form: (a, b, c) = x should not assign to c...
1958 compile_error(ast_ctx(self), "not an l-value (code-block)");
1962 if (self->expression.outr) {
1963 *out = self->expression.outr;
1967 /* output is NULL at first, we'll have each expression
1968 * assign to out output, thus, a comma-operator represention
1969 * using an ast_block will return the last generated value,
1970 * so: (b, c) + a executed both b and c, and returns c,
1971 * which is then added to a.
1975 /* generate locals */
1976 for (i = 0; i < vec_size(self->locals); ++i)
1978 if (!ast_local_codegen(self->locals[i], func->ir_func, false)) {
1979 if (OPTS_OPTION_BOOL(OPTION_DEBUG))
1980 compile_error(ast_ctx(self), "failed to generate local `%s`", self->locals[i]->name);
1985 for (i = 0; i < vec_size(self->exprs); ++i)
1987 ast_expression_codegen *gen;
1988 if (func->curblock->final && !starts_a_label(self->exprs[i])) {
1989 if (compile_warning(ast_ctx(self->exprs[i]), WARN_UNREACHABLE_CODE, "unreachable statement"))
1993 gen = self->exprs[i]->codegen;
1994 if (!(*gen)(self->exprs[i], func, false, out))
1998 self->expression.outr = *out;
2003 bool ast_store_codegen(ast_store *self, ast_function *func, bool lvalue, ir_value **out)
2005 ast_expression_codegen *cgen;
2006 ir_value *left = NULL;
2007 ir_value *right = NULL;
2011 ast_array_index *ai = NULL;
2013 if (lvalue && self->expression.outl) {
2014 *out = self->expression.outl;
2018 if (!lvalue && self->expression.outr) {
2019 *out = self->expression.outr;
2023 if (ast_istype(self->dest, ast_array_index))
2026 ai = (ast_array_index*)self->dest;
2027 idx = (ast_value*)ai->index;
2029 if (ast_istype(ai->index, ast_value) && idx->hasvalue && idx->cvq == CV_CONST)
2034 /* we need to call the setter */
2035 ir_value *iridx, *funval;
2039 compile_error(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
2043 arr = (ast_value*)ai->array;
2044 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
2045 compile_error(ast_ctx(self), "value has no setter (%s)", arr->name);
2049 cgen = idx->expression.codegen;
2050 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
2053 cgen = arr->setter->expression.codegen;
2054 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
2057 cgen = self->source->codegen;
2058 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
2061 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "store"), funval, false);
2064 ir_call_param(call, iridx);
2065 ir_call_param(call, right);
2066 self->expression.outr = right;
2072 cgen = self->dest->codegen;
2074 if (!(*cgen)((ast_expression*)(self->dest), func, true, &left))
2076 self->expression.outl = left;
2078 cgen = self->source->codegen;
2080 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
2083 if (!ir_block_create_store_op(func->curblock, ast_ctx(self), self->op, left, right))
2085 self->expression.outr = right;
2088 /* Theoretically, an assinment returns its left side as an
2089 * lvalue, if we don't need an lvalue though, we return
2090 * the right side as an rvalue, otherwise we have to
2091 * somehow know whether or not we need to dereference the pointer
2092 * on the left side - that is: OP_LOAD if it was an address.
2093 * Also: in original QC we cannot OP_LOADP *anyway*.
2095 *out = (lvalue ? left : right);
2100 bool ast_binary_codegen(ast_binary *self, ast_function *func, bool lvalue, ir_value **out)
2102 ast_expression_codegen *cgen;
2103 ir_value *left, *right;
2105 /* A binary operation cannot yield an l-value */
2107 compile_error(ast_ctx(self), "not an l-value (binop)");
2111 if (self->expression.outr) {
2112 *out = self->expression.outr;
2116 if ((OPTS_FLAG(SHORT_LOGIC) || OPTS_FLAG(PERL_LOGIC)) &&
2117 (self->op == INSTR_AND || self->op == INSTR_OR))
2119 /* NOTE: The short-logic path will ignore right_first */
2121 /* short circuit evaluation */
2122 ir_block *other, *merge;
2123 ir_block *from_left, *from_right;
2127 /* prepare end-block */
2128 merge_id = vec_size(func->ir_func->blocks);
2129 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "sce_merge"));
2131 /* generate the left expression */
2132 cgen = self->left->codegen;
2133 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
2135 /* remember the block */
2136 from_left = func->curblock;
2138 /* create a new block for the right expression */
2139 other = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "sce_other"));
2140 if (self->op == INSTR_AND) {
2141 /* on AND: left==true -> other */
2142 if (!ir_block_create_if(func->curblock, ast_ctx(self), left, other, merge))
2145 /* on OR: left==false -> other */
2146 if (!ir_block_create_if(func->curblock, ast_ctx(self), left, merge, other))
2149 /* use the likely flag */
2150 vec_last(func->curblock->instr)->likely = true;
2152 /* enter the right-expression's block */
2153 func->curblock = other;
2155 cgen = self->right->codegen;
2156 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
2158 /* remember block */
2159 from_right = func->curblock;
2161 /* jump to the merge block */
2162 if (!ir_block_create_jump(func->curblock, ast_ctx(self), merge))
2165 vec_remove(func->ir_func->blocks, merge_id, 1);
2166 vec_push(func->ir_func->blocks, merge);
2168 func->curblock = merge;
2169 phi = ir_block_create_phi(func->curblock, ast_ctx(self),
2170 ast_function_label(func, "sce_value"),
2171 self->expression.vtype);
2172 ir_phi_add(phi, from_left, left);
2173 ir_phi_add(phi, from_right, right);
2174 *out = ir_phi_value(phi);
2178 if (!OPTS_FLAG(PERL_LOGIC)) {
2180 if (OPTS_FLAG(CORRECT_LOGIC) && (*out)->vtype == TYPE_VECTOR) {
2181 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
2182 ast_function_label(func, "sce_bool_v"),
2186 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
2187 ast_function_label(func, "sce_bool"),
2192 else if (OPTS_FLAG(FALSE_EMPTY_STRINGS) && (*out)->vtype == TYPE_STRING) {
2193 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
2194 ast_function_label(func, "sce_bool_s"),
2198 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
2199 ast_function_label(func, "sce_bool"),
2205 *out = ir_block_create_binop(func->curblock, ast_ctx(self),
2206 ast_function_label(func, "sce_bool"),
2207 INSTR_AND, *out, *out);
2213 self->expression.outr = *out;
2214 codegen_output_type(self, *out);
2218 if (self->right_first) {
2219 cgen = self->right->codegen;
2220 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
2222 cgen = self->left->codegen;
2223 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
2226 cgen = self->left->codegen;
2227 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
2229 cgen = self->right->codegen;
2230 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
2234 *out = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "bin"),
2235 self->op, left, right);
2238 self->expression.outr = *out;
2239 codegen_output_type(self, *out);
2244 bool ast_binstore_codegen(ast_binstore *self, ast_function *func, bool lvalue, ir_value **out)
2246 ast_expression_codegen *cgen;
2247 ir_value *leftl = NULL, *leftr, *right, *bin;
2251 ast_array_index *ai = NULL;
2252 ir_value *iridx = NULL;
2254 if (lvalue && self->expression.outl) {
2255 *out = self->expression.outl;
2259 if (!lvalue && self->expression.outr) {
2260 *out = self->expression.outr;
2264 if (ast_istype(self->dest, ast_array_index))
2267 ai = (ast_array_index*)self->dest;
2268 idx = (ast_value*)ai->index;
2270 if (ast_istype(ai->index, ast_value) && idx->hasvalue && idx->cvq == CV_CONST)
2274 /* for a binstore we need both an lvalue and an rvalue for the left side */
2275 /* rvalue of destination! */
2277 cgen = idx->expression.codegen;
2278 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
2281 cgen = self->dest->codegen;
2282 if (!(*cgen)((ast_expression*)(self->dest), func, false, &leftr))
2285 /* source as rvalue only */
2286 cgen = self->source->codegen;
2287 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
2290 /* now the binary */
2291 bin = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "binst"),
2292 self->opbin, leftr, right);
2293 self->expression.outr = bin;
2297 /* we need to call the setter */
2302 compile_error(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
2306 arr = (ast_value*)ai->array;
2307 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
2308 compile_error(ast_ctx(self), "value has no setter (%s)", arr->name);
2312 cgen = arr->setter->expression.codegen;
2313 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
2316 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "store"), funval, false);
2319 ir_call_param(call, iridx);
2320 ir_call_param(call, bin);
2321 self->expression.outr = bin;
2323 /* now store them */
2324 cgen = self->dest->codegen;
2325 /* lvalue of destination */
2326 if (!(*cgen)((ast_expression*)(self->dest), func, true, &leftl))
2328 self->expression.outl = leftl;
2330 if (!ir_block_create_store_op(func->curblock, ast_ctx(self), self->opstore, leftl, bin))
2332 self->expression.outr = bin;
2335 /* Theoretically, an assinment returns its left side as an
2336 * lvalue, if we don't need an lvalue though, we return
2337 * the right side as an rvalue, otherwise we have to
2338 * somehow know whether or not we need to dereference the pointer
2339 * on the left side - that is: OP_LOAD if it was an address.
2340 * Also: in original QC we cannot OP_LOADP *anyway*.
2342 *out = (lvalue ? leftl : bin);
2347 bool ast_unary_codegen(ast_unary *self, ast_function *func, bool lvalue, ir_value **out)
2349 ast_expression_codegen *cgen;
2352 /* An unary operation cannot yield an l-value */
2354 compile_error(ast_ctx(self), "not an l-value (binop)");
2358 if (self->expression.outr) {
2359 *out = self->expression.outr;
2363 cgen = self->operand->codegen;
2365 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
2368 *out = ir_block_create_unary(func->curblock, ast_ctx(self), ast_function_label(func, "unary"),
2372 self->expression.outr = *out;
2377 bool ast_return_codegen(ast_return *self, ast_function *func, bool lvalue, ir_value **out)
2379 ast_expression_codegen *cgen;
2384 /* In the context of a return operation, we don't actually return
2388 compile_error(ast_ctx(self), "return-expression is not an l-value");
2392 if (self->expression.outr) {
2393 compile_error(ast_ctx(self), "internal error: ast_return cannot be reused, it bears no result!");
2396 self->expression.outr = (ir_value*)1;
2398 if (self->operand) {
2399 cgen = self->operand->codegen;
2401 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
2404 if (!ir_block_create_return(func->curblock, ast_ctx(self), operand))
2407 if (!ir_block_create_return(func->curblock, ast_ctx(self), NULL))
2414 bool ast_entfield_codegen(ast_entfield *self, ast_function *func, bool lvalue, ir_value **out)
2416 ast_expression_codegen *cgen;
2417 ir_value *ent, *field;
2419 /* This function needs to take the 'lvalue' flag into account!
2420 * As lvalue we provide a field-pointer, as rvalue we provide the
2424 if (lvalue && self->expression.outl) {
2425 *out = self->expression.outl;
2429 if (!lvalue && self->expression.outr) {
2430 *out = self->expression.outr;
2434 cgen = self->entity->codegen;
2435 if (!(*cgen)((ast_expression*)(self->entity), func, false, &ent))
2438 cgen = self->field->codegen;
2439 if (!(*cgen)((ast_expression*)(self->field), func, false, &field))
2444 *out = ir_block_create_fieldaddress(func->curblock, ast_ctx(self), ast_function_label(func, "efa"),
2447 *out = ir_block_create_load_from_ent(func->curblock, ast_ctx(self), ast_function_label(func, "efv"),
2448 ent, field, self->expression.vtype);
2449 /* Done AFTER error checking:
2450 codegen_output_type(self, *out);
2454 compile_error(ast_ctx(self), "failed to create %s instruction (output type %s)",
2455 (lvalue ? "ADDRESS" : "FIELD"),
2456 type_name[self->expression.vtype]);
2460 codegen_output_type(self, *out);
2463 self->expression.outl = *out;
2465 self->expression.outr = *out;
2467 /* Hm that should be it... */
2471 bool ast_member_codegen(ast_member *self, ast_function *func, bool lvalue, ir_value **out)
2473 ast_expression_codegen *cgen;
2476 /* in QC this is always an lvalue */
2477 if (lvalue && self->rvalue) {
2478 compile_error(ast_ctx(self), "not an l-value (member access)");
2481 if (self->expression.outl) {
2482 *out = self->expression.outl;
2486 cgen = self->owner->codegen;
2487 if (!(*cgen)((ast_expression*)(self->owner), func, false, &vec))
2490 if (vec->vtype != TYPE_VECTOR &&
2491 !(vec->vtype == TYPE_FIELD && self->owner->next->vtype == TYPE_VECTOR))
2496 *out = ir_value_vector_member(vec, self->field);
2497 self->expression.outl = *out;
2499 return (*out != NULL);
2502 bool ast_array_index_codegen(ast_array_index *self, ast_function *func, bool lvalue, ir_value **out)
2507 if (!lvalue && self->expression.outr) {
2508 *out = self->expression.outr;
2511 if (lvalue && self->expression.outl) {
2512 *out = self->expression.outl;
2516 if (!ast_istype(self->array, ast_value)) {
2517 compile_error(ast_ctx(self), "array indexing this way is not supported");
2518 /* note this would actually be pointer indexing because the left side is
2519 * not an actual array but (hopefully) an indexable expression.
2520 * Once we get integer arithmetic, and GADDRESS/GSTORE/GLOAD instruction
2521 * support this path will be filled.
2526 arr = (ast_value*)self->array;
2527 idx = (ast_value*)self->index;
2529 if (!ast_istype(self->index, ast_value) || !idx->hasvalue || idx->cvq != CV_CONST) {
2530 /* Time to use accessor functions */
2531 ast_expression_codegen *cgen;
2532 ir_value *iridx, *funval;
2536 compile_error(ast_ctx(self), "(.2) array indexing here needs a compile-time constant");
2541 compile_error(ast_ctx(self), "value has no getter, don't know how to index it");
2545 cgen = self->index->codegen;
2546 if (!(*cgen)((ast_expression*)(self->index), func, false, &iridx))
2549 cgen = arr->getter->expression.codegen;
2550 if (!(*cgen)((ast_expression*)(arr->getter), func, true, &funval))
2553 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "fetch"), funval, false);
2556 ir_call_param(call, iridx);
2558 *out = ir_call_value(call);
2559 self->expression.outr = *out;
2560 (*out)->vtype = self->expression.vtype;
2561 codegen_output_type(self, *out);
2565 if (idx->expression.vtype == TYPE_FLOAT) {
2566 unsigned int arridx = idx->constval.vfloat;
2567 if (arridx >= self->array->count)
2569 compile_error(ast_ctx(self), "array index out of bounds: %i", arridx);
2572 *out = arr->ir_values[arridx];
2574 else if (idx->expression.vtype == TYPE_INTEGER) {
2575 unsigned int arridx = idx->constval.vint;
2576 if (arridx >= self->array->count)
2578 compile_error(ast_ctx(self), "array index out of bounds: %i", arridx);
2581 *out = arr->ir_values[arridx];
2584 compile_error(ast_ctx(self), "array indexing here needs an integer constant");
2587 (*out)->vtype = self->expression.vtype;
2588 codegen_output_type(self, *out);
2592 bool ast_argpipe_codegen(ast_argpipe *self, ast_function *func, bool lvalue, ir_value **out)
2596 compile_error(ast_ctx(self), "argpipe node: not an lvalue");
2601 compile_error(ast_ctx(self), "TODO: argpipe codegen not implemented");
2605 bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_value **out)
2607 ast_expression_codegen *cgen;
2615 ir_block *ontrue_endblock = NULL;
2616 ir_block *onfalse_endblock = NULL;
2617 ir_block *merge = NULL;
2620 /* We don't output any value, thus also don't care about r/lvalue */
2624 if (self->expression.outr) {
2625 compile_error(ast_ctx(self), "internal error: ast_ifthen cannot be reused, it bears no result!");
2628 self->expression.outr = (ir_value*)1;
2630 /* generate the condition */
2631 cgen = self->cond->codegen;
2632 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2634 /* update the block which will get the jump - because short-logic or ternaries may have changed this */
2635 cond = func->curblock;
2637 /* try constant folding away the condition */
2638 if ((fold = fold_cond_ifthen(condval, func, self)) != -1)
2641 if (self->on_true) {
2642 /* create on-true block */
2643 ontrue = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "ontrue"));
2647 /* enter the block */
2648 func->curblock = ontrue;
2651 cgen = self->on_true->codegen;
2652 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &dummy))
2655 /* we now need to work from the current endpoint */
2656 ontrue_endblock = func->curblock;
2661 if (self->on_false) {
2662 /* create on-false block */
2663 onfalse = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "onfalse"));
2667 /* enter the block */
2668 func->curblock = onfalse;
2671 cgen = self->on_false->codegen;
2672 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &dummy))
2675 /* we now need to work from the current endpoint */
2676 onfalse_endblock = func->curblock;
2680 /* Merge block were they all merge in to */
2681 if (!ontrue || !onfalse || !ontrue_endblock->final || !onfalse_endblock->final)
2683 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "endif"));
2686 /* add jumps ot the merge block */
2687 if (ontrue && !ontrue_endblock->final && !ir_block_create_jump(ontrue_endblock, ast_ctx(self), merge))
2689 if (onfalse && !onfalse_endblock->final && !ir_block_create_jump(onfalse_endblock, ast_ctx(self), merge))
2692 /* Now enter the merge block */
2693 func->curblock = merge;
2696 /* we create the if here, that way all blocks are ordered :)
2698 if (!ir_block_create_if(cond, ast_ctx(self), condval,
2699 (ontrue ? ontrue : merge),
2700 (onfalse ? onfalse : merge)))
2708 bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_value **out)
2710 ast_expression_codegen *cgen;
2713 ir_value *trueval, *falseval;
2716 ir_block *cond = func->curblock;
2717 ir_block *cond_out = NULL;
2718 ir_block *ontrue, *ontrue_out = NULL;
2719 ir_block *onfalse, *onfalse_out = NULL;
2723 /* Ternary can never create an lvalue... */
2727 /* In theory it shouldn't be possible to pass through a node twice, but
2728 * in case we add any kind of optimization pass for the AST itself, it
2729 * may still happen, thus we remember a created ir_value and simply return one
2730 * if it already exists.
2732 if (self->expression.outr) {
2733 *out = self->expression.outr;
2737 /* In the following, contraty to ast_ifthen, we assume both paths exist. */
2739 /* generate the condition */
2740 func->curblock = cond;
2741 cgen = self->cond->codegen;
2742 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2744 cond_out = func->curblock;
2746 /* try constant folding away the condition */
2747 if ((fold = fold_cond_ternary(condval, func, self)) != -1)
2750 /* create on-true block */
2751 ontrue = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_T"));
2756 /* enter the block */
2757 func->curblock = ontrue;
2760 cgen = self->on_true->codegen;
2761 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &trueval))
2764 ontrue_out = func->curblock;
2767 /* create on-false block */
2768 onfalse = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_F"));
2773 /* enter the block */
2774 func->curblock = onfalse;
2777 cgen = self->on_false->codegen;
2778 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &falseval))
2781 onfalse_out = func->curblock;
2784 /* create merge block */
2785 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_out"));
2788 /* jump to merge block */
2789 if (!ir_block_create_jump(ontrue_out, ast_ctx(self), merge))
2791 if (!ir_block_create_jump(onfalse_out, ast_ctx(self), merge))
2794 /* create if instruction */
2795 if (!ir_block_create_if(cond_out, ast_ctx(self), condval, ontrue, onfalse))
2798 /* Now enter the merge block */
2799 func->curblock = merge;
2801 /* Here, now, we need a PHI node
2802 * but first some sanity checking...
2804 if (trueval->vtype != falseval->vtype && trueval->vtype != TYPE_NIL && falseval->vtype != TYPE_NIL) {
2805 /* error("ternary with different types on the two sides"); */
2806 compile_error(ast_ctx(self), "internal error: ternary operand types invalid");
2811 phi = ir_block_create_phi(merge, ast_ctx(self), ast_function_label(func, "phi"), self->expression.vtype);
2813 compile_error(ast_ctx(self), "internal error: failed to generate phi node");
2816 ir_phi_add(phi, ontrue_out, trueval);
2817 ir_phi_add(phi, onfalse_out, falseval);
2819 self->expression.outr = ir_phi_value(phi);
2820 *out = self->expression.outr;
2822 codegen_output_type(self, *out);
2827 bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value **out)
2829 ast_expression_codegen *cgen;
2831 ir_value *dummy = NULL;
2832 ir_value *precond = NULL;
2833 ir_value *postcond = NULL;
2835 /* Since we insert some jumps "late" so we have blocks
2836 * ordered "nicely", we need to keep track of the actual end-blocks
2837 * of expressions to add the jumps to.
2839 ir_block *bbody = NULL, *end_bbody = NULL;
2840 ir_block *bprecond = NULL, *end_bprecond = NULL;
2841 ir_block *bpostcond = NULL, *end_bpostcond = NULL;
2842 ir_block *bincrement = NULL, *end_bincrement = NULL;
2843 ir_block *bout = NULL, *bin = NULL;
2845 /* let's at least move the outgoing block to the end */
2848 /* 'break' and 'continue' need to be able to find the right blocks */
2849 ir_block *bcontinue = NULL;
2850 ir_block *bbreak = NULL;
2852 ir_block *tmpblock = NULL;
2857 if (self->expression.outr) {
2858 compile_error(ast_ctx(self), "internal error: ast_loop cannot be reused, it bears no result!");
2861 self->expression.outr = (ir_value*)1;
2864 * Should we ever need some kind of block ordering, better make this function
2865 * move blocks around than write a block ordering algorithm later... after all
2866 * the ast and ir should work together, not against each other.
2869 /* initexpr doesn't get its own block, it's pointless, it could create more blocks
2870 * anyway if for example it contains a ternary.
2874 cgen = self->initexpr->codegen;
2875 if (!(*cgen)((ast_expression*)(self->initexpr), func, false, &dummy))
2879 /* Store the block from which we enter this chaos */
2880 bin = func->curblock;
2882 /* The pre-loop condition needs its own block since we
2883 * need to be able to jump to the start of that expression.
2887 bprecond = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "pre_loop_cond"));
2891 /* the pre-loop-condition the least important place to 'continue' at */
2892 bcontinue = bprecond;
2895 func->curblock = bprecond;
2898 cgen = self->precond->codegen;
2899 if (!(*cgen)((ast_expression*)(self->precond), func, false, &precond))
2902 end_bprecond = func->curblock;
2904 bprecond = end_bprecond = NULL;
2907 /* Now the next blocks won't be ordered nicely, but we need to
2908 * generate them this early for 'break' and 'continue'.
2910 if (self->increment) {
2911 bincrement = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "loop_increment"));
2914 bcontinue = bincrement; /* increment comes before the pre-loop-condition */
2916 bincrement = end_bincrement = NULL;
2919 if (self->postcond) {
2920 bpostcond = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "post_loop_cond"));
2923 bcontinue = bpostcond; /* postcond comes before the increment */
2925 bpostcond = end_bpostcond = NULL;
2928 bout_id = vec_size(func->ir_func->blocks);
2929 bout = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "after_loop"));
2934 /* The loop body... */
2935 /* if (self->body) */
2937 bbody = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "loop_body"));
2942 func->curblock = bbody;
2944 vec_push(func->breakblocks, bbreak);
2946 vec_push(func->continueblocks, bcontinue);
2948 vec_push(func->continueblocks, bbody);
2952 cgen = self->body->codegen;
2953 if (!(*cgen)((ast_expression*)(self->body), func, false, &dummy))
2957 end_bbody = func->curblock;
2958 vec_pop(func->breakblocks);
2959 vec_pop(func->continueblocks);
2962 /* post-loop-condition */
2966 func->curblock = bpostcond;
2969 cgen = self->postcond->codegen;
2970 if (!(*cgen)((ast_expression*)(self->postcond), func, false, &postcond))
2973 end_bpostcond = func->curblock;
2976 /* The incrementor */
2977 if (self->increment)
2980 func->curblock = bincrement;
2983 cgen = self->increment->codegen;
2984 if (!(*cgen)((ast_expression*)(self->increment), func, false, &dummy))
2987 end_bincrement = func->curblock;
2990 /* In any case now, we continue from the outgoing block */
2991 func->curblock = bout;
2993 /* Now all blocks are in place */
2994 /* From 'bin' we jump to whatever comes first */
2995 if (bprecond) tmpblock = bprecond;
2996 else tmpblock = bbody; /* can never be null */
2999 else if (bpostcond) tmpblock = bpostcond;
3000 else tmpblock = bout;
3003 if (!ir_block_create_jump(bin, ast_ctx(self), tmpblock))
3009 ir_block *ontrue, *onfalse;
3010 ontrue = bbody; /* can never be null */
3012 /* all of this is dead code
3013 else if (bincrement) ontrue = bincrement;
3014 else ontrue = bpostcond;
3018 if (self->pre_not) {
3023 if (!ir_block_create_if(end_bprecond, ast_ctx(self), precond, ontrue, onfalse))
3030 if (bincrement) tmpblock = bincrement;
3031 else if (bpostcond) tmpblock = bpostcond;
3032 else if (bprecond) tmpblock = bprecond;
3033 else tmpblock = bbody;
3034 if (!end_bbody->final && !ir_block_create_jump(end_bbody, ast_ctx(self), tmpblock))
3038 /* from increment */
3041 if (bpostcond) tmpblock = bpostcond;
3042 else if (bprecond) tmpblock = bprecond;
3043 else if (bbody) tmpblock = bbody;
3044 else tmpblock = bout;
3045 if (!ir_block_create_jump(end_bincrement, ast_ctx(self), tmpblock))
3052 ir_block *ontrue, *onfalse;
3053 if (bprecond) ontrue = bprecond;
3054 else ontrue = bbody; /* can never be null */
3056 /* all of this is dead code
3057 else if (bincrement) ontrue = bincrement;
3058 else ontrue = bpostcond;
3062 if (self->post_not) {
3067 if (!ir_block_create_if(end_bpostcond, ast_ctx(self), postcond, ontrue, onfalse))
3071 /* Move 'bout' to the end */
3072 vec_remove(func->ir_func->blocks, bout_id, 1);
3073 vec_push(func->ir_func->blocks, bout);
3078 bool ast_breakcont_codegen(ast_breakcont *self, ast_function *func, bool lvalue, ir_value **out)
3085 compile_error(ast_ctx(self), "break/continue expression is not an l-value");
3089 if (self->expression.outr) {
3090 compile_error(ast_ctx(self), "internal error: ast_breakcont cannot be reused!");
3093 self->expression.outr = (ir_value*)1;
3095 if (self->is_continue)
3096 target = func->continueblocks[vec_size(func->continueblocks)-1-self->levels];
3098 target = func->breakblocks[vec_size(func->breakblocks)-1-self->levels];
3101 compile_error(ast_ctx(self), "%s is lacking a target block", (self->is_continue ? "continue" : "break"));
3105 if (!ir_block_create_jump(func->curblock, ast_ctx(self), target))
3110 bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_value **out)
3112 ast_expression_codegen *cgen;
3114 ast_switch_case *def_case = NULL;
3115 ir_block *def_bfall = NULL;
3116 ir_block *def_bfall_to = NULL;
3117 bool set_def_bfall_to = false;
3119 ir_value *dummy = NULL;
3120 ir_value *irop = NULL;
3121 ir_block *bout = NULL;
3122 ir_block *bfall = NULL;
3130 compile_error(ast_ctx(self), "switch expression is not an l-value");
3134 if (self->expression.outr) {
3135 compile_error(ast_ctx(self), "internal error: ast_switch cannot be reused!");
3138 self->expression.outr = (ir_value*)1;
3143 cgen = self->operand->codegen;
3144 if (!(*cgen)((ast_expression*)(self->operand), func, false, &irop))
3147 if (!vec_size(self->cases))
3150 cmpinstr = type_eq_instr[irop->vtype];
3151 if (cmpinstr >= VINSTR_END) {
3152 ast_type_to_string(self->operand, typestr, sizeof(typestr));
3153 compile_error(ast_ctx(self), "invalid type to perform a switch on: %s", typestr);
3157 bout_id = vec_size(func->ir_func->blocks);
3158 bout = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "after_switch"));
3162 /* setup the break block */
3163 vec_push(func->breakblocks, bout);
3165 /* Now create all cases */
3166 for (c = 0; c < vec_size(self->cases); ++c) {
3167 ir_value *cond, *val;
3168 ir_block *bcase, *bnot;
3171 ast_switch_case *swcase = &self->cases[c];
3173 if (swcase->value) {
3174 /* A regular case */
3175 /* generate the condition operand */
3176 cgen = swcase->value->codegen;
3177 if (!(*cgen)((ast_expression*)(swcase->value), func, false, &val))
3179 /* generate the condition */
3180 cond = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "switch_eq"), cmpinstr, irop, val);
3184 bcase = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "case"));
3185 bnot_id = vec_size(func->ir_func->blocks);
3186 bnot = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "not_case"));
3187 if (!bcase || !bnot)
3189 if (set_def_bfall_to) {
3190 set_def_bfall_to = false;
3191 def_bfall_to = bcase;
3193 if (!ir_block_create_if(func->curblock, ast_ctx(self), cond, bcase, bnot))
3196 /* Make the previous case-end fall through */
3197 if (bfall && !bfall->final) {
3198 if (!ir_block_create_jump(bfall, ast_ctx(self), bcase))
3202 /* enter the case */
3203 func->curblock = bcase;
3204 cgen = swcase->code->codegen;
3205 if (!(*cgen)((ast_expression*)swcase->code, func, false, &dummy))
3208 /* remember this block to fall through from */
3209 bfall = func->curblock;
3211 /* enter the else and move it down */
3212 func->curblock = bnot;
3213 vec_remove(func->ir_func->blocks, bnot_id, 1);
3214 vec_push(func->ir_func->blocks, bnot);
3216 /* The default case */
3217 /* Remember where to fall through from: */
3220 /* remember which case it was */
3222 /* And the next case will be remembered */
3223 set_def_bfall_to = true;
3227 /* Jump from the last bnot to bout */
3228 if (bfall && !bfall->final && !ir_block_create_jump(bfall, ast_ctx(self), bout)) {
3230 astwarning(ast_ctx(bfall), WARN_???, "missing break after last case");
3235 /* If there was a default case, put it down here */
3239 /* No need to create an extra block */
3240 bcase = func->curblock;
3242 /* Insert the fallthrough jump */
3243 if (def_bfall && !def_bfall->final) {
3244 if (!ir_block_create_jump(def_bfall, ast_ctx(self), bcase))
3248 /* Now generate the default code */
3249 cgen = def_case->code->codegen;
3250 if (!(*cgen)((ast_expression*)def_case->code, func, false, &dummy))
3253 /* see if we need to fall through */
3254 if (def_bfall_to && !func->curblock->final)
3256 if (!ir_block_create_jump(func->curblock, ast_ctx(self), def_bfall_to))
3261 /* Jump from the last bnot to bout */
3262 if (!func->curblock->final && !ir_block_create_jump(func->curblock, ast_ctx(self), bout))
3264 /* enter the outgoing block */
3265 func->curblock = bout;
3267 /* restore the break block */
3268 vec_pop(func->breakblocks);
3270 /* Move 'bout' to the end, it's nicer */
3271 vec_remove(func->ir_func->blocks, bout_id, 1);
3272 vec_push(func->ir_func->blocks, bout);
3277 bool ast_label_codegen(ast_label *self, ast_function *func, bool lvalue, ir_value **out)
3282 if (self->undefined) {
3283 compile_error(ast_ctx(self), "internal error: ast_label never defined");
3289 compile_error(ast_ctx(self), "internal error: ast_label cannot be an lvalue");
3293 /* simply create a new block and jump to it */
3294 self->irblock = ir_function_create_block(ast_ctx(self), func->ir_func, self->name);
3295 if (!self->irblock) {
3296 compile_error(ast_ctx(self), "failed to allocate label block `%s`", self->name);
3299 if (!func->curblock->final) {
3300 if (!ir_block_create_jump(func->curblock, ast_ctx(self), self->irblock))
3304 /* enter the new block */
3305 func->curblock = self->irblock;
3307 /* Generate all the leftover gotos */
3308 for (i = 0; i < vec_size(self->gotos); ++i) {
3309 if (!ast_goto_codegen(self->gotos[i], func, false, &dummy))
3316 bool ast_goto_codegen(ast_goto *self, ast_function *func, bool lvalue, ir_value **out)
3320 compile_error(ast_ctx(self), "internal error: ast_goto cannot be an lvalue");
3324 if (self->target->irblock) {
3325 if (self->irblock_from) {
3326 /* we already tried once, this is the callback */
3327 self->irblock_from->final = false;
3328 if (!ir_block_create_goto(self->irblock_from, ast_ctx(self), self->target->irblock)) {
3329 compile_error(ast_ctx(self), "failed to generate goto to `%s`", self->name);
3335 if (!ir_block_create_goto(func->curblock, ast_ctx(self), self->target->irblock)) {
3336 compile_error(ast_ctx(self), "failed to generate goto to `%s`", self->name);
3343 /* the target has not yet been created...
3344 * close this block in a sneaky way:
3346 func->curblock->final = true;
3347 self->irblock_from = func->curblock;
3348 ast_label_register_goto(self->target, self);
3354 bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value **out)
3356 ast_expression_codegen *cgen;
3358 ir_instr *callinstr;
3361 ir_value *funval = NULL;
3363 /* return values are never lvalues */
3365 compile_error(ast_ctx(self), "not an l-value (function call)");
3369 if (self->expression.outr) {
3370 *out = self->expression.outr;
3374 cgen = self->func->codegen;
3375 if (!(*cgen)((ast_expression*)(self->func), func, false, &funval))
3383 for (i = 0; i < vec_size(self->params); ++i)
3386 ast_expression *expr = self->params[i];
3388 cgen = expr->codegen;
3389 if (!(*cgen)(expr, func, false, ¶m))
3393 vec_push(params, param);
3396 /* varargs counter */
3397 if (self->va_count) {
3399 ir_builder *builder = func->curblock->owner->owner;
3400 cgen = self->va_count->codegen;
3401 if (!(*cgen)((ast_expression*)(self->va_count), func, false, &va_count))
3403 if (!ir_block_create_store_op(func->curblock, ast_ctx(self), INSTR_STORE_F,
3404 ir_builder_get_va_count(builder), va_count))
3410 callinstr = ir_block_create_call(func->curblock, ast_ctx(self),
3411 ast_function_label(func, "call"),
3412 funval, !!(self->func->flags & AST_FLAG_NORETURN));
3416 for (i = 0; i < vec_size(params); ++i) {
3417 ir_call_param(callinstr, params[i]);
3420 *out = ir_call_value(callinstr);
3421 self->expression.outr = *out;
3423 codegen_output_type(self, *out);
3432 /* iterator functions */
3433 static ast_node* ast_member_next_child(ast_member *self, ast_node *cur) {
3434 (void)self; (void)cur;
3438 static ast_node* ast_value_next_child(ast_value *self, ast_node *cur) {
3439 (void)self; (void)cur;
3443 static ast_node* ast_array_index_next_child(ast_array_index *self, ast_node *cur) {
3444 if (cur == (ast_node*)self)
3445 return (ast_node*)self->array;
3446 if (cur == (ast_node*)self->array)
3447 return (ast_node*)self->index;
3451 static ast_node* ast_argpipe_next_child(ast_argpipe *self, ast_node *cur) {
3452 if (cur == (ast_node*)self)
3453 return (ast_node*)self->index;
3457 static ast_node* ast_store_next_child(ast_store *self, ast_node *cur) {
3458 if (cur == (ast_node*)self)
3459 return (ast_node*)self->dest;
3460 if (cur == (ast_node*)self->dest)
3461 return (ast_node*)self->source;
3465 static ast_node* ast_ifthen_next_child(ast_ifthen *self, ast_node *cur) {
3466 if (cur == (ast_node*)self) {
3467 if (self->cond) return (ast_node*)self->cond;
3468 if (self->on_true) return (ast_node*)self->on_true;
3469 return (ast_node*)self->on_false;
3471 if (self->cond && cur == (ast_node*)self->cond) {
3472 if (self->on_true) return (ast_node*)self->on_true;
3473 return (ast_node*)self->on_false;
3475 if (self->on_true && cur == (ast_node*)self->on_true)
3476 return (ast_node*)self->on_false;
3480 static ast_node* ast_ternary_next_child(ast_ternary *self, ast_node *cur) {
3481 if (cur == (ast_node*)self)
3482 return (ast_node*)self->cond;
3483 if (self->cond && cur == (ast_node*)self->cond)
3484 return (ast_node*)self->on_true;
3485 if (self->on_true && cur == (ast_node*)self->on_true)
3486 return (ast_node*)self->on_false;
3490 static ast_node* ast_loop_next_child(ast_loop *self, ast_node *cur) {
3491 if (cur == (ast_node*)self) {
3492 /* If only we'd use ?: then this would be: return a ?: b ?: c ?: d; */
3493 if (self->initexpr) return (ast_node*)self->initexpr;
3494 if (self->precond) return (ast_node*)self->precond;
3495 if (self->body) return (ast_node*)self->body;
3496 if (self->postcond) return (ast_node*)self->postcond;
3497 return (ast_node*)self->increment;
3499 if (self->initexpr && cur == (ast_node*)self->initexpr) {
3500 if (self->precond) return (ast_node*)self->precond;
3501 if (self->body) return (ast_node*)self->body;
3502 if (self->postcond) return (ast_node*)self->postcond;
3503 return (ast_node*)self->increment;
3505 if (self->precond && cur == (ast_node*)self->precond) {
3506 if (self->body) return (ast_node*)self->body;
3507 if (self->postcond) return (ast_node*)self->postcond;
3508 return (ast_node*)self->increment;
3510 if (self->body && cur == (ast_node*)self->body) {
3511 if (self->postcond) return (ast_node*)self->postcond;
3512 return (ast_node*)self->increment;
3517 static ast_node* ast_breakcont_next_child(ast_breakcont *self, ast_node *cur) {
3518 (void)self; (void)cur;
3522 static ast_node* ast_switch_next_child(ast_switch *self, ast_node *cur) {
3524 if (cur == (ast_node*)self)
3525 return (ast_node*)self->operand;
3526 cases = vec_size(self->cases);
3529 if (cur == (ast_node*)self->operand)
3530 return (ast_node*)self->cases[0].value;
3531 for (i = 0; i != cases; ++i) {
3532 if (cur == (ast_node*)self->cases[i].value)
3533 return (ast_node*)self->cases[i].code;
3534 if (cur == (ast_node*)self->cases[i].code) {
3535 return (i+1 != cases) ? (ast_node*)self->cases[i+1].value
3542 static ast_node* ast_label_next_child(ast_label *self, ast_node *cur) {
3543 (void)self; (void)cur;
3547 static ast_node* ast_goto_next_child(ast_goto *self, ast_node *cur) {
3548 (void)self; (void)cur;
3552 static ast_node* ast_call_next_child(ast_call *self, ast_node *cur) {
3554 if (cur == (ast_node*)self)
3555 return (ast_node*)self->func;
3556 params = vec_size(self->params);
3559 if (cur == (ast_node*)self->func)
3560 return (ast_node*)self->params[0];
3561 for (i = 1; i != params; ++i) {
3562 if (cur == (ast_node*)self->params[i-1])
3563 return (ast_node*)self->params[i];
3568 static ast_node* ast_block_next_child(ast_block *self, ast_node *cur) {
3569 size_t i, exprs = vec_size(self->exprs);
3572 if (cur == (ast_node*)self)
3573 return (ast_node*)self->exprs[0];
3574 for (i = 1; i != exprs; ++i) {
3575 if (cur == (ast_node*)self->exprs[i-1])
3576 return (ast_node*)self->exprs[i];
3581 static ast_node* ast_unary_next_child(ast_unary *self, ast_node *cur) {
3582 if (cur == (ast_node*)self)
3583 return (ast_node*)self->operand;
3587 static ast_node* ast_entfield_next_child(ast_entfield *self, ast_node *cur) {
3588 if (cur == (ast_node*)self)
3589 return (ast_node*)self->entity;
3590 if (cur == (ast_node*)self->entity)
3591 return (ast_node*)self->field;
3595 static ast_node* ast_return_next_child(ast_return *self, ast_node *cur) {
3596 if (cur == (ast_node*)self)
3597 return (ast_node*)self->operand;
3601 static ast_node* ast_binstore_next_child(ast_binstore *self, ast_node *cur) {
3602 if (cur == (ast_node*)self)
3603 return (ast_node*)self->dest;
3604 if (cur == (ast_node*)self->dest)
3605 return (ast_node*)self->source;
3609 static ast_node* ast_binary_next_child(ast_binary *self, ast_node *cur) {
3610 if (cur == (ast_node*)self)
3611 return (ast_node*)self->left;
3612 if (cur == (ast_node*)self->left)
3613 return (ast_node*)self->right;
3617 static ast_node* ast_function_next_child(ast_function *self, ast_node *cur) {
3618 size_t i, blocks = vec_size(self->blocks);
3621 if (cur == (ast_node*)self)
3622 return (ast_node*)self->blocks[0];
3623 for (i = 1; i != blocks; ++i) {
3624 if (cur == (ast_node*)self->blocks[i-1])
3625 return (ast_node*)self->blocks[i];