2018-12-05 11:02:49 +00:00
|
|
|
/*
|
|
|
|
This file is part of solidity.
|
|
|
|
|
|
|
|
solidity is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
solidity is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <test/libyul/ObjectCompilerTest.h>
|
|
|
|
|
2019-02-11 15:00:24 +00:00
|
|
|
#include <libdevcore/AnsiColorized.h>
|
2018-12-05 11:02:49 +00:00
|
|
|
|
2019-02-13 11:07:20 +00:00
|
|
|
#include <libyul/AssemblyStack.h>
|
2018-12-05 11:02:49 +00:00
|
|
|
|
|
|
|
#include <libevmasm/Instruction.h>
|
|
|
|
|
|
|
|
#include <liblangutil/SourceReferenceFormatter.h>
|
|
|
|
|
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
using namespace dev;
|
|
|
|
using namespace langutil;
|
|
|
|
using namespace yul;
|
|
|
|
using namespace yul::test;
|
|
|
|
using namespace dev::solidity;
|
|
|
|
using namespace dev::solidity::test;
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
ObjectCompilerTest::ObjectCompilerTest(string const& _filename)
|
|
|
|
{
|
|
|
|
boost::filesystem::path path(_filename);
|
|
|
|
|
|
|
|
ifstream file(_filename);
|
|
|
|
if (!file)
|
|
|
|
BOOST_THROW_EXCEPTION(runtime_error("Cannot open test case: \"" + _filename + "\"."));
|
|
|
|
file.exceptions(ios::badbit);
|
|
|
|
|
|
|
|
string line;
|
|
|
|
while (getline(file, line))
|
|
|
|
{
|
|
|
|
if (boost::algorithm::starts_with(line, "// ----"))
|
|
|
|
break;
|
|
|
|
if (m_source.empty() && boost::algorithm::starts_with(line, "// optimize"))
|
|
|
|
m_optimize = true;
|
|
|
|
m_source += line + "\n";
|
|
|
|
}
|
|
|
|
while (getline(file, line))
|
|
|
|
if (boost::algorithm::starts_with(line, "//"))
|
|
|
|
m_expectation += line.substr((line.size() >= 3 && line[2] == ' ') ? 3 : 2) + "\n";
|
|
|
|
else
|
|
|
|
m_expectation += line + "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ObjectCompilerTest::run(ostream& _stream, string const& _linePrefix, bool const _formatted)
|
|
|
|
{
|
2019-03-27 11:49:50 +00:00
|
|
|
AssemblyStack stack(
|
|
|
|
EVMVersion(),
|
|
|
|
AssemblyStack::Language::StrictAssembly,
|
|
|
|
m_optimize ? OptimiserSettings::full() : OptimiserSettings::minimal()
|
|
|
|
);
|
2018-12-05 11:02:49 +00:00
|
|
|
if (!stack.parseAndAnalyze("source", m_source))
|
|
|
|
{
|
2019-02-11 15:00:24 +00:00
|
|
|
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::RED}) << _linePrefix << "Error parsing source." << endl;
|
2018-12-05 11:02:49 +00:00
|
|
|
printErrors(_stream, stack.errors());
|
|
|
|
return false;
|
|
|
|
}
|
2019-03-27 11:49:50 +00:00
|
|
|
stack.optimize();
|
2018-12-05 11:02:49 +00:00
|
|
|
|
2019-03-27 11:49:50 +00:00
|
|
|
MachineAssemblyObject obj = stack.assemble(AssemblyStack::Machine::EVM);
|
2018-12-05 11:02:49 +00:00
|
|
|
solAssert(obj.bytecode, "");
|
|
|
|
|
|
|
|
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: " +
|
2019-03-28 11:47:21 +00:00
|
|
|
boost::trim_copy(dev::eth::disassemble(obj.bytecode->bytecode)) +
|
2018-12-05 11:02:49 +00:00
|
|
|
"\n";
|
|
|
|
|
|
|
|
if (m_expectation != m_obtainedResult)
|
|
|
|
{
|
|
|
|
string nextIndentLevel = _linePrefix + " ";
|
2019-02-11 15:00:24 +00:00
|
|
|
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::CYAN}) << _linePrefix << "Expected result:" << endl;
|
2018-12-05 11:02:49 +00:00
|
|
|
printIndented(_stream, m_expectation, nextIndentLevel);
|
2019-02-11 15:00:24 +00:00
|
|
|
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::CYAN}) << _linePrefix << "Obtained result:" << endl;
|
2018-12-05 11:02:49 +00:00
|
|
|
printIndented(_stream, m_obtainedResult, nextIndentLevel);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjectCompilerTest::printSource(ostream& _stream, string const& _linePrefix, bool const) const
|
|
|
|
{
|
|
|
|
printIndented(_stream, m_source, _linePrefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjectCompilerTest::printUpdatedExpectations(ostream& _stream, string const& _linePrefix) const
|
|
|
|
{
|
|
|
|
printIndented(_stream, m_obtainedResult, _linePrefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjectCompilerTest::printIndented(ostream& _stream, string const& _output, string const& _linePrefix) const
|
|
|
|
{
|
|
|
|
stringstream output(_output);
|
|
|
|
string line;
|
|
|
|
while (getline(output, line))
|
|
|
|
if (line.empty())
|
|
|
|
// Avoid trailing spaces.
|
|
|
|
_stream << boost::trim_right_copy(_linePrefix) << endl;
|
|
|
|
else
|
|
|
|
_stream << _linePrefix << line << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjectCompilerTest::printErrors(ostream& _stream, ErrorList const& _errors)
|
|
|
|
{
|
|
|
|
SourceReferenceFormatter formatter(_stream);
|
|
|
|
|
|
|
|
for (auto const& error: _errors)
|
|
|
|
formatter.printExceptionInformation(
|
|
|
|
*error,
|
|
|
|
(error->type() == Error::Type::Warning) ? "Warning" : "Error"
|
|
|
|
);
|
|
|
|
}
|