jfecher / ante

A safe, easy systems language
http://antelang.org
MIT License
1.9k stars 80 forks source link

Compiling to WASM with Zig #149

Closed iacore closed 1 year ago

iacore commented 1 year ago

Apparently, this just works.

ante -b -i --backend llvm hello.ante 2> hello.ll
zig cc --target=wasm32-wasi hello.ll
wasmtime a.out

Can you add an option that only emit LLVM IR? The rest can be handled by Zig/Clang.

Zig can compile to many platforms quite easily. It does that by linking the correct libc for target platform automatically, including wasi-musl. I think it's better to leave the part after LLVM IR to Zig.

Zig can also compile itself (excluding LLVM) in WASM. It is used for bootstrapping. Maybe useful for embedding inside the Ante compiler?

Zig's supported targets: https://ziglang.org/download/0.10.0/release-notes.html#Support-Table

jfecher commented 1 year ago

The -i/--show-ir option you had should print the llvm ir to stdout (so you should use > hello.ll instead of 2> hello.ll). I can look into directly outputting to a file or cleaning this up a bit (e.g. the cranelift output for -i contains extra debug information) but this should work for your purpose.

Or am I misunderstanding your request?

iacore commented 1 year ago

Currently -i produce the executable as well. I'd like it to only output LLVM IR, and stop there.

I also hope that the Ante compiler can be used together with other LLVM compilers (like clang).

jfecher commented 1 year ago

Pushed an update to remove the --show-ir and --show-hir flags in favor of a --emit ir and --emit hir flags which will emit the ir or hir then stop. You should now be able to use:

ante -eir --backend llvm hello.an > hello.ll

without additional artifacts being generated :). Note that you can also bump up the optimization level to automatically switch over to the llvm backend:

ante -eir -O2 hello.an > hello.ll

One day I think these flags will write to the file system instead of stdout by default but for now this change should make them a bit simpler.