13 #define ast_instantiate(T, ctx, destroyfn) \
15 if (!self) return nullptr; \
16 ast_node_init(self, ctx, TYPE_##T); \
17 self->m_destroy = (ast_node_delete*)destroyfn
20 * forward declarations, these need not be in ast.h for obvious
23 static bool ast_member_codegen(ast_member*, ast_function*, bool lvalue, ir_value**);
24 static void ast_array_index_delete(ast_array_index*);
25 static bool ast_array_index_codegen(ast_array_index*, ast_function*, bool lvalue, ir_value**);
26 static void ast_argpipe_delete(ast_argpipe*);
27 static bool ast_argpipe_codegen(ast_argpipe*, ast_function*, bool lvalue, ir_value**);
28 static void ast_store_delete(ast_store*);
29 static bool ast_store_codegen(ast_store*, ast_function*, bool lvalue, ir_value**);
30 static void ast_ifthen_delete(ast_ifthen*);
31 static bool ast_ifthen_codegen(ast_ifthen*, ast_function*, bool lvalue, ir_value**);
32 static void ast_ternary_delete(ast_ternary*);
33 static bool ast_ternary_codegen(ast_ternary*, ast_function*, bool lvalue, ir_value**);
34 static void ast_loop_delete(ast_loop*);
35 static bool ast_loop_codegen(ast_loop*, ast_function*, bool lvalue, ir_value**);
36 static void ast_breakcont_delete(ast_breakcont*);
37 static bool ast_breakcont_codegen(ast_breakcont*, ast_function*, bool lvalue, ir_value**);
38 static void ast_switch_delete(ast_switch*);
39 static bool ast_switch_codegen(ast_switch*, ast_function*, bool lvalue, ir_value**);
40 static void ast_label_delete(ast_label*);
41 static void ast_label_register_goto(ast_label*, ast_goto*);
42 static bool ast_label_codegen(ast_label*, ast_function*, bool lvalue, ir_value**);
43 static bool ast_goto_codegen(ast_goto*, ast_function*, bool lvalue, ir_value**);
44 static void ast_goto_delete(ast_goto*);
45 static void ast_call_delete(ast_call*);
46 static bool ast_call_codegen(ast_call*, ast_function*, bool lvalue, ir_value**);
47 static bool ast_block_codegen(ast_block*, ast_function*, bool lvalue, ir_value**);
48 static void ast_unary_delete(ast_unary*);
49 static bool ast_unary_codegen(ast_unary*, ast_function*, bool lvalue, ir_value**);
50 static void ast_entfield_delete(ast_entfield*);
51 static bool ast_entfield_codegen(ast_entfield*, ast_function*, bool lvalue, ir_value**);
52 static void ast_return_delete(ast_return*);
53 static bool ast_return_codegen(ast_return*, ast_function*, bool lvalue, ir_value**);
54 static void ast_binstore_delete(ast_binstore*);
55 static bool ast_binstore_codegen(ast_binstore*, ast_function*, bool lvalue, ir_value**);
56 static void ast_binary_delete(ast_binary*);
57 static bool ast_binary_codegen(ast_binary*, ast_function*, bool lvalue, ir_value**);
58 static bool ast_state_codegen(ast_state*, ast_function*, bool lvalue, ir_value**);
60 /* Initialize main ast node aprts */
61 ast_node::ast_node(lex_ctx_t ctx, int node_type)
63 m_node_type(node_type),
73 /* weight and side effects */
74 void ast_node::propagate_side_effects(ast_node *other) const
76 other->m_side_effects = m_side_effects;
79 /* General expression initialization */
80 ast_expression::ast_expression(lex_ctx_t ctx, int nodetype, qc_type type)
81 : ast_node(ctx, nodetype),
84 if (OPTS_OPTION_BOOL(OPTION_COVERAGE))
85 m_flags |= AST_FLAG_BLOCK_COVERAGE;
87 ast_expression::ast_expression(lex_ctx_t ctx, int nodetype)
88 : ast_expression(ctx, nodetype, TYPE_VOID)
91 ast_expression::~ast_expression()
99 ast_expression::ast_expression(ast_copy_type_t, int nodetype, const ast_expression &other)
100 : ast_expression(other.m_context, nodetype)
102 m_vtype = other.m_vtype;
103 m_count = other.m_count;
104 m_flags = other.m_flags;
106 m_next = new ast_expression(ast_copy_type, TYPE_ast_expression, *other.m_next);
107 m_type_params.reserve(other.m_type_params.size());
108 for (auto &it : other.m_type_params)
109 m_type_params.emplace_back(new ast_value(ast_copy_type, *it));
112 ast_expression::ast_expression(ast_copy_type_t, const ast_expression &other)
113 : ast_expression(other.m_context, TYPE_ast_expression)
116 ast_expression *ast_expression::shallow_type(lex_ctx_t ctx, qc_type vtype) {
117 auto expr = new ast_expression(ctx, TYPE_ast_expression);
118 expr->m_vtype = vtype;
122 void ast_expression::adopt_type(const ast_expression &other)
124 m_vtype = other.m_vtype;
126 m_next = new ast_expression(ast_copy_type, TYPE_ast_expression, *other.m_next);
127 m_count = other.m_count;
128 m_flags = other.m_flags;
129 m_type_params.clear();
130 m_type_params.reserve(other.m_type_params.size());
131 for (auto &it : other.m_type_params)
132 m_type_params.emplace_back(new ast_value(ast_copy_type, *it));
135 bool ast_expression::compare_type(const ast_expression &other) const
137 if (m_vtype == TYPE_NIL ||
138 other.m_vtype == TYPE_NIL)
140 if (m_vtype != other.m_vtype)
142 if (!m_next != !other.m_next)
144 if (m_type_params.size() != other.m_type_params.size())
146 if ((m_flags & AST_FLAG_TYPE_MASK) !=
147 (other.m_flags & AST_FLAG_TYPE_MASK) )
151 if (m_type_params.size()) {
153 for (i = 0; i < m_type_params.size(); ++i) {
154 if (!m_type_params[i]->compare_type(*other.m_type_params[i]))
159 return m_next->compare_type(*other.m_next);
163 ast_value::ast_value(ast_copy_type_t, const ast_value &other, const std::string &name)
164 : ast_value(ast_copy_type, static_cast<const ast_expression&>(other), name)
167 ast_value::ast_value(ast_copy_type_t, const ast_value &other)
168 : ast_value(ast_copy_type, static_cast<const ast_expression&>(other), other.m_name)
171 ast_value::ast_value(ast_copy_type_t, const ast_expression &other, const std::string &name)
172 : ast_expression(ast_copy_type, other),
176 ast_value::ast_value(lex_ctx_t ctx, const std::string &name, qc_type t)
177 : ast_expression(ctx, TYPE_ast_value, t),
180 m_keep_node = true; // keep values, always
181 memset(&m_constval, 0, sizeof(m_constval));
184 ast_value::~ast_value()
187 mem_d((void*)m_argcounter);
192 mem_d((void*)m_constval.vstring);
195 // unlink us from the function node
196 m_constval.vfunc->m_function_type = nullptr;
198 // NOTE: delete function? currently collected in
199 // the parser structure
207 // initlist imples an array which implies .next in the expression exists.
208 if (m_initlist.size() && m_next->m_vtype == TYPE_STRING) {
209 for (auto &it : m_initlist)
215 static size_t ast_type_to_string_impl(const ast_expression *e, char *buf, size_t bufsize, size_t pos)
222 if (pos + 6 >= bufsize)
224 util_strncpy(buf + pos, "(null)", 6);
228 if (pos + 1 >= bufsize)
231 switch (e->m_vtype) {
233 util_strncpy(buf + pos, "(variant)", 9);
238 return ast_type_to_string_impl(e->m_next, buf, bufsize, pos);
241 if (pos + 3 >= bufsize)
245 pos = ast_type_to_string_impl(e->m_next, buf, bufsize, pos);
246 if (pos + 1 >= bufsize)
252 pos = ast_type_to_string_impl(e->m_next, buf, bufsize, pos);
253 if (pos + 2 >= bufsize)
255 if (e->m_type_params.empty()) {
261 pos = ast_type_to_string_impl(e->m_type_params[0].get(), buf, bufsize, pos);
262 for (i = 1; i < e->m_type_params.size(); ++i) {
263 if (pos + 2 >= bufsize)
267 pos = ast_type_to_string_impl(e->m_type_params[i].get(), buf, bufsize, pos);
269 if (pos + 1 >= bufsize)
275 pos = ast_type_to_string_impl(e->m_next, buf, bufsize, pos);
276 if (pos + 1 >= bufsize)
279 pos += util_snprintf(buf + pos, bufsize - pos - 1, "%i", (int)e->m_count);
280 if (pos + 1 >= bufsize)
286 typestr = type_name[e->m_vtype];
287 typelen = strlen(typestr);
288 if (pos + typelen >= bufsize)
290 util_strncpy(buf + pos, typestr, typelen);
291 return pos + typelen;
295 buf[bufsize-3] = '.';
296 buf[bufsize-2] = '.';
297 buf[bufsize-1] = '.';
301 void ast_type_to_string(const ast_expression *e, char *buf, size_t bufsize)
303 size_t pos = ast_type_to_string_impl(e, buf, bufsize-1, 0);
307 void ast_value::add_param(ast_value *p)
309 m_type_params.emplace_back(p);
312 ast_binary::ast_binary(lex_ctx_t ctx, int op,
313 ast_expression* left, ast_expression* right)
314 : ast_expression(ctx, TYPE_ast_binary),
316 // m_left/m_right happen after the peephole step right below
319 if (ast_istype(right, ast_unary) && OPTS_OPTIMIZATION(OPTIM_PEEPHOLE)) {
320 ast_unary *unary = ((ast_unary*)right);
321 ast_expression *normal = unary->m_operand;
323 /* make a-(-b) => a + b */
324 if (unary->m_op == VINSTR_NEG_F || unary->m_op == VINSTR_NEG_V) {
325 if (op == INSTR_SUB_F) {
328 ++opts_optimizationcount[OPTIM_PEEPHOLE];
329 } else if (op == INSTR_SUB_V) {
332 ++opts_optimizationcount[OPTIM_PEEPHOLE];
340 propagate_side_effects(left);
341 propagate_side_effects(right);
343 if (op >= INSTR_EQ_F && op <= INSTR_GT)
344 m_vtype = TYPE_FLOAT;
345 else if (op == INSTR_AND || op == INSTR_OR) {
346 if (OPTS_FLAG(PERL_LOGIC))
349 m_vtype = TYPE_FLOAT;
351 else if (op == INSTR_BITAND || op == INSTR_BITOR)
352 m_vtype = TYPE_FLOAT;
353 else if (op == INSTR_MUL_VF || op == INSTR_MUL_FV)
354 m_vtype = TYPE_VECTOR;
355 else if (op == INSTR_MUL_V)
356 m_vtype = TYPE_FLOAT;
358 m_vtype = left->m_vtype;
361 m_refs = AST_REF_ALL;
364 ast_binary::~ast_binary()
366 if (m_refs & AST_REF_LEFT) ast_unref(m_left);
367 if (m_refs & AST_REF_RIGHT) ast_unref(m_right);
370 ast_binstore::ast_binstore(lex_ctx_t ctx, int storop, int mathop,
371 ast_expression* left, ast_expression* right)
372 : ast_expression(ctx, TYPE_ast_binstore),
379 m_side_effects = true;
383 ast_binstore::~ast_binstore()
390 ast_unary* ast_unary::make(lex_ctx_t ctx, int op, ast_expression *expr)
392 if (ast_istype(expr, ast_unary) && OPTS_OPTIMIZATION(OPTIM_PEEPHOLE)) {
393 ast_unary *prev = (ast_unary*)((ast_unary*)expr)->m_operand;
395 /* Handle for double negation */
396 if (((ast_unary*)expr)->m_op == op)
397 prev = (ast_unary*)((ast_unary*)expr)->m_operand;
399 if (ast_istype(prev, ast_unary)) {
400 ++opts_optimizationcount[OPTIM_PEEPHOLE];
405 return new ast_unary(ctx, op, expr);
408 ast_unary::ast_unary(lex_ctx_t ctx, int op, ast_expression *expr)
409 : ast_expression(ctx, TYPE_ast_unary),
413 propagate_side_effects(expr);
414 if ((op >= INSTR_NOT_F && op <= INSTR_NOT_FNC) || op == VINSTR_NEG_F) {
415 m_vtype = TYPE_FLOAT;
416 } else if (op == VINSTR_NEG_V) {
417 m_vtype = TYPE_VECTOR;
419 compile_error(ctx, "cannot determine type of unary operation %s", util_instr_str[op]);
423 ast_unary::~ast_unary()
426 ast_unref(m_operand);
429 ast_return::ast_return(lex_ctx_t ctx, ast_expression *expr)
430 : ast_expression(ctx, TYPE_ast_return),
434 propagate_side_effects(expr);
437 ast_return::~ast_return()
440 ast_unref(m_operand);
443 ast_entfield::ast_entfield(lex_ctx_t ctx, ast_expression *entity, ast_expression *field)
444 : ast_entfield(ctx, entity, field, field->m_next)
446 if (field->m_vtype != TYPE_FIELD)
447 compile_error(ctx, "ast_entfield with expression not of type field");
450 ast_entfield::ast_entfield(lex_ctx_t ctx, ast_expression *entity, ast_expression *field, const ast_expression *outtype)
451 : ast_expression(ctx, TYPE_ast_entfield),
455 propagate_side_effects(*m_entity);
456 propagate_side_effects(*m_field);
459 compile_error(ctx, "ast_entfield: field has no type");
463 adopt_type(*outtype);
466 ast_entfield::~ast_entfield()
472 ast_member* ast_member_new(lex_ctx_t ctx, ast_expression *owner, unsigned int field, const char *name)
474 ast_instantiate(ast_member, ctx, ast_member_delete);
480 if (owner->m_vtype != TYPE_VECTOR &&
481 owner->m_vtype != TYPE_FIELD) {
482 compile_error(ctx, "member-access on an invalid owner of type %s", type_name[owner->m_vtype]);
487 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_member_codegen);
488 self->m_keep_node = true; /* keep */
490 if (owner->m_vtype == TYPE_VECTOR) {
491 self->m_vtype = TYPE_FLOAT;
492 self->m_next = nullptr;
494 self->m_vtype = TYPE_FIELD;
495 self->m_next = ast_shallow_type(ctx, TYPE_FLOAT);
498 self->m_rvalue = false;
499 self->m_owner = owner;
500 self->propagate_side_effects(owner);
502 self->m_field = field;
504 self->m_name = util_strdup(name);
506 self->m_name = nullptr;
511 void ast_member_delete(ast_member *self)
513 /* The owner is always an ast_value, which has .keep_node=true,
514 * also: ast_members are usually deleted after the owner, thus
515 * this will cause invalid access
516 ast_unref(self->m_owner);
517 * once we allow (expression).x to access a vector-member, we need
518 * to change this: preferably by creating an alternate ast node for this
519 * purpose that is not garbage-collected.
521 ast_expression_delete((ast_expression*)self);
527 bool ast_member_set_name(ast_member *self, const char *name)
530 mem_d((void*)self->m_name);
531 self->m_name = util_strdup(name);
532 return !!self->m_name;
535 ast_array_index* ast_array_index_new(lex_ctx_t ctx, ast_expression *array, ast_expression *index)
537 ast_expression *outtype;
538 ast_instantiate(ast_array_index, ctx, ast_array_index_delete);
540 outtype = array->m_next;
543 /* Error: field has no type... */
547 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_array_index_codegen);
549 self->m_array = array;
550 self->m_index = index;
551 self->propagate_side_effects(array);
552 self->propagate_side_effects(index);
554 ast_type_adopt(self, outtype);
555 if (array->m_vtype == TYPE_FIELD && outtype->m_vtype == TYPE_ARRAY) {
556 if (self->m_vtype != TYPE_ARRAY) {
557 compile_error(self->m_context, "array_index node on type");
558 ast_array_index_delete(self);
561 self->m_array = outtype;
562 self->m_vtype = TYPE_FIELD;
568 void ast_array_index_delete(ast_array_index *self)
571 ast_unref(self->m_array);
573 ast_unref(self->m_index);
574 ast_expression_delete((ast_expression*)self);
578 ast_argpipe* ast_argpipe_new(lex_ctx_t ctx, ast_expression *index)
580 ast_instantiate(ast_argpipe, ctx, ast_argpipe_delete);
581 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_argpipe_codegen);
582 self->m_index = index;
583 self->m_vtype = TYPE_NOEXPR;
587 void ast_argpipe_delete(ast_argpipe *self)
590 ast_unref(self->m_index);
591 ast_expression_delete((ast_expression*)self);
592 self->~ast_argpipe();
596 ast_ifthen* ast_ifthen_new(lex_ctx_t ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
598 ast_instantiate(ast_ifthen, ctx, ast_ifthen_delete);
599 if (!ontrue && !onfalse) {
600 /* because it is invalid */
604 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ifthen_codegen);
607 self->m_on_true = ontrue;
608 self->m_on_false = onfalse;
609 self->propagate_side_effects(cond);
611 self->propagate_side_effects(ontrue);
613 self->propagate_side_effects(onfalse);
618 void ast_ifthen_delete(ast_ifthen *self)
620 ast_unref(self->m_cond);
622 ast_unref(self->m_on_true);
623 if (self->m_on_false)
624 ast_unref(self->m_on_false);
625 ast_expression_delete((ast_expression*)self);
630 ast_ternary* ast_ternary_new(lex_ctx_t ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
632 ast_expression *exprtype = ontrue;
633 ast_instantiate(ast_ternary, ctx, ast_ternary_delete);
634 /* This time NEITHER must be nullptr */
635 if (!ontrue || !onfalse) {
639 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_ternary_codegen);
642 self->m_on_true = ontrue;
643 self->m_on_false = onfalse;
644 self->propagate_side_effects(cond);
645 self->propagate_side_effects(ontrue);
646 self->propagate_side_effects(onfalse);
648 if (ontrue->m_vtype == TYPE_NIL)
650 ast_type_adopt(self, exprtype);
655 void ast_ternary_delete(ast_ternary *self)
657 /* the if()s are only there because computed-gotos can set them
660 if (self->m_cond) ast_unref(self->m_cond);
661 if (self->m_on_true) ast_unref(self->m_on_true);
662 if (self->m_on_false) ast_unref(self->m_on_false);
663 ast_expression_delete((ast_expression*)self);
664 self->~ast_ternary();
668 ast_loop* ast_loop_new(lex_ctx_t ctx,
669 ast_expression *initexpr,
670 ast_expression *precond, bool pre_not,
671 ast_expression *postcond, bool post_not,
672 ast_expression *increment,
673 ast_expression *body)
675 ast_instantiate(ast_loop, ctx, ast_loop_delete);
676 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_loop_codegen);
678 self->m_initexpr = initexpr;
679 self->m_precond = precond;
680 self->m_postcond = postcond;
681 self->m_increment = increment;
684 self->m_pre_not = pre_not;
685 self->m_post_not = post_not;
688 self->propagate_side_effects(initexpr);
690 self->propagate_side_effects(precond);
692 self->propagate_side_effects(postcond);
694 self->propagate_side_effects(increment);
696 self->propagate_side_effects(body);
701 void ast_loop_delete(ast_loop *self)
703 if (self->m_initexpr)
704 ast_unref(self->m_initexpr);
706 ast_unref(self->m_precond);
707 if (self->m_postcond)
708 ast_unref(self->m_postcond);
709 if (self->m_increment)
710 ast_unref(self->m_increment);
712 ast_unref(self->m_body);
713 ast_expression_delete((ast_expression*)self);
718 ast_breakcont* ast_breakcont_new(lex_ctx_t ctx, bool iscont, unsigned int levels)
720 ast_instantiate(ast_breakcont, ctx, ast_breakcont_delete);
721 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_breakcont_codegen);
723 self->m_is_continue = iscont;
724 self->m_levels = levels;
729 void ast_breakcont_delete(ast_breakcont *self)
731 ast_expression_delete((ast_expression*)self);
732 self->~ast_breakcont();
736 ast_switch* ast_switch_new(lex_ctx_t ctx, ast_expression *op)
738 ast_instantiate(ast_switch, ctx, ast_switch_delete);
739 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_switch_codegen);
741 self->m_operand = op;
743 self->propagate_side_effects(op);
748 void ast_switch_delete(ast_switch *self)
750 ast_unref(self->m_operand);
752 for (auto &it : self->m_cases) {
754 ast_unref(it.m_value);
755 ast_unref(it.m_code);
758 ast_expression_delete((ast_expression*)self);
763 ast_label* ast_label_new(lex_ctx_t ctx, const char *name, bool undefined)
765 ast_instantiate(ast_label, ctx, ast_label_delete);
766 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_label_codegen);
768 self->m_vtype = TYPE_NOEXPR;
770 self->m_name = util_strdup(name);
771 self->m_irblock = nullptr;
772 self->m_undefined = undefined;
777 void ast_label_delete(ast_label *self)
779 mem_d((void*)self->m_name);
780 ast_expression_delete((ast_expression*)self);
785 static void ast_label_register_goto(ast_label *self, ast_goto *g)
787 self->m_gotos.push_back(g);
790 ast_goto* ast_goto_new(lex_ctx_t ctx, const char *name)
792 ast_instantiate(ast_goto, ctx, ast_goto_delete);
793 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_goto_codegen);
795 self->m_name = util_strdup(name);
796 self->m_target = nullptr;
797 self->m_irblock_from = nullptr;
802 void ast_goto_delete(ast_goto *self)
804 mem_d((void*)self->m_name);
805 ast_expression_delete((ast_expression*)self);
810 void ast_goto_set_label(ast_goto *self, ast_label *label)
812 self->m_target = label;
815 ast_state* ast_state_new(lex_ctx_t ctx, ast_expression *frame, ast_expression *think)
817 ast_instantiate(ast_state, ctx, ast_state_delete);
818 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_state_codegen);
819 self->m_framenum = frame;
820 self->m_nextthink = think;
824 void ast_state_delete(ast_state *self)
826 if (self->m_framenum)
827 ast_unref(self->m_framenum);
828 if (self->m_nextthink)
829 ast_unref(self->m_nextthink);
831 ast_expression_delete((ast_expression*)self);
836 ast_call* ast_call_new(lex_ctx_t ctx,
837 ast_expression *funcexpr)
839 ast_instantiate(ast_call, ctx, ast_call_delete);
840 if (!funcexpr->m_next) {
841 compile_error(ctx, "not a function");
845 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_call_codegen);
847 self->m_side_effects = true;
849 self->m_func = funcexpr;
850 self->m_va_count = nullptr;
852 ast_type_adopt(self, funcexpr->m_next);
857 void ast_call_delete(ast_call *self)
859 for (auto &it : self->m_params)
863 ast_unref(self->m_func);
865 if (self->m_va_count)
866 ast_unref(self->m_va_count);
868 ast_expression_delete((ast_expression*)self);
873 static bool ast_call_check_vararg(ast_call *self, ast_expression *va_type, ast_expression *exp_type)
879 if (!va_type || !ast_compare_type(va_type, exp_type))
881 if (va_type && exp_type)
883 ast_type_to_string(va_type, tgot, sizeof(tgot));
884 ast_type_to_string(exp_type, texp, sizeof(texp));
885 if (OPTS_FLAG(UNSAFE_VARARGS)) {
886 if (compile_warning(self->m_context, WARN_UNSAFE_TYPES,
887 "piped variadic argument differs in type: constrained to type %s, expected type %s",
891 compile_error(self->m_context,
892 "piped variadic argument differs in type: constrained to type %s, expected type %s",
899 ast_type_to_string(exp_type, texp, sizeof(texp));
900 if (OPTS_FLAG(UNSAFE_VARARGS)) {
901 if (compile_warning(self->m_context, WARN_UNSAFE_TYPES,
902 "piped variadic argument may differ in type: expected type %s",
906 compile_error(self->m_context,
907 "piped variadic argument may differ in type: expected type %s",
916 bool ast_call_check_types(ast_call *self, ast_expression *va_type)
922 const ast_expression *func = self->m_func;
923 size_t count = self->m_params.size();
924 if (count > func->m_type_params.size())
925 count = func->m_type_params.size();
927 for (i = 0; i < count; ++i) {
928 if (ast_istype(self->m_params[i], ast_argpipe)) {
929 /* warn about type safety instead */
931 compile_error(self->m_context, "argpipe must be the last parameter to a function call");
934 if (!ast_call_check_vararg(self, va_type, (ast_expression*)func->m_type_params[i]))
937 else if (!ast_compare_type(self->m_params[i], (ast_expression*)(func->m_type_params[i])))
939 ast_type_to_string(self->m_params[i], tgot, sizeof(tgot));
940 ast_type_to_string((ast_expression*)func->m_type_params[i], texp, sizeof(texp));
941 compile_error(self->m_context, "invalid type for parameter %u in function call: expected %s, got %s",
942 (unsigned int)(i+1), texp, tgot);
943 /* we don't immediately return */
947 count = self->m_params.size();
948 if (count > func->m_type_params.size() && func->m_varparam) {
949 for (; i < count; ++i) {
950 if (ast_istype(self->m_params[i], ast_argpipe)) {
951 /* warn about type safety instead */
953 compile_error(self->m_context, "argpipe must be the last parameter to a function call");
956 if (!ast_call_check_vararg(self, va_type, func->m_varparam))
959 else if (!ast_compare_type(self->m_params[i], func->m_varparam))
961 ast_type_to_string(self->m_params[i], tgot, sizeof(tgot));
962 ast_type_to_string(func->m_varparam, texp, sizeof(texp));
963 compile_error(self->m_context, "invalid type for variadic parameter %u in function call: expected %s, got %s",
964 (unsigned int)(i+1), texp, tgot);
965 /* we don't immediately return */
973 ast_store* ast_store_new(lex_ctx_t ctx, int op,
974 ast_expression *dest, ast_expression *source)
976 ast_instantiate(ast_store, ctx, ast_store_delete);
977 ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_store_codegen);
979 self->m_side_effects = true;
983 self->m_source = source;
985 ast_type_adopt(self, dest);
990 void ast_store_delete(ast_store *self)
992 ast_unref(self->m_dest);
993 ast_unref(self->m_source);
994 ast_expression_delete((ast_expression*)self);
999 ast_block* ast_block_new(lex_ctx_t ctx)
1001 ast_instantiate(ast_block, ctx, ast_block_delete);
1002 ast_expression_init((ast_expression*)self,
1003 (ast_expression_codegen*)&ast_block_codegen);
1007 bool ast_block_add_expr(ast_block *self, ast_expression *e)
1009 self->propagate_side_effects(e);
1010 self->m_exprs.push_back(e);
1012 ast_delete(self->m_next);
1013 self->m_next = nullptr;
1015 ast_type_adopt(self, e);
1019 void ast_block_collect(ast_block *self, ast_expression *expr)
1021 self->m_collect.push_back(expr);
1022 expr->m_keep_node = true;
1025 void ast_block_delete(ast_block *self)
1027 for (auto &it : self->m_exprs) ast_unref(it);
1028 for (auto &it : self->m_locals) ast_delete(it);
1029 for (auto &it : self->m_collect) ast_delete(it);
1030 ast_expression_delete((ast_expression*)self);
1035 void ast_block_set_type(ast_block *self, ast_expression *from)
1038 ast_delete(self->m_next);
1039 ast_type_adopt(self, from);
1042 ast_function* ast_function_new(lex_ctx_t ctx, const char *name, ast_value *vtype)
1044 ast_instantiate(ast_function, ctx, ast_function_delete);
1047 compile_error(self->m_context, "internal error: ast_function_new condition 0");
1049 } else if (vtype->m_hasvalue || vtype->m_vtype != TYPE_FUNCTION) {
1050 compile_error(self->m_context, "internal error: ast_function_new condition %i %i type=%i (probably 2 bodies?)",
1052 (int)vtype->m_hasvalue,
1057 self->m_function_type = vtype;
1058 self->m_name = name ? util_strdup(name) : nullptr;
1060 self->m_labelcount = 0;
1061 self->m_builtin = 0;
1063 self->m_ir_func = nullptr;
1064 self->m_curblock = nullptr;
1066 vtype->m_hasvalue = true;
1067 vtype->m_constval.vfunc = self;
1069 self->m_varargs = nullptr;
1070 self->m_argc = nullptr;
1071 self->m_fixedparams = nullptr;
1072 self->m_return_value = nullptr;
1073 self->m_static_count = 0;
1082 void ast_function_delete(ast_function *self)
1085 mem_d((void*)self->m_name);
1086 if (self->m_function_type) {
1087 /* ast_value_delete(self->m_function_type); */
1088 self->m_function_type->m_hasvalue = false;
1089 self->m_function_type->m_constval.vfunc = nullptr;
1090 /* We use unref - if it was stored in a global table it is supposed
1091 * to be deleted from *there*
1093 ast_unref(self->m_function_type);
1095 for (auto &it : self->m_static_names)
1097 // FIXME::DELME:: unique_ptr used on ast_block
1098 //for (auto &it : self->m_blocks)
1100 if (self->m_varargs)
1101 ast_delete(self->m_varargs);
1103 ast_delete(self->m_argc);
1104 if (self->m_fixedparams)
1105 ast_unref(self->m_fixedparams);
1106 if (self->m_return_value)
1107 ast_unref(self->m_return_value);
1108 self->~ast_function();
1112 const char* ast_function_label(ast_function *self, const char *prefix)
1118 if (!OPTS_OPTION_BOOL(OPTION_DUMP) &&
1119 !OPTS_OPTION_BOOL(OPTION_DUMPFIN) &&
1120 !OPTS_OPTION_BOOL(OPTION_DEBUG))
1125 id = (self->m_labelcount++);
1126 len = strlen(prefix);
1128 from = self->m_labelbuf + sizeof(self->m_labelbuf)-1;
1131 *from-- = (id%10) + '0';
1135 memcpy(from - len, prefix, len);
1139 /*********************************************************************/
1141 * by convention you must never pass nullptr to the 'ir_value **out'
1142 * parameter. If you really don't care about the output, pass a dummy.
1143 * But I can't imagine a pituation where the output is truly unnecessary.
1146 static void _ast_codegen_output_type(ast_expression *self, ir_value *out)
1148 if (out->m_vtype == TYPE_FIELD)
1149 out->m_fieldtype = self->m_next->m_vtype;
1150 if (out->m_vtype == TYPE_FUNCTION)
1151 out->m_outtype = self->m_next->m_vtype;
1154 #define codegen_output_type(a,o) (_ast_codegen_output_type(static_cast<ast_expression*>((a)),(o)))
1156 bool ast_value_codegen(ast_value *self, ast_function *func, bool lvalue, ir_value **out)
1160 if (self->m_vtype == TYPE_NIL) {
1161 *out = func->m_ir_func->m_owner->m_nil;
1164 /* NOTE: This is the codegen for a variable used in an
1165 * It is not the codegen to generate the value. For this purpose,
1166 * ast_local_codegen and ast_global_codegen are to be used before this
1167 * is executed. ast_function_codegen should take care of its locals,
1168 * and the ast-user should take care of ast_global_codegen to be used
1169 * on all the globals.
1171 if (!self->m_ir_v) {
1172 char tname[1024]; /* typename is reserved in C++ */
1173 ast_type_to_string((ast_expression*)self, tname, sizeof(tname));
1174 compile_error(self->m_context, "ast_value used before generated %s %s", tname, self->m_name);
1177 *out = self->m_ir_v;
1181 static bool ast_global_array_set(ast_value *self)
1183 size_t count = self->m_initlist.size();
1186 if (count > self->m_count) {
1187 compile_error(self->m_context, "too many elements in initializer");
1188 count = self->m_count;
1190 else if (count < self->m_count) {
1192 compile_warning(self->m_context, "not all elements are initialized");
1196 for (i = 0; i != count; ++i) {
1197 switch (self->m_next->m_vtype) {
1199 if (!ir_value_set_float(self->m_ir_values[i], self->m_initlist[i].vfloat))
1203 if (!ir_value_set_vector(self->m_ir_values[i], self->m_initlist[i].vvec))
1207 if (!ir_value_set_string(self->m_ir_values[i], self->m_initlist[i].vstring))
1211 /* we don't support them in any other place yet either */
1212 compile_error(self->m_context, "TODO: nested arrays");
1215 /* this requiers a bit more work - similar to the fields I suppose */
1216 compile_error(self->m_context, "global of type function not properly generated");
1219 if (!self->m_initlist[i].vfield) {
1220 compile_error(self->m_context, "field constant without vfield set");
1223 if (!self->m_initlist[i].vfield->m_ir_v) {
1224 compile_error(self->m_context, "field constant generated before its field");
1227 if (!ir_value_set_field(self->m_ir_values[i], self->m_initlist[i].vfield->m_ir_v))
1231 compile_error(self->m_context, "TODO: global constant type %i", self->m_vtype);
1238 static bool check_array(ast_value *self, ast_value *array)
1240 if (array->m_flags & AST_FLAG_ARRAY_INIT && array->m_initlist.empty()) {
1241 compile_error(self->m_context, "array without size: %s", self->m_name);
1244 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1245 if (!array->m_count || array->m_count > OPTS_OPTION_U32(OPTION_MAX_ARRAY_SIZE)) {
1246 compile_error(self->m_context, "Invalid array of size %lu", (unsigned long)array->m_count);
1252 bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield)
1254 ir_value *v = nullptr;
1256 if (self->m_vtype == TYPE_NIL) {
1257 compile_error(self->m_context, "internal error: trying to generate a variable of TYPE_NIL");
1261 if (self->m_hasvalue && self->m_vtype == TYPE_FUNCTION)
1263 ir_function *func = ir_builder_create_function(ir, self->m_name, self->m_next->m_vtype);
1266 func->m_context = self->m_context;
1267 func->m_value->m_context = self->m_context;
1269 self->m_constval.vfunc->m_ir_func = func;
1270 self->m_ir_v = func->m_value;
1271 if (self->m_flags & AST_FLAG_INCLUDE_DEF)
1272 self->m_ir_v->m_flags |= IR_FLAG_INCLUDE_DEF;
1273 if (self->m_flags & AST_FLAG_ERASEABLE)
1274 self->m_ir_v->m_flags |= IR_FLAG_ERASABLE;
1275 if (self->m_flags & AST_FLAG_BLOCK_COVERAGE)
1276 func->m_flags |= IR_FLAG_BLOCK_COVERAGE;
1277 /* The function is filled later on ast_function_codegen... */
1281 if (isfield && self->m_vtype == TYPE_FIELD) {
1282 ast_expression *fieldtype = self->m_next;
1284 if (self->m_hasvalue) {
1285 compile_error(self->m_context, "TODO: constant field pointers with value");
1289 if (fieldtype->m_vtype == TYPE_ARRAY) {
1294 ast_expression *elemtype;
1296 ast_value *array = (ast_value*)fieldtype;
1298 if (!ast_istype(fieldtype, ast_value)) {
1299 compile_error(self->m_context, "internal error: ast_value required");
1303 if (!check_array(self, array))
1306 elemtype = array->m_next;
1307 vtype = elemtype->m_vtype;
1309 v = ir_builder_create_field(ir, self->m_name, vtype);
1311 compile_error(self->m_context, "ir_builder_create_global failed on `%s`", self->m_name);
1314 v->m_context = self->m_context;
1315 v->m_unique_life = true;
1317 array->m_ir_v = self->m_ir_v = v;
1319 if (self->m_flags & AST_FLAG_INCLUDE_DEF)
1320 self->m_ir_v->m_flags |= IR_FLAG_INCLUDE_DEF;
1321 if (self->m_flags & AST_FLAG_ERASEABLE)
1322 self->m_ir_v->m_flags |= IR_FLAG_ERASABLE;
1324 namelen = strlen(self->m_name);
1325 name = (char*)mem_a(namelen + 16);
1326 util_strncpy(name, self->m_name, namelen);
1328 array->m_ir_values = (ir_value**)mem_a(sizeof(array->m_ir_values[0]) * array->m_count);
1329 array->m_ir_values[0] = v;
1330 for (ai = 1; ai < array->m_count; ++ai) {
1331 util_snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1332 array->m_ir_values[ai] = ir_builder_create_field(ir, name, vtype);
1333 if (!array->m_ir_values[ai]) {
1335 compile_error(self->m_context, "ir_builder_create_global failed on `%s`", name);
1338 array->m_ir_values[ai]->m_context = self->m_context;
1339 array->m_ir_values[ai]->m_unique_life = true;
1340 array->m_ir_values[ai]->m_locked = true;
1341 if (self->m_flags & AST_FLAG_INCLUDE_DEF)
1342 self->m_ir_values[ai]->m_flags |= IR_FLAG_INCLUDE_DEF;
1348 v = ir_builder_create_field(ir, self->m_name, self->m_next->m_vtype);
1351 v->m_context = self->m_context;
1353 if (self->m_flags & AST_FLAG_INCLUDE_DEF)
1354 self->m_ir_v->m_flags |= IR_FLAG_INCLUDE_DEF;
1356 if (self->m_flags & AST_FLAG_ERASEABLE)
1357 self->m_ir_v->m_flags |= IR_FLAG_ERASABLE;
1362 if (self->m_vtype == TYPE_ARRAY) {
1367 ast_expression *elemtype = self->m_next;
1368 qc_type vtype = elemtype->m_vtype;
1370 if (self->m_flags & AST_FLAG_ARRAY_INIT && !self->m_count) {
1371 compile_error(self->m_context, "array `%s' has no size", self->m_name);
1375 /* same as with field arrays */
1376 if (!check_array(self, self))
1379 v = ir_builder_create_global(ir, self->m_name, vtype);
1381 compile_error(self->m_context, "ir_builder_create_global failed `%s`", self->m_name);
1384 v->m_context = self->m_context;
1385 v->m_unique_life = true;
1388 if (self->m_flags & AST_FLAG_INCLUDE_DEF)
1389 v->m_flags |= IR_FLAG_INCLUDE_DEF;
1390 if (self->m_flags & AST_FLAG_ERASEABLE)
1391 self->m_ir_v->m_flags |= IR_FLAG_ERASABLE;
1393 namelen = strlen(self->m_name);
1394 name = (char*)mem_a(namelen + 16);
1395 util_strncpy(name, self->m_name, namelen);
1397 self->m_ir_values = (ir_value**)mem_a(sizeof(self->m_ir_values[0]) * self->m_count);
1398 self->m_ir_values[0] = v;
1399 for (ai = 1; ai < self->m_count; ++ai) {
1400 util_snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1401 self->m_ir_values[ai] = ir_builder_create_global(ir, name, vtype);
1402 if (!self->m_ir_values[ai]) {
1404 compile_error(self->m_context, "ir_builder_create_global failed `%s`", name);
1407 self->m_ir_values[ai]->m_context = self->m_context;
1408 self->m_ir_values[ai]->m_unique_life = true;
1409 self->m_ir_values[ai]->m_locked = true;
1410 if (self->m_flags & AST_FLAG_INCLUDE_DEF)
1411 self->m_ir_values[ai]->m_flags |= IR_FLAG_INCLUDE_DEF;
1417 /* Arrays don't do this since there's no "array" value which spans across the
1420 v = ir_builder_create_global(ir, self->m_name, self->m_vtype);
1422 compile_error(self->m_context, "ir_builder_create_global failed on `%s`", self->m_name);
1425 codegen_output_type(self, v);
1426 v->m_context = self->m_context;
1429 /* link us to the ir_value */
1430 v->m_cvq = self->m_cvq;
1433 if (self->m_flags & AST_FLAG_INCLUDE_DEF)
1434 self->m_ir_v->m_flags |= IR_FLAG_INCLUDE_DEF;
1435 if (self->m_flags & AST_FLAG_ERASEABLE)
1436 self->m_ir_v->m_flags |= IR_FLAG_ERASABLE;
1439 if (self->m_hasvalue) {
1440 switch (self->m_vtype)
1443 if (!ir_value_set_float(v, self->m_constval.vfloat))
1447 if (!ir_value_set_vector(v, self->m_constval.vvec))
1451 if (!ir_value_set_string(v, self->m_constval.vstring))
1455 ast_global_array_set(self);
1458 compile_error(self->m_context, "global of type function not properly generated");
1460 /* Cannot generate an IR value for a function,
1461 * need a pointer pointing to a function rather.
1464 if (!self->m_constval.vfield) {
1465 compile_error(self->m_context, "field constant without vfield set");
1468 if (!self->m_constval.vfield->m_ir_v) {
1469 compile_error(self->m_context, "field constant generated before its field");
1472 if (!ir_value_set_field(v, self->m_constval.vfield->m_ir_v))
1476 compile_error(self->m_context, "TODO: global constant type %i", self->m_vtype);
1482 error: /* clean up */
1487 static bool ast_local_codegen(ast_value *self, ir_function *func, bool param)
1489 ir_value *v = nullptr;
1491 if (self->m_vtype == TYPE_NIL) {
1492 compile_error(self->m_context, "internal error: trying to generate a variable of TYPE_NIL");
1496 if (self->m_hasvalue && self->m_vtype == TYPE_FUNCTION)
1498 /* Do we allow local functions? I think not...
1499 * this is NOT a function pointer atm.
1504 if (self->m_vtype == TYPE_ARRAY) {
1509 ast_expression *elemtype = self->m_next;
1510 qc_type vtype = elemtype->m_vtype;
1512 func->m_flags |= IR_FLAG_HAS_ARRAYS;
1514 if (param && !(self->m_flags & AST_FLAG_IS_VARARG)) {
1515 compile_error(self->m_context, "array-parameters are not supported");
1519 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1520 if (!check_array(self, self))
1523 self->m_ir_values = (ir_value**)mem_a(sizeof(self->m_ir_values[0]) * self->m_count);
1524 if (!self->m_ir_values) {
1525 compile_error(self->m_context, "failed to allocate array values");
1529 v = ir_function_create_local(func, self->m_name, vtype, param);
1531 compile_error(self->m_context, "internal error: ir_function_create_local failed");
1534 v->m_context = self->m_context;
1535 v->m_unique_life = true;
1538 namelen = strlen(self->m_name);
1539 name = (char*)mem_a(namelen + 16);
1540 util_strncpy(name, self->m_name, namelen);
1542 self->m_ir_values[0] = v;
1543 for (ai = 1; ai < self->m_count; ++ai) {
1544 util_snprintf(name + namelen, 16, "[%u]", (unsigned int)ai);
1545 self->m_ir_values[ai] = ir_function_create_local(func, name, vtype, param);
1546 if (!self->m_ir_values[ai]) {
1547 compile_error(self->m_context, "internal_error: ir_builder_create_global failed on `%s`", name);
1550 self->m_ir_values[ai]->m_context = self->m_context;
1551 self->m_ir_values[ai]->m_unique_life = true;
1552 self->m_ir_values[ai]->m_locked = true;
1558 v = ir_function_create_local(func, self->m_name, self->m_vtype, param);
1561 codegen_output_type(self, v);
1562 v->m_context = self->m_context;
1565 /* A constant local... hmmm...
1566 * I suppose the IR will have to deal with this
1568 if (self->m_hasvalue) {
1569 switch (self->m_vtype)
1572 if (!ir_value_set_float(v, self->m_constval.vfloat))
1576 if (!ir_value_set_vector(v, self->m_constval.vvec))
1580 if (!ir_value_set_string(v, self->m_constval.vstring))
1584 compile_error(self->m_context, "TODO: global constant type %i", self->m_vtype);
1589 /* link us to the ir_value */
1590 v->m_cvq = self->m_cvq;
1593 if (!ast_generate_accessors(self, func->m_owner))
1597 error: /* clean up */
1602 bool ast_generate_accessors(ast_value *self, ir_builder *ir)
1605 bool warn = OPTS_WARN(WARN_USED_UNINITIALIZED);
1606 if (!self->m_setter || !self->m_getter)
1608 for (i = 0; i < self->m_count; ++i) {
1609 if (!self->m_ir_values) {
1610 compile_error(self->m_context, "internal error: no array values generated for `%s`", self->m_name);
1613 if (!self->m_ir_values[i]) {
1614 compile_error(self->m_context, "internal error: not all array values have been generated for `%s`", self->m_name);
1617 if (!self->m_ir_values[i]->m_life.empty()) {
1618 compile_error(self->m_context, "internal error: function containing `%s` already generated", self->m_name);
1623 opts_set(opts.warn, WARN_USED_UNINITIALIZED, false);
1624 if (self->m_setter) {
1625 if (!ast_global_codegen (self->m_setter, ir, false) ||
1626 !ast_function_codegen(self->m_setter->m_constval.vfunc, ir) ||
1627 !ir_function_finalize(self->m_setter->m_constval.vfunc->m_ir_func))
1629 compile_error(self->m_context, "internal error: failed to generate setter for `%s`", self->m_name);
1630 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1634 if (self->m_getter) {
1635 if (!ast_global_codegen (self->m_getter, ir, false) ||
1636 !ast_function_codegen(self->m_getter->m_constval.vfunc, ir) ||
1637 !ir_function_finalize(self->m_getter->m_constval.vfunc->m_ir_func))
1639 compile_error(self->m_context, "internal error: failed to generate getter for `%s`", self->m_name);
1640 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1644 for (i = 0; i < self->m_count; ++i)
1645 self->m_ir_values[i]->m_life.clear();
1646 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1650 bool ast_function_codegen(ast_function *self, ir_builder *ir)
1655 ast_expression_codegen *cgen;
1659 irf = self->m_ir_func;
1661 compile_error(self->m_context, "internal error: ast_function's related ast_value was not generated yet");
1665 /* fill the parameter list */
1666 ec = self->m_function_type;
1667 for (auto &it : ec->m_type_params) {
1668 if (it->m_vtype == TYPE_FIELD)
1669 vec_push(irf->m_params, it->m_next->m_vtype);
1671 vec_push(irf->m_params, it->m_vtype);
1672 if (!self->m_builtin) {
1673 if (!ast_local_codegen(it, self->m_ir_func, true))
1678 if (self->m_varargs) {
1679 if (!ast_local_codegen(self->m_varargs, self->m_ir_func, true))
1681 irf->m_max_varargs = self->m_varargs->m_count;
1684 if (self->m_builtin) {
1685 irf->m_builtin = self->m_builtin;
1689 /* have a local return value variable? */
1690 if (self->m_return_value) {
1691 if (!ast_local_codegen(self->m_return_value, self->m_ir_func, false))
1695 if (self->m_blocks.empty()) {
1696 compile_error(self->m_context, "function `%s` has no body", self->m_name);
1700 irf->m_first = self->m_curblock = ir_function_create_block(self->m_context, irf, "entry");
1701 if (!self->m_curblock) {
1702 compile_error(self->m_context, "failed to allocate entry block for `%s`", self->m_name);
1710 if (!ast_local_codegen(self->m_argc, self->m_ir_func, true))
1712 cgen = self->m_argc->m_codegen;
1713 if (!(*cgen)((ast_expression*)(self->m_argc), self, false, &va_count))
1715 cgen = self->m_fixedparams->m_codegen;
1716 if (!(*cgen)((ast_expression*)(self->m_fixedparams), self, false, &fixed))
1718 sub = ir_block_create_binop(self->m_curblock, self->m_context,
1719 ast_function_label(self, "va_count"), INSTR_SUB_F,
1720 ir_builder_get_va_count(ir), fixed);
1723 if (!ir_block_create_store_op(self->m_curblock, self->m_context, INSTR_STORE_F,
1730 for (auto &it : self->m_blocks) {
1731 cgen = it->m_codegen;
1732 if (!(*cgen)(it.get(), self, false, &dummy))
1736 /* TODO: check return types */
1737 if (!self->m_curblock->m_final)
1739 if (!self->m_function_type->m_next ||
1740 self->m_function_type->m_next->m_vtype == TYPE_VOID)
1742 return ir_block_create_return(self->m_curblock, self->m_context, nullptr);
1744 else if (vec_size(self->m_curblock->m_entries) || self->m_curblock == irf->m_first)
1746 if (self->m_return_value) {
1747 cgen = self->m_return_value->m_codegen;
1748 if (!(*cgen)((ast_expression*)(self->m_return_value), self, false, &dummy))
1750 return ir_block_create_return(self->m_curblock, self->m_context, dummy);
1752 else if (compile_warning(self->m_context, WARN_MISSING_RETURN_VALUES,
1753 "control reaches end of non-void function (`%s`) via %s",
1754 self->m_name, self->m_curblock->m_label.c_str()))
1758 return ir_block_create_return(self->m_curblock, self->m_context, nullptr);
1764 static bool starts_a_label(ast_expression *ex)
1766 while (ex && ast_istype(ex, ast_block)) {
1767 ast_block *b = (ast_block*)ex;
1772 return ast_istype(ex, ast_label);
1775 /* Note, you will not see ast_block_codegen generate ir_blocks.
1776 * To the AST and the IR, blocks are 2 different things.
1777 * In the AST it represents a block of code, usually enclosed in
1778 * curly braces {...}.
1779 * While in the IR it represents a block in terms of control-flow.
1781 bool ast_block_codegen(ast_block *self, ast_function *func, bool lvalue, ir_value **out)
1783 /* We don't use this
1784 * Note: an ast-representation using the comma-operator
1785 * of the form: (a, b, c) = x should not assign to c...
1788 compile_error(self->m_context, "not an l-value (code-block)");
1793 *out = self->m_outr;
1797 /* output is nullptr at first, we'll have each expression
1798 * assign to out output, thus, a comma-operator represention
1799 * using an ast_block will return the last generated value,
1800 * so: (b, c) + a executed both b and c, and returns c,
1801 * which is then added to a.
1805 /* generate locals */
1806 for (auto &it : self->m_locals) {
1807 if (!ast_local_codegen(it, func->m_ir_func, false)) {
1808 if (OPTS_OPTION_BOOL(OPTION_DEBUG))
1809 compile_error(self->m_context, "failed to generate local `%s`", it->m_name);
1814 for (auto &it : self->m_exprs) {
1815 ast_expression_codegen *gen;
1816 if (func->m_curblock->m_final && !starts_a_label(it)) {
1817 if (compile_warning(it->m_context, WARN_UNREACHABLE_CODE, "unreachable statement"))
1821 gen = it->m_codegen;
1822 if (!(*gen)(it, func, false, out))
1826 self->m_outr = *out;
1831 bool ast_store_codegen(ast_store *self, ast_function *func, bool lvalue, ir_value **out)
1833 ast_expression_codegen *cgen;
1834 ir_value *left = nullptr;
1835 ir_value *right = nullptr;
1839 ast_array_index *ai = nullptr;
1841 if (lvalue && self->m_outl) {
1842 *out = self->m_outl;
1846 if (!lvalue && self->m_outr) {
1847 *out = self->m_outr;
1851 if (ast_istype(self->m_dest, ast_array_index))
1854 ai = (ast_array_index*)self->m_dest;
1855 idx = (ast_value*)ai->m_index;
1857 if (ast_istype(ai->m_index, ast_value) && idx->m_hasvalue && idx->m_cvq == CV_CONST)
1862 /* we need to call the setter */
1863 ir_value *iridx, *funval;
1867 compile_error(self->m_context, "array-subscript assignment cannot produce lvalues");
1871 arr = (ast_value*)ai->m_array;
1872 if (!ast_istype(ai->m_array, ast_value) || !arr->m_setter) {
1873 compile_error(self->m_context, "value has no setter (%s)", arr->m_name);
1877 cgen = idx->m_codegen;
1878 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
1881 cgen = arr->m_setter->m_codegen;
1882 if (!(*cgen)((ast_expression*)(arr->m_setter), func, true, &funval))
1885 cgen = self->m_source->m_codegen;
1886 if (!(*cgen)((ast_expression*)(self->m_source), func, false, &right))
1889 call = ir_block_create_call(func->m_curblock, self->m_context, ast_function_label(func, "store"), funval, false);
1892 ir_call_param(call, iridx);
1893 ir_call_param(call, right);
1894 self->m_outr = right;
1900 cgen = self->m_dest->m_codegen;
1902 if (!(*cgen)((ast_expression*)(self->m_dest), func, true, &left))
1904 self->m_outl = left;
1906 cgen = self->m_source->m_codegen;
1908 if (!(*cgen)((ast_expression*)(self->m_source), func, false, &right))
1911 if (!ir_block_create_store_op(func->m_curblock, self->m_context, self->m_op, left, right))
1913 self->m_outr = right;
1916 /* Theoretically, an assinment returns its left side as an
1917 * lvalue, if we don't need an lvalue though, we return
1918 * the right side as an rvalue, otherwise we have to
1919 * somehow know whether or not we need to dereference the pointer
1920 * on the left side - that is: OP_LOAD if it was an address.
1921 * Also: in original QC we cannot OP_LOADP *anyway*.
1923 *out = (lvalue ? left : right);
1928 bool ast_binary_codegen(ast_binary *self, ast_function *func, bool lvalue, ir_value **out)
1930 ast_expression_codegen *cgen;
1931 ir_value *left, *right;
1933 /* A binary operation cannot yield an l-value */
1935 compile_error(self->m_context, "not an l-value (binop)");
1940 *out = self->m_outr;
1944 if ((OPTS_FLAG(SHORT_LOGIC) || OPTS_FLAG(PERL_LOGIC)) &&
1945 (self->m_op == INSTR_AND || self->m_op == INSTR_OR))
1947 /* NOTE: The short-logic path will ignore right_first */
1949 /* short circuit evaluation */
1950 ir_block *other, *merge;
1951 ir_block *from_left, *from_right;
1955 /* prepare end-block */
1956 merge_id = func->m_ir_func->m_blocks.size();
1957 merge = ir_function_create_block(self->m_context, func->m_ir_func, ast_function_label(func, "sce_merge"));
1959 /* generate the left expression */
1960 cgen = self->m_left->m_codegen;
1961 if (!(*cgen)((ast_expression*)(self->m_left), func, false, &left))
1963 /* remember the block */
1964 from_left = func->m_curblock;
1966 /* create a new block for the right expression */
1967 other = ir_function_create_block(self->m_context, func->m_ir_func, ast_function_label(func, "sce_other"));
1968 if (self->m_op == INSTR_AND) {
1969 /* on AND: left==true -> other */
1970 if (!ir_block_create_if(func->m_curblock, self->m_context, left, other, merge))
1973 /* on OR: left==false -> other */
1974 if (!ir_block_create_if(func->m_curblock, self->m_context, left, merge, other))
1977 /* use the likely flag */
1978 vec_last(func->m_curblock->m_instr)->m_likely = true;
1980 /* enter the right-expression's block */
1981 func->m_curblock = other;
1983 cgen = self->m_right->m_codegen;
1984 if (!(*cgen)((ast_expression*)(self->m_right), func, false, &right))
1986 /* remember block */
1987 from_right = func->m_curblock;
1989 /* jump to the merge block */
1990 if (!ir_block_create_jump(func->m_curblock, self->m_context, merge))
1993 algo::shiftback(func->m_ir_func->m_blocks.begin() + merge_id,
1994 func->m_ir_func->m_blocks.end());
1996 //func->m_ir_func->m_blocks[merge_id].release();
1997 //func->m_ir_func->m_blocks.erase(func->m_ir_func->m_blocks.begin() + merge_id);
1998 //func->m_ir_func->m_blocks.emplace_back(merge);
2000 func->m_curblock = merge;
2001 phi = ir_block_create_phi(func->m_curblock, self->m_context,
2002 ast_function_label(func, "sce_value"),
2004 ir_phi_add(phi, from_left, left);
2005 ir_phi_add(phi, from_right, right);
2006 *out = ir_phi_value(phi);
2010 if (!OPTS_FLAG(PERL_LOGIC)) {
2012 if (OPTS_FLAG(CORRECT_LOGIC) && (*out)->m_vtype == TYPE_VECTOR) {
2013 *out = ir_block_create_unary(func->m_curblock, self->m_context,
2014 ast_function_label(func, "sce_bool_v"),
2018 *out = ir_block_create_unary(func->m_curblock, self->m_context,
2019 ast_function_label(func, "sce_bool"),
2024 else if (OPTS_FLAG(FALSE_EMPTY_STRINGS) && (*out)->m_vtype == TYPE_STRING) {
2025 *out = ir_block_create_unary(func->m_curblock, self->m_context,
2026 ast_function_label(func, "sce_bool_s"),
2030 *out = ir_block_create_unary(func->m_curblock, self->m_context,
2031 ast_function_label(func, "sce_bool"),
2037 *out = ir_block_create_binop(func->m_curblock, self->m_context,
2038 ast_function_label(func, "sce_bool"),
2039 INSTR_AND, *out, *out);
2045 self->m_outr = *out;
2046 codegen_output_type(self, *out);
2050 if (self->m_right_first) {
2051 cgen = self->m_right->m_codegen;
2052 if (!(*cgen)((ast_expression*)(self->m_right), func, false, &right))
2054 cgen = self->m_left->m_codegen;
2055 if (!(*cgen)((ast_expression*)(self->m_left), func, false, &left))
2058 cgen = self->m_left->m_codegen;
2059 if (!(*cgen)((ast_expression*)(self->m_left), func, false, &left))
2061 cgen = self->m_right->m_codegen;
2062 if (!(*cgen)((ast_expression*)(self->m_right), func, false, &right))
2066 *out = ir_block_create_binop(func->m_curblock, self->m_context, ast_function_label(func, "bin"),
2067 self->m_op, left, right);
2070 self->m_outr = *out;
2071 codegen_output_type(self, *out);
2076 bool ast_binstore_codegen(ast_binstore *self, ast_function *func, bool lvalue, ir_value **out)
2078 ast_expression_codegen *cgen;
2079 ir_value *leftl = nullptr, *leftr, *right, *bin;
2083 ast_array_index *ai = nullptr;
2084 ir_value *iridx = nullptr;
2086 if (lvalue && self->m_outl) {
2087 *out = self->m_outl;
2091 if (!lvalue && self->m_outr) {
2092 *out = self->m_outr;
2096 if (ast_istype(self->m_dest, ast_array_index))
2099 ai = (ast_array_index*)self->m_dest;
2100 idx = (ast_value*)ai->m_index;
2102 if (ast_istype(ai->m_index, ast_value) && idx->m_hasvalue && idx->m_cvq == CV_CONST)
2106 /* for a binstore we need both an lvalue and an rvalue for the left side */
2107 /* rvalue of destination! */
2109 cgen = idx->m_codegen;
2110 if (!(*cgen)((ast_expression*)(idx), func, false, &iridx))
2113 cgen = self->m_dest->m_codegen;
2114 if (!(*cgen)((ast_expression*)(self->m_dest), func, false, &leftr))
2117 /* source as rvalue only */
2118 cgen = self->m_source->m_codegen;
2119 if (!(*cgen)((ast_expression*)(self->m_source), func, false, &right))
2122 /* now the binary */
2123 bin = ir_block_create_binop(func->m_curblock, self->m_context, ast_function_label(func, "binst"),
2124 self->m_opbin, leftr, right);
2128 /* we need to call the setter */
2133 compile_error(self->m_context, "array-subscript assignment cannot produce lvalues");
2137 arr = (ast_value*)ai->m_array;
2138 if (!ast_istype(ai->m_array, ast_value) || !arr->m_setter) {
2139 compile_error(self->m_context, "value has no setter (%s)", arr->m_name);
2143 cgen = arr->m_setter->m_codegen;
2144 if (!(*cgen)((ast_expression*)(arr->m_setter), func, true, &funval))
2147 call = ir_block_create_call(func->m_curblock, self->m_context, ast_function_label(func, "store"), funval, false);
2150 ir_call_param(call, iridx);
2151 ir_call_param(call, bin);
2154 /* now store them */
2155 cgen = self->m_dest->m_codegen;
2156 /* lvalue of destination */
2157 if (!(*cgen)((ast_expression*)(self->m_dest), func, true, &leftl))
2159 self->m_outl = leftl;
2161 if (!ir_block_create_store_op(func->m_curblock, self->m_context, self->m_opstore, leftl, bin))
2166 /* Theoretically, an assinment returns its left side as an
2167 * lvalue, if we don't need an lvalue though, we return
2168 * the right side as an rvalue, otherwise we have to
2169 * somehow know whether or not we need to dereference the pointer
2170 * on the left side - that is: OP_LOAD if it was an address.
2171 * Also: in original QC we cannot OP_LOADP *anyway*.
2173 *out = (lvalue ? leftl : bin);
2178 bool ast_unary_codegen(ast_unary *self, ast_function *func, bool lvalue, ir_value **out)
2180 ast_expression_codegen *cgen;
2183 /* An unary operation cannot yield an l-value */
2185 compile_error(self->m_context, "not an l-value (binop)");
2190 *out = self->m_outr;
2194 cgen = self->m_operand->m_codegen;
2196 if (!(*cgen)((ast_expression*)(self->m_operand), func, false, &operand))
2199 *out = ir_block_create_unary(func->m_curblock, self->m_context, ast_function_label(func, "unary"),
2200 self->m_op, operand);
2203 self->m_outr = *out;
2208 bool ast_return_codegen(ast_return *self, ast_function *func, bool lvalue, ir_value **out)
2210 ast_expression_codegen *cgen;
2215 /* In the context of a return operation, we don't actually return
2219 compile_error(self->m_context, "return-expression is not an l-value");
2224 compile_error(self->m_context, "internal error: ast_return cannot be reused, it bears no result!");
2227 self->m_outr = (ir_value*)1;
2229 if (self->m_operand) {
2230 cgen = self->m_operand->m_codegen;
2232 if (!(*cgen)((ast_expression*)(self->m_operand), func, false, &operand))
2235 if (!ir_block_create_return(func->m_curblock, self->m_context, operand))
2238 if (!ir_block_create_return(func->m_curblock, self->m_context, nullptr))
2245 bool ast_entfield_codegen(ast_entfield *self, ast_function *func, bool lvalue, ir_value **out)
2247 ast_expression_codegen *cgen;
2248 ir_value *ent, *field;
2250 /* This function needs to take the 'lvalue' flag into account!
2251 * As lvalue we provide a field-pointer, as rvalue we provide the
2255 if (lvalue && self->m_outl) {
2256 *out = self->m_outl;
2260 if (!lvalue && self->m_outr) {
2261 *out = self->m_outr;
2265 cgen = self->m_entity->m_codegen;
2266 if (!(*cgen)((ast_expression*)(self->m_entity), func, false, &ent))
2269 cgen = self->m_field->m_codegen;
2270 if (!(*cgen)((ast_expression*)(self->m_field), func, false, &field))
2275 *out = ir_block_create_fieldaddress(func->m_curblock, self->m_context, ast_function_label(func, "efa"),
2278 *out = ir_block_create_load_from_ent(func->m_curblock, self->m_context, ast_function_label(func, "efv"),
2279 ent, field, self->m_vtype);
2280 /* Done AFTER error checking:
2281 codegen_output_type(self, *out);
2285 compile_error(self->m_context, "failed to create %s instruction (output type %s)",
2286 (lvalue ? "ADDRESS" : "FIELD"),
2287 type_name[self->m_vtype]);
2291 codegen_output_type(self, *out);
2294 self->m_outl = *out;
2296 self->m_outr = *out;
2298 /* Hm that should be it... */
2302 bool ast_member_codegen(ast_member *self, ast_function *func, bool lvalue, ir_value **out)
2304 ast_expression_codegen *cgen;
2307 /* in QC this is always an lvalue */
2308 if (lvalue && self->m_rvalue) {
2309 compile_error(self->m_context, "not an l-value (member access)");
2313 *out = self->m_outl;
2317 cgen = self->m_owner->m_codegen;
2318 if (!(*cgen)((ast_expression*)(self->m_owner), func, false, &vec))
2321 if (vec->m_vtype != TYPE_VECTOR &&
2322 !(vec->m_vtype == TYPE_FIELD && self->m_owner->m_next->m_vtype == TYPE_VECTOR))
2327 *out = ir_value_vector_member(vec, self->m_field);
2328 self->m_outl = *out;
2330 return (*out != nullptr);
2333 bool ast_array_index_codegen(ast_array_index *self, ast_function *func, bool lvalue, ir_value **out)
2338 if (!lvalue && self->m_outr) {
2339 *out = self->m_outr;
2342 if (lvalue && self->m_outl) {
2343 *out = self->m_outl;
2347 if (!ast_istype(self->m_array, ast_value)) {
2348 compile_error(self->m_context, "array indexing this way is not supported");
2349 /* note this would actually be pointer indexing because the left side is
2350 * not an actual array but (hopefully) an indexable expression.
2351 * Once we get integer arithmetic, and GADDRESS/GSTORE/GLOAD instruction
2352 * support this path will be filled.
2357 arr = (ast_value*)self->m_array;
2358 idx = (ast_value*)self->m_index;
2360 if (!ast_istype(self->m_index, ast_value) || !idx->m_hasvalue || idx->m_cvq != CV_CONST) {
2361 /* Time to use accessor functions */
2362 ast_expression_codegen *cgen;
2363 ir_value *iridx, *funval;
2367 compile_error(self->m_context, "(.2) array indexing here needs a compile-time constant");
2371 if (!arr->m_getter) {
2372 compile_error(self->m_context, "value has no getter, don't know how to index it");
2376 cgen = self->m_index->m_codegen;
2377 if (!(*cgen)((ast_expression*)(self->m_index), func, false, &iridx))
2380 cgen = arr->m_getter->m_codegen;
2381 if (!(*cgen)((ast_expression*)(arr->m_getter), func, true, &funval))
2384 call = ir_block_create_call(func->m_curblock, self->m_context, ast_function_label(func, "fetch"), funval, false);
2387 ir_call_param(call, iridx);
2389 *out = ir_call_value(call);
2390 self->m_outr = *out;
2391 (*out)->m_vtype = self->m_vtype;
2392 codegen_output_type(self, *out);
2396 if (idx->m_vtype == TYPE_FLOAT) {
2397 unsigned int arridx = idx->m_constval.vfloat;
2398 if (arridx >= self->m_array->m_count)
2400 compile_error(self->m_context, "array index out of bounds: %i", arridx);
2403 *out = arr->m_ir_values[arridx];
2405 else if (idx->m_vtype == TYPE_INTEGER) {
2406 unsigned int arridx = idx->m_constval.vint;
2407 if (arridx >= self->m_array->m_count)
2409 compile_error(self->m_context, "array index out of bounds: %i", arridx);
2412 *out = arr->m_ir_values[arridx];
2415 compile_error(self->m_context, "array indexing here needs an integer constant");
2418 (*out)->m_vtype = self->m_vtype;
2419 codegen_output_type(self, *out);
2423 bool ast_argpipe_codegen(ast_argpipe *self, ast_function *func, bool lvalue, ir_value **out)
2427 compile_error(self->m_context, "argpipe node: not an lvalue");
2432 compile_error(self->m_context, "TODO: argpipe codegen not implemented");
2436 bool ast_ifthen_codegen(ast_ifthen *self, ast_function *func, bool lvalue, ir_value **out)
2438 ast_expression_codegen *cgen;
2446 ir_block *ontrue_endblock = nullptr;
2447 ir_block *onfalse_endblock = nullptr;
2448 ir_block *merge = nullptr;
2451 /* We don't output any value, thus also don't care about r/lvalue */
2456 compile_error(self->m_context, "internal error: ast_ifthen cannot be reused, it bears no result!");
2459 self->m_outr = (ir_value*)1;
2461 /* generate the condition */
2462 cgen = self->m_cond->m_codegen;
2463 if (!(*cgen)((ast_expression*)(self->m_cond), func, false, &condval))
2465 /* update the block which will get the jump - because short-logic or ternaries may have changed this */
2466 cond = func->m_curblock;
2468 /* try constant folding away the condition */
2469 if ((folded = fold::cond_ifthen(condval, func, self)) != -1)
2472 if (self->m_on_true) {
2473 /* create on-true block */
2474 ontrue = ir_function_create_block(self->m_context, func->m_ir_func, ast_function_label(func, "ontrue"));
2478 /* enter the block */
2479 func->m_curblock = ontrue;
2482 cgen = self->m_on_true->m_codegen;
2483 if (!(*cgen)((ast_expression*)(self->m_on_true), func, false, &dummy))
2486 /* we now need to work from the current endpoint */
2487 ontrue_endblock = func->m_curblock;
2492 if (self->m_on_false) {
2493 /* create on-false block */
2494 onfalse = ir_function_create_block(self->m_context, func->m_ir_func, ast_function_label(func, "onfalse"));
2498 /* enter the block */
2499 func->m_curblock = onfalse;
2502 cgen = self->m_on_false->m_codegen;
2503 if (!(*cgen)((ast_expression*)(self->m_on_false), func, false, &dummy))
2506 /* we now need to work from the current endpoint */
2507 onfalse_endblock = func->m_curblock;
2511 /* Merge block were they all merge in to */
2512 if (!ontrue || !onfalse || !ontrue_endblock->m_final || !onfalse_endblock->m_final)
2514 merge = ir_function_create_block(self->m_context, func->m_ir_func, ast_function_label(func, "endif"));
2517 /* add jumps ot the merge block */
2518 if (ontrue && !ontrue_endblock->m_final && !ir_block_create_jump(ontrue_endblock, self->m_context, merge))
2520 if (onfalse && !onfalse_endblock->m_final && !ir_block_create_jump(onfalse_endblock, self->m_context, merge))
2523 /* Now enter the merge block */
2524 func->m_curblock = merge;
2527 /* we create the if here, that way all blocks are ordered :)
2529 if (!ir_block_create_if(cond, self->m_context, condval,
2530 (ontrue ? ontrue : merge),
2531 (onfalse ? onfalse : merge)))
2539 bool ast_ternary_codegen(ast_ternary *self, ast_function *func, bool lvalue, ir_value **out)
2541 ast_expression_codegen *cgen;
2544 ir_value *trueval, *falseval;
2547 ir_block *cond = func->m_curblock;
2548 ir_block *cond_out = nullptr;
2549 ir_block *ontrue, *ontrue_out = nullptr;
2550 ir_block *onfalse, *onfalse_out = nullptr;
2554 /* Ternary can never create an lvalue... */
2558 /* In theory it shouldn't be possible to pass through a node twice, but
2559 * in case we add any kind of optimization pass for the AST itself, it
2560 * may still happen, thus we remember a created ir_value and simply return one
2561 * if it already exists.
2564 *out = self->m_outr;
2568 /* In the following, contraty to ast_ifthen, we assume both paths exist. */
2570 /* generate the condition */
2571 func->m_curblock = cond;
2572 cgen = self->m_cond->m_codegen;
2573 if (!(*cgen)((ast_expression*)(self->m_cond), func, false, &condval))
2575 cond_out = func->m_curblock;
2577 /* try constant folding away the condition */
2578 if ((folded = fold::cond_ternary(condval, func, self)) != -1)
2581 /* create on-true block */
2582 ontrue = ir_function_create_block(self->m_context, func->m_ir_func, ast_function_label(func, "tern_T"));
2587 /* enter the block */
2588 func->m_curblock = ontrue;
2591 cgen = self->m_on_true->m_codegen;
2592 if (!(*cgen)((ast_expression*)(self->m_on_true), func, false, &trueval))
2595 ontrue_out = func->m_curblock;
2598 /* create on-false block */
2599 onfalse = ir_function_create_block(self->m_context, func->m_ir_func, ast_function_label(func, "tern_F"));
2604 /* enter the block */
2605 func->m_curblock = onfalse;
2608 cgen = self->m_on_false->m_codegen;
2609 if (!(*cgen)((ast_expression*)(self->m_on_false), func, false, &falseval))
2612 onfalse_out = func->m_curblock;
2615 /* create merge block */
2616 merge = ir_function_create_block(self->m_context, func->m_ir_func, ast_function_label(func, "tern_out"));
2619 /* jump to merge block */
2620 if (!ir_block_create_jump(ontrue_out, self->m_context, merge))
2622 if (!ir_block_create_jump(onfalse_out, self->m_context, merge))
2625 /* create if instruction */
2626 if (!ir_block_create_if(cond_out, self->m_context, condval, ontrue, onfalse))
2629 /* Now enter the merge block */
2630 func->m_curblock = merge;
2632 /* Here, now, we need a PHI node
2633 * but first some sanity checking...
2635 if (trueval->m_vtype != falseval->m_vtype && trueval->m_vtype != TYPE_NIL && falseval->m_vtype != TYPE_NIL) {
2636 /* error("ternary with different types on the two sides"); */
2637 compile_error(self->m_context, "internal error: ternary operand types invalid");
2642 phi = ir_block_create_phi(merge, self->m_context, ast_function_label(func, "phi"), self->m_vtype);
2644 compile_error(self->m_context, "internal error: failed to generate phi node");
2647 ir_phi_add(phi, ontrue_out, trueval);
2648 ir_phi_add(phi, onfalse_out, falseval);
2650 self->m_outr = ir_phi_value(phi);
2651 *out = self->m_outr;
2653 codegen_output_type(self, *out);
2658 bool ast_loop_codegen(ast_loop *self, ast_function *func, bool lvalue, ir_value **out)
2660 ast_expression_codegen *cgen;
2662 ir_value *dummy = nullptr;
2663 ir_value *precond = nullptr;
2664 ir_value *postcond = nullptr;
2666 /* Since we insert some jumps "late" so we have blocks
2667 * ordered "nicely", we need to keep track of the actual end-blocks
2668 * of expressions to add the jumps to.
2670 ir_block *bbody = nullptr, *end_bbody = nullptr;
2671 ir_block *bprecond = nullptr, *end_bprecond = nullptr;
2672 ir_block *bpostcond = nullptr, *end_bpostcond = nullptr;
2673 ir_block *bincrement = nullptr, *end_bincrement = nullptr;
2674 ir_block *bout = nullptr, *bin = nullptr;
2676 /* let's at least move the outgoing block to the end */
2679 /* 'break' and 'continue' need to be able to find the right blocks */
2680 ir_block *bcontinue = nullptr;
2681 ir_block *bbreak = nullptr;
2683 ir_block *tmpblock = nullptr;
2689 compile_error(self->m_context, "internal error: ast_loop cannot be reused, it bears no result!");
2692 self->m_outr = (ir_value*)1;
2695 * Should we ever need some kind of block ordering, better make this function
2696 * move blocks around than write a block ordering algorithm later... after all
2697 * the ast and ir should work together, not against each other.
2700 /* initexpr doesn't get its own block, it's pointless, it could create more blocks
2701 * anyway if for example it contains a ternary.
2703 if (self->m_initexpr)
2705 cgen = self->m_initexpr->m_codegen;
2706 if (!(*cgen)((ast_expression*)(self->m_initexpr), func, false, &dummy))
2710 /* Store the block from which we enter this chaos */
2711 bin = func->m_curblock;
2713 /* The pre-loop condition needs its own block since we
2714 * need to be able to jump to the start of that expression.
2716 if (self->m_precond)
2718 bprecond = ir_function_create_block(self->m_context, func->m_ir_func, ast_function_label(func, "pre_loop_cond"));
2722 /* the pre-loop-condition the least important place to 'continue' at */
2723 bcontinue = bprecond;
2726 func->m_curblock = bprecond;
2729 cgen = self->m_precond->m_codegen;
2730 if (!(*cgen)((ast_expression*)(self->m_precond), func, false, &precond))
2733 end_bprecond = func->m_curblock;
2735 bprecond = end_bprecond = nullptr;
2738 /* Now the next blocks won't be ordered nicely, but we need to
2739 * generate them this early for 'break' and 'continue'.
2741 if (self->m_increment) {
2742 bincrement = ir_function_create_block(self->m_context, func->m_ir_func, ast_function_label(func, "loop_increment"));
2745 bcontinue = bincrement; /* increment comes before the pre-loop-condition */
2747 bincrement = end_bincrement = nullptr;
2750 if (self->m_postcond) {
2751 bpostcond = ir_function_create_block(self->m_context, func->m_ir_func, ast_function_label(func, "post_loop_cond"));
2754 bcontinue = bpostcond; /* postcond comes before the increment */
2756 bpostcond = end_bpostcond = nullptr;
2759 bout_id = func->m_ir_func->m_blocks.size();
2760 bout = ir_function_create_block(self->m_context, func->m_ir_func, ast_function_label(func, "after_loop"));
2765 /* The loop body... */
2766 /* if (self->m_body) */
2768 bbody = ir_function_create_block(self->m_context, func->m_ir_func, ast_function_label(func, "loop_body"));
2773 func->m_curblock = bbody;
2775 func->m_breakblocks.push_back(bbreak);
2777 func->m_continueblocks.push_back(bcontinue);
2779 func->m_continueblocks.push_back(bbody);
2783 cgen = self->m_body->m_codegen;
2784 if (!(*cgen)((ast_expression*)(self->m_body), func, false, &dummy))
2788 end_bbody = func->m_curblock;
2789 func->m_breakblocks.pop_back();
2790 func->m_continueblocks.pop_back();
2793 /* post-loop-condition */
2794 if (self->m_postcond)
2797 func->m_curblock = bpostcond;
2800 cgen = self->m_postcond->m_codegen;
2801 if (!(*cgen)((ast_expression*)(self->m_postcond), func, false, &postcond))
2804 end_bpostcond = func->m_curblock;
2807 /* The incrementor */
2808 if (self->m_increment)
2811 func->m_curblock = bincrement;
2814 cgen = self->m_increment->m_codegen;
2815 if (!(*cgen)((ast_expression*)(self->m_increment), func, false, &dummy))
2818 end_bincrement = func->m_curblock;
2821 /* In any case now, we continue from the outgoing block */
2822 func->m_curblock = bout;
2824 /* Now all blocks are in place */
2825 /* From 'bin' we jump to whatever comes first */
2826 if (bprecond) tmpblock = bprecond;
2827 else tmpblock = bbody; /* can never be null */
2830 else if (bpostcond) tmpblock = bpostcond;
2831 else tmpblock = bout;
2834 if (!ir_block_create_jump(bin, self->m_context, tmpblock))
2840 ir_block *ontrue, *onfalse;
2841 ontrue = bbody; /* can never be null */
2843 /* all of this is dead code
2844 else if (bincrement) ontrue = bincrement;
2845 else ontrue = bpostcond;
2849 if (self->m_pre_not) {
2854 if (!ir_block_create_if(end_bprecond, self->m_context, precond, ontrue, onfalse))
2861 if (bincrement) tmpblock = bincrement;
2862 else if (bpostcond) tmpblock = bpostcond;
2863 else if (bprecond) tmpblock = bprecond;
2864 else tmpblock = bbody;
2865 if (!end_bbody->m_final && !ir_block_create_jump(end_bbody, self->m_context, tmpblock))
2869 /* from increment */
2872 if (bpostcond) tmpblock = bpostcond;
2873 else if (bprecond) tmpblock = bprecond;
2874 else if (bbody) tmpblock = bbody;
2875 else tmpblock = bout;
2876 if (!ir_block_create_jump(end_bincrement, self->m_context, tmpblock))
2883 ir_block *ontrue, *onfalse;
2884 if (bprecond) ontrue = bprecond;
2885 else ontrue = bbody; /* can never be null */
2887 /* all of this is dead code
2888 else if (bincrement) ontrue = bincrement;
2889 else ontrue = bpostcond;
2893 if (self->m_post_not) {
2898 if (!ir_block_create_if(end_bpostcond, self->m_context, postcond, ontrue, onfalse))
2902 /* Move 'bout' to the end */
2903 algo::shiftback(func->m_ir_func->m_blocks.begin() + bout_id,
2904 func->m_ir_func->m_blocks.end());
2906 //func->m_ir_func->m_blocks[bout_id].release(); // it's a vector<unique_ptr<>>
2907 //func->m_ir_func->m_blocks.erase(func->m_ir_func->m_blocks.begin() + bout_id);
2908 //func->m_ir_func->m_blocks.emplace_back(bout);
2913 bool ast_breakcont_codegen(ast_breakcont *self, ast_function *func, bool lvalue, ir_value **out)
2920 compile_error(self->m_context, "break/continue expression is not an l-value");
2925 compile_error(self->m_context, "internal error: ast_breakcont cannot be reused!");
2928 self->m_outr = (ir_value*)1;
2930 if (self->m_is_continue)
2931 target = func->m_continueblocks[func->m_continueblocks.size()-1-self->m_levels];
2933 target = func->m_breakblocks[func->m_breakblocks.size()-1-self->m_levels];
2936 compile_error(self->m_context, "%s is lacking a target block", (self->m_is_continue ? "continue" : "break"));
2940 if (!ir_block_create_jump(func->m_curblock, self->m_context, target))
2945 bool ast_switch_codegen(ast_switch *self, ast_function *func, bool lvalue, ir_value **out)
2947 ast_expression_codegen *cgen;
2949 ast_switch_case *def_case = nullptr;
2950 ir_block *def_bfall = nullptr;
2951 ir_block *def_bfall_to = nullptr;
2952 bool set_def_bfall_to = false;
2954 ir_value *dummy = nullptr;
2955 ir_value *irop = nullptr;
2956 ir_block *bout = nullptr;
2957 ir_block *bfall = nullptr;
2964 compile_error(self->m_context, "switch expression is not an l-value");
2969 compile_error(self->m_context, "internal error: ast_switch cannot be reused!");
2972 self->m_outr = (ir_value*)1;
2977 cgen = self->m_operand->m_codegen;
2978 if (!(*cgen)((ast_expression*)(self->m_operand), func, false, &irop))
2981 if (self->m_cases.empty())
2984 cmpinstr = type_eq_instr[irop->m_vtype];
2985 if (cmpinstr >= VINSTR_END) {
2986 ast_type_to_string(self->m_operand, typestr, sizeof(typestr));
2987 compile_error(self->m_context, "invalid type to perform a switch on: %s", typestr);
2991 bout_id = func->m_ir_func->m_blocks.size();
2992 bout = ir_function_create_block(self->m_context, func->m_ir_func, ast_function_label(func, "after_switch"));
2996 /* setup the break block */
2997 func->m_breakblocks.push_back(bout);
2999 /* Now create all cases */
3000 for (auto &it : self->m_cases) {
3001 ir_value *cond, *val;
3002 ir_block *bcase, *bnot;
3005 ast_switch_case *swcase = ⁢
3007 if (swcase->m_value) {
3008 /* A regular case */
3009 /* generate the condition operand */
3010 cgen = swcase->m_value->m_codegen;
3011 if (!(*cgen)((ast_expression*)(swcase->m_value), func, false, &val))
3013 /* generate the condition */
3014 cond = ir_block_create_binop(func->m_curblock, self->m_context, ast_function_label(func, "switch_eq"), cmpinstr, irop, val);
3018 bcase = ir_function_create_block(self->m_context, func->m_ir_func, ast_function_label(func, "case"));
3019 bnot_id = func->m_ir_func->m_blocks.size();
3020 bnot = ir_function_create_block(self->m_context, func->m_ir_func, ast_function_label(func, "not_case"));
3021 if (!bcase || !bnot)
3023 if (set_def_bfall_to) {
3024 set_def_bfall_to = false;
3025 def_bfall_to = bcase;
3027 if (!ir_block_create_if(func->m_curblock, self->m_context, cond, bcase, bnot))
3030 /* Make the previous case-end fall through */
3031 if (bfall && !bfall->m_final) {
3032 if (!ir_block_create_jump(bfall, self->m_context, bcase))
3036 /* enter the case */
3037 func->m_curblock = bcase;
3038 cgen = swcase->m_code->m_codegen;
3039 if (!(*cgen)((ast_expression*)swcase->m_code, func, false, &dummy))
3042 /* remember this block to fall through from */
3043 bfall = func->m_curblock;
3045 /* enter the else and move it down */
3046 func->m_curblock = bnot;
3047 algo::shiftback(func->m_ir_func->m_blocks.begin() + bnot_id,
3048 func->m_ir_func->m_blocks.end());
3050 //func->m_ir_func->m_blocks[bnot_id].release();
3051 //func->m_ir_func->m_blocks.erase(func->m_ir_func->m_blocks.begin() + bnot_id);
3052 //func->m_ir_func->m_blocks.emplace_back(bnot);
3054 /* The default case */
3055 /* Remember where to fall through from: */
3058 /* remember which case it was */
3060 /* And the next case will be remembered */
3061 set_def_bfall_to = true;
3065 /* Jump from the last bnot to bout */
3066 if (bfall && !bfall->m_final && !ir_block_create_jump(bfall, self->m_context, bout)) {
3068 astwarning(bfall->m_context, WARN_???, "missing break after last case");
3073 /* If there was a default case, put it down here */
3077 /* No need to create an extra block */
3078 bcase = func->m_curblock;
3080 /* Insert the fallthrough jump */
3081 if (def_bfall && !def_bfall->m_final) {
3082 if (!ir_block_create_jump(def_bfall, self->m_context, bcase))
3086 /* Now generate the default code */
3087 cgen = def_case->m_code->m_codegen;
3088 if (!(*cgen)((ast_expression*)def_case->m_code, func, false, &dummy))
3091 /* see if we need to fall through */
3092 if (def_bfall_to && !func->m_curblock->m_final)
3094 if (!ir_block_create_jump(func->m_curblock, self->m_context, def_bfall_to))
3099 /* Jump from the last bnot to bout */
3100 if (!func->m_curblock->m_final && !ir_block_create_jump(func->m_curblock, self->m_context, bout))
3102 /* enter the outgoing block */
3103 func->m_curblock = bout;
3105 /* restore the break block */
3106 func->m_breakblocks.pop_back();
3108 /* Move 'bout' to the end, it's nicer */
3109 algo::shiftback(func->m_ir_func->m_blocks.begin() + bout_id,
3110 func->m_ir_func->m_blocks.end());
3112 //func->m_ir_func->m_blocks[bout_id].release();
3113 //func->m_ir_func->m_blocks.erase(func->m_ir_func->m_blocks.begin() + bout_id);
3114 //func->m_ir_func->m_blocks.emplace_back(bout);
3119 bool ast_label_codegen(ast_label *self, ast_function *func, bool lvalue, ir_value **out)
3123 if (self->m_undefined) {
3124 compile_error(self->m_context, "internal error: ast_label never defined");
3130 compile_error(self->m_context, "internal error: ast_label cannot be an lvalue");
3134 /* simply create a new block and jump to it */
3135 self->m_irblock = ir_function_create_block(self->m_context, func->m_ir_func, self->m_name);
3136 if (!self->m_irblock) {
3137 compile_error(self->m_context, "failed to allocate label block `%s`", self->m_name);
3140 if (!func->m_curblock->m_final) {
3141 if (!ir_block_create_jump(func->m_curblock, self->m_context, self->m_irblock))
3145 /* enter the new block */
3146 func->m_curblock = self->m_irblock;
3148 /* Generate all the leftover gotos */
3149 for (auto &it : self->m_gotos) {
3150 if (!ast_goto_codegen(it, func, false, &dummy))
3157 bool ast_goto_codegen(ast_goto *self, ast_function *func, bool lvalue, ir_value **out)
3161 compile_error(self->m_context, "internal error: ast_goto cannot be an lvalue");
3165 if (self->m_target->m_irblock) {
3166 if (self->m_irblock_from) {
3167 /* we already tried once, this is the callback */
3168 self->m_irblock_from->m_final = false;
3169 if (!ir_block_create_goto(self->m_irblock_from, self->m_context, self->m_target->m_irblock)) {
3170 compile_error(self->m_context, "failed to generate goto to `%s`", self->m_name);
3176 if (!ir_block_create_goto(func->m_curblock, self->m_context, self->m_target->m_irblock)) {
3177 compile_error(self->m_context, "failed to generate goto to `%s`", self->m_name);
3184 /* the target has not yet been created...
3185 * close this block in a sneaky way:
3187 func->m_curblock->m_final = true;
3188 self->m_irblock_from = func->m_curblock;
3189 ast_label_register_goto(self->m_target, self);
3196 bool ast_state_codegen(ast_state *self, ast_function *func, bool lvalue, ir_value **out)
3198 ast_expression_codegen *cgen;
3200 ir_value *frameval, *thinkval;
3203 compile_error(self->m_context, "not an l-value (state operation)");
3207 compile_error(self->m_context, "internal error: ast_state cannot be reused!");
3212 cgen = self->m_framenum->m_codegen;
3213 if (!(*cgen)((ast_expression*)(self->m_framenum), func, false, &frameval))
3218 cgen = self->m_nextthink->m_codegen;
3219 if (!(*cgen)((ast_expression*)(self->m_nextthink), func, false, &thinkval))
3224 if (!ir_block_create_state_op(func->m_curblock, self->m_context, frameval, thinkval)) {
3225 compile_error(self->m_context, "failed to create STATE instruction");
3229 self->m_outr = (ir_value*)1;
3233 bool ast_call_codegen(ast_call *self, ast_function *func, bool lvalue, ir_value **out)
3235 ast_expression_codegen *cgen;
3236 std::vector<ir_value*> params;
3237 ir_instr *callinstr;
3239 ir_value *funval = nullptr;
3241 /* return values are never lvalues */
3243 compile_error(self->m_context, "not an l-value (function call)");
3248 *out = self->m_outr;
3252 cgen = self->m_func->m_codegen;
3253 if (!(*cgen)((ast_expression*)(self->m_func), func, false, &funval))
3259 for (auto &it : self->m_params) {
3261 cgen = it->m_codegen;
3262 if (!(*cgen)(it, func, false, ¶m))
3266 params.push_back(param);
3269 /* varargs counter */
3270 if (self->m_va_count) {
3272 ir_builder *builder = func->m_curblock->m_owner->m_owner;
3273 cgen = self->m_va_count->m_codegen;
3274 if (!(*cgen)((ast_expression*)(self->m_va_count), func, false, &va_count))
3276 if (!ir_block_create_store_op(func->m_curblock, self->m_context, INSTR_STORE_F,
3277 ir_builder_get_va_count(builder), va_count))
3283 callinstr = ir_block_create_call(func->m_curblock, self->m_context,
3284 ast_function_label(func, "call"),
3285 funval, !!(self->m_func->m_flags & AST_FLAG_NORETURN));
3289 for (auto &it : params)
3290 ir_call_param(callinstr, it);
3292 *out = ir_call_value(callinstr);
3293 self->m_outr = *out;
3295 codegen_output_type(self, *out);