2 * Copyright (C) 2012, 2013, 2014
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) \
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
40 * forward declarations, these need not be in ast.h for obvious
43 static bool ast_member_codegen(ast_member*, ast_function*, bool lvalue, ir_value**);
44 static void ast_array_index_delete(ast_array_index*);
45 static bool ast_array_index_codegen(ast_array_index*, ast_function*, bool lvalue, ir_value**);
46 static void ast_argpipe_delete(ast_argpipe*);
47 static bool ast_argpipe_codegen(ast_argpipe*, ast_function*, bool lvalue, ir_value**);
48 static void ast_store_delete(ast_store*);
49 static bool ast_store_codegen(ast_store*, ast_function*, bool lvalue, ir_value**);
50 static void ast_ifthen_delete(ast_ifthen*);
51 static bool ast_ifthen_codegen(ast_ifthen*, ast_function*, bool lvalue, ir_value**);
52 static void ast_ternary_delete(ast_ternary*);
53 static bool ast_ternary_codegen(ast_ternary*, ast_function*, bool lvalue, ir_value**);
54 static void ast_loop_delete(ast_loop*);
55 static bool ast_loop_codegen(ast_loop*, ast_function*, bool lvalue, ir_value**);
56 static void ast_breakcont_delete(ast_breakcont*);
57 static bool ast_breakcont_codegen(ast_breakcont*, ast_function*, bool lvalue, ir_value**);
58 static void ast_switch_delete(ast_switch*);
59 static bool ast_switch_codegen(ast_switch*, ast_function*, bool lvalue, ir_value**);
60 static void ast_label_delete(ast_label*);
61 static void ast_label_register_goto(ast_label*, ast_goto*);
62 static bool ast_label_codegen(ast_label*, ast_function*, bool lvalue, ir_value**);
63 static bool ast_goto_codegen(ast_goto*, ast_function*, bool lvalue, ir_value**);
64 static void ast_goto_delete(ast_goto*);
65 static void ast_call_delete(ast_call*);
66 static bool ast_call_codegen(ast_call*, ast_function*, bool lvalue, ir_value**);
67 static bool ast_block_codegen(ast_block*, ast_function*, bool lvalue, ir_value**);
68 static void ast_unary_delete(ast_unary*);
69 static bool ast_unary_codegen(ast_unary*, ast_function*, bool lvalue, ir_value**);
70 static void ast_entfield_delete(ast_entfield*);
71 static bool ast_entfield_codegen(ast_entfield*, ast_function*, bool lvalue, ir_value**);
72 static void ast_return_delete(ast_return*);
73 static bool ast_return_codegen(ast_return*, ast_function*, bool lvalue, ir_value**);
74 static void ast_binstore_delete(ast_binstore*);
75 static bool ast_binstore_codegen(ast_binstore*, ast_function*, bool lvalue, ir_value**);
76 static void ast_binary_delete(ast_binary*);
77 static bool ast_binary_codegen(ast_binary*, ast_function*, bool lvalue, ir_value**);
79 /* It must not be possible to get here. */
80 static GMQCC_NORETURN void _ast_node_destroy(ast_node *self)
83 con_err("ast node missing destroy()\n");
87 /* Initialize main ast node aprts */
88 static void ast_node_init(ast_node *self, lex_ctx_t ctx, int nodetype)
91 self->destroy = &_ast_node_destroy;
93 self->nodetype = nodetype;
94 self->side_effects = false;
97 /* weight and side effects */
98 static void _ast_propagate_effects(ast_node *self, ast_node *other)
100 if (ast_side_effects(other))
101 ast_side_effects(self) = true;
103 #define ast_propagate_effects(s,o) _ast_propagate_effects(((ast_node*)(s)), ((ast_node*)(o)))
105 /* General expression initialization */
106 static void ast_expression_init(ast_expression *self,
107 ast_expression_codegen *codegen)
109 self->codegen = codegen;
110 self->vtype = TYPE_VOID;
117 self->varparam = NULL;
120 static void ast_expression_delete(ast_expression *self)
124 ast_delete(self->next);
125 for (i = 0; i < vec_size(self->params); ++i) {
126 ast_delete(self->params[i]);
128 vec_free(self->params);
130 ast_delete(self->varparam);
133 static void ast_expression_delete_full(ast_expression *self)
135 ast_expression_delete(self);
139 ast_value* ast_value_copy(const ast_value *self)
142 const ast_expression *fromex;
143 ast_expression *selfex;
144 ast_value *cp = ast_value_new(self->expression.node.context, self->name, self->expression.vtype);
145 if (self->expression.next) {
146 cp->expression.next = ast_type_copy(self->expression.node.context, self->expression.next);
148 fromex = &self->expression;
149 selfex = &cp->expression;
150 selfex->count = fromex->count;
151 selfex->flags = fromex->flags;
152 for (i = 0; i < vec_size(fromex->params); ++i) {
153 ast_value *v = ast_value_copy(fromex->params[i]);
154 vec_push(selfex->params, v);
159 void ast_type_adopt_impl(ast_expression *self, const ast_expression *other)
162 const ast_expression *fromex;
163 ast_expression *selfex;
164 self->vtype = other->vtype;
166 self->next = (ast_expression*)ast_type_copy(ast_ctx(self), other->next);
170 selfex->count = fromex->count;
171 selfex->flags = fromex->flags;
172 for (i = 0; i < vec_size(fromex->params); ++i) {
173 ast_value *v = ast_value_copy(fromex->params[i]);
174 vec_push(selfex->params, v);
178 static ast_expression* ast_shallow_type(lex_ctx_t ctx, int vtype)
180 ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
181 ast_expression_init(self, NULL);
182 self->codegen = NULL;
188 ast_expression* ast_type_copy(lex_ctx_t ctx, const ast_expression *ex)
191 const ast_expression *fromex;
192 ast_expression *selfex;
198 ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
199 ast_expression_init(self, NULL);
204 /* This may never be codegen()d */
205 selfex->codegen = NULL;
207 selfex->vtype = fromex->vtype;
209 selfex->next = ast_type_copy(ctx, fromex->next);
213 selfex->count = fromex->count;
214 selfex->flags = fromex->flags;
215 for (i = 0; i < vec_size(fromex->params); ++i) {
216 ast_value *v = ast_value_copy(fromex->params[i]);
217 vec_push(selfex->params, v);
224 bool ast_compare_type(ast_expression *a, ast_expression *b)
226 if (a->vtype == TYPE_NIL ||
227 b->vtype == TYPE_NIL)
229 if (a->vtype != b->vtype)
231 if (!a->next != !b->next)
233 if (vec_size(a->params) != vec_size(b->params))
235 if ((a->flags & AST_FLAG_TYPE_MASK) !=
236 (b->flags & AST_FLAG_TYPE_MASK) )
240 if (vec_size(a->params)) {
242 for (i = 0; i < vec_size(a->params); ++i) {
243 if (!ast_compare_type((ast_expression*)a->params[i],
244 (ast_expression*)b->params[i]))
249 return ast_compare_type(a->next, b->next);
253 static size_t ast_type_to_string_impl(ast_expression *e, char *buf, size_t bufsize, size_t pos)
260 if (pos + 6 >= bufsize)
262 util_strncpy(buf + pos, "(null)", 6);
266 if (pos + 1 >= bufsize)
271 util_strncpy(buf + pos, "(variant)", 9);
276 return ast_type_to_string_impl(e->next, buf, bufsize, pos);
279 if (pos + 3 >= bufsize)
283 pos = ast_type_to_string_impl(e->next, buf, bufsize, pos);
284 if (pos + 1 >= bufsize)
290 pos = ast_type_to_string_impl(e->next, buf, bufsize, pos);
291 if (pos + 2 >= bufsize)
293 if (!vec_size(e->params)) {
299 pos = ast_type_to_string_impl((ast_expression*)(e->params[0]), buf, bufsize, pos);
300 for (i = 1; i < vec_size(e->params); ++i) {
301 if (pos + 2 >= bufsize)
305 pos = ast_type_to_string_impl((ast_expression*)(e->params[i]), buf, bufsize, pos);
307 if (pos + 1 >= bufsize)
313 pos = ast_type_to_string_impl(e->next, buf, bufsize, pos);
314 if (pos + 1 >= bufsize)
317 pos += util_snprintf(buf + pos, bufsize - pos - 1, "%i", (int)e->count);
318 if (pos + 1 >= bufsize)
324 typestr = type_name[e->vtype];
325 typelen = strlen(typestr);
326 if (pos + typelen >= bufsize)
328 util_strncpy(buf + pos, typestr, typelen);
329 return pos + typelen;
333 buf[bufsize-3] = '.';
334 buf[bufsize-2] = '.';
335 buf[bufsize-1] = '.';
339 void ast_type_to_string(ast_expression *e, char *buf, size_t bufsize)
341 size_t pos = ast_type_to_string_impl(e, buf, bufsize-1, 0);
345 static bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out);
346 ast_value* ast_value_new(lex_ctx_t ctx, const char *name, int t)
348 ast_instantiate(ast_value, ctx, ast_value_delete);
349 ast_expression_init((ast_expression*)self,
350 (ast_expression_codegen*)&ast_value_codegen);
351 self->expression.node.keep = true; /* keep */
353 self->name = name ? util_strdup(name) : NULL;
354 self->expression.vtype = t;
355 self->expression.next = NULL;
356 self->isfield = false;
358 self->hasvalue = false;
361 memset(&self->constval, 0, sizeof(self->constval));
362 self->initlist = NULL;
365 self->ir_values = NULL;
366 self->ir_value_count = 0;
372 self->argcounter = NULL;
373 self->intrinsic = false;
378 void ast_value_delete(ast_value* self)
381 mem_d((void*)self->name);
382 if (self->argcounter)
383 mem_d((void*)self->argcounter);
384 if (self->hasvalue) {
385 switch (self->expression.vtype)
388 mem_d((void*)self->constval.vstring);
391 /* unlink us from the function node */
392 self->constval.vfunc->vtype = NULL;
394 /* NOTE: delete function? currently collected in
395 * the parser structure
402 mem_d(self->ir_values);
407 if (self->initlist) {
408 if (self->expression.next->vtype == TYPE_STRING) {
409 /* strings are allocated, free them */
410 size_t i, len = vec_size(self->initlist);
411 /* in theory, len should be expression.count
412 * but let's not take any chances */
413 for (i = 0; i < len; ++i) {
414 if (self->initlist[i].vstring)
415 mem_d(self->initlist[i].vstring);
418 vec_free(self->initlist);
421 ast_expression_delete((ast_expression*)self);
425 void ast_value_params_add(ast_value *self, ast_value *p)
427 vec_push(self->expression.params, p);
430 bool ast_value_set_name(ast_value *self, const char *name)
433 mem_d((void*)self->name);
434 self->name = util_strdup(name);
438 ast_binary* ast_binary_new(lex_ctx_t ctx, int op,
439 ast_expression* left, ast_expression* right)
441 ast_instantiate(ast_binary, ctx, ast_binary_delete);
442 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binary_codegen);
444 if (ast_istype(right, ast_unary) && OPTS_OPTIMIZATION(OPTIM_PEEPHOLE)) {
445 ast_unary *unary = ((ast_unary*)right);
446 ast_expression *normal = unary->operand;
448 /* make a-(-b) => a + b */
449 if (unary->op == VINSTR_NEG_F || unary->op == VINSTR_NEG_V) {
450 if (op == INSTR_SUB_F) {
453 ++opts_optimizationcount[OPTIM_PEEPHOLE];
454 } else if (op == INSTR_SUB_V) {
457 ++opts_optimizationcount[OPTIM_PEEPHOLE];
465 self->right_first = false;
467 ast_propagate_effects(self, left);
468 ast_propagate_effects(self, right);
470 if (op >= INSTR_EQ_F && op <= INSTR_GT)
471 self->expression.vtype = TYPE_FLOAT;
472 else if (op == INSTR_AND || op == INSTR_OR) {
473 if (OPTS_FLAG(PERL_LOGIC))
474 ast_type_adopt(self, right);
476 self->expression.vtype = TYPE_FLOAT;
478 else if (op == INSTR_BITAND || op == INSTR_BITOR)
479 self->expression.vtype = TYPE_FLOAT;
480 else if (op == INSTR_MUL_VF || op == INSTR_MUL_FV)
481 self->expression.vtype = TYPE_VECTOR;
482 else if (op == INSTR_MUL_V)
483 self->expression.vtype = TYPE_FLOAT;
485 self->expression.vtype = left->vtype;
488 self->refs = AST_REF_ALL;
493 void ast_binary_delete(ast_binary *self)
495 if (self->refs & AST_REF_LEFT) ast_unref(self->left);
496 if (self->refs & AST_REF_RIGHT) ast_unref(self->right);
498 ast_expression_delete((ast_expression*)self);
502 ast_binstore* ast_binstore_new(lex_ctx_t ctx, int storop, int op,
503 ast_expression* left, ast_expression* right)
505 ast_instantiate(ast_binstore, ctx, ast_binstore_delete);
506 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binstore_codegen);
508 ast_side_effects(self) = true;
510 self->opstore = storop;
513 self->source = right;
515 self->keep_dest = false;
517 ast_type_adopt(self, left);
521 void ast_binstore_delete(ast_binstore *self)
523 if (!self->keep_dest)
524 ast_unref(self->dest);
525 ast_unref(self->source);
526 ast_expression_delete((ast_expression*)self);
530 ast_unary* ast_unary_new(lex_ctx_t ctx, int op,
531 ast_expression *expr)
533 ast_instantiate(ast_unary, ctx, ast_unary_delete);
534 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_unary_codegen);
537 self->operand = expr;
540 if (ast_istype(expr, ast_unary) && OPTS_OPTIMIZATION(OPTIM_PEEPHOLE)) {
541 ast_unary *prev = (ast_unary*)((ast_unary*)expr)->operand;
543 /* Handle for double negation */
544 if (((ast_unary*)expr)->op == op)
545 prev = (ast_unary*)((ast_unary*)expr)->operand;
547 if (ast_istype(prev, ast_unary)) {
548 ast_expression_delete((ast_expression*)self);
550 ++opts_optimizationcount[OPTIM_PEEPHOLE];
555 ast_propagate_effects(self, expr);
557 if ((op >= INSTR_NOT_F && op <= INSTR_NOT_FNC) || op == VINSTR_NEG_F) {
558 self->expression.vtype = TYPE_FLOAT;
559 } else if (op == VINSTR_NEG_V) {
560 self->expression.vtype = TYPE_VECTOR;
562 compile_error(ctx, "cannot determine type of unary operation %s", util_instr_str[op]);
568 void ast_unary_delete(ast_unary *self)
570 if (self->operand) ast_unref(self->operand);
571 ast_expression_delete((ast_expression*)self);
575 ast_return* ast_return_new(lex_ctx_t ctx, ast_expression *expr)
577 ast_instantiate(ast_return, ctx, ast_return_delete);
578 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_return_codegen);
580 self->operand = expr;
583 ast_propagate_effects(self, expr);
588 void ast_return_delete(ast_return *self)
591 ast_unref(self->operand);
592 ast_expression_delete((ast_expression*)self);
596 ast_entfield* ast_entfield_new(lex_ctx_t ctx, ast_expression *entity, ast_expression *field)
598 if (field->vtype != TYPE_FIELD) {
599 compile_error(ctx, "ast_entfield_new with expression not of type field");
602 return ast_entfield_new_force(ctx, entity, field, field->next);
605 ast_entfield* ast_entfield_new_force(lex_ctx_t ctx, ast_expression *entity, ast_expression *field, const ast_expression *outtype)
607 ast_instantiate(ast_entfield, ctx, ast_entfield_delete);
611 /* Error: field has no type... */
615 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_entfield_codegen);
617 self->entity = entity;
619 ast_propagate_effects(self, entity);
620 ast_propagate_effects(self, field);
622 ast_type_adopt(self, outtype);
626 void ast_entfield_delete(ast_entfield *self)
628 ast_unref(self->entity);
629 ast_unref(self->field);
630 ast_expression_delete((ast_expression*)self);
634 ast_member* ast_member_new(lex_ctx_t ctx, ast_expression *owner, unsigned int field, const char *name)
636 ast_instantiate(ast_member, ctx, ast_member_delete);
642 if (owner->vtype != TYPE_VECTOR &&
643 owner->vtype != TYPE_FIELD) {
644 compile_error(ctx, "member-access on an invalid owner of type %s", type_name[owner->vtype]);
649 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_member_codegen);
650 self->expression.node.keep = true; /* keep */
652 if (owner->vtype == TYPE_VECTOR) {
653 self->expression.vtype = TYPE_FLOAT;
654 self->expression.next = NULL;
656 self->expression.vtype = TYPE_FIELD;
657 self->expression.next = ast_shallow_type(ctx, TYPE_FLOAT);
660 self->rvalue = false;
662 ast_propagate_effects(self, owner);
666 self->name = util_strdup(name);
673 void ast_member_delete(ast_member *self)
675 /* The owner is always an ast_value, which has .keep=true,
676 * also: ast_members are usually deleted after the owner, thus
677 * this will cause invalid access
678 ast_unref(self->owner);
679 * once we allow (expression).x to access a vector-member, we need
680 * to change this: preferably by creating an alternate ast node for this
681 * purpose that is not garbage-collected.
683 ast_expression_delete((ast_expression*)self);
688 bool ast_member_set_name(ast_member *self, const char *name)
691 mem_d((void*)self->name);
692 self->name = util_strdup(name);
696 ast_array_index* ast_array_index_new(lex_ctx_t ctx, ast_expression *array, ast_expression *index)
698 ast_expression *outtype;
699 ast_instantiate(ast_array_index, ctx, ast_array_index_delete);
701 outtype = array->next;
704 /* Error: field has no type... */
708 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_array_index_codegen);
712 ast_propagate_effects(self, array);
713 ast_propagate_effects(self, index);
715 ast_type_adopt(self, outtype);
716 if (array->vtype == TYPE_FIELD && outtype->vtype == TYPE_ARRAY) {
717 if (self->expression.vtype != TYPE_ARRAY) {
718 compile_error(ast_ctx(self), "array_index node on type");
719 ast_array_index_delete(self);
722 self->array = outtype;
723 self->expression.vtype = TYPE_FIELD;
729 void ast_array_index_delete(ast_array_index *self)
732 ast_unref(self->array);
734 ast_unref(self->index);
735 ast_expression_delete((ast_expression*)self);
739 ast_argpipe* ast_argpipe_new(lex_ctx_t ctx, ast_expression *index)
741 ast_instantiate(ast_argpipe, ctx, ast_argpipe_delete);
742 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_argpipe_codegen);
744 self->expression.vtype = TYPE_NOEXPR;
748 void ast_argpipe_delete(ast_argpipe *self)
751 ast_unref(self->index);
752 ast_expression_delete((ast_expression*)self);
756 ast_ifthen* ast_ifthen_new(lex_ctx_t ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
758 ast_instantiate(ast_ifthen, ctx, ast_ifthen_delete);
759 if (!ontrue && !onfalse) {
760 /* because it is invalid */
764 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ifthen_codegen);
767 self->on_true = ontrue;
768 self->on_false = onfalse;
769 ast_propagate_effects(self, cond);
771 ast_propagate_effects(self, ontrue);
773 ast_propagate_effects(self, onfalse);
778 void ast_ifthen_delete(ast_ifthen *self)
780 ast_unref(self->cond);
782 ast_unref(self->on_true);
784 ast_unref(self->on_false);
785 ast_expression_delete((ast_expression*)self);
789 ast_ternary* ast_ternary_new(lex_ctx_t ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
791 ast_expression *exprtype = ontrue;
792 ast_instantiate(ast_ternary, ctx, ast_ternary_delete);
793 /* This time NEITHER must be NULL */
794 if (!ontrue || !onfalse) {
798 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ternary_codegen);
801 self->on_true = ontrue;
802 self->on_false = onfalse;
803 ast_propagate_effects(self, cond);
804 ast_propagate_effects(self, ontrue);
805 ast_propagate_effects(self, onfalse);
807 if (ontrue->vtype == TYPE_NIL)
809 ast_type_adopt(self, exprtype);
814 void ast_ternary_delete(ast_ternary *self)
816 /* the if()s are only there because computed-gotos can set them
819 if (self->cond) ast_unref(self->cond);
820 if (self->on_true) ast_unref(self->on_true);
821 if (self->on_false) ast_unref(self->on_false);
822 ast_expression_delete((ast_expression*)self);
826 ast_loop* ast_loop_new(lex_ctx_t ctx,
827 ast_expression *initexpr,
828 ast_expression *precond, bool pre_not,
829 ast_expression *postcond, bool post_not,
830 ast_expression *increment,
831 ast_expression *body)
833 ast_instantiate(ast_loop, ctx, ast_loop_delete);
834 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_loop_codegen);
836 self->initexpr = initexpr;
837 self->precond = precond;
838 self->postcond = postcond;
839 self->increment = increment;
842 self->pre_not = pre_not;
843 self->post_not = post_not;
846 ast_propagate_effects(self, initexpr);
848 ast_propagate_effects(self, precond);
850 ast_propagate_effects(self, postcond);
852 ast_propagate_effects(self, increment);
854 ast_propagate_effects(self, body);
859 void ast_loop_delete(ast_loop *self)
862 ast_unref(self->initexpr);
864 ast_unref(self->precond);
866 ast_unref(self->postcond);
868 ast_unref(self->increment);
870 ast_unref(self->body);
871 ast_expression_delete((ast_expression*)self);
875 ast_breakcont* ast_breakcont_new(lex_ctx_t ctx, bool iscont, unsigned int levels)
877 ast_instantiate(ast_breakcont, ctx, ast_breakcont_delete);
878 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_breakcont_codegen);
880 self->is_continue = iscont;
881 self->levels = levels;
886 void ast_breakcont_delete(ast_breakcont *self)
888 ast_expression_delete((ast_expression*)self);
892 ast_switch* ast_switch_new(lex_ctx_t ctx, ast_expression *op)
894 ast_instantiate(ast_switch, ctx, ast_switch_delete);
895 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_switch_codegen);
900 ast_propagate_effects(self, op);
905 void ast_switch_delete(ast_switch *self)
908 ast_unref(self->operand);
910 for (i = 0; i < vec_size(self->cases); ++i) {
911 if (self->cases[i].value)
912 ast_unref(self->cases[i].value);
913 ast_unref(self->cases[i].code);
915 vec_free(self->cases);
917 ast_expression_delete((ast_expression*)self);
921 ast_label* ast_label_new(lex_ctx_t ctx, const char *name, bool undefined)
923 ast_instantiate(ast_label, ctx, ast_label_delete);
924 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_label_codegen);
926 self->expression.vtype = TYPE_NOEXPR;
928 self->name = util_strdup(name);
929 self->irblock = NULL;
931 self->undefined = undefined;
936 void ast_label_delete(ast_label *self)
938 mem_d((void*)self->name);
939 vec_free(self->gotos);
940 ast_expression_delete((ast_expression*)self);
944 static void ast_label_register_goto(ast_label *self, ast_goto *g)
946 vec_push(self->gotos, g);
949 ast_goto* ast_goto_new(lex_ctx_t ctx, const char *name)
951 ast_instantiate(ast_goto, ctx, ast_goto_delete);
952 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_goto_codegen);
954 self->name = util_strdup(name);
956 self->irblock_from = NULL;
961 void ast_goto_delete(ast_goto *self)
963 mem_d((void*)self->name);
964 ast_expression_delete((ast_expression*)self);
968 void ast_goto_set_label(ast_goto *self, ast_label *label)
970 self->target = label;
973 ast_call* ast_call_new(lex_ctx_t ctx,
974 ast_expression *funcexpr)
976 ast_instantiate(ast_call, ctx, ast_call_delete);
977 if (!funcexpr->next) {
978 compile_error(ctx, "not a function");
982 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_call_codegen);
984 ast_side_effects(self) = true;
987 self->func = funcexpr;
988 self->va_count = NULL;
990 ast_type_adopt(self, funcexpr->next);
995 void ast_call_delete(ast_call *self)
998 for (i = 0; i < vec_size(self->params); ++i)
999 ast_unref(self->params[i]);
1000 vec_free(self->params);
1003 ast_unref(self->func);
1006 ast_unref(self->va_count);
1008 ast_expression_delete((ast_expression*)self);
1012 static bool ast_call_check_vararg(ast_call *self, ast_expression *va_type, ast_expression *exp_type)
1018 if (!va_type || !ast_compare_type(va_type, exp_type))
1020 if (va_type && exp_type)
1022 ast_type_to_string(va_type, tgot, sizeof(tgot));
1023 ast_type_to_string(exp_type, texp, sizeof(texp));
1024 if (OPTS_FLAG(UNSAFE_VARARGS)) {
1025 if (compile_warning(ast_ctx(self), WARN_UNSAFE_TYPES,
1026 "piped variadic argument differs in type: constrained to type %s, expected type %s",
1030 compile_error(ast_ctx(self),
1031 "piped variadic argument differs in type: constrained to type %s, expected type %s",
1038 ast_type_to_string(exp_type, texp, sizeof(texp));
1039 if (OPTS_FLAG(UNSAFE_VARARGS)) {
1040 if (compile_warning(ast_ctx(self), WARN_UNSAFE_TYPES,
1041 "piped variadic argument may differ in type: expected type %s",
1045 compile_error(ast_ctx(self),
1046 "piped variadic argument may differ in type: expected type %s",
1055 bool ast_call_check_types(ast_call *self, ast_expression *va_type)
1061 const ast_expression *func = self->func;
1062 size_t count = vec_size(self->params);
1063 if (count > vec_size(func->params))
1064 count = vec_size(func->params);
1066 for (i = 0; i < count; ++i) {
1067 if (ast_istype(self->params[i], ast_argpipe)) {
1068 /* warn about type safety instead */
1070 compile_error(ast_ctx(self), "argpipe must be the last parameter to a function call");
1073 if (!ast_call_check_vararg(self, va_type, (ast_expression*)func->params[i]))
1076 else if (!ast_compare_type(self->params[i], (ast_expression*)(func->params[i])))
1078 ast_type_to_string(self->params[i], tgot, sizeof(tgot));
1079 ast_type_to_string((ast_expression*)func->params[i], texp, sizeof(texp));
1080 compile_error(ast_ctx(self), "invalid type for parameter %u in function call: expected %s, got %s",
1081 (unsigned int)(i+1), texp, tgot);
1082 /* we don't immediately return */
1086 count = vec_size(self->params);
1087 if (count > vec_size(func->params) && func->varparam) {
1088 for (; i < count; ++i) {
1089 if (ast_istype(self->params[i], ast_argpipe)) {
1090 /* warn about type safety instead */
1092 compile_error(ast_ctx(self), "argpipe must be the last parameter to a function call");
1095 if (!ast_call_check_vararg(self, va_type, func->varparam))
1098 else if (!ast_compare_type(self->params[i], func->varparam))
1100 ast_type_to_string(self->params[i], tgot, sizeof(tgot));
1101 ast_type_to_string(func->varparam, texp, sizeof(texp));
1102 compile_error(ast_ctx(self), "invalid type for variadic parameter %u in function call: expected %s, got %s",
1103 (unsigned int)(i+1), texp, tgot);
1104 /* we don't immediately return */
1112 ast_store* ast_store_new(lex_ctx_t ctx, int op,
1113 ast_expression *dest, ast_expression *source)
1115 ast_instantiate(ast_store, ctx, ast_store_delete);
1116 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_store_codegen);
1118 ast_side_effects(self) = true;
1122 self->source = source;
1124 ast_type_adopt(self, dest);
1129 void ast_store_delete(ast_store *self)
1131 ast_unref(self->dest);
1132 ast_unref(self->source);
1133 ast_expression_delete((ast_expression*)self);
1137 ast_block* ast_block_new(lex_ctx_t ctx)
1139 ast_instantiate(ast_block, ctx, ast_block_delete);
1140 ast_expression_init((ast_expression*)self,
1141 (ast_expression_codegen*)&ast_block_codegen);
1143 self->locals = NULL;
1145 self->collect = NULL;
1150 bool ast_block_add_expr(ast_block *self, ast_expression *e)
1152 ast_propagate_effects(self, e);
1153 vec_push(self->exprs, e);
1154 if (self->expression.next) {
1155 ast_delete(self->expression.next);
1156 self->expression.next = NULL;
1158 ast_type_adopt(self, e);
1162 void ast_block_collect(ast_block *self, ast_expression *expr)
1164 vec_push(self->collect, expr);
1165 expr->node.keep = true;
1168 void ast_block_delete(ast_block *self)
1171 for (i = 0; i < vec_size(self->exprs); ++i)
1172 ast_unref(self->exprs[i]);
1173 vec_free(self->exprs);
1174 for (i = 0; i < vec_size(self->locals); ++i)
1175 ast_delete(self->locals[i]);
1176 vec_free(self->locals);
1177 for (i = 0; i < vec_size(self->collect); ++i)
1178 ast_delete(self->collect[i]);
1179 vec_free(self->collect);
1180 ast_expression_delete((ast_expression*)self);
1184 void ast_block_set_type(ast_block *self, ast_expression *from)
1186 if (self->expression.next)
1187 ast_delete(self->expression.next);
1188 ast_type_adopt(self, from);
1191 ast_function* ast_function_new(lex_ctx_t ctx, const char *name, ast_value *vtype)
1193 ast_instantiate(ast_function, ctx, ast_function_delete);
1196 compile_error(ast_ctx(self), "internal error: ast_function_new condition 0");
1198 } else if (vtype->hasvalue || vtype->expression.vtype != TYPE_FUNCTION) {
1199 compile_error(ast_ctx(self), "internal error: ast_function_new condition %i %i type=%i (probably 2 bodies?)",
1201 (int)vtype->hasvalue,
1202 vtype->expression.vtype);
1206 self->vtype = vtype;
1207 self->name = name ? util_strdup(name) : NULL;
1208 self->blocks = NULL;
1210 self->labelcount = 0;
1213 self->ir_func = NULL;
1214 self->curblock = NULL;
1216 self->breakblocks = NULL;
1217 self->continueblocks = NULL;
1219 vtype->hasvalue = true;
1220 vtype->constval.vfunc = self;
1222 self->varargs = NULL;
1224 self->fixedparams = NULL;
1225 self->return_value = NULL;
1227 self->static_names = NULL;
1228 self->static_count = 0;
1237 void ast_function_delete(ast_function *self)
1241 mem_d((void*)self->name);
1243 /* ast_value_delete(self->vtype); */
1244 self->vtype->hasvalue = false;
1245 self->vtype->constval.vfunc = NULL;
1246 /* We use unref - if it was stored in a global table it is supposed
1247 * to be deleted from *there*
1249 ast_unref(self->vtype);
1251 for (i = 0; i < vec_size(self->static_names); ++i)
1252 mem_d(self->static_names[i]);
1253 vec_free(self->static_names);
1254 for (i = 0; i < vec_size(self->blocks); ++i)
1255 ast_delete(self->blocks[i]);
1256 vec_free(self->blocks);
1257 vec_free(self->breakblocks);
1258 vec_free(self->continueblocks);
1260 ast_delete(self->varargs);
1262 ast_delete(self->argc);
1263 if (self->fixedparams)
1264 ast_unref(self->fixedparams);
1265 if (self->return_value)
1266 ast_unref(self->return_value);
1270 const char* ast_function_label(ast_function *self, const char *prefix)
1276 if (!OPTS_OPTION_BOOL(OPTION_DUMP) &&
1277 !OPTS_OPTION_BOOL(OPTION_DUMPFIN) &&
1278 !OPTS_OPTION_BOOL(OPTION_DEBUG))
1283 id = (self->labelcount++);
1284 len = strlen(prefix);
1286 from = self->labelbuf + sizeof(self->labelbuf)-1;
1289 *from-- = (id%10) + '0';
1293 memcpy(from - len, prefix, len);
1297 /*********************************************************************/
1299 * by convention you must never pass NULL to the 'ir_value **out'
1300 * parameter. If you really don't care about the output, pass a dummy.
1301 * But I can't imagine a pituation where the output is truly unnecessary.
1304 static void _ast_codegen_output_type(ast_expression *self, ir_value *out)
1306 if (out->vtype == TYPE_FIELD)
1307 out->fieldtype = self->next->vtype;
1308 if (out->vtype == TYPE_FUNCTION)
1309 out->outtype = self->next->vtype;
1312 #define codegen_output_type(a,o) (_ast_codegen_output_type(&((a)->expression),(o)))
1314 bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out)
1318 if (self->expression.vtype == TYPE_NIL) {
1319 *out = func->ir_func->owner->nil;
1322 /* NOTE: This is the codegen for a variable used in an expression.
1323 * It is not the codegen to generate the value. For this purpose,
1324 * ast_local_codegen and ast_global_codegen are to be used before this
1325 * is executed. ast_function_codegen should take care of its locals,
1326 * and the ast-user should take care of ast_global_codegen to be used
1327 * on all the globals.
1330 char tname[1024]; /* typename is reserved in C++ */
1331 ast_type_to_string((ast_expression*)self, tname, sizeof(tname));
1332 compile_error(ast_ctx(self), "ast_value used before generated %s %s", tname, self->name);
1339 static bool ast_global_array_set(ast_value *self)
1341 size_t count = vec_size(self->initlist);
1344 if (count > self->expression.count) {
1345 compile_error(ast_ctx(self), "too many elements in initializer");
1346 count = self->expression.count;
1348 else if (count < self->expression.count) {
1350 compile_warning(ast_ctx(self), "not all elements are initialized");
1354 for (i = 0; i != count; ++i) {
1355 switch (self->expression.next->vtype) {
1357 if (!ir_value_set_float(self->ir_values[i], self->initlist[i].vfloat))
1361 if (!ir_value_set_vector(self->ir_values[i], self->initlist[i].vvec))
1365 if (!ir_value_set_string(self->ir_values[i], self->initlist[i].vstring))
1369 /* we don't support them in any other place yet either */
1370 compile_error(ast_ctx(self), "TODO: nested arrays");
1373 /* this requiers a bit more work - similar to the fields I suppose */
1374 compile_error(ast_ctx(self), "global of type function not properly generated");
1377 if (!self->initlist[i].vfield) {
1378 compile_error(ast_ctx(self), "field constant without vfield set");
1381 if (!self->initlist[i].vfield->ir_v) {
1382 compile_error(ast_ctx(self), "field constant generated before its field");
1385 if (!ir_value_set_field(self->ir_values[i], self->initlist[i].vfield->ir_v))
1389 compile_error(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1396 static bool check_array(ast_value *self, ast_value *array)
1398 if (array->expression.flags & AST_FLAG_ARRAY_INIT && !array->initlist) {
1399 compile_error(ast_ctx(self), "array without size: %s", self->name);
1402 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1403 if (!array->expression.count || array->expression.count > OPTS_OPTION_U32(OPTION_MAX_ARRAY_SIZE)) {
1404 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)array->expression.count);
1410 bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
1414 if (self->expression.vtype == TYPE_NIL) {
1415 compile_error(ast_ctx(self), "internal error: trying to generate a variable of TYPE_NIL");
1419 if (self->hasvalue && self->expression.vtype == TYPE_FUNCTION)
1421 ir_function *func = ir_builder_create_function(ir, self->name, self->expression.next->vtype);
1424 func->context = ast_ctx(self);
1425 func->value->context = ast_ctx(self);
1427 self->constval.vfunc->ir_func = func;
1428 self->ir_v = func->value;
1429 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1430 self->ir_v->flags |= IR_FLAG_INCLUDE_DEF;
1431 if (self->expression.flags & AST_FLAG_ERASEABLE)
1432 self->ir_v->flags |= IR_FLAG_ERASEABLE;
1433 /* The function is filled later on ast_function_codegen... */
1437 if (isfield && self->expression.vtype == TYPE_FIELD) {
1438 ast_expression *fieldtype = self->expression.next;
1440 if (self->hasvalue) {
1441 compile_error(ast_ctx(self), "TODO: constant field pointers with value");
1445 if (fieldtype->vtype == TYPE_ARRAY) {
1450 ast_expression *elemtype;
1452 ast_value *array = (ast_value*)fieldtype;
1454 if (!ast_istype(fieldtype, ast_value)) {
1455 compile_error(ast_ctx(self), "internal error: ast_value required");
1459 if (!check_array(self, array))
1462 elemtype = array->expression.next;
1463 vtype = elemtype->vtype;
1465 v = ir_builder_create_field(ir, self->name, vtype);
1467 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", self->name);
1470 v->context = ast_ctx(self);
1471 v->unique_life = true;
1473 array->ir_v = self->ir_v = v;
1475 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1476 self->ir_v->flags |= IR_FLAG_INCLUDE_DEF;
1477 if (self->expression.flags & AST_FLAG_ERASEABLE)
1478 self->ir_v->flags |= IR_FLAG_ERASEABLE;
1480 namelen = strlen(self->name);
1481 name = (char*)mem_a(namelen + 16);
1482 util_strncpy(name, self->name, namelen);
1484 array->ir_values = (ir_value**)mem_a(sizeof(array->ir_values[0]) * array->expression.count);
1485 array->ir_values[0] = v;
1486 for (ai = 1; ai < array->expression.count; ++ai) {
1487 util_snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1488 array->ir_values[ai] = ir_builder_create_field(ir, name, vtype);
1489 if (!array->ir_values[ai]) {
1491 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", name);
1494 array->ir_values[ai]->context = ast_ctx(self);
1495 array->ir_values[ai]->unique_life = true;
1496 array->ir_values[ai]->locked = true;
1497 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1498 self->ir_values[ai]->flags |= IR_FLAG_INCLUDE_DEF;
1504 v = ir_builder_create_field(ir, self->name, self->expression.next->vtype);
1507 v->context = ast_ctx(self);
1509 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1510 self->ir_v->flags |= IR_FLAG_INCLUDE_DEF;
1512 if (self->expression.flags & AST_FLAG_ERASEABLE)
1513 self->ir_v->flags |= IR_FLAG_ERASEABLE;
1518 if (self->expression.vtype == TYPE_ARRAY) {
1523 ast_expression *elemtype = self->expression.next;
1524 int vtype = elemtype->vtype;
1526 if (self->expression.flags & AST_FLAG_ARRAY_INIT && !self->expression.count) {
1527 compile_error(ast_ctx(self), "array `%s' has no size", self->name);
1531 /* same as with field arrays */
1532 if (!check_array(self, self))
1535 v = ir_builder_create_global(ir, self->name, vtype);
1537 compile_error(ast_ctx(self), "ir_builder_create_global failed `%s`", self->name);
1540 v->context = ast_ctx(self);
1541 v->unique_life = true;
1544 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1545 v->flags |= IR_FLAG_INCLUDE_DEF;
1546 if (self->expression.flags & AST_FLAG_ERASEABLE)
1547 self->ir_v->flags |= IR_FLAG_ERASEABLE;
1549 namelen = strlen(self->name);
1550 name = (char*)mem_a(namelen + 16);
1551 util_strncpy(name, self->name, namelen);
1553 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1554 self->ir_values[0] = v;
1555 for (ai = 1; ai < self->expression.count; ++ai) {
1556 util_snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1557 self->ir_values[ai] = ir_builder_create_global(ir, name, vtype);
1558 if (!self->ir_values[ai]) {
1560 compile_error(ast_ctx(self), "ir_builder_create_global failed `%s`", name);
1563 self->ir_values[ai]->context = ast_ctx(self);
1564 self->ir_values[ai]->unique_life = true;
1565 self->ir_values[ai]->locked = true;
1566 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1567 self->ir_values[ai]->flags |= IR_FLAG_INCLUDE_DEF;
1573 /* Arrays don't do this since there's no "array" value which spans across the
1576 v = ir_builder_create_global(ir, self->name, self->expression.vtype);
1578 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", self->name);
1581 codegen_output_type(self, v);
1582 v->context = ast_ctx(self);
1585 /* link us to the ir_value */
1589 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1590 self->ir_v->flags |= IR_FLAG_INCLUDE_DEF;
1591 if (self->expression.flags & AST_FLAG_ERASEABLE)
1592 self->ir_v->flags |= IR_FLAG_ERASEABLE;
1595 if (self->hasvalue) {
1596 switch (self->expression.vtype)
1599 if (!ir_value_set_float(v, self->constval.vfloat))
1603 if (!ir_value_set_vector(v, self->constval.vvec))
1607 if (!ir_value_set_string(v, self->constval.vstring))
1611 ast_global_array_set(self);
1614 compile_error(ast_ctx(self), "global of type function not properly generated");
1616 /* Cannot generate an IR value for a function,
1617 * need a pointer pointing to a function rather.
1620 if (!self->constval.vfield) {
1621 compile_error(ast_ctx(self), "field constant without vfield set");
1624 if (!self->constval.vfield->ir_v) {
1625 compile_error(ast_ctx(self), "field constant generated before its field");
1628 if (!ir_value_set_field(v, self->constval.vfield->ir_v))
1632 compile_error(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1638 error: /* clean up */
1639 if(v) ir_value_delete(v);
1643 static bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
1647 if (self->expression.vtype == TYPE_NIL) {
1648 compile_error(ast_ctx(self), "internal error: trying to generate a variable of TYPE_NIL");
1652 if (self->hasvalue && self->expression.vtype == TYPE_FUNCTION)
1654 /* Do we allow local functions? I think not...
1655 * this is NOT a function pointer atm.
1660 if (self->expression.vtype == TYPE_ARRAY) {
1665 ast_expression *elemtype = self->expression.next;
1666 int vtype = elemtype->vtype;
1668 func->flags |= IR_FLAG_HAS_ARRAYS;
1670 if (param && !(self->expression.flags & AST_FLAG_IS_VARARG)) {
1671 compile_error(ast_ctx(self), "array-parameters are not supported");
1675 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1676 if (!check_array(self, self))
1679 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1680 if (!self->ir_values) {
1681 compile_error(ast_ctx(self), "failed to allocate array values");
1685 v = ir_function_create_local(func, self->name, vtype, param);
1687 compile_error(ast_ctx(self), "internal error: ir_function_create_local failed");
1690 v->context = ast_ctx(self);
1691 v->unique_life = true;
1694 namelen = strlen(self->name);
1695 name = (char*)mem_a(namelen + 16);
1696 util_strncpy(name, self->name, namelen);
1698 self->ir_values[0] = v;
1699 for (ai = 1; ai < self->expression.count; ++ai) {
1700 util_snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1701 self->ir_values[ai] = ir_function_create_local(func, name, vtype, param);
1702 if (!self->ir_values[ai]) {
1703 compile_error(ast_ctx(self), "internal_error: ir_builder_create_global failed on `%s`", name);
1706 self->ir_values[ai]->context = ast_ctx(self);
1707 self->ir_values[ai]->unique_life = true;
1708 self->ir_values[ai]->locked = true;
1714 v = ir_function_create_local(func, self->name, self->expression.vtype, param);
1717 codegen_output_type(self, v);
1718 v->context = ast_ctx(self);
1721 /* A constant local... hmmm...
1722 * I suppose the IR will have to deal with this
1724 if (self->hasvalue) {
1725 switch (self->expression.vtype)
1728 if (!ir_value_set_float(v, self->constval.vfloat))
1732 if (!ir_value_set_vector(v, self->constval.vvec))
1736 if (!ir_value_set_string(v, self->constval.vstring))
1740 compile_error(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1745 /* link us to the ir_value */
1749 if (!ast_generate_accessors(self, func->owner))
1753 error: /* clean up */
1758 bool ast_generate_accessors(ast_value *self, ir_builder *ir)
1761 bool warn = OPTS_WARN(WARN_USED_UNINITIALIZED);
1762 if (!self->setter || !self->getter)
1764 for (i = 0; i < self->expression.count; ++i) {
1765 if (!self->ir_values) {
1766 compile_error(ast_ctx(self), "internal error: no array values generated for `%s`", self->name);
1769 if (!self->ir_values[i]) {
1770 compile_error(ast_ctx(self), "internal error: not all array values have been generated for `%s`", self->name);
1773 if (self->ir_values[i]->life) {
1774 compile_error(ast_ctx(self), "internal error: function containing `%s` already generated", self->name);
1779 opts_set(opts.warn, WARN_USED_UNINITIALIZED, false);
1781 if (!ast_global_codegen (self->setter, ir, false) ||
1782 !ast_function_codegen(self->setter->constval.vfunc, ir) ||
1783 !ir_function_finalize(self->setter->constval.vfunc->ir_func))
1785 compile_error(ast_ctx(self), "internal error: failed to generate setter for `%s`", self->name);
1786 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1791 if (!ast_global_codegen (self->getter, ir, false) ||
1792 !ast_function_codegen(self->getter->constval.vfunc, ir) ||
1793 !ir_function_finalize(self->getter->constval.vfunc->ir_func))
1795 compile_error(ast_ctx(self), "internal error: failed to generate getter for `%s`", self->name);
1796 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1800 for (i = 0; i < self->expression.count; ++i) {
1801 vec_free(self->ir_values[i]->life);
1803 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1807 bool ast_function_codegen(ast_function *self, ir_builder *ir)
1812 ast_expression_codegen *cgen;
1818 irf = self->ir_func;
1820 compile_error(ast_ctx(self), "internal error: ast_function's related ast_value was not generated yet");
1824 /* fill the parameter list */
1825 ec = &self->vtype->expression;
1826 for (i = 0; i < vec_size(ec->params); ++i)
1828 if (ec->params[i]->expression.vtype == TYPE_FIELD)
1829 vec_push(irf->params, ec->params[i]->expression.next->vtype);
1831 vec_push(irf->params, ec->params[i]->expression.vtype);
1832 if (!self->builtin) {
1833 if (!ast_local_codegen(ec->params[i], self->ir_func, true))
1838 if (self->varargs) {
1839 if (!ast_local_codegen(self->varargs, self->ir_func, true))
1841 irf->max_varargs = self->varargs->expression.count;
1844 if (self->builtin) {
1845 irf->builtin = self->builtin;
1849 /* have a local return value variable? */
1850 if (self->return_value) {
1851 if (!ast_local_codegen(self->return_value, self->ir_func, false))
1855 if (!vec_size(self->blocks)) {
1856 compile_error(ast_ctx(self), "function `%s` has no body", self->name);
1860 irf->first = self->curblock = ir_function_create_block(ast_ctx(self), irf, "entry");
1861 if (!self->curblock) {
1862 compile_error(ast_ctx(self), "failed to allocate entry block for `%s`", self->name);
1870 if (!ast_local_codegen(self->argc, self->ir_func, true))
1872 cgen = self->argc->expression.codegen;
1873 if (!(*cgen)((ast_expression*)(self->argc), self, false, &va_count))
1875 cgen = self->fixedparams->expression.codegen;
1876 if (!(*cgen)((ast_expression*)(self->fixedparams), self, false, &fixed))
1878 sub = ir_block_create_binop(self->curblock, ast_ctx(self),
1879 ast_function_label(self, "va_count"), INSTR_SUB_F,
1880 ir_builder_get_va_count(ir), fixed);
1883 if (!ir_block_create_store_op(self->curblock, ast_ctx(self), INSTR_STORE_F,
1890 for (i = 0; i < vec_size(self->blocks); ++i) {
1891 cgen = self->blocks[i]->expression.codegen;
1892 if (!(*cgen)((ast_expression*)self->blocks[i], self, false, &dummy))
1896 /* TODO: check return types */
1897 if (!self->curblock->final)
1899 if (!self->vtype->expression.next ||
1900 self->vtype->expression.next->vtype == TYPE_VOID)
1902 return ir_block_create_return(self->curblock, ast_ctx(self), NULL);
1904 else if (vec_size(self->curblock->entries) || self->curblock == irf->first)
1906 if (self->return_value) {
1907 cgen = self->return_value->expression.codegen;
1908 if (!(*cgen)((ast_expression*)(self->return_value), self, false, &dummy))
1910 return ir_block_create_return(self->curblock, ast_ctx(self), dummy);
1912 else if (compile_warning(ast_ctx(self), WARN_MISSING_RETURN_VALUES,
1913 "control reaches end of non-void function (`%s`) via %s",
1914 self->name, self->curblock->label))
1918 return ir_block_create_return(self->curblock, ast_ctx(self), NULL);
1924 static bool starts_a_label(ast_expression *ex)
1926 while (ex && ast_istype(ex, ast_block)) {
1927 ast_block *b = (ast_block*)ex;
1932 return ast_istype(ex, ast_label);
1935 /* Note, you will not see ast_block_codegen generate ir_blocks.
1936 * To the AST and the IR, blocks are 2 different things.
1937 * In the AST it represents a block of code, usually enclosed in
1938 * curly braces {...}.
1939 * While in the IR it represents a block in terms of control-flow.
1941 bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_value **out)
1945 /* We don't use this
1946 * Note: an ast-representation using the comma-operator
1947 * of the form: (a, b, c) = x should not assign to c...
1950 compile_error(ast_ctx(self), "not an l-value (code-block)");
1954 if (self->expression.outr) {
1955 *out = self->expression.outr;
1959 /* output is NULL at first, we'll have each expression
1960 * assign to out output, thus, a comma-operator represention
1961 * using an ast_block will return the last generated value,
1962 * so: (b, c) + a executed both b and c, and returns c,
1963 * which is then added to a.
1967 /* generate locals */
1968 for (i = 0; i < vec_size(self->locals); ++i)
1970 if (!ast_local_codegen(self->locals[i], func->ir_func, false)) {
1971 if (OPTS_OPTION_BOOL(OPTION_DEBUG))
1972 compile_error(ast_ctx(self), "failed to generate local `%s`", self->locals[i]->name);
1977 for (i = 0; i < vec_size(self->exprs); ++i)
1979 ast_expression_codegen *gen;
1980 if (func->curblock->final && !starts_a_label(self->exprs[i])) {
1981 if (compile_warning(ast_ctx(self->exprs[i]), WARN_UNREACHABLE_CODE, "unreachable statement"))
1985 gen = self->exprs[i]->codegen;
1986 if (!(*gen)(self->exprs[i], func, false, out))
1990 self->expression.outr = *out;
1995 bool ast_store_codegen(ast_store *self, ast_function *func, bool lvalue, ir_value **out)
1997 ast_expression_codegen *cgen;
1998 ir_value *left = NULL;
1999 ir_value *right = NULL;
2003 ast_array_index *ai = NULL;
2005 if (lvalue && self->expression.outl) {
2006 *out = self->expression.outl;
2010 if (!lvalue && self->expression.outr) {
2011 *out = self->expression.outr;
2015 if (ast_istype(self->dest, ast_array_index))
2018 ai = (ast_array_index*)self->dest;
2019 idx = (ast_value*)ai->index;
2021 if (ast_istype(ai->index, ast_value) && idx->hasvalue && idx->cvq == CV_CONST)
2026 /* we need to call the setter */
2027 ir_value *iridx, *funval;
2031 compile_error(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
2035 arr = (ast_value*)ai->array;
2036 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
2037 compile_error(ast_ctx(self), "value has no setter (%s)", arr->name);
2041 cgen = idx->expression.codegen;
2042 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
2045 cgen = arr->setter->expression.codegen;
2046 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
2049 cgen = self->source->codegen;
2050 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
2053 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "store"), funval, false);
2056 ir_call_param(call, iridx);
2057 ir_call_param(call, right);
2058 self->expression.outr = right;
2064 cgen = self->dest->codegen;
2066 if (!(*cgen)((ast_expression*)(self->dest), func, true, &left))
2068 self->expression.outl = left;
2070 cgen = self->source->codegen;
2072 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
2075 if (!ir_block_create_store_op(func->curblock, ast_ctx(self), self->op, left, right))
2077 self->expression.outr = right;
2080 /* Theoretically, an assinment returns its left side as an
2081 * lvalue, if we don't need an lvalue though, we return
2082 * the right side as an rvalue, otherwise we have to
2083 * somehow know whether or not we need to dereference the pointer
2084 * on the left side - that is: OP_LOAD if it was an address.
2085 * Also: in original QC we cannot OP_LOADP *anyway*.
2087 *out = (lvalue ? left : right);
2092 bool ast_binary_codegen(ast_binary *self, ast_function *func, bool lvalue, ir_value **out)
2094 ast_expression_codegen *cgen;
2095 ir_value *left, *right;
2097 /* A binary operation cannot yield an l-value */
2099 compile_error(ast_ctx(self), "not an l-value (binop)");
2103 if (self->expression.outr) {
2104 *out = self->expression.outr;
2108 if ((OPTS_FLAG(SHORT_LOGIC) || OPTS_FLAG(PERL_LOGIC)) &&
2109 (self->op == INSTR_AND || self->op == INSTR_OR))
2111 /* NOTE: The short-logic path will ignore right_first */
2113 /* short circuit evaluation */
2114 ir_block *other, *merge;
2115 ir_block *from_left, *from_right;
2119 /* prepare end-block */
2120 merge_id = vec_size(func->ir_func->blocks);
2121 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "sce_merge"));
2123 /* generate the left expression */
2124 cgen = self->left->codegen;
2125 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
2127 /* remember the block */
2128 from_left = func->curblock;
2130 /* create a new block for the right expression */
2131 other = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "sce_other"));
2132 if (self->op == INSTR_AND) {
2133 /* on AND: left==true -> other */
2134 if (!ir_block_create_if(func->curblock, ast_ctx(self), left, other, merge))
2137 /* on OR: left==false -> other */
2138 if (!ir_block_create_if(func->curblock, ast_ctx(self), left, merge, other))
2141 /* use the likely flag */
2142 vec_last(func->curblock->instr)->likely = true;
2144 /* enter the right-expression's block */
2145 func->curblock = other;
2147 cgen = self->right->codegen;
2148 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
2150 /* remember block */
2151 from_right = func->curblock;
2153 /* jump to the merge block */
2154 if (!ir_block_create_jump(func->curblock, ast_ctx(self), merge))
2157 vec_remove(func->ir_func->blocks, merge_id, 1);
2158 vec_push(func->ir_func->blocks, merge);
2160 func->curblock = merge;
2161 phi = ir_block_create_phi(func->curblock, ast_ctx(self),
2162 ast_function_label(func, "sce_value"),
2163 self->expression.vtype);
2164 ir_phi_add(phi, from_left, left);
2165 ir_phi_add(phi, from_right, right);
2166 *out = ir_phi_value(phi);
2170 if (!OPTS_FLAG(PERL_LOGIC)) {
2172 if (OPTS_FLAG(CORRECT_LOGIC) && (*out)->vtype == TYPE_VECTOR) {
2173 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
2174 ast_function_label(func, "sce_bool_v"),
2178 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
2179 ast_function_label(func, "sce_bool"),
2184 else if (OPTS_FLAG(FALSE_EMPTY_STRINGS) && (*out)->vtype == TYPE_STRING) {
2185 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
2186 ast_function_label(func, "sce_bool_s"),
2190 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
2191 ast_function_label(func, "sce_bool"),
2197 *out = ir_block_create_binop(func->curblock, ast_ctx(self),
2198 ast_function_label(func, "sce_bool"),
2199 INSTR_AND, *out, *out);
2205 self->expression.outr = *out;
2206 codegen_output_type(self, *out);
2210 if (self->right_first) {
2211 cgen = self->right->codegen;
2212 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
2214 cgen = self->left->codegen;
2215 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
2218 cgen = self->left->codegen;
2219 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
2221 cgen = self->right->codegen;
2222 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
2226 *out = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "bin"),
2227 self->op, left, right);
2230 self->expression.outr = *out;
2231 codegen_output_type(self, *out);
2236 bool ast_binstore_codegen(ast_binstore *self, ast_function *func, bool lvalue, ir_value **out)
2238 ast_expression_codegen *cgen;
2239 ir_value *leftl = NULL, *leftr, *right, *bin;
2243 ast_array_index *ai = NULL;
2244 ir_value *iridx = NULL;
2246 if (lvalue && self->expression.outl) {
2247 *out = self->expression.outl;
2251 if (!lvalue && self->expression.outr) {
2252 *out = self->expression.outr;
2256 if (ast_istype(self->dest, ast_array_index))
2259 ai = (ast_array_index*)self->dest;
2260 idx = (ast_value*)ai->index;
2262 if (ast_istype(ai->index, ast_value) && idx->hasvalue && idx->cvq == CV_CONST)
2266 /* for a binstore we need both an lvalue and an rvalue for the left side */
2267 /* rvalue of destination! */
2269 cgen = idx->expression.codegen;
2270 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
2273 cgen = self->dest->codegen;
2274 if (!(*cgen)((ast_expression*)(self->dest), func, false, &leftr))
2277 /* source as rvalue only */
2278 cgen = self->source->codegen;
2279 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
2282 /* now the binary */
2283 bin = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "binst"),
2284 self->opbin, leftr, right);
2285 self->expression.outr = bin;
2289 /* we need to call the setter */
2294 compile_error(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
2298 arr = (ast_value*)ai->array;
2299 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
2300 compile_error(ast_ctx(self), "value has no setter (%s)", arr->name);
2304 cgen = arr->setter->expression.codegen;
2305 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
2308 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "store"), funval, false);
2311 ir_call_param(call, iridx);
2312 ir_call_param(call, bin);
2313 self->expression.outr = bin;
2315 /* now store them */
2316 cgen = self->dest->codegen;
2317 /* lvalue of destination */
2318 if (!(*cgen)((ast_expression*)(self->dest), func, true, &leftl))
2320 self->expression.outl = leftl;
2322 if (!ir_block_create_store_op(func->curblock, ast_ctx(self), self->opstore, leftl, bin))
2324 self->expression.outr = bin;
2327 /* Theoretically, an assinment returns its left side as an
2328 * lvalue, if we don't need an lvalue though, we return
2329 * the right side as an rvalue, otherwise we have to
2330 * somehow know whether or not we need to dereference the pointer
2331 * on the left side - that is: OP_LOAD if it was an address.
2332 * Also: in original QC we cannot OP_LOADP *anyway*.
2334 *out = (lvalue ? leftl : bin);
2339 bool ast_unary_codegen(ast_unary *self, ast_function *func, bool lvalue, ir_value **out)
2341 ast_expression_codegen *cgen;
2344 /* An unary operation cannot yield an l-value */
2346 compile_error(ast_ctx(self), "not an l-value (binop)");
2350 if (self->expression.outr) {
2351 *out = self->expression.outr;
2355 cgen = self->operand->codegen;
2357 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
2360 *out = ir_block_create_unary(func->curblock, ast_ctx(self), ast_function_label(func, "unary"),
2364 self->expression.outr = *out;
2369 bool ast_return_codegen(ast_return *self, ast_function *func, bool lvalue, ir_value **out)
2371 ast_expression_codegen *cgen;
2376 /* In the context of a return operation, we don't actually return
2380 compile_error(ast_ctx(self), "return-expression is not an l-value");
2384 if (self->expression.outr) {
2385 compile_error(ast_ctx(self), "internal error: ast_return cannot be reused, it bears no result!");
2388 self->expression.outr = (ir_value*)1;
2390 if (self->operand) {
2391 cgen = self->operand->codegen;
2393 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
2396 if (!ir_block_create_return(func->curblock, ast_ctx(self), operand))
2399 if (!ir_block_create_return(func->curblock, ast_ctx(self), NULL))
2406 bool ast_entfield_codegen(ast_entfield *self, ast_function *func, bool lvalue, ir_value **out)
2408 ast_expression_codegen *cgen;
2409 ir_value *ent, *field;
2411 /* This function needs to take the 'lvalue' flag into account!
2412 * As lvalue we provide a field-pointer, as rvalue we provide the
2416 if (lvalue && self->expression.outl) {
2417 *out = self->expression.outl;
2421 if (!lvalue && self->expression.outr) {
2422 *out = self->expression.outr;
2426 cgen = self->entity->codegen;
2427 if (!(*cgen)((ast_expression*)(self->entity), func, false, &ent))
2430 cgen = self->field->codegen;
2431 if (!(*cgen)((ast_expression*)(self->field), func, false, &field))
2436 *out = ir_block_create_fieldaddress(func->curblock, ast_ctx(self), ast_function_label(func, "efa"),
2439 *out = ir_block_create_load_from_ent(func->curblock, ast_ctx(self), ast_function_label(func, "efv"),
2440 ent, field, self->expression.vtype);
2441 /* Done AFTER error checking:
2442 codegen_output_type(self, *out);
2446 compile_error(ast_ctx(self), "failed to create %s instruction (output type %s)",
2447 (lvalue ? "ADDRESS" : "FIELD"),
2448 type_name[self->expression.vtype]);
2452 codegen_output_type(self, *out);
2455 self->expression.outl = *out;
2457 self->expression.outr = *out;
2459 /* Hm that should be it... */
2463 bool ast_member_codegen(ast_member *self, ast_function *func, bool lvalue, ir_value **out)
2465 ast_expression_codegen *cgen;
2468 /* in QC this is always an lvalue */
2469 if (lvalue && self->rvalue) {
2470 compile_error(ast_ctx(self), "not an l-value (member access)");
2473 if (self->expression.outl) {
2474 *out = self->expression.outl;
2478 cgen = self->owner->codegen;
2479 if (!(*cgen)((ast_expression*)(self->owner), func, false, &vec))
2482 if (vec->vtype != TYPE_VECTOR &&
2483 !(vec->vtype == TYPE_FIELD && self->owner->next->vtype == TYPE_VECTOR))
2488 *out = ir_value_vector_member(vec, self->field);
2489 self->expression.outl = *out;
2491 return (*out != NULL);
2494 bool ast_array_index_codegen(ast_array_index *self, ast_function *func, bool lvalue, ir_value **out)
2499 if (!lvalue && self->expression.outr) {
2500 *out = self->expression.outr;
2503 if (lvalue && self->expression.outl) {
2504 *out = self->expression.outl;
2508 if (!ast_istype(self->array, ast_value)) {
2509 compile_error(ast_ctx(self), "array indexing this way is not supported");
2510 /* note this would actually be pointer indexing because the left side is
2511 * not an actual array but (hopefully) an indexable expression.
2512 * Once we get integer arithmetic, and GADDRESS/GSTORE/GLOAD instruction
2513 * support this path will be filled.
2518 arr = (ast_value*)self->array;
2519 idx = (ast_value*)self->index;
2521 if (!ast_istype(self->index, ast_value) || !idx->hasvalue || idx->cvq != CV_CONST) {
2522 /* Time to use accessor functions */
2523 ast_expression_codegen *cgen;
2524 ir_value *iridx, *funval;
2528 compile_error(ast_ctx(self), "(.2) array indexing here needs a compile-time constant");
2533 compile_error(ast_ctx(self), "value has no getter, don't know how to index it");
2537 cgen = self->index->codegen;
2538 if (!(*cgen)((ast_expression*)(self->index), func, false, &iridx))
2541 cgen = arr->getter->expression.codegen;
2542 if (!(*cgen)((ast_expression*)(arr->getter), func, true, &funval))
2545 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "fetch"), funval, false);
2548 ir_call_param(call, iridx);
2550 *out = ir_call_value(call);
2551 self->expression.outr = *out;
2552 (*out)->vtype = self->expression.vtype;
2553 codegen_output_type(self, *out);
2557 if (idx->expression.vtype == TYPE_FLOAT) {
2558 unsigned int arridx = idx->constval.vfloat;
2559 if (arridx >= self->array->count)
2561 compile_error(ast_ctx(self), "array index out of bounds: %i", arridx);
2564 *out = arr->ir_values[arridx];
2566 else if (idx->expression.vtype == TYPE_INTEGER) {
2567 unsigned int arridx = idx->constval.vint;
2568 if (arridx >= self->array->count)
2570 compile_error(ast_ctx(self), "array index out of bounds: %i", arridx);
2573 *out = arr->ir_values[arridx];
2576 compile_error(ast_ctx(self), "array indexing here needs an integer constant");
2579 (*out)->vtype = self->expression.vtype;
2580 codegen_output_type(self, *out);
2584 bool ast_argpipe_codegen(ast_argpipe *self, ast_function *func, bool lvalue, ir_value **out)
2588 compile_error(ast_ctx(self), "argpipe node: not an lvalue");
2593 compile_error(ast_ctx(self), "TODO: argpipe codegen not implemented");
2597 bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_value **out)
2599 ast_expression_codegen *cgen;
2607 ir_block *ontrue_endblock = NULL;
2608 ir_block *onfalse_endblock = NULL;
2609 ir_block *merge = NULL;
2612 /* We don't output any value, thus also don't care about r/lvalue */
2616 if (self->expression.outr) {
2617 compile_error(ast_ctx(self), "internal error: ast_ifthen cannot be reused, it bears no result!");
2620 self->expression.outr = (ir_value*)1;
2622 /* generate the condition */
2623 cgen = self->cond->codegen;
2624 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2626 /* update the block which will get the jump - because short-logic or ternaries may have changed this */
2627 cond = func->curblock;
2629 /* try constant folding away the condition */
2630 if ((fold = fold_cond_ifthen(condval, func, self)) != -1)
2633 if (self->on_true) {
2634 /* create on-true block */
2635 ontrue = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "ontrue"));
2639 /* enter the block */
2640 func->curblock = ontrue;
2643 cgen = self->on_true->codegen;
2644 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &dummy))
2647 /* we now need to work from the current endpoint */
2648 ontrue_endblock = func->curblock;
2653 if (self->on_false) {
2654 /* create on-false block */
2655 onfalse = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "onfalse"));
2659 /* enter the block */
2660 func->curblock = onfalse;
2663 cgen = self->on_false->codegen;
2664 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &dummy))
2667 /* we now need to work from the current endpoint */
2668 onfalse_endblock = func->curblock;
2672 /* Merge block were they all merge in to */
2673 if (!ontrue || !onfalse || !ontrue_endblock->final || !onfalse_endblock->final)
2675 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "endif"));
2678 /* add jumps ot the merge block */
2679 if (ontrue && !ontrue_endblock->final && !ir_block_create_jump(ontrue_endblock, ast_ctx(self), merge))
2681 if (onfalse && !onfalse_endblock->final && !ir_block_create_jump(onfalse_endblock, ast_ctx(self), merge))
2684 /* Now enter the merge block */
2685 func->curblock = merge;
2688 /* we create the if here, that way all blocks are ordered :)
2690 if (!ir_block_create_if(cond, ast_ctx(self), condval,
2691 (ontrue ? ontrue : merge),
2692 (onfalse ? onfalse : merge)))
2700 bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_value **out)
2702 ast_expression_codegen *cgen;
2705 ir_value *trueval, *falseval;
2708 ir_block *cond = func->curblock;
2709 ir_block *cond_out = NULL;
2710 ir_block *ontrue, *ontrue_out = NULL;
2711 ir_block *onfalse, *onfalse_out = NULL;
2715 /* Ternary can never create an lvalue... */
2719 /* In theory it shouldn't be possible to pass through a node twice, but
2720 * in case we add any kind of optimization pass for the AST itself, it
2721 * may still happen, thus we remember a created ir_value and simply return one
2722 * if it already exists.
2724 if (self->expression.outr) {
2725 *out = self->expression.outr;
2729 /* In the following, contraty to ast_ifthen, we assume both paths exist. */
2731 /* generate the condition */
2732 func->curblock = cond;
2733 cgen = self->cond->codegen;
2734 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2736 cond_out = func->curblock;
2738 /* try constant folding away the condition */
2739 if ((fold = fold_cond_ternary(condval, func, self)) != -1)
2742 /* create on-true block */
2743 ontrue = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_T"));
2748 /* enter the block */
2749 func->curblock = ontrue;
2752 cgen = self->on_true->codegen;
2753 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &trueval))
2756 ontrue_out = func->curblock;
2759 /* create on-false block */
2760 onfalse = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_F"));
2765 /* enter the block */
2766 func->curblock = onfalse;
2769 cgen = self->on_false->codegen;
2770 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &falseval))
2773 onfalse_out = func->curblock;
2776 /* create merge block */
2777 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_out"));
2780 /* jump to merge block */
2781 if (!ir_block_create_jump(ontrue_out, ast_ctx(self), merge))
2783 if (!ir_block_create_jump(onfalse_out, ast_ctx(self), merge))
2786 /* create if instruction */
2787 if (!ir_block_create_if(cond_out, ast_ctx(self), condval, ontrue, onfalse))
2790 /* Now enter the merge block */
2791 func->curblock = merge;
2793 /* Here, now, we need a PHI node
2794 * but first some sanity checking...
2796 if (trueval->vtype != falseval->vtype && trueval->vtype != TYPE_NIL && falseval->vtype != TYPE_NIL) {
2797 /* error("ternary with different types on the two sides"); */
2798 compile_error(ast_ctx(self), "internal error: ternary operand types invalid");
2803 phi = ir_block_create_phi(merge, ast_ctx(self), ast_function_label(func, "phi"), self->expression.vtype);
2805 compile_error(ast_ctx(self), "internal error: failed to generate phi node");
2808 ir_phi_add(phi, ontrue_out, trueval);
2809 ir_phi_add(phi, onfalse_out, falseval);
2811 self->expression.outr = ir_phi_value(phi);
2812 *out = self->expression.outr;
2814 codegen_output_type(self, *out);
2819 bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value **out)
2821 ast_expression_codegen *cgen;
2823 ir_value *dummy = NULL;
2824 ir_value *precond = NULL;
2825 ir_value *postcond = NULL;
2827 /* Since we insert some jumps "late" so we have blocks
2828 * ordered "nicely", we need to keep track of the actual end-blocks
2829 * of expressions to add the jumps to.
2831 ir_block *bbody = NULL, *end_bbody = NULL;
2832 ir_block *bprecond = NULL, *end_bprecond = NULL;
2833 ir_block *bpostcond = NULL, *end_bpostcond = NULL;
2834 ir_block *bincrement = NULL, *end_bincrement = NULL;
2835 ir_block *bout = NULL, *bin = NULL;
2837 /* let's at least move the outgoing block to the end */
2840 /* 'break' and 'continue' need to be able to find the right blocks */
2841 ir_block *bcontinue = NULL;
2842 ir_block *bbreak = NULL;
2844 ir_block *tmpblock = NULL;
2849 if (self->expression.outr) {
2850 compile_error(ast_ctx(self), "internal error: ast_loop cannot be reused, it bears no result!");
2853 self->expression.outr = (ir_value*)1;
2856 * Should we ever need some kind of block ordering, better make this function
2857 * move blocks around than write a block ordering algorithm later... after all
2858 * the ast and ir should work together, not against each other.
2861 /* initexpr doesn't get its own block, it's pointless, it could create more blocks
2862 * anyway if for example it contains a ternary.
2866 cgen = self->initexpr->codegen;
2867 if (!(*cgen)((ast_expression*)(self->initexpr), func, false, &dummy))
2871 /* Store the block from which we enter this chaos */
2872 bin = func->curblock;
2874 /* The pre-loop condition needs its own block since we
2875 * need to be able to jump to the start of that expression.
2879 bprecond = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "pre_loop_cond"));
2883 /* the pre-loop-condition the least important place to 'continue' at */
2884 bcontinue = bprecond;
2887 func->curblock = bprecond;
2890 cgen = self->precond->codegen;
2891 if (!(*cgen)((ast_expression*)(self->precond), func, false, &precond))
2894 end_bprecond = func->curblock;
2896 bprecond = end_bprecond = NULL;
2899 /* Now the next blocks won't be ordered nicely, but we need to
2900 * generate them this early for 'break' and 'continue'.
2902 if (self->increment) {
2903 bincrement = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "loop_increment"));
2906 bcontinue = bincrement; /* increment comes before the pre-loop-condition */
2908 bincrement = end_bincrement = NULL;
2911 if (self->postcond) {
2912 bpostcond = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "post_loop_cond"));
2915 bcontinue = bpostcond; /* postcond comes before the increment */
2917 bpostcond = end_bpostcond = NULL;
2920 bout_id = vec_size(func->ir_func->blocks);
2921 bout = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "after_loop"));
2926 /* The loop body... */
2927 /* if (self->body) */
2929 bbody = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "loop_body"));
2934 func->curblock = bbody;
2936 vec_push(func->breakblocks, bbreak);
2938 vec_push(func->continueblocks, bcontinue);
2940 vec_push(func->continueblocks, bbody);
2944 cgen = self->body->codegen;
2945 if (!(*cgen)((ast_expression*)(self->body), func, false, &dummy))
2949 end_bbody = func->curblock;
2950 vec_pop(func->breakblocks);
2951 vec_pop(func->continueblocks);
2954 /* post-loop-condition */
2958 func->curblock = bpostcond;
2961 cgen = self->postcond->codegen;
2962 if (!(*cgen)((ast_expression*)(self->postcond), func, false, &postcond))
2965 end_bpostcond = func->curblock;
2968 /* The incrementor */
2969 if (self->increment)
2972 func->curblock = bincrement;
2975 cgen = self->increment->codegen;
2976 if (!(*cgen)((ast_expression*)(self->increment), func, false, &dummy))
2979 end_bincrement = func->curblock;
2982 /* In any case now, we continue from the outgoing block */
2983 func->curblock = bout;
2985 /* Now all blocks are in place */
2986 /* From 'bin' we jump to whatever comes first */
2987 if (bprecond) tmpblock = bprecond;
2988 else tmpblock = bbody; /* can never be null */
2991 else if (bpostcond) tmpblock = bpostcond;
2992 else tmpblock = bout;
2995 if (!ir_block_create_jump(bin, ast_ctx(self), tmpblock))
3001 ir_block *ontrue, *onfalse;
3002 ontrue = bbody; /* can never be null */
3004 /* all of this is dead code
3005 else if (bincrement) ontrue = bincrement;
3006 else ontrue = bpostcond;
3010 if (self->pre_not) {
3015 if (!ir_block_create_if(end_bprecond, ast_ctx(self), precond, ontrue, onfalse))
3022 if (bincrement) tmpblock = bincrement;
3023 else if (bpostcond) tmpblock = bpostcond;
3024 else if (bprecond) tmpblock = bprecond;
3025 else tmpblock = bbody;
3026 if (!end_bbody->final && !ir_block_create_jump(end_bbody, ast_ctx(self), tmpblock))
3030 /* from increment */
3033 if (bpostcond) tmpblock = bpostcond;
3034 else if (bprecond) tmpblock = bprecond;
3035 else if (bbody) tmpblock = bbody;
3036 else tmpblock = bout;
3037 if (!ir_block_create_jump(end_bincrement, ast_ctx(self), tmpblock))
3044 ir_block *ontrue, *onfalse;
3045 if (bprecond) ontrue = bprecond;
3046 else ontrue = bbody; /* can never be null */
3048 /* all of this is dead code
3049 else if (bincrement) ontrue = bincrement;
3050 else ontrue = bpostcond;
3054 if (self->post_not) {
3059 if (!ir_block_create_if(end_bpostcond, ast_ctx(self), postcond, ontrue, onfalse))
3063 /* Move 'bout' to the end */
3064 vec_remove(func->ir_func->blocks, bout_id, 1);
3065 vec_push(func->ir_func->blocks, bout);
3070 bool ast_breakcont_codegen(ast_breakcont *self, ast_function *func, bool lvalue, ir_value **out)
3077 compile_error(ast_ctx(self), "break/continue expression is not an l-value");
3081 if (self->expression.outr) {
3082 compile_error(ast_ctx(self), "internal error: ast_breakcont cannot be reused!");
3085 self->expression.outr = (ir_value*)1;
3087 if (self->is_continue)
3088 target = func->continueblocks[vec_size(func->continueblocks)-1-self->levels];
3090 target = func->breakblocks[vec_size(func->breakblocks)-1-self->levels];
3093 compile_error(ast_ctx(self), "%s is lacking a target block", (self->is_continue ? "continue" : "break"));
3097 if (!ir_block_create_jump(func->curblock, ast_ctx(self), target))
3102 bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_value **out)
3104 ast_expression_codegen *cgen;
3106 ast_switch_case *def_case = NULL;
3107 ir_block *def_bfall = NULL;
3108 ir_block *def_bfall_to = NULL;
3109 bool set_def_bfall_to = false;
3111 ir_value *dummy = NULL;
3112 ir_value *irop = NULL;
3113 ir_block *bout = NULL;
3114 ir_block *bfall = NULL;
3122 compile_error(ast_ctx(self), "switch expression is not an l-value");
3126 if (self->expression.outr) {
3127 compile_error(ast_ctx(self), "internal error: ast_switch cannot be reused!");
3130 self->expression.outr = (ir_value*)1;
3135 cgen = self->operand->codegen;
3136 if (!(*cgen)((ast_expression*)(self->operand), func, false, &irop))
3139 if (!vec_size(self->cases))
3142 cmpinstr = type_eq_instr[irop->vtype];
3143 if (cmpinstr >= VINSTR_END) {
3144 ast_type_to_string(self->operand, typestr, sizeof(typestr));
3145 compile_error(ast_ctx(self), "invalid type to perform a switch on: %s", typestr);
3149 bout_id = vec_size(func->ir_func->blocks);
3150 bout = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "after_switch"));
3154 /* setup the break block */
3155 vec_push(func->breakblocks, bout);
3157 /* Now create all cases */
3158 for (c = 0; c < vec_size(self->cases); ++c) {
3159 ir_value *cond, *val;
3160 ir_block *bcase, *bnot;
3163 ast_switch_case *swcase = &self->cases[c];
3165 if (swcase->value) {
3166 /* A regular case */
3167 /* generate the condition operand */
3168 cgen = swcase->value->codegen;
3169 if (!(*cgen)((ast_expression*)(swcase->value), func, false, &val))
3171 /* generate the condition */
3172 cond = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "switch_eq"), cmpinstr, irop, val);
3176 bcase = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "case"));
3177 bnot_id = vec_size(func->ir_func->blocks);
3178 bnot = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "not_case"));
3179 if (!bcase || !bnot)
3181 if (set_def_bfall_to) {
3182 set_def_bfall_to = false;
3183 def_bfall_to = bcase;
3185 if (!ir_block_create_if(func->curblock, ast_ctx(self), cond, bcase, bnot))
3188 /* Make the previous case-end fall through */
3189 if (bfall && !bfall->final) {
3190 if (!ir_block_create_jump(bfall, ast_ctx(self), bcase))
3194 /* enter the case */
3195 func->curblock = bcase;
3196 cgen = swcase->code->codegen;
3197 if (!(*cgen)((ast_expression*)swcase->code, func, false, &dummy))
3200 /* remember this block to fall through from */
3201 bfall = func->curblock;
3203 /* enter the else and move it down */
3204 func->curblock = bnot;
3205 vec_remove(func->ir_func->blocks, bnot_id, 1);
3206 vec_push(func->ir_func->blocks, bnot);
3208 /* The default case */
3209 /* Remember where to fall through from: */
3212 /* remember which case it was */
3214 /* And the next case will be remembered */
3215 set_def_bfall_to = true;
3219 /* Jump from the last bnot to bout */
3220 if (bfall && !bfall->final && !ir_block_create_jump(bfall, ast_ctx(self), bout)) {
3222 astwarning(ast_ctx(bfall), WARN_???, "missing break after last case");
3227 /* If there was a default case, put it down here */
3231 /* No need to create an extra block */
3232 bcase = func->curblock;
3234 /* Insert the fallthrough jump */
3235 if (def_bfall && !def_bfall->final) {
3236 if (!ir_block_create_jump(def_bfall, ast_ctx(self), bcase))
3240 /* Now generate the default code */
3241 cgen = def_case->code->codegen;
3242 if (!(*cgen)((ast_expression*)def_case->code, func, false, &dummy))
3245 /* see if we need to fall through */
3246 if (def_bfall_to && !func->curblock->final)
3248 if (!ir_block_create_jump(func->curblock, ast_ctx(self), def_bfall_to))
3253 /* Jump from the last bnot to bout */
3254 if (!func->curblock->final && !ir_block_create_jump(func->curblock, ast_ctx(self), bout))
3256 /* enter the outgoing block */
3257 func->curblock = bout;
3259 /* restore the break block */
3260 vec_pop(func->breakblocks);
3262 /* Move 'bout' to the end, it's nicer */
3263 vec_remove(func->ir_func->blocks, bout_id, 1);
3264 vec_push(func->ir_func->blocks, bout);
3269 bool ast_label_codegen(ast_label *self, ast_function *func, bool lvalue, ir_value **out)
3274 if (self->undefined) {
3275 compile_error(ast_ctx(self), "internal error: ast_label never defined");
3281 compile_error(ast_ctx(self), "internal error: ast_label cannot be an lvalue");
3285 /* simply create a new block and jump to it */
3286 self->irblock = ir_function_create_block(ast_ctx(self), func->ir_func, self->name);
3287 if (!self->irblock) {
3288 compile_error(ast_ctx(self), "failed to allocate label block `%s`", self->name);
3291 if (!func->curblock->final) {
3292 if (!ir_block_create_jump(func->curblock, ast_ctx(self), self->irblock))
3296 /* enter the new block */
3297 func->curblock = self->irblock;
3299 /* Generate all the leftover gotos */
3300 for (i = 0; i < vec_size(self->gotos); ++i) {
3301 if (!ast_goto_codegen(self->gotos[i], func, false, &dummy))
3308 bool ast_goto_codegen(ast_goto *self, ast_function *func, bool lvalue, ir_value **out)
3312 compile_error(ast_ctx(self), "internal error: ast_goto cannot be an lvalue");
3316 if (self->target->irblock) {
3317 if (self->irblock_from) {
3318 /* we already tried once, this is the callback */
3319 self->irblock_from->final = false;
3320 if (!ir_block_create_goto(self->irblock_from, ast_ctx(self), self->target->irblock)) {
3321 compile_error(ast_ctx(self), "failed to generate goto to `%s`", self->name);
3327 if (!ir_block_create_goto(func->curblock, ast_ctx(self), self->target->irblock)) {
3328 compile_error(ast_ctx(self), "failed to generate goto to `%s`", self->name);
3335 /* the target has not yet been created...
3336 * close this block in a sneaky way:
3338 func->curblock->final = true;
3339 self->irblock_from = func->curblock;
3340 ast_label_register_goto(self->target, self);
3346 bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value **out)
3348 ast_expression_codegen *cgen;
3350 ir_instr *callinstr;
3353 ir_value *funval = NULL;
3355 /* return values are never lvalues */
3357 compile_error(ast_ctx(self), "not an l-value (function call)");
3361 if (self->expression.outr) {
3362 *out = self->expression.outr;
3366 cgen = self->func->codegen;
3367 if (!(*cgen)((ast_expression*)(self->func), func, false, &funval))
3375 for (i = 0; i < vec_size(self->params); ++i)
3378 ast_expression *expr = self->params[i];
3380 cgen = expr->codegen;
3381 if (!(*cgen)(expr, func, false, ¶m))
3385 vec_push(params, param);
3388 /* varargs counter */
3389 if (self->va_count) {
3391 ir_builder *builder = func->curblock->owner->owner;
3392 cgen = self->va_count->codegen;
3393 if (!(*cgen)((ast_expression*)(self->va_count), func, false, &va_count))
3395 if (!ir_block_create_store_op(func->curblock, ast_ctx(self), INSTR_STORE_F,
3396 ir_builder_get_va_count(builder), va_count))
3402 callinstr = ir_block_create_call(func->curblock, ast_ctx(self),
3403 ast_function_label(func, "call"),
3404 funval, !!(self->func->flags & AST_FLAG_NORETURN));
3408 for (i = 0; i < vec_size(params); ++i) {
3409 ir_call_param(callinstr, params[i]);
3412 *out = ir_call_value(callinstr);
3413 self->expression.outr = *out;
3415 codegen_output_type(self, *out);