Coverage Report

Created: 2025-06-24 07:59

/src/solidity/libevmasm/EVMAssemblyStack.cpp
Line
Count
Source (jump to first uncovered line)
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
#include <libevmasm/EVMAssemblyStack.h>
20
21
#include <libsolutil/JSON.h>
22
#include <liblangutil/Exceptions.h>
23
#include <libsolidity/codegen/CompilerContext.h>
24
25
#include <range/v3/view/enumerate.hpp>
26
#include <range/v3/view/transform.hpp>
27
28
#include <tuple>
29
30
using namespace solidity::util;
31
using namespace solidity::langutil;
32
using namespace solidity::frontend;
33
34
namespace solidity::evmasm
35
{
36
37
void EVMAssemblyStack::parseAndAnalyze(std::string const& _sourceName, std::string const& _source)
38
0
{
39
0
  Json assemblyJson;
40
0
  solRequire(jsonParseStrict(_source, assemblyJson), AssemblyImportException, "Could not parse JSON file.");
41
0
  analyze(_sourceName, assemblyJson);
42
0
}
43
44
void EVMAssemblyStack::analyze(std::string const& _sourceName, Json const& _assemblyJson)
45
0
{
46
0
  solAssert(!m_evmAssembly);
47
0
  m_name = _sourceName;
48
0
  std::tie(m_evmAssembly, m_sourceList) = evmasm::Assembly::fromJSON(_assemblyJson, {}, 0, m_eofVersion);
49
0
  solRequire(m_evmAssembly != nullptr, AssemblyImportException, "Could not create evm assembly object.");
50
0
}
51
52
void EVMAssemblyStack::assemble()
53
0
{
54
0
  solAssert(m_evmAssembly);
55
0
  solAssert(m_evmAssembly->isCreation());
56
0
  solAssert(!m_evmRuntimeAssembly);
57
58
0
  m_evmAssembly->optimise(m_optimiserSettings);
59
0
  m_object = m_evmAssembly->assemble();
60
  // TODO: Check for EOF
61
0
  solAssert(m_evmAssembly->codeSections().size() == 1);
62
0
  m_sourceMapping = AssemblyItem::computeSourceMapping(m_evmAssembly->codeSections().front().items, sourceIndices());
63
0
  if (m_evmAssembly->numSubs() > 0)
64
0
  {
65
0
    m_evmRuntimeAssembly = std::make_shared<evmasm::Assembly>(m_evmAssembly->sub(0));
66
0
    solAssert(m_evmRuntimeAssembly && !m_evmRuntimeAssembly->isCreation());
67
    // TODO: Check for EOF
68
0
    solAssert(m_evmRuntimeAssembly->codeSections().size() == 1);
69
0
    m_runtimeSourceMapping = AssemblyItem::computeSourceMapping(m_evmRuntimeAssembly->codeSections().front().items, sourceIndices());
70
0
    m_runtimeObject = m_evmRuntimeAssembly->assemble();
71
0
  }
72
0
}
73
74
LinkerObject const& EVMAssemblyStack::object(std::string const& _contractName) const
75
0
{
76
0
  solAssert(_contractName == m_name);
77
0
  return object();
78
0
}
79
80
LinkerObject const& EVMAssemblyStack::runtimeObject(std::string const& _contractName) const
81
0
{
82
0
  solAssert(_contractName == m_name);
83
0
  return runtimeObject();
84
0
}
85
86
std::map<std::string, unsigned> EVMAssemblyStack::sourceIndices() const
87
0
{
88
0
  solAssert(m_evmAssembly);
89
0
  return m_sourceList
90
0
    | ranges::views::enumerate
91
0
    | ranges::views::transform([](auto const& _source) { return std::make_pair(_source.second, _source.first); })
92
0
    | ranges::to<std::map<std::string, unsigned>>;
93
0
}
94
95
std::string const* EVMAssemblyStack::sourceMapping(std::string const& _contractName) const
96
0
{
97
0
  solAssert(_contractName == m_name);
98
0
  return &sourceMapping();
99
0
}
100
101
std::string const* EVMAssemblyStack::runtimeSourceMapping(std::string const& _contractName) const
102
0
{
103
0
  solAssert(_contractName == m_name);
104
0
  return &runtimeSourceMapping();
105
0
}
106
107
Json EVMAssemblyStack::ethdebug(std::string const& _contractName) const
108
0
{
109
0
  solAssert(_contractName == m_name);
110
0
  solAssert(m_ethdebug != nullptr);
111
0
  return *m_ethdebug;
112
0
}
113
114
Json EVMAssemblyStack::ethdebugRuntime(std::string const& _contractName) const
115
0
{
116
0
  solAssert(_contractName == m_name);
117
0
  solAssert(m_ethdebugRuntime != nullptr);
118
0
  return *m_ethdebugRuntime;
119
0
}
120
121
Json EVMAssemblyStack::ethdebug() const
122
0
{
123
0
  return {};
124
0
}
125
126
Json EVMAssemblyStack::assemblyJSON() const
127
0
{
128
0
  solAssert(m_evmAssembly);
129
0
  return m_evmAssembly->assemblyJSON(sourceIndices());
130
0
}
131
132
Json EVMAssemblyStack::assemblyJSON(std::string const& _contractName) const
133
0
{
134
0
  solAssert(_contractName == m_name);
135
0
  return assemblyJSON();
136
0
}
137
138
std::string EVMAssemblyStack::assemblyString(StringMap const& _sourceCodes) const
139
0
{
140
0
  solAssert(m_evmAssembly);
141
0
  return m_evmAssembly->assemblyString(m_debugInfoSelection, _sourceCodes);
142
0
}
143
144
std::string EVMAssemblyStack::assemblyString(std::string const& _contractName, StringMap const& _sourceCodes) const
145
0
{
146
0
  solAssert(_contractName == m_name);
147
0
  return assemblyString(_sourceCodes);
148
0
}
149
150
std::string const EVMAssemblyStack::filesystemFriendlyName(std::string const& _contractName) const
151
0
{
152
0
  solAssert(_contractName == m_name);
153
0
  return m_name;
154
0
}
155
156
std::vector<std::string> EVMAssemblyStack::sourceNames() const
157
0
{
158
0
  return m_sourceList;
159
0
}
160
161
} // namespace solidity::evmasm