-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvm.go
168 lines (159 loc) · 5.14 KB
/
vm.go
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package deecpy
import (
"reflect"
"unsafe"
"v8.run/go/deecpy/unsafeops"
)
type ptrmap struct {
k unsafe.Pointer
v unsafe.Pointer
}
func ptrmapSearch(m *[]ptrmap, k unsafe.Pointer) (unsafe.Pointer, bool) {
for _, v := range *m {
if v.k == k {
return v.v, true
}
}
return nil, false
}
func exec(dst, src unsafe.Pointer, inst *instructions, nocopy bool, pmap *[]ptrmap) {
*pmap = append(*pmap, ptrmap{k: src, v: dst})
L:
for i := range inst.ops {
switch v := inst.ops[i].(type) {
case *opCopyMem:
if nocopy {
continue L
}
unsafeops.MemMove(
unsafe.Add(dst, v.Offset),
unsafe.Add(src, v.Offset),
v.Size,
)
case *opPtrDup:
srcPtr := *(*unsafe.Pointer)(unsafe.Add(src, v.Offset))
if srcPtr == nil {
*(*unsafe.Pointer)(unsafe.Add(dst, v.Offset)) = nil
continue
}
if val, ok := ptrmapSearch(pmap, srcPtr); ok {
*(*unsafe.Pointer)(unsafe.Add(dst, v.Offset)) = val
continue
}
*(*unsafe.Pointer)(unsafe.Add(dst, v.Offset)) = unsafeops.NewObject(v.UnsafeType)
exec(*(*unsafe.Pointer)(unsafe.Add(dst, v.Offset)), srcPtr, v.SubInstructions, false, pmap)
case *opPtrDupMem:
srcPtr := *(*unsafe.Pointer)(unsafe.Add(src, v.Offset))
if srcPtr == nil {
*(*unsafe.Pointer)(unsafe.Add(dst, v.Offset)) = nil
continue L
}
if val, ok := ptrmapSearch(pmap, srcPtr); ok {
*(*unsafe.Pointer)(unsafe.Add(dst, v.Offset)) = val
continue
}
*(*unsafe.Pointer)(unsafe.Add(dst, v.Offset)) = unsafeops.NewObject(v.UnsafeType)
if v.Size == 0 {
continue L
}
unsafeops.MemMove(
*(*unsafe.Pointer)(unsafe.Add(dst, v.Offset)),
srcPtr,
v.Size,
)
case *opArrayCopy:
for i := uintptr(0); i < v.ArrayLen; i++ {
exec(
unsafe.Add(dst, v.Offset+i*v.ElemSize),
unsafe.Add(src, v.Offset+i*v.ElemSize),
v.SubInstructions,
nocopy,
pmap,
)
}
case *opSliceCopy:
s := (*reflect.SliceHeader)(unsafe.Add(src, v.Offset))
(*reflect.SliceHeader)(unsafe.Add(dst, v.Offset)).Cap = s.Cap
(*reflect.SliceHeader)(unsafe.Add(dst, v.Offset)).Len = s.Len
if uintptr(s.Cap)*v.ElemSize == 0 {
continue L
}
(*reflect.SliceHeader)(unsafe.Add(dst, v.Offset)).Data = uintptr(unsafeops.NewArray(v.UnsafeElemType, s.Cap))
sliceSrcData := unsafe.Pointer((*reflect.SliceHeader)(unsafe.Add(src, v.Offset)).Data)
sliceDstData := unsafe.Pointer((*reflect.SliceHeader)(unsafe.Add(dst, v.Offset)).Data)
for i := uintptr(0); i < uintptr(s.Cap); i++ {
exec(
unsafe.Add(sliceSrcData, uintptr(i)*v.ElemSize),
unsafe.Add(sliceDstData, uintptr(i)*v.ElemSize),
v.SubInstructions,
false,
pmap,
)
}
case *opSliceCopyMem:
s := (*reflect.SliceHeader)(unsafe.Add(src, v.Offset))
(*reflect.SliceHeader)(unsafe.Add(dst, v.Offset)).Cap = s.Cap
(*reflect.SliceHeader)(unsafe.Add(dst, v.Offset)).Len = s.Len
if s.Cap == 0 {
continue L
}
(*reflect.SliceHeader)(unsafe.Add(dst, v.Offset)).Data = uintptr(unsafeops.NewArray(v.UnsafeElemType, s.Cap))
unsafeops.MemMove(
unsafe.Pointer((*reflect.SliceHeader)(unsafe.Add(dst, v.Offset)).Data),
unsafe.Pointer(s.Data),
uintptr(s.Cap)*v.ElemSize,
)
case *opMapDup:
srcMap := (*unsafe.Pointer)(unsafe.Add(src, v.Offset))
srcMapIface := unsafeops.MakeEface(*srcMap, v.MapUnsafeType)
srcMapReflectValue := reflect.ValueOf(srcMapIface)
keys := srcMapReflectValue.MapKeys()
newMap := reflect.MakeMapWithSize(v.ReflectType, len(keys))
for i := range keys {
oldKey := keys[i]
oldKeyIface := oldKey.Interface()
oldKeyPtr := unsafeops.DataOf(&oldKeyIface)
oldKeyType := unsafeops.TypeID(&oldKeyIface)
var newKeyPtr unsafe.Pointer
if val, ok := ptrmapSearch(pmap, oldKeyPtr); ok {
newKeyPtr = val
} else {
newKeyPtr = unsafeops.NewObject(v.KeyUnsafeType)
exec(newKeyPtr, oldKeyPtr, v.KeySubInstructions, false, pmap)
}
newKeyIface := unsafeops.MakeEface(newKeyPtr, oldKeyType)
newKeyValue := reflect.ValueOf(newKeyIface)
oldValue := srcMapReflectValue.MapIndex(oldKey)
oldValueIface := oldValue.Interface()
oldValuePtr := unsafeops.DataOf(&oldValueIface)
oldValueType := unsafeops.TypeID(&oldValueIface)
var newValuePtr unsafe.Pointer
if val, ok := ptrmapSearch(pmap, oldValuePtr); ok {
newValuePtr = val
} else {
newValuePtr = unsafeops.NewObject(v.ValueUnsafeType)
exec(newValuePtr, oldValuePtr, v.ValueSubInstructions, false, pmap)
}
newValueIface := unsafeops.MakeEface(newValuePtr, oldValueType)
newValueValue := reflect.ValueOf(newValueIface)
newMap.SetMapIndex(newKeyValue, newValueValue)
}
newMapPtr := newMap.UnsafePointer()
*(*unsafe.Pointer)(unsafe.Add(dst, v.Offset)) = newMapPtr
case *opCopyString:
s := *(*string)(unsafe.Add(src, v.Offset))
buffer := make([]byte, len(s))
copy(buffer, s)
*(*string)(unsafe.Add(dst, v.Offset)) = *(*string)(unsafe.Pointer(&buffer))
case *opCopyStruct:
newDst := unsafe.Add(dst, v.Offset)
newSrc := unsafe.Add(src, v.Offset)
exec(newDst, newSrc, v.SubInstructions, true, pmap)
case *opCopyInterface:
// TODO: Implement Interface Copy
default:
// Unreachable
panic("unreachable")
}
}
}