Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exec backend #35

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions compiler/compileArg.ml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type t =
; fs_output : string option
; fs_external : bool
; backend : Backend.t option
; backend_flags : string option
; keep_unit_names : bool }

(* TODO: Add these utilities into Fp.re *)
Expand Down Expand Up @@ -188,6 +189,13 @@ let options =
& opt (some (enum backend)) None
& info ["backend"] ~docs:backend_section ~doc)
in
let backend_flags =
let doc = "Tweak backend with some specific flags." in
Arg.(
value
& opt (some string) None
& info ["backend-flags"] ~docs:backend_section ~doc)
in
let build_t
common
set_param
Expand All @@ -200,6 +208,7 @@ let options =
fs_files
fs_output
backend
backend_flags
fs_external
nocmis
profile
Expand Down Expand Up @@ -299,6 +308,7 @@ let options =
; fs_files
; fs_output
; backend
; backend_flags
; fs_external
; nocmis
; output_file
Expand All @@ -320,6 +330,7 @@ let options =
$ fs_files
$ fs_output
$ backend
$ backend_flags
$ fs_external
$ nocmis
$ profile
Expand Down
1 change: 1 addition & 0 deletions compiler/compileArg.mli
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type t =
; fs_output : string option
; fs_external : bool
; backend : Backend.t option
; backend_flags : string option
; keep_unit_names : bool }

val options : t Cmdliner.Term.t
Expand Down
3 changes: 2 additions & 1 deletion compiler/js_of_ocaml.ml
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ let f
; input_file
; output_file
; backend
; backend_flags
; params
; static_env
; dynlink
Expand All @@ -244,7 +245,7 @@ let f
| None -> (module Backend_js : Backend.Backend_implementation)
| Some be -> be
in
Backend.set_backend backend;
Backend.set_backend backend backend_flags;
let use_hashing = common.CommonArg.use_hashing in
let custom_header = common.CommonArg.custom_header in
let hide_compilation_summary = common.CommonArg.hide_compilation_summary in
Expand Down
4 changes: 4 additions & 0 deletions compiler/lib/RehpDriver.re
Original file line number Diff line number Diff line change
Expand Up @@ -662,4 +662,8 @@ let backends = [
Backend_php.compiler_backend_flag(),
(module Backend_php): (module Backend.Backend_implementation),
),
(
Backend_exec.compiler_backend_flag(),
(module Backend_exec): (module Backend.Backend_implementation),
),
];
10 changes: 9 additions & 1 deletion compiler/lib/backend.re
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ module type Rehp_external_implementations = {};
type runtime_getter = unit => Rehp.expression;

