@@ -222,141 +222,80 @@ string Assembly::assemblyString(
222
222
return tmp.str ();
223
223
}
224
224
225
- Json::Value Assembly::createJsonValue (string _name, int _source, int _begin, int _end, string _value, string _jumpType)
226
- {
227
- Json::Value value{Json::objectValue};
228
- value[" name" ] = _name;
229
- value[" source" ] = _source;
230
- value[" begin" ] = _begin;
231
- value[" end" ] = _end;
232
- if (!_value.empty ())
233
- value[" value" ] = _value;
234
- if (!_jumpType.empty ())
235
- value[" jumpType" ] = _jumpType;
236
- return value;
237
- }
238
-
239
- string Assembly::toStringInHex (u256 _value)
240
- {
241
- std::stringstream hexStr;
242
- hexStr << std::uppercase << hex << _value;
243
- return hexStr.str ();
244
- }
245
-
246
- Json::Value Assembly::assemblyJSON (map<string, unsigned > const & _sourceIndices) const
225
+ Json::Value Assembly::assemblyJSON (map<string, unsigned > const & _sourceIndices, bool _includeSourceList) const
247
226
{
248
227
Json::Value root;
249
228
root[" .code" ] = Json::arrayValue;
250
-
251
- Json::Value& collection = root[" .code" ];
252
- for (AssemblyItem const & i: m_items)
229
+ Json::Value& code = root[" .code" ];
230
+ for (AssemblyItem const & item: m_items)
253
231
{
254
232
int sourceIndex = -1 ;
255
- if (i .location ().sourceName )
233
+ if (item .location ().sourceName )
256
234
{
257
- auto iter = _sourceIndices.find (*i .location ().sourceName );
235
+ auto iter = _sourceIndices.find (*item .location ().sourceName );
258
236
if (iter != _sourceIndices.end ())
259
237
sourceIndex = static_cast <int >(iter->second );
260
238
}
261
239
262
- switch (i.type ())
240
+ auto [name, data] = item.nameAndData ();
241
+ Json::Value jsonItem;
242
+ jsonItem[" name" ] = name;
243
+ jsonItem[" begin" ] = item.location ().start ;
244
+ jsonItem[" end" ] = item.location ().end ;
245
+ if (item.m_modifierDepth != 0 )
246
+ jsonItem[" modifierDepth" ] = static_cast <int >(item.m_modifierDepth );
247
+ std::string jumpType = item.getJumpTypeAsString ();
248
+ if (!jumpType.empty ())
249
+ jsonItem[" jumpType" ] = jumpType;
250
+ if (name == " PUSHLIB" )
251
+ data = m_libraries.at (h256 (data));
252
+ else if (name == " PUSHIMMUTABLE" || name == " ASSIGNIMMUTABLE" )
253
+ data = m_immutables.at (h256 (data));
254
+ if (!data.empty ())
255
+ jsonItem[" value" ] = data;
256
+ jsonItem[" source" ] = sourceIndex;
257
+ code.append (move (jsonItem));
258
+
259
+ if (item.type () == AssemblyItemType::Tag)
263
260
{
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, " " );
261
+ Json::Value jumpdest;
262
+ jumpdest[" name" ] = " JUMPDEST" ;
263
+ jumpdest[" begin" ] = item.location ().start ;
264
+ jumpdest[" end" ] = item.location ().end ;
265
+ jumpdest[" source" ] = sourceIndex;
266
+ if (item.m_modifierDepth != 0 )
267
+ jumpdest[" modifierDepth" ] = static_cast <int >(item.m_modifierDepth );
268
+ code.append (move (jumpdest));
340
269
}
341
270
}
271
+ if (_includeSourceList)
272
+ {
273
+ root[" sourceList" ] = Json::arrayValue;
274
+ Json::Value& jsonSourceList = root[" sourceList" ];
275
+ vector<string> sourceNames (_sourceIndices.size ());
276
+ for (auto const & [name, index]: _sourceIndices)
277
+ sourceNames[index] = name;
278
+ for (auto const & item: sourceNames)
279
+ jsonSourceList.append (item);
280
+ }
342
281
343
282
if (!m_data.empty () || !m_subs.empty ())
344
283
{
345
284
root[" .data" ] = Json::objectValue;
346
285
Json::Value& data = root[" .data" ];
347
286
for (auto const & i: m_data)
348
287
if (u256 (i.first ) >= m_subs.size ())
349
- data[toStringInHex (( u256)i.first )] = util::toHex (i.second );
288
+ data[util::toHex ( toBigEndian (( u256)i.first ), util::HexPrefix::DontAdd, util::HexCase::Upper )] = util::toHex (i.second );
350
289
351
290
for (size_t i = 0 ; i < m_subs.size (); ++i)
352
291
{
353
292
std::stringstream hexStr;
354
293
hexStr << hex << i;
355
- data[hexStr.str ()] = m_subs[i]->assemblyJSON (_sourceIndices);
294
+ data[hexStr.str ()] = m_subs[i]->assemblyJSON (_sourceIndices, /* _includeSourceList = */ false );
356
295
}
357
296
}
358
297
359
- if (m_auxiliaryData.size () > 0 )
298
+ if (! m_auxiliaryData.empty () )
360
299
root[" .auxdata" ] = util::toHex (m_auxiliaryData);
361
300
362
301
return root;
0 commit comments