Skip to content

Commit

Permalink
fix: miden executable errors
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbinth committed Nov 20, 2022
1 parent c132a30 commit 2d3f84b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 38 deletions.
11 changes: 9 additions & 2 deletions assembly/src/assembler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,15 @@ type ProcedureCache = BTreeMap<ProcedureId, Procedure>;

// ASSEMBLER
// ================================================================================================

/// TODO: add comments
/// Miden Assembler which can be used to convert Miden assembly source code into program MAST (
/// represented by the [Program] struct). The assembler can be instantiated in several ways using
/// a "builder" patter. Specifically:
/// - If `with_kernel()` or `with_kernel_module()` methods are not used, the assembler will be
/// instantiated with a default empty kernel. Programs compiled using such assembler
/// cannot make calls to kernel procedures via `syscall` instruction.
/// - If `with_module_provider()` method is not used, the assembler will be instantiated without
/// access to external libraries. Programs compiled with such assembler must be self-contained
/// (i.e., they cannot invoke procedures from external libraries).
pub struct Assembler {
kernel: Kernel,
module_provider: Box<dyn ModuleProvider>,
Expand Down
2 changes: 1 addition & 1 deletion assembly/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{ProcedureId, String, ToString, Token};
use super::{ProcedureId, String, ToString, Token, Vec};
use core::fmt;

// ASSEMBLY ERROR
Expand Down
21 changes: 21 additions & 0 deletions miden/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use core::fmt;
use miden::{AssemblyError, ExecutionError};
use structopt::StructOpt;

mod cli;
Expand Down Expand Up @@ -52,3 +54,22 @@ pub fn main() {
println!("{}", error);
}
}

// PROGRAM ERROR
// ================================================================================================

/// This is used to specify the error type returned from analyze.
#[derive(Debug)]
pub enum ProgramError {
AssemblyError(AssemblyError),
ExecutionError(ExecutionError),
}

impl fmt::Display for ProgramError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ProgramError::AssemblyError(e) => write!(f, "Assembly Error: {:?}", e),
ProgramError::ExecutionError(e) => write!(f, "Execution Error: {:?}", e),
}
}
}
19 changes: 6 additions & 13 deletions miden/src/repl/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use air::StarkField;
use processor::{ExecutionError, Process};
use super::ProgramError;
use processor::Process;
use rustyline::{error::ReadlineError, Editor};
use vm_core::{Felt, ProgramInputs, Word};
use vm_core::{Felt, ProgramInputs, StarkField, Word};

/// This work is in continuation to the amazing work done by team `Scribe`
/// [here](https://github.com/ControlCplusControlV/Scribe/blob/main/transpiler/src/repl.rs#L8)
Expand Down Expand Up @@ -259,17 +259,17 @@ pub fn start_repl() {
/// Compiles and executes a compiled Miden program, returning the stack, memory and any Miden errors.
/// The program is passed in as a String, passed to the Miden Assembler, and then passed into the Miden
/// Processor to be executed.
fn execute(program: String) -> Result<(Vec<(u64, Word)>, Vec<Felt>), MidenError> {
fn execute(program: String) -> Result<(Vec<(u64, Word)>, Vec<Felt>), ProgramError> {
let program = assembly::Assembler::new()
.compile(&program)
.map_err(MidenError::AssemblyError)?;
.map_err(ProgramError::AssemblyError)?;

let pub_inputs = vec![];
let inputs = ProgramInputs::new(&pub_inputs, &[], vec![]).unwrap();
let mut process = Process::new_debug(program.kernel(), inputs);
let _program_outputs = process
.execute(&program)
.map_err(MidenError::ExecutionError);
.map_err(ProgramError::ExecutionError);

let (sys, _, stack, _, chiplets) = process.to_components();

Expand All @@ -282,13 +282,6 @@ fn execute(program: String) -> Result<(Vec<(u64, Word)>, Vec<Felt>), MidenError>
Ok((mem, stack_state))
}

/// Errors that are returned from the Miden processor during execution.
#[derive(Debug)]
pub enum MidenError {
AssemblyError(assembly::ParsingError),
ExecutionError(ExecutionError),
}

/// Parses the address in integer form from "!mem[addr]" command, otherwise throws an error.
fn read_mem_address(mem_str: &str) -> Result<u64, String> {
// the first five characters is "!mem[" and the digit character should start from 6th
Expand Down
24 changes: 2 additions & 22 deletions miden/src/tools/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use super::cli::InputFile;
use assembly::ParsingError;
use super::{cli::InputFile, ProgramError};
use core::fmt;
use miden::Assembler;
use processor::{AsmOpInfo, ExecutionError};
use processor::AsmOpInfo;
use std::path::PathBuf;
use stdlib::StdLibrary;
use structopt::StructOpt;
Expand Down Expand Up @@ -163,25 +162,6 @@ pub fn analyze(program: &str, inputs: ProgramInputs) -> Result<ProgramInfo, Prog
Ok(program_info)
}

// PROGRAM ERROR
// ================================================================================================

/// This is used to specify the error type returned from analyze.
#[derive(Debug)]
pub enum ProgramError {
AssemblyError(ParsingError),
ExecutionError(ExecutionError),
}

impl fmt::Display for ProgramError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ProgramError::AssemblyError(e) => write!(f, "Assembly Error: {:?}", e),
ProgramError::ExecutionError(e) => write!(f, "Execution Error: {:?}", e),
}
}
}

// ASMOP STATS
// ================================================================================================

Expand Down

0 comments on commit 2d3f84b

Please sign in to comment.