Skip to content

feat:add new target to support wasm freestanding #3072

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/os/dir_other.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build baremetal || js || wasi || windows
// +build baremetal js wasi windows
//go:build baremetal || js || wasi || windows || wasm_freestanding
// +build baremetal js wasi windows wasm_freestanding

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
Expand Down
4 changes: 2 additions & 2 deletions src/os/dir_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// 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
// +build linux,!baremetal,!wasi
//go:build linux && !baremetal && !wasi && !wasm_freestanding
// +build linux,!baremetal,!wasi,!wasm_freestanding

package os

Expand Down
4 changes: 2 additions & 2 deletions src/os/dirent_linux.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build !baremetal && !js && !wasi
// +build !baremetal,!js,!wasi
//go:build !baremetal && !js && !wasi && !wasm_freestanding
// +build !baremetal,!js,!wasi,!wasm_freestanding

// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
Expand Down
4 changes: 2 additions & 2 deletions src/os/env_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build darwin || linux
// +build darwin linux
//go:build darwin || (linux && !wasm_freestanding)
// +build darwin linux,!wasm_freestanding

package os_test

Expand Down
4 changes: 2 additions & 2 deletions src/os/exec_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// 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
// +build aix darwin dragonfly freebsd js,wasm linux netbsd openbsd solaris windows
//go:build aix || darwin || dragonfly || freebsd || (js && wasm) || (linux && !wasm_freestanding) || netbsd || openbsd || solaris || windows
// +build aix darwin dragonfly freebsd js,wasm linux,!wasm_freestanding netbsd openbsd solaris windows

package os

Expand Down
4 changes: 2 additions & 2 deletions src/os/executable_other.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build !linux || baremetal
// +build !linux baremetal
//go:build !linux || baremetal || wasm_freestanding
// +build !linux baremetal wasm_freestanding

package os

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

//go:build linux && !baremetal
// +build linux,!baremetal
//go:build linux && !baremetal && !wasm_freestanding
// +build linux,!baremetal,!wasm_freestanding

package os

Expand Down
4 changes: 2 additions & 2 deletions src/os/file_anyos.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build !baremetal && !js
// +build !baremetal,!js
//go:build !baremetal && !js && !wasm_freestanding
// +build !baremetal,!js,!wasm_freestanding

// Portions copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
Expand Down
4 changes: 2 additions & 2 deletions src/os/file_anyos_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build !baremetal && !js
// +build !baremetal,!js
//go:build !baremetal && !js && !wasm_freestanding
// +build !baremetal,!js,!wasm_freestanding

package os_test

Expand Down
13 changes: 11 additions & 2 deletions src/os/file_other.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
//go:build baremetal || (wasm && !wasi)
// +build baremetal wasm,!wasi
//go:build baremetal || wasm_freestanding || (wasm && !wasi)
// +build baremetal wasm_freestanding wasm,!wasi

package os

import (
"io"
_ "unsafe"
)

