Skip to content

Commit 9d7cc21

Browse files
committed
libevmasm: refactor asm-json export & add support for source list.
1 parent 724af73 commit 9d7cc21

File tree

4 files changed

+226
-141
lines changed

4 files changed

+226
-141
lines changed

libevmasm/Assembly.cpp

Lines changed: 125 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -222,17 +222,19 @@ string Assembly::assemblyString(
222222
return tmp.str();
223223
}
224224

225-
Json::Value Assembly::createJsonValue(string _name, int _source, int _begin, int _end, string _value, string _jumpType)
225+
Json::Value Assembly::createJsonValue(string const& _name, int _sourceIndex, AssemblyItem const& _item, std::string const& _value)
226226
{
227227
Json::Value value{Json::objectValue};
228228
value["name"] = _name;
229-
value["source"] = _source;
230-
value["begin"] = _begin;
231-
value["end"] = _end;
229+
value["source"] = _sourceIndex;
230+
value["begin"] = _item.location().start;
231+
value["end"] = _item.location().end;
232+
if (_item.m_modifierDepth != 0)
233+
value["modifierDepth"] = static_cast<int>(_item.m_modifierDepth);
232234
if (!_value.empty())
233235
value["value"] = _value;
234-
if (!_jumpType.empty())
235-
value["jumpType"] = _jumpType;
236+
if (!_item.getJumpTypeAsString().empty())
237+
value["jumpType"] = _item.getJumpTypeAsString();
236238
return value;
237239
}
238240

@@ -242,13 +244,123 @@ string Assembly::toStringInHex(u256 _value)
242244
hexStr << std::uppercase << hex << _value;
243245
return hexStr.str();
244246
}
247+
vector<Json::Value> Assembly::assemblyItemAsJSON(AssemblyItem const& _item, int _sourceIndex) const
248+
{
249+
vector<Json::Value> result;
250+
251+
switch (_item.type())
252+
{
253+
case Operation:
254+
result.emplace_back(createJsonValue(
255+
instructionInfo(_item.instruction()).name,
256+
_sourceIndex,
257+
_item));
258+
break;
259+
case Push:
260+
result.emplace_back(createJsonValue(
261+
"PUSH",
262+
_sourceIndex,
263+
_item,
264+
toStringInHex(_item.data())));
265+
break;
266+
case PushTag:
267+
if (_item.data() == 0)
268+
result.emplace_back(createJsonValue(
269+
"PUSH [ErrorTag]",
270+
_sourceIndex,
271+
_item));
272+
else
273+
result.emplace_back(createJsonValue(
274+
"PUSH [tag]",
275+
_sourceIndex,
276+
_item, toString(_item.data())));
277+
break;
278+
case PushSub:
279+
result.emplace_back(createJsonValue(
280+
"PUSH [$]",
281+
_sourceIndex,
282+
_item,
283+
toString(h256(_item.data()))
284+
));
285+
break;
286+
case PushSubSize:
287+
result.emplace_back(createJsonValue(
288+
"PUSH #[$]",_sourceIndex,_item, toString(h256(_item.data()))));
289+
break;
290+
case PushProgramSize:
291+
result.emplace_back(createJsonValue(
292+
"PUSHSIZE", _sourceIndex, _item));
293+
break;
294+
case PushLibraryAddress:
295+
result.emplace_back(createJsonValue(
296+
"PUSHLIB",
297+
_sourceIndex,
298+
_item,
299+
m_libraries.at(h256(_item.data()))));
300+
break;
301+
case PushDeployTimeAddress:
302+
result.emplace_back(createJsonValue(
303+
"PUSHDEPLOYADDRESS", _sourceIndex, _item));
304+
break;
305+
case PushImmutable:
306+
result.emplace_back(createJsonValue(
307+
"PUSHIMMUTABLE",
308+
_sourceIndex,
309+
_item,
310+
m_immutables.at(h256(_item.data()))));
311+
break;
312+
case AssignImmutable:
313+
result.emplace_back(createJsonValue(
314+
"ASSIGNIMMUTABLE",
315+
_sourceIndex,
316+
_item,
317+
m_immutables.at(h256(_item.data()))));
318+
break;
319+
case Tag:
320+
result.emplace_back(createJsonValue(
321+
"tag",
322+
_sourceIndex,
323+
_item,
324+
toString(_item.data())));
325+
result.emplace_back(createJsonValue(
326+
"JUMPDEST", _sourceIndex, _item));
327+
break;
328+
case PushData:
329+
result.emplace_back(createJsonValue(
330+
"PUSH data",
331+
_sourceIndex,
332+
_item,
333+
toStringInHex(_item.data())));
334+
break;
335+
case VerbatimBytecode:
336+
result.emplace_back(createJsonValue(
337+
"VERBATIM",
338+
_sourceIndex,
339+
_item,
340+
util::toHex(_item.verbatimData())));
341+
break;
342+
default:
343+
assertThrow(false, InvalidOpcode, "");
344+
}
345+
return result;
346+
}
245347

