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;
40 if (OPTS_FLAG(DEFAULT_ERASEABLE))
41 m_flags |= AST_FLAG_ERASEABLE;
43 ast_expression::ast_expression(lex_ctx_t ctx, int nodetype)
44 : ast_expression(ctx, nodetype, TYPE_VOID)
47 ast_expression::~ast_expression()
55 ast_expression::ast_expression(ast_copy_type_t, const ast_expression &other)
56 : ast_expression(ast_copy_type, other.m_context, other)
59 ast_expression::ast_expression(ast_copy_type_t, lex_ctx_t ctx, const ast_expression &other)
60 : ast_expression(ast_copy_type, TYPE_ast_expression, ctx, other)
63 ast_expression::ast_expression(ast_copy_type_t, int nodetype, const ast_expression &other)
64 : ast_expression(ast_copy_type, nodetype, other.m_context, other)
67 ast_expression::ast_expression(ast_copy_type_t, int nodetype, lex_ctx_t ctx, const ast_expression &other)
68 : ast_expression(ctx, nodetype)
70 m_vtype = other.m_vtype;
71 m_count = other.m_count;
72 m_flags = other.m_flags;
74 m_next = new ast_expression(ast_copy_type, *other.m_next);
75 m_type_params.reserve(other.m_type_params.size());
76 for (auto &it : other.m_type_params)
77 m_type_params.emplace_back(new ast_value(ast_copy_type, *it));
81 ast_expression *ast_expression::shallowType(lex_ctx_t ctx, qc_type vtype) {
82 auto expr = new ast_expression(ctx, TYPE_ast_expression);
83 expr->m_vtype = vtype;
87 void ast_expression::adoptType(const ast_expression &other)
89 m_vtype = other.m_vtype;
91 m_next = new ast_expression(ast_copy_type, *other.m_next);
92 m_count = other.m_count;
93 m_flags = other.m_flags;
94 m_type_params.clear();
95 m_type_params.reserve(other.m_type_params.size());
96 for (auto &it : other.m_type_params)
97 m_type_params.emplace_back(new ast_value(ast_copy_type, *it));
100 bool ast_expression::compareType(const ast_expression &other) const
102 if (m_vtype == TYPE_NIL ||
103 other.m_vtype == TYPE_NIL)
105 if (m_vtype != other.m_vtype)
107 if (!m_next != !other.m_next)
109 if (m_type_params.size() != other.m_type_params.size())
111 if ((m_flags & AST_FLAG_TYPE_MASK) !=
112 (other.m_flags & AST_FLAG_TYPE_MASK) )
116 if (m_type_params.size()) {
118 for (i = 0; i < m_type_params.size(); ++i) {
119 if (!m_type_params[i]->compareType(*other.m_type_params[i]))
124 return m_next->compareType(*other.m_next);
128 bool ast_expression::codegen(ast_function*, bool, ir_value**) {
129 compile_error(m_context, "ast_expression::codegen called!");
134 ast_value::ast_value(ast_copy_type_t, const ast_value &other, const std::string &name)
135 : ast_value(ast_copy_type, static_cast<const ast_expression&>(other), name)
137 m_keep_node = true; // keep values, always
138 memset(&m_constval, 0, sizeof(m_constval));
141 ast_value::ast_value(ast_copy_type_t, const ast_value &other)
142 : ast_value(ast_copy_type, static_cast<const ast_expression&>(other), other.m_name)
144 m_keep_node = true; // keep values, always
145 memset(&m_constval, 0, sizeof(m_constval));
148 ast_value::ast_value(ast_copy_type_t, const ast_expression &other, const std::string &name)
149 : ast_expression(ast_copy_type, TYPE_ast_value, other)
152 m_keep_node = true; // keep values, always
153 memset(&m_constval, 0, sizeof(m_constval));
156 ast_value::ast_value(lex_ctx_t ctx, const std::string &name, qc_type t)
157 : ast_expression(ctx, TYPE_ast_value, t)
160 m_keep_node = true; // keep values, always
161 memset(&m_constval, 0, sizeof(m_constval));
164 ast_value::~ast_value()
167 mem_d((void*)m_argcounter);
172 mem_d((void*)m_constval.vstring);
175 // unlink us from the function node
176 m_constval.vfunc->m_function_type = nullptr;
178 // NOTE: delete function? currently collected in
179 // the parser structure
185 // initlist imples an array which implies .next in the expression exists.
186 if (m_initlist.size() && m_next->m_vtype == TYPE_STRING) {
187 for (auto &it : m_initlist)
193 static size_t ast_type_to_string_impl(const ast_expression *e, char *buf, size_t bufsize, size_t pos)
200 if (pos + 6 >= bufsize)
202 util_strncpy(buf + pos, "(null)", 6);
206 if (pos + 1 >= bufsize)
209 switch (e->m_vtype) {
211 util_strncpy(buf + pos, "(variant)", 9);
216 return ast_type_to_string_impl(e->m_next, buf, bufsize, pos);
219 if (pos + 3 >= bufsize)
223 pos = ast_type_to_string_impl(e->m_next, buf, bufsize, pos);
224 if (pos + 1 >= bufsize)
230 pos = ast_type_to_string_impl(e->m_next, buf, bufsize, pos);
231 if (pos + 2 >= bufsize)
233 if (e->m_type_params.empty()) {
239 pos = ast_type_to_string_impl(e->m_type_params[0].get(), buf, bufsize, pos);
240 for (i = 1; i < e->m_type_params.size(); ++i) {
241 if (pos + 2 >= bufsize)
245 pos = ast_type_to_string_impl(e->m_type_params[i].get(), buf, bufsize, pos);
247 if (pos + 1 >= bufsize)
253 pos = ast_type_to_string_impl(e->m_next, buf, bufsize, pos);
254 if (pos + 1 >= bufsize)
257 pos += util_snprintf(buf + pos, bufsize - pos - 1, "%i", (int)e->m_count);
258 if (pos + 1 >= bufsize)
264 typestr = type_name[e->m_vtype];
265 typelen = strlen(typestr);
266 if (pos + typelen >= bufsize)
268 util_strncpy(buf + pos, typestr, typelen);
269 return pos + typelen;
273 buf[bufsize-3] = '.';
274 buf[bufsize-2] = '.';
275 buf[bufsize-1] = '.';
279 void ast_type_to_string(const ast_expression *e, char *buf, size_t bufsize)
281 size_t pos = ast_type_to_string_impl(e, buf, bufsize-1, 0);
285 void ast_value::addParam(ast_value *p)
287 m_type_params.emplace_back(p);
290 ast_binary::ast_binary(lex_ctx_t ctx, int op,
291 ast_expression* left, ast_expression* right)
292 : ast_expression(ctx, TYPE_ast_binary)
294 // m_left/m_right happen after the peephole step right below
295 , m_right_first(false)
297 if (ast_istype(right, ast_unary) && OPTS_OPTIMIZATION(OPTIM_PEEPHOLE)) {
298 ast_unary *unary = ((ast_unary*)right);
299 ast_expression *normal = unary->m_operand;
301 /* make a-(-b) => a + b */
302 if (unary->m_op == VINSTR_NEG_F || unary->m_op == VINSTR_NEG_V) {
303 if (op == INSTR_SUB_F) {
304 op = m_op = INSTR_ADD_F;
306 ++opts_optimizationcount[OPTIM_PEEPHOLE];
307 } else if (op == INSTR_SUB_V) {
308 op = m_op = INSTR_ADD_V;
310 ++opts_optimizationcount[OPTIM_PEEPHOLE];
318 propagateSideEffects(left);
319 propagateSideEffects(right);
321 if (op >= INSTR_EQ_F && op <= INSTR_GT)
322 m_vtype = TYPE_FLOAT;
323 else if (op == INSTR_AND || op == INSTR_OR) {
324 if (OPTS_FLAG(PERL_LOGIC))
327 m_vtype = TYPE_FLOAT;
329 else if (op == INSTR_BITAND || op == INSTR_BITOR)
330 m_vtype = TYPE_FLOAT;
331 else if (op == INSTR_MUL_VF || op == INSTR_MUL_FV)
332 m_vtype = TYPE_VECTOR;
333 else if (op == INSTR_MUL_V)
334 m_vtype = TYPE_FLOAT;
336 m_vtype = left->m_vtype;
339 m_refs = AST_REF_ALL;
342 ast_binary::~ast_binary()
344 if (m_refs & AST_REF_LEFT) ast_unref(m_left);
345 if (m_refs & AST_REF_RIGHT) ast_unref(m_right);
348 ast_binstore::ast_binstore(lex_ctx_t ctx, int storop, int mathop,
349 ast_expression* left, ast_expression* right)
350 : ast_expression(ctx, TYPE_ast_binstore)
357 m_side_effects = true;
361 ast_binstore::~ast_binstore()
368 ast_unary* ast_unary::make(lex_ctx_t ctx, int op, ast_expression *expr)
370 // handle double negation, double bitwise or logical not
371 if (op == opid2('!','P') ||
372 op == opid2('~','P') ||
373 op == opid2('-','P'))
375 if (ast_istype(expr, ast_unary) && OPTS_OPTIMIZATION(OPTIM_PEEPHOLE)) {
376 ast_unary *unary = reinterpret_cast<ast_unary*>(expr);
377 if (unary->m_op == op) {
378 auto out = reinterpret_cast<ast_unary*>(unary->m_operand);
379 unary->m_operand = nullptr;
381 ++opts_optimizationcount[OPTIM_PEEPHOLE];
387 return new ast_unary(ctx, op, expr);
390 ast_unary::ast_unary(lex_ctx_t ctx, int op, ast_expression *expr)
391 : ast_expression(ctx, TYPE_ast_unary)
395 propagateSideEffects(expr);
396 if ((op >= INSTR_NOT_F && op <= INSTR_NOT_FNC) || op == VINSTR_NEG_F) {
397 m_vtype = TYPE_FLOAT;
398 } else if (op == VINSTR_NEG_V) {
399 m_vtype = TYPE_VECTOR;
401 compile_error(ctx, "cannot determine type of unary operation %s", util_instr_str[op]);
405 ast_unary::~ast_unary()
408 ast_unref(m_operand);
411 ast_return::ast_return(lex_ctx_t ctx, ast_expression *expr)
412 : ast_expression(ctx, TYPE_ast_return)
416 propagateSideEffects(expr);
419 ast_return::~ast_return()
422 ast_unref(m_operand);
425 ast_entfield::ast_entfield(lex_ctx_t ctx, ast_expression *entity, ast_expression *field)
426 : ast_entfield(ctx, entity, field, field->m_next)
428 if (field->m_vtype != TYPE_FIELD)
429 compile_error(ctx, "ast_entfield with expression not of type field");
432 ast_entfield::ast_entfield(lex_ctx_t ctx, ast_expression *entity, ast_expression *field, const ast_expression *outtype)
433 : ast_expression(ctx, TYPE_ast_entfield)
437 propagateSideEffects(m_entity);
438 propagateSideEffects(m_field);
441 compile_error(ctx, "ast_entfield: field has no type");
448 ast_entfield::~ast_entfield()
454 ast_member *ast_member::make(lex_ctx_t ctx, ast_expression *owner, unsigned int field, const std::string &name)
457 compile_error(ctx, "ast_member: invalid field (>=3): %u", field);
460 if (owner->m_vtype != TYPE_VECTOR &&
461 owner->m_vtype != TYPE_FIELD)
463 compile_error(ctx, "member-access on an invalid owner of type %s", type_name[owner->m_vtype]);
466 return new ast_member(ctx, owner, field, name);
469 ast_member::ast_member(lex_ctx_t ctx, ast_expression *owner, unsigned int field, const std::string &name)
470 : ast_expression(ctx, TYPE_ast_member)
478 if (m_owner->m_vtype == TYPE_VECTOR) {
479 m_vtype = TYPE_FLOAT;
482 m_vtype = TYPE_FIELD;
483 m_next = ast_expression::shallowType(ctx, TYPE_FLOAT);
486 propagateSideEffects(owner);
489 ast_member::~ast_member()
491 // The owner is always an ast_value, which has .keep_node=true,
492 // also: ast_members are usually deleted after the owner, thus
493 // this will cause invalid access
494 //ast_unref(self->m_owner);
495 // once we allow (expression).x to access a vector-member, we need
496 // to change this: preferably by creating an alternate ast node for this
497 // purpose that is not garbage-collected.
500 ast_array_index* ast_array_index::make(lex_ctx_t ctx, ast_expression *array, ast_expression *index)
502 ast_expression *outtype = array->m_next;
508 return new ast_array_index(ctx, array, index);
511 ast_array_index::ast_array_index(lex_ctx_t ctx, ast_expression *array, ast_expression *index)
512 : ast_expression(ctx, TYPE_ast_array_index)
516 propagateSideEffects(array);
517 propagateSideEffects(index);
519 ast_expression *outtype = m_array->m_next;
522 if (array->m_vtype == TYPE_FIELD && outtype->m_vtype == TYPE_ARRAY) {
523 // FIXME: investigate - this is not possible after adoptType
524 //if (m_vtype != TYPE_ARRAY) {
525 // compile_error(self->m_context, "array_index node on type");
526 // ast_array_index_delete(self);
531 m_vtype = TYPE_FIELD;
535 ast_array_index::~ast_array_index()
543 ast_argpipe::ast_argpipe(lex_ctx_t ctx, ast_expression *index)
544 : ast_expression(ctx, TYPE_ast_argpipe)
547 m_vtype = TYPE_NOEXPR;
550 ast_argpipe::~ast_argpipe()
556 ast_store::ast_store(lex_ctx_t ctx, int op, ast_expression *dest, ast_expression *source)
557 : ast_expression(ctx, TYPE_ast_store)
562 m_side_effects = true;
566 ast_store::~ast_store()
572 ast_ifthen::ast_ifthen(lex_ctx_t ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
573 : ast_expression(ctx, TYPE_ast_ifthen)
576 , m_on_false(onfalse)
578 propagateSideEffects(cond);
580 propagateSideEffects(ontrue);
582 propagateSideEffects(onfalse);
585 ast_ifthen::~ast_ifthen()
589 ast_unref(m_on_true);
591 ast_unref(m_on_false);
594 ast_ternary::ast_ternary(lex_ctx_t ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse)
595 : ast_expression(ctx, TYPE_ast_ternary)
598 , m_on_false(onfalse)
600 propagateSideEffects(cond);
601 propagateSideEffects(ontrue);
602 propagateSideEffects(onfalse);
604 if (ontrue->m_vtype == TYPE_NIL)
610 ast_ternary::~ast_ternary()
612 /* the if()s are only there because computed-gotos can set them
615 if (m_cond) ast_unref(m_cond);
616 if (m_on_true) ast_unref(m_on_true);
617 if (m_on_false) ast_unref(m_on_false);
620 ast_loop::ast_loop(lex_ctx_t ctx,
621 ast_expression *initexpr,
622 ast_expression *precond, bool pre_not,
623 ast_expression *postcond, bool post_not,
624 ast_expression *increment,
625 ast_expression *body)
626 : ast_expression(ctx, TYPE_ast_loop)
627 , m_initexpr(initexpr)
629 , m_postcond(postcond)
630 , m_increment(increment)
633 , m_post_not(post_not)
636 propagateSideEffects(initexpr);
638 propagateSideEffects(precond);
640 propagateSideEffects(postcond);
642 propagateSideEffects(increment);
644 propagateSideEffects(body);
647 ast_loop::~ast_loop()
650 ast_unref(m_initexpr);
652 ast_unref(m_precond);
654 ast_unref(m_postcond);
656 ast_unref(m_increment);
661 ast_breakcont::ast_breakcont(lex_ctx_t ctx, bool iscont, unsigned int levels)
662 : ast_expression(ctx, TYPE_ast_breakcont)
663 , m_is_continue(iscont)
668 ast_breakcont::~ast_breakcont()
672 ast_switch::ast_switch(lex_ctx_t ctx, ast_expression *op)
673 : ast_expression(ctx, TYPE_ast_switch)
676 propagateSideEffects(op);
679 ast_switch::~ast_switch()
681 ast_unref(m_operand);
683 for (auto &it : m_cases) {
685 ast_unref(it.m_value);
686 ast_unref(it.m_code);
690 ast_label::ast_label(lex_ctx_t ctx, const std::string &name, bool undefined)
691 : ast_expression(ctx, TYPE_ast_label)
694 , m_undefined(undefined)
696 m_vtype = TYPE_NOEXPR;
699 ast_label::~ast_label()
703 void ast_label::registerGoto(ast_goto *g)
705 m_gotos.push_back(g);
708 ast_goto::ast_goto(lex_ctx_t ctx, const std::string &name)
709 : ast_expression(ctx, TYPE_ast_goto)
712 , m_irblock_from(nullptr)
716 ast_goto::~ast_goto()
720 void ast_goto::setLabel(ast_label *label)
725 ast_state::ast_state(lex_ctx_t ctx, ast_expression *frame, ast_expression *think)
726 : ast_expression(ctx, TYPE_ast_expression)
732 ast_state::~ast_state()
735 ast_unref(m_framenum);
737 ast_unref(m_nextthink);
740 ast_call *ast_call::make(lex_ctx_t ctx, ast_expression *funcexpr)
742 if (!funcexpr->m_next) {
743 compile_error(ctx, "not a function");
746 return new ast_call(ctx, funcexpr);
749 ast_call::ast_call(lex_ctx_t ctx, ast_expression *funcexpr)
750 : ast_expression(ctx, TYPE_ast_call)
752 , m_va_count(nullptr)
754 m_side_effects = true;
755 adoptType(*funcexpr->m_next);
758 ast_call::~ast_call()
760 for (auto &it : m_params)
767 ast_unref(m_va_count);
770 bool ast_call::checkVararg(ast_expression *va_type, ast_expression *exp_type) const
776 if (!va_type || !va_type->compareType(*exp_type))
778 if (va_type && exp_type)
780 ast_type_to_string(va_type, tgot, sizeof(tgot));
781 ast_type_to_string(exp_type, texp, sizeof(texp));
782 if (OPTS_FLAG(UNSAFE_VARARGS)) {
783 if (compile_warning(m_context, WARN_UNSAFE_TYPES,
784 "piped variadic argument differs in type: constrained to type %s, expected type %s",
788 compile_error(m_context,
789 "piped variadic argument differs in type: constrained to type %s, expected type %s",
796 ast_type_to_string(exp_type, texp, sizeof(texp));
797 if (OPTS_FLAG(UNSAFE_VARARGS)) {
798 if (compile_warning(m_context, WARN_UNSAFE_TYPES,
799 "piped variadic argument may differ in type: expected type %s",
803 compile_error(m_context,
804 "piped variadic argument may differ in type: expected type %s",
813 bool ast_call::checkTypes(ast_expression *va_type) const
820 size_t count = m_params.size();
821 if (count > m_func->m_type_params.size())
822 count = m_func->m_type_params.size();
824 for (i = 0; i < count; ++i) {
825 if (ast_istype(m_params[i], ast_argpipe)) {
826 /* warn about type safety instead */
828 compile_error(m_context, "argpipe must be the last parameter to a function call");
831 if (!checkVararg(va_type, m_func->m_type_params[i].get()))
834 else if (!m_params[i]->compareType(*m_func->m_type_params[i]))
836 ast_type_to_string(m_params[i], tgot, sizeof(tgot));
837 ast_type_to_string(m_func->m_type_params[i].get(), texp, sizeof(texp));
838 compile_error(m_context, "invalid type for parameter %u in function call: expected %s, got %s",
839 (unsigned int)(i+1), texp, tgot);
840 /* we don't immediately return */
844 count = m_params.size();
845 if (count > m_func->m_type_params.size() && m_func->m_varparam) {
846 for (; i < count; ++i) {
847 if (ast_istype(m_params[i], ast_argpipe)) {
848 /* warn about type safety instead */
850 compile_error(m_context, "argpipe must be the last parameter to a function call");
853 if (!checkVararg(va_type, m_func->m_varparam))
856 else if (!m_params[i]->compareType(*m_func->m_varparam))
858 ast_type_to_string(m_params[i], tgot, sizeof(tgot));
859 ast_type_to_string(m_func->m_varparam, texp, sizeof(texp));
860 compile_error(m_context, "invalid type for variadic parameter %u in function call: expected %s, got %s",
861 (unsigned int)(i+1), texp, tgot);
862 /* we don't immediately return */
870 ast_block::ast_block(lex_ctx_t ctx)
871 : ast_expression(ctx, TYPE_ast_block)
875 ast_block::~ast_block()
877 for (auto &it : m_exprs) ast_unref(it);
878 for (auto &it : m_locals) delete it;
879 for (auto &it : m_collect) delete it;
882 void ast_block::setType(const ast_expression &from)
890 bool ast_block::addExpr(ast_expression *e)
892 propagateSideEffects(e);
893 m_exprs.push_back(e);
902 void ast_block::collect(ast_expression *expr)
904 m_collect.push_back(expr);
905 expr->m_keep_node = true;
908 ast_function *ast_function::make(lex_ctx_t ctx, const std::string &name, ast_value *vtype)
911 compile_error(ctx, "internal error: ast_function_new condition 0");
913 } else if (vtype->m_hasvalue || vtype->m_vtype != TYPE_FUNCTION) {
914 compile_error(ctx, "internal error: ast_function_new condition %i %i type=%i (probably 2 bodies?)",
916 (int)vtype->m_hasvalue,
920 return new ast_function(ctx, name, vtype);
923 ast_function::ast_function(lex_ctx_t ctx, const std::string &name, ast_value *vtype)
924 : ast_node(ctx, TYPE_ast_function)
925 , m_function_type(vtype)
930 , m_curblock(nullptr)
934 , m_fixedparams(nullptr)
935 , m_return_value(nullptr)
937 vtype->m_hasvalue = true;
938 vtype->m_constval.vfunc = this;
941 ast_function::~ast_function()
943 if (m_function_type) {
944 // ast_value_delete(m_function_type);
945 m_function_type->m_hasvalue = false;
946 m_function_type->m_constval.vfunc = nullptr;
947 // We use unref - if it was stored in a global table it is supposed
948 // to be deleted from *there*
949 ast_unref(m_function_type);
953 ast_unref(m_fixedparams);
955 ast_unref(m_return_value);
957 // force this to be cleared before m_varargs/m_argc as blocks might
958 // try to access them via ast_unref()
962 const char* ast_function::makeLabel(const char *prefix)
968 if (!OPTS_OPTION_BOOL(OPTION_DUMP) &&
969 !OPTS_OPTION_BOOL(OPTION_DUMPFIN) &&
970 !OPTS_OPTION_BOOL(OPTION_DEBUG))
975 id = (m_labelcount++);
976 len = strlen(prefix);
978 from = m_labelbuf + sizeof(m_labelbuf)-1;
981 *from-- = (id%10) + '0';
985 memcpy(from - len, prefix, len);
989 /*********************************************************************/
991 * by convention you must never pass nullptr to the 'ir_value **out'
992 * parameter. If you really don't care about the output, pass a dummy.
993 * But I can't imagine a pituation where the output is truly unnecessary.
996 static void codegen_output_type(ast_expression *self, ir_value *out)
998 if (out->m_vtype == TYPE_FIELD)
999 out->m_fieldtype = self->m_next->m_vtype;
1000 if (out->m_vtype == TYPE_FUNCTION)
1001 out->m_outtype = self->m_next->m_vtype;
1004 bool ast_value::codegen(ast_function *func, bool lvalue, ir_value **out)
1008 if (m_vtype == TYPE_NIL) {
1009 *out = func->m_ir_func->m_owner->m_nil;
1012 // NOTE: This is the codegen for a variable used in an expression.
1013 // It is not the codegen to generate the value storage. For this purpose,
1014 // generateLocal and generateGlobal are to be used before this
1015 // is executed. ast_function::generateFunction should take care of its
1016 // locals, and the ast-user should take care of generateGlobal to be used
1017 // on all the globals.
1019 char tname[1024]; /* typename is reserved in C++ */
1020 ast_type_to_string(this, tname, sizeof(tname));
1021 compile_error(m_context, "ast_value used before generated %s %s", tname, m_name);
1028 bool ast_value::setGlobalArray()
1030 size_t count = m_initlist.size();
1033 if (count > m_count) {
1034 compile_error(m_context, "too many elements in initializer");
1037 else if (count < m_count) {
1039 compile_warning(m_context, "not all elements are initialized");
1043 for (i = 0; i != count; ++i) {
1044 switch (m_next->m_vtype) {
1046 if (!m_ir_values[i]->setFloat(m_initlist[i].vfloat))
1050 if (!m_ir_values[i]->setVector(m_initlist[i].vvec))
1054 if (!m_ir_values[i]->setString(m_initlist[i].vstring))
1058 /* we don't support them in any other place yet either */
1059 compile_error(m_context, "TODO: nested arrays");
1062 /* this requiers a bit more work - similar to the fields I suppose */
1063 compile_error(m_context, "global of type function not properly generated");
1066 if (!m_initlist[i].vfield) {
1067 compile_error(m_context, "field constant without vfield set");
1070 if (!m_initlist[i].vfield->m_ir_v) {
1071 compile_error(m_context, "field constant generated before its field");
1074 if (!m_ir_values[i]->setField(m_initlist[i].vfield->m_ir_v))
1078 compile_error(m_context, "TODO: global constant type %i", m_vtype);
1085 bool ast_value::checkArray(const ast_value &array) const
1087 if (array.m_flags & AST_FLAG_ARRAY_INIT && array.m_initlist.empty()) {
1088 compile_error(m_context, "array without size: %s", m_name);
1091 // we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements
1092 if (!array.m_count || array.m_count > OPTS_OPTION_U32(OPTION_MAX_ARRAY_SIZE)) {
1093 compile_error(m_context, "Invalid array of size %lu", (unsigned long)array.m_count);
1099 bool ast_value::generateGlobal(ir_builder *ir, bool isfield)
1101 if (m_vtype == TYPE_NIL) {
1102 compile_error(m_context, "internal error: trying to generate a variable of TYPE_NIL");
1106 if (m_hasvalue && m_vtype == TYPE_FUNCTION)
1107 return generateGlobalFunction(ir);
1109 if (isfield && m_vtype == TYPE_FIELD)
1110 return generateGlobalField(ir);
1112 ir_value *v = nullptr;
1113 if (m_vtype == TYPE_ARRAY) {
1114 v = prepareGlobalArray(ir);
1118 // Arrays don't do this since there's no "array" value which spans across the
1120 v = ir->createGlobal(m_name, m_vtype);
1122 compile_error(m_context, "ir_builder::createGlobal failed on `%s`", m_name);
1125 codegen_output_type(this, v);
1126 v->m_context = m_context;
1129 /* link us to the ir_value */
1133 if (m_flags & AST_FLAG_INCLUDE_DEF)
1134 m_ir_v->m_flags |= IR_FLAG_INCLUDE_DEF;
1135 if (m_flags & AST_FLAG_ERASEABLE && !(m_flags & AST_FLAG_NOERASE))
1136 m_ir_v->m_flags |= IR_FLAG_ERASABLE;
1137 if (m_flags & AST_FLAG_NOREF)
1138 m_ir_v->m_flags |= IR_FLAG_NOREF;
1145 if (!v->setFloat(m_constval.vfloat))
1149 if (!v->setVector(m_constval.vvec))
1153 if (!v->setString(m_constval.vstring))
1157 if (!setGlobalArray())
1161 compile_error(m_context, "global of type function not properly generated");
1163 /* Cannot generate an IR value for a function,
1164 * need a pointer pointing to a function rather.
1167 if (!m_constval.vfield) {
1168 compile_error(m_context, "field constant without vfield set");
1171 if (!m_constval.vfield->m_ir_v) {
1172 compile_error(m_context, "field constant generated before its field");
1175 if (!v->setField(m_constval.vfield->m_ir_v))
1179 compile_error(m_context, "TODO: global constant type %i", m_vtype);
1187 bool ast_value::generateGlobalFunction(ir_builder *ir)
1189 ir_function *func = ir->createFunction(m_name, m_next->m_vtype);
1192 func->m_context = m_context;
1193 func->m_value->m_context = m_context;
1195 m_constval.vfunc->m_ir_func = func;
1196 m_ir_v = func->m_value;
1197 if (m_flags & AST_FLAG_INCLUDE_DEF)
1198 m_ir_v->m_flags |= IR_FLAG_INCLUDE_DEF;
1199 if (m_flags & AST_FLAG_ERASEABLE && !(m_flags & AST_FLAG_NOERASE))
1200 m_ir_v->m_flags |= IR_FLAG_ERASABLE;
1201 if (m_flags & AST_FLAG_BLOCK_COVERAGE)
1202 func->m_flags |= IR_FLAG_BLOCK_COVERAGE;
1203 // The function is filled later on ast_function::generateFunction...
1207 bool ast_value::generateGlobalField(ir_builder *ir)
1209 ast_expression *fieldtype = m_next;
1212 compile_error(m_context, "TODO: constant field pointers with value");
1216 if (fieldtype->m_vtype == TYPE_ARRAY) {
1217 if (!ast_istype(fieldtype, ast_value)) {
1218 compile_error(m_context, "internal error: ast_value required");
1221 ast_value *array = reinterpret_cast<ast_value*>(fieldtype);
1223 if (!checkArray(*array))
1226 ast_expression *elemtype = array->m_next;
1227 qc_type vtype = elemtype->m_vtype;
1229 ir_value *v = ir->createField(m_name, vtype);
1231 compile_error(m_context, "ir_builder::createGlobal failed on `%s`", m_name);
1234 v->m_context = m_context;
1235 v->m_unique_life = true;
1237 array->m_ir_v = m_ir_v = v;
1239 if (m_flags & AST_FLAG_INCLUDE_DEF)
1240 m_ir_v->m_flags |= IR_FLAG_INCLUDE_DEF;
1241 if (m_flags & AST_FLAG_ERASEABLE && !(m_flags & AST_FLAG_NOERASE))
1242 m_ir_v->m_flags |= IR_FLAG_ERASABLE;
1243 if (m_flags & AST_FLAG_NOREF)
1244 m_ir_v->m_flags |= IR_FLAG_NOREF;
1246 const size_t namelen = m_name.length();
1247 std::unique_ptr<char[]> name(new char[namelen+16]);
1248 util_strncpy(name.get(), m_name.c_str(), namelen);
1250 array->m_ir_values.resize(array->m_count);
1251 array->m_ir_values[0] = v;
1252 for (size_t ai = 1; ai < array->m_count; ++ai) {
1253 util_snprintf(name.get() + namelen, 16, "[%u]", (unsigned int)ai);
1254 array->m_ir_values[ai] = ir->createField(name.get(), vtype);
1255 if (!array->m_ir_values[ai]) {
1256 compile_error(m_context, "ir_builder::createGlobal failed on `%s`", name.get());
1259 array->m_ir_values[ai]->m_context = m_context;
1260 array->m_ir_values[ai]->m_unique_life = true;
1261 array->m_ir_values[ai]->m_locked = true;
1262 if (m_flags & AST_FLAG_INCLUDE_DEF)
1263 array->m_ir_values[ai]->m_flags |= IR_FLAG_INCLUDE_DEF;
1264 if (m_flags & AST_FLAG_NOREF)
1265 array->m_ir_values[ai]->m_flags |= IR_FLAG_NOREF;
1270 ir_value *v = ir->createField(m_name, m_next->m_vtype);
1273 v->m_context = m_context;
1275 if (m_flags & AST_FLAG_INCLUDE_DEF)
1276 m_ir_v->m_flags |= IR_FLAG_INCLUDE_DEF;
1277 if (m_flags & AST_FLAG_ERASEABLE && !(m_flags & AST_FLAG_NOERASE))
1278 m_ir_v->m_flags |= IR_FLAG_ERASABLE;
1279 if (m_flags & AST_FLAG_NOREF)
1280 m_ir_v->m_flags |= IR_FLAG_NOREF;
1285 ir_value *ast_value::prepareGlobalArray(ir_builder *ir)
1287 ast_expression *elemtype = m_next;
1288 qc_type vtype = elemtype->m_vtype;
1290 if (m_flags & AST_FLAG_ARRAY_INIT && !m_count) {
1291 compile_error(m_context, "array `%s' has no size", m_name);
1295 /* same as with field arrays */
1296 if (!checkArray(*this))
1299 ir_value *v = ir->createGlobal(m_name, vtype);
1301 compile_error(m_context, "ir_builder::createGlobal failed `%s`", m_name);
1304 v->m_context = m_context;
1305 v->m_unique_life = true;
1308 if (m_flags & AST_FLAG_INCLUDE_DEF)
1309 v->m_flags |= IR_FLAG_INCLUDE_DEF;
1310 if (m_flags & AST_FLAG_ERASEABLE && !(m_flags & AST_FLAG_NOERASE))
1311 v->m_flags |= IR_FLAG_ERASABLE;
1312 if (m_flags & AST_FLAG_NOREF)
1313 v->m_flags |= IR_FLAG_NOREF;
1315 const size_t namelen = m_name.length();
1316 std::unique_ptr<char[]> name(new char[namelen+16]);
1317 util_strncpy(name.get(), m_name.c_str(), namelen);
1319 m_ir_values.resize(m_count);
1321 for (size_t ai = 1; ai < m_count; ++ai) {
1322 util_snprintf(name.get() + namelen, 16, "[%u]", (unsigned int)ai);
1323 m_ir_values[ai] = ir->createGlobal(name.get(), vtype);
1324 if (!m_ir_values[ai]) {
1325 compile_error(m_context, "ir_builder::createGlobal failed `%s`", name.get());
1328 m_ir_values[ai]->m_context = m_context;
1329 m_ir_values[ai]->m_unique_life = true;
1330 m_ir_values[ai]->m_locked = true;
1331 if (m_flags & AST_FLAG_INCLUDE_DEF)
1332 m_ir_values[ai]->m_flags |= IR_FLAG_INCLUDE_DEF;
1333 if (m_flags & AST_FLAG_NOREF)
1334 m_ir_values[ai]->m_flags |= IR_FLAG_NOREF;
1340 bool ast_value::generateLocal(ir_function *func, bool param)
1342 if (m_vtype == TYPE_NIL) {
1343 compile_error(m_context, "internal error: trying to generate a variable of TYPE_NIL");
1347 if (m_hasvalue && m_vtype == TYPE_FUNCTION)
1349 /* Do we allow local functions? I think not...
1350 * this is NOT a function pointer atm.
1355 ir_value *v = nullptr;
1356 if (m_vtype == TYPE_ARRAY) {
1357 ast_expression *elemtype = m_next;
1358 qc_type vtype = elemtype->m_vtype;
1360 func->m_flags |= IR_FLAG_HAS_ARRAYS;
1362 if (param && !(m_flags & AST_FLAG_IS_VARARG)) {
1363 compile_error(m_context, "array-parameters are not supported");
1367 /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1368 if (!checkArray(*this))
1371 m_ir_values.resize(m_count);
1372 v = ir_function_create_local(func, m_name, vtype, param);
1374 compile_error(m_context, "internal error: ir_function_create_local failed");
1377 v->m_context = m_context;
1378 v->m_unique_life = true;
1381 if (m_flags & AST_FLAG_NOREF)
1382 v->m_flags |= IR_FLAG_NOREF;
1384 const size_t namelen = m_name.length();
1385 std::unique_ptr<char[]> name(new char[namelen+16]);
1386 util_strncpy(name.get(), m_name.c_str(), namelen);
1389 for (size_t ai = 1; ai < m_count; ++ai) {
1390 util_snprintf(name.get() + namelen, 16, "[%u]", (unsigned int)ai);
1391 m_ir_values[ai] = ir_function_create_local(func, name.get(), vtype, param);
1392 if (!m_ir_values[ai]) {
1393 compile_error(m_context, "internal_error: ir_builder::createGlobal failed on `%s`", name.get());
1396 m_ir_values[ai]->m_context = m_context;
1397 m_ir_values[ai]->m_unique_life = true;
1398 m_ir_values[ai]->m_locked = true;
1400 if (m_flags & AST_FLAG_NOREF)
1401 m_ir_values[ai]->m_flags |= IR_FLAG_NOREF;
1406 v = ir_function_create_local(func, m_name, m_vtype, param);
1409 codegen_output_type(this, v);
1410 v->m_context = m_context;
1413 // A constant local... hmmm...
1414 // I suppose the IR will have to deal with this
1419 if (!v->setFloat(m_constval.vfloat))
1423 if (!v->setVector(m_constval.vvec))
1427 if (!v->setString(m_constval.vstring))
1431 compile_error(m_context, "TODO: global constant type %i", m_vtype);
1436 // link us to the ir_value
1440 if (m_flags & AST_FLAG_NOREF)
1441 m_ir_v->m_flags |= IR_FLAG_NOREF;
1443 if (!generateAccessors(func->m_owner))
1447 error: /* clean up */
1452 bool ast_value::generateAccessors(ir_builder *ir)
1455 bool warn = OPTS_WARN(WARN_USED_UNINITIALIZED);
1456 if (!m_setter || !m_getter)
1458 if (m_count && m_ir_values.empty()) {
1459 compile_error(m_context, "internal error: no array values generated for `%s`", m_name);
1462 for (i = 0; i < m_count; ++i) {
1463 if (!m_ir_values[i]) {
1464 compile_error(m_context, "internal error: not all array values have been generated for `%s`", m_name);
1467 if (!m_ir_values[i]->m_life.empty()) {
1468 compile_error(m_context, "internal error: function containing `%s` already generated", m_name);
1473 opts_set(opts.warn, WARN_USED_UNINITIALIZED, false);
1475 if (!m_setter->generateGlobal(ir, false) ||
1476 !m_setter->m_constval.vfunc->generateFunction(ir) ||
1477 !ir_function_finalize(m_setter->m_constval.vfunc->m_ir_func))
1479 compile_error(m_context, "internal error: failed to generate setter for `%s`", m_name);
1480 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1485 if (!m_getter->generateGlobal(ir, false) ||
1486 !m_getter->m_constval.vfunc->generateFunction(ir) ||
1487 !ir_function_finalize(m_getter->m_constval.vfunc->m_ir_func))
1489 compile_error(m_context, "internal error: failed to generate getter for `%s`", m_name);
1490 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1494 for (i = 0; i < m_count; ++i)
1495 m_ir_values[i]->m_life.clear();
1496 opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1500 bool ast_function::generateFunction(ir_builder *ir)
1506 ir_function *irf = m_ir_func;
1508 compile_error(m_context, "internal error: ast_function's related ast_value was not generated yet");
1512 /* fill the parameter list */
1513 for (auto &it : m_function_type->m_type_params) {
1514 if (it->m_vtype == TYPE_FIELD)
1515 irf->m_params.push_back(it->m_next->m_vtype);
1517 irf->m_params.push_back(it->m_vtype);
1519 if (!it->generateLocal(m_ir_func, true))
1525 if (!m_varargs->generateLocal(m_ir_func, true))
1527 irf->m_max_varargs = m_varargs->m_count;
1531 irf->m_builtin = m_builtin;
1535 /* have a local return value variable? */
1536 if (m_return_value) {
1537 if (!m_return_value->generateLocal(m_ir_func, false))
1541 if (m_blocks.empty()) {
1542 compile_error(m_context, "function `%s` has no body", m_name);
1546 irf->m_first = m_curblock = ir_function_create_block(m_context, irf, "entry");
1548 compile_error(m_context, "failed to allocate entry block for `%s`", m_name);
1556 if (!m_argc->generateLocal(m_ir_func, true))
1558 if (!m_argc->codegen(this, false, &va_count))
1560 if (!m_fixedparams->codegen(this, false, &fixed))
1562 sub = ir_block_create_binop(m_curblock, m_context,
1563 makeLabel("va_count"), INSTR_SUB_F,
1564 ir->get_va_count(), fixed);
1567 if (!ir_block_create_store_op(m_curblock, m_context, INSTR_STORE_F,
1574 for (auto &it : m_blocks) {
1575 if (!it->codegen(this, false, &dummy))
1579 /* TODO: check return types */
1580 if (!m_curblock->m_final)
1582 if (!m_function_type->m_next ||
1583 m_function_type->m_next->m_vtype == TYPE_VOID)
1585 return ir_block_create_return(m_curblock, m_context, nullptr);
1587 else if (m_curblock->m_entries.size() || m_curblock == irf->m_first)
1589 if (m_return_value) {
1590 if (!m_return_value->codegen(this, false, &dummy))
1592 return ir_block_create_return(m_curblock, m_context, dummy);
1594 else if (compile_warning(m_context, WARN_MISSING_RETURN_VALUES,
1595 "control reaches end of non-void function (`%s`) via %s",
1596 m_name.c_str(), m_curblock->m_label.c_str()))
1600 return ir_block_create_return(m_curblock, m_context, nullptr);
1606 static bool starts_a_label(const ast_expression *ex)
1608 while (ex && ast_istype(ex, ast_block)) {
1609 auto b = reinterpret_cast<const ast_block*>(ex);
1614 return ast_istype(ex, ast_label);
1617 /* Note, you will not see ast_block_codegen generate ir_blocks.
1618 * To the AST and the IR, blocks are 2 different things.
1619 * In the AST it represents a block of code, usually enclosed in
1620 * curly braces {...}.
1621 * While in the IR it represents a block in terms of control-flow.
1623 bool ast_block::codegen(ast_function *func, bool lvalue, ir_value **out)
1625 /* We don't use this
1626 * Note: an ast-representation using the comma-operator
1627 * of the form: (a, b, c) = x should not assign to c...
1630 compile_error(m_context, "not an l-value (code-block)");
1639 /* output is nullptr at first, we'll have each expression
1640 * assign to out output, thus, a comma-operator represention
1641 * using an ast_block will return the last generated value,
1642 * so: (b, c) + a executed both b and c, and returns c,
1643 * which is then added to a.
1647 /* generate locals */
1648 for (auto &it : m_locals) {
1649 if (!it->generateLocal(func->m_ir_func, false)) {
1650 if (OPTS_OPTION_BOOL(OPTION_DEBUG))
1651 compile_error(m_context, "failed to generate local `%s`", it->m_name);
1656 for (auto &it : m_exprs) {
1657 if (func->m_curblock->m_final && !starts_a_label(it)) {
1658 if (compile_warning(it->m_context, WARN_UNREACHABLE_CODE, "unreachable statement"))
1662 if (!it->codegen(func, false, out))
1671 bool ast_store::codegen(ast_function *func, bool lvalue, ir_value **out)
1673 ir_value *left = nullptr;
1674 ir_value *right = nullptr;
1677 ast_array_index *ai = nullptr;
1679 if (lvalue && m_outl) {
1684 if (!lvalue && m_outr) {
1689 if (ast_istype(m_dest, ast_array_index))
1692 ai = (ast_array_index*)m_dest;
1693 idx = (ast_value*)ai->m_index;
1695 if (ast_istype(ai->m_index, ast_value) && idx->m_hasvalue && idx->m_cvq == CV_CONST)
1700 /* we need to call the setter */
1701 ir_value *iridx, *funval;
1705 compile_error(m_context, "array-subscript assignment cannot produce lvalues");
1709 auto arr = reinterpret_cast<ast_value*>(ai->m_array);
1710 if (!ast_istype(ai->m_array, ast_value) || !arr->m_setter) {
1711 compile_error(m_context, "value has no setter (%s)", arr->m_name);
1715 if (!idx->codegen(func, false, &iridx))
1718 if (!arr->m_setter->codegen(func, true, &funval))
1721 if (!m_source->codegen(func, false, &right))
1724 call = ir_block_create_call(func->m_curblock, m_context, func->makeLabel("store"), funval, false);
1727 ir_call_param(call, iridx);
1728 ir_call_param(call, right);
1736 if (!m_dest->codegen(func, true, &left))
1741 if (!m_source->codegen(func, false, &right))
1744 if (!ir_block_create_store_op(func->m_curblock, m_context, m_op, left, right))
1749 /* Theoretically, an assinment returns its left side as an
1750 * lvalue, if we don't need an lvalue though, we return
1751 * the right side as an rvalue, otherwise we have to
1752 * somehow know whether or not we need to dereference the pointer
1753 * on the left side - that is: OP_LOAD if it was an address.
1754 * Also: in original QC we cannot OP_LOADP *anyway*.
1756 *out = (lvalue ? left : right);
1761 bool ast_binary::codegen(ast_function *func, bool lvalue, ir_value **out)
1763 ir_value *left, *right;
1765 /* A binary operation cannot yield an l-value */
1767 compile_error(m_context, "not an l-value (binop)");
1776 if ((OPTS_FLAG(SHORT_LOGIC) || OPTS_FLAG(PERL_LOGIC)) &&
1777 (m_op == INSTR_AND || m_op == INSTR_OR))
1779 /* NOTE: The short-logic path will ignore right_first */
1781 /* short circuit evaluation */
1782 ir_block *other, *merge;
1783 ir_block *from_left, *from_right;
1787 /* prepare end-block */
1788 merge_id = func->m_ir_func->m_blocks.size();
1789 merge = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("sce_merge"));
1791 /* generate the left expression */
1792 if (!m_left->codegen(func, false, &left))
1794 /* remember the block */
1795 from_left = func->m_curblock;
1797 /* create a new block for the right expression */
1798 other = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("sce_other"));
1799 if (m_op == INSTR_AND) {
1800 /* on AND: left==true -> other */
1801 if (!ir_block_create_if(func->m_curblock, m_context, left, other, merge))
1804 /* on OR: left==false -> other */
1805 if (!ir_block_create_if(func->m_curblock, m_context, left, merge, other))
1808 /* use the likely flag */
1809 func->m_curblock->m_instr.back()->m_likely = true;
1811 /* enter the right-expression's block */
1812 func->m_curblock = other;
1814 if (!m_right->codegen(func, false, &right))
1816 /* remember block */
1817 from_right = func->m_curblock;
1819 /* jump to the merge block */
1820 if (!ir_block_create_jump(func->m_curblock, m_context, merge))
1823 algo::shiftback(func->m_ir_func->m_blocks.begin() + merge_id,
1824 func->m_ir_func->m_blocks.end());
1826 //func->m_ir_func->m_blocks[merge_id].release();
1827 //func->m_ir_func->m_blocks.erase(func->m_ir_func->m_blocks.begin() + merge_id);
1828 //func->m_ir_func->m_blocks.emplace_back(merge);
1830 func->m_curblock = merge;
1831 phi = ir_block_create_phi(func->m_curblock, m_context,
1832 func->makeLabel("sce_value"),
1834 ir_phi_add(phi, from_left, left);
1835 ir_phi_add(phi, from_right, right);
1836 *out = ir_phi_value(phi);
1840 if (!OPTS_FLAG(PERL_LOGIC)) {
1842 if (OPTS_FLAG(CORRECT_LOGIC) && (*out)->m_vtype == TYPE_VECTOR) {
1843 *out = ir_block_create_unary(func->m_curblock, m_context,
1844 func->makeLabel("sce_bool_v"),
1848 *out = ir_block_create_unary(func->m_curblock, m_context,
1849 func->makeLabel("sce_bool"),
1854 else if (OPTS_FLAG(FALSE_EMPTY_STRINGS) && (*out)->m_vtype == TYPE_STRING) {
1855 *out = ir_block_create_unary(func->m_curblock, m_context,
1856 func->makeLabel("sce_bool_s"),
1860 *out = ir_block_create_unary(func->m_curblock, m_context,
1861 func->makeLabel("sce_bool"),
1867 *out = ir_block_create_binop(func->m_curblock, m_context,
1868 func->makeLabel("sce_bool"),
1869 INSTR_AND, *out, *out);
1876 codegen_output_type(this, *out);
1880 if (m_right_first) {
1881 if (!m_right->codegen(func, false, &right))
1883 if (!m_left->codegen(func, false, &left))
1886 if (!m_left->codegen(func, false, &left))
1888 if (!m_right->codegen(func, false, &right))
1892 *out = ir_block_create_binop(func->m_curblock, m_context, func->makeLabel("bin"),
1897 codegen_output_type(this, *out);
1902 bool ast_binstore::codegen(ast_function *func, bool lvalue, ir_value **out)
1904 ir_value *leftl = nullptr, *leftr, *right, *bin;
1908 ast_array_index *ai = nullptr;
1909 ir_value *iridx = nullptr;
1911 if (lvalue && m_outl) {
1916 if (!lvalue && m_outr) {
1921 if (ast_istype(m_dest, ast_array_index))
1924 ai = (ast_array_index*)m_dest;
1925 idx = (ast_value*)ai->m_index;
1927 if (ast_istype(ai->m_index, ast_value) && idx->m_hasvalue && idx->m_cvq == CV_CONST)
1931 /* for a binstore we need both an lvalue and an rvalue for the left side */
1932 /* rvalue of destination! */
1934 if (!idx->codegen(func, false, &iridx))
1938 if (!m_dest->codegen(func, false, &leftr))
1941 /* source as rvalue only */
1942 if (!m_source->codegen(func, false, &right))
1945 /* now the binary */
1946 bin = ir_block_create_binop(func->m_curblock, m_context, func->makeLabel("binst"),
1947 m_opbin, leftr, right);
1951 /* we need to call the setter */
1956 compile_error(m_context, "array-subscript assignment cannot produce lvalues");
1960 arr = (ast_value*)ai->m_array;
1961 if (!ast_istype(ai->m_array, ast_value) || !arr->m_setter) {
1962 compile_error(m_context, "value has no setter (%s)", arr->m_name);
1966 if (!arr->m_setter->codegen(func, true, &funval))
1969 call = ir_block_create_call(func->m_curblock, m_context, func->makeLabel("store"), funval, false);
1972 ir_call_param(call, iridx);
1973 ir_call_param(call, bin);
1977 // lvalue of destination
1978 if (!m_dest->codegen(func, true, &leftl))
1982 if (!ir_block_create_store_op(func->m_curblock, m_context, m_opstore, leftl, bin))
1987 /* Theoretically, an assinment returns its left side as an
1988 * lvalue, if we don't need an lvalue though, we return
1989 * the right side as an rvalue, otherwise we have to
1990 * somehow know whether or not we need to dereference the pointer
1991 * on the left side - that is: OP_LOAD if it was an address.
1992 * Also: in original QC we cannot OP_LOADP *anyway*.
1994 *out = (lvalue ? leftl : bin);
1999 bool ast_unary::codegen(ast_function *func, bool lvalue, ir_value **out)
2003 /* An unary operation cannot yield an l-value */
2005 compile_error(m_context, "not an l-value (binop)");
2015 if (!m_operand->codegen(func, false, &operand))
2018 *out = ir_block_create_unary(func->m_curblock, m_context, func->makeLabel("unary"),
2027 bool ast_return::codegen(ast_function *func, bool lvalue, ir_value **out)
2033 /* In the context of a return operation, we don't actually return
2037 compile_error(m_context, "return-expression is not an l-value");
2042 compile_error(m_context, "internal error: ast_return cannot be reused, it bears no result!");
2045 m_outr = (ir_value*)1;
2049 if (!m_operand->codegen(func, false, &operand))
2052 if (!ir_block_create_return(func->m_curblock, m_context, operand))
2055 if (!ir_block_create_return(func->m_curblock, m_context, nullptr))
2062 bool ast_entfield::codegen(ast_function *func, bool lvalue, ir_value **out)
2064 ir_value *ent, *field;
2066 // This function needs to take the 'lvalue' flag into account!
2067 // As lvalue we provide a field-pointer, as rvalue we provide the
2070 if (lvalue && m_outl) {
2075 if (!lvalue && m_outr) {
2080 if (!m_entity->codegen(func, false, &ent))
2083 if (!m_field->codegen(func, false, &field))
2088 *out = ir_block_create_fieldaddress(func->m_curblock, m_context, func->makeLabel("efa"),
2091 *out = ir_block_create_load_from_ent(func->m_curblock, m_context, func->makeLabel("efv"),
2092 ent, field, m_vtype);
2093 /* Done AFTER error checking:
2094 codegen_output_type(this, *out);
2098 compile_error(m_context, "failed to create %s instruction (output type %s)",
2099 (lvalue ? "ADDRESS" : "FIELD"),
2100 type_name[m_vtype]);
2104 codegen_output_type(this, *out);
2111 // Hm that should be it...
2115 bool ast_member::codegen(ast_function *func, bool lvalue, ir_value **out)
2119 /* in QC this is always an lvalue */
2120 if (lvalue && m_rvalue) {
2121 compile_error(m_context, "not an l-value (member access)");
2124 if (lvalue && m_outl) {
2128 if (!lvalue && m_outr) {
2133 if (ast_istype(m_owner, ast_entfield)) {
2134 ir_value *ent, *field;
2135 auto entfield = reinterpret_cast<ast_entfield*>(m_owner);
2136 if (!entfield->m_entity->codegen(func, false, &ent))
2138 if (!entfield->m_field->codegen(func, false, &vec))
2140 field = vec->vectorMember(m_field);
2142 *out = ir_block_create_fieldaddress(func->m_curblock, m_context, func->makeLabel("mefa"),
2145 *out = ir_block_create_load_from_ent(func->m_curblock, m_context, func->makeLabel("mefv"),
2146 ent, field, m_vtype);
2149 compile_error(m_context, "failed to create %s instruction (output type %s)",
2150 (lvalue ? "ADDRESS" : "FIELD"),
2151 type_name[m_vtype]);
2158 return (*out != nullptr);
2161 // Vector member access
2162 if (!m_owner->codegen(func, lvalue, &vec))
2165 if (vec->m_vtype != TYPE_VECTOR &&
2166 !(vec->m_vtype == TYPE_FIELD && m_owner->m_next->m_vtype == TYPE_VECTOR))
2168 compile_error(m_context, "vector member produced neither vector nor field");
2172 *out = vec->vectorMember(m_field);
2174 compile_error(m_context, "internal error: failed to create vector member access");
2182 return (*out != nullptr);
2185 bool ast_array_index::codegen(ast_function *func, bool lvalue, ir_value **out)
2190 if (!lvalue && m_outr) {
2194 if (lvalue && m_outl) {
2199 if (!ast_istype(m_array, ast_value)) {
2200 compile_error(m_context, "array indexing this way is not supported");
2201 /* note this would actually be pointer indexing because the left side is
2202 * not an actual array but (hopefully) an indexable expression.
2203 * Once we get integer arithmetic, and GADDRESS/GSTORE/GLOAD instruction
2204 * support this path will be filled.
2209 arr = reinterpret_cast<ast_value*>(m_array);
2210 idx = reinterpret_cast<ast_value*>(m_index);
2212 if (!ast_istype(m_index, ast_value) || !idx->m_hasvalue || idx->m_cvq != CV_CONST) {
2213 /* Time to use accessor functions */
2214 ir_value *iridx, *funval;
2218 compile_error(m_context, "(.2) array indexing here needs a compile-time constant");
2222 if (!arr->m_getter) {
2223 compile_error(m_context, "value has no getter, don't know how to index it");
2227 if (!m_index->codegen(func, false, &iridx))
2230 if (!arr->m_getter->codegen(func, true, &funval))
2233 call = ir_block_create_call(func->m_curblock, m_context, func->makeLabel("fetch"), funval, false);
2236 ir_call_param(call, iridx);
2238 *out = ir_call_value(call);
2240 (*out)->m_vtype = m_vtype;
2241 codegen_output_type(this, *out);
2245 if (idx->m_vtype == TYPE_FLOAT) {
2246 unsigned int arridx = idx->m_constval.vfloat;
2247 if (arridx >= m_array->m_count)
2249 compile_error(m_context, "array index out of bounds: %i", arridx);
2252 *out = arr->m_ir_values[arridx];
2254 else if (idx->m_vtype == TYPE_INTEGER) {
2255 unsigned int arridx = idx->m_constval.vint;
2256 if (arridx >= m_array->m_count)
2258 compile_error(m_context, "array index out of bounds: %i", arridx);
2261 *out = arr->m_ir_values[arridx];
2264 compile_error(m_context, "array indexing here needs an integer constant");
2267 (*out)->m_vtype = m_vtype;
2268 codegen_output_type(this, *out);
2272 bool ast_argpipe::codegen(ast_function *func, bool lvalue, ir_value **out)
2276 compile_error(m_context, "argpipe node: not an lvalue");
2281 compile_error(m_context, "TODO: argpipe codegen not implemented");
2285 bool ast_ifthen::codegen(ast_function *func, bool lvalue, ir_value **out)
2293 ir_block *ontrue_endblock = nullptr;
2294 ir_block *onfalse_endblock = nullptr;
2295 ir_block *merge = nullptr;
2297 /* We don't output any value, thus also don't care about r/lvalue */
2302 compile_error(m_context, "internal error: ast_ifthen cannot be reused, it bears no result!");
2305 m_outr = (ir_value*)1;
2307 /* try constant folding away the condition */
2308 switch (fold::cond_ifthen((ast_value*)m_cond, this)) {
2312 return m_on_true->codegen(func, false, out);
2313 case fold::ON_FALSE:
2314 return m_on_false->codegen(func, false, out);
2317 /* generate the condition */
2318 if (!m_cond->codegen(func, false, &condval))
2320 /* update the block which will get the jump - because short-logic or ternaries may have changed this */
2321 cond = func->m_curblock;
2324 /* create on-true block */
2325 ontrue = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("ontrue"));
2329 /* enter the block */
2330 func->m_curblock = ontrue;
2333 if (!m_on_true->codegen(func, false, &dummy))
2336 /* we now need to work from the current endpoint */
2337 ontrue_endblock = func->m_curblock;
2343 /* create on-false block */
2344 onfalse = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("onfalse"));
2348 /* enter the block */
2349 func->m_curblock = onfalse;
2352 if (!m_on_false->codegen(func, false, &dummy))
2355 /* we now need to work from the current endpoint */
2356 onfalse_endblock = func->m_curblock;
2360 /* Merge block were they all merge in to */
2361 if (!ontrue || !onfalse || !ontrue_endblock->m_final || !onfalse_endblock->m_final)
2363 merge = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("endif"));
2366 /* add jumps ot the merge block */
2367 if (ontrue && !ontrue_endblock->m_final && !ir_block_create_jump(ontrue_endblock, m_context, merge))
2369 if (onfalse && !onfalse_endblock->m_final && !ir_block_create_jump(onfalse_endblock, m_context, merge))
2372 /* Now enter the merge block */
2373 func->m_curblock = merge;
2376 /* we create the if here, that way all blocks are ordered :)
2378 if (!ir_block_create_if(cond, m_context, condval,
2379 (ontrue ? ontrue : merge),
2380 (onfalse ? onfalse : merge)))
2388 bool ast_ternary::codegen(ast_function *func, bool lvalue, ir_value **out)
2391 ir_value *trueval, *falseval;
2394 ir_block *cond = func->m_curblock;
2395 ir_block *cond_out = nullptr;
2396 ir_block *ontrue, *ontrue_out = nullptr;
2397 ir_block *onfalse, *onfalse_out = nullptr;
2400 /* Ternary can never create an lvalue... */
2404 /* In theory it shouldn't be possible to pass through a node twice, but
2405 * in case we add any kind of optimization pass for the AST itself, it
2406 * may still happen, thus we remember a created ir_value and simply return one
2407 * if it already exists.
2414 /* try constant folding away the condition */
2415 switch (fold::cond_ternary((ast_value*)m_cond, this)) {
2419 return m_on_true->codegen(func, false, out);
2420 case fold::ON_FALSE:
2421 return m_on_false->codegen(func, false, out);
2424 /* In the following, contraty to ast_ifthen, we assume both paths exist. */
2426 /* generate the condition */
2427 func->m_curblock = cond;
2428 if (!m_cond->codegen(func, false, &condval))
2430 cond_out = func->m_curblock;
2432 /* create on-true block */
2433 ontrue = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("tern_T"));
2438 /* enter the block */
2439 func->m_curblock = ontrue;
2442 if (!m_on_true->codegen(func, false, &trueval))
2445 ontrue_out = func->m_curblock;
2448 /* create on-false block */
2449 onfalse = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("tern_F"));
2454 /* enter the block */
2455 func->m_curblock = onfalse;
2458 if (!m_on_false->codegen(func, false, &falseval))
2461 onfalse_out = func->m_curblock;
2464 /* create merge block */
2465 merge = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("tern_out"));
2468 /* jump to merge block */
2469 if (!ir_block_create_jump(ontrue_out, m_context, merge))
2471 if (!ir_block_create_jump(onfalse_out, m_context, merge))
2474 /* create if instruction */
2475 if (!ir_block_create_if(cond_out, m_context, condval, ontrue, onfalse))
2478 /* Now enter the merge block */
2479 func->m_curblock = merge;
2481 /* Here, now, we need a PHI node
2482 * but first some sanity checking...
2484 if (trueval->m_vtype != falseval->m_vtype && trueval->m_vtype != TYPE_NIL && falseval->m_vtype != TYPE_NIL) {
2485 /* error("ternary with different types on the two sides"); */
2486 compile_error(m_context, "internal error: ternary operand types invalid");
2491 phi = ir_block_create_phi(merge, m_context, func->makeLabel("phi"), m_vtype);
2493 compile_error(m_context, "internal error: failed to generate phi node");
2496 ir_phi_add(phi, ontrue_out, trueval);
2497 ir_phi_add(phi, onfalse_out, falseval);
2499 m_outr = ir_phi_value(phi);
2502 codegen_output_type(this, *out);
2507 bool ast_loop::codegen(ast_function *func, bool lvalue, ir_value **out)
2509 ir_value *dummy = nullptr;
2510 ir_value *precond = nullptr;
2511 ir_value *postcond = nullptr;
2513 /* Since we insert some jumps "late" so we have blocks
2514 * ordered "nicely", we need to keep track of the actual end-blocks
2515 * of expressions to add the jumps to.
2517 ir_block *bbody = nullptr, *end_bbody = nullptr;
2518 ir_block *bprecond = nullptr, *end_bprecond = nullptr;
2519 ir_block *bpostcond = nullptr, *end_bpostcond = nullptr;
2520 ir_block *bincrement = nullptr, *end_bincrement = nullptr;
2521 ir_block *bout = nullptr, *bin = nullptr;
2523 /* let's at least move the outgoing block to the end */
2526 /* 'break' and 'continue' need to be able to find the right blocks */
2527 ir_block *bcontinue = nullptr;
2528 ir_block *bbreak = nullptr;
2530 ir_block *tmpblock = nullptr;
2536 compile_error(m_context, "internal error: ast_loop cannot be reused, it bears no result!");
2539 m_outr = (ir_value*)1;
2542 * Should we ever need some kind of block ordering, better make this function
2543 * move blocks around than write a block ordering algorithm later... after all
2544 * the ast and ir should work together, not against each other.
2547 /* initexpr doesn't get its own block, it's pointless, it could create more blocks
2548 * anyway if for example it contains a ternary.
2552 if (!m_initexpr->codegen(func, false, &dummy))
2556 /* Store the block from which we enter this chaos */
2557 bin = func->m_curblock;
2559 /* The pre-loop condition needs its own block since we
2560 * need to be able to jump to the start of that expression.
2564 bprecond = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("pre_loop_cond"));
2568 /* the pre-loop-condition the least important place to 'continue' at */
2569 bcontinue = bprecond;
2572 func->m_curblock = bprecond;
2575 if (!m_precond->codegen(func, false, &precond))
2578 end_bprecond = func->m_curblock;
2580 bprecond = end_bprecond = nullptr;
2583 /* Now the next blocks won't be ordered nicely, but we need to
2584 * generate them this early for 'break' and 'continue'.
2587 bincrement = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("loop_increment"));
2590 bcontinue = bincrement; /* increment comes before the pre-loop-condition */
2592 bincrement = end_bincrement = nullptr;
2596 bpostcond = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("post_loop_cond"));
2599 bcontinue = bpostcond; /* postcond comes before the increment */
2601 bpostcond = end_bpostcond = nullptr;
2604 bout_id = func->m_ir_func->m_blocks.size();
2605 bout = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("after_loop"));
2610 /* The loop body... */
2613 bbody = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("loop_body"));
2618 func->m_curblock = bbody;
2620 func->m_breakblocks.push_back(bbreak);
2622 func->m_continueblocks.push_back(bcontinue);
2624 func->m_continueblocks.push_back(bbody);
2628 if (!m_body->codegen(func, false, &dummy))
2632 end_bbody = func->m_curblock;
2633 func->m_breakblocks.pop_back();
2634 func->m_continueblocks.pop_back();
2637 /* post-loop-condition */
2641 func->m_curblock = bpostcond;
2644 if (!m_postcond->codegen(func, false, &postcond))
2647 end_bpostcond = func->m_curblock;
2650 /* The incrementor */
2654 func->m_curblock = bincrement;
2657 if (!m_increment->codegen(func, false, &dummy))
2660 end_bincrement = func->m_curblock;
2663 /* In any case now, we continue from the outgoing block */
2664 func->m_curblock = bout;
2666 /* Now all blocks are in place */
2667 /* From 'bin' we jump to whatever comes first */
2668 if (bprecond) tmpblock = bprecond;
2669 else tmpblock = bbody; /* can never be null */
2672 else if (bpostcond) tmpblock = bpostcond;
2673 else tmpblock = bout;
2676 if (!ir_block_create_jump(bin, m_context, tmpblock))
2682 ir_block *ontrue, *onfalse;
2683 ontrue = bbody; /* can never be null */
2685 /* all of this is dead code
2686 else if (bincrement) ontrue = bincrement;
2687 else ontrue = bpostcond;
2696 if (!ir_block_create_if(end_bprecond, m_context, precond, ontrue, onfalse))
2703 if (bincrement) tmpblock = bincrement;
2704 else if (bpostcond) tmpblock = bpostcond;
2705 else if (bprecond) tmpblock = bprecond;
2706 else tmpblock = bbody;
2707 if (!end_bbody->m_final && !ir_block_create_jump(end_bbody, m_context, tmpblock))
2711 /* from increment */
2714 if (bpostcond) tmpblock = bpostcond;
2715 else if (bprecond) tmpblock = bprecond;
2716 else if (bbody) tmpblock = bbody;
2717 else tmpblock = bout;
2718 if (!ir_block_create_jump(end_bincrement, m_context, tmpblock))
2725 ir_block *ontrue, *onfalse;
2726 if (bprecond) ontrue = bprecond;
2727 else ontrue = bbody; /* can never be null */
2729 /* all of this is dead code
2730 else if (bincrement) ontrue = bincrement;
2731 else ontrue = bpostcond;
2740 if (!ir_block_create_if(end_bpostcond, m_context, postcond, ontrue, onfalse))
2744 /* Move 'bout' to the end */
2745 algo::shiftback(func->m_ir_func->m_blocks.begin() + bout_id,
2746 func->m_ir_func->m_blocks.end());
2748 //func->m_ir_func->m_blocks[bout_id].release(); // it's a vector<std::unique_ptr<>>
2749 //func->m_ir_func->m_blocks.erase(func->m_ir_func->m_blocks.begin() + bout_id);
2750 //func->m_ir_func->m_blocks.emplace_back(bout);
2755 bool ast_breakcont::codegen(ast_function *func, bool lvalue, ir_value **out)
2762 compile_error(m_context, "break/continue expression is not an l-value");
2767 compile_error(m_context, "internal error: ast_breakcont cannot be reused!");
2770 m_outr = (ir_value*)1;
2773 target = func->m_continueblocks[func->m_continueblocks.size()-1-m_levels];
2775 target = func->m_breakblocks[func->m_breakblocks.size()-1-m_levels];
2778 compile_error(m_context, "%s is lacking a target block", (m_is_continue ? "continue" : "break"));
2782 if (!ir_block_create_jump(func->m_curblock, m_context, target))
2787 bool ast_switch::codegen(ast_function *func, bool lvalue, ir_value **out)
2789 ast_switch_case *def_case = nullptr;
2790 ir_block *def_bfall = nullptr;
2791 ir_block *def_bfall_to = nullptr;
2792 bool set_def_bfall_to = false;
2794 ir_value *dummy = nullptr;
2795 ir_value *irop = nullptr;
2796 ir_block *bout = nullptr;
2797 ir_block *bfall = nullptr;
2804 compile_error(m_context, "switch expression is not an l-value");
2809 compile_error(m_context, "internal error: ast_switch cannot be reused!");
2812 m_outr = (ir_value*)1;
2817 if (!m_operand->codegen(func, false, &irop))
2820 if (m_cases.empty())
2823 cmpinstr = type_eq_instr[irop->m_vtype];
2824 if (cmpinstr >= VINSTR_END) {
2825 ast_type_to_string(m_operand, typestr, sizeof(typestr));
2826 compile_error(m_context, "invalid type to perform a switch on: %s", typestr);
2830 bout_id = func->m_ir_func->m_blocks.size();
2831 bout = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("after_switch"));
2835 /* setup the break block */
2836 func->m_breakblocks.push_back(bout);
2838 /* Now create all cases */
2839 for (auto &it : m_cases) {
2840 ir_value *cond, *val;
2841 ir_block *bcase, *bnot;
2844 ast_switch_case *swcase = ⁢
2846 if (swcase->m_value) {
2847 /* A regular case */
2848 /* generate the condition operand */
2849 if (!swcase->m_value->codegen(func, false, &val))
2851 /* generate the condition */
2852 cond = ir_block_create_binop(func->m_curblock, m_context, func->makeLabel("switch_eq"), cmpinstr, irop, val);
2856 bcase = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("case"));
2857 bnot_id = func->m_ir_func->m_blocks.size();
2858 bnot = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("not_case"));
2859 if (!bcase || !bnot)
2861 if (set_def_bfall_to) {
2862 set_def_bfall_to = false;
2863 def_bfall_to = bcase;
2865 if (!ir_block_create_if(func->m_curblock, m_context, cond, bcase, bnot))
2868 /* Make the previous case-end fall through */
2869 if (bfall && !bfall->m_final) {
2870 if (!ir_block_create_jump(bfall, m_context, bcase))
2874 /* enter the case */
2875 func->m_curblock = bcase;
2876 if (!swcase->m_code->codegen(func, false, &dummy))
2879 /* remember this block to fall through from */
2880 bfall = func->m_curblock;
2882 /* enter the else and move it down */
2883 func->m_curblock = bnot;
2884 algo::shiftback(func->m_ir_func->m_blocks.begin() + bnot_id,
2885 func->m_ir_func->m_blocks.end());
2887 //func->m_ir_func->m_blocks[bnot_id].release();
2888 //func->m_ir_func->m_blocks.erase(func->m_ir_func->m_blocks.begin() + bnot_id);
2889 //func->m_ir_func->m_blocks.emplace_back(bnot);
2891 /* The default case */
2892 /* Remember where to fall through from: */
2895 /* remember which case it was */
2897 /* And the next case will be remembered */
2898 set_def_bfall_to = true;
2902 /* Jump from the last bnot to bout */
2903 if (bfall && !bfall->m_final && !ir_block_create_jump(bfall, m_context, bout)) {
2905 astwarning(bfall->m_context, WARN_???, "missing break after last case");
2910 /* If there was a default case, put it down here */
2914 /* No need to create an extra block */
2915 bcase = func->m_curblock;
2917 /* Insert the fallthrough jump */
2918 if (def_bfall && !def_bfall->m_final) {
2919 if (!ir_block_create_jump(def_bfall, m_context, bcase))
2923 /* Now generate the default code */
2924 if (!def_case->m_code->codegen(func, false, &dummy))
2927 /* see if we need to fall through */
2928 if (def_bfall_to && !func->m_curblock->m_final)
2930 if (!ir_block_create_jump(func->m_curblock, m_context, def_bfall_to))
2935 /* Jump from the last bnot to bout */
2936 if (!func->m_curblock->m_final && !ir_block_create_jump(func->m_curblock, m_context, bout))
2938 /* enter the outgoing block */
2939 func->m_curblock = bout;
2941 /* restore the break block */
2942 func->m_breakblocks.pop_back();
2944 /* Move 'bout' to the end, it's nicer */
2945 algo::shiftback(func->m_ir_func->m_blocks.begin() + bout_id,
2946 func->m_ir_func->m_blocks.end());
2948 //func->m_ir_func->m_blocks[bout_id].release();
2949 //func->m_ir_func->m_blocks.erase(func->m_ir_func->m_blocks.begin() + bout_id);
2950 //func->m_ir_func->m_blocks.emplace_back(bout);
2955 bool ast_label::codegen(ast_function *func, bool lvalue, ir_value **out)
2960 compile_error(m_context, "internal error: ast_label never defined");
2966 compile_error(m_context, "internal error: ast_label cannot be an lvalue");
2970 /* simply create a new block and jump to it */
2971 m_irblock = ir_function_create_block(m_context, func->m_ir_func, m_name.c_str());
2973 compile_error(m_context, "failed to allocate label block `%s`", m_name);
2976 if (!func->m_curblock->m_final) {
2977 if (!ir_block_create_jump(func->m_curblock, m_context, m_irblock))
2981 /* enter the new block */
2982 func->m_curblock = m_irblock;
2984 /* Generate all the leftover gotos */
2985 for (auto &it : m_gotos) {
2986 if (!it->codegen(func, false, &dummy))
2993 bool ast_goto::codegen(ast_function *func, bool lvalue, ir_value **out)
2997 compile_error(m_context, "internal error: ast_goto cannot be an lvalue");
3001 if (m_target->m_irblock) {
3002 if (m_irblock_from) {
3003 /* we already tried once, this is the callback */
3004 m_irblock_from->m_final = false;
3005 if (!ir_block_create_goto(m_irblock_from, m_context, m_target->m_irblock)) {
3006 compile_error(m_context, "failed to generate goto to `%s`", m_name);
3012 if (!ir_block_create_goto(func->m_curblock, m_context, m_target->m_irblock)) {
3013 compile_error(m_context, "failed to generate goto to `%s`", m_name);
3020 /* the target has not yet been created...
3021 * close this block in a sneaky way:
3023 func->m_curblock->m_final = true;
3024 m_irblock_from = func->m_curblock;
3025 m_target->registerGoto(this);
3031 bool ast_state::codegen(ast_function *func, bool lvalue, ir_value **out)
3033 ir_value *frameval, *thinkval;
3036 compile_error(m_context, "not an l-value (state operation)");
3040 compile_error(m_context, "internal error: ast_state cannot be reused!");
3045 if (!m_framenum->codegen(func, false, &frameval))
3050 if (!m_nextthink->codegen(func, false, &thinkval))
3055 if (!ir_block_create_state_op(func->m_curblock, m_context, frameval, thinkval)) {
3056 compile_error(m_context, "failed to create STATE instruction");
3060 m_outr = (ir_value*)1;
3064 bool ast_call::codegen(ast_function *func, bool lvalue, ir_value **out)
3066 std::vector<ir_value*> params;
3067 ir_instr *callinstr;
3069 ir_value *funval = nullptr;
3071 /* return values are never lvalues */
3073 compile_error(m_context, "not an l-value (function call)");
3082 if (!m_func->codegen(func, false, &funval))
3088 for (auto &it : m_params) {
3090 if (!it->codegen(func, false, ¶m))
3094 params.push_back(param);
3097 /* varargs counter */
3100 ir_builder *builder = func->m_curblock->m_owner->m_owner;
3101 if (!m_va_count->codegen(func, false, &va_count))
3103 if (!ir_block_create_store_op(func->m_curblock, m_context, INSTR_STORE_F,
3104 builder->get_va_count(), va_count))
3110 callinstr = ir_block_create_call(func->m_curblock, m_context,
3111 func->makeLabel("call"),
3112 funval, !!(m_func->m_flags & AST_FLAG_NORETURN));
3116 for (auto &it : params)
3117 ir_call_param(callinstr, it);
3119 *out = ir_call_value(callinstr);
3122 codegen_output_type(this, *out);