aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian <c@ethdev.com>2015-02-11 21:24:34 +0800
committerChristian <c@ethdev.com>2015-02-12 18:33:10 +0800
commit8a2879a603b256dbff9c4592943c48e68fba1aea (patch)
tree9f9beda6bcd8febcac37b5e6b439bb4217b35282
parentadb434569c7f54a12dfbdc674b50a4a4baca59e4 (diff)
downloaddexon-solidity-8a2879a603b256dbff9c4592943c48e68fba1aea.tar
dexon-solidity-8a2879a603b256dbff9c4592943c48e68fba1aea.tar.gz
dexon-solidity-8a2879a603b256dbff9c4592943c48e68fba1aea.tar.bz2
dexon-solidity-8a2879a603b256dbff9c4592943c48e68fba1aea.tar.lz
dexon-solidity-8a2879a603b256dbff9c4592943c48e68fba1aea.tar.xz
dexon-solidity-8a2879a603b256dbff9c4592943c48e68fba1aea.tar.zst
dexon-solidity-8a2879a603b256dbff9c4592943c48e68fba1aea.zip
Copy storage to storage.
-rw-r--r--CompilerUtils.cpp6
-rw-r--r--CompilerUtils.h5
-rw-r--r--ExpressionCompiler.cpp77
3 files changed, 81 insertions, 7 deletions
diff --git a/CompilerUtils.cpp b/CompilerUtils.cpp
index 12940367..c38149c1 100644
--- a/CompilerUtils.cpp
+++ b/CompilerUtils.cpp
@@ -116,6 +116,12 @@ unsigned CompilerUtils::getSizeOnStack(vector<shared_ptr<Type const>> const& _va
return size;
}
+void CompilerUtils::computeHashStatic(Type const& _type, bool _padToWordBoundaries)
+{
+ unsigned length = storeInMemory(0, _type, _padToWordBoundaries);
+ m_context << u256(length) << u256(0) << eth::Instruction::SHA3;
+}
+
unsigned CompilerUtils::prepareMemoryStore(Type const& _type, bool _padToWordBoundaries)
{
unsigned _encodedSize = _type.getCalldataEncodedSize();
diff --git a/CompilerUtils.h b/CompilerUtils.h
index e591e9a7..49375047 100644
--- a/CompilerUtils.h
+++ b/CompilerUtils.h
@@ -70,6 +70,11 @@ public:
static unsigned getSizeOnStack(std::vector<T> const& _variables);
static unsigned getSizeOnStack(std::vector<std::shared_ptr<Type const>> const& _variableTypes);
+ /// Appends code that computes tha SHA3 hash of the topmost stack element of type @a _type.
+ /// If @a _pad is set, padds the type to muliples of 32 bytes.
+ /// @note Only works for types of fixed size.
+ void computeHashStatic(Type const& _type = IntegerType(256), bool _padToWordBoundaries = false);
+
/// Bytes we need to the start of call data.
/// - The size in bytes of the function (hash) identifier.
static const unsigned int dataStartOffset;
diff --git a/ExpressionCompiler.cpp b/ExpressionCompiler.cpp
index 363d1d4b..a05d71e6 100644
--- a/ExpressionCompiler.cpp
+++ b/ExpressionCompiler.cpp
@@ -886,6 +886,8 @@ void ExpressionCompiler::appendArgumentsCopyToMemory(vector<ASTPointer<Expressio
void ExpressionCompiler::appendTypeMoveToMemory(Type const& _type, bool _padToWordBoundaries)
{
+ // @TODO move this to CompilerUtils!
+
if (_type.getCategory() == Type::Category::ByteArray)
{
auto const& type = dynamic_cast<ByteArrayType const&>(_type);
@@ -901,9 +903,8 @@ void ExpressionCompiler::appendTypeMoveToMemory(Type const& _type, bool _padToWo
m_context << eth::Instruction::DUP3 << eth::Instruction::ADD << eth::Instruction::SWAP2;
// actual array data is stored at SHA3(storage_offset)
m_context << eth::Instruction::SWAP1;
- CompilerUtils(m_context).storeInMemory(0);
- m_context << u256(32) << u256(0) << eth::Instruction::SHA3
- << eth::Instruction::SWAP1;
+ CompilerUtils(m_context).computeHashStatic();
+ m_context << eth::Instruction::SWAP1;
// stack here: memory_end_offset storage_data_offset memory_offset
eth::AssemblyItem loopStart = m_context.newTag();
@@ -1204,6 +1205,9 @@ void ExpressionCompiler::LValue::copyByteArrayToStorage(ByteArrayType const& _ta
// need to leave target_ref on the stack at the end
solAssert(m_type == LValueType::Storage, "");
solAssert(_targetType.getLocation() == ByteArrayType::Location::Storage, "");
+
+ // @TODO move this to CompilerUtils!
+
switch (_sourceType.getLocation())
{
case ByteArrayType::Location::CallData:
@@ -1224,8 +1228,7 @@ void ExpressionCompiler::LValue::copyByteArrayToStorage(ByteArrayType const& _ta
m_context->appendConditionalJumpTo(loopEnd);
// actual array data is stored at SHA3(storage_offset)
*m_context << eth::Instruction::DUP3;
- CompilerUtils(*m_context).storeInMemory(0);
- *m_context << u256(32) << u256(0) << eth::Instruction::SHA3;
+ CompilerUtils(*m_context).computeHashStatic();
*m_context << _sourceType.getOffset();
// stack now: target_ref 32 1 target_data_ref calldata_offset
@@ -1246,10 +1249,70 @@ void ExpressionCompiler::LValue::copyByteArrayToStorage(ByteArrayType const& _ta
break;
}
case ByteArrayType::Location::Storage:
- solAssert(false, "Not Yet implemented.");
+ {
+ // this copies source to target and also clears target if it was larger
+
+ // stack: source_ref target_ref
+ // fetch lengthes
+ *m_context << eth::Instruction::DUP1 << eth::Instruction::SLOAD << eth::Instruction::SWAP2
+ << eth::Instruction::DUP1 << eth::Instruction::SLOAD;
+ // stack: target_len_bytes target_ref source_ref source_len_bytes
+ // store new target length
+ *m_context << eth::Instruction::DUP1 << eth::Instruction::DUP4 << eth::Instruction::SSTORE;
+ // compute hashes (data positions)
+ *m_context << eth::Instruction::SWAP2;
+ CompilerUtils(*m_context).computeHashStatic();
+ *m_context << eth::Instruction::SWAP1;
+ CompilerUtils(*m_context).computeHashStatic();
+ // stack: target_len_bytes source_len_bytes target_data_pos source_data_pos
+ // convert lengthes from bytes to storage slots
+ *m_context << u256(31) << u256(32) << eth::Instruction::DUP1 << eth::Instruction::DUP3
+ << eth::Instruction::DUP8 << eth::Instruction::ADD << eth::Instruction::DIV
+ << eth::Instruction::SWAP2
+ << eth::Instruction::DUP6 << eth::Instruction::ADD << eth::Instruction::DIV;
+ // stack: target_len_bytes source_len_bytes target_data_pos source_data_pos target_len source_len
+ // @todo we might be able to go without a third counter
+ *m_context << u256(0);
+ // stack: target_len_bytes source_len_bytes target_data_pos source_data_pos target_len source_len counter
+ eth::AssemblyItem copyLoopStart = m_context->newTag();
+ *m_context << copyLoopStart;
+ // check for loop condition
+ *m_context << eth::Instruction::DUP1 << eth::Instruction::DUP3
+ << eth::Instruction::GT << eth::Instruction::ISZERO;
+ eth::AssemblyItem copyLoopEnd = m_context->newTag();
+ m_context->appendConditionalJumpTo(copyLoopEnd);
+ // copy
+ *m_context << eth::Instruction::DUP4 << eth::Instruction::DUP2 << eth::Instruction::ADD
+ << eth::Instruction::SLOAD
+ << eth::Instruction::DUP6 << eth::Instruction::DUP3 << eth::Instruction::ADD
+ << eth::Instruction::SSTORE;
+ // increment
+ *m_context << u256(1) << eth::Instruction::ADD;
+ m_context->appendJumpTo(copyLoopStart);
+ *m_context << copyLoopEnd;
+ // zero-out leftovers in target
+ eth::AssemblyItem zeroLoopStart = m_context->newTag();
+ *m_context << zeroLoopStart;
+ // check for loop condition
+ *m_context << eth::Instruction::DUP1 << eth::Instruction::DUP4
+ << eth::Instruction::GT << eth::Instruction::ISZERO;
+ eth::AssemblyItem zeroLoopEnd = m_context->newTag();
+ m_context->appendConditionalJumpTo(zeroLoopEnd);
+ // zero out
+ *m_context << u256(0)
+ << eth::Instruction::DUP6 << eth::Instruction::DUP3 << eth::Instruction::ADD
+ << eth::Instruction::SSTORE;
+ // increment
+ *m_context << u256(1) << eth::Instruction::ADD;
+ m_context->appendJumpTo(zeroLoopStart);
+ // cleanup
+ *m_context << zeroLoopEnd;
+ *m_context << eth::Instruction::POP << eth::Instruction::POP << eth::Instruction::POP
+ << eth::Instruction::POP << eth::Instruction::POP << eth::Instruction::POP;
break;
+ }
default:
- solAssert(false, "Byte array location not implemented.");
+ solAssert(false, "Given byte array location not implemented.");
}
}