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

De-macroified basic machine #144

Merged
merged 12 commits into from
Apr 10, 2024
528 changes: 517 additions & 11 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ members = [
"assembler",
"alu_u32",
"basic",
"basic_macro",
"bus",
"cpu",
"derive",
Expand Down
43 changes: 21 additions & 22 deletions basic/src/bin/valida.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use p3_baby_bear::BabyBear;

use p3_fri::{FriConfig, TwoAdicFriPcs, TwoAdicFriPcsConfig};
use valida_cpu::MachineWithCpuChip;
use valida_machine::{Machine, MachineProof, ProgramROM, StdinAdviceProvider};
use valida_machine::{Machine, MachineProof, ProgramROM, StdinAdviceProvider, StoppingFlag};
use valida_memory::MachineWithMemoryChip;

use valida_elf::{load_executable_file, Program};
Expand All @@ -34,7 +34,7 @@ use valida_output::MachineWithOutputChip;
use reedline_repl_rs::clap::{Arg, ArgMatches, Command};
use reedline_repl_rs::{Repl, Result};

#[derive(Parser)]
#[derive(Parser, Clone)]
struct Args {
/// Command option either "run" or "prove" or "verify" or "interactive"
#[arg(name = "Action Option")]
Expand All @@ -53,23 +53,23 @@ struct Args {
stack_height: u32,
}

struct Context<'a> {
struct Context {
machine_: BasicMachine<BabyBear>,
args_: &'a Args,
args_: Args,
breakpoints_: Vec<u32>,
stopped_: bool,
stopped_: StoppingFlag,
last_fp_: u32,
recorded_current_fp_: u32,
last_fp_size_: u32,
}

impl Context<'_> {
impl Context {
fn new(args: &Args) -> Context {
let mut context = Context {
machine_: BasicMachine::<BabyBear>::default(),
args_: args.clone(),
args_: (*args).clone(),
breakpoints_: Vec::new(),
stopped_: false,
stopped_: StoppingFlag::DidNotStop,
last_fp_: args.stack_height,
recorded_current_fp_: args.stack_height,
last_fp_size_: 0,
Expand All @@ -88,10 +88,10 @@ impl Context<'_> {
context
}

fn step(&mut self) -> (bool, u32) {
fn step(&mut self) -> (StoppingFlag, u32) {
// do not execute if already stopped
if self.stopped_ {
return (true, 0);
if self.stopped_ == StoppingFlag::DidStop {
return (StoppingFlag::DidStop, 0);
}
let state = self.machine_.step(&mut StdinAdviceProvider);
let pc = self.machine_.cpu().pc;
Expand All @@ -113,21 +113,20 @@ impl Context<'_> {
}
}

fn init_context(args: ArgMatches, context: &mut Context) -> Result<Option<String>> {
fn init_context(_args: ArgMatches, _context: &mut Context) -> Result<Option<String>> {
Ok(Some(String::from("created machine")))
}

fn status(args: ArgMatches, context: &mut Context) -> Result<Option<String>> {
fn status(_args: ArgMatches, context: &mut Context) -> Result<Option<String>> {
// construct machine status
let mut status = String::new();
status.push_str("FP: ");
status.push_str(&context.machine_.cpu().fp.to_string());
status.push_str(", PC: ");
status.push_str(&context.machine_.cpu().pc.to_string());
status.push_str(if context.stopped_ {
", Stopped"
} else {
", Running"
status.push_str(match context.stopped_ {
StoppingFlag::DidStop => ", Stopped",
StoppingFlag::DidNotStop => ", Running",
});
Ok(Some(status))
}
Expand Down Expand Up @@ -233,11 +232,11 @@ fn show_memory(args: ArgMatches, context: &mut Context) -> Result<Option<String>
Ok(Some(memory))
}

fn run_until(_: ArgMatches, context: &mut Context) -> Result<Option<String>> {
fn run_until(_args: ArgMatches, context: &mut Context) -> Result<Option<String>> {
let mut message = String::new();
loop {
let (stop, pc) = context.step();
if stop {
if stop == StoppingFlag::DidStop {
message.push_str("Execution stopped");
break;
}
Expand All @@ -250,10 +249,10 @@ fn run_until(_: ArgMatches, context: &mut Context) -> Result<Option<String>> {
Ok(Some(message))
}

fn step(_: ArgMatches, context: &mut Context) -> Result<Option<String>> {
fn step(_args: ArgMatches, context: &mut Context) -> Result<Option<String>> {
let (stop, _) = context.step();
if stop {
context.stopped_ = true;
if stop == StoppingFlag::DidStop {
context.stopped_ = StoppingFlag::DidStop;
Ok(Some(String::from("Execution stopped")))
} else {
Ok(None)
Expand Down
Loading
Loading