diff options
-rw-r--r-- | Changelog.md | 1 | ||||
-rw-r--r-- | appveyor.yml | 3 | ||||
-rw-r--r-- | libevmasm/Assembly.cpp | 19 | ||||
-rw-r--r-- | libevmasm/Assembly.h | 11 | ||||
-rw-r--r-- | libevmasm/AssemblyItem.h | 8 | ||||
-rw-r--r-- | libevmasm/ConstantOptimiser.cpp | 5 | ||||
-rw-r--r-- | libevmasm/PeepholeOptimiser.cpp | 23 | ||||
-rw-r--r-- | liblll/CodeFragment.cpp | 7 | ||||
-rw-r--r-- | liblll/Compiler.cpp | 5 | ||||
-rw-r--r-- | libsolidity/codegen/CompilerContext.h | 8 | ||||
-rw-r--r-- | libsolidity/interface/CompilerStack.cpp | 8 | ||||
-rw-r--r-- | libsolidity/interface/CompilerStack.h | 3 | ||||
-rw-r--r-- | scripts/bytecodecompare/storebytecode.bat | 6 | ||||
-rw-r--r-- | solc/CommandLineInterface.cpp | 3 | ||||
-rw-r--r-- | solc/jsonCompiler.cpp | 10 | ||||
-rw-r--r-- | test/liblll/EndToEndTest.cpp | 2 | ||||
-rw-r--r-- | test/libsolidity/SolidityOptimizer.cpp | 26 | ||||
-rw-r--r-- | test/libsolidity/StandardCompiler.cpp | 5 |
18 files changed, 107 insertions, 46 deletions
diff --git a/Changelog.md b/Changelog.md index eed7e258..b2e080a0 100644 --- a/Changelog.md +++ b/Changelog.md @@ -2,6 +2,7 @@ Features: * Assembly: renamed ``SHA3`` to ``KECCAK256``. + * Assembly: Display auxiliary data in the assembly output. * Assembly: Add ``CREATE2`` (EIP86), ``STATICCALL`` (EIP214), ``RETURNDATASIZE`` and ``RETURNDATACOPY`` (EIP211) instructions. * AST: export all attributes to JSON format. * Inline Assembly: Present proper error message when not supplying enough arguments to a functional diff --git a/appveyor.yml b/appveyor.yml index 22ec30a0..55c80a21 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -65,7 +65,8 @@ build_script: - msbuild solidity.sln /p:Configuration=%CONFIGURATION% /m:%NUMBER_OF_PROCESSORS% /v:minimal - cd %APPVEYOR_BUILD_FOLDER% - scripts\release.bat %CONFIGURATION% - - scripts\bytecodecompare\storebytecode.bat %CONFIGURATION% %APPVEYOR_REPO_COMMIT% + - ps: $bytecodedir = git show -s --format="%cd-%H" --date=short + - ps: scripts\bytecodecompare\storebytecode.bat $Env:CONFIGURATION $bytecodedir test_script: - cd %APPVEYOR_BUILD_FOLDER% diff --git a/libevmasm/Assembly.cpp b/libevmasm/Assembly.cpp index ea061a30..92a4c2a4 100644 --- a/libevmasm/Assembly.cpp +++ b/libevmasm/Assembly.cpp @@ -40,7 +40,7 @@ void Assembly::append(Assembly const& _a) auto newDeposit = m_deposit + _a.deposit(); for (AssemblyItem i: _a.m_items) { - if (i.type() == Tag || (i.type() == PushTag && i != errorTag())) + if (i.type() == Tag || i.type() == PushTag) i.setData(i.data() + m_usedTags); else if (i.type() == PushSub || i.type() == PushSubSize) i.setData(i.data() + m_subs.size()); @@ -72,13 +72,6 @@ void Assembly::append(Assembly const& _a, int _deposit) } } -string Assembly::out() const -{ - stringstream ret; - stream(ret); - return ret.str(); -} - unsigned Assembly::bytesRequired(unsigned subTagSize) const { for (unsigned tagSize = subTagSize; true; ++tagSize) @@ -216,6 +209,9 @@ ostream& Assembly::streamAsm(ostream& _out, string const& _prefix, StringMap con } } + if (m_auxiliaryData.size() > 0) + _out << endl << _prefix << "auxdata: 0x" << toHex(m_auxiliaryData) << endl; + return _out; } @@ -315,8 +311,13 @@ Json::Value Assembly::streamAsmJson(ostream& _out, StringMap const& _sourceCodes data[hexStr.str()] = m_subs[i]->stream(_out, "", _sourceCodes, true); } root[".data"] = data; - _out << root; } + + if (m_auxiliaryData.size() > 0) + root[".auxdata"] = toHex(m_auxiliaryData); + + _out << root; + return root; } diff --git a/libevmasm/Assembly.h b/libevmasm/Assembly.h index 528c9e74..13d82e1a 100644 --- a/libevmasm/Assembly.h +++ b/libevmasm/Assembly.h @@ -69,7 +69,13 @@ public: AssemblyItem appendJumpI() { auto ret = append(newPushTag()); append(solidity::Instruction::JUMPI); return ret; } AssemblyItem appendJump(AssemblyItem const& _tag) { auto ret = append(_tag.pushTag()); append(solidity::Instruction::JUMP); return ret; } AssemblyItem appendJumpI(AssemblyItem const& _tag) { auto ret = append(_tag.pushTag()); append(solidity::Instruction::JUMPI); return ret; } - AssemblyItem errorTag() { return AssemblyItem(PushTag, 0); } + + /// Adds a subroutine to the code (in the data section) and pushes its size (via a tag) + /// on the stack. @returns the pushsub assembly item. + AssemblyItem appendSubroutine(AssemblyPointer const& _assembly) { auto sub = newSub(_assembly); append(newPushSubSize(size_t(sub.data()))); return sub; } + void pushSubroutineSize(size_t _subRoutine) { append(newPushSubSize(_subRoutine)); } + /// Pushes the offset of the subroutine. + void pushSubroutineOffset(size_t _subRoutine) { append(AssemblyItem(PushSub, _subRoutine)); } /// Appends @a _data literally to the very end of the bytecode. void appendAuxiliaryDataToEnd(bytes const& _data) { m_auxiliaryData += _data; } @@ -85,10 +91,7 @@ public: void ignored() { m_baseDeposit = m_deposit; } void endIgnored() { m_deposit = m_baseDeposit; m_baseDeposit = 0; } - void popTo(int _deposit) { while (m_deposit > _deposit) append(solidity::Instruction::POP); } - void injectStart(AssemblyItem const& _i); - std::string out() const; int deposit() const { return m_deposit; } void adjustDeposit(int _adjustment) { m_deposit += _adjustment; if (asserts(m_deposit >= 0)) BOOST_THROW_EXCEPTION(InvalidDeposit()); } void setDeposit(int _deposit) { m_deposit = _deposit; if (asserts(m_deposit >= 0)) BOOST_THROW_EXCEPTION(InvalidDeposit()); } diff --git a/libevmasm/AssemblyItem.h b/libevmasm/AssemblyItem.h index 464368fb..d38db927 100644 --- a/libevmasm/AssemblyItem.h +++ b/libevmasm/AssemblyItem.h @@ -148,6 +148,14 @@ private: using AssemblyItems = std::vector<AssemblyItem>; +inline size_t bytesRequired(AssemblyItems const& _items, size_t _addressLength) +{ + size_t size = 0; + for (AssemblyItem const& item: _items) + size += item.bytesRequired(_addressLength); + return size; +} + std::ostream& operator<<(std::ostream& _out, AssemblyItem const& _item); inline std::ostream& operator<<(std::ostream& _out, AssemblyItems const& _items) { diff --git a/libevmasm/ConstantOptimiser.cpp b/libevmasm/ConstantOptimiser.cpp index 0c093ebf..2ecbfa7f 100644 --- a/libevmasm/ConstantOptimiser.cpp +++ b/libevmasm/ConstantOptimiser.cpp @@ -99,10 +99,7 @@ bigint ConstantOptimisationMethod::dataGas(bytes const& _data) const size_t ConstantOptimisationMethod::bytesRequired(AssemblyItems const& _items) { - size_t size = 0; - for (AssemblyItem const& item: _items) - size += item.bytesRequired(3); // assume 3 byte addresses - return size; + return eth::bytesRequired(_items, 3); // assume 3 byte addresses } void ConstantOptimisationMethod::replaceConstants( diff --git a/libevmasm/PeepholeOptimiser.cpp b/libevmasm/PeepholeOptimiser.cpp index 6c92d76b..e94a8ba4 100644 --- a/libevmasm/PeepholeOptimiser.cpp +++ b/libevmasm/PeepholeOptimiser.cpp @@ -136,6 +136,21 @@ struct DoubleSwap: SimplePeepholeOptimizerMethod<DoubleSwap, 2> } }; +struct DoublePush: SimplePeepholeOptimizerMethod<DoublePush, 2> +{ + static bool applySimple(AssemblyItem const& _push1, AssemblyItem const& _push2, std::back_insert_iterator<AssemblyItems> _out) + { + if (_push1.type() == Push && _push2.type() == Push && _push1.data() == _push2.data()) + { + *_out = _push1; + *_out = {Instruction::DUP1, _push2.location()}; + return true; + } + else + return false; + } +}; + struct JumpToNext: SimplePeepholeOptimizerMethod<JumpToNext, 3> { static size_t applySimple( @@ -235,13 +250,15 @@ bool PeepholeOptimiser::optimise() { OptimiserState state {m_items, 0, std::back_inserter(m_optimisedItems)}; while (state.i < m_items.size()) - applyMethods(state, PushPop(), OpPop(), DoubleSwap(), JumpToNext(), UnreachableCode(), TagConjunctions(), Identity()); - if (m_optimisedItems.size() < m_items.size()) + applyMethods(state, PushPop(), OpPop(), DoublePush(), DoubleSwap(), JumpToNext(), UnreachableCode(), TagConjunctions(), Identity()); + if (m_optimisedItems.size() < m_items.size() || ( + m_optimisedItems.size() == m_items.size() && + eth::bytesRequired(m_optimisedItems, 3) < eth::bytesRequired(m_items, 3) + )) { m_items = std::move(m_optimisedItems); return true; } else return false; - } diff --git a/liblll/CodeFragment.cpp b/liblll/CodeFragment.cpp index 0f7f48ba..9f37bc65 100644 --- a/liblll/CodeFragment.cpp +++ b/liblll/CodeFragment.cpp @@ -515,8 +515,7 @@ void CodeFragment::constructOperation(sp::utree const& _t, CompilerState& _s) requireMaxSize(3); requireDeposit(1, 1); - auto subPush = m_asm.newSub(make_shared<Assembly>(code[0].assembly(ns))); - m_asm.append(m_asm.newPushSubSize(subPush.data())); + auto subPush = m_asm.appendSubroutine(make_shared<Assembly>(code[0].assembly(ns))); m_asm.append(Instruction::DUP1); if (code.size() == 3) { @@ -571,7 +570,9 @@ void CodeFragment::constructOperation(sp::utree const& _t, CompilerState& _s) { for (auto const& i: code) m_asm.append(i.m_asm); - m_asm.popTo(1); + // Leave only the last item on stack. + while (m_asm.deposit() > 1) + m_asm.append(Instruction::POP); } else if (us == "BYTECODESIZE") { diff --git a/liblll/Compiler.cpp b/liblll/Compiler.cpp index ea8b27af..05376cd5 100644 --- a/liblll/Compiler.cpp +++ b/liblll/Compiler.cpp @@ -69,10 +69,11 @@ std::string dev::eth::compileLLLToAsm(std::string const& _src, bool _opt, std::v { CompilerState cs; cs.populateStandard(); - string ret = CodeFragment::compile(_src, cs).assembly(cs).optimise(_opt).out(); + stringstream ret; + CodeFragment::compile(_src, cs).assembly(cs).optimise(_opt).stream(ret); for (auto i: cs.treesToKill) killBigints(i); - return ret; + return ret.str(); } catch (Exception const& _e) { diff --git a/libsolidity/codegen/CompilerContext.h b/libsolidity/codegen/CompilerContext.h index c37142c9..030b35a6 100644 --- a/libsolidity/codegen/CompilerContext.h +++ b/libsolidity/codegen/CompilerContext.h @@ -141,8 +141,6 @@ public: CompilerContext& appendInvalid(); /// Appends a conditional INVALID instruction CompilerContext& appendConditionalInvalid(); - /// Returns an "ErrorTag" - eth::AssemblyItem errorTag() { return m_asm->errorTag(); } /// Appends a JUMP to a specific tag CompilerContext& appendJumpTo(eth::AssemblyItem const& _tag) { m_asm->appendJump(_tag); return *this; } /// Appends pushing of a new tag and @returns the new tag. @@ -151,10 +149,10 @@ public: eth::AssemblyItem newTag() { return m_asm->newTag(); } /// Adds a subroutine to the code (in the data section) and pushes its size (via a tag) /// on the stack. @returns the pushsub assembly item. - eth::AssemblyItem addSubroutine(eth::AssemblyPointer const& _assembly) { auto sub = m_asm->newSub(_assembly); m_asm->append(m_asm->newPushSubSize(size_t(sub.data()))); return sub; } - void pushSubroutineSize(size_t _subRoutine) { m_asm->append(m_asm->newPushSubSize(_subRoutine)); } + eth::AssemblyItem addSubroutine(eth::AssemblyPointer const& _assembly) { return m_asm->appendSubroutine(_assembly); } + void pushSubroutineSize(size_t _subRoutine) { m_asm->pushSubroutineSize(_subRoutine); } /// Pushes the offset of the subroutine. - void pushSubroutineOffset(size_t _subRoutine) { m_asm->append(eth::AssemblyItem(eth::PushSub, _subRoutine)); } + void pushSubroutineOffset(size_t _subRoutine) { m_asm->pushSubroutineOffset(_subRoutine); } /// Pushes the size of the final program void appendProgramSize() { m_asm->appendProgramSize(); } /// Adds data to the data section, pushes a reference to the stack diff --git a/libsolidity/interface/CompilerStack.cpp b/libsolidity/interface/CompilerStack.cpp index aca9ce39..b09108b0 100644 --- a/libsolidity/interface/CompilerStack.cpp +++ b/libsolidity/interface/CompilerStack.cpp @@ -481,6 +481,14 @@ Json::Value const& CompilerStack::natspec(Contract const& _contract, Documentati return *(*doc); } +Json::Value CompilerStack::functionHashes(ContractDefinition const& _contract) +{ + Json::Value functionHashes(Json::objectValue); + for (auto const& it: _contract.interfaceFunctions()) + functionHashes[it.second->externalSignature()] = toHex(it.first.ref()); + return functionHashes; +} + string const& CompilerStack::onChainMetadata(string const& _contractName) const { if (m_stackState != CompilationSuccessful) diff --git a/libsolidity/interface/CompilerStack.h b/libsolidity/interface/CompilerStack.h index bffdeabd..3250429b 100644 --- a/libsolidity/interface/CompilerStack.h +++ b/libsolidity/interface/CompilerStack.h @@ -177,6 +177,9 @@ public: /// @param type The type of the documentation to get. /// Can be one of 4 types defined at @c DocumentationType Json::Value const& natspec(std::string const& _contractName, DocumentationType _type) const; + + Json::Value functionHashes(ContractDefinition const& _contract); + std::string const& onChainMetadata(std::string const& _contractName) const; void useMetadataLiteralSources(bool _metadataLiteralSources) { m_metadataLiteralSources = _metadataLiteralSources; } diff --git a/scripts/bytecodecompare/storebytecode.bat b/scripts/bytecodecompare/storebytecode.bat index 969a42a4..aa829d20 100644 --- a/scripts/bytecodecompare/storebytecode.bat +++ b/scripts/bytecodecompare/storebytecode.bat @@ -20,7 +20,7 @@ REM Copyright (c) 2017 solidity contributors. REM --------------------------------------------------------------------------- set CONFIGURATION=%1 -set COMMIT=%2 +set DIRECTORY=%2 mkdir bytecode cd bytecode @@ -33,8 +33,8 @@ git config user.name "travis" git config user.email "chris@ethereum.org" git clean -f -d -x -mkdir %COMMIT% -set REPORT=%COMMIT%/windows.txt +if not exist %DIRECTORY% mkdir %DIRECTORY% +set REPORT=%DIRECTORY%/windows.txt cp ../report.txt %REPORT% git add %REPORT% git commit -a -m "Added report." diff --git a/solc/CommandLineInterface.cpp b/solc/CommandLineInterface.cpp index 58c8bf73..e37922c6 100644 --- a/solc/CommandLineInterface.cpp +++ b/solc/CommandLineInterface.cpp @@ -165,6 +165,7 @@ static set<string> const g_combinedJsonArgs g_strNatspecUser, g_strNatspecDev, g_strOpcodes, + g_strSignatureHashes, g_strSrcMap, g_strSrcMapRuntime }; @@ -887,6 +888,8 @@ void CommandLineInterface::handleCombinedJSON() auto map = m_compiler->runtimeSourceMapping(contractName); contractData[g_strSrcMapRuntime] = map ? *map : ""; } + if (requests.count(g_strSignatureHashes)) + contractData[g_strSignatureHashes] = m_compiler->functionHashes(m_compiler->contractDefinition(contractName)); if (requests.count(g_strNatspecDev)) contractData[g_strNatspecDev] = dev::jsonCompactPrint(m_compiler->natspec(contractName, DocumentationType::NatspecDev)); if (requests.count(g_strNatspecUser)) diff --git a/solc/jsonCompiler.cpp b/solc/jsonCompiler.cpp index 353258a6..c01c8061 100644 --- a/solc/jsonCompiler.cpp +++ b/solc/jsonCompiler.cpp @@ -88,14 +88,6 @@ ReadFile::Callback wrapReadCallback(CStyleReadFileCallback _readCallback = nullp return readCallback; } -Json::Value functionHashes(ContractDefinition const& _contract) -{ - Json::Value functionHashes(Json::objectValue); - for (auto const& it: _contract.interfaceFunctions()) - functionHashes[it.second->externalSignature()] = toHex(it.first.ref()); - return functionHashes; -} - /// Translates a gas value as a string to a JSON number or null Json::Value gasToJson(Json::Value const& _value) { @@ -200,7 +192,7 @@ string compile(StringMap const& _sources, bool _optimize, CStyleReadFileCallback contractData["runtimeBytecode"] = compiler.runtimeObject(contractName).toHex(); contractData["opcodes"] = solidity::disassemble(compiler.object(contractName).bytecode); contractData["metadata"] = compiler.onChainMetadata(contractName); - contractData["functionHashes"] = functionHashes(compiler.contractDefinition(contractName)); + contractData["functionHashes"] = compiler.functionHashes(compiler.contractDefinition(contractName)); contractData["gasEstimates"] = estimateGas(compiler, contractName); auto sourceMap = compiler.sourceMapping(contractName); contractData["srcmap"] = sourceMap ? *sourceMap : ""; diff --git a/test/liblll/EndToEndTest.cpp b/test/liblll/EndToEndTest.cpp index 9021fa43..3928ff45 100644 --- a/test/liblll/EndToEndTest.cpp +++ b/test/liblll/EndToEndTest.cpp @@ -272,7 +272,7 @@ BOOST_AUTO_TEST_CASE(assembly_codecopy) (seq (lit 0x00 "abcdef") (asm - 0x06 0x16 0x20 codecopy + 0x06 6 codesize sub 0x20 codecopy 0x20 0x20 return))) )"; compileAndRun(sourceCode); diff --git a/test/libsolidity/SolidityOptimizer.cpp b/test/libsolidity/SolidityOptimizer.cpp index bdcdacff..7afbe04e 100644 --- a/test/libsolidity/SolidityOptimizer.cpp +++ b/test/libsolidity/SolidityOptimizer.cpp @@ -1189,6 +1189,32 @@ BOOST_AUTO_TEST_CASE(clear_unreachable_code) ); } +BOOST_AUTO_TEST_CASE(peephole_double_push) +{ + AssemblyItems items{ + u256(0), + u256(0), + u256(5), + u256(5), + u256(4), + u256(5) + }; + AssemblyItems expectation{ + u256(0), + Instruction::DUP1, + u256(5), + Instruction::DUP1, + u256(4), + u256(5) + }; + PeepholeOptimiser peepOpt(items); + BOOST_REQUIRE(peepOpt.optimise()); + BOOST_CHECK_EQUAL_COLLECTIONS( + items.begin(), items.end(), + expectation.begin(), expectation.end() + ); +} + BOOST_AUTO_TEST_CASE(computing_constants) { char const* sourceCode = R"( diff --git a/test/libsolidity/StandardCompiler.cpp b/test/libsolidity/StandardCompiler.cpp index 92bb471b..35644a4d 100644 --- a/test/libsolidity/StandardCompiler.cpp +++ b/test/libsolidity/StandardCompiler.cpp @@ -201,11 +201,12 @@ BOOST_AUTO_TEST_CASE(basic_compilation) BOOST_CHECK(dev::test::bytecodeSansMetadata(contract["evm"]["bytecode"]["object"].asString()) == "60606040523415600b57fe5b5b60338060196000396000f30060606040525bfe00"); BOOST_CHECK(contract["evm"]["assembly"].isString()); - BOOST_CHECK(contract["evm"]["assembly"].asString() == + BOOST_CHECK(contract["evm"]["assembly"].asString().find( " /* \"fileA\":0:14 contract A { } */\n mstore(0x40, 0x60)\n jumpi(tag_1, iszero(callvalue))\n" " invalid\ntag_1:\ntag_2:\n dataSize(sub_0)\n dup1\n dataOffset(sub_0)\n 0x0\n codecopy\n 0x0\n" " return\nstop\n\nsub_0: assembly {\n /* \"fileA\":0:14 contract A { } */\n" - " mstore(0x40, 0x60)\n tag_1:\n invalid\n}\n"); + " mstore(0x40, 0x60)\n tag_1:\n invalid\n\n" + " auxdata: 0xa165627a7a72305820") != std::string::npos); BOOST_CHECK(contract["evm"]["gasEstimates"].isObject()); BOOST_CHECK(dev::jsonCompactPrint(contract["evm"]["gasEstimates"]) == "{\"creation\":{\"codeDepositCost\":\"10200\",\"executionCost\":\"62\",\"totalCost\":\"10262\"}}"); |