Skip to content

Commit

Permalink
Just some tbl things. (#1699)
Browse files Browse the repository at this point in the history
  • Loading branch information
Drup authored and mshinwell committed Jul 23, 2018
1 parent b0ebe69 commit 1be47bf
Show file tree
Hide file tree
Showing 58 changed files with 667 additions and 836 deletions.
166 changes: 83 additions & 83 deletions .depend

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,10 @@ OCaml 4.07.0 (10 July 2018)
* GPR#1704: Make Ident.t abstract and immutable.
(Gabriel Radanne, review by Mark Shinwell)

- GPR#1699: Clean up Maps and Sets throughout the compiler.
Remove the Tbl module in favor of dedicated Maps.
(Gabriel Radanne, review by Mark Shinwell)

### Bug fixes:

- MPR#4499, GPR#1479: Use native Windows API to implement Sys.getenv,
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ OCAMLDOC_OPT=$(WITH_OCAMLDOC:=.opt)

UTILS=utils/config.cmo utils/misc.cmo \
utils/identifiable.cmo utils/numbers.cmo utils/arg_helper.cmo \
utils/clflags.cmo utils/tbl.cmo utils/profile.cmo \
utils/clflags.cmo utils/profile.cmo \
utils/terminfo.cmo utils/ccomp.cmo utils/warnings.cmo \
utils/consistbl.cmo \
utils/strongly_connected_components.cmo \
Expand Down
18 changes: 9 additions & 9 deletions asmcomp/amd64/emit.mlp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

(* Emission of Intel x86_64 assembly code *)

open Misc
open Cmm
open Arch
open Proc
Expand All @@ -28,6 +27,7 @@ open Emitaux
open X86_ast
open X86_proc
open X86_dsl
module String = Misc.Stdlib.String

(* [Branch_relaxation] is not used in this file, but is required by
emit.mlp files for certain other targets; the reference here ensures
Expand Down Expand Up @@ -103,11 +103,11 @@ let emit_symbol s = string_of_symbol symbol_prefix s
(* Record symbols used and defined - at the end generate extern for those
used but not defined *)

let symbols_defined = ref StringSet.empty
let symbols_used = ref StringSet.empty
let symbols_defined = ref String.Set.empty
let symbols_used = ref String.Set.empty

let add_def_symbol s = symbols_defined := StringSet.add s !symbols_defined
let add_used_symbol s = symbols_used := StringSet.add s !symbols_used
let add_def_symbol s = symbols_defined := String.Set.add s !symbols_defined
let add_used_symbol s = symbols_used := String.Set.add s !symbols_used

let imp_table = Hashtbl.create 16

Expand Down Expand Up @@ -1114,13 +1114,13 @@ let end_assembly() =

if system = S_win64 then begin
D.comment "External functions";
StringSet.iter
String.Set.iter
(fun s ->
if not (StringSet.mem s !symbols_defined) then
if not (String.Set.mem s !symbols_defined) then
D.extrn (emit_symbol s) NEAR)
!symbols_used;
symbols_used := StringSet.empty;
symbols_defined := StringSet.empty;
symbols_used := String.Set.empty;
symbols_defined := String.Set.empty;
end;

let asm =
Expand Down
55 changes: 28 additions & 27 deletions asmcomp/closure.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ open Lambda
open Switch
open Clambda

module Int = Numbers.Int
module Storer =
Switch.Store
(struct
Expand All @@ -41,9 +42,9 @@ let rec split_list n l =
end

let rec build_closure_env env_param pos = function
[] -> Tbl.empty
[] -> Ident.Map.empty
| id :: rem ->
Tbl.add id (Uprim(Pfield pos, [Uvar env_param], Debuginfo.none))
Ident.Map.add id (Uprim(Pfield pos, [Uvar env_param], Debuginfo.none))
(build_closure_env env_param (pos+1) rem)

(* Auxiliary for accessing globals. We change the name of the global
Expand Down Expand Up @@ -546,7 +547,7 @@ let subst_debuginfo loc dbg =
let rec substitute loc fpc sb rn ulam =
match ulam with
Uvar v ->
begin try Tbl.find v sb with Not_found -> ulam end
begin try Ident.Map.find v sb with Not_found -> ulam end
| Uconst _ -> ulam
| Udirect_apply(lbl, args, dbg) ->
let dbg = subst_debuginfo loc dbg in
Expand All @@ -569,13 +570,13 @@ let rec substitute loc fpc sb rn ulam =
| Ulet(str, kind, id, u1, u2) ->
let id' = Ident.rename id in
Ulet(str, kind, id', substitute loc fpc sb rn u1,
substitute loc fpc (Tbl.add id (Uvar id') sb) rn u2)
substitute loc fpc (Ident.Map.add id (Uvar id') sb) rn u2)
| Uletrec(bindings, body) ->
let bindings1 =
List.map (fun (id, rhs) -> (id, Ident.rename id, rhs)) bindings in
let sb' =
List.fold_right
(fun (id, id', _) s -> Tbl.add id (Uvar id') s)
(fun (id, id', _) s -> Ident.Map.add id (Uvar id') s)
bindings1 sb in
Uletrec(
List.map
Expand Down Expand Up @@ -626,7 +627,7 @@ let rec substitute loc fpc sb rn ulam =
match rn with
| Some rn ->
begin try
Tbl.find nfail rn
Int.Map.find nfail rn
with Not_found ->
fatal_errorf "Closure.split_list: invalid nfail (%d)" nfail
end
Expand All @@ -637,20 +638,20 @@ let rec substitute loc fpc sb rn ulam =
match rn with
| Some rn ->
let new_nfail = next_raise_count () in
new_nfail, Some (Tbl.add nfail new_nfail rn)
new_nfail, Some (Int.Map.add nfail new_nfail rn)
| None -> nfail, rn in
let ids' = List.map Ident.rename ids in
let sb' =
List.fold_right2
(fun id id' s -> Tbl.add id (Uvar id') s)
(fun id id' s -> Ident.Map.add id (Uvar id') s)
ids ids' sb
in
Ucatch(nfail, ids', substitute loc fpc sb rn u1,
substitute loc fpc sb' rn u2)
| Utrywith(u1, id, u2) ->
let id' = Ident.rename id in
Utrywith(substitute loc fpc sb rn u1, id',
substitute loc fpc (Tbl.add id (Uvar id') sb) rn u2)
substitute loc fpc (Ident.Map.add id (Uvar id') sb) rn u2)
| Uifthenelse(u1, u2, u3) ->
begin match substitute loc fpc sb rn u1 with
Uconst (Uconst_ptr n) ->
Expand All @@ -671,11 +672,11 @@ let rec substitute loc fpc sb rn ulam =
| Ufor(id, u1, u2, dir, u3) ->
let id' = Ident.rename id in
Ufor(id', substitute loc fpc sb rn u1, substitute loc fpc sb rn u2, dir,
substitute loc fpc (Tbl.add id (Uvar id') sb) rn u3)
substitute loc fpc (Ident.Map.add id (Uvar id') sb) rn u3)
| Uassign(id, u) ->
let id' =
try
match Tbl.find id sb with Uvar i -> i | _ -> assert false
match Ident.Map.find id sb with Uvar i -> i | _ -> assert false
with Not_found ->
id in
Uassign(id', substitute loc fpc sb rn u)
Expand All @@ -698,10 +699,10 @@ let no_effects = function

let rec bind_params_rec loc fpc subst params args body =
match (params, args) with
([], []) -> substitute loc fpc subst (Some Tbl.empty) body
([], []) -> substitute loc fpc subst (Some Int.Map.empty) body
| (p1 :: pl, a1 :: al) ->
if is_simple_argument a1 then
bind_params_rec loc fpc (Tbl.add p1 a1 subst) pl al body
bind_params_rec loc fpc (Ident.Map.add p1 a1 subst) pl al body
else begin
let p1' = Ident.rename p1 in
let u1, u2 =
Expand All @@ -712,7 +713,7 @@ let rec bind_params_rec loc fpc subst params args body =
a1, Uvar p1'
in
let body' =
bind_params_rec loc fpc (Tbl.add p1 u2 subst) pl al body in
bind_params_rec loc fpc (Ident.Map.add p1 u2 subst) pl al body in
if occurs_var p1 body then Ulet(Immutable, Pgenval, p1', u1, body')
else if no_effects a1 then body'
else Usequence(a1, body')
Expand All @@ -722,7 +723,7 @@ let rec bind_params_rec loc fpc subst params args body =
let bind_params loc fpc params args body =
(* Reverse parameters and arguments to preserve right-to-left
evaluation order (PR#2910). *)
bind_params_rec loc fpc Tbl.empty (List.rev params) (List.rev args) body
bind_params_rec loc fpc Ident.Map.empty (List.rev params) (List.rev args) body

(* Check if a lambda term is ``pure'',
that is without side-effects *and* not containing function definitions *)
Expand Down Expand Up @@ -814,11 +815,11 @@ let excessive_function_nesting_depth = 5
exception NotClosed

let close_approx_var fenv cenv id =
let approx = try Tbl.find id fenv with Not_found -> Value_unknown in
let approx = try Ident.Map.find id fenv with Not_found -> Value_unknown in
match approx with
Value_const c -> make_const c
| approx ->
let subst = try Tbl.find id cenv with Not_found -> Uvar id in
let subst = try Ident.Map.find id cenv with Not_found -> Uvar id in
(subst, approx)

let close_var fenv cenv id =
Expand Down Expand Up @@ -899,7 +900,7 @@ let rec close fenv cenv = function
@ (List.map (fun arg -> Lvar arg ) final_args)
in
let funct_var = Ident.create "funct" in
let fenv = Tbl.add funct_var fapprox fenv in
let fenv = Ident.Map.add funct_var fapprox fenv in
let (new_fun, approx) = close fenv cenv
(Lfunction{
kind = Curried;
Expand Down Expand Up @@ -959,9 +960,9 @@ let rec close fenv cenv = function
(Ulet(Mutable, kind, id, ulam, ubody), abody)
| (_, Value_const _)
when str = Alias || is_pure lam ->
close (Tbl.add id alam fenv) cenv body
close (Ident.Map.add id alam fenv) cenv body
| (_, _) ->
let (ubody, abody) = close (Tbl.add id alam fenv) cenv body in
let (ubody, abody) = close (Ident.Map.add id alam fenv) cenv body in
(Ulet(Immutable, kind, id, ulam, ubody), abody)
end
| Lletrec(defs, body) ->
Expand All @@ -974,14 +975,14 @@ let rec close fenv cenv = function
let clos_ident = Ident.create "clos" in
let fenv_body =
List.fold_right
(fun (id, _pos, approx) fenv -> Tbl.add id approx fenv)
(fun (id, _pos, approx) fenv -> Ident.Map.add id approx fenv)
infos fenv in
let (ubody, approx) = close fenv_body cenv body in
let sb =
List.fold_right
(fun (id, pos, _approx) sb ->
Tbl.add id (Uoffset(Uvar clos_ident, pos)) sb)
infos Tbl.empty in
Ident.Map.add id (Uoffset(Uvar clos_ident, pos)) sb)
infos Ident.Map.empty in
(Ulet(Immutable, Pgenval, clos_ident, clos,
substitute Location.none !Clflags.float_const_prop sb None ubody),
approx)
Expand All @@ -992,7 +993,7 @@ let rec close fenv cenv = function
| (id, lam) :: rem ->
let (udefs, fenv_body) = clos_defs rem in
let (ulam, approx) = close_named fenv cenv id lam in
((id, ulam) :: udefs, Tbl.add id approx fenv_body) in
((id, ulam) :: udefs, Ident.Map.add id approx fenv_body) in
let (udefs, fenv_body) = clos_defs defs in
let (ubody, approx) = close fenv_body cenv body in
(Uletrec(udefs, ubody), approx)
Expand Down Expand Up @@ -1185,7 +1186,7 @@ and close_functions fenv cenv fun_defs =
let fenv_rec =
List.fold_right
(fun (id, _params, _body, fundesc, _dbg) fenv ->
Tbl.add id (Value_closure(fundesc, Value_unknown)) fenv)
Ident.Map.add id (Value_closure(fundesc, Value_unknown)) fenv)
uncurried_defs fenv in
(* Determine the offsets of each function's closure in the shared block *)
let env_pos = ref (-1) in
Expand All @@ -1208,7 +1209,7 @@ and close_functions fenv cenv fun_defs =
let cenv_body =
List.fold_right2
(fun (id, _params, _body, _fundesc, _dbg) pos env ->
Tbl.add id (Uoffset(Uvar env_param, pos - env_pos)) env)
Ident.Map.add id (Uoffset(Uvar env_param, pos - env_pos)) env)
uncurried_defs clos_offsets cenv_fv in
let (ubody, approx) = close fenv_rec cenv_body body in
if !useless_env && occurs_var env_param ubody then raise NotClosed;
Expand Down Expand Up @@ -1404,7 +1405,7 @@ let intro size lam =
let id = Compilenv.make_symbol None in
global_approx := Array.init size (fun i -> Value_global_field (id, i));
Compilenv.set_global_approx(Value_tuple !global_approx);
let (ulam, _approx) = close Tbl.empty Tbl.empty lam in
let (ulam, _approx) = close Ident.Map.empty Ident.Map.empty lam in
let opaque =
!Clflags.opaque
|| Env.is_imported_opaque (Compilenv.current_unit_name ())
Expand Down
38 changes: 14 additions & 24 deletions asmcomp/cmmgen.ml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ open Lambda
open Clambda
open Cmm
open Cmx_format
module String = Misc.Stdlib.String

(* Environments used for translation to Cmm. *)

Expand Down Expand Up @@ -2847,20 +2848,14 @@ let transl_function f =

(* Translate all function definitions *)

module StringSet =
Set.Make(struct
type t = string
let compare (x:t) y = compare x y
end)

let rec transl_all_functions already_translated cont =
try
let f = Queue.take functions in
if StringSet.mem f.label already_translated then
if String.Set.mem f.label already_translated then
transl_all_functions already_translated cont
else begin
transl_all_functions
(StringSet.add f.label already_translated)
(String.Set.add f.label already_translated)
((f.dbg, transl_function f) :: cont)
end
with Queue.Empty ->
Expand Down Expand Up @@ -3034,7 +3029,7 @@ let transl_all_functions_and_emit_all_constants cont =
aux already_translated cont translated_functions
in
let cont, translated_functions =
aux StringSet.empty cont []
aux String.Set.empty cont []
in
let translated_functions =
(* Sort functions according to source position *)
Expand Down Expand Up @@ -3424,30 +3419,25 @@ let curry_function arity =
then intermediate_curry_functions arity 0
else [tuplify_function (-arity)]

module Int = Numbers.Int

module IntSet = Set.Make(
struct
type t = int
let compare (x:t) y = compare x y
end)

let default_apply = IntSet.add 2 (IntSet.add 3 IntSet.empty)
let default_apply = Int.Set.add 2 (Int.Set.add 3 Int.Set.empty)
(* These apply funs are always present in the main program because
the run-time system needs them (cf. runtime/<arch>.S) . *)

let generic_functions shared units =
let (apply,send,curry) =
List.fold_left
(fun (apply,send,curry) ui ->
List.fold_right IntSet.add ui.ui_apply_fun apply,
List.fold_right IntSet.add ui.ui_send_fun send,
List.fold_right IntSet.add ui.ui_curry_fun curry)
(IntSet.empty,IntSet.empty,IntSet.empty)
List.fold_right Int.Set.add ui.ui_apply_fun apply,
List.fold_right Int.Set.add ui.ui_send_fun send,
List.fold_right Int.Set.add ui.ui_curry_fun curry)
(Int.Set.empty,Int.Set.empty,Int.Set.empty)
units in
let apply = if shared then apply else IntSet.union apply default_apply in
let accu = IntSet.fold (fun n accu -> apply_function n :: accu) apply [] in
let accu = IntSet.fold (fun n accu -> send_function n :: accu) send accu in
IntSet.fold (fun n accu -> curry_function n @ accu) curry accu
let apply = if shared then apply else Int.Set.union apply default_apply in
let accu = Int.Set.fold (fun n accu -> apply_function n :: accu) apply [] in
let accu = Int.Set.fold (fun n accu -> send_function n :: accu) send accu in
Int.Set.fold (fun n accu -> curry_function n @ accu) curry accu

(* Generate the entry point *)

Expand Down
Loading

0 comments on commit 1be47bf

Please sign in to comment.