Skip to content

Commit 44f7065

Browse files
authored
Merge pull request #12074 from ethereum/output-selection-in-assembler-mode
CLI output selection in assembler mode
2 parents 4f87193 + 1a19d9a commit 44f7065

File tree

44 files changed

+249
-21
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+249
-21
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions

solc/CommandLineInterface.cpp

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,34 +1042,52 @@ bool CommandLineInterface::assemble(yul::AssemblyStack::Language _language, yul:
10421042

10431043
yul::AssemblyStack& stack = assemblyStacks[src.first];
10441044

1045-
sout() << endl << "Pretty printed source:" << endl;
1046-
sout() << stack.print() << endl;
1045+
if (m_options.compiler.outputs.irOptimized)
1046+
{
1047+
// NOTE: This actually outputs unoptimized code when the optimizer is disabled but
1048+
// 'ir' output in StandardCompiler works the same way.
1049+
sout() << endl << "Pretty printed source:" << endl;
1050+
sout() << stack.print() << endl;
1051+
}
10471052

10481053
if (_language != yul::AssemblyStack::Language::Ewasm && _targetMachine == yul::AssemblyStack::Machine::Ewasm)
10491054
{
10501055
stack.translate(yul::AssemblyStack::Language::Ewasm);
10511056
stack.optimize();
10521057

1053-
sout() << endl << "==========================" << endl;
1054-
sout() << endl << "Translated source:" << endl;
1055-
sout() << stack.print() << endl;
1058+
if (m_options.compiler.outputs.ewasmIR)
1059+
{
1060+
sout() << endl << "==========================" << endl;
1061+
sout() << endl << "Translated source:" << endl;
1062+
sout() << stack.print() << endl;
1063+
}
10561064
}
10571065

10581066
yul::MachineAssemblyObject object;
10591067
object = stack.assemble(_targetMachine);
10601068
object.bytecode->link(m_options.linker.libraries);
10611069

1062-
sout() << endl << "Binary representation:" << endl;
1063-
if (object.bytecode)
1064-
sout() << object.bytecode->toHex() << endl;
1065-
else
1066-
serr() << "No binary representation found." << endl;
1070+
if (m_options.compiler.outputs.binary)
1071+
{
1072+
sout() << endl << "Binary representation:" << endl;
1073+
if (object.bytecode)
1074+
sout() << object.bytecode->toHex() << endl;
1075+
else
1076+
serr() << "No binary representation found." << endl;
1077+
}
10671078

1068-
sout() << endl << "Text representation:" << endl;
1069-
if (!object.assembly.empty())
1070-
sout() << object.assembly << endl;
1071-
else
1072-
serr() << "No text representation found." << endl;
1079+
solAssert(_targetMachine == yul::AssemblyStack::Machine::Ewasm || _targetMachine == yul::AssemblyStack::Machine::EVM, "");
1080+
if (
1081+
(_targetMachine == yul::AssemblyStack::Machine::EVM && m_options.compiler.outputs.asm_) ||
1082+
(_targetMachine == yul::AssemblyStack::Machine::Ewasm && m_options.compiler.outputs.ewasm)
1083+
)
1084+
{
1085+
sout() << endl << "Text representation:" << endl;
1086+
if (!object.assembly.empty())
1087+
sout() << object.assembly << endl;
1088+
else
1089+
serr() << "No text representation found." << endl;
1090+
}
10731091
}
10741092

10751093
return true;

