Add wasm support to libyul/ObjectCompilerTest

This commit is contained in:
Alex Beregszaszi 2020-11-09 21:04:00 +00:00 committed by Kamil Śliwak
parent d3a016b597
commit e5396e42c3
6 changed files with 168 additions and 15 deletions

View File

@ -44,6 +44,7 @@ ObjectCompilerTest::ObjectCompilerTest(string const& _filename):
{
m_source = m_reader.source();
m_optimize = m_reader.boolSetting("optimize", false);
m_wasm = m_reader.boolSetting("wasm", false);
m_expectation = m_reader.simpleExpectations();
}
@ -51,7 +52,7 @@ TestCase::TestResult ObjectCompilerTest::run(ostream& _stream, string const& _li
{
AssemblyStack stack(
EVMVersion(),
AssemblyStack::Language::StrictAssembly,
m_wasm ? AssemblyStack::Language::Ewasm : AssemblyStack::Language::StrictAssembly,
m_optimize ? OptimiserSettings::full() : OptimiserSettings::minimal()
);
if (!stack.parseAndAnalyze("source", m_source))
@ -62,22 +63,33 @@ TestCase::TestResult ObjectCompilerTest::run(ostream& _stream, string const& _li
}
stack.optimize();
MachineAssemblyObject obj = stack.assemble(AssemblyStack::Machine::EVM);
solAssert(obj.bytecode, "");
solAssert(obj.sourceMappings, "");
if (m_wasm)
{
MachineAssemblyObject obj = stack.assemble(AssemblyStack::Machine::Ewasm);
solAssert(obj.bytecode, "");
m_obtainedResult = "Assembly:\n" + obj.assembly;
if (obj.bytecode->bytecode.empty())
m_obtainedResult += "-- empty bytecode --\n";
m_obtainedResult = "Text:\n" + obj.assembly + "\n";
m_obtainedResult += "Binary:\n" + toHex(obj.bytecode->bytecode) + "\n";
}
else
m_obtainedResult +=
"Bytecode: " +
toHex(obj.bytecode->bytecode) +
"\nOpcodes: " +
boost::trim_copy(evmasm::disassemble(obj.bytecode->bytecode)) +
"\nSourceMappings:" +
(obj.sourceMappings->empty() ? "" : " " + *obj.sourceMappings) +
"\n";
{
MachineAssemblyObject obj = stack.assemble(AssemblyStack::Machine::EVM);
solAssert(obj.bytecode, "");
solAssert(obj.sourceMappings, "");
m_obtainedResult = "Assembly:\n" + obj.assembly;
if (obj.bytecode->bytecode.empty())
m_obtainedResult += "-- empty bytecode --\n";
else
m_obtainedResult +=
"Bytecode: " +
toHex(obj.bytecode->bytecode) +
"\nOpcodes: " +
boost::trim_copy(evmasm::disassemble(obj.bytecode->bytecode)) +
"\nSourceMappings:" +
(obj.sourceMappings->empty() ? "" : " " + *obj.sourceMappings) +
"\n";
}
return checkResult(_stream, _linePrefix, _formatted);
}

View File

@ -55,6 +55,7 @@ private:
static void printErrors(std::ostream& _stream, langutil::ErrorList const& _errors);
bool m_optimize = false;
bool m_wasm = false;
};
}

View File

@ -0,0 +1,22 @@
{
function not_main() {
i64.drop(i64.add(0, 1))
}
}
// ====
// wasm: true
// ----
// Text:
// (module
// (memory $memory (export "memory") 1)
//
// (func $not_main
// (block $label_
// (drop (i64.add (i64.const 0) (i64.const 1)))
// )
// )
//
// )
//
// Binary:
// 0061736d01000000010401600000020100030201000503010001060100070a01066d656d6f727902000a0801060002401a0b0b

View File

@ -0,0 +1,23 @@
{
function main() {
i64.drop(i64.add(0, 1))
}
}
// ====
// wasm: true
// ----
// Text:
// (module
// (memory $memory (export "memory") 1)
// (export "main" (func $main))
//
// (func $main
// (block $label_
// (drop (i64.add (i64.const 0) (i64.const 1)))
// )
// )
//
// )
//
// Binary:
// 0061736d01000000010401600000020100030201000503010001060100071102066d656d6f72790200046d61696e00000a0801060002401a0b0b

View File

@ -0,0 +1,19 @@
object "a" {
code {}
// Unreferenced data is not added to the assembled bytecode.
data "str" "Hello, World!"
object "sub" { code { function main() { i64.drop(11) } } }
}
// ====
// wasm: true
// ----
// Text:
// (module
// ;; sub-module "sub" will be encoded as custom section in binary here, but is skipped in text mode.
// ;; custom-section "str" will be encoded as custom section in binary here, but is skipped in text mode.
// (memory $memory (export "memory") 1)
//
// )
//
// Binary:
// 0061736d010000000101000201000301000503010001060100070a01066d656d6f72790200003e037375620061736d01000000010401600000020100030201000503010001060100071102066d656d6f72790200046d61696e00000a0801060002401a0b0b00110373747248656c6c6f2c20576f726c64210a0100

View File

@ -0,0 +1,76 @@
object "A" {
code {
function main() {
// TODO: support this
// i64.drop(dataoffset("A"))
// i64.drop(datasize("A"))
i64.drop(dataoffset("B"))
i64.drop(datasize("B"))
// TODO: support sub-subobjects
// i64.drop(dataoffset("B.C"))
// i64.drop(datasize("B.C"))
// i64.drop(dataoffset("B.E"))
// i64.drop(datasize("B.E"))
// i64.drop(dataoffset("B.C.D"))
// i64.drop(datasize("B.C.D"))
}
}
data "data1" "Hello, World!"
object "B" {
code {
function main() {
i64.drop(dataoffset("C"))
i64.drop(datasize("C"))
i64.drop(dataoffset("E"))
i64.drop(datasize("E"))
// i64.drop(dataoffset("C.D"))
// i64.drop(datasize("C.D"))
}
}
object "C" {
code {
function main() {
i64.drop(dataoffset("D"))
i64.drop(datasize("D"))
}
}
object "D" {
code {
function main() {
unreachable()
}
}
}
}
object "E" {
code {
function main() {
unreachable()
}
}
}
}
}
// ====
// wasm: true
// ----
// Text:
// (module
// ;; sub-module "B" will be encoded as custom section in binary here, but is skipped in text mode.
// ;; custom-section "data1" will be encoded as custom section in binary here, but is skipped in text mode.
// (memory $memory (export "memory") 1)
// (export "main" (func $main))
//
// (func $main
// (block $label_
// (drop (dataoffset "B"))
// (drop (datasize "B"))
// )
// )
//
// )
//
// Binary:
// 0061736d01000000010401600000020100030201000503010001060100071102066d656d6f72790200046d61696e000000fa0101420061736d01000000010401600000020100030201000503010001060100071102066d656d6f72790200046d61696e0000007b01430061736d01000000010401600000020100030201000503010001060100071102066d656d6f72790200046d61696e0000003c01440061736d01000000010401600000020100030201000503010001060100071102066d656d6f72790200046d61696e00000a080106000240000b0b0a0901070002401a1a0b0b003c01450061736d01000000010401600000020100030201000503010001060100071102066d656d6f72790200046d61696e00000a080106000240000b0b0a0b01090002401a1a1a1a0b0b001305646174613148656c6c6f2c20576f726c64210a0901070002401a1a0b0b