forked from WebKit/WebKit
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathInPlaceInterpreter.asm
6355 lines (5509 loc) · 153 KB
/
InPlaceInterpreter.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# IPInt: the WASM in-place interpreter
# DISCLAIMER: not tested on x86 yet (as of 05 Jul 2023); IPInt may break *very* badly.
#
# docs by Daniel Liu <[email protected] / [email protected]>; 2023 intern project
#
# 0. OfflineASM:
# --------------
#
# For a crash course on OfflineASM, check out LowLevelInterpreter.asm.
#
# 1. Code Structure
# -----------------
#
# IPInt is designed to start up quickly and interpret WASM code efficiently. To optimize for speed, we utilize a jump
# table, using the opcode's first byte as an offset. This jump table is set up in _ipint_setup.
# For more complex opcodes (ex. SIMD), we define additional jump tables that utilize further bytes as indices.
#
# 2. Setting Up
# -------------
#
# Before we can execute WebAssembly, we have to handle the call frame that is given to us. This is handled in _ipint_entry.
# We start by saving registers to the stack as per the system calling convention. Then, we have IPInt specific logic:
#
# 2.1. Locals
# -----------
#
# To ensure that we are able to access local variables quickly, we allocate a section of the stack to store local variables.
# We allocate 8 bytes for each local variable on the stack.
#
# Additionally, we need to load the parameters to the function into local variables. As per the calling convention, arguments
# are passed via registers, and then on the stack if all argument registers have been exhausted. Thus, we need to handle those
# cases. We keep track of the number of arguments in IPIntCallee, allowing us to know exactly where to load arguments from.
#
# Finally, we set the value of the `PL` (pointer to locals) register to the position of the first local. This allows us to quickly
# index into locals.
#
# 2.2. Bytecode and Metadata
# --------------------------
#
# The final step before executing is to load the bytecode to execute, as well as the metadata. For an explanation of why we use
# metadata in IPInt, check out WasmIPIntGenerator.cpp. We load these into registers `PB` (pointer to bytecode) and `PM` (pointer
# to metadata). Additionally, registers `PC` (program counter) and `MC` (metadata counter) are set to 0.
#
# 3. Executing WebAssembly
# ------------------------
#
# WebAssembly execution revolves around a stack machine, which we run on the program stack. We work with the constraint
# that the stack must be 16B aligned by ensuring that pushes and pops are always 16B. This makes certain opcodes (ex. drop)
# much easier as well.
#
# For each instruction, we align its assembly to a 256B boundary. Thus, we can take (address of instruction 0) + opcode * 256
# to find the exact point where we need to jump for each opcode without any dependent loads.
#
# 4. Returning
# ------------
#
# To return values to the caller, IPInt uses the standard WebAssembly calling convention. Return values are passed in the
# system return registers, and on the stack if not possible. After this, we perform cleanup logic to reset the stack to its
# original state, and return to the caller.
#
#################################
# Register and Size Definitions #
#################################
# PC = t4
const MC = t5 # Metadata counter (index into metadata)
const PL = t6 # Pointer to locals (index into locals)
const PM = metadataTable
if ARM64 or ARM64E
const IB = t7 # instruction base
end
# TODO: SIMD support, since locals will need double the space. Can we do it only sometimes?
# May just need to write metadata that rewrites offsets. May be worth the space savings.
# Actually, what if we just use the same thing but have a SIMD section separately allocated that
# is "pointed" to by the 8B entries on the stack? Easier and we only need to allocate SIMD when we need
# instead of blowing up the stack. Argument copying a little trickier though.
const PtrSize = constexpr (sizeof(void*))
const MachineRegisterSize = constexpr (sizeof(CPURegister))
const SlotSize = constexpr (sizeof(Register))
const LocalSize = SlotSize
const StackValueSize = 16
if X86_64 or ARM64 or ARM64E or RISCV64
const wasmInstance = csr0
const memoryBase = csr3
const boundsCheckingSize = csr4
elsif ARMv7
const wasmInstance = csr0
const memoryBase = invalidGPR
const boundsCheckingSize = invalidGPR
else
end
const UnboxedWasmCalleeStackSlot = CallerFrame - constexpr Wasm::numberOfIPIntCalleeSaveRegisters * SlotSize - MachineRegisterSize
##########
# Macros #
##########
# Callee Save
# FIXME: This happens to work because UnboxedWasmCalleeStackSlot sits in the extra space we should be more precise in case we want to use an even number of callee saves in the future.
const IPIntCalleeSaveSpaceStackAligned = 2*CalleeSaveSpaceStackAligned
macro saveIPIntRegisters()
subp IPIntCalleeSaveSpaceStackAligned, sp
if ARM64 or ARM64E
storepairq PM, PB, -16[cfr]
storeq wasmInstance, -24[cfr]
elsif X86_64 or RISCV64
storep PB, -0x8[cfr]
storep PM, -0x10[cfr]
storep wasmInstance, -0x18[cfr]
else
end
end
macro restoreIPIntRegisters()
if ARM64 or ARM64E
loadpairq -16[cfr], PM, PB
loadq -24[cfr], wasmInstance
elsif X86_64 or RISCV64
loadp -0x8[cfr], PB
loadp -0x10[cfr], PM
loadp -0x18[cfr], wasmInstance
else
end
addp IPIntCalleeSaveSpaceStackAligned, sp
end
# Get IPIntCallee object at startup
macro getIPIntCallee()
loadp Callee[cfr], ws0
if JSVALUE64
andp ~(constexpr JSValue::NativeCalleeTag), ws0
end
leap WTFConfig + constexpr WTF::offsetOfWTFConfigLowestAccessibleAddress, ws1
loadp [ws1], ws1
addp ws1, ws0
storep ws0, UnboxedWasmCalleeStackSlot[cfr]
end
# Tail-call dispatch
macro advancePC(amount)
addq amount, PC
end
macro advancePCByReg(amount)
addq amount, PC
end
macro advanceMC(amount)
addq amount, MC
end
macro advanceMCByReg(amount)
addq amount, MC
end
macro nextIPIntInstruction()
# Consistency check
# move MC, t0
# andq 7, t0
# bqeq t0, 0, .fine
# break
# .fine:
loadb [PB, PC, 1], t0
if ARM64 or ARM64E
# x7 = IB
# x0 = opcode
emit "add x0, x7, x0, lsl #8"
emit "br x0"
elsif X86_64
lshiftq 8, t0
leap (_ipint_unreachable), t1
addq t1, t0
emit "jmp *(%eax)"
else
break
end
end
# Stack operations
# Every value on the stack is always 16 bytes! This makes life easy.
macro pushQuad(reg)
if ARM64 or ARM64E
push reg, reg
else
push reg
end
end
macro pushQuadPair(reg1, reg2)
push reg1, reg2
end
macro popQuad(reg, scratch)
if ARM64 or ARM64E
pop reg, scratch
else
pop reg
end
end
macro pushVectorReg0()
if ARM64 or ARM64E
emit "str q0, [sp, #-16]!"
else
emit "sub $16, %esp"
emit "movdqu %xmm0, (%esp)"
end
end
macro pushVectorReg1()
if ARM64 or ARM64E
emit "str q1, [sp, #-16]!"
else
emit "sub $16, %esp"
emit "movdqu %xmm1, (%esp)"
end
end
macro pushVectorReg2()
if ARM64 or ARM64E
emit "str q2, [sp, #-16]!"
else
emit "sub $16, %esp"
emit "movdqu %xmm2, (%esp)"
end
end
macro popVectorReg0()
if ARM64 or ARM64E
emit "ldr q0, [sp], #16"
elsif X86_64
emit "movdqu (%esp), %xmm0"
emit "add $16, %esp"
end
end
macro popVectorReg1()
if ARM64 or ARM64E
emit "ldr q1, [sp], #16"
elsif X86_64
emit "movdqu (%esp), %xmm1"
emit "add $16, %esp"
end
end
macro popVectorReg2()
if ARM64 or ARM64E
emit "ldr q2, [sp], #16"
elsif X86_64
emit "movdqu (%esp), %xmm2"
emit "add $16, %esp"
end
end
# Pushes ft0 because macros
macro pushFPR()
if ARM64 or ARM64E
emit "str q0, [sp, #-16]!"
else
emit "sub $16, %esp"
emit "movdqu %xmm0, (%esp)"
end
end
macro pushFPR1()
if ARM64 or ARM64E
emit "str q1, [sp, #-16]!"
else
emit "sub $16, %esp"
emit "movdqu %xmm1, (%esp)"
end
end
macro popFPR()
if ARM64 or ARM64E
# We'll just drop the entire q0 register in here
# to keep stack aligned to 16
# We'll never actually use q0 as a whole for FP,
# since we only work with f32 (s0) or f64 (d0)
emit "ldr q0, [sp], #16"
elsif X86_64
emit "movdqu (%esp), %xmm0"
emit "add $16, %esp"
end
end
macro popFPR1()
if ARM64 or ARM64E
emit "ldr q1, [sp], #16"
elsif X86_64
emit "movdqu (%esp), %xmm1"
emit "add $16, %esp"
end
end
# Typed push/pop to make code pretty
macro pushInt32(reg)
pushQuad(reg)
end
macro popInt32(reg, scratch)
popQuad(reg, scratch)
end
macro pushInt64(reg)
pushQuad(reg)
end
macro popInt64(reg, scratch)
popQuad(reg, scratch)
end
macro pushFloat32FT0()
pushFPR()
end
macro pushFloat32FT1()
pushFPR1()
end
macro popFloat32FT0()
popFPR()
end
macro popFloat32FT1()
popFPR1()
end
macro pushFloat64FT0()
pushFPR()
end
macro pushFloat64FT1()
pushFPR1()
end
macro popFloat64FT0()
popFPR()
end
macro popFloat64FT1()
popFPR1()
end
# Instruction labels
# Important Note: If you don't use the unaligned global label from C++ (in our case we use the
# labels in InPlaceInterpreter.cpp) then some linkers will still remove the definition which
# causes all kinds of problems.
# FIXME: switch offlineasm unalignedglobal to take alignment and optionally pad with breakpoint instructions (rdar://113594783)
macro alignment()
if ARM64 or ARM64E
# fill with brk instructions
emit ".balignl 256, 0xd4388e20"
elsif X86_64
# fill with int 3 instructions
emit ".balign 256, 0xcc"
end
end
macro instructionLabel(instrname)
alignment()
unalignedglobal _ipint%instrname%_validate
_ipint%instrname%:
_ipint%instrname%_validate:
end
macro unimplementedInstruction(instrname)
alignment()
instructionLabel(instrname)
break
end
macro reservedOpcode(opcode)
alignment()
break
end
# Memory
macro ipintReloadMemory()
if ARM64 or ARM64E
loadpairq Wasm::Instance::m_cachedMemory[wasmInstance], memoryBase, boundsCheckingSize
else
loadp Wasm::Instance::m_cachedMemory[wasmInstance], memoryBase
loadp Wasm::Instance::m_cachedBoundsCheckingSize[wasmInstance], boundsCheckingSize
end
if not ARMv7
cagedPrimitiveMayBeNull(memoryBase, boundsCheckingSize, t2, t3)
end
end
# Operation Calls
macro operationCall(fn)
move wasmInstance, a0
push PC, MC
push PL, ws0
fn()
pop ws0, PL
pop MC, PC
if ARM64 or ARM64E
pcrtoaddr _ipint_unreachable, IB
end
end
macro operationCallMayThrow(fn)
storei PC, CallSiteIndex[cfr]
move wasmInstance, a0
push PC, MC
push PL, ws0
fn()
bqneq r0, 1, .continuation
storei r1, ArgumentCountIncludingThis + PayloadOffset[cfr]
jmp _wasm_throw_from_slow_path_trampoline
.continuation:
pop ws0, PL
pop MC, PC
if ARM64 or ARM64E
pcrtoaddr _ipint_unreachable, IB
end
end
# Exception handling
macro ipintException(exception)
# move PL, sp
# loadi Wasm::IPIntCallee::m_localSizeToAlloc[ws0], PM
# mulq SlotSize, PM
# addq PM, sp
# restoreCallerPCAndCFR()
storei constexpr Wasm::ExceptionType::%exception%, ArgumentCountIncludingThis + PayloadOffset[cfr]
jmp _wasm_throw_from_slow_path_trampoline
end
# OSR
macro ipintPrologueOSR(increment)
if JIT
loadp UnboxedWasmCalleeStackSlot[cfr], ws0
baddis increment, Wasm::IPIntCallee::m_tierUpCounter + Wasm::LLIntTierUpCounter::m_counter[ws0], .continue
subq (NumberOfWasmArgumentJSRs + NumberOfWasmArgumentFPRs) * 8, sp
if ARM64 or ARM64E
forEachArgumentJSR(macro (offset, gpr1, gpr2)
storepairq gpr2, gpr1, offset[sp]
end)
elsif JSVALUE64
forEachArgumentJSR(macro (offset, gpr)
storeq gpr, offset[sp]
end)
else
forEachArgumentJSR(macro (offset, gprMsw, gpLsw)
store2ia gpLsw, gprMsw, offset[sp]
end)
end
if ARM64 or ARM64E
forEachArgumentFPR(macro (offset, fpr1, fpr2)
storepaird fpr2, fpr1, offset[sp]
end)
else
forEachArgumentFPR(macro (offset, fpr)
stored fpr, offset[sp]
end)
end
ipintReloadMemory()
push memoryBase, boundsCheckingSize
move cfr, a1
operationCall(macro() cCall2(_ipint_extern_prologue_osr) end)
move r0, ws0
pop boundsCheckingSize, memoryBase
if ARM64 or ARM64E
forEachArgumentFPR(macro (offset, fpr1, fpr2)
loadpaird offset[sp], fpr2, fpr1
end)
else
forEachArgumentFPR(macro (offset, fpr)
loadd offset[sp], fpr
end)
end
if ARM64 or ARM64E
forEachArgumentJSR(macro (offset, gpr1, gpr2)
loadpairq offset[sp], gpr2, gpr1
end)
elsif JSVALUE64
forEachArgumentJSR(macro (offset, gpr)
loadq offset[sp], gpr
end)
else
forEachArgumentJSR(macro (offset, gprMsw, gpLsw)
load2ia offset[sp], gpLsw, gpMsw
end)
end
addq (NumberOfWasmArgumentJSRs + NumberOfWasmArgumentFPRs) * 8, sp
btpz ws0, .recover
restoreIPIntRegisters()
restoreCallerPCAndCFR()
if ARM64E
leap _g_config, ws1
jmp JSCConfigGateMapOffset + (constexpr Gate::wasmOSREntry) * PtrSize[ws1], NativeToJITGatePtrTag # WasmEntryPtrTag
else
jmp ws0, WasmEntryPtrTag
end
.recover:
loadp UnboxedWasmCalleeStackSlot[cfr], ws0
.continue:
end
end
macro ipintLoopOSR(increment)
if JIT
loadp UnboxedWasmCalleeStackSlot[cfr], ws0
baddis increment, Wasm::IPIntCallee::m_tierUpCounter + Wasm::LLIntTierUpCounter::m_counter[ws0], .continue
move cfr, a1
move PC, a2
# Add 1 to the index due to WTF::HashMap not supporting 0 as a key
addq 1, a2
move PL, a3
operationCall(macro() cCall4(_ipint_extern_loop_osr) end)
btpz r1, .recover
restoreIPIntRegisters()
restoreCallerPCAndCFR()
move r0, a0
if ARM64E
move r1, ws0
leap _g_config, ws1
jmp JSCConfigGateMapOffset + (constexpr Gate::wasmOSREntry) * PtrSize[ws1], NativeToJITGatePtrTag # WasmEntryPtrTag
else
jmp r1, WasmEntryPtrTag
end
.recover:
loadp UnboxedWasmCalleeStackSlot[cfr], ws0
.continue:
end
end
macro ipintEpilogueOSR(increment)
if JIT
loadp UnboxedWasmCalleeStackSlot[cfr], ws0
baddis increment, Wasm::IPIntCallee::m_tierUpCounter + Wasm::LLIntTierUpCounter::m_counter[ws0], .continue
move cfr, a1
operationCall(macro() cCall2(_ipint_extern_epilogue_osr) end)
.continue:
end
end
macro decodeLEBVarUInt32(offset, dst, scratch1, scratch2, scratch3, scratch4)
# if it's a single byte, fastpath it
const tempPC = scratch4
leap offset[PC], tempPC
loadb [PB, tempPC], dst
bbb dst, 0x80, .fastpath
# otherwise, set up for second iteration
# next shift is 7
move 7, scratch1
# take off high bit
subi 0x80, dst
.loop:
addp 1, tempPC
loadb [PB, tempPC], scratch2
# scratch3 = high bit 7
# leave scratch2 with low bits 6-0
move 0x80, scratch3
andi scratch2, scratch3
xori scratch3, scratch2
lshifti scratch1, scratch2
addi 7, scratch1
ori scratch2, dst
bbneq scratch3, 0, .loop
.fastpath:
end
########################
# In-Place Interpreter #
########################
# FIXME: switch offlineasm unalignedglobal to take alignment and optionally pad with breakpoint instructions (rdar://113594783)
macro argumINTAlign()
emit ".balign 64"
end
macro argumINTDispatch()
loadb [PM], csr0
addq 1, PM
lshiftq 6, csr0
if ARM64 or ARM64E
pcrtoaddr _argumINT_a0, csr4
addq csr0, csr4
emit "br x23"
elsif X86_64
leap (_argumINT_a0), csr4
addq csr0, csr4
emit "jmp *(%r13)"
end
end
global _ipint_entry
_ipint_entry:
if WEBASSEMBLY and (ARM64 or ARM64E or X86_64)
preserveCallerPCAndCFR()
saveIPIntRegisters()
storep wasmInstance, CodeBlock[cfr]
getIPIntCallee()
# Allocate space for locals and rethrow values
if ARM64 or ARM64E
loadpairi Wasm::IPIntCallee::m_localSizeToAlloc[ws0], csr0, csr3
else
loadi Wasm::IPIntCallee::m_localSizeToAlloc[ws0], csr0
loadi Wasm::IPIntCallee::m_numRethrowSlotsToAlloc[ws0], csr3
end
addq csr3, csr0
mulq LocalSize, csr0
move sp, csr3
subq csr0, sp
move sp, csr4
loadp Wasm::IPIntCallee::m_argumINTBytecodePointer[ws0], PM
push csr0, csr1, csr2, csr3
# PM = location in argumINT bytecode
# csr0 = tmp
# csr1 = dst
# csr2 = src
# csr3 = end
# csr4 = for dispatch
const argumINTDest = csr1
const argumINTSrc = csr2
move csr4, argumINTDest
leap FirstArgumentOffset[cfr], argumINTSrc
argumINTDispatch()
.ipint_entry_end_local:
# zero out remaining locals
bqeq argumINTDest, csr3, .ipint_entry_finish_zero
storeq 0, [argumINTDest]
addq 8, argumINTDest
jmp .ipint_entry_end_local
.ipint_entry_finish_zero:
pop csr3, csr2, csr1, csr0
loadp CodeBlock[cfr], wasmInstance
# OSR Check
ipintPrologueOSR(5)
move sp, PL
if ARM64 or ARM64E
pcrtoaddr _ipint_unreachable, IB
end
loadp Wasm::IPIntCallee::m_bytecode[ws0], PB
move 0, PC
loadp Wasm::IPIntCallee::m_metadata[ws0], PM
move 0, MC
# Load memory
ipintReloadMemory()
nextIPIntInstruction()
.ipint_exit:
# Clean up locals
# Don't overwrite the return registers
# Will use PM as a temp because we don't want to use the actual temps.
# move PL, sp
# loadi Wasm::IPIntCallee::m_localSizeToAlloc[ws0], PM
# mulq LocalSize, PM
# addq PM, sp
ipintReloadMemory()
restoreIPIntRegisters()
restoreCallerPCAndCFR()
ret
else
ret
end
global _ipint_entry_simd
_ipint_entry_simd:
if WEBASSEMBLY and (ARM64 or ARM64E or X86_64)
preserveCallerPCAndCFR()
saveIPIntRegisters()
storep wasmInstance, CodeBlock[cfr]
getIPIntCallee()
# Allocate space for locals and rethrow values
if ARM64 or ARM64E
loadpairi Wasm::IPIntCallee::m_localSizeToAlloc[ws0], csr0, csr3
else
loadi Wasm::IPIntCallee::m_localSizeToAlloc[ws0], csr0
loadi Wasm::IPIntCallee::m_numRethrowSlotsToAlloc[ws0], csr3
end
addq csr3, csr0
mulq LocalSize, csr0
move sp, csr3
subq csr0, sp
move sp, csr4
loadp Wasm::IPIntCallee::m_argumINTBytecodePointer[ws0], PM
push csr0, csr1, csr2, csr3
# PM = location in argumINT bytecode
# csr0 = tmp
# csr1 = dst
# csr2 = src
# csr3 = end
# csr4 = for dispatch
const argumINTDest = csr1
const argumINTSrc = csr2
move csr4, argumINTDest
leap FirstArgumentOffset[cfr], argumINTSrc
argumINTDispatch()
.ipint_entry_end_local_simd:
# zero out remaining locals
bqeq argumINTDest, csr3, .ipint_entry_finish_zero_simd
storeq 0, [argumINTDest]
addq 8, argumINTDest
jmp .ipint_entry_end_local_simd
.ipint_entry_finish_zero_simd:
pop csr3, csr2, csr1, csr0
loadp CodeBlock[cfr], wasmInstance
# OSR Check
ipintPrologueOSR(5)
move sp, PL
if ARM64 or ARM64E
pcrtoaddr _ipint_unreachable, IB
end
loadp Wasm::IPIntCallee::m_bytecode[ws0], PB
move 0, PC
loadp Wasm::IPIntCallee::m_metadata[ws0], PM
move 0, MC
# Load memory
ipintReloadMemory()
nextIPIntInstruction()
else
ret
end
macro ipintCatchCommon()
getVMFromCallFrame(t3, t0)
restoreCalleeSavesFromVMEntryFrameCalleeSavesBuffer(t3, t0)
loadp VM::callFrameForCatch[t3], cfr
storep 0, VM::callFrameForCatch[t3]
loadp VM::targetInterpreterPCForThrow[t3], PC
loadp VM::targetInterpreterMetadataPCForThrow[t3], MC
getIPIntCallee()
loadp CodeBlock[cfr], wasmInstance
if ARM64 or ARM64E
pcrtoaddr _ipint_unreachable, IB
end
loadp Wasm::IPIntCallee::m_bytecode[ws0], PB
loadp Wasm::IPIntCallee::m_metadata[ws0], PM
# Recompute PL
if ARM64 or ARM64E
loadpairi Wasm::IPIntCallee::m_localSizeToAlloc[ws0], t0, t1
else
loadi Wasm::IPIntCallee::m_localSizeToAlloc[ws0], t0
loadi Wasm::IPIntCallee::m_numRethrowSlotsToAlloc[ws0], t1
end
addq t1, t0
# FIXME: Can this be an leaq?
mulq LocalSize, t0
addq IPIntCalleeSaveSpaceStackAligned, t0
subq cfr, t0, PL
loadi [PM, MC], t0
# 1 << 4 == StackValueSize
lshiftq 4, t0
addq IPIntCalleeSaveSpaceStackAligned, t0
subp cfr, t0, t0
move t0, sp
end
global _ipint_catch_entry
_ipint_catch_entry:
if WEBASSEMBLY and (ARM64 or ARM64E or X86_64)
ipintCatchCommon()
move cfr, a1
move sp, a2
move PL, a3
operationCall(macro() cCall4(_ipint_extern_retrieve_and_clear_exception) end)
ipintReloadMemory()
advanceMC(4)
nextIPIntInstruction()
end
global _ipint_catch_all_entry
_ipint_catch_all_entry:
if WEBASSEMBLY and (ARM64 or ARM64E or X86_64)
ipintCatchCommon()
move cfr, a1
move 0, a2
move PL, a3
operationCall(macro() cCall4(_ipint_extern_retrieve_and_clear_exception) end)
ipintReloadMemory()
advanceMC(4)
nextIPIntInstruction()
end
if WEBASSEMBLY and (ARM64 or ARM64E or X86_64)
# Put all instructions after this `if`, or 32 bit will fail to build.
#############################
# 0x00 - 0x11: control flow #
#############################
instructionLabel(_unreachable)
# unreachable
ipintException(Unreachable)
instructionLabel(_nop)
# nop
advancePC(1)
nextIPIntInstruction()
instructionLabel(_block)
# block
loadi [PM, MC], PC
loadi 4[PM, MC], MC
nextIPIntInstruction()
instructionLabel(_loop)
# loop
ipintLoopOSR(1)
loadb [PM, MC], t0
advancePCByReg(t0)
advanceMC(1)
nextIPIntInstruction()
instructionLabel(_if)
# if
popInt32(t0, t1)
bqneq 0, t0, .ipint_if_taken
loadi [PM, MC], PC
loadi 4[PM, MC], MC
nextIPIntInstruction()
.ipint_if_taken:
# Skip LEB128
loadb 8[PM, MC], t0
advanceMC(9)
advancePCByReg(t0)
nextIPIntInstruction()
instructionLabel(_else)
# else
# Counterintuitively, we only run this instruction if the if
# clause is TAKEN. This is used to branch to the end of the
# block.
loadi [PM, MC], PC
loadi 4[PM, MC], MC
nextIPIntInstruction()
instructionLabel(_try)
loadb [PM, MC], t0
advancePCByReg(t0)
advanceMC(1)
nextIPIntInstruction()
instructionLabel(_catch)
# Counterintuitively, like else, we only run this instruction
# if no exception was thrown during the preceeding try or catch block.
loadi [PM, MC], PC
loadi 4[PM, MC], MC
nextIPIntInstruction()
instructionLabel(_throw)
storei PC, CallSiteIndex[cfr]
loadp Wasm::Instance::m_vm[wasmInstance], t0
loadp VM::topEntryFrame[t0], t0
copyCalleeSavesToEntryFrameCalleeSavesBuffer(t0)
move cfr, a1
move sp, a2
loadi [PM, MC], a3
operationCall(macro() cCall4(_ipint_extern_throw_exception) end)
jumpToException()
instructionLabel(_rethrow)
storei PC, CallSiteIndex[cfr]
loadp Wasm::Instance::m_vm[wasmInstance], t0
loadp VM::topEntryFrame[t0], t0
copyCalleeSavesToEntryFrameCalleeSavesBuffer(t0)
move cfr, a1
move PL, a2
loadi [PM, MC], a3
operationCall(macro() cCall4(_ipint_extern_rethrow_exception) end)
jumpToException()
reservedOpcode(0x0a)
# FIXME: switch offlineasm unalignedglobal to take alignment and optionally pad with breakpoint instructions (rdar://113594783)
macro uintAlign()
emit ".balign 64"
end
macro uintDispatch()
if ARM64 or ARM64E
loadb [PM], ws2
addq 1, PM
bilt ws2, 5, .safe
break
.safe:
lshiftq 6, ws2
pcrtoaddr _uint_r0, ws3
addq ws2, ws3
# ws3 = x12
emit "br x12"
elsif X86_64
loadb [PM], r1
addq 1, PM
bilt r1, 5, .safe
break
.safe:
lshiftq 6, r1
leap (_uint_r0), t0
addq r1, t0
emit "jmp *(%rax)"
end
end
instructionLabel(_end)
loadi Wasm::IPIntCallee::m_bytecodeLength[ws0], t0
subq 1, t0
bqeq PC, t0, .ipint_end_ret
advancePC(1)
nextIPIntInstruction()
.ipint_end_ret:
ipintEpilogueOSR(10)
addq MC, PM
uintDispatch()
instructionLabel(_br)
# br
# number to pop
loadh 8[PM, MC], t0
# number to keep
loadh 10[PM, MC], t1
# ex. pop 3 and keep 2
#
# +4 +3 +2 +1 sp
# a b c d e
# d e
#
# [sp + k + numToPop] = [sp + k] for k in numToKeep-1 -> 0
move t0, t2
lshiftq 4, t2
leap [sp, t2], t2