246-
Json::Value Assembly::assemblyJSON(map<string, unsigned> const& _sourceIndices) const
348+
Json::Value Assembly::assemblyJSON(map<string, unsigned> const& _sourceIndices, bool _includeSourceList) const
247349
{
248350
Json::Value root;
249-
root[".code"] = Json::arrayValue;
351+
if (_includeSourceList)
352+
{
353+
root["sourceList"] = Json::arrayValue;
354+
Json::Value& sourceList = root["sourceList"];
355+
vector<string> sources(_sourceIndices.size());
356+
for (auto const& item: _sourceIndices)
357+
sources[item.second] = item.first;
358+
for (auto const& item: sources)
359+
sourceList.append(item);
360+
}
250361

251-
Json::Value& collection = root[".code"];
362+
root[".code"] = Json::arrayValue;
363+
Json::Value& code = root[".code"];
252364
for (AssemblyItem const& i: m_items)
253365
{
254366
int sourceIndex = -1;
@@ -259,85 +371,8 @@ Json::Value Assembly::assemblyJSON(map<string, unsigned> const& _sourceIndices)
259371
sourceIndex = static_cast<int>(iter->second);
260372
}
261373

262-
switch (i.type())
263-
{
264-
case Operation:
265-
collection.append(
266-
createJsonValue(
267-
instructionInfo(i.instruction()).name,
268-
sourceIndex,
269-
i.location().start,
270-
i.location().end,
271-
i.getJumpTypeAsString())
272-
);
273-
break;
274-
case Push:
275-
collection.append(
276-
createJsonValue("PUSH", sourceIndex, i.location().start, i.location().end, toStringInHex(i.data()), i.getJumpTypeAsString()));
277-
break;
278-
case PushTag:
279-
if (i.data() == 0)
280-
collection.append(
281-
createJsonValue("PUSH [ErrorTag]", sourceIndex, i.location().start, i.location().end, ""));
282-
else
283-
collection.append(
284-
createJsonValue("PUSH [tag]", sourceIndex, i.location().start, i.location().end, toString(i.data())));
285-
break;
286-
case PushSub:
287-
collection.append(
288-
createJsonValue("PUSH [$]", sourceIndex, i.location().start, i.location().end, toString(h256(i.data()))));
289-
break;
290-
case PushSubSize:
291-
collection.append(
292-
createJsonValue("PUSH #[$]", sourceIndex, i.location().start, i.location().end, toString(h256(i.data()))));
293-
break;
294-
case PushProgramSize:
295-
collection.append(
296-
createJsonValue("PUSHSIZE", sourceIndex, i.location().start, i.location().end));
297-
break;
298-
case PushLibraryAddress:
299-
collection.append(
300-
createJsonValue("PUSHLIB", sourceIndex, i.location().start, i.location().end, m_libraries.at(h256(i.data())))
301-
);
302-
break;
303-
case PushDeployTimeAddress:
304-
collection.append(
305-
createJsonValue("PUSHDEPLOYADDRESS", sourceIndex, i.location().start, i.location().end)
306-
);
307-
break;
308-
case PushImmutable:
309-
collection.append(createJsonValue(
310-
"PUSHIMMUTABLE",
311-
sourceIndex,
312-
i.location().start,
313-
i.location().end,
314-
m_immutables.at(h256(i.data()))
315-
));
316-
break;
317-
case AssignImmutable:
318-
collection.append(createJsonValue(
319-
"ASSIGNIMMUTABLE",
320-
sourceIndex,
321-
i.location().start,
322-
i.location().end,
323-
m_immutables.at(h256(i.data()))
324-
));
325-
break;
326-
case Tag:
327-
collection.append(
328-
createJsonValue("tag", sourceIndex, i.location().start, i.location().end, toString(i.data())));
329-
collection.append(
330-
createJsonValue("JUMPDEST", sourceIndex, i.location().start, i.location().end));
331-
break;
332-
case PushData:
333-
collection.append(createJsonValue("PUSH data", sourceIndex, i.location().start, i.location().end, toStringInHex(i.data())));
334-
break;
335-
case VerbatimBytecode:
336-
collection.append(createJsonValue("VERBATIM", sourceIndex, i.location().start, i.location().end, util::toHex(i.verbatimData())));
337-
break;
338-
default:
339-
assertThrow(false, InvalidOpcode, "");
340-
}
374+
for (Json::Value const& item: assemblyItemAsJSON(i, sourceIndex))
375+
code.append(item);
341376
}
342377

