Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instruction debug fmt improvements #546

Merged
merged 4 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Changed

- [#546](https://github.com/FuelLabs/fuel-vm/pull/546): Improve debug formatting of instruction in panic receipts.

## [Version 0.36.0]

### Changed
Expand Down
32 changes: 16 additions & 16 deletions fuel-asm/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -903,26 +903,26 @@ macro_rules! op_debug_fmt {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
let ra = self.unpack();
f.debug_struct(stringify!($Op))
.field(stringify!($ra), &u8::from(ra))
.field(stringify!($ra), &format_args!("{:#02x}", u8::from(ra)))
.finish()
}
};
($Op:ident[$ra:ident : RegId $rb:ident : RegId]) => {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
let (ra, rb) = self.unpack();
f.debug_struct(stringify!($Op))
.field(stringify!($ra), &u8::from(ra))
.field(stringify!($rb), &u8::from(rb))
.field(stringify!($ra), &format_args!("{:#02x}", u8::from(ra)))
.field(stringify!($rb), &format_args!("{:#02x}", u8::from(rb)))
.finish()
}
};
($Op:ident[$ra:ident : RegId $rb:ident : RegId $rc:ident : RegId]) => {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
let (ra, rb, rc) = self.unpack();
f.debug_struct(stringify!($Op))
.field(stringify!($ra), &u8::from(ra))
.field(stringify!($rb), &u8::from(rb))
.field(stringify!($rc), &u8::from(rc))
.field(stringify!($ra), &format_args!("{:#02x}", u8::from(ra)))
.field(stringify!($rb), &format_args!("{:#02x}", u8::from(rb)))
.field(stringify!($rc), &format_args!("{:#02x}", u8::from(rc)))
.finish()
}
};
Expand All @@ -932,10 +932,10 @@ macro_rules! op_debug_fmt {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
let (ra, rb, rc, rd) = self.unpack();
f.debug_struct(stringify!($Op))
.field(stringify!($ra), &u8::from(ra))
.field(stringify!($rb), &u8::from(rb))
.field(stringify!($rc), &u8::from(rc))
.field(stringify!($rd), &u8::from(rd))
.field(stringify!($ra), &format_args!("{:#02x}", u8::from(ra)))
.field(stringify!($rb), &format_args!("{:#02x}", u8::from(rb)))
.field(stringify!($rc), &format_args!("{:#02x}", u8::from(rc)))
.field(stringify!($rd), &format_args!("{:#02x}", u8::from(rd)))
.finish()
}
};
Expand All @@ -945,9 +945,9 @@ macro_rules! op_debug_fmt {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
let (ra, rb, rc, imm) = self.unpack();
f.debug_struct(stringify!($Op))
.field(stringify!($ra), &u8::from(ra))
.field(stringify!($rb), &u8::from(rb))
.field(stringify!($rc), &u8::from(rc))
.field(stringify!($ra), &format_args!("{:#02x}", u8::from(ra)))
.field(stringify!($rb), &format_args!("{:#02x}", u8::from(rb)))
.field(stringify!($rc), &format_args!("{:#02x}", u8::from(rc)))
.field(stringify!($imm), &u8::from(imm))
.finish()
}
Expand All @@ -956,8 +956,8 @@ macro_rules! op_debug_fmt {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
let (ra, rb, imm) = self.unpack();
f.debug_struct(stringify!($Op))
.field(stringify!($ra), &u8::from(ra))
.field(stringify!($rb), &u8::from(rb))
.field(stringify!($ra), &format_args!("{:#02x}", u8::from(ra)))
.field(stringify!($rb), &format_args!("{:#02x}", u8::from(rb)))
.field(stringify!($imm), &u16::from(imm))
.finish()
}
Expand All @@ -966,7 +966,7 @@ macro_rules! op_debug_fmt {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
let (ra, imm) = self.unpack();
f.debug_struct(stringify!($Op))
.field(stringify!($ra), &u8::from(ra))
.field(stringify!($ra), &format_args!("{:#02x}", u8::from(ra)))
.field(stringify!($imm), &u32::from(imm))
.finish()
}
Expand Down
33 changes: 32 additions & 1 deletion fuel-asm/src/panic_instruction.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use core::fmt;

use crate::{
Instruction,
PanicReason,
RawInstruction,
Word,
};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "typescript", wasm_bindgen::prelude::wasm_bindgen)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
Expand Down Expand Up @@ -35,6 +37,35 @@ impl PanicInstruction {
}
}

/// Helper struct to debug-format a `RawInstruction` in `PanicInstruction::fmt`.
struct InstructionDbg(RawInstruction);
impl fmt::Debug for InstructionDbg {
/// Formats like this: `MOVI { dst: 32, val: 32 } (bytes: 72 80 00 20)`}`
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match Instruction::try_from(self.0) {
Ok(instr) => write!(f, "{:?}", instr)?,
Err(_) => write!(f, "Unknown")?,
Copy link
Member

@MitchTurner MitchTurner Aug 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Maybe something like Unknown Instruction or Unknown Operation might be slightly more clear when the caller doesn't know what to expect in this field. I don't feel strongly about it though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It already says instruction: Unknown, so I think it's good enough

};
write!(f, " (bytes: ")?;
for (i, byte) in self.0.to_be_bytes().iter().enumerate() {
if i != 0 {
write!(f, " ")?;
}
write!(f, "{:02x}", byte)?;
}
write!(f, ")")
}
}

impl fmt::Debug for PanicInstruction {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("PanicInstruction")
.field("reason", &self.reason)
.field("instruction", &InstructionDbg(self.instruction))
.finish()
}
}

#[cfg(feature = "typescript")]
#[wasm_bindgen::prelude::wasm_bindgen]
impl PanicInstruction {
Expand Down