all: rewrite goroutine lowering

Before this commit, goroutine support was spread through the compiler.
This commit changes this support, so that the compiler itself only
generates simple intrinsics and leaves the real support to a compiler
pass that runs as one of the TinyGo-specific optimization passes.

The biggest change, that was done together with the rewrite, was support
for goroutines in WebAssembly for JavaScript. The challenge in
JavaScript is that in general no blocking operations are allowed, which
means that programs that call time.Sleep() but do not start goroutines
also have to be scheduled by the scheduler.
This commit is contained in:
Ayke van Laethem
2019-01-10 16:54:09 +01:00
parent 072ef603fe
commit 602c264749
24 changed files with 807 additions and 483 deletions
+15 -11
View File
@@ -6,11 +6,9 @@ import (
"unsafe"
)
type timeUnit int64
type timeUnit float64 // time in milliseconds, just like Date.now() in JavaScript
const tickMicros = 1
var timestamp timeUnit
const tickMicros = 1000000
//go:export io_get_stdout
func io_get_stdout() int32
@@ -32,21 +30,27 @@ func _start() {
//go:export cwa_main
func cwa_main() {
initAll() // _start is not called by olin/cwa so has to be called here
mainWrapper()
callMain()
}
func putchar(c byte) {
resource_write(stdout, &c, 1)
}
func sleepTicks(d timeUnit) {
// TODO: actually sleep here for the given time.
timestamp += d
//go:export go_scheduler
func go_scheduler() {
scheduler()
}
func ticks() timeUnit {
return timestamp
}
const asyncScheduler = true
// This function is called by the scheduler.
// Schedule a call to runtime.scheduler, do not actually sleep.
//go:export runtime.sleepTicks
func sleepTicks(d timeUnit)
//go:export runtime.ticks
func ticks() timeUnit
// Abort executes the wasm 'unreachable' instruction.
func abort() {