Skip to content

Commit d464b25

Browse files
bnoordhuisandrewrk
authored andcommitted
support --target-arch wasm32 (#1094)
Add wasm32 support to the build-obj, build-exe and build-lib commands of the stage 1 compiler. Wasm64 should work transparently once it's supported in upstream LLVM. To export a function: // lib.zig - for exposition, not necessary for this example pub use @import("add.zig"); // add.zig export fn add(a: i32, b: i32) i32 { return a + b; } To import a function: // cube.zig extern fn square(x: i32) i32; export fn cube(x: i32) i32 { return x * square(x); }
1 parent 7a96355 commit d464b25

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed

build.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ pub fn build(b: *Builder) !void {
6363
exe.addObjectFile(lib);
6464
}
6565
} else {
66+
addCppLib(b, exe, cmake_binary_dir, "embedded_lld_wasm");
6667
addCppLib(b, exe, cmake_binary_dir, "embedded_lld_elf");
6768
addCppLib(b, exe, cmake_binary_dir, "embedded_lld_coff");
6869
addCppLib(b, exe, cmake_binary_dir, "embedded_lld_lib");

src/link.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,19 @@ static void construct_linker_job_elf(LinkJob *lj) {
391391
}
392392
}
393393

394+
static void construct_linker_job_wasm(LinkJob *lj) {
395+
CodeGen *g = lj->codegen;
396+
397+
lj->args.append("--relocatable"); // So lld doesn't look for _start.
398+
lj->args.append("-o");
399+
lj->args.append(buf_ptr(&lj->out_file));
400+
401+
// .o files
402+
for (size_t i = 0; i < g->link_objects.length; i += 1) {
403+
lj->args.append((const char *)buf_ptr(g->link_objects.at(i)));
404+
}
405+
}
406+
394407
//static bool is_target_cyg_mingw(const ZigTarget *target) {
395408
// return (target->os == ZigLLVM_Win32 && target->env_type == ZigLLVM_Cygnus) ||
396409
// (target->os == ZigLLVM_Win32 && target->env_type == ZigLLVM_GNU);
@@ -924,7 +937,7 @@ static void construct_linker_job(LinkJob *lj) {
924937
case ZigLLVM_MachO:
925938
return construct_linker_job_macho(lj);
926939
case ZigLLVM_Wasm:
927-
zig_panic("TODO link wasm");
940+
return construct_linker_job_wasm(lj);
928941
}
929942
}
930943

src/target.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -597,12 +597,15 @@ void resolve_target_object_format(ZigTarget *target) {
597597
case ZigLLVM_tce:
598598
case ZigLLVM_tcele:
599599
case ZigLLVM_thumbeb:
600-
case ZigLLVM_wasm32:
601-
case ZigLLVM_wasm64:
602600
case ZigLLVM_xcore:
603601
target->oformat= ZigLLVM_ELF;
604602
return;
605603

604+
case ZigLLVM_wasm32:
605+
case ZigLLVM_wasm64:
606+
target->oformat = ZigLLVM_Wasm;
607+
return;
608+
606609
case ZigLLVM_ppc:
607610
case ZigLLVM_ppc64:
608611
if (is_os_darwin(target)) {

src/zig_llvm.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ bool ZigLLDLink(ZigLLVM_ObjectFormatType oformat, const char **args, size_t arg_
838838
return lld::mach_o::link(array_ref_args, diag);
839839

840840
case ZigLLVM_Wasm:
841-
assert(false); // TODO ZigLLDLink for Wasm
841+
return lld::wasm::link(array_ref_args, false, diag);
842842
}
843843
assert(false); // unreachable
844844
abort();

0 commit comments

Comments
 (0)