forked from ruby/ruby
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathir.rs
2152 lines (1852 loc) · 74.1 KB
/
ir.rs
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 std::collections::HashMap;
use std::fmt;
use std::convert::From;
use std::mem::take;
use crate::codegen::{gen_counted_exit, gen_outlined_exit};
use crate::cruby::{vm_stack_canary, SIZEOF_VALUE_I32, VALUE, VM_ENV_DATA_SIZE};
use crate::virtualmem::CodePtr;
use crate::asm::{CodeBlock, OutlinedCb};
use crate::core::{Context, RegMapping, RegOpnd, MAX_CTX_TEMPS};
use crate::options::*;
use crate::stats::*;
use crate::backend::current::*;
pub const EC: Opnd = _EC;
pub const CFP: Opnd = _CFP;
pub const SP: Opnd = _SP;
pub const C_ARG_OPNDS: [Opnd; 6] = _C_ARG_OPNDS;
pub const C_RET_OPND: Opnd = _C_RET_OPND;
pub use crate::backend::current::{Reg, C_RET_REG};
// Memory operand base
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum MemBase
{
Reg(u8),
InsnOut(usize),
}
// Memory location
#[derive(Copy, Clone, PartialEq, Eq)]
pub struct Mem
{
// Base register number or instruction index
pub(super) base: MemBase,
// Offset relative to the base pointer
pub(super) disp: i32,
// Size in bits
pub(super) num_bits: u8,
}
impl fmt::Debug for Mem {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "Mem{}[{:?}", self.num_bits, self.base)?;
if self.disp != 0 {
let sign = if self.disp > 0 { '+' } else { '-' };
write!(fmt, " {sign} {}", self.disp)?;
}
write!(fmt, "]")
}
}
/// Operand to an IR instruction
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Opnd
{
None, // For insns with no output
// Immediate Ruby value, may be GC'd, movable
Value(VALUE),
/// C argument register. The alloc_regs resolves its register dependencies.
CArg(Reg),
// Output of a preceding instruction in this block
InsnOut{ idx: usize, num_bits: u8 },
/// Pointer to a slot on the VM stack
Stack {
/// Index from stack top. Used for conversion to StackOpnd.
idx: i32,
/// Number of bits for Opnd::Reg and Opnd::Mem.
num_bits: u8,
/// ctx.stack_size when this operand is made. Used with idx for Opnd::Reg.
stack_size: u8,
/// The number of local variables in the current ISEQ. Used only for locals.
num_locals: Option<u32>,
/// ctx.sp_offset when this operand is made. Used with idx for Opnd::Mem.
sp_offset: i8,
/// ctx.reg_mapping when this operand is read. Used for register allocation.
reg_mapping: Option<RegMapping>
},
// Low-level operands, for lowering
Imm(i64), // Raw signed immediate
UImm(u64), // Raw unsigned immediate
Mem(Mem), // Memory location
Reg(Reg), // Machine register
}
impl fmt::Debug for Opnd {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
use Opnd::*;
match self {
Self::None => write!(fmt, "None"),
Value(val) => write!(fmt, "Value({val:?})"),
CArg(reg) => write!(fmt, "CArg({reg:?})"),
Stack { idx, sp_offset, .. } => write!(fmt, "SP[{}]", *sp_offset as i32 - idx - 1),
InsnOut { idx, num_bits } => write!(fmt, "Out{num_bits}({idx})"),
Imm(signed) => write!(fmt, "{signed:x}_i64"),
UImm(unsigned) => write!(fmt, "{unsigned:x}_u64"),
// Say Mem and Reg only once
Mem(mem) => write!(fmt, "{mem:?}"),
Reg(reg) => write!(fmt, "{reg:?}"),
}
}
}
impl Opnd
{
/// Convenience constructor for memory operands
pub fn mem(num_bits: u8, base: Opnd, disp: i32) -> Self {
match base {
Opnd::Reg(base_reg) => {
assert!(base_reg.num_bits == 64);
Opnd::Mem(Mem {
base: MemBase::Reg(base_reg.reg_no),
disp: disp,
num_bits: num_bits,
})
},
Opnd::InsnOut{idx, num_bits: out_num_bits } => {
assert!(num_bits <= out_num_bits);
Opnd::Mem(Mem {
base: MemBase::InsnOut(idx),
disp: disp,
num_bits: num_bits,
})
},
_ => unreachable!("memory operand with non-register base")
}
}
/// Constructor for constant pointer operand
pub fn const_ptr(ptr: *const u8) -> Self {
Opnd::UImm(ptr as u64)
}
/// Constructor for a C argument operand
pub fn c_arg(reg_opnd: Opnd) -> Self {
match reg_opnd {
Opnd::Reg(reg) => Opnd::CArg(reg),
_ => unreachable!(),
}
}
/// Unwrap a register operand
pub fn unwrap_reg(&self) -> Reg {
match self {
Opnd::Reg(reg) => *reg,
_ => unreachable!("trying to unwrap {:?} into reg", self)
}
}
/// Get the size in bits for this operand if there is one.
pub fn num_bits(&self) -> Option<u8> {
match *self {
Opnd::Reg(Reg { num_bits, .. }) => Some(num_bits),
Opnd::Mem(Mem { num_bits, .. }) => Some(num_bits),
Opnd::InsnOut { num_bits, .. } => Some(num_bits),
_ => None
}
}
pub fn with_num_bits(&self, num_bits: u8) -> Option<Opnd> {
assert!(num_bits == 8 || num_bits == 16 || num_bits == 32 || num_bits == 64);
match *self {
Opnd::Reg(reg) => Some(Opnd::Reg(reg.with_num_bits(num_bits))),
Opnd::Mem(Mem { base, disp, .. }) => Some(Opnd::Mem(Mem { base, disp, num_bits })),
Opnd::InsnOut { idx, .. } => Some(Opnd::InsnOut { idx, num_bits }),
Opnd::Stack { idx, stack_size, num_locals, sp_offset, reg_mapping, .. } => Some(Opnd::Stack { idx, num_bits, stack_size, num_locals, sp_offset, reg_mapping }),
_ => None,
}
}
/// Get the size in bits for register/memory operands.
pub fn rm_num_bits(&self) -> u8 {
self.num_bits().unwrap()
}
/// Maps the indices from a previous list of instructions to a new list of
/// instructions.
pub fn map_index(self, indices: &Vec<usize>) -> Opnd {
match self {
Opnd::InsnOut { idx, num_bits } => {
Opnd::InsnOut { idx: indices[idx], num_bits }
}
Opnd::Mem(Mem { base: MemBase::InsnOut(idx), disp, num_bits }) => {
Opnd::Mem(Mem { base: MemBase::InsnOut(indices[idx]), disp, num_bits })
},
_ => self
}
}
/// When there aren't any operands to check against, this is the number of
/// bits that should be used for any given output variable.
const DEFAULT_NUM_BITS: u8 = 64;
/// Determine the size in bits from the iterator of operands. If any of them
/// are different sizes this will panic.
pub fn match_num_bits_iter<'a>(opnds: impl Iterator<Item = &'a Opnd>) -> u8 {
let mut value: Option<u8> = None;
for opnd in opnds {
if let Some(num_bits) = opnd.num_bits() {
match value {
None => {
value = Some(num_bits);
},
Some(value) => {
assert_eq!(value, num_bits, "operands of incompatible sizes");
}
};
}
}
value.unwrap_or(Self::DEFAULT_NUM_BITS)
}
/// Determine the size in bits of the slice of the given operands. If any of
/// them are different sizes this will panic.
pub fn match_num_bits(opnds: &[Opnd]) -> u8 {
Self::match_num_bits_iter(opnds.iter())
}
/// Convert Opnd::Stack into RegMapping
pub fn reg_opnd(&self) -> RegOpnd {
self.get_reg_opnd().unwrap()
}
/// Convert an operand into RegMapping if it's Opnd::Stack
pub fn get_reg_opnd(&self) -> Option<RegOpnd> {
match *self {
Opnd::Stack { idx, stack_size, num_locals, .. } => Some(
if let Some(num_locals) = num_locals {
let last_idx = stack_size as i32 + VM_ENV_DATA_SIZE as i32 - 1;
assert!(last_idx <= idx, "Local index {} must be >= last local index {}", idx, last_idx);
assert!(idx <= last_idx + num_locals as i32, "Local index {} must be < last local index {} + local size {}", idx, last_idx, num_locals);
RegOpnd::Local((last_idx + num_locals as i32 - idx) as u8)
} else {
assert!(idx < stack_size as i32);
RegOpnd::Stack((stack_size as i32 - idx - 1) as u8)
}
),
_ => None,
}
}
}
impl From<usize> for Opnd {
fn from(value: usize) -> Self {
Opnd::UImm(value.try_into().unwrap())
}
}
impl From<u64> for Opnd {
fn from(value: u64) -> Self {
Opnd::UImm(value)
}
}
impl From<i64> for Opnd {
fn from(value: i64) -> Self {
Opnd::Imm(value)
}
}
impl From<i32> for Opnd {
fn from(value: i32) -> Self {
Opnd::Imm(value.try_into().unwrap())
}
}
impl From<u32> for Opnd {
fn from(value: u32) -> Self {
Opnd::UImm(value as u64)
}
}
impl From<VALUE> for Opnd {
fn from(value: VALUE) -> Self {
Opnd::Value(value)
}
}
/// Branch target (something that we can jump to)
/// for branch instructions
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Target
{
/// Pointer to a piece of YJIT-generated code
CodePtr(CodePtr),
/// Side exit with a counter
SideExit { counter: Counter, context: Option<SideExitContext> },
/// Pointer to a side exit code
SideExitPtr(CodePtr),
/// A label within the generated code
Label(usize),
}
impl Target
{
pub fn side_exit(counter: Counter) -> Target {
Target::SideExit { counter, context: None }
}
pub fn unwrap_label_idx(&self) -> usize {
match self {
Target::Label(idx) => *idx,
_ => unreachable!("trying to unwrap {:?} into label", self)
}
}
pub fn unwrap_code_ptr(&self) -> CodePtr {
match self {
Target::CodePtr(ptr) => *ptr,
Target::SideExitPtr(ptr) => *ptr,
_ => unreachable!("trying to unwrap {:?} into code ptr", self)
}
}
}
impl From<CodePtr> for Target {
fn from(code_ptr: CodePtr) -> Self {
Target::CodePtr(code_ptr)
}
}
type PosMarkerFn = Box<dyn Fn(CodePtr, &CodeBlock)>;
/// YJIT IR instruction
pub enum Insn {
/// Add two operands together, and return the result as a new operand.
Add { left: Opnd, right: Opnd, out: Opnd },
/// This is the same as the OP_ADD instruction, except that it performs the
/// binary AND operation.
And { left: Opnd, right: Opnd, out: Opnd },
/// Bake a string directly into the instruction stream.
BakeString(String),
// Trigger a debugger breakpoint
#[allow(dead_code)]
Breakpoint,
/// Add a comment into the IR at the point that this instruction is added.
/// It won't have any impact on that actual compiled code.
Comment(String),
/// Compare two operands
Cmp { left: Opnd, right: Opnd },
/// Pop a register from the C stack
CPop { out: Opnd },
/// Pop all of the caller-save registers and the flags from the C stack
CPopAll,
/// Pop a register from the C stack and store it into another register
CPopInto(Opnd),
/// Push a register onto the C stack
CPush(Opnd),
/// Push all of the caller-save registers and the flags to the C stack
CPushAll,
// C function call with N arguments (variadic)
CCall { opnds: Vec<Opnd>, fptr: *const u8, out: Opnd },
// C function return
CRet(Opnd),
/// Conditionally select if equal
CSelE { truthy: Opnd, falsy: Opnd, out: Opnd },
/// Conditionally select if greater
CSelG { truthy: Opnd, falsy: Opnd, out: Opnd },
/// Conditionally select if greater or equal
CSelGE { truthy: Opnd, falsy: Opnd, out: Opnd },
/// Conditionally select if less
CSelL { truthy: Opnd, falsy: Opnd, out: Opnd },
/// Conditionally select if less or equal
CSelLE { truthy: Opnd, falsy: Opnd, out: Opnd },
/// Conditionally select if not equal
CSelNE { truthy: Opnd, falsy: Opnd, out: Opnd },
/// Conditionally select if not zero
CSelNZ { truthy: Opnd, falsy: Opnd, out: Opnd },
/// Conditionally select if zero
CSelZ { truthy: Opnd, falsy: Opnd, out: Opnd },
/// Set up the frame stack as necessary per the architecture.
FrameSetup,
/// Tear down the frame stack as necessary per the architecture.
FrameTeardown,
// Atomically increment a counter
// Input: memory operand, increment value
// Produces no output
IncrCounter { mem: Opnd, value: Opnd },
/// Jump if below or equal (unsigned)
Jbe(Target),
/// Jump if below (unsigned)
Jb(Target),
/// Jump if equal
Je(Target),
/// Jump if lower
Jl(Target),
/// Jump if greater
Jg(Target),
/// Jump if greater or equal
Jge(Target),
// Unconditional jump to a branch target
Jmp(Target),
// Unconditional jump which takes a reg/mem address operand
JmpOpnd(Opnd),
/// Jump if not equal
Jne(Target),
/// Jump if not zero
Jnz(Target),
/// Jump if overflow
Jo(Target),
/// Jump if overflow in multiplication
JoMul(Target),
/// Jump if zero
Jz(Target),
/// Jump if operand is zero (only used during lowering at the moment)
Joz(Opnd, Target),
/// Jump if operand is non-zero (only used during lowering at the moment)
Jonz(Opnd, Target),
// Add a label into the IR at the point that this instruction is added.
Label(Target),
/// Get the code address of a jump target
LeaJumpTarget { target: Target, out: Opnd },
// Load effective address
Lea { opnd: Opnd, out: Opnd },
/// Take a specific register. Signal the register allocator to not use it.
LiveReg { opnd: Opnd, out: Opnd },
// A low-level instruction that loads a value into a register.
Load { opnd: Opnd, out: Opnd },
// A low-level instruction that loads a value into a specified register.
LoadInto { dest: Opnd, opnd: Opnd },
// A low-level instruction that loads a value into a register and
// sign-extends it to a 64-bit value.
LoadSExt { opnd: Opnd, out: Opnd },
/// Shift a value left by a certain amount.
LShift { opnd: Opnd, shift: Opnd, out: Opnd },
// A low-level mov instruction. It accepts two operands.
Mov { dest: Opnd, src: Opnd },
// Perform the NOT operation on an individual operand, and return the result
// as a new operand. This operand can then be used as the operand on another
// instruction.
Not { opnd: Opnd, out: Opnd },
// This is the same as the OP_ADD instruction, except that it performs the
// binary OR operation.
Or { left: Opnd, right: Opnd, out: Opnd },
/// Pad nop instructions to accommodate Op::Jmp in case the block or the insn
/// is invalidated.
PadInvalPatch,
// Mark a position in the generated code
PosMarker(PosMarkerFn),
/// Shift a value right by a certain amount (signed).
RShift { opnd: Opnd, shift: Opnd, out: Opnd },
// Low-level instruction to store a value to memory.
Store { dest: Opnd, src: Opnd },
// This is the same as the add instruction, except for subtraction.
Sub { left: Opnd, right: Opnd, out: Opnd },
// Integer multiplication
Mul { left: Opnd, right: Opnd, out: Opnd },
// Bitwise AND test instruction
Test { left: Opnd, right: Opnd },
/// Shift a value right by a certain amount (unsigned).
URShift { opnd: Opnd, shift: Opnd, out: Opnd },
// This is the same as the OP_ADD instruction, except that it performs the
// binary XOR operation.
Xor { left: Opnd, right: Opnd, out: Opnd }
}
impl Insn {
/// Create an iterator that will yield a non-mutable reference to each
/// operand in turn for this instruction.
pub(super) fn opnd_iter(&self) -> InsnOpndIterator {
InsnOpndIterator::new(self)
}
/// Create an iterator that will yield a mutable reference to each operand
/// in turn for this instruction.
pub(super) fn opnd_iter_mut(&mut self) -> InsnOpndMutIterator {
InsnOpndMutIterator::new(self)
}
/// Get a mutable reference to a Target if it exists.
pub(super) fn target_mut(&mut self) -> Option<&mut Target> {
match self {
Insn::Jbe(target) |
Insn::Jb(target) |
Insn::Je(target) |
Insn::Jl(target) |
Insn::Jg(target) |
Insn::Jge(target) |
Insn::Jmp(target) |
Insn::Jne(target) |
Insn::Jnz(target) |
Insn::Jo(target) |
Insn::Jz(target) |
Insn::Label(target) |
Insn::JoMul(target) |
Insn::Joz(_, target) |
Insn::Jonz(_, target) |
Insn::LeaJumpTarget { target, .. } => {
Some(target)
}
_ => None,
}
}
/// Returns a string that describes which operation this instruction is
/// performing. This is used for debugging.
fn op(&self) -> &'static str {
match self {
Insn::Add { .. } => "Add",
Insn::And { .. } => "And",
Insn::BakeString(_) => "BakeString",
Insn::Breakpoint => "Breakpoint",
Insn::Comment(_) => "Comment",
Insn::Cmp { .. } => "Cmp",
Insn::CPop { .. } => "CPop",
Insn::CPopAll => "CPopAll",
Insn::CPopInto(_) => "CPopInto",
Insn::CPush(_) => "CPush",
Insn::CPushAll => "CPushAll",
Insn::CCall { .. } => "CCall",
Insn::CRet(_) => "CRet",
Insn::CSelE { .. } => "CSelE",
Insn::CSelG { .. } => "CSelG",
Insn::CSelGE { .. } => "CSelGE",
Insn::CSelL { .. } => "CSelL",
Insn::CSelLE { .. } => "CSelLE",
Insn::CSelNE { .. } => "CSelNE",
Insn::CSelNZ { .. } => "CSelNZ",
Insn::CSelZ { .. } => "CSelZ",
Insn::FrameSetup => "FrameSetup",
Insn::FrameTeardown => "FrameTeardown",
Insn::IncrCounter { .. } => "IncrCounter",
Insn::Jbe(_) => "Jbe",
Insn::Jb(_) => "Jb",
Insn::Je(_) => "Je",
Insn::Jl(_) => "Jl",
Insn::Jg(_) => "Jg",
Insn::Jge(_) => "Jge",
Insn::Jmp(_) => "Jmp",
Insn::JmpOpnd(_) => "JmpOpnd",
Insn::Jne(_) => "Jne",
Insn::Jnz(_) => "Jnz",
Insn::Jo(_) => "Jo",
Insn::JoMul(_) => "JoMul",
Insn::Jz(_) => "Jz",
Insn::Joz(..) => "Joz",
Insn::Jonz(..) => "Jonz",
Insn::Label(_) => "Label",
Insn::LeaJumpTarget { .. } => "LeaJumpTarget",
Insn::Lea { .. } => "Lea",
Insn::LiveReg { .. } => "LiveReg",
Insn::Load { .. } => "Load",
Insn::LoadInto { .. } => "LoadInto",
Insn::LoadSExt { .. } => "LoadSExt",
Insn::LShift { .. } => "LShift",
Insn::Mov { .. } => "Mov",
Insn::Not { .. } => "Not",
Insn::Or { .. } => "Or",
Insn::PadInvalPatch => "PadEntryExit",
Insn::PosMarker(_) => "PosMarker",
Insn::RShift { .. } => "RShift",
Insn::Store { .. } => "Store",
Insn::Sub { .. } => "Sub",
Insn::Mul { .. } => "Mul",
Insn::Test { .. } => "Test",
Insn::URShift { .. } => "URShift",
Insn::Xor { .. } => "Xor"
}
}
/// Return a non-mutable reference to the out operand for this instruction
/// if it has one.
pub fn out_opnd(&self) -> Option<&Opnd> {
match self {
Insn::Add { out, .. } |
Insn::And { out, .. } |
Insn::CCall { out, .. } |
Insn::CPop { out, .. } |
Insn::CSelE { out, .. } |
Insn::CSelG { out, .. } |
Insn::CSelGE { out, .. } |
Insn::CSelL { out, .. } |
Insn::CSelLE { out, .. } |
Insn::CSelNE { out, .. } |
Insn::CSelNZ { out, .. } |
Insn::CSelZ { out, .. } |
Insn::Lea { out, .. } |
Insn::LeaJumpTarget { out, .. } |
Insn::LiveReg { out, .. } |
Insn::Load { out, .. } |
Insn::LoadSExt { out, .. } |
Insn::LShift { out, .. } |
Insn::Not { out, .. } |
Insn::Or { out, .. } |
Insn::RShift { out, .. } |
Insn::Sub { out, .. } |
Insn::Mul { out, .. } |
Insn::URShift { out, .. } |
Insn::Xor { out, .. } => Some(out),
_ => None
}
}
/// Return a mutable reference to the out operand for this instruction if it
/// has one.
pub fn out_opnd_mut(&mut self) -> Option<&mut Opnd> {
match self {
Insn::Add { out, .. } |
Insn::And { out, .. } |
Insn::CCall { out, .. } |
Insn::CPop { out, .. } |
Insn::CSelE { out, .. } |
Insn::CSelG { out, .. } |
Insn::CSelGE { out, .. } |
Insn::CSelL { out, .. } |
Insn::CSelLE { out, .. } |
Insn::CSelNE { out, .. } |
Insn::CSelNZ { out, .. } |
Insn::CSelZ { out, .. } |
Insn::Lea { out, .. } |
Insn::LeaJumpTarget { out, .. } |
Insn::LiveReg { out, .. } |
Insn::Load { out, .. } |
Insn::LoadSExt { out, .. } |
Insn::LShift { out, .. } |
Insn::Not { out, .. } |
Insn::Or { out, .. } |
Insn::RShift { out, .. } |
Insn::Sub { out, .. } |
Insn::Mul { out, .. } |
Insn::URShift { out, .. } |
Insn::Xor { out, .. } => Some(out),
_ => None
}
}
/// Returns the target for this instruction if there is one.
pub fn target(&self) -> Option<&Target> {
match self {
Insn::Jbe(target) |
Insn::Jb(target) |
Insn::Je(target) |
Insn::Jl(target) |
Insn::Jg(target) |
Insn::Jge(target) |
Insn::Jmp(target) |
Insn::Jne(target) |
Insn::Jnz(target) |
Insn::Jo(target) |
Insn::Jz(target) |
Insn::LeaJumpTarget { target, .. } => Some(target),
_ => None
}
}
/// Returns the text associated with this instruction if there is some.
pub fn text(&self) -> Option<&String> {
match self {
Insn::BakeString(text) |
Insn::Comment(text) => Some(text),
_ => None
}
}
}
/// An iterator that will yield a non-mutable reference to each operand in turn
/// for the given instruction.
pub(super) struct InsnOpndIterator<'a> {
insn: &'a Insn,
idx: usize,
}
impl<'a> InsnOpndIterator<'a> {
fn new(insn: &'a Insn) -> Self {
Self { insn, idx: 0 }
}
}
impl<'a> Iterator for InsnOpndIterator<'a> {
type Item = &'a Opnd;
fn next(&mut self) -> Option<Self::Item> {
match self.insn {
Insn::BakeString(_) |
Insn::Breakpoint |
Insn::Comment(_) |
Insn::CPop { .. } |
Insn::CPopAll |
Insn::CPushAll |
Insn::FrameSetup |
Insn::FrameTeardown |
Insn::Jbe(_) |
Insn::Jb(_) |
Insn::Je(_) |
Insn::Jl(_) |
Insn::Jg(_) |
Insn::Jge(_) |
Insn::Jmp(_) |
Insn::Jne(_) |
Insn::Jnz(_) |
Insn::Jo(_) |
Insn::JoMul(_) |
Insn::Jz(_) |
Insn::Label(_) |
Insn::LeaJumpTarget { .. } |
Insn::PadInvalPatch |
Insn::PosMarker(_) => None,
Insn::CPopInto(opnd) |
Insn::CPush(opnd) |
Insn::CRet(opnd) |
Insn::JmpOpnd(opnd) |
Insn::Lea { opnd, .. } |
Insn::LiveReg { opnd, .. } |
Insn::Load { opnd, .. } |
Insn::LoadSExt { opnd, .. } |
Insn::Joz(opnd, _) |
Insn::Jonz(opnd, _) |
Insn::Not { opnd, .. } => {
match self.idx {
0 => {
self.idx += 1;
Some(&opnd)
},
_ => None
}
},
Insn::Add { left: opnd0, right: opnd1, .. } |
Insn::And { left: opnd0, right: opnd1, .. } |
Insn::Cmp { left: opnd0, right: opnd1 } |
Insn::CSelE { truthy: opnd0, falsy: opnd1, .. } |
Insn::CSelG { truthy: opnd0, falsy: opnd1, .. } |
Insn::CSelGE { truthy: opnd0, falsy: opnd1, .. } |
Insn::CSelL { truthy: opnd0, falsy: opnd1, .. } |
Insn::CSelLE { truthy: opnd0, falsy: opnd1, .. } |
Insn::CSelNE { truthy: opnd0, falsy: opnd1, .. } |
Insn::CSelNZ { truthy: opnd0, falsy: opnd1, .. } |
Insn::CSelZ { truthy: opnd0, falsy: opnd1, .. } |
Insn::IncrCounter { mem: opnd0, value: opnd1, .. } |
Insn::LoadInto { dest: opnd0, opnd: opnd1 } |
Insn::LShift { opnd: opnd0, shift: opnd1, .. } |
Insn::Mov { dest: opnd0, src: opnd1 } |
Insn::Or { left: opnd0, right: opnd1, .. } |
Insn::RShift { opnd: opnd0, shift: opnd1, .. } |
Insn::Store { dest: opnd0, src: opnd1 } |
Insn::Sub { left: opnd0, right: opnd1, .. } |
Insn::Mul { left: opnd0, right: opnd1, .. } |
Insn::Test { left: opnd0, right: opnd1 } |
Insn::URShift { opnd: opnd0, shift: opnd1, .. } |
Insn::Xor { left: opnd0, right: opnd1, .. } => {
match self.idx {
0 => {
self.idx += 1;
Some(&opnd0)
}
1 => {
self.idx += 1;
Some(&opnd1)
}
_ => None
}
},
Insn::CCall { opnds, .. } => {
if self.idx < opnds.len() {
let opnd = &opnds[self.idx];
self.idx += 1;
Some(opnd)
} else {
None
}
}
}
}
}
/// An iterator that will yield each operand in turn for the given instruction.
pub(super) struct InsnOpndMutIterator<'a> {
insn: &'a mut Insn,
idx: usize,
}
impl<'a> InsnOpndMutIterator<'a> {
fn new(insn: &'a mut Insn) -> Self {
Self { insn, idx: 0 }
}
pub(super) fn next(&mut self) -> Option<&mut Opnd> {
match self.insn {
Insn::BakeString(_) |
Insn::Breakpoint |
Insn::Comment(_) |
Insn::CPop { .. } |
Insn::CPopAll |
Insn::CPushAll |
Insn::FrameSetup |
Insn::FrameTeardown |
Insn::Jbe(_) |
Insn::Jb(_) |
Insn::Je(_) |
Insn::Jl(_) |
Insn::Jg(_) |
Insn::Jge(_) |
Insn::Jmp(_) |
Insn::Jne(_) |
Insn::Jnz(_) |
Insn::Jo(_) |
Insn::JoMul(_) |
Insn::Jz(_) |
Insn::Label(_) |
Insn::LeaJumpTarget { .. } |
Insn::PadInvalPatch |
Insn::PosMarker(_) => None,
Insn::CPopInto(opnd) |
Insn::CPush(opnd) |
Insn::CRet(opnd) |
Insn::JmpOpnd(opnd) |
Insn::Lea { opnd, .. } |
Insn::LiveReg { opnd, .. } |
Insn::Load { opnd, .. } |
Insn::LoadSExt { opnd, .. } |
Insn::Joz(opnd, _) |
Insn::Jonz(opnd, _) |
Insn::Not { opnd, .. } => {
match self.idx {
0 => {
self.idx += 1;
Some(opnd)
},
_ => None
}
},
Insn::Add { left: opnd0, right: opnd1, .. } |
Insn::And { left: opnd0, right: opnd1, .. } |
Insn::Cmp { left: opnd0, right: opnd1 } |
Insn::CSelE { truthy: opnd0, falsy: opnd1, .. } |
Insn::CSelG { truthy: opnd0, falsy: opnd1, .. } |
Insn::CSelGE { truthy: opnd0, falsy: opnd1, .. } |
Insn::CSelL { truthy: opnd0, falsy: opnd1, .. } |
Insn::CSelLE { truthy: opnd0, falsy: opnd1, .. } |
Insn::CSelNE { truthy: opnd0, falsy: opnd1, .. } |
Insn::CSelNZ { truthy: opnd0, falsy: opnd1, .. } |
Insn::CSelZ { truthy: opnd0, falsy: opnd1, .. } |
Insn::IncrCounter { mem: opnd0, value: opnd1, .. } |
Insn::LoadInto { dest: opnd0, opnd: opnd1 } |
Insn::LShift { opnd: opnd0, shift: opnd1, .. } |
Insn::Mov { dest: opnd0, src: opnd1 } |
Insn::Or { left: opnd0, right: opnd1, .. } |
Insn::RShift { opnd: opnd0, shift: opnd1, .. } |
Insn::Store { dest: opnd0, src: opnd1 } |
Insn::Sub { left: opnd0, right: opnd1, .. } |
Insn::Mul { left: opnd0, right: opnd1, .. } |
Insn::Test { left: opnd0, right: opnd1 } |
Insn::URShift { opnd: opnd0, shift: opnd1, .. } |
Insn::Xor { left: opnd0, right: opnd1, .. } => {
match self.idx {
0 => {
self.idx += 1;
Some(opnd0)
}
1 => {
self.idx += 1;
Some(opnd1)
}
_ => None
}
},
Insn::CCall { opnds, .. } => {
if self.idx < opnds.len() {
let opnd = &mut opnds[self.idx];
self.idx += 1;
Some(opnd)
} else {
None
}
}
}
}
}
impl fmt::Debug for Insn {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "{}(", self.op())?;
// Print list of operands
let mut opnd_iter = self.opnd_iter();
if let Some(first_opnd) = opnd_iter.next() {
write!(fmt, "{first_opnd:?}")?;
}
for opnd in opnd_iter {
write!(fmt, ", {opnd:?}")?;
}
write!(fmt, ")")?;
// Print text, target, and pos if they are present
if let Some(text) = self.text() {
write!(fmt, " {text:?}")?
}
if let Some(target) = self.target() {
write!(fmt, " target={target:?}")?;
}
write!(fmt, " -> {:?}", self.out_opnd().unwrap_or(&Opnd::None))
}
}
/// Set of variables used for generating side exits
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct SideExitContext {
/// PC of the instruction being compiled
pub pc: *mut VALUE,
/// Context fields used by get_generic_ctx()
pub stack_size: u8,
pub sp_offset: i8,
pub reg_mapping: RegMapping,
pub is_return_landing: bool,
pub is_deferred: bool,
}
impl SideExitContext {
/// Convert PC and Context into SideExitContext
pub fn new(pc: *mut VALUE, ctx: Context) -> Self {
let exit_ctx = SideExitContext {
pc,
stack_size: ctx.get_stack_size(),
sp_offset: ctx.get_sp_offset(),
reg_mapping: ctx.get_reg_mapping(),
is_return_landing: ctx.is_return_landing(),
is_deferred: ctx.is_deferred(),
};
if cfg!(debug_assertions) {
// Assert that we're not losing any mandatory metadata
assert_eq!(exit_ctx.get_ctx(), ctx.get_generic_ctx());
}
exit_ctx
}
/// Convert SideExitContext to Context