forked from WebKit/WebKit
-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathAssemblyHelpers.cpp
More file actions
2545 lines (2237 loc) · 111 KB
/
Copy pathAssemblyHelpers.cpp
File metadata and controls
2545 lines (2237 loc) · 111 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
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
/*
* Copyright (C) 2011-2025 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "AssemblyHelpers.h"
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN
#if ENABLE(JIT)
#include "AccessCase.h"
#include "AssemblyHelpersSpoolers.h"
#include "BaselineJITCode.h"
#include "ConcurrentButterflyOperations.h"
#include "JITOperations.h"
#include "JSArrayBufferView.h"
#include "JSCConfig.h"
#include "JSCJSValueInlines.h"
#include "JSDataView.h"
#include "LinkBuffer.h"
#include "MaxFrameExtentForSlowPathCall.h"
#include "MegamorphicCache.h"
#include "SuperSampler.h"
#include "ThunkGenerators.h"
#include "UnlinkedCodeBlock.h"
#include <atomic>
#include <mutex>
#if OS(DARWIN) && ENABLE(FAST_TLS_JIT)
#include <wtf/FastTLS.h>
#endif
#if ENABLE(WEBASSEMBLY)
#include "JSWebAssemblyInstance.h"
#include "WasmContext.h"
#include "WasmMemoryInformation.h"
#endif
namespace JSC {
namespace AssemblyHelpersInternal {
constexpr bool dumpVerbose = false;
// SPEC-jit section 5.5 (Task 8): frozen butterfly-tag encoding
// (SPEC-objectmodel section 2). Mirrored locally to keep this file
// independent of the object-model workstream's header; the values are FROZEN.
[[maybe_unused]] constexpr uint64_t butterflyTagPointerMask = 0x0000ffffffffffffULL;
[[maybe_unused]] constexpr uint64_t butterflyTagFloor = 1ULL << 48; // any word >= this carries tag bits
// Flag-on guard for the LEGACY (no slow-path list) property accessors: these
// remain reachable only from emissions whose butterflies are provably
// tag-free (gated transition/delete/megamorphic forms - see the Task 8 site
// inventory in docs/threads/INTEGRATE-jit.md) and from DFG/FTL emitters that
// Tasks 9/10 convert. A tagged or segmented word traps here instead of being
// misread as a pointer (I14 enforcement-by-construction; flag-off this emits
// nothing). Flag-on callers with a slow path must use
// CCallHelpers::loadProperty/storeProperty(..., slowCases) or the
// loadButterflyForRead/ForWrite choke points.
static void emitLegacyButterflyTagTrap(AssemblyHelpers& jit, GPRReg butterflyGPR)
{
#if USE(JSVALUE64)
if (Options::useJSThreads()) [[unlikely]] {
auto untagged = jit.branch64(AssemblyHelpers::Below, butterflyGPR, AssemblyHelpers::TrustedImm64(static_cast<int64_t>(butterflyTagFloor)));
jit.breakpoint();
untagged.link(&jit);
}
#else
UNUSED_PARAM(jit);
UNUSED_PARAM(butterflyGPR);
#endif
}
// SPEC-jit section 5.5 Task 8 (I14(a)) + r48 (FUZZ.md / SCALEBENCH §48):
// load the ArrayBuffer* from a Wasteful typed-array view's butterfly,
// flag-on segment-aware. The previous "typed-array wasteful-mode butterflies
// are never segmented" claim is FALSE — a foreign-thread named-property add
// that escapes E4 (TTL fired / foreign TID) and grows outOfLineCapacity
// segments via trySegmentedTransition (the §44 StayFlatShared gate requires
// !hasIndexingHeader, which a Wasteful view HAS). Under that conversion the
// IndexingHeader (and its u.typedArray.buffer) lives at indexed fragment 0
// slot 0 (§4.1; the I8 alias equation pins it to the flat-era B-8 location,
// so the buffer pointer survives the conversion verbatim). resultGPR ==
// arrayBuffer on exit; scratchGPR is clobbered flag-on (callers re-load
// m_mode afterwards). Flag-off codegen unchanged: one masked loadPtr at
// [butterfly + offsetOfArrayBuffer].
static void emitLoadTypedArrayArrayBuffer(AssemblyHelpers& jit, GPRReg baseGPR, GPRReg resultGPR, [[maybe_unused]] GPRReg scratchGPR)
{
jit.loadPtr(AssemblyHelpers::Address(baseGPR, JSObject::butterflyOffset()), resultGPR);
#if USE(JSVALUE64)
if (Options::useJSThreads()) [[unlikely]] {
ASSERT(scratchGPR != resultGPR && scratchGPR != baseGPR);
// Segmented iff TID == notTTLTID (the all-ones TID, I3; payload != 0
// is given by m_mode==Wasteful => the wastage install happened).
constexpr uint64_t tidMask = 0x7fffULL << 48;
static_assert(tidMask == JSC::butterflyTIDMask);
jit.move(AssemblyHelpers::TrustedImm64(static_cast<int64_t>(tidMask)), scratchGPR);
jit.and64(resultGPR, scratchGPR);
auto notSegmented = jit.branch64(AssemblyHelpers::NotEqual, scratchGPR, AssemblyHelpers::TrustedImm64(static_cast<int64_t>(tidMask)));
// Segmented: resultGPR = spine after mask; arrayBuffer is at
// [indexedFragment(0) + 0] = [[spine + 32 + outOfLineFragmentCount*8] + 0].
jit.and64(AssemblyHelpers::TrustedImm64(static_cast<int64_t>(butterflyTagPointerMask)), resultGPR);
jit.load32(AssemblyHelpers::Address(resultGPR, 0), scratchGPR); // ButterflySpine::outOfLineFragmentCount
static_assert(!OBJECT_OFFSETOF(ButterflySpine, outOfLineFragmentCount));
static_assert(sizeof(ButterflySpine) == 32 && sizeof(void*) == 8);
jit.loadPtr(AssemblyHelpers::BaseIndex(resultGPR, scratchGPR, AssemblyHelpers::TimesEight, sizeof(ButterflySpine)), resultGPR); // indexedFragment(0)
static_assert(!OBJECT_OFFSETOF(ButterflyFragment, slots));
static_assert(!IndexingHeader::offsetOfArrayBuffer()); // u.typedArray.buffer at offset 0 of the header slot.
jit.loadPtr(AssemblyHelpers::Address(resultGPR, 0), resultGPR);
auto done = jit.jump();
notSegmented.link(&jit);
jit.and64(AssemblyHelpers::TrustedImm64(static_cast<int64_t>(butterflyTagPointerMask)), resultGPR);
jit.loadPtr(AssemblyHelpers::Address(resultGPR, Butterfly::offsetOfArrayBuffer()), resultGPR);
done.link(&jit);
return;
}
#endif
jit.loadPtr(AssemblyHelpers::Address(resultGPR, Butterfly::offsetOfArrayBuffer()), resultGPR);
}
}
// ===========================================================================
// UNGIL §A.1.1 (SPEC-ungil A.1.1; UNGIL-HANDOUT U-T3): loadVMLite — the
// one-load read of the current thread's VMLite* (the §A.1.3 GIL-off Group-3
// base; vmstate L4). Per-OS mechanics mirror SPEC-jit annex App. R5 exactly
// (the loadButterflyTIDTag precedent, jit/CCallHelpers.cpp /
// jit/ConcurrentButterflyOperations.cpp):
//
// - ELF (Linux glibc+musl, x86-64/arm64): one load off the thread register
// at the initial-exec TLS offset of g_jscCurrentVMLite (the JIT/LLInt-
// visible mirror of VMLite.cpp's `t_currentVMLite`, defined in
// runtime/VM.cpp; VMLite::setCurrent keeps it coherent — the same
// post-TLS-write discipline as the App. R5 CS3 TID-tag hook). IE-model
// offsets are link-time thread-invariant; we still latch the first
// computed value and RELEASE_ASSERT constancy at every emission (DFG/FTL
// emit from multiple compiler threads, so this re-checks the App. R5
// constancy property for free).
// - Darwin: Mach-O TLV has no constant offset, so the mirror lives in a
// pthread TSD slot whose key is published through the M4a-style JSCConfig
// slot (g_jscConfig.vmLiteTLSKey, beside butterflyTIDTagTLSKey;
// JSC_CONFIG_HAS_VMLITE_TLS_KEY). pthread_key_create + per-thread
// pthread_setspecific are the VMLite::setCurrent side's duty, exactly as
// for the TID tag.
// - Other (Windows): unsupported flag-on per App. R5 — no new story owed.
//
// REMATERIALIZATION is the correctness carrier (§A.1.2): every site needing
// the lite may simply re-emit this load; prologue temps and the
// VMEntryRecord::m_vmLite slot are optimizations only.
//
// Flag-off / GIL-on identity: nothing calls this emitter except gilOff-mode
// compilations (§A.1.3 COMPILED-FOR-VM-mode rule, U-T4a/U-T4b) — flag-off
// golden disasm is unchanged by this definition existing.
// ===========================================================================
#if OS(LINUX) && (CPU(X86_64) || CPU(ARM64))
// Defined in runtime/VM.cpp (initial-exec model there too); declared with the
// same language linkage + TLS model so the address-of below resolves to the
// thread-invariant TPOFF the baked immediate relies on.
extern "C" __attribute__((tls_model("initial-exec"))) thread_local VMLite* g_jscCurrentVMLite;
namespace {
ALWAYS_INLINE uintptr_t currentThreadPointerForVMLiteTLS()
{
uintptr_t threadPointer;
#if CPU(X86_64)
// x86-64 ELF TLS ABI (glibc and musl): the TCB self-pointer lives at
// %fs:0, so this single load yields the thread pointer the JIT's
// fs-prefixed loads are relative to.
asm volatile("movq %%fs:0, %0" : "=r"(threadPointer));
#elif CPU(ARM64)
asm volatile("mrs %0, tpidr_el0" : "=r"(threadPointer));
#endif
return threadPointer;
}
intptr_t currentVMLiteELFTLSOffset()
{
intptr_t offset = static_cast<intptr_t>(
reinterpret_cast<uintptr_t>(&g_jscCurrentVMLite) - currentThreadPointerForVMLiteTLS());
// Both per-arch loadFromELFTLS64 emitters encode the offset as a
// (sign-extended) disp32.
RELEASE_ASSERT(offset == static_cast<intptr_t>(static_cast<int32_t>(offset)));
static std::once_flag onceFlag;
static intptr_t latchedOffset;
std::call_once(onceFlag, [&] {
latchedOffset = offset;
});
// App. R5 constancy property, re-verified per emission (and hence across
// every compiler thread that ever emits this load).
RELEASE_ASSERT(latchedOffset == offset);
return offset;
}
} // anonymous namespace
#endif // OS(LINUX) && (CPU(X86_64) || CPU(ARM64))
// Free-function form: self-contained in this TU (compiles in any tree
// slice). The member spelling `jit.loadVMLite(reg)` — the surface U-T4a/b
// emission calls, mirroring CCallHelpers::loadButterflyTIDTag — forwards
// here once AssemblyHelpers.h (owned by the emission task per the U-T3/U-T4
// file split) declares it and defines JSC_ASSEMBLYHELPERS_HAS_LOAD_VMLITE
// beside the declaration (the JSC_CONFIG_HAS_BUTTERFLY_TID_TAG_TLS_KEY
// inversion pattern).
void loadVMLite(AssemblyHelpers&, GPRReg); // self-declaration (no header owns this form yet)
void loadVMLite(AssemblyHelpers& jit, GPRReg destGPR)
{
#if OS(LINUX) && (CPU(X86_64) || CPU(ARM64))
// ARM64 note (mirrors loadFromTLS64): the offset is materialized through
// destGPR itself for encodable offsets; load64 falls back to the data
// temp for unencodable ones, so destGPR must not be the data temp.
jit.loadFromELFTLS64(currentVMLiteELFTLSOffset(), destGPR);
#elif OS(DARWIN) && ENABLE(FAST_TLS_JIT)
#if defined(JSC_CONFIG_HAS_VMLITE_TLS_KEY)
// TSD slots are uniform, so direct-offset reads via fastTLSOffsetForKey
// are valid for dynamically created keys (App. R5 Darwin mechanics). The
// key is created (and the per-thread copy maintained) by the
// VMLite::setCurrent side before any gilOff-mode compilation can run —
// U0c fixes the mode pre-codegen, and a gilOff compilation implies a
// registered, installed lite existed.
uint32_t key = g_jscConfig.vmLiteTLSKey;
RELEASE_ASSERT(key);
jit.loadFromTLS64(fastTLSOffsetForKey(key), destGPR);
#else
// OPEN OBLIGATION (App. R5 Darwin mechanics, normative; escalated at
// the U-T3 amendment): the JSCConfig vmLiteTLSKey slot (+
// JSC_CONFIG_HAS_VMLITE_TLS_KEY beside it), its pthread_key_create at
// the P5-init point that creates the TID-tag key, and the
// VMLite::setCurrent-side per-thread pthread_setspecific are NOT YET
// LANDED — runtime/JSCConfig.h and runtime/VMLite.cpp are outside this
// slice's writable file set, and no IU obligation row yet names an
// owner. They MUST land (with an IU row) before any task emits this
// path on Darwin. Until then gilOff-mode compilation (the only caller)
// fail-stops here rather than emitting a wrong load.
UNUSED_PARAM(jit);
UNUSED_PARAM(destGPR);
RELEASE_ASSERT_NOT_REACHED();
#endif
#else
// App. R5: no JIT-visible TLS mechanism on this platform; useJSThreads
// (and a fortiori gilOffProcess) is unsupported here, so emission must
// never get this far.
UNUSED_PARAM(jit);
UNUSED_PARAM(destGPR);
RELEASE_ASSERT_NOT_REACHED();
#endif
}
#if defined(JSC_ASSEMBLYHELPERS_HAS_LOAD_VMLITE)
// Member surface (declared in AssemblyHelpers.h by the emission-task slice;
// the macro is defined beside the declaration). §A.1.2: callers may
// rematerialize freely — this is one TLS-relative load, no side effects.
void AssemblyHelpers::loadVMLite(GPRReg destGPR)
{
JSC::loadVMLite(*this, destGPR);
}
#endif
// SPEC-jit App. R5: hoisted from CCallHelpers so emitTagInstalledButterflyWithTID
// (and the emitAllocateJSObject* templates) can call it. Mechanics are the
// loadVMLite precedent above (ELF IE-TLS / Darwin TSD, offset baked at
// emission via butterflyTIDTagELFTLSOffset / butterflyTIDTagTLSKey from
// ConcurrentButterflyOperations.h). CCallHelpers / SpeculativeJIT inherit it
// unchanged, so every existing call site (loadButterflyForWrite, the DFG/FTL
// §5.5 predicate emitters, the FTL loadButterflyTIDTag patchpoint generator)
// keeps compiling.
void AssemblyHelpers::loadButterflyTIDTag(GPRReg destGPR)
{
#if OS(LINUX) && (CPU(X86_64) || CPU(ARM64))
loadFromELFTLS64(butterflyTIDTagELFTLSOffset(), destGPR);
#elif OS(DARWIN) && ENABLE(FAST_TLS_JIT)
loadFromTLS64(fastTLSOffsetForKey(butterflyTIDTagTLSKey()), destGPR);
#else
// D8/App. R5: no JIT-visible TLS mechanism; useJSThreads is unsupported
// on this platform, so emission must never get here.
UNUSED_PARAM(destGPR);
RELEASE_ASSERT_NOT_REACHED();
#endif
}
// Task-8 (SPEC-objectmodel §2.1): see the declaration's comment. Kept
// out-of-line so AssemblyHelpers.h doesn't need ConcurrentButterflyOperations.h.
void AssemblyHelpers::emitTagInstalledButterflyWithTID(GPRReg resultGPR, GPRReg storageGPR, GPRReg scratchGPR)
{
#if USE(JSVALUE64)
ASSERT(scratchGPR != resultGPR);
ASSERT(scratchGPR != storageGPR);
loadButterflyTIDTag(scratchGPR);
// scratchGPR := (currentButterflyTID() << 48) | storageGPR — i.e.
// encodeButterfly(storageGPR, currentButterflyTID(), false). storageGPR is
// left untagged for the caller's post-install header/element writes.
or64(storageGPR, scratchGPR);
storePtr(scratchGPR, Address(resultGPR, JSObject::butterflyOffset()));
#else
UNUSED_PARAM(resultGPR);
UNUSED_PARAM(storageGPR);
UNUSED_PARAM(scratchGPR);
RELEASE_ASSERT_NOT_REACHED(); // flag-on requires 64-bit (D8)
#endif
}
AssemblyHelpers::Jump AssemblyHelpers::branchIfFastTypedArray(GPRReg baseGPR)
{
return branch8(
Equal,
Address(baseGPR, JSArrayBufferView::offsetOfMode()),
TrustedImm32(FastTypedArray));
}
AssemblyHelpers::Jump AssemblyHelpers::branchIfNotFastTypedArray(GPRReg baseGPR)
{
return branch8(
NotEqual,
Address(baseGPR, JSArrayBufferView::offsetOfMode()),
TrustedImm32(FastTypedArray));
}
void AssemblyHelpers::incrementSuperSamplerCount()
{
add32(TrustedImm32(1), AbsoluteAddress(std::bit_cast<const void*>(&g_superSamplerCount)));
}
void AssemblyHelpers::decrementSuperSamplerCount()
{
sub32(TrustedImm32(1), AbsoluteAddress(std::bit_cast<const void*>(&g_superSamplerCount)));
}
void AssemblyHelpers::purifyNaN(FPRReg inputFPR, FPRReg resultFPR)
{
ASSERT(inputFPR != fpTempRegister);
#if CPU(ADDRESS64)
move64ToDouble(TrustedImm64(std::bit_cast<uint64_t>(PNaN)), fpTempRegister);
moveDoubleConditionallyDouble(DoubleEqualAndOrdered, inputFPR, inputFPR, inputFPR, fpTempRegister, resultFPR);
#else
moveDouble(inputFPR, resultFPR);
auto notNaN = branchIfNotNaN(resultFPR);
move64ToDouble(TrustedImm64(std::bit_cast<uint64_t>(PNaN)), resultFPR);
notNaN.link(this);
#endif
}
#if ENABLE(SAMPLING_FLAGS)
void AssemblyHelpers::setSamplingFlag(int32_t flag)
{
ASSERT(flag >= 1);
ASSERT(flag <= 32);
or32(TrustedImm32(1u << (flag - 1)), AbsoluteAddress(SamplingFlags::addressOfFlags()));
}
void AssemblyHelpers::clearSamplingFlag(int32_t flag)
{
ASSERT(flag >= 1);
ASSERT(flag <= 32);
and32(TrustedImm32(~(1u << (flag - 1))), AbsoluteAddress(SamplingFlags::addressOfFlags()));
}
#endif
#if ASSERT_ENABLED
#if USE(JSVALUE64)
void AssemblyHelpers::jitAssertIsInt32(GPRReg gpr)
{
if (!Options::useJITAsserts())
return;
#if CPU(X86_64) || CPU(ARM64)
JIT_COMMENT(*this, "ASSERT is unboxed int32");
Jump checkInt32 = branch64(BelowOrEqual, gpr, TrustedImm64(static_cast<uintptr_t>(0xFFFFFFFFu)));
abortWithReason(AHIsNotInt32);
checkInt32.link(this);
#else
UNUSED_PARAM(gpr);
#endif
}
void AssemblyHelpers::jitAssertIsJSInt32(GPRReg gpr)
{
if (!Options::useJITAsserts())
return;
JIT_COMMENT(*this, "ASSERT is JS boxed int32");
Jump checkJSInt32 = branch64(AboveOrEqual, gpr, GPRInfo::numberTagRegister);
abortWithReason(AHIsNotJSInt32);
checkJSInt32.link(this);
}
void AssemblyHelpers::jitAssertIsJSNumber(GPRReg gpr)
{
if (!Options::useJITAsserts())
return;
JIT_COMMENT(*this, "ASSERT is JS boxed number");
Jump checkJSNumber = branchTest64(MacroAssembler::NonZero, gpr, GPRInfo::numberTagRegister);
abortWithReason(AHIsNotJSNumber);
checkJSNumber.link(this);
}
void AssemblyHelpers::jitAssertIsJSDouble(GPRReg gpr)
{
if (!Options::useJITAsserts())
return;
JIT_COMMENT(*this, "ASSERT is JS boxed double (non-int32 number)");
Jump checkJSInt32 = branch64(AboveOrEqual, gpr, GPRInfo::numberTagRegister);
Jump checkJSNumber = branchTest64(MacroAssembler::NonZero, gpr, GPRInfo::numberTagRegister);
checkJSInt32.link(this);
abortWithReason(AHIsNotJSDouble);
checkJSNumber.link(this);
}
void AssemblyHelpers::jitAssertIsCell(GPRReg gpr)
{
if (!Options::useJITAsserts())
return;
JIT_COMMENT(*this, "ASSERT is JSCell");
Jump checkCell = branchTest64(MacroAssembler::Zero, gpr, GPRInfo::notCellMaskRegister);
abortWithReason(AHIsNotCell);
checkCell.link(this);
}
void AssemblyHelpers::jitAssertTagsInPlace()
{
if (!Options::useJITAsserts())
return;
Jump ok = branch64(Equal, GPRInfo::numberTagRegister, TrustedImm64(JSValue::NumberTag));
abortWithReason(AHNumberTagNotInPlace);
breakpoint();
ok.link(this);
ok = branch64(Equal, GPRInfo::notCellMaskRegister, TrustedImm64(JSValue::NotCellMask));
abortWithReason(AHNotCellMaskNotInPlace);
ok.link(this);
}
#elif USE(JSVALUE32_64)
void AssemblyHelpers::jitAssertIsInt32(GPRReg gpr)
{
if (!Options::useJITAsserts())
return;
UNUSED_PARAM(gpr);
}
void AssemblyHelpers::jitAssertIsJSInt32(GPRReg gpr)
{
if (!Options::useJITAsserts())
return;
Jump checkJSInt32 = branch32(Equal, gpr, TrustedImm32(JSValue::Int32Tag));
abortWithReason(AHIsNotJSInt32);
checkJSInt32.link(this);
}
void AssemblyHelpers::jitAssertIsJSNumber(GPRReg gpr)
{
if (!Options::useJITAsserts())
return;
Jump checkJSInt32 = branch32(Equal, gpr, TrustedImm32(JSValue::Int32Tag));
Jump checkJSDouble = branch32(Below, gpr, TrustedImm32(JSValue::LowestTag));
abortWithReason(AHIsNotJSNumber);
checkJSInt32.link(this);
checkJSDouble.link(this);
}
void AssemblyHelpers::jitAssertIsJSDouble(GPRReg gpr)
{
if (!Options::useJITAsserts())
return;
Jump checkJSDouble = branch32(Below, gpr, TrustedImm32(JSValue::LowestTag));
abortWithReason(AHIsNotJSDouble);
checkJSDouble.link(this);
}
void AssemblyHelpers::jitAssertIsCell(GPRReg gpr)
{
if (!Options::useJITAsserts())
return;
Jump checkCell = branchIfCell(gpr);
abortWithReason(AHIsNotCell);
checkCell.link(this);
}
void AssemblyHelpers::jitAssertTagsInPlace()
{
if (!Options::useJITAsserts())
return;
}
#endif // USE(JSVALUE32_64)
void AssemblyHelpers::jitAssertHasValidCallFrame()
{
if (!Options::useJITAsserts())
return;
Jump checkCFR = branchTestPtr(Zero, GPRInfo::callFrameRegister, TrustedImm32(7));
abortWithReason(AHCallFrameMisaligned);
checkCFR.link(this);
}
void AssemblyHelpers::jitAssertIsNull(GPRReg gpr)
{
if (!Options::useJITAsserts())
return;
Jump checkNull = branchTestPtr(Zero, gpr);
abortWithReason(AHIsNotNull);
checkNull.link(this);
}
void AssemblyHelpers::jitAssertArgumentCountSane()
{
if (!Options::useJITAsserts())
return;
Jump ok = branch32(Below, payloadFor(CallFrameSlot::argumentCountIncludingThis), TrustedImm32(10000000));
abortWithReason(AHInsaneArgumentCount);
ok.link(this);
}
void AssemblyHelpers::jitAssertCodeBlockOnCallFrameWithType(GPRReg scratchGPR, JITType type)
{
if (!Options::useJITAsserts())
return;
JIT_COMMENT(*this, "jitAssertCodeBlockOnCallFrameWithType | ", scratchGPR, " = callFrame->codeBlock->jitCode->jitType == ", type);
emitGetFromCallFrameHeaderPtr(CallFrameSlot::codeBlock, scratchGPR);
loadPtr(Address(scratchGPR, CodeBlock::jitCodeOffset()), scratchGPR);
load8(Address(scratchGPR, JITCode::offsetOfJITType()), scratchGPR);
Jump ok = branch32(Equal, scratchGPR, TrustedImm32(static_cast<unsigned>(type)));
abortWithReason(AHInvalidCodeBlock);
ok.link(this);
}
void AssemblyHelpers::jitAssertCodeBlockMatchesCurrentCalleeCodeBlockOnCallFrame(GPRReg scratchGPR, GPRReg scratchGPR2, UnlinkedCodeBlock& block)
{
if (!Options::useJITAsserts())
return;
// UNGIL FIX-2 (gilOff mode-split): with N mutators,
// executable->codeBlockFor(kind) is a moving publish slot — tier-up
// republish (ScriptExecutable::installCode, IT-8) and post-jettison
// reinstall legitimately advance it while activations dispatched through
// a coherent-but-stale CallLinkRecord are still entering or executing the
// OLD CodeBlock (deferred invalidation, SPEC-jit I21: resumed mutators
// keep executing replaced code until their next invalidation point).
// "callFrame->codeBlock == the executable's CURRENTLY published
// codeBlock" is therefore not an invariant gilOff, and the load emitted
// below is unordered against the installer's fences anyway. Benign by
// design => do not emit the check. Emission-time gate on the sticky
// Config-page byte (same gate as ~CallLinkInfo): the gate is evaluated
// while GENERATING code, so flag-off/GIL-on emitted code is
// byte-identical and the V5b flag-off bench rule is untouched.
// Accepted loss (review record): this was the only check that could catch
// a same-tier WRONG-FUNCTION CodeBlock in the frame slot; the retained
// canaries at jitAssertCodeBlockOnCallFrameWithType and
// jitAssertCodeBlockOnCallFrameIsOptimizingJIT only see tier mismatches.
if (g_jscConfig.gilOffProcess) [[unlikely]]
return;
if (block.codeType() != FunctionCode)
return;
auto kind = block.isConstructor() ? CodeSpecializationKind::CodeForConstruct : CodeSpecializationKind::CodeForCall;
JIT_COMMENT(*this, "jitAssertCodeBlockMatchesCurrentCalleeCodeBlockOnCallFrame with code block type: ", kind, " | ", scratchGPR, " = callFrame->callee->executableOrRareData");
emitGetFromCallFrameHeaderPtr(CallFrameSlot::callee, scratchGPR);
loadPtr(Address(scratchGPR, JSFunction::offsetOfExecutableOrRareData()), scratchGPR);
auto hasExecutable = branchTestPtr(Zero, scratchGPR, TrustedImm32(JSFunction::rareDataTag));
loadPtr(Address(scratchGPR, FunctionRareData::offsetOfExecutable() - JSFunction::rareDataTag), scratchGPR);
hasExecutable.link(this);
JIT_COMMENT(*this, scratchGPR, " = (", scratchGPR, ": Executable)->codeBlock");
loadPtr(Address(scratchGPR, FunctionExecutable::offsetOfCodeBlockFor(kind)), scratchGPR);
JIT_COMMENT(*this, scratchGPR2, " = callFrame->codeBlock");
emitGetFromCallFrameHeaderPtr(CallFrameSlot::codeBlock, scratchGPR2);
Jump ok = branch32(Equal, scratchGPR, scratchGPR2);
abortWithReason(AHInvalidCodeBlock);
ok.link(this);
}
void AssemblyHelpers::jitAssertCodeBlockOnCallFrameIsOptimizingJIT(GPRReg scratchGPR)
{
if (!Options::useJITAsserts())
return;
emitGetFromCallFrameHeaderPtr(CallFrameSlot::codeBlock, scratchGPR);
loadPtr(Address(scratchGPR, CodeBlock::jitCodeOffset()), scratchGPR);
load8(Address(scratchGPR, JITCode::offsetOfJITType()), scratchGPR);
JumpList ok;
ok.append(branch32(Equal, scratchGPR, TrustedImm32(static_cast<unsigned>(JITType::DFGJIT))));
ok.append(branch32(Equal, scratchGPR, TrustedImm32(static_cast<unsigned>(JITType::FTLJIT))));
abortWithReason(AHInvalidCodeBlock);
ok.link(this);
}
#endif // ASSERT_ENABLED
// UNGIL §A.1.3 (U-T4): see the declaration comment in AssemblyHelpers.h.
AssemblyHelpers::Address AssemblyHelpers::materializeGILOffExceptionSlot()
{
#if CPU(ARM64)
// Cache-invalidating accessor for the same reason as prepareCallOperation:
// loadVMLite writes the temp via mrs+ldr without updating
// m_cachedMemoryTempRegister.
GPRReg scratchGPR = getCachedMemoryTempRegisterIDAndInvalidate();
#elif CPU(X86_64)
GPRReg scratchGPR = scratchRegister(); // r11, already clobbered by the GIL-on AbsoluteAddress form.
#else
// SPEC-jit annex App. R5: no gilOff support here; loadVMLite fail-stops
// at emission before this address is used.
GPRReg scratchGPR = GPRInfo::nonArgGPR0;
#endif
loadVMLite(scratchGPR);
return Address(scratchGPR, static_cast<int32_t>(VMLite::offsetOfPrimitives() + VMLitePrimitives::offsetOf_m_exception()));
}
void AssemblyHelpers::loadException(VM& vm, GPRReg destGPR)
{
if (vm.gilOff()) [[unlikely]] {
loadVMLite(destGPR);
loadPtr(Address(destGPR, static_cast<int32_t>(VMLite::offsetOfPrimitives() + VMLitePrimitives::offsetOf_m_exception())), destGPR);
} else
loadPtr(vm.addressOfException(), destGPR);
}
void AssemblyHelpers::jitReleaseAssertNoException(VM& vm)
{
Jump noException;
#if USE(JSVALUE64)
if (vm.gilOff()) [[unlikely]]
noException = branchTestPtr(Zero, materializeGILOffExceptionSlot());
else
noException = branchTest64(Zero, AbsoluteAddress(vm.addressOfException()));
#elif USE(JSVALUE32_64)
// GIL-off is unsupported on 32-bit platforms (jit App. R5); loadVMLite
// fail-stops at emission, so the absolute form stays correct here.
noException = branch32(Equal, AbsoluteAddress(vm.addressOfException()), TrustedImm32(0));
#endif
abortWithReason(JITUncaughtExceptionAfterCall);
noException.link(this);
}
void AssemblyHelpers::callExceptionFuzz(VM& vm, GPRReg exceptionReg)
{
RELEASE_ASSERT(Options::useExceptionFuzz());
EncodedJSValue* buffer = vm.exceptionFuzzingBuffer(sizeof(EncodedJSValue) * (GPRInfo::numberOfRegisters + FPRInfo::numberOfRegisters));
for (unsigned i = 0; i < GPRInfo::numberOfRegisters; ++i) {
#if USE(JSVALUE64)
store64(GPRInfo::toRegister(i), buffer + i);
#else
store32(GPRInfo::toRegister(i), buffer + i);
#endif
}
for (unsigned i = 0; i < FPRInfo::numberOfRegisters; ++i) {
move(TrustedImmPtr(buffer + GPRInfo::numberOfRegisters + i), GPRInfo::regT0);
storeDouble(FPRInfo::toRegister(i), Address(GPRInfo::regT0));
}
// Set up one argument.
move(TrustedImmPtr(&vm), GPRInfo::argumentGPR0);
move(TrustedImmPtr(tagCFunction<OperationPtrTag>(operationExceptionFuzzWithCallFrame)), GPRInfo::nonPreservedNonReturnGPR);
prepareCallOperation(vm);
call(GPRInfo::nonPreservedNonReturnGPR, OperationPtrTag);
for (unsigned i = 0; i < FPRInfo::numberOfRegisters; ++i) {
move(TrustedImmPtr(buffer + GPRInfo::numberOfRegisters + i), GPRInfo::regT0);
loadDouble(Address(GPRInfo::regT0), FPRInfo::toRegister(i));
}
for (unsigned i = 0; i < GPRInfo::numberOfRegisters; ++i) {
#if USE(JSVALUE64)
load64(buffer + i, GPRInfo::toRegister(i));
#else
load32(buffer + i, GPRInfo::toRegister(i));
#endif
}
if (exceptionReg != InvalidGPRReg)
loadException(vm, exceptionReg);
}
AssemblyHelpers::Jump AssemblyHelpers::emitJumpIfException(VM& vm)
{
return emitExceptionCheck(vm, NormalExceptionCheck);
}
AssemblyHelpers::Jump AssemblyHelpers::emitExceptionCheck(VM& vm, ExceptionCheckKind kind, ExceptionJumpWidth width, GPRReg exceptionReg)
{
if (Options::useExceptionFuzz()) [[unlikely]]
callExceptionFuzz(vm, exceptionReg);
if (width == FarJumpWidth)
kind = (kind == NormalExceptionCheck ? InvertedExceptionCheck : NormalExceptionCheck);
Jump result;
if (exceptionReg != InvalidGPRReg) {
#if ASSERT_ENABLED
JIT_COMMENT(*this, "Exception validation");
Jump ok;
if (vm.gilOff()) [[unlikely]]
ok = branchPtr(Equal, materializeGILOffExceptionSlot(), exceptionReg);
else
ok = branchPtr(Equal, AbsoluteAddress(vm.addressOfException()), exceptionReg);
breakpoint();
ok.link(this);
#endif
JIT_COMMENT(*this, "Exception check from operation result register");
result = branchTestPtr(kind == NormalExceptionCheck ? NonZero : Zero, exceptionReg);
} else {
JIT_COMMENT(*this, "Exception check from vm");
if (vm.gilOff()) [[unlikely]]
result = branchTestPtr(kind == NormalExceptionCheck ? NonZero : Zero, materializeGILOffExceptionSlot());
else
result = branchTestPtr(kind == NormalExceptionCheck ? NonZero : Zero, AbsoluteAddress(vm.addressOfException()));
}
if (width == NormalJumpWidth)
return result;
PatchableJump realJump = patchableJump();
result.link(this);
return realJump.m_jump;
}
AssemblyHelpers::Jump AssemblyHelpers::emitNonPatchableExceptionCheck(VM& vm, GPRReg exceptionReg)
{
return emitExceptionCheck(vm, NormalExceptionCheck, NormalJumpWidth, exceptionReg);
}
void AssemblyHelpers::emitStoreStructureWithTypeInfo(AssemblyHelpers& jit, TrustedImmPtr structure, RegisterID dest)
{
const Structure* structurePtr = reinterpret_cast<const Structure*>(structure.m_value);
#if USE(JSVALUE64)
jit.store64(TrustedImm64(static_cast<uint64_t>(structurePtr->id().bits()) | (static_cast<uint64_t>(structurePtr->typeInfoBlob()) << 32)), MacroAssembler::Address(dest, JSCell::structureIDOffset()));
if (ASSERT_ENABLED) {
Jump correctStructure = jit.branch32(Equal, MacroAssembler::Address(dest, JSCell::structureIDOffset()), TrustedImm32(structurePtr->id().bits()));
jit.abortWithReason(AHStructureIDIsValid);
correctStructure.link(&jit);
Jump correctIndexingType = jit.branch8(Equal, MacroAssembler::Address(dest, JSCell::indexingTypeAndMiscOffset()), TrustedImm32(structurePtr->indexingModeIncludingHistory()));
jit.abortWithReason(AHIndexingTypeIsValid);
correctIndexingType.link(&jit);
Jump correctType = jit.branch8(Equal, MacroAssembler::Address(dest, JSCell::typeInfoTypeOffset()), TrustedImm32(structurePtr->typeInfo().type()));
jit.abortWithReason(AHTypeInfoIsValid);
correctType.link(&jit);
Jump correctFlags = jit.branch8(Equal, MacroAssembler::Address(dest, JSCell::typeInfoFlagsOffset()), TrustedImm32(structurePtr->typeInfo().inlineTypeFlags()));
jit.abortWithReason(AHTypeInfoInlineTypeFlagsAreValid);
correctFlags.link(&jit);
}
#else
// Do a 32-bit wide store to initialize the cell's fields.
jit.store32(TrustedImm32(structurePtr->typeInfoBlob()), MacroAssembler::Address(dest, JSCell::indexingTypeAndMiscOffset()));
jit.storePtr(structure, MacroAssembler::Address(dest, JSCell::structureIDOffset()));
#endif
}
void AssemblyHelpers::loadProperty(GPRReg object, GPRReg offset, JSValueRegs result)
{
ASSERT(noOverlap(offset, result));
Jump isInline = branch32(LessThan, offset, TrustedImm32(firstOutOfLineOffset));
loadPtr(Address(object, JSObject::butterflyOffset()), result.payloadGPR());
AssemblyHelpersInternal::emitLegacyButterflyTagTrap(*this, result.payloadGPR()); // SPEC-jit section 5.5 Task 8
neg32(offset);
signExtend32ToPtr(offset, offset);
Jump ready = jump();
isInline.link(this);
addPtr(
TrustedImm32(
static_cast<int32_t>(JSObject::offsetOfInlineStorage()) -
(static_cast<int32_t>(firstOutOfLineOffset) - 2) * static_cast<int32_t>(sizeof(EncodedJSValue))),
object, result.payloadGPR());
ready.link(this);
loadValue(
BaseIndex(
result.payloadGPR(), offset, TimesEight, (firstOutOfLineOffset - 2) * sizeof(EncodedJSValue)),
result);
}
void AssemblyHelpers::storeProperty(JSValueRegs value, GPRReg object, GPRReg offset, GPRReg scratch)
{
// Actually, object can be the same to scratch.
ASSERT(noOverlap(offset, scratch));
ASSERT(noOverlap(value, scratch));
Jump isInline = branch32(LessThan, offset, TrustedImm32(firstOutOfLineOffset));
loadPtr(Address(object, JSObject::butterflyOffset()), scratch);
AssemblyHelpersInternal::emitLegacyButterflyTagTrap(*this, scratch); // SPEC-jit section 5.5 Task 8
neg32(offset);
signExtend32ToPtr(offset, offset);
Jump ready = jump();
isInline.link(this);
addPtr(
TrustedImm32(
static_cast<int32_t>(JSObject::offsetOfInlineStorage()) -
(static_cast<int32_t>(firstOutOfLineOffset) - 2) * static_cast<int32_t>(sizeof(EncodedJSValue))),
object, scratch);
ready.link(this);
storeValue(value, BaseIndex(scratch, offset, TimesEight, (firstOutOfLineOffset - 2) * sizeof(EncodedJSValue)));
}
#if USE(JSVALUE64)
AssemblyHelpers::JumpList AssemblyHelpers::loadMegamorphicProperty(VM& vm, GPRReg baseGPR, GPRReg uidGPR, UniquedStringImpl* uid, GPRReg resultGPR, GPRReg scratch1GPR, GPRReg scratch2GPR, GPRReg scratch3GPR)
{
if (Options::useJSThreads()) [[unlikely]] {
// SPEC-jit section 5.5 (Task 8): the megamorphic fast path reads the
// VM-global MegamorphicCache without synchronization and dereferences
// the butterfly without the TID/SW predicate; flag-on every
// megamorphic access defers to the generic operation (Task 8
// inventory; revisit with vmstate's shared-cache story).
JumpList slowCases;
slowCases.append(jump());
return slowCases;
}
// uidGPR can be InvalidGPRReg if uid is non-nullptr.
if (!uid)
ASSERT(uidGPR != InvalidGPRReg);
JumpList primaryFail;
JumpList slowCases;
load32(Address(baseGPR, JSCell::structureIDOffset()), scratch1GPR);
#if CPU(ARM64)
extractUnsignedBitfield32(scratch1GPR, TrustedImm32(MegamorphicCache::structureIDHashShift1), TrustedImm32(32 - MegamorphicCache::structureIDHashShift1), scratch2GPR);
xorUnsignedRightShift32(scratch2GPR, scratch1GPR, TrustedImm32(MegamorphicCache::structureIDHashShift2), scratch3GPR);
#else
urshift32(scratch1GPR, TrustedImm32(MegamorphicCache::structureIDHashShift1), scratch2GPR);
urshift32(scratch1GPR, TrustedImm32(MegamorphicCache::structureIDHashShift2), scratch3GPR);
xor32(scratch2GPR, scratch3GPR);
#endif
if (uid)
add32(TrustedImm32(uid->hash()), scratch3GPR);
else {
// Note that we don't test if the hash is zero here. AtomStringImpl's can't have a zero
// hash, however, a SymbolImpl may. But, because this is a cache, we don't care. We only
// ever load the result from the cache if the cache entry matches what we are querying for.
// So we either get super lucky and use zero for the hash and somehow collide with the entity
// we're looking for, or we realize we're comparing against another entity, and go to the
// slow path anyways.
load32(Address(uidGPR, UniquedStringImpl::flagsOffset()), scratch2GPR);
urshift32(TrustedImm32(StringImpl::s_flagCount), scratch2GPR);
add32(scratch2GPR, scratch3GPR);
}
and32(TrustedImm32(MegamorphicCache::loadCachePrimaryMask), scratch3GPR);
if (hasOneBitSet(sizeof(MegamorphicCache::LoadEntry))) // is a power of 2
lshift32(TrustedImm32(getLSBSet(sizeof(MegamorphicCache::LoadEntry))), scratch3GPR);
else
mul32(TrustedImm32(sizeof(MegamorphicCache::LoadEntry)), scratch3GPR, scratch3GPR);
auto& cache = vm.ensureMegamorphicCache();
move(TrustedImmPtr(&cache), scratch2GPR);
static_assert(!MegamorphicCache::offsetOfLoadCachePrimaryEntries());
addPtr(scratch2GPR, scratch3GPR);
load16(Address(scratch2GPR, MegamorphicCache::offsetOfEpoch()), scratch2GPR);
primaryFail.append(branch32(NotEqual, scratch1GPR, Address(scratch3GPR, MegamorphicCache::LoadEntry::offsetOfStructureID())));
if (uid)
primaryFail.append(branchPtr(NotEqual, Address(scratch3GPR, MegamorphicCache::LoadEntry::offsetOfUid()), TrustedImmPtr(uid)));
else
primaryFail.append(branchPtr(NotEqual, Address(scratch3GPR, MegamorphicCache::LoadEntry::offsetOfUid()), uidGPR));
// We already hit StructureID and uid. And we get stale epoch for this entry.
// Since all entries in the secondary cache has stale epoch for this StructureID and uid pair, we should just go to the slow case.
slowCases.append(branch32WithMemory16(NotEqual, Address(scratch3GPR, MegamorphicCache::LoadEntry::offsetOfEpoch()), scratch2GPR));
// Cache hit!
Label cacheHit = label();
loadPtr(Address(scratch3GPR, MegamorphicCache::LoadEntry::offsetOfHolder()), scratch2GPR);
auto missed = branchTestPtr(Zero, scratch2GPR);
moveConditionally64(Equal, scratch2GPR, TrustedImm32(std::bit_cast<uintptr_t>(JSCell::seenMultipleCalleeObjects())), baseGPR, scratch2GPR, scratch1GPR);
load16(Address(scratch3GPR, MegamorphicCache::LoadEntry::offsetOfOffset()), scratch2GPR);
loadProperty(scratch1GPR, scratch2GPR, JSValueRegs { resultGPR });
auto done = jump();
// Secondary cache lookup. Now,
// 1. scratch1GPR holds StructureID.
// 2. scratch2GPR holds global epoch.
primaryFail.link(this);
if (uid)
add32(TrustedImm32(static_cast<uint32_t>(std::bit_cast<uintptr_t>(uid))), scratch1GPR, scratch3GPR);
else
add32(uidGPR, scratch1GPR, scratch3GPR);
addUnsignedRightShift32(scratch3GPR, scratch3GPR, TrustedImm32(MegamorphicCache::structureIDHashShift3), scratch3GPR);
and32(TrustedImm32(MegamorphicCache::loadCacheSecondaryMask), scratch3GPR);
if constexpr (hasOneBitSet(sizeof(MegamorphicCache::LoadEntry))) // is a power of 2
lshift32(TrustedImm32(getLSBSet(sizeof(MegamorphicCache::LoadEntry))), scratch3GPR);
else
mul32(TrustedImm32(sizeof(MegamorphicCache::LoadEntry)), scratch3GPR, scratch3GPR);
addPtr(TrustedImmPtr(std::bit_cast<uint8_t*>(&cache) + MegamorphicCache::offsetOfLoadCacheSecondaryEntries()), scratch3GPR);
slowCases.append(branch32(NotEqual, scratch1GPR, Address(scratch3GPR, MegamorphicCache::LoadEntry::offsetOfStructureID())));
if (uid)
slowCases.append(branchPtr(NotEqual, Address(scratch3GPR, MegamorphicCache::LoadEntry::offsetOfUid()), TrustedImmPtr(uid)));
else
slowCases.append(branchPtr(NotEqual, Address(scratch3GPR, MegamorphicCache::LoadEntry::offsetOfUid()), uidGPR));
slowCases.append(branch32WithMemory16(NotEqual, Address(scratch3GPR, MegamorphicCache::LoadEntry::offsetOfEpoch()), scratch2GPR));
jump().linkTo(cacheHit, this);
missed.link(this);
moveTrustedValue(jsUndefined(), JSValueRegs { resultGPR });
done.link(this);
return slowCases;
}
std::tuple<AssemblyHelpers::JumpList, AssemblyHelpers::JumpList> AssemblyHelpers::storeMegamorphicProperty(VM& vm, GPRReg baseGPR, GPRReg uidGPR, UniquedStringImpl* uid, GPRReg valueGPR, GPRReg scratch1GPR, GPRReg scratch2GPR, GPRReg scratch3GPR)
{
if (Options::useJSThreads()) [[unlikely]] {
// SPEC-jit section 5.5 (Task 8): see loadMegamorphicProperty above.
JumpList slowCases;
slowCases.append(jump());
return { WTF::move(slowCases), JumpList() };
}
// uidGPR can be InvalidGPRReg if uid is non-nullptr.
if (!uid)
ASSERT(uidGPR != InvalidGPRReg);
JumpList primaryFail;
JumpList slowCases;
JumpList reallocatingCases;
load32(Address(baseGPR, JSCell::structureIDOffset()), scratch1GPR);
#if CPU(ARM64)
extractUnsignedBitfield32(scratch1GPR, TrustedImm32(MegamorphicCache::structureIDHashShift1), TrustedImm32(32 - MegamorphicCache::structureIDHashShift1), scratch2GPR);
xorUnsignedRightShift32(scratch2GPR, scratch1GPR, TrustedImm32(MegamorphicCache::structureIDHashShift4), scratch3GPR);
#else
urshift32(scratch1GPR, TrustedImm32(MegamorphicCache::structureIDHashShift1), scratch2GPR);
urshift32(scratch1GPR, TrustedImm32(MegamorphicCache::structureIDHashShift4), scratch3GPR);
xor32(scratch2GPR, scratch3GPR);
#endif
if (uid)
add32(TrustedImm32(uid->hash()), scratch3GPR);
else {
// Note that we don't test if the hash is zero here. AtomStringImpl's can't have a zero
// hash, however, a SymbolImpl may. But, because this is a cache, we don't care. We only
// ever load the result from the cache if the cache entry matches what we are querying for.
// So we either get super lucky and use zero for the hash and somehow collide with the entity
// we're looking for, or we realize we're comparing against another entity, and go to the
// slow path anyways.
load32(Address(uidGPR, UniquedStringImpl::flagsOffset()), scratch2GPR);
urshift32(TrustedImm32(StringImpl::s_flagCount), scratch2GPR);
add32(scratch2GPR, scratch3GPR);
}
and32(TrustedImm32(MegamorphicCache::storeCachePrimaryMask), scratch3GPR);
if (hasOneBitSet(sizeof(MegamorphicCache::StoreEntry))) // is a power of 2
lshift32(TrustedImm32(getLSBSet(sizeof(MegamorphicCache::StoreEntry))), scratch3GPR);
else
mul32(TrustedImm32(sizeof(MegamorphicCache::StoreEntry)), scratch3GPR, scratch3GPR);
auto& cache = vm.ensureMegamorphicCache();
move(TrustedImmPtr(&cache), scratch2GPR);
addPtr(scratch2GPR, scratch3GPR);
addPtr(TrustedImmPtr(MegamorphicCache::offsetOfStoreCachePrimaryEntries()), scratch3GPR);
load16(Address(scratch2GPR, MegamorphicCache::offsetOfEpoch()), scratch2GPR);
primaryFail.append(branch32(NotEqual, scratch1GPR, Address(scratch3GPR, MegamorphicCache::StoreEntry::offsetOfOldStructureID())));
if (uid)
primaryFail.append(branchPtr(NotEqual, Address(scratch3GPR, MegamorphicCache::StoreEntry::offsetOfUid()), TrustedImmPtr(uid)));
else
primaryFail.append(branchPtr(NotEqual, Address(scratch3GPR, MegamorphicCache::StoreEntry::offsetOfUid()), uidGPR));
// We already hit StructureID and uid. And we get stale epoch for this entry.
// Since all entries in the secondary cache has stale epoch for this StructureID and uid pair, we should just go to the slow case.
slowCases.append(branch32WithMemory16(NotEqual, Address(scratch3GPR, MegamorphicCache::StoreEntry::offsetOfEpoch()), scratch2GPR));
// Cache hit!
Label cacheHit = label();
reallocatingCases.append(branchTest8(NonZero, Address(scratch3GPR, MegamorphicCache::StoreEntry::offsetOfReallocating())));
load32(Address(scratch3GPR, MegamorphicCache::StoreEntry::offsetOfNewStructureID()), scratch2GPR);
load16(Address(scratch3GPR, MegamorphicCache::StoreEntry::offsetOfOffset()), scratch3GPR);
auto replaceCase = branch32(Equal, scratch2GPR, scratch1GPR);
// We only support non-allocating transition. This means we do not need to nuke Structure* for transition here.
store32(scratch2GPR, Address(baseGPR, JSCell::structureIDOffset()));
replaceCase.link(this);