Skip to content

Commit

Permalink
SpanBuilder methods
Browse files Browse the repository at this point in the history
  • Loading branch information
plafer committed Jun 5, 2024
1 parent fde1c72 commit f9a1d0d
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion assembly/src/assembler/span_builder.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use super::{AssemblyContext, BodyWrapper, Decorator, DecoratorList, Instruction};
use alloc::{borrow::Borrow, string::ToString, vec::Vec};
use vm_core::{code_blocks::CodeBlock, AdviceInjector, AssemblyOp, Operation};
use vm_core::{code_blocks::CodeBlock, mast::MastNode, AdviceInjector, AssemblyOp, Operation};

// SPAN BUILDER
// ================================================================================================

// TODOP: Rename `BasicBlockBuilder`
/// A helper struct for constructing SPAN blocks while compiling procedure bodies.
///
/// Operations and decorators can be added to a span builder via various `add_*()` and `push_*()`
Expand Down Expand Up @@ -144,4 +145,37 @@ impl SpanBuilder {
self.ops.append(&mut self.epilogue);
self.extract_span_into(target);
}

/// Creates and returns a new BASIC BLOCK node from the operations and decorators currently in
/// this builder. If the builder is empty, then no node is created and `None` is returned.
///
/// This consumes all operations and decorators in the builder, but does not touch the
/// operations in the epilogue of the builder.
pub fn to_basic_block(&mut self) -> Option<MastNode> {

Check warning on line 154 in assembly/src/assembler/span_builder.rs

View workflow job for this annotation

GitHub Actions / Check Rust stable on ubuntu with --all-targets

methods `to_basic_block` and `into_basic_block` are never used

Check warning on line 154 in assembly/src/assembler/span_builder.rs

View workflow job for this annotation

GitHub Actions / Check Rust stable on ubuntu with --all-targets --all-features

methods `to_basic_block` and `into_basic_block` are never used

Check warning on line 154 in assembly/src/assembler/span_builder.rs

View workflow job for this annotation

GitHub Actions / Check Rust nightly on ubuntu with --all-targets --no-default-features

methods `to_basic_block` and `into_basic_block` are never used

Check warning on line 154 in assembly/src/assembler/span_builder.rs

View workflow job for this annotation

GitHub Actions / Check Rust nightly on ubuntu with --all-targets

methods `to_basic_block` and `into_basic_block` are never used

Check warning on line 154 in assembly/src/assembler/span_builder.rs

View workflow job for this annotation

GitHub Actions / Check Rust nightly on ubuntu with --all-targets --all-features

methods `to_basic_block` and `into_basic_block` are never used

Check warning on line 154 in assembly/src/assembler/span_builder.rs

View workflow job for this annotation

GitHub Actions / Check Rust stable on ubuntu with --all-targets --no-default-features

methods `to_basic_block` and `into_basic_block` are never used

Check warning on line 154 in assembly/src/assembler/span_builder.rs

View workflow job for this annotation

GitHub Actions / no-std (stable, wasm32-unknown-unknown)

methods `to_basic_block` and `into_basic_block` are never used

Check warning on line 154 in assembly/src/assembler/span_builder.rs

View workflow job for this annotation

GitHub Actions / no-std (nightly, wasm32-unknown-unknown)

methods `to_basic_block` and `into_basic_block` are never used

Check warning on line 154 in assembly/src/assembler/span_builder.rs

View workflow job for this annotation

GitHub Actions / Test Rust stable on ubuntu with --profile test-release --doc

methods `to_basic_block` and `into_basic_block` are never used

Check warning on line 154 in assembly/src/assembler/span_builder.rs

View workflow job for this annotation

GitHub Actions / Test Rust nightly on ubuntu with --profile test-release --doc

methods `to_basic_block` and `into_basic_block` are never used

Check warning on line 154 in assembly/src/assembler/span_builder.rs

View workflow job for this annotation

GitHub Actions / Test Rust stable on ubuntu with --profile test-release

methods `to_basic_block` and `into_basic_block` are never used

Check warning on line 154 in assembly/src/assembler/span_builder.rs

View workflow job for this annotation

GitHub Actions / Test Rust nightly on ubuntu with --profile test-release

methods `to_basic_block` and `into_basic_block` are never used
if !self.ops.is_empty() {
let ops = self.ops.drain(..).collect();
let decorators = self.decorators.drain(..).collect();

Some(MastNode::new_basic_block_with_decorators(ops, decorators))
} else if !self.decorators.is_empty() {
// this is a bug in the assembler. we shouldn't have decorators added without their
// associated operations
// TODO: change this to an error or allow decorators in empty span blocks
unreachable!("decorators in an empty SPAN block")
} else {
None
}
}

/// Creates and returns a new BASIC BLOCK node from the operations and decorators currently in
/// this builder. If the builder is empty, then no node is created and `None` is returned.
///
/// The main differences with [`Self::to_basic_block`] are:
/// - Operations contained in the epilogue of the builder are appended to the list of ops which
/// go into the new BASIC BLOCK node.
/// - The builder is consumed in the process.
pub fn into_basic_block(mut self) -> Option<MastNode> {
self.ops.append(&mut self.epilogue);
self.to_basic_block()
}
}

0 comments on commit f9a1d0d

Please sign in to comment.