5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is furnished to do
10 * so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 #define ast_instantiate(T, ctx, destroyfn) \
31 T* self = (T*)mem_a(sizeof(T)); \
35 ast_node_init((ast_node*)self, ctx, TYPE_##T); \
36 ( (ast_node*)self )->node.destroy = (ast_node_delete*)destroyfn
39 static void asterror(lex_ctx ctx, const char *msg, ...)
43 con_cvprintmsg((void*)&ctx, LVL_ERROR, "error", msg, ap);
47 /* It must not be possible to get here. */
48 static GMQCC_NORETURN void _ast_node_destroy(ast_node *self)
50 con_err("ast node missing destroy()\n");
54 /* Initialize main ast node aprts */
55 static void ast_node_init(ast_node *self, lex_ctx ctx, int nodetype)
57 self->node.context = ctx;
58 self->node.destroy = &_ast_node_destroy;
59 self->node.keep = false;
60 self->node.nodetype = nodetype;
63 /* General expression initialization */
64 static void ast_expression_init(ast_expression *self,
65 ast_expression_codegen *codegen)
67 self->expression.codegen = codegen;
68 self->expression.vtype = TYPE_VOID;
69 self->expression.next = NULL;
70 self->expression.outl = NULL;
71 self->expression.outr = NULL;
72 self->expression.variadic = false;
73 self->expression.params = NULL;
76 static void ast_expression_delete(ast_expression *self)
79 if (self->expression.next)
80 ast_delete(self->expression.next);
81 for (i = 0; i < vec_size(self->expression.params); ++i) {
82 ast_delete(self->expression.params[i]);
84 vec_free(self->expression.params);
87 static void ast_expression_delete_full(ast_expression *self)
89 ast_expression_delete(self);
93 ast_value* ast_value_copy(const ast_value *self)
96 const ast_expression_common *fromex;
97 ast_expression_common *selfex;
98 ast_value *cp = ast_value_new(self->expression.node.context, self->name, self->expression.vtype);
99 if (self->expression.next) {
100 cp->expression.next = ast_type_copy(self->expression.node.context, self->expression.next);
101 if (!cp->expression.next) {
102 ast_value_delete(cp);
106 fromex = &self->expression;
107 selfex = &cp->expression;
108 selfex->variadic = fromex->variadic;
109 for (i = 0; i < vec_size(fromex->params); ++i) {
110 ast_value *v = ast_value_copy(fromex->params[i]);
112 ast_value_delete(cp);
115 vec_push(selfex->params, v);
120 bool ast_type_adopt_impl(ast_expression *self, const ast_expression *other)
123 const ast_expression_common *fromex;
124 ast_expression_common *selfex;
125 self->expression.vtype = other->expression.vtype;
126 if (other->expression.next) {
127 self->expression.next = (ast_expression*)ast_type_copy(ast_ctx(self), other->expression.next);
128 if (!self->expression.next)
131 fromex = &other->expression;
132 selfex = &self->expression;
133 selfex->variadic = fromex->variadic;
134 for (i = 0; i < vec_size(fromex->params); ++i) {
135 ast_value *v = ast_value_copy(fromex->params[i]);
138 vec_push(selfex->params, v);
143 static ast_expression* ast_shallow_type(lex_ctx ctx, int vtype)
145 ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
146 ast_expression_init(self, NULL);
147 self->expression.codegen = NULL;
148 self->expression.next = NULL;
149 self->expression.vtype = vtype;
153 ast_expression* ast_type_copy(lex_ctx ctx, const ast_expression *ex)
156 const ast_expression_common *fromex;
157 ast_expression_common *selfex;
163 ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
164 ast_expression_init(self, NULL);
166 fromex = &ex->expression;
167 selfex = &self->expression;
169 /* This may never be codegen()d */
170 selfex->codegen = NULL;
172 selfex->vtype = fromex->vtype;
175 selfex->next = ast_type_copy(ctx, fromex->next);
177 ast_expression_delete_full(self);
184 selfex->variadic = fromex->variadic;
185 for (i = 0; i < vec_size(fromex->params); ++i) {
186 ast_value *v = ast_value_copy(fromex->params[i]);
188 ast_expression_delete_full(self);
191 vec_push(selfex->params, v);
198 bool ast_compare_type(ast_expression *a, ast_expression *b)
200 if (a->expression.vtype != b->expression.vtype)
202 if (!a->expression.next != !b->expression.next)
204 if (vec_size(a->expression.params) != vec_size(b->expression.params))
206 if (a->expression.variadic != b->expression.variadic)
208 if (vec_size(a->expression.params)) {
210 for (i = 0; i < vec_size(a->expression.params); ++i) {
211 if (!ast_compare_type((ast_expression*)a->expression.params[i],
212 (ast_expression*)b->expression.params[i]))
216 if (a->expression.next)
217 return ast_compare_type(a->expression.next, b->expression.next);
221 static size_t ast_type_to_string_impl(ast_expression *e, char *buf, size_t bufsize, size_t pos)
228 if (pos + 6 >= bufsize)
230 strcpy(buf + pos, "(null)");
234 if (pos + 1 >= bufsize)
237 switch (e->expression.vtype) {
239 strcpy(buf + pos, "(variant)");
244 return ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
247 if (pos + 3 >= bufsize)
251 pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
252 if (pos + 1 >= bufsize)
258 pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
259 if (pos + 2 >= bufsize)
261 if (!vec_size(e->expression.params)) {
267 pos = ast_type_to_string_impl((ast_expression*)(e->expression.params[0]), buf, bufsize, pos);
268 for (i = 1; i < vec_size(e->expression.params); ++i) {
269 if (pos + 2 >= bufsize)
273 pos = ast_type_to_string_impl((ast_expression*)(e->expression.params[i]), buf, bufsize, pos);
275 if (pos + 1 >= bufsize)
281 pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
282 if (pos + 1 >= bufsize)
285 pos += snprintf(buf + pos, bufsize - pos - 1, "%i", (int)e->expression.count);
286 if (pos + 1 >= bufsize)
292 typestr = type_name[e->expression.vtype];
293 typelen = strlen(typestr);
294 if (pos + typelen >= bufsize)
296 strcpy(buf + pos, typestr);
297 return pos + typelen;
301 buf[bufsize-3] = '.';
302 buf[bufsize-2] = '.';
303 buf[bufsize-1] = '.';
307 void ast_type_to_string(ast_expression *e, char *buf, size_t bufsize)
309 size_t pos = ast_type_to_string_impl(e, buf, bufsize-1, 0);
313 ast_value* ast_value_new(lex_ctx ctx, const char *name, int t)
315 ast_instantiate(ast_value, ctx, ast_value_delete);
316 ast_expression_init((ast_expression*)self,
317 (ast_expression_codegen*)&ast_value_codegen);
318 self->expression.node.keep = true; /* keep */
320 self->name = name ? util_strdup(name) : NULL;
321 self->expression.vtype = t;
322 self->expression.next = NULL;
323 self->isconst = false;
325 memset(&self->constval, 0, sizeof(self->constval));
328 self->ir_values = NULL;
329 self->ir_value_count = 0;
337 void ast_value_delete(ast_value* self)
340 mem_d((void*)self->name);
342 switch (self->expression.vtype)
345 mem_d((void*)self->constval.vstring);
348 /* unlink us from the function node */
349 self->constval.vfunc->vtype = NULL;
351 /* NOTE: delete function? currently collected in
352 * the parser structure
359 mem_d(self->ir_values);
360 ast_expression_delete((ast_expression*)self);
364 void ast_value_params_add(ast_value *self, ast_value *p)
366 vec_push(self->expression.params, p);
369 bool ast_value_set_name(ast_value *self, const char *name)
372 mem_d((void*)self->name);
373 self->name = util_strdup(name);
377 ast_binary* ast_binary_new(lex_ctx ctx, int op,
378 ast_expression* left, ast_expression* right)
380 ast_instantiate(ast_binary, ctx, ast_binary_delete);
381 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binary_codegen);
387 if (op >= INSTR_EQ_F && op <= INSTR_GT)
388 self->expression.vtype = TYPE_FLOAT;
389 else if (op == INSTR_AND || op == INSTR_OR ||
390 op == INSTR_BITAND || op == INSTR_BITOR)
391 self->expression.vtype = TYPE_FLOAT;
392 else if (op == INSTR_MUL_VF || op == INSTR_MUL_FV)
393 self->expression.vtype = TYPE_VECTOR;
394 else if (op == INSTR_MUL_V)
395 self->expression.vtype = TYPE_FLOAT;
397 self->expression.vtype = left->expression.vtype;
402 void ast_binary_delete(ast_binary *self)
404 ast_unref(self->left);
405 ast_unref(self->right);
406 ast_expression_delete((ast_expression*)self);
410 ast_binstore* ast_binstore_new(lex_ctx ctx, int storop, int op,
411 ast_expression* left, ast_expression* right)
413 ast_instantiate(ast_binstore, ctx, ast_binstore_delete);
414 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binstore_codegen);
416 self->opstore = storop;
419 self->source = right;
421 self->expression.vtype = left->expression.vtype;
422 if (left->expression.next) {
423 self->expression.next = ast_type_copy(ctx, left);
424 if (!self->expression.next) {
430 self->expression.next = NULL;
435 void ast_binstore_delete(ast_binstore *self)
437 ast_unref(self->dest);
438 ast_unref(self->source);
439 ast_expression_delete((ast_expression*)self);
443 ast_unary* ast_unary_new(lex_ctx ctx, int op,
444 ast_expression *expr)
446 ast_instantiate(ast_unary, ctx, ast_unary_delete);
447 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_unary_codegen);
450 self->operand = expr;
452 if (op >= INSTR_NOT_F && op <= INSTR_NOT_FNC) {
453 self->expression.vtype = TYPE_FLOAT;
455 asterror(ctx, "cannot determine type of unary operation %s", asm_instr[op].m);
460 void ast_unary_delete(ast_unary *self)
462 ast_unref(self->operand);
463 ast_expression_delete((ast_expression*)self);
467 ast_return* ast_return_new(lex_ctx ctx, ast_expression *expr)
469 ast_instantiate(ast_return, ctx, ast_return_delete);
470 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_return_codegen);
472 self->operand = expr;
477 void ast_return_delete(ast_return *self)
480 ast_unref(self->operand);
481 ast_expression_delete((ast_expression*)self);
485 ast_entfield* ast_entfield_new(lex_ctx ctx, ast_expression *entity, ast_expression *field)
487 if (field->expression.vtype != TYPE_FIELD) {
488 asterror(ctx, "ast_entfield_new with expression not of type field");
491 return ast_entfield_new_force(ctx, entity, field, field->expression.next);
494 ast_entfield* ast_entfield_new_force(lex_ctx ctx, ast_expression *entity, ast_expression *field, const ast_expression *outtype)
496 ast_instantiate(ast_entfield, ctx, ast_entfield_delete);
500 /* Error: field has no type... */
504 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_entfield_codegen);
506 self->entity = entity;
509 if (!ast_type_adopt(self, outtype)) {
510 ast_entfield_delete(self);
517 void ast_entfield_delete(ast_entfield *self)
519 ast_unref(self->entity);
520 ast_unref(self->field);
521 ast_expression_delete((ast_expression*)self);
525 ast_member* ast_member_new(lex_ctx ctx, ast_expression *owner, unsigned int field)
527 ast_instantiate(ast_member, ctx, ast_member_delete);
533 if (owner->expression.vtype != TYPE_VECTOR &&
534 owner->expression.vtype != TYPE_FIELD) {
535 asterror(ctx, "member-access on an invalid owner of type %s", type_name[owner->expression.vtype]);
540 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_member_codegen);
541 self->expression.node.keep = true; /* keep */
543 if (owner->expression.vtype == TYPE_VECTOR) {
544 self->expression.vtype = TYPE_FLOAT;
545 self->expression.next = NULL;
547 self->expression.vtype = TYPE_FIELD;
548 self->expression.next = ast_shallow_type(ctx, TYPE_FLOAT);
557 void ast_member_delete(ast_member *self)
559 /* The owner is always an ast_value, which has .keep=true,
560 * also: ast_members are usually deleted after the owner, thus
561 * this will cause invalid access
562 ast_unref(self->owner);
563 * once we allow (expression).x to access a vector-member, we need
564 * to change this: preferably by creating an alternate ast node for this
565 * purpose that is not garbage-collected.
567 ast_expression_delete((ast_expression*)self);
571 ast_array_index* ast_array_index_new(lex_ctx ctx, ast_expression *array, ast_expression *index)
573 ast_expression *outtype;
574 ast_instantiate(ast_array_index, ctx, ast_array_index_delete);
576 outtype = array->expression.next;
579 /* Error: field has no type... */
583 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_array_index_codegen);
588 if (!ast_type_adopt(self, outtype)) {
589 ast_array_index_delete(self);
592 if (array->expression.vtype == TYPE_FIELD && outtype->expression.vtype == TYPE_ARRAY) {
593 if (self->expression.vtype != TYPE_ARRAY) {
594 asterror(ast_ctx(self), "array_index node on type");
595 ast_array_index_delete(self);
598 self->array = outtype;
599 self->expression.vtype = TYPE_FIELD;
605 void ast_array_index_delete(ast_array_index *self)
607 ast_unref(self->array);
608 ast_unref(self->index);
609 ast_expression_delete((ast_expression*)self);
613 ast_ifthen* ast_ifthen_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
615 ast_instantiate(ast_ifthen, ctx, ast_ifthen_delete);
616 if (!ontrue && !onfalse) {
617 /* because it is invalid */
621 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ifthen_codegen);
624 self->on_true = ontrue;
625 self->on_false = onfalse;
630 void ast_ifthen_delete(ast_ifthen *self)
632 ast_unref(self->cond);
634 ast_unref(self->on_true);
636 ast_unref(self->on_false);
637 ast_expression_delete((ast_expression*)self);
641 ast_ternary* ast_ternary_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
643 ast_instantiate(ast_ternary, ctx, ast_ternary_delete);
644 /* This time NEITHER must be NULL */
645 if (!ontrue || !onfalse) {
649 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ternary_codegen);
652 self->on_true = ontrue;
653 self->on_false = onfalse;
654 self->phi_out = NULL;
659 void ast_ternary_delete(ast_ternary *self)
661 ast_unref(self->cond);
662 ast_unref(self->on_true);
663 ast_unref(self->on_false);
664 ast_expression_delete((ast_expression*)self);
668 ast_loop* ast_loop_new(lex_ctx ctx,
669 ast_expression *initexpr,
670 ast_expression *precond,
671 ast_expression *postcond,
672 ast_expression *increment,
673 ast_expression *body)
675 ast_instantiate(ast_loop, ctx, ast_loop_delete);
676 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_loop_codegen);
678 self->initexpr = initexpr;
679 self->precond = precond;
680 self->postcond = postcond;
681 self->increment = increment;
687 void ast_loop_delete(ast_loop *self)
690 ast_unref(self->initexpr);
692 ast_unref(self->precond);
694 ast_unref(self->postcond);
696 ast_unref(self->increment);
698 ast_unref(self->body);
699 ast_expression_delete((ast_expression*)self);
703 ast_breakcont* ast_breakcont_new(lex_ctx ctx, bool iscont)
705 ast_instantiate(ast_breakcont, ctx, ast_breakcont_delete);
706 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_breakcont_codegen);
708 self->is_continue = iscont;
713 void ast_breakcont_delete(ast_breakcont *self)
715 ast_expression_delete((ast_expression*)self);
719 ast_call* ast_call_new(lex_ctx ctx,
720 ast_expression *funcexpr)
722 ast_instantiate(ast_call, ctx, ast_call_delete);
723 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_call_codegen);
726 self->func = funcexpr;
728 self->expression.vtype = funcexpr->expression.next->expression.vtype;
729 if (funcexpr->expression.next->expression.next)
730 self->expression.next = ast_type_copy(ctx, funcexpr->expression.next->expression.next);
735 void ast_call_delete(ast_call *self)
738 for (i = 0; i < vec_size(self->params); ++i)
739 ast_unref(self->params[i]);
740 vec_free(self->params);
743 ast_unref(self->func);
745 ast_expression_delete((ast_expression*)self);
749 bool ast_call_check_types(ast_call *self)
753 const ast_expression *func = self->func;
754 size_t count = vec_size(self->params);
755 if (count > vec_size(func->expression.params))
756 count = vec_size(func->expression.params);
758 for (i = 0; i < count; ++i) {
759 if (!ast_compare_type(self->params[i], (ast_expression*)(func->expression.params[i]))) {
760 asterror(ast_ctx(self), "invalid type for parameter %u in function call",
761 (unsigned int)(i+1));
762 /* we don't immediately return */
769 ast_store* ast_store_new(lex_ctx ctx, int op,
770 ast_expression *dest, ast_expression *source)
772 ast_instantiate(ast_store, ctx, ast_store_delete);
773 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_store_codegen);
777 self->source = source;
779 self->expression.vtype = dest->expression.vtype;
780 if (dest->expression.next) {
781 self->expression.next = ast_type_copy(ctx, dest);
782 if (!self->expression.next) {
788 self->expression.next = NULL;
793 void ast_store_delete(ast_store *self)
795 ast_unref(self->dest);
796 ast_unref(self->source);
797 ast_expression_delete((ast_expression*)self);
801 ast_block* ast_block_new(lex_ctx ctx)
803 ast_instantiate(ast_block, ctx, ast_block_delete);
804 ast_expression_init((ast_expression*)self,
805 (ast_expression_codegen*)&ast_block_codegen);
809 self->collect = NULL;
814 void ast_block_collect(ast_block *self, ast_expression *expr)
816 vec_push(self->collect, expr);
817 expr->expression.node.keep = true;
820 void ast_block_delete(ast_block *self)
823 for (i = 0; i < vec_size(self->exprs); ++i)
824 ast_unref(self->exprs[i]);
825 vec_free(self->exprs);
826 for (i = 0; i < vec_size(self->locals); ++i)
827 ast_delete(self->locals[i]);
828 vec_free(self->locals);
829 for (i = 0; i < vec_size(self->collect); ++i)
830 ast_delete(self->collect[i]);
831 vec_free(self->collect);
832 ast_expression_delete((ast_expression*)self);
836 bool ast_block_set_type(ast_block *self, ast_expression *from)
838 if (self->expression.next)
839 ast_delete(self->expression.next);
840 self->expression.vtype = from->expression.vtype;
841 if (from->expression.next) {
842 self->expression.next = ast_type_copy(self->expression.node.context, from->expression.next);
843 if (!self->expression.next)
847 self->expression.next = NULL;
851 ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype)
853 ast_instantiate(ast_function, ctx, ast_function_delete);
857 vtype->expression.vtype != TYPE_FUNCTION)
864 self->name = name ? util_strdup(name) : NULL;
867 self->labelcount = 0;
870 self->ir_func = NULL;
871 self->curblock = NULL;
873 self->breakblock = NULL;
874 self->continueblock = NULL;
876 vtype->isconst = true;
877 vtype->constval.vfunc = self;
882 void ast_function_delete(ast_function *self)
886 mem_d((void*)self->name);
888 /* ast_value_delete(self->vtype); */
889 self->vtype->isconst = false;
890 self->vtype->constval.vfunc = NULL;
891 /* We use unref - if it was stored in a global table it is supposed
892 * to be deleted from *there*
894 ast_unref(self->vtype);
896 for (i = 0; i < vec_size(self->blocks); ++i)
897 ast_delete(self->blocks[i]);
898 vec_free(self->blocks);
902 const char* ast_function_label(ast_function *self, const char *prefix)
911 id = (self->labelcount++);
912 len = strlen(prefix);
914 from = self->labelbuf + sizeof(self->labelbuf)-1;
917 unsigned int digit = id % 10;
921 memcpy(from - len, prefix, len);
925 /*********************************************************************/
927 * by convention you must never pass NULL to the 'ir_value **out'
928 * parameter. If you really don't care about the output, pass a dummy.
929 * But I can't imagine a pituation where the output is truly unnecessary.
932 bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out)
934 /* NOTE: This is the codegen for a variable used in an expression.
935 * It is not the codegen to generate the value. For this purpose,
936 * ast_local_codegen and ast_global_codegen are to be used before this
937 * is executed. ast_function_codegen should take care of its locals,
938 * and the ast-user should take care of ast_global_codegen to be used
939 * on all the globals.
943 ast_type_to_string((ast_expression*)self, typename, sizeof(typename));
944 asterror(ast_ctx(self), "ast_value used before generated %s %s", typename, self->name);
951 bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
955 if (self->isconst && self->expression.vtype == TYPE_FUNCTION)
957 ir_function *func = ir_builder_create_function(ir, self->name, self->expression.next->expression.vtype);
960 func->context = ast_ctx(self);
961 func->value->context = ast_ctx(self);
963 self->constval.vfunc->ir_func = func;
964 self->ir_v = func->value;
965 /* The function is filled later on ast_function_codegen... */
969 if (isfield && self->expression.vtype == TYPE_FIELD) {
970 ast_expression *fieldtype = self->expression.next;
973 asterror(ast_ctx(self), "TODO: constant field pointers with value");
977 if (fieldtype->expression.vtype == TYPE_ARRAY) {
982 ast_expression_common *elemtype;
984 ast_value *array = (ast_value*)fieldtype;
986 if (!ast_istype(fieldtype, ast_value)) {
987 asterror(ast_ctx(self), "internal error: ast_value required");
991 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
992 if (!array->expression.count || array->expression.count > opts_max_array_size)
993 asterror(ast_ctx(self), "Invalid array of size %lu", (unsigned long)array->expression.count);
995 elemtype = &array->expression.next->expression;
996 vtype = elemtype->vtype;
998 v = ir_builder_create_field(ir, self->name, vtype);
1000 asterror(ast_ctx(self), "ir_builder_create_global failed");
1003 if (vtype == TYPE_FIELD)
1004 v->fieldtype = elemtype->next->expression.vtype;
1005 v->context = ast_ctx(self);
1006 array->ir_v = self->ir_v = v;
1008 namelen = strlen(self->name);
1009 name = (char*)mem_a(namelen + 16);
1010 strcpy(name, self->name);
1012 array->ir_values = (ir_value**)mem_a(sizeof(array->ir_values[0]) * array->expression.count);
1013 array->ir_values[0] = v;
1014 for (ai = 1; ai < array->expression.count; ++ai) {
1015 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1016 array->ir_values[ai] = ir_builder_create_field(ir, name, vtype);
1017 if (!array->ir_values[ai]) {
1019 asterror(ast_ctx(self), "ir_builder_create_global failed");
1022 if (vtype == TYPE_FIELD)
1023 array->ir_values[ai]->fieldtype = elemtype->next->expression.vtype;
1024 array->ir_values[ai]->context = ast_ctx(self);
1030 v = ir_builder_create_field(ir, self->name, self->expression.next->expression.vtype);
1033 v->context = ast_ctx(self);
1039 if (self->expression.vtype == TYPE_ARRAY) {
1044 ast_expression_common *elemtype = &self->expression.next->expression;
1045 int vtype = elemtype->vtype;
1047 /* same as with field arrays */
1048 if (!self->expression.count || self->expression.count > opts_max_array_size)
1049 asterror(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1051 v = ir_builder_create_global(ir, self->name, vtype);
1053 asterror(ast_ctx(self), "ir_builder_create_global failed");
1056 if (vtype == TYPE_FIELD)
1057 v->fieldtype = elemtype->next->expression.vtype;
1058 v->context = ast_ctx(self);
1060 namelen = strlen(self->name);
1061 name = (char*)mem_a(namelen + 16);
1062 strcpy(name, self->name);
1064 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1065 self->ir_values[0] = v;
1066 for (ai = 1; ai < self->expression.count; ++ai) {
1067 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1068 self->ir_values[ai] = ir_builder_create_global(ir, name, vtype);
1069 if (!self->ir_values[ai]) {
1071 asterror(ast_ctx(self), "ir_builder_create_global failed");
1074 if (vtype == TYPE_FIELD)
1075 self->ir_values[ai]->fieldtype = elemtype->next->expression.vtype;
1076 self->ir_values[ai]->context = ast_ctx(self);
1082 /* Arrays don't do this since there's no "array" value which spans across the
1085 v = ir_builder_create_global(ir, self->name, self->expression.vtype);
1087 asterror(ast_ctx(self), "ir_builder_create_global failed");
1090 if (self->expression.vtype == TYPE_FIELD)
1091 v->fieldtype = self->expression.next->expression.vtype;
1092 v->context = ast_ctx(self);
1095 if (self->isconst) {
1096 switch (self->expression.vtype)
1099 if (!ir_value_set_float(v, self->constval.vfloat))
1103 if (!ir_value_set_vector(v, self->constval.vvec))
1107 if (!ir_value_set_string(v, self->constval.vstring))
1111 asterror(ast_ctx(self), "TODO: global constant array");
1114 asterror(ast_ctx(self), "global of type function not properly generated");
1116 /* Cannot generate an IR value for a function,
1117 * need a pointer pointing to a function rather.
1120 asterror(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1125 /* link us to the ir_value */
1129 error: /* clean up */
1134 bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
1137 if (self->isconst && self->expression.vtype == TYPE_FUNCTION)
1139 /* Do we allow local functions? I think not...
1140 * this is NOT a function pointer atm.
1145 if (self->expression.vtype == TYPE_ARRAY) {
1150 ast_expression_common *elemtype = &self->expression.next->expression;
1151 int vtype = elemtype->vtype;
1154 asterror(ast_ctx(self), "array-parameters are not supported");
1158 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1159 if (!self->expression.count || self->expression.count > opts_max_array_size) {
1160 asterror(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1163 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1164 if (!self->ir_values) {
1165 asterror(ast_ctx(self), "failed to allocate array values");
1169 v = ir_function_create_local(func, self->name, vtype, param);
1171 asterror(ast_ctx(self), "ir_function_create_local failed");
1174 if (vtype == TYPE_FIELD)
1175 v->fieldtype = elemtype->next->expression.vtype;
1176 v->context = ast_ctx(self);
1178 namelen = strlen(self->name);
1179 name = (char*)mem_a(namelen + 16);
1180 strcpy(name, self->name);
1182 self->ir_values[0] = v;
1183 for (ai = 1; ai < self->expression.count; ++ai) {
1184 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1185 self->ir_values[ai] = ir_function_create_local(func, name, vtype, param);
1186 if (!self->ir_values[ai]) {
1187 asterror(ast_ctx(self), "ir_builder_create_global failed");
1190 if (vtype == TYPE_FIELD)
1191 self->ir_values[ai]->fieldtype = elemtype->next->expression.vtype;
1192 self->ir_values[ai]->context = ast_ctx(self);
1197 v = ir_function_create_local(func, self->name, self->expression.vtype, param);
1200 if (self->expression.vtype == TYPE_FIELD)
1201 v->fieldtype = self->expression.next->expression.vtype;
1202 v->context = ast_ctx(self);
1205 /* A constant local... hmmm...
1206 * I suppose the IR will have to deal with this
1208 if (self->isconst) {
1209 switch (self->expression.vtype)
1212 if (!ir_value_set_float(v, self->constval.vfloat))
1216 if (!ir_value_set_vector(v, self->constval.vvec))
1220 if (!ir_value_set_string(v, self->constval.vstring))
1224 asterror(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1229 /* link us to the ir_value */
1233 if (!ast_global_codegen(self->setter, func->owner, false) ||
1234 !ast_function_codegen(self->setter->constval.vfunc, func->owner) ||
1235 !ir_function_finalize(self->setter->constval.vfunc->ir_func))
1239 if (!ast_global_codegen(self->getter, func->owner, false) ||
1240 !ast_function_codegen(self->getter->constval.vfunc, func->owner) ||
1241 !ir_function_finalize(self->getter->constval.vfunc->ir_func))
1246 error: /* clean up */
1251 bool ast_function_codegen(ast_function *self, ir_builder *ir)
1255 ast_expression_common *ec;
1258 irf = self->ir_func;
1260 asterror(ast_ctx(self), "ast_function's related ast_value was not generated yet");
1264 /* fill the parameter list */
1265 ec = &self->vtype->expression;
1266 for (i = 0; i < vec_size(ec->params); ++i)
1268 vec_push(irf->params, ec->params[i]->expression.vtype);
1269 if (!self->builtin) {
1270 if (!ast_local_codegen(ec->params[i], self->ir_func, true))
1275 if (self->builtin) {
1276 irf->builtin = self->builtin;
1280 if (!vec_size(self->blocks)) {
1281 asterror(ast_ctx(self), "function `%s` has no body", self->name);
1285 self->curblock = ir_function_create_block(irf, "entry");
1286 if (!self->curblock) {
1287 asterror(ast_ctx(self), "failed to allocate entry block for `%s`", self->name);
1291 for (i = 0; i < vec_size(self->blocks); ++i) {
1292 ast_expression_codegen *gen = self->blocks[i]->expression.codegen;
1293 if (!(*gen)((ast_expression*)self->blocks[i], self, false, &dummy))
1297 /* TODO: check return types */
1298 if (!self->curblock->is_return)
1300 return ir_block_create_return(self->curblock, NULL);
1301 /* From now on the parser has to handle this situation */
1303 if (!self->vtype->expression.next ||
1304 self->vtype->expression.next->expression.vtype == TYPE_VOID)
1306 return ir_block_create_return(self->curblock, NULL);
1310 /* error("missing return"); */
1311 asterror(ast_ctx(self), "function `%s` missing return value", self->name);
1319 /* Note, you will not see ast_block_codegen generate ir_blocks.
1320 * To the AST and the IR, blocks are 2 different things.
1321 * In the AST it represents a block of code, usually enclosed in
1322 * curly braces {...}.
1323 * While in the IR it represents a block in terms of control-flow.
1325 bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_value **out)
1329 /* We don't use this
1330 * Note: an ast-representation using the comma-operator
1331 * of the form: (a, b, c) = x should not assign to c...
1334 asterror(ast_ctx(self), "not an l-value (code-block)");
1338 if (self->expression.outr) {
1339 *out = self->expression.outr;
1343 /* output is NULL at first, we'll have each expression
1344 * assign to out output, thus, a comma-operator represention
1345 * using an ast_block will return the last generated value,
1346 * so: (b, c) + a executed both b and c, and returns c,
1347 * which is then added to a.
1351 /* generate locals */
1352 for (i = 0; i < vec_size(self->locals); ++i)
1354 if (!ast_local_codegen(self->locals[i], func->ir_func, false)) {
1356 asterror(ast_ctx(self), "failed to generate local `%s`", self->locals[i]->name);
1361 for (i = 0; i < vec_size(self->exprs); ++i)
1363 ast_expression_codegen *gen = self->exprs[i]->expression.codegen;
1364 if (!(*gen)(self->exprs[i], func, false, out))
1368 self->expression.outr = *out;
1373 bool ast_store_codegen(ast_store *self, ast_function *func, bool lvalue, ir_value **out)
1375 ast_expression_codegen *cgen;
1376 ir_value *left, *right;
1380 ast_array_index *ai = NULL;
1382 if (lvalue && self->expression.outl) {
1383 *out = self->expression.outl;
1387 if (!lvalue && self->expression.outr) {
1388 *out = self->expression.outr;
1392 if (ast_istype(self->dest, ast_array_index))
1395 ai = (ast_array_index*)self->dest;
1396 idx = (ast_value*)ai->index;
1398 if (ast_istype(ai->index, ast_value) && idx->isconst)
1403 /* we need to call the setter */
1404 ir_value *iridx, *funval;
1408 asterror(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1412 arr = (ast_value*)ai->array;
1413 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1414 asterror(ast_ctx(self), "value has no setter (%s)", arr->name);
1418 cgen = idx->expression.codegen;
1419 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1422 cgen = arr->setter->expression.codegen;
1423 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
1426 cgen = self->source->expression.codegen;
1427 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1430 call = ir_block_create_call(func->curblock, ast_function_label(func, "store"), funval);
1433 ir_call_param(call, iridx);
1434 ir_call_param(call, right);
1435 self->expression.outr = right;
1441 cgen = self->dest->expression.codegen;
1443 if (!(*cgen)((ast_expression*)(self->dest), func, true, &left))
1445 self->expression.outl = left;
1447 cgen = self->source->expression.codegen;
1449 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1452 if (!ir_block_create_store_op(func->curblock, self->op, left, right))
1454 self->expression.outr = right;
1457 /* Theoretically, an assinment returns its left side as an
1458 * lvalue, if we don't need an lvalue though, we return
1459 * the right side as an rvalue, otherwise we have to
1460 * somehow know whether or not we need to dereference the pointer
1461 * on the left side - that is: OP_LOAD if it was an address.
1462 * Also: in original QC we cannot OP_LOADP *anyway*.
1464 *out = (lvalue ? left : right);
1469 bool ast_binary_codegen(ast_binary *self, ast_function *func, bool lvalue, ir_value **out)
1471 ast_expression_codegen *cgen;
1472 ir_value *left, *right;
1474 /* A binary operation cannot yield an l-value */
1476 asterror(ast_ctx(self), "not an l-value (binop)");
1480 if (self->expression.outr) {
1481 *out = self->expression.outr;
1485 cgen = self->left->expression.codegen;
1487 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1490 cgen = self->right->expression.codegen;
1492 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1495 *out = ir_block_create_binop(func->curblock, ast_function_label(func, "bin"),
1496 self->op, left, right);
1499 self->expression.outr = *out;
1504 bool ast_binstore_codegen(ast_binstore *self, ast_function *func, bool lvalue, ir_value **out)
1506 ast_expression_codegen *cgen;
1507 ir_value *leftl, *leftr, *right, *bin;
1509 if (lvalue && self->expression.outl) {
1510 *out = self->expression.outl;
1514 if (!lvalue && self->expression.outr) {
1515 *out = self->expression.outr;
1519 /* for a binstore we need both an lvalue and an rvalue for the left side */
1520 /* rvalue of destination! */
1521 cgen = self->dest->expression.codegen;
1522 if (!(*cgen)((ast_expression*)(self->dest), func, false, &leftr))
1525 /* source as rvalue only */
1526 cgen = self->source->expression.codegen;
1527 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1530 /* now the binary */
1531 bin = ir_block_create_binop(func->curblock, ast_function_label(func, "binst"),
1532 self->opbin, leftr, right);
1533 self->expression.outr = bin;
1535 /* now store them */
1536 cgen = self->dest->expression.codegen;
1537 /* lvalue of destination */
1538 if (!(*cgen)((ast_expression*)(self->dest), func, true, &leftl))
1540 self->expression.outl = leftl;
1542 if (!ir_block_create_store_op(func->curblock, self->opstore, leftl, bin))
1544 self->expression.outr = bin;
1546 /* Theoretically, an assinment returns its left side as an
1547 * lvalue, if we don't need an lvalue though, we return
1548 * the right side as an rvalue, otherwise we have to
1549 * somehow know whether or not we need to dereference the pointer
1550 * on the left side - that is: OP_LOAD if it was an address.
1551 * Also: in original QC we cannot OP_LOADP *anyway*.
1553 *out = (lvalue ? leftl : bin);
1558 bool ast_unary_codegen(ast_unary *self, ast_function *func, bool lvalue, ir_value **out)
1560 ast_expression_codegen *cgen;
1563 /* An unary operation cannot yield an l-value */
1565 asterror(ast_ctx(self), "not an l-value (binop)");
1569 if (self->expression.outr) {
1570 *out = self->expression.outr;
1574 cgen = self->operand->expression.codegen;
1576 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
1579 *out = ir_block_create_unary(func->curblock, ast_function_label(func, "unary"),
1583 self->expression.outr = *out;
1588 bool ast_return_codegen(ast_return *self, ast_function *func, bool lvalue, ir_value **out)
1590 ast_expression_codegen *cgen;
1593 /* In the context of a return operation, we don't actually return
1597 asterror(ast_ctx(self), "return-expression is not an l-value");
1601 if (self->expression.outr) {
1602 asterror(ast_ctx(self), "internal error: ast_return cannot be reused, it bears no result!");
1605 self->expression.outr = (ir_value*)1;
1607 if (self->operand) {
1608 cgen = self->operand->expression.codegen;
1610 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
1613 if (!ir_block_create_return(func->curblock, operand))
1616 if (!ir_block_create_return(func->curblock, NULL))
1623 bool ast_entfield_codegen(ast_entfield *self, ast_function *func, bool lvalue, ir_value **out)
1625 ast_expression_codegen *cgen;
1626 ir_value *ent, *field;
1628 /* This function needs to take the 'lvalue' flag into account!
1629 * As lvalue we provide a field-pointer, as rvalue we provide the
1633 if (lvalue && self->expression.outl) {
1634 *out = self->expression.outl;
1638 if (!lvalue && self->expression.outr) {
1639 *out = self->expression.outr;
1643 cgen = self->entity->expression.codegen;
1644 if (!(*cgen)((ast_expression*)(self->entity), func, false, &ent))
1647 cgen = self->field->expression.codegen;
1648 if (!(*cgen)((ast_expression*)(self->field), func, false, &field))
1653 *out = ir_block_create_fieldaddress(func->curblock, ast_function_label(func, "efa"),
1656 *out = ir_block_create_load_from_ent(func->curblock, ast_function_label(func, "efv"),
1657 ent, field, self->expression.vtype);
1660 asterror(ast_ctx(self), "failed to create %s instruction (output type %s)",
1661 (lvalue ? "ADDRESS" : "FIELD"),
1662 type_name[self->expression.vtype]);
1667 self->expression.outl = *out;
1669 self->expression.outr = *out;
1671 /* Hm that should be it... */
1675 bool ast_member_codegen(ast_member *self, ast_function *func, bool lvalue, ir_value **out)
1677 ast_expression_codegen *cgen;
1680 /* in QC this is always an lvalue */
1682 if (self->expression.outl) {
1683 *out = self->expression.outl;
1687 cgen = self->owner->expression.codegen;
1688 if (!(*cgen)((ast_expression*)(self->owner), func, true, &vec))
1691 if (vec->vtype != TYPE_VECTOR &&
1692 !(vec->vtype == TYPE_FIELD && self->owner->expression.next->expression.vtype == TYPE_VECTOR))
1697 *out = ir_value_vector_member(vec, self->field);
1698 self->expression.outl = *out;
1700 return (*out != NULL);
1703 bool ast_array_index_codegen(ast_array_index *self, ast_function *func, bool lvalue, ir_value **out)
1708 if (!lvalue && self->expression.outr) {
1709 *out = self->expression.outr;
1711 if (lvalue && self->expression.outl) {
1712 *out = self->expression.outl;
1715 if (!ast_istype(self->array, ast_value)) {
1716 asterror(ast_ctx(self), "array indexing this way is not supported");
1717 /* note this would actually be pointer indexing because the left side is
1718 * not an actual array but (hopefully) an indexable expression.
1719 * Once we get integer arithmetic, and GADDRESS/GSTORE/GLOAD instruction
1720 * support this path will be filled.
1725 arr = (ast_value*)self->array;
1726 idx = (ast_value*)self->index;
1728 if (!ast_istype(self->index, ast_value) || !idx->isconst) {
1729 /* Time to use accessor functions */
1730 ast_expression_codegen *cgen;
1731 ir_value *iridx, *funval;
1735 asterror(ast_ctx(self), "(.2) array indexing here needs a compile-time constant");
1740 asterror(ast_ctx(self), "value has no getter, don't know how to index it");
1744 cgen = self->index->expression.codegen;
1745 if (!(*cgen)((ast_expression*)(self->index), func, true, &iridx))
1748 cgen = arr->getter->expression.codegen;
1749 if (!(*cgen)((ast_expression*)(arr->getter), func, true, &funval))
1752 call = ir_block_create_call(func->curblock, ast_function_label(func, "fetch"), funval);
1755 ir_call_param(call, iridx);
1757 *out = ir_call_value(call);
1758 self->expression.outr = *out;
1762 if (idx->expression.vtype == TYPE_FLOAT)
1763 *out = arr->ir_values[(int)idx->constval.vfloat];
1764 else if (idx->expression.vtype == TYPE_INTEGER)
1765 *out = arr->ir_values[idx->constval.vint];
1767 asterror(ast_ctx(self), "array indexing here needs an integer constant");
1773 bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_value **out)
1775 ast_expression_codegen *cgen;
1780 ir_block *cond = func->curblock;
1783 ir_block *ontrue_endblock = NULL;
1784 ir_block *onfalse_endblock = NULL;
1787 /* We don't output any value, thus also don't care about r/lvalue */
1791 if (self->expression.outr) {
1792 asterror(ast_ctx(self), "internal error: ast_ifthen cannot be reused, it bears no result!");
1795 self->expression.outr = (ir_value*)1;
1797 /* generate the condition */
1798 func->curblock = cond;
1799 cgen = self->cond->expression.codegen;
1800 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
1805 if (self->on_true) {
1806 /* create on-true block */
1807 ontrue = ir_function_create_block(func->ir_func, ast_function_label(func, "ontrue"));
1811 /* enter the block */
1812 func->curblock = ontrue;
1815 cgen = self->on_true->expression.codegen;
1816 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &dummy))
1819 /* we now need to work from the current endpoint */
1820 ontrue_endblock = func->curblock;
1825 if (self->on_false) {
1826 /* create on-false block */
1827 onfalse = ir_function_create_block(func->ir_func, ast_function_label(func, "onfalse"));
1831 /* enter the block */
1832 func->curblock = onfalse;
1835 cgen = self->on_false->expression.codegen;
1836 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &dummy))
1839 /* we now need to work from the current endpoint */
1840 onfalse_endblock = func->curblock;
1844 /* Merge block were they all merge in to */
1845 merge = ir_function_create_block(func->ir_func, ast_function_label(func, "endif"));
1849 /* add jumps ot the merge block */
1850 if (ontrue && !ontrue_endblock->final && !ir_block_create_jump(ontrue_endblock, merge))
1852 if (onfalse && !onfalse_endblock->final && !ir_block_create_jump(onfalse_endblock, merge))
1855 /* we create the if here, that way all blocks are ordered :)
1857 if (!ir_block_create_if(cond, condval,
1858 (ontrue ? ontrue : merge),
1859 (onfalse ? onfalse : merge)))
1864 /* Now enter the merge block */
1865 func->curblock = merge;
1870 bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_value **out)
1872 ast_expression_codegen *cgen;
1875 ir_value *trueval, *falseval;
1878 ir_block *cond = func->curblock;
1883 /* Ternary can never create an lvalue... */
1887 /* In theory it shouldn't be possible to pass through a node twice, but
1888 * in case we add any kind of optimization pass for the AST itself, it
1889 * may still happen, thus we remember a created ir_value and simply return one
1890 * if it already exists.
1892 if (self->phi_out) {
1893 *out = self->phi_out;
1897 /* In the following, contraty to ast_ifthen, we assume both paths exist. */
1899 /* generate the condition */
1900 func->curblock = cond;
1901 cgen = self->cond->expression.codegen;
1902 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
1905 /* create on-true block */
1906 ontrue = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_T"));
1911 /* enter the block */
1912 func->curblock = ontrue;
1915 cgen = self->on_true->expression.codegen;
1916 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &trueval))
1920 /* create on-false block */
1921 onfalse = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_F"));
1926 /* enter the block */
1927 func->curblock = onfalse;
1930 cgen = self->on_false->expression.codegen;
1931 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &falseval))
1935 /* create merge block */
1936 merge = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_out"));
1939 /* jump to merge block */
1940 if (!ir_block_create_jump(ontrue, merge))
1942 if (!ir_block_create_jump(onfalse, merge))
1945 /* create if instruction */
1946 if (!ir_block_create_if(cond, condval, ontrue, onfalse))
1949 /* Now enter the merge block */
1950 func->curblock = merge;
1952 /* Here, now, we need a PHI node
1953 * but first some sanity checking...
1955 if (trueval->vtype != falseval->vtype) {
1956 /* error("ternary with different types on the two sides"); */
1961 phi = ir_block_create_phi(merge, ast_function_label(func, "phi"), trueval->vtype);
1964 ir_phi_add(phi, ontrue, trueval);
1965 ir_phi_add(phi, onfalse, falseval);
1967 self->phi_out = ir_phi_value(phi);
1968 *out = self->phi_out;
1973 bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value **out)
1975 ast_expression_codegen *cgen;
1977 ir_value *dummy = NULL;
1978 ir_value *precond = NULL;
1979 ir_value *postcond = NULL;
1981 /* Since we insert some jumps "late" so we have blocks
1982 * ordered "nicely", we need to keep track of the actual end-blocks
1983 * of expressions to add the jumps to.
1985 ir_block *bbody = NULL, *end_bbody = NULL;
1986 ir_block *bprecond = NULL, *end_bprecond = NULL;
1987 ir_block *bpostcond = NULL, *end_bpostcond = NULL;
1988 ir_block *bincrement = NULL, *end_bincrement = NULL;
1989 ir_block *bout = NULL, *bin = NULL;
1991 /* let's at least move the outgoing block to the end */
1994 /* 'break' and 'continue' need to be able to find the right blocks */
1995 ir_block *bcontinue = NULL;
1996 ir_block *bbreak = NULL;
1998 ir_block *old_bcontinue = NULL;
1999 ir_block *old_bbreak = NULL;
2001 ir_block *tmpblock = NULL;
2006 if (self->expression.outr) {
2007 asterror(ast_ctx(self), "internal error: ast_loop cannot be reused, it bears no result!");
2010 self->expression.outr = (ir_value*)1;
2013 * Should we ever need some kind of block ordering, better make this function
2014 * move blocks around than write a block ordering algorithm later... after all
2015 * the ast and ir should work together, not against each other.
2018 /* initexpr doesn't get its own block, it's pointless, it could create more blocks
2019 * anyway if for example it contains a ternary.
2023 cgen = self->initexpr->expression.codegen;
2024 if (!(*cgen)((ast_expression*)(self->initexpr), func, false, &dummy))
2028 /* Store the block from which we enter this chaos */
2029 bin = func->curblock;
2031 /* The pre-loop condition needs its own block since we
2032 * need to be able to jump to the start of that expression.
2036 bprecond = ir_function_create_block(func->ir_func, ast_function_label(func, "pre_loop_cond"));
2040 /* the pre-loop-condition the least important place to 'continue' at */
2041 bcontinue = bprecond;
2044 func->curblock = bprecond;
2047 cgen = self->precond->expression.codegen;
2048 if (!(*cgen)((ast_expression*)(self->precond), func, false, &precond))
2051 end_bprecond = func->curblock;
2053 bprecond = end_bprecond = NULL;
2056 /* Now the next blocks won't be ordered nicely, but we need to
2057 * generate them this early for 'break' and 'continue'.
2059 if (self->increment) {
2060 bincrement = ir_function_create_block(func->ir_func, ast_function_label(func, "loop_increment"));
2063 bcontinue = bincrement; /* increment comes before the pre-loop-condition */
2065 bincrement = end_bincrement = NULL;
2068 if (self->postcond) {
2069 bpostcond = ir_function_create_block(func->ir_func, ast_function_label(func, "post_loop_cond"));
2072 bcontinue = bpostcond; /* postcond comes before the increment */
2074 bpostcond = end_bpostcond = NULL;
2077 bout_id = vec_size(func->ir_func->blocks);
2078 bout = ir_function_create_block(func->ir_func, ast_function_label(func, "after_loop"));
2083 /* The loop body... */
2086 bbody = ir_function_create_block(func->ir_func, ast_function_label(func, "loop_body"));
2091 func->curblock = bbody;
2093 old_bbreak = func->breakblock;
2094 old_bcontinue = func->continueblock;
2095 func->breakblock = bbreak;
2096 func->continueblock = bcontinue;
2099 cgen = self->body->expression.codegen;
2100 if (!(*cgen)((ast_expression*)(self->body), func, false, &dummy))
2103 end_bbody = func->curblock;
2104 func->breakblock = old_bbreak;
2105 func->continueblock = old_bcontinue;
2108 /* post-loop-condition */
2112 func->curblock = bpostcond;
2115 cgen = self->postcond->expression.codegen;
2116 if (!(*cgen)((ast_expression*)(self->postcond), func, false, &postcond))
2119 end_bpostcond = func->curblock;
2122 /* The incrementor */
2123 if (self->increment)
2126 func->curblock = bincrement;
2129 cgen = self->increment->expression.codegen;
2130 if (!(*cgen)((ast_expression*)(self->increment), func, false, &dummy))
2133 end_bincrement = func->curblock;
2136 /* In any case now, we continue from the outgoing block */
2137 func->curblock = bout;
2139 /* Now all blocks are in place */
2140 /* From 'bin' we jump to whatever comes first */
2141 if (bprecond) tmpblock = bprecond;
2142 else if (bbody) tmpblock = bbody;
2143 else if (bpostcond) tmpblock = bpostcond;
2144 else tmpblock = bout;
2145 if (!ir_block_create_jump(bin, tmpblock))
2151 ir_block *ontrue, *onfalse;
2152 if (bbody) ontrue = bbody;
2153 else if (bincrement) ontrue = bincrement;
2154 else if (bpostcond) ontrue = bpostcond;
2155 else ontrue = bprecond;
2157 if (!ir_block_create_if(end_bprecond, precond, ontrue, onfalse))
2164 if (bincrement) tmpblock = bincrement;
2165 else if (bpostcond) tmpblock = bpostcond;
2166 else if (bprecond) tmpblock = bprecond;
2167 else tmpblock = bout;
2168 if (!end_bbody->final && !ir_block_create_jump(end_bbody, tmpblock))
2172 /* from increment */
2175 if (bpostcond) tmpblock = bpostcond;
2176 else if (bprecond) tmpblock = bprecond;
2177 else if (bbody) tmpblock = bbody;
2178 else tmpblock = bout;
2179 if (!ir_block_create_jump(end_bincrement, tmpblock))
2186 ir_block *ontrue, *onfalse;
2187 if (bprecond) ontrue = bprecond;
2188 else if (bbody) ontrue = bbody;
2189 else if (bincrement) ontrue = bincrement;
2190 else ontrue = bpostcond;
2192 if (!ir_block_create_if(end_bpostcond, postcond, ontrue, onfalse))
2196 /* Move 'bout' to the end */
2197 vec_remove(func->ir_func->blocks, bout_id, 1);
2198 vec_push(func->ir_func->blocks, bout);
2203 bool ast_breakcont_codegen(ast_breakcont *self, ast_function *func, bool lvalue, ir_value **out)
2208 asterror(ast_ctx(self), "break/continue expression is not an l-value");
2212 if (self->expression.outr) {
2213 asterror(ast_ctx(self), "internal error: ast_breakcont cannot be reused!");
2216 self->expression.outr = (ir_value*)1;
2218 if (self->is_continue)
2219 target = func->continueblock;
2221 target = func->breakblock;
2223 if (!ir_block_create_jump(func->curblock, target))
2228 bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value **out)
2230 ast_expression_codegen *cgen;
2232 ir_instr *callinstr;
2235 ir_value *funval = NULL;
2237 /* return values are never lvalues */
2239 asterror(ast_ctx(self), "not an l-value (function call)");
2243 if (self->expression.outr) {
2244 *out = self->expression.outr;
2248 cgen = self->func->expression.codegen;
2249 if (!(*cgen)((ast_expression*)(self->func), func, false, &funval))
2257 for (i = 0; i < vec_size(self->params); ++i)
2260 ast_expression *expr = self->params[i];
2262 cgen = expr->expression.codegen;
2263 if (!(*cgen)(expr, func, false, ¶m))
2267 vec_push(params, param);
2270 callinstr = ir_block_create_call(func->curblock, ast_function_label(func, "call"), funval);
2274 for (i = 0; i < vec_size(params); ++i) {
2275 ir_call_param(callinstr, params[i]);
2278 *out = ir_call_value(callinstr);
2279 self->expression.outr = *out;