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(const ast_node *other)
29 if (other->m_side_effects)
30 m_side_effects = true;
33 /* General expression initialization */
34 ast_expression::ast_expression(lex_ctx_t ctx, int nodetype, qc_type type)
35 : ast_node(ctx, nodetype)
38 if (OPTS_OPTION_BOOL(OPTION_COVERAGE))
39 m_flags |= AST_FLAG_BLOCK_COVERAGE;
41 ast_expression::ast_expression(lex_ctx_t ctx, int nodetype)
42 : ast_expression(ctx, nodetype, TYPE_VOID)
45 ast_expression::~ast_expression()
53 ast_expression::ast_expression(ast_copy_type_t, const ast_expression &other)
54 : ast_expression(ast_copy_type, other.m_context, other)
57 ast_expression::ast_expression(ast_copy_type_t, lex_ctx_t ctx, const ast_expression &other)
58 : ast_expression(ast_copy_type, TYPE_ast_expression, ctx, other)
61 ast_expression::ast_expression(ast_copy_type_t, int nodetype, const ast_expression &other)
62 : ast_expression(ast_copy_type, nodetype, other.m_context, other)
65 ast_expression::ast_expression(ast_copy_type_t, int nodetype, lex_ctx_t ctx, const ast_expression &other)
66 : ast_expression(ctx, nodetype)
68 m_vtype = other.m_vtype;
69 m_count = other.m_count;
70 m_flags = other.m_flags;
72 m_next = new ast_expression(ast_copy_type, *other.m_next);
73 m_type_params.reserve(other.m_type_params.size());
74 for (auto &it : other.m_type_params)
75 m_type_params.emplace_back(new ast_value(ast_copy_type, *it));
79 ast_expression *ast_expression::shallowType(lex_ctx_t ctx, qc_type vtype) {
80 auto expr = new ast_expression(ctx, TYPE_ast_expression);
81 expr->m_vtype = vtype;
85 void ast_expression::adoptType(const ast_expression &other)
87 m_vtype = other.m_vtype;
89 m_next = new ast_expression(ast_copy_type, *other.m_next);
90 m_count = other.m_count;
91 m_flags = other.m_flags;
92 m_type_params.clear();
93 m_type_params.reserve(other.m_type_params.size());
94 for (auto &it : other.m_type_params)
95 m_type_params.emplace_back(new ast_value(ast_copy_type, *it));
98 bool ast_expression::compareType(const ast_expression &other) const
100 if (m_vtype == TYPE_NIL ||
101 other.m_vtype == TYPE_NIL)
103 if (m_vtype != other.m_vtype)
105 if (!m_next != !other.m_next)
107 if (m_type_params.size() != other.m_type_params.size())
109 if ((m_flags & AST_FLAG_TYPE_MASK) !=
110 (other.m_flags & AST_FLAG_TYPE_MASK) )
114 if (m_type_params.size()) {
116 for (i = 0; i < m_type_params.size(); ++i) {
117 if (!m_type_params[i]->compareType(*other.m_type_params[i]))
122 return m_next->compareType(*other.m_next);
126 bool ast_expression::codegen(ast_function*, bool, ir_value**) {
127 compile_error(m_context, "ast_expression::codegen called!");
132 ast_value::ast_value(ast_copy_type_t, const ast_value &other, const std::string &name)
133 : ast_value(ast_copy_type, static_cast<const ast_expression&>(other), name)
135 m_keep_node = true; // keep values, always
136 memset(&m_constval, 0, sizeof(m_constval));
139 ast_value::ast_value(ast_copy_type_t, const ast_value &other)
140 : ast_value(ast_copy_type, static_cast<const ast_expression&>(other), other.m_name)
142 m_keep_node = true; // keep values, always
143 memset(&m_constval, 0, sizeof(m_constval));
146 ast_value::ast_value(ast_copy_type_t, const ast_expression &other, const std::string &name)
147 : ast_expression(ast_copy_type, TYPE_ast_value, other)
150 m_keep_node = true; // keep values, always
151 memset(&m_constval, 0, sizeof(m_constval));
154 ast_value::ast_value(lex_ctx_t ctx, const std::string &name, qc_type t)
155 : ast_expression(ctx, TYPE_ast_value, t)
158 m_keep_node = true; // keep values, always
159 memset(&m_constval, 0, sizeof(m_constval));
162 ast_value::~ast_value()
165 mem_d((void*)m_argcounter);
170 mem_d((void*)m_constval.vstring);
173 // unlink us from the function node
174 m_constval.vfunc->m_function_type = nullptr;
176 // NOTE: delete function? currently collected in
177 // the parser structure
183 // initlist imples an array which implies .next in the expression exists.
184 if (m_initlist.size() && m_next->m_vtype == TYPE_STRING) {
185 for (auto &it : m_initlist)
191 static size_t ast_type_to_string_impl(const ast_expression *e, char *buf, size_t bufsize, size_t pos)
198 if (pos + 6 >= bufsize)
200 util_strncpy(buf + pos, "(null)", 6);
204 if (pos + 1 >= bufsize)
207 switch (e->m_vtype) {
209 util_strncpy(buf + pos, "(variant)", 9);
214 return ast_type_to_string_impl(e->m_next, buf, bufsize, pos);
217 if (pos + 3 >= bufsize)
221 pos = ast_type_to_string_impl(e->m_next, buf, bufsize, pos);
222 if (pos + 1 >= bufsize)
228 pos = ast_type_to_string_impl(e->m_next, buf, bufsize, pos);
229 if (pos + 2 >= bufsize)
231 if (e->m_type_params.empty()) {
237 pos = ast_type_to_string_impl(e->m_type_params[0].get(), buf, bufsize, pos);
238 for (i = 1; i < e->m_type_params.size(); ++i) {
239 if (pos + 2 >= bufsize)
243 pos = ast_type_to_string_impl(e->m_type_params[i].get(), buf, bufsize, pos);
245 if (pos + 1 >= bufsize)
251 pos = ast_type_to_string_impl(e->m_next, buf, bufsize, pos);
252 if (pos + 1 >= bufsize)
255 pos += util_snprintf(buf + pos, bufsize - pos - 1, "%i", (int)e->m_count);
256 if (pos + 1 >= bufsize)
262 typestr = type_name[e->m_vtype];
263 typelen = strlen(typestr);
264 if (pos + typelen >= bufsize)
266 util_strncpy(buf + pos, typestr, typelen);
267 return pos + typelen;
271 buf[bufsize-3] = '.';
272 buf[bufsize-2] = '.';
273 buf[bufsize-1] = '.';
277 void ast_type_to_string(const ast_expression *e, char *buf, size_t bufsize)
279 size_t pos = ast_type_to_string_impl(e, buf, bufsize-1, 0);
283 void ast_value::addParam(ast_value *p)
285 m_type_params.emplace_back(p);
288 ast_binary::ast_binary(lex_ctx_t ctx, int op,
289 ast_expression* left, ast_expression* right)
290 : ast_expression(ctx, TYPE_ast_binary)
292 // m_left/m_right happen after the peephole step right below
293 , m_right_first(false)
295 if (ast_istype(right, ast_unary) && OPTS_OPTIMIZATION(OPTIM_PEEPHOLE)) {
296 ast_unary *unary = ((ast_unary*)right);
297 ast_expression *normal = unary->m_operand;
299 /* make a-(-b) => a + b */
300 if (unary->m_op == VINSTR_NEG_F || unary->m_op == VINSTR_NEG_V) {
301 if (op == INSTR_SUB_F) {
304 ++opts_optimizationcount[OPTIM_PEEPHOLE];
305 } else if (op == INSTR_SUB_V) {
308 ++opts_optimizationcount[OPTIM_PEEPHOLE];
316 propagateSideEffects(left);
317 propagateSideEffects(right);
319 if (op >= INSTR_EQ_F && op <= INSTR_GT)
320 m_vtype = TYPE_FLOAT;
321 else if (op == INSTR_AND || op == INSTR_OR) {
322 if (OPTS_FLAG(PERL_LOGIC))
325 m_vtype = TYPE_FLOAT;
327 else if (op == INSTR_BITAND || op == INSTR_BITOR)
328 m_vtype = TYPE_FLOAT;
329 else if (op == INSTR_MUL_VF || op == INSTR_MUL_FV)
330 m_vtype = TYPE_VECTOR;
331 else if (op == INSTR_MUL_V)
332 m_vtype = TYPE_FLOAT;
334 m_vtype = left->m_vtype;
337 m_refs = AST_REF_ALL;
340 ast_binary::~ast_binary()
342 if (m_refs & AST_REF_LEFT) ast_unref(m_left);
343 if (m_refs & AST_REF_RIGHT) ast_unref(m_right);
346 ast_binstore::ast_binstore(lex_ctx_t ctx, int storop, int mathop,
347 ast_expression* left, ast_expression* right)
348 : ast_expression(ctx, TYPE_ast_binstore)
355 m_side_effects = true;
359 ast_binstore::~ast_binstore()
366 ast_unary* ast_unary::make(lex_ctx_t ctx, int op, ast_expression *expr)
368 if (ast_istype(expr, ast_unary) && OPTS_OPTIMIZATION(OPTIM_PEEPHOLE)) {
369 ast_unary *prev = (ast_unary*)((ast_unary*)expr)->m_operand;
371 /* Handle for double negation */
372 if (((ast_unary*)expr)->m_op == op)
373 prev = (ast_unary*)((ast_unary*)expr)->m_operand;
375 if (ast_istype(prev, ast_unary)) {
376 ++opts_optimizationcount[OPTIM_PEEPHOLE];
381 return new ast_unary(ctx, op, expr);
384 ast_unary::ast_unary(lex_ctx_t ctx, int op, ast_expression *expr)
385 : ast_expression(ctx, TYPE_ast_unary)
389 propagateSideEffects(expr);
390 if ((op >= INSTR_NOT_F && op <= INSTR_NOT_FNC) || op == VINSTR_NEG_F) {
391 m_vtype = TYPE_FLOAT;
392 } else if (op == VINSTR_NEG_V) {
393 m_vtype = TYPE_VECTOR;
395 compile_error(ctx, "cannot determine type of unary operation %s", util_instr_str[op]);
399 ast_unary::~ast_unary()
402 ast_unref(m_operand);
405 ast_return::ast_return(lex_ctx_t ctx, ast_expression *expr)
406 : ast_expression(ctx, TYPE_ast_return)
410 propagateSideEffects(expr);
413 ast_return::~ast_return()
416 ast_unref(m_operand);
419 ast_entfield::ast_entfield(lex_ctx_t ctx, ast_expression *entity, ast_expression *field)
420 : ast_entfield(ctx, entity, field, field->m_next)
422 if (field->m_vtype != TYPE_FIELD)
423 compile_error(ctx, "ast_entfield with expression not of type field");
426 ast_entfield::ast_entfield(lex_ctx_t ctx, ast_expression *entity, ast_expression *field, const ast_expression *outtype)
427 : ast_expression(ctx, TYPE_ast_entfield)
431 propagateSideEffects(m_entity);
432 propagateSideEffects(m_field);
435 compile_error(ctx, "ast_entfield: field has no type");
442 ast_entfield::~ast_entfield()
448 ast_member *ast_member::make(lex_ctx_t ctx, ast_expression *owner, unsigned int field, const std::string &name)
451 compile_error(ctx, "ast_member: invalid field (>=3): %u", field);
454 if (owner->m_vtype != TYPE_VECTOR &&
455 owner->m_vtype != TYPE_FIELD)
457 compile_error(ctx, "member-access on an invalid owner of type %s", type_name[owner->m_vtype]);
460 return new ast_member(ctx, owner, field, name);
463 ast_member::ast_member(lex_ctx_t ctx, ast_expression *owner, unsigned int field, const std::string &name)
464 : ast_expression(ctx, TYPE_ast_member)
472 if (m_owner->m_vtype == TYPE_VECTOR) {
473 m_vtype = TYPE_FLOAT;
476 m_vtype = TYPE_FIELD;
477 m_next = ast_expression::shallowType(ctx, TYPE_FLOAT);
480 propagateSideEffects(owner);
483 ast_member::~ast_member()
485 // The owner is always an ast_value, which has .keep_node=true,
486 // also: ast_members are usually deleted after the owner, thus
487 // this will cause invalid access
488 //ast_unref(self->m_owner);
489 // once we allow (expression).x to access a vector-member, we need
490 // to change this: preferably by creating an alternate ast node for this
491 // purpose that is not garbage-collected.
494 ast_array_index* ast_array_index::make(lex_ctx_t ctx, ast_expression *array, ast_expression *index)
496 ast_expression *outtype = array->m_next;
502 return new ast_array_index(ctx, array, index);
505 ast_array_index::ast_array_index(lex_ctx_t ctx, ast_expression *array, ast_expression *index)
506 : ast_expression(ctx, TYPE_ast_array_index)
510 propagateSideEffects(array);
511 propagateSideEffects(index);
513 ast_expression *outtype = m_array->m_next;
516 if (array->m_vtype == TYPE_FIELD && outtype->m_vtype == TYPE_ARRAY) {
517 // FIXME: investigate - this is not possible after adoptType
518 //if (m_vtype != TYPE_ARRAY) {
519 // compile_error(self->m_context, "array_index node on type");
520 // ast_array_index_delete(self);
525 m_vtype = TYPE_FIELD;
529 ast_array_index::~ast_array_index()
537 ast_argpipe::ast_argpipe(lex_ctx_t ctx, ast_expression *index)
538 : ast_expression(ctx, TYPE_ast_argpipe)
541 m_vtype = TYPE_NOEXPR;
544 ast_argpipe::~ast_argpipe()
550 ast_store::ast_store(lex_ctx_t ctx, int op, ast_expression *dest, ast_expression *source)
551 : ast_expression(ctx, TYPE_ast_store)
556 m_side_effects = true;
560 ast_store::~ast_store()
566 ast_ifthen::ast_ifthen(lex_ctx_t ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
567 : ast_expression(ctx, TYPE_ast_ifthen)
570 , m_on_false(onfalse)
572 propagateSideEffects(cond);
574 propagateSideEffects(ontrue);
576 propagateSideEffects(onfalse);
579 ast_ifthen::~ast_ifthen()
583 ast_unref(m_on_true);
585 ast_unref(m_on_false);
588 ast_ternary::ast_ternary(lex_ctx_t ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
589 : ast_expression(ctx, TYPE_ast_ternary)
592 , m_on_false(onfalse)
594 propagateSideEffects(cond);
595 propagateSideEffects(ontrue);
596 propagateSideEffects(onfalse);
598 if (ontrue->m_vtype == TYPE_NIL)
604 ast_ternary::~ast_ternary()
606 /* the if()s are only there because computed-gotos can set them
609 if (m_cond) ast_unref(m_cond);
610 if (m_on_true) ast_unref(m_on_true);
611 if (m_on_false) ast_unref(m_on_false);
614 ast_loop::ast_loop(lex_ctx_t ctx,
615 ast_expression *initexpr,
616 ast_expression *precond, bool pre_not,
617 ast_expression *postcond, bool post_not,
618 ast_expression *increment,
619 ast_expression *body)
620 : ast_expression(ctx, TYPE_ast_loop)
621 , m_initexpr(initexpr)
623 , m_postcond(postcond)
624 , m_increment(increment)
627 , m_post_not(post_not)
630 propagateSideEffects(initexpr);
632 propagateSideEffects(precond);
634 propagateSideEffects(postcond);
636 propagateSideEffects(increment);
638 propagateSideEffects(body);
641 ast_loop::~ast_loop()
644 ast_unref(m_initexpr);
646 ast_unref(m_precond);
648 ast_unref(m_postcond);
650 ast_unref(m_increment);
655 ast_breakcont::ast_breakcont(lex_ctx_t ctx, bool iscont, unsigned int levels)
656 : ast_expression(ctx, TYPE_ast_breakcont)
657 , m_is_continue(iscont)
662 ast_breakcont::~ast_breakcont()
666 ast_switch::ast_switch(lex_ctx_t ctx, ast_expression *op)
667 : ast_expression(ctx, TYPE_ast_switch)
670 propagateSideEffects(op);
673 ast_switch::~ast_switch()
675 ast_unref(m_operand);
677 for (auto &it : m_cases) {
679 ast_unref(it.m_value);
680 ast_unref(it.m_code);
684 ast_label::ast_label(lex_ctx_t ctx, const std::string &name, bool undefined)
685 : ast_expression(ctx, TYPE_ast_label)
688 , m_undefined(undefined)
690 m_vtype = TYPE_NOEXPR;
693 ast_label::~ast_label()
697 void ast_label::registerGoto(ast_goto *g)
699 m_gotos.push_back(g);
702 ast_goto::ast_goto(lex_ctx_t ctx, const std::string &name)
703 : ast_expression(ctx, TYPE_ast_goto)
706 , m_irblock_from(nullptr)
710 ast_goto::~ast_goto()
714 void ast_goto::setLabel(ast_label *label)
719 ast_state::ast_state(lex_ctx_t ctx, ast_expression *frame, ast_expression *think)
720 : ast_expression(ctx, TYPE_ast_expression)
726 ast_state::~ast_state()
729 ast_unref(m_framenum);
731 ast_unref(m_nextthink);
734 ast_call *ast_call::make(lex_ctx_t ctx, ast_expression *funcexpr)
736 if (!funcexpr->m_next) {
737 compile_error(ctx, "not a function");
740 return new ast_call(ctx, funcexpr);
743 ast_call::ast_call(lex_ctx_t ctx, ast_expression *funcexpr)
744 : ast_expression(ctx, TYPE_ast_call)
746 , m_va_count(nullptr)
748 m_side_effects = true;
749 adoptType(*funcexpr->m_next);
752 ast_call::~ast_call()
754 for (auto &it : m_params)
761 ast_unref(m_va_count);
764 bool ast_call::checkVararg(ast_expression *va_type, ast_expression *exp_type) const
770 if (!va_type || !va_type->compareType(*exp_type))
772 if (va_type && exp_type)
774 ast_type_to_string(va_type, tgot, sizeof(tgot));
775 ast_type_to_string(exp_type, texp, sizeof(texp));
776 if (OPTS_FLAG(UNSAFE_VARARGS)) {
777 if (compile_warning(m_context, WARN_UNSAFE_TYPES,
778 "piped variadic argument differs in type: constrained to type %s, expected type %s",
782 compile_error(m_context,
783 "piped variadic argument differs in type: constrained to type %s, expected type %s",
790 ast_type_to_string(exp_type, texp, sizeof(texp));
791 if (OPTS_FLAG(UNSAFE_VARARGS)) {
792 if (compile_warning(m_context, WARN_UNSAFE_TYPES,
793 "piped variadic argument may differ in type: expected type %s",
797 compile_error(m_context,
798 "piped variadic argument may differ in type: expected type %s",
807 bool ast_call::checkTypes(ast_expression *va_type) const
814 size_t count = m_params.size();
815 if (count > m_func->m_type_params.size())
816 count = m_func->m_type_params.size();
818 for (i = 0; i < count; ++i) {
819 if (ast_istype(m_params[i], ast_argpipe)) {
820 /* warn about type safety instead */
822 compile_error(m_context, "argpipe must be the last parameter to a function call");
825 if (!checkVararg(va_type, m_func->m_type_params[i].get()))
828 else if (!m_params[i]->compareType(*m_func->m_type_params[i]))
830 ast_type_to_string(m_params[i], tgot, sizeof(tgot));
831 ast_type_to_string(m_func->m_type_params[i].get(), texp, sizeof(texp));
832 compile_error(m_context, "invalid type for parameter %u in function call: expected %s, got %s",
833 (unsigned int)(i+1), texp, tgot);
834 /* we don't immediately return */
838 count = m_params.size();
839 if (count > m_func->m_type_params.size() && m_func->m_varparam) {
840 for (; i < count; ++i) {
841 if (ast_istype(m_params[i], ast_argpipe)) {
842 /* warn about type safety instead */
844 compile_error(m_context, "argpipe must be the last parameter to a function call");
847 if (!checkVararg(va_type, m_func->m_varparam))
850 else if (!m_params[i]->compareType(*m_func->m_varparam))
852 ast_type_to_string(m_params[i], tgot, sizeof(tgot));
853 ast_type_to_string(m_func->m_varparam, texp, sizeof(texp));
854 compile_error(m_context, "invalid type for variadic parameter %u in function call: expected %s, got %s",
855 (unsigned int)(i+1), texp, tgot);
856 /* we don't immediately return */
864 ast_block::ast_block(lex_ctx_t ctx)
865 : ast_expression(ctx, TYPE_ast_block)
869 ast_block::~ast_block()
871 for (auto &it : m_exprs) ast_unref(it);
872 for (auto &it : m_locals) delete it;
873 for (auto &it : m_collect) delete it;
876 void ast_block::setType(const ast_expression &from)
884 bool ast_block::addExpr(ast_expression *e)
886 propagateSideEffects(e);
887 m_exprs.push_back(e);
896 void ast_block::collect(ast_expression *expr)
898 m_collect.push_back(expr);
899 expr->m_keep_node = true;
902 ast_function *ast_function::make(lex_ctx_t ctx, const std::string &name, ast_value *vtype)
905 compile_error(ctx, "internal error: ast_function_new condition 0");
907 } else if (vtype->m_hasvalue || vtype->m_vtype != TYPE_FUNCTION) {
908 compile_error(ctx, "internal error: ast_function_new condition %i %i type=%i (probably 2 bodies?)",
910 (int)vtype->m_hasvalue,
914 return new ast_function(ctx, name, vtype);
917 ast_function::ast_function(lex_ctx_t ctx, const std::string &name, ast_value *vtype)
918 : ast_node(ctx, TYPE_ast_function)
919 , m_function_type(vtype)
924 , m_curblock(nullptr)
928 , m_fixedparams(nullptr)
929 , m_return_value(nullptr)
931 vtype->m_hasvalue = true;
932 vtype->m_constval.vfunc = this;
935 ast_function::~ast_function()
937 if (m_function_type) {
938 // ast_value_delete(m_function_type);
939 m_function_type->m_hasvalue = false;
940 m_function_type->m_constval.vfunc = nullptr;
941 // We use unref - if it was stored in a global table it is supposed
942 // to be deleted from *there*
943 ast_unref(m_function_type);
947 ast_unref(m_fixedparams);
949 ast_unref(m_return_value);
951 // force this to be cleared before m_varargs/m_argc as blocks might
952 // try to access them via ast_unref()
956 const char* ast_function::makeLabel(const char *prefix)
962 if (!OPTS_OPTION_BOOL(OPTION_DUMP) &&
963 !OPTS_OPTION_BOOL(OPTION_DUMPFIN) &&
964 !OPTS_OPTION_BOOL(OPTION_DEBUG))
969 id = (m_labelcount++);
970 len = strlen(prefix);
972 from = m_labelbuf + sizeof(m_labelbuf)-1;
975 *from-- = (id%10) + '0';
979 memcpy(from - len, prefix, len);
983 /*********************************************************************/
985 * by convention you must never pass nullptr to the 'ir_value **out'
986 * parameter. If you really don't care about the output, pass a dummy.
987 * But I can't imagine a pituation where the output is truly unnecessary.
990 static void codegen_output_type(ast_expression *self, ir_value *out)
992 if (out->m_vtype == TYPE_FIELD)
993 out->m_fieldtype = self->m_next->m_vtype;
994 if (out->m_vtype == TYPE_FUNCTION)
995 out->m_outtype = self->m_next->m_vtype;
998 bool ast_value::codegen(ast_function *func, bool lvalue, ir_value **out)
1002 if (m_vtype == TYPE_NIL) {
1003 *out = func->m_ir_func->m_owner->m_nil;
1006 // NOTE: This is the codegen for a variable used in an expression.
1007 // It is not the codegen to generate the value storage. For this purpose,
1008 // generateLocal and generateGlobal are to be used before this
1009 // is executed. ast_function::generateFunction should take care of its
1010 // locals, and the ast-user should take care of generateGlobal to be used
1011 // on all the globals.
1013 char tname[1024]; /* typename is reserved in C++ */
1014 ast_type_to_string(this, tname, sizeof(tname));
1015 compile_error(m_context, "ast_value used before generated %s %s", tname, m_name);
1022 bool ast_value::setGlobalArray()
1024 size_t count = m_initlist.size();
1027 if (count > m_count) {
1028 compile_error(m_context, "too many elements in initializer");
1031 else if (count < m_count) {
1033 compile_warning(m_context, "not all elements are initialized");
1037 for (i = 0; i != count; ++i) {
1038 switch (m_next->m_vtype) {
1040 if (!ir_value_set_float(m_ir_values[i], m_initlist[i].vfloat))
1044 if (!ir_value_set_vector(m_ir_values[i], m_initlist[i].vvec))
1048 if (!ir_value_set_string(m_ir_values[i], m_initlist[i].vstring))
1052 /* we don't support them in any other place yet either */
1053 compile_error(m_context, "TODO: nested arrays");
1056 /* this requiers a bit more work - similar to the fields I suppose */
1057 compile_error(m_context, "global of type function not properly generated");
1060 if (!m_initlist[i].vfield) {
1061 compile_error(m_context, "field constant without vfield set");
1064 if (!m_initlist[i].vfield->m_ir_v) {
1065 compile_error(m_context, "field constant generated before its field");
1068 if (!ir_value_set_field(m_ir_values[i], m_initlist[i].vfield->m_ir_v))
1072 compile_error(m_context, "TODO: global constant type %i", m_vtype);
1079 bool ast_value::checkArray(const ast_value &array) const
1081 if (array.m_flags & AST_FLAG_ARRAY_INIT && array.m_initlist.empty()) {
1082 compile_error(m_context, "array without size: %s", m_name);
1085 // we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements
1086 if (!array.m_count || array.m_count > OPTS_OPTION_U32(OPTION_MAX_ARRAY_SIZE)) {
1087 compile_error(m_context, "Invalid array of size %lu", (unsigned long)array.m_count);
1093 bool ast_value::generateGlobal(ir_builder *ir, bool isfield)
1095 if (m_vtype == TYPE_NIL) {
1096 compile_error(m_context, "internal error: trying to generate a variable of TYPE_NIL");
1100 if (m_hasvalue && m_vtype == TYPE_FUNCTION)
1101 return generateGlobalFunction(ir);
1103 if (isfield && m_vtype == TYPE_FIELD)
1104 return generateGlobalField(ir);
1106 ir_value *v = nullptr;
1107 if (m_vtype == TYPE_ARRAY) {
1108 v = prepareGlobalArray(ir);
1112 // Arrays don't do this since there's no "array" value which spans across the
1114 v = ir_builder_create_global(ir, m_name, m_vtype);
1116 compile_error(m_context, "ir_builder_create_global failed on `%s`", m_name);
1119 codegen_output_type(this, v);
1120 v->m_context = m_context;
1123 /* link us to the ir_value */
1127 if (m_flags & AST_FLAG_INCLUDE_DEF)
1128 m_ir_v->m_flags |= IR_FLAG_INCLUDE_DEF;
1129 if (m_flags & AST_FLAG_ERASEABLE)
1130 m_ir_v->m_flags |= IR_FLAG_ERASABLE;
1137 if (!ir_value_set_float(v, m_constval.vfloat))
1141 if (!ir_value_set_vector(v, m_constval.vvec))
1145 if (!ir_value_set_string(v, m_constval.vstring))
1149 if (!setGlobalArray())
1153 compile_error(m_context, "global of type function not properly generated");
1155 /* Cannot generate an IR value for a function,
1156 * need a pointer pointing to a function rather.
1159 if (!m_constval.vfield) {
1160 compile_error(m_context, "field constant without vfield set");
1163 if (!m_constval.vfield->m_ir_v) {
1164 compile_error(m_context, "field constant generated before its field");
1167 if (!ir_value_set_field(v, m_constval.vfield->m_ir_v))
1171 compile_error(m_context, "TODO: global constant type %i", m_vtype);
1179 bool ast_value::generateGlobalFunction(ir_builder *ir)
1181 ir_function *func = ir_builder_create_function(ir, m_name, m_next->m_vtype);
1184 func->m_context = m_context;
1185 func->m_value->m_context = m_context;
1187 m_constval.vfunc->m_ir_func = func;
1188 m_ir_v = func->m_value;
1189 if (m_flags & AST_FLAG_INCLUDE_DEF)
1190 m_ir_v->m_flags |= IR_FLAG_INCLUDE_DEF;
1191 if (m_flags & AST_FLAG_ERASEABLE)
1192 m_ir_v->m_flags |= IR_FLAG_ERASABLE;
1193 if (m_flags & AST_FLAG_BLOCK_COVERAGE)
1194 func->m_flags |= IR_FLAG_BLOCK_COVERAGE;
1195 // The function is filled later on ast_function::generateFunction...
1199 bool ast_value::generateGlobalField(ir_builder *ir)
1201 ast_expression *fieldtype = m_next;
1204 compile_error(m_context, "TODO: constant field pointers with value");
1208 if (fieldtype->m_vtype == TYPE_ARRAY) {
1209 if (!ast_istype(fieldtype, ast_value)) {
1210 compile_error(m_context, "internal error: ast_value required");
1213 ast_value *array = reinterpret_cast<ast_value*>(fieldtype);
1215 if (!checkArray(*array))
1218 ast_expression *elemtype = array->m_next;
1219 qc_type vtype = elemtype->m_vtype;
1221 ir_value *v = ir_builder_create_field(ir, m_name, vtype);
1223 compile_error(m_context, "ir_builder_create_global failed on `%s`", m_name);
1226 v->m_context = m_context;
1227 v->m_unique_life = true;
1229 array->m_ir_v = m_ir_v = v;
1231 if (m_flags & AST_FLAG_INCLUDE_DEF)
1232 m_ir_v->m_flags |= IR_FLAG_INCLUDE_DEF;
1233 if (m_flags & AST_FLAG_ERASEABLE)
1234 m_ir_v->m_flags |= IR_FLAG_ERASABLE;
1236 const size_t namelen = m_name.length();
1237 std::unique_ptr<char[]> name(new char[namelen+16]);
1238 util_strncpy(name.get(), m_name.c_str(), namelen);
1240 array->m_ir_values.resize(array->m_count);
1241 array->m_ir_values[0] = v;
1242 for (size_t ai = 1; ai < array->m_count; ++ai) {
1243 util_snprintf(name.get() + namelen, 16, "[%u]", (unsigned int)ai);
1244 array->m_ir_values[ai] = ir_builder_create_field(ir, name.get(), vtype);
1245 if (!array->m_ir_values[ai]) {
1246 compile_error(m_context, "ir_builder_create_global failed on `%s`", name.get());
1249 array->m_ir_values[ai]->m_context = m_context;
1250 array->m_ir_values[ai]->m_unique_life = true;
1251 array->m_ir_values[ai]->m_locked = true;
1252 if (m_flags & AST_FLAG_INCLUDE_DEF)
1253 m_ir_values[ai]->m_flags |= IR_FLAG_INCLUDE_DEF;
1258 ir_value *v = ir_builder_create_field(ir, m_name, m_next->m_vtype);
1261 v->m_context = m_context;
1263 if (m_flags & AST_FLAG_INCLUDE_DEF)
1264 m_ir_v->m_flags |= IR_FLAG_INCLUDE_DEF;
1266 if (m_flags & AST_FLAG_ERASEABLE)
1267 m_ir_v->m_flags |= IR_FLAG_ERASABLE;
1272 ir_value *ast_value::prepareGlobalArray(ir_builder *ir)
1274 ast_expression *elemtype = m_next;
1275 qc_type vtype = elemtype->m_vtype;
1277 if (m_flags & AST_FLAG_ARRAY_INIT && !m_count) {
1278 compile_error(m_context, "array `%s' has no size", m_name);
1282 /* same as with field arrays */
1283 if (!checkArray(*this))
1286 ir_value *v = ir_builder_create_global(ir, m_name, vtype);
1288 compile_error(m_context, "ir_builder_create_global failed `%s`", m_name);
1291 v->m_context = m_context;
1292 v->m_unique_life = true;
1295 if (m_flags & AST_FLAG_INCLUDE_DEF)
1296 v->m_flags |= IR_FLAG_INCLUDE_DEF;
1297 if (m_flags & AST_FLAG_ERASEABLE)
1298 m_ir_v->m_flags |= IR_FLAG_ERASABLE;
1300 const size_t namelen = m_name.length();
1301 std::unique_ptr<char[]> name(new char[namelen+16]);
1302 util_strncpy(name.get(), m_name.c_str(), namelen);
1304 m_ir_values.resize(m_count);
1306 for (size_t ai = 1; ai < m_count; ++ai) {
1307 util_snprintf(name.get() + namelen, 16, "[%u]", (unsigned int)ai);
1308 m_ir_values[ai] = ir_builder_create_global(ir, name.get(), vtype);
1309 if (!m_ir_values[ai]) {
1310 compile_error(m_context, "ir_builder_create_global failed `%s`", name.get());
1313 m_ir_values[ai]->m_context = m_context;
1314 m_ir_values[ai]->m_unique_life = true;
1315 m_ir_values[ai]->m_locked = true;
1316 if (m_flags & AST_FLAG_INCLUDE_DEF)
1317 m_ir_values[ai]->m_flags |= IR_FLAG_INCLUDE_DEF;
1323 bool ast_value::generateLocal(ir_function *func, bool param)
1325 if (m_vtype == TYPE_NIL) {
1326 compile_error(m_context, "internal error: trying to generate a variable of TYPE_NIL");
1330 if (m_hasvalue && m_vtype == TYPE_FUNCTION)
1332 /* Do we allow local functions? I think not...
1333 * this is NOT a function pointer atm.
1338 ir_value *v = nullptr;
1339 if (m_vtype == TYPE_ARRAY) {
1340 ast_expression *elemtype = m_next;
1341 qc_type vtype = elemtype->m_vtype;
1343 func->m_flags |= IR_FLAG_HAS_ARRAYS;
1345 if (param && !(m_flags & AST_FLAG_IS_VARARG)) {
1346 compile_error(m_context, "array-parameters are not supported");
1350 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1351 if (!checkArray(*this))
1354 m_ir_values.resize(m_count);
1355 v = ir_function_create_local(func, m_name, vtype, param);
1357 compile_error(m_context, "internal error: ir_function_create_local failed");
1360 v->m_context = m_context;
1361 v->m_unique_life = true;
1364 const size_t namelen = m_name.length();
1365 std::unique_ptr<char[]> name(new char[namelen+16]);
1366 util_strncpy(name.get(), m_name.c_str(), namelen);
1369 for (size_t ai = 1; ai < m_count; ++ai) {
1370 util_snprintf(name.get() + namelen, 16, "[%u]", (unsigned int)ai);
1371 m_ir_values[ai] = ir_function_create_local(func, name.get(), vtype, param);
1372 if (!m_ir_values[ai]) {
1373 compile_error(m_context, "internal_error: ir_builder_create_global failed on `%s`", name.get());
1376 m_ir_values[ai]->m_context = m_context;
1377 m_ir_values[ai]->m_unique_life = true;
1378 m_ir_values[ai]->m_locked = true;
1383 v = ir_function_create_local(func, m_name, m_vtype, param);
1386 codegen_output_type(this, v);
1387 v->m_context = m_context;
1390 // A constant local... hmmm...
1391 // I suppose the IR will have to deal with this
1396 if (!ir_value_set_float(v, m_constval.vfloat))
1400 if (!ir_value_set_vector(v, m_constval.vvec))
1404 if (!ir_value_set_string(v, m_constval.vstring))
1408 compile_error(m_context, "TODO: global constant type %i", m_vtype);
1413 // link us to the ir_value
1417 if (!generateAccessors(func->m_owner))
1421 error: /* clean up */
1426 bool ast_value::generateAccessors(ir_builder *ir)
1429 bool warn = OPTS_WARN(WARN_USED_UNINITIALIZED);
1430 if (!m_setter || !m_getter)
1432 if (m_count && m_ir_values.empty()) {
1433 compile_error(m_context, "internal error: no array values generated for `%s`", m_name);
1436 for (i = 0; i < m_count; ++i) {
1437 if (!m_ir_values[i]) {
1438 compile_error(m_context, "internal error: not all array values have been generated for `%s`", m_name);
1441 if (!m_ir_values[i]->m_life.empty()) {
1442 compile_error(m_context, "internal error: function containing `%s` already generated", m_name);
1447 opts_set(opts.warn, WARN_USED_UNINITIALIZED, false);
1449 if (!m_setter->generateGlobal(ir, false) ||
1450 !m_setter->m_constval.vfunc->generateFunction(ir) ||
1451 !ir_function_finalize(m_setter->m_constval.vfunc->m_ir_func))
1453 compile_error(m_context, "internal error: failed to generate setter for `%s`", m_name);
1454 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1459 if (!m_getter->generateGlobal(ir, false) ||
1460 !m_getter->m_constval.vfunc->generateFunction(ir) ||
1461 !ir_function_finalize(m_getter->m_constval.vfunc->m_ir_func))
1463 compile_error(m_context, "internal error: failed to generate getter for `%s`", m_name);
1464 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1468 for (i = 0; i < m_count; ++i)
1469 m_ir_values[i]->m_life.clear();
1470 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1474 bool ast_function::generateFunction(ir_builder *ir)
1480 ir_function *irf = m_ir_func;
1482 compile_error(m_context, "internal error: ast_function's related ast_value was not generated yet");
1486 /* fill the parameter list */
1487 for (auto &it : m_function_type->m_type_params) {
1488 if (it->m_vtype == TYPE_FIELD)
1489 vec_push(irf->m_params, it->m_next->m_vtype);
1491 vec_push(irf->m_params, it->m_vtype);
1493 if (!it->generateLocal(m_ir_func, true))
1499 if (!m_varargs->generateLocal(m_ir_func, true))
1501 irf->m_max_varargs = m_varargs->m_count;
1505 irf->m_builtin = m_builtin;
1509 /* have a local return value variable? */
1510 if (m_return_value) {
1511 if (!m_return_value->generateLocal(m_ir_func, false))
1515 if (m_blocks.empty()) {
1516 compile_error(m_context, "function `%s` has no body", m_name);
1520 irf->m_first = m_curblock = ir_function_create_block(m_context, irf, "entry");
1522 compile_error(m_context, "failed to allocate entry block for `%s`", m_name);
1530 if (!m_argc->generateLocal(m_ir_func, true))
1532 if (!m_argc->codegen(this, false, &va_count))
1534 if (!m_fixedparams->codegen(this, false, &fixed))
1536 sub = ir_block_create_binop(m_curblock, m_context,
1537 makeLabel("va_count"), INSTR_SUB_F,
1538 ir_builder_get_va_count(ir), fixed);
1541 if (!ir_block_create_store_op(m_curblock, m_context, INSTR_STORE_F,
1548 for (auto &it : m_blocks) {
1549 if (!it->codegen(this, false, &dummy))
1553 /* TODO: check return types */
1554 if (!m_curblock->m_final)
1556 if (!m_function_type->m_next ||
1557 m_function_type->m_next->m_vtype == TYPE_VOID)
1559 return ir_block_create_return(m_curblock, m_context, nullptr);
1561 else if (vec_size(m_curblock->m_entries) || m_curblock == irf->m_first)
1563 if (m_return_value) {
1564 if (!m_return_value->codegen(this, false, &dummy))
1566 return ir_block_create_return(m_curblock, m_context, dummy);
1568 else if (compile_warning(m_context, WARN_MISSING_RETURN_VALUES,
1569 "control reaches end of non-void function (`%s`) via %s",
1570 m_name.c_str(), m_curblock->m_label.c_str()))
1574 return ir_block_create_return(m_curblock, m_context, nullptr);
1580 static bool starts_a_label(const ast_expression *ex)
1582 while (ex && ast_istype(ex, ast_block)) {
1583 auto b = reinterpret_cast<const ast_block*>(ex);
1588 return ast_istype(ex, ast_label);
1591 /* Note, you will not see ast_block_codegen generate ir_blocks.
1592 * To the AST and the IR, blocks are 2 different things.
1593 * In the AST it represents a block of code, usually enclosed in
1594 * curly braces {...}.
1595 * While in the IR it represents a block in terms of control-flow.
1597 bool ast_block::codegen(ast_function *func, bool lvalue, ir_value **out)
1599 /* We don't use this
1600 * Note: an ast-representation using the comma-operator
1601 * of the form: (a, b, c) = x should not assign to c...
1604 compile_error(m_context, "not an l-value (code-block)");
1613 /* output is nullptr at first, we'll have each expression
1614 * assign to out output, thus, a comma-operator represention
1615 * using an ast_block will return the last generated value,
1616 * so: (b, c) + a executed both b and c, and returns c,
1617 * which is then added to a.
1621 /* generate locals */
1622 for (auto &it : m_locals) {
1623 if (!it->generateLocal(func->m_ir_func, false)) {
1624 if (OPTS_OPTION_BOOL(OPTION_DEBUG))
1625 compile_error(m_context, "failed to generate local `%s`", it->m_name);
1630 for (auto &it : m_exprs) {
1631 if (func->m_curblock->m_final && !starts_a_label(it)) {
1632 if (compile_warning(it->m_context, WARN_UNREACHABLE_CODE, "unreachable statement"))
1636 if (!it->codegen(func, false, out))
1645 bool ast_store::codegen(ast_function *func, bool lvalue, ir_value **out)
1647 ir_value *left = nullptr;
1648 ir_value *right = nullptr;
1651 ast_array_index *ai = nullptr;
1653 if (lvalue && m_outl) {
1658 if (!lvalue && m_outr) {
1663 if (ast_istype(m_dest, ast_array_index))
1666 ai = (ast_array_index*)m_dest;
1667 idx = (ast_value*)ai->m_index;
1669 if (ast_istype(ai->m_index, ast_value) && idx->m_hasvalue && idx->m_cvq == CV_CONST)
1674 /* we need to call the setter */
1675 ir_value *iridx, *funval;
1679 compile_error(m_context, "array-subscript assignment cannot produce lvalues");
1683 auto arr = reinterpret_cast<ast_value*>(ai->m_array);
1684 if (!ast_istype(ai->m_array, ast_value) || !arr->m_setter) {
1685 compile_error(m_context, "value has no setter (%s)", arr->m_name);
1689 if (!idx->codegen(func, false, &iridx))
1692 if (!arr->m_setter->codegen(func, true, &funval))
1695 if (!m_source->codegen(func, false, &right))
1698 call = ir_block_create_call(func->m_curblock, m_context, func->makeLabel("store"), funval, false);
1701 ir_call_param(call, iridx);
1702 ir_call_param(call, right);
1710 if (!m_dest->codegen(func, true, &left))
1715 if (!m_source->codegen(func, false, &right))
1718 if (!ir_block_create_store_op(func->m_curblock, m_context, m_op, left, right))
1723 /* Theoretically, an assinment returns its left side as an
1724 * lvalue, if we don't need an lvalue though, we return
1725 * the right side as an rvalue, otherwise we have to
1726 * somehow know whether or not we need to dereference the pointer
1727 * on the left side - that is: OP_LOAD if it was an address.
1728 * Also: in original QC we cannot OP_LOADP *anyway*.
1730 *out = (lvalue ? left : right);
1735 bool ast_binary::codegen(ast_function *func, bool lvalue, ir_value **out)
1737 ir_value *left, *right;
1739 /* A binary operation cannot yield an l-value */
1741 compile_error(m_context, "not an l-value (binop)");
1750 if ((OPTS_FLAG(SHORT_LOGIC) || OPTS_FLAG(PERL_LOGIC)) &&
1751 (m_op == INSTR_AND || m_op == INSTR_OR))
1753 /* NOTE: The short-logic path will ignore right_first */
1755 /* short circuit evaluation */
1756 ir_block *other, *merge;
1757 ir_block *from_left, *from_right;
1761 /* prepare end-block */
1762 merge_id = func->m_ir_func->m_blocks.size();
1763 merge = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("sce_merge"));
1765 /* generate the left expression */
1766 if (!m_left->codegen(func, false, &left))
1768 /* remember the block */
1769 from_left = func->m_curblock;
1771 /* create a new block for the right expression */
1772 other = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("sce_other"));
1773 if (m_op == INSTR_AND) {
1774 /* on AND: left==true -> other */
1775 if (!ir_block_create_if(func->m_curblock, m_context, left, other, merge))
1778 /* on OR: left==false -> other */
1779 if (!ir_block_create_if(func->m_curblock, m_context, left, merge, other))
1782 /* use the likely flag */
1783 vec_last(func->m_curblock->m_instr)->m_likely = true;
1785 /* enter the right-expression's block */
1786 func->m_curblock = other;
1788 if (!m_right->codegen(func, false, &right))
1790 /* remember block */
1791 from_right = func->m_curblock;
1793 /* jump to the merge block */
1794 if (!ir_block_create_jump(func->m_curblock, m_context, merge))
1797 algo::shiftback(func->m_ir_func->m_blocks.begin() + merge_id,
1798 func->m_ir_func->m_blocks.end());
1800 //func->m_ir_func->m_blocks[merge_id].release();
1801 //func->m_ir_func->m_blocks.erase(func->m_ir_func->m_blocks.begin() + merge_id);
1802 //func->m_ir_func->m_blocks.emplace_back(merge);
1804 func->m_curblock = merge;
1805 phi = ir_block_create_phi(func->m_curblock, m_context,
1806 func->makeLabel("sce_value"),
1808 ir_phi_add(phi, from_left, left);
1809 ir_phi_add(phi, from_right, right);
1810 *out = ir_phi_value(phi);
1814 if (!OPTS_FLAG(PERL_LOGIC)) {
1816 if (OPTS_FLAG(CORRECT_LOGIC) && (*out)->m_vtype == TYPE_VECTOR) {
1817 *out = ir_block_create_unary(func->m_curblock, m_context,
1818 func->makeLabel("sce_bool_v"),
1822 *out = ir_block_create_unary(func->m_curblock, m_context,
1823 func->makeLabel("sce_bool"),
1828 else if (OPTS_FLAG(FALSE_EMPTY_STRINGS) && (*out)->m_vtype == TYPE_STRING) {
1829 *out = ir_block_create_unary(func->m_curblock, m_context,
1830 func->makeLabel("sce_bool_s"),
1834 *out = ir_block_create_unary(func->m_curblock, m_context,
1835 func->makeLabel("sce_bool"),
1841 *out = ir_block_create_binop(func->m_curblock, m_context,
1842 func->makeLabel("sce_bool"),
1843 INSTR_AND, *out, *out);
1850 codegen_output_type(this, *out);
1854 if (m_right_first) {
1855 if (!m_right->codegen(func, false, &right))
1857 if (!m_left->codegen(func, false, &left))
1860 if (!m_left->codegen(func, false, &left))
1862 if (!m_right->codegen(func, false, &right))
1866 *out = ir_block_create_binop(func->m_curblock, m_context, func->makeLabel("bin"),
1871 codegen_output_type(this, *out);
1876 bool ast_binstore::codegen(ast_function *func, bool lvalue, ir_value **out)
1878 ir_value *leftl = nullptr, *leftr, *right, *bin;
1882 ast_array_index *ai = nullptr;
1883 ir_value *iridx = nullptr;
1885 if (lvalue && m_outl) {
1890 if (!lvalue && m_outr) {
1895 if (ast_istype(m_dest, ast_array_index))
1898 ai = (ast_array_index*)m_dest;
1899 idx = (ast_value*)ai->m_index;
1901 if (ast_istype(ai->m_index, ast_value) && idx->m_hasvalue && idx->m_cvq == CV_CONST)
1905 /* for a binstore we need both an lvalue and an rvalue for the left side */
1906 /* rvalue of destination! */
1908 if (!idx->codegen(func, false, &iridx))
1911 if (!m_dest->codegen(func, false, &leftr))
1914 /* source as rvalue only */
1915 if (!m_source->codegen(func, false, &right))
1918 /* now the binary */
1919 bin = ir_block_create_binop(func->m_curblock, m_context, func->makeLabel("binst"),
1920 m_opbin, leftr, right);
1924 /* we need to call the setter */
1929 compile_error(m_context, "array-subscript assignment cannot produce lvalues");
1933 arr = (ast_value*)ai->m_array;
1934 if (!ast_istype(ai->m_array, ast_value) || !arr->m_setter) {
1935 compile_error(m_context, "value has no setter (%s)", arr->m_name);
1939 if (!arr->m_setter->codegen(func, true, &funval))
1942 call = ir_block_create_call(func->m_curblock, m_context, func->makeLabel("store"), funval, false);
1945 ir_call_param(call, iridx);
1946 ir_call_param(call, bin);
1950 // lvalue of destination
1951 if (!m_dest->codegen(func, true, &leftl))
1955 if (!ir_block_create_store_op(func->m_curblock, m_context, m_opstore, leftl, bin))
1960 /* Theoretically, an assinment returns its left side as an
1961 * lvalue, if we don't need an lvalue though, we return
1962 * the right side as an rvalue, otherwise we have to
1963 * somehow know whether or not we need to dereference the pointer
1964 * on the left side - that is: OP_LOAD if it was an address.
1965 * Also: in original QC we cannot OP_LOADP *anyway*.
1967 *out = (lvalue ? leftl : bin);
1972 bool ast_unary::codegen(ast_function *func, bool lvalue, ir_value **out)
1976 /* An unary operation cannot yield an l-value */
1978 compile_error(m_context, "not an l-value (binop)");
1988 if (!m_operand->codegen(func, false, &operand))
1991 *out = ir_block_create_unary(func->m_curblock, m_context, func->makeLabel("unary"),
2000 bool ast_return::codegen(ast_function *func, bool lvalue, ir_value **out)
2006 /* In the context of a return operation, we don't actually return
2010 compile_error(m_context, "return-expression is not an l-value");
2015 compile_error(m_context, "internal error: ast_return cannot be reused, it bears no result!");
2018 m_outr = (ir_value*)1;
2022 if (!m_operand->codegen(func, false, &operand))
2025 if (!ir_block_create_return(func->m_curblock, m_context, operand))
2028 if (!ir_block_create_return(func->m_curblock, m_context, nullptr))
2035 bool ast_entfield::codegen(ast_function *func, bool lvalue, ir_value **out)
2037 ir_value *ent, *field;
2039 // This function needs to take the 'lvalue' flag into account!
2040 // As lvalue we provide a field-pointer, as rvalue we provide the
2043 if (lvalue && m_outl) {
2048 if (!lvalue && m_outr) {
2053 if (!m_entity->codegen(func, false, &ent))
2056 if (!m_field->codegen(func, false, &field))
2061 *out = ir_block_create_fieldaddress(func->m_curblock, m_context, func->makeLabel("efa"),
2064 *out = ir_block_create_load_from_ent(func->m_curblock, m_context, func->makeLabel("efv"),
2065 ent, field, m_vtype);
2066 /* Done AFTER error checking:
2067 codegen_output_type(this, *out);
2071 compile_error(m_context, "failed to create %s instruction (output type %s)",
2072 (lvalue ? "ADDRESS" : "FIELD"),
2073 type_name[m_vtype]);
2077 codegen_output_type(this, *out);
2084 // Hm that should be it...
2088 bool ast_member::codegen(ast_function *func, bool lvalue, ir_value **out)
2092 /* in QC this is always an lvalue */
2093 if (lvalue && m_rvalue) {
2094 compile_error(m_context, "not an l-value (member access)");
2102 if (!m_owner->codegen(func, false, &vec))
2105 if (vec->m_vtype != TYPE_VECTOR &&
2106 !(vec->m_vtype == TYPE_FIELD && m_owner->m_next->m_vtype == TYPE_VECTOR))
2111 *out = ir_value_vector_member(vec, m_field);
2114 return (*out != nullptr);
2117 bool ast_array_index::codegen(ast_function *func, bool lvalue, ir_value **out)
2122 if (!lvalue && m_outr) {
2126 if (lvalue && m_outl) {
2131 if (!ast_istype(m_array, ast_value)) {
2132 compile_error(m_context, "array indexing this way is not supported");
2133 /* note this would actually be pointer indexing because the left side is
2134 * not an actual array but (hopefully) an indexable expression.
2135 * Once we get integer arithmetic, and GADDRESS/GSTORE/GLOAD instruction
2136 * support this path will be filled.
2141 arr = reinterpret_cast<ast_value*>(m_array);
2142 idx = reinterpret_cast<ast_value*>(m_index);
2144 if (!ast_istype(m_index, ast_value) || !idx->m_hasvalue || idx->m_cvq != CV_CONST) {
2145 /* Time to use accessor functions */
2146 ir_value *iridx, *funval;
2150 compile_error(m_context, "(.2) array indexing here needs a compile-time constant");
2154 if (!arr->m_getter) {
2155 compile_error(m_context, "value has no getter, don't know how to index it");
2159 if (!m_index->codegen(func, false, &iridx))
2162 if (!arr->m_getter->codegen(func, true, &funval))
2165 call = ir_block_create_call(func->m_curblock, m_context, func->makeLabel("fetch"), funval, false);
2168 ir_call_param(call, iridx);
2170 *out = ir_call_value(call);
2172 (*out)->m_vtype = m_vtype;
2173 codegen_output_type(this, *out);
2177 if (idx->m_vtype == TYPE_FLOAT) {
2178 unsigned int arridx = idx->m_constval.vfloat;
2179 if (arridx >= m_array->m_count)
2181 compile_error(m_context, "array index out of bounds: %i", arridx);
2184 *out = arr->m_ir_values[arridx];
2186 else if (idx->m_vtype == TYPE_INTEGER) {
2187 unsigned int arridx = idx->m_constval.vint;
2188 if (arridx >= m_array->m_count)
2190 compile_error(m_context, "array index out of bounds: %i", arridx);
2193 *out = arr->m_ir_values[arridx];
2196 compile_error(m_context, "array indexing here needs an integer constant");
2199 (*out)->m_vtype = m_vtype;
2200 codegen_output_type(this, *out);
2204 bool ast_argpipe::codegen(ast_function *func, bool lvalue, ir_value **out)
2208 compile_error(m_context, "argpipe node: not an lvalue");
2213 compile_error(m_context, "TODO: argpipe codegen not implemented");
2217 bool ast_ifthen::codegen(ast_function *func, bool lvalue, ir_value **out)
2225 ir_block *ontrue_endblock = nullptr;
2226 ir_block *onfalse_endblock = nullptr;
2227 ir_block *merge = nullptr;
2230 /* We don't output any value, thus also don't care about r/lvalue */
2235 compile_error(m_context, "internal error: ast_ifthen cannot be reused, it bears no result!");
2238 m_outr = (ir_value*)1;
2240 /* generate the condition */
2241 if (!m_cond->codegen(func, false, &condval))
2243 /* update the block which will get the jump - because short-logic or ternaries may have changed this */
2244 cond = func->m_curblock;
2246 /* try constant folding away the condition */
2247 if ((folded = fold::cond_ifthen(condval, func, this)) != -1)
2251 /* create on-true block */
2252 ontrue = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("ontrue"));
2256 /* enter the block */
2257 func->m_curblock = ontrue;
2260 if (!m_on_true->codegen(func, false, &dummy))
2263 /* we now need to work from the current endpoint */
2264 ontrue_endblock = func->m_curblock;
2270 /* create on-false block */
2271 onfalse = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("onfalse"));
2275 /* enter the block */
2276 func->m_curblock = onfalse;
2279 if (!m_on_false->codegen(func, false, &dummy))
2282 /* we now need to work from the current endpoint */
2283 onfalse_endblock = func->m_curblock;
2287 /* Merge block were they all merge in to */
2288 if (!ontrue || !onfalse || !ontrue_endblock->m_final || !onfalse_endblock->m_final)
2290 merge = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("endif"));
2293 /* add jumps ot the merge block */
2294 if (ontrue && !ontrue_endblock->m_final && !ir_block_create_jump(ontrue_endblock, m_context, merge))
2296 if (onfalse && !onfalse_endblock->m_final && !ir_block_create_jump(onfalse_endblock, m_context, merge))
2299 /* Now enter the merge block */
2300 func->m_curblock = merge;
2303 /* we create the if here, that way all blocks are ordered :)
2305 if (!ir_block_create_if(cond, m_context, condval,
2306 (ontrue ? ontrue : merge),
2307 (onfalse ? onfalse : merge)))
2315 bool ast_ternary::codegen(ast_function *func, bool lvalue, ir_value **out)
2318 ir_value *trueval, *falseval;
2321 ir_block *cond = func->m_curblock;
2322 ir_block *cond_out = nullptr;
2323 ir_block *ontrue, *ontrue_out = nullptr;
2324 ir_block *onfalse, *onfalse_out = nullptr;
2328 /* Ternary can never create an lvalue... */
2332 /* In theory it shouldn't be possible to pass through a node twice, but
2333 * in case we add any kind of optimization pass for the AST itself, it
2334 * may still happen, thus we remember a created ir_value and simply return one
2335 * if it already exists.
2342 /* In the following, contraty to ast_ifthen, we assume both paths exist. */
2344 /* generate the condition */
2345 func->m_curblock = cond;
2346 if (!m_cond->codegen(func, false, &condval))
2348 cond_out = func->m_curblock;
2350 /* try constant folding away the condition */
2351 if ((folded = fold::cond_ternary(condval, func, this)) != -1)
2354 /* create on-true block */
2355 ontrue = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("tern_T"));
2360 /* enter the block */
2361 func->m_curblock = ontrue;
2364 if (!m_on_true->codegen(func, false, &trueval))
2367 ontrue_out = func->m_curblock;
2370 /* create on-false block */
2371 onfalse = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("tern_F"));
2376 /* enter the block */
2377 func->m_curblock = onfalse;
2380 if (!m_on_false->codegen(func, false, &falseval))
2383 onfalse_out = func->m_curblock;
2386 /* create merge block */
2387 merge = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("tern_out"));
2390 /* jump to merge block */
2391 if (!ir_block_create_jump(ontrue_out, m_context, merge))
2393 if (!ir_block_create_jump(onfalse_out, m_context, merge))
2396 /* create if instruction */
2397 if (!ir_block_create_if(cond_out, m_context, condval, ontrue, onfalse))
2400 /* Now enter the merge block */
2401 func->m_curblock = merge;
2403 /* Here, now, we need a PHI node
2404 * but first some sanity checking...
2406 if (trueval->m_vtype != falseval->m_vtype && trueval->m_vtype != TYPE_NIL && falseval->m_vtype != TYPE_NIL) {
2407 /* error("ternary with different types on the two sides"); */
2408 compile_error(m_context, "internal error: ternary operand types invalid");
2413 phi = ir_block_create_phi(merge, m_context, func->makeLabel("phi"), m_vtype);
2415 compile_error(m_context, "internal error: failed to generate phi node");
2418 ir_phi_add(phi, ontrue_out, trueval);
2419 ir_phi_add(phi, onfalse_out, falseval);
2421 m_outr = ir_phi_value(phi);
2424 codegen_output_type(this, *out);
2429 bool ast_loop::codegen(ast_function *func, bool lvalue, ir_value **out)
2431 ir_value *dummy = nullptr;
2432 ir_value *precond = nullptr;
2433 ir_value *postcond = nullptr;
2435 /* Since we insert some jumps "late" so we have blocks
2436 * ordered "nicely", we need to keep track of the actual end-blocks
2437 * of expressions to add the jumps to.
2439 ir_block *bbody = nullptr, *end_bbody = nullptr;
2440 ir_block *bprecond = nullptr, *end_bprecond = nullptr;
2441 ir_block *bpostcond = nullptr, *end_bpostcond = nullptr;
2442 ir_block *bincrement = nullptr, *end_bincrement = nullptr;
2443 ir_block *bout = nullptr, *bin = nullptr;
2445 /* let's at least move the outgoing block to the end */
2448 /* 'break' and 'continue' need to be able to find the right blocks */
2449 ir_block *bcontinue = nullptr;
2450 ir_block *bbreak = nullptr;
2452 ir_block *tmpblock = nullptr;
2458 compile_error(m_context, "internal error: ast_loop cannot be reused, it bears no result!");
2461 m_outr = (ir_value*)1;
2464 * Should we ever need some kind of block ordering, better make this function
2465 * move blocks around than write a block ordering algorithm later... after all
2466 * the ast and ir should work together, not against each other.
2469 /* initexpr doesn't get its own block, it's pointless, it could create more blocks
2470 * anyway if for example it contains a ternary.
2474 if (!m_initexpr->codegen(func, false, &dummy))
2478 /* Store the block from which we enter this chaos */
2479 bin = func->m_curblock;
2481 /* The pre-loop condition needs its own block since we
2482 * need to be able to jump to the start of that expression.
2486 bprecond = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("pre_loop_cond"));
2490 /* the pre-loop-condition the least important place to 'continue' at */
2491 bcontinue = bprecond;
2494 func->m_curblock = bprecond;
2497 if (!m_precond->codegen(func, false, &precond))
2500 end_bprecond = func->m_curblock;
2502 bprecond = end_bprecond = nullptr;
2505 /* Now the next blocks won't be ordered nicely, but we need to
2506 * generate them this early for 'break' and 'continue'.
2509 bincrement = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("loop_increment"));
2512 bcontinue = bincrement; /* increment comes before the pre-loop-condition */
2514 bincrement = end_bincrement = nullptr;
2518 bpostcond = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("post_loop_cond"));
2521 bcontinue = bpostcond; /* postcond comes before the increment */
2523 bpostcond = end_bpostcond = nullptr;
2526 bout_id = func->m_ir_func->m_blocks.size();
2527 bout = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("after_loop"));
2532 /* The loop body... */
2535 bbody = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("loop_body"));
2540 func->m_curblock = bbody;
2542 func->m_breakblocks.push_back(bbreak);
2544 func->m_continueblocks.push_back(bcontinue);
2546 func->m_continueblocks.push_back(bbody);
2550 if (!m_body->codegen(func, false, &dummy))
2554 end_bbody = func->m_curblock;
2555 func->m_breakblocks.pop_back();
2556 func->m_continueblocks.pop_back();
2559 /* post-loop-condition */
2563 func->m_curblock = bpostcond;
2566 if (!m_postcond->codegen(func, false, &postcond))
2569 end_bpostcond = func->m_curblock;
2572 /* The incrementor */
2576 func->m_curblock = bincrement;
2579 if (!m_increment->codegen(func, false, &dummy))
2582 end_bincrement = func->m_curblock;
2585 /* In any case now, we continue from the outgoing block */
2586 func->m_curblock = bout;
2588 /* Now all blocks are in place */
2589 /* From 'bin' we jump to whatever comes first */
2590 if (bprecond) tmpblock = bprecond;
2591 else tmpblock = bbody; /* can never be null */
2594 else if (bpostcond) tmpblock = bpostcond;
2595 else tmpblock = bout;
2598 if (!ir_block_create_jump(bin, m_context, tmpblock))
2604 ir_block *ontrue, *onfalse;
2605 ontrue = bbody; /* can never be null */
2607 /* all of this is dead code
2608 else if (bincrement) ontrue = bincrement;
2609 else ontrue = bpostcond;
2618 if (!ir_block_create_if(end_bprecond, m_context, precond, ontrue, onfalse))
2625 if (bincrement) tmpblock = bincrement;
2626 else if (bpostcond) tmpblock = bpostcond;
2627 else if (bprecond) tmpblock = bprecond;
2628 else tmpblock = bbody;
2629 if (!end_bbody->m_final && !ir_block_create_jump(end_bbody, m_context, tmpblock))
2633 /* from increment */
2636 if (bpostcond) tmpblock = bpostcond;
2637 else if (bprecond) tmpblock = bprecond;
2638 else if (bbody) tmpblock = bbody;
2639 else tmpblock = bout;
2640 if (!ir_block_create_jump(end_bincrement, m_context, tmpblock))
2647 ir_block *ontrue, *onfalse;
2648 if (bprecond) ontrue = bprecond;
2649 else ontrue = bbody; /* can never be null */
2651 /* all of this is dead code
2652 else if (bincrement) ontrue = bincrement;
2653 else ontrue = bpostcond;
2662 if (!ir_block_create_if(end_bpostcond, m_context, postcond, ontrue, onfalse))
2666 /* Move 'bout' to the end */
2667 algo::shiftback(func->m_ir_func->m_blocks.begin() + bout_id,
2668 func->m_ir_func->m_blocks.end());
2670 //func->m_ir_func->m_blocks[bout_id].release(); // it's a vector<std::unique_ptr<>>
2671 //func->m_ir_func->m_blocks.erase(func->m_ir_func->m_blocks.begin() + bout_id);
2672 //func->m_ir_func->m_blocks.emplace_back(bout);
2677 bool ast_breakcont::codegen(ast_function *func, bool lvalue, ir_value **out)
2684 compile_error(m_context, "break/continue expression is not an l-value");
2689 compile_error(m_context, "internal error: ast_breakcont cannot be reused!");
2692 m_outr = (ir_value*)1;
2695 target = func->m_continueblocks[func->m_continueblocks.size()-1-m_levels];
2697 target = func->m_breakblocks[func->m_breakblocks.size()-1-m_levels];
2700 compile_error(m_context, "%s is lacking a target block", (m_is_continue ? "continue" : "break"));
2704 if (!ir_block_create_jump(func->m_curblock, m_context, target))
2709 bool ast_switch::codegen(ast_function *func, bool lvalue, ir_value **out)
2711 ast_switch_case *def_case = nullptr;
2712 ir_block *def_bfall = nullptr;
2713 ir_block *def_bfall_to = nullptr;
2714 bool set_def_bfall_to = false;
2716 ir_value *dummy = nullptr;
2717 ir_value *irop = nullptr;
2718 ir_block *bout = nullptr;
2719 ir_block *bfall = nullptr;
2726 compile_error(m_context, "switch expression is not an l-value");
2731 compile_error(m_context, "internal error: ast_switch cannot be reused!");
2734 m_outr = (ir_value*)1;
2739 if (!m_operand->codegen(func, false, &irop))
2742 if (m_cases.empty())
2745 cmpinstr = type_eq_instr[irop->m_vtype];
2746 if (cmpinstr >= VINSTR_END) {
2747 ast_type_to_string(m_operand, typestr, sizeof(typestr));
2748 compile_error(m_context, "invalid type to perform a switch on: %s", typestr);
2752 bout_id = func->m_ir_func->m_blocks.size();
2753 bout = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("after_switch"));
2757 /* setup the break block */
2758 func->m_breakblocks.push_back(bout);
2760 /* Now create all cases */
2761 for (auto &it : m_cases) {
2762 ir_value *cond, *val;
2763 ir_block *bcase, *bnot;
2766 ast_switch_case *swcase = ⁢
2768 if (swcase->m_value) {
2769 /* A regular case */
2770 /* generate the condition operand */
2771 if (!swcase->m_value->codegen(func, false, &val))
2773 /* generate the condition */
2774 cond = ir_block_create_binop(func->m_curblock, m_context, func->makeLabel("switch_eq"), cmpinstr, irop, val);
2778 bcase = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("case"));
2779 bnot_id = func->m_ir_func->m_blocks.size();
2780 bnot = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("not_case"));
2781 if (!bcase || !bnot)
2783 if (set_def_bfall_to) {
2784 set_def_bfall_to = false;
2785 def_bfall_to = bcase;
2787 if (!ir_block_create_if(func->m_curblock, m_context, cond, bcase, bnot))
2790 /* Make the previous case-end fall through */
2791 if (bfall && !bfall->m_final) {
2792 if (!ir_block_create_jump(bfall, m_context, bcase))
2796 /* enter the case */
2797 func->m_curblock = bcase;
2798 if (!swcase->m_code->codegen(func, false, &dummy))
2801 /* remember this block to fall through from */
2802 bfall = func->m_curblock;
2804 /* enter the else and move it down */
2805 func->m_curblock = bnot;
2806 algo::shiftback(func->m_ir_func->m_blocks.begin() + bnot_id,
2807 func->m_ir_func->m_blocks.end());
2809 //func->m_ir_func->m_blocks[bnot_id].release();
2810 //func->m_ir_func->m_blocks.erase(func->m_ir_func->m_blocks.begin() + bnot_id);
2811 //func->m_ir_func->m_blocks.emplace_back(bnot);
2813 /* The default case */
2814 /* Remember where to fall through from: */
2817 /* remember which case it was */
2819 /* And the next case will be remembered */
2820 set_def_bfall_to = true;
2824 /* Jump from the last bnot to bout */
2825 if (bfall && !bfall->m_final && !ir_block_create_jump(bfall, m_context, bout)) {
2827 astwarning(bfall->m_context, WARN_???, "missing break after last case");
2832 /* If there was a default case, put it down here */
2836 /* No need to create an extra block */
2837 bcase = func->m_curblock;
2839 /* Insert the fallthrough jump */
2840 if (def_bfall && !def_bfall->m_final) {
2841 if (!ir_block_create_jump(def_bfall, m_context, bcase))
2845 /* Now generate the default code */
2846 if (!def_case->m_code->codegen(func, false, &dummy))
2849 /* see if we need to fall through */
2850 if (def_bfall_to && !func->m_curblock->m_final)
2852 if (!ir_block_create_jump(func->m_curblock, m_context, def_bfall_to))
2857 /* Jump from the last bnot to bout */
2858 if (!func->m_curblock->m_final && !ir_block_create_jump(func->m_curblock, m_context, bout))
2860 /* enter the outgoing block */
2861 func->m_curblock = bout;
2863 /* restore the break block */
2864 func->m_breakblocks.pop_back();
2866 /* Move 'bout' to the end, it's nicer */
2867 algo::shiftback(func->m_ir_func->m_blocks.begin() + bout_id,
2868 func->m_ir_func->m_blocks.end());
2870 //func->m_ir_func->m_blocks[bout_id].release();
2871 //func->m_ir_func->m_blocks.erase(func->m_ir_func->m_blocks.begin() + bout_id);
2872 //func->m_ir_func->m_blocks.emplace_back(bout);
2877 bool ast_label::codegen(ast_function *func, bool lvalue, ir_value **out)
2882 compile_error(m_context, "internal error: ast_label never defined");
2888 compile_error(m_context, "internal error: ast_label cannot be an lvalue");
2892 /* simply create a new block and jump to it */
2893 m_irblock = ir_function_create_block(m_context, func->m_ir_func, m_name.c_str());
2895 compile_error(m_context, "failed to allocate label block `%s`", m_name);
2898 if (!func->m_curblock->m_final) {
2899 if (!ir_block_create_jump(func->m_curblock, m_context, m_irblock))
2903 /* enter the new block */
2904 func->m_curblock = m_irblock;
2906 /* Generate all the leftover gotos */
2907 for (auto &it : m_gotos) {
2908 if (!it->codegen(func, false, &dummy))
2915 bool ast_goto::codegen(ast_function *func, bool lvalue, ir_value **out)
2919 compile_error(m_context, "internal error: ast_goto cannot be an lvalue");
2923 if (m_target->m_irblock) {
2924 if (m_irblock_from) {
2925 /* we already tried once, this is the callback */
2926 m_irblock_from->m_final = false;
2927 if (!ir_block_create_goto(m_irblock_from, m_context, m_target->m_irblock)) {
2928 compile_error(m_context, "failed to generate goto to `%s`", m_name);
2934 if (!ir_block_create_goto(func->m_curblock, m_context, m_target->m_irblock)) {
2935 compile_error(m_context, "failed to generate goto to `%s`", m_name);
2942 /* the target has not yet been created...
2943 * close this block in a sneaky way:
2945 func->m_curblock->m_final = true;
2946 m_irblock_from = func->m_curblock;
2947 m_target->registerGoto(this);
2953 bool ast_state::codegen(ast_function *func, bool lvalue, ir_value **out)
2955 ir_value *frameval, *thinkval;
2958 compile_error(m_context, "not an l-value (state operation)");
2962 compile_error(m_context, "internal error: ast_state cannot be reused!");
2967 if (!m_framenum->codegen(func, false, &frameval))
2972 if (!m_nextthink->codegen(func, false, &thinkval))
2977 if (!ir_block_create_state_op(func->m_curblock, m_context, frameval, thinkval)) {
2978 compile_error(m_context, "failed to create STATE instruction");
2982 m_outr = (ir_value*)1;
2986 bool ast_call::codegen(ast_function *func, bool lvalue, ir_value **out)
2988 std::vector<ir_value*> params;
2989 ir_instr *callinstr;
2991 ir_value *funval = nullptr;
2993 /* return values are never lvalues */
2995 compile_error(m_context, "not an l-value (function call)");
3004 if (!m_func->codegen(func, false, &funval))
3010 for (auto &it : m_params) {
3012 if (!it->codegen(func, false, ¶m))
3016 params.push_back(param);
3019 /* varargs counter */
3022 ir_builder *builder = func->m_curblock->m_owner->m_owner;
3023 if (!m_va_count->codegen(func, false, &va_count))
3025 if (!ir_block_create_store_op(func->m_curblock, m_context, INSTR_STORE_F,
3026 ir_builder_get_va_count(builder), va_count))
3032 callinstr = ir_block_create_call(func->m_curblock, m_context,
3033 func->makeLabel("call"),
3034 funval, !!(m_func->m_flags & AST_FLAG_NORETURN));
3038 for (auto &it : params)
3039 ir_call_param(callinstr, it);
3041 *out = ir_call_value(callinstr);
3044 codegen_output_type(this, *out);