Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
lialan committed Apr 10, 2024
1 parent cb8d47d commit bec888e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 49 deletions.
12 changes: 6 additions & 6 deletions basic/src/bin/valida.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,7 @@ fn list_instrs(args: ArgMatches, context: &mut Context) -> Result<Option<String>
let program_rom = &context.machine_.program().program_rom;
let total_size = program_rom.0.len();

let print_size_arg = args
.get_one::<String>("size");
let print_size_arg = args.get_one::<String>("size");

let print_size = match print_size_arg {
Some(size) => size.parse::<u32>().unwrap(),
Expand Down Expand Up @@ -216,7 +215,7 @@ fn set_bp(args: ArgMatches, context: &mut Context) -> Result<Option<String>> {
Ok(Some(message))
}

fn run_until(_ : ArgMatches, context: &mut Context) -> Result<Option<String>> {
fn run_until(_: ArgMatches, context: &mut Context) -> Result<Option<String>> {
let mut message = String::new();
loop {
let (stop, pc) = context.step();
Expand All @@ -233,7 +232,7 @@ fn run_until(_ : ArgMatches, context: &mut Context) -> Result<Option<String>> {
Ok(Some(message))
}

fn step(_ : ArgMatches, context: &mut Context) -> Result<Option<String>> {
fn step(_: ArgMatches, context: &mut Context) -> Result<Option<String>> {
let (stop, _) = context.step();
if stop {
context.stopped_ = true;
Expand Down Expand Up @@ -278,8 +277,9 @@ fn repl_run(args: &Args) {
run_until,
)
.with_command(
Command::new("l").about("list instruction at current PC")
.arg(Arg::new("size").required(false)),
Command::new("l")
.about("list instruction at current PC")
.arg(Arg::new("size").required(false)),
list_instrs,
)
.with_command(
Expand Down
78 changes: 38 additions & 40 deletions machine/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ impl ToString for InstructionWord<i32> {
Ok(opcode_name) => {
format!("{:?}", opcode_name)
}
Err(_) => {format!("UNKNOWN_OP:{}", self.opcode.to_string())}
Err(_) => {
format!("UNKNOWN_OP:{}", self.opcode.to_string())
}
};
format!("{} {}", opcode, self.print_operands())
}
Expand All @@ -46,13 +48,17 @@ impl InstructionWord<i32> {

pub fn print_imm32(&self) -> String {
assert!(self.opcode == IMM32, "Instruction is not immediate");

//extract the immediate value
let imm0 = self.operands.0[1];
let imm1 = self.operands.0[2];
let imm2 = self.operands.0[3];
let imm3 = self.operands.0[4];
format!("{}(fp), {}", self.operands.0[0], imm0 << 24 | imm1 << 16 | imm2 << 8 | imm3)
format!(
"{}(fp), {}",
self.operands.0[0],
imm0 << 24 | imm1 << 16 | imm2 << 8 | imm3
)
}

pub fn print_first_operand(&self) -> String {
Expand All @@ -68,51 +74,43 @@ impl InstructionWord<i32> {
}
}

pub fn print_address(&self, index : usize) -> String {
format!("{}", self.operands.0[index]/24)
pub fn print_address(&self, index: usize) -> String {
format!("{}", self.operands.0[index] / 24)
}

pub fn print_operands(&self) -> String {
match self.opcode {
valida_opcodes::IMM32 => self.print_imm32(),
valida_opcodes::JAL =>
format!(
"{}(fp), PC: {}, {}",
self.operands.0[0],
self.print_address(1),
self.operands.0[2]),
valida_opcodes::JALV =>
format!(
"{}(fp), {}(fp), {}(fp)",
self.operands.0[0],
self.operands.0[1],
self.operands.0[2]),
valida_opcodes::LOADFP =>
format!(
"{}(fp), {}",
self.operands.0[0],
self.operands.0[1]),
valida_opcodes::BEQ |
valida_opcodes::BNE =>
format!(
"{}, {}, {}",
self.print_address(0),
self.print_first_operand(),
self.print_second_operand()),
valida_opcodes::JAL => format!(
"{}(fp), PC: {}, {}",
self.operands.0[0],
self.print_address(1),
self.operands.0[2]
),
valida_opcodes::JALV => format!(
"{}(fp), {}(fp), {}(fp)",
self.operands.0[0], self.operands.0[1], self.operands.0[2]
),
valida_opcodes::LOADFP => format!("{}(fp), {}", self.operands.0[0], self.operands.0[1]),
valida_opcodes::BEQ | valida_opcodes::BNE => format!(
"{}, {}, {}",
self.print_address(0),
self.print_first_operand(),
self.print_second_operand()
),
valida_opcodes::STOP => "".to_string(),
valida_opcodes::LOAD32 =>
format!(
"{}(fp), {}(fp)",
self.operands.0[0],
self.operands.0[1]),
valida_opcodes::STORE32 =>
format!(
"{}(fp), {}(fp)",
self.operands.0[1],
self.operands.0[2]),
valida_opcodes::LOAD32 => {
format!("{}(fp), {}(fp)", self.operands.0[0], self.operands.0[1])
}
valida_opcodes::STORE32 => {
format!("{}(fp), {}(fp)", self.operands.0[1], self.operands.0[2])
}
_ => {
format!(
"{}(fp), {}, {}", self.operands.0[0], self.print_first_operand(), self.print_second_operand()
"{}(fp), {}, {}",
self.operands.0[0],
self.print_first_operand(),
self.print_second_operand()
)
}
}
Expand Down
4 changes: 1 addition & 3 deletions opcodes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub enum Opcode {

macro_rules! declare_opcode {
($opcode : ident) => {
pub const $opcode : u32 = Opcode::$opcode as u32;
pub const $opcode: u32 = Opcode::$opcode as u32;
};
}

Expand All @@ -60,7 +60,6 @@ declare_opcode!(LOADFP);
/// NONDETERMINISTIC
declare_opcode!(READ_ADVICE);


/// U32 ALU
declare_opcode!(ADD32);
declare_opcode!(SUB32);
Expand All @@ -80,7 +79,6 @@ declare_opcode!(MULHS32);
declare_opcode!(LTE32);
declare_opcode!(EQ32);


/// NATIVE FIELD
declare_opcode!(ADD);
declare_opcode!(SUB);
Expand Down

0 comments on commit bec888e

Please sign in to comment.