Skip to content

Commit

Permalink
Don't ignore output selection in assembly mode
Browse files Browse the repository at this point in the history
  • Loading branch information
cameel committed Oct 14, 2021
1 parent fcd5af8 commit c42266d
Show file tree
Hide file tree
Showing 29 changed files with 211 additions and 16 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Language Features:
Compiler Features:
* Commandline Interface: Accept nested brackets in step sequences passed to ``--yul-optimizations``.
* Commandline Interface: Add ``--debug-info`` option for selecting how much extra debug information should be included in the produced EVM assembly and Yul code.
* Commandline Interface: Support ``--asm``, ``--bin``, ``--ir-optimized`` and ``--ewasm`` output selection options in assembler mode.
* SMTChecker: Output values for ``block.*``, ``msg.*`` and ``tx.*`` variables that are present in the called functions.
* Standard JSON: Accept nested brackets in step sequences passed to ``settings.optimizer.details.yulDetails.optimizerSteps``.
* Standard JSON: Add ``settings.debug.debugInfo`` option for selecting how much extra debug information should be included in the produced EVM assembly and Yul code.
Expand Down
50 changes: 35 additions & 15 deletions solc/CommandLineInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1013,34 +1013,54 @@ bool CommandLineInterface::assemble(yul::AssemblyStack::Language _language, yul:

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

sout() << endl << "Pretty printed source:" << endl;
sout() << stack.print() << endl;
if (m_options.compiler.outputs.irOptimized)
{
// NOTE: This actually outputs unoptimized code when the optimizer is disabled but
// 'ir' output in StandardCompiler works the same way.
sout() << endl << "Pretty printed source:" << 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;
// TODO: This isn't ewasm but it's only present when we're doing Yul->EWASM translation.
// It should get its own output flag in the future.
if (m_options.compiler.outputs.ewasm)
{
sout() << endl << "==========================" << endl;
sout() << endl << "Translated source:" << endl;
sout() << stack.print() << endl;
}
}

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

sout() << endl << "Binary representation:" << endl;
if (object.bytecode)
sout() << object.bytecode->toHex() << endl;
else
serr() << "No binary representation found." << endl;
if (m_options.compiler.outputs.binary)
{
sout() << endl << "Binary representation:" << endl;
if (object.bytecode)
sout() << object.bytecode->toHex() << endl;
else
serr() << "No binary representation found." << endl;
}

sout() << endl << "Text representation:" << endl;
if (!object.assembly.empty())
sout() << object.assembly << endl;
else
serr() << "No text representation found." << endl;
solAssert(_targetMachine == yul::AssemblyStack::Machine::Ewasm || _targetMachine == yul::AssemblyStack::Machine::EVM, "");
if (
(_targetMachine == yul::AssemblyStack::Machine::EVM && m_options.compiler.outputs.asm_) ||
(_targetMachine == yul::AssemblyStack::Machine::Ewasm && m_options.compiler.outputs.ewasm)
)
{
sout() << endl << "Text representation:" << endl;
if (!object.assembly.empty())
sout() << object.assembly << endl;
else
serr() << "No text representation found." << endl;
}
}

return true;
Expand Down
17 changes: 17 additions & 0 deletions solc/CommandLineParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,13 +478,20 @@ bool CommandLineParser::parseOutputSelection()
CompilerOutputs::componentMap() |
ranges::views::keys |
ranges::to<set>();
static set<string> const assemblerModeOutputs = {
CompilerOutputs::componentName(&CompilerOutputs::asm_),
CompilerOutputs::componentName(&CompilerOutputs::binary),
CompilerOutputs::componentName(&CompilerOutputs::irOptimized),
CompilerOutputs::componentName(&CompilerOutputs::ewasm),
};

