Skip to content

Commit 8e979cf

Browse files
committed
chore(interpreter): better formatting
1 parent 8b29b26 commit 8e979cf

2 files changed

Lines changed: 81 additions & 38 deletions

File tree

interpreter/src/ir/lower.rs

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,24 @@
33
// For the full copyright and license information, please view the LICENSE
44
// files that was distributed with this source code.
55

6-
use std::{fmt::Display, hash::Hash, mem::forget};
6+
use std::{hash::Hash, mem::forget};
77

88
use bumpalo::{Bump, collections::Vec};
9-
use indexmap::IndexSet;
109
use parser::{
1110
Atom, BinaryOperator, BinaryPlaceOperator, Body, Expr, ExprNode, Place, SimpleStatement,
1211
Statement, UnaryOperator, Variable,
1312
};
1413

1514
use crate::{
1615
ir::{Hint, HintedReg, Instruction, Label, NonLocal, OpCode, Reg},
17-
vm::{ExecMode, Interpreter, SymbolTable, Value},
16+
vm::{Consts, ExecMode, Interpreter, SymbolTable, Value},
1817
};
1918

2019
#[derive(Debug)]
2120
pub struct Code<'arena> {
2221
pub arena: &'arena Bump,
2322
pub bc: Bytecode<'arena>,
24-
pub consts: IndexSet<Value>,
23+
pub consts: Consts,
2524
pub symbols: SymbolTable<'arena>,
2625
free_regs: Vec<'arena, Reg>,
2726
pub reg_pointer: u16,
@@ -176,7 +175,7 @@ impl Code<'_> {
176175
}
177176

178177
fn register_const(&mut self, value: Value) -> NonLocal {
179-
NonLocal(self.consts.insert_full(value).0 as u16)
178+
NonLocal(self.consts.0.insert_full(value).0 as u16)
180179
}
181180

182181
fn following_instr(&self, nth: u16) -> Label {
@@ -236,21 +235,20 @@ impl RegsState {
236235
}
237236
}
238237

239-
pub fn test_interpreter(stmnt: &Body<'_>) -> impl Display {
238+
pub fn test_interpreter(stmnt: &Body<'_>) -> String {
240239
let bump = Bump::with_capacity(16384);
241240
let mut c = Code {
242241
arena: &bump,
243242
bc: Bytecode::new_in(&bump),
244-
consts: IndexSet::new(),
243+
consts: Consts::new(),
245244
symbols: SymbolTable::new_in(&bump),
246245
reg_pointer: 0,
247246
free_regs: Vec::new_in(&bump),
248247
};
249248
c.lower_body(stmnt);
250-
let code = c.to_string();
251249
let mut vm = Interpreter::new(ExecMode::Uu, c);
252250
vm.run();
253-
format!("{code}---\n{vm:#?}")
251+
vm.to_string()
254252
}
255253

256254
impl From<UnaryOperator> for OpCode {
@@ -301,33 +299,6 @@ impl Eq for Value {}
301299
// fn fold(&self, args: Self::Args) -> T;
302300
// }
303301

304-
impl Display for Bytecode<'_> {
305-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
306-
let n = self.code.len() / 10 + 1;
307-
for (i, e) in self.code.iter().enumerate() {
308-
write!(f, "{i:n$}: {e}")?;
309-
if i + 1 < self.code.len() as _ {
310-
writeln!(f)?;
311-
}
312-
}
313-
Ok(())
314-
}
315-
}
316-
317-
impl Display for Code<'_> {
318-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
319-
writeln!(f, "Bytecode:\n{}\n", self.bc)?;
320-
writeln!(f, "Consts:")?;
321-
for (i, e) in self.consts.iter().enumerate() {
322-
write!(f, "mem[{i}] = {}", e.0)?;
323-
if i + 1 < self.consts.len() as _ {
324-
writeln!(f)?;
325-
}
326-
}
327-
Ok(())
328-
}
329-
}
330-
331302
impl LinearReg {
332303
fn into_inner(self) -> Reg {
333304
let inner = self.0;

interpreter/src/vm.rs

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::fmt::{self, Display};
2+
13
use bumpalo::{Bump, collections::Vec};
24
use hashbrown::{DefaultHashBuilder, HashMap};
35
use indexmap::{IndexMap, IndexSet};
@@ -26,7 +28,7 @@ pub struct Interpreter<'a> {
2628
program_counter: usize,
2729
registers: Registers<'a>,
2830
symbols: SymbolTable<'a>,
29-
consts: IndexSet<Value>,
31+
consts: Consts,
3032
compat: ExecMode,
3133
}
3234

@@ -41,6 +43,9 @@ pub struct SymbolTable<'a> {
4143
// etc
4244
}
4345

