...
1
16
17 package rt
18
19 import (
20 `fmt`
21 `strings`
22 `unsafe`
23
24 )
25
26 type Bitmap struct {
27 N int
28 B []byte
29 }
30
31 func (self *Bitmap) grow() {
32 if self.N >= len(self.B) * 8 {
33 self.B = append(self.B, 0)
34 }
35 }
36
37 func (self *Bitmap) mark(i int, bv int) {
38 if bv != 0 {
39 self.B[i / 8] |= 1 << (i % 8)
40 } else {
41 self.B[i / 8] &^= 1 << (i % 8)
42 }
43 }
44
45 func (self *Bitmap) Set(i int, bv int) {
46 if i >= self.N {
47 panic("bitmap: invalid bit position")
48 } else {
49 self.mark(i, bv)
50 }
51 }
52
53 func (self *Bitmap) Append(bv int) {
54 self.grow()
55 self.mark(self.N, bv)
56 self.N++
57 }
58
59 func (self *Bitmap) AppendMany(n int, bv int) {
60 for i := 0; i < n; i++ {
61 self.Append(bv)
62 }
63 }
64
65
66
67
68
69
70 type BitVec struct {
71 N uintptr
72 B unsafe.Pointer
73 }
74
75 func (self BitVec) Bit(i uintptr) byte {
76 return (*(*byte)(unsafe.Pointer(uintptr(self.B) + i / 8)) >> (i % 8)) & 1
77 }
78
79 func (self BitVec) String() string {
80 var i uintptr
81 var v []string
82
83
84 for i = 0; i < self.N; i++ {
85 v = append(v, fmt.Sprintf("%d", self.Bit(i)))
86 }
87
88
89 return fmt.Sprintf(
90 "BitVec { %s }",
91 strings.Join(v, ", "),
92 )
93 }
94
95 type StackMap struct {
96 N int32
97 L int32
98 B [1]byte
99 }
100
101
102
103
104
105
106
107 func (self *StackMap) Pin() uintptr {
108
109 return uintptr(unsafe.Pointer(self))
110 }
111
112 func (self *StackMap) Get(i int32) BitVec {
113 return BitVec {
114 N: uintptr(self.L),
115 B: unsafe.Pointer(uintptr(unsafe.Pointer(&self.B)) + uintptr(i * ((self.L + 7) >> 3))),
116 }
117 }
118
119 func (self *StackMap) String() string {
120 sb := strings.Builder{}
121 sb.WriteString("StackMap {")
122
123
124 for i := int32(0); i < self.N; i++ {
125 sb.WriteRune('\n')
126 sb.WriteString(" " + self.Get(i).String())
127 }
128
129
130 sb.WriteString("\n}")
131 return sb.String()
132 }
133
134 func (self *StackMap) MarshalBinary() ([]byte, error) {
135 size := int(self.N) * int(self.L) + int(unsafe.Sizeof(self.L)) + int(unsafe.Sizeof(self.N))
136 return BytesFrom(unsafe.Pointer(self), size, size), nil
137 }
138
139 var (
140 byteType = UnpackEface(byte(0)).Type
141 )
142
143 const (
144 _StackMapSize = unsafe.Sizeof(StackMap{})
145 )
146
147
148
149 func mallocgc(nb uintptr, vt *GoType, zero bool) unsafe.Pointer
150
151 type StackMapBuilder struct {
152 b Bitmap
153 }
154
155
156 func (self *StackMapBuilder) Build() (p *StackMap) {
157 nb := len(self.b.B)
158 bm := mallocgc(_StackMapSize + uintptr(nb) - 1, byteType, false)
159
160
161 p = (*StackMap)(bm)
162 p.N, p.L = 1, int32(self.b.N)
163 copy(BytesFrom(unsafe.Pointer(&p.B), nb, nb), self.b.B)
164 return
165 }
166
167 func (self *StackMapBuilder) AddField(ptr bool) {
168 if ptr {
169 self.b.Append(1)
170 } else {
171 self.b.Append(0)
172 }
173 }
174
175 func (self *StackMapBuilder) AddFields(n int, ptr bool) {
176 if ptr {
177 self.b.AppendMany(n, 1)
178 } else {
179 self.b.AppendMany(n, 0)
180 }
181 }
View as plain text