use alloc::string::String;
use crate::{Layout, LayoutError, Scalar, StructLayout, Type, UnionLayout};
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TargetLayout {
pointer: Layout,
scalar_alignments: [u64; 10],
}
impl TargetLayout {
pub fn builder() -> TargetLayoutBuilder {
TargetLayoutBuilder::default()
}
pub fn wasm32() -> Self {
Self::builder()
.pointer_size(4)
.pointer_align(4)
.build()
.expect("valid wasm32 preset")
}
pub fn x86_64() -> Self {
Self::builder()
.pointer_size(8)
.pointer_align(8)
.build()
.expect("valid x86-64 preset")
}
pub const fn pointer_layout(&self) -> Layout {
self.pointer
}
pub fn scalar_layout(&self, scalar: Scalar) -> Layout {
Layout::new(scalar.size(), self.scalar_alignments[scalar.index()])
.expect("target builder validated scalar alignment")
}
pub fn layout_of(&self, ty: &Type) -> Result<Layout, LayoutError> {
if let Some(scalar) = ty.scalar() {
return Ok(self.scalar_layout(scalar));
}
match ty {
Type::Pointer => Ok(self.pointer),
Type::Array(element, count) => Ok(self.layout_of(element)?.repeat(*count)?.0),
Type::Struct(fields) => {
let mut layout = Layout::EMPTY;
for field in fields {
layout = layout.extend(self.layout_of(field)?)?.0;
}
layout.pad_to_align()
}
Type::Union(fields) => {
let mut size = 0;
let mut align = 1;
for field in fields {
let layout = self.layout_of(field)?;
size = size.max(layout.size());
align = align.max(layout.align());
}
Layout::new(size, align)?.pad_to_align()
}
_ => unreachable!("scalar types were handled above"),
}
}
pub fn struct_layout<I, N>(&self, fields: I) -> Result<StructLayout, LayoutError>
where
I: IntoIterator<Item = (N, Type)>,
N: Into<String>,
{
let mut builder = StructLayout::builder();
for (name, ty) in fields {
builder = builder.field(name, self.layout_of(&ty)?);
}
builder.build()
}
pub fn union_layout<I, N>(&self, fields: I) -> Result<UnionLayout, LayoutError>
where
I: IntoIterator<Item = (N, Type)>,
N: Into<String>,
{
let mut builder = UnionLayout::builder();
for (name, ty) in fields {
builder = builder.field(name, self.layout_of(&ty)?);
}
builder.build()
}
}
#[derive(Clone, Debug)]
#[must_use]
pub struct TargetLayoutBuilder {
pointer_size: Option<u64>,
pointer_align: Option<u64>,
scalar_alignments: [u64; 10],
}
impl Default for TargetLayoutBuilder {
fn default() -> Self {
Self {
pointer_size: None,
pointer_align: None,
scalar_alignments: [1, 2, 4, 8, 1, 2, 4, 8, 4, 8],
}
}
}
impl TargetLayoutBuilder {
pub fn pointer_size(mut self, size: u64) -> Self {
self.pointer_size = Some(size);
self
}
pub fn pointer_align(mut self, align: u64) -> Self {
self.pointer_align = Some(align);
self
}
pub fn scalar_alignment(mut self, scalar: Scalar, align: u64) -> Self {
self.scalar_alignments[scalar.index()] = align;
self
}
pub fn build(self) -> Result<TargetLayout, LayoutError> {
let size = self.pointer_size.ok_or(LayoutError::MissingPointerLayout)?;
let align = self
.pointer_align
.ok_or(LayoutError::MissingPointerLayout)?;
let pointer = validate_type(size, align)?;
for scalar in Scalar::ALL {
validate_type(scalar.size(), self.scalar_alignments[scalar.index()])?;
}
Ok(TargetLayout {
pointer,
scalar_alignments: self.scalar_alignments,
})
}
}
fn validate_type(size: u64, align: u64) -> Result<Layout, LayoutError> {
let layout = Layout::new(size, align)?;
if size == 0 || size % align != 0 {
return Err(LayoutError::InvalidTypeSize { size, align });
}
Ok(layout)
}