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)
51 con_err("ast node missing destroy()\n");
55 /* Initialize main ast node aprts */
56 static void ast_node_init(ast_node *self, lex_ctx ctx, int nodetype)
58 self->node.context = ctx;
59 self->node.destroy = &_ast_node_destroy;
60 self->node.keep = false;
61 self->node.nodetype = nodetype;
64 /* General expression initialization */
65 static void ast_expression_init(ast_expression *self,
66 ast_expression_codegen *codegen)
68 self->expression.codegen = codegen;
69 self->expression.vtype = TYPE_VOID;
70 self->expression.next = NULL;
71 self->expression.outl = NULL;
72 self->expression.outr = NULL;
73 self->expression.variadic = false;
74 self->expression.params = NULL;
77 static void ast_expression_delete(ast_expression *self)
80 if (self->expression.next)
81 ast_delete(self->expression.next);
82 for (i = 0; i < vec_size(self->expression.params); ++i) {
83 ast_delete(self->expression.params[i]);
85 vec_free(self->expression.params);
88 static void ast_expression_delete_full(ast_expression *self)
90 ast_expression_delete(self);
94 ast_value* ast_value_copy(const ast_value *self)
97 const ast_expression_common *fromex;
98 ast_expression_common *selfex;
99 ast_value *cp = ast_value_new(self->expression.node.context, self->name, self->expression.vtype);
100 if (self->expression.next) {
101 cp->expression.next = ast_type_copy(self->expression.node.context, self->expression.next);
102 if (!cp->expression.next) {
103 ast_value_delete(cp);
107 fromex = &self->expression;
108 selfex = &cp->expression;
109 selfex->variadic = fromex->variadic;
110 for (i = 0; i < vec_size(fromex->params); ++i) {
111 ast_value *v = ast_value_copy(fromex->params[i]);
113 ast_value_delete(cp);
116 vec_push(selfex->params, v);
121 bool ast_type_adopt_impl(ast_expression *self, const ast_expression *other)
124 const ast_expression_common *fromex;
125 ast_expression_common *selfex;
126 self->expression.vtype = other->expression.vtype;
127 if (other->expression.next) {
128 self->expression.next = (ast_expression*)ast_type_copy(ast_ctx(self), other->expression.next);
129 if (!self->expression.next)
132 fromex = &other->expression;
133 selfex = &self->expression;
134 selfex->variadic = fromex->variadic;
135 for (i = 0; i < vec_size(fromex->params); ++i) {
136 ast_value *v = ast_value_copy(fromex->params[i]);
139 vec_push(selfex->params, v);
144 static ast_expression* ast_shallow_type(lex_ctx ctx, int vtype)
146 ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
147 ast_expression_init(self, NULL);
148 self->expression.codegen = NULL;
149 self->expression.next = NULL;
150 self->expression.vtype = vtype;
154 ast_expression* ast_type_copy(lex_ctx ctx, const ast_expression *ex)
157 const ast_expression_common *fromex;
158 ast_expression_common *selfex;
164 ast_instantiate(ast_expression, ctx, ast_expression_delete_full);
165 ast_expression_init(self, NULL);
167 fromex = &ex->expression;
168 selfex = &self->expression;
170 /* This may never be codegen()d */
171 selfex->codegen = NULL;
173 selfex->vtype = fromex->vtype;
176 selfex->next = ast_type_copy(ctx, fromex->next);
178 ast_expression_delete_full(self);
185 selfex->variadic = fromex->variadic;
186 for (i = 0; i < vec_size(fromex->params); ++i) {
187 ast_value *v = ast_value_copy(fromex->params[i]);
189 ast_expression_delete_full(self);
192 vec_push(selfex->params, v);
199 bool ast_compare_type(ast_expression *a, ast_expression *b)
201 if (a->expression.vtype != b->expression.vtype)
203 if (!a->expression.next != !b->expression.next)
205 if (vec_size(a->expression.params) != vec_size(b->expression.params))
207 if (a->expression.variadic != b->expression.variadic)
209 if (vec_size(a->expression.params)) {
211 for (i = 0; i < vec_size(a->expression.params); ++i) {
212 if (!ast_compare_type((ast_expression*)a->expression.params[i],
213 (ast_expression*)b->expression.params[i]))
217 if (a->expression.next)
218 return ast_compare_type(a->expression.next, b->expression.next);
222 static size_t ast_type_to_string_impl(ast_expression *e, char *buf, size_t bufsize, size_t pos)
229 if (pos + 6 >= bufsize)
231 strcpy(buf + pos, "(null)");
235 if (pos + 1 >= bufsize)
238 switch (e->expression.vtype) {
240 strcpy(buf + pos, "(variant)");
245 return ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
248 if (pos + 3 >= bufsize)
252 pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
253 if (pos + 1 >= bufsize)
259 pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
260 if (pos + 2 >= bufsize)
262 if (!vec_size(e->expression.params)) {
268 pos = ast_type_to_string_impl((ast_expression*)(e->expression.params[0]), buf, bufsize, pos);
269 for (i = 1; i < vec_size(e->expression.params); ++i) {
270 if (pos + 2 >= bufsize)
274 pos = ast_type_to_string_impl((ast_expression*)(e->expression.params[i]), buf, bufsize, pos);
276 if (pos + 1 >= bufsize)
282 pos = ast_type_to_string_impl(e->expression.next, buf, bufsize, pos);
283 if (pos + 1 >= bufsize)
286 pos += snprintf(buf + pos, bufsize - pos - 1, "%i", (int)e->expression.count);
287 if (pos + 1 >= bufsize)
293 typestr = type_name[e->expression.vtype];
294 typelen = strlen(typestr);
295 if (pos + typelen >= bufsize)
297 strcpy(buf + pos, typestr);
298 return pos + typelen;
302 buf[bufsize-3] = '.';
303 buf[bufsize-2] = '.';
304 buf[bufsize-1] = '.';
308 void ast_type_to_string(ast_expression *e, char *buf, size_t bufsize)
310 size_t pos = ast_type_to_string_impl(e, buf, bufsize-1, 0);
314 ast_value* ast_value_new(lex_ctx ctx, const char *name, int t)
316 ast_instantiate(ast_value, ctx, ast_value_delete);
317 ast_expression_init((ast_expression*)self,
318 (ast_expression_codegen*)&ast_value_codegen);
319 self->expression.node.keep = true; /* keep */
321 self->name = name ? util_strdup(name) : NULL;
322 self->expression.vtype = t;
323 self->expression.next = NULL;
324 self->isconst = false;
326 memset(&self->constval, 0, sizeof(self->constval));
329 self->ir_values = NULL;
330 self->ir_value_count = 0;
338 void ast_value_delete(ast_value* self)
341 mem_d((void*)self->name);
343 switch (self->expression.vtype)
346 mem_d((void*)self->constval.vstring);
349 /* unlink us from the function node */
350 self->constval.vfunc->vtype = NULL;
352 /* NOTE: delete function? currently collected in
353 * the parser structure
360 mem_d(self->ir_values);
361 ast_expression_delete((ast_expression*)self);
365 void ast_value_params_add(ast_value *self, ast_value *p)
367 vec_push(self->expression.params, p);
370 bool ast_value_set_name(ast_value *self, const char *name)
373 mem_d((void*)self->name);
374 self->name = util_strdup(name);
378 ast_binary* ast_binary_new(lex_ctx ctx, int op,
379 ast_expression* left, ast_expression* right)
381 ast_instantiate(ast_binary, ctx, ast_binary_delete);
382 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binary_codegen);
388 if (op >= INSTR_EQ_F && op <= INSTR_GT)
389 self->expression.vtype = TYPE_FLOAT;
390 else if (op == INSTR_AND || op == INSTR_OR ||
391 op == INSTR_BITAND || op == INSTR_BITOR)
392 self->expression.vtype = TYPE_FLOAT;
393 else if (op == INSTR_MUL_VF || op == INSTR_MUL_FV)
394 self->expression.vtype = TYPE_VECTOR;
395 else if (op == INSTR_MUL_V)
396 self->expression.vtype = TYPE_FLOAT;
398 self->expression.vtype = left->expression.vtype;
403 void ast_binary_delete(ast_binary *self)
405 ast_unref(self->left);
406 ast_unref(self->right);
407 ast_expression_delete((ast_expression*)self);
411 ast_binstore* ast_binstore_new(lex_ctx ctx, int storop, int op,
412 ast_expression* left, ast_expression* right)
414 ast_instantiate(ast_binstore, ctx, ast_binstore_delete);
415 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binstore_codegen);
417 self->opstore = storop;
420 self->source = right;
422 self->expression.vtype = left->expression.vtype;
423 if (left->expression.next) {
424 self->expression.next = ast_type_copy(ctx, left);
425 if (!self->expression.next) {
431 self->expression.next = NULL;
436 void ast_binstore_delete(ast_binstore *self)
438 ast_unref(self->dest);
439 ast_unref(self->source);
440 ast_expression_delete((ast_expression*)self);
444 ast_unary* ast_unary_new(lex_ctx ctx, int op,
445 ast_expression *expr)
447 ast_instantiate(ast_unary, ctx, ast_unary_delete);
448 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_unary_codegen);
451 self->operand = expr;
453 if (op >= INSTR_NOT_F && op <= INSTR_NOT_FNC) {
454 self->expression.vtype = TYPE_FLOAT;
456 asterror(ctx, "cannot determine type of unary operation %s", asm_instr[op].m);
461 void ast_unary_delete(ast_unary *self)
463 ast_unref(self->operand);
464 ast_expression_delete((ast_expression*)self);
468 ast_return* ast_return_new(lex_ctx ctx, ast_expression *expr)
470 ast_instantiate(ast_return, ctx, ast_return_delete);
471 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_return_codegen);
473 self->operand = expr;
478 void ast_return_delete(ast_return *self)
481 ast_unref(self->operand);
482 ast_expression_delete((ast_expression*)self);
486 ast_entfield* ast_entfield_new(lex_ctx ctx, ast_expression *entity, ast_expression *field)
488 if (field->expression.vtype != TYPE_FIELD) {
489 asterror(ctx, "ast_entfield_new with expression not of type field");
492 return ast_entfield_new_force(ctx, entity, field, field->expression.next);
495 ast_entfield* ast_entfield_new_force(lex_ctx ctx, ast_expression *entity, ast_expression *field, const ast_expression *outtype)
497 ast_instantiate(ast_entfield, ctx, ast_entfield_delete);
501 /* Error: field has no type... */
505 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_entfield_codegen);
507 self->entity = entity;
510 if (!ast_type_adopt(self, outtype)) {
511 ast_entfield_delete(self);
518 void ast_entfield_delete(ast_entfield *self)
520 ast_unref(self->entity);
521 ast_unref(self->field);
522 ast_expression_delete((ast_expression*)self);
526 ast_member* ast_member_new(lex_ctx ctx, ast_expression *owner, unsigned int field)
528 ast_instantiate(ast_member, ctx, ast_member_delete);
534 if (owner->expression.vtype != TYPE_VECTOR &&
535 owner->expression.vtype != TYPE_FIELD) {
536 asterror(ctx, "member-access on an invalid owner of type %s", type_name[owner->expression.vtype]);
541 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_member_codegen);
542 self->expression.node.keep = true; /* keep */
544 if (owner->expression.vtype == TYPE_VECTOR) {
545 self->expression.vtype = TYPE_FLOAT;
546 self->expression.next = NULL;
548 self->expression.vtype = TYPE_FIELD;
549 self->expression.next = ast_shallow_type(ctx, TYPE_FLOAT);
558 void ast_member_delete(ast_member *self)
560 /* The owner is always an ast_value, which has .keep=true,
561 * also: ast_members are usually deleted after the owner, thus
562 * this will cause invalid access
563 ast_unref(self->owner);
564 * once we allow (expression).x to access a vector-member, we need
565 * to change this: preferably by creating an alternate ast node for this
566 * purpose that is not garbage-collected.
568 ast_expression_delete((ast_expression*)self);
572 ast_array_index* ast_array_index_new(lex_ctx ctx, ast_expression *array, ast_expression *index)
574 ast_expression *outtype;
575 ast_instantiate(ast_array_index, ctx, ast_array_index_delete);
577 outtype = array->expression.next;
580 /* Error: field has no type... */
584 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_array_index_codegen);
589 if (!ast_type_adopt(self, outtype)) {
590 ast_array_index_delete(self);
593 if (array->expression.vtype == TYPE_FIELD && outtype->expression.vtype == TYPE_ARRAY) {
594 if (self->expression.vtype != TYPE_ARRAY) {
595 asterror(ast_ctx(self), "array_index node on type");
596 ast_array_index_delete(self);
599 self->array = outtype;
600 self->expression.vtype = TYPE_FIELD;
606 void ast_array_index_delete(ast_array_index *self)
608 ast_unref(self->array);
609 ast_unref(self->index);
610 ast_expression_delete((ast_expression*)self);
614 ast_ifthen* ast_ifthen_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
616 ast_instantiate(ast_ifthen, ctx, ast_ifthen_delete);
617 if (!ontrue && !onfalse) {
618 /* because it is invalid */
622 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ifthen_codegen);
625 self->on_true = ontrue;
626 self->on_false = onfalse;
631 void ast_ifthen_delete(ast_ifthen *self)
633 ast_unref(self->cond);
635 ast_unref(self->on_true);
637 ast_unref(self->on_false);
638 ast_expression_delete((ast_expression*)self);
642 ast_ternary* ast_ternary_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
644 ast_instantiate(ast_ternary, ctx, ast_ternary_delete);
645 /* This time NEITHER must be NULL */
646 if (!ontrue || !onfalse) {
650 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ternary_codegen);
653 self->on_true = ontrue;
654 self->on_false = onfalse;
656 if (!ast_type_adopt(self, ontrue)) {
657 ast_ternary_delete(self);
664 void ast_ternary_delete(ast_ternary *self)
666 ast_unref(self->cond);
667 ast_unref(self->on_true);
668 ast_unref(self->on_false);
669 ast_expression_delete((ast_expression*)self);
673 ast_loop* ast_loop_new(lex_ctx ctx,
674 ast_expression *initexpr,
675 ast_expression *precond,
676 ast_expression *postcond,
677 ast_expression *increment,
678 ast_expression *body)
680 ast_instantiate(ast_loop, ctx, ast_loop_delete);
681 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_loop_codegen);
683 self->initexpr = initexpr;
684 self->precond = precond;
685 self->postcond = postcond;
686 self->increment = increment;
692 void ast_loop_delete(ast_loop *self)
695 ast_unref(self->initexpr);
697 ast_unref(self->precond);
699 ast_unref(self->postcond);
701 ast_unref(self->increment);
703 ast_unref(self->body);
704 ast_expression_delete((ast_expression*)self);
708 ast_breakcont* ast_breakcont_new(lex_ctx ctx, bool iscont)
710 ast_instantiate(ast_breakcont, ctx, ast_breakcont_delete);
711 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_breakcont_codegen);
713 self->is_continue = iscont;
718 void ast_breakcont_delete(ast_breakcont *self)
720 ast_expression_delete((ast_expression*)self);
724 ast_switch* ast_switch_new(lex_ctx ctx, ast_expression *op)
726 ast_instantiate(ast_switch, ctx, ast_switch_delete);
727 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_switch_codegen);
735 void ast_switch_delete(ast_switch *self)
738 ast_unref(self->operand);
740 for (i = 0; i < vec_size(self->cases); ++i) {
741 if (self->cases[i].value)
742 ast_unref(self->cases[i].value);
743 ast_unref(self->cases[i].code);
745 vec_free(self->cases);
747 ast_expression_delete((ast_expression*)self);
751 ast_call* ast_call_new(lex_ctx ctx,
752 ast_expression *funcexpr)
754 ast_instantiate(ast_call, ctx, ast_call_delete);
755 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_call_codegen);
758 self->func = funcexpr;
760 self->expression.vtype = funcexpr->expression.next->expression.vtype;
761 if (funcexpr->expression.next->expression.next)
762 self->expression.next = ast_type_copy(ctx, funcexpr->expression.next->expression.next);
767 void ast_call_delete(ast_call *self)
770 for (i = 0; i < vec_size(self->params); ++i)
771 ast_unref(self->params[i]);
772 vec_free(self->params);
775 ast_unref(self->func);
777 ast_expression_delete((ast_expression*)self);
781 bool ast_call_check_types(ast_call *self)
785 const ast_expression *func = self->func;
786 size_t count = vec_size(self->params);
787 if (count > vec_size(func->expression.params))
788 count = vec_size(func->expression.params);
790 for (i = 0; i < count; ++i) {
791 if (!ast_compare_type(self->params[i], (ast_expression*)(func->expression.params[i]))) {
794 ast_type_to_string(self->params[i], tgot, sizeof(tgot));
795 ast_type_to_string((ast_expression*)func->expression.params[i], texp, sizeof(texp));
796 asterror(ast_ctx(self), "invalid type for parameter %u in function call: expected %s, got %s",
797 (unsigned int)(i+1), texp, tgot);
798 /* we don't immediately return */
805 ast_store* ast_store_new(lex_ctx ctx, int op,
806 ast_expression *dest, ast_expression *source)
808 ast_instantiate(ast_store, ctx, ast_store_delete);
809 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_store_codegen);
813 self->source = source;
815 self->expression.vtype = dest->expression.vtype;
816 if (dest->expression.next) {
817 self->expression.next = ast_type_copy(ctx, dest);
818 if (!self->expression.next) {
824 self->expression.next = NULL;
829 void ast_store_delete(ast_store *self)
831 ast_unref(self->dest);
832 ast_unref(self->source);
833 ast_expression_delete((ast_expression*)self);
837 ast_block* ast_block_new(lex_ctx ctx)
839 ast_instantiate(ast_block, ctx, ast_block_delete);
840 ast_expression_init((ast_expression*)self,
841 (ast_expression_codegen*)&ast_block_codegen);
845 self->collect = NULL;
850 void ast_block_collect(ast_block *self, ast_expression *expr)
852 vec_push(self->collect, expr);
853 expr->expression.node.keep = true;
856 void ast_block_delete(ast_block *self)
859 for (i = 0; i < vec_size(self->exprs); ++i)
860 ast_unref(self->exprs[i]);
861 vec_free(self->exprs);
862 for (i = 0; i < vec_size(self->locals); ++i)
863 ast_delete(self->locals[i]);
864 vec_free(self->locals);
865 for (i = 0; i < vec_size(self->collect); ++i)
866 ast_delete(self->collect[i]);
867 vec_free(self->collect);
868 ast_expression_delete((ast_expression*)self);
872 bool ast_block_set_type(ast_block *self, ast_expression *from)
874 if (self->expression.next)
875 ast_delete(self->expression.next);
876 self->expression.vtype = from->expression.vtype;
877 if (from->expression.next) {
878 self->expression.next = ast_type_copy(self->expression.node.context, from->expression.next);
879 if (!self->expression.next)
883 self->expression.next = NULL;
887 ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype)
889 ast_instantiate(ast_function, ctx, ast_function_delete);
893 vtype->expression.vtype != TYPE_FUNCTION)
895 asterror(ast_ctx(self), "internal error: ast_function_new condition %i %i type=%i",
898 vtype->expression.vtype);
904 self->name = name ? util_strdup(name) : NULL;
907 self->labelcount = 0;
910 self->ir_func = NULL;
911 self->curblock = NULL;
913 self->breakblock = NULL;
914 self->continueblock = NULL;
916 vtype->isconst = true;
917 vtype->constval.vfunc = self;
922 void ast_function_delete(ast_function *self)
926 mem_d((void*)self->name);
928 /* ast_value_delete(self->vtype); */
929 self->vtype->isconst = false;
930 self->vtype->constval.vfunc = NULL;
931 /* We use unref - if it was stored in a global table it is supposed
932 * to be deleted from *there*
934 ast_unref(self->vtype);
936 for (i = 0; i < vec_size(self->blocks); ++i)
937 ast_delete(self->blocks[i]);
938 vec_free(self->blocks);
942 const char* ast_function_label(ast_function *self, const char *prefix)
948 if (!opts_dump && !opts_dumpfin)
951 id = (self->labelcount++);
952 len = strlen(prefix);
954 from = self->labelbuf + sizeof(self->labelbuf)-1;
957 unsigned int digit = id % 10;
961 memcpy(from - len, prefix, len);
965 /*********************************************************************/
967 * by convention you must never pass NULL to the 'ir_value **out'
968 * parameter. If you really don't care about the output, pass a dummy.
969 * But I can't imagine a pituation where the output is truly unnecessary.
972 bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out)
976 /* NOTE: This is the codegen for a variable used in an expression.
977 * It is not the codegen to generate the value. For this purpose,
978 * ast_local_codegen and ast_global_codegen are to be used before this
979 * is executed. ast_function_codegen should take care of its locals,
980 * and the ast-user should take care of ast_global_codegen to be used
981 * on all the globals.
985 ast_type_to_string((ast_expression*)self, typename, sizeof(typename));
986 asterror(ast_ctx(self), "ast_value used before generated %s %s", typename, self->name);
993 bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
997 if (self->isconst && self->expression.vtype == TYPE_FUNCTION)
999 ir_function *func = ir_builder_create_function(ir, self->name, self->expression.next->expression.vtype);
1002 func->context = ast_ctx(self);
1003 func->value->context = ast_ctx(self);
1005 self->constval.vfunc->ir_func = func;
1006 self->ir_v = func->value;
1007 /* The function is filled later on ast_function_codegen... */
1011 if (isfield && self->expression.vtype == TYPE_FIELD) {
1012 ast_expression *fieldtype = self->expression.next;
1014 if (self->isconst) {
1015 asterror(ast_ctx(self), "TODO: constant field pointers with value");
1019 if (fieldtype->expression.vtype == TYPE_ARRAY) {
1024 ast_expression_common *elemtype;
1026 ast_value *array = (ast_value*)fieldtype;
1028 if (!ast_istype(fieldtype, ast_value)) {
1029 asterror(ast_ctx(self), "internal error: ast_value required");
1033 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1034 if (!array->expression.count || array->expression.count > opts_max_array_size)
1035 asterror(ast_ctx(self), "Invalid array of size %lu", (unsigned long)array->expression.count);
1037 elemtype = &array->expression.next->expression;
1038 vtype = elemtype->vtype;
1040 v = ir_builder_create_field(ir, self->name, vtype);
1042 asterror(ast_ctx(self), "ir_builder_create_global failed");
1045 if (vtype == TYPE_FIELD)
1046 v->fieldtype = elemtype->next->expression.vtype;
1047 v->context = ast_ctx(self);
1048 array->ir_v = self->ir_v = v;
1050 namelen = strlen(self->name);
1051 name = (char*)mem_a(namelen + 16);
1052 strcpy(name, self->name);
1054 array->ir_values = (ir_value**)mem_a(sizeof(array->ir_values[0]) * array->expression.count);
1055 array->ir_values[0] = v;
1056 for (ai = 1; ai < array->expression.count; ++ai) {
1057 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1058 array->ir_values[ai] = ir_builder_create_field(ir, name, vtype);
1059 if (!array->ir_values[ai]) {
1061 asterror(ast_ctx(self), "ir_builder_create_global failed");
1064 if (vtype == TYPE_FIELD)
1065 array->ir_values[ai]->fieldtype = elemtype->next->expression.vtype;
1066 array->ir_values[ai]->context = ast_ctx(self);
1072 v = ir_builder_create_field(ir, self->name, self->expression.next->expression.vtype);
1075 v->context = ast_ctx(self);
1081 if (self->expression.vtype == TYPE_ARRAY) {
1086 ast_expression_common *elemtype = &self->expression.next->expression;
1087 int vtype = elemtype->vtype;
1089 /* same as with field arrays */
1090 if (!self->expression.count || self->expression.count > opts_max_array_size)
1091 asterror(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1093 v = ir_builder_create_global(ir, self->name, vtype);
1095 asterror(ast_ctx(self), "ir_builder_create_global failed");
1098 if (vtype == TYPE_FIELD)
1099 v->fieldtype = elemtype->next->expression.vtype;
1100 v->context = ast_ctx(self);
1102 namelen = strlen(self->name);
1103 name = (char*)mem_a(namelen + 16);
1104 strcpy(name, self->name);
1106 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1107 self->ir_values[0] = v;
1108 for (ai = 1; ai < self->expression.count; ++ai) {
1109 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1110 self->ir_values[ai] = ir_builder_create_global(ir, name, vtype);
1111 if (!self->ir_values[ai]) {
1113 asterror(ast_ctx(self), "ir_builder_create_global failed");
1116 if (vtype == TYPE_FIELD)
1117 self->ir_values[ai]->fieldtype = elemtype->next->expression.vtype;
1118 self->ir_values[ai]->context = ast_ctx(self);
1124 /* Arrays don't do this since there's no "array" value which spans across the
1127 v = ir_builder_create_global(ir, self->name, self->expression.vtype);
1129 asterror(ast_ctx(self), "ir_builder_create_global failed");
1132 if (self->expression.vtype == TYPE_FIELD)
1133 v->fieldtype = self->expression.next->expression.vtype;
1134 v->context = ast_ctx(self);
1137 if (self->isconst) {
1138 switch (self->expression.vtype)
1141 if (!ir_value_set_float(v, self->constval.vfloat))
1145 if (!ir_value_set_vector(v, self->constval.vvec))
1149 if (!ir_value_set_string(v, self->constval.vstring))
1153 asterror(ast_ctx(self), "TODO: global constant array");
1156 asterror(ast_ctx(self), "global of type function not properly generated");
1158 /* Cannot generate an IR value for a function,
1159 * need a pointer pointing to a function rather.
1162 asterror(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1167 /* link us to the ir_value */
1171 error: /* clean up */
1176 bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
1179 if (self->isconst && self->expression.vtype == TYPE_FUNCTION)
1181 /* Do we allow local functions? I think not...
1182 * this is NOT a function pointer atm.
1187 if (self->expression.vtype == TYPE_ARRAY) {
1192 ast_expression_common *elemtype = &self->expression.next->expression;
1193 int vtype = elemtype->vtype;
1196 asterror(ast_ctx(self), "array-parameters are not supported");
1200 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1201 if (!self->expression.count || self->expression.count > opts_max_array_size) {
1202 asterror(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1205 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1206 if (!self->ir_values) {
1207 asterror(ast_ctx(self), "failed to allocate array values");
1211 v = ir_function_create_local(func, self->name, vtype, param);
1213 asterror(ast_ctx(self), "ir_function_create_local failed");
1216 if (vtype == TYPE_FIELD)
1217 v->fieldtype = elemtype->next->expression.vtype;
1218 v->context = ast_ctx(self);
1220 namelen = strlen(self->name);
1221 name = (char*)mem_a(namelen + 16);
1222 strcpy(name, self->name);
1224 self->ir_values[0] = v;
1225 for (ai = 1; ai < self->expression.count; ++ai) {
1226 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1227 self->ir_values[ai] = ir_function_create_local(func, name, vtype, param);
1228 if (!self->ir_values[ai]) {
1229 asterror(ast_ctx(self), "ir_builder_create_global failed");
1232 if (vtype == TYPE_FIELD)
1233 self->ir_values[ai]->fieldtype = elemtype->next->expression.vtype;
1234 self->ir_values[ai]->context = ast_ctx(self);
1239 v = ir_function_create_local(func, self->name, self->expression.vtype, param);
1242 if (self->expression.vtype == TYPE_FIELD)
1243 v->fieldtype = self->expression.next->expression.vtype;
1244 v->context = ast_ctx(self);
1247 /* A constant local... hmmm...
1248 * I suppose the IR will have to deal with this
1250 if (self->isconst) {
1251 switch (self->expression.vtype)
1254 if (!ir_value_set_float(v, self->constval.vfloat))
1258 if (!ir_value_set_vector(v, self->constval.vvec))
1262 if (!ir_value_set_string(v, self->constval.vstring))
1266 asterror(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1271 /* link us to the ir_value */
1275 if (!ast_global_codegen(self->setter, func->owner, false) ||
1276 !ast_function_codegen(self->setter->constval.vfunc, func->owner) ||
1277 !ir_function_finalize(self->setter->constval.vfunc->ir_func))
1281 if (!ast_global_codegen(self->getter, func->owner, false) ||
1282 !ast_function_codegen(self->getter->constval.vfunc, func->owner) ||
1283 !ir_function_finalize(self->getter->constval.vfunc->ir_func))
1288 error: /* clean up */
1293 bool ast_function_codegen(ast_function *self, ir_builder *ir)
1297 ast_expression_common *ec;
1302 irf = self->ir_func;
1304 asterror(ast_ctx(self), "ast_function's related ast_value was not generated yet");
1308 /* fill the parameter list */
1309 ec = &self->vtype->expression;
1310 for (i = 0; i < vec_size(ec->params); ++i)
1312 vec_push(irf->params, ec->params[i]->expression.vtype);
1313 if (!self->builtin) {
1314 if (!ast_local_codegen(ec->params[i], self->ir_func, true))
1319 if (self->builtin) {
1320 irf->builtin = self->builtin;
1324 if (!vec_size(self->blocks)) {
1325 asterror(ast_ctx(self), "function `%s` has no body", self->name);
1329 self->curblock = ir_function_create_block(irf, "entry");
1330 if (!self->curblock) {
1331 asterror(ast_ctx(self), "failed to allocate entry block for `%s`", self->name);
1335 for (i = 0; i < vec_size(self->blocks); ++i) {
1336 ast_expression_codegen *gen = self->blocks[i]->expression.codegen;
1337 if (!(*gen)((ast_expression*)self->blocks[i], self, false, &dummy))
1341 /* TODO: check return types */
1342 if (!self->curblock->is_return)
1344 return ir_block_create_return(self->curblock, NULL);
1345 /* From now on the parser has to handle this situation */
1347 if (!self->vtype->expression.next ||
1348 self->vtype->expression.next->expression.vtype == TYPE_VOID)
1350 return ir_block_create_return(self->curblock, NULL);
1354 /* error("missing return"); */
1355 asterror(ast_ctx(self), "function `%s` missing return value", self->name);
1363 /* Note, you will not see ast_block_codegen generate ir_blocks.
1364 * To the AST and the IR, blocks are 2 different things.
1365 * In the AST it represents a block of code, usually enclosed in
1366 * curly braces {...}.
1367 * While in the IR it represents a block in terms of control-flow.
1369 bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_value **out)
1373 /* We don't use this
1374 * Note: an ast-representation using the comma-operator
1375 * of the form: (a, b, c) = x should not assign to c...
1378 asterror(ast_ctx(self), "not an l-value (code-block)");
1382 if (self->expression.outr) {
1383 *out = self->expression.outr;
1387 /* output is NULL at first, we'll have each expression
1388 * assign to out output, thus, a comma-operator represention
1389 * using an ast_block will return the last generated value,
1390 * so: (b, c) + a executed both b and c, and returns c,
1391 * which is then added to a.
1395 /* generate locals */
1396 for (i = 0; i < vec_size(self->locals); ++i)
1398 if (!ast_local_codegen(self->locals[i], func->ir_func, false)) {
1400 asterror(ast_ctx(self), "failed to generate local `%s`", self->locals[i]->name);
1405 for (i = 0; i < vec_size(self->exprs); ++i)
1407 ast_expression_codegen *gen = self->exprs[i]->expression.codegen;
1408 if (func->curblock->final) {
1409 asterror(ast_ctx(self->exprs[i]), "unreachable statement");
1412 if (!(*gen)(self->exprs[i], func, false, out))
1416 self->expression.outr = *out;
1421 bool ast_store_codegen(ast_store *self, ast_function *func, bool lvalue, ir_value **out)
1423 ast_expression_codegen *cgen;
1424 ir_value *left = NULL;
1425 ir_value *right = NULL;
1429 ast_array_index *ai = NULL;
1431 if (lvalue && self->expression.outl) {
1432 *out = self->expression.outl;
1436 if (!lvalue && self->expression.outr) {
1437 *out = self->expression.outr;
1441 if (ast_istype(self->dest, ast_array_index))
1444 ai = (ast_array_index*)self->dest;
1445 idx = (ast_value*)ai->index;
1447 if (ast_istype(ai->index, ast_value) && idx->isconst)
1452 /* we need to call the setter */
1453 ir_value *iridx, *funval;
1457 asterror(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1461 arr = (ast_value*)ai->array;
1462 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1463 asterror(ast_ctx(self), "value has no setter (%s)", arr->name);
1467 cgen = idx->expression.codegen;
1468 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1471 cgen = arr->setter->expression.codegen;
1472 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
1475 cgen = self->source->expression.codegen;
1476 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1479 call = ir_block_create_call(func->curblock, ast_function_label(func, "store"), funval);
1482 ir_call_param(call, iridx);
1483 ir_call_param(call, right);
1484 self->expression.outr = right;
1490 cgen = self->dest->expression.codegen;
1492 if (!(*cgen)((ast_expression*)(self->dest), func, true, &left))
1494 self->expression.outl = left;
1496 cgen = self->source->expression.codegen;
1498 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1501 if (!ir_block_create_store_op(func->curblock, self->op, left, right))
1503 self->expression.outr = right;
1506 /* Theoretically, an assinment returns its left side as an
1507 * lvalue, if we don't need an lvalue though, we return
1508 * the right side as an rvalue, otherwise we have to
1509 * somehow know whether or not we need to dereference the pointer
1510 * on the left side - that is: OP_LOAD if it was an address.
1511 * Also: in original QC we cannot OP_LOADP *anyway*.
1513 *out = (lvalue ? left : right);
1518 bool ast_binary_codegen(ast_binary *self, ast_function *func, bool lvalue, ir_value **out)
1520 ast_expression_codegen *cgen;
1521 ir_value *left, *right;
1523 /* A binary operation cannot yield an l-value */
1525 asterror(ast_ctx(self), "not an l-value (binop)");
1529 if (self->expression.outr) {
1530 *out = self->expression.outr;
1534 if (OPTS_FLAG(SHORT_LOGIC) &&
1535 (self->op == INSTR_AND || self->op == INSTR_OR))
1537 /* short circuit evaluation */
1538 ir_block *other, *merge;
1539 ir_block *from_left, *from_right;
1544 /* Note about casting to true boolean values:
1545 * We use a single NOT for sub expressions, and an
1546 * overall NOT at the end, and for that purpose swap
1547 * all the jump conditions in order for the NOT to get
1549 * ie: (a && b) usually becomes (!!a ? !!b : !!a)
1550 * but we translate this to (!(!a ? !a : !b))
1553 merge_id = vec_size(func->ir_func->blocks);
1554 merge = ir_function_create_block(func->ir_func, ast_function_label(func, "sce_merge"));
1556 cgen = self->left->expression.codegen;
1557 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1559 if (!OPTS_FLAG(PERL_LOGIC)) {
1560 notop = type_not_instr[left->vtype];
1561 if (notop == AINSTR_END) {
1562 asterror(ast_ctx(self), "don't know how to cast to bool...");
1565 left = ir_block_create_unary(func->curblock,
1566 ast_function_label(func, "sce_not"),
1570 from_left = func->curblock;
1572 other = ir_function_create_block(func->ir_func, ast_function_label(func, "sce_other"));
1573 if ( !(self->op == INSTR_OR) != !OPTS_FLAG(PERL_LOGIC) ) {
1574 if (!ir_block_create_if(func->curblock, left, other, merge))
1577 if (!ir_block_create_if(func->curblock, left, merge, other))
1580 /* use the likely flag */
1581 vec_last(func->curblock->instr)->likely = true;
1583 func->curblock = other;
1584 cgen = self->right->expression.codegen;
1585 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1587 if (!OPTS_FLAG(PERL_LOGIC)) {
1588 notop = type_not_instr[right->vtype];
1589 if (notop == AINSTR_END) {
1590 asterror(ast_ctx(self), "don't know how to cast to bool...");
1593 right = ir_block_create_unary(func->curblock,
1594 ast_function_label(func, "sce_not"),
1598 from_right = func->curblock;
1600 if (!ir_block_create_jump(func->curblock, merge))
1603 vec_remove(func->ir_func->blocks, merge_id, 1);
1604 vec_push(func->ir_func->blocks, merge);
1606 func->curblock = merge;
1607 phi = ir_block_create_phi(func->curblock, ast_function_label(func, "sce_value"), TYPE_FLOAT);
1608 ir_phi_add(phi, from_left, left);
1609 ir_phi_add(phi, from_right, right);
1610 *out = ir_phi_value(phi);
1611 if (!OPTS_FLAG(PERL_LOGIC)) {
1612 notop = type_not_instr[(*out)->vtype];
1613 if (notop == AINSTR_END) {
1614 asterror(ast_ctx(self), "don't know how to cast to bool...");
1617 *out = ir_block_create_unary(func->curblock,
1618 ast_function_label(func, "sce_final_not"),
1624 self->expression.outr = *out;
1628 cgen = self->left->expression.codegen;
1629 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1632 cgen = self->right->expression.codegen;
1633 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1636 *out = ir_block_create_binop(func->curblock, ast_function_label(func, "bin"),
1637 self->op, left, right);
1640 self->expression.outr = *out;
1645 bool ast_binstore_codegen(ast_binstore *self, ast_function *func, bool lvalue, ir_value **out)
1647 ast_expression_codegen *cgen;
1648 ir_value *leftl = NULL, *leftr, *right, *bin;
1652 ast_array_index *ai = NULL;
1653 ir_value *iridx = NULL;
1655 if (lvalue && self->expression.outl) {
1656 *out = self->expression.outl;
1660 if (!lvalue && self->expression.outr) {
1661 *out = self->expression.outr;
1665 if (ast_istype(self->dest, ast_array_index))
1668 ai = (ast_array_index*)self->dest;
1669 idx = (ast_value*)ai->index;
1671 if (ast_istype(ai->index, ast_value) && idx->isconst)
1675 /* for a binstore we need both an lvalue and an rvalue for the left side */
1676 /* rvalue of destination! */
1678 cgen = idx->expression.codegen;
1679 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1682 cgen = self->dest->expression.codegen;
1683 if (!(*cgen)((ast_expression*)(self->dest), func, false, &leftr))
1686 /* source as rvalue only */
1687 cgen = self->source->expression.codegen;
1688 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1691 /* now the binary */
1692 bin = ir_block_create_binop(func->curblock, ast_function_label(func, "binst"),
1693 self->opbin, leftr, right);
1694 self->expression.outr = bin;
1698 /* we need to call the setter */
1703 asterror(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1707 arr = (ast_value*)ai->array;
1708 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1709 asterror(ast_ctx(self), "value has no setter (%s)", arr->name);
1713 cgen = arr->setter->expression.codegen;
1714 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
1717 call = ir_block_create_call(func->curblock, ast_function_label(func, "store"), funval);
1720 ir_call_param(call, iridx);
1721 ir_call_param(call, bin);
1722 self->expression.outr = bin;
1724 /* now store them */
1725 cgen = self->dest->expression.codegen;
1726 /* lvalue of destination */
1727 if (!(*cgen)((ast_expression*)(self->dest), func, true, &leftl))
1729 self->expression.outl = leftl;
1731 if (!ir_block_create_store_op(func->curblock, self->opstore, leftl, bin))
1733 self->expression.outr = bin;
1736 /* Theoretically, an assinment returns its left side as an
1737 * lvalue, if we don't need an lvalue though, we return
1738 * the right side as an rvalue, otherwise we have to
1739 * somehow know whether or not we need to dereference the pointer
1740 * on the left side - that is: OP_LOAD if it was an address.
1741 * Also: in original QC we cannot OP_LOADP *anyway*.
1743 *out = (lvalue ? leftl : bin);
1748 bool ast_unary_codegen(ast_unary *self, ast_function *func, bool lvalue, ir_value **out)
1750 ast_expression_codegen *cgen;
1753 /* An unary operation cannot yield an l-value */
1755 asterror(ast_ctx(self), "not an l-value (binop)");
1759 if (self->expression.outr) {
1760 *out = self->expression.outr;
1764 cgen = self->operand->expression.codegen;
1766 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
1769 *out = ir_block_create_unary(func->curblock, ast_function_label(func, "unary"),
1773 self->expression.outr = *out;
1778 bool ast_return_codegen(ast_return *self, ast_function *func, bool lvalue, ir_value **out)
1780 ast_expression_codegen *cgen;
1785 /* In the context of a return operation, we don't actually return
1789 asterror(ast_ctx(self), "return-expression is not an l-value");
1793 if (self->expression.outr) {
1794 asterror(ast_ctx(self), "internal error: ast_return cannot be reused, it bears no result!");
1797 self->expression.outr = (ir_value*)1;
1799 if (self->operand) {
1800 cgen = self->operand->expression.codegen;
1802 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
1805 if (!ir_block_create_return(func->curblock, operand))
1808 if (!ir_block_create_return(func->curblock, NULL))
1815 bool ast_entfield_codegen(ast_entfield *self, ast_function *func, bool lvalue, ir_value **out)
1817 ast_expression_codegen *cgen;
1818 ir_value *ent, *field;
1820 /* This function needs to take the 'lvalue' flag into account!
1821 * As lvalue we provide a field-pointer, as rvalue we provide the
1825 if (lvalue && self->expression.outl) {
1826 *out = self->expression.outl;
1830 if (!lvalue && self->expression.outr) {
1831 *out = self->expression.outr;
1835 cgen = self->entity->expression.codegen;
1836 if (!(*cgen)((ast_expression*)(self->entity), func, false, &ent))
1839 cgen = self->field->expression.codegen;
1840 if (!(*cgen)((ast_expression*)(self->field), func, false, &field))
1845 *out = ir_block_create_fieldaddress(func->curblock, ast_function_label(func, "efa"),
1848 *out = ir_block_create_load_from_ent(func->curblock, ast_function_label(func, "efv"),
1849 ent, field, self->expression.vtype);
1852 asterror(ast_ctx(self), "failed to create %s instruction (output type %s)",
1853 (lvalue ? "ADDRESS" : "FIELD"),
1854 type_name[self->expression.vtype]);
1859 self->expression.outl = *out;
1861 self->expression.outr = *out;
1863 /* Hm that should be it... */
1867 bool ast_member_codegen(ast_member *self, ast_function *func, bool lvalue, ir_value **out)
1869 ast_expression_codegen *cgen;
1872 /* in QC this is always an lvalue */
1874 if (self->expression.outl) {
1875 *out = self->expression.outl;
1879 cgen = self->owner->expression.codegen;
1880 if (!(*cgen)((ast_expression*)(self->owner), func, true, &vec))
1883 if (vec->vtype != TYPE_VECTOR &&
1884 !(vec->vtype == TYPE_FIELD && self->owner->expression.next->expression.vtype == TYPE_VECTOR))
1889 *out = ir_value_vector_member(vec, self->field);
1890 self->expression.outl = *out;
1892 return (*out != NULL);
1895 bool ast_array_index_codegen(ast_array_index *self, ast_function *func, bool lvalue, ir_value **out)
1900 if (!lvalue && self->expression.outr) {
1901 *out = self->expression.outr;
1903 if (lvalue && self->expression.outl) {
1904 *out = self->expression.outl;
1907 if (!ast_istype(self->array, ast_value)) {
1908 asterror(ast_ctx(self), "array indexing this way is not supported");
1909 /* note this would actually be pointer indexing because the left side is
1910 * not an actual array but (hopefully) an indexable expression.
1911 * Once we get integer arithmetic, and GADDRESS/GSTORE/GLOAD instruction
1912 * support this path will be filled.
1917 arr = (ast_value*)self->array;
1918 idx = (ast_value*)self->index;
1920 if (!ast_istype(self->index, ast_value) || !idx->isconst) {
1921 /* Time to use accessor functions */
1922 ast_expression_codegen *cgen;
1923 ir_value *iridx, *funval;
1927 asterror(ast_ctx(self), "(.2) array indexing here needs a compile-time constant");
1932 asterror(ast_ctx(self), "value has no getter, don't know how to index it");
1936 cgen = self->index->expression.codegen;
1937 if (!(*cgen)((ast_expression*)(self->index), func, true, &iridx))
1940 cgen = arr->getter->expression.codegen;
1941 if (!(*cgen)((ast_expression*)(arr->getter), func, true, &funval))
1944 call = ir_block_create_call(func->curblock, ast_function_label(func, "fetch"), funval);
1947 ir_call_param(call, iridx);
1949 *out = ir_call_value(call);
1950 self->expression.outr = *out;
1954 if (idx->expression.vtype == TYPE_FLOAT)
1955 *out = arr->ir_values[(int)idx->constval.vfloat];
1956 else if (idx->expression.vtype == TYPE_INTEGER)
1957 *out = arr->ir_values[idx->constval.vint];
1959 asterror(ast_ctx(self), "array indexing here needs an integer constant");
1965 bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_value **out)
1967 ast_expression_codegen *cgen;
1972 ir_block *cond = func->curblock;
1975 ir_block *ontrue_endblock = NULL;
1976 ir_block *onfalse_endblock = NULL;
1979 /* We don't output any value, thus also don't care about r/lvalue */
1983 if (self->expression.outr) {
1984 asterror(ast_ctx(self), "internal error: ast_ifthen cannot be reused, it bears no result!");
1987 self->expression.outr = (ir_value*)1;
1989 /* generate the condition */
1990 cgen = self->cond->expression.codegen;
1991 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
1993 /* update the block which will get the jump - because short-logic or ternaries may have changed this */
1994 cond = func->curblock;
1998 if (self->on_true) {
1999 /* create on-true block */
2000 ontrue = ir_function_create_block(func->ir_func, ast_function_label(func, "ontrue"));
2004 /* enter the block */
2005 func->curblock = ontrue;
2008 cgen = self->on_true->expression.codegen;
2009 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &dummy))
2012 /* we now need to work from the current endpoint */
2013 ontrue_endblock = func->curblock;
2018 if (self->on_false) {
2019 /* create on-false block */
2020 onfalse = ir_function_create_block(func->ir_func, ast_function_label(func, "onfalse"));
2024 /* enter the block */
2025 func->curblock = onfalse;
2028 cgen = self->on_false->expression.codegen;
2029 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &dummy))
2032 /* we now need to work from the current endpoint */
2033 onfalse_endblock = func->curblock;
2037 /* Merge block were they all merge in to */
2038 merge = ir_function_create_block(func->ir_func, ast_function_label(func, "endif"));
2041 /* add jumps ot the merge block */
2042 if (ontrue && !ontrue_endblock->final && !ir_block_create_jump(ontrue_endblock, merge))
2044 if (onfalse && !onfalse_endblock->final && !ir_block_create_jump(onfalse_endblock, merge))
2047 /* we create the if here, that way all blocks are ordered :)
2049 if (!ir_block_create_if(cond, condval,
2050 (ontrue ? ontrue : merge),
2051 (onfalse ? onfalse : merge)))
2056 /* Now enter the merge block */
2057 func->curblock = merge;
2062 bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_value **out)
2064 ast_expression_codegen *cgen;
2067 ir_value *trueval, *falseval;
2070 ir_block *cond = func->curblock;
2075 /* Ternary can never create an lvalue... */
2079 /* In theory it shouldn't be possible to pass through a node twice, but
2080 * in case we add any kind of optimization pass for the AST itself, it
2081 * may still happen, thus we remember a created ir_value and simply return one
2082 * if it already exists.
2084 if (self->expression.outr) {
2085 *out = self->expression.outr;
2089 /* In the following, contraty to ast_ifthen, we assume both paths exist. */
2091 /* generate the condition */
2092 func->curblock = cond;
2093 cgen = self->cond->expression.codegen;
2094 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2097 /* create on-true block */
2098 ontrue = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_T"));
2103 /* enter the block */
2104 func->curblock = ontrue;
2107 cgen = self->on_true->expression.codegen;
2108 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &trueval))
2112 /* create on-false block */
2113 onfalse = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_F"));
2118 /* enter the block */
2119 func->curblock = onfalse;
2122 cgen = self->on_false->expression.codegen;
2123 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &falseval))
2127 /* create merge block */
2128 merge = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_out"));
2131 /* jump to merge block */
2132 if (!ir_block_create_jump(ontrue, merge))
2134 if (!ir_block_create_jump(onfalse, merge))
2137 /* create if instruction */
2138 if (!ir_block_create_if(cond, condval, ontrue, onfalse))
2141 /* Now enter the merge block */
2142 func->curblock = merge;
2144 /* Here, now, we need a PHI node
2145 * but first some sanity checking...
2147 if (trueval->vtype != falseval->vtype) {
2148 /* error("ternary with different types on the two sides"); */
2153 phi = ir_block_create_phi(merge, ast_function_label(func, "phi"), trueval->vtype);
2156 ir_phi_add(phi, ontrue, trueval);
2157 ir_phi_add(phi, onfalse, falseval);
2159 self->expression.outr = ir_phi_value(phi);
2160 *out = self->expression.outr;
2165 bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value **out)
2167 ast_expression_codegen *cgen;
2169 ir_value *dummy = NULL;
2170 ir_value *precond = NULL;
2171 ir_value *postcond = NULL;
2173 /* Since we insert some jumps "late" so we have blocks
2174 * ordered "nicely", we need to keep track of the actual end-blocks
2175 * of expressions to add the jumps to.
2177 ir_block *bbody = NULL, *end_bbody = NULL;
2178 ir_block *bprecond = NULL, *end_bprecond = NULL;
2179 ir_block *bpostcond = NULL, *end_bpostcond = NULL;
2180 ir_block *bincrement = NULL, *end_bincrement = NULL;
2181 ir_block *bout = NULL, *bin = NULL;
2183 /* let's at least move the outgoing block to the end */
2186 /* 'break' and 'continue' need to be able to find the right blocks */
2187 ir_block *bcontinue = NULL;
2188 ir_block *bbreak = NULL;
2190 ir_block *old_bcontinue = NULL;
2191 ir_block *old_bbreak = NULL;
2193 ir_block *tmpblock = NULL;
2198 if (self->expression.outr) {
2199 asterror(ast_ctx(self), "internal error: ast_loop cannot be reused, it bears no result!");
2202 self->expression.outr = (ir_value*)1;
2205 * Should we ever need some kind of block ordering, better make this function
2206 * move blocks around than write a block ordering algorithm later... after all
2207 * the ast and ir should work together, not against each other.
2210 /* initexpr doesn't get its own block, it's pointless, it could create more blocks
2211 * anyway if for example it contains a ternary.
2215 cgen = self->initexpr->expression.codegen;
2216 if (!(*cgen)((ast_expression*)(self->initexpr), func, false, &dummy))
2220 /* Store the block from which we enter this chaos */
2221 bin = func->curblock;
2223 /* The pre-loop condition needs its own block since we
2224 * need to be able to jump to the start of that expression.
2228 bprecond = ir_function_create_block(func->ir_func, ast_function_label(func, "pre_loop_cond"));
2232 /* the pre-loop-condition the least important place to 'continue' at */
2233 bcontinue = bprecond;
2236 func->curblock = bprecond;
2239 cgen = self->precond->expression.codegen;
2240 if (!(*cgen)((ast_expression*)(self->precond), func, false, &precond))
2243 end_bprecond = func->curblock;
2245 bprecond = end_bprecond = NULL;
2248 /* Now the next blocks won't be ordered nicely, but we need to
2249 * generate them this early for 'break' and 'continue'.
2251 if (self->increment) {
2252 bincrement = ir_function_create_block(func->ir_func, ast_function_label(func, "loop_increment"));
2255 bcontinue = bincrement; /* increment comes before the pre-loop-condition */
2257 bincrement = end_bincrement = NULL;
2260 if (self->postcond) {
2261 bpostcond = ir_function_create_block(func->ir_func, ast_function_label(func, "post_loop_cond"));
2264 bcontinue = bpostcond; /* postcond comes before the increment */
2266 bpostcond = end_bpostcond = NULL;
2269 bout_id = vec_size(func->ir_func->blocks);
2270 bout = ir_function_create_block(func->ir_func, ast_function_label(func, "after_loop"));
2275 /* The loop body... */
2278 bbody = ir_function_create_block(func->ir_func, ast_function_label(func, "loop_body"));
2283 func->curblock = bbody;
2285 old_bbreak = func->breakblock;
2286 old_bcontinue = func->continueblock;
2287 func->breakblock = bbreak;
2288 func->continueblock = bcontinue;
2291 cgen = self->body->expression.codegen;
2292 if (!(*cgen)((ast_expression*)(self->body), func, false, &dummy))
2295 end_bbody = func->curblock;
2296 func->breakblock = old_bbreak;
2297 func->continueblock = old_bcontinue;
2300 /* post-loop-condition */
2304 func->curblock = bpostcond;
2307 cgen = self->postcond->expression.codegen;
2308 if (!(*cgen)((ast_expression*)(self->postcond), func, false, &postcond))
2311 end_bpostcond = func->curblock;
2314 /* The incrementor */
2315 if (self->increment)
2318 func->curblock = bincrement;
2321 cgen = self->increment->expression.codegen;
2322 if (!(*cgen)((ast_expression*)(self->increment), func, false, &dummy))
2325 end_bincrement = func->curblock;
2328 /* In any case now, we continue from the outgoing block */
2329 func->curblock = bout;
2331 /* Now all blocks are in place */
2332 /* From 'bin' we jump to whatever comes first */
2333 if (bprecond) tmpblock = bprecond;
2334 else if (bbody) tmpblock = bbody;
2335 else if (bpostcond) tmpblock = bpostcond;
2336 else tmpblock = bout;
2337 if (!ir_block_create_jump(bin, tmpblock))
2343 ir_block *ontrue, *onfalse;
2344 if (bbody) ontrue = bbody;
2345 else if (bincrement) ontrue = bincrement;
2346 else if (bpostcond) ontrue = bpostcond;
2347 else ontrue = bprecond;
2349 if (!ir_block_create_if(end_bprecond, precond, ontrue, onfalse))
2356 if (bincrement) tmpblock = bincrement;
2357 else if (bpostcond) tmpblock = bpostcond;
2358 else if (bprecond) tmpblock = bprecond;
2359 else tmpblock = bout;
2360 if (!end_bbody->final && !ir_block_create_jump(end_bbody, tmpblock))
2364 /* from increment */
2367 if (bpostcond) tmpblock = bpostcond;
2368 else if (bprecond) tmpblock = bprecond;
2369 else if (bbody) tmpblock = bbody;
2370 else tmpblock = bout;
2371 if (!ir_block_create_jump(end_bincrement, tmpblock))
2378 ir_block *ontrue, *onfalse;
2379 if (bprecond) ontrue = bprecond;
2380 else if (bbody) ontrue = bbody;
2381 else if (bincrement) ontrue = bincrement;
2382 else ontrue = bpostcond;
2384 if (!ir_block_create_if(end_bpostcond, postcond, ontrue, onfalse))
2388 /* Move 'bout' to the end */
2389 vec_remove(func->ir_func->blocks, bout_id, 1);
2390 vec_push(func->ir_func->blocks, bout);
2395 bool ast_breakcont_codegen(ast_breakcont *self, ast_function *func, bool lvalue, ir_value **out)
2402 asterror(ast_ctx(self), "break/continue expression is not an l-value");
2406 if (self->expression.outr) {
2407 asterror(ast_ctx(self), "internal error: ast_breakcont cannot be reused!");
2410 self->expression.outr = (ir_value*)1;
2412 if (self->is_continue)
2413 target = func->continueblock;
2415 target = func->breakblock;
2417 if (!ir_block_create_jump(func->curblock, target))
2422 bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_value **out)
2424 ast_expression_codegen *cgen;
2426 ast_switch_case *def_case = NULL;
2427 ir_block *def_bfall = NULL;
2429 ir_value *dummy = NULL;
2430 ir_value *irop = NULL;
2431 ir_block *old_break = NULL;
2432 ir_block *bout = NULL;
2433 ir_block *bfall = NULL;
2441 asterror(ast_ctx(self), "switch expression is not an l-value");
2445 if (self->expression.outr) {
2446 asterror(ast_ctx(self), "internal error: ast_switch cannot be reused!");
2449 self->expression.outr = (ir_value*)1;
2454 cgen = self->operand->expression.codegen;
2455 if (!(*cgen)((ast_expression*)(self->operand), func, false, &irop))
2458 if (!vec_size(self->cases))
2461 cmpinstr = type_eq_instr[irop->vtype];
2462 if (cmpinstr >= AINSTR_END) {
2463 ast_type_to_string(self->operand, typestr, sizeof(typestr));
2464 asterror(ast_ctx(self), "invalid type to perform a switch on: %s", typestr);
2468 bout_id = vec_size(func->ir_func->blocks);
2469 bout = ir_function_create_block(func->ir_func, ast_function_label(func, "after_switch"));
2473 /* setup the break block */
2474 old_break = func->breakblock;
2475 func->breakblock = bout;
2477 /* Now create all cases */
2478 for (c = 0; c < vec_size(self->cases); ++c) {
2479 ir_value *cond, *val;
2480 ir_block *bcase, *bnot;
2483 ast_switch_case *swcase = &self->cases[c];
2485 if (swcase->value) {
2486 /* A regular case */
2487 /* generate the condition operand */
2488 cgen = swcase->value->expression.codegen;
2489 if (!(*cgen)((ast_expression*)(swcase->value), func, false, &val))
2491 /* generate the condition */
2492 cond = ir_block_create_binop(func->curblock, ast_function_label(func, "switch_eq"), cmpinstr, irop, val);
2496 bcase = ir_function_create_block(func->ir_func, ast_function_label(func, "case"));
2497 bnot_id = vec_size(func->ir_func->blocks);
2498 bnot = ir_function_create_block(func->ir_func, ast_function_label(func, "not_case"));
2499 if (!bcase || !bnot)
2501 if (!ir_block_create_if(func->curblock, cond, bcase, bnot))
2504 /* Make the previous case-end fall through */
2505 if (bfall && !bfall->final) {
2506 if (!ir_block_create_jump(bfall, bcase))
2510 /* enter the case */
2511 func->curblock = bcase;
2512 cgen = swcase->code->expression.codegen;
2513 if (!(*cgen)((ast_expression*)swcase->code, func, false, &dummy))
2516 /* remember this block to fall through from */
2517 bfall = func->curblock;
2519 /* enter the else and move it down */
2520 func->curblock = bnot;
2521 vec_remove(func->ir_func->blocks, bnot_id, 1);
2522 vec_push(func->ir_func->blocks, bnot);
2524 /* The default case */
2525 /* Remember where to fall through from: */
2528 /* remember which case it was */
2533 /* Jump from the last bnot to bout */
2534 if (bfall && !bfall->final && !ir_block_create_jump(bfall, bout)) {
2536 astwarning(ast_ctx(bfall), WARN_???, "missing break after last case");
2541 /* If there was a default case, put it down here */
2545 /* No need to create an extra block */
2546 bcase = func->curblock;
2548 /* Insert the fallthrough jump */
2549 if (def_bfall && !def_bfall->final) {
2550 if (!ir_block_create_jump(def_bfall, bcase))
2554 /* Now generate the default code */
2555 cgen = def_case->code->expression.codegen;
2556 if (!(*cgen)((ast_expression*)def_case->code, func, false, &dummy))
2560 /* Jump from the last bnot to bout */
2561 if (!func->curblock->final && !ir_block_create_jump(func->curblock, bout))
2563 /* enter the outgoing block */
2564 func->curblock = bout;
2566 /* restore the break block */
2567 func->breakblock = old_break;
2569 /* Move 'bout' to the end, it's nicer */
2570 vec_remove(func->ir_func->blocks, bout_id, 1);
2571 vec_push(func->ir_func->blocks, bout);
2576 bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value **out)
2578 ast_expression_codegen *cgen;
2580 ir_instr *callinstr;
2583 ir_value *funval = NULL;
2585 /* return values are never lvalues */
2587 asterror(ast_ctx(self), "not an l-value (function call)");
2591 if (self->expression.outr) {
2592 *out = self->expression.outr;
2596 cgen = self->func->expression.codegen;
2597 if (!(*cgen)((ast_expression*)(self->func), func, false, &funval))
2605 for (i = 0; i < vec_size(self->params); ++i)
2608 ast_expression *expr = self->params[i];
2610 cgen = expr->expression.codegen;
2611 if (!(*cgen)(expr, func, false, ¶m))
2615 vec_push(params, param);
2618 callinstr = ir_block_create_call(func->curblock, ast_function_label(func, "call"), funval);
2622 for (i = 0; i < vec_size(params); ++i) {
2623 ir_call_param(callinstr, params[i]);
2626 *out = ir_call_value(callinstr);
2627 self->expression.outr = *out;