From 29f6aa7d560a7d82a9088489e663a079a3b41f73 Mon Sep 17 00:00:00 2001 From: chriseth Date: Wed, 16 Jan 2019 11:44:11 +0100 Subject: Do not create a copy of the assembly. --- libsolidity/codegen/Compiler.cpp | 6 ++++++ libsolidity/codegen/Compiler.h | 4 +++- libsolidity/codegen/CompilerContext.cpp | 8 ++++---- libsolidity/codegen/CompilerContext.h | 12 ++++++------ libsolidity/codegen/CompilerUtils.cpp | 4 ++-- libsolidity/codegen/ContractCompiler.cpp | 2 +- 6 files changed, 22 insertions(+), 14 deletions(-) (limited to 'libsolidity') diff --git a/libsolidity/codegen/Compiler.cpp b/libsolidity/codegen/Compiler.cpp index f0eaca4f..72efed33 100644 --- a/libsolidity/codegen/Compiler.cpp +++ b/libsolidity/codegen/Compiler.cpp @@ -47,6 +47,12 @@ void Compiler::compileContract( m_context.optimise(m_optimize, m_optimizeRuns); } +std::shared_ptr Compiler::runtimeAssemblyPtr() const +{ + solAssert(m_context.runtimeContext(), ""); + return m_context.runtimeContext()->assemblyPtr(); +} + eth::AssemblyItem Compiler::functionEntryLabel(FunctionDefinition const& _function) const { return m_runtimeContext.functionEntryLabelIfExists(_function); diff --git a/libsolidity/codegen/Compiler.h b/libsolidity/codegen/Compiler.h index 953267a4..c21de96d 100644 --- a/libsolidity/codegen/Compiler.h +++ b/libsolidity/codegen/Compiler.h @@ -50,8 +50,10 @@ public: ); /// @returns Entire assembly. eth::Assembly const& assembly() const { return m_context.assembly(); } + /// @returns Entire assembly as a shared pointer to non-const. + std::shared_ptr assemblyPtr() const { return m_context.assemblyPtr(); } /// @returns Runtime assembly. - eth::Assembly const& runtimeAssembly() const { return m_context.assembly().sub(m_runtimeSub); } + std::shared_ptr runtimeAssemblyPtr() const; /// @returns The entire assembled object (with constructor). eth::LinkerObject assembledObject() const { return m_context.assembledObject(); } /// @returns Only the runtime object (without constructor). diff --git a/libsolidity/codegen/CompilerContext.cpp b/libsolidity/codegen/CompilerContext.cpp index 20e1af7c..861b1c98 100644 --- a/libsolidity/codegen/CompilerContext.cpp +++ b/libsolidity/codegen/CompilerContext.cpp @@ -167,18 +167,18 @@ unsigned CompilerContext::numberOfLocalVariables() const return m_localVariables.size(); } -eth::Assembly const& CompilerContext::compiledContract(ContractDefinition const& _contract) const +shared_ptr CompilerContext::compiledContract(ContractDefinition const& _contract) const { auto ret = m_otherCompilers.find(&_contract); solAssert(ret != m_otherCompilers.end(), "Compiled contract not found."); - return ret->second->assembly(); + return ret->second->assemblyPtr(); } -eth::Assembly const& CompilerContext::compiledContractRuntime(ContractDefinition const& _contract) const +shared_ptr CompilerContext::compiledContractRuntime(ContractDefinition const& _contract) const { auto ret = m_otherCompilers.find(&_contract); solAssert(ret != m_otherCompilers.end(), "Compiled contract not found."); - return ret->second->runtimeAssembly(); + return ret->second->runtimeAssemblyPtr(); } bool CompilerContext::isLocalVariable(Declaration const* _declaration) const diff --git a/libsolidity/codegen/CompilerContext.h b/libsolidity/codegen/CompilerContext.h index 43e1ea77..e5ddfbc5 100644 --- a/libsolidity/codegen/CompilerContext.h +++ b/libsolidity/codegen/CompilerContext.h @@ -76,8 +76,8 @@ public: unsigned numberOfLocalVariables() const; void setOtherCompilers(std::map> const& _otherCompilers) { m_otherCompilers = _otherCompilers; } - eth::Assembly const& compiledContract(ContractDefinition const& _contract) const; - eth::Assembly const& compiledContractRuntime(ContractDefinition const& _contract) const; + std::shared_ptr compiledContract(ContractDefinition const& _contract) const; + std::shared_ptr compiledContractRuntime(ContractDefinition const& _contract) const; void setStackOffset(int _offset) { m_asm->setDeposit(_offset); } void adjustStackOffset(int _adjustment) { m_asm->adjustDeposit(_adjustment); } @@ -224,15 +224,15 @@ public: void optimise(bool _fullOptimsation, unsigned _runs = 200) { m_asm->optimise(_fullOptimsation, m_evmVersion, true, _runs); } /// @returns the runtime context if in creation mode and runtime context is set, nullptr otherwise. - CompilerContext* runtimeContext() { return m_runtimeContext; } + CompilerContext* runtimeContext() const { return m_runtimeContext; } /// @returns the identifier of the runtime subroutine. size_t runtimeSub() const { return m_runtimeSub; } /// @returns a const reference to the underlying assembly. eth::Assembly const& assembly() const { return *m_asm; } - /// @returns non-const reference to the underlying assembly. Should be avoided in favour of - /// wrappers in this class. - eth::Assembly& nonConstAssembly() { return *m_asm; } + /// @returns a shared pointer to the assembly. + /// 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 diff --git a/libsolidity/codegen/CompilerUtils.cpp b/libsolidity/codegen/CompilerUtils.cpp index 9b7244ba..6cfb0777 100644 --- a/libsolidity/codegen/CompilerUtils.cpp +++ b/libsolidity/codegen/CompilerUtils.cpp @@ -1209,12 +1209,12 @@ void CompilerUtils::copyContractCodeToMemory(ContractDefinition const& contract, [&contract, _creation](CompilerContext& _context) { // copy the contract's code into memory - eth::Assembly const& assembly = + shared_ptr assembly = _creation ? _context.compiledContract(contract) : _context.compiledContractRuntime(contract); // pushes size - auto subroutine = _context.addSubroutine(make_shared(assembly)); + auto subroutine = _context.addSubroutine(assembly); _context << Instruction::DUP1 << subroutine; _context << Instruction::DUP4 << Instruction::CODECOPY; _context << Instruction::ADD; diff --git a/libsolidity/codegen/ContractCompiler.cpp b/libsolidity/codegen/ContractCompiler.cpp index a718603b..f843e07a 100644 --- a/libsolidity/codegen/ContractCompiler.cpp +++ b/libsolidity/codegen/ContractCompiler.cpp @@ -716,7 +716,7 @@ bool ContractCompiler::visit(InlineAssembly const& _inlineAssembly) CodeGenerator::assemble( _inlineAssembly.operations(), *_inlineAssembly.annotation().analysisInfo, - m_context.nonConstAssembly(), + *m_context.assemblyPtr(), identifierAccess ); m_context.setStackOffset(startStackHeight); -- cgit