mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Allow assembly stack to translate its source.
This commit is contained in:
parent
c1db89161b
commit
ccfc1840a9
@ -1060,25 +1060,17 @@ void CompilerStack::generateEWasm(ContractDefinition const& _contract)
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
// Re-parse the Yul IR in EVM dialect
|
// Re-parse the Yul IR in EVM dialect
|
||||||
yul::AssemblyStack evmStack(m_evmVersion, yul::AssemblyStack::Language::StrictAssembly, m_optimiserSettings);
|
yul::AssemblyStack stack(m_evmVersion, yul::AssemblyStack::Language::StrictAssembly, m_optimiserSettings);
|
||||||
evmStack.parseAndAnalyze("", compiledContract.yulIROptimized);
|
stack.parseAndAnalyze("", compiledContract.yulIROptimized);
|
||||||
|
|
||||||
// Turn into eWasm dialect
|
stack.optimize();
|
||||||
yul::Object ewasmObject = yul::EVMToEWasmTranslator(
|
stack.translate(yul::AssemblyStack::Language::EWasm);
|
||||||
yul::EVMDialect::strictAssemblyForEVMObjects(m_evmVersion)
|
stack.optimize();
|
||||||
).run(*evmStack.parserResult());
|
|
||||||
|
|
||||||
// Re-inject into an assembly stack for the eWasm dialect
|
//cout << yul::AsmPrinter{}(*stack.parserResult()->code) << endl;
|
||||||
yul::AssemblyStack ewasmStack(m_evmVersion, yul::AssemblyStack::Language::EWasm, m_optimiserSettings);
|
|
||||||
// TODO this is a hack for now - provide as structured AST!
|
|
||||||
ewasmStack.parseAndAnalyze("", "{}");
|
|
||||||
*ewasmStack.parserResult() = move(ewasmObject);
|
|
||||||
ewasmStack.optimize();
|
|
||||||
|
|
||||||
//cout << yul::AsmPrinter{}(*ewasmStack.parserResult()->code) << endl;
|
|
||||||
|
|
||||||
// Turn into eWasm text representation.
|
// Turn into eWasm text representation.
|
||||||
auto result = ewasmStack.assemble(yul::AssemblyStack::Machine::eWasm);
|
auto result = stack.assemble(yul::AssemblyStack::Machine::eWasm);
|
||||||
compiledContract.eWasm = std::move(result.assembly);
|
compiledContract.eWasm = std::move(result.assembly);
|
||||||
compiledContract.eWasmObject = std::move(*result.bytecode);
|
compiledContract.eWasmObject = std::move(*result.bytecode);
|
||||||
}
|
}
|
||||||
|
@ -34,6 +34,7 @@
|
|||||||
#include <libyul/backends/evm/EVMMetrics.h>
|
#include <libyul/backends/evm/EVMMetrics.h>
|
||||||
#include <libyul/backends/wasm/WasmDialect.h>
|
#include <libyul/backends/wasm/WasmDialect.h>
|
||||||
#include <libyul/backends/wasm/EWasmObjectCompiler.h>
|
#include <libyul/backends/wasm/EWasmObjectCompiler.h>
|
||||||
|
#include <libyul/backends/wasm/EVMToEWasmTranslator.h>
|
||||||
#include <libyul/optimiser/Metrics.h>
|
#include <libyul/optimiser/Metrics.h>
|
||||||
#include <libyul/ObjectParser.h>
|
#include <libyul/ObjectParser.h>
|
||||||
#include <libyul/optimiser/Suite.h>
|
#include <libyul/optimiser/Suite.h>
|
||||||
@ -102,6 +103,23 @@ void AssemblyStack::optimize()
|
|||||||
solAssert(analyzeParsed(), "Invalid source code after optimization.");
|
solAssert(analyzeParsed(), "Invalid source code after optimization.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void AssemblyStack::translate(AssemblyStack::Language _targetLanguage)
|
||||||
|
{
|
||||||
|
if (m_language == _targetLanguage)
|
||||||
|
return;
|
||||||
|
|
||||||
|
solAssert(
|
||||||
|
m_language == Language::StrictAssembly && _targetLanguage == Language::EWasm,
|
||||||
|
"Invalid language combination"
|
||||||
|
);
|
||||||
|
|
||||||
|
*m_parserResult = EVMToEWasmTranslator(
|
||||||
|
languageToDialect(m_language, m_evmVersion)
|
||||||
|
).run(*parserResult());
|
||||||
|
|
||||||
|
m_language = _targetLanguage;
|
||||||
|
}
|
||||||
|
|
||||||
bool AssemblyStack::analyzeParsed()
|
bool AssemblyStack::analyzeParsed()
|
||||||
{
|
{
|
||||||
solAssert(m_parserResult, "");
|
solAssert(m_parserResult, "");
|
||||||
|
@ -81,6 +81,9 @@ public:
|
|||||||
/// If the settings (see constructor) disabled the optimizer, nothing is done here.
|
/// If the settings (see constructor) disabled the optimizer, nothing is done here.
|
||||||
void optimize();
|
void optimize();
|
||||||
|
|
||||||
|
/// Translate the source to a different language / dialect.
|
||||||
|
void translate(Language _targetLanguage);
|
||||||
|
|
||||||
/// Run the assembly step (should only be called after parseAndAnalyze).
|
/// Run the assembly step (should only be called after parseAndAnalyze).
|
||||||
MachineAssemblyObject assemble(Machine _machine) const;
|
MachineAssemblyObject assemble(Machine _machine) const;
|
||||||
|
|
||||||
|
@ -1364,14 +1364,14 @@ bool CommandLineInterface::assemble(
|
|||||||
for (auto const& sourceAndStack: assemblyStacks)
|
for (auto const& sourceAndStack: assemblyStacks)
|
||||||
{
|
{
|
||||||
auto const& stack = sourceAndStack.second;
|
auto const& stack = sourceAndStack.second;
|
||||||
|
for (auto const& error: stack.errors())
|
||||||
|
{
|
||||||
unique_ptr<SourceReferenceFormatter> formatter;
|
unique_ptr<SourceReferenceFormatter> formatter;
|
||||||
if (m_args.count(g_argNewReporter))
|
if (m_args.count(g_argNewReporter))
|
||||||
formatter = make_unique<SourceReferenceFormatterHuman>(serr(false), m_coloredOutput);
|
formatter = make_unique<SourceReferenceFormatterHuman>(serr(false), m_coloredOutput);
|
||||||
else
|
else
|
||||||
formatter = make_unique<SourceReferenceFormatter>(serr(false));
|
formatter = make_unique<SourceReferenceFormatter>(serr(false));
|
||||||
|
|
||||||
for (auto const& error: stack.errors())
|
|
||||||
{
|
|
||||||
g_hasOutput = true;
|
g_hasOutput = true;
|
||||||
formatter->printErrorInformation(*error);
|
formatter->printErrorInformation(*error);
|
||||||
}
|
}
|
||||||
@ -1384,19 +1384,27 @@ bool CommandLineInterface::assemble(
|
|||||||
|
|
||||||
for (auto const& src: m_sourceCodes)
|
for (auto const& src: m_sourceCodes)
|
||||||
{
|
{
|
||||||
// TODO translate from EVM to eWasm if
|
|
||||||
// _language is EVM and _targetMachine is EWasm
|
|
||||||
|
|
||||||
string machine =
|
string machine =
|
||||||
_targetMachine == yul::AssemblyStack::Machine::EVM ? "EVM" :
|
_targetMachine == yul::AssemblyStack::Machine::EVM ? "EVM" :
|
||||||
_targetMachine == yul::AssemblyStack::Machine::EVM15 ? "EVM 1.5" :
|
_targetMachine == yul::AssemblyStack::Machine::EVM15 ? "EVM 1.5" :
|
||||||
"eWasm";
|
"eWasm";
|
||||||
sout() << endl << "======= " << src.first << " (" << machine << ") =======" << endl;
|
sout() << endl << "======= " << src.first << " (" << machine << ") =======" << endl;
|
||||||
|
|
||||||
yul::AssemblyStack& stack = assemblyStacks[src.first];
|
yul::AssemblyStack& stack = assemblyStacks[src.first];
|
||||||
|
|
||||||
sout() << endl << "Pretty printed source:" << endl;
|
sout() << endl << "Pretty printed source:" << endl;
|
||||||
sout() << stack.print() << endl;
|
sout() << stack.print() << endl;
|
||||||
|
|
||||||
|
if (_language != yul::AssemblyStack::Language::EWasm && _targetMachine == yul::AssemblyStack::Machine::eWasm)
|
||||||
|
{
|
||||||
|
stack.translate(yul::AssemblyStack::Language::EWasm);
|
||||||
|
stack.optimize();
|
||||||
|
|
||||||
|
sout() << endl << "==========================" << endl;
|
||||||
|
sout() << endl << "Translated source:" << endl;
|
||||||
|
sout() << stack.print() << endl;
|
||||||
|
}
|
||||||
|
|
||||||
yul::MachineAssemblyObject object;
|
yul::MachineAssemblyObject object;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user