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->hasvalue = false;
336 memset(&self->constval, 0, sizeof(self->constval));
339 self->ir_values = NULL;
340 self->ir_value_count = 0;
348 void ast_value_delete(ast_value* self)
351 mem_d((void*)self->name);
352 if (self->hasvalue) {
353 switch (self->expression.vtype)
356 mem_d((void*)self->constval.vstring);
359 /* unlink us from the function node */
360 self->constval.vfunc->vtype = NULL;
362 /* NOTE: delete function? currently collected in
363 * the parser structure
370 mem_d(self->ir_values);
371 ast_expression_delete((ast_expression*)self);
375 void ast_value_params_add(ast_value *self, ast_value *p)
377 vec_push(self->expression.params, p);
380 bool ast_value_set_name(ast_value *self, const char *name)
383 mem_d((void*)self->name);
384 self->name = util_strdup(name);
388 ast_binary* ast_binary_new(lex_ctx ctx, int op,
389 ast_expression* left, ast_expression* right)
391 ast_instantiate(ast_binary, ctx, ast_binary_delete);
392 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binary_codegen);
398 ast_propagate_effects(self, left);
399 ast_propagate_effects(self, right);
401 if (op >= INSTR_EQ_F && op <= INSTR_GT)
402 self->expression.vtype = TYPE_FLOAT;
403 else if (op == INSTR_AND || op == INSTR_OR ||
404 op == INSTR_BITAND || op == INSTR_BITOR)
405 self->expression.vtype = TYPE_FLOAT;
406 else if (op == INSTR_MUL_VF || op == INSTR_MUL_FV)
407 self->expression.vtype = TYPE_VECTOR;
408 else if (op == INSTR_MUL_V)
409 self->expression.vtype = TYPE_FLOAT;
411 self->expression.vtype = left->expression.vtype;
416 void ast_binary_delete(ast_binary *self)
418 ast_unref(self->left);
419 ast_unref(self->right);
420 ast_expression_delete((ast_expression*)self);
424 ast_binstore* ast_binstore_new(lex_ctx ctx, int storop, int op,
425 ast_expression* left, ast_expression* right)
427 ast_instantiate(ast_binstore, ctx, ast_binstore_delete);
428 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binstore_codegen);
430 ast_side_effects(self) = true;
432 self->opstore = storop;
435 self->source = right;
437 self->expression.vtype = left->expression.vtype;
438 if (left->expression.next) {
439 self->expression.next = ast_type_copy(ctx, left);
440 if (!self->expression.next) {
446 self->expression.next = NULL;
451 void ast_binstore_delete(ast_binstore *self)
453 ast_unref(self->dest);
454 ast_unref(self->source);
455 ast_expression_delete((ast_expression*)self);
459 ast_unary* ast_unary_new(lex_ctx ctx, int op,
460 ast_expression *expr)
462 ast_instantiate(ast_unary, ctx, ast_unary_delete);
463 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_unary_codegen);
466 self->operand = expr;
468 ast_propagate_effects(self, expr);
470 if (op >= INSTR_NOT_F && op <= INSTR_NOT_FNC) {
471 self->expression.vtype = TYPE_FLOAT;
473 asterror(ctx, "cannot determine type of unary operation %s", asm_instr[op].m);
478 void ast_unary_delete(ast_unary *self)
480 ast_unref(self->operand);
481 ast_expression_delete((ast_expression*)self);
485 ast_return* ast_return_new(lex_ctx ctx, ast_expression *expr)
487 ast_instantiate(ast_return, ctx, ast_return_delete);
488 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_return_codegen);
490 self->operand = expr;
493 ast_propagate_effects(self, expr);
498 void ast_return_delete(ast_return *self)
501 ast_unref(self->operand);
502 ast_expression_delete((ast_expression*)self);
506 ast_entfield* ast_entfield_new(lex_ctx ctx, ast_expression *entity, ast_expression *field)
508 if (field->expression.vtype != TYPE_FIELD) {
509 asterror(ctx, "ast_entfield_new with expression not of type field");
512 return ast_entfield_new_force(ctx, entity, field, field->expression.next);
515 ast_entfield* ast_entfield_new_force(lex_ctx ctx, ast_expression *entity, ast_expression *field, const ast_expression *outtype)
517 ast_instantiate(ast_entfield, ctx, ast_entfield_delete);
521 /* Error: field has no type... */
525 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_entfield_codegen);
527 self->entity = entity;
529 ast_propagate_effects(self, entity);
530 ast_propagate_effects(self, field);
532 if (!ast_type_adopt(self, outtype)) {
533 ast_entfield_delete(self);
540 void ast_entfield_delete(ast_entfield *self)
542 ast_unref(self->entity);
543 ast_unref(self->field);
544 ast_expression_delete((ast_expression*)self);
548 ast_member* ast_member_new(lex_ctx ctx, ast_expression *owner, unsigned int field, const char *name)
550 ast_instantiate(ast_member, ctx, ast_member_delete);
556 if (owner->expression.vtype != TYPE_VECTOR &&
557 owner->expression.vtype != TYPE_FIELD) {
558 asterror(ctx, "member-access on an invalid owner of type %s", type_name[owner->expression.vtype]);
563 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_member_codegen);
564 self->expression.node.keep = true; /* keep */
566 if (owner->expression.vtype == TYPE_VECTOR) {
567 self->expression.vtype = TYPE_FLOAT;
568 self->expression.next = NULL;
570 self->expression.vtype = TYPE_FIELD;
571 self->expression.next = ast_shallow_type(ctx, TYPE_FLOAT);
575 ast_propagate_effects(self, owner);
579 self->name = util_strdup(name);
586 void ast_member_delete(ast_member *self)
588 /* The owner is always an ast_value, which has .keep=true,
589 * also: ast_members are usually deleted after the owner, thus
590 * this will cause invalid access
591 ast_unref(self->owner);
592 * once we allow (expression).x to access a vector-member, we need
593 * to change this: preferably by creating an alternate ast node for this
594 * purpose that is not garbage-collected.
596 ast_expression_delete((ast_expression*)self);
600 ast_array_index* ast_array_index_new(lex_ctx ctx, ast_expression *array, ast_expression *index)
602 ast_expression *outtype;
603 ast_instantiate(ast_array_index, ctx, ast_array_index_delete);
605 outtype = array->expression.next;
608 /* Error: field has no type... */
612 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_array_index_codegen);
616 ast_propagate_effects(self, array);
617 ast_propagate_effects(self, index);
619 if (!ast_type_adopt(self, outtype)) {
620 ast_array_index_delete(self);
623 if (array->expression.vtype == TYPE_FIELD && outtype->expression.vtype == TYPE_ARRAY) {
624 if (self->expression.vtype != TYPE_ARRAY) {
625 asterror(ast_ctx(self), "array_index node on type");
626 ast_array_index_delete(self);
629 self->array = outtype;
630 self->expression.vtype = TYPE_FIELD;
636 void ast_array_index_delete(ast_array_index *self)
638 ast_unref(self->array);
639 ast_unref(self->index);
640 ast_expression_delete((ast_expression*)self);
644 ast_ifthen* ast_ifthen_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
646 ast_instantiate(ast_ifthen, ctx, ast_ifthen_delete);
647 if (!ontrue && !onfalse) {
648 /* because it is invalid */
652 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ifthen_codegen);
655 self->on_true = ontrue;
656 self->on_false = onfalse;
657 ast_propagate_effects(self, cond);
659 ast_propagate_effects(self, ontrue);
661 ast_propagate_effects(self, onfalse);
666 void ast_ifthen_delete(ast_ifthen *self)
668 ast_unref(self->cond);
670 ast_unref(self->on_true);
672 ast_unref(self->on_false);
673 ast_expression_delete((ast_expression*)self);
677 ast_ternary* ast_ternary_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
679 ast_instantiate(ast_ternary, ctx, ast_ternary_delete);
680 /* This time NEITHER must be NULL */
681 if (!ontrue || !onfalse) {
685 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ternary_codegen);
688 self->on_true = ontrue;
689 self->on_false = onfalse;
690 ast_propagate_effects(self, cond);
691 ast_propagate_effects(self, ontrue);
692 ast_propagate_effects(self, onfalse);
694 if (!ast_type_adopt(self, ontrue)) {
695 ast_ternary_delete(self);
702 void ast_ternary_delete(ast_ternary *self)
704 ast_unref(self->cond);
705 ast_unref(self->on_true);
706 ast_unref(self->on_false);
707 ast_expression_delete((ast_expression*)self);
711 ast_loop* ast_loop_new(lex_ctx ctx,
712 ast_expression *initexpr,
713 ast_expression *precond,
714 ast_expression *postcond,
715 ast_expression *increment,
716 ast_expression *body)
718 ast_instantiate(ast_loop, ctx, ast_loop_delete);
719 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_loop_codegen);
721 self->initexpr = initexpr;
722 self->precond = precond;
723 self->postcond = postcond;
724 self->increment = increment;
728 ast_propagate_effects(self, initexpr);
730 ast_propagate_effects(self, precond);
732 ast_propagate_effects(self, postcond);
734 ast_propagate_effects(self, increment);
736 ast_propagate_effects(self, body);
741 void ast_loop_delete(ast_loop *self)
744 ast_unref(self->initexpr);
746 ast_unref(self->precond);
748 ast_unref(self->postcond);
750 ast_unref(self->increment);
752 ast_unref(self->body);
753 ast_expression_delete((ast_expression*)self);
757 ast_breakcont* ast_breakcont_new(lex_ctx ctx, bool iscont)
759 ast_instantiate(ast_breakcont, ctx, ast_breakcont_delete);
760 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_breakcont_codegen);
762 self->is_continue = iscont;
767 void ast_breakcont_delete(ast_breakcont *self)
769 ast_expression_delete((ast_expression*)self);
773 ast_switch* ast_switch_new(lex_ctx ctx, ast_expression *op)
775 ast_instantiate(ast_switch, ctx, ast_switch_delete);
776 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_switch_codegen);
781 ast_propagate_effects(self, op);
786 void ast_switch_delete(ast_switch *self)
789 ast_unref(self->operand);
791 for (i = 0; i < vec_size(self->cases); ++i) {
792 if (self->cases[i].value)
793 ast_unref(self->cases[i].value);
794 ast_unref(self->cases[i].code);
796 vec_free(self->cases);
798 ast_expression_delete((ast_expression*)self);
802 ast_call* ast_call_new(lex_ctx ctx,
803 ast_expression *funcexpr)
805 ast_instantiate(ast_call, ctx, ast_call_delete);
806 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_call_codegen);
808 ast_side_effects(self) = true;
811 self->func = funcexpr;
813 self->expression.vtype = funcexpr->expression.next->expression.vtype;
814 if (funcexpr->expression.next->expression.next)
815 self->expression.next = ast_type_copy(ctx, funcexpr->expression.next->expression.next);
820 void ast_call_delete(ast_call *self)
823 for (i = 0; i < vec_size(self->params); ++i)
824 ast_unref(self->params[i]);
825 vec_free(self->params);
828 ast_unref(self->func);
830 ast_expression_delete((ast_expression*)self);
834 bool ast_call_check_types(ast_call *self)
838 const ast_expression *func = self->func;
839 size_t count = vec_size(self->params);
840 if (count > vec_size(func->expression.params))
841 count = vec_size(func->expression.params);
843 for (i = 0; i < count; ++i) {
844 if (!ast_compare_type(self->params[i], (ast_expression*)(func->expression.params[i]))) {
847 ast_type_to_string(self->params[i], tgot, sizeof(tgot));
848 ast_type_to_string((ast_expression*)func->expression.params[i], texp, sizeof(texp));
849 asterror(ast_ctx(self), "invalid type for parameter %u in function call: expected %s, got %s",
850 (unsigned int)(i+1), texp, tgot);
851 /* we don't immediately return */
858 ast_store* ast_store_new(lex_ctx ctx, int op,
859 ast_expression *dest, ast_expression *source)
861 ast_instantiate(ast_store, ctx, ast_store_delete);
862 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_store_codegen);
864 ast_side_effects(self) = true;
868 self->source = source;
870 self->expression.vtype = dest->expression.vtype;
871 if (dest->expression.next) {
872 self->expression.next = ast_type_copy(ctx, dest);
873 if (!self->expression.next) {
879 self->expression.next = NULL;
884 void ast_store_delete(ast_store *self)
886 ast_unref(self->dest);
887 ast_unref(self->source);
888 ast_expression_delete((ast_expression*)self);
892 ast_block* ast_block_new(lex_ctx ctx)
894 ast_instantiate(ast_block, ctx, ast_block_delete);
895 ast_expression_init((ast_expression*)self,
896 (ast_expression_codegen*)&ast_block_codegen);
900 self->collect = NULL;
905 void ast_block_add_expr(ast_block *self, ast_expression *e)
907 ast_propagate_effects(self, e);
908 vec_push(self->exprs, e);
911 void ast_block_collect(ast_block *self, ast_expression *expr)
913 vec_push(self->collect, expr);
914 expr->expression.node.keep = true;
917 void ast_block_delete(ast_block *self)
920 for (i = 0; i < vec_size(self->exprs); ++i)
921 ast_unref(self->exprs[i]);
922 vec_free(self->exprs);
923 for (i = 0; i < vec_size(self->locals); ++i)
924 ast_delete(self->locals[i]);
925 vec_free(self->locals);
926 for (i = 0; i < vec_size(self->collect); ++i)
927 ast_delete(self->collect[i]);
928 vec_free(self->collect);
929 ast_expression_delete((ast_expression*)self);
933 bool ast_block_set_type(ast_block *self, ast_expression *from)
935 if (self->expression.next)
936 ast_delete(self->expression.next);
937 self->expression.vtype = from->expression.vtype;
938 if (from->expression.next) {
939 self->expression.next = ast_type_copy(self->expression.node.context, from->expression.next);
940 if (!self->expression.next)
944 self->expression.next = NULL;
948 ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype)
950 ast_instantiate(ast_function, ctx, ast_function_delete);
954 vtype->expression.vtype != TYPE_FUNCTION)
956 asterror(ast_ctx(self), "internal error: ast_function_new condition %i %i type=%i",
958 (int)vtype->hasvalue,
959 vtype->expression.vtype);
965 self->name = name ? util_strdup(name) : NULL;
968 self->labelcount = 0;
971 self->ir_func = NULL;
972 self->curblock = NULL;
974 self->breakblock = NULL;
975 self->continueblock = NULL;
977 vtype->hasvalue = true;
978 vtype->constval.vfunc = self;
983 void ast_function_delete(ast_function *self)
987 mem_d((void*)self->name);
989 /* ast_value_delete(self->vtype); */
990 self->vtype->hasvalue = false;
991 self->vtype->constval.vfunc = NULL;
992 /* We use unref - if it was stored in a global table it is supposed
993 * to be deleted from *there*
995 ast_unref(self->vtype);
997 for (i = 0; i < vec_size(self->blocks); ++i)
998 ast_delete(self->blocks[i]);
999 vec_free(self->blocks);
1003 const char* ast_function_label(ast_function *self, const char *prefix)
1009 if (!opts_dump && !opts_dumpfin)
1012 id = (self->labelcount++);
1013 len = strlen(prefix);
1015 from = self->labelbuf + sizeof(self->labelbuf)-1;
1018 unsigned int digit = id % 10;
1019 *from = digit + '0';
1022 memcpy(from - len, prefix, len);
1026 /*********************************************************************/
1028 * by convention you must never pass NULL to the 'ir_value **out'
1029 * parameter. If you really don't care about the output, pass a dummy.
1030 * But I can't imagine a pituation where the output is truly unnecessary.
1033 bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out)
1037 /* NOTE: This is the codegen for a variable used in an expression.
1038 * It is not the codegen to generate the value. For this purpose,
1039 * ast_local_codegen and ast_global_codegen are to be used before this
1040 * is executed. ast_function_codegen should take care of its locals,
1041 * and the ast-user should take care of ast_global_codegen to be used
1042 * on all the globals.
1045 char typename[1024];
1046 ast_type_to_string((ast_expression*)self, typename, sizeof(typename));
1047 asterror(ast_ctx(self), "ast_value used before generated %s %s", typename, self->name);
1054 bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
1058 if (self->hasvalue && self->expression.vtype == TYPE_FUNCTION)
1060 ir_function *func = ir_builder_create_function(ir, self->name, self->expression.next->expression.vtype);
1063 func->context = ast_ctx(self);
1064 func->value->context = ast_ctx(self);
1066 self->constval.vfunc->ir_func = func;
1067 self->ir_v = func->value;
1068 /* The function is filled later on ast_function_codegen... */
1072 if (isfield && self->expression.vtype == TYPE_FIELD) {
1073 ast_expression *fieldtype = self->expression.next;
1075 if (self->hasvalue) {
1076 asterror(ast_ctx(self), "TODO: constant field pointers with value");
1080 if (fieldtype->expression.vtype == TYPE_ARRAY) {
1085 ast_expression_common *elemtype;
1087 ast_value *array = (ast_value*)fieldtype;
1089 if (!ast_istype(fieldtype, ast_value)) {
1090 asterror(ast_ctx(self), "internal error: ast_value required");
1094 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1095 if (!array->expression.count || array->expression.count > opts_max_array_size)
1096 asterror(ast_ctx(self), "Invalid array of size %lu", (unsigned long)array->expression.count);
1098 elemtype = &array->expression.next->expression;
1099 vtype = elemtype->vtype;
1101 v = ir_builder_create_field(ir, self->name, vtype);
1103 asterror(ast_ctx(self), "ir_builder_create_global failed");
1106 if (vtype == TYPE_FIELD)
1107 v->fieldtype = elemtype->next->expression.vtype;
1108 v->context = ast_ctx(self);
1109 array->ir_v = self->ir_v = v;
1111 namelen = strlen(self->name);
1112 name = (char*)mem_a(namelen + 16);
1113 strcpy(name, self->name);
1115 array->ir_values = (ir_value**)mem_a(sizeof(array->ir_values[0]) * array->expression.count);
1116 array->ir_values[0] = v;
1117 for (ai = 1; ai < array->expression.count; ++ai) {
1118 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1119 array->ir_values[ai] = ir_builder_create_field(ir, name, vtype);
1120 if (!array->ir_values[ai]) {
1122 asterror(ast_ctx(self), "ir_builder_create_global failed");
1125 if (vtype == TYPE_FIELD)
1126 array->ir_values[ai]->fieldtype = elemtype->next->expression.vtype;
1127 array->ir_values[ai]->context = ast_ctx(self);
1133 v = ir_builder_create_field(ir, self->name, self->expression.next->expression.vtype);
1136 v->context = ast_ctx(self);
1142 if (self->expression.vtype == TYPE_ARRAY) {
1147 ast_expression_common *elemtype = &self->expression.next->expression;
1148 int vtype = elemtype->vtype;
1150 /* same as with field arrays */
1151 if (!self->expression.count || self->expression.count > opts_max_array_size)
1152 asterror(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1154 v = ir_builder_create_global(ir, self->name, vtype);
1156 asterror(ast_ctx(self), "ir_builder_create_global failed");
1159 if (vtype == TYPE_FIELD)
1160 v->fieldtype = elemtype->next->expression.vtype;
1161 v->context = ast_ctx(self);
1163 namelen = strlen(self->name);
1164 name = (char*)mem_a(namelen + 16);
1165 strcpy(name, self->name);
1167 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1168 self->ir_values[0] = v;
1169 for (ai = 1; ai < self->expression.count; ++ai) {
1170 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1171 self->ir_values[ai] = ir_builder_create_global(ir, name, vtype);
1172 if (!self->ir_values[ai]) {
1174 asterror(ast_ctx(self), "ir_builder_create_global failed");
1177 if (vtype == TYPE_FIELD)
1178 self->ir_values[ai]->fieldtype = elemtype->next->expression.vtype;
1179 self->ir_values[ai]->context = ast_ctx(self);
1185 /* Arrays don't do this since there's no "array" value which spans across the
1188 v = ir_builder_create_global(ir, self->name, self->expression.vtype);
1190 asterror(ast_ctx(self), "ir_builder_create_global failed");
1193 if (self->expression.vtype == TYPE_FIELD)
1194 v->fieldtype = self->expression.next->expression.vtype;
1195 v->context = ast_ctx(self);
1198 if (self->hasvalue) {
1199 switch (self->expression.vtype)
1202 if (!ir_value_set_float(v, self->constval.vfloat))
1206 if (!ir_value_set_vector(v, self->constval.vvec))
1210 if (!ir_value_set_string(v, self->constval.vstring))
1214 asterror(ast_ctx(self), "TODO: global constant array");
1217 asterror(ast_ctx(self), "global of type function not properly generated");
1219 /* Cannot generate an IR value for a function,
1220 * need a pointer pointing to a function rather.
1223 asterror(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1228 /* link us to the ir_value */
1232 error: /* clean up */
1237 bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
1240 if (self->hasvalue && self->expression.vtype == TYPE_FUNCTION)
1242 /* Do we allow local functions? I think not...
1243 * this is NOT a function pointer atm.
1248 if (self->expression.vtype == TYPE_ARRAY) {
1253 ast_expression_common *elemtype = &self->expression.next->expression;
1254 int vtype = elemtype->vtype;
1257 asterror(ast_ctx(self), "array-parameters are not supported");
1261 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1262 if (!self->expression.count || self->expression.count > opts_max_array_size) {
1263 asterror(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1266 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1267 if (!self->ir_values) {
1268 asterror(ast_ctx(self), "failed to allocate array values");
1272 v = ir_function_create_local(func, self->name, vtype, param);
1274 asterror(ast_ctx(self), "ir_function_create_local failed");
1277 if (vtype == TYPE_FIELD)
1278 v->fieldtype = elemtype->next->expression.vtype;
1279 v->context = ast_ctx(self);
1281 namelen = strlen(self->name);
1282 name = (char*)mem_a(namelen + 16);
1283 strcpy(name, self->name);
1285 self->ir_values[0] = v;
1286 for (ai = 1; ai < self->expression.count; ++ai) {
1287 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1288 self->ir_values[ai] = ir_function_create_local(func, name, vtype, param);
1289 if (!self->ir_values[ai]) {
1290 asterror(ast_ctx(self), "ir_builder_create_global failed");
1293 if (vtype == TYPE_FIELD)
1294 self->ir_values[ai]->fieldtype = elemtype->next->expression.vtype;
1295 self->ir_values[ai]->context = ast_ctx(self);
1300 v = ir_function_create_local(func, self->name, self->expression.vtype, param);
1303 if (self->expression.vtype == TYPE_FIELD)
1304 v->fieldtype = self->expression.next->expression.vtype;
1305 v->context = ast_ctx(self);
1308 /* A constant local... hmmm...
1309 * I suppose the IR will have to deal with this
1311 if (self->hasvalue) {
1312 switch (self->expression.vtype)
1315 if (!ir_value_set_float(v, self->constval.vfloat))
1319 if (!ir_value_set_vector(v, self->constval.vvec))
1323 if (!ir_value_set_string(v, self->constval.vstring))
1327 asterror(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1332 /* link us to the ir_value */
1336 if (!ast_global_codegen(self->setter, func->owner, false) ||
1337 !ast_function_codegen(self->setter->constval.vfunc, func->owner) ||
1338 !ir_function_finalize(self->setter->constval.vfunc->ir_func))
1342 if (!ast_global_codegen(self->getter, func->owner, false) ||
1343 !ast_function_codegen(self->getter->constval.vfunc, func->owner) ||
1344 !ir_function_finalize(self->getter->constval.vfunc->ir_func))
1349 error: /* clean up */
1354 bool ast_function_codegen(ast_function *self, ir_builder *ir)
1358 ast_expression_common *ec;
1363 irf = self->ir_func;
1365 asterror(ast_ctx(self), "ast_function's related ast_value was not generated yet");
1369 /* fill the parameter list */
1370 ec = &self->vtype->expression;
1371 for (i = 0; i < vec_size(ec->params); ++i)
1373 vec_push(irf->params, ec->params[i]->expression.vtype);
1374 if (!self->builtin) {
1375 if (!ast_local_codegen(ec->params[i], self->ir_func, true))
1380 if (self->builtin) {
1381 irf->builtin = self->builtin;
1385 if (!vec_size(self->blocks)) {
1386 asterror(ast_ctx(self), "function `%s` has no body", self->name);
1390 self->curblock = ir_function_create_block(irf, "entry");
1391 if (!self->curblock) {
1392 asterror(ast_ctx(self), "failed to allocate entry block for `%s`", self->name);
1396 for (i = 0; i < vec_size(self->blocks); ++i) {
1397 ast_expression_codegen *gen = self->blocks[i]->expression.codegen;
1398 if (!(*gen)((ast_expression*)self->blocks[i], self, false, &dummy))
1402 /* TODO: check return types */
1403 if (!self->curblock->is_return)
1405 return ir_block_create_return(self->curblock, NULL);
1406 /* From now on the parser has to handle this situation */
1408 if (!self->vtype->expression.next ||
1409 self->vtype->expression.next->expression.vtype == TYPE_VOID)
1411 return ir_block_create_return(self->curblock, NULL);
1415 /* error("missing return"); */
1416 asterror(ast_ctx(self), "function `%s` missing return value", self->name);
1424 /* Note, you will not see ast_block_codegen generate ir_blocks.
1425 * To the AST and the IR, blocks are 2 different things.
1426 * In the AST it represents a block of code, usually enclosed in
1427 * curly braces {...}.
1428 * While in the IR it represents a block in terms of control-flow.
1430 bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_value **out)
1434 /* We don't use this
1435 * Note: an ast-representation using the comma-operator
1436 * of the form: (a, b, c) = x should not assign to c...
1439 asterror(ast_ctx(self), "not an l-value (code-block)");
1443 if (self->expression.outr) {
1444 *out = self->expression.outr;
1448 /* output is NULL at first, we'll have each expression
1449 * assign to out output, thus, a comma-operator represention
1450 * using an ast_block will return the last generated value,
1451 * so: (b, c) + a executed both b and c, and returns c,
1452 * which is then added to a.
1456 /* generate locals */
1457 for (i = 0; i < vec_size(self->locals); ++i)
1459 if (!ast_local_codegen(self->locals[i], func->ir_func, false)) {
1461 asterror(ast_ctx(self), "failed to generate local `%s`", self->locals[i]->name);
1466 for (i = 0; i < vec_size(self->exprs); ++i)
1468 ast_expression_codegen *gen = self->exprs[i]->expression.codegen;
1469 if (func->curblock->final) {
1470 asterror(ast_ctx(self->exprs[i]), "unreachable statement");
1473 if (!(*gen)(self->exprs[i], func, false, out))
1477 self->expression.outr = *out;
1482 bool ast_store_codegen(ast_store *self, ast_function *func, bool lvalue, ir_value **out)
1484 ast_expression_codegen *cgen;
1485 ir_value *left = NULL;
1486 ir_value *right = NULL;
1490 ast_array_index *ai = NULL;
1492 if (lvalue && self->expression.outl) {
1493 *out = self->expression.outl;
1497 if (!lvalue && self->expression.outr) {
1498 *out = self->expression.outr;
1502 if (ast_istype(self->dest, ast_array_index))
1505 ai = (ast_array_index*)self->dest;
1506 idx = (ast_value*)ai->index;
1508 if (ast_istype(ai->index, ast_value) && idx->hasvalue)
1513 /* we need to call the setter */
1514 ir_value *iridx, *funval;
1518 asterror(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1522 arr = (ast_value*)ai->array;
1523 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1524 asterror(ast_ctx(self), "value has no setter (%s)", arr->name);
1528 cgen = idx->expression.codegen;
1529 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1532 cgen = arr->setter->expression.codegen;
1533 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
1536 cgen = self->source->expression.codegen;
1537 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1540 call = ir_block_create_call(func->curblock, ast_function_label(func, "store"), funval);
1543 ir_call_param(call, iridx);
1544 ir_call_param(call, right);
1545 self->expression.outr = right;
1551 cgen = self->dest->expression.codegen;
1553 if (!(*cgen)((ast_expression*)(self->dest), func, true, &left))
1555 self->expression.outl = left;
1557 cgen = self->source->expression.codegen;
1559 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1562 if (!ir_block_create_store_op(func->curblock, self->op, left, right))
1564 self->expression.outr = right;
1567 /* Theoretically, an assinment returns its left side as an
1568 * lvalue, if we don't need an lvalue though, we return
1569 * the right side as an rvalue, otherwise we have to
1570 * somehow know whether or not we need to dereference the pointer
1571 * on the left side - that is: OP_LOAD if it was an address.
1572 * Also: in original QC we cannot OP_LOADP *anyway*.
1574 *out = (lvalue ? left : right);
1579 bool ast_binary_codegen(ast_binary *self, ast_function *func, bool lvalue, ir_value **out)
1581 ast_expression_codegen *cgen;
1582 ir_value *left, *right;
1584 /* A binary operation cannot yield an l-value */
1586 asterror(ast_ctx(self), "not an l-value (binop)");
1590 if (self->expression.outr) {
1591 *out = self->expression.outr;
1595 if (OPTS_FLAG(SHORT_LOGIC) &&
1596 (self->op == INSTR_AND || self->op == INSTR_OR))
1598 /* short circuit evaluation */
1599 ir_block *other, *merge;
1600 ir_block *from_left, *from_right;
1605 /* Note about casting to true boolean values:
1606 * We use a single NOT for sub expressions, and an
1607 * overall NOT at the end, and for that purpose swap
1608 * all the jump conditions in order for the NOT to get
1610 * ie: (a && b) usually becomes (!!a ? !!b : !!a)
1611 * but we translate this to (!(!a ? !a : !b))
1614 merge_id = vec_size(func->ir_func->blocks);
1615 merge = ir_function_create_block(func->ir_func, ast_function_label(func, "sce_merge"));
1617 cgen = self->left->expression.codegen;
1618 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1620 if (!OPTS_FLAG(PERL_LOGIC)) {
1621 notop = type_not_instr[left->vtype];
1622 if (notop == AINSTR_END) {
1623 asterror(ast_ctx(self), "don't know how to cast to bool...");
1626 left = ir_block_create_unary(func->curblock,
1627 ast_function_label(func, "sce_not"),
1631 from_left = func->curblock;
1633 other = ir_function_create_block(func->ir_func, ast_function_label(func, "sce_other"));
1634 if ( !(self->op == INSTR_OR) != !OPTS_FLAG(PERL_LOGIC) ) {
1635 if (!ir_block_create_if(func->curblock, left, other, merge))
1638 if (!ir_block_create_if(func->curblock, left, merge, other))
1641 /* use the likely flag */
1642 vec_last(func->curblock->instr)->likely = true;
1644 func->curblock = other;
1645 cgen = self->right->expression.codegen;
1646 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1648 if (!OPTS_FLAG(PERL_LOGIC)) {
1649 notop = type_not_instr[right->vtype];
1650 if (notop == AINSTR_END) {
1651 asterror(ast_ctx(self), "don't know how to cast to bool...");
1654 right = ir_block_create_unary(func->curblock,
1655 ast_function_label(func, "sce_not"),
1659 from_right = func->curblock;
1661 if (!ir_block_create_jump(func->curblock, merge))
1664 vec_remove(func->ir_func->blocks, merge_id, 1);
1665 vec_push(func->ir_func->blocks, merge);
1667 func->curblock = merge;
1668 phi = ir_block_create_phi(func->curblock, ast_function_label(func, "sce_value"), TYPE_FLOAT);
1669 ir_phi_add(phi, from_left, left);
1670 ir_phi_add(phi, from_right, right);
1671 *out = ir_phi_value(phi);
1672 if (!OPTS_FLAG(PERL_LOGIC)) {
1673 notop = type_not_instr[(*out)->vtype];
1674 if (notop == AINSTR_END) {
1675 asterror(ast_ctx(self), "don't know how to cast to bool...");
1678 *out = ir_block_create_unary(func->curblock,
1679 ast_function_label(func, "sce_final_not"),
1685 self->expression.outr = *out;
1689 cgen = self->left->expression.codegen;
1690 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1693 cgen = self->right->expression.codegen;
1694 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1697 *out = ir_block_create_binop(func->curblock, ast_function_label(func, "bin"),
1698 self->op, left, right);
1701 self->expression.outr = *out;
1706 bool ast_binstore_codegen(ast_binstore *self, ast_function *func, bool lvalue, ir_value **out)
1708 ast_expression_codegen *cgen;
1709 ir_value *leftl = NULL, *leftr, *right, *bin;
1713 ast_array_index *ai = NULL;
1714 ir_value *iridx = NULL;
1716 if (lvalue && self->expression.outl) {
1717 *out = self->expression.outl;
1721 if (!lvalue && self->expression.outr) {
1722 *out = self->expression.outr;
1726 if (ast_istype(self->dest, ast_array_index))
1729 ai = (ast_array_index*)self->dest;
1730 idx = (ast_value*)ai->index;
1732 if (ast_istype(ai->index, ast_value) && idx->hasvalue)
1736 /* for a binstore we need both an lvalue and an rvalue for the left side */
1737 /* rvalue of destination! */
1739 cgen = idx->expression.codegen;
1740 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1743 cgen = self->dest->expression.codegen;
1744 if (!(*cgen)((ast_expression*)(self->dest), func, false, &leftr))
1747 /* source as rvalue only */
1748 cgen = self->source->expression.codegen;
1749 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1752 /* now the binary */
1753 bin = ir_block_create_binop(func->curblock, ast_function_label(func, "binst"),
1754 self->opbin, leftr, right);
1755 self->expression.outr = bin;
1759 /* we need to call the setter */
1764 asterror(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1768 arr = (ast_value*)ai->array;
1769 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1770 asterror(ast_ctx(self), "value has no setter (%s)", arr->name);
1774 cgen = arr->setter->expression.codegen;
1775 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
1778 call = ir_block_create_call(func->curblock, ast_function_label(func, "store"), funval);
1781 ir_call_param(call, iridx);
1782 ir_call_param(call, bin);
1783 self->expression.outr = bin;
1785 /* now store them */
1786 cgen = self->dest->expression.codegen;
1787 /* lvalue of destination */
1788 if (!(*cgen)((ast_expression*)(self->dest), func, true, &leftl))
1790 self->expression.outl = leftl;
1792 if (!ir_block_create_store_op(func->curblock, self->opstore, leftl, bin))
1794 self->expression.outr = bin;
1797 /* Theoretically, an assinment returns its left side as an
1798 * lvalue, if we don't need an lvalue though, we return
1799 * the right side as an rvalue, otherwise we have to
1800 * somehow know whether or not we need to dereference the pointer
1801 * on the left side - that is: OP_LOAD if it was an address.
1802 * Also: in original QC we cannot OP_LOADP *anyway*.
1804 *out = (lvalue ? leftl : bin);
1809 bool ast_unary_codegen(ast_unary *self, ast_function *func, bool lvalue, ir_value **out)
1811 ast_expression_codegen *cgen;
1814 /* An unary operation cannot yield an l-value */
1816 asterror(ast_ctx(self), "not an l-value (binop)");
1820 if (self->expression.outr) {
1821 *out = self->expression.outr;
1825 cgen = self->operand->expression.codegen;
1827 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
1830 *out = ir_block_create_unary(func->curblock, ast_function_label(func, "unary"),
1834 self->expression.outr = *out;
1839 bool ast_return_codegen(ast_return *self, ast_function *func, bool lvalue, ir_value **out)
1841 ast_expression_codegen *cgen;
1846 /* In the context of a return operation, we don't actually return
1850 asterror(ast_ctx(self), "return-expression is not an l-value");
1854 if (self->expression.outr) {
1855 asterror(ast_ctx(self), "internal error: ast_return cannot be reused, it bears no result!");
1858 self->expression.outr = (ir_value*)1;
1860 if (self->operand) {
1861 cgen = self->operand->expression.codegen;
1863 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
1866 if (!ir_block_create_return(func->curblock, operand))
1869 if (!ir_block_create_return(func->curblock, NULL))
1876 bool ast_entfield_codegen(ast_entfield *self, ast_function *func, bool lvalue, ir_value **out)
1878 ast_expression_codegen *cgen;
1879 ir_value *ent, *field;
1881 /* This function needs to take the 'lvalue' flag into account!
1882 * As lvalue we provide a field-pointer, as rvalue we provide the
1886 if (lvalue && self->expression.outl) {
1887 *out = self->expression.outl;
1891 if (!lvalue && self->expression.outr) {
1892 *out = self->expression.outr;
1896 cgen = self->entity->expression.codegen;
1897 if (!(*cgen)((ast_expression*)(self->entity), func, false, &ent))
1900 cgen = self->field->expression.codegen;
1901 if (!(*cgen)((ast_expression*)(self->field), func, false, &field))
1906 *out = ir_block_create_fieldaddress(func->curblock, ast_function_label(func, "efa"),
1909 *out = ir_block_create_load_from_ent(func->curblock, ast_function_label(func, "efv"),
1910 ent, field, self->expression.vtype);
1913 asterror(ast_ctx(self), "failed to create %s instruction (output type %s)",
1914 (lvalue ? "ADDRESS" : "FIELD"),
1915 type_name[self->expression.vtype]);
1920 self->expression.outl = *out;
1922 self->expression.outr = *out;
1924 /* Hm that should be it... */
1928 bool ast_member_codegen(ast_member *self, ast_function *func, bool lvalue, ir_value **out)
1930 ast_expression_codegen *cgen;
1933 /* in QC this is always an lvalue */
1935 if (self->expression.outl) {
1936 *out = self->expression.outl;
1940 cgen = self->owner->expression.codegen;
1941 if (!(*cgen)((ast_expression*)(self->owner), func, true, &vec))
1944 if (vec->vtype != TYPE_VECTOR &&
1945 !(vec->vtype == TYPE_FIELD && self->owner->expression.next->expression.vtype == TYPE_VECTOR))
1950 *out = ir_value_vector_member(vec, self->field);
1951 self->expression.outl = *out;
1953 return (*out != NULL);
1956 bool ast_array_index_codegen(ast_array_index *self, ast_function *func, bool lvalue, ir_value **out)
1961 if (!lvalue && self->expression.outr) {
1962 *out = self->expression.outr;
1964 if (lvalue && self->expression.outl) {
1965 *out = self->expression.outl;
1968 if (!ast_istype(self->array, ast_value)) {
1969 asterror(ast_ctx(self), "array indexing this way is not supported");
1970 /* note this would actually be pointer indexing because the left side is
1971 * not an actual array but (hopefully) an indexable expression.
1972 * Once we get integer arithmetic, and GADDRESS/GSTORE/GLOAD instruction
1973 * support this path will be filled.
1978 arr = (ast_value*)self->array;
1979 idx = (ast_value*)self->index;
1981 if (!ast_istype(self->index, ast_value) || !idx->hasvalue) {
1982 /* Time to use accessor functions */
1983 ast_expression_codegen *cgen;
1984 ir_value *iridx, *funval;
1988 asterror(ast_ctx(self), "(.2) array indexing here needs a compile-time constant");
1993 asterror(ast_ctx(self), "value has no getter, don't know how to index it");
1997 cgen = self->index->expression.codegen;
1998 if (!(*cgen)((ast_expression*)(self->index), func, true, &iridx))
2001 cgen = arr->getter->expression.codegen;
2002 if (!(*cgen)((ast_expression*)(arr->getter), func, true, &funval))
2005 call = ir_block_create_call(func->curblock, ast_function_label(func, "fetch"), funval);
2008 ir_call_param(call, iridx);
2010 *out = ir_call_value(call);
2011 self->expression.outr = *out;
2015 if (idx->expression.vtype == TYPE_FLOAT)
2016 *out = arr->ir_values[(int)idx->constval.vfloat];
2017 else if (idx->expression.vtype == TYPE_INTEGER)
2018 *out = arr->ir_values[idx->constval.vint];
2020 asterror(ast_ctx(self), "array indexing here needs an integer constant");
2026 bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_value **out)
2028 ast_expression_codegen *cgen;
2033 ir_block *cond = func->curblock;
2036 ir_block *ontrue_endblock = NULL;
2037 ir_block *onfalse_endblock = NULL;
2040 /* We don't output any value, thus also don't care about r/lvalue */
2044 if (self->expression.outr) {
2045 asterror(ast_ctx(self), "internal error: ast_ifthen cannot be reused, it bears no result!");
2048 self->expression.outr = (ir_value*)1;
2050 /* generate the condition */
2051 cgen = self->cond->expression.codegen;
2052 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2054 /* update the block which will get the jump - because short-logic or ternaries may have changed this */
2055 cond = func->curblock;
2059 if (self->on_true) {
2060 /* create on-true block */
2061 ontrue = ir_function_create_block(func->ir_func, ast_function_label(func, "ontrue"));
2065 /* enter the block */
2066 func->curblock = ontrue;
2069 cgen = self->on_true->expression.codegen;
2070 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &dummy))
2073 /* we now need to work from the current endpoint */
2074 ontrue_endblock = func->curblock;
2079 if (self->on_false) {
2080 /* create on-false block */
2081 onfalse = ir_function_create_block(func->ir_func, ast_function_label(func, "onfalse"));
2085 /* enter the block */
2086 func->curblock = onfalse;
2089 cgen = self->on_false->expression.codegen;
2090 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &dummy))
2093 /* we now need to work from the current endpoint */
2094 onfalse_endblock = func->curblock;
2098 /* Merge block were they all merge in to */
2099 merge = ir_function_create_block(func->ir_func, ast_function_label(func, "endif"));
2102 /* add jumps ot the merge block */
2103 if (ontrue && !ontrue_endblock->final && !ir_block_create_jump(ontrue_endblock, merge))
2105 if (onfalse && !onfalse_endblock->final && !ir_block_create_jump(onfalse_endblock, merge))
2108 /* we create the if here, that way all blocks are ordered :)
2110 if (!ir_block_create_if(cond, condval,
2111 (ontrue ? ontrue : merge),
2112 (onfalse ? onfalse : merge)))
2117 /* Now enter the merge block */
2118 func->curblock = merge;
2123 bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_value **out)
2125 ast_expression_codegen *cgen;
2128 ir_value *trueval, *falseval;
2131 ir_block *cond = func->curblock;
2136 /* Ternary can never create an lvalue... */
2140 /* In theory it shouldn't be possible to pass through a node twice, but
2141 * in case we add any kind of optimization pass for the AST itself, it
2142 * may still happen, thus we remember a created ir_value and simply return one
2143 * if it already exists.
2145 if (self->expression.outr) {
2146 *out = self->expression.outr;
2150 /* In the following, contraty to ast_ifthen, we assume both paths exist. */
2152 /* generate the condition */
2153 func->curblock = cond;
2154 cgen = self->cond->expression.codegen;
2155 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2158 /* create on-true block */
2159 ontrue = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_T"));
2164 /* enter the block */
2165 func->curblock = ontrue;
2168 cgen = self->on_true->expression.codegen;
2169 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &trueval))
2173 /* create on-false block */
2174 onfalse = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_F"));
2179 /* enter the block */
2180 func->curblock = onfalse;
2183 cgen = self->on_false->expression.codegen;
2184 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &falseval))
2188 /* create merge block */
2189 merge = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_out"));
2192 /* jump to merge block */
2193 if (!ir_block_create_jump(ontrue, merge))
2195 if (!ir_block_create_jump(onfalse, merge))
2198 /* create if instruction */
2199 if (!ir_block_create_if(cond, condval, ontrue, onfalse))
2202 /* Now enter the merge block */
2203 func->curblock = merge;
2205 /* Here, now, we need a PHI node
2206 * but first some sanity checking...
2208 if (trueval->vtype != falseval->vtype) {
2209 /* error("ternary with different types on the two sides"); */
2214 phi = ir_block_create_phi(merge, ast_function_label(func, "phi"), trueval->vtype);
2217 ir_phi_add(phi, ontrue, trueval);
2218 ir_phi_add(phi, onfalse, falseval);
2220 self->expression.outr = ir_phi_value(phi);
2221 *out = self->expression.outr;
2226 bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value **out)
2228 ast_expression_codegen *cgen;
2230 ir_value *dummy = NULL;
2231 ir_value *precond = NULL;
2232 ir_value *postcond = NULL;
2234 /* Since we insert some jumps "late" so we have blocks
2235 * ordered "nicely", we need to keep track of the actual end-blocks
2236 * of expressions to add the jumps to.
2238 ir_block *bbody = NULL, *end_bbody = NULL;
2239 ir_block *bprecond = NULL, *end_bprecond = NULL;
2240 ir_block *bpostcond = NULL, *end_bpostcond = NULL;
2241 ir_block *bincrement = NULL, *end_bincrement = NULL;
2242 ir_block *bout = NULL, *bin = NULL;
2244 /* let's at least move the outgoing block to the end */
2247 /* 'break' and 'continue' need to be able to find the right blocks */
2248 ir_block *bcontinue = NULL;
2249 ir_block *bbreak = NULL;
2251 ir_block *old_bcontinue = NULL;
2252 ir_block *old_bbreak = NULL;
2254 ir_block *tmpblock = NULL;
2259 if (self->expression.outr) {
2260 asterror(ast_ctx(self), "internal error: ast_loop cannot be reused, it bears no result!");
2263 self->expression.outr = (ir_value*)1;
2266 * Should we ever need some kind of block ordering, better make this function
2267 * move blocks around than write a block ordering algorithm later... after all
2268 * the ast and ir should work together, not against each other.
2271 /* initexpr doesn't get its own block, it's pointless, it could create more blocks
2272 * anyway if for example it contains a ternary.
2276 cgen = self->initexpr->expression.codegen;
2277 if (!(*cgen)((ast_expression*)(self->initexpr), func, false, &dummy))
2281 /* Store the block from which we enter this chaos */
2282 bin = func->curblock;
2284 /* The pre-loop condition needs its own block since we
2285 * need to be able to jump to the start of that expression.
2289 bprecond = ir_function_create_block(func->ir_func, ast_function_label(func, "pre_loop_cond"));
2293 /* the pre-loop-condition the least important place to 'continue' at */
2294 bcontinue = bprecond;
2297 func->curblock = bprecond;
2300 cgen = self->precond->expression.codegen;
2301 if (!(*cgen)((ast_expression*)(self->precond), func, false, &precond))
2304 end_bprecond = func->curblock;
2306 bprecond = end_bprecond = NULL;
2309 /* Now the next blocks won't be ordered nicely, but we need to
2310 * generate them this early for 'break' and 'continue'.
2312 if (self->increment) {
2313 bincrement = ir_function_create_block(func->ir_func, ast_function_label(func, "loop_increment"));
2316 bcontinue = bincrement; /* increment comes before the pre-loop-condition */
2318 bincrement = end_bincrement = NULL;
2321 if (self->postcond) {
2322 bpostcond = ir_function_create_block(func->ir_func, ast_function_label(func, "post_loop_cond"));
2325 bcontinue = bpostcond; /* postcond comes before the increment */
2327 bpostcond = end_bpostcond = NULL;
2330 bout_id = vec_size(func->ir_func->blocks);
2331 bout = ir_function_create_block(func->ir_func, ast_function_label(func, "after_loop"));
2336 /* The loop body... */
2339 bbody = ir_function_create_block(func->ir_func, ast_function_label(func, "loop_body"));
2344 func->curblock = bbody;
2346 old_bbreak = func->breakblock;
2347 old_bcontinue = func->continueblock;
2348 func->breakblock = bbreak;
2349 func->continueblock = bcontinue;
2352 cgen = self->body->expression.codegen;
2353 if (!(*cgen)((ast_expression*)(self->body), func, false, &dummy))
2356 end_bbody = func->curblock;
2357 func->breakblock = old_bbreak;
2358 func->continueblock = old_bcontinue;
2361 /* post-loop-condition */
2365 func->curblock = bpostcond;
2368 cgen = self->postcond->expression.codegen;
2369 if (!(*cgen)((ast_expression*)(self->postcond), func, false, &postcond))
2372 end_bpostcond = func->curblock;
2375 /* The incrementor */
2376 if (self->increment)
2379 func->curblock = bincrement;
2382 cgen = self->increment->expression.codegen;
2383 if (!(*cgen)((ast_expression*)(self->increment), func, false, &dummy))
2386 end_bincrement = func->curblock;
2389 /* In any case now, we continue from the outgoing block */
2390 func->curblock = bout;
2392 /* Now all blocks are in place */
2393 /* From 'bin' we jump to whatever comes first */
2394 if (bprecond) tmpblock = bprecond;
2395 else if (bbody) tmpblock = bbody;
2396 else if (bpostcond) tmpblock = bpostcond;
2397 else tmpblock = bout;
2398 if (!ir_block_create_jump(bin, tmpblock))
2404 ir_block *ontrue, *onfalse;
2405 if (bbody) ontrue = bbody;
2406 else if (bincrement) ontrue = bincrement;
2407 else if (bpostcond) ontrue = bpostcond;
2408 else ontrue = bprecond;
2410 if (!ir_block_create_if(end_bprecond, precond, ontrue, onfalse))
2417 if (bincrement) tmpblock = bincrement;
2418 else if (bpostcond) tmpblock = bpostcond;
2419 else if (bprecond) tmpblock = bprecond;
2420 else tmpblock = bout;
2421 if (!end_bbody->final && !ir_block_create_jump(end_bbody, tmpblock))
2425 /* from increment */
2428 if (bpostcond) tmpblock = bpostcond;
2429 else if (bprecond) tmpblock = bprecond;
2430 else if (bbody) tmpblock = bbody;
2431 else tmpblock = bout;
2432 if (!ir_block_create_jump(end_bincrement, tmpblock))
2439 ir_block *ontrue, *onfalse;
2440 if (bprecond) ontrue = bprecond;
2441 else if (bbody) ontrue = bbody;
2442 else if (bincrement) ontrue = bincrement;
2443 else ontrue = bpostcond;
2445 if (!ir_block_create_if(end_bpostcond, postcond, ontrue, onfalse))
2449 /* Move 'bout' to the end */
2450 vec_remove(func->ir_func->blocks, bout_id, 1);
2451 vec_push(func->ir_func->blocks, bout);
2456 bool ast_breakcont_codegen(ast_breakcont *self, ast_function *func, bool lvalue, ir_value **out)
2463 asterror(ast_ctx(self), "break/continue expression is not an l-value");
2467 if (self->expression.outr) {
2468 asterror(ast_ctx(self), "internal error: ast_breakcont cannot be reused!");
2471 self->expression.outr = (ir_value*)1;
2473 if (self->is_continue)
2474 target = func->continueblock;
2476 target = func->breakblock;
2478 if (!ir_block_create_jump(func->curblock, target))
2483 bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_value **out)
2485 ast_expression_codegen *cgen;
2487 ast_switch_case *def_case = NULL;
2488 ir_block *def_bfall = NULL;
2490 ir_value *dummy = NULL;
2491 ir_value *irop = NULL;
2492 ir_block *old_break = NULL;
2493 ir_block *bout = NULL;
2494 ir_block *bfall = NULL;
2502 asterror(ast_ctx(self), "switch expression is not an l-value");
2506 if (self->expression.outr) {
2507 asterror(ast_ctx(self), "internal error: ast_switch cannot be reused!");
2510 self->expression.outr = (ir_value*)1;
2515 cgen = self->operand->expression.codegen;
2516 if (!(*cgen)((ast_expression*)(self->operand), func, false, &irop))
2519 if (!vec_size(self->cases))
2522 cmpinstr = type_eq_instr[irop->vtype];
2523 if (cmpinstr >= AINSTR_END) {
2524 ast_type_to_string(self->operand, typestr, sizeof(typestr));
2525 asterror(ast_ctx(self), "invalid type to perform a switch on: %s", typestr);
2529 bout_id = vec_size(func->ir_func->blocks);
2530 bout = ir_function_create_block(func->ir_func, ast_function_label(func, "after_switch"));
2534 /* setup the break block */
2535 old_break = func->breakblock;
2536 func->breakblock = bout;
2538 /* Now create all cases */
2539 for (c = 0; c < vec_size(self->cases); ++c) {
2540 ir_value *cond, *val;
2541 ir_block *bcase, *bnot;
2544 ast_switch_case *swcase = &self->cases[c];
2546 if (swcase->value) {
2547 /* A regular case */
2548 /* generate the condition operand */
2549 cgen = swcase->value->expression.codegen;
2550 if (!(*cgen)((ast_expression*)(swcase->value), func, false, &val))
2552 /* generate the condition */
2553 cond = ir_block_create_binop(func->curblock, ast_function_label(func, "switch_eq"), cmpinstr, irop, val);
2557 bcase = ir_function_create_block(func->ir_func, ast_function_label(func, "case"));
2558 bnot_id = vec_size(func->ir_func->blocks);
2559 bnot = ir_function_create_block(func->ir_func, ast_function_label(func, "not_case"));
2560 if (!bcase || !bnot)
2562 if (!ir_block_create_if(func->curblock, cond, bcase, bnot))
2565 /* Make the previous case-end fall through */
2566 if (bfall && !bfall->final) {
2567 if (!ir_block_create_jump(bfall, bcase))
2571 /* enter the case */
2572 func->curblock = bcase;
2573 cgen = swcase->code->expression.codegen;
2574 if (!(*cgen)((ast_expression*)swcase->code, func, false, &dummy))
2577 /* remember this block to fall through from */
2578 bfall = func->curblock;
2580 /* enter the else and move it down */
2581 func->curblock = bnot;
2582 vec_remove(func->ir_func->blocks, bnot_id, 1);
2583 vec_push(func->ir_func->blocks, bnot);
2585 /* The default case */
2586 /* Remember where to fall through from: */
2589 /* remember which case it was */
2594 /* Jump from the last bnot to bout */
2595 if (bfall && !bfall->final && !ir_block_create_jump(bfall, bout)) {
2597 astwarning(ast_ctx(bfall), WARN_???, "missing break after last case");
2602 /* If there was a default case, put it down here */
2606 /* No need to create an extra block */
2607 bcase = func->curblock;
2609 /* Insert the fallthrough jump */
2610 if (def_bfall && !def_bfall->final) {
2611 if (!ir_block_create_jump(def_bfall, bcase))
2615 /* Now generate the default code */
2616 cgen = def_case->code->expression.codegen;
2617 if (!(*cgen)((ast_expression*)def_case->code, func, false, &dummy))
2621 /* Jump from the last bnot to bout */
2622 if (!func->curblock->final && !ir_block_create_jump(func->curblock, bout))
2624 /* enter the outgoing block */
2625 func->curblock = bout;
2627 /* restore the break block */
2628 func->breakblock = old_break;
2630 /* Move 'bout' to the end, it's nicer */
2631 vec_remove(func->ir_func->blocks, bout_id, 1);
2632 vec_push(func->ir_func->blocks, bout);
2637 bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value **out)
2639 ast_expression_codegen *cgen;
2641 ir_instr *callinstr;
2644 ir_value *funval = NULL;
2646 /* return values are never lvalues */
2648 asterror(ast_ctx(self), "not an l-value (function call)");
2652 if (self->expression.outr) {
2653 *out = self->expression.outr;
2657 cgen = self->func->expression.codegen;
2658 if (!(*cgen)((ast_expression*)(self->func), func, false, &funval))
2666 for (i = 0; i < vec_size(self->params); ++i)
2669 ast_expression *expr = self->params[i];
2671 cgen = expr->expression.codegen;
2672 if (!(*cgen)(expr, func, false, ¶m))
2676 vec_push(params, param);
2679 callinstr = ir_block_create_call(func->curblock, ast_function_label(func, "call"), funval);
2683 for (i = 0; i < vec_size(params); ++i) {
2684 ir_call_param(callinstr, params[i]);
2687 *out = ir_call_value(callinstr);
2688 self->expression.outr = *out;