Skip to content

Commit

Permalink
refactor: fix bug, improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
Fumuran committed Oct 16, 2023
1 parent e13205f commit e580c9e
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 254 deletions.
22 changes: 7 additions & 15 deletions assembly/src/assembler/instruction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,28 +419,20 @@ fn process_debug_options(
) -> Result<Decorator, AssemblyError> {
let num_locals = ctx.num_proc_locals() as u32;
match options {
DebugOptions::LocalIndex(n) => {
if *n >= num_locals {
return Err(AssemblyError::ParsingError(format!(
"Local addres should be less than {}, but {} was provided",
num_locals, n
)));
}
Ok(Decorator::Debug(*options))
}
DebugOptions::LocalInterval(_, m, print_all) => {
DebugOptions::LocalInterval(interval, _, print_all) => {
if *print_all {
let locals_all = DebugOptions::LocalInterval(0, num_locals - 1, true);
let locals_all = DebugOptions::LocalInterval((0, num_locals - 1), num_locals, true);
Ok(Decorator::Debug(locals_all))
} else {
if *m >= num_locals {
return Err(AssemblyError::ParsingError(format!("The index of the end of the interval should be less than {}, but {} was provided", num_locals, m)));
if interval.1 >= num_locals {
return Err(AssemblyError::ParsingError(format!("The index of the end of the interval should be less than {}, but {} was provided", num_locals, interval.1)));
}
Ok(Decorator::Debug(*options))
let locals_interval = DebugOptions::LocalInterval(*interval, num_locals, false);
Ok(Decorator::Debug(locals_interval))
}
}
DebugOptions::All(_) => {
let all = DebugOptions::All(ctx.num_proc_locals());
let all = DebugOptions::All(num_locals);
Ok(Decorator::Debug(all))
}
_ => Ok(Decorator::Debug(*options)),
Expand Down
38 changes: 11 additions & 27 deletions assembly/src/ast/nodes/serde/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ use super::{super::DebugOptions, ByteReader, ByteWriter, DeserializationError, T
const STACK_ALL: u8 = 0;
const STACK_TOP: u8 = 1;
const MEM_ALL: u8 = 2;
const MEM_ADDR: u8 = 3;
const MEM_INTERVAL: u8 = 4;
const LOCAL_INDEX: u8 = 5;
const LOCAL_INTERVAL: u8 = 6;
const ALL: u8 = 7;
const MEM_INTERVAL: u8 = 3;
const LOCAL_INTERVAL: u8 = 4;
const ALL: u8 = 5;

/// Writes the provided [DebugOptions] into the provided target.
pub fn write_options_into<W: ByteWriter>(target: &mut W, options: &DebugOptions) {
Expand All @@ -18,28 +16,21 @@ pub fn write_options_into<W: ByteWriter>(target: &mut W, options: &DebugOptions)
target.write_u16(*n);
}
DebugOptions::MemAll => target.write_u8(MEM_ALL),
DebugOptions::MemAddr(n) => {
target.write_u8(MEM_ADDR);
target.write_u32(*n);
}
DebugOptions::MemInterval(n, m) => {
target.write_u8(MEM_INTERVAL);
target.write_u32(*n);
target.write_u32(*m);
}
DebugOptions::LocalIndex(n) => {
target.write_u8(LOCAL_INDEX);
target.write_u32(*n);
}
DebugOptions::LocalInterval(n, m, print_all) => {
DebugOptions::LocalInterval(interval, num_locals, print_all) => {
target.write_u8(LOCAL_INTERVAL);
target.write_u32(*n);
target.write_u32(*m);
target.write_u32(interval.0);
target.write_u32(interval.1);
target.write_u32(*num_locals);
target.write_bool(*print_all);
}
DebugOptions::All(num_locals) => {
target.write_u8(ALL);
target.write_u16(*num_locals);
target.write_u32(*num_locals);
}
}
}
Expand All @@ -58,27 +49,20 @@ pub fn read_options_from<R: ByteReader>(
Ok(DebugOptions::StackTop(n))
}
MEM_ALL => Ok(DebugOptions::MemAll),
MEM_ADDR => {
let n = source.read_u32()?;
Ok(DebugOptions::MemAddr(n))
}
MEM_INTERVAL => {
let n = source.read_u32()?;
let m = source.read_u32()?;
Ok(DebugOptions::MemInterval(n, m))
}
LOCAL_INDEX => {
let n = source.read_u32()?;
Ok(DebugOptions::LocalIndex(n))
}
LOCAL_INTERVAL => {
let n = source.read_u32()?;
let m = source.read_u32()?;
let num_locals = source.read_u32()?;
let print_all = source.read_bool()?;
Ok(DebugOptions::LocalInterval(n, m, print_all))
Ok(DebugOptions::LocalInterval((n, m), num_locals, print_all))
}
ALL => {
let num_locals = source.read_u16()?;
let num_locals = source.read_u32()?;
Ok(DebugOptions::All(num_locals))
}
val => Err(DeserializationError::InvalidValue(val.to_string())),
Expand Down
8 changes: 4 additions & 4 deletions assembly/src/ast/parsers/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn parse_debug(op: &Token) -> Result<Node, ParsingError> {
2 => DebugOptions::MemAll,
3 => {
let n: u32 = parse_checked_param(op, 2, 1..=u32::MAX)?;
DebugOptions::MemAddr(n)
DebugOptions::MemInterval(n, n)
}
4 => {
let n: u32 = parse_checked_param(op, 2, 0..=u32::MAX)?;
Expand All @@ -46,18 +46,18 @@ pub fn parse_debug(op: &Token) -> Result<Node, ParsingError> {
_ => return Err(ParsingError::extra_param(op)),
},
"local" => match op.num_parts() {
2 => DebugOptions::LocalInterval(0, 0, true),
2 => DebugOptions::LocalInterval((0, 0), 0, true),
3 => {
let n: u32 = parse_checked_param(op, 2, 0..=u32::MAX)?;
DebugOptions::LocalIndex(n)
DebugOptions::LocalInterval((n, n), 0, false)
}
4 => {
let n: u32 = parse_checked_param(op, 2, 0..=u32::MAX)?;
let m: u32 = parse_checked_param(op, 3, 0..=u32::MAX)?;
if m < n {
return Err(ParsingError::invalid_param_with_reason(op, 3, "the index of the end of the interval must be greater than the index of its beginning"));
}
DebugOptions::LocalInterval(n, m, false)
DebugOptions::LocalInterval((n, m), 0, false)
}
_ => return Err(ParsingError::extra_param(op)),
},
Expand Down
17 changes: 6 additions & 11 deletions core/src/operations/decorators/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,16 @@ pub enum DebugOptions {
StackTop(u16),
/// Prints out the entire contents of RAM.
MemAll,
/// Prints out contents of memory at the provided address.
MemAddr(u32),
/// Prints out the contents of memory stored in the provided interval. Interval boundaries are
/// both inclusive.
MemInterval(u32, u32),
/// Prints out contents of the local at the provided index for the currently executing
/// procedure.
LocalIndex(u32),
/// Prints out locals stored in the provided interval of the currently executing procedure.
/// Interval boundaries are both inclusive.
LocalInterval(u32, u32, bool),
///
/// Boolean parameter indicated whether whole local memory should be printed.
LocalInterval((u32, u32), u32, bool),
/// Prints out the entire state of the VM (stack and RAM).
All(u16),
All(u32),
}

impl fmt::Display for DebugOptions {
Expand All @@ -36,14 +33,12 @@ impl fmt::Display for DebugOptions {
Self::StackAll => write!(f, "stack"),
Self::StackTop(n) => write!(f, "stack.{n}"),
Self::MemAll => write!(f, "mem"),
Self::MemAddr(n) => write!(f, "mem.{n}"),
Self::MemInterval(n, m) => write!(f, "mem.{n}.{m}"),
Self::LocalIndex(n) => write!(f, "local.{n}"),
Self::LocalInterval(n, m, print_all) => {
Self::LocalInterval(interval, _, print_all) => {
if *print_all {
write!(f, "local")
} else {
write!(f, "local.{n}.{m}")
write!(f, "local.{}.{}", interval.0, interval.1)
}
}
Self::All(_) => write!(f, "all"),
Expand Down
27 changes: 27 additions & 0 deletions miden/examples/merkle_store/merkle_store.masm
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
proc.foo.15
push.11
loc_store.0
push.101
loc_store.1

debug.stack
debug.mem
push.10.1
mem_store
push.1.2.3.4.1000
mem_storew
debug.mem
debug.mem.1000
debug.mem.1001
debug.mem.999.1002
debug.mem.1000.1000

debug.local
debug.local.1
debug.local.0.1

debug.all
end

begin
# push the root of the Partial Merkle Tree on the stack
push.0x82bb4d9a8e93582f1387611949703eecd8e4c74b904880f9c9c85f1fc1d7576d
Expand Down Expand Up @@ -33,6 +58,8 @@ begin
push.22.0.0.0
assert_eqw

exec.foo

dropw

end
Loading

0 comments on commit e580c9e

Please sign in to comment.