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->expression.vtype = left->expression.vtype;
439 if (left->expression.next) {
440 self->expression.next = ast_type_copy(ctx, left);
441 if (!self->expression.next) {
447 self->expression.next = NULL;
452 void ast_binstore_delete(ast_binstore *self)
454 ast_unref(self->dest);
455 ast_unref(self->source);
456 ast_expression_delete((ast_expression*)self);
460 ast_unary* ast_unary_new(lex_ctx ctx, int op,
461 ast_expression *expr)
463 ast_instantiate(ast_unary, ctx, ast_unary_delete);
464 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_unary_codegen);
467 self->operand = expr;
469 ast_propagate_effects(self, expr);
471 if (op >= INSTR_NOT_F && op <= INSTR_NOT_FNC) {
472 self->expression.vtype = TYPE_FLOAT;
474 asterror(ctx, "cannot determine type of unary operation %s", asm_instr[op].m);
479 void ast_unary_delete(ast_unary *self)
481 ast_unref(self->operand);
482 ast_expression_delete((ast_expression*)self);
486 ast_return* ast_return_new(lex_ctx ctx, ast_expression *expr)
488 ast_instantiate(ast_return, ctx, ast_return_delete);
489 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_return_codegen);
491 self->operand = expr;
494 ast_propagate_effects(self, expr);
499 void ast_return_delete(ast_return *self)
502 ast_unref(self->operand);
503 ast_expression_delete((ast_expression*)self);
507 ast_entfield* ast_entfield_new(lex_ctx ctx, ast_expression *entity, ast_expression *field)
509 if (field->expression.vtype != TYPE_FIELD) {
510 asterror(ctx, "ast_entfield_new with expression not of type field");
513 return ast_entfield_new_force(ctx, entity, field, field->expression.next);
516 ast_entfield* ast_entfield_new_force(lex_ctx ctx, ast_expression *entity, ast_expression *field, const ast_expression *outtype)
518 ast_instantiate(ast_entfield, ctx, ast_entfield_delete);
522 /* Error: field has no type... */
526 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_entfield_codegen);
528 self->entity = entity;
530 ast_propagate_effects(self, entity);
531 ast_propagate_effects(self, field);
533 if (!ast_type_adopt(self, outtype)) {
534 ast_entfield_delete(self);
541 void ast_entfield_delete(ast_entfield *self)
543 ast_unref(self->entity);
544 ast_unref(self->field);
545 ast_expression_delete((ast_expression*)self);
549 ast_member* ast_member_new(lex_ctx ctx, ast_expression *owner, unsigned int field, const char *name)
551 ast_instantiate(ast_member, ctx, ast_member_delete);
557 if (owner->expression.vtype != TYPE_VECTOR &&
558 owner->expression.vtype != TYPE_FIELD) {
559 asterror(ctx, "member-access on an invalid owner of type %s", type_name[owner->expression.vtype]);
564 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_member_codegen);
565 self->expression.node.keep = true; /* keep */
567 if (owner->expression.vtype == TYPE_VECTOR) {
568 self->expression.vtype = TYPE_FLOAT;
569 self->expression.next = NULL;
571 self->expression.vtype = TYPE_FIELD;
572 self->expression.next = ast_shallow_type(ctx, TYPE_FLOAT);
576 ast_propagate_effects(self, owner);
580 self->name = util_strdup(name);
587 void ast_member_delete(ast_member *self)
589 /* The owner is always an ast_value, which has .keep=true,
590 * also: ast_members are usually deleted after the owner, thus
591 * this will cause invalid access
592 ast_unref(self->owner);
593 * once we allow (expression).x to access a vector-member, we need
594 * to change this: preferably by creating an alternate ast node for this
595 * purpose that is not garbage-collected.
597 ast_expression_delete((ast_expression*)self);
601 ast_array_index* ast_array_index_new(lex_ctx ctx, ast_expression *array, ast_expression *index)
603 ast_expression *outtype;
604 ast_instantiate(ast_array_index, ctx, ast_array_index_delete);
606 outtype = array->expression.next;
609 /* Error: field has no type... */
613 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_array_index_codegen);
617 ast_propagate_effects(self, array);
618 ast_propagate_effects(self, index);
620 if (!ast_type_adopt(self, outtype)) {
621 ast_array_index_delete(self);
624 if (array->expression.vtype == TYPE_FIELD && outtype->expression.vtype == TYPE_ARRAY) {
625 if (self->expression.vtype != TYPE_ARRAY) {
626 asterror(ast_ctx(self), "array_index node on type");
627 ast_array_index_delete(self);
630 self->array = outtype;
631 self->expression.vtype = TYPE_FIELD;
637 void ast_array_index_delete(ast_array_index *self)
639 ast_unref(self->array);
640 ast_unref(self->index);
641 ast_expression_delete((ast_expression*)self);
645 ast_ifthen* ast_ifthen_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
647 ast_instantiate(ast_ifthen, ctx, ast_ifthen_delete);
648 if (!ontrue && !onfalse) {
649 /* because it is invalid */
653 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ifthen_codegen);
656 self->on_true = ontrue;
657 self->on_false = onfalse;
658 ast_propagate_effects(self, cond);
660 ast_propagate_effects(self, ontrue);
662 ast_propagate_effects(self, onfalse);
667 void ast_ifthen_delete(ast_ifthen *self)
669 ast_unref(self->cond);
671 ast_unref(self->on_true);
673 ast_unref(self->on_false);
674 ast_expression_delete((ast_expression*)self);
678 ast_ternary* ast_ternary_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
680 ast_instantiate(ast_ternary, ctx, ast_ternary_delete);
681 /* This time NEITHER must be NULL */
682 if (!ontrue || !onfalse) {
686 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ternary_codegen);
689 self->on_true = ontrue;
690 self->on_false = onfalse;
691 ast_propagate_effects(self, cond);
692 ast_propagate_effects(self, ontrue);
693 ast_propagate_effects(self, onfalse);
695 if (!ast_type_adopt(self, ontrue)) {
696 ast_ternary_delete(self);
703 void ast_ternary_delete(ast_ternary *self)
705 ast_unref(self->cond);
706 ast_unref(self->on_true);
707 ast_unref(self->on_false);
708 ast_expression_delete((ast_expression*)self);
712 ast_loop* ast_loop_new(lex_ctx ctx,
713 ast_expression *initexpr,
714 ast_expression *precond,
715 ast_expression *postcond,
716 ast_expression *increment,
717 ast_expression *body)
719 ast_instantiate(ast_loop, ctx, ast_loop_delete);
720 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_loop_codegen);
722 self->initexpr = initexpr;
723 self->precond = precond;
724 self->postcond = postcond;
725 self->increment = increment;
729 ast_propagate_effects(self, initexpr);
731 ast_propagate_effects(self, precond);
733 ast_propagate_effects(self, postcond);
735 ast_propagate_effects(self, increment);
737 ast_propagate_effects(self, body);
742 void ast_loop_delete(ast_loop *self)
745 ast_unref(self->initexpr);
747 ast_unref(self->precond);
749 ast_unref(self->postcond);
751 ast_unref(self->increment);
753 ast_unref(self->body);
754 ast_expression_delete((ast_expression*)self);
758 ast_breakcont* ast_breakcont_new(lex_ctx ctx, bool iscont)
760 ast_instantiate(ast_breakcont, ctx, ast_breakcont_delete);
761 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_breakcont_codegen);
763 self->is_continue = iscont;
768 void ast_breakcont_delete(ast_breakcont *self)
770 ast_expression_delete((ast_expression*)self);
774 ast_switch* ast_switch_new(lex_ctx ctx, ast_expression *op)
776 ast_instantiate(ast_switch, ctx, ast_switch_delete);
777 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_switch_codegen);
782 ast_propagate_effects(self, op);
787 void ast_switch_delete(ast_switch *self)
790 ast_unref(self->operand);
792 for (i = 0; i < vec_size(self->cases); ++i) {
793 if (self->cases[i].value)
794 ast_unref(self->cases[i].value);
795 ast_unref(self->cases[i].code);
797 vec_free(self->cases);
799 ast_expression_delete((ast_expression*)self);
803 ast_call* ast_call_new(lex_ctx ctx,
804 ast_expression *funcexpr)
806 ast_instantiate(ast_call, ctx, ast_call_delete);
807 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_call_codegen);
809 ast_side_effects(self) = true;
812 self->func = funcexpr;
814 self->expression.vtype = funcexpr->expression.next->expression.vtype;
815 if (funcexpr->expression.next->expression.next)
816 self->expression.next = ast_type_copy(ctx, funcexpr->expression.next->expression.next);
821 void ast_call_delete(ast_call *self)
824 for (i = 0; i < vec_size(self->params); ++i)
825 ast_unref(self->params[i]);
826 vec_free(self->params);
829 ast_unref(self->func);
831 ast_expression_delete((ast_expression*)self);
835 bool ast_call_check_types(ast_call *self)
839 const ast_expression *func = self->func;
840 size_t count = vec_size(self->params);
841 if (count > vec_size(func->expression.params))
842 count = vec_size(func->expression.params);
844 for (i = 0; i < count; ++i) {
845 if (!ast_compare_type(self->params[i], (ast_expression*)(func->expression.params[i]))) {
848 ast_type_to_string(self->params[i], tgot, sizeof(tgot));
849 ast_type_to_string((ast_expression*)func->expression.params[i], texp, sizeof(texp));
850 asterror(ast_ctx(self), "invalid type for parameter %u in function call: expected %s, got %s",
851 (unsigned int)(i+1), texp, tgot);
852 /* we don't immediately return */
859 ast_store* ast_store_new(lex_ctx ctx, int op,
860 ast_expression *dest, ast_expression *source)
862 ast_instantiate(ast_store, ctx, ast_store_delete);
863 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_store_codegen);
865 ast_side_effects(self) = true;
869 self->source = source;
871 self->expression.vtype = dest->expression.vtype;
872 if (dest->expression.next) {
873 self->expression.next = ast_type_copy(ctx, dest);
874 if (!self->expression.next) {
880 self->expression.next = NULL;
885 void ast_store_delete(ast_store *self)
887 ast_unref(self->dest);
888 ast_unref(self->source);
889 ast_expression_delete((ast_expression*)self);
893 ast_block* ast_block_new(lex_ctx ctx)
895 ast_instantiate(ast_block, ctx, ast_block_delete);
896 ast_expression_init((ast_expression*)self,
897 (ast_expression_codegen*)&ast_block_codegen);
901 self->collect = NULL;
906 void ast_block_add_expr(ast_block *self, ast_expression *e)
908 ast_propagate_effects(self, e);
909 vec_push(self->exprs, e);
912 void ast_block_collect(ast_block *self, ast_expression *expr)
914 vec_push(self->collect, expr);
915 expr->expression.node.keep = true;
918 void ast_block_delete(ast_block *self)
921 for (i = 0; i < vec_size(self->exprs); ++i)
922 ast_unref(self->exprs[i]);
923 vec_free(self->exprs);
924 for (i = 0; i < vec_size(self->locals); ++i)
925 ast_delete(self->locals[i]);
926 vec_free(self->locals);
927 for (i = 0; i < vec_size(self->collect); ++i)
928 ast_delete(self->collect[i]);
929 vec_free(self->collect);
930 ast_expression_delete((ast_expression*)self);
934 bool ast_block_set_type(ast_block *self, ast_expression *from)
936 if (self->expression.next)
937 ast_delete(self->expression.next);
938 self->expression.vtype = from->expression.vtype;
939 if (from->expression.next) {
940 self->expression.next = ast_type_copy(self->expression.node.context, from->expression.next);
941 if (!self->expression.next)
945 self->expression.next = NULL;
949 ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype)
951 ast_instantiate(ast_function, ctx, ast_function_delete);
955 vtype->expression.vtype != TYPE_FUNCTION)
957 asterror(ast_ctx(self), "internal error: ast_function_new condition %i %i type=%i",
959 (int)vtype->hasvalue,
960 vtype->expression.vtype);
966 self->name = name ? util_strdup(name) : NULL;
969 self->labelcount = 0;
972 self->ir_func = NULL;
973 self->curblock = NULL;
975 self->breakblock = NULL;
976 self->continueblock = NULL;
978 vtype->hasvalue = true;
979 vtype->constval.vfunc = self;
984 void ast_function_delete(ast_function *self)
988 mem_d((void*)self->name);
990 /* ast_value_delete(self->vtype); */
991 self->vtype->hasvalue = false;
992 self->vtype->constval.vfunc = NULL;
993 /* We use unref - if it was stored in a global table it is supposed
994 * to be deleted from *there*
996 ast_unref(self->vtype);
998 for (i = 0; i < vec_size(self->blocks); ++i)
999 ast_delete(self->blocks[i]);
1000 vec_free(self->blocks);
1004 const char* ast_function_label(ast_function *self, const char *prefix)
1010 if (!opts_dump && !opts_dumpfin)
1013 id = (self->labelcount++);
1014 len = strlen(prefix);
1016 from = self->labelbuf + sizeof(self->labelbuf)-1;
1019 unsigned int digit = id % 10;
1020 *from = digit + '0';
1023 memcpy(from - len, prefix, len);
1027 /*********************************************************************/
1029 * by convention you must never pass NULL to the 'ir_value **out'
1030 * parameter. If you really don't care about the output, pass a dummy.
1031 * But I can't imagine a pituation where the output is truly unnecessary.
1034 bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out)
1038 /* NOTE: This is the codegen for a variable used in an expression.
1039 * It is not the codegen to generate the value. For this purpose,
1040 * ast_local_codegen and ast_global_codegen are to be used before this
1041 * is executed. ast_function_codegen should take care of its locals,
1042 * and the ast-user should take care of ast_global_codegen to be used
1043 * on all the globals.
1046 char typename[1024];
1047 ast_type_to_string((ast_expression*)self, typename, sizeof(typename));
1048 asterror(ast_ctx(self), "ast_value used before generated %s %s", typename, self->name);
1055 bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
1059 if (self->hasvalue && self->expression.vtype == TYPE_FUNCTION)
1061 ir_function *func = ir_builder_create_function(ir, self->name, self->expression.next->expression.vtype);
1064 func->context = ast_ctx(self);
1065 func->value->context = ast_ctx(self);
1067 self->constval.vfunc->ir_func = func;
1068 self->ir_v = func->value;
1069 /* The function is filled later on ast_function_codegen... */
1073 if (isfield && self->expression.vtype == TYPE_FIELD) {
1074 ast_expression *fieldtype = self->expression.next;
1076 if (self->hasvalue) {
1077 asterror(ast_ctx(self), "TODO: constant field pointers with value");
1081 if (fieldtype->expression.vtype == TYPE_ARRAY) {
1086 ast_expression_common *elemtype;
1088 ast_value *array = (ast_value*)fieldtype;
1090 if (!ast_istype(fieldtype, ast_value)) {
1091 asterror(ast_ctx(self), "internal error: ast_value required");
1095 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1096 if (!array->expression.count || array->expression.count > opts_max_array_size)
1097 asterror(ast_ctx(self), "Invalid array of size %lu", (unsigned long)array->expression.count);
1099 elemtype = &array->expression.next->expression;
1100 vtype = elemtype->vtype;
1102 v = ir_builder_create_field(ir, self->name, vtype);
1104 asterror(ast_ctx(self), "ir_builder_create_global failed");
1107 if (vtype == TYPE_FIELD)
1108 v->fieldtype = elemtype->next->expression.vtype;
1109 v->context = ast_ctx(self);
1110 array->ir_v = self->ir_v = v;
1112 namelen = strlen(self->name);
1113 name = (char*)mem_a(namelen + 16);
1114 strcpy(name, self->name);
1116 array->ir_values = (ir_value**)mem_a(sizeof(array->ir_values[0]) * array->expression.count);
1117 array->ir_values[0] = v;
1118 for (ai = 1; ai < array->expression.count; ++ai) {
1119 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1120 array->ir_values[ai] = ir_builder_create_field(ir, name, vtype);
1121 if (!array->ir_values[ai]) {
1123 asterror(ast_ctx(self), "ir_builder_create_global failed");
1126 if (vtype == TYPE_FIELD)
1127 array->ir_values[ai]->fieldtype = elemtype->next->expression.vtype;
1128 array->ir_values[ai]->context = ast_ctx(self);
1134 v = ir_builder_create_field(ir, self->name, self->expression.next->expression.vtype);
1137 v->context = ast_ctx(self);
1143 if (self->expression.vtype == TYPE_ARRAY) {
1148 ast_expression_common *elemtype = &self->expression.next->expression;
1149 int vtype = elemtype->vtype;
1151 /* same as with field arrays */
1152 if (!self->expression.count || self->expression.count > opts_max_array_size)
1153 asterror(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1155 v = ir_builder_create_global(ir, self->name, vtype);
1157 asterror(ast_ctx(self), "ir_builder_create_global failed");
1160 if (vtype == TYPE_FIELD)
1161 v->fieldtype = elemtype->next->expression.vtype;
1162 v->context = ast_ctx(self);
1164 namelen = strlen(self->name);
1165 name = (char*)mem_a(namelen + 16);
1166 strcpy(name, self->name);
1168 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1169 self->ir_values[0] = v;
1170 for (ai = 1; ai < self->expression.count; ++ai) {
1171 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1172 self->ir_values[ai] = ir_builder_create_global(ir, name, vtype);
1173 if (!self->ir_values[ai]) {
1175 asterror(ast_ctx(self), "ir_builder_create_global failed");
1178 if (vtype == TYPE_FIELD)
1179 self->ir_values[ai]->fieldtype = elemtype->next->expression.vtype;
1180 self->ir_values[ai]->context = ast_ctx(self);
1186 /* Arrays don't do this since there's no "array" value which spans across the
1189 v = ir_builder_create_global(ir, self->name, self->expression.vtype);
1191 asterror(ast_ctx(self), "ir_builder_create_global failed");
1194 if (self->expression.vtype == TYPE_FIELD)
1195 v->fieldtype = self->expression.next->expression.vtype;
1196 v->context = ast_ctx(self);
1199 if (self->hasvalue) {
1200 switch (self->expression.vtype)
1203 if (!ir_value_set_float(v, self->constval.vfloat))
1207 if (!ir_value_set_vector(v, self->constval.vvec))
1211 if (!ir_value_set_string(v, self->constval.vstring))
1215 asterror(ast_ctx(self), "TODO: global constant array");
1218 asterror(ast_ctx(self), "global of type function not properly generated");
1220 /* Cannot generate an IR value for a function,
1221 * need a pointer pointing to a function rather.
1224 asterror(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1229 /* link us to the ir_value */
1233 error: /* clean up */
1238 bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
1241 if (self->hasvalue && self->expression.vtype == TYPE_FUNCTION)
1243 /* Do we allow local functions? I think not...
1244 * this is NOT a function pointer atm.
1249 if (self->expression.vtype == TYPE_ARRAY) {
1254 ast_expression_common *elemtype = &self->expression.next->expression;
1255 int vtype = elemtype->vtype;
1258 asterror(ast_ctx(self), "array-parameters are not supported");
1262 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1263 if (!self->expression.count || self->expression.count > opts_max_array_size) {
1264 asterror(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1267 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1268 if (!self->ir_values) {
1269 asterror(ast_ctx(self), "failed to allocate array values");
1273 v = ir_function_create_local(func, self->name, vtype, param);
1275 asterror(ast_ctx(self), "ir_function_create_local failed");
1278 if (vtype == TYPE_FIELD)
1279 v->fieldtype = elemtype->next->expression.vtype;
1280 v->context = ast_ctx(self);
1282 namelen = strlen(self->name);
1283 name = (char*)mem_a(namelen + 16);
1284 strcpy(name, self->name);
1286 self->ir_values[0] = v;
1287 for (ai = 1; ai < self->expression.count; ++ai) {
1288 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1289 self->ir_values[ai] = ir_function_create_local(func, name, vtype, param);
1290 if (!self->ir_values[ai]) {
1291 asterror(ast_ctx(self), "ir_builder_create_global failed");
1294 if (vtype == TYPE_FIELD)
1295 self->ir_values[ai]->fieldtype = elemtype->next->expression.vtype;
1296 self->ir_values[ai]->context = ast_ctx(self);
1301 v = ir_function_create_local(func, self->name, self->expression.vtype, param);
1304 if (self->expression.vtype == TYPE_FIELD)
1305 v->fieldtype = self->expression.next->expression.vtype;
1306 v->context = ast_ctx(self);
1309 /* A constant local... hmmm...
1310 * I suppose the IR will have to deal with this
1312 if (self->hasvalue) {
1313 switch (self->expression.vtype)
1316 if (!ir_value_set_float(v, self->constval.vfloat))
1320 if (!ir_value_set_vector(v, self->constval.vvec))
1324 if (!ir_value_set_string(v, self->constval.vstring))
1328 asterror(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1333 /* link us to the ir_value */
1337 if (!ast_global_codegen(self->setter, func->owner, false) ||
1338 !ast_function_codegen(self->setter->constval.vfunc, func->owner) ||
1339 !ir_function_finalize(self->setter->constval.vfunc->ir_func))
1343 if (!ast_global_codegen(self->getter, func->owner, false) ||
1344 !ast_function_codegen(self->getter->constval.vfunc, func->owner) ||
1345 !ir_function_finalize(self->getter->constval.vfunc->ir_func))
1350 error: /* clean up */
1355 bool ast_function_codegen(ast_function *self, ir_builder *ir)
1359 ast_expression_common *ec;
1364 irf = self->ir_func;
1366 asterror(ast_ctx(self), "ast_function's related ast_value was not generated yet");
1370 /* fill the parameter list */
1371 ec = &self->vtype->expression;
1372 for (i = 0; i < vec_size(ec->params); ++i)
1374 vec_push(irf->params, ec->params[i]->expression.vtype);
1375 if (!self->builtin) {
1376 if (!ast_local_codegen(ec->params[i], self->ir_func, true))
1381 if (self->builtin) {
1382 irf->builtin = self->builtin;
1386 if (!vec_size(self->blocks)) {
1387 asterror(ast_ctx(self), "function `%s` has no body", self->name);
1391 self->curblock = ir_function_create_block(irf, "entry");
1392 if (!self->curblock) {
1393 asterror(ast_ctx(self), "failed to allocate entry block for `%s`", self->name);
1397 for (i = 0; i < vec_size(self->blocks); ++i) {
1398 ast_expression_codegen *gen = self->blocks[i]->expression.codegen;
1399 if (!(*gen)((ast_expression*)self->blocks[i], self, false, &dummy))
1403 /* TODO: check return types */
1404 if (!self->curblock->is_return)
1406 return ir_block_create_return(self->curblock, NULL);
1407 /* From now on the parser has to handle this situation */
1409 if (!self->vtype->expression.next ||
1410 self->vtype->expression.next->expression.vtype == TYPE_VOID)
1412 return ir_block_create_return(self->curblock, NULL);
1416 /* error("missing return"); */
1417 asterror(ast_ctx(self), "function `%s` missing return value", self->name);
1425 /* Note, you will not see ast_block_codegen generate ir_blocks.
1426 * To the AST and the IR, blocks are 2 different things.
1427 * In the AST it represents a block of code, usually enclosed in
1428 * curly braces {...}.
1429 * While in the IR it represents a block in terms of control-flow.
1431 bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_value **out)
1435 /* We don't use this
1436 * Note: an ast-representation using the comma-operator
1437 * of the form: (a, b, c) = x should not assign to c...
1440 asterror(ast_ctx(self), "not an l-value (code-block)");
1444 if (self->expression.outr) {
1445 *out = self->expression.outr;
1449 /* output is NULL at first, we'll have each expression
1450 * assign to out output, thus, a comma-operator represention
1451 * using an ast_block will return the last generated value,
1452 * so: (b, c) + a executed both b and c, and returns c,
1453 * which is then added to a.
1457 /* generate locals */
1458 for (i = 0; i < vec_size(self->locals); ++i)
1460 if (!ast_local_codegen(self->locals[i], func->ir_func, false)) {
1462 asterror(ast_ctx(self), "failed to generate local `%s`", self->locals[i]->name);
1467 for (i = 0; i < vec_size(self->exprs); ++i)
1469 ast_expression_codegen *gen = self->exprs[i]->expression.codegen;
1470 if (func->curblock->final) {
1471 asterror(ast_ctx(self->exprs[i]), "unreachable statement");
1474 if (!(*gen)(self->exprs[i], func, false, out))
1478 self->expression.outr = *out;
1483 bool ast_store_codegen(ast_store *self, ast_function *func, bool lvalue, ir_value **out)
1485 ast_expression_codegen *cgen;
1486 ir_value *left = NULL;
1487 ir_value *right = NULL;
1491 ast_array_index *ai = NULL;
1493 if (lvalue && self->expression.outl) {
1494 *out = self->expression.outl;
1498 if (!lvalue && self->expression.outr) {
1499 *out = self->expression.outr;
1503 if (ast_istype(self->dest, ast_array_index))
1506 ai = (ast_array_index*)self->dest;
1507 idx = (ast_value*)ai->index;
1509 if (ast_istype(ai->index, ast_value) && idx->hasvalue)
1514 /* we need to call the setter */
1515 ir_value *iridx, *funval;
1519 asterror(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1523 arr = (ast_value*)ai->array;
1524 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1525 asterror(ast_ctx(self), "value has no setter (%s)", arr->name);
1529 cgen = idx->expression.codegen;
1530 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1533 cgen = arr->setter->expression.codegen;
1534 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
1537 cgen = self->source->expression.codegen;
1538 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1541 call = ir_block_create_call(func->curblock, ast_function_label(func, "store"), funval);
1544 ir_call_param(call, iridx);
1545 ir_call_param(call, right);
1546 self->expression.outr = right;
1552 cgen = self->dest->expression.codegen;
1554 if (!(*cgen)((ast_expression*)(self->dest), func, true, &left))
1556 self->expression.outl = left;
1558 cgen = self->source->expression.codegen;
1560 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1563 if (!ir_block_create_store_op(func->curblock, self->op, left, right))
1565 self->expression.outr = right;
1568 /* Theoretically, an assinment returns its left side as an
1569 * lvalue, if we don't need an lvalue though, we return
1570 * the right side as an rvalue, otherwise we have to
1571 * somehow know whether or not we need to dereference the pointer
1572 * on the left side - that is: OP_LOAD if it was an address.
1573 * Also: in original QC we cannot OP_LOADP *anyway*.
1575 *out = (lvalue ? left : right);
1580 bool ast_binary_codegen(ast_binary *self, ast_function *func, bool lvalue, ir_value **out)
1582 ast_expression_codegen *cgen;
1583 ir_value *left, *right;
1585 /* A binary operation cannot yield an l-value */
1587 asterror(ast_ctx(self), "not an l-value (binop)");
1591 if (self->expression.outr) {
1592 *out = self->expression.outr;
1596 if (OPTS_FLAG(SHORT_LOGIC) &&
1597 (self->op == INSTR_AND || self->op == INSTR_OR))
1599 /* short circuit evaluation */
1600 ir_block *other, *merge;
1601 ir_block *from_left, *from_right;
1606 /* Note about casting to true boolean values:
1607 * We use a single NOT for sub expressions, and an
1608 * overall NOT at the end, and for that purpose swap
1609 * all the jump conditions in order for the NOT to get
1611 * ie: (a && b) usually becomes (!!a ? !!b : !!a)
1612 * but we translate this to (!(!a ? !a : !b))
1615 merge_id = vec_size(func->ir_func->blocks);
1616 merge = ir_function_create_block(func->ir_func, ast_function_label(func, "sce_merge"));
1618 cgen = self->left->expression.codegen;
1619 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1621 if (!OPTS_FLAG(PERL_LOGIC)) {
1622 notop = type_not_instr[left->vtype];
1623 if (notop == AINSTR_END) {
1624 asterror(ast_ctx(self), "don't know how to cast to bool...");
1627 left = ir_block_create_unary(func->curblock,
1628 ast_function_label(func, "sce_not"),
1632 from_left = func->curblock;
1634 other = ir_function_create_block(func->ir_func, ast_function_label(func, "sce_other"));
1635 if ( !(self->op == INSTR_OR) != !OPTS_FLAG(PERL_LOGIC) ) {
1636 if (!ir_block_create_if(func->curblock, left, other, merge))
1639 if (!ir_block_create_if(func->curblock, left, merge, other))
1642 /* use the likely flag */
1643 vec_last(func->curblock->instr)->likely = true;
1645 func->curblock = other;
1646 cgen = self->right->expression.codegen;
1647 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1649 if (!OPTS_FLAG(PERL_LOGIC)) {
1650 notop = type_not_instr[right->vtype];
1651 if (notop == AINSTR_END) {
1652 asterror(ast_ctx(self), "don't know how to cast to bool...");
1655 right = ir_block_create_unary(func->curblock,
1656 ast_function_label(func, "sce_not"),
1660 from_right = func->curblock;
1662 if (!ir_block_create_jump(func->curblock, merge))
1665 vec_remove(func->ir_func->blocks, merge_id, 1);
1666 vec_push(func->ir_func->blocks, merge);
1668 func->curblock = merge;
1669 phi = ir_block_create_phi(func->curblock, ast_function_label(func, "sce_value"), TYPE_FLOAT);
1670 ir_phi_add(phi, from_left, left);
1671 ir_phi_add(phi, from_right, right);
1672 *out = ir_phi_value(phi);
1673 if (!OPTS_FLAG(PERL_LOGIC)) {
1674 notop = type_not_instr[(*out)->vtype];
1675 if (notop == AINSTR_END) {
1676 asterror(ast_ctx(self), "don't know how to cast to bool...");
1679 *out = ir_block_create_unary(func->curblock,
1680 ast_function_label(func, "sce_final_not"),
1686 self->expression.outr = *out;
1690 cgen = self->left->expression.codegen;
1691 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1694 cgen = self->right->expression.codegen;
1695 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1698 *out = ir_block_create_binop(func->curblock, ast_function_label(func, "bin"),
1699 self->op, left, right);
1702 self->expression.outr = *out;
1707 bool ast_binstore_codegen(ast_binstore *self, ast_function *func, bool lvalue, ir_value **out)
1709 ast_expression_codegen *cgen;
1710 ir_value *leftl = NULL, *leftr, *right, *bin;
1714 ast_array_index *ai = NULL;
1715 ir_value *iridx = NULL;
1717 if (lvalue && self->expression.outl) {
1718 *out = self->expression.outl;
1722 if (!lvalue && self->expression.outr) {
1723 *out = self->expression.outr;
1727 if (ast_istype(self->dest, ast_array_index))
1730 ai = (ast_array_index*)self->dest;
1731 idx = (ast_value*)ai->index;
1733 if (ast_istype(ai->index, ast_value) && idx->hasvalue)
1737 /* for a binstore we need both an lvalue and an rvalue for the left side */
1738 /* rvalue of destination! */
1740 cgen = idx->expression.codegen;
1741 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1744 cgen = self->dest->expression.codegen;
1745 if (!(*cgen)((ast_expression*)(self->dest), func, false, &leftr))
1748 /* source as rvalue only */
1749 cgen = self->source->expression.codegen;
1750 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1753 /* now the binary */
1754 bin = ir_block_create_binop(func->curblock, ast_function_label(func, "binst"),
1755 self->opbin, leftr, right);
1756 self->expression.outr = bin;
1760 /* we need to call the setter */
1765 asterror(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1769 arr = (ast_value*)ai->array;
1770 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1771 asterror(ast_ctx(self), "value has no setter (%s)", arr->name);
1775 cgen = arr->setter->expression.codegen;
1776 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
1779 call = ir_block_create_call(func->curblock, ast_function_label(func, "store"), funval);
1782 ir_call_param(call, iridx);
1783 ir_call_param(call, bin);
1784 self->expression.outr = bin;
1786 /* now store them */
1787 cgen = self->dest->expression.codegen;
1788 /* lvalue of destination */
1789 if (!(*cgen)((ast_expression*)(self->dest), func, true, &leftl))
1791 self->expression.outl = leftl;
1793 if (!ir_block_create_store_op(func->curblock, self->opstore, leftl, bin))
1795 self->expression.outr = bin;
1798 /* Theoretically, an assinment returns its left side as an
1799 * lvalue, if we don't need an lvalue though, we return
1800 * the right side as an rvalue, otherwise we have to
1801 * somehow know whether or not we need to dereference the pointer
1802 * on the left side - that is: OP_LOAD if it was an address.
1803 * Also: in original QC we cannot OP_LOADP *anyway*.
1805 *out = (lvalue ? leftl : bin);
1810 bool ast_unary_codegen(ast_unary *self, ast_function *func, bool lvalue, ir_value **out)
1812 ast_expression_codegen *cgen;
1815 /* An unary operation cannot yield an l-value */
1817 asterror(ast_ctx(self), "not an l-value (binop)");
1821 if (self->expression.outr) {
1822 *out = self->expression.outr;
1826 cgen = self->operand->expression.codegen;
1828 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
1831 *out = ir_block_create_unary(func->curblock, ast_function_label(func, "unary"),
1835 self->expression.outr = *out;
1840 bool ast_return_codegen(ast_return *self, ast_function *func, bool lvalue, ir_value **out)
1842 ast_expression_codegen *cgen;
1847 /* In the context of a return operation, we don't actually return
1851 asterror(ast_ctx(self), "return-expression is not an l-value");
1855 if (self->expression.outr) {
1856 asterror(ast_ctx(self), "internal error: ast_return cannot be reused, it bears no result!");
1859 self->expression.outr = (ir_value*)1;
1861 if (self->operand) {
1862 cgen = self->operand->expression.codegen;
1864 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
1867 if (!ir_block_create_return(func->curblock, operand))
1870 if (!ir_block_create_return(func->curblock, NULL))
1877 bool ast_entfield_codegen(ast_entfield *self, ast_function *func, bool lvalue, ir_value **out)
1879 ast_expression_codegen *cgen;
1880 ir_value *ent, *field;
1882 /* This function needs to take the 'lvalue' flag into account!
1883 * As lvalue we provide a field-pointer, as rvalue we provide the
1887 if (lvalue && self->expression.outl) {
1888 *out = self->expression.outl;
1892 if (!lvalue && self->expression.outr) {
1893 *out = self->expression.outr;
1897 cgen = self->entity->expression.codegen;
1898 if (!(*cgen)((ast_expression*)(self->entity), func, false, &ent))
1901 cgen = self->field->expression.codegen;
1902 if (!(*cgen)((ast_expression*)(self->field), func, false, &field))
1907 *out = ir_block_create_fieldaddress(func->curblock, ast_function_label(func, "efa"),
1910 *out = ir_block_create_load_from_ent(func->curblock, ast_function_label(func, "efv"),
1911 ent, field, self->expression.vtype);
1914 asterror(ast_ctx(self), "failed to create %s instruction (output type %s)",
1915 (lvalue ? "ADDRESS" : "FIELD"),
1916 type_name[self->expression.vtype]);
1921 self->expression.outl = *out;
1923 self->expression.outr = *out;
1925 /* Hm that should be it... */
1929 bool ast_member_codegen(ast_member *self, ast_function *func, bool lvalue, ir_value **out)
1931 ast_expression_codegen *cgen;
1934 /* in QC this is always an lvalue */
1936 if (self->expression.outl) {
1937 *out = self->expression.outl;
1941 cgen = self->owner->expression.codegen;
1942 if (!(*cgen)((ast_expression*)(self->owner), func, true, &vec))
1945 if (vec->vtype != TYPE_VECTOR &&
1946 !(vec->vtype == TYPE_FIELD && self->owner->expression.next->expression.vtype == TYPE_VECTOR))
1951 *out = ir_value_vector_member(vec, self->field);
1952 self->expression.outl = *out;
1954 return (*out != NULL);
1957 bool ast_array_index_codegen(ast_array_index *self, ast_function *func, bool lvalue, ir_value **out)
1962 if (!lvalue && self->expression.outr) {
1963 *out = self->expression.outr;
1965 if (lvalue && self->expression.outl) {
1966 *out = self->expression.outl;
1969 if (!ast_istype(self->array, ast_value)) {
1970 asterror(ast_ctx(self), "array indexing this way is not supported");
1971 /* note this would actually be pointer indexing because the left side is
1972 * not an actual array but (hopefully) an indexable expression.
1973 * Once we get integer arithmetic, and GADDRESS/GSTORE/GLOAD instruction
1974 * support this path will be filled.
1979 arr = (ast_value*)self->array;
1980 idx = (ast_value*)self->index;
1982 if (!ast_istype(self->index, ast_value) || !idx->hasvalue) {
1983 /* Time to use accessor functions */
1984 ast_expression_codegen *cgen;
1985 ir_value *iridx, *funval;
1989 asterror(ast_ctx(self), "(.2) array indexing here needs a compile-time constant");
1994 asterror(ast_ctx(self), "value has no getter, don't know how to index it");
1998 cgen = self->index->expression.codegen;
1999 if (!(*cgen)((ast_expression*)(self->index), func, true, &iridx))
2002 cgen = arr->getter->expression.codegen;
2003 if (!(*cgen)((ast_expression*)(arr->getter), func, true, &funval))
2006 call = ir_block_create_call(func->curblock, ast_function_label(func, "fetch"), funval);
2009 ir_call_param(call, iridx);
2011 *out = ir_call_value(call);
2012 self->expression.outr = *out;
2016 if (idx->expression.vtype == TYPE_FLOAT)
2017 *out = arr->ir_values[(int)idx->constval.vfloat];
2018 else if (idx->expression.vtype == TYPE_INTEGER)
2019 *out = arr->ir_values[idx->constval.vint];
2021 asterror(ast_ctx(self), "array indexing here needs an integer constant");
2027 bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_value **out)
2029 ast_expression_codegen *cgen;
2034 ir_block *cond = func->curblock;
2037 ir_block *ontrue_endblock = NULL;
2038 ir_block *onfalse_endblock = NULL;
2041 /* We don't output any value, thus also don't care about r/lvalue */
2045 if (self->expression.outr) {
2046 asterror(ast_ctx(self), "internal error: ast_ifthen cannot be reused, it bears no result!");
2049 self->expression.outr = (ir_value*)1;
2051 /* generate the condition */
2052 cgen = self->cond->expression.codegen;
2053 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2055 /* update the block which will get the jump - because short-logic or ternaries may have changed this */
2056 cond = func->curblock;
2060 if (self->on_true) {
2061 /* create on-true block */
2062 ontrue = ir_function_create_block(func->ir_func, ast_function_label(func, "ontrue"));
2066 /* enter the block */
2067 func->curblock = ontrue;
2070 cgen = self->on_true->expression.codegen;
2071 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &dummy))
2074 /* we now need to work from the current endpoint */
2075 ontrue_endblock = func->curblock;
2080 if (self->on_false) {
2081 /* create on-false block */
2082 onfalse = ir_function_create_block(func->ir_func, ast_function_label(func, "onfalse"));
2086 /* enter the block */
2087 func->curblock = onfalse;
2090 cgen = self->on_false->expression.codegen;
2091 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &dummy))
2094 /* we now need to work from the current endpoint */
2095 onfalse_endblock = func->curblock;
2099 /* Merge block were they all merge in to */
2100 merge = ir_function_create_block(func->ir_func, ast_function_label(func, "endif"));
2103 /* add jumps ot the merge block */
2104 if (ontrue && !ontrue_endblock->final && !ir_block_create_jump(ontrue_endblock, merge))
2106 if (onfalse && !onfalse_endblock->final && !ir_block_create_jump(onfalse_endblock, merge))
2109 /* we create the if here, that way all blocks are ordered :)
2111 if (!ir_block_create_if(cond, condval,
2112 (ontrue ? ontrue : merge),
2113 (onfalse ? onfalse : merge)))
2118 /* Now enter the merge block */
2119 func->curblock = merge;
2124 bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_value **out)
2126 ast_expression_codegen *cgen;
2129 ir_value *trueval, *falseval;
2132 ir_block *cond = func->curblock;
2137 /* Ternary can never create an lvalue... */
2141 /* In theory it shouldn't be possible to pass through a node twice, but
2142 * in case we add any kind of optimization pass for the AST itself, it
2143 * may still happen, thus we remember a created ir_value and simply return one
2144 * if it already exists.
2146 if (self->expression.outr) {
2147 *out = self->expression.outr;
2151 /* In the following, contraty to ast_ifthen, we assume both paths exist. */
2153 /* generate the condition */
2154 func->curblock = cond;
2155 cgen = self->cond->expression.codegen;
2156 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2159 /* create on-true block */
2160 ontrue = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_T"));
2165 /* enter the block */
2166 func->curblock = ontrue;
2169 cgen = self->on_true->expression.codegen;
2170 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &trueval))
2174 /* create on-false block */
2175 onfalse = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_F"));
2180 /* enter the block */
2181 func->curblock = onfalse;
2184 cgen = self->on_false->expression.codegen;
2185 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &falseval))
2189 /* create merge block */
2190 merge = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_out"));
2193 /* jump to merge block */
2194 if (!ir_block_create_jump(ontrue, merge))
2196 if (!ir_block_create_jump(onfalse, merge))
2199 /* create if instruction */
2200 if (!ir_block_create_if(cond, condval, ontrue, onfalse))
2203 /* Now enter the merge block */
2204 func->curblock = merge;
2206 /* Here, now, we need a PHI node
2207 * but first some sanity checking...
2209 if (trueval->vtype != falseval->vtype) {
2210 /* error("ternary with different types on the two sides"); */
2215 phi = ir_block_create_phi(merge, ast_function_label(func, "phi"), trueval->vtype);
2218 ir_phi_add(phi, ontrue, trueval);
2219 ir_phi_add(phi, onfalse, falseval);
2221 self->expression.outr = ir_phi_value(phi);
2222 *out = self->expression.outr;
2227 bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value **out)
2229 ast_expression_codegen *cgen;
2231 ir_value *dummy = NULL;
2232 ir_value *precond = NULL;
2233 ir_value *postcond = NULL;
2235 /* Since we insert some jumps "late" so we have blocks
2236 * ordered "nicely", we need to keep track of the actual end-blocks
2237 * of expressions to add the jumps to.
2239 ir_block *bbody = NULL, *end_bbody = NULL;
2240 ir_block *bprecond = NULL, *end_bprecond = NULL;
2241 ir_block *bpostcond = NULL, *end_bpostcond = NULL;
2242 ir_block *bincrement = NULL, *end_bincrement = NULL;
2243 ir_block *bout = NULL, *bin = NULL;
2245 /* let's at least move the outgoing block to the end */
2248 /* 'break' and 'continue' need to be able to find the right blocks */
2249 ir_block *bcontinue = NULL;
2250 ir_block *bbreak = NULL;
2252 ir_block *old_bcontinue = NULL;
2253 ir_block *old_bbreak = NULL;
2255 ir_block *tmpblock = NULL;
2260 if (self->expression.outr) {
2261 asterror(ast_ctx(self), "internal error: ast_loop cannot be reused, it bears no result!");
2264 self->expression.outr = (ir_value*)1;
2267 * Should we ever need some kind of block ordering, better make this function
2268 * move blocks around than write a block ordering algorithm later... after all
2269 * the ast and ir should work together, not against each other.
2272 /* initexpr doesn't get its own block, it's pointless, it could create more blocks
2273 * anyway if for example it contains a ternary.
2277 cgen = self->initexpr->expression.codegen;
2278 if (!(*cgen)((ast_expression*)(self->initexpr), func, false, &dummy))
2282 /* Store the block from which we enter this chaos */
2283 bin = func->curblock;
2285 /* The pre-loop condition needs its own block since we
2286 * need to be able to jump to the start of that expression.
2290 bprecond = ir_function_create_block(func->ir_func, ast_function_label(func, "pre_loop_cond"));
2294 /* the pre-loop-condition the least important place to 'continue' at */
2295 bcontinue = bprecond;
2298 func->curblock = bprecond;
2301 cgen = self->precond->expression.codegen;
2302 if (!(*cgen)((ast_expression*)(self->precond), func, false, &precond))
2305 end_bprecond = func->curblock;
2307 bprecond = end_bprecond = NULL;
2310 /* Now the next blocks won't be ordered nicely, but we need to
2311 * generate them this early for 'break' and 'continue'.
2313 if (self->increment) {
2314 bincrement = ir_function_create_block(func->ir_func, ast_function_label(func, "loop_increment"));
2317 bcontinue = bincrement; /* increment comes before the pre-loop-condition */
2319 bincrement = end_bincrement = NULL;
2322 if (self->postcond) {
2323 bpostcond = ir_function_create_block(func->ir_func, ast_function_label(func, "post_loop_cond"));
2326 bcontinue = bpostcond; /* postcond comes before the increment */
2328 bpostcond = end_bpostcond = NULL;
2331 bout_id = vec_size(func->ir_func->blocks);
2332 bout = ir_function_create_block(func->ir_func, ast_function_label(func, "after_loop"));
2337 /* The loop body... */
2340 bbody = ir_function_create_block(func->ir_func, ast_function_label(func, "loop_body"));
2345 func->curblock = bbody;
2347 old_bbreak = func->breakblock;
2348 old_bcontinue = func->continueblock;
2349 func->breakblock = bbreak;
2350 func->continueblock = bcontinue;
2353 cgen = self->body->expression.codegen;
2354 if (!(*cgen)((ast_expression*)(self->body), func, false, &dummy))
2357 end_bbody = func->curblock;
2358 func->breakblock = old_bbreak;
2359 func->continueblock = old_bcontinue;
2362 /* post-loop-condition */
2366 func->curblock = bpostcond;
2369 cgen = self->postcond->expression.codegen;
2370 if (!(*cgen)((ast_expression*)(self->postcond), func, false, &postcond))
2373 end_bpostcond = func->curblock;
2376 /* The incrementor */
2377 if (self->increment)
2380 func->curblock = bincrement;
2383 cgen = self->increment->expression.codegen;
2384 if (!(*cgen)((ast_expression*)(self->increment), func, false, &dummy))
2387 end_bincrement = func->curblock;
2390 /* In any case now, we continue from the outgoing block */
2391 func->curblock = bout;
2393 /* Now all blocks are in place */
2394 /* From 'bin' we jump to whatever comes first */
2395 if (bprecond) tmpblock = bprecond;
2396 else if (bbody) tmpblock = bbody;
2397 else if (bpostcond) tmpblock = bpostcond;
2398 else tmpblock = bout;
2399 if (!ir_block_create_jump(bin, tmpblock))
2405 ir_block *ontrue, *onfalse;
2406 if (bbody) ontrue = bbody;
2407 else if (bincrement) ontrue = bincrement;
2408 else if (bpostcond) ontrue = bpostcond;
2409 else ontrue = bprecond;
2411 if (!ir_block_create_if(end_bprecond, precond, ontrue, onfalse))
2418 if (bincrement) tmpblock = bincrement;
2419 else if (bpostcond) tmpblock = bpostcond;
2420 else if (bprecond) tmpblock = bprecond;
2421 else tmpblock = bout;
2422 if (!end_bbody->final && !ir_block_create_jump(end_bbody, tmpblock))
2426 /* from increment */
2429 if (bpostcond) tmpblock = bpostcond;
2430 else if (bprecond) tmpblock = bprecond;
2431 else if (bbody) tmpblock = bbody;
2432 else tmpblock = bout;
2433 if (!ir_block_create_jump(end_bincrement, tmpblock))
2440 ir_block *ontrue, *onfalse;
2441 if (bprecond) ontrue = bprecond;
2442 else if (bbody) ontrue = bbody;
2443 else if (bincrement) ontrue = bincrement;
2444 else ontrue = bpostcond;
2446 if (!ir_block_create_if(end_bpostcond, postcond, ontrue, onfalse))
2450 /* Move 'bout' to the end */
2451 vec_remove(func->ir_func->blocks, bout_id, 1);
2452 vec_push(func->ir_func->blocks, bout);
2457 bool ast_breakcont_codegen(ast_breakcont *self, ast_function *func, bool lvalue, ir_value **out)
2464 asterror(ast_ctx(self), "break/continue expression is not an l-value");
2468 if (self->expression.outr) {
2469 asterror(ast_ctx(self), "internal error: ast_breakcont cannot be reused!");
2472 self->expression.outr = (ir_value*)1;
2474 if (self->is_continue)
2475 target = func->continueblock;
2477 target = func->breakblock;
2479 if (!ir_block_create_jump(func->curblock, target))
2484 bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_value **out)
2486 ast_expression_codegen *cgen;
2488 ast_switch_case *def_case = NULL;
2489 ir_block *def_bfall = NULL;
2491 ir_value *dummy = NULL;
2492 ir_value *irop = NULL;
2493 ir_block *old_break = NULL;
2494 ir_block *bout = NULL;
2495 ir_block *bfall = NULL;
2503 asterror(ast_ctx(self), "switch expression is not an l-value");
2507 if (self->expression.outr) {
2508 asterror(ast_ctx(self), "internal error: ast_switch cannot be reused!");
2511 self->expression.outr = (ir_value*)1;
2516 cgen = self->operand->expression.codegen;
2517 if (!(*cgen)((ast_expression*)(self->operand), func, false, &irop))
2520 if (!vec_size(self->cases))
2523 cmpinstr = type_eq_instr[irop->vtype];
2524 if (cmpinstr >= AINSTR_END) {
2525 ast_type_to_string(self->operand, typestr, sizeof(typestr));
2526 asterror(ast_ctx(self), "invalid type to perform a switch on: %s", typestr);
2530 bout_id = vec_size(func->ir_func->blocks);
2531 bout = ir_function_create_block(func->ir_func, ast_function_label(func, "after_switch"));
2535 /* setup the break block */
2536 old_break = func->breakblock;
2537 func->breakblock = bout;
2539 /* Now create all cases */
2540 for (c = 0; c < vec_size(self->cases); ++c) {
2541 ir_value *cond, *val;
2542 ir_block *bcase, *bnot;
2545 ast_switch_case *swcase = &self->cases[c];
2547 if (swcase->value) {
2548 /* A regular case */
2549 /* generate the condition operand */
2550 cgen = swcase->value->expression.codegen;
2551 if (!(*cgen)((ast_expression*)(swcase->value), func, false, &val))
2553 /* generate the condition */
2554 cond = ir_block_create_binop(func->curblock, ast_function_label(func, "switch_eq"), cmpinstr, irop, val);
2558 bcase = ir_function_create_block(func->ir_func, ast_function_label(func, "case"));
2559 bnot_id = vec_size(func->ir_func->blocks);
2560 bnot = ir_function_create_block(func->ir_func, ast_function_label(func, "not_case"));
2561 if (!bcase || !bnot)
2563 if (!ir_block_create_if(func->curblock, cond, bcase, bnot))
2566 /* Make the previous case-end fall through */
2567 if (bfall && !bfall->final) {
2568 if (!ir_block_create_jump(bfall, bcase))
2572 /* enter the case */
2573 func->curblock = bcase;
2574 cgen = swcase->code->expression.codegen;
2575 if (!(*cgen)((ast_expression*)swcase->code, func, false, &dummy))
2578 /* remember this block to fall through from */
2579 bfall = func->curblock;
2581 /* enter the else and move it down */
2582 func->curblock = bnot;
2583 vec_remove(func->ir_func->blocks, bnot_id, 1);
2584 vec_push(func->ir_func->blocks, bnot);
2586 /* The default case */
2587 /* Remember where to fall through from: */
2590 /* remember which case it was */
2595 /* Jump from the last bnot to bout */
2596 if (bfall && !bfall->final && !ir_block_create_jump(bfall, bout)) {
2598 astwarning(ast_ctx(bfall), WARN_???, "missing break after last case");
2603 /* If there was a default case, put it down here */
2607 /* No need to create an extra block */
2608 bcase = func->curblock;
2610 /* Insert the fallthrough jump */
2611 if (def_bfall && !def_bfall->final) {
2612 if (!ir_block_create_jump(def_bfall, bcase))
2616 /* Now generate the default code */
2617 cgen = def_case->code->expression.codegen;
2618 if (!(*cgen)((ast_expression*)def_case->code, func, false, &dummy))
2622 /* Jump from the last bnot to bout */
2623 if (!func->curblock->final && !ir_block_create_jump(func->curblock, bout))
2625 /* enter the outgoing block */
2626 func->curblock = bout;
2628 /* restore the break block */
2629 func->breakblock = old_break;
2631 /* Move 'bout' to the end, it's nicer */
2632 vec_remove(func->ir_func->blocks, bout_id, 1);
2633 vec_push(func->ir_func->blocks, bout);
2638 bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value **out)
2640 ast_expression_codegen *cgen;
2642 ir_instr *callinstr;
2645 ir_value *funval = NULL;
2647 /* return values are never lvalues */
2649 asterror(ast_ctx(self), "not an l-value (function call)");
2653 if (self->expression.outr) {
2654 *out = self->expression.outr;
2658 cgen = self->func->expression.codegen;
2659 if (!(*cgen)((ast_expression*)(self->func), func, false, &funval))
2667 for (i = 0; i < vec_size(self->params); ++i)
2670 ast_expression *expr = self->params[i];
2672 cgen = expr->expression.codegen;
2673 if (!(*cgen)(expr, func, false, ¶m))
2677 vec_push(params, param);
2680 callinstr = ir_block_create_call(func->curblock, ast_function_label(func, "call"), funval);
2684 for (i = 0; i < vec_size(params); ++i) {
2685 ir_call_param(callinstr, params[i]);
2688 *out = ir_call_value(callinstr);
2689 self->expression.outr = *out;