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) ( ((ast_node*)x)->node_type == (TYPE_##t) )
97 #define ast_ctx(node) (((ast_node*)(node))->context)
98 #define ast_side_effects(node) (((ast_node*)(node))->side_effects)
100 /* Node interface with common components
102 typedef void ast_node_delete(ast_node*);
107 /* I don't feel comfortable using keywords like 'delete' as names... */
108 ast_node_delete *destroy;
110 /* keep_node: if a node contains this node, 'keep_node'
111 * prevents its dtor from destroying this node as well.
117 #define ast_delete(x) (*( ((ast_node*)(x))->destroy ))((ast_node*)(x))
118 #define ast_unref(x) do \
120 if (! (((ast_node*)(x))->keep_node) ) { \
125 /* Expression interface
127 * Any expression or block returns an ir_value, and needs
128 * to know the current function.
130 typedef bool ast_expression_codegen(ast_expression*,
134 /* TODO: the codegen function should take an output-type parameter
135 * indicating whether a variable, type, label etc. is expected, and
137 * Then later an ast_ident could have a codegen using this to figure
138 * out what to look for.
139 * eg. in code which uses a not-yet defined variable, the expression
140 * would take an ast_ident, and the codegen would be called with
141 * type `expression`, so the ast_ident's codegen would search for
142 * variables through the environment (or functions, constants...).
144 struct ast_expression : ast_node {
147 ast_expression_codegen *codegen;
149 ast_expression *next;
150 /* arrays get a member-count */
152 std::vector<ast_value*> params;
155 /* void foo(string...) gets varparam set as a restriction
156 * for variadic parameters
158 ast_expression *varparam;
159 /* The codegen functions should store their output values
160 * so we can call it multiple times without re-evaluating.
161 * Store lvalue and rvalue seperately though. So that
162 * ast_entfield for example can generate both if required.
170 * Types are also values, both have a type and a name.
171 * especially considering possible constructs like typedefs.
173 * is like creating a 'float foo', foo serving as the type's name.
175 union basic_value_t {
187 ast_expression expression;
192 const char *argcounter;
194 int cvq; /* const/var qualifier */
195 bool isfield; /* this declares a field */
196 bool isimm; /* an immediate, not just const */
198 bool inexact; /* inexact coming from folded expression */
199 basic_value_t constval;
200 /* for TYPE_ARRAY we have an optional vector
201 * of constants when an initializer list
204 std::vector<basic_value_t> initlist;
206 /* usecount for the parser */
210 ir_value **ir_values;
211 size_t ir_value_count;
213 /* ONLY for arrays in progs version up to 6 */
218 bool intrinsic; /* true if associated with intrinsic */
221 ast_value* ast_value_new(lex_ctx_t ctx, const char *name, int qctype);
222 ast_value* ast_value_copy(const ast_value *self);
223 /* This will NOT delete an underlying ast_function */
224 void ast_value_delete(ast_value*);
226 bool ast_value_set_name(ast_value*, const char *name);
229 bool ast_value_codegen(ast_value*, ast_function*, bool lvalue, ir_value**);
230 bool ast_local_codegen(ast_value *self, ir_function *func, bool isparam);
233 bool ast_global_codegen(ast_value *self, ir_builder *ir, bool isfield);
235 void ast_value_params_add(ast_value*, ast_value*);
237 bool ast_compare_type(ast_expression *a, ast_expression *b);
238 ast_expression* ast_type_copy(lex_ctx_t ctx, const ast_expression *ex);
239 #define ast_type_adopt(a, b) ast_type_adopt_impl((ast_expression*)(a), (ast_expression*)(b))
240 void ast_type_adopt_impl(ast_expression *self, const ast_expression *other);
241 void ast_type_to_string(ast_expression *e, char *buf, size_t bufsize);
243 enum ast_binary_ref {
245 AST_REF_LEFT = 1 << 1,
246 AST_REF_RIGHT = 1 << 2,
247 AST_REF_ALL = (AST_REF_LEFT | AST_REF_RIGHT)
253 * A value-returning binary expression.
257 ast_expression expression;
259 ast_expression *left;
260 ast_expression *right;
264 ast_binary* ast_binary_new(lex_ctx_t ctx,
266 ast_expression *left,
267 ast_expression *right);
271 * An assignment including a binary expression with the source as left operand.
272 * Eg. a += b; is a binstore { INSTR_STORE, INSTR_ADD, a, b }
276 ast_expression expression;
279 ast_expression *dest;
280 ast_expression *source;
281 /* for &~= which uses the destination in a binary in source we can use this */
284 ast_binstore* ast_binstore_new(lex_ctx_t ctx,
287 ast_expression *left,
288 ast_expression *right);
292 * Regular unary expressions: not,neg
296 ast_expression expression;
298 ast_expression *operand;
300 ast_unary* ast_unary_new(lex_ctx_t ctx,
302 ast_expression *expr);
306 * Make sure 'return' only happens at the end of a block, otherwise the IR
307 * will refuse to create further instructions.
308 * This should be honored by the parser.
312 ast_expression expression;
313 ast_expression *operand;
315 ast_return* ast_return_new(lex_ctx_t ctx,
316 ast_expression *expr);
320 * This must do 2 things:
321 * -) Provide a way to fetch an entity field value. (Rvalue)
322 * -) Provide a pointer to an entity field. (Lvalue)
324 * In original QC, there's only a STORE via pointer, but
325 * no LOAD via pointer.
326 * So we must know beforehand if we are going to read or assign
328 * For this we will have to extend the codegen() functions with
329 * a flag saying whether or not we need an L or an R-value.
333 ast_expression expression;
334 /* The entity can come from an expression of course. */
335 ast_expression *entity;
336 /* As can the field, it just must result in a value of TYPE_FIELD */
337 ast_expression *field;
339 ast_entfield* ast_entfield_new(lex_ctx_t ctx, ast_expression *entity, ast_expression *field);
340 ast_entfield* ast_entfield_new_force(lex_ctx_t ctx, ast_expression *entity, ast_expression *field, const ast_expression *outtype);
344 * For now used for vectors. If we get structs or unions
345 * we can have them handled here as well.
349 ast_expression expression;
350 ast_expression *owner;
355 ast_member* ast_member_new(lex_ctx_t ctx, ast_expression *owner, unsigned int field, const char *name);
356 void ast_member_delete(ast_member*);
357 bool ast_member_set_name(ast_member*, const char *name);
360 /* Array index access:
362 * QC forces us to take special action on arrays:
363 * an ast_store on an ast_array_index must not codegen the index,
364 * but call its setter - unless we have an instruction set which supports
366 * Any other array index access will be codegened to a call to the getter.
367 * In any case, accessing an element via a compiletime-constant index will
368 * result in quick access to that variable.
370 struct ast_array_index
372 ast_expression expression;
373 ast_expression *array;
374 ast_expression *index;
376 ast_array_index* ast_array_index_new(lex_ctx_t ctx, ast_expression *array, ast_expression *index);
380 * copy all varargs starting from a specific index
384 ast_expression expression;
385 ast_expression *index;
387 ast_argpipe* ast_argpipe_new(lex_ctx_t ctx, ast_expression *index);
391 * Stores left<-right and returns left.
392 * Specialized binary expression node
396 ast_expression expression;
398 ast_expression *dest;
399 ast_expression *source;
401 ast_store* ast_store_new(lex_ctx_t ctx, int op,
402 ast_expression *d, ast_expression *s);
406 * A general 'if then else' statement, either side can be nullptr and will
407 * thus be omitted. It is an error for *both* cases to be nullptr at once.
409 * During its 'codegen' it'll be changing the ast_function's block.
411 * An if is also an "expression". Its codegen will put nullptr into the
412 * output field though. For ternary expressions an ast_ternary will be
417 ast_expression expression;
418 ast_expression *cond;
419 /* It's all just 'expressions', since an ast_block is one too. */
420 ast_expression *on_true;
421 ast_expression *on_false;
423 ast_ifthen* ast_ifthen_new(lex_ctx_t ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse);
425 /* Ternary expressions...
427 * Contrary to 'if-then-else' nodes, ternary expressions actually
428 * return a value, otherwise they behave the very same way.
429 * The difference in 'codegen' is that it'll return the value of
432 * The other difference is that in an ast_ternary, NEITHER side
433 * must be nullptr, there's ALWAYS an else branch.
435 * This is the only ast_node beside ast_value which contains
436 * an ir_value. Theoretically we don't need to remember it though.
440 ast_expression expression;
441 ast_expression *cond;
442 /* It's all just 'expressions', since an ast_block is one too. */
443 ast_expression *on_true;
444 ast_expression *on_false;
446 ast_ternary* ast_ternary_new(lex_ctx_t ctx, ast_expression *cond, ast_expression *ontrue, ast_expression *onfalse);
448 /* A general loop node
450 * For convenience it contains 4 parts:
451 * -) (ini) = initializing expression
452 * -) (pre) = pre-loop condition
453 * -) (pst) = post-loop condition
454 * -) (inc) = "increment" expression
455 * The following is a psudo-representation of this loop
456 * note that '=>' bears the logical meaning of "implies".
457 * (a => b) equals (!a || b)
460 while (has_pre => {pre})
464 continue: // a 'continue' will jump here
465 if (has_pst => {pst})
473 ast_expression expression;
474 ast_expression *initexpr;
475 ast_expression *precond;
476 ast_expression *postcond;
477 ast_expression *increment;
478 ast_expression *body;
479 /* For now we allow a seperate flag on whether or not the condition
480 * is supposed to be true or false.
481 * That way, the parser can generate a 'while not(!x)' for `while(x)`
482 * if desired, which is useful for the new -f{true,false}-empty-strings
488 ast_loop* ast_loop_new(lex_ctx_t ctx,
489 ast_expression *initexpr,
490 ast_expression *precond, bool pre_not,
491 ast_expression *postcond, bool post_not,
492 ast_expression *increment,
493 ast_expression *body);
499 ast_expression expression;
503 ast_breakcont* ast_breakcont_new(lex_ctx_t ctx, bool iscont, unsigned int levels);
507 * A few notes about this: with the original QCVM, no real optimization
508 * is possible. The SWITCH instruction set isn't really helping a lot, since
509 * it only collapes the EQ and IF instructions into one.
510 * Note: Declaring local variables inside caseblocks is normal.
511 * Since we don't have to deal with a stack there's no unnatural behaviour to
512 * be expected from it.
515 struct ast_switch_case {
516 ast_expression *value; /* #20 will replace this */
517 ast_expression *code;
522 ast_expression expression;
523 ast_expression *operand;
524 std::vector<ast_switch_case> cases;
527 ast_switch* ast_switch_new(lex_ctx_t ctx, ast_expression *op);
531 * Introduce a label which can be used together with 'goto'
535 ast_expression expression;
538 std::vector<ast_goto*> gotos;
540 /* means it has not yet been defined */
544 ast_label* ast_label_new(lex_ctx_t ctx, const char *name, bool undefined);
548 * Go to a label, the label node is filled in at a later point!
552 ast_expression expression;
555 ir_block *irblock_from;
558 ast_goto* ast_goto_new(lex_ctx_t ctx, const char *name);
559 void ast_goto_set_label(ast_goto*, ast_label*);
563 * For frame/think state updates: void foo() [framenum, nextthink] {}
567 ast_expression expression;
568 ast_expression *framenum;
569 ast_expression *nextthink;
571 ast_state* ast_state_new(lex_ctx_t ctx, ast_expression *frame, ast_expression *think);
572 void ast_state_delete(ast_state*);
576 * Contains an ast_expression as target, rather than an ast_function/value.
577 * Since it's how QC works, every ast_function has an ast_value
578 * associated anyway - in other words, the VM contains function
579 * pointers for every function anyway. Thus, this node will call
581 * Additionally it contains a list of ast_expressions as parameters.
582 * Since calls can return values, an ast_call is also an ast_expression.
586 ast_expression expression;
587 ast_expression *func;
588 std::vector<ast_expression *> params;
589 ast_expression *va_count;
591 ast_call* ast_call_new(lex_ctx_t ctx,
592 ast_expression *funcexpr);
593 bool ast_call_check_types(ast_call*, ast_expression *this_func_va_type);
600 ast_expression expression;
602 std::vector<ast_value*> locals;
603 std::vector<ast_expression*> exprs;
604 std::vector<ast_expression*> collect;
606 ast_block* ast_block_new(lex_ctx_t ctx);
607 void ast_block_delete(ast_block*);
608 void ast_block_set_type(ast_block*, ast_expression *from);
609 void ast_block_collect(ast_block*, ast_expression*);
611 bool GMQCC_WARN ast_block_add_expr(ast_block*, ast_expression*);
615 * Contains a list of blocks... at least in theory.
616 * Usually there's just the main block, other blocks are inside that.
618 * Technically, functions don't need to be an AST node, since we have
619 * neither functions inside functions, nor lambdas, and function
620 * pointers could just work with a name. However, this way could be
621 * more flexible, and adds no real complexity.
632 /* list of used-up names for statics without the count suffix */
633 std::vector<char*> static_names;
634 /* number of static variables, by convention this includes the
635 * ones without the count-suffix - remember this when dealing
636 * with savegames. uint instead of size_t as %zu in printf is
637 * C99, so no windows support. */
638 unsigned int static_count;
640 ir_function *ir_func;
642 std::vector<ir_block*> breakblocks;
643 std::vector<ir_block*> continueblocks;
646 /* in order for thread safety - for the optional
647 * channel abesed multithreading... keeping a buffer
648 * here to use in ast_function_label.
651 std::vector<ast_block*> blocks;
654 ast_value *fixedparams;
655 ast_value *return_value;
657 ast_function* ast_function_new(lex_ctx_t ctx, const char *name, ast_value *vtype);
658 /* This will NOT delete the underlying ast_value */
659 void ast_function_delete(ast_function*);
660 /* For "optimized" builds this can just keep returning "foo"...
663 const char* ast_function_label(ast_function*, const char *prefix);
665 bool ast_function_codegen(ast_function *self, ir_builder *builder);
666 bool ast_generate_accessors(ast_value *asvalue, ir_builder *ir);
669 * If the condition creates a situation where this becomes -1 size it means there are
670 * more AST_FLAGs than the type ast_flag_t is capable of holding. So either eliminate
671 * the AST flag count or change the ast_flag_t typedef to a type large enough to accomodate
674 typedef int static_assert_is_ast_flag_safe [((AST_FLAG_LAST) <= (ast_flag_t)(-1)) ? 1 : -1];