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 cvprintmsg(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 fprintf(stderr, "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 MEM_VECTOR_INIT(&self->expression, params);
75 static void ast_expression_delete(ast_expression *self)
78 if (self->expression.next)
79 ast_delete(self->expression.next);
80 for (i = 0; i < self->expression.params_count; ++i) {
81 ast_delete(self->expression.params[i]);
83 MEM_VECTOR_CLEAR(&self->expression, params);
86 static void ast_expression_delete_full(ast_expression *self)
88 ast_expression_delete(self);
92 MEM_VEC_FUNCTIONS(ast_expression_common, ast_value*, params)
94 static ast_expression* ast_type_copy(lex_ctx ctx, const ast_expression *ex);
95 ast_value* ast_value_copy(const ast_value *self)
98 const ast_expression_common *fromex;
99 ast_expression_common *selfex;
100 ast_value *cp = ast_value_new(self->expression.node.context, self->name, self->expression.vtype);
101 if (self->expression.next) {
102 cp->expression.next = ast_type_copy(self->expression.node.context, self->expression.next);
103 if (!cp->expression.next) {
104 ast_value_delete(cp);
108 fromex = &self->expression;
109 selfex = &cp->expression;
110 for (i = 0; i < fromex->params_count; ++i) {
111 ast_value *v = ast_value_copy(fromex->params[i]);
112 if (!v || !ast_expression_common_params_add(selfex, v)) {
113 ast_value_delete(cp);
120 #define ast_type_adopt(a, b) ast_type_adopt_impl((ast_expression*)(a), (ast_expression*)(b))
121 static bool ast_type_adopt_impl(ast_expression *self, const ast_expression *other)
124 const ast_expression_common *fromex;
125 ast_expression_common *selfex;
126 self->expression.vtype = other->expression.vtype;
127 if (other->expression.next) {
128 self->expression.next = (ast_expression*)ast_type_copy(ast_ctx(self), other->expression.next);
129 if (!self->expression.next)
132 fromex = &other->expression;
133 selfex = &self->expression;
134 for (i = 0; i < fromex->params_count; ++i) {
135 ast_value *v = ast_value_copy(fromex->params[i]);
136 if (!v || !ast_expression_common_params_add(selfex, v))
142 static ast_expression* ast_shallow_type(lex_ctx ctx, int vtype)
144 ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
145 ast_expression_init(self, NULL);
146 self->expression.codegen = NULL;
147 self->expression.next = NULL;
148 self->expression.vtype = vtype;
152 static ast_expression* ast_type_copy(lex_ctx ctx, const ast_expression *ex)
155 const ast_expression_common *fromex;
156 ast_expression_common *selfex;
162 ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
163 ast_expression_init(self, NULL);
165 fromex = &ex->expression;
166 selfex = &self->expression;
168 /* This may never be codegen()d */
169 selfex->codegen = NULL;
171 selfex->vtype = fromex->vtype;
174 selfex->next = ast_type_copy(ctx, fromex->next);
176 ast_expression_delete_full(self);
183 for (i = 0; i < fromex->params_count; ++i) {
184 ast_value *v = ast_value_copy(fromex->params[i]);
185 if (!v || !ast_expression_common_params_add(selfex, v)) {
186 ast_expression_delete_full(self);
195 bool ast_compare_type(ast_expression *a, ast_expression *b)
197 if (a->expression.vtype != b->expression.vtype)
199 if (!a->expression.next != !b->expression.next)
201 if (a->expression.params_count != b->expression.params_count)
203 if (a->expression.params_count) {
205 for (i = 0; i < a->expression.params_count; ++i) {
206 if (!ast_compare_type((ast_expression*)a->expression.params[i],
207 (ast_expression*)b->expression.params[i]))
211 if (a->expression.next)
212 return ast_compare_type(a->expression.next, b->expression.next);
216 ast_value* ast_value_new(lex_ctx ctx, const char *name, int t)
218 ast_instantiate(ast_value, ctx, ast_value_delete);
219 ast_expression_init((ast_expression*)self,
220 (ast_expression_codegen*)&ast_value_codegen);
221 self->expression.node.keep = true; /* keep */
223 self->name = name ? util_strdup(name) : NULL;
224 self->expression.vtype = t;
225 self->expression.next = NULL;
226 self->isconst = false;
227 memset(&self->constval, 0, sizeof(self->constval));
234 void ast_value_delete(ast_value* self)
237 mem_d((void*)self->name);
239 switch (self->expression.vtype)
242 mem_d((void*)self->constval.vstring);
245 /* unlink us from the function node */
246 self->constval.vfunc->vtype = NULL;
248 /* NOTE: delete function? currently collected in
249 * the parser structure
255 ast_expression_delete((ast_expression*)self);
259 bool GMQCC_WARN ast_value_params_add(ast_value *self, ast_value *p)
261 return ast_expression_common_params_add(&self->expression, p);
264 bool ast_value_set_name(ast_value *self, const char *name)
267 mem_d((void*)self->name);
268 self->name = util_strdup(name);
272 ast_binary* ast_binary_new(lex_ctx ctx, int op,
273 ast_expression* left, ast_expression* right)
275 ast_instantiate(ast_binary, ctx, ast_binary_delete);
276 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binary_codegen);
282 if (op >= INSTR_EQ_F && op <= INSTR_GT)
283 self->expression.vtype = TYPE_FLOAT;
284 else if (op == INSTR_AND || op == INSTR_OR ||
285 op == INSTR_BITAND || op == INSTR_BITOR)
286 self->expression.vtype = TYPE_FLOAT;
287 else if (op == INSTR_MUL_VF || op == INSTR_MUL_FV)
288 self->expression.vtype = TYPE_VECTOR;
289 else if (op == INSTR_MUL_V)
290 self->expression.vtype = TYPE_FLOAT;
292 self->expression.vtype = left->expression.vtype;
297 void ast_binary_delete(ast_binary *self)
299 ast_unref(self->left);
300 ast_unref(self->right);
301 ast_expression_delete((ast_expression*)self);
305 ast_binstore* ast_binstore_new(lex_ctx ctx, int storop, int op,
306 ast_expression* left, ast_expression* right)
308 ast_instantiate(ast_binstore, ctx, ast_binstore_delete);
309 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binstore_codegen);
311 self->opstore = storop;
314 self->source = right;
316 self->expression.vtype = left->expression.vtype;
317 if (left->expression.next) {
318 self->expression.next = ast_type_copy(ctx, left);
319 if (!self->expression.next) {
325 self->expression.next = NULL;
330 void ast_binstore_delete(ast_binstore *self)
332 ast_unref(self->dest);
333 ast_unref(self->source);
334 ast_expression_delete((ast_expression*)self);
338 ast_unary* ast_unary_new(lex_ctx ctx, int op,
339 ast_expression *expr)
341 ast_instantiate(ast_unary, ctx, ast_unary_delete);
342 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_unary_codegen);
345 self->operand = expr;
347 if (op >= INSTR_NOT_F && op <= INSTR_NOT_FNC) {
348 self->expression.vtype = TYPE_FLOAT;
350 asterror(ctx, "cannot determine type of unary operation %s", asm_instr[op].m);
355 void ast_unary_delete(ast_unary *self)
357 ast_unref(self->operand);
358 ast_expression_delete((ast_expression*)self);
362 ast_return* ast_return_new(lex_ctx ctx, ast_expression *expr)
364 ast_instantiate(ast_return, ctx, ast_return_delete);
365 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_return_codegen);
367 self->operand = expr;
372 void ast_return_delete(ast_return *self)
375 ast_unref(self->operand);
376 ast_expression_delete((ast_expression*)self);
380 ast_entfield* ast_entfield_new(lex_ctx ctx, ast_expression *entity, ast_expression *field)
382 const ast_expression *outtype;
384 ast_instantiate(ast_entfield, ctx, ast_entfield_delete);
386 if (field->expression.vtype != TYPE_FIELD) {
391 outtype = field->expression.next;
394 /* Error: field has no type... */
398 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_entfield_codegen);
400 self->entity = entity;
403 if (!ast_type_adopt(self, outtype)) {
404 ast_entfield_delete(self);
411 void ast_entfield_delete(ast_entfield *self)
413 ast_unref(self->entity);
414 ast_unref(self->field);
415 ast_expression_delete((ast_expression*)self);
419 ast_member* ast_member_new(lex_ctx ctx, ast_expression *owner, unsigned int field)
421 ast_instantiate(ast_member, ctx, ast_member_delete);
427 if (owner->expression.vtype != TYPE_VECTOR &&
428 owner->expression.vtype != TYPE_FIELD) {
429 asterror(ctx, "member-access on an invalid owner of type %s\n", type_name[owner->expression.vtype]);
434 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_member_codegen);
435 self->expression.node.keep = true; /* keep */
437 if (owner->expression.vtype == TYPE_VECTOR) {
438 self->expression.vtype = TYPE_FLOAT;
439 self->expression.next = NULL;
441 self->expression.vtype = TYPE_FIELD;
442 self->expression.next = ast_shallow_type(ctx, TYPE_FLOAT);
451 void ast_member_delete(ast_member *self)
453 /* The owner is always an ast_value, which has .keep=true,
454 * also: ast_members are usually deleted after the owner, thus
455 * this will cause invalid access
456 ast_unref(self->owner);
457 * once we allow (expression).x to access a vector-member, we need
458 * to change this: preferably by creating an alternate ast node for this
459 * purpose that is not garbage-collected.
461 ast_expression_delete((ast_expression*)self);
465 ast_ifthen* ast_ifthen_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
467 ast_instantiate(ast_ifthen, ctx, ast_ifthen_delete);
468 if (!ontrue && !onfalse) {
469 /* because it is invalid */
473 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ifthen_codegen);
476 self->on_true = ontrue;
477 self->on_false = onfalse;
482 void ast_ifthen_delete(ast_ifthen *self)
484 ast_unref(self->cond);
486 ast_unref(self->on_true);
488 ast_unref(self->on_false);
489 ast_expression_delete((ast_expression*)self);
493 ast_ternary* ast_ternary_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
495 ast_instantiate(ast_ternary, ctx, ast_ternary_delete);
496 /* This time NEITHER must be NULL */
497 if (!ontrue || !onfalse) {
501 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ternary_codegen);
504 self->on_true = ontrue;
505 self->on_false = onfalse;
506 self->phi_out = NULL;
511 void ast_ternary_delete(ast_ternary *self)
513 ast_unref(self->cond);
514 ast_unref(self->on_true);
515 ast_unref(self->on_false);
516 ast_expression_delete((ast_expression*)self);
520 ast_loop* ast_loop_new(lex_ctx ctx,
521 ast_expression *initexpr,
522 ast_expression *precond,
523 ast_expression *postcond,
524 ast_expression *increment,
525 ast_expression *body)
527 ast_instantiate(ast_loop, ctx, ast_loop_delete);
528 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_loop_codegen);
530 self->initexpr = initexpr;
531 self->precond = precond;
532 self->postcond = postcond;
533 self->increment = increment;
539 void ast_loop_delete(ast_loop *self)
542 ast_unref(self->initexpr);
544 ast_unref(self->precond);
546 ast_unref(self->postcond);
548 ast_unref(self->increment);
550 ast_unref(self->body);
551 ast_expression_delete((ast_expression*)self);
555 ast_call* ast_call_new(lex_ctx ctx,
556 ast_expression *funcexpr)
558 ast_instantiate(ast_call, ctx, ast_call_delete);
559 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_call_codegen);
561 MEM_VECTOR_INIT(self, params);
563 self->func = funcexpr;
565 self->expression.vtype = funcexpr->expression.next->expression.vtype;
566 if (funcexpr->expression.next->expression.next)
567 self->expression.next = ast_type_copy(ctx, funcexpr->expression.next->expression.next);
571 MEM_VEC_FUNCTIONS(ast_call, ast_expression*, params)
573 void ast_call_delete(ast_call *self)
576 for (i = 0; i < self->params_count; ++i)
577 ast_unref(self->params[i]);
578 MEM_VECTOR_CLEAR(self, params);
581 ast_unref(self->func);
583 ast_expression_delete((ast_expression*)self);
587 ast_store* ast_store_new(lex_ctx ctx, int op,
588 ast_expression *dest, ast_expression *source)
590 ast_instantiate(ast_store, ctx, ast_store_delete);
591 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_store_codegen);
595 self->source = source;
600 void ast_store_delete(ast_store *self)
602 ast_unref(self->dest);
603 ast_unref(self->source);
604 ast_expression_delete((ast_expression*)self);
608 ast_block* ast_block_new(lex_ctx ctx)
610 ast_instantiate(ast_block, ctx, ast_block_delete);
611 ast_expression_init((ast_expression*)self,
612 (ast_expression_codegen*)&ast_block_codegen);
614 MEM_VECTOR_INIT(self, locals);
615 MEM_VECTOR_INIT(self, exprs);
616 MEM_VECTOR_INIT(self, collect);
620 MEM_VEC_FUNCTIONS(ast_block, ast_value*, locals)
621 MEM_VEC_FUNCTIONS(ast_block, ast_expression*, exprs)
622 MEM_VEC_FUNCTIONS(ast_block, ast_expression*, collect)
624 bool ast_block_collect(ast_block *self, ast_expression *expr)
626 if (!ast_block_collect_add(self, expr))
628 expr->expression.node.keep = true;
632 void ast_block_delete(ast_block *self)
635 for (i = 0; i < self->exprs_count; ++i)
636 ast_unref(self->exprs[i]);
637 MEM_VECTOR_CLEAR(self, exprs);
638 for (i = 0; i < self->locals_count; ++i)
639 ast_delete(self->locals[i]);
640 MEM_VECTOR_CLEAR(self, locals);
641 for (i = 0; i < self->collect_count; ++i)
642 ast_delete(self->collect[i]);
643 MEM_VECTOR_CLEAR(self, collect);
644 ast_expression_delete((ast_expression*)self);
648 bool ast_block_set_type(ast_block *self, ast_expression *from)
650 if (self->expression.next)
651 ast_delete(self->expression.next);
652 self->expression.vtype = from->expression.vtype;
653 if (from->expression.next) {
654 self->expression.next = ast_type_copy(self->expression.node.context, from->expression.next);
655 if (!self->expression.next)
659 self->expression.next = NULL;
663 ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype)
665 ast_instantiate(ast_function, ctx, ast_function_delete);
669 vtype->expression.vtype != TYPE_FUNCTION)
676 self->name = name ? util_strdup(name) : NULL;
677 MEM_VECTOR_INIT(self, blocks);
679 self->labelcount = 0;
682 self->ir_func = NULL;
683 self->curblock = NULL;
685 self->breakblock = NULL;
686 self->continueblock = NULL;
688 vtype->isconst = true;
689 vtype->constval.vfunc = self;
694 MEM_VEC_FUNCTIONS(ast_function, ast_block*, blocks)
696 void ast_function_delete(ast_function *self)
700 mem_d((void*)self->name);
702 /* ast_value_delete(self->vtype); */
703 self->vtype->isconst = false;
704 self->vtype->constval.vfunc = NULL;
705 /* We use unref - if it was stored in a global table it is supposed
706 * to be deleted from *there*
708 ast_unref(self->vtype);
710 for (i = 0; i < self->blocks_count; ++i)
711 ast_delete(self->blocks[i]);
712 MEM_VECTOR_CLEAR(self, blocks);
716 static void ast_util_hexitoa(char *buf, size_t size, unsigned int num)
718 unsigned int base = 10;
719 #define checknul() do { if (size == 1) { *buf = 0; return; } } while (0)
720 #define addch(x) do { *buf++ = (x); --size; checknul(); } while (0)
729 int digit = num % base;
740 const char* ast_function_label(ast_function *self, const char *prefix)
742 size_t id = (self->labelcount++);
743 size_t len = strlen(prefix);
744 strncpy(self->labelbuf, prefix, sizeof(self->labelbuf));
745 ast_util_hexitoa(self->labelbuf + len, sizeof(self->labelbuf)-len, id);
746 return self->labelbuf;
749 /*********************************************************************/
751 * by convention you must never pass NULL to the 'ir_value **out'
752 * parameter. If you really don't care about the output, pass a dummy.
753 * But I can't imagine a pituation where the output is truly unnecessary.
756 bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out)
758 /* NOTE: This is the codegen for a variable used in an expression.
759 * It is not the codegen to generate the value. For this purpose,
760 * ast_local_codegen and ast_global_codegen are to be used before this
761 * is executed. ast_function_codegen should take care of its locals,
762 * and the ast-user should take care of ast_global_codegen to be used
763 * on all the globals.
766 asterror(ast_ctx(self), "ast_value used before generated (%s)\n", self->name);
773 bool ast_global_codegen(ast_value *self, ir_builder *ir)
776 if (self->isconst && self->expression.vtype == TYPE_FUNCTION)
778 ir_function *func = ir_builder_create_function(ir, self->name, self->expression.next->expression.vtype);
782 self->constval.vfunc->ir_func = func;
783 self->ir_v = func->value;
784 /* The function is filled later on ast_function_codegen... */
788 if (self->expression.vtype == TYPE_FIELD) {
789 v = ir_builder_create_field(ir, self->name, self->expression.next->expression.vtype);
793 asterror(ast_ctx(self), "TODO: constant field pointers with value\n");
800 v = ir_builder_create_global(ir, self->name, self->expression.vtype);
802 asterror(ast_ctx(self), "ir_builder_create_global failed\n");
807 switch (self->expression.vtype)
810 if (!ir_value_set_float(v, self->constval.vfloat))
814 if (!ir_value_set_vector(v, self->constval.vvec))
818 if (!ir_value_set_string(v, self->constval.vstring))
822 asterror(ast_ctx(self), "global of type function not properly generated\n");
824 /* Cannot generate an IR value for a function,
825 * need a pointer pointing to a function rather.
828 asterror(ast_ctx(self), "TODO: global constant type %i\n", self->expression.vtype);
833 /* link us to the ir_value */
837 error: /* clean up */
842 bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
845 if (self->isconst && self->expression.vtype == TYPE_FUNCTION)
847 /* Do we allow local functions? I think not...
848 * this is NOT a function pointer atm.
853 v = ir_function_create_local(func, self->name, self->expression.vtype, param);
857 /* A constant local... hmmm...
858 * I suppose the IR will have to deal with this
861 switch (self->expression.vtype)
864 if (!ir_value_set_float(v, self->constval.vfloat))
868 if (!ir_value_set_vector(v, self->constval.vvec))
872 if (!ir_value_set_string(v, self->constval.vstring))
876 asterror(ast_ctx(self), "TODO: global constant type %i\n", self->expression.vtype);
881 /* link us to the ir_value */
885 error: /* clean up */
890 bool ast_function_codegen(ast_function *self, ir_builder *ir)
894 ast_expression_common *ec;
899 asterror(ast_ctx(self), "ast_function's related ast_value was not generated yet\n");
903 /* fill the parameter list */
904 ec = &self->vtype->expression;
905 for (i = 0; i < ec->params_count; ++i)
907 if (!ir_function_params_add(irf, ec->params[i]->expression.vtype))
909 if (!self->builtin) {
910 if (!ast_local_codegen(ec->params[i], self->ir_func, true))
916 irf->builtin = self->builtin;
920 if (!self->blocks_count) {
921 asterror(ast_ctx(self), "function `%s` has no body", self->name);
925 self->curblock = ir_function_create_block(irf, "entry");
929 for (i = 0; i < self->blocks_count; ++i) {
930 ast_expression_codegen *gen = self->blocks[i]->expression.codegen;
931 if (!(*gen)((ast_expression*)self->blocks[i], self, false, &dummy))
935 /* TODO: check return types */
936 if (!self->curblock->is_return)
938 if (!self->vtype->expression.next ||
939 self->vtype->expression.next->expression.vtype == TYPE_VOID)
941 return ir_block_create_return(self->curblock, NULL);
945 /* error("missing return"); */
946 asterror(ast_ctx(self), "function `%s` missing return value", self->name);
953 /* Note, you will not see ast_block_codegen generate ir_blocks.
954 * To the AST and the IR, blocks are 2 different things.
955 * In the AST it represents a block of code, usually enclosed in
956 * curly braces {...}.
957 * While in the IR it represents a block in terms of control-flow.
959 bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_value **out)
964 * Note: an ast-representation using the comma-operator
965 * of the form: (a, b, c) = x should not assign to c...
968 if (self->expression.outr) {
969 *out = self->expression.outr;
973 /* output is NULL at first, we'll have each expression
974 * assign to out output, thus, a comma-operator represention
975 * using an ast_block will return the last generated value,
976 * so: (b, c) + a executed both b and c, and returns c,
977 * which is then added to a.
981 /* generate locals */
982 for (i = 0; i < self->locals_count; ++i)
984 if (!ast_local_codegen(self->locals[i], func->ir_func, false))
988 for (i = 0; i < self->exprs_count; ++i)
990 ast_expression_codegen *gen = self->exprs[i]->expression.codegen;
991 if (!(*gen)(self->exprs[i], func, false, out))
995 self->expression.outr = *out;
1000 bool ast_store_codegen(ast_store *self, ast_function *func, bool lvalue, ir_value **out)
1002 ast_expression_codegen *cgen;
1003 ir_value *left, *right;
1005 if (lvalue && self->expression.outl) {
1006 *out = self->expression.outl;
1010 if (!lvalue && self->expression.outr) {
1011 *out = self->expression.outr;
1015 cgen = self->dest->expression.codegen;
1017 if (!(*cgen)((ast_expression*)(self->dest), func, true, &left))
1019 self->expression.outl = left;
1021 cgen = self->source->expression.codegen;
1023 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1026 if (!ir_block_create_store_op(func->curblock, self->op, left, right))
1028 self->expression.outr = right;
1030 /* Theoretically, an assinment returns its left side as an
1031 * lvalue, if we don't need an lvalue though, we return
1032 * the right side as an rvalue, otherwise we have to
1033 * somehow know whether or not we need to dereference the pointer
1034 * on the left side - that is: OP_LOAD if it was an address.
1035 * Also: in original QC we cannot OP_LOADP *anyway*.
1037 *out = (lvalue ? left : right);
1042 bool ast_binary_codegen(ast_binary *self, ast_function *func, bool lvalue, ir_value **out)
1044 ast_expression_codegen *cgen;
1045 ir_value *left, *right;
1047 /* In the context of a binary operation, we can disregard
1051 if (self->expression.outr) {
1052 *out = self->expression.outr;
1056 cgen = self->left->expression.codegen;
1058 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1061 cgen = self->right->expression.codegen;
1063 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1066 *out = ir_block_create_binop(func->curblock, ast_function_label(func, "bin"),
1067 self->op, left, right);
1070 self->expression.outr = *out;
1075 bool ast_binstore_codegen(ast_binstore *self, ast_function *func, bool lvalue, ir_value **out)
1077 ast_expression_codegen *cgen;
1078 ir_value *leftl, *leftr, *right, *bin;
1080 if (lvalue && self->expression.outl) {
1081 *out = self->expression.outl;
1085 if (!lvalue && self->expression.outr) {
1086 *out = self->expression.outr;
1090 /* for a binstore we need both an lvalue and an rvalue for the left side */
1091 /* rvalue of destination! */
1092 cgen = self->dest->expression.codegen;
1093 if (!(*cgen)((ast_expression*)(self->dest), func, false, &leftr))
1096 /* source as rvalue only */
1097 cgen = self->source->expression.codegen;
1098 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1101 /* now the binary */
1102 bin = ir_block_create_binop(func->curblock, ast_function_label(func, "binst"),
1103 self->opbin, leftr, right);
1104 self->expression.outr = bin;
1106 /* now store them */
1107 cgen = self->dest->expression.codegen;
1108 /* lvalue of destination */
1109 if (!(*cgen)((ast_expression*)(self->dest), func, true, &leftl))
1111 self->expression.outl = leftl;
1113 if (!ir_block_create_store_op(func->curblock, self->opstore, leftl, bin))
1115 self->expression.outr = bin;
1117 /* Theoretically, an assinment returns its left side as an
1118 * lvalue, if we don't need an lvalue though, we return
1119 * the right side as an rvalue, otherwise we have to
1120 * somehow know whether or not we need to dereference the pointer
1121 * on the left side - that is: OP_LOAD if it was an address.
1122 * Also: in original QC we cannot OP_LOADP *anyway*.
1124 *out = (lvalue ? leftl : bin);
1129 bool ast_unary_codegen(ast_unary *self, ast_function *func, bool lvalue, ir_value **out)
1131 ast_expression_codegen *cgen;
1134 /* In the context of a unary operation, we can disregard
1138 if (self->expression.outr) {
1139 *out = self->expression.outr;
1143 cgen = self->operand->expression.codegen;
1145 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
1148 *out = ir_block_create_unary(func->curblock, ast_function_label(func, "unary"),
1152 self->expression.outr = *out;
1157 bool ast_return_codegen(ast_return *self, ast_function *func, bool lvalue, ir_value **out)
1159 ast_expression_codegen *cgen;
1162 /* In the context of a return operation, we can disregard
1166 if (self->expression.outr) {
1167 asterror(ast_ctx(self), "internal error: ast_return cannot be reused, it bears no result!\n");
1170 self->expression.outr = (ir_value*)1;
1172 if (self->operand) {
1173 cgen = self->operand->expression.codegen;
1175 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
1178 if (!ir_block_create_return(func->curblock, operand))
1181 if (!ir_block_create_return(func->curblock, NULL))
1188 bool ast_entfield_codegen(ast_entfield *self, ast_function *func, bool lvalue, ir_value **out)
1190 ast_expression_codegen *cgen;
1191 ir_value *ent, *field;
1193 /* This function needs to take the 'lvalue' flag into account!
1194 * As lvalue we provide a field-pointer, as rvalue we provide the
1198 if (lvalue && self->expression.outl) {
1199 *out = self->expression.outl;
1203 if (!lvalue && self->expression.outr) {
1204 *out = self->expression.outr;
1208 cgen = self->entity->expression.codegen;
1209 if (!(*cgen)((ast_expression*)(self->entity), func, false, &ent))
1212 cgen = self->field->expression.codegen;
1213 if (!(*cgen)((ast_expression*)(self->field), func, false, &field))
1218 *out = ir_block_create_fieldaddress(func->curblock, ast_function_label(func, "efa"),
1221 *out = ir_block_create_load_from_ent(func->curblock, ast_function_label(func, "efv"),
1222 ent, field, self->expression.vtype);
1225 asterror(ast_ctx(self), "failed to create %s instruction (output type %s)",
1226 (lvalue ? "ADDRESS" : "FIELD"),
1227 type_name[self->expression.vtype]);
1232 self->expression.outl = *out;
1234 self->expression.outr = *out;
1236 /* Hm that should be it... */
1240 bool ast_member_codegen(ast_member *self, ast_function *func, bool lvalue, ir_value **out)
1242 ast_expression_codegen *cgen;
1245 /* in QC this is always an lvalue */
1247 if (self->expression.outl) {
1248 *out = self->expression.outl;
1252 cgen = self->owner->expression.codegen;
1253 if (!(*cgen)((ast_expression*)(self->owner), func, true, &vec))
1256 if (vec->vtype != TYPE_VECTOR &&
1257 !(vec->vtype == TYPE_FIELD && self->owner->expression.next->expression.vtype == TYPE_VECTOR))
1262 *out = ir_value_vector_member(vec, self->field);
1263 self->expression.outl = *out;
1265 return (*out != NULL);
1268 bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_value **out)
1270 ast_expression_codegen *cgen;
1275 ir_block *cond = func->curblock;
1280 /* We don't output any value, thus also don't care about r/lvalue */
1284 if (self->expression.outr) {
1285 asterror(ast_ctx(self), "internal error: ast_ifthen cannot be reused, it bears no result!\n");
1288 self->expression.outr = (ir_value*)1;
1290 /* generate the condition */
1291 func->curblock = cond;
1292 cgen = self->cond->expression.codegen;
1293 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
1298 if (self->on_true) {
1299 /* create on-true block */
1300 ontrue = ir_function_create_block(func->ir_func, ast_function_label(func, "ontrue"));
1304 /* enter the block */
1305 func->curblock = ontrue;
1308 cgen = self->on_true->expression.codegen;
1309 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &dummy))
1315 if (self->on_false) {
1316 /* create on-false block */
1317 onfalse = ir_function_create_block(func->ir_func, ast_function_label(func, "onfalse"));
1321 /* enter the block */
1322 func->curblock = onfalse;
1325 cgen = self->on_false->expression.codegen;
1326 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &dummy))
1331 /* Merge block were they all merge in to */
1332 merge = ir_function_create_block(func->ir_func, ast_function_label(func, "endif"));
1336 /* add jumps ot the merge block */
1337 if (ontrue && !ir_block_create_jump(ontrue, merge))
1339 if (onfalse && !ir_block_create_jump(onfalse, merge))
1342 /* we create the if here, that way all blocks are ordered :)
1344 if (!ir_block_create_if(cond, condval,
1345 (ontrue ? ontrue : merge),
1346 (onfalse ? onfalse : merge)))
1351 /* Now enter the merge block */
1352 func->curblock = merge;
1357 bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_value **out)
1359 ast_expression_codegen *cgen;
1362 ir_value *trueval, *falseval;
1365 ir_block *cond = func->curblock;
1370 /* Ternary can never create an lvalue... */
1374 /* In theory it shouldn't be possible to pass through a node twice, but
1375 * in case we add any kind of optimization pass for the AST itself, it
1376 * may still happen, thus we remember a created ir_value and simply return one
1377 * if it already exists.
1379 if (self->phi_out) {
1380 *out = self->phi_out;
1384 /* In the following, contraty to ast_ifthen, we assume both paths exist. */
1386 /* generate the condition */
1387 func->curblock = cond;
1388 cgen = self->cond->expression.codegen;
1389 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
1392 /* create on-true block */
1393 ontrue = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_T"));
1398 /* enter the block */
1399 func->curblock = ontrue;
1402 cgen = self->on_true->expression.codegen;
1403 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &trueval))
1407 /* create on-false block */
1408 onfalse = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_F"));
1413 /* enter the block */
1414 func->curblock = onfalse;
1417 cgen = self->on_false->expression.codegen;
1418 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &falseval))
1422 /* create merge block */
1423 merge = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_out"));
1426 /* jump to merge block */
1427 if (!ir_block_create_jump(ontrue, merge))
1429 if (!ir_block_create_jump(onfalse, merge))
1432 /* create if instruction */
1433 if (!ir_block_create_if(cond, condval, ontrue, onfalse))
1436 /* Now enter the merge block */
1437 func->curblock = merge;
1439 /* Here, now, we need a PHI node
1440 * but first some sanity checking...
1442 if (trueval->vtype != falseval->vtype) {
1443 /* error("ternary with different types on the two sides"); */
1448 phi = ir_block_create_phi(merge, ast_function_label(func, "phi"), trueval->vtype);
1450 !ir_phi_add(phi, ontrue, trueval) ||
1451 !ir_phi_add(phi, onfalse, falseval))
1456 self->phi_out = ir_phi_value(phi);
1457 *out = self->phi_out;
1462 bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value **out)
1464 ast_expression_codegen *cgen;
1466 ir_value *dummy = NULL;
1467 ir_value *precond = NULL;
1468 ir_value *postcond = NULL;
1470 /* Since we insert some jumps "late" so we have blocks
1471 * ordered "nicely", we need to keep track of the actual end-blocks
1472 * of expressions to add the jumps to.
1474 ir_block *bbody = NULL, *end_bbody = NULL;
1475 ir_block *bprecond = NULL, *end_bprecond = NULL;
1476 ir_block *bpostcond = NULL, *end_bpostcond = NULL;
1477 ir_block *bincrement = NULL, *end_bincrement = NULL;
1478 ir_block *bout = NULL, *bin = NULL;
1480 /* let's at least move the outgoing block to the end */
1483 /* 'break' and 'continue' need to be able to find the right blocks */
1484 ir_block *bcontinue = NULL;
1485 ir_block *bbreak = NULL;
1487 ir_block *old_bcontinue = NULL;
1488 ir_block *old_bbreak = NULL;
1490 ir_block *tmpblock = NULL;
1495 if (self->expression.outr) {
1496 asterror(ast_ctx(self), "internal error: ast_loop cannot be reused, it bears no result!\n");
1499 self->expression.outr = (ir_value*)1;
1502 * Should we ever need some kind of block ordering, better make this function
1503 * move blocks around than write a block ordering algorithm later... after all
1504 * the ast and ir should work together, not against each other.
1507 /* initexpr doesn't get its own block, it's pointless, it could create more blocks
1508 * anyway if for example it contains a ternary.
1512 cgen = self->initexpr->expression.codegen;
1513 if (!(*cgen)((ast_expression*)(self->initexpr), func, false, &dummy))
1517 /* Store the block from which we enter this chaos */
1518 bin = func->curblock;
1520 /* The pre-loop condition needs its own block since we
1521 * need to be able to jump to the start of that expression.
1525 bprecond = ir_function_create_block(func->ir_func, ast_function_label(func, "pre_loop_cond"));
1529 /* the pre-loop-condition the least important place to 'continue' at */
1530 bcontinue = bprecond;
1533 func->curblock = bprecond;
1536 cgen = self->precond->expression.codegen;
1537 if (!(*cgen)((ast_expression*)(self->precond), func, false, &precond))
1540 end_bprecond = func->curblock;
1542 bprecond = end_bprecond = NULL;
1545 /* Now the next blocks won't be ordered nicely, but we need to
1546 * generate them this early for 'break' and 'continue'.
1548 if (self->increment) {
1549 bincrement = ir_function_create_block(func->ir_func, ast_function_label(func, "loop_increment"));
1552 bcontinue = bincrement; /* increment comes before the pre-loop-condition */
1554 bincrement = end_bincrement = NULL;
1557 if (self->postcond) {
1558 bpostcond = ir_function_create_block(func->ir_func, ast_function_label(func, "post_loop_cond"));
1561 bcontinue = bpostcond; /* postcond comes before the increment */
1563 bpostcond = end_bpostcond = NULL;
1566 bout_id = func->ir_func->blocks_count;
1567 bout = ir_function_create_block(func->ir_func, ast_function_label(func, "after_loop"));
1572 /* The loop body... */
1575 bbody = ir_function_create_block(func->ir_func, ast_function_label(func, "loop_body"));
1580 func->curblock = bbody;
1582 old_bbreak = func->breakblock;
1583 old_bcontinue = func->continueblock;
1584 func->breakblock = bbreak;
1585 func->continueblock = bcontinue;
1588 cgen = self->body->expression.codegen;
1589 if (!(*cgen)((ast_expression*)(self->body), func, false, &dummy))
1592 end_bbody = func->curblock;
1593 func->breakblock = old_bbreak;
1594 func->continueblock = old_bcontinue;
1597 /* post-loop-condition */
1601 func->curblock = bpostcond;
1604 cgen = self->postcond->expression.codegen;
1605 if (!(*cgen)((ast_expression*)(self->postcond), func, false, &postcond))
1608 end_bpostcond = func->curblock;
1611 /* The incrementor */
1612 if (self->increment)
1615 func->curblock = bincrement;
1618 cgen = self->increment->expression.codegen;
1619 if (!(*cgen)((ast_expression*)(self->increment), func, false, &dummy))
1622 end_bincrement = func->curblock;
1625 /* In any case now, we continue from the outgoing block */
1626 func->curblock = bout;
1628 /* Now all blocks are in place */
1629 /* From 'bin' we jump to whatever comes first */
1630 if (bprecond) tmpblock = bprecond;
1631 else if (bbody) tmpblock = bbody;
1632 else if (bpostcond) tmpblock = bpostcond;
1633 else tmpblock = bout;
1634 if (!ir_block_create_jump(bin, tmpblock))
1640 ir_block *ontrue, *onfalse;
1641 if (bbody) ontrue = bbody;
1642 else if (bincrement) ontrue = bincrement;
1643 else if (bpostcond) ontrue = bpostcond;
1644 else ontrue = bprecond;
1646 if (!ir_block_create_if(end_bprecond, precond, ontrue, onfalse))
1653 if (bincrement) tmpblock = bincrement;
1654 else if (bpostcond) tmpblock = bpostcond;
1655 else if (bprecond) tmpblock = bprecond;
1656 else tmpblock = bout;
1657 if (!ir_block_create_jump(end_bbody, tmpblock))
1661 /* from increment */
1664 if (bpostcond) tmpblock = bpostcond;
1665 else if (bprecond) tmpblock = bprecond;
1666 else if (bbody) tmpblock = bbody;
1667 else tmpblock = bout;
1668 if (!ir_block_create_jump(end_bincrement, tmpblock))
1675 ir_block *ontrue, *onfalse;
1676 if (bprecond) ontrue = bprecond;
1677 else if (bbody) ontrue = bbody;
1678 else if (bincrement) ontrue = bincrement;
1679 else ontrue = bpostcond;
1681 if (!ir_block_create_if(end_bpostcond, postcond, ontrue, onfalse))
1685 /* Move 'bout' to the end */
1686 if (!ir_function_blocks_remove(func->ir_func, bout_id) ||
1687 !ir_function_blocks_add(func->ir_func, bout))
1689 ir_block_delete(bout);
1696 bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value **out)
1698 ast_expression_codegen *cgen;
1699 ir_value_vector params;
1700 ir_instr *callinstr;
1703 ir_value *funval = NULL;
1705 /* return values are never lvalues */
1708 if (self->expression.outr) {
1709 *out = self->expression.outr;
1713 cgen = self->func->expression.codegen;
1714 if (!(*cgen)((ast_expression*)(self->func), func, false, &funval))
1719 MEM_VECTOR_INIT(¶ms, v);
1722 for (i = 0; i < self->params_count; ++i)
1725 ast_expression *expr = self->params[i];
1727 cgen = expr->expression.codegen;
1728 if (!(*cgen)(expr, func, false, ¶m))
1732 if (!ir_value_vector_v_add(¶ms, param))
1736 callinstr = ir_block_create_call(func->curblock, ast_function_label(func, "call"), funval);
1740 for (i = 0; i < params.v_count; ++i) {
1741 if (!ir_call_param(callinstr, params.v[i]))
1745 *out = ir_call_value(callinstr);
1746 self->expression.outr = *out;
1748 MEM_VECTOR_CLEAR(¶ms, v);
1751 MEM_VECTOR_CLEAR(¶ms, v);