util.ml 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. open Printf
  2. open Lexing
  3. open Types
  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. (* Constants are marked by a double $$ for recognition during constant
  19. * propagation *)
  20. let fresh_const prefix = fresh_var (prefix ^ "$")
  21. let loc_from_lexpos pstart pend =
  22. let (fname, ystart, yend, xstart, xend) = (
  23. pstart.pos_fname,
  24. pstart.pos_lnum,
  25. pend.pos_lnum,
  26. (pstart.pos_cnum - pstart.pos_bol + 1),
  27. (pend.pos_cnum - pend.pos_bol)
  28. ) in
  29. if ystart = yend && xend < xstart then
  30. (fname, ystart, yend, xstart, xstart)
  31. else
  32. (fname, ystart, yend, xstart, xend)
  33. let rec flatten_blocks lst =
  34. let flatten = flatten_blocks in
  35. let trav = function
  36. | FunDef (export, ret_type, name, params, Block body, loc) ->
  37. FunDef (export, ret_type, name, flatten params, Block (flatten body), loc)
  38. | If (cond, Block body, loc) ->
  39. If (cond, Block (flatten body), loc)
  40. | IfElse (cond, Block tbody, Block fbody, loc) ->
  41. IfElse (cond, Block (flatten tbody), Block (flatten fbody), loc)
  42. | While (cond, Block body, loc) ->
  43. While (cond, Block (flatten body), loc)
  44. | DoWhile (cond, Block body, loc) ->
  45. DoWhile (cond, Block (flatten body), loc)
  46. | For (counter, start, stop, step, Block body, loc) ->
  47. For (counter, start, stop, step, Block (flatten body), loc)
  48. | node -> node
  49. in
  50. match lst with
  51. | [] -> []
  52. | Block nodes :: tl -> flatten nodes @ (flatten tl)
  53. | DummyNode :: tl -> flatten tl
  54. | hd :: tl -> trav hd :: (flatten tl)
  55. (* Default tree transformation
  56. * (node -> node) -> node -> node *)
  57. let transform_children trav node =
  58. let trav_all nodes = List.map trav nodes in
  59. match node with
  60. | Program (decls, loc) ->
  61. Program (flatten_blocks (trav_all decls), loc)
  62. | FunDec (ret_type, name, params, loc) ->
  63. FunDec (ret_type, name, trav_all params, loc)
  64. | FunDef (export, ret_type, name, params, body, loc) ->
  65. FunDef (export, ret_type, name, trav_all params, trav body, loc)
  66. | GlobalDec (ctype, name, loc) ->
  67. GlobalDec (ctype, name, loc)
  68. | GlobalDef (export, ctype, name, Some init, loc) ->
  69. GlobalDef (export, ctype, name, Some (trav init), loc)
  70. | VarDec (ctype, name, Some init, loc) ->
  71. VarDec (ctype, name, Some (trav init), loc)
  72. | Assign (name, None, value, loc) ->
  73. Assign (name, None, trav value, loc)
  74. | Assign (name, Some dims, value, loc) ->
  75. Assign (name, Some (trav_all dims), trav value, loc)
  76. | VarLet (dec, None, value, loc) ->
  77. VarLet (dec, None, trav value, loc)
  78. | VarLet (dec, Some dims, value, loc) ->
  79. VarLet (dec, Some (trav_all dims), trav value, loc)
  80. | Return (value, loc) ->
  81. Return (trav value, loc)
  82. | If (cond, body, loc) ->
  83. If (trav cond, trav body, loc)
  84. | IfElse (cond, true_body, false_body, loc) ->
  85. IfElse (trav cond, trav true_body, trav false_body, loc)
  86. | While (cond, body, loc) ->
  87. While (trav cond, trav body, loc)
  88. | DoWhile (cond, body, loc) ->
  89. DoWhile (trav cond, trav body, loc)
  90. | For (counter, start, stop, step, body, loc) ->
  91. For (counter, trav start, trav stop, trav step, trav body, loc)
  92. | Allocate (name, dims, dec, loc) ->
  93. Allocate (name, trav_all dims, dec, loc)
  94. | Expr value ->
  95. Expr (trav value)
  96. | Block (body) ->
  97. Block (trav_all body)
  98. | Monop (op, value, loc) ->
  99. Monop (op, trav value, loc)
  100. | Binop (op, left, right, loc) ->
  101. Binop (op, trav left, trav right, loc)
  102. | Cond (cond, true_expr, false_expr, loc) ->
  103. Cond (trav cond, trav true_expr, trav false_expr, loc)
  104. | TypeCast (ctype, value, loc) ->
  105. TypeCast (ctype, trav value, loc)
  106. | FunCall (name, args, loc) ->
  107. FunCall (name, trav_all args, loc)
  108. | Arg value ->
  109. Arg (trav value)
  110. | ArrayInit (value, dims) ->
  111. ArrayInit (trav value, dims)
  112. | ArrayScalar value ->
  113. ArrayScalar (trav value)
  114. | Var (dec, Some dims, ann) ->
  115. Var (dec, Some (trav_all dims), ann)
  116. | VarUse (dec, Some dims, ann) ->
  117. VarUse (dec, Some (trav_all dims), ann)
  118. | FunUse (dec, params, ann) ->
  119. FunUse (dec, trav_all params, ann)
  120. | _ -> node
  121. let rec annof = function
  122. | Program (_, ann)
  123. | Param (_, _, ann)
  124. | Dim (_, ann)
  125. | FunDec (_, _, _, ann)
  126. | FunDef (_, _, _, _, _, ann)
  127. | GlobalDec (_, _, ann)
  128. | GlobalDef (_, _, _, _, ann)
  129. | VarDec (_, _, _, ann)
  130. | Assign (_, _, _, ann)
  131. | VarLet (_, _, _, ann)
  132. | Return (_, ann)
  133. | If (_, _, ann)
  134. | IfElse (_, _, _, ann)
  135. | While (_, _, ann)
  136. | DoWhile (_, _, ann)
  137. | For (_, _, _, _, _, ann)
  138. | Allocate (_, _, _, ann)
  139. | BoolConst (_, ann)
  140. | IntConst (_, ann)
  141. | FloatConst (_, ann)
  142. | ArrayConst (_, ann)
  143. | Var (_, _, ann)
  144. | Monop (_, _, ann)
  145. | Binop (_, _, _, ann)
  146. | Cond (_, _, _, ann)
  147. | TypeCast (_, _, ann)
  148. | VarUse (_, _, ann)
  149. | FunUse (_, _, ann)
  150. | FunCall (_, _, ann) -> ann
  151. | ArrayInit (value, _)
  152. | ArrayScalar value
  153. | Expr value
  154. | Arg value -> annof value
  155. | _ -> raise InvalidNode
  156. let locof node =
  157. let rec trav = function
  158. | [] -> noloc
  159. | Loc loc :: tl -> loc
  160. | _ :: tl -> trav tl
  161. in trav (annof node)
  162. let rec depthof node =
  163. let rec trav = function
  164. | [] -> raise InvalidNode
  165. | Depth depth :: tl -> depth
  166. | _ :: tl -> trav tl
  167. in trav (annof node)
  168. let typeof = function
  169. (* Some nodes have their type as property *)
  170. | VarDec (ctype, _, _, _)
  171. | Param (ctype, _, _)
  172. | FunDec (ctype, _, _, _)
  173. | FunDef (_, ctype, _, _, _, _)
  174. | GlobalDec (ctype, _, _)
  175. | GlobalDef (_, ctype, _, _, _)
  176. | TypeCast (ctype, _, _)
  177. -> ctype
  178. (* Other nodes must be annotated during typechecking *)
  179. | node ->
  180. let rec trav = function
  181. | [] -> prerr_string "cannot get type for: "; prt_node node; raise InvalidNode
  182. | Type t :: tl -> t
  183. | _ :: tl -> trav tl
  184. in trav (annof node)
  185. let prerr_loc (fname, ystart, yend, xstart, xend) =
  186. let file = open_in fname in
  187. (* skip lines until the first matched line *)
  188. for i = 1 to ystart - 1 do input_line file done;
  189. (* for each line in `loc`, print the source line with an underline *)
  190. for l = ystart to yend do
  191. let line = input_line file in
  192. let linewidth = String.length line in
  193. let left = if l = ystart then xstart else 1 in
  194. let right = if l = yend then xend else linewidth in
  195. if linewidth > 0 then (
  196. prerr_endline line;
  197. for i = 1 to left - 1 do prerr_char ' ' done;
  198. for i = left to right do prerr_char '^' done;
  199. prerr_endline "";
  200. )
  201. done;
  202. ()
  203. let prerr_loc_msg loc msg verbose =
  204. let (fname, ystart, yend, xstart, xend) = loc in
  205. let line_s = if yend != ystart
  206. then sprintf "lines %d-%d" ystart yend
  207. else sprintf "line %d" ystart
  208. in
  209. let char_s = if xend != xstart || yend != ystart
  210. then sprintf "characters %d-%d" xstart xend
  211. else sprintf "character %d" xstart
  212. in
  213. eprintf "File \"%s\", %s, %s:\n" fname line_s char_s;
  214. eprintf "%s\n" msg;
  215. if verbose >= 2 then prerr_loc loc;
  216. ()
  217. let block_body = function
  218. | Block nodes -> nodes
  219. | _ -> raise InvalidNode
  220. let rec list_size = function
  221. | [] -> 0
  222. | hd :: tl -> 1 + (list_size tl)
  223. let basetypeof node = match typeof node with
  224. | Array (ctype, _)
  225. | ctype -> ctype
  226. let array_depth = function
  227. | Array (_, dims) -> list_size dims
  228. | _ -> raise InvalidNode
  229. let nameof = function
  230. | GlobalDec (_, name, _)
  231. | GlobalDef (_, _, name, _, _)
  232. | FunDec (_, name, _, _)
  233. | FunDef (_, _, name, _, _, _)
  234. | VarDec (_, name, _, _)
  235. | Param (_, name, _)
  236. | Dim (name, _) -> name
  237. | _ -> raise InvalidNode
  238. let optmap f = function
  239. | None -> None
  240. | Some lst -> Some (List.map f lst)
  241. let optmapl f = function
  242. | None -> []
  243. | Some lst -> List.map f lst
  244. let mapi f lst =
  245. let rec trav i = function
  246. | [] -> []
  247. | hd :: tl -> f i hd :: (trav (i + 1) tl)
  248. in trav 0 lst