-
Notifications
You must be signed in to change notification settings - Fork 849
Expand file tree
/
Copy pathwasm-stack.cpp
More file actions
3722 lines (3533 loc) · 123 KB
/
wasm-stack.cpp
File metadata and controls
3722 lines (3533 loc) · 123 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 2019 WebAssembly Community Group participants
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "wasm-stack.h"
#include "ir/find_all.h"
#include "ir/properties.h"
#include "wasm-binary.h"
#include "wasm-debug.h"
namespace wasm {
static Name IMPOSSIBLE_CONTINUE("impossible-continue");
void BinaryInstWriter::emitResultType(Type type) {
if (type == Type::unreachable) {
parent.writeType(Type::none);
} else if (type.isTuple()) {
o << S32LEB(parent.getSignatureIndex(Signature(Type::none, type)));
} else {
parent.writeType(type);
}
}
void BinaryInstWriter::visitBlock(Block* curr) {
breakStack.push_back(curr->name);
o << static_cast<int8_t>(BinaryConsts::Block);
emitResultType(curr->type);
}
void BinaryInstWriter::visitIf(If* curr) {
// the binary format requires this; we have a block if we need one
// TODO: optimize this in Stack IR (if child is a block, we may break to this
// instead)
breakStack.emplace_back(IMPOSSIBLE_CONTINUE);
o << static_cast<int8_t>(BinaryConsts::If);
emitResultType(curr->type);
}
void BinaryInstWriter::emitIfElse(If* curr) {
if (func) {
parent.trackExpressionDelimiter(curr, func, BinaryLocations::Else);
}
o << static_cast<int8_t>(BinaryConsts::Else);
}
void BinaryInstWriter::emitStoreOpcode(uint8_t bytes, Type valueType) {
switch (valueType.getBasic()) {
case Type::i32: {
switch (bytes) {
case 1:
o << static_cast<int8_t>(BinaryConsts::I32StoreMem8);
break;
case 2:
o << static_cast<int8_t>(BinaryConsts::I32StoreMem16);
break;
case 4:
o << static_cast<int8_t>(BinaryConsts::I32StoreMem);
break;
default:
abort();
}
break;
}
case Type::i64: {
switch (bytes) {
case 1:
o << static_cast<int8_t>(BinaryConsts::I64StoreMem8);
break;
case 2:
o << static_cast<int8_t>(BinaryConsts::I64StoreMem16);
break;
case 4:
o << static_cast<int8_t>(BinaryConsts::I64StoreMem32);
break;
case 8:
o << static_cast<int8_t>(BinaryConsts::I64StoreMem);
break;
default:
abort();
}
break;
}
case Type::f32: {
switch (bytes) {
case 2:
o << static_cast<int8_t>(BinaryConsts::MiscPrefix)
<< U32LEB(BinaryConsts::F32_F16StoreMem);
break;
case 4:
o << static_cast<int8_t>(BinaryConsts::F32StoreMem);
break;
default:
WASM_UNREACHABLE("invalid store size");
}
break;
}
case Type::f64:
o << static_cast<int8_t>(BinaryConsts::F64StoreMem);
break;
case Type::v128:
o << static_cast<int8_t>(BinaryConsts::SIMDPrefix)
<< U32LEB(BinaryConsts::V128Store);
break;
case Type::none:
case Type::unreachable:
WASM_UNREACHABLE("unexpected type");
}
}
void BinaryInstWriter::emitLoadOpcode(unsigned bytes, bool signed_, Type type) {
switch (type.getBasic()) {
case Type::i32: {
switch (bytes) {
case 1:
o << static_cast<int8_t>(signed_ ? BinaryConsts::I32LoadMem8S
: BinaryConsts::I32LoadMem8U);
break;
case 2:
o << static_cast<int8_t>(signed_ ? BinaryConsts::I32LoadMem16S
: BinaryConsts::I32LoadMem16U);
break;
case 4:
o << static_cast<int8_t>(BinaryConsts::I32LoadMem);
break;
default:
abort();
}
break;
}
case Type::i64: {
switch (bytes) {
case 1:
o << static_cast<int8_t>(signed_ ? BinaryConsts::I64LoadMem8S
: BinaryConsts::I64LoadMem8U);
break;
case 2:
o << static_cast<int8_t>(signed_ ? BinaryConsts::I64LoadMem16S
: BinaryConsts::I64LoadMem16U);
break;
case 4:
o << static_cast<int8_t>(signed_ ? BinaryConsts::I64LoadMem32S
: BinaryConsts::I64LoadMem32U);
break;
case 8:
o << static_cast<int8_t>(BinaryConsts::I64LoadMem);
break;
default:
abort();
}
break;
}
case Type::f32: {
switch (bytes) {
case 2:
o << static_cast<int8_t>(BinaryConsts::MiscPrefix)
<< U32LEB(BinaryConsts::F32_F16LoadMem);
break;
case 4:
o << static_cast<int8_t>(BinaryConsts::F32LoadMem);
break;
default:
WASM_UNREACHABLE("invalid load size");
}
break;
}
case Type::f64:
o << static_cast<int8_t>(BinaryConsts::F64LoadMem);
break;
case Type::v128:
o << static_cast<int8_t>(BinaryConsts::SIMDPrefix)
<< U32LEB(BinaryConsts::V128Load);
break;
default:
WASM_UNREACHABLE("unexpected type");
}
}
void BinaryInstWriter::visitLoop(Loop* curr) {
breakStack.push_back(curr->name);
o << static_cast<int8_t>(BinaryConsts::Loop);
emitResultType(curr->type);
}
void BinaryInstWriter::visitBreak(Break* curr) {
auto type = curr->type;
// See comment on |brIfsNeedingHandling| for the extra handling we need to
// emit here for certain br_ifs. If we need that handling, we either use a
// cast in simple cases, or scratch locals otherwise. We use the scratch
// locals to stash the stack before the br_if (which contains the refined
// types), then restore it later from those locals.
bool needScratchLocals = false;
// If we need locals, we must track how many we've used from each type as we
// go, as a type might appear multiple times in the tuple. We know we have
// enough of a range allocated for them, so we just increment as we go.
std::unordered_map<Type, Index> scratchTypeUses;
// Logic to stash and restore the stack, given a vector of types we are
// stashing/restoring. We will first stash the entire stack, including the i32
// condition, and after the br_if, restore the value (without the condition).
auto stashStack = [&](const std::vector<Type>& types) {
for (Index i = 0; i < types.size(); i++) {
auto t = types[types.size() - i - 1];
assert(scratchLocals.find(t) != scratchLocals.end());
auto localIndex = scratchLocals[t] + scratchTypeUses[t]++;
o << static_cast<int8_t>(BinaryConsts::LocalSet) << U32LEB(localIndex);
}
};
auto restoreStack = [&](const std::vector<Type>& types) {
// Use a copy of this data, as we will restore twice.
auto currScratchTypeUses = scratchTypeUses;
for (Index i = 0; i < types.size(); i++) {
auto t = types[i];
auto localIndex = scratchLocals[t] + --currScratchTypeUses[t];
o << static_cast<int8_t>(BinaryConsts::LocalGet) << U32LEB(localIndex);
}
};
// The types on the stack before the br_if. We need this if we use locals to
// stash the stack.
std::vector<Type> typesOnStack;
auto needHandling = brIfsNeedingHandling.contains(curr);
if (needHandling) {
// Tuples always need scratch locals. Uncastable types do as well, we we
// can't fix them up below with a simple cast.
needScratchLocals = type.isTuple() || !type.isCastable();
if (needScratchLocals) {
// Stash all the values on the stack to those locals, then reload them for
// the br_if to consume. Later, we can reload the refined values after the
// br_if, for its parent to consume.
typesOnStack = std::vector<Type>(type.begin(), type.end());
typesOnStack.push_back(Type::i32);
stashStack(typesOnStack);
restoreStack(typesOnStack);
// The stack is now in the same state as before, but we have copies in
// locals for later.
}
}
o << static_cast<int8_t>(curr->condition ? BinaryConsts::BrIf
: BinaryConsts::Br)
<< U32LEB(getBreakIndex(curr->name));
if (needHandling) {
if (!needScratchLocals) {
// We can just cast here, avoiding scratch locals. (Casting adds overhead,
// but this is very rare, and it avoids adding locals, which would keep
// growing the wasm with each roundtrip.)
// Shim a tiny bit of IR, just enough to get visitRefCast to see what we
// are casting, and to emit the proper thing.
RefCast cast;
cast.type = type;
cast.ref = cast.desc = nullptr;
visitRefCast(&cast);
} else {
// We need locals. Earlier we stashed the stack, so we just need to
// restore the value from there (note we don't restore the condition),
// after dropping the br_if's unrefined values.
for (Index i = 0; i < type.size(); ++i) {
o << static_cast<int8_t>(BinaryConsts::Drop);
}
assert(typesOnStack.back() == Type::i32);
typesOnStack.pop_back();
restoreStack(typesOnStack);
}
}
}
void BinaryInstWriter::visitSwitch(Switch* curr) {
o << static_cast<int8_t>(BinaryConsts::BrTable)
<< U32LEB(curr->targets.size());
for (auto target : curr->targets) {
o << U32LEB(getBreakIndex(target));
}
o << U32LEB(getBreakIndex(curr->default_));
}
void BinaryInstWriter::visitCall(Call* curr) {
int8_t op =
curr->isReturn ? BinaryConsts::RetCallFunction : BinaryConsts::CallFunction;
o << op << U32LEB(parent.getFunctionIndex(curr->target));
}
void BinaryInstWriter::visitCallIndirect(CallIndirect* curr) {
Index tableIdx = parent.getTableIndex(curr->table);
int8_t op =
curr->isReturn ? BinaryConsts::RetCallIndirect : BinaryConsts::CallIndirect;
o << op << U32LEB(parent.getTypeIndex(curr->heapType)) << U32LEB(tableIdx);
}
void BinaryInstWriter::visitLocalGet(LocalGet* curr) {
if (deferredGets.contains(curr)) {
// This local.get will be emitted as part of the instruction that consumes
// it.
return;
}
if (auto it = extractedGets.find(curr); it != extractedGets.end()) {
// We have a tuple of locals to get, but we will only end up using one of
// them, so we can just emit that one.
o << static_cast<int8_t>(BinaryConsts::LocalGet)
<< U32LEB(mappedLocals[std::make_pair(curr->index, it->second)]);
return;
}
size_t numValues = func->getLocalType(curr->index).size();
for (Index i = 0; i < numValues; ++i) {
o << static_cast<int8_t>(BinaryConsts::LocalGet)
<< U32LEB(mappedLocals[std::make_pair(curr->index, i)]);
}
}
void BinaryInstWriter::visitLocalSet(LocalSet* curr) {
size_t numValues = func->getLocalType(curr->index).size();
// If this is a tuple, set all the elements with nonzero index.
for (Index i = numValues - 1; i >= 1; --i) {
o << static_cast<int8_t>(BinaryConsts::LocalSet)
<< U32LEB(mappedLocals[std::make_pair(curr->index, i)]);
}
if (!curr->isTee()) {
// This is not a tee, so just finish setting the values.
o << static_cast<int8_t>(BinaryConsts::LocalSet)
<< U32LEB(mappedLocals[std::make_pair(curr->index, 0)]);
} else if (auto it = extractedGets.find(curr); it != extractedGets.end()) {
// We only need to get the single extracted value.
if (it->second == 0) {
o << static_cast<int8_t>(BinaryConsts::LocalTee)
<< U32LEB(mappedLocals[std::make_pair(curr->index, 0)]);
} else {
o << static_cast<int8_t>(BinaryConsts::LocalSet)
<< U32LEB(mappedLocals[std::make_pair(curr->index, 0)]);
o << static_cast<int8_t>(BinaryConsts::LocalGet)
<< U32LEB(mappedLocals[std::make_pair(curr->index, it->second)]);
}
} else {
// We need to get all the values.
o << static_cast<int8_t>(BinaryConsts::LocalTee)
<< U32LEB(mappedLocals[std::make_pair(curr->index, 0)]);
for (Index i = 1; i < numValues; ++i) {
o << static_cast<int8_t>(BinaryConsts::LocalGet)
<< U32LEB(mappedLocals[std::make_pair(curr->index, i)]);
}
}
}
void BinaryInstWriter::visitGlobalGet(GlobalGet* curr) {
Index index = parent.getGlobalIndex(curr->name);
if (auto it = extractedGets.find(curr); it != extractedGets.end()) {
// We have a tuple of globals to get, but we will only end up using one of
// them, so we can just emit that one.
o << static_cast<int8_t>(BinaryConsts::GlobalGet)
<< U32LEB(index + it->second);
return;
}
// Emit a global.get for each element if this is a tuple global
size_t numValues = curr->type.size();
for (Index i = 0; i < numValues; ++i) {
o << static_cast<int8_t>(BinaryConsts::GlobalGet) << U32LEB(index + i);
}
}
void BinaryInstWriter::visitGlobalSet(GlobalSet* curr) {
// Emit a global.set for each element if this is a tuple global
Index index = parent.getGlobalIndex(curr->name);
size_t numValues = parent.getModule()->getGlobal(curr->name)->type.size();
for (int i = numValues - 1; i >= 0; --i) {
o << static_cast<int8_t>(BinaryConsts::GlobalSet) << U32LEB(index + i);
}
}
void BinaryInstWriter::visitLoad(Load* curr) {
if (curr->type == Type::unreachable) {
// the pointer is unreachable, so we are never reached; just don't emit
// a load
return;
}
if (!curr->isAtomic()) {
emitLoadOpcode(curr->bytes, curr->signed_, curr->type);
} else {
o << static_cast<int8_t>(BinaryConsts::AtomicPrefix);
switch (curr->type.getBasic()) {
case Type::i32: {
switch (curr->bytes) {
case 1:
o << static_cast<int8_t>(BinaryConsts::I32AtomicLoad8U);
break;
case 2:
o << static_cast<int8_t>(BinaryConsts::I32AtomicLoad16U);
break;
case 4:
o << static_cast<int8_t>(BinaryConsts::I32AtomicLoad);
break;
default:
WASM_UNREACHABLE("invalid load size");
}
break;
}
case Type::i64: {
switch (curr->bytes) {
case 1:
o << static_cast<int8_t>(BinaryConsts::I64AtomicLoad8U);
break;
case 2:
o << static_cast<int8_t>(BinaryConsts::I64AtomicLoad16U);
break;
case 4:
o << static_cast<int8_t>(BinaryConsts::I64AtomicLoad32U);
break;
case 8:
o << static_cast<int8_t>(BinaryConsts::I64AtomicLoad);
break;
default:
WASM_UNREACHABLE("invalid load size");
}
break;
}
default:
WASM_UNREACHABLE("unexpected type");
}
}
emitMemoryAccess(curr->align,
curr->bytes,
curr->offset,
curr->memory,
curr->order,
/*isRMW=*/false);
}
void BinaryInstWriter::visitStore(Store* curr) {
if (!curr->isAtomic()) {
emitStoreOpcode(curr->bytes, curr->valueType);
} else {
o << static_cast<int8_t>(BinaryConsts::AtomicPrefix);
switch (curr->valueType.getBasic()) {
case Type::i32: {
switch (curr->bytes) {
case 1:
o << static_cast<int8_t>(BinaryConsts::I32AtomicStore8);
break;
case 2:
o << static_cast<int8_t>(BinaryConsts::I32AtomicStore16);
break;
case 4:
o << static_cast<int8_t>(BinaryConsts::I32AtomicStore);
break;
default:
WASM_UNREACHABLE("invalid store size");
}
break;
}
case Type::i64: {
switch (curr->bytes) {
case 1:
o << static_cast<int8_t>(BinaryConsts::I64AtomicStore8);
break;
case 2:
o << static_cast<int8_t>(BinaryConsts::I64AtomicStore16);
break;
case 4:
o << static_cast<int8_t>(BinaryConsts::I64AtomicStore32);
break;
case 8:
o << static_cast<int8_t>(BinaryConsts::I64AtomicStore);
break;
default:
WASM_UNREACHABLE("invalid store size");
}
break;
}
default:
WASM_UNREACHABLE("unexpected type");
}
}
emitMemoryAccess(curr->align,
curr->bytes,
curr->offset,
curr->memory,
curr->order,
/*isRMW=*/false);
}
void BinaryInstWriter::visitAtomicRMW(AtomicRMW* curr) {
o << static_cast<int8_t>(BinaryConsts::AtomicPrefix);
#define CASE_FOR_OP(Op) \
case RMW##Op: \
switch (curr->type.getBasic()) { \
case Type::i32: \
switch (curr->bytes) { \
case 1: \
o << static_cast<int8_t>(BinaryConsts::I32AtomicRMW##Op##8U); \
break; \
case 2: \
o << static_cast<int8_t>(BinaryConsts::I32AtomicRMW##Op##16U); \
break; \
case 4: \
o << static_cast<int8_t>(BinaryConsts::I32AtomicRMW##Op); \
break; \
default: \
WASM_UNREACHABLE("invalid rmw size"); \
} \
break; \
case Type::i64: \
switch (curr->bytes) { \
case 1: \
o << static_cast<int8_t>(BinaryConsts::I64AtomicRMW##Op##8U); \
break; \
case 2: \
o << static_cast<int8_t>(BinaryConsts::I64AtomicRMW##Op##16U); \
break; \
case 4: \
o << static_cast<int8_t>(BinaryConsts::I64AtomicRMW##Op##32U); \
break; \
case 8: \
o << static_cast<int8_t>(BinaryConsts::I64AtomicRMW##Op); \
break; \
default: \
WASM_UNREACHABLE("invalid rmw size"); \
} \
break; \
default: \
WASM_UNREACHABLE("unexpected type"); \
} \
break
switch (curr->op) {
CASE_FOR_OP(Add);
CASE_FOR_OP(Sub);
CASE_FOR_OP(And);
CASE_FOR_OP(Or);
CASE_FOR_OP(Xor);
CASE_FOR_OP(Xchg);
default:
WASM_UNREACHABLE("unexpected op");
}
emitMemoryAccess(curr->bytes,
curr->bytes,
curr->offset,
curr->memory,
curr->order,
/*isRMW=*/true);
}
void BinaryInstWriter::visitAtomicCmpxchg(AtomicCmpxchg* curr) {
o << static_cast<int8_t>(BinaryConsts::AtomicPrefix);
switch (curr->type.getBasic()) {
case Type::i32:
switch (curr->bytes) {
case 1:
o << static_cast<int8_t>(BinaryConsts::I32AtomicCmpxchg8U);
break;
case 2:
o << static_cast<int8_t>(BinaryConsts::I32AtomicCmpxchg16U);
break;
case 4:
o << static_cast<int8_t>(BinaryConsts::I32AtomicCmpxchg);
break;
default:
WASM_UNREACHABLE("invalid size");
}
break;
case Type::i64:
switch (curr->bytes) {
case 1:
o << static_cast<int8_t>(BinaryConsts::I64AtomicCmpxchg8U);
break;
case 2:
o << static_cast<int8_t>(BinaryConsts::I64AtomicCmpxchg16U);
break;
case 4:
o << static_cast<int8_t>(BinaryConsts::I64AtomicCmpxchg32U);
break;
case 8:
o << static_cast<int8_t>(BinaryConsts::I64AtomicCmpxchg);
break;
default:
WASM_UNREACHABLE("invalid size");
}
break;
default:
WASM_UNREACHABLE("unexpected type");
}
emitMemoryAccess(curr->bytes,
curr->bytes,
curr->offset,
curr->memory,
curr->order,
/*isRMW=*/true);
}
void BinaryInstWriter::visitAtomicWait(AtomicWait* curr) {
o << static_cast<int8_t>(BinaryConsts::AtomicPrefix);
switch (curr->expectedType.getBasic()) {
case Type::i32: {
o << static_cast<int8_t>(BinaryConsts::I32AtomicWait);
emitMemoryAccess(
4, 4, curr->offset, curr->memory, MemoryOrder::SeqCst, /*isRMW=*/false);
break;
}
case Type::i64: {
o << static_cast<int8_t>(BinaryConsts::I64AtomicWait);
emitMemoryAccess(
8, 8, curr->offset, curr->memory, MemoryOrder::SeqCst, /*isRMW=*/false);
break;
}
default:
WASM_UNREACHABLE("unexpected type");
}
}
void BinaryInstWriter::visitAtomicNotify(AtomicNotify* curr) {
o << static_cast<int8_t>(BinaryConsts::AtomicPrefix)
<< static_cast<int8_t>(BinaryConsts::AtomicNotify);
emitMemoryAccess(
4, 4, curr->offset, curr->memory, MemoryOrder::SeqCst, /*isRMW=*/false);
}
void BinaryInstWriter::visitAtomicFence(AtomicFence* curr) {
o << static_cast<int8_t>(BinaryConsts::AtomicPrefix)
<< static_cast<int8_t>(BinaryConsts::AtomicFence)
<< static_cast<int8_t>(curr->order);
}
void BinaryInstWriter::visitPause(Pause* curr) {
o << static_cast<int8_t>(BinaryConsts::AtomicPrefix)
<< U32LEB(BinaryConsts::Pause);
}
void BinaryInstWriter::visitSIMDExtract(SIMDExtract* curr) {
o << static_cast<int8_t>(BinaryConsts::SIMDPrefix);
switch (curr->op) {
case ExtractLaneSVecI8x16:
o << U32LEB(BinaryConsts::I8x16ExtractLaneS);
break;
case ExtractLaneUVecI8x16:
o << U32LEB(BinaryConsts::I8x16ExtractLaneU);
break;
case ExtractLaneSVecI16x8:
o << U32LEB(BinaryConsts::I16x8ExtractLaneS);
break;
case ExtractLaneUVecI16x8:
o << U32LEB(BinaryConsts::I16x8ExtractLaneU);
break;
case ExtractLaneVecI32x4:
o << U32LEB(BinaryConsts::I32x4ExtractLane);
break;
case ExtractLaneVecI64x2:
o << U32LEB(BinaryConsts::I64x2ExtractLane);
break;
case ExtractLaneVecF16x8:
o << U32LEB(BinaryConsts::F16x8ExtractLane);
break;
case ExtractLaneVecF32x4:
o << U32LEB(BinaryConsts::F32x4ExtractLane);
break;
case ExtractLaneVecF64x2:
o << U32LEB(BinaryConsts::F64x2ExtractLane);
break;
}
o << static_cast<uint8_t>(curr->index);
}
void BinaryInstWriter::visitSIMDReplace(SIMDReplace* curr) {
o << static_cast<int8_t>(BinaryConsts::SIMDPrefix);
switch (curr->op) {
case ReplaceLaneVecI8x16:
o << U32LEB(BinaryConsts::I8x16ReplaceLane);
break;
case ReplaceLaneVecI16x8:
o << U32LEB(BinaryConsts::I16x8ReplaceLane);
break;
case ReplaceLaneVecI32x4:
o << U32LEB(BinaryConsts::I32x4ReplaceLane);
break;
case ReplaceLaneVecI64x2:
o << U32LEB(BinaryConsts::I64x2ReplaceLane);
break;
case ReplaceLaneVecF16x8:
o << U32LEB(BinaryConsts::F16x8ReplaceLane);
break;
case ReplaceLaneVecF32x4:
o << U32LEB(BinaryConsts::F32x4ReplaceLane);
break;
case ReplaceLaneVecF64x2:
o << U32LEB(BinaryConsts::F64x2ReplaceLane);
break;
}
assert(curr->index < 16);
o << static_cast<uint8_t>(curr->index);
}
void BinaryInstWriter::visitSIMDShuffle(SIMDShuffle* curr) {
o << static_cast<int8_t>(BinaryConsts::SIMDPrefix)
<< U32LEB(BinaryConsts::I8x16Shuffle);
for (uint8_t m : curr->mask) {
o << m;
}
}
void BinaryInstWriter::visitSIMDTernary(SIMDTernary* curr) {
o << static_cast<int8_t>(BinaryConsts::SIMDPrefix);
switch (curr->op) {
case Bitselect:
o << U32LEB(BinaryConsts::V128Bitselect);
break;
case LaneselectI8x16:
o << U32LEB(BinaryConsts::I8x16Laneselect);
break;
case LaneselectI16x8:
o << U32LEB(BinaryConsts::I16x8Laneselect);
break;
case LaneselectI32x4:
o << U32LEB(BinaryConsts::I32x4Laneselect);
break;
case LaneselectI64x2:
o << U32LEB(BinaryConsts::I64x2Laneselect);
break;
case MaddVecF16x8:
o << U32LEB(BinaryConsts::F16x8Madd);
break;
case NmaddVecF16x8:
o << U32LEB(BinaryConsts::F16x8Nmadd);
break;
case RelaxedMaddVecF32x4:
o << U32LEB(BinaryConsts::F32x4RelaxedMadd);
break;
case RelaxedNmaddVecF32x4:
o << U32LEB(BinaryConsts::F32x4RelaxedNmadd);
break;
case RelaxedMaddVecF64x2:
o << U32LEB(BinaryConsts::F64x2RelaxedMadd);
break;
case RelaxedNmaddVecF64x2:
o << U32LEB(BinaryConsts::F64x2RelaxedNmadd);
break;
case DotI8x16I7x16AddSToVecI32x4:
o << U32LEB(BinaryConsts::I32x4DotI8x16I7x16AddS);
break;
}
}
void BinaryInstWriter::visitSIMDShift(SIMDShift* curr) {
o << static_cast<int8_t>(BinaryConsts::SIMDPrefix);
switch (curr->op) {
case ShlVecI8x16:
o << U32LEB(BinaryConsts::I8x16Shl);
break;
case ShrSVecI8x16:
o << U32LEB(BinaryConsts::I8x16ShrS);
break;
case ShrUVecI8x16:
o << U32LEB(BinaryConsts::I8x16ShrU);
break;
case ShlVecI16x8:
o << U32LEB(BinaryConsts::I16x8Shl);
break;
case ShrSVecI16x8:
o << U32LEB(BinaryConsts::I16x8ShrS);
break;
case ShrUVecI16x8:
o << U32LEB(BinaryConsts::I16x8ShrU);
break;
case ShlVecI32x4:
o << U32LEB(BinaryConsts::I32x4Shl);
break;
case ShrSVecI32x4:
o << U32LEB(BinaryConsts::I32x4ShrS);
break;
case ShrUVecI32x4:
o << U32LEB(BinaryConsts::I32x4ShrU);
break;
case ShlVecI64x2:
o << U32LEB(BinaryConsts::I64x2Shl);
break;
case ShrSVecI64x2:
o << U32LEB(BinaryConsts::I64x2ShrS);
break;
case ShrUVecI64x2:
o << U32LEB(BinaryConsts::I64x2ShrU);
break;
}
}
void BinaryInstWriter::visitSIMDLoad(SIMDLoad* curr) {
o << static_cast<int8_t>(BinaryConsts::SIMDPrefix);
switch (curr->op) {
case Load8SplatVec128:
o << U32LEB(BinaryConsts::V128Load8Splat);
break;
case Load16SplatVec128:
o << U32LEB(BinaryConsts::V128Load16Splat);
break;
case Load32SplatVec128:
o << U32LEB(BinaryConsts::V128Load32Splat);
break;
case Load64SplatVec128:
o << U32LEB(BinaryConsts::V128Load64Splat);
break;
case Load8x8SVec128:
o << U32LEB(BinaryConsts::V128Load8x8S);
break;
case Load8x8UVec128:
o << U32LEB(BinaryConsts::V128Load8x8U);
break;
case Load16x4SVec128:
o << U32LEB(BinaryConsts::V128Load16x4S);
break;
case Load16x4UVec128:
o << U32LEB(BinaryConsts::V128Load16x4U);
break;
case Load32x2SVec128:
o << U32LEB(BinaryConsts::V128Load32x2S);
break;
case Load32x2UVec128:
o << U32LEB(BinaryConsts::V128Load32x2U);
break;
case Load32ZeroVec128:
o << U32LEB(BinaryConsts::V128Load32Zero);
break;
case Load64ZeroVec128:
o << U32LEB(BinaryConsts::V128Load64Zero);
break;
}
assert(curr->align);
emitMemoryAccess(curr->align,
/*(unused) bytes=*/0,
curr->offset,
curr->memory,
MemoryOrder::Unordered,
/*isRMW=*/false);
}
void BinaryInstWriter::visitSIMDLoadStoreLane(SIMDLoadStoreLane* curr) {
o << static_cast<int8_t>(BinaryConsts::SIMDPrefix);
switch (curr->op) {
case Load8LaneVec128:
o << U32LEB(BinaryConsts::V128Load8Lane);
break;
case Load16LaneVec128:
o << U32LEB(BinaryConsts::V128Load16Lane);
break;
case Load32LaneVec128:
o << U32LEB(BinaryConsts::V128Load32Lane);
break;
case Load64LaneVec128:
o << U32LEB(BinaryConsts::V128Load64Lane);
break;
case Store8LaneVec128:
o << U32LEB(BinaryConsts::V128Store8Lane);
break;
case Store16LaneVec128:
o << U32LEB(BinaryConsts::V128Store16Lane);
break;
case Store32LaneVec128:
o << U32LEB(BinaryConsts::V128Store32Lane);
break;
case Store64LaneVec128:
o << U32LEB(BinaryConsts::V128Store64Lane);
break;
}
assert(curr->align);
emitMemoryAccess(curr->align,
/*(unused) bytes=*/0,
curr->offset,
curr->memory,
MemoryOrder::Unordered,
/*isRMW=*/false);
o << curr->index;
}
void BinaryInstWriter::visitMemoryInit(MemoryInit* curr) {
o << static_cast<int8_t>(BinaryConsts::MiscPrefix);
o << U32LEB(BinaryConsts::MemoryInit);
o << U32LEB(parent.getDataSegmentIndex(curr->segment));
o << U32LEB(parent.getMemoryIndex(curr->memory));
}
void BinaryInstWriter::visitDataDrop(DataDrop* curr) {
o << static_cast<int8_t>(BinaryConsts::MiscPrefix);
o << U32LEB(BinaryConsts::DataDrop);
o << U32LEB(parent.getDataSegmentIndex(curr->segment));
}
void BinaryInstWriter::visitMemoryCopy(MemoryCopy* curr) {
o << static_cast<int8_t>(BinaryConsts::MiscPrefix);
o << U32LEB(BinaryConsts::MemoryCopy);
o << U32LEB(parent.getMemoryIndex(curr->destMemory));
o << U32LEB(parent.getMemoryIndex(curr->sourceMemory));
}
void BinaryInstWriter::visitMemoryFill(MemoryFill* curr) {
o << static_cast<int8_t>(BinaryConsts::MiscPrefix);
o << U32LEB(BinaryConsts::MemoryFill);
o << U32LEB(parent.getMemoryIndex(curr->memory));
}
void BinaryInstWriter::visitConst(Const* curr) {
switch (curr->type.getBasic()) {
case Type::i32: {
o << static_cast<int8_t>(BinaryConsts::I32Const)
<< S32LEB(curr->value.geti32());
break;
}
case Type::i64: {
o << static_cast<int8_t>(BinaryConsts::I64Const)
<< S64LEB(curr->value.geti64());
break;
}
case Type::f32: {
o << static_cast<int8_t>(BinaryConsts::F32Const)
<< curr->value.reinterpreti32();
break;
}
case Type::f64: {
o << static_cast<int8_t>(BinaryConsts::F64Const)
<< curr->value.reinterpreti64();
break;
}
case Type::v128: {
o << static_cast<int8_t>(BinaryConsts::SIMDPrefix)
<< U32LEB(BinaryConsts::V128Const);
std::array<uint8_t, 16> v = curr->value.getv128();
for (size_t i = 0; i < 16; ++i) {
o << static_cast<uint8_t>(v[i]);
}
break;
}
case Type::none:
case Type::unreachable:
WASM_UNREACHABLE("unexpected type");
}
}
void BinaryInstWriter::visitUnary(Unary* curr) {
switch (curr->op) {
case ClzInt32:
o << static_cast<int8_t>(BinaryConsts::I32Clz);
break;
case CtzInt32:
o << static_cast<int8_t>(BinaryConsts::I32Ctz);
break;
case PopcntInt32:
o << static_cast<int8_t>(BinaryConsts::I32Popcnt);
break;
case EqZInt32:
o << static_cast<int8_t>(BinaryConsts::I32EqZ);
break;
case ClzInt64:
o << static_cast<int8_t>(BinaryConsts::I64Clz);
break;
case CtzInt64:
o << static_cast<int8_t>(BinaryConsts::I64Ctz);
break;
case PopcntInt64:
o << static_cast<int8_t>(BinaryConsts::I64Popcnt);
break;
case EqZInt64:
o << static_cast<int8_t>(BinaryConsts::I64EqZ);
break;
case NegFloat32:
o << static_cast<int8_t>(BinaryConsts::F32Neg);
break;
case AbsFloat32:
o << static_cast<int8_t>(BinaryConsts::F32Abs);
break;
case CeilFloat32:
o << static_cast<int8_t>(BinaryConsts::F32Ceil);
break;
case FloorFloat32:
o << static_cast<int8_t>(BinaryConsts::F32Floor);
break;
case TruncFloat32:
o << static_cast<int8_t>(BinaryConsts::F32Trunc);
break;
case NearestFloat32:
o << static_cast<int8_t>(BinaryConsts::F32Nearest);
break;
case SqrtFloat32:
o << static_cast<int8_t>(BinaryConsts::F32Sqrt);
break;
case NegFloat64:
o << static_cast<int8_t>(BinaryConsts::F64Neg);
break;
case AbsFloat64:
o << static_cast<int8_t>(BinaryConsts::F64Abs);
break;