/src/solidity/libyul/backends/evm/ssa/spill/Emitter.h
Line | Count | Source |
1 | | /* |
2 | | This file is part of solidity. |
3 | | |
4 | | solidity is free software: you can redistribute it and/or modify |
5 | | it under the terms of the GNU General Public License as published by |
6 | | the Free Software Foundation, either version 3 of the License, or |
7 | | (at your option) any later version. |
8 | | |
9 | | solidity is distributed in the hope that it will be useful, |
10 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | | GNU General Public License for more details. |
13 | | |
14 | | You should have received a copy of the GNU General Public License |
15 | | along with solidity. If not, see <http://www.gnu.org/licenses/>. |
16 | | */ |
17 | | // SPDX-License-Identifier: GPL-3.0 |
18 | | |
19 | | #pragma once |
20 | | |
21 | | #include <libyul/backends/evm/AbstractAssembly.h> |
22 | | #include <libyul/backends/evm/ssa/SSACFGTypes.h> |
23 | | #include <libyul/backends/evm/ssa/spill/MemoryAddressing.h> |
24 | | |
25 | | #include <libevmasm/Instruction.h> |
26 | | |
27 | | namespace solidity::yul::ssa::spill |
28 | | { |
29 | | |
30 | | /// Emits the two memory accesses that move a spilled variable between the stack and its reserved memory slot. |
31 | | class Emitter |
32 | | { |
33 | | public: |
34 | | Emitter( |
35 | | MemoryAddressing const& _addressing, |
36 | | FunctionGraphID const _cfgIdx, |
37 | | AbstractAssembly& _assembly |
38 | | ): |
39 | 0 | m_addressing(&_addressing), |
40 | 0 | m_cfgIdx(_cfgIdx), |
41 | 0 | m_assembly(&_assembly) |
42 | 0 | { |
43 | 0 | } |
44 | | |
45 | | [[nodiscard]] bool hasAddress(SpillKey const _key) const |
46 | 0 | { |
47 | 0 | return m_addressing->hasAddress(m_cfgIdx, _key); |
48 | 0 | } |
49 | | |
50 | | /// Materializes the spilled variable `_key` on the stack top: `PUSH addr; MLOAD`. Dual of emitStore. |
51 | | void emitLoad(SpillKey const _key) const |
52 | 0 | { |
53 | 0 | m_assembly->appendConstant(m_addressing->addressOf(m_cfgIdx, _key)); |
54 | 0 | m_assembly->appendInstruction(evmasm::Instruction::MLOAD); |
55 | 0 | } |
56 | | |
57 | | /// Writes the stack-top value into the spill slot of `_key`, consuming it: `PUSH addr; MSTORE`. |
58 | | /// Dual of emitLoad. The value must already be on the stack top. |
59 | | void emitStore(SpillKey const _key) const |
60 | 0 | { |
61 | 0 | m_assembly->appendConstant(m_addressing->addressOf(m_cfgIdx, _key)); |
62 | 0 | m_assembly->appendInstruction(evmasm::Instruction::MSTORE); |
63 | 0 | } |
64 | | |
65 | | private: |
66 | | MemoryAddressing const* m_addressing; |
67 | | FunctionGraphID m_cfgIdx; |
68 | | AbstractAssembly* m_assembly; |
69 | | }; |
70 | | |
71 | | } |