util.ml 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. open Printf
  2. open Lexing
  3. open Types
  4. let rec repeat s n = if n < 1 then "" else s ^ (repeat s (n - 1))
  5. let expand n text = text ^ repeat " " (n - String.length text)
  6. (* Logging functions *)
  7. let hline = "-----------------------------------------------------------------"
  8. let prt_line = prerr_endline
  9. let prt_node node = prt_line (Stringify.node2str node)
  10. let log_plain_line verbosity line =
  11. if args.verbose >= verbosity then prt_line line
  12. let log_line verbosity line =
  13. log_plain_line verbosity (repeat " " 13 ^ line)
  14. let log_node verbosity node =
  15. if args.verbose >= verbosity then prt_node node
  16. (* Variable generation *)
  17. let var_counter = ref 0
  18. let fresh_var prefix =
  19. var_counter := !var_counter + 1;
  20. prefix ^ "$" ^ string_of_int !var_counter
  21. (* Constants are marked by a double $$ for recognition during constant
  22. * propagation *)
  23. let fresh_const prefix = fresh_var (prefix ^ "$")
  24. let loc_from_lexpos pstart pend =
  25. let (fname, ystart, yend, xstart, xend) = (
  26. pstart.pos_fname,
  27. pstart.pos_lnum,
  28. pend.pos_lnum,
  29. (pstart.pos_cnum - pstart.pos_bol + 1),
  30. (pend.pos_cnum - pend.pos_bol)
  31. ) in
  32. if ystart = yend && xend < xstart then
  33. (fname, ystart, yend, xstart, xstart)
  34. else
  35. (fname, ystart, yend, xstart, xend)
  36. let rec flatten_blocks lst =
  37. let flatten = flatten_blocks in
  38. let rec trav = function
  39. | Block body ->
  40. Block (flatten body)
  41. | FunDef (export, ret_type, name, params, body, ann) ->
  42. FunDef (export, ret_type, name, flatten params, trav body, ann)
  43. | If (cond, body, ann) ->
  44. If (cond, trav body, ann)
  45. | IfElse (cond, tbody, fbody, ann) ->
  46. IfElse (cond, trav tbody, trav fbody, ann)
  47. | While (cond, body, ann) ->
  48. While (cond, trav body, ann)
  49. | DoWhile (cond, body, ann) ->
  50. DoWhile (cond, trav body, ann)
  51. | For (counter, start, stop, step, body, ann) ->
  52. For (counter, start, stop, step, trav body, ann)
  53. | VarDecs decs ->
  54. VarDecs (flatten decs)
  55. | LocalFuns decs ->
  56. LocalFuns (flatten decs)
  57. | node -> node
  58. in
  59. match lst with
  60. | [] -> []
  61. | Block nodes :: tl -> flatten nodes @ (flatten tl)
  62. | DummyNode :: tl -> flatten tl
  63. | hd :: tl -> trav hd :: (flatten tl)
  64. (* Default tree transformation
  65. * (node -> node) -> node -> node *)
  66. let transform_children trav node =
  67. let trav_all nodes = List.map trav nodes in
  68. match node with
  69. | Program (decls, ann) ->
  70. Program (flatten_blocks (trav_all decls), ann)
  71. | FunDec (ret_type, name, params, ann) ->
  72. FunDec (ret_type, name, trav_all params, ann)
  73. | FunDef (export, ret_type, name, params, body, ann) ->
  74. FunDef (export, ret_type, name, trav_all params, trav body, ann)
  75. | GlobalDec (ctype, name, ann) ->
  76. GlobalDec (ctype, name, ann)
  77. | GlobalDef (export, ctype, name, Some init, ann) ->
  78. GlobalDef (export, ctype, name, Some (trav init), ann)
  79. | VarDecs decs ->
  80. VarDecs (trav_all decs)
  81. | LocalFuns funs ->
  82. LocalFuns (trav_all funs)
  83. | VarDec (ctype, name, Some init, ann) ->
  84. VarDec (ctype, name, Some (trav init), ann)
  85. | Assign (name, None, value, ann) ->
  86. Assign (name, None, trav value, ann)
  87. | Assign (name, Some dims, value, ann) ->
  88. Assign (name, Some (trav_all dims), trav value, ann)
  89. | VarLet (dec, None, value, ann) ->
  90. VarLet (dec, None, trav value, ann)
  91. | VarLet (dec, Some dims, value, ann) ->
  92. VarLet (dec, Some (trav_all dims), trav value, ann)
  93. | Return (value, ann) ->
  94. Return (trav value, ann)
  95. | If (cond, body, ann) ->
  96. If (trav cond, trav body, ann)
  97. | IfElse (cond, true_body, false_body, ann) ->
  98. IfElse (trav cond, trav true_body, trav false_body, ann)
  99. | While (cond, body, ann) ->
  100. While (trav cond, trav body, ann)
  101. | DoWhile (cond, body, ann) ->
  102. DoWhile (trav cond, trav body, ann)
  103. | For (counter, start, stop, step, body, ann) ->
  104. For (counter, trav start, trav stop, trav step, trav body, ann)
  105. | Allocate (dec, dims, ann) ->
  106. Allocate (dec, trav_all dims, ann)
  107. | Expr value ->
  108. Expr (trav value)
  109. | Block (body) ->
  110. Block (trav_all body)
  111. | Monop (op, value, ann) ->
  112. Monop (op, trav value, ann)
  113. | Binop (op, left, right, ann) ->
  114. Binop (op, trav left, trav right, ann)
  115. | Cond (cond, true_expr, false_expr, ann) ->
  116. Cond (trav cond, trav true_expr, trav false_expr, ann)
  117. | TypeCast (ctype, value, ann) ->
  118. TypeCast (ctype, trav value, ann)
  119. | FunCall (name, args, ann) ->
  120. FunCall (name, trav_all args, ann)
  121. | Arg value ->
  122. Arg (trav value)
  123. | ArrayInit (value, dims) ->
  124. ArrayInit (trav value, dims)
  125. | ArrayScalar value ->
  126. ArrayScalar (trav value)
  127. | Var (dec, Some dims, ann) ->
  128. Var (dec, Some (trav_all dims), ann)
  129. | VarUse (dec, Some dims, ann) ->
  130. VarUse (dec, Some (trav_all dims), ann)
  131. | FunUse (dec, params, ann) ->
  132. FunUse (dec, trav_all params, ann)
  133. | _ -> node
  134. let annotate a = function
  135. | Program (decls, ann) ->
  136. Program (decls, a :: ann)
  137. | FunDec (ret_type, name, params, ann) ->
  138. FunDec (ret_type, name, params, a :: ann)
  139. | FunDef (export, ret_type, name, params, body, ann) ->
  140. FunDef (export, ret_type, name, params, body, a :: ann)
  141. | GlobalDec (ctype, name, ann) ->
  142. GlobalDec (ctype, name, a :: ann)
  143. | GlobalDef (export, ctype, name, init, ann) ->
  144. GlobalDef (export, ctype, name, init, a :: ann)
  145. | VarDec (ctype, name, init, ann) ->
  146. VarDec (ctype, name, init, a :: ann)
  147. | Assign (name, dims, value, ann) ->
  148. Assign (name, dims, value, a :: ann)
  149. | VarLet (dec, dims, value, ann) ->
  150. VarLet (dec, dims, value, a :: ann)
  151. | Return (value, ann) ->
  152. Return (value, a :: ann)
  153. | If (cond, body, ann) ->
  154. If (cond, body, a :: ann)
  155. | IfElse (cond, true_body, false_body, ann) ->
  156. IfElse (cond, true_body, false_body, a :: ann)
  157. | While (cond, body, ann) ->
  158. While (cond, body, a :: ann)
  159. | DoWhile (cond, body, ann) ->
  160. DoWhile (cond, body, a :: ann)
  161. | For (counter, start, stop, step, body, ann) ->
  162. For (counter, start, stop, step, body, a :: ann)
  163. | Allocate (dec, dims, ann) ->
  164. Allocate (dec, dims, a :: ann)
  165. | Monop (op, value, ann) ->
  166. Monop (op, value, a :: ann)
  167. | Binop (op, left, right, ann) ->
  168. Binop (op, left, right, a :: ann)
  169. | Cond (cond, true_expr, false_expr, ann) ->
  170. Cond (cond, true_expr, false_expr, a :: ann)
  171. | TypeCast (ctype, value, ann) ->
  172. TypeCast (ctype, value, a :: ann)
  173. | FunCall (name, args, ann) ->
  174. FunCall (name, args, a :: ann)
  175. | Arg value ->
  176. Arg (value)
  177. | Var (dec, dims, ann) ->
  178. Var (dec, dims, a :: ann)
  179. | VarUse (dec, dims, ann) ->
  180. VarUse (dec, dims, a :: ann)
  181. | FunUse (dec, params, ann) ->
  182. FunUse (dec, params, a :: ann)
  183. | Const (BoolVal value, ann) ->
  184. Const (BoolVal value, a :: ann)
  185. | Const (IntVal value, ann) ->
  186. Const (IntVal value, a :: ann)
  187. | Const (FloatVal value, ann) ->
  188. Const (FloatVal value, a :: ann)
  189. | ArrayConst (value, ann) ->
  190. ArrayConst (value, a :: ann)
  191. | Param (ctype, name, ann) ->
  192. Param (ctype, name, a :: ann)
  193. | Dim (name, ann) ->
  194. Dim (name, a :: ann)
  195. | _ -> raise InvalidNode
  196. let rec annof = function
  197. | Program (_, ann)
  198. | Param (_, _, ann)
  199. | Dim (_, ann)
  200. | FunDec (_, _, _, ann)
  201. | FunDef (_, _, _, _, _, ann)
  202. | GlobalDec (_, _, ann)
  203. | GlobalDef (_, _, _, _, ann)
  204. | VarDec (_, _, _, ann)
  205. | Assign (_, _, _, ann)
  206. | VarLet (_, _, _, ann)
  207. | Return (_, ann)
  208. | If (_, _, ann)
  209. | IfElse (_, _, _, ann)
  210. | While (_, _, ann)
  211. | DoWhile (_, _, ann)
  212. | For (_, _, _, _, _, ann)
  213. | Allocate (_, _, ann)
  214. | Const (BoolVal _, ann)
  215. | Const (IntVal _, ann)
  216. | Const (FloatVal _, ann)
  217. | ArrayConst (_, ann)
  218. | Var (_, _, ann)
  219. | Monop (_, _, ann)
  220. | Binop (_, _, _, ann)
  221. | Cond (_, _, _, ann)
  222. | TypeCast (_, _, ann)
  223. | VarUse (_, _, ann)
  224. | FunUse (_, _, ann)
  225. | FunCall (_, _, ann) -> ann
  226. | ArrayInit (value, _)
  227. | ArrayScalar value
  228. | Expr value
  229. | Arg value -> annof value
  230. | _ -> raise InvalidNode
  231. let locof node =
  232. let rec trav = function
  233. | [] -> noloc
  234. | Loc loc :: _ -> loc
  235. | _ :: tl -> trav tl
  236. in trav (annof node)
  237. let depthof node =
  238. let rec trav = function
  239. | [] ->
  240. prerr_string "cannot get depth for: ";
  241. prt_node node;
  242. raise InvalidNode
  243. | Depth depth :: _ -> depth
  244. | _ :: tl -> trav tl
  245. in trav (annof node)
  246. let indexof node =
  247. let rec trav = function
  248. | [] ->
  249. prerr_string "cannot get index for: ";
  250. prt_node node;
  251. raise InvalidNode
  252. | Index index :: _ -> index
  253. | _ :: tl -> trav tl
  254. in trav (annof node)
  255. let typeof = function
  256. (* Some nodes have their type as property *)
  257. | VarDec (ctype, _, _, _)
  258. | Param (ctype, _, _)
  259. | FunDec (ctype, _, _, _)
  260. | FunDef (_, ctype, _, _, _, _)
  261. | GlobalDec (ctype, _, _)
  262. | GlobalDef (_, ctype, _, _, _)
  263. | TypeCast (ctype, _, _)
  264. -> ctype
  265. (* Dim nodes are always type Int, and are copied by context analysis before
  266. * they are annotated with Type Int, so this match is necessary *)
  267. | Dim _ -> Int
  268. (* Other nodes must be annotated during typechecking *)
  269. | node ->
  270. let rec trav = function
  271. | [] ->
  272. prerr_string "cannot get type for: ";
  273. prt_node node;
  274. raise InvalidNode
  275. | Type t :: _ -> t
  276. | _ :: tl -> trav tl
  277. in trav (annof node)
  278. let labelof node =
  279. let rec trav = function
  280. | [] ->
  281. prerr_string "cannot get label for: ";
  282. prt_node node;
  283. raise InvalidNode
  284. | LabelName label :: _ -> label
  285. | _ :: tl -> trav tl
  286. in trav (annof node)
  287. let const_type = function
  288. | BoolVal _ -> Bool
  289. | IntVal _ -> Int
  290. | FloatVal _ -> Float
  291. (*
  292. let get_line str n =
  293. let rec find_start from = function
  294. | n when n < 1 -> raise (Invalid_argument "n")
  295. | 1 -> from
  296. | n -> find_start ((String.index_from str from '\n') + 1) (n - 1)
  297. in
  298. let linestart = find_start 0 n in
  299. let len = String.length str in
  300. let lineend =
  301. try String.index_from str linestart '\n'
  302. with Not_found -> len
  303. in
  304. String.sub str linestart (lineend - linestart)
  305. *)
  306. let prerr_loc (fname, ystart, yend, xstart, xend) =
  307. let file = open_in fname in
  308. (* skip lines until the first matched line *)
  309. for i = 1 to ystart - 1 do let _ = input_line file in () done;
  310. (* for each line in `loc`, print the source line with an underline *)
  311. for l = ystart to yend do
  312. let line = input_line file in
  313. let linewidth = String.length line in
  314. let left = if l = ystart then xstart else 1 in
  315. let right = if l = yend then xend else linewidth in
  316. if linewidth > 0 then (
  317. prerr_endline line;
  318. for i = 1 to left - 1 do prerr_char ' ' done;
  319. for i = left to right do prerr_char '^' done;
  320. prerr_endline "";
  321. )
  322. done;
  323. ()
  324. let prerr_loc_msg loc msg =
  325. if args.verbose >= 1 then (
  326. let (fname, ystart, yend, xstart, xend) = loc in
  327. if loc != noloc then (
  328. let line_s = if yend != ystart
  329. then sprintf "lines %d-%d" ystart yend
  330. else sprintf "line %d" ystart
  331. in
  332. let char_s = if xend != xstart || yend != ystart
  333. then sprintf "characters %d-%d" xstart xend
  334. else sprintf "character %d" xstart
  335. in
  336. eprintf "File \"%s\", %s, %s:\n" fname line_s char_s;
  337. );
  338. eprintf "%s\n" msg;
  339. if args.verbose >= 1 && loc != noloc then
  340. try prerr_loc loc
  341. with Sys_error _ -> ()
  342. );
  343. ()
  344. let block_body = function
  345. | Block nodes -> nodes
  346. | _ -> raise InvalidNode
  347. let basetypeof node = match typeof node with
  348. | ArrayDims (ctype, _)
  349. | Array ctype
  350. | ctype -> ctype
  351. let nameof = function
  352. | GlobalDec (_, name, _)
  353. | GlobalDef (_, _, name, _, _)
  354. | FunDec (_, name, _, _)
  355. | FunDef (_, _, name, _, _, _)
  356. | VarDec (_, name, _, _)
  357. | Param (_, name, _)
  358. | Dim (name, _) -> name
  359. | _ -> raise InvalidNode
  360. let optmap f = function
  361. | None -> None
  362. | Some lst -> Some (List.map f lst)
  363. let optmapl f = function
  364. | None -> []
  365. | Some lst -> List.map f lst
  366. let mapi f lst =
  367. let rec trav i = function
  368. | [] -> []
  369. | hd :: tl -> f i hd :: (trav (i + 1) tl)
  370. in trav 0 lst
  371. let is_immediate_const const =
  372. if args.optimize then List.mem const immediate_consts else false
  373. let is_array node = match typeof node with
  374. | ArrayDims _ | Array _ -> true
  375. | _ -> false
  376. let node_warning node msg =
  377. prerr_loc_msg (locof node) ("Warning: " ^ msg)