-
Notifications
You must be signed in to change notification settings - Fork 1
/
closure_tree.rs
215 lines (188 loc) · 5.34 KB
/
closure_tree.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
#![allow(dead_code)]
#[cfg(test)]
use crate::benchmark;
use super::{handler, Bits, Context, Outcome};
#[derive(Copy, Clone)]
pub struct Register(usize);
/// A closure based expression.
pub struct Expr {
handler: Box<dyn Fn(&mut Context) -> Bits>,
}
pub enum Input {
Register(Register),
Immediate(Bits),
Expr(Expr),
}
impl From<Register> for Input {
fn from(register: Register) -> Self {
Self::Register(register)
}
}
impl From<Bits> for Input {
fn from(bits: Bits) -> Self {
Self::Immediate(bits)
}
}
impl From<Expr> for Input {
fn from(expr: Expr) -> Self {
Self::Expr(expr)
}
}
impl Expr {
/// Executes the given expression using the given [`Context`].
pub fn execute(&self, context: &mut Context) -> Bits {
(self.handler)(context)
}
/// Creates a new [`Inst`] from the given closure.
fn new<T>(handler: T) -> Self
where
T: Fn(&mut Context) -> Bits + 'static,
{
Self {
handler: Box::new(handler),
}
}
pub fn add<P0, P1>(result: Register, lhs: P0, rhs: P1) -> Self
where
P0: Eval + 'static,
P1: Eval + 'static,
{
Self::new(move |context| {
let lhs = lhs.eval(context);
let rhs = rhs.eval(context);
let new_value = lhs.wrapping_add(rhs);
context.set_reg(result.0, new_value);
new_value
})
}
pub fn sub<P0, P1>(result: Register, lhs: P0, rhs: P1) -> Self
where
P0: Eval + 'static,
P1: Eval + 'static,
{
Self::new(move |context| {
let lhs = lhs.eval(context);
let rhs = rhs.eval(context);
let new_value = lhs.wrapping_sub(rhs);
context.set_reg(result.0, new_value);
new_value
})
}
pub fn mul<P0, P1>(result: Register, lhs: P0, rhs: P1) -> Self
where
P0: Eval + 'static,
P1: Eval + 'static,
{
Self::new(move |context| {
let lhs = lhs.eval(context);
let rhs = rhs.eval(context);
let new_value = lhs.wrapping_mul(rhs);
context.set_reg(result.0, new_value);
new_value
})
}
}
/// A closure based instruction.
pub struct Inst {
/// The closure stores everything required for the instruction execution.
handler: Box<dyn Fn(&mut Context) -> Outcome>,
}
pub trait Eval {
fn eval(&self, context: &mut Context) -> Bits;
}
impl Eval for Register {
fn eval(&self, context: &mut Context) -> Bits {
context.get_reg(self.0)
}
}
impl Eval for Bits {
fn eval(&self, context: &mut Context) -> Bits {
*self
}
}
impl Eval for Expr {
fn eval(&self, context: &mut Context) -> Bits {
self.execute(context)
}
}
impl Inst {
/// Executes the given instruction using the given [`Context`].
pub fn execute(&self, context: &mut Context) -> Outcome {
(self.handler)(context)
}
/// Creates a new [`Inst`] from the given closure.
fn new<T>(handler: T) -> Self
where
T: Fn(&mut Context) -> Outcome + 'static,
{
Self {
handler: Box::new(handler),
}
}
pub fn exec(expr: Expr) -> Self {
Self::new(move |context| {
expr.eval(context);
Outcome::Continue
})
}
pub fn local_set<I>(result: Register, input: I) -> Self
where
I: Eval + 'static,
{
Self::new(move |context| {
let new_value = input.eval(context);
context.set_reg(result.0, new_value);
Outcome::Continue
})
}
/// Branches to the instruction indexed by `target` if the contents of `condition` are zero.
pub fn branch_eqz(condition: Expr) -> Self {
Self::new(move |context| {
let condition = condition.execute(context);
if condition == 0 {
Outcome::Return
} else {
Outcome::Continue
}
})
}
/// Executes all the instructions in the basic block one after another.
pub fn basic_block<I>(insts: I) -> Self
where
I: IntoIterator<Item = Inst>,
{
let insts = insts.into_iter().collect::<Vec<_>>().into_boxed_slice();
Self::new(move |context| {
for inst in &insts[..] {
match inst.execute(context) {
Outcome::Continue => (),
Outcome::Return => return Outcome::Return,
}
}
Outcome::Continue
})
}
/// Loops the body until it returns.
pub fn loop_block(body: Inst) -> Self {
Self::new(move |context| loop {
match body.execute(context) {
Outcome::Continue => (),
Outcome::Return => return Outcome::Return,
}
})
}
/// Returns execution of the function and returns the result in `result`.
pub fn ret(result: Register) -> Self {
Self::new(move |context| handler::ret(context, result.0))
}
}
#[test]
fn counter_loop() {
let repetitions = 100_000_000;
let inst = Inst::basic_block(vec![
Inst::exec(Expr::add(Register(0), Register(0), repetitions)),
Inst::loop_block(Inst::branch_eqz(Expr::sub(Register(0), Register(0), 1))),
]);
let mut context = Context::default();
benchmark(|| inst.execute(&mut context));
}