Skip to content

Commit 6fbc80e

Browse files
mshinwellpoechsel
authored andcommitted
flambda-backend: To upstream: refactor otherlibs/dynlink/, removing byte/ and native/
1 parent 71a03ef commit 6fbc80e

File tree

7 files changed

+167
-181
lines changed

7 files changed

+167
-181
lines changed

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,6 @@ _build
127127
/otherlibs/*/.dep
128128
/otherlibs/dynlink/extract_crc
129129
/otherlibs/dynlink/dynlink_platform_intf.mli
130-
/otherlibs/dynlink/byte/dynlink.mli
131-
/otherlibs/dynlink/native/dynlink.mli
132130
/otherlibs/dynlink/dynlink_compilerlibs/Makefile
133131
/otherlibs/dynlink/dynlink_compilerlibs/*.ml
134132
/otherlibs/dynlink/dynlink_compilerlibs/*.mli

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ natruntop:
636636

637637
# Native dynlink
638638

639-
otherlibs/dynlink/dynlink.cmxa: otherlibs/dynlink/native/dynlink.ml
639+
otherlibs/dynlink/dynlink.cmxa: otherlibs/dynlink/dynlink.ml
640640
$(MAKE) -C otherlibs/dynlink allopt
641641

642642
# The lexer

otherlibs/dynlink/.depend

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
byte/dynlink.cmo : \
1+
dynlink.cmo : \
22
dynlink_types.cmi \
3-
byte/dynlink_compilerlibs.cmi \
3+
dynlink_compilerlibs.cmi \
44
dynlink_common.cmi \
5-
byte/dynlink.cmi
5+
dynlink.cmi
66
dynlink.cmi :
77
dynlink_common.cmo : \
88
dynlink_types.cmi \
99
dynlink_platform_intf.cmi \
10-
byte/dynlink_compilerlibs.cmi \
10+
dynlink_compilerlibs.cmi \
1111
dynlink_common.cmi
1212
dynlink_common.cmi : \
1313
dynlink_platform_intf.cmi
@@ -20,21 +20,21 @@ dynlink_types.cmo : \
2020
dynlink_types.cmi
2121
dynlink_types.cmi :
2222
extract_crc.cmo : \
23-
byte/dynlink_compilerlibs.cmi
23+
dynlink_compilerlibs.cmi
2424
dynlink_common.cmx : \
2525
dynlink_types.cmx \
2626
dynlink_platform_intf.cmx \
27-
native/dynlink_compilerlibs.cmx \
27+
dynlink_compilerlibs.cmx \
2828
dynlink_common.cmi
2929
dynlink_platform_intf.cmx : \
3030
dynlink_types.cmx \
3131
dynlink_platform_intf.cmi
3232
dynlink_types.cmx : \
3333
dynlink_types.cmi
3434
extract_crc.cmx : \
35-
native/dynlink_compilerlibs.cmx
36-
native/dynlink.cmx : \
35+
dynlink_compilerlibs.cmx
36+
dynlink.cmx : \
3737
dynlink_types.cmx \
38-
native/dynlink_compilerlibs.cmx \
38+
dynlink_compilerlibs.cmx \
3939
dynlink_common.cmx \
40-
native/dynlink.cmi
40+
dynlink.cmi

otherlibs/dynlink/Makefile

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,13 @@ ifeq "$(FLAMBDA)" "true"
3737
OPTCOMPFLAGS += -O3
3838
endif
3939

40-
COMPFLAGS += -I byte
41-
OPTCOMPFLAGS += -I native
42-
4340
LOCAL_SRC=dynlink_compilerlibs
4441

45-
OBJS=byte/dynlink_compilerlibs.cmo dynlink_types.cmo \
46-
dynlink_platform_intf.cmo dynlink_common.cmo byte/dynlink.cmo
42+
OBJS=dynlink_compilerlibs.cmo dynlink_types.cmo \
43+
dynlink_platform_intf.cmo dynlink_common.cmo dynlink.cmo
4744

48-
NATOBJS=native/dynlink_compilerlibs.cmx dynlink_types.cmx \
49-
dynlink_platform_intf.cmx dynlink_common.cmx native/dynlink.cmx
45+
NATOBJS=dynlink_compilerlibs.cmx dynlink_types.cmx \
46+
dynlink_platform_intf.cmx dynlink_common.cmx dynlink.cmx
5047

5148
# We need/desire access to compilerlibs for various reasons:
5249
# - The bytecode dynamic linker is in compilerlibs and has many dependencies
@@ -189,17 +186,14 @@ $(LOCAL_SRC)/%.cmx: $(LOCAL_SRC)/%.ml
189186
# Rules for building the [Dynlink_compilerlibs] bytecode and native packs
190187
# from their components.
191188

192-
byte/dynlink_compilerlibs.cmo: $(COMPILERLIBS_CMO)
189+
dynlink_compilerlibs.cmo: $(COMPILERLIBS_CMO)
193190
$(OCAMLC) $(COMPFLAGS) -pack -o $@ $(COMPILERLIBS_CMO)
194191

195-
byte/dynlink_compilerlibs.cmi: byte/dynlink_compilerlibs.cmo
192+
dynlink_compilerlibs.cmi: dynlink_compilerlibs.cmo
196193

197-
native/dynlink_compilerlibs.cmx: $(COMPILERLIBS_CMX)
194+
dynlink_compilerlibs.cmx: $(COMPILERLIBS_CMX)
198195
$(OCAMLOPT) $(COMPFLAGS) $(OPTCOMPFLAGS) -pack -o $@ $(COMPILERLIBS_CMX)
199196

200-
%/dynlink.cmi: dynlink.cmi dynlink.mli
201-
cp $^ $*/
202-
203197
# Rules for building the interface of the [Dynlink_compilerlibs] packs.
204198
# To avoid falling foul of the problem described below, the .cmo and .cmx
205199
# files for the dynlink-specific compilerlibs packs generated here---and in
@@ -214,17 +208,10 @@ all: dynlink.cma $(extract_crc)
214208
allopt: dynlink.cmxa
215209