343378
if (!m_data.empty() || !m_subs.empty())
@@ -352,11 +387,11 @@ Json::Value Assembly::assemblyJSON(map<string, unsigned> const& _sourceIndices)
352387
{
353388
std::stringstream hexStr;
354389
hexStr << hex << i;
355-
data[hexStr.str()] = m_subs[i]->assemblyJSON(_sourceIndices);
390+
data[hexStr.str()] = m_subs[i]->assemblyJSON(_sourceIndices, false);
356391
}
357392
}
358393

359-
if (m_auxiliaryData.size() > 0)
394+
if (!m_auxiliaryData.empty())
360395
root[".auxdata"] = util::toHex(m_auxiliaryData);
361396

362397
return root;

libevmasm/Assembly.h

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include <sstream>
4040
#include <memory>
4141
#include <map>
42+
#include <utility>
4243

4344
namespace solidity::evmasm
4445
{
@@ -147,7 +148,8 @@ class Assembly
147148

148149
/// Create a JSON representation of the assembly.
149150
Json::Value assemblyJSON(
150-
std::map<std::string, unsigned> const& _sourceIndices = std::map<std::string, unsigned>()
151+
std::map<std::string, unsigned> const& _sourceIndices = std::map<std::string, unsigned>(),
152+
bool _includeSourceList = true
151153
) const;
152154

153155
/// Mark this assembly as invalid. Calling ``assemble`` on it will throw.
@@ -158,6 +160,16 @@ class Assembly
158160

159161
bool isCreation() const { return m_creation; }
160162

163+
void setSources(std::vector<std::shared_ptr<std::string const>> _sources) {
164+
m_sources = std::move(_sources);
165+
}
166+
167+
void setSources(std::vector<std::string> const& _sources) {
168+
for (auto const& item: _sources)
169+
m_sources.emplace_back(std::make_shared<std::string>(item));
170+
}
171+
std::vector<std::shared_ptr<std::string const>> sources() const& { return m_sources; }
172+
161173
protected:
162174
/// Does the same operations as @a optimise, but should only be applied to a sub and
163175
/// returns the replaced tags. Also takes an argument containing the tags of this assembly
@@ -166,15 +178,10 @@ class Assembly
166178

167179
unsigned codeSize(unsigned subTagSize) const;
168180

181+
std::vector<Json::Value> assemblyItemAsJSON(AssemblyItem const& _item, int _sourceIndex) const;
182+
169183
private:
170-
static Json::Value createJsonValue(
171-
std::string _name,
172-
int _source,
173-
int _begin,
174-
int _end,
175-
std::string _value = std::string(),
176-
std::string _jumpType = std::string()
177-
);
184+
static Json::Value createJsonValue(std::string const& _name, int _sourceIndex, AssemblyItem const& _item, std::string const& _value = "");
178185
static std::string toStringInHex(u256 _value);
179186

180187
bool m_invalid = false;
@@ -222,6 +229,8 @@ class Assembly
222229
std::string m_name;
223230

224231
langutil::SourceLocation m_currentSourceLocation;
232+
std::vector<std::shared_ptr<std::string const>> m_sources;
233+
225234
public:
226235
size_t m_currentModifierDepth = 0;
227236
};

0 commit comments

Comments
 (0)