module type Backend_implementation = {
/** Initialize backend with flags */
let init: option(string) => unit;
/* module Rehp_external_implementations: Rehp_external_implementations; */
let custom_module_registration:
unit =>
Expand Down Expand Up @@ -201,7 +203,7 @@ type t = (module Backend_implementation);

let current = ref(None);

let set_backend = (backend: (module Backend_implementation)) => {
let set_backend = (backend: (module Backend_implementation), opt_flags) => {
if (current.contents !== None) {
raise(
Invalid_argument(
Expand All @@ -210,12 +212,18 @@ let set_backend = (backend: (module Backend_implementation)) => {
);
};
module NewBackend = (val backend);
NewBackend.init(opt_flags);
current := Some(backend);
VarPrinter.add_reserved(StringSet.elements(NewBackend.keyword()));
VarPrinter.add_reserved(StringSet.elements(NewBackend.provided()));
};

module Current: Backend_implementation = {
let init = opt_flags =>
switch (current^) {
| None => raise(Invalid_argument("Compiler backend not set"))
| Some((module CurrentBackend)) => CurrentBackend.init(opt_flags)
};
let extension = () =>
switch (current^) {
| None => raise(Invalid_argument("Compiler backend not set"))
Expand Down
126 changes: 126 additions & 0 deletions compiler/lib/backend_exec.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
open Stdlib;

type backend_config = {
extension: string,
keywords: list(string),
globals: list(string),
supplied_primitives: list((string, string)),
allow_simplify_ifdecl: bool,
};

type rpc_resp =
| Config(backend_config)
| Output(string);

type rpc_req =
| FetchConfig
| ProcessTree(Rehp.program);


module Subprocess = {
type t = {
ic: in_channel,
oc: out_channel,
};
let create = cmd => {
let (ic, oc) = Unix.open_process(cmd);
{ic, oc};
};
let rpc = (t, req) => {
Marshal.to_channel(t.oc, req, [Marshal.No_sharing]);
flush(t.oc);
let resp : rpc_resp = Marshal.from_channel(t.ic);
resp;
};
let fetch_config = t => {
switch (rpc(t, FetchConfig)) {
| Config(cfg) => cfg
| _ => failwith("unexpected rpc response for FetchConfig")
};
};
let process_tree = (t, tree) => {
switch (rpc(t, ProcessTree(tree))) {
| Output(str) => str
| _ => failwith("unexpected rpc response for ProcessIR")
};
};
};

let config = ref(None);
let subprocess = ref(None);

let get_config = () =>
switch (config^) {
| Some(cfg) => cfg
| None => failwith("init() was not called")
};

let get_subprocess = () =>
switch (subprocess^) {
| Some(sp) => sp
| None => failwith("init() was not called")
};

let init = flags => {
let cmd =
switch (flags) {
| Some(flags) => flags
| None =>
failwith(
"exec backend expects command line for exec passed as backend-flags",
)
};
let sp = Subprocess.create(cmd);
subprocess := Some(sp);
let cfg = Subprocess.fetch_config(sp);
config := Some(cfg);
};

let extension = () => get_config().extension;
let compiler_backend_flag = () => "exec";
let keyword = () =>
List.fold_left(
~f=(acc, x) => StringSet.add(x, acc),
~init=StringSet.empty,
get_config().keywords,
);
let is_ident = () => Backend.Helpers.is_ident(keyword());
let allow_simplify_ifdecl = () => get_config().allow_simplify_ifdecl;
let provided = () =>
List.fold_left(
~f=(acc, x) => StringSet.add(x, acc),
~init=StringSet.empty,
get_config().globals,
);
let object_wrapper = ((), obj: Rehp.expression) => obj;

let output =
(
(),
formatter,
~custom_header as _,
~source_map as _=?,
((rehp, _module_export_metadatas), _linkinfos),
) => {
let output = Subprocess.process_tree(get_subprocess(), rehp);
Pretty_print.start_group(formatter, 0);
Pretty_print.string(formatter, output);
Pretty_print.end_group(formatter);
};

let is_prim_supplied = ((), s) =>
List.assoc_opt(s, get_config().supplied_primitives);

let custom_module_registration = () =>
Some(
(_runtime_getter, module_expression, _module_export_metadatas) => {
Some(Rehp.ECustomRegister(module_expression))
},
);

let custom_module_loader = () =>
Some((_runtime_getter, name) => Some(Rehp.ECustomRequire(name)));

let module_require = () => Some(name => Some(Rehp.ERequire(name)));

let runtime_module_var = () => Rehp.ERuntime;
1 change: 1 addition & 0 deletions compiler/lib/backend_js.re
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ open Stdlib;
module Fp = RehpFp;
module PP = Pretty_print;
let times = Debug.find("times");
let init = _ => ();
let js_keywords =
List.fold_left(
~f=(acc, x) => StringSet.add(x, acc),
Expand Down
1 change: 1 addition & 0 deletions compiler/lib/backend_php.re
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
open Stdlib;
module PP = Pretty_print;
let times = Debug.find("times");
let init = _ => ();
let php_keywords =
List.fold_left(
~f=(acc, x) => StringSet.add(x, acc),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ module.exports = HashBugReproduce;
}} */
module.exports = ((module.exports /*:: : any*/) /*:: :Exports */);

/*____hashes flags: 589793685 bytecode: 4165296732 debug-data: 129913994 primitives: 1058613066*/
/*____hashes flags: 343193019 bytecode: 4165296732 debug-data: 129913994 primitives: 1058613066*/
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ module.exports = HashBugReproduce_Consumer;
module.exports = ((module.exports /*:: : any*/) /*:: :Exports */);
module.exports.getField = module.exports[1];

/*____hashes flags: 589793685 bytecode: 7303456054 debug-data: 1066682977 primitives: 1058613066*/
/*____hashes flags: 343193019 bytecode: 7303456054 debug-data: 1066682977 primitives: 1058613066*/
Original file line number Diff line number Diff line change
Expand Up @@ -1669,4 +1669,4 @@ module.exports.getX29 = module.exports[58];
module.exports.getX28 = module.exports[59];
module.exports.helperUtil = module.exports[60];

/*____hashes flags: 589793685 bytecode: 729515992277 debug-data: 54564383401 primitives: 1058613066*/
/*____hashes flags: 343193019 bytecode: 729515992277 debug-data: 54564383401 primitives: 1058613066*/
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ public static function get() : Vector<dynamic> {
}

}
/*____hashes flags: 1314811087 bytecode: 4165296732 debug-data: 129913994 primitives: 1058613066*/
/*____hashes flags: 1068210421 bytecode: 4165296732 debug-data: 129913994 primitives: 1058613066*/
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ public static function getField(dynamic $o): dynamic {
}

}
/*____hashes flags: 1314811087 bytecode: 7303456054 debug-data: 1066682977 primitives: 1058613066*/
/*____hashes flags: 1068210421 bytecode: 7303456054 debug-data: 1066682977 primitives: 1058613066*/
Original file line number Diff line number Diff line change
Expand Up @@ -1608,4 +1608,4 @@ public static function helperUtil(dynamic $b, dynamic $a): dynamic {
}

}
/*____hashes flags: 1314811087 bytecode: 729515992277 debug-data: 54564383401 primitives: 1058613066*/
/*____hashes flags: 1068210421 bytecode: 729515992277 debug-data: 54564383401 primitives: 1058613066*/
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ module.exports = SeparateCompilation;
}} */
module.exports = ((module.exports /*:: : any*/) /*:: :Exports */);

/*____hashes flags: 589793685 bytecode: 8090041333 debug-data: 389742017 primitives: 1058613066*/
/*____hashes flags: 343193019 bytecode: 8090041333 debug-data: 389742017 primitives: 1058613066*/
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ module.exports = SeparateCompilation;
}} */
module.exports = ((module.exports /*:: : any*/) /*:: :Exports */);

