generated from stacks-network/.github
-
Notifications
You must be signed in to change notification settings - Fork 19
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
BowTiedWoo
wants to merge
4
commits into
main
Choose a base branch
from
feat/list-of-literals-at-compile-time
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a369070
feat: write literal values at compile-time
BowTiedWoo 62d93c0
Merge branch 'main' into feat/list-of-literals-at-compile-time
BowTiedWoo dd154f8
Merge branch 'main' into feat/list-of-literals-at-compile-time
BowTiedWoo c53f8f1
Merge branch 'main' into feat/list-of-literals-at-compile-time
BowTiedWoo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
use clarity::vm::clarity_wasm::get_type_size; | ||
use clarity::vm::types::{ | ||
FunctionType, ListTypeData, SequenceSubtype, StringSubtype, TypeSignature, | ||
FunctionType, ListTypeData, PrincipalData, SequenceData, SequenceSubtype, StringSubtype, | ||
TypeSignature, | ||
}; | ||
use clarity::vm::{ClarityName, SymbolicExpression}; | ||
use clarity::vm::{ClarityName, SymbolicExpression, Value}; | ||
use walrus::ir::{self, BinaryOp, IfElse, InstrSeqType, Loop, UnaryOp}; | ||
use walrus::ValType; | ||
|
||
|
@@ -43,30 +44,82 @@ impl ComplexWord for ListCons { | |
ty | ||
))); | ||
}; | ||
// Check if all elements are literals | ||
let all_literals = list.iter().all(|expr| { | ||
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(_)) | ||
) | ||
}); | ||
|
||
// Allocate space on the data stack for the entire list | ||
let (offset, _size) = generator.create_call_stack_local(builder, &ty, false, true); | ||
|
||
// Loop through the expressions in the list and store them onto the | ||
// data stack. | ||
let mut total_size = 0; | ||
for expr in list.iter() { | ||
// WORKAROUND: if you have a list like `(list (some 1) none)`, even if the list elements have type | ||
// `optional int`, the typechecker will give NoType to `none`. | ||
// This means that the placeholder will be represented with a different number of `ValType`, and will | ||
// cause errors (example: function called with wrong number of arguments). | ||
// While we wait for a real fix in the typechecker, here is a workaround to set all the elements types. | ||
generator.set_expr_type(expr, elem_ty.clone())?; | ||
|
||
generator.traverse_expr(builder, expr)?; | ||
// Write this element to memory | ||
let elem_size = generator.write_to_memory(builder, offset, total_size, elem_ty)?; | ||
total_size += elem_size; | ||
if all_literals { | ||
let mut literal_data = Vec::new(); | ||
|
||
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(), | ||
)) | ||
} | ||
} | ||
} | ||
Comment on lines
+63
to
+99
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should use |
||
let (offset, len) = generator.add_bytes_literal(&literal_data)?; | ||
builder.i32_const(offset as i32).i32_const(len as i32); | ||
} else { | ||
let (offset, _size) = generator.create_call_stack_local(builder, &ty, false, true); | ||
// Loop through the expressions in the list and store them onto the | ||
// data stack. | ||
let mut total_size = 0; | ||
for expr in list.iter() { | ||
// WORKAROUND: if you have a list like `(list (some 1) none)`, even if the list elements have type | ||
// `optional int`, the typechecker will give NoType to `none`. | ||
// This means that the placeholder will be represented with a different number of `ValType`, and will | ||
// cause errors (example: function called with wrong number of arguments). | ||
// While we wait for a real fix in the typechecker, here is a workaround to set all the elements types. | ||
generator.set_expr_type(expr, elem_ty.clone())?; | ||
|
||
generator.traverse_expr(builder, expr)?; | ||
// Write this element to memory | ||
let elem_size = generator.write_to_memory(builder, offset, total_size, elem_ty)?; | ||
total_size += elem_size; | ||
} | ||
builder.local_get(offset).i32_const(total_size as i32); | ||
} | ||
|
||
// Push the offset and size to the data stack | ||
builder.local_get(offset).i32_const(total_size as i32); | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 areLiteralValue
and check if all elements inlist
have this property.