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**);
78 static bool ast_state_codegen(ast_state*, ast_function*, bool lvalue, ir_value**);
80 /* It must not be possible to get here. */
81 static GMQCC_NORETURN void _ast_node_destroy(ast_node *self)
84 con_err("ast node missing destroy()\n");
88 /* Initialize main ast node aprts */
89 static void ast_node_init(ast_node *self, lex_ctx_t ctx, int nodetype)
92 self->destroy = &_ast_node_destroy;
94 self->nodetype = nodetype;
95 self->side_effects = false;
98 /* weight and side effects */
99 static void _ast_propagate_effects(ast_node *self, ast_node *other)
101 if (ast_side_effects(other))
102 ast_side_effects(self) = true;
104 #define ast_propagate_effects(s,o) _ast_propagate_effects(((ast_node*)(s)), ((ast_node*)(o)))
106 /* General expression initialization */
107 static void ast_expression_init(ast_expression *self,
108 ast_expression_codegen *codegen)
110 self->codegen = codegen;
111 self->vtype = TYPE_VOID;
117 self->varparam = NULL;
119 if (OPTS_OPTION_BOOL(OPTION_COVERAGE))
120 self->flags |= AST_FLAG_BLOCK_COVERAGE;
123 static void ast_expression_delete(ast_expression *self)
127 ast_delete(self->next);
128 for (i = 0; i < vec_size(self->params); ++i) {
129 ast_delete(self->params[i]);
131 vec_free(self->params);
133 ast_delete(self->varparam);
136 static void ast_expression_delete_full(ast_expression *self)
138 ast_expression_delete(self);
142 ast_value* ast_value_copy(const ast_value *self)
145 const ast_expression *fromex;
146 ast_expression *selfex;
147 ast_value *cp = ast_value_new(self->expression.node.context, self->name, self->expression.vtype);
148 if (self->expression.next) {
149 cp->expression.next = ast_type_copy(self->expression.node.context, self->expression.next);
151 fromex = &self->expression;
152 selfex = &cp->expression;
153 selfex->count = fromex->count;
154 selfex->flags = fromex->flags;
155 for (i = 0; i < vec_size(fromex->params); ++i) {
156 ast_value *v = ast_value_copy(fromex->params[i]);
157 vec_push(selfex->params, v);
162 void ast_type_adopt_impl(ast_expression *self, const ast_expression *other)
165 const ast_expression *fromex;
166 ast_expression *selfex;
167 self->vtype = other->vtype;
169 self->next = (ast_expression*)ast_type_copy(ast_ctx(self), other->next);
173 selfex->count = fromex->count;
174 selfex->flags = fromex->flags;
175 for (i = 0; i < vec_size(fromex->params); ++i) {
176 ast_value *v = ast_value_copy(fromex->params[i]);
177 vec_push(selfex->params, v);
181 static ast_expression* ast_shallow_type(lex_ctx_t ctx, int vtype)
183 ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
184 ast_expression_init(self, NULL);
185 self->codegen = NULL;
191 ast_expression* ast_type_copy(lex_ctx_t ctx, const ast_expression *ex)
194 const ast_expression *fromex;
195 ast_expression *selfex;
201 ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
202 ast_expression_init(self, NULL);
207 /* This may never be codegen()d */
208 selfex->codegen = NULL;
210 selfex->vtype = fromex->vtype;
212 selfex->next = ast_type_copy(ctx, fromex->next);
216 selfex->count = fromex->count;
217 selfex->flags = fromex->flags;
218 for (i = 0; i < vec_size(fromex->params); ++i) {
219 ast_value *v = ast_value_copy(fromex->params[i]);
220 vec_push(selfex->params, v);
227 bool ast_compare_type(ast_expression *a, ast_expression *b)
229 if (a->vtype == TYPE_NIL ||
230 b->vtype == TYPE_NIL)
232 if (a->vtype != b->vtype)
234 if (!a->next != !b->next)
236 if (vec_size(a->params) != vec_size(b->params))
238 if ((a->flags & AST_FLAG_TYPE_MASK) !=
239 (b->flags & AST_FLAG_TYPE_MASK) )
243 if (vec_size(a->params)) {
245 for (i = 0; i < vec_size(a->params); ++i) {
246 if (!ast_compare_type((ast_expression*)a->params[i],
247 (ast_expression*)b->params[i]))
252 return ast_compare_type(a->next, b->next);
256 static size_t ast_type_to_string_impl(ast_expression *e, char *buf, size_t bufsize, size_t pos)
263 if (pos + 6 >= bufsize)
265 util_strncpy(buf + pos, "(null)", 6);
269 if (pos + 1 >= bufsize)
274 util_strncpy(buf + pos, "(variant)", 9);
279 return ast_type_to_string_impl(e->next, buf, bufsize, pos);
282 if (pos + 3 >= bufsize)
286 pos = ast_type_to_string_impl(e->next, buf, bufsize, pos);
287 if (pos + 1 >= bufsize)
293 pos = ast_type_to_string_impl(e->next, buf, bufsize, pos);
294 if (pos + 2 >= bufsize)
296 if (!vec_size(e->params)) {
302 pos = ast_type_to_string_impl((ast_expression*)(e->params[0]), buf, bufsize, pos);
303 for (i = 1; i < vec_size(e->params); ++i) {
304 if (pos + 2 >= bufsize)
308 pos = ast_type_to_string_impl((ast_expression*)(e->params[i]), buf, bufsize, pos);
310 if (pos + 1 >= bufsize)
316 pos = ast_type_to_string_impl(e->next, buf, bufsize, pos);
317 if (pos + 1 >= bufsize)
320 pos += util_snprintf(buf + pos, bufsize - pos - 1, "%i", (int)e->count);
321 if (pos + 1 >= bufsize)
327 typestr = type_name[e->vtype];
328 typelen = strlen(typestr);
329 if (pos + typelen >= bufsize)
331 util_strncpy(buf + pos, typestr, typelen);
332 return pos + typelen;
336 buf[bufsize-3] = '.';
337 buf[bufsize-2] = '.';
338 buf[bufsize-1] = '.';
342 void ast_type_to_string(ast_expression *e, char *buf, size_t bufsize)
344 size_t pos = ast_type_to_string_impl(e, buf, bufsize-1, 0);
348 static bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out);
349 ast_value* ast_value_new(lex_ctx_t ctx, const char *name, int t)
351 ast_instantiate(ast_value, ctx, ast_value_delete);
352 ast_expression_init((ast_expression*)self,
353 (ast_expression_codegen*)&ast_value_codegen);
354 self->expression.node.keep = true; /* keep */
356 self->name = name ? util_strdup(name) : NULL;
357 self->expression.vtype = t;
358 self->expression.next = NULL;
359 self->isfield = false;
361 self->hasvalue = false;
364 memset(&self->constval, 0, sizeof(self->constval));
365 self->initlist = NULL;
368 self->ir_values = NULL;
369 self->ir_value_count = 0;
375 self->argcounter = NULL;
376 self->intrinsic = false;
381 void ast_value_delete(ast_value* self)
384 mem_d((void*)self->name);
385 if (self->argcounter)
386 mem_d((void*)self->argcounter);
387 if (self->hasvalue) {
388 switch (self->expression.vtype)
391 mem_d((void*)self->constval.vstring);
394 /* unlink us from the function node */
395 self->constval.vfunc->vtype = NULL;
397 /* NOTE: delete function? currently collected in
398 * the parser structure
405 mem_d(self->ir_values);
410 if (self->initlist) {
411 if (self->expression.next->vtype == TYPE_STRING) {
412 /* strings are allocated, free them */
413 size_t i, len = vec_size(self->initlist);
414 /* in theory, len should be expression.count
415 * but let's not take any chances */
416 for (i = 0; i < len; ++i) {
417 if (self->initlist[i].vstring)
418 mem_d(self->initlist[i].vstring);
421 vec_free(self->initlist);
424 ast_expression_delete((ast_expression*)self);
428 void ast_value_params_add(ast_value *self, ast_value *p)
430 vec_push(self->expression.params, p);
433 bool ast_value_set_name(ast_value *self, const char *name)
436 mem_d((void*)self->name);
437 self->name = util_strdup(name);
441 ast_binary* ast_binary_new(lex_ctx_t ctx, int op,
442 ast_expression* left, ast_expression* right)
444 ast_instantiate(ast_binary, ctx, ast_binary_delete);
445 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binary_codegen);
447 if (ast_istype(right, ast_unary) && OPTS_OPTIMIZATION(OPTIM_PEEPHOLE)) {
448 ast_unary *unary = ((ast_unary*)right);
449 ast_expression *normal = unary->operand;
451 /* make a-(-b) => a + b */
452 if (unary->op == VINSTR_NEG_F || unary->op == VINSTR_NEG_V) {
453 if (op == INSTR_SUB_F) {
456 ++opts_optimizationcount[OPTIM_PEEPHOLE];
457 } else if (op == INSTR_SUB_V) {
460 ++opts_optimizationcount[OPTIM_PEEPHOLE];
468 self->right_first = false;
470 ast_propagate_effects(self, left);
471 ast_propagate_effects(self, right);
473 if (op >= INSTR_EQ_F && op <= INSTR_GT)
474 self->expression.vtype = TYPE_FLOAT;
475 else if (op == INSTR_AND || op == INSTR_OR) {
476 if (OPTS_FLAG(PERL_LOGIC))
477 ast_type_adopt(self, right);
479 self->expression.vtype = TYPE_FLOAT;
481 else if (op == INSTR_BITAND || op == INSTR_BITOR)
482 self->expression.vtype = TYPE_FLOAT;
483 else if (op == INSTR_MUL_VF || op == INSTR_MUL_FV)
484 self->expression.vtype = TYPE_VECTOR;
485 else if (op == INSTR_MUL_V)
486 self->expression.vtype = TYPE_FLOAT;
488 self->expression.vtype = left->vtype;
491 self->refs = AST_REF_ALL;
496 void ast_binary_delete(ast_binary *self)
498 if (self->refs & AST_REF_LEFT) ast_unref(self->left);
499 if (self->refs & AST_REF_RIGHT) ast_unref(self->right);
501 ast_expression_delete((ast_expression*)self);
505 ast_binstore* ast_binstore_new(lex_ctx_t ctx, int storop, int op,
506 ast_expression* left, ast_expression* right)
508 ast_instantiate(ast_binstore, ctx, ast_binstore_delete);
509 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binstore_codegen);
511 ast_side_effects(self) = true;
513 self->opstore = storop;
516 self->source = right;
518 self->keep_dest = false;
520 ast_type_adopt(self, left);
524 void ast_binstore_delete(ast_binstore *self)
526 if (!self->keep_dest)
527 ast_unref(self->dest);
528 ast_unref(self->source);
529 ast_expression_delete((ast_expression*)self);
533 ast_unary* ast_unary_new(lex_ctx_t ctx, int op,
534 ast_expression *expr)
536 ast_instantiate(ast_unary, ctx, ast_unary_delete);
537 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_unary_codegen);
540 self->operand = expr;
543 if (ast_istype(expr, ast_unary) && OPTS_OPTIMIZATION(OPTIM_PEEPHOLE)) {
544 ast_unary *prev = (ast_unary*)((ast_unary*)expr)->operand;
546 /* Handle for double negation */
547 if (((ast_unary*)expr)->op == op)
548 prev = (ast_unary*)((ast_unary*)expr)->operand;
550 if (ast_istype(prev, ast_unary)) {
551 ast_expression_delete((ast_expression*)self);
553 ++opts_optimizationcount[OPTIM_PEEPHOLE];
558 ast_propagate_effects(self, expr);
560 if ((op >= INSTR_NOT_F && op <= INSTR_NOT_FNC) || op == VINSTR_NEG_F) {
561 self->expression.vtype = TYPE_FLOAT;
562 } else if (op == VINSTR_NEG_V) {
563 self->expression.vtype = TYPE_VECTOR;
565 compile_error(ctx, "cannot determine type of unary operation %s", util_instr_str[op]);
571 void ast_unary_delete(ast_unary *self)
573 if (self->operand) ast_unref(self->operand);
574 ast_expression_delete((ast_expression*)self);
578 ast_return* ast_return_new(lex_ctx_t ctx, ast_expression *expr)
580 ast_instantiate(ast_return, ctx, ast_return_delete);
581 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_return_codegen);
583 self->operand = expr;
586 ast_propagate_effects(self, expr);
591 void ast_return_delete(ast_return *self)
594 ast_unref(self->operand);
595 ast_expression_delete((ast_expression*)self);
599 ast_entfield* ast_entfield_new(lex_ctx_t ctx, ast_expression *entity, ast_expression *field)
601 if (field->vtype != TYPE_FIELD) {
602 compile_error(ctx, "ast_entfield_new with expression not of type field");
605 return ast_entfield_new_force(ctx, entity, field, field->next);
608 ast_entfield* ast_entfield_new_force(lex_ctx_t ctx, ast_expression *entity, ast_expression *field, const ast_expression *outtype)
610 ast_instantiate(ast_entfield, ctx, ast_entfield_delete);
614 /* Error: field has no type... */
618 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_entfield_codegen);
620 self->entity = entity;
622 ast_propagate_effects(self, entity);
623 ast_propagate_effects(self, field);
625 ast_type_adopt(self, outtype);
629 void ast_entfield_delete(ast_entfield *self)
631 ast_unref(self->entity);
632 ast_unref(self->field);
633 ast_expression_delete((ast_expression*)self);
637 ast_member* ast_member_new(lex_ctx_t ctx, ast_expression *owner, unsigned int field, const char *name)
639 ast_instantiate(ast_member, ctx, ast_member_delete);
645 if (owner->vtype != TYPE_VECTOR &&
646 owner->vtype != TYPE_FIELD) {
647 compile_error(ctx, "member-access on an invalid owner of type %s", type_name[owner->vtype]);
652 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_member_codegen);
653 self->expression.node.keep = true; /* keep */
655 if (owner->vtype == TYPE_VECTOR) {
656 self->expression.vtype = TYPE_FLOAT;
657 self->expression.next = NULL;
659 self->expression.vtype = TYPE_FIELD;
660 self->expression.next = ast_shallow_type(ctx, TYPE_FLOAT);
663 self->rvalue = false;
665 ast_propagate_effects(self, owner);
669 self->name = util_strdup(name);
676 void ast_member_delete(ast_member *self)
678 /* The owner is always an ast_value, which has .keep=true,
679 * also: ast_members are usually deleted after the owner, thus
680 * this will cause invalid access
681 ast_unref(self->owner);
682 * once we allow (expression).x to access a vector-member, we need
683 * to change this: preferably by creating an alternate ast node for this
684 * purpose that is not garbage-collected.
686 ast_expression_delete((ast_expression*)self);
691 bool ast_member_set_name(ast_member *self, const char *name)
694 mem_d((void*)self->name);
695 self->name = util_strdup(name);
699 ast_array_index* ast_array_index_new(lex_ctx_t ctx, ast_expression *array, ast_expression *index)
701 ast_expression *outtype;
702 ast_instantiate(ast_array_index, ctx, ast_array_index_delete);
704 outtype = array->next;
707 /* Error: field has no type... */
711 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_array_index_codegen);
715 ast_propagate_effects(self, array);
716 ast_propagate_effects(self, index);
718 ast_type_adopt(self, outtype);
719 if (array->vtype == TYPE_FIELD && outtype->vtype == TYPE_ARRAY) {
720 if (self->expression.vtype != TYPE_ARRAY) {
721 compile_error(ast_ctx(self), "array_index node on type");
722 ast_array_index_delete(self);
725 self->array = outtype;
726 self->expression.vtype = TYPE_FIELD;
732 void ast_array_index_delete(ast_array_index *self)
735 ast_unref(self->array);
737 ast_unref(self->index);
738 ast_expression_delete((ast_expression*)self);
742 ast_argpipe* ast_argpipe_new(lex_ctx_t ctx, ast_expression *index)
744 ast_instantiate(ast_argpipe, ctx, ast_argpipe_delete);
745 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_argpipe_codegen);
747 self->expression.vtype = TYPE_NOEXPR;
751 void ast_argpipe_delete(ast_argpipe *self)
754 ast_unref(self->index);
755 ast_expression_delete((ast_expression*)self);
759 ast_ifthen* ast_ifthen_new(lex_ctx_t ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
761 ast_instantiate(ast_ifthen, ctx, ast_ifthen_delete);
762 if (!ontrue && !onfalse) {
763 /* because it is invalid */
767 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ifthen_codegen);
770 self->on_true = ontrue;
771 self->on_false = onfalse;
772 ast_propagate_effects(self, cond);
774 ast_propagate_effects(self, ontrue);
776 ast_propagate_effects(self, onfalse);
781 void ast_ifthen_delete(ast_ifthen *self)
783 ast_unref(self->cond);
785 ast_unref(self->on_true);
787 ast_unref(self->on_false);
788 ast_expression_delete((ast_expression*)self);
792 ast_ternary* ast_ternary_new(lex_ctx_t ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
794 ast_expression *exprtype = ontrue;
795 ast_instantiate(ast_ternary, ctx, ast_ternary_delete);
796 /* This time NEITHER must be NULL */
797 if (!ontrue || !onfalse) {
801 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ternary_codegen);
804 self->on_true = ontrue;
805 self->on_false = onfalse;
806 ast_propagate_effects(self, cond);
807 ast_propagate_effects(self, ontrue);
808 ast_propagate_effects(self, onfalse);
810 if (ontrue->vtype == TYPE_NIL)
812 ast_type_adopt(self, exprtype);
817 void ast_ternary_delete(ast_ternary *self)
819 /* the if()s are only there because computed-gotos can set them
822 if (self->cond) ast_unref(self->cond);
823 if (self->on_true) ast_unref(self->on_true);
824 if (self->on_false) ast_unref(self->on_false);
825 ast_expression_delete((ast_expression*)self);
829 ast_loop* ast_loop_new(lex_ctx_t ctx,
830 ast_expression *initexpr,
831 ast_expression *precond, bool pre_not,
832 ast_expression *postcond, bool post_not,
833 ast_expression *increment,
834 ast_expression *body)
836 ast_instantiate(ast_loop, ctx, ast_loop_delete);
837 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_loop_codegen);
839 self->initexpr = initexpr;
840 self->precond = precond;
841 self->postcond = postcond;
842 self->increment = increment;
845 self->pre_not = pre_not;
846 self->post_not = post_not;
849 ast_propagate_effects(self, initexpr);
851 ast_propagate_effects(self, precond);
853 ast_propagate_effects(self, postcond);
855 ast_propagate_effects(self, increment);
857 ast_propagate_effects(self, body);
862 void ast_loop_delete(ast_loop *self)
865 ast_unref(self->initexpr);
867 ast_unref(self->precond);
869 ast_unref(self->postcond);
871 ast_unref(self->increment);
873 ast_unref(self->body);
874 ast_expression_delete((ast_expression*)self);
878 ast_breakcont* ast_breakcont_new(lex_ctx_t ctx, bool iscont, unsigned int levels)
880 ast_instantiate(ast_breakcont, ctx, ast_breakcont_delete);
881 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_breakcont_codegen);
883 self->is_continue = iscont;
884 self->levels = levels;
889 void ast_breakcont_delete(ast_breakcont *self)
891 ast_expression_delete((ast_expression*)self);
895 ast_switch* ast_switch_new(lex_ctx_t ctx, ast_expression *op)
897 ast_instantiate(ast_switch, ctx, ast_switch_delete);
898 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_switch_codegen);
903 ast_propagate_effects(self, op);
908 void ast_switch_delete(ast_switch *self)
911 ast_unref(self->operand);
913 for (i = 0; i < vec_size(self->cases); ++i) {
914 if (self->cases[i].value)
915 ast_unref(self->cases[i].value);
916 ast_unref(self->cases[i].code);
918 vec_free(self->cases);
920 ast_expression_delete((ast_expression*)self);
924 ast_label* ast_label_new(lex_ctx_t ctx, const char *name, bool undefined)
926 ast_instantiate(ast_label, ctx, ast_label_delete);
927 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_label_codegen);
929 self->expression.vtype = TYPE_NOEXPR;
931 self->name = util_strdup(name);
932 self->irblock = NULL;
934 self->undefined = undefined;
939 void ast_label_delete(ast_label *self)
941 mem_d((void*)self->name);
942 vec_free(self->gotos);
943 ast_expression_delete((ast_expression*)self);
947 static void ast_label_register_goto(ast_label *self, ast_goto *g)
949 vec_push(self->gotos, g);
952 ast_goto* ast_goto_new(lex_ctx_t ctx, const char *name)
954 ast_instantiate(ast_goto, ctx, ast_goto_delete);
955 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_goto_codegen);
957 self->name = util_strdup(name);
959 self->irblock_from = NULL;
964 void ast_goto_delete(ast_goto *self)
966 mem_d((void*)self->name);
967 ast_expression_delete((ast_expression*)self);
971 void ast_goto_set_label(ast_goto *self, ast_label *label)
973 self->target = label;
976 ast_state* ast_state_new(lex_ctx_t ctx, ast_expression *frame, ast_expression *think)
978 ast_instantiate(ast_state, ctx, ast_state_delete);
979 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_state_codegen);
980 self->framenum = frame;
981 self->nextthink = think;
985 void ast_state_delete(ast_state *self)
988 ast_unref(self->framenum);
990 ast_unref(self->nextthink);
992 ast_expression_delete((ast_expression*)self);
996 ast_call* ast_call_new(lex_ctx_t ctx,
997 ast_expression *funcexpr)
999 ast_instantiate(ast_call, ctx, ast_call_delete);
1000 if (!funcexpr->next) {
1001 compile_error(ctx, "not a function");
1005 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_call_codegen);
1007 ast_side_effects(self) = true;
1009 self->params = NULL;
1010 self->func = funcexpr;
1011 self->va_count = NULL;
1013 ast_type_adopt(self, funcexpr->next);
1018 void ast_call_delete(ast_call *self)
1021 for (i = 0; i < vec_size(self->params); ++i)
1022 ast_unref(self->params[i]);
1023 vec_free(self->params);
1026 ast_unref(self->func);
1029 ast_unref(self->va_count);
1031 ast_expression_delete((ast_expression*)self);
1035 static bool ast_call_check_vararg(ast_call *self, ast_expression *va_type, ast_expression *exp_type)
1041 if (!va_type || !ast_compare_type(va_type, exp_type))
1043 if (va_type && exp_type)
1045 ast_type_to_string(va_type, tgot, sizeof(tgot));
1046 ast_type_to_string(exp_type, texp, sizeof(texp));
1047 if (OPTS_FLAG(UNSAFE_VARARGS)) {
1048 if (compile_warning(ast_ctx(self), WARN_UNSAFE_TYPES,
1049 "piped variadic argument differs in type: constrained to type %s, expected type %s",
1053 compile_error(ast_ctx(self),
1054 "piped variadic argument differs in type: constrained to type %s, expected type %s",
1061 ast_type_to_string(exp_type, texp, sizeof(texp));
1062 if (OPTS_FLAG(UNSAFE_VARARGS)) {
1063 if (compile_warning(ast_ctx(self), WARN_UNSAFE_TYPES,
1064 "piped variadic argument may differ in type: expected type %s",
1068 compile_error(ast_ctx(self),
1069 "piped variadic argument may differ in type: expected type %s",
1078 bool ast_call_check_types(ast_call *self, ast_expression *va_type)
1084 const ast_expression *func = self->func;
1085 size_t count = vec_size(self->params);
1086 if (count > vec_size(func->params))
1087 count = vec_size(func->params);
1089 for (i = 0; i < count; ++i) {
1090 if (ast_istype(self->params[i], ast_argpipe)) {
1091 /* warn about type safety instead */
1093 compile_error(ast_ctx(self), "argpipe must be the last parameter to a function call");
1096 if (!ast_call_check_vararg(self, va_type, (ast_expression*)func->params[i]))
1099 else if (!ast_compare_type(self->params[i], (ast_expression*)(func->params[i])))
1101 ast_type_to_string(self->params[i], tgot, sizeof(tgot));
1102 ast_type_to_string((ast_expression*)func->params[i], texp, sizeof(texp));
1103 compile_error(ast_ctx(self), "invalid type for parameter %u in function call: expected %s, got %s",
1104 (unsigned int)(i+1), texp, tgot);
1105 /* we don't immediately return */
1109 count = vec_size(self->params);
1110 if (count > vec_size(func->params) && func->varparam) {
1111 for (; i < count; ++i) {
1112 if (ast_istype(self->params[i], ast_argpipe)) {
1113 /* warn about type safety instead */
1115 compile_error(ast_ctx(self), "argpipe must be the last parameter to a function call");
1118 if (!ast_call_check_vararg(self, va_type, func->varparam))
1121 else if (!ast_compare_type(self->params[i], func->varparam))
1123 ast_type_to_string(self->params[i], tgot, sizeof(tgot));
1124 ast_type_to_string(func->varparam, texp, sizeof(texp));
1125 compile_error(ast_ctx(self), "invalid type for variadic parameter %u in function call: expected %s, got %s",
1126 (unsigned int)(i+1), texp, tgot);
1127 /* we don't immediately return */
1135 ast_store* ast_store_new(lex_ctx_t ctx, int op,
1136 ast_expression *dest, ast_expression *source)
1138 ast_instantiate(ast_store, ctx, ast_store_delete);
1139 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_store_codegen);
1141 ast_side_effects(self) = true;
1145 self->source = source;
1147 ast_type_adopt(self, dest);
1152 void ast_store_delete(ast_store *self)
1154 ast_unref(self->dest);
1155 ast_unref(self->source);
1156 ast_expression_delete((ast_expression*)self);
1160 ast_block* ast_block_new(lex_ctx_t ctx)
1162 ast_instantiate(ast_block, ctx, ast_block_delete);
1163 ast_expression_init((ast_expression*)self,
1164 (ast_expression_codegen*)&ast_block_codegen);
1166 self->locals = NULL;
1168 self->collect = NULL;
1173 bool ast_block_add_expr(ast_block *self, ast_expression *e)
1175 ast_propagate_effects(self, e);
1176 vec_push(self->exprs, e);
1177 if (self->expression.next) {
1178 ast_delete(self->expression.next);
1179 self->expression.next = NULL;
1181 ast_type_adopt(self, e);
1185 void ast_block_collect(ast_block *self, ast_expression *expr)
1187 vec_push(self->collect, expr);
1188 expr->node.keep = true;
1191 void ast_block_delete(ast_block *self)
1194 for (i = 0; i < vec_size(self->exprs); ++i)
1195 ast_unref(self->exprs[i]);
1196 vec_free(self->exprs);
1197 for (i = 0; i < vec_size(self->locals); ++i)
1198 ast_delete(self->locals[i]);
1199 vec_free(self->locals);
1200 for (i = 0; i < vec_size(self->collect); ++i)
1201 ast_delete(self->collect[i]);
1202 vec_free(self->collect);
1203 ast_expression_delete((ast_expression*)self);
1207 void ast_block_set_type(ast_block *self, ast_expression *from)
1209 if (self->expression.next)
1210 ast_delete(self->expression.next);
1211 ast_type_adopt(self, from);
1214 ast_function* ast_function_new(lex_ctx_t ctx, const char *name, ast_value *vtype)
1216 ast_instantiate(ast_function, ctx, ast_function_delete);
1219 compile_error(ast_ctx(self), "internal error: ast_function_new condition 0");
1221 } else if (vtype->hasvalue || vtype->expression.vtype != TYPE_FUNCTION) {
1222 compile_error(ast_ctx(self), "internal error: ast_function_new condition %i %i type=%i (probably 2 bodies?)",
1224 (int)vtype->hasvalue,
1225 vtype->expression.vtype);
1229 self->vtype = vtype;
1230 self->name = name ? util_strdup(name) : NULL;
1231 self->blocks = NULL;
1233 self->labelcount = 0;
1236 self->ir_func = NULL;
1237 self->curblock = NULL;
1239 self->breakblocks = NULL;
1240 self->continueblocks = NULL;
1242 vtype->hasvalue = true;
1243 vtype->constval.vfunc = self;
1245 self->varargs = NULL;
1247 self->fixedparams = NULL;
1248 self->return_value = NULL;
1250 self->static_names = NULL;
1251 self->static_count = 0;
1260 void ast_function_delete(ast_function *self)
1264 mem_d((void*)self->name);
1266 /* ast_value_delete(self->vtype); */
1267 self->vtype->hasvalue = false;
1268 self->vtype->constval.vfunc = NULL;
1269 /* We use unref - if it was stored in a global table it is supposed
1270 * to be deleted from *there*
1272 ast_unref(self->vtype);
1274 for (i = 0; i < vec_size(self->static_names); ++i)
1275 mem_d(self->static_names[i]);
1276 vec_free(self->static_names);
1277 for (i = 0; i < vec_size(self->blocks); ++i)
1278 ast_delete(self->blocks[i]);
1279 vec_free(self->blocks);
1280 vec_free(self->breakblocks);
1281 vec_free(self->continueblocks);
1283 ast_delete(self->varargs);
1285 ast_delete(self->argc);
1286 if (self->fixedparams)
1287 ast_unref(self->fixedparams);
1288 if (self->return_value)
1289 ast_unref(self->return_value);
1293 const char* ast_function_label(ast_function *self, const char *prefix)
1299 if (!OPTS_OPTION_BOOL(OPTION_DUMP) &&
1300 !OPTS_OPTION_BOOL(OPTION_DUMPFIN) &&
1301 !OPTS_OPTION_BOOL(OPTION_DEBUG))
1306 id = (self->labelcount++);
1307 len = strlen(prefix);
1309 from = self->labelbuf + sizeof(self->labelbuf)-1;
1312 *from-- = (id%10) + '0';
1316 memcpy(from - len, prefix, len);
1320 /*********************************************************************/
1322 * by convention you must never pass NULL to the 'ir_value **out'
1323 * parameter. If you really don't care about the output, pass a dummy.
1324 * But I can't imagine a pituation where the output is truly unnecessary.
1327 static void _ast_codegen_output_type(ast_expression *self, ir_value *out)
1329 if (out->vtype == TYPE_FIELD)
1330 out->fieldtype = self->next->vtype;
1331 if (out->vtype == TYPE_FUNCTION)
1332 out->outtype = self->next->vtype;
1335 #define codegen_output_type(a,o) (_ast_codegen_output_type(&((a)->expression),(o)))
1337 bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out)
1341 if (self->expression.vtype == TYPE_NIL) {
1342 *out = func->ir_func->owner->nil;
1345 /* NOTE: This is the codegen for a variable used in an expression.
1346 * It is not the codegen to generate the value. For this purpose,
1347 * ast_local_codegen and ast_global_codegen are to be used before this
1348 * is executed. ast_function_codegen should take care of its locals,
1349 * and the ast-user should take care of ast_global_codegen to be used
1350 * on all the globals.
1353 char tname[1024]; /* typename is reserved in C++ */
1354 ast_type_to_string((ast_expression*)self, tname, sizeof(tname));
1355 compile_error(ast_ctx(self), "ast_value used before generated %s %s", tname, self->name);
1362 static bool ast_global_array_set(ast_value *self)
1364 size_t count = vec_size(self->initlist);
1367 if (count > self->expression.count) {
1368 compile_error(ast_ctx(self), "too many elements in initializer");
1369 count = self->expression.count;
1371 else if (count < self->expression.count) {
1373 compile_warning(ast_ctx(self), "not all elements are initialized");
1377 for (i = 0; i != count; ++i) {
1378 switch (self->expression.next->vtype) {
1380 if (!ir_value_set_float(self->ir_values[i], self->initlist[i].vfloat))
1384 if (!ir_value_set_vector(self->ir_values[i], self->initlist[i].vvec))
1388 if (!ir_value_set_string(self->ir_values[i], self->initlist[i].vstring))
1392 /* we don't support them in any other place yet either */
1393 compile_error(ast_ctx(self), "TODO: nested arrays");
1396 /* this requiers a bit more work - similar to the fields I suppose */
1397 compile_error(ast_ctx(self), "global of type function not properly generated");
1400 if (!self->initlist[i].vfield) {
1401 compile_error(ast_ctx(self), "field constant without vfield set");
1404 if (!self->initlist[i].vfield->ir_v) {
1405 compile_error(ast_ctx(self), "field constant generated before its field");
1408 if (!ir_value_set_field(self->ir_values[i], self->initlist[i].vfield->ir_v))
1412 compile_error(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1419 static bool check_array(ast_value *self, ast_value *array)
1421 if (array->expression.flags & AST_FLAG_ARRAY_INIT && !array->initlist) {
1422 compile_error(ast_ctx(self), "array without size: %s", self->name);
1425 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1426 if (!array->expression.count || array->expression.count > OPTS_OPTION_U32(OPTION_MAX_ARRAY_SIZE)) {
1427 compile_error(ast_ctx(self), "Invalid array of size %lu", (unsigned long)array->expression.count);
1433 bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
1437 if (self->expression.vtype == TYPE_NIL) {
1438 compile_error(ast_ctx(self), "internal error: trying to generate a variable of TYPE_NIL");
1442 if (self->hasvalue && self->expression.vtype == TYPE_FUNCTION)
1444 ir_function *func = ir_builder_create_function(ir, self->name, self->expression.next->vtype);
1447 func->context = ast_ctx(self);
1448 func->value->context = ast_ctx(self);
1450 self->constval.vfunc->ir_func = func;
1451 self->ir_v = func->value;
1452 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1453 self->ir_v->flags |= IR_FLAG_INCLUDE_DEF;
1454 if (self->expression.flags & AST_FLAG_ERASEABLE)
1455 self->ir_v->flags |= IR_FLAG_ERASEABLE;
1456 if (self->expression.flags & AST_FLAG_BLOCK_COVERAGE)
1457 func->flags |= IR_FLAG_BLOCK_COVERAGE;
1458 /* The function is filled later on ast_function_codegen... */
1462 if (isfield && self->expression.vtype == TYPE_FIELD) {
1463 ast_expression *fieldtype = self->expression.next;
1465 if (self->hasvalue) {
1466 compile_error(ast_ctx(self), "TODO: constant field pointers with value");
1470 if (fieldtype->vtype == TYPE_ARRAY) {
1475 ast_expression *elemtype;
1477 ast_value *array = (ast_value*)fieldtype;
1479 if (!ast_istype(fieldtype, ast_value)) {
1480 compile_error(ast_ctx(self), "internal error: ast_value required");
1484 if (!check_array(self, array))
1487 elemtype = array->expression.next;
1488 vtype = elemtype->vtype;
1490 v = ir_builder_create_field(ir, self->name, vtype);
1492 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", self->name);
1495 v->context = ast_ctx(self);
1496 v->unique_life = true;
1498 array->ir_v = self->ir_v = v;
1500 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1501 self->ir_v->flags |= IR_FLAG_INCLUDE_DEF;
1502 if (self->expression.flags & AST_FLAG_ERASEABLE)
1503 self->ir_v->flags |= IR_FLAG_ERASEABLE;
1505 namelen = strlen(self->name);
1506 name = (char*)mem_a(namelen + 16);
1507 util_strncpy(name, self->name, namelen);
1509 array->ir_values = (ir_value**)mem_a(sizeof(array->ir_values[0]) * array->expression.count);
1510 array->ir_values[0] = v;
1511 for (ai = 1; ai < array->expression.count; ++ai) {
1512 util_snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1513 array->ir_values[ai] = ir_builder_create_field(ir, name, vtype);
1514 if (!array->ir_values[ai]) {
1516 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", name);
1519 array->ir_values[ai]->context = ast_ctx(self);
1520 array->ir_values[ai]->unique_life = true;
1521 array->ir_values[ai]->locked = true;
1522 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1523 self->ir_values[ai]->flags |= IR_FLAG_INCLUDE_DEF;
1529 v = ir_builder_create_field(ir, self->name, self->expression.next->vtype);
1532 v->context = ast_ctx(self);
1534 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1535 self->ir_v->flags |= IR_FLAG_INCLUDE_DEF;
1537 if (self->expression.flags & AST_FLAG_ERASEABLE)
1538 self->ir_v->flags |= IR_FLAG_ERASEABLE;
1543 if (self->expression.vtype == TYPE_ARRAY) {
1548 ast_expression *elemtype = self->expression.next;
1549 int vtype = elemtype->vtype;
1551 if (self->expression.flags & AST_FLAG_ARRAY_INIT && !self->expression.count) {
1552 compile_error(ast_ctx(self), "array `%s' has no size", self->name);
1556 /* same as with field arrays */
1557 if (!check_array(self, self))
1560 v = ir_builder_create_global(ir, self->name, vtype);
1562 compile_error(ast_ctx(self), "ir_builder_create_global failed `%s`", self->name);
1565 v->context = ast_ctx(self);
1566 v->unique_life = true;
1569 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1570 v->flags |= IR_FLAG_INCLUDE_DEF;
1571 if (self->expression.flags & AST_FLAG_ERASEABLE)
1572 self->ir_v->flags |= IR_FLAG_ERASEABLE;
1574 namelen = strlen(self->name);
1575 name = (char*)mem_a(namelen + 16);
1576 util_strncpy(name, self->name, namelen);
1578 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1579 self->ir_values[0] = v;
1580 for (ai = 1; ai < self->expression.count; ++ai) {
1581 util_snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1582 self->ir_values[ai] = ir_builder_create_global(ir, name, vtype);
1583 if (!self->ir_values[ai]) {
1585 compile_error(ast_ctx(self), "ir_builder_create_global failed `%s`", name);
1588 self->ir_values[ai]->context = ast_ctx(self);
1589 self->ir_values[ai]->unique_life = true;
1590 self->ir_values[ai]->locked = true;
1591 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1592 self->ir_values[ai]->flags |= IR_FLAG_INCLUDE_DEF;
1598 /* Arrays don't do this since there's no "array" value which spans across the
1601 v = ir_builder_create_global(ir, self->name, self->expression.vtype);
1603 compile_error(ast_ctx(self), "ir_builder_create_global failed on `%s`", self->name);
1606 codegen_output_type(self, v);
1607 v->context = ast_ctx(self);
1610 /* link us to the ir_value */
1614 if (self->expression.flags & AST_FLAG_INCLUDE_DEF)
1615 self->ir_v->flags |= IR_FLAG_INCLUDE_DEF;
1616 if (self->expression.flags & AST_FLAG_ERASEABLE)
1617 self->ir_v->flags |= IR_FLAG_ERASEABLE;
1620 if (self->hasvalue) {
1621 switch (self->expression.vtype)
1624 if (!ir_value_set_float(v, self->constval.vfloat))
1628 if (!ir_value_set_vector(v, self->constval.vvec))
1632 if (!ir_value_set_string(v, self->constval.vstring))
1636 ast_global_array_set(self);
1639 compile_error(ast_ctx(self), "global of type function not properly generated");
1641 /* Cannot generate an IR value for a function,
1642 * need a pointer pointing to a function rather.
1645 if (!self->constval.vfield) {
1646 compile_error(ast_ctx(self), "field constant without vfield set");
1649 if (!self->constval.vfield->ir_v) {
1650 compile_error(ast_ctx(self), "field constant generated before its field");
1653 if (!ir_value_set_field(v, self->constval.vfield->ir_v))
1657 compile_error(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1663 error: /* clean up */
1664 if(v) ir_value_delete(v);
1668 static bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
1672 if (self->expression.vtype == TYPE_NIL) {
1673 compile_error(ast_ctx(self), "internal error: trying to generate a variable of TYPE_NIL");
1677 if (self->hasvalue && self->expression.vtype == TYPE_FUNCTION)
1679 /* Do we allow local functions? I think not...
1680 * this is NOT a function pointer atm.
1685 if (self->expression.vtype == TYPE_ARRAY) {
1690 ast_expression *elemtype = self->expression.next;
1691 int vtype = elemtype->vtype;
1693 func->flags |= IR_FLAG_HAS_ARRAYS;
1695 if (param && !(self->expression.flags & AST_FLAG_IS_VARARG)) {
1696 compile_error(ast_ctx(self), "array-parameters are not supported");
1700 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1701 if (!check_array(self, self))
1704 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1705 if (!self->ir_values) {
1706 compile_error(ast_ctx(self), "failed to allocate array values");
1710 v = ir_function_create_local(func, self->name, vtype, param);
1712 compile_error(ast_ctx(self), "internal error: ir_function_create_local failed");
1715 v->context = ast_ctx(self);
1716 v->unique_life = true;
1719 namelen = strlen(self->name);
1720 name = (char*)mem_a(namelen + 16);
1721 util_strncpy(name, self->name, namelen);
1723 self->ir_values[0] = v;
1724 for (ai = 1; ai < self->expression.count; ++ai) {
1725 util_snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1726 self->ir_values[ai] = ir_function_create_local(func, name, vtype, param);
1727 if (!self->ir_values[ai]) {
1728 compile_error(ast_ctx(self), "internal_error: ir_builder_create_global failed on `%s`", name);
1731 self->ir_values[ai]->context = ast_ctx(self);
1732 self->ir_values[ai]->unique_life = true;
1733 self->ir_values[ai]->locked = true;
1739 v = ir_function_create_local(func, self->name, self->expression.vtype, param);
1742 codegen_output_type(self, v);
1743 v->context = ast_ctx(self);
1746 /* A constant local... hmmm...
1747 * I suppose the IR will have to deal with this
1749 if (self->hasvalue) {
1750 switch (self->expression.vtype)
1753 if (!ir_value_set_float(v, self->constval.vfloat))
1757 if (!ir_value_set_vector(v, self->constval.vvec))
1761 if (!ir_value_set_string(v, self->constval.vstring))
1765 compile_error(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1770 /* link us to the ir_value */
1774 if (!ast_generate_accessors(self, func->owner))
1778 error: /* clean up */
1783 bool ast_generate_accessors(ast_value *self, ir_builder *ir)
1786 bool warn = OPTS_WARN(WARN_USED_UNINITIALIZED);
1787 if (!self->setter || !self->getter)
1789 for (i = 0; i < self->expression.count; ++i) {
1790 if (!self->ir_values) {
1791 compile_error(ast_ctx(self), "internal error: no array values generated for `%s`", self->name);
1794 if (!self->ir_values[i]) {
1795 compile_error(ast_ctx(self), "internal error: not all array values have been generated for `%s`", self->name);
1798 if (self->ir_values[i]->life) {
1799 compile_error(ast_ctx(self), "internal error: function containing `%s` already generated", self->name);
1804 opts_set(opts.warn, WARN_USED_UNINITIALIZED, false);
1806 if (!ast_global_codegen (self->setter, ir, false) ||
1807 !ast_function_codegen(self->setter->constval.vfunc, ir) ||
1808 !ir_function_finalize(self->setter->constval.vfunc->ir_func))
1810 compile_error(ast_ctx(self), "internal error: failed to generate setter for `%s`", self->name);
1811 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1816 if (!ast_global_codegen (self->getter, ir, false) ||
1817 !ast_function_codegen(self->getter->constval.vfunc, ir) ||
1818 !ir_function_finalize(self->getter->constval.vfunc->ir_func))
1820 compile_error(ast_ctx(self), "internal error: failed to generate getter for `%s`", self->name);
1821 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1825 for (i = 0; i < self->expression.count; ++i) {
1826 vec_free(self->ir_values[i]->life);
1828 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1832 bool ast_function_codegen(ast_function *self, ir_builder *ir)
1837 ast_expression_codegen *cgen;
1843 irf = self->ir_func;
1845 compile_error(ast_ctx(self), "internal error: ast_function's related ast_value was not generated yet");
1849 /* fill the parameter list */
1850 ec = &self->vtype->expression;
1851 for (i = 0; i < vec_size(ec->params); ++i)
1853 if (ec->params[i]->expression.vtype == TYPE_FIELD)
1854 vec_push(irf->params, ec->params[i]->expression.next->vtype);
1856 vec_push(irf->params, ec->params[i]->expression.vtype);
1857 if (!self->builtin) {
1858 if (!ast_local_codegen(ec->params[i], self->ir_func, true))
1863 if (self->varargs) {
1864 if (!ast_local_codegen(self->varargs, self->ir_func, true))
1866 irf->max_varargs = self->varargs->expression.count;
1869 if (self->builtin) {
1870 irf->builtin = self->builtin;
1874 /* have a local return value variable? */
1875 if (self->return_value) {
1876 if (!ast_local_codegen(self->return_value, self->ir_func, false))
1880 if (!vec_size(self->blocks)) {
1881 compile_error(ast_ctx(self), "function `%s` has no body", self->name);
1885 irf->first = self->curblock = ir_function_create_block(ast_ctx(self), irf, "entry");
1886 if (!self->curblock) {
1887 compile_error(ast_ctx(self), "failed to allocate entry block for `%s`", self->name);
1895 if (!ast_local_codegen(self->argc, self->ir_func, true))
1897 cgen = self->argc->expression.codegen;
1898 if (!(*cgen)((ast_expression*)(self->argc), self, false, &va_count))
1900 cgen = self->fixedparams->expression.codegen;
1901 if (!(*cgen)((ast_expression*)(self->fixedparams), self, false, &fixed))
1903 sub = ir_block_create_binop(self->curblock, ast_ctx(self),
1904 ast_function_label(self, "va_count"), INSTR_SUB_F,
1905 ir_builder_get_va_count(ir), fixed);
1908 if (!ir_block_create_store_op(self->curblock, ast_ctx(self), INSTR_STORE_F,
1915 for (i = 0; i < vec_size(self->blocks); ++i) {
1916 cgen = self->blocks[i]->expression.codegen;
1917 if (!(*cgen)((ast_expression*)self->blocks[i], self, false, &dummy))
1921 /* TODO: check return types */
1922 if (!self->curblock->final)
1924 if (!self->vtype->expression.next ||
1925 self->vtype->expression.next->vtype == TYPE_VOID)
1927 return ir_block_create_return(self->curblock, ast_ctx(self), NULL);
1929 else if (vec_size(self->curblock->entries) || self->curblock == irf->first)
1931 if (self->return_value) {
1932 cgen = self->return_value->expression.codegen;
1933 if (!(*cgen)((ast_expression*)(self->return_value), self, false, &dummy))
1935 return ir_block_create_return(self->curblock, ast_ctx(self), dummy);
1937 else if (compile_warning(ast_ctx(self), WARN_MISSING_RETURN_VALUES,
1938 "control reaches end of non-void function (`%s`) via %s",
1939 self->name, self->curblock->label))
1943 return ir_block_create_return(self->curblock, ast_ctx(self), NULL);
1949 static bool starts_a_label(ast_expression *ex)
1951 while (ex && ast_istype(ex, ast_block)) {
1952 ast_block *b = (ast_block*)ex;
1957 return ast_istype(ex, ast_label);
1960 /* Note, you will not see ast_block_codegen generate ir_blocks.
1961 * To the AST and the IR, blocks are 2 different things.
1962 * In the AST it represents a block of code, usually enclosed in
1963 * curly braces {...}.
1964 * While in the IR it represents a block in terms of control-flow.
1966 bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_value **out)
1970 /* We don't use this
1971 * Note: an ast-representation using the comma-operator
1972 * of the form: (a, b, c) = x should not assign to c...
1975 compile_error(ast_ctx(self), "not an l-value (code-block)");
1979 if (self->expression.outr) {
1980 *out = self->expression.outr;
1984 /* output is NULL at first, we'll have each expression
1985 * assign to out output, thus, a comma-operator represention
1986 * using an ast_block will return the last generated value,
1987 * so: (b, c) + a executed both b and c, and returns c,
1988 * which is then added to a.
1992 /* generate locals */
1993 for (i = 0; i < vec_size(self->locals); ++i)
1995 if (!ast_local_codegen(self->locals[i], func->ir_func, false)) {
1996 if (OPTS_OPTION_BOOL(OPTION_DEBUG))
1997 compile_error(ast_ctx(self), "failed to generate local `%s`", self->locals[i]->name);
2002 for (i = 0; i < vec_size(self->exprs); ++i)
2004 ast_expression_codegen *gen;
2005 if (func->curblock->final && !starts_a_label(self->exprs[i])) {
2006 if (compile_warning(ast_ctx(self->exprs[i]), WARN_UNREACHABLE_CODE, "unreachable statement"))
2010 gen = self->exprs[i]->codegen;
2011 if (!(*gen)(self->exprs[i], func, false, out))
2015 self->expression.outr = *out;
2020 bool ast_store_codegen(ast_store *self, ast_function *func, bool lvalue, ir_value **out)
2022 ast_expression_codegen *cgen;
2023 ir_value *left = NULL;
2024 ir_value *right = NULL;
2028 ast_array_index *ai = NULL;
2030 if (lvalue && self->expression.outl) {
2031 *out = self->expression.outl;
2035 if (!lvalue && self->expression.outr) {
2036 *out = self->expression.outr;
2040 if (ast_istype(self->dest, ast_array_index))
2043 ai = (ast_array_index*)self->dest;
2044 idx = (ast_value*)ai->index;
2046 if (ast_istype(ai->index, ast_value) && idx->hasvalue && idx->cvq == CV_CONST)
2051 /* we need to call the setter */
2052 ir_value *iridx, *funval;
2056 compile_error(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
2060 arr = (ast_value*)ai->array;
2061 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
2062 compile_error(ast_ctx(self), "value has no setter (%s)", arr->name);
2066 cgen = idx->expression.codegen;
2067 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
2070 cgen = arr->setter->expression.codegen;
2071 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
2074 cgen = self->source->codegen;
2075 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
2078 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "store"), funval, false);
2081 ir_call_param(call, iridx);
2082 ir_call_param(call, right);
2083 self->expression.outr = right;
2089 cgen = self->dest->codegen;
2091 if (!(*cgen)((ast_expression*)(self->dest), func, true, &left))
2093 self->expression.outl = left;
2095 cgen = self->source->codegen;
2097 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
2100 if (!ir_block_create_store_op(func->curblock, ast_ctx(self), self->op, left, right))
2102 self->expression.outr = right;
2105 /* Theoretically, an assinment returns its left side as an
2106 * lvalue, if we don't need an lvalue though, we return
2107 * the right side as an rvalue, otherwise we have to
2108 * somehow know whether or not we need to dereference the pointer
2109 * on the left side - that is: OP_LOAD if it was an address.
2110 * Also: in original QC we cannot OP_LOADP *anyway*.
2112 *out = (lvalue ? left : right);
2117 bool ast_binary_codegen(ast_binary *self, ast_function *func, bool lvalue, ir_value **out)
2119 ast_expression_codegen *cgen;
2120 ir_value *left, *right;
2122 /* A binary operation cannot yield an l-value */
2124 compile_error(ast_ctx(self), "not an l-value (binop)");
2128 if (self->expression.outr) {
2129 *out = self->expression.outr;
2133 if ((OPTS_FLAG(SHORT_LOGIC) || OPTS_FLAG(PERL_LOGIC)) &&
2134 (self->op == INSTR_AND || self->op == INSTR_OR))
2136 /* NOTE: The short-logic path will ignore right_first */
2138 /* short circuit evaluation */
2139 ir_block *other, *merge;
2140 ir_block *from_left, *from_right;
2144 /* prepare end-block */
2145 merge_id = vec_size(func->ir_func->blocks);
2146 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "sce_merge"));
2148 /* generate the left expression */
2149 cgen = self->left->codegen;
2150 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
2152 /* remember the block */
2153 from_left = func->curblock;
2155 /* create a new block for the right expression */
2156 other = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "sce_other"));
2157 if (self->op == INSTR_AND) {
2158 /* on AND: left==true -> other */
2159 if (!ir_block_create_if(func->curblock, ast_ctx(self), left, other, merge))
2162 /* on OR: left==false -> other */
2163 if (!ir_block_create_if(func->curblock, ast_ctx(self), left, merge, other))
2166 /* use the likely flag */
2167 vec_last(func->curblock->instr)->likely = true;
2169 /* enter the right-expression's block */
2170 func->curblock = other;
2172 cgen = self->right->codegen;
2173 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
2175 /* remember block */
2176 from_right = func->curblock;
2178 /* jump to the merge block */
2179 if (!ir_block_create_jump(func->curblock, ast_ctx(self), merge))
2182 vec_remove(func->ir_func->blocks, merge_id, 1);
2183 vec_push(func->ir_func->blocks, merge);
2185 func->curblock = merge;
2186 phi = ir_block_create_phi(func->curblock, ast_ctx(self),
2187 ast_function_label(func, "sce_value"),
2188 self->expression.vtype);
2189 ir_phi_add(phi, from_left, left);
2190 ir_phi_add(phi, from_right, right);
2191 *out = ir_phi_value(phi);
2195 if (!OPTS_FLAG(PERL_LOGIC)) {
2197 if (OPTS_FLAG(CORRECT_LOGIC) && (*out)->vtype == TYPE_VECTOR) {
2198 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
2199 ast_function_label(func, "sce_bool_v"),
2203 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
2204 ast_function_label(func, "sce_bool"),
2209 else if (OPTS_FLAG(FALSE_EMPTY_STRINGS) && (*out)->vtype == TYPE_STRING) {
2210 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
2211 ast_function_label(func, "sce_bool_s"),
2215 *out = ir_block_create_unary(func->curblock, ast_ctx(self),
2216 ast_function_label(func, "sce_bool"),
2222 *out = ir_block_create_binop(func->curblock, ast_ctx(self),
2223 ast_function_label(func, "sce_bool"),
2224 INSTR_AND, *out, *out);
2230 self->expression.outr = *out;
2231 codegen_output_type(self, *out);
2235 if (self->right_first) {
2236 cgen = self->right->codegen;
2237 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
2239 cgen = self->left->codegen;
2240 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
2243 cgen = self->left->codegen;
2244 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
2246 cgen = self->right->codegen;
2247 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
2251 *out = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "bin"),
2252 self->op, left, right);
2255 self->expression.outr = *out;
2256 codegen_output_type(self, *out);
2261 bool ast_binstore_codegen(ast_binstore *self, ast_function *func, bool lvalue, ir_value **out)
2263 ast_expression_codegen *cgen;
2264 ir_value *leftl = NULL, *leftr, *right, *bin;
2268 ast_array_index *ai = NULL;
2269 ir_value *iridx = NULL;
2271 if (lvalue && self->expression.outl) {
2272 *out = self->expression.outl;
2276 if (!lvalue && self->expression.outr) {
2277 *out = self->expression.outr;
2281 if (ast_istype(self->dest, ast_array_index))
2284 ai = (ast_array_index*)self->dest;
2285 idx = (ast_value*)ai->index;
2287 if (ast_istype(ai->index, ast_value) && idx->hasvalue && idx->cvq == CV_CONST)
2291 /* for a binstore we need both an lvalue and an rvalue for the left side */
2292 /* rvalue of destination! */
2294 cgen = idx->expression.codegen;
2295 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
2298 cgen = self->dest->codegen;
2299 if (!(*cgen)((ast_expression*)(self->dest), func, false, &leftr))
2302 /* source as rvalue only */
2303 cgen = self->source->codegen;
2304 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
2307 /* now the binary */
2308 bin = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "binst"),
2309 self->opbin, leftr, right);
2310 self->expression.outr = bin;
2314 /* we need to call the setter */
2319 compile_error(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
2323 arr = (ast_value*)ai->array;
2324 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
2325 compile_error(ast_ctx(self), "value has no setter (%s)", arr->name);
2329 cgen = arr->setter->expression.codegen;
2330 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
2333 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "store"), funval, false);
2336 ir_call_param(call, iridx);
2337 ir_call_param(call, bin);
2338 self->expression.outr = bin;
2340 /* now store them */
2341 cgen = self->dest->codegen;
2342 /* lvalue of destination */
2343 if (!(*cgen)((ast_expression*)(self->dest), func, true, &leftl))
2345 self->expression.outl = leftl;
2347 if (!ir_block_create_store_op(func->curblock, ast_ctx(self), self->opstore, leftl, bin))
2349 self->expression.outr = bin;
2352 /* Theoretically, an assinment returns its left side as an
2353 * lvalue, if we don't need an lvalue though, we return
2354 * the right side as an rvalue, otherwise we have to
2355 * somehow know whether or not we need to dereference the pointer
2356 * on the left side - that is: OP_LOAD if it was an address.
2357 * Also: in original QC we cannot OP_LOADP *anyway*.
2359 *out = (lvalue ? leftl : bin);
2364 bool ast_unary_codegen(ast_unary *self, ast_function *func, bool lvalue, ir_value **out)
2366 ast_expression_codegen *cgen;
2369 /* An unary operation cannot yield an l-value */
2371 compile_error(ast_ctx(self), "not an l-value (binop)");
2375 if (self->expression.outr) {
2376 *out = self->expression.outr;
2380 cgen = self->operand->codegen;
2382 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
2385 *out = ir_block_create_unary(func->curblock, ast_ctx(self), ast_function_label(func, "unary"),
2389 self->expression.outr = *out;
2394 bool ast_return_codegen(ast_return *self, ast_function *func, bool lvalue, ir_value **out)
2396 ast_expression_codegen *cgen;
2401 /* In the context of a return operation, we don't actually return
2405 compile_error(ast_ctx(self), "return-expression is not an l-value");
2409 if (self->expression.outr) {
2410 compile_error(ast_ctx(self), "internal error: ast_return cannot be reused, it bears no result!");
2413 self->expression.outr = (ir_value*)1;
2415 if (self->operand) {
2416 cgen = self->operand->codegen;
2418 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
2421 if (!ir_block_create_return(func->curblock, ast_ctx(self), operand))
2424 if (!ir_block_create_return(func->curblock, ast_ctx(self), NULL))
2431 bool ast_entfield_codegen(ast_entfield *self, ast_function *func, bool lvalue, ir_value **out)
2433 ast_expression_codegen *cgen;
2434 ir_value *ent, *field;
2436 /* This function needs to take the 'lvalue' flag into account!
2437 * As lvalue we provide a field-pointer, as rvalue we provide the
2441 if (lvalue && self->expression.outl) {
2442 *out = self->expression.outl;
2446 if (!lvalue && self->expression.outr) {
2447 *out = self->expression.outr;
2451 cgen = self->entity->codegen;
2452 if (!(*cgen)((ast_expression*)(self->entity), func, false, &ent))
2455 cgen = self->field->codegen;
2456 if (!(*cgen)((ast_expression*)(self->field), func, false, &field))
2461 *out = ir_block_create_fieldaddress(func->curblock, ast_ctx(self), ast_function_label(func, "efa"),
2464 *out = ir_block_create_load_from_ent(func->curblock, ast_ctx(self), ast_function_label(func, "efv"),
2465 ent, field, self->expression.vtype);
2466 /* Done AFTER error checking:
2467 codegen_output_type(self, *out);
2471 compile_error(ast_ctx(self), "failed to create %s instruction (output type %s)",
2472 (lvalue ? "ADDRESS" : "FIELD"),
2473 type_name[self->expression.vtype]);
2477 codegen_output_type(self, *out);
2480 self->expression.outl = *out;
2482 self->expression.outr = *out;
2484 /* Hm that should be it... */
2488 bool ast_member_codegen(ast_member *self, ast_function *func, bool lvalue, ir_value **out)
2490 ast_expression_codegen *cgen;
2493 /* in QC this is always an lvalue */
2494 if (lvalue && self->rvalue) {
2495 compile_error(ast_ctx(self), "not an l-value (member access)");
2498 if (self->expression.outl) {
2499 *out = self->expression.outl;
2503 cgen = self->owner->codegen;
2504 if (!(*cgen)((ast_expression*)(self->owner), func, false, &vec))
2507 if (vec->vtype != TYPE_VECTOR &&
2508 !(vec->vtype == TYPE_FIELD && self->owner->next->vtype == TYPE_VECTOR))
2513 *out = ir_value_vector_member(vec, self->field);
2514 self->expression.outl = *out;
2516 return (*out != NULL);
2519 bool ast_array_index_codegen(ast_array_index *self, ast_function *func, bool lvalue, ir_value **out)
2524 if (!lvalue && self->expression.outr) {
2525 *out = self->expression.outr;
2528 if (lvalue && self->expression.outl) {
2529 *out = self->expression.outl;
2533 if (!ast_istype(self->array, ast_value)) {
2534 compile_error(ast_ctx(self), "array indexing this way is not supported");
2535 /* note this would actually be pointer indexing because the left side is
2536 * not an actual array but (hopefully) an indexable expression.
2537 * Once we get integer arithmetic, and GADDRESS/GSTORE/GLOAD instruction
2538 * support this path will be filled.
2543 arr = (ast_value*)self->array;
2544 idx = (ast_value*)self->index;
2546 if (!ast_istype(self->index, ast_value) || !idx->hasvalue || idx->cvq != CV_CONST) {
2547 /* Time to use accessor functions */
2548 ast_expression_codegen *cgen;
2549 ir_value *iridx, *funval;
2553 compile_error(ast_ctx(self), "(.2) array indexing here needs a compile-time constant");
2558 compile_error(ast_ctx(self), "value has no getter, don't know how to index it");
2562 cgen = self->index->codegen;
2563 if (!(*cgen)((ast_expression*)(self->index), func, false, &iridx))
2566 cgen = arr->getter->expression.codegen;
2567 if (!(*cgen)((ast_expression*)(arr->getter), func, true, &funval))
2570 call = ir_block_create_call(func->curblock, ast_ctx(self), ast_function_label(func, "fetch"), funval, false);
2573 ir_call_param(call, iridx);
2575 *out = ir_call_value(call);
2576 self->expression.outr = *out;
2577 (*out)->vtype = self->expression.vtype;
2578 codegen_output_type(self, *out);
2582 if (idx->expression.vtype == TYPE_FLOAT) {
2583 unsigned int arridx = idx->constval.vfloat;
2584 if (arridx >= self->array->count)
2586 compile_error(ast_ctx(self), "array index out of bounds: %i", arridx);
2589 *out = arr->ir_values[arridx];
2591 else if (idx->expression.vtype == TYPE_INTEGER) {
2592 unsigned int arridx = idx->constval.vint;
2593 if (arridx >= self->array->count)
2595 compile_error(ast_ctx(self), "array index out of bounds: %i", arridx);
2598 *out = arr->ir_values[arridx];
2601 compile_error(ast_ctx(self), "array indexing here needs an integer constant");
2604 (*out)->vtype = self->expression.vtype;
2605 codegen_output_type(self, *out);
2609 bool ast_argpipe_codegen(ast_argpipe *self, ast_function *func, bool lvalue, ir_value **out)
2613 compile_error(ast_ctx(self), "argpipe node: not an lvalue");
2618 compile_error(ast_ctx(self), "TODO: argpipe codegen not implemented");
2622 bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_value **out)
2624 ast_expression_codegen *cgen;
2632 ir_block *ontrue_endblock = NULL;
2633 ir_block *onfalse_endblock = NULL;
2634 ir_block *merge = NULL;
2637 /* We don't output any value, thus also don't care about r/lvalue */
2641 if (self->expression.outr) {
2642 compile_error(ast_ctx(self), "internal error: ast_ifthen cannot be reused, it bears no result!");
2645 self->expression.outr = (ir_value*)1;
2647 /* generate the condition */
2648 cgen = self->cond->codegen;
2649 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2651 /* update the block which will get the jump - because short-logic or ternaries may have changed this */
2652 cond = func->curblock;
2654 /* try constant folding away the condition */
2655 if ((fold = fold_cond_ifthen(condval, func, self)) != -1)
2658 if (self->on_true) {
2659 /* create on-true block */
2660 ontrue = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "ontrue"));
2664 /* enter the block */
2665 func->curblock = ontrue;
2668 cgen = self->on_true->codegen;
2669 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &dummy))
2672 /* we now need to work from the current endpoint */
2673 ontrue_endblock = func->curblock;
2678 if (self->on_false) {
2679 /* create on-false block */
2680 onfalse = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "onfalse"));
2684 /* enter the block */
2685 func->curblock = onfalse;
2688 cgen = self->on_false->codegen;
2689 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &dummy))
2692 /* we now need to work from the current endpoint */
2693 onfalse_endblock = func->curblock;
2697 /* Merge block were they all merge in to */
2698 if (!ontrue || !onfalse || !ontrue_endblock->final || !onfalse_endblock->final)
2700 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "endif"));
2703 /* add jumps ot the merge block */
2704 if (ontrue && !ontrue_endblock->final && !ir_block_create_jump(ontrue_endblock, ast_ctx(self), merge))
2706 if (onfalse && !onfalse_endblock->final && !ir_block_create_jump(onfalse_endblock, ast_ctx(self), merge))
2709 /* Now enter the merge block */
2710 func->curblock = merge;
2713 /* we create the if here, that way all blocks are ordered :)
2715 if (!ir_block_create_if(cond, ast_ctx(self), condval,
2716 (ontrue ? ontrue : merge),
2717 (onfalse ? onfalse : merge)))
2725 bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_value **out)
2727 ast_expression_codegen *cgen;
2730 ir_value *trueval, *falseval;
2733 ir_block *cond = func->curblock;
2734 ir_block *cond_out = NULL;
2735 ir_block *ontrue, *ontrue_out = NULL;
2736 ir_block *onfalse, *onfalse_out = NULL;
2740 /* Ternary can never create an lvalue... */
2744 /* In theory it shouldn't be possible to pass through a node twice, but
2745 * in case we add any kind of optimization pass for the AST itself, it
2746 * may still happen, thus we remember a created ir_value and simply return one
2747 * if it already exists.
2749 if (self->expression.outr) {
2750 *out = self->expression.outr;
2754 /* In the following, contraty to ast_ifthen, we assume both paths exist. */
2756 /* generate the condition */
2757 func->curblock = cond;
2758 cgen = self->cond->codegen;
2759 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2761 cond_out = func->curblock;
2763 /* try constant folding away the condition */
2764 if ((fold = fold_cond_ternary(condval, func, self)) != -1)
2767 /* create on-true block */
2768 ontrue = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_T"));
2773 /* enter the block */
2774 func->curblock = ontrue;
2777 cgen = self->on_true->codegen;
2778 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &trueval))
2781 ontrue_out = func->curblock;
2784 /* create on-false block */
2785 onfalse = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_F"));
2790 /* enter the block */
2791 func->curblock = onfalse;
2794 cgen = self->on_false->codegen;
2795 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &falseval))
2798 onfalse_out = func->curblock;
2801 /* create merge block */
2802 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_out"));
2805 /* jump to merge block */
2806 if (!ir_block_create_jump(ontrue_out, ast_ctx(self), merge))
2808 if (!ir_block_create_jump(onfalse_out, ast_ctx(self), merge))
2811 /* create if instruction */
2812 if (!ir_block_create_if(cond_out, ast_ctx(self), condval, ontrue, onfalse))
2815 /* Now enter the merge block */
2816 func->curblock = merge;
2818 /* Here, now, we need a PHI node
2819 * but first some sanity checking...
2821 if (trueval->vtype != falseval->vtype && trueval->vtype != TYPE_NIL && falseval->vtype != TYPE_NIL) {
2822 /* error("ternary with different types on the two sides"); */
2823 compile_error(ast_ctx(self), "internal error: ternary operand types invalid");
2828 phi = ir_block_create_phi(merge, ast_ctx(self), ast_function_label(func, "phi"), self->expression.vtype);
2830 compile_error(ast_ctx(self), "internal error: failed to generate phi node");
2833 ir_phi_add(phi, ontrue_out, trueval);
2834 ir_phi_add(phi, onfalse_out, falseval);
2836 self->expression.outr = ir_phi_value(phi);
2837 *out = self->expression.outr;
2839 codegen_output_type(self, *out);
2844 bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value **out)
2846 ast_expression_codegen *cgen;
2848 ir_value *dummy = NULL;
2849 ir_value *precond = NULL;
2850 ir_value *postcond = NULL;
2852 /* Since we insert some jumps "late" so we have blocks
2853 * ordered "nicely", we need to keep track of the actual end-blocks
2854 * of expressions to add the jumps to.
2856 ir_block *bbody = NULL, *end_bbody = NULL;
2857 ir_block *bprecond = NULL, *end_bprecond = NULL;
2858 ir_block *bpostcond = NULL, *end_bpostcond = NULL;
2859 ir_block *bincrement = NULL, *end_bincrement = NULL;
2860 ir_block *bout = NULL, *bin = NULL;
2862 /* let's at least move the outgoing block to the end */
2865 /* 'break' and 'continue' need to be able to find the right blocks */
2866 ir_block *bcontinue = NULL;
2867 ir_block *bbreak = NULL;
2869 ir_block *tmpblock = NULL;
2874 if (self->expression.outr) {
2875 compile_error(ast_ctx(self), "internal error: ast_loop cannot be reused, it bears no result!");
2878 self->expression.outr = (ir_value*)1;
2881 * Should we ever need some kind of block ordering, better make this function
2882 * move blocks around than write a block ordering algorithm later... after all
2883 * the ast and ir should work together, not against each other.
2886 /* initexpr doesn't get its own block, it's pointless, it could create more blocks
2887 * anyway if for example it contains a ternary.
2891 cgen = self->initexpr->codegen;
2892 if (!(*cgen)((ast_expression*)(self->initexpr), func, false, &dummy))
2896 /* Store the block from which we enter this chaos */
2897 bin = func->curblock;
2899 /* The pre-loop condition needs its own block since we
2900 * need to be able to jump to the start of that expression.
2904 bprecond = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "pre_loop_cond"));
2908 /* the pre-loop-condition the least important place to 'continue' at */
2909 bcontinue = bprecond;
2912 func->curblock = bprecond;
2915 cgen = self->precond->codegen;
2916 if (!(*cgen)((ast_expression*)(self->precond), func, false, &precond))
2919 end_bprecond = func->curblock;
2921 bprecond = end_bprecond = NULL;
2924 /* Now the next blocks won't be ordered nicely, but we need to
2925 * generate them this early for 'break' and 'continue'.
2927 if (self->increment) {
2928 bincrement = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "loop_increment"));
2931 bcontinue = bincrement; /* increment comes before the pre-loop-condition */
2933 bincrement = end_bincrement = NULL;
2936 if (self->postcond) {
2937 bpostcond = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "post_loop_cond"));
2940 bcontinue = bpostcond; /* postcond comes before the increment */
2942 bpostcond = end_bpostcond = NULL;
2945 bout_id = vec_size(func->ir_func->blocks);
2946 bout = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "after_loop"));
2951 /* The loop body... */
2952 /* if (self->body) */
2954 bbody = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "loop_body"));
2959 func->curblock = bbody;
2961 vec_push(func->breakblocks, bbreak);
2963 vec_push(func->continueblocks, bcontinue);
2965 vec_push(func->continueblocks, bbody);
2969 cgen = self->body->codegen;
2970 if (!(*cgen)((ast_expression*)(self->body), func, false, &dummy))
2974 end_bbody = func->curblock;
2975 vec_pop(func->breakblocks);
2976 vec_pop(func->continueblocks);
2979 /* post-loop-condition */
2983 func->curblock = bpostcond;
2986 cgen = self->postcond->codegen;
2987 if (!(*cgen)((ast_expression*)(self->postcond), func, false, &postcond))
2990 end_bpostcond = func->curblock;
2993 /* The incrementor */
2994 if (self->increment)
2997 func->curblock = bincrement;
3000 cgen = self->increment->codegen;
3001 if (!(*cgen)((ast_expression*)(self->increment), func, false, &dummy))
3004 end_bincrement = func->curblock;
3007 /* In any case now, we continue from the outgoing block */
3008 func->curblock = bout;
3010 /* Now all blocks are in place */
3011 /* From 'bin' we jump to whatever comes first */
3012 if (bprecond) tmpblock = bprecond;
3013 else tmpblock = bbody; /* can never be null */
3016 else if (bpostcond) tmpblock = bpostcond;
3017 else tmpblock = bout;
3020 if (!ir_block_create_jump(bin, ast_ctx(self), tmpblock))
3026 ir_block *ontrue, *onfalse;
3027 ontrue = bbody; /* can never be null */
3029 /* all of this is dead code
3030 else if (bincrement) ontrue = bincrement;
3031 else ontrue = bpostcond;
3035 if (self->pre_not) {
3040 if (!ir_block_create_if(end_bprecond, ast_ctx(self), precond, ontrue, onfalse))
3047 if (bincrement) tmpblock = bincrement;
3048 else if (bpostcond) tmpblock = bpostcond;
3049 else if (bprecond) tmpblock = bprecond;
3050 else tmpblock = bbody;
3051 if (!end_bbody->final && !ir_block_create_jump(end_bbody, ast_ctx(self), tmpblock))
3055 /* from increment */
3058 if (bpostcond) tmpblock = bpostcond;
3059 else if (bprecond) tmpblock = bprecond;
3060 else if (bbody) tmpblock = bbody;
3061 else tmpblock = bout;
3062 if (!ir_block_create_jump(end_bincrement, ast_ctx(self), tmpblock))
3069 ir_block *ontrue, *onfalse;
3070 if (bprecond) ontrue = bprecond;
3071 else ontrue = bbody; /* can never be null */
3073 /* all of this is dead code
3074 else if (bincrement) ontrue = bincrement;
3075 else ontrue = bpostcond;
3079 if (self->post_not) {
3084 if (!ir_block_create_if(end_bpostcond, ast_ctx(self), postcond, ontrue, onfalse))
3088 /* Move 'bout' to the end */
3089 vec_remove(func->ir_func->blocks, bout_id, 1);
3090 vec_push(func->ir_func->blocks, bout);
3095 bool ast_breakcont_codegen(ast_breakcont *self, ast_function *func, bool lvalue, ir_value **out)
3102 compile_error(ast_ctx(self), "break/continue expression is not an l-value");
3106 if (self->expression.outr) {
3107 compile_error(ast_ctx(self), "internal error: ast_breakcont cannot be reused!");
3110 self->expression.outr = (ir_value*)1;
3112 if (self->is_continue)
3113 target = func->continueblocks[vec_size(func->continueblocks)-1-self->levels];
3115 target = func->breakblocks[vec_size(func->breakblocks)-1-self->levels];
3118 compile_error(ast_ctx(self), "%s is lacking a target block", (self->is_continue ? "continue" : "break"));
3122 if (!ir_block_create_jump(func->curblock, ast_ctx(self), target))
3127 bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_value **out)
3129 ast_expression_codegen *cgen;
3131 ast_switch_case *def_case = NULL;
3132 ir_block *def_bfall = NULL;
3133 ir_block *def_bfall_to = NULL;
3134 bool set_def_bfall_to = false;
3136 ir_value *dummy = NULL;
3137 ir_value *irop = NULL;
3138 ir_block *bout = NULL;
3139 ir_block *bfall = NULL;
3147 compile_error(ast_ctx(self), "switch expression is not an l-value");
3151 if (self->expression.outr) {
3152 compile_error(ast_ctx(self), "internal error: ast_switch cannot be reused!");
3155 self->expression.outr = (ir_value*)1;
3160 cgen = self->operand->codegen;
3161 if (!(*cgen)((ast_expression*)(self->operand), func, false, &irop))
3164 if (!vec_size(self->cases))
3167 cmpinstr = type_eq_instr[irop->vtype];
3168 if (cmpinstr >= VINSTR_END) {
3169 ast_type_to_string(self->operand, typestr, sizeof(typestr));
3170 compile_error(ast_ctx(self), "invalid type to perform a switch on: %s", typestr);
3174 bout_id = vec_size(func->ir_func->blocks);
3175 bout = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "after_switch"));
3179 /* setup the break block */
3180 vec_push(func->breakblocks, bout);
3182 /* Now create all cases */
3183 for (c = 0; c < vec_size(self->cases); ++c) {
3184 ir_value *cond, *val;
3185 ir_block *bcase, *bnot;
3188 ast_switch_case *swcase = &self->cases[c];
3190 if (swcase->value) {
3191 /* A regular case */
3192 /* generate the condition operand */
3193 cgen = swcase->value->codegen;
3194 if (!(*cgen)((ast_expression*)(swcase->value), func, false, &val))
3196 /* generate the condition */
3197 cond = ir_block_create_binop(func->curblock, ast_ctx(self), ast_function_label(func, "switch_eq"), cmpinstr, irop, val);
3201 bcase = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "case"));
3202 bnot_id = vec_size(func->ir_func->blocks);
3203 bnot = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "not_case"));
3204 if (!bcase || !bnot)
3206 if (set_def_bfall_to) {
3207 set_def_bfall_to = false;
3208 def_bfall_to = bcase;
3210 if (!ir_block_create_if(func->curblock, ast_ctx(self), cond, bcase, bnot))
3213 /* Make the previous case-end fall through */
3214 if (bfall && !bfall->final) {
3215 if (!ir_block_create_jump(bfall, ast_ctx(self), bcase))
3219 /* enter the case */
3220 func->curblock = bcase;
3221 cgen = swcase->code->codegen;
3222 if (!(*cgen)((ast_expression*)swcase->code, func, false, &dummy))
3225 /* remember this block to fall through from */
3226 bfall = func->curblock;
3228 /* enter the else and move it down */
3229 func->curblock = bnot;
3230 vec_remove(func->ir_func->blocks, bnot_id, 1);
3231 vec_push(func->ir_func->blocks, bnot);
3233 /* The default case */
3234 /* Remember where to fall through from: */
3237 /* remember which case it was */
3239 /* And the next case will be remembered */
3240 set_def_bfall_to = true;
3244 /* Jump from the last bnot to bout */
3245 if (bfall && !bfall->final && !ir_block_create_jump(bfall, ast_ctx(self), bout)) {
3247 astwarning(ast_ctx(bfall), WARN_???, "missing break after last case");
3252 /* If there was a default case, put it down here */
3256 /* No need to create an extra block */
3257 bcase = func->curblock;
3259 /* Insert the fallthrough jump */
3260 if (def_bfall && !def_bfall->final) {
3261 if (!ir_block_create_jump(def_bfall, ast_ctx(self), bcase))
3265 /* Now generate the default code */
3266 cgen = def_case->code->codegen;
3267 if (!(*cgen)((ast_expression*)def_case->code, func, false, &dummy))
3270 /* see if we need to fall through */
3271 if (def_bfall_to && !func->curblock->final)
3273 if (!ir_block_create_jump(func->curblock, ast_ctx(self), def_bfall_to))
3278 /* Jump from the last bnot to bout */
3279 if (!func->curblock->final && !ir_block_create_jump(func->curblock, ast_ctx(self), bout))
3281 /* enter the outgoing block */
3282 func->curblock = bout;
3284 /* restore the break block */
3285 vec_pop(func->breakblocks);
3287 /* Move 'bout' to the end, it's nicer */
3288 vec_remove(func->ir_func->blocks, bout_id, 1);
3289 vec_push(func->ir_func->blocks, bout);
3294 bool ast_label_codegen(ast_label *self, ast_function *func, bool lvalue, ir_value **out)
3299 if (self->undefined) {
3300 compile_error(ast_ctx(self), "internal error: ast_label never defined");
3306 compile_error(ast_ctx(self), "internal error: ast_label cannot be an lvalue");
3310 /* simply create a new block and jump to it */
3311 self->irblock = ir_function_create_block(ast_ctx(self), func->ir_func, self->name);
3312 if (!self->irblock) {
3313 compile_error(ast_ctx(self), "failed to allocate label block `%s`", self->name);
3316 if (!func->curblock->final) {
3317 if (!ir_block_create_jump(func->curblock, ast_ctx(self), self->irblock))
3321 /* enter the new block */
3322 func->curblock = self->irblock;
3324 /* Generate all the leftover gotos */
3325 for (i = 0; i < vec_size(self->gotos); ++i) {
3326 if (!ast_goto_codegen(self->gotos[i], func, false, &dummy))
3333 bool ast_goto_codegen(ast_goto *self, ast_function *func, bool lvalue, ir_value **out)
3337 compile_error(ast_ctx(self), "internal error: ast_goto cannot be an lvalue");
3341 if (self->target->irblock) {
3342 if (self->irblock_from) {
3343 /* we already tried once, this is the callback */
3344 self->irblock_from->final = false;
3345 if (!ir_block_create_goto(self->irblock_from, ast_ctx(self), self->target->irblock)) {
3346 compile_error(ast_ctx(self), "failed to generate goto to `%s`", self->name);
3352 if (!ir_block_create_goto(func->curblock, ast_ctx(self), self->target->irblock)) {
3353 compile_error(ast_ctx(self), "failed to generate goto to `%s`", self->name);
3360 /* the target has not yet been created...
3361 * close this block in a sneaky way:
3363 func->curblock->final = true;
3364 self->irblock_from = func->curblock;
3365 ast_label_register_goto(self->target, self);
3372 bool ast_state_codegen(ast_state *self, ast_function *func, bool lvalue, ir_value **out)
3374 ast_expression_codegen *cgen;
3376 ir_value *frameval, *thinkval;
3379 compile_error(ast_ctx(self), "not an l-value (state operation)");
3382 if (self->expression.outr) {
3383 compile_error(ast_ctx(self), "internal error: ast_state cannot be reused!");
3388 cgen = self->framenum->codegen;
3389 if (!(*cgen)((ast_expression*)(self->framenum), func, false, &frameval))
3394 cgen = self->nextthink->codegen;
3395 if (!(*cgen)((ast_expression*)(self->nextthink), func, false, &thinkval))
3400 if (!ir_block_create_state_op(func->curblock, ast_ctx(self), frameval, thinkval)) {
3401 compile_error(ast_ctx(self), "failed to create STATE instruction");
3405 self->expression.outr = (ir_value*)1;
3409 bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value **out)
3411 ast_expression_codegen *cgen;
3413 ir_instr *callinstr;
3416 ir_value *funval = NULL;
3418 /* return values are never lvalues */
3420 compile_error(ast_ctx(self), "not an l-value (function call)");
3424 if (self->expression.outr) {
3425 *out = self->expression.outr;
3429 cgen = self->func->codegen;
3430 if (!(*cgen)((ast_expression*)(self->func), func, false, &funval))
3438 for (i = 0; i < vec_size(self->params); ++i)
3441 ast_expression *expr = self->params[i];
3443 cgen = expr->codegen;
3444 if (!(*cgen)(expr, func, false, ¶m))
3448 vec_push(params, param);
3451 /* varargs counter */
3452 if (self->va_count) {
3454 ir_builder *builder = func->curblock->owner->owner;
3455 cgen = self->va_count->codegen;
3456 if (!(*cgen)((ast_expression*)(self->va_count), func, false, &va_count))
3458 if (!ir_block_create_store_op(func->curblock, ast_ctx(self), INSTR_STORE_F,
3459 ir_builder_get_va_count(builder), va_count))
3465 callinstr = ir_block_create_call(func->curblock, ast_ctx(self),
3466 ast_function_label(func, "call"),
3467 funval, !!(self->func->flags & AST_FLAG_NORETURN));
3471 for (i = 0; i < vec_size(params); ++i) {
3472 ir_call_param(callinstr, params[i]);
3475 *out = ir_call_value(callinstr);
3476 self->expression.outr = *out;
3478 codegen_output_type(self, *out);