216210
dynlink.cma: $(OBJS)
217-
$(OCAMLC) $(COMPFLAGS) -ccopt "$(NATDYNLINKOPTS)" -a -I byte -o $@ $^
211+
$(OCAMLC) $(COMPFLAGS) -ccopt "$(NATDYNLINKOPTS)" -a -o $@ $^
218212

219213
dynlink.cmxa: $(NATOBJS)
220-
$(OCAMLOPT) $(COMPFLAGS) -ccopt "$(NATDYNLINKOPTS)" -a -I native \
221-
-o $@ $^
222-
# As for all other .cmxa files, ensure that the .cmx files are in the same
223-
# directory. If this were omitted, ocamldoc in particular will fail to build
224-
# with a -opaque warning. Note that installopt refers to $(NATOBJS) so doesn't
225-
# require this file to exist, hence its inclusion in the recipe for dynlink.cmxa
226-
# rather than as a dependency elsewhere.
227-
cp native/dynlink.cmx dynlink.cmx
214+
$(OCAMLOPT) $(COMPFLAGS) -ccopt "$(NATDYNLINKOPTS)" -a -o $@ $^
228215

229216
# Since there is no .mli for [Dynlink_platform_intf], we need to be
230217
# careful that compilation of the .cmx file does not write the .cmi file again,
@@ -235,7 +222,7 @@ dynlink_platform_intf.mli: dynlink_platform_intf.ml
235222

236223
$(eval $(call PROGRAM_SYNONYM,extract_crc))
237224

238-
$(extract_crc): dynlink.cma byte/dynlink_compilerlibs.cmo extract_crc.cmo
225+
$(extract_crc): dynlink.cma dynlink_compilerlibs.cmo extract_crc.cmo
239226
$(OCAMLC) -o $@ $^
240227

