util.ml 13 KB

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