Repository navigation
Expand file tree
/
Copy pathcode_atomic.go
More file actions
260 lines (232 loc) · 8.73 KB
/
Copy pathcode_atomic.go
File metadata and controls
260 lines (232 loc) · 8.73 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package main
import (
"fmt"
"go/ast"
"strings"
)
func (t *translator) readOpcodeAtomic(fn *funcCompiler) error {
code, err := readLEB128(t.in)
if err != nil {
return err
}
if code == 0x03 { // atomic.fence
_, err = t.in.ReadByte() // 0x00
fn.helpers.add("atomic_fence")
fn.emit(&ast.ExprStmt{X: &ast.CallExpr{Fun: newID("atomic_fence")}})
return nil
}
needsUnsafe("unsupported atomic opcode")
var offset uint64
_, err = readLEB128(t.in) // align
if err == nil {
offset, err = readLEB128(t.in)
}
if err != nil {
return err
}
switch code {
case 0x00: // memory.atomic.notify
fn.push(fn.atomicNotify("atomic_notify", offset))
case 0x01: // memory.atomic.wait32
fn.push(fn.atomicWait("atomic_wait32", offset))
case 0x02: // memory.atomic.wait64
fn.push(fn.atomicWait("atomic_wait64", offset))
case 0x10: // i32.atomic.load
fn.push(fn.atomicLoad("int32", "atomic_load32", offset))
case 0x11: // i64.atomic.load
fn.push(fn.atomicLoad("int64", "atomic_load64", offset))
case 0x12: // i32.atomic.load8_u
fn.push(fn.atomicLoad("int32", "atomic_load8", offset))
case 0x13: // i32.atomic.load16_u
fn.push(fn.atomicLoad("int32", "atomic_load16", offset))
case 0x14: // i64.atomic.load8_u
fn.push(fn.atomicLoad("int64", "atomic_load8", offset))
case 0x15: // i64.atomic.load16_u
fn.push(fn.atomicLoad("int64", "atomic_load16", offset))
case 0x16: // i64.atomic.load32_u
fn.push(fn.atomicLoad("int64", "atomic_load32", offset))
case 0x17, 0x1d: // i32.atomic.store, i64.atomic.store32
fn.emit(fn.atomicStore("atomic_store32", offset))
case 0x18: // i64.atomic.store
fn.emit(fn.atomicStore("atomic_store64", offset))
case 0x19, 0x1b: // i32.atomic.store8, i64.atomic.store8
fn.emit(fn.atomicStore("atomic_store8", offset))
case 0x1a, 0x1c: // i32.atomic.store16, i64.atomic.store16
fn.emit(fn.atomicStore("atomic_store16", offset))
case 0x1e: // i32.atomic.rmw.add
fn.push(fn.atomicRmw("int32", "atomic_add32", offset))
case 0x1f: // i64.atomic.rmw.add
fn.push(fn.atomicRmw("int64", "atomic_add64", offset))
case 0x20: // i32.atomic.rmw8.add_u
fn.push(fn.atomicRmw("int32", "atomic_add8", offset))
case 0x21: // i32.atomic.rmw16.add_u
fn.push(fn.atomicRmw("int32", "atomic_add16", offset))
case 0x22: // i64.atomic.rmw8.add_u
fn.push(fn.atomicRmw("int64", "atomic_add8", offset))
case 0x23: // i64.atomic.rmw16.add_u
fn.push(fn.atomicRmw("int64", "atomic_add16", offset))
case 0x24: // i64.atomic.rmw32.add_u
fn.push(fn.atomicRmw("int64", "atomic_add32", offset))
case 0x25: // i32.atomic.rmw.sub
fn.push(fn.atomicRmw("int32", "atomic_sub32", offset))
case 0x26: // i64.atomic.rmw.sub
fn.push(fn.atomicRmw("int64", "atomic_sub64", offset))
case 0x27: // i32.atomic.rmw8.sub_u
fn.push(fn.atomicRmw("int32", "atomic_sub8", offset))
case 0x28: // i32.atomic.rmw16.sub_u
fn.push(fn.atomicRmw("int32", "atomic_sub16", offset))
case 0x29: // i64.atomic.rmw8.sub_u
fn.push(fn.atomicRmw("int64", "atomic_sub8", offset))
case 0x2a: // i64.atomic.rmw16.sub_u
fn.push(fn.atomicRmw("int64", "atomic_sub16", offset))
case 0x2b: // i64.atomic.rmw32.sub_u
fn.push(fn.atomicRmw("int64", "atomic_sub32", offset))
case 0x2c: // i32.atomic.rmw.and
fn.push(fn.atomicRmw("int32", "atomic_and32", offset))
case 0x2d: // i64.atomic.rmw.and
fn.push(fn.atomicRmw("int64", "atomic_and64", offset))
case 0x2e: // i32.atomic.rmw8.and_u
fn.push(fn.atomicRmw("int32", "atomic_and8", offset))
case 0x2f: // i32.atomic.rmw16.and_u
fn.push(fn.atomicRmw("int32", "atomic_and16", offset))
case 0x30: // i64.atomic.rmw8.and_u
fn.push(fn.atomicRmw("int64", "atomic_and8", offset))
case 0x31: // i64.atomic.rmw16.and_u
fn.push(fn.atomicRmw("int64", "atomic_and16", offset))
case 0x32: // i64.atomic.rmw32.and_u
fn.push(fn.atomicRmw("int64", "atomic_and32", offset))
case 0x33: // i32.atomic.rmw.or
fn.push(fn.atomicRmw("int32", "atomic_or32", offset))
case 0x34: // i64.atomic.rmw.or
fn.push(fn.atomicRmw("int64", "atomic_or64", offset))
case 0x35: // i32.atomic.rmw8.or_u
fn.push(fn.atomicRmw("int32", "atomic_or8", offset))
case 0x36: // i32.atomic.rmw16.or_u
fn.push(fn.atomicRmw("int32", "atomic_or16", offset))
case 0x37: // i64.atomic.rmw8.or_u
fn.push(fn.atomicRmw("int64", "atomic_or8", offset))
case 0x38: // i64.atomic.rmw16.or_u
fn.push(fn.atomicRmw("int64", "atomic_or16", offset))
case 0x39: // i64.atomic.rmw32.or_u
fn.push(fn.atomicRmw("int64", "atomic_or32", offset))
case 0x3a: // i32.atomic.rmw.xor
fn.push(fn.atomicRmw("int32", "atomic_xor32", offset))
case 0x3b: // i64.atomic.rmw.xor
fn.push(fn.atomicRmw("int64", "atomic_xor64", offset))
case 0x3c: // i32.atomic.rmw8.xor_u
fn.push(fn.atomicRmw("int32", "atomic_xor8", offset))
case 0x3d: // i32.atomic.rmw16.xor_u
fn.push(fn.atomicRmw("int32", "atomic_xor16", offset))
case 0x3e: // i64.atomic.rmw8.xor_u
fn.push(fn.atomicRmw("int64", "atomic_xor8", offset))
case 0x3f: // i64.atomic.rmw16.xor_u
fn.push(fn.atomicRmw("int64", "atomic_xor16", offset))
case 0x40: // i64.atomic.rmw32.xor_u
fn.push(fn.atomicRmw("int64", "atomic_xor32", offset))
case 0x41: // i32.atomic.rmw.xchg
fn.push(fn.atomicRmw("int32", "atomic_xchg32", offset))
case 0x42: // i64.atomic.rmw.xchg
fn.push(fn.atomicRmw("int64", "atomic_xchg64", offset))
case 0x43: // i32.atomic.rmw8.xchg_u
fn.push(fn.atomicRmw("int32", "atomic_xchg8", offset))
case 0x44: // i32.atomic.rmw16.xchg_u
fn.push(fn.atomicRmw("int32", "atomic_xchg16", offset))
case 0x45: // i64.atomic.rmw8.xchg_u
fn.push(fn.atomicRmw("int64", "atomic_xchg8", offset))
case 0x46: // i64.atomic.rmw16.xchg_u
fn.push(fn.atomicRmw("int64", "atomic_xchg16", offset))
case 0x47: // i64.atomic.rmw32.xchg_u
fn.push(fn.atomicRmw("int64", "atomic_xchg32", offset))
case 0x48: // i32.atomic.rmw.cmpxchg
fn.push(fn.atomicCmpxchg("int32", "atomic_cmpxchg32", offset))
case 0x49: // i64.atomic.rmw.cmpxchg
fn.push(fn.atomicCmpxchg("int64", "atomic_cmpxchg64", offset))
case 0x4a: // i32.atomic.rmw8.cmpxchg_u
fn.push(fn.atomicCmpxchg("int32", "atomic_cmpxchg8", offset))
case 0x4b: // i32.atomic.rmw16.cmpxchg_u
fn.push(fn.atomicCmpxchg("int32", "atomic_cmpxchg16", offset))
case 0x4c: // i64.atomic.rmw8.cmpxchg_u
fn.push(fn.atomicCmpxchg("int64", "atomic_cmpxchg8", offset))
case 0x4d: // i64.atomic.rmw16.cmpxchg_u
fn.push(fn.atomicCmpxchg("int64", "atomic_cmpxchg16", offset))
case 0x4e: // i64.atomic.rmw32.cmpxchg_u
fn.push(fn.atomicCmpxchg("int64", "atomic_cmpxchg32", offset))
default:
return fmt.Errorf("unsupported opcode (atomic): 0xFE 0x%02X", code)
}
return nil
}
func (fn *funcCompiler) atomicLoad(typ, name string, offset uint64) ast.Expr {
addr := fn.popAddr(offset)
fn.helpers.add(name)
return convert(&ast.CallExpr{
Fun: newID(name),
Args: []ast.Expr{fn.memory.selector, addr},
}, typ)
}
func (fn *funcCompiler) atomicStore(name string, offset uint64) ast.Stmt {
val := fn.pop()
addr := fn.popAddr(offset)
bits := name[strings.IndexAny(name, "0123456789"):]
val = convert(val, "uint"+bits)
fn.helpers.add(name)
fn.helpers.add("atomic_ptr" + bits)
return &ast.ExprStmt{X: &ast.CallExpr{
Fun: newID(name),
Args: []ast.Expr{fn.memory.selector, addr, val},
}}
}
func (fn *funcCompiler) atomicRmw(typ, name string, offset uint64) ast.Expr {
val := fn.pop()
addr := fn.popAddr(offset)
bits := name[strings.IndexAny(name, "0123456789"):]
val = convert(val, "uint"+bits)
fn.helpers.add(name)
fn.helpers.add("atomic_ptr" + bits)
return convert(&ast.CallExpr{
Fun: newID(name),
Args: []ast.Expr{fn.memory.selector, addr, val},
}, typ)
}
func (fn *funcCompiler) atomicCmpxchg(typ, name string, offset uint64) ast.Expr {
new := fn.pop()
old := fn.pop()
addr := fn.popAddr(offset)
bits := name[strings.IndexAny(name, "0123456789"):]
new = convert(new, "uint"+bits)
old = convert(old, "uint"+bits)
fn.helpers.add(name)
fn.helpers.add("atomic_ptr" + bits)
return convert(&ast.CallExpr{
Fun: newID(name),
Args: []ast.Expr{fn.memory.selector, addr, old, new},
}, typ)
}
func (fn *funcCompiler) atomicNotify(name string, offset uint64) ast.Expr {
count := fn.pop()
addr := fn.popAddr(offset)
fn.helpers.add(name)
fn.helpers.add("atomic_waiters")
fn.helpers.add("atomic_ptr32")
return &ast.CallExpr{
Fun: newID(name),
Args: []ast.Expr{
fn.memory.selector, addr, count,
modSel(newID("waiters"))}}
}
func (fn *funcCompiler) atomicWait(name string, offset uint64) ast.Expr {
timeout := fn.pop()
exp := fn.pop()
addr := fn.popAddr(offset)
bits := name[strings.IndexAny(name, "0123456789"):]
exp = convert(exp, "uint"+bits)
fn.helpers.add(name)
fn.helpers.add("atomic_wait")
fn.helpers.add("atomic_waiters")
fn.helpers.add("atomic_ptr" + bits)
return &ast.CallExpr{
Fun: newID(name),
Args: []ast.Expr{
fn.memory.selector, addr, exp, timeout,
modSel(newID("waiters"))}}
}