switch (_mode)
{
case InputMode::Compiler:
case InputMode::CompilerWithASTImport:
return contains(compilerModeOutputs, _outputName);
case InputMode::Assembler:
return contains(assemblerModeOutputs,_outputName);
case InputMode::StandardJson:
case InputMode::Linker:
return false;
Expand All @@ -496,6 +503,16 @@ bool CommandLineParser::parseOutputSelection()
for (auto&& [optionName, outputComponent]: CompilerOutputs::componentMap())
m_options.compiler.outputs.*outputComponent = (m_args.count(optionName) > 0);

if (m_options.input.mode == InputMode::Assembler && m_options.compiler.outputs == CompilerOutputs{})
{
// In assembly mode keep the default outputs enabled for backwards-compatibility.
// TODO: Remove this (must be done in a breaking release).
m_options.compiler.outputs.asm_ = true;
m_options.compiler.outputs.binary = true;
m_options.compiler.outputs.irOptimized = true;
m_options.compiler.outputs.ewasm = true;
}

vector<string> unsupportedOutputs;
for (auto&& [optionName, outputComponent]: CompilerOutputs::componentMap())
if (m_options.compiler.outputs.*outputComponent && !outputSupported(m_options.input.mode, optionName))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--assemble --optimize --yul-dialect evm --machine ewasm --asm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Warning: Yul is still experimental. Please use the output with care.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
let x := 42
sstore(0, x)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

======= evm_to_wasm_output_selection_asm_only/input.yul (Ewasm) =======
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--assemble --optimize --yul-dialect evm --machine ewasm --ewasm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Warning: Yul is still experimental. Please use the output with care.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
let x := 42
sstore(0, x)
}
87 changes: 87 additions & 0 deletions test/cmdlineTests/evm_to_wasm_output_selection_ewasm_only/output
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@

======= evm_to_wasm_output_selection_ewasm_only/input.yul (Ewasm) =======

==========================

Translated source:
object "object" {
code {
function main()
{
let hi := i64.shl(i64.extend_i32_u(bswap32(i32.wrap_i64(0))), 32)
let y := i64.or(hi, i64.extend_i32_u(bswap32(i32.wrap_i64(i64.shr_u(0, 32)))))
i64.store(0:i32, y)
i64.store(i32.add(0:i32, 8:i32), y)
i64.store(i32.add(0:i32, 16:i32), y)
i64.store(i32.add(0:i32, 24:i32), y)
i64.store(32:i32, y)
i64.store(i32.add(32:i32, 8:i32), y)
i64.store(i32.add(32:i32, 16:i32), y)
let hi_1 := i64.shl(i64.extend_i32_u(bswap32(i32.wrap_i64(42))), 32)
i64.store(i32.add(32:i32, 24:i32), i64.or(hi_1, i64.extend_i32_u(bswap32(i32.wrap_i64(i64.shr_u(42, 32))))))
eth.storageStore(0:i32, 32:i32)
}
function bswap16(x:i32) -> y:i32
{
y := i32.or(i32.and(i32.shl(x, 8:i32), 0xff00:i32), i32.and(i32.shr_u(x, 8:i32), 0xff:i32))
}
function bswap32(x:i32) -> y:i32
{
let hi:i32 := i32.shl(bswap16(x), 16:i32)
y := i32.or(hi, bswap16(i32.shr_u(x, 16:i32)))
}
}
}


Text representation:
(module
(import "ethereum" "storageStore" (func $eth.storageStore (param i32 i32)))
(memory $memory (export "memory") 1)
(export "main" (func $main))

(func $main
(local $hi i64)
(local $y i64)
(local $hi_1 i64)
(block $label_
(local.set $hi (i64.shl (i64.extend_i32_u (call $bswap32 (i32.wrap_i64 (i64.const 0)))) (i64.const 32)))
(local.set $y (i64.or (local.get $hi) (i64.extend_i32_u (call $bswap32 (i32.wrap_i64 (i64.shr_u (i64.const 0) (i64.const 32)))))))
(i64.store (i32.const 0) (local.get $y))
(i64.store (i32.add (i32.const 0) (i32.const 8)) (local.get $y))
(i64.store (i32.add (i32.const 0) (i32.const 16)) (local.get $y))
(i64.store (i32.add (i32.const 0) (i32.const 24)) (local.get $y))
(i64.store (i32.const 32) (local.get $y))
(i64.store (i32.add (i32.const 32) (i32.const 8)) (local.get $y))
(i64.store (i32.add (i32.const 32) (i32.const 16)) (local.get $y))
(local.set $hi_1 (i64.shl (i64.extend_i32_u (call $bswap32 (i32.wrap_i64 (i64.const 42)))) (i64.const 32)))
(i64.store (i32.add (i32.const 32) (i32.const 24)) (i64.or (local.get $hi_1) (i64.extend_i32_u (call $bswap32 (i32.wrap_i64 (i64.shr_u (i64.const 42) (i64.const 32)))))))
(call $eth.storageStore (i32.const 0) (i32.const 32))
)
)

(func $bswap16
(param $x i32)
(result i32)
(local $y i32)
(block $label__1
(local.set $y (i32.or (i32.and (i32.shl (local.get $x) (i32.const 8)) (i32.const 65280)) (i32.and (i32.shr_u (local.get $x) (i32.const 8)) (i32.const 255))))

)
(local.get $y)
)

(func $bswap32
(param $x i32)
(result i32)
(local $y i32)
(local $hi i32)
(block $label__2
(local.set $hi (i32.shl (call $bswap16 (local.get $x)) (i32.const 16)))
(local.set $y (i32.or (local.get $hi) (call $bswap16 (i32.shr_u (local.get $x) (i32.const 16)))))

)
(local.get $y)
)

)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--strict-assembly --optimize --asm
1 change: 1 addition & 0 deletions test/cmdlineTests/strict_asm_output_selection_asm_only/err
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Warning: Yul is still experimental. Please use the output with care.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
let x := 42
sstore(0, x)
}
10 changes: 10 additions & 0 deletions test/cmdlineTests/strict_asm_output_selection_asm_only/output
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

