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

Write literal values to memory at compile-time #605

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

BowTiedWoo
Copy link
Collaborator

This PR introduces a feature where lists containing only literal elements, such as (list 1 2 3), are no longer constructed at runtime. Instead, the compiler now identifies these lists and computes their memory representation during compile-time.

Closes: #587

@BowTiedWoo BowTiedWoo requested a review from Acaccia January 23, 2025 16:13
Copy link

codecov bot commented Jan 23, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 85.61%. Comparing base (18d3d73) to head (c53f8f1).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #605      +/-   ##
==========================================
- Coverage   85.65%   85.61%   -0.05%     
==========================================
  Files          46       46              
  Lines       24554    24590      +36     
  Branches    24554    24590      +36     
==========================================
+ Hits        21032    21052      +20     
- Misses       1672     1681       +9     
- Partials     1850     1857       +7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Comment on lines +63 to +99
for expr in list.iter() {
match expr.match_literal_value() {
Some(Value::Int(i)) => literal_data.extend_from_slice(&i.to_le_bytes()),
Some(Value::UInt(u)) => literal_data.extend_from_slice(&u.to_le_bytes()),
Some(Value::Bool(b)) => literal_data.extend_from_slice(&[*b as u8]),
Some(Value::Sequence(SequenceData::String(string_data))) => {
let (offset, len) = generator.add_clarity_string_literal(string_data)?;
literal_data.extend_from_slice(&offset.to_le_bytes());
literal_data.extend_from_slice(&len.to_le_bytes());
}
Some(Value::Sequence(SequenceData::Buffer(buffer))) => {
let (offset, len) = generator
.add_literal(&Value::Sequence(SequenceData::Buffer(buffer.clone())))?;
literal_data.extend_from_slice(&offset.to_le_bytes());
literal_data.extend_from_slice(&len.to_le_bytes());
}
Some(Value::Principal(PrincipalData::Standard(standard))) => {
let (offset, len) = generator.add_literal(&Value::Principal(
PrincipalData::Standard(standard.clone()),
))?;
literal_data.extend_from_slice(&offset.to_le_bytes());
literal_data.extend_from_slice(&len.to_le_bytes());
}
Some(Value::Principal(PrincipalData::Contract(contract))) => {
let (offset, len) = generator.add_literal(&Value::Principal(
PrincipalData::Contract(contract.clone()),
))?;
literal_data.extend_from_slice(&offset.to_le_bytes());
literal_data.extend_from_slice(&len.to_le_bytes());
}
_ => {
return Err(GeneratorError::TypeError(
"Unsupported literal type in list".to_owned(),
))
}
}
}
Copy link
Collaborator

@Acaccia Acaccia Jan 27, 2025

Choose a reason for hiding this comment

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

I think we should use write_to_wasm() here. The function already exists and is heavily tested.

Comment on lines +49 to +57
matches!(
expr.match_literal_value(),
Some(Value::Int(_))
| Some(Value::UInt(_))
| Some(Value::Bool(_))
| Some(Value::Sequence(SequenceData::String(_)))
| Some(Value::Sequence(SequenceData::Buffer(_)))
| Some(Value::Principal(_))
)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a good start, but what about (list {a: 1} {a: 2}) or (list (list 1) (list 2))?
I think the easiest thing to do here would be to create a function that checks if all the leaves in a SymbolicExpression tree are LiteralValue and check if all elements in list have this property.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create lists of literals at compile time
2 participants