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_binstore* ast_binstore_new(lex_ctx ctx, int storop, int op,
240 ast_expression* left, ast_expression* right)
242 ast_instantiate(ast_binstore, ctx, ast_binstore_delete);
243 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binstore_codegen);
245 self->opstore = storop;
248 self->source = right;
250 self->expression.vtype = left->expression.vtype;
251 if (left->expression.next) {
252 self->expression.next = ast_type_copy(ctx, left);
253 if (!self->expression.next) {
259 self->expression.next = NULL;
264 void ast_binstore_delete(ast_binstore *self)
266 ast_unref(self->dest);
267 ast_unref(self->source);
268 ast_expression_delete((ast_expression*)self);
272 ast_unary* ast_unary_new(lex_ctx ctx, int op,
273 ast_expression *expr)
275 ast_instantiate(ast_unary, ctx, ast_unary_delete);
276 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_unary_codegen);
279 self->operand = expr;
284 void ast_unary_delete(ast_unary *self)
286 ast_unref(self->operand);
287 ast_expression_delete((ast_expression*)self);
291 ast_return* ast_return_new(lex_ctx ctx, ast_expression *expr)
293 ast_instantiate(ast_return, ctx, ast_return_delete);
294 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_return_codegen);
296 self->operand = expr;
301 void ast_return_delete(ast_return *self)
303 ast_unref(self->operand);
304 ast_expression_delete((ast_expression*)self);
308 ast_entfield* ast_entfield_new(lex_ctx ctx, ast_expression *entity, ast_expression *field)
310 const ast_expression *outtype;
312 ast_instantiate(ast_entfield, ctx, ast_entfield_delete);
314 if (field->expression.vtype != TYPE_FIELD) {
319 outtype = field->expression.next;
322 /* Error: field has no type... */
326 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_entfield_codegen);
328 self->expression.vtype = outtype->expression.vtype;
329 self->expression.next = ast_type_copy(ctx, outtype->expression.next);
331 self->entity = entity;
337 void ast_entfield_delete(ast_entfield *self)
339 ast_unref(self->entity);
340 ast_unref(self->field);
341 ast_expression_delete((ast_expression*)self);
345 ast_member* ast_member_new(lex_ctx ctx, ast_expression *owner, unsigned int field)
347 ast_instantiate(ast_member, ctx, ast_member_delete);
353 if (owner->expression.vtype != TYPE_VECTOR &&
354 owner->expression.vtype != TYPE_FIELD) {
355 printf("ast_member on an invalid owner of type %i\n", (int)owner->expression.vtype);
360 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_member_codegen);
362 if (owner->expression.vtype == TYPE_VECTOR) {
363 self->expression.vtype = TYPE_FLOAT;
364 self->expression.next = NULL;
366 self->expression.vtype = TYPE_FIELD;
367 self->expression.next = ast_shallow_type(ctx, TYPE_FLOAT);
376 void ast_member_delete(ast_member *self)
378 ast_unref(self->owner);
379 ast_expression_delete((ast_expression*)self);
383 ast_ifthen* ast_ifthen_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
385 ast_instantiate(ast_ifthen, ctx, ast_ifthen_delete);
386 if (!ontrue && !onfalse) {
387 /* because it is invalid */
391 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ifthen_codegen);
394 self->on_true = ontrue;
395 self->on_false = onfalse;
400 void ast_ifthen_delete(ast_ifthen *self)
402 ast_unref(self->cond);
404 ast_unref(self->on_true);
406 ast_unref(self->on_false);
407 ast_expression_delete((ast_expression*)self);
411 ast_ternary* ast_ternary_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
413 ast_instantiate(ast_ternary, ctx, ast_ternary_delete);
414 /* This time NEITHER must be NULL */
415 if (!ontrue || !onfalse) {
419 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ternary_codegen);
422 self->on_true = ontrue;
423 self->on_false = onfalse;
424 self->phi_out = NULL;
429 void ast_ternary_delete(ast_ternary *self)
431 ast_unref(self->cond);
432 ast_unref(self->on_true);
433 ast_unref(self->on_false);
434 ast_expression_delete((ast_expression*)self);
438 ast_loop* ast_loop_new(lex_ctx ctx,
439 ast_expression *initexpr,
440 ast_expression *precond,
441 ast_expression *postcond,
442 ast_expression *increment,
443 ast_expression *body)
445 ast_instantiate(ast_loop, ctx, ast_loop_delete);
446 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_loop_codegen);
448 self->initexpr = initexpr;
449 self->precond = precond;
450 self->postcond = postcond;
451 self->increment = increment;
457 void ast_loop_delete(ast_loop *self)
460 ast_unref(self->initexpr);
462 ast_unref(self->precond);
464 ast_unref(self->postcond);
466 ast_unref(self->increment);
468 ast_unref(self->body);
469 ast_expression_delete((ast_expression*)self);
473 ast_call* ast_call_new(lex_ctx ctx,
474 ast_expression *funcexpr)
476 ast_instantiate(ast_call, ctx, ast_call_delete);
477 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_call_codegen);
479 MEM_VECTOR_INIT(self, params);
481 self->func = funcexpr;
485 MEM_VEC_FUNCTIONS(ast_call, ast_expression*, params)
487 void ast_call_delete(ast_call *self)
490 for (i = 0; i < self->params_count; ++i)
491 ast_unref(self->params[i]);
492 MEM_VECTOR_CLEAR(self, params);
495 ast_unref(self->func);
497 ast_expression_delete((ast_expression*)self);
501 ast_store* ast_store_new(lex_ctx ctx, int op,
502 ast_expression *dest, ast_expression *source)
504 ast_instantiate(ast_store, ctx, ast_store_delete);
505 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_store_codegen);
509 self->source = source;
514 void ast_store_delete(ast_store *self)
516 ast_unref(self->dest);
517 ast_unref(self->source);
518 ast_expression_delete((ast_expression*)self);
522 ast_block* ast_block_new(lex_ctx ctx)
524 ast_instantiate(ast_block, ctx, ast_block_delete);
525 ast_expression_init((ast_expression*)self,
526 (ast_expression_codegen*)&ast_block_codegen);
528 MEM_VECTOR_INIT(self, locals);
529 MEM_VECTOR_INIT(self, exprs);
533 MEM_VEC_FUNCTIONS(ast_block, ast_value*, locals)
534 MEM_VEC_FUNCTIONS(ast_block, ast_expression*, exprs)
536 void ast_block_delete(ast_block *self)
539 for (i = 0; i < self->exprs_count; ++i)
540 ast_unref(self->exprs[i]);
541 MEM_VECTOR_CLEAR(self, exprs);
542 for (i = 0; i < self->locals_count; ++i)
543 ast_delete(self->locals[i]);
544 MEM_VECTOR_CLEAR(self, locals);
545 ast_expression_delete((ast_expression*)self);
549 bool ast_block_set_type(ast_block *self, ast_expression *from)
551 if (self->expression.next)
552 ast_delete(self->expression.next);
553 self->expression.vtype = from->expression.vtype;
554 if (from->expression.next) {
555 self->expression.next = ast_type_copy(self->expression.node.context, from->expression.next);
556 if (!self->expression.next)
562 ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype)
564 ast_instantiate(ast_function, ctx, ast_function_delete);
568 vtype->expression.vtype != TYPE_FUNCTION)
575 self->name = name ? util_strdup(name) : NULL;
576 MEM_VECTOR_INIT(self, blocks);
578 self->labelcount = 0;
581 self->ir_func = NULL;
582 self->curblock = NULL;
584 self->breakblock = NULL;
585 self->continueblock = NULL;
587 vtype->isconst = true;
588 vtype->constval.vfunc = self;
593 MEM_VEC_FUNCTIONS(ast_function, ast_block*, blocks)
595 void ast_function_delete(ast_function *self)
599 mem_d((void*)self->name);
601 /* ast_value_delete(self->vtype); */
602 self->vtype->isconst = false;
603 self->vtype->constval.vfunc = NULL;
604 /* We use unref - if it was stored in a global table it is supposed
605 * to be deleted from *there*
607 ast_unref(self->vtype);
609 for (i = 0; i < self->blocks_count; ++i)
610 ast_delete(self->blocks[i]);
611 MEM_VECTOR_CLEAR(self, blocks);
615 static void ast_util_hexitoa(char *buf, size_t size, unsigned int num)
617 unsigned int base = 10;
618 #define checknul() do { if (size == 1) { *buf = 0; return; } } while (0)
619 #define addch(x) do { *buf++ = (x); --size; checknul(); } while (0)
628 int digit = num % base;
639 const char* ast_function_label(ast_function *self, const char *prefix)
641 size_t id = (self->labelcount++);
642 size_t len = strlen(prefix);
643 strncpy(self->labelbuf, prefix, sizeof(self->labelbuf));
644 ast_util_hexitoa(self->labelbuf + len, sizeof(self->labelbuf)-len, id);
645 return self->labelbuf;
648 /*********************************************************************/
650 * by convention you must never pass NULL to the 'ir_value **out'
651 * parameter. If you really don't care about the output, pass a dummy.
652 * But I can't imagine a pituation where the output is truly unnecessary.
655 bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out)
657 /* NOTE: This is the codegen for a variable used in an expression.
658 * It is not the codegen to generate the value. For this purpose,
659 * ast_local_codegen and ast_global_codegen are to be used before this
660 * is executed. ast_function_codegen should take care of its locals,
661 * and the ast-user should take care of ast_global_codegen to be used
662 * on all the globals.
665 printf("ast_value used before generated (%s)\n", self->name);
672 bool ast_global_codegen(ast_value *self, ir_builder *ir)
675 if (self->isconst && self->expression.vtype == TYPE_FUNCTION)
677 ir_function *func = ir_builder_create_function(ir, self->name, self->expression.next->expression.vtype);
681 self->constval.vfunc->ir_func = func;
682 self->ir_v = func->value;
683 /* The function is filled later on ast_function_codegen... */
687 if (self->expression.vtype == TYPE_FIELD) {
688 v = ir_builder_create_field(ir, self->name, self->expression.next->expression.vtype);
692 printf("TODO: constant field pointers with value\n");
699 v = ir_builder_create_global(ir, self->name, self->expression.vtype);
701 printf("ir_builder_create_global failed\n");
706 switch (self->expression.vtype)
709 if (!ir_value_set_float(v, self->constval.vfloat))
713 if (!ir_value_set_vector(v, self->constval.vvec))
717 if (!ir_value_set_string(v, self->constval.vstring))
721 printf("global of type function not properly generated\n");
723 /* Cannot generate an IR value for a function,
724 * need a pointer pointing to a function rather.
727 printf("TODO: global constant type %i\n", self->expression.vtype);
732 /* link us to the ir_value */
736 error: /* clean up */
741 bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
744 if (self->isconst && self->expression.vtype == TYPE_FUNCTION)
746 /* Do we allow local functions? I think not...
747 * this is NOT a function pointer atm.
752 v = ir_function_create_local(func, self->name, self->expression.vtype, param);
756 /* A constant local... hmmm...
757 * I suppose the IR will have to deal with this
760 switch (self->expression.vtype)
763 if (!ir_value_set_float(v, self->constval.vfloat))
767 if (!ir_value_set_vector(v, self->constval.vvec))
771 if (!ir_value_set_string(v, self->constval.vstring))
775 printf("TODO: global constant type %i\n", self->expression.vtype);
780 /* link us to the ir_value */
784 error: /* clean up */
789 bool ast_function_codegen(ast_function *self, ir_builder *ir)
793 ast_expression_common *ec;
798 printf("ast_function's related ast_value was not generated yet\n");
802 /* fill the parameter list */
803 ec = &self->vtype->expression;
804 for (i = 0; i < ec->params_count; ++i)
806 if (!ir_function_params_add(irf, ec->params[i]->expression.vtype))
808 if (!self->builtin) {
809 if (!ast_local_codegen(ec->params[i], self->ir_func, true))
815 irf->builtin = self->builtin;
819 self->curblock = ir_function_create_block(irf, "entry");
823 for (i = 0; i < self->blocks_count; ++i) {
824 ast_expression_codegen *gen = self->blocks[i]->expression.codegen;
825 if (!(*gen)((ast_expression*)self->blocks[i], self, false, &dummy))
829 /* TODO: check return types */
830 if (!self->curblock->is_return)
832 if (!self->vtype->expression.next ||
833 self->vtype->expression.next->expression.vtype == TYPE_VOID)
835 return ir_block_create_return(self->curblock, NULL);
839 /* error("missing return"); */
846 /* Note, you will not see ast_block_codegen generate ir_blocks.
847 * To the AST and the IR, blocks are 2 different things.
848 * In the AST it represents a block of code, usually enclosed in
849 * curly braces {...}.
850 * While in the IR it represents a block in terms of control-flow.
852 bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_value **out)
857 * Note: an ast-representation using the comma-operator
858 * of the form: (a, b, c) = x should not assign to c...
861 if (self->expression.outr) {
862 *out = self->expression.outr;
866 /* output is NULL at first, we'll have each expression
867 * assign to out output, thus, a comma-operator represention
868 * using an ast_block will return the last generated value,
869 * so: (b, c) + a executed both b and c, and returns c,
870 * which is then added to a.
874 /* generate locals */
875 for (i = 0; i < self->locals_count; ++i)
877 if (!ast_local_codegen(self->locals[i], func->ir_func, false))
881 for (i = 0; i < self->exprs_count; ++i)
883 ast_expression_codegen *gen = self->exprs[i]->expression.codegen;
884 if (!(*gen)(self->exprs[i], func, false, out))
888 self->expression.outr = *out;
893 bool ast_store_codegen(ast_store *self, ast_function *func, bool lvalue, ir_value **out)
895 ast_expression_codegen *cgen;
896 ir_value *left, *right;
898 if (lvalue && self->expression.outl) {
899 *out = self->expression.outl;
903 if (!lvalue && self->expression.outr) {
904 *out = self->expression.outr;
908 cgen = self->dest->expression.codegen;
910 if (!(*cgen)((ast_expression*)(self->dest), func, true, &left))
912 self->expression.outl = left;
914 cgen = self->source->expression.codegen;
916 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
919 if (!ir_block_create_store_op(func->curblock, self->op, left, right))
921 self->expression.outr = right;
923 /* Theoretically, an assinment returns its left side as an
924 * lvalue, if we don't need an lvalue though, we return
925 * the right side as an rvalue, otherwise we have to
926 * somehow know whether or not we need to dereference the pointer
927 * on the left side - that is: OP_LOAD if it was an address.
928 * Also: in original QC we cannot OP_LOADP *anyway*.
930 *out = (lvalue ? left : right);
935 bool ast_binary_codegen(ast_binary *self, ast_function *func, bool lvalue, ir_value **out)
937 ast_expression_codegen *cgen;
938 ir_value *left, *right;
940 /* In the context of a binary operation, we can disregard
944 if (self->expression.outr) {
945 *out = self->expression.outr;
949 cgen = self->left->expression.codegen;
951 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
954 cgen = self->right->expression.codegen;
956 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
959 *out = ir_block_create_binop(func->curblock, ast_function_label(func, "bin"),
960 self->op, left, right);
963 self->expression.outr = *out;
968 bool ast_binstore_codegen(ast_binstore *self, ast_function *func, bool lvalue, ir_value **out)
970 ast_expression_codegen *cgen;
971 ir_value *leftl, *leftr, *right, *bin;
973 if (lvalue && self->expression.outl) {
974 *out = self->expression.outl;
978 if (!lvalue && self->expression.outr) {
979 *out = self->expression.outr;
983 /* for a binstore we need both an lvalue and an rvalue for the left side */
984 /* rvalue of destination! */
985 cgen = self->dest->expression.codegen;
986 if (!(*cgen)((ast_expression*)(self->dest), func, true, &leftr))
989 /* source as rvalue only */
990 cgen = self->source->expression.codegen;
991 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
995 bin = ir_block_create_binop(func->curblock, ast_function_label(func, "binst"),
996 self->opbin, leftr, right);
997 self->expression.outr = bin;
1000 cgen = self->dest->expression.codegen;
1001 /* lvalue of destination */
1002 if (!(*cgen)((ast_expression*)(self->dest), func, true, &leftl))
1004 self->expression.outl = leftl;
1006 if (!ir_block_create_store_op(func->curblock, self->opstore, leftl, bin))
1008 self->expression.outr = bin;
1010 /* Theoretically, an assinment returns its left side as an
1011 * lvalue, if we don't need an lvalue though, we return
1012 * the right side as an rvalue, otherwise we have to
1013 * somehow know whether or not we need to dereference the pointer
1014 * on the left side - that is: OP_LOAD if it was an address.
1015 * Also: in original QC we cannot OP_LOADP *anyway*.
1017 *out = (lvalue ? leftl : bin);
1022 bool ast_unary_codegen(ast_unary *self, ast_function *func, bool lvalue, ir_value **out)
1024 ast_expression_codegen *cgen;
1027 /* In the context of a unary operation, we can disregard
1031 if (self->expression.outr) {
1032 *out = self->expression.outr;
1036 cgen = self->operand->expression.codegen;
1038 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
1041 *out = ir_block_create_unary(func->curblock, ast_function_label(func, "unary"),
1045 self->expression.outr = *out;
1050 bool ast_return_codegen(ast_return *self, ast_function *func, bool lvalue, ir_value **out)
1052 ast_expression_codegen *cgen;
1055 /* In the context of a return operation, we can disregard
1059 if (self->expression.outr) {
1060 printf("internal error: ast_return cannot be reused, it bears no result!\n");
1063 self->expression.outr = (ir_value*)1;
1065 cgen = self->operand->expression.codegen;
1067 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
1070 if (!ir_block_create_return(func->curblock, operand))
1076 bool ast_entfield_codegen(ast_entfield *self, ast_function *func, bool lvalue, ir_value **out)
1078 ast_expression_codegen *cgen;
1079 ir_value *ent, *field;
1081 /* This function needs to take the 'lvalue' flag into account!
1082 * As lvalue we provide a field-pointer, as rvalue we provide the
1086 if (lvalue && self->expression.outl) {
1087 *out = self->expression.outl;
1091 if (!lvalue && self->expression.outr) {
1092 *out = self->expression.outr;
1096 cgen = self->entity->expression.codegen;
1097 if (!(*cgen)((ast_expression*)(self->entity), func, false, &ent))
1100 cgen = self->field->expression.codegen;
1101 if (!(*cgen)((ast_expression*)(self->field), func, false, &field))
1106 *out = ir_block_create_fieldaddress(func->curblock, ast_function_label(func, "efa"),
1109 *out = ir_block_create_load_from_ent(func->curblock, ast_function_label(func, "efv"),
1110 ent, field, self->expression.vtype);
1116 self->expression.outl = *out;
1118 self->expression.outr = *out;
1120 /* Hm that should be it... */
1124 bool ast_member_codegen(ast_member *self, ast_function *func, bool lvalue, ir_value **out)
1126 ast_expression_codegen *cgen;
1129 /* in QC this is always an lvalue */
1131 if (self->expression.outl) {
1132 *out = self->expression.outl;
1136 cgen = self->owner->expression.codegen;
1137 if (!(*cgen)((ast_expression*)(self->owner), func, true, &vec))
1140 if (vec->vtype != TYPE_VECTOR &&
1141 !(vec->vtype == TYPE_FIELD && self->owner->expression.next->expression.vtype == TYPE_VECTOR))
1146 *out = ir_value_vector_member(vec, self->field);
1147 self->expression.outl = *out;
1149 return (*out != NULL);
1152 bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_value **out)
1154 ast_expression_codegen *cgen;
1159 ir_block *cond = func->curblock;
1164 /* We don't output any value, thus also don't care about r/lvalue */
1168 if (self->expression.outr) {
1169 printf("internal error: ast_ifthen cannot be reused, it bears no result!\n");
1172 self->expression.outr = (ir_value*)1;
1174 /* generate the condition */
1175 func->curblock = cond;
1176 cgen = self->cond->expression.codegen;
1177 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
1182 if (self->on_true) {
1183 /* create on-true block */
1184 ontrue = ir_function_create_block(func->ir_func, ast_function_label(func, "ontrue"));
1188 /* enter the block */
1189 func->curblock = ontrue;
1192 cgen = self->on_true->expression.codegen;
1193 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &dummy))
1199 if (self->on_false) {
1200 /* create on-false block */
1201 onfalse = ir_function_create_block(func->ir_func, ast_function_label(func, "onfalse"));
1205 /* enter the block */
1206 func->curblock = onfalse;
1209 cgen = self->on_false->expression.codegen;
1210 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &dummy))
1215 /* Merge block were they all merge in to */
1216 merge = ir_function_create_block(func->ir_func, ast_function_label(func, "endif"));
1220 /* add jumps ot the merge block */
1221 if (ontrue && !ir_block_create_jump(ontrue, merge))
1223 if (onfalse && !ir_block_create_jump(onfalse, merge))
1226 /* we create the if here, that way all blocks are ordered :)
1228 if (!ir_block_create_if(cond, condval,
1229 (ontrue ? ontrue : merge),
1230 (onfalse ? onfalse : merge)))
1235 /* Now enter the merge block */
1236 func->curblock = merge;
1241 bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_value **out)
1243 ast_expression_codegen *cgen;
1246 ir_value *trueval, *falseval;
1249 ir_block *cond = func->curblock;
1254 /* Ternary can never create an lvalue... */
1258 /* In theory it shouldn't be possible to pass through a node twice, but
1259 * in case we add any kind of optimization pass for the AST itself, it
1260 * may still happen, thus we remember a created ir_value and simply return one
1261 * if it already exists.
1263 if (self->phi_out) {
1264 *out = self->phi_out;
1268 /* In the following, contraty to ast_ifthen, we assume both paths exist. */
1270 /* generate the condition */
1271 func->curblock = cond;
1272 cgen = self->cond->expression.codegen;
1273 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
1276 /* create on-true block */
1277 ontrue = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_T"));
1282 /* enter the block */
1283 func->curblock = ontrue;
1286 cgen = self->on_true->expression.codegen;
1287 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &trueval))
1291 /* create on-false block */
1292 onfalse = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_F"));
1297 /* enter the block */
1298 func->curblock = onfalse;
1301 cgen = self->on_false->expression.codegen;
1302 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &falseval))
1306 /* create merge block */
1307 merge = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_out"));
1310 /* jump to merge block */
1311 if (!ir_block_create_jump(ontrue, merge))
1313 if (!ir_block_create_jump(onfalse, merge))
1316 /* create if instruction */
1317 if (!ir_block_create_if(cond, condval, ontrue, onfalse))
1320 /* Now enter the merge block */
1321 func->curblock = merge;
1323 /* Here, now, we need a PHI node
1324 * but first some sanity checking...
1326 if (trueval->vtype != falseval->vtype) {
1327 /* error("ternary with different types on the two sides"); */
1332 phi = ir_block_create_phi(merge, ast_function_label(func, "phi"), trueval->vtype);
1334 !ir_phi_add(phi, ontrue, trueval) ||
1335 !ir_phi_add(phi, onfalse, falseval))
1340 self->phi_out = ir_phi_value(phi);
1341 *out = self->phi_out;
1346 bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value **out)
1348 ast_expression_codegen *cgen;
1350 ir_value *dummy = NULL;
1351 ir_value *precond = NULL;
1352 ir_value *postcond = NULL;
1354 /* Since we insert some jumps "late" so we have blocks
1355 * ordered "nicely", we need to keep track of the actual end-blocks
1356 * of expressions to add the jumps to.
1358 ir_block *bbody = NULL, *end_bbody = NULL;
1359 ir_block *bprecond = NULL, *end_bprecond = NULL;
1360 ir_block *bpostcond = NULL, *end_bpostcond = NULL;
1361 ir_block *bincrement = NULL, *end_bincrement = NULL;
1362 ir_block *bout = NULL, *bin = NULL;
1364 /* let's at least move the outgoing block to the end */
1367 /* 'break' and 'continue' need to be able to find the right blocks */
1368 ir_block *bcontinue = NULL;
1369 ir_block *bbreak = NULL;
1371 ir_block *old_bcontinue = NULL;
1372 ir_block *old_bbreak = NULL;
1374 ir_block *tmpblock = NULL;
1379 if (self->expression.outr) {
1380 printf("internal error: ast_loop cannot be reused, it bears no result!\n");
1383 self->expression.outr = (ir_value*)1;
1386 * Should we ever need some kind of block ordering, better make this function
1387 * move blocks around than write a block ordering algorithm later... after all
1388 * the ast and ir should work together, not against each other.
1391 /* initexpr doesn't get its own block, it's pointless, it could create more blocks
1392 * anyway if for example it contains a ternary.
1396 cgen = self->initexpr->expression.codegen;
1397 if (!(*cgen)((ast_expression*)(self->initexpr), func, false, &dummy))
1401 /* Store the block from which we enter this chaos */
1402 bin = func->curblock;
1404 /* The pre-loop condition needs its own block since we
1405 * need to be able to jump to the start of that expression.
1409 bprecond = ir_function_create_block(func->ir_func, ast_function_label(func, "pre_loop_cond"));
1413 /* the pre-loop-condition the least important place to 'continue' at */
1414 bcontinue = bprecond;
1417 func->curblock = bprecond;
1420 cgen = self->precond->expression.codegen;
1421 if (!(*cgen)((ast_expression*)(self->precond), func, false, &precond))
1424 end_bprecond = func->curblock;
1426 bprecond = end_bprecond = NULL;
1429 /* Now the next blocks won't be ordered nicely, but we need to
1430 * generate them this early for 'break' and 'continue'.
1432 if (self->increment) {
1433 bincrement = ir_function_create_block(func->ir_func, ast_function_label(func, "loop_increment"));
1436 bcontinue = bincrement; /* increment comes before the pre-loop-condition */
1438 bincrement = end_bincrement = NULL;
1441 if (self->postcond) {
1442 bpostcond = ir_function_create_block(func->ir_func, ast_function_label(func, "post_loop_cond"));
1445 bcontinue = bpostcond; /* postcond comes before the increment */
1447 bpostcond = end_bpostcond = NULL;
1450 bout_id = func->ir_func->blocks_count;
1451 bout = ir_function_create_block(func->ir_func, ast_function_label(func, "after_loop"));
1456 /* The loop body... */
1459 bbody = ir_function_create_block(func->ir_func, ast_function_label(func, "loop_body"));
1464 func->curblock = bbody;
1466 old_bbreak = func->breakblock;
1467 old_bcontinue = func->continueblock;
1468 func->breakblock = bbreak;
1469 func->continueblock = bcontinue;
1472 cgen = self->body->expression.codegen;
1473 if (!(*cgen)((ast_expression*)(self->body), func, false, &dummy))
1476 end_bbody = func->curblock;
1477 func->breakblock = old_bbreak;
1478 func->continueblock = old_bcontinue;
1481 /* post-loop-condition */
1485 func->curblock = bpostcond;
1488 cgen = self->postcond->expression.codegen;
1489 if (!(*cgen)((ast_expression*)(self->postcond), func, false, &postcond))
1492 end_bpostcond = func->curblock;
1495 /* The incrementor */
1496 if (self->increment)
1499 func->curblock = bincrement;
1502 cgen = self->increment->expression.codegen;
1503 if (!(*cgen)((ast_expression*)(self->increment), func, false, &dummy))
1506 end_bincrement = func->curblock;
1509 /* In any case now, we continue from the outgoing block */
1510 func->curblock = bout;
1512 /* Now all blocks are in place */
1513 /* From 'bin' we jump to whatever comes first */
1514 if (bprecond) tmpblock = bprecond;
1515 else if (bbody) tmpblock = bbody;
1516 else if (bpostcond) tmpblock = bpostcond;
1517 else tmpblock = bout;
1518 if (!ir_block_create_jump(bin, tmpblock))
1524 ir_block *ontrue, *onfalse;
1525 if (bbody) ontrue = bbody;
1526 else if (bincrement) ontrue = bincrement;
1527 else if (bpostcond) ontrue = bpostcond;
1528 else ontrue = bprecond;
1530 if (!ir_block_create_if(end_bprecond, precond, ontrue, onfalse))
1537 if (bincrement) tmpblock = bincrement;
1538 else if (bpostcond) tmpblock = bpostcond;
1539 else if (bprecond) tmpblock = bprecond;
1540 else tmpblock = bout;
1541 if (!ir_block_create_jump(end_bbody, tmpblock))
1545 /* from increment */
1548 if (bpostcond) tmpblock = bpostcond;
1549 else if (bprecond) tmpblock = bprecond;
1550 else if (bbody) tmpblock = bbody;
1551 else tmpblock = bout;
1552 if (!ir_block_create_jump(end_bincrement, tmpblock))
1559 ir_block *ontrue, *onfalse;
1560 if (bprecond) ontrue = bprecond;
1561 else if (bbody) ontrue = bbody;
1562 else if (bincrement) ontrue = bincrement;
1563 else ontrue = bpostcond;
1565 if (!ir_block_create_if(end_bpostcond, postcond, ontrue, onfalse))
1569 /* Move 'bout' to the end */
1570 if (!ir_function_blocks_remove(func->ir_func, bout_id) ||
1571 !ir_function_blocks_add(func->ir_func, bout))
1573 ir_block_delete(bout);
1580 bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value **out)
1582 ast_expression_codegen *cgen;
1583 ir_value_vector params;
1584 ir_instr *callinstr;
1587 ir_value *funval = NULL;
1589 /* return values are never lvalues */
1592 if (self->expression.outr) {
1593 *out = self->expression.outr;
1597 cgen = self->func->expression.codegen;
1598 if (!(*cgen)((ast_expression*)(self->func), func, false, &funval))
1603 MEM_VECTOR_INIT(¶ms, v);
1606 for (i = 0; i < self->params_count; ++i)
1609 ast_expression *expr = self->params[i];
1611 cgen = expr->expression.codegen;
1612 if (!(*cgen)(expr, func, false, ¶m))
1616 if (!ir_value_vector_v_add(¶ms, param))
1620 callinstr = ir_block_create_call(func->curblock, ast_function_label(func, "call"), funval);
1624 for (i = 0; i < params.v_count; ++i) {
1625 if (!ir_call_param(callinstr, params.v[i]))
1629 *out = ir_call_value(callinstr);
1630 self->expression.outr = *out;
1632 MEM_VECTOR_CLEAR(¶ms, v);
1635 MEM_VECTOR_CLEAR(¶ms, v);