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
use std::collections::BTreeMap;
use move_binary_format::errors::{Location, PartialVMError, VMError};
use move_core_types::vm_status::StatusCode;
use move_model::ast::TempIndex;
use crate::concrete::{
ty::{CodeOffset, Type},
value::{LocalSlot, Pointer, TypedValue},
};
#[derive(Clone, Debug)]
pub enum AbortInfo {
User(u64, Location),
Internal(StatusCode, Location),
}
impl AbortInfo {
pub fn into_err(self) -> VMError {
match self {
Self::User(status_code, location) => PartialVMError::new(StatusCode::ABORTED)
.with_sub_status(status_code)
.finish(location),
Self::Internal(status_code, location) => {
PartialVMError::new(status_code).finish(location)
}
}
}
pub fn get_status_code(&self) -> u64 {
match self {
Self::User(status_code, _) => *status_code,
Self::Internal(status_code, _) => *status_code as u64,
}
}
}
#[derive(Debug)]
pub enum TerminationStatus {
None,
PostAbort(AbortInfo),
Return(Vec<TypedValue>),
Abort(AbortInfo),
}
pub struct LocalState {
slots: Vec<LocalSlot>,
pc: CodeOffset,
pc_branch: bool,
termination: TerminationStatus,
destroyed_args: BTreeMap<TempIndex, TypedValue>,
}
impl LocalState {
pub fn new(slots: Vec<LocalSlot>) -> Self {
Self {
slots,
pc: 0,
pc_branch: false,
termination: TerminationStatus::None,
destroyed_args: BTreeMap::new(),
}
}
pub fn num_slots(&self) -> usize {
self.slots.len()
}
pub fn get_type(&self, index: TempIndex) -> &Type {
self.slots.get(index).unwrap().get_type()
}
pub fn has_value(&self, index: TempIndex) -> bool {
self.slots.get(index).unwrap().has_value()
}
pub fn get_value(&self, index: TempIndex) -> TypedValue {
self.slots.get(index).unwrap().get_value()
}
pub fn put_value_override(&mut self, index: TempIndex, val: TypedValue) {
self.slots.get_mut(index).unwrap().put_value_override(val)
}
pub fn put_value(&mut self, index: TempIndex, val: TypedValue) {
self.slots.get_mut(index).unwrap().put_value(val)
}
pub fn del_value(&mut self, index: TempIndex) -> TypedValue {
self.slots.get_mut(index).unwrap().del_value()
}
pub fn save_destroyed_arg(&mut self, index: TempIndex, val: TypedValue) {
let exists = self.destroyed_args.insert(index, val);
if cfg!(debug_assertions) {
assert!(exists.is_none());
}
}
pub fn load_destroyed_arg(&mut self, index: TempIndex) -> TypedValue {
self.destroyed_args.remove(&index).unwrap()
}
pub fn get_pc(&self) -> CodeOffset {
self.pc
}
pub fn set_pc(&mut self, pc: CodeOffset) {
if cfg!(debug_assertions) {
assert!(!self.pc_branch);
}
self.pc = pc;
self.pc_branch = true;
}
pub fn ready_pc_for_next_instruction(&mut self) {
if self.pc_branch {
self.pc_branch = false
} else {
self.pc += 1;
}
}
pub fn collect_pointers(&self) -> BTreeMap<TempIndex, &Pointer> {
self.slots
.iter()
.enumerate()
.filter_map(|(idx, slot)| slot.get_content().map(|(_, ptr)| (idx, ptr)))
.collect()
}
pub fn transit_to_post_abort(&mut self, info: AbortInfo) {
if cfg!(debug_assertions) {
assert!(matches!(self.termination, TerminationStatus::None));
}
self.termination = TerminationStatus::PostAbort(info);
}
pub fn is_terminated(&self) -> bool {
matches!(
self.termination,
TerminationStatus::Return(_) | TerminationStatus::Abort(_)
)
}
pub fn is_post_abort(&self) -> bool {
matches!(self.termination, TerminationStatus::PostAbort(_))
}
pub fn terminate_with_abort(&mut self, abort_info: AbortInfo) {
if cfg!(debug_assertions) {
assert!(!self.is_terminated());
}
let info = match &self.termination {
TerminationStatus::None => {
abort_info
}
TerminationStatus::PostAbort(original_info) => {
if cfg!(debug_assertions) {
assert_eq!(
original_info.get_status_code(),
abort_info.get_status_code()
);
}
original_info.clone()
}
_ => unreachable!(),
};
self.termination = TerminationStatus::Abort(info);
}
pub fn terminate_with_return(&mut self, ret_vals: Vec<TypedValue>) {
if cfg!(debug_assertions) {
assert!(!self.is_terminated());
}
self.termination = TerminationStatus::Return(ret_vals);
}
pub fn into_termination_status(self) -> TerminationStatus {
self.termination
}
}