-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathWasmIPIntGenerator.cpp
More file actions
3237 lines (2740 loc) · 125 KB
/
WasmIPIntGenerator.cpp
File metadata and controls
3237 lines (2740 loc) · 125 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) 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.
*/
#include "config.h"
#include "WasmIPIntGenerator.h"
#include <cstdint>
#if ENABLE(WEBASSEMBLY)
#include "BytecodeGeneratorBaseInlines.h"
#include "BytecodeStructs.h"
#include "InstructionStream.h"
#include "JSCJSValueInlines.h"
#include "Label.h"
#include "WasmCallingConvention.h"
#include "WasmContext.h"
#include "WasmFunctionIPIntMetadataGenerator.h"
#include "WasmFunctionParser.h"
#include "WasmModuleDebugInfo.h"
#include <wtf/Assertions.h>
#include <wtf/CompletionHandler.h>
#include <wtf/RefPtr.h>
/*
* WebAssembly in-place interpreter metadata generator
*
* docs by Daniel Liu <daniel_liu4@apple.com / danlliu@umich.edu>; 2023 intern project
*
* 1. Why Metadata?
* ----------------
*
* WebAssembly's bytecode format isn't always the easiest to interpret by itself: jumps would require parsing
* through many bytes to find their target, constants are stored in LEB128, and a myriad of other reasons.
* For IPInt, we design metadata to act as "supporting information" for the interpreter, allowing it to quickly
* find important values such as constants, indices, and branch targets.
*
* FIXME: We should consider not aligning on Apple ARM64 cores since they don't typically have a penatly for unaligned loads/stores.
*
* 2. Metadata Structure
* ---------------------
*
* Metadata is kept in a vector of UInt8 (bytes). We handle metadata in "metadata entries", which are groups of
* 8 metadata bytes. We keep metadata aligned to 8B to improve access times. Sometimes, this results in higher
* memory overhead; however, these cases are relatively sparse. Each instruction pushes a certain number of
* entries to the metadata vector.
*
* 3. Metadata for Instructions
* ----------------------------
*
* block (0x02): 1 entry; 8B PC of next instruction
* loop (0x03): 1 entry; 8B PC of next instruction
* if (0x04): 2 entries; 4B new PC, 4B new MC for `else`, 8B new PC for `if`
* else (0x05): 1 entry; 4B new PC, 4B new MC for `end`
* end (0x0b): If exiting the function: ceil((# return values + 2) / 8) entries; 2B for total entry size, 1B / value returned
* br (0x0c): 2 entries; 4B new PC, 4B new MC, 2B number of values to pop, 2B arity, 4B PC after br
* br_if (0x0d): 2 entries; same as br
* br_table (0x0e): 1 + 2n entries for n branches: 8B number of targets; n br metadata entries
* local.get (0x20): 1 entry; 4B index of local, 4B size of instruction
* local.set (0x21): 1 entry; 4B index of local, 4B size of instruction
* local.tee (0x22): 2 entries because of how FunctionParser works
* global.get (0x23): 1 entry; 4B index of global, 4B size of instruction
* global.set (0x24): 1 entry; 4B index of global, 4B size of instruction
* table.get (0x23): 1 entry; 4B index of table, 4B size of instruction
* table.set (0x24): 1 entry; 4B index of table, 4B size of instruction
* mem load (0x28 - 0x35): 1 entry; 4B memarg, 4B size of instruction
* mem store (0x28 - 0x35): 1 entry; 4B memarg, 4B size of instruction
* i32.const (0x41): 1 entry; 4B value, 4B size of instruction
* i64.const (0x42): 2 entries; 8B value, 8B size of instruction
*
* i32, i64, f32, and f64 operations (besides the ones shown above) do not require metadata
*
*/
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN
#if ENABLE(WEBASSEMBLY_DEBUGGER)
#define RECORD_NEXT_INSTRUCTION(fromPC, toPC) \
do { \
if (Options::enableWasmDebugger()) [[unlikely]] { \
if (m_debugInfo) { \
uint32_t fromOffset = fromPC + m_metadata->m_bytecodeOffset + m_functionStartByteOffset; \
uint32_t toOffset = toPC + m_metadata->m_bytecodeOffset + m_functionStartByteOffset; \
m_debugInfo->addNextInstruction(fromOffset, toOffset); \
} \
} \
} while (0)
#else
#define RECORD_NEXT_INSTRUCTION(fromPC, toPC) do { (void)(fromPC); (void)(toPC); } while (0)
#endif
namespace JSC { namespace Wasm {
using ErrorType = String;
using PartialResult = Expected<void, ErrorType>;
using UnexpectedResult = Unexpected<ErrorType>;
struct Value { };
// ControlBlock
struct IPIntLocation {
uint32_t pc;
uint32_t mc;
};
struct IPIntControlType {
friend class IPIntGenerator;
IPIntControlType()
{
}
IPIntControlType(BlockSignature&& signature, uint32_t stackSize, BlockType blockType, CatchKind catchKind = CatchKind::Catch)
: m_signature(WTF::move(signature))
, m_blockType(blockType)
, m_catchKind(catchKind)
, m_stackSize(stackSize)
{ }
static bool isIf(const IPIntControlType& control) { return control.blockType() == BlockType::If; }
static bool isElse(const IPIntControlType& control) { return control.blockType() == BlockType::Else; }
static bool isTry(const IPIntControlType& control) { return control.blockType() == BlockType::Try; }
static bool isTryTable(const IPIntControlType& control) { return control.blockType() == BlockType::TryTable; }
static bool isAnyCatch(const IPIntControlType& control) { return control.blockType() == BlockType::Catch; }
static bool isTopLevel(const IPIntControlType& control) { return control.blockType() == BlockType::TopLevel; }
static bool isLoop(const IPIntControlType& control) { return control.blockType() == BlockType::Loop; }
static bool isBlock(const IPIntControlType& control) { return control.blockType() == BlockType::Block; }
static bool isCatch(const IPIntControlType& control)
{
if (control.blockType() != BlockType::Catch)
return false;
return control.catchKind() == CatchKind::Catch;
}
void dump(PrintStream&) const
{ }
BlockType blockType() const { return m_blockType; }
CatchKind catchKind() const { return m_catchKind; }
const BlockSignature& signature() const { return m_signature; }
unsigned stackSize() const { return m_stackSize; }
Type branchTargetType(unsigned i) const
{
ASSERT(i < branchTargetArity());
if (blockType() == BlockType::Loop)
return m_signature.argumentType(i);
return m_signature.returnType(i);
}
unsigned branchTargetArity() const
{
return isLoop(*this)
? m_signature.argumentCount()
: m_signature.returnCount();
}
private:
BlockSignature m_signature;
BlockType m_blockType;
CatchKind m_catchKind;
int32_t m_pendingOffset { -1 };
uint32_t m_index { 0 };
uint32_t m_pc { 0 }; // where am i?
uint32_t m_mc { 0 };
uint32_t m_pcEnd { 0 };
uint32_t m_stackSize { 0 };
uint32_t m_tryDepth { 0 };
Vector<IPIntLocation> m_catchesAwaitingFixup;
struct TryTableTarget {
CatchKind type;
uint32_t tag;
const TypeDefinition* exceptionSignature;
ControlRef target;
};
Vector<TryTableTarget> m_tryTableTargets;
};
class IPIntGenerator {
public:
IPIntGenerator(ModuleInformation&, FunctionCodeIndex, const TypeDefinition&, std::span<const uint8_t>, FunctionDebugInfo* = nullptr);
static constexpr bool shouldFuseBranchCompare = false;
using ControlType = IPIntControlType;
using ExpressionType = Value;
using CallType = CallLinkInfo::CallType;
using ResultList = Vector<Value, 8>;
using ExpressionList = Vector<Value, 1>;
using ControlEntry = FunctionParser<IPIntGenerator>::ControlEntry;
using ControlStack = FunctionParser<IPIntGenerator>::ControlStack;
using Stack = FunctionParser<IPIntGenerator>::Stack;
using TypedExpression = FunctionParser<IPIntGenerator>::TypedExpression;
using CatchHandler = FunctionParser<IPIntGenerator>::CatchHandler;
using ArgumentList = FunctionParser<IPIntGenerator>::ArgumentList;
static ExpressionType emptyExpression() { return { }; };
[[nodiscard]] PartialResult addDrop(ExpressionType);
template <typename ...Args>
[[nodiscard]] NEVER_INLINE UnexpectedResult fail(Args... args) const
{
using namespace FailureHelper; // See ADL comment in WasmParser.h.
return UnexpectedResult(makeString("WebAssembly.Module failed compiling: "_s, makeString(args)...));
}
#define WASM_COMPILE_FAIL_IF(condition, ...) do { \
if (condition) [[unlikely]] \
return fail(__VA_ARGS__); \
} while (0)
std::unique_ptr<FunctionIPIntMetadataGenerator> finalize();
[[nodiscard]] PartialResult addArguments(const TypeDefinition&);
[[nodiscard]] PartialResult addLocal(Type, uint32_t);
Value addConstant(Type, uint64_t);
// SIMD
bool usesSIMD() { return m_usesSIMD; }
void notifyFunctionUsesSIMD() { ASSERT(Options::useWasmSIMD()); m_usesSIMD = true; }
[[nodiscard]] PartialResult addSIMDLoad(ExpressionType, uint32_t, ExpressionType&);
[[nodiscard]] PartialResult addSIMDStore(ExpressionType, ExpressionType, uint32_t);
[[nodiscard]] PartialResult addSIMDSplat(SIMDLane, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addSIMDShuffle(v128_t, ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addSIMDShift(SIMDLaneOperation, SIMDInfo, ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addSIMDExtmul(SIMDLaneOperation, SIMDInfo, ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addSIMDLoadSplat(SIMDLaneOperation, ExpressionType, uint32_t, ExpressionType&);
[[nodiscard]] PartialResult addSIMDLoadLane(SIMDLaneOperation, ExpressionType, ExpressionType, uint32_t, uint8_t, ExpressionType&);
[[nodiscard]] PartialResult addSIMDStoreLane(SIMDLaneOperation, ExpressionType, ExpressionType, uint32_t, uint8_t);
[[nodiscard]] PartialResult addSIMDLoadExtend(SIMDLaneOperation, ExpressionType, uint32_t, ExpressionType&);
[[nodiscard]] PartialResult addSIMDLoadPad(SIMDLaneOperation, ExpressionType, uint32_t, ExpressionType&);
ExpressionType addSIMDConstant(v128_t);
// SIMD generated
[[nodiscard]] PartialResult addSIMDExtractLane(SIMDInfo, uint8_t, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addSIMDReplaceLane(SIMDInfo, uint8_t, ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addSIMDI_V(SIMDLaneOperation, SIMDInfo, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addSIMDV_V(SIMDLaneOperation, SIMDInfo, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addSIMDBitwiseSelect(ExpressionType, ExpressionType, ExpressionType, ExpressionType&);
#if ENABLE(B3_JIT)
[[nodiscard]] PartialResult addSIMDRelOp(SIMDLaneOperation, SIMDInfo, ExpressionType, ExpressionType, B3::Air::Arg, ExpressionType&);
#endif
[[nodiscard]] PartialResult addSIMDV_VV(SIMDLaneOperation, SIMDInfo, ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addSIMDRelaxedFMA(SIMDLaneOperation, SIMDInfo, ExpressionType, ExpressionType, ExpressionType, ExpressionType&);
// References
[[nodiscard]] PartialResult addRefIsNull(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addRefFunc(FunctionSpaceIndex, ExpressionType&);
[[nodiscard]] PartialResult addRefAsNonNull(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addRefEq(ExpressionType, ExpressionType, ExpressionType&);
// Tables
[[nodiscard]] PartialResult addTableGet(unsigned, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addTableSet(unsigned, ExpressionType, ExpressionType);
[[nodiscard]] PartialResult addTableInit(unsigned, unsigned, ExpressionType, ExpressionType, ExpressionType);
[[nodiscard]] PartialResult addElemDrop(unsigned);
[[nodiscard]] PartialResult addTableSize(unsigned, ExpressionType&);
[[nodiscard]] PartialResult addTableGrow(unsigned, ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addTableFill(unsigned, ExpressionType, ExpressionType, ExpressionType);
[[nodiscard]] PartialResult addTableCopy(unsigned, unsigned, ExpressionType, ExpressionType, ExpressionType);
// Locals
[[nodiscard]] PartialResult getLocal(uint32_t index, ExpressionType&);
[[nodiscard]] PartialResult setLocal(uint32_t, ExpressionType);
[[nodiscard]] PartialResult teeLocal(uint32_t, ExpressionType, ExpressionType& result);
// Globals
[[nodiscard]] PartialResult getGlobal(uint32_t, ExpressionType&);
[[nodiscard]] PartialResult setGlobal(uint32_t, ExpressionType);
// Memory
[[nodiscard]] PartialResult load(LoadOpType, ExpressionType, ExpressionType&, uint64_t, uint8_t);
[[nodiscard]] PartialResult store(StoreOpType, ExpressionType, ExpressionType, uint64_t, uint8_t);
[[nodiscard]] PartialResult addGrowMemory(ExpressionType, ExpressionType&, uint8_t);
[[nodiscard]] PartialResult addCurrentMemory(ExpressionType&, uint8_t);
[[nodiscard]] PartialResult addMemoryFill(ExpressionType, ExpressionType, ExpressionType, uint8_t);
[[nodiscard]] PartialResult addMemoryCopy(ExpressionType, ExpressionType, ExpressionType, uint8_t, uint8_t);
[[nodiscard]] PartialResult addMemoryInit(unsigned, ExpressionType, ExpressionType, ExpressionType, uint8_t);
[[nodiscard]] PartialResult addDataDrop(unsigned);
// Atomics
[[nodiscard]] PartialResult atomicLoad(ExtAtomicOpType, Type, ExpressionType, ExpressionType&, uint32_t);
[[nodiscard]] PartialResult atomicStore(ExtAtomicOpType, Type, ExpressionType, ExpressionType, uint32_t);
[[nodiscard]] PartialResult atomicBinaryRMW(ExtAtomicOpType, Type, ExpressionType, ExpressionType, ExpressionType&, uint32_t);
[[nodiscard]] PartialResult atomicCompareExchange(ExtAtomicOpType, Type, ExpressionType, ExpressionType, ExpressionType, ExpressionType&, uint32_t);
[[nodiscard]] PartialResult atomicWait(ExtAtomicOpType, ExpressionType, ExpressionType, ExpressionType, ExpressionType&, uint32_t);
[[nodiscard]] PartialResult atomicNotify(ExtAtomicOpType, ExpressionType, ExpressionType, ExpressionType&, uint32_t);
[[nodiscard]] PartialResult atomicFence(ExtAtomicOpType, uint8_t);
// Saturated truncation
[[nodiscard]] PartialResult truncSaturated(Ext1OpType, ExpressionType, ExpressionType&, Type, Type);
// GC
[[nodiscard]] PartialResult addRefI31(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI31GetS(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI31GetU(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addArrayNew(uint32_t, ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addArrayNewDefault(uint32_t, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addArrayNewData(uint32_t, uint32_t, ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addArrayNewElem(uint32_t, uint32_t, ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addArrayNewFixed(uint32_t, ArgumentList&, ExpressionType&);
[[nodiscard]] PartialResult addArrayGet(ExtGCOpType, uint32_t, ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addArraySet(uint32_t, ExpressionType, ExpressionType, ExpressionType);
[[nodiscard]] PartialResult addArrayLen(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addArrayFill(uint32_t, ExpressionType, ExpressionType, ExpressionType, ExpressionType);
[[nodiscard]] PartialResult addArrayCopy(uint32_t, ExpressionType, ExpressionType, uint32_t, ExpressionType, ExpressionType, ExpressionType);
[[nodiscard]] PartialResult addArrayInitElem(uint32_t, ExpressionType, ExpressionType, uint32_t, ExpressionType, ExpressionType);
[[nodiscard]] PartialResult addArrayInitData(uint32_t, ExpressionType, ExpressionType, uint32_t, ExpressionType, ExpressionType);
[[nodiscard]] PartialResult addStructNew(uint32_t, ArgumentList&, ExpressionType&);
[[nodiscard]] PartialResult addStructNewDefault(uint32_t, ExpressionType&);
[[nodiscard]] PartialResult addStructGet(ExtGCOpType, ExpressionType, const StructType&, const RTT&, uint32_t, ExpressionType&);
[[nodiscard]] PartialResult addStructSet(ExpressionType, const StructType&, const RTT&, uint32_t, ExpressionType);
[[nodiscard]] PartialResult addRefTest(ExpressionType, bool, int32_t, bool, ExpressionType&);
[[nodiscard]] PartialResult addRefCast(ExpressionType, bool, int32_t, ExpressionType&);
[[nodiscard]] PartialResult addAnyConvertExtern(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addExternConvertAny(ExpressionType, ExpressionType&);
// Basic operators
[[nodiscard]] PartialResult addI32DivS(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI32RemS(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI32DivU(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI32RemU(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI64DivS(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI64RemS(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI64DivU(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI64RemU(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI32Ctz(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI32Popcnt(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI64Popcnt(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF64Nearest(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF32Nearest(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF64Trunc(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF32Trunc(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI32TruncSF64(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI32TruncSF32(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI32TruncUF64(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI32TruncUF32(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI64TruncSF64(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI64TruncSF32(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI64TruncUF64(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI64TruncUF32(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF32Ceil(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI32Mul(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI32Sub(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF64Le(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF32DemoteF64(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF64Ne(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF64Lt(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF32Min(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF32Max(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF64Min(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF64Max(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF64Mul(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF32Div(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI32Clz(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF32Copysign(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF32ReinterpretI32(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF32Ne(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF64Gt(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF32Sqrt(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF64Ge(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI64GtS(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI64GtU(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF64Div(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF32Add(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI32LeU(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI32LeS(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI64Ne(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI64Clz(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF32Neg(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI32And(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI32LtU(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI64Rotr(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF64Abs(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI32LtS(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI32Eq(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF64Copysign(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF32ConvertSI64(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI64Rotl(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF32Lt(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF64ConvertSI32(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF64Eq(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF32Le(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF32Ge(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI32ShrU(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF32ConvertUI32(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI32ShrS(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI32GeU(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF64Ceil(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI32GeS(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI32Shl(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF64Floor(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI32Xor(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF32Abs(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF32Mul(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI64Sub(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI32ReinterpretF32(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI32Add(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF64Sub(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI32Or(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI64LtU(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI64LtS(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF64ConvertSI64(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI64Xor(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI64GeU(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI64Mul(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF32Sub(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF64PromoteF32(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF64Add(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI64GeS(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI64ExtendUI32(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI32Ne(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF64ReinterpretI64(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF32Eq(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI64Eq(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF32Floor(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF32ConvertSI32(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI64And(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI64Or(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI64Ctz(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI32Eqz(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI64Eqz(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI64ReinterpretF64(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF64ConvertUI32(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF32ConvertUI64(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF64ConvertUI64(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI64ShrS(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI64ShrU(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF64Sqrt(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI64Shl(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF32Gt(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI32WrapI64(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI32Rotl(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI32Rotr(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI32GtU(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI64ExtendSI32(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI32Extend8S(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI32Extend16S(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI64Extend8S(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI64Extend16S(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI64Extend32S(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI32GtS(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addF64Neg(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI64LeU(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI64LeS(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addI64Add(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult addSelect(ExpressionType, ExpressionType, ExpressionType, ExpressionType&);
// Control flow
[[nodiscard]] ControlType addTopLevel(BlockSignature&&);
[[nodiscard]] PartialResult addBlock(BlockSignature&&, Stack&, ControlType&, Stack&);
[[nodiscard]] PartialResult addLoop(BlockSignature&&, Stack&, ControlType&, Stack&, uint32_t);
[[nodiscard]] PartialResult addIf(ExpressionType, BlockSignature&&, Stack&, ControlType&, Stack&);
[[nodiscard]] PartialResult addElse(ControlType&, Stack&);
[[nodiscard]] PartialResult addElseToUnreachable(ControlType&);
[[nodiscard]] PartialResult addTry(BlockSignature&&, Stack&, ControlType&, Stack&);
[[nodiscard]] PartialResult addTryTable(BlockSignature&&, Stack& enclosingStack, const Vector<CatchHandler>& targets, ControlType& result, Stack& newStack);
[[nodiscard]] PartialResult addCatch(unsigned, const TypeDefinition&, Stack&, ControlType&, ResultList&);
[[nodiscard]] PartialResult addCatchToUnreachable(unsigned, const TypeDefinition&, ControlType&, ResultList&);
[[nodiscard]] PartialResult addCatchAll(Stack&, ControlType&);
[[nodiscard]] PartialResult addCatchAllToUnreachable(ControlType&);
[[nodiscard]] PartialResult addDelegate(ControlType&, ControlType&);
[[nodiscard]] PartialResult addDelegateToUnreachable(ControlType&, ControlType&);
[[nodiscard]] PartialResult addThrow(unsigned, ArgumentList&, Stack&);
[[nodiscard]] PartialResult addRethrow(unsigned, ControlType&);
[[nodiscard]] PartialResult addThrowRef(ExpressionType, Stack&);
[[nodiscard]] PartialResult addReturn(const ControlType&, const Stack&);
[[nodiscard]] PartialResult addBranch(ControlType&, ExpressionType, const Stack&);
[[nodiscard]] PartialResult addBranchNull(ControlType&, ExpressionType, Stack&, bool, ExpressionType&);
[[nodiscard]] PartialResult addBranchCast(ControlType&, ExpressionType, Stack&, bool, int32_t, bool);
[[nodiscard]] PartialResult addSwitch(ExpressionType, const Vector<ControlType*>&, ControlType&, const Stack&);
[[nodiscard]] PartialResult endBlock(ControlEntry&, Stack&);
void endTryTable(const ControlType& data);
[[nodiscard]] PartialResult addEndToUnreachable(ControlEntry&, Stack&);
[[nodiscard]] PartialResult endTopLevel(const Stack&);
// Fused comparison stubs (TODO: make use of these for better codegen)
[[nodiscard]] PartialResult addFusedBranchCompare(OpType, ControlType&, ExpressionType, const Stack&) { RELEASE_ASSERT_NOT_REACHED(); }
[[nodiscard]] PartialResult addFusedBranchCompare(OpType, ControlType&, ExpressionType, ExpressionType, const Stack&) { RELEASE_ASSERT_NOT_REACHED(); }
[[nodiscard]] PartialResult addFusedIfCompare(OpType, ExpressionType, BlockSignature&&, Stack&, ControlType&, Stack&) { RELEASE_ASSERT_NOT_REACHED(); }
[[nodiscard]] PartialResult addFusedIfCompare(OpType, ExpressionType, ExpressionType, BlockSignature&&, Stack&, ControlType&, Stack&) { RELEASE_ASSERT_NOT_REACHED(); }
// Calls
[[nodiscard]] PartialResult addCall(unsigned, FunctionSpaceIndex, const TypeDefinition&, ArgumentList&, ResultList&, CallType = CallType::Call);
[[nodiscard]] PartialResult addCallIndirect(unsigned, unsigned, const TypeDefinition&, ArgumentList&, ResultList&, CallType = CallType::Call);
[[nodiscard]] PartialResult addCallRef(unsigned, const TypeDefinition&, ArgumentList&, ResultList&, CallType = CallType::Call);
[[nodiscard]] PartialResult addUnreachable();
[[nodiscard]] PartialResult addCrash();
void setParser(FunctionParser<IPIntGenerator>* parser) { m_parser = parser; };
size_t getCurrentInstructionLength()
{
return m_parser->offset() - m_parser->currentOpcodeStartingOffset();
}
void addCallCommonData(const FunctionSignature&, const CallInformation&);
void addTailCallCommonData(const FunctionSignature&, const CallInformation&);
void didFinishParsingLocals()
{
m_metadata->m_bytecodeOffset = m_parser->offset();
}
void didPopValueFromStack(ExpressionType, ASCIILiteral) { }
void willParseOpcode() { }
void willParseExtendedOpcode() { }
void didParseOpcode()
{
if (!m_parser->unreachableBlocks()) {
ASSERT(m_parser->getStackHeightInValues() == m_stackSize.value());
if (Options::enableWasmDebugger()) [[unlikely]] {
if (m_debugInfo) {
OpType currentOpcode = m_parser->currentOpcode();
bool isControlFlowInstruction = Wasm::isControlFlowInstructionWithExtGC(currentOpcode, [this]() {
return m_parser->currentExtendedOpcode();
});
if (!isControlFlowInstruction || currentOpcode == AnnotatedSelect)
RECORD_NEXT_INSTRUCTION(curPC(), nextPC());
}
}
}
}
void dump(const ControlStack&, const Stack*);
void convertTryToCatch(ControlType& tryBlock, CatchKind);
ALWAYS_INLINE void changeStackSize(int delta)
{
m_stackSize += delta;
if (delta > 0)
m_maxStackSize = std::max(m_maxStackSize, m_stackSize.value());
}
void coalesceControlFlow(bool force = false);
void resolveEntryTarget(unsigned, IPIntLocation);
void resolveExitTarget(unsigned, IPIntLocation);
void tryToResolveEntryTarget(uint32_t index, IPIntLocation loc, uint8_t*)
{
m_controlStructuresAwaitingCoalescing[index].m_awaitingEntryTarget.append(loc);
}
void tryToResolveExitTarget(uint32_t index, IPIntLocation loc, uint8_t*)
{
m_controlStructuresAwaitingCoalescing[index].m_awaitingExitTarget.append(loc);
}
void tryToResolveBranchTarget(ControlType& targetBlock, IPIntLocation loc, uint8_t* metadata)
{
if (ControlType::isTopLevel(targetBlock)) {
m_jumpLocationsAwaitingEnd.append(loc);
return;
}
auto index = targetBlock.m_index;
auto& target = m_controlStructuresAwaitingCoalescing[index];
if (target.isLoop) {
ASSERT(target.m_entryResolved);
IPInt::BlockMetadata md = { static_cast<int32_t>(target.m_entryTarget.pc - loc.pc), static_cast<int32_t>(target.m_entryTarget.mc - loc.mc) };
WRITE_TO_METADATA(metadata + loc.mc, md, IPInt::BlockMetadata);
RECORD_NEXT_INSTRUCTION(loc.pc, target.m_entryTarget.pc);
} else {
ASSERT(!target.m_exitResolved);
target.m_awaitingBranchTarget.append(loc);
}
}
ALWAYS_INLINE const CallInformation& cachedCallInformationFor(const FunctionSignature& signature)
{
if (m_cachedSignature != &signature) {
m_cachedSignature = &signature;
m_cachedCallBytecode.shrink(0);
m_cachedCallInformation = wasmCallingConvention().callInformationFor(signature, CallRole::Caller);
}
return m_cachedCallInformation;
}
static constexpr bool tierSupportsSIMD() { return true; }
static constexpr bool validateFunctionBodySize = true;
private:
Checked<uint32_t> m_stackSize { 0 };
uint32_t m_maxStackSize { 0 };
Checked<uint32_t> m_tryDepth { 0 };
uint32_t m_maxTryDepth { 0 };
FunctionParser<IPIntGenerator>* m_parser { nullptr };
ModuleInformation& m_info;
const FunctionCodeIndex m_functionIndex;
std::unique_ptr<FunctionIPIntMetadataGenerator> m_metadata;
struct ControlStructureAwaitingCoalescing {
Vector<IPIntLocation, 16> m_awaitingEntryTarget { };
Vector<IPIntLocation, 16> m_awaitingBranchTarget { };
Vector<IPIntLocation, 16> m_awaitingExitTarget { };
IPIntLocation m_entryTarget { 0, 0 }; // where do we go when entering normally?
IPIntLocation m_exitTarget { 0, 0 }; // where do we go when leaving?
uint32_t startPC { 0 };
bool isLoop { false };
bool m_entryResolved { false };
bool m_exitResolved { false };
};
Vector<ControlStructureAwaitingCoalescing, 16> m_controlStructuresAwaitingCoalescing;
struct QueuedCoalesceRequest {
size_t index;
bool isEntry;
};
Vector<QueuedCoalesceRequest, 16> m_coalesceQueue;
// if this is 0, all our control structures have been coalesced and we can clean up the vector
unsigned m_coalesceDebt { 0 };
// exit loations can still be unresolved when the ControlType* dies, so we put them here
Vector<IPIntLocation> m_exitHandlersAwaitingCoalescing;
// all jumps that go to the top level and return
Vector<IPIntLocation> m_jumpLocationsAwaitingEnd;
inline uint32_t curPC() { return m_parser->currentOpcodeStartingOffset() - m_metadata->m_bytecodeOffset; }
inline uint32_t nextPC() { return m_parser->offset() - m_metadata->m_bytecodeOffset; }
inline uint32_t curMC() { return m_metadata->m_metadata.size(); }
CallInformation m_cachedCallInformation { };
const FunctionSignature* m_cachedSignature { nullptr };
Vector<uint8_t, 16> m_cachedCallBytecode;
Checked<int32_t> m_argumentAndResultsStackSize;
bool m_usesRethrow { false };
bool m_usesSIMD { false };
size_t m_functionStartByteOffset { 0 };
FunctionDebugInfo* m_debugInfo { nullptr };
};
// use if (true) to avoid warnings.
#define IPINT_UNIMPLEMENTED { if (true) { CRASH(); } return { }; }
IPIntGenerator::IPIntGenerator(ModuleInformation& info, FunctionCodeIndex functionIndex, const TypeDefinition&, std::span<const uint8_t> bytecode, FunctionDebugInfo* debugInfo)
: m_info(info)
, m_functionIndex(functionIndex)
, m_metadata(WTF::makeUnique<FunctionIPIntMetadataGenerator>(functionIndex, bytecode))
, m_functionStartByteOffset(info.functions[functionIndex].start)
, m_debugInfo(debugInfo)
{
}
[[nodiscard]] PartialResult IPIntGenerator::addDrop(ExpressionType)
{
changeStackSize(-1);
return { };
}
Value IPIntGenerator::addConstant(Type type, uint64_t value)
{
changeStackSize(1);
m_metadata->addLEB128ConstantAndLengthForType(type, value, getCurrentInstructionLength());
return { };
}
// SIMD
[[nodiscard]] PartialResult IPIntGenerator::addSIMDLoad(ExpressionType, uint32_t offset, ExpressionType&)
{
changeStackSize(0); // Pop address, push v128 value (net change = 0)
m_metadata->addLEB128ConstantInt32AndLength(offset, getCurrentInstructionLength());
return { };
}
[[nodiscard]] PartialResult IPIntGenerator::addSIMDStore(ExpressionType, ExpressionType, uint32_t offset)
{
changeStackSize(-2); // Pop address and v128 value
m_metadata->addLEB128ConstantInt32AndLength(offset, getCurrentInstructionLength());
return { };
}
[[nodiscard]] PartialResult IPIntGenerator::addSIMDSplat(SIMDLane, ExpressionType, ExpressionType&)
{
m_metadata->addLength(getCurrentInstructionLength());
return { };
}
[[nodiscard]] PartialResult IPIntGenerator::addSIMDShuffle(v128_t, ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
m_metadata->addLength(getCurrentInstructionLength());
return { };
}
[[nodiscard]] PartialResult IPIntGenerator::addSIMDShift(SIMDLaneOperation, SIMDInfo, ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
m_metadata->addLength(getCurrentInstructionLength());
return { };
}
[[nodiscard]] PartialResult IPIntGenerator::addSIMDExtmul(SIMDLaneOperation, SIMDInfo, ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
m_metadata->addLength(getCurrentInstructionLength());
return { };
}
[[nodiscard]] PartialResult IPIntGenerator::addSIMDLoadSplat(SIMDLaneOperation, ExpressionType pointer, uint32_t offset, ExpressionType& result)
{
return addSIMDLoad(pointer, offset, result);
}
[[nodiscard]] PartialResult IPIntGenerator::addSIMDLoadLane(SIMDLaneOperation, ExpressionType, ExpressionType, uint32_t offset, uint8_t, ExpressionType&)
{
changeStackSize(-1);
m_metadata->addLEB128ConstantInt32AndLength(offset, getCurrentInstructionLength());
return { };
}
[[nodiscard]] PartialResult IPIntGenerator::addSIMDStoreLane(SIMDLaneOperation, ExpressionType, ExpressionType, uint32_t offset, uint8_t)
{
changeStackSize(-2);
m_metadata->addLEB128ConstantInt32AndLength(offset, getCurrentInstructionLength());
return { };
}
[[nodiscard]] PartialResult IPIntGenerator::addSIMDLoadExtend(SIMDLaneOperation, ExpressionType pointer, uint32_t offset, ExpressionType& result)
{
return addSIMDLoad(pointer, offset, result);
}
[[nodiscard]] PartialResult IPIntGenerator::addSIMDLoadPad(SIMDLaneOperation, ExpressionType pointer, uint32_t offset, ExpressionType& result)
{
return addSIMDLoad(pointer, offset, result);
}
IPIntGenerator::ExpressionType IPIntGenerator::addSIMDConstant(v128_t)
{
changeStackSize(1);
m_metadata->addLength(getCurrentInstructionLength());
return { };
}
[[nodiscard]] PartialResult IPIntGenerator::addSIMDExtractLane(SIMDInfo, uint8_t, ExpressionType, ExpressionType&)
{
m_metadata->addLength(getCurrentInstructionLength());
return { };
}
[[nodiscard]] PartialResult IPIntGenerator::addSIMDReplaceLane(SIMDInfo, uint8_t, ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
m_metadata->addLength(getCurrentInstructionLength());
return { };
}
[[nodiscard]] PartialResult IPIntGenerator::addSIMDI_V(SIMDLaneOperation, SIMDInfo, ExpressionType, ExpressionType&)
{
m_metadata->addLength(getCurrentInstructionLength());
return { };
}
[[nodiscard]] PartialResult IPIntGenerator::addSIMDV_V(SIMDLaneOperation, SIMDInfo, ExpressionType, ExpressionType&)
{
m_metadata->addLength(getCurrentInstructionLength());
return { };
}
[[nodiscard]] PartialResult IPIntGenerator::addSIMDBitwiseSelect(ExpressionType, ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-2); // 3 operands, 1 result
m_metadata->addLength(getCurrentInstructionLength());
return { };
}
#if ENABLE(B3_JIT)
[[nodiscard]] PartialResult IPIntGenerator::addSIMDRelOp(SIMDLaneOperation, SIMDInfo, ExpressionType, ExpressionType, B3::Air::Arg, ExpressionType&)
{
changeStackSize(-1);
m_metadata->addLength(getCurrentInstructionLength());
return { };
}
#endif
[[nodiscard]] PartialResult IPIntGenerator::addSIMDV_VV(SIMDLaneOperation, SIMDInfo, ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1); // Pop two v128 values, push one v128 value
m_metadata->addLength(getCurrentInstructionLength());
return { };
}
[[nodiscard]] PartialResult IPIntGenerator::addSIMDRelaxedFMA(SIMDLaneOperation, SIMDInfo, ExpressionType, ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-2); // Pop three v128 values, push one v128 value
return { };
}
// References
[[nodiscard]] PartialResult IPIntGenerator::addRefIsNull(ExpressionType, ExpressionType&)
{
return { };
}
[[nodiscard]] PartialResult IPIntGenerator::addRefFunc(FunctionSpaceIndex index, ExpressionType&)
{
changeStackSize(1);
m_metadata->addLEB128ConstantInt32AndLength(index, getCurrentInstructionLength());
return { };
}
[[nodiscard]] PartialResult IPIntGenerator::addRefAsNonNull(ExpressionType, ExpressionType&)
{
return { };
}
[[nodiscard]] PartialResult IPIntGenerator::addRefEq(ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
return { };
}
// Tables
[[nodiscard]] PartialResult IPIntGenerator::addTableGet(unsigned index, ExpressionType, ExpressionType&)
{
m_metadata->addLEB128ConstantInt32AndLength(index, getCurrentInstructionLength());
return { };
}
[[nodiscard]] PartialResult IPIntGenerator::addTableSet(unsigned index, ExpressionType, ExpressionType)
{
changeStackSize(-2);
m_metadata->addLEB128ConstantInt32AndLength(index, getCurrentInstructionLength());
return { };
}
[[nodiscard]] PartialResult IPIntGenerator::addTableInit(unsigned elementIndex, unsigned tableIndex, ExpressionType, ExpressionType, ExpressionType)
{
changeStackSize(-3);
IPInt::TableInitMetadata table {
.elementIndex = safeCast<uint32_t>(elementIndex),
.tableIndex = safeCast<uint32_t>(tableIndex),
.instructionLength = { .length = safeCast<uint8_t>(getCurrentInstructionLength()) }
};
m_metadata->appendMetadata(table);
return { };
}
[[nodiscard]] PartialResult IPIntGenerator::addElemDrop(unsigned elementIndex)
{
m_metadata->addLEB128ConstantInt32AndLength(elementIndex, getCurrentInstructionLength());
return { };
}
[[nodiscard]] PartialResult IPIntGenerator::addTableSize(unsigned tableIndex, ExpressionType&)
{
changeStackSize(1);
m_metadata->addLEB128ConstantInt32AndLength(tableIndex, getCurrentInstructionLength());
return { };
}
[[nodiscard]] PartialResult IPIntGenerator::addTableGrow(unsigned tableIndex, ExpressionType, ExpressionType, ExpressionType&)
{
changeStackSize(-1);
IPInt::TableGrowMetadata table {
.tableIndex = safeCast<uint32_t>(tableIndex),
.instructionLength = { .length = safeCast<uint8_t>(getCurrentInstructionLength()) }
};
m_metadata->appendMetadata(table);
return { };
}
[[nodiscard]] PartialResult IPIntGenerator::addTableFill(unsigned tableIndex, ExpressionType, ExpressionType, ExpressionType)
{
changeStackSize(-3);
IPInt::TableFillMetadata table {
.tableIndex = safeCast<uint32_t>(tableIndex),
.instructionLength = { .length = safeCast<uint8_t>(getCurrentInstructionLength()) }
};
m_metadata->appendMetadata(table);
return { };
}
[[nodiscard]] PartialResult IPIntGenerator::addTableCopy(unsigned dstTableIndex, unsigned srcTableIndex, ExpressionType, ExpressionType, ExpressionType)
{
changeStackSize(-3);
IPInt::TableCopyMetadata table {
.dstTableIndex = safeCast<uint32_t>(dstTableIndex),
.srcTableIndex = safeCast<uint32_t>(srcTableIndex),
.instructionLength = { .length = safeCast<uint8_t>(getCurrentInstructionLength()) }
};
m_metadata->appendMetadata(table);
return { };
}
// Locals and Globals
[[nodiscard]] PartialResult IPIntGenerator::addArguments(const TypeDefinition &signature)
{
auto sig = signature.as<FunctionSignature>();
const CallInformation callCC = wasmCallingConvention().callInformationFor(*sig, CallRole::Callee);
ASSERT(callCC.headerAndArgumentStackSizeInBytes >= callCC.headerIncludingThisSizeInBytes);
m_argumentAndResultsStackSize = WTF::roundUpToMultipleOf<stackAlignmentBytes()>(callCC.headerAndArgumentStackSizeInBytes) - callCC.headerIncludingThisSizeInBytes;
ASSERT(!Options::useWasmIPInt() || !(m_argumentAndResultsStackSize % 16)); // mINT requires this
auto numArgs = sig->argumentCount();
m_metadata->m_numLocals += numArgs;
m_metadata->m_numArguments = numArgs;
m_metadata->m_argumINTBytecode.reserveInitialCapacity(sig->argumentCount() + 1);
constexpr static int NUM_ARGUMINT_GPRS = 8;
constexpr static int NUM_ARGUMINT_FPRS = 8;
ASSERT_UNUSED(NUM_ARGUMINT_GPRS, wasmCallingConvention().jsrArgs.size() <= NUM_ARGUMINT_GPRS);
ASSERT_UNUSED(NUM_ARGUMINT_FPRS, wasmCallingConvention().fprArgs.size() <= NUM_ARGUMINT_FPRS);
// 0x00 - 0x07: GPR 0-7
// 0x08 - 0x0f: FPR 0-3
// 0x10: stack
// 0x11: end
for (size_t i = 0; i < numArgs; ++i) {
const ArgumentLocation& argLoc = callCC.params[i];
const ValueLocation& loc = argLoc.location;
if (loc.isGPR()) {
#if USE(JSVALUE64)
ASSERT_UNUSED(NUM_ARGUMINT_GPRS, GPRInfo::toArgumentIndex(loc.jsr().gpr()) < NUM_ARGUMINT_GPRS);
m_metadata->m_argumINTBytecode.append(static_cast<uint8_t>(IPInt::ArgumINTBytecode::ArgGPR) + GPRInfo::toArgumentIndex(loc.jsr().gpr()));
#elif USE(JSVALUE32_64)
ASSERT_UNUSED(NUM_ARGUMINT_GPRS, GPRInfo::toArgumentIndex(loc.jsr().payloadGPR()) < NUM_ARGUMINT_GPRS);
ASSERT_UNUSED(NUM_ARGUMINT_GPRS, GPRInfo::toArgumentIndex(loc.jsr().tagGPR()) < NUM_ARGUMINT_GPRS);
m_metadata->m_argumINTBytecode.append(static_cast<uint8_t>(IPInt::ArgumINTBytecode::ArgGPR) + GPRInfo::toArgumentIndex(loc.jsr().gpr(WhichValueWord::PayloadWord)) / 2);
#endif
} else if (loc.isFPR()) {
ASSERT_UNUSED(NUM_ARGUMINT_FPRS, FPRInfo::toArgumentIndex(loc.fpr()) < NUM_ARGUMINT_FPRS);
m_metadata->m_argumINTBytecode.append(static_cast<uint8_t>(IPInt::ArgumINTBytecode::ArgFPR) + FPRInfo::toArgumentIndex(loc.fpr()));
} else {
RELEASE_ASSERT(loc.isStack());
switch (argLoc.width) {
case Width::Width64:
m_metadata->m_argumINTBytecode.append(static_cast<uint8_t>(IPInt::ArgumINTBytecode::Stack));
break;
case Width::Width128:
m_metadata->m_argumINTBytecode.append(static_cast<uint8_t>(IPInt::ArgumINTBytecode::StackVector));
break;
default:
RELEASE_ASSERT_NOT_REACHED("No argumINT bytecode for result width");
}
}
}
m_metadata->m_argumINTBytecode.append(static_cast<uint8_t>(IPInt::ArgumINTBytecode::End));
#if ENABLE(WEBASSEMBLY_DEBUGGER)
if (Options::enableWasmDebugger()) [[unlikely]] {
if (m_debugInfo) {
auto* localTypes = &m_debugInfo->locals;
for (size_t i = 0; i < numArgs; ++i)