Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alignment overhaul #13

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ All notable changes to this project will be documented in this file.

- `Type::size()` can now correctly calculate the size of aggregate types
([#12](https://github.com/garritfra/qbe-rs/pull/12)).
- `Type::align()` helper function to calculate alignment of types
([#13](https://github.com/garritfra/qbe-rs/pull/13)).

### Changed

- `Type::Aggregate` now takes a `TypeDef` instead of the name of a type
([#12](https://github.com/garritfra/qbe-rs/pull/12)).
- `Instr::Alloc{4,8,16}(size)` were replaced by a single
`Instr::Alloc(size, alignment)`
([#13](https://github.com/garritfra/qbe-rs/pull/13)).

## [2.0.0] - 2022-03-10

Expand Down
42 changes: 31 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,8 @@ pub enum Instr<'a> {
Jmp(String),
/// Calls a function
Call(String, Vec<(Type<'a>, Value)>),
/// Allocates a 4-byte aligned area on the stack
Alloc4(u32),
/// Allocates a 8-byte aligned area on the stack
Alloc8(u64),
/// Allocates a 16-byte aligned area on the stack
Alloc16(u128),
/// Allocates an area on the stack
Alloc(u64, usize),
/// Stores a value into memory pointed to by destination.
/// `(type, destination, value)`
Store(Type<'a>, Value, Value),
Expand Down Expand Up @@ -124,9 +120,13 @@ impl<'a> fmt::Display for Instr<'a> {
.join(", "),
)
}
Self::Alloc4(size) => write!(f, "alloc4 {}", size),
Self::Alloc8(size) => write!(f, "alloc8 {}", size),
Self::Alloc16(size) => write!(f, "alloc16 {}", size),
Self::Alloc(size, align) => {
assert!(
*align == 4 || *align == 8 || *align == 16,
"Invalid stack alignment"
);
write!(f, "alloc{} {}", align, size)
Comment on lines +124 to +128
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really a fan of this, as it moves the error from compile-time to run-time, which we should avoid.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, we have to provide some way to get the corresponding allocation instruction by giving an alignment from Type.align() or similar

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But what keeps you from writing qbe::Alloc(5, 0), which would result in a runtime error?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative would be to have Alloc(Type, Align) but that diverges too much from the original QBE IL. Let's just keep this as is.🙂

}
Self::Store(ty, dest, value) => {
if matches!(ty, Type::Aggregate(_)) {
unimplemented!("Store to an aggregate type");
Expand Down Expand Up @@ -181,6 +181,23 @@ impl<'a> Type<'a> {
}
}

/// Returns alignment of the type
pub fn align(&self) -> u64 {
match self {
Self::Byte => 1,
Self::Halfword => 2,
Self::Word | Self::Single => 4,
Self::Long | Self::Double => 8,
Self::Aggregate(td) => match td.align {
Some(align) => align,
None => {
assert!(!td.items.is_empty(), "Invalid empty TypeDef");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really needed? Are typedefs required to have at least one item?

Copy link
Contributor Author

@YerinAlexey YerinAlexey Jun 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, absolutely. The only possible way to get empty typedef is not-yet-supported opaque typedefs, but they require explicit alignment.

td.items.iter().map(|(ty, _)| ty.align()).max().unwrap()
}
},
}
}

/// Returns byte size for values of the type
pub fn size(&self) -> u64 {
match self {
Expand All @@ -189,10 +206,13 @@ impl<'a> Type<'a> {
Self::Word | Self::Single => 4,
Self::Long | Self::Double => 8,
Self::Aggregate(td) => {
// TODO: correct for alignment
let align = self.align();
let mut sz = 0_u64;
for (item, repeat) in td.items.iter() {
sz += item.size() * (*repeat as u64);
// https://en.wikipedia.org/wiki/Data_structure_alignment
let itemsz = item.size();
let aligned = itemsz + (align - itemsz % align) % align;
sz += aligned * (*repeat as u64);
}
sz
}
Expand Down
53 changes: 23 additions & 30 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,47 +109,40 @@ fn typedef() {
}

#[test]
fn type_size() {
assert!(Type::Byte.size() == 1);
assert!(Type::Halfword.size() == 2);
assert!(Type::Word.size() == 4);
assert!(Type::Single.size() == 4);
assert!(Type::Long.size() == 8);
assert!(Type::Double.size() == 8);

let typedef = TypeDef {
fn type_size_and_align() {
assert_eq!(Type::Byte.size(), 1);
assert_eq!(Type::Halfword.size(), 2);
assert_eq!(Type::Word.size(), 4);
assert_eq!(Type::Single.size(), 4);
assert_eq!(Type::Long.size(), 8);
assert_eq!(Type::Double.size(), 8);

let person = TypeDef {
name: "person".into(),
align: None,
items: vec![(Type::Long, 1), (Type::Word, 2), (Type::Byte, 1)],
};
let aggregate = Type::Aggregate(&typedef);
assert!(aggregate.size() == 17);
}
let aggregate = Type::Aggregate(&person);
assert_eq!(aggregate.align(), 8);
assert_eq!(aggregate.size(), 32);

#[test]
fn type_size_nested_aggregate() {
let inner = TypeDef {
name: "dog".into(),
let typedef = TypeDef {
name: "nested_person".into(),
align: None,
items: vec![(Type::Long, 2)],
items: vec![(Type::Word, 1), (Type::Aggregate(&person), 1)],
};
let inner_aggregate = Type::Aggregate(&inner);

assert!(inner_aggregate.size() == 16);
let aggregate = Type::Aggregate(&typedef);
assert_eq!(aggregate.align(), 8);
assert_eq!(aggregate.size(), 40);

let typedef = TypeDef {
name: "person".into(),
align: None,
items: vec![
(Type::Long, 1),
(Type::Word, 2),
(Type::Byte, 1),
(Type::Aggregate(&inner), 1),
],
name: "packed_person".into(),
align: Some(1),
items: vec![(Type::Long, 1), (Type::Word, 2), (Type::Byte, 1)],
};
let aggregate = Type::Aggregate(&typedef);

assert!(aggregate.size() == 33);
assert_eq!(aggregate.align(), 1);
assert_eq!(aggregate.size(), 17);
}

#[test]
Expand Down