Skip to content

Commit

Permalink
Move front::Emitter to proc.
Browse files Browse the repository at this point in the history
With the addition of the constant evaluator to the `proc` module, it
is now concerned with constructing expressions. Any code that
constructs expressions will generally also need to deal with `Emit`
statements, which are handled by the `Emitter` type. However,
`Emitter` is private to the `front` module. This patch moves it to
`proc` and makes it accessible to both the constant evaluator and the
front ends.
  • Loading branch information
jimblandy committed Sep 15, 2023
1 parent c8e3d60 commit 667208f
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 60 deletions.
7 changes: 3 additions & 4 deletions src/front/glsl/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ use super::{
Frontend, Result,
};
use crate::{
front::{Emitter, Typifier},
AddressSpace, Arena, BinaryOperator, Block, Expression, FastHashMap, FunctionArgument, Handle,
Literal, LocalVariable, RelationalFunction, ScalarKind, Span, Statement, Type, TypeInner,
VectorSize,
front::Typifier, proc::Emitter, AddressSpace, Arena, BinaryOperator, Block, Expression,
FastHashMap, FunctionArgument, Handle, Literal, LocalVariable, RelationalFunction, ScalarKind,
Span, Statement, Type, TypeInner, VectorSize,
};
use std::ops::Index;

Expand Down
39 changes: 0 additions & 39 deletions src/front/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,45 +19,6 @@ use crate::{
};
use std::ops;

/// Helper class to emit expressions
#[allow(dead_code)]
#[derive(Default, Debug)]
struct Emitter {
start_len: Option<usize>,
}

#[allow(dead_code)]
impl Emitter {
fn start(&mut self, arena: &Arena<crate::Expression>) {
if self.start_len.is_some() {
unreachable!("Emitting has already started!");
}
self.start_len = Some(arena.len());
}
const fn is_running(&self) -> bool {
self.start_len.is_some()
}
#[must_use]
fn finish(
&mut self,
arena: &Arena<crate::Expression>,
) -> Option<(crate::Statement, crate::span::Span)> {
let start_len = self.start_len.take().unwrap();
if start_len != arena.len() {
#[allow(unused_mut)]
let mut span = crate::span::Span::default();
let range = arena.range_from(start_len);
#[cfg(feature = "span")]
for handle in range.clone() {
span.subsume(arena.get_span(handle))
}
Some((crate::Statement::Emit(range), span))
} else {
None
}
}
}

/// A table of types for an `Arena<Expression>`.
///
/// A front end can use a `Typifier` to get types for an arena's expressions
Expand Down
2 changes: 1 addition & 1 deletion src/front/spv/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
};

use super::{Error, Instruction, LookupExpression, LookupHelper as _};
use crate::front::Emitter;
use crate::proc::Emitter;

pub type BlockId = u32;

