Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Achille Roussel <[email protected]>
  • Loading branch information
achille-roussel committed Sep 19, 2023
1 parent bebf7c8 commit a7ec0bf
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 58 deletions.
2 changes: 1 addition & 1 deletion compiler/coroutine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func TestCoroutineYield(t *testing.T) {

{
name: "range over closure capturing heterogenous values",
coro: Range10ClosureCapturingPointers,
coro: Range10ClosureHeterogenousCapture,
yields: []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
},

Expand Down
27 changes: 16 additions & 11 deletions compiler/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,26 @@ func generateFunctypesInit(pkg *ssa.Package, fn *ssa.Function, init *ast.BlockSt
},
})

fmt.Printf("%v => %+v\n", fn, fn.AnonFuncs)

anonFuncs := slices.Clone(fn.AnonFuncs)
slices.SortFunc(anonFuncs, func(f1, f2 *ssa.Function) int {
return cmp.Compare(f1.Name(), f2.Name())
})

for index, anonFunc := range anonFuncs {
_, colored := colors[anonFunc]
if colored {
// Colored functions (those rewritten into coroutines) have a
// deferred anonymous function injected at the beginning to perform
// stack unwinding, which takes the ".func1" name.
index++
}
name = anonFuncLinkName(name, index)
generateFunctypesInit(pkg, anonFunc, init, name, colors)
index := 0
// Colored functions (those rewritten into coroutines) have a
// deferred anonymous function injected at the beginning to perform
// stack unwinding, which takes the ".func1" name.
_, colored := colors[fn]
if colored {
index++
}

for _, anonFunc := range anonFuncs {
index++
anonFuncName := anonFuncLinkName(name, index)
generateFunctypesInit(pkg, anonFunc, init, anonFuncName, colors)
}
}

Expand All @@ -133,5 +138,5 @@ func generateFunctypesInit(pkg *ssa.Package, fn *ssa.Function, init *ast.BlockSt
// The function works with multiple levels of nesting as each level adds another
// ".func<index>" suffix, with the index being local to the parent scope.
func anonFuncLinkName(base string, index int) string {
return fmt.Sprintf("%s.func%d", base, index+1)
return fmt.Sprintf("%s.func%d", base, index)
}
24 changes: 11 additions & 13 deletions compiler/testdata/coroutine.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package testdata

import (
"strconv"
"time"
"unsafe"

Expand Down Expand Up @@ -315,16 +314,16 @@ func Range10ClosureCapturingPointers() {

func Range10ClosureHeterogenousCapture() {
var (
a int8 = 0
b int16 = 1
c int32 = 2
d int64 = 3
e uint8 = 4
f uint16 = 5
g uint32 = 6
h uint64 = 7
i uintptr = 8
j func() string = func() string { return "9" }
a int8 = 0
b int16 = 1
c int32 = 2
d int64 = 3
e uint8 = 4
f uint16 = 5
g uint32 = 6
h uint64 = 7
i uintptr = 8
j = func() int { return int(i) + 1 }
)

n := 0
Expand All @@ -350,8 +349,7 @@ func Range10ClosureHeterogenousCapture() {
case 8:
v = int(i)
case 9:
s := j()
v, _ = strconv.Atoi(s)
v = j()
}
n++
coroutine.Yield[int, any](v)
Expand Down
33 changes: 4 additions & 29 deletions compiler/testdata/coroutine_durable.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions compiler/testdata/coroutine_functypes.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions internal/serde/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ func SerializeAny(s *Serializer, t reflect.Type, p unsafe.Pointer) {
SerializeUint16(s, *(*uint16)(p))
case reflect.Uint8:
SerializeUint8(s, *(*uint8)(p))
case reflect.Uintptr:
SerializeUintptr(s, *(*uintptr)(p))
case reflect.Float64:
SerializeFloat64(s, *(*float64)(p))
case reflect.Float32:
Expand Down Expand Up @@ -179,6 +181,8 @@ func DeserializeAny(d *Deserializer, t reflect.Type, p unsafe.Pointer) {
DeserializeUint16(d, (*uint16)(p))
case reflect.Uint8:
DeserializeUint8(d, (*uint8)(p))
case reflect.Uintptr:
DeserializeUintptr(d, (*uintptr)(p))
case reflect.Float64:
DeserializeFloat64(d, (*float64)(p))
case reflect.Float32:
Expand Down Expand Up @@ -648,6 +652,16 @@ func DeserializeUint8(d *Deserializer, x *uint8) {
d.b = d.b[1:]
}

func SerializeUintptr(s *Serializer, x uintptr) {
SerializeUint64(s, uint64(x))
}

func DeserializeUintptr(d *Deserializer, x *uintptr) {
u := uint64(0)
DeserializeUint64(d, &u)
*x = uintptr(u)
}

func SerializeFloat32(s *Serializer, x float32) {
SerializeUint32(s, math.Float32bits(x))
}
Expand Down
2 changes: 1 addition & 1 deletion internal/serde/serde.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (d *Deserializer) readPtr() (unsafe.Pointer, sID) {

func (d *Deserializer) store(i sID, p unsafe.Pointer) {
if d.ptrs[i] != nil {
panic(fmt.Errorf("trying to overwirte known ID %d with %p", i, p))
panic(fmt.Errorf("trying to overwwrite known ID %d with %p", i, p))
}
d.ptrs[i] = p
}
Expand Down

0 comments on commit a7ec0bf

Please sign in to comment.