Skip to content

wit-parser/sizealign: Introduce format() variant with optional brackets around the sum for codegen #2048

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

Merged
merged 1 commit into from
Feb 11, 2025
Merged
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
30 changes: 24 additions & 6 deletions crates/wit-parser/src/sizealign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,20 +180,38 @@ impl ArchitectureSize {

// create a suitable expression in bytes from a pointer size argument
pub fn format(&self, ptrsize_expr: &str) -> String {
self.format_term(ptrsize_expr, false)
}

// create a suitable expression in bytes from a pointer size argument,
// extended API with optional brackets around the sum
pub fn format_term(&self, ptrsize_expr: &str, suppress_brackets: bool) -> String {
if self.pointers != 0 {
if self.bytes > 0 {
// both
format!(
"({}+{}*{ptrsize_expr})",
self.constant_bytes(),
self.pointers_to_add()
)
if suppress_brackets {
format!(
"{}+{}*{ptrsize_expr}",
self.constant_bytes(),
self.pointers_to_add()
)
} else {
format!(
"({}+{}*{ptrsize_expr})",
self.constant_bytes(),
self.pointers_to_add()
)
}
} else if self.pointers == 1 {
// one pointer
ptrsize_expr.into()
} else {
// only pointer
format!("({}*{ptrsize_expr})", self.pointers_to_add())
if suppress_brackets {
format!("{}*{ptrsize_expr}", self.pointers_to_add())
} else {
format!("({}*{ptrsize_expr})", self.pointers_to_add())
}
}
} else {
// only bytes
Expand Down