From 22bfd3da41ae9efa6e68e884f722502ab3adcf50 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Thu, 29 Mar 2018 09:56:51 +0100 Subject: Use native shift instructions on Constantinople --- libsolidity/codegen/CompilerUtils.cpp | 10 ++++++++-- libsolidity/codegen/ExpressionCompiler.cpp | 13 +++++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) (limited to 'libsolidity/codegen') diff --git a/libsolidity/codegen/CompilerUtils.cpp b/libsolidity/codegen/CompilerUtils.cpp index 4af7d905..46e81d49 100644 --- a/libsolidity/codegen/CompilerUtils.cpp +++ b/libsolidity/codegen/CompilerUtils.cpp @@ -1265,13 +1265,19 @@ void CompilerUtils::cleanHigherOrderBits(IntegerType const& _typeOnStack) void CompilerUtils::leftShiftNumberOnStack(unsigned _bits) { solAssert(_bits < 256, ""); - m_context << (u256(1) << _bits) << Instruction::MUL; + if (m_context.evmVersion().hasBitwiseShifting()) + m_context << _bits << Instruction::SHL; + else + m_context << (u256(1) << _bits) << Instruction::MUL; } void CompilerUtils::rightShiftNumberOnStack(unsigned _bits, bool _isSigned) { solAssert(_bits < 256, ""); - m_context << (u256(1) << _bits) << Instruction::SWAP1 << (_isSigned ? Instruction::SDIV : Instruction::DIV); + if (m_context.evmVersion().hasBitwiseShifting()) + m_context << _bits << (_isSigned ? Instruction::SAR : Instruction::SHR); + else + m_context << (u256(1) << _bits) << Instruction::SWAP1 << (_isSigned ? Instruction::SDIV : Instruction::DIV); } unsigned CompilerUtils::prepareMemoryStore(Type const& _type, bool _padToWords) diff --git a/libsolidity/codegen/ExpressionCompiler.cpp b/libsolidity/codegen/ExpressionCompiler.cpp index 3cf46a9d..1a33fe7c 100644 --- a/libsolidity/codegen/ExpressionCompiler.cpp +++ b/libsolidity/codegen/ExpressionCompiler.cpp @@ -1706,13 +1706,22 @@ void ExpressionCompiler::appendShiftOperatorCode(Token::Value _operator, Type co m_context.appendConditionalInvalid(); } + m_context << Instruction::SWAP1; + // stack: value_to_shift shift_amount + switch (_operator) { case Token::SHL: - m_context << Instruction::SWAP1 << u256(2) << Instruction::EXP << Instruction::MUL; + if (m_context.evmVersion().hasBitwiseShifting()) + m_context << Instruction::SHL; + else + m_context << u256(2) << Instruction::EXP << Instruction::MUL; break; case Token::SAR: - m_context << Instruction::SWAP1 << u256(2) << Instruction::EXP << Instruction::SWAP1 << (c_valueSigned ? Instruction::SDIV : Instruction::DIV); + if (m_context.evmVersion().hasBitwiseShifting()) + m_context << (c_valueSigned ? Instruction::SAR : Instruction::SHR); + else + m_context << u256(2) << Instruction::EXP << Instruction::SWAP1 << (c_valueSigned ? Instruction::SDIV : Instruction::DIV); break; case Token::SHR: default: -- cgit