From c9f9b2ab4d421e99055425ace5deeb76d8f4fdd2 Mon Sep 17 00:00:00 2001 From: Yoichi Hirai Date: Tue, 8 Nov 2016 15:25:32 +0100 Subject: [PATCH] codegen: add a compilation mode and a runtime context to CompilerContext --- libsolidity/codegen/Compiler.cpp | 6 +++--- libsolidity/codegen/Compiler.h | 4 +++- libsolidity/codegen/CompilerContext.h | 15 +++++++++++++++ libsolidity/codegen/ContractCompiler.h | 4 ++-- test/libsolidity/SolidityExpressionCompiler.cpp | 2 +- 5 files changed, 24 insertions(+), 7 deletions(-) diff --git a/libsolidity/codegen/Compiler.cpp b/libsolidity/codegen/Compiler.cpp index 1ae5dda9c..bb8211ad8 100644 --- a/libsolidity/codegen/Compiler.cpp +++ b/libsolidity/codegen/Compiler.cpp @@ -33,10 +33,10 @@ void Compiler::compileContract( std::map const& _contracts ) { - ContractCompiler runtimeCompiler(m_runtimeContext, m_optimize); + ContractCompiler runtimeCompiler(CompilationMode::Runtime, nullptr, m_runtimeContext, m_optimize); runtimeCompiler.compileContract(_contract, _contracts); - ContractCompiler creationCompiler(m_context, m_optimize); + ContractCompiler creationCompiler(CompilationMode::Creation, &m_runtimeContext, m_context, m_optimize); m_runtimeSub = creationCompiler.compileConstructor(m_runtimeContext, _contract, _contracts); if (m_optimize) @@ -54,7 +54,7 @@ void Compiler::compileClone( map const& _contracts ) { - ContractCompiler cloneCompiler(m_context, m_optimize); + ContractCompiler cloneCompiler(CompilationMode::Creation, &m_runtimeContext, m_context, m_optimize); m_runtimeSub = cloneCompiler.compileClone(_contract, _contracts); if (m_optimize) diff --git a/libsolidity/codegen/Compiler.h b/libsolidity/codegen/Compiler.h index fccb68a95..56849ea00 100644 --- a/libsolidity/codegen/Compiler.h +++ b/libsolidity/codegen/Compiler.h @@ -35,7 +35,9 @@ class Compiler public: explicit Compiler(bool _optimize = false, unsigned _runs = 200): m_optimize(_optimize), - m_optimizeRuns(_runs) + m_optimizeRuns(_runs), + m_context(CompilationMode::Creation, &m_runtimeContext), + m_runtimeContext(CompilationMode::Runtime) { } void compileContract( diff --git a/libsolidity/codegen/CompilerContext.h b/libsolidity/codegen/CompilerContext.h index 6c509685a..8b95c9f5b 100644 --- a/libsolidity/codegen/CompilerContext.h +++ b/libsolidity/codegen/CompilerContext.h @@ -37,6 +37,10 @@ namespace dev { namespace solidity { +/// Depending on the compilation is on the runtime code or the creation code, +/// the interpretation of internal function values differ. +enum class CompilationMode { Runtime, Creation }; + /** * Context to be shared by all units that compile the same contract. * It stores the generated bytecode and the position of identifiers in memory and on the stack. @@ -44,6 +48,13 @@ namespace solidity { class CompilerContext { public: + CompilerContext(CompilationMode _mode, CompilerContext* _runtimeContext = nullptr) : + m_mode(_mode), m_runtimeContext(_runtimeContext) + { + solAssert(m_mode != CompilationMode::Runtime || !m_runtimeContext, "runtime but another runtime context provided"); + solAssert(m_mode != CompilationMode::Creation || m_runtimeContext, "creation but no runtime context provided"); + } + bool isCreationPhase() const { return m_mode == CompilationMode::Creation; } void addMagicGlobal(MagicVariableDeclaration const& _declaration); @@ -230,6 +241,10 @@ private: std::vector m_inheritanceHierarchy; /// Stack of current visited AST nodes, used for location attachment std::stack m_visitedNodes; + /// The current mode of the compilation + CompilationMode m_mode; + /// The runtime context if in Creation mode, this is used for generating tags that would be stored into the storage and then used at runtime. + CompilerContext *m_runtimeContext; }; } diff --git a/libsolidity/codegen/ContractCompiler.h b/libsolidity/codegen/ContractCompiler.h index 0799a5431..fecf6f5ad 100644 --- a/libsolidity/codegen/ContractCompiler.h +++ b/libsolidity/codegen/ContractCompiler.h @@ -38,11 +38,11 @@ namespace solidity { class ContractCompiler: private ASTConstVisitor { public: - explicit ContractCompiler(CompilerContext& _context, bool _optimise): + explicit ContractCompiler(CompilationMode _mode, CompilerContext* _runtimeContext, CompilerContext& _context, bool _optimise): m_optimise(_optimise), m_context(_context) { - m_context = CompilerContext(); + m_context = CompilerContext(_mode, _runtimeContext); } void compileContract( diff --git a/test/libsolidity/SolidityExpressionCompiler.cpp b/test/libsolidity/SolidityExpressionCompiler.cpp index e9a05745e..09d556a51 100644 --- a/test/libsolidity/SolidityExpressionCompiler.cpp +++ b/test/libsolidity/SolidityExpressionCompiler.cpp @@ -136,7 +136,7 @@ bytes compileFirstExpression( FirstExpressionExtractor extractor(*contract); BOOST_REQUIRE(extractor.expression() != nullptr); - CompilerContext context; + CompilerContext context { CompilationMode::Runtime /* probably simpler */ }; context.resetVisitedNodes(contract); context.setInheritanceHierarchy(inheritanceHierarchy); unsigned parametersSize = _localVariables.size(); // assume they are all one slot on the stack