diff options
author | benjaminion <ben@edginet.org> | 2017-07-12 04:22:38 +0800 |
---|---|---|
committer | benjaminion <ben@edginet.org> | 2017-07-12 04:22:38 +0800 |
commit | 3bc935d932da3d7e8ff21bb3057276338c4ad497 (patch) | |
tree | 36bd8668c7908189707690942a3e1806a3c0c113 /liblll/CodeFragment.cpp | |
parent | 0b17ff1bdde6a56d3c9b48e8c40da7ad4e9a43f5 (diff) | |
download | dexon-solidity-3bc935d932da3d7e8ff21bb3057276338c4ad497.tar.gz dexon-solidity-3bc935d932da3d7e8ff21bb3057276338c4ad497.tar.zst dexon-solidity-3bc935d932da3d7e8ff21bb3057276338c4ad497.zip |
LLL: rewrite alloc to avoid issues with edge cases.
Diffstat (limited to 'liblll/CodeFragment.cpp')
-rw-r--r-- | liblll/CodeFragment.cpp | 28 |
1 files changed, 22 insertions, 6 deletions
diff --git a/liblll/CodeFragment.cpp b/liblll/CodeFragment.cpp index 56c1e26a..f637dfb1 100644 --- a/liblll/CodeFragment.cpp +++ b/liblll/CodeFragment.cpp @@ -523,14 +523,30 @@ void CodeFragment::constructOperation(sp::utree const& _t, CompilerState& _s) requireSize(1); requireDeposit(0, 1); - m_asm.append(Instruction::MSIZE); - m_asm.append(u256(0)); + // (alloc N): + // - Evaluates to (msize) before the allocation - the start of the allocated memory + // - Does not allocate memory when N is zero + // - Size of memory allocated is N bytes rounded up to a multiple of 32 + // - Uses MLOAD to expand MSIZE to avoid modifying memory. + + auto end = m_asm.newTag(); + m_asm.append(Instruction::MSIZE); // Result will be original top of memory + m_asm.append(code[0].m_asm, 1); // The alloc argument N + m_asm.append(Instruction::DUP1); + m_asm.append(Instruction::ISZERO);// (alloc 0) does not change MSIZE + m_asm.appendJumpI(end); m_asm.append(u256(1)); - m_asm.append(code[0].m_asm, 1); - m_asm.append(Instruction::MSIZE); + m_asm.append(Instruction::DUP2); // Copy N + m_asm.append(Instruction::SUB); // N-1 + m_asm.append(u256(0x1f)); // Bit mask + m_asm.append(Instruction::NOT); // Invert + m_asm.append(Instruction::AND); // Align N-1 on 32 byte boundary + m_asm.append(Instruction::MSIZE); // MSIZE is cheap m_asm.append(Instruction::ADD); - m_asm.append(Instruction::SUB); - m_asm.append(Instruction::MSTORE8); + m_asm.append(Instruction::MLOAD); // Updates MSIZE + m_asm.append(Instruction::POP); // Discard the result of the MLOAD + m_asm.append(end); + m_asm.append(Instruction::POP); // Discard duplicate N _s.usedAlloc = true; } |