13 /* Initialize main ast node aprts */
14 ast_node::ast_node(lex_ctx_t ctx, int node_type)
16 , m_node_type(node_type)
18 , m_side_effects(false)
26 /* weight and side effects */
27 void ast_node::propagateSideEffects(ast_node *other) const
29 other->m_side_effects = m_side_effects;
32 /* General expression initialization */
33 ast_expression::ast_expression(lex_ctx_t ctx, int nodetype, qc_type type)
34 : ast_node(ctx, nodetype)
37 if (OPTS_OPTION_BOOL(OPTION_COVERAGE))
38 m_flags |= AST_FLAG_BLOCK_COVERAGE;
40 ast_expression::ast_expression(lex_ctx_t ctx, int nodetype)
41 : ast_expression(ctx, nodetype, TYPE_VOID)
44 ast_expression::~ast_expression()
52 ast_expression::ast_expression(ast_copy_type_t, const ast_expression &other)
53 : ast_expression(ast_copy_type, other.m_context, other)
56 ast_expression::ast_expression(ast_copy_type_t, lex_ctx_t ctx, const ast_expression &other)
57 : ast_expression(ast_copy_type, TYPE_ast_expression, ctx, other)
60 ast_expression::ast_expression(ast_copy_type_t, int nodetype, const ast_expression &other)
61 : ast_expression(ast_copy_type, nodetype, other.m_context, other)
64 ast_expression::ast_expression(ast_copy_type_t, int nodetype, lex_ctx_t ctx, const ast_expression &other)
65 : ast_expression(ctx, nodetype)
67 m_vtype = other.m_vtype;
68 m_count = other.m_count;
69 m_flags = other.m_flags;
71 m_next = new ast_expression(ast_copy_type, *other.m_next);
72 m_type_params.reserve(other.m_type_params.size());
73 for (auto &it : other.m_type_params)
74 m_type_params.emplace_back(new ast_value(ast_copy_type, *it));
78 ast_expression *ast_expression::shallowType(lex_ctx_t ctx, qc_type vtype) {
79 auto expr = new ast_expression(ctx, TYPE_ast_expression);
80 expr->m_vtype = vtype;
84 void ast_expression::adoptType(const ast_expression &other)
86 m_vtype = other.m_vtype;
88 m_next = new ast_expression(ast_copy_type, *other.m_next);
89 m_count = other.m_count;
90 m_flags = other.m_flags;
91 m_type_params.clear();
92 m_type_params.reserve(other.m_type_params.size());
93 for (auto &it : other.m_type_params)
94 m_type_params.emplace_back(new ast_value(ast_copy_type, *it));
97 bool ast_expression::compareType(const ast_expression &other) const
99 if (m_vtype == TYPE_NIL ||
100 other.m_vtype == TYPE_NIL)
102 if (m_vtype != other.m_vtype)
104 if (!m_next != !other.m_next)
106 if (m_type_params.size() != other.m_type_params.size())
108 if ((m_flags & AST_FLAG_TYPE_MASK) !=
109 (other.m_flags & AST_FLAG_TYPE_MASK) )
113 if (m_type_params.size()) {
115 for (i = 0; i < m_type_params.size(); ++i) {
116 if (!m_type_params[i]->compareType(*other.m_type_params[i]))
121 return m_next->compareType(*other.m_next);
125 bool ast_expression::codegen(ast_function*, bool, ir_value**) {
126 compile_error(m_context, "ast_expression::codegen called!");
131 ast_value::ast_value(ast_copy_type_t, const ast_value &other, const std::string &name)
132 : ast_value(ast_copy_type, static_cast<const ast_expression&>(other), name)
134 m_keep_node = true; // keep values, always
135 memset(&m_constval, 0, sizeof(m_constval));
138 ast_value::ast_value(ast_copy_type_t, const ast_value &other)
139 : ast_value(ast_copy_type, static_cast<const ast_expression&>(other), other.m_name)
141 m_keep_node = true; // keep values, always
142 memset(&m_constval, 0, sizeof(m_constval));
145 ast_value::ast_value(ast_copy_type_t, const ast_expression &other, const std::string &name)
146 : ast_expression(ast_copy_type, TYPE_ast_value, other)
149 m_keep_node = true; // keep values, always
150 memset(&m_constval, 0, sizeof(m_constval));
153 ast_value::ast_value(lex_ctx_t ctx, const std::string &name, qc_type t)
154 : ast_expression(ctx, TYPE_ast_value, t)
157 m_keep_node = true; // keep values, always
158 memset(&m_constval, 0, sizeof(m_constval));
161 ast_value::~ast_value()
164 mem_d((void*)m_argcounter);
169 mem_d((void*)m_constval.vstring);
172 // unlink us from the function node
173 m_constval.vfunc->m_function_type = nullptr;
175 // NOTE: delete function? currently collected in
176 // the parser structure
182 // initlist imples an array which implies .next in the expression exists.
183 if (m_initlist.size() && m_next->m_vtype == TYPE_STRING) {
184 for (auto &it : m_initlist)
190 static size_t ast_type_to_string_impl(const ast_expression *e, char *buf, size_t bufsize, size_t pos)
197 if (pos + 6 >= bufsize)
199 util_strncpy(buf + pos, "(null)", 6);
203 if (pos + 1 >= bufsize)
206 switch (e->m_vtype) {
208 util_strncpy(buf + pos, "(variant)", 9);
213 return ast_type_to_string_impl(e->m_next, buf, bufsize, pos);
216 if (pos + 3 >= bufsize)
220 pos = ast_type_to_string_impl(e->m_next, buf, bufsize, pos);
221 if (pos + 1 >= bufsize)
227 pos = ast_type_to_string_impl(e->m_next, buf, bufsize, pos);
228 if (pos + 2 >= bufsize)
230 if (e->m_type_params.empty()) {
236 pos = ast_type_to_string_impl(e->m_type_params[0].get(), buf, bufsize, pos);
237 for (i = 1; i < e->m_type_params.size(); ++i) {
238 if (pos + 2 >= bufsize)
242 pos = ast_type_to_string_impl(e->m_type_params[i].get(), buf, bufsize, pos);
244 if (pos + 1 >= bufsize)
250 pos = ast_type_to_string_impl(e->m_next, buf, bufsize, pos);
251 if (pos + 1 >= bufsize)
254 pos += util_snprintf(buf + pos, bufsize - pos - 1, "%i", (int)e->m_count);
255 if (pos + 1 >= bufsize)
261 typestr = type_name[e->m_vtype];
262 typelen = strlen(typestr);
263 if (pos + typelen >= bufsize)
265 util_strncpy(buf + pos, typestr, typelen);
266 return pos + typelen;
270 buf[bufsize-3] = '.';
271 buf[bufsize-2] = '.';
272 buf[bufsize-1] = '.';
276 void ast_type_to_string(const ast_expression *e, char *buf, size_t bufsize)
278 size_t pos = ast_type_to_string_impl(e, buf, bufsize-1, 0);
282 void ast_value::addParam(ast_value *p)
284 m_type_params.emplace_back(p);
287 ast_binary::ast_binary(lex_ctx_t ctx, int op,
288 ast_expression* left, ast_expression* right)
289 : ast_expression(ctx, TYPE_ast_binary)
291 // m_left/m_right happen after the peephole step right below
292 , m_right_first(false)
294 if (ast_istype(right, ast_unary) && OPTS_OPTIMIZATION(OPTIM_PEEPHOLE)) {
295 ast_unary *unary = ((ast_unary*)right);
296 ast_expression *normal = unary->m_operand;
298 /* make a-(-b) => a + b */
299 if (unary->m_op == VINSTR_NEG_F || unary->m_op == VINSTR_NEG_V) {
300 if (op == INSTR_SUB_F) {
303 ++opts_optimizationcount[OPTIM_PEEPHOLE];
304 } else if (op == INSTR_SUB_V) {
307 ++opts_optimizationcount[OPTIM_PEEPHOLE];
315 propagateSideEffects(left);
316 propagateSideEffects(right);
318 if (op >= INSTR_EQ_F && op <= INSTR_GT)
319 m_vtype = TYPE_FLOAT;
320 else if (op == INSTR_AND || op == INSTR_OR) {
321 if (OPTS_FLAG(PERL_LOGIC))
324 m_vtype = TYPE_FLOAT;
326 else if (op == INSTR_BITAND || op == INSTR_BITOR)
327 m_vtype = TYPE_FLOAT;
328 else if (op == INSTR_MUL_VF || op == INSTR_MUL_FV)
329 m_vtype = TYPE_VECTOR;
330 else if (op == INSTR_MUL_V)
331 m_vtype = TYPE_FLOAT;
333 m_vtype = left->m_vtype;
336 m_refs = AST_REF_ALL;
339 ast_binary::~ast_binary()
341 if (m_refs & AST_REF_LEFT) ast_unref(m_left);
342 if (m_refs & AST_REF_RIGHT) ast_unref(m_right);
345 ast_binstore::ast_binstore(lex_ctx_t ctx, int storop, int mathop,
346 ast_expression* left, ast_expression* right)
347 : ast_expression(ctx, TYPE_ast_binstore)
354 m_side_effects = true;
358 ast_binstore::~ast_binstore()
365 ast_unary* ast_unary::make(lex_ctx_t ctx, int op, ast_expression *expr)
367 if (ast_istype(expr, ast_unary) && OPTS_OPTIMIZATION(OPTIM_PEEPHOLE)) {
368 ast_unary *prev = (ast_unary*)((ast_unary*)expr)->m_operand;
370 /* Handle for double negation */
371 if (((ast_unary*)expr)->m_op == op)
372 prev = (ast_unary*)((ast_unary*)expr)->m_operand;
374 if (ast_istype(prev, ast_unary)) {
375 ++opts_optimizationcount[OPTIM_PEEPHOLE];
380 return new ast_unary(ctx, op, expr);
383 ast_unary::ast_unary(lex_ctx_t ctx, int op, ast_expression *expr)
384 : ast_expression(ctx, TYPE_ast_unary)
388 propagateSideEffects(expr);
389 if ((op >= INSTR_NOT_F && op <= INSTR_NOT_FNC) || op == VINSTR_NEG_F) {
390 m_vtype = TYPE_FLOAT;
391 } else if (op == VINSTR_NEG_V) {
392 m_vtype = TYPE_VECTOR;
394 compile_error(ctx, "cannot determine type of unary operation %s", util_instr_str[op]);
398 ast_unary::~ast_unary()
401 ast_unref(m_operand);
404 ast_return::ast_return(lex_ctx_t ctx, ast_expression *expr)
405 : ast_expression(ctx, TYPE_ast_return)
409 propagateSideEffects(expr);
412 ast_return::~ast_return()
415 ast_unref(m_operand);
418 ast_entfield::ast_entfield(lex_ctx_t ctx, ast_expression *entity, ast_expression *field)
419 : ast_entfield(ctx, entity, field, field->m_next)
421 if (field->m_vtype != TYPE_FIELD)
422 compile_error(ctx, "ast_entfield with expression not of type field");
425 ast_entfield::ast_entfield(lex_ctx_t ctx, ast_expression *entity, ast_expression *field, const ast_expression *outtype)
426 : ast_expression(ctx, TYPE_ast_entfield)
430 propagateSideEffects(m_entity);
431 propagateSideEffects(m_field);
434 compile_error(ctx, "ast_entfield: field has no type");
441 ast_entfield::~ast_entfield()
447 ast_member *ast_member::make(lex_ctx_t ctx, ast_expression *owner, unsigned int field, const std::string &name)
450 compile_error(ctx, "ast_member: invalid field (>=3): %u", field);
453 if (owner->m_vtype != TYPE_VECTOR &&
454 owner->m_vtype != TYPE_FIELD)
456 compile_error(ctx, "member-access on an invalid owner of type %s", type_name[owner->m_vtype]);
459 return new ast_member(ctx, owner, field, name);
462 ast_member::ast_member(lex_ctx_t ctx, ast_expression *owner, unsigned int field, const std::string &name)
463 : ast_expression(ctx, TYPE_ast_member)
471 if (m_owner->m_vtype == TYPE_VECTOR) {
472 m_vtype = TYPE_FLOAT;
475 m_vtype = TYPE_FIELD;
476 m_next = ast_expression::shallowType(ctx, TYPE_FLOAT);
479 propagateSideEffects(owner);
482 ast_member::~ast_member()
484 // The owner is always an ast_value, which has .keep_node=true,
485 // also: ast_members are usually deleted after the owner, thus
486 // this will cause invalid access
487 //ast_unref(self->m_owner);
488 // once we allow (expression).x to access a vector-member, we need
489 // to change this: preferably by creating an alternate ast node for this
490 // purpose that is not garbage-collected.
493 ast_array_index* ast_array_index::make(lex_ctx_t ctx, ast_expression *array, ast_expression *index)
495 ast_expression *outtype = array->m_next;
501 return new ast_array_index(ctx, array, index);
504 ast_array_index::ast_array_index(lex_ctx_t ctx, ast_expression *array, ast_expression *index)
505 : ast_expression(ctx, TYPE_ast_array_index)
509 propagateSideEffects(array);
510 propagateSideEffects(index);
512 ast_expression *outtype = m_array->m_next;
515 if (array->m_vtype == TYPE_FIELD && outtype->m_vtype == TYPE_ARRAY) {
516 // FIXME: investigate - this is not possible after adoptType
517 //if (m_vtype != TYPE_ARRAY) {
518 // compile_error(self->m_context, "array_index node on type");
519 // ast_array_index_delete(self);
524 m_vtype = TYPE_FIELD;
528 ast_array_index::~ast_array_index()
536 ast_argpipe::ast_argpipe(lex_ctx_t ctx, ast_expression *index)
537 : ast_expression(ctx, TYPE_ast_argpipe)
540 m_vtype = TYPE_NOEXPR;
543 ast_argpipe::~ast_argpipe()
549 ast_store::ast_store(lex_ctx_t ctx, int op, ast_expression *dest, ast_expression *source)
550 : ast_expression(ctx, TYPE_ast_store)
555 m_side_effects = true;
559 ast_store::~ast_store()
565 ast_ifthen::ast_ifthen(lex_ctx_t ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
566 : ast_expression(ctx, TYPE_ast_ifthen)
569 , m_on_false(onfalse)
571 propagateSideEffects(cond);
573 propagateSideEffects(ontrue);
575 propagateSideEffects(onfalse);
578 ast_ifthen::~ast_ifthen()
582 ast_unref(m_on_true);
584 ast_unref(m_on_false);
587 ast_ternary::ast_ternary(lex_ctx_t ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
588 : ast_expression(ctx, TYPE_ast_ternary)
591 , m_on_false(onfalse)
593 propagateSideEffects(cond);
594 propagateSideEffects(ontrue);
595 propagateSideEffects(onfalse);
597 if (ontrue->m_vtype == TYPE_NIL)
603 ast_ternary::~ast_ternary()
605 /* the if()s are only there because computed-gotos can set them
608 if (m_cond) ast_unref(m_cond);
609 if (m_on_true) ast_unref(m_on_true);
610 if (m_on_false) ast_unref(m_on_false);
613 ast_loop::ast_loop(lex_ctx_t ctx,
614 ast_expression *initexpr,
615 ast_expression *precond, bool pre_not,
616 ast_expression *postcond, bool post_not,
617 ast_expression *increment,
618 ast_expression *body)
619 : ast_expression(ctx, TYPE_ast_loop)
620 , m_initexpr(initexpr)
622 , m_postcond(postcond)
623 , m_increment(increment)
626 , m_post_not(post_not)
629 propagateSideEffects(initexpr);
631 propagateSideEffects(precond);
633 propagateSideEffects(postcond);
635 propagateSideEffects(increment);
637 propagateSideEffects(body);
640 ast_loop::~ast_loop()
643 ast_unref(m_initexpr);
645 ast_unref(m_precond);
647 ast_unref(m_postcond);
649 ast_unref(m_increment);
654 ast_breakcont::ast_breakcont(lex_ctx_t ctx, bool iscont, unsigned int levels)
655 : ast_expression(ctx, TYPE_ast_breakcont)
656 , m_is_continue(iscont)
661 ast_breakcont::~ast_breakcont()
665 ast_switch::ast_switch(lex_ctx_t ctx, ast_expression *op)
666 : ast_expression(ctx, TYPE_ast_switch)
669 propagateSideEffects(op);
672 ast_switch::~ast_switch()
674 ast_unref(m_operand);
676 for (auto &it : m_cases) {
678 ast_unref(it.m_value);
679 ast_unref(it.m_code);
683 ast_label::ast_label(lex_ctx_t ctx, const std::string &name, bool undefined)
684 : ast_expression(ctx, TYPE_ast_label)
687 , m_undefined(undefined)
689 m_vtype = TYPE_NOEXPR;
692 ast_label::~ast_label()
696 void ast_label::registerGoto(ast_goto *g)
698 m_gotos.push_back(g);
701 ast_goto::ast_goto(lex_ctx_t ctx, const std::string &name)
702 : ast_expression(ctx, TYPE_ast_goto)
705 , m_irblock_from(nullptr)
709 ast_goto::~ast_goto()
713 void ast_goto::setLabel(ast_label *label)
718 ast_state::ast_state(lex_ctx_t ctx, ast_expression *frame, ast_expression *think)
719 : ast_expression(ctx, TYPE_ast_expression)
725 ast_state::~ast_state()
728 ast_unref(m_framenum);
730 ast_unref(m_nextthink);
733 ast_call *ast_call::make(lex_ctx_t ctx, ast_expression *funcexpr)
735 if (!funcexpr->m_next) {
736 compile_error(ctx, "not a function");
739 return new ast_call(ctx, funcexpr);
742 ast_call::ast_call(lex_ctx_t ctx, ast_expression *funcexpr)
743 : ast_expression(ctx, TYPE_ast_call)
745 , m_va_count(nullptr)
747 m_side_effects = true;
748 adoptType(*funcexpr->m_next);
751 ast_call::~ast_call()
753 for (auto &it : m_params)
760 ast_unref(m_va_count);
763 bool ast_call::checkVararg(ast_expression *va_type, ast_expression *exp_type) const
769 if (!va_type || !va_type->compareType(*exp_type))
771 if (va_type && exp_type)
773 ast_type_to_string(va_type, tgot, sizeof(tgot));
774 ast_type_to_string(exp_type, texp, sizeof(texp));
775 if (OPTS_FLAG(UNSAFE_VARARGS)) {
776 if (compile_warning(m_context, WARN_UNSAFE_TYPES,
777 "piped variadic argument differs in type: constrained to type %s, expected type %s",
781 compile_error(m_context,
782 "piped variadic argument differs in type: constrained to type %s, expected type %s",
789 ast_type_to_string(exp_type, texp, sizeof(texp));
790 if (OPTS_FLAG(UNSAFE_VARARGS)) {
791 if (compile_warning(m_context, WARN_UNSAFE_TYPES,
792 "piped variadic argument may differ in type: expected type %s",
796 compile_error(m_context,
797 "piped variadic argument may differ in type: expected type %s",
806 bool ast_call::checkTypes(ast_expression *va_type) const
813 size_t count = m_params.size();
814 if (count > m_func->m_type_params.size())
815 count = m_func->m_type_params.size();
817 for (i = 0; i < count; ++i) {
818 if (ast_istype(m_params[i], ast_argpipe)) {
819 /* warn about type safety instead */
821 compile_error(m_context, "argpipe must be the last parameter to a function call");
824 if (!checkVararg(va_type, m_func->m_type_params[i].get()))
827 else if (!m_params[i]->compareType(*m_func->m_type_params[i]))
829 ast_type_to_string(m_params[i], tgot, sizeof(tgot));
830 ast_type_to_string(m_func->m_type_params[i].get(), texp, sizeof(texp));
831 compile_error(m_context, "invalid type for parameter %u in function call: expected %s, got %s",
832 (unsigned int)(i+1), texp, tgot);
833 /* we don't immediately return */
837 count = m_params.size();
838 if (count > m_func->m_type_params.size() && m_func->m_varparam) {
839 for (; i < count; ++i) {
840 if (ast_istype(m_params[i], ast_argpipe)) {
841 /* warn about type safety instead */
843 compile_error(m_context, "argpipe must be the last parameter to a function call");
846 if (!checkVararg(va_type, m_func->m_varparam))
849 else if (!m_params[i]->compareType(*m_func->m_varparam))
851 ast_type_to_string(m_params[i], tgot, sizeof(tgot));
852 ast_type_to_string(m_func->m_varparam, texp, sizeof(texp));
853 compile_error(m_context, "invalid type for variadic parameter %u in function call: expected %s, got %s",
854 (unsigned int)(i+1), texp, tgot);
855 /* we don't immediately return */
863 ast_block::ast_block(lex_ctx_t ctx)
864 : ast_expression(ctx, TYPE_ast_block)
868 ast_block::~ast_block()
870 for (auto &it : m_exprs) ast_unref(it);
871 for (auto &it : m_locals) delete it;
872 for (auto &it : m_collect) delete it;
875 void ast_block::setType(const ast_expression &from)
883 bool ast_block::addExpr(ast_expression *e)
885 propagateSideEffects(e);
886 m_exprs.push_back(e);
895 void ast_block::collect(ast_expression *expr)
897 m_collect.push_back(expr);
898 expr->m_keep_node = true;
901 ast_function *ast_function::make(lex_ctx_t ctx, const std::string &name, ast_value *vtype)
904 compile_error(ctx, "internal error: ast_function_new condition 0");
906 } else if (vtype->m_hasvalue || vtype->m_vtype != TYPE_FUNCTION) {
907 compile_error(ctx, "internal error: ast_function_new condition %i %i type=%i (probably 2 bodies?)",
909 (int)vtype->m_hasvalue,
913 return new ast_function(ctx, name, vtype);
916 ast_function::ast_function(lex_ctx_t ctx, const std::string &name, ast_value *vtype)
917 : ast_node(ctx, TYPE_ast_function)
918 , m_function_type(vtype)
923 , m_curblock(nullptr)
927 , m_fixedparams(nullptr)
928 , m_return_value(nullptr)
930 vtype->m_hasvalue = true;
931 vtype->m_constval.vfunc = this;
934 ast_function::~ast_function()
936 if (m_function_type) {
937 // ast_value_delete(m_function_type);
938 m_function_type->m_hasvalue = false;
939 m_function_type->m_constval.vfunc = nullptr;
940 // We use unref - if it was stored in a global table it is supposed
941 // to be deleted from *there*
942 ast_unref(m_function_type);
946 ast_unref(m_fixedparams);
948 ast_unref(m_return_value);
951 const char* ast_function::makeLabel(const char *prefix)
957 if (!OPTS_OPTION_BOOL(OPTION_DUMP) &&
958 !OPTS_OPTION_BOOL(OPTION_DUMPFIN) &&
959 !OPTS_OPTION_BOOL(OPTION_DEBUG))
964 id = (m_labelcount++);
965 len = strlen(prefix);
967 from = m_labelbuf + sizeof(m_labelbuf)-1;
970 *from-- = (id%10) + '0';
974 memcpy(from - len, prefix, len);
978 /*********************************************************************/
980 * by convention you must never pass nullptr to the 'ir_value **out'
981 * parameter. If you really don't care about the output, pass a dummy.
982 * But I can't imagine a pituation where the output is truly unnecessary.
985 static void codegen_output_type(ast_expression *self, ir_value *out)
987 if (out->m_vtype == TYPE_FIELD)
988 out->m_fieldtype = self->m_next->m_vtype;
989 if (out->m_vtype == TYPE_FUNCTION)
990 out->m_outtype = self->m_next->m_vtype;
993 bool ast_value::codegen(ast_function *func, bool lvalue, ir_value **out)
997 if (m_vtype == TYPE_NIL) {
998 *out = func->m_ir_func->m_owner->m_nil;
1001 // NOTE: This is the codegen for a variable used in an expression.
1002 // It is not the codegen to generate the value storage. For this purpose,
1003 // generateLocal and generateGlobal are to be used before this
1004 // is executed. ast_function::generateFunction should take care of its
1005 // locals, and the ast-user should take care of generateGlobal to be used
1006 // on all the globals.
1008 char tname[1024]; /* typename is reserved in C++ */
1009 ast_type_to_string(this, tname, sizeof(tname));
1010 compile_error(m_context, "ast_value used before generated %s %s", tname, m_name);
1017 bool ast_value::setGlobalArray()
1019 size_t count = m_initlist.size();
1022 if (count > m_count) {
1023 compile_error(m_context, "too many elements in initializer");
1026 else if (count < m_count) {
1028 compile_warning(m_context, "not all elements are initialized");
1032 for (i = 0; i != count; ++i) {
1033 switch (m_next->m_vtype) {
1035 if (!ir_value_set_float(m_ir_values[i], m_initlist[i].vfloat))
1039 if (!ir_value_set_vector(m_ir_values[i], m_initlist[i].vvec))
1043 if (!ir_value_set_string(m_ir_values[i], m_initlist[i].vstring))
1047 /* we don't support them in any other place yet either */
1048 compile_error(m_context, "TODO: nested arrays");
1051 /* this requiers a bit more work - similar to the fields I suppose */
1052 compile_error(m_context, "global of type function not properly generated");
1055 if (!m_initlist[i].vfield) {
1056 compile_error(m_context, "field constant without vfield set");
1059 if (!m_initlist[i].vfield->m_ir_v) {
1060 compile_error(m_context, "field constant generated before its field");
1063 if (!ir_value_set_field(m_ir_values[i], m_initlist[i].vfield->m_ir_v))
1067 compile_error(m_context, "TODO: global constant type %i", m_vtype);
1074 bool ast_value::checkArray(const ast_value &array) const
1076 if (array.m_flags & AST_FLAG_ARRAY_INIT && array.m_initlist.empty()) {
1077 compile_error(m_context, "array without size: %s", m_name);
1080 // we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements
1081 if (!array.m_count || array.m_count > OPTS_OPTION_U32(OPTION_MAX_ARRAY_SIZE)) {
1082 compile_error(m_context, "Invalid array of size %lu", (unsigned long)array.m_count);
1088 bool ast_value::generateGlobal(ir_builder *ir, bool isfield)
1090 if (m_vtype == TYPE_NIL) {
1091 compile_error(m_context, "internal error: trying to generate a variable of TYPE_NIL");
1095 if (m_hasvalue && m_vtype == TYPE_FUNCTION)
1096 return generateGlobalFunction(ir);
1098 if (isfield && m_vtype == TYPE_FIELD)
1099 return generateGlobalField(ir);
1101 ir_value *v = nullptr;
1102 if (m_vtype == TYPE_ARRAY) {
1103 v = prepareGlobalArray(ir);
1107 // Arrays don't do this since there's no "array" value which spans across the
1109 v = ir_builder_create_global(ir, m_name, m_vtype);
1111 compile_error(m_context, "ir_builder_create_global failed on `%s`", m_name);
1114 codegen_output_type(this, v);
1115 v->m_context = m_context;
1118 /* link us to the ir_value */
1122 if (m_flags & AST_FLAG_INCLUDE_DEF)
1123 m_ir_v->m_flags |= IR_FLAG_INCLUDE_DEF;
1124 if (m_flags & AST_FLAG_ERASEABLE)
1125 m_ir_v->m_flags |= IR_FLAG_ERASABLE;
1132 if (!ir_value_set_float(v, m_constval.vfloat))
1136 if (!ir_value_set_vector(v, m_constval.vvec))
1140 if (!ir_value_set_string(v, m_constval.vstring))
1144 if (!setGlobalArray())
1148 compile_error(m_context, "global of type function not properly generated");
1150 /* Cannot generate an IR value for a function,
1151 * need a pointer pointing to a function rather.
1154 if (!m_constval.vfield) {
1155 compile_error(m_context, "field constant without vfield set");
1158 if (!m_constval.vfield->m_ir_v) {
1159 compile_error(m_context, "field constant generated before its field");
1162 if (!ir_value_set_field(v, m_constval.vfield->m_ir_v))
1166 compile_error(m_context, "TODO: global constant type %i", m_vtype);
1174 bool ast_value::generateGlobalFunction(ir_builder *ir)
1176 ir_function *func = ir_builder_create_function(ir, m_name, m_next->m_vtype);
1179 func->m_context = m_context;
1180 func->m_value->m_context = m_context;
1182 m_constval.vfunc->m_ir_func = func;
1183 m_ir_v = func->m_value;
1184 if (m_flags & AST_FLAG_INCLUDE_DEF)
1185 m_ir_v->m_flags |= IR_FLAG_INCLUDE_DEF;
1186 if (m_flags & AST_FLAG_ERASEABLE)
1187 m_ir_v->m_flags |= IR_FLAG_ERASABLE;
1188 if (m_flags & AST_FLAG_BLOCK_COVERAGE)
1189 func->m_flags |= IR_FLAG_BLOCK_COVERAGE;
1190 // The function is filled later on ast_function::generateFunction...
1194 bool ast_value::generateGlobalField(ir_builder *ir)
1196 ast_expression *fieldtype = m_next;
1199 compile_error(m_context, "TODO: constant field pointers with value");
1203 if (fieldtype->m_vtype == TYPE_ARRAY) {
1204 if (!ast_istype(fieldtype, ast_value)) {
1205 compile_error(m_context, "internal error: ast_value required");
1208 ast_value *array = reinterpret_cast<ast_value*>(fieldtype);
1210 if (!checkArray(*array))
1213 ast_expression *elemtype = array->m_next;
1214 qc_type vtype = elemtype->m_vtype;
1216 ir_value *v = ir_builder_create_field(ir, m_name, vtype);
1218 compile_error(m_context, "ir_builder_create_global failed on `%s`", m_name);
1221 v->m_context = m_context;
1222 v->m_unique_life = true;
1224 array->m_ir_v = m_ir_v = v;
1226 if (m_flags & AST_FLAG_INCLUDE_DEF)
1227 m_ir_v->m_flags |= IR_FLAG_INCLUDE_DEF;
1228 if (m_flags & AST_FLAG_ERASEABLE)
1229 m_ir_v->m_flags |= IR_FLAG_ERASABLE;
1231 const size_t namelen = m_name.length();
1232 std::unique_ptr<char[]> name(new char[namelen+16]);
1233 util_strncpy(name.get(), m_name.c_str(), namelen);
1235 array->m_ir_values.resize(array->m_count);
1236 array->m_ir_values[0] = v;
1237 for (size_t ai = 1; ai < array->m_count; ++ai) {
1238 util_snprintf(name.get() + namelen, 16, "[%u]", (unsigned int)ai);
1239 array->m_ir_values[ai] = ir_builder_create_field(ir, name.get(), vtype);
1240 if (!array->m_ir_values[ai]) {
1241 compile_error(m_context, "ir_builder_create_global failed on `%s`", name.get());
1244 array->m_ir_values[ai]->m_context = m_context;
1245 array->m_ir_values[ai]->m_unique_life = true;
1246 array->m_ir_values[ai]->m_locked = true;
1247 if (m_flags & AST_FLAG_INCLUDE_DEF)
1248 m_ir_values[ai]->m_flags |= IR_FLAG_INCLUDE_DEF;
1253 ir_value *v = ir_builder_create_field(ir, m_name, m_next->m_vtype);
1256 v->m_context = m_context;
1258 if (m_flags & AST_FLAG_INCLUDE_DEF)
1259 m_ir_v->m_flags |= IR_FLAG_INCLUDE_DEF;
1261 if (m_flags & AST_FLAG_ERASEABLE)
1262 m_ir_v->m_flags |= IR_FLAG_ERASABLE;
1267 ir_value *ast_value::prepareGlobalArray(ir_builder *ir)
1269 ast_expression *elemtype = m_next;
1270 qc_type vtype = elemtype->m_vtype;
1272 if (m_flags & AST_FLAG_ARRAY_INIT && !m_count) {
1273 compile_error(m_context, "array `%s' has no size", m_name);
1277 /* same as with field arrays */
1278 if (!checkArray(*this))
1281 ir_value *v = ir_builder_create_global(ir, m_name, vtype);
1283 compile_error(m_context, "ir_builder_create_global failed `%s`", m_name);
1286 v->m_context = m_context;
1287 v->m_unique_life = true;
1290 if (m_flags & AST_FLAG_INCLUDE_DEF)
1291 v->m_flags |= IR_FLAG_INCLUDE_DEF;
1292 if (m_flags & AST_FLAG_ERASEABLE)
1293 m_ir_v->m_flags |= IR_FLAG_ERASABLE;
1295 const size_t namelen = m_name.length();
1296 std::unique_ptr<char[]> name(new char[namelen+16]);
1297 util_strncpy(name.get(), m_name.c_str(), namelen);
1299 m_ir_values.resize(m_count);
1301 for (size_t ai = 1; ai < m_count; ++ai) {
1302 util_snprintf(name.get() + namelen, 16, "[%u]", (unsigned int)ai);
1303 m_ir_values[ai] = ir_builder_create_global(ir, name.get(), vtype);
1304 if (!m_ir_values[ai]) {
1305 compile_error(m_context, "ir_builder_create_global failed `%s`", name.get());
1308 m_ir_values[ai]->m_context = m_context;
1309 m_ir_values[ai]->m_unique_life = true;
1310 m_ir_values[ai]->m_locked = true;
1311 if (m_flags & AST_FLAG_INCLUDE_DEF)
1312 m_ir_values[ai]->m_flags |= IR_FLAG_INCLUDE_DEF;
1318 bool ast_value::generateLocal(ir_function *func, bool param)
1320 if (m_vtype == TYPE_NIL) {
1321 compile_error(m_context, "internal error: trying to generate a variable of TYPE_NIL");
1325 if (m_hasvalue && m_vtype == TYPE_FUNCTION)
1327 /* Do we allow local functions? I think not...
1328 * this is NOT a function pointer atm.
1333 ir_value *v = nullptr;
1334 if (m_vtype == TYPE_ARRAY) {
1335 ast_expression *elemtype = m_next;
1336 qc_type vtype = elemtype->m_vtype;
1338 func->m_flags |= IR_FLAG_HAS_ARRAYS;
1340 if (param && !(m_flags & AST_FLAG_IS_VARARG)) {
1341 compile_error(m_context, "array-parameters are not supported");
1345 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1346 if (!checkArray(*this))
1349 m_ir_values.resize(m_count);
1350 v = ir_function_create_local(func, m_name, vtype, param);
1352 compile_error(m_context, "internal error: ir_function_create_local failed");
1355 v->m_context = m_context;
1356 v->m_unique_life = true;
1359 const size_t namelen = m_name.length();
1360 std::unique_ptr<char[]> name(new char[namelen+16]);
1361 util_strncpy(name.get(), m_name.c_str(), namelen);
1364 for (size_t ai = 1; ai < m_count; ++ai) {
1365 util_snprintf(name.get() + namelen, 16, "[%u]", (unsigned int)ai);
1366 m_ir_values[ai] = ir_function_create_local(func, name.get(), vtype, param);
1367 if (!m_ir_values[ai]) {
1368 compile_error(m_context, "internal_error: ir_builder_create_global failed on `%s`", name.get());
1371 m_ir_values[ai]->m_context = m_context;
1372 m_ir_values[ai]->m_unique_life = true;
1373 m_ir_values[ai]->m_locked = true;
1378 v = ir_function_create_local(func, m_name, m_vtype, param);
1381 codegen_output_type(this, v);
1382 v->m_context = m_context;
1385 // A constant local... hmmm...
1386 // I suppose the IR will have to deal with this
1391 if (!ir_value_set_float(v, m_constval.vfloat))
1395 if (!ir_value_set_vector(v, m_constval.vvec))
1399 if (!ir_value_set_string(v, m_constval.vstring))
1403 compile_error(m_context, "TODO: global constant type %i", m_vtype);
1408 // link us to the ir_value
1412 if (!generateAccessors(func->m_owner))
1416 error: /* clean up */
1421 bool ast_value::generateAccessors(ir_builder *ir)
1424 bool warn = OPTS_WARN(WARN_USED_UNINITIALIZED);
1425 if (!m_setter || !m_getter)
1427 if (m_count && m_ir_values.empty()) {
1428 compile_error(m_context, "internal error: no array values generated for `%s`", m_name);
1431 for (i = 0; i < m_count; ++i) {
1432 if (!m_ir_values[i]) {
1433 compile_error(m_context, "internal error: not all array values have been generated for `%s`", m_name);
1436 if (!m_ir_values[i]->m_life.empty()) {
1437 compile_error(m_context, "internal error: function containing `%s` already generated", m_name);
1442 opts_set(opts.warn, WARN_USED_UNINITIALIZED, false);
1444 if (!m_setter->generateGlobal(ir, false) ||
1445 !m_setter->m_constval.vfunc->generateFunction(ir) ||
1446 !ir_function_finalize(m_setter->m_constval.vfunc->m_ir_func))
1448 compile_error(m_context, "internal error: failed to generate setter for `%s`", m_name);
1449 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1454 if (!m_getter->generateGlobal(ir, false) ||
1455 !m_getter->m_constval.vfunc->generateFunction(ir) ||
1456 !ir_function_finalize(m_getter->m_constval.vfunc->m_ir_func))
1458 compile_error(m_context, "internal error: failed to generate getter for `%s`", m_name);
1459 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1463 for (i = 0; i < m_count; ++i)
1464 m_ir_values[i]->m_life.clear();
1465 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1469 bool ast_function::generateFunction(ir_builder *ir)
1475 ir_function *irf = m_ir_func;
1477 compile_error(m_context, "internal error: ast_function's related ast_value was not generated yet");
1481 /* fill the parameter list */
1482 for (auto &it : m_function_type->m_type_params) {
1483 if (it->m_vtype == TYPE_FIELD)
1484 vec_push(irf->m_params, it->m_next->m_vtype);
1486 vec_push(irf->m_params, it->m_vtype);
1488 if (!it->generateLocal(m_ir_func, true))
1494 if (!m_varargs->generateLocal(m_ir_func, true))
1496 irf->m_max_varargs = m_varargs->m_count;
1500 irf->m_builtin = m_builtin;
1504 /* have a local return value variable? */
1505 if (m_return_value) {
1506 if (!m_return_value->generateLocal(m_ir_func, false))
1510 if (m_blocks.empty()) {
1511 compile_error(m_context, "function `%s` has no body", m_name);
1515 irf->m_first = m_curblock = ir_function_create_block(m_context, irf, "entry");
1517 compile_error(m_context, "failed to allocate entry block for `%s`", m_name);
1525 if (!m_argc->generateLocal(m_ir_func, true))
1527 if (!m_argc->codegen(this, false, &va_count))
1529 if (!m_fixedparams->codegen(this, false, &fixed))
1531 sub = ir_block_create_binop(m_curblock, m_context,
1532 makeLabel("va_count"), INSTR_SUB_F,
1533 ir_builder_get_va_count(ir), fixed);
1536 if (!ir_block_create_store_op(m_curblock, m_context, INSTR_STORE_F,
1543 for (auto &it : m_blocks) {
1544 if (!it->codegen(this, false, &dummy))
1548 /* TODO: check return types */
1549 if (!m_curblock->m_final)
1551 if (!m_function_type->m_next ||
1552 m_function_type->m_next->m_vtype == TYPE_VOID)
1554 return ir_block_create_return(m_curblock, m_context, nullptr);
1556 else if (vec_size(m_curblock->m_entries) || m_curblock == irf->m_first)
1558 if (m_return_value) {
1559 if (!m_return_value->codegen(this, false, &dummy))
1561 return ir_block_create_return(m_curblock, m_context, dummy);
1563 else if (compile_warning(m_context, WARN_MISSING_RETURN_VALUES,
1564 "control reaches end of non-void function (`%s`) via %s",
1565 m_name.c_str(), m_curblock->m_label.c_str()))
1569 return ir_block_create_return(m_curblock, m_context, nullptr);
1575 static bool starts_a_label(const ast_expression *ex)
1577 while (ex && ast_istype(ex, ast_block)) {
1578 auto b = reinterpret_cast<const ast_block*>(ex);
1583 return ast_istype(ex, ast_label);
1586 /* Note, you will not see ast_block_codegen generate ir_blocks.
1587 * To the AST and the IR, blocks are 2 different things.
1588 * In the AST it represents a block of code, usually enclosed in
1589 * curly braces {...}.
1590 * While in the IR it represents a block in terms of control-flow.
1592 bool ast_block::codegen(ast_function *func, bool lvalue, ir_value **out)
1594 /* We don't use this
1595 * Note: an ast-representation using the comma-operator
1596 * of the form: (a, b, c) = x should not assign to c...
1599 compile_error(m_context, "not an l-value (code-block)");
1608 /* output is nullptr at first, we'll have each expression
1609 * assign to out output, thus, a comma-operator represention
1610 * using an ast_block will return the last generated value,
1611 * so: (b, c) + a executed both b and c, and returns c,
1612 * which is then added to a.
1616 /* generate locals */
1617 for (auto &it : m_locals) {
1618 if (!it->generateLocal(func->m_ir_func, false)) {
1619 if (OPTS_OPTION_BOOL(OPTION_DEBUG))
1620 compile_error(m_context, "failed to generate local `%s`", it->m_name);
1625 for (auto &it : m_exprs) {
1626 if (func->m_curblock->m_final && !starts_a_label(it)) {
1627 if (compile_warning(it->m_context, WARN_UNREACHABLE_CODE, "unreachable statement"))
1631 if (!it->codegen(func, false, out))
1640 bool ast_store::codegen(ast_function *func, bool lvalue, ir_value **out)
1642 ir_value *left = nullptr;
1643 ir_value *right = nullptr;
1646 ast_array_index *ai = nullptr;
1648 if (lvalue && m_outl) {
1653 if (!lvalue && m_outr) {
1658 if (ast_istype(m_dest, ast_array_index))
1661 ai = (ast_array_index*)m_dest;
1662 idx = (ast_value*)ai->m_index;
1664 if (ast_istype(ai->m_index, ast_value) && idx->m_hasvalue && idx->m_cvq == CV_CONST)
1669 /* we need to call the setter */
1670 ir_value *iridx, *funval;
1674 compile_error(m_context, "array-subscript assignment cannot produce lvalues");
1678 auto arr = reinterpret_cast<ast_value*>(ai->m_array);
1679 if (!ast_istype(ai->m_array, ast_value) || !arr->m_setter) {
1680 compile_error(m_context, "value has no setter (%s)", arr->m_name);
1684 if (!idx->codegen(func, false, &iridx))
1687 if (!arr->m_setter->codegen(func, true, &funval))
1690 if (!m_source->codegen(func, false, &right))
1693 call = ir_block_create_call(func->m_curblock, m_context, func->makeLabel("store"), funval, false);
1696 ir_call_param(call, iridx);
1697 ir_call_param(call, right);
1705 if (!m_dest->codegen(func, true, &left))
1710 if (!m_source->codegen(func, false, &right))
1713 if (!ir_block_create_store_op(func->m_curblock, m_context, m_op, left, right))
1718 /* Theoretically, an assinment returns its left side as an
1719 * lvalue, if we don't need an lvalue though, we return
1720 * the right side as an rvalue, otherwise we have to
1721 * somehow know whether or not we need to dereference the pointer
1722 * on the left side - that is: OP_LOAD if it was an address.
1723 * Also: in original QC we cannot OP_LOADP *anyway*.
1725 *out = (lvalue ? left : right);
1730 bool ast_binary::codegen(ast_function *func, bool lvalue, ir_value **out)
1732 ir_value *left, *right;
1734 /* A binary operation cannot yield an l-value */
1736 compile_error(m_context, "not an l-value (binop)");
1745 if ((OPTS_FLAG(SHORT_LOGIC) || OPTS_FLAG(PERL_LOGIC)) &&
1746 (m_op == INSTR_AND || m_op == INSTR_OR))
1748 /* NOTE: The short-logic path will ignore right_first */
1750 /* short circuit evaluation */
1751 ir_block *other, *merge;
1752 ir_block *from_left, *from_right;
1756 /* prepare end-block */
1757 merge_id = func->m_ir_func->m_blocks.size();
1758 merge = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("sce_merge"));
1760 /* generate the left expression */
1761 if (!m_left->codegen(func, false, &left))
1763 /* remember the block */
1764 from_left = func->m_curblock;
1766 /* create a new block for the right expression */
1767 other = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("sce_other"));
1768 if (m_op == INSTR_AND) {
1769 /* on AND: left==true -> other */
1770 if (!ir_block_create_if(func->m_curblock, m_context, left, other, merge))
1773 /* on OR: left==false -> other */
1774 if (!ir_block_create_if(func->m_curblock, m_context, left, merge, other))
1777 /* use the likely flag */
1778 vec_last(func->m_curblock->m_instr)->m_likely = true;
1780 /* enter the right-expression's block */
1781 func->m_curblock = other;
1783 if (!m_right->codegen(func, false, &right))
1785 /* remember block */
1786 from_right = func->m_curblock;
1788 /* jump to the merge block */
1789 if (!ir_block_create_jump(func->m_curblock, m_context, merge))
1792 algo::shiftback(func->m_ir_func->m_blocks.begin() + merge_id,
1793 func->m_ir_func->m_blocks.end());
1795 //func->m_ir_func->m_blocks[merge_id].release();
1796 //func->m_ir_func->m_blocks.erase(func->m_ir_func->m_blocks.begin() + merge_id);
1797 //func->m_ir_func->m_blocks.emplace_back(merge);
1799 func->m_curblock = merge;
1800 phi = ir_block_create_phi(func->m_curblock, m_context,
1801 func->makeLabel("sce_value"),
1803 ir_phi_add(phi, from_left, left);
1804 ir_phi_add(phi, from_right, right);
1805 *out = ir_phi_value(phi);
1809 if (!OPTS_FLAG(PERL_LOGIC)) {
1811 if (OPTS_FLAG(CORRECT_LOGIC) && (*out)->m_vtype == TYPE_VECTOR) {
1812 *out = ir_block_create_unary(func->m_curblock, m_context,
1813 func->makeLabel("sce_bool_v"),
1817 *out = ir_block_create_unary(func->m_curblock, m_context,
1818 func->makeLabel("sce_bool"),
1823 else if (OPTS_FLAG(FALSE_EMPTY_STRINGS) && (*out)->m_vtype == TYPE_STRING) {
1824 *out = ir_block_create_unary(func->m_curblock, m_context,
1825 func->makeLabel("sce_bool_s"),
1829 *out = ir_block_create_unary(func->m_curblock, m_context,
1830 func->makeLabel("sce_bool"),
1836 *out = ir_block_create_binop(func->m_curblock, m_context,
1837 func->makeLabel("sce_bool"),
1838 INSTR_AND, *out, *out);
1845 codegen_output_type(this, *out);
1849 if (m_right_first) {
1850 if (!m_right->codegen(func, false, &right))
1852 if (!m_left->codegen(func, false, &left))
1855 if (!m_left->codegen(func, false, &left))
1857 if (!m_right->codegen(func, false, &right))
1861 *out = ir_block_create_binop(func->m_curblock, m_context, func->makeLabel("bin"),
1866 codegen_output_type(this, *out);
1871 bool ast_binstore::codegen(ast_function *func, bool lvalue, ir_value **out)
1873 ir_value *leftl = nullptr, *leftr, *right, *bin;
1877 ast_array_index *ai = nullptr;
1878 ir_value *iridx = nullptr;
1880 if (lvalue && m_outl) {
1885 if (!lvalue && m_outr) {
1890 if (ast_istype(m_dest, ast_array_index))
1893 ai = (ast_array_index*)m_dest;
1894 idx = (ast_value*)ai->m_index;
1896 if (ast_istype(ai->m_index, ast_value) && idx->m_hasvalue && idx->m_cvq == CV_CONST)
1900 /* for a binstore we need both an lvalue and an rvalue for the left side */
1901 /* rvalue of destination! */
1903 if (!idx->codegen(func, false, &iridx))
1906 if (!m_dest->codegen(func, false, &leftr))
1909 /* source as rvalue only */
1910 if (!m_source->codegen(func, false, &right))
1913 /* now the binary */
1914 bin = ir_block_create_binop(func->m_curblock, m_context, func->makeLabel("binst"),
1915 m_opbin, leftr, right);
1919 /* we need to call the setter */
1924 compile_error(m_context, "array-subscript assignment cannot produce lvalues");
1928 arr = (ast_value*)ai->m_array;
1929 if (!ast_istype(ai->m_array, ast_value) || !arr->m_setter) {
1930 compile_error(m_context, "value has no setter (%s)", arr->m_name);
1934 if (!arr->m_setter->codegen(func, true, &funval))
1937 call = ir_block_create_call(func->m_curblock, m_context, func->makeLabel("store"), funval, false);
1940 ir_call_param(call, iridx);
1941 ir_call_param(call, bin);
1945 // lvalue of destination
1946 if (!m_dest->codegen(func, true, &leftl))
1950 if (!ir_block_create_store_op(func->m_curblock, m_context, m_opstore, leftl, bin))
1955 /* Theoretically, an assinment returns its left side as an
1956 * lvalue, if we don't need an lvalue though, we return
1957 * the right side as an rvalue, otherwise we have to
1958 * somehow know whether or not we need to dereference the pointer
1959 * on the left side - that is: OP_LOAD if it was an address.
1960 * Also: in original QC we cannot OP_LOADP *anyway*.
1962 *out = (lvalue ? leftl : bin);
1967 bool ast_unary::codegen(ast_function *func, bool lvalue, ir_value **out)
1971 /* An unary operation cannot yield an l-value */
1973 compile_error(m_context, "not an l-value (binop)");
1983 if (!m_operand->codegen(func, false, &operand))
1986 *out = ir_block_create_unary(func->m_curblock, m_context, func->makeLabel("unary"),
1995 bool ast_return::codegen(ast_function *func, bool lvalue, ir_value **out)
2001 /* In the context of a return operation, we don't actually return
2005 compile_error(m_context, "return-expression is not an l-value");
2010 compile_error(m_context, "internal error: ast_return cannot be reused, it bears no result!");
2013 m_outr = (ir_value*)1;
2017 if (!m_operand->codegen(func, false, &operand))
2020 if (!ir_block_create_return(func->m_curblock, m_context, operand))
2023 if (!ir_block_create_return(func->m_curblock, m_context, nullptr))
2030 bool ast_entfield::codegen(ast_function *func, bool lvalue, ir_value **out)
2032 ir_value *ent, *field;
2034 // This function needs to take the 'lvalue' flag into account!
2035 // As lvalue we provide a field-pointer, as rvalue we provide the
2038 if (lvalue && m_outl) {
2043 if (!lvalue && m_outr) {
2048 if (!m_entity->codegen(func, false, &ent))
2051 if (!m_field->codegen(func, false, &field))
2056 *out = ir_block_create_fieldaddress(func->m_curblock, m_context, func->makeLabel("efa"),
2059 *out = ir_block_create_load_from_ent(func->m_curblock, m_context, func->makeLabel("efv"),
2060 ent, field, m_vtype);
2061 /* Done AFTER error checking:
2062 codegen_output_type(this, *out);
2066 compile_error(m_context, "failed to create %s instruction (output type %s)",
2067 (lvalue ? "ADDRESS" : "FIELD"),
2068 type_name[m_vtype]);
2072 codegen_output_type(this, *out);
2079 // Hm that should be it...
2083 bool ast_member::codegen(ast_function *func, bool lvalue, ir_value **out)
2087 /* in QC this is always an lvalue */
2088 if (lvalue && m_rvalue) {
2089 compile_error(m_context, "not an l-value (member access)");
2097 if (!m_owner->codegen(func, false, &vec))
2100 if (vec->m_vtype != TYPE_VECTOR &&
2101 !(vec->m_vtype == TYPE_FIELD && m_owner->m_next->m_vtype == TYPE_VECTOR))
2106 *out = ir_value_vector_member(vec, m_field);
2109 return (*out != nullptr);
2112 bool ast_array_index::codegen(ast_function *func, bool lvalue, ir_value **out)
2117 if (!lvalue && m_outr) {
2121 if (lvalue && m_outl) {
2126 if (!ast_istype(m_array, ast_value)) {
2127 compile_error(m_context, "array indexing this way is not supported");
2128 /* note this would actually be pointer indexing because the left side is
2129 * not an actual array but (hopefully) an indexable expression.
2130 * Once we get integer arithmetic, and GADDRESS/GSTORE/GLOAD instruction
2131 * support this path will be filled.
2136 arr = reinterpret_cast<ast_value*>(m_array);
2137 idx = reinterpret_cast<ast_value*>(m_index);
2139 if (!ast_istype(m_index, ast_value) || !idx->m_hasvalue || idx->m_cvq != CV_CONST) {
2140 /* Time to use accessor functions */
2141 ir_value *iridx, *funval;
2145 compile_error(m_context, "(.2) array indexing here needs a compile-time constant");
2149 if (!arr->m_getter) {
2150 compile_error(m_context, "value has no getter, don't know how to index it");
2154 if (!m_index->codegen(func, false, &iridx))
2157 if (!arr->m_getter->codegen(func, true, &funval))
2160 call = ir_block_create_call(func->m_curblock, m_context, func->makeLabel("fetch"), funval, false);
2163 ir_call_param(call, iridx);
2165 *out = ir_call_value(call);
2167 (*out)->m_vtype = m_vtype;
2168 codegen_output_type(this, *out);
2172 if (idx->m_vtype == TYPE_FLOAT) {
2173 unsigned int arridx = idx->m_constval.vfloat;
2174 if (arridx >= m_array->m_count)
2176 compile_error(m_context, "array index out of bounds: %i", arridx);
2179 *out = arr->m_ir_values[arridx];
2181 else if (idx->m_vtype == TYPE_INTEGER) {
2182 unsigned int arridx = idx->m_constval.vint;
2183 if (arridx >= m_array->m_count)
2185 compile_error(m_context, "array index out of bounds: %i", arridx);
2188 *out = arr->m_ir_values[arridx];
2191 compile_error(m_context, "array indexing here needs an integer constant");
2194 (*out)->m_vtype = m_vtype;
2195 codegen_output_type(this, *out);
2199 bool ast_argpipe::codegen(ast_function *func, bool lvalue, ir_value **out)
2203 compile_error(m_context, "argpipe node: not an lvalue");
2208 compile_error(m_context, "TODO: argpipe codegen not implemented");
2212 bool ast_ifthen::codegen(ast_function *func, bool lvalue, ir_value **out)
2220 ir_block *ontrue_endblock = nullptr;
2221 ir_block *onfalse_endblock = nullptr;
2222 ir_block *merge = nullptr;
2225 /* We don't output any value, thus also don't care about r/lvalue */
2230 compile_error(m_context, "internal error: ast_ifthen cannot be reused, it bears no result!");
2233 m_outr = (ir_value*)1;
2235 /* generate the condition */
2236 if (!m_cond->codegen(func, false, &condval))
2238 /* update the block which will get the jump - because short-logic or ternaries may have changed this */
2239 cond = func->m_curblock;
2241 /* try constant folding away the condition */
2242 if ((folded = fold::cond_ifthen(condval, func, this)) != -1)
2246 /* create on-true block */
2247 ontrue = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("ontrue"));
2251 /* enter the block */
2252 func->m_curblock = ontrue;
2255 if (!m_on_true->codegen(func, false, &dummy))
2258 /* we now need to work from the current endpoint */
2259 ontrue_endblock = func->m_curblock;
2265 /* create on-false block */
2266 onfalse = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("onfalse"));
2270 /* enter the block */
2271 func->m_curblock = onfalse;
2274 if (!m_on_false->codegen(func, false, &dummy))
2277 /* we now need to work from the current endpoint */
2278 onfalse_endblock = func->m_curblock;
2282 /* Merge block were they all merge in to */
2283 if (!ontrue || !onfalse || !ontrue_endblock->m_final || !onfalse_endblock->m_final)
2285 merge = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("endif"));
2288 /* add jumps ot the merge block */
2289 if (ontrue && !ontrue_endblock->m_final && !ir_block_create_jump(ontrue_endblock, m_context, merge))
2291 if (onfalse && !onfalse_endblock->m_final && !ir_block_create_jump(onfalse_endblock, m_context, merge))
2294 /* Now enter the merge block */
2295 func->m_curblock = merge;
2298 /* we create the if here, that way all blocks are ordered :)
2300 if (!ir_block_create_if(cond, m_context, condval,
2301 (ontrue ? ontrue : merge),
2302 (onfalse ? onfalse : merge)))
2310 bool ast_ternary::codegen(ast_function *func, bool lvalue, ir_value **out)
2313 ir_value *trueval, *falseval;
2316 ir_block *cond = func->m_curblock;
2317 ir_block *cond_out = nullptr;
2318 ir_block *ontrue, *ontrue_out = nullptr;
2319 ir_block *onfalse, *onfalse_out = nullptr;
2323 /* Ternary can never create an lvalue... */
2327 /* In theory it shouldn't be possible to pass through a node twice, but
2328 * in case we add any kind of optimization pass for the AST itself, it
2329 * may still happen, thus we remember a created ir_value and simply return one
2330 * if it already exists.
2337 /* In the following, contraty to ast_ifthen, we assume both paths exist. */
2339 /* generate the condition */
2340 func->m_curblock = cond;
2341 if (!m_cond->codegen(func, false, &condval))
2343 cond_out = func->m_curblock;
2345 /* try constant folding away the condition */
2346 if ((folded = fold::cond_ternary(condval, func, this)) != -1)
2349 /* create on-true block */
2350 ontrue = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("tern_T"));
2355 /* enter the block */
2356 func->m_curblock = ontrue;
2359 if (!m_on_true->codegen(func, false, &trueval))
2362 ontrue_out = func->m_curblock;
2365 /* create on-false block */
2366 onfalse = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("tern_F"));
2371 /* enter the block */
2372 func->m_curblock = onfalse;
2375 if (!m_on_false->codegen(func, false, &falseval))
2378 onfalse_out = func->m_curblock;
2381 /* create merge block */
2382 merge = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("tern_out"));
2385 /* jump to merge block */
2386 if (!ir_block_create_jump(ontrue_out, m_context, merge))
2388 if (!ir_block_create_jump(onfalse_out, m_context, merge))
2391 /* create if instruction */
2392 if (!ir_block_create_if(cond_out, m_context, condval, ontrue, onfalse))
2395 /* Now enter the merge block */
2396 func->m_curblock = merge;
2398 /* Here, now, we need a PHI node
2399 * but first some sanity checking...
2401 if (trueval->m_vtype != falseval->m_vtype && trueval->m_vtype != TYPE_NIL && falseval->m_vtype != TYPE_NIL) {
2402 /* error("ternary with different types on the two sides"); */
2403 compile_error(m_context, "internal error: ternary operand types invalid");
2408 phi = ir_block_create_phi(merge, m_context, func->makeLabel("phi"), m_vtype);
2410 compile_error(m_context, "internal error: failed to generate phi node");
2413 ir_phi_add(phi, ontrue_out, trueval);
2414 ir_phi_add(phi, onfalse_out, falseval);
2416 m_outr = ir_phi_value(phi);
2419 codegen_output_type(this, *out);
2424 bool ast_loop::codegen(ast_function *func, bool lvalue, ir_value **out)
2426 ir_value *dummy = nullptr;
2427 ir_value *precond = nullptr;
2428 ir_value *postcond = nullptr;
2430 /* Since we insert some jumps "late" so we have blocks
2431 * ordered "nicely", we need to keep track of the actual end-blocks
2432 * of expressions to add the jumps to.
2434 ir_block *bbody = nullptr, *end_bbody = nullptr;
2435 ir_block *bprecond = nullptr, *end_bprecond = nullptr;
2436 ir_block *bpostcond = nullptr, *end_bpostcond = nullptr;
2437 ir_block *bincrement = nullptr, *end_bincrement = nullptr;
2438 ir_block *bout = nullptr, *bin = nullptr;
2440 /* let's at least move the outgoing block to the end */
2443 /* 'break' and 'continue' need to be able to find the right blocks */
2444 ir_block *bcontinue = nullptr;
2445 ir_block *bbreak = nullptr;
2447 ir_block *tmpblock = nullptr;
2453 compile_error(m_context, "internal error: ast_loop cannot be reused, it bears no result!");
2456 m_outr = (ir_value*)1;
2459 * Should we ever need some kind of block ordering, better make this function
2460 * move blocks around than write a block ordering algorithm later... after all
2461 * the ast and ir should work together, not against each other.
2464 /* initexpr doesn't get its own block, it's pointless, it could create more blocks
2465 * anyway if for example it contains a ternary.
2469 if (!m_initexpr->codegen(func, false, &dummy))
2473 /* Store the block from which we enter this chaos */
2474 bin = func->m_curblock;
2476 /* The pre-loop condition needs its own block since we
2477 * need to be able to jump to the start of that expression.
2481 bprecond = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("pre_loop_cond"));
2485 /* the pre-loop-condition the least important place to 'continue' at */
2486 bcontinue = bprecond;
2489 func->m_curblock = bprecond;
2492 if (!m_precond->codegen(func, false, &precond))
2495 end_bprecond = func->m_curblock;
2497 bprecond = end_bprecond = nullptr;
2500 /* Now the next blocks won't be ordered nicely, but we need to
2501 * generate them this early for 'break' and 'continue'.
2504 bincrement = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("loop_increment"));
2507 bcontinue = bincrement; /* increment comes before the pre-loop-condition */
2509 bincrement = end_bincrement = nullptr;
2513 bpostcond = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("post_loop_cond"));
2516 bcontinue = bpostcond; /* postcond comes before the increment */
2518 bpostcond = end_bpostcond = nullptr;
2521 bout_id = func->m_ir_func->m_blocks.size();
2522 bout = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("after_loop"));
2527 /* The loop body... */
2530 bbody = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("loop_body"));
2535 func->m_curblock = bbody;
2537 func->m_breakblocks.push_back(bbreak);
2539 func->m_continueblocks.push_back(bcontinue);
2541 func->m_continueblocks.push_back(bbody);
2545 if (!m_body->codegen(func, false, &dummy))
2549 end_bbody = func->m_curblock;
2550 func->m_breakblocks.pop_back();
2551 func->m_continueblocks.pop_back();
2554 /* post-loop-condition */
2558 func->m_curblock = bpostcond;
2561 if (!m_postcond->codegen(func, false, &postcond))
2564 end_bpostcond = func->m_curblock;
2567 /* The incrementor */
2571 func->m_curblock = bincrement;
2574 if (!m_increment->codegen(func, false, &dummy))
2577 end_bincrement = func->m_curblock;
2580 /* In any case now, we continue from the outgoing block */
2581 func->m_curblock = bout;
2583 /* Now all blocks are in place */
2584 /* From 'bin' we jump to whatever comes first */
2585 if (bprecond) tmpblock = bprecond;
2586 else tmpblock = bbody; /* can never be null */
2589 else if (bpostcond) tmpblock = bpostcond;
2590 else tmpblock = bout;
2593 if (!ir_block_create_jump(bin, m_context, tmpblock))
2599 ir_block *ontrue, *onfalse;
2600 ontrue = bbody; /* can never be null */
2602 /* all of this is dead code
2603 else if (bincrement) ontrue = bincrement;
2604 else ontrue = bpostcond;
2613 if (!ir_block_create_if(end_bprecond, m_context, precond, ontrue, onfalse))
2620 if (bincrement) tmpblock = bincrement;
2621 else if (bpostcond) tmpblock = bpostcond;
2622 else if (bprecond) tmpblock = bprecond;
2623 else tmpblock = bbody;
2624 if (!end_bbody->m_final && !ir_block_create_jump(end_bbody, m_context, tmpblock))
2628 /* from increment */
2631 if (bpostcond) tmpblock = bpostcond;
2632 else if (bprecond) tmpblock = bprecond;
2633 else if (bbody) tmpblock = bbody;
2634 else tmpblock = bout;
2635 if (!ir_block_create_jump(end_bincrement, m_context, tmpblock))
2642 ir_block *ontrue, *onfalse;
2643 if (bprecond) ontrue = bprecond;
2644 else ontrue = bbody; /* can never be null */
2646 /* all of this is dead code
2647 else if (bincrement) ontrue = bincrement;
2648 else ontrue = bpostcond;
2657 if (!ir_block_create_if(end_bpostcond, m_context, postcond, ontrue, onfalse))
2661 /* Move 'bout' to the end */
2662 algo::shiftback(func->m_ir_func->m_blocks.begin() + bout_id,
2663 func->m_ir_func->m_blocks.end());
2665 //func->m_ir_func->m_blocks[bout_id].release(); // it's a vector<std::unique_ptr<>>
2666 //func->m_ir_func->m_blocks.erase(func->m_ir_func->m_blocks.begin() + bout_id);
2667 //func->m_ir_func->m_blocks.emplace_back(bout);
2672 bool ast_breakcont::codegen(ast_function *func, bool lvalue, ir_value **out)
2679 compile_error(m_context, "break/continue expression is not an l-value");
2684 compile_error(m_context, "internal error: ast_breakcont cannot be reused!");
2687 m_outr = (ir_value*)1;
2690 target = func->m_continueblocks[func->m_continueblocks.size()-1-m_levels];
2692 target = func->m_breakblocks[func->m_breakblocks.size()-1-m_levels];
2695 compile_error(m_context, "%s is lacking a target block", (m_is_continue ? "continue" : "break"));
2699 if (!ir_block_create_jump(func->m_curblock, m_context, target))
2704 bool ast_switch::codegen(ast_function *func, bool lvalue, ir_value **out)
2706 ast_switch_case *def_case = nullptr;
2707 ir_block *def_bfall = nullptr;
2708 ir_block *def_bfall_to = nullptr;
2709 bool set_def_bfall_to = false;
2711 ir_value *dummy = nullptr;
2712 ir_value *irop = nullptr;
2713 ir_block *bout = nullptr;
2714 ir_block *bfall = nullptr;
2721 compile_error(m_context, "switch expression is not an l-value");
2726 compile_error(m_context, "internal error: ast_switch cannot be reused!");
2729 m_outr = (ir_value*)1;
2734 if (!m_operand->codegen(func, false, &irop))
2737 if (m_cases.empty())
2740 cmpinstr = type_eq_instr[irop->m_vtype];
2741 if (cmpinstr >= VINSTR_END) {
2742 ast_type_to_string(m_operand, typestr, sizeof(typestr));
2743 compile_error(m_context, "invalid type to perform a switch on: %s", typestr);
2747 bout_id = func->m_ir_func->m_blocks.size();
2748 bout = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("after_switch"));
2752 /* setup the break block */
2753 func->m_breakblocks.push_back(bout);
2755 /* Now create all cases */
2756 for (auto &it : m_cases) {
2757 ir_value *cond, *val;
2758 ir_block *bcase, *bnot;
2761 ast_switch_case *swcase = ⁢
2763 if (swcase->m_value) {
2764 /* A regular case */
2765 /* generate the condition operand */
2766 if (!swcase->m_value->codegen(func, false, &val))
2768 /* generate the condition */
2769 cond = ir_block_create_binop(func->m_curblock, m_context, func->makeLabel("switch_eq"), cmpinstr, irop, val);
2773 bcase = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("case"));
2774 bnot_id = func->m_ir_func->m_blocks.size();
2775 bnot = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("not_case"));
2776 if (!bcase || !bnot)
2778 if (set_def_bfall_to) {
2779 set_def_bfall_to = false;
2780 def_bfall_to = bcase;
2782 if (!ir_block_create_if(func->m_curblock, m_context, cond, bcase, bnot))
2785 /* Make the previous case-end fall through */
2786 if (bfall && !bfall->m_final) {
2787 if (!ir_block_create_jump(bfall, m_context, bcase))
2791 /* enter the case */
2792 func->m_curblock = bcase;
2793 if (!swcase->m_code->codegen(func, false, &dummy))
2796 /* remember this block to fall through from */
2797 bfall = func->m_curblock;
2799 /* enter the else and move it down */
2800 func->m_curblock = bnot;
2801 algo::shiftback(func->m_ir_func->m_blocks.begin() + bnot_id,
2802 func->m_ir_func->m_blocks.end());
2804 //func->m_ir_func->m_blocks[bnot_id].release();
2805 //func->m_ir_func->m_blocks.erase(func->m_ir_func->m_blocks.begin() + bnot_id);
2806 //func->m_ir_func->m_blocks.emplace_back(bnot);
2808 /* The default case */
2809 /* Remember where to fall through from: */
2812 /* remember which case it was */
2814 /* And the next case will be remembered */
2815 set_def_bfall_to = true;
2819 /* Jump from the last bnot to bout */
2820 if (bfall && !bfall->m_final && !ir_block_create_jump(bfall, m_context, bout)) {
2822 astwarning(bfall->m_context, WARN_???, "missing break after last case");
2827 /* If there was a default case, put it down here */
2831 /* No need to create an extra block */
2832 bcase = func->m_curblock;
2834 /* Insert the fallthrough jump */
2835 if (def_bfall && !def_bfall->m_final) {
2836 if (!ir_block_create_jump(def_bfall, m_context, bcase))
2840 /* Now generate the default code */
2841 if (!def_case->m_code->codegen(func, false, &dummy))
2844 /* see if we need to fall through */
2845 if (def_bfall_to && !func->m_curblock->m_final)
2847 if (!ir_block_create_jump(func->m_curblock, m_context, def_bfall_to))
2852 /* Jump from the last bnot to bout */
2853 if (!func->m_curblock->m_final && !ir_block_create_jump(func->m_curblock, m_context, bout))
2855 /* enter the outgoing block */
2856 func->m_curblock = bout;
2858 /* restore the break block */
2859 func->m_breakblocks.pop_back();
2861 /* Move 'bout' to the end, it's nicer */
2862 algo::shiftback(func->m_ir_func->m_blocks.begin() + bout_id,
2863 func->m_ir_func->m_blocks.end());
2865 //func->m_ir_func->m_blocks[bout_id].release();
2866 //func->m_ir_func->m_blocks.erase(func->m_ir_func->m_blocks.begin() + bout_id);
2867 //func->m_ir_func->m_blocks.emplace_back(bout);
2872 bool ast_label::codegen(ast_function *func, bool lvalue, ir_value **out)
2877 compile_error(m_context, "internal error: ast_label never defined");
2883 compile_error(m_context, "internal error: ast_label cannot be an lvalue");
2887 /* simply create a new block and jump to it */
2888 m_irblock = ir_function_create_block(m_context, func->m_ir_func, m_name.c_str());
2890 compile_error(m_context, "failed to allocate label block `%s`", m_name);
2893 if (!func->m_curblock->m_final) {
2894 if (!ir_block_create_jump(func->m_curblock, m_context, m_irblock))
2898 /* enter the new block */
2899 func->m_curblock = m_irblock;
2901 /* Generate all the leftover gotos */
2902 for (auto &it : m_gotos) {
2903 if (!it->codegen(func, false, &dummy))
2910 bool ast_goto::codegen(ast_function *func, bool lvalue, ir_value **out)
2914 compile_error(m_context, "internal error: ast_goto cannot be an lvalue");
2918 if (m_target->m_irblock) {
2919 if (m_irblock_from) {
2920 /* we already tried once, this is the callback */
2921 m_irblock_from->m_final = false;
2922 if (!ir_block_create_goto(m_irblock_from, m_context, m_target->m_irblock)) {
2923 compile_error(m_context, "failed to generate goto to `%s`", m_name);
2929 if (!ir_block_create_goto(func->m_curblock, m_context, m_target->m_irblock)) {
2930 compile_error(m_context, "failed to generate goto to `%s`", m_name);
2937 /* the target has not yet been created...
2938 * close this block in a sneaky way:
2940 func->m_curblock->m_final = true;
2941 m_irblock_from = func->m_curblock;
2942 m_target->registerGoto(this);
2948 bool ast_state::codegen(ast_function *func, bool lvalue, ir_value **out)
2950 ir_value *frameval, *thinkval;
2953 compile_error(m_context, "not an l-value (state operation)");
2957 compile_error(m_context, "internal error: ast_state cannot be reused!");
2962 if (!m_framenum->codegen(func, false, &frameval))
2967 if (!m_nextthink->codegen(func, false, &thinkval))
2972 if (!ir_block_create_state_op(func->m_curblock, m_context, frameval, thinkval)) {
2973 compile_error(m_context, "failed to create STATE instruction");
2977 m_outr = (ir_value*)1;
2981 bool ast_call::codegen(ast_function *func, bool lvalue, ir_value **out)
2983 std::vector<ir_value*> params;
2984 ir_instr *callinstr;
2986 ir_value *funval = nullptr;
2988 /* return values are never lvalues */
2990 compile_error(m_context, "not an l-value (function call)");
2999 if (!m_func->codegen(func, false, &funval))
3005 for (auto &it : m_params) {
3007 if (!it->codegen(func, false, ¶m))
3011 params.push_back(param);
3014 /* varargs counter */
3017 ir_builder *builder = func->m_curblock->m_owner->m_owner;
3018 if (!m_va_count->codegen(func, false, &va_count))
3020 if (!ir_block_create_store_op(func->m_curblock, m_context, INSTR_STORE_F,
3021 ir_builder_get_va_count(builder), va_count))
3027 callinstr = ir_block_create_call(func->m_curblock, m_context,
3028 func->makeLabel("call"),
3029 funval, !!(m_func->m_flags & AST_FLAG_NORETURN));
3033 for (auto &it : params)
3034 ir_call_param(callinstr, it);
3036 *out = ir_call_value(callinstr);
3039 codegen_output_type(this, *out);