/*____hashes flags: 589793685 bytecode: 4860596054 debug-data: 129913994 primitives: 1058613066*/
/*____hashes flags: 343193019 bytecode: 4860596054 debug-data: 129913994 primitives: 1058613066*/
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ module.exports = SeparateCompilation_SeparateCompilationHelper;
module.exports = ((module.exports /*:: : any*/) /*:: :Exports */);
module.exports.helperVal = module.exports[1];

/*____hashes flags: 589793685 bytecode: 4371475640 debug-data: 1014914789 primitives: 1058613066*/
/*____hashes flags: 343193019 bytecode: 4371475640 debug-data: 1014914789 primitives: 1058613066*/
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ public static function get() : Vector<dynamic> {
}

}
/*____hashes flags: 1314811087 bytecode: 8090041333 debug-data: 389742017 primitives: 1058613066*/
/*____hashes flags: 1068210421 bytecode: 8090041333 debug-data: 389742017 primitives: 1058613066*/
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ public static function get() : Vector<dynamic> {
}

}
/*____hashes flags: 1314811087 bytecode: 4860596054 debug-data: 129913994 primitives: 1058613066*/
/*____hashes flags: 1068210421 bytecode: 4860596054 debug-data: 129913994 primitives: 1058613066*/
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ public static function get() : Vector<dynamic> {
}

}
/*____hashes flags: 1314811087 bytecode: 4371475640 debug-data: 1014914789 primitives: 1058613066*/
/*____hashes flags: 1068210421 bytecode: 4371475640 debug-data: 1014914789 primitives: 1058613066*/
2 changes: 1 addition & 1 deletion rehack_tests/output/my-lib.cma.js/MyLib.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,4 @@ module.exports.get = module.exports[20];
module.exports.construct = module.exports[22];
module.exports.genThisShouldBeAsyncTransformed1 = module.exports[23];

/*____hashes flags: 589793685 bytecode: 90287558315 debug-data: 22114087654 primitives: 314532832 rehp-hash-bust:v4*/
/*____hashes flags: 343193019 bytecode: 90287558315 debug-data: 22114087654 primitives: 314532832 rehp-hash-bust:v4*/
2 changes: 1 addition & 1 deletion rehack_tests/output/my-lib.cma.js/MyLib__.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ module.exports = MyLib;
}} */
module.exports = ((module.exports /*:: : any*/) /*:: :Exports */);

/*____hashes flags: 589793685 bytecode: 4874759914 debug-data: 129913994 primitives: 1058613066 rehp-hash-bust:v4*/
/*____hashes flags: 343193019 bytecode: 4874759914 debug-data: 129913994 primitives: 1058613066 rehp-hash-bust:v4*/
2 changes: 1 addition & 1 deletion rehack_tests/output/my-lib.cma.js/MyLib__MyLibUtility.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ module.exports = MyLib_MyLibUtility;
module.exports = ((module.exports /*:: : any*/) /*:: :Exports */);
module.exports.thisIsAUtilityFunction = module.exports[1];

/*____hashes flags: 589793685 bytecode: 8526643038 debug-data: 1020972089 primitives: 1058613066 rehp-hash-bust:v4*/
/*____hashes flags: 343193019 bytecode: 8526643038 debug-data: 1020972089 primitives: 1058613066 rehp-hash-bust:v4*/
2 changes: 1 addition & 1 deletion rehack_tests/output/my-lib.cma.php/MyLib.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,4 +257,4 @@ public static function genThisShouldBeAsyncTransformed1(): Awaitable<dynamic> {
}

}
/*____hashes flags: 1314811087 bytecode: 90287558315 debug-data: 22114087654 primitives: 314532832 rehp-hash-bust:v4*/
/*____hashes flags: 1068210421 bytecode: 90287558315 debug-data: 22114087654 primitives: 314532832 rehp-hash-bust:v4*/
2 changes: 1 addition & 1 deletion rehack_tests/output/my-lib.cma.php/MyLib__.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ public static function get() : Vector<dynamic> {
}

}
/*____hashes flags: 1314811087 bytecode: 4874759914 debug-data: 129913994 primitives: 1058613066 rehp-hash-bust:v4*/
/*____hashes flags: 1068210421 bytecode: 4874759914 debug-data: 129913994 primitives: 1058613066 rehp-hash-bust:v4*/
2 changes: 1 addition & 1 deletion rehack_tests/output/my-lib.cma.php/MyLib__MyLibUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ public static function thisIsAUtilityFunction(dynamic $param): dynamic {
}

}
/*____hashes flags: 1314811087 bytecode: 8526643038 debug-data: 1020972089 primitives: 1058613066 rehp-hash-bust:v4*/
/*____hashes flags: 1068210421 bytecode: 8526643038 debug-data: 1020972089 primitives: 1058613066 rehp-hash-bust:v4*/