Expand Down
8 changes: 4 additions & 4 deletions src/front/spv/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ impl<I: Iterator<Item = u32>> super::Frontend<I> {
&mut self,
words_left: u16,
ctx: &mut super::BlockContext,
emitter: &mut crate::front::Emitter,
emitter: &mut crate::proc::Emitter,
block: &mut crate::Block,
body_idx: usize,
) -> Result<crate::Statement, Error> {
Expand Down Expand Up @@ -315,7 +315,7 @@ impl<I: Iterator<Item = u32>> super::Frontend<I> {
&mut self,
mut words_left: u16,
ctx: &mut super::BlockContext,
emitter: &mut crate::front::Emitter,
emitter: &mut crate::proc::Emitter,
block: &mut crate::Block,
block_id: spirv::Word,
body_idx: usize,
Expand Down Expand Up @@ -415,7 +415,7 @@ impl<I: Iterator<Item = u32>> super::Frontend<I> {
mut words_left: u16,
options: SamplingOptions,
ctx: &mut super::BlockContext,
emitter: &mut crate::front::Emitter,
emitter: &mut crate::proc::Emitter,
block: &mut crate::Block,
block_id: spirv::Word,
body_idx: usize,
Expand Down Expand Up @@ -663,7 +663,7 @@ impl<I: Iterator<Item = u32>> super::Frontend<I> {
&mut self,
at_level: bool,
ctx: &mut super::BlockContext,
emitter: &mut crate::front::Emitter,
emitter: &mut crate::proc::Emitter,
block: &mut crate::Block,
block_id: spirv::Word,
body_idx: usize,
Expand Down
20 changes: 10 additions & 10 deletions src/front/spv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ impl<I: Iterator<Item = u32>> Frontend<I> {
id: spirv::Word,
lookup: &LookupExpression,
ctx: &mut BlockContext,
emitter: &mut super::Emitter,
emitter: &mut crate::proc::Emitter,
block: &mut crate::Block,
body_idx: BodyIndex,
) -> Handle<crate::Expression> {
Expand Down Expand Up @@ -855,7 +855,7 @@ impl<I: Iterator<Item = u32>> Frontend<I> {
fn parse_expr_unary_op(
&mut self,
ctx: &mut BlockContext,
emitter: &mut super::Emitter,
emitter: &mut crate::proc::Emitter,
block: &mut crate::Block,
block_id: spirv::Word,
body_idx: usize,
Expand Down Expand Up @@ -884,7 +884,7 @@ impl<I: Iterator<Item = u32>> Frontend<I> {
fn parse_expr_binary_op(
&mut self,
ctx: &mut BlockContext,
emitter: &mut super::Emitter,
emitter: &mut crate::proc::Emitter,
block: &mut crate::Block,
block_id: spirv::Word,
body_idx: usize,
Expand Down Expand Up @@ -918,7 +918,7 @@ impl<I: Iterator<Item = u32>> Frontend<I> {
fn parse_expr_unary_op_sign_adjusted(
&mut self,
ctx: &mut BlockContext,
emitter: &mut super::Emitter,
emitter: &mut crate::proc::Emitter,
block: &mut crate::Block,
block_id: spirv::Word,
body_idx: usize,
Expand Down Expand Up @@ -973,7 +973,7 @@ impl<I: Iterator<Item = u32>> Frontend<I> {
fn parse_expr_binary_op_sign_adjusted(
&mut self,
ctx: &mut BlockContext,
emitter: &mut super::Emitter,
emitter: &mut crate::proc::Emitter,
block: &mut crate::Block,
block_id: spirv::Word,
body_idx: usize,
Expand Down Expand Up @@ -1051,7 +1051,7 @@ impl<I: Iterator<Item = u32>> Frontend<I> {
fn parse_expr_int_comparison(
&mut self,
ctx: &mut BlockContext,
emitter: &mut super::Emitter,
emitter: &mut crate::proc::Emitter,
block: &mut crate::Block,
block_id: spirv::Word,
body_idx: usize,
Expand Down Expand Up @@ -1122,7 +1122,7 @@ impl<I: Iterator<Item = u32>> Frontend<I> {
fn parse_expr_shift_op(
&mut self,
ctx: &mut BlockContext,
emitter: &mut super::Emitter,
emitter: &mut crate::proc::Emitter,
block: &mut crate::Block,
block_id: spirv::Word,
body_idx: usize,
Expand Down Expand Up @@ -1165,7 +1165,7 @@ impl<I: Iterator<Item = u32>> Frontend<I> {
fn parse_expr_derivative(
&mut self,
ctx: &mut BlockContext,
emitter: &mut super::Emitter,
emitter: &mut crate::proc::Emitter,
block: &mut crate::Block,
block_id: spirv::Word,
body_idx: usize,
Expand Down Expand Up @@ -1296,7 +1296,7 @@ impl<I: Iterator<Item = u32>> Frontend<I> {
})
}

let mut emitter = super::Emitter::default();
let mut emitter = crate::proc::Emitter::default();
emitter.start(ctx.expressions);

// Find the `Body` to which this block contributes.
Expand Down Expand Up @@ -5286,7 +5286,7 @@ fn make_index_literal(
ctx: &mut BlockContext,
index: u32,
block: &mut crate::Block,
emitter: &mut super::Emitter,
emitter: &mut crate::proc::Emitter,
index_type: Handle<crate::Type>,
index_type_id: spirv::Word,
span: crate::Span,
Expand Down
5 changes: 3 additions & 2 deletions src/front/wgsl/lower/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use crate::front::wgsl::error::{Error, ExpectedToken, InvalidAssignmentType};
use crate::front::wgsl::index::Index;
use crate::front::wgsl::parse::number::Number;
use crate::front::wgsl::parse::{ast, conv};
use crate::front::{Emitter, Typifier};
use crate::front::Typifier;
use crate::proc::{
ensure_block_returns, Alignment, ConstantEvaluator, Layouter, ResolveContext, TypeResolution,
ensure_block_returns, Alignment, ConstantEvaluator, Emitter, Layouter, ResolveContext,
TypeResolution,
};
use crate::{Arena, FastHashMap, Handle, Span};
use indexmap::IndexMap;
Expand Down
40 changes: 40 additions & 0 deletions src/proc/emitter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use crate::arena::Arena;

/// Helper class to emit expressions
#[allow(dead_code)]
#[derive(Default, Debug)]
pub struct Emitter {
start_len: Option<usize>,
}

#[allow(dead_code)]
impl Emitter {
pub fn start(&mut self, arena: &Arena<crate::Expression>) {
if self.start_len.is_some() {
unreachable!("Emitting has already started!");
}
self.start_len = Some(arena.len());
}
pub const fn is_running(&self) -> bool {
self.start_len.is_some()
}
#[must_use]
pub fn finish(
&mut self,
arena: &Arena<crate::Expression>,
) -> Option<(crate::Statement, crate::span::Span)> {
let start_len = self.start_len.take().unwrap();
if start_len != arena.len() {
#[allow(unused_mut)]
let mut span = crate::span::Span::default();
let range = arena.range_from(start_len);
#[cfg(feature = "span")]
for handle in range.clone() {
span.subsume(arena.get_span(handle))
}
Some((crate::Statement::Emit(range), span))
} else {
None
}
}
}
2 changes: 2 additions & 0 deletions src/proc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
*/

mod constant_evaluator;
mod emitter;
pub mod index;
mod layouter;
mod namer;
mod terminator;
mod typifier;

pub use constant_evaluator::{ConstantEvaluator, ConstantEvaluatorError};
pub use emitter::Emitter;
pub use index::{BoundsCheckPolicies, BoundsCheckPolicy, IndexableLength, IndexableLengthError};
pub use layouter::{Alignment, LayoutError, LayoutErrorInner, Layouter, TypeLayout};
pub use namer::{EntryPointIndex, NameKey, Namer};
Expand Down

0 comments on commit 667208f

Please sign in to comment.