util.ml 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. open Printf
  2. open Lexing
  3. open Ast
  4. (* Logging functions *)
  5. let prt_line = prerr_endline
  6. let prt_node node = prt_line (Stringify.node2str node)
  7. let log_line verbosity line =
  8. if args.verbose >= verbosity then prt_line line
  9. let log_node verbosity node =
  10. if args.verbose >= verbosity then prt_node node
  11. let dbg_line = log_line verbosity_debug
  12. let dbg_node = log_node verbosity_debug
  13. (* Variable generation *)
  14. let var_counter = ref 0
  15. let fresh_var prefix =
  16. var_counter := !var_counter + 1;
  17. prefix ^ "$" ^ string_of_int !var_counter
  18. let loc_from_lexpos pstart pend =
  19. let (fname, ystart, yend, xstart, xend) = (
  20. pstart.pos_fname,
  21. pstart.pos_lnum,
  22. pend.pos_lnum,
  23. (pstart.pos_cnum - pstart.pos_bol + 1),
  24. (pend.pos_cnum - pend.pos_bol)
  25. ) in
  26. if ystart = yend && xend < xstart then
  27. (fname, ystart, yend, xstart, xstart)
  28. else
  29. (fname, ystart, yend, xstart, xend)
  30. (* Default tree transformation
  31. * (node -> node) -> node -> node *)
  32. let transform_children trav node =
  33. let trav_all nodes = List.map trav nodes in
  34. match node with
  35. | Program (decls, loc) ->
  36. Program (trav_all decls, loc)
  37. | FunDec (ret_type, name, params, loc) ->
  38. FunDec (ret_type, name, trav_all params, loc)
  39. | FunDef (export, ret_type, name, params, body, loc) ->
  40. FunDef (export, ret_type, name, trav_all params, trav body, loc)
  41. | GlobalDec (ctype, name, loc) ->
  42. GlobalDec (ctype, name, loc)
  43. | GlobalDef (export, ctype, name, Some init, loc) ->
  44. GlobalDef (export, ctype, name, Some (trav init), loc)
  45. | VarDec (ctype, name, Some init, loc) ->
  46. VarDec (ctype, name, Some (trav init), loc)
  47. | Assign (name, None, value, loc) ->
  48. Assign (name, None, trav value, loc)
  49. | Assign (name, Some dims, value, loc) ->
  50. Assign (name, Some (trav_all dims), trav value, loc)
  51. | Return (value, loc) ->
  52. Return (trav value, loc)
  53. | If (cond, body, loc) ->
  54. If (trav cond, trav body, loc)
  55. | IfElse (cond, true_body, false_body, loc) ->
  56. IfElse (trav cond, trav true_body, trav false_body, loc)
  57. | While (cond, body, loc) ->
  58. While (trav cond, trav body, loc)
  59. | DoWhile (cond, body, loc) ->
  60. DoWhile (trav cond, trav body, loc)
  61. | For (counter, start, stop, step, body, loc) ->
  62. For (counter, trav start, trav stop, trav step, trav body, loc)
  63. | Allocate (name, dims, dec, loc) ->
  64. Allocate (name, trav_all dims, dec, loc)
  65. | Expr value ->
  66. Expr (trav value)
  67. | Block (body) ->
  68. Block (trav_all body)
  69. | Monop (op, value, loc) ->
  70. Monop (op, trav value, loc)
  71. | Binop (op, left, right, loc) ->
  72. Binop (op, trav left, trav right, loc)
  73. | Cond (cond, true_expr, false_expr, loc) ->
  74. Cond (trav cond, trav true_expr, trav false_expr, loc)
  75. | TypeCast (ctype, value, loc) ->
  76. TypeCast (ctype, trav value, loc)
  77. | FunCall (name, args, loc) ->
  78. FunCall (name, trav_all args, loc)
  79. | Arg value ->
  80. Arg (trav value)
  81. | Deref (name, dims, loc) ->
  82. Deref (name, trav_all dims, loc)
  83. | ArrayInit (value, dims) ->
  84. ArrayInit (trav value, dims)
  85. | ArrayScalar value ->
  86. ArrayScalar (trav value)
  87. | Type (value, ctype) ->
  88. Type (trav value, ctype)
  89. | VarLet (assign, def, depth) ->
  90. VarLet (trav assign, def, depth)
  91. | VarUse (var, def, depth) ->
  92. VarUse (trav var, def, depth)
  93. | FunUse (funcall, def, depth) ->
  94. FunUse (trav funcall, def, depth)
  95. | DimDec node ->
  96. DimDec (trav node)
  97. | _ -> node
  98. (* Default tree transformation
  99. * (node -> node) -> node -> node *)
  100. let rec transform_all trav = function
  101. | [] -> []
  102. | node :: tail -> trav node :: (transform_all trav tail)
  103. let rec locof = function
  104. | Program (_, loc)
  105. | Param (_, _, loc)
  106. | Dim (_, loc)
  107. | FunDec (_, _, _, loc)
  108. | FunDef (_, _, _, _, _, loc)
  109. | GlobalDec (_, _, loc)
  110. | GlobalDef (_, _, _, _, loc)
  111. | VarDec (_, _, _, loc)
  112. | Assign (_, _, _, loc)
  113. | Return (_, loc)
  114. | If (_, _, loc)
  115. | IfElse (_, _, _, loc)
  116. | While (_, _, loc)
  117. | DoWhile (_, _, loc)
  118. | For (_, _, _, _, _, loc)
  119. | Allocate (_, _, _, loc)
  120. | BoolConst (_, loc)
  121. | IntConst (_, loc)
  122. | FloatConst (_, loc)
  123. | ArrayConst (_, loc)
  124. | Var (_, loc)
  125. | Deref (_, _, loc)
  126. | Monop (_, _, loc)
  127. | Binop (_, _, _, loc)
  128. | Cond (_, _, _, loc)
  129. | TypeCast (_, _, loc)
  130. | FunCall (_, _, loc) -> loc
  131. | ArrayInit (value, _)
  132. | ArrayScalar value
  133. | Expr value
  134. | VarLet (value, _, _)
  135. | VarUse (value, _, _)
  136. | FunUse (value, _, _)
  137. | Arg value
  138. | Type (value, _)
  139. | DimDec value -> locof value
  140. | _ -> noloc
  141. let prerr_loc (fname, ystart, yend, xstart, xend) =
  142. let file = open_in fname in
  143. (* skip lines until the first matched line *)
  144. for i = 1 to ystart - 1 do input_line file done;
  145. (* for each line in `loc`, print the source line with an underline *)
  146. for l = ystart to yend do
  147. let line = input_line file in
  148. let linewidth = String.length line in
  149. let left = if l = ystart then xstart else 1 in
  150. let right = if l = yend then xend else linewidth in
  151. if linewidth > 0 then (
  152. prerr_endline line;
  153. for i = 1 to left - 1 do prerr_char ' ' done;
  154. for i = left to right do prerr_char '^' done;
  155. prerr_endline "";
  156. )
  157. done;
  158. ()
  159. let prerr_loc_msg loc msg verbose =
  160. let (fname, ystart, yend, xstart, xend) = loc in
  161. let line_s = if yend != ystart
  162. then sprintf "lines %d-%d" ystart yend
  163. else sprintf "line %d" ystart
  164. in
  165. let char_s = if xend != xstart || yend != ystart
  166. then sprintf "characters %d-%d" xstart xend
  167. else sprintf "character %d" xstart
  168. in
  169. eprintf "File \"%s\", %s, %s:\n" fname line_s char_s;
  170. eprintf "%s\n" msg;
  171. if verbose >= 2 then prerr_loc loc;
  172. ()
  173. let rec flatten_blocks = function
  174. | [] -> []
  175. | Block nodes :: t -> (flatten_blocks nodes) @ (flatten_blocks t)
  176. | h :: t -> h :: (flatten_blocks t)
  177. let ctypeof = function
  178. | VarDec (ctype, _, _, _)
  179. | Param (ctype, _, _)
  180. | FunDec (ctype, _, _, _)
  181. | FunDef (_, ctype, _, _, _, _)
  182. | GlobalDec (ctype, _, _)
  183. | GlobalDef (_, ctype, _, _, _)
  184. | TypeCast (ctype, _, _)
  185. | Type (_, ctype)
  186. -> ctype
  187. | Dim _ | DimDec _ -> Int
  188. | _ -> raise InvalidNode
  189. let block_body = function
  190. | Block nodes -> nodes
  191. | _ -> raise InvalidNode
  192. let rec list_size = function
  193. | [] -> 0
  194. | hd :: tl -> 1 + (list_size tl)
  195. let base_type = function
  196. | Array (ctype, _)
  197. | ctype -> ctype
  198. let array_depth = function
  199. | Array (_, dims) -> list_size dims
  200. | _ -> raise InvalidNode