Skip to content

Commit d38a4ac

Browse files
hhugovouillon
authored andcommitted
Compiler: cleanup deadcode and small refactoring
1 parent 8a3f396 commit d38a4ac

File tree

17 files changed

+152
-251
lines changed

17 files changed

+152
-251
lines changed

compiler/bin-jsoo_minify/jsoo_minify.ml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ let f { Cmd_arg.common; output_file; use_stdin; files } =
4545
let gen pp =
4646
let pretty = Config.Flag.pretty () in
4747
Pretty_print.set_compact pp (not pretty);
48-
Code.Var.set_pretty pretty;
4948
let error_of_pi pi =
5049
match pi with
5150
| { Parse_info.name = Some src; line; col; _ }
@@ -67,17 +66,6 @@ let f { Cmd_arg.common; output_file; use_stdin; files } =
6766
try p @ Parse_js.parse lex with Parse_js.Parsing_error pi -> error_of_pi pi
6867
else p
6968
in
70-
let free = new Js_traverse.free in
71-
let (_ : Javascript.program) = free#program p in
72-
let toplevel_def_and_use =
73-
let state = free#state in
74-
Javascript.IdentSet.union state.def_var state.use
75-
in
76-
Javascript.IdentSet.iter
77-
(function
78-
| V _ -> ()
79-
| S { name = Utf8_string.Utf8 x; _ } -> Var_printer.add_reserved x)
80-
toplevel_def_and_use;
8169
let true_ () = true in
8270
let open Config in
8371
let passes : ((unit -> bool) * (unit -> Js_traverse.mapper)) list =

compiler/bin-wasm_of_ocaml/compile.ml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,7 @@ let opt_with action x f =
7070
| None -> f None
7171
| Some x -> action x (fun y -> f (Some y))
7272

73-
let output_gen output_file f =
74-
Code.Var.set_pretty true;
75-
Code.Var.set_stable (Config.Flag.stable_var ());
76-
Filename.gen_file output_file f
73+
let output_gen output_file f = Filename.gen_file output_file f
7774

7875
let with_runtime_files ~runtime_wasm_files f =
7976
let inputs =

compiler/lib-wasm/wasm_output.ml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,6 +1173,4 @@ let f ch fields =
11731173

11741174
let string = output_string
11751175
end) in
1176-
Code.Var.set_pretty true;
1177-
Code.Var.set_stable (Config.Flag.stable_var ());
11781176
O.output_module ch fields

compiler/lib/code.ml

Lines changed: 90 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ module Var : sig
6363

6464
val of_idx : int -> t
6565

66-
val to_string : ?origin:t -> t -> string
67-
6866
val fresh : unit -> t
6967

7068
val fresh_n : string -> t
@@ -75,18 +73,14 @@ module Var : sig
7573

7674
val compare : t -> t -> int
7775

78-
val name : t -> string -> unit
76+
val set_name : t -> string -> unit
7977

8078
val get_name : t -> string option
8179

8280
val propagate_name : t -> t -> unit
8381

8482
val reset : unit -> unit
8583

86-
val set_pretty : bool -> unit
87-
88-
val set_stable : bool -> unit
89-
9084
module Set : Set.S with type elt = t
9185

9286
module Map : Map.S with type key = t
@@ -155,27 +149,106 @@ end = struct
155149

156150
let printer = Var_printer.create Var_printer.Alphabet.javascript
157151

