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
39 static void asterror(lex_ctx ctx, const char *msg, ...)
43 con_cvprintmsg((void*)&ctx, LVL_ERROR, "error", msg, ap);
47 /* It must not be possible to get here. */
48 static GMQCC_NORETURN void _ast_node_destroy(ast_node *self)
50 con_err("ast node missing destroy()\n");
54 /* Initialize main ast node aprts */
55 static void ast_node_init(ast_node *self, lex_ctx ctx, int nodetype)
57 self->node.context = ctx;
58 self->node.destroy = &_ast_node_destroy;
59 self->node.keep = false;
60 self->node.nodetype = nodetype;
63 /* General expression initialization */
64 static void ast_expression_init(ast_expression *self,
65 ast_expression_codegen *codegen)
67 self->expression.codegen = codegen;
68 self->expression.vtype = TYPE_VOID;
69 self->expression.next = NULL;
70 self->expression.outl = NULL;
71 self->expression.outr = NULL;
72 self->expression.variadic = false;
73 self->expression.params = NULL;
76 static void ast_expression_delete(ast_expression *self)
79 if (self->expression.next)
80 ast_delete(self->expression.next);
81 for (i = 0; i < vec_size(self->expression.params); ++i) {
82 ast_delete(self->expression.params[i]);
84 vec_free(self->expression.params);
87 static void ast_expression_delete_full(ast_expression *self)
89 ast_expression_delete(self);
93 ast_value* ast_value_copy(const ast_value *self)
96 const ast_expression_common *fromex;
97 ast_expression_common *selfex;
98 ast_value *cp = ast_value_new(self->expression.node.context, self->name, self->expression.vtype);
99 if (self->expression.next) {
100 cp->expression.next = ast_type_copy(self->expression.node.context, self->expression.next);
101 if (!cp->expression.next) {
102 ast_value_delete(cp);
106 fromex = &self->expression;
107 selfex = &cp->expression;
108 selfex->variadic = fromex->variadic;
109 for (i = 0; i < vec_size(fromex->params); ++i) {
110 ast_value *v = ast_value_copy(fromex->params[i]);
112 ast_value_delete(cp);
115 vec_push(selfex->params, v);
120 bool ast_type_adopt_impl(ast_expression *self, const ast_expression *other)
123 const ast_expression_common *fromex;
124 ast_expression_common *selfex;
125 self->expression.vtype = other->expression.vtype;
126 if (other->expression.next) {
127 self->expression.next = (ast_expression*)ast_type_copy(ast_ctx(self), other->expression.next);
128 if (!self->expression.next)
131 fromex = &other->expression;
132 selfex = &self->expression;
133 selfex->variadic = fromex->variadic;
134 for (i = 0; i < vec_size(fromex->params); ++i) {
135 ast_value *v = ast_value_copy(fromex->params[i]);
138 vec_push(selfex->params, v);
143 static ast_expression* ast_shallow_type(lex_ctx ctx, int vtype)
145 ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
146 ast_expression_init(self, NULL);
147 self->expression.codegen = NULL;
148 self->expression.next = NULL;
149 self->expression.vtype = vtype;
153 ast_expression* ast_type_copy(lex_ctx ctx, const ast_expression *ex)
156 const ast_expression_common *fromex;
157 ast_expression_common *selfex;
163 ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
164 ast_expression_init(self, NULL);
166 fromex = &ex->expression;
167 selfex = &self->expression;
169 /* This may never be codegen()d */
170 selfex->codegen = NULL;
172 selfex->vtype = fromex->vtype;
175 selfex->next = ast_type_copy(ctx, fromex->next);
177 ast_expression_delete_full(self);
184 selfex->variadic = fromex->variadic;
185 for (i = 0; i < vec_size(fromex->params); ++i) {
186 ast_value *v = ast_value_copy(fromex->params[i]);
188 ast_expression_delete_full(self);
191 vec_push(selfex->params, v);
198 bool ast_compare_type(ast_expression *a, ast_expression *b)
200 if (a->expression.vtype != b->expression.vtype)
202 if (!a->expression.next != !b->expression.next)
204 if (vec_size(a->expression.params) != vec_size(b->expression.params))
206 if (a->expression.variadic != b->expression.variadic)
208 if (vec_size(a->expression.params)) {
210 for (i = 0; i < vec_size(a->expression.params); ++i) {
211 if (!ast_compare_type((ast_expression*)a->expression.params[i],
212 (ast_expression*)b->expression.params[i]))
216 if (a->expression.next)
217 return ast_compare_type(a->expression.next, b->expression.next);
221 static size_t ast_type_to_string_impl(ast_expression *e, char *buf, size_t bufsize, size_t pos)
228 if (pos + 6 >= bufsize)
230 strcpy(buf + pos, "(null)");
234 if (pos + 1 >= bufsize)
237 switch (e->expression.vtype) {
239 strcpy(buf + pos, "(variant)");
244 return ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
247 if (pos + 3 >= bufsize)
251 pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
252 if (pos + 1 >= bufsize)
258 pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
259 if (pos + 2 >= bufsize)
261 if (!vec_size(e->expression.params)) {
267 pos = ast_type_to_string_impl((ast_expression*)(e->expression.params[0]), buf, bufsize, pos);
268 for (i = 1; i < vec_size(e->expression.params); ++i) {
269 if (pos + 2 >= bufsize)
273 pos = ast_type_to_string_impl((ast_expression*)(e->expression.params[i]), buf, bufsize, pos);
275 if (pos + 1 >= bufsize)
281 pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
282 if (pos + 1 >= bufsize)
285 pos += snprintf(buf + pos, bufsize - pos - 1, "%i", (int)e->expression.count);
286 if (pos + 1 >= bufsize)
292 typestr = type_name[e->expression.vtype];
293 typelen = strlen(typestr);
294 if (pos + typelen >= bufsize)
296 strcpy(buf + pos, typestr);
297 return pos + typelen;
301 buf[bufsize-3] = '.';
302 buf[bufsize-2] = '.';
303 buf[bufsize-1] = '.';
307 void ast_type_to_string(ast_expression *e, char *buf, size_t bufsize)
309 size_t pos = ast_type_to_string_impl(e, buf, bufsize-1, 0);
313 ast_value* ast_value_new(lex_ctx ctx, const char *name, int t)
315 ast_instantiate(ast_value, ctx, ast_value_delete);
316 ast_expression_init((ast_expression*)self,
317 (ast_expression_codegen*)&ast_value_codegen);
318 self->expression.node.keep = true; /* keep */
320 self->name = name ? util_strdup(name) : NULL;
321 self->expression.vtype = t;
322 self->expression.next = NULL;
323 self->isconst = false;
325 memset(&self->constval, 0, sizeof(self->constval));
328 self->ir_values = NULL;
329 self->ir_value_count = 0;
337 void ast_value_delete(ast_value* self)
340 mem_d((void*)self->name);
342 switch (self->expression.vtype)
345 mem_d((void*)self->constval.vstring);
348 /* unlink us from the function node */
349 self->constval.vfunc->vtype = NULL;
351 /* NOTE: delete function? currently collected in
352 * the parser structure
359 mem_d(self->ir_values);
360 ast_expression_delete((ast_expression*)self);
364 void ast_value_params_add(ast_value *self, ast_value *p)
366 vec_push(self->expression.params, p);
369 bool ast_value_set_name(ast_value *self, const char *name)
372 mem_d((void*)self->name);
373 self->name = util_strdup(name);
377 ast_binary* ast_binary_new(lex_ctx ctx, int op,
378 ast_expression* left, ast_expression* right)
380 ast_instantiate(ast_binary, ctx, ast_binary_delete);
381 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binary_codegen);
387 if (op >= INSTR_EQ_F && op <= INSTR_GT)
388 self->expression.vtype = TYPE_FLOAT;
389 else if (op == INSTR_AND || op == INSTR_OR ||
390 op == INSTR_BITAND || op == INSTR_BITOR)
391 self->expression.vtype = TYPE_FLOAT;
392 else if (op == INSTR_MUL_VF || op == INSTR_MUL_FV)
393 self->expression.vtype = TYPE_VECTOR;
394 else if (op == INSTR_MUL_V)
395 self->expression.vtype = TYPE_FLOAT;
397 self->expression.vtype = left->expression.vtype;
402 void ast_binary_delete(ast_binary *self)
404 ast_unref(self->left);
405 ast_unref(self->right);
406 ast_expression_delete((ast_expression*)self);
410 ast_binstore* ast_binstore_new(lex_ctx ctx, int storop, int op,
411 ast_expression* left, ast_expression* right)
413 ast_instantiate(ast_binstore, ctx, ast_binstore_delete);
414 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binstore_codegen);
416 self->opstore = storop;
419 self->source = right;
421 self->expression.vtype = left->expression.vtype;
422 if (left->expression.next) {
423 self->expression.next = ast_type_copy(ctx, left);
424 if (!self->expression.next) {
430 self->expression.next = NULL;
435 void ast_binstore_delete(ast_binstore *self)
437 ast_unref(self->dest);
438 ast_unref(self->source);
439 ast_expression_delete((ast_expression*)self);
443 ast_unary* ast_unary_new(lex_ctx ctx, int op,
444 ast_expression *expr)
446 ast_instantiate(ast_unary, ctx, ast_unary_delete);
447 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_unary_codegen);
450 self->operand = expr;
452 if (op >= INSTR_NOT_F && op <= INSTR_NOT_FNC) {
453 self->expression.vtype = TYPE_FLOAT;
455 asterror(ctx, "cannot determine type of unary operation %s", asm_instr[op].m);
460 void ast_unary_delete(ast_unary *self)
462 ast_unref(self->operand);
463 ast_expression_delete((ast_expression*)self);
467 ast_return* ast_return_new(lex_ctx ctx, ast_expression *expr)
469 ast_instantiate(ast_return, ctx, ast_return_delete);
470 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_return_codegen);
472 self->operand = expr;
477 void ast_return_delete(ast_return *self)
480 ast_unref(self->operand);
481 ast_expression_delete((ast_expression*)self);
485 ast_entfield* ast_entfield_new(lex_ctx ctx, ast_expression *entity, ast_expression *field)
487 if (field->expression.vtype != TYPE_FIELD) {
488 asterror(ctx, "ast_entfield_new with expression not of type field");
491 return ast_entfield_new_force(ctx, entity, field, field->expression.next);
494 ast_entfield* ast_entfield_new_force(lex_ctx ctx, ast_expression *entity, ast_expression *field, const ast_expression *outtype)
496 ast_instantiate(ast_entfield, ctx, ast_entfield_delete);
500 /* Error: field has no type... */
504 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_entfield_codegen);
506 self->entity = entity;
509 if (!ast_type_adopt(self, outtype)) {
510 ast_entfield_delete(self);
517 void ast_entfield_delete(ast_entfield *self)
519 ast_unref(self->entity);
520 ast_unref(self->field);
521 ast_expression_delete((ast_expression*)self);
525 ast_member* ast_member_new(lex_ctx ctx, ast_expression *owner, unsigned int field)
527 ast_instantiate(ast_member, ctx, ast_member_delete);
533 if (owner->expression.vtype != TYPE_VECTOR &&
534 owner->expression.vtype != TYPE_FIELD) {
535 asterror(ctx, "member-access on an invalid owner of type %s", type_name[owner->expression.vtype]);
540 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_member_codegen);
541 self->expression.node.keep = true; /* keep */
543 if (owner->expression.vtype == TYPE_VECTOR) {
544 self->expression.vtype = TYPE_FLOAT;
545 self->expression.next = NULL;
547 self->expression.vtype = TYPE_FIELD;
548 self->expression.next = ast_shallow_type(ctx, TYPE_FLOAT);
557 void ast_member_delete(ast_member *self)
559 /* The owner is always an ast_value, which has .keep=true,
560 * also: ast_members are usually deleted after the owner, thus
561 * this will cause invalid access
562 ast_unref(self->owner);
563 * once we allow (expression).x to access a vector-member, we need
564 * to change this: preferably by creating an alternate ast node for this
565 * purpose that is not garbage-collected.
567 ast_expression_delete((ast_expression*)self);
571 ast_array_index* ast_array_index_new(lex_ctx ctx, ast_expression *array, ast_expression *index)
573 ast_expression *outtype;
574 ast_instantiate(ast_array_index, ctx, ast_array_index_delete);
576 outtype = array->expression.next;
579 /* Error: field has no type... */
583 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_array_index_codegen);
588 if (!ast_type_adopt(self, outtype)) {
589 ast_array_index_delete(self);
592 if (array->expression.vtype == TYPE_FIELD && outtype->expression.vtype == TYPE_ARRAY) {
593 if (self->expression.vtype != TYPE_ARRAY) {
594 asterror(ast_ctx(self), "array_index node on type");
595 ast_array_index_delete(self);
598 self->array = outtype;
599 self->expression.vtype = TYPE_FIELD;
605 void ast_array_index_delete(ast_array_index *self)
607 ast_unref(self->array);
608 ast_unref(self->index);
609 ast_expression_delete((ast_expression*)self);
613 ast_ifthen* ast_ifthen_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
615 ast_instantiate(ast_ifthen, ctx, ast_ifthen_delete);
616 if (!ontrue && !onfalse) {
617 /* because it is invalid */
621 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ifthen_codegen);
624 self->on_true = ontrue;
625 self->on_false = onfalse;
630 void ast_ifthen_delete(ast_ifthen *self)
632 ast_unref(self->cond);
634 ast_unref(self->on_true);
636 ast_unref(self->on_false);
637 ast_expression_delete((ast_expression*)self);
641 ast_ternary* ast_ternary_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
643 ast_instantiate(ast_ternary, ctx, ast_ternary_delete);
644 /* This time NEITHER must be NULL */
645 if (!ontrue || !onfalse) {
649 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ternary_codegen);
652 self->on_true = ontrue;
653 self->on_false = onfalse;
655 if (!ast_type_adopt(self, ontrue)) {
656 ast_ternary_delete(self);
663 void ast_ternary_delete(ast_ternary *self)
665 ast_unref(self->cond);
666 ast_unref(self->on_true);
667 ast_unref(self->on_false);
668 ast_expression_delete((ast_expression*)self);
672 ast_loop* ast_loop_new(lex_ctx ctx,
673 ast_expression *initexpr,
674 ast_expression *precond,
675 ast_expression *postcond,
676 ast_expression *increment,
677 ast_expression *body)
679 ast_instantiate(ast_loop, ctx, ast_loop_delete);
680 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_loop_codegen);
682 self->initexpr = initexpr;
683 self->precond = precond;
684 self->postcond = postcond;
685 self->increment = increment;
691 void ast_loop_delete(ast_loop *self)
694 ast_unref(self->initexpr);
696 ast_unref(self->precond);
698 ast_unref(self->postcond);
700 ast_unref(self->increment);
702 ast_unref(self->body);
703 ast_expression_delete((ast_expression*)self);
707 ast_breakcont* ast_breakcont_new(lex_ctx ctx, bool iscont)
709 ast_instantiate(ast_breakcont, ctx, ast_breakcont_delete);
710 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_breakcont_codegen);
712 self->is_continue = iscont;
717 void ast_breakcont_delete(ast_breakcont *self)
719 ast_expression_delete((ast_expression*)self);
723 ast_switch* ast_switch_new(lex_ctx ctx, ast_expression *op)
725 ast_instantiate(ast_switch, ctx, ast_switch_delete);
726 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_switch_codegen);
734 void ast_switch_delete(ast_switch *self)
737 ast_unref(self->operand);
739 for (i = 0; i < vec_size(self->cases); ++i) {
740 if (self->cases[i].value)
741 ast_unref(self->cases[i].value);
742 ast_unref(self->cases[i].code);
744 vec_free(self->cases);
746 ast_expression_delete((ast_expression*)self);
750 ast_call* ast_call_new(lex_ctx ctx,
751 ast_expression *funcexpr)
753 ast_instantiate(ast_call, ctx, ast_call_delete);
754 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_call_codegen);
757 self->func = funcexpr;
759 self->expression.vtype = funcexpr->expression.next->expression.vtype;
760 if (funcexpr->expression.next->expression.next)
761 self->expression.next = ast_type_copy(ctx, funcexpr->expression.next->expression.next);
766 void ast_call_delete(ast_call *self)
769 for (i = 0; i < vec_size(self->params); ++i)
770 ast_unref(self->params[i]);
771 vec_free(self->params);
774 ast_unref(self->func);
776 ast_expression_delete((ast_expression*)self);
780 bool ast_call_check_types(ast_call *self)
784 const ast_expression *func = self->func;
785 size_t count = vec_size(self->params);
786 if (count > vec_size(func->expression.params))
787 count = vec_size(func->expression.params);
789 for (i = 0; i < count; ++i) {
790 if (!ast_compare_type(self->params[i], (ast_expression*)(func->expression.params[i]))) {
793 ast_type_to_string(self->params[i], tgot, sizeof(tgot));
794 ast_type_to_string((ast_expression*)func->expression.params[i], texp, sizeof(texp));
795 asterror(ast_ctx(self), "invalid type for parameter %u in function call: expected %s, got %s",
796 (unsigned int)(i+1), texp, tgot);
797 /* we don't immediately return */
804 ast_store* ast_store_new(lex_ctx ctx, int op,
805 ast_expression *dest, ast_expression *source)
807 ast_instantiate(ast_store, ctx, ast_store_delete);
808 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_store_codegen);
812 self->source = source;
814 self->expression.vtype = dest->expression.vtype;
815 if (dest->expression.next) {
816 self->expression.next = ast_type_copy(ctx, dest);
817 if (!self->expression.next) {
823 self->expression.next = NULL;
828 void ast_store_delete(ast_store *self)
830 ast_unref(self->dest);
831 ast_unref(self->source);
832 ast_expression_delete((ast_expression*)self);
836 ast_block* ast_block_new(lex_ctx ctx)
838 ast_instantiate(ast_block, ctx, ast_block_delete);
839 ast_expression_init((ast_expression*)self,
840 (ast_expression_codegen*)&ast_block_codegen);
844 self->collect = NULL;
849 void ast_block_collect(ast_block *self, ast_expression *expr)
851 vec_push(self->collect, expr);
852 expr->expression.node.keep = true;
855 void ast_block_delete(ast_block *self)
858 for (i = 0; i < vec_size(self->exprs); ++i)
859 ast_unref(self->exprs[i]);
860 vec_free(self->exprs);
861 for (i = 0; i < vec_size(self->locals); ++i)
862 ast_delete(self->locals[i]);
863 vec_free(self->locals);
864 for (i = 0; i < vec_size(self->collect); ++i)
865 ast_delete(self->collect[i]);
866 vec_free(self->collect);
867 ast_expression_delete((ast_expression*)self);
871 bool ast_block_set_type(ast_block *self, ast_expression *from)
873 if (self->expression.next)
874 ast_delete(self->expression.next);
875 self->expression.vtype = from->expression.vtype;
876 if (from->expression.next) {
877 self->expression.next = ast_type_copy(self->expression.node.context, from->expression.next);
878 if (!self->expression.next)
882 self->expression.next = NULL;
886 ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype)
888 ast_instantiate(ast_function, ctx, ast_function_delete);
892 vtype->expression.vtype != TYPE_FUNCTION)
899 self->name = name ? util_strdup(name) : NULL;
902 self->labelcount = 0;
905 self->ir_func = NULL;
906 self->curblock = NULL;
908 self->breakblock = NULL;
909 self->continueblock = NULL;
911 vtype->isconst = true;
912 vtype->constval.vfunc = self;
917 void ast_function_delete(ast_function *self)
921 mem_d((void*)self->name);
923 /* ast_value_delete(self->vtype); */
924 self->vtype->isconst = false;
925 self->vtype->constval.vfunc = NULL;
926 /* We use unref - if it was stored in a global table it is supposed
927 * to be deleted from *there*
929 ast_unref(self->vtype);
931 for (i = 0; i < vec_size(self->blocks); ++i)
932 ast_delete(self->blocks[i]);
933 vec_free(self->blocks);
937 const char* ast_function_label(ast_function *self, const char *prefix)
943 if (!opts_dump && !opts_dumpfin)
946 id = (self->labelcount++);
947 len = strlen(prefix);
949 from = self->labelbuf + sizeof(self->labelbuf)-1;
952 unsigned int digit = id % 10;
956 memcpy(from - len, prefix, len);
960 /*********************************************************************/
962 * by convention you must never pass NULL to the 'ir_value **out'
963 * parameter. If you really don't care about the output, pass a dummy.
964 * But I can't imagine a pituation where the output is truly unnecessary.
967 bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out)
969 /* NOTE: This is the codegen for a variable used in an expression.
970 * It is not the codegen to generate the value. For this purpose,
971 * ast_local_codegen and ast_global_codegen are to be used before this
972 * is executed. ast_function_codegen should take care of its locals,
973 * and the ast-user should take care of ast_global_codegen to be used
974 * on all the globals.
978 ast_type_to_string((ast_expression*)self, typename, sizeof(typename));
979 asterror(ast_ctx(self), "ast_value used before generated %s %s", typename, self->name);
986 bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
990 if (self->isconst && self->expression.vtype == TYPE_FUNCTION)
992 ir_function *func = ir_builder_create_function(ir, self->name, self->expression.next->expression.vtype);
995 func->context = ast_ctx(self);
996 func->value->context = ast_ctx(self);
998 self->constval.vfunc->ir_func = func;
999 self->ir_v = func->value;
1000 /* The function is filled later on ast_function_codegen... */
1004 if (isfield && self->expression.vtype == TYPE_FIELD) {
1005 ast_expression *fieldtype = self->expression.next;
1007 if (self->isconst) {
1008 asterror(ast_ctx(self), "TODO: constant field pointers with value");
1012 if (fieldtype->expression.vtype == TYPE_ARRAY) {
1017 ast_expression_common *elemtype;
1019 ast_value *array = (ast_value*)fieldtype;
1021 if (!ast_istype(fieldtype, ast_value)) {
1022 asterror(ast_ctx(self), "internal error: ast_value required");
1026 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1027 if (!array->expression.count || array->expression.count > opts_max_array_size)
1028 asterror(ast_ctx(self), "Invalid array of size %lu", (unsigned long)array->expression.count);
1030 elemtype = &array->expression.next->expression;
1031 vtype = elemtype->vtype;
1033 v = ir_builder_create_field(ir, self->name, vtype);
1035 asterror(ast_ctx(self), "ir_builder_create_global failed");
1038 if (vtype == TYPE_FIELD)
1039 v->fieldtype = elemtype->next->expression.vtype;
1040 v->context = ast_ctx(self);
1041 array->ir_v = self->ir_v = v;
1043 namelen = strlen(self->name);
1044 name = (char*)mem_a(namelen + 16);
1045 strcpy(name, self->name);
1047 array->ir_values = (ir_value**)mem_a(sizeof(array->ir_values[0]) * array->expression.count);
1048 array->ir_values[0] = v;
1049 for (ai = 1; ai < array->expression.count; ++ai) {
1050 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1051 array->ir_values[ai] = ir_builder_create_field(ir, name, vtype);
1052 if (!array->ir_values[ai]) {
1054 asterror(ast_ctx(self), "ir_builder_create_global failed");
1057 if (vtype == TYPE_FIELD)
1058 array->ir_values[ai]->fieldtype = elemtype->next->expression.vtype;
1059 array->ir_values[ai]->context = ast_ctx(self);
1065 v = ir_builder_create_field(ir, self->name, self->expression.next->expression.vtype);
1068 v->context = ast_ctx(self);
1074 if (self->expression.vtype == TYPE_ARRAY) {
1079 ast_expression_common *elemtype = &self->expression.next->expression;
1080 int vtype = elemtype->vtype;
1082 /* same as with field arrays */
1083 if (!self->expression.count || self->expression.count > opts_max_array_size)
1084 asterror(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1086 v = ir_builder_create_global(ir, self->name, vtype);
1088 asterror(ast_ctx(self), "ir_builder_create_global failed");
1091 if (vtype == TYPE_FIELD)
1092 v->fieldtype = elemtype->next->expression.vtype;
1093 v->context = ast_ctx(self);
1095 namelen = strlen(self->name);
1096 name = (char*)mem_a(namelen + 16);
1097 strcpy(name, self->name);
1099 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1100 self->ir_values[0] = v;
1101 for (ai = 1; ai < self->expression.count; ++ai) {
1102 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1103 self->ir_values[ai] = ir_builder_create_global(ir, name, vtype);
1104 if (!self->ir_values[ai]) {
1106 asterror(ast_ctx(self), "ir_builder_create_global failed");
1109 if (vtype == TYPE_FIELD)
1110 self->ir_values[ai]->fieldtype = elemtype->next->expression.vtype;
1111 self->ir_values[ai]->context = ast_ctx(self);
1117 /* Arrays don't do this since there's no "array" value which spans across the
1120 v = ir_builder_create_global(ir, self->name, self->expression.vtype);
1122 asterror(ast_ctx(self), "ir_builder_create_global failed");
1125 if (self->expression.vtype == TYPE_FIELD)
1126 v->fieldtype = self->expression.next->expression.vtype;
1127 v->context = ast_ctx(self);
1130 if (self->isconst) {
1131 switch (self->expression.vtype)
1134 if (!ir_value_set_float(v, self->constval.vfloat))
1138 if (!ir_value_set_vector(v, self->constval.vvec))
1142 if (!ir_value_set_string(v, self->constval.vstring))
1146 asterror(ast_ctx(self), "TODO: global constant array");
1149 asterror(ast_ctx(self), "global of type function not properly generated");
1151 /* Cannot generate an IR value for a function,
1152 * need a pointer pointing to a function rather.
1155 asterror(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1160 /* link us to the ir_value */
1164 error: /* clean up */
1169 bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
1172 if (self->isconst && self->expression.vtype == TYPE_FUNCTION)
1174 /* Do we allow local functions? I think not...
1175 * this is NOT a function pointer atm.
1180 if (self->expression.vtype == TYPE_ARRAY) {
1185 ast_expression_common *elemtype = &self->expression.next->expression;
1186 int vtype = elemtype->vtype;
1189 asterror(ast_ctx(self), "array-parameters are not supported");
1193 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1194 if (!self->expression.count || self->expression.count > opts_max_array_size) {
1195 asterror(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1198 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1199 if (!self->ir_values) {
1200 asterror(ast_ctx(self), "failed to allocate array values");
1204 v = ir_function_create_local(func, self->name, vtype, param);
1206 asterror(ast_ctx(self), "ir_function_create_local failed");
1209 if (vtype == TYPE_FIELD)
1210 v->fieldtype = elemtype->next->expression.vtype;
1211 v->context = ast_ctx(self);
1213 namelen = strlen(self->name);
1214 name = (char*)mem_a(namelen + 16);
1215 strcpy(name, self->name);
1217 self->ir_values[0] = v;
1218 for (ai = 1; ai < self->expression.count; ++ai) {
1219 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1220 self->ir_values[ai] = ir_function_create_local(func, name, vtype, param);
1221 if (!self->ir_values[ai]) {
1222 asterror(ast_ctx(self), "ir_builder_create_global failed");
1225 if (vtype == TYPE_FIELD)
1226 self->ir_values[ai]->fieldtype = elemtype->next->expression.vtype;
1227 self->ir_values[ai]->context = ast_ctx(self);
1232 v = ir_function_create_local(func, self->name, self->expression.vtype, param);
1235 if (self->expression.vtype == TYPE_FIELD)
1236 v->fieldtype = self->expression.next->expression.vtype;
1237 v->context = ast_ctx(self);
1240 /* A constant local... hmmm...
1241 * I suppose the IR will have to deal with this
1243 if (self->isconst) {
1244 switch (self->expression.vtype)
1247 if (!ir_value_set_float(v, self->constval.vfloat))
1251 if (!ir_value_set_vector(v, self->constval.vvec))
1255 if (!ir_value_set_string(v, self->constval.vstring))
1259 asterror(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1264 /* link us to the ir_value */
1268 if (!ast_global_codegen(self->setter, func->owner, false) ||
1269 !ast_function_codegen(self->setter->constval.vfunc, func->owner) ||
1270 !ir_function_finalize(self->setter->constval.vfunc->ir_func))
1274 if (!ast_global_codegen(self->getter, func->owner, false) ||
1275 !ast_function_codegen(self->getter->constval.vfunc, func->owner) ||
1276 !ir_function_finalize(self->getter->constval.vfunc->ir_func))
1281 error: /* clean up */
1286 bool ast_function_codegen(ast_function *self, ir_builder *ir)
1290 ast_expression_common *ec;
1293 irf = self->ir_func;
1295 asterror(ast_ctx(self), "ast_function's related ast_value was not generated yet");
1299 /* fill the parameter list */
1300 ec = &self->vtype->expression;
1301 for (i = 0; i < vec_size(ec->params); ++i)
1303 vec_push(irf->params, ec->params[i]->expression.vtype);
1304 if (!self->builtin) {
1305 if (!ast_local_codegen(ec->params[i], self->ir_func, true))
1310 if (self->builtin) {
1311 irf->builtin = self->builtin;
1315 if (!vec_size(self->blocks)) {
1316 asterror(ast_ctx(self), "function `%s` has no body", self->name);
1320 self->curblock = ir_function_create_block(irf, "entry");
1321 if (!self->curblock) {
1322 asterror(ast_ctx(self), "failed to allocate entry block for `%s`", self->name);
1326 for (i = 0; i < vec_size(self->blocks); ++i) {
1327 ast_expression_codegen *gen = self->blocks[i]->expression.codegen;
1328 if (!(*gen)((ast_expression*)self->blocks[i], self, false, &dummy))
1332 /* TODO: check return types */
1333 if (!self->curblock->is_return)
1335 return ir_block_create_return(self->curblock, NULL);
1336 /* From now on the parser has to handle this situation */
1338 if (!self->vtype->expression.next ||
1339 self->vtype->expression.next->expression.vtype == TYPE_VOID)
1341 return ir_block_create_return(self->curblock, NULL);
1345 /* error("missing return"); */
1346 asterror(ast_ctx(self), "function `%s` missing return value", self->name);
1354 /* Note, you will not see ast_block_codegen generate ir_blocks.
1355 * To the AST and the IR, blocks are 2 different things.
1356 * In the AST it represents a block of code, usually enclosed in
1357 * curly braces {...}.
1358 * While in the IR it represents a block in terms of control-flow.
1360 bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_value **out)
1364 /* We don't use this
1365 * Note: an ast-representation using the comma-operator
1366 * of the form: (a, b, c) = x should not assign to c...
1369 asterror(ast_ctx(self), "not an l-value (code-block)");
1373 if (self->expression.outr) {
1374 *out = self->expression.outr;
1378 /* output is NULL at first, we'll have each expression
1379 * assign to out output, thus, a comma-operator represention
1380 * using an ast_block will return the last generated value,
1381 * so: (b, c) + a executed both b and c, and returns c,
1382 * which is then added to a.
1386 /* generate locals */
1387 for (i = 0; i < vec_size(self->locals); ++i)
1389 if (!ast_local_codegen(self->locals[i], func->ir_func, false)) {
1391 asterror(ast_ctx(self), "failed to generate local `%s`", self->locals[i]->name);
1396 for (i = 0; i < vec_size(self->exprs); ++i)
1398 ast_expression_codegen *gen = self->exprs[i]->expression.codegen;
1399 if (func->curblock->final) {
1400 asterror(ast_ctx(self->exprs[i]), "unreachable statement");
1403 if (!(*gen)(self->exprs[i], func, false, out))
1407 self->expression.outr = *out;
1412 bool ast_store_codegen(ast_store *self, ast_function *func, bool lvalue, ir_value **out)
1414 ast_expression_codegen *cgen;
1415 ir_value *left = NULL;
1416 ir_value *right = NULL;
1420 ast_array_index *ai = NULL;
1422 if (lvalue && self->expression.outl) {
1423 *out = self->expression.outl;
1427 if (!lvalue && self->expression.outr) {
1428 *out = self->expression.outr;
1432 if (ast_istype(self->dest, ast_array_index))
1435 ai = (ast_array_index*)self->dest;
1436 idx = (ast_value*)ai->index;
1438 if (ast_istype(ai->index, ast_value) && idx->isconst)
1443 /* we need to call the setter */
1444 ir_value *iridx, *funval;
1448 asterror(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1452 arr = (ast_value*)ai->array;
1453 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1454 asterror(ast_ctx(self), "value has no setter (%s)", arr->name);
1458 cgen = idx->expression.codegen;
1459 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1462 cgen = arr->setter->expression.codegen;
1463 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
1466 cgen = self->source->expression.codegen;
1467 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1470 call = ir_block_create_call(func->curblock, ast_function_label(func, "store"), funval);
1473 ir_call_param(call, iridx);
1474 ir_call_param(call, right);
1475 self->expression.outr = right;
1481 cgen = self->dest->expression.codegen;
1483 if (!(*cgen)((ast_expression*)(self->dest), func, true, &left))
1485 self->expression.outl = left;
1487 cgen = self->source->expression.codegen;
1489 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1492 if (!ir_block_create_store_op(func->curblock, self->op, left, right))
1494 self->expression.outr = right;
1497 /* Theoretically, an assinment returns its left side as an
1498 * lvalue, if we don't need an lvalue though, we return
1499 * the right side as an rvalue, otherwise we have to
1500 * somehow know whether or not we need to dereference the pointer
1501 * on the left side - that is: OP_LOAD if it was an address.
1502 * Also: in original QC we cannot OP_LOADP *anyway*.
1504 *out = (lvalue ? left : right);
1509 bool ast_binary_codegen(ast_binary *self, ast_function *func, bool lvalue, ir_value **out)
1511 ast_expression_codegen *cgen;
1512 ir_value *left, *right;
1514 /* A binary operation cannot yield an l-value */
1516 asterror(ast_ctx(self), "not an l-value (binop)");
1520 if (self->expression.outr) {
1521 *out = self->expression.outr;
1525 if (OPTS_FLAG(SHORT_LOGIC) &&
1526 (self->op == INSTR_AND || self->op == INSTR_OR))
1528 /* short circuit evaluation */
1529 ir_block *other, *merge;
1530 ir_block *from_left, *from_right;
1535 /* Note about casting to true boolean values:
1536 * We use a single NOT for sub expressions, and an
1537 * overall NOT at the end, and for that purpose swap
1538 * all the jump conditions in order for the NOT to get
1540 * ie: (a && b) usually becomes (!!a ? !!b : !!a)
1541 * but we translate this to (!(!a ? !a : !b))
1544 merge_id = vec_size(func->ir_func->blocks);
1545 merge = ir_function_create_block(func->ir_func, ast_function_label(func, "sce_merge"));
1547 cgen = self->left->expression.codegen;
1548 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1550 if (!OPTS_FLAG(PERL_LOGIC)) {
1551 notop = type_not_instr[left->vtype];
1552 if (notop == AINSTR_END) {
1553 asterror(ast_ctx(self), "don't know how to cast to bool...");
1556 left = ir_block_create_unary(func->curblock,
1557 ast_function_label(func, "sce_not"),
1561 from_left = func->curblock;
1563 other = ir_function_create_block(func->ir_func, ast_function_label(func, "sce_other"));
1564 if ( !(self->op == INSTR_OR) != !OPTS_FLAG(PERL_LOGIC) ) {
1565 if (!ir_block_create_if(func->curblock, left, other, merge))
1568 if (!ir_block_create_if(func->curblock, left, merge, other))
1571 /* use the likely flag */
1572 vec_last(func->curblock->instr)->likely = true;
1574 func->curblock = other;
1575 cgen = self->right->expression.codegen;
1576 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1578 if (!OPTS_FLAG(PERL_LOGIC)) {
1579 notop = type_not_instr[right->vtype];
1580 if (notop == AINSTR_END) {
1581 asterror(ast_ctx(self), "don't know how to cast to bool...");
1584 right = ir_block_create_unary(func->curblock,
1585 ast_function_label(func, "sce_not"),
1589 from_right = func->curblock;
1591 if (!ir_block_create_jump(func->curblock, merge))
1594 vec_remove(func->ir_func->blocks, merge_id, 1);
1595 vec_push(func->ir_func->blocks, merge);
1597 func->curblock = merge;
1598 phi = ir_block_create_phi(func->curblock, ast_function_label(func, "sce_value"), TYPE_FLOAT);
1599 ir_phi_add(phi, from_left, left);
1600 ir_phi_add(phi, from_right, right);
1601 *out = ir_phi_value(phi);
1602 if (!OPTS_FLAG(PERL_LOGIC)) {
1603 notop = type_not_instr[(*out)->vtype];
1604 if (notop == AINSTR_END) {
1605 asterror(ast_ctx(self), "don't know how to cast to bool...");
1608 *out = ir_block_create_unary(func->curblock,
1609 ast_function_label(func, "sce_final_not"),
1615 self->expression.outr = *out;
1619 cgen = self->left->expression.codegen;
1620 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1623 cgen = self->right->expression.codegen;
1624 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1627 *out = ir_block_create_binop(func->curblock, ast_function_label(func, "bin"),
1628 self->op, left, right);
1631 self->expression.outr = *out;
1636 bool ast_binstore_codegen(ast_binstore *self, ast_function *func, bool lvalue, ir_value **out)
1638 ast_expression_codegen *cgen;
1639 ir_value *leftl, *leftr, *right, *bin;
1641 if (lvalue && self->expression.outl) {
1642 *out = self->expression.outl;
1646 if (!lvalue && self->expression.outr) {
1647 *out = self->expression.outr;
1651 /* for a binstore we need both an lvalue and an rvalue for the left side */
1652 /* rvalue of destination! */
1653 cgen = self->dest->expression.codegen;
1654 if (!(*cgen)((ast_expression*)(self->dest), func, false, &leftr))
1657 /* source as rvalue only */
1658 cgen = self->source->expression.codegen;
1659 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1662 /* now the binary */
1663 bin = ir_block_create_binop(func->curblock, ast_function_label(func, "binst"),
1664 self->opbin, leftr, right);
1665 self->expression.outr = bin;
1667 /* now store them */
1668 cgen = self->dest->expression.codegen;
1669 /* lvalue of destination */
1670 if (!(*cgen)((ast_expression*)(self->dest), func, true, &leftl))
1672 self->expression.outl = leftl;
1674 if (!ir_block_create_store_op(func->curblock, self->opstore, leftl, bin))
1676 self->expression.outr = bin;
1678 /* Theoretically, an assinment returns its left side as an
1679 * lvalue, if we don't need an lvalue though, we return
1680 * the right side as an rvalue, otherwise we have to
1681 * somehow know whether or not we need to dereference the pointer
1682 * on the left side - that is: OP_LOAD if it was an address.
1683 * Also: in original QC we cannot OP_LOADP *anyway*.
1685 *out = (lvalue ? leftl : bin);
1690 bool ast_unary_codegen(ast_unary *self, ast_function *func, bool lvalue, ir_value **out)
1692 ast_expression_codegen *cgen;
1695 /* An unary operation cannot yield an l-value */
1697 asterror(ast_ctx(self), "not an l-value (binop)");
1701 if (self->expression.outr) {
1702 *out = self->expression.outr;
1706 cgen = self->operand->expression.codegen;
1708 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
1711 *out = ir_block_create_unary(func->curblock, ast_function_label(func, "unary"),
1715 self->expression.outr = *out;
1720 bool ast_return_codegen(ast_return *self, ast_function *func, bool lvalue, ir_value **out)
1722 ast_expression_codegen *cgen;
1725 /* In the context of a return operation, we don't actually return
1729 asterror(ast_ctx(self), "return-expression is not an l-value");
1733 if (self->expression.outr) {
1734 asterror(ast_ctx(self), "internal error: ast_return cannot be reused, it bears no result!");
1737 self->expression.outr = (ir_value*)1;
1739 if (self->operand) {
1740 cgen = self->operand->expression.codegen;
1742 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
1745 if (!ir_block_create_return(func->curblock, operand))
1748 if (!ir_block_create_return(func->curblock, NULL))
1755 bool ast_entfield_codegen(ast_entfield *self, ast_function *func, bool lvalue, ir_value **out)
1757 ast_expression_codegen *cgen;
1758 ir_value *ent, *field;
1760 /* This function needs to take the 'lvalue' flag into account!
1761 * As lvalue we provide a field-pointer, as rvalue we provide the
1765 if (lvalue && self->expression.outl) {
1766 *out = self->expression.outl;
1770 if (!lvalue && self->expression.outr) {
1771 *out = self->expression.outr;
1775 cgen = self->entity->expression.codegen;
1776 if (!(*cgen)((ast_expression*)(self->entity), func, false, &ent))
1779 cgen = self->field->expression.codegen;
1780 if (!(*cgen)((ast_expression*)(self->field), func, false, &field))
1785 *out = ir_block_create_fieldaddress(func->curblock, ast_function_label(func, "efa"),
1788 *out = ir_block_create_load_from_ent(func->curblock, ast_function_label(func, "efv"),
1789 ent, field, self->expression.vtype);
1792 asterror(ast_ctx(self), "failed to create %s instruction (output type %s)",
1793 (lvalue ? "ADDRESS" : "FIELD"),
1794 type_name[self->expression.vtype]);
1799 self->expression.outl = *out;
1801 self->expression.outr = *out;
1803 /* Hm that should be it... */
1807 bool ast_member_codegen(ast_member *self, ast_function *func, bool lvalue, ir_value **out)
1809 ast_expression_codegen *cgen;
1812 /* in QC this is always an lvalue */
1814 if (self->expression.outl) {
1815 *out = self->expression.outl;
1819 cgen = self->owner->expression.codegen;
1820 if (!(*cgen)((ast_expression*)(self->owner), func, true, &vec))
1823 if (vec->vtype != TYPE_VECTOR &&
1824 !(vec->vtype == TYPE_FIELD && self->owner->expression.next->expression.vtype == TYPE_VECTOR))
1829 *out = ir_value_vector_member(vec, self->field);
1830 self->expression.outl = *out;
1832 return (*out != NULL);
1835 bool ast_array_index_codegen(ast_array_index *self, ast_function *func, bool lvalue, ir_value **out)
1840 if (!lvalue && self->expression.outr) {
1841 *out = self->expression.outr;
1843 if (lvalue && self->expression.outl) {
1844 *out = self->expression.outl;
1847 if (!ast_istype(self->array, ast_value)) {
1848 asterror(ast_ctx(self), "array indexing this way is not supported");
1849 /* note this would actually be pointer indexing because the left side is
1850 * not an actual array but (hopefully) an indexable expression.
1851 * Once we get integer arithmetic, and GADDRESS/GSTORE/GLOAD instruction
1852 * support this path will be filled.
1857 arr = (ast_value*)self->array;
1858 idx = (ast_value*)self->index;
1860 if (!ast_istype(self->index, ast_value) || !idx->isconst) {
1861 /* Time to use accessor functions */
1862 ast_expression_codegen *cgen;
1863 ir_value *iridx, *funval;
1867 asterror(ast_ctx(self), "(.2) array indexing here needs a compile-time constant");
1872 asterror(ast_ctx(self), "value has no getter, don't know how to index it");
1876 cgen = self->index->expression.codegen;
1877 if (!(*cgen)((ast_expression*)(self->index), func, true, &iridx))
1880 cgen = arr->getter->expression.codegen;
1881 if (!(*cgen)((ast_expression*)(arr->getter), func, true, &funval))
1884 call = ir_block_create_call(func->curblock, ast_function_label(func, "fetch"), funval);
1887 ir_call_param(call, iridx);
1889 *out = ir_call_value(call);
1890 self->expression.outr = *out;
1894 if (idx->expression.vtype == TYPE_FLOAT)
1895 *out = arr->ir_values[(int)idx->constval.vfloat];
1896 else if (idx->expression.vtype == TYPE_INTEGER)
1897 *out = arr->ir_values[idx->constval.vint];
1899 asterror(ast_ctx(self), "array indexing here needs an integer constant");
1905 bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_value **out)
1907 ast_expression_codegen *cgen;
1912 ir_block *cond = func->curblock;
1915 ir_block *ontrue_endblock = NULL;
1916 ir_block *onfalse_endblock = NULL;
1919 /* We don't output any value, thus also don't care about r/lvalue */
1923 if (self->expression.outr) {
1924 asterror(ast_ctx(self), "internal error: ast_ifthen cannot be reused, it bears no result!");
1927 self->expression.outr = (ir_value*)1;
1929 /* generate the condition */
1930 cgen = self->cond->expression.codegen;
1931 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
1933 /* update the block which will get the jump - because short-logic or ternaries may have changed this */
1934 cond = func->curblock;
1938 if (self->on_true) {
1939 /* create on-true block */
1940 ontrue = ir_function_create_block(func->ir_func, ast_function_label(func, "ontrue"));
1944 /* enter the block */
1945 func->curblock = ontrue;
1948 cgen = self->on_true->expression.codegen;
1949 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &dummy))
1952 /* we now need to work from the current endpoint */
1953 ontrue_endblock = func->curblock;
1958 if (self->on_false) {
1959 /* create on-false block */
1960 onfalse = ir_function_create_block(func->ir_func, ast_function_label(func, "onfalse"));
1964 /* enter the block */
1965 func->curblock = onfalse;
1968 cgen = self->on_false->expression.codegen;
1969 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &dummy))
1972 /* we now need to work from the current endpoint */
1973 onfalse_endblock = func->curblock;
1977 /* Merge block were they all merge in to */
1978 merge = ir_function_create_block(func->ir_func, ast_function_label(func, "endif"));
1981 /* add jumps ot the merge block */
1982 if (ontrue && !ontrue_endblock->final && !ir_block_create_jump(ontrue_endblock, merge))
1984 if (onfalse && !onfalse_endblock->final && !ir_block_create_jump(onfalse_endblock, merge))
1987 /* we create the if here, that way all blocks are ordered :)
1989 if (!ir_block_create_if(cond, condval,
1990 (ontrue ? ontrue : merge),
1991 (onfalse ? onfalse : merge)))
1996 /* Now enter the merge block */
1997 func->curblock = merge;
2002 bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_value **out)
2004 ast_expression_codegen *cgen;
2007 ir_value *trueval, *falseval;
2010 ir_block *cond = func->curblock;
2015 /* Ternary can never create an lvalue... */
2019 /* In theory it shouldn't be possible to pass through a node twice, but
2020 * in case we add any kind of optimization pass for the AST itself, it
2021 * may still happen, thus we remember a created ir_value and simply return one
2022 * if it already exists.
2024 if (self->expression.outr) {
2025 *out = self->expression.outr;
2029 /* In the following, contraty to ast_ifthen, we assume both paths exist. */
2031 /* generate the condition */
2032 func->curblock = cond;
2033 cgen = self->cond->expression.codegen;
2034 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2037 /* create on-true block */
2038 ontrue = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_T"));
2043 /* enter the block */
2044 func->curblock = ontrue;
2047 cgen = self->on_true->expression.codegen;
2048 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &trueval))
2052 /* create on-false block */
2053 onfalse = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_F"));
2058 /* enter the block */
2059 func->curblock = onfalse;
2062 cgen = self->on_false->expression.codegen;
2063 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &falseval))
2067 /* create merge block */
2068 merge = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_out"));
2071 /* jump to merge block */
2072 if (!ir_block_create_jump(ontrue, merge))
2074 if (!ir_block_create_jump(onfalse, merge))
2077 /* create if instruction */
2078 if (!ir_block_create_if(cond, condval, ontrue, onfalse))
2081 /* Now enter the merge block */
2082 func->curblock = merge;
2084 /* Here, now, we need a PHI node
2085 * but first some sanity checking...
2087 if (trueval->vtype != falseval->vtype) {
2088 /* error("ternary with different types on the two sides"); */
2093 phi = ir_block_create_phi(merge, ast_function_label(func, "phi"), trueval->vtype);
2096 ir_phi_add(phi, ontrue, trueval);
2097 ir_phi_add(phi, onfalse, falseval);
2099 self->expression.outr = ir_phi_value(phi);
2100 *out = self->expression.outr;
2105 bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value **out)
2107 ast_expression_codegen *cgen;
2109 ir_value *dummy = NULL;
2110 ir_value *precond = NULL;
2111 ir_value *postcond = NULL;
2113 /* Since we insert some jumps "late" so we have blocks
2114 * ordered "nicely", we need to keep track of the actual end-blocks
2115 * of expressions to add the jumps to.
2117 ir_block *bbody = NULL, *end_bbody = NULL;
2118 ir_block *bprecond = NULL, *end_bprecond = NULL;
2119 ir_block *bpostcond = NULL, *end_bpostcond = NULL;
2120 ir_block *bincrement = NULL, *end_bincrement = NULL;
2121 ir_block *bout = NULL, *bin = NULL;
2123 /* let's at least move the outgoing block to the end */
2126 /* 'break' and 'continue' need to be able to find the right blocks */
2127 ir_block *bcontinue = NULL;
2128 ir_block *bbreak = NULL;
2130 ir_block *old_bcontinue = NULL;
2131 ir_block *old_bbreak = NULL;
2133 ir_block *tmpblock = NULL;
2138 if (self->expression.outr) {
2139 asterror(ast_ctx(self), "internal error: ast_loop cannot be reused, it bears no result!");
2142 self->expression.outr = (ir_value*)1;
2145 * Should we ever need some kind of block ordering, better make this function
2146 * move blocks around than write a block ordering algorithm later... after all
2147 * the ast and ir should work together, not against each other.
2150 /* initexpr doesn't get its own block, it's pointless, it could create more blocks
2151 * anyway if for example it contains a ternary.
2155 cgen = self->initexpr->expression.codegen;
2156 if (!(*cgen)((ast_expression*)(self->initexpr), func, false, &dummy))
2160 /* Store the block from which we enter this chaos */
2161 bin = func->curblock;
2163 /* The pre-loop condition needs its own block since we
2164 * need to be able to jump to the start of that expression.
2168 bprecond = ir_function_create_block(func->ir_func, ast_function_label(func, "pre_loop_cond"));
2172 /* the pre-loop-condition the least important place to 'continue' at */
2173 bcontinue = bprecond;
2176 func->curblock = bprecond;
2179 cgen = self->precond->expression.codegen;
2180 if (!(*cgen)((ast_expression*)(self->precond), func, false, &precond))
2183 end_bprecond = func->curblock;
2185 bprecond = end_bprecond = NULL;
2188 /* Now the next blocks won't be ordered nicely, but we need to
2189 * generate them this early for 'break' and 'continue'.
2191 if (self->increment) {
2192 bincrement = ir_function_create_block(func->ir_func, ast_function_label(func, "loop_increment"));
2195 bcontinue = bincrement; /* increment comes before the pre-loop-condition */
2197 bincrement = end_bincrement = NULL;
2200 if (self->postcond) {
2201 bpostcond = ir_function_create_block(func->ir_func, ast_function_label(func, "post_loop_cond"));
2204 bcontinue = bpostcond; /* postcond comes before the increment */
2206 bpostcond = end_bpostcond = NULL;
2209 bout_id = vec_size(func->ir_func->blocks);
2210 bout = ir_function_create_block(func->ir_func, ast_function_label(func, "after_loop"));
2215 /* The loop body... */
2218 bbody = ir_function_create_block(func->ir_func, ast_function_label(func, "loop_body"));
2223 func->curblock = bbody;
2225 old_bbreak = func->breakblock;
2226 old_bcontinue = func->continueblock;
2227 func->breakblock = bbreak;
2228 func->continueblock = bcontinue;
2231 cgen = self->body->expression.codegen;
2232 if (!(*cgen)((ast_expression*)(self->body), func, false, &dummy))
2235 end_bbody = func->curblock;
2236 func->breakblock = old_bbreak;
2237 func->continueblock = old_bcontinue;
2240 /* post-loop-condition */
2244 func->curblock = bpostcond;
2247 cgen = self->postcond->expression.codegen;
2248 if (!(*cgen)((ast_expression*)(self->postcond), func, false, &postcond))
2251 end_bpostcond = func->curblock;
2254 /* The incrementor */
2255 if (self->increment)
2258 func->curblock = bincrement;
2261 cgen = self->increment->expression.codegen;
2262 if (!(*cgen)((ast_expression*)(self->increment), func, false, &dummy))
2265 end_bincrement = func->curblock;
2268 /* In any case now, we continue from the outgoing block */
2269 func->curblock = bout;
2271 /* Now all blocks are in place */
2272 /* From 'bin' we jump to whatever comes first */
2273 if (bprecond) tmpblock = bprecond;
2274 else if (bbody) tmpblock = bbody;
2275 else if (bpostcond) tmpblock = bpostcond;
2276 else tmpblock = bout;
2277 if (!ir_block_create_jump(bin, tmpblock))
2283 ir_block *ontrue, *onfalse;
2284 if (bbody) ontrue = bbody;
2285 else if (bincrement) ontrue = bincrement;
2286 else if (bpostcond) ontrue = bpostcond;
2287 else ontrue = bprecond;
2289 if (!ir_block_create_if(end_bprecond, precond, ontrue, onfalse))
2296 if (bincrement) tmpblock = bincrement;
2297 else if (bpostcond) tmpblock = bpostcond;
2298 else if (bprecond) tmpblock = bprecond;
2299 else tmpblock = bout;
2300 if (!end_bbody->final && !ir_block_create_jump(end_bbody, tmpblock))
2304 /* from increment */
2307 if (bpostcond) tmpblock = bpostcond;
2308 else if (bprecond) tmpblock = bprecond;
2309 else if (bbody) tmpblock = bbody;
2310 else tmpblock = bout;
2311 if (!ir_block_create_jump(end_bincrement, tmpblock))
2318 ir_block *ontrue, *onfalse;
2319 if (bprecond) ontrue = bprecond;
2320 else if (bbody) ontrue = bbody;
2321 else if (bincrement) ontrue = bincrement;
2322 else ontrue = bpostcond;
2324 if (!ir_block_create_if(end_bpostcond, postcond, ontrue, onfalse))
2328 /* Move 'bout' to the end */
2329 vec_remove(func->ir_func->blocks, bout_id, 1);
2330 vec_push(func->ir_func->blocks, bout);
2335 bool ast_breakcont_codegen(ast_breakcont *self, ast_function *func, bool lvalue, ir_value **out)
2340 asterror(ast_ctx(self), "break/continue expression is not an l-value");
2344 if (self->expression.outr) {
2345 asterror(ast_ctx(self), "internal error: ast_breakcont cannot be reused!");
2348 self->expression.outr = (ir_value*)1;
2350 if (self->is_continue)
2351 target = func->continueblock;
2353 target = func->breakblock;
2355 if (!ir_block_create_jump(func->curblock, target))
2360 bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_value **out)
2362 ast_expression_codegen *cgen;
2364 ast_switch_case *def_case = NULL;
2365 ir_block *def_bfall = NULL;
2367 ir_value *dummy = NULL;
2368 ir_value *irop = NULL;
2369 ir_block *old_break = NULL;
2370 ir_block *bout = NULL;
2371 ir_block *bfall = NULL;
2379 asterror(ast_ctx(self), "switch expression is not an l-value");
2383 if (self->expression.outr) {
2384 asterror(ast_ctx(self), "internal error: ast_switch cannot be reused!");
2387 self->expression.outr = (ir_value*)1;
2392 cgen = self->operand->expression.codegen;
2393 if (!(*cgen)((ast_expression*)(self->operand), func, false, &irop))
2396 if (!vec_size(self->cases))
2399 cmpinstr = type_eq_instr[irop->vtype];
2400 if (cmpinstr >= AINSTR_END) {
2401 ast_type_to_string(self->operand, typestr, sizeof(typestr));
2402 asterror(ast_ctx(self), "invalid type to perform a switch on: %s", typestr);
2406 bout_id = vec_size(func->ir_func->blocks);
2407 bout = ir_function_create_block(func->ir_func, ast_function_label(func, "after_switch"));
2411 /* setup the break block */
2412 old_break = func->breakblock;
2413 func->breakblock = bout;
2415 /* Now create all cases */
2416 for (c = 0; c < vec_size(self->cases); ++c) {
2417 ir_value *cond, *val;
2418 ir_block *bcase, *bnot;
2421 ast_switch_case *swcase = &self->cases[c];
2423 if (swcase->value) {
2424 /* A regular case */
2425 /* generate the condition operand */
2426 cgen = swcase->value->expression.codegen;
2427 if (!(*cgen)((ast_expression*)(swcase->value), func, false, &val))
2429 /* generate the condition */
2430 cond = ir_block_create_binop(func->curblock, ast_function_label(func, "switch_eq"), cmpinstr, irop, val);
2434 bcase = ir_function_create_block(func->ir_func, ast_function_label(func, "case"));
2435 bnot_id = vec_size(func->ir_func->blocks);
2436 bnot = ir_function_create_block(func->ir_func, ast_function_label(func, "not_case"));
2437 if (!bcase || !bnot)
2439 if (!ir_block_create_if(func->curblock, cond, bcase, bnot))
2442 /* Make the previous case-end fall through */
2443 if (bfall && !bfall->final) {
2444 if (!ir_block_create_jump(bfall, bcase))
2448 /* enter the case */
2449 func->curblock = bcase;
2450 cgen = swcase->code->expression.codegen;
2451 if (!(*cgen)((ast_expression*)swcase->code, func, false, &dummy))
2454 /* remember this block to fall through from */
2455 bfall = func->curblock;
2457 /* enter the else and move it down */
2458 func->curblock = bnot;
2459 vec_remove(func->ir_func->blocks, bnot_id, 1);
2460 vec_push(func->ir_func->blocks, bnot);
2462 /* The default case */
2463 /* Remember where to fall through from: */
2466 /* remember which case it was */
2471 /* Jump from the last bnot to bout */
2472 if (bfall && !bfall->final && !ir_block_create_jump(bfall, bout)) {
2474 astwarning(ast_ctx(bfall), WARN_???, "missing break after last case");
2479 /* If there was a default case, put it down here */
2483 /* No need to create an extra block */
2484 bcase = func->curblock;
2486 /* Insert the fallthrough jump */
2487 if (def_bfall && !def_bfall->final) {
2488 if (!ir_block_create_jump(def_bfall, bcase))
2492 /* Now generate the default code */
2493 cgen = def_case->code->expression.codegen;
2494 if (!(*cgen)((ast_expression*)def_case->code, func, false, &dummy))
2498 /* Jump from the last bnot to bout */
2499 if (!func->curblock->final && !ir_block_create_jump(func->curblock, bout))
2501 /* enter the outgoing block */
2502 func->curblock = bout;
2504 /* restore the break block */
2505 func->breakblock = old_break;
2507 /* Move 'bout' to the end, it's nicer */
2508 vec_remove(func->ir_func->blocks, bout_id, 1);
2509 vec_push(func->ir_func->blocks, bout);
2514 bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value **out)
2516 ast_expression_codegen *cgen;
2518 ir_instr *callinstr;
2521 ir_value *funval = NULL;
2523 /* return values are never lvalues */
2525 asterror(ast_ctx(self), "not an l-value (function call)");
2529 if (self->expression.outr) {
2530 *out = self->expression.outr;
2534 cgen = self->func->expression.codegen;
2535 if (!(*cgen)((ast_expression*)(self->func), func, false, &funval))
2543 for (i = 0; i < vec_size(self->params); ++i)
2546 ast_expression *expr = self->params[i];
2548 cgen = expr->expression.codegen;
2549 if (!(*cgen)(expr, func, false, ¶m))
2553 vec_push(params, param);
2556 callinstr = ir_block_create_call(func->curblock, ast_function_label(func, "call"), funval);
2560 for (i = 0; i < vec_size(params); ++i) {
2561 ir_call_param(callinstr, params[i]);
2564 *out = ir_call_value(callinstr);
2565 self->expression.outr = *out;