Coverage Report

Created: 2026-09-14 06:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/solidity/libyul/backends/evm/EthAssemblyAdapter.cpp
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
 * Adaptor between AbstractAssembly and libevmasm.
20
 */
21
22
#include <libyul/backends/evm/EthAssemblyAdapter.h>
23
24
#include <libyul/backends/evm/AbstractAssembly.h>
25
#include <libyul/Exceptions.h>
26
27
#include <libevmasm/Assembly.h>
28
#include <libevmasm/AssemblyItem.h>
29
#include <libevmasm/Instruction.h>
30
31
#include <liblangutil/SourceLocation.h>
32
33
#include <memory>
34
#include <functional>
35
36
using namespace solidity;
37
using namespace solidity::yul;
38
using namespace solidity::util;
39
using namespace solidity::langutil;
40
41
EthAssemblyAdapter::EthAssemblyAdapter(evmasm::Assembly& _assembly):
42
107k
  m_assembly(_assembly)
43
107k
{
44
107k
}
45
46
void EthAssemblyAdapter::setSourceLocation(SourceLocation const& _location)
47
15.9M
{
48
15.9M
  m_assembly.setSourceLocation(_location);
49
15.9M
}
50
51
int EthAssemblyAdapter::stackHeight() const
52
25.8M
{
53
25.8M
  return m_assembly.deposit();
54
25.8M
}
55
56
void EthAssemblyAdapter::setStackHeight(int height)
57
1.53M
{
58
1.53M
  m_assembly.setDeposit(height);
59
1.53M
}
60
61
void EthAssemblyAdapter::appendInstruction(evmasm::Instruction _instruction)
62
5.95M
{
63
5.95M
  m_assembly.append(_instruction);
64
5.95M
}
65
66
void EthAssemblyAdapter::appendConstant(u256 const& _constant)
67
2.55M
{
68
2.55M
  m_assembly.append(_constant);
69
2.55M
}
70
71
void EthAssemblyAdapter::appendLabel(LabelID _labelId)
72
1.65M
{
73
1.65M
  m_assembly.append(evmasm::AssemblyItem(evmasm::Tag, _labelId));
74
1.65M
}
75
76
void EthAssemblyAdapter::appendLabelReference(LabelID _labelId)
77
1.58M
{
78
1.58M
  m_assembly.append(evmasm::AssemblyItem(evmasm::PushTag, _labelId));
79
1.58M
}
80
81
size_t EthAssemblyAdapter::newLabelId()
82
1.31M
{
83
1.31M
  return assemblyTagToIdentifier(m_assembly.newTag());
84
1.31M
}
85
86
size_t EthAssemblyAdapter::namedLabel(std::string const& _name, size_t _params, size_t _returns, std::optional<size_t> _sourceID)
87
337k
{
88
337k
  return assemblyTagToIdentifier(m_assembly.namedTag(_name, _params, _returns, _sourceID));
89
337k
}
90
91
void EthAssemblyAdapter::appendLinkerSymbol(std::string const& _linkerSymbol)
92
1.40k
{
93
1.40k
  m_assembly.appendLibraryAddress(_linkerSymbol);
94
1.40k
}
95
96
void EthAssemblyAdapter::appendVerbatim(bytes _data, size_t _arguments, size_t _returnVariables)
97
1.20k
{
98
1.20k
  m_assembly.appendVerbatim(std::move(_data), _arguments, _returnVariables);
99
1.20k
}
100
101
void EthAssemblyAdapter::appendJump(int _stackDiffAfter, JumpType _jumpType)
102
1.07M
{
103
1.07M
  appendJumpInstruction(evmasm::Instruction::JUMP, _jumpType);
104
1.07M
  m_assembly.adjustDeposit(_stackDiffAfter);
105
1.07M
}
106
107
void EthAssemblyAdapter::appendJumpTo(LabelID _labelId, int _stackDiffAfter, JumpType _jumpType)
108
745k
{
109
745k
  appendLabelReference(_labelId);
110
745k
  appendJump(_stackDiffAfter, _jumpType);
111
745k
}
112
113
void EthAssemblyAdapter::appendJumpToIf(LabelID _labelId, JumpType _jumpType)
114
302k
{
115
302k
  appendLabelReference(_labelId);
116
302k
  appendJumpInstruction(evmasm::Instruction::JUMPI, _jumpType);
117
302k
}
118
119
void EthAssemblyAdapter::appendAssemblySize()
120
317
{
121
317
  m_assembly.appendProgramSize();
122
317
}
123
124
std::pair<std::shared_ptr<AbstractAssembly>, AbstractAssembly::SubID> EthAssemblyAdapter::createSubAssembly(bool _creation, std::string _name)
125
8.03k
{
126
8.03k
  std::shared_ptr<evmasm::Assembly> assembly{std::make_shared<evmasm::Assembly>(m_assembly.evmVersion(), _creation, std::move(_name))};
127
8.03k
  auto sub = m_assembly.newSub(assembly);
128
8.03k
  return {std::make_shared<EthAssemblyAdapter>(*assembly), SubID{sub.data()}};
129
8.03k
}
130
131
void EthAssemblyAdapter::appendDataOffset(std::vector<AbstractAssembly::SubID> const& _subPath)
132
6.38k
{
133
6.38k
  if (auto it = m_dataHashBySubId.find(_subPath[0]); it != m_dataHashBySubId.end())
134
883
  {
135
883
    yulAssert(_subPath.size() == 1, "");
136
883
    m_assembly << evmasm::AssemblyItem(evmasm::PushData, it->second);
137
883
    return;
138
883
  }
139
140
5.50k
  m_assembly.pushSubroutineOffset(m_assembly.encodeSubPath(_subPath));
141
5.50k
}
142
143
void EthAssemblyAdapter::appendDataSize(std::vector<AbstractAssembly::SubID> const& _subPath)
144
8.21k
{
145
8.21k
  if (auto it = m_dataHashBySubId.find(_subPath[0]); it != m_dataHashBySubId.end())
146
331
  {
147
331
    yulAssert(_subPath.size() == 1, "");
148
331
    m_assembly << u256(m_assembly.data(h256(it->second)).size());
149
331
    return;
150
331
  }
151
152
7.87k
  m_assembly.pushSubroutineSize(m_assembly.encodeSubPath(_subPath));
153
7.87k
}
154
155
AbstractAssembly::SubID EthAssemblyAdapter::appendData(bytes const& _data)
156
3.90k
{
157
3.90k
  evmasm::AssemblyItem pushData = m_assembly.newData(_data);
158
3.90k
  SubID const subID{m_nextDataCounter++};
159
3.90k
  m_dataHashBySubId[subID] = pushData.data();
160
3.90k
  return subID;
161
3.90k
}
162
163
void EthAssemblyAdapter::appendToAuxiliaryData(bytes const& _data)
164
4.50k
{
165
4.50k
  m_assembly.appendToAuxiliaryData(_data);
166
4.50k
}
167
168
void EthAssemblyAdapter::appendImmutable(std::string const& _identifier)
169
151
{
170
151
  m_assembly.appendImmutable(_identifier);
171
151
}
172
173
void EthAssemblyAdapter::appendImmutableAssignment(std::string const& _identifier)
174
191
{
175
191
  m_assembly.appendImmutableAssignment(_identifier);
176
191
}
177
178
void EthAssemblyAdapter::markAsInvalid()
179
21.6k
{
180
21.6k
  m_assembly.markAsInvalid();
181
21.6k
}
182
183
langutil::EVMVersion EthAssemblyAdapter::evmVersion() const
184
2.55k
{
185
2.55k
  return m_assembly.evmVersion();
186
2.55k
}
187
188
EthAssemblyAdapter::LabelID EthAssemblyAdapter::assemblyTagToIdentifier(evmasm::AssemblyItem const& _tag)
189
1.65M
{
190
1.65M
  u256 id = _tag.data();
191
1.65M
  yulAssert(id <= std::numeric_limits<LabelID>::max(), "Tag id too large.");
192
1.65M
  return LabelID(id);
193
1.65M
}
194
195
void EthAssemblyAdapter::appendJumpInstruction(evmasm::Instruction _instruction, JumpType _jumpType)
196
1.37M
{
197
1.37M
  yulAssert(_instruction == evmasm::Instruction::JUMP || _instruction == evmasm::Instruction::JUMPI, "");
198
1.37M
  evmasm::AssemblyItem jump(_instruction);
199
1.37M
  switch (_jumpType)
200
1.37M
  {
201
491k
  case JumpType::Ordinary:
202
491k
    yulAssert(jump.getJumpType() == evmasm::AssemblyItem::JumpType::Ordinary, "");
203
491k
    break;
204
556k
  case JumpType::IntoFunction:
205
556k
    jump.setJumpType(evmasm::AssemblyItem::JumpType::IntoFunction);
206
556k
    break;
207
326k
  case JumpType::OutOfFunction:
208
326k
    jump.setJumpType(evmasm::AssemblyItem::JumpType::OutOfFunction);
209
326k
    break;
210
1.37M
  }
211
1.37M
  m_assembly.append(std::move(jump));
212
1.37M
}