mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Packing for arrays.
This commit is contained in:
parent
895c08342c
commit
2cde4f3404
190
ArrayUtils.cpp
190
ArrayUtils.cpp
@ -52,15 +52,21 @@ void ArrayUtils::copyArrayToStorage(ArrayType const& _targetType, ArrayType cons
|
|||||||
// TODO unroll loop for small sizes
|
// TODO unroll loop for small sizes
|
||||||
|
|
||||||
bool sourceIsStorage = _sourceType.getLocation() == ArrayType::Location::Storage;
|
bool sourceIsStorage = _sourceType.getLocation() == ArrayType::Location::Storage;
|
||||||
|
bool directCopy = sourceIsStorage && sourceBaseType->isValueType() && *sourceBaseType == *targetBaseType;
|
||||||
|
bool haveByteOffsetSource = !directCopy && sourceIsStorage && sourceBaseType->getStorageBytes() <= 16;
|
||||||
|
bool haveByteOffsetTarget = !directCopy && targetBaseType->getStorageBytes() <= 16;
|
||||||
|
unsigned byteOffsetSize = (haveByteOffsetSource ? 1 : 0) + (haveByteOffsetTarget ? 1 : 0);
|
||||||
|
|
||||||
// stack: source_ref [source_byte_off] [source_length] target_ref target_byte_off
|
// stack: source_ref [source_byte_off] [source_length] target_ref target_byte_off
|
||||||
// store target_ref
|
// store target_ref
|
||||||
m_context << eth::Instruction::POP; //@todo
|
// arrays always start at zero byte offset, pop offset
|
||||||
|
m_context << eth::Instruction::POP;
|
||||||
for (unsigned i = _sourceType.getSizeOnStack(); i > 0; --i)
|
for (unsigned i = _sourceType.getSizeOnStack(); i > 0; --i)
|
||||||
m_context << eth::swapInstruction(i);
|
m_context << eth::swapInstruction(i);
|
||||||
// stack: target_ref source_ref [source_byte_off] [source_length]
|
// stack: target_ref source_ref [source_byte_off] [source_length]
|
||||||
if (sourceIsStorage)
|
if (sourceIsStorage)
|
||||||
m_context << eth::Instruction::POP; //@todo
|
// arrays always start at zero byte offset, pop offset
|
||||||
|
m_context << eth::Instruction::POP;
|
||||||
// stack: target_ref source_ref [source_length]
|
// stack: target_ref source_ref [source_length]
|
||||||
// retrieve source length
|
// retrieve source length
|
||||||
if (_sourceType.getLocation() != ArrayType::Location::CallData || !_sourceType.isDynamicallySized())
|
if (_sourceType.getLocation() != ArrayType::Location::CallData || !_sourceType.isDynamicallySized())
|
||||||
@ -81,7 +87,7 @@ void ArrayUtils::copyArrayToStorage(ArrayType const& _targetType, ArrayType cons
|
|||||||
m_context
|
m_context
|
||||||
<< eth::Instruction::POP << eth::Instruction::POP
|
<< eth::Instruction::POP << eth::Instruction::POP
|
||||||
<< eth::Instruction::POP << eth::Instruction::POP;
|
<< eth::Instruction::POP << eth::Instruction::POP;
|
||||||
m_context << u256(0); //@todo
|
m_context << u256(0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// compute hashes (data positions)
|
// compute hashes (data positions)
|
||||||
@ -97,8 +103,8 @@ void ArrayUtils::copyArrayToStorage(ArrayType const& _targetType, ArrayType cons
|
|||||||
// stack: target_ref target_data_end source_length target_data_pos source_ref
|
// stack: target_ref target_data_end source_length target_data_pos source_ref
|
||||||
// skip copying if source length is zero
|
// skip copying if source length is zero
|
||||||
m_context << eth::Instruction::DUP3 << eth::Instruction::ISZERO;
|
m_context << eth::Instruction::DUP3 << eth::Instruction::ISZERO;
|
||||||
eth::AssemblyItem copyLoopEnd = m_context.newTag();
|
eth::AssemblyItem copyLoopEndWithoutByteOffset = m_context.newTag();
|
||||||
m_context.appendConditionalJumpTo(copyLoopEnd);
|
m_context.appendConditionalJumpTo(copyLoopEndWithoutByteOffset);
|
||||||
|
|
||||||
if (_sourceType.getLocation() == ArrayType::Location::Storage && _sourceType.isDynamicallySized())
|
if (_sourceType.getLocation() == ArrayType::Location::Storage && _sourceType.isDynamicallySized())
|
||||||
CompilerUtils(m_context).computeHashStatic();
|
CompilerUtils(m_context).computeHashStatic();
|
||||||
@ -107,18 +113,24 @@ void ArrayUtils::copyArrayToStorage(ArrayType const& _targetType, ArrayType cons
|
|||||||
convertLengthToSize(_sourceType);
|
convertLengthToSize(_sourceType);
|
||||||
m_context << eth::Instruction::DUP3 << eth::Instruction::ADD;
|
m_context << eth::Instruction::DUP3 << eth::Instruction::ADD;
|
||||||
// stack: target_ref target_data_end source_data_pos target_data_pos source_data_end
|
// stack: target_ref target_data_end source_data_pos target_data_pos source_data_end
|
||||||
|
if (haveByteOffsetTarget)
|
||||||
|
m_context << u256(0);
|
||||||
|
if (haveByteOffsetSource)
|
||||||
|
m_context << u256(0);
|
||||||
|
// stack: target_ref target_data_end source_data_pos target_data_pos source_data_end [target_byte_offset] [source_byte_offset]
|
||||||
eth::AssemblyItem copyLoopStart = m_context.newTag();
|
eth::AssemblyItem copyLoopStart = m_context.newTag();
|
||||||
m_context << copyLoopStart;
|
m_context << copyLoopStart;
|
||||||
// check for loop condition
|
// check for loop condition
|
||||||
m_context
|
m_context
|
||||||
<< eth::Instruction::DUP3 << eth::Instruction::DUP2
|
<< eth::dupInstruction(3 + byteOffsetSize) << eth::dupInstruction(2 + byteOffsetSize)
|
||||||
<< eth::Instruction::GT << eth::Instruction::ISZERO;
|
<< eth::Instruction::GT << eth::Instruction::ISZERO;
|
||||||
|
eth::AssemblyItem copyLoopEnd = m_context.newTag();
|
||||||
m_context.appendConditionalJumpTo(copyLoopEnd);
|
m_context.appendConditionalJumpTo(copyLoopEnd);
|
||||||
// stack: target_ref target_data_end source_data_pos target_data_pos source_data_end
|
// stack: target_ref target_data_end source_data_pos target_data_pos source_data_end [target_byte_offset] [source_byte_offset]
|
||||||
// copy
|
// copy
|
||||||
if (sourceBaseType->getCategory() == Type::Category::Array)
|
if (sourceBaseType->getCategory() == Type::Category::Array)
|
||||||
{
|
{
|
||||||
//@todo
|
solAssert(byteOffsetSize == 0, "Byte offset for array as base type.");
|
||||||
m_context << eth::Instruction::DUP3;
|
m_context << eth::Instruction::DUP3;
|
||||||
if (sourceIsStorage)
|
if (sourceIsStorage)
|
||||||
m_context << u256(0);
|
m_context << u256(0);
|
||||||
@ -129,11 +141,25 @@ void ArrayUtils::copyArrayToStorage(ArrayType const& _targetType, ArrayType cons
|
|||||||
);
|
);
|
||||||
m_context << eth::Instruction::POP << eth::Instruction::POP;
|
m_context << eth::Instruction::POP << eth::Instruction::POP;
|
||||||
}
|
}
|
||||||
|
else if (directCopy)
|
||||||
|
{
|
||||||
|
solAssert(byteOffsetSize == 0, "Byte offset for direct copy.");
|
||||||
|
m_context
|
||||||
|
<< eth::Instruction::DUP3 << eth::Instruction::SLOAD
|
||||||
|
<< eth::Instruction::DUP3 << eth::Instruction::SSTORE;
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
m_context << eth::Instruction::DUP3;
|
// Note that we have to copy each element on its own in case conversion is involved.
|
||||||
|
// We might copy too much if there is padding at the last element, but this way end
|
||||||
|
// checking is easier.
|
||||||
|
// stack: target_ref target_data_end source_data_pos target_data_pos source_data_end [target_byte_offset] [source_byte_offset]
|
||||||
|
m_context << eth::dupInstruction(3 + byteOffsetSize);
|
||||||
if (_sourceType.getLocation() == ArrayType::Location::Storage)
|
if (_sourceType.getLocation() == ArrayType::Location::Storage)
|
||||||
{
|
{
|
||||||
|
if (haveByteOffsetSource)
|
||||||
|
m_context << eth::Instruction::DUP2;
|
||||||
|
else
|
||||||
m_context << u256(0);
|
m_context << u256(0);
|
||||||
StorageItem(m_context, *sourceBaseType).retrieveValue(SourceLocation(), true);
|
StorageItem(m_context, *sourceBaseType).retrieveValue(SourceLocation(), true);
|
||||||
}
|
}
|
||||||
@ -141,24 +167,54 @@ void ArrayUtils::copyArrayToStorage(ArrayType const& _targetType, ArrayType cons
|
|||||||
CompilerUtils(m_context).loadFromMemoryDynamic(*sourceBaseType, true, true, false);
|
CompilerUtils(m_context).loadFromMemoryDynamic(*sourceBaseType, true, true, false);
|
||||||
else
|
else
|
||||||
solAssert(false, "Copying of unknown type requested: " + sourceBaseType->toString());
|
solAssert(false, "Copying of unknown type requested: " + sourceBaseType->toString());
|
||||||
solAssert(2 + sourceBaseType->getSizeOnStack() <= 16, "Stack too deep.");
|
// stack: target_ref target_data_end source_data_pos target_data_pos source_data_end [target_byte_offset] [source_byte_offset] <source_value>...
|
||||||
m_context << eth::dupInstruction(2 + sourceBaseType->getSizeOnStack()) << u256(0);
|
solAssert(2 + byteOffsetSize + sourceBaseType->getSizeOnStack() <= 16, "Stack too deep.");
|
||||||
|
// fetch target storage reference
|
||||||
|
m_context << eth::dupInstruction(2 + byteOffsetSize + sourceBaseType->getSizeOnStack());
|
||||||
|
if (haveByteOffsetTarget)
|
||||||
|
m_context << eth::dupInstruction(1 + byteOffsetSize + sourceBaseType->getSizeOnStack());
|
||||||
|
else
|
||||||
|
m_context << u256(0);
|
||||||
StorageItem(m_context, *targetBaseType).storeValue(*sourceBaseType, SourceLocation(), true);
|
StorageItem(m_context, *targetBaseType).storeValue(*sourceBaseType, SourceLocation(), true);
|
||||||
}
|
}
|
||||||
|
// stack: target_ref target_data_end source_data_pos target_data_pos source_data_end [target_byte_offset] [source_byte_offset]
|
||||||
// increment source
|
// increment source
|
||||||
|
if (haveByteOffsetSource)
|
||||||
|
incrementByteOffset(sourceBaseType->getStorageBytes(), 1, haveByteOffsetTarget ? 5 : 4);
|
||||||
|
else
|
||||||
m_context
|
m_context
|
||||||
<< eth::Instruction::SWAP2
|
<< eth::swapInstruction(2 + byteOffsetSize)
|
||||||
<< (sourceIsStorage ? sourceBaseType->getStorageSize() : sourceBaseType->getCalldataEncodedSize())
|
<< (sourceIsStorage ? sourceBaseType->getStorageSize() : sourceBaseType->getCalldataEncodedSize())
|
||||||
<< eth::Instruction::ADD
|
<< eth::Instruction::ADD
|
||||||
<< eth::Instruction::SWAP2;
|
<< eth::swapInstruction(2 + byteOffsetSize);
|
||||||
// increment target
|
// increment target
|
||||||
|
if (haveByteOffsetTarget)
|
||||||
|
incrementByteOffset(targetBaseType->getStorageBytes(), byteOffsetSize, byteOffsetSize + 2);
|
||||||
|
else
|
||||||
m_context
|
m_context
|
||||||
<< eth::Instruction::SWAP1
|
<< eth::swapInstruction(1 + byteOffsetSize)
|
||||||
<< targetBaseType->getStorageSize()
|
<< targetBaseType->getStorageSize()
|
||||||
<< eth::Instruction::ADD
|
<< eth::Instruction::ADD
|
||||||
<< eth::Instruction::SWAP1;
|
<< eth::swapInstruction(1 + byteOffsetSize);
|
||||||
m_context.appendJumpTo(copyLoopStart);
|
m_context.appendJumpTo(copyLoopStart);
|
||||||
m_context << copyLoopEnd;
|
m_context << copyLoopEnd;
|
||||||
|
if (haveByteOffsetTarget)
|
||||||
|
{
|
||||||
|
// clear elements that might be left over in the current slot in target
|
||||||
|
// stack: target_ref target_data_end source_data_pos target_data_pos source_data_end target_byte_offset [source_byte_offset]
|
||||||
|
m_context << eth::dupInstruction(byteOffsetSize) << eth::Instruction::ISZERO;
|
||||||
|
eth::AssemblyItem copyCleanupLoopEnd = m_context.appendConditionalJump();
|
||||||
|
m_context << eth::dupInstruction(2 + byteOffsetSize) << eth::dupInstruction(1 + byteOffsetSize);
|
||||||
|
StorageItem(m_context, *targetBaseType).setToZero(SourceLocation(), true);
|
||||||
|
incrementByteOffset(targetBaseType->getStorageBytes(), byteOffsetSize, byteOffsetSize + 2);
|
||||||
|
m_context.appendJumpTo(copyLoopEnd);
|
||||||
|
|
||||||
|
m_context << copyCleanupLoopEnd;
|
||||||
|
m_context << eth::Instruction::POP; // might pop the source, but then target is popped next
|
||||||
|
}
|
||||||
|
if (haveByteOffsetSource)
|
||||||
|
m_context << eth::Instruction::POP;
|
||||||
|
m_context << copyLoopEndWithoutByteOffset;
|
||||||
|
|
||||||
// zero-out leftovers in target
|
// zero-out leftovers in target
|
||||||
// stack: target_ref target_data_end source_data_pos target_data_pos_updated source_data_end
|
// stack: target_ref target_data_end source_data_pos target_data_pos_updated source_data_end
|
||||||
@ -166,41 +222,61 @@ void ArrayUtils::copyArrayToStorage(ArrayType const& _targetType, ArrayType cons
|
|||||||
// stack: target_ref target_data_end target_data_pos_updated
|
// stack: target_ref target_data_end target_data_pos_updated
|
||||||
clearStorageLoop(*targetBaseType);
|
clearStorageLoop(*targetBaseType);
|
||||||
m_context << eth::Instruction::POP;
|
m_context << eth::Instruction::POP;
|
||||||
m_context << u256(0); //@todo
|
m_context << u256(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ArrayUtils::clearArray(ArrayType const& _type) const
|
void ArrayUtils::clearArray(ArrayType const& _type) const
|
||||||
{
|
{
|
||||||
unsigned stackHeightStart = m_context.getStackHeight();
|
unsigned stackHeightStart = m_context.getStackHeight();
|
||||||
solAssert(_type.getLocation() == ArrayType::Location::Storage, "");
|
solAssert(_type.getLocation() == ArrayType::Location::Storage, "");
|
||||||
if (_type.isDynamicallySized())
|
if (_type.getBaseType()->getStorageBytes() < 32)
|
||||||
{
|
{
|
||||||
m_context << eth::Instruction::POP; // remove byte offset
|
solAssert(_type.getBaseType()->isValueType(), "Invalid storage size for non-value type.");
|
||||||
clearDynamicArray(_type);
|
solAssert(_type.getBaseType()->getStorageSize() <= 1, "Invalid storage size for type.");
|
||||||
}
|
}
|
||||||
|
if (_type.getBaseType()->isValueType())
|
||||||
|
solAssert(_type.getBaseType()->getStorageSize() <= 1, "Invalid size for value type.");
|
||||||
|
|
||||||
|
m_context << eth::Instruction::POP; // remove byte offset
|
||||||
|
if (_type.isDynamicallySized())
|
||||||
|
clearDynamicArray(_type);
|
||||||
else if (_type.getLength() == 0 || _type.getBaseType()->getCategory() == Type::Category::Mapping)
|
else if (_type.getLength() == 0 || _type.getBaseType()->getCategory() == Type::Category::Mapping)
|
||||||
m_context << eth::Instruction::POP << eth::Instruction::POP;
|
m_context << eth::Instruction::POP;
|
||||||
else if (_type.getLength() < 5) // unroll loop for small arrays @todo choose a good value
|
else if (_type.getBaseType()->isValueType() && _type.getStorageSize() <= 5)
|
||||||
{
|
{
|
||||||
solAssert(!_type.isByteArray(), "");
|
// unroll loop for small arrays @todo choose a good value
|
||||||
|
// Note that we loop over storage slots here, not elements.
|
||||||
|
for (unsigned i = 1; i < _type.getStorageSize(); ++i)
|
||||||
|
m_context
|
||||||
|
<< u256(0) << eth::Instruction::DUP2 << eth::Instruction::SSTORE
|
||||||
|
<< u256(1) << eth::Instruction::ADD;
|
||||||
|
m_context << u256(0) << eth::Instruction::SWAP1 << eth::Instruction::SSTORE;
|
||||||
|
}
|
||||||
|
else if (!_type.getBaseType()->isValueType() && _type.getLength() <= 4)
|
||||||
|
{
|
||||||
|
// unroll loop for small arrays @todo choose a good value
|
||||||
|
solAssert(_type.getBaseType()->getStorageBytes() >= 32, "Invalid storage size.");
|
||||||
for (unsigned i = 1; i < _type.getLength(); ++i)
|
for (unsigned i = 1; i < _type.getLength(); ++i)
|
||||||
{
|
{
|
||||||
|
m_context << u256(0);
|
||||||
StorageItem(m_context, *_type.getBaseType()).setToZero(SourceLocation(), false);
|
StorageItem(m_context, *_type.getBaseType()).setToZero(SourceLocation(), false);
|
||||||
m_context << eth::Instruction::SWAP1;
|
m_context
|
||||||
m_context << u256(_type.getBaseType()->getStorageSize()) << eth::Instruction::ADD;
|
<< eth::Instruction::POP
|
||||||
m_context << eth::Instruction::SWAP1;
|
<< u256(_type.getBaseType()->getStorageSize()) << eth::Instruction::ADD;
|
||||||
}
|
}
|
||||||
|
m_context << u256(0);
|
||||||
StorageItem(m_context, *_type.getBaseType()).setToZero(SourceLocation(), true);
|
StorageItem(m_context, *_type.getBaseType()).setToZero(SourceLocation(), true);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
solAssert(!_type.isByteArray(), "");
|
|
||||||
m_context << eth::Instruction::SWAP1;
|
|
||||||
m_context << eth::Instruction::DUP1 << _type.getLength();
|
m_context << eth::Instruction::DUP1 << _type.getLength();
|
||||||
convertLengthToSize(_type);
|
convertLengthToSize(_type);
|
||||||
m_context << eth::Instruction::ADD << eth::Instruction::SWAP1;
|
m_context << eth::Instruction::ADD << eth::Instruction::SWAP1;
|
||||||
|
if (_type.getBaseType()->getStorageBytes() < 32)
|
||||||
|
clearStorageLoop(IntegerType(256));
|
||||||
|
else
|
||||||
clearStorageLoop(*_type.getBaseType());
|
clearStorageLoop(*_type.getBaseType());
|
||||||
m_context << eth::Instruction::POP << eth::Instruction::POP;
|
m_context << eth::Instruction::POP;
|
||||||
}
|
}
|
||||||
solAssert(m_context.getStackHeight() == stackHeightStart - 2, "");
|
solAssert(m_context.getStackHeight() == stackHeightStart - 2, "");
|
||||||
}
|
}
|
||||||
@ -224,7 +300,7 @@ void ArrayUtils::clearDynamicArray(ArrayType const& _type) const
|
|||||||
m_context << eth::Instruction::SWAP1 << eth::Instruction::DUP2 << eth::Instruction::ADD
|
m_context << eth::Instruction::SWAP1 << eth::Instruction::DUP2 << eth::Instruction::ADD
|
||||||
<< eth::Instruction::SWAP1;
|
<< eth::Instruction::SWAP1;
|
||||||
// stack: data_pos_end data_pos
|
// stack: data_pos_end data_pos
|
||||||
if (_type.isByteArray())
|
if (_type.isByteArray() || _type.getBaseType()->getStorageBytes() < 32)
|
||||||
clearStorageLoop(IntegerType(256));
|
clearStorageLoop(IntegerType(256));
|
||||||
else
|
else
|
||||||
clearStorageLoop(*_type.getBaseType());
|
clearStorageLoop(*_type.getBaseType());
|
||||||
@ -237,6 +313,8 @@ void ArrayUtils::resizeDynamicArray(const ArrayType& _type) const
|
|||||||
{
|
{
|
||||||
solAssert(_type.getLocation() == ArrayType::Location::Storage, "");
|
solAssert(_type.getLocation() == ArrayType::Location::Storage, "");
|
||||||
solAssert(_type.isDynamicallySized(), "");
|
solAssert(_type.isDynamicallySized(), "");
|
||||||
|
if (!_type.isByteArray() && _type.getBaseType()->getStorageBytes() < 32)
|
||||||
|
solAssert(_type.getBaseType()->isValueType(), "Invalid storage size for non-value type.");
|
||||||
|
|
||||||
unsigned stackHeightStart = m_context.getStackHeight();
|
unsigned stackHeightStart = m_context.getStackHeight();
|
||||||
eth::AssemblyItem resizeEnd = m_context.newTag();
|
eth::AssemblyItem resizeEnd = m_context.newTag();
|
||||||
@ -266,7 +344,7 @@ void ArrayUtils::resizeDynamicArray(const ArrayType& _type) const
|
|||||||
// stack: ref new_length data_pos new_size delete_end
|
// stack: ref new_length data_pos new_size delete_end
|
||||||
m_context << eth::Instruction::SWAP2 << eth::Instruction::ADD;
|
m_context << eth::Instruction::SWAP2 << eth::Instruction::ADD;
|
||||||
// stack: ref new_length delete_end delete_start
|
// stack: ref new_length delete_end delete_start
|
||||||
if (_type.isByteArray())
|
if (_type.isByteArray() || _type.getBaseType()->getStorageBytes() < 32)
|
||||||
clearStorageLoop(IntegerType(256));
|
clearStorageLoop(IntegerType(256));
|
||||||
else
|
else
|
||||||
clearStorageLoop(*_type.getBaseType());
|
clearStorageLoop(*_type.getBaseType());
|
||||||
@ -294,7 +372,7 @@ void ArrayUtils::clearStorageLoop(Type const& _type) const
|
|||||||
eth::AssemblyItem zeroLoopEnd = m_context.newTag();
|
eth::AssemblyItem zeroLoopEnd = m_context.newTag();
|
||||||
m_context.appendConditionalJumpTo(zeroLoopEnd);
|
m_context.appendConditionalJumpTo(zeroLoopEnd);
|
||||||
// delete
|
// delete
|
||||||
m_context << u256(0); //@todo
|
m_context << u256(0);
|
||||||
StorageItem(m_context, _type).setToZero(SourceLocation(), false);
|
StorageItem(m_context, _type).setToZero(SourceLocation(), false);
|
||||||
m_context << eth::Instruction::POP;
|
m_context << eth::Instruction::POP;
|
||||||
// increment
|
// increment
|
||||||
@ -313,7 +391,20 @@ void ArrayUtils::convertLengthToSize(ArrayType const& _arrayType, bool _pad) con
|
|||||||
if (_arrayType.isByteArray())
|
if (_arrayType.isByteArray())
|
||||||
m_context << u256(31) << eth::Instruction::ADD
|
m_context << u256(31) << eth::Instruction::ADD
|
||||||
<< u256(32) << eth::Instruction::SWAP1 << eth::Instruction::DIV;
|
<< u256(32) << eth::Instruction::SWAP1 << eth::Instruction::DIV;
|
||||||
else if (_arrayType.getBaseType()->getStorageSize() > 1)
|
else if (_arrayType.getBaseType()->getStorageSize() <= 1)
|
||||||
|
{
|
||||||
|
unsigned baseBytes = _arrayType.getBaseType()->getStorageBytes();
|
||||||
|
if (baseBytes == 0)
|
||||||
|
m_context << eth::Instruction::POP << u256(1);
|
||||||
|
else if (baseBytes <= 16)
|
||||||
|
{
|
||||||
|
unsigned itemsPerSlot = 32 / baseBytes;
|
||||||
|
m_context
|
||||||
|
<< u256(itemsPerSlot - 1) << eth::Instruction::ADD
|
||||||
|
<< u256(itemsPerSlot) << eth::Instruction::SWAP1 << eth::Instruction::DIV;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
m_context << _arrayType.getBaseType()->getStorageSize() << eth::Instruction::MUL;
|
m_context << _arrayType.getBaseType()->getStorageSize() << eth::Instruction::MUL;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -349,3 +440,38 @@ void ArrayUtils::retrieveLength(ArrayType const& _arrayType) const
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ArrayUtils::incrementByteOffset(unsigned byteSize, unsigned byteOffsetPosition, unsigned storageOffsetPosition) const
|
||||||
|
{
|
||||||
|
solAssert(byteSize < 32, "");
|
||||||
|
// We do the following, but avoiding jumps:
|
||||||
|
// byteOffset += byteSize
|
||||||
|
// if (byteOffset + byteSize > 32)
|
||||||
|
// {
|
||||||
|
// storageOffset++;
|
||||||
|
// byteOffset = 0;
|
||||||
|
// }
|
||||||
|
if (byteOffsetPosition > 1)
|
||||||
|
m_context << eth::swapInstruction(byteOffsetPosition - 1);
|
||||||
|
m_context << u256(byteSize) << eth::Instruction::ADD;
|
||||||
|
if (byteOffsetPosition > 1)
|
||||||
|
m_context << eth::swapInstruction(byteOffsetPosition - 1);
|
||||||
|
// compute, X := (byteOffset + byteSize - 1) / 32, should be 1 iff byteOffset + bytesize > 32
|
||||||
|
m_context
|
||||||
|
<< u256(32) << eth::dupInstruction(1 + byteOffsetPosition) << u256(byteSize - 1)
|
||||||
|
<< eth::Instruction::ADD << eth::Instruction::DIV;
|
||||||
|
// increment storage offset if X == 1 (just add X to it)
|
||||||
|
// stack: X
|
||||||
|
m_context
|
||||||
|
<< eth::swapInstruction(storageOffsetPosition) << eth::dupInstruction(storageOffsetPosition + 1)
|
||||||
|
<< eth::Instruction::ADD << eth::swapInstruction(storageOffsetPosition);
|
||||||
|
// stack: X
|
||||||
|
// set source_byte_offset to zero if X == 1 (using source_byte_offset *= 1 - X)
|
||||||
|
m_context << u256(1) << eth::Instruction::SUB;
|
||||||
|
// stack: 1 - X
|
||||||
|
if (byteOffsetPosition == 1)
|
||||||
|
m_context << eth::Instruction::MUL;
|
||||||
|
else
|
||||||
|
m_context
|
||||||
|
<< eth::dupInstruction(byteOffsetPosition + 1) << eth::Instruction::MUL
|
||||||
|
<< eth::swapInstruction(byteOffsetPosition) << eth::Instruction::POP;
|
||||||
|
}
|
||||||
|
@ -72,6 +72,12 @@ public:
|
|||||||
void retrieveLength(ArrayType const& _arrayType) const;
|
void retrieveLength(ArrayType const& _arrayType) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
/// Adds the given number of bytes to a storage byte offset counter and also increments
|
||||||
|
/// the storage offset if adding this number again would increase the counter over 32.
|
||||||
|
/// @param byteOffsetPosition the stack offset of the storage byte offset
|
||||||
|
/// @param storageOffsetPosition the stack offset of the storage slot offset
|
||||||
|
void incrementByteOffset(unsigned byteSize, unsigned byteOffsetPosition, unsigned storageOffsetPosition) const;
|
||||||
|
|
||||||
CompilerContext& m_context;
|
CompilerContext& m_context;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -753,7 +753,6 @@ bool ExpressionCompiler::visit(IndexAccess const& _indexAccess)
|
|||||||
}
|
}
|
||||||
else if (baseType.getCategory() == Type::Category::Array)
|
else if (baseType.getCategory() == Type::Category::Array)
|
||||||
{
|
{
|
||||||
// stack layout: <base_ref> [storage_byte_offset] [<length>] <index>
|
|
||||||
ArrayType const& arrayType = dynamic_cast<ArrayType const&>(baseType);
|
ArrayType const& arrayType = dynamic_cast<ArrayType const&>(baseType);
|
||||||
solAssert(_indexAccess.getIndexExpression(), "Index expression expected.");
|
solAssert(_indexAccess.getIndexExpression(), "Index expression expected.");
|
||||||
ArrayType::Location location = arrayType.getLocation();
|
ArrayType::Location location = arrayType.getLocation();
|
||||||
@ -762,6 +761,11 @@ bool ExpressionCompiler::visit(IndexAccess const& _indexAccess)
|
|||||||
location == ArrayType::Location::Memory ? eth::Instruction::MLOAD :
|
location == ArrayType::Location::Memory ? eth::Instruction::MLOAD :
|
||||||
eth::Instruction::CALLDATALOAD;
|
eth::Instruction::CALLDATALOAD;
|
||||||
|
|
||||||
|
// remove storage byte offset
|
||||||
|
if (location == ArrayType::Location::Storage)
|
||||||
|
m_context << eth::Instruction::POP;
|
||||||
|
|
||||||
|
// stack layout: <base_ref> [<length>] <index>
|
||||||
_indexAccess.getIndexExpression()->accept(*this);
|
_indexAccess.getIndexExpression()->accept(*this);
|
||||||
// retrieve length
|
// retrieve length
|
||||||
if (!arrayType.isDynamicallySized())
|
if (!arrayType.isDynamicallySized())
|
||||||
@ -769,11 +773,9 @@ bool ExpressionCompiler::visit(IndexAccess const& _indexAccess)
|
|||||||
else if (location == ArrayType::Location::CallData)
|
else if (location == ArrayType::Location::CallData)
|
||||||
// length is stored on the stack
|
// length is stored on the stack
|
||||||
m_context << eth::Instruction::SWAP1;
|
m_context << eth::Instruction::SWAP1;
|
||||||
else if (location == ArrayType::Location::Storage)
|
|
||||||
m_context << eth::Instruction::DUP3 << load;
|
|
||||||
else
|
else
|
||||||
m_context << eth::Instruction::DUP2 << load;
|
m_context << eth::Instruction::DUP2 << load;
|
||||||
// stack: <base_ref> [storage_byte_offset] <index> <length>
|
// stack: <base_ref> <index> <length>
|
||||||
// check out-of-bounds access
|
// check out-of-bounds access
|
||||||
m_context << eth::Instruction::DUP2 << eth::Instruction::LT;
|
m_context << eth::Instruction::DUP2 << eth::Instruction::LT;
|
||||||
eth::AssemblyItem legalAccess = m_context.appendConditionalJump();
|
eth::AssemblyItem legalAccess = m_context.appendConditionalJump();
|
||||||
@ -781,23 +783,22 @@ bool ExpressionCompiler::visit(IndexAccess const& _indexAccess)
|
|||||||
m_context << eth::Instruction::STOP;
|
m_context << eth::Instruction::STOP;
|
||||||
|
|
||||||
m_context << legalAccess;
|
m_context << legalAccess;
|
||||||
// stack: <base_ref> [storage_byte_offset] <index>
|
// stack: <base_ref> <index>
|
||||||
if (arrayType.isByteArray())
|
if (arrayType.isByteArray())
|
||||||
// byte array is packed differently, especially in storage
|
|
||||||
switch (location)
|
switch (location)
|
||||||
{
|
{
|
||||||
case ArrayType::Location::Storage:
|
case ArrayType::Location::Storage:
|
||||||
// byte array index storage lvalue on stack (goal):
|
// byte array index storage lvalue on stack (goal):
|
||||||
// <ref> <byte_number> = <base_ref + index / 32> <index % 32>
|
// <ref> <byte_number> = <base_ref + index / 32> <index % 32>
|
||||||
m_context << u256(32) << eth::Instruction::SWAP3;
|
m_context << u256(32) << eth::Instruction::SWAP2;
|
||||||
CompilerUtils(m_context).computeHashStatic();
|
CompilerUtils(m_context).computeHashStatic();
|
||||||
// stack: 32 storage_byte_offset index data_ref
|
// stack: 32 index data_ref
|
||||||
m_context
|
m_context
|
||||||
<< eth::Instruction::DUP4 << eth::Instruction::DUP3
|
<< eth::Instruction::DUP3 << eth::Instruction::DUP3
|
||||||
<< eth::Instruction::DIV << eth::Instruction::ADD
|
<< eth::Instruction::DIV << eth::Instruction::ADD
|
||||||
// stack: 32 storage_byte_offset index (data_ref + index / 32)
|
// stack: 32 index (data_ref + index / 32)
|
||||||
<< eth::Instruction::SWAP3 << eth::Instruction::SWAP2
|
<< eth::Instruction::SWAP2 << eth::Instruction::SWAP1
|
||||||
<< eth::Instruction::POP << eth::Instruction::MOD;
|
<< eth::Instruction::MOD;
|
||||||
setLValue<StorageByteArrayElement>(_indexAccess);
|
setLValue<StorageByteArrayElement>(_indexAccess);
|
||||||
break;
|
break;
|
||||||
case ArrayType::Location::CallData:
|
case ArrayType::Location::CallData:
|
||||||
@ -811,36 +812,50 @@ bool ExpressionCompiler::visit(IndexAccess const& _indexAccess)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// stack: <base_ref> [storage_byte_offset] <index>
|
// stack: <base_ref> <index>
|
||||||
if (location == ArrayType::Location::Storage)
|
m_context << eth::Instruction::SWAP1;
|
||||||
//@todo use byte offset, remove it for now
|
|
||||||
m_context << eth::Instruction::SWAP1 << eth::Instruction::POP;
|
|
||||||
u256 elementSize =
|
|
||||||
location == ArrayType::Location::Storage ?
|
|
||||||
arrayType.getBaseType()->getStorageSize() :
|
|
||||||
arrayType.getBaseType()->getCalldataEncodedSize();
|
|
||||||
solAssert(elementSize != 0, "Invalid element size.");
|
|
||||||
if (elementSize > 1)
|
|
||||||
m_context << elementSize << eth::Instruction::MUL;
|
|
||||||
if (arrayType.isDynamicallySized())
|
if (arrayType.isDynamicallySized())
|
||||||
{
|
{
|
||||||
if (location == ArrayType::Location::Storage)
|
if (location == ArrayType::Location::Storage)
|
||||||
{
|
|
||||||
m_context << eth::Instruction::SWAP1;
|
|
||||||
CompilerUtils(m_context).computeHashStatic();
|
CompilerUtils(m_context).computeHashStatic();
|
||||||
}
|
|
||||||
else if (location == ArrayType::Location::Memory)
|
else if (location == ArrayType::Location::Memory)
|
||||||
m_context << u256(32) << eth::Instruction::ADD;
|
m_context << u256(32) << eth::Instruction::ADD;
|
||||||
}
|
}
|
||||||
m_context << eth::Instruction::ADD;
|
// stack: <index> <data_ref>
|
||||||
switch (location)
|
switch (location)
|
||||||
{
|
{
|
||||||
case ArrayType::Location::CallData:
|
case ArrayType::Location::CallData:
|
||||||
|
m_context
|
||||||
|
<< eth::Instruction::SWAP1 << arrayType.getBaseType()->getCalldataEncodedSize()
|
||||||
|
<< eth::Instruction::MUL << eth::Instruction::ADD;
|
||||||
if (arrayType.getBaseType()->isValueType())
|
if (arrayType.getBaseType()->isValueType())
|
||||||
CompilerUtils(m_context).loadFromMemoryDynamic(*arrayType.getBaseType(), true, true, false);
|
CompilerUtils(m_context).loadFromMemoryDynamic(*arrayType.getBaseType(), true, true, false);
|
||||||
break;
|
break;
|
||||||
case ArrayType::Location::Storage:
|
case ArrayType::Location::Storage:
|
||||||
m_context << u256(0); // @todo
|
m_context << eth::Instruction::SWAP1;
|
||||||
|
if (arrayType.getBaseType()->getStorageBytes() <= 16)
|
||||||
|
{
|
||||||
|
// stack: <data_ref> <index>
|
||||||
|
// goal:
|
||||||
|
// <ref> <byte_number> = <base_ref + index / itemsPerSlot> <(index % itemsPerSlot) * byteSize>
|
||||||
|
unsigned byteSize = arrayType.getBaseType()->getStorageBytes();
|
||||||
|
unsigned itemsPerSlot = 32 / byteSize;
|
||||||
|
m_context << u256(itemsPerSlot) << eth::Instruction::SWAP2;
|
||||||
|
// stack: itemsPerSlot index data_ref
|
||||||
|
m_context
|
||||||
|
<< eth::Instruction::DUP3 << eth::Instruction::DUP3
|
||||||
|
<< eth::Instruction::DIV << eth::Instruction::ADD
|
||||||
|
// stack: itemsPerSlot index (data_ref + index / itemsPerSlot)
|
||||||
|
<< eth::Instruction::SWAP2 << eth::Instruction::SWAP1
|
||||||
|
<< eth::Instruction::MOD
|
||||||
|
<< u256(byteSize) << eth::Instruction::MUL;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (arrayType.getBaseType()->getStorageSize() != 1)
|
||||||
|
m_context << arrayType.getBaseType()->getStorageSize() << eth::Instruction::MUL;
|
||||||
|
m_context << eth::Instruction::ADD << u256(0);
|
||||||
|
}
|
||||||
setLValueToStorageItem(_indexAccess);
|
setLValueToStorageItem(_indexAccess);
|
||||||
break;
|
break;
|
||||||
case ArrayType::Location::Memory:
|
case ArrayType::Location::Memory:
|
||||||
|
14
Types.cpp
14
Types.cpp
@ -706,14 +706,22 @@ u256 ArrayType::getStorageSize() const
|
|||||||
{
|
{
|
||||||
if (isDynamicallySized())
|
if (isDynamicallySized())
|
||||||
return 1;
|
return 1;
|
||||||
else
|
|
||||||
|
bigint size;
|
||||||
|
unsigned baseBytes = getBaseType()->getStorageBytes();
|
||||||
|
if (baseBytes == 0)
|
||||||
|
size = 1;
|
||||||
|
else if (baseBytes < 32)
|
||||||
{
|
{
|
||||||
bigint size = bigint(getLength()) * getBaseType()->getStorageSize();
|
unsigned itemsPerSlot = 32 / baseBytes;
|
||||||
|
size = (bigint(getLength()) + (itemsPerSlot - 1)) / itemsPerSlot;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
size = bigint(getLength()) * getBaseType()->getStorageSize();
|
||||||
if (size >= bigint(1) << 256)
|
if (size >= bigint(1) << 256)
|
||||||
BOOST_THROW_EXCEPTION(TypeError() << errinfo_comment("Array too large for storage."));
|
BOOST_THROW_EXCEPTION(TypeError() << errinfo_comment("Array too large for storage."));
|
||||||
return max<u256>(1, u256(size));
|
return max<u256>(1, u256(size));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
unsigned ArrayType::getSizeOnStack() const
|
unsigned ArrayType::getSizeOnStack() const
|
||||||
{
|
{
|
||||||
|
15
Types.h
15
Types.h
@ -335,13 +335,22 @@ public:
|
|||||||
|
|
||||||
/// Constructor for a byte array ("bytes")
|
/// Constructor for a byte array ("bytes")
|
||||||
explicit ArrayType(Location _location):
|
explicit ArrayType(Location _location):
|
||||||
m_location(_location), m_isByteArray(true), m_baseType(std::make_shared<IntegerType>(8)) {}
|
m_location(_location),
|
||||||
|
m_isByteArray(true),
|
||||||
|
m_baseType(std::make_shared<FixedBytesType>(8))
|
||||||
|
{}
|
||||||
/// Constructor for a dynamically sized array type ("type[]")
|
/// Constructor for a dynamically sized array type ("type[]")
|
||||||
ArrayType(Location _location, const TypePointer &_baseType):
|
ArrayType(Location _location, const TypePointer &_baseType):
|
||||||
m_location(_location), m_baseType(_baseType) {}
|
m_location(_location),
|
||||||
|
m_baseType(_baseType)
|
||||||
|
{}
|
||||||
/// Constructor for a fixed-size array type ("type[20]")
|
/// Constructor for a fixed-size array type ("type[20]")
|
||||||
ArrayType(Location _location, const TypePointer &_baseType, u256 const& _length):
|
ArrayType(Location _location, const TypePointer &_baseType, u256 const& _length):
|
||||||
m_location(_location), m_baseType(_baseType), m_hasDynamicLength(false), m_length(_length) {}
|
m_location(_location),
|
||||||
|
m_baseType(_baseType),
|
||||||
|
m_hasDynamicLength(false),
|
||||||
|
m_length(_length)
|
||||||
|
{}
|
||||||
|
|
||||||
virtual bool isImplicitlyConvertibleTo(Type const& _convertTo) const override;
|
virtual bool isImplicitlyConvertibleTo(Type const& _convertTo) const override;
|
||||||
virtual TypePointer unaryOperatorResult(Token::Value _operator) const override;
|
virtual TypePointer unaryOperatorResult(Token::Value _operator) const override;
|
||||||
|
Loading…
Reference in New Issue
Block a user