types.ml 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 ctype * 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 node (* [ifb]load_[01tf] <value> *)
  90. | Op of operator * ctype (* [ifb]() *)
  91. | Convert of ctype * ctype (* i2f|f2i *)
  92. (* Control flow *)
  93. | RtnInit of stack_scope
  94. | RtnJmp of rtn_scope
  95. | RtnEnter of int
  96. | Ret of ctype
  97. | Branch of bool * string
  98. | Jump of string
  99. (* Instructions *)
  100. | Inc of int * int (* iinc L C *)
  101. | Dec of int * int (* idec L C *)
  102. (* Stack management *)
  103. | Pop of ctype (* [ifb]pop *)
  104. (* Arrays *)
  105. | NewArray of ctype * int
  106. | LoadArray of ctype
  107. | StoreArray of ctype
  108. | EmptyLine
  109. | DummyInstr
  110. (* Container for command-line arguments *)
  111. type args_record = {
  112. mutable infile : string option;
  113. mutable outfile : string option;
  114. mutable verbose : int;
  115. mutable cpp : bool;
  116. mutable optimize : bool;
  117. }
  118. (* Default config *)
  119. let verbosity_default = 2 (* TODO: set to 1 when done with debugging *)
  120. let verbosity_debug = 3
  121. (* Commandline args are stored in a global struct
  122. * (yes, it IS dirty, but I don't know how to do this without passin [args] to
  123. * every function) *)
  124. let args = {
  125. infile = None;
  126. outfile = None;
  127. verbose = verbosity_default;
  128. cpp = true;
  129. optimize = true;
  130. }
  131. (* intermediate representations between phases *)
  132. type intermediate =
  133. | Empty
  134. | FileContent of string * string
  135. | Ast of node
  136. | Assembly of instr list
  137. (* exceptions *)
  138. exception LocError of location * string
  139. exception NodeError of node * string
  140. exception CompileError of string
  141. exception EmptyError
  142. exception InvalidNode
  143. exception InvalidInput of string