|
|
@@ -22,7 +22,7 @@ type const =
|
|
|
| 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 an
|
|
|
+ of dimensions. {!Dimreduce} replaces this multi-dimensional type with the
|
|
|
[Array] type, which signifies a one-dimensional array of a certain basic
|
|
|
type. *)
|
|
|
type ctype =
|
|
|
@@ -51,9 +51,12 @@ and annotation =
|
|
|
and ann = annotation list
|
|
|
|
|
|
(** Abstract Syntax Tree nodes. All attributes except for [ann] (annotations)
|
|
|
- are documented. *)
|
|
|
+ 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
|
|
|
@@ -73,7 +76,8 @@ and node =
|
|
|
| VarDecs of node list (** Holds all [VarDec] nodes in a [FunDef]. *)
|
|
|
| LocalFuns of node list (** Holds all local [FunDef] nodes in a [FunDef]. *)
|
|
|
|
|
|
- (** {4 Statements } *)
|
|
|
+ (* Statements *)
|
|
|
+
|
|
|
| VarDec of ctype * string * node option * ann
|
|
|
(** type, name, initialisation [<type> <name>[= <initialisation>];] *)
|
|
|
| Assign of string * node list option * node * ann
|
|
|
@@ -86,17 +90,18 @@ and node =
|
|
|
| Expr of node
|
|
|
(** expression statement [<expr>;] *)
|
|
|
| Block of node list
|
|
|
- (** body [{ <body> }] *)
|
|
|
+ (** statement list enclosed in braces: body [{ <body> }] *)
|
|
|
| If of node * node * ann
|
|
|
- (** condition, body [if (condition) { body }] *)
|
|
|
+ (** condition, body [if (<condition>) <body>] *)
|
|
|
| IfElse of node * node * node * ann
|
|
|
- (** condition, true_body, false_body [if (<condition>) { <true_body> } else { <false_body> }] *)
|
|
|
+ (** condition, true_body, false_body [if (<condition>) <true_body> else <false_body>] *)
|
|
|
| While of node * node * ann
|
|
|
- (** condition, body [while (<condition>) { <body> }] *)
|
|
|
+ (** condition, body [while (<condition>) <body>] *)
|
|
|
| DoWhile of node * node * ann
|
|
|
- (** condition, body [do { <body> } whlie (<condition>)] *)
|
|
|
+ (** condition, body [do <body> whlie (<condition>)] *)
|
|
|
|
|
|
(* Expressions *)
|
|
|
+
|
|
|
| Const of const * ann
|
|
|
(** constant [bool|int|float constant] *)
|
|
|
| ArrayConst of node list * ann
|
|
|
@@ -116,8 +121,10 @@ and node =
|
|
|
|
|
|
(* Additional types for convenience in traversals
|
|
|
* Mostly used to annotate existing nodes with information from declarations *)
|
|
|
+
|
|
|
| Allocate of node * node list * ann
|
|
|
- (** declaration, dims [<declaration> = __allocate(<dims>);] *)
|
|
|
+ (** 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
|
|
|
@@ -135,55 +142,61 @@ and node =
|
|
|
|
|
|
(** {2 Assembly instructions} *)
|
|
|
|
|
|
+(** Stack variable scopes. Correspond to [(load|store)[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>: *)
|
|
|
+ | Comment of string (** [# <comment>] *)
|
|
|
+ | InlineComment of instr * string (** [<instr> # <comment>] *)
|
|
|
+ | Label of string (** [<label>:] *)
|
|
|
|
|
|
(* Directives *)
|
|
|
- (* .export "<name>" <ret_type> [ <arg_type>; ... ] <label> *)
|
|
|
| Export of string * ctype * ctype list * string
|
|
|
- (* .import "<name>" <ret_type> [ <arg_type>; ... ] *)
|
|
|
+ (** [.export "<name>" <ret_type> [<arg_type_1> ...] <label>] *)
|
|
|
| Import of string * ctype * ctype list
|
|
|
- (* .const <value> *)
|
|
|
+ (** [.import "<name>" <ret_type> [<arg_type_1> ...]] *)
|
|
|
| ConstDef of const
|
|
|
- (* .global <type> *)
|
|
|
+ (** [.const <value>] *)
|
|
|
| Global of ctype
|
|
|
+ (** [.global <type>] *)
|
|
|
|
|
|
- | Store of ctype * stack_scope * int (* [ifba]store[ gn] *)
|
|
|
+ | 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]load_[01tf] <value> *)
|
|
|
+ | Load of ctype * stack_scope * int (** [[ifb]load[ gn] G] *)
|
|
|
+ | LoadConst of ctype * int (** [[ifb]loadc C] *)
|
|
|
+ | LoadImm of const (** [[ifb]loadc_[01tf]] *)
|
|
|
|
|
|
- (* Operators *)
|
|
|
- | Op of operator * ctype (* [ifb]() *)
|
|
|
- | 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 *)
|
|
|
+ (* 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
|
|
|
- | RtnJmp of rtn_scope
|
|
|
- | RtnEnter of int
|
|
|
- | Ret of ctype
|
|
|
- | Branch of bool * string
|
|
|
- | Jump of string
|
|
|
+ | 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 *)
|
|
|
+ | Pop of ctype (** [[ifb]pop] *)
|
|
|
|
|
|
(* Arrays *)
|
|
|
- | NewArray of ctype * int
|
|
|
- | LoadArray of ctype
|
|
|
- | StoreArray of ctype
|
|
|
+ | NewArray of ctype * int (** [[ifb]newa D] *)
|
|
|
+ | ArraySize of int (** [asize D] *)
|
|
|
+ | LoadArray of ctype (** [[ifb]loada] *)
|
|
|
+ | StoreArray of ctype (** [[ifb]storea] *)
|
|
|
|
|
|
- | EmptyLine
|
|
|
- | DummyInstr
|
|
|
+ | EmptyLine (** Empty line, added in between functions for readability. *)
|
|
|
|
|
|
(** {2 General} *)
|
|
|
|