-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast_interpreter.rs
141 lines (136 loc) · 4.51 KB
/
ast_interpreter.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
use ckb_vm::{
instructions::ast::{ActionOp1, ActionOp2, SignActionOp2, Value},
Error, Machine, Memory, Register, RISCV_GENERAL_REGISTER_NUMBER,
};
pub const PC_INDEX: usize = 0xFFFF;
/// An interpreter function for CKB-VM's AST value, which might be super
/// helpful in terms of debugging.
pub fn interpret<Mac: Machine>(value: &Value, machine: &mut Mac) -> Result<Mac::REG, Error> {
match value {
Value::Imm(imm) => Ok(Mac::REG::from_u64(*imm)),
Value::Register(index) => {
if *index < RISCV_GENERAL_REGISTER_NUMBER {
Ok(machine.registers()[*index].clone())
} else if *index == PC_INDEX {
Ok(machine.pc().clone())
} else {
Err(Error::External(format!(
"Invalid register index: {}",
index
)))
}
}
Value::Op1(op, v) => interpret_op1::<Mac>(op, interpret(v, machine)?),
Value::Op2(op, lhs, rhs) => {
interpret_op2::<Mac>(op, interpret(lhs, machine)?, interpret(rhs, machine)?)
}
Value::SignOp2(op, lhs, rhs, signed) => interpret_sign_op2::<Mac>(
op,
interpret(lhs, machine)?,
interpret(rhs, machine)?,
*signed,
),
Value::Cond(c, t, f) => {
let c = interpret(c, machine)?;
if c.to_u64() == Mac::REG::one().to_u64() {
interpret(t, machine)
} else {
interpret(f, machine)
}
}
Value::Load(addr, size) => {
let addr = interpret(addr, machine)?;
match size {
1 => machine.memory_mut().load8(&addr),
2 => machine.memory_mut().load16(&addr),
4 => machine.memory_mut().load32(&addr),
8 => machine.memory_mut().load64(&addr),
_ => Err(Error::External(format!("Invalid load size: {}", size))),
}
}
}
}
fn interpret_op1<Mac: Machine>(op: &ActionOp1, value: Mac::REG) -> Result<Mac::REG, Error> {
match op {
ActionOp1::Not => Ok(!value),
ActionOp1::LogicalNot => Ok(value.logical_not()),
ActionOp1::Clz => Ok(value.clz()),
ActionOp1::Ctz => Ok(value.ctz()),
ActionOp1::Cpop => Ok(value.cpop()),
ActionOp1::Orcb => Ok(value.orcb()),
ActionOp1::Rev8 => Ok(value.rev8()),
}
}
fn interpret_op2<Mac: Machine>(
op: &ActionOp2,
lhs: Mac::REG,
rhs: Mac::REG,
) -> Result<Mac::REG, Error> {
match op {
ActionOp2::Add => Ok(lhs.overflowing_add(&rhs)),
ActionOp2::Sub => Ok(lhs.overflowing_sub(&rhs)),
ActionOp2::Mul => Ok(lhs.overflowing_mul(&rhs)),
ActionOp2::Mulhsu => Ok(lhs.overflowing_mul_high_signed_unsigned(&rhs)),
ActionOp2::Bitand => Ok(lhs & rhs),
ActionOp2::Bitor => Ok(lhs | rhs),
ActionOp2::Bitxor => Ok(lhs ^ rhs),
ActionOp2::Shl => Ok(lhs << rhs),
ActionOp2::Eq => Ok(lhs.eq(&rhs)),
ActionOp2::Clmul => Ok(lhs.clmul(&rhs)),
ActionOp2::Clmulh => Ok(lhs.clmulh(&rhs)),
ActionOp2::Clmulr => Ok(lhs.clmulr(&rhs)),
ActionOp2::Rol => Ok(lhs.rol(&rhs)),
ActionOp2::Ror => Ok(lhs.ror(&rhs)),
}
}
fn interpret_sign_op2<Mac: Machine>(
op: &SignActionOp2,
lhs: Mac::REG,
rhs: Mac::REG,
signed: bool,
) -> Result<Mac::REG, Error> {
match op {
SignActionOp2::Mulh => {
if signed {
Ok(lhs.overflowing_mul_high_signed(&rhs))
} else {
Ok(lhs.overflowing_mul_high_unsigned(&rhs))
}
}
SignActionOp2::Div => {
if signed {
Ok(lhs.overflowing_div_signed(&rhs))
} else {
Ok(lhs.overflowing_div(&rhs))
}
}
SignActionOp2::Rem => {
if signed {
Ok(lhs.overflowing_rem_signed(&rhs))
} else {
Ok(lhs.overflowing_rem(&rhs))
}
}
SignActionOp2::Shr => {
if signed {
Ok(lhs.signed_shr(&rhs))
} else {
Ok(lhs >> rhs)
}
}
SignActionOp2::Lt => {
if signed {
Ok(lhs.lt_s(&rhs))
} else {
Ok(lhs.lt(&rhs))
}
}
SignActionOp2::Extend => {
if signed {
Ok(lhs.sign_extend(&rhs))
} else {
Ok(lhs.zero_extend(&rhs))
}
}
}
}