152+
module Name = struct
153+
let names = Hashtbl.create 100
154+
155+
let reset () = Hashtbl.clear names
156+
157+
let reserved = Hashtbl.create 100
158+
159+
let () = StringSet.iter (fun s -> Hashtbl.add reserved s ()) Reserved.keyword
160+
161+
let is_reserved s = Hashtbl.mem reserved s
162+
163+
let merge n1 n2 =
164+
match n1, n2 with
165+
| "", n2 -> n2
166+
| n1, "" -> n1
167+
| n1, n2 ->
168+
if generated_name n1
169+
then n2
170+
else if generated_name n2
171+
then n1
172+
else if String.length n1 > String.length n2
173+
then n1
174+
else n2
175+
176+
let set_raw v nm = Hashtbl.replace names v nm
177+
178+
let propagate v v' =
179+
try
180+
let name = Hashtbl.find names v in
181+
match Hashtbl.find names v' with
182+
| exception Not_found -> set_raw v' name
183+
| name' -> set_raw v' (merge name name')
184+
with Not_found -> ()
185+
186+
let set v nm_orig =
187+
let len = String.length nm_orig in
188+
if len > 0
189+
then (
190+
let buf = Buffer.create (String.length nm_orig) in
191+
let idx = ref 0 in
192+
while !idx < len && not (Char.is_letter nm_orig.[!idx]) do
193+
incr idx
194+
done;
195+
let pending = ref false in
196+
if !idx >= len
197+
then (
198+
pending := true;
199+
idx := 0);
200+
for i = !idx to len - 1 do
201+
if Char.is_letter nm_orig.[i] || Char.is_digit nm_orig.[i]
202+
then (
203+
if !pending then Buffer.add_char buf '_';
204+
Buffer.add_char buf nm_orig.[i];
205+
pending := false)
206+
else pending := true
207+
done;
208+
let str = Buffer.contents buf in
209+
let str =
210+
match str, nm_orig with
211+
| "", ">>=" -> "symbol_bind"
212+
| "", ">>|" -> "symbol_map"
213+
| "", "^" -> "symbol_concat"
214+
| "", _ -> "symbol"
215+
| str, _ -> if is_reserved str then str ^ "$" else str
216+
in
217+
(* protect against large names *)
218+
let max_len = 30 in
219+
let str =
220+
if String.length str > max_len then String.sub str ~pos:0 ~len:max_len else str
221+
in
222+
set_raw v str)
223+
224+
let get v = try Some (Hashtbl.find names v) with Not_found -> None
225+
end
226+
158227
let last_var = ref 0
159228

160229
let reset () =
161230
last_var := 0;
162-
Var_printer.reset printer
163-
164-
let to_string ?origin i = Var_printer.to_string printer ?origin i
165-
166-
let print f x = Format.fprintf f "v%d" x
231+
Var_printer.reset printer;
232+
Name.reset ()
167233

168-
(* Format.fprintf f "%s" (to_string x) *)
234+
let print f x =
235+
Format.fprintf
236+
f
237+
"v%d%s"
238+
x
239+
(match Name.get x with
240+
| None -> ""
241+
| Some nm -> "{" ^ nm ^ "}")
169242

170-
let name i nm = Var_printer.name printer i nm
243+
let set_name i nm = Name.set i nm
171244

172245
let fresh () =
173246
incr last_var;
174247
!last_var
175248

176249
let fresh_n nm =
177250
incr last_var;
178-
name !last_var nm;
251+
set_name !last_var nm;
179252
!last_var
180253

181254
let count () = !last_var + 1
@@ -184,13 +257,9 @@ end = struct
184257

185258
let of_idx v = v
186259

187-
let get_name i = Var_printer.get_name printer i
188-
189-
let propagate_name i j = Var_printer.propagate_name printer i j
190-
191-
let set_pretty b = Var_printer.set_pretty printer b
260+
let get_name i = Name.get i
192261

193-
let set_stable b = Var_printer.set_stable printer b
262+
let propagate_name i j = Name.propagate i j
194263

195264
let fork o =
196265
let n = fresh () in

compiler/lib/code.mli

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ module Var : sig
5656

5757
val of_idx : int -> t
5858

59-
val to_string : ?origin:t -> t -> string
60-
6159
val fresh : unit -> t
6260

6361
val fresh_n : string -> t
@@ -70,16 +68,12 @@ module Var : sig
7068

7169
val get_name : t -> string option
7270

73-
val name : t -> string -> unit
71+
val set_name : t -> string -> unit
7472

7573
val propagate_name : t -> t -> unit
7674

7775
val reset : unit -> unit
7876

79-
val set_pretty : bool -> unit
80-
81-
val set_stable : bool -> unit
82-
8377
module Set : Set.S with type elt = t
8478

8579
module Map : Map.S with type key = t

compiler/lib/driver.ml

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -463,8 +463,6 @@ let check_js js =
463463
let missing = StringSet.inter free all_external in
464464
let missing = StringSet.diff missing Reserved.provided in
465465
let other = StringSet.diff free missing in
466-
let res = Var_printer.get_reserved () in
467-
let other = StringSet.diff other res in
468466
if not (StringSet.is_empty missing) then report_missing_primitives missing;
469467
let probably_prov = StringSet.inter other Reserved.provided in
470468
let other = StringSet.diff other probably_prov in
@@ -493,17 +491,6 @@ let name_variables js =
493491
in
494492
let traverse = new Js_traverse.free in
495493
let js = traverse#program js in
496-
let free = traverse#get_free in
497-
Javascript.IdentSet.iter
498-
(fun x ->
499-
match x with
500-
| V _ ->
501-
(* This is an error. We don't complain here as we want
502-
to be able to name other variable to make it
503-
easier to spot the problematic ones *)
504-
()
505-
| S { name = Utf8 x; _ } -> Var_printer.add_reserved x)
506-
free;
507494
let js = Js_assign.program js in
508495
if times () then Format.eprintf " coloring: %a@." Timer.print t;
509496
js
@@ -672,9 +659,7 @@ let simplify_js js =
672659

673660
let configure formatter =
674661
let pretty = Config.Flag.pretty () in
675-
Pretty_print.set_compact formatter (not pretty);
676-
Code.Var.set_pretty (pretty && not (Config.Flag.shortvar ()));
677-
Code.Var.set_stable (Config.Flag.stable_var ())
662+
Pretty_print.set_compact formatter (not pretty)
678663

679664
let link_and_pack ?(standalone = true) ?(wrap_with_fun = `Iife) ?(link = `No) p =
680665
let export_runtime =

compiler/lib/effects.ml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -776,8 +776,7 @@ let cps_transform ~live_vars ~flow_info ~cps_needed p =
776776
Code.fold_closures_innermost_first
777777
p
778778
(fun name_opt params (start, args) ({ Code.blocks; free_pc; _ } as p) ->
779-
Option.iter name_opt ~f:(fun v ->
780-
debug_print "@[<v>cname = %s@,@]" @@ Var.to_string v);
779+
Option.iter name_opt ~f:(fun v -> debug_print "@[<v>cname = %a@,@]" Var.print v);
781780
(* We speculatively add a block at the beginning of the
782781
function. In case of tail-recursion optimization, the
783782
function implementing the loop body may have to be placed
@@ -1175,7 +1174,7 @@ let f ~flow_info ~live_vars p =
11751174
then (
11761175
debug_print "@]";
11771176
debug_print "@[<v>cps_needed (after lifting) = @[<hov 2>";
1178-
Var.Set.iter (fun v -> debug_print "%s,@ " (Var.to_string v)) cps_needed;
1177+
Var.Set.iter (fun v -> debug_print "%a,@ " Var.print v) cps_needed;
11791178
debug_print "@]@,@]";
11801179
debug_print "@[<v>After lambda lifting...@,";
11811180
Code.Print.program (fun _ _ -> "") p;

compiler/lib/generate_closure.ml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,11 @@ module Trampoline = struct
168168
then (
169169
Format.eprintf "Detect cycles of size (%d).\n%!" (List.length all);
170170
Format.eprintf
171-
"%s\n%!"
172-
(String.concat ~sep:", " (List.map all ~f:(fun x -> Var.to_string x))));
171+
"%a\n%!"
172+
(Format.pp_print_list
173+
~pp_sep:(fun fmt () -> Format.pp_print_string fmt ", ")
174+
Var.print)
175+
all);
173176
let tailcall_max_depth = Config.Param.tailcall_max_depth () in
174177
let all =
175178
List.map all ~f:(fun id ->
@@ -182,7 +185,7 @@ module Trampoline = struct
182185
~init:(blocks, free_pc, [])
183186
~f:(fun (blocks, free_pc, closures) (counter, ci) ->
184187
if debug_tc ()
185-
then Format.eprintf "Rewriting for %s\n%!" (Var.to_string ci.f_name);
188+
then Format.eprintf "Rewriting for %a\n%!" Var.print ci.f_name;
186189
let new_f = Code.Var.fork ci.f_name in
187190
let new_args = List.map ci.args ~f:Code.Var.fork in
188191
let wrapper_pc = free_pc in

compiler/lib/js_assign.ml

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -421,19 +421,24 @@ let program' (module Strategy : Strategy) p =
421421
let names = Strategy.allocate_variables state ~count:mapper#get_count in
422422
(* ignore the choosen name for escaping/free [V _] variables *)
423423
Var.Set.iter (fun x -> names.(Var.idx x) <- "") freevar;
424-
let ident = function
425-
| V v as x -> (
426-
if Config.Flag.stable_var ()
427-
then
428-
ident ~var:v (Utf8_string.of_string_exn (Printf.sprintf "v%d" (Code.Var.idx v)))
429-
else
424+
let ident =
425+
if Config.Flag.stable_var ()
426+
then
427+
function
428+
| S _ as x -> x
429+
| V v ->
430+
let name = Printf.sprintf "v%d" (Code.Var.idx v) in
431+
ident ~var:v (Utf8_string.of_string_exn name)
432+
else
433+
function
434+
| S _ as x -> x
435+
| V v as x -> (
430436
let name = names.(Var.idx v) in
431437
match name with
432438
| "" ->
433439
unallocated_names := Var.Set.add v !unallocated_names;
434440
x
435441
| _ -> ident ~var:v (Utf8_string.of_string_exn name))
436-
| S _ as x -> x
437442
in
438443
let label_printer = Var_printer.create Var_printer.Alphabet.javascript in
439444
let max_label_depth = Hashtbl.fold (fun _ d acc -> max d acc) labels 0 in

compiler/lib/js_output.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ struct
242242
| S { name = Utf8 name; var = None; _ } -> PP.string f name
243243
| V v ->
244244
assert accept_unnamed_var;
245-
PP.string f ("<" ^ Code.Var.to_string v ^ ">")
245+
PP.string f (Format.asprintf "<%a>" Code.Var.print v)
246246

247247
let opt_identifier f ~kind i =
248248
match i with

0 commit comments

Comments
 (0)