HigherOrderCO/hvm-64

support compilation/loading of dynamic libraries

Closed this issue · 0 comments

#111 proposes a way to use IO functions in compiled mode, but it is limited to using hvm-core-defined IO, hvm-core readback, etc. Currently, if we wanted to support compiling hvm-lang programs to binaries, hvm-lang would need to have a comparable copy-the-whole-source-of-the-library-into-a-rust-crate-and-build-it script, which isn't really tenable.

Instead, it would be much more desirable if the hvm-core compilation could be modular – i.e. one could compile an hvm-lang book to a native form and then use it in the hvm-lang CLI. Only the definitions in the compiled book would need to be included in the compilation output, and the hvm-lang CLI would supply extraneous things like IO, lambda calc readback, etc.

Thus, I would propose that we support compiling hvm-core code to dynamic libraries (i.e. #![crate_type = "dylib"] in rust terms), and loading dynamic libraries to extend Hosts.

I don't think this would require too many changes. Building off of #111, it would roughly just replace the main.rs file with:

#![crate_type = "dylib"]

#[no_mangle]
pub fn hvmc_dylib_v0__insert_host(host: &mut Host) {
  gen::insert_into_host(host)
}

(it could also disable the cli feature)

Then loading one of these compiled dylibs would look roughly like:

let lib = libloading::Library::new("/path/to/dylib")?;
let insert_host = unsafe { lib.get::<fn(&mut Host)>(b"hvmc_dylib_v0__insert_host\0") };
insert_host(&mut host);
mem::forget(lib); // not sure if this is necessary, but I think it may need to be leaked