Skip to content

Commit 78c85de

Browse files
committed
runtime, targets: some WIP on wasm unknown in part from PR #3072
Signed-off-by: deadprogram <[email protected]>
1 parent 828b961 commit 78c85de

File tree

8 files changed

+136
-5
lines changed

8 files changed

+136
-5
lines changed

src/runtime/os_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build linux && !baremetal && !nintendoswitch && !wasi
1+
//go:build linux && !baremetal && !nintendoswitch && !wasi && !wasm_unknown
22

33
package runtime
44

src/runtime/os_other.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build linux && (baremetal || nintendoswitch || wasi)
1+
//go:build linux && (baremetal || nintendoswitch || wasi || wasm_unknown)
22

33
// Other systems that aren't operating systems supported by the Go toolchain
44
// need to pretend to be an existing operating system. Linux seems like a good

src/runtime/runtime_tinygowasm.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build tinygo.wasm
1+
//go:build tinygo.wasm && !wasm_unknown
22

33
package runtime
44

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//go:build wasm_unknown
2+
3+
package runtime
4+
5+
const (
6+
stdout = 1
7+
)
8+
9+
func putchar(c byte) {
10+
}
11+
12+
func getchar() byte {
13+
// dummy, TODO
14+
return 0
15+
}
16+
17+
func buffered() int {
18+
// dummy, TODO
19+
return 0
20+
}
21+
22+
//go:linkname now time.now
23+
func now() (sec int64, nsec int32, mono int64) {
24+
return 0, 0, 0
25+
}
26+
27+
// Abort executes the wasm 'unreachable' instruction.
28+
func abort() {
29+
trap()
30+
}
31+
32+
//go:linkname syscall_Exit syscall.Exit
33+
func syscall_Exit(code int) {
34+
//proc_exit(uint32(code))
35+
}
36+
37+
// TinyGo does not yet support any form of parallelism on WebAssembly, so these
38+
// can be left empty.
39+
40+
//go:linkname procPin sync/atomic.runtime_procPin
41+
func procPin() {
42+
}
43+
44+
//go:linkname procUnpin sync/atomic.runtime_procUnpin
45+
func procUnpin() {
46+
}
47+
48+
func hardwareRand() (n uint64, ok bool) {
49+
return 0, false
50+
}

src/runtime/runtime_unix.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build (darwin || (linux && !baremetal && !wasi)) && !nintendoswitch
1+
//go:build (darwin || (linux && !baremetal && !wasi && !wasm_unknown)) && !nintendoswitch
22

33
package runtime
44

src/runtime/runtime_wasm_js_scheduler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build wasm && !wasi && !scheduler.none && !wasip1
1+
//go:build wasm && !wasi && !scheduler.none && !wasip1 && !wasm_unknown
22

33
package runtime
44

src/runtime/runtime_wasm_unknown.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//go:build wasm_unknown
2+
3+
package runtime
4+
5+
import (
6+
"unsafe"
7+
)
8+
9+
type timeUnit int64
10+
11+
// libc constructors
12+
//
13+
// export __wasm_call_ctors
14+
// func __wasm_call_ctors()
15+
16+
//export _start
17+
func _start() {
18+
// These need to be initialized early so that the heap can be initialized.
19+
heapStart = uintptr(unsafe.Pointer(&heapStartSymbol))
20+
heapEnd = uintptr(wasm_memory_size(0) * wasmPageSize)
21+
run()
22+
}
23+
24+
// Read the command line arguments from WASI.
25+
// For example, they can be passed to a program with wasmtime like this:
26+
//
27+
// wasmtime run ./program.wasm arg1 arg2
28+
func init() {
29+
// __wasm_call_ctors()
30+
}
31+
32+
var args []string
33+
34+
//go:linkname os_runtime_args os.runtime_args
35+
func os_runtime_args() []string {
36+
return nil
37+
}
38+
39+
func ticksToNanoseconds(ticks timeUnit) int64 {
40+
return int64(ticks)
41+
}
42+
43+
func nanosecondsToTicks(ns int64) timeUnit {
44+
return timeUnit(ns)
45+
}
46+
47+
const timePrecisionNanoseconds = 1000 // TODO: how can we determine the appropriate `precision`?
48+
49+
func sleepTicks(d timeUnit) {
50+
}
51+
52+
func ticks() timeUnit {
53+
return timeUnit(0)
54+
}

targets/wasm-unknown.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"llvm-target": "wasm32-unknown-unknown",
3+
"cpu": "generic",
4+
"features": "+bulk-memory,+mutable-globals,+nontrapping-fptoint,+sign-ext",
5+
"build-tags": ["tinygo.wasm", "wasm_unknown"],
6+
"goos": "linux",
7+
"goarch": "arm",
8+
"linker": "wasm-ld",
9+
"rtlib": "compiler-rt",
10+
"scheduler": "none",
11+
"default-stack-size": 4096,
12+
"cflags": [
13+
"-mbulk-memory",
14+
"-mnontrapping-fptoint",
15+
"-msign-ext"
16+
],
17+
"ldflags": [
18+
"--stack-first",
19+
"--no-demangle",
20+
"--no-entry",
21+
"--import-memory"
22+
],
23+
"extra-files": [
24+
"src/runtime/asm_tinygowasm.S"
25+
],
26+
"emulator": "wasmtime --dir={tmpDir}::/tmp {}"
27+
}

0 commit comments

Comments
 (0)