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

stage2: add a wasm backend #6056

Merged
merged 5 commits into from
Aug 18, 2020
Merged

stage2: add a wasm backend #6056

merged 5 commits into from
Aug 18, 2020

Conversation

ifreund
Copy link
Member

@ifreund ifreund commented Aug 14, 2020

Thus far, we only generate the type, function, export, and code
sections. These are sufficient to generate and export simple functions.

TODO:

  • Create basic infrastructure for incremental compilation of the wasm container format.
  • Implement trivial codegen
  • Add wasm test cases
  • Fix output files always having .o extension even with -ofmt=wasm

@andrewrk
Copy link
Member

Fix output files always having .o extension even with -ofmt=wasm

This function should be modified to accept an (optional?) ObjectFormat parameter, and it should also be improved to take into account architecture. Wait, is that how it works though? Wasm uses the same extension for both objects and executables? I thought wasm objects were still .o

zig/lib/std/target.zig

Lines 1047 to 1049 in c12a262

pub fn oFileExt(self: Target) [:0]const u8 {
return self.abi.oFileExt();
}

Called here:

.Obj => return std.fmt.allocPrint(allocator, "{}{}", .{ root_name, target.oFileExt() }),

This C logic should be removed in favor of making binNameAlloc aware of .c as an object format:

.yes_default_path => if (object_format != null and object_format.? == .c)
try std.fmt.allocPrint(arena, "{}.c", .{root_name})
else
try std.zig.binNameAlloc(arena, root_name, target_info.target, output_mode, link_mode),

src-self-hosted/codegen/wasm.zig Outdated Show resolved Hide resolved
src-self-hosted/link/Wasm.zig Show resolved Hide resolved
src-self-hosted/link/Wasm.zig Outdated Show resolved Hide resolved
Comment on lines 124 to 126
// TODO: updateDeclExports() may currently be called before updateDecl,
// presumably due to a bug. For now just rely on the following call
// being made in updateDecl().
Copy link
Member

Choose a reason for hiding this comment

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

I'll look into this

Copy link
Member

Choose a reason for hiding this comment

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

I think this is the only change that needs to happen regarding this:

diff --git a/src-self-hosted/link.zig b/src-self-hosted/link.zig
index a835cc6b7..e89210763 100644
--- a/src-self-hosted/link.zig
+++ b/src-self-hosted/link.zig
@@ -186,7 +186,8 @@ pub const File = struct {
         };
     }
 
-    /// Must be called only after a successful call to `updateDecl`.
+    /// Can be called any time before or after the call to `updateDecl`. Same
+    /// with `allocateDeclIndexes`.
     pub fn updateDeclExports(
         base: *File,
         module: *Module,

Happy to discuss other possible ways of doing things

src-self-hosted/link/Wasm.zig Show resolved Hide resolved
@ifreund
Copy link
Member Author

ifreund commented Aug 16, 2020

Discovered a design flaw with regards to incremental compilation. On "freeing" a function decl we immediately mark both the function itself and the corresponding type in the types section as "dead." This breaks down however if the type is overwritten and reused before the function is erased from the binary as this may cause a type mismatch.

One way to solve this would be to keep the type corresponding to each function "alive" until the function is overwritten.

@kubkon kubkon added frontend Tokenization, parsing, AstGen, Sema, and Liveness. arch-wasm 32-bit and 64-bit WebAssembly labels Aug 16, 2020
@ifreund ifreund force-pushed the wasm-backend branch 2 times, most recently from b36c75d to f419ae1 Compare August 17, 2020 22:28
Thus far, we only generate the type, function, export, and code
sections. These are sufficient to generate and export simple functions.

Codegen is currently hardcoded to `i32.const 42`, the main goal of this
commit is to create infrastructure for the container format which will
work with incremental compilation.
We now generate code for returning constants of any of the basic types.
Exports now have a dirty flag and are rewritten on flush if this flag
has been set.

A couple other minor changes have been made based on Andrew's review.
Functions which are free'd are not immediately removed from the binary
as this would cause a shifting of function indexes. Instead, they hang
around until they can be overwritten by a new function. This means that
the types associated with these dead functions must also remain until
the function is overwritten to avoid a type mismatch.
@ifreund ifreund marked this pull request as ready for review August 17, 2020 23:48
@ifreund
Copy link
Member Author

ifreund commented Aug 17, 2020

I think this is ready to merge. The last remaining checkbox is orthogonal to these additions and could easily be done in another PR

@andrewrk andrewrk merged commit 3cc1f8b into ziglang:master Aug 18, 2020
@ifreund ifreund deleted the wasm-backend branch August 18, 2020 08:45
,
// This is what you get when you take the bits of the IEE-754
// representation of 42.0 and reinterpret them as an unsigned
// integer. Guess that's a bug in wasmtime.
Copy link

Choose a reason for hiding this comment

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

Not sure if it's a bug: at least in wasi the main/start function always returns int

@@ -150,6 +150,7 @@ const usage_build_generic =
\\ -ofmt=[mode] Override target object format
\\ elf Executable and Linking Format
\\ c Compile to C source code
\\ wasm WebAssembly
Copy link

Choose a reason for hiding this comment

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

maybe (planned) / (alpha) would be appropriate here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arch-wasm 32-bit and 64-bit WebAssembly frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants