compiler: allow larger-than-int values to be sent across a channel

Instead of storing the value to send/receive in the coroutine promise,
store only a pointer in the promise. This simplifies the code a lot and
allows larger value sizes to be sent across a channel.

Unfortunately, this new system has a code size impact. For example,
compiling testdata/channel.go for the BBC micro:bit, there is an
increase in code size from 4776 bytes to 4856 bytes. However, the
improved flexibility and simplicity of the code should be worth it. If
this becomes an issue, we can always refactor the code at a later time.
This commit is contained in:
Ayke van Laethem
2019-05-04 21:32:00 +02:00
committed by Ron Evans
parent 46d5ea8cf6
commit 9a54ee4241
9 changed files with 112 additions and 130 deletions
+21 -7
View File
@@ -49,13 +49,17 @@ func (t *coroutine) promise() *taskState {
func makeGoroutine(*uint8) *uint8
// Compiler stub to get the current goroutine. Calls to this function are
// removed in the goroutine lowering pass.
func getCoroutine() *coroutine
// State/promise of a task. Internally represented as:
//
// {i8* next, i1 commaOk, i32/i64 data}
type taskState struct {
next *coroutine
commaOk bool // 'comma-ok' flag for channel receive operation
data uint
next *coroutine
ptr unsafe.Pointer
data uint
}
// Queues used by the scheduler.
@@ -107,12 +111,22 @@ func activateTask(task *coroutine) {
runqueuePushBack(task)
}
func setTaskData(task *coroutine, value unsafe.Pointer) {
task.promise().data = uint(uintptr(value))
// getTaskPromisePtr is a helper function to set the current .ptr field of a
// coroutine promise.
func setTaskPromisePtr(task *coroutine, value unsafe.Pointer) {
task.promise().ptr = value
}
func getTaskData(task *coroutine) unsafe.Pointer {
return unsafe.Pointer(uintptr(task.promise().data))
// getTaskPromisePtr is a helper function to get the current .ptr field from a
// coroutine promise.
func getTaskPromisePtr(task *coroutine) unsafe.Pointer {
return task.promise().ptr
}
// getTaskPromiseData is a helper function to get the current .data field of a
// coroutine promise.
func getTaskPromiseData(task *coroutine) uint {
return task.promise().data
}
// Add this task to the end of the run queue. May also destroy the task if it's