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 self->expression.outl = NULL;
62 self->expression.outr = NULL;
63 MEM_VECTOR_INIT(&self->expression, params);
66 static void ast_expression_delete(ast_expression *self)
69 if (self->expression.next)
70 ast_delete(self->expression.next);
71 for (i = 0; i < self->expression.params_count; ++i) {
72 ast_delete(self->expression.params[i]);
74 MEM_VECTOR_CLEAR(&self->expression, params);
77 static void ast_expression_delete_full(ast_expression *self)
79 ast_expression_delete(self);
83 MEM_VEC_FUNCTIONS(ast_expression_common, ast_value*, params)
85 static ast_expression* ast_type_copy(lex_ctx ctx, const ast_expression *ex);
86 static ast_value* ast_value_copy(const ast_value *self)
88 ast_value *cp = ast_value_new(self->expression.node.context, self->name, self->expression.vtype);
89 if (self->expression.next) {
90 cp->expression.next = ast_type_copy(self->expression.node.context, self->expression.next);
91 if (!cp->expression.next) {
99 static ast_expression* ast_shallow_type(lex_ctx ctx, int vtype)
101 ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
102 self->expression.codegen = NULL;
103 self->expression.next = NULL;
104 self->expression.vtype = vtype;
108 static ast_expression* ast_type_copy(lex_ctx ctx, const ast_expression *ex)
111 const ast_expression_common *fromex;
112 ast_expression_common *selfex;
118 ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
120 fromex = &ex->expression;
121 selfex = &self->expression;
123 /* This may never be codegen()d */
124 selfex->codegen = NULL;
126 selfex->vtype = fromex->vtype;
129 selfex->next = ast_type_copy(ctx, fromex->next);
131 ast_expression_delete_full(self);
138 for (i = 0; i < fromex->params_count; ++i) {
139 ast_value *v = ast_value_copy(fromex->params[i]);
140 if (!v || !ast_expression_common_params_add(selfex, v)) {
141 ast_expression_delete_full(self);
150 ast_value* ast_value_new(lex_ctx ctx, const char *name, int t)
152 ast_instantiate(ast_value, ctx, ast_value_delete);
153 ast_expression_init((ast_expression*)self,
154 (ast_expression_codegen*)&ast_value_codegen);
155 self->expression.node.keep = true; /* keep */
157 self->name = name ? util_strdup(name) : NULL;
158 self->expression.vtype = t;
159 self->expression.next = NULL;
160 self->isconst = false;
161 memset(&self->constval, 0, sizeof(self->constval));
168 void ast_value_delete(ast_value* self)
171 mem_d((void*)self->name);
173 switch (self->expression.vtype)
176 mem_d((void*)self->constval.vstring);
179 /* unlink us from the function node */
180 self->constval.vfunc->vtype = NULL;
182 /* NOTE: delete function? currently collected in
183 * the parser structure
189 ast_expression_delete((ast_expression*)self);
193 bool GMQCC_WARN ast_value_params_add(ast_value *self, ast_value *p)
195 return ast_expression_common_params_add(&self->expression, p);
198 bool ast_value_set_name(ast_value *self, const char *name)
201 mem_d((void*)self->name);
202 self->name = util_strdup(name);
206 ast_binary* ast_binary_new(lex_ctx ctx, int op,
207 ast_expression* left, ast_expression* right)
209 ast_instantiate(ast_binary, ctx, ast_binary_delete);
210 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binary_codegen);
216 if (op >= INSTR_EQ_F && op <= INSTR_GT)
217 self->expression.vtype = TYPE_FLOAT;
218 else if (op == INSTR_AND || op == INSTR_OR ||
219 op == INSTR_BITAND || op == INSTR_BITOR)
220 self->expression.vtype = TYPE_FLOAT;
221 else if (op == INSTR_MUL_VF || op == INSTR_MUL_FV)
222 self->expression.vtype = TYPE_VECTOR;
223 else if (op == INSTR_MUL_V)
224 self->expression.vtype = TYPE_FLOAT;
226 self->expression.vtype = left->expression.vtype;
231 void ast_binary_delete(ast_binary *self)
233 ast_unref(self->left);
234 ast_unref(self->right);
235 ast_expression_delete((ast_expression*)self);
239 ast_unary* ast_unary_new(lex_ctx ctx, int op,
240 ast_expression *expr)
242 ast_instantiate(ast_unary, ctx, ast_unary_delete);
243 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_unary_codegen);
246 self->operand = expr;
251 void ast_unary_delete(ast_unary *self)
253 ast_unref(self->operand);
254 ast_expression_delete((ast_expression*)self);
258 ast_return* ast_return_new(lex_ctx ctx, ast_expression *expr)
260 ast_instantiate(ast_return, ctx, ast_return_delete);
261 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_return_codegen);
263 self->operand = expr;
268 void ast_return_delete(ast_return *self)
270 ast_unref(self->operand);
271 ast_expression_delete((ast_expression*)self);
275 ast_entfield* ast_entfield_new(lex_ctx ctx, ast_expression *entity, ast_expression *field)
277 const ast_expression *outtype;
279 ast_instantiate(ast_entfield, ctx, ast_entfield_delete);
281 if (field->expression.vtype != TYPE_FIELD) {
286 outtype = field->expression.next;
289 /* Error: field has no type... */
293 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_entfield_codegen);
295 self->expression.vtype = outtype->expression.vtype;
296 self->expression.next = ast_type_copy(ctx, outtype->expression.next);
298 self->entity = entity;
304 void ast_entfield_delete(ast_entfield *self)
306 ast_unref(self->entity);
307 ast_unref(self->field);
308 ast_expression_delete((ast_expression*)self);
312 ast_member* ast_member_new(lex_ctx ctx, ast_expression *owner, unsigned int field)
314 ast_instantiate(ast_member, ctx, ast_member_delete);
320 if (owner->expression.vtype != TYPE_VECTOR &&
321 owner->expression.vtype != TYPE_FIELD) {
322 printf("ast_member on an invalid owner of type %i\n", (int)owner->expression.vtype);
327 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_member_codegen);
329 if (owner->expression.vtype == TYPE_VECTOR) {
330 self->expression.vtype = TYPE_FLOAT;
331 self->expression.next = NULL;
333 self->expression.vtype = TYPE_FIELD;
334 self->expression.next = ast_shallow_type(ctx, TYPE_FLOAT);
343 void ast_member_delete(ast_member *self)
345 ast_unref(self->owner);
346 ast_expression_delete((ast_expression*)self);
350 ast_ifthen* ast_ifthen_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
352 ast_instantiate(ast_ifthen, ctx, ast_ifthen_delete);
353 if (!ontrue && !onfalse) {
354 /* because it is invalid */
358 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ifthen_codegen);
361 self->on_true = ontrue;
362 self->on_false = onfalse;
367 void ast_ifthen_delete(ast_ifthen *self)
369 ast_unref(self->cond);
371 ast_unref(self->on_true);
373 ast_unref(self->on_false);
374 ast_expression_delete((ast_expression*)self);
378 ast_ternary* ast_ternary_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
380 ast_instantiate(ast_ternary, ctx, ast_ternary_delete);
381 /* This time NEITHER must be NULL */
382 if (!ontrue || !onfalse) {
386 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ternary_codegen);
389 self->on_true = ontrue;
390 self->on_false = onfalse;
391 self->phi_out = NULL;
396 void ast_ternary_delete(ast_ternary *self)
398 ast_unref(self->cond);
399 ast_unref(self->on_true);
400 ast_unref(self->on_false);
401 ast_expression_delete((ast_expression*)self);
405 ast_loop* ast_loop_new(lex_ctx ctx,
406 ast_expression *initexpr,
407 ast_expression *precond,
408 ast_expression *postcond,
409 ast_expression *increment,
410 ast_expression *body)
412 ast_instantiate(ast_loop, ctx, ast_loop_delete);
413 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_loop_codegen);
415 self->initexpr = initexpr;
416 self->precond = precond;
417 self->postcond = postcond;
418 self->increment = increment;
424 void ast_loop_delete(ast_loop *self)
427 ast_unref(self->initexpr);
429 ast_unref(self->precond);
431 ast_unref(self->postcond);
433 ast_unref(self->increment);
435 ast_unref(self->body);
436 ast_expression_delete((ast_expression*)self);
440 ast_call* ast_call_new(lex_ctx ctx,
441 ast_expression *funcexpr)
443 ast_instantiate(ast_call, ctx, ast_call_delete);
444 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_call_codegen);
446 MEM_VECTOR_INIT(self, params);
448 self->func = funcexpr;
452 MEM_VEC_FUNCTIONS(ast_call, ast_expression*, params)
454 void ast_call_delete(ast_call *self)
457 for (i = 0; i < self->params_count; ++i)
458 ast_unref(self->params[i]);
459 MEM_VECTOR_CLEAR(self, params);
462 ast_unref(self->func);
464 ast_expression_delete((ast_expression*)self);
468 ast_store* ast_store_new(lex_ctx ctx, int op,
469 ast_expression *dest, ast_expression *source)
471 ast_instantiate(ast_store, ctx, ast_store_delete);
472 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_store_codegen);
476 self->source = source;
481 void ast_store_delete(ast_store *self)
483 ast_unref(self->dest);
484 ast_unref(self->source);
485 ast_expression_delete((ast_expression*)self);
489 ast_block* ast_block_new(lex_ctx ctx)
491 ast_instantiate(ast_block, ctx, ast_block_delete);
492 ast_expression_init((ast_expression*)self,
493 (ast_expression_codegen*)&ast_block_codegen);
495 MEM_VECTOR_INIT(self, locals);
496 MEM_VECTOR_INIT(self, exprs);
500 MEM_VEC_FUNCTIONS(ast_block, ast_value*, locals)
501 MEM_VEC_FUNCTIONS(ast_block, ast_expression*, exprs)
503 void ast_block_delete(ast_block *self)
506 for (i = 0; i < self->exprs_count; ++i)
507 ast_unref(self->exprs[i]);
508 MEM_VECTOR_CLEAR(self, exprs);
509 for (i = 0; i < self->locals_count; ++i)
510 ast_delete(self->locals[i]);
511 MEM_VECTOR_CLEAR(self, locals);
512 ast_expression_delete((ast_expression*)self);
516 bool ast_block_set_type(ast_block *self, ast_expression *from)
518 if (self->expression.next)
519 ast_delete(self->expression.next);
520 self->expression.vtype = from->expression.vtype;
521 if (from->expression.next) {
522 self->expression.next = ast_type_copy(self->expression.node.context, from->expression.next);
523 if (!self->expression.next)
529 ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype)
531 ast_instantiate(ast_function, ctx, ast_function_delete);
535 vtype->expression.vtype != TYPE_FUNCTION)
542 self->name = name ? util_strdup(name) : NULL;
543 MEM_VECTOR_INIT(self, blocks);
545 self->labelcount = 0;
548 self->ir_func = NULL;
549 self->curblock = NULL;
551 self->breakblock = NULL;
552 self->continueblock = NULL;
554 vtype->isconst = true;
555 vtype->constval.vfunc = self;
560 MEM_VEC_FUNCTIONS(ast_function, ast_block*, blocks)
562 void ast_function_delete(ast_function *self)
566 mem_d((void*)self->name);
568 /* ast_value_delete(self->vtype); */
569 self->vtype->isconst = false;
570 self->vtype->constval.vfunc = NULL;
571 /* We use unref - if it was stored in a global table it is supposed
572 * to be deleted from *there*
574 ast_unref(self->vtype);
576 for (i = 0; i < self->blocks_count; ++i)
577 ast_delete(self->blocks[i]);
578 MEM_VECTOR_CLEAR(self, blocks);
582 static void ast_util_hexitoa(char *buf, size_t size, unsigned int num)
584 unsigned int base = 10;
585 #define checknul() do { if (size == 1) { *buf = 0; return; } } while (0)
586 #define addch(x) do { *buf++ = (x); --size; checknul(); } while (0)
595 int digit = num % base;
606 const char* ast_function_label(ast_function *self, const char *prefix)
608 size_t id = (self->labelcount++);
609 size_t len = strlen(prefix);
610 strncpy(self->labelbuf, prefix, sizeof(self->labelbuf));
611 ast_util_hexitoa(self->labelbuf + len, sizeof(self->labelbuf)-len, id);
612 return self->labelbuf;
615 /*********************************************************************/
617 * by convention you must never pass NULL to the 'ir_value **out'
618 * parameter. If you really don't care about the output, pass a dummy.
619 * But I can't imagine a pituation where the output is truly unnecessary.
622 bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out)
624 /* NOTE: This is the codegen for a variable used in an expression.
625 * It is not the codegen to generate the value. For this purpose,
626 * ast_local_codegen and ast_global_codegen are to be used before this
627 * is executed. ast_function_codegen should take care of its locals,
628 * and the ast-user should take care of ast_global_codegen to be used
629 * on all the globals.
632 printf("ast_value used before generated (%s)\n", self->name);
639 bool ast_global_codegen(ast_value *self, ir_builder *ir)
642 if (self->isconst && self->expression.vtype == TYPE_FUNCTION)
644 ir_function *func = ir_builder_create_function(ir, self->name, self->expression.next->expression.vtype);
648 self->constval.vfunc->ir_func = func;
649 self->ir_v = func->value;
650 /* The function is filled later on ast_function_codegen... */
654 if (self->expression.vtype == TYPE_FIELD) {
655 v = ir_builder_create_field(ir, self->name, self->expression.next->expression.vtype);
659 printf("TODO: constant field pointers with value\n");
666 v = ir_builder_create_global(ir, self->name, self->expression.vtype);
668 printf("ir_builder_create_global failed\n");
673 switch (self->expression.vtype)
676 if (!ir_value_set_float(v, self->constval.vfloat))
680 if (!ir_value_set_vector(v, self->constval.vvec))
684 if (!ir_value_set_string(v, self->constval.vstring))
688 printf("global of type function not properly generated\n");
690 /* Cannot generate an IR value for a function,
691 * need a pointer pointing to a function rather.
694 printf("TODO: global constant type %i\n", self->expression.vtype);
699 /* link us to the ir_value */
703 error: /* clean up */
708 bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
711 if (self->isconst && self->expression.vtype == TYPE_FUNCTION)
713 /* Do we allow local functions? I think not...
714 * this is NOT a function pointer atm.
719 v = ir_function_create_local(func, self->name, self->expression.vtype, param);
723 /* A constant local... hmmm...
724 * I suppose the IR will have to deal with this
727 switch (self->expression.vtype)
730 if (!ir_value_set_float(v, self->constval.vfloat))
734 if (!ir_value_set_vector(v, self->constval.vvec))
738 if (!ir_value_set_string(v, self->constval.vstring))
742 printf("TODO: global constant type %i\n", self->expression.vtype);
747 /* link us to the ir_value */
751 error: /* clean up */
756 bool ast_function_codegen(ast_function *self, ir_builder *ir)
760 ast_expression_common *ec;
765 printf("ast_function's related ast_value was not generated yet\n");
769 /* fill the parameter list */
770 ec = &self->vtype->expression;
771 for (i = 0; i < ec->params_count; ++i)
773 if (!ir_function_params_add(irf, ec->params[i]->expression.vtype))
775 if (!self->builtin) {
776 if (!ast_local_codegen(ec->params[i], self->ir_func, true))
782 irf->builtin = self->builtin;
786 self->curblock = ir_function_create_block(irf, "entry");
790 for (i = 0; i < self->blocks_count; ++i) {
791 ast_expression_codegen *gen = self->blocks[i]->expression.codegen;
792 if (!(*gen)((ast_expression*)self->blocks[i], self, false, &dummy))
796 /* TODO: check return types */
797 if (!self->curblock->is_return)
799 if (!self->vtype->expression.next ||
800 self->vtype->expression.next->expression.vtype == TYPE_VOID)
802 return ir_block_create_return(self->curblock, NULL);
806 /* error("missing return"); */
813 /* Note, you will not see ast_block_codegen generate ir_blocks.
814 * To the AST and the IR, blocks are 2 different things.
815 * In the AST it represents a block of code, usually enclosed in
816 * curly braces {...}.
817 * While in the IR it represents a block in terms of control-flow.
819 bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_value **out)
824 * Note: an ast-representation using the comma-operator
825 * of the form: (a, b, c) = x should not assign to c...
829 /* output is NULL at first, we'll have each expression
830 * assign to out output, thus, a comma-operator represention
831 * using an ast_block will return the last generated value,
832 * so: (b, c) + a executed both b and c, and returns c,
833 * which is then added to a.
837 /* generate locals */
838 for (i = 0; i < self->locals_count; ++i)
840 if (!ast_local_codegen(self->locals[i], func->ir_func, false))
844 for (i = 0; i < self->exprs_count; ++i)
846 ast_expression_codegen *gen = self->exprs[i]->expression.codegen;
847 if (!(*gen)(self->exprs[i], func, false, out))
854 bool ast_store_codegen(ast_store *self, ast_function *func, bool lvalue, ir_value **out)
856 ast_expression_codegen *cgen;
857 ir_value *left, *right;
859 cgen = self->dest->expression.codegen;
861 if (!(*cgen)((ast_expression*)(self->dest), func, true, &left))
864 cgen = self->source->expression.codegen;
866 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
869 if (!ir_block_create_store_op(func->curblock, self->op, left, right))
872 /* Theoretically, an assinment returns its left side as an
873 * lvalue, if we don't need an lvalue though, we return
874 * the right side as an rvalue, otherwise we have to
875 * somehow know whether or not we need to dereference the pointer
876 * on the left side - that is: OP_LOAD if it was an address.
877 * Also: in original QC we cannot OP_LOADP *anyway*.
879 *out = (lvalue ? left : right);
884 bool ast_binary_codegen(ast_binary *self, ast_function *func, bool lvalue, ir_value **out)
886 ast_expression_codegen *cgen;
887 ir_value *left, *right;
889 /* In the context of a binary operation, we can disregard
894 cgen = self->left->expression.codegen;
896 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
899 cgen = self->right->expression.codegen;
901 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
904 *out = ir_block_create_binop(func->curblock, ast_function_label(func, "bin"),
905 self->op, left, right);
912 bool ast_unary_codegen(ast_unary *self, ast_function *func, bool lvalue, ir_value **out)
914 ast_expression_codegen *cgen;
917 /* In the context of a unary operation, we can disregard
922 cgen = self->operand->expression.codegen;
924 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
927 *out = ir_block_create_unary(func->curblock, ast_function_label(func, "unary"),
935 bool ast_return_codegen(ast_return *self, ast_function *func, bool lvalue, ir_value **out)
937 ast_expression_codegen *cgen;
940 /* In the context of a return operation, we can disregard
945 cgen = self->operand->expression.codegen;
947 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
950 if (!ir_block_create_return(func->curblock, operand))
956 bool ast_entfield_codegen(ast_entfield *self, ast_function *func, bool lvalue, ir_value **out)
958 ast_expression_codegen *cgen;
959 ir_value *ent, *field;
961 /* This function needs to take the 'lvalue' flag into account!
962 * As lvalue we provide a field-pointer, as rvalue we provide the
966 cgen = self->entity->expression.codegen;
967 if (!(*cgen)((ast_expression*)(self->entity), func, false, &ent))
970 cgen = self->field->expression.codegen;
971 if (!(*cgen)((ast_expression*)(self->field), func, false, &field))
976 *out = ir_block_create_fieldaddress(func->curblock, ast_function_label(func, "efa"),
979 *out = ir_block_create_load_from_ent(func->curblock, ast_function_label(func, "efv"),
980 ent, field, self->expression.vtype);
985 /* Hm that should be it... */
989 bool ast_member_codegen(ast_member *self, ast_function *func, bool lvalue, ir_value **out)
991 ast_expression_codegen *cgen;
994 cgen = self->owner->expression.codegen;
995 if (!(*cgen)((ast_expression*)(self->owner), func, true, &vec))
998 if (vec->vtype != TYPE_VECTOR &&
999 !(vec->vtype == TYPE_FIELD && self->owner->expression.next->expression.vtype == TYPE_VECTOR))
1004 *out = ir_value_vector_member(vec, self->field);
1006 return (*out != NULL);
1009 bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_value **out)
1011 ast_expression_codegen *cgen;
1016 ir_block *cond = func->curblock;
1021 /* We don't output any value, thus also don't care about r/lvalue */
1025 /* generate the condition */
1026 func->curblock = cond;
1027 cgen = self->cond->expression.codegen;
1028 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
1033 if (self->on_true) {
1034 /* create on-true block */
1035 ontrue = ir_function_create_block(func->ir_func, ast_function_label(func, "ontrue"));
1039 /* enter the block */
1040 func->curblock = ontrue;
1043 cgen = self->on_true->expression.codegen;
1044 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &dummy))
1050 if (self->on_false) {
1051 /* create on-false block */
1052 onfalse = ir_function_create_block(func->ir_func, ast_function_label(func, "onfalse"));
1056 /* enter the block */
1057 func->curblock = onfalse;
1060 cgen = self->on_false->expression.codegen;
1061 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &dummy))
1066 /* Merge block were they all merge in to */
1067 merge = ir_function_create_block(func->ir_func, ast_function_label(func, "endif"));
1071 /* add jumps ot the merge block */
1072 if (ontrue && !ir_block_create_jump(ontrue, merge))
1074 if (onfalse && !ir_block_create_jump(onfalse, merge))
1077 /* we create the if here, that way all blocks are ordered :)
1079 if (!ir_block_create_if(cond, condval,
1080 (ontrue ? ontrue : merge),
1081 (onfalse ? onfalse : merge)))
1086 /* Now enter the merge block */
1087 func->curblock = merge;
1092 bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_value **out)
1094 ast_expression_codegen *cgen;
1097 ir_value *trueval, *falseval;
1100 ir_block *cond = func->curblock;
1105 /* In theory it shouldn't be possible to pass through a node twice, but
1106 * in case we add any kind of optimization pass for the AST itself, it
1107 * may still happen, thus we remember a created ir_value and simply return one
1108 * if it already exists.
1110 if (self->phi_out) {
1111 *out = self->phi_out;
1115 /* Ternary can never create an lvalue... */
1119 /* In the following, contraty to ast_ifthen, we assume both paths exist. */
1121 /* generate the condition */
1122 func->curblock = cond;
1123 cgen = self->cond->expression.codegen;
1124 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
1127 /* create on-true block */
1128 ontrue = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_T"));
1133 /* enter the block */
1134 func->curblock = ontrue;
1137 cgen = self->on_true->expression.codegen;
1138 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &trueval))
1142 /* create on-false block */
1143 onfalse = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_F"));
1148 /* enter the block */
1149 func->curblock = onfalse;
1152 cgen = self->on_false->expression.codegen;
1153 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &falseval))
1157 /* create merge block */
1158 merge = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_out"));
1161 /* jump to merge block */
1162 if (!ir_block_create_jump(ontrue, merge))
1164 if (!ir_block_create_jump(onfalse, merge))
1167 /* create if instruction */
1168 if (!ir_block_create_if(cond, condval, ontrue, onfalse))
1171 /* Now enter the merge block */
1172 func->curblock = merge;
1174 /* Here, now, we need a PHI node
1175 * but first some sanity checking...
1177 if (trueval->vtype != falseval->vtype) {
1178 /* error("ternary with different types on the two sides"); */
1183 phi = ir_block_create_phi(merge, ast_function_label(func, "phi"), trueval->vtype);
1185 !ir_phi_add(phi, ontrue, trueval) ||
1186 !ir_phi_add(phi, onfalse, falseval))
1191 self->phi_out = ir_phi_value(phi);
1192 *out = self->phi_out;
1197 bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value **out)
1199 ast_expression_codegen *cgen;
1201 ir_value *dummy = NULL;
1202 ir_value *precond = NULL;
1203 ir_value *postcond = NULL;
1205 /* Since we insert some jumps "late" so we have blocks
1206 * ordered "nicely", we need to keep track of the actual end-blocks
1207 * of expressions to add the jumps to.
1209 ir_block *bbody = NULL, *end_bbody = NULL;
1210 ir_block *bprecond = NULL, *end_bprecond = NULL;
1211 ir_block *bpostcond = NULL, *end_bpostcond = NULL;
1212 ir_block *bincrement = NULL, *end_bincrement = NULL;
1213 ir_block *bout = NULL, *bin = NULL;
1215 /* let's at least move the outgoing block to the end */
1218 /* 'break' and 'continue' need to be able to find the right blocks */
1219 ir_block *bcontinue = NULL;
1220 ir_block *bbreak = NULL;
1222 ir_block *old_bcontinue = NULL;
1223 ir_block *old_bbreak = NULL;
1225 ir_block *tmpblock = NULL;
1231 * Should we ever need some kind of block ordering, better make this function
1232 * move blocks around than write a block ordering algorithm later... after all
1233 * the ast and ir should work together, not against each other.
1236 /* initexpr doesn't get its own block, it's pointless, it could create more blocks
1237 * anyway if for example it contains a ternary.
1241 cgen = self->initexpr->expression.codegen;
1242 if (!(*cgen)((ast_expression*)(self->initexpr), func, false, &dummy))
1246 /* Store the block from which we enter this chaos */
1247 bin = func->curblock;
1249 /* The pre-loop condition needs its own block since we
1250 * need to be able to jump to the start of that expression.
1254 bprecond = ir_function_create_block(func->ir_func, ast_function_label(func, "pre_loop_cond"));
1258 /* the pre-loop-condition the least important place to 'continue' at */
1259 bcontinue = bprecond;
1262 func->curblock = bprecond;
1265 cgen = self->precond->expression.codegen;
1266 if (!(*cgen)((ast_expression*)(self->precond), func, false, &precond))
1269 end_bprecond = func->curblock;
1271 bprecond = end_bprecond = NULL;
1274 /* Now the next blocks won't be ordered nicely, but we need to
1275 * generate them this early for 'break' and 'continue'.
1277 if (self->increment) {
1278 bincrement = ir_function_create_block(func->ir_func, ast_function_label(func, "loop_increment"));
1281 bcontinue = bincrement; /* increment comes before the pre-loop-condition */
1283 bincrement = end_bincrement = NULL;
1286 if (self->postcond) {
1287 bpostcond = ir_function_create_block(func->ir_func, ast_function_label(func, "post_loop_cond"));
1290 bcontinue = bpostcond; /* postcond comes before the increment */
1292 bpostcond = end_bpostcond = NULL;
1295 bout_id = func->ir_func->blocks_count;
1296 bout = ir_function_create_block(func->ir_func, ast_function_label(func, "after_loop"));
1301 /* The loop body... */
1304 bbody = ir_function_create_block(func->ir_func, ast_function_label(func, "loop_body"));
1309 func->curblock = bbody;
1311 old_bbreak = func->breakblock;
1312 old_bcontinue = func->continueblock;
1313 func->breakblock = bbreak;
1314 func->continueblock = bcontinue;
1317 cgen = self->body->expression.codegen;
1318 if (!(*cgen)((ast_expression*)(self->body), func, false, &dummy))
1321 end_bbody = func->curblock;
1322 func->breakblock = old_bbreak;
1323 func->continueblock = old_bcontinue;
1326 /* post-loop-condition */
1330 func->curblock = bpostcond;
1333 cgen = self->postcond->expression.codegen;
1334 if (!(*cgen)((ast_expression*)(self->postcond), func, false, &postcond))
1337 end_bpostcond = func->curblock;
1340 /* The incrementor */
1341 if (self->increment)
1344 func->curblock = bincrement;
1347 cgen = self->increment->expression.codegen;
1348 if (!(*cgen)((ast_expression*)(self->increment), func, false, &dummy))
1351 end_bincrement = func->curblock;
1354 /* In any case now, we continue from the outgoing block */
1355 func->curblock = bout;
1357 /* Now all blocks are in place */
1358 /* From 'bin' we jump to whatever comes first */
1359 if (bprecond) tmpblock = bprecond;
1360 else if (bbody) tmpblock = bbody;
1361 else if (bpostcond) tmpblock = bpostcond;
1362 else tmpblock = bout;
1363 if (!ir_block_create_jump(bin, tmpblock))
1369 ir_block *ontrue, *onfalse;
1370 if (bbody) ontrue = bbody;
1371 else if (bincrement) ontrue = bincrement;
1372 else if (bpostcond) ontrue = bpostcond;
1373 else ontrue = bprecond;
1375 if (!ir_block_create_if(end_bprecond, precond, ontrue, onfalse))
1382 if (bincrement) tmpblock = bincrement;
1383 else if (bpostcond) tmpblock = bpostcond;
1384 else if (bprecond) tmpblock = bprecond;
1385 else tmpblock = bout;
1386 if (!ir_block_create_jump(end_bbody, tmpblock))
1390 /* from increment */
1393 if (bpostcond) tmpblock = bpostcond;
1394 else if (bprecond) tmpblock = bprecond;
1395 else if (bbody) tmpblock = bbody;
1396 else tmpblock = bout;
1397 if (!ir_block_create_jump(end_bincrement, tmpblock))
1404 ir_block *ontrue, *onfalse;
1405 if (bprecond) ontrue = bprecond;
1406 else if (bbody) ontrue = bbody;
1407 else if (bincrement) ontrue = bincrement;
1408 else ontrue = bpostcond;
1410 if (!ir_block_create_if(end_bpostcond, postcond, ontrue, onfalse))
1414 /* Move 'bout' to the end */
1415 if (!ir_function_blocks_remove(func->ir_func, bout_id) ||
1416 !ir_function_blocks_add(func->ir_func, bout))
1418 ir_block_delete(bout);
1425 bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value **out)
1427 ast_expression_codegen *cgen;
1428 ir_value_vector params;
1429 ir_instr *callinstr;
1432 ir_value *funval = NULL;
1434 /* return values are never rvalues */
1437 cgen = self->func->expression.codegen;
1438 if (!(*cgen)((ast_expression*)(self->func), func, false, &funval))
1443 MEM_VECTOR_INIT(¶ms, v);
1446 for (i = 0; i < self->params_count; ++i)
1449 ast_expression *expr = self->params[i];
1451 cgen = expr->expression.codegen;
1452 if (!(*cgen)(expr, func, false, ¶m))
1456 if (!ir_value_vector_v_add(¶ms, param))
1460 callinstr = ir_block_create_call(func->curblock, ast_function_label(func, "call"), funval);
1464 for (i = 0; i < params.v_count; ++i) {
1465 if (!ir_call_param(callinstr, params.v[i]))
1469 *out = ir_call_value(callinstr);
1471 MEM_VECTOR_CLEAR(¶ms, v);
1474 MEM_VECTOR_CLEAR(¶ms, v);