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, const char *name)
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);
555 self->name = util_strdup(name);
562 void ast_member_delete(ast_member *self)
564 /* The owner is always an ast_value, which has .keep=true,
565 * also: ast_members are usually deleted after the owner, thus
566 * this will cause invalid access
567 ast_unref(self->owner);
568 * once we allow (expression).x to access a vector-member, we need
569 * to change this: preferably by creating an alternate ast node for this
570 * purpose that is not garbage-collected.
572 ast_expression_delete((ast_expression*)self);
576 ast_array_index* ast_array_index_new(lex_ctx ctx, ast_expression *array, ast_expression *index)
578 ast_expression *outtype;
579 ast_instantiate(ast_array_index, ctx, ast_array_index_delete);
581 outtype = array->expression.next;
584 /* Error: field has no type... */
588 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_array_index_codegen);
593 if (!ast_type_adopt(self, outtype)) {
594 ast_array_index_delete(self);
597 if (array->expression.vtype == TYPE_FIELD && outtype->expression.vtype == TYPE_ARRAY) {
598 if (self->expression.vtype != TYPE_ARRAY) {
599 asterror(ast_ctx(self), "array_index node on type");
600 ast_array_index_delete(self);
603 self->array = outtype;
604 self->expression.vtype = TYPE_FIELD;
610 void ast_array_index_delete(ast_array_index *self)
612 ast_unref(self->array);
613 ast_unref(self->index);
614 ast_expression_delete((ast_expression*)self);
618 ast_ifthen* ast_ifthen_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
620 ast_instantiate(ast_ifthen, ctx, ast_ifthen_delete);
621 if (!ontrue && !onfalse) {
622 /* because it is invalid */
626 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ifthen_codegen);
629 self->on_true = ontrue;
630 self->on_false = onfalse;
635 void ast_ifthen_delete(ast_ifthen *self)
637 ast_unref(self->cond);
639 ast_unref(self->on_true);
641 ast_unref(self->on_false);
642 ast_expression_delete((ast_expression*)self);
646 ast_ternary* ast_ternary_new(lex_ctx ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
648 ast_instantiate(ast_ternary, ctx, ast_ternary_delete);
649 /* This time NEITHER must be NULL */
650 if (!ontrue || !onfalse) {
654 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ternary_codegen);
657 self->on_true = ontrue;
658 self->on_false = onfalse;
660 if (!ast_type_adopt(self, ontrue)) {
661 ast_ternary_delete(self);
668 void ast_ternary_delete(ast_ternary *self)
670 ast_unref(self->cond);
671 ast_unref(self->on_true);
672 ast_unref(self->on_false);
673 ast_expression_delete((ast_expression*)self);
677 ast_loop* ast_loop_new(lex_ctx ctx,
678 ast_expression *initexpr,
679 ast_expression *precond,
680 ast_expression *postcond,
681 ast_expression *increment,
682 ast_expression *body)
684 ast_instantiate(ast_loop, ctx, ast_loop_delete);
685 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_loop_codegen);
687 self->initexpr = initexpr;
688 self->precond = precond;
689 self->postcond = postcond;
690 self->increment = increment;
696 void ast_loop_delete(ast_loop *self)
699 ast_unref(self->initexpr);
701 ast_unref(self->precond);
703 ast_unref(self->postcond);
705 ast_unref(self->increment);
707 ast_unref(self->body);
708 ast_expression_delete((ast_expression*)self);
712 ast_breakcont* ast_breakcont_new(lex_ctx ctx, bool iscont)
714 ast_instantiate(ast_breakcont, ctx, ast_breakcont_delete);
715 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_breakcont_codegen);
717 self->is_continue = iscont;
722 void ast_breakcont_delete(ast_breakcont *self)
724 ast_expression_delete((ast_expression*)self);
728 ast_switch* ast_switch_new(lex_ctx ctx, ast_expression *op)
730 ast_instantiate(ast_switch, ctx, ast_switch_delete);
731 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_switch_codegen);
739 void ast_switch_delete(ast_switch *self)
742 ast_unref(self->operand);
744 for (i = 0; i < vec_size(self->cases); ++i) {
745 if (self->cases[i].value)
746 ast_unref(self->cases[i].value);
747 ast_unref(self->cases[i].code);
749 vec_free(self->cases);
751 ast_expression_delete((ast_expression*)self);
755 ast_call* ast_call_new(lex_ctx ctx,
756 ast_expression *funcexpr)
758 ast_instantiate(ast_call, ctx, ast_call_delete);
759 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_call_codegen);
762 self->func = funcexpr;
764 self->expression.vtype = funcexpr->expression.next->expression.vtype;
765 if (funcexpr->expression.next->expression.next)
766 self->expression.next = ast_type_copy(ctx, funcexpr->expression.next->expression.next);
771 void ast_call_delete(ast_call *self)
774 for (i = 0; i < vec_size(self->params); ++i)
775 ast_unref(self->params[i]);
776 vec_free(self->params);
779 ast_unref(self->func);
781 ast_expression_delete((ast_expression*)self);
785 bool ast_call_check_types(ast_call *self)
789 const ast_expression *func = self->func;
790 size_t count = vec_size(self->params);
791 if (count > vec_size(func->expression.params))
792 count = vec_size(func->expression.params);
794 for (i = 0; i < count; ++i) {
795 if (!ast_compare_type(self->params[i], (ast_expression*)(func->expression.params[i]))) {
798 ast_type_to_string(self->params[i], tgot, sizeof(tgot));
799 ast_type_to_string((ast_expression*)func->expression.params[i], texp, sizeof(texp));
800 asterror(ast_ctx(self), "invalid type for parameter %u in function call: expected %s, got %s",
801 (unsigned int)(i+1), texp, tgot);
802 /* we don't immediately return */
809 ast_store* ast_store_new(lex_ctx ctx, int op,
810 ast_expression *dest, ast_expression *source)
812 ast_instantiate(ast_store, ctx, ast_store_delete);
813 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_store_codegen);
817 self->source = source;
819 self->expression.vtype = dest->expression.vtype;
820 if (dest->expression.next) {
821 self->expression.next = ast_type_copy(ctx, dest);
822 if (!self->expression.next) {
828 self->expression.next = NULL;
833 void ast_store_delete(ast_store *self)
835 ast_unref(self->dest);
836 ast_unref(self->source);
837 ast_expression_delete((ast_expression*)self);
841 ast_block* ast_block_new(lex_ctx ctx)
843 ast_instantiate(ast_block, ctx, ast_block_delete);
844 ast_expression_init((ast_expression*)self,
845 (ast_expression_codegen*)&ast_block_codegen);
849 self->collect = NULL;
854 void ast_block_collect(ast_block *self, ast_expression *expr)
856 vec_push(self->collect, expr);
857 expr->expression.node.keep = true;
860 void ast_block_delete(ast_block *self)
863 for (i = 0; i < vec_size(self->exprs); ++i)
864 ast_unref(self->exprs[i]);
865 vec_free(self->exprs);
866 for (i = 0; i < vec_size(self->locals); ++i)
867 ast_delete(self->locals[i]);
868 vec_free(self->locals);
869 for (i = 0; i < vec_size(self->collect); ++i)
870 ast_delete(self->collect[i]);
871 vec_free(self->collect);
872 ast_expression_delete((ast_expression*)self);
876 bool ast_block_set_type(ast_block *self, ast_expression *from)
878 if (self->expression.next)
879 ast_delete(self->expression.next);
880 self->expression.vtype = from->expression.vtype;
881 if (from->expression.next) {
882 self->expression.next = ast_type_copy(self->expression.node.context, from->expression.next);
883 if (!self->expression.next)
887 self->expression.next = NULL;
891 ast_function* ast_function_new(lex_ctx ctx, const char *name, ast_value *vtype)
893 ast_instantiate(ast_function, ctx, ast_function_delete);
897 vtype->expression.vtype != TYPE_FUNCTION)
899 asterror(ast_ctx(self), "internal error: ast_function_new condition %i %i type=%i",
902 vtype->expression.vtype);
908 self->name = name ? util_strdup(name) : NULL;
911 self->labelcount = 0;
914 self->ir_func = NULL;
915 self->curblock = NULL;
917 self->breakblock = NULL;
918 self->continueblock = NULL;
920 vtype->isconst = true;
921 vtype->constval.vfunc = self;
926 void ast_function_delete(ast_function *self)
930 mem_d((void*)self->name);
932 /* ast_value_delete(self->vtype); */
933 self->vtype->isconst = false;
934 self->vtype->constval.vfunc = NULL;
935 /* We use unref - if it was stored in a global table it is supposed
936 * to be deleted from *there*
938 ast_unref(self->vtype);
940 for (i = 0; i < vec_size(self->blocks); ++i)
941 ast_delete(self->blocks[i]);
942 vec_free(self->blocks);
946 const char* ast_function_label(ast_function *self, const char *prefix)
952 if (!opts_dump && !opts_dumpfin)
955 id = (self->labelcount++);
956 len = strlen(prefix);
958 from = self->labelbuf + sizeof(self->labelbuf)-1;
961 unsigned int digit = id % 10;
965 memcpy(from - len, prefix, len);
969 /*********************************************************************/
971 * by convention you must never pass NULL to the 'ir_value **out'
972 * parameter. If you really don't care about the output, pass a dummy.
973 * But I can't imagine a pituation where the output is truly unnecessary.
976 bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out)
980 /* NOTE: This is the codegen for a variable used in an expression.
981 * It is not the codegen to generate the value. For this purpose,
982 * ast_local_codegen and ast_global_codegen are to be used before this
983 * is executed. ast_function_codegen should take care of its locals,
984 * and the ast-user should take care of ast_global_codegen to be used
985 * on all the globals.
989 ast_type_to_string((ast_expression*)self, typename, sizeof(typename));
990 asterror(ast_ctx(self), "ast_value used before generated %s %s", typename, self->name);
997 bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
1001 if (self->isconst && self->expression.vtype == TYPE_FUNCTION)
1003 ir_function *func = ir_builder_create_function(ir, self->name, self->expression.next->expression.vtype);
1006 func->context = ast_ctx(self);
1007 func->value->context = ast_ctx(self);
1009 self->constval.vfunc->ir_func = func;
1010 self->ir_v = func->value;
1011 /* The function is filled later on ast_function_codegen... */
1015 if (isfield && self->expression.vtype == TYPE_FIELD) {
1016 ast_expression *fieldtype = self->expression.next;
1018 if (self->isconst) {
1019 asterror(ast_ctx(self), "TODO: constant field pointers with value");
1023 if (fieldtype->expression.vtype == TYPE_ARRAY) {
1028 ast_expression_common *elemtype;
1030 ast_value *array = (ast_value*)fieldtype;
1032 if (!ast_istype(fieldtype, ast_value)) {
1033 asterror(ast_ctx(self), "internal error: ast_value required");
1037 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1038 if (!array->expression.count || array->expression.count > opts_max_array_size)
1039 asterror(ast_ctx(self), "Invalid array of size %lu", (unsigned long)array->expression.count);
1041 elemtype = &array->expression.next->expression;
1042 vtype = elemtype->vtype;
1044 v = ir_builder_create_field(ir, self->name, vtype);
1046 asterror(ast_ctx(self), "ir_builder_create_global failed");
1049 if (vtype == TYPE_FIELD)
1050 v->fieldtype = elemtype->next->expression.vtype;
1051 v->context = ast_ctx(self);
1052 array->ir_v = self->ir_v = v;
1054 namelen = strlen(self->name);
1055 name = (char*)mem_a(namelen + 16);
1056 strcpy(name, self->name);
1058 array->ir_values = (ir_value**)mem_a(sizeof(array->ir_values[0]) * array->expression.count);
1059 array->ir_values[0] = v;
1060 for (ai = 1; ai < array->expression.count; ++ai) {
1061 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1062 array->ir_values[ai] = ir_builder_create_field(ir, name, vtype);
1063 if (!array->ir_values[ai]) {
1065 asterror(ast_ctx(self), "ir_builder_create_global failed");
1068 if (vtype == TYPE_FIELD)
1069 array->ir_values[ai]->fieldtype = elemtype->next->expression.vtype;
1070 array->ir_values[ai]->context = ast_ctx(self);
1076 v = ir_builder_create_field(ir, self->name, self->expression.next->expression.vtype);
1079 v->context = ast_ctx(self);
1085 if (self->expression.vtype == TYPE_ARRAY) {
1090 ast_expression_common *elemtype = &self->expression.next->expression;
1091 int vtype = elemtype->vtype;
1093 /* same as with field arrays */
1094 if (!self->expression.count || self->expression.count > opts_max_array_size)
1095 asterror(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1097 v = ir_builder_create_global(ir, self->name, vtype);
1099 asterror(ast_ctx(self), "ir_builder_create_global failed");
1102 if (vtype == TYPE_FIELD)
1103 v->fieldtype = elemtype->next->expression.vtype;
1104 v->context = ast_ctx(self);
1106 namelen = strlen(self->name);
1107 name = (char*)mem_a(namelen + 16);
1108 strcpy(name, self->name);
1110 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1111 self->ir_values[0] = v;
1112 for (ai = 1; ai < self->expression.count; ++ai) {
1113 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1114 self->ir_values[ai] = ir_builder_create_global(ir, name, vtype);
1115 if (!self->ir_values[ai]) {
1117 asterror(ast_ctx(self), "ir_builder_create_global failed");
1120 if (vtype == TYPE_FIELD)
1121 self->ir_values[ai]->fieldtype = elemtype->next->expression.vtype;
1122 self->ir_values[ai]->context = ast_ctx(self);
1128 /* Arrays don't do this since there's no "array" value which spans across the
1131 v = ir_builder_create_global(ir, self->name, self->expression.vtype);
1133 asterror(ast_ctx(self), "ir_builder_create_global failed");
1136 if (self->expression.vtype == TYPE_FIELD)
1137 v->fieldtype = self->expression.next->expression.vtype;
1138 v->context = ast_ctx(self);
1141 if (self->isconst) {
1142 switch (self->expression.vtype)
1145 if (!ir_value_set_float(v, self->constval.vfloat))
1149 if (!ir_value_set_vector(v, self->constval.vvec))
1153 if (!ir_value_set_string(v, self->constval.vstring))
1157 asterror(ast_ctx(self), "TODO: global constant array");
1160 asterror(ast_ctx(self), "global of type function not properly generated");
1162 /* Cannot generate an IR value for a function,
1163 * need a pointer pointing to a function rather.
1166 asterror(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1171 /* link us to the ir_value */
1175 error: /* clean up */
1180 bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
1183 if (self->isconst && self->expression.vtype == TYPE_FUNCTION)
1185 /* Do we allow local functions? I think not...
1186 * this is NOT a function pointer atm.
1191 if (self->expression.vtype == TYPE_ARRAY) {
1196 ast_expression_common *elemtype = &self->expression.next->expression;
1197 int vtype = elemtype->vtype;
1200 asterror(ast_ctx(self), "array-parameters are not supported");
1204 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1205 if (!self->expression.count || self->expression.count > opts_max_array_size) {
1206 asterror(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1209 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1210 if (!self->ir_values) {
1211 asterror(ast_ctx(self), "failed to allocate array values");
1215 v = ir_function_create_local(func, self->name, vtype, param);
1217 asterror(ast_ctx(self), "ir_function_create_local failed");
1220 if (vtype == TYPE_FIELD)
1221 v->fieldtype = elemtype->next->expression.vtype;
1222 v->context = ast_ctx(self);
1224 namelen = strlen(self->name);
1225 name = (char*)mem_a(namelen + 16);
1226 strcpy(name, self->name);
1228 self->ir_values[0] = v;
1229 for (ai = 1; ai < self->expression.count; ++ai) {
1230 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1231 self->ir_values[ai] = ir_function_create_local(func, name, vtype, param);
1232 if (!self->ir_values[ai]) {
1233 asterror(ast_ctx(self), "ir_builder_create_global failed");
1236 if (vtype == TYPE_FIELD)
1237 self->ir_values[ai]->fieldtype = elemtype->next->expression.vtype;
1238 self->ir_values[ai]->context = ast_ctx(self);
1243 v = ir_function_create_local(func, self->name, self->expression.vtype, param);
1246 if (self->expression.vtype == TYPE_FIELD)
1247 v->fieldtype = self->expression.next->expression.vtype;
1248 v->context = ast_ctx(self);
1251 /* A constant local... hmmm...
1252 * I suppose the IR will have to deal with this
1254 if (self->isconst) {
1255 switch (self->expression.vtype)
1258 if (!ir_value_set_float(v, self->constval.vfloat))
1262 if (!ir_value_set_vector(v, self->constval.vvec))
1266 if (!ir_value_set_string(v, self->constval.vstring))
1270 asterror(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1275 /* link us to the ir_value */
1279 if (!ast_global_codegen(self->setter, func->owner, false) ||
1280 !ast_function_codegen(self->setter->constval.vfunc, func->owner) ||
1281 !ir_function_finalize(self->setter->constval.vfunc->ir_func))
1285 if (!ast_global_codegen(self->getter, func->owner, false) ||
1286 !ast_function_codegen(self->getter->constval.vfunc, func->owner) ||
1287 !ir_function_finalize(self->getter->constval.vfunc->ir_func))
1292 error: /* clean up */
1297 bool ast_function_codegen(ast_function *self, ir_builder *ir)
1301 ast_expression_common *ec;
1306 irf = self->ir_func;
1308 asterror(ast_ctx(self), "ast_function's related ast_value was not generated yet");
1312 /* fill the parameter list */
1313 ec = &self->vtype->expression;
1314 for (i = 0; i < vec_size(ec->params); ++i)
1316 vec_push(irf->params, ec->params[i]->expression.vtype);
1317 if (!self->builtin) {
1318 if (!ast_local_codegen(ec->params[i], self->ir_func, true))
1323 if (self->builtin) {
1324 irf->builtin = self->builtin;
1328 if (!vec_size(self->blocks)) {
1329 asterror(ast_ctx(self), "function `%s` has no body", self->name);
1333 self->curblock = ir_function_create_block(irf, "entry");
1334 if (!self->curblock) {
1335 asterror(ast_ctx(self), "failed to allocate entry block for `%s`", self->name);
1339 for (i = 0; i < vec_size(self->blocks); ++i) {
1340 ast_expression_codegen *gen = self->blocks[i]->expression.codegen;
1341 if (!(*gen)((ast_expression*)self->blocks[i], self, false, &dummy))
1345 /* TODO: check return types */
1346 if (!self->curblock->is_return)
1348 return ir_block_create_return(self->curblock, NULL);
1349 /* From now on the parser has to handle this situation */
1351 if (!self->vtype->expression.next ||
1352 self->vtype->expression.next->expression.vtype == TYPE_VOID)
1354 return ir_block_create_return(self->curblock, NULL);
1358 /* error("missing return"); */
1359 asterror(ast_ctx(self), "function `%s` missing return value", self->name);
1367 /* Note, you will not see ast_block_codegen generate ir_blocks.
1368 * To the AST and the IR, blocks are 2 different things.
1369 * In the AST it represents a block of code, usually enclosed in
1370 * curly braces {...}.
1371 * While in the IR it represents a block in terms of control-flow.
1373 bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_value **out)
1377 /* We don't use this
1378 * Note: an ast-representation using the comma-operator
1379 * of the form: (a, b, c) = x should not assign to c...
1382 asterror(ast_ctx(self), "not an l-value (code-block)");
1386 if (self->expression.outr) {
1387 *out = self->expression.outr;
1391 /* output is NULL at first, we'll have each expression
1392 * assign to out output, thus, a comma-operator represention
1393 * using an ast_block will return the last generated value,
1394 * so: (b, c) + a executed both b and c, and returns c,
1395 * which is then added to a.
1399 /* generate locals */
1400 for (i = 0; i < vec_size(self->locals); ++i)
1402 if (!ast_local_codegen(self->locals[i], func->ir_func, false)) {
1404 asterror(ast_ctx(self), "failed to generate local `%s`", self->locals[i]->name);
1409 for (i = 0; i < vec_size(self->exprs); ++i)
1411 ast_expression_codegen *gen = self->exprs[i]->expression.codegen;
1412 if (func->curblock->final) {
1413 asterror(ast_ctx(self->exprs[i]), "unreachable statement");
1416 if (!(*gen)(self->exprs[i], func, false, out))
1420 self->expression.outr = *out;
1425 bool ast_store_codegen(ast_store *self, ast_function *func, bool lvalue, ir_value **out)
1427 ast_expression_codegen *cgen;
1428 ir_value *left = NULL;
1429 ir_value *right = NULL;
1433 ast_array_index *ai = NULL;
1435 if (lvalue && self->expression.outl) {
1436 *out = self->expression.outl;
1440 if (!lvalue && self->expression.outr) {
1441 *out = self->expression.outr;
1445 if (ast_istype(self->dest, ast_array_index))
1448 ai = (ast_array_index*)self->dest;
1449 idx = (ast_value*)ai->index;
1451 if (ast_istype(ai->index, ast_value) && idx->isconst)
1456 /* we need to call the setter */
1457 ir_value *iridx, *funval;
1461 asterror(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1465 arr = (ast_value*)ai->array;
1466 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1467 asterror(ast_ctx(self), "value has no setter (%s)", arr->name);
1471 cgen = idx->expression.codegen;
1472 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1475 cgen = arr->setter->expression.codegen;
1476 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
1479 cgen = self->source->expression.codegen;
1480 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1483 call = ir_block_create_call(func->curblock, ast_function_label(func, "store"), funval);
1486 ir_call_param(call, iridx);
1487 ir_call_param(call, right);
1488 self->expression.outr = right;
1494 cgen = self->dest->expression.codegen;
1496 if (!(*cgen)((ast_expression*)(self->dest), func, true, &left))
1498 self->expression.outl = left;
1500 cgen = self->source->expression.codegen;
1502 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1505 if (!ir_block_create_store_op(func->curblock, self->op, left, right))
1507 self->expression.outr = right;
1510 /* Theoretically, an assinment returns its left side as an
1511 * lvalue, if we don't need an lvalue though, we return
1512 * the right side as an rvalue, otherwise we have to
1513 * somehow know whether or not we need to dereference the pointer
1514 * on the left side - that is: OP_LOAD if it was an address.
1515 * Also: in original QC we cannot OP_LOADP *anyway*.
1517 *out = (lvalue ? left : right);
1522 bool ast_binary_codegen(ast_binary *self, ast_function *func, bool lvalue, ir_value **out)
1524 ast_expression_codegen *cgen;
1525 ir_value *left, *right;
1527 /* A binary operation cannot yield an l-value */
1529 asterror(ast_ctx(self), "not an l-value (binop)");
1533 if (self->expression.outr) {
1534 *out = self->expression.outr;
1538 if (OPTS_FLAG(SHORT_LOGIC) &&
1539 (self->op == INSTR_AND || self->op == INSTR_OR))
1541 /* short circuit evaluation */
1542 ir_block *other, *merge;
1543 ir_block *from_left, *from_right;
1548 /* Note about casting to true boolean values:
1549 * We use a single NOT for sub expressions, and an
1550 * overall NOT at the end, and for that purpose swap
1551 * all the jump conditions in order for the NOT to get
1553 * ie: (a && b) usually becomes (!!a ? !!b : !!a)
1554 * but we translate this to (!(!a ? !a : !b))
1557 merge_id = vec_size(func->ir_func->blocks);
1558 merge = ir_function_create_block(func->ir_func, ast_function_label(func, "sce_merge"));
1560 cgen = self->left->expression.codegen;
1561 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1563 if (!OPTS_FLAG(PERL_LOGIC)) {
1564 notop = type_not_instr[left->vtype];
1565 if (notop == AINSTR_END) {
1566 asterror(ast_ctx(self), "don't know how to cast to bool...");
1569 left = ir_block_create_unary(func->curblock,
1570 ast_function_label(func, "sce_not"),
1574 from_left = func->curblock;
1576 other = ir_function_create_block(func->ir_func, ast_function_label(func, "sce_other"));
1577 if ( !(self->op == INSTR_OR) != !OPTS_FLAG(PERL_LOGIC) ) {
1578 if (!ir_block_create_if(func->curblock, left, other, merge))
1581 if (!ir_block_create_if(func->curblock, left, merge, other))
1584 /* use the likely flag */
1585 vec_last(func->curblock->instr)->likely = true;
1587 func->curblock = other;
1588 cgen = self->right->expression.codegen;
1589 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1591 if (!OPTS_FLAG(PERL_LOGIC)) {
1592 notop = type_not_instr[right->vtype];
1593 if (notop == AINSTR_END) {
1594 asterror(ast_ctx(self), "don't know how to cast to bool...");
1597 right = ir_block_create_unary(func->curblock,
1598 ast_function_label(func, "sce_not"),
1602 from_right = func->curblock;
1604 if (!ir_block_create_jump(func->curblock, merge))
1607 vec_remove(func->ir_func->blocks, merge_id, 1);
1608 vec_push(func->ir_func->blocks, merge);
1610 func->curblock = merge;
1611 phi = ir_block_create_phi(func->curblock, ast_function_label(func, "sce_value"), TYPE_FLOAT);
1612 ir_phi_add(phi, from_left, left);
1613 ir_phi_add(phi, from_right, right);
1614 *out = ir_phi_value(phi);
1615 if (!OPTS_FLAG(PERL_LOGIC)) {
1616 notop = type_not_instr[(*out)->vtype];
1617 if (notop == AINSTR_END) {
1618 asterror(ast_ctx(self), "don't know how to cast to bool...");
1621 *out = ir_block_create_unary(func->curblock,
1622 ast_function_label(func, "sce_final_not"),
1628 self->expression.outr = *out;
1632 cgen = self->left->expression.codegen;
1633 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1636 cgen = self->right->expression.codegen;
1637 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1640 *out = ir_block_create_binop(func->curblock, ast_function_label(func, "bin"),
1641 self->op, left, right);
1644 self->expression.outr = *out;
1649 bool ast_binstore_codegen(ast_binstore *self, ast_function *func, bool lvalue, ir_value **out)
1651 ast_expression_codegen *cgen;
1652 ir_value *leftl = NULL, *leftr, *right, *bin;
1656 ast_array_index *ai = NULL;
1657 ir_value *iridx = NULL;
1659 if (lvalue && self->expression.outl) {
1660 *out = self->expression.outl;
1664 if (!lvalue && self->expression.outr) {
1665 *out = self->expression.outr;
1669 if (ast_istype(self->dest, ast_array_index))
1672 ai = (ast_array_index*)self->dest;
1673 idx = (ast_value*)ai->index;
1675 if (ast_istype(ai->index, ast_value) && idx->isconst)
1679 /* for a binstore we need both an lvalue and an rvalue for the left side */
1680 /* rvalue of destination! */
1682 cgen = idx->expression.codegen;
1683 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1686 cgen = self->dest->expression.codegen;
1687 if (!(*cgen)((ast_expression*)(self->dest), func, false, &leftr))
1690 /* source as rvalue only */
1691 cgen = self->source->expression.codegen;
1692 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1695 /* now the binary */
1696 bin = ir_block_create_binop(func->curblock, ast_function_label(func, "binst"),
1697 self->opbin, leftr, right);
1698 self->expression.outr = bin;
1702 /* we need to call the setter */
1707 asterror(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1711 arr = (ast_value*)ai->array;
1712 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1713 asterror(ast_ctx(self), "value has no setter (%s)", arr->name);
1717 cgen = arr->setter->expression.codegen;
1718 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
1721 call = ir_block_create_call(func->curblock, ast_function_label(func, "store"), funval);
1724 ir_call_param(call, iridx);
1725 ir_call_param(call, bin);
1726 self->expression.outr = bin;
1728 /* now store them */
1729 cgen = self->dest->expression.codegen;
1730 /* lvalue of destination */
1731 if (!(*cgen)((ast_expression*)(self->dest), func, true, &leftl))
1733 self->expression.outl = leftl;
1735 if (!ir_block_create_store_op(func->curblock, self->opstore, leftl, bin))
1737 self->expression.outr = bin;
1740 /* Theoretically, an assinment returns its left side as an
1741 * lvalue, if we don't need an lvalue though, we return
1742 * the right side as an rvalue, otherwise we have to
1743 * somehow know whether or not we need to dereference the pointer
1744 * on the left side - that is: OP_LOAD if it was an address.
1745 * Also: in original QC we cannot OP_LOADP *anyway*.
1747 *out = (lvalue ? leftl : bin);
1752 bool ast_unary_codegen(ast_unary *self, ast_function *func, bool lvalue, ir_value **out)
1754 ast_expression_codegen *cgen;
1757 /* An unary operation cannot yield an l-value */
1759 asterror(ast_ctx(self), "not an l-value (binop)");
1763 if (self->expression.outr) {
1764 *out = self->expression.outr;
1768 cgen = self->operand->expression.codegen;
1770 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
1773 *out = ir_block_create_unary(func->curblock, ast_function_label(func, "unary"),
1777 self->expression.outr = *out;
1782 bool ast_return_codegen(ast_return *self, ast_function *func, bool lvalue, ir_value **out)
1784 ast_expression_codegen *cgen;
1789 /* In the context of a return operation, we don't actually return
1793 asterror(ast_ctx(self), "return-expression is not an l-value");
1797 if (self->expression.outr) {
1798 asterror(ast_ctx(self), "internal error: ast_return cannot be reused, it bears no result!");
1801 self->expression.outr = (ir_value*)1;
1803 if (self->operand) {
1804 cgen = self->operand->expression.codegen;
1806 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
1809 if (!ir_block_create_return(func->curblock, operand))
1812 if (!ir_block_create_return(func->curblock, NULL))
1819 bool ast_entfield_codegen(ast_entfield *self, ast_function *func, bool lvalue, ir_value **out)
1821 ast_expression_codegen *cgen;
1822 ir_value *ent, *field;
1824 /* This function needs to take the 'lvalue' flag into account!
1825 * As lvalue we provide a field-pointer, as rvalue we provide the
1829 if (lvalue && self->expression.outl) {
1830 *out = self->expression.outl;
1834 if (!lvalue && self->expression.outr) {
1835 *out = self->expression.outr;
1839 cgen = self->entity->expression.codegen;
1840 if (!(*cgen)((ast_expression*)(self->entity), func, false, &ent))
1843 cgen = self->field->expression.codegen;
1844 if (!(*cgen)((ast_expression*)(self->field), func, false, &field))
1849 *out = ir_block_create_fieldaddress(func->curblock, ast_function_label(func, "efa"),
1852 *out = ir_block_create_load_from_ent(func->curblock, ast_function_label(func, "efv"),
1853 ent, field, self->expression.vtype);
1856 asterror(ast_ctx(self), "failed to create %s instruction (output type %s)",
1857 (lvalue ? "ADDRESS" : "FIELD"),
1858 type_name[self->expression.vtype]);
1863 self->expression.outl = *out;
1865 self->expression.outr = *out;
1867 /* Hm that should be it... */
1871 bool ast_member_codegen(ast_member *self, ast_function *func, bool lvalue, ir_value **out)
1873 ast_expression_codegen *cgen;
1876 /* in QC this is always an lvalue */
1878 if (self->expression.outl) {
1879 *out = self->expression.outl;
1883 cgen = self->owner->expression.codegen;
1884 if (!(*cgen)((ast_expression*)(self->owner), func, true, &vec))
1887 if (vec->vtype != TYPE_VECTOR &&
1888 !(vec->vtype == TYPE_FIELD && self->owner->expression.next->expression.vtype == TYPE_VECTOR))
1893 *out = ir_value_vector_member(vec, self->field);
1894 self->expression.outl = *out;
1896 return (*out != NULL);
1899 bool ast_array_index_codegen(ast_array_index *self, ast_function *func, bool lvalue, ir_value **out)
1904 if (!lvalue && self->expression.outr) {
1905 *out = self->expression.outr;
1907 if (lvalue && self->expression.outl) {
1908 *out = self->expression.outl;
1911 if (!ast_istype(self->array, ast_value)) {
1912 asterror(ast_ctx(self), "array indexing this way is not supported");
1913 /* note this would actually be pointer indexing because the left side is
1914 * not an actual array but (hopefully) an indexable expression.
1915 * Once we get integer arithmetic, and GADDRESS/GSTORE/GLOAD instruction
1916 * support this path will be filled.
1921 arr = (ast_value*)self->array;
1922 idx = (ast_value*)self->index;
1924 if (!ast_istype(self->index, ast_value) || !idx->isconst) {
1925 /* Time to use accessor functions */
1926 ast_expression_codegen *cgen;
1927 ir_value *iridx, *funval;
1931 asterror(ast_ctx(self), "(.2) array indexing here needs a compile-time constant");
1936 asterror(ast_ctx(self), "value has no getter, don't know how to index it");
1940 cgen = self->index->expression.codegen;
1941 if (!(*cgen)((ast_expression*)(self->index), func, true, &iridx))
1944 cgen = arr->getter->expression.codegen;
1945 if (!(*cgen)((ast_expression*)(arr->getter), func, true, &funval))
1948 call = ir_block_create_call(func->curblock, ast_function_label(func, "fetch"), funval);
1951 ir_call_param(call, iridx);
1953 *out = ir_call_value(call);
1954 self->expression.outr = *out;
1958 if (idx->expression.vtype == TYPE_FLOAT)
1959 *out = arr->ir_values[(int)idx->constval.vfloat];
1960 else if (idx->expression.vtype == TYPE_INTEGER)
1961 *out = arr->ir_values[idx->constval.vint];
1963 asterror(ast_ctx(self), "array indexing here needs an integer constant");
1969 bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_value **out)
1971 ast_expression_codegen *cgen;
1976 ir_block *cond = func->curblock;
1979 ir_block *ontrue_endblock = NULL;
1980 ir_block *onfalse_endblock = NULL;
1983 /* We don't output any value, thus also don't care about r/lvalue */
1987 if (self->expression.outr) {
1988 asterror(ast_ctx(self), "internal error: ast_ifthen cannot be reused, it bears no result!");
1991 self->expression.outr = (ir_value*)1;
1993 /* generate the condition */
1994 cgen = self->cond->expression.codegen;
1995 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
1997 /* update the block which will get the jump - because short-logic or ternaries may have changed this */
1998 cond = func->curblock;
2002 if (self->on_true) {
2003 /* create on-true block */
2004 ontrue = ir_function_create_block(func->ir_func, ast_function_label(func, "ontrue"));
2008 /* enter the block */
2009 func->curblock = ontrue;
2012 cgen = self->on_true->expression.codegen;
2013 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &dummy))
2016 /* we now need to work from the current endpoint */
2017 ontrue_endblock = func->curblock;
2022 if (self->on_false) {
2023 /* create on-false block */
2024 onfalse = ir_function_create_block(func->ir_func, ast_function_label(func, "onfalse"));
2028 /* enter the block */
2029 func->curblock = onfalse;
2032 cgen = self->on_false->expression.codegen;
2033 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &dummy))
2036 /* we now need to work from the current endpoint */
2037 onfalse_endblock = func->curblock;
2041 /* Merge block were they all merge in to */
2042 merge = ir_function_create_block(func->ir_func, ast_function_label(func, "endif"));
2045 /* add jumps ot the merge block */
2046 if (ontrue && !ontrue_endblock->final && !ir_block_create_jump(ontrue_endblock, merge))
2048 if (onfalse && !onfalse_endblock->final && !ir_block_create_jump(onfalse_endblock, merge))
2051 /* we create the if here, that way all blocks are ordered :)
2053 if (!ir_block_create_if(cond, condval,
2054 (ontrue ? ontrue : merge),
2055 (onfalse ? onfalse : merge)))
2060 /* Now enter the merge block */
2061 func->curblock = merge;
2066 bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_value **out)
2068 ast_expression_codegen *cgen;
2071 ir_value *trueval, *falseval;
2074 ir_block *cond = func->curblock;
2079 /* Ternary can never create an lvalue... */
2083 /* In theory it shouldn't be possible to pass through a node twice, but
2084 * in case we add any kind of optimization pass for the AST itself, it
2085 * may still happen, thus we remember a created ir_value and simply return one
2086 * if it already exists.
2088 if (self->expression.outr) {
2089 *out = self->expression.outr;
2093 /* In the following, contraty to ast_ifthen, we assume both paths exist. */
2095 /* generate the condition */
2096 func->curblock = cond;
2097 cgen = self->cond->expression.codegen;
2098 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2101 /* create on-true block */
2102 ontrue = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_T"));
2107 /* enter the block */
2108 func->curblock = ontrue;
2111 cgen = self->on_true->expression.codegen;
2112 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &trueval))
2116 /* create on-false block */
2117 onfalse = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_F"));
2122 /* enter the block */
2123 func->curblock = onfalse;
2126 cgen = self->on_false->expression.codegen;
2127 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &falseval))
2131 /* create merge block */
2132 merge = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_out"));
2135 /* jump to merge block */
2136 if (!ir_block_create_jump(ontrue, merge))
2138 if (!ir_block_create_jump(onfalse, merge))
2141 /* create if instruction */
2142 if (!ir_block_create_if(cond, condval, ontrue, onfalse))
2145 /* Now enter the merge block */
2146 func->curblock = merge;
2148 /* Here, now, we need a PHI node
2149 * but first some sanity checking...
2151 if (trueval->vtype != falseval->vtype) {
2152 /* error("ternary with different types on the two sides"); */
2157 phi = ir_block_create_phi(merge, ast_function_label(func, "phi"), trueval->vtype);
2160 ir_phi_add(phi, ontrue, trueval);
2161 ir_phi_add(phi, onfalse, falseval);
2163 self->expression.outr = ir_phi_value(phi);
2164 *out = self->expression.outr;
2169 bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value **out)
2171 ast_expression_codegen *cgen;
2173 ir_value *dummy = NULL;
2174 ir_value *precond = NULL;
2175 ir_value *postcond = NULL;
2177 /* Since we insert some jumps "late" so we have blocks
2178 * ordered "nicely", we need to keep track of the actual end-blocks
2179 * of expressions to add the jumps to.
2181 ir_block *bbody = NULL, *end_bbody = NULL;
2182 ir_block *bprecond = NULL, *end_bprecond = NULL;
2183 ir_block *bpostcond = NULL, *end_bpostcond = NULL;
2184 ir_block *bincrement = NULL, *end_bincrement = NULL;
2185 ir_block *bout = NULL, *bin = NULL;
2187 /* let's at least move the outgoing block to the end */
2190 /* 'break' and 'continue' need to be able to find the right blocks */
2191 ir_block *bcontinue = NULL;
2192 ir_block *bbreak = NULL;
2194 ir_block *old_bcontinue = NULL;
2195 ir_block *old_bbreak = NULL;
2197 ir_block *tmpblock = NULL;
2202 if (self->expression.outr) {
2203 asterror(ast_ctx(self), "internal error: ast_loop cannot be reused, it bears no result!");
2206 self->expression.outr = (ir_value*)1;
2209 * Should we ever need some kind of block ordering, better make this function
2210 * move blocks around than write a block ordering algorithm later... after all
2211 * the ast and ir should work together, not against each other.
2214 /* initexpr doesn't get its own block, it's pointless, it could create more blocks
2215 * anyway if for example it contains a ternary.
2219 cgen = self->initexpr->expression.codegen;
2220 if (!(*cgen)((ast_expression*)(self->initexpr), func, false, &dummy))
2224 /* Store the block from which we enter this chaos */
2225 bin = func->curblock;
2227 /* The pre-loop condition needs its own block since we
2228 * need to be able to jump to the start of that expression.
2232 bprecond = ir_function_create_block(func->ir_func, ast_function_label(func, "pre_loop_cond"));
2236 /* the pre-loop-condition the least important place to 'continue' at */
2237 bcontinue = bprecond;
2240 func->curblock = bprecond;
2243 cgen = self->precond->expression.codegen;
2244 if (!(*cgen)((ast_expression*)(self->precond), func, false, &precond))
2247 end_bprecond = func->curblock;
2249 bprecond = end_bprecond = NULL;
2252 /* Now the next blocks won't be ordered nicely, but we need to
2253 * generate them this early for 'break' and 'continue'.
2255 if (self->increment) {
2256 bincrement = ir_function_create_block(func->ir_func, ast_function_label(func, "loop_increment"));
2259 bcontinue = bincrement; /* increment comes before the pre-loop-condition */
2261 bincrement = end_bincrement = NULL;
2264 if (self->postcond) {
2265 bpostcond = ir_function_create_block(func->ir_func, ast_function_label(func, "post_loop_cond"));
2268 bcontinue = bpostcond; /* postcond comes before the increment */
2270 bpostcond = end_bpostcond = NULL;
2273 bout_id = vec_size(func->ir_func->blocks);
2274 bout = ir_function_create_block(func->ir_func, ast_function_label(func, "after_loop"));
2279 /* The loop body... */
2282 bbody = ir_function_create_block(func->ir_func, ast_function_label(func, "loop_body"));
2287 func->curblock = bbody;
2289 old_bbreak = func->breakblock;
2290 old_bcontinue = func->continueblock;
2291 func->breakblock = bbreak;
2292 func->continueblock = bcontinue;
2295 cgen = self->body->expression.codegen;
2296 if (!(*cgen)((ast_expression*)(self->body), func, false, &dummy))
2299 end_bbody = func->curblock;
2300 func->breakblock = old_bbreak;
2301 func->continueblock = old_bcontinue;
2304 /* post-loop-condition */
2308 func->curblock = bpostcond;
2311 cgen = self->postcond->expression.codegen;
2312 if (!(*cgen)((ast_expression*)(self->postcond), func, false, &postcond))
2315 end_bpostcond = func->curblock;
2318 /* The incrementor */
2319 if (self->increment)
2322 func->curblock = bincrement;
2325 cgen = self->increment->expression.codegen;
2326 if (!(*cgen)((ast_expression*)(self->increment), func, false, &dummy))
2329 end_bincrement = func->curblock;
2332 /* In any case now, we continue from the outgoing block */
2333 func->curblock = bout;
2335 /* Now all blocks are in place */
2336 /* From 'bin' we jump to whatever comes first */
2337 if (bprecond) tmpblock = bprecond;
2338 else if (bbody) tmpblock = bbody;
2339 else if (bpostcond) tmpblock = bpostcond;
2340 else tmpblock = bout;
2341 if (!ir_block_create_jump(bin, tmpblock))
2347 ir_block *ontrue, *onfalse;
2348 if (bbody) ontrue = bbody;
2349 else if (bincrement) ontrue = bincrement;
2350 else if (bpostcond) ontrue = bpostcond;
2351 else ontrue = bprecond;
2353 if (!ir_block_create_if(end_bprecond, precond, ontrue, onfalse))
2360 if (bincrement) tmpblock = bincrement;
2361 else if (bpostcond) tmpblock = bpostcond;
2362 else if (bprecond) tmpblock = bprecond;
2363 else tmpblock = bout;
2364 if (!end_bbody->final && !ir_block_create_jump(end_bbody, tmpblock))
2368 /* from increment */
2371 if (bpostcond) tmpblock = bpostcond;
2372 else if (bprecond) tmpblock = bprecond;
2373 else if (bbody) tmpblock = bbody;
2374 else tmpblock = bout;
2375 if (!ir_block_create_jump(end_bincrement, tmpblock))
2382 ir_block *ontrue, *onfalse;
2383 if (bprecond) ontrue = bprecond;
2384 else if (bbody) ontrue = bbody;
2385 else if (bincrement) ontrue = bincrement;
2386 else ontrue = bpostcond;
2388 if (!ir_block_create_if(end_bpostcond, postcond, ontrue, onfalse))
2392 /* Move 'bout' to the end */
2393 vec_remove(func->ir_func->blocks, bout_id, 1);
2394 vec_push(func->ir_func->blocks, bout);
2399 bool ast_breakcont_codegen(ast_breakcont *self, ast_function *func, bool lvalue, ir_value **out)
2406 asterror(ast_ctx(self), "break/continue expression is not an l-value");
2410 if (self->expression.outr) {
2411 asterror(ast_ctx(self), "internal error: ast_breakcont cannot be reused!");
2414 self->expression.outr = (ir_value*)1;
2416 if (self->is_continue)
2417 target = func->continueblock;
2419 target = func->breakblock;
2421 if (!ir_block_create_jump(func->curblock, target))
2426 bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_value **out)
2428 ast_expression_codegen *cgen;
2430 ast_switch_case *def_case = NULL;
2431 ir_block *def_bfall = NULL;
2433 ir_value *dummy = NULL;
2434 ir_value *irop = NULL;
2435 ir_block *old_break = NULL;
2436 ir_block *bout = NULL;
2437 ir_block *bfall = NULL;
2445 asterror(ast_ctx(self), "switch expression is not an l-value");
2449 if (self->expression.outr) {
2450 asterror(ast_ctx(self), "internal error: ast_switch cannot be reused!");
2453 self->expression.outr = (ir_value*)1;
2458 cgen = self->operand->expression.codegen;
2459 if (!(*cgen)((ast_expression*)(self->operand), func, false, &irop))
2462 if (!vec_size(self->cases))
2465 cmpinstr = type_eq_instr[irop->vtype];
2466 if (cmpinstr >= AINSTR_END) {
2467 ast_type_to_string(self->operand, typestr, sizeof(typestr));
2468 asterror(ast_ctx(self), "invalid type to perform a switch on: %s", typestr);
2472 bout_id = vec_size(func->ir_func->blocks);
2473 bout = ir_function_create_block(func->ir_func, ast_function_label(func, "after_switch"));
2477 /* setup the break block */
2478 old_break = func->breakblock;
2479 func->breakblock = bout;
2481 /* Now create all cases */
2482 for (c = 0; c < vec_size(self->cases); ++c) {
2483 ir_value *cond, *val;
2484 ir_block *bcase, *bnot;
2487 ast_switch_case *swcase = &self->cases[c];
2489 if (swcase->value) {
2490 /* A regular case */
2491 /* generate the condition operand */
2492 cgen = swcase->value->expression.codegen;
2493 if (!(*cgen)((ast_expression*)(swcase->value), func, false, &val))
2495 /* generate the condition */
2496 cond = ir_block_create_binop(func->curblock, ast_function_label(func, "switch_eq"), cmpinstr, irop, val);
2500 bcase = ir_function_create_block(func->ir_func, ast_function_label(func, "case"));
2501 bnot_id = vec_size(func->ir_func->blocks);
2502 bnot = ir_function_create_block(func->ir_func, ast_function_label(func, "not_case"));
2503 if (!bcase || !bnot)
2505 if (!ir_block_create_if(func->curblock, cond, bcase, bnot))
2508 /* Make the previous case-end fall through */
2509 if (bfall && !bfall->final) {
2510 if (!ir_block_create_jump(bfall, bcase))
2514 /* enter the case */
2515 func->curblock = bcase;
2516 cgen = swcase->code->expression.codegen;
2517 if (!(*cgen)((ast_expression*)swcase->code, func, false, &dummy))
2520 /* remember this block to fall through from */
2521 bfall = func->curblock;
2523 /* enter the else and move it down */
2524 func->curblock = bnot;
2525 vec_remove(func->ir_func->blocks, bnot_id, 1);
2526 vec_push(func->ir_func->blocks, bnot);
2528 /* The default case */
2529 /* Remember where to fall through from: */
2532 /* remember which case it was */
2537 /* Jump from the last bnot to bout */
2538 if (bfall && !bfall->final && !ir_block_create_jump(bfall, bout)) {
2540 astwarning(ast_ctx(bfall), WARN_???, "missing break after last case");
2545 /* If there was a default case, put it down here */
2549 /* No need to create an extra block */
2550 bcase = func->curblock;
2552 /* Insert the fallthrough jump */
2553 if (def_bfall && !def_bfall->final) {
2554 if (!ir_block_create_jump(def_bfall, bcase))
2558 /* Now generate the default code */
2559 cgen = def_case->code->expression.codegen;
2560 if (!(*cgen)((ast_expression*)def_case->code, func, false, &dummy))
2564 /* Jump from the last bnot to bout */
2565 if (!func->curblock->final && !ir_block_create_jump(func->curblock, bout))
2567 /* enter the outgoing block */
2568 func->curblock = bout;
2570 /* restore the break block */
2571 func->breakblock = old_break;
2573 /* Move 'bout' to the end, it's nicer */
2574 vec_remove(func->ir_func->blocks, bout_id, 1);
2575 vec_push(func->ir_func->blocks, bout);
2580 bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value **out)
2582 ast_expression_codegen *cgen;
2584 ir_instr *callinstr;
2587 ir_value *funval = NULL;
2589 /* return values are never lvalues */
2591 asterror(ast_ctx(self), "not an l-value (function call)");
2595 if (self->expression.outr) {
2596 *out = self->expression.outr;
2600 cgen = self->func->expression.codegen;
2601 if (!(*cgen)((ast_expression*)(self->func), func, false, &funval))
2609 for (i = 0; i < vec_size(self->params); ++i)
2612 ast_expression *expr = self->params[i];
2614 cgen = expr->expression.codegen;
2615 if (!(*cgen)(expr, func, false, ¶m))
2619 vec_push(params, param);
2622 callinstr = ir_block_create_call(func->curblock, ast_function_label(func, "call"), funval);
2626 for (i = 0; i < vec_size(params); ++i) {
2627 ir_call_param(callinstr, params[i]);
2630 *out = ir_call_value(callinstr);
2631 self->expression.outr = *out;