types.mli 6.9 KB

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