Skip to content

Commit 5d74861

Browse files
committed
[libevmasm] Add 'modifierDepth' to assembly json.
1 parent 3de13c4 commit 5d74861

File tree

2 files changed

+89
-33
lines changed

2 files changed

+89
-33
lines changed

libevmasm/Assembly.cpp

Lines changed: 87 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,16 @@ 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(
226+
string _name, int _sourceIndex, size_t _modifierDepth, int _begin, int _end, string _value, string _jumpType)
226227
{
227228
Json::Value value{Json::objectValue};
228229
value["name"] = _name;
229-
value["source"] = _source;
230+
value["source"] = _sourceIndex;
230231
value["begin"] = _begin;
231232
value["end"] = _end;
233+
if (_modifierDepth != 0)
234+
value["modifierDepth"] = static_cast<int>(_modifierDepth);
232235
if (!_value.empty())
233236
value["value"] = _value;
234237
if (!_jumpType.empty())
@@ -248,7 +251,8 @@ AssemblyItem Assembly::loadItemFromJSON(Json::Value const& _json)
248251
std::string name = _json["name"].isString() ? _json["name"].asString() : "";
249252
int begin = _json["begin"].isInt() ? _json["begin"].asInt() : -1;
250253
int end = _json["end"].isInt() ? _json["end"].asInt() : -1;
251-
int srcIndex = _json["source"].isInt() ? _json["source"].asInt() : -1;
254+
int srcIndex = _json["source"].isInt() ? _json["source"].asInt() : -1;
255+
size_t modifierDepth = _json["modifierDepth"].isInt() ? static_cast<size_t>(_json["modifierDepth"].asInt()) : 0;
252256
std::string value = _json["value"].isString() ? _json["value"].asString() : "";
253257
std::string jumpType = _json["jumpType"].isString() ? _json["jumpType"].asString() : "";
254258
solAssert(!name.empty(), "");
@@ -274,15 +278,18 @@ AssemblyItem Assembly::loadItemFromJSON(Json::Value const& _json)
274278
SourceLocation location;
275279
location.start = begin;
276280
location.end = end;
277-
if (srcIndex > -1 && srcIndex < (int)sources().size())
281+
if (srcIndex > -1 && srcIndex < (int) sources().size())
278282
location.sourceName = sources()[static_cast<size_t>(srcIndex)];
279283

284+
AssemblyItem result(0);
285+
280286
if (c_instructions.find(name) != c_instructions.end())
281287
{
282288
AssemblyItem item{c_instructions.at(name), location};
289+
item.m_modifierDepth = modifierDepth;
283290
if (!value.empty())
284291
item.setJumpType(value);
285-
return item;
292+
result = item;
286293
}
287294
else
288295
{
@@ -294,69 +301,71 @@ AssemblyItem Assembly::loadItemFromJSON(Json::Value const& _json)
294301
AssemblyItem item{AssemblyItemType::Push, data, location};
295302
if (!jumpType.empty())
296303
item.setJumpType(jumpType);
297-
return item;
304+
result = item;
298305
}
299306
else if (name == "PUSH [ErrorTag]")
300-
return {AssemblyItemType::PushTag, data, location};
307+
result = {AssemblyItemType::PushTag, data, location};
301308
else if (name == "PUSH [tag]")
302309
{
303310
if (!value.empty())
304311
data = u256(value);
305312
updateUsedTags(data);
306-
return {AssemblyItemType::PushTag, data, location};
313+
result = {AssemblyItemType::PushTag, data, location};
307314
}
308315
else if (name == "PUSH [$]")
309316
{
310317
if (!value.empty())
311318
data = u256("0x" + value);
312-
return {AssemblyItemType::PushSub, data, location};
319+
result = {AssemblyItemType::PushSub, data, location};
313320
}
314321
else if (name == "PUSH #[$]")
315322
{
316323
if (!value.empty())
317324
data = u256("0x" + value);
318-
return {AssemblyItemType::PushSubSize, data, location};
325+
result = {AssemblyItemType::PushSubSize, data, location};
319326
}
320327
else if (name == "PUSHSIZE")
321-
return {AssemblyItemType::PushProgramSize, data, location};
328+
result = {AssemblyItemType::PushProgramSize, data, location};
322329
else if (name == "PUSHLIB")
323330
{
324331
h256 hash = updateLibraries(value);
325-
return {AssemblyItemType::PushLibraryAddress, hash, location};
332+
result = {AssemblyItemType::PushLibraryAddress, hash, location};
326333
}
327334
else if (name == "PUSHDEPLOYADDRESS")
328-
return {AssemblyItemType::PushDeployTimeAddress, data, location};
335+
result = {AssemblyItemType::PushDeployTimeAddress, data, location};
329336
else if (name == "PUSHIMMUTABLE")
330337
{
331338
h256 hash = updateImmutables(value);
332-
return {AssemblyItemType::PushImmutable, hash, location};
339+
result = {AssemblyItemType::PushImmutable, hash, location};
333340
}
334341
else if (name == "ASSIGNIMMUTABLE")
335342
{
336343
h256 hash = updateImmutables(value);
337-
return {AssemblyItemType::AssignImmutable, hash, location};
344+
result = {AssemblyItemType::AssignImmutable, hash, location};
338345
}
339346
else if (name == "tag")
340347
{
341348
if (!value.empty())
342349
data = u256(value);
343-
return {AssemblyItemType::Tag, data, location};
350+
result = {AssemblyItemType::Tag, data, location};
344351
}
345352
else if (name == "PUSH data")
346353
{
347354
if (!value.empty())
348355
data = u256("0x" + value);
349-
return {AssemblyItemType::PushData, data, location};
356+
result = {AssemblyItemType::PushData, data, location};
350357
}
351358
else if (name == "VERBATIM")
352359
{
353360
AssemblyItem item(fromHex(value), 0, 0);
354361
item.setLocation(location);
355-
return item;
362+
result = item;
356363
}
357364
else
358365
assertThrow(false, InvalidOpcode, "");
359366
}
367+
result.m_modifierDepth = modifierDepth;
368+
return result;
360369
}
361370

