types.ml 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. and ann = annotation list
  18. and node =
  19. (* Global *)
  20. | Program of node list * ann
  21. (* list of declarations *)
  22. | FunDec of ctype * string * node list * ann
  23. (* ret_type, name, params *)
  24. | FunDef of bool * ctype * string * node list * node * ann
  25. (* export, ret_type, name, params, body *)
  26. | GlobalDec of ctype * string * ann
  27. (* type, name *)
  28. | GlobalDef of bool * ctype * string * node option * ann
  29. (* export, type, name, initialisation? *)
  30. | Param of ctype * string * ann
  31. (* type, name *)
  32. | Dim of string * ann
  33. (* dimension name in array Param *)
  34. | VarDecs of node list
  35. | LocalFuns of node list
  36. (* Statements *)
  37. | VarDec of ctype * string * node option * ann
  38. (* type, name, initialisation? *)
  39. | Assign of string * node list option * node * ann
  40. (* name, indices?, value *)
  41. | For of string * node * node * node * node * ann
  42. (* counter, start, stop, step, body *)
  43. | Allocate of string * node list * node * ann
  44. (* name, dims, decl # name = __allocate(dims) *)
  45. | Return of node * ann (* return <value>; *)
  46. | Expr of node (* <expr>; *)
  47. | Block of node list (* { <body> } *)
  48. | If of node * node * ann (* cond, body *)
  49. | IfElse of node * node * node * ann (* cond, true_body, false_body *)
  50. | While of node * node * ann (* cond, body *)
  51. | DoWhile of node * node * ann (* cond, body *)
  52. (* Expressions *)
  53. | Const of const * ann (* bool|int|float value *)
  54. | ArrayConst of node list * ann (* [<exprs>] *)
  55. | Var of string * node list option * ann (* <name> [<indices>]? *)
  56. | Monop of operator * node * ann (* op, operand *)
  57. | Binop of operator * node * node * ann (* op, left, right *)
  58. | TypeCast of ctype * node * ann (* (type) operand *)
  59. | FunCall of string * node list * ann (* name(args) *)
  60. | Arg of node (* function argument *)
  61. (* Additional types for convenience in traversals
  62. * Mostly used to annotate existing nodes with information from declarations *)
  63. | VarUse of node * node list option * ann (* Same as Var, but with decl. *)
  64. | FunUse of node * node list * ann (* Same as FunCall, but with decl. *)
  65. | VarLet of node * node list option * node * ann (* replacement for Assign *)
  66. | ArrayScalar of node (* (Bool|Int|Float)Const *)
  67. | ArrayInit of node * ctype (* Array(Scalar|Const) * dimensions *)
  68. | Cond of node * node * node * ann (* cond, true_expr, false_expr *)
  69. | DummyNode (* null node, pruned by traversals *)
  70. type instr =
  71. | Comment of string (* # <comment> *)
  72. | InlineComment of instr * string (* <instr> # <comment> *)
  73. | Label of string (* <label>: *)
  74. (* Directives *)
  75. (* .export "<name>" <ret_type> [ <arg_type>; ... ] <label> *)
  76. | Export of string * ctype * ctype list * string
  77. (* .import "<name>" <ret_type> [ <arg_type>; ... ] *)
  78. | Import of string * ctype * ctype list
  79. (* .const <value> *)
  80. | ConstDef of const
  81. (* .global <type> *)
  82. | Global of ctype
  83. | StoreGlob of ctype * int (* [ifba]storeg G *)
  84. | StoreLoc of ctype * int (* [ifba]store L *)
  85. | StoreRel of ctype * int * int (* [ifba]storen N L *)
  86. | LoadGlob of ctype * int (* [ifb]loadg G *)
  87. | LoadLoc of ctype * int (* [ifba]load L *)
  88. | LoadRel of ctype * int * int (* [ifba]loadn N L *)
  89. | LoadConst of ctype * int (* [ifb]loadc C *)
  90. | LoadImm of node (* [ifb]load_[01tf] <value> *)
  91. | Op of operator * ctype (* [ifb]() *)
  92. | Convert of ctype * ctype (* i2f|f2i *)
  93. (* Control flow *)
  94. | RtnEnter of int
  95. | RtnInit
  96. | RtnJmp
  97. | Ret of ctype
  98. (* Instructions *)
  99. | Inc of int * int (* i(inc|dec) L C *)
  100. | IncOne of int (* i(inc|dec)_1 C *)
  101. | EmptyLine
  102. | DummyInstr
  103. (* Container for command-line arguments *)
  104. type args_record = {
  105. mutable infile : string option;
  106. mutable outfile : string option;
  107. mutable verbose : int;
  108. mutable cpp : bool;
  109. mutable optimize : bool;
  110. }
  111. (* Default config *)
  112. let verbosity_default = 2 (* TODO: set to 1 when done with debugging *)
  113. let verbosity_debug = 3
  114. (* Commandline args are stored in a global struct
  115. * (yes, it IS dirty, but I don't know how to do this without passin [args] to
  116. * every function) *)
  117. let args = {
  118. infile = None;
  119. outfile = None;
  120. verbose = verbosity_default;
  121. cpp = true;
  122. optimize = true;
  123. }
  124. (* intermediate representations between phases *)
  125. type intermediate =
  126. | Empty
  127. | FileContent of string * string
  128. | Ast of node
  129. | Assembly of instr list
  130. (* exceptions *)
  131. exception LocError of location * string
  132. exception NodeError of node * string
  133. exception CompileError of string
  134. exception EmptyError
  135. exception InvalidNode
  136. exception InvalidInput of string