types.ml 4.9 KB

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