diff options
author | Christian <c@ethdev.com> | 2014-12-11 00:15:17 +0800 |
---|---|---|
committer | Christian <c@ethdev.com> | 2014-12-11 00:15:17 +0800 |
commit | 373f0da2675f298984f265672d7414a58c061512 (patch) | |
tree | 72cf9741f01020410d10d07103d82714a4383bd0 /CompilerUtils.cpp | |
parent | 130ff85e85de3ea8a9666f84843428a3a09b1aab (diff) | |
download | dexon-solidity-373f0da2675f298984f265672d7414a58c061512.tar.gz dexon-solidity-373f0da2675f298984f265672d7414a58c061512.tar.zst dexon-solidity-373f0da2675f298984f265672d7414a58c061512.zip |
Helper functions to access memory.
Diffstat (limited to 'CompilerUtils.cpp')
-rw-r--r-- | CompilerUtils.cpp | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/CompilerUtils.cpp b/CompilerUtils.cpp index d4dfbe3c..0ee4a53c 100644 --- a/CompilerUtils.cpp +++ b/CompilerUtils.cpp @@ -31,6 +31,36 @@ namespace dev namespace solidity { +void CompilerUtils::loadFromMemory(unsigned _offset, unsigned _bytes, bool _leftAligned, bool _fromCalldata) +{ + eth::Instruction load = _fromCalldata ? eth::Instruction::CALLDATALOAD : eth::Instruction::MLOAD; + if (asserts(0 < _bytes && _bytes <= 32)) + BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Memory load of 0 or more than 32 bytes requested.")); + if (_bytes == 32) + m_context << u256(_offset) << load; + 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; + } +} + +void CompilerUtils::storeInMemory(unsigned _offset, unsigned _bytes, bool _leftAligned) +{ + if (asserts(0 < _bytes && _bytes <= 32)) + BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Memory store of 0 or more than 32 bytes requested.")); + if (_bytes != 32 && !_leftAligned) + // shift the value accordingly before storing + m_context << (u256(1) << ((32 - _bytes) * 8)) << eth::Instruction::MUL; + m_context << u256(_offset) << eth::Instruction::MSTORE; +} + void CompilerUtils::moveToStackVariable(VariableDeclaration const& _variable) { unsigned const stackPosition = m_context.baseToCurrentStackOffset(m_context.getBaseStackOffsetOfVariable(_variable)); |