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
38 /* It must not be possible to get here. */
39 static GMQCC_NORETURN void _ast_node_destroy(ast_node *self)
41 fprintf(stderr, "ast node missing destroy()\n");
45 /* Initialize main ast node aprts */
46 static void ast_node_init(ast_node *self, lex_ctx ctx, int nodetype)
48 self->node.context = ctx;
49 self->node.destroy = &_ast_node_destroy;
50 self->node.keep = false;
51 self->node.nodetype = nodetype;
54 /* General expression initialization */
55 static void ast_expression_init(ast_expression *self,
56 ast_expression_codegen *codegen)
58 self->expression.codegen = codegen;
59 self->expression.vtype = TYPE_VOID;
60 self->expression.next = NULL;
61 MEM_VECTOR_INIT(&self->expression, params);
64 static void ast_expression_delete(ast_expression *self)
67 if (self->expression.next)
68 ast_delete(self->expression.next);
69 for (i = 0; i < self->expression.params_count; ++i) {
70 ast_delete(self->expression.params[i]);
72 MEM_VECTOR_CLEAR(&self->expression, params);
75 static void ast_expression_delete_full(ast_expression *self)
77 ast_expression_delete(self);
81 MEM_VEC_FUNCTIONS(ast_expression_common, ast_value*, params)
83 static ast_expression* ast_type_copy(lex_ctx ctx, const ast_expression *ex);
84 static ast_value* ast_value_copy(const ast_value *self)
86 ast_value *cp = ast_value_new(self->expression.node.context, self->name, self->expression.vtype);
87 if (self->expression.next) {
88 cp->expression.next = ast_type_copy(self->expression.node.context, self->expression.next);
89 if (!cp->expression.next) {
97 static ast_expression* ast_shallow_type(lex_ctx ctx, int vtype)
99 ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
100 self->expression.codegen = NULL;
101 self->expression.next = NULL;
102 self->expression.vtype = vtype;
106 static ast_expression* ast_type_copy(lex_ctx ctx, const ast_expression *ex)
109 const ast_expression_common *fromex;
110 ast_expression_common *selfex;
116 ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
118 fromex = &ex->expression;
119 selfex = &self->expression;
121 /* This may never be codegen()d */
122 selfex->codegen = NULL;
124 selfex->vtype = fromex->vtype;
127 selfex->next = ast_type_copy(ctx, fromex->next);
129 ast_expression_delete_full(self);
136 for (i = 0; i < fromex->params_count; ++i) {
137 ast_value *v = ast_value_copy(fromex->params[i]);
138 if (!v || !ast_expression_common_params_add(selfex, v)) {
139 ast_expression_delete_full(self);
148 ast_value* ast_value_new(lex_ctx ctx, const char *name, int t)
150 ast_instantiate(ast_value, ctx, ast_value_delete);
151 ast_expression_init((ast_expression*)self,
152 (ast_expression_codegen*)&ast_value_codegen);
153 self->expression.node.keep = true; /* keep */
155 self->name = name ? util_strdup(name) : NULL;
156 self->expression.vtype = t;
157 self->expression.next = NULL;
158 self->isconst = false;
159 memset(&self->constval, 0, sizeof(self->constval));
166 void ast_value_delete(ast_value* self)
169 mem_d((void*)self->name);
171 switch (self->expression.vtype)
174 mem_d((void*)self->constval.vstring);
177 /* unlink us from the function node */
178 self->constval.vfunc->vtype = NULL;
180 /* NOTE: delete function? currently collected in
181 * the parser structure
187 ast_expression_delete((ast_expression*)self);
191 bool GMQCC_WARN ast_value_params_add(ast_value *self, ast_value *p)
193 return ast_expression_common_params_add(&self->expression, p);
196 bool ast_value_set_name(ast_value *self, const char *name)
199 mem_d((void*)self->name);
200 self->name = util_strdup(name);
204 ast_binary* ast_binary_new(lex_ctx ctx, int op,
205 ast_expression* left, ast_expression* right)
207 ast_instantiate(ast_binary, ctx, ast_binary_delete);
208 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binary_codegen);
214 if (op >= INSTR_EQ_F && op <= INSTR_GT)
215 self->expression.vtype = TYPE_FLOAT;
216 else if (op == INSTR_AND || op == INSTR_OR ||
217 op == INSTR_BITAND || op == INSTR_BITOR)
218 self->expression.vtype = TYPE_FLOAT;
219 else if (op == INSTR_MUL_VF || op == INSTR_MUL_FV)
220 self->expression.vtype = TYPE_VECTOR;
221 else if (op == INSTR_MUL_V)
222 self->expression.vtype = TYPE_FLOAT;
224 self->expression.vtype = left->expression.vtype;
229 void ast_binary_delete(ast_binary *self)
231 ast_unref(self->left);
232 ast_unref(self->right);
233 ast_expression_delete((ast_expression*)self);
237 ast_unary* ast_unary_new(lex_ctx ctx, int op,
238 ast_expression *expr)
240 ast_instantiate(ast_unary, ctx, ast_unary_delete);
241 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_unary_codegen);
244 self->operand = expr;
249 void ast_unary_delete(ast_unary *self)
251 ast_unref(self->operand);
252 ast_expression_delete((ast_expression*)self);
256 ast_return* ast_return_new(lex_ctx ctx, ast_expression *expr)
258 ast_instantiate(ast_return, ctx, ast_return_delete);
259 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_return_codegen);
261 self->operand = expr;
266 void ast_return_delete(ast_return *self)
268 ast_unref(self->operand);
269 ast_expression_delete((ast_expression*)self);
273 ast_entfield* ast_entfield_new(lex_ctx ctx, ast_expression *entity, ast_expression *field)
275 const ast_expression *outtype;
277 ast_instantiate(ast_entfield, ctx, ast_entfield_delete);
279 if (field->expression.vtype != TYPE_FIELD) {
284 outtype = field->expression.next;
287 /* Error: field has no type... */
291 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_entfield_codegen);
293 self->expression.vtype = outtype->expression.vtype;
294 self->expression.next = ast_type_copy(ctx, outtype->expression.next);
296 self->entity = entity;
302 void ast_entfield_delete(ast_entfield *self)
304 ast_unref(self->entity);
305 ast_unref(self->field);
306 ast_expression_delete((ast_expression*)self);
310 ast_member* ast_member_new(lex_ctx ctx, ast_expression *owner, unsigned int field)
312 ast_instantiate(ast_member, ctx, ast_member_delete);
318 if (owner->expression.vtype != TYPE_FLOAT &&
319 owner->expression.vtype != TYPE_FIELD) {
320 printf("ast_member on an invalid owner of type %i\n", (int)owner->expression.vtype);
325 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_member_codegen);
327 if (owner->expression.vtype == TYPE_VECTOR) {
328 self->expression.vtype = TYPE_FLOAT;
329 self->expression.next = NULL;
331 self->expression.vtype = TYPE_FIELD;
332 self->expression.next = ast_shallow_type(ctx, TYPE_FLOAT);
341 void ast_member_delete(ast_member *self)
343 ast_unref(self->owner);
344 ast_expression_delete((ast_expression*)self);
348 ast_ifthen* ast_ifthen_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
350 ast_instantiate(ast_ifthen, ctx, ast_ifthen_delete);
351 if (!ontrue && !onfalse) {
352 /* because it is invalid */
356 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ifthen_codegen);
359 self->on_true = ontrue;
360 self->on_false = onfalse;
365 void ast_ifthen_delete(ast_ifthen *self)
367 ast_unref(self->cond);
369 ast_unref(self->on_true);
371 ast_unref(self->on_false);
372 ast_expression_delete((ast_expression*)self);
376 ast_ternary* ast_ternary_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
378 ast_instantiate(ast_ternary, ctx, ast_ternary_delete);
379 /* This time NEITHER must be NULL */
380 if (!ontrue || !onfalse) {
384 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ternary_codegen);
387 self->on_true = ontrue;
388 self->on_false = onfalse;
389 self->phi_out = NULL;
394 void ast_ternary_delete(ast_ternary *self)
396 ast_unref(self->cond);
397 ast_unref(self->on_true);
398 ast_unref(self->on_false);
399 ast_expression_delete((ast_expression*)self);
403 ast_loop* ast_loop_new(lex_ctx ctx,
404 ast_expression *initexpr,
405 ast_expression *precond,
406 ast_expression *postcond,
407 ast_expression *increment,
408 ast_expression *body)
410 ast_instantiate(ast_loop, ctx, ast_loop_delete);
411 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_loop_codegen);
413 self->initexpr = initexpr;
414 self->precond = precond;
415 self->postcond = postcond;
416 self->increment = increment;
422 void ast_loop_delete(ast_loop *self)
425 ast_unref(self->initexpr);
427 ast_unref(self->precond);
429 ast_unref(self->postcond);
431 ast_unref(self->increment);
433 ast_unref(self->body);
434 ast_expression_delete((ast_expression*)self);
438 ast_call* ast_call_new(lex_ctx ctx,
439 ast_expression *funcexpr)
441 ast_instantiate(ast_call, ctx, ast_call_delete);
442 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_call_codegen);
444 MEM_VECTOR_INIT(self, params);
446 self->func = funcexpr;
450 MEM_VEC_FUNCTIONS(ast_call, ast_expression*, params)
452 void ast_call_delete(ast_call *self)
455 for (i = 0; i < self->params_count; ++i)
456 ast_unref(self->params[i]);
457 MEM_VECTOR_CLEAR(self, params);
460 ast_unref(self->func);
462 ast_expression_delete((ast_expression*)self);
466 ast_store* ast_store_new(lex_ctx ctx, int op,
467 ast_expression *dest, ast_expression *source)
469 ast_instantiate(ast_store, ctx, ast_store_delete);
470 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_store_codegen);
474 self->source = source;
479 void ast_store_delete(ast_store *self)
481 ast_unref(self->dest);
482 ast_unref(self->source);
483 ast_expression_delete((ast_expression*)self);
487 ast_block* ast_block_new(lex_ctx ctx)
489 ast_instantiate(ast_block, ctx, ast_block_delete);
490 ast_expression_init((ast_expression*)self,
491 (ast_expression_codegen*)&ast_block_codegen);
493 MEM_VECTOR_INIT(self, locals);
494 MEM_VECTOR_INIT(self, exprs);
498 MEM_VEC_FUNCTIONS(ast_block, ast_value*, locals)
499 MEM_VEC_FUNCTIONS(ast_block, ast_expression*, exprs)
501 void ast_block_delete(ast_block *self)
504 for (i = 0; i < self->exprs_count; ++i)
505 ast_unref(self->exprs[i]);
506 MEM_VECTOR_CLEAR(self, exprs);
507 for (i = 0; i < self->locals_count; ++i)
508 ast_delete(self->locals[i]);
509 MEM_VECTOR_CLEAR(self, locals);
510 ast_expression_delete((ast_expression*)self);
514 bool ast_block_set_type(ast_block *self, ast_expression *from)
516 if (self->expression.next)
517 ast_delete(self->expression.next);
518 self->expression.vtype = from->expression.vtype;
519 if (from->expression.next) {
520 self->expression.next = ast_type_copy(self->expression.node.context, from->expression.next);
521 if (!self->expression.next)
527 ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype)
529 ast_instantiate(ast_function, ctx, ast_function_delete);
533 vtype->expression.vtype != TYPE_FUNCTION)
540 self->name = name ? util_strdup(name) : NULL;
541 MEM_VECTOR_INIT(self, blocks);
543 self->labelcount = 0;
546 self->ir_func = NULL;
547 self->curblock = NULL;
549 self->breakblock = NULL;
550 self->continueblock = NULL;
552 vtype->isconst = true;
553 vtype->constval.vfunc = self;
558 MEM_VEC_FUNCTIONS(ast_function, ast_block*, blocks)
560 void ast_function_delete(ast_function *self)
564 mem_d((void*)self->name);
566 /* ast_value_delete(self->vtype); */
567 self->vtype->isconst = false;
568 self->vtype->constval.vfunc = NULL;
569 /* We use unref - if it was stored in a global table it is supposed
570 * to be deleted from *there*
572 ast_unref(self->vtype);
574 for (i = 0; i < self->blocks_count; ++i)
575 ast_delete(self->blocks[i]);
576 MEM_VECTOR_CLEAR(self, blocks);
580 static void ast_util_hexitoa(char *buf, size_t size, unsigned int num)
582 unsigned int base = 10;
583 #define checknul() do { if (size == 1) { *buf = 0; return; } } while (0)
584 #define addch(x) do { *buf++ = (x); --size; checknul(); } while (0)
593 int digit = num % base;
604 const char* ast_function_label(ast_function *self, const char *prefix)
606 size_t id = (self->labelcount++);
607 size_t len = strlen(prefix);
608 strncpy(self->labelbuf, prefix, sizeof(self->labelbuf));
609 ast_util_hexitoa(self->labelbuf + len, sizeof(self->labelbuf)-len, id);
610 return self->labelbuf;
613 /*********************************************************************/
615 * by convention you must never pass NULL to the 'ir_value **out'
616 * parameter. If you really don't care about the output, pass a dummy.
617 * But I can't imagine a pituation where the output is truly unnecessary.
620 bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out)
622 /* NOTE: This is the codegen for a variable used in an expression.
623 * It is not the codegen to generate the value. For this purpose,
624 * ast_local_codegen and ast_global_codegen are to be used before this
625 * is executed. ast_function_codegen should take care of its locals,
626 * and the ast-user should take care of ast_global_codegen to be used
627 * on all the globals.
630 printf("ast_value used before generated (%s)\n", self->name);
637 bool ast_global_codegen(ast_value *self, ir_builder *ir)
640 if (self->isconst && self->expression.vtype == TYPE_FUNCTION)
642 ir_function *func = ir_builder_create_function(ir, self->name, self->expression.next->expression.vtype);
646 self->constval.vfunc->ir_func = func;
647 self->ir_v = func->value;
648 /* The function is filled later on ast_function_codegen... */
652 if (self->expression.vtype == TYPE_FIELD) {
653 v = ir_builder_create_field(ir, self->name, self->expression.next->expression.vtype);
657 printf("TODO: constant field pointers with value\n");
664 v = ir_builder_create_global(ir, self->name, self->expression.vtype);
666 printf("ir_builder_create_global failed\n");
671 switch (self->expression.vtype)
674 if (!ir_value_set_float(v, self->constval.vfloat))
678 if (!ir_value_set_vector(v, self->constval.vvec))
682 if (!ir_value_set_string(v, self->constval.vstring))
686 printf("global of type function not properly generated\n");
688 /* Cannot generate an IR value for a function,
689 * need a pointer pointing to a function rather.
692 printf("TODO: global constant type %i\n", self->expression.vtype);
697 /* link us to the ir_value */
701 error: /* clean up */
706 bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
709 if (self->isconst && self->expression.vtype == TYPE_FUNCTION)
711 /* Do we allow local functions? I think not...
712 * this is NOT a function pointer atm.
717 v = ir_function_create_local(func, self->name, self->expression.vtype, param);
721 /* A constant local... hmmm...
722 * I suppose the IR will have to deal with this
725 switch (self->expression.vtype)
728 if (!ir_value_set_float(v, self->constval.vfloat))
732 if (!ir_value_set_vector(v, self->constval.vvec))
736 if (!ir_value_set_string(v, self->constval.vstring))
740 printf("TODO: global constant type %i\n", self->expression.vtype);
745 /* link us to the ir_value */
749 error: /* clean up */
754 bool ast_function_codegen(ast_function *self, ir_builder *ir)
758 ast_expression_common *ec;
763 printf("ast_function's related ast_value was not generated yet\n");
767 /* fill the parameter list */
768 ec = &self->vtype->expression;
769 for (i = 0; i < ec->params_count; ++i)
771 if (!ir_function_params_add(irf, ec->params[i]->expression.vtype))
773 if (!self->builtin) {
774 if (!ast_local_codegen(ec->params[i], self->ir_func, true))
780 irf->builtin = self->builtin;
784 self->curblock = ir_function_create_block(irf, "entry");
788 for (i = 0; i < self->blocks_count; ++i) {
789 ast_expression_codegen *gen = self->blocks[i]->expression.codegen;
790 if (!(*gen)((ast_expression*)self->blocks[i], self, false, &dummy))
794 /* TODO: check return types */
795 if (!self->curblock->is_return)
797 if (!self->vtype->expression.next ||
798 self->vtype->expression.next->expression.vtype == TYPE_VOID)
800 return ir_block_create_return(self->curblock, NULL);
804 /* error("missing return"); */
811 /* Note, you will not see ast_block_codegen generate ir_blocks.
812 * To the AST and the IR, blocks are 2 different things.
813 * In the AST it represents a block of code, usually enclosed in
814 * curly braces {...}.
815 * While in the IR it represents a block in terms of control-flow.
817 bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_value **out)
822 * Note: an ast-representation using the comma-operator
823 * of the form: (a, b, c) = x should not assign to c...
827 /* output is NULL at first, we'll have each expression
828 * assign to out output, thus, a comma-operator represention
829 * using an ast_block will return the last generated value,
830 * so: (b, c) + a executed both b and c, and returns c,
831 * which is then added to a.
835 /* generate locals */
836 for (i = 0; i < self->locals_count; ++i)
838 if (!ast_local_codegen(self->locals[i], func->ir_func, false))
842 for (i = 0; i < self->exprs_count; ++i)
844 ast_expression_codegen *gen = self->exprs[i]->expression.codegen;
845 if (!(*gen)(self->exprs[i], func, false, out))
852 bool ast_store_codegen(ast_store *self, ast_function *func, bool lvalue, ir_value **out)
854 ast_expression_codegen *cgen;
855 ir_value *left, *right;
857 cgen = self->dest->expression.codegen;
859 if (!(*cgen)((ast_expression*)(self->dest), func, true, &left))
862 cgen = self->source->expression.codegen;
864 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
867 if (!ir_block_create_store_op(func->curblock, self->op, left, right))
870 /* Theoretically, an assinment returns its left side as an
871 * lvalue, if we don't need an lvalue though, we return
872 * the right side as an rvalue, otherwise we have to
873 * somehow know whether or not we need to dereference the pointer
874 * on the left side - that is: OP_LOAD if it was an address.
875 * Also: in original QC we cannot OP_LOADP *anyway*.
877 *out = (lvalue ? left : right);
882 bool ast_binary_codegen(ast_binary *self, ast_function *func, bool lvalue, ir_value **out)
884 ast_expression_codegen *cgen;
885 ir_value *left, *right;
887 /* In the context of a binary operation, we can disregard
892 cgen = self->left->expression.codegen;
894 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
897 cgen = self->right->expression.codegen;
899 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
902 *out = ir_block_create_binop(func->curblock, ast_function_label(func, "bin"),
903 self->op, left, right);
910 bool ast_unary_codegen(ast_unary *self, ast_function *func, bool lvalue, ir_value **out)
912 ast_expression_codegen *cgen;
915 /* In the context of a unary operation, we can disregard
920 cgen = self->operand->expression.codegen;
922 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
925 *out = ir_block_create_unary(func->curblock, ast_function_label(func, "unary"),
933 bool ast_return_codegen(ast_return *self, ast_function *func, bool lvalue, ir_value **out)
935 ast_expression_codegen *cgen;
938 /* In the context of a return operation, we can disregard
943 cgen = self->operand->expression.codegen;
945 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
948 if (!ir_block_create_return(func->curblock, operand))
954 bool ast_entfield_codegen(ast_entfield *self, ast_function *func, bool lvalue, ir_value **out)
956 ast_expression_codegen *cgen;
957 ir_value *ent, *field;
959 /* This function needs to take the 'lvalue' flag into account!
960 * As lvalue we provide a field-pointer, as rvalue we provide the
964 cgen = self->entity->expression.codegen;
965 if (!(*cgen)((ast_expression*)(self->entity), func, false, &ent))
968 cgen = self->field->expression.codegen;
969 if (!(*cgen)((ast_expression*)(self->field), func, false, &field))
974 *out = ir_block_create_fieldaddress(func->curblock, ast_function_label(func, "efa"),
977 *out = ir_block_create_load_from_ent(func->curblock, ast_function_label(func, "efv"),
978 ent, field, self->expression.vtype);
983 /* Hm that should be it... */
987 bool ast_member_codegen(ast_member *self, ast_function *func, bool lvalue, ir_value **out)
989 ast_expression_codegen *cgen;
992 cgen = self->owner->expression.codegen;
993 if (!(*cgen)((ast_expression*)(self->owner), func, true, &vec))
996 if (vec->vtype != TYPE_VECTOR &&
997 !(vec->vtype == TYPE_FIELD && self->owner->expression.next->expression.vtype == TYPE_VECTOR))
1002 *out = ir_value_vector_member(vec, self->field);
1004 return (*out != NULL);
1007 bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_value **out)
1009 ast_expression_codegen *cgen;
1014 ir_block *cond = func->curblock;
1019 /* We don't output any value, thus also don't care about r/lvalue */
1023 /* generate the condition */
1024 func->curblock = cond;
1025 cgen = self->cond->expression.codegen;
1026 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
1031 if (self->on_true) {
1032 /* create on-true block */
1033 ontrue = ir_function_create_block(func->ir_func, ast_function_label(func, "ontrue"));
1037 /* enter the block */
1038 func->curblock = ontrue;
1041 cgen = self->on_true->expression.codegen;
1042 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &dummy))
1048 if (self->on_false) {
1049 /* create on-false block */
1050 onfalse = ir_function_create_block(func->ir_func, ast_function_label(func, "onfalse"));
1054 /* enter the block */
1055 func->curblock = onfalse;
1058 cgen = self->on_false->expression.codegen;
1059 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &dummy))
1064 /* Merge block were they all merge in to */
1065 merge = ir_function_create_block(func->ir_func, ast_function_label(func, "endif"));
1069 /* add jumps ot the merge block */
1070 if (ontrue && !ir_block_create_jump(ontrue, merge))
1072 if (onfalse && !ir_block_create_jump(onfalse, merge))
1075 /* we create the if here, that way all blocks are ordered :)
1077 if (!ir_block_create_if(cond, condval,
1078 (ontrue ? ontrue : merge),
1079 (onfalse ? onfalse : merge)))
1084 /* Now enter the merge block */
1085 func->curblock = merge;
1090 bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_value **out)
1092 ast_expression_codegen *cgen;
1095 ir_value *trueval, *falseval;
1098 ir_block *cond = func->curblock;
1103 /* In theory it shouldn't be possible to pass through a node twice, but
1104 * in case we add any kind of optimization pass for the AST itself, it
1105 * may still happen, thus we remember a created ir_value and simply return one
1106 * if it already exists.
1108 if (self->phi_out) {
1109 *out = self->phi_out;
1113 /* Ternary can never create an lvalue... */
1117 /* In the following, contraty to ast_ifthen, we assume both paths exist. */
1119 /* generate the condition */
1120 func->curblock = cond;
1121 cgen = self->cond->expression.codegen;
1122 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
1125 /* create on-true block */
1126 ontrue = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_T"));
1131 /* enter the block */
1132 func->curblock = ontrue;
1135 cgen = self->on_true->expression.codegen;
1136 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &trueval))
1140 /* create on-false block */
1141 onfalse = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_F"));
1146 /* enter the block */
1147 func->curblock = onfalse;
1150 cgen = self->on_false->expression.codegen;
1151 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &falseval))
1155 /* create merge block */
1156 merge = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_out"));
1159 /* jump to merge block */
1160 if (!ir_block_create_jump(ontrue, merge))
1162 if (!ir_block_create_jump(onfalse, merge))
1165 /* create if instruction */
1166 if (!ir_block_create_if(cond, condval, ontrue, onfalse))
1169 /* Now enter the merge block */
1170 func->curblock = merge;
1172 /* Here, now, we need a PHI node
1173 * but first some sanity checking...
1175 if (trueval->vtype != falseval->vtype) {
1176 /* error("ternary with different types on the two sides"); */
1181 phi = ir_block_create_phi(merge, ast_function_label(func, "phi"), trueval->vtype);
1183 !ir_phi_add(phi, ontrue, trueval) ||
1184 !ir_phi_add(phi, onfalse, falseval))
1189 self->phi_out = ir_phi_value(phi);
1190 *out = self->phi_out;
1195 bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value **out)
1197 ast_expression_codegen *cgen;
1199 ir_value *dummy = NULL;
1200 ir_value *precond = NULL;
1201 ir_value *postcond = NULL;
1203 /* Since we insert some jumps "late" so we have blocks
1204 * ordered "nicely", we need to keep track of the actual end-blocks
1205 * of expressions to add the jumps to.
1207 ir_block *bbody = NULL, *end_bbody = NULL;
1208 ir_block *bprecond = NULL, *end_bprecond = NULL;
1209 ir_block *bpostcond = NULL, *end_bpostcond = NULL;
1210 ir_block *bincrement = NULL, *end_bincrement = NULL;
1211 ir_block *bout = NULL, *bin = NULL;
1213 /* let's at least move the outgoing block to the end */
1216 /* 'break' and 'continue' need to be able to find the right blocks */
1217 ir_block *bcontinue = NULL;
1218 ir_block *bbreak = NULL;
1220 ir_block *old_bcontinue = NULL;
1221 ir_block *old_bbreak = NULL;
1223 ir_block *tmpblock = NULL;
1229 * Should we ever need some kind of block ordering, better make this function
1230 * move blocks around than write a block ordering algorithm later... after all
1231 * the ast and ir should work together, not against each other.
1234 /* initexpr doesn't get its own block, it's pointless, it could create more blocks
1235 * anyway if for example it contains a ternary.
1239 cgen = self->initexpr->expression.codegen;
1240 if (!(*cgen)((ast_expression*)(self->initexpr), func, false, &dummy))
1244 /* Store the block from which we enter this chaos */
1245 bin = func->curblock;
1247 /* The pre-loop condition needs its own block since we
1248 * need to be able to jump to the start of that expression.
1252 bprecond = ir_function_create_block(func->ir_func, ast_function_label(func, "pre_loop_cond"));
1256 /* the pre-loop-condition the least important place to 'continue' at */
1257 bcontinue = bprecond;
1260 func->curblock = bprecond;
1263 cgen = self->precond->expression.codegen;
1264 if (!(*cgen)((ast_expression*)(self->precond), func, false, &precond))
1267 end_bprecond = func->curblock;
1269 bprecond = end_bprecond = NULL;
1272 /* Now the next blocks won't be ordered nicely, but we need to
1273 * generate them this early for 'break' and 'continue'.
1275 if (self->increment) {
1276 bincrement = ir_function_create_block(func->ir_func, ast_function_label(func, "loop_increment"));
1279 bcontinue = bincrement; /* increment comes before the pre-loop-condition */
1281 bincrement = end_bincrement = NULL;
1284 if (self->postcond) {
1285 bpostcond = ir_function_create_block(func->ir_func, ast_function_label(func, "post_loop_cond"));
1288 bcontinue = bpostcond; /* postcond comes before the increment */
1290 bpostcond = end_bpostcond = NULL;
1293 bout_id = func->ir_func->blocks_count;
1294 bout = ir_function_create_block(func->ir_func, ast_function_label(func, "after_loop"));
1299 /* The loop body... */
1302 bbody = ir_function_create_block(func->ir_func, ast_function_label(func, "loop_body"));
1307 func->curblock = bbody;
1309 old_bbreak = func->breakblock;
1310 old_bcontinue = func->continueblock;
1311 func->breakblock = bbreak;
1312 func->continueblock = bcontinue;
1315 cgen = self->body->expression.codegen;
1316 if (!(*cgen)((ast_expression*)(self->body), func, false, &dummy))
1319 end_bbody = func->curblock;
1320 func->breakblock = old_bbreak;
1321 func->continueblock = old_bcontinue;
1324 /* post-loop-condition */
1328 func->curblock = bpostcond;
1331 cgen = self->postcond->expression.codegen;
1332 if (!(*cgen)((ast_expression*)(self->postcond), func, false, &postcond))
1335 end_bpostcond = func->curblock;
1338 /* The incrementor */
1339 if (self->increment)
1342 func->curblock = bincrement;
1345 cgen = self->increment->expression.codegen;
1346 if (!(*cgen)((ast_expression*)(self->increment), func, false, &dummy))
1349 end_bincrement = func->curblock;
1352 /* In any case now, we continue from the outgoing block */
1353 func->curblock = bout;
1355 /* Now all blocks are in place */
1356 /* From 'bin' we jump to whatever comes first */
1357 if (bprecond) tmpblock = bprecond;
1358 else if (bbody) tmpblock = bbody;
1359 else if (bpostcond) tmpblock = bpostcond;
1360 else tmpblock = bout;
1361 if (!ir_block_create_jump(bin, tmpblock))
1367 ir_block *ontrue, *onfalse;
1368 if (bbody) ontrue = bbody;
1369 else if (bincrement) ontrue = bincrement;
1370 else if (bpostcond) ontrue = bpostcond;
1371 else ontrue = bprecond;
1373 if (!ir_block_create_if(end_bprecond, precond, ontrue, onfalse))
1380 if (bincrement) tmpblock = bincrement;
1381 else if (bpostcond) tmpblock = bpostcond;
1382 else if (bprecond) tmpblock = bprecond;
1383 else tmpblock = bout;
1384 if (!ir_block_create_jump(end_bbody, tmpblock))
1388 /* from increment */
1391 if (bpostcond) tmpblock = bpostcond;
1392 else if (bprecond) tmpblock = bprecond;
1393 else if (bbody) tmpblock = bbody;
1394 else tmpblock = bout;
1395 if (!ir_block_create_jump(end_bincrement, tmpblock))
1402 ir_block *ontrue, *onfalse;
1403 if (bprecond) ontrue = bprecond;
1404 else if (bbody) ontrue = bbody;
1405 else if (bincrement) ontrue = bincrement;
1406 else ontrue = bpostcond;
1408 if (!ir_block_create_if(end_bpostcond, postcond, ontrue, onfalse))
1412 /* Move 'bout' to the end */
1413 if (!ir_function_blocks_remove(func->ir_func, bout_id) ||
1414 !ir_function_blocks_add(func->ir_func, bout))
1416 ir_block_delete(bout);
1423 bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value **out)
1425 ast_expression_codegen *cgen;
1426 ir_value_vector params;
1427 ir_instr *callinstr;
1430 ir_value *funval = NULL;
1432 /* return values are never rvalues */
1435 cgen = self->func->expression.codegen;
1436 if (!(*cgen)((ast_expression*)(self->func), func, false, &funval))
1441 MEM_VECTOR_INIT(¶ms, v);
1444 for (i = 0; i < self->params_count; ++i)
1447 ast_expression *expr = self->params[i];
1449 cgen = expr->expression.codegen;
1450 if (!(*cgen)(expr, func, false, ¶m))
1454 if (!ir_value_vector_v_add(¶ms, param))
1458 callinstr = ir_block_create_call(func->curblock, ast_function_label(func, "call"), funval);
1462 for (i = 0; i < params.v_count; ++i) {
1463 if (!ir_call_param(callinstr, params.v[i]))
1467 *out = ir_call_value(callinstr);
1469 MEM_VECTOR_CLEAR(¶ms, v);
1472 MEM_VECTOR_CLEAR(¶ms, v);