solidity/test/tools/yulrun.cpp

170 lines
4.4 KiB
C++
Raw Normal View History

2018-02-07 20:56:14 +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/>.
*/
// SPDX-License-Identifier: GPL-3.0
2018-02-07 20:56:14 +00:00
/**
* Yul interpreter.
*/
#include <test/tools/yulInterpreter/Interpreter.h>
#include <test/tools/yulInterpreter/Inspector.h>
2018-02-07 20:56:14 +00:00
#include <libyul/AsmAnalysisInfo.h>
#include <libyul/AsmAnalysis.h>
#include <libyul/Dialect.h>
#include <libyul/backends/evm/EVMDialect.h>
#include <libyul/YulStack.h>
2018-02-07 20:56:14 +00:00
#include <liblangutil/DebugInfoSelection.h>
2018-02-07 20:56:14 +00:00
#include <liblangutil/Exceptions.h>
#include <liblangutil/EVMVersion.h>
2020-12-01 13:22:15 +00:00
#include <liblangutil/SourceReferenceFormatter.h>
2018-02-07 20:56:14 +00:00
#include <libsolutil/CommonIO.h>
#include <libsolutil/CommonData.h>
#include <libsolutil/Exceptions.h>
2018-02-07 20:56:14 +00:00
#include <boost/program_options.hpp>
#include <string>
#include <memory>
#include <iostream>
using namespace std;
using namespace solidity;
using namespace solidity::util;
using namespace solidity::langutil;
using namespace solidity::yul;
using namespace solidity::yul::test;
2018-02-07 20:56:14 +00:00
namespace po = boost::program_options;
namespace
{
pair<shared_ptr<Block>, shared_ptr<AsmAnalysisInfo>> parse(string const& _source)
{
YulStack stack(
langutil::EVMVersion(),
nullopt,
YulStack::Language::StrictAssembly,
solidity::frontend::OptimiserSettings::none(),
DebugInfoSelection::Default()
);
2018-02-07 20:56:14 +00:00
if (stack.parseAndAnalyze("--INPUT--", _source))
{
yulAssert(stack.errors().empty(), "Parsed successfully but had errors.");
return make_pair(stack.parserResult()->code, stack.parserResult()->analysisInfo);
}
else
{
2021-06-29 12:38:59 +00:00
SourceReferenceFormatter(cout, stack, true, false).printErrorInformation(stack.errors());
2018-02-07 20:56:14 +00:00
return {};
}
}
void interpret(string const& _source, bool _inspect, bool _disableExternalCalls)
2018-02-07 20:56:14 +00:00
{
shared_ptr<Block> ast;
shared_ptr<AsmAnalysisInfo> analysisInfo;
2019-02-26 16:56:52 +00:00
tie(ast, analysisInfo) = parse(_source);
2018-02-07 20:56:14 +00:00
if (!ast || !analysisInfo)
return;
InterpreterState state;
state.maxTraceSize = 10000;
try
{
2020-07-09 12:24:49 +00:00
Dialect const& dialect(EVMDialect::strictAssemblyForEVMObjects(langutil::EVMVersion{}));
if (_inspect)
InspectedInterpreter::run(std::make_shared<Inspector>(_source, state), state, dialect, *ast, _disableExternalCalls, /*disableMemoryTracing=*/false);
else
Interpreter::run(state, dialect, *ast, _disableExternalCalls, /*disableMemoryTracing=*/false);
2018-02-07 20:56:14 +00:00
}
catch (InterpreterTerminatedGeneric const&)
2018-02-07 20:56:14 +00:00
{
}
state.dumpTraceAndState(cout, /*disableMemoryTracing=*/false);
2018-02-07 20:56:14 +00:00
}
}
int main(int argc, char** argv)
{
po::options_description options(
R"(yulrun, the Yul interpreter.
Usage: yulrun [Options] < input
Reads a single source from stdin, runs it and prints a trace of all side-effects.
Allowed options)",
po::options_description::m_default_line_length,
po::options_description::m_default_line_length - 23);
options.add_options()
2019-02-26 16:56:52 +00:00
("help", "Show this help screen.")
("enable-external-calls", "Enable external calls")
("interactive", "Run interactive")
2019-02-26 16:56:52 +00:00
("input-file", po::value<vector<string>>(), "input file");
po::positional_options_description filesPositions;
filesPositions.add("input-file", -1);
2018-02-07 20:56:14 +00:00
po::variables_map arguments;
try
{
po::command_line_parser cmdLineParser(argc, argv);
2019-02-26 16:56:52 +00:00
cmdLineParser.options(options).positional(filesPositions);
2018-02-07 20:56:14 +00:00
po::store(cmdLineParser.run(), arguments);
}
catch (po::error const& _exception)
{
cerr << _exception.what() << endl;
return 1;
}
if (arguments.count("help"))
cout << options;
else
2019-02-26 16:56:52 +00:00
{
string input;
if (arguments.count("input-file"))
for (string path: arguments["input-file"].as<vector<string>>())
{
try
{
input += readFileAsString(path);
}
catch (FileNotFound const&)
{
cerr << "File not found: " << path << endl;
return 1;
}
catch (NotAFile const&)
{
cerr << "Not a regular file: " << path << endl;
return 1;
}
}
2019-02-26 16:56:52 +00:00
else
input = readUntilEnd(cin);
2019-02-26 16:56:52 +00:00
interpret(input, arguments.count("interactive"), !arguments.count("enable-external-calls"));
2019-02-26 16:56:52 +00:00
}
2018-02-07 20:56:14 +00:00
return 0;
}