-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathlib.rs
More file actions
442 lines (409 loc) · 14.8 KB
/
Copy pathlib.rs
File metadata and controls
442 lines (409 loc) · 14.8 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
//! Tape bytecode format
//!
//! Fidget's bytecode is a packed representation of a
//! [`RegTape`](fidget_core::compiler::RegTape). It may be used as the
//! evaluation tape for non-Rust VMs, e.g. an interpreter running on a GPU.
//!
//! The format is **not stable**; it may change without notice. It would be
//! wise to dynamically check any interpreter against [`iter_ops`], which
//! associates opcode integers with their names.
//!
//! The bytecode format is a list of little-endian `u32` words, representing
//! tape operations in forward-evaluation order. Each operation in the tape maps
//! to two words, though the second word is not always used. Having a
//! fixed-length representation makes it easier to iterate both forwards (for
//! evaluation) and backwards (for simplification).
//!
//! The first two words are always `0xFFFF_FFFF 0x0000_0000`, and the last two
//! words are always `0xFFFF_FFFF 0xFFFF_FFFF`. Note that this is equivalent to
//! an operation with opcode `0xFF`; this special opcode may also be used with
//! user-defined semantics, as long as the immediate is not either reserved
//! value.
//!
//! Operations are packed into the first `u32` as follows:
//!
//! | Byte | Value |
//! |------|---------------------------------------------|
//! | 0 | opcode |
//! | 1 | output register |
//! | 2 | first input register |
//! | 3 | second input register |
//!
//! The opcode byte is generated automatically from [`BytecodeOp`] tags.
//!
//! Depending on the opcode, the input register bytes may not be used.
//!
//! An input register byte of `0xFF` indicates that the second word should be
//! used as an immediate value; the `u32` should be bitcast to an `f32`.
//!
//! [`Load`](RegOp::Load) and [`Store`](RegOp::Store) are implemented with
//! [`BytecodeOp::Mem`], using the immediate flag `0xFF` to indicate whether the
//! operation reads or writes to memory. The second word is the `u32` immediate
//! representing a memory slot.
#![warn(missing_docs)]
use fidget_core::{compiler::RegOp, vm::VmData};
use zerocopy::IntoBytes;
/// Error type for bytecode builder
#[derive(thiserror::Error, Debug, PartialEq)]
#[error("register 255 is reserved")]
pub struct ReservedRegister;
/// Operations in the bytecode tape
#[derive(
Copy,
Clone,
Debug,
PartialEq,
serde::Serialize,
serde::Deserialize,
strum::EnumIter,
strum::EnumCount,
strum::IntoStaticStr,
strum::FromRepr,
)]
#[expect(missing_docs)]
#[repr(u8)]
pub enum BytecodeOp {
Output,
Input,
Copy,
Neg,
Abs,
Recip,
Sqrt,
Square,
Floor,
Ceil,
Round,
Not,
Sin,
Cos,
Tan,
Asin,
Acos,
Atan,
Exp,
Ln,
Add,
Sub,
Mul,
Div,
Atan2,
Compare,
Mod,
Min,
Max,
And,
Or,
Mem,
}
impl From<RegOp> for BytecodeOp {
fn from(op: RegOp) -> Self {
match op {
RegOp::Input(..) => BytecodeOp::Input,
RegOp::Output(..) => BytecodeOp::Output,
RegOp::NegReg(..) => BytecodeOp::Neg,
RegOp::AbsReg(..) => BytecodeOp::Abs,
RegOp::RecipReg(..) => BytecodeOp::Recip,
RegOp::SqrtReg(..) => BytecodeOp::Sqrt,
RegOp::SquareReg(..) => BytecodeOp::Square,
RegOp::FloorReg(..) => BytecodeOp::Floor,
RegOp::CeilReg(..) => BytecodeOp::Ceil,
RegOp::RoundReg(..) => BytecodeOp::Round,
RegOp::SinReg(..) => BytecodeOp::Sin,
RegOp::CosReg(..) => BytecodeOp::Cos,
RegOp::TanReg(..) => BytecodeOp::Tan,
RegOp::AsinReg(..) => BytecodeOp::Asin,
RegOp::AcosReg(..) => BytecodeOp::Acos,
RegOp::AtanReg(..) => BytecodeOp::Atan,
RegOp::ExpReg(..) => BytecodeOp::Exp,
RegOp::LnReg(..) => BytecodeOp::Ln,
RegOp::NotReg(..) => BytecodeOp::Not,
RegOp::Load(..) | RegOp::Store(..) => BytecodeOp::Mem,
RegOp::CopyImm(..) | RegOp::CopyReg(..) => BytecodeOp::Copy,
RegOp::AddRegReg(..) | RegOp::AddRegImm(..) => BytecodeOp::Add,
RegOp::MulRegReg(..) | RegOp::MulRegImm(..) => BytecodeOp::Mul,
RegOp::DivRegReg(..)
| RegOp::DivRegImm(..)
| RegOp::DivImmReg(..) => BytecodeOp::Div,
RegOp::SubRegReg(..)
| RegOp::SubRegImm(..)
| RegOp::SubImmReg(..) => BytecodeOp::Sub,
RegOp::AtanRegReg(..)
| RegOp::AtanRegImm(..)
| RegOp::AtanImmReg(..) => BytecodeOp::Atan2,
RegOp::MinRegReg(..) | RegOp::MinRegImm(..) => BytecodeOp::Min,
RegOp::MaxRegReg(..) | RegOp::MaxRegImm(..) => BytecodeOp::Max,
RegOp::CompareRegReg(..)
| RegOp::CompareRegImm(..)
| RegOp::CompareImmReg(..) => BytecodeOp::Compare,
RegOp::ModRegReg(..)
| RegOp::ModRegImm(..)
| RegOp::ModImmReg(..) => BytecodeOp::Mod,
RegOp::AndRegReg(..) | RegOp::AndRegImm(..) => BytecodeOp::And,
RegOp::OrRegReg(..) | RegOp::OrRegImm(..) => BytecodeOp::Or,
}
}
}
/// Serialized bytecode for external evaluation
pub struct Bytecode {
reg_count: u8,
mem_count: u32,
data: Vec<u32>,
}
impl Bytecode {
/// Returns the length of the bytecode data (in `u32` words)
#[allow(clippy::len_without_is_empty)]
pub fn len(&self) -> usize {
self.data.len()
}
/// Raw serialized operations
pub fn data(&self) -> &[u32] {
&self.data
}
/// Number of registers (0-indexed) used by the tape
///
/// This does not include the virtual register `0xFF` used for immediates
pub fn reg_count(&self) -> u8 {
self.reg_count
}
/// Number of memory slots (0-indexed) used for `Load` / `Store` operations
pub fn mem_count(&self) -> u32 {
self.mem_count
}
/// Returns a view of the byte slice
pub fn as_bytes(&self) -> &[u8] {
self.data.as_bytes()
}
/// Builds a new bytecode object from VM data
///
/// Registers are reordered by frequency of use, e.g. the most frequently
/// used register becomes register 0.
///
/// Returns an error if the reserved register (255) is in use, which should
/// only happen if the incoming tape has 256 active registers.
pub fn new<const N: usize>(
t: &VmData<N>,
) -> Result<Self, ReservedRegister> {
// Build a map for repacking registers by frequency
let map = t.asm().repack_map();
// The initial opcode is `OP_JUMP 0x0000_0000`
let mut data = vec![u32::MAX, 0u32];
let mut reg_count = 0u8;
let mut mem_count = 0u32;
let mem_offset = u32::try_from(N).unwrap();
for op in t.iter_asm() {
let mut word = [0xFF; 4];
let mut imm = None;
let mut store_reg = |i, r| {
let r = map[&r];
if r == u8::MAX {
Err(ReservedRegister)
} else {
reg_count = reg_count.max(r + 1);
word[i] = r;
Ok(())
}
};
match op {
RegOp::Input(reg, slot) | RegOp::Output(reg, slot) => {
store_reg(1, reg)?;
imm = Some(slot);
}
RegOp::Load(reg, slot) => {
store_reg(1, reg)?;
word[2] = u8::MAX;
mem_count = mem_count.max(slot + 1 - mem_offset);
imm = Some(slot - mem_offset);
}
RegOp::Store(reg, slot) => {
store_reg(2, reg)?;
word[1] = u8::MAX;
mem_count = mem_count.max(slot + 1 - mem_offset);
imm = Some(slot - mem_offset);
}
RegOp::CopyImm(out, imm_f32) => {
store_reg(1, out)?;
word[2] = u8::MAX;
imm = Some(imm_f32.to_bits());
}
RegOp::NegReg(out, reg)
| RegOp::AbsReg(out, reg)
| RegOp::RecipReg(out, reg)
| RegOp::SqrtReg(out, reg)
| RegOp::SquareReg(out, reg)
| RegOp::FloorReg(out, reg)
| RegOp::CeilReg(out, reg)
| RegOp::RoundReg(out, reg)
| RegOp::CopyReg(out, reg)
| RegOp::SinReg(out, reg)
| RegOp::CosReg(out, reg)
| RegOp::TanReg(out, reg)
| RegOp::AsinReg(out, reg)
| RegOp::AcosReg(out, reg)
| RegOp::AtanReg(out, reg)
| RegOp::ExpReg(out, reg)
| RegOp::LnReg(out, reg)
| RegOp::NotReg(out, reg) => {
store_reg(1, out)?;
store_reg(2, reg)?;
}
RegOp::AddRegImm(out, reg, imm_f32)
| RegOp::MulRegImm(out, reg, imm_f32)
| RegOp::DivRegImm(out, reg, imm_f32)
| RegOp::SubRegImm(out, reg, imm_f32)
| RegOp::AtanRegImm(out, reg, imm_f32)
| RegOp::MinRegImm(out, reg, imm_f32)
| RegOp::MaxRegImm(out, reg, imm_f32)
| RegOp::CompareRegImm(out, reg, imm_f32)
| RegOp::ModRegImm(out, reg, imm_f32)
| RegOp::AndRegImm(out, reg, imm_f32)
| RegOp::OrRegImm(out, reg, imm_f32) => {
store_reg(1, out)?;
store_reg(2, reg)?;
word[3] = u8::MAX;
imm = Some(imm_f32.to_bits());
}
RegOp::DivImmReg(out, reg, imm_f32)
| RegOp::SubImmReg(out, reg, imm_f32)
| RegOp::AtanImmReg(out, reg, imm_f32)
| RegOp::CompareImmReg(out, reg, imm_f32)
| RegOp::ModImmReg(out, reg, imm_f32) => {
store_reg(1, out)?;
store_reg(3, reg)?;
word[2] = u8::MAX;
imm = Some(imm_f32.to_bits());
}
RegOp::AddRegReg(out, lhs, rhs)
| RegOp::MulRegReg(out, lhs, rhs)
| RegOp::DivRegReg(out, lhs, rhs)
| RegOp::SubRegReg(out, lhs, rhs)
| RegOp::AtanRegReg(out, lhs, rhs)
| RegOp::MinRegReg(out, lhs, rhs)
| RegOp::MaxRegReg(out, lhs, rhs)
| RegOp::CompareRegReg(out, lhs, rhs)
| RegOp::ModRegReg(out, lhs, rhs)
| RegOp::AndRegReg(out, lhs, rhs)
| RegOp::OrRegReg(out, lhs, rhs) => {
store_reg(1, out)?;
store_reg(2, lhs)?;
store_reg(3, rhs)?;
}
};
word[0] = BytecodeOp::from(op) as u8;
data.push(u32::from_le_bytes(word));
data.push(imm.unwrap_or(0xFF000000));
}
// Add the final `OP_JUMP 0xFFFF_FFFF`
data.extend([u32::MAX, u32::MAX]);
Ok(Bytecode {
data,
mem_count,
reg_count,
})
}
}
/// Iterates over opcode `(names, value)` tuples, with names in `CamelCase`
///
/// This is a helper function for defining constants in a VM interpreter
pub fn iter_ops<'a>() -> impl Iterator<Item = (&'a str, u8)> {
use strum::IntoEnumIterator;
BytecodeOp::iter().enumerate().map(|(i, op)| {
let s: &'static str = op.into();
(s, i as u8)
})
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn simple_bytecode() {
let mut ctx = fidget_core::Context::new();
let x = ctx.x();
let c = ctx.constant(1.0);
let out = ctx.add(x, c).unwrap();
let data = VmData::<255>::new(&ctx, &[out]).unwrap();
let bc = Bytecode::new(&data).unwrap();
let mut iter = bc.data.iter();
let mut next = || *iter.next().unwrap();
assert_eq!(next(), 0xFFFFFFFF); // start marker
assert_eq!(next(), 0);
assert_eq!(
next().to_le_bytes(),
[BytecodeOp::Input as u8, 0, 0xFF, 0xFF]
);
assert_eq!(next(), 0); // input slot 0
assert_eq!(next().to_le_bytes(), [BytecodeOp::Add as u8, 0, 0, 0xFF]);
assert_eq!(f32::from_bits(next()), 1.0);
assert_eq!(
next().to_le_bytes(),
[BytecodeOp::Output as u8, 0, 0xFF, 0xFF]
);
assert_eq!(next(), 0); // output slot 0
assert_eq!(next(), 0xFFFFFFFF); // end marker
assert_eq!(next(), 0xFFFFFFFF);
assert!(iter.next().is_none());
}
#[test]
fn load_store() {
// Build a bytecode tape with only 2 registers to test load and store
let mut ctx = fidget_core::Context::new();
let x = ctx.x();
let y = ctx.y();
let z = ctx.z();
let xy = ctx.max(x, y).unwrap();
let out = ctx.max(xy, z).unwrap();
let data = VmData::<2>::new(&ctx, &[out]).unwrap();
let bc = Bytecode::new(&data).unwrap();
assert_eq!(bc.reg_count(), 2);
assert_eq!(bc.mem_count(), 1);
let mut iter = bc.data.iter();
let mut next = || *iter.next().unwrap();
assert_eq!(next(), 0xFFFFFFFF); // start marker
assert_eq!(next(), 0);
// Input(1, Z)
assert_eq!(
next().to_le_bytes(),
[BytecodeOp::Input as u8, 1, 0xFF, 0xFF]
);
assert_eq!(next(), 2); // Z
// Copy from reg[1] -> mem[0]
assert_eq!(
next().to_le_bytes(),
[BytecodeOp::Mem as u8, 0xFF, 1, 0xFF]
);
assert_eq!(next(), 0);
// Input(1, Y)
assert_eq!(
next().to_le_bytes(),
[BytecodeOp::Input as u8, 1, 0xFF, 0xFF]
);
assert_eq!(next(), 1); // Y
// Input(0, X)
assert_eq!(
next().to_le_bytes(),
[BytecodeOp::Input as u8, 0, 0xFF, 0xFF]
);
assert_eq!(next(), 0); // X
// r1 = max(1, 0)
assert_eq!(next().to_le_bytes(), [BytecodeOp::Max as u8, 1, 1, 0]);
assert_eq!(next(), 0xFF000000);
// Copy from mem[0] -> reg[0]
assert_eq!(
next().to_le_bytes(),
[BytecodeOp::Mem as u8, 0, 0xFF, 0xFF]
);
assert_eq!(next(), 0);
// r0 = max(0, 1)
assert_eq!(next().to_le_bytes(), [BytecodeOp::Max as u8, 0, 0, 1]);
assert_eq!(next(), 0xFF000000);
// output(0) = r0
assert_eq!(
next().to_le_bytes(),
[BytecodeOp::Output as u8, 0, 0xFF, 0xFF]
);
assert_eq!(next(), 0);
assert_eq!(next(), 0xFFFFFFFF); // end marker
assert_eq!(next(), 0xFFFFFFFF);
assert!(iter.next().is_none());
}
}