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/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// 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>
|
2020-10-29 14:00:27 +00:00
|
|
|
#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;
|
|
|
|
|
2019-05-15 21:40:30 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
};
|
|
|
|
|
2020-04-06 12:47:44 +00:00
|
|
|
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
|
2021-09-03 13:55:17 +00:00
|
|
|
/// assembly. Expects all non-literal arguments of the call to be on stack in reverse order
|
|
|
|
/// (i.e. right-most argument pushed first).
|
|
|
|
/// Expects the caller to set the source location.
|
|
|
|
std::function<void(FunctionCall const&, AbstractAssembly&, BuiltinContext&)> generateCode;
|
2018-12-06 23:56:16 +00:00
|
|
|
};
|
|
|
|
|
2019-05-15 21:40:30 +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
|
|
|
|
{
|
2019-05-16 08:56:56 +00:00
|
|
|
/// 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;
|
|
|
|
|
2020-02-18 23:18:13 +00:00
|
|
|
BuiltinFunctionForEVM const* discardFunction(YulString /*_type*/) const override { return builtin("pop"_yulstring); }
|
|
|
|
BuiltinFunctionForEVM const* equalityFunction(YulString /*_type*/) const override { return builtin("eq"_yulstring); }
|
2019-09-11 14:30:47 +00:00
|
|
|
BuiltinFunctionForEVM const* booleanNegationFunction() const override { return builtin("iszero"_yulstring); }
|
2020-09-17 16:00:08 +00:00
|
|
|
BuiltinFunctionForEVM const* memoryStoreFunction(YulString /*_type*/) const override { return builtin("mstore"_yulstring); }
|
|
|
|
BuiltinFunctionForEVM const* memoryLoadFunction(YulString /*_type*/) const override { return builtin("mload"_yulstring); }
|
2020-12-03 17:48:17 +00:00
|
|
|
BuiltinFunctionForEVM const* storageStoreFunction(YulString /*_type*/) const override { return builtin("sstore"_yulstring); }
|
|
|
|
BuiltinFunctionForEVM const* storageLoadFunction(YulString /*_type*/) const override { return builtin("sload"_yulstring); }
|
2021-04-19 10:51:07 +00:00
|
|
|
YulString hashFunction(YulString /*_type*/) const override { return "keccak256"_yulstring; }
|
2019-05-28 10:57:15 +00:00
|
|
|
|
2019-05-16 08:56:56 +00:00
|
|
|
static EVMDialect const& strictAssemblyForEVM(langutil::EVMVersion _version);
|
|
|
|
static EVMDialect const& strictAssemblyForEVMObjects(langutil::EVMVersion _version);
|
2019-02-21 20:56:30 +00:00
|
|
|
|
2019-02-25 14:29:57 +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);
|
2019-08-13 12:40:26 +00:00
|
|
|
|
2019-02-21 09:28:34 +00:00
|
|
|
protected:
|
2021-03-17 18:37:39 +00:00
|
|
|
BuiltinFunctionForEVM const* verbatimFunction(size_t _arguments, size_t _returnVariables) const;
|
|
|
|
|
2019-05-16 08:18:08 +00:00
|
|
|
bool const m_objectAccess;
|
|
|
|
langutil::EVMVersion const m_evmVersion;
|
2018-12-06 23:56:16 +00:00
|
|
|
std::map<YulString, BuiltinFunctionForEVM> m_functions;
|
2021-03-17 18:37:39 +00:00
|
|
|
std::map<std::pair<size_t, size_t>, std::shared_ptr<BuiltinFunctionForEVM const>> mutable m_verbatimFunctions;
|
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);
|
|
|
|
|
2020-02-18 23:18:13 +00:00
|
|
|
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
|
|
|
}
|