util.ml 13 KB

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