Merge pull request #937 from LefterisJP/sol_MultiArgSHA3

Solidity multiple arg sha3
This commit is contained in:
chriseth 2015-02-09 00:14:13 +01:00
commit a66db516fb
5 changed files with 61 additions and 23 deletions

View File

@ -489,15 +489,18 @@ void FunctionCall::checkTypeRequirements()
// and then ask if that is implicitly convertible to the struct represented by the // and then ask if that is implicitly convertible to the struct represented by the
// function parameters // function parameters
TypePointers const& parameterTypes = functionType->getParameterTypes(); TypePointers const& parameterTypes = functionType->getParameterTypes();
if (parameterTypes.size() != m_arguments.size()) if (functionType->getLocation() != FunctionType::Location::SHA3 && parameterTypes.size() != m_arguments.size())
BOOST_THROW_EXCEPTION(createTypeError("Wrong argument count for function call.")); BOOST_THROW_EXCEPTION(createTypeError("Wrong argument count for function call."));
if (m_names.empty()) if (m_names.empty())
{ {
for (size_t i = 0; i < m_arguments.size(); ++i) for (size_t i = 0; i < m_arguments.size(); ++i)
if (!m_arguments[i]->getType()->isImplicitlyConvertibleTo(*parameterTypes[i])) if (functionType->getLocation() != FunctionType::Location::SHA3 &&
!m_arguments[i]->getType()->isImplicitlyConvertibleTo(*parameterTypes[i]))
BOOST_THROW_EXCEPTION(createTypeError("Invalid type for argument in function call.")); BOOST_THROW_EXCEPTION(createTypeError("Invalid type for argument in function call."));
} }
else if (functionType->getLocation() == FunctionType::Location::SHA3)
BOOST_THROW_EXCEPTION(createTypeError("Named arguments can't be used for SHA3."));
else else
{ {
auto const& parameterNames = functionType->getParameterNames(); auto const& parameterNames = functionType->getParameterNames();

View File

@ -206,7 +206,8 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
TypePointers const& parameterTypes = function.getParameterTypes(); TypePointers const& parameterTypes = function.getParameterTypes();
vector<ASTPointer<Expression const>> const& callArguments = _functionCall.getArguments(); vector<ASTPointer<Expression const>> const& callArguments = _functionCall.getArguments();
vector<ASTPointer<ASTString>> const& callArgumentNames = _functionCall.getNames(); vector<ASTPointer<ASTString>> const& callArgumentNames = _functionCall.getNames();
solAssert(callArguments.size() == parameterTypes.size(), ""); if (function.getLocation() != Location::SHA3)
solAssert(callArguments.size() == parameterTypes.size(), "");
vector<ASTPointer<Expression const>> arguments; vector<ASTPointer<Expression const>> arguments;
if (callArgumentNames.empty()) if (callArgumentNames.empty())
@ -274,7 +275,7 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
m_context << u256(0) << eth::Instruction::CODECOPY; m_context << u256(0) << eth::Instruction::CODECOPY;
unsigned length = bytecode.size(); unsigned length = bytecode.size();
length += appendArgumentCopyToMemory(function.getParameterTypes(), arguments, length); length += appendArgumentsCopyToMemory(arguments, function.getParameterTypes(), length);
// size, offset, endowment // size, offset, endowment
m_context << u256(length) << u256(0); m_context << u256(length) << u256(0);
if (function.valueSet()) if (function.valueSet())
@ -325,9 +326,11 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
m_context << eth::Instruction::SUICIDE; m_context << eth::Instruction::SUICIDE;
break; break;
case Location::SHA3: case Location::SHA3:
appendExpressionCopyToMemory(*function.getParameterTypes().front(), *arguments.front()); {
m_context << u256(32) << u256(0) << eth::Instruction::SHA3; unsigned length = appendArgumentsCopyToMemory(arguments);
m_context << u256(length) << u256(0) << eth::Instruction::SHA3;
break; break;
}
case Location::LOG0: case Location::LOG0:
case Location::LOG1: case Location::LOG1:
case Location::LOG2: case Location::LOG2:
@ -797,7 +800,7 @@ void ExpressionCompiler::appendExternalFunctionCall(FunctionType const& _functio
// reserve space for the function identifier // reserve space for the function identifier
unsigned dataOffset = bare ? 0 : CompilerUtils::dataStartOffset; unsigned dataOffset = bare ? 0 : CompilerUtils::dataStartOffset;
dataOffset += appendArgumentCopyToMemory(_functionType.getParameterTypes(), _arguments, dataOffset); dataOffset += appendArgumentsCopyToMemory(_arguments, _functionType.getParameterTypes(), dataOffset);
//@todo only return the first return value for now //@todo only return the first return value for now
Type const* firstType = _functionType.getReturnParameterTypes().empty() ? nullptr : Type const* firstType = _functionType.getReturnParameterTypes().empty() ? nullptr :
@ -833,28 +836,45 @@ void ExpressionCompiler::appendExternalFunctionCall(FunctionType const& _functio
} }
} }
unsigned ExpressionCompiler::appendArgumentCopyToMemory(TypePointers const& _types, unsigned ExpressionCompiler::appendArgumentsCopyToMemory(vector<ASTPointer<Expression const>> const& _arguments,
vector<ASTPointer<Expression const>> const& _arguments, TypePointers const& _types,
unsigned _memoryOffset) unsigned _memoryOffset)
{ {
unsigned length = 0; unsigned length = 0;
if (!_types.empty())
{
for (unsigned i = 0; i < _arguments.size(); ++i)
length += appendExpressionCopyToMemory(*_types[i], *_arguments[i], _memoryOffset + length);
return length;
}
// without type conversion
for (unsigned i = 0; i < _arguments.size(); ++i) for (unsigned i = 0; i < _arguments.size(); ++i)
length += appendExpressionCopyToMemory(*_types[i], *_arguments[i], _memoryOffset + length); {
const bool wantPadding = false;
_arguments[i]->accept(*this);
length += moveTypeToMemory(*_arguments[i]->getType()->getRealType(), _arguments[i]->getLocation(), _memoryOffset + length, wantPadding);
}
return length; return length;
} }
unsigned ExpressionCompiler::moveTypeToMemory(Type const& _type, Location const& _location, unsigned _memoryOffset, bool _padToWordBoundaries)
{
unsigned const c_encodedSize = _type.getCalldataEncodedSize();
unsigned const c_numBytes = _padToWordBoundaries ? CompilerUtils::getPaddedSize(c_encodedSize) : c_encodedSize;
if (c_numBytes == 0 || c_numBytes > 32)
BOOST_THROW_EXCEPTION(CompilerError()
<< errinfo_sourceLocation(_location)
<< errinfo_comment("Type " + _type.toString() + " not yet supported."));
bool const c_leftAligned = _type.getCategory() == Type::Category::STRING;
return CompilerUtils(m_context).storeInMemory(_memoryOffset, c_numBytes, c_leftAligned, _padToWordBoundaries);
}
unsigned ExpressionCompiler::appendTypeConversionAndMoveToMemory(Type const& _expectedType, Type const& _type, unsigned ExpressionCompiler::appendTypeConversionAndMoveToMemory(Type const& _expectedType, Type const& _type,
Location const& _location, unsigned _memoryOffset) Location const& _location, unsigned _memoryOffset)
{ {
appendTypeConversion(_type, _expectedType, true); appendTypeConversion(_type, _expectedType, true);
unsigned const c_numBytes = CompilerUtils::getPaddedSize(_expectedType.getCalldataEncodedSize()); return moveTypeToMemory(_expectedType, _location, _memoryOffset);
if (c_numBytes == 0 || c_numBytes > 32)
BOOST_THROW_EXCEPTION(CompilerError()
<< 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, unsigned ExpressionCompiler::appendExpressionCopyToMemory(Type const& _expectedType,

View File

@ -94,13 +94,16 @@ private:
bool bare = false); bool bare = false);
/// Appends code that copies the given arguments to memory (with optional offset). /// Appends code that copies the given arguments to memory (with optional offset).
/// @returns the number of bytes copied to memory /// @returns the number of bytes copied to memory
unsigned appendArgumentCopyToMemory(TypePointers const& _types, unsigned appendArgumentsCopyToMemory(std::vector<ASTPointer<Expression const>> const& _arguments,
std::vector<ASTPointer<Expression const>> const& _arguments, TypePointers const& _types = {},
unsigned _memoryOffset = 0); unsigned _memoryOffset = 0);
/// Appends code that copies a type to memory. /// Appends code that copies a type to memory.
/// @returns the number of bytes copied to memory /// @returns the number of bytes copied to memory
unsigned appendTypeConversionAndMoveToMemory(Type const& _expectedType, Type const& _type, unsigned appendTypeConversionAndMoveToMemory(Type const& _expectedType, Type const& _type,
Location const& _location, unsigned _memoryOffset = 0); Location const& _location, unsigned _memoryOffset = 0);
/// Appends code that moves a type to memory
/// @returns the number of bytes copied to memory
unsigned moveTypeToMemory(Type const& _type, Location const& _location, unsigned _memoryOffset, bool _padToWordBoundaries = true);
/// Appends code that evaluates a single expression and copies it to memory (with optional offset). /// Appends code that evaluates a single expression and copies it to memory (with optional offset).
/// @returns the number of bytes copied to memory /// @returns the number of bytes copied to memory
unsigned appendExpressionCopyToMemory(Type const& _expectedType, Expression const& _expression, unsigned appendExpressionCopyToMemory(Type const& _expectedType, Expression const& _expression,

View File

@ -357,6 +357,13 @@ u256 IntegerConstantType::literalValue(Literal const*) const
return value; return value;
} }
TypePointer IntegerConstantType::getRealType() const
{
auto intType = getIntegerType();
solAssert(!!intType, std::string("getRealType called with invalid integer constant") + toString());
return intType;
}
shared_ptr<IntegerType const> IntegerConstantType::getIntegerType() const shared_ptr<IntegerType const> IntegerConstantType::getIntegerType() const
{ {
bigint value = m_value; bigint value = m_value;

View File

@ -130,6 +130,8 @@ public:
/// i.e. it behaves differently in lvalue context and in value context. /// i.e. it behaves differently in lvalue context and in value context.
virtual bool isValueType() const { return false; } virtual bool isValueType() const { return false; }
virtual unsigned getSizeOnStack() const { return 1; } virtual unsigned getSizeOnStack() const { return 1; }
/// @returns the real type of some types, like e.g: IntegerConstant
virtual TypePointer getRealType() const { return shared_from_this(); }
/// Returns the list of all members of this type. Default implementation: no members. /// Returns the list of all members of this type. Default implementation: no members.
virtual MemberList const& getMembers() const { return EmptyMemberList; } virtual MemberList const& getMembers() const { return EmptyMemberList; }
@ -140,7 +142,7 @@ public:
virtual u256 literalValue(Literal const*) const virtual u256 literalValue(Literal const*) const
{ {
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Literal value requested " BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Literal value requested "
"for type without literals.")); "for type without literals."));
} }
protected: protected:
@ -175,6 +177,7 @@ public:
virtual MemberList const& getMembers() const { return isAddress() ? AddressMemberList : EmptyMemberList; } virtual MemberList const& getMembers() const { return isAddress() ? AddressMemberList : EmptyMemberList; }
virtual std::string toString() const override; virtual std::string toString() const override;
virtual TypePointer getRealType() const { return std::make_shared<IntegerType>(m_bits, m_modifier); }
int getNumBits() const { return m_bits; } int getNumBits() const { return m_bits; }
bool isHash() const { return m_modifier == Modifier::HASH || m_modifier == Modifier::ADDRESS; } bool isHash() const { return m_modifier == Modifier::HASH || m_modifier == Modifier::ADDRESS; }
@ -213,6 +216,7 @@ public:
virtual std::string toString() const override; virtual std::string toString() const override;
virtual u256 literalValue(Literal const* _literal) const override; virtual u256 literalValue(Literal const* _literal) const override;
virtual TypePointer getRealType() const override;
/// @returns the smallest integer type that can hold the value or an empty pointer if not possible. /// @returns the smallest integer type that can hold the value or an empty pointer if not possible.
std::shared_ptr<IntegerType const> getIntegerType() const; std::shared_ptr<IntegerType const> getIntegerType() const;
@ -244,6 +248,7 @@ public:
virtual std::string toString() const override { return "string" + dev::toString(m_bytes); } virtual std::string toString() const override { return "string" + dev::toString(m_bytes); }
virtual u256 literalValue(Literal const* _literal) const override; virtual u256 literalValue(Literal const* _literal) const override;
virtual TypePointer getRealType() const override { return std::make_shared<StaticStringType>(m_bytes); }
int getNumBytes() const { return m_bytes; } int getNumBytes() const { return m_bytes; }