Skip to content

Commit f37f081

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

File tree

4 files changed

+197
-109
lines changed

4 files changed

+197
-109
lines changed

libevmasm/Assembly.cpp

Lines changed: 128 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -222,17 +222,22 @@ 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+
{
238+
assertThrow(_name == "JUMP" && value.isMember("value") == false, AssemblyException, "");
239+
value["value"] = _item.getJumpTypeAsString();
240+
}
236241
return value;
237242
}
238243

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

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

251-
Json::Value& collection = root[".code"];
365+
root[".code"] = Json::arrayValue;
366+
Json::Value& code = root[".code"];
252367
for (AssemblyItem const& i: m_items)
253368
{
254369
int sourceIndex = -1;
@@ -259,85 +374,8 @@ Json::Value Assembly::assemblyJSON(map<string, unsigned> const& _sourceIndices)
259374
sourceIndex = static_cast<int>(iter->second);
260375
}
261376

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-
}
377+
for (Json::Value const& item: assemblyItemAsJSON(i, sourceIndex))
378+
code.append(item);
341379
}
342380

343381
if (!m_data.empty() || !m_subs.empty())
@@ -352,11 +390,11 @@ Json::Value Assembly::assemblyJSON(map<string, unsigned> const& _sourceIndices)
352390
{
353391
std::stringstream hexStr;
354392
hexStr << hex << i;
355-
data[hexStr.str()] = m_subs[i]->assemblyJSON(_sourceIndices);
393+
data[hexStr.str()] = m_subs[i]->assemblyJSON(_sourceIndices, false);
356394
}
357395
}
358396

359-
if (m_auxiliaryData.size() > 0)
397+
if (!m_auxiliaryData.empty())
360398
root[".auxdata"] = util::toHex(m_auxiliaryData);
361399

362400
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
};

test/cmdlineTests/asm_json/output

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1582,5 +1582,10 @@ EVM assembly:
15821582
}
15831583
]
15841584
}
1585-
}
1585+
},
1586+
"sourceList":
1587+
[
1588+
"asm_json/input.sol",
1589+
"#utility.yul"
1590+
]
15861591
}

0 commit comments

Comments
 (0)