46+
#[derive(Debug)]
47+
pub struct Consts(pub IndexSet<Value>);
48+
4449
impl<'a> Interpreter<'a> {
4550
pub fn new(compat: ExecMode, code: Code<'a>) -> Self {
4651
Self {
@@ -83,6 +88,12 @@ impl<'a> SymbolTable<'a> {
8388
}
8489
}
8590

91+
impl Consts {
92+
pub fn new() -> Self {
93+
Self(IndexSet::with_capacity(4))
94+
}
95+
}
96+
8697
impl Interpreter<'_> {
8798
pub fn run(&mut self) {
8899
while let Some(instr) = self.bc.code.get(self.program_counter) {
@@ -103,7 +114,7 @@ impl Interpreter<'_> {
103114
ix if let Some(&(dest, src)) = ix.get_load_store() => match ix.opcode {
104115
OpCode::LoadConst => self
105116
.registers
106-
.write(dest, self.consts.get_index(src.0 as _).unwrap()),
117+
.write(dest, self.consts.0.get_index(src.0 as _).unwrap()),
107118
OpCode::LoadUser => {
108119
self.registers
109120
.write(dest, self.symbols.lookup_user_var(src));
@@ -141,3 +152,64 @@ impl Registers<'_> {
141152
self.0[dest.0 as usize] = Value::clone(src);
142153
}
143154
}
155+
156+
impl Display for Interpreter<'_> {
157+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
158+
writeln!(f, "{}\n", self.bc)?;
159+
writeln!(f, "{}\n", self.registers)?;
160+
writeln!(f, "{}\n", self.symbols)?;
161+
write!(f, "{}", self.consts)
162+
}
163+
}
164+
165+
impl Display for Code<'_> {
166+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
167+
writeln!(f, "{}\n", self.bc)?;
168+
writeln!(f, "{}\n", self.symbols)?;
169+
write!(f, "{}", self.consts)
170+
}
171+
}
172+
173+
impl Display for Bytecode<'_> {
174+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
175+
write!(f, "Bytecode:")?;
176+
let n = self.code.len().checked_ilog10().unwrap_or(0) as usize + 1;
177+
fmt_list(f, self.code.iter(), |f, i, e| write!(f, "{i:0n$}: {e}"))
178+
}
179+
}
180+
181+
impl Display for Registers<'_> {
182+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
183+
write!(f, "Registers:")?;
184+
let n = self.0.len().checked_ilog10().unwrap_or(0) as usize + 1;
185+
fmt_list(f, self.0.iter(), |f, i, e| write!(f, "r{i:0n$} = {e:?}"))
186+
}
187+
}
188+
189+
impl Display for SymbolTable<'_> {
190+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
191+
write!(f, "Symbols:")?;
192+
fmt_list(f, self.user.iter(), |f, i, (k, v)| {
193+
write!(f, "user[{i}] @ {k} = {v:?}")
194+
})
195+
}
196+
}
197+
198+
impl Display for Consts {
199+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
200+
write!(f, "Consts:")?;
201+
fmt_list(f, self.0.iter(), |f, i, e| write!(f, "mem[{i}] = {e:?}"))
202+
}
203+
}
204+
205+
fn fmt_list<'a, T: Copy>(
206+
f: &mut fmt::Formatter<'a>,
207+
iter: impl Iterator<Item = T>,
208+
cb: impl Fn(&mut fmt::Formatter<'a>, usize, T) -> fmt::Result,
209+
) -> fmt::Result {
210+
for (i, e) in iter.enumerate() {
211+
write!(f, "\n ")?;
212+
cb(f, i, e)?;
213+
}
214+
Ok(())
215+
}

0 commit comments

Comments
 (0)