Skip to content

Commit

Permalink
lib: add Fail execution step
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Oct 18, 2024
1 parent 9a99445 commit 1f52ba1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
22 changes: 11 additions & 11 deletions src/isa/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ pub enum ExecStep {
/// Stop program execution
Stop,

/// Stop and fail program execution
Fail,

/// Move to the next instruction
Next,

Expand Down Expand Up @@ -227,37 +230,34 @@ impl InstructionSet for ControlFlowOp {

fn exec(&self, regs: &mut CoreRegs, site: LibSite, _: &()) -> ExecStep {
match self {
ControlFlowOp::Fail => {
regs.st0 = false;
ExecStep::Stop
}
ControlFlowOp::Fail => ExecStep::Fail,

Check warning on line 233 in src/isa/exec.rs

View check run for this annotation

Codecov / codecov/patch

src/isa/exec.rs#L233

Added line #L233 was not covered by tests
ControlFlowOp::Test => {
if regs.st0 {
ExecStep::Next
} else {
ExecStep::Stop
ExecStep::Fail
}
}
ControlFlowOp::Jmp(offset) => {
regs.jmp().map(|_| ExecStep::Jump(*offset)).unwrap_or(ExecStep::Stop)
regs.jmp().map(|_| ExecStep::Jump(*offset)).unwrap_or(ExecStep::Fail)

Check warning on line 242 in src/isa/exec.rs

View check run for this annotation

Codecov / codecov/patch

src/isa/exec.rs#L242

Added line #L242 was not covered by tests
}
ControlFlowOp::Jif(offset) => {
if regs.st0 {
regs.jmp().map(|_| ExecStep::Jump(*offset)).unwrap_or(ExecStep::Stop)
regs.jmp().map(|_| ExecStep::Jump(*offset)).unwrap_or(ExecStep::Fail)

Check warning on line 246 in src/isa/exec.rs

View check run for this annotation

Codecov / codecov/patch

src/isa/exec.rs#L246

Added line #L246 was not covered by tests
} else {
ExecStep::Next
}
}
ControlFlowOp::Routine(offset) => {
regs.call(site).map(|_| ExecStep::Jump(*offset)).unwrap_or(ExecStep::Stop)
regs.call(site).map(|_| ExecStep::Jump(*offset)).unwrap_or(ExecStep::Fail)

Check warning on line 252 in src/isa/exec.rs

View check run for this annotation

Codecov / codecov/patch

src/isa/exec.rs#L252

Added line #L252 was not covered by tests
}
ControlFlowOp::Call(site) => {
regs.call(*site).map(|_| ExecStep::Call(*site)).unwrap_or(ExecStep::Stop)
regs.call(*site).map(|_| ExecStep::Call(*site)).unwrap_or(ExecStep::Fail)

Check warning on line 255 in src/isa/exec.rs

View check run for this annotation

Codecov / codecov/patch

src/isa/exec.rs#L255

Added line #L255 was not covered by tests
}
ControlFlowOp::Exec(site) => {
regs.jmp().map(|_| ExecStep::Call(*site)).unwrap_or(ExecStep::Stop)
regs.jmp().map(|_| ExecStep::Call(*site)).unwrap_or(ExecStep::Fail)

Check warning on line 258 in src/isa/exec.rs

View check run for this annotation

Codecov / codecov/patch

src/isa/exec.rs#L258

Added line #L258 was not covered by tests
}
ControlFlowOp::Ret => regs.ret().map(ExecStep::Call).unwrap_or(ExecStep::Stop),
ControlFlowOp::Ret => regs.ret().map(ExecStep::Call).unwrap_or(ExecStep::Fail),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/isa/instr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ pub enum CmpOp {
St(MergeFlag, RegA, Reg8),

/// Inverses value in `st0` register
#[display("stinv")]
#[display("inv st0")]
StInv,
}

Expand Down
7 changes: 7 additions & 0 deletions src/library/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,13 @@ impl Lib {
}
return None;
}
ExecStep::Fail => {
registers.st0 = false;
assert_eq!(registers.st0, false);
#[cfg(feature = "log")]
eprintln!("execution stopped; {d}st0={z}{r}{}{z}", registers.st0);
return None;
}
ExecStep::Next => {
#[cfg(feature = "log")]
eprintln!();
Expand Down

0 comments on commit 1f52ba1

Please sign in to comment.