-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlink_test.go
More file actions
61 lines (49 loc) · 1.46 KB
/
Copy pathlink_test.go
File metadata and controls
61 lines (49 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package asm_test
import (
"runtime"
"testing"
"unsafe"
"github.com/siyul-park/minivm/asm"
"github.com/siyul-park/minivm/asm/arm64"
"github.com/stretchr/testify/require"
)
func TestLink(t *testing.T) {
t.Run("nil buffer", func(t *testing.T) {
_, err := asm.Link(nil, arm64.New(), nil, nil)
require.ErrorIs(t, err, asm.ErrInvalidArgs)
})
if runtime.GOARCH != "arm64" {
t.Skipf("native invoke requires arm64, got %s", runtime.GOARCH)
}
t.Run("exposes internal entry", func(t *testing.T) {
arch := arm64.New()
a := asm.New(arch)
ctx := a.Reg(asm.RegTypeInt, asm.Width64)
v := a.Reg(asm.RegTypeInt, asm.Width64)
require.NoError(t, a.Pin(ctx, arm64.X0))
entry := a.Label()
a.Emit(arm64.LDI(v, 3)...)
a.Emit(arm64.STR(v, ctx, 0))
a.Emit(arm64.RET())
a.Entry(entry)
a.Emit(arm64.LDR(v, ctx, 0))
a.Emit(arm64.ADDI(v, v, 1))
a.Emit(arm64.STR(v, ctx, 0))
a.Emit(arm64.RET())
code, err := a.Build()
require.NoError(t, err)
buf, err := asm.NewBuffer(4096)
require.NoError(t, err)
defer buf.Free()
linked, err := asm.Link(buf, arch, []*asm.Code{code}, nil)
require.NoError(t, err)
require.Len(t, linked, 1)
require.Contains(t, linked[0].Entries, entry)
ctxBuf := []uint64{0}
require.NoError(t, linked[0].Callable.Call(unsafe.Pointer(&ctxBuf[0])))
require.Equal(t, uint64(3), ctxBuf[0])
ctxBuf[0] = 41
require.NoError(t, linked[0].Entries[entry].Call(unsafe.Pointer(&ctxBuf[0])))
require.Equal(t, uint64(42), ctxBuf[0])
})
}