From 186018abebe67052ce53c610ce0eaab1bef6d24a Mon Sep 17 00:00:00 2001 From: deadprogram Date: Mon, 12 Feb 2024 13:43:54 +0100 Subject: runtime, targets: some WIP on wasm unknown in part from PR #3072 Signed-off-by: deadprogram --- src/runtime/os_linux.go | 2 +- src/runtime/os_other.go | 2 +- src/runtime/runtime_tinygowasm.go | 2 +- src/runtime/runtime_tinygowasm_unknown.go | 50 +++++++++++++++++++++++++++++++ src/runtime/runtime_unix.go | 2 +- src/runtime/runtime_wasm_js_scheduler.go | 2 +- src/runtime/runtime_wasm_unknown.go | 49 ++++++++++++++++++++++++++++++ targets/wasm-unknown.json | 25 ++++++++++++++++ 8 files changed, 129 insertions(+), 5 deletions(-) create mode 100644 src/runtime/runtime_tinygowasm_unknown.go create mode 100644 src/runtime/runtime_wasm_unknown.go create mode 100644 targets/wasm-unknown.json diff --git a/src/runtime/os_linux.go b/src/runtime/os_linux.go index fefba85f4..3f27dfd16 100644 --- a/src/runtime/os_linux.go +++ b/src/runtime/os_linux.go @@ -1,4 +1,4 @@ -//go:build linux && !baremetal && !nintendoswitch && !wasi +//go:build linux && !baremetal && !nintendoswitch && !wasi && !wasm_unknown package runtime diff --git a/src/runtime/os_other.go b/src/runtime/os_other.go index 63d8153fe..c8330bafc 100644 --- a/src/runtime/os_other.go +++ b/src/runtime/os_other.go @@ -1,4 +1,4 @@ -//go:build linux && (baremetal || nintendoswitch || wasi) +//go:build linux && (baremetal || nintendoswitch || wasi || wasm_unknown) // 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 diff --git a/src/runtime/runtime_tinygowasm.go b/src/runtime/runtime_tinygowasm.go index 8ed73df77..399860353 100644 --- a/src/runtime/runtime_tinygowasm.go +++ b/src/runtime/runtime_tinygowasm.go @@ -1,4 +1,4 @@ -//go:build tinygo.wasm +//go:build tinygo.wasm && !wasm_unknown package runtime diff --git a/src/runtime/runtime_tinygowasm_unknown.go b/src/runtime/runtime_tinygowasm_unknown.go new file mode 100644 index 000000000..08a9414fc --- /dev/null +++ b/src/runtime/runtime_tinygowasm_unknown.go @@ -0,0 +1,50 @@ +//go:build wasm_unknown + +package runtime + +const ( + stdout = 1 +) + +func putchar(c byte) { +} + +func getchar() byte { + // dummy, TODO + return 0 +} + +func buffered() int { + // dummy, TODO + return 0 +} + +//go:linkname now time.now +func now() (sec int64, nsec int32, mono int64) { + return 0, 0, 0 +} + +// Abort executes the wasm 'unreachable' instruction. +func abort() { + trap() +} + +//go:linkname syscall_Exit syscall.Exit +func syscall_Exit(code int) { + //proc_exit(uint32(code)) +} + +// TinyGo does not yet support any form of parallelism on WebAssembly, so these +// can be left empty. + +//go:linkname procPin sync/atomic.runtime_procPin +func procPin() { +} + +//go:linkname procUnpin sync/atomic.runtime_procUnpin +func procUnpin() { +} + +func hardwareRand() (n uint64, ok bool) { + return 0, false +} diff --git a/src/runtime/runtime_unix.go b/src/runtime/runtime_unix.go index 8af3d673c..f1f1c4df7 100644 --- a/src/runtime/runtime_unix.go +++ b/src/runtime/runtime_unix.go @@ -1,4 +1,4 @@ -//go:build (darwin || (linux && !baremetal && !wasi)) && !nintendoswitch +//go:build (darwin || (linux && !baremetal && !wasi && !wasm_unknown)) && !nintendoswitch package runtime diff --git a/src/runtime/runtime_wasm_js_scheduler.go b/src/runtime/runtime_wasm_js_scheduler.go index fc599a2a8..d5e1012de 100644 --- a/src/runtime/runtime_wasm_js_scheduler.go +++ b/src/runtime/runtime_wasm_js_scheduler.go @@ -1,4 +1,4 @@ -//go:build wasm && !wasi && !scheduler.none && !wasip1 +//go:build wasm && !wasi && !scheduler.none && !wasip1 && !wasm_unknown package runtime diff --git a/src/runtime/runtime_wasm_unknown.go b/src/runtime/runtime_wasm_unknown.go new file mode 100644 index 000000000..c17ece257 --- /dev/null +++ b/src/runtime/runtime_wasm_unknown.go @@ -0,0 +1,49 @@ +//go:build wasm_unknown + +package runtime + +import ( + "unsafe" +) + +type timeUnit int64 + +// libc constructors +// +//export __wasm_call_ctors +func __wasm_call_ctors() + +//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() +} + +// Read the command line arguments from WASI. +// For example, they can be passed to a program with wasmtime like this: +// +// wasmtime run ./program.wasm arg1 arg2 +func init() { + __wasm_call_ctors() +} + +var args []string + +func ticksToNanoseconds(ticks timeUnit) int64 { + return int64(ticks) +} + +func nanosecondsToTicks(ns int64) timeUnit { + return timeUnit(ns) +} + +const timePrecisionNanoseconds = 1000 // TODO: how can we determine the appropriate `precision`? + +func sleepTicks(d timeUnit) { +} + +func ticks() timeUnit { + return timeUnit(0) +} diff --git a/targets/wasm-unknown.json b/targets/wasm-unknown.json new file mode 100644 index 000000000..3a96eb036 --- /dev/null +++ b/targets/wasm-unknown.json @@ -0,0 +1,25 @@ +{ + "llvm-target": "wasm32-unknown-unknown", + "cpu": "generic", + "features": "+bulk-memory,+mutable-globals,+nontrapping-fptoint,+sign-ext", + "build-tags": ["tinygo.wasm", "wasm_unknown"], + "goos": "linux", + "goarch": "arm", + "linker": "wasm-ld", + "rtlib": "compiler-rt", + "scheduler": "none", + "default-stack-size": 4096, + "cflags": [ + "-mbulk-memory", + "-mnontrapping-fptoint", + "-msign-ext" + ], + "ldflags": [ + "--no-demangle", + "--no-entry" + ], + "extra-files": [ + "src/runtime/asm_tinygowasm.S" + ], + "emulator": "wasmtime --dir={tmpDir}::/tmp {}" +} -- cgit v1.2.3