Sizing processor

This commit is contained in:
Dzmitry Malyshau
2020-12-18 01:11:47 -05:00
committed by Dzmitry Malyshau
parent b95346877a
commit c0dfb247b6
3 changed files with 68 additions and 0 deletions

View File

@@ -4,6 +4,7 @@
mod call_graph;
mod interface;
mod namer;
mod sizer;
mod terminator;
mod typifier;
mod validator;
@@ -12,6 +13,7 @@ mod validator;
pub use call_graph::{CallGraph, CallGraphBuilder};
pub use interface::{Interface, Visitor};
pub use namer::{EntryPointIndex, NameKey, Namer};
pub use sizer::Sizer;
pub use terminator::ensure_block_returns;
pub use typifier::{check_constant_type, ResolveContext, ResolveError, Typifier};
pub use validator::{ValidationError, Validator};

65
src/proc/sizer.rs Normal file
View File

@@ -0,0 +1,65 @@
use crate::arena::Arena;
/// Helper processor that derives the sizes of all types.
#[derive(Debug)]
pub struct Sizer {
sizes: Vec<u32>,
}
impl Sizer {
pub fn new(types: &Arena<crate::Type>, constants: &Arena<crate::Constant>) -> Self {
use crate::TypeInner as Ti;
let mut sizes = Vec::with_capacity(types.len());
for (_, ty) in types.iter() {
sizes.push(match ty.inner {
Ti::Scalar { kind: _, width } => width as u32,
Ti::Vector {
size,
kind: _,
width,
} => (size as u8 * width) as u32,
Ti::Matrix {
columns,
rows,
width,
} => (columns as u8 * rows as u8 * width) as u32,
Ti::Pointer { .. } => 0,
Ti::Array { base, size, stride } => {
let count = match size {
crate::ArraySize::Constant(handle) => match constants[handle].inner {
crate::ConstantInner::Uint(value) => value as u32,
ref other => unreachable!("Unexpected array size {:?}", other),
},
crate::ArraySize::Dynamic => 1,
};
let stride = match stride {
Some(value) => value.get(),
None => sizes[base.index()],
};
count * stride
}
Ti::Struct {
block: _,
ref members,
} => {
let mut total = 0;
for member in members {
total += match member.span {
Some(span) => span.get(),
None => sizes[member.ty.index()],
};
}
total
}
Ti::Image { .. } | Ti::Sampler { .. } => 0,
});
}
Sizer { sizes }
}
pub fn resolve(&self, handle: crate::Handle<crate::Type>) -> u32 {
self.sizes[handle.index()]
}
}

View File

@@ -36,6 +36,7 @@ impl Clone for Resolution {
}
}
/// Helper processor that derives the types of all expressions.
#[derive(Debug)]
pub struct Typifier {
resolutions: Vec<Resolution>,