solidity/libyul/backends/evm/EVMDialect.h

124 lines
4.6 KiB
C
Raw Normal View History

2018-12-06 23:56:16 +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-12-06 23:56:16 +00:00
/**
* Yul dialects for EVM.
*/
#pragma once
#include <libyul/Dialect.h>
#include <libyul/backends/evm/AbstractAssembly.h>
#include <libyul/ASTForward.h>
2019-02-21 20:56:30 +00:00
#include <liblangutil/EVMVersion.h>
2018-12-06 23:56:16 +00:00
#include <map>
2020-07-29 17:42:15 +00:00
#include <set>
2018-12-06 23:56:16 +00:00
2019-12-11 16:31:36 +00:00
namespace solidity::yul
2018-12-06 23:56:16 +00:00
{
class YulString;
using Type = YulString;
struct FunctionCall;
struct Object;
/**
* Context used during code generation.
*/
struct BuiltinContext
{
Object const* currentObject = nullptr;
/// Mapping from named objects to abstract assembly sub IDs.
std::map<YulString, AbstractAssembly::SubID> subIDs;
};
struct BuiltinFunctionForEVM: public BuiltinFunction
2018-12-06 23:56:16 +00:00
{
2019-12-11 16:31:36 +00:00
std::optional<evmasm::Instruction> instruction;
2018-12-06 23:56:16 +00:00
/// Function to generate code for the given function call and append it to the abstract
/// assembly. The fourth parameter is called to visit (and generate code for) the given
/// argument.
std::function<void(FunctionCall const&, AbstractAssembly&, BuiltinContext&, std::function<void(Expression const&)>)> generateCode;
2018-12-06 23:56:16 +00:00
};
2018-12-06 23:56:16 +00:00
/**
* Yul dialect for EVM as a backend.
* The main difference is that the builtin functions take an AbstractAssembly for the
* code generation.
*/
struct EVMDialect: public Dialect
{
/// Constructor, should only be used internally. Use the factory functions below.
2019-12-19 16:58:20 +00:00
EVMDialect(langutil::EVMVersion _evmVersion, bool _objectAccess);
2018-12-06 23:56:16 +00:00
/// @returns the builtin function of the given name or a nullptr if it is not a builtin function.
BuiltinFunctionForEVM const* builtin(YulString _name) const override;
2020-07-29 17:42:15 +00:00
/// @returns true if the identifier is reserved. This includes the builtins too.
bool reservedIdentifier(YulString _name) const override;
BuiltinFunctionForEVM const* discardFunction(YulString /*_type*/) const override { return builtin("pop"_yulstring); }
BuiltinFunctionForEVM const* equalityFunction(YulString /*_type*/) const override { return builtin("eq"_yulstring); }
BuiltinFunctionForEVM const* booleanNegationFunction() const override { return builtin("iszero"_yulstring); }
BuiltinFunctionForEVM const* memoryStoreFunction(YulString /*_type*/) const override { return builtin("mstore"_yulstring); }
BuiltinFunctionForEVM const* memoryLoadFunction(YulString /*_type*/) const override { return builtin("mload"_yulstring); }
BuiltinFunctionForEVM const* storageStoreFunction(YulString /*_type*/) const override { return builtin("sstore"_yulstring); }
BuiltinFunctionForEVM const* storageLoadFunction(YulString /*_type*/) const override { return builtin("sload"_yulstring); }
2019-05-28 10:57:15 +00:00
static EVMDialect const& strictAssemblyForEVM(langutil::EVMVersion _version);
static EVMDialect const& strictAssemblyForEVMObjects(langutil::EVMVersion _version);
2019-02-21 20:56:30 +00:00
langutil::EVMVersion evmVersion() const { return m_evmVersion; }
2018-12-06 23:56:16 +00:00
bool providesObjectAccess() const { return m_objectAccess; }
2019-12-11 16:31:36 +00:00
static SideEffects sideEffectsOfInstruction(evmasm::Instruction _instruction);
protected:
bool const m_objectAccess;
langutil::EVMVersion const m_evmVersion;
2018-12-06 23:56:16 +00:00
std::map<YulString, BuiltinFunctionForEVM> m_functions;
2020-07-29 17:42:15 +00:00
std::set<YulString> m_reserved;
2018-12-06 23:56:16 +00:00
};
2019-12-19 15:01:28 +00:00
/**
* EVM dialect with types u256 (default) and bool.
* Difference to EVMDialect:
* - All comparison functions return type bool
* - bitwise operations are called bitor, bitand, bitxor and bitnot
* - and, or, xor take bool and return bool
2020-01-29 17:45:35 +00:00
* - iszero is replaced by not, which takes bool and returns bool
2019-12-19 15:01:28 +00:00
* - there are conversion functions bool_to_u256 and u256_to_bool.
* - there is popbool
*/
struct EVMDialectTyped: public EVMDialect
{
/// Constructor, should only be used internally. Use the factory function below.
EVMDialectTyped(langutil::EVMVersion _evmVersion, bool _objectAccess);
BuiltinFunctionForEVM const* discardFunction(YulString _type) const override;
BuiltinFunctionForEVM const* equalityFunction(YulString _type) const override;
BuiltinFunctionForEVM const* booleanNegationFunction() const override { return builtin("not"_yulstring); }
2019-12-19 15:01:28 +00:00
static EVMDialectTyped const& instance(langutil::EVMVersion _version);
};
2018-12-06 23:56:16 +00:00
}