util.ml 16 KB

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