diff --git a/crates/pyrometer/tests/test_data/logical.sol b/crates/pyrometer/tests/test_data/logical.sol index fa969cc2..b297d8c7 100644 --- a/crates/pyrometer/tests/test_data/logical.sol +++ b/crates/pyrometer/tests/test_data/logical.sol @@ -1,33 +1,34 @@ // SPDX-License-Identifier: MIT or APACHE2 pragma solidity ^0.8.0; -enum MyEnum { - A, - B, - C -} + +// enum MyEnum { +// A, +// B, +// C +// } contract Logical { - function enumCmp() public pure returns (bool) { - return MyEnum.A > MyEnum.B; - } + // function enumCmp() public pure returns (bool) { + // return MyEnum.A > MyEnum.B; + // } - function yulCmp() internal pure { - uint x; - uint y; - assembly { - x := gt(2, 3) - y := eq(2, 3) - } - } + // function yulCmp() internal pure { + // uint x; + // uint y; + // assembly { + // x := gt(2, 3) + // y := eq(2, 3) + // } + // } - function yulComplicatedCmp(address a) internal { - bool success; - /// @solidity memory-safe-assembly - assembly { - success := and(eq(a, 0), call(gas(), a, 4, 5, 6, 7, 8)) //error - } - require(success); - } + // function yulComplicatedCmp(address a) internal { + // bool success; + // /// @solidity memory-safe-assembly + // assembly { + // success := and(eq(a, 0), call(gas(), a, 4, 5, 6, 7, 8)) //error + // } + // require(success); + // } function or(address a) internal virtual { assembly { @@ -39,75 +40,75 @@ contract Logical { } } - function eq(address a) public pure { - assembly { - if eq(0x0, a) { + // function eq(address a) public pure { + // assembly { + // if eq(0x0, a) { - } - } - } + // } + // } + // } - function not() public pure { - uint256 a = 100; - bool s = a < 100; - require(!s); - } + // function not() public pure { + // uint256 a = 100; + // bool s = a < 100; + // require(!s); + // } - function cond_not(uint256 a) public pure { - bool s = a < 100; - if (!s) { - require(!s); - } else { - require(s); - } - } + // function cond_not(uint256 a) public pure { + // bool s = a < 100; + // if (!s) { + // require(!s); + // } else { + // require(s); + // } + // } - function cond_and(bool a, bool b) public pure { - if (a && b) { - require(a); - require(b); - bool f = a && b; - require(f); - } else { - require(!a || !b); - } - } + // function cond_and(bool a, bool b) public pure { + // if (a && b) { + // require(a); + // require(b); + // bool f = a && b; + // require(f); + // } else { + // require(!a || !b); + // } + // } - function cond_if(uint256 a) public pure { - bool s = a < 100; - if (s) { - require(s); - } else { - require(!s); - } - } + // function cond_if(uint256 a) public pure { + // bool s = a < 100; + // if (s) { + // require(s); + // } else { + // require(!s); + // } + // } - function and() public pure { - uint256 a = 100; - uint256 b = 1000; - bool s = a > 99; - bool t = b > 999; - require(s && t); - } + // function and() public pure { + // uint256 a = 100; + // uint256 b = 1000; + // bool s = a > 99; + // bool t = b > 999; + // require(s && t); + // } - function or_basic() public pure { - uint256 a = 100; - uint256 b = 1000; - bool s = a > 99; - bool t = b < 1000; - require(s || t); - } + // function or_basic() public pure { + // uint256 a = 100; + // uint256 b = 1000; + // bool s = a > 99; + // bool t = b < 1000; + // require(s || t); + // } - function or() public pure { - uint256 a = 100; - uint256 b = 1000; - bool s = a > 99 || b < 1000; - require(s); - } + // function or() public pure { + // uint256 a = 100; + // uint256 b = 1000; + // bool s = a > 99 || b < 1000; + // require(s); + // } - function or_inline() public pure { - uint256 a = 100; - uint256 b = 1000; - require(a > 99 || b < 1000); - } + // function or_inline() public pure { + // uint256 a = 100; + // uint256 b = 1000; + // require(a > 99 || b < 1000); + // } } diff --git a/crates/shared/src/flattened.rs b/crates/shared/src/flattened.rs index ed65d6de..8761fdf9 100644 --- a/crates/shared/src/flattened.rs +++ b/crates/shared/src/flattened.rs @@ -399,8 +399,8 @@ impl FlatExpr { | ArrayLiteral(loc, ..) => Some(*loc), FunctionCallName(..) - | YulExpr(FlatYulExpr::YulStartBlock) - | YulExpr(FlatYulExpr::YulEndBlock) => None, + | YulExpr(FlatYulExpr::YulStartBlock(_)) + | YulExpr(FlatYulExpr::YulEndBlock(_)) => None, } } } diff --git a/crates/shared/src/flattened_yul.rs b/crates/shared/src/flattened_yul.rs index 261d3e23..a07cb3ab 100644 --- a/crates/shared/src/flattened_yul.rs +++ b/crates/shared/src/flattened_yul.rs @@ -6,14 +6,14 @@ use solang_parser::pt::{ #[derive(Debug, Clone, Copy, Eq, PartialEq)] pub enum FlatYulExpr { - YulStartBlock, + YulStartBlock(usize), YulVariable(Loc, &'static str), YulFuncCall(Loc, &'static str, usize), YulSuffixAccess(Loc, &'static str), YulAssign(Loc, usize), YulVarDecl(Loc, usize, bool), YulFuncDef(Loc, &'static str, usize), - YulEndBlock, + YulEndBlock(usize), } impl std::fmt::Display for FlatYulExpr { diff --git a/crates/solc-expressions/src/context_builder/flattened.rs b/crates/solc-expressions/src/context_builder/flattened.rs index bb89e873..89e2ccfd 100644 --- a/crates/solc-expressions/src/context_builder/flattened.rs +++ b/crates/solc-expressions/src/context_builder/flattened.rs @@ -269,9 +269,14 @@ pub trait Flatten: flags: _, block: yul_block, } => { - self.push_expr(FlatExpr::YulExpr(FlatYulExpr::YulStartBlock)); + self.increment_asm_block(); + self.push_expr(FlatExpr::YulExpr(FlatYulExpr::YulStartBlock( + self.current_asm_block(), + ))); self.traverse_yul_statement(&YulStatement::Block(yul_block.clone())); - self.push_expr(FlatExpr::YulExpr(FlatYulExpr::YulEndBlock)); + self.push_expr(FlatExpr::YulExpr(FlatYulExpr::YulEndBlock( + self.current_asm_block(), + ))); } Return(loc, maybe_ret_expr) => { if let Some(ret_expr) = maybe_ret_expr { @@ -414,11 +419,16 @@ pub trait Flatten: YulStatement::VariableDeclaration(def.loc, def.returns.clone(), None); self.traverse_yul_statement(&rets_as_var_decl); - self.push_expr(FlatExpr::YulExpr(FlatYulExpr::YulStartBlock)); + self.increment_asm_block(); + self.push_expr(FlatExpr::YulExpr(FlatYulExpr::YulStartBlock( + self.current_asm_block(), + ))); for stmt in def.body.statements.iter() { self.traverse_yul_statement(stmt); } - self.push_expr(FlatExpr::YulExpr(FlatYulExpr::YulEndBlock)); + self.push_expr(FlatExpr::YulExpr(FlatYulExpr::YulEndBlock( + self.current_asm_block(), + ))); let func = self.expr_stack_mut().drain(start_len..).collect::>(); @@ -1046,8 +1056,10 @@ pub trait Flatten: Try { .. } => todo!(), // Yul - YulExpr(FlatYulExpr::YulStartBlock) => { - self.increment_asm_block(); + YulExpr(FlatYulExpr::YulStartBlock(s)) => { + if self.current_asm_block() < s { + self.increment_asm_block(); + } Ok(()) } YulExpr(yul @ FlatYulExpr::YulVariable(..)) => self.interp_yul_var(arena, ctx, yul), @@ -1062,8 +1074,10 @@ pub trait Flatten: YulExpr(yul @ FlatYulExpr::YulVarDecl(..)) => { self.interp_yul_var_decl(arena, ctx, stack, yul, parse_idx) } - YulExpr(FlatYulExpr::YulEndBlock) => { - self.decrement_asm_block(); + YulExpr(FlatYulExpr::YulEndBlock(s)) => { + if self.current_asm_block() > s { + self.decrement_asm_block(); + } Ok(()) } }?;