7 * Implements the interface to LICE's abstract syntax tree
9 typedef struct ast_s ast_t;
13 * The type of ast node
17 * AST_TYPE_LITERAL - Literal
18 * AST_TYPE_STRING - String literal
19 * AST_TYPE_VAR_LOCAL - Local variable
20 * AST_TYPE_VAR_GLOBAL - Global variable
21 * AST_TYPE_CALL - Direct function call
22 * AST_TYPE_POINTERCALL - Indirect function call
23 * AST_TYPE_FUNCTION - Function
24 * AST_TYPE_PROTOTYPE - Prototype
25 * AST_TYPE_DECLARATION - Declaration
26 * AST_TYPE_INITIALIZER - Initializer
27 * AST_TYPE_STRUCT - Structure
28 * AST_TYPE_ADDRESS - Address of operation
29 * AST_TYPE_DEREFERENCE - Pointer dereference
30 * AST_TYPE_EXPRESSION_TERNARY - Ternary expression
31 * AST_TYPE_EXPRESSION_CAST - Type cast expression
32 * AST_TYPE_STATEMENT_IF - If statement
33 * AST_TYPE_STATEMENT_FOR - For statement
34 * AST_TYPE_STATEMENT_WHILE - While statement
35 * AST_TYPE_STATEMENT_DO - Do statement
36 * AST_TYPE_STATEMENT_SWITCH - Switch statement
37 * AST_TYPE_STATEMENT_CASE - Switch statement case
38 * AST_TYPE_STATEMENT_DEFAULT - Switch statement default case
39 * AST_TYPE_STATEMENT_RETURN - Return statement
40 * AST_TYPE_STATEMENT_BREAK - Break statement
41 * AST_TYPE_STATEMENT_CONTINUE - Continue statement
42 * AST_TYPE_STATEMENT_COMPOUND - Compound statement
43 * AST_TYPE_STATEMENT_GOTO - Goto statement
44 * AST_TYPE_STATEMENT_LABEL - Goto statement label
45 * AST_TYPE_POST_INCREMENT - Post increment operation
46 * AST_TYPE_POST_DECREMENT - Post decrement operation
47 * AST_TYPE_PRE_INCREMENT - Pre increment operation
48 * AST_TYPE_PRE_DECREMENT - Pre decrement operation
49 * AST_TYPE_LSHIFT - Left shift operation
50 * AST_TYPE_RSHIFT - Right shift operation
51 * AST_TYPE_LRSHIFT - Logical right shift operation
52 * AST_TYPE_EQUAL - Equality condition
53 * AST_TYPE_GEQUAL - Greater-or-equal condition
54 * AST_TYPE_LEQUAL - Less-or-equal condition
55 * AST_TYPE_NEQUAL - Not-equal condition
56 * AST_TYPE_NEGATE - Negation
57 * AST_TYPE_AND - Logical-and operation
58 * AST_TYPE_OR - Logical-or operation
59 * AST_TYPE_NEGATE - Unary minus
60 * AST_TYPE_VA_START - __builtin_va_start
61 * AST_TYPE_VA_ARG - __builtin_va_arg
62 * AST_TYPE_DESIGNATOR - Designation on function types
63 * AST_TYPE_CONVERT - Type conversion
66 AST_TYPE_LITERAL = 0x100,
79 AST_TYPE_EXPRESSION_TERNARY,
80 AST_TYPE_EXPRESSION_CAST,
81 AST_TYPE_STATEMENT_IF,
82 AST_TYPE_STATEMENT_FOR,
83 AST_TYPE_STATEMENT_WHILE,
84 AST_TYPE_STATEMENT_DO,
85 AST_TYPE_STATEMENT_SWITCH,
86 AST_TYPE_STATEMENT_CASE,
87 AST_TYPE_STATEMENT_DEFAULT,
88 AST_TYPE_STATEMENT_RETURN,
89 AST_TYPE_STATEMENT_BREAK,
90 AST_TYPE_STATEMENT_CONTINUE,
91 AST_TYPE_STATEMENT_COMPOUND,
92 AST_TYPE_STATEMENT_GOTO,
93 AST_TYPE_STATEMENT_LABEL,
94 AST_TYPE_STATEMENT_GOTO_COMPUTED,
95 AST_TYPE_STATEMENT_LABEL_COMPUTED,
96 AST_TYPE_POST_INCREMENT,
97 AST_TYPE_POST_DECREMENT,
98 AST_TYPE_PRE_INCREMENT,
99 AST_TYPE_PRE_DECREMENT,
119 * Type describing the ast type.
124 * TYPE_BOOL - boolean
129 * TYPE_LLONG - long long
130 * TYPE_DOUBLE - double
131 * TYPE_LDOUBLE - long double
132 * TYPE_ARRAY - array (also contains a type_t for base type)
133 * TYPE_POINTER - pointer (also contains a type_t for base type)
134 * TYPE_STRUCTURE - structure (user defined)
135 * TYPE_FUNCTION - function (user defined)
136 * TYPE_CDECL - used by the parser for dealing with declarations
157 * Type: ast_data_type_t
158 * Type describing the indice into `ast_data_table`
162 * AST_DATA_VOID - void
163 * AST_DATA_BOOL - boolean
164 * AST_DATA_LONG - long
165 * AST_DATA_LLONG - long long
167 * AST_DATA_SHORT - short
168 * AST_DATA_CHAR - char
169 * AST_DATA_FLOAT - float
170 * AST_DATA_DOUBLE - double
171 * AST_DATA_LDOUBLE - long double
172 * AST_DATA_ULONG - unsigned long
173 * AST_DATA_ULLONG - unsigned long long
174 * AST_DATA_FUNCTION - function (current)
195 * Describes type of declarations
199 * CDECL_BODY - function body
200 * CDECL_PARAMETER - parameters (with name)
201 * CDECL_TYPEONLY - parameters (without name)
213 * Describes the storage class for a given variable
217 * STORAGE_TYPEDEF - typedef to another type
218 * STORAGE_EXTERN - external linkage
219 * STORAGE_STATIC - static storage
220 * STORAGE_AUTO - automatic storage (implicit)
221 * STORAGE_REGISTER - make use of register for storage
232 * Struct: data_type_t
233 * A structure that describes a data type.
235 typedef struct data_type_s data_type_t;
239 * The type of the data type.
241 * See <type_t> Constants for a list of
242 * valid constant values.
248 * The size of the given data type, this is often the value
249 * provided for the given type in arch_[target].h
255 * Describes if the type is signed or unsigned.
261 * Describes if the type is static.
267 * Instances of the data type.
269 * When used as a base-type, i.e not an array; this will be
270 * 1, otherwise it will be the length of the array, or -1
271 * if the size of the array is unknown.
277 * When the variable is a pointer type, this will point to another
278 * data type that describes the base type of the pointer, NULL other-
281 data_type_t *pointer;
287 * Pointer to a table of fields (if structure)
293 * Offset of the given field in a structure (if a structure base type)
299 * If we're dealing with a structure this will be true, false
315 * Variable: returntype
316 * Pointer to a data type which describes the return type
317 * of the function (if a function)
319 data_type_t *returntype;
322 * Variable: parameters
323 * Pointer to a list of parameters for a function.
329 * Describes if the given function is variable-argument.
331 * Contains the value `true` when the function has
332 * three dots `...` in it's prototype, otherwise `false`.
339 * Struct: ast_string_t
340 * The *AST_TYPE_STRING* ast node.
351 * Name of the label associated with the string.
357 * Struct: ast_variable_t
358 * The *AST_TYPE_VAR_LOCAL* and *AST_TYPE_VAR_GLOBAL* ast node.
363 * Name of the variable
369 * Offset of the variable on the stack.
375 * Name of the label associated with the variable.
381 * Compound literal list for initialization
387 * Struct ast_function_call_t
391 * Not associated with any node. Instead describes the
392 * data associated with a function call for *ast_function_t*
397 * Pointer to a list of arguments for a function call
403 * The basetype of the function that is being called. This type
404 * is the 'function type', ie. accessing the returntype member of
405 * this will return the FUNCTIONS return type.
410 * Variable: functionpointer
411 * Pointer to function for function pointer call.
414 * This only associates with AST_TYPE_POINTERCALL, in which this
415 * node is filled with the function in which to call by indirection
418 ast_t *functionpointer;
419 } ast_function_call_t;
422 * Struct: ast_function_t
423 * The *AST_TYPE_FUNCTION* ast node.
434 * Data associated with a function call.
436 ast_function_call_t call;
440 * Pointer to a list of parameters.
446 * Pointer to a list of locals.
452 * Pointer to an ast node which describes the body.
455 * A body is usually composed of a serise of ast nodes,
456 * typically a compound expression, but could also contain
457 * nested compound expressions. Think of this as a pointer
458 * to the head of the beginning of a serise of basic-blocks
459 * which are the forming of the function body.
465 * Struct: ast_unary_t
466 * Represents a unary operation in the AST tree
471 * Pointer to the operand the unary operation is to
479 * Represents a declaration in the AST tree
484 * Pointer to the variable node associated with the
491 * When the declaration includes an initialization this points
492 * to a initlization list.
498 * Struct: ast_ifthan_t
499 * Represents a if-than node in the AST tree.
502 * Describes a two-branch gaurded by conditional test node in the AST
503 * tree for implementing ternary expressions and if statements.
514 * Basic block for truth path in branch
520 * Basic block for false path in branch
527 * Represents a for-loop node in the AST tree.
530 * Standard for loop with precondition / initilization expression,
531 * conditionally testsed, and post step / expression, ergo
532 * for(init; cond; step) body;
548 * Represents an initializer in the AST tree.
551 * Represents array initializer lists, as well as aggregate initializer
552 * lists for structure, enum and union. Also represents a designated
553 * initializer for a structure.
556 /* Variable: value */
559 /* Variable: offset */
567 * Struct: ast_switch_t
568 * Represents a switch statement in the AST tree.
579 * Represents a goto statement (or label) in the AST tree.
584 * When not used as a goto statement, describes the name of a label
585 * that may be 'gone to' with 'goto'
591 * Where to go (label wise) for a goto statement.
598 * The monolthic ast tree and node at the same time.
601 * The ast tree is just a doubly-linked list of ast nodes which are
602 * capable of being all the possible ast nodes at once. This is
603 * acomplished with a rather large union of all ast nodes. The only
604 * thing that declares what a node actually is, is the nodes type
605 * member. This is beneficial to keeping the complexity of the AST
606 * tree down, while also keeping memory footprint low. One more
607 * interesting aspect of this is the ability to have the AST tree
608 * nodes (next, prev), which make up the doubly-linked list part
609 * of the same union, giving us a free way to terminate the tree
610 * without using additional space to determine it.
625 ast_variable_t variable;
626 ast_function_t function;
631 ast_switch_t switchstmt;
646 data_type_t *fieldtype;
656 extern data_type_t *ast_data_table[AST_DATA_COUNT];
658 extern list_t *ast_locals;
659 extern list_t *ast_gotos;
660 extern table_t *ast_globalenv;
661 extern table_t *ast_localenv;
662 extern table_t *ast_structures;
663 extern table_t *ast_unions;
664 extern table_t *ast_labels;
667 * Function: ast_structure_reference
668 * Creates an structure reference of a given type for a given field
671 * type - The type of the field for reference
672 * structure - The structure that contains said field to be referenced
673 * name - The name of the field in that structure to reference
676 * An ast node referencing that field in that paticular structure on
677 * success, otherwise NULL.
679 ast_t *ast_structure_reference(data_type_t *type, ast_t *structure, char *name);
682 * Function: ast_structure_new
683 * Creates a structure data type
686 * field - A table of data_type_t fields for the structure
687 * size - The size of the structure
688 * isstruct - true if structure, false if structure-like
691 * A new structure data type with the specified fields and size on
692 * success, NULL otherwise.
694 data_type_t *ast_structure_new(table_t *fields, int size, bool isstruct);
697 ast_t *ast_new_unary(int type, data_type_t *data, ast_t *operand);
698 ast_t *ast_new_binary(data_type_t *ctype, int type, ast_t *left, ast_t *right);
699 ast_t *ast_new_integer(data_type_t *type, int value);
700 ast_t *ast_new_floating(data_type_t *, double value);
701 ast_t *ast_new_char(char value);
702 ast_t *ast_new_string(char *value);
703 ast_t *ast_new_label(char *);
705 char *ast_label(void);
707 ast_t *ast_declaration(ast_t *var, list_t *init);
708 ast_t *ast_variable_local(data_type_t *type, char *name);
709 ast_t *ast_variable_global(data_type_t *type, char *name);
710 ast_t *ast_call(data_type_t *type, char *name, list_t *args);
711 ast_t *ast_pointercall(ast_t *ast, list_t *args);
712 ast_t *ast_function(data_type_t *type, char *name, list_t *params, ast_t *body, list_t *locals);
713 ast_t *ast_initializer(ast_t *, data_type_t *, int);
714 ast_t *ast_if(ast_t *cond, ast_t *then, ast_t *last);
715 ast_t *ast_for(ast_t *init, ast_t *cond, ast_t *step, ast_t *body);
716 ast_t *ast_while(ast_t *cond, ast_t *body);
717 ast_t *ast_do(ast_t *cond, ast_t *body);
718 ast_t *ast_return(ast_t *val);
719 ast_t *ast_compound(list_t *statements);
720 ast_t *ast_ternary(data_type_t *type, ast_t *cond, ast_t *then, ast_t *last);
721 ast_t *ast_switch(ast_t *expr, ast_t *body);
722 ast_t *ast_case(int begin, int end);
723 ast_t *ast_goto(char *);
724 ast_t *ast_goto_computed(ast_t *expression);
725 ast_t *ast_label_address(char *);
726 ast_t *ast_make(int type);
728 ast_t *ast_va_start(ast_t *);
729 ast_t *ast_va_arg(data_type_t *, ast_t*);
731 ast_t *ast_designator(char *name, ast_t *func);
733 data_type_t *ast_prototype(data_type_t *returntype, list_t *paramtypes, bool dots);
734 data_type_t *ast_pointer(data_type_t *type);
735 data_type_t *ast_array(data_type_t *type, int size);
736 data_type_t *ast_array_convert(data_type_t *ast);
737 data_type_t *ast_result_type(int operation, data_type_t *);
739 ast_t *ast_designator_convert(ast_t *ast);
741 bool ast_struct_compare(data_type_t *a, data_type_t *b);
744 * Function: ast_type_string
745 * Get the type of a data_type_t as a string.
747 const char *ast_type_string(data_type_t *type);
750 * Function: ast_type_isinteger
751 * Check if a given data type is an integral type.
754 * type - pointer to the data_type_t object to check
757 * true if *type* is integral type, false otherwise.
760 * Integral includes any of the following data types:
767 bool ast_type_isinteger(data_type_t *type);
770 * Function: ast_type_isfloating
771 * Check if a given data type is a floating-point one.
774 * type - pointer to the data_type_t object to check
777 * true if *type* is floating point, false otherwise.
780 * Floating-point includes any of the following data types
785 bool ast_type_isfloating(data_type_t *type);
788 * Function: ast_type_isstring
789 * Check if a given data type is string
792 * type - pointer to the data_type object to check
795 * trye if *type* is a string type, false otherwise.
798 * string type is determined if it's an array and the base
799 * type of that array (when decayed to pointer type) is
802 bool ast_type_isstring(data_type_t *type);
804 data_type_t *ast_type_copy(data_type_t *type);
805 data_type_t *ast_type_copy_incomplete(data_type_t *type);
806 data_type_t *ast_type_create(type_t type, bool sign);
807 data_type_t *ast_type_stub(void);
809 ast_t *ast_type_convert(data_type_t *type, ast_t *ast);
812 char *ast_string(ast_t *ast);