From ec7a9bf919c393b10a91f0bc77fe2932c66a0c6d Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Fri, 30 Jan 2015 16:06:56 +0100 Subject: Adding mapping treatment to FunctionType Plus a TypeResolution test for it --- Types.cpp | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/Types.cpp b/Types.cpp index ab401332..7fa4561e 100644 --- a/Types.cpp +++ b/Types.cpp @@ -621,12 +621,25 @@ FunctionType::FunctionType(FunctionDefinition const& _function, bool _isInternal FunctionType::FunctionType(VariableDeclaration const& _varDecl): m_location(Location::EXTERNAL), m_isConstant(true), m_declaration(&_varDecl) { - TypePointers params({}); - vector paramNames({}); - TypePointers retParams({_varDecl.getType()}); - vector retParamNames({ _varDecl.getName()}); - // for now, no input parameters LTODO: change for some things like mapping + TypePointers params; + vector paramNames; + TypePointers retParams; + vector retParamNames; + TypePointer varDeclType = _varDecl.getType(); + auto mappingType = dynamic_cast(varDeclType.get()); + if (mappingType!= nullptr) + { + params.push_back(mappingType->getKeyType()); + paramNames.push_back(mappingType->getKeyType()->toString()); + retParams.push_back(mappingType->getValueType()); + retParamNames.push_back(mappingType->getValueType()->toString()); + } + else // elelemntary type + { + retParams.push_back(varDeclType); + retParamNames.push_back(_varDecl.getName()); + } swap(params, m_parameterTypes); swap(paramNames, m_parameterNames); swap(retParams, m_returnParameterTypes); -- cgit v1.2.3 From 82bcb7e33a4a00be8145588615c0d7a812fd4ce2 Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Sun, 1 Feb 2015 02:41:14 +0100 Subject: Code generation for mapping state variable accessor - Work in progress --- Compiler.cpp | 4 --- ExpressionCompiler.cpp | 75 ++++++++++++++++++++++++++++++++++++++++++++------ ExpressionCompiler.h | 4 +++ 3 files changed, 71 insertions(+), 12 deletions(-) diff --git a/Compiler.cpp b/Compiler.cpp index 3c46d455..389f826b 100644 --- a/Compiler.cpp +++ b/Compiler.cpp @@ -240,10 +240,6 @@ bool Compiler::visit(VariableDeclaration const& _variableDeclaration) m_context << m_context.getFunctionEntryLabel(_variableDeclaration); ExpressionCompiler::appendStateVariableAccessor(m_context, _variableDeclaration); - unsigned sizeOnStack = _variableDeclaration.getType()->getSizeOnStack(); - solAssert(sizeOnStack <= 15, "Stack too deep."); - m_context << eth::dupInstruction(sizeOnStack + 1) << eth::Instruction::JUMP; - return false; } diff --git a/ExpressionCompiler.cpp b/ExpressionCompiler.cpp index 7d58ea2e..edd04256 100644 --- a/ExpressionCompiler.cpp +++ b/ExpressionCompiler.cpp @@ -823,26 +823,85 @@ unsigned ExpressionCompiler::appendArgumentCopyToMemory(TypePointers const& _typ return length; } -unsigned ExpressionCompiler::appendExpressionCopyToMemory(Type const& _expectedType, - Expression const& _expression, unsigned _memoryOffset) +unsigned ExpressionCompiler::appendTypeCopyToMemory(Type const& _expectedType, TypePointer const& _type, + Location const& _location, unsigned _memoryOffset) { - _expression.accept(*this); - appendTypeConversion(*_expression.getType(), _expectedType, true); + appendTypeConversion(*_type, _expectedType, true); unsigned const c_numBytes = CompilerUtils::getPaddedSize(_expectedType.getCalldataEncodedSize()); if (c_numBytes == 0 || c_numBytes > 32) BOOST_THROW_EXCEPTION(CompilerError() - << errinfo_sourceLocation(_expression.getLocation()) + << errinfo_sourceLocation(_location) << errinfo_comment("Type " + _expectedType.toString() + " not yet supported.")); bool const c_leftAligned = _expectedType.getCategory() == Type::Category::STRING; bool const c_padToWords = true; return CompilerUtils(m_context).storeInMemory(_memoryOffset, c_numBytes, c_leftAligned, c_padToWords); } +unsigned ExpressionCompiler::appendExpressionCopyToMemory(Type const& _expectedType, + Expression const& _expression, + unsigned _memoryOffset) +{ + _expression.accept(*this); + return appendTypeCopyToMemory(_expectedType, _expression.getType(), _expression.getLocation(), _memoryOffset); +} + void ExpressionCompiler::appendStateVariableAccessor(VariableDeclaration const& _varDecl) { - m_currentLValue.fromStateVariable(_varDecl, _varDecl.getType()); - solAssert(m_currentLValue.isInStorage(), ""); - m_currentLValue.retrieveValue(_varDecl.getType(), Location(), true); + auto mappingType = dynamic_cast(_varDecl.getType().get()); + unsigned sizeOnStack; + if (mappingType != nullptr) + { + // this copies from Compiler::visit(FunctionDefinition..) for argument reading + unsigned parameterSize = mappingType->getKeyType()->getSizeOnStack(); + m_context.adjustStackOffset(parameterSize); + m_context.addVariable(_varDecl, parameterSize); + // this copies from ExpressionCompiler::visit(IndexAccess .. ) for mapping access + TypePointer const& keyType = mappingType->getKeyType(); + unsigned length = appendTypeCopyToMemory(*keyType, mappingType->getValueType(), Location()); + solAssert(length == 32, "Mapping key has to take 32 bytes in memory (for now)."); + // @todo move this once we actually use memory + length += CompilerUtils(m_context).storeInMemory(length); + m_context << u256(length) << u256(0) << eth::Instruction::SHA3; + + m_currentLValue = LValue(m_context, LValue::STORAGE, *mappingType->getValueType()); + m_currentLValue.retrieveValue(mappingType->getValueType(), Location(), true); + + unsigned const c_argumentsSize = keyType->getSizeOnStack(); + unsigned const c_returnValuesSize = mappingType->getValueType()->getSizeOnStack(); + unsigned const c_localVariablesSize = 0; + + vector stackLayout; + stackLayout.push_back(c_returnValuesSize); // target of return address + stackLayout += vector(c_argumentsSize, -1); // discard all arguments + for (unsigned i = 0; i < c_returnValuesSize; ++i) + stackLayout.push_back(i); + stackLayout += vector(c_localVariablesSize, -1); + + while (stackLayout.back() != int(stackLayout.size() - 1)) + if (stackLayout.back() < 0) + { + m_context << eth::Instruction::POP; + stackLayout.pop_back(); + } + else + { + m_context << eth::swapInstruction(stackLayout.size() - stackLayout.back() - 1); + swap(stackLayout[stackLayout.back()], stackLayout.back()); + } + //@todo assert that everything is in place now + + m_context << eth::Instruction::JUMP; + } + else + { + m_currentLValue.fromStateVariable(_varDecl, _varDecl.getType()); + solAssert(m_currentLValue.isInStorage(), ""); + m_currentLValue.retrieveValue(_varDecl.getType(), Location(), true); + sizeOnStack = _varDecl.getType()->getSizeOnStack(); + solAssert(sizeOnStack <= 15, "Stack too deep."); + m_context << eth::dupInstruction(sizeOnStack + 1) << eth::Instruction::JUMP; + } + } ExpressionCompiler::LValue::LValue(CompilerContext& _compilerContext, LValueType _type, Type const& _dataType, diff --git a/ExpressionCompiler.h b/ExpressionCompiler.h index caecbfe8..d93ab28e 100644 --- a/ExpressionCompiler.h +++ b/ExpressionCompiler.h @@ -97,6 +97,10 @@ private: unsigned appendArgumentCopyToMemory(TypePointers const& _types, std::vector> const& _arguments, unsigned _memoryOffset = 0); + /// Appends code that copies a type to memory. + /// @returns the number of bytes copied to memory + unsigned appendTypeCopyToMemory(Type const& _expectedType, TypePointer const& _type, + Location const& _location, unsigned _memoryOffset = 0); /// Appends code that evaluates a single expression and copies it to memory (with optional offset). /// @returns the number of bytes copied to memory unsigned appendExpressionCopyToMemory(Type const& _expectedType, Expression const& _expression, -- cgit v1.2.3 From 5a374afe62ad2175f16d1c5d4a6e7a07cdea41d2 Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Mon, 2 Feb 2015 15:03:44 +0100 Subject: Simple mapping accessors working --- ExpressionCompiler.cpp | 53 +++++++++++++------------------------------------- ExpressionCompiler.h | 4 ++-- 2 files changed, 16 insertions(+), 41 deletions(-) diff --git a/ExpressionCompiler.cpp b/ExpressionCompiler.cpp index edd04256..722ac989 100644 --- a/ExpressionCompiler.cpp +++ b/ExpressionCompiler.cpp @@ -823,10 +823,10 @@ unsigned ExpressionCompiler::appendArgumentCopyToMemory(TypePointers const& _typ return length; } -unsigned ExpressionCompiler::appendTypeCopyToMemory(Type const& _expectedType, TypePointer const& _type, - Location const& _location, unsigned _memoryOffset) +unsigned ExpressionCompiler::appendTypeConversionAndMoveToMemory(Type const& _expectedType, Type const& _type, + Location const& _location, unsigned _memoryOffset) { - appendTypeConversion(*_type, _expectedType, true); + appendTypeConversion(_type, _expectedType, true); unsigned const c_numBytes = CompilerUtils::getPaddedSize(_expectedType.getCalldataEncodedSize()); if (c_numBytes == 0 || c_numBytes > 32) BOOST_THROW_EXCEPTION(CompilerError() @@ -842,65 +842,40 @@ unsigned ExpressionCompiler::appendExpressionCopyToMemory(Type const& _expectedT unsigned _memoryOffset) { _expression.accept(*this); - return appendTypeCopyToMemory(_expectedType, _expression.getType(), _expression.getLocation(), _memoryOffset); + return appendTypeConversionAndMoveToMemory(_expectedType, *_expression.getType(), _expression.getLocation(), _memoryOffset); } void ExpressionCompiler::appendStateVariableAccessor(VariableDeclaration const& _varDecl) { - auto mappingType = dynamic_cast(_varDecl.getType().get()); + TypePointer resultType = _varDecl.getType(); + auto mappingType = dynamic_cast(resultType.get()); unsigned sizeOnStack; + if (mappingType != nullptr) { - // this copies from Compiler::visit(FunctionDefinition..) for argument reading - unsigned parameterSize = mappingType->getKeyType()->getSizeOnStack(); - m_context.adjustStackOffset(parameterSize); - m_context.addVariable(_varDecl, parameterSize); // this copies from ExpressionCompiler::visit(IndexAccess .. ) for mapping access TypePointer const& keyType = mappingType->getKeyType(); - unsigned length = appendTypeCopyToMemory(*keyType, mappingType->getValueType(), Location()); + unsigned length = appendTypeConversionAndMoveToMemory(*keyType, *keyType, Location()); solAssert(length == 32, "Mapping key has to take 32 bytes in memory (for now)."); // @todo move this once we actually use memory + m_context << m_context.getStorageLocationOfVariable(_varDecl); length += CompilerUtils(m_context).storeInMemory(length); m_context << u256(length) << u256(0) << eth::Instruction::SHA3; - m_currentLValue = LValue(m_context, LValue::STORAGE, *mappingType->getValueType()); m_currentLValue.retrieveValue(mappingType->getValueType(), Location(), true); + m_currentLValue.reset(); - unsigned const c_argumentsSize = keyType->getSizeOnStack(); - unsigned const c_returnValuesSize = mappingType->getValueType()->getSizeOnStack(); - unsigned const c_localVariablesSize = 0; - - vector stackLayout; - stackLayout.push_back(c_returnValuesSize); // target of return address - stackLayout += vector(c_argumentsSize, -1); // discard all arguments - for (unsigned i = 0; i < c_returnValuesSize; ++i) - stackLayout.push_back(i); - stackLayout += vector(c_localVariablesSize, -1); - - while (stackLayout.back() != int(stackLayout.size() - 1)) - if (stackLayout.back() < 0) - { - m_context << eth::Instruction::POP; - stackLayout.pop_back(); - } - else - { - m_context << eth::swapInstruction(stackLayout.size() - stackLayout.back() - 1); - swap(stackLayout[stackLayout.back()], stackLayout.back()); - } - //@todo assert that everything is in place now - - m_context << eth::Instruction::JUMP; + resultType = mappingType->getValueType(); } else { m_currentLValue.fromStateVariable(_varDecl, _varDecl.getType()); solAssert(m_currentLValue.isInStorage(), ""); m_currentLValue.retrieveValue(_varDecl.getType(), Location(), true); - sizeOnStack = _varDecl.getType()->getSizeOnStack(); - solAssert(sizeOnStack <= 15, "Stack too deep."); - m_context << eth::dupInstruction(sizeOnStack + 1) << eth::Instruction::JUMP; } + sizeOnStack = _varDecl.getType()->getSizeOnStack(); + solAssert(sizeOnStack <= 15, "Stack too deep."); + m_context << eth::dupInstruction(sizeOnStack + 1) << eth::Instruction::JUMP; } diff --git a/ExpressionCompiler.h b/ExpressionCompiler.h index d93ab28e..7de577e6 100644 --- a/ExpressionCompiler.h +++ b/ExpressionCompiler.h @@ -99,8 +99,8 @@ private: unsigned _memoryOffset = 0); /// Appends code that copies a type to memory. /// @returns the number of bytes copied to memory - unsigned appendTypeCopyToMemory(Type const& _expectedType, TypePointer const& _type, - Location const& _location, unsigned _memoryOffset = 0); + unsigned appendTypeConversionAndMoveToMemory(Type const& _expectedType, Type const& _type, + Location const& _location, unsigned _memoryOffset = 0); /// Appends code that evaluates a single expression and copies it to memory (with optional offset). /// @returns the number of bytes copied to memory unsigned appendExpressionCopyToMemory(Type const& _expectedType, Expression const& _expression, -- cgit v1.2.3 From 8cd3d4b4b7a440815229288d853b6ed81ba4eb43 Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Mon, 2 Feb 2015 17:52:50 +0100 Subject: Accessors for multiple mappings implemented --- ExpressionCompiler.cpp | 45 ++++++++++++++++++++++----------------------- Types.cpp | 22 +++++++++++----------- 2 files changed, 33 insertions(+), 34 deletions(-) diff --git a/ExpressionCompiler.cpp b/ExpressionCompiler.cpp index 722ac989..45e0e80b 100644 --- a/ExpressionCompiler.cpp +++ b/ExpressionCompiler.cpp @@ -22,6 +22,7 @@ #include #include +#include #include #include #include @@ -847,36 +848,34 @@ unsigned ExpressionCompiler::appendExpressionCopyToMemory(Type const& _expectedT void ExpressionCompiler::appendStateVariableAccessor(VariableDeclaration const& _varDecl) { - TypePointer resultType = _varDecl.getType(); - auto mappingType = dynamic_cast(resultType.get()); + FunctionType thisType(_varDecl); + solAssert(thisType.getReturnParameterTypes().size() == 1, ""); + TypePointer const& resultType = thisType.getReturnParameterTypes().front(); unsigned sizeOnStack; - if (mappingType != nullptr) - { - // this copies from ExpressionCompiler::visit(IndexAccess .. ) for mapping access - TypePointer const& keyType = mappingType->getKeyType(); - unsigned length = appendTypeConversionAndMoveToMemory(*keyType, *keyType, Location()); - solAssert(length == 32, "Mapping key has to take 32 bytes in memory (for now)."); - // @todo move this once we actually use memory - m_context << m_context.getStorageLocationOfVariable(_varDecl); - length += CompilerUtils(m_context).storeInMemory(length); - m_context << u256(length) << u256(0) << eth::Instruction::SHA3; - m_currentLValue = LValue(m_context, LValue::STORAGE, *mappingType->getValueType()); - m_currentLValue.retrieveValue(mappingType->getValueType(), Location(), true); - m_currentLValue.reset(); + unsigned length = 0; + TypePointers const& params = thisType.getParameterTypes(); + // move arguments to memory + for (TypePointer const& param: boost::adaptors::reverse(params)) + length += appendTypeConversionAndMoveToMemory(*param, *param, Location(), length); - resultType = mappingType->getValueType(); - } - else + // retrieve the position of the mapping + m_context << m_context.getStorageLocationOfVariable(_varDecl); + + for (TypePointer const& param: params) { - m_currentLValue.fromStateVariable(_varDecl, _varDecl.getType()); - solAssert(m_currentLValue.isInStorage(), ""); - m_currentLValue.retrieveValue(_varDecl.getType(), Location(), true); + // move offset to memory + CompilerUtils(m_context).storeInMemory(length); + unsigned argLen = CompilerUtils::getPaddedSize(param->getCalldataEncodedSize()); + length -= argLen; + m_context << u256(argLen + 32) << u256(length) << eth::Instruction::SHA3; } - sizeOnStack = _varDecl.getType()->getSizeOnStack(); + + m_currentLValue = LValue(m_context, LValue::STORAGE, *resultType); + m_currentLValue.retrieveValue(resultType, Location(), true); + sizeOnStack = resultType->getSizeOnStack(); solAssert(sizeOnStack <= 15, "Stack too deep."); m_context << eth::dupInstruction(sizeOnStack + 1) << eth::Instruction::JUMP; - } ExpressionCompiler::LValue::LValue(CompilerContext& _compilerContext, LValueType _type, Type const& _dataType, diff --git a/Types.cpp b/Types.cpp index 7fa4561e..cfb852c2 100644 --- a/Types.cpp +++ b/Types.cpp @@ -626,20 +626,20 @@ FunctionType::FunctionType(VariableDeclaration const& _varDecl): TypePointers retParams; vector retParamNames; TypePointer varDeclType = _varDecl.getType(); - auto mappingType = dynamic_cast(varDeclType.get()); - if (mappingType!= nullptr) - { - params.push_back(mappingType->getKeyType()); - paramNames.push_back(mappingType->getKeyType()->toString()); + auto mappingType = dynamic_cast(varDeclType.get()); + auto returnType = varDeclType; - retParams.push_back(mappingType->getValueType()); - retParamNames.push_back(mappingType->getValueType()->toString()); - } - else // elelemntary type + while (mappingType!= nullptr) { - retParams.push_back(varDeclType); - retParamNames.push_back(_varDecl.getName()); + params.push_back(mappingType->getKeyType()); + paramNames.push_back(""); + returnType = mappingType->getValueType(); + mappingType = dynamic_cast(mappingType->getValueType().get()); } + + retParams.push_back(returnType); + retParamNames.push_back(""); + swap(params, m_parameterTypes); swap(paramNames, m_parameterNames); swap(retParams, m_returnParameterTypes); -- cgit v1.2.3