types.ml 5.9 KB

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