Skip to content

Commit

Permalink
feat: add offsets to the layout.
Browse files Browse the repository at this point in the history
  • Loading branch information
rzvxa committed Jul 30, 2024
1 parent f336121 commit d84a2db
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
4 changes: 4 additions & 0 deletions tasks/ast_codegen/src/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,12 @@ impl From<&REnum> for EnumDef {
fn from(it @ REnum { item, meta }: &REnum) -> Self {
let (size_64, align_64) = meta
.layout_64
.clone()
.layout()
.map_or_else(|| panic!("Uncalculated layout on {}!", item.ident), KnownLayout::unpack);
let (size_32, align_32) = meta
.layout_32
.clone()
.layout()
.map_or_else(|| panic!("Uncalculated layout on {}!", item.ident), KnownLayout::unpack);
Self {
Expand All @@ -100,10 +102,12 @@ impl From<&RStruct> for StructDef {
fn from(it @ RStruct { item, meta }: &RStruct) -> Self {
let (size_64, align_64) = meta
.layout_64
.clone()
.layout()
.map_or_else(|| panic!("Uncalculated layout on {}!", item.ident), KnownLayout::unpack);
let (size_32, align_32) = meta
.layout_32
.clone()
.layout()
.map_or_else(|| panic!("Uncalculated layout on {}!", item.ident), KnownLayout::unpack);
Self {
Expand Down
15 changes: 11 additions & 4 deletions tasks/ast_codegen/src/layout.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[derive(Debug, Default, Clone, Copy)]
#[derive(Debug, Default, Clone)]
pub enum Layout {
#[default]
Unknown,
Expand All @@ -7,7 +7,7 @@ pub enum Layout {

impl Layout {
pub const fn known(size: usize, align: usize, niches: u128) -> Self {
Self::Layout(KnownLayout { size, align, niches })
Self::Layout(KnownLayout { size, align, niches, offsets: None })
}

pub fn layout(self) -> Option<KnownLayout> {
Expand All @@ -25,18 +25,20 @@ impl From<KnownLayout> for Layout {
}
}

#[derive(Debug, Default, Clone, Copy)]
#[derive(Debug, Default, Clone)]
pub struct KnownLayout {
size: usize,
align: usize,
/// number of available niches
niches: u128,
offsets: Option<Vec<usize>>,
}

impl KnownLayout {
pub const fn new(size: usize, align: usize, niches: u128) -> Self {
Self { size, align, niches }
Self { size, align, niches, offsets: None }
}

#[inline]
pub fn size(&self) -> usize {
self.size
Expand Down Expand Up @@ -65,6 +67,11 @@ impl KnownLayout {
self.niches = niches;
}

pub fn with_offsets(mut self, offsets: Vec<usize>) -> Self {
self.offsets = Some(offsets);
self
}

/// Panics
/// if doesn't have enough viable space and `can_resize` is false
pub fn consume_niches(&mut self, n: u128, can_resize: bool) {
Expand Down
15 changes: 4 additions & 11 deletions tasks/ast_codegen/src/passes/calc_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,6 @@ impl PlatformLayout {
Self(Layout::wide_ptr_64(), Layout::wide_ptr_32())
}

fn x64(&self) -> Layout {
self.0
}

fn x32(&self) -> Layout {
self.1
}

/// Return `true` if either of platform layouts is unknown.
fn is_unknown(&self) -> bool {
self.0.is_unknown() || self.1.is_unknown()
Expand Down Expand Up @@ -86,7 +78,8 @@ pub fn calc_layout(ty: &mut RType, ctx: &CodegenCtx) -> Result<bool> {
if layout.is_unknown() {
Ok(false)
} else {
ty.set_layout(layout.x64(), layout.x32())?;
let PlatformLayout(x64, x32) = layout;
ty.set_layout(x64, x32)?;
Ok(true)
}
}
Expand All @@ -98,7 +91,7 @@ fn calc_enum_layout(ty: &mut REnum, ctx: &CodegenCtx) -> Result<PlatformLayout>
// all AST enums are `repr(C)` so it would have a 4 byte layout/alignment,
let layout = KnownLayout::new(0, 4, 0);
let layout = Layout::Layout(layout);
Ok(vec![PlatformLayout(layout, layout)])
Ok(vec![PlatformLayout(layout.clone(), layout)])
} else {
ty.item
.variants
Expand Down Expand Up @@ -181,7 +174,7 @@ fn calc_struct_layout(ty: &mut RStruct, ctx: &CodegenCtx) -> Result<PlatformLayo
niches += layout.niches();
}
let output = output.pad_to_align();
Ok(KnownLayout::new(output.size(), output.align(), niches))
Ok(KnownLayout::new(output.size(), output.align(), niches).with_offsets(offsets))
}

let layouts = collect_field_layouts(ty, ctx)?;
Expand Down
8 changes: 4 additions & 4 deletions tasks/ast_codegen/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,16 +177,16 @@ impl RType {

pub fn layout_32(&self) -> Result<Layout> {
match self {
RType::Enum(it) => Ok(it.meta.layout_32),
RType::Struct(it) => Ok(it.meta.layout_32),
RType::Enum(it) => Ok(it.meta.layout_32.clone()),
RType::Struct(it) => Ok(it.meta.layout_32.clone()),
_ => Err("Unsupported type!".to_string()),
}
}

pub fn layout_64(&self) -> Result<Layout> {
match self {
RType::Enum(it) => Ok(it.meta.layout_64),
RType::Struct(it) => Ok(it.meta.layout_64),
RType::Enum(it) => Ok(it.meta.layout_64.clone()),
RType::Struct(it) => Ok(it.meta.layout_64.clone()),
_ => Err("Unsupported type!".to_string()),
}
}
Expand Down

0 comments on commit d84a2db

Please sign in to comment.