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

Export all functions with alpha numeric names from entrypoint module #1425

Merged
merged 3 commits into from
Aug 1, 2022

Conversation

paulcadman
Copy link
Collaborator

This is part of the implementation for #1419

This PR adds __attribute__((export_name(<name>)) declarations to every type signature corresponding to every symbol in the entry point module that has an alpha numeric name (so that it can safely be present in the output C file). This is an intermediate step to adding the export information to the juvix.yaml file as described in #1419.

This causes these functions to be present in the export table of the resulting WASM module.

To test this we need to be able to invoke the exported functions using the WASM module. This raises the issue of how we will execute these functions in the Anoma VM and (possibly) in the browser. For this use case it is not appropriate to use the WASM-WASI runtime as these IO functions (providing fd_write, fd_read, proc_exit etc.) are not available in the Anoma VM or in the browser.

So this PR also adds a new standalone runtime for the compiler, in addition to the WASI standalone and the WASI libc runtimes. This runtime is appropriate for compiling WASM libraries which export functions that can be invoked without the WASI imports (and that do not, for example, have a main entry point). This is the runtime we'll use for compiling validity predicates for example.

To try this runtime out:

cd tests/positive/MiniC/ExportName
juvix compile --runtime standalone Input.juvix

Then use wasmer to inspect the output WASM file, see it exports all the functions in scope in the module:

wasmer inspect Input.wasm
Type: wasm
Size: 169 B
Imports:
  Functions:
  Memories:
  Tables:
  Globals:
Exports:
  Functions:
    "one": [] -> [I32]
    "two": [] -> [I32]
    "three": [] -> [I32]
    "four": [] -> [I32]
    "five": [] -> [I32]
    "six": [] -> [I32]
    "fun": [] -> [I32]
  Memories:
    "memory": not shared (2 pages..)
  Tables:
  Globals:

We can then invoke the fun function for example:

wasmer run Input.wasm --invoke fun
6

Set the export_name attribute on every function signature that has a
fully alpha numeric name.
WASM libraries we want to run in the browser and in Anoma VM do not have
access to the WASI runtime. For this usecase we need a target runtime
that does not use the WASI runtime.

A `wasi-` prefix is added to existing compile targets and introduce a new
standalone target. This target does not have IO functions like
`putStrLn` and `exit` calls `__builtin_trap` which corresponds to the
`unreachable` WASM instruction.

In the non-exhaustive case error a debug message is emitted to tell the
user which function had the error. This is now abstracted to a `debug`
function in the runtime which calls `putStrLn` in the case of WASI and
is no-op in the case of non-WASI.

Tests are added which check that exported names can be called with the
correct name and result.
@paulcadman paulcadman added this to the 0.2.3 milestone Jul 29, 2022
@paulcadman paulcadman self-assigned this Jul 29, 2022
Copy link
Collaborator

@jonaprieto jonaprieto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants