Introduce assemblyString

This commit is contained in:
Alex Beregszaszi 2017-08-30 02:17:15 +01:00
parent 50570c6c79
commit bbfb16cf5c
10 changed files with 27 additions and 24 deletions

View File

@ -208,6 +208,13 @@ void Assembly::assemblyStream(ostream& _out, string const& _prefix, StringMap co
_out << endl << _prefix << "auxdata: 0x" << toHex(m_auxiliaryData) << endl; _out << endl << _prefix << "auxdata: 0x" << toHex(m_auxiliaryData) << endl;
} }
string Assembly::assemblyString(StringMap const& _sourceCodes) const
{
ostringstream tmp;
assemblyStream(tmp, "", _sourceCodes);
return tmp.str();
}
Json::Value Assembly::createJsonValue(string _name, int _begin, int _end, string _value, string _jumpType) Json::Value Assembly::createJsonValue(string _name, int _begin, int _end, string _value, string _jumpType)
{ {
Json::Value value; Json::Value value;

View File

@ -121,6 +121,9 @@ public:
Assembly& optimise(bool _enable, bool _isCreation = true, size_t _runs = 200); Assembly& optimise(bool _enable, bool _isCreation = true, size_t _runs = 200);
/// Create a text representation of the assembly. /// Create a text representation of the assembly.
std::string assemblyString(
StringMap const& _sourceCodes = StringMap()
) const;
void assemblyStream( void assemblyStream(
std::ostream& _out, std::ostream& _out,
std::string const& _prefix = "", std::string const& _prefix = "",

View File

@ -72,14 +72,13 @@ std::string dev::eth::compileLLLToAsm(std::string const& _src, bool _opt, std::v
{ {
CompilerState cs; CompilerState cs;
cs.populateStandard(); cs.populateStandard();
stringstream ret;
auto assembly = CodeFragment::compile(_src, cs).assembly(cs); auto assembly = CodeFragment::compile(_src, cs).assembly(cs);
if (_opt) if (_opt)
assembly = assembly.optimise(true); assembly = assembly.optimise(true);
assembly.assemblyStream(ret); string ret = assembly.assemblyString();
for (auto i: cs.treesToKill) for (auto i: cs.treesToKill)
killBigints(i); killBigints(i);
return ret.str(); return ret;
} }
catch (Exception const& _e) catch (Exception const& _e)
{ {

View File

@ -60,9 +60,9 @@ public:
/// @returns Only the runtime object (without constructor). /// @returns Only the runtime object (without constructor).
eth::LinkerObject runtimeObject() const { return m_context.assembledRuntimeObject(m_runtimeSub); } eth::LinkerObject runtimeObject() const { return m_context.assembledRuntimeObject(m_runtimeSub); }
/// @arg _sourceCodes is the map of input files to source code strings /// @arg _sourceCodes is the map of input files to source code strings
void assemblyStream(std::ostream& _stream, StringMap const& _sourceCodes = StringMap()) const std::string assemblyString(StringMap const& _sourceCodes = StringMap()) const
{ {
m_context.assemblyStream(_stream, _sourceCodes); return m_context.assemblyString(_sourceCodes);
} }
/// @arg _sourceCodes is the map of input files to source code strings /// @arg _sourceCodes is the map of input files to source code strings
Json::Value assemblyJSON(StringMap const& _sourceCodes = StringMap()) const Json::Value assemblyJSON(StringMap const& _sourceCodes = StringMap()) const

View File

@ -209,9 +209,9 @@ public:
eth::Assembly& nonConstAssembly() { return *m_asm; } eth::Assembly& nonConstAssembly() { return *m_asm; }
/// @arg _sourceCodes is the map of input files to source code strings /// @arg _sourceCodes is the map of input files to source code strings
void assemblyStream(std::ostream& _stream, StringMap const& _sourceCodes = StringMap()) const std::string assemblyString(StringMap const& _sourceCodes = StringMap()) const
{ {
m_asm->assemblyStream(_stream, "", _sourceCodes); return m_asm->assemblyString(_sourceCodes);
} }
/// @arg _sourceCodes is the map of input files to source code strings /// @arg _sourceCodes is the map of input files to source code strings

View File

@ -91,9 +91,7 @@ MachineAssemblyObject AssemblyStack::assemble(Machine _machine) const
eth::Assembly assembly; eth::Assembly assembly;
assembly::CodeGenerator::assemble(*m_parserResult, *m_analysisInfo, assembly); assembly::CodeGenerator::assemble(*m_parserResult, *m_analysisInfo, assembly);
object.bytecode = make_shared<eth::LinkerObject>(assembly.assemble()); object.bytecode = make_shared<eth::LinkerObject>(assembly.assemble());
ostringstream tmp; object.assembly = assembly.assemblyString();
assembly.assemblyStream(tmp);
object.assembly = tmp.str();
return object; return object;
} }
case Machine::EVM15: case Machine::EVM15:

View File

@ -347,15 +347,14 @@ eth::LinkerObject const& CompilerStack::cloneObject(string const& _contractName)
return contract(_contractName).cloneObject; return contract(_contractName).cloneObject;
} }
void CompilerStack::assemblyStream(ostream& _outStream, string const& _contractName, StringMap _sourceCodes) const /// FIXME: cache this string
string CompilerStack::assemblyString(string const& _contractName, StringMap _sourceCodes) const
{ {
Contract const& currentContract = contract(_contractName); Contract const& currentContract = contract(_contractName);
if (currentContract.compiler) if (currentContract.compiler)
currentContract.compiler->assemblyStream(_outStream, _sourceCodes); return currentContract.compiler->assemblyString(_sourceCodes);
else else
{ return string();
_outStream << "Contract not fully implemented" << endl;
}
} }
/// FIXME: cache the JSON /// FIXME: cache the JSON

View File

@ -190,10 +190,10 @@ public:
/// if the contract does not (yet) have bytecode. /// if the contract does not (yet) have bytecode.
std::string const* runtimeSourceMapping(std::string const& _contractName = "") const; std::string const* runtimeSourceMapping(std::string const& _contractName = "") const;
/// Streams a verbose version of the assembly to @a _outStream. /// @return a verbose text representation of the assembly.
/// @arg _sourceCodes is the map of input files to source code strings /// @arg _sourceCodes is the map of input files to source code strings
/// Prerequisite: Successful compilation. /// Prerequisite: Successful compilation.
void assemblyStream(std::ostream& _outStream, std::string const& _contractName = "", StringMap _sourceCodes = StringMap()) const; std::string assemblyString(std::string const& _contractName = "", StringMap _sourceCodes = StringMap()) const;
/// @returns a JSON representation of the assembly. /// @returns a JSON representation of the assembly.
/// @arg _sourceCodes is the map of input files to source code strings /// @arg _sourceCodes is the map of input files to source code strings

View File

@ -400,9 +400,7 @@ Json::Value StandardCompiler::compileInternal(Json::Value const& _input)
// EVM // EVM
Json::Value evmData(Json::objectValue); Json::Value evmData(Json::objectValue);
// @TODO: add ir // @TODO: add ir
ostringstream tmp; evmData["assembly"] = m_compilerStack.assemblyString(contractName, createSourceList(_input));
m_compilerStack.assemblyStream(tmp, contractName, createSourceList(_input));
evmData["assembly"] = tmp.str();
evmData["legacyAssembly"] = m_compilerStack.assemblyJSON(contractName, createSourceList(_input)); evmData["legacyAssembly"] = m_compilerStack.assemblyJSON(contractName, createSourceList(_input));
evmData["methodIdentifiers"] = m_compilerStack.methodIdentifiers(contractName); evmData["methodIdentifiers"] = m_compilerStack.methodIdentifiers(contractName);
evmData["gasEstimates"] = m_compilerStack.gasEstimates(contractName); evmData["gasEstimates"] = m_compilerStack.gasEstimates(contractName);

View File

@ -1156,9 +1156,8 @@ void CommandLineInterface::outputCompilationResults()
} }
else else
{ {
stringstream data; string ret = m_compiler->assemblyString(contract, m_sourceCodes);
m_compiler->assemblyStream(data, contract, m_sourceCodes); createFile(m_compiler->filesystemFriendlyName(contract) + ".evm", ret));
createFile(m_compiler->filesystemFriendlyName(contract) + ".evm", data.str());
} }
} }
else else
@ -1167,7 +1166,7 @@ void CommandLineInterface::outputCompilationResults()
if (m_args.count(g_argAsmJson) if (m_args.count(g_argAsmJson)
cout << m_compiler->assemblyJSON(contract, m_sourceCodes); cout << m_compiler->assemblyJSON(contract, m_sourceCodes);
else else
m_compiler->assemblyStream(cout, contract, m_sourceCodes); cout << m_compiler->assemblyString(contract, m_sourceCodes);
} }
} }