From 98d8174d31f70c425c3c9f5ced3dac0edad703b4 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Tue, 17 Nov 2020 16:50:42 +0000 Subject: [PATCH] Simplify Compiler/CompilerContext by removing extra layers of indirection --- libsolidity/codegen/Compiler.h | 22 +++---------------- libsolidity/codegen/CompilerContext.cpp | 7 ------ libsolidity/codegen/CompilerContext.h | 15 ------------- libsolidity/interface/CompilerStack.cpp | 15 +++++++------ test/libsolidity/Assembly.cpp | 2 +- .../SolidityExpressionCompiler.cpp | 6 ++++- 6 files changed, 17 insertions(+), 50 deletions(-) diff --git a/libsolidity/codegen/Compiler.h b/libsolidity/codegen/Compiler.h index 4dbbeb099..6e6fd968e 100644 --- a/libsolidity/codegen/Compiler.h +++ b/libsolidity/codegen/Compiler.h @@ -51,28 +51,12 @@ public: ); /// @returns Entire assembly. evmasm::Assembly const& assembly() const { return m_context.assembly(); } + /// @returns Runtime assembly. + evmasm::Assembly const& runtimeAssembly() const { return m_context.assembly().sub(m_runtimeSub); } /// @returns Entire assembly as a shared pointer to non-const. std::shared_ptr assemblyPtr() const { return m_context.assemblyPtr(); } - /// @returns Runtime assembly. + /// @returns Runtime assembly as a shared pointer. std::shared_ptr runtimeAssemblyPtr() const; - /// @returns The entire assembled object (with constructor). - evmasm::LinkerObject assembledObject() const { return m_context.assembledObject(); } - /// @returns Only the runtime object (without constructor). - evmasm::LinkerObject runtimeObject() const { return m_context.assembledRuntimeObject(m_runtimeSub); } - /// @arg _sourceCodes is the map of input files to source code strings - std::string assemblyString(StringMap const& _sourceCodes = StringMap()) const - { - return m_context.assemblyString(_sourceCodes); - } - /// @arg _sourceCodes is the map of input files to source code strings - Json::Value assemblyJSON(std::map const& _indices = std::map()) const - { - return m_context.assemblyJSON(_indices); - } - /// @returns Assembly items of the normal compiler context - evmasm::AssemblyItems const& assemblyItems() const { return m_context.assembly().items(); } - /// @returns Assembly items of the runtime compiler context - evmasm::AssemblyItems const& runtimeAssemblyItems() const { return m_context.assembly().sub(m_runtimeSub).items(); } std::string generatedYulUtilityCode() const { return m_context.generatedYulUtilityCode(); } std::string runtimeGeneratedYulUtilityCode() const { return m_runtimeContext.generatedYulUtilityCode(); } diff --git a/libsolidity/codegen/CompilerContext.cpp b/libsolidity/codegen/CompilerContext.cpp index dcee585be..0600dfb3a 100644 --- a/libsolidity/codegen/CompilerContext.cpp +++ b/libsolidity/codegen/CompilerContext.cpp @@ -548,13 +548,6 @@ void CompilerContext::optimizeYul(yul::Object& _object, yul::EVMDialect const& _ #endif } -LinkerObject const& CompilerContext::assembledObject() const -{ - LinkerObject const& object = m_asm->assemble(); - solAssert(object.immutableReferences.empty(), "Leftover immutables."); - return object; -} - string CompilerContext::revertReasonIfDebug(string const& _message) { return YulUtilFunctions::revertReasonIfDebug(m_revertStrings, _message); diff --git a/libsolidity/codegen/CompilerContext.h b/libsolidity/codegen/CompilerContext.h index d32349e89..ddb68c912 100644 --- a/libsolidity/codegen/CompilerContext.h +++ b/libsolidity/codegen/CompilerContext.h @@ -287,21 +287,6 @@ public: /// Should be avoided except when adding sub-assemblies. std::shared_ptr assemblyPtr() const { return m_asm; } - /// @arg _sourceCodes is the map of input files to source code strings - std::string assemblyString(StringMap const& _sourceCodes = StringMap()) const - { - return m_asm->assemblyString(_sourceCodes); - } - - /// @arg _sourceCodes is the map of input files to source code strings - Json::Value assemblyJSON(std::map const& _indicies = std::map()) const - { - return m_asm->assemblyJSON(_indicies); - } - - evmasm::LinkerObject const& assembledObject() const; - evmasm::LinkerObject const& assembledRuntimeObject(size_t _subIndex) const { return m_asm->sub(_subIndex).assemble(); } - /** * Helper class to pop the visited nodes stack when a scope closes */ diff --git a/libsolidity/interface/CompilerStack.cpp b/libsolidity/interface/CompilerStack.cpp index 90c809e13..bab844751 100644 --- a/libsolidity/interface/CompilerStack.cpp +++ b/libsolidity/interface/CompilerStack.cpp @@ -627,7 +627,7 @@ evmasm::AssemblyItems const* CompilerStack::assemblyItems(string const& _contrac BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Compilation was not successful.")); Contract const& currentContract = contract(_contractName); - return currentContract.compiler ? &contract(_contractName).compiler->assemblyItems() : nullptr; + return currentContract.compiler ? &contract(_contractName).compiler->assembly()->items() : nullptr; } evmasm::AssemblyItems const* CompilerStack::runtimeAssemblyItems(string const& _contractName) const @@ -636,7 +636,7 @@ evmasm::AssemblyItems const* CompilerStack::runtimeAssemblyItems(string const& _ BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Compilation was not successful.")); Contract const& currentContract = contract(_contractName); - return currentContract.compiler ? &contract(_contractName).compiler->runtimeAssemblyItems() : nullptr; + return currentContract.compiler ? &contract(_contractName).compiler->runtimeAssembly()->items() : nullptr; } Json::Value CompilerStack::generatedSources(string const& _contractName, bool _runtime) const @@ -791,7 +791,7 @@ string CompilerStack::assemblyString(string const& _contractName, StringMap _sou Contract const& currentContract = contract(_contractName); if (currentContract.compiler) - return currentContract.compiler->assemblyString(_sourceCodes); + return currentContract.compiler->assembly().assemblyString(_sourceCodes); else return string(); } @@ -804,7 +804,7 @@ Json::Value CompilerStack::assemblyJSON(string const& _contractName) const Contract const& currentContract = contract(_contractName); if (currentContract.compiler) - return currentContract.compiler->assemblyJSON(sourceIndices()); + return currentContract.compiler->assembly().assemblyJSON(sourceIndices()); else return Json::Value(); } @@ -977,7 +977,7 @@ size_t CompilerStack::functionEntryPoint( evmasm::AssemblyItem tag = compiler->functionEntryLabel(_function); if (tag.type() == evmasm::UndefinedItem) return 0; - evmasm::AssemblyItems const& items = compiler->runtimeAssemblyItems(); + evmasm::AssemblyItems const& items = compiler->runtimeAssembly().items(); for (size_t i = 0; i < items.size(); ++i) if (items.at(i).type() == evmasm::Tag && items.at(i).data() == tag.data()) return i; @@ -1203,17 +1203,18 @@ void CompilerStack::compileContract( try { // Assemble deployment (incl. runtime) object. - compiledContract.object = compiler->assembledObject(); + compiledContract.object = compiler->assembly().assemble(); } catch(evmasm::AssemblyException const&) { solAssert(false, "Assembly exception for bytecode"); } + solAssert(compiledContract.object.immutableReferences.empty(), "Leftover immutables."); try { // Assemble runtime object. - compiledContract.runtimeObject = compiler->runtimeObject(); + compiledContract.runtimeObject = compiler->runtimeAssembly().assemble(); } catch(evmasm::AssemblyException const&) { diff --git a/test/libsolidity/Assembly.cpp b/test/libsolidity/Assembly.cpp index 4b599da68..676f63313 100644 --- a/test/libsolidity/Assembly.cpp +++ b/test/libsolidity/Assembly.cpp @@ -91,7 +91,7 @@ evmasm::AssemblyItems compileContract(std::shared_ptr _sourceCode) ); compiler.compileContract(*contract, map>{}, bytes()); - return compiler.runtimeAssemblyItems(); + return compiler.runtimeAssembly().items(); } BOOST_FAIL("No contract found in source."); return AssemblyItems(); diff --git a/test/libsolidity/SolidityExpressionCompiler.cpp b/test/libsolidity/SolidityExpressionCompiler.cpp index d5644a38d..15a67e6fe 100644 --- a/test/libsolidity/SolidityExpressionCompiler.cpp +++ b/test/libsolidity/SolidityExpressionCompiler.cpp @@ -35,6 +35,7 @@ #include #include #include +#include #include #include @@ -160,7 +161,10 @@ bytes compileFirstExpression( context << context.functionEntryLabel(dynamic_cast( resolveDeclaration(*sourceUnit, function, resolver) )); - bytes instructions = context.assembledObject().bytecode; + BOOST_REQUIRE(context.assemblyPtr()); + LinkerObject const& object = context.assemblyPtr()->assemble(); + BOOST_REQUIRE(object.immutableReferences.empty()); + bytes instructions = object.bytecode; // debug // cout << evmasm::disassemble(instructions) << endl; return instructions;