util.ml 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. | VarDec (ctype, name, Some init, ann) ->
  80. VarDec (ctype, name, Some (trav init), ann)
  81. | Assign (name, None, value, ann) ->
  82. Assign (name, None, trav value, ann)
  83. | Assign (name, Some dims, value, ann) ->
  84. Assign (name, Some (trav_all dims), trav value, ann)
  85. | VarLet (dec, None, value, ann) ->
  86. VarLet (dec, None, trav value, ann)
  87. | VarLet (dec, Some dims, value, ann) ->
  88. VarLet (dec, Some (trav_all dims), trav value, ann)
  89. | Return (value, ann) ->
  90. Return (trav value, ann)
  91. | If (cond, body, ann) ->
  92. If (trav cond, trav body, ann)
  93. | IfElse (cond, true_body, false_body, ann) ->
  94. IfElse (trav cond, trav true_body, trav false_body, ann)
  95. | While (cond, body, ann) ->
  96. While (trav cond, trav body, ann)
  97. | DoWhile (cond, body, ann) ->
  98. DoWhile (trav cond, trav body, ann)
  99. | For (counter, start, stop, step, body, ann) ->
  100. For (counter, trav start, trav stop, trav step, trav body, ann)
  101. | Allocate (dec, dims, ann) ->
  102. Allocate (dec, trav_all dims, ann)
  103. | Expr value ->
  104. Expr (trav value)
  105. | Block (body) ->
  106. Block (trav_all body)
  107. | Monop (op, value, ann) ->
  108. Monop (op, trav value, ann)
  109. | Binop (op, left, right, ann) ->
  110. Binop (op, trav left, trav right, ann)
  111. | Cond (cond, true_expr, false_expr, ann) ->
  112. Cond (trav cond, trav true_expr, trav false_expr, ann)
  113. | TypeCast (ctype, value, ann) ->
  114. TypeCast (ctype, trav value, ann)
  115. | FunCall (name, args, ann) ->
  116. FunCall (name, trav_all args, ann)
  117. | Arg value ->
  118. Arg (trav value)
  119. | ArrayInit (value, dims) ->
  120. ArrayInit (trav value, dims)
  121. | ArrayScalar value ->
  122. ArrayScalar (trav value)
  123. | Var (dec, Some dims, ann) ->
  124. Var (dec, Some (trav_all dims), ann)
  125. | VarUse (dec, Some dims, ann) ->
  126. VarUse (dec, Some (trav_all dims), ann)
  127. | FunUse (dec, params, ann) ->
  128. FunUse (dec, trav_all params, ann)
  129. | VarDecs decs ->
  130. VarDecs (trav_all decs)
  131. | LocalFuns funs ->
  132. LocalFuns (trav_all funs)
  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. (* Other nodes must be annotated during typechecking *)
  266. | node ->
  267. let rec trav = function
  268. | [] ->
  269. prerr_string "cannot get type for: ";
  270. prt_node node;
  271. raise InvalidNode
  272. | Type t :: _ -> t
  273. | _ :: tl -> trav tl
  274. in trav (annof node)
  275. let labelof node =
  276. let rec trav = function
  277. | [] ->
  278. prerr_string "cannot get label for: ";
  279. prt_node node;
  280. raise InvalidNode
  281. | LabelName label :: _ -> label
  282. | _ :: tl -> trav tl
  283. in trav (annof node)
  284. let const_type = function
  285. | BoolVal _ -> Bool
  286. | IntVal _ -> Int
  287. | FloatVal _ -> Float
  288. (*
  289. let get_line str n =
  290. let rec find_start from = function
  291. | n when n < 1 -> raise (Invalid_argument "n")
  292. | 1 -> from
  293. | n -> find_start ((String.index_from str from '\n') + 1) (n - 1)
  294. in
  295. let linestart = find_start 0 n in
  296. let len = String.length str in
  297. let lineend =
  298. try String.index_from str linestart '\n'
  299. with Not_found -> len
  300. in
  301. String.sub str linestart (lineend - linestart)
  302. *)
  303. let prerr_loc (fname, ystart, yend, xstart, xend) =
  304. let file = open_in fname in
  305. (* skip lines until the first matched line *)
  306. for i = 1 to ystart - 1 do let _ = input_line file in () done;
  307. (* for each line in `loc`, print the source line with an underline *)
  308. for l = ystart to yend do
  309. let line = input_line file in
  310. let linewidth = String.length line in
  311. let left = if l = ystart then xstart else 1 in
  312. let right = if l = yend then xend else linewidth in
  313. if linewidth > 0 then (
  314. prerr_endline line;
  315. for i = 1 to left - 1 do prerr_char ' ' done;
  316. for i = left to right do prerr_char '^' done;
  317. prerr_endline "";
  318. )
  319. done;
  320. ()
  321. let prerr_loc_msg loc msg =
  322. if args.verbose >= 1 then (
  323. let (fname, ystart, yend, xstart, xend) = loc in
  324. if loc != noloc then (
  325. let line_s = if yend != ystart
  326. then sprintf "lines %d-%d" ystart yend
  327. else sprintf "line %d" ystart
  328. in
  329. let char_s = if xend != xstart || yend != ystart
  330. then sprintf "characters %d-%d" xstart xend
  331. else sprintf "character %d" xstart
  332. in
  333. eprintf "File \"%s\", %s, %s:\n" fname line_s char_s;
  334. );
  335. eprintf "%s\n" msg;
  336. if args.verbose >= 1 && loc != noloc then
  337. try prerr_loc loc
  338. with Sys_error _ -> ()
  339. );
  340. ()
  341. let block_body = function
  342. | Block nodes -> nodes
  343. | _ -> raise InvalidNode
  344. let rec list_size = function
  345. | [] -> 0
  346. | hd :: tl -> 1 + (list_size tl)
  347. let basetypeof node = match typeof node with
  348. | Array (ctype, _)
  349. | FlatArray ctype
  350. | ctype -> ctype
  351. let array_depth = function
  352. | Array (_, dims) -> list_size dims
  353. | _ -> raise InvalidNode
  354. let nameof = function
  355. | GlobalDec (_, name, _)
  356. | GlobalDef (_, _, name, _, _)
  357. | FunDec (_, name, _, _)
  358. | FunDef (_, _, name, _, _, _)
  359. | VarDec (_, name, _, _)
  360. | Param (_, name, _)
  361. | Dim (name, _) -> name
  362. | _ -> raise InvalidNode
  363. let optmap f = function
  364. | None -> None
  365. | Some lst -> Some (List.map f lst)
  366. let optmapl f = function
  367. | None -> []
  368. | Some lst -> List.map f lst
  369. let mapi f lst =
  370. let rec trav i = function
  371. | [] -> []
  372. | hd :: tl -> f i hd :: (trav (i + 1) tl)
  373. in trav 0 lst
  374. let is_immediate_const const =
  375. if args.optimize then List.mem const immediate_consts else false