From 0f09b850fc6d97a841a62579cf41d4bf8bf010f3 Mon Sep 17 00:00:00 2001 From: Daniel Kirchner Date: Wed, 8 Sep 2021 12:51:13 +0200 Subject: [PATCH] Use index into m_asm->items() to modify free memory push. --- libevmasm/AssemblyItem.h | 3 --- libsolidity/codegen/CompilerContext.cpp | 18 ++++++++++++++++-- libsolidity/codegen/CompilerContext.h | 10 +++------- libsolidity/codegen/CompilerUtils.cpp | 2 +- libyul/optimiser/OptimiserStep.h | 2 +- libyul/optimiser/Suite.cpp | 2 +- libyul/optimiser/Suite.h | 4 ++-- 7 files changed, 24 insertions(+), 17 deletions(-) diff --git a/libevmasm/AssemblyItem.h b/libevmasm/AssemblyItem.h index 89dd6b2be..f1b2b5043 100644 --- a/libevmasm/AssemblyItem.h +++ b/libevmasm/AssemblyItem.h @@ -62,9 +62,6 @@ public: AssemblyItem(u256 _push, langutil::SourceLocation _location = langutil::SourceLocation()): AssemblyItem(Push, std::move(_push), std::move(_location)) { } - /// Used only for the free memory pointer as "memoryguard". Should probably be replaced by a separate AssemblyItemType. - AssemblyItem(std::shared_ptr _pushData, langutil::SourceLocation _location = langutil::SourceLocation()): - m_type(Push), m_data(std::move(_pushData)), m_location(std::move(_location)) { } AssemblyItem(Instruction _i, langutil::SourceLocation _location = langutil::SourceLocation()): m_type(Operation), m_instruction(_i), diff --git a/libsolidity/codegen/CompilerContext.cpp b/libsolidity/codegen/CompilerContext.cpp index 7d87fa08e..e3857a901 100644 --- a/libsolidity/codegen/CompilerContext.cpp +++ b/libsolidity/codegen/CompilerContext.cpp @@ -545,7 +545,13 @@ void CompilerContext::optimizeYul( bool const isCreation = runtimeContext() != nullptr; yul::GasMeter meter(_dialect, isCreation, _optimiserSettings.expectedExecutionsPerDeployment); - solAssert(m_freeMemoryInitPush, ""); + unique_ptr freeMemoryInitPushValue; + if (_system) + { + solAssert(m_freeMemoryInitPush, ""); + solAssert(m_asm->items().size() > *m_freeMemoryInitPush, ""); + freeMemoryInitPushValue = make_unique(m_asm->items().at(*m_freeMemoryInitPush).data()); + } yul::OptimiserSuite::run( _dialect, &meter, @@ -554,8 +560,10 @@ void CompilerContext::optimizeYul( _optimiserSettings.yulOptimiserSteps, isCreation? nullopt : make_optional(_optimiserSettings.expectedExecutionsPerDeployment), _externalIdentifiers, - _system ? m_freeMemoryInitPush : shared_ptr{} + freeMemoryInitPushValue.get() ); + if (_system) + m_asm->items().at(*m_freeMemoryInitPush).setData(*freeMemoryInitPushValue); #ifdef SOL_OUTPUT_ASM cout << "After optimizer:" << endl; @@ -572,6 +580,12 @@ string CompilerContext::revertReasonIfDebug(string const& _message) ); } +void CompilerContext::appendFreeMemoryInitPush(u256 _value) +{ + m_freeMemoryInitPush = m_asm->items().size(); + m_asm->append(AssemblyItem(_value)); +} + void CompilerContext::updateSourceLocation() { m_asm->setSourceLocation(m_visitedNodes.empty() ? SourceLocation() : m_visitedNodes.top()->location()); diff --git a/libsolidity/codegen/CompilerContext.h b/libsolidity/codegen/CompilerContext.h index 6cefe6231..2a93ab63f 100644 --- a/libsolidity/codegen/CompilerContext.h +++ b/libsolidity/codegen/CompilerContext.h @@ -315,11 +315,7 @@ public: RevertStrings revertStrings() const { return m_revertStrings; } - evmasm::AssemblyItem makeFreeMemoryInitPush(u256 _value) - { - m_freeMemoryInitPush = std::make_shared(_value); - return evmasm::AssemblyItem(m_freeMemoryInitPush); - } + void appendFreeMemoryInitPush(u256 _value); private: /// Updates source location set in the assembly. @@ -406,8 +402,8 @@ private: std::queue>> m_lowLevelFunctionGenerationQueue; /// Flag to check that appendYulUtilityFunctions() was called exactly once bool m_appendYulUtilityFunctionsRan = false; - /// The assembly item that pushes the initial value of the free memory pointer. - std::shared_ptr m_freeMemoryInitPush; + /// The index of the assembly item that pushes the initial value of the free memory pointer. + std::optional m_freeMemoryInitPush; }; } diff --git a/libsolidity/codegen/CompilerUtils.cpp b/libsolidity/codegen/CompilerUtils.cpp index e342d2d08..8177da1f2 100644 --- a/libsolidity/codegen/CompilerUtils.cpp +++ b/libsolidity/codegen/CompilerUtils.cpp @@ -55,7 +55,7 @@ void CompilerUtils::initialiseFreeMemoryPointer() { size_t reservedMemory = m_context.reservedMemory(); solAssert(bigint(generalPurposeMemoryStart) + bigint(reservedMemory) < bigint(1) << 63, ""); - m_context << m_context.makeFreeMemoryInitPush((u256(generalPurposeMemoryStart) + reservedMemory)); + m_context.appendFreeMemoryInitPush((u256(generalPurposeMemoryStart) + reservedMemory)); storeFreeMemoryPointer(); } diff --git a/libyul/optimiser/OptimiserStep.h b/libyul/optimiser/OptimiserStep.h index abc631bba..62d12fc77 100644 --- a/libyul/optimiser/OptimiserStep.h +++ b/libyul/optimiser/OptimiserStep.h @@ -41,7 +41,7 @@ struct OptimiserStepContext std::set const& reservedIdentifiers; /// The value nullopt represents creation code std::optional expectedExecutionsPerDeployment; - std::shared_ptr externalFreeMemoryPointerInitializer{}; + u256* externalFreeMemoryPointerInitializer = nullptr; }; diff --git a/libyul/optimiser/Suite.cpp b/libyul/optimiser/Suite.cpp index 1376cb326..d5099e767 100644 --- a/libyul/optimiser/Suite.cpp +++ b/libyul/optimiser/Suite.cpp @@ -90,7 +90,7 @@ void OptimiserSuite::run( string const& _optimisationSequence, optional _expectedExecutionsPerDeployment, set const& _externallyUsedIdentifiers, - std::shared_ptr _externalFreeMemoryPointerInitializer + u256* _externalFreeMemoryPointerInitializer ) { EVMDialect const* evmDialect = dynamic_cast(&_dialect); diff --git a/libyul/optimiser/Suite.h b/libyul/optimiser/Suite.h index 817ffe755..648cae2d4 100644 --- a/libyul/optimiser/Suite.h +++ b/libyul/optimiser/Suite.h @@ -67,7 +67,7 @@ public: std::string const& _optimisationSequence, std::optional _expectedExecutionsPerDeployment, std::set const& _externallyUsedIdentifiers = {}, - std::shared_ptr _externalFreeMemoryPointerInitializer = {} + u256* _externalFreeMemoryPointerInitializer = nullptr ); /// Ensures that specified sequence of step abbreviations is well-formed and can be executed. @@ -93,7 +93,7 @@ private: Debug _debug, Block& _ast, std::optional expectedExecutionsPerDeployment, - std::shared_ptr _externalFreeMemoryPointerInitializer = {} + u256* _externalFreeMemoryPointerInitializer = nullptr ): m_dispenser{_dialect, _ast, _externallyUsedIdentifiers},