solc/CommandLineParser.cpp

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -446,10 +446,18 @@ bool CommandLineParser::parseOutputSelection()
446446
{
447447
static auto outputSupported = [](InputMode _mode, string_view _outputName)
448448
{
449-
static set<string> const compilerModeOutputs =
449+
static set<string> const compilerModeOutputs = (
450450
CompilerOutputs::componentMap() |
451451
ranges::views::keys |
452-
ranges::to<set>();
452+
ranges::to<set>()
453+
) - set<string>{CompilerOutputs::componentName(&CompilerOutputs::ewasmIR)};
454+
static set<string> const assemblerModeOutputs = {
455+
CompilerOutputs::componentName(&CompilerOutputs::asm_),
456+
CompilerOutputs::componentName(&CompilerOutputs::binary),
457+
CompilerOutputs::componentName(&CompilerOutputs::irOptimized),
458+
CompilerOutputs::componentName(&CompilerOutputs::ewasm),
459+
CompilerOutputs::componentName(&CompilerOutputs::ewasmIR),
460+
};
453461

454462
switch (_mode)
455463
{
@@ -461,6 +469,7 @@ bool CommandLineParser::parseOutputSelection()
461469
case InputMode::CompilerWithASTImport:
462470
return contains(compilerModeOutputs, _outputName);
463471
case InputMode::Assembler:
472+
return contains(assemblerModeOutputs, _outputName);
464473
case InputMode::StandardJson:
465474
case InputMode::Linker:
466475
return false;
@@ -472,6 +481,17 @@ bool CommandLineParser::parseOutputSelection()
472481
for (auto&& [optionName, outputComponent]: CompilerOutputs::componentMap())
473482
m_options.compiler.outputs.*outputComponent = (m_args.count(optionName) > 0);
474483

484+
if (m_options.input.mode == InputMode::Assembler && m_options.compiler.outputs == CompilerOutputs{})
485+
{
486+
// In assembly mode keep the default outputs enabled for backwards-compatibility.
487+
// TODO: Remove this (must be done in a breaking release).
488+
m_options.compiler.outputs.asm_ = true;
489+
m_options.compiler.outputs.binary = true;
490+
m_options.compiler.outputs.irOptimized = true;
491+
m_options.compiler.outputs.ewasm = true;
492+
m_options.compiler.outputs.ewasmIR = true;
493+
}
494+
475495
vector<string> unsupportedOutputs;
476496
for (auto&& [optionName, outputComponent]: CompilerOutputs::componentMap())
477497
if (m_options.compiler.outputs.*outputComponent && !outputSupported(m_options.input.mode, optionName))
@@ -692,6 +712,7 @@ General Information)").c_str(),
692712
(CompilerOutputs::componentName(&CompilerOutputs::ir).c_str(), "Intermediate Representation (IR) of all contracts (EXPERIMENTAL).")
693713
(CompilerOutputs::componentName(&CompilerOutputs::irOptimized).c_str(), "Optimized intermediate Representation (IR) of all contracts (EXPERIMENTAL).")
694714
(CompilerOutputs::componentName(&CompilerOutputs::ewasm).c_str(), "Ewasm text representation of all contracts (EXPERIMENTAL).")
715+
(CompilerOutputs::componentName(&CompilerOutputs::ewasmIR).c_str(), "Intermediate representation (IR) converted to a form that can be translated directly into Ewasm text representation (EXPERIMENTAL).")
695716
(CompilerOutputs::componentName(&CompilerOutputs::signatureHashes).c_str(), "Function signature hashes of the contracts.")
696717
(CompilerOutputs::componentName(&CompilerOutputs::natspecUser).c_str(), "Natspec user documentation of all contracts.")
697718
(CompilerOutputs::componentName(&CompilerOutputs::natspecDev).c_str(), "Natspec developer documentation of all contracts.")
@@ -906,11 +927,12 @@ bool CommandLineParser::processArgs()
906927
if (!checkMutuallyExclusive({g_strColor, g_strNoColor}))
907928
return false;
908929

909-
array<string, 8> const conflictingWithStopAfter{
930+
array<string, 9> const conflictingWithStopAfter{
910931
CompilerOutputs::componentName(&CompilerOutputs::binary),
911932
CompilerOutputs::componentName(&CompilerOutputs::ir),
912933
CompilerOutputs::componentName(&CompilerOutputs::irOptimized),
913934
CompilerOutputs::componentName(&CompilerOutputs::ewasm),
935+
CompilerOutputs::componentName(&CompilerOutputs::ewasmIR),
914936
g_strGas,
915937
CompilerOutputs::componentName(&CompilerOutputs::asm_),
916938
CompilerOutputs::componentName(&CompilerOutputs::asmJson),

solc/CommandLineParser.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ struct CompilerOutputs
7878
{"ir", &CompilerOutputs::ir},
7979
{"ir-optimized", &CompilerOutputs::irOptimized},
8080
{"ewasm", &CompilerOutputs::ewasm},
81+
{"ewasm-ir", &CompilerOutputs::ewasmIR},
8182
{"hashes", &CompilerOutputs::signatureHashes},
8283
{"userdoc", &CompilerOutputs::natspecUser},
8384
{"devdoc", &CompilerOutputs::natspecDev},
@@ -97,6 +98,7 @@ struct CompilerOutputs
9798
bool ir = false;
9899
bool irOptimized = false;
99100
bool ewasm = false;
101+
bool ewasmIR = false;
100102
bool signatureHashes = false;
101103
bool natspecUser = false;
102104
bool natspecDev = false;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--assemble --optimize --yul-dialect evm --machine ewasm --asm
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Warning: Yul is still experimental. Please use the output with care.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
let x := 42
3+
sstore(0, x)
4+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
======= evm_to_wasm_output_selection_asm_only/input.yul (Ewasm) =======
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--assemble --optimize --yul-dialect evm --machine ewasm --ewasm-ir
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Warning: Yul is still experimental. Please use the output with care.

0 commit comments

Comments
 (0)