From 130ff85e85de3ea8a9666f84843428a3a09b1aab Mon Sep 17 00:00:00 2001 From: Christian Date: Tue, 9 Dec 2014 18:46:18 +0100 Subject: String types. --- ExpressionCompiler.cpp | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 'ExpressionCompiler.cpp') diff --git a/ExpressionCompiler.cpp b/ExpressionCompiler.cpp index f1086c14..5e688f6b 100644 --- a/ExpressionCompiler.cpp +++ b/ExpressionCompiler.cpp @@ -226,7 +226,8 @@ bool ExpressionCompiler::visit(FunctionCall& _functionCall) << errinfo_sourceLocation(arguments[i]->getLocation()) << errinfo_comment("Type " + type.toString() + " not yet supported.")); if (numBytes != 32) - m_context << (u256(1) << ((32 - numBytes) * 8)) << eth::Instruction::MUL; + if (type.getCategory() != Type::Category::STRING) + m_context << (u256(1) << ((32 - numBytes) * 8)) << eth::Instruction::MUL; m_context << u256(dataOffset) << eth::Instruction::MSTORE; dataOffset += numBytes; } @@ -243,8 +244,15 @@ bool ExpressionCompiler::visit(FunctionCall& _functionCall) if (retSize == 32) m_context << u256(0) << eth::Instruction::MLOAD; else if (retSize > 0) - m_context << (u256(1) << ((32 - retSize) * 8)) - << u256(0) << eth::Instruction::MLOAD << eth::Instruction::DIV; + { + if (function.getReturnParameterTypes().front()->getCategory() == Type::Category::STRING) + m_context << (u256(1) << ((32 - retSize) * 8)) << eth::Instruction::DUP1 + << u256(0) << eth::Instruction::MLOAD + << eth::Instruction::DIV << eth::Instruction::MUL; + else + m_context << (u256(1) << ((32 - retSize) * 8)) + << u256(0) << eth::Instruction::MLOAD << eth::Instruction::DIV; + } break; } case Location::SEND: @@ -411,10 +419,11 @@ void ExpressionCompiler::endVisit(Literal& _literal) { case Type::Category::INTEGER: case Type::Category::BOOL: + case Type::Category::STRING: m_context << _literal.getType()->literalValue(_literal); break; default: - BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Only integer and boolean literals implemented for now.")); + BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Only integer, boolean and string literals implemented for now.")); } } @@ -550,6 +559,11 @@ void ExpressionCompiler::appendTypeConversion(Type const& _typeOnStack, Type con return; if (_typeOnStack.getCategory() == Type::Category::INTEGER) appendHighBitsCleanup(dynamic_cast(_typeOnStack)); + else if (_typeOnStack.getCategory() == Type::Category::STRING) + { + // nothing to do, strings are high-order-bit-aligned + //@todo clear lower-order bytes if we allow explicit conversion to shorter strings + } else if (_typeOnStack != _targetType) // All other types should not be convertible to non-equal types. BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Invalid type conversion requested.")); -- cgit From 373f0da2675f298984f265672d7414a58c061512 Mon Sep 17 00:00:00 2001 From: Christian Date: Wed, 10 Dec 2014 17:15:17 +0100 Subject: Helper functions to access memory. --- ExpressionCompiler.cpp | 36 +++++++++++++++--------------------- 1 file changed, 15 insertions(+), 21 deletions(-) (limited to 'ExpressionCompiler.cpp') diff --git a/ExpressionCompiler.cpp b/ExpressionCompiler.cpp index 5e688f6b..d71d98cd 100644 --- a/ExpressionCompiler.cpp +++ b/ExpressionCompiler.cpp @@ -225,15 +225,14 @@ bool ExpressionCompiler::visit(FunctionCall& _functionCall) BOOST_THROW_EXCEPTION(CompilerError() << errinfo_sourceLocation(arguments[i]->getLocation()) << errinfo_comment("Type " + type.toString() + " not yet supported.")); - if (numBytes != 32) - if (type.getCategory() != Type::Category::STRING) - m_context << (u256(1) << ((32 - numBytes) * 8)) << eth::Instruction::MUL; - m_context << u256(dataOffset) << eth::Instruction::MSTORE; + bool const leftAligned = type.getCategory() == Type::Category::STRING; + CompilerUtils(m_context).storeInMemory(dataOffset, numBytes, leftAligned); dataOffset += numBytes; } //@todo only return the first return value for now - unsigned retSize = function.getReturnParameterTypes().empty() ? 0 - : function.getReturnParameterTypes().front()->getCalldataEncodedSize(); + Type const* firstType = function.getReturnParameterTypes().empty() ? nullptr : + function.getReturnParameterTypes().front().get(); + unsigned retSize = firstType ? firstType->getCalldataEncodedSize() : 0; // CALL arguments: outSize, outOff, inSize, inOff, value, addr, gas (stack top) m_context << u256(retSize) << u256(0) << u256(dataOffset) << u256(0) << u256(0); _functionCall.getExpression().accept(*this); // pushes addr and function index @@ -241,17 +240,10 @@ bool ExpressionCompiler::visit(FunctionCall& _functionCall) << u256(25) << eth::Instruction::GAS << eth::Instruction::SUB << eth::Instruction::CALL << eth::Instruction::POP; // @todo do not ignore failure indicator - if (retSize == 32) - m_context << u256(0) << eth::Instruction::MLOAD; - else if (retSize > 0) + if (retSize > 0) { - if (function.getReturnParameterTypes().front()->getCategory() == Type::Category::STRING) - m_context << (u256(1) << ((32 - retSize) * 8)) << eth::Instruction::DUP1 - << u256(0) << eth::Instruction::MLOAD - << eth::Instruction::DIV << eth::Instruction::MUL; - else - m_context << (u256(1) << ((32 - retSize) * 8)) - << u256(0) << eth::Instruction::MLOAD << eth::Instruction::DIV; + bool const leftAligned = firstType->getCategory() == Type::Category::STRING; + CompilerUtils(m_context).loadFromMemory(0, retSize, leftAligned); } break; } @@ -275,7 +267,8 @@ bool ExpressionCompiler::visit(FunctionCall& _functionCall) arguments.front()->accept(*this); appendTypeConversion(*arguments.front()->getType(), *function.getParameterTypes().front(), true); // @todo move this once we actually use memory - m_context << u256(0) << eth::Instruction::MSTORE << u256(32) << u256(0) << eth::Instruction::SHA3; + CompilerUtils(m_context).storeInMemory(0); + m_context << u256(32) << u256(0) << eth::Instruction::SHA3; break; case Location::ECRECOVER: case Location::SHA256: @@ -291,13 +284,13 @@ bool ExpressionCompiler::visit(FunctionCall& _functionCall) arguments[i]->accept(*this); appendTypeConversion(*arguments[i]->getType(), *function.getParameterTypes()[i], true); // @todo move this once we actually use memory - m_context << u256(i * 32) << eth::Instruction::MSTORE; + CompilerUtils(m_context).storeInMemory(i * 32); } m_context << u256(32) << u256(0) << u256(arguments.size() * 32) << u256(0) << u256(0) << contractAddress << u256(500) //@todo determine actual gas requirement << eth::Instruction::CALL - << eth::Instruction::POP - << u256(0) << eth::Instruction::MLOAD; + << eth::Instruction::POP; + CompilerUtils(m_context).loadFromMemory(0); break; } default: @@ -381,7 +374,8 @@ bool ExpressionCompiler::visit(IndexAccess& _indexAccess) *dynamic_cast(*_indexAccess.getBaseExpression().getType()).getKeyType(), true); // @todo move this once we actually use memory - m_context << u256(32) << eth::Instruction::MSTORE << u256(0) << eth::Instruction::MSTORE; + CompilerUtils(m_context).storeInMemory(0); + CompilerUtils(m_context).storeInMemory(32); m_context << u256(64) << u256(0) << eth::Instruction::SHA3; m_currentLValue = LValue(m_context, LValue::STORAGE, *_indexAccess.getType()); -- cgit