362371
vector<Json::Value> Assembly::assemblyItemAsJSON(AssemblyItem const& _item, int _sourceIndex) const
@@ -369,6 +378,7 @@ vector<Json::Value> Assembly::assemblyItemAsJSON(AssemblyItem const& _item, int
369378
result.emplace_back(createJsonValue(
370379
instructionInfo(_item.instruction()).name,
371380
_sourceIndex,
381+
_item.m_modifierDepth,
372382
_item.location().start,
373383
_item.location().end,
374384
_item.getJumpTypeAsString()));
@@ -377,42 +387,70 @@ vector<Json::Value> Assembly::assemblyItemAsJSON(AssemblyItem const& _item, int
377387
result.emplace_back(createJsonValue(
378388
"PUSH",
379389
_sourceIndex,
390+
_item.m_modifierDepth,
380391
_item.location().start,
381392
_item.location().end,
382393
toStringInHex(_item.data()),
383394
_item.getJumpTypeAsString()));
384395
break;
385396
case PushTag:
386397
if (_item.data() == 0)
387-
result.emplace_back(
388-
createJsonValue("PUSH [ErrorTag]", _sourceIndex, _item.location().start, _item.location().end, ""));
398+
result.emplace_back(createJsonValue(
399+
"PUSH [ErrorTag]",
400+
_sourceIndex,
401+
_item.m_modifierDepth,
402+
_item.location().start,
403+
_item.location().end,
404+
""));
389405
else
390406
result.emplace_back(createJsonValue(
391-
"PUSH [tag]", _sourceIndex, _item.location().start, _item.location().end, toString(_item.data())));
407+
"PUSH [tag]",
408+
_sourceIndex,
409+
_item.m_modifierDepth,
410+
_item.location().start,
411+
_item.location().end,
412+
toString(_item.data())));
392413
break;
393414
case PushSub:
394415
result.emplace_back(createJsonValue(
395-
"PUSH [$]", _sourceIndex, _item.location().start, _item.location().end, toString(h256(_item.data()))));
416+
"PUSH [$]",
417+
_sourceIndex,
418+
_item.m_modifierDepth,
419+
_item.location().start,
420+
_item.location().end,
421+
toString(h256(_item.data()))));
396422
break;
397423
case PushSubSize:
398424
result.emplace_back(createJsonValue(
399-
"PUSH #[$]", _sourceIndex, _item.location().start, _item.location().end, toString(h256(_item.data()))));
425+
"PUSH #[$]",
426+
_sourceIndex,
427+
_item.m_modifierDepth,
428+
_item.location().start,
429+
_item.location().end,
430+
toString(h256(_item.data()))));
400431
break;
401432
case PushProgramSize:
402-
result.emplace_back(createJsonValue("PUSHSIZE", _sourceIndex, _item.location().start, _item.location().end));
433+
result.emplace_back(createJsonValue(
434+
"PUSHSIZE", _sourceIndex, _item.m_modifierDepth, _item.location().start, _item.location().end));
403435
break;
404436
case PushLibraryAddress:
405437
result.emplace_back(createJsonValue(
406-
"PUSHLIB", _sourceIndex, _item.location().start, _item.location().end, m_libraries.at(h256(_item.data()))));
438+
"PUSHLIB",
439+
_sourceIndex,
440+
_item.m_modifierDepth,
441+
_item.location().start,
442+
_item.location().end,
443+
m_libraries.at(h256(_item.data()))));
407444
break;
408445
case PushDeployTimeAddress:
409-
result.emplace_back(
410-
createJsonValue("PUSHDEPLOYADDRESS", _sourceIndex, _item.location().start, _item.location().end));
446+
result.emplace_back(createJsonValue(
447+
"PUSHDEPLOYADDRESS", _sourceIndex, _item.m_modifierDepth, _item.location().start, _item.location().end));
411448
break;
412449
case PushImmutable:
413450
result.emplace_back(createJsonValue(
414451
"PUSHIMMUTABLE",
415452
_sourceIndex,
453+
_item.m_modifierDepth,
416454
_item.location().start,
417455
_item.location().end,
418456
m_immutables.at(h256(_item.data()))));
@@ -421,22 +459,39 @@ vector<Json::Value> Assembly::assemblyItemAsJSON(AssemblyItem const& _item, int
421459
result.emplace_back(createJsonValue(
422460
"ASSIGNIMMUTABLE",
423461
_sourceIndex,
462+
_item.m_modifierDepth,
424463
_item.location().start,
425464
_item.location().end,
426465
m_immutables.at(h256(_item.data()))));
427466
break;
428467
case Tag:
429-
result.emplace_back(
430-
createJsonValue("tag", _sourceIndex, _item.location().start, _item.location().end, toString(_item.data())));
431-
result.emplace_back(createJsonValue("JUMPDEST", _sourceIndex, _item.location().start, _item.location().end));
468+
result.emplace_back(createJsonValue(
469+
"tag",
470+
_sourceIndex,
471+
_item.m_modifierDepth,
472+
_item.location().start,
473+
_item.location().end,
474+
toString(_item.data())));
475+
result.emplace_back(createJsonValue(
476+
"JUMPDEST", _sourceIndex, _item.m_modifierDepth, _item.location().start, _item.location().end));
432477
break;
433478
case PushData:
434479
result.emplace_back(createJsonValue(
435-
"PUSH data", _sourceIndex, _item.location().start, _item.location().end, toStringInHex(_item.data())));
480+
"PUSH data",
481+
_sourceIndex,
482+
_item.m_modifierDepth,
483+
_item.location().start,
484+
_item.location().end,
485+
toStringInHex(_item.data())));
436486
break;
437487
case VerbatimBytecode:
438488
result.emplace_back(createJsonValue(
439-
"VERBATIM", _sourceIndex, _item.location().start, _item.location().end, util::toHex(_item.verbatimData())));
489+
"VERBATIM",
490+
_sourceIndex,
491+
_item.m_modifierDepth,
492+
_item.location().start,
493+
_item.location().end,
494+
util::toHex(_item.verbatimData())));
440495
break;
441496
default:
442497
assertThrow(false, InvalidOpcode, "");

libevmasm/Assembly.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,8 @@ class Assembly
193193
bool addAssemblyItemsFromJSON(Json::Value const& _code);
194194
static Json::Value createJsonValue(
195195
std::string _name,
196-
int _source,
196+
int _sourceIndex,
197+
size_t _modifierDepth,
197198
int _begin,
198199
int _end,
199200
std::string _value = std::string(),

0 commit comments

Comments
 (0)