Skip to content

Commit

Permalink
Verification pass (#73)
Browse files Browse the repository at this point in the history
* Define VerificationCtx

* Define trait VerificationPass
  • Loading branch information
emhane authored Oct 17, 2024
1 parent 21184cd commit c6a9dc2
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
34 changes: 34 additions & 0 deletions crates/verifier/src/ctx.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//! Verification context

use sonatina_ir::{ControlFlowGraph, Function};

use crate::{error::ErrorData, ErrorStack};

pub struct VerificationCtx<'a> {
pub func: &'a Function,
pub cfg: ControlFlowGraph,
pub error_stack: ErrorStack,
}

impl<'a> VerificationCtx<'a> {
pub fn new(func: &'a Function) -> Self {
let mut cfg = ControlFlowGraph::new();
cfg.compute(func);

Self {
func,
cfg,
error_stack: ErrorStack::default(),
}
}

pub fn report_nonfatal(&mut self, errs: &[ErrorData]) {
for e in errs {
let _err_ref = self.error_stack.push(*e);
}
}

pub fn report_fatal(&mut self, e: ErrorData) {
self.error_stack.fatal_error = Some(e);
}
}
17 changes: 13 additions & 4 deletions crates/verifier/src/error_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,30 @@ use crate::error::{Error, ErrorData, ErrorRef};

#[derive(Debug, Default)]
pub struct ErrorStack {
pub errors: PrimaryMap<ErrorRef, ErrorData>,
pub fatal_error: Option<ErrorData>,
pub non_fatal_errors: PrimaryMap<ErrorRef, ErrorData>,
}

impl ErrorStack {
pub fn push(&mut self, err: ErrorData) -> ErrorRef {
self.errors.push(err)
self.non_fatal_errors.push(err)
}

pub fn into_errs_iter(
self,
func: &Function,
func_ref: FuncRef,
) -> impl IntoIterator<Item = Error<'_>> {
self.errors
.into_iter()
let Self {
fatal_error,
non_fatal_errors: mut errs,
} = self;

if let Some(err) = fatal_error {
errs.push(err);
}

errs.into_iter()
.map(move |(_, err)| Error::new(err, func, func_ref))
}
}
6 changes: 6 additions & 0 deletions crates/verifier/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
pub mod ctx;
pub mod error;
pub mod error_stack;
pub mod pass;

pub use ctx::VerificationCtx;
pub use error_stack::ErrorStack;
pub use pass::VerificationPass;
7 changes: 7 additions & 0 deletions crates/verifier/src/pass.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//! Verification pass

use crate::VerificationCtx;

pub trait VerificationPass {
fn run(&mut self, ctx: VerificationCtx);
}

0 comments on commit c6a9dc2

Please sign in to comment.