241228
install:
@@ -259,33 +246,28 @@ installopt:
259246

260247
partialclean:
261248
rm -f $(extract_crc) *.cm[ioaxt] *.cmti *.cmxa \
262-
byte/*.cm[iot] byte/*.cmti \
263-
native/*.cm[ixt] native/*.cmti native/*.o native/*.obj \
249+
*.cm[iotx] *.cmti *.o *.obj \
264250
$(LOCAL_SRC)/*.cm[ioaxt] $(LOCAL_SRC)/*.cmti \
265251
$(LOCAL_SRC)/*.o $(LOCAL_SRC)/*.obj
266252

267253
clean: partialclean
268254
rm -f extract_crc extract_crc.exe
269255
rm -f *.a *.lib *.o *.obj *.so *.dll dynlink_platform_intf.mli \
270256
$(LOCAL_SRC)/*.ml $(LOCAL_SRC)/*.mli $(LOCAL_SRC)/Makefile \
271-
$(LOCAL_SRC)/.depend byte/dynlink.mli native/dynlink.mli
257+
$(LOCAL_SRC)/.depend
272258

273259
.PHONY: beforedepend
274260
beforedepend: dynlink_platform_intf.mli
275261

276262
.PHONY: depend
277263
DEPEND_DUMMY_FILES=\
278-
native/dynlink_compilerlibs.ml \
279-
byte/dynlink_compilerlibs.mli \
280-
byte/dynlink.mli \
281-
native/dynlink.mli
264+
dynlink_compilerlibs.ml \
265+
dynlink_compilerlibs.mli
282266

283267
depend: beforedepend
284268
touch $(DEPEND_DUMMY_FILES)
285269
$(CAMLRUN) $(ROOTDIR)/boot/ocamlc -depend -slash \
286-
-I byte -bytecode *.mli *.ml byte/dynlink.ml > .depend
287-
$(CAMLRUN) $(ROOTDIR)/boot/ocamlc -depend -slash \
288-
-I native -native *.ml native/dynlink.ml >> .depend
270+
-bytecode -native *.mli *.ml dynlink.ml > .depend
289271
rm -f $(DEPEND_DUMMY_FILES)
290272

291273
include .depend

otherlibs/dynlink/byte/dynlink.ml renamed to otherlibs/dynlink/dynlink.ml

Lines changed: 138 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,95 @@ module Bytecode = struct
183183
close_in ic
184184
end
185185

186-
include DC.Make (Bytecode)
186+
module B = DC.Make (Bytecode)
187+
188+
type global_map = {
189+
name : string;
190+
crc_intf : Digest.t option;
191+
crc_impl : Digest.t option;
192+
syms : string list
193+
}
194+
195+
module Native = struct
196+
type handle
197+
198+
(* mshinwell: We need something better than caml_sys_exit *)
199+
external ndl_open : string -> bool -> handle * Cmxs_format.dynheader
200+
= "caml_sys_exit" "caml_natdynlink_open"
201+
external ndl_run : handle -> string -> unit
202+
= "caml_sys_exit" "caml_natdynlink_run"
203+
external ndl_getmap : unit -> global_map list
204+
= "caml_sys_exit" "caml_natdynlink_getmap"
205+
external ndl_globals_inited : unit -> int
206+
= "caml_sys_exit" "caml_natdynlink_globals_inited"
207+
external ndl_loadsym : string -> Obj.t
208+
= "caml_sys_exit" "caml_natdynlink_loadsym"
209+
210+
module Unit_header = struct
211+
type t = Cmxs_format.dynunit
212+
213+
let name (t : t) = t.dynu_name
214+
let crc (t : t) = Some t.dynu_crc
215+
216+
let interface_imports (t : t) = t.dynu_imports_cmi
217+
let implementation_imports (t : t) = t.dynu_imports_cmx
218+
219+
let defined_symbols (t : t) = t.dynu_defines
220+
let unsafe_module _t = false
221+
end
222+
223+
let init () = ()
224+
225+
let is_native = true
226+
let adapt_filename f = Filename.chop_extension f ^ ".cmxs"
227+
228+
let num_globals_inited () = ndl_globals_inited ()
229+
230+
let fold_initial_units ~init ~f =
231+
let rank = ref 0 in
232+
List.fold_left (fun acc { name; crc_intf; crc_impl; syms; } ->
233+
rank := !rank + List.length syms;
234+
let implementation =
235+
match crc_impl with
236+
| None -> None
237+
| Some _ as crco -> Some (crco, DT.Check_inited !rank)
238+
in
239+
f acc ~comp_unit:name ~interface:crc_intf
240+
~implementation ~defined_symbols:syms)
241+
init
242+
(ndl_getmap ())
243+
244+
let run_shared_startup handle =
245+
ndl_run handle "_shared_startup"
246+
247+
let run handle ~unit_header ~priv:_ =
248+
List.iter (fun cu ->
249+
try ndl_run handle cu
250+
with exn ->
251+
Printexc.raise_with_backtrace
252+
(DT.Error (Library's_module_initializers_failed exn))
253+
(Printexc.get_raw_backtrace ()))
254+
(Unit_header.defined_symbols unit_header)
255+
256+
let load ~filename ~priv =
257+
let handle, header =
258+
try ndl_open filename (not priv)
259+
with exn -> raise (DT.Error (Cannot_open_dynamic_library exn))
260+
in
261+
if header.dynu_magic <> Config.cmxs_magic_number then begin
262+
raise (DT.Error (Not_a_bytecode_file filename))
263+
end;
264+
handle, header.dynu_units
265+
266+
let unsafe_get_global_value ~bytecode_or_asm_symbol =
267+
match ndl_loadsym bytecode_or_asm_symbol with
268+
| exception _ -> None
269+
| obj -> Some obj
270+
271+
let finish _handle = ()
272+
end
273+
274+
module N = DC.Make (Native)
187275

188276
type linking_error = DT.linking_error =
189277
| Undefined_global of string
@@ -205,3 +293,52 @@ type error = DT.error =
205293

206294
exception Error = DT.Error
207295
let error_message = DT.error_message
296+
297+
let is_native =
298+
match Sys.backend_type with
299+
| Native -> true
300+
| Bytecode | Other _ -> false
301+
302+
let loadfile file =
303+
if is_native then N.loadfile file
304+
else B.loadfile file
305+
306+
let loadfile_private file =
307+
if is_native then N.loadfile_private file
308+
else B.loadfile_private file
309+
310+
let unsafe_get_global_value ~bytecode_or_asm_symbol =
311+
if is_native then N.unsafe_get_global_value ~bytecode_or_asm_symbol
312+
else B.unsafe_get_global_value ~bytecode_or_asm_symbol
313+
314+
let adapt_filename file =
315+
if is_native then N.adapt_filename file
316+
else B.adapt_filename file
317+
318+
let set_allowed_units units =
319+
if is_native then N.set_allowed_units units
320+
else B.set_allowed_units units
321+
322+
let allow_only units =
323+
if is_native then N.allow_only units
324+
else B.allow_only units
325+
326+
let prohibit units =
327+
if is_native then N.prohibit units
328+
else B.prohibit units
329+
330+
let main_program_units units =
331+
if is_native then N.main_program_units units
332+
else B.main_program_units units
333+
334+
let public_dynamically_loaded_units units =
335+
if is_native then N.public_dynamically_loaded_units units
336+
else B.public_dynamically_loaded_units units
337+
338+
let all_units () =
339+
if is_native then N.all_units ()
340+
else B.all_units ()
341+
342+
let allow_unsafe_modules allow =
343+
if is_native then N.allow_unsafe_modules allow
344+
else B.allow_unsafe_modules allow

0 commit comments

Comments
 (0)