diff options
author | Alex Beregszaszi <alex@rtfs.hu> | 2017-04-27 23:26:08 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-04-27 23:26:08 +0800 |
commit | 2c8b77006255b15743efd0b54964227bccc07c57 (patch) | |
tree | 17cb0166bb6290aeb125d07e4247e57d58203b64 /libevmasm/ConstantOptimiser.cpp | |
parent | 122dc65b3608b368897701dbdebc689bc614933f (diff) | |
parent | 0e91b8fb98ee942aeb668ef5ff1c6e40c52a7a2e (diff) | |
download | dexon-solidity-2c8b77006255b15743efd0b54964227bccc07c57.tar dexon-solidity-2c8b77006255b15743efd0b54964227bccc07c57.tar.gz dexon-solidity-2c8b77006255b15743efd0b54964227bccc07c57.tar.bz2 dexon-solidity-2c8b77006255b15743efd0b54964227bccc07c57.tar.lz dexon-solidity-2c8b77006255b15743efd0b54964227bccc07c57.tar.xz dexon-solidity-2c8b77006255b15743efd0b54964227bccc07c57.tar.zst dexon-solidity-2c8b77006255b15743efd0b54964227bccc07c57.zip |
Merge pull request #2187 from ethereum/testOptimizer
Add recomputation check for number representation.
Diffstat (limited to 'libevmasm/ConstantOptimiser.cpp')
-rw-r--r-- | libevmasm/ConstantOptimiser.cpp | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/libevmasm/ConstantOptimiser.cpp b/libevmasm/ConstantOptimiser.cpp index a1dfd21c..d2ed4faf 100644 --- a/libevmasm/ConstantOptimiser.cpp +++ b/libevmasm/ConstantOptimiser.cpp @@ -232,6 +232,54 @@ AssemblyItems ComputeMethod::findRepresentation(u256 const& _value) } } +bool ComputeMethod::checkRepresentation(u256 const& _value, AssemblyItems const& _routine) +{ + // This is a tiny EVM that can only evaluate some instructions. + vector<u256> stack; + for (AssemblyItem const& item: _routine) + { + switch (item.type()) + { + case Operation: + { + if (stack.size() < size_t(item.arguments())) + return false; + u256* sp = &stack.back(); + switch (item.instruction()) + { + case Instruction::MUL: + sp[-1] = sp[0] * sp[-1]; + break; + case Instruction::EXP: + if (sp[-1] > 0xff) + return false; + sp[-1] = boost::multiprecision::pow(sp[0], unsigned(sp[-1])); + break; + case Instruction::ADD: + sp[-1] = sp[0] + sp[-1]; + break; + case Instruction::SUB: + sp[-1] = sp[0] - sp[-1]; + break; + case Instruction::NOT: + sp[0] = ~sp[0]; + break; + default: + return false; + } + stack.resize(stack.size() + item.deposit()); + break; + } + case Push: + stack.push_back(item.data()); + break; + default: + return false; + } + } + return stack.size() == 1 && stack.front() == _value; +} + bigint ComputeMethod::gasNeeded(AssemblyItems const& _routine) { size_t numExps = count(_routine.begin(), _routine.end(), Instruction::EXP); |