aboutsummaryrefslogtreecommitdiffstats
path: root/libyul/backends/evm/EVMAssembly.cpp
blob: 99506317a15803b9ecdd5d0b6f6e68ec47c24da5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
    This file is part of solidity.

    solidity is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    solidity is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with solidity.  If not, see <http://www.gnu.org/licenses/>.
*/
/**
 * Assembly interface for EVM and EVM1.5.
 */

#include <libyul/backends/evm/EVMAssembly.h>

#include <libevmasm/Instruction.h>

#include <liblangutil/Exceptions.h>

using namespace std;
using namespace dev;
using namespace langutil;
using namespace yul;

namespace
{
/// Size of labels in bytes. Four-byte labels are required by some EVM1.5 instructions.
size_t constexpr labelReferenceSize = 4;

size_t constexpr assemblySizeReferenceSize = 4;
}


void EVMAssembly::setSourceLocation(SourceLocation const&)
{
    // Ignored for now;
}

void EVMAssembly::appendInstruction(solidity::Instruction _instr)
{
    m_bytecode.push_back(uint8_t(_instr));
    m_stackHeight += solidity::instructionInfo(_instr).ret - solidity::instructionInfo(_instr).args;
}

void EVMAssembly::appendConstant(u256 const& _constant)
{
    bytes data = toCompactBigEndian(_constant, 1);
    appendInstruction(solidity::pushInstruction(data.size()));
    m_bytecode += data;
}

void EVMAssembly::appendLabel(LabelID _labelId)
{
    setLabelToCurrentPosition(_labelId);
    appendInstruction(solidity::Instruction::JUMPDEST);
}

void EVMAssembly::appendLabelReference(LabelID _labelId)
{
    solAssert(!m_evm15, "Cannot use plain label references in EMV1.5 mode.");
    // @TODO we now always use labelReferenceSize for all labels, it could be shortened
    // for some of them.
    appendInstruction(solidity::pushInstruction(labelReferenceSize));
    m_labelReferences[m_bytecode.size()] = _labelId;
    m_bytecode += bytes(labelReferenceSize);
}

EVMAssembly::LabelID EVMAssembly::newLabelId()
{
    m_labelPositions[m_nextLabelId] = size_t(-1);
    return m_nextLabelId++;
}

AbstractAssembly::LabelID EVMAssembly::namedLabel(string const& _name)
{
    solAssert(!_name.empty(), "");
    if (!m_namedLabels.count(_name))
        m_namedLabels[_name] = newLabelId();
    return m_namedLabels[_name];
}

void EVMAssembly::appendLinkerSymbol(string const&)
{
    solAssert(false, "Linker symbols not yet implemented.");
}

void EVMAssembly::appendJump(int _stackDiffAfter)
{
    solAssert(!m_evm15, "Plain JUMP used for EVM 1.5");
    appendInstruction(solidity::Instruction::JUMP);
    m_stackHeight += _stackDiffAfter;
}

void EVMAssembly::appendJumpTo(LabelID _labelId, int _stackDiffAfter)
{
    if (m_evm15)
    {
        m_bytecode.push_back(uint8_t(solidity::Instruction::JUMPTO));
        appendLabelReferenceInternal(_labelId);
        m_stackHeight += _stackDiffAfter;
    }
    else
    {
        appendLabelReference(_labelId);
        appendJump(_stackDiffAfter);
    }
}

void EVMAssembly::appendJumpToIf(LabelID _labelId)
{
    if (m_evm15)
    {
        m_bytecode.push_back(uint8_t(solidity::Instruction::JUMPIF));
        appendLabelReferenceInternal(_labelId);
        m_stackHeight--;
    }
    else
    {
        appendLabelReference(_labelId);
        appendInstruction(solidity::Instruction::JUMPI);
    }
}

void EVMAssembly::appendBeginsub(LabelID _labelId, int _arguments)
{
    solAssert(m_evm15, "BEGINSUB used for EVM 1.0");
    solAssert(_arguments >= 0, "");
    setLabelToCurrentPosition(_labelId);
    m_bytecode.push_back(uint8_t(solidity::Instruction::BEGINSUB));
    m_stackHeight += _arguments;
}

void EVMAssembly::appendJumpsub(LabelID _labelId, int _arguments, int _returns)
{
    solAssert(m_evm15, "JUMPSUB used for EVM 1.0");
    solAssert(_arguments >= 0 && _returns >= 0, "");
    m_bytecode.push_back(uint8_t(solidity::Instruction::JUMPSUB));
    appendLabelReferenceInternal(_labelId);
    m_stackHeight += _returns - _arguments;
}

void EVMAssembly::appendReturnsub(int _returns, int _stackDiffAfter)
{
    solAssert(m_evm15, "RETURNSUB used for EVM 1.0");
    solAssert(_returns >= 0, "");
    m_bytecode.push_back(uint8_t(solidity::Instruction::RETURNSUB));
    m_stackHeight += _stackDiffAfter - _returns;
}

eth::LinkerObject EVMAssembly::finalize()
{
    size_t bytecodeSize = m_bytecode.size();
    for (auto const& ref: m_assemblySizePositions)
        updateReference(ref, assemblySizeReferenceSize, u256(bytecodeSize));

    for (auto const& ref: m_labelReferences)
    {
        size_t referencePos = ref.first;
        solAssert(m_labelPositions.count(ref.second), "");
        size_t labelPos = m_labelPositions.at(ref.second);
        solAssert(labelPos != size_t(-1), "Undefined but allocated label used.");
        updateReference(referencePos, labelReferenceSize, u256(labelPos));
    }

    eth::LinkerObject obj;
    obj.bytecode = m_bytecode;
    return obj;
}

void EVMAssembly::setLabelToCurrentPosition(LabelID _labelId)
{
    solAssert(m_labelPositions.count(_labelId), "Label not found.");
    solAssert(m_labelPositions[_labelId] == size_t(-1), "Label already set.");
    m_labelPositions[_labelId] = m_bytecode.size();
}

void EVMAssembly::appendLabelReferenceInternal(LabelID _labelId)
{
    m_labelReferences[m_bytecode.size()] = _labelId;
    m_bytecode += bytes(labelReferenceSize);
}

void EVMAssembly::appendAssemblySize()
{
    appendInstruction(solidity::pushInstruction(assemblySizeReferenceSize));
    m_assemblySizePositions.push_back(m_bytecode.size());
    m_bytecode += bytes(assemblySizeReferenceSize);
}

void EVMAssembly::updateReference(size_t pos, size_t size, u256 value)
{
    solAssert(m_bytecode.size() >= size && pos <= m_bytecode.size() - size, "");
    solAssert(value < (u256(1) << (8 * size)), "");
    for (size_t i = 0; i < size; i++)
        m_bytecode[pos + i] = uint8_t((value >> (8 * (size - i - 1))) & 0xff);
}