======= strict_asm_output_selection_asm_only/input.yul (EVM) =======

Text representation:
/* "strict_asm_output_selection_asm_only/input.yul":15:17 */
0x2a
/* "strict_asm_output_selection_asm_only/input.yul":29:30 */
0x00
/* "strict_asm_output_selection_asm_only/input.yul":22:34 */
sstore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--strict-assembly --optimize --bin
1 change: 1 addition & 0 deletions test/cmdlineTests/strict_asm_output_selection_bin_only/err
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Warning: Yul is still experimental. Please use the output with care.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
let x := 42
sstore(0, x)
}
5 changes: 5 additions & 0 deletions test/cmdlineTests/strict_asm_output_selection_bin_only/output
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

======= strict_asm_output_selection_bin_only/input.yul (EVM) =======

Binary representation:
602a600055
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--strict-assembly --optimize --ewasm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Warning: Yul is still experimental. Please use the output with care.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
let x := 42
sstore(0, x)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

======= strict_asm_output_selection_ewasm_only/input.yul (EVM) =======
2 changes: 1 addition & 1 deletion test/cmdlineTests/strict_asm_output_selection_invalid/err
Original file line number Diff line number Diff line change
@@ -1 +1 @@
The following outputs are not supported in assembler mode: --abi, --asm, --asm-json, --bin, --bin-runtime, --devdoc, --ewasm, --hashes, --ir, --ir-optimized, --metadata, --opcodes, --storage-layout, --userdoc.
The following outputs are not supported in assembler mode: --abi, --asm-json, --bin-runtime, --devdoc, --hashes, --ir, --metadata, --opcodes, --storage-layout, --userdoc.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--strict-assembly --optimize --ir-optimized
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Warning: Yul is still experimental. Please use the output with care.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
let x := 42
sstore(0, x)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

======= strict_asm_output_selection_ir_optimized_only/input.yul (EVM) =======

Pretty printed source:
object "object" {
code { { sstore(0, 42) } }
}
8 changes: 8 additions & 0 deletions test/solc/CommandLineParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,10 @@ BOOST_AUTO_TEST_CASE(assembly_mode_options)
"underflow,"
"divByZero",
"--model-checker-timeout=5", // Ignored in assembly mode
"--asm",
"--bin",
"--ir-optimized",
"--ewasm",
};
commandLine += assemblyOptions;
if (expectedLanguage == AssemblyStack::Language::StrictAssembly || expectedLanguage == AssemblyStack::Language::Ewasm)
Expand Down Expand Up @@ -328,6 +332,10 @@ BOOST_AUTO_TEST_CASE(assembly_mode_options)
};
expectedOptions.formatting.coloredOutput = false;
expectedOptions.formatting.withErrorIds = true;
expectedOptions.compiler.outputs.asm_ = true;
expectedOptions.compiler.outputs.binary = true;
expectedOptions.compiler.outputs.irOptimized = true;
expectedOptions.compiler.outputs.ewasm = true;
if (expectedLanguage == AssemblyStack::Language::StrictAssembly || expectedLanguage == AssemblyStack::Language::Ewasm)
{
expectedOptions.optimizer.enabled = true;
Expand Down

0 comments on commit c42266d

Please sign in to comment.