types.mli 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. (** Type definitions for abstract syntax tree, assembly code, and exceptions. *)
  2. (** {2 Abstract Syntax Tree} *)
  3. (** [(filename, startline, endline, startcol, endcol)]
  4. Location of node in source code. Used for error reporting. *)
  5. type location = string * int * int * int * int
  6. (** All operators supported by CiviC. *)
  7. type operator =
  8. | Neg | Not
  9. | Add | Sub | Mul | Div | Mod
  10. | Eq | Ne | Lt | Le | Gt | Ge
  11. | And | Or
  12. (** Wrappers for constant values. This eliminates the need for separate
  13. [(Bool|Int|Float)Const] nodes, and these values can be passed to {!instr}
  14. constructors as well. *)
  15. type const =
  16. | BoolVal of bool
  17. | IntVal of int
  18. | FloatVal of float
  19. (** Data types supported by CiviC. [ArrayDims] defines an array type with a set
  20. of dimensions. {!Dimreduce} replaces this multi-dimensional type with the
  21. [Array] type, which signifies a one-dimensional array of a certain basic
  22. type. [Unknown] is used by {!Typecheck} when the result of an operation is
  23. unclear due to a type error. *)
  24. type ctype =
  25. | Void | Bool | Int | Float | Array of ctype
  26. | ArrayDims of ctype * node list
  27. | Unknown
  28. (** Annotations for {!node}. Each node has a list of zero or more annotations
  29. that are gradually added through the compiler phases:
  30. - [Loc] is a location added by the parser.
  31. - [Depth] is the nesting level of a variable or function, and is added by
  32. {!Context}.
  33. - [Type] is a {!ctype} annotation added by {!Typecheck}.
  34. - [Index] is the stack index of a variable declaration, added by {!Index}.
  35. - [LabelName] is also added during index analysis, it holds the label name
  36. of a [FunDef]. *)
  37. and annotation =
  38. | Loc of location
  39. | Depth of int
  40. | Type of ctype
  41. | Index of int
  42. | LabelName of string
  43. (** Shorthand for annotation list used in definition of {!node}, should not be
  44. used elsewhere. *)
  45. and ann = annotation list
  46. (** Abstract Syntax Tree nodes. All attributes except for [ann] (annotations)
  47. are documented. [Program] to [Arg] are types returned by the parser, the
  48. rest of the types are annotations or transformations for convenience during
  49. later traversals. *)
  50. and node =
  51. (* Global *)
  52. | Program of node list * ann
  53. (** list of declarations *)
  54. | GlobalDec of ctype * string * ann
  55. (** type, name [extern <type> <name>;] *)
  56. | GlobalDef of bool * ctype * string * node option * ann
  57. (** export, type, name, initialisation [<type> <name>[= <initialisation>];] *)
  58. | FunDec of ctype * string * node list * ann
  59. (** ret_type, name, params [extern <ret_type> <name>(<params>);] *)
  60. | FunDef of bool * ctype * string * node list * node * ann
  61. (** export, ret_type, name, params, body
  62. [[export ]<ret_type> <name>(<params>) <body>] *)
  63. | Param of ctype * string * ann
  64. (** type, name [<type> <name>] *)
  65. | Dim of string * ann
  66. (** name [<name>] Used in [ArrayDims] for [Param] of array type. *)
  67. | VarDecs of node list (** Holds all [VarDec] nodes in a [FunDef]. *)
  68. | LocalFuns of node list (** Holds all local [FunDef] nodes in a [FunDef]. *)
  69. (* Statements *)
  70. | VarDec of ctype * string * node option * ann
  71. (** type, name, initialisation [<type> <name>[= <initialisation>];] *)
  72. | Assign of string * node list option * node * ann
  73. (** name, indices, value [<name>[<indices>] = <value>;] *)
  74. | For of string * node * node * node * node * ann
  75. (** counter, start, stop, step, body
  76. [for (int <counter> = <start>, <stop>[, <step>]) <body>] *)
  77. | Return of node * ann
  78. (** Return value [return <value>;] *)
  79. | Expr of node
  80. (** Expression statement [<expr>;] *)
  81. | Block of node list
  82. (** Statement list enclosed in braces: body [{ <body> }] *)
  83. | If of node * node * ann
  84. (** condition, body [if (<condition>) <body>] *)
  85. | IfElse of node * node * node * ann
  86. (** condition, true_body, false_body [if (<condition>) <true_body> else <false_body>] *)
  87. | While of node * node * ann
  88. (** condition, body [while (<condition>) <body>] *)
  89. | DoWhile of node * node * ann
  90. (** condition, body [do <body> while (<condition>)] *)
  91. (* Expressions *)
  92. | Const of const * ann
  93. (** Constant value, e.g. [0] or [1.0] or [true]. *)
  94. | ArrayConst of node list * ann
  95. (** Array initialisation [[ <expr> , ... ]] *)
  96. | Var of string * node list option * ann
  97. (** name, indices [<name> | <name>[<indices>] ] *)
  98. | Monop of operator * node * ann
  99. (** operator, operand [<operator><operand>] *)
  100. | Binop of operator * node * node * ann
  101. (** operator, left, right [<left> <operator> <right>] *)
  102. | TypeCast of ctype * node * ann
  103. (** target_type, value [(target_type)value] *)
  104. | FunCall of string * node list * ann
  105. (** name, args [<name>(<args>)] *)
  106. | Arg of node
  107. (** Function argument wrapper. *)
  108. (* Additional types for convenience in traversals
  109. * Mostly used to annotate existing nodes with information from declarations *)
  110. | Allocate of node * node list * ann
  111. (** Array allocation: declaration, dimensions
  112. [<declaration> = __allocate(<dimensions>);] *)
  113. | VarUse of node * node list option * ann
  114. (** Replacement for [Var] with declaration. *)
  115. | FunUse of node * node list * ann
  116. (** Replacement for [FunCall] with declaration. *)
  117. | VarLet of node * node list option * node * ann
  118. (** Replacement for [Assign] with declaration. *)
  119. | ArrayInit of node * node list
  120. (** Wrapper for array initalisation with dimensions, used by {!Desug}. *)
  121. | Cond of node * node * node * ann
  122. (** cond, true_expr, false_expr [<cond> ? <true_expr> : <false_expr>]
  123. Used for short-circuit evaluation. *)
  124. | DummyNode
  125. (** Null node, pruned by {!Util.flatten_blocks}, which is called by
  126. {!Util.traverse_unit} in a traversal. *)
  127. (** {2 Assembly instructions} *)
  128. (** Stack scopes. Correspond to [(load|store|isr)[gln ]] respectively. *)
  129. type stack_scope = Glob | Local | Rel of int | Current
  130. (** Function scopes. *)
  131. type rtn_scope = ExternFun of int | LocalFun of int * string
  132. (** Assembly instructions. *)
  133. type instr =
  134. | Comment of string (** [; <comment>] *)
  135. | InlineComment of instr * string (** [<instr> ; <comment>] *)
  136. | Label of string (** [<label>:] *)
  137. (* Directives *)
  138. | Export of string * ctype * ctype list * string
  139. (** [.export "<name>" <ret_type> [<arg_type_1> ...] <label>] *)
  140. | Import of string * ctype * ctype list
  141. (** [.import "<name>" <ret_type> [<arg_type_1> ...]] *)
  142. | ConstDef of const
  143. (** [.const <value>] *)
  144. | Global of ctype
  145. (** [.global <type>] *)
  146. | Store of ctype * stack_scope * int (** [[ifba]store[ gn]] *)
  147. | Load of ctype * stack_scope * int (** [[ifb]load[ gn] G] *)
  148. | LoadConst of ctype * int (** [[ifb]loadc C] *)
  149. | LoadImm of const (** [[ifb]loadc_[01tf] | iloadc_m1] *)
  150. (* Operators ] *)
  151. | Op of operator * ctype
  152. (** [[ifb](add|sub|mul|div|rem|neg|not|ne|eq|lt|le|gt|ge)] *)
  153. | Convert of ctype * ctype (** [i2f | f2i] *)
  154. | Inc of int * int (** [iinc L C] *)
  155. | Dec of int * int (** [idec L C] *)
  156. | IncOne of int (** [iinc_1 L] *)
  157. | DecOne of int (** [idec_1 L] *)
  158. (* Control flow *)
  159. | RtnInit of stack_scope (** [isr[ lg] | isrn N] *)
  160. | RtnJmp of rtn_scope (** [jsr A O | jsre I] *)
  161. | RtnEnter of int (** [esr L] *)
  162. | Ret of ctype (** [[ ifb]return] *)
  163. | Branch of bool * string (** [branch_[tf] O] *)
  164. | Jump of string (** [jump O] *)
  165. (* Stack management *)
  166. | Pop of ctype (** [[ifb]pop] *)
  167. (* Arrays *)
  168. | NewArray of ctype * int (** [[ifb]newa D] *)
  169. | ArraySize of int (** [asize D] *)
  170. | LoadArray of ctype (** [[ifb]loada] *)
  171. | StoreArray of ctype (** [[ifb]storea] *)
  172. | EmptyLine (** Empty line, added in between functions for readability. *)
  173. (** {2 Global} *)
  174. (** Intermediate representations, passed from phase to phase. *)
  175. type intermediate =
  176. | Empty
  177. (** For phases without input/output (first and last phase). *)
  178. | FileContent of string * string
  179. (** Input file content. *)
  180. | Ast of node
  181. (** Abstract Syntax Tree. *)
  182. | Assembly of instr list
  183. (** List of assembly instructions. *)
  184. (** Container for command-line arguments. *)
  185. type args_record = {
  186. mutable infile : string option;
  187. (** Input filename. *)
  188. mutable outfile : string option;
  189. (** Output filename. *)
  190. mutable verbose : int;
  191. (** Verbosity level. *)
  192. mutable cpp : bool;
  193. (** Run C preprocessor? *)
  194. mutable optimize : bool;
  195. (** Run optimization phases? *)
  196. mutable endphase : string;
  197. (** Stop at the phase which has the given identifier (see {!Main.phases}). *)
  198. }
  199. (** {2 Exceptions} *)
  200. type error_msg =
  201. (** General compilation error message (caught by main function). *)
  202. | Msg of string
  203. (** Error occurred at a certain location in an input file. Used in combination
  204. with {!Util.prerr_loc} and {!Util.prerr_loc_msg}. *)
  205. | LocMsg of location * string
  206. (** Error occurred at a certain AST node, use {!Util.locof} to get node
  207. location. *)
  208. | NodeMsg of node * string
  209. (** No error message, just quit *)
  210. | NoMsg
  211. (** Raised error message. Makes the compiler print the message and quit with
  212. status code 1. *)
  213. exception FatalError of error_msg
  214. (** Catch-all error for traversals that accept a limit set of node types. *)
  215. exception InvalidNode
  216. (** Error raised when a phase receives an unsupported {!intermediate} type. *)
  217. exception InvalidInput