Skip to content

Commit

Permalink
fix(lib): fixes rust-lang/rfcs#3535
Browse files Browse the repository at this point in the history
- identify instructions with integer enum instead of function pointer
- impl `IntoIterator` for `Memory`
- output runtime errors even when `Source` is blank
  • Loading branch information
SaadiSave committed Nov 20, 2024
1 parent e1697a0 commit b1e98d5
Show file tree
Hide file tree
Showing 12 changed files with 197 additions and 69 deletions.
3 changes: 3 additions & 0 deletions lib/examples/showoff.pasm
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ ldm r0, #206
ldm r1, #5
call read
mov acc, 206
ldm r0, #206
ldm r1, #5
call print
end

// procedure to print from linear memory
Expand Down
14 changes: 8 additions & 6 deletions lib/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ use bincode::{Decode, Encode};
#[cfg_attr(feature = "bincode", derive(Encode, Decode))]
#[derive(Debug)]
struct CompiledInst {
pub id: u64,
pub inst: String,
pub op: Op,
}

impl CompiledInst {
pub fn new(inst: String, op: Op) -> Self {
Self { inst, op }
pub fn new(id: u64, inst: String, op: Op) -> Self {
Self { id, inst, op }
}
}

Expand Down Expand Up @@ -60,10 +61,11 @@ impl CompiledProg {
let prog = self
.prog
.into_iter()
.map(|(addr, CompiledInst { inst, op })| {
.map(|(addr, CompiledInst { inst, op, id })| {
(
addr,
ExecInst::new(
id,
inst.parse::<T>()
.unwrap_or_else(|s| panic!("{s}"))
.as_func_ptr(),
Expand Down Expand Up @@ -92,14 +94,14 @@ where

let prog = prog
.into_iter()
.map(|(addr, ExecInst { func, op })| {
let str_inst = match T::from_func_ptr(func) {
.map(|(addr, ExecInst { op, id, .. })| {
let str_inst = match T::from_id(id) {
Ok(inst) => inst,
Err(e) => panic!("{e}"),
}
.to_string();

(addr, CompiledInst::new(str_inst, op))
(addr, CompiledInst::new(id, str_inst, op))
})
.collect();

Expand Down
5 changes: 5 additions & 0 deletions lib/src/exec/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ impl Source {
writeln!(write, "Runtime Error:")?;
writeln!(write)?;

if self.0.is_empty() {
writeln!(write, "(source empty, error at position {pos})")?;
return writeln!(write, "message: {err}");
}

for (i, s) in self.0.iter().enumerate() {
if pos == i {
if let Some(prev) = self.0.get(i - 1) {
Expand Down
6 changes: 4 additions & 2 deletions lib/src/exec/inst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ pub type ExecFunc = fn(&mut Context, &Op) -> RtResult;
/// Runtime representation of an instruction
#[derive(Clone)]
pub struct ExecInst {
/// Identifies the instruction with an integer, fixes rust-lang/rfcs#3535
pub id: u64,
pub func: ExecFunc,
pub op: Op,
}

impl ExecInst {
pub fn new(inst: ExecFunc, op: Op) -> Self {
Self { func: inst, op }
pub fn new(id: u64, inst: ExecFunc, op: Op) -> Self {
Self { func: inst, op, id }
}
}

Expand Down
8 changes: 8 additions & 0 deletions lib/src/exec/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ impl Memory {
}
}

impl<'a> IntoIterator for &'a Memory {
type IntoIter = std::collections::btree_map::Iter<'a, usize, usize>;
type Item = (&'a usize, &'a usize);
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}

impl<T> From<T> for Memory
where
T: Into<BTreeMap<usize, usize>>,
Expand Down
22 changes: 11 additions & 11 deletions lib/src/exec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ impl Display for Context {

writeln!(f, "{:>6}: Memory {{", "mem")?;

for (addr, entry) in self.mem.iter() {
for (addr, entry) in &self.mem {
writeln!(f, "{addr:>8}: {entry},")?;
}

Expand Down Expand Up @@ -334,7 +334,7 @@ impl Executor {

trace!(
"Executing instruction {} {}",
T::from_func_ptr(inst.func).unwrap_or_else(|msg| panic!("{msg}")),
T::from_id(inst.id).unwrap_or_else(|msg| panic!("{msg}")),
inst.op
);

Expand Down Expand Up @@ -375,7 +375,7 @@ impl Executor {
}
}

pub fn display<T>(&self) -> Result<String, <T as FromStr>::Err>
pub fn display_with_opcodes<T>(&self) -> Result<String, <T as FromStr>::Err>
where
T: InstSet,
<T as FromStr>::Err: Display,
Expand All @@ -388,8 +388,8 @@ impl Executor {

writeln!(s, "Executor {{").unwrap();

for (addr, ExecInst { op, func }) in &self.prog {
writeln!(s, "{addr:>6}: {func} {op}", func = T::from_func_ptr(*func)?).unwrap();
for (addr, ExecInst { id, op, .. }) in &self.prog {
writeln!(s, "{addr:>6}: {func} {op}", func = T::from_id(*id)?).unwrap();
}

s.push('}');
Expand Down Expand Up @@ -435,12 +435,12 @@ fn exec() {
let prog: BTreeMap<usize, ExecInst> = BTreeMap::from(
// Division algorithm from examples/division.pasm
[
(0, ExecInst::new(arith::inc, "202".into())),
(1, ExecInst::new(arith::add, "203,201".into())),
(2, ExecInst::new(cmp::cmp, "203,204".into())),
(3, ExecInst::new(cmp::jpn, "0".into())),
(4, ExecInst::new(mov::ldd, "202".into())),
(5, ExecInst::new(io::end, "".into())),
(0, ExecInst::new(0, arith::inc, "202".into())),
(1, ExecInst::new(0, arith::add, "203,201".into())),
(2, ExecInst::new(0, cmp::cmp, "203,204".into())),
(3, ExecInst::new(0, cmp::jpn, "0".into())),
(4, ExecInst::new(0, mov::ldd, "202".into())),
(5, ExecInst::new(0, io::end, "".into())),
],
);

Expand Down
Loading

0 comments on commit b1e98d5

Please sign in to comment.