forked from WebKit/WebKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWasmBBQJIT.h
More file actions
2284 lines (1657 loc) · 86.8 KB
/
WasmBBQJIT.h
File metadata and controls
2284 lines (1657 loc) · 86.8 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) 2019-2023 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.
*/
#pragma once
#if ENABLE(WEBASSEMBLY_BBQJIT)
#include "WasmCallingConvention.h"
#include "WasmCompilationContext.h"
#include "WasmFunctionParser.h"
#include "WasmLimits.h"
namespace JSC { namespace Wasm {
namespace BBQJITImpl {
class BBQJIT {
public:
using ErrorType = String;
using PartialResult = Expected<void, ErrorType>;
using Address = MacroAssembler::Address;
using BaseIndex = MacroAssembler::BaseIndex;
using Imm32 = MacroAssembler::Imm32;
using Imm64 = MacroAssembler::Imm64;
using TrustedImm32 = MacroAssembler::TrustedImm32;
using TrustedImm64 = MacroAssembler::TrustedImm64;
using TrustedImmPtr = MacroAssembler::TrustedImmPtr;
using RelationalCondition = MacroAssembler::RelationalCondition;
using ResultCondition = MacroAssembler::ResultCondition;
using DoubleCondition = MacroAssembler::DoubleCondition;
using StatusCondition = MacroAssembler::StatusCondition;
using Jump = MacroAssembler::Jump;
using JumpList = MacroAssembler::JumpList;
using DataLabelPtr = MacroAssembler::DataLabelPtr;
// Functions can have up to 1000000 instructions, so 32 bits is a sensible maximum number of stack items or locals.
using LocalOrTempIndex = uint32_t;
static constexpr unsigned LocalIndexBits = 21;
static_assert(maxFunctionLocals < 1 << LocalIndexBits);
static constexpr GPRReg wasmScratchGPR = GPRInfo::nonPreservedNonArgumentGPR0; // Scratch registers to hold temporaries in operations.
#if USE(JSVALUE32_64)
static constexpr GPRReg wasmScratchGPR2 = GPRInfo::nonPreservedNonArgumentGPR1;
#else
static constexpr GPRReg wasmScratchGPR2 = InvalidGPRReg;
#endif
static constexpr FPRReg wasmScratchFPR = FPRInfo::nonPreservedNonArgumentFPR0;
#if CPU(X86) || CPU(X86_64)
static constexpr GPRReg shiftRCX = X86Registers::ecx;
#else
static constexpr GPRReg shiftRCX = InvalidGPRReg;
#endif
#if USE(JSVALUE64)
static constexpr GPRReg wasmBaseMemoryPointer = GPRInfo::wasmBaseMemoryPointer;
static constexpr GPRReg wasmBoundsCheckingSizeRegister = GPRInfo::wasmBoundsCheckingSizeRegister;
#else
static constexpr GPRReg wasmBaseMemoryPointer = InvalidGPRReg;
static constexpr GPRReg wasmBoundsCheckingSizeRegister = InvalidGPRReg;
#endif
public:
struct Location {
enum Kind : uint8_t {
None = 0,
Stack = 1,
Gpr = 2,
Fpr = 3,
Global = 4,
StackArgument = 5,
Gpr2 = 6
};
Location()
: m_kind(None)
{ }
static Location none();
static Location fromStack(int32_t stackOffset);
static Location fromStackArgument(int32_t stackOffset);
static Location fromGPR(GPRReg gpr);
static Location fromGPR2(GPRReg hi, GPRReg lo);
static Location fromFPR(FPRReg fpr);
static Location fromGlobal(int32_t globalOffset);
static Location fromArgumentLocation(ArgumentLocation argLocation, TypeKind type);
bool isNone() const;
bool isGPR() const;
bool isGPR2() const;
bool isFPR() const;
bool isRegister() const;
bool isStack() const;
bool isStackArgument() const;
bool isGlobal() const;
bool isMemory() const;
int32_t asStackOffset() const;
Address asStackAddress() const;
int32_t asGlobalOffset() const;
Address asGlobalAddress() const;
int32_t asStackArgumentOffset() const;
Address asStackArgumentAddress() const;
Address asAddress() const;
GPRReg asGPR() const;
FPRReg asFPR() const;
GPRReg asGPRlo() const;
GPRReg asGPRhi() const;
void dump(PrintStream& out) const;
bool operator==(Location other) const;
Kind kind() const;
private:
union {
// It's useful to we be able to cram a location into a 4-byte space, so that
// we can store them efficiently in ControlData.
struct {
unsigned m_kind : 3;
int32_t m_offset : 29;
};
struct {
Kind m_padGpr;
GPRReg m_gpr;
};
struct {
Kind m_padFpr;
FPRReg m_fpr;
};
struct {
Kind m_padGpr2;
GPRReg m_gprhi, m_gprlo;
};
};
};
static_assert(sizeof(Location) == 4);
static bool isValidValueTypeKind(TypeKind kind);
static TypeKind pointerType();
static bool isFloatingPointType(TypeKind type);
static bool typeNeedsGPR2(TypeKind type);
public:
static uint32_t sizeOfType(TypeKind type);
static TypeKind toValueKind(TypeKind kind);
class Value {
public:
// Represents the location in which this value is stored.
enum Kind : uint8_t {
None = 0,
Const = 1,
Temp = 2,
Local = 3,
Pinned = 4 // Used if we need to represent a Location as a Value, mostly in operation calls
};
ALWAYS_INLINE bool isNone() const
{
return m_kind == None;
}
ALWAYS_INLINE bool isTemp() const
{
return m_kind == Temp;
}
ALWAYS_INLINE bool isLocal() const
{
return m_kind == Local;
}
ALWAYS_INLINE bool isPinned() const
{
return m_kind == Pinned;
}
ALWAYS_INLINE Kind kind() const
{
return m_kind;
}
ALWAYS_INLINE int32_t asI32() const
{
ASSERT(m_kind == Const);
return m_i32;
}
ALWAYS_INLINE int64_t asI64() const
{
ASSERT(m_kind == Const);
return m_i64;
}
ALWAYS_INLINE float asF32() const
{
ASSERT(m_kind == Const);
return m_f32;
}
ALWAYS_INLINE double asF64() const
{
ASSERT(m_kind == Const);
return m_f64;
}
ALWAYS_INLINE EncodedJSValue asRef() const
{
ASSERT(m_kind == Const);
return m_ref;
}
ALWAYS_INLINE LocalOrTempIndex asTemp() const
{
ASSERT(m_kind == Temp);
return m_index;
}
ALWAYS_INLINE LocalOrTempIndex asLocal() const
{
ASSERT(m_kind == Local);
return m_index;
}
ALWAYS_INLINE bool isConst() const
{
return m_kind == Const;
}
ALWAYS_INLINE Location asPinned() const
{
ASSERT(m_kind == Pinned);
return m_pinned;
}
ALWAYS_INLINE static Value fromI32(int32_t immediate)
{
Value val;
val.m_kind = Const;
val.m_type = TypeKind::I32;
val.m_i32 = immediate;
return val;
}
ALWAYS_INLINE static Value fromI64(int64_t immediate)
{
Value val;
val.m_kind = Const;
val.m_type = TypeKind::I64;
val.m_i64 = immediate;
return val;
}
ALWAYS_INLINE static Value fromF32(float immediate)
{
Value val;
val.m_kind = Const;
val.m_type = TypeKind::F32;
val.m_f32 = immediate;
return val;
}
ALWAYS_INLINE static Value fromF64(double immediate)
{
Value val;
val.m_kind = Const;
val.m_type = TypeKind::F64;
val.m_f64 = immediate;
return val;
}
ALWAYS_INLINE static Value fromRef(TypeKind refType, EncodedJSValue ref)
{
Value val;
val.m_kind = Const;
val.m_type = toValueKind(refType);
val.m_ref = ref;
return val;
}
ALWAYS_INLINE static Value fromTemp(TypeKind type, LocalOrTempIndex temp)
{
Value val;
val.m_kind = Temp;
val.m_type = toValueKind(type);
val.m_index = temp;
return val;
}
ALWAYS_INLINE static Value fromLocal(TypeKind type, LocalOrTempIndex local)
{
Value val;
val.m_kind = Local;
val.m_type = toValueKind(type);
val.m_index = local;
return val;
}
ALWAYS_INLINE static Value pinned(TypeKind type, Location location)
{
Value val;
val.m_kind = Pinned;
val.m_type = toValueKind(type);
val.m_pinned = location;
return val;
}
ALWAYS_INLINE static Value none()
{
Value val;
val.m_kind = None;
return val;
}
ALWAYS_INLINE uint32_t size() const
{
return sizeOfType(m_type);
}
ALWAYS_INLINE bool isFloat() const
{
return isFloatingPointType(m_type);
}
ALWAYS_INLINE TypeKind type() const
{
ASSERT(isValidValueTypeKind(m_type));
return m_type;
}
ALWAYS_INLINE Value()
: m_kind(None)
{ }
int32_t asI64hi() const;
int32_t asI64lo() const;
void dump(PrintStream& out) const;
private:
union {
int32_t m_i32;
struct {
int32_t lo, hi;
} m_i32_pair;
int64_t m_i64;
float m_f32;
double m_f64;
LocalOrTempIndex m_index;
Location m_pinned;
EncodedJSValue m_ref;
};
Kind m_kind;
TypeKind m_type;
};
public:
struct RegisterBinding {
enum Kind : uint8_t {
None = 0,
Local = 1,
Temp = 2,
Scratch = 3, // Denotes a register bound for use as a scratch, not as a local or temp's location.
};
union {
uint32_t m_uintValue;
struct {
TypeKind m_type;
unsigned m_kind : 3;
unsigned m_index : LocalIndexBits;
};
};
RegisterBinding()
: m_uintValue(0)
{ }
RegisterBinding(uint32_t uintValue)
: m_uintValue(uintValue)
{ }
static RegisterBinding fromValue(Value value);
static RegisterBinding none();
static RegisterBinding scratch();
Value toValue() const;
bool isNone() const;
bool isValid() const;
bool isScratch() const;
bool operator==(RegisterBinding other) const;
void dump(PrintStream& out) const;
unsigned hash() const;
uint32_t encode() const;
};
public:
struct ControlData {
static bool isIf(const ControlData& control) { return control.blockType() == BlockType::If; }
static bool isTry(const ControlData& control) { return control.blockType() == BlockType::Try; }
static bool isAnyCatch(const ControlData& control) { return control.blockType() == BlockType::Catch; }
static bool isCatch(const ControlData& control) { return isAnyCatch(control) && control.catchKind() == CatchKind::Catch; }
static bool isTopLevel(const ControlData& control) { return control.blockType() == BlockType::TopLevel; }
static bool isLoop(const ControlData& control) { return control.blockType() == BlockType::Loop; }
static bool isBlock(const ControlData& control) { return control.blockType() == BlockType::Block; }
ControlData()
: m_enclosedHeight(0)
{ }
ControlData(BBQJIT& generator, BlockType blockType, BlockSignature signature, LocalOrTempIndex enclosedHeight, RegisterSet liveScratchGPRs);
// Re-use the argument layout of another block (eg. else will re-use the argument/result locations from if)
enum BranchCallingConventionReuseTag { UseBlockCallingConventionOfOtherBranch };
ControlData(BranchCallingConventionReuseTag, BlockType blockType, ControlData& otherBranch)
: m_signature(otherBranch.m_signature)
, m_blockType(blockType)
, m_argumentLocations(otherBranch.m_argumentLocations)
, m_resultLocations(otherBranch.m_resultLocations)
, m_enclosedHeight(otherBranch.m_enclosedHeight)
{
}
// This function is intentionally not using implicitSlots since arguments and results should not include implicit slot.
Location allocateArgumentOrResult(BBQJIT& generator, TypeKind type, unsigned i, RegisterSet& remainingGPRs, RegisterSet& remainingFPRs);
template<typename Stack>
void flushAtBlockBoundary(BBQJIT& generator, unsigned targetArity, Stack& expressionStack, bool endOfWasmBlock)
{
// First, we flush all locals that were allocated outside of their designated slots in this block.
for (unsigned i = 0; i < expressionStack.size(); ++i) {
if (expressionStack[i].value().isLocal())
m_touchedLocals.add(expressionStack[i].value().asLocal());
}
for (LocalOrTempIndex touchedLocal : m_touchedLocals) {
Value value = Value::fromLocal(generator.m_localTypes[touchedLocal], touchedLocal);
if (generator.locationOf(value).isRegister())
generator.flushValue(value);
}
// If we are a catch block, we need to flush the exception value, since it's not represented on the expression stack.
if (isAnyCatch(*this)) {
Value value = generator.exception(*this);
if (!endOfWasmBlock)
generator.flushValue(value);
else
generator.consume(value);
}
for (unsigned i = 0; i < expressionStack.size(); ++i) {
Value& value = expressionStack[i].value();
int resultIndex = static_cast<int>(i) - static_cast<int>(expressionStack.size() - targetArity);
// Next, we turn all constants into temporaries, so they can be given persistent slots on the stack.
// If this is the end of the enclosing wasm block, we know we won't need them again, so this can be skipped.
if (value.isConst() && (resultIndex < 0 || !endOfWasmBlock)) {
Value constant = value;
value = Value::fromTemp(value.type(), static_cast<LocalOrTempIndex>(enclosedHeight() + implicitSlots() + i));
Location slot = generator.locationOf(value);
generator.emitMoveConst(constant, slot);
}
// Next, we flush or consume all the temp values on the stack.
if (value.isTemp()) {
if (!endOfWasmBlock)
generator.flushValue(value);
else if (resultIndex < 0)
generator.consume(value);
}
}
}
template<typename Stack, size_t N>
bool addExit(BBQJIT& generator, const Vector<Location, N>& targetLocations, Stack& expressionStack)
{
unsigned targetArity = targetLocations.size();
if (!targetArity)
return false;
// We move all passed temporaries to the successor, in its argument slots.
unsigned offset = expressionStack.size() - targetArity;
Vector<Value, 8> resultValues;
Vector<Location, 8> resultLocations;
for (unsigned i = 0; i < targetArity; ++i) {
resultValues.append(expressionStack[i + offset].value());
resultLocations.append(targetLocations[i]);
}
generator.emitShuffle(resultValues, resultLocations);
return true;
}
template<typename Stack>
void finalizeBlock(BBQJIT& generator, unsigned targetArity, Stack& expressionStack, bool preserveArguments)
{
// Finally, as we are leaving the block, we convert any constants into temporaries on the stack, so we don't blindly assume they have
// the same constant values in the successor.
unsigned offset = expressionStack.size() - targetArity;
for (unsigned i = 0; i < targetArity; ++i) {
Value& value = expressionStack[i + offset].value();
if (value.isConst()) {
Value constant = value;
value = Value::fromTemp(value.type(), static_cast<LocalOrTempIndex>(enclosedHeight() + implicitSlots() + i + offset));
if (preserveArguments)
generator.emitMoveConst(constant, generator.canonicalSlot(value));
} else if (value.isTemp()) {
if (preserveArguments)
generator.flushValue(value);
else
generator.consume(value);
}
}
}
template<typename Stack>
void flushAndSingleExit(BBQJIT& generator, ControlData& target, Stack& expressionStack, bool isChildBlock, bool endOfWasmBlock, bool unreachable = false)
{
// Helper to simplify the common case where we don't need to handle multiple exits.
const auto& targetLocations = isChildBlock ? target.argumentLocations() : target.targetLocations();
flushAtBlockBoundary(generator, targetLocations.size(), expressionStack, endOfWasmBlock);
if (!unreachable)
addExit(generator, targetLocations, expressionStack);
finalizeBlock(generator, targetLocations.size(), expressionStack, false);
}
template<typename Stack>
void startBlock(BBQJIT& generator, Stack& expressionStack)
{
ASSERT(expressionStack.size() >= m_argumentLocations.size());
for (unsigned i = 0; i < m_argumentLocations.size(); ++i) {
ASSERT(!expressionStack[i + expressionStack.size() - m_argumentLocations.size()].value().isConst());
generator.bind(expressionStack[i].value(), m_argumentLocations[i]);
}
}
template<typename Stack>
void resumeBlock(BBQJIT& generator, const ControlData& predecessor, Stack& expressionStack)
{
ASSERT(expressionStack.size() >= predecessor.resultLocations().size());
for (unsigned i = 0; i < predecessor.resultLocations().size(); ++i) {
unsigned offset = expressionStack.size() - predecessor.resultLocations().size();
// Intentionally not using implicitSlots since results should not include implicit slot.
expressionStack[i + offset].value() = Value::fromTemp(expressionStack[i + offset].type().kind, predecessor.enclosedHeight() + i);
generator.bind(expressionStack[i + offset].value(), predecessor.resultLocations()[i]);
}
}
void convertIfToBlock();
void convertLoopToBlock();
void addBranch(Jump jump);
void addLabel(Box<CCallHelpers::Label>&& label);
void delegateJumpsTo(ControlData& delegateTarget);
void linkJumps(MacroAssembler::AbstractMacroAssemblerType* masm);
void linkJumpsTo(MacroAssembler::Label label, MacroAssembler::AbstractMacroAssemblerType* masm);
void linkIfBranch(MacroAssembler::AbstractMacroAssemblerType* masm);
void dump(PrintStream& out) const;
LocalOrTempIndex enclosedHeight() const;
unsigned implicitSlots() const;
const Vector<Location, 2>& targetLocations() const;
const Vector<Location, 2>& argumentLocations() const;
const Vector<Location, 2>& resultLocations() const;
BlockType blockType() const;
BlockSignature signature() const;
FunctionArgCount branchTargetArity() const;
Type branchTargetType(unsigned i) const;
Type argumentType(unsigned i) const;
CatchKind catchKind() const;
void setCatchKind(CatchKind catchKind);
unsigned tryStart() const;
unsigned tryEnd() const;
unsigned tryCatchDepth() const;
void setTryInfo(unsigned tryStart, unsigned tryEnd, unsigned tryCatchDepth);
void setIfBranch(MacroAssembler::Jump branch);
void setLoopLabel(MacroAssembler::Label label);
const MacroAssembler::Label& loopLabel() const;
void touch(LocalOrTempIndex local);
private:
friend class BBQJIT;
void fillLabels(CCallHelpers::Label label);
BlockSignature m_signature;
BlockType m_blockType;
CatchKind m_catchKind { CatchKind::Catch };
Vector<Location, 2> m_argumentLocations; // List of input locations to write values into when entering this block.
Vector<Location, 2> m_resultLocations; // List of result locations to write values into when exiting this block.
JumpList m_branchList; // List of branch control info for branches targeting the end of this block.
Vector<Box<CCallHelpers::Label>> m_labels; // List of labels filled.
MacroAssembler::Label m_loopLabel;
MacroAssembler::Jump m_ifBranch;
LocalOrTempIndex m_enclosedHeight; // Height of enclosed expression stack, used as the base for all temporary locations.
BitVector m_touchedLocals; // Number of locals allocated to registers in this block.
unsigned m_tryStart { 0 };
unsigned m_tryEnd { 0 };
unsigned m_tryCatchDepth { 0 };
};
friend struct ControlData;
using ExpressionType = Value;
using ControlType = ControlData;
using CallType = CallLinkInfo::CallType;
using ResultList = Vector<ExpressionType, 8>;
using ControlEntry = typename FunctionParserTypes<ControlType, ExpressionType, CallType>::ControlEntry;
using TypedExpression = typename FunctionParserTypes<ControlType, ExpressionType, CallType>::TypedExpression;
using Stack = FunctionParser<BBQJIT>::Stack;
using ControlStack = FunctionParser<BBQJIT>::ControlStack;
unsigned stackCheckSize() const { return alignedFrameSize(m_maxCalleeStackSize + m_frameSize); }
private:
unsigned m_loggingIndent = 0;
template<typename... Args>
void logInstructionData(bool first, const Value& value, const Location& location, const Args&... args)
{
if (!first)
dataLog(", ");
dataLog(value);
if (location.kind() != Location::None)
dataLog(":", location);
logInstructionData(false, args...);
}
template<typename... Args>
void logInstructionData(bool first, const Value& value, const Args&... args)
{
if (!first)
dataLog(", ");
dataLog(value);
if (!value.isConst() && !value.isPinned())
dataLog(":", locationOf(value));
logInstructionData(false, args...);
}
template<size_t N, typename OverflowHandler, typename... Args>
void logInstructionData(bool first, const Vector<TypedExpression, N, OverflowHandler>& expressions, const Args&... args)
{
for (const TypedExpression& expression : expressions) {
if (!first)
dataLog(", ");
const Value& value = expression.value();
dataLog(value);
if (!value.isConst() && !value.isPinned())
dataLog(":", locationOf(value));
first = false;
}
logInstructionData(false, args...);
}
template<size_t N, typename OverflowHandler, typename... Args>
void logInstructionData(bool first, const Vector<Value, N, OverflowHandler>& values, const Args&... args)
{
for (const Value& value : values) {
if (!first)
dataLog(", ");
dataLog(value);
if (!value.isConst() && !value.isPinned())
dataLog(":", locationOf(value));
first = false;
}
logInstructionData(false, args...);
}
template<size_t N, typename OverflowHandler, typename... Args>
void logInstructionData(bool first, const Vector<Location, N, OverflowHandler>& values, const Args&... args)
{
for (const Location& value : values) {
if (!first)
dataLog(", ");
dataLog(value);
first = false;
}
logInstructionData(false, args...);
}
inline void logInstructionData(bool)
{
dataLogLn();
}
template<typename... Data>
ALWAYS_INLINE void logInstruction(const char* opcode, SIMDLaneOperation op, const Data&... data)
{
dataLog("BBQ\t");
for (unsigned i = 0; i < m_loggingIndent; i ++)
dataLog(" ");
dataLog(opcode, SIMDLaneOperationDump(op), " ");
logInstructionData(true, data...);
}
template<typename... Data>
ALWAYS_INLINE void logInstruction(const char* opcode, const Data&... data)
{
dataLog("BBQ\t");
for (unsigned i = 0; i < m_loggingIndent; i ++)
dataLog(" ");
dataLog(opcode, " ");
logInstructionData(true, data...);
}
template<typename T>
struct Result {
T value;
Result(const T& value_in)
: value(value_in)
{ }
};
template<typename... Args>
void logInstructionData(bool first, const char* const& literal, const Args&... args)
{
if (!first)
dataLog(" ");
dataLog(literal);
if (!std::strcmp(literal, "=> "))
logInstructionData(true, args...);
else
logInstructionData(false, args...);
}
template<typename T, typename... Args>
void logInstructionData(bool first, const Result<T>& result, const Args&... args)
{
if (!first)
dataLog(" ");
dataLog("=> ");
logInstructionData(true, result.value, args...);
}
template<typename T, typename... Args>
void logInstructionData(bool first, const T& t, const Args&... args)
{
if (!first)
dataLog(", ");
dataLog(t);
logInstructionData(false, args...);
}
#define RESULT(...) Result { __VA_ARGS__ }
#define LOG_INSTRUCTION(...) do { if (UNLIKELY(Options::verboseBBQJITInstructions())) { logInstruction(__VA_ARGS__); } } while (false)
#define LOG_INDENT() do { if (UNLIKELY(Options::verboseBBQJITInstructions())) { m_loggingIndent += 2; } } while (false);
#define LOG_DEDENT() do { if (UNLIKELY(Options::verboseBBQJITInstructions())) { m_loggingIndent -= 2; } } while (false);
public:
static constexpr bool tierSupportsSIMD = true;
BBQJIT(CCallHelpers& jit, const TypeDefinition& signature, BBQCallee& callee, const FunctionData& function, uint32_t functionIndex, const ModuleInformation& info, Vector<UnlinkedWasmToWasmCall>& unlinkedWasmToWasmCalls, MemoryMode mode, InternalFunction* compilation, std::optional<bool> hasExceptionHandlers, unsigned loopIndexForOSREntry, TierUpCount* tierUp);
ALWAYS_INLINE static Value emptyExpression()
{
return Value::none();
}
void setParser(FunctionParser<BBQJIT>* parser);
bool addArguments(const TypeDefinition& signature);
Value addConstant(Type type, uint64_t value);
PartialResult addDrop(Value value);
PartialResult addLocal(Type type, uint32_t numberOfLocals);
Value instanceValue();
// Tables
PartialResult WARN_UNUSED_RETURN addTableGet(unsigned tableIndex, Value index, Value& result);
PartialResult WARN_UNUSED_RETURN addTableSet(unsigned tableIndex, Value index, Value value);
PartialResult WARN_UNUSED_RETURN addTableInit(unsigned elementIndex, unsigned tableIndex, ExpressionType dstOffset, ExpressionType srcOffset, ExpressionType length);
PartialResult WARN_UNUSED_RETURN addElemDrop(unsigned elementIndex);
PartialResult WARN_UNUSED_RETURN addTableSize(unsigned tableIndex, Value& result);
PartialResult WARN_UNUSED_RETURN addTableGrow(unsigned tableIndex, Value fill, Value delta, Value& result);
PartialResult WARN_UNUSED_RETURN addTableFill(unsigned tableIndex, Value offset, Value fill, Value count);
PartialResult WARN_UNUSED_RETURN addTableCopy(unsigned dstTableIndex, unsigned srcTableIndex, Value dstOffset, Value srcOffset, Value length);
// Locals
PartialResult WARN_UNUSED_RETURN getLocal(uint32_t localIndex, Value& result);
PartialResult WARN_UNUSED_RETURN setLocal(uint32_t localIndex, Value value);
// Globals
Value topValue(TypeKind type);
Value exception(const ControlData& control);
PartialResult WARN_UNUSED_RETURN getGlobal(uint32_t index, Value& result);
void emitWriteBarrier(GPRReg cellGPR);
PartialResult WARN_UNUSED_RETURN setGlobal(uint32_t index, Value value);
// Memory
inline Location emitCheckAndPreparePointer(Value pointer, uint32_t uoffset, uint32_t sizeOfOperation)
{
ScratchScope<1, 0> scratches(*this);
Location pointerLocation;
if (pointer.isConst()) {
pointerLocation = Location::fromGPR(scratches.gpr(0));
emitMoveConst(pointer, pointerLocation);
} else
pointerLocation = loadIfNecessary(pointer);
ASSERT(pointerLocation.isGPR());
#if USE(JSVALUE32_64)
ScratchScope<2, 0> globals(*this);
GPRReg wasmBaseMemoryPointer = globals.gpr(0);
GPRReg wasmBoundsCheckingSizeRegister = globals.gpr(1);
loadWebAssemblyGlobalState(wasmBaseMemoryPointer, wasmBoundsCheckingSizeRegister);
#endif
uint64_t boundary = static_cast<uint64_t>(sizeOfOperation) + uoffset - 1;
switch (m_mode) {
case MemoryMode::BoundsChecking: {
// We're not using signal handling only when the memory is not shared.
// Regardless of signaling, we must check that no memory access exceeds the current memory size.
m_jit.zeroExtend32ToWord(pointerLocation.asGPR(), wasmScratchGPR);
if (boundary)
m_jit.addPtr(TrustedImmPtr(boundary), wasmScratchGPR);
throwExceptionIf(ExceptionType::OutOfBoundsMemoryAccess, m_jit.branchPtr(RelationalCondition::AboveOrEqual, wasmScratchGPR, wasmBoundsCheckingSizeRegister));
break;
}
case MemoryMode::Signaling: {
// We've virtually mapped 4GiB+redzone for this memory. Only the user-allocated pages are addressable, contiguously in range [0, current],
// and everything above is mapped PROT_NONE. We don't need to perform any explicit bounds check in the 4GiB range because WebAssembly register
// memory accesses are 32-bit. However WebAssembly register + offset accesses perform the addition in 64-bit which can push an access above
// the 32-bit limit (the offset is unsigned 32-bit). The redzone will catch most small offsets, and we'll explicitly bounds check any
// register + large offset access. We don't think this will be generated frequently.
//
// We could check that register + large offset doesn't exceed 4GiB+redzone since that's technically the limit we need to avoid overflowing the
// PROT_NONE region, but it's better if we use a smaller immediate because it can codegens better. We know that anything equal to or greater
// than the declared 'maximum' will trap, so we can compare against that number. If there was no declared 'maximum' then we still know that
// any access equal to or greater than 4GiB will trap, no need to add the redzone.
if (uoffset >= Memory::fastMappedRedzoneBytes()) {
uint64_t maximum = m_info.memory.maximum() ? m_info.memory.maximum().bytes() : std::numeric_limits<uint32_t>::max();
m_jit.zeroExtend32ToWord(pointerLocation.asGPR(), wasmScratchGPR);
if (boundary)
m_jit.addPtr(TrustedImmPtr(boundary), wasmScratchGPR);
throwExceptionIf(ExceptionType::OutOfBoundsMemoryAccess, m_jit.branchPtr(RelationalCondition::AboveOrEqual, wasmScratchGPR, TrustedImmPtr(static_cast<int64_t>(maximum))));
}
break;
}
}
#if CPU(ARM64)
m_jit.addZeroExtend64(wasmBaseMemoryPointer, pointerLocation.asGPR(), wasmScratchGPR);
#else
m_jit.zeroExtend32ToWord(pointerLocation.asGPR(), wasmScratchGPR);
m_jit.addPtr(wasmBaseMemoryPointer, wasmScratchGPR);
#endif
consume(pointer);
return Location::fromGPR(wasmScratchGPR);
}
template<typename Functor>
auto emitCheckAndPrepareAndMaterializePointerApply(Value pointer, uint32_t uoffset, uint32_t sizeOfOperation, Functor&& functor) -> decltype(auto);
static inline uint32_t sizeOfLoadOp(LoadOpType op)
{
switch (op) {
case LoadOpType::I32Load8S:
case LoadOpType::I32Load8U:
case LoadOpType::I64Load8S:
case LoadOpType::I64Load8U:
return 1;
case LoadOpType::I32Load16S:
case LoadOpType::I64Load16S:
case LoadOpType::I32Load16U:
case LoadOpType::I64Load16U:
return 2;
case LoadOpType::I32Load:
case LoadOpType::I64Load32S:
case LoadOpType::I64Load32U:
case LoadOpType::F32Load:
return 4;
case LoadOpType::I64Load:
case LoadOpType::F64Load:
return 8;
}
RELEASE_ASSERT_NOT_REACHED();
}
static inline TypeKind typeOfLoadOp(LoadOpType op)
{
switch (op) {