diff --git a/rust2/src/lib.rs b/rust2/src/lib.rs index 4f1e15b..7baf4f3 100644 --- a/rust2/src/lib.rs +++ b/rust2/src/lib.rs @@ -86,7 +86,7 @@ where if let Some(DumpKind::Mir) = config.dump { let mir_alloc = Bump::new(); let mir = mir::optimized_mir(&mir_alloc, &optimized_hir); - println!("{mir:#?}"); + //println!("{mir:#?}"); } let cg_alloc = Bump::new(); diff --git a/rust2/src/main.rs b/rust2/src/main.rs index d7220d2..cc1f2b3 100644 --- a/rust2/src/main.rs +++ b/rust2/src/main.rs @@ -16,6 +16,7 @@ fn main() { tracing_subscriber::fmt() .with_env_filter(tracing_subscriber::filter::EnvFilter::from_default_env()) + .without_time() .init(); let src = fs::read_to_string(&args.file).unwrap_or_else(|err| { diff --git a/rust2/src/mir/mod.rs b/rust2/src/mir/mod.rs index d49c627..928b852 100644 --- a/rust2/src/mir/mod.rs +++ b/rust2/src/mir/mod.rs @@ -54,10 +54,10 @@ enum StmtKind<'mir> { SetN(u8, Store), } -#[tracing::instrument] +#[tracing::instrument(skip(alloc, hir))] pub fn optimized_mir<'mir>(alloc: &'mir Bump, hir: &Hir<'_>) -> Mir<'mir> { let mut mir = hir_to_mir(alloc, hir); - opts::pass_get_state_info(alloc, &mut mir); + opts::passes(alloc, &mut mir); mir } diff --git a/rust2/src/mir/opts.rs b/rust2/src/mir/opts.rs index 1abe2ab..a13b88c 100644 --- a/rust2/src/mir/opts.rs +++ b/rust2/src/mir/opts.rs @@ -1,4 +1,5 @@ use bumpalo::Bump; +use tracing::info; use crate::mir::{ state::{CellState, MemoryState, MemoryStateChange}, @@ -6,13 +7,19 @@ use crate::mir::{ }; /// this pass fills out as much state info for all statements as possible -#[tracing::instrument] +#[tracing::instrument(skip(alloc, mir))] +pub fn passes<'mir>(alloc: &'mir Bump, mir: &mut Mir<'mir>) { + pass_get_state_info(alloc, mir); + pass_const_propagation(mir); +} +/// this pass fills out as much state info for all statements as possible +#[tracing::instrument(skip(alloc, mir))] pub fn pass_get_state_info<'mir>(alloc: &'mir Bump, mir: &mut Mir<'mir>) { let empty_state = MemoryState::empty(alloc); pass_get_state_info_inner(alloc, mir, empty_state); } -#[tracing::instrument] +#[tracing::instrument(skip(alloc, mir))] fn pass_get_state_info_inner<'mir>( alloc: &'mir Bump, mir: &mut Mir<'mir>, @@ -85,5 +92,28 @@ fn pass_get_state_info_inner<'mir>( } } -#[tracing::instrument] -fn pass_const_propagation(mir: &mut Mir<'_>) {} +#[tracing::instrument(skip(mir))] +fn pass_const_propagation(mir: &mut Mir<'_>) { + pass_const_propagation_inner(mir) +} + +fn pass_const_propagation_inner(mir: &mut Mir<'_>) { + for stmt in &mut mir.stmts { + match &mut stmt.kind { + StmtKind::Out => { + let state = stmt.state.state_for_offset(0); + info!(?state, "We got the state of the output 😳😳😳"); + // we could now insert a `SetN` before the `Out`, to mark the previous store + // as dead. + } + StmtKind::Loop(body) => { + let state = stmt.state.state_for_offset(0); + info!(?state, "We got the state of the output 😳😳😳"); + // we could now insert a `SetN` before the `Out`, to mark the previous store + // as dead. + pass_const_propagation_inner(body); + } + _ => {} + } + } +} diff --git a/rust2/src/mir/state.rs b/rust2/src/mir/state.rs index 6beb210..209229f 100644 --- a/rust2/src/mir/state.rs +++ b/rust2/src/mir/state.rs @@ -38,7 +38,7 @@ impl<'mir> MemoryState<'mir> { }))) } - pub fn state_for_offset(&self, offset: i32) -> &'mir CellState { + pub fn state_for_offset(&self, offset: i32) -> CellState { self.0.borrow().state_for_offset(offset) } } @@ -60,26 +60,25 @@ pub struct MemoryStateInner<'mir> { } impl<'mir> MemoryStateInner<'mir> { - pub fn state_for_offset(&self, offset: i32) -> &'mir CellState { + pub fn state_for_offset(&self, offset: i32) -> CellState { let mut offset = offset; for delta in &self.deltas { match delta { MemoryStateChange::Change { offset: write_offset, new_state, - } if *write_offset == offset => { - return new_state; - } + } if *write_offset == offset => return new_state.clone(), MemoryStateChange::Move(change) => offset -= change, // we may not access the forbidden knowledge - MemoryStateChange::Forget => return &CellState::Unknown, + MemoryStateChange::Forget => return CellState::Unknown, _ => {} } } self.prev + .as_ref() .map(|state| state.state_for_offset(offset)) - .unwrap_or(&CellState::Unknown) + .unwrap_or(CellState::Unknown) } } diff --git a/rust2/test.bf b/rust2/test.bf index b56e5f8..e420ff6 100644 --- a/rust2/test.bf +++ b/rust2/test.bf @@ -1 +1 @@ -+>. \ No newline at end of file +[-]+>[-]<<>. \ No newline at end of file