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)
900 self->name = name ? util_strdup(name) : NULL;
903 self->labelcount = 0;
906 self->ir_func = NULL;
907 self->curblock = NULL;
909 self->breakblock = NULL;
910 self->continueblock = NULL;
912 vtype->isconst = true;
913 vtype->constval.vfunc = self;
918 void ast_function_delete(ast_function *self)
922 mem_d((void*)self->name);
924 /* ast_value_delete(self->vtype); */
925 self->vtype->isconst = false;
926 self->vtype->constval.vfunc = NULL;
927 /* We use unref - if it was stored in a global table it is supposed
928 * to be deleted from *there*
930 ast_unref(self->vtype);
932 for (i = 0; i < vec_size(self->blocks); ++i)
933 ast_delete(self->blocks[i]);
934 vec_free(self->blocks);
938 const char* ast_function_label(ast_function *self, const char *prefix)
944 if (!opts_dump && !opts_dumpfin)
947 id = (self->labelcount++);
948 len = strlen(prefix);
950 from = self->labelbuf + sizeof(self->labelbuf)-1;
953 unsigned int digit = id % 10;
957 memcpy(from - len, prefix, len);
961 /*********************************************************************/
963 * by convention you must never pass NULL to the 'ir_value **out'
964 * parameter. If you really don't care about the output, pass a dummy.
965 * But I can't imagine a pituation where the output is truly unnecessary.
968 bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out)
972 /* NOTE: This is the codegen for a variable used in an expression.
973 * It is not the codegen to generate the value. For this purpose,
974 * ast_local_codegen and ast_global_codegen are to be used before this
975 * is executed. ast_function_codegen should take care of its locals,
976 * and the ast-user should take care of ast_global_codegen to be used
977 * on all the globals.
981 ast_type_to_string((ast_expression*)self, typename, sizeof(typename));
982 asterror(ast_ctx(self), "ast_value used before generated %s %s", typename, self->name);
989 bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
993 if (self->isconst && self->expression.vtype == TYPE_FUNCTION)
995 ir_function *func = ir_builder_create_function(ir, self->name, self->expression.next->expression.vtype);
998 func->context = ast_ctx(self);
999 func->value->context = ast_ctx(self);
1001 self->constval.vfunc->ir_func = func;
1002 self->ir_v = func->value;
1003 /* The function is filled later on ast_function_codegen... */
1007 if (isfield && self->expression.vtype == TYPE_FIELD) {
1008 ast_expression *fieldtype = self->expression.next;
1010 if (self->isconst) {
1011 asterror(ast_ctx(self), "TODO: constant field pointers with value");
1015 if (fieldtype->expression.vtype == TYPE_ARRAY) {
1020 ast_expression_common *elemtype;
1022 ast_value *array = (ast_value*)fieldtype;
1024 if (!ast_istype(fieldtype, ast_value)) {
1025 asterror(ast_ctx(self), "internal error: ast_value required");
1029 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1030 if (!array->expression.count || array->expression.count > opts_max_array_size)
1031 asterror(ast_ctx(self), "Invalid array of size %lu", (unsigned long)array->expression.count);
1033 elemtype = &array->expression.next->expression;
1034 vtype = elemtype->vtype;
1036 v = ir_builder_create_field(ir, self->name, vtype);
1038 asterror(ast_ctx(self), "ir_builder_create_global failed");
1041 if (vtype == TYPE_FIELD)
1042 v->fieldtype = elemtype->next->expression.vtype;
1043 v->context = ast_ctx(self);
1044 array->ir_v = self->ir_v = v;
1046 namelen = strlen(self->name);
1047 name = (char*)mem_a(namelen + 16);
1048 strcpy(name, self->name);
1050 array->ir_values = (ir_value**)mem_a(sizeof(array->ir_values[0]) * array->expression.count);
1051 array->ir_values[0] = v;
1052 for (ai = 1; ai < array->expression.count; ++ai) {
1053 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1054 array->ir_values[ai] = ir_builder_create_field(ir, name, vtype);
1055 if (!array->ir_values[ai]) {
1057 asterror(ast_ctx(self), "ir_builder_create_global failed");
1060 if (vtype == TYPE_FIELD)
1061 array->ir_values[ai]->fieldtype = elemtype->next->expression.vtype;
1062 array->ir_values[ai]->context = ast_ctx(self);
1068 v = ir_builder_create_field(ir, self->name, self->expression.next->expression.vtype);
1071 v->context = ast_ctx(self);
1077 if (self->expression.vtype == TYPE_ARRAY) {
1082 ast_expression_common *elemtype = &self->expression.next->expression;
1083 int vtype = elemtype->vtype;
1085 /* same as with field arrays */
1086 if (!self->expression.count || self->expression.count > opts_max_array_size)
1087 asterror(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1089 v = ir_builder_create_global(ir, self->name, vtype);
1091 asterror(ast_ctx(self), "ir_builder_create_global failed");
1094 if (vtype == TYPE_FIELD)
1095 v->fieldtype = elemtype->next->expression.vtype;
1096 v->context = ast_ctx(self);
1098 namelen = strlen(self->name);
1099 name = (char*)mem_a(namelen + 16);
1100 strcpy(name, self->name);
1102 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1103 self->ir_values[0] = v;
1104 for (ai = 1; ai < self->expression.count; ++ai) {
1105 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1106 self->ir_values[ai] = ir_builder_create_global(ir, name, vtype);
1107 if (!self->ir_values[ai]) {
1109 asterror(ast_ctx(self), "ir_builder_create_global failed");
1112 if (vtype == TYPE_FIELD)
1113 self->ir_values[ai]->fieldtype = elemtype->next->expression.vtype;
1114 self->ir_values[ai]->context = ast_ctx(self);
1120 /* Arrays don't do this since there's no "array" value which spans across the
1123 v = ir_builder_create_global(ir, self->name, self->expression.vtype);
1125 asterror(ast_ctx(self), "ir_builder_create_global failed");
1128 if (self->expression.vtype == TYPE_FIELD)
1129 v->fieldtype = self->expression.next->expression.vtype;
1130 v->context = ast_ctx(self);
1133 if (self->isconst) {
1134 switch (self->expression.vtype)
1137 if (!ir_value_set_float(v, self->constval.vfloat))
1141 if (!ir_value_set_vector(v, self->constval.vvec))
1145 if (!ir_value_set_string(v, self->constval.vstring))
1149 asterror(ast_ctx(self), "TODO: global constant array");
1152 asterror(ast_ctx(self), "global of type function not properly generated");
1154 /* Cannot generate an IR value for a function,
1155 * need a pointer pointing to a function rather.
1158 asterror(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1163 /* link us to the ir_value */
1167 error: /* clean up */
1172 bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
1175 if (self->isconst && self->expression.vtype == TYPE_FUNCTION)
1177 /* Do we allow local functions? I think not...
1178 * this is NOT a function pointer atm.
1183 if (self->expression.vtype == TYPE_ARRAY) {
1188 ast_expression_common *elemtype = &self->expression.next->expression;
1189 int vtype = elemtype->vtype;
1192 asterror(ast_ctx(self), "array-parameters are not supported");
1196 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1197 if (!self->expression.count || self->expression.count > opts_max_array_size) {
1198 asterror(ast_ctx(self), "Invalid array of size %lu", (unsigned long)self->expression.count);
1201 self->ir_values = (ir_value**)mem_a(sizeof(self->ir_values[0]) * self->expression.count);
1202 if (!self->ir_values) {
1203 asterror(ast_ctx(self), "failed to allocate array values");
1207 v = ir_function_create_local(func, self->name, vtype, param);
1209 asterror(ast_ctx(self), "ir_function_create_local failed");
1212 if (vtype == TYPE_FIELD)
1213 v->fieldtype = elemtype->next->expression.vtype;
1214 v->context = ast_ctx(self);
1216 namelen = strlen(self->name);
1217 name = (char*)mem_a(namelen + 16);
1218 strcpy(name, self->name);
1220 self->ir_values[0] = v;
1221 for (ai = 1; ai < self->expression.count; ++ai) {
1222 snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1223 self->ir_values[ai] = ir_function_create_local(func, name, vtype, param);
1224 if (!self->ir_values[ai]) {
1225 asterror(ast_ctx(self), "ir_builder_create_global failed");
1228 if (vtype == TYPE_FIELD)
1229 self->ir_values[ai]->fieldtype = elemtype->next->expression.vtype;
1230 self->ir_values[ai]->context = ast_ctx(self);
1235 v = ir_function_create_local(func, self->name, self->expression.vtype, param);
1238 if (self->expression.vtype == TYPE_FIELD)
1239 v->fieldtype = self->expression.next->expression.vtype;
1240 v->context = ast_ctx(self);
1243 /* A constant local... hmmm...
1244 * I suppose the IR will have to deal with this
1246 if (self->isconst) {
1247 switch (self->expression.vtype)
1250 if (!ir_value_set_float(v, self->constval.vfloat))
1254 if (!ir_value_set_vector(v, self->constval.vvec))
1258 if (!ir_value_set_string(v, self->constval.vstring))
1262 asterror(ast_ctx(self), "TODO: global constant type %i", self->expression.vtype);
1267 /* link us to the ir_value */
1271 if (!ast_global_codegen(self->setter, func->owner, false) ||
1272 !ast_function_codegen(self->setter->constval.vfunc, func->owner) ||
1273 !ir_function_finalize(self->setter->constval.vfunc->ir_func))
1277 if (!ast_global_codegen(self->getter, func->owner, false) ||
1278 !ast_function_codegen(self->getter->constval.vfunc, func->owner) ||
1279 !ir_function_finalize(self->getter->constval.vfunc->ir_func))
1284 error: /* clean up */
1289 bool ast_function_codegen(ast_function *self, ir_builder *ir)
1293 ast_expression_common *ec;
1298 irf = self->ir_func;
1300 asterror(ast_ctx(self), "ast_function's related ast_value was not generated yet");
1304 /* fill the parameter list */
1305 ec = &self->vtype->expression;
1306 for (i = 0; i < vec_size(ec->params); ++i)
1308 vec_push(irf->params, ec->params[i]->expression.vtype);
1309 if (!self->builtin) {
1310 if (!ast_local_codegen(ec->params[i], self->ir_func, true))
1315 if (self->builtin) {
1316 irf->builtin = self->builtin;
1320 if (!vec_size(self->blocks)) {
1321 asterror(ast_ctx(self), "function `%s` has no body", self->name);
1325 self->curblock = ir_function_create_block(irf, "entry");
1326 if (!self->curblock) {
1327 asterror(ast_ctx(self), "failed to allocate entry block for `%s`", self->name);
1331 for (i = 0; i < vec_size(self->blocks); ++i) {
1332 ast_expression_codegen *gen = self->blocks[i]->expression.codegen;
1333 if (!(*gen)((ast_expression*)self->blocks[i], self, false, &dummy))
1337 /* TODO: check return types */
1338 if (!self->curblock->is_return)
1340 return ir_block_create_return(self->curblock, NULL);
1341 /* From now on the parser has to handle this situation */
1343 if (!self->vtype->expression.next ||
1344 self->vtype->expression.next->expression.vtype == TYPE_VOID)
1346 return ir_block_create_return(self->curblock, NULL);
1350 /* error("missing return"); */
1351 asterror(ast_ctx(self), "function `%s` missing return value", self->name);
1359 /* Note, you will not see ast_block_codegen generate ir_blocks.
1360 * To the AST and the IR, blocks are 2 different things.
1361 * In the AST it represents a block of code, usually enclosed in
1362 * curly braces {...}.
1363 * While in the IR it represents a block in terms of control-flow.
1365 bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_value **out)
1369 /* We don't use this
1370 * Note: an ast-representation using the comma-operator
1371 * of the form: (a, b, c) = x should not assign to c...
1374 asterror(ast_ctx(self), "not an l-value (code-block)");
1378 if (self->expression.outr) {
1379 *out = self->expression.outr;
1383 /* output is NULL at first, we'll have each expression
1384 * assign to out output, thus, a comma-operator represention
1385 * using an ast_block will return the last generated value,
1386 * so: (b, c) + a executed both b and c, and returns c,
1387 * which is then added to a.
1391 /* generate locals */
1392 for (i = 0; i < vec_size(self->locals); ++i)
1394 if (!ast_local_codegen(self->locals[i], func->ir_func, false)) {
1396 asterror(ast_ctx(self), "failed to generate local `%s`", self->locals[i]->name);
1401 for (i = 0; i < vec_size(self->exprs); ++i)
1403 ast_expression_codegen *gen = self->exprs[i]->expression.codegen;
1404 if (func->curblock->final) {
1405 asterror(ast_ctx(self->exprs[i]), "unreachable statement");
1408 if (!(*gen)(self->exprs[i], func, false, out))
1412 self->expression.outr = *out;
1417 bool ast_store_codegen(ast_store *self, ast_function *func, bool lvalue, ir_value **out)
1419 ast_expression_codegen *cgen;
1420 ir_value *left = NULL;
1421 ir_value *right = NULL;
1425 ast_array_index *ai = NULL;
1427 if (lvalue && self->expression.outl) {
1428 *out = self->expression.outl;
1432 if (!lvalue && self->expression.outr) {
1433 *out = self->expression.outr;
1437 if (ast_istype(self->dest, ast_array_index))
1440 ai = (ast_array_index*)self->dest;
1441 idx = (ast_value*)ai->index;
1443 if (ast_istype(ai->index, ast_value) && idx->isconst)
1448 /* we need to call the setter */
1449 ir_value *iridx, *funval;
1453 asterror(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1457 arr = (ast_value*)ai->array;
1458 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1459 asterror(ast_ctx(self), "value has no setter (%s)", arr->name);
1463 cgen = idx->expression.codegen;
1464 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1467 cgen = arr->setter->expression.codegen;
1468 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
1471 cgen = self->source->expression.codegen;
1472 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1475 call = ir_block_create_call(func->curblock, ast_function_label(func, "store"), funval);
1478 ir_call_param(call, iridx);
1479 ir_call_param(call, right);
1480 self->expression.outr = right;
1486 cgen = self->dest->expression.codegen;
1488 if (!(*cgen)((ast_expression*)(self->dest), func, true, &left))
1490 self->expression.outl = left;
1492 cgen = self->source->expression.codegen;
1494 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1497 if (!ir_block_create_store_op(func->curblock, self->op, left, right))
1499 self->expression.outr = right;
1502 /* Theoretically, an assinment returns its left side as an
1503 * lvalue, if we don't need an lvalue though, we return
1504 * the right side as an rvalue, otherwise we have to
1505 * somehow know whether or not we need to dereference the pointer
1506 * on the left side - that is: OP_LOAD if it was an address.
1507 * Also: in original QC we cannot OP_LOADP *anyway*.
1509 *out = (lvalue ? left : right);
1514 bool ast_binary_codegen(ast_binary *self, ast_function *func, bool lvalue, ir_value **out)
1516 ast_expression_codegen *cgen;
1517 ir_value *left, *right;
1519 /* A binary operation cannot yield an l-value */
1521 asterror(ast_ctx(self), "not an l-value (binop)");
1525 if (self->expression.outr) {
1526 *out = self->expression.outr;
1530 if (OPTS_FLAG(SHORT_LOGIC) &&
1531 (self->op == INSTR_AND || self->op == INSTR_OR))
1533 /* short circuit evaluation */
1534 ir_block *other, *merge;
1535 ir_block *from_left, *from_right;
1540 /* Note about casting to true boolean values:
1541 * We use a single NOT for sub expressions, and an
1542 * overall NOT at the end, and for that purpose swap
1543 * all the jump conditions in order for the NOT to get
1545 * ie: (a && b) usually becomes (!!a ? !!b : !!a)
1546 * but we translate this to (!(!a ? !a : !b))
1549 merge_id = vec_size(func->ir_func->blocks);
1550 merge = ir_function_create_block(func->ir_func, ast_function_label(func, "sce_merge"));
1552 cgen = self->left->expression.codegen;
1553 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1555 if (!OPTS_FLAG(PERL_LOGIC)) {
1556 notop = type_not_instr[left->vtype];
1557 if (notop == AINSTR_END) {
1558 asterror(ast_ctx(self), "don't know how to cast to bool...");
1561 left = ir_block_create_unary(func->curblock,
1562 ast_function_label(func, "sce_not"),
1566 from_left = func->curblock;
1568 other = ir_function_create_block(func->ir_func, ast_function_label(func, "sce_other"));
1569 if ( !(self->op == INSTR_OR) != !OPTS_FLAG(PERL_LOGIC) ) {
1570 if (!ir_block_create_if(func->curblock, left, other, merge))
1573 if (!ir_block_create_if(func->curblock, left, merge, other))
1576 /* use the likely flag */
1577 vec_last(func->curblock->instr)->likely = true;
1579 func->curblock = other;
1580 cgen = self->right->expression.codegen;
1581 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1583 if (!OPTS_FLAG(PERL_LOGIC)) {
1584 notop = type_not_instr[right->vtype];
1585 if (notop == AINSTR_END) {
1586 asterror(ast_ctx(self), "don't know how to cast to bool...");
1589 right = ir_block_create_unary(func->curblock,
1590 ast_function_label(func, "sce_not"),
1594 from_right = func->curblock;
1596 if (!ir_block_create_jump(func->curblock, merge))
1599 vec_remove(func->ir_func->blocks, merge_id, 1);
1600 vec_push(func->ir_func->blocks, merge);
1602 func->curblock = merge;
1603 phi = ir_block_create_phi(func->curblock, ast_function_label(func, "sce_value"), TYPE_FLOAT);
1604 ir_phi_add(phi, from_left, left);
1605 ir_phi_add(phi, from_right, right);
1606 *out = ir_phi_value(phi);
1607 if (!OPTS_FLAG(PERL_LOGIC)) {
1608 notop = type_not_instr[(*out)->vtype];
1609 if (notop == AINSTR_END) {
1610 asterror(ast_ctx(self), "don't know how to cast to bool...");
1613 *out = ir_block_create_unary(func->curblock,
1614 ast_function_label(func, "sce_final_not"),
1620 self->expression.outr = *out;
1624 cgen = self->left->expression.codegen;
1625 if (!(*cgen)((ast_expression*)(self->left), func, false, &left))
1628 cgen = self->right->expression.codegen;
1629 if (!(*cgen)((ast_expression*)(self->right), func, false, &right))
1632 *out = ir_block_create_binop(func->curblock, ast_function_label(func, "bin"),
1633 self->op, left, right);
1636 self->expression.outr = *out;
1641 bool ast_binstore_codegen(ast_binstore *self, ast_function *func, bool lvalue, ir_value **out)
1643 ast_expression_codegen *cgen;
1644 ir_value *leftl = NULL, *leftr, *right, *bin;
1648 ast_array_index *ai = NULL;
1649 ir_value *iridx = NULL;
1651 if (lvalue && self->expression.outl) {
1652 *out = self->expression.outl;
1656 if (!lvalue && self->expression.outr) {
1657 *out = self->expression.outr;
1661 if (ast_istype(self->dest, ast_array_index))
1664 ai = (ast_array_index*)self->dest;
1665 idx = (ast_value*)ai->index;
1667 if (ast_istype(ai->index, ast_value) && idx->isconst)
1671 /* for a binstore we need both an lvalue and an rvalue for the left side */
1672 /* rvalue of destination! */
1674 cgen = idx->expression.codegen;
1675 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1678 cgen = self->dest->expression.codegen;
1679 if (!(*cgen)((ast_expression*)(self->dest), func, false, &leftr))
1682 /* source as rvalue only */
1683 cgen = self->source->expression.codegen;
1684 if (!(*cgen)((ast_expression*)(self->source), func, false, &right))
1687 /* now the binary */
1688 bin = ir_block_create_binop(func->curblock, ast_function_label(func, "binst"),
1689 self->opbin, leftr, right);
1690 self->expression.outr = bin;
1694 /* we need to call the setter */
1699 asterror(ast_ctx(self), "array-subscript assignment cannot produce lvalues");
1703 arr = (ast_value*)ai->array;
1704 if (!ast_istype(ai->array, ast_value) || !arr->setter) {
1705 asterror(ast_ctx(self), "value has no setter (%s)", arr->name);
1709 cgen = arr->setter->expression.codegen;
1710 if (!(*cgen)((ast_expression*)(arr->setter), func, true, &funval))
1713 call = ir_block_create_call(func->curblock, ast_function_label(func, "store"), funval);
1716 ir_call_param(call, iridx);
1717 ir_call_param(call, bin);
1718 self->expression.outr = bin;
1720 /* now store them */
1721 cgen = self->dest->expression.codegen;
1722 /* lvalue of destination */
1723 if (!(*cgen)((ast_expression*)(self->dest), func, true, &leftl))
1725 self->expression.outl = leftl;
1727 if (!ir_block_create_store_op(func->curblock, self->opstore, leftl, bin))
1729 self->expression.outr = bin;
1732 /* Theoretically, an assinment returns its left side as an
1733 * lvalue, if we don't need an lvalue though, we return
1734 * the right side as an rvalue, otherwise we have to
1735 * somehow know whether or not we need to dereference the pointer
1736 * on the left side - that is: OP_LOAD if it was an address.
1737 * Also: in original QC we cannot OP_LOADP *anyway*.
1739 *out = (lvalue ? leftl : bin);
1744 bool ast_unary_codegen(ast_unary *self, ast_function *func, bool lvalue, ir_value **out)
1746 ast_expression_codegen *cgen;
1749 /* An unary operation cannot yield an l-value */
1751 asterror(ast_ctx(self), "not an l-value (binop)");
1755 if (self->expression.outr) {
1756 *out = self->expression.outr;
1760 cgen = self->operand->expression.codegen;
1762 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
1765 *out = ir_block_create_unary(func->curblock, ast_function_label(func, "unary"),
1769 self->expression.outr = *out;
1774 bool ast_return_codegen(ast_return *self, ast_function *func, bool lvalue, ir_value **out)
1776 ast_expression_codegen *cgen;
1781 /* In the context of a return operation, we don't actually return
1785 asterror(ast_ctx(self), "return-expression is not an l-value");
1789 if (self->expression.outr) {
1790 asterror(ast_ctx(self), "internal error: ast_return cannot be reused, it bears no result!");
1793 self->expression.outr = (ir_value*)1;
1795 if (self->operand) {
1796 cgen = self->operand->expression.codegen;
1798 if (!(*cgen)((ast_expression*)(self->operand), func, false, &operand))
1801 if (!ir_block_create_return(func->curblock, operand))
1804 if (!ir_block_create_return(func->curblock, NULL))
1811 bool ast_entfield_codegen(ast_entfield *self, ast_function *func, bool lvalue, ir_value **out)
1813 ast_expression_codegen *cgen;
1814 ir_value *ent, *field;
1816 /* This function needs to take the 'lvalue' flag into account!
1817 * As lvalue we provide a field-pointer, as rvalue we provide the
1821 if (lvalue && self->expression.outl) {
1822 *out = self->expression.outl;
1826 if (!lvalue && self->expression.outr) {
1827 *out = self->expression.outr;
1831 cgen = self->entity->expression.codegen;
1832 if (!(*cgen)((ast_expression*)(self->entity), func, false, &ent))
1835 cgen = self->field->expression.codegen;
1836 if (!(*cgen)((ast_expression*)(self->field), func, false, &field))
1841 *out = ir_block_create_fieldaddress(func->curblock, ast_function_label(func, "efa"),
1844 *out = ir_block_create_load_from_ent(func->curblock, ast_function_label(func, "efv"),
1845 ent, field, self->expression.vtype);
1848 asterror(ast_ctx(self), "failed to create %s instruction (output type %s)",
1849 (lvalue ? "ADDRESS" : "FIELD"),
1850 type_name[self->expression.vtype]);
1855 self->expression.outl = *out;
1857 self->expression.outr = *out;
1859 /* Hm that should be it... */
1863 bool ast_member_codegen(ast_member *self, ast_function *func, bool lvalue, ir_value **out)
1865 ast_expression_codegen *cgen;
1868 /* in QC this is always an lvalue */
1870 if (self->expression.outl) {
1871 *out = self->expression.outl;
1875 cgen = self->owner->expression.codegen;
1876 if (!(*cgen)((ast_expression*)(self->owner), func, true, &vec))
1879 if (vec->vtype != TYPE_VECTOR &&
1880 !(vec->vtype == TYPE_FIELD && self->owner->expression.next->expression.vtype == TYPE_VECTOR))
1885 *out = ir_value_vector_member(vec, self->field);
1886 self->expression.outl = *out;
1888 return (*out != NULL);
1891 bool ast_array_index_codegen(ast_array_index *self, ast_function *func, bool lvalue, ir_value **out)
1896 if (!lvalue && self->expression.outr) {
1897 *out = self->expression.outr;
1899 if (lvalue && self->expression.outl) {
1900 *out = self->expression.outl;
1903 if (!ast_istype(self->array, ast_value)) {
1904 asterror(ast_ctx(self), "array indexing this way is not supported");
1905 /* note this would actually be pointer indexing because the left side is
1906 * not an actual array but (hopefully) an indexable expression.
1907 * Once we get integer arithmetic, and GADDRESS/GSTORE/GLOAD instruction
1908 * support this path will be filled.
1913 arr = (ast_value*)self->array;
1914 idx = (ast_value*)self->index;
1916 if (!ast_istype(self->index, ast_value) || !idx->isconst) {
1917 /* Time to use accessor functions */
1918 ast_expression_codegen *cgen;
1919 ir_value *iridx, *funval;
1923 asterror(ast_ctx(self), "(.2) array indexing here needs a compile-time constant");
1928 asterror(ast_ctx(self), "value has no getter, don't know how to index it");
1932 cgen = self->index->expression.codegen;
1933 if (!(*cgen)((ast_expression*)(self->index), func, true, &iridx))
1936 cgen = arr->getter->expression.codegen;
1937 if (!(*cgen)((ast_expression*)(arr->getter), func, true, &funval))
1940 call = ir_block_create_call(func->curblock, ast_function_label(func, "fetch"), funval);
1943 ir_call_param(call, iridx);
1945 *out = ir_call_value(call);
1946 self->expression.outr = *out;
1950 if (idx->expression.vtype == TYPE_FLOAT)
1951 *out = arr->ir_values[(int)idx->constval.vfloat];
1952 else if (idx->expression.vtype == TYPE_INTEGER)
1953 *out = arr->ir_values[idx->constval.vint];
1955 asterror(ast_ctx(self), "array indexing here needs an integer constant");
1961 bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_value **out)
1963 ast_expression_codegen *cgen;
1968 ir_block *cond = func->curblock;
1971 ir_block *ontrue_endblock = NULL;
1972 ir_block *onfalse_endblock = NULL;
1975 /* We don't output any value, thus also don't care about r/lvalue */
1979 if (self->expression.outr) {
1980 asterror(ast_ctx(self), "internal error: ast_ifthen cannot be reused, it bears no result!");
1983 self->expression.outr = (ir_value*)1;
1985 /* generate the condition */
1986 cgen = self->cond->expression.codegen;
1987 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
1989 /* update the block which will get the jump - because short-logic or ternaries may have changed this */
1990 cond = func->curblock;
1994 if (self->on_true) {
1995 /* create on-true block */
1996 ontrue = ir_function_create_block(func->ir_func, ast_function_label(func, "ontrue"));
2000 /* enter the block */
2001 func->curblock = ontrue;
2004 cgen = self->on_true->expression.codegen;
2005 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &dummy))
2008 /* we now need to work from the current endpoint */
2009 ontrue_endblock = func->curblock;
2014 if (self->on_false) {
2015 /* create on-false block */
2016 onfalse = ir_function_create_block(func->ir_func, ast_function_label(func, "onfalse"));
2020 /* enter the block */
2021 func->curblock = onfalse;
2024 cgen = self->on_false->expression.codegen;
2025 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &dummy))
2028 /* we now need to work from the current endpoint */
2029 onfalse_endblock = func->curblock;
2033 /* Merge block were they all merge in to */
2034 merge = ir_function_create_block(func->ir_func, ast_function_label(func, "endif"));
2037 /* add jumps ot the merge block */
2038 if (ontrue && !ontrue_endblock->final && !ir_block_create_jump(ontrue_endblock, merge))
2040 if (onfalse && !onfalse_endblock->final && !ir_block_create_jump(onfalse_endblock, merge))
2043 /* we create the if here, that way all blocks are ordered :)
2045 if (!ir_block_create_if(cond, condval,
2046 (ontrue ? ontrue : merge),
2047 (onfalse ? onfalse : merge)))
2052 /* Now enter the merge block */
2053 func->curblock = merge;
2058 bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_value **out)
2060 ast_expression_codegen *cgen;
2063 ir_value *trueval, *falseval;
2066 ir_block *cond = func->curblock;
2071 /* Ternary can never create an lvalue... */
2075 /* In theory it shouldn't be possible to pass through a node twice, but
2076 * in case we add any kind of optimization pass for the AST itself, it
2077 * may still happen, thus we remember a created ir_value and simply return one
2078 * if it already exists.
2080 if (self->expression.outr) {
2081 *out = self->expression.outr;
2085 /* In the following, contraty to ast_ifthen, we assume both paths exist. */
2087 /* generate the condition */
2088 func->curblock = cond;
2089 cgen = self->cond->expression.codegen;
2090 if (!(*cgen)((ast_expression*)(self->cond), func, false, &condval))
2093 /* create on-true block */
2094 ontrue = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_T"));
2099 /* enter the block */
2100 func->curblock = ontrue;
2103 cgen = self->on_true->expression.codegen;
2104 if (!(*cgen)((ast_expression*)(self->on_true), func, false, &trueval))
2108 /* create on-false block */
2109 onfalse = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_F"));
2114 /* enter the block */
2115 func->curblock = onfalse;
2118 cgen = self->on_false->expression.codegen;
2119 if (!(*cgen)((ast_expression*)(self->on_false), func, false, &falseval))
2123 /* create merge block */
2124 merge = ir_function_create_block(func->ir_func, ast_function_label(func, "tern_out"));
2127 /* jump to merge block */
2128 if (!ir_block_create_jump(ontrue, merge))
2130 if (!ir_block_create_jump(onfalse, merge))
2133 /* create if instruction */
2134 if (!ir_block_create_if(cond, condval, ontrue, onfalse))
2137 /* Now enter the merge block */
2138 func->curblock = merge;
2140 /* Here, now, we need a PHI node
2141 * but first some sanity checking...
2143 if (trueval->vtype != falseval->vtype) {
2144 /* error("ternary with different types on the two sides"); */
2149 phi = ir_block_create_phi(merge, ast_function_label(func, "phi"), trueval->vtype);
2152 ir_phi_add(phi, ontrue, trueval);
2153 ir_phi_add(phi, onfalse, falseval);
2155 self->expression.outr = ir_phi_value(phi);
2156 *out = self->expression.outr;
2161 bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value **out)
2163 ast_expression_codegen *cgen;
2165 ir_value *dummy = NULL;
2166 ir_value *precond = NULL;
2167 ir_value *postcond = NULL;
2169 /* Since we insert some jumps "late" so we have blocks
2170 * ordered "nicely", we need to keep track of the actual end-blocks
2171 * of expressions to add the jumps to.
2173 ir_block *bbody = NULL, *end_bbody = NULL;
2174 ir_block *bprecond = NULL, *end_bprecond = NULL;
2175 ir_block *bpostcond = NULL, *end_bpostcond = NULL;
2176 ir_block *bincrement = NULL, *end_bincrement = NULL;
2177 ir_block *bout = NULL, *bin = NULL;
2179 /* let's at least move the outgoing block to the end */
2182 /* 'break' and 'continue' need to be able to find the right blocks */
2183 ir_block *bcontinue = NULL;
2184 ir_block *bbreak = NULL;
2186 ir_block *old_bcontinue = NULL;
2187 ir_block *old_bbreak = NULL;
2189 ir_block *tmpblock = NULL;
2194 if (self->expression.outr) {
2195 asterror(ast_ctx(self), "internal error: ast_loop cannot be reused, it bears no result!");
2198 self->expression.outr = (ir_value*)1;
2201 * Should we ever need some kind of block ordering, better make this function
2202 * move blocks around than write a block ordering algorithm later... after all
2203 * the ast and ir should work together, not against each other.
2206 /* initexpr doesn't get its own block, it's pointless, it could create more blocks
2207 * anyway if for example it contains a ternary.
2211 cgen = self->initexpr->expression.codegen;
2212 if (!(*cgen)((ast_expression*)(self->initexpr), func, false, &dummy))
2216 /* Store the block from which we enter this chaos */
2217 bin = func->curblock;
2219 /* The pre-loop condition needs its own block since we
2220 * need to be able to jump to the start of that expression.
2224 bprecond = ir_function_create_block(func->ir_func, ast_function_label(func, "pre_loop_cond"));
2228 /* the pre-loop-condition the least important place to 'continue' at */
2229 bcontinue = bprecond;
2232 func->curblock = bprecond;
2235 cgen = self->precond->expression.codegen;
2236 if (!(*cgen)((ast_expression*)(self->precond), func, false, &precond))
2239 end_bprecond = func->curblock;
2241 bprecond = end_bprecond = NULL;
2244 /* Now the next blocks won't be ordered nicely, but we need to
2245 * generate them this early for 'break' and 'continue'.
2247 if (self->increment) {
2248 bincrement = ir_function_create_block(func->ir_func, ast_function_label(func, "loop_increment"));
2251 bcontinue = bincrement; /* increment comes before the pre-loop-condition */
2253 bincrement = end_bincrement = NULL;
2256 if (self->postcond) {
2257 bpostcond = ir_function_create_block(func->ir_func, ast_function_label(func, "post_loop_cond"));
2260 bcontinue = bpostcond; /* postcond comes before the increment */
2262 bpostcond = end_bpostcond = NULL;
2265 bout_id = vec_size(func->ir_func->blocks);
2266 bout = ir_function_create_block(func->ir_func, ast_function_label(func, "after_loop"));
2271 /* The loop body... */
2274 bbody = ir_function_create_block(func->ir_func, ast_function_label(func, "loop_body"));
2279 func->curblock = bbody;
2281 old_bbreak = func->breakblock;
2282 old_bcontinue = func->continueblock;
2283 func->breakblock = bbreak;
2284 func->continueblock = bcontinue;
2287 cgen = self->body->expression.codegen;
2288 if (!(*cgen)((ast_expression*)(self->body), func, false, &dummy))
2291 end_bbody = func->curblock;
2292 func->breakblock = old_bbreak;
2293 func->continueblock = old_bcontinue;
2296 /* post-loop-condition */
2300 func->curblock = bpostcond;
2303 cgen = self->postcond->expression.codegen;
2304 if (!(*cgen)((ast_expression*)(self->postcond), func, false, &postcond))
2307 end_bpostcond = func->curblock;
2310 /* The incrementor */
2311 if (self->increment)
2314 func->curblock = bincrement;
2317 cgen = self->increment->expression.codegen;
2318 if (!(*cgen)((ast_expression*)(self->increment), func, false, &dummy))
2321 end_bincrement = func->curblock;
2324 /* In any case now, we continue from the outgoing block */
2325 func->curblock = bout;
2327 /* Now all blocks are in place */
2328 /* From 'bin' we jump to whatever comes first */
2329 if (bprecond) tmpblock = bprecond;
2330 else if (bbody) tmpblock = bbody;
2331 else if (bpostcond) tmpblock = bpostcond;
2332 else tmpblock = bout;
2333 if (!ir_block_create_jump(bin, tmpblock))
2339 ir_block *ontrue, *onfalse;
2340 if (bbody) ontrue = bbody;
2341 else if (bincrement) ontrue = bincrement;
2342 else if (bpostcond) ontrue = bpostcond;
2343 else ontrue = bprecond;
2345 if (!ir_block_create_if(end_bprecond, precond, ontrue, onfalse))
2352 if (bincrement) tmpblock = bincrement;
2353 else if (bpostcond) tmpblock = bpostcond;
2354 else if (bprecond) tmpblock = bprecond;
2355 else tmpblock = bout;
2356 if (!end_bbody->final && !ir_block_create_jump(end_bbody, tmpblock))
2360 /* from increment */
2363 if (bpostcond) tmpblock = bpostcond;
2364 else if (bprecond) tmpblock = bprecond;
2365 else if (bbody) tmpblock = bbody;
2366 else tmpblock = bout;
2367 if (!ir_block_create_jump(end_bincrement, tmpblock))
2374 ir_block *ontrue, *onfalse;
2375 if (bprecond) ontrue = bprecond;
2376 else if (bbody) ontrue = bbody;
2377 else if (bincrement) ontrue = bincrement;
2378 else ontrue = bpostcond;
2380 if (!ir_block_create_if(end_bpostcond, postcond, ontrue, onfalse))
2384 /* Move 'bout' to the end */
2385 vec_remove(func->ir_func->blocks, bout_id, 1);
2386 vec_push(func->ir_func->blocks, bout);
2391 bool ast_breakcont_codegen(ast_breakcont *self, ast_function *func, bool lvalue, ir_value **out)
2398 asterror(ast_ctx(self), "break/continue expression is not an l-value");
2402 if (self->expression.outr) {
2403 asterror(ast_ctx(self), "internal error: ast_breakcont cannot be reused!");
2406 self->expression.outr = (ir_value*)1;
2408 if (self->is_continue)
2409 target = func->continueblock;
2411 target = func->breakblock;
2413 if (!ir_block_create_jump(func->curblock, target))
2418 bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_value **out)
2420 ast_expression_codegen *cgen;
2422 ast_switch_case *def_case = NULL;
2423 ir_block *def_bfall = NULL;
2425 ir_value *dummy = NULL;
2426 ir_value *irop = NULL;
2427 ir_block *old_break = NULL;
2428 ir_block *bout = NULL;
2429 ir_block *bfall = NULL;
2437 asterror(ast_ctx(self), "switch expression is not an l-value");
2441 if (self->expression.outr) {
2442 asterror(ast_ctx(self), "internal error: ast_switch cannot be reused!");
2445 self->expression.outr = (ir_value*)1;
2450 cgen = self->operand->expression.codegen;
2451 if (!(*cgen)((ast_expression*)(self->operand), func, false, &irop))
2454 if (!vec_size(self->cases))
2457 cmpinstr = type_eq_instr[irop->vtype];
2458 if (cmpinstr >= AINSTR_END) {
2459 ast_type_to_string(self->operand, typestr, sizeof(typestr));
2460 asterror(ast_ctx(self), "invalid type to perform a switch on: %s", typestr);
2464 bout_id = vec_size(func->ir_func->blocks);
2465 bout = ir_function_create_block(func->ir_func, ast_function_label(func, "after_switch"));
2469 /* setup the break block */
2470 old_break = func->breakblock;
2471 func->breakblock = bout;
2473 /* Now create all cases */
2474 for (c = 0; c < vec_size(self->cases); ++c) {
2475 ir_value *cond, *val;
2476 ir_block *bcase, *bnot;
2479 ast_switch_case *swcase = &self->cases[c];
2481 if (swcase->value) {
2482 /* A regular case */
2483 /* generate the condition operand */
2484 cgen = swcase->value->expression.codegen;
2485 if (!(*cgen)((ast_expression*)(swcase->value), func, false, &val))
2487 /* generate the condition */
2488 cond = ir_block_create_binop(func->curblock, ast_function_label(func, "switch_eq"), cmpinstr, irop, val);
2492 bcase = ir_function_create_block(func->ir_func, ast_function_label(func, "case"));
2493 bnot_id = vec_size(func->ir_func->blocks);
2494 bnot = ir_function_create_block(func->ir_func, ast_function_label(func, "not_case"));
2495 if (!bcase || !bnot)
2497 if (!ir_block_create_if(func->curblock, cond, bcase, bnot))
2500 /* Make the previous case-end fall through */
2501 if (bfall && !bfall->final) {
2502 if (!ir_block_create_jump(bfall, bcase))
2506 /* enter the case */
2507 func->curblock = bcase;
2508 cgen = swcase->code->expression.codegen;
2509 if (!(*cgen)((ast_expression*)swcase->code, func, false, &dummy))
2512 /* remember this block to fall through from */
2513 bfall = func->curblock;
2515 /* enter the else and move it down */
2516 func->curblock = bnot;
2517 vec_remove(func->ir_func->blocks, bnot_id, 1);
2518 vec_push(func->ir_func->blocks, bnot);
2520 /* The default case */
2521 /* Remember where to fall through from: */
2524 /* remember which case it was */
2529 /* Jump from the last bnot to bout */
2530 if (bfall && !bfall->final && !ir_block_create_jump(bfall, bout)) {
2532 astwarning(ast_ctx(bfall), WARN_???, "missing break after last case");
2537 /* If there was a default case, put it down here */
2541 /* No need to create an extra block */
2542 bcase = func->curblock;
2544 /* Insert the fallthrough jump */
2545 if (def_bfall && !def_bfall->final) {
2546 if (!ir_block_create_jump(def_bfall, bcase))
2550 /* Now generate the default code */
2551 cgen = def_case->code->expression.codegen;
2552 if (!(*cgen)((ast_expression*)def_case->code, func, false, &dummy))
2556 /* Jump from the last bnot to bout */
2557 if (!func->curblock->final && !ir_block_create_jump(func->curblock, bout))
2559 /* enter the outgoing block */
2560 func->curblock = bout;
2562 /* restore the break block */
2563 func->breakblock = old_break;
2565 /* Move 'bout' to the end, it's nicer */
2566 vec_remove(func->ir_func->blocks, bout_id, 1);
2567 vec_push(func->ir_func->blocks, bout);
2572 bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value **out)
2574 ast_expression_codegen *cgen;
2576 ir_instr *callinstr;
2579 ir_value *funval = NULL;
2581 /* return values are never lvalues */
2583 asterror(ast_ctx(self), "not an l-value (function call)");
2587 if (self->expression.outr) {
2588 *out = self->expression.outr;
2592 cgen = self->func->expression.codegen;
2593 if (!(*cgen)((ast_expression*)(self->func), func, false, &funval))
2601 for (i = 0; i < vec_size(self->params); ++i)
2604 ast_expression *expr = self->params[i];
2606 cgen = expr->expression.codegen;
2607 if (!(*cgen)(expr, func, false, ¶m))
2611 vec_push(params, param);
2614 callinstr = ir_block_create_call(func->curblock, ast_function_label(func, "call"), funval);
2618 for (i = 0; i < vec_size(params); ++i) {
2619 ir_call_param(callinstr, params[i]);
2622 *out = ir_call_value(callinstr);
2623 self->expression.outr = *out;