forked from bytecodealliance/wasmtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmasm.rs
More file actions
1383 lines (1200 loc) · 43.6 KB
/
masm.rs
File metadata and controls
1383 lines (1200 loc) · 43.6 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::abi::{self, align_to, scratch, LocalSlot};
use crate::codegen::{CodeGenContext, Emission, FuncEnv};
use crate::isa::{
reg::{writable, Reg, WritableReg},
CallingConvention,
};
use anyhow::Result;
use cranelift_codegen::{
binemit::CodeOffset,
ir::{Endianness, LibCall, MemFlags, RelSourceLoc, SourceLoc, UserExternalNameRef},
Final, MachBufferFinalized, MachLabel,
};
use std::{fmt::Debug, ops::Range};
use wasmtime_environ::PtrSize;
pub(crate) use cranelift_codegen::ir::TrapCode;
#[derive(Eq, PartialEq)]
pub(crate) enum DivKind {
/// Signed division.
Signed,
/// Unsigned division.
Unsigned,
}
/// 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)
}
}
#[derive(Copy, Clone, PartialEq, Eq)]
pub(crate) enum MemOpKind {
/// An atomic memory operation with SeqCst memory ordering.
Atomic,
/// A memory operation with no memory ordering constraint.
Normal,
}
#[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 }
}
}
/// 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 {
/// 8 to 32 bit signed extend.
I32Extend8S,
/// 8 to 32 bit unsigned extend.
I32Extend8U,
/// 16 to 32 bit signed extend.
I32Extend16S,
/// 16 to 32 bit unsigned extend.
I32Extend16U,
/// 8 to 64 bit signed extend.
I64Extend8S,
/// 8 to 64 bit unsigned extend.
I64Extend8U,
/// 16 to 64 bit signed extend.
I64Extend16S,
/// 16 to 64 bit unsigned extend.
I64Extend16U,
/// 32 to 64 bit signed extend.
I64Extend32S,
/// 32 to 64 bit unsigned extend.
I64Extend32U,
}
impl ExtendKind {
pub fn signed(&self) -> bool {
match self {
Self::I32Extend8S
| Self::I32Extend16S
| Self::I64Extend8S
| Self::I64Extend16S
| Self::I64Extend32S => true,
_ => false,
}
}
pub fn from_bits(&self) -> u8 {
match self {
Self::I64Extend32S | Self::I64Extend32U => 32,
Self::I32Extend8S | Self::I32Extend8U | Self::I64Extend8S | Self::I64Extend8U => 8,
Self::I32Extend16S | Self::I64Extend16S | Self::I32Extend16U | Self::I64Extend16U => 16,
}
}
pub fn to_bits(&self) -> u8 {
match self {
Self::I64Extend32S
| Self::I64Extend32U
| Self::I64Extend8S
| Self::I64Extend8U
| Self::I64Extend16S
| Self::I64Extend16U => 64,
Self::I32Extend8S | Self::I32Extend8U | Self::I32Extend16U | Self::I32Extend16S => 32,
}
}
}
/// Kinds of vector extends in WebAssembly. Each MacroAssembler implementation
/// is responsible for emitting the correct sequence of instructions when
/// lowering to machine code.
pub(crate) enum VectorExtendKind {
/// Sign extends eight 8 bit integers to eight 16 bit lanes.
V128Extend8x8S,
/// Zero extends eight 8 bit integers to eight 16 bit lanes.
V128Extend8x8U,
/// Sign extends four 16 bit integers to four 32 bit lanes.
V128Extend16x4S,
/// Zero extends four 16 bit integers to four 32 bit lanes.
V128Extend16x4U,
/// Sign extends two 32 bit integers to two 64 bit lanes.
V128Extend32x2S,
/// Zero extends two 32 bit integers to two 64 bit lanes.
V128Extend32x2U,
}
/// 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 ExtendKind {
fn from(value: ExtractLaneKind) -> Self {
match value {
ExtractLaneKind::I8x16S => Self::I32Extend8S,
ExtractLaneKind::I16x8S => Self::I32Extend16S,
_ => unimplemented!(),
}
}
}
/// Kinds of behavior supported by Wasm loads.
pub(crate) enum LoadKind {
/// Load the entire bytes of the operand size without any modifications.
Operand(OperandSize),
/// Duplicate value into vector lanes.
Splat(SplatLoadKind),
/// Scalar (non-vector) extend.
ScalarExtend(ExtendKind),
/// Vector extend.
VectorExtend(VectorExtendKind),
}
impl LoadKind {
/// Returns the [`OperandSize`] used in the load operation.
pub(crate) fn derive_operand_size(&self) -> OperandSize {
match self {
Self::ScalarExtend(scalar) => Self::operand_size_for_scalar(scalar),
Self::VectorExtend(vector) => Self::operand_size_for_vector(vector),
Self::Splat(kind) => Self::operand_size_for_splat(kind),
Self::Operand(op) => *op,
}
}
fn operand_size_for_vector(vector: &VectorExtendKind) -> OperandSize {
match vector {
VectorExtendKind::V128Extend8x8S | VectorExtendKind::V128Extend8x8U => OperandSize::S8,
VectorExtendKind::V128Extend16x4S | VectorExtendKind::V128Extend16x4U => {
OperandSize::S16
}
VectorExtendKind::V128Extend32x2S | VectorExtendKind::V128Extend32x2U => {
OperandSize::S32
}
}
}
fn operand_size_for_scalar(extend_kind: &ExtendKind) -> OperandSize {
match extend_kind {
ExtendKind::I32Extend8S
| ExtendKind::I32Extend8U
| ExtendKind::I64Extend8S
| ExtendKind::I64Extend8U => OperandSize::S8,
ExtendKind::I32Extend16S
| ExtendKind::I32Extend16U
| ExtendKind::I64Extend16U
| ExtendKind::I64Extend16S => OperandSize::S16,
ExtendKind::I64Extend32U | ExtendKind::I64Extend32S => OperandSize::S32,
}
}
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,
}
}
}
/// Operand size, in bits.
#[derive(Copy, Debug, Clone, Eq, PartialEq)]
pub(crate) enum OperandSize {
/// 8 bits.
S8,
/// 16 bits.
S16,
/// 32 bits.
S32,
/// 64 bits.
S64,
/// 128 bits.
S128,
}
impl OperandSize {
/// The number of bits in the operand.
pub fn num_bits(&self) -> u8 {
match self {
OperandSize::S8 => 8,
OperandSize::S16 => 16,
OperandSize::S32 => 32,
OperandSize::S64 => 64,
OperandSize::S128 => 128,
}
}
/// The number of bytes in the operand.
pub fn bytes(&self) -> u32 {
match self {
Self::S8 => 1,
Self::S16 => 2,
Self::S32 => 4,
Self::S64 => 8,
Self::S128 => 16,
}
}
/// The binary logarithm of the number of bits in the operand.
pub fn log2(&self) -> u8 {
match self {
OperandSize::S8 => 3,
OperandSize::S16 => 4,
OperandSize::S32 => 5,
OperandSize::S64 => 6,
OperandSize::S128 => 7,
}
}
/// Create an [`OperandSize`] from the given number of bytes.
pub fn from_bytes(bytes: u8) -> Self {
use OperandSize::*;
match bytes {
4 => S32,
8 => S64,
16 => S128,
_ => panic!("Invalid bytes {bytes} for OperandSize"),
}
}
}
/// An abstraction over a register or immediate.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub(crate) enum RegImm {
/// A register.
Reg(Reg),
/// A tagged immediate argument.
Imm(Imm),
}
/// An tagged representation of an immediate.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub(crate) enum Imm {
/// I32 immediate.
I32(u32),
/// I64 immediate.
I64(u64),
/// F32 immediate.
F32(u32),
/// F64 immediate.
F64(u64),
/// V128 immediate.
V128(i128),
}
impl Imm {
/// Create a new I64 immediate.
pub fn i64(val: i64) -> Self {
Self::I64(val as u64)
}
/// Create a new I32 immediate.
pub fn i32(val: i32) -> Self {
Self::I32(val as u32)
}
/// Create a new F32 immediate.
pub fn f32(bits: u32) -> Self {
Self::F32(bits)
}
/// Create a new F64 immediate.
pub fn f64(bits: u64) -> Self {
Self::F64(bits)
}
/// Create a new V128 immediate.
pub fn v128(bits: i128) -> Self {
Self::V128(bits)
}
/// Convert the immediate to i32, if possible.
pub fn to_i32(&self) -> Option<i32> {
match self {
Self::I32(v) => Some(*v as i32),
Self::I64(v) => i32::try_from(*v as i64).ok(),
_ => None,
}
}
/// Returns true if the [`Imm`] is float.
pub fn is_float(&self) -> bool {
match self {
Self::F32(_) | Self::F64(_) => true,
_ => false,
}
}
/// Get the operand size of the immediate.
pub fn size(&self) -> OperandSize {
match self {
Self::I32(_) | Self::F32(_) => OperandSize::S32,
Self::I64(_) | Self::F64(_) => OperandSize::S64,
Self::V128(_) => OperandSize::S128,
}
}
/// Get a little endian representation of the immediate.
///
/// This method heap allocates and is intended to be used when adding
/// values to the constant pool.
pub fn to_bytes(&self) -> Vec<u8> {
match self {
Imm::I32(n) => n.to_le_bytes().to_vec(),
Imm::I64(n) => n.to_le_bytes().to_vec(),
Imm::F32(n) => n.to_le_bytes().to_vec(),
Imm::F64(n) => n.to_le_bytes().to_vec(),
Imm::V128(n) => n.to_le_bytes().to_vec(),
}
}
}
/// The location of the [VMcontext] used for function calls.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub(crate) enum VMContextLoc {
/// Dynamic, stored in the given register.
Reg(Reg),
/// The pinned [VMContext] register.
Pinned,
}
/// The maximum number of context arguments currently used across the compiler.
pub(crate) const MAX_CONTEXT_ARGS: usize = 2;
/// Out-of-band special purpose arguments used for function call emission.
///
/// We cannot rely on the value stack for these values given that inserting
/// register or memory values at arbitrary locations of the value stack has the
/// potential to break the stack ordering principle, which states that older
/// values must always precede newer values, effectively simulating the order of
/// values in the machine stack.
/// The [ContextArgs] are meant to be resolved at every callsite; in some cases
/// it might be possible to construct it early on, but given that it might
/// contain allocatable registers, it's preferred to construct it in
/// [FnCall::emit].
#[derive(Clone, Debug)]
pub(crate) enum ContextArgs {
/// No context arguments required. This is used for libcalls that don't
/// require any special context arguments. For example builtin functions
/// that perform float calculations.
None,
/// A single context argument is required; the current pinned [VMcontext]
/// register must be passed as the first argument of the function call.
VMContext([VMContextLoc; 1]),
/// The callee and caller context arguments are required. In this case, the
/// callee context argument is usually stored into an allocatable register
/// and the caller is always the current pinned [VMContext] pointer.
CalleeAndCallerVMContext([VMContextLoc; MAX_CONTEXT_ARGS]),
}
impl ContextArgs {
/// Construct an empty [ContextArgs].
pub fn none() -> Self {
Self::None
}
/// Construct a [ContextArgs] declaring the usage of the pinned [VMContext]
/// register as both the caller and callee context arguments.
pub fn pinned_callee_and_caller_vmctx() -> Self {
Self::CalleeAndCallerVMContext([VMContextLoc::Pinned, VMContextLoc::Pinned])
}
/// Construct a [ContextArgs] that declares the usage of the pinned
/// [VMContext] register as the only context argument.
pub fn pinned_vmctx() -> Self {
Self::VMContext([VMContextLoc::Pinned])
}
/// Construct a [ContextArgs] that declares a dynamic callee context and the
/// pinned [VMContext] register as the context arguments.
pub fn with_callee_and_pinned_caller(callee_vmctx: Reg) -> Self {
Self::CalleeAndCallerVMContext([VMContextLoc::Reg(callee_vmctx), VMContextLoc::Pinned])
}
/// Get the length of the [ContextArgs].
pub fn len(&self) -> usize {
self.as_slice().len()
}
/// Get a slice of the context arguments.
pub fn as_slice(&self) -> &[VMContextLoc] {
match self {
Self::None => &[],
Self::VMContext(a) => a.as_slice(),
Self::CalleeAndCallerVMContext(a) => a.as_slice(),
}
}
}
#[derive(Copy, Clone, Debug)]
pub(crate) enum CalleeKind {
/// A function call to a raw address.
Indirect(Reg),
/// A function call to a local function.
Direct(UserExternalNameRef),
/// Call to a well known LibCall.
LibCall(LibCall),
}
impl CalleeKind {
/// Creates a callee kind from a register.
pub fn indirect(reg: Reg) -> Self {
Self::Indirect(reg)
}
/// Creates a direct callee kind from a function name.
pub fn direct(name: UserExternalNameRef) -> Self {
Self::Direct(name)
}
/// Creates a known callee kind from a libcall.
pub fn libcall(call: LibCall) -> Self {
Self::LibCall(call)
}
}
impl RegImm {
/// Register constructor.
pub fn reg(r: Reg) -> Self {
RegImm::Reg(r)
}
/// I64 immediate constructor.
pub fn i64(val: i64) -> Self {
RegImm::Imm(Imm::i64(val))
}
/// I32 immediate constructor.
pub fn i32(val: i32) -> Self {
RegImm::Imm(Imm::i32(val))
}
/// F32 immediate, stored using its bits representation.
pub fn f32(bits: u32) -> Self {
RegImm::Imm(Imm::f32(bits))
}
/// F64 immediate, stored using its bits representation.
pub fn f64(bits: u64) -> Self {
RegImm::Imm(Imm::f64(bits))
}
/// V128 immediate.
pub fn v128(bits: i128) -> Self {
RegImm::Imm(Imm::v128(bits))
}
}
impl From<Reg> for RegImm {
fn from(r: Reg) -> Self {
Self::Reg(r)
}
}
#[derive(Debug)]
pub enum RoundingMode {
Nearest,
Up,
Down,
Zero,
}
/// Memory flags for trusted loads/stores.
pub const TRUSTED_FLAGS: MemFlags = MemFlags::trusted();
/// Flags used for WebAssembly loads / stores.
/// Untrusted by default so we don't set `no_trap`.
/// We also ensure that the endianness is the right one for WebAssembly.
pub const UNTRUSTED_FLAGS: MemFlags = MemFlags::new().with_endianness(Endianness::Little);
/// Generic MacroAssembler interface used by the code generation.
///
/// The MacroAssembler trait aims to expose an interface, high-level enough,
/// so that each ISA can provide its own lowering to machine code. For example,
/// for WebAssembly operators that don't have a direct mapping to a machine
/// a instruction, the interface defines a signature matching the WebAssembly
/// operator, allowing each implementation to lower such operator entirely.
/// This approach attributes more responsibility to the MacroAssembler, but frees
/// the caller from concerning about assembling the right sequence of
/// instructions at the operator callsite.
///
/// The interface defaults to a three-argument form for binary operations;
/// this allows a natural mapping to instructions for RISC architectures,
/// that use three-argument form.
/// This approach allows for a more general interface that can be restricted
/// where needed, in the case of architectures that use a two-argument form.
pub(crate) trait MacroAssembler {
/// The addressing mode.
type Address: Copy + Debug;
/// The pointer representation of the target ISA,
/// used to access information from [`VMOffsets`].
type Ptr: PtrSize;
/// The ABI details of the target.
type ABI: abi::ABI;
/// Emit the function prologue.
fn prologue(&mut self, vmctx: Reg) -> Result<()> {
self.frame_setup()?;
self.check_stack(vmctx)
}
/// Generate the frame setup sequence.
fn frame_setup(&mut self) -> Result<()>;
/// Generate the frame restore sequence.
fn frame_restore(&mut self) -> Result<()>;
/// Emit a stack check.
fn check_stack(&mut self, vmctx: Reg) -> Result<()>;
/// Emit the function epilogue.
fn epilogue(&mut self) -> Result<()> {
self.frame_restore()
}
/// Reserve stack space.
fn reserve_stack(&mut self, bytes: u32) -> Result<()>;
/// Free stack space.
fn free_stack(&mut self, bytes: u32) -> Result<()>;
/// Reset the stack pointer to the given offset;
///
/// Used to reset the stack pointer to a given offset
/// when dealing with unreachable code.
fn reset_stack_pointer(&mut self, offset: SPOffset) -> Result<()>;
/// Get the address of a local slot.
fn local_address(&mut self, local: &LocalSlot) -> Result<Self::Address>;
/// Constructs an address with an offset that is relative to the
/// current position of the stack pointer (e.g. [sp + (sp_offset -
/// offset)].
fn address_from_sp(&self, offset: SPOffset) -> Result<Self::Address>;
/// Constructs an address with an offset that is absolute to the
/// current position of the stack pointer (e.g. [sp + offset].
fn address_at_sp(&self, offset: SPOffset) -> Result<Self::Address>;
/// Alias for [`Self::address_at_reg`] using the VMContext register as
/// a base. The VMContext register is derived from the ABI type that is
/// associated to the MacroAssembler.
fn address_at_vmctx(&self, offset: u32) -> Result<Self::Address>;
/// Construct an address that is absolute to the current position
/// of the given register.
fn address_at_reg(&self, reg: Reg, offset: u32) -> Result<Self::Address>;
/// Emit a function call to either a local or external function.
fn call(
&mut self,
stack_args_size: u32,
f: impl FnMut(&mut Self) -> Result<(CalleeKind, CallingConvention)>,
) -> Result<u32>;
/// Get stack pointer offset.
fn sp_offset(&self) -> Result<SPOffset>;
/// Perform a stack store.
fn store(&mut self, src: RegImm, dst: Self::Address, size: OperandSize) -> Result<()>;
/// Alias for `MacroAssembler::store` with the operand size corresponding
/// to the pointer size of the target.
fn store_ptr(&mut self, src: Reg, dst: Self::Address) -> Result<()>;
/// Perform a WebAssembly store.
/// A WebAssembly store introduces several additional invariants compared to
/// [Self::store], more precisely, it can implicitly trap, in certain
/// circumstances, even if explicit bounds checks are elided, in that sense,
/// we consider this type of load as untrusted. It can also differ with
/// regards to the endianness depending on the target ISA. For this reason,
/// [Self::wasm_store], should be explicitly used when emitting WebAssembly
/// stores.
fn wasm_store(
&mut self,
src: Reg,
dst: Self::Address,
size: OperandSize,
op_kind: MemOpKind,
) -> Result<()>;
/// Perform a zero-extended stack load.
fn load(&mut self, src: Self::Address, dst: WritableReg, size: OperandSize) -> Result<()>;
/// Perform a WebAssembly load.
/// A WebAssembly load introduces several additional invariants compared to
/// [Self::load], more precisely, it can implicitly trap, in certain
/// circumstances, even if explicit bounds checks are elided, in that sense,
/// we consider this type of load as untrusted. It can also differ with
/// regards to the endianness depending on the target ISA. For this reason,
/// [Self::wasm_load], should be explicitly used when emitting WebAssembly
/// loads.
fn wasm_load(
&mut self,
src: Self::Address,
dst: WritableReg,
kind: LoadKind,
op_kind: MemOpKind,
) -> Result<()>;
/// Alias for `MacroAssembler::load` with the operand size corresponding
/// to the pointer size of the target.
fn load_ptr(&mut self, src: Self::Address, dst: WritableReg) -> Result<()>;
/// Loads the effective address into destination.
fn load_addr(
&mut self,
_src: Self::Address,
_dst: WritableReg,
_size: OperandSize,
) -> Result<()>;
/// Pop a value from the machine stack into the given register.
fn pop(&mut self, dst: WritableReg, size: OperandSize) -> Result<()>;
/// Perform a move.
fn mov(&mut self, dst: WritableReg, src: RegImm, size: OperandSize) -> Result<()>;
/// Perform a conditional move.
fn cmov(&mut self, dst: WritableReg, src: Reg, cc: IntCmpKind, size: OperandSize)
-> Result<()>;
/// Performs a memory move of bytes from src to dest.
/// Bytes are moved in blocks of 8 bytes, where possible.
fn memmove(
&mut self,
src: SPOffset,
dst: SPOffset,
bytes: u32,
direction: MemMoveDirection,
) -> Result<()> {
match direction {
MemMoveDirection::LowToHigh => debug_assert!(dst.as_u32() < src.as_u32()),
MemMoveDirection::HighToLow => debug_assert!(dst.as_u32() > src.as_u32()),
}
// At least 4 byte aligned.
debug_assert!(bytes % 4 == 0);
let mut remaining = bytes;
let word_bytes = <Self::ABI as abi::ABI>::word_bytes();
let scratch = scratch!(Self);
let mut dst_offs = dst.as_u32() - bytes;
let mut src_offs = src.as_u32() - bytes;
let word_bytes = word_bytes as u32;
while remaining >= word_bytes {
remaining -= word_bytes;
dst_offs += word_bytes;
src_offs += word_bytes;
self.load_ptr(
self.address_from_sp(SPOffset::from_u32(src_offs))?,
writable!(scratch),
)?;
self.store_ptr(
scratch.into(),
self.address_from_sp(SPOffset::from_u32(dst_offs))?,
)?;
}
if remaining > 0 {
let half_word = word_bytes / 2;
let ptr_size = OperandSize::from_bytes(half_word as u8);
debug_assert!(remaining == half_word);
dst_offs += half_word;
src_offs += half_word;
self.load(
self.address_from_sp(SPOffset::from_u32(src_offs))?,
writable!(scratch),
ptr_size,
)?;
self.store(
scratch.into(),
self.address_from_sp(SPOffset::from_u32(dst_offs))?,
ptr_size,
)?;
}
Ok(())
}
/// Perform add operation.
fn add(&mut self, dst: WritableReg, lhs: Reg, rhs: RegImm, size: OperandSize) -> Result<()>;
/// Perform a checked unsigned integer addition, emitting the provided trap
/// if the addition overflows.
fn checked_uadd(
&mut self,
dst: WritableReg,
lhs: Reg,
rhs: RegImm,
size: OperandSize,
trap: TrapCode,
) -> Result<()>;
/// Perform subtraction operation.
fn sub(&mut self, dst: WritableReg, lhs: Reg, rhs: RegImm, size: OperandSize) -> Result<()>;
/// Perform multiplication operation.
fn mul(&mut self, dst: WritableReg, lhs: Reg, rhs: RegImm, size: OperandSize) -> Result<()>;
/// Perform a floating point add operation.
fn float_add(&mut self, dst: WritableReg, lhs: Reg, rhs: Reg, size: OperandSize) -> Result<()>;
/// Perform a floating point subtraction operation.
fn float_sub(&mut self, dst: WritableReg, lhs: Reg, rhs: Reg, size: OperandSize) -> Result<()>;
/// Perform a floating point multiply operation.
fn float_mul(&mut self, dst: WritableReg, lhs: Reg, rhs: Reg, size: OperandSize) -> Result<()>;
/// Perform a floating point divide operation.
fn float_div(&mut self, dst: WritableReg, lhs: Reg, rhs: Reg, size: OperandSize) -> Result<()>;
/// Perform a floating point minimum operation. In x86, this will emit
/// multiple instructions.
fn float_min(&mut self, dst: WritableReg, lhs: Reg, rhs: Reg, size: OperandSize) -> Result<()>;
/// Perform a floating point maximum operation. In x86, this will emit
/// multiple instructions.
fn float_max(&mut self, dst: WritableReg, lhs: Reg, rhs: Reg, size: OperandSize) -> Result<()>;
/// Perform a floating point copysign operation. In x86, this will emit
/// multiple instructions.
fn float_copysign(
&mut self,
dst: WritableReg,
lhs: Reg,
rhs: Reg,
size: OperandSize,
) -> Result<()>;
/// Perform a floating point abs operation.
fn float_abs(&mut self, dst: WritableReg, size: OperandSize) -> Result<()>;
/// Perform a floating point negation operation.
fn float_neg(&mut self, dst: WritableReg, size: OperandSize) -> Result<()>;