From c6a9dc2451f3fdf5e841b570291c3371c41a010f Mon Sep 17 00:00:00 2001 From: Emilia Hane Date: Thu, 17 Oct 2024 09:24:41 +0200 Subject: [PATCH] Verification pass (#73) * Define VerificationCtx * Define trait VerificationPass --- crates/verifier/src/ctx.rs | 34 ++++++++++++++++++++++++++++++ crates/verifier/src/error_stack.rs | 17 +++++++++++---- crates/verifier/src/lib.rs | 6 ++++++ crates/verifier/src/pass.rs | 7 ++++++ 4 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 crates/verifier/src/ctx.rs create mode 100644 crates/verifier/src/pass.rs diff --git a/crates/verifier/src/ctx.rs b/crates/verifier/src/ctx.rs new file mode 100644 index 00000000..9547f862 --- /dev/null +++ b/crates/verifier/src/ctx.rs @@ -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); + } +} diff --git a/crates/verifier/src/error_stack.rs b/crates/verifier/src/error_stack.rs index 6f6ec236..08b0e749 100644 --- a/crates/verifier/src/error_stack.rs +++ b/crates/verifier/src/error_stack.rs @@ -5,12 +5,13 @@ use crate::error::{Error, ErrorData, ErrorRef}; #[derive(Debug, Default)] pub struct ErrorStack { - pub errors: PrimaryMap, + pub fatal_error: Option, + pub non_fatal_errors: PrimaryMap, } 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( @@ -18,8 +19,16 @@ impl ErrorStack { func: &Function, func_ref: FuncRef, ) -> impl IntoIterator> { - 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)) } } diff --git a/crates/verifier/src/lib.rs b/crates/verifier/src/lib.rs index d32fd422..5cadc79b 100644 --- a/crates/verifier/src/lib.rs +++ b/crates/verifier/src/lib.rs @@ -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; diff --git a/crates/verifier/src/pass.rs b/crates/verifier/src/pass.rs new file mode 100644 index 00000000..9c4198ee --- /dev/null +++ b/crates/verifier/src/pass.rs @@ -0,0 +1,7 @@ +//! Verification pass + +use crate::VerificationCtx; + +pub trait VerificationPass { + fn run(&mut self, ctx: VerificationCtx); +}