#pragma once
#include <liblangutil/CharStreamProvider.h>
#include <liblangutil/DebugInfoSelection.h>
#include <liblangutil/ErrorReporter.h>
#include <liblangutil/EVMVersion.h>
#include <libyul/Object.h>
#include <libyul/ObjectParser.h>
#include <libsolidity/interface/OptimiserSettings.h>
#include <libevmasm/LinkerObject.h>
#include <memory>
#include <string>
namespace solidity::evmasm
{
class Assembly;
}
namespace solidity::langutil
{
class Scanner;
}
namespace solidity::yul
{
class AbstractAssembly;
struct MachineAssemblyObject
{
std::shared_ptr<evmasm::LinkerObject> bytecode;
std::string assembly;
std::unique_ptr<std::string> sourceMappings;
};
class YulStack: public langutil::CharStreamProvider
{
public:
enum class Language { Yul, Assembly, StrictAssembly, Ewasm };
enum class Machine { EVM, Ewasm };
YulStack():
YulStack(
langutil::EVMVersion{},
Language::Assembly,
solidity::frontend::OptimiserSettings::none(),
langutil::DebugInfoSelection::Default()
)
{}
YulStack(
langutil::EVMVersion _evmVersion,
Language _language,
solidity::frontend::OptimiserSettings _optimiserSettings,
langutil::DebugInfoSelection const& _debugInfoSelection
):
m_language(_language),
m_evmVersion(_evmVersion),
m_optimiserSettings(std::move(_optimiserSettings)),
m_debugInfoSelection(_debugInfoSelection),
m_errorReporter(m_errors)
{}
langutil::CharStream const& charStream(std::string const& _sourceName) const override;
bool parseAndAnalyze(std::string const& _sourceName, std::string const& _source);
void optimize();
void translate(Language _targetLanguage);
MachineAssemblyObject assemble(Machine _machine) const;
std::pair<MachineAssemblyObject, MachineAssemblyObject>
assembleWithDeployed(
std::optional<std::string_view> _deployName = {}
) const;
std::pair<std::shared_ptr<evmasm::Assembly>, std::shared_ptr<evmasm::Assembly>>
assembleEVMWithDeployed(
std::optional<std::string_view> _deployName = {}
) const;
langutil::ErrorList const& errors() const { return m_errors; }
std::string print(
langutil::CharStreamProvider const* _soliditySourceProvider = nullptr
) const;
std::shared_ptr<Object> parserResult() const;
private:
bool analyzeParsed();
bool analyzeParsed(yul::Object& _object);
void compileEVM(yul::AbstractAssembly& _assembly, bool _optimize) const;
void optimize(yul::Object& _object, bool _isCreation);
Language m_language = Language::Assembly;
langutil::EVMVersion m_evmVersion;
solidity::frontend::OptimiserSettings m_optimiserSettings;
langutil::DebugInfoSelection m_debugInfoSelection{};
std::unique_ptr<langutil::CharStream> m_charStream;
bool m_analysisSuccessful = false;
std::shared_ptr<yul::Object> m_parserResult;
langutil::ErrorList m_errors;
langutil::ErrorReporter m_errorReporter;
std::unique_ptr<std::string> m_sourceMappings;
};
}