diff options
author | chriseth <c@ethdev.com> | 2015-06-06 01:22:30 +0800 |
---|---|---|
committer | chriseth <c@ethdev.com> | 2015-06-06 01:22:30 +0800 |
commit | 4141a0de5edc933fbed912dda92eb50a0e0aa90f (patch) | |
tree | 2cbeb2031b5cb5808aaf323c5350722d7c1cbe65 | |
parent | 7aff1fb942c7a9b5a5fa04e886336db737337d0d (diff) | |
parent | 4111cdf376de08ae45a2e0ca4105132fa3f8985f (diff) | |
download | dexon-solidity-4141a0de5edc933fbed912dda92eb50a0e0aa90f.tar.gz dexon-solidity-4141a0de5edc933fbed912dda92eb50a0e0aa90f.tar.zst dexon-solidity-4141a0de5edc933fbed912dda92eb50a0e0aa90f.zip |
Merge pull request #2084 from chriseth/sol_computeConstants
Compute constants
-rw-r--r-- | libsolidity/SolidityOptimizer.cpp | 45 | ||||
-rw-r--r-- | libsolidity/solidityExecutionFramework.h | 3 |
2 files changed, 47 insertions, 1 deletions
diff --git a/libsolidity/SolidityOptimizer.cpp b/libsolidity/SolidityOptimizer.cpp index 827d8833..4ed081fd 100644 --- a/libsolidity/SolidityOptimizer.cpp +++ b/libsolidity/SolidityOptimizer.cpp @@ -1036,6 +1036,51 @@ BOOST_AUTO_TEST_CASE(block_deduplicator_loops) BOOST_CHECK_EQUAL(pushTags.size(), 1); } +BOOST_AUTO_TEST_CASE(computing_constants) +{ + char const* sourceCode = R"( + contract c { + uint a; + uint b; + uint c; + function set() returns (uint a, uint b, uint c) { + a = 0x77abc0000000000000000000000000000000000000000000000000000000001; + b = 0x817416927846239487123469187231298734162934871263941234127518276; + g(); + } + function g() { + b = 0x817416927846239487123469187231298734162934871263941234127518276; + c = 0x817416927846239487123469187231298734162934871263941234127518276; + } + function get() returns (uint ra, uint rb, uint rc) { + ra = a; + rb = b; + rc = c ; + } + } + )"; + compileBothVersions(sourceCode); + compareVersions("set()"); + compareVersions("get()"); + + m_optimize = true; + m_optimizeRuns = 1; + bytes optimizedBytecode = compileAndRun(sourceCode, 0, "c"); + bytes complicatedConstant = toBigEndian(u256("0x817416927846239487123469187231298734162934871263941234127518276")); + unsigned occurrences = 0; + for (auto iter = optimizedBytecode.cbegin(); iter < optimizedBytecode.cend(); ++occurrences) + iter = search(iter, optimizedBytecode.cend(), complicatedConstant.cbegin(), complicatedConstant.cend()) + 1; + BOOST_CHECK_EQUAL(2, occurrences); + + bytes constantWithZeros = toBigEndian(u256("0x77abc0000000000000000000000000000000000000000000000000000000001")); + BOOST_CHECK(search( + optimizedBytecode.cbegin(), + optimizedBytecode.cend(), + constantWithZeros.cbegin(), + constantWithZeros.cend() + ) == optimizedBytecode.cend()); +} + BOOST_AUTO_TEST_SUITE_END() } diff --git a/libsolidity/solidityExecutionFramework.h b/libsolidity/solidityExecutionFramework.h index 1e11ae90..e81b4d7b 100644 --- a/libsolidity/solidityExecutionFramework.h +++ b/libsolidity/solidityExecutionFramework.h @@ -46,7 +46,7 @@ public: { m_compiler.reset(false, m_addStandardSources); m_compiler.addSource("", _sourceCode); - ETH_TEST_REQUIRE_NO_THROW(m_compiler.compile(m_optimize), "Compiling contract failed"); + ETH_TEST_REQUIRE_NO_THROW(m_compiler.compile(m_optimize, m_optimizeRuns), "Compiling contract failed"); bytes code = m_compiler.getBytecode(_contractName); sendMessage(code, true, _value); return m_output; @@ -180,6 +180,7 @@ protected: m_logs = executive.logs(); } + size_t m_optimizeRuns = 200; bool m_optimize = false; bool m_addStandardSources = false; dev::solidity::CompilerStack m_compiler; |