titzer / virgil

A fast and lightweight native programming language
1.22k stars 42 forks source link

How to compile programs into WASI? #71

Closed syrusakbary closed 2 years ago

syrusakbary commented 2 years ago

Hi,

I'm starting to trial Virgil a little bit more, and I was unable to get an application compiling to WASI.

Here's what I'm doing (in latest master)

cd apps/HelloWorld/
$ ../../bin/dev/v3c-wasi -run HelloWorld.v3
!EvalUnimplemented: CallAddress
    in main() [HelloWorld.v3 @ 2:20]

I also tried to compile and then run it with Wasmer, but it seems that the Wasm file is not valid:

cd apps/HelloWorld/
$ ../../bin/dev/v3c-wasi HelloWorld.v3
$ wasmer HelloWorld.wasm
error: failed to run `HelloWorld.wasm`
│   1: module instantiation failed (engine: universal, compiler: cranelift)
╰─▶ 2: Validation error: type mismatch: expected i32 but nothing on stack (at offset 409)

Did I miss some steps here? What can I do to help fix the issue?

titzer commented 2 years ago

Ok, let me explain a little to make sense of what's going on.

  1. The scripts in bin/ and /bin/dev are wrappers around the v3c compile command that add command-line options and the requisite runtime files (from rt/<target>/) for a given target.
  2. The latest stable binary is usually a little behind on full features, so generally want to do aeneas bootstrap on a fresh checkout. I suspect the above validation error is because of that.

The first one is a combination of (1) and a documentation failure on my part. Because of (1), running v3c-wasi adds the runtime files associated with the wasi target to the compiler. But the -run option instructs the compiler to run the program in the built-in interpreter. There's a conflict for which of them defines the System component--and the wasi target code wins...which tries to call something even lower-level (probably a wasm import), which doesn't exist. Thus the crash. So you want either v3c -run HelloWorld.v3 or v3c-wasi HelloWorld.v3, but not the combination.

I just tried HelloWorld with v3c-wasi running on wasm3, wizard, and node, and it seems to work.

I'm currently working on a test suite in test/wasi and fixing things as I go.

syrusakbary commented 2 years ago

Oh, I might be missing something.

Sorry if this question is too obvious. I haven't been able to properly run it. I have an Aeneas.jar jar file in /bin/Aeneas.jar. But locally I have no aeneas binary. Is it the same as ./bin/stable/jar/Aeneas ? If not, do you know how I can set it up? (I tried running ./bin/stable/jar/Aeneas boostrap but it seems nothing happened locally)

Note: I'm working in a macOS M1 (so I think the bootstrap will only work through the JVM initially).

titzer commented 2 years ago

No worries. There is a script bin/dev/aeneas. I normally just put both virgil/bin and virgil/bin/dev in my path. All of the executables in these directories figure out their own location and the location of the repository automatically, so you can just run aeneas, v3c, v3c-<target>, etc, and they will find each other and the runtime code without the need for further setup.

Just run aeneas bootstrap and it will generate a new compiler in bin/current and link it from bin/.

titzer commented 2 years ago

And if you are wondering, typically alongside every mydir/Foo.jar generated there is a little script mydir/Foo that just invokes java with the right arguments to run the .jar.

syrusakbary commented 2 years ago

Awesome. I was able to get a basic program compiling. Closing the issue. Thanks!