Skip to content

Commit 3b55aca

Browse files
committed
Revamp storage vectors
1 parent bae49ab commit 3b55aca

File tree

14 files changed

+255
-349
lines changed

14 files changed

+255
-349
lines changed

pintc/src/asm_gen/asm_builder.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,8 @@ impl<'a> AsmBuilder<'a> {
809809
}
810810

811811
match kind {
812+
ExternalIntrinsic::PanicIf => asm.push(PNCIF),
813+
812814
ExternalIntrinsic::RecoverSECP256k1 => asm.push(RSECP),
813815

814816
ExternalIntrinsic::Sha256 => asm.extend([PUSH(3), SHL, SHA2]),

pintc/src/expr/intrinsics.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ pub enum ExternalIntrinsic {
4949
// Returns the address of a predicate in the same contract
5050
AddressOf,
5151

52+
// Panics if supplied condition is `true`
53+
PanicIf,
54+
5255
// Recovers the public key from a secp256k1 signature.
5356
RecoverSECP256k1,
5457

@@ -75,6 +78,7 @@ impl Display for ExternalIntrinsic {
7578
fn fmt(&self, f: &mut Formatter) -> Result {
7679
match self {
7780
Self::AddressOf => write!(f, "__address_of"),
81+
Self::PanicIf => write!(f, "__panic_if"),
7882
Self::RecoverSECP256k1 => write!(f, "__recover_secp256k1"),
7983
Self::Sha256 => write!(f, "__sha256"),
8084
Self::SizeOf => write!(f, "__size_of"),
@@ -92,6 +96,9 @@ impl ExternalIntrinsic {
9296
Self::AddressOf => vec![
9397
string(), // path to a predicate in the contract
9498
],
99+
Self::PanicIf => vec![
100+
r#bool(), // path to a predicate in the contract
101+
],
95102
Self::RecoverSECP256k1 => vec![
96103
b256(), // data hash
97104
tuple(vec![b256(), b256(), int()]), // signature
@@ -116,6 +123,7 @@ impl ExternalIntrinsic {
116123
pub fn ty(&self) -> Type {
117124
match self {
118125
Self::AddressOf => b256(),
126+
Self::PanicIf => any(),
119127
Self::RecoverSECP256k1 => tuple(vec![b256(), int()]),
120128
Self::Sha256 => b256(),
121129
Self::SizeOf => int(),

pintc/src/parser/context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,7 @@ impl<'a> ParserContext<'a> {
495495
kind: (
496496
match &name.name[..] {
497497
"__address_of" => IntrinsicKind::External(ExternalIntrinsic::AddressOf),
498+
"__panic_if" => IntrinsicKind::External(ExternalIntrinsic::PanicIf),
498499
"__recover_secp256k1" => {
499500
IntrinsicKind::External(ExternalIntrinsic::RecoverSECP256k1)
500501
}

pintc/src/parser/tests.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,7 @@ fn storage_types() {
264264
// Multi dimensional vectors are not yet supported
265265
check(
266266
&run_parser!(storage_var_type, "int[][]"),
267-
expect_test::expect![[r#"
268-
expected `?`, found `[`
269-
@18..19: expected `?`
270-
"#]],
267+
expect_test::expect!["int[][]"],
271268
);
272269
}
273270

pintc/src/pint_parser.lalrpop

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -548,11 +548,12 @@ MapType: Type = {
548548
}
549549
}
550550

551-
// This only allows single dimensional vectors for the time being
552551
VectorType: Type = {
553-
<l:@L> <ty:TypeAtom> "[" "]" <r:@R> => Type::Vector {
554-
ty: Box::new(ty),
555-
span: (context.span_from)(l, r),
552+
<l:@L> <ty:TypeAtom> <ranges: ("[" "]")+> <r:@R> => {
553+
ranges.iter().rev().fold(ty, |acc, _| Type::Vector {
554+
ty: Box::new(acc),
555+
span: (context.span_from)(l, r),
556+
})
556557
}
557558
}
558559

pintc/src/predicate/transform.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
mod legalize;
21
mod lower;
32
mod unroll;
43
mod validate;
54

65
use crate::error::{ErrorEmitted, Handler};
7-
use legalize::legalize_vector_accesses;
86
use lower::{
97
coalesce_prime_ops, lower_aliases, lower_array_ranges, lower_casts, lower_ifs,
108
lower_imm_accesses, lower_ins, lower_matches, lower_storage_accesses,
@@ -80,11 +78,8 @@ impl super::Contract {
8078
// (e.g., `option::none`) from Expr::Path to Expr::UnionVariant.
8179
lower_union_variant_paths(&mut self);
8280

83-
// Insert OOB checks for storage vector accesses
84-
let _ = legalize_vector_accesses(handler, &mut self);
85-
86-
// Lower all storage accesses to __storage_get and __storage_get_extern intrinsics. Also
87-
// add constraints on mutable keys
81+
// Lower all storage accesses to storage intrinsics (`__pre_state`, `__pre_state_extern`,
82+
// `_post_state`, and `__post_state_extern`). Also add constraints on mutable keys
8883
let _ = lower_storage_accesses(handler, &mut self);
8984

9085
// Ensure that the final contract is indeed final

pintc/src/predicate/transform/legalize.rs

Lines changed: 0 additions & 289 deletions
This file was deleted.

0 commit comments

Comments
 (0)