Debian Codegraph @

internal/engine/wazevo/frontend/lower.go

raw
1 package frontend
2
3 import (
4 "encoding/binary"
5 "fmt"
6 "math"
7 "runtime"
8 "strings"
9
10 "github.com/tetratelabs/wazero/api"
11 "github.com/tetratelabs/wazero/internal/engine/wazevo/ssa"
12 "github.com/tetratelabs/wazero/internal/engine/wazevo/wazevoapi"
13 "github.com/tetratelabs/wazero/internal/leb128"
14 "github.com/tetratelabs/wazero/internal/wasm"
15 )
16
17 type (
18 // loweringState is used to keep the state of lowering.
19 loweringState struct {
20 // values holds the values on the Wasm stack.
21 values []ssa.Value
22 controlFrames []controlFrame
23 unreachable bool
24 unreachableDepth int
25 tmpForBrTable []uint32
26 pc int
27 }
28 controlFrame struct {
29 kind controlFrameKind
30 // originalStackLen holds the number of values on the Wasm stack
31 // when start executing this control frame minus params for the block.
32 originalStackLenWithoutParam int
33 // blk is the loop header if this is loop, and is the else-block if this is an if frame.
34 blk,
35 // followingBlock is the basic block we enter if we reach "end" of block.
36 followingBlock ssa.BasicBlock
37 blockType *wasm.FunctionType
38 // clonedArgs hold the arguments to Else block.
39 clonedArgs ssa.Values
40 }
41
42 controlFrameKind byte
43 )
44
45 // String implements fmt.Stringer for debugging.
46 func (l *loweringState) String() string {
47 var str []string
48 for _, v := range l.values {
49 str = append(str, fmt.Sprintf("v%v", v.ID()))
50 }
51 var frames []string
52 for i := range l.controlFrames {
53 frames = append(frames, l.controlFrames[i].kind.String())
54 }
55 return fmt.Sprintf("\n\tunreachable=%v(depth=%d)\n\tstack: %s\n\tcontrol frames: %s",
56 l.unreachable, l.unreachableDepth,
57 strings.Join(str, ", "),
58 strings.Join(frames, ", "),
59 )
60 }
61
62 const (
63 controlFrameKindFunction = iota + 1
64 controlFrameKindLoop
65 controlFrameKindIfWithElse
66 controlFrameKindIfWithoutElse
67 controlFrameKindBlock
68 controlFrameKindTryTable
69 )
70
71 // String implements fmt.Stringer for debugging.
72 func (k controlFrameKind) String() string {
73 switch k {
74 case controlFrameKindFunction:
75 return "function"
76 case controlFrameKindLoop:
77 return "loop"
78 case controlFrameKindIfWithElse:
79 return "if_with_else"
80 case controlFrameKindIfWithoutElse:
81 return "if_without_else"
82 case controlFrameKindBlock:
83 return "block"
84 case controlFrameKindTryTable:
85 return "try_table"
86 default:
87 panic(k)
88 }
89 }
90
91 // isLoop returns true if this is a loop frame.
92 func (ctrl *controlFrame) isLoop() bool {
93 return ctrl.kind == controlFrameKindLoop
94 }
95
96 // reset resets the state of loweringState for reuse.
97 func (l *loweringState) reset() {
98 l.values = l.values[:0]
99 l.controlFrames = l.controlFrames[:0]
100 l.pc = 0
101 l.unreachable = false
102 l.unreachableDepth = 0
103 }
104
105 func (l *loweringState) peek() (ret ssa.Value) {
106 tail := len(l.values) - 1
107 return l.values[tail]
108 }
109
110 func (l *loweringState) pop() (ret ssa.Value) {
111 tail := len(l.values) - 1
112 ret = l.values[tail]
113 l.values = l.values[:tail]
114 return
115 }
116
117 func (l *loweringState) push(ret ssa.Value) {
118 l.values = append(l.values, ret)
119 }
120
121 func (c *Compiler) nPeekDup(n int) ssa.Values {
122 if n == 0 {
123 return ssa.ValuesNil
124 }
125
126 l := c.state()
127 tail := len(l.values)
128
129 args := c.allocateVarLengthValues(n, l.values[tail-n:tail]...)
130 return args
131 }
132
133 func (l *loweringState) ctrlPop() (ret controlFrame) {
134 tail := len(l.controlFrames) - 1
135 ret = l.controlFrames[tail]
136 l.controlFrames = l.controlFrames[:tail]
137 return
138 }
139
140 func (l *loweringState) ctrlPush(ret controlFrame) {
141 l.controlFrames = append(l.controlFrames, ret)
142 }
143
144 func (l *loweringState) ctrlPeekAt(n int) (ret *controlFrame) {
145 tail := len(l.controlFrames) - 1
146 return &l.controlFrames[tail-n]
147 }
148
149 // lowerBody lowers the body of the Wasm function to the SSA form.
150 func (c *Compiler) lowerBody(entryBlk ssa.BasicBlock) {
151 c.ssaBuilder.Seal(entryBlk)
152
153 if c.needListener {
154 c.callListenerBefore()
155 }
156
157 // Pushes the empty control frame which corresponds to the function return.
158 c.loweringState.ctrlPush(controlFrame{
159 kind: controlFrameKindFunction,
160 blockType: c.wasmFunctionTyp,
161 followingBlock: c.ssaBuilder.ReturnBlock(),
162 })
163
164 for c.loweringState.pc < len(c.wasmFunctionBody) {
165 blkBeforeLowering := c.ssaBuilder.CurrentBlock()
166 c.lowerCurrentOpcode()
167 blkAfterLowering := c.ssaBuilder.CurrentBlock()
168 if blkBeforeLowering != blkAfterLowering {
169 // In Wasm, once a block exits, that means we've done compiling the block.
170 // Therefore, we finalize the known bounds at the end of the block for the exiting block.
171 c.finalizeKnownSafeBoundsAtTheEndOfBlock(blkBeforeLowering.ID())
172 // After that, we initialize the known bounds for the new compilation target block.
173 c.initializeCurrentBlockKnownBounds()
174 }
175 }
176 }
177
178 func (c *Compiler) state() *loweringState {
179 return &c.loweringState
180 }
181
182 func (c *Compiler) lowerCurrentOpcode() {
183 op := c.wasmFunctionBody[c.loweringState.pc]
184
185 if c.needSourceOffsetInfo {
186 c.ssaBuilder.SetCurrentSourceOffset(
187 ssa.SourceOffset(c.loweringState.pc) + ssa.SourceOffset(c.wasmFunctionBodyOffsetInCodeSection),
188 )
189 }
190
191 builder := c.ssaBuilder
192 state := c.state()
193 switch op {
194 case wasm.OpcodeI32Const:
195 c := c.readI32s()
196 if state.unreachable {
197 break
198 }
199
200 iconst := builder.AllocateInstruction().AsIconst32(uint32(c)).Insert(builder)
201 value := iconst.Return()
202 state.push(value)
203 case wasm.OpcodeI64Const:
204 c := c.readI64s()
205 if state.unreachable {
206 break
207 }
208 iconst := builder.AllocateInstruction().AsIconst64(uint64(c)).Insert(builder)
209 value := iconst.Return()
210 state.push(value)
211 case wasm.OpcodeF32Const:
212 f32 := c.readF32()
213 if state.unreachable {
214 break
215 }
216 f32const := builder.AllocateInstruction().
217 AsF32const(f32).
218 Insert(builder).
219 Return()
220 state.push(f32const)
221 case wasm.OpcodeF64Const:
222 f64 := c.readF64()
223 if state.unreachable {
224 break
225 }
226 f64const := builder.AllocateInstruction().
227 AsF64const(f64).
228 Insert(builder).
229 Return()
230 state.push(f64const)
231 case wasm.OpcodeI32Add, wasm.OpcodeI64Add:
232 if state.unreachable {
233 break
234 }
235 y, x := state.pop(), state.pop()
236 iadd := builder.AllocateInstruction()
237 iadd.AsIadd(x, y)
238 builder.InsertInstruction(iadd)
239 value := iadd.Return()
240 state.push(value)
241 case wasm.OpcodeI32Sub, wasm.OpcodeI64Sub:
242 if state.unreachable {
243 break
244 }
245 y, x := state.pop(), state.pop()
246 isub := builder.AllocateInstruction()
247 isub.AsIsub(x, y)
248 builder.InsertInstruction(isub)
249 value := isub.Return()
250 state.push(value)
251 case wasm.OpcodeF32Add, wasm.OpcodeF64Add:
252 if state.unreachable {
253 break
254 }
255 y, x := state.pop(), state.pop()
256 iadd := builder.AllocateInstruction()
257 iadd.AsFadd(x, y)
258 builder.InsertInstruction(iadd)
259 value := iadd.Return()
260 state.push(value)
261 case wasm.OpcodeI32Mul, wasm.OpcodeI64Mul:
262 if state.unreachable {
263 break
264 }
265 y, x := state.pop(), state.pop()
266 imul := builder.AllocateInstruction()
267 imul.AsImul(x, y)
268 builder.InsertInstruction(imul)
269 value := imul.Return()
270 state.push(value)
271 case wasm.OpcodeF32Sub, wasm.OpcodeF64Sub:
272 if state.unreachable {
273 break
274 }
275 y, x := state.pop(), state.pop()
276 isub := builder.AllocateInstruction()
277 isub.AsFsub(x, y)
278 builder.InsertInstruction(isub)
279 value := isub.Return()
280 state.push(value)
281 case wasm.OpcodeF32Mul, wasm.OpcodeF64Mul:
282 if state.unreachable {
283 break
284 }
285 y, x := state.pop(), state.pop()
286 isub := builder.AllocateInstruction()
287 isub.AsFmul(x, y)
288 builder.InsertInstruction(isub)
289 value := isub.Return()
290 state.push(value)
291 case wasm.OpcodeF32Div, wasm.OpcodeF64Div:
292 if state.unreachable {
293 break
294 }
295 y, x := state.pop(), state.pop()
296 isub := builder.AllocateInstruction()
297 isub.AsFdiv(x, y)
298 builder.InsertInstruction(isub)
299 value := isub.Return()
300 state.push(value)
301 case wasm.OpcodeF32Max, wasm.OpcodeF64Max:
302 if state.unreachable {
303 break
304 }
305 y, x := state.pop(), state.pop()
306 isub := builder.AllocateInstruction()
307 isub.AsFmax(x, y)
308 builder.InsertInstruction(isub)
309 value := isub.Return()
310 state.push(value)
311 case wasm.OpcodeF32Min, wasm.OpcodeF64Min:
312 if state.unreachable {
313 break
314 }
315 y, x := state.pop(), state.pop()
316 isub := builder.AllocateInstruction()
317 isub.AsFmin(x, y)
318 builder.InsertInstruction(isub)
319 value := isub.Return()
320 state.push(value)
321 case wasm.OpcodeI64Extend8S:
322 if state.unreachable {
323 break
324 }
325 c.insertIntegerExtend(true, 8, 64)
326 case wasm.OpcodeI64Extend16S:
327 if state.unreachable {
328 break
329 }
330 c.insertIntegerExtend(true, 16, 64)
331 case wasm.OpcodeI64Extend32S, wasm.OpcodeI64ExtendI32S:
332 if state.unreachable {
333 break
334 }
335 c.insertIntegerExtend(true, 32, 64)
336 case wasm.OpcodeI64ExtendI32U:
337 if state.unreachable {
338 break
339 }
340 c.insertIntegerExtend(false, 32, 64)
341 case wasm.OpcodeI32Extend8S:
342 if state.unreachable {
343 break
344 }
345 c.insertIntegerExtend(true, 8, 32)
346 case wasm.OpcodeI32Extend16S:
347 if state.unreachable {
348 break
349 }
350 c.insertIntegerExtend(true, 16, 32)
351 case wasm.OpcodeI32Eqz, wasm.OpcodeI64Eqz:
352 if state.unreachable {
353 break
354 }
355 x := state.pop()
356 zero := builder.AllocateInstruction()
357 if op == wasm.OpcodeI32Eqz {
358 zero.AsIconst32(0)
359 } else {
360 zero.AsIconst64(0)
361 }
362 builder.InsertInstruction(zero)
363 icmp := builder.AllocateInstruction().
364 AsIcmp(x, zero.Return(), ssa.IntegerCmpCondEqual).
365 Insert(builder).
366 Return()
367 state.push(icmp)
368 case wasm.OpcodeI32Eq, wasm.OpcodeI64Eq:
369 if state.unreachable {
370 break
371 }
372 c.insertIcmp(ssa.IntegerCmpCondEqual)
373 case wasm.OpcodeI32Ne, wasm.OpcodeI64Ne:
374 if state.unreachable {
375 break
376 }
377 c.insertIcmp(ssa.IntegerCmpCondNotEqual)
378 case wasm.OpcodeI32LtS, wasm.OpcodeI64LtS:
379 if state.unreachable {
380 break
381 }
382 c.insertIcmp(ssa.IntegerCmpCondSignedLessThan)
383 case wasm.OpcodeI32LtU, wasm.OpcodeI64LtU:
384 if state.unreachable {
385 break
386 }
387 c.insertIcmp(ssa.IntegerCmpCondUnsignedLessThan)
388 case wasm.OpcodeI32GtS, wasm.OpcodeI64GtS:
389 if state.unreachable {
390 break
391 }
392 c.insertIcmp(ssa.IntegerCmpCondSignedGreaterThan)
393 case wasm.OpcodeI32GtU, wasm.OpcodeI64GtU:
394 if state.unreachable {
395 break
396 }
397 c.insertIcmp(ssa.IntegerCmpCondUnsignedGreaterThan)
398 case wasm.OpcodeI32LeS, wasm.OpcodeI64LeS:
399 if state.unreachable {
400 break
401 }
402 c.insertIcmp(ssa.IntegerCmpCondSignedLessThanOrEqual)
403 case wasm.OpcodeI32LeU, wasm.OpcodeI64LeU:
404 if state.unreachable {
405 break
406 }
407 c.insertIcmp(ssa.IntegerCmpCondUnsignedLessThanOrEqual)
408 case wasm.OpcodeI32GeS, wasm.OpcodeI64GeS:
409 if state.unreachable {
410 break
411 }
412 c.insertIcmp(ssa.IntegerCmpCondSignedGreaterThanOrEqual)
413 case wasm.OpcodeI32GeU, wasm.OpcodeI64GeU:
414 if state.unreachable {
415 break
416 }
417 c.insertIcmp(ssa.IntegerCmpCondUnsignedGreaterThanOrEqual)
418
419 case wasm.OpcodeF32Eq, wasm.OpcodeF64Eq:
420 if state.unreachable {
421 break
422 }
423 c.insertFcmp(ssa.FloatCmpCondEqual)
424 case wasm.OpcodeF32Ne, wasm.OpcodeF64Ne:
425 if state.unreachable {
426 break
427 }
428 c.insertFcmp(ssa.FloatCmpCondNotEqual)
429 case wasm.OpcodeF32Lt, wasm.OpcodeF64Lt:
430 if state.unreachable {
431 break
432 }
433 c.insertFcmp(ssa.FloatCmpCondLessThan)
434 case wasm.OpcodeF32Gt, wasm.OpcodeF64Gt:
435 if state.unreachable {
436 break
437 }
438 c.insertFcmp(ssa.FloatCmpCondGreaterThan)
439 case wasm.OpcodeF32Le, wasm.OpcodeF64Le:
440 if state.unreachable {
441 break
442 }
443 c.insertFcmp(ssa.FloatCmpCondLessThanOrEqual)
444 case wasm.OpcodeF32Ge, wasm.OpcodeF64Ge:
445 if state.unreachable {
446 break
447 }
448 c.insertFcmp(ssa.FloatCmpCondGreaterThanOrEqual)
449 case wasm.OpcodeF32Neg, wasm.OpcodeF64Neg:
450 if state.unreachable {
451 break
452 }
453 x := state.pop()
454 v := builder.AllocateInstruction().AsFneg(x).Insert(builder).Return()
455 state.push(v)
456 case wasm.OpcodeF32Sqrt, wasm.OpcodeF64Sqrt:
457 if state.unreachable {
458 break
459 }
460 x := state.pop()
461 v := builder.AllocateInstruction().AsSqrt(x).Insert(builder).Return()
462 state.push(v)
463 case wasm.OpcodeF32Abs, wasm.OpcodeF64Abs:
464 if state.unreachable {
465 break
466 }
467 x := state.pop()
468 v := builder.AllocateInstruction().AsFabs(x).Insert(builder).Return()
469 state.push(v)
470 case wasm.OpcodeF32Copysign, wasm.OpcodeF64Copysign:
471 if state.unreachable {
472 break
473 }
474 y, x := state.pop(), state.pop()
475 v := builder.AllocateInstruction().AsFcopysign(x, y).Insert(builder).Return()
476 state.push(v)
477
478 case wasm.OpcodeF32Ceil, wasm.OpcodeF64Ceil:
479 if state.unreachable {
480 break
481 }
482 x := state.pop()
483 v := builder.AllocateInstruction().AsCeil(x).Insert(builder).Return()
484 state.push(v)
485 case wasm.OpcodeF32Floor, wasm.OpcodeF64Floor:
486 if state.unreachable {
487 break
488 }
489 x := state.pop()
490 v := builder.AllocateInstruction().AsFloor(x).Insert(builder).Return()
491 state.push(v)
492 case wasm.OpcodeF32Trunc, wasm.OpcodeF64Trunc:
493 if state.unreachable {
494 break
495 }
496 x := state.pop()
497 v := builder.AllocateInstruction().AsTrunc(x).Insert(builder).Return()
498 state.push(v)
499 case wasm.OpcodeF32Nearest, wasm.OpcodeF64Nearest:
500 if state.unreachable {
501 break
502 }
503 x := state.pop()
504 v := builder.AllocateInstruction().AsNearest(x).Insert(builder).Return()
505 state.push(v)
506 case wasm.OpcodeI64TruncF64S, wasm.OpcodeI64TruncF32S,
507 wasm.OpcodeI32TruncF64S, wasm.OpcodeI32TruncF32S,
508 wasm.OpcodeI64TruncF64U, wasm.OpcodeI64TruncF32U,
509 wasm.OpcodeI32TruncF64U, wasm.OpcodeI32TruncF32U:
510 if state.unreachable {
511 break
512 }
513 ret := builder.AllocateInstruction().AsFcvtToInt(
514 state.pop(),
515 c.execCtxPtrValue,
516 op == wasm.OpcodeI64TruncF64S || op == wasm.OpcodeI64TruncF32S || op == wasm.OpcodeI32TruncF32S || op == wasm.OpcodeI32TruncF64S,
517 op == wasm.OpcodeI64TruncF64S || op == wasm.OpcodeI64TruncF32S || op == wasm.OpcodeI64TruncF64U || op == wasm.OpcodeI64TruncF32U,
518 false,
519 ).Insert(builder).Return()
520 state.push(ret)
521 case wasm.OpcodeMiscPrefix:
522 state.pc++
523 // A misc opcode is encoded as an unsigned variable 32-bit integer.
524 miscOpUint, num, err := leb128.LoadUint32(c.wasmFunctionBody[state.pc:])
525 if err != nil {
526 // In normal conditions this should never happen because the function has passed validation.
527 panic(fmt.Sprintf("failed to read misc opcode: %v", err))
528 }
529 state.pc += int(num - 1)
530 miscOp := wasm.OpcodeMisc(miscOpUint)
531 switch miscOp {
532 case wasm.OpcodeMiscI64TruncSatF64S, wasm.OpcodeMiscI64TruncSatF32S,
533 wasm.OpcodeMiscI32TruncSatF64S, wasm.OpcodeMiscI32TruncSatF32S,
534 wasm.OpcodeMiscI64TruncSatF64U, wasm.OpcodeMiscI64TruncSatF32U,
535 wasm.OpcodeMiscI32TruncSatF64U, wasm.OpcodeMiscI32TruncSatF32U:
536 if state.unreachable {
537 break
538 }
539 ret := builder.AllocateInstruction().AsFcvtToInt(
540 state.pop(),
541 c.execCtxPtrValue,
542 miscOp == wasm.OpcodeMiscI64TruncSatF64S || miscOp == wasm.OpcodeMiscI64TruncSatF32S || miscOp == wasm.OpcodeMiscI32TruncSatF32S || miscOp == wasm.OpcodeMiscI32TruncSatF64S,
543 miscOp == wasm.OpcodeMiscI64TruncSatF64S || miscOp == wasm.OpcodeMiscI64TruncSatF32S || miscOp == wasm.OpcodeMiscI64TruncSatF64U || miscOp == wasm.OpcodeMiscI64TruncSatF32U,
544 true,
545 ).Insert(builder).Return()
546 state.push(ret)
547
548 case wasm.OpcodeMiscTableSize:
549 tableIndex := c.readI32u()
550 if state.unreachable {
551 break
552 }
553
554 // Load the table.
555 loadTableInstancePtr := builder.AllocateInstruction()
556 loadTableInstancePtr.AsLoad(c.moduleCtxPtrValue, c.offset.TableOffset(int(tableIndex)).U32(), ssa.TypeI64)
557 builder.InsertInstruction(loadTableInstancePtr)
558 tableInstancePtr := loadTableInstancePtr.Return()
559
560 // Load the table's length.
561 loadTableLen := builder.AllocateInstruction().
562 AsLoad(tableInstancePtr, tableInstanceLenOffset, ssa.TypeI32).
563 Insert(builder)
564 state.push(loadTableLen.Return())
565
566 case wasm.OpcodeMiscTableGrow:
567 tableIndex := c.readI32u()
568 if state.unreachable {
569 break
570 }
571
572 c.storeCallerModuleContext()
573
574 tableIndexVal := builder.AllocateInstruction().AsIconst32(tableIndex).Insert(builder).Return()
575
576 num := state.pop()
577 r := state.pop()
578
579 tableGrowPtr := builder.AllocateInstruction().
580 AsLoad(c.execCtxPtrValue,
581 wazevoapi.ExecutionContextOffsetTableGrowTrampolineAddress.U32(),
582 ssa.TypeI64,
583 ).Insert(builder).Return()
584
585 args := c.allocateVarLengthValues(4, c.execCtxPtrValue, tableIndexVal, num, r)
586 callGrowRet := builder.
587 AllocateInstruction().
588 AsCallIndirect(tableGrowPtr, &c.tableGrowSig, args).
589 Insert(builder).Return()
590 state.push(callGrowRet)
591
592 case wasm.OpcodeMiscTableCopy:
593 dstTableIndex := c.readI32u()
594 srcTableIndex := c.readI32u()
595 if state.unreachable {
596 break
597 }
598
599 copySize := builder.
600 AllocateInstruction().AsUExtend(state.pop(), 32, 64).Insert(builder).Return()
601 srcOffset := builder.
602 AllocateInstruction().AsUExtend(state.pop(), 32, 64).Insert(builder).Return()
603 dstOffset := builder.
604 AllocateInstruction().AsUExtend(state.pop(), 32, 64).Insert(builder).Return()
605
606 // Out of bounds check.
607 dstTableInstancePtr := c.boundsCheckInTable(dstTableIndex, dstOffset, copySize)
608 srcTableInstancePtr := c.boundsCheckInTable(srcTableIndex, srcOffset, copySize)
609
610 dstTableBaseAddr := c.loadTableBaseAddr(dstTableInstancePtr)
611 srcTableBaseAddr := c.loadTableBaseAddr(srcTableInstancePtr)
612
613 three := builder.AllocateInstruction().AsIconst64(3).Insert(builder).Return()
614
615 dstOffsetInBytes := builder.AllocateInstruction().AsIshl(dstOffset, three).Insert(builder).Return()
616 dstAddr := builder.AllocateInstruction().AsIadd(dstTableBaseAddr, dstOffsetInBytes).Insert(builder).Return()
617 srcOffsetInBytes := builder.AllocateInstruction().AsIshl(srcOffset, three).Insert(builder).Return()
618 srcAddr := builder.AllocateInstruction().AsIadd(srcTableBaseAddr, srcOffsetInBytes).Insert(builder).Return()
619
620 copySizeInBytes := builder.AllocateInstruction().AsIshl(copySize, three).Insert(builder).Return()
621 c.callMemmove(dstAddr, srcAddr, copySizeInBytes)
622
623 case wasm.OpcodeMiscMemoryCopy:
624 state.pc += 2 // +2 to skip two memory indexes which are fixed to zero.
625 if state.unreachable {
626 break
627 }
628
629 copySize := builder.
630 AllocateInstruction().AsUExtend(state.pop(), 32, 64).Insert(builder).Return()
631 srcOffset := builder.
632 AllocateInstruction().AsUExtend(state.pop(), 32, 64).Insert(builder).Return()
633 dstOffset := builder.
634 AllocateInstruction().AsUExtend(state.pop(), 32, 64).Insert(builder).Return()
635
636 // Out of bounds check.
637 memLen := c.getMemoryLenValue(false)
638 c.boundsCheckInMemory(memLen, dstOffset, copySize)
639 c.boundsCheckInMemory(memLen, srcOffset, copySize)
640
641 memBase := c.getMemoryBaseValue(false)
642 dstAddr := builder.AllocateInstruction().AsIadd(memBase, dstOffset).Insert(builder).Return()
643 srcAddr := builder.AllocateInstruction().AsIadd(memBase, srcOffset).Insert(builder).Return()
644
645 c.callMemmove(dstAddr, srcAddr, copySize)
646
647 case wasm.OpcodeMiscTableFill:
648 tableIndex := c.readI32u()
649 if state.unreachable {
650 break
651 }
652 fillSize := state.pop()
653 value := state.pop()
654 offset := state.pop()
655
656 fillSizeExt := builder.
657 AllocateInstruction().AsUExtend(fillSize, 32, 64).Insert(builder).Return()
658 offsetExt := builder.
659 AllocateInstruction().AsUExtend(offset, 32, 64).Insert(builder).Return()
660 tableInstancePtr := c.boundsCheckInTable(tableIndex, offsetExt, fillSizeExt)
661
662 three := builder.AllocateInstruction().AsIconst64(3).Insert(builder).Return()
663 offsetInBytes := builder.AllocateInstruction().AsIshl(offsetExt, three).Insert(builder).Return()
664 fillSizeInBytes := builder.AllocateInstruction().AsIshl(fillSizeExt, three).Insert(builder).Return()
665
666 // Calculate the base address of the table.
667 tableBaseAddr := c.loadTableBaseAddr(tableInstancePtr)
668 addr := builder.AllocateInstruction().AsIadd(tableBaseAddr, offsetInBytes).Insert(builder).Return()
669
670 // Uses the copy trick for faster filling buffer like memory.fill, but in this case we copy 8 bytes at a time.
671 // Tables are rarely huge, so ignore the 8KB maximum.
672 // https://github.com/golang/go/blob/go1.24.0/src/slices/slices.go#L514-L517
673 //
674 // buf := memoryInst.Buffer[offset : offset+fillSize]
675 // buf[0:8] = value
676 // for i := 8; i < fillSize; i *= 2 { Begin with 8 bytes.
677 // copy(buf[i:], buf[:i])
678 // }
679
680 // Prepare the loop and following block.
681 beforeLoop := builder.AllocateBasicBlock()
682 loopBlk := builder.AllocateBasicBlock()
683 loopVar := loopBlk.AddParam(builder, ssa.TypeI64)
684 followingBlk := builder.AllocateBasicBlock()
685
686 // Insert the jump to the beforeLoop block; If the fillSize is zero, then jump to the following block to skip entire logics.
687 zero := builder.AllocateInstruction().AsIconst64(0).Insert(builder).Return()
688 ifFillSizeZero := builder.AllocateInstruction().AsIcmp(fillSizeExt, zero, ssa.IntegerCmpCondEqual).
689 Insert(builder).Return()
690 builder.AllocateInstruction().AsBrnz(ifFillSizeZero, ssa.ValuesNil, followingBlk).Insert(builder)
691 c.insertJumpToBlock(ssa.ValuesNil, beforeLoop)
692
693 // buf[0:8] = value
694 builder.SetCurrentBlock(beforeLoop)
695 builder.AllocateInstruction().AsStore(ssa.OpcodeStore, value, addr, 0).Insert(builder)
696 eight := builder.AllocateInstruction().AsIconst64(8).Insert(builder).Return()
697 c.insertJumpToBlock(c.allocateVarLengthValues(1, eight), loopBlk)
698
699 builder.SetCurrentBlock(loopBlk)
700 dstAddr := builder.AllocateInstruction().AsIadd(addr, loopVar).Insert(builder).Return()
701
702 newLoopVar := builder.AllocateInstruction().AsIadd(loopVar, loopVar).Insert(builder).Return()
703 newLoopVarLessThanFillSize := builder.AllocateInstruction().
704 AsIcmp(newLoopVar, fillSizeInBytes, ssa.IntegerCmpCondUnsignedLessThan).Insert(builder).Return()
705
706 // On the last iteration, count must be fillSizeInBytes-loopVar.
707 diff := builder.AllocateInstruction().AsIsub(fillSizeInBytes, loopVar).Insert(builder).Return()
708 count := builder.AllocateInstruction().AsSelect(newLoopVarLessThanFillSize, loopVar, diff).Insert(builder).Return()
709
710 c.callMemmove(dstAddr, addr, count)
711
712 builder.AllocateInstruction().
713 AsBrnz(newLoopVarLessThanFillSize, c.allocateVarLengthValues(1, newLoopVar), loopBlk).
714 Insert(builder)
715
716 c.insertJumpToBlock(ssa.ValuesNil, followingBlk)
717 builder.SetCurrentBlock(followingBlk)
718
719 builder.Seal(beforeLoop)
720 builder.Seal(loopBlk)
721 builder.Seal(followingBlk)
722
723 case wasm.OpcodeMiscMemoryFill:
724 state.pc++ // Skip the memory index which is fixed to zero.
725 if state.unreachable {
726 break
727 }
728
729 fillSize := builder.
730 AllocateInstruction().AsUExtend(state.pop(), 32, 64).Insert(builder).Return()
731 value := state.pop()
732 offset := builder.
733 AllocateInstruction().AsUExtend(state.pop(), 32, 64).Insert(builder).Return()
734
735 // Out of bounds check.
736 c.boundsCheckInMemory(c.getMemoryLenValue(false), offset, fillSize)
737
738 // Calculate the base address:
739 addr := builder.AllocateInstruction().AsIadd(c.getMemoryBaseValue(false), offset).Insert(builder).Return()
740
741 // Uses the copy trick for faster filling buffer, with a maximum chunk size of 8KB.
742 // https://github.com/golang/go/blob/go1.24.0/src/bytes/bytes.go#L664-L673
743 //
744 // buf := memoryInst.Buffer[offset : offset+fillSize]
745 // buf[0] = value
746 // for i := 1; i < fillSize; {
747 // chunk := ((i - 1) & 8191) + 1
748 // copy(buf[i:], buf[:chunk])
749 // i += chunk
750 // }
751
752 // Prepare the loop and following block.
753 beforeLoop := builder.AllocateBasicBlock()
754 loopBlk := builder.AllocateBasicBlock()
755 loopVar := loopBlk.AddParam(builder, ssa.TypeI64)
756 followingBlk := builder.AllocateBasicBlock()
757
758 // Insert the jump to the beforeLoop block; If the fillSize is zero, then jump to the following block to skip entire logics.
759 zero := builder.AllocateInstruction().AsIconst64(0).Insert(builder).Return()
760 ifFillSizeZero := builder.AllocateInstruction().AsIcmp(fillSize, zero, ssa.IntegerCmpCondEqual).
761 Insert(builder).Return()
762 builder.AllocateInstruction().AsBrnz(ifFillSizeZero, ssa.ValuesNil, followingBlk).Insert(builder)
763 c.insertJumpToBlock(ssa.ValuesNil, beforeLoop)
764
765 // buf[0] = value
766 builder.SetCurrentBlock(beforeLoop)
767 builder.AllocateInstruction().AsStore(ssa.OpcodeIstore8, value, addr, 0).Insert(builder)
768 one := builder.AllocateInstruction().AsIconst64(1).Insert(builder).Return()
769 c.insertJumpToBlock(c.allocateVarLengthValues(1, one), loopBlk)
770
771 builder.SetCurrentBlock(loopBlk)
772 dstAddr := builder.AllocateInstruction().AsIadd(addr, loopVar).Insert(builder).Return()
773
774 // chunk := ((i - 1) & 8191) + 1
775 mask := builder.AllocateInstruction().AsIconst64(8191).Insert(builder).Return()
776 tmp1 := builder.AllocateInstruction().AsIsub(loopVar, one).Insert(builder).Return()
777 tmp2 := builder.AllocateInstruction().AsBand(tmp1, mask).Insert(builder).Return()
778 chunk := builder.AllocateInstruction().AsIadd(tmp2, one).Insert(builder).Return()
779
780 // i += chunk
781 newLoopVar := builder.AllocateInstruction().AsIadd(loopVar, chunk).Insert(builder).Return()
782 newLoopVarLessThanFillSize := builder.AllocateInstruction().
783 AsIcmp(newLoopVar, fillSize, ssa.IntegerCmpCondUnsignedLessThan).Insert(builder).Return()
784
785 // count = min(chunk, fillSize-loopVar)
786 diff := builder.AllocateInstruction().AsIsub(fillSize, loopVar).Insert(builder).Return()
787 count := builder.AllocateInstruction().AsSelect(newLoopVarLessThanFillSize, chunk, diff).Insert(builder).Return()
788
789 c.callMemmove(dstAddr, addr, count)
790
791 builder.AllocateInstruction().
792 AsBrnz(newLoopVarLessThanFillSize, c.allocateVarLengthValues(1, newLoopVar), loopBlk).
793 Insert(builder)
794
795 c.insertJumpToBlock(ssa.ValuesNil, followingBlk)
796 builder.SetCurrentBlock(followingBlk)
797
798 builder.Seal(beforeLoop)
799 builder.Seal(loopBlk)
800 builder.Seal(followingBlk)
801
802 case wasm.OpcodeMiscMemoryInit:
803 index := c.readI32u()
804 state.pc++ // Skip the memory index which is fixed to zero.
805 if state.unreachable {
806 break
807 }
808
809 copySize := builder.
810 AllocateInstruction().AsUExtend(state.pop(), 32, 64).Insert(builder).Return()
811 offsetInDataInstance := builder.
812 AllocateInstruction().AsUExtend(state.pop(), 32, 64).Insert(builder).Return()
813 offsetInMemory := builder.
814 AllocateInstruction().AsUExtend(state.pop(), 32, 64).Insert(builder).Return()
815
816 dataInstPtr := c.dataOrElementInstanceAddr(index, c.offset.DataInstances1stElement)
817
818 // Bounds check.
819 c.boundsCheckInMemory(c.getMemoryLenValue(false), offsetInMemory, copySize)
820 c.boundsCheckInDataOrElementInstance(dataInstPtr, offsetInDataInstance, copySize, wazevoapi.ExitCodeMemoryOutOfBounds)
821
822 dataInstBaseAddr := builder.AllocateInstruction().AsLoad(dataInstPtr, 0, ssa.TypeI64).Insert(builder).Return()
823 srcAddr := builder.AllocateInstruction().AsIadd(dataInstBaseAddr, offsetInDataInstance).Insert(builder).Return()
824
825 memBase := c.getMemoryBaseValue(false)
826 dstAddr := builder.AllocateInstruction().AsIadd(memBase, offsetInMemory).Insert(builder).Return()
827
828 c.callMemmove(dstAddr, srcAddr, copySize)
829
830 case wasm.OpcodeMiscTableInit:
831 elemIndex := c.readI32u()
832 tableIndex := c.readI32u()
833 if state.unreachable {
834 break
835 }
836
837 copySize := builder.
838 AllocateInstruction().AsUExtend(state.pop(), 32, 64).Insert(builder).Return()
839 offsetInElementInstance := builder.
840 AllocateInstruction().AsUExtend(state.pop(), 32, 64).Insert(builder).Return()
841 offsetInTable := builder.
842 AllocateInstruction().AsUExtend(state.pop(), 32, 64).Insert(builder).Return()
843
844 elemInstPtr := c.dataOrElementInstanceAddr(elemIndex, c.offset.ElementInstances1stElement)
845
846 // Bounds check.
847 tableInstancePtr := c.boundsCheckInTable(tableIndex, offsetInTable, copySize)
848 c.boundsCheckInDataOrElementInstance(elemInstPtr, offsetInElementInstance, copySize, wazevoapi.ExitCodeTableOutOfBounds)
849
850 three := builder.AllocateInstruction().AsIconst64(3).Insert(builder).Return()
851 // Calculates the destination address in the table.
852 tableOffsetInBytes := builder.AllocateInstruction().AsIshl(offsetInTable, three).Insert(builder).Return()
853 tableBaseAddr := c.loadTableBaseAddr(tableInstancePtr)
854 dstAddr := builder.AllocateInstruction().AsIadd(tableBaseAddr, tableOffsetInBytes).Insert(builder).Return()
855
856 // Calculates the source address in the element instance.
857 srcOffsetInBytes := builder.AllocateInstruction().AsIshl(offsetInElementInstance, three).Insert(builder).Return()
858 elemInstBaseAddr := builder.AllocateInstruction().AsLoad(elemInstPtr, 0, ssa.TypeI64).Insert(builder).Return()
859 srcAddr := builder.AllocateInstruction().AsIadd(elemInstBaseAddr, srcOffsetInBytes).Insert(builder).Return()
860
861 copySizeInBytes := builder.AllocateInstruction().AsIshl(copySize, three).Insert(builder).Return()
862 c.callMemmove(dstAddr, srcAddr, copySizeInBytes)
863
864 case wasm.OpcodeMiscElemDrop:
865 index := c.readI32u()
866 if state.unreachable {
867 break
868 }
869
870 c.dropDataOrElementInstance(index, c.offset.ElementInstances1stElement)
871
872 case wasm.OpcodeMiscDataDrop:
873 index := c.readI32u()
874 if state.unreachable {
875 break
876 }
877 c.dropDataOrElementInstance(index, c.offset.DataInstances1stElement)
878
879 default:
880 panic("Unknown MiscOp " + wasm.MiscInstructionName(miscOp))
881 }
882
883 case wasm.OpcodeI32ReinterpretF32:
884 if state.unreachable {
885 break
886 }
887 reinterpret := builder.AllocateInstruction().
888 AsBitcast(state.pop(), ssa.TypeI32).
889 Insert(builder).Return()
890 state.push(reinterpret)
891
892 case wasm.OpcodeI64ReinterpretF64:
893 if state.unreachable {
894 break
895 }
896 reinterpret := builder.AllocateInstruction().
897 AsBitcast(state.pop(), ssa.TypeI64).
898 Insert(builder).Return()
899 state.push(reinterpret)
900
901 case wasm.OpcodeF32ReinterpretI32:
902 if state.unreachable {
903 break
904 }
905 reinterpret := builder.AllocateInstruction().
906 AsBitcast(state.pop(), ssa.TypeF32).
907 Insert(builder).Return()
908 state.push(reinterpret)
909
910 case wasm.OpcodeF64ReinterpretI64:
911 if state.unreachable {
912 break
913 }
914 reinterpret := builder.AllocateInstruction().
915 AsBitcast(state.pop(), ssa.TypeF64).
916 Insert(builder).Return()
917 state.push(reinterpret)
918
919 case wasm.OpcodeI32DivS, wasm.OpcodeI64DivS:
920 if state.unreachable {
921 break
922 }
923 y, x := state.pop(), state.pop()
924 result := builder.AllocateInstruction().AsSDiv(x, y, c.execCtxPtrValue).Insert(builder).Return()
925 state.push(result)
926
927 case wasm.OpcodeI32DivU, wasm.OpcodeI64DivU:
928 if state.unreachable {
929 break
930 }
931 y, x := state.pop(), state.pop()
932 result := builder.AllocateInstruction().AsUDiv(x, y, c.execCtxPtrValue).Insert(builder).Return()
933 state.push(result)
934
935 case wasm.OpcodeI32RemS, wasm.OpcodeI64RemS:
936 if state.unreachable {
937 break
938 }
939 y, x := state.pop(), state.pop()
940 result := builder.AllocateInstruction().AsSRem(x, y, c.execCtxPtrValue).Insert(builder).Return()
941 state.push(result)
942
943 case wasm.OpcodeI32RemU, wasm.OpcodeI64RemU:
944 if state.unreachable {
945 break
946 }
947 y, x := state.pop(), state.pop()
948 result := builder.AllocateInstruction().AsURem(x, y, c.execCtxPtrValue).Insert(builder).Return()
949 state.push(result)
950
951 case wasm.OpcodeI32And, wasm.OpcodeI64And:
952 if state.unreachable {
953 break
954 }
955 y, x := state.pop(), state.pop()
956 and := builder.AllocateInstruction()
957 and.AsBand(x, y)
958 builder.InsertInstruction(and)
959 value := and.Return()
960 state.push(value)
961 case wasm.OpcodeI32Or, wasm.OpcodeI64Or:
962 if state.unreachable {
963 break
964 }
965 y, x := state.pop(), state.pop()
966 or := builder.AllocateInstruction()
967 or.AsBor(x, y)
968 builder.InsertInstruction(or)
969 value := or.Return()
970 state.push(value)
971 case wasm.OpcodeI32Xor, wasm.OpcodeI64Xor:
972 if state.unreachable {
973 break
974 }
975 y, x := state.pop(), state.pop()
976 xor := builder.AllocateInstruction()
977 xor.AsBxor(x, y)
978 builder.InsertInstruction(xor)
979 value := xor.Return()
980 state.push(value)
981 case wasm.OpcodeI32Shl, wasm.OpcodeI64Shl:
982 if state.unreachable {
983 break
984 }
985 y, x := state.pop(), state.pop()
986 ishl := builder.AllocateInstruction()
987 ishl.AsIshl(x, y)
988 builder.InsertInstruction(ishl)
989 value := ishl.Return()
990 state.push(value)
991 case wasm.OpcodeI32ShrU, wasm.OpcodeI64ShrU:
992 if state.unreachable {
993 break
994 }
995 y, x := state.pop(), state.pop()
996 ishl := builder.AllocateInstruction()
997 ishl.AsUshr(x, y)
998 builder.InsertInstruction(ishl)
999 value := ishl.Return()
1000 state.push(value)
1001 case wasm.OpcodeI32ShrS, wasm.OpcodeI64ShrS:
1002 if state.unreachable {
1003 break
1004 }
1005 y, x := state.pop(), state.pop()
1006 ishl := builder.AllocateInstruction()
1007 ishl.AsSshr(x, y)
1008 builder.InsertInstruction(ishl)
1009 value := ishl.Return()
1010 state.push(value)
1011 case wasm.OpcodeI32Rotl, wasm.OpcodeI64Rotl:
1012 if state.unreachable {
1013 break
1014 }
1015 y, x := state.pop(), state.pop()
1016 rotl := builder.AllocateInstruction()
1017 rotl.AsRotl(x, y)
1018 builder.InsertInstruction(rotl)
1019 value := rotl.Return()
1020 state.push(value)
1021 case wasm.OpcodeI32Rotr, wasm.OpcodeI64Rotr:
1022 if state.unreachable {
1023 break
1024 }
1025 y, x := state.pop(), state.pop()
1026 rotr := builder.AllocateInstruction()
1027 rotr.AsRotr(x, y)
1028 builder.InsertInstruction(rotr)
1029 value := rotr.Return()
1030 state.push(value)
1031 case wasm.OpcodeI32Clz, wasm.OpcodeI64Clz:
1032 if state.unreachable {
1033 break
1034 }
1035 x := state.pop()
1036 clz := builder.AllocateInstruction()
1037 clz.AsClz(x)
1038 builder.InsertInstruction(clz)
1039 value := clz.Return()
1040 state.push(value)
1041 case wasm.OpcodeI32Ctz, wasm.OpcodeI64Ctz:
1042 if state.unreachable {
1043 break
1044 }
1045 x := state.pop()
1046 ctz := builder.AllocateInstruction()
1047 ctz.AsCtz(x)
1048 builder.InsertInstruction(ctz)
1049 value := ctz.Return()
1050 state.push(value)
1051 case wasm.OpcodeI32Popcnt, wasm.OpcodeI64Popcnt:
1052 if state.unreachable {
1053 break
1054 }
1055 x := state.pop()
1056 popcnt := builder.AllocateInstruction()
1057 popcnt.AsPopcnt(x)
1058 builder.InsertInstruction(popcnt)
1059 value := popcnt.Return()
1060 state.push(value)
1061
1062 case wasm.OpcodeI32WrapI64:
1063 if state.unreachable {
1064 break
1065 }
1066 x := state.pop()
1067 wrap := builder.AllocateInstruction().AsIreduce(x, ssa.TypeI32).Insert(builder).Return()
1068 state.push(wrap)
1069 case wasm.OpcodeGlobalGet:
1070 index := c.readI32u()
1071 if state.unreachable {
1072 break
1073 }
1074 v := c.getWasmGlobalValue(index, false)
1075 state.push(v)
1076 case wasm.OpcodeGlobalSet:
1077 index := c.readI32u()
1078 if state.unreachable {
1079 break
1080 }
1081 v := state.pop()
1082 c.setWasmGlobalValue(index, v)
1083 case wasm.OpcodeLocalGet:
1084 index := c.readI32u()
1085 if state.unreachable {
1086 break
1087 }
1088 variable := c.localVariable(index)
1089 state.push(builder.MustFindValue(variable))
1090
1091 case wasm.OpcodeLocalSet:
1092 index := c.readI32u()
1093 if state.unreachable {
1094 break
1095 }
1096 variable := c.localVariable(index)
1097 newValue := state.pop()
1098 builder.DefineVariableInCurrentBB(variable, newValue)
1099
1100 case wasm.OpcodeLocalTee:
1101 index := c.readI32u()
1102 if state.unreachable {
1103 break
1104 }
1105 variable := c.localVariable(index)
1106 newValue := state.peek()
1107 builder.DefineVariableInCurrentBB(variable, newValue)
1108
1109 case wasm.OpcodeSelect, wasm.OpcodeTypedSelect:
1110 if op == wasm.OpcodeTypedSelect {
1111 state.pc += 2 // ignores the type which is only needed during validation.
1112 }
1113
1114 if state.unreachable {
1115 break
1116 }
1117
1118 cond := state.pop()
1119 v2 := state.pop()
1120 v1 := state.pop()
1121
1122 sl := builder.AllocateInstruction().
1123 AsSelect(cond, v1, v2).
1124 Insert(builder).
1125 Return()
1126 state.push(sl)
1127
1128 case wasm.OpcodeMemorySize:
1129 state.pc++ // skips the memory index.
1130 if state.unreachable {
1131 break
1132 }
1133
1134 var memSizeInBytes ssa.Value
1135 if c.offset.LocalMemoryBegin < 0 {
1136 memInstPtr := builder.AllocateInstruction().
1137 AsLoad(c.moduleCtxPtrValue, c.offset.ImportedMemoryBegin.U32(), ssa.TypeI64).
1138 Insert(builder).
1139 Return()
1140
1141 memSizeInBytes = builder.AllocateInstruction().
1142 AsLoad(memInstPtr, memoryInstanceBufSizeOffset, ssa.TypeI32).
1143 Insert(builder).
1144 Return()
1145 } else {
1146 memSizeInBytes = builder.AllocateInstruction().
1147 AsLoad(c.moduleCtxPtrValue, c.offset.LocalMemoryLen().U32(), ssa.TypeI32).
1148 Insert(builder).
1149 Return()
1150 }
1151
1152 amount := builder.AllocateInstruction()
1153 amount.AsIconst32(uint32(wasm.MemoryPageSizeInBits))
1154 builder.InsertInstruction(amount)
1155 memSize := builder.AllocateInstruction().
1156 AsUshr(memSizeInBytes, amount.Return()).
1157 Insert(builder).
1158 Return()
1159 state.push(memSize)
1160
1161 case wasm.OpcodeMemoryGrow:
1162 state.pc++ // skips the memory index.
1163 if state.unreachable {
1164 break
1165 }
1166
1167 c.storeCallerModuleContext()
1168
1169 pages := state.pop()
1170 memoryGrowPtr := builder.AllocateInstruction().
1171 AsLoad(c.execCtxPtrValue,
1172 wazevoapi.ExecutionContextOffsetMemoryGrowTrampolineAddress.U32(),
1173 ssa.TypeI64,
1174 ).Insert(builder).Return()
1175
1176 args := c.allocateVarLengthValues(2, c.execCtxPtrValue, pages)
1177 callGrowRet := builder.
1178 AllocateInstruction().
1179 AsCallIndirect(memoryGrowPtr, &c.memoryGrowSig, args).
1180 Insert(builder).Return()
1181 state.push(callGrowRet)
1182
1183 // After the memory grow, reload the cached memory base and len.
1184 c.reloadMemoryBaseLen()
1185
1186 case wasm.OpcodeI32Store,
1187 wasm.OpcodeI64Store,
1188 wasm.OpcodeF32Store,
1189 wasm.OpcodeF64Store,
1190 wasm.OpcodeI32Store8,
1191 wasm.OpcodeI32Store16,
1192 wasm.OpcodeI64Store8,
1193 wasm.OpcodeI64Store16,
1194 wasm.OpcodeI64Store32:
1195
1196 _, offset := c.readMemArg()
1197 if state.unreachable {
1198 break
1199 }
1200 var opSize uint64
1201 var opcode ssa.Opcode
1202 switch op {
1203 case wasm.OpcodeI32Store, wasm.OpcodeF32Store:
1204 opcode = ssa.OpcodeStore
1205 opSize = 4
1206 case wasm.OpcodeI64Store, wasm.OpcodeF64Store:
1207 opcode = ssa.OpcodeStore
1208 opSize = 8
1209 case wasm.OpcodeI32Store8, wasm.OpcodeI64Store8:
1210 opcode = ssa.OpcodeIstore8
1211 opSize = 1
1212 case wasm.OpcodeI32Store16, wasm.OpcodeI64Store16:
1213 opcode = ssa.OpcodeIstore16
1214 opSize = 2
1215 case wasm.OpcodeI64Store32:
1216 opcode = ssa.OpcodeIstore32
1217 opSize = 4
1218 default:
1219 panic("BUG")
1220 }
1221
1222 value := state.pop()
1223 baseAddr := state.pop()
1224 addr := c.memOpSetup(baseAddr, uint64(offset), opSize)
1225 builder.AllocateInstruction().
1226 AsStore(opcode, value, addr, offset).
1227 Insert(builder)
1228
1229 case wasm.OpcodeI32Load,
1230 wasm.OpcodeI64Load,
1231 wasm.OpcodeF32Load,
1232 wasm.OpcodeF64Load,
1233 wasm.OpcodeI32Load8S,
1234 wasm.OpcodeI32Load8U,
1235 wasm.OpcodeI32Load16S,
1236 wasm.OpcodeI32Load16U,
1237 wasm.OpcodeI64Load8S,
1238 wasm.OpcodeI64Load8U,
1239 wasm.OpcodeI64Load16S,
1240 wasm.OpcodeI64Load16U,
1241 wasm.OpcodeI64Load32S,
1242 wasm.OpcodeI64Load32U:
1243 _, offset := c.readMemArg()
1244 if state.unreachable {
1245 break
1246 }
1247
1248 var opSize uint64
1249 switch op {
1250 case wasm.OpcodeI32Load, wasm.OpcodeF32Load:
1251 opSize = 4
1252 case wasm.OpcodeI64Load, wasm.OpcodeF64Load:
1253 opSize = 8
1254 case wasm.OpcodeI32Load8S, wasm.OpcodeI32Load8U:
1255 opSize = 1
1256 case wasm.OpcodeI32Load16S, wasm.OpcodeI32Load16U:
1257 opSize = 2
1258 case wasm.OpcodeI64Load8S, wasm.OpcodeI64Load8U:
1259 opSize = 1
1260 case wasm.OpcodeI64Load16S, wasm.OpcodeI64Load16U:
1261 opSize = 2
1262 case wasm.OpcodeI64Load32S, wasm.OpcodeI64Load32U:
1263 opSize = 4
1264 default:
1265 panic("BUG")
1266 }
1267
1268 baseAddr := state.pop()
1269 addr := c.memOpSetup(baseAddr, uint64(offset), opSize)
1270 load := builder.AllocateInstruction()
1271 switch op {
1272 case wasm.OpcodeI32Load:
1273 load.AsLoad(addr, offset, ssa.TypeI32)
1274 case wasm.OpcodeI64Load:
1275 load.AsLoad(addr, offset, ssa.TypeI64)
1276 case wasm.OpcodeF32Load:
1277 load.AsLoad(addr, offset, ssa.TypeF32)
1278 case wasm.OpcodeF64Load:
1279 load.AsLoad(addr, offset, ssa.TypeF64)
1280 case wasm.OpcodeI32Load8S:
1281 load.AsExtLoad(ssa.OpcodeSload8, addr, offset, false)
1282 case wasm.OpcodeI32Load8U:
1283 load.AsExtLoad(ssa.OpcodeUload8, addr, offset, false)
1284 case wasm.OpcodeI32Load16S:
1285 load.AsExtLoad(ssa.OpcodeSload16, addr, offset, false)
1286 case wasm.OpcodeI32Load16U:
1287 load.AsExtLoad(ssa.OpcodeUload16, addr, offset, false)
1288 case wasm.OpcodeI64Load8S:
1289 load.AsExtLoad(ssa.OpcodeSload8, addr, offset, true)
1290 case wasm.OpcodeI64Load8U:
1291 load.AsExtLoad(ssa.OpcodeUload8, addr, offset, true)
1292 case wasm.OpcodeI64Load16S:
1293 load.AsExtLoad(ssa.OpcodeSload16, addr, offset, true)
1294 case wasm.OpcodeI64Load16U:
1295 load.AsExtLoad(ssa.OpcodeUload16, addr, offset, true)
1296 case wasm.OpcodeI64Load32S:
1297 load.AsExtLoad(ssa.OpcodeSload32, addr, offset, true)
1298 case wasm.OpcodeI64Load32U:
1299 load.AsExtLoad(ssa.OpcodeUload32, addr, offset, true)
1300 default:
1301 panic("BUG")
1302 }
1303 builder.InsertInstruction(load)
1304 state.push(load.Return())
1305 case wasm.OpcodeBlock:
1306 // Note: we do not need to create a BB for this as that would always have only one predecessor
1307 // which is the current BB, and therefore it's always ok to merge them in any way.
1308
1309 bt := c.readBlockType()
1310
1311 if state.unreachable {
1312 state.unreachableDepth++
1313 break
1314 }
1315
1316 followingBlk := builder.AllocateBasicBlock()
1317 c.addBlockParamsFromWasmTypes(bt.Results, followingBlk)
1318
1319 state.ctrlPush(controlFrame{
1320 kind: controlFrameKindBlock,
1321 originalStackLenWithoutParam: len(state.values) - len(bt.Params),
1322 followingBlock: followingBlk,
1323 blockType: bt,
1324 })
1325 case wasm.OpcodeLoop:
1326 bt := c.readBlockType()
1327
1328 if state.unreachable {
1329 state.unreachableDepth++
1330 break
1331 }
1332
1333 loopHeader, afterLoopBlock := builder.AllocateBasicBlock(), builder.AllocateBasicBlock()
1334 c.addBlockParamsFromWasmTypes(bt.Params, loopHeader)
1335 c.addBlockParamsFromWasmTypes(bt.Results, afterLoopBlock)
1336
1337 originalLen := len(state.values) - len(bt.Params)
1338 state.ctrlPush(controlFrame{
1339 originalStackLenWithoutParam: originalLen,
1340 kind: controlFrameKindLoop,
1341 blk: loopHeader,
1342 followingBlock: afterLoopBlock,
1343 blockType: bt,
1344 })
1345
1346 args := c.allocateVarLengthValues(len(bt.Params), state.values[originalLen:]...)
1347
1348 // Insert the jump to the header of loop.
1349 br := builder.AllocateInstruction()
1350 br.AsJump(args, loopHeader)
1351 builder.InsertInstruction(br)
1352
1353 c.switchTo(originalLen, loopHeader)
1354
1355 if c.ensureTermination {
1356 checkModuleExitCodePtr := builder.AllocateInstruction().
1357 AsLoad(c.execCtxPtrValue,
1358 wazevoapi.ExecutionContextOffsetCheckModuleExitCodeTrampolineAddress.U32(),
1359 ssa.TypeI64,
1360 ).Insert(builder).Return()
1361
1362 args := c.allocateVarLengthValues(1, c.execCtxPtrValue)
1363 builder.AllocateInstruction().
1364 AsCallIndirect(checkModuleExitCodePtr, &c.checkModuleExitCodeSig, args).
1365 Insert(builder)
1366 }
1367 case wasm.OpcodeIf:
1368 bt := c.readBlockType()
1369
1370 if state.unreachable {
1371 state.unreachableDepth++
1372 break
1373 }
1374
1375 v := state.pop()
1376 thenBlk, elseBlk, followingBlk := builder.AllocateBasicBlock(), builder.AllocateBasicBlock(), builder.AllocateBasicBlock()
1377
1378 // We do not make the Wasm-level block parameters as SSA-level block params for if-else blocks
1379 // since they won't be PHI and the definition is unique.
1380
1381 // On the other hand, the following block after if-else-end will likely have
1382 // multiple definitions (one in Then and another in Else blocks).
1383 c.addBlockParamsFromWasmTypes(bt.Results, followingBlk)
1384
1385 args := c.allocateVarLengthValues(len(bt.Params), state.values[len(state.values)-len(bt.Params):]...)
1386
1387 // Insert the conditional jump to the Else block.
1388 brz := builder.AllocateInstruction()
1389 brz.AsBrz(v, ssa.ValuesNil, elseBlk)
1390 builder.InsertInstruction(brz)
1391
1392 // Then, insert the jump to the Then block.
1393 br := builder.AllocateInstruction()
1394 br.AsJump(ssa.ValuesNil, thenBlk)
1395 builder.InsertInstruction(br)
1396
1397 state.ctrlPush(controlFrame{
1398 kind: controlFrameKindIfWithoutElse,
1399 originalStackLenWithoutParam: len(state.values) - len(bt.Params),
1400 blk: elseBlk,
1401 followingBlock: followingBlk,
1402 blockType: bt,
1403 clonedArgs: args,
1404 })
1405
1406 builder.SetCurrentBlock(thenBlk)
1407
1408 // Then and Else (if exists) have only one predecessor.
1409 builder.Seal(thenBlk)
1410 builder.Seal(elseBlk)
1411 case wasm.OpcodeElse:
1412 ifctrl := state.ctrlPeekAt(0)
1413 if unreachable := state.unreachable; unreachable && state.unreachableDepth > 0 {
1414 // If it is currently in unreachable and is a nested if,
1415 // we just remove the entire else block.
1416 break
1417 }
1418
1419 ifctrl.kind = controlFrameKindIfWithElse
1420 if !state.unreachable {
1421 // If this Then block is currently reachable, we have to insert the branching to the following BB.
1422 followingBlk := ifctrl.followingBlock // == the BB after if-then-else.
1423 args := c.nPeekDup(len(ifctrl.blockType.Results))
1424 c.insertJumpToBlock(args, followingBlk)
1425 } else {
1426 state.unreachable = false
1427 }
1428
1429 // Reset the stack so that we can correctly handle the else block.
1430 state.values = state.values[:ifctrl.originalStackLenWithoutParam]
1431 elseBlk := ifctrl.blk
1432 for _, arg := range ifctrl.clonedArgs.View() {
1433 state.push(arg)
1434 }
1435
1436 builder.SetCurrentBlock(elseBlk)
1437
1438 case wasm.OpcodeEnd:
1439 if state.unreachableDepth > 0 {
1440 state.unreachableDepth--
1441 break
1442 }
1443
1444 ctrl := state.ctrlPop()
1445 followingBlk := ctrl.followingBlock
1446
1447 unreachable := state.unreachable
1448 if !unreachable {
1449 // For try_table, emit the leave trampoline before the jump to the following block.
1450 if ctrl.kind == controlFrameKindTryTable {
1451 c.emitTryTableLeave()
1452 }
1453
1454 // Top n-th args will be used as a result of the current control frame.
1455 args := c.nPeekDup(len(ctrl.blockType.Results))
1456
1457 // Insert the unconditional branch to the target.
1458 c.insertJumpToBlock(args, followingBlk)
1459 } else { // recover from the unreachable state.
1460 state.unreachable = false
1461 }
1462
1463 switch ctrl.kind {
1464 case controlFrameKindFunction:
1465 break // This is the very end of function.
1466 case controlFrameKindLoop:
1467 // Loop header block can be reached from any br/br_table contained in the loop,
1468 // so now that we've reached End of it, we can seal it.
1469 builder.Seal(ctrl.blk)
1470 case controlFrameKindIfWithoutElse:
1471 // If this is the end of Then block, we have to emit the empty Else block.
1472 elseBlk := ctrl.blk
1473 builder.SetCurrentBlock(elseBlk)
1474 c.insertJumpToBlock(ctrl.clonedArgs, followingBlk)
1475 }
1476
1477 builder.Seal(followingBlk)
1478
1479 // Ready to start translating the following block.
1480 c.switchTo(ctrl.originalStackLenWithoutParam, followingBlk)
1481
1482 case wasm.OpcodeBr:
1483 labelIndex := c.readI32u()
1484 if state.unreachable {
1485 break
1486 }
1487
1488 c.emitTryTableLeaves(int(labelIndex))
1489 targetBlk, argNum := state.brTargetArgNumFor(labelIndex)
1490 args := c.nPeekDup(argNum)
1491 c.insertJumpToBlock(args, targetBlk)
1492
1493 state.unreachable = true
1494
1495 case wasm.OpcodeBrIf:
1496 labelIndex := c.readI32u()
1497 if state.unreachable {
1498 break
1499 }
1500
1501 v := state.pop()
1502
1503 targetBlk, argNum := state.brTargetArgNumFor(labelIndex)
1504 args := c.nPeekDup(argNum)
1505 var sealTargetBlk bool
1506
1507 // If the branch exits any try_table frames, emit TryTableLeave
1508 // calls in a trampoline block that only runs on the taken path.
1509 if c.branchExitsTryTable(int(labelIndex)) {
1510 current := builder.CurrentBlock()
1511 trampolineBlk := builder.AllocateBasicBlock()
1512 builder.SetCurrentBlock(trampolineBlk)
1513 c.emitTryTableLeaves(int(labelIndex))
1514 c.insertJumpToBlock(args, targetBlk)
1515 builder.SetCurrentBlock(current)
1516 targetBlk = trampolineBlk
1517 sealTargetBlk = true
1518 args = ssa.ValuesNil
1519 }
1520
1521 if c.needListener && targetBlk.ReturnBlock() { // In this case, we have to call the listener before returning.
1522 // Save the currently active block.
1523 current := builder.CurrentBlock()
1524
1525 // Allocate the trampoline block to the return where we call the listener.
1526 targetBlk = builder.AllocateBasicBlock()
1527 builder.SetCurrentBlock(targetBlk)
1528 sealTargetBlk = true
1529
1530 c.callListenerAfter()
1531
1532 instr := builder.AllocateInstruction()
1533 instr.AsReturn(args)
1534 builder.InsertInstruction(instr)
1535
1536 args = ssa.ValuesNil
1537
1538 // Revert the current block.
1539 builder.SetCurrentBlock(current)
1540 }
1541
1542 // Insert the conditional jump to the target block.
1543 brnz := builder.AllocateInstruction()
1544 brnz.AsBrnz(v, args, targetBlk)
1545 builder.InsertInstruction(brnz)
1546
1547 if sealTargetBlk {
1548 builder.Seal(targetBlk)
1549 }
1550
1551 // Insert the unconditional jump to the Else block which corresponds to after br_if.
1552 elseBlk := builder.AllocateBasicBlock()
1553 c.insertJumpToBlock(ssa.ValuesNil, elseBlk)
1554
1555 // Now start translating the instructions after br_if.
1556 builder.Seal(elseBlk) // Else of br_if has the current block as the only one successor.
1557 builder.SetCurrentBlock(elseBlk)
1558
1559 case wasm.OpcodeBrTable:
1560 labels := state.tmpForBrTable[:0]
1561 labelCount := c.readI32u()
1562 for i := 0; i < int(labelCount); i++ {
1563 labels = append(labels, c.readI32u())
1564 }
1565 labels = append(labels, c.readI32u()) // default label.
1566 if state.unreachable {
1567 break
1568 }
1569
1570 index := state.pop()
1571 if labelCount == 0 { // If this br_table is empty, we can just emit the unconditional jump.
1572 targetBlk, argNum := state.brTargetArgNumFor(labels[0])
1573 args := c.nPeekDup(argNum)
1574 c.insertJumpToBlock(args, targetBlk)
1575 } else {
1576 c.lowerBrTable(labels, index)
1577 }
1578 state.tmpForBrTable = labels // reuse the temporary slice for next use.
1579 state.unreachable = true
1580
1581 case wasm.OpcodeNop:
1582 case wasm.OpcodeReturn:
1583 if state.unreachable {
1584 break
1585 }
1586 c.emitTryTableLeaves(len(state.controlFrames))
1587 if c.needListener {
1588 c.callListenerAfter()
1589 }
1590
1591 c.lowerReturn(builder)
1592 state.unreachable = true
1593
1594 case wasm.OpcodeUnreachable:
1595 if state.unreachable {
1596 break
1597 }
1598 exit := builder.AllocateInstruction()
1599 exit.AsExitWithCode(c.execCtxPtrValue, wazevoapi.ExitCodeUnreachable)
1600 builder.InsertInstruction(exit)
1601 state.unreachable = true
1602
1603 case wasm.OpcodeCallIndirect:
1604 typeIndex := c.readI32u()
1605 tableIndex := c.readI32u()
1606 if state.unreachable {
1607 break
1608 }
1609 c.lowerCallIndirect(typeIndex, tableIndex)
1610
1611 case wasm.OpcodeCall:
1612 fnIndex := c.readI32u()
1613 if state.unreachable {
1614 break
1615 }
1616 c.lowerCall(fnIndex)
1617
1618 case wasm.OpcodeDrop:
1619 if state.unreachable {
1620 break
1621 }
1622 _ = state.pop()
1623 case wasm.OpcodeF64ConvertI32S, wasm.OpcodeF64ConvertI64S, wasm.OpcodeF64ConvertI32U, wasm.OpcodeF64ConvertI64U:
1624 if state.unreachable {
1625 break
1626 }
1627 result := builder.AllocateInstruction().AsFcvtFromInt(
1628 state.pop(),
1629 op == wasm.OpcodeF64ConvertI32S || op == wasm.OpcodeF64ConvertI64S,
1630 true,
1631 ).Insert(builder).Return()
1632 state.push(result)
1633 case wasm.OpcodeF32ConvertI32S, wasm.OpcodeF32ConvertI64S, wasm.OpcodeF32ConvertI32U, wasm.OpcodeF32ConvertI64U:
1634 if state.unreachable {
1635 break
1636 }
1637 result := builder.AllocateInstruction().AsFcvtFromInt(
1638 state.pop(),
1639 op == wasm.OpcodeF32ConvertI32S || op == wasm.OpcodeF32ConvertI64S,
1640 false,
1641 ).Insert(builder).Return()
1642 state.push(result)
1643 case wasm.OpcodeF32DemoteF64:
1644 if state.unreachable {
1645 break
1646 }
1647 cvt := builder.AllocateInstruction()
1648 cvt.AsFdemote(state.pop())
1649 builder.InsertInstruction(cvt)
1650 state.push(cvt.Return())
1651 case wasm.OpcodeF64PromoteF32:
1652 if state.unreachable {
1653 break
1654 }
1655 cvt := builder.AllocateInstruction()
1656 cvt.AsFpromote(state.pop())
1657 builder.InsertInstruction(cvt)
1658 state.push(cvt.Return())
1659
1660 case wasm.OpcodeVecPrefix:
1661 state.pc++
1662 vecOp := c.wasmFunctionBody[state.pc]
1663 switch vecOp {
1664 case wasm.OpcodeVecV128Const:
1665 state.pc++
1666 lo := binary.LittleEndian.Uint64(c.wasmFunctionBody[state.pc:])
1667 state.pc += 8
1668 hi := binary.LittleEndian.Uint64(c.wasmFunctionBody[state.pc:])
1669 state.pc += 7
1670 if state.unreachable {
1671 break
1672 }
1673 ret := builder.AllocateInstruction().AsVconst(lo, hi).Insert(builder).Return()
1674 state.push(ret)
1675 case wasm.OpcodeVecV128Load:
1676 _, offset := c.readMemArg()
1677 if state.unreachable {
1678 break
1679 }
1680 baseAddr := state.pop()
1681 addr := c.memOpSetup(baseAddr, uint64(offset), 16)
1682 load := builder.AllocateInstruction()
1683 load.AsLoad(addr, offset, ssa.TypeV128)
1684 builder.InsertInstruction(load)
1685 state.push(load.Return())
1686 case wasm.OpcodeVecV128Load8Lane, wasm.OpcodeVecV128Load16Lane, wasm.OpcodeVecV128Load32Lane:
1687 _, offset := c.readMemArg()
1688 state.pc++
1689 if state.unreachable {
1690 break
1691 }
1692 var lane ssa.VecLane
1693 var loadOp ssa.Opcode
1694 var opSize uint64
1695 switch vecOp {
1696 case wasm.OpcodeVecV128Load8Lane:
1697 loadOp, lane, opSize = ssa.OpcodeUload8, ssa.VecLaneI8x16, 1
1698 case wasm.OpcodeVecV128Load16Lane:
1699 loadOp, lane, opSize = ssa.OpcodeUload16, ssa.VecLaneI16x8, 2
1700 case wasm.OpcodeVecV128Load32Lane:
1701 loadOp, lane, opSize = ssa.OpcodeUload32, ssa.VecLaneI32x4, 4
1702 }
1703 laneIndex := c.wasmFunctionBody[state.pc]
1704 vector := state.pop()
1705 baseAddr := state.pop()
1706 addr := c.memOpSetup(baseAddr, uint64(offset), opSize)
1707 load := builder.AllocateInstruction().
1708 AsExtLoad(loadOp, addr, offset, false).
1709 Insert(builder).Return()
1710 ret := builder.AllocateInstruction().
1711 AsInsertlane(vector, load, laneIndex, lane).
1712 Insert(builder).Return()
1713 state.push(ret)
1714 case wasm.OpcodeVecV128Load64Lane:
1715 _, offset := c.readMemArg()
1716 state.pc++
1717 if state.unreachable {
1718 break
1719 }
1720 laneIndex := c.wasmFunctionBody[state.pc]
1721 vector := state.pop()
1722 baseAddr := state.pop()
1723 addr := c.memOpSetup(baseAddr, uint64(offset), 8)
1724 load := builder.AllocateInstruction().
1725 AsLoad(addr, offset, ssa.TypeI64).
1726 Insert(builder).Return()
1727 ret := builder.AllocateInstruction().
1728 AsInsertlane(vector, load, laneIndex, ssa.VecLaneI64x2).
1729 Insert(builder).Return()
1730 state.push(ret)
1731
1732 case wasm.OpcodeVecV128Load32zero, wasm.OpcodeVecV128Load64zero:
1733 _, offset := c.readMemArg()
1734 if state.unreachable {
1735 break
1736 }
1737
1738 var scalarType ssa.Type
1739 switch vecOp {
1740 case wasm.OpcodeVecV128Load32zero:
1741 scalarType = ssa.TypeF32
1742 case wasm.OpcodeVecV128Load64zero:
1743 scalarType = ssa.TypeF64
1744 }
1745
1746 baseAddr := state.pop()
1747 addr := c.memOpSetup(baseAddr, uint64(offset), uint64(scalarType.Size()))
1748
1749 ret := builder.AllocateInstruction().
1750 AsVZeroExtLoad(addr, offset, scalarType).
1751 Insert(builder).Return()
1752 state.push(ret)
1753
1754 case wasm.OpcodeVecV128Load8x8u, wasm.OpcodeVecV128Load8x8s,
1755 wasm.OpcodeVecV128Load16x4u, wasm.OpcodeVecV128Load16x4s,
1756 wasm.OpcodeVecV128Load32x2u, wasm.OpcodeVecV128Load32x2s:
1757 _, offset := c.readMemArg()
1758 if state.unreachable {
1759 break
1760 }
1761 var lane ssa.VecLane
1762 var signed bool
1763 switch vecOp {
1764 case wasm.OpcodeVecV128Load8x8s:
1765 signed = true
1766 fallthrough
1767 case wasm.OpcodeVecV128Load8x8u:
1768 lane = ssa.VecLaneI8x16
1769 case wasm.OpcodeVecV128Load16x4s:
1770 signed = true
1771 fallthrough
1772 case wasm.OpcodeVecV128Load16x4u:
1773 lane = ssa.VecLaneI16x8
1774 case wasm.OpcodeVecV128Load32x2s:
1775 signed = true
1776 fallthrough
1777 case wasm.OpcodeVecV128Load32x2u:
1778 lane = ssa.VecLaneI32x4
1779 }
1780 baseAddr := state.pop()
1781 addr := c.memOpSetup(baseAddr, uint64(offset), 8)
1782 load := builder.AllocateInstruction().
1783 AsLoad(addr, offset, ssa.TypeF64).
1784 Insert(builder).Return()
1785 ret := builder.AllocateInstruction().
1786 AsWiden(load, lane, signed, true).
1787 Insert(builder).Return()
1788 state.push(ret)
1789 case wasm.OpcodeVecV128Load8Splat, wasm.OpcodeVecV128Load16Splat,
1790 wasm.OpcodeVecV128Load32Splat, wasm.OpcodeVecV128Load64Splat:
1791 _, offset := c.readMemArg()
1792 if state.unreachable {
1793 break
1794 }
1795 var lane ssa.VecLane
1796 var opSize uint64
1797 switch vecOp {
1798 case wasm.OpcodeVecV128Load8Splat:
1799 lane, opSize = ssa.VecLaneI8x16, 1
1800 case wasm.OpcodeVecV128Load16Splat:
1801 lane, opSize = ssa.VecLaneI16x8, 2
1802 case wasm.OpcodeVecV128Load32Splat:
1803 lane, opSize = ssa.VecLaneI32x4, 4
1804 case wasm.OpcodeVecV128Load64Splat:
1805 lane, opSize = ssa.VecLaneI64x2, 8
1806 }
1807 baseAddr := state.pop()
1808 addr := c.memOpSetup(baseAddr, uint64(offset), opSize)
1809 ret := builder.AllocateInstruction().
1810 AsLoadSplat(addr, offset, lane).
1811 Insert(builder).Return()
1812 state.push(ret)
1813 case wasm.OpcodeVecV128Store:
1814 _, offset := c.readMemArg()
1815 if state.unreachable {
1816 break
1817 }
1818 value := state.pop()
1819 baseAddr := state.pop()
1820 addr := c.memOpSetup(baseAddr, uint64(offset), 16)
1821 builder.AllocateInstruction().
1822 AsStore(ssa.OpcodeStore, value, addr, offset).
1823 Insert(builder)
1824 case wasm.OpcodeVecV128Store8Lane, wasm.OpcodeVecV128Store16Lane,
1825 wasm.OpcodeVecV128Store32Lane, wasm.OpcodeVecV128Store64Lane:
1826 _, offset := c.readMemArg()
1827 state.pc++
1828 if state.unreachable {
1829 break
1830 }
1831 laneIndex := c.wasmFunctionBody[state.pc]
1832 var storeOp ssa.Opcode
1833 var lane ssa.VecLane
1834 var opSize uint64
1835 switch vecOp {
1836 case wasm.OpcodeVecV128Store8Lane:
1837 storeOp, lane, opSize = ssa.OpcodeIstore8, ssa.VecLaneI8x16, 1
1838 case wasm.OpcodeVecV128Store16Lane:
1839 storeOp, lane, opSize = ssa.OpcodeIstore16, ssa.VecLaneI16x8, 2
1840 case wasm.OpcodeVecV128Store32Lane:
1841 storeOp, lane, opSize = ssa.OpcodeIstore32, ssa.VecLaneI32x4, 4
1842 case wasm.OpcodeVecV128Store64Lane:
1843 storeOp, lane, opSize = ssa.OpcodeStore, ssa.VecLaneI64x2, 8
1844 }
1845 vector := state.pop()
1846 baseAddr := state.pop()
1847 addr := c.memOpSetup(baseAddr, uint64(offset), opSize)
1848 value := builder.AllocateInstruction().
1849 AsExtractlane(vector, laneIndex, lane, false).
1850 Insert(builder).Return()
1851 builder.AllocateInstruction().
1852 AsStore(storeOp, value, addr, offset).
1853 Insert(builder)
1854 case wasm.OpcodeVecV128Not:
1855 if state.unreachable {
1856 break
1857 }
1858 v1 := state.pop()
1859 ret := builder.AllocateInstruction().AsVbnot(v1).Insert(builder).Return()
1860 state.push(ret)
1861 case wasm.OpcodeVecV128And:
1862 if state.unreachable {
1863 break
1864 }
1865 v2 := state.pop()
1866 v1 := state.pop()
1867 ret := builder.AllocateInstruction().AsVband(v1, v2).Insert(builder).Return()
1868 state.push(ret)
1869 case wasm.OpcodeVecV128AndNot:
1870 if state.unreachable {
1871 break
1872 }
1873 v2 := state.pop()
1874 v1 := state.pop()
1875 ret := builder.AllocateInstruction().AsVbandnot(v1, v2).Insert(builder).Return()
1876 state.push(ret)
1877 case wasm.OpcodeVecV128Or:
1878 if state.unreachable {
1879 break
1880 }
1881 v2 := state.pop()
1882 v1 := state.pop()
1883 ret := builder.AllocateInstruction().AsVbor(v1, v2).Insert(builder).Return()
1884 state.push(ret)
1885 case wasm.OpcodeVecV128Xor:
1886 if state.unreachable {
1887 break
1888 }
1889 v2 := state.pop()
1890 v1 := state.pop()
1891 ret := builder.AllocateInstruction().AsVbxor(v1, v2).Insert(builder).Return()
1892 state.push(ret)
1893 case wasm.OpcodeVecV128Bitselect:
1894 if state.unreachable {
1895 break
1896 }
1897 c := state.pop()
1898 v2 := state.pop()
1899 v1 := state.pop()
1900 ret := builder.AllocateInstruction().AsVbitselect(c, v1, v2).Insert(builder).Return()
1901 state.push(ret)
1902 case wasm.OpcodeVecV128AnyTrue:
1903 if state.unreachable {
1904 break
1905 }
1906 v1 := state.pop()
1907 ret := builder.AllocateInstruction().AsVanyTrue(v1).Insert(builder).Return()
1908 state.push(ret)
1909 case wasm.OpcodeVecI8x16AllTrue, wasm.OpcodeVecI16x8AllTrue, wasm.OpcodeVecI32x4AllTrue, wasm.OpcodeVecI64x2AllTrue:
1910 if state.unreachable {
1911 break
1912 }
1913 var lane ssa.VecLane
1914 switch vecOp {
1915 case wasm.OpcodeVecI8x16AllTrue:
1916 lane = ssa.VecLaneI8x16
1917 case wasm.OpcodeVecI16x8AllTrue:
1918 lane = ssa.VecLaneI16x8
1919 case wasm.OpcodeVecI32x4AllTrue:
1920 lane = ssa.VecLaneI32x4
1921 case wasm.OpcodeVecI64x2AllTrue:
1922 lane = ssa.VecLaneI64x2
1923 }
1924 v1 := state.pop()
1925 ret := builder.AllocateInstruction().AsVallTrue(v1, lane).Insert(builder).Return()
1926 state.push(ret)
1927 case wasm.OpcodeVecI8x16BitMask, wasm.OpcodeVecI16x8BitMask, wasm.OpcodeVecI32x4BitMask, wasm.OpcodeVecI64x2BitMask:
1928 if state.unreachable {
1929 break
1930 }
1931 var lane ssa.VecLane
1932 switch vecOp {
1933 case wasm.OpcodeVecI8x16BitMask:
1934 lane = ssa.VecLaneI8x16
1935 case wasm.OpcodeVecI16x8BitMask:
1936 lane = ssa.VecLaneI16x8
1937 case wasm.OpcodeVecI32x4BitMask:
1938 lane = ssa.VecLaneI32x4
1939 case wasm.OpcodeVecI64x2BitMask:
1940 lane = ssa.VecLaneI64x2
1941 }
1942 v1 := state.pop()
1943 ret := builder.AllocateInstruction().AsVhighBits(v1, lane).Insert(builder).Return()
1944 state.push(ret)
1945 case wasm.OpcodeVecI8x16Abs, wasm.OpcodeVecI16x8Abs, wasm.OpcodeVecI32x4Abs, wasm.OpcodeVecI64x2Abs:
1946 if state.unreachable {
1947 break
1948 }
1949 var lane ssa.VecLane
1950 switch vecOp {
1951 case wasm.OpcodeVecI8x16Abs:
1952 lane = ssa.VecLaneI8x16
1953 case wasm.OpcodeVecI16x8Abs:
1954 lane = ssa.VecLaneI16x8
1955 case wasm.OpcodeVecI32x4Abs:
1956 lane = ssa.VecLaneI32x4
1957 case wasm.OpcodeVecI64x2Abs:
1958 lane = ssa.VecLaneI64x2
1959 }
1960 v1 := state.pop()
1961 ret := builder.AllocateInstruction().AsVIabs(v1, lane).Insert(builder).Return()
1962 state.push(ret)
1963 case wasm.OpcodeVecI8x16Neg, wasm.OpcodeVecI16x8Neg, wasm.OpcodeVecI32x4Neg, wasm.OpcodeVecI64x2Neg:
1964 if state.unreachable {
1965 break
1966 }
1967 var lane ssa.VecLane
1968 switch vecOp {
1969 case wasm.OpcodeVecI8x16Neg:
1970 lane = ssa.VecLaneI8x16
1971 case wasm.OpcodeVecI16x8Neg:
1972 lane = ssa.VecLaneI16x8
1973 case wasm.OpcodeVecI32x4Neg:
1974 lane = ssa.VecLaneI32x4
1975 case wasm.OpcodeVecI64x2Neg:
1976 lane = ssa.VecLaneI64x2
1977 }
1978 v1 := state.pop()
1979 ret := builder.AllocateInstruction().AsVIneg(v1, lane).Insert(builder).Return()
1980 state.push(ret)
1981 case wasm.OpcodeVecI8x16Popcnt:
1982 if state.unreachable {
1983 break
1984 }
1985 lane := ssa.VecLaneI8x16
1986 v1 := state.pop()
1987
1988 ret := builder.AllocateInstruction().AsVIpopcnt(v1, lane).Insert(builder).Return()
1989 state.push(ret)
1990 case wasm.OpcodeVecI8x16Add, wasm.OpcodeVecI16x8Add, wasm.OpcodeVecI32x4Add, wasm.OpcodeVecI64x2Add:
1991 if state.unreachable {
1992 break
1993 }
1994 var lane ssa.VecLane
1995 switch vecOp {
1996 case wasm.OpcodeVecI8x16Add:
1997 lane = ssa.VecLaneI8x16
1998 case wasm.OpcodeVecI16x8Add:
1999 lane = ssa.VecLaneI16x8
2000 case wasm.OpcodeVecI32x4Add:
2001 lane = ssa.VecLaneI32x4
2002 case wasm.OpcodeVecI64x2Add:
2003 lane = ssa.VecLaneI64x2
2004 }
2005 v2 := state.pop()
2006 v1 := state.pop()
2007 ret := builder.AllocateInstruction().AsVIadd(v1, v2, lane).Insert(builder).Return()
2008 state.push(ret)
2009 case wasm.OpcodeVecI8x16AddSatS, wasm.OpcodeVecI16x8AddSatS:
2010 if state.unreachable {
2011 break
2012 }
2013 var lane ssa.VecLane
2014 switch vecOp {
2015 case wasm.OpcodeVecI8x16AddSatS:
2016 lane = ssa.VecLaneI8x16
2017 case wasm.OpcodeVecI16x8AddSatS:
2018 lane = ssa.VecLaneI16x8
2019 }
2020 v2 := state.pop()
2021 v1 := state.pop()
2022 ret := builder.AllocateInstruction().AsVSaddSat(v1, v2, lane).Insert(builder).Return()
2023 state.push(ret)
2024 case wasm.OpcodeVecI8x16AddSatU, wasm.OpcodeVecI16x8AddSatU:
2025 if state.unreachable {
2026 break
2027 }
2028 var lane ssa.VecLane
2029 switch vecOp {
2030 case wasm.OpcodeVecI8x16AddSatU:
2031 lane = ssa.VecLaneI8x16
2032 case wasm.OpcodeVecI16x8AddSatU:
2033 lane = ssa.VecLaneI16x8
2034 }
2035 v2 := state.pop()
2036 v1 := state.pop()
2037 ret := builder.AllocateInstruction().AsVUaddSat(v1, v2, lane).Insert(builder).Return()
2038 state.push(ret)
2039 case wasm.OpcodeVecI8x16SubSatS, wasm.OpcodeVecI16x8SubSatS:
2040 if state.unreachable {
2041 break
2042 }
2043 var lane ssa.VecLane
2044 switch vecOp {
2045 case wasm.OpcodeVecI8x16SubSatS:
2046 lane = ssa.VecLaneI8x16
2047 case wasm.OpcodeVecI16x8SubSatS:
2048 lane = ssa.VecLaneI16x8
2049 }
2050 v2 := state.pop()
2051 v1 := state.pop()
2052 ret := builder.AllocateInstruction().AsVSsubSat(v1, v2, lane).Insert(builder).Return()
2053 state.push(ret)
2054 case wasm.OpcodeVecI8x16SubSatU, wasm.OpcodeVecI16x8SubSatU:
2055 if state.unreachable {
2056 break
2057 }
2058 var lane ssa.VecLane
2059 switch vecOp {
2060 case wasm.OpcodeVecI8x16SubSatU:
2061 lane = ssa.VecLaneI8x16
2062 case wasm.OpcodeVecI16x8SubSatU:
2063 lane = ssa.VecLaneI16x8
2064 }
2065 v2 := state.pop()
2066 v1 := state.pop()
2067 ret := builder.AllocateInstruction().AsVUsubSat(v1, v2, lane).Insert(builder).Return()
2068 state.push(ret)
2069
2070 case wasm.OpcodeVecI8x16Sub, wasm.OpcodeVecI16x8Sub, wasm.OpcodeVecI32x4Sub, wasm.OpcodeVecI64x2Sub:
2071 if state.unreachable {
2072 break
2073 }
2074 var lane ssa.VecLane
2075 switch vecOp {
2076 case wasm.OpcodeVecI8x16Sub:
2077 lane = ssa.VecLaneI8x16
2078 case wasm.OpcodeVecI16x8Sub:
2079 lane = ssa.VecLaneI16x8
2080 case wasm.OpcodeVecI32x4Sub:
2081 lane = ssa.VecLaneI32x4
2082 case wasm.OpcodeVecI64x2Sub:
2083 lane = ssa.VecLaneI64x2
2084 }
2085 v2 := state.pop()
2086 v1 := state.pop()
2087 ret := builder.AllocateInstruction().AsVIsub(v1, v2, lane).Insert(builder).Return()
2088 state.push(ret)
2089 case wasm.OpcodeVecI8x16MinS, wasm.OpcodeVecI16x8MinS, wasm.OpcodeVecI32x4MinS:
2090 if state.unreachable {
2091 break
2092 }
2093 var lane ssa.VecLane
2094 switch vecOp {
2095 case wasm.OpcodeVecI8x16MinS:
2096 lane = ssa.VecLaneI8x16
2097 case wasm.OpcodeVecI16x8MinS:
2098 lane = ssa.VecLaneI16x8
2099 case wasm.OpcodeVecI32x4MinS:
2100 lane = ssa.VecLaneI32x4
2101 }
2102 v2 := state.pop()
2103 v1 := state.pop()
2104 ret := builder.AllocateInstruction().AsVImin(v1, v2, lane).Insert(builder).Return()
2105 state.push(ret)
2106 case wasm.OpcodeVecI8x16MinU, wasm.OpcodeVecI16x8MinU, wasm.OpcodeVecI32x4MinU:
2107 if state.unreachable {
2108 break
2109 }
2110 var lane ssa.VecLane
2111 switch vecOp {
2112 case wasm.OpcodeVecI8x16MinU:
2113 lane = ssa.VecLaneI8x16
2114 case wasm.OpcodeVecI16x8MinU:
2115 lane = ssa.VecLaneI16x8
2116 case wasm.OpcodeVecI32x4MinU:
2117 lane = ssa.VecLaneI32x4
2118 }
2119 v2 := state.pop()
2120 v1 := state.pop()
2121 ret := builder.AllocateInstruction().AsVUmin(v1, v2, lane).Insert(builder).Return()
2122 state.push(ret)
2123 case wasm.OpcodeVecI8x16MaxS, wasm.OpcodeVecI16x8MaxS, wasm.OpcodeVecI32x4MaxS:
2124 if state.unreachable {
2125 break
2126 }
2127 var lane ssa.VecLane
2128 switch vecOp {
2129 case wasm.OpcodeVecI8x16MaxS:
2130 lane = ssa.VecLaneI8x16
2131 case wasm.OpcodeVecI16x8MaxS:
2132 lane = ssa.VecLaneI16x8
2133 case wasm.OpcodeVecI32x4MaxS:
2134 lane = ssa.VecLaneI32x4
2135 }
2136 v2 := state.pop()
2137 v1 := state.pop()
2138 ret := builder.AllocateInstruction().AsVImax(v1, v2, lane).Insert(builder).Return()
2139 state.push(ret)
2140 case wasm.OpcodeVecI8x16MaxU, wasm.OpcodeVecI16x8MaxU, wasm.OpcodeVecI32x4MaxU:
2141 if state.unreachable {
2142 break
2143 }
2144 var lane ssa.VecLane
2145 switch vecOp {
2146 case wasm.OpcodeVecI8x16MaxU:
2147 lane = ssa.VecLaneI8x16
2148 case wasm.OpcodeVecI16x8MaxU:
2149 lane = ssa.VecLaneI16x8
2150 case wasm.OpcodeVecI32x4MaxU:
2151 lane = ssa.VecLaneI32x4
2152 }
2153 v2 := state.pop()
2154 v1 := state.pop()
2155 ret := builder.AllocateInstruction().AsVUmax(v1, v2, lane).Insert(builder).Return()
2156 state.push(ret)
2157 case wasm.OpcodeVecI8x16AvgrU, wasm.OpcodeVecI16x8AvgrU:
2158 if state.unreachable {
2159 break
2160 }
2161 var lane ssa.VecLane
2162 switch vecOp {
2163 case wasm.OpcodeVecI8x16AvgrU:
2164 lane = ssa.VecLaneI8x16
2165 case wasm.OpcodeVecI16x8AvgrU:
2166 lane = ssa.VecLaneI16x8
2167 }
2168 v2 := state.pop()
2169 v1 := state.pop()
2170 ret := builder.AllocateInstruction().AsVAvgRound(v1, v2, lane).Insert(builder).Return()
2171 state.push(ret)
2172 case wasm.OpcodeVecI16x8Mul, wasm.OpcodeVecI32x4Mul, wasm.OpcodeVecI64x2Mul:
2173 if state.unreachable {
2174 break
2175 }
2176 var lane ssa.VecLane
2177 switch vecOp {
2178 case wasm.OpcodeVecI16x8Mul:
2179 lane = ssa.VecLaneI16x8
2180 case wasm.OpcodeVecI32x4Mul:
2181 lane = ssa.VecLaneI32x4
2182 case wasm.OpcodeVecI64x2Mul:
2183 lane = ssa.VecLaneI64x2
2184 }
2185 v2 := state.pop()
2186 v1 := state.pop()
2187 ret := builder.AllocateInstruction().AsVImul(v1, v2, lane).Insert(builder).Return()
2188 state.push(ret)
2189 case wasm.OpcodeVecI16x8Q15mulrSatS:
2190 if state.unreachable {
2191 break
2192 }
2193 v2 := state.pop()
2194 v1 := state.pop()
2195 ret := builder.AllocateInstruction().AsSqmulRoundSat(v1, v2, ssa.VecLaneI16x8).Insert(builder).Return()
2196 state.push(ret)
2197 case wasm.OpcodeVecI8x16Eq, wasm.OpcodeVecI16x8Eq, wasm.OpcodeVecI32x4Eq, wasm.OpcodeVecI64x2Eq:
2198 if state.unreachable {
2199 break
2200 }
2201 var lane ssa.VecLane
2202 switch vecOp {
2203 case wasm.OpcodeVecI8x16Eq:
2204 lane = ssa.VecLaneI8x16
2205 case wasm.OpcodeVecI16x8Eq:
2206 lane = ssa.VecLaneI16x8
2207 case wasm.OpcodeVecI32x4Eq:
2208 lane = ssa.VecLaneI32x4
2209 case wasm.OpcodeVecI64x2Eq:
2210 lane = ssa.VecLaneI64x2
2211 }
2212 v2 := state.pop()
2213 v1 := state.pop()
2214 ret := builder.AllocateInstruction().
2215 AsVIcmp(v1, v2, ssa.IntegerCmpCondEqual, lane).Insert(builder).Return()
2216 state.push(ret)
2217 case wasm.OpcodeVecI8x16Ne, wasm.OpcodeVecI16x8Ne, wasm.OpcodeVecI32x4Ne, wasm.OpcodeVecI64x2Ne:
2218 if state.unreachable {
2219 break
2220 }
2221 var lane ssa.VecLane
2222 switch vecOp {
2223 case wasm.OpcodeVecI8x16Ne:
2224 lane = ssa.VecLaneI8x16
2225 case wasm.OpcodeVecI16x8Ne:
2226 lane = ssa.VecLaneI16x8
2227 case wasm.OpcodeVecI32x4Ne:
2228 lane = ssa.VecLaneI32x4
2229 case wasm.OpcodeVecI64x2Ne:
2230 lane = ssa.VecLaneI64x2
2231 }
2232 v2 := state.pop()
2233 v1 := state.pop()
2234 ret := builder.AllocateInstruction().
2235 AsVIcmp(v1, v2, ssa.IntegerCmpCondNotEqual, lane).Insert(builder).Return()
2236 state.push(ret)
2237 case wasm.OpcodeVecI8x16LtS, wasm.OpcodeVecI16x8LtS, wasm.OpcodeVecI32x4LtS, wasm.OpcodeVecI64x2LtS:
2238 if state.unreachable {
2239 break
2240 }
2241 var lane ssa.VecLane
2242 switch vecOp {
2243 case wasm.OpcodeVecI8x16LtS:
2244 lane = ssa.VecLaneI8x16
2245 case wasm.OpcodeVecI16x8LtS:
2246 lane = ssa.VecLaneI16x8
2247 case wasm.OpcodeVecI32x4LtS:
2248 lane = ssa.VecLaneI32x4
2249 case wasm.OpcodeVecI64x2LtS:
2250 lane = ssa.VecLaneI64x2
2251 }
2252 v2 := state.pop()
2253 v1 := state.pop()
2254 ret := builder.AllocateInstruction().
2255 AsVIcmp(v1, v2, ssa.IntegerCmpCondSignedLessThan, lane).Insert(builder).Return()
2256 state.push(ret)
2257 case wasm.OpcodeVecI8x16LtU, wasm.OpcodeVecI16x8LtU, wasm.OpcodeVecI32x4LtU:
2258 if state.unreachable {
2259 break
2260 }
2261 var lane ssa.VecLane
2262 switch vecOp {
2263 case wasm.OpcodeVecI8x16LtU:
2264 lane = ssa.VecLaneI8x16
2265 case wasm.OpcodeVecI16x8LtU:
2266 lane = ssa.VecLaneI16x8
2267 case wasm.OpcodeVecI32x4LtU:
2268 lane = ssa.VecLaneI32x4
2269 }
2270 v2 := state.pop()
2271 v1 := state.pop()
2272 ret := builder.AllocateInstruction().
2273 AsVIcmp(v1, v2, ssa.IntegerCmpCondUnsignedLessThan, lane).Insert(builder).Return()
2274 state.push(ret)
2275 case wasm.OpcodeVecI8x16LeS, wasm.OpcodeVecI16x8LeS, wasm.OpcodeVecI32x4LeS, wasm.OpcodeVecI64x2LeS:
2276 if state.unreachable {
2277 break
2278 }
2279 var lane ssa.VecLane
2280 switch vecOp {
2281 case wasm.OpcodeVecI8x16LeS:
2282 lane = ssa.VecLaneI8x16
2283 case wasm.OpcodeVecI16x8LeS:
2284 lane = ssa.VecLaneI16x8
2285 case wasm.OpcodeVecI32x4LeS:
2286 lane = ssa.VecLaneI32x4
2287 case wasm.OpcodeVecI64x2LeS:
2288 lane = ssa.VecLaneI64x2
2289 }
2290 v2 := state.pop()
2291 v1 := state.pop()
2292 ret := builder.AllocateInstruction().
2293 AsVIcmp(v1, v2, ssa.IntegerCmpCondSignedLessThanOrEqual, lane).Insert(builder).Return()
2294 state.push(ret)
2295 case wasm.OpcodeVecI8x16LeU, wasm.OpcodeVecI16x8LeU, wasm.OpcodeVecI32x4LeU:
2296 if state.unreachable {
2297 break
2298 }
2299 var lane ssa.VecLane
2300 switch vecOp {
2301 case wasm.OpcodeVecI8x16LeU:
2302 lane = ssa.VecLaneI8x16
2303 case wasm.OpcodeVecI16x8LeU:
2304 lane = ssa.VecLaneI16x8
2305 case wasm.OpcodeVecI32x4LeU:
2306 lane = ssa.VecLaneI32x4
2307 }
2308 v2 := state.pop()
2309 v1 := state.pop()
2310 ret := builder.AllocateInstruction().
2311 AsVIcmp(v1, v2, ssa.IntegerCmpCondUnsignedLessThanOrEqual, lane).Insert(builder).Return()
2312 state.push(ret)
2313 case wasm.OpcodeVecI8x16GtS, wasm.OpcodeVecI16x8GtS, wasm.OpcodeVecI32x4GtS, wasm.OpcodeVecI64x2GtS:
2314 if state.unreachable {
2315 break
2316 }
2317 var lane ssa.VecLane
2318 switch vecOp {
2319 case wasm.OpcodeVecI8x16GtS:
2320 lane = ssa.VecLaneI8x16
2321 case wasm.OpcodeVecI16x8GtS:
2322 lane = ssa.VecLaneI16x8
2323 case wasm.OpcodeVecI32x4GtS:
2324 lane = ssa.VecLaneI32x4
2325 case wasm.OpcodeVecI64x2GtS:
2326 lane = ssa.VecLaneI64x2
2327 }
2328 v2 := state.pop()
2329 v1 := state.pop()
2330 ret := builder.AllocateInstruction().
2331 AsVIcmp(v1, v2, ssa.IntegerCmpCondSignedGreaterThan, lane).Insert(builder).Return()
2332 state.push(ret)
2333 case wasm.OpcodeVecI8x16GtU, wasm.OpcodeVecI16x8GtU, wasm.OpcodeVecI32x4GtU:
2334 if state.unreachable {
2335 break
2336 }
2337 var lane ssa.VecLane
2338 switch vecOp {
2339 case wasm.OpcodeVecI8x16GtU:
2340 lane = ssa.VecLaneI8x16
2341 case wasm.OpcodeVecI16x8GtU:
2342 lane = ssa.VecLaneI16x8
2343 case wasm.OpcodeVecI32x4GtU:
2344 lane = ssa.VecLaneI32x4
2345 }
2346 v2 := state.pop()
2347 v1 := state.pop()
2348 ret := builder.AllocateInstruction().
2349 AsVIcmp(v1, v2, ssa.IntegerCmpCondUnsignedGreaterThan, lane).Insert(builder).Return()
2350 state.push(ret)
2351 case wasm.OpcodeVecI8x16GeS, wasm.OpcodeVecI16x8GeS, wasm.OpcodeVecI32x4GeS, wasm.OpcodeVecI64x2GeS:
2352 if state.unreachable {
2353 break
2354 }
2355 var lane ssa.VecLane
2356 switch vecOp {
2357 case wasm.OpcodeVecI8x16GeS:
2358 lane = ssa.VecLaneI8x16
2359 case wasm.OpcodeVecI16x8GeS:
2360 lane = ssa.VecLaneI16x8
2361 case wasm.OpcodeVecI32x4GeS:
2362 lane = ssa.VecLaneI32x4
2363 case wasm.OpcodeVecI64x2GeS:
2364 lane = ssa.VecLaneI64x2
2365 }
2366 v2 := state.pop()
2367 v1 := state.pop()
2368 ret := builder.AllocateInstruction().
2369 AsVIcmp(v1, v2, ssa.IntegerCmpCondSignedGreaterThanOrEqual, lane).Insert(builder).Return()
2370 state.push(ret)
2371 case wasm.OpcodeVecI8x16GeU, wasm.OpcodeVecI16x8GeU, wasm.OpcodeVecI32x4GeU:
2372 if state.unreachable {
2373 break
2374 }
2375 var lane ssa.VecLane
2376 switch vecOp {
2377 case wasm.OpcodeVecI8x16GeU:
2378 lane = ssa.VecLaneI8x16
2379 case wasm.OpcodeVecI16x8GeU:
2380 lane = ssa.VecLaneI16x8
2381 case wasm.OpcodeVecI32x4GeU:
2382 lane = ssa.VecLaneI32x4
2383 }
2384 v2 := state.pop()
2385 v1 := state.pop()
2386 ret := builder.AllocateInstruction().
2387 AsVIcmp(v1, v2, ssa.IntegerCmpCondUnsignedGreaterThanOrEqual, lane).Insert(builder).Return()
2388 state.push(ret)
2389 case wasm.OpcodeVecF32x4Max, wasm.OpcodeVecF64x2Max:
2390 if state.unreachable {
2391 break
2392 }
2393 var lane ssa.VecLane
2394 switch vecOp {
2395 case wasm.OpcodeVecF32x4Max:
2396 lane = ssa.VecLaneF32x4
2397 case wasm.OpcodeVecF64x2Max:
2398 lane = ssa.VecLaneF64x2
2399 }
2400 v2 := state.pop()
2401 v1 := state.pop()
2402 ret := builder.AllocateInstruction().AsVFmax(v1, v2, lane).Insert(builder).Return()
2403 state.push(ret)
2404 case wasm.OpcodeVecF32x4Abs, wasm.OpcodeVecF64x2Abs:
2405 if state.unreachable {
2406 break
2407 }
2408 var lane ssa.VecLane
2409 switch vecOp {
2410 case wasm.OpcodeVecF32x4Abs:
2411 lane = ssa.VecLaneF32x4
2412 case wasm.OpcodeVecF64x2Abs:
2413 lane = ssa.VecLaneF64x2
2414 }
2415 v1 := state.pop()
2416 ret := builder.AllocateInstruction().AsVFabs(v1, lane).Insert(builder).Return()
2417 state.push(ret)
2418 case wasm.OpcodeVecF32x4Min, wasm.OpcodeVecF64x2Min:
2419 if state.unreachable {
2420 break
2421 }
2422 var lane ssa.VecLane
2423 switch vecOp {
2424 case wasm.OpcodeVecF32x4Min:
2425 lane = ssa.VecLaneF32x4
2426 case wasm.OpcodeVecF64x2Min:
2427 lane = ssa.VecLaneF64x2
2428 }
2429 v2 := state.pop()
2430 v1 := state.pop()
2431 ret := builder.AllocateInstruction().AsVFmin(v1, v2, lane).Insert(builder).Return()
2432 state.push(ret)
2433 case wasm.OpcodeVecF32x4Neg, wasm.OpcodeVecF64x2Neg:
2434 if state.unreachable {
2435 break
2436 }
2437 var lane ssa.VecLane
2438 switch vecOp {
2439 case wasm.OpcodeVecF32x4Neg:
2440 lane = ssa.VecLaneF32x4
2441 case wasm.OpcodeVecF64x2Neg:
2442 lane = ssa.VecLaneF64x2
2443 }
2444 v1 := state.pop()
2445 ret := builder.AllocateInstruction().AsVFneg(v1, lane).Insert(builder).Return()
2446 state.push(ret)
2447 case wasm.OpcodeVecF32x4Sqrt, wasm.OpcodeVecF64x2Sqrt:
2448 if state.unreachable {
2449 break
2450 }
2451 var lane ssa.VecLane
2452 switch vecOp {
2453 case wasm.OpcodeVecF32x4Sqrt:
2454 lane = ssa.VecLaneF32x4
2455 case wasm.OpcodeVecF64x2Sqrt:
2456 lane = ssa.VecLaneF64x2
2457 }
2458 v1 := state.pop()
2459 ret := builder.AllocateInstruction().AsVSqrt(v1, lane).Insert(builder).Return()
2460 state.push(ret)
2461
2462 case wasm.OpcodeVecF32x4Add, wasm.OpcodeVecF64x2Add:
2463 if state.unreachable {
2464 break
2465 }
2466 var lane ssa.VecLane
2467 switch vecOp {
2468 case wasm.OpcodeVecF32x4Add:
2469 lane = ssa.VecLaneF32x4
2470 case wasm.OpcodeVecF64x2Add:
2471 lane = ssa.VecLaneF64x2
2472 }
2473 v2 := state.pop()
2474 v1 := state.pop()
2475 ret := builder.AllocateInstruction().AsVFadd(v1, v2, lane).Insert(builder).Return()
2476 state.push(ret)
2477 case wasm.OpcodeVecF32x4Sub, wasm.OpcodeVecF64x2Sub:
2478 if state.unreachable {
2479 break
2480 }
2481 var lane ssa.VecLane
2482 switch vecOp {
2483 case wasm.OpcodeVecF32x4Sub:
2484 lane = ssa.VecLaneF32x4
2485 case wasm.OpcodeVecF64x2Sub:
2486 lane = ssa.VecLaneF64x2
2487 }
2488 v2 := state.pop()
2489 v1 := state.pop()
2490 ret := builder.AllocateInstruction().AsVFsub(v1, v2, lane).Insert(builder).Return()
2491 state.push(ret)
2492 case wasm.OpcodeVecF32x4Mul, wasm.OpcodeVecF64x2Mul:
2493 if state.unreachable {
2494 break
2495 }
2496 var lane ssa.VecLane
2497 switch vecOp {
2498 case wasm.OpcodeVecF32x4Mul:
2499 lane = ssa.VecLaneF32x4
2500 case wasm.OpcodeVecF64x2Mul:
2501 lane = ssa.VecLaneF64x2
2502 }
2503 v2 := state.pop()
2504 v1 := state.pop()
2505 ret := builder.AllocateInstruction().AsVFmul(v1, v2, lane).Insert(builder).Return()
2506 state.push(ret)
2507 case wasm.OpcodeVecF32x4Div, wasm.OpcodeVecF64x2Div:
2508 if state.unreachable {
2509 break
2510 }
2511 var lane ssa.VecLane
2512 switch vecOp {
2513 case wasm.OpcodeVecF32x4Div:
2514 lane = ssa.VecLaneF32x4
2515 case wasm.OpcodeVecF64x2Div:
2516 lane = ssa.VecLaneF64x2
2517 }
2518 v2 := state.pop()
2519 v1 := state.pop()
2520 ret := builder.AllocateInstruction().AsVFdiv(v1, v2, lane).Insert(builder).Return()
2521 state.push(ret)
2522
2523 case wasm.OpcodeVecI16x8ExtaddPairwiseI8x16S, wasm.OpcodeVecI16x8ExtaddPairwiseI8x16U:
2524 if state.unreachable {
2525 break
2526 }
2527 v := state.pop()
2528 signed := vecOp == wasm.OpcodeVecI16x8ExtaddPairwiseI8x16S
2529 ret := builder.AllocateInstruction().AsExtIaddPairwise(v, ssa.VecLaneI8x16, signed).Insert(builder).Return()
2530 state.push(ret)
2531
2532 case wasm.OpcodeVecI32x4ExtaddPairwiseI16x8S, wasm.OpcodeVecI32x4ExtaddPairwiseI16x8U:
2533 if state.unreachable {
2534 break
2535 }
2536 v := state.pop()
2537 signed := vecOp == wasm.OpcodeVecI32x4ExtaddPairwiseI16x8S
2538 ret := builder.AllocateInstruction().AsExtIaddPairwise(v, ssa.VecLaneI16x8, signed).Insert(builder).Return()
2539 state.push(ret)
2540
2541 case wasm.OpcodeVecI16x8ExtMulLowI8x16S, wasm.OpcodeVecI16x8ExtMulLowI8x16U:
2542 if state.unreachable {
2543 break
2544 }
2545 v2 := state.pop()
2546 v1 := state.pop()
2547 ret := c.lowerExtMul(
2548 v1, v2,
2549 ssa.VecLaneI8x16, ssa.VecLaneI16x8,
2550 vecOp == wasm.OpcodeVecI16x8ExtMulLowI8x16S, true)
2551 state.push(ret)
2552
2553 case wasm.OpcodeVecI16x8ExtMulHighI8x16S, wasm.OpcodeVecI16x8ExtMulHighI8x16U:
2554 if state.unreachable {
2555 break
2556 }
2557 v2 := state.pop()
2558 v1 := state.pop()
2559 ret := c.lowerExtMul(
2560 v1, v2,
2561 ssa.VecLaneI8x16, ssa.VecLaneI16x8,
2562 vecOp == wasm.OpcodeVecI16x8ExtMulHighI8x16S, false)
2563 state.push(ret)
2564
2565 case wasm.OpcodeVecI32x4ExtMulLowI16x8S, wasm.OpcodeVecI32x4ExtMulLowI16x8U:
2566 if state.unreachable {
2567 break
2568 }
2569 v2 := state.pop()
2570 v1 := state.pop()
2571 ret := c.lowerExtMul(
2572 v1, v2,
2573 ssa.VecLaneI16x8, ssa.VecLaneI32x4,
2574 vecOp == wasm.OpcodeVecI32x4ExtMulLowI16x8S, true)
2575 state.push(ret)
2576
2577 case wasm.OpcodeVecI32x4ExtMulHighI16x8S, wasm.OpcodeVecI32x4ExtMulHighI16x8U:
2578 if state.unreachable {
2579 break
2580 }
2581 v2 := state.pop()
2582 v1 := state.pop()
2583 ret := c.lowerExtMul(
2584 v1, v2,
2585 ssa.VecLaneI16x8, ssa.VecLaneI32x4,
2586 vecOp == wasm.OpcodeVecI32x4ExtMulHighI16x8S, false)
2587 state.push(ret)
2588 case wasm.OpcodeVecI64x2ExtMulLowI32x4S, wasm.OpcodeVecI64x2ExtMulLowI32x4U:
2589 if state.unreachable {
2590 break
2591 }
2592 v2 := state.pop()
2593 v1 := state.pop()
2594 ret := c.lowerExtMul(
2595 v1, v2,
2596 ssa.VecLaneI32x4, ssa.VecLaneI64x2,
2597 vecOp == wasm.OpcodeVecI64x2ExtMulLowI32x4S, true)
2598 state.push(ret)
2599
2600 case wasm.OpcodeVecI64x2ExtMulHighI32x4S, wasm.OpcodeVecI64x2ExtMulHighI32x4U:
2601 if state.unreachable {
2602 break
2603 }
2604 v2 := state.pop()
2605 v1 := state.pop()
2606 ret := c.lowerExtMul(
2607 v1, v2,
2608 ssa.VecLaneI32x4, ssa.VecLaneI64x2,
2609 vecOp == wasm.OpcodeVecI64x2ExtMulHighI32x4S, false)
2610 state.push(ret)
2611
2612 case wasm.OpcodeVecI32x4DotI16x8S:
2613 if state.unreachable {
2614 break
2615 }
2616 v2 := state.pop()
2617 v1 := state.pop()
2618
2619 ret := builder.AllocateInstruction().AsWideningPairwiseDotProductS(v1, v2).Insert(builder).Return()
2620 state.push(ret)
2621
2622 case wasm.OpcodeVecF32x4Eq, wasm.OpcodeVecF64x2Eq:
2623 if state.unreachable {
2624 break
2625 }
2626 var lane ssa.VecLane
2627 switch vecOp {
2628 case wasm.OpcodeVecF32x4Eq:
2629 lane = ssa.VecLaneF32x4
2630 case wasm.OpcodeVecF64x2Eq:
2631 lane = ssa.VecLaneF64x2
2632 }
2633 v2 := state.pop()
2634 v1 := state.pop()
2635 ret := builder.AllocateInstruction().
2636 AsVFcmp(v1, v2, ssa.FloatCmpCondEqual, lane).Insert(builder).Return()
2637 state.push(ret)
2638 case wasm.OpcodeVecF32x4Ne, wasm.OpcodeVecF64x2Ne:
2639 if state.unreachable {
2640 break
2641 }
2642 var lane ssa.VecLane
2643 switch vecOp {
2644 case wasm.OpcodeVecF32x4Ne:
2645 lane = ssa.VecLaneF32x4
2646 case wasm.OpcodeVecF64x2Ne:
2647 lane = ssa.VecLaneF64x2
2648 }
2649 v2 := state.pop()
2650 v1 := state.pop()
2651 ret := builder.AllocateInstruction().
2652 AsVFcmp(v1, v2, ssa.FloatCmpCondNotEqual, lane).Insert(builder).Return()
2653 state.push(ret)
2654 case wasm.OpcodeVecF32x4Lt, wasm.OpcodeVecF64x2Lt:
2655 if state.unreachable {
2656 break
2657 }
2658 var lane ssa.VecLane
2659 switch vecOp {
2660 case wasm.OpcodeVecF32x4Lt:
2661 lane = ssa.VecLaneF32x4
2662 case wasm.OpcodeVecF64x2Lt:
2663 lane = ssa.VecLaneF64x2
2664 }
2665 v2 := state.pop()
2666 v1 := state.pop()
2667 ret := builder.AllocateInstruction().
2668 AsVFcmp(v1, v2, ssa.FloatCmpCondLessThan, lane).Insert(builder).Return()
2669 state.push(ret)
2670 case wasm.OpcodeVecF32x4Le, wasm.OpcodeVecF64x2Le:
2671 if state.unreachable {
2672 break
2673 }
2674 var lane ssa.VecLane
2675 switch vecOp {
2676 case wasm.OpcodeVecF32x4Le:
2677 lane = ssa.VecLaneF32x4
2678 case wasm.OpcodeVecF64x2Le:
2679 lane = ssa.VecLaneF64x2
2680 }
2681 v2 := state.pop()
2682 v1 := state.pop()
2683 ret := builder.AllocateInstruction().
2684 AsVFcmp(v1, v2, ssa.FloatCmpCondLessThanOrEqual, lane).Insert(builder).Return()
2685 state.push(ret)
2686 case wasm.OpcodeVecF32x4Gt, wasm.OpcodeVecF64x2Gt:
2687 if state.unreachable {
2688 break
2689 }
2690 var lane ssa.VecLane
2691 switch vecOp {
2692 case wasm.OpcodeVecF32x4Gt:
2693 lane = ssa.VecLaneF32x4
2694 case wasm.OpcodeVecF64x2Gt:
2695 lane = ssa.VecLaneF64x2
2696 }
2697 v2 := state.pop()
2698 v1 := state.pop()
2699 ret := builder.AllocateInstruction().
2700 AsVFcmp(v1, v2, ssa.FloatCmpCondGreaterThan, lane).Insert(builder).Return()
2701 state.push(ret)
2702 case wasm.OpcodeVecF32x4Ge, wasm.OpcodeVecF64x2Ge:
2703 if state.unreachable {
2704 break
2705 }
2706 var lane ssa.VecLane
2707 switch vecOp {
2708 case wasm.OpcodeVecF32x4Ge:
2709 lane = ssa.VecLaneF32x4
2710 case wasm.OpcodeVecF64x2Ge:
2711 lane = ssa.VecLaneF64x2
2712 }
2713 v2 := state.pop()
2714 v1 := state.pop()
2715 ret := builder.AllocateInstruction().
2716 AsVFcmp(v1, v2, ssa.FloatCmpCondGreaterThanOrEqual, lane).Insert(builder).Return()
2717 state.push(ret)
2718 case wasm.OpcodeVecF32x4Ceil, wasm.OpcodeVecF64x2Ceil:
2719 if state.unreachable {
2720 break
2721 }
2722 var lane ssa.VecLane
2723 switch vecOp {
2724 case wasm.OpcodeVecF32x4Ceil:
2725 lane = ssa.VecLaneF32x4
2726 case wasm.OpcodeVecF64x2Ceil:
2727 lane = ssa.VecLaneF64x2
2728 }
2729 v1 := state.pop()
2730 ret := builder.AllocateInstruction().AsVCeil(v1, lane).Insert(builder).Return()
2731 state.push(ret)
2732 case wasm.OpcodeVecF32x4Floor, wasm.OpcodeVecF64x2Floor:
2733 if state.unreachable {
2734 break
2735 }
2736 var lane ssa.VecLane
2737 switch vecOp {
2738 case wasm.OpcodeVecF32x4Floor:
2739 lane = ssa.VecLaneF32x4
2740 case wasm.OpcodeVecF64x2Floor:
2741 lane = ssa.VecLaneF64x2
2742 }
2743 v1 := state.pop()
2744 ret := builder.AllocateInstruction().AsVFloor(v1, lane).Insert(builder).Return()
2745 state.push(ret)
2746 case wasm.OpcodeVecF32x4Trunc, wasm.OpcodeVecF64x2Trunc:
2747 if state.unreachable {
2748 break
2749 }
2750 var lane ssa.VecLane
2751 switch vecOp {
2752 case wasm.OpcodeVecF32x4Trunc:
2753 lane = ssa.VecLaneF32x4
2754 case wasm.OpcodeVecF64x2Trunc:
2755 lane = ssa.VecLaneF64x2
2756 }
2757 v1 := state.pop()
2758 ret := builder.AllocateInstruction().AsVTrunc(v1, lane).Insert(builder).Return()
2759 state.push(ret)
2760 case wasm.OpcodeVecF32x4Nearest, wasm.OpcodeVecF64x2Nearest:
2761 if state.unreachable {
2762 break
2763 }
2764 var lane ssa.VecLane
2765 switch vecOp {
2766 case wasm.OpcodeVecF32x4Nearest:
2767 lane = ssa.VecLaneF32x4
2768 case wasm.OpcodeVecF64x2Nearest:
2769 lane = ssa.VecLaneF64x2
2770 }
2771 v1 := state.pop()
2772 ret := builder.AllocateInstruction().AsVNearest(v1, lane).Insert(builder).Return()
2773 state.push(ret)
2774 case wasm.OpcodeVecF32x4Pmin, wasm.OpcodeVecF64x2Pmin:
2775 if state.unreachable {
2776 break
2777 }
2778 var lane ssa.VecLane
2779 switch vecOp {
2780 case wasm.OpcodeVecF32x4Pmin:
2781 lane = ssa.VecLaneF32x4
2782 case wasm.OpcodeVecF64x2Pmin:
2783 lane = ssa.VecLaneF64x2
2784 }
2785 v2 := state.pop()
2786 v1 := state.pop()
2787 ret := builder.AllocateInstruction().AsVMinPseudo(v1, v2, lane).Insert(builder).Return()
2788 state.push(ret)
2789 case wasm.OpcodeVecF32x4Pmax, wasm.OpcodeVecF64x2Pmax:
2790 if state.unreachable {
2791 break
2792 }
2793 var lane ssa.VecLane
2794 switch vecOp {
2795 case wasm.OpcodeVecF32x4Pmax:
2796 lane = ssa.VecLaneF32x4
2797 case wasm.OpcodeVecF64x2Pmax:
2798 lane = ssa.VecLaneF64x2
2799 }
2800 v2 := state.pop()
2801 v1 := state.pop()
2802 ret := builder.AllocateInstruction().AsVMaxPseudo(v1, v2, lane).Insert(builder).Return()
2803 state.push(ret)
2804 case wasm.OpcodeVecI32x4TruncSatF32x4S, wasm.OpcodeVecI32x4TruncSatF32x4U:
2805 if state.unreachable {
2806 break
2807 }
2808 v1 := state.pop()
2809 ret := builder.AllocateInstruction().
2810 AsVFcvtToIntSat(v1, ssa.VecLaneF32x4, vecOp == wasm.OpcodeVecI32x4TruncSatF32x4S).Insert(builder).Return()
2811 state.push(ret)
2812 case wasm.OpcodeVecI32x4TruncSatF64x2SZero, wasm.OpcodeVecI32x4TruncSatF64x2UZero:
2813 if state.unreachable {
2814 break
2815 }
2816 v1 := state.pop()
2817 ret := builder.AllocateInstruction().
2818 AsVFcvtToIntSat(v1, ssa.VecLaneF64x2, vecOp == wasm.OpcodeVecI32x4TruncSatF64x2SZero).Insert(builder).Return()
2819 state.push(ret)
2820 case wasm.OpcodeVecF32x4ConvertI32x4S, wasm.OpcodeVecF32x4ConvertI32x4U:
2821 if state.unreachable {
2822 break
2823 }
2824 v1 := state.pop()
2825 ret := builder.AllocateInstruction().
2826 AsVFcvtFromInt(v1, ssa.VecLaneF32x4, vecOp == wasm.OpcodeVecF32x4ConvertI32x4S).Insert(builder).Return()
2827 state.push(ret)
2828 case wasm.OpcodeVecF64x2ConvertLowI32x4S, wasm.OpcodeVecF64x2ConvertLowI32x4U:
2829 if state.unreachable {
2830 break
2831 }
2832 v1 := state.pop()
2833 if runtime.GOARCH == "arm64" {
2834 // TODO: this is weird. fix.
2835 v1 = builder.AllocateInstruction().
2836 AsWiden(v1, ssa.VecLaneI32x4, vecOp == wasm.OpcodeVecF64x2ConvertLowI32x4S, true).Insert(builder).Return()
2837 }
2838 ret := builder.AllocateInstruction().
2839 AsVFcvtFromInt(v1, ssa.VecLaneF64x2, vecOp == wasm.OpcodeVecF64x2ConvertLowI32x4S).
2840 Insert(builder).Return()
2841 state.push(ret)
2842 case wasm.OpcodeVecI8x16NarrowI16x8S, wasm.OpcodeVecI8x16NarrowI16x8U:
2843 if state.unreachable {
2844 break
2845 }
2846 v2 := state.pop()
2847 v1 := state.pop()
2848 ret := builder.AllocateInstruction().
2849 AsNarrow(v1, v2, ssa.VecLaneI16x8, vecOp == wasm.OpcodeVecI8x16NarrowI16x8S).
2850 Insert(builder).Return()
2851 state.push(ret)
2852 case wasm.OpcodeVecI16x8NarrowI32x4S, wasm.OpcodeVecI16x8NarrowI32x4U:
2853 if state.unreachable {
2854 break
2855 }
2856 v2 := state.pop()
2857 v1 := state.pop()
2858 ret := builder.AllocateInstruction().
2859 AsNarrow(v1, v2, ssa.VecLaneI32x4, vecOp == wasm.OpcodeVecI16x8NarrowI32x4S).
2860 Insert(builder).Return()
2861 state.push(ret)
2862 case wasm.OpcodeVecI16x8ExtendLowI8x16S, wasm.OpcodeVecI16x8ExtendLowI8x16U:
2863 if state.unreachable {
2864 break
2865 }
2866 v1 := state.pop()
2867 ret := builder.AllocateInstruction().
2868 AsWiden(v1, ssa.VecLaneI8x16, vecOp == wasm.OpcodeVecI16x8ExtendLowI8x16S, true).
2869 Insert(builder).Return()
2870 state.push(ret)
2871 case wasm.OpcodeVecI16x8ExtendHighI8x16S, wasm.OpcodeVecI16x8ExtendHighI8x16U:
2872 if state.unreachable {
2873 break
2874 }
2875 v1 := state.pop()
2876 ret := builder.AllocateInstruction().
2877 AsWiden(v1, ssa.VecLaneI8x16, vecOp == wasm.OpcodeVecI16x8ExtendHighI8x16S, false).
2878 Insert(builder).Return()
2879 state.push(ret)
2880 case wasm.OpcodeVecI32x4ExtendLowI16x8S, wasm.OpcodeVecI32x4ExtendLowI16x8U:
2881 if state.unreachable {
2882 break
2883 }
2884 v1 := state.pop()
2885 ret := builder.AllocateInstruction().
2886 AsWiden(v1, ssa.VecLaneI16x8, vecOp == wasm.OpcodeVecI32x4ExtendLowI16x8S, true).
2887 Insert(builder).Return()
2888 state.push(ret)
2889 case wasm.OpcodeVecI32x4ExtendHighI16x8S, wasm.OpcodeVecI32x4ExtendHighI16x8U:
2890 if state.unreachable {
2891 break
2892 }
2893 v1 := state.pop()
2894 ret := builder.AllocateInstruction().
2895 AsWiden(v1, ssa.VecLaneI16x8, vecOp == wasm.OpcodeVecI32x4ExtendHighI16x8S, false).
2896 Insert(builder).Return()
2897 state.push(ret)
2898 case wasm.OpcodeVecI64x2ExtendLowI32x4S, wasm.OpcodeVecI64x2ExtendLowI32x4U:
2899 if state.unreachable {
2900 break
2901 }
2902 v1 := state.pop()
2903 ret := builder.AllocateInstruction().
2904 AsWiden(v1, ssa.VecLaneI32x4, vecOp == wasm.OpcodeVecI64x2ExtendLowI32x4S, true).
2905 Insert(builder).Return()
2906 state.push(ret)
2907 case wasm.OpcodeVecI64x2ExtendHighI32x4S, wasm.OpcodeVecI64x2ExtendHighI32x4U:
2908 if state.unreachable {
2909 break
2910 }
2911 v1 := state.pop()
2912 ret := builder.AllocateInstruction().
2913 AsWiden(v1, ssa.VecLaneI32x4, vecOp == wasm.OpcodeVecI64x2ExtendHighI32x4S, false).
2914 Insert(builder).Return()
2915 state.push(ret)
2916
2917 case wasm.OpcodeVecF64x2PromoteLowF32x4Zero:
2918 if state.unreachable {
2919 break
2920 }
2921 v1 := state.pop()
2922 ret := builder.AllocateInstruction().
2923 AsFvpromoteLow(v1, ssa.VecLaneF32x4).
2924 Insert(builder).Return()
2925 state.push(ret)
2926 case wasm.OpcodeVecF32x4DemoteF64x2Zero:
2927 if state.unreachable {
2928 break
2929 }
2930 v1 := state.pop()
2931 ret := builder.AllocateInstruction().
2932 AsFvdemote(v1, ssa.VecLaneF64x2).
2933 Insert(builder).Return()
2934 state.push(ret)
2935 case wasm.OpcodeVecI8x16Shl, wasm.OpcodeVecI16x8Shl, wasm.OpcodeVecI32x4Shl, wasm.OpcodeVecI64x2Shl:
2936 if state.unreachable {
2937 break
2938 }
2939 var lane ssa.VecLane
2940 switch vecOp {
2941 case wasm.OpcodeVecI8x16Shl:
2942 lane = ssa.VecLaneI8x16
2943 case wasm.OpcodeVecI16x8Shl:
2944 lane = ssa.VecLaneI16x8
2945 case wasm.OpcodeVecI32x4Shl:
2946 lane = ssa.VecLaneI32x4
2947 case wasm.OpcodeVecI64x2Shl:
2948 lane = ssa.VecLaneI64x2
2949 }
2950 v2 := state.pop()
2951 v1 := state.pop()
2952 ret := builder.AllocateInstruction().AsVIshl(v1, v2, lane).Insert(builder).Return()
2953 state.push(ret)
2954 case wasm.OpcodeVecI8x16ShrS, wasm.OpcodeVecI16x8ShrS, wasm.OpcodeVecI32x4ShrS, wasm.OpcodeVecI64x2ShrS:
2955 if state.unreachable {
2956 break
2957 }
2958 var lane ssa.VecLane
2959 switch vecOp {
2960 case wasm.OpcodeVecI8x16ShrS:
2961 lane = ssa.VecLaneI8x16
2962 case wasm.OpcodeVecI16x8ShrS:
2963 lane = ssa.VecLaneI16x8
2964 case wasm.OpcodeVecI32x4ShrS:
2965 lane = ssa.VecLaneI32x4
2966 case wasm.OpcodeVecI64x2ShrS:
2967 lane = ssa.VecLaneI64x2
2968 }
2969 v2 := state.pop()
2970 v1 := state.pop()
2971 ret := builder.AllocateInstruction().AsVSshr(v1, v2, lane).Insert(builder).Return()
2972 state.push(ret)
2973 case wasm.OpcodeVecI8x16ShrU, wasm.OpcodeVecI16x8ShrU, wasm.OpcodeVecI32x4ShrU, wasm.OpcodeVecI64x2ShrU:
2974 if state.unreachable {
2975 break
2976 }
2977 var lane ssa.VecLane
2978 switch vecOp {
2979 case wasm.OpcodeVecI8x16ShrU:
2980 lane = ssa.VecLaneI8x16
2981 case wasm.OpcodeVecI16x8ShrU:
2982 lane = ssa.VecLaneI16x8
2983 case wasm.OpcodeVecI32x4ShrU:
2984 lane = ssa.VecLaneI32x4
2985 case wasm.OpcodeVecI64x2ShrU:
2986 lane = ssa.VecLaneI64x2
2987 }
2988 v2 := state.pop()
2989 v1 := state.pop()
2990 ret := builder.AllocateInstruction().AsVUshr(v1, v2, lane).Insert(builder).Return()
2991 state.push(ret)
2992 case wasm.OpcodeVecI8x16ExtractLaneS, wasm.OpcodeVecI16x8ExtractLaneS:
2993 state.pc++
2994 if state.unreachable {
2995 break
2996 }
2997 var lane ssa.VecLane
2998 switch vecOp {
2999 case wasm.OpcodeVecI8x16ExtractLaneS:
3000 lane = ssa.VecLaneI8x16
3001 case wasm.OpcodeVecI16x8ExtractLaneS:
3002 lane = ssa.VecLaneI16x8
3003 }
3004 v1 := state.pop()
3005 index := c.wasmFunctionBody[state.pc]
3006 ext := builder.AllocateInstruction().AsExtractlane(v1, index, lane, true).Insert(builder).Return()
3007 state.push(ext)
3008 case wasm.OpcodeVecI8x16ExtractLaneU, wasm.OpcodeVecI16x8ExtractLaneU,
3009 wasm.OpcodeVecI32x4ExtractLane, wasm.OpcodeVecI64x2ExtractLane,
3010 wasm.OpcodeVecF32x4ExtractLane, wasm.OpcodeVecF64x2ExtractLane:
3011 state.pc++ // Skip the immediate value.
3012 if state.unreachable {
3013 break
3014 }
3015 var lane ssa.VecLane
3016 switch vecOp {
3017 case wasm.OpcodeVecI8x16ExtractLaneU:
3018 lane = ssa.VecLaneI8x16
3019 case wasm.OpcodeVecI16x8ExtractLaneU:
3020 lane = ssa.VecLaneI16x8
3021 case wasm.OpcodeVecI32x4ExtractLane:
3022 lane = ssa.VecLaneI32x4
3023 case wasm.OpcodeVecI64x2ExtractLane:
3024 lane = ssa.VecLaneI64x2
3025 case wasm.OpcodeVecF32x4ExtractLane:
3026 lane = ssa.VecLaneF32x4
3027 case wasm.OpcodeVecF64x2ExtractLane:
3028 lane = ssa.VecLaneF64x2
3029 }
3030 v1 := state.pop()
3031 index := c.wasmFunctionBody[state.pc]
3032 ext := builder.AllocateInstruction().AsExtractlane(v1, index, lane, false).Insert(builder).Return()
3033 state.push(ext)
3034 case wasm.OpcodeVecI8x16ReplaceLane, wasm.OpcodeVecI16x8ReplaceLane,
3035 wasm.OpcodeVecI32x4ReplaceLane, wasm.OpcodeVecI64x2ReplaceLane,
3036 wasm.OpcodeVecF32x4ReplaceLane, wasm.OpcodeVecF64x2ReplaceLane:
3037 state.pc++
3038 if state.unreachable {
3039 break
3040 }
3041 var lane ssa.VecLane
3042 switch vecOp {
3043 case wasm.OpcodeVecI8x16ReplaceLane:
3044 lane = ssa.VecLaneI8x16
3045 case wasm.OpcodeVecI16x8ReplaceLane:
3046 lane = ssa.VecLaneI16x8
3047 case wasm.OpcodeVecI32x4ReplaceLane:
3048 lane = ssa.VecLaneI32x4
3049 case wasm.OpcodeVecI64x2ReplaceLane:
3050 lane = ssa.VecLaneI64x2
3051 case wasm.OpcodeVecF32x4ReplaceLane:
3052 lane = ssa.VecLaneF32x4
3053 case wasm.OpcodeVecF64x2ReplaceLane:
3054 lane = ssa.VecLaneF64x2
3055 }
3056 v2 := state.pop()
3057 v1 := state.pop()
3058 index := c.wasmFunctionBody[state.pc]
3059 ret := builder.AllocateInstruction().AsInsertlane(v1, v2, index, lane).Insert(builder).Return()
3060 state.push(ret)
3061 case wasm.OpcodeVecV128i8x16Shuffle:
3062 state.pc++
3063 laneIndexes := c.wasmFunctionBody[state.pc : state.pc+16]
3064 state.pc += 15
3065 if state.unreachable {
3066 break
3067 }
3068 v2 := state.pop()
3069 v1 := state.pop()
3070 ret := builder.AllocateInstruction().AsShuffle(v1, v2, laneIndexes).Insert(builder).Return()
3071 state.push(ret)
3072
3073 case wasm.OpcodeVecI8x16Swizzle:
3074 if state.unreachable {
3075 break
3076 }
3077 v2 := state.pop()
3078 v1 := state.pop()
3079 ret := builder.AllocateInstruction().AsSwizzle(v1, v2, ssa.VecLaneI8x16).Insert(builder).Return()
3080 state.push(ret)
3081
3082 case wasm.OpcodeVecI8x16Splat,
3083 wasm.OpcodeVecI16x8Splat,
3084 wasm.OpcodeVecI32x4Splat,
3085 wasm.OpcodeVecI64x2Splat,
3086 wasm.OpcodeVecF32x4Splat,
3087 wasm.OpcodeVecF64x2Splat:
3088 if state.unreachable {
3089 break
3090 }
3091 var lane ssa.VecLane
3092 switch vecOp {
3093 case wasm.OpcodeVecI8x16Splat:
3094 lane = ssa.VecLaneI8x16
3095 case wasm.OpcodeVecI16x8Splat:
3096 lane = ssa.VecLaneI16x8
3097 case wasm.OpcodeVecI32x4Splat:
3098 lane = ssa.VecLaneI32x4
3099 case wasm.OpcodeVecI64x2Splat:
3100 lane = ssa.VecLaneI64x2
3101 case wasm.OpcodeVecF32x4Splat:
3102 lane = ssa.VecLaneF32x4
3103 case wasm.OpcodeVecF64x2Splat:
3104 lane = ssa.VecLaneF64x2
3105 }
3106 v1 := state.pop()
3107 ret := builder.AllocateInstruction().AsSplat(v1, lane).Insert(builder).Return()
3108 state.push(ret)
3109
3110 default:
3111 panic("TODO: unsupported vector instruction: " + wasm.VectorInstructionName(vecOp))
3112 }
3113 case wasm.OpcodeAtomicPrefix:
3114 state.pc++
3115 atomicOp := c.wasmFunctionBody[state.pc]
3116 switch atomicOp {
3117 case wasm.OpcodeAtomicMemoryWait32, wasm.OpcodeAtomicMemoryWait64:
3118 _, offset := c.readMemArg()
3119 if state.unreachable {
3120 break
3121 }
3122
3123 c.storeCallerModuleContext()
3124
3125 var opSize uint64
3126 var trampoline wazevoapi.Offset
3127 var sig *ssa.Signature
3128 switch atomicOp {
3129 case wasm.OpcodeAtomicMemoryWait32:
3130 opSize = 4
3131 trampoline = wazevoapi.ExecutionContextOffsetMemoryWait32TrampolineAddress
3132 sig = &c.memoryWait32Sig
3133 case wasm.OpcodeAtomicMemoryWait64:
3134 opSize = 8
3135 trampoline = wazevoapi.ExecutionContextOffsetMemoryWait64TrampolineAddress
3136 sig = &c.memoryWait64Sig
3137 }
3138
3139 timeout := state.pop()
3140 exp := state.pop()
3141 baseAddr := state.pop()
3142 addr := c.atomicMemOpSetup(baseAddr, uint64(offset), opSize)
3143
3144 memoryWaitPtr := builder.AllocateInstruction().
3145 AsLoad(c.execCtxPtrValue,
3146 trampoline.U32(),
3147 ssa.TypeI64,
3148 ).Insert(builder).Return()
3149
3150 args := c.allocateVarLengthValues(4, c.execCtxPtrValue, timeout, exp, addr)
3151 memoryWaitRet := builder.AllocateInstruction().
3152 AsCallIndirect(memoryWaitPtr, sig, args).
3153 Insert(builder).Return()
3154 state.push(memoryWaitRet)
3155 case wasm.OpcodeAtomicMemoryNotify:
3156 _, offset := c.readMemArg()
3157 if state.unreachable {
3158 break
3159 }
3160
3161 c.storeCallerModuleContext()
3162 count := state.pop()
3163 baseAddr := state.pop()
3164 addr := c.atomicMemOpSetup(baseAddr, uint64(offset), 4)
3165
3166 memoryNotifyPtr := builder.AllocateInstruction().
3167 AsLoad(c.execCtxPtrValue,
3168 wazevoapi.ExecutionContextOffsetMemoryNotifyTrampolineAddress.U32(),
3169 ssa.TypeI64,
3170 ).Insert(builder).Return()
3171 args := c.allocateVarLengthValues(3, c.execCtxPtrValue, count, addr)
3172 memoryNotifyRet := builder.AllocateInstruction().
3173 AsCallIndirect(memoryNotifyPtr, &c.memoryNotifySig, args).
3174 Insert(builder).Return()
3175 state.push(memoryNotifyRet)
3176 case wasm.OpcodeAtomicI32Load, wasm.OpcodeAtomicI64Load, wasm.OpcodeAtomicI32Load8U, wasm.OpcodeAtomicI32Load16U, wasm.OpcodeAtomicI64Load8U, wasm.OpcodeAtomicI64Load16U, wasm.OpcodeAtomicI64Load32U:
3177 _, offset := c.readMemArg()
3178 if state.unreachable {
3179 break
3180 }
3181
3182 baseAddr := state.pop()
3183
3184 var size uint64
3185 switch atomicOp {
3186 case wasm.OpcodeAtomicI64Load:
3187 size = 8
3188 case wasm.OpcodeAtomicI32Load, wasm.OpcodeAtomicI64Load32U:
3189 size = 4
3190 case wasm.OpcodeAtomicI32Load16U, wasm.OpcodeAtomicI64Load16U:
3191 size = 2
3192 case wasm.OpcodeAtomicI32Load8U, wasm.OpcodeAtomicI64Load8U:
3193 size = 1
3194 }
3195
3196 var typ ssa.Type
3197 switch atomicOp {
3198 case wasm.OpcodeAtomicI64Load, wasm.OpcodeAtomicI64Load32U, wasm.OpcodeAtomicI64Load16U, wasm.OpcodeAtomicI64Load8U:
3199 typ = ssa.TypeI64
3200 case wasm.OpcodeAtomicI32Load, wasm.OpcodeAtomicI32Load16U, wasm.OpcodeAtomicI32Load8U:
3201 typ = ssa.TypeI32
3202 }
3203
3204 addr := c.atomicMemOpSetup(baseAddr, uint64(offset), size)
3205 res := builder.AllocateInstruction().AsAtomicLoad(addr, size, typ).Insert(builder).Return()
3206 state.push(res)
3207 case wasm.OpcodeAtomicI32Store, wasm.OpcodeAtomicI64Store, wasm.OpcodeAtomicI32Store8, wasm.OpcodeAtomicI32Store16, wasm.OpcodeAtomicI64Store8, wasm.OpcodeAtomicI64Store16, wasm.OpcodeAtomicI64Store32:
3208 _, offset := c.readMemArg()
3209 if state.unreachable {
3210 break
3211 }
3212
3213 val := state.pop()
3214 baseAddr := state.pop()
3215
3216 var size uint64
3217 switch atomicOp {
3218 case wasm.OpcodeAtomicI64Store:
3219 size = 8
3220 case wasm.OpcodeAtomicI32Store, wasm.OpcodeAtomicI64Store32:
3221 size = 4
3222 case wasm.OpcodeAtomicI32Store16, wasm.OpcodeAtomicI64Store16:
3223 size = 2
3224 case wasm.OpcodeAtomicI32Store8, wasm.OpcodeAtomicI64Store8:
3225 size = 1
3226 }
3227
3228 addr := c.atomicMemOpSetup(baseAddr, uint64(offset), size)
3229 builder.AllocateInstruction().AsAtomicStore(addr, val, size).Insert(builder)
3230 case wasm.OpcodeAtomicI32RmwAdd, wasm.OpcodeAtomicI64RmwAdd, wasm.OpcodeAtomicI32Rmw8AddU, wasm.OpcodeAtomicI32Rmw16AddU, wasm.OpcodeAtomicI64Rmw8AddU, wasm.OpcodeAtomicI64Rmw16AddU, wasm.OpcodeAtomicI64Rmw32AddU,
3231 wasm.OpcodeAtomicI32RmwSub, wasm.OpcodeAtomicI64RmwSub, wasm.OpcodeAtomicI32Rmw8SubU, wasm.OpcodeAtomicI32Rmw16SubU, wasm.OpcodeAtomicI64Rmw8SubU, wasm.OpcodeAtomicI64Rmw16SubU, wasm.OpcodeAtomicI64Rmw32SubU,
3232 wasm.OpcodeAtomicI32RmwAnd, wasm.OpcodeAtomicI64RmwAnd, wasm.OpcodeAtomicI32Rmw8AndU, wasm.OpcodeAtomicI32Rmw16AndU, wasm.OpcodeAtomicI64Rmw8AndU, wasm.OpcodeAtomicI64Rmw16AndU, wasm.OpcodeAtomicI64Rmw32AndU,
3233 wasm.OpcodeAtomicI32RmwOr, wasm.OpcodeAtomicI64RmwOr, wasm.OpcodeAtomicI32Rmw8OrU, wasm.OpcodeAtomicI32Rmw16OrU, wasm.OpcodeAtomicI64Rmw8OrU, wasm.OpcodeAtomicI64Rmw16OrU, wasm.OpcodeAtomicI64Rmw32OrU,
3234 wasm.OpcodeAtomicI32RmwXor, wasm.OpcodeAtomicI64RmwXor, wasm.OpcodeAtomicI32Rmw8XorU, wasm.OpcodeAtomicI32Rmw16XorU, wasm.OpcodeAtomicI64Rmw8XorU, wasm.OpcodeAtomicI64Rmw16XorU, wasm.OpcodeAtomicI64Rmw32XorU,
3235 wasm.OpcodeAtomicI32RmwXchg, wasm.OpcodeAtomicI64RmwXchg, wasm.OpcodeAtomicI32Rmw8XchgU, wasm.OpcodeAtomicI32Rmw16XchgU, wasm.OpcodeAtomicI64Rmw8XchgU, wasm.OpcodeAtomicI64Rmw16XchgU, wasm.OpcodeAtomicI64Rmw32XchgU:
3236 _, offset := c.readMemArg()
3237 if state.unreachable {
3238 break
3239 }
3240
3241 val := state.pop()
3242 baseAddr := state.pop()
3243
3244 var rmwOp ssa.AtomicRmwOp
3245 var size uint64
3246 switch atomicOp {
3247 case wasm.OpcodeAtomicI32RmwAdd, wasm.OpcodeAtomicI64RmwAdd, wasm.OpcodeAtomicI32Rmw8AddU, wasm.OpcodeAtomicI32Rmw16AddU, wasm.OpcodeAtomicI64Rmw8AddU, wasm.OpcodeAtomicI64Rmw16AddU, wasm.OpcodeAtomicI64Rmw32AddU:
3248 rmwOp = ssa.AtomicRmwOpAdd
3249 switch atomicOp {
3250 case wasm.OpcodeAtomicI64RmwAdd:
3251 size = 8
3252 case wasm.OpcodeAtomicI32RmwAdd, wasm.OpcodeAtomicI64Rmw32AddU:
3253 size = 4
3254 case wasm.OpcodeAtomicI32Rmw16AddU, wasm.OpcodeAtomicI64Rmw16AddU:
3255 size = 2
3256 case wasm.OpcodeAtomicI32Rmw8AddU, wasm.OpcodeAtomicI64Rmw8AddU:
3257 size = 1
3258 }
3259 case wasm.OpcodeAtomicI32RmwSub, wasm.OpcodeAtomicI64RmwSub, wasm.OpcodeAtomicI32Rmw8SubU, wasm.OpcodeAtomicI32Rmw16SubU, wasm.OpcodeAtomicI64Rmw8SubU, wasm.OpcodeAtomicI64Rmw16SubU, wasm.OpcodeAtomicI64Rmw32SubU:
3260 rmwOp = ssa.AtomicRmwOpSub
3261 switch atomicOp {
3262 case wasm.OpcodeAtomicI64RmwSub:
3263 size = 8
3264 case wasm.OpcodeAtomicI32RmwSub, wasm.OpcodeAtomicI64Rmw32SubU:
3265 size = 4
3266 case wasm.OpcodeAtomicI32Rmw16SubU, wasm.OpcodeAtomicI64Rmw16SubU:
3267 size = 2
3268 case wasm.OpcodeAtomicI32Rmw8SubU, wasm.OpcodeAtomicI64Rmw8SubU:
3269 size = 1
3270 }
3271 case wasm.OpcodeAtomicI32RmwAnd, wasm.OpcodeAtomicI64RmwAnd, wasm.OpcodeAtomicI32Rmw8AndU, wasm.OpcodeAtomicI32Rmw16AndU, wasm.OpcodeAtomicI64Rmw8AndU, wasm.OpcodeAtomicI64Rmw16AndU, wasm.OpcodeAtomicI64Rmw32AndU:
3272 rmwOp = ssa.AtomicRmwOpAnd
3273 switch atomicOp {
3274 case wasm.OpcodeAtomicI64RmwAnd:
3275 size = 8
3276 case wasm.OpcodeAtomicI32RmwAnd, wasm.OpcodeAtomicI64Rmw32AndU:
3277 size = 4
3278 case wasm.OpcodeAtomicI32Rmw16AndU, wasm.OpcodeAtomicI64Rmw16AndU:
3279 size = 2
3280 case wasm.OpcodeAtomicI32Rmw8AndU, wasm.OpcodeAtomicI64Rmw8AndU:
3281 size = 1
3282 }
3283 case wasm.OpcodeAtomicI32RmwOr, wasm.OpcodeAtomicI64RmwOr, wasm.OpcodeAtomicI32Rmw8OrU, wasm.OpcodeAtomicI32Rmw16OrU, wasm.OpcodeAtomicI64Rmw8OrU, wasm.OpcodeAtomicI64Rmw16OrU, wasm.OpcodeAtomicI64Rmw32OrU:
3284 rmwOp = ssa.AtomicRmwOpOr
3285 switch atomicOp {
3286 case wasm.OpcodeAtomicI64RmwOr:
3287 size = 8
3288 case wasm.OpcodeAtomicI32RmwOr, wasm.OpcodeAtomicI64Rmw32OrU:
3289 size = 4
3290 case wasm.OpcodeAtomicI32Rmw16OrU, wasm.OpcodeAtomicI64Rmw16OrU:
3291 size = 2
3292 case wasm.OpcodeAtomicI32Rmw8OrU, wasm.OpcodeAtomicI64Rmw8OrU:
3293 size = 1
3294 }
3295 case wasm.OpcodeAtomicI32RmwXor, wasm.OpcodeAtomicI64RmwXor, wasm.OpcodeAtomicI32Rmw8XorU, wasm.OpcodeAtomicI32Rmw16XorU, wasm.OpcodeAtomicI64Rmw8XorU, wasm.OpcodeAtomicI64Rmw16XorU, wasm.OpcodeAtomicI64Rmw32XorU:
3296 rmwOp = ssa.AtomicRmwOpXor
3297 switch atomicOp {
3298 case wasm.OpcodeAtomicI64RmwXor:
3299 size = 8
3300 case wasm.OpcodeAtomicI32RmwXor, wasm.OpcodeAtomicI64Rmw32XorU:
3301 size = 4
3302 case wasm.OpcodeAtomicI32Rmw16XorU, wasm.OpcodeAtomicI64Rmw16XorU:
3303 size = 2
3304 case wasm.OpcodeAtomicI32Rmw8XorU, wasm.OpcodeAtomicI64Rmw8XorU:
3305 size = 1
3306 }
3307 case wasm.OpcodeAtomicI32RmwXchg, wasm.OpcodeAtomicI64RmwXchg, wasm.OpcodeAtomicI32Rmw8XchgU, wasm.OpcodeAtomicI32Rmw16XchgU, wasm.OpcodeAtomicI64Rmw8XchgU, wasm.OpcodeAtomicI64Rmw16XchgU, wasm.OpcodeAtomicI64Rmw32XchgU:
3308 rmwOp = ssa.AtomicRmwOpXchg
3309 switch atomicOp {
3310 case wasm.OpcodeAtomicI64RmwXchg:
3311 size = 8
3312 case wasm.OpcodeAtomicI32RmwXchg, wasm.OpcodeAtomicI64Rmw32XchgU:
3313 size = 4
3314 case wasm.OpcodeAtomicI32Rmw16XchgU, wasm.OpcodeAtomicI64Rmw16XchgU:
3315 size = 2
3316 case wasm.OpcodeAtomicI32Rmw8XchgU, wasm.OpcodeAtomicI64Rmw8XchgU:
3317 size = 1
3318 }
3319 }
3320
3321 addr := c.atomicMemOpSetup(baseAddr, uint64(offset), size)
3322 res := builder.AllocateInstruction().AsAtomicRmw(rmwOp, addr, val, size).Insert(builder).Return()
3323 state.push(res)
3324 case wasm.OpcodeAtomicI32RmwCmpxchg, wasm.OpcodeAtomicI64RmwCmpxchg, wasm.OpcodeAtomicI32Rmw8CmpxchgU, wasm.OpcodeAtomicI32Rmw16CmpxchgU, wasm.OpcodeAtomicI64Rmw8CmpxchgU, wasm.OpcodeAtomicI64Rmw16CmpxchgU, wasm.OpcodeAtomicI64Rmw32CmpxchgU:
3325 _, offset := c.readMemArg()
3326 if state.unreachable {
3327 break
3328 }
3329
3330 repl := state.pop()
3331 exp := state.pop()
3332 baseAddr := state.pop()
3333
3334 var size uint64
3335 switch atomicOp {
3336 case wasm.OpcodeAtomicI64RmwCmpxchg:
3337 size = 8
3338 case wasm.OpcodeAtomicI32RmwCmpxchg, wasm.OpcodeAtomicI64Rmw32CmpxchgU:
3339 size = 4
3340 case wasm.OpcodeAtomicI32Rmw16CmpxchgU, wasm.OpcodeAtomicI64Rmw16CmpxchgU:
3341 size = 2
3342 case wasm.OpcodeAtomicI32Rmw8CmpxchgU, wasm.OpcodeAtomicI64Rmw8CmpxchgU:
3343 size = 1
3344 }
3345 addr := c.atomicMemOpSetup(baseAddr, uint64(offset), size)
3346 res := builder.AllocateInstruction().AsAtomicCas(addr, exp, repl, size).Insert(builder).Return()
3347 state.push(res)
3348 case wasm.OpcodeAtomicFence:
3349 order := c.readByte()
3350 if state.unreachable {
3351 break
3352 }
3353 if c.needMemory {
3354 builder.AllocateInstruction().AsFence(order).Insert(builder)
3355 }
3356 default:
3357 panic("TODO: unsupported atomic instruction: " + wasm.AtomicInstructionName(atomicOp))
3358 }
3359 case wasm.OpcodeRefFunc:
3360 funcIndex := c.readI32u()
3361 if state.unreachable {
3362 break
3363 }
3364
3365 c.storeCallerModuleContext()
3366
3367 funcIndexVal := builder.AllocateInstruction().AsIconst32(funcIndex).Insert(builder).Return()
3368
3369 refFuncPtr := builder.AllocateInstruction().
3370 AsLoad(c.execCtxPtrValue,
3371 wazevoapi.ExecutionContextOffsetRefFuncTrampolineAddress.U32(),
3372 ssa.TypeI64,
3373 ).Insert(builder).Return()
3374
3375 args := c.allocateVarLengthValues(2, c.execCtxPtrValue, funcIndexVal)
3376 refFuncRet := builder.
3377 AllocateInstruction().
3378 AsCallIndirect(refFuncPtr, &c.refFuncSig, args).
3379 Insert(builder).Return()
3380 state.push(refFuncRet)
3381
3382 case wasm.OpcodeRefNull:
3383 switch reftype := c.wasmFunctionBody[c.loweringState.pc+1]; wasm.ValueType(reftype) {
3384 case wasm.ValueTypeFuncref, wasm.ValueTypeExternref, wasm.ValueTypeExnref:
3385 c.loweringState.pc++
3386 default:
3387 c.readI32u()
3388 }
3389 if state.unreachable {
3390 break
3391 }
3392 ret := builder.AllocateInstruction().AsIconst64(0).Insert(builder).Return()
3393 state.push(ret)
3394 case wasm.OpcodeRefIsNull:
3395 if state.unreachable {
3396 break
3397 }
3398 r := state.pop()
3399 zero := builder.AllocateInstruction().AsIconst64(0).Insert(builder)
3400 icmp := builder.AllocateInstruction().
3401 AsIcmp(r, zero.Return(), ssa.IntegerCmpCondEqual).
3402 Insert(builder).
3403 Return()
3404 state.push(icmp)
3405 case wasm.OpcodeTableSet:
3406 tableIndex := c.readI32u()
3407 if state.unreachable {
3408 break
3409 }
3410 r := state.pop()
3411 targetOffsetInTable := state.pop()
3412
3413 elementAddr := c.lowerAccessTableWithBoundsCheck(tableIndex, targetOffsetInTable)
3414 builder.AllocateInstruction().AsStore(ssa.OpcodeStore, r, elementAddr, 0).Insert(builder)
3415
3416 case wasm.OpcodeTableGet:
3417 tableIndex := c.readI32u()
3418 if state.unreachable {
3419 break
3420 }
3421 targetOffsetInTable := state.pop()
3422 elementAddr := c.lowerAccessTableWithBoundsCheck(tableIndex, targetOffsetInTable)
3423 loaded := builder.AllocateInstruction().AsLoad(elementAddr, 0, ssa.TypeI64).Insert(builder).Return()
3424 state.push(loaded)
3425
3426 case wasm.OpcodeTailCallReturnCallIndirect:
3427 typeIndex := c.readI32u()
3428 tableIndex := c.readI32u()
3429 if state.unreachable {
3430 break
3431 }
3432 // Per spec, return_call leaves the current frame, so all enclosing
3433 // try_table handlers must be popped before the tail call.
3434 c.emitTryTableLeaves(len(c.state().controlFrames))
3435 _, _ = typeIndex, tableIndex
3436 c.lowerTailCallReturnCallIndirect(typeIndex, tableIndex)
3437 state.unreachable = true
3438
3439 case wasm.OpcodeTailCallReturnCall:
3440 fnIndex := c.readI32u()
3441 if state.unreachable {
3442 break
3443 }
3444 // Per spec, return_call leaves the current frame, so all enclosing
3445 // try_table handlers must be popped before the tail call.
3446 c.emitTryTableLeaves(len(c.state().controlFrames))
3447 c.lowerTailCallReturnCall(fnIndex)
3448 state.unreachable = true
3449
3450 case wasm.OpcodeThrow:
3451 tagIndex := c.readI32u()
3452 if state.unreachable {
3453 break
3454 }
3455 tagType := c.resolveTagType(tagIndex)
3456 // Pop the tag's param values from the stack.
3457 var throwParams []ssa.Value
3458 if tagType != nil {
3459 throwParams = make([]ssa.Value, len(tagType.Params))
3460 for i := len(tagType.Params) - 1; i >= 0; i-- {
3461 throwParams[i] = state.pop()
3462 }
3463 }
3464
3465 c.storeCallerModuleContext()
3466
3467 tagIdxVal := builder.AllocateInstruction().AsIconst64(uint64(tagIndex)).Insert(builder).Return()
3468
3469 // We need to store the throwParams in the exception and then throw it.
3470 // However, each exception might have a variable number of parameters,
3471 // so we let Go allocate the reference on the heap.
3472 // The Go side allocates the Exception object (Params sized to nParams)
3473 // and stores the pointer to the backing-array into execCtx.exceptionParamsPtr.
3474 throwAllocPtr := builder.AllocateInstruction().
3475 AsLoad(c.execCtxPtrValue,
3476 wazevoapi.ExecutionContextOffsetThrowAllocTrampolineAddress.U32(),
3477 ssa.TypeI64,
3478 ).Insert(builder).Return()
3479 throwAllocArgs := c.allocateVarLengthValues(2, c.execCtxPtrValue, tagIdxVal)
3480 exnref := builder.AllocateInstruction().
3481 AsCallIndirect(throwAllocPtr, &c.throwAllocSig, throwAllocArgs).
3482 Insert(builder).Return()
3483
3484 // Reload memory pointers invalidated by the Go call.
3485 c.reloadAfterCall()
3486
3487 // We can now store each param directly into Exception.Params using the pointer
3488 // stored into execCtx.exceptionParamsPtr.
3489 if len(throwParams) > 0 {
3490 paramsPtr := builder.AllocateInstruction().
3491 AsLoad(c.execCtxPtrValue,
3492 wazevoapi.ExecutionContextOffsetExceptionParamsPtr.U32(),
3493 ssa.TypeI64,
3494 ).Insert(builder).Return()
3495 for i, v := range throwParams {
3496 switch v.Type() {
3497 case ssa.TypeF32:
3498 v = builder.AllocateInstruction().AsBitcast(v, ssa.TypeI32).Insert(builder).Return()
3499 case ssa.TypeF64:
3500 v = builder.AllocateInstruction().AsBitcast(v, ssa.TypeI64).Insert(builder).Return()
3501 }
3502 builder.AllocateInstruction().
3503 AsStore(ssa.OpcodeStore, v, paramsPtr, uint32(i)*8).
3504 Insert(builder)
3505 }
3506 }
3507
3508 // We return again control to Go to search and dispatch to a matching catch clause.
3509 c.emitThrow(exnref)
3510 state.unreachable = true
3511
3512 case wasm.OpcodeThrowRef:
3513 if state.unreachable {
3514 break
3515 }
3516 exnref := state.pop()
3517 // Check for null exnref.
3518 zero := builder.AllocateInstruction().AsIconst64(0).Insert(builder).Return()
3519 isNull := builder.AllocateInstruction()
3520 isNull.AsIcmp(exnref, zero, ssa.IntegerCmpCondEqual)
3521 builder.InsertInstruction(isNull)
3522 exitIfNull := builder.AllocateInstruction()
3523 exitIfNull.AsExitIfTrueWithCode(c.execCtxPtrValue, isNull.Return(), wazevoapi.ExitCodeNullReference)
3524 builder.InsertInstruction(exitIfNull)
3525
3526 c.storeCallerModuleContext()
3527
3528 c.emitThrow(exnref)
3529 state.unreachable = true
3530
3531 case wasm.OpcodeTryTable:
3532 bt := c.readBlockType()
3533
3534 if state.unreachable {
3535 state.unreachableDepth++
3536 // Still need to skip the catch clause bytes in the unreachable case.
3537 c.skipTryTableCatchClauses()
3538 break
3539 }
3540
3541 // Parse catch clauses.
3542 c.loweringState.pc++
3543 catchCount, catchNum, _ := leb128.LoadUint32(c.wasmFunctionBody[c.loweringState.pc:])
3544 c.loweringState.pc += int(catchNum) - 1
3545
3546 var catchClauses []catchClause
3547 for i := uint32(0); i < catchCount; i++ {
3548 c.loweringState.pc++
3549 kind := c.wasmFunctionBody[c.loweringState.pc]
3550 var tagIdx uint32
3551 switch kind {
3552 case wasm.CatchKindCatch, wasm.CatchKindCatchRef:
3553 c.loweringState.pc++
3554 var n uint64
3555 tagIdx, n, _ = leb128.LoadUint32(c.wasmFunctionBody[c.loweringState.pc:])
3556 c.loweringState.pc += int(n) - 1
3557 case wasm.CatchKindCatchAll, wasm.CatchKindCatchAllRef:
3558 // No tagIdx for catch_all variants.
3559 }
3560 c.loweringState.pc++
3561 labelIdx, n, _ := leb128.LoadUint32(c.wasmFunctionBody[c.loweringState.pc:])
3562 c.loweringState.pc += int(n) - 1
3563 catchClauses = append(catchClauses, catchClause{kind: kind, tagIndex: tagIdx, labelIdx: labelIdx})
3564 }
3565
3566 // Register catch clauses in the table and get the try_table ID.
3567 var clauseInstances []wazevoapi.CatchClauseInstance
3568 for _, cc := range catchClauses {
3569 clauseInstances = append(clauseInstances, wazevoapi.CatchClauseInstance{
3570 Kind: cc.kind,
3571 TagIndex: cc.tagIndex,
3572 })
3573 }
3574 tryTableID := c.catchClauseTable.Append(clauseInstances)
3575
3576 // Allocate the following block (after try_table end) and body block.
3577 followingBlk := builder.AllocateBasicBlock()
3578 c.addBlockParamsFromWasmTypes(bt.Results, followingBlk)
3579 bodyBlk := builder.AllocateBasicBlock()
3580
3581 if len(catchClauses) > 0 {
3582 // Store the caller module context so the dispatch loop can find the module.
3583 c.storeCallerModuleContext()
3584
3585 // For each catch clause, create a handler block that loads exception
3586 // params and jumps to the wasm target label.
3587 // NOTE: catch clause label indices do NOT include the try_table itself
3588 // (the try_table is pushed onto the control stack after the catch clauses
3589 // are processed, per the spec). So we resolve labels BEFORE pushing.
3590 varPool := builder.VarLengthPool()
3591 targets := varPool.Allocate(len(catchClauses) + 1) // +1 for bodyBlk
3592
3593 currentBlk := builder.CurrentBlock()
3594 for _, cc := range catchClauses {
3595 handlerBlk := builder.AllocateBasicBlock()
3596 builder.SetCurrentBlock(handlerBlk)
3597 c.reloadAfterCall()
3598
3599 // Resolve the wasm target label.
3600 targetBlk, _ := state.brTargetArgNumFor(cc.labelIdx)
3601
3602 // Load exception params and jump to wasm target.
3603 var brArgs []ssa.Value
3604 switch cc.kind {
3605 case wasm.CatchKindCatch:
3606 if tagType := c.resolveTagType(cc.tagIndex); tagType != nil {
3607 brArgs = c.loadExceptionParams(tagType)
3608 }
3609 case wasm.CatchKindCatchRef:
3610 if tagType := c.resolveTagType(cc.tagIndex); tagType != nil {
3611 brArgs = c.loadExceptionParams(tagType)
3612 }
3613 brArgs = append(brArgs, c.loadExnRef())
3614 case wasm.CatchKindCatchAll:
3615 // No values.
3616 case wasm.CatchKindCatchAllRef:
3617 brArgs = append(brArgs, c.loadExnRef())
3618 }
3619
3620 jmpArgs := c.allocateVarLengthValues(len(brArgs), brArgs...)
3621 c.insertJumpToBlock(jmpArgs, targetBlk)
3622
3623 targets = targets.Append(varPool, ssa.Value(handlerBlk.ID()))
3624 }
3625 // Last target is the body block (default for clauseIdx == -1 / out of range).
3626 targets = targets.Append(varPool, ssa.Value(bodyBlk.ID()))
3627
3628 // Back to the original block: call the try_table enter trampoline,
3629 // then dispatch on the caught clause index.
3630 builder.SetCurrentBlock(currentBlk)
3631 encodedExitCode := uint64(wazevoapi.ExitCodeTryTableEnter | wazevoapi.ExitCode(tryTableID<<8))
3632
3633 // Load trampoline address from execCtx.
3634 enterPtr := builder.AllocateInstruction().
3635 AsLoad(c.execCtxPtrValue,
3636 wazevoapi.ExecutionContextOffsetTryTableEnterTrampolineAddress.U32(),
3637 ssa.TypeI64,
3638 ).Insert(builder).Return()
3639
3640 // Call the trampoline: (execCtx, encodedExitCode) -> ().
3641 exitCodeVal := builder.AllocateInstruction().AsIconst64(encodedExitCode).Insert(builder).Return()
3642 args := c.allocateVarLengthValues(2, c.execCtxPtrValue, exitCodeVal)
3643 builder.AllocateInstruction().
3644 AsCallIndirect(enterPtr, &c.tryTableEnterSig, args).
3645 Insert(builder)
3646
3647 // Load the caught clause index written by the dispatch loop.
3648 clauseIdx := builder.AllocateInstruction().
3649 AsLoad(c.execCtxPtrValue,
3650 wazevoapi.ExecutionContextOffsetCaughtExceptionClauseIdx.U32(),
3651 ssa.TypeI64,
3652 ).Insert(builder).Return()
3653
3654 // Dispatch to handler blocks or body block via br_table.
3655 brTable := builder.AllocateInstruction()
3656 brTable.AsBrTable(clauseIdx, targets)
3657 builder.InsertInstruction(brTable)
3658
3659 // Seal handler blocks after BrTable is inserted (so predecessors are registered).
3660 for _, targetID := range targets.View() {
3661 blk := builder.BasicBlock(ssa.BasicBlockID(targetID))
3662 if !blk.Sealed() {
3663 builder.Seal(blk)
3664 }
3665 }
3666 } else {
3667 // No catch clauses — try_table acts as a plain block.
3668 // Jump directly to body without entering exception handling.
3669 c.insertJumpToBlock(ssa.ValuesNil, bodyBlk)
3670 }
3671
3672 if !bodyBlk.Sealed() {
3673 builder.Seal(bodyBlk)
3674 }
3675 builder.SetCurrentBlock(bodyBlk)
3676 if len(catchClauses) > 0 {
3677 // Body block is entered after the trampoline call, so we need to reload.
3678 c.reloadAfterCall()
3679 }
3680
3681 // Push the try_table control frame AFTER resolving catch labels.
3682 state.ctrlPush(controlFrame{
3683 kind: controlFrameKindTryTable,
3684 originalStackLenWithoutParam: len(state.values) - len(bt.Params),
3685 followingBlock: followingBlk,
3686 blockType: bt,
3687 })
3688
3689 case wasm.OpcodeRefAsNonNull:
3690 if state.unreachable {
3691 break
3692 }
3693 r := state.pop()
3694 zero := builder.AllocateInstruction().AsIconst64(0).Insert(builder)
3695 checkNull := builder.AllocateInstruction().
3696 AsIcmp(r, zero.Return(), ssa.IntegerCmpCondEqual).
3697 Insert(builder).Return()
3698 exitIfNull := builder.AllocateInstruction()
3699 exitIfNull.AsExitIfTrueWithCode(c.execCtxPtrValue, checkNull, wazevoapi.ExitCodeNullReference)
3700 builder.InsertInstruction(exitIfNull)
3701 state.push(r)
3702
3703 case wasm.OpcodeBrOnNull:
3704 labelIndex := c.readI32u()
3705 if state.unreachable {
3706 break
3707 }
3708
3709 r := state.pop()
3710 zero := builder.AllocateInstruction().AsIconst64(0).Insert(builder)
3711 isNull := builder.AllocateInstruction().
3712 AsIcmp(r, zero.Return(), ssa.IntegerCmpCondEqual).
3713 Insert(builder).Return()
3714
3715 targetBlk, argNum := state.brTargetArgNumFor(labelIndex)
3716 args := c.nPeekDup(argNum)
3717 var sealTargetBlk bool
3718
3719 if c.branchExitsTryTable(int(labelIndex)) {
3720 current := builder.CurrentBlock()
3721 trampolineBlk := builder.AllocateBasicBlock()
3722 builder.SetCurrentBlock(trampolineBlk)
3723 c.emitTryTableLeaves(int(labelIndex))
3724 c.insertJumpToBlock(args, targetBlk)
3725 builder.SetCurrentBlock(current)
3726 targetBlk = trampolineBlk
3727 sealTargetBlk = true
3728 args = ssa.ValuesNil
3729 }
3730
3731 if c.needListener && targetBlk.ReturnBlock() {
3732 current := builder.CurrentBlock()
3733 targetBlk = builder.AllocateBasicBlock()
3734 builder.SetCurrentBlock(targetBlk)
3735 sealTargetBlk = true
3736 c.callListenerAfter()
3737 instr := builder.AllocateInstruction()
3738 instr.AsReturn(args)
3739 builder.InsertInstruction(instr)
3740 args = ssa.ValuesNil
3741 builder.SetCurrentBlock(current)
3742 }
3743
3744 brnz := builder.AllocateInstruction()
3745 brnz.AsBrnz(isNull, args, targetBlk)
3746 builder.InsertInstruction(brnz)
3747
3748 if sealTargetBlk {
3749 builder.Seal(targetBlk)
3750 }
3751
3752 // Fall-through: ref is non-null, push it back.
3753 elseBlk := builder.AllocateBasicBlock()
3754 c.insertJumpToBlock(ssa.ValuesNil, elseBlk)
3755 builder.Seal(elseBlk)
3756 builder.SetCurrentBlock(elseBlk)
3757 state.push(r)
3758
3759 case wasm.OpcodeBrOnNonNull:
3760 labelIndex := c.readI32u()
3761 if state.unreachable {
3762 break
3763 }
3764
3765 r := state.pop()
3766 zero := builder.AllocateInstruction().AsIconst64(0).Insert(builder)
3767 isNonNull := builder.AllocateInstruction().
3768 AsIcmp(r, zero.Return(), ssa.IntegerCmpCondNotEqual).
3769 Insert(builder).Return()
3770
3771 // When non-null, branch to label with args + the non-null ref.
3772 targetBlk, argNum := state.brTargetArgNumFor(labelIndex)
3773 // The branch delivers argNum-1 values from the stack plus the ref.
3774 // The ref is the last value delivered to the label target.
3775 args := c.nPeekDup(argNum - 1)
3776 args = args.Append(builder.VarLengthPool(), r)
3777 var sealTargetBlk bool
3778
3779 if c.branchExitsTryTable(int(labelIndex)) {
3780 current := builder.CurrentBlock()
3781 trampolineBlk := builder.AllocateBasicBlock()
3782 builder.SetCurrentBlock(trampolineBlk)
3783 c.emitTryTableLeaves(int(labelIndex))
3784 c.insertJumpToBlock(args, targetBlk)
3785 builder.SetCurrentBlock(current)
3786 targetBlk = trampolineBlk
3787 sealTargetBlk = true
3788 args = ssa.ValuesNil
3789 }
3790
3791 if c.needListener && targetBlk.ReturnBlock() {
3792 current := builder.CurrentBlock()
3793 targetBlk = builder.AllocateBasicBlock()
3794 builder.SetCurrentBlock(targetBlk)
3795 sealTargetBlk = true
3796 c.callListenerAfter()
3797 instr := builder.AllocateInstruction()
3798 instr.AsReturn(args)
3799 builder.InsertInstruction(instr)
3800 args = ssa.ValuesNil
3801 builder.SetCurrentBlock(current)
3802 }
3803
3804 brnz := builder.AllocateInstruction()
3805 brnz.AsBrnz(isNonNull, args, targetBlk)
3806 builder.InsertInstruction(brnz)
3807
3808 if sealTargetBlk {
3809 builder.Seal(targetBlk)
3810 }
3811
3812 // Fall-through: ref is null, nothing extra pushed.
3813 elseBlk := builder.AllocateBasicBlock()
3814 c.insertJumpToBlock(ssa.ValuesNil, elseBlk)
3815 builder.Seal(elseBlk)
3816 builder.SetCurrentBlock(elseBlk)
3817
3818 case wasm.OpcodeCallRef:
3819 typeIndex := c.readI32u()
3820 if state.unreachable {
3821 break
3822 }
3823 c.lowerCallRef(typeIndex)
3824
3825 case wasm.OpcodeReturnCallRef:
3826 typeIndex := c.readI32u()
3827 if state.unreachable {
3828 break
3829 }
3830 c.emitTryTableLeaves(len(c.state().controlFrames))
3831 c.lowerTailCallReturnCallRef(typeIndex)
3832 state.unreachable = true
3833
3834 default:
3835 panic("TODO: unsupported in wazevo yet: " + wasm.InstructionName(op))
3836 }
3837
3838 if wazevoapi.FrontEndLoggingEnabled {
3839 fmt.Println("--------- Translated " + wasm.InstructionName(op) + " --------")
3840 fmt.Println("state: " + c.loweringState.String())
3841 fmt.Println(c.formatBuilder())
3842 fmt.Println("--------------------------")
3843 }
3844 c.loweringState.pc++
3845 }
3846
3847 func (c *Compiler) lowerReturn(builder ssa.Builder) {
3848 results := c.nPeekDup(c.results())
3849 instr := builder.AllocateInstruction()
3850
3851 instr.AsReturn(results)
3852 builder.InsertInstruction(instr)
3853 }
3854
3855 func (c *Compiler) lowerExtMul(v1, v2 ssa.Value, from, to ssa.VecLane, signed, low bool) ssa.Value {
3856 // TODO: The sequence `Widen; Widen; VIMul` can be substituted for a single instruction on some ISAs.
3857 builder := c.ssaBuilder
3858
3859 v1lo := builder.AllocateInstruction().AsWiden(v1, from, signed, low).Insert(builder).Return()
3860 v2lo := builder.AllocateInstruction().AsWiden(v2, from, signed, low).Insert(builder).Return()
3861
3862 return builder.AllocateInstruction().AsVImul(v1lo, v2lo, to).Insert(builder).Return()
3863 }
3864
3865 const (
3866 tableInstanceBaseAddressOffset = 0
3867 tableInstanceLenOffset = tableInstanceBaseAddressOffset + 8
3868 )
3869
3870 func (c *Compiler) lowerAccessTableWithBoundsCheck(tableIndex uint32, elementOffsetInTable ssa.Value) (elementAddress ssa.Value) {
3871 builder := c.ssaBuilder
3872
3873 // Load the table.
3874 loadTableInstancePtr := builder.AllocateInstruction()
3875 loadTableInstancePtr.AsLoad(c.moduleCtxPtrValue, c.offset.TableOffset(int(tableIndex)).U32(), ssa.TypeI64)
3876 builder.InsertInstruction(loadTableInstancePtr)
3877 tableInstancePtr := loadTableInstancePtr.Return()
3878
3879 // Load the table's length.
3880 loadTableLen := builder.AllocateInstruction()
3881 loadTableLen.AsLoad(tableInstancePtr, tableInstanceLenOffset, ssa.TypeI32)
3882 builder.InsertInstruction(loadTableLen)
3883 tableLen := loadTableLen.Return()
3884
3885 // Compare the length and the target, and trap if out of bounds.
3886 checkOOB := builder.AllocateInstruction()
3887 checkOOB.AsIcmp(elementOffsetInTable, tableLen, ssa.IntegerCmpCondUnsignedGreaterThanOrEqual)
3888 builder.InsertInstruction(checkOOB)
3889 exitIfOOB := builder.AllocateInstruction()
3890 exitIfOOB.AsExitIfTrueWithCode(c.execCtxPtrValue, checkOOB.Return(), wazevoapi.ExitCodeTableOutOfBounds)
3891 builder.InsertInstruction(exitIfOOB)
3892
3893 // Get the base address of wasm.TableInstance.References.
3894 loadTableBaseAddress := builder.AllocateInstruction()
3895 loadTableBaseAddress.AsLoad(tableInstancePtr, tableInstanceBaseAddressOffset, ssa.TypeI64)
3896 builder.InsertInstruction(loadTableBaseAddress)
3897 tableBase := loadTableBaseAddress.Return()
3898
3899 // Calculate the address of the target function. First we need to multiply targetOffsetInTable by 8 (pointer size).
3900 multiplyBy8 := builder.AllocateInstruction()
3901 three := builder.AllocateInstruction()
3902 three.AsIconst64(3)
3903 builder.InsertInstruction(three)
3904 multiplyBy8.AsIshl(elementOffsetInTable, three.Return())
3905 builder.InsertInstruction(multiplyBy8)
3906 targetOffsetInTableMultipliedBy8 := multiplyBy8.Return()
3907
3908 // Then add the multiplied value to the base which results in the address of the target function (*wazevo.functionInstance)
3909 calcElementAddressInTable := builder.AllocateInstruction()
3910 calcElementAddressInTable.AsIadd(tableBase, targetOffsetInTableMultipliedBy8)
3911 builder.InsertInstruction(calcElementAddressInTable)
3912 return calcElementAddressInTable.Return()
3913 }
3914
3915 func (c *Compiler) prepareCall(fnIndex uint32) (isIndirect bool, sig *ssa.Signature, args ssa.Values, funcRefOrPtrValue uint64) {
3916 builder := c.ssaBuilder
3917 state := c.state()
3918 var typIndex wasm.Index
3919 if fnIndex < c.m.ImportFunctionCount {
3920 // Before transfer the control to the callee, we have to store the current module's moduleContextPtr
3921 // into execContext.callerModuleContextPtr in case when the callee is a Go function.
3922 c.storeCallerModuleContext()
3923 var fi int
3924 for i := range c.m.ImportSection {
3925 imp := &c.m.ImportSection[i]
3926 if imp.Type == wasm.ExternTypeFunc {
3927 if fi == int(fnIndex) {
3928 typIndex = imp.DescFunc
3929 break
3930 }
3931 fi++
3932 }
3933 }
3934 } else {
3935 typIndex = c.m.FunctionSection[fnIndex-c.m.ImportFunctionCount]
3936 }
3937 typ := &c.m.TypeSection[typIndex]
3938
3939 argN := len(typ.Params)
3940 tail := len(state.values) - argN
3941 vs := state.values[tail:]
3942 state.values = state.values[:tail]
3943 args = c.allocateVarLengthValues(2+len(vs), c.execCtxPtrValue)
3944
3945 sig = c.signatures[typ]
3946 if fnIndex >= c.m.ImportFunctionCount {
3947 args = args.Append(builder.VarLengthPool(), c.moduleCtxPtrValue) // This case the callee module is itself.
3948 args = args.Append(builder.VarLengthPool(), vs...)
3949 return false, sig, args, uint64(FunctionIndexToFuncRef(fnIndex))
3950 } else {
3951 // This case we have to read the address of the imported function from the module context.
3952 moduleCtx := c.moduleCtxPtrValue
3953 loadFuncPtr, loadModuleCtxPtr := builder.AllocateInstruction(), builder.AllocateInstruction()
3954 funcPtrOffset, moduleCtxPtrOffset, _ := c.offset.ImportedFunctionOffset(fnIndex)
3955 loadFuncPtr.AsLoad(moduleCtx, funcPtrOffset.U32(), ssa.TypeI64)
3956 loadModuleCtxPtr.AsLoad(moduleCtx, moduleCtxPtrOffset.U32(), ssa.TypeI64)
3957 builder.InsertInstruction(loadFuncPtr)
3958 builder.InsertInstruction(loadModuleCtxPtr)
3959
3960 args = args.Append(builder.VarLengthPool(), loadModuleCtxPtr.Return())
3961 args = args.Append(builder.VarLengthPool(), vs...)
3962
3963 return true, sig, args, uint64(loadFuncPtr.Return())
3964 }
3965 }
3966
3967 func (c *Compiler) lowerCall(fnIndex uint32) {
3968 builder := c.ssaBuilder
3969 state := c.state()
3970 isIndirect, sig, args, funcRefOrPtrValue := c.prepareCall(fnIndex)
3971
3972 call := builder.AllocateInstruction()
3973 if isIndirect {
3974 call.AsCallIndirect(ssa.Value(funcRefOrPtrValue), sig, args)
3975 } else {
3976 call.AsCall(ssa.FuncRef(funcRefOrPtrValue), sig, args)
3977 }
3978 builder.InsertInstruction(call)
3979
3980 first, rest := call.Returns()
3981 if first.Valid() {
3982 state.push(first)
3983 }
3984 for _, v := range rest {
3985 state.push(v)
3986 }
3987
3988 c.reloadAfterCall()
3989 }
3990
3991 func (c *Compiler) prepareCallIndirect(typeIndex, tableIndex uint32) (ssa.Value, *wasm.FunctionType, ssa.Values) {
3992 builder := c.ssaBuilder
3993 state := c.state()
3994
3995 elementOffsetInTable := state.pop()
3996 functionInstancePtrAddress := c.lowerAccessTableWithBoundsCheck(tableIndex, elementOffsetInTable)
3997 loadFunctionInstancePtr := builder.AllocateInstruction()
3998 loadFunctionInstancePtr.AsLoad(functionInstancePtrAddress, 0, ssa.TypeI64)
3999 builder.InsertInstruction(loadFunctionInstancePtr)
4000 functionInstancePtr := loadFunctionInstancePtr.Return()
4001
4002 // Check if it is not the null pointer.
4003 zero := builder.AllocateInstruction()
4004 zero.AsIconst64(0)
4005 builder.InsertInstruction(zero)
4006 checkNull := builder.AllocateInstruction()
4007 checkNull.AsIcmp(functionInstancePtr, zero.Return(), ssa.IntegerCmpCondEqual)
4008 builder.InsertInstruction(checkNull)
4009 exitIfNull := builder.AllocateInstruction()
4010 exitIfNull.AsExitIfTrueWithCode(c.execCtxPtrValue, checkNull.Return(), wazevoapi.ExitCodeIndirectCallNullPointer)
4011 builder.InsertInstruction(exitIfNull)
4012
4013 // We need to do the type check. First, load the target function instance's typeID.
4014 loadTypeID := builder.AllocateInstruction()
4015 loadTypeID.AsLoad(functionInstancePtr, wazevoapi.FunctionInstanceTypeIDOffset, ssa.TypeI32)
4016 builder.InsertInstruction(loadTypeID)
4017 actualTypeID := loadTypeID.Return()
4018
4019 // Next, we load the expected TypeID:
4020 loadTypeIDsBegin := builder.AllocateInstruction()
4021 loadTypeIDsBegin.AsLoad(c.moduleCtxPtrValue, c.offset.TypeIDs1stElement.U32(), ssa.TypeI64)
4022 builder.InsertInstruction(loadTypeIDsBegin)
4023 typeIDsBegin := loadTypeIDsBegin.Return()
4024
4025 loadExpectedTypeID := builder.AllocateInstruction()
4026 loadExpectedTypeID.AsLoad(typeIDsBegin, uint32(typeIndex)*4 /* size of wasm.FunctionTypeID */, ssa.TypeI32)
4027 builder.InsertInstruction(loadExpectedTypeID)
4028 expectedTypeID := loadExpectedTypeID.Return()
4029
4030 // Check if the type ID matches.
4031 checkTypeID := builder.AllocateInstruction()
4032 checkTypeID.AsIcmp(actualTypeID, expectedTypeID, ssa.IntegerCmpCondNotEqual)
4033 builder.InsertInstruction(checkTypeID)
4034 exitIfNotMatch := builder.AllocateInstruction()
4035 exitIfNotMatch.AsExitIfTrueWithCode(c.execCtxPtrValue, checkTypeID.Return(), wazevoapi.ExitCodeIndirectCallTypeMismatch)
4036 builder.InsertInstruction(exitIfNotMatch)
4037
4038 // Now ready to call the function. Load the executable and moduleContextOpaquePtr from the function instance.
4039 loadExecutablePtr := builder.AllocateInstruction()
4040 loadExecutablePtr.AsLoad(functionInstancePtr, wazevoapi.FunctionInstanceExecutableOffset, ssa.TypeI64)
4041 builder.InsertInstruction(loadExecutablePtr)
4042 executablePtr := loadExecutablePtr.Return()
4043 loadModuleContextOpaquePtr := builder.AllocateInstruction()
4044 loadModuleContextOpaquePtr.AsLoad(functionInstancePtr, wazevoapi.FunctionInstanceModuleContextOpaquePtrOffset, ssa.TypeI64)
4045 builder.InsertInstruction(loadModuleContextOpaquePtr)
4046 moduleContextOpaquePtr := loadModuleContextOpaquePtr.Return()
4047
4048 typ := &c.m.TypeSection[typeIndex]
4049 tail := len(state.values) - len(typ.Params)
4050 vs := state.values[tail:]
4051 state.values = state.values[:tail]
4052 args := c.allocateVarLengthValues(2+len(vs), c.execCtxPtrValue, moduleContextOpaquePtr)
4053 args = args.Append(builder.VarLengthPool(), vs...)
4054
4055 // Before transfer the control to the callee, we have to store the current module's moduleContextPtr
4056 // into execContext.callerModuleContextPtr in case when the callee is a Go function.
4057 c.storeCallerModuleContext()
4058
4059 return executablePtr, typ, args
4060 }
4061
4062 func (c *Compiler) lowerCallIndirect(typeIndex, tableIndex uint32) {
4063 builder := c.ssaBuilder
4064 state := c.state()
4065 executablePtr, typ, args := c.prepareCallIndirect(typeIndex, tableIndex)
4066
4067 call := builder.AllocateInstruction()
4068 call.AsCallIndirect(executablePtr, c.signatures[typ], args)
4069 builder.InsertInstruction(call)
4070
4071 first, rest := call.Returns()
4072 if first.Valid() {
4073 state.push(first)
4074 }
4075 for _, v := range rest {
4076 state.push(v)
4077 }
4078
4079 c.reloadAfterCall()
4080 }
4081
4082 func (c *Compiler) lowerTailCallReturnCall(fnIndex uint32) {
4083 isIndirect, sig, args, funcRefOrPtrValue := c.prepareCall(fnIndex)
4084 builder := c.ssaBuilder
4085 state := c.state()
4086
4087 call := builder.AllocateInstruction()
4088 if isIndirect {
4089 call.AsTailCallReturnCallIndirect(ssa.Value(funcRefOrPtrValue), sig, args)
4090 } else {
4091 call.AsTailCallReturnCall(ssa.FuncRef(funcRefOrPtrValue), sig, args)
4092 }
4093 builder.InsertInstruction(call)
4094
4095 // In a proper tail call, the following code is unreachable since execution
4096 // transfers to the callee. However, sometimes the backend might need to fall back to
4097 // a regular call, so we include return handling and let the backend delete it
4098 // when redundant.
4099 // For details, see internal/engine/RATIONALE.md
4100 first, rest := call.Returns()
4101 if first.Valid() {
4102 state.push(first)
4103 }
4104 for _, v := range rest {
4105 state.push(v)
4106 }
4107
4108 c.reloadAfterCall()
4109 c.lowerReturn(builder)
4110 }
4111
4112 func (c *Compiler) lowerTailCallReturnCallIndirect(typeIndex, tableIndex uint32) {
4113 builder := c.ssaBuilder
4114 state := c.state()
4115 executablePtr, typ, args := c.prepareCallIndirect(typeIndex, tableIndex)
4116
4117 call := builder.AllocateInstruction()
4118 call.AsTailCallReturnCallIndirect(executablePtr, c.signatures[typ], args)
4119 builder.InsertInstruction(call)
4120
4121 // In a proper tail call, the following code is unreachable since execution
4122 // transfers to the callee. However, sometimes the backend might need to fall back to
4123 // a regular call, so we include return handling and let the backend delete it
4124 // when redundant.
4125 // For details, see internal/engine/RATIONALE.md
4126 first, rest := call.Returns()
4127 if first.Valid() {
4128 state.push(first)
4129 }
4130 for _, v := range rest {
4131 state.push(v)
4132 }
4133
4134 c.reloadAfterCall()
4135 c.lowerReturn(builder)
4136 }
4137
4138 func (c *Compiler) prepareCallRef(typeIndex uint32) (ssa.Value, *wasm.FunctionType, ssa.Values) {
4139 builder := c.ssaBuilder
4140 state := c.state()
4141
4142 functionInstancePtr := state.pop()
4143
4144 // Check if it is not the null pointer.
4145 zero := builder.AllocateInstruction()
4146 zero.AsIconst64(0)
4147 builder.InsertInstruction(zero)
4148 checkNull := builder.AllocateInstruction()
4149 checkNull.AsIcmp(functionInstancePtr, zero.Return(), ssa.IntegerCmpCondEqual)
4150 builder.InsertInstruction(checkNull)
4151 exitIfNull := builder.AllocateInstruction()
4152 exitIfNull.AsExitIfTrueWithCode(c.execCtxPtrValue, checkNull.Return(), wazevoapi.ExitCodeNullReference)
4153 builder.InsertInstruction(exitIfNull)
4154
4155 // Load the executable and moduleContextOpaquePtr from the function instance.
4156 loadExecutablePtr := builder.AllocateInstruction()
4157 loadExecutablePtr.AsLoad(functionInstancePtr, wazevoapi.FunctionInstanceExecutableOffset, ssa.TypeI64)
4158 builder.InsertInstruction(loadExecutablePtr)
4159 executablePtr := loadExecutablePtr.Return()
4160 loadModuleContextOpaquePtr := builder.AllocateInstruction()
4161 loadModuleContextOpaquePtr.AsLoad(functionInstancePtr, wazevoapi.FunctionInstanceModuleContextOpaquePtrOffset, ssa.TypeI64)
4162 builder.InsertInstruction(loadModuleContextOpaquePtr)
4163 moduleContextOpaquePtr := loadModuleContextOpaquePtr.Return()
4164
4165 typ := &c.m.TypeSection[typeIndex]
4166 tail := len(state.values) - len(typ.Params)
4167 vs := state.values[tail:]
4168 state.values = state.values[:tail]
4169 args := c.allocateVarLengthValues(2+len(vs), c.execCtxPtrValue, moduleContextOpaquePtr)
4170 args = args.Append(builder.VarLengthPool(), vs...)
4171
4172 c.storeCallerModuleContext()
4173
4174 return executablePtr, typ, args
4175 }
4176
4177 func (c *Compiler) lowerCallRef(typeIndex uint32) {
4178 builder := c.ssaBuilder
4179 state := c.state()
4180 executablePtr, typ, args := c.prepareCallRef(typeIndex)
4181
4182 call := builder.AllocateInstruction()
4183 call.AsCallIndirect(executablePtr, c.signatures[typ], args)
4184 builder.InsertInstruction(call)
4185
4186 first, rest := call.Returns()
4187 if first.Valid() {
4188 state.push(first)
4189 }
4190 for _, v := range rest {
4191 state.push(v)
4192 }
4193
4194 c.reloadAfterCall()
4195 }
4196
4197 func (c *Compiler) lowerTailCallReturnCallRef(typeIndex uint32) {
4198 builder := c.ssaBuilder
4199 state := c.state()
4200 executablePtr, typ, args := c.prepareCallRef(typeIndex)
4201
4202 call := builder.AllocateInstruction()
4203 call.AsTailCallReturnCallIndirect(executablePtr, c.signatures[typ], args)
4204 builder.InsertInstruction(call)
4205
4206 // In a proper tail call, the following code is unreachable since execution
4207 // transfers to the callee. However, sometimes the backend might need to fall back to
4208 // a regular call, so we include return handling and let the backend delete it
4209 // when redundant.
4210 // For details, see internal/engine/RATIONALE.md
4211 first, rest := call.Returns()
4212 if first.Valid() {
4213 state.push(first)
4214 }
4215 for _, v := range rest {
4216 state.push(v)
4217 }
4218
4219 c.reloadAfterCall()
4220 c.lowerReturn(builder)
4221 }
4222
4223 // memOpSetup inserts the bounds check and calculates the address of the memory operation (loads/stores).
4224 func (c *Compiler) memOpSetup(baseAddr ssa.Value, constOffset, operationSizeInBytes uint64) (address ssa.Value) {
4225 address = ssa.ValueInvalid
4226 builder := c.ssaBuilder
4227
4228 baseAddrID := baseAddr.ID()
4229 ceil := constOffset + operationSizeInBytes
4230 if known := c.getKnownSafeBound(baseAddrID); known.valid() {
4231 // We reuse the calculated absolute address even if the bound is not known to be safe.
4232 address = known.absoluteAddr
4233 if ceil <= known.bound {
4234 if !address.Valid() {
4235 // This means that, the bound is known to be safe, but the memory base might have changed.
4236 // So, we re-calculate the address.
4237 memBase := c.getMemoryBaseValue(false)
4238 extBaseAddr := builder.AllocateInstruction().
4239 AsUExtend(baseAddr, 32, 64).
4240 Insert(builder).
4241 Return()
4242 address = builder.AllocateInstruction().
4243 AsIadd(memBase, extBaseAddr).Insert(builder).Return()
4244 known.absoluteAddr = address // Update the absolute address for the subsequent memory access.
4245 }
4246 return
4247 }
4248 }
4249
4250 ceilConst := builder.AllocateInstruction()
4251 ceilConst.AsIconst64(ceil)
4252 builder.InsertInstruction(ceilConst)
4253
4254 // We calculate the offset in 64-bit space.
4255 extBaseAddr := builder.AllocateInstruction().
4256 AsUExtend(baseAddr, 32, 64).
4257 Insert(builder).
4258 Return()
4259
4260 // Note: memLen is already zero extended to 64-bit space at the load time.
4261 memLen := c.getMemoryLenValue(false)
4262
4263 // baseAddrPlusCeil = baseAddr + ceil
4264 baseAddrPlusCeil := builder.AllocateInstruction()
4265 baseAddrPlusCeil.AsIadd(extBaseAddr, ceilConst.Return())
4266 builder.InsertInstruction(baseAddrPlusCeil)
4267
4268 // Check for out of bounds memory access: `memLen >= baseAddrPlusCeil`.
4269 cmp := builder.AllocateInstruction()
4270 cmp.AsIcmp(memLen, baseAddrPlusCeil.Return(), ssa.IntegerCmpCondUnsignedLessThan)
4271 builder.InsertInstruction(cmp)
4272 exitIfNZ := builder.AllocateInstruction()
4273 exitIfNZ.AsExitIfTrueWithCode(c.execCtxPtrValue, cmp.Return(), wazevoapi.ExitCodeMemoryOutOfBounds)
4274 builder.InsertInstruction(exitIfNZ)
4275
4276 // Load the value from memBase + extBaseAddr.
4277 if address == ssa.ValueInvalid { // Reuse the value if the memBase is already calculated at this point.
4278 memBase := c.getMemoryBaseValue(false)
4279 address = builder.AllocateInstruction().
4280 AsIadd(memBase, extBaseAddr).Insert(builder).Return()
4281 }
4282
4283 // Record the bound ceil for this baseAddr is known to be safe for the subsequent memory access in the same block.
4284 c.recordKnownSafeBound(baseAddrID, ceil, address)
4285 return
4286 }
4287
4288 // atomicMemOpSetup inserts the bounds check and calculates the address of the memory operation (loads/stores), including
4289 // the constant offset and performs an alignment check on the final address.
4290 func (c *Compiler) atomicMemOpSetup(baseAddr ssa.Value, constOffset, operationSizeInBytes uint64) (address ssa.Value) {
4291 builder := c.ssaBuilder
4292
4293 addrWithoutOffset := c.memOpSetup(baseAddr, constOffset, operationSizeInBytes)
4294 var addr ssa.Value
4295 if constOffset == 0 {
4296 addr = addrWithoutOffset
4297 } else {
4298 offset := builder.AllocateInstruction().AsIconst64(constOffset).Insert(builder).Return()
4299 addr = builder.AllocateInstruction().AsIadd(addrWithoutOffset, offset).Insert(builder).Return()
4300 }
4301
4302 c.memAlignmentCheck(addr, operationSizeInBytes)
4303
4304 return addr
4305 }
4306
4307 func (c *Compiler) memAlignmentCheck(addr ssa.Value, operationSizeInBytes uint64) {
4308 if operationSizeInBytes == 1 {
4309 return // No alignment restrictions when accessing a byte
4310 }
4311 var checkBits uint64
4312 switch operationSizeInBytes {
4313 case 2:
4314 checkBits = 0b1
4315 case 4:
4316 checkBits = 0b11
4317 case 8:
4318 checkBits = 0b111
4319 }
4320
4321 builder := c.ssaBuilder
4322
4323 mask := builder.AllocateInstruction().AsIconst64(checkBits).Insert(builder).Return()
4324 masked := builder.AllocateInstruction().AsBand(addr, mask).Insert(builder).Return()
4325 zero := builder.AllocateInstruction().AsIconst64(0).Insert(builder).Return()
4326 cmp := builder.AllocateInstruction().AsIcmp(masked, zero, ssa.IntegerCmpCondNotEqual).Insert(builder).Return()
4327 builder.AllocateInstruction().AsExitIfTrueWithCode(c.execCtxPtrValue, cmp, wazevoapi.ExitCodeUnalignedAtomic).Insert(builder)
4328 }
4329
4330 func (c *Compiler) callMemmove(dst, src, size ssa.Value) {
4331 args := c.allocateVarLengthValues(3, dst, src, size)
4332 if size.Type() != ssa.TypeI64 {
4333 panic("TODO: memmove size must be i64")
4334 }
4335
4336 builder := c.ssaBuilder
4337 memmovePtr := builder.AllocateInstruction().
4338 AsLoad(c.execCtxPtrValue,
4339 wazevoapi.ExecutionContextOffsetMemmoveAddress.U32(),
4340 ssa.TypeI64,
4341 ).Insert(builder).Return()
4342 builder.AllocateInstruction().AsCallGoRuntimeMemmove(memmovePtr, &c.memmoveSig, args).Insert(builder)
4343 }
4344
4345 func (c *Compiler) reloadAfterCall() {
4346 // Note that when these are not used in the following instructions, they will be optimized out.
4347 // So in any ways, we define them!
4348
4349 // After calling any function, memory buffer might have changed. So we need to re-define the variable.
4350 // However, if the memory is shared, we don't need to reload the memory base and length as the base will never change.
4351 if c.needMemory && !c.memoryShared {
4352 c.reloadMemoryBaseLen()
4353 }
4354
4355 // Also, any mutable Global can change.
4356 for _, index := range c.mutableGlobalVariablesIndexes {
4357 _ = c.getWasmGlobalValue(index, true)
4358 }
4359 }
4360
4361 func (c *Compiler) reloadMemoryBaseLen() {
4362 _ = c.getMemoryBaseValue(true)
4363 _ = c.getMemoryLenValue(true)
4364
4365 // This function being called means that the memory base might have changed.
4366 // Therefore, we need to clear the absolute addresses recorded in the known safe bounds
4367 // because we cache the absolute address of the memory access per each base offset.
4368 c.resetAbsoluteAddressInSafeBounds()
4369 }
4370
4371 func (c *Compiler) setWasmGlobalValue(index wasm.Index, v ssa.Value) {
4372 variable := c.globalVariables[index]
4373 opaqueOffset := c.offset.GlobalInstanceOffset(index)
4374
4375 builder := c.ssaBuilder
4376 if index < c.m.ImportGlobalCount {
4377 loadGlobalInstPtr := builder.AllocateInstruction()
4378 loadGlobalInstPtr.AsLoad(c.moduleCtxPtrValue, uint32(opaqueOffset), ssa.TypeI64)
4379 builder.InsertInstruction(loadGlobalInstPtr)
4380
4381 store := builder.AllocateInstruction()
4382 store.AsStore(ssa.OpcodeStore, v, loadGlobalInstPtr.Return(), uint32(0))
4383 builder.InsertInstruction(store)
4384
4385 } else {
4386 store := builder.AllocateInstruction()
4387 store.AsStore(ssa.OpcodeStore, v, c.moduleCtxPtrValue, uint32(opaqueOffset))
4388 builder.InsertInstruction(store)
4389 }
4390
4391 // The value has changed to `v`, so we record it.
4392 builder.DefineVariableInCurrentBB(variable, v)
4393 }
4394
4395 func (c *Compiler) getWasmGlobalValue(index wasm.Index, forceLoad bool) ssa.Value {
4396 variable := c.globalVariables[index]
4397 typ := c.globalVariablesTypes[index]
4398 opaqueOffset := c.offset.GlobalInstanceOffset(index)
4399
4400 builder := c.ssaBuilder
4401 if !forceLoad {
4402 if v := builder.FindValueInLinearPath(variable); v.Valid() {
4403 return v
4404 }
4405 }
4406
4407 var load *ssa.Instruction
4408 if index < c.m.ImportGlobalCount {
4409 loadGlobalInstPtr := builder.AllocateInstruction()
4410 loadGlobalInstPtr.AsLoad(c.moduleCtxPtrValue, uint32(opaqueOffset), ssa.TypeI64)
4411 builder.InsertInstruction(loadGlobalInstPtr)
4412 load = builder.AllocateInstruction().
4413 AsLoad(loadGlobalInstPtr.Return(), uint32(0), typ)
4414 } else {
4415 load = builder.AllocateInstruction().
4416 AsLoad(c.moduleCtxPtrValue, uint32(opaqueOffset), typ)
4417 }
4418
4419 v := load.Insert(builder).Return()
4420 builder.DefineVariableInCurrentBB(variable, v)
4421 return v
4422 }
4423
4424 const (
4425 memoryInstanceBufOffset = 0
4426 memoryInstanceBufSizeOffset = memoryInstanceBufOffset + 8
4427 )
4428
4429 func (c *Compiler) getMemoryBaseValue(forceReload bool) ssa.Value {
4430 builder := c.ssaBuilder
4431 variable := c.memoryBaseVariable
4432 if !forceReload {
4433 if v := builder.FindValueInLinearPath(variable); v.Valid() {
4434 return v
4435 }
4436 }
4437
4438 var ret ssa.Value
4439 if c.offset.LocalMemoryBegin < 0 {
4440 loadMemInstPtr := builder.AllocateInstruction()
4441 loadMemInstPtr.AsLoad(c.moduleCtxPtrValue, c.offset.ImportedMemoryBegin.U32(), ssa.TypeI64)
4442 builder.InsertInstruction(loadMemInstPtr)
4443 memInstPtr := loadMemInstPtr.Return()
4444
4445 loadBufPtr := builder.AllocateInstruction()
4446 loadBufPtr.AsLoad(memInstPtr, memoryInstanceBufOffset, ssa.TypeI64)
4447 builder.InsertInstruction(loadBufPtr)
4448 ret = loadBufPtr.Return()
4449 } else {
4450 load := builder.AllocateInstruction()
4451 load.AsLoad(c.moduleCtxPtrValue, c.offset.LocalMemoryBase().U32(), ssa.TypeI64)
4452 builder.InsertInstruction(load)
4453 ret = load.Return()
4454 }
4455
4456 builder.DefineVariableInCurrentBB(variable, ret)
4457 return ret
4458 }
4459
4460 func (c *Compiler) getMemoryLenValue(forceReload bool) ssa.Value {
4461 variable := c.memoryLenVariable
4462 builder := c.ssaBuilder
4463 if !forceReload && !c.memoryShared {
4464 if v := builder.FindValueInLinearPath(variable); v.Valid() {
4465 return v
4466 }
4467 }
4468
4469 var ret ssa.Value
4470 if c.offset.LocalMemoryBegin < 0 {
4471 loadMemInstPtr := builder.AllocateInstruction()
4472 loadMemInstPtr.AsLoad(c.moduleCtxPtrValue, c.offset.ImportedMemoryBegin.U32(), ssa.TypeI64)
4473 builder.InsertInstruction(loadMemInstPtr)
4474 memInstPtr := loadMemInstPtr.Return()
4475
4476 loadBufSizePtr := builder.AllocateInstruction()
4477 if c.memoryShared {
4478 sizeOffset := builder.AllocateInstruction().AsIconst64(memoryInstanceBufSizeOffset).Insert(builder).Return()
4479 addr := builder.AllocateInstruction().AsIadd(memInstPtr, sizeOffset).Insert(builder).Return()
4480 loadBufSizePtr.AsAtomicLoad(addr, 8, ssa.TypeI64)
4481 } else {
4482 loadBufSizePtr.AsLoad(memInstPtr, memoryInstanceBufSizeOffset, ssa.TypeI64)
4483 }
4484 builder.InsertInstruction(loadBufSizePtr)
4485
4486 ret = loadBufSizePtr.Return()
4487 } else {
4488 load := builder.AllocateInstruction()
4489 if c.memoryShared {
4490 lenOffset := builder.AllocateInstruction().AsIconst64(c.offset.LocalMemoryLen().U64()).Insert(builder).Return()
4491 addr := builder.AllocateInstruction().AsIadd(c.moduleCtxPtrValue, lenOffset).Insert(builder).Return()
4492 load.AsAtomicLoad(addr, 8, ssa.TypeI64)
4493 } else {
4494 load.AsExtLoad(ssa.OpcodeUload32, c.moduleCtxPtrValue, c.offset.LocalMemoryLen().U32(), true)
4495 }
4496 builder.InsertInstruction(load)
4497 ret = load.Return()
4498 }
4499
4500 builder.DefineVariableInCurrentBB(variable, ret)
4501 return ret
4502 }
4503
4504 func (c *Compiler) insertIcmp(cond ssa.IntegerCmpCond) {
4505 state, builder := c.state(), c.ssaBuilder
4506 y, x := state.pop(), state.pop()
4507 cmp := builder.AllocateInstruction()
4508 cmp.AsIcmp(x, y, cond)
4509 builder.InsertInstruction(cmp)
4510 value := cmp.Return()
4511 state.push(value)
4512 }
4513
4514 func (c *Compiler) insertFcmp(cond ssa.FloatCmpCond) {
4515 state, builder := c.state(), c.ssaBuilder
4516 y, x := state.pop(), state.pop()
4517 cmp := builder.AllocateInstruction()
4518 cmp.AsFcmp(x, y, cond)
4519 builder.InsertInstruction(cmp)
4520 value := cmp.Return()
4521 state.push(value)
4522 }
4523
4524 // storeCallerModuleContext stores the current module's moduleContextPtr into execContext.callerModuleContextPtr.
4525 func (c *Compiler) storeCallerModuleContext() {
4526 builder := c.ssaBuilder
4527 execCtx := c.execCtxPtrValue
4528 store := builder.AllocateInstruction()
4529 store.AsStore(ssa.OpcodeStore,
4530 c.moduleCtxPtrValue, execCtx, wazevoapi.ExecutionContextOffsetCallerModuleContextPtr.U32())
4531 builder.InsertInstruction(store)
4532 }
4533
4534 // resolveTagType returns the FunctionType for the tag at the given module-local index.
4535 func (c *Compiler) resolveTagType(tagIndex uint32) *wasm.FunctionType {
4536 if tagIndex < c.m.ImportTagCount {
4537 cur := uint32(0)
4538 for i := range c.m.ImportSection {
4539 imp := &c.m.ImportSection[i]
4540 if imp.Type != wasm.ExternTypeTag {
4541 continue
4542 }
4543 if tagIndex == cur {
4544 return &c.m.TypeSection[imp.DescTag]
4545 }
4546 cur++
4547 }
4548 } else {
4549 tagSectionIdx := tagIndex - c.m.ImportTagCount
4550 if tagSectionIdx < uint32(len(c.m.TagSection)) {
4551 typeIdx := c.m.TagSection[tagSectionIdx].Type
4552 return &c.m.TypeSection[typeIdx]
4553 }
4554 }
4555 return nil
4556 }
4557
4558 // emitThrow emits a call to the shared throw trampoline with the given exnref,
4559 // followed by an unreachable exit (throw never returns).
4560 func (c *Compiler) emitThrow(exnref ssa.Value) {
4561 builder := c.ssaBuilder
4562 throwPtr := builder.AllocateInstruction().
4563 AsLoad(c.execCtxPtrValue,
4564 wazevoapi.ExecutionContextOffsetThrowTrampolineAddress.U32(),
4565 ssa.TypeI64,
4566 ).Insert(builder).Return()
4567 throwArgs := c.allocateVarLengthValues(2, c.execCtxPtrValue, exnref)
4568 builder.AllocateInstruction().
4569 AsCallIndirect(throwPtr, &c.throwSig, throwArgs).
4570 Insert(builder)
4571
4572 exit := builder.AllocateInstruction()
4573 exit.AsExitWithCode(c.execCtxPtrValue, wazevoapi.ExitCodeUnreachable)
4574 builder.InsertInstruction(exit)
4575 }
4576
4577 // emitTryTableLeave emits a trampoline call to pop the try handler in the dispatch loop.
4578 func (c *Compiler) emitTryTableLeave() {
4579 builder := c.ssaBuilder
4580 c.storeCallerModuleContext()
4581
4582 leavePtr := builder.AllocateInstruction().
4583 AsLoad(c.execCtxPtrValue,
4584 wazevoapi.ExecutionContextOffsetTryTableLeaveTrampolineAddress.U32(),
4585 ssa.TypeI64,
4586 ).Insert(builder).Return()
4587
4588 args := c.allocateVarLengthValues(1, c.execCtxPtrValue)
4589 builder.AllocateInstruction().
4590 AsCallIndirect(leavePtr, &c.tryTableLeaveSig, args).
4591 Insert(builder)
4592 }
4593
4594 // branchExitsTryTable returns true if a branch to the given depth would
4595 // exit at least one try_table frame.
4596 func (c *Compiler) branchExitsTryTable(depth int) bool {
4597 state := c.state()
4598 tail := len(state.controlFrames) - 1
4599 for i := 0; i < depth; i++ {
4600 if state.controlFrames[tail-i].kind == controlFrameKindTryTable {
4601 return true
4602 }
4603 }
4604 return false
4605 }
4606
4607 // emitTryTableLeaves emits TryTableLeave calls for try_table frames
4608 // that would be exited by a branch to the given depth.
4609 func (c *Compiler) emitTryTableLeaves(depth int) {
4610 state := c.state()
4611 tail := len(state.controlFrames) - 1
4612 for i := 0; i < depth; i++ {
4613 if state.controlFrames[tail-i].kind == controlFrameKindTryTable {
4614 c.emitTryTableLeave()
4615 }
4616 }
4617 }
4618
4619 // catchClause holds a parsed catch clause from a try_table instruction.
4620 type catchClause struct {
4621 kind byte
4622 tagIndex uint32
4623 labelIdx uint32
4624 }
4625
4626 // loadExceptionParams loads the exception params from the caught Exception's
4627 // Params slice. The dispatch loop sets execCtx.exceptionParamsPtr to the
4628 // slice's backing-array pointer after matching a handler. We load that pointer
4629 // and then read each param from [ptr + i*8], mirroring the stores emitted by
4630 // the throw lowering. Float params were bitcast to integers at the throw site,
4631 // so we load as integer and bitcast back to the original type.
4632 func (c *Compiler) loadExceptionParams(tagType *wasm.FunctionType) []ssa.Value {
4633 if len(tagType.Params) == 0 {
4634 return nil
4635 }
4636 builder := c.ssaBuilder
4637
4638 // Load the pointer to the caught Exception's Params backing array.
4639 paramsPtr := builder.AllocateInstruction().
4640 AsLoad(c.execCtxPtrValue,
4641 wazevoapi.ExecutionContextOffsetExceptionParamsPtr.U32(),
4642 ssa.TypeI64,
4643 ).Insert(builder).Return()
4644
4645 var values []ssa.Value
4646 for i, vt := range tagType.Params {
4647 offset := uint32(i) * 8
4648 ssaType := WasmTypeToSSAType(vt)
4649 switch ssaType {
4650 case ssa.TypeF32:
4651 // Stored as i32 at throw site; bitcast back to f32.
4652 raw := builder.AllocateInstruction().
4653 AsLoad(paramsPtr, offset, ssa.TypeI32).
4654 Insert(builder).Return()
4655 val := builder.AllocateInstruction().AsBitcast(raw, ssa.TypeF32).Insert(builder).Return()
4656 values = append(values, val)
4657 case ssa.TypeF64:
4658 // Stored as i64 at throw site; bitcast back to f64.
4659 raw := builder.AllocateInstruction().
4660 AsLoad(paramsPtr, offset, ssa.TypeI64).
4661 Insert(builder).Return()
4662 val := builder.AllocateInstruction().AsBitcast(raw, ssa.TypeF64).Insert(builder).Return()
4663 values = append(values, val)
4664 default:
4665 val := builder.AllocateInstruction().
4666 AsLoad(paramsPtr, offset, ssaType).
4667 Insert(builder).Return()
4668 values = append(values, val)
4669 }
4670 }
4671 return values
4672 }
4673
4674 // loadExnRef loads the exnref (pointer to Exception) from the executionContext.
4675 // The dispatch loop writes it to exceptionPtr after matching a handler.
4676 func (c *Compiler) loadExnRef() ssa.Value {
4677 builder := c.ssaBuilder
4678 return builder.AllocateInstruction().
4679 AsLoad(c.execCtxPtrValue,
4680 wazevoapi.ExecutionContextOffsetExceptionPtr.U32(),
4681 ssa.TypeI64,
4682 ).Insert(builder).Return()
4683 }
4684
4685 // skipTryTableCatchClauses advances the bytecode PC past the catch clauses
4686 // of a try_table instruction. This is used both in reachable and unreachable states.
4687 func (c *Compiler) skipTryTableCatchClauses() {
4688 c.loweringState.pc++
4689 catchCount, catchNum, _ := leb128.LoadUint32(c.wasmFunctionBody[c.loweringState.pc:])
4690 c.loweringState.pc += int(catchNum) - 1
4691 for i := uint32(0); i < catchCount; i++ {
4692 c.loweringState.pc++
4693 kind := c.wasmFunctionBody[c.loweringState.pc]
4694 switch kind {
4695 case wasm.CatchKindCatch, wasm.CatchKindCatchRef:
4696 // Read tag index.
4697 c.loweringState.pc++
4698 _, n, _ := leb128.LoadUint32(c.wasmFunctionBody[c.loweringState.pc:])
4699 c.loweringState.pc += int(n) - 1
4700 // Read label index.
4701 c.loweringState.pc++
4702 _, n, _ = leb128.LoadUint32(c.wasmFunctionBody[c.loweringState.pc:])
4703 c.loweringState.pc += int(n) - 1
4704 case wasm.CatchKindCatchAll, wasm.CatchKindCatchAllRef:
4705 // Read label index.
4706 c.loweringState.pc++
4707 _, n, _ := leb128.LoadUint32(c.wasmFunctionBody[c.loweringState.pc:])
4708 c.loweringState.pc += int(n) - 1
4709 }
4710 }
4711 }
4712
4713 func (c *Compiler) readByte() byte {
4714 v := c.wasmFunctionBody[c.loweringState.pc+1]
4715 c.loweringState.pc++
4716 return v
4717 }
4718
4719 func (c *Compiler) readI32u() uint32 {
4720 v, n, err := leb128.LoadUint32(c.wasmFunctionBody[c.loweringState.pc+1:])
4721 if err != nil {
4722 panic(err) // shouldn't be reached since compilation comes after validation.
4723 }
4724 c.loweringState.pc += int(n)
4725 return v
4726 }
4727
4728 func (c *Compiler) readI32s() int32 {
4729 v, n, err := leb128.LoadInt32(c.wasmFunctionBody[c.loweringState.pc+1:])
4730 if err != nil {
4731 panic(err) // shouldn't be reached since compilation comes after validation.
4732 }
4733 c.loweringState.pc += int(n)
4734 return v
4735 }
4736
4737 func (c *Compiler) readI64s() int64 {
4738 v, n, err := leb128.LoadInt64(c.wasmFunctionBody[c.loweringState.pc+1:])
4739 if err != nil {
4740 panic(err) // shouldn't be reached since compilation comes after validation.
4741 }
4742 c.loweringState.pc += int(n)
4743 return v
4744 }
4745
4746 func (c *Compiler) readF32() float32 {
4747 v := math.Float32frombits(binary.LittleEndian.Uint32(c.wasmFunctionBody[c.loweringState.pc+1:]))
4748 c.loweringState.pc += 4
4749 return v
4750 }
4751
4752 func (c *Compiler) readF64() float64 {
4753 v := math.Float64frombits(binary.LittleEndian.Uint64(c.wasmFunctionBody[c.loweringState.pc+1:]))
4754 c.loweringState.pc += 8
4755 return v
4756 }
4757
4758 // readBlockType reads the block type from the current position of the bytecode reader.
4759 func (c *Compiler) readBlockType() *wasm.FunctionType {
4760 state := c.state()
4761
4762 c.br.Reset(c.wasmFunctionBody[state.pc+1:])
4763 bt, num, err := wasm.DecodeBlockType(c.m.TypeSection, c.br, api.CoreFeaturesV2)
4764 if err != nil {
4765 panic(err) // shouldn't be reached since compilation comes after validation.
4766 }
4767 state.pc += int(num)
4768
4769 return bt
4770 }
4771
4772 func (c *Compiler) readMemArg() (align, offset uint32) {
4773 state := c.state()
4774
4775 align, num, err := leb128.LoadUint32(c.wasmFunctionBody[state.pc+1:])
4776 if err != nil {
4777 panic(fmt.Errorf("read memory align: %v", err))
4778 }
4779
4780 state.pc += int(num)
4781 offset, num, err = leb128.LoadUint32(c.wasmFunctionBody[state.pc+1:])
4782 if err != nil {
4783 panic(fmt.Errorf("read memory offset: %v", err))
4784 }
4785
4786 state.pc += int(num)
4787 return align, offset
4788 }
4789
4790 // insertJumpToBlock inserts a jump instruction to the given block in the current block.
4791 func (c *Compiler) insertJumpToBlock(args ssa.Values, targetBlk ssa.BasicBlock) {
4792 if targetBlk.ReturnBlock() {
4793 if c.needListener {
4794 c.callListenerAfter()
4795 }
4796 }
4797
4798 builder := c.ssaBuilder
4799 jmp := builder.AllocateInstruction()
4800 jmp.AsJump(args, targetBlk)
4801 builder.InsertInstruction(jmp)
4802 }
4803
4804 func (c *Compiler) insertIntegerExtend(signed bool, from, to byte) {
4805 state := c.state()
4806 builder := c.ssaBuilder
4807 v := state.pop()
4808 extend := builder.AllocateInstruction()
4809 if signed {
4810 extend.AsSExtend(v, from, to)
4811 } else {
4812 extend.AsUExtend(v, from, to)
4813 }
4814 builder.InsertInstruction(extend)
4815 value := extend.Return()
4816 state.push(value)
4817 }
4818
4819 func (c *Compiler) switchTo(originalStackLen int, targetBlk ssa.BasicBlock) {
4820 if targetBlk.Preds() == 0 {
4821 c.loweringState.unreachable = true
4822 }
4823
4824 // Now we should adjust the stack and start translating the continuation block.
4825 c.loweringState.values = c.loweringState.values[:originalStackLen]
4826
4827 c.ssaBuilder.SetCurrentBlock(targetBlk)
4828
4829 // At this point, blocks params consist only of the Wasm-level parameters,
4830 // (since it's added only when we are trying to resolve variable *inside* this block).
4831 for i := 0; i < targetBlk.Params(); i++ {
4832 value := targetBlk.Param(i)
4833 c.loweringState.push(value)
4834 }
4835 }
4836
4837 // results returns the number of results of the current function.
4838 func (c *Compiler) results() int {
4839 return len(c.wasmFunctionTyp.Results)
4840 }
4841
4842 func (c *Compiler) lowerBrTable(labels []uint32, index ssa.Value) {
4843 state := c.state()
4844 builder := c.ssaBuilder
4845
4846 f := state.ctrlPeekAt(int(labels[0]))
4847 var numArgs int
4848 if f.isLoop() {
4849 numArgs = len(f.blockType.Params)
4850 } else {
4851 numArgs = len(f.blockType.Results)
4852 }
4853
4854 varPool := builder.VarLengthPool()
4855 trampolineBlockIDs := varPool.Allocate(len(labels))
4856
4857 // We need trampoline blocks since depending on the target block structure, we might end up inserting moves before jumps,
4858 // which cannot be done with br_table. Instead, we can do such per-block moves in the trampoline blocks.
4859 // At the linking phase (very end of the backend), we can remove the unnecessary jumps, and therefore no runtime overhead.
4860 currentBlk := builder.CurrentBlock()
4861 for _, l := range labels {
4862 // Args are always on the top of the stack. Note that we should not share the args slice
4863 // among the jump instructions since the args are modified during passes (e.g. redundant phi elimination).
4864 args := c.nPeekDup(numArgs)
4865 targetBlk, _ := state.brTargetArgNumFor(l)
4866 trampoline := builder.AllocateBasicBlock()
4867 builder.SetCurrentBlock(trampoline)
4868 c.emitTryTableLeaves(int(l))
4869 c.insertJumpToBlock(args, targetBlk)
4870 trampolineBlockIDs = trampolineBlockIDs.Append(builder.VarLengthPool(), ssa.Value(trampoline.ID()))
4871 }
4872 builder.SetCurrentBlock(currentBlk)
4873
4874 // If the target block has no arguments, we can just jump to the target block.
4875 brTable := builder.AllocateInstruction()
4876 brTable.AsBrTable(index, trampolineBlockIDs)
4877 builder.InsertInstruction(brTable)
4878
4879 for _, trampolineID := range trampolineBlockIDs.View() {
4880 builder.Seal(builder.BasicBlock(ssa.BasicBlockID(trampolineID)))
4881 }
4882 }
4883
4884 func (l *loweringState) brTargetArgNumFor(labelIndex uint32) (targetBlk ssa.BasicBlock, argNum int) {
4885 targetFrame := l.ctrlPeekAt(int(labelIndex))
4886 if targetFrame.isLoop() {
4887 targetBlk, argNum = targetFrame.blk, len(targetFrame.blockType.Params)
4888 } else {
4889 targetBlk, argNum = targetFrame.followingBlock, len(targetFrame.blockType.Results)
4890 }
4891 return
4892 }
4893
4894 func (c *Compiler) callListenerBefore() {
4895 c.storeCallerModuleContext()
4896
4897 builder := c.ssaBuilder
4898 beforeListeners1stElement := builder.AllocateInstruction().
4899 AsLoad(c.moduleCtxPtrValue,
4900 c.offset.BeforeListenerTrampolines1stElement.U32(),
4901 ssa.TypeI64,
4902 ).Insert(builder).Return()
4903
4904 beforeListenerPtr := builder.AllocateInstruction().
4905 AsLoad(beforeListeners1stElement, uint32(c.wasmFunctionTypeIndex)*8 /* 8 bytes per index */, ssa.TypeI64).Insert(builder).Return()
4906
4907 entry := builder.EntryBlock()
4908 ps := entry.Params()
4909
4910 args := c.allocateVarLengthValues(ps, c.execCtxPtrValue,
4911 builder.AllocateInstruction().AsIconst32(c.wasmLocalFunctionIndex).Insert(builder).Return())
4912 for i := 2; i < ps; i++ {
4913 args = args.Append(builder.VarLengthPool(), entry.Param(i))
4914 }
4915
4916 beforeSig := c.listenerSignatures[c.wasmFunctionTyp][0]
4917 builder.AllocateInstruction().
4918 AsCallIndirect(beforeListenerPtr, beforeSig, args).
4919 Insert(builder)
4920 }
4921
4922 func (c *Compiler) callListenerAfter() {
4923 c.storeCallerModuleContext()
4924
4925 builder := c.ssaBuilder
4926 afterListeners1stElement := builder.AllocateInstruction().
4927 AsLoad(c.moduleCtxPtrValue,
4928 c.offset.AfterListenerTrampolines1stElement.U32(),
4929 ssa.TypeI64,
4930 ).Insert(builder).Return()
4931
4932 afterListenerPtr := builder.AllocateInstruction().
4933 AsLoad(afterListeners1stElement,
4934 uint32(c.wasmFunctionTypeIndex)*8 /* 8 bytes per index */, ssa.TypeI64).
4935 Insert(builder).
4936 Return()
4937
4938 afterSig := c.listenerSignatures[c.wasmFunctionTyp][1]
4939 args := c.allocateVarLengthValues(
4940 c.results()+2,
4941 c.execCtxPtrValue,
4942 builder.AllocateInstruction().AsIconst32(c.wasmLocalFunctionIndex).Insert(builder).Return(),
4943 )
4944
4945 l := c.state()
4946 tail := len(l.values)
4947 args = args.Append(c.ssaBuilder.VarLengthPool(), l.values[tail-c.results():tail]...)
4948 builder.AllocateInstruction().
4949 AsCallIndirect(afterListenerPtr, afterSig, args).
4950 Insert(builder)
4951 }
4952
4953 const (
4954 elementOrDataInstanceLenOffset = 8
4955 elementOrDataInstanceSize = 24
4956 )
4957
4958 // dropInstance inserts instructions to drop the element/data instance specified by the given index.
4959 func (c *Compiler) dropDataOrElementInstance(index uint32, firstItemOffset wazevoapi.Offset) {
4960 builder := c.ssaBuilder
4961 instPtr := c.dataOrElementInstanceAddr(index, firstItemOffset)
4962
4963 zero := builder.AllocateInstruction().AsIconst64(0).Insert(builder).Return()
4964
4965 // Clear the instance.
4966 builder.AllocateInstruction().AsStore(ssa.OpcodeStore, zero, instPtr, 0).Insert(builder)
4967 builder.AllocateInstruction().AsStore(ssa.OpcodeStore, zero, instPtr, elementOrDataInstanceLenOffset).Insert(builder)
4968 builder.AllocateInstruction().AsStore(ssa.OpcodeStore, zero, instPtr, elementOrDataInstanceLenOffset+8).Insert(builder)
4969 }
4970
4971 func (c *Compiler) dataOrElementInstanceAddr(index uint32, firstItemOffset wazevoapi.Offset) ssa.Value {
4972 builder := c.ssaBuilder
4973
4974 _1stItemPtr := builder.
4975 AllocateInstruction().
4976 AsLoad(c.moduleCtxPtrValue, firstItemOffset.U32(), ssa.TypeI64).
4977 Insert(builder).Return()
4978
4979 // Each data/element instance is a slice, so we need to multiply index by 16 to get the offset of the target instance.
4980 index = index * elementOrDataInstanceSize
4981 indexExt := builder.AllocateInstruction().AsIconst64(uint64(index)).Insert(builder).Return()
4982 // Then, add the offset to the address of the instance.
4983 instPtr := builder.AllocateInstruction().AsIadd(_1stItemPtr, indexExt).Insert(builder).Return()
4984 return instPtr
4985 }
4986
4987 func (c *Compiler) boundsCheckInDataOrElementInstance(instPtr, offsetInInstance, copySize ssa.Value, exitCode wazevoapi.ExitCode) {
4988 builder := c.ssaBuilder
4989 dataInstLen := builder.AllocateInstruction().
4990 AsLoad(instPtr, elementOrDataInstanceLenOffset, ssa.TypeI64).
4991 Insert(builder).Return()
4992 ceil := builder.AllocateInstruction().AsIadd(offsetInInstance, copySize).Insert(builder).Return()
4993 cmp := builder.AllocateInstruction().
4994 AsIcmp(dataInstLen, ceil, ssa.IntegerCmpCondUnsignedLessThan).
4995 Insert(builder).
4996 Return()
4997 builder.AllocateInstruction().
4998 AsExitIfTrueWithCode(c.execCtxPtrValue, cmp, exitCode).
4999 Insert(builder)
5000 }
5001
5002 func (c *Compiler) boundsCheckInTable(tableIndex uint32, offset, size ssa.Value) (tableInstancePtr ssa.Value) {
5003 builder := c.ssaBuilder
5004 dstCeil := builder.AllocateInstruction().AsIadd(offset, size).Insert(builder).Return()
5005
5006 // Load the table.
5007 tableInstancePtr = builder.AllocateInstruction().
5008 AsLoad(c.moduleCtxPtrValue, c.offset.TableOffset(int(tableIndex)).U32(), ssa.TypeI64).
5009 Insert(builder).Return()
5010
5011 // Load the table's length.
5012 tableLen := builder.AllocateInstruction().
5013 AsLoad(tableInstancePtr, tableInstanceLenOffset, ssa.TypeI32).Insert(builder).Return()
5014 tableLenExt := builder.AllocateInstruction().AsUExtend(tableLen, 32, 64).Insert(builder).Return()
5015
5016 // Compare the length and the target, and trap if out of bounds.
5017 checkOOB := builder.AllocateInstruction()
5018 checkOOB.AsIcmp(tableLenExt, dstCeil, ssa.IntegerCmpCondUnsignedLessThan)
5019 builder.InsertInstruction(checkOOB)
5020 exitIfOOB := builder.AllocateInstruction()
5021 exitIfOOB.AsExitIfTrueWithCode(c.execCtxPtrValue, checkOOB.Return(), wazevoapi.ExitCodeTableOutOfBounds)
5022 builder.InsertInstruction(exitIfOOB)
5023 return
5024 }
5025
5026 func (c *Compiler) loadTableBaseAddr(tableInstancePtr ssa.Value) ssa.Value {
5027 builder := c.ssaBuilder
5028 loadTableBaseAddress := builder.
5029 AllocateInstruction().
5030 AsLoad(tableInstancePtr, tableInstanceBaseAddressOffset, ssa.TypeI64).
5031 Insert(builder)
5032 return loadTableBaseAddress.Return()
5033 }
5034
5035 func (c *Compiler) boundsCheckInMemory(memLen, offset, size ssa.Value) {
5036 builder := c.ssaBuilder
5037 ceil := builder.AllocateInstruction().AsIadd(offset, size).Insert(builder).Return()
5038 cmp := builder.AllocateInstruction().
5039 AsIcmp(memLen, ceil, ssa.IntegerCmpCondUnsignedLessThan).
5040 Insert(builder).
5041 Return()
5042 builder.AllocateInstruction().
5043 AsExitIfTrueWithCode(c.execCtxPtrValue, cmp, wazevoapi.ExitCodeMemoryOutOfBounds).
5044 Insert(builder)
5045 }