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
//! MIR basic blocks.
use super::{BlockId, InstId, ValueId};
use smallvec::SmallVec;
use std::fmt;
/// A basic block in the MIR.
#[derive(Clone, Debug)]
pub struct BasicBlock {
/// The instructions in this block (excluding the terminator).
pub instructions: Vec<InstId>,
/// The terminator instruction.
pub terminator: Option<Terminator>,
/// Predecessor blocks.
pub predecessors: SmallVec<[BlockId; 4]>,
}
impl BasicBlock {
/// Creates a new empty basic block.
#[must_use]
pub fn new() -> Self {
Self { instructions: Vec::new(), terminator: None, predecessors: SmallVec::new() }
}
/// Returns true if this block has a terminator.
#[must_use]
pub const fn is_terminated(&self) -> bool {
self.terminator.is_some()
}
/// Returns the terminator, if present.
#[must_use]
pub const fn terminator(&self) -> Option<&Terminator> {
self.terminator.as_ref()
}
}
impl Default for BasicBlock {
fn default() -> Self {
Self::new()
}
}
/// A block terminator instruction.
#[derive(Clone, Debug, PartialEq)]
pub enum Terminator {
/// Unconditional jump to another block.
Jump(BlockId),
/// Conditional branch.
Branch {
/// The condition value (must be boolean).
condition: ValueId,
/// The block to jump to if true.
then_block: BlockId,
/// The block to jump to if false.
else_block: BlockId,
},
/// Multi-way switch.
Switch {
/// The value to switch on.
value: ValueId,
/// The default block.
default: BlockId,
/// The cases: (value, block).
cases: Vec<(ValueId, BlockId)>,
},
/// Return from function.
Return {
/// The return values.
values: SmallVec<[ValueId; 2]>,
},
/// Revert execution.
Revert {
/// Memory offset of revert data.
offset: ValueId,
/// Size of revert data.
size: ValueId,
},
/// Return raw, already-encoded data: `RETURN(offset, size)`. Used for
/// ABI-encoded external returns whose size is computed at runtime.
ReturnData {
/// Memory offset of the return data.
offset: ValueId,
/// Size of the return data in bytes.
size: ValueId,
},
/// Stop execution.
Stop,
/// Self-destruct the contract.
SelfDestruct {
/// The address to send remaining funds to.
recipient: ValueId,
},
/// Invalid operation (unreachable code).
Invalid,
}
impl Terminator {
/// Returns the successor blocks of this terminator.
#[must_use]
pub fn successors(&self) -> SmallVec<[BlockId; 2]> {
match self {
Self::Jump(target) => smallvec::smallvec![*target],
Self::Branch { then_block, else_block, .. } => {
smallvec::smallvec![*then_block, *else_block]
}
Self::Switch { default, cases, .. } => {
let mut succs = SmallVec::with_capacity(cases.len() + 1);
succs.push(*default);
for (_, block) in cases {
succs.push(*block);
}
succs
}
Self::Return { .. }
| Self::Revert { .. }
| Self::ReturnData { .. }
| Self::Stop
| Self::SelfDestruct { .. }
| Self::Invalid => SmallVec::new(),
}
}
/// Returns the mnemonic for this terminator.
#[must_use]
pub const fn mnemonic(&self) -> &'static str {
match self {
Self::Jump(_) => "jump",
Self::Branch { .. } => "branch",
Self::Switch { .. } => "switch",
Self::Return { .. } => "return",
Self::Revert { .. } => "revert",
Self::ReturnData { .. } => "returndata",
Self::Stop => "stop",
Self::SelfDestruct { .. } => "selfdestruct",
Self::Invalid => "invalid",
}
}
/// Returns the [`ValueId`] operands of this terminator (the values it reads).
/// Block targets are NOT included; use [`Self::successors`] for those.
#[must_use]
pub fn operands(&self) -> SmallVec<[ValueId; 4]> {
let mut out = SmallVec::new();
match self {
Self::Jump(_) => {}
Self::Branch { condition, .. } => out.push(*condition),
Self::Switch { value, cases, .. } => {
out.push(*value);
for (case_val, _) in cases {
out.push(*case_val);
}
}
Self::Return { values } => out.extend(values.iter().copied()),
Self::Revert { offset, size } | Self::ReturnData { offset, size } => {
out.push(*offset);
out.push(*size);
}
Self::Stop | Self::Invalid => {}
Self::SelfDestruct { recipient } => out.push(*recipient),
}
out
}
}
impl fmt::Display for Terminator {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Jump(target) => write!(f, "jump bb{}", target.index()),
Self::Branch { condition, then_block, else_block } => {
write!(
f,
"branch v{}, bb{}, bb{}",
condition.index(),
then_block.index(),
else_block.index()
)
}
Self::Switch { value, default, cases } => {
write!(f, "switch v{}, default bb{}", value.index(), default.index())?;
for (val, block) in cases {
write!(f, ", v{} => bb{}", val.index(), block.index())?;
}
Ok(())
}
Self::Return { values } => {
write!(f, "return")?;
for (i, v) in values.iter().enumerate() {
if i > 0 {
write!(f, ",")?;
}
write!(f, " v{}", v.index())?;
}
Ok(())
}
Self::Revert { offset, size } => {
write!(f, "revert v{}, v{}", offset.index(), size.index())
}
Self::ReturnData { offset, size } => {
write!(f, "returndata v{}, v{}", offset.index(), size.index())
}
Self::Stop => write!(f, "stop"),
Self::SelfDestruct { recipient } => {
write!(f, "selfdestruct v{}", recipient.index())
}
Self::Invalid => write!(f, "invalid"),
}
}
}