From 77f442458934d2f0d9ffde34784bb58bd177b3ef Mon Sep 17 00:00:00 2001 From: chriseth Date: Tue, 16 Aug 2016 16:27:20 +0200 Subject: Provide inline assembly to the code generator. (#840) * Directly usable inline assembly. * Add missing header. --- libsolidity/codegen/CompilerContext.cpp | 48 ++++++++++++++++++++++++++++++ libsolidity/codegen/CompilerContext.h | 9 ++++++ libsolidity/codegen/ExpressionCompiler.cpp | 1 + 3 files changed, 58 insertions(+) (limited to 'libsolidity/codegen') diff --git a/libsolidity/codegen/CompilerContext.cpp b/libsolidity/codegen/CompilerContext.cpp index c1dc8dfb..3ac5bd3c 100644 --- a/libsolidity/codegen/CompilerContext.cpp +++ b/libsolidity/codegen/CompilerContext.cpp @@ -23,9 +23,12 @@ #include #include #include +#include #include #include #include +#include +#include using namespace std; @@ -172,6 +175,51 @@ void CompilerContext::resetVisitedNodes(ASTNode const* _node) updateSourceLocation(); } +void CompilerContext::appendInlineAssembly( + string const& _assembly, + vector const& _localVariables, + map const& _replacements +) +{ + string replacedAssembly; + string const* assembly = &_assembly; + if (!_replacements.empty()) + { + replacedAssembly = _assembly; + for (auto const& replacement: _replacements) + replacedAssembly = boost::algorithm::replace_all_copy(replacedAssembly, replacement.first, replacement.second); + assembly = &replacedAssembly; + } + + unsigned startStackHeight = stackHeight(); + auto identifierAccess = [&]( + assembly::Identifier const& _identifier, + eth::Assembly& _assembly, + assembly::CodeGenerator::IdentifierContext _context + ) { + auto it = std::find(_localVariables.begin(), _localVariables.end(), _identifier.name); + if (it == _localVariables.end()) + return false; + unsigned stackDepth = _localVariables.end() - it; + int stackDiff = _assembly.deposit() - startStackHeight + stackDepth; + if (stackDiff < 1 || stackDiff > 16) + BOOST_THROW_EXCEPTION( + CompilerError() << + errinfo_comment("Stack too deep, try removing local variables.") + ); + if (_context == assembly::CodeGenerator::IdentifierContext::RValue) + _assembly.append(dupInstruction(stackDiff)); + else + { + _assembly.append(swapInstruction(stackDiff)); + _assembly.append(Instruction::POP); + } + return true; + }; + + solAssert(assembly::InlineAssemblyStack().parseAndAssemble(*assembly, m_asm, identifierAccess), ""); +} + void CompilerContext::injectVersionStampIntoSub(size_t _subIndex) { eth::Assembly& sub = m_asm.sub(_subIndex); diff --git a/libsolidity/codegen/CompilerContext.h b/libsolidity/codegen/CompilerContext.h index a56335ce..0c1500b0 100644 --- a/libsolidity/codegen/CompilerContext.h +++ b/libsolidity/codegen/CompilerContext.h @@ -132,6 +132,15 @@ public: CompilerContext& operator<<(u256 const& _value) { m_asm.append(_value); return *this; } CompilerContext& operator<<(bytes const& _data) { m_asm.append(_data); return *this; } + /// Appends inline assembly. @a _replacements are string-matching replacements that are performed + /// prior to parsing the inline assembly. + /// @param _localVariables assigns stack positions to variables with the last one being the stack top + void appendInlineAssembly( + std::string const& _assembly, + std::vector const& _localVariables = std::vector(), + std::map const& _replacements = std::map{} + ); + /// Prepends "PUSH POP" void injectVersionStampIntoSub(size_t _subIndex); diff --git a/libsolidity/codegen/ExpressionCompiler.cpp b/libsolidity/codegen/ExpressionCompiler.cpp index 3fbf27ce..41c14517 100644 --- a/libsolidity/codegen/ExpressionCompiler.cpp +++ b/libsolidity/codegen/ExpressionCompiler.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include -- cgit