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
|
/*
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/>.
*/
/**
* Adaptor between the abstract assembly and eth assembly.
*/
#include <libsolidity/codegen/AsmCodeGen.h>
#include <libyul/AsmData.h>
#include <libyul/AsmAnalysisInfo.h>
#include <libyul/backends/evm/AbstractAssembly.h>
#include <libyul/backends/evm/EVMCodeTransform.h>
#include <libevmasm/Assembly.h>
#include <libevmasm/AssemblyItem.h>
#include <libevmasm/Instruction.h>
#include <liblangutil/SourceLocation.h>
#include <libdevcore/FixedHash.h>
#include <memory>
#include <functional>
using namespace std;
using namespace dev;
using namespace langutil;
using namespace yul;
using namespace dev::solidity;
EthAssemblyAdapter::EthAssemblyAdapter(eth::Assembly& _assembly):
m_assembly(_assembly)
{
}
void EthAssemblyAdapter::setSourceLocation(SourceLocation const& _location)
{
m_assembly.setSourceLocation(_location);
}
int EthAssemblyAdapter::stackHeight() const
{
return m_assembly.deposit();
}
void EthAssemblyAdapter::appendInstruction(solidity::Instruction _instruction)
{
m_assembly.append(_instruction);
}
void EthAssemblyAdapter::appendConstant(u256 const& _constant)
{
m_assembly.append(_constant);
}
void EthAssemblyAdapter::appendLabel(LabelID _labelId)
{
m_assembly.append(eth::AssemblyItem(eth::Tag, _labelId));
}
void EthAssemblyAdapter::appendLabelReference(LabelID _labelId)
{
m_assembly.append(eth::AssemblyItem(eth::PushTag, _labelId));
}
size_t EthAssemblyAdapter::newLabelId()
{
return assemblyTagToIdentifier(m_assembly.newTag());
}
size_t EthAssemblyAdapter::namedLabel(std::string const& _name)
{
return assemblyTagToIdentifier(m_assembly.namedTag(_name));
}
void EthAssemblyAdapter::appendLinkerSymbol(std::string const& _linkerSymbol)
{
m_assembly.appendLibraryAddress(_linkerSymbol);
}
void EthAssemblyAdapter::appendJump(int _stackDiffAfter)
{
appendInstruction(solidity::Instruction::JUMP);
m_assembly.adjustDeposit(_stackDiffAfter);
}
void EthAssemblyAdapter::appendJumpTo(LabelID _labelId, int _stackDiffAfter)
{
appendLabelReference(_labelId);
appendJump(_stackDiffAfter);
}
void EthAssemblyAdapter::appendJumpToIf(LabelID _labelId)
{
appendLabelReference(_labelId);
appendInstruction(solidity::Instruction::JUMPI);
}
void EthAssemblyAdapter::appendBeginsub(LabelID, int)
{
// TODO we could emulate that, though
solAssert(false, "BEGINSUB not implemented for EVM 1.0");
}
void EthAssemblyAdapter::appendJumpsub(LabelID, int, int)
{
// TODO we could emulate that, though
solAssert(false, "JUMPSUB not implemented for EVM 1.0");
}
void EthAssemblyAdapter::appendReturnsub(int, int)
{
// TODO we could emulate that, though
solAssert(false, "RETURNSUB not implemented for EVM 1.0");
}
void EthAssemblyAdapter::appendAssemblySize()
{
m_assembly.appendProgramSize();
}
pair<shared_ptr<AbstractAssembly>, AbstractAssembly::SubID> EthAssemblyAdapter::createSubAssembly()
{
shared_ptr<eth::Assembly> assembly{make_shared<eth::Assembly>()};
auto sub = m_assembly.newSub(assembly);
return {make_shared<EthAssemblyAdapter>(*assembly), size_t(sub.data())};
}
void EthAssemblyAdapter::appendDataOffset(AbstractAssembly::SubID _sub)
{
auto it = m_dataHashBySubId.find(_sub);
if (it == m_dataHashBySubId.end())
m_assembly.pushSubroutineOffset(size_t(_sub));
else
m_assembly << eth::AssemblyItem(eth::PushData, it->second);
}
void EthAssemblyAdapter::appendDataSize(AbstractAssembly::SubID _sub)
{
auto it = m_dataHashBySubId.find(_sub);
if (it == m_dataHashBySubId.end())
m_assembly.pushSubroutineSize(size_t(_sub));
else
m_assembly << u256(m_assembly.data(h256(it->second)).size());
}
AbstractAssembly::SubID EthAssemblyAdapter::appendData(bytes const& _data)
{
eth::AssemblyItem pushData = m_assembly.newData(_data);
SubID subID = m_nextDataCounter++;
m_dataHashBySubId[subID] = pushData.data();
return subID;
}
EthAssemblyAdapter::LabelID EthAssemblyAdapter::assemblyTagToIdentifier(eth::AssemblyItem const& _tag)
{
u256 id = _tag.data();
solAssert(id <= std::numeric_limits<LabelID>::max(), "Tag id too large.");
return LabelID(id);
}
void CodeGenerator::assemble(
Block const& _parsedData,
AsmAnalysisInfo& _analysisInfo,
eth::Assembly& _assembly,
ExternalIdentifierAccess const& _identifierAccess,
bool _useNamedLabelsForFunctions,
bool _optimize
)
{
EthAssemblyAdapter assemblyAdapter(_assembly);
CodeTransform(
assemblyAdapter,
_analysisInfo,
_parsedData,
_optimize,
Dialect::strictAssemblyForEVM(),
false,
_identifierAccess,
_useNamedLabelsForFunctions
)(_parsedData);
}
|