]> git.xonotic.org Git - xonotic/gmqcc.git/blob - ast.cpp
Merge pull request #187 from divVerent/patch-1
[xonotic/gmqcc.git] / ast.cpp
1 #include <new>
2
3 #include <stdlib.h>
4 #include <string.h>
5
6 #include "gmqcc.h"
7 #include "ast.h"
8 #include "fold.h"
9 //#include "parser.h"
10
11 #include "algo.h"
12
13 /* Initialize main ast node aprts */
14 ast_node::ast_node(lex_ctx_t ctx, int node_type)
15     : m_context(ctx)
16     , m_node_type(node_type)
17     , m_keep_node(false)
18     , m_side_effects(false)
19 {
20 }
21
22 ast_node::~ast_node()
23 {
24 }
25
26 /* weight and side effects */
27 void ast_node::propagateSideEffects(const ast_node *other)
28 {
29     if (other->m_side_effects)
30         m_side_effects = true;
31 }
32
33 /* General expression initialization */
34 ast_expression::ast_expression(lex_ctx_t ctx, int nodetype, qc_type type)
35     : ast_node(ctx, nodetype)
36     , m_vtype(type)
37 {
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;
42 }
43 ast_expression::ast_expression(lex_ctx_t ctx, int nodetype)
44     : ast_expression(ctx, nodetype, TYPE_VOID)
45 {}
46
47 ast_expression::~ast_expression()
48 {
49     if (m_next)
50         delete m_next;
51     if (m_varparam)
52         delete m_varparam;
53 }
54
55 ast_expression::ast_expression(ast_copy_type_t, const ast_expression &other)
56     : ast_expression(ast_copy_type, other.m_context, other)
57 {}
58
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)
61 {}
62
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)
65 {}
66
67 ast_expression::ast_expression(ast_copy_type_t, int nodetype, lex_ctx_t ctx, const ast_expression &other)
68     : ast_expression(ctx, nodetype)
69 {
70     m_vtype = other.m_vtype;
71     m_count = other.m_count;
72     m_flags = other.m_flags;
73     if (other.m_next)
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));
78 }
79
80
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;
84     return expr;
85 }
86
87 void ast_expression::adoptType(const ast_expression &other)
88 {
89     m_vtype = other.m_vtype;
90     if (other.m_next)
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));
98 }
99
100 bool ast_expression::compareType(const ast_expression &other) const
101 {
102     if (m_vtype == TYPE_NIL ||
103         other.m_vtype == TYPE_NIL)
104         return true;
105     if (m_vtype != other.m_vtype)
106         return false;
107     if (!m_next != !other.m_next)
108         return false;
109     if (m_type_params.size() != other.m_type_params.size())
110         return false;
111     if ((m_flags & AST_FLAG_TYPE_MASK) !=
112         (other.m_flags & AST_FLAG_TYPE_MASK) )
113     {
114         return false;
115     }
116     if (m_type_params.size()) {
117         size_t i;
118         for (i = 0; i < m_type_params.size(); ++i) {
119             if (!m_type_params[i]->compareType(*other.m_type_params[i]))
120                 return false;
121         }
122     }
123     if (m_next)
124         return m_next->compareType(*other.m_next);
125     return true;
126 }
127
128 bool ast_expression::codegen(ast_function*, bool, ir_value**) {
129     compile_error(m_context, "ast_expression::codegen called!");
130     abort();
131     return false;
132 }
133
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)
136 {
137     m_keep_node = true; // keep values, always
138     memset(&m_constval, 0, sizeof(m_constval));
139 }
140
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)
143 {
144     m_keep_node = true; // keep values, always
145     memset(&m_constval, 0, sizeof(m_constval));
146 }
147
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)
150     , m_name(name)
151 {
152     m_keep_node = true; // keep values, always
153     memset(&m_constval, 0, sizeof(m_constval));
154 }
155
156 ast_value::ast_value(lex_ctx_t ctx, const std::string &name, qc_type t)
157     : ast_expression(ctx, TYPE_ast_value, t)
158     , m_name(name)
159 {
160     m_keep_node = true; // keep values, always
161     memset(&m_constval, 0, sizeof(m_constval));
162 }
163
164 ast_value::~ast_value()
165 {
166     if (m_argcounter)
167         mem_d((void*)m_argcounter);
168     if (m_hasvalue) {
169         switch (m_vtype)
170         {
171         case TYPE_STRING:
172             mem_d((void*)m_constval.vstring);
173             break;
174         case TYPE_FUNCTION:
175             // unlink us from the function node
176             m_constval.vfunc->m_function_type = nullptr;
177             break;
178         // NOTE: delete function? currently collected in
179         // the parser structure
180         default:
181             break;
182         }
183     }
184
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)
188             if (it.vstring)
189                 mem_d(it.vstring);
190     }
191 }
192
193 static size_t ast_type_to_string_impl(const ast_expression *e, char *buf, size_t bufsize, size_t pos)
194 {
195     const char *typestr;
196     size_t typelen;
197     size_t i;
198
199     if (!e) {
200         if (pos + 6 >= bufsize)
201             goto full;
202         util_strncpy(buf + pos, "(null)", 6);
203         return pos + 6;
204     }
205
206     if (pos + 1 >= bufsize)
207         goto full;
208
209     switch (e->m_vtype) {
210         case TYPE_VARIANT:
211             util_strncpy(buf + pos, "(variant)", 9);
212             return pos + 9;
213
214         case TYPE_FIELD:
215             buf[pos++] = '.';
216             return ast_type_to_string_impl(e->m_next, buf, bufsize, pos);
217
218         case TYPE_POINTER:
219             if (pos + 3 >= bufsize)
220                 goto full;
221             buf[pos++] = '*';
222             buf[pos++] = '(';
223             pos = ast_type_to_string_impl(e->m_next, buf, bufsize, pos);
224             if (pos + 1 >= bufsize)
225                 goto full;
226             buf[pos++] = ')';
227             return pos;
228
229         case TYPE_FUNCTION:
230             pos = ast_type_to_string_impl(e->m_next, buf, bufsize, pos);
231             if (pos + 2 >= bufsize)
232                 goto full;
233             if (e->m_type_params.empty()) {
234                 buf[pos++] = '(';
235                 buf[pos++] = ')';
236                 return pos;
237             }
238             buf[pos++] = '(';
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)
242                     goto full;
243                 buf[pos++] = ',';
244                 buf[pos++] = ' ';
245                 pos = ast_type_to_string_impl(e->m_type_params[i].get(), buf, bufsize, pos);
246             }
247             if (pos + 1 >= bufsize)
248                 goto full;
249             buf[pos++] = ')';
250             return pos;
251
252         case TYPE_ARRAY:
253             pos = ast_type_to_string_impl(e->m_next, buf, bufsize, pos);
254             if (pos + 1 >= bufsize)
255                 goto full;
256             buf[pos++] = '[';
257             pos += util_snprintf(buf + pos, bufsize - pos - 1, "%i", (int)e->m_count);
258             if (pos + 1 >= bufsize)
259                 goto full;
260             buf[pos++] = ']';
261             return pos;
262
263         default:
264             typestr = type_name[e->m_vtype];
265             typelen = strlen(typestr);
266             if (pos + typelen >= bufsize)
267                 goto full;
268             util_strncpy(buf + pos, typestr, typelen);
269             return pos + typelen;
270     }
271
272 full:
273     buf[bufsize-3] = '.';
274     buf[bufsize-2] = '.';
275     buf[bufsize-1] = '.';
276     return bufsize;
277 }
278
279 void ast_type_to_string(const ast_expression *e, char *buf, size_t bufsize)
280 {
281     size_t pos = ast_type_to_string_impl(e, buf, bufsize-1, 0);
282     buf[pos] = 0;
283 }
284
285 void ast_value::addParam(ast_value *p)
286 {
287     m_type_params.emplace_back(p);
288 }
289
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)
293     , m_op(op)
294     // m_left/m_right happen after the peephole step right below
295     , m_right_first(false)
296 {
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;
300
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 = INSTR_ADD_F;
305                 right = normal;
306                 ++opts_optimizationcount[OPTIM_PEEPHOLE];
307             } else if (op == INSTR_SUB_V) {
308                 op = INSTR_ADD_V;
309                 right = normal;
310                 ++opts_optimizationcount[OPTIM_PEEPHOLE];
311             }
312         }
313     }
314
315     m_left = left;
316     m_right = right;
317
318     propagateSideEffects(left);
319     propagateSideEffects(right);
320
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))
325             adoptType(*right);
326         else
327             m_vtype = TYPE_FLOAT;
328     }
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;
335     else
336         m_vtype = left->m_vtype;
337
338     // references all
339     m_refs = AST_REF_ALL;
340 }
341
342 ast_binary::~ast_binary()
343 {
344     if (m_refs & AST_REF_LEFT)  ast_unref(m_left);
345     if (m_refs & AST_REF_RIGHT) ast_unref(m_right);
346 }
347
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)
351     , m_opstore(storop)
352     , m_opbin(mathop)
353     , m_dest(left)
354     , m_source(right)
355     , m_keep_dest(false)
356 {
357     m_side_effects = true;
358     adoptType(*left);
359 }
360
361 ast_binstore::~ast_binstore()
362 {
363     if (!m_keep_dest)
364         ast_unref(m_dest);
365     ast_unref(m_source);
366 }
367
368 ast_unary* ast_unary::make(lex_ctx_t ctx, int op, ast_expression *expr)
369 {
370     // handle double negation, double bitwise or logical not
371     if (op == opid2('!','P') ||
372         op == opid2('~','P') ||
373         op == opid2('-','P'))
374     {
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;
380                 delete unary;
381                 ++opts_optimizationcount[OPTIM_PEEPHOLE];
382                 return out;
383             }
384         }
385     }
386
387     return new ast_unary(ctx, op, expr);
388 }
389
390 ast_unary::ast_unary(lex_ctx_t ctx, int op, ast_expression *expr)
391     : ast_expression(ctx, TYPE_ast_unary)
392     , m_op(op)
393     , m_operand(expr)
394 {
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;
400     } else {
401         compile_error(ctx, "cannot determine type of unary operation %s", util_instr_str[op]);
402     }
403 }
404
405 ast_unary::~ast_unary()
406 {
407     if (m_operand)
408         ast_unref(m_operand);
409 }
410
411 ast_return::ast_return(lex_ctx_t ctx, ast_expression *expr)
412     : ast_expression(ctx, TYPE_ast_return)
413     , m_operand(expr)
414 {
415     if (expr)
416         propagateSideEffects(expr);
417 }
418
419 ast_return::~ast_return()
420 {
421     if (m_operand)
422         ast_unref(m_operand);
423 }
424
425 ast_entfield::ast_entfield(lex_ctx_t ctx, ast_expression *entity, ast_expression *field)
426     : ast_entfield(ctx, entity, field, field->m_next)
427 {
428     if (field->m_vtype != TYPE_FIELD)
429         compile_error(ctx, "ast_entfield with expression not of type field");
430 }
431
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)
434     , m_entity(entity)
435     , m_field(field)
436 {
437     propagateSideEffects(m_entity);
438     propagateSideEffects(m_field);
439
440     if (!outtype) {
441         compile_error(ctx, "ast_entfield: field has no type");
442         m_vtype = TYPE_VOID;
443     }
444     else
445         adoptType(*outtype);
446 }
447
448 ast_entfield::~ast_entfield()
449 {
450     ast_unref(m_entity);
451     ast_unref(m_field);
452 }
453
454 ast_member *ast_member::make(lex_ctx_t ctx, ast_expression *owner, unsigned int field, const std::string &name)
455 {
456     if (field >= 3) {
457         compile_error(ctx, "ast_member: invalid field (>=3): %u", field);
458         return nullptr;
459     }
460     if (owner->m_vtype != TYPE_VECTOR &&
461         owner->m_vtype != TYPE_FIELD)
462     {
463         compile_error(ctx, "member-access on an invalid owner of type %s", type_name[owner->m_vtype]);
464         return nullptr;
465     }
466     return new ast_member(ctx, owner, field, name);
467 }
468
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)
471     , m_owner(owner)
472     , m_field(field)
473     , m_name(name)
474     , m_rvalue(false)
475 {
476     m_keep_node = true;
477
478     if (m_owner->m_vtype == TYPE_VECTOR) {
479         m_vtype = TYPE_FLOAT;
480         m_next  = nullptr;
481     } else {
482         m_vtype = TYPE_FIELD;
483         m_next = ast_expression::shallowType(ctx, TYPE_FLOAT);
484     }
485
486     propagateSideEffects(owner);
487 }
488
489 ast_member::~ast_member()
490 {
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.
498 }
499
500 ast_array_index* ast_array_index::make(lex_ctx_t ctx, ast_expression *array, ast_expression *index)
501 {
502     ast_expression *outtype = array->m_next;
503     if (!outtype) {
504         // field has no type
505         return nullptr;
506     }
507
508     return new ast_array_index(ctx, array, index);
509 }
510
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)
513     , m_array(array)
514     , m_index(index)
515 {
516     propagateSideEffects(array);
517     propagateSideEffects(index);
518
519     ast_expression *outtype = m_array->m_next;
520     adoptType(*outtype);
521
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);
527         //    return nullptr;
528         //}
529
530         m_array = outtype;
531         m_vtype = TYPE_FIELD;
532     }
533 }
534
535 ast_array_index::~ast_array_index()
536 {
537     if (m_array)
538         ast_unref(m_array);
539     if (m_index)
540         ast_unref(m_index);
541 }
542
543 ast_argpipe::ast_argpipe(lex_ctx_t ctx, ast_expression *index)
544     : ast_expression(ctx, TYPE_ast_argpipe)
545     , m_index(index)
546 {
547     m_vtype = TYPE_NOEXPR;
548 }
549
550 ast_argpipe::~ast_argpipe()
551 {
552     if (m_index)
553         ast_unref(m_index);
554 }
555
556 ast_store::ast_store(lex_ctx_t ctx, int op, ast_expression *dest, ast_expression *source)
557     : ast_expression(ctx, TYPE_ast_store)
558     , m_op(op)
559     , m_dest(dest)
560     , m_source(source)
561 {
562     m_side_effects = true;
563     adoptType(*dest);
564 }
565
566 ast_store::~ast_store()
567 {
568     ast_unref(m_dest);
569     ast_unref(m_source);
570 }
571
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)
574     , m_cond(cond)
575     , m_on_true(ontrue)
576     , m_on_false(onfalse)
577 {
578     propagateSideEffects(cond);
579     if (ontrue)
580         propagateSideEffects(ontrue);
581     if (onfalse)
582         propagateSideEffects(onfalse);
583 }
584
585 ast_ifthen::~ast_ifthen()
586 {
587     ast_unref(m_cond);
588     if (m_on_true)
589         ast_unref(m_on_true);
590     if (m_on_false)
591         ast_unref(m_on_false);
592 }
593
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)
596     , m_cond(cond)
597     , m_on_true(ontrue)
598     , m_on_false(onfalse)
599 {
600     propagateSideEffects(cond);
601     propagateSideEffects(ontrue);
602     propagateSideEffects(onfalse);
603
604     if (ontrue->m_vtype == TYPE_NIL)
605         adoptType(*onfalse);
606     else
607         adoptType(*ontrue);
608 }
609
610 ast_ternary::~ast_ternary()
611 {
612     /* the if()s are only there because computed-gotos can set them
613      * to nullptr
614      */
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);
618 }
619
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)
628     , m_precond(precond)
629     , m_postcond(postcond)
630     , m_increment(increment)
631     , m_body(body)
632     , m_pre_not(pre_not)
633     , m_post_not(post_not)
634 {
635     if (initexpr)
636         propagateSideEffects(initexpr);
637     if (precond)
638         propagateSideEffects(precond);
639     if (postcond)
640         propagateSideEffects(postcond);
641     if (increment)
642         propagateSideEffects(increment);
643     if (body)
644         propagateSideEffects(body);
645 }
646
647 ast_loop::~ast_loop()
648 {
649     if (m_initexpr)
650         ast_unref(m_initexpr);
651     if (m_precond)
652         ast_unref(m_precond);
653     if (m_postcond)
654         ast_unref(m_postcond);
655     if (m_increment)
656         ast_unref(m_increment);
657     if (m_body)
658         ast_unref(m_body);
659 }
660
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)
664     , m_levels(levels)
665 {
666 }
667
668 ast_breakcont::~ast_breakcont()
669 {
670 }
671
672 ast_switch::ast_switch(lex_ctx_t ctx, ast_expression *op)
673     : ast_expression(ctx, TYPE_ast_switch)
674     , m_operand(op)
675 {
676     propagateSideEffects(op);
677 }
678
679 ast_switch::~ast_switch()
680 {
681     ast_unref(m_operand);
682
683     for (auto &it : m_cases) {
684         if (it.m_value)
685             ast_unref(it.m_value);
686         ast_unref(it.m_code);
687     }
688 }
689
690 ast_label::ast_label(lex_ctx_t ctx, const std::string &name, bool undefined)
691     : ast_expression(ctx, TYPE_ast_label)
692     , m_name(name)
693     , m_irblock(nullptr)
694     , m_undefined(undefined)
695 {
696     m_vtype = TYPE_NOEXPR;
697 }
698
699 ast_label::~ast_label()
700 {
701 }
702
703 void ast_label::registerGoto(ast_goto *g)
704 {
705    m_gotos.push_back(g);
706 }
707
708 ast_goto::ast_goto(lex_ctx_t ctx, const std::string &name)
709     : ast_expression(ctx, TYPE_ast_goto)
710     , m_name(name)
711     , m_target(nullptr)
712     , m_irblock_from(nullptr)
713 {
714 }
715
716 ast_goto::~ast_goto()
717 {
718 }
719
720 void ast_goto::setLabel(ast_label *label)
721 {
722     m_target = label;
723 }
724
725 ast_state::ast_state(lex_ctx_t ctx, ast_expression *frame, ast_expression *think)
726     : ast_expression(ctx, TYPE_ast_expression)
727     , m_framenum(frame)
728     , m_nextthink(think)
729 {
730 }
731
732 ast_state::~ast_state()
733 {
734     if (m_framenum)
735         ast_unref(m_framenum);
736     if (m_nextthink)
737         ast_unref(m_nextthink);
738 }
739
740 ast_call *ast_call::make(lex_ctx_t ctx, ast_expression *funcexpr)
741 {
742     if (!funcexpr->m_next) {
743         compile_error(ctx, "not a function");
744         return nullptr;
745     }
746     return new ast_call(ctx, funcexpr);
747 }
748
749 ast_call::ast_call(lex_ctx_t ctx, ast_expression *funcexpr)
750     : ast_expression(ctx, TYPE_ast_call)
751     , m_func(funcexpr)
752     , m_va_count(nullptr)
753 {
754     m_side_effects = true;
755     adoptType(*funcexpr->m_next);
756 }
757
758 ast_call::~ast_call()
759 {
760     for (auto &it : m_params)
761         ast_unref(it);
762
763     if (m_func)
764         ast_unref(m_func);
765
766     if (m_va_count)
767         ast_unref(m_va_count);
768 }
769
770 bool ast_call::checkVararg(ast_expression *va_type, ast_expression *exp_type) const
771 {
772     char texp[1024];
773     char tgot[1024];
774     if (!exp_type)
775         return true;
776     if (!va_type || !va_type->compareType(*exp_type))
777     {
778         if (va_type && exp_type)
779         {
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",
785                                     tgot, texp))
786                     return false;
787             } else {
788                 compile_error(m_context,
789                               "piped variadic argument differs in type: constrained to type %s, expected type %s",
790                               tgot, texp);
791                 return false;
792             }
793         }
794         else
795         {
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",
800                                     texp))
801                     return false;
802             } else {
803                 compile_error(m_context,
804                               "piped variadic argument may differ in type: expected type %s",
805                               texp);
806                 return false;
807             }
808         }
809     }
810     return true;
811 }
812
813 bool ast_call::checkTypes(ast_expression *va_type) const
814 {
815     char texp[1024];
816     char tgot[1024];
817     size_t i;
818     bool retval = true;
819
820     size_t count = m_params.size();
821     if (count > m_func->m_type_params.size())
822         count = m_func->m_type_params.size();
823
824     for (i = 0; i < count; ++i) {
825         if (ast_istype(m_params[i], ast_argpipe)) {
826             /* warn about type safety instead */
827             if (i+1 != count) {
828                 compile_error(m_context, "argpipe must be the last parameter to a function call");
829                 return false;
830             }
831             if (!checkVararg(va_type, m_func->m_type_params[i].get()))
832                 retval = false;
833         }
834         else if (!m_params[i]->compareType(*m_func->m_type_params[i]))
835         {
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 */
841             retval = false;
842         }
843     }
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 */
849                 if (i+1 != count) {
850                     compile_error(m_context, "argpipe must be the last parameter to a function call");
851                     return false;
852                 }
853                 if (!checkVararg(va_type, m_func->m_varparam))
854                     retval = false;
855             }
856             else if (!m_params[i]->compareType(*m_func->m_varparam))
857             {
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 */
863                 retval = false;
864             }
865         }
866     }
867     return retval;
868 }
869
870 ast_block::ast_block(lex_ctx_t ctx)
871     : ast_expression(ctx, TYPE_ast_block)
872 {
873 }
874
875 ast_block::~ast_block()
876 {
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;
880 }
881
882 void ast_block::setType(const ast_expression &from)
883 {
884     if (m_next)
885         delete m_next;
886     adoptType(from);
887 }
888
889
890 bool ast_block::addExpr(ast_expression *e)
891 {
892     propagateSideEffects(e);
893     m_exprs.push_back(e);
894     if (m_next) {
895         delete m_next;
896         m_next = nullptr;
897     }
898     adoptType(*e);
899     return true;
900 }
901
902 void ast_block::collect(ast_expression *expr)
903 {
904     m_collect.push_back(expr);
905     expr->m_keep_node = true;
906 }
907
908 ast_function *ast_function::make(lex_ctx_t ctx, const std::string &name, ast_value *vtype)
909 {
910     if (!vtype) {
911         compile_error(ctx, "internal error: ast_function_new condition 0");
912         return nullptr;
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?)",
915                  (int)!vtype,
916                  (int)vtype->m_hasvalue,
917                  vtype->m_vtype);
918         return nullptr;
919     }
920     return new ast_function(ctx, name, vtype);
921 }
922
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)
926     , m_name(name)
927     , m_builtin(0)
928     , m_static_count(0)
929     , m_ir_func(nullptr)
930     , m_curblock(nullptr)
931     , m_labelcount(0)
932     , m_varargs(nullptr)
933     , m_argc(nullptr)
934     , m_fixedparams(nullptr)
935     , m_return_value(nullptr)
936 {
937     vtype->m_hasvalue = true;
938     vtype->m_constval.vfunc = this;
939 }
940
941 ast_function::~ast_function()
942 {
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);
950     }
951
952     if (m_fixedparams)
953         ast_unref(m_fixedparams);
954     if (m_return_value)
955         ast_unref(m_return_value);
956
957     // force this to be cleared before m_varargs/m_argc as blocks might
958     // try to access them via ast_unref()
959     m_blocks.clear();
960 }
961
962 const char* ast_function::makeLabel(const char *prefix)
963 {
964     size_t id;
965     size_t len;
966     char  *from;
967
968     if (!OPTS_OPTION_BOOL(OPTION_DUMP)    &&
969         !OPTS_OPTION_BOOL(OPTION_DUMPFIN) &&
970         !OPTS_OPTION_BOOL(OPTION_DEBUG))
971     {
972         return nullptr;
973     }
974
975     id  = (m_labelcount++);
976     len = strlen(prefix);
977
978     from = m_labelbuf + sizeof(m_labelbuf)-1;
979     *from-- = 0;
980     do {
981         *from-- = (id%10) + '0';
982         id /= 10;
983     } while (id);
984     ++from;
985     memcpy(from - len, prefix, len);
986     return from - len;
987 }
988
989 /*********************************************************************/
990 /* AST codegen part
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.
994  */
995
996 static void codegen_output_type(ast_expression *self, ir_value *out)
997 {
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;
1002 }
1003
1004 bool ast_value::codegen(ast_function *func, bool lvalue, ir_value **out)
1005 {
1006     (void)func;
1007     (void)lvalue;
1008     if (m_vtype == TYPE_NIL) {
1009         *out = func->m_ir_func->m_owner->m_nil;
1010         return true;
1011     }
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.
1018     if (!m_ir_v) {
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);
1022         return false;
1023     }
1024     *out = m_ir_v;
1025     return true;
1026 }
1027
1028 bool ast_value::setGlobalArray()
1029 {
1030     size_t count = m_initlist.size();
1031     size_t i;
1032
1033     if (count > m_count) {
1034         compile_error(m_context, "too many elements in initializer");
1035         count = m_count;
1036     }
1037     else if (count < m_count) {
1038         /* add this?
1039         compile_warning(m_context, "not all elements are initialized");
1040         */
1041     }
1042
1043     for (i = 0; i != count; ++i) {
1044         switch (m_next->m_vtype) {
1045             case TYPE_FLOAT:
1046                 if (!m_ir_values[i]->setFloat(m_initlist[i].vfloat))
1047                     return false;
1048                 break;
1049             case TYPE_VECTOR:
1050                 if (!m_ir_values[i]->setVector(m_initlist[i].vvec))
1051                     return false;
1052                 break;
1053             case TYPE_STRING:
1054                 if (!m_ir_values[i]->setString(m_initlist[i].vstring))
1055                     return false;
1056                 break;
1057             case TYPE_ARRAY:
1058                 /* we don't support them in any other place yet either */
1059                 compile_error(m_context, "TODO: nested arrays");
1060                 return false;
1061             case TYPE_FUNCTION:
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");
1064                 return false;
1065             case TYPE_FIELD:
1066                 if (!m_initlist[i].vfield) {
1067                     compile_error(m_context, "field constant without vfield set");
1068                     return false;
1069                 }
1070                 if (!m_initlist[i].vfield->m_ir_v) {
1071                     compile_error(m_context, "field constant generated before its field");
1072                     return false;
1073                 }
1074                 if (!m_ir_values[i]->setField(m_initlist[i].vfield->m_ir_v))
1075                     return false;
1076                 break;
1077             default:
1078                 compile_error(m_context, "TODO: global constant type %i", m_vtype);
1079                 break;
1080         }
1081     }
1082     return true;
1083 }
1084
1085 bool ast_value::checkArray(const ast_value &array) const
1086 {
1087     if (array.m_flags & AST_FLAG_ARRAY_INIT && array.m_initlist.empty()) {
1088         compile_error(m_context, "array without size: %s", m_name);
1089         return false;
1090     }
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);
1094         return false;
1095     }
1096     return true;
1097 }
1098
1099 bool ast_value::generateGlobal(ir_builder *ir, bool isfield)
1100 {
1101     if (m_vtype == TYPE_NIL) {
1102         compile_error(m_context, "internal error: trying to generate a variable of TYPE_NIL");
1103         return false;
1104     }
1105
1106     if (m_hasvalue && m_vtype == TYPE_FUNCTION)
1107         return generateGlobalFunction(ir);
1108
1109     if (isfield && m_vtype == TYPE_FIELD)
1110         return generateGlobalField(ir);
1111
1112     ir_value *v = nullptr;
1113     if (m_vtype == TYPE_ARRAY) {
1114         v = prepareGlobalArray(ir);
1115         if (!v)
1116             return false;
1117     } else {
1118         // Arrays don't do this since there's no "array" value which spans across the
1119         // whole thing.
1120         v = ir->createGlobal(m_name, m_vtype);
1121         if (!v) {
1122             compile_error(m_context, "ir_builder::createGlobal failed on `%s`", m_name);
1123             return false;
1124         }
1125         codegen_output_type(this, v);
1126         v->m_context = m_context;
1127     }
1128
1129     /* link us to the ir_value */
1130     v->m_cvq = m_cvq;
1131     m_ir_v = v;
1132
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;
1139
1140     /* initialize */
1141     if (m_hasvalue) {
1142         switch (m_vtype)
1143         {
1144             case TYPE_FLOAT:
1145                 if (!v->setFloat(m_constval.vfloat))
1146                     return false;
1147                 break;
1148             case TYPE_VECTOR:
1149                 if (!v->setVector(m_constval.vvec))
1150                     return false;
1151                 break;
1152             case TYPE_STRING:
1153                 if (!v->setString(m_constval.vstring))
1154                     return false;
1155                 break;
1156             case TYPE_ARRAY:
1157                 if (!setGlobalArray())
1158                     return false;
1159                 break;
1160             case TYPE_FUNCTION:
1161                 compile_error(m_context, "global of type function not properly generated");
1162                 return false;
1163                 /* Cannot generate an IR value for a function,
1164                  * need a pointer pointing to a function rather.
1165                  */
1166             case TYPE_FIELD:
1167                 if (!m_constval.vfield) {
1168                     compile_error(m_context, "field constant without vfield set");
1169                     return false;
1170                 }
1171                 if (!m_constval.vfield->m_ir_v) {
1172                     compile_error(m_context, "field constant generated before its field");
1173                     return false;
1174                 }
1175                 if (!v->setField(m_constval.vfield->m_ir_v))
1176                     return false;
1177                 break;
1178             default:
1179                 compile_error(m_context, "TODO: global constant type %i", m_vtype);
1180                 break;
1181         }
1182     }
1183
1184     return true;
1185 }
1186
1187 bool ast_value::generateGlobalFunction(ir_builder *ir)
1188 {
1189     ir_function *func = ir->createFunction(m_name, m_next->m_vtype);
1190     if (!func)
1191         return false;
1192     func->m_context = m_context;
1193     func->m_value->m_context = m_context;
1194
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...
1204     return true;
1205 }
1206
1207 bool ast_value::generateGlobalField(ir_builder *ir)
1208 {
1209     ast_expression *fieldtype = m_next;
1210
1211     if (m_hasvalue) {
1212         compile_error(m_context, "TODO: constant field pointers with value");
1213         return false;
1214     }
1215
1216     if (fieldtype->m_vtype == TYPE_ARRAY) {
1217         if (!ast_istype(fieldtype, ast_value)) {
1218             compile_error(m_context, "internal error: ast_value required");
1219             return false;
1220         }
1221         ast_value      *array = reinterpret_cast<ast_value*>(fieldtype);
1222
1223         if (!checkArray(*array))
1224             return false;
1225
1226         ast_expression *elemtype = array->m_next;
1227         qc_type vtype = elemtype->m_vtype;
1228
1229         ir_value *v = ir->createField(m_name, vtype);
1230         if (!v) {
1231             compile_error(m_context, "ir_builder::createGlobal failed on `%s`", m_name);
1232             return false;
1233         }
1234         v->m_context = m_context;
1235         v->m_unique_life = true;
1236         v->m_locked      = true;
1237         array->m_ir_v = m_ir_v = v;
1238
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;
1245
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);
1249
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());
1257                 return false;
1258             }
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;
1266         }
1267     }
1268     else
1269     {
1270         ir_value *v = ir->createField(m_name, m_next->m_vtype);
1271         if (!v)
1272             return false;
1273         v->m_context = m_context;
1274         m_ir_v = v;
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;
1281     }
1282     return true;
1283 }
1284
1285 ir_value *ast_value::prepareGlobalArray(ir_builder *ir)
1286 {
1287     ast_expression *elemtype = m_next;
1288     qc_type vtype = elemtype->m_vtype;
1289
1290     if (m_flags & AST_FLAG_ARRAY_INIT && !m_count) {
1291         compile_error(m_context, "array `%s' has no size", m_name);
1292         return nullptr;
1293     }
1294
1295     /* same as with field arrays */
1296     if (!checkArray(*this))
1297         return nullptr;
1298
1299     ir_value *v = ir->createGlobal(m_name, vtype);
1300     if (!v) {
1301         compile_error(m_context, "ir_builder::createGlobal failed `%s`", m_name);
1302         return nullptr;
1303     }
1304     v->m_context = m_context;
1305     v->m_unique_life = true;
1306     v->m_locked      = true;
1307
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;
1314
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);
1318
1319     m_ir_values.resize(m_count);
1320     m_ir_values[0] = v;
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());
1326             return nullptr;
1327         }
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;
1335     }
1336
1337     return v;
1338 }
1339
1340 bool ast_value::generateLocal(ir_function *func, bool param)
1341 {
1342     if (m_vtype == TYPE_NIL) {
1343         compile_error(m_context, "internal error: trying to generate a variable of TYPE_NIL");
1344         return false;
1345     }
1346
1347     if (m_hasvalue && m_vtype == TYPE_FUNCTION)
1348     {
1349         /* Do we allow local functions? I think not...
1350          * this is NOT a function pointer atm.
1351          */
1352         return false;
1353     }
1354
1355     ir_value *v = nullptr;
1356     if (m_vtype == TYPE_ARRAY) {
1357         ast_expression *elemtype = m_next;
1358         qc_type vtype = elemtype->m_vtype;
1359
1360         func->m_flags |= IR_FLAG_HAS_ARRAYS;
1361
1362         if (param && !(m_flags & AST_FLAG_IS_VARARG)) {
1363             compile_error(m_context, "array-parameters are not supported");
1364             return false;
1365         }
1366
1367         /* we are lame now - considering the way QC works we won't tolerate arrays > 1024 elements */
1368         if (!checkArray(*this))
1369             return false;
1370
1371         m_ir_values.resize(m_count);
1372         v = ir_function_create_local(func, m_name, vtype, param);
1373         if (!v) {
1374             compile_error(m_context, "internal error: ir_function_create_local failed");
1375             return false;
1376         }
1377         v->m_context = m_context;
1378         v->m_unique_life = true;
1379         v->m_locked      = true;
1380
1381         if (m_flags & AST_FLAG_NOREF)
1382             v->m_flags |= IR_FLAG_NOREF;
1383
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);
1387
1388         m_ir_values[0] = v;
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());
1394                 return false;
1395             }
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;
1399
1400             if (m_flags & AST_FLAG_NOREF)
1401                 m_ir_values[ai]->m_flags |= IR_FLAG_NOREF;
1402         }
1403     }
1404     else
1405     {
1406         v = ir_function_create_local(func, m_name, m_vtype, param);
1407         if (!v)
1408             return false;
1409         codegen_output_type(this, v);
1410         v->m_context = m_context;
1411     }
1412
1413     // A constant local... hmmm...
1414     // I suppose the IR will have to deal with this
1415     if (m_hasvalue) {
1416         switch (m_vtype)
1417         {
1418             case TYPE_FLOAT:
1419                 if (!v->setFloat(m_constval.vfloat))
1420                     goto error;
1421                 break;
1422             case TYPE_VECTOR:
1423                 if (!v->setVector(m_constval.vvec))
1424                     goto error;
1425                 break;
1426             case TYPE_STRING:
1427                 if (!v->setString(m_constval.vstring))
1428                     goto error;
1429                 break;
1430             default:
1431                 compile_error(m_context, "TODO: global constant type %i", m_vtype);
1432                 break;
1433         }
1434     }
1435
1436     // link us to the ir_value
1437     v->m_cvq = m_cvq;
1438     m_ir_v = v;
1439
1440     if (m_flags & AST_FLAG_NOREF)
1441         m_ir_v->m_flags |= IR_FLAG_NOREF;
1442
1443     if (!generateAccessors(func->m_owner))
1444         return false;
1445     return true;
1446
1447 error: /* clean up */
1448     delete v;
1449     return false;
1450 }
1451
1452 bool ast_value::generateAccessors(ir_builder *ir)
1453 {
1454     size_t i;
1455     bool warn = OPTS_WARN(WARN_USED_UNINITIALIZED);
1456     if (!m_setter || !m_getter)
1457         return true;
1458     if (m_count && m_ir_values.empty()) {
1459         compile_error(m_context, "internal error: no array values generated for `%s`", m_name);
1460         return false;
1461     }
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);
1465             return false;
1466         }
1467         if (!m_ir_values[i]->m_life.empty()) {
1468             compile_error(m_context, "internal error: function containing `%s` already generated", m_name);
1469             return false;
1470         }
1471     }
1472
1473     opts_set(opts.warn, WARN_USED_UNINITIALIZED, false);
1474     if (m_setter) {
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))
1478         {
1479             compile_error(m_context, "internal error: failed to generate setter for `%s`", m_name);
1480             opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1481             return false;
1482         }
1483     }
1484     if (m_getter) {
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))
1488         {
1489             compile_error(m_context, "internal error: failed to generate getter for `%s`", m_name);
1490             opts_set(opts.warn, WARN_USED_UNINITIALIZED, warn);
1491             return false;
1492         }
1493     }
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);
1497     return true;
1498 }
1499
1500 bool ast_function::generateFunction(ir_builder *ir)
1501 {
1502     (void)ir;
1503
1504     ir_value *dummy;
1505
1506     ir_function *irf = m_ir_func;
1507     if (!irf) {
1508         compile_error(m_context, "internal error: ast_function's related ast_value was not generated yet");
1509         return false;
1510     }
1511
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);
1516         else
1517             irf->m_params.push_back(it->m_vtype);
1518         if (!m_builtin) {
1519             if (!it->generateLocal(m_ir_func, true))
1520                 return false;
1521         }
1522     }
1523
1524     if (m_varargs) {
1525         if (!m_varargs->generateLocal(m_ir_func, true))
1526             return false;
1527         irf->m_max_varargs = m_varargs->m_count;
1528     }
1529
1530     if (m_builtin) {
1531         irf->m_builtin = m_builtin;
1532         return true;
1533     }
1534
1535     /* have a local return value variable? */
1536     if (m_return_value) {
1537         if (!m_return_value->generateLocal(m_ir_func, false))
1538             return false;
1539     }
1540
1541     if (m_blocks.empty()) {
1542         compile_error(m_context, "function `%s` has no body", m_name);
1543         return false;
1544     }
1545
1546     irf->m_first = m_curblock = ir_function_create_block(m_context, irf, "entry");
1547     if (!m_curblock) {
1548         compile_error(m_context, "failed to allocate entry block for `%s`", m_name);
1549         return false;
1550     }
1551
1552     if (m_argc) {
1553         ir_value *va_count;
1554         ir_value *fixed;
1555         ir_value *sub;
1556         if (!m_argc->generateLocal(m_ir_func, true))
1557             return false;
1558         if (!m_argc->codegen(this, false, &va_count))
1559             return false;
1560         if (!m_fixedparams->codegen(this, false, &fixed))
1561             return false;
1562         sub = ir_block_create_binop(m_curblock, m_context,
1563                                     makeLabel("va_count"), INSTR_SUB_F,
1564                                     ir->get_va_count(), fixed);
1565         if (!sub)
1566             return false;
1567         if (!ir_block_create_store_op(m_curblock, m_context, INSTR_STORE_F,
1568                                       va_count, sub))
1569         {
1570             return false;
1571         }
1572     }
1573
1574     for (auto &it : m_blocks) {
1575         if (!it->codegen(this, false, &dummy))
1576           return false;
1577     }
1578
1579     /* TODO: check return types */
1580     if (!m_curblock->m_final)
1581     {
1582         if (!m_function_type->m_next ||
1583             m_function_type->m_next->m_vtype == TYPE_VOID)
1584         {
1585             return ir_block_create_return(m_curblock, m_context, nullptr);
1586         }
1587         else if (m_curblock->m_entries.size() || m_curblock == irf->m_first)
1588         {
1589             if (m_return_value) {
1590                 if (!m_return_value->codegen(this, false, &dummy))
1591                     return false;
1592                 return ir_block_create_return(m_curblock, m_context, dummy);
1593             }
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()))
1597             {
1598                 return false;
1599             }
1600             return ir_block_create_return(m_curblock, m_context, nullptr);
1601         }
1602     }
1603     return true;
1604 }
1605
1606 static bool starts_a_label(const ast_expression *ex)
1607 {
1608     while (ex && ast_istype(ex, ast_block)) {
1609         auto b = reinterpret_cast<const ast_block*>(ex);
1610         ex = b->m_exprs[0];
1611     }
1612     if (!ex)
1613         return false;
1614     return ast_istype(ex, ast_label);
1615 }
1616
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.
1622  */
1623 bool ast_block::codegen(ast_function *func, bool lvalue, ir_value **out)
1624 {
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...
1628      */
1629     if (lvalue) {
1630         compile_error(m_context, "not an l-value (code-block)");
1631         return false;
1632     }
1633
1634     if (m_outr) {
1635         *out = m_outr;
1636         return true;
1637     }
1638
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.
1644      */
1645     *out = nullptr;
1646
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);
1652             return false;
1653         }
1654     }
1655
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"))
1659                 return false;
1660             continue;
1661         }
1662         if (!it->codegen(func, false, out))
1663             return false;
1664     }
1665
1666     m_outr = *out;
1667
1668     return true;
1669 }
1670
1671 bool ast_store::codegen(ast_function *func, bool lvalue, ir_value **out)
1672 {
1673     ir_value *left  = nullptr;
1674     ir_value *right = nullptr;
1675
1676     ast_value       *idx = 0;
1677     ast_array_index *ai = nullptr;
1678
1679     if (lvalue && m_outl) {
1680         *out = m_outl;
1681         return true;
1682     }
1683
1684     if (!lvalue && m_outr) {
1685         *out = m_outr;
1686         return true;
1687     }
1688
1689     if (ast_istype(m_dest, ast_array_index))
1690     {
1691
1692         ai = (ast_array_index*)m_dest;
1693         idx = (ast_value*)ai->m_index;
1694
1695         if (ast_istype(ai->m_index, ast_value) && idx->m_hasvalue && idx->m_cvq == CV_CONST)
1696             ai = nullptr;
1697     }
1698
1699     if (ai) {
1700         /* we need to call the setter */
1701         ir_value  *iridx, *funval;
1702         ir_instr  *call;
1703
1704         if (lvalue) {
1705             compile_error(m_context, "array-subscript assignment cannot produce lvalues");
1706             return false;
1707         }
1708
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);
1712             return false;
1713         }
1714
1715         if (!idx->codegen(func, false, &iridx))
1716             return false;
1717
1718         if (!arr->m_setter->codegen(func, true, &funval))
1719             return false;
1720
1721         if (!m_source->codegen(func, false, &right))
1722             return false;
1723
1724         call = ir_block_create_call(func->m_curblock, m_context, func->makeLabel("store"), funval, false);
1725         if (!call)
1726             return false;
1727         ir_call_param(call, iridx);
1728         ir_call_param(call, right);
1729         m_outr = right;
1730     }
1731     else
1732     {
1733         // regular code
1734
1735         // lvalue!
1736         if (!m_dest->codegen(func, true, &left))
1737             return false;
1738         m_outl = left;
1739
1740         /* rvalue! */
1741         if (!m_source->codegen(func, false, &right))
1742             return false;
1743
1744         if (!ir_block_create_store_op(func->m_curblock, m_context, m_op, left, right))
1745             return false;
1746         m_outr = right;
1747     }
1748
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*.
1755      */
1756     *out = (lvalue ? left : right);
1757
1758     return true;
1759 }
1760
1761 bool ast_binary::codegen(ast_function *func, bool lvalue, ir_value **out)
1762 {
1763     ir_value *left, *right;
1764
1765     /* A binary operation cannot yield an l-value */
1766     if (lvalue) {
1767         compile_error(m_context, "not an l-value (binop)");
1768         return false;
1769     }
1770
1771     if (m_outr) {
1772         *out = m_outr;
1773         return true;
1774     }
1775
1776     if ((OPTS_FLAG(SHORT_LOGIC) || OPTS_FLAG(PERL_LOGIC)) &&
1777         (m_op == INSTR_AND || m_op == INSTR_OR))
1778     {
1779         /* NOTE: The short-logic path will ignore right_first */
1780
1781         /* short circuit evaluation */
1782         ir_block *other, *merge;
1783         ir_block *from_left, *from_right;
1784         ir_instr *phi;
1785         size_t    merge_id;
1786
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"));
1790
1791         /* generate the left expression */
1792         if (!m_left->codegen(func, false, &left))
1793             return false;
1794         /* remember the block */
1795         from_left = func->m_curblock;
1796
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))
1802                 return false;
1803         } else {
1804             /* on OR: left==false -> other */
1805             if (!ir_block_create_if(func->m_curblock, m_context, left, merge, other))
1806                 return false;
1807         }
1808         /* use the likely flag */
1809         func->m_curblock->m_instr.back()->m_likely = true;
1810
1811         /* enter the right-expression's block */
1812         func->m_curblock = other;
1813         /* generate */
1814         if (!m_right->codegen(func, false, &right))
1815             return false;
1816         /* remember block */
1817         from_right = func->m_curblock;
1818
1819         /* jump to the merge block */
1820         if (!ir_block_create_jump(func->m_curblock, m_context, merge))
1821             return false;
1822
1823         algo::shiftback(func->m_ir_func->m_blocks.begin() + merge_id,
1824                         func->m_ir_func->m_blocks.end());
1825         // FIXME::DELME::
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);
1829
1830         func->m_curblock = merge;
1831         phi = ir_block_create_phi(func->m_curblock, m_context,
1832                                   func->makeLabel("sce_value"),
1833                                   m_vtype);
1834         ir_phi_add(phi, from_left, left);
1835         ir_phi_add(phi, from_right, right);
1836         *out = ir_phi_value(phi);
1837         if (!*out)
1838             return false;
1839
1840         if (!OPTS_FLAG(PERL_LOGIC)) {
1841             /* cast-to-bool */
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"),
1845                                              INSTR_NOT_V, *out);
1846                 if (!*out)
1847                     return false;
1848                 *out = ir_block_create_unary(func->m_curblock, m_context,
1849                                              func->makeLabel("sce_bool"),
1850                                              INSTR_NOT_F, *out);
1851                 if (!*out)
1852                     return false;
1853             }
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"),
1857                                              INSTR_NOT_S, *out);
1858                 if (!*out)
1859                     return false;
1860                 *out = ir_block_create_unary(func->m_curblock, m_context,
1861                                              func->makeLabel("sce_bool"),
1862                                              INSTR_NOT_F, *out);
1863                 if (!*out)
1864                     return false;
1865             }
1866             else {
1867                 *out = ir_block_create_binop(func->m_curblock, m_context,
1868                                              func->makeLabel("sce_bool"),
1869                                              INSTR_AND, *out, *out);
1870                 if (!*out)
1871                     return false;
1872             }
1873         }
1874
1875         m_outr = *out;
1876         codegen_output_type(this, *out);
1877         return true;
1878     }
1879
1880     if (m_right_first) {
1881         if (!m_right->codegen(func, false, &right))
1882             return false;
1883         if (!m_left->codegen(func, false, &left))
1884             return false;
1885     } else {
1886         if (!m_left->codegen(func, false, &left))
1887             return false;
1888         if (!m_right->codegen(func, false, &right))
1889             return false;
1890     }
1891
1892     *out = ir_block_create_binop(func->m_curblock, m_context, func->makeLabel("bin"),
1893                                  m_op, left, right);
1894     if (!*out)
1895         return false;
1896     m_outr = *out;
1897     codegen_output_type(this, *out);
1898
1899     return true;
1900 }
1901
1902 bool ast_binstore::codegen(ast_function *func, bool lvalue, ir_value **out)
1903 {
1904     ir_value *leftl = nullptr, *leftr, *right, *bin;
1905
1906     ast_value       *arr;
1907     ast_value       *idx = 0;
1908     ast_array_index *ai = nullptr;
1909     ir_value        *iridx = nullptr;
1910
1911     if (lvalue && m_outl) {
1912         *out = m_outl;
1913         return true;
1914     }
1915
1916     if (!lvalue && m_outr) {
1917         *out = m_outr;
1918         return true;
1919     }
1920
1921     if (ast_istype(m_dest, ast_array_index))
1922     {
1923
1924         ai = (ast_array_index*)m_dest;
1925         idx = (ast_value*)ai->m_index;
1926
1927         if (ast_istype(ai->m_index, ast_value) && idx->m_hasvalue && idx->m_cvq == CV_CONST)
1928             ai = nullptr;
1929     }
1930
1931     /* for a binstore we need both an lvalue and an rvalue for the left side */
1932     /* rvalue of destination! */
1933     if (ai) {
1934         if (!idx->codegen(func, false, &iridx))
1935             return false;
1936     }
1937
1938     if (!m_dest->codegen(func, false, &leftr))
1939         return false;
1940
1941     /* source as rvalue only */
1942     if (!m_source->codegen(func, false, &right))
1943         return false;
1944
1945     /* now the binary */
1946     bin = ir_block_create_binop(func->m_curblock, m_context, func->makeLabel("binst"),
1947                                 m_opbin, leftr, right);
1948     m_outr = bin;
1949
1950     if (ai) {
1951         /* we need to call the setter */
1952         ir_value  *funval;
1953         ir_instr  *call;
1954
1955         if (lvalue) {
1956             compile_error(m_context, "array-subscript assignment cannot produce lvalues");
1957             return false;
1958         }
1959
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);
1963             return false;
1964         }
1965
1966         if (!arr->m_setter->codegen(func, true, &funval))
1967             return false;
1968
1969         call = ir_block_create_call(func->m_curblock, m_context, func->makeLabel("store"), funval, false);
1970         if (!call)
1971             return false;
1972         ir_call_param(call, iridx);
1973         ir_call_param(call, bin);
1974         m_outr = bin;
1975     } else {
1976         // now store them
1977         // lvalue of destination
1978         if (!m_dest->codegen(func, true, &leftl))
1979             return false;
1980         m_outl = leftl;
1981
1982         if (!ir_block_create_store_op(func->m_curblock, m_context, m_opstore, leftl, bin))
1983             return false;
1984         m_outr = bin;
1985     }
1986
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*.
1993      */
1994     *out = (lvalue ? leftl : bin);
1995
1996     return true;
1997 }
1998
1999 bool ast_unary::codegen(ast_function *func, bool lvalue, ir_value **out)
2000 {
2001     ir_value *operand;
2002
2003     /* An unary operation cannot yield an l-value */
2004     if (lvalue) {
2005         compile_error(m_context, "not an l-value (binop)");
2006         return false;
2007     }
2008
2009     if (m_outr) {
2010         *out = m_outr;
2011         return true;
2012     }
2013
2014     /* lvalue! */
2015     if (!m_operand->codegen(func, false, &operand))
2016         return false;
2017
2018     *out = ir_block_create_unary(func->m_curblock, m_context, func->makeLabel("unary"),
2019                                  m_op, operand);
2020     if (!*out)
2021         return false;
2022     m_outr = *out;
2023
2024     return true;
2025 }
2026
2027 bool ast_return::codegen(ast_function *func, bool lvalue, ir_value **out)
2028 {
2029     ir_value *operand;
2030
2031     *out = nullptr;
2032
2033     /* In the context of a return operation, we don't actually return
2034      * anything...
2035      */
2036     if (lvalue) {
2037         compile_error(m_context, "return-expression is not an l-value");
2038         return false;
2039     }
2040
2041     if (m_outr) {
2042         compile_error(m_context, "internal error: ast_return cannot be reused, it bears no result!");
2043         return false;
2044     }
2045     m_outr = (ir_value*)1;
2046
2047     if (m_operand) {
2048         /* lvalue! */
2049         if (!m_operand->codegen(func, false, &operand))
2050             return false;
2051
2052         if (!ir_block_create_return(func->m_curblock, m_context, operand))
2053             return false;
2054     } else {
2055         if (!ir_block_create_return(func->m_curblock, m_context, nullptr))
2056             return false;
2057     }
2058
2059     return true;
2060 }
2061
2062 bool ast_entfield::codegen(ast_function *func, bool lvalue, ir_value **out)
2063 {
2064     ir_value *ent, *field;
2065
2066     // This function needs to take the 'lvalue' flag into account!
2067     // As lvalue we provide a field-pointer, as rvalue we provide the
2068     // value in a temp.
2069
2070     if (lvalue && m_outl) {
2071         *out = m_outl;
2072         return true;
2073     }
2074
2075     if (!lvalue && m_outr) {
2076         *out = m_outr;
2077         return true;
2078     }
2079
2080     if (!m_entity->codegen(func, false, &ent))
2081         return false;
2082
2083     if (!m_field->codegen(func, false, &field))
2084         return false;
2085
2086     if (lvalue) {
2087         /* address! */
2088         *out = ir_block_create_fieldaddress(func->m_curblock, m_context, func->makeLabel("efa"),
2089                                             ent, field);
2090     } else {
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);
2095         */
2096     }
2097     if (!*out) {
2098         compile_error(m_context, "failed to create %s instruction (output type %s)",
2099                  (lvalue ? "ADDRESS" : "FIELD"),
2100                  type_name[m_vtype]);
2101         return false;
2102     }
2103     if (!lvalue)
2104         codegen_output_type(this, *out);
2105
2106     if (lvalue)
2107         m_outl = *out;
2108     else
2109         m_outr = *out;
2110
2111     // Hm that should be it...
2112     return true;
2113 }
2114
2115 bool ast_member::codegen(ast_function *func, bool lvalue, ir_value **out)
2116 {
2117     ir_value *vec;
2118
2119     /* in QC this is always an lvalue */
2120     if (lvalue && m_rvalue) {
2121         compile_error(m_context, "not an l-value (member access)");
2122         return false;
2123     }
2124     if (lvalue && m_outl) {
2125         *out = m_outl;
2126         return true;
2127     }
2128     if (!lvalue && m_outr) {
2129         *out = m_outr;
2130         return true;
2131     }
2132
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))
2137             return false;
2138         if (!entfield->m_field->codegen(func, false, &vec))
2139             return false;
2140         field = vec->vectorMember(m_field);
2141         if (lvalue) {
2142             *out = ir_block_create_fieldaddress(func->m_curblock, m_context, func->makeLabel("mefa"),
2143                                                 ent, field);
2144         } else {
2145             *out = ir_block_create_load_from_ent(func->m_curblock, m_context, func->makeLabel("mefv"),
2146                                                  ent, field, m_vtype);
2147         }
2148         if (!*out) {
2149             compile_error(m_context, "failed to create %s instruction (output type %s)",
2150                      (lvalue ? "ADDRESS" : "FIELD"),
2151                      type_name[m_vtype]);
2152             return false;
2153         }
2154         if (lvalue)
2155             m_outl = *out;
2156         else
2157             m_outr = *out;
2158         return (*out != nullptr);
2159     }
2160
2161     // Vector member access
2162     if (!m_owner->codegen(func, lvalue, &vec))
2163         return false;
2164
2165     if (vec->m_vtype != TYPE_VECTOR &&
2166         !(vec->m_vtype == TYPE_FIELD && m_owner->m_next->m_vtype == TYPE_VECTOR))
2167     {
2168         compile_error(m_context, "vector member produced neither vector nor field");
2169         return false;
2170     }
2171
2172     *out = vec->vectorMember(m_field);
2173     if (!*out) {
2174         compile_error(m_context, "internal error: failed to create vector member access");
2175         return false;
2176     }
2177     if (lvalue)
2178         m_outl = *out;
2179     else
2180         m_outr = *out;
2181
2182     return (*out != nullptr);
2183 }
2184
2185 bool ast_array_index::codegen(ast_function *func, bool lvalue, ir_value **out)
2186 {
2187     ast_value *arr;
2188     ast_value *idx;
2189
2190     if (!lvalue && m_outr) {
2191         *out = m_outr;
2192         return true;
2193     }
2194     if (lvalue && m_outl) {
2195         *out = m_outl;
2196         return true;
2197     }
2198
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.
2205          */
2206         return false;
2207     }
2208
2209     arr = reinterpret_cast<ast_value*>(m_array);
2210     idx = reinterpret_cast<ast_value*>(m_index);
2211
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;
2215         ir_instr               *call;
2216
2217         if (lvalue) {
2218             compile_error(m_context, "(.2) array indexing here needs a compile-time constant");
2219             return false;
2220         }
2221
2222         if (!arr->m_getter) {
2223             compile_error(m_context, "value has no getter, don't know how to index it");
2224             return false;
2225         }
2226
2227         if (!m_index->codegen(func, false, &iridx))
2228             return false;
2229
2230         if (!arr->m_getter->codegen(func, true, &funval))
2231             return false;
2232
2233         call = ir_block_create_call(func->m_curblock, m_context, func->makeLabel("fetch"), funval, false);
2234         if (!call)
2235             return false;
2236         ir_call_param(call, iridx);
2237
2238         *out = ir_call_value(call);
2239         m_outr = *out;
2240         (*out)->m_vtype = m_vtype;
2241         codegen_output_type(this, *out);
2242         return true;
2243     }
2244
2245     if (idx->m_vtype == TYPE_FLOAT) {
2246         unsigned int arridx = idx->m_constval.vfloat;
2247         if (arridx >= m_array->m_count)
2248         {
2249             compile_error(m_context, "array index out of bounds: %i", arridx);
2250             return false;
2251         }
2252         *out = arr->m_ir_values[arridx];
2253     }
2254     else if (idx->m_vtype == TYPE_INTEGER) {
2255         unsigned int arridx = idx->m_constval.vint;
2256         if (arridx >= m_array->m_count)
2257         {
2258             compile_error(m_context, "array index out of bounds: %i", arridx);
2259             return false;
2260         }
2261         *out = arr->m_ir_values[arridx];
2262     }
2263     else {
2264         compile_error(m_context, "array indexing here needs an integer constant");
2265         return false;
2266     }
2267     (*out)->m_vtype = m_vtype;
2268     codegen_output_type(this, *out);
2269     return true;
2270 }
2271
2272 bool ast_argpipe::codegen(ast_function *func, bool lvalue, ir_value **out)
2273 {
2274     *out = nullptr;
2275     if (lvalue) {
2276         compile_error(m_context, "argpipe node: not an lvalue");
2277         return false;
2278     }
2279     (void)func;
2280     (void)out;
2281     compile_error(m_context, "TODO: argpipe codegen not implemented");
2282     return false;
2283 }
2284
2285 bool ast_ifthen::codegen(ast_function *func, bool lvalue, ir_value **out)
2286 {
2287     ir_value *condval;
2288     ir_value *dummy;
2289
2290     ir_block *cond;
2291     ir_block *ontrue;
2292     ir_block *onfalse;
2293     ir_block *ontrue_endblock = nullptr;
2294     ir_block *onfalse_endblock = nullptr;
2295     ir_block *merge = nullptr;
2296     int folded = 0;
2297
2298     /* We don't output any value, thus also don't care about r/lvalue */
2299     (void)out;
2300     (void)lvalue;
2301
2302     if (m_outr) {
2303         compile_error(m_context, "internal error: ast_ifthen cannot be reused, it bears no result!");
2304         return false;
2305     }
2306     m_outr = (ir_value*)1;
2307
2308     /* generate the condition */
2309     if (!m_cond->codegen(func, false, &condval))
2310         return false;
2311     /* update the block which will get the jump - because short-logic or ternaries may have changed this */
2312     cond = func->m_curblock;
2313
2314     /* try constant folding away the condition */
2315     if ((folded = fold::cond_ifthen(condval, func, this)) != -1)
2316         return folded;
2317
2318     if (m_on_true) {
2319         /* create on-true block */
2320         ontrue = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("ontrue"));
2321         if (!ontrue)
2322             return false;
2323
2324         /* enter the block */
2325         func->m_curblock = ontrue;
2326
2327         /* generate */
2328         if (!m_on_true->codegen(func, false, &dummy))
2329             return false;
2330
2331         /* we now need to work from the current endpoint */
2332         ontrue_endblock = func->m_curblock;
2333     } else
2334         ontrue = nullptr;
2335
2336     /* on-false path */
2337     if (m_on_false) {
2338         /* create on-false block */
2339         onfalse = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("onfalse"));
2340         if (!onfalse)
2341             return false;
2342
2343         /* enter the block */
2344         func->m_curblock = onfalse;
2345
2346         /* generate */
2347         if (!m_on_false->codegen(func, false, &dummy))
2348             return false;
2349
2350         /* we now need to work from the current endpoint */
2351         onfalse_endblock = func->m_curblock;
2352     } else
2353         onfalse = nullptr;
2354
2355     /* Merge block were they all merge in to */
2356     if (!ontrue || !onfalse || !ontrue_endblock->m_final || !onfalse_endblock->m_final)
2357     {
2358         merge = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("endif"));
2359         if (!merge)
2360             return false;
2361         /* add jumps ot the merge block */
2362         if (ontrue && !ontrue_endblock->m_final && !ir_block_create_jump(ontrue_endblock, m_context, merge))
2363             return false;
2364         if (onfalse && !onfalse_endblock->m_final && !ir_block_create_jump(onfalse_endblock, m_context, merge))
2365             return false;
2366
2367         /* Now enter the merge block */
2368         func->m_curblock = merge;
2369     }
2370
2371     /* we create the if here, that way all blocks are ordered :)
2372      */
2373     if (!ir_block_create_if(cond, m_context, condval,
2374                             (ontrue  ? ontrue  : merge),
2375                             (onfalse ? onfalse : merge)))
2376     {
2377         return false;
2378     }
2379
2380     return true;
2381 }
2382
2383 bool ast_ternary::codegen(ast_function *func, bool lvalue, ir_value **out)
2384 {
2385     ir_value *condval;
2386     ir_value *trueval, *falseval;
2387     ir_instr *phi;
2388
2389     ir_block *cond = func->m_curblock;
2390     ir_block *cond_out = nullptr;
2391     ir_block *ontrue, *ontrue_out = nullptr;
2392     ir_block *onfalse, *onfalse_out = nullptr;
2393     ir_block *merge;
2394     int folded = 0;
2395
2396     /* Ternary can never create an lvalue... */
2397     if (lvalue)
2398         return false;
2399
2400     /* In theory it shouldn't be possible to pass through a node twice, but
2401      * in case we add any kind of optimization pass for the AST itself, it
2402      * may still happen, thus we remember a created ir_value and simply return one
2403      * if it already exists.
2404      */
2405     if (m_outr) {
2406         *out = m_outr;
2407         return true;
2408     }
2409
2410     /* In the following, contraty to ast_ifthen, we assume both paths exist. */
2411
2412     /* generate the condition */
2413     func->m_curblock = cond;
2414     if (!m_cond->codegen(func, false, &condval))
2415         return false;
2416     cond_out = func->m_curblock;
2417
2418     /* try constant folding away the condition */
2419     if ((folded = fold::cond_ternary(condval, func, this)) != -1)
2420         return folded;
2421
2422     /* create on-true block */
2423     ontrue = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("tern_T"));
2424     if (!ontrue)
2425         return false;
2426     else
2427     {
2428         /* enter the block */
2429         func->m_curblock = ontrue;
2430
2431         /* generate */
2432         if (!m_on_true->codegen(func, false, &trueval))
2433             return false;
2434
2435         ontrue_out = func->m_curblock;
2436     }
2437
2438     /* create on-false block */
2439     onfalse = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("tern_F"));
2440     if (!onfalse)
2441         return false;
2442     else
2443     {
2444         /* enter the block */
2445         func->m_curblock = onfalse;
2446
2447         /* generate */
2448         if (!m_on_false->codegen(func, false, &falseval))
2449             return false;
2450
2451         onfalse_out = func->m_curblock;
2452     }
2453
2454     /* create merge block */
2455     merge = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("tern_out"));
2456     if (!merge)
2457         return false;
2458     /* jump to merge block */
2459     if (!ir_block_create_jump(ontrue_out, m_context, merge))
2460         return false;
2461     if (!ir_block_create_jump(onfalse_out, m_context, merge))
2462         return false;
2463
2464     /* create if instruction */
2465     if (!ir_block_create_if(cond_out, m_context, condval, ontrue, onfalse))
2466         return false;
2467
2468     /* Now enter the merge block */
2469     func->m_curblock = merge;
2470
2471     /* Here, now, we need a PHI node
2472      * but first some sanity checking...
2473      */
2474     if (trueval->m_vtype != falseval->m_vtype && trueval->m_vtype != TYPE_NIL && falseval->m_vtype != TYPE_NIL) {
2475         /* error("ternary with different types on the two sides"); */
2476         compile_error(m_context, "internal error: ternary operand types invalid");
2477         return false;
2478     }
2479
2480     /* create PHI */
2481     phi = ir_block_create_phi(merge, m_context, func->makeLabel("phi"), m_vtype);
2482     if (!phi) {
2483         compile_error(m_context, "internal error: failed to generate phi node");
2484         return false;
2485     }
2486     ir_phi_add(phi, ontrue_out,  trueval);
2487     ir_phi_add(phi, onfalse_out, falseval);
2488
2489     m_outr = ir_phi_value(phi);
2490     *out = m_outr;
2491
2492     codegen_output_type(this, *out);
2493
2494     return true;
2495 }
2496
2497 bool ast_loop::codegen(ast_function *func, bool lvalue, ir_value **out)
2498 {
2499     ir_value *dummy      = nullptr;
2500     ir_value *precond    = nullptr;
2501     ir_value *postcond   = nullptr;
2502
2503     /* Since we insert some jumps "late" so we have blocks
2504      * ordered "nicely", we need to keep track of the actual end-blocks
2505      * of expressions to add the jumps to.
2506      */
2507     ir_block *bbody      = nullptr, *end_bbody      = nullptr;
2508     ir_block *bprecond   = nullptr, *end_bprecond   = nullptr;
2509     ir_block *bpostcond  = nullptr, *end_bpostcond  = nullptr;
2510     ir_block *bincrement = nullptr, *end_bincrement = nullptr;
2511     ir_block *bout       = nullptr, *bin            = nullptr;
2512
2513     /* let's at least move the outgoing block to the end */
2514     size_t    bout_id;
2515
2516     /* 'break' and 'continue' need to be able to find the right blocks */
2517     ir_block *bcontinue     = nullptr;
2518     ir_block *bbreak        = nullptr;
2519
2520     ir_block *tmpblock      = nullptr;
2521
2522     (void)lvalue;
2523     (void)out;
2524
2525     if (m_outr) {
2526         compile_error(m_context, "internal error: ast_loop cannot be reused, it bears no result!");
2527         return false;
2528     }
2529     m_outr = (ir_value*)1;
2530
2531     /* NOTE:
2532      * Should we ever need some kind of block ordering, better make this function
2533      * move blocks around than write a block ordering algorithm later... after all
2534      * the ast and ir should work together, not against each other.
2535      */
2536
2537     /* initexpr doesn't get its own block, it's pointless, it could create more blocks
2538      * anyway if for example it contains a ternary.
2539      */
2540     if (m_initexpr)
2541     {
2542         if (!m_initexpr->codegen(func, false, &dummy))
2543             return false;
2544     }
2545
2546     /* Store the block from which we enter this chaos */
2547     bin = func->m_curblock;
2548
2549     /* The pre-loop condition needs its own block since we
2550      * need to be able to jump to the start of that expression.
2551      */
2552     if (m_precond)
2553     {
2554         bprecond = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("pre_loop_cond"));
2555         if (!bprecond)
2556             return false;
2557
2558         /* the pre-loop-condition the least important place to 'continue' at */
2559         bcontinue = bprecond;
2560
2561         /* enter */
2562         func->m_curblock = bprecond;
2563
2564         /* generate */
2565         if (!m_precond->codegen(func, false, &precond))
2566             return false;
2567
2568         end_bprecond = func->m_curblock;
2569     } else {
2570         bprecond = end_bprecond = nullptr;
2571     }
2572
2573     /* Now the next blocks won't be ordered nicely, but we need to
2574      * generate them this early for 'break' and 'continue'.
2575      */
2576     if (m_increment) {
2577         bincrement = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("loop_increment"));
2578         if (!bincrement)
2579             return false;
2580         bcontinue = bincrement; /* increment comes before the pre-loop-condition */
2581     } else {
2582         bincrement = end_bincrement = nullptr;
2583     }
2584
2585     if (m_postcond) {
2586         bpostcond = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("post_loop_cond"));
2587         if (!bpostcond)
2588             return false;
2589         bcontinue = bpostcond; /* postcond comes before the increment */
2590     } else {
2591         bpostcond = end_bpostcond = nullptr;
2592     }
2593
2594     bout_id = func->m_ir_func->m_blocks.size();
2595     bout = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("after_loop"));
2596     if (!bout)
2597         return false;
2598     bbreak = bout;
2599
2600     /* The loop body... */
2601     /* if (m_body) */
2602     {
2603         bbody = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("loop_body"));
2604         if (!bbody)
2605             return false;
2606
2607         /* enter */
2608         func->m_curblock = bbody;
2609
2610         func->m_breakblocks.push_back(bbreak);
2611         if (bcontinue)
2612             func->m_continueblocks.push_back(bcontinue);
2613         else
2614             func->m_continueblocks.push_back(bbody);
2615
2616         /* generate */
2617         if (m_body) {
2618             if (!m_body->codegen(func, false, &dummy))
2619                 return false;
2620         }
2621
2622         end_bbody = func->m_curblock;
2623         func->m_breakblocks.pop_back();
2624         func->m_continueblocks.pop_back();
2625     }
2626
2627     /* post-loop-condition */
2628     if (m_postcond)
2629     {
2630         /* enter */
2631         func->m_curblock = bpostcond;
2632
2633         /* generate */
2634         if (!m_postcond->codegen(func, false, &postcond))
2635             return false;
2636
2637         end_bpostcond = func->m_curblock;
2638     }
2639
2640     /* The incrementor */
2641     if (m_increment)
2642     {
2643         /* enter */
2644         func->m_curblock = bincrement;
2645
2646         /* generate */
2647         if (!m_increment->codegen(func, false, &dummy))
2648             return false;
2649
2650         end_bincrement = func->m_curblock;
2651     }
2652
2653     /* In any case now, we continue from the outgoing block */
2654     func->m_curblock = bout;
2655
2656     /* Now all blocks are in place */
2657     /* From 'bin' we jump to whatever comes first */
2658     if      (bprecond)   tmpblock = bprecond;
2659     else                 tmpblock = bbody;    /* can never be null */
2660
2661     /* DEAD CODE
2662     else if (bpostcond)  tmpblock = bpostcond;
2663     else                 tmpblock = bout;
2664     */
2665
2666     if (!ir_block_create_jump(bin, m_context, tmpblock))
2667         return false;
2668
2669     /* From precond */
2670     if (bprecond)
2671     {
2672         ir_block *ontrue, *onfalse;
2673         ontrue = bbody; /* can never be null */
2674
2675         /* all of this is dead code
2676         else if (bincrement) ontrue = bincrement;
2677         else                 ontrue = bpostcond;
2678         */
2679
2680         onfalse = bout;
2681         if (m_pre_not) {
2682             tmpblock = ontrue;
2683             ontrue   = onfalse;
2684             onfalse  = tmpblock;
2685         }
2686         if (!ir_block_create_if(end_bprecond, m_context, precond, ontrue, onfalse))
2687             return false;
2688     }
2689
2690     /* from body */
2691     if (bbody)
2692     {
2693         if      (bincrement) tmpblock = bincrement;
2694         else if (bpostcond)  tmpblock = bpostcond;
2695         else if (bprecond)   tmpblock = bprecond;
2696         else                 tmpblock = bbody;
2697         if (!end_bbody->m_final && !ir_block_create_jump(end_bbody, m_context, tmpblock))
2698             return false;
2699     }
2700
2701     /* from increment */
2702     if (bincrement)
2703     {
2704         if      (bpostcond)  tmpblock = bpostcond;
2705         else if (bprecond)   tmpblock = bprecond;
2706         else if (bbody)      tmpblock = bbody;
2707         else                 tmpblock = bout;
2708         if (!ir_block_create_jump(end_bincrement, m_context, tmpblock))
2709             return false;
2710     }
2711
2712     /* from postcond */
2713     if (bpostcond)
2714     {
2715         ir_block *ontrue, *onfalse;
2716         if      (bprecond)   ontrue = bprecond;
2717         else                 ontrue = bbody; /* can never be null */
2718
2719         /* all of this is dead code
2720         else if (bincrement) ontrue = bincrement;
2721         else                 ontrue = bpostcond;
2722         */
2723
2724         onfalse = bout;
2725         if (m_post_not) {
2726             tmpblock = ontrue;
2727             ontrue   = onfalse;
2728             onfalse  = tmpblock;
2729         }
2730         if (!ir_block_create_if(end_bpostcond, m_context, postcond, ontrue, onfalse))
2731             return false;
2732     }
2733
2734     /* Move 'bout' to the end */
2735     algo::shiftback(func->m_ir_func->m_blocks.begin() + bout_id,
2736                     func->m_ir_func->m_blocks.end());
2737     // FIXME::DELME::
2738     //func->m_ir_func->m_blocks[bout_id].release(); // it's a vector<std::unique_ptr<>>
2739     //func->m_ir_func->m_blocks.erase(func->m_ir_func->m_blocks.begin() + bout_id);
2740     //func->m_ir_func->m_blocks.emplace_back(bout);
2741
2742     return true;
2743 }
2744
2745 bool ast_breakcont::codegen(ast_function *func, bool lvalue, ir_value **out)
2746 {
2747     ir_block *target;
2748
2749     *out = nullptr;
2750
2751     if (lvalue) {
2752         compile_error(m_context, "break/continue expression is not an l-value");
2753         return false;
2754     }
2755
2756     if (m_outr) {
2757         compile_error(m_context, "internal error: ast_breakcont cannot be reused!");
2758         return false;
2759     }
2760     m_outr = (ir_value*)1;
2761
2762     if (m_is_continue)
2763         target = func->m_continueblocks[func->m_continueblocks.size()-1-m_levels];
2764     else
2765         target = func->m_breakblocks[func->m_breakblocks.size()-1-m_levels];
2766
2767     if (!target) {
2768         compile_error(m_context, "%s is lacking a target block", (m_is_continue ? "continue" : "break"));
2769         return false;
2770     }
2771
2772     if (!ir_block_create_jump(func->m_curblock, m_context, target))
2773         return false;
2774     return true;
2775 }
2776
2777 bool ast_switch::codegen(ast_function *func, bool lvalue, ir_value **out)
2778 {
2779     ast_switch_case *def_case     = nullptr;
2780     ir_block        *def_bfall    = nullptr;
2781     ir_block        *def_bfall_to = nullptr;
2782     bool set_def_bfall_to = false;
2783
2784     ir_value *dummy     = nullptr;
2785     ir_value *irop      = nullptr;
2786     ir_block *bout      = nullptr;
2787     ir_block *bfall     = nullptr;
2788     size_t    bout_id;
2789
2790     char      typestr[1024];
2791     uint16_t  cmpinstr;
2792
2793     if (lvalue) {
2794         compile_error(m_context, "switch expression is not an l-value");
2795         return false;
2796     }
2797
2798     if (m_outr) {
2799         compile_error(m_context, "internal error: ast_switch cannot be reused!");
2800         return false;
2801     }
2802     m_outr = (ir_value*)1;
2803
2804     (void)lvalue;
2805     (void)out;
2806
2807     if (!m_operand->codegen(func, false, &irop))
2808         return false;
2809
2810     if (m_cases.empty())
2811         return true;
2812
2813     cmpinstr = type_eq_instr[irop->m_vtype];
2814     if (cmpinstr >= VINSTR_END) {
2815         ast_type_to_string(m_operand, typestr, sizeof(typestr));
2816         compile_error(m_context, "invalid type to perform a switch on: %s", typestr);
2817         return false;
2818     }
2819
2820     bout_id = func->m_ir_func->m_blocks.size();
2821     bout = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("after_switch"));
2822     if (!bout)
2823         return false;
2824
2825     /* setup the break block */
2826     func->m_breakblocks.push_back(bout);
2827
2828     /* Now create all cases */
2829     for (auto &it : m_cases) {
2830         ir_value *cond, *val;
2831         ir_block *bcase, *bnot;
2832         size_t bnot_id;
2833
2834         ast_switch_case *swcase = &it;
2835
2836         if (swcase->m_value) {
2837             /* A regular case */
2838             /* generate the condition operand */
2839             if (!swcase->m_value->codegen(func, false, &val))
2840                 return false;
2841             /* generate the condition */
2842             cond = ir_block_create_binop(func->m_curblock, m_context, func->makeLabel("switch_eq"), cmpinstr, irop, val);
2843             if (!cond)
2844                 return false;
2845
2846             bcase = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("case"));
2847             bnot_id = func->m_ir_func->m_blocks.size();
2848             bnot = ir_function_create_block(m_context, func->m_ir_func, func->makeLabel("not_case"));
2849             if (!bcase || !bnot)
2850                 return false;
2851             if (set_def_bfall_to) {
2852                 set_def_bfall_to = false;
2853                 def_bfall_to = bcase;
2854             }
2855             if (!ir_block_create_if(func->m_curblock, m_context, cond, bcase, bnot))
2856                 return false;
2857
2858             /* Make the previous case-end fall through */
2859             if (bfall && !bfall->m_final) {
2860                 if (!ir_block_create_jump(bfall, m_context, bcase))
2861                     return false;
2862             }
2863
2864             /* enter the case */
2865             func->m_curblock = bcase;
2866             if (!swcase->m_code->codegen(func, false, &dummy))
2867                 return false;
2868
2869             /* remember this block to fall through from */
2870             bfall = func->m_curblock;
2871
2872             /* enter the else and move it down */
2873             func->m_curblock = bnot;
2874             algo::shiftback(func->m_ir_func->m_blocks.begin() + bnot_id,
2875                             func->m_ir_func->m_blocks.end());
2876             // FIXME::DELME::
2877             //func->m_ir_func->m_blocks[bnot_id].release();
2878             //func->m_ir_func->m_blocks.erase(func->m_ir_func->m_blocks.begin() + bnot_id);
2879             //func->m_ir_func->m_blocks.emplace_back(bnot);
2880         } else {
2881             /* The default case */
2882             /* Remember where to fall through from: */
2883             def_bfall = bfall;
2884             bfall     = nullptr;
2885             /* remember which case it was */
2886             def_case  = swcase;
2887             /* And the next case will be remembered */
2888             set_def_bfall_to = true;
2889         }
2890     }
2891
2892     /* Jump from the last bnot to bout */
2893     if (bfall && !bfall->m_final && !ir_block_create_jump(bfall, m_context, bout)) {
2894         /*
2895         astwarning(bfall->m_context, WARN_???, "missing break after last case");
2896         */
2897         return false;
2898     }
2899
2900     /* If there was a default case, put it down here */
2901     if (def_case) {
2902         ir_block *bcase;
2903
2904         /* No need to create an extra block */
2905         bcase = func->m_curblock;
2906
2907         /* Insert the fallthrough jump */
2908         if (def_bfall && !def_bfall->m_final) {
2909             if (!ir_block_create_jump(def_bfall, m_context, bcase))
2910                 return false;
2911         }
2912
2913         /* Now generate the default code */
2914         if (!def_case->m_code->codegen(func, false, &dummy))
2915             return false;
2916
2917         /* see if we need to fall through */
2918         if (def_bfall_to && !func->m_curblock->m_final)
2919         {
2920             if (!ir_block_create_jump(func->m_curblock, m_context, def_bfall_to))
2921                 return false;
2922         }
2923     }
2924
2925     /* Jump from the last bnot to bout */
2926     if (!func->m_curblock->m_final && !ir_block_create_jump(func->m_curblock, m_context, bout))
2927         return false;
2928     /* enter the outgoing block */
2929     func->m_curblock = bout;
2930
2931     /* restore the break block */
2932     func->m_breakblocks.pop_back();
2933
2934     /* Move 'bout' to the end, it's nicer */
2935     algo::shiftback(func->m_ir_func->m_blocks.begin() + bout_id,
2936                     func->m_ir_func->m_blocks.end());
2937     // FIXME::DELME::
2938     //func->m_ir_func->m_blocks[bout_id].release();
2939     //func->m_ir_func->m_blocks.erase(func->m_ir_func->m_blocks.begin() + bout_id);
2940     //func->m_ir_func->m_blocks.emplace_back(bout);
2941
2942     return true;
2943 }
2944
2945 bool ast_label::codegen(ast_function *func, bool lvalue, ir_value **out)
2946 {
2947     ir_value *dummy;
2948
2949     if (m_undefined) {
2950         compile_error(m_context, "internal error: ast_label never defined");
2951         return false;
2952     }
2953
2954     *out = nullptr;
2955     if (lvalue) {
2956         compile_error(m_context, "internal error: ast_label cannot be an lvalue");
2957         return false;
2958     }
2959
2960     /* simply create a new block and jump to it */
2961     m_irblock = ir_function_create_block(m_context, func->m_ir_func, m_name.c_str());
2962     if (!m_irblock) {
2963         compile_error(m_context, "failed to allocate label block `%s`", m_name);
2964         return false;
2965     }
2966     if (!func->m_curblock->m_final) {
2967         if (!ir_block_create_jump(func->m_curblock, m_context, m_irblock))
2968             return false;
2969     }
2970
2971     /* enter the new block */
2972     func->m_curblock = m_irblock;
2973
2974     /* Generate all the leftover gotos */
2975     for (auto &it : m_gotos) {
2976         if (!it->codegen(func, false, &dummy))
2977             return false;
2978     }
2979
2980     return true;
2981 }
2982
2983 bool ast_goto::codegen(ast_function *func, bool lvalue, ir_value **out)
2984 {
2985     *out = nullptr;
2986     if (lvalue) {
2987         compile_error(m_context, "internal error: ast_goto cannot be an lvalue");
2988         return false;
2989     }
2990
2991     if (m_target->m_irblock) {
2992         if (m_irblock_from) {
2993             /* we already tried once, this is the callback */
2994             m_irblock_from->m_final = false;
2995             if (!ir_block_create_goto(m_irblock_from, m_context, m_target->m_irblock)) {
2996                 compile_error(m_context, "failed to generate goto to `%s`", m_name);
2997                 return false;
2998             }
2999         }
3000         else
3001         {
3002             if (!ir_block_create_goto(func->m_curblock, m_context, m_target->m_irblock)) {
3003                 compile_error(m_context, "failed to generate goto to `%s`", m_name);
3004                 return false;
3005             }
3006         }
3007     }
3008     else
3009     {
3010         /* the target has not yet been created...
3011          * close this block in a sneaky way:
3012          */
3013         func->m_curblock->m_final = true;
3014         m_irblock_from = func->m_curblock;
3015         m_target->registerGoto(this);
3016     }
3017
3018     return true;
3019 }
3020
3021 bool ast_state::codegen(ast_function *func, bool lvalue, ir_value **out)
3022 {
3023     ir_value *frameval, *thinkval;
3024
3025     if (lvalue) {
3026         compile_error(m_context, "not an l-value (state operation)");
3027         return false;
3028     }
3029     if (m_outr) {
3030         compile_error(m_context, "internal error: ast_state cannot be reused!");
3031         return false;
3032     }
3033     *out = nullptr;
3034
3035     if (!m_framenum->codegen(func, false, &frameval))
3036         return false;
3037     if (!frameval)
3038         return false;
3039
3040     if (!m_nextthink->codegen(func, false, &thinkval))
3041         return false;
3042     if (!frameval)
3043         return false;
3044
3045     if (!ir_block_create_state_op(func->m_curblock, m_context, frameval, thinkval)) {
3046         compile_error(m_context, "failed to create STATE instruction");
3047         return false;
3048     }
3049
3050     m_outr = (ir_value*)1;
3051     return true;
3052 }
3053
3054 bool ast_call::codegen(ast_function *func, bool lvalue, ir_value **out)
3055 {
3056     std::vector<ir_value*> params;
3057     ir_instr *callinstr;
3058
3059     ir_value *funval = nullptr;
3060
3061     /* return values are never lvalues */
3062     if (lvalue) {
3063         compile_error(m_context, "not an l-value (function call)");
3064         return false;
3065     }
3066
3067     if (m_outr) {
3068         *out = m_outr;
3069         return true;
3070     }
3071
3072     if (!m_func->codegen(func, false, &funval))
3073         return false;
3074     if (!funval)
3075         return false;
3076
3077     /* parameters */
3078     for (auto &it : m_params) {
3079         ir_value *param;
3080         if (!it->codegen(func, false, &param))
3081             return false;
3082         if (!param)
3083             return false;
3084         params.push_back(param);
3085     }
3086
3087     /* varargs counter */
3088     if (m_va_count) {
3089         ir_value   *va_count;
3090         ir_builder *builder = func->m_curblock->m_owner->m_owner;
3091         if (!m_va_count->codegen(func, false, &va_count))
3092             return false;
3093         if (!ir_block_create_store_op(func->m_curblock, m_context, INSTR_STORE_F,
3094                                       builder->get_va_count(), va_count))
3095         {
3096             return false;
3097         }
3098     }
3099
3100     callinstr = ir_block_create_call(func->m_curblock, m_context,
3101                                      func->makeLabel("call"),
3102                                      funval, !!(m_func->m_flags & AST_FLAG_NORETURN));
3103     if (!callinstr)
3104         return false;
3105
3106     for (auto &it : params)
3107         ir_call_param(callinstr, it);
3108
3109     *out = ir_call_value(callinstr);
3110     m_outr = *out;
3111
3112     codegen_output_type(this, *out);
3113
3114     return true;
3115 }