| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- (** Type definitions for abstract syntax tree, assembly code, and exceptions. *)
- (** {2 Abstract Syntax Tree} *)
- (** [(filename, startline, endline, startcol, endcol)]
- Location of node in source code. Used for error reporting. *)
- type location = string * int * int * int * int
- (** All operators supported by CiviC. *)
- type operator =
- | Neg | Not
- | Add | Sub | Mul | Div | Mod
- | Eq | Ne | Lt | Le | Gt | Ge
- | And | Or
- (** Wrappers for constant values. This eliminates the need for separate
- [(Bool|Int|Float)Const] nodes, and these values can be passed to {!instr}
- constructors as well. *)
- type const =
- | BoolVal of bool
- | IntVal of int32
- | FloatVal of float
- (** Data types supported by CiviC. [ArrayDims] defines an array type with a set
- of dimensions. {!Dimreduce} replaces this multi-dimensional type with the
- [Array] type, which signifies a one-dimensional array of a certain basic
- type. [Unknown] is used by {!Typecheck} when the result of an operation is
- unclear due to a type error. *)
- type ctype =
- | Void | Bool | Int | Float | Array of ctype
- | ArrayDims of ctype * node list
- | Unknown
- (** Annotations for {!node}. Each node has a list of zero or more annotations
- that are gradually added through the compiler phases:
- - [Loc] is a location added by the parser.
- - [Depth] is the nesting level of a variable or function, and is added by
- {!Context}.
- - [Type] is a {!ctype} annotation added by {!Typecheck}.
- - [Index] is the stack index of a variable declaration, added by {!Index}.
- - [LabelName] is also added during index analysis, it holds the label name
- of a [FunDef]. *)
- and annotation =
- | Loc of location
- | Depth of int
- | Type of ctype
- | Index of int
- | LabelName of string
- (** Shorthand for annotation list used in definition of {!node}, should not be
- used elsewhere. *)
- and ann = annotation list
- (** Abstract Syntax Tree nodes. All attributes except for [ann] (annotations)
- are documented. [Program] to [Arg] are types returned by the parser, the
- rest of the types are annotations or transformations for convenience during
- later traversals. *)
- and node =
- (* Global *)
- | Program of node list * ann
- (** list of declarations *)
- | GlobalDec of ctype * string * ann
- (** type, name [extern <type> <name>;] *)
- | GlobalDef of bool * ctype * string * node option * ann
- (** export, type, name, initialisation [<type> <name>[= <initialisation>];] *)
- | FunDec of ctype * string * node list * ann
- (** ret_type, name, params [extern <ret_type> <name>(<params>);] *)
- | FunDef of bool * ctype * string * node list * node * ann
- (** export, ret_type, name, params, body
- [[export ]<ret_type> <name>(<params>) <body>] *)
- | Param of ctype * string * ann
- (** type, name [<type> <name>] *)
- | Dim of string * ann
- (** name [<name>] Used in [ArrayDims] for [Param] of array type. *)
- | VarDecs of node list (** Holds all [VarDec] nodes in a [FunDef]. *)
- | LocalFuns of node list (** Holds all local [FunDef] nodes in a [FunDef]. *)
- (* Statements *)
- | VarDec of ctype * string * node option * ann
- (** type, name, initialisation [<type> <name>[= <initialisation>];] *)
- | Assign of string * node list option * node * ann
- (** name, indices, value [<name>[<indices>] = <value>;] *)
- | For of string * node * node * node * node * ann
- (** counter, start, stop, step, body
- [for (int <counter> = <start>, <stop>[, <step>]) <body>] *)
- | Return of node * ann
- (** Return value [return <value>;] *)
- | Expr of node
- (** Expression statement [<expr>;] *)
- | Block of node list
- (** Statement list enclosed in braces: body [{ <body> }] *)
- | If of node * node * ann
- (** condition, body [if (<condition>) <body>] *)
- | IfElse of node * node * node * ann
- (** condition, true_body, false_body [if (<condition>) <true_body> else <false_body>] *)
- | While of node * node * ann
- (** condition, body [while (<condition>) <body>] *)
- | DoWhile of node * node * ann
- (** condition, body [do <body> while (<condition>)] *)
- (* Expressions *)
- | Const of const * ann
- (** Constant value, e.g. [0] or [1.0] or [true]. *)
- | ArrayConst of node list * ann
- (** Array initialisation [[ <expr> , ... ]] *)
- | Var of string * node list option * ann
- (** name, indices [<name> | <name>[<indices>] ] *)
- | Monop of operator * node * ann
- (** operator, operand [<operator><operand>] *)
- | Binop of operator * node * node * ann
- (** operator, left, right [<left> <operator> <right>] *)
- | TypeCast of ctype * node * ann
- (** target_type, value [(target_type)value] *)
- | FunCall of string * node list * ann
- (** name, args [<name>(<args>)] *)
- | Arg of node
- (** Function argument wrapper. *)
- (* Additional types for convenience in traversals
- * Mostly used to annotate existing nodes with information from declarations *)
- | Allocate of node * node list * ann
- (** Array allocation: declaration, dimensions
- [<declaration> = __allocate(<dimensions>);] *)
- | VarUse of node * node list option * ann
- (** Replacement for [Var] with declaration. *)
- | FunUse of node * node list * ann
- (** Replacement for [FunCall] with declaration. *)
- | VarLet of node * node list option * node * ann
- (** Replacement for [Assign] with declaration. *)
- | ArrayInit of node * node list
- (** Wrapper for array initalisation with dimensions, used by {!Desug}. *)
- | Cond of node * node * node * ann
- (** cond, true_expr, false_expr [<cond> ? <true_expr> : <false_expr>]
- Used for short-circuit evaluation. *)
- | DummyNode
- (** Null node, pruned by {!Util.flatten_blocks}, which is called by
- {!Util.traverse_unit} in a traversal. *)
- (** {2 Assembly instructions} *)
- (** Stack scopes. Correspond to [(load|store|isr)[gln ]] respectively. *)
- type stack_scope = Glob | Local | Rel of int | Current
- (** Function scopes. *)
- type rtn_scope = ExternFun of int | LocalFun of int * string
- (** Assembly instructions. *)
- type instr =
- | Comment of string (** [; <comment>] *)
- | InlineComment of instr * string (** [<instr> ; <comment>] *)
- | Label of string (** [<label>:] *)
- (* Directives *)
- | Export of string * ctype * ctype list * string
- (** [.export "<name>" <ret_type> [<arg_type_1> ...] <label>] *)
- | Import of string * ctype * ctype list
- (** [.import "<name>" <ret_type> [<arg_type_1> ...]] *)
- | ConstDef of const
- (** [.const <value>] *)
- | Global of ctype
- (** [.global <type>] *)
- | Store of ctype * stack_scope * int (** [[ifba]store[ gn]] *)
- | Load of ctype * stack_scope * int (** [[ifb]load[ gn] G] *)
- | LoadConst of ctype * int (** [[ifb]loadc C] *)
- | LoadImm of const (** [[ifb]loadc_[01tf] | iloadc_m1] *)
- (* Operators ] *)
- | Op of operator * ctype
- (** [[ifb](add|sub|mul|div|rem|neg|not|ne|eq|lt|le|gt|ge)] *)
- | Convert of ctype * ctype (** [i2f | f2i] *)
- | Inc of int * int (** [iinc L C] *)
- | Dec of int * int (** [idec L C] *)
- | IncOne of int (** [iinc_1 L] *)
- | DecOne of int (** [idec_1 L] *)
- (* Control flow *)
- | RtnInit of stack_scope (** [isr[ lg] | isrn N] *)
- | RtnJmp of rtn_scope (** [jsr A O | jsre I] *)
- | RtnEnter of int (** [esr L] *)
- | Ret of ctype (** [[ ifb]return] *)
- | Branch of bool * string (** [branch_[tf] O] *)
- | Jump of string (** [jump O] *)
- (* Stack management *)
- | Pop of ctype (** [[ifb]pop] *)
- (* Arrays *)
- | NewArray of ctype (** [[ifb]newa] *)
- | LoadArray of ctype (** [[ifb]loada] *)
- | StoreArray of ctype (** [[ifb]storea] *)
- | EmptyLine (** Empty line, added in between functions for readability. *)
- (** {2 Global} *)
- (** Intermediate representations, passed from phase to phase. *)
- type intermediate =
- | Empty
- (** For phases without input/output (first and last phase). *)
- | FileContent of string * string
- (** Input file content. *)
- | Ast of node
- (** Abstract Syntax Tree. *)
- | Assembly of instr list
- (** List of assembly instructions. *)
- (** Container for command-line arguments. *)
- type args_record = {
- mutable infile : string option;
- (** Input filename. *)
- mutable outfile : string option;
- (** Output filename. *)
- mutable verbose : int;
- (** Verbosity level. *)
- mutable cpp : bool;
- (** Run C preprocessor? *)
- mutable optimize : bool;
- (** Run optimization phases? *)
- mutable endphase : string;
- (** Stop at the phase which has the given identifier (see {!Main.phases}). *)
- }
- (** {2 Exceptions} *)
- type error_msg =
- (** General compilation error message (caught by main function). *)
- | Msg of string
- (** Error occurred at a certain location in an input file. Used in combination
- with {!Util.prerr_loc} and {!Util.prerr_loc_msg}. *)
- | LocMsg of location * string
- (** Error occurred at a certain AST node, use {!Util.locof} to get node
- location. *)
- | NodeMsg of node * string
- (** No error message, just quit *)
- | NoMsg
- (** Raised error message. Makes the compiler print the message and quit with
- status code 1. *)
- exception FatalError of error_msg
- (** Catch-all error for traversals that accept a limit set of node types. *)
- exception InvalidNode
- (** Error raised when a phase receives an unsupported {!intermediate} type. *)
- exception InvalidInput
|