|
|
@ -33,35 +33,26 @@ namespace solidity |
|
|
|
|
|
|
|
const unsigned int CompilerUtils::dataStartOffset = 4; |
|
|
|
|
|
|
|
unsigned CompilerUtils::loadFromMemory(unsigned _offset, unsigned _bytes, bool _leftAligned, |
|
|
|
bool _fromCalldata, bool _padToWordBoundaries) |
|
|
|
unsigned CompilerUtils::loadFromMemory(unsigned _offset, Type const& _type, |
|
|
|
bool _fromCalldata, bool _padToWordBoundaries) |
|
|
|
{ |
|
|
|
if (_bytes == 0) |
|
|
|
{ |
|
|
|
m_context << u256(0); |
|
|
|
return 0; |
|
|
|
} |
|
|
|
eth::Instruction load = _fromCalldata ? eth::Instruction::CALLDATALOAD : eth::Instruction::MLOAD; |
|
|
|
solAssert(_bytes <= 32, "Memory load of more than 32 bytes requested."); |
|
|
|
if (_bytes == 32 || _padToWordBoundaries) |
|
|
|
{ |
|
|
|
m_context << u256(_offset) << load; |
|
|
|
return 32; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
// load data and add leading or trailing zeros by dividing/multiplying depending on alignment
|
|
|
|
u256 shiftFactor = u256(1) << ((32 - _bytes) * 8); |
|
|
|
m_context << shiftFactor; |
|
|
|
if (_leftAligned) |
|
|
|
m_context << eth::Instruction::DUP1; |
|
|
|
m_context << u256(_offset) << load << eth::Instruction::DIV; |
|
|
|
if (_leftAligned) |
|
|
|
m_context << eth::Instruction::MUL; |
|
|
|
return _bytes; |
|
|
|
} |
|
|
|
solAssert(_type.getCategory() != Type::Category::ByteArray, "Unable to statically load dynamic type."); |
|
|
|
m_context << u256(_offset); |
|
|
|
return loadFromMemoryHelper(_type, _fromCalldata, _padToWordBoundaries); |
|
|
|
} |
|
|
|
|
|
|
|
void CompilerUtils::loadFromMemoryDynamic(Type const& _type, bool _fromCalldata, bool _padToWordBoundaries) |
|
|
|
{ |
|
|
|
solAssert(_type.getCategory() != Type::Category::ByteArray, "Byte arrays not yet implemented."); |
|
|
|
m_context << eth::Instruction::DUP1; |
|
|
|
unsigned numBytes = loadFromMemoryHelper(_type, _fromCalldata, _padToWordBoundaries); |
|
|
|
// update memory counter
|
|
|
|
for (unsigned i = 0; i < _type.getSizeOnStack(); ++i) |
|
|
|
m_context << eth::swapInstruction(1 + i); |
|
|
|
m_context << u256(numBytes) << eth::Instruction::ADD; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
unsigned CompilerUtils::storeInMemory(unsigned _offset, Type const& _type, bool _padToWordBoundaries) |
|
|
|
{ |
|
|
|
solAssert(_type.getCategory() != Type::Category::ByteArray, "Unable to statically store dynamic type."); |
|
|
@ -79,9 +70,12 @@ void CompilerUtils::storeInMemoryDynamic(Type const& _type, bool _padToWordBound |
|
|
|
|
|
|
|
if (type.getLocation() == ByteArrayType::Location::CallData) |
|
|
|
{ |
|
|
|
m_context << eth::Instruction::CALLDATASIZE << u256(0) << eth::Instruction::DUP3 |
|
|
|
<< eth::Instruction::CALLDATACOPY |
|
|
|
<< eth::Instruction::CALLDATASIZE << eth::Instruction::ADD; |
|
|
|
// stack: target source_offset source_len
|
|
|
|
m_context << eth::Instruction::DUP1 << eth::Instruction::DUP3 << eth::Instruction::DUP5 |
|
|
|
// stack: target source_offset source_len source_len source_offset target
|
|
|
|
<< eth::Instruction::CALLDATACOPY |
|
|
|
<< eth::Instruction::DUP3 << eth::Instruction::ADD |
|
|
|
<< eth::Instruction::SWAP2 << eth::Instruction::POP << eth::Instruction::POP; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
@ -120,6 +114,7 @@ void CompilerUtils::storeInMemoryDynamic(Type const& _type, bool _padToWordBound |
|
|
|
unsigned numBytes = prepareMemoryStore(_type, _padToWordBoundaries); |
|
|
|
if (numBytes > 0) |
|
|
|
{ |
|
|
|
solAssert(_type.getSizeOnStack() == 1, "Memory store of types with stack size != 1 not implemented."); |
|
|
|
m_context << eth::Instruction::DUP2 << eth::Instruction::MSTORE; |
|
|
|
m_context << u256(numBytes) << eth::Instruction::ADD; |
|
|
|
} |
|
|
@ -179,29 +174,32 @@ void CompilerUtils::copyByteArrayToStorage(ByteArrayType const& _targetType, |
|
|
|
{ |
|
|
|
case ByteArrayType::Location::CallData: |
|
|
|
{ |
|
|
|
// @todo this does not take length into account. It also assumes that after "CALLDATALENGTH" we only have zeros.
|
|
|
|
// This also assumes that after "length" we only have zeros, i.e. it cannot be used to
|
|
|
|
// slice a byte array from calldata.
|
|
|
|
|
|
|
|
// stack: source_offset source_len target_ref
|
|
|
|
// fetch old length and convert to words
|
|
|
|
m_context << eth::Instruction::DUP1 << eth::Instruction::SLOAD; |
|
|
|
m_context << u256(31) << eth::Instruction::ADD |
|
|
|
<< u256(32) << eth::Instruction::SWAP1 << eth::Instruction::DIV; |
|
|
|
// stack here: target_ref target_length_words
|
|
|
|
// stack here: source_offset source_len target_ref target_length_words
|
|
|
|
// actual array data is stored at SHA3(storage_offset)
|
|
|
|
m_context << eth::Instruction::DUP2; |
|
|
|
CompilerUtils(m_context).computeHashStatic(); |
|
|
|
// compute target_data_end
|
|
|
|
m_context << eth::Instruction::DUP1 << eth::Instruction::SWAP2 << eth::Instruction::ADD |
|
|
|
<< eth::Instruction::SWAP1; |
|
|
|
// stack here: target_ref target_data_end target_data_ref
|
|
|
|
// stack here: source_offset source_len target_ref target_data_end target_data_ref
|
|
|
|
// store length (in bytes)
|
|
|
|
m_context << eth::Instruction::CALLDATASIZE; |
|
|
|
m_context << eth::Instruction::DUP1 << eth::Instruction::DUP5 << eth::Instruction::SSTORE; |
|
|
|
m_context << eth::Instruction::DUP4 << eth::Instruction::DUP1 << eth::Instruction::DUP5 |
|
|
|
<< eth::Instruction::SSTORE; |
|
|
|
// jump to end if length is zero
|
|
|
|
m_context << eth::Instruction::ISZERO; |
|
|
|
eth::AssemblyItem copyLoopEnd = m_context.newTag(); |
|
|
|
m_context.appendConditionalJumpTo(copyLoopEnd); |
|
|
|
// store start offset
|
|
|
|
m_context << u256(0); |
|
|
|
// stack now: target_ref target_data_end target_data_ref calldata_offset
|
|
|
|
m_context << eth::Instruction::DUP5; |
|
|
|
// stack now: source_offset source_len target_ref target_data_end target_data_ref calldata_offset
|
|
|
|
eth::AssemblyItem copyLoopStart = m_context.newTag(); |
|
|
|
m_context << copyLoopStart |
|
|
|
// copy from calldata and store
|
|
|
@ -212,16 +210,18 @@ void CompilerUtils::copyByteArrayToStorage(ByteArrayType const& _targetType, |
|
|
|
// increment calldata_offset by 32
|
|
|
|
<< eth::Instruction::SWAP1 << u256(32) << eth::Instruction::ADD |
|
|
|
// check for loop condition
|
|
|
|
<< eth::Instruction::DUP1 << eth::Instruction::CALLDATASIZE << eth::Instruction::GT; |
|
|
|
<< eth::Instruction::DUP1 << eth::Instruction::DUP6 << eth::Instruction::GT; |
|
|
|
m_context.appendConditionalJumpTo(copyLoopStart); |
|
|
|
m_context << eth::Instruction::POP; |
|
|
|
m_context << copyLoopEnd; |
|
|
|
|
|
|
|
// now clear leftover bytes of the old value
|
|
|
|
// stack now: target_ref target_data_end target_data_ref
|
|
|
|
// stack now: source_offset source_len target_ref target_data_end target_data_ref
|
|
|
|
clearStorageLoop(); |
|
|
|
// stack now: source_offset source_len target_ref target_data_end
|
|
|
|
|
|
|
|
m_context << eth::Instruction::POP; |
|
|
|
m_context << eth::Instruction::POP << eth::Instruction::SWAP2 |
|
|
|
<< eth::Instruction::POP << eth::Instruction::POP; |
|
|
|
break; |
|
|
|
} |
|
|
|
case ByteArrayType::Location::Storage: |
|
|
@ -289,6 +289,30 @@ void CompilerUtils::copyByteArrayToStorage(ByteArrayType const& _targetType, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
unsigned CompilerUtils::loadFromMemoryHelper(Type const& _type, bool _fromCalldata, bool _padToWordBoundaries) |
|
|
|
{ |
|
|
|
unsigned _encodedSize = _type.getCalldataEncodedSize(); |
|
|
|
unsigned numBytes = _padToWordBoundaries ? getPaddedSize(_encodedSize) : _encodedSize; |
|
|
|
bool leftAligned = _type.getCategory() == Type::Category::String; |
|
|
|
if (numBytes == 0) |
|
|
|
m_context << eth::Instruction::POP << u256(0); |
|
|
|
else |
|
|
|
{ |
|
|
|
solAssert(numBytes <= 32, "Static memory load of more than 32 bytes requested."); |
|
|
|
m_context << (_fromCalldata ? eth::Instruction::CALLDATALOAD : eth::Instruction::MLOAD); |
|
|
|
if (numBytes != 32) |
|
|
|
{ |
|
|
|
// add leading or trailing zeros by dividing/multiplying depending on alignment
|
|
|
|
u256 shiftFactor = u256(1) << ((32 - numBytes) * 8); |
|
|
|
m_context << shiftFactor << eth::Instruction::SWAP1 << eth::Instruction::DIV; |
|
|
|
if (leftAligned) |
|
|
|
m_context << shiftFactor << eth::Instruction::MUL; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return numBytes; |
|
|
|
} |
|
|
|
|
|
|
|
void CompilerUtils::clearByteArray(ByteArrayType const& _type) const |
|
|
|
{ |
|
|
|
solAssert(_type.getLocation() == ByteArrayType::Location::Storage, ""); |
|
|
|