5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is furnished to do
10 * so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 #define ast_instantiate(T, ctx, destroyfn) \
31 T* self = (T*)mem_a(sizeof(T)); \
35 ast_node_init((ast_node*)self, ctx, TYPE_##T); \
36 ( (ast_node*)self )->node.destroy = (ast_node_delete*)destroyfn
40 static void asterror(lex_ctx ctx, const char *msg, ...)
44 con_cvprintmsg((void*)&ctx, LVL_ERROR, "error", msg, ap);
48 /* It must not be possible to get here. */
49 static GMQCC_NORETURN void _ast_node_destroy(ast_node *self)
52 con_err("ast node missing destroy()\n");
56 /* Initialize main ast node aprts */
57 static void ast_node_init(ast_node *self, lex_ctx ctx, int nodetype)
59 self->node.context = ctx;
60 self->node.destroy = &_ast_node_destroy;
61 self->node.keep = false;
62 self->node.nodetype = nodetype;
63 self->node.side_effects = false;
66 /* weight and side effects */
67 static void _ast_propagate_effects(ast_node *self, ast_node *other)
69 if (ast_side_effects(other))
70 ast_side_effects(self) = true;
72 #define ast_propagate_effects(s,o) _ast_propagate_effects(((ast_node*)(s)), ((ast_node*)(o)))
74 /* General expression initialization */
75 static void ast_expression_init(ast_expression *self,
76 ast_expression_codegen *codegen)
78 self->expression.codegen = codegen;
79 self->expression.vtype = TYPE_VOID;
80 self->expression.next = NULL;
81 self->expression.outl = NULL;
82 self->expression.outr = NULL;
83 self->expression.variadic = false;
84 self->expression.params = NULL;
87 static void ast_expression_delete(ast_expression *self)
90 if (self->expression.next)
91 ast_delete(self->expression.next);
92 for (i = 0; i < vec_size(self->expression.params); ++i) {
93 ast_delete(self->expression.params[i]);
95 vec_free(self->expression.params);
98 static void ast_expression_delete_full(ast_expression *self)
100 ast_expression_delete(self);
104 ast_value* ast_value_copy(const ast_value *self)
107 const ast_expression_common *fromex;
108 ast_expression_common *selfex;
109 ast_value *cp = ast_value_new(self->expression.node.context, self->name, self->expression.vtype);
110 if (self->expression.next) {
111 cp->expression.next = ast_type_copy(self->expression.node.context, self->expression.next);
112 if (!cp->expression.next) {
113 ast_value_delete(cp);
117 fromex = &self->expression;
118 selfex = &cp->expression;
119 selfex->variadic = fromex->variadic;
120 for (i = 0; i < vec_size(fromex->params); ++i) {
121 ast_value *v = ast_value_copy(fromex->params[i]);
123 ast_value_delete(cp);
126 vec_push(selfex->params, v);
131 bool ast_type_adopt_impl(ast_expression *self, const ast_expression *other)
134 const ast_expression_common *fromex;
135 ast_expression_common *selfex;
136 self->expression.vtype = other->expression.vtype;
137 if (other->expression.next) {
138 self->expression.next = (ast_expression*)ast_type_copy(ast_ctx(self), other->expression.next);
139 if (!self->expression.next)
142 fromex = &other->expression;
143 selfex = &self->expression;
144 selfex->variadic = fromex->variadic;
145 for (i = 0; i < vec_size(fromex->params); ++i) {
146 ast_value *v = ast_value_copy(fromex->params[i]);
149 vec_push(selfex->params, v);
154 static ast_expression* ast_shallow_type(lex_ctx ctx, int vtype)
156 ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
157 ast_expression_init(self, NULL);
158 self->expression.codegen = NULL;
159 self->expression.next = NULL;
160 self->expression.vtype = vtype;
164 ast_expression* ast_type_copy(lex_ctx ctx, const ast_expression *ex)
167 const ast_expression_common *fromex;
168 ast_expression_common *selfex;
174 ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
175 ast_expression_init(self, NULL);
177 fromex = &ex->expression;
178 selfex = &self->expression;
180 /* This may never be codegen()d */
181 selfex->codegen = NULL;
183 selfex->vtype = fromex->vtype;
186 selfex->next = ast_type_copy(ctx, fromex->next);
188 ast_expression_delete_full(self);
195 selfex->variadic = fromex->variadic;
196 for (i = 0; i < vec_size(fromex->params); ++i) {
197 ast_value *v = ast_value_copy(fromex->params[i]);
199 ast_expression_delete_full(self);
202 vec_push(selfex->params, v);
209 bool ast_compare_type(ast_expression *a, ast_expression *b)
211 if (a->expression.vtype != b->expression.vtype)
213 if (!a->expression.next != !b->expression.next)
215 if (vec_size(a->expression.params) != vec_size(b->expression.params))
217 if (a->expression.variadic != b->expression.variadic)
219 if (vec_size(a->expression.params)) {
221 for (i = 0; i < vec_size(a->expression.params); ++i) {
222 if (!ast_compare_type((ast_expression*)a->expression.params[i],
223 (ast_expression*)b->expression.params[i]))
227 if (a->expression.next)
228 return ast_compare_type(a->expression.next, b->expression.next);
232 static size_t ast_type_to_string_impl(ast_expression *e, char *buf, size_t bufsize, size_t pos)
239 if (pos + 6 >= bufsize)
241 strcpy(buf + pos, "(null)");
245 if (pos + 1 >= bufsize)
248 switch (e->expression.vtype) {
250 strcpy(buf + pos, "(variant)");
255 return ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
258 if (pos + 3 >= bufsize)
262 pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
263 if (pos + 1 >= bufsize)
269 pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
270 if (pos + 2 >= bufsize)
272 if (!vec_size(e->expression.params)) {
278 pos = ast_type_to_string_impl((ast_expression*)(e->expression.params[0]), buf, bufsize, pos);
279 for (i = 1; i < vec_size(e->expression.params); ++i) {
280 if (pos + 2 >= bufsize)
284 pos = ast_type_to_string_impl((ast_expression*)(e->expression.params[i]), buf, bufsize, pos);
286 if (pos + 1 >= bufsize)
292 pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
293 if (pos + 1 >= bufsize)
296 pos += snprintf(buf + pos, bufsize - pos - 1, "%i", (int)e->expression.count);
297 if (pos + 1 >= bufsize)
303 typestr = type_name[e->expression.vtype];
304 typelen = strlen(typestr);
305 if (pos + typelen >= bufsize)
307 strcpy(buf + pos, typestr);
308 return pos + typelen;
312 buf[bufsize-3] = '.';
313 buf[bufsize-2] = '.';
314 buf[bufsize-1] = '.';
318 void ast_type_to_string(ast_expression *e, char *buf, size_t bufsize)
320 size_t pos = ast_type_to_string_impl(e, buf, bufsize-1, 0);
324 ast_value* ast_value_new(lex_ctx ctx, const char *name, int t)
326 ast_instantiate(ast_value, ctx, ast_value_delete);
327 ast_expression_init((ast_expression*)self,
328 (ast_expression_codegen*)&ast_value_codegen);
329 self->expression.node.keep = true; /* keep */
331 self->name = name ? util_strdup(name) : NULL;
332 self->expression.vtype = t;
333 self->expression.next = NULL;
334 self->constant = false;
335 self->hasvalue = false;
337 memset(&self->constval, 0, sizeof(self->constval));
340 self->ir_values = NULL;
341 self->ir_value_count = 0;
349 void ast_value_delete(ast_value* self)
352 mem_d((void*)self->name);
353 if (self->hasvalue) {
354 switch (self->expression.vtype)
357 mem_d((void*)self->constval.vstring);
360 /* unlink us from the function node */
361 self->constval.vfunc->vtype = NULL;
363 /* NOTE: delete function? currently collected in
364 * the parser structure
371 mem_d(self->ir_values);
372 ast_expression_delete((ast_expression*)self);
376 void ast_value_params_add(ast_value *self, ast_value *p)
378 vec_push(self->expression.params, p);
381 bool ast_value_set_name(ast_value *self, const char *name)
384 mem_d((void*)self->name);
385 self->name = util_strdup(name);
389 ast_binary* ast_binary_new(lex_ctx ctx, int op,
390 ast_expression* left, ast_expression* right)
392 ast_instantiate(ast_binary, ctx, ast_binary_delete);
393 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binary_codegen);
399 ast_propagate_effects(self, left);
400 ast_propagate_effects(self, right);
402 if (op >= INSTR_EQ_F && op <= INSTR_GT)
403 self->expression.vtype = TYPE_FLOAT;
404 else if (op == INSTR_AND || op == INSTR_OR ||
405 op == INSTR_BITAND || op == INSTR_BITOR)
406 self->expression.vtype = TYPE_FLOAT;
407 else if (op == INSTR_MUL_VF || op == INSTR_MUL_FV)
408 self->expression.vtype = TYPE_VECTOR;
409 else if (op == INSTR_MUL_V)
410 self->expression.vtype = TYPE_FLOAT;
412 self->expression.vtype = left->expression.vtype;
417 void ast_binary_delete(ast_binary *self)
419 ast_unref(self->left);
420 ast_unref(self->right);
421 ast_expression_delete((ast_expression*)self);
425 ast_binstore* ast_binstore_new(lex_ctx ctx, int storop, int op,
426 ast_expression* left, ast_expression* right)
428 ast_instantiate(ast_binstore, ctx, ast_binstore_delete);
429 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binstore_codegen);
431 ast_side_effects(self) = true;
433 self->opstore = storop;
436 self->source = right;
438 self->keep_dest = false;
440 self->expression.vtype = left->expression.vtype;
441 if (left->expression.next) {
442 self->expression.next = ast_type_copy(ctx, left);
443 if (!self->expression.next) {
449 self->expression.next = NULL;
454 void ast_binstore_delete(ast_binstore *self)
456 if (!self->keep_dest)
457 ast_unref(self->dest);
458 ast_unref(self->source);
459 ast_expression_delete((ast_expression*)self);
463 ast_unary* ast_unary_new(lex_ctx ctx, int op,
464 ast_expression *expr)
466 ast_instantiate(ast_unary, ctx, ast_unary_delete);
467 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_unary_codegen);
470 self->operand = expr;
472 ast_propagate_effects(self, expr);
474 if (op >= INSTR_NOT_F && op <= INSTR_NOT_FNC) {
475 self->expression.vtype = TYPE_FLOAT;
477 asterror(ctx, "cannot determine type of unary operation %s", asm_instr[op].m);
482 void ast_unary_delete(ast_unary *self)
484 ast_unref(self->operand);
485 ast_expression_delete((ast_expression*)self);
489 ast_return* ast_return_new(lex_ctx ctx, ast_expression *expr)
491 ast_instantiate(ast_return, ctx, ast_return_delete);
492 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_return_codegen);
494 self->operand = expr;
497 ast_propagate_effects(self, expr);
502 void ast_return_delete(ast_return *self)
505 ast_unref(self->operand);
506 ast_expression_delete((ast_expression*)self);
510 ast_entfield* ast_entfield_new(lex_ctx ctx, ast_expression *entity, ast_expression *field)
512 if (field->expression.vtype != TYPE_FIELD) {
513 asterror(ctx, "ast_entfield_new with expression not of type field");
516 return ast_entfield_new_force(ctx, entity, field, field->expression.next);
519 ast_entfield* ast_entfield_new_force(lex_ctx ctx, ast_expression *entity, ast_expression *field, const ast_expression *outtype)
521 ast_instantiate(ast_entfield, ctx, ast_entfield_delete);
525 /* Error: field has no type... */
529 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_entfield_codegen);
531 self->entity = entity;
533 ast_propagate_effects(self, entity);
534 ast_propagate_effects(self, field);
536 if (!ast_type_adopt(self, outtype)) {
537 ast_entfield_delete(self);
544 void ast_entfield_delete(ast_entfield *self)
546 ast_unref(self->entity);
547 ast_unref(self->field);
548 ast_expression_delete((ast_expression*)self);
552 ast_member* ast_member_new(lex_ctx ctx, ast_expression *owner, unsigned int field, const char *name)
554 ast_instantiate(ast_member, ctx, ast_member_delete);
560 if (owner->expression.vtype != TYPE_VECTOR &&
561 owner->expression.vtype != TYPE_FIELD) {
562 asterror(ctx, "member-access on an invalid owner of type %s", type_name[owner->expression.vtype]);
567 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_member_codegen);
568 self->expression.node.keep = true; /* keep */
570 if (owner->expression.vtype == TYPE_VECTOR) {
571 self->expression.vtype = TYPE_FLOAT;
572 self->expression.next = NULL;
574 self->expression.vtype = TYPE_FIELD;
575 self->expression.next = ast_shallow_type(ctx, TYPE_FLOAT);
579 ast_propagate_effects(self, owner);
583 self->name = util_strdup(name);
590 void ast_member_delete(ast_member *self)
592 /* The owner is always an ast_value, which has .keep=true,
593 * also: ast_members are usually deleted after the owner, thus
594 * this will cause invalid access
595 ast_unref(self->owner);
596 * once we allow (expression).x to access a vector-member, we need
597 * to change this: preferably by creating an alternate ast node for this
598 * purpose that is not garbage-collected.
600 ast_expression_delete((ast_expression*)self);
604 ast_array_index* ast_array_index_new(lex_ctx ctx, ast_expression *array, ast_expression *index)
606 ast_expression *outtype;
607 ast_instantiate(ast_array_index, ctx, ast_array_index_delete);
609 outtype = array->expression.next;
612 /* Error: field has no type... */
616 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_array_index_codegen);
620 ast_propagate_effects(self, array);
621 ast_propagate_effects(self, index);
623 if (!ast_type_adopt(self, outtype)) {
624 ast_array_index_delete(self);
627 if (array->expression.vtype == TYPE_FIELD && outtype->expression.vtype == TYPE_ARRAY) {
628 if (self->expression.vtype != TYPE_ARRAY) {
629 asterror(ast_ctx(self), "array_index node on type");
630 ast_array_index_delete(self);
633 self->array = outtype;
634 self->expression.vtype = TYPE_FIELD;
640 void ast_array_index_delete(ast_array_index *self)
642 ast_unref(self->array);
643 ast_unref(self->index);
644 ast_expression_delete((ast_expression*)self);
648 ast_ifthen* ast_ifthen_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
650 ast_instantiate(ast_ifthen, ctx, ast_ifthen_delete);
651 if (!ontrue && !onfalse) {
652 /* because it is invalid */
656 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ifthen_codegen);
659 self->on_true = ontrue;
660 self->on_false = onfalse;
661 ast_propagate_effects(self, cond);
663 ast_propagate_effects(self, ontrue);
665 ast_propagate_effects(self, onfalse);
670 void ast_ifthen_delete(ast_ifthen *self)
672 ast_unref(self->cond);
674 ast_unref(self->on_true);
676 ast_unref(self->on_false);
677 ast_expression_delete((ast_expression*)self);
681 ast_ternary* ast_ternary_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
683 ast_instantiate(ast_ternary, ctx, ast_ternary_delete);
684 /* This time NEITHER must be NULL */
685 if (!ontrue || !onfalse) {
689 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ternary_codegen);
692 self->on_true = ontrue;
693 self->on_false = onfalse;
694 ast_propagate_effects(self, cond);
695 ast_propagate_effects(self, ontrue);
696 ast_propagate_effects(self, onfalse);
698 if (!ast_type_adopt(self, ontrue)) {
699 ast_ternary_delete(self);
706 void ast_ternary_delete(ast_ternary *self)
708 ast_unref(self->cond);
709 ast_unref(self->on_true);
710 ast_unref(self->on_false);
711 ast_expression_delete((ast_expression*)self);
715 ast_loop* ast_loop_new(lex_ctx ctx,
716 ast_expression *initexpr,
717 ast_expression *precond,
718 ast_expression *postcond,
719 ast_expression *increment,
720 ast_expression *body)
722 ast_instantiate(ast_loop, ctx, ast_loop_delete);
723 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_loop_codegen);
725 self->initexpr = initexpr;
726 self->precond = precond;
727 self->postcond = postcond;
728 self->increment = increment;
732 ast_propagate_effects(self, initexpr);
734 ast_propagate_effects(self, precond);
736 ast_propagate_effects(self, postcond);
738 ast_propagate_effects(self, increment);
740 ast_propagate_effects(self, body);
745 void ast_loop_delete(ast_loop *self)
748 ast_unref(self->initexpr);
750 ast_unref(self->precond);
752 ast_unref(self->postcond);
754 ast_unref(self->increment);
756 ast_unref(self->body);
757 ast_expression_delete((ast_expression*)self);
761 ast_breakcont* ast_breakcont_new(lex_ctx ctx, bool iscont)
763 ast_instantiate(ast_breakcont, ctx, ast_breakcont_delete);
764 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_breakcont_codegen);
766 self->is_continue = iscont;
771 void ast_breakcont_delete(ast_breakcont *self)
773 ast_expression_delete((ast_expression*)self);
777 ast_switch* ast_switch_new(lex_ctx ctx, ast_expression *op)
779 ast_instantiate(ast_switch, ctx, ast_switch_delete);
780 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_switch_codegen);
785 ast_propagate_effects(self, op);
790 void ast_switch_delete(ast_switch *self)
793 ast_unref(self->operand);
795 for (i = 0; i < vec_size(self->cases); ++i) {
796 if (self->cases[i].value)
797 ast_unref(self->cases[i].value);
798 ast_unref(self->cases[i].code);
800 vec_free(self->cases);
802 ast_expression_delete((ast_expression*)self);
806 ast_label* ast_label_new(lex_ctx ctx, const char *name)
808 ast_instantiate(ast_label, ctx, ast_label_delete);
809 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_label_codegen);
811 self->name = util_strdup(name);
812 self->irblock = NULL;
818 void ast_label_delete(ast_label *self)
820 mem_d((void*)self->name);
821 vec_free(self->gotos);
822 ast_expression_delete((ast_expression*)self);
826 void ast_label_register_goto(ast_label *self, ast_goto *g)
828 vec_push(self->gotos, g);
831 ast_goto* ast_goto_new(lex_ctx ctx, const char *name)
833 ast_instantiate(ast_goto, ctx, ast_goto_delete);
834 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_goto_codegen);
836 self->name = util_strdup(name);
838 self->irblock_from = NULL;
843 void ast_goto_delete(ast_goto *self)
845 mem_d((void*)self->name);
846 ast_expression_delete((ast_expression*)self);
850 void ast_goto_set_label(ast_goto *self, ast_label *label)
852 self->target = label;
855 ast_call* ast_call_new(lex_ctx ctx,
856 ast_expression *funcexpr)
858 ast_instantiate(ast_call, ctx, ast_call_delete);
859 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_call_codegen);
861 ast_side_effects(self) = true;
864 self->func = funcexpr;
866 self->expression.vtype = funcexpr->expression.next->expression.vtype;
867 if (funcexpr->expression.next->expression.next)
868 self->expression.next = ast_type_copy(ctx, funcexpr->expression.next->expression.next);
873 void ast_call_delete(ast_call *self)
876 for (i = 0; i < vec_size(self->params); ++i)
877 ast_unref(self->params[i]);
878 vec_free(self->params);
881 ast_unref(self->func);
883 ast_expression_delete((ast_expression*)self);
887 bool ast_call_check_types(ast_call *self)
891 const ast_expression *func = self->func;
892 size_t count = vec_size(self->params);
893 if (count > vec_size(func->expression.params))
894 count = vec_size(func->expression.params);
896 for (i = 0; i < count; ++i) {
897 if (!ast_compare_type(self->params[i], (ast_expression*)(func->expression.params[i]))) {
900 ast_type_to_string(self->params[i], tgot, sizeof(tgot));
901 ast_type_to_string((ast_expression*)func->expression.params[i], texp, sizeof(texp));
902 asterror(ast_ctx(self), "invalid type for parameter %u in function call: expected %s, got %s",
903 (unsigned int)(i+1), texp, tgot);
904 /* we don't immediately return */
911 ast_store* ast_store_new(lex_ctx ctx, int op,
912 ast_expression *dest, ast_expression *source)
914 ast_instantiate(ast_store, ctx, ast_store_delete);
915 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_store_codegen);
917 ast_side_effects(self) = true;
921 self->source = source;
923 self->expression.vtype = dest->expression.vtype;
924 if (dest->expression.next) {
925 self->expression.next = ast_type_copy(ctx, dest);
926 if (!self->expression.next) {
932 self->expression.next = NULL;
937 void ast_store_delete(ast_store *self)
939 ast_unref(self->dest);
940 ast_unref(self->source);
941 ast_expression_delete((ast_expression*)self);
945 ast_block* ast_block_new(lex_ctx ctx)
947 ast_instantiate(ast_block, ctx, ast_block_delete);
948 ast_expression_init((ast_expression*)self,
949 (ast_expression_codegen*)&ast_block_codegen);
953 self->collect = NULL;
958 void ast_block_add_expr(ast_block *self, ast_expression *e)
960 ast_propagate_effects(self, e);
961 vec_push(self->exprs, e);
964 void ast_block_collect(ast_block *self, ast_expression *expr)
966 vec_push(self->collect, expr);
967 expr->expression.node.keep = true;
970 void ast_block_delete(ast_block *self)
973 for (i = 0; i < vec_size(self->exprs); ++i)
974 ast_unref(self->exprs[i]);
975 vec_free(self->exprs);
976 for (i = 0; i < vec_size(self->locals); ++i)
977 ast_delete(self->locals[i]);
978 vec_free(self->locals);
979 for (i = 0; i < vec_size(self->collect); ++i)
980 ast_delete(self->collect[i]);
981 vec_free(self->collect);
982 ast_expression_delete((ast_expression*)self);
986 bool ast_block_set_type(ast_block *self, ast_expression *from)
988 if (self->expression.next)
989 ast_delete(self->expression.next);
990 self->expression.vtype = from->expression.vtype;
991 if (from->expression.next) {
992 self->expression.next = ast_type_copy(self->expression.node.context, from->expression.next);
993 if (!self->expression.next)
997 self->expression.next = NULL;
1001 ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype)
1003 ast_instantiate(ast_function, ctx, ast_function_delete);
1007 vtype->expression.vtype != TYPE_FUNCTION)
1009 asterror(ast_ctx(self), "internal error: ast_function_new condition %i %i type=%i",
1011 (int)vtype->hasvalue,
1012 vtype->expression.vtype);
1017 self->vtype = vtype;
1018 self->name = name ? util_strdup(name) : NULL;
1019 self->blocks = NULL;
1021 self->labelcount = 0;
1024 self->ir_func = NULL;
1025 self->curblock = NULL;
1027 self->breakblock = NULL;
1028 self->continueblock = NULL;
1030 vtype->hasvalue = true;
1031 vtype->constval.vfunc = self;
1036 void ast_function_delete(ast_function *self)
1040 mem_d((void*)self->name);
1042 /* ast_value_delete(self->vtype); */
1043 self->vtype->hasvalue = false;
1044 self->vtype->constval.vfunc = NULL;
1045 /* We use unref - if it was stored in a global table it is supposed
1046 * to be deleted from *there*
1048 ast_unref(self->vtype);
1050 for (i = 0; i < vec_size(self->blocks); ++i)
1051 ast_delete(self->blocks[i]);
1052 vec_free(self->blocks);
1056 const char* ast_function_label(ast_function *self, const char *prefix)
1062 if (!opts_dump && !opts_dumpfin)
1065 id = (self->labelcount++);
1066 len = strlen(prefix);
1068 from = self->labelbuf + sizeof(self->labelbuf)-1;
1071 unsigned int digit = id % 10;
1072 *from = digit + '0';
1075 memcpy(from - len, prefix, len);
1079 /*********************************************************************/
1081 * by convention you must never pass NULL to the 'ir_value **out'
1082 * parameter. If you really don't care about the output, pass a dummy.
1083 * But I can't imagine a pituation where the output is truly unnecessary.
1086 bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out)
1090 /* NOTE: This is the codegen for a variable used in an expression.
1091 * It is not the codegen to generate the value. For this purpose,
1092 * ast_local_codegen and ast_global_codegen are to be used before this
1093 * is executed. ast_function_codegen should take care of its locals,
1094 * and the ast-user should take care of ast_global_codegen to be used
1095 * on all the globals.
1098 char typename[1024];
1099 ast_type_to_string((ast_expression*)self, typename, sizeof(typename));
1100 asterror(ast_ctx(self), "ast_value used before generated %s %s", typename, self->name);
1107 bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
1111 if (self->hasvalue && self->expression.vtype == TYPE_FUNCTION)
1113 ir_function *func = ir_builder_create_function(ir, self->name, self->expression.next->expression.vtype);
1116 func->context = ast_ctx(self);
1117 func->value->context = ast_ctx(self);
1119 self->constval.vfunc->ir_func = func;
1120 self->ir_v = func->value;
1121 /* The function is filled later on ast_function_codegen... */
1125 if (isfield && self->expression.vtype == TYPE_FIELD) {
1126 ast_expression *fieldtype = self->expression.next;
1128 if (self->hasvalue) {
1129 asterror(ast_ctx(self), "TODO: constant field pointers with value");
1133 if (fieldtype->expression.vtype == TYPE_ARRAY) {
1138 ast_expression_common *elemtype;
1140 ast_value *array = (ast_value*)fieldtype;
1142 if (!ast_istype(fieldtype, ast_value)) {
1143 asterror(ast_ctx(self), "internal error: ast_value required");
1147 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1148 if (!array->expression.count || array->expression.count > opts_max_array_size)
1149 asterror(ast_ctx(self), "Invalid array of size %lu", (unsigned long)array->expression.count);
1151 elemtype = &array->expression.next->expression;
1152 vtype = elemtype->vtype;
1154 v = ir_builder_create_field(ir, self->name, vtype);
1156 asterror(ast_ctx(self), "ir_builder_create_global failed on `%s`", self->name);
1159 if (vtype == TYPE_FIELD)
1160 v->fieldtype = elemtype->next->expression.vtype;
1161 v->context = ast_ctx(self);
1162 array->ir_v = self->ir_v = v;
1164 namelen = strlen(self->name);
1165 name = (char*)mem_a(namelen + 16);
1166 strcpy(name, self->name);
1168 array->ir_values = (ir_value**)mem_a(sizeof(array->ir_values[0]) * array->expression.count);
1169 array->ir_values[0] = v;
1170 for (ai = 1; ai < array->expression.count; ++ai) {
1171 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1172 array->ir_values[ai] = ir_builder_create_field(ir, name, vtype);
1173 if (!array->ir_values[ai]) {
1175 asterror(ast_ctx(self), "ir_builder_create_global failed on `%s`", name);
1178 if (vtype == TYPE_FIELD)
1179 array->ir_values[ai]->fieldtype = elemtype->next->expression.vtype;
1180 array->ir_values[ai]->context = ast_ctx(self);
1186 v = ir_builder_create_field(ir, self->name, self->expression.next->expression.vtype);
1189 v->context = ast_ctx(self);
1195 if (self->expression.vtype == TYPE_ARRAY) {
1200 ast_expression_common *elemtype = &self->expression.next->expression;
1201 int vtype = elemtype->vtype;
1203 /* same as with field arrays */
1204 if (!self->expression.count || self->expression.count > opts_max_array_size)
1205 asterror(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1207 v = ir_builder_create_global(ir, self->name, vtype);
1209 asterror(ast_ctx(self), "ir_builder_create_global failed `%s`", self->name);
1212 if (vtype == TYPE_FIELD)
1213 v->fieldtype = elemtype->next->expression.vtype;
1214 v->context = ast_ctx(self);
1216 namelen = strlen(self->name);
1217 name = (char*)mem_a(namelen + 16);
1218 strcpy(name, self->name);
1220 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1221 self->ir_values[0] = v;
1222 for (ai = 1; ai < self->expression.count; ++ai) {
1223 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1224 self->ir_values[ai] = ir_builder_create_global(ir, name, vtype);
1225 if (!self->ir_values[ai]) {
1227 asterror(ast_ctx(self), "ir_builder_create_global failed `%s`", name);
1230 if (vtype == TYPE_FIELD)
1231 self->ir_values[ai]->fieldtype = elemtype->next->expression.vtype;
1232 self->ir_values[ai]->context = ast_ctx(self);
1238 /* Arrays don't do this since there's no "array" value which spans across the
1241 v = ir_builder_create_global(ir, self->name, self->expression.vtype);
1243 asterror(ast_ctx(self), "ir_builder_create_global failed on `%s`", self->name);
1246 if (self->expression.vtype == TYPE_FIELD)
1247 v->fieldtype = self->expression.next->expression.vtype;
1248 v->context = ast_ctx(self);
1251 if (self->hasvalue) {
1252 switch (self->expression.vtype)
1255 if (!ir_value_set_float(v, self->constval.vfloat))
1259 if (!ir_value_set_vector(v, self->constval.vvec))
1263 if (!ir_value_set_string(v, self->constval.vstring))
1267 asterror(ast_ctx(self), "TODO: global constant array");
1270 asterror(ast_ctx(self), "global of type function not properly generated");
1272 /* Cannot generate an IR value for a function,
1273 * need a pointer pointing to a function rather.
1276 asterror(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1281 /* link us to the ir_value */
1285 error: /* clean up */
1290 bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
1293 if (self->hasvalue && self->expression.vtype == TYPE_FUNCTION)
1295 /* Do we allow local functions? I think not...
1296 * this is NOT a function pointer atm.
1301 if (self->expression.vtype == TYPE_ARRAY) {
1306 ast_expression_common *elemtype = &self->expression.next->expression;
1307 int vtype = elemtype->vtype;
1310 asterror(ast_ctx(self), "array-parameters are not supported");
1314 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1315 if (!self->expression.count || self->expression.count > opts_max_array_size) {
1316 asterror(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1319 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1320 if (!self->ir_values) {
1321 asterror(ast_ctx(self), "failed to allocate array values");
1325 v = ir_function_create_local(func, self->name, vtype, param);
1327 asterror(ast_ctx(self), "ir_function_create_local failed");
1330 if (vtype == TYPE_FIELD)
1331 v->fieldtype = elemtype->next->expression.vtype;
1332 v->context = ast_ctx(self);
1334 namelen = strlen(self->name);
1335 name = (char*)mem_a(namelen + 16);
1336 strcpy(name, self->name);
1338 self->ir_values[0] = v;
1339 for (ai = 1; ai < self->expression.count; ++ai) {
1340 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1341 self->ir_values[ai] = ir_function_create_local(func, name, vtype, param);
1342 if (!self->ir_values[ai]) {
1343 asterror(ast_ctx(self), "ir_builder_create_global failed on `%s`", name);
1346 if (vtype == TYPE_FIELD)
1347 self->ir_values[ai]->fieldtype = elemtype->next->expression.vtype;
1348 self->ir_values[ai]->context = ast_ctx(self);
1353 v = ir_function_create_local(func, self->name, self->expression.vtype, param);
1356 if (self->expression.vtype == TYPE_FIELD)
1357 v->fieldtype = self->expression.next->expression.vtype;
1358 v->context = ast_ctx(self);
1361 /* A constant local... hmmm...
1362 * I suppose the IR will have to deal with this
1364 if (self->hasvalue) {
1365 switch (self->expression.vtype)
1368 if (!ir_value_set_float(v, self->constval.vfloat))
1372 if (!ir_value_set_vector(v, self->constval.vvec))
1376 if (!ir_value_set_string(v, self->constval.vstring))
1380 asterror(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1385 /* link us to the ir_value */
1389 if (!ast_global_codegen(self->setter, func->owner, false) ||
1390 !ast_function_codegen(self->setter->constval.vfunc, func->owner) ||
1391 !ir_function_finalize(self->setter->constval.vfunc->ir_func))
1395 if (!ast_global_codegen(self->getter, func->owner, false) ||
1396 !ast_function_codegen(self->getter->constval.vfunc, func->owner) ||
1397 !ir_function_finalize(self->getter->constval.vfunc->ir_func))
1402 error: /* clean up */
1407 bool ast_function_codegen(ast_function *self, ir_builder *ir)
1411 ast_expression_common *ec;
1416 irf = self->ir_func;
1418 asterror(ast_ctx(self), "ast_function's related ast_value was not generated yet");
1422 /* fill the parameter list */
1423 ec = &self->vtype->expression;
1424 for (i = 0; i < vec_size(ec->params); ++i)
1426 vec_push(irf->params, ec->params[i]->expression.vtype);
1427 if (!self->builtin) {
1428 if (!ast_local_codegen(ec->params[i], self->ir_func, true))
1433 if (self->builtin) {
1434 irf->builtin = self->builtin;
1438 if (!vec_size(self->blocks)) {
1439 asterror(ast_ctx(self), "function `%s` has no body", self->name);
1443 self->curblock = ir_function_create_block(ast_ctx(self), irf, "entry");
1444 if (!self->curblock) {
1445 asterror(ast_ctx(self), "failed to allocate entry block for `%s`", self->name);
1449 for (i = 0; i < vec_size(self->blocks); ++i) {
1450 ast_expression_codegen *gen = self->blocks[i]->expression.codegen;
1451 if (!(*gen)((ast_expression*)self->blocks[i], self, false, &dummy))
1455 /* TODO: check return types */
1456 if (!self->curblock->is_return)
1458 return ir_block_create_return(self->curblock, NULL);
1459 /* From now on the parser has to handle this situation */
1461 if (!self->vtype->expression.next ||
1462 self->vtype->expression.next->expression.vtype == TYPE_VOID)
1464 return ir_block_create_return(self->curblock, NULL);
1468 /* error("missing return"); */
1469 asterror(ast_ctx(self), "function `%s` missing return value", self->name);
1477 /* Note, you will not see ast_block_codegen generate ir_blocks.
1478 * To the AST and the IR, blocks are 2 different things.
1479 * In the AST it represents a block of code, usually enclosed in
1480 * curly braces {...}.
1481 * While in the IR it represents a block in terms of control-flow.
1483 bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_value **out)
1487 /* We don't use this
1488 * Note: an ast-representation using the comma-operator
1489 * of the form: (a, b, c) = x should not assign to c...
1492 asterror(ast_ctx(self), "not an l-value (code-block)");
1496 if (self->expression.outr) {
1497 *out = self->expression.outr;
1501 /* output is NULL at first, we'll have each expression
1502 * assign to out output, thus, a comma-operator represention
1503 * using an ast_block will return the last generated value,
1504 * so: (b, c) + a executed both b and c, and returns c,
1505 * which is then added to a.
1509 /* generate locals */
1510 for (i = 0; i < vec_size(self->locals); ++i)
1512 if (!ast_local_codegen(self->locals[i], func->ir_func, false)) {
1514 asterror(ast_ctx(self), "failed to generate local `%s`", self->locals[i]->name);
1519 for (i = 0; i < vec_size(self->exprs); ++i)
1521 ast_expression_codegen *gen = self->exprs[i]->expression.codegen;
1522 if (func->curblock->final && !ast_istype(self->exprs[i], ast_label)) {
1523 asterror(ast_ctx(self->exprs[i]), "unreachable statement");
1526 if (!(*gen)(self->exprs[i], func, false, out))
1530 self->expression.outr = *out;
1535 bool ast_store_codegen(ast_store *self, ast_function *func, bool lvalue, ir_value **out)
1537 ast_expression_codegen *cgen;
1538 ir_value *left = NULL;
1539 ir_value *right = NULL;
1543 ast_array_index *ai = NULL;
1545 if (lvalue && self->expression.outl) {
1546 *out = self->expression.outl;
1550 if (!lvalue && self->expression.outr) {
1551 *out = self->expression.outr;
1555 if (ast_istype(self->dest, ast_array_index))
1558 ai = (ast_array_index*)self->dest;
1559 idx = (ast_value*)ai->index;
1561 if (ast_istype(ai->index, ast_value) && idx->hasvalue)
1566 /* we need to call the setter */
1567 ir_value *iridx, *funval;
1571 asterror(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1575 arr = (ast_value*)ai->array;
1576 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1577 asterror(ast_ctx(self), "value has no setter (%s)", arr->name);
1581 cgen = idx->expression.codegen;
1582 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1585 cgen = arr->setter->expression.codegen;
1586 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
1589 cgen = self->source->expression.codegen;
1590 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1593 call = ir_block_create_call(func->curblock, ast_function_label(func, "store"), funval);
1596 ir_call_param(call, iridx);
1597 ir_call_param(call, right);
1598 self->expression.outr = right;
1604 cgen = self->dest->expression.codegen;
1606 if (!(*cgen)((ast_expression*)(self->dest), func, true, &left))
1608 self->expression.outl = left;
1610 cgen = self->source->expression.codegen;
1612 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1615 if (!ir_block_create_store_op(func->curblock, self->op, left, right))
1617 self->expression.outr = right;
1620 /* Theoretically, an assinment returns its left side as an
1621 * lvalue, if we don't need an lvalue though, we return
1622 * the right side as an rvalue, otherwise we have to
1623 * somehow know whether or not we need to dereference the pointer
1624 * on the left side - that is: OP_LOAD if it was an address.
1625 * Also: in original QC we cannot OP_LOADP *anyway*.
1627 *out = (lvalue ? left : right);
1632 bool ast_binary_codegen(ast_binary *self, ast_function *func, bool lvalue, ir_value **out)
1634 ast_expression_codegen *cgen;
1635 ir_value *left, *right;
1637 /* A binary operation cannot yield an l-value */
1639 asterror(ast_ctx(self), "not an l-value (binop)");
1643 if (self->expression.outr) {
1644 *out = self->expression.outr;
1648 if (OPTS_FLAG(SHORT_LOGIC) &&
1649 (self->op == INSTR_AND || self->op == INSTR_OR))
1651 /* short circuit evaluation */
1652 ir_block *other, *merge;
1653 ir_block *from_left, *from_right;
1658 /* Note about casting to true boolean values:
1659 * We use a single NOT for sub expressions, and an
1660 * overall NOT at the end, and for that purpose swap
1661 * all the jump conditions in order for the NOT to get
1663 * ie: (a && b) usually becomes (!!a ? !!b : !!a)
1664 * but we translate this to (!(!a ? !a : !b))
1667 merge_id = vec_size(func->ir_func->blocks);
1668 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "sce_merge"));
1670 cgen = self->left->expression.codegen;
1671 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1673 if (!OPTS_FLAG(PERL_LOGIC)) {
1674 notop = type_not_instr[left->vtype];
1675 if (notop == AINSTR_END) {
1676 asterror(ast_ctx(self), "don't know how to cast to bool...");
1679 left = ir_block_create_unary(func->curblock,
1680 ast_function_label(func, "sce_not"),
1684 from_left = func->curblock;
1686 other = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "sce_other"));
1687 if ( !(self->op == INSTR_OR) != !OPTS_FLAG(PERL_LOGIC) ) {
1688 if (!ir_block_create_if(func->curblock, left, other, merge))
1691 if (!ir_block_create_if(func->curblock, left, merge, other))
1694 /* use the likely flag */
1695 vec_last(func->curblock->instr)->likely = true;
1697 func->curblock = other;
1698 cgen = self->right->expression.codegen;
1699 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1701 if (!OPTS_FLAG(PERL_LOGIC)) {
1702 notop = type_not_instr[right->vtype];
1703 if (notop == AINSTR_END) {
1704 asterror(ast_ctx(self), "don't know how to cast to bool...");
1707 right = ir_block_create_unary(func->curblock,
1708 ast_function_label(func, "sce_not"),
1712 from_right = func->curblock;
1714 if (!ir_block_create_jump(func->curblock, merge))
1717 vec_remove(func->ir_func->blocks, merge_id, 1);
1718 vec_push(func->ir_func->blocks, merge);
1720 func->curblock = merge;
1721 phi = ir_block_create_phi(func->curblock, ast_function_label(func, "sce_value"), TYPE_FLOAT);
1722 ir_phi_add(phi, from_left, left);
1723 ir_phi_add(phi, from_right, right);
1724 *out = ir_phi_value(phi);
1725 if (!OPTS_FLAG(PERL_LOGIC)) {
1726 notop = type_not_instr[(*out)->vtype];
1727 if (notop == AINSTR_END) {
1728 asterror(ast_ctx(self), "don't know how to cast to bool...");
1731 *out = ir_block_create_unary(func->curblock,
1732 ast_function_label(func, "sce_final_not"),
1738 self->expression.outr = *out;
1742 cgen = self->left->expression.codegen;
1743 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1746 cgen = self->right->expression.codegen;
1747 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1750 *out = ir_block_create_binop(func->curblock, ast_function_label(func, "bin"),
1751 self->op, left, right);
1754 self->expression.outr = *out;
1759 bool ast_binstore_codegen(ast_binstore *self, ast_function *func, bool lvalue, ir_value **out)
1761 ast_expression_codegen *cgen;
1762 ir_value *leftl = NULL, *leftr, *right, *bin;
1766 ast_array_index *ai = NULL;
1767 ir_value *iridx = NULL;
1769 if (lvalue && self->expression.outl) {
1770 *out = self->expression.outl;
1774 if (!lvalue && self->expression.outr) {
1775 *out = self->expression.outr;
1779 if (ast_istype(self->dest, ast_array_index))
1782 ai = (ast_array_index*)self->dest;
1783 idx = (ast_value*)ai->index;
1785 if (ast_istype(ai->index, ast_value) && idx->hasvalue)
1789 /* for a binstore we need both an lvalue and an rvalue for the left side */
1790 /* rvalue of destination! */
1792 cgen = idx->expression.codegen;
1793 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1796 cgen = self->dest->expression.codegen;
1797 if (!(*cgen)((ast_expression*)(self->dest), func, false, &leftr))
1800 /* source as rvalue only */
1801 cgen = self->source->expression.codegen;
1802 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1805 /* now the binary */
1806 bin = ir_block_create_binop(func->curblock, ast_function_label(func, "binst"),
1807 self->opbin, leftr, right);
1808 self->expression.outr = bin;
1812 /* we need to call the setter */
1817 asterror(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1821 arr = (ast_value*)ai->array;
1822 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1823 asterror(ast_ctx(self), "value has no setter (%s)", arr->name);
1827 cgen = arr->setter->expression.codegen;
1828 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
1831 call = ir_block_create_call(func->curblock, ast_function_label(func, "store"), funval);
1834 ir_call_param(call, iridx);
1835 ir_call_param(call, bin);
1836 self->expression.outr = bin;
1838 /* now store them */
1839 cgen = self->dest->expression.codegen;
1840 /* lvalue of destination */
1841 if (!(*cgen)((ast_expression*)(self->dest), func, true, &leftl))
1843 self->expression.outl = leftl;
1845 if (!ir_block_create_store_op(func->curblock, self->opstore, leftl, bin))
1847 self->expression.outr = bin;
1850 /* Theoretically, an assinment returns its left side as an
1851 * lvalue, if we don't need an lvalue though, we return
1852 * the right side as an rvalue, otherwise we have to
1853 * somehow know whether or not we need to dereference the pointer
1854 * on the left side - that is: OP_LOAD if it was an address.
1855 * Also: in original QC we cannot OP_LOADP *anyway*.
1857 *out = (lvalue ? leftl : bin);
1862 bool ast_unary_codegen(ast_unary *self, ast_function *func, bool lvalue, ir_value **out)
1864 ast_expression_codegen *cgen;
1867 /* An unary operation cannot yield an l-value */
1869 asterror(ast_ctx(self), "not an l-value (binop)");
1873 if (self->expression.outr) {
1874 *out = self->expression.outr;
1878 cgen = self->operand->expression.codegen;
1880 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
1883 *out = ir_block_create_unary(func->curblock, ast_function_label(func, "unary"),
1887 self->expression.outr = *out;
1892 bool ast_return_codegen(ast_return *self, ast_function *func, bool lvalue, ir_value **out)
1894 ast_expression_codegen *cgen;
1899 /* In the context of a return operation, we don't actually return
1903 asterror(ast_ctx(self), "return-expression is not an l-value");
1907 if (self->expression.outr) {
1908 asterror(ast_ctx(self), "internal error: ast_return cannot be reused, it bears no result!");
1911 self->expression.outr = (ir_value*)1;
1913 if (self->operand) {
1914 cgen = self->operand->expression.codegen;
1916 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
1919 if (!ir_block_create_return(func->curblock, operand))
1922 if (!ir_block_create_return(func->curblock, NULL))
1929 bool ast_entfield_codegen(ast_entfield *self, ast_function *func, bool lvalue, ir_value **out)
1931 ast_expression_codegen *cgen;
1932 ir_value *ent, *field;
1934 /* This function needs to take the 'lvalue' flag into account!
1935 * As lvalue we provide a field-pointer, as rvalue we provide the
1939 if (lvalue && self->expression.outl) {
1940 *out = self->expression.outl;
1944 if (!lvalue && self->expression.outr) {
1945 *out = self->expression.outr;
1949 cgen = self->entity->expression.codegen;
1950 if (!(*cgen)((ast_expression*)(self->entity), func, false, &ent))
1953 cgen = self->field->expression.codegen;
1954 if (!(*cgen)((ast_expression*)(self->field), func, false, &field))
1959 *out = ir_block_create_fieldaddress(func->curblock, ast_function_label(func, "efa"),
1962 *out = ir_block_create_load_from_ent(func->curblock, ast_function_label(func, "efv"),
1963 ent, field, self->expression.vtype);
1966 asterror(ast_ctx(self), "failed to create %s instruction (output type %s)",
1967 (lvalue ? "ADDRESS" : "FIELD"),
1968 type_name[self->expression.vtype]);
1973 self->expression.outl = *out;
1975 self->expression.outr = *out;
1977 /* Hm that should be it... */
1981 bool ast_member_codegen(ast_member *self, ast_function *func, bool lvalue, ir_value **out)
1983 ast_expression_codegen *cgen;
1986 /* in QC this is always an lvalue */
1988 if (self->expression.outl) {
1989 *out = self->expression.outl;
1993 cgen = self->owner->expression.codegen;
1994 if (!(*cgen)((ast_expression*)(self->owner), func, true, &vec))
1997 if (vec->vtype != TYPE_VECTOR &&
1998 !(vec->vtype == TYPE_FIELD && self->owner->expression.next->expression.vtype == TYPE_VECTOR))
2003 *out = ir_value_vector_member(vec, self->field);
2004 self->expression.outl = *out;
2006 return (*out != NULL);
2009 bool ast_array_index_codegen(ast_array_index *self, ast_function *func, bool lvalue, ir_value **out)
2014 if (!lvalue && self->expression.outr) {
2015 *out = self->expression.outr;
2017 if (lvalue && self->expression.outl) {
2018 *out = self->expression.outl;
2021 if (!ast_istype(self->array, ast_value)) {
2022 asterror(ast_ctx(self), "array indexing this way is not supported");
2023 /* note this would actually be pointer indexing because the left side is
2024 * not an actual array but (hopefully) an indexable expression.
2025 * Once we get integer arithmetic, and GADDRESS/GSTORE/GLOAD instruction
2026 * support this path will be filled.
2031 arr = (ast_value*)self->array;
2032 idx = (ast_value*)self->index;
2034 if (!ast_istype(self->index, ast_value) || !idx->hasvalue) {
2035 /* Time to use accessor functions */
2036 ast_expression_codegen *cgen;
2037 ir_value *iridx, *funval;
2041 asterror(ast_ctx(self), "(.2) array indexing here needs a compile-time constant");
2046 asterror(ast_ctx(self), "value has no getter, don't know how to index it");
2050 cgen = self->index->expression.codegen;
2051 if (!(*cgen)((ast_expression*)(self->index), func, false, &iridx))
2054 cgen = arr->getter->expression.codegen;
2055 if (!(*cgen)((ast_expression*)(arr->getter), func, true, &funval))
2058 call = ir_block_create_call(func->curblock, ast_function_label(func, "fetch"), funval);
2061 ir_call_param(call, iridx);
2063 *out = ir_call_value(call);
2064 self->expression.outr = *out;
2068 if (idx->expression.vtype == TYPE_FLOAT)
2069 *out = arr->ir_values[(int)idx->constval.vfloat];
2070 else if (idx->expression.vtype == TYPE_INTEGER)
2071 *out = arr->ir_values[idx->constval.vint];
2073 asterror(ast_ctx(self), "array indexing here needs an integer constant");
2079 bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_value **out)
2081 ast_expression_codegen *cgen;
2086 ir_block *cond = func->curblock;
2089 ir_block *ontrue_endblock = NULL;
2090 ir_block *onfalse_endblock = NULL;
2093 /* We don't output any value, thus also don't care about r/lvalue */
2097 if (self->expression.outr) {
2098 asterror(ast_ctx(self), "internal error: ast_ifthen cannot be reused, it bears no result!");
2101 self->expression.outr = (ir_value*)1;
2103 /* generate the condition */
2104 cgen = self->cond->expression.codegen;
2105 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2107 /* update the block which will get the jump - because short-logic or ternaries may have changed this */
2108 cond = func->curblock;
2112 if (self->on_true) {
2113 /* create on-true block */
2114 ontrue = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "ontrue"));
2118 /* enter the block */
2119 func->curblock = ontrue;
2122 cgen = self->on_true->expression.codegen;
2123 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &dummy))
2126 /* we now need to work from the current endpoint */
2127 ontrue_endblock = func->curblock;
2132 if (self->on_false) {
2133 /* create on-false block */
2134 onfalse = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "onfalse"));
2138 /* enter the block */
2139 func->curblock = onfalse;
2142 cgen = self->on_false->expression.codegen;
2143 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &dummy))
2146 /* we now need to work from the current endpoint */
2147 onfalse_endblock = func->curblock;
2151 /* Merge block were they all merge in to */
2152 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "endif"));
2155 /* add jumps ot the merge block */
2156 if (ontrue && !ontrue_endblock->final && !ir_block_create_jump(ontrue_endblock, merge))
2158 if (onfalse && !onfalse_endblock->final && !ir_block_create_jump(onfalse_endblock, merge))
2161 /* we create the if here, that way all blocks are ordered :)
2163 if (!ir_block_create_if(cond, condval,
2164 (ontrue ? ontrue : merge),
2165 (onfalse ? onfalse : merge)))
2170 /* Now enter the merge block */
2171 func->curblock = merge;
2176 bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_value **out)
2178 ast_expression_codegen *cgen;
2181 ir_value *trueval, *falseval;
2184 ir_block *cond = func->curblock;
2185 ir_block *cond_out = NULL;
2186 ir_block *ontrue, *ontrue_out = NULL;
2187 ir_block *onfalse, *onfalse_out = NULL;
2190 /* Ternary can never create an lvalue... */
2194 /* In theory it shouldn't be possible to pass through a node twice, but
2195 * in case we add any kind of optimization pass for the AST itself, it
2196 * may still happen, thus we remember a created ir_value and simply return one
2197 * if it already exists.
2199 if (self->expression.outr) {
2200 *out = self->expression.outr;
2204 /* In the following, contraty to ast_ifthen, we assume both paths exist. */
2206 /* generate the condition */
2207 func->curblock = cond;
2208 cgen = self->cond->expression.codegen;
2209 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2211 cond_out = func->curblock;
2213 /* create on-true block */
2214 ontrue = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_T"));
2219 /* enter the block */
2220 func->curblock = ontrue;
2223 cgen = self->on_true->expression.codegen;
2224 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &trueval))
2227 ontrue_out = func->curblock;
2230 /* create on-false block */
2231 onfalse = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_F"));
2236 /* enter the block */
2237 func->curblock = onfalse;
2240 cgen = self->on_false->expression.codegen;
2241 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &falseval))
2244 onfalse_out = func->curblock;
2247 /* create merge block */
2248 merge = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "tern_out"));
2251 /* jump to merge block */
2252 if (!ir_block_create_jump(ontrue_out, merge))
2254 if (!ir_block_create_jump(onfalse_out, merge))
2257 /* create if instruction */
2258 if (!ir_block_create_if(cond_out, condval, ontrue, onfalse))
2261 /* Now enter the merge block */
2262 func->curblock = merge;
2264 /* Here, now, we need a PHI node
2265 * but first some sanity checking...
2267 if (trueval->vtype != falseval->vtype) {
2268 /* error("ternary with different types on the two sides"); */
2273 phi = ir_block_create_phi(merge, ast_function_label(func, "phi"), trueval->vtype);
2276 ir_phi_add(phi, ontrue_out, trueval);
2277 ir_phi_add(phi, onfalse_out, falseval);
2279 self->expression.outr = ir_phi_value(phi);
2280 *out = self->expression.outr;
2285 bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value **out)
2287 ast_expression_codegen *cgen;
2289 ir_value *dummy = NULL;
2290 ir_value *precond = NULL;
2291 ir_value *postcond = NULL;
2293 /* Since we insert some jumps "late" so we have blocks
2294 * ordered "nicely", we need to keep track of the actual end-blocks
2295 * of expressions to add the jumps to.
2297 ir_block *bbody = NULL, *end_bbody = NULL;
2298 ir_block *bprecond = NULL, *end_bprecond = NULL;
2299 ir_block *bpostcond = NULL, *end_bpostcond = NULL;
2300 ir_block *bincrement = NULL, *end_bincrement = NULL;
2301 ir_block *bout = NULL, *bin = NULL;
2303 /* let's at least move the outgoing block to the end */
2306 /* 'break' and 'continue' need to be able to find the right blocks */
2307 ir_block *bcontinue = NULL;
2308 ir_block *bbreak = NULL;
2310 ir_block *old_bcontinue = NULL;
2311 ir_block *old_bbreak = NULL;
2313 ir_block *tmpblock = NULL;
2318 if (self->expression.outr) {
2319 asterror(ast_ctx(self), "internal error: ast_loop cannot be reused, it bears no result!");
2322 self->expression.outr = (ir_value*)1;
2325 * Should we ever need some kind of block ordering, better make this function
2326 * move blocks around than write a block ordering algorithm later... after all
2327 * the ast and ir should work together, not against each other.
2330 /* initexpr doesn't get its own block, it's pointless, it could create more blocks
2331 * anyway if for example it contains a ternary.
2335 cgen = self->initexpr->expression.codegen;
2336 if (!(*cgen)((ast_expression*)(self->initexpr), func, false, &dummy))
2340 /* Store the block from which we enter this chaos */
2341 bin = func->curblock;
2343 /* The pre-loop condition needs its own block since we
2344 * need to be able to jump to the start of that expression.
2348 bprecond = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "pre_loop_cond"));
2352 /* the pre-loop-condition the least important place to 'continue' at */
2353 bcontinue = bprecond;
2356 func->curblock = bprecond;
2359 cgen = self->precond->expression.codegen;
2360 if (!(*cgen)((ast_expression*)(self->precond), func, false, &precond))
2363 end_bprecond = func->curblock;
2365 bprecond = end_bprecond = NULL;
2368 /* Now the next blocks won't be ordered nicely, but we need to
2369 * generate them this early for 'break' and 'continue'.
2371 if (self->increment) {
2372 bincrement = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "loop_increment"));
2375 bcontinue = bincrement; /* increment comes before the pre-loop-condition */
2377 bincrement = end_bincrement = NULL;
2380 if (self->postcond) {
2381 bpostcond = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "post_loop_cond"));
2384 bcontinue = bpostcond; /* postcond comes before the increment */
2386 bpostcond = end_bpostcond = NULL;
2389 bout_id = vec_size(func->ir_func->blocks);
2390 bout = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "after_loop"));
2395 /* The loop body... */
2398 bbody = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "loop_body"));
2403 func->curblock = bbody;
2405 old_bbreak = func->breakblock;
2406 old_bcontinue = func->continueblock;
2407 func->breakblock = bbreak;
2408 func->continueblock = bcontinue;
2409 if (!func->continueblock)
2410 func->continueblock = bbody;
2413 cgen = self->body->expression.codegen;
2414 if (!(*cgen)((ast_expression*)(self->body), func, false, &dummy))
2417 end_bbody = func->curblock;
2418 func->breakblock = old_bbreak;
2419 func->continueblock = old_bcontinue;
2422 /* post-loop-condition */
2426 func->curblock = bpostcond;
2429 cgen = self->postcond->expression.codegen;
2430 if (!(*cgen)((ast_expression*)(self->postcond), func, false, &postcond))
2433 end_bpostcond = func->curblock;
2436 /* The incrementor */
2437 if (self->increment)
2440 func->curblock = bincrement;
2443 cgen = self->increment->expression.codegen;
2444 if (!(*cgen)((ast_expression*)(self->increment), func, false, &dummy))
2447 end_bincrement = func->curblock;
2450 /* In any case now, we continue from the outgoing block */
2451 func->curblock = bout;
2453 /* Now all blocks are in place */
2454 /* From 'bin' we jump to whatever comes first */
2455 if (bprecond) tmpblock = bprecond;
2456 else if (bbody) tmpblock = bbody;
2457 else if (bpostcond) tmpblock = bpostcond;
2458 else tmpblock = bout;
2459 if (!ir_block_create_jump(bin, tmpblock))
2465 ir_block *ontrue, *onfalse;
2466 if (bbody) ontrue = bbody;
2467 else if (bincrement) ontrue = bincrement;
2468 else if (bpostcond) ontrue = bpostcond;
2469 else ontrue = bprecond;
2471 if (!ir_block_create_if(end_bprecond, precond, ontrue, onfalse))
2478 if (bincrement) tmpblock = bincrement;
2479 else if (bpostcond) tmpblock = bpostcond;
2480 else if (bprecond) tmpblock = bprecond;
2481 else tmpblock = bout;
2482 if (!end_bbody->final && !ir_block_create_jump(end_bbody, tmpblock))
2486 /* from increment */
2489 if (bpostcond) tmpblock = bpostcond;
2490 else if (bprecond) tmpblock = bprecond;
2491 else if (bbody) tmpblock = bbody;
2492 else tmpblock = bout;
2493 if (!ir_block_create_jump(end_bincrement, tmpblock))
2500 ir_block *ontrue, *onfalse;
2501 if (bprecond) ontrue = bprecond;
2502 else if (bbody) ontrue = bbody;
2503 else if (bincrement) ontrue = bincrement;
2504 else ontrue = bpostcond;
2506 if (!ir_block_create_if(end_bpostcond, postcond, ontrue, onfalse))
2510 /* Move 'bout' to the end */
2511 vec_remove(func->ir_func->blocks, bout_id, 1);
2512 vec_push(func->ir_func->blocks, bout);
2517 bool ast_breakcont_codegen(ast_breakcont *self, ast_function *func, bool lvalue, ir_value **out)
2524 asterror(ast_ctx(self), "break/continue expression is not an l-value");
2528 if (self->expression.outr) {
2529 asterror(ast_ctx(self), "internal error: ast_breakcont cannot be reused!");
2532 self->expression.outr = (ir_value*)1;
2534 if (self->is_continue)
2535 target = func->continueblock;
2537 target = func->breakblock;
2540 asterror(ast_ctx(self), "%s is lacking a target block", (self->is_continue ? "continue" : "break"));
2544 if (!ir_block_create_jump(func->curblock, target))
2549 bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_value **out)
2551 ast_expression_codegen *cgen;
2553 ast_switch_case *def_case = NULL;
2554 ir_block *def_bfall = NULL;
2556 ir_value *dummy = NULL;
2557 ir_value *irop = NULL;
2558 ir_block *old_break = NULL;
2559 ir_block *bout = NULL;
2560 ir_block *bfall = NULL;
2568 asterror(ast_ctx(self), "switch expression is not an l-value");
2572 if (self->expression.outr) {
2573 asterror(ast_ctx(self), "internal error: ast_switch cannot be reused!");
2576 self->expression.outr = (ir_value*)1;
2581 cgen = self->operand->expression.codegen;
2582 if (!(*cgen)((ast_expression*)(self->operand), func, false, &irop))
2585 if (!vec_size(self->cases))
2588 cmpinstr = type_eq_instr[irop->vtype];
2589 if (cmpinstr >= AINSTR_END) {
2590 ast_type_to_string(self->operand, typestr, sizeof(typestr));
2591 asterror(ast_ctx(self), "invalid type to perform a switch on: %s", typestr);
2595 bout_id = vec_size(func->ir_func->blocks);
2596 bout = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "after_switch"));
2600 /* setup the break block */
2601 old_break = func->breakblock;
2602 func->breakblock = bout;
2604 /* Now create all cases */
2605 for (c = 0; c < vec_size(self->cases); ++c) {
2606 ir_value *cond, *val;
2607 ir_block *bcase, *bnot;
2610 ast_switch_case *swcase = &self->cases[c];
2612 if (swcase->value) {
2613 /* A regular case */
2614 /* generate the condition operand */
2615 cgen = swcase->value->expression.codegen;
2616 if (!(*cgen)((ast_expression*)(swcase->value), func, false, &val))
2618 /* generate the condition */
2619 cond = ir_block_create_binop(func->curblock, ast_function_label(func, "switch_eq"), cmpinstr, irop, val);
2623 bcase = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "case"));
2624 bnot_id = vec_size(func->ir_func->blocks);
2625 bnot = ir_function_create_block(ast_ctx(self), func->ir_func, ast_function_label(func, "not_case"));
2626 if (!bcase || !bnot)
2628 if (!ir_block_create_if(func->curblock, cond, bcase, bnot))
2631 /* Make the previous case-end fall through */
2632 if (bfall && !bfall->final) {
2633 if (!ir_block_create_jump(bfall, bcase))
2637 /* enter the case */
2638 func->curblock = bcase;
2639 cgen = swcase->code->expression.codegen;
2640 if (!(*cgen)((ast_expression*)swcase->code, func, false, &dummy))
2643 /* remember this block to fall through from */
2644 bfall = func->curblock;
2646 /* enter the else and move it down */
2647 func->curblock = bnot;
2648 vec_remove(func->ir_func->blocks, bnot_id, 1);
2649 vec_push(func->ir_func->blocks, bnot);
2651 /* The default case */
2652 /* Remember where to fall through from: */
2655 /* remember which case it was */
2660 /* Jump from the last bnot to bout */
2661 if (bfall && !bfall->final && !ir_block_create_jump(bfall, bout)) {
2663 astwarning(ast_ctx(bfall), WARN_???, "missing break after last case");
2668 /* If there was a default case, put it down here */
2672 /* No need to create an extra block */
2673 bcase = func->curblock;
2675 /* Insert the fallthrough jump */
2676 if (def_bfall && !def_bfall->final) {
2677 if (!ir_block_create_jump(def_bfall, bcase))
2681 /* Now generate the default code */
2682 cgen = def_case->code->expression.codegen;
2683 if (!(*cgen)((ast_expression*)def_case->code, func, false, &dummy))
2687 /* Jump from the last bnot to bout */
2688 if (!func->curblock->final && !ir_block_create_jump(func->curblock, bout))
2690 /* enter the outgoing block */
2691 func->curblock = bout;
2693 /* restore the break block */
2694 func->breakblock = old_break;
2696 /* Move 'bout' to the end, it's nicer */
2697 vec_remove(func->ir_func->blocks, bout_id, 1);
2698 vec_push(func->ir_func->blocks, bout);
2703 bool ast_label_codegen(ast_label *self, ast_function *func, bool lvalue, ir_value **out)
2710 asterror(ast_ctx(self), "internal error: ast_label cannot be an lvalue");
2714 /* simply create a new block and jump to it */
2715 self->irblock = ir_function_create_block(ast_ctx(self), func->ir_func, self->name);
2716 if (!self->irblock) {
2717 asterror(ast_ctx(self), "failed to allocate label block `%s`", self->name);
2720 if (!func->curblock->final) {
2721 if (!ir_block_create_jump(func->curblock, self->irblock))
2725 /* enter the new block */
2726 func->curblock = self->irblock;
2728 /* Generate all the leftover gotos */
2729 for (i = 0; i < vec_size(self->gotos); ++i) {
2730 if (!ast_goto_codegen(self->gotos[i], func, false, &dummy))
2737 bool ast_goto_codegen(ast_goto *self, ast_function *func, bool lvalue, ir_value **out)
2741 asterror(ast_ctx(self), "internal error: ast_goto cannot be an lvalue");
2745 if (self->target->irblock) {
2746 if (self->irblock_from) {
2747 /* we already tried once, this is the callback */
2748 self->irblock_from->final = false;
2749 if (!ir_block_create_jump(self->irblock_from, self->target->irblock)) {
2750 asterror(ast_ctx(self), "failed to generate goto to `%s`", self->name);
2756 if (!ir_block_create_jump(func->curblock, self->target->irblock)) {
2757 asterror(ast_ctx(self), "failed to generate goto to `%s`", self->name);
2764 /* the target has not yet been created...
2765 * close this block in a sneaky way:
2767 func->curblock->final = true;
2768 self->irblock_from = func->curblock;
2769 ast_label_register_goto(self->target, self);
2775 bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value **out)
2777 ast_expression_codegen *cgen;
2779 ir_instr *callinstr;
2782 ir_value *funval = NULL;
2784 /* return values are never lvalues */
2786 asterror(ast_ctx(self), "not an l-value (function call)");
2790 if (self->expression.outr) {
2791 *out = self->expression.outr;
2795 cgen = self->func->expression.codegen;
2796 if (!(*cgen)((ast_expression*)(self->func), func, false, &funval))
2804 for (i = 0; i < vec_size(self->params); ++i)
2807 ast_expression *expr = self->params[i];
2809 cgen = expr->expression.codegen;
2810 if (!(*cgen)(expr, func, false, ¶m))
2814 vec_push(params, param);
2817 callinstr = ir_block_create_call(func->curblock, ast_function_label(func, "call"), funval);
2821 for (i = 0; i < vec_size(params); ++i) {
2822 ir_call_param(callinstr, params[i]);
2825 *out = ir_call_value(callinstr);
2826 self->expression.outr = *out;