types.mli 9.4 KB

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