Skip to content

Commit

Permalink
feat: new polkadot wasm target and custom gc (#22)
Browse files Browse the repository at this point in the history
* polkawasm: add dockerfile and build script
* polkawasm: add new target separate from the existing wasm/wasi
* polkawasm: import memory, allow undefined, export globals and tables, disable scheduler, specify gc
* polkawasm: switch wasi-libc and wasm-opt to MVP instructions set
* polkawasm: remove _start export. remove wasm-libc allocation functions
* polkawasm: add custom gc that relies on an external allocator
  • Loading branch information
radkomih committed Jan 23, 2024
1 parent 6cdfc29 commit 7ed5d3f
Show file tree
Hide file tree
Showing 58 changed files with 909 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
branch = main
[submodule "lib/wasi-libc"]
path = lib/wasi-libc
url = https://github.com/WebAssembly/wasi-libc
url = https://github.com/LimeChain/wasi-libc
[submodule "lib/picolibc"]
path = lib/picolibc
url = https://github.com/keith-packard/picolibc.git
Expand Down
33 changes: 33 additions & 0 deletions Dockerfile.polkawasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Diffs:
# - prebuild LLVM

FROM golang:1.21-bullseye AS tinygo-base

ENV GO111MODULE=on
ENV GOFLAGS="-buildvcs=false"

# Add the LLVM 16 repo for Debian 11 Bullseye
RUN wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | apt-key add - && \
echo "deb http://apt.llvm.org/bullseye/ llvm-toolchain-bullseye-16 main" | tee /etc/apt/sources.list.d/llvm.list

# Install LLVM toolchain packages
RUN apt-get update && apt-get install -y \
clang-16 lld-16 llvm-16-dev libclang-16-dev cmake ninja-build

# Copy TinyGo repo
COPY . /tinygo

WORKDIR /tinygo/lib/binaryen

# Install binaryen(wasm-opt)
RUN cmake . && make

ENV WASMOPT="/tinygo/lib/binaryen/bin/wasm-opt"

WORKDIR /tinygo

# Build wasi-libc and tinygo compiler
RUN make wasi-libc && \
go install .

CMD ["tinygo"]
26 changes: 20 additions & 6 deletions builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -816,12 +816,26 @@ func Build(pkgName, outpath, tmpdir string, config *compileopts.Config) (BuildRe
args = append(args, "--asyncify")
}

args = append(args,
opt,
"-g",
result.Executable,
"--output", result.Executable,
)
if config.Target.Triple == "wasm32-unknown-polkawasm" {
args = append(args,
opt,
"--signext-lowering",
// "--signature-pruning",
// "--const-hoisting",
// "--mvp-features",
result.Executable,
"--output",
result.Executable,
)
} else {
args = append(args,
opt,
"-g",
result.Executable,
"--output",
result.Executable,
)
}

cmd := exec.Command(goenv.Get("WASMOPT"), args...)
cmd.Stdout = os.Stdout
Expand Down
2 changes: 1 addition & 1 deletion compileopts/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (c *Config) GC() string {
// that can be traced by the garbage collector.
func (c *Config) NeedsStackObjects() bool {
switch c.GC() {
case "conservative", "custom", "precise":
case "conservative", "custom", "extalloc", "extalloc_leaking", "precise":
for _, tag := range c.BuildTags() {
if tag == "tinygo.wasm" {
return true
Expand Down
2 changes: 1 addition & 1 deletion compileopts/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

var (
validGCOptions = []string{"none", "leaking", "conservative", "custom", "precise"}
validGCOptions = []string{"none", "leaking", "conservative", "custom", "extalloc", "extalloc_leaking", "precise"}
validSchedulerOptions = []string{"none", "tasks", "asyncify"}
validSerialOptions = []string{"none", "uart", "usb", "rtt"}
validPrintSizeOptions = []string{"none", "short", "full"}
Expand Down
2 changes: 1 addition & 1 deletion compileopts/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func TestVerifyOptions(t *testing.T) {

expectedGCError := errors.New(`invalid gc option 'incorrect': valid values are none, leaking, conservative, custom, precise`)
expectedGCError := errors.New(`invalid gc option 'incorrect': valid values are none, leaking, conservative, custom, extalloc, extalloc_leaking, precise`)
expectedSchedulerError := errors.New(`invalid scheduler option 'incorrect': valid values are none, tasks, asyncify`)
expectedPrintSizeError := errors.New(`invalid size option 'incorrect': valid values are none, short, full`)
expectedPanicStrategyError := errors.New(`invalid panic option 'incorrect': valid values are print, trap`)
Expand Down
4 changes: 4 additions & 0 deletions polkawasm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

docker build --tag tinygo/polkawasm:0.31.0-dev -f Dockerfile.polkawasm .
docker run --rm -it tinygo/polkawasm:0.31.0-dev bash
2 changes: 1 addition & 1 deletion src/os/dir_other.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build baremetal || js || windows
//go:build baremetal || js || windows || polkawasm

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
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 && !wasip1
//go:build linux && !baremetal && !wasi && !wasip1 && !polkawasm

package os

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

// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
Expand Down
2 changes: 1 addition & 1 deletion src/os/env_unix_test.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 darwin || linux || wasip1
//go:build darwin || (linux && !polkawasm) || wasip1

package os_test

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 || wasip1 || windows
//go:build aix || darwin || dragonfly || freebsd || (js && wasm) || (linux && !polkawasm) || netbsd || openbsd || solaris || wasip1 || windows

package os

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

package os

Expand Down
2 changes: 1 addition & 1 deletion src/os/executable_procfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,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
//go:build linux && !baremetal && !polkawasm

package os

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

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

package os_test

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 && !wasip1)
//go:build baremetal || polkawasm || (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) || wasip1
//go:build darwin || (linux && !baremetal && !polkawasm) || 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/getpagesize_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build windows || darwin || (linux && !baremetal) || wasip1
//go:build windows || darwin || (linux && !baremetal && !polkawasm) || wasip1

package os_test

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

package os_test

Expand Down
2 changes: 1 addition & 1 deletion src/os/os_chmod_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build !baremetal && !js && !wasi && !wasip1
//go:build !baremetal && !js && !wasi && !wasip1 && !polkawasm

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

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

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

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
Expand Down
2 changes: 1 addition & 1 deletion src/os/removeall_noat.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 !baremetal && !js && !wasi && !wasip1
//go:build !baremetal && !js && !wasi && !wasip1 && !polkawasm

package os

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

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

// TODO: implement ReadDir on windows

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

package os

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

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

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

// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
Expand Down
2 changes: 1 addition & 1 deletion src/os/types_anyos.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build !baremetal && !js
//go:build !baremetal && !js && !polkawasm

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
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) || wasip1
//go:build darwin || (linux && !baremetal && !polkawasm) || wasip1

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/arch_tinygowasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ var (
func align(ptr uintptr) uintptr {
// Align to 16, which is the alignment of max_align_t:
// https://godbolt.org/z/dYqTsWrGq
const heapAlign = 16
const heapAlign = 8 // matches the allocator's alignment (consumes less memory)
return (ptr + heapAlign - 1) &^ (heapAlign - 1)
}

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/arch_tinygowasm_malloc.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build tinygo.wasm && !custommalloc
//go:build tinygo.wasm && !custommalloc && !polkawasm

package runtime

Expand Down
1 change: 0 additions & 1 deletion src/runtime/gc_custom.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build gc.custom
// +build gc.custom

package runtime

Expand Down
47 changes: 47 additions & 0 deletions src/runtime/gc_debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//go:build gc.extalloc || gc.extalloc_leaking

package runtime

const gcDebug = false

func printnum(num int) {
if num == 0 {
printstr("0")
return
}

digits := [16]int{} // store up to 16 digits
digitStrings := [10]string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
count := 0 // count of digits

// extract digits from the number
for ; num > 0; num /= 10 {
digits[count] = num % 10
count++
}

// reverse the digits
for i := 0; i < count/2; i++ {
j := count - i - 1
digits[i], digits[j] = digits[j], digits[i]
}

// print each digit
for i := 0; i < count; i++ {
printstr(digitStrings[digits[i]])
}
}

func printstr(str string) {
if !gcDebug {
return
}

for i := 0; i < len(str); i++ {
if putcharPosition >= putcharBufferSize {
break
}

putchar(str[i])
}
}
Loading

0 comments on commit 7ed5d3f

Please sign in to comment.