Expand Down Expand Up @@ -41,11 +42,19 @@ func NewFile(fd uintptr, name string) *File {
// Read reads up to len(b) bytes from machine.Serial.
// It returns the number of bytes read and any error encountered.
func (f stdioFileHandle) Read(b []byte) (n int, err error) {
if f != 0 {
return 0, ErrUnsupported
}

if len(b) == 0 {
return 0, nil
}

size := buffered()
if size < 0 {
return 0, io.EOF
}

for size == 0 {
gosched()
size = buffered()
Expand Down
4 changes: 2 additions & 2 deletions src/os/file_unix.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build darwin || (linux && !baremetal)
// +build darwin linux,!baremetal
//go:build darwin || (linux && !baremetal && !wasm_freestanding)
// +build darwin linux,!baremetal,!wasm_freestanding

// 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
4 changes: 2 additions & 2 deletions src/os/getpagesize_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build windows || darwin || (linux && !baremetal)
// +build windows darwin linux,!baremetal
//go:build windows || darwin || (linux && !baremetal && !wasm_freestanding)
// +build windows darwin linux,!baremetal,!wasm_freestanding

package os_test

Expand Down
4 changes: 2 additions & 2 deletions src/os/os_anyos_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build windows || darwin || (linux && !baremetal)
// +build windows darwin linux,!baremetal
//go:build windows || darwin || (linux && !baremetal && !wasm_freestanding)
// +build windows darwin linux,!baremetal,!wasm_freestanding

package os_test

Expand Down
4 changes: 2 additions & 2 deletions src/os/os_chmod_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build !baremetal && !js && !wasi
// +build !baremetal,!js,!wasi
//go:build !baremetal && !js && !wasi && !wasm_freestanding
// +build !baremetal,!js,!wasi,!wasm_freestanding

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
Expand Down
4 changes: 2 additions & 2 deletions src/os/os_symlink_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build !windows && !baremetal && !js && !wasi
// +build !windows,!baremetal,!js,!wasi
//go:build !windows && !baremetal && !js && !wasi && !wasm_freestanding
// +build !windows,!baremetal,!js,!wasi,!wasm_freestanding

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
Expand Down
4 changes: 2 additions & 2 deletions src/os/pipe_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build windows || darwin || (linux && !baremetal && !wasi)
// +build windows darwin linux,!baremetal,!wasi
//go:build windows || darwin || (linux && !baremetal && !wasi && !wasm_freestanding)
// +build windows darwin linux,!baremetal,!wasi,!wasm_freestanding

// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
Expand Down
4 changes: 2 additions & 2 deletions src/os/read_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build !baremetal && !js && !wasi
// +build !baremetal,!js,!wasi
//go:build !baremetal && !js && !wasi && !wasm_freestanding
// +build !baremetal,!js,!wasi,!wasm_freestanding

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
Expand Down
4 changes: 2 additions & 2 deletions src/os/removeall_noat.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build !baremetal && !js && !wasi
// +build !baremetal,!js,!wasi
//go:build !baremetal && !js && !wasi && !wasm_freestanding
// +build !baremetal,!js,!wasi,!wasm_freestanding

package os

Expand Down
4 changes: 2 additions & 2 deletions src/os/removeall_other.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build baremetal || js || wasi
// +build baremetal js wasi
//go:build baremetal || wasm_freestanding || js || wasi
// +build baremetal wasm_freestanding js wasi

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
Expand Down
4 changes: 2 additions & 2 deletions src/os/removeall_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build darwin || (linux && !baremetal && !js && !wasi)
// +build darwin linux,!baremetal,!js,!wasi
//go:build darwin || (linux && !baremetal && !js && !wasi && !wasm_freestanding)
// +build darwin linux,!baremetal,!js,!wasi,!wasm_freestanding

// TODO: implement ReadDir on windows

Expand Down
4 changes: 2 additions & 2 deletions src/os/seek_unix_bad.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build (linux && !baremetal && 386) || (linux && !baremetal && arm && !wasi)
// +build linux,!baremetal,386 linux,!baremetal,arm,!wasi
//go:build (linux && !baremetal && 386) || (linux && !baremetal && arm && !wasi && !wasm_freestanding)
// +build linux,!baremetal,386 linux,!baremetal,arm,!wasi,!wasm_freestanding

package os

Expand Down
4 changes: 2 additions & 2 deletions src/os/stat_linux.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build linux && !baremetal
// +build linux,!baremetal
//go:build linux && !baremetal && !wasm_freestanding
// +build linux,!baremetal,!wasm_freestanding

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
Expand Down
4 changes: 2 additions & 2 deletions src/os/stat_other.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build baremetal || (wasm && !wasi)
// +build baremetal wasm,!wasi
//go:build baremetal || wasm_freestanding || (wasm && !wasi)
// +build baremetal wasm_freestanding wasm,!wasi

// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
Expand Down
4 changes: 2 additions & 2 deletions src/os/stat_unix.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build darwin || (linux && !baremetal)
// +build darwin linux,!baremetal
//go:build darwin || (linux && !baremetal && !wasm_freestanding)
// +build darwin linux,!baremetal,!wasm_freestanding

// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
Expand Down
3 changes: 3 additions & 0 deletions src/os/tempfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build !wasm_freestanding
// +build !wasm_freestanding

package os

import (
Expand Down
4 changes: 2 additions & 2 deletions src/os/tempfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build !baremetal && !js && !wasi
// +build !baremetal,!js,!wasi
//go:build !baremetal && !js && !wasi && !wasm_freestanding
// +build !baremetal,!js,!wasi,!wasm_freestanding

package os_test

Expand Down
4 changes: 2 additions & 2 deletions src/os/types_anyos.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build !baremetal && !js
// +build !baremetal,!js
//go:build !baremetal && !js && !wasm_freestanding
// +build !baremetal,!js,!wasm_freestanding

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
Expand Down
4 changes: 2 additions & 2 deletions src/os/types_unix.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build darwin || (linux && !baremetal)
// +build darwin linux,!baremetal
//go:build darwin || (linux && !baremetal && !wasm_freestanding)
// +build darwin linux,!baremetal,!wasm_freestanding

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/os_linux.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build linux && !baremetal && !nintendoswitch && !wasi
// +build linux,!baremetal,!nintendoswitch,!wasi
//go:build linux && !baremetal && !nintendoswitch && !wasi && !wasm_freestanding
// +build linux,!baremetal,!nintendoswitch,!wasi,!wasm_freestanding

package runtime

Expand Down
4 changes: 2 additions & 2 deletions src/runtime/os_other.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//go:build linux && (baremetal || nintendoswitch || wasi)
//go:build linux && (baremetal || nintendoswitch || wasi || wasm_freestanding)
// +build linux
// +build baremetal nintendoswitch wasi
// +build baremetal nintendoswitch wasi wasm_freestanding

// Other systems that aren't operating systems supported by the Go toolchain
// need to pretend to be an existing operating system. Linux seems like a good
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/runtime_tinygowasm.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build tinygo.wasm
// +build tinygo.wasm
//go:build tinygo.wasm && !wasm_freestanding
// +build tinygo.wasm,!wasm_freestanding

package runtime

Expand Down
4 changes: 2 additions & 2 deletions src/runtime/runtime_unix.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build (darwin || (linux && !baremetal && !wasi)) && !nintendoswitch
// +build darwin linux,!baremetal,!wasi
//go:build (darwin || (linux && !baremetal && !wasi && !wasm_freestanding)) && !nintendoswitch
// +build darwin linux,!baremetal,!wasi,!wasm_freestanding
// +build !nintendoswitch

package runtime
Expand Down
78 changes: 78 additions & 0 deletions src/runtime/runtime_wasm_freestanding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//go:build wasm_freestanding
// +build wasm_freestanding

package runtime

import (
"unsafe"
)

//export _start
func _start() {
// These need to be initialized early so that the heap can be initialized.
heapStart = uintptr(unsafe.Pointer(&heapStartSymbol))
heapEnd = uintptr(wasm_memory_size(0) * wasmPageSize)
run()
}

// Abort executes the wasm 'unreachable' instruction.
func abort() {
trap()
}

//go:linkname os_runtime_args os.runtime_args
func os_runtime_args() []string {
return []string{}
}

//go:linkname syscall_runtime_envs syscall.runtime_envs
func syscall_runtime_envs() []string {
return []string{}
}

func putchar(c byte) {
}

func getchar() byte {
return 0
}

func buffered() int {
return -1
}

type timeUnit int64

func ticksToNanoseconds(ticks timeUnit) int64 {
panic("unimplemented: ticksToNanoseconds")
}

func nanosecondsToTicks(ns int64) timeUnit {
panic("unimplemented: nanosecondsToTicks")
}

func sleepTicks(d timeUnit) {
panic("unimplemented: sleepTicks")
}

func ticks() timeUnit {
panic("unimplemented: ticks")
}

//go:linkname now time.now
func now() (int64, int32, int64) {
panic("unimplemented: now")
}

//go:linkname syscall_Exit syscall.Exit
func syscall_Exit(code int) {
return
}

//go:linkname procPin sync/atomic.runtime_procPin
func procPin() {
}

//go:linkname procUnpin sync/atomic.runtime_procUnpin
func procUnpin() {
}
4 changes: 2 additions & 2 deletions src/runtime/runtime_wasm_js.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build wasm && !wasi
// +build wasm,!wasi
//go:build wasm && !wasi && !wasm_freestanding
// +build wasm,!wasi,!wasm_freestanding

package runtime

Expand Down
4 changes: 2 additions & 2 deletions src/runtime/runtime_wasm_js_scheduler.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build wasm && !wasi && !scheduler.none
// +build wasm,!wasi,!scheduler.none
//go:build wasm && !wasi && !wasm_freestanding && !scheduler.none
// +build wasm,!wasi,!wasm_freestanding,!scheduler.none

package runtime

Expand Down
Loading