From 708129abd5f67838913dd42df590b3b64a825e3a Mon Sep 17 00:00:00 2001 From: chriseth Date: Fri, 20 May 2016 16:22:31 +0200 Subject: Fixes for invalid cleanups for small types. --- libsolidity/codegen/CompilerUtils.cpp | 17 ++++++++++------- libsolidity/codegen/ExpressionCompiler.cpp | 7 +++++-- 2 files changed, 15 insertions(+), 9 deletions(-) (limited to 'libsolidity/codegen') diff --git a/libsolidity/codegen/CompilerUtils.cpp b/libsolidity/codegen/CompilerUtils.cpp index e35f3374..efb9b10a 100644 --- a/libsolidity/codegen/CompilerUtils.cpp +++ b/libsolidity/codegen/CompilerUtils.cpp @@ -323,18 +323,17 @@ void CompilerUtils::convertType(Type const& _typeOnStack, Type const& _targetTyp } else { - // clear lower-order bytes for conversion to shorter bytes - we always clean + // clear for conversion to longer bytes solAssert(targetTypeCategory == Type::Category::FixedBytes, "Invalid type conversion requested."); FixedBytesType const& targetType = dynamic_cast(_targetType); - if (targetType.numBytes() < typeOnStack.numBytes()) + if (targetType.numBytes() > typeOnStack.numBytes() || _cleanupNeeded) { - if (targetType.numBytes() == 0) - m_context << Instruction::DUP1 << Instruction::XOR; + if (typeOnStack.numBytes() == 0) + m_context << Instruction::POP << u256(0); else { - m_context << (u256(1) << (256 - targetType.numBytes() * 8)); - m_context << Instruction::DUP1 << Instruction::SWAP2; - m_context << Instruction::DIV << Instruction::MUL; + m_context << ((u256(1) << (256 - typeOnStack.numBytes() * 8)) - 1); + m_context << Instruction::NOT << Instruction::AND; } } } @@ -637,6 +636,10 @@ void CompilerUtils::convertType(Type const& _typeOnStack, Type const& _targetTyp } break; } + case Type::Category::Bool: + solAssert(_targetType == _typeOnStack, "Invalid conversion for bool."); + if (_cleanupNeeded) + m_context << Instruction::ISZERO << Instruction::ISZERO; default: // All other types should not be convertible to non-equal types. solAssert(_typeOnStack == _targetType, "Invalid type conversion requested."); diff --git a/libsolidity/codegen/ExpressionCompiler.cpp b/libsolidity/codegen/ExpressionCompiler.cpp index a01e306e..16677093 100644 --- a/libsolidity/codegen/ExpressionCompiler.cpp +++ b/libsolidity/codegen/ExpressionCompiler.cpp @@ -360,8 +360,11 @@ bool ExpressionCompiler::visit(BinaryOperation const& _binaryOperation) m_context << commonType.literalValue(nullptr); else { - bool cleanupNeeded = commonType.category() == Type::Category::Integer && - (Token::isCompareOp(c_op) || c_op == Token::Div || c_op == Token::Mod); + bool cleanupNeeded = false; + if (Token::isCompareOp(c_op)) + cleanupNeeded = true; + if (commonType.category() == Type::Category::Integer && (c_op == Token::Div || c_op == Token::Mod)) + cleanupNeeded = true; // for commutative operators, push the literal as late as possible to allow improved optimization auto isLiteral = [](Expression const& _e) -- cgit