types.mli 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. (** Type definitions for abstract syntax tree, assembly code, and exceptions. *)
  2. (** [(filename, startline, endline, startcol, endcol)]
  3. Location of node in source code. Used for error reporting. *)
  4. type location = string * int * int * int * int
  5. (** All operators supported by CiviC. *)
  6. type operator =
  7. | Neg | Not
  8. | Add | Sub | Mul | Div | Mod
  9. | Eq | Ne | Lt | Le | Gt | Ge
  10. | And | Or
  11. (** Wrappers for constant values. This eliminates the need for separate
  12. [(Bool|Int|Float)Const] nodes, and these values can be passed to {!instr}
  13. constructors as well. *)
  14. type const =
  15. | BoolVal of bool
  16. | IntVal of int
  17. | FloatVal of float
  18. (** Data types supported by CiviC. [ArrayDims] defines an array type with a set
  19. of dimensions. {!Dimreduce} replaces this multi-dimensional type with an
  20. [Array] type, which signifies a one-dimensional array of a certain basic
  21. type. *)
  22. type ctype =
  23. | Void | Bool | Int | Float | Array of ctype
  24. | ArrayDims of ctype * node list
  25. (** Annotations for {!node}. Each node has a list of zero or more annotations
  26. that are gradually added through the compiler phases:
  27. - [Loc] is a location added by the parser.
  28. - [Depth] is the nesting level of a variable or function, and is added by
  29. {!Context}.
  30. - [Type] is a {!ctype} annotation added by {!Typecheck}.
  31. - [Index] is the stack index of a variable declaration, added by {!Index}.
  32. - [LabelName] is also added during index analysis, it holds the label name
  33. of a [FunDef]. *)
  34. and annotation =
  35. | Loc of location
  36. | Depth of int
  37. | Type of ctype
  38. | Index of int
  39. | LabelName of string
  40. (** Shorthand for annotation list used in definition of {!node}, should not be
  41. used elsewhere. *)
  42. and ann = annotation list
  43. (** Abstract Syntax Tree nodes. *)
  44. and node =
  45. (* Global *)
  46. | Program of node list * ann
  47. (** list of declarations *)
  48. | FunDec of ctype * string * node list * ann
  49. (** ret_type, name, params *)
  50. | FunDef of bool * ctype * string * node list * node * ann
  51. (** export, ret_type, name, params, body *)
  52. | GlobalDec of ctype * string * ann
  53. (** type, name *)
  54. | GlobalDef of bool * ctype * string * node option * ann
  55. (** export, type, name, initialisation *)
  56. | Param of ctype * string * ann
  57. (** type, name *)
  58. | Dim of string * ann
  59. (** name *)
  60. | VarDecs of node list
  61. | LocalFuns of node list
  62. (* Statements *)
  63. | VarDec of ctype * string * node option * ann
  64. (** type, name, initialisation *)
  65. | Assign of string * node list option * node * ann
  66. (** name, indices, value *)
  67. | For of string * node * node * node * node * ann
  68. (** counter, start, stop, step, body *)
  69. | Allocate of node * node list * ann
  70. (** declaration, dims *)
  71. | Return of node * ann (** return value [return <value>;] *)
  72. | Expr of node (** expression statement [<expr>;] *)
  73. | Block of node list (** body [{ <body> }] *)
  74. | If of node * node * ann (** condition, body [if (condition) { body }] *)
  75. | IfElse of node * node * node * ann
  76. (** condition, true_body, false_body [if (<condition>) { <true_body> } else { <false_body> }] *)
  77. | While of node * node * ann
  78. (** condition, body [while (<condition>) { <body> }] *)
  79. | DoWhile of node * node * ann
  80. (** condition, body [do { <body> } whlie (<condition>)] *)
  81. (* Expressions *)
  82. | Const of const * ann (** constant [bool|int|float constant] *)
  83. | ArrayConst of node list * ann (** array initialisation [[ <expr> , ... ]] *)
  84. | Var of string * node list option * ann (** name, indices [<name> | <name>[<indices>] ] *)
  85. | Monop of operator * node * ann (** operator, operand [<operator><operand>] *)
  86. | Binop of operator * node * node * ann (** operator, left, right [<left> <operator> <right>] *)
  87. | TypeCast of ctype * node * ann (** target_type, value [(target_type)value] *)
  88. | FunCall of string * node list * ann (** name, args [<name>(<args>)] *)
  89. | Arg of node (** function argument *)
  90. (* Additional types for convenience in traversals
  91. * Mostly used to annotate existing nodes with information from declarations *)
  92. | VarUse of node * node list option * ann (* Same as Var, but with decl. *)
  93. | FunUse of node * node list * ann (* Same as FunCall, but with decl. *)
  94. | VarLet of node * node list option * node * ann (* replacement for Assign *)
  95. | ArrayScalar of node (* (Bool|Int|Float)Const *)
  96. | ArrayInit of node * node list (* Array(Scalar|Const), dimensions *)
  97. | Cond of node * node * node * ann (* cond, true_expr, false_expr *)
  98. | DummyNode (* null node, pruned by traversals *)
  99. type stack_scope = Glob | Local | Rel of int | Current
  100. type rtn_scope = ExternFun of int | LocalFun of int * string
  101. type instr =
  102. | Comment of string (* # <comment> *)
  103. | InlineComment of instr * string (* <instr> # <comment> *)
  104. | Label of string (* <label>: *)
  105. (* Directives *)
  106. (* .export "<name>" <ret_type> [ <arg_type>; ... ] <label> *)
  107. | Export of string * ctype * ctype list * string
  108. (* .import "<name>" <ret_type> [ <arg_type>; ... ] *)
  109. | Import of string * ctype * ctype list
  110. (* .const <value> *)
  111. | ConstDef of const
  112. (* .global <type> *)
  113. | Global of ctype
  114. | Store of ctype * stack_scope * int (* [ifba]store[ gn] *)
  115. | Load of ctype * stack_scope * int (* [ifb]load[ gn] G *)
  116. | LoadConst of ctype * int (* [ifb]loadc C *)
  117. | LoadImm of const (* [ifb]load_[01tf] <value> *)
  118. (* Operators *)
  119. | Op of operator * ctype (* [ifb]() *)
  120. | Convert of ctype * ctype (* i2f|f2i *)
  121. | Inc of int * int (* iinc L C *)
  122. | Dec of int * int (* idec L C *)
  123. | IncOne of int (* iinc_1 L *)
  124. | DecOne of int (* idec_1 L *)
  125. (* Control flow *)
  126. | RtnInit of stack_scope
  127. | RtnJmp of rtn_scope
  128. | RtnEnter of int
  129. | Ret of ctype
  130. | Branch of bool * string
  131. | Jump of string
  132. (* Stack management *)
  133. | Pop of ctype (* [ifb]pop *)
  134. (* Arrays *)
  135. | NewArray of ctype * int
  136. | LoadArray of ctype
  137. | StoreArray of ctype
  138. | EmptyLine
  139. | DummyInstr
  140. (* Intermediate representations between phases *)
  141. type intermediate =
  142. | Empty
  143. | FileContent of string * string
  144. | Ast of node
  145. | Assembly of instr list
  146. (** Container for command-line arguments. *)
  147. type args_record = {
  148. mutable infile : string option;
  149. mutable outfile : string option;
  150. mutable verbose : int;
  151. mutable cpp : bool;
  152. mutable optimize : bool;
  153. mutable endphase : string;
  154. }
  155. (* Exceptions *)
  156. exception LocError of location * string
  157. exception NodeError of node * string
  158. exception CompileError of string
  159. exception EmptyError
  160. exception InvalidNode
  161. exception InvalidInput of string