types.mli 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 an
  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. *)
  46. and node =
  47. (* Global *)
  48. | Program of node list * ann
  49. (** list of declarations *)
  50. | GlobalDec of ctype * string * ann
  51. (** type, name [extern <type> <name>;] *)
  52. | GlobalDef of bool * ctype * string * node option * ann
  53. (** export, type, name, initialisation [<type> <name>[= <initialisation>];] *)
  54. | FunDec of ctype * string * node list * ann
  55. (** ret_type, name, params [extern <ret_type> <name>(<params>);] *)
  56. | FunDef of bool * ctype * string * node list * node * ann
  57. (** export, ret_type, name, params, body
  58. [[export ]<ret_type> <name>(<params>) <body>] *)
  59. | Param of ctype * string * ann
  60. (** type, name [<type> <name>] *)
  61. | Dim of string * ann
  62. (** name [<name>] Used in [ArrayDims] for [Param] of array type. *)
  63. | VarDecs of node list (** Holds all [VarDec] nodes in a [FunDef]. *)
  64. | LocalFuns of node list (** Holds all local [FunDef] nodes in a [FunDef]. *)
  65. (** {4 Statements } *)
  66. | VarDec of ctype * string * node option * ann
  67. (** type, name, initialisation [<type> <name>[= <initialisation>];] *)
  68. | Assign of string * node list option * node * ann
  69. (** name, indices, value [<name>[<indices>] = <value>;] *)
  70. | For of string * node * node * node * node * ann
  71. (** counter, start, stop, step, body
  72. [for (int <counter> = <start>, <stop>[, <step>]) <body>] *)
  73. | Return of node * ann
  74. (** return value [return <value>;] *)
  75. | Expr of node
  76. (** expression statement [<expr>;] *)
  77. | Block of node list
  78. (** body [{ <body> }] *)
  79. | If of node * node * ann
  80. (** condition, body [if (condition) { body }] *)
  81. | IfElse of node * node * node * ann
  82. (** condition, true_body, false_body [if (<condition>) { <true_body> } else { <false_body> }] *)
  83. | While of node * node * ann
  84. (** condition, body [while (<condition>) { <body> }] *)
  85. | DoWhile of node * node * ann
  86. (** condition, body [do { <body> } whlie (<condition>)] *)
  87. (* Expressions *)
  88. | Const of const * ann
  89. (** constant [bool|int|float constant] *)
  90. | ArrayConst of node list * ann
  91. (** array initialisation [[ <expr> , ... ]] *)
  92. | Var of string * node list option * ann
  93. (** name, indices [<name> | <name>[<indices>] ] *)
  94. | Monop of operator * node * ann
  95. (** operator, operand [<operator><operand>] *)
  96. | Binop of operator * node * node * ann
  97. (** operator, left, right [<left> <operator> <right>] *)
  98. | TypeCast of ctype * node * ann
  99. (** target_type, value [(target_type)value] *)
  100. | FunCall of string * node list * ann
  101. (** name, args [<name>(<args>)] *)
  102. | Arg of node
  103. (** Function argument wrapper. *)
  104. (* Additional types for convenience in traversals
  105. * Mostly used to annotate existing nodes with information from declarations *)
  106. | Allocate of node * node list * ann
  107. (** declaration, dims [<declaration> = __allocate(<dims>);] *)
  108. | VarUse of node * node list option * ann
  109. (** Replacement for [Var] with declaration. *)
  110. | FunUse of node * node list * ann
  111. (** Replacement for [FunCall] with declaration. *)
  112. | VarLet of node * node list option * node * ann
  113. (** Replacement for [Assign] with declaration. *)
  114. | ArrayScalar of node (* TODO: remove *)
  115. | ArrayInit of node * node list
  116. (** Wrapper for array initalisation with dimensions, used by {!Desug}. *)
  117. | Cond of node * node * node * ann
  118. (** cond, true_expr, false_expr [<cond> ? <true_expr> : <false_expr>]
  119. Used for short-circuit evaluation. *)
  120. | DummyNode
  121. (** Null node, pruned by traversals. *)
  122. (** {2 Assembly instructions} *)
  123. type stack_scope = Glob | Local | Rel of int | Current
  124. type rtn_scope = ExternFun of int | LocalFun of int * string
  125. type instr =
  126. | Comment of string (* # <comment> *)
  127. | InlineComment of instr * string (* <instr> # <comment> *)
  128. | Label of string (* <label>: *)
  129. (* Directives *)
  130. (* .export "<name>" <ret_type> [ <arg_type>; ... ] <label> *)
  131. | Export of string * ctype * ctype list * string
  132. (* .import "<name>" <ret_type> [ <arg_type>; ... ] *)
  133. | Import of string * ctype * ctype list
  134. (* .const <value> *)
  135. | ConstDef of const
  136. (* .global <type> *)
  137. | Global of ctype
  138. | Store of ctype * stack_scope * int (* [ifba]store[ gn] *)
  139. | Load of ctype * stack_scope * int (* [ifb]load[ gn] G *)
  140. | LoadConst of ctype * int (* [ifb]loadc C *)
  141. | LoadImm of const (* [ifb]load_[01tf] <value> *)
  142. (* Operators *)
  143. | Op of operator * ctype (* [ifb]() *)
  144. | Convert of ctype * ctype (* i2f|f2i *)
  145. | Inc of int * int (* iinc L C *)
  146. | Dec of int * int (* idec L C *)
  147. | IncOne of int (* iinc_1 L *)
  148. | DecOne of int (* idec_1 L *)
  149. (* Control flow *)
  150. | RtnInit of stack_scope
  151. | RtnJmp of rtn_scope
  152. | RtnEnter of int
  153. | Ret of ctype
  154. | Branch of bool * string
  155. | Jump of string
  156. (* Stack management *)
  157. | Pop of ctype (* [ifb]pop *)
  158. (* Arrays *)
  159. | NewArray of ctype * int
  160. | LoadArray of ctype
  161. | StoreArray of ctype
  162. | EmptyLine
  163. | DummyInstr
  164. (** {2 General} *)
  165. (* Intermediate representations between phases *)
  166. type intermediate =
  167. | Empty
  168. | FileContent of string * string
  169. | Ast of node
  170. | Assembly of instr list
  171. (** Container for command-line arguments. *)
  172. type args_record = {
  173. mutable infile : string option;
  174. mutable outfile : string option;
  175. mutable verbose : int;
  176. mutable cpp : bool;
  177. mutable optimize : bool;
  178. mutable endphase : string;
  179. }
  180. (** {2 Exceptions} *)
  181. exception LocError of location * string
  182. exception NodeError of node * string
  183. exception CompileError of string
  184. exception EmptyError
  185. exception InvalidNode
  186. exception InvalidInput of string