6 typedef uint16_t ast_flag_t;
8 /* Note: I will not be using a _t suffix for the
9 * "main" ast node types for now.
13 struct ast_expression;
28 struct ast_array_index;
37 AST_FLAG_VARIADIC = 1 << 0,
38 AST_FLAG_NORETURN = 1 << 1,
39 AST_FLAG_INLINE = 1 << 2,
40 AST_FLAG_INITIALIZED = 1 << 3,
41 AST_FLAG_DEPRECATED = 1 << 4,
42 AST_FLAG_INCLUDE_DEF = 1 << 5,
43 AST_FLAG_IS_VARARG = 1 << 6,
44 AST_FLAG_ALIAS = 1 << 7,
45 AST_FLAG_ERASEABLE = 1 << 8,
46 AST_FLAG_ACCUMULATE = 1 << 9,
48 /* An array declared as []
49 * so that the size is taken from the initializer
51 AST_FLAG_ARRAY_INIT = 1 << 10,
53 AST_FLAG_FINAL_DECL = 1 << 11,
55 /* Several coverage options
56 * AST_FLAG_COVERAGE means there was an explicit [[coverage]] attribute,
57 * which will overwrite the default set via the commandline switches.
58 * BLOCK_COVERAGE inserts coverage() calls into every basic block.
59 * In the future there might be more options like tracking variable access
60 * by creating get/set wrapper functions.
62 AST_FLAG_COVERAGE = 1 << 12,
63 AST_FLAG_BLOCK_COVERAGE = 1 << 13,
66 AST_FLAG_TYPE_MASK = (AST_FLAG_VARIADIC | AST_FLAG_NORETURN),
67 AST_FLAG_COVERAGE_MASK = (AST_FLAG_BLOCK_COVERAGE)
71 TYPE_ast_node, /* 0 */
72 TYPE_ast_expression, /* 1 */
73 TYPE_ast_value, /* 2 */
74 TYPE_ast_function, /* 3 */
75 TYPE_ast_block, /* 4 */
76 TYPE_ast_binary, /* 5 */
77 TYPE_ast_store, /* 6 */
78 TYPE_ast_binstore, /* 7 */
79 TYPE_ast_entfield, /* 8 */
80 TYPE_ast_ifthen, /* 9 */
81 TYPE_ast_ternary, /* 10 */
82 TYPE_ast_loop, /* 11 */
83 TYPE_ast_call, /* 12 */
84 TYPE_ast_unary, /* 13 */
85 TYPE_ast_return, /* 14 */
86 TYPE_ast_member, /* 15 */
87 TYPE_ast_array_index, /* 16 */
88 TYPE_ast_breakcont, /* 17 */
89 TYPE_ast_switch, /* 18 */
90 TYPE_ast_label, /* 19 */
91 TYPE_ast_goto, /* 20 */
92 TYPE_ast_argpipe, /* 21 */
93 TYPE_ast_state /* 22 */
96 #define ast_istype(x, t) ( (x)->m_node_type == (TYPE_##t) )
101 ast_node(lex_ctx_t, int nodetype);
105 /* I don't feel comfortable using keywords like 'delete' as names... */
107 /* keep_node: if a node contains this node, 'keep_node'
108 * prevents its dtor from destroying this node as well.
113 void propagateSideEffects(const ast_node *other);
116 #define ast_unref(x) do \
118 if (! (x)->m_keep_node ) { \
123 enum class ast_copy_type_t { value };
124 static const ast_copy_type_t ast_copy_type = ast_copy_type_t::value;
126 /* TODO: the codegen function should take an output-type parameter
127 * indicating whether a variable, type, label etc. is expected, and
129 * Then later an ast_ident could have a codegen using this to figure
130 * out what to look for.
131 * eg. in code which uses a not-yet defined variable, the expression
132 * would take an ast_ident, and the codegen would be called with
133 * type `expression`, so the ast_ident's codegen would search for
134 * variables through the environment (or functions, constants...).
136 struct ast_expression : ast_node {
137 ast_expression() = delete;
138 ast_expression(lex_ctx_t ctx, int nodetype, qc_type vtype);
139 ast_expression(lex_ctx_t ctx, int nodetype);
142 ast_expression(ast_copy_type_t, const ast_expression&);
143 ast_expression(ast_copy_type_t, lex_ctx_t ctx, const ast_expression&);
144 ast_expression(ast_copy_type_t, int nodetype, const ast_expression&);
145 ast_expression(ast_copy_type_t, int nodetype, lex_ctx_t ctx, const ast_expression&);
147 static ast_expression *shallowType(lex_ctx_t ctx, qc_type vtype);
149 bool compareType(const ast_expression &other) const;
150 void adoptType(const ast_expression &other);
152 qc_type m_vtype = TYPE_VOID;
153 ast_expression *m_next = nullptr;
154 /* arrays get a member-count */
156 std::vector<std::unique_ptr<ast_value>> m_type_params;
158 ast_flag_t m_flags = 0;
159 /* void foo(string...) gets varparam set as a restriction
160 * for variadic parameters
162 ast_expression *m_varparam = nullptr;
163 /* The codegen functions should store their output values
164 * so we can call it multiple times without re-evaluating.
165 * Store lvalue and rvalue seperately though. So that
166 * ast_entfield for example can generate both if required.
168 ir_value *m_outl = nullptr;
169 ir_value *m_outr = nullptr;
171 virtual bool codegen(ast_function *current, bool lvalue, ir_value **out);
176 * Types are also values, both have a type and a name.
177 * especially considering possible constructs like typedefs.
179 * is like creating a 'float foo', foo serving as the type's name.
181 union basic_value_t {
191 struct ast_value : ast_expression
193 ast_value() = delete;
194 ast_value(lex_ctx_t ctx, const std::string &name, qc_type qctype);
197 ast_value(ast_copy_type_t, const ast_expression&, const std::string&);
198 ast_value(ast_copy_type_t, const ast_value&);
199 ast_value(ast_copy_type_t, const ast_value&, const std::string&);
201 bool codegen(ast_function *current, bool lvalue, ir_value **out) override;
203 void addParam(ast_value*);
205 bool generateGlobal(ir_builder*, bool isfield);
206 bool generateLocal(ir_function*, bool param);
207 bool generateAccessors(ir_builder*);
212 const char *m_argcounter = nullptr;
214 int m_cvq = CV_NONE; /* const/var qualifier */
215 bool m_isfield = false; /* this declares a field */
216 bool m_isimm = false; /* an immediate, not just const */
217 bool m_hasvalue = false;
218 bool m_inexact = false; /* inexact coming from folded expression */
219 basic_value_t m_constval;
220 /* for TYPE_ARRAY we have an optional vector
221 * of constants when an initializer list
224 std::vector<basic_value_t> m_initlist;
226 /* usecount for the parser */
229 ir_value *m_ir_v = nullptr;
230 std::vector<ir_value*> m_ir_values;
231 size_t m_ir_value_count = 0;
233 /* ONLY for arrays in progs version up to 6 */
234 ast_value *m_setter = nullptr;
235 ast_value *m_getter = nullptr;
237 bool m_intrinsic = false; /* true if associated with intrinsic */
240 bool generateGlobalFunction(ir_builder*);
241 bool generateGlobalField(ir_builder*);
242 ir_value *prepareGlobalArray(ir_builder*);
243 bool setGlobalArray();
244 bool checkArray(const ast_value &array) const;
247 void ast_type_to_string(const ast_expression *e, char *buf, size_t bufsize);
249 enum ast_binary_ref {
251 AST_REF_LEFT = 1 << 1,
252 AST_REF_RIGHT = 1 << 2,
253 AST_REF_ALL = (AST_REF_LEFT | AST_REF_RIGHT)
259 * A value-returning binary expression.
261 struct ast_binary : ast_expression
263 ast_binary() = delete;
264 ast_binary(lex_ctx_t ctx, int op, ast_expression *l, ast_expression *r);
267 bool codegen(ast_function *current, bool lvalue, ir_value **out) override;
270 ast_expression *m_left;
271 ast_expression *m_right;
272 ast_binary_ref m_refs;
278 * An assignment including a binary expression with the source as left operand.
279 * Eg. a += b; is a binstore { INSTR_STORE, INSTR_ADD, a, b }
281 struct ast_binstore : ast_expression
283 ast_binstore() = delete;
284 ast_binstore(lex_ctx_t ctx, int storeop, int mathop, ast_expression *l, ast_expression *r);
287 bool codegen(ast_function *current, bool lvalue, ir_value **out) override;
291 ast_expression *m_dest;
292 ast_expression *m_source;
293 /* for &~= which uses the destination in a binary in source we can use this */
299 * Regular unary expressions: not,neg
301 struct ast_unary : ast_expression
303 ast_unary() = delete;
306 static ast_unary* make(lex_ctx_t ctx, int op, ast_expression *expr);
308 bool codegen(ast_function *current, bool lvalue, ir_value **out) override;
311 ast_expression *m_operand;
314 ast_unary(lex_ctx_t ctx, int op, ast_expression *expr);
319 * Make sure 'return' only happens at the end of a block, otherwise the IR
320 * will refuse to create further instructions.
321 * This should be honored by the parser.
323 struct ast_return : ast_expression
325 ast_return() = delete;
326 ast_return(lex_ctx_t ctx, ast_expression *expr);
329 bool codegen(ast_function *current, bool lvalue, ir_value **out) override;
331 ast_expression *m_operand;
336 * This must do 2 things:
337 * -) Provide a way to fetch an entity field value. (Rvalue)
338 * -) Provide a pointer to an entity field. (Lvalue)
340 * In original QC, there's only a STORE via pointer, but
341 * no LOAD via pointer.
342 * So we must know beforehand if we are going to read or assign
344 * For this we will have to extend the codegen() functions with
345 * a flag saying whether or not we need an L or an R-value.
347 struct ast_entfield : ast_expression
349 ast_entfield() = delete;
350 ast_entfield(lex_ctx_t ctx, ast_expression *entity, ast_expression *field);
351 ast_entfield(lex_ctx_t ctx, ast_expression *entity, ast_expression *field, const ast_expression *outtype);
354 bool codegen(ast_function *current, bool lvalue, ir_value **out) override;
356 // The entity can come from an expression of course.
357 ast_expression *m_entity;
358 // As can the field, it just must result in a value of TYPE_FIELD
359 ast_expression *m_field;
364 * For now used for vectors. If we get structs or unions
365 * we can have them handled here as well.
367 struct ast_member : ast_expression
369 static ast_member *make(lex_ctx_t ctx, ast_expression *owner, unsigned int field, const std::string &name);
372 bool codegen(ast_function *current, bool lvalue, ir_value **out) override;
374 ast_expression *m_owner;
375 unsigned int m_field;
380 ast_member() = delete;
381 ast_member(lex_ctx_t ctx, ast_expression *owner, unsigned int field, const std::string &name);
384 /* Array index access:
386 * QC forces us to take special action on arrays:
387 * an ast_store on an ast_array_index must not codegen the index,
388 * but call its setter - unless we have an instruction set which supports
390 * Any other array index access will be codegened to a call to the getter.
391 * In any case, accessing an element via a compiletime-constant index will
392 * result in quick access to that variable.
394 struct ast_array_index : ast_expression
396 static ast_array_index* make(lex_ctx_t ctx, ast_expression *array, ast_expression *index);
399 bool codegen(ast_function *current, bool lvalue, ir_value **out) override;
401 ast_expression *m_array;
402 ast_expression *m_index;
404 ast_array_index() = delete;
405 ast_array_index(lex_ctx_t ctx, ast_expression *array, ast_expression *index);
410 * copy all varargs starting from a specific index
412 struct ast_argpipe : ast_expression
414 ast_argpipe() = delete;
415 ast_argpipe(lex_ctx_t ctx, ast_expression *index);
417 bool codegen(ast_function *current, bool lvalue, ir_value **out) override;
420 ast_expression *m_index;
425 * Stores left<-right and returns left.
426 * Specialized binary expression node
428 struct ast_store : ast_expression
430 ast_store() = delete;
431 ast_store(lex_ctx_t ctx, int op, ast_expression *d, ast_expression *s);
434 bool codegen(ast_function *current, bool lvalue, ir_value **out) override;
437 ast_expression *m_dest;
438 ast_expression *m_source;
443 * A general 'if then else' statement, either side can be nullptr and will
444 * thus be omitted. It is an error for *both* cases to be nullptr at once.
446 * During its 'codegen' it'll be changing the ast_function's block.
448 * An if is also an "expression". Its codegen will put nullptr into the
449 * output field though. For ternary expressions an ast_ternary will be
452 struct ast_ifthen : ast_expression
454 ast_ifthen() = delete;
455 ast_ifthen(lex_ctx_t ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse);
458 bool codegen(ast_function *current, bool lvalue, ir_value **out) override;
460 ast_expression *m_cond;
461 /* It's all just 'expressions', since an ast_block is one too. */
462 ast_expression *m_on_true;
463 ast_expression *m_on_false;
466 /* Ternary expressions...
468 * Contrary to 'if-then-else' nodes, ternary expressions actually
469 * return a value, otherwise they behave the very same way.
470 * The difference in 'codegen' is that it'll return the value of
473 * The other difference is that in an ast_ternary, NEITHER side
474 * must be nullptr, there's ALWAYS an else branch.
476 * This is the only ast_node beside ast_value which contains
477 * an ir_value. Theoretically we don't need to remember it though.
479 struct ast_ternary : ast_expression
481 ast_ternary() = delete;
482 ast_ternary(lex_ctx_t ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse);
485 bool codegen(ast_function *current, bool lvalue, ir_value **out) override;
487 ast_expression *m_cond;
488 /* It's all just 'expressions', since an ast_block is one too. */
489 ast_expression *m_on_true;
490 ast_expression *m_on_false;
493 /* A general loop node
495 * For convenience it contains 4 parts:
496 * -) (ini) = initializing expression
497 * -) (pre) = pre-loop condition
498 * -) (pst) = post-loop condition
499 * -) (inc) = "increment" expression
500 * The following is a psudo-representation of this loop
501 * note that '=>' bears the logical meaning of "implies".
502 * (a => b) equals (!a || b)
505 while (has_pre => {pre})
509 continue: // a 'continue' will jump here
510 if (has_pst => {pst})
516 struct ast_loop : ast_expression
519 ast_loop(lex_ctx_t ctx,
520 ast_expression *initexpr,
521 ast_expression *precond, bool pre_not,
522 ast_expression *postcond, bool post_not,
523 ast_expression *increment,
524 ast_expression *body);
527 bool codegen(ast_function *current, bool lvalue, ir_value **out) override;
529 ast_expression *m_initexpr;
530 ast_expression *m_precond;
531 ast_expression *m_postcond;
532 ast_expression *m_increment;
533 ast_expression *m_body;
534 /* For now we allow a seperate flag on whether or not the condition
535 * is supposed to be true or false.
536 * That way, the parser can generate a 'while not(!x)' for `while(x)`
537 * if desired, which is useful for the new -f{true,false}-empty-strings
546 struct ast_breakcont : ast_expression
548 ast_breakcont() = delete;
549 ast_breakcont(lex_ctx_t ctx, bool iscont, unsigned int levels);
552 bool codegen(ast_function *current, bool lvalue, ir_value **out) override;
556 unsigned int m_levels;
561 * A few notes about this: with the original QCVM, no real optimization
562 * is possible. The SWITCH instruction set isn't really helping a lot, since
563 * it only collapes the EQ and IF instructions into one.
564 * Note: Declaring local variables inside caseblocks is normal.
565 * Since we don't have to deal with a stack there's no unnatural behaviour to
566 * be expected from it.
569 struct ast_switch_case {
570 ast_expression *m_value; /* #20 will replace this */
571 ast_expression *m_code;
574 struct ast_switch : ast_expression
576 ast_switch() = delete;
577 ast_switch(lex_ctx_t ctx, ast_expression *op);
580 bool codegen(ast_function *current, bool lvalue, ir_value **out) override;
582 ast_expression *m_operand;
583 std::vector<ast_switch_case> m_cases;
589 * Introduce a label which can be used together with 'goto'
591 struct ast_label : ast_expression
593 ast_label() = delete;
594 ast_label(lex_ctx_t ctx, const std::string &name, bool undefined);
597 bool codegen(ast_function *current, bool lvalue, ir_value **out) override;
601 std::vector<ast_goto*> m_gotos;
603 /* means it has not yet been defined */
607 void registerGoto(ast_goto*);
608 friend struct ast_goto;
613 * Go to a label, the label node is filled in at a later point!
615 struct ast_goto : ast_expression
618 ast_goto(lex_ctx_t ctx, const std::string &name);
621 bool codegen(ast_function *current, bool lvalue, ir_value **out) override;
623 void setLabel(ast_label*);
627 ir_block *m_irblock_from;
632 * For frame/think state updates: void foo() [framenum, nextthink] {}
634 struct ast_state : ast_expression
636 ast_state() = delete;
637 ast_state(lex_ctx_t ctx, ast_expression *frame, ast_expression *think);
640 bool codegen(ast_function *current, bool lvalue, ir_value **out) override;
642 ast_expression *m_framenum;
643 ast_expression *m_nextthink;
648 * Contains an ast_expression as target, rather than an ast_function/value.
649 * Since it's how QC works, every ast_function has an ast_value
650 * associated anyway - in other words, the VM contains function
651 * pointers for every function anyway. Thus, this node will call
653 * Additionally it contains a list of ast_expressions as parameters.
654 * Since calls can return values, an ast_call is also an ast_expression.
656 struct ast_call : ast_expression
659 static ast_call *make(lex_ctx_t, ast_expression*);
662 bool codegen(ast_function *current, bool lvalue, ir_value **out) override;
664 bool checkTypes(ast_expression *this_func_va_type) const;
666 ast_expression *m_func;
667 std::vector<ast_expression *> m_params;
668 ast_expression *m_va_count;
671 ast_call(lex_ctx_t ctx, ast_expression *funcexpr);
672 bool checkVararg(ast_expression *va_type, ast_expression *exp_type) const;
678 struct ast_block : ast_expression
680 ast_block() = delete;
681 ast_block(lex_ctx_t ctx);
684 bool codegen(ast_function *current, bool lvalue, ir_value **out) override;
686 std::vector<ast_value*> m_locals;
687 std::vector<ast_expression*> m_exprs;
688 std::vector<ast_expression*> m_collect;
690 void setType(const ast_expression &from);
691 bool GMQCC_WARN addExpr(ast_expression*);
692 void collect(ast_expression*);
697 * Contains a list of blocks... at least in theory.
698 * Usually there's just the main block, other blocks are inside that.
700 * Technically, functions don't need to be an AST node, since we have
701 * neither functions inside functions, nor lambdas, and function
702 * pointers could just work with a name. However, this way could be
703 * more flexible, and adds no real complexity.
705 * The destructor will NOT delete the underlying ast_value
708 struct ast_function : ast_node
710 ast_function() = delete;
711 static ast_function *make(lex_ctx_t ctx, const std::string &name, ast_value *vtype);
714 const char* makeLabel(const char *prefix);
715 virtual bool generateFunction(ir_builder*);
717 ast_value *m_function_type = nullptr;
722 /* list of used-up names for statics without the count suffix */
723 std::vector<std::string> m_static_names;
724 /* number of static variables, by convention this includes the
725 * ones without the count-suffix - remember this when dealing
726 * with savegames. uint instead of size_t as %zu in printf is
727 * C99, so no windows support. */
728 unsigned int m_static_count = 0;
730 ir_function *m_ir_func = nullptr;
731 ir_block *m_curblock = nullptr;
732 std::vector<ir_block*> m_breakblocks;
733 std::vector<ir_block*> m_continueblocks;
735 size_t m_labelcount = 0;
736 /* in order for thread safety - for the optional
737 * channel abesed multithreading... keeping a buffer
738 * here to use in ast_function_label.
740 std::vector<std::unique_ptr<ast_block>> m_blocks;
741 std::unique_ptr<ast_value> m_varargs;
742 std::unique_ptr<ast_value> m_argc;
743 ast_value *m_fixedparams = nullptr; // these use unref()
744 ast_value *m_return_value = nullptr;
747 ast_function(lex_ctx_t ctx, const std::string &name, ast_value *vtype);
753 * If the condition creates a situation where this becomes -1 size it means there are
754 * more AST_FLAGs than the type ast_flag_t is capable of holding. So either eliminate
755 * the AST flag count or change the ast_flag_t typedef to a type large enough to accomodate
758 typedef int static_assert_is_ast_flag_safe [((AST_FLAG_LAST) <= (ast_flag_t)(-1)) ? 1 : -1];