types.ml 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. type location = string * int * int * int * int
  2. let noloc = ("", 0, 0, 0, 0)
  3. type operator = Neg | Not
  4. | Add | Sub | Mul | Div | Mod
  5. | Eq | Ne | Lt | Le | Gt | Ge
  6. | And | Or
  7. type const = BoolVal of bool | IntVal of int | FloatVal of float
  8. type ctype = Void | Bool | Int | Float
  9. | Array of ctype * node list
  10. | ArrayDepth of ctype * int (* TODO: remove? *)
  11. | FlatArray of ctype
  12. and annotation =
  13. | Loc of location
  14. | Depth of int
  15. | Index of int
  16. | Type of ctype
  17. | LabelName of string
  18. and ann = annotation list
  19. and node =
  20. (* Global *)
  21. | Program of node list * ann
  22. (* list of declarations *)
  23. | FunDec of ctype * string * node list * ann
  24. (* ret_type, name, params *)
  25. | FunDef of bool * ctype * string * node list * node * ann
  26. (* export, ret_type, name, params, body *)
  27. | GlobalDec of ctype * string * ann
  28. (* type, name *)
  29. | GlobalDef of bool * ctype * string * node option * ann
  30. (* export, type, name, initialisation? *)
  31. | Param of ctype * string * ann
  32. (* type, name *)
  33. | Dim of string * ann
  34. (* dimension name in array Param *)
  35. | VarDecs of node list
  36. | LocalFuns of node list
  37. (* Statements *)
  38. | VarDec of ctype * string * node option * ann
  39. (* type, name, initialisation? *)
  40. | Assign of string * node list option * node * ann
  41. (* name, indices?, value *)
  42. | For of string * node * node * node * node * ann
  43. (* counter, start, stop, step, body *)
  44. | Allocate of node * node list * ann
  45. (* dec, dims # name = __allocate(dims) *)
  46. | Return of node * ann (* return <value>; *)
  47. | Expr of node (* <expr>; *)
  48. | Block of node list (* { <body> } *)
  49. | If of node * node * ann (* cond, body *)
  50. | IfElse of node * node * node * ann (* cond, true_body, false_body *)
  51. | While of node * node * ann (* cond, body *)
  52. | DoWhile of node * node * ann (* cond, body *)
  53. (* Expressions *)
  54. | Const of const * ann (* bool|int|float value *)
  55. | ArrayConst of node list * ann (* [<exprs>] *)
  56. | Var of string * node list option * ann (* <name> [<indices>]? *)
  57. | Monop of operator * node * ann (* op, operand *)
  58. | Binop of operator * node * node * ann (* op, left, right *)
  59. | TypeCast of ctype * node * ann (* (type) operand *)
  60. | FunCall of string * node list * ann (* name(args) *)
  61. | Arg of node (* function argument *)
  62. (* Additional types for convenience in traversals
  63. * Mostly used to annotate existing nodes with information from declarations *)
  64. | VarUse of node * node list option * ann (* Same as Var, but with decl. *)
  65. | FunUse of node * node list * ann (* Same as FunCall, but with decl. *)
  66. | VarLet of node * node list option * node * ann (* replacement for Assign *)
  67. | ArrayScalar of node (* (Bool|Int|Float)Const *)
  68. | ArrayInit of node * ctype (* Array(Scalar|Const) * dimensions *)
  69. | Cond of node * node * node * ann (* cond, true_expr, false_expr *)
  70. | DummyNode (* null node, pruned by traversals *)
  71. type stack_scope = Glob | Local | Rel of int | Current
  72. type rtn_scope = ExternFun of int | LocalFun of int * string
  73. type instr =
  74. | Comment of string (* # <comment> *)
  75. | InlineComment of instr * string (* <instr> # <comment> *)
  76. | Label of string (* <label>: *)
  77. (* Directives *)
  78. (* .export "<name>" <ret_type> [ <arg_type>; ... ] <label> *)
  79. | Export of string * ctype * ctype list * string
  80. (* .import "<name>" <ret_type> [ <arg_type>; ... ] *)
  81. | Import of string * ctype * ctype list
  82. (* .const <value> *)
  83. | ConstDef of const
  84. (* .global <type> *)
  85. | Global of ctype
  86. | Store of ctype * stack_scope * int (* [ifba]store[ gn] *)
  87. | Load of ctype * stack_scope * int (* [ifb]load[ gn] G *)
  88. | LoadConst of ctype * int (* [ifb]loadc C *)
  89. | LoadImm of const (* [ifb]load_[01tf] <value> *)
  90. (* Operators *)
  91. | Op of operator * ctype (* [ifb]() *)
  92. | Convert of ctype * ctype (* i2f|f2i *)
  93. | Inc of int * int (* iinc L C *)
  94. | Dec of int * int (* idec L C *)
  95. | IncOne of int (* iinc_1 L *)
  96. | DecOne of int (* idec_1 L *)
  97. (* Control flow *)
  98. | RtnInit of stack_scope
  99. | RtnJmp of rtn_scope
  100. | RtnEnter of int
  101. | Ret of ctype
  102. | Branch of bool * string
  103. | Jump of string
  104. (* Stack management *)
  105. | Pop of ctype (* [ifb]pop *)
  106. (* Arrays *)
  107. | NewArray of ctype * int
  108. | LoadArray of ctype
  109. | StoreArray of ctype
  110. | EmptyLine
  111. | DummyInstr
  112. let immediate_consts = [
  113. BoolVal true;
  114. BoolVal false;
  115. IntVal (-1);
  116. IntVal 0;
  117. IntVal 1;
  118. FloatVal 0.0;
  119. FloatVal 1.0;
  120. ]
  121. (* Container for command-line arguments *)
  122. type args_record = {
  123. mutable infile : string option;
  124. mutable outfile : string option;
  125. mutable verbose : int;
  126. mutable cpp : bool;
  127. mutable optimize : bool;
  128. }
  129. (* Default config *)
  130. let verbosity_default = 2 (* TODO: set to 1 when done with debugging *)
  131. let verbosity_debug = 3
  132. (* Commandline args are stored in a global struct
  133. * (yes, it IS dirty, but I don't know how to do this without passin [args] to
  134. * every function) *)
  135. let args = {
  136. infile = None;
  137. outfile = None;
  138. verbose = verbosity_default;
  139. cpp = true;
  140. optimize = true;
  141. }
  142. (* intermediate representations between phases *)
  143. type intermediate =
  144. | Empty
  145. | FileContent of string * string
  146. | Ast of node
  147. | Assembly of instr list
  148. (* exceptions *)
  149. exception LocError of location * string
  150. exception NodeError of node * string
  151. exception CompileError of string
  152. exception EmptyError
  153. exception InvalidNode
  154. exception InvalidInput of string