-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathmasm.rs
More file actions
2330 lines (2046 loc) · 74.1 KB
/
masm.rs
File metadata and controls
2330 lines (2046 loc) · 74.1 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
use crate::Result;
use crate::abi::{self, LocalSlot, align_to};
use crate::codegen::{CodeGenContext, Emission, FuncEnv};
use crate::isa::{
CallingConvention,
reg::{Reg, RegClass, WritableReg, writable},
};
use cranelift_codegen::{
Final, MachBufferFinalized, MachLabel,
binemit::CodeOffset,
ir::{Endianness, MemFlags, RelSourceLoc, SourceLoc, UserExternalNameRef},
};
use std::{fmt::Debug, ops::Range};
use wasmtime_environ::{PtrSize, WasmHeapType, WasmRefType, WasmValType};
pub(crate) use cranelift_codegen::ir::TrapCode;
#[derive(Eq, PartialEq)]
pub(crate) enum DivKind {
/// Signed division.
Signed,
/// Unsigned division.
Unsigned,
}
/// Represents the `memory.atomic.wait*` kind.
#[derive(Debug, Clone, Copy)]
pub(crate) enum AtomicWaitKind {
Wait32,
Wait64,
}
/// Remainder kind.
#[derive(Copy, Clone)]
pub(crate) enum RemKind {
/// Signed remainder.
Signed,
/// Unsigned remainder.
Unsigned,
}
impl RemKind {
pub fn is_signed(&self) -> bool {
matches!(self, Self::Signed)
}
}
/// Kinds of vector min operation supported by WebAssembly.
pub(crate) enum V128MinKind {
/// 4 lanes of 32-bit floats.
F32x4,
/// 2 lanes of 64-bit floats.
F64x2,
/// 16 lanes of signed 8-bit integers.
I8x16S,
/// 16 lanes of unsigned 8-bit integers.
I8x16U,
/// 8 lanes of signed 16-bit integers.
I16x8S,
/// 8 lanes of unsigned 16-bit integers.
I16x8U,
/// 4 lanes of signed 32-bit integers.
I32x4S,
/// 4 lanes of unsigned 32-bit integers.
I32x4U,
}
impl V128MinKind {
/// The size of each lane.
pub(crate) fn lane_size(&self) -> OperandSize {
match self {
Self::F32x4 | Self::I32x4S | Self::I32x4U => OperandSize::S32,
Self::F64x2 => OperandSize::S64,
Self::I8x16S | Self::I8x16U => OperandSize::S8,
Self::I16x8S | Self::I16x8U => OperandSize::S16,
}
}
}
/// Kinds of vector max operation supported by WebAssembly.
pub(crate) enum V128MaxKind {
/// 4 lanes of 32-bit floats.
F32x4,
/// 2 lanes of 64-bit floats.
F64x2,
/// 16 lanes of signed 8-bit integers.
I8x16S,
/// 16 lanes of unsigned 8-bit integers.
I8x16U,
/// 8 lanes of signed 16-bit integers.
I16x8S,
/// 8 lanes of unsigned 16-bit integers.
I16x8U,
/// 4 lanes of signed 32-bit integers.
I32x4S,
/// 4 lanes of unsigned 32-bit integers.
I32x4U,
}
impl V128MaxKind {
/// The size of each lane.
pub(crate) fn lane_size(&self) -> OperandSize {
match self {
Self::F32x4 | Self::I32x4S | Self::I32x4U => OperandSize::S32,
Self::F64x2 => OperandSize::S64,
Self::I8x16S | Self::I8x16U => OperandSize::S8,
Self::I16x8S | Self::I16x8U => OperandSize::S16,
}
}
}
#[derive(Eq, PartialEq)]
pub(crate) enum MulWideKind {
Signed,
Unsigned,
}
/// Type of operation for a read-modify-write instruction.
pub(crate) enum RmwOp {
Add,
Sub,
Xchg,
And,
Or,
Xor,
}
/// The direction to perform the memory move.
#[derive(Debug, Clone, Eq, PartialEq)]
pub(crate) enum MemMoveDirection {
/// From high memory addresses to low memory addresses.
/// Invariant: the source location is closer to the FP than the destination
/// location, which will be closer to the SP.
HighToLow,
/// From low memory addresses to high memory addresses.
/// Invariant: the source location is closer to the SP than the destination
/// location, which will be closer to the FP.
LowToHigh,
}
/// Classifies how to treat float-to-int conversions.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub(crate) enum TruncKind {
/// Saturating conversion. If the source value is greater than the maximum
/// value of the destination type, the result is clamped to the
/// destination maximum value.
Checked,
/// An exception is raised if the source value is greater than the maximum
/// value of the destination type.
Unchecked,
}
impl TruncKind {
/// Returns true if the truncation kind is checked.
pub(crate) fn is_checked(&self) -> bool {
*self == TruncKind::Checked
}
/// Returns `true` if the trunc kind is [`Unchecked`].
///
/// [`Unchecked`]: TruncKind::Unchecked
#[must_use]
pub(crate) fn is_unchecked(&self) -> bool {
matches!(self, Self::Unchecked)
}
}
/// Representation of the stack pointer offset.
#[derive(Copy, Clone, Eq, PartialEq, Debug, PartialOrd, Ord, Default)]
pub struct SPOffset(u32);
impl SPOffset {
pub fn from_u32(offs: u32) -> Self {
Self(offs)
}
pub fn as_u32(&self) -> u32 {
self.0
}
}
/// A stack slot.
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct StackSlot {
/// The location of the slot, relative to the stack pointer.
pub offset: SPOffset,
/// The size of the slot, in bytes.
pub size: u32,
}
impl StackSlot {
pub fn new(offs: SPOffset, size: u32) -> Self {
Self { offset: offs, size }
}
}
pub trait ScratchType {
/// Derive the register class from the scratch register type.
fn reg_class() -> RegClass;
}
/// A scratch register type of integer class.
pub struct IntScratch;
/// A scratch register type of floating point class.
pub struct FloatScratch;
impl ScratchType for IntScratch {
fn reg_class() -> RegClass {
RegClass::Int
}
}
impl ScratchType for FloatScratch {
fn reg_class() -> RegClass {
RegClass::Float
}
}
/// A scratch register scope.
#[derive(Debug, Clone, Copy)]
pub struct Scratch(Reg);
impl Scratch {
pub fn new(r: Reg) -> Self {
Self(r)
}
#[inline]
pub fn inner(&self) -> Reg {
self.0
}
#[inline]
pub fn writable(&self) -> WritableReg {
writable!(self.0)
}
}
/// Kinds of integer binary comparison in WebAssembly. The [`MacroAssembler`]
/// implementation for each ISA is responsible for emitting the correct
/// sequence of instructions when lowering to machine code.
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub(crate) enum IntCmpKind {
/// Equal.
Eq,
/// Not equal.
Ne,
/// Signed less than.
LtS,
/// Unsigned less than.
LtU,
/// Signed greater than.
GtS,
/// Unsigned greater than.
GtU,
/// Signed less than or equal.
LeS,
/// Unsigned less than or equal.
LeU,
/// Signed greater than or equal.
GeS,
/// Unsigned greater than or equal.
GeU,
}
/// Kinds of float binary comparison in WebAssembly. The [`MacroAssembler`]
/// implementation for each ISA is responsible for emitting the correct
/// sequence of instructions when lowering code.
#[derive(Debug)]
pub(crate) enum FloatCmpKind {
/// Equal.
Eq,
/// Not equal.
Ne,
/// Less than.
Lt,
/// Greater than.
Gt,
/// Less than or equal.
Le,
/// Greater than or equal.
Ge,
}
/// Kinds of shifts in WebAssembly.The [`masm`] implementation for each ISA is
/// responsible for emitting the correct sequence of instructions when
/// lowering to machine code.
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub(crate) enum ShiftKind {
/// Left shift.
Shl,
/// Signed right shift.
ShrS,
/// Unsigned right shift.
ShrU,
/// Left rotate.
Rotl,
/// Right rotate.
Rotr,
}
/// Kinds of extends in WebAssembly. Each MacroAssembler implementation
/// is responsible for emitting the correct sequence of instructions when
/// lowering to machine code.
#[derive(Copy, Clone)]
pub(crate) enum ExtendKind {
Signed(Extend<Signed>),
Unsigned(Extend<Zero>),
}
#[derive(Copy, Clone)]
pub(crate) enum Signed {}
#[derive(Copy, Clone)]
pub(crate) enum Zero {}
pub(crate) trait ExtendType {}
impl ExtendType for Signed {}
impl ExtendType for Zero {}
#[derive(Copy, Clone)]
pub(crate) enum Extend<T: ExtendType> {
/// 8 to 32 bit extend.
I32Extend8,
/// 16 to 32 bit extend.
I32Extend16,
/// 8 to 64 bit extend.
I64Extend8,
/// 16 to 64 bit extend.
I64Extend16,
/// 32 to 64 bit extend.
I64Extend32,
/// Variant to hold the kind of extend marker.
///
/// This is `Signed` or `Zero`, that are empty enums, which means that this variant cannot be
/// constructed.
__Kind(T),
}
impl From<Extend<Zero>> for ExtendKind {
fn from(value: Extend<Zero>) -> Self {
ExtendKind::Unsigned(value)
}
}
impl<T: ExtendType> Extend<T> {
pub fn from_size(&self) -> OperandSize {
match self {
Extend::I32Extend8 | Extend::I64Extend8 => OperandSize::S8,
Extend::I32Extend16 | Extend::I64Extend16 => OperandSize::S16,
Extend::I64Extend32 => OperandSize::S32,
Extend::__Kind(_) => unreachable!(),
}
}
pub fn to_size(&self) -> OperandSize {
match self {
Extend::I32Extend8 | Extend::I32Extend16 => OperandSize::S32,
Extend::I64Extend8 | Extend::I64Extend16 | Extend::I64Extend32 => OperandSize::S64,
Extend::__Kind(_) => unreachable!(),
}
}
pub fn from_bits(&self) -> u8 {
self.from_size().num_bits()
}
pub fn to_bits(&self) -> u8 {
self.to_size().num_bits()
}
}
impl From<Extend<Signed>> for ExtendKind {
fn from(value: Extend<Signed>) -> Self {
ExtendKind::Signed(value)
}
}
impl ExtendKind {
pub fn signed(&self) -> bool {
match self {
Self::Signed(_) => true,
_ => false,
}
}
pub fn from_bits(&self) -> u8 {
match self {
Self::Signed(s) => s.from_bits(),
Self::Unsigned(u) => u.from_bits(),
}
}
pub fn to_bits(&self) -> u8 {
match self {
Self::Signed(s) => s.to_bits(),
Self::Unsigned(u) => u.to_bits(),
}
}
}
/// Kinds of vector load and extends in WebAssembly. Each MacroAssembler
/// implementation is responsible for emitting the correct sequence of
/// instructions when lowering to machine code.
#[derive(Copy, Clone)]
pub(crate) enum V128LoadExtendKind {
/// Sign extends eight 8 bit integers to eight 16 bit lanes.
E8x8S,
/// Zero extends eight 8 bit integers to eight 16 bit lanes.
E8x8U,
/// Sign extends four 16 bit integers to four 32 bit lanes.
E16x4S,
/// Zero extends four 16 bit integers to four 32 bit lanes.
E16x4U,
/// Sign extends two 32 bit integers to two 64 bit lanes.
E32x2S,
/// Zero extends two 32 bit integers to two 64 bit lanes.
E32x2U,
}
/// Kinds of splat loads supported by WebAssembly.
pub(crate) enum SplatLoadKind {
/// 8 bits.
S8,
/// 16 bits.
S16,
/// 32 bits.
S32,
/// 64 bits.
S64,
}
/// Kinds of splat supported by WebAssembly.
#[derive(Copy, Debug, Clone, Eq, PartialEq)]
pub(crate) enum SplatKind {
/// 8 bit integer.
I8x16,
/// 16 bit integer.
I16x8,
/// 32 bit integer.
I32x4,
/// 64 bit integer.
I64x2,
/// 32 bit float.
F32x4,
/// 64 bit float.
F64x2,
}
impl SplatKind {
/// The lane size to use for different kinds of splats.
pub(crate) fn lane_size(&self) -> OperandSize {
match self {
SplatKind::I8x16 => OperandSize::S8,
SplatKind::I16x8 => OperandSize::S16,
SplatKind::I32x4 | SplatKind::F32x4 => OperandSize::S32,
SplatKind::I64x2 | SplatKind::F64x2 => OperandSize::S64,
}
}
}
/// Kinds of extract lane supported by WebAssembly.
#[derive(Copy, Debug, Clone, Eq, PartialEq)]
pub(crate) enum ExtractLaneKind {
/// 16 lanes of 8-bit integers sign extended to 32-bits.
I8x16S,
/// 16 lanes of 8-bit integers zero extended to 32-bits.
I8x16U,
/// 8 lanes of 16-bit integers sign extended to 32-bits.
I16x8S,
/// 8 lanes of 16-bit integers zero extended to 32-bits.
I16x8U,
/// 4 lanes of 32-bit integers.
I32x4,
/// 2 lanes of 64-bit integers.
I64x2,
/// 4 lanes of 32-bit floats.
F32x4,
/// 2 lanes of 64-bit floats.
F64x2,
}
impl ExtractLaneKind {
/// The lane size to use for different kinds of extract lane kinds.
pub(crate) fn lane_size(&self) -> OperandSize {
match self {
ExtractLaneKind::I8x16S | ExtractLaneKind::I8x16U => OperandSize::S8,
ExtractLaneKind::I16x8S | ExtractLaneKind::I16x8U => OperandSize::S16,
ExtractLaneKind::I32x4 | ExtractLaneKind::F32x4 => OperandSize::S32,
ExtractLaneKind::I64x2 | ExtractLaneKind::F64x2 => OperandSize::S64,
}
}
}
impl From<ExtractLaneKind> for Extend<Signed> {
fn from(value: ExtractLaneKind) -> Self {
match value {
ExtractLaneKind::I8x16S => Extend::I32Extend8,
ExtractLaneKind::I16x8S => Extend::I32Extend16,
_ => unimplemented!(),
}
}
}
/// Kinds of replace lane supported by WebAssembly.
pub(crate) enum ReplaceLaneKind {
/// 16 lanes of 8 bit integers.
I8x16,
/// 8 lanes of 16 bit integers.
I16x8,
/// 4 lanes of 32 bit integers.
I32x4,
/// 2 lanes of 64 bit integers.
I64x2,
/// 4 lanes of 32 bit floats.
F32x4,
/// 2 lanes of 64 bit floats.
F64x2,
}
impl ReplaceLaneKind {
/// The lane size to use for different kinds of replace lane kinds.
pub(crate) fn lane_size(&self) -> OperandSize {
match self {
ReplaceLaneKind::I8x16 => OperandSize::S8,
ReplaceLaneKind::I16x8 => OperandSize::S16,
ReplaceLaneKind::I32x4 => OperandSize::S32,
ReplaceLaneKind::I64x2 => OperandSize::S64,
ReplaceLaneKind::F32x4 => OperandSize::S32,
ReplaceLaneKind::F64x2 => OperandSize::S64,
}
}
}
/// Kinds of behavior supported by Wasm loads.
pub(crate) enum LoadKind {
/// Load the entire bytes of the operand size without any modifications.
Operand(OperandSize),
/// Atomic load, with optional scalar extend.
Atomic(OperandSize, Option<ExtendKind>),
/// Duplicate value into vector lanes.
Splat(SplatLoadKind),
/// Scalar (non-vector) extend.
ScalarExtend(ExtendKind),
/// Vector extend.
VectorExtend(V128LoadExtendKind),
/// Load content into select lane.
VectorLane(LaneSelector),
/// Load a single element into the lowest bits of a vector and initialize
/// all other bits to zero.
VectorZero(OperandSize),
}
impl LoadKind {
/// Returns the [`OperandSize`] used in the load operation.
pub(crate) fn derive_operand_size(&self) -> OperandSize {
match self {
Self::ScalarExtend(extend) | Self::Atomic(_, Some(extend)) => {
Self::operand_size_for_scalar(extend)
}
Self::VectorExtend(_) => OperandSize::S64,
Self::Splat(kind) => Self::operand_size_for_splat(kind),
Self::Operand(size)
| Self::Atomic(size, None)
| Self::VectorLane(LaneSelector { size, .. })
| Self::VectorZero(size) => *size,
}
}
pub fn vector_lane(lane: u8, size: OperandSize) -> Self {
Self::VectorLane(LaneSelector { lane, size })
}
fn operand_size_for_scalar(extend_kind: &ExtendKind) -> OperandSize {
match extend_kind {
ExtendKind::Signed(s) => s.from_size(),
ExtendKind::Unsigned(u) => u.from_size(),
}
}
fn operand_size_for_splat(kind: &SplatLoadKind) -> OperandSize {
match kind {
SplatLoadKind::S8 => OperandSize::S8,
SplatLoadKind::S16 => OperandSize::S16,
SplatLoadKind::S32 => OperandSize::S32,
SplatLoadKind::S64 => OperandSize::S64,
}
}
pub(crate) fn is_atomic(&self) -> bool {
matches!(self, Self::Atomic(_, _))
}
}
/// Kinds of behavior supported by Wasm loads.
#[derive(Copy, Clone)]
pub enum StoreKind {
/// Store the entire bytes of the operand size without any modifications.
Operand(OperandSize),
/// Store the entire bytes of the operand size without any modifications, atomically.
Atomic(OperandSize),
/// Store the content of selected lane.
VectorLane(LaneSelector),
}
impl StoreKind {
pub fn vector_lane(lane: u8, size: OperandSize) -> Self {
Self::VectorLane(LaneSelector { lane, size })
}
}
#[derive(Copy, Clone)]
pub struct LaneSelector {
pub lane: u8,
pub size: OperandSize,
}
/// Types of vector integer to float conversions supported by WebAssembly.
pub(crate) enum V128ConvertKind {
/// 4 lanes of signed 32-bit integers to 4 lanes of 32-bit floats.
I32x4S,
/// 4 lanes of unsigned 32-bit integers to 4 lanes of 32-bit floats.
I32x4U,
/// 4 lanes of signed 32-bit integers to low bits of 2 lanes of 64-bit
/// floats.
I32x4LowS,
/// 4 lanes of unsigned 32-bit integers to low bits of 2 lanes of 64-bit
/// floats.
I32x4LowU,
}
impl V128ConvertKind {
pub(crate) fn src_lane_size(&self) -> OperandSize {
match self {
V128ConvertKind::I32x4S
| V128ConvertKind::I32x4U
| V128ConvertKind::I32x4LowS
| V128ConvertKind::I32x4LowU => OperandSize::S32,
}
}
pub(crate) fn dst_lane_size(&self) -> OperandSize {
match self {
V128ConvertKind::I32x4S | V128ConvertKind::I32x4U => OperandSize::S32,
V128ConvertKind::I32x4LowS | V128ConvertKind::I32x4LowU => OperandSize::S64,
}
}
}
/// Kinds of vector narrowing operations supported by WebAssembly.
pub(crate) enum V128NarrowKind {
/// Narrow 8 lanes of 16-bit integers to 16 lanes of 8-bit integers using
/// signed saturation.
I16x8S,
/// Narrow 8 lanes of 16-bit integers to 16 lanes of 8-bit integers using
/// unsigned saturation.
I16x8U,
/// Narrow 4 lanes of 32-bit integers to 8 lanes of 16-bit integers using
/// signed saturation.
I32x4S,
/// Narrow 4 lanes of 32-bit integers to 8 lanes of 16-bit integers using
/// unsigned saturation.
I32x4U,
}
impl V128NarrowKind {
/// Return the size of the destination lanes.
pub(crate) fn dst_lane_size(&self) -> OperandSize {
match self {
Self::I16x8S | Self::I16x8U => OperandSize::S8,
Self::I32x4S | Self::I32x4U => OperandSize::S16,
}
}
}
/// Kinds of vector extending operations supported by WebAssembly.
#[derive(Debug, Copy, Clone)]
pub(crate) enum V128ExtendKind {
/// Low half of i8x16 sign extended.
LowI8x16S,
/// High half of i8x16 sign extended.
HighI8x16S,
/// Low half of i8x16 zero extended.
LowI8x16U,
/// High half of i8x16 zero extended.
HighI8x16U,
/// Low half of i16x8 sign extended.
LowI16x8S,
/// High half of i16x8 sign extended.
HighI16x8S,
/// Low half of i16x8 zero extended.
LowI16x8U,
/// High half of i16x8 zero extended.
HighI16x8U,
/// Low half of i32x4 sign extended.
LowI32x4S,
/// High half of i32x4 sign extended.
HighI32x4S,
/// Low half of i32x4 zero extended.
LowI32x4U,
/// High half of i32x4 zero extended.
HighI32x4U,
}
impl V128ExtendKind {
/// The size of the source's lanes.
pub(crate) fn src_lane_size(&self) -> OperandSize {
match self {
Self::LowI8x16S | Self::LowI8x16U | Self::HighI8x16S | Self::HighI8x16U => {
OperandSize::S8
}
Self::LowI16x8S | Self::LowI16x8U | Self::HighI16x8S | Self::HighI16x8U => {
OperandSize::S16
}
Self::LowI32x4S | Self::LowI32x4U | Self::HighI32x4S | Self::HighI32x4U => {
OperandSize::S32
}
}
}
}
/// Kinds of vector equalities and non-equalities supported by WebAssembly.
pub(crate) enum VectorEqualityKind {
/// 16 lanes of 8 bit integers.
I8x16,
/// 8 lanes of 16 bit integers.
I16x8,
/// 4 lanes of 32 bit integers.
I32x4,
/// 2 lanes of 64 bit integers.
I64x2,
/// 4 lanes of 32 bit floats.
F32x4,
/// 2 lanes of 64 bit floats.
F64x2,
}
impl VectorEqualityKind {
/// Get the lane size to use.
pub(crate) fn lane_size(&self) -> OperandSize {
match self {
Self::I8x16 => OperandSize::S8,
Self::I16x8 => OperandSize::S16,
Self::I32x4 | Self::F32x4 => OperandSize::S32,
Self::I64x2 | Self::F64x2 => OperandSize::S64,
}
}
}
/// Kinds of vector comparisons supported by WebAssembly.
pub(crate) enum VectorCompareKind {
/// 16 lanes of signed 8 bit integers.
I8x16S,
/// 16 lanes of unsigned 8 bit integers.
I8x16U,
/// 8 lanes of signed 16 bit integers.
I16x8S,
/// 8 lanes of unsigned 16 bit integers.
I16x8U,
/// 4 lanes of signed 32 bit integers.
I32x4S,
/// 4 lanes of unsigned 32 bit integers.
I32x4U,
/// 2 lanes of signed 64 bit integers.
I64x2S,
/// 4 lanes of 32 bit floats.
F32x4,
/// 2 lanes of 64 bit floats.
F64x2,
}
impl VectorCompareKind {
/// Get the lane size to use.
pub(crate) fn lane_size(&self) -> OperandSize {
match self {
Self::I8x16S | Self::I8x16U => OperandSize::S8,
Self::I16x8S | Self::I16x8U => OperandSize::S16,
Self::I32x4S | Self::I32x4U | Self::F32x4 => OperandSize::S32,
Self::I64x2S | Self::F64x2 => OperandSize::S64,
}
}
}
/// Kinds of vector absolute operations supported by WebAssembly.
#[derive(Copy, Debug, Clone, Eq, PartialEq)]
pub(crate) enum V128AbsKind {
/// 8 bit integers.
I8x16,
/// 16 bit integers.
I16x8,
/// 32 bit integers.
I32x4,
/// 64 bit integers.
I64x2,
/// 32 bit floats.
F32x4,
/// 64 bit floats.
F64x2,
}
impl V128AbsKind {
/// The lane size to use.
pub(crate) fn lane_size(&self) -> OperandSize {
match self {
Self::I8x16 => OperandSize::S8,
Self::I16x8 => OperandSize::S16,
Self::I32x4 | Self::F32x4 => OperandSize::S32,
Self::I64x2 | Self::F64x2 => OperandSize::S64,
}
}
}
/// Kinds of truncation for vectors supported by WebAssembly.
pub(crate) enum V128TruncKind {
/// Truncates 4 lanes of 32-bit floats to nearest integral value.
F32x4,
/// Truncates 2 lanes of 64-bit floats to nearest integral value.
F64x2,
/// Integers from signed F32x4.
I32x4FromF32x4S,
/// Integers from unsigned F32x4.
I32x4FromF32x4U,
/// Integers from signed F64x2.
I32x4FromF64x2SZero,
/// Integers from unsigned F64x2.
I32x4FromF64x2UZero,
}
impl V128TruncKind {
/// The size of the source lanes.
pub(crate) fn src_lane_size(&self) -> OperandSize {
match self {
V128TruncKind::F32x4
| V128TruncKind::I32x4FromF32x4S
| V128TruncKind::I32x4FromF32x4U => OperandSize::S32,
V128TruncKind::F64x2
| V128TruncKind::I32x4FromF64x2SZero
| V128TruncKind::I32x4FromF64x2UZero => OperandSize::S64,
}
}
/// The size of the destination lanes.
pub(crate) fn dst_lane_size(&self) -> OperandSize {
if let V128TruncKind::F64x2 = self {
OperandSize::S64
} else {
OperandSize::S32
}
}
}
/// Kinds of vector addition supported by WebAssembly.
pub(crate) enum V128AddKind {
/// 4 lanes of 32-bit floats wrapping.
F32x4,
/// 2 lanes of 64-bit floats wrapping.
F64x2,
/// 16 lanes of 8-bit integers wrapping.
I8x16,
/// 16 lanes of 8-bit integers signed saturating.
I8x16SatS,
/// 16 lanes of 8-bit integers unsigned saturating.
I8x16SatU,
/// 8 lanes of 16-bit integers wrapping.
I16x8,
/// 8 lanes of 16-bit integers signed saturating.
I16x8SatS,
/// 8 lanes of 16-bit integers unsigned saturating.
I16x8SatU,
/// 4 lanes of 32-bit integers wrapping.
I32x4,
/// 2 lanes of 64-bit integers wrapping.
I64x2,
}
/// Kinds of vector subtraction supported by WebAssembly.
pub(crate) enum V128SubKind {
/// 4 lanes of 32-bit floats wrapping.
F32x4,
/// 2 lanes of 64-bit floats wrapping.
F64x2,
/// 16 lanes of 8-bit integers wrapping.
I8x16,
/// 16 lanes of 8-bit integers signed saturating.
I8x16SatS,
/// 16 lanes of 8-bit integers unsigned saturating.
I8x16SatU,
/// 8 lanes of 16-bit integers wrapping.
I16x8,
/// 8 lanes of 16-bit integers signed saturating.
I16x8SatS,
/// 8 lanes of 16-bit integers unsigned saturating.
I16x8SatU,
/// 4 lanes of 32-bit integers wrapping.
I32x4,
/// 2 lanes of 64-bit integers wrapping.
I64x2,
}
impl From<V128NegKind> for V128SubKind {
fn from(value: V128NegKind) -> Self {
match value {
V128NegKind::I8x16 => Self::I8x16,
V128NegKind::I16x8 => Self::I16x8,
V128NegKind::I32x4 => Self::I32x4,
V128NegKind::I64x2 => Self::I64x2,
V128NegKind::F32x4 | V128NegKind::F64x2 => unimplemented!(),
}
}
}
/// Kinds of vector multiplication supported by WebAssembly.
pub(crate) enum V128MulKind {
/// 4 lanes of 32-bit floats.
F32x4,
/// 2 lanes of 64-bit floats.
F64x2,
/// 8 lanes of 16-bit integers.
I16x8,
/// 4 lanes of 32-bit integers.
I32x4,
/// 2 lanes of 64-bit integers.
I64x2,
}
/// Kinds of vector negation supported by WebAssembly.
#[derive(Copy, Clone)]
pub(crate) enum V128NegKind {
/// 4 lanes of 32-bit floats.
F32x4,
/// 2 lanes of 64-bit floats.
F64x2,
/// 16 lanes of 8-bit integers.
I8x16,
/// 8 lanes of 16-bit integers.
I16x8,
/// 4 lanes of 32-bit integers.
I32x4,
/// 2 lanes of 64-bit integers.
I64x2,
}
impl V128NegKind {
/// The size of the lanes.
pub(crate) fn lane_size(&self) -> OperandSize {
match self {
Self::F32x4 | Self::I32x4 => OperandSize::S32,
Self::F64x2 | Self::I64x2 => OperandSize::S64,
Self::I8x16 => OperandSize::S8,
Self::I16x8 => OperandSize::S16,
}
}
}
/// Kinds of extended pairwise addition supported by WebAssembly.
pub(crate) enum V128ExtAddKind {
/// 16 lanes of signed 8-bit integers.
I8x16S,
/// 16 lanes of unsigned 8-bit integers.
I8x16U,
/// 8 lanes of signed 16-bit integers.
I16x8S,
/// 8 lanes of unsigned 16-bit integers.
I16x8U,
}
/// Kinds of vector extended multiplication supported by WebAssembly.
#[derive(Debug, Clone, Copy)]
pub(crate) enum V128ExtMulKind {
LowI8x16S,
HighI8x16S,
LowI8x16U,
HighI8x16U,
LowI16x8S,
HighI16x8S,
LowI16x8U,
HighI16x8U,
LowI32x4S,
HighI32x4S,
LowI32x4U,
HighI32x4U,
}
impl From<V128ExtMulKind> for V128ExtendKind {
fn from(value: V128ExtMulKind) -> Self {
match value {
V128ExtMulKind::LowI8x16S => Self::LowI8x16S,
V128ExtMulKind::HighI8x16S => Self::HighI8x16S,
V128ExtMulKind::LowI8x16U => Self::LowI8x16U,
V128ExtMulKind::HighI8x16U => Self::HighI8x16U,
V128ExtMulKind::LowI16x8S => Self::LowI16x8S,
V128ExtMulKind::HighI16x8S => Self::HighI16x8S,
V128ExtMulKind::LowI16x8U => Self::LowI16x8U,
V128ExtMulKind::HighI16x8U => Self::HighI16x8U,
V128ExtMulKind::LowI32x4S => Self::LowI32x4S,
V128ExtMulKind::HighI32x4S => Self::HighI32x4S,
V128ExtMulKind::LowI32x4U => Self::LowI32x4U,
V128ExtMulKind::HighI32x4U => Self::HighI32x4U,
}