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
|
use crate::heap;
use crate::value::Value;
use std::error::Error;
#[derive(Debug, Clone, Copy)]
pub enum Arg {
Local(u8),
Const(Value),
}
#[derive(Debug, Clone, Copy)]
pub struct Alloc {
pub out: u8,
pub size: Arg,
}
#[derive(Debug, Clone, Copy)]
pub struct Poke {
pub offset: usize,
pub ptr: u8,
pub val: Arg,
}
#[derive(Debug, Clone, Copy)]
pub struct Peek {
pub out: u8,
pub offset: usize,
pub val: Arg,
}
#[derive(Debug, Clone, Copy)]
pub struct Shuf {
pub out: u8,
pub val: Arg,
}
#[derive(Debug, Clone, Copy)]
pub struct Exit {
pub val: Arg,
}
#[derive(Debug, Clone, Copy)]
pub struct Add {
pub out: u8,
pub val1: Arg,
pub val2: Arg,
}
#[derive(Debug, Clone, Copy)]
pub struct Sub {
pub out: u8,
pub val1: Arg,
pub val2: Arg,
}
#[derive(Debug, Clone, Copy)]
pub struct Mul {
pub out: u8,
pub val1: Arg,
pub val2: Arg,
}
#[derive(Debug, Clone, Copy)]
pub struct Div {
pub out: u8,
pub val1: Arg,
pub val2: Arg,
}
#[derive(Debug, Clone, Copy)]
pub enum Op {
Alloc(Alloc),
Call,
Poke(Poke),
Peek(Peek),
Shuf(Shuf),
Exit(Exit),
Add(Add),
Sub(Sub),
Mul(Mul),
Div(Div),
}
struct Reader<'a> {
data: &'a [u8],
n: usize,
}
impl Reader<'_> {
fn advance(&mut self, size: usize) {
self.data = &self.data[size..];
self.n += size;
}
fn parse_byte(&mut self) -> Result<u8, Box<dyn Error>> {
if self.data.is_empty() {
return Err(Box::from("read byte: no data"));
}
let res = self.data[0];
self.advance(1);
Ok(res)
}
fn parse_local(&mut self) -> Result<u8, Box<dyn Error>> {
if self.data.is_empty() {
return Err(Box::from("local: no data"));
}
let res = self.data[0];
if usize::from(res) >= heap::NUM_LOCALS {
return Err(Box::from(format!("invalid local (out of range): {}", res)));
}
self.advance(1);
Ok(res)
}
fn parse_value(&mut self) -> Result<Value, Box<dyn Error>> {
if self.data.len() < 8 {
return Err(Box::from("value: no data"));
}
let mut res = [0; 8];
res.copy_from_slice(&self.data[..8]);
self.advance(8);
Ok(Value(u64::from_le_bytes(res)))
}
fn parse_arg(&mut self, is_const: bool) -> Result<Arg, Box<dyn Error>> {
if is_const {
Ok(Arg::Const(self.parse_value()?))
} else {
Ok(Arg::Local(self.parse_local()?))
}
}
}
impl Op {
pub fn parse(data: &[u8]) -> Result<(usize, Op), Box<dyn Error>> {
let mut r = Reader { data, n: 0 };
let code_byte = r.parse_byte()?;
let code = code_byte >> 2;
let arg1_const = code_byte & 2 != 0;
let arg2_const = code_byte & 1 != 0;
let op = match code {
1 => {
let out = r.parse_local()?;
let size = r.parse_arg(arg1_const)?;
Op::Alloc(Alloc { out, size })
}
2 => Op::Call,
3 => {
let Some(ioffset) = r.parse_value()?.to_int() else {
return Err(Box::from("invalid offset"));
};
let offset = usize::try_from(ioffset)?;
let ptr = r.parse_local()?;
let val = r.parse_arg(arg1_const)?;
Op::Poke(Poke { offset, ptr, val })
}
4 => {
let out = r.parse_local()?;
let Some(ioffset) = r.parse_value()?.to_int() else {
return Err(Box::from("invalid offset"));
};
let offset = usize::try_from(ioffset)?;
let val = r.parse_arg(arg1_const)?;
Op::Peek(Peek { out, offset, val })
}
5 => {
let out = r.parse_local()?;
let val = r.parse_arg(arg1_const)?;
Op::Shuf(Shuf { out, val })
}
6 => {
let val = r.parse_arg(arg1_const)?;
Op::Exit(Exit { val })
}
7 => {
let out = r.parse_local()?;
let val1 = r.parse_arg(arg1_const)?;
let val2 = r.parse_arg(arg2_const)?;
Op::Add(Add { out, val1, val2 })
}
8 => {
let out = r.parse_local()?;
let val1 = r.parse_arg(arg1_const)?;
let val2 = r.parse_arg(arg2_const)?;
Op::Sub(Sub { out, val1, val2 })
}
9 => {
let out = r.parse_local()?;
let val1 = r.parse_arg(arg1_const)?;
let val2 = r.parse_arg(arg2_const)?;
Op::Mul(Mul { out, val1, val2 })
}
10 => {
let out = r.parse_local()?;
let val1 = r.parse_arg(arg1_const)?;
let val2 = r.parse_arg(arg2_const)?;
Op::Div(Div { out, val1, val2 })
}
_ => {
return Err(Box::from(format!("invalid code {}", code)));
}
};
Ok((r.n, op))
}
}
|