Merge pull request #5256 from ethereum/lvalueCleanup

Lvalue cleanup
This commit is contained in:
chriseth 2018-10-19 11:20:12 +02:00 committed by GitHub
commit c13b5280c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 42 deletions

View File

@ -134,8 +134,7 @@ void MemoryItem::storeValue(Type const& _sourceType, SourceLocation const&, bool
void MemoryItem::setToZero(SourceLocation const&, bool _removeReference) const void MemoryItem::setToZero(SourceLocation const&, bool _removeReference) const
{ {
CompilerUtils utils(m_context); CompilerUtils utils(m_context);
if (!_removeReference) solAssert(_removeReference, "");
m_context << Instruction::DUP1;
utils.pushZeroValue(*m_dataType); utils.pushZeroValue(*m_dataType);
utils.storeInMemoryDynamic(*m_dataType, m_padded); utils.storeInMemoryDynamic(*m_dataType, m_padded);
m_context << Instruction::POP; m_context << Instruction::POP;
@ -460,8 +459,7 @@ void StorageByteArrayElement::storeValue(Type const&, SourceLocation const&, boo
void StorageByteArrayElement::setToZero(SourceLocation const&, bool _removeReference) const void StorageByteArrayElement::setToZero(SourceLocation const&, bool _removeReference) const
{ {
// stack: ref byte_number // stack: ref byte_number
if (!_removeReference) solAssert(_removeReference, "");
m_context << Instruction::DUP2 << Instruction::DUP2;
m_context << u256(31) << Instruction::SUB << u256(0x100) << Instruction::EXP; m_context << u256(31) << Instruction::SUB << u256(0x100) << Instruction::EXP;
// stack: ref (1<<(8*(31-byte_number))) // stack: ref (1<<(8*(31-byte_number)))
m_context << Instruction::DUP2 << Instruction::SLOAD; m_context << Instruction::DUP2 << Instruction::SLOAD;
@ -498,8 +496,7 @@ void StorageArrayLength::storeValue(Type const&, SourceLocation const&, bool _mo
void StorageArrayLength::setToZero(SourceLocation const&, bool _removeReference) const void StorageArrayLength::setToZero(SourceLocation const&, bool _removeReference) const
{ {
if (!_removeReference) solAssert(_removeReference, "");
m_context << Instruction::DUP1;
ArrayUtils(m_context).clearDynamicArray(m_arrayType); ArrayUtils(m_context).clearDynamicArray(m_arrayType);
} }
@ -521,24 +518,9 @@ unsigned TupleObject::sizeOnStack() const
return size; return size;
} }
void TupleObject::retrieveValue(SourceLocation const& _location, bool _remove) const void TupleObject::retrieveValue(SourceLocation const&, bool) const
{ {
unsigned initialDepth = sizeOnStack(); solAssert(false, "Tried to retrieve value of tuple.");
unsigned initialStack = m_context.stackHeight();
for (auto const& lv: m_lvalues)
if (lv)
{
solAssert(initialDepth + m_context.stackHeight() >= initialStack, "");
unsigned depth = initialDepth + m_context.stackHeight() - initialStack;
if (lv->sizeOnStack() > 0)
{
if (_remove && depth > lv->sizeOnStack())
CompilerUtils(m_context).moveToStackTop(depth, depth - lv->sizeOnStack());
else if (!_remove && depth > 0)
CompilerUtils(m_context).copyToStackTop(depth, lv->sizeOnStack());
}
lv->retrieveValue(_location, true);
}
} }
void TupleObject::storeValue(Type const& _sourceType, SourceLocation const& _location, bool) const void TupleObject::storeValue(Type const& _sourceType, SourceLocation const& _location, bool) const
@ -569,24 +551,7 @@ void TupleObject::storeValue(Type const& _sourceType, SourceLocation const& _loc
CompilerUtils(m_context).popStackElement(_sourceType); CompilerUtils(m_context).popStackElement(_sourceType);
} }
void TupleObject::setToZero(SourceLocation const& _location, bool _removeReference) const void TupleObject::setToZero(SourceLocation const&, bool) const
{ {
if (_removeReference) solAssert(false, "Tried to delete tuple.");
{
for (size_t i = 0; i < m_lvalues.size(); ++i)
if (m_lvalues[m_lvalues.size() - i])
m_lvalues[m_lvalues.size() - i]->setToZero(_location, true);
}
else
{
unsigned depth = sizeOnStack();
for (auto const& val: m_lvalues)
if (val)
{
if (val->sizeOnStack() > 0)
CompilerUtils(m_context).copyToStackTop(depth, val->sizeOnStack());
val->setToZero(_location, false);
depth -= val->sizeOnStack();
}
}
} }

View File

@ -1787,6 +1787,24 @@ BOOST_AUTO_TEST_CASE(deleteLocals)
ABI_CHECK(callContractFunction("delLocal()"), encodeArgs(6, 7)); ABI_CHECK(callContractFunction("delLocal()"), encodeArgs(6, 7));
} }
BOOST_AUTO_TEST_CASE(deleteLength)
{
char const* sourceCode = R"(
contract test {
uint[] x;
function f() public returns (uint){
x.length = 1;
x[0] = 1;
delete x.length;
return x.length;
}
}
)";
compileAndRun(sourceCode);
ABI_CHECK(callContractFunction("f()"), encodeArgs(0));
BOOST_CHECK(storageEmpty(m_contractAddress));
}
BOOST_AUTO_TEST_CASE(constructor) BOOST_AUTO_TEST_CASE(constructor)
{ {
char const* sourceCode = R"( char const* sourceCode = R"(