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);
950 // force this to be cleared before m_varargs/m_argc as blocks might
951 // try to access them via ast_unref()
955 const char* ast_function::makeLabel(const char *prefix)
961 if (!OPTS_OPTION_BOOL(OPTION_DUMP) &&
962 !OPTS_OPTION_BOOL(OPTION_DUMPFIN) &&
963 !OPTS_OPTION_BOOL(OPTION_DEBUG))
968 id = (m_labelcount++);
969 len = strlen(prefix);
971 from = m_labelbuf + sizeof(m_labelbuf)-1;
974 *from-- = (id%10) + '0';
978 memcpy(from - len, prefix, len);
982 /*********************************************************************/
984 * by convention you must never pass nullptr to the 'ir_value **out'
985 * parameter. If you really don't care about the output, pass a dummy.
986 * But I can't imagine a pituation where the output is truly unnecessary.
989 static void codegen_output_type(ast_expression *self, ir_value *out)
991 if (out->m_vtype == TYPE_FIELD)
992 out->m_fieldtype = self->m_next->m_vtype;
993 if (out->m_vtype == TYPE_FUNCTION)
994 out->m_outtype = self->m_next->m_vtype;
997 bool ast_value::codegen(ast_function *func, bool lvalue, ir_value **out)
1001 if (m_vtype == TYPE_NIL) {
1002 *out = func->m_ir_func->m_owner->m_nil;
1005 // NOTE: This is the codegen for a variable used in an expression.
1006 // It is not the codegen to generate the value storage. For this purpose,
1007 // generateLocal and generateGlobal are to be used before this
1008 // is executed. ast_function::generateFunction should take care of its
1009 // locals, and the ast-user should take care of generateGlobal to be used
1010 // on all the globals.
1012 char tname[1024]; /* typename is reserved in C++ */
1013 ast_type_to_string(this, tname, sizeof(tname));
1014 compile_error(m_context, "ast_value used before generated %s %s", tname, m_name);
1021 bool ast_value::setGlobalArray()
1023 size_t count = m_initlist.size();
1026 if (count > m_count) {
1027 compile_error(m_context, "too many elements in initializer");
1030 else if (count < m_count) {
1032 compile_warning(m_context, "not all elements are initialized");
1036 for (i = 0; i != count; ++i) {
1037 switch (m_next->m_vtype) {
1039 if (!ir_value_set_float(m_ir_values[i], m_initlist[i].vfloat))
1043 if (!ir_value_set_vector(m_ir_values[i], m_initlist[i].vvec))
1047 if (!ir_value_set_string(m_ir_values[i], m_initlist[i].vstring))
1051 /* we don't support them in any other place yet either */
1052 compile_error(m_context, "TODO: nested arrays");
1055 /* this requiers a bit more work - similar to the fields I suppose */
1056 compile_error(m_context, "global of type function not properly generated");
1059 if (!m_initlist[i].vfield) {
1060 compile_error(m_context, "field constant without vfield set");
1063 if (!m_initlist[i].vfield->m_ir_v) {
1064 compile_error(m_context, "field constant generated before its field");
1067 if (!ir_value_set_field(m_ir_values[i], m_initlist[i].vfield->m_ir_v))
1071 compile_error(m_context, "TODO: global constant type %i", m_vtype);
1078 bool ast_value::checkArray(const ast_value &array) const
1080 if (array.m_flags & AST_FLAG_ARRAY_INIT && array.m_initlist.empty()) {
1081 compile_error(m_context, "array without size: %s", m_name);
1084 // we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements
1085 if (!array.m_count || array.m_count > OPTS_OPTION_U32(OPTION_MAX_ARRAY_SIZE)) {
1086 compile_error(m_context, "Invalid array of size %lu", (unsigned long)array.m_count);
1092 bool ast_value::generateGlobal(ir_builder *ir, bool isfield)
1094 if (m_vtype == TYPE_NIL) {
1095 compile_error(m_context, "internal error: trying to generate a variable of TYPE_NIL");
1099 if (m_hasvalue && m_vtype == TYPE_FUNCTION)
1100 return generateGlobalFunction(ir);
1102 if (isfield && m_vtype == TYPE_FIELD)
1103 return generateGlobalField(ir);
1105 ir_value *v = nullptr;
1106 if (m_vtype == TYPE_ARRAY) {
1107 v = prepareGlobalArray(ir);
1111 // Arrays don't do this since there's no "array" value which spans across the
1113 v = ir_builder_create_global(ir, m_name, m_vtype);
1115 compile_error(m_context, "ir_builder_create_global failed on `%s`", m_name);
1118 codegen_output_type(this, v);
1119 v->m_context = m_context;
1122 /* link us to the ir_value */
1126 if (m_flags & AST_FLAG_INCLUDE_DEF)
1127 m_ir_v->m_flags |= IR_FLAG_INCLUDE_DEF;
1128 if (m_flags & AST_FLAG_ERASEABLE)
1129 m_ir_v->m_flags |= IR_FLAG_ERASABLE;
1136 if (!ir_value_set_float(v, m_constval.vfloat))
1140 if (!ir_value_set_vector(v, m_constval.vvec))
1144 if (!ir_value_set_string(v, m_constval.vstring))
1148 if (!setGlobalArray())
1152 compile_error(m_context, "global of type function not properly generated");
1154 /* Cannot generate an IR value for a function,
1155 * need a pointer pointing to a function rather.
1158 if (!m_constval.vfield) {
1159 compile_error(m_context, "field constant without vfield set");
1162 if (!m_constval.vfield->m_ir_v) {
1163 compile_error(m_context, "field constant generated before its field");
1166 if (!ir_value_set_field(v, m_constval.vfield->m_ir_v))
1170 compile_error(m_context, "TODO: global constant type %i", m_vtype);
1178 bool ast_value::generateGlobalFunction(ir_builder *ir)
1180 ir_function *func = ir_builder_create_function(ir, m_name, m_next->m_vtype);
1183 func->m_context = m_context;
1184 func->m_value->m_context = m_context;
1186 m_constval.vfunc->m_ir_func = func;
1187 m_ir_v = func->m_value;
1188 if (m_flags & AST_FLAG_INCLUDE_DEF)
1189 m_ir_v->m_flags |= IR_FLAG_INCLUDE_DEF;
1190 if (m_flags & AST_FLAG_ERASEABLE)
1191 m_ir_v->m_flags |= IR_FLAG_ERASABLE;
1192 if (m_flags & AST_FLAG_BLOCK_COVERAGE)
1193 func->m_flags |= IR_FLAG_BLOCK_COVERAGE;
1194 // The function is filled later on ast_function::generateFunction...
1198 bool ast_value::generateGlobalField(ir_builder *ir)
1200 ast_expression *fieldtype = m_next;
1203 compile_error(m_context, "TODO: constant field pointers with value");
1207 if (fieldtype->m_vtype == TYPE_ARRAY) {
1208 if (!ast_istype(fieldtype, ast_value)) {
1209 compile_error(m_context, "internal error: ast_value required");
1212 ast_value *array = reinterpret_cast<ast_value*>(fieldtype);
1214 if (!checkArray(*array))
1217 ast_expression *elemtype = array->m_next;
1218 qc_type vtype = elemtype->m_vtype;
1220 ir_value *v = ir_builder_create_field(ir, m_name, vtype);
1222 compile_error(m_context, "ir_builder_create_global failed on `%s`", m_name);
1225 v->m_context = m_context;
1226 v->m_unique_life = true;
1228 array->m_ir_v = m_ir_v = v;
1230 if (m_flags & AST_FLAG_INCLUDE_DEF)
1231 m_ir_v->m_flags |= IR_FLAG_INCLUDE_DEF;
1232 if (m_flags & AST_FLAG_ERASEABLE)
1233 m_ir_v->m_flags |= IR_FLAG_ERASABLE;
1235 const size_t namelen = m_name.length();
1236 std::unique_ptr<char[]> name(new char[namelen+16]);
1237 util_strncpy(name.get(), m_name.c_str(), namelen);
1239 array->m_ir_values.resize(array->m_count);
1240 array->m_ir_values[0] = v;
1241 for (size_t ai = 1; ai < array->m_count; ++ai) {
1242 util_snprintf(name.get() + namelen, 16, "[%u]", (unsigned int)ai);
1243 array->m_ir_values[ai] = ir_builder_create_field(ir, name.get(), vtype);
1244 if (!array->m_ir_values[ai]) {
1245 compile_error(m_context, "ir_builder_create_global failed on `%s`", name.get());
1248 array->m_ir_values[ai]->m_context = m_context;
1249 array->m_ir_values[ai]->m_unique_life = true;
1250 array->m_ir_values[ai]->m_locked = true;
1251 if (m_flags & AST_FLAG_INCLUDE_DEF)
1252 m_ir_values[ai]->m_flags |= IR_FLAG_INCLUDE_DEF;
1257 ir_value *v = ir_builder_create_field(ir, m_name, m_next->m_vtype);
1260 v->m_context = m_context;
1262 if (m_flags & AST_FLAG_INCLUDE_DEF)
1263 m_ir_v->m_flags |= IR_FLAG_INCLUDE_DEF;
1265 if (m_flags & AST_FLAG_ERASEABLE)
1266 m_ir_v->m_flags |= IR_FLAG_ERASABLE;
1271 ir_value *ast_value::prepareGlobalArray(ir_builder *ir)
1273 ast_expression *elemtype = m_next;
1274 qc_type vtype = elemtype->m_vtype;
1276 if (m_flags & AST_FLAG_ARRAY_INIT && !m_count) {
1277 compile_error(m_context, "array `%s' has no size", m_name);
1281 /* same as with field arrays */
1282 if (!checkArray(*this))
1285 ir_value *v = ir_builder_create_global(ir, m_name, vtype);
1287 compile_error(m_context, "ir_builder_create_global failed `%s`", m_name);
1290 v->m_context = m_context;
1291 v->m_unique_life = true;
1294 if (m_flags & AST_FLAG_INCLUDE_DEF)
1295 v->m_flags |= IR_FLAG_INCLUDE_DEF;
1296 if (m_flags & AST_FLAG_ERASEABLE)
1297 m_ir_v->m_flags |= IR_FLAG_ERASABLE;
1299 const size_t namelen = m_name.length();
1300 std::unique_ptr<char[]> name(new char[namelen+16]);
1301 util_strncpy(name.get(), m_name.c_str(), namelen);
1303 m_ir_values.resize(m_count);
1305 for (size_t ai = 1; ai < m_count; ++ai) {
1306 util_snprintf(name.get() + namelen, 16, "[%u]", (unsigned int)ai);
1307 m_ir_values[ai] = ir_builder_create_global(ir, name.get(), vtype);
1308 if (!m_ir_values[ai]) {
1309 compile_error(m_context, "ir_builder_create_global failed `%s`", name.get());
1312 m_ir_values[ai]->m_context = m_context;
1313 m_ir_values[ai]->m_unique_life = true;
1314 m_ir_values[ai]->m_locked = true;
1315 if (m_flags & AST_FLAG_INCLUDE_DEF)
1316 m_ir_values[ai]->m_flags |= IR_FLAG_INCLUDE_DEF;
1322 bool ast_value::generateLocal(ir_function *func, bool param)
1324 if (m_vtype == TYPE_NIL) {
1325 compile_error(m_context, "internal error: trying to generate a variable of TYPE_NIL");
1329 if (m_hasvalue && m_vtype == TYPE_FUNCTION)
1331 /* Do we allow local functions? I think not...
1332 * this is NOT a function pointer atm.
1337 ir_value *v = nullptr;
1338 if (m_vtype == TYPE_ARRAY) {
1339 ast_expression *elemtype = m_next;
1340 qc_type vtype = elemtype->m_vtype;
1342 func->m_flags |= IR_FLAG_HAS_ARRAYS;
1344 if (param && !(m_flags & AST_FLAG_IS_VARARG)) {
1345 compile_error(m_context, "array-parameters are not supported");
1349 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1350 if (!checkArray(*this))
1353 m_ir_values.resize(m_count);
1354 v = ir_function_create_local(func, m_name, vtype, param);
1356 compile_error(m_context, "internal error: ir_function_create_local failed");
1359 v->m_context = m_context;
1360 v->m_unique_life = true;
1363 const size_t namelen = m_name.length();
1364 std::unique_ptr<char[]> name(new char[namelen+16]);
1365 util_strncpy(name.get(), m_name.c_str(), namelen);
1368 for (size_t ai = 1; ai < m_count; ++ai) {
1369 util_snprintf(name.get() + namelen, 16, "[%u]", (unsigned int)ai);
1370 m_ir_values[ai] = ir_function_create_local(func, name.get(), vtype, param);
1371 if (!m_ir_values[ai]) {
1372 compile_error(m_context, "internal_error: ir_builder_create_global failed on `%s`", name.get());
1375 m_ir_values[ai]->m_context = m_context;
1376 m_ir_values[ai]->m_unique_life = true;
1377 m_ir_values[ai]->m_locked = true;
1382 v = ir_function_create_local(func, m_name, m_vtype, param);
1385 codegen_output_type(this, v);
1386 v->m_context = m_context;
1389 // A constant local... hmmm...
1390 // I suppose the IR will have to deal with this
1395 if (!ir_value_set_float(v, m_constval.vfloat))
1399 if (!ir_value_set_vector(v, m_constval.vvec))
1403 if (!ir_value_set_string(v, m_constval.vstring))
1407 compile_error(m_context, "TODO: global constant type %i", m_vtype);
1412 // link us to the ir_value
1416 if (!generateAccessors(func->m_owner))
1420 error: /* clean up */
1425 bool ast_value::generateAccessors(ir_builder *ir)
1428 bool warn = OPTS_WARN(WARN_USED_UNINITIALIZED);
1429 if (!m_setter || !m_getter)
1431 if (m_count && m_ir_values.empty()) {
1432 compile_error(m_context, "internal error: no array values generated for `%s`", m_name);
1435 for (i = 0; i < m_count; ++i) {
1436 if (!m_ir_values[i]) {
1437 compile_error(m_context, "internal error: not all array values have been generated for `%s`", m_name);
1440 if (!m_ir_values[i]->m_life.empty()) {
1441 compile_error(m_context, "internal error: function containing `%s` already generated", m_name);
1446 opts_set(opts.warn, WARN_USED_UNINITIALIZED, false);
1448 if (!m_setter->generateGlobal(ir, false) ||
1449 !m_setter->m_constval.vfunc->generateFunction(ir) ||
1450 !ir_function_finalize(m_setter->m_constval.vfunc->m_ir_func))
1452 compile_error(m_context, "internal error: failed to generate setter for `%s`", m_name);
1453 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1458 if (!m_getter->generateGlobal(ir, false) ||
1459 !m_getter->m_constval.vfunc->generateFunction(ir) ||
1460 !ir_function_finalize(m_getter->m_constval.vfunc->m_ir_func))
1462 compile_error(m_context, "internal error: failed to generate getter for `%s`", m_name);
1463 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1467 for (i = 0; i < m_count; ++i)
1468 m_ir_values[i]->m_life.clear();
1469 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1473 bool ast_function::generateFunction(ir_builder *ir)
1479 ir_function *irf = m_ir_func;
1481 compile_error(m_context, "internal error: ast_function's related ast_value was not generated yet");
1485 /* fill the parameter list */
1486 for (auto &it : m_function_type->m_type_params) {
1487 if (it->m_vtype == TYPE_FIELD)
1488 vec_push(irf->m_params, it->m_next->m_vtype);
1490 vec_push(irf->m_params, it->m_vtype);
1492 if (!it->generateLocal(m_ir_func, true))
1498 if (!m_varargs->generateLocal(m_ir_func, true))
1500 irf->m_max_varargs = m_varargs->m_count;
1504 irf->m_builtin = m_builtin;
1508 /* have a local return value variable? */
1509 if (m_return_value) {
1510 if (!m_return_value->generateLocal(m_ir_func, false))
1514 if (m_blocks.empty()) {
1515 compile_error(m_context, "function `%s` has no body", m_name);
1519 irf->m_first = m_curblock = ir_function_create_block(m_context, irf, "entry");
1521 compile_error(m_context, "failed to allocate entry block for `%s`", m_name);
1529 if (!m_argc->generateLocal(m_ir_func, true))
1531 if (!m_argc->codegen(this, false, &va_count))
1533 if (!m_fixedparams->codegen(this, false, &fixed))
1535 sub = ir_block_create_binop(m_curblock, m_context,
1536 makeLabel("va_count"), INSTR_SUB_F,
1537 ir_builder_get_va_count(ir), fixed);
1540 if (!ir_block_create_store_op(m_curblock, m_context, INSTR_STORE_F,
1547 for (auto &it : m_blocks) {
1548 if (!it->codegen(this, false, &dummy))
1552 /* TODO: check return types */
1553 if (!m_curblock->m_final)
1555 if (!m_function_type->m_next ||
1556 m_function_type->m_next->m_vtype == TYPE_VOID)
1558 return ir_block_create_return(m_curblock, m_context, nullptr);
1560 else if (vec_size(m_curblock->m_entries) || m_curblock == irf->m_first)
1562 if (m_return_value) {
1563 if (!m_return_value->codegen(this, false, &dummy))
1565 return ir_block_create_return(m_curblock, m_context, dummy);
1567 else if (compile_warning(m_context, WARN_MISSING_RETURN_VALUES,
1568 "control reaches end of non-void function (`%s`) via %s",
1569 m_name.c_str(), m_curblock->m_label.c_str()))
1573 return ir_block_create_return(m_curblock, m_context, nullptr);
1579 static bool starts_a_label(const ast_expression *ex)
1581 while (ex && ast_istype(ex, ast_block)) {
1582 auto b = reinterpret_cast<const ast_block*>(ex);
1587 return ast_istype(ex, ast_label);
1590 /* Note, you will not see ast_block_codegen generate ir_blocks.
1591 * To the AST and the IR, blocks are 2 different things.
1592 * In the AST it represents a block of code, usually enclosed in
1593 * curly braces {...}.
1594 * While in the IR it represents a block in terms of control-flow.
1596 bool ast_block::codegen(ast_function *func, bool lvalue, ir_value **out)
1598 /* We don't use this
1599 * Note: an ast-representation using the comma-operator
1600 * of the form: (a, b, c) = x should not assign to c...
1603 compile_error(m_context, "not an l-value (code-block)");
1612 /* output is nullptr at first, we'll have each expression
1613 * assign to out output, thus, a comma-operator represention
1614 * using an ast_block will return the last generated value,
1615 * so: (b, c) + a executed both b and c, and returns c,
1616 * which is then added to a.
1620 /* generate locals */
1621 for (auto &it : m_locals) {
1622 if (!it->generateLocal(func->m_ir_func, false)) {
1623 if (OPTS_OPTION_BOOL(OPTION_DEBUG))
1624 compile_error(m_context, "failed to generate local `%s`", it->m_name);
1629 for (auto &it : m_exprs) {
1630 if (func->m_curblock->m_final && !starts_a_label(it)) {
1631 if (compile_warning(it->m_context, WARN_UNREACHABLE_CODE, "unreachable statement"))
1635 if (!it->codegen(func, false, out))
1644 bool ast_store::codegen(ast_function *func, bool lvalue, ir_value **out)
1646 ir_value *left = nullptr;
1647 ir_value *right = nullptr;
1650 ast_array_index *ai = nullptr;
1652 if (lvalue && m_outl) {
1657 if (!lvalue && m_outr) {
1662 if (ast_istype(m_dest, ast_array_index))
1665 ai = (ast_array_index*)m_dest;
1666 idx = (ast_value*)ai->m_index;
1668 if (ast_istype(ai->m_index, ast_value) && idx->m_hasvalue && idx->m_cvq == CV_CONST)
1673 /* we need to call the setter */
1674 ir_value *iridx, *funval;
1678 compile_error(m_context, "array-subscript assignment cannot produce lvalues");
1682 auto arr = reinterpret_cast<ast_value*>(ai->m_array);
1683 if (!ast_istype(ai->m_array, ast_value) || !arr->m_setter) {
1684 compile_error(m_context, "value has no setter (%s)", arr->m_name);
1688 if (!idx->codegen(func, false, &iridx))
1691 if (!arr->m_setter->codegen(func, true, &funval))
1694 if (!m_source->codegen(func, false, &right))
1697 call = ir_block_create_call(func->m_curblock, m_context, func->makeLabel("store"), funval, false);
1700 ir_call_param(call, iridx);
1701 ir_call_param(call, right);
1709 if (!m_dest->codegen(func, true, &left))
1714 if (!m_source->codegen(func, false, &right))
1717 if (!ir_block_create_store_op(func->m_curblock, m_context, m_op, left, right))
1722 /* Theoretically, an assinment returns its left side as an
1723 * lvalue, if we don't need an lvalue though, we return
1724 * the right side as an rvalue, otherwise we have to
1725 * somehow know whether or not we need to dereference the pointer
1726 * on the left side - that is: OP_LOAD if it was an address.
1727 * Also: in original QC we cannot OP_LOADP *anyway*.
1729 *out = (lvalue ? left : right);
1734 bool ast_binary::codegen(ast_function *func, bool lvalue, ir_value **out)
1736 ir_value *left, *right;
1738 /* A binary operation cannot yield an l-value */
1740 compile_error(m_context, "not an l-value (binop)");
1749 if ((OPTS_FLAG(SHORT_LOGIC) || OPTS_FLAG(PERL_LOGIC)) &&
1750 (m_op == INSTR_AND || m_op == INSTR_OR))
1752 /* NOTE: The short-logic path will ignore right_first */
1754 /* short circuit evaluation */
1755 ir_block *other, *merge;
1756 ir_block *from_left, *from_right;
1760 /* prepare end-block */
1761 merge_id = func->m_ir_func->m_blocks.size();
1762 merge = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("sce_merge"));
1764 /* generate the left expression */
1765 if (!m_left->codegen(func, false, &left))
1767 /* remember the block */
1768 from_left = func->m_curblock;
1770 /* create a new block for the right expression */
1771 other = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("sce_other"));
1772 if (m_op == INSTR_AND) {
1773 /* on AND: left==true -> other */
1774 if (!ir_block_create_if(func->m_curblock, m_context, left, other, merge))
1777 /* on OR: left==false -> other */
1778 if (!ir_block_create_if(func->m_curblock, m_context, left, merge, other))
1781 /* use the likely flag */
1782 vec_last(func->m_curblock->m_instr)->m_likely = true;
1784 /* enter the right-expression's block */
1785 func->m_curblock = other;
1787 if (!m_right->codegen(func, false, &right))
1789 /* remember block */
1790 from_right = func->m_curblock;
1792 /* jump to the merge block */
1793 if (!ir_block_create_jump(func->m_curblock, m_context, merge))
1796 algo::shiftback(func->m_ir_func->m_blocks.begin() + merge_id,
1797 func->m_ir_func->m_blocks.end());
1799 //func->m_ir_func->m_blocks[merge_id].release();
1800 //func->m_ir_func->m_blocks.erase(func->m_ir_func->m_blocks.begin() + merge_id);
1801 //func->m_ir_func->m_blocks.emplace_back(merge);
1803 func->m_curblock = merge;
1804 phi = ir_block_create_phi(func->m_curblock, m_context,
1805 func->makeLabel("sce_value"),
1807 ir_phi_add(phi, from_left, left);
1808 ir_phi_add(phi, from_right, right);
1809 *out = ir_phi_value(phi);
1813 if (!OPTS_FLAG(PERL_LOGIC)) {
1815 if (OPTS_FLAG(CORRECT_LOGIC) && (*out)->m_vtype == TYPE_VECTOR) {
1816 *out = ir_block_create_unary(func->m_curblock, m_context,
1817 func->makeLabel("sce_bool_v"),
1821 *out = ir_block_create_unary(func->m_curblock, m_context,
1822 func->makeLabel("sce_bool"),
1827 else if (OPTS_FLAG(FALSE_EMPTY_STRINGS) && (*out)->m_vtype == TYPE_STRING) {
1828 *out = ir_block_create_unary(func->m_curblock, m_context,
1829 func->makeLabel("sce_bool_s"),
1833 *out = ir_block_create_unary(func->m_curblock, m_context,
1834 func->makeLabel("sce_bool"),
1840 *out = ir_block_create_binop(func->m_curblock, m_context,
1841 func->makeLabel("sce_bool"),
1842 INSTR_AND, *out, *out);
1849 codegen_output_type(this, *out);
1853 if (m_right_first) {
1854 if (!m_right->codegen(func, false, &right))
1856 if (!m_left->codegen(func, false, &left))
1859 if (!m_left->codegen(func, false, &left))
1861 if (!m_right->codegen(func, false, &right))
1865 *out = ir_block_create_binop(func->m_curblock, m_context, func->makeLabel("bin"),
1870 codegen_output_type(this, *out);
1875 bool ast_binstore::codegen(ast_function *func, bool lvalue, ir_value **out)
1877 ir_value *leftl = nullptr, *leftr, *right, *bin;
1881 ast_array_index *ai = nullptr;
1882 ir_value *iridx = nullptr;
1884 if (lvalue && m_outl) {
1889 if (!lvalue && m_outr) {
1894 if (ast_istype(m_dest, ast_array_index))
1897 ai = (ast_array_index*)m_dest;
1898 idx = (ast_value*)ai->m_index;
1900 if (ast_istype(ai->m_index, ast_value) && idx->m_hasvalue && idx->m_cvq == CV_CONST)
1904 /* for a binstore we need both an lvalue and an rvalue for the left side */
1905 /* rvalue of destination! */
1907 if (!idx->codegen(func, false, &iridx))
1910 if (!m_dest->codegen(func, false, &leftr))
1913 /* source as rvalue only */
1914 if (!m_source->codegen(func, false, &right))
1917 /* now the binary */
1918 bin = ir_block_create_binop(func->m_curblock, m_context, func->makeLabel("binst"),
1919 m_opbin, leftr, right);
1923 /* we need to call the setter */
1928 compile_error(m_context, "array-subscript assignment cannot produce lvalues");
1932 arr = (ast_value*)ai->m_array;
1933 if (!ast_istype(ai->m_array, ast_value) || !arr->m_setter) {
1934 compile_error(m_context, "value has no setter (%s)", arr->m_name);
1938 if (!arr->m_setter->codegen(func, true, &funval))
1941 call = ir_block_create_call(func->m_curblock, m_context, func->makeLabel("store"), funval, false);
1944 ir_call_param(call, iridx);
1945 ir_call_param(call, bin);
1949 // lvalue of destination
1950 if (!m_dest->codegen(func, true, &leftl))
1954 if (!ir_block_create_store_op(func->m_curblock, m_context, m_opstore, leftl, bin))
1959 /* Theoretically, an assinment returns its left side as an
1960 * lvalue, if we don't need an lvalue though, we return
1961 * the right side as an rvalue, otherwise we have to
1962 * somehow know whether or not we need to dereference the pointer
1963 * on the left side - that is: OP_LOAD if it was an address.
1964 * Also: in original QC we cannot OP_LOADP *anyway*.
1966 *out = (lvalue ? leftl : bin);
1971 bool ast_unary::codegen(ast_function *func, bool lvalue, ir_value **out)
1975 /* An unary operation cannot yield an l-value */
1977 compile_error(m_context, "not an l-value (binop)");
1987 if (!m_operand->codegen(func, false, &operand))
1990 *out = ir_block_create_unary(func->m_curblock, m_context, func->makeLabel("unary"),
1999 bool ast_return::codegen(ast_function *func, bool lvalue, ir_value **out)
2005 /* In the context of a return operation, we don't actually return
2009 compile_error(m_context, "return-expression is not an l-value");
2014 compile_error(m_context, "internal error: ast_return cannot be reused, it bears no result!");
2017 m_outr = (ir_value*)1;
2021 if (!m_operand->codegen(func, false, &operand))
2024 if (!ir_block_create_return(func->m_curblock, m_context, operand))
2027 if (!ir_block_create_return(func->m_curblock, m_context, nullptr))
2034 bool ast_entfield::codegen(ast_function *func, bool lvalue, ir_value **out)
2036 ir_value *ent, *field;
2038 // This function needs to take the 'lvalue' flag into account!
2039 // As lvalue we provide a field-pointer, as rvalue we provide the
2042 if (lvalue && m_outl) {
2047 if (!lvalue && m_outr) {
2052 if (!m_entity->codegen(func, false, &ent))
2055 if (!m_field->codegen(func, false, &field))
2060 *out = ir_block_create_fieldaddress(func->m_curblock, m_context, func->makeLabel("efa"),
2063 *out = ir_block_create_load_from_ent(func->m_curblock, m_context, func->makeLabel("efv"),
2064 ent, field, m_vtype);
2065 /* Done AFTER error checking:
2066 codegen_output_type(this, *out);
2070 compile_error(m_context, "failed to create %s instruction (output type %s)",
2071 (lvalue ? "ADDRESS" : "FIELD"),
2072 type_name[m_vtype]);
2076 codegen_output_type(this, *out);
2083 // Hm that should be it...
2087 bool ast_member::codegen(ast_function *func, bool lvalue, ir_value **out)
2091 /* in QC this is always an lvalue */
2092 if (lvalue && m_rvalue) {
2093 compile_error(m_context, "not an l-value (member access)");
2101 if (!m_owner->codegen(func, false, &vec))
2104 if (vec->m_vtype != TYPE_VECTOR &&
2105 !(vec->m_vtype == TYPE_FIELD && m_owner->m_next->m_vtype == TYPE_VECTOR))
2110 *out = ir_value_vector_member(vec, m_field);
2113 return (*out != nullptr);
2116 bool ast_array_index::codegen(ast_function *func, bool lvalue, ir_value **out)
2121 if (!lvalue && m_outr) {
2125 if (lvalue && m_outl) {
2130 if (!ast_istype(m_array, ast_value)) {
2131 compile_error(m_context, "array indexing this way is not supported");
2132 /* note this would actually be pointer indexing because the left side is
2133 * not an actual array but (hopefully) an indexable expression.
2134 * Once we get integer arithmetic, and GADDRESS/GSTORE/GLOAD instruction
2135 * support this path will be filled.
2140 arr = reinterpret_cast<ast_value*>(m_array);
2141 idx = reinterpret_cast<ast_value*>(m_index);
2143 if (!ast_istype(m_index, ast_value) || !idx->m_hasvalue || idx->m_cvq != CV_CONST) {
2144 /* Time to use accessor functions */
2145 ir_value *iridx, *funval;
2149 compile_error(m_context, "(.2) array indexing here needs a compile-time constant");
2153 if (!arr->m_getter) {
2154 compile_error(m_context, "value has no getter, don't know how to index it");
2158 if (!m_index->codegen(func, false, &iridx))
2161 if (!arr->m_getter->codegen(func, true, &funval))
2164 call = ir_block_create_call(func->m_curblock, m_context, func->makeLabel("fetch"), funval, false);
2167 ir_call_param(call, iridx);
2169 *out = ir_call_value(call);
2171 (*out)->m_vtype = m_vtype;
2172 codegen_output_type(this, *out);
2176 if (idx->m_vtype == TYPE_FLOAT) {
2177 unsigned int arridx = idx->m_constval.vfloat;
2178 if (arridx >= m_array->m_count)
2180 compile_error(m_context, "array index out of bounds: %i", arridx);
2183 *out = arr->m_ir_values[arridx];
2185 else if (idx->m_vtype == TYPE_INTEGER) {
2186 unsigned int arridx = idx->m_constval.vint;
2187 if (arridx >= m_array->m_count)
2189 compile_error(m_context, "array index out of bounds: %i", arridx);
2192 *out = arr->m_ir_values[arridx];
2195 compile_error(m_context, "array indexing here needs an integer constant");
2198 (*out)->m_vtype = m_vtype;
2199 codegen_output_type(this, *out);
2203 bool ast_argpipe::codegen(ast_function *func, bool lvalue, ir_value **out)
2207 compile_error(m_context, "argpipe node: not an lvalue");
2212 compile_error(m_context, "TODO: argpipe codegen not implemented");
2216 bool ast_ifthen::codegen(ast_function *func, bool lvalue, ir_value **out)
2224 ir_block *ontrue_endblock = nullptr;
2225 ir_block *onfalse_endblock = nullptr;
2226 ir_block *merge = nullptr;
2229 /* We don't output any value, thus also don't care about r/lvalue */
2234 compile_error(m_context, "internal error: ast_ifthen cannot be reused, it bears no result!");
2237 m_outr = (ir_value*)1;
2239 /* generate the condition */
2240 if (!m_cond->codegen(func, false, &condval))
2242 /* update the block which will get the jump - because short-logic or ternaries may have changed this */
2243 cond = func->m_curblock;
2245 /* try constant folding away the condition */
2246 if ((folded = fold::cond_ifthen(condval, func, this)) != -1)
2250 /* create on-true block */
2251 ontrue = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("ontrue"));
2255 /* enter the block */
2256 func->m_curblock = ontrue;
2259 if (!m_on_true->codegen(func, false, &dummy))
2262 /* we now need to work from the current endpoint */
2263 ontrue_endblock = func->m_curblock;
2269 /* create on-false block */
2270 onfalse = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("onfalse"));
2274 /* enter the block */
2275 func->m_curblock = onfalse;
2278 if (!m_on_false->codegen(func, false, &dummy))
2281 /* we now need to work from the current endpoint */
2282 onfalse_endblock = func->m_curblock;
2286 /* Merge block were they all merge in to */
2287 if (!ontrue || !onfalse || !ontrue_endblock->m_final || !onfalse_endblock->m_final)
2289 merge = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("endif"));
2292 /* add jumps ot the merge block */
2293 if (ontrue && !ontrue_endblock->m_final && !ir_block_create_jump(ontrue_endblock, m_context, merge))
2295 if (onfalse && !onfalse_endblock->m_final && !ir_block_create_jump(onfalse_endblock, m_context, merge))
2298 /* Now enter the merge block */
2299 func->m_curblock = merge;
2302 /* we create the if here, that way all blocks are ordered :)
2304 if (!ir_block_create_if(cond, m_context, condval,
2305 (ontrue ? ontrue : merge),
2306 (onfalse ? onfalse : merge)))
2314 bool ast_ternary::codegen(ast_function *func, bool lvalue, ir_value **out)
2317 ir_value *trueval, *falseval;
2320 ir_block *cond = func->m_curblock;
2321 ir_block *cond_out = nullptr;
2322 ir_block *ontrue, *ontrue_out = nullptr;
2323 ir_block *onfalse, *onfalse_out = nullptr;
2327 /* Ternary can never create an lvalue... */
2331 /* In theory it shouldn't be possible to pass through a node twice, but
2332 * in case we add any kind of optimization pass for the AST itself, it
2333 * may still happen, thus we remember a created ir_value and simply return one
2334 * if it already exists.
2341 /* In the following, contraty to ast_ifthen, we assume both paths exist. */
2343 /* generate the condition */
2344 func->m_curblock = cond;
2345 if (!m_cond->codegen(func, false, &condval))
2347 cond_out = func->m_curblock;
2349 /* try constant folding away the condition */
2350 if ((folded = fold::cond_ternary(condval, func, this)) != -1)
2353 /* create on-true block */
2354 ontrue = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("tern_T"));
2359 /* enter the block */
2360 func->m_curblock = ontrue;
2363 if (!m_on_true->codegen(func, false, &trueval))
2366 ontrue_out = func->m_curblock;
2369 /* create on-false block */
2370 onfalse = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("tern_F"));
2375 /* enter the block */
2376 func->m_curblock = onfalse;
2379 if (!m_on_false->codegen(func, false, &falseval))
2382 onfalse_out = func->m_curblock;
2385 /* create merge block */
2386 merge = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("tern_out"));
2389 /* jump to merge block */
2390 if (!ir_block_create_jump(ontrue_out, m_context, merge))
2392 if (!ir_block_create_jump(onfalse_out, m_context, merge))
2395 /* create if instruction */
2396 if (!ir_block_create_if(cond_out, m_context, condval, ontrue, onfalse))
2399 /* Now enter the merge block */
2400 func->m_curblock = merge;
2402 /* Here, now, we need a PHI node
2403 * but first some sanity checking...
2405 if (trueval->m_vtype != falseval->m_vtype && trueval->m_vtype != TYPE_NIL && falseval->m_vtype != TYPE_NIL) {
2406 /* error("ternary with different types on the two sides"); */
2407 compile_error(m_context, "internal error: ternary operand types invalid");
2412 phi = ir_block_create_phi(merge, m_context, func->makeLabel("phi"), m_vtype);
2414 compile_error(m_context, "internal error: failed to generate phi node");
2417 ir_phi_add(phi, ontrue_out, trueval);
2418 ir_phi_add(phi, onfalse_out, falseval);
2420 m_outr = ir_phi_value(phi);
2423 codegen_output_type(this, *out);
2428 bool ast_loop::codegen(ast_function *func, bool lvalue, ir_value **out)
2430 ir_value *dummy = nullptr;
2431 ir_value *precond = nullptr;
2432 ir_value *postcond = nullptr;
2434 /* Since we insert some jumps "late" so we have blocks
2435 * ordered "nicely", we need to keep track of the actual end-blocks
2436 * of expressions to add the jumps to.
2438 ir_block *bbody = nullptr, *end_bbody = nullptr;
2439 ir_block *bprecond = nullptr, *end_bprecond = nullptr;
2440 ir_block *bpostcond = nullptr, *end_bpostcond = nullptr;
2441 ir_block *bincrement = nullptr, *end_bincrement = nullptr;
2442 ir_block *bout = nullptr, *bin = nullptr;
2444 /* let's at least move the outgoing block to the end */
2447 /* 'break' and 'continue' need to be able to find the right blocks */
2448 ir_block *bcontinue = nullptr;
2449 ir_block *bbreak = nullptr;
2451 ir_block *tmpblock = nullptr;
2457 compile_error(m_context, "internal error: ast_loop cannot be reused, it bears no result!");
2460 m_outr = (ir_value*)1;
2463 * Should we ever need some kind of block ordering, better make this function
2464 * move blocks around than write a block ordering algorithm later... after all
2465 * the ast and ir should work together, not against each other.
2468 /* initexpr doesn't get its own block, it's pointless, it could create more blocks
2469 * anyway if for example it contains a ternary.
2473 if (!m_initexpr->codegen(func, false, &dummy))
2477 /* Store the block from which we enter this chaos */
2478 bin = func->m_curblock;
2480 /* The pre-loop condition needs its own block since we
2481 * need to be able to jump to the start of that expression.
2485 bprecond = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("pre_loop_cond"));
2489 /* the pre-loop-condition the least important place to 'continue' at */
2490 bcontinue = bprecond;
2493 func->m_curblock = bprecond;
2496 if (!m_precond->codegen(func, false, &precond))
2499 end_bprecond = func->m_curblock;
2501 bprecond = end_bprecond = nullptr;
2504 /* Now the next blocks won't be ordered nicely, but we need to
2505 * generate them this early for 'break' and 'continue'.
2508 bincrement = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("loop_increment"));
2511 bcontinue = bincrement; /* increment comes before the pre-loop-condition */
2513 bincrement = end_bincrement = nullptr;
2517 bpostcond = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("post_loop_cond"));
2520 bcontinue = bpostcond; /* postcond comes before the increment */
2522 bpostcond = end_bpostcond = nullptr;
2525 bout_id = func->m_ir_func->m_blocks.size();
2526 bout = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("after_loop"));
2531 /* The loop body... */
2534 bbody = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("loop_body"));
2539 func->m_curblock = bbody;
2541 func->m_breakblocks.push_back(bbreak);
2543 func->m_continueblocks.push_back(bcontinue);
2545 func->m_continueblocks.push_back(bbody);
2549 if (!m_body->codegen(func, false, &dummy))
2553 end_bbody = func->m_curblock;
2554 func->m_breakblocks.pop_back();
2555 func->m_continueblocks.pop_back();
2558 /* post-loop-condition */
2562 func->m_curblock = bpostcond;
2565 if (!m_postcond->codegen(func, false, &postcond))
2568 end_bpostcond = func->m_curblock;
2571 /* The incrementor */
2575 func->m_curblock = bincrement;
2578 if (!m_increment->codegen(func, false, &dummy))
2581 end_bincrement = func->m_curblock;
2584 /* In any case now, we continue from the outgoing block */
2585 func->m_curblock = bout;
2587 /* Now all blocks are in place */
2588 /* From 'bin' we jump to whatever comes first */
2589 if (bprecond) tmpblock = bprecond;
2590 else tmpblock = bbody; /* can never be null */
2593 else if (bpostcond) tmpblock = bpostcond;
2594 else tmpblock = bout;
2597 if (!ir_block_create_jump(bin, m_context, tmpblock))
2603 ir_block *ontrue, *onfalse;
2604 ontrue = bbody; /* can never be null */
2606 /* all of this is dead code
2607 else if (bincrement) ontrue = bincrement;
2608 else ontrue = bpostcond;
2617 if (!ir_block_create_if(end_bprecond, m_context, precond, ontrue, onfalse))
2624 if (bincrement) tmpblock = bincrement;
2625 else if (bpostcond) tmpblock = bpostcond;
2626 else if (bprecond) tmpblock = bprecond;
2627 else tmpblock = bbody;
2628 if (!end_bbody->m_final && !ir_block_create_jump(end_bbody, m_context, tmpblock))
2632 /* from increment */
2635 if (bpostcond) tmpblock = bpostcond;
2636 else if (bprecond) tmpblock = bprecond;
2637 else if (bbody) tmpblock = bbody;
2638 else tmpblock = bout;
2639 if (!ir_block_create_jump(end_bincrement, m_context, tmpblock))
2646 ir_block *ontrue, *onfalse;
2647 if (bprecond) ontrue = bprecond;
2648 else ontrue = bbody; /* can never be null */
2650 /* all of this is dead code
2651 else if (bincrement) ontrue = bincrement;
2652 else ontrue = bpostcond;
2661 if (!ir_block_create_if(end_bpostcond, m_context, postcond, ontrue, onfalse))
2665 /* Move 'bout' to the end */
2666 algo::shiftback(func->m_ir_func->m_blocks.begin() + bout_id,
2667 func->m_ir_func->m_blocks.end());
2669 //func->m_ir_func->m_blocks[bout_id].release(); // it's a vector<std::unique_ptr<>>
2670 //func->m_ir_func->m_blocks.erase(func->m_ir_func->m_blocks.begin() + bout_id);
2671 //func->m_ir_func->m_blocks.emplace_back(bout);
2676 bool ast_breakcont::codegen(ast_function *func, bool lvalue, ir_value **out)
2683 compile_error(m_context, "break/continue expression is not an l-value");
2688 compile_error(m_context, "internal error: ast_breakcont cannot be reused!");
2691 m_outr = (ir_value*)1;
2694 target = func->m_continueblocks[func->m_continueblocks.size()-1-m_levels];
2696 target = func->m_breakblocks[func->m_breakblocks.size()-1-m_levels];
2699 compile_error(m_context, "%s is lacking a target block", (m_is_continue ? "continue" : "break"));
2703 if (!ir_block_create_jump(func->m_curblock, m_context, target))
2708 bool ast_switch::codegen(ast_function *func, bool lvalue, ir_value **out)
2710 ast_switch_case *def_case = nullptr;
2711 ir_block *def_bfall = nullptr;
2712 ir_block *def_bfall_to = nullptr;
2713 bool set_def_bfall_to = false;
2715 ir_value *dummy = nullptr;
2716 ir_value *irop = nullptr;
2717 ir_block *bout = nullptr;
2718 ir_block *bfall = nullptr;
2725 compile_error(m_context, "switch expression is not an l-value");
2730 compile_error(m_context, "internal error: ast_switch cannot be reused!");
2733 m_outr = (ir_value*)1;
2738 if (!m_operand->codegen(func, false, &irop))
2741 if (m_cases.empty())
2744 cmpinstr = type_eq_instr[irop->m_vtype];
2745 if (cmpinstr >= VINSTR_END) {
2746 ast_type_to_string(m_operand, typestr, sizeof(typestr));
2747 compile_error(m_context, "invalid type to perform a switch on: %s", typestr);
2751 bout_id = func->m_ir_func->m_blocks.size();
2752 bout = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("after_switch"));
2756 /* setup the break block */
2757 func->m_breakblocks.push_back(bout);
2759 /* Now create all cases */
2760 for (auto &it : m_cases) {
2761 ir_value *cond, *val;
2762 ir_block *bcase, *bnot;
2765 ast_switch_case *swcase = ⁢
2767 if (swcase->m_value) {
2768 /* A regular case */
2769 /* generate the condition operand */
2770 if (!swcase->m_value->codegen(func, false, &val))
2772 /* generate the condition */
2773 cond = ir_block_create_binop(func->m_curblock, m_context, func->makeLabel("switch_eq"), cmpinstr, irop, val);
2777 bcase = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("case"));
2778 bnot_id = func->m_ir_func->m_blocks.size();
2779 bnot = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("not_case"));
2780 if (!bcase || !bnot)
2782 if (set_def_bfall_to) {
2783 set_def_bfall_to = false;
2784 def_bfall_to = bcase;
2786 if (!ir_block_create_if(func->m_curblock, m_context, cond, bcase, bnot))
2789 /* Make the previous case-end fall through */
2790 if (bfall && !bfall->m_final) {
2791 if (!ir_block_create_jump(bfall, m_context, bcase))
2795 /* enter the case */
2796 func->m_curblock = bcase;
2797 if (!swcase->m_code->codegen(func, false, &dummy))
2800 /* remember this block to fall through from */
2801 bfall = func->m_curblock;
2803 /* enter the else and move it down */
2804 func->m_curblock = bnot;
2805 algo::shiftback(func->m_ir_func->m_blocks.begin() + bnot_id,
2806 func->m_ir_func->m_blocks.end());
2808 //func->m_ir_func->m_blocks[bnot_id].release();
2809 //func->m_ir_func->m_blocks.erase(func->m_ir_func->m_blocks.begin() + bnot_id);
2810 //func->m_ir_func->m_blocks.emplace_back(bnot);
2812 /* The default case */
2813 /* Remember where to fall through from: */
2816 /* remember which case it was */
2818 /* And the next case will be remembered */
2819 set_def_bfall_to = true;
2823 /* Jump from the last bnot to bout */
2824 if (bfall && !bfall->m_final && !ir_block_create_jump(bfall, m_context, bout)) {
2826 astwarning(bfall->m_context, WARN_???, "missing break after last case");
2831 /* If there was a default case, put it down here */
2835 /* No need to create an extra block */
2836 bcase = func->m_curblock;
2838 /* Insert the fallthrough jump */
2839 if (def_bfall && !def_bfall->m_final) {
2840 if (!ir_block_create_jump(def_bfall, m_context, bcase))
2844 /* Now generate the default code */
2845 if (!def_case->m_code->codegen(func, false, &dummy))
2848 /* see if we need to fall through */
2849 if (def_bfall_to && !func->m_curblock->m_final)
2851 if (!ir_block_create_jump(func->m_curblock, m_context, def_bfall_to))
2856 /* Jump from the last bnot to bout */
2857 if (!func->m_curblock->m_final && !ir_block_create_jump(func->m_curblock, m_context, bout))
2859 /* enter the outgoing block */
2860 func->m_curblock = bout;
2862 /* restore the break block */
2863 func->m_breakblocks.pop_back();
2865 /* Move 'bout' to the end, it's nicer */
2866 algo::shiftback(func->m_ir_func->m_blocks.begin() + bout_id,
2867 func->m_ir_func->m_blocks.end());
2869 //func->m_ir_func->m_blocks[bout_id].release();
2870 //func->m_ir_func->m_blocks.erase(func->m_ir_func->m_blocks.begin() + bout_id);
2871 //func->m_ir_func->m_blocks.emplace_back(bout);
2876 bool ast_label::codegen(ast_function *func, bool lvalue, ir_value **out)
2881 compile_error(m_context, "internal error: ast_label never defined");
2887 compile_error(m_context, "internal error: ast_label cannot be an lvalue");
2891 /* simply create a new block and jump to it */
2892 m_irblock = ir_function_create_block(m_context, func->m_ir_func, m_name.c_str());
2894 compile_error(m_context, "failed to allocate label block `%s`", m_name);
2897 if (!func->m_curblock->m_final) {
2898 if (!ir_block_create_jump(func->m_curblock, m_context, m_irblock))
2902 /* enter the new block */
2903 func->m_curblock = m_irblock;
2905 /* Generate all the leftover gotos */
2906 for (auto &it : m_gotos) {
2907 if (!it->codegen(func, false, &dummy))
2914 bool ast_goto::codegen(ast_function *func, bool lvalue, ir_value **out)
2918 compile_error(m_context, "internal error: ast_goto cannot be an lvalue");
2922 if (m_target->m_irblock) {
2923 if (m_irblock_from) {
2924 /* we already tried once, this is the callback */
2925 m_irblock_from->m_final = false;
2926 if (!ir_block_create_goto(m_irblock_from, m_context, m_target->m_irblock)) {
2927 compile_error(m_context, "failed to generate goto to `%s`", m_name);
2933 if (!ir_block_create_goto(func->m_curblock, m_context, m_target->m_irblock)) {
2934 compile_error(m_context, "failed to generate goto to `%s`", m_name);
2941 /* the target has not yet been created...
2942 * close this block in a sneaky way:
2944 func->m_curblock->m_final = true;
2945 m_irblock_from = func->m_curblock;
2946 m_target->registerGoto(this);
2952 bool ast_state::codegen(ast_function *func, bool lvalue, ir_value **out)
2954 ir_value *frameval, *thinkval;
2957 compile_error(m_context, "not an l-value (state operation)");
2961 compile_error(m_context, "internal error: ast_state cannot be reused!");
2966 if (!m_framenum->codegen(func, false, &frameval))
2971 if (!m_nextthink->codegen(func, false, &thinkval))
2976 if (!ir_block_create_state_op(func->m_curblock, m_context, frameval, thinkval)) {
2977 compile_error(m_context, "failed to create STATE instruction");
2981 m_outr = (ir_value*)1;
2985 bool ast_call::codegen(ast_function *func, bool lvalue, ir_value **out)
2987 std::vector<ir_value*> params;
2988 ir_instr *callinstr;
2990 ir_value *funval = nullptr;
2992 /* return values are never lvalues */
2994 compile_error(m_context, "not an l-value (function call)");
3003 if (!m_func->codegen(func, false, &funval))
3009 for (auto &it : m_params) {
3011 if (!it->codegen(func, false, ¶m))
3015 params.push_back(param);
3018 /* varargs counter */
3021 ir_builder *builder = func->m_curblock->m_owner->m_owner;
3022 if (!m_va_count->codegen(func, false, &va_count))
3024 if (!ir_block_create_store_op(func->m_curblock, m_context, INSTR_STORE_F,
3025 ir_builder_get_va_count(builder), va_count))
3031 callinstr = ir_block_create_call(func->m_curblock, m_context,
3032 func->makeLabel("call"),
3033 funval, !!(m_func->m_flags & AST_FLAG_NORETURN));
3037 for (auto &it : params)
3038 ir_call_param(callinstr, it);
3040 *out = ir_call_value(callinstr);
3043 codegen_output_type(this, *out);