mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Add setimmutable and loadimmutable to dialect.
This commit is contained in:
parent
53ea962864
commit
debcc8c056
@ -306,11 +306,15 @@ vector<YulString> AsmAnalyzer::operator()(FunctionCall const& _funCall)
|
||||
_funCall.functionName.location,
|
||||
"Function expects direct literals as arguments."
|
||||
);
|
||||
else if (!m_dataNames.count(std::get<Literal>(arg).value))
|
||||
typeError(
|
||||
_funCall.functionName.location,
|
||||
"Unknown data object \"" + std::get<Literal>(arg).value.str() + "\"."
|
||||
);
|
||||
else if (
|
||||
_funCall.functionName.name.str() == "datasize" ||
|
||||
_funCall.functionName.name.str() == "dataoffset"
|
||||
)
|
||||
if (!m_dataNames.count(std::get<Literal>(arg).value))
|
||||
typeError(
|
||||
_funCall.functionName.location,
|
||||
"Unknown data object \"" + std::get<Literal>(arg).value.str() + "\"."
|
||||
);
|
||||
}
|
||||
}
|
||||
std::reverse(argTypes.begin(), argTypes.end());
|
||||
|
@ -105,6 +105,11 @@ public:
|
||||
virtual void appendDataSize(SubID _sub) = 0;
|
||||
/// Appends the given data to the assembly and returns its ID.
|
||||
virtual SubID appendData(bytes const& _data) = 0;
|
||||
|
||||
/// Appends loading an immutable variable.
|
||||
virtual void appendImmutable(std::string const& _identifier) = 0;
|
||||
/// Appends an assignment to an immutable variable.
|
||||
virtual void appendImmutableAssignment(std::string const& _identifier) = 0;
|
||||
};
|
||||
|
||||
enum class IdentifierContext { LValue, RValue, VariableDeclaration };
|
||||
|
@ -172,6 +172,16 @@ AbstractAssembly::SubID EthAssemblyAdapter::appendData(bytes const& _data)
|
||||
return subID;
|
||||
}
|
||||
|
||||
void EthAssemblyAdapter::appendImmutable(std::string const& _identifier)
|
||||
{
|
||||
m_assembly.appendImmutable(_identifier);
|
||||
}
|
||||
|
||||
void EthAssemblyAdapter::appendImmutableAssignment(std::string const& _identifier)
|
||||
{
|
||||
m_assembly.appendImmutableAssignment(_identifier);
|
||||
}
|
||||
|
||||
EthAssemblyAdapter::LabelID EthAssemblyAdapter::assemblyTagToIdentifier(evmasm::AssemblyItem const& _tag)
|
||||
{
|
||||
u256 id = _tag.data();
|
||||
|
@ -61,6 +61,9 @@ public:
|
||||
void appendDataSize(SubID _sub) override;
|
||||
SubID appendData(bytes const& _data) override;
|
||||
|
||||
void appendImmutable(std::string const& _identifier) override;
|
||||
void appendImmutableAssignment(std::string const& _identifier) override;
|
||||
|
||||
private:
|
||||
static LabelID assemblyTagToIdentifier(evmasm::AssemblyItem const& _tag);
|
||||
|
||||
|
@ -215,6 +215,16 @@ AbstractAssembly::SubID EVMAssembly::appendData(bytes const&)
|
||||
yulAssert(false, "Data not implemented.");
|
||||
}
|
||||
|
||||
void EVMAssembly::appendImmutable(std::string const&)
|
||||
{
|
||||
yulAssert(false, "loadimmutable not implemented.");
|
||||
}
|
||||
|
||||
void EVMAssembly::appendImmutableAssignment(std::string const&)
|
||||
{
|
||||
yulAssert(false, "setimmutable not implemented.");
|
||||
}
|
||||
|
||||
void EVMAssembly::updateReference(size_t pos, size_t size, u256 value)
|
||||
{
|
||||
yulAssert(m_bytecode.size() >= size && pos <= m_bytecode.size() - size, "");
|
||||
|
@ -83,6 +83,9 @@ public:
|
||||
void appendDataSize(SubID _sub) override;
|
||||
SubID appendData(bytes const& _data) override;
|
||||
|
||||
void appendImmutable(std::string const& _identifier) override;
|
||||
void appendImmutableAssignment(std::string const& _identifier) override;
|
||||
|
||||
/// Resolves references inside the bytecode and returns the linker object.
|
||||
evmasm::LinkerObject finalize();
|
||||
|
||||
|
@ -257,13 +257,9 @@ void CodeTransform::operator()(FunctionCall const& _call)
|
||||
yulAssert(m_scope, "");
|
||||
|
||||
if (BuiltinFunctionForEVM const* builtin = m_dialect.builtin(_call.functionName.name))
|
||||
{
|
||||
builtin->generateCode(_call, m_assembly, m_builtinContext, [&]() {
|
||||
for (auto const& arg: _call.arguments | boost::adaptors::reversed)
|
||||
visitExpression(arg);
|
||||
m_assembly.setSourceLocation(_call.location);
|
||||
builtin->generateCode(_call, m_assembly, m_builtinContext, [&](Expression const& _expression) {
|
||||
visitExpression(_expression);
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
m_assembly.setSourceLocation(_call.location);
|
||||
|
@ -41,6 +41,20 @@ using namespace solidity::util;
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
void visitArguments(
|
||||
AbstractAssembly& _assembly,
|
||||
FunctionCall const& _call,
|
||||
function<void(Expression const&)> _visitExpression
|
||||
)
|
||||
{
|
||||
for (auto const& arg: _call.arguments | boost::adaptors::reversed)
|
||||
_visitExpression(arg);
|
||||
|
||||
_assembly.setSourceLocation(_call.location);
|
||||
}
|
||||
|
||||
|
||||
pair<YulString, BuiltinFunctionForEVM> createEVMFunction(
|
||||
string const& _name,
|
||||
evmasm::Instruction _instruction
|
||||
@ -58,12 +72,12 @@ pair<YulString, BuiltinFunctionForEVM> createEVMFunction(
|
||||
f.literalArguments.reset();
|
||||
f.instruction = _instruction;
|
||||
f.generateCode = [_instruction](
|
||||
FunctionCall const&,
|
||||
FunctionCall const& _call,
|
||||
AbstractAssembly& _assembly,
|
||||
BuiltinContext&,
|
||||
std::function<void()> _visitArguments
|
||||
std::function<void(Expression const&)> _visitExpression
|
||||
) {
|
||||
_visitArguments();
|
||||
visitArguments(_assembly, _call, _visitExpression);
|
||||
_assembly.appendInstruction(_instruction);
|
||||
};
|
||||
|
||||
@ -76,7 +90,7 @@ pair<YulString, BuiltinFunctionForEVM> createFunction(
|
||||
size_t _returns,
|
||||
SideEffects _sideEffects,
|
||||
vector<bool> _literalArguments,
|
||||
std::function<void(FunctionCall const&, AbstractAssembly&, BuiltinContext&, std::function<void()>)> _generateCode
|
||||
std::function<void(FunctionCall const&, AbstractAssembly&, BuiltinContext&, std::function<void(Expression const&)>)> _generateCode
|
||||
)
|
||||
{
|
||||
solAssert(_literalArguments.size() == _params || _literalArguments.empty(), "");
|
||||
@ -116,7 +130,7 @@ map<YulString, BuiltinFunctionForEVM> createBuiltins(langutil::EVMVersion _evmVe
|
||||
FunctionCall const& _call,
|
||||
AbstractAssembly& _assembly,
|
||||
BuiltinContext& _context,
|
||||
function<void()>
|
||||
std::function<void(Expression const&)>
|
||||
) {
|
||||
yulAssert(_context.currentObject, "No object available.");
|
||||
yulAssert(_call.arguments.size() == 1, "");
|
||||
@ -137,7 +151,7 @@ map<YulString, BuiltinFunctionForEVM> createBuiltins(langutil::EVMVersion _evmVe
|
||||
FunctionCall const& _call,
|
||||
AbstractAssembly& _assembly,
|
||||
BuiltinContext& _context,
|
||||
std::function<void()>
|
||||
std::function<void(Expression const&)>
|
||||
) {
|
||||
yulAssert(_context.currentObject, "No object available.");
|
||||
yulAssert(_call.arguments.size() == 1, "");
|
||||
@ -161,21 +175,58 @@ map<YulString, BuiltinFunctionForEVM> createBuiltins(langutil::EVMVersion _evmVe
|
||||
SideEffects{false, false, false, false, true},
|
||||
{},
|
||||
[](
|
||||
FunctionCall const&,
|
||||
FunctionCall const& _call,
|
||||
AbstractAssembly& _assembly,
|
||||
BuiltinContext&,
|
||||
std::function<void()> _visitArguments
|
||||
std::function<void(Expression const&)> _visitExpression
|
||||
) {
|
||||
_visitArguments();
|
||||
visitArguments(_assembly, _call, _visitExpression);
|
||||
_assembly.appendInstruction(evmasm::Instruction::CODECOPY);
|
||||
}
|
||||
));
|
||||
builtins.emplace(createFunction(
|
||||
"setimmutable",
|
||||
2,
|
||||
0,
|
||||
SideEffects{false, false, false, false, true},
|
||||
{true, false},
|
||||
[](
|
||||
FunctionCall const& _call,
|
||||
AbstractAssembly& _assembly,
|
||||
BuiltinContext&,
|
||||
std::function<void(Expression const&)> _visitExpression
|
||||
) {
|
||||
solAssert(_call.arguments.size() == 2, "");
|
||||
|
||||
_visitExpression(_call.arguments[1]);
|
||||
_assembly.setSourceLocation(_call.location);
|
||||
YulString identifier = std::get<Literal>(_call.arguments.front()).value;
|
||||
_assembly.appendImmutableAssignment(identifier.str());
|
||||
}
|
||||
));
|
||||
builtins.emplace(createFunction(
|
||||
"loadimmutable",
|
||||
1,
|
||||
1,
|
||||
SideEffects{},
|
||||
{true},
|
||||
[](
|
||||
FunctionCall const& _call,
|
||||
AbstractAssembly& _assembly,
|
||||
BuiltinContext&,
|
||||
std::function<void(Expression const&)>
|
||||
) {
|
||||
solAssert(_call.arguments.size() == 1, "");
|
||||
_assembly.appendImmutable(std::get<Literal>(_call.arguments.front()).value.str());
|
||||
}
|
||||
));
|
||||
}
|
||||
return builtins;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
EVMDialect::EVMDialect(langutil::EVMVersion _evmVersion, bool _objectAccess):
|
||||
m_objectAccess(_objectAccess),
|
||||
m_evmVersion(_evmVersion),
|
||||
@ -268,23 +319,23 @@ EVMDialectTyped::EVMDialectTyped(langutil::EVMVersion _evmVersion, bool _objectA
|
||||
m_functions["popbool"_yulstring].name = "popbool"_yulstring;
|
||||
m_functions["popbool"_yulstring].parameters = {"bool"_yulstring};
|
||||
m_functions.insert(createFunction("bool_to_u256", 1, 1, {}, {}, [](
|
||||
FunctionCall const&,
|
||||
AbstractAssembly&,
|
||||
FunctionCall const& _call,
|
||||
AbstractAssembly& _assembly,
|
||||
BuiltinContext&,
|
||||
std::function<void()> _visitArguments
|
||||
std::function<void(Expression const&)> _visitExpression
|
||||
) {
|
||||
_visitArguments();
|
||||
visitArguments(_assembly, _call, _visitExpression);
|
||||
}));
|
||||
m_functions["bool_to_u256"_yulstring].parameters = {"bool"_yulstring};
|
||||
m_functions["bool_to_u256"_yulstring].returns = {"u256"_yulstring};
|
||||
m_functions.insert(createFunction("u256_to_bool", 1, 1, {}, {}, [](
|
||||
FunctionCall const&,
|
||||
FunctionCall const& _call,
|
||||
AbstractAssembly& _assembly,
|
||||
BuiltinContext&,
|
||||
std::function<void()> _visitArguments
|
||||
std::function<void(Expression const&)> _visitExpression
|
||||
) {
|
||||
// A value larger than 1 causes an invalid instruction.
|
||||
_visitArguments();
|
||||
visitArguments(_assembly, _call, _visitExpression);
|
||||
_assembly.appendConstant(2);
|
||||
_assembly.appendInstruction(evmasm::Instruction::DUP2);
|
||||
_assembly.appendInstruction(evmasm::Instruction::LT);
|
||||
|
@ -23,6 +23,7 @@
|
||||
#include <libyul/Dialect.h>
|
||||
|
||||
#include <libyul/backends/evm/AbstractAssembly.h>
|
||||
#include <libyul/AsmData.h>
|
||||
#include <liblangutil/EVMVersion.h>
|
||||
|
||||
#include <map>
|
||||
@ -49,9 +50,9 @@ struct BuiltinFunctionForEVM: public BuiltinFunction
|
||||
{
|
||||
std::optional<evmasm::Instruction> instruction;
|
||||
/// 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 arguments
|
||||
/// from right to left.
|
||||
std::function<void(FunctionCall const&, AbstractAssembly&, BuiltinContext&, std::function<void()>)> generateCode;
|
||||
/// 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;
|
||||
};
|
||||
|
||||
|
||||
|
@ -19,10 +19,14 @@
|
||||
*/
|
||||
|
||||
#include <libyul/backends/evm/NoOutputAssembly.h>
|
||||
|
||||
#include <libyul/Exceptions.h>
|
||||
|
||||
#include <libevmasm/Instruction.h>
|
||||
|
||||
#include <boost/range/adaptor/reversed.hpp>
|
||||
|
||||
|
||||
using namespace std;
|
||||
using namespace solidity;
|
||||
using namespace solidity::yul;
|
||||
@ -142,6 +146,17 @@ AbstractAssembly::SubID NoOutputAssembly::appendData(bytes const&)
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
void NoOutputAssembly::appendImmutable(std::string const&)
|
||||
{
|
||||
yulAssert(false, "loadimmutable not implemented.");
|
||||
}
|
||||
|
||||
void NoOutputAssembly::appendImmutableAssignment(std::string const&)
|
||||
{
|
||||
yulAssert(false, "setimmutable not implemented.");
|
||||
}
|
||||
|
||||
NoOutputEVMDialect::NoOutputEVMDialect(EVMDialect const& _copyFrom):
|
||||
EVMDialect(_copyFrom.evmVersion(), _copyFrom.providesObjectAccess())
|
||||
{
|
||||
@ -149,9 +164,11 @@ NoOutputEVMDialect::NoOutputEVMDialect(EVMDialect const& _copyFrom):
|
||||
{
|
||||
size_t parameters = fun.second.parameters.size();
|
||||
size_t returns = fun.second.returns.size();
|
||||
fun.second.generateCode = [=](FunctionCall const&, AbstractAssembly& _assembly, BuiltinContext&, std::function<void()> _visitArguments)
|
||||
fun.second.generateCode = [=](FunctionCall const& _call, AbstractAssembly& _assembly, BuiltinContext&, std::function<void(Expression const&)> _visitExpression)
|
||||
{
|
||||
_visitArguments();
|
||||
for (auto const& arg: _call.arguments | boost::adaptors::reversed)
|
||||
_visitExpression(arg);
|
||||
|
||||
for (size_t i = 0; i < parameters; i++)
|
||||
_assembly.appendInstruction(evmasm::Instruction::POP);
|
||||
|
||||
|
@ -71,6 +71,9 @@ public:
|
||||
void appendDataSize(SubID _sub) override;
|
||||
SubID appendData(bytes const& _data) override;
|
||||
|
||||
void appendImmutable(std::string const& _identifier) override;
|
||||
void appendImmutableAssignment(std::string const& _identifier) override;
|
||||
|
||||
private:
|
||||
bool m_evm15 = false; ///< if true, switch to evm1.5 mode
|
||||
int m_stackHeight = 0;
|
||||
|
11
test/libsolidity/syntaxTests/inlineAssembly/immutables.sol
Normal file
11
test/libsolidity/syntaxTests/inlineAssembly/immutables.sol
Normal file
@ -0,0 +1,11 @@
|
||||
contract C {
|
||||
function f() public pure {
|
||||
assembly {
|
||||
setimmutable("abc", 0)
|
||||
loadimmutable("abc")
|
||||
}
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// DeclarationError: (63-75): Function not found.
|
||||
// DeclarationError: (92-105): Function not found.
|
Loading…
Reference in New Issue
Block a user