mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
codegen: add an option to CovertType so that it can truncate sign bits
This commit is contained in:
parent
56d6641086
commit
ea628001d5
@ -358,7 +358,7 @@ void CompilerUtils::pushCombinedFunctionEntryLabel(Declaration const& _function)
|
|||||||
Instruction::OR;
|
Instruction::OR;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CompilerUtils::convertType(Type const& _typeOnStack, Type const& _targetType, bool _cleanupNeeded)
|
void CompilerUtils::convertType(Type const& _typeOnStack, Type const& _targetType, bool _cleanupNeeded, bool _chopSignBits)
|
||||||
{
|
{
|
||||||
// For a type extension, we need to remove all higher-order bits that we might have ignored in
|
// For a type extension, we need to remove all higher-order bits that we might have ignored in
|
||||||
// previous operations.
|
// previous operations.
|
||||||
@ -370,6 +370,12 @@ void CompilerUtils::convertType(Type const& _typeOnStack, Type const& _targetTyp
|
|||||||
Type::Category targetTypeCategory = _targetType.category();
|
Type::Category targetTypeCategory = _targetType.category();
|
||||||
|
|
||||||
bool enumOverflowCheckPending = (targetTypeCategory == Type::Category::Enum || stackTypeCategory == Type::Category::Enum);
|
bool enumOverflowCheckPending = (targetTypeCategory == Type::Category::Enum || stackTypeCategory == Type::Category::Enum);
|
||||||
|
bool chopSignBitsPending = _chopSignBits && targetTypeCategory == Type::Category::Integer;
|
||||||
|
if (chopSignBitsPending)
|
||||||
|
{
|
||||||
|
const IntegerType& targetIntegerType = dynamic_cast<const IntegerType &>(_targetType);
|
||||||
|
chopSignBitsPending = targetIntegerType.isSigned();
|
||||||
|
}
|
||||||
|
|
||||||
switch (stackTypeCategory)
|
switch (stackTypeCategory)
|
||||||
{
|
{
|
||||||
@ -482,6 +488,17 @@ void CompilerUtils::convertType(Type const& _typeOnStack, Type const& _targetTyp
|
|||||||
cleanHigherOrderBits(typeOnStack);
|
cleanHigherOrderBits(typeOnStack);
|
||||||
else if (_cleanupNeeded)
|
else if (_cleanupNeeded)
|
||||||
cleanHigherOrderBits(targetType);
|
cleanHigherOrderBits(targetType);
|
||||||
|
if (chopSignBitsPending)
|
||||||
|
{
|
||||||
|
if (typeOnStack.numBits() < 256)
|
||||||
|
m_context
|
||||||
|
<< (u256(1) << (256 - typeOnStack.numBits()))
|
||||||
|
<< Instruction::SWAP1
|
||||||
|
<< Instruction::DUP2
|
||||||
|
<< Instruction::MUL
|
||||||
|
<< Instruction::DIV;
|
||||||
|
chopSignBitsPending = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -728,6 +745,7 @@ void CompilerUtils::convertType(Type const& _typeOnStack, Type const& _targetTyp
|
|||||||
}
|
}
|
||||||
|
|
||||||
solAssert(!enumOverflowCheckPending, "enum overflow checking missing.");
|
solAssert(!enumOverflowCheckPending, "enum overflow checking missing.");
|
||||||
|
solAssert(!chopSignBitsPending, "forgot to chop the sign bits.");
|
||||||
}
|
}
|
||||||
|
|
||||||
void CompilerUtils::pushZeroValue(Type const& _type)
|
void CompilerUtils::pushZeroValue(Type const& _type)
|
||||||
|
@ -130,7 +130,7 @@ public:
|
|||||||
/// if a reference type is converted from calldata or storage to memory.
|
/// if a reference type is converted from calldata or storage to memory.
|
||||||
/// If @a _cleanupNeeded, high order bits cleanup is also done if no type conversion would be
|
/// If @a _cleanupNeeded, high order bits cleanup is also done if no type conversion would be
|
||||||
/// necessary.
|
/// necessary.
|
||||||
void convertType(Type const& _typeOnStack, Type const& _targetType, bool _cleanupNeeded = false);
|
void convertType(Type const& _typeOnStack, Type const& _targetType, bool _cleanupNeeded = false, bool _chopSignBits = false);
|
||||||
|
|
||||||
/// Creates a zero-value for the given type and puts it onto the stack. This might allocate
|
/// Creates a zero-value for the given type and puts it onto the stack. This might allocate
|
||||||
/// memory for memory references.
|
/// memory for memory references.
|
||||||
|
@ -234,7 +234,7 @@ void StorageItem::storeValue(Type const& _sourceType, SourceLocation const& _loc
|
|||||||
|
|
||||||
m_context << Instruction::SWAP1;
|
m_context << Instruction::SWAP1;
|
||||||
utils.convertType(_sourceType, _sourceType, true);
|
utils.convertType(_sourceType, _sourceType, true);
|
||||||
utils.convertType(*m_dataType, *m_dataType, true);
|
utils.convertType(*m_dataType, *m_dataType, true, true);
|
||||||
m_context << Instruction::SWAP1;
|
m_context << Instruction::SWAP1;
|
||||||
|
|
||||||
m_context << Instruction::SSTORE;
|
m_context << Instruction::SSTORE;
|
||||||
@ -280,13 +280,7 @@ void StorageItem::storeValue(Type const& _sourceType, SourceLocation const& _loc
|
|||||||
{
|
{
|
||||||
solAssert(m_dataType->sizeOnStack() == 1, "Invalid stack size for opaque type.");
|
solAssert(m_dataType->sizeOnStack() == 1, "Invalid stack size for opaque type.");
|
||||||
// remove the higher order bits
|
// remove the higher order bits
|
||||||
utils.convertType(*m_dataType, *m_dataType, true);
|
utils.convertType(*m_dataType, *m_dataType, true, true);
|
||||||
m_context
|
|
||||||
<< (u256(1) << (8 * (32 - m_dataType->storageBytes())))
|
|
||||||
<< Instruction::SWAP1
|
|
||||||
<< Instruction::DUP2
|
|
||||||
<< Instruction::MUL
|
|
||||||
<< Instruction::DIV;
|
|
||||||
}
|
}
|
||||||
m_context << Instruction::MUL << Instruction::OR;
|
m_context << Instruction::MUL << Instruction::OR;
|
||||||
// stack: value storage_ref updated_value
|
// stack: value storage_ref updated_value
|
||||||
|
Loading…
Reference in New Issue
Block a user