Skip to content

Commit

Permalink
wasm: add support for GOOS=wasip1
Browse files Browse the repository at this point in the history
  • Loading branch information
aykevl committed Aug 10, 2023
1 parent ab64e21 commit 1725623
Show file tree
Hide file tree
Showing 18 changed files with 55 additions and 14 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,8 @@ tinygo-bench-fast:
# Same thing, except for wasi rather than the current platform.
tinygo-test-wasi:
$(TINYGO) test -target wasi $(TEST_PACKAGES_FAST) $(TEST_PACKAGES_SLOW) ./tests/runtime_wasi
tinygo-test-wasip1:
GOOS=wasip1 GOARCH=wasm $(TINYGO) test $(TEST_PACKAGES_FAST) $(TEST_PACKAGES_SLOW) ./tests/runtime_wasi
tinygo-test-wasi-fast:
$(TINYGO) test -target wasi $(TEST_PACKAGES_FAST) ./tests/runtime_wasi
tinygo-bench-wasi:
Expand Down
1 change: 1 addition & 0 deletions builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func TestClangAttributes(t *testing.T) {
{GOOS: "darwin", GOARCH: "arm64"},
{GOOS: "windows", GOARCH: "amd64"},
{GOOS: "windows", GOARCH: "arm64"},
{GOOS: "wasip1", GOARCH: "wasm"},
} {
name := "GOOS=" + options.GOOS + ",GOARCH=" + options.GOARCH
if options.GOARCH == "arm" {
Expand Down
31 changes: 30 additions & 1 deletion compileopts/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,15 @@ func LoadTarget(options *Options) (*TargetSpec, error) {
default:
return nil, fmt.Errorf("invalid GOARM=%s, must be 5, 6, or 7", options.GOARM)
}
case "wasm":
llvmarch = "wasm32"
default:
llvmarch = options.GOARCH
}
llvmvendor := "unknown"
llvmos := options.GOOS
if llvmos == "darwin" {
switch llvmos {
case "darwin":
// Use macosx* instead of darwin, otherwise darwin/arm64 will refer
// to iOS!
llvmos = "macosx10.12.0"
Expand All @@ -207,6 +210,8 @@ func LoadTarget(options *Options) (*TargetSpec, error) {
llvmos = "macosx11.0.0"
}
llvmvendor = "apple"
case "wasip1":
llvmos = "wasi"
}
// Target triples (which actually have four components, but are called
// triples for historical reasons) have the form:
Expand Down Expand Up @@ -277,6 +282,15 @@ func defaultTarget(goos, goarch, triple string) (*TargetSpec, error) {
case "arm64":
spec.CPU = "generic"
spec.Features = "+neon"
case "wasm":
spec.CPU = "generic"
spec.Features = "+bulk-memory,+nontrapping-fptoint,+sign-ext"
spec.BuildTags = append(spec.BuildTags, "tinygo.wasm")
spec.CFlags = append(spec.CFlags,
"-mbulk-memory",
"-mnontrapping-fptoint",
"-msign-ext",
)
}
if goos == "darwin" {
spec.Linker = "ld.lld"
Expand Down Expand Up @@ -320,6 +334,21 @@ func defaultTarget(goos, goarch, triple string) (*TargetSpec, error) {
"--no-insert-timestamp",
"--no-dynamicbase",
)
} else if goos == "wasip1" {
spec.Scheduler = "asyncify"
spec.Linker = "wasm-ld"
spec.RTLib = "compiler-rt"
spec.Libc = "wasi-libc"
spec.DefaultStackSize = 1024 * 16 // 16kB
spec.LDFlags = append(spec.LDFlags,
"--stack-first",
"--no-demangle",
)
spec.Emulator = "wasmtime --mapdir=/tmp::{tmpDir} {}"
spec.ExtraFiles = append(spec.ExtraFiles,
"src/runtime/asm_tinygowasm.S",
"src/internal/task/task_asyncify_wasm.S",
)
} else {
spec.LDFlags = append(spec.LDFlags, "-no-pie", "-Wl,--gc-sections") // WARNING: clang < 5.0 requires -nopie
}
Expand Down
1 change: 1 addition & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var supportedLinuxArches = map[string]string{
"X86Linux": "linux/386",
"ARMLinux": "linux/arm/6",
"ARM64Linux": "linux/arm64",
"WASIp1": "wasip1/wasm",
}

var sema = make(chan struct{}, runtime.NumCPU())
Expand Down
2 changes: 1 addition & 1 deletion src/os/dir_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build linux && !baremetal && !wasi
//go:build linux && !baremetal && !wasi && !wasip1

package os

Expand Down
2 changes: 1 addition & 1 deletion src/os/dir_wasi.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// fairly similar: we use fdopendir, fdclosedir, and readdir from wasi-libc in
// a similar way that the darwin code uses functions from libc.

//go:build wasi
//go:build wasi || wasip1

package os

Expand Down
2 changes: 1 addition & 1 deletion src/os/exec_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build aix || darwin || dragonfly || freebsd || (js && wasm) || linux || netbsd || openbsd || solaris || windows
//go:build aix || darwin || dragonfly || freebsd || (js && wasm) || linux || netbsd || openbsd || solaris || wasip1 || windows

package os

Expand Down
2 changes: 1 addition & 1 deletion src/os/file_other.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build baremetal || (wasm && !wasi)
//go:build baremetal || (wasm && !wasi && !wasip1)

package os

Expand Down
2 changes: 1 addition & 1 deletion src/os/file_unix.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build darwin || (linux && !baremetal)
//go:build darwin || (linux && !baremetal) || wasip1

// target wasi sets GOOS=linux and thus the +linux build tag,
// even though it doesn't show up in "tinygo info target -wasi"
Expand Down
2 changes: 1 addition & 1 deletion src/os/types_unix.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build darwin || (linux && !baremetal)
//go:build darwin || (linux && !baremetal) || wasip1

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
Expand Down
8 changes: 8 additions & 0 deletions src/runtime/os_wasip1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package runtime

// The actual GOOS=wasip1, as newly added in the Go 1.21 toolchain.
// Previously we supported -target=wasi, but that was essentially faked by using
// linux/arm instead because that was the closest thing that was already
// supported in the Go standard library.

const GOOS = "wasip1"
2 changes: 1 addition & 1 deletion src/runtime/runtime_wasm_js_scheduler.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build wasm && !wasi && !scheduler.none
//go:build wasm && !wasi && !scheduler.none && !wasip1

package runtime

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/runtime_wasm_wasi.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build tinygo.wasm && wasi
//go:build tinygo.wasm && (wasi || wasip1)

package runtime

Expand Down
2 changes: 1 addition & 1 deletion src/syscall/errno_other.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build !wasi && !darwin
//go:build !wasi && !wasip1 && !darwin

package syscall

Expand Down
2 changes: 1 addition & 1 deletion src/syscall/proc_emulated.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build baremetal || wasi || wasm
//go:build baremetal || tinygo.wasm

// This file emulates some process-related functions that are only available
// under a real operating system.
Expand Down
2 changes: 1 addition & 1 deletion src/syscall/proc_hosted.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build !baremetal && !wasi && !wasm
//go:build !baremetal && !tinygo.wasm

// This file assumes there is a libc available that runs on a real operating
// system.
Expand Down
2 changes: 1 addition & 1 deletion src/syscall/syscall_libc.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build darwin || nintendoswitch || wasi
//go:build darwin || nintendoswitch || wasi || wasip1

package syscall

Expand Down
2 changes: 1 addition & 1 deletion src/syscall/syscall_libc_wasi.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build wasi
//go:build wasi || wasip1

package syscall

Expand Down

0 comments on commit 1725623

Please sign in to comment.