mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge remote-tracking branch 'origin/develop' into breaking
This commit is contained in:
@@ -1480,13 +1480,13 @@ bool TypeChecker::visit(UnaryOperation const& _operation)
|
||||
TypePointer t = type(_operation.subExpression())->unaryOperatorResult(op);
|
||||
if (!t)
|
||||
{
|
||||
m_errorReporter.typeError(
|
||||
_operation.location(),
|
||||
"Unary operator " +
|
||||
string(TokenTraits::toString(op)) +
|
||||
" cannot be applied to type " +
|
||||
subExprType->toString()
|
||||
);
|
||||
string description = "Unary operator " + string(TokenTraits::toString(op)) + " cannot be applied to type " + subExprType->toString();
|
||||
if (modifying)
|
||||
// Cannot just report the error, ignore the unary operator, and continue,
|
||||
// because the sub-expression was already processed with requireLValue()
|
||||
m_errorReporter.fatalTypeError(_operation.location(), description);
|
||||
else
|
||||
m_errorReporter.typeError(_operation.location(), description);
|
||||
t = subExprType;
|
||||
}
|
||||
_operation.annotation().type = t;
|
||||
|
||||
@@ -733,7 +733,10 @@ bool ContractCompiler::visit(InlineAssembly const& _inlineAssembly)
|
||||
{
|
||||
case Type::Category::Bool:
|
||||
case Type::Category::Address:
|
||||
solAssert(*type == *variable->annotation().type, "");
|
||||
// Either both the literal and the variable are bools, or they are both addresses.
|
||||
// If they are both bools, comparing category is the same as comparing the types.
|
||||
// If they are both addresses, compare category so that payable/nonpayable is not compared.
|
||||
solAssert(type->category() == variable->annotation().type->category(), "");
|
||||
value = type->literalValue(literal);
|
||||
break;
|
||||
case Type::Category::StringLiteral:
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
|
||||
#include <libsolidity/codegen/YulUtilFunctions.h>
|
||||
#include <libsolidity/codegen/ABIFunctions.h>
|
||||
#include <libsolidity/codegen/CompilerUtils.h>
|
||||
#include <libsolidity/ast/AST.h>
|
||||
#include <libsolidity/ast/TypeProvider.h>
|
||||
|
||||
@@ -76,6 +77,36 @@ IRVariable const& IRGenerationContext::localVariable(VariableDeclaration const&
|
||||
return m_localVariables.at(&_varDecl);
|
||||
}
|
||||
|
||||
void IRGenerationContext::registerImmutableVariable(VariableDeclaration const& _variable)
|
||||
{
|
||||
solAssert(_variable.immutable(), "Attempted to register a non-immutable variable as immutable.");
|
||||
solUnimplementedAssert(
|
||||
_variable.annotation().type->isValueType(),
|
||||
"Only immutable variables of value type are supported."
|
||||
);
|
||||
solAssert(m_reservedMemory.has_value(), "Reserved memory has already been reset.");
|
||||
m_immutableVariables[&_variable] = CompilerUtils::generalPurposeMemoryStart + *m_reservedMemory;
|
||||
solAssert(_variable.annotation().type->memoryHeadSize() == 32, "Memory writes might overlap.");
|
||||
*m_reservedMemory += _variable.annotation().type->memoryHeadSize();
|
||||
}
|
||||
|
||||
size_t IRGenerationContext::immutableMemoryOffset(VariableDeclaration const& _variable) const
|
||||
{
|
||||
solAssert(
|
||||
m_immutableVariables.count(&_variable),
|
||||
"Unknown immutable variable: " + _variable.name()
|
||||
);
|
||||
return m_immutableVariables.at(&_variable);
|
||||
}
|
||||
|
||||
size_t IRGenerationContext::reservedMemory()
|
||||
{
|
||||
solAssert(m_reservedMemory.has_value(), "Reserved memory was used before.");
|
||||
size_t reservedMemory = *m_reservedMemory;
|
||||
m_reservedMemory = std::nullopt;
|
||||
return reservedMemory;
|
||||
}
|
||||
|
||||
void IRGenerationContext::addStateVariable(
|
||||
VariableDeclaration const& _declaration,
|
||||
u256 _storageOffset,
|
||||
|
||||
@@ -81,6 +81,17 @@ public:
|
||||
bool isLocalVariable(VariableDeclaration const& _varDecl) const { return m_localVariables.count(&_varDecl); }
|
||||
IRVariable const& localVariable(VariableDeclaration const& _varDecl);
|
||||
|
||||
/// Registers an immutable variable of the contract.
|
||||
/// Should only be called at construction time.
|
||||
void registerImmutableVariable(VariableDeclaration const& _varDecl);
|
||||
/// @returns the reserved memory for storing the value of the
|
||||
/// immutable @a _variable during contract creation.
|
||||
size_t immutableMemoryOffset(VariableDeclaration const& _variable) const;
|
||||
/// @returns the reserved memory and resets it to mark it as used.
|
||||
/// Intended to be used only once for initializing the free memory pointer
|
||||
/// to after the area used for immutables.
|
||||
size_t reservedMemory();
|
||||
|
||||
void addStateVariable(VariableDeclaration const& _varDecl, u256 _storageOffset, unsigned _byteOffset);
|
||||
bool isStateVariable(VariableDeclaration const& _varDecl) const { return m_stateVariables.count(&_varDecl); }
|
||||
std::pair<u256, unsigned> storageLocationOfVariable(VariableDeclaration const& _varDecl) const
|
||||
@@ -123,6 +134,12 @@ private:
|
||||
OptimiserSettings m_optimiserSettings;
|
||||
ContractDefinition const* m_mostDerivedContract = nullptr;
|
||||
std::map<VariableDeclaration const*, IRVariable> m_localVariables;
|
||||
/// Memory offsets reserved for the values of immutable variables during contract creation.
|
||||
/// This map is empty in the runtime context.
|
||||
std::map<VariableDeclaration const*, size_t> m_immutableVariables;
|
||||
/// Total amount of reserved memory. Reserved memory is used to store
|
||||
/// immutable variables during contract creation.
|
||||
std::optional<size_t> m_reservedMemory = {0};
|
||||
/// Storage offsets of state variables
|
||||
std::map<VariableDeclaration const*, std::pair<u256, unsigned>> m_stateVariables;
|
||||
MultiUseYulFunctionCollector m_functions;
|
||||
|
||||
@@ -114,6 +114,8 @@ string IRGenerator::generate(
|
||||
)");
|
||||
|
||||
resetContext(_contract);
|
||||
for (VariableDeclaration const* var: ContractType(_contract).immutableVariables())
|
||||
m_context.registerImmutableVariable(*var);
|
||||
|
||||
t("CreationObject", m_context.creationObjectName(_contract));
|
||||
t("memoryInit", memoryInit());
|
||||
@@ -142,6 +144,7 @@ string IRGenerator::generate(
|
||||
t("subObjects", subObjectSources(m_context.subObjectsCreated()));
|
||||
|
||||
resetContext(_contract);
|
||||
// Do not register immutables to avoid assignment.
|
||||
t("RuntimeObject", m_context.runtimeObjectName(_contract));
|
||||
t("dispatch", dispatchRoutine(_contract));
|
||||
generateQueuedFunctions();
|
||||
@@ -200,7 +203,6 @@ string IRGenerator::generateGetter(VariableDeclaration const& _varDecl)
|
||||
Type const* type = _varDecl.annotation().type;
|
||||
|
||||
solAssert(!_varDecl.isConstant(), "");
|
||||
solAssert(!_varDecl.immutable(), "");
|
||||
solAssert(_varDecl.isStateVariable(), "");
|
||||
|
||||
if (auto const* mappingType = dynamic_cast<MappingType const*>(type))
|
||||
@@ -254,17 +256,32 @@ string IRGenerator::generateGetter(VariableDeclaration const& _varDecl)
|
||||
solUnimplementedAssert(type->isValueType(), "");
|
||||
|
||||
return m_context.functionCollector().createFunction(functionName, [&]() {
|
||||
pair<u256, unsigned> slot_offset = m_context.storageLocationOfVariable(_varDecl);
|
||||
if (_varDecl.immutable())
|
||||
{
|
||||
solUnimplementedAssert(type->sizeOnStack() == 1, "");
|
||||
return Whiskers(R"(
|
||||
function <functionName>() -> rval {
|
||||
rval := loadimmutable("<id>")
|
||||
}
|
||||
)")
|
||||
("functionName", functionName)
|
||||
("id", to_string(_varDecl.id()))
|
||||
.render();
|
||||
}
|
||||
else
|
||||
{
|
||||
pair<u256, unsigned> slot_offset = m_context.storageLocationOfVariable(_varDecl);
|
||||
|
||||
return Whiskers(R"(
|
||||
function <functionName>() -> rval {
|
||||
rval := <readStorage>(<slot>)
|
||||
}
|
||||
)")
|
||||
("functionName", functionName)
|
||||
("readStorage", m_utils.readFromStorage(*type, slot_offset.second, false))
|
||||
("slot", slot_offset.first.str())
|
||||
.render();
|
||||
return Whiskers(R"(
|
||||
function <functionName>() -> rval {
|
||||
rval := <readStorage>(<slot>)
|
||||
}
|
||||
)")
|
||||
("functionName", functionName)
|
||||
("readStorage", m_utils.readFromStorage(*type, slot_offset.second, false))
|
||||
("slot", slot_offset.first.str())
|
||||
.render();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -325,7 +342,7 @@ string IRGenerator::initStateVariables(ContractDefinition const& _contract)
|
||||
{
|
||||
IRGeneratorForStatements generator{m_context, m_utils};
|
||||
for (VariableDeclaration const* variable: _contract.stateVariables())
|
||||
if (!variable->isConstant() && !variable->immutable())
|
||||
if (!variable->isConstant())
|
||||
generator.initializeStateVar(*variable);
|
||||
|
||||
return generator.code();
|
||||
@@ -391,10 +408,41 @@ void IRGenerator::generateImplicitConstructors(ContractDefinition const& _contra
|
||||
string IRGenerator::deployCode(ContractDefinition const& _contract)
|
||||
{
|
||||
Whiskers t(R"X(
|
||||
<#loadImmutables>
|
||||
let <var> := mload(<memoryOffset>)
|
||||
</loadImmutables>
|
||||
|
||||
codecopy(0, dataoffset("<object>"), datasize("<object>"))
|
||||
|
||||
<#storeImmutables>
|
||||
setimmutable("<immutableName>", <var>)
|
||||
</storeImmutables>
|
||||
|
||||
return(0, datasize("<object>"))
|
||||
)X");
|
||||
t("object", m_context.runtimeObjectName(_contract));
|
||||
|
||||
vector<map<string, string>> loadImmutables;
|
||||
vector<map<string, string>> storeImmutables;
|
||||
|
||||
for (VariableDeclaration const* immutable: ContractType(_contract).immutableVariables())
|
||||
{
|
||||
solUnimplementedAssert(immutable->type()->isValueType(), "");
|
||||
solUnimplementedAssert(immutable->type()->sizeOnStack() == 1, "");
|
||||
string yulVar = m_context.newYulVariable();
|
||||
loadImmutables.emplace_back(map<string, string>{
|
||||
{"var"s, yulVar},
|
||||
{"memoryOffset"s, to_string(m_context.immutableMemoryOffset(*immutable))}
|
||||
});
|
||||
storeImmutables.emplace_back(map<string, string>{
|
||||
{"var"s, yulVar},
|
||||
{"immutableName"s, to_string(immutable->id())}
|
||||
});
|
||||
}
|
||||
t("loadImmutables", std::move(loadImmutables));
|
||||
// reverse order to ease stack strain
|
||||
reverse(storeImmutables.begin(), storeImmutables.end());
|
||||
t("storeImmutables", std::move(storeImmutables));
|
||||
return t.render();
|
||||
}
|
||||
|
||||
@@ -489,9 +537,9 @@ string IRGenerator::memoryInit()
|
||||
// and thus can assume all memory to be zero, including the contents of
|
||||
// the "zero memory area" (the position CompilerUtils::zeroPointer points to).
|
||||
return
|
||||
Whiskers{"mstore(<memPtr>, <generalPurposeStart>)"}
|
||||
Whiskers{"mstore(<memPtr>, <freeMemoryStart>)"}
|
||||
("memPtr", to_string(CompilerUtils::freeMemoryPointer))
|
||||
("generalPurposeStart", to_string(CompilerUtils::generalPurposeMemoryStart))
|
||||
("freeMemoryStart", to_string(CompilerUtils::generalPurposeMemoryStart + m_context.reservedMemory()))
|
||||
.render();
|
||||
}
|
||||
|
||||
|
||||
@@ -140,20 +140,21 @@ string IRGeneratorForStatements::code() const
|
||||
|
||||
void IRGeneratorForStatements::initializeStateVar(VariableDeclaration const& _varDecl)
|
||||
{
|
||||
solAssert(m_context.isStateVariable(_varDecl), "Must be a state variable.");
|
||||
solAssert(_varDecl.immutable() || m_context.isStateVariable(_varDecl), "Must be immutable or a state variable.");
|
||||
solAssert(!_varDecl.isConstant(), "");
|
||||
solAssert(!_varDecl.immutable(), "");
|
||||
if (_varDecl.value())
|
||||
{
|
||||
_varDecl.value()->accept(*this);
|
||||
writeToLValue(IRLValue{
|
||||
*_varDecl.annotation().type,
|
||||
IRLValue::Storage{
|
||||
util::toCompactHexWithPrefix(m_context.storageLocationOfVariable(_varDecl).first),
|
||||
m_context.storageLocationOfVariable(_varDecl).second
|
||||
}
|
||||
}, *_varDecl.value());
|
||||
}
|
||||
if (!_varDecl.value())
|
||||
return;
|
||||
|
||||
_varDecl.value()->accept(*this);
|
||||
writeToLValue(
|
||||
_varDecl.immutable() ?
|
||||
IRLValue{*_varDecl.annotation().type, IRLValue::Immutable{&_varDecl}} :
|
||||
IRLValue{*_varDecl.annotation().type, IRLValue::Storage{
|
||||
util::toCompactHexWithPrefix(m_context.storageLocationOfVariable(_varDecl).first),
|
||||
m_context.storageLocationOfVariable(_varDecl).second
|
||||
}},
|
||||
*_varDecl.value()
|
||||
);
|
||||
}
|
||||
|
||||
void IRGeneratorForStatements::initializeLocalVar(VariableDeclaration const& _varDecl)
|
||||
@@ -584,7 +585,7 @@ void IRGeneratorForStatements::endVisit(FunctionCall const& _functionCall)
|
||||
case FunctionType::Kind::Internal:
|
||||
{
|
||||
vector<string> args;
|
||||
for (unsigned i = 0; i < arguments.size(); ++i)
|
||||
for (size_t i = 0; i < arguments.size(); ++i)
|
||||
if (functionType->takesArbitraryParameters())
|
||||
args.emplace_back(IRVariable(*arguments[i]).commaSeparatedList());
|
||||
else
|
||||
@@ -730,6 +731,16 @@ void IRGeneratorForStatements::endVisit(FunctionCall const& _functionCall)
|
||||
|
||||
break;
|
||||
}
|
||||
case FunctionType::Kind::Revert:
|
||||
{
|
||||
solAssert(arguments.size() == parameterTypes.size(), "");
|
||||
if (arguments.empty())
|
||||
m_code << "revert(0, 0)\n";
|
||||
else
|
||||
solUnimplementedAssert(false, "");
|
||||
|
||||
break;
|
||||
}
|
||||
// Array creation using new
|
||||
case FunctionType::Kind::ObjectCreation:
|
||||
{
|
||||
@@ -818,15 +829,43 @@ void IRGeneratorForStatements::endVisit(FunctionCall const& _functionCall)
|
||||
{
|
||||
break;
|
||||
}
|
||||
case FunctionType::Kind::GasLeft:
|
||||
case FunctionType::Kind::AddMod:
|
||||
case FunctionType::Kind::MulMod:
|
||||
{
|
||||
define(_functionCall) << "gas()\n";
|
||||
static map<FunctionType::Kind, string> functions = {
|
||||
{FunctionType::Kind::AddMod, "addmod"},
|
||||
{FunctionType::Kind::MulMod, "mulmod"},
|
||||
};
|
||||
solAssert(functions.find(functionType->kind()) != functions.end(), "");
|
||||
solAssert(arguments.size() == 3 && parameterTypes.size() == 3, "");
|
||||
|
||||
IRVariable modulus(m_context.newYulVariable(), *(parameterTypes[2]));
|
||||
define(modulus, *arguments[2]);
|
||||
Whiskers templ("if iszero(<modulus>) { invalid() }\n");
|
||||
m_code << templ("modulus", modulus.name()).render();
|
||||
|
||||
string args;
|
||||
for (size_t i = 0; i < 2; ++i)
|
||||
args += expressionAsType(*arguments[i], *(parameterTypes[i])) + ", ";
|
||||
args += modulus.name();
|
||||
define(_functionCall) << functions[functionType->kind()] << "(" << args << ")\n";
|
||||
break;
|
||||
}
|
||||
case FunctionType::Kind::GasLeft:
|
||||
case FunctionType::Kind::Selfdestruct:
|
||||
case FunctionType::Kind::BlockHash:
|
||||
{
|
||||
solAssert(arguments.size() == 1, "");
|
||||
define(_functionCall) << "selfdestruct(" << expressionAsType(*arguments.front(), *parameterTypes.front()) << ")\n";
|
||||
static map<FunctionType::Kind, string> functions = {
|
||||
{FunctionType::Kind::GasLeft, "gas"},
|
||||
{FunctionType::Kind::Selfdestruct, "selfdestruct"},
|
||||
{FunctionType::Kind::BlockHash, "blockhash"},
|
||||
};
|
||||
solAssert(functions.find(functionType->kind()) != functions.end(), "");
|
||||
|
||||
string args;
|
||||
for (size_t i = 0; i < arguments.size(); ++i)
|
||||
args += (args.empty() ? "" : ", ") + expressionAsType(*arguments[i], *(parameterTypes[i]));
|
||||
define(_functionCall) << functions[functionType->kind()] << "(" << args << ")\n";
|
||||
break;
|
||||
}
|
||||
case FunctionType::Kind::Log0:
|
||||
@@ -908,6 +947,34 @@ void IRGeneratorForStatements::endVisit(FunctionCall const& _functionCall)
|
||||
|
||||
break;
|
||||
}
|
||||
case FunctionType::Kind::Send:
|
||||
case FunctionType::Kind::Transfer:
|
||||
{
|
||||
solAssert(arguments.size() == 1 && parameterTypes.size() == 1, "");
|
||||
string address{IRVariable(_functionCall.expression()).part("address").name()};
|
||||
string value{expressionAsType(*arguments[0], *(parameterTypes[0]))};
|
||||
Whiskers templ(R"(
|
||||
let <gas> := 0
|
||||
if iszero(<value>) { <gas> := <callStipend> }
|
||||
let <success> := call(<gas>, <address>, <value>, 0, 0, 0, 0)
|
||||
<?isTransfer>
|
||||
if iszero(<success>) { <forwardingRevert>() }
|
||||
</isTransfer>
|
||||
)");
|
||||
templ("gas", m_context.newYulVariable());
|
||||
templ("callStipend", toString(evmasm::GasCosts::callStipend));
|
||||
templ("address", address);
|
||||
templ("value", value);
|
||||
if (functionType->kind() == FunctionType::Kind::Transfer)
|
||||
templ("success", m_context.newYulVariable());
|
||||
else
|
||||
templ("success", IRVariable(_functionCall).commaSeparatedList());
|
||||
templ("isTransfer", functionType->kind() == FunctionType::Kind::Transfer);
|
||||
templ("forwardingRevert", m_utils.forwardingRevertFunction());
|
||||
m_code << templ.render();
|
||||
|
||||
break;
|
||||
}
|
||||
default:
|
||||
solUnimplemented("FunctionKind " + toString(static_cast<int>(functionType->kind())) + " not yet implemented");
|
||||
}
|
||||
@@ -1479,8 +1546,12 @@ void IRGeneratorForStatements::handleVariableReference(
|
||||
// If the value is visited twice, `defineExpression` is called twice on
|
||||
// the same expression.
|
||||
solUnimplementedAssert(!_variable.isConstant(), "");
|
||||
solUnimplementedAssert(!_variable.immutable(), "");
|
||||
if (m_context.isLocalVariable(_variable))
|
||||
if (_variable.isStateVariable() && _variable.immutable())
|
||||
setLValue(_referencingExpression, IRLValue{
|
||||
*_variable.annotation().type,
|
||||
IRLValue::Immutable{&_variable}
|
||||
});
|
||||
else if (m_context.isLocalVariable(_variable))
|
||||
setLValue(_referencingExpression, IRLValue{
|
||||
*_variable.annotation().type,
|
||||
IRLValue::Stack{m_context.localVariable(_variable)}
|
||||
@@ -1901,6 +1972,18 @@ void IRGeneratorForStatements::writeToLValue(IRLValue const& _lvalue, IRVariable
|
||||
}
|
||||
},
|
||||
[&](IRLValue::Stack const& _stack) { assign(_stack.variable, _value); },
|
||||
[&](IRLValue::Immutable const& _immutable)
|
||||
{
|
||||
solUnimplementedAssert(_lvalue.type.isValueType(), "");
|
||||
solUnimplementedAssert(_lvalue.type.sizeOnStack() == 1, "");
|
||||
solAssert(_lvalue.type == *_immutable.variable->type(), "");
|
||||
size_t memOffset = m_context.immutableMemoryOffset(*_immutable.variable);
|
||||
|
||||
IRVariable prepared(m_context.newYulVariable(), _lvalue.type);
|
||||
define(prepared, _value);
|
||||
|
||||
m_code << "mstore(" << to_string(memOffset) << ", " << prepared.commaSeparatedList() << ")\n";
|
||||
},
|
||||
[&](IRLValue::Tuple const& _tuple) {
|
||||
auto components = std::move(_tuple.components);
|
||||
for (size_t i = 0; i < components.size(); i++)
|
||||
@@ -1956,6 +2039,12 @@ IRVariable IRGeneratorForStatements::readFromLValue(IRLValue const& _lvalue)
|
||||
[&](IRLValue::Stack const& _stack) {
|
||||
define(result, _stack.variable);
|
||||
},
|
||||
[&](IRLValue::Immutable const& _immutable) {
|
||||
solUnimplementedAssert(_lvalue.type.isValueType(), "");
|
||||
solUnimplementedAssert(_lvalue.type.sizeOnStack() == 1, "");
|
||||
solAssert(_lvalue.type == *_immutable.variable->type(), "");
|
||||
define(result) << "loadimmutable(\"" << to_string(_immutable.variable->id()) << "\")\n";
|
||||
},
|
||||
[&](IRLValue::Tuple const&) {
|
||||
solAssert(false, "Attempted to read from tuple lvalue.");
|
||||
}
|
||||
|
||||
@@ -35,6 +35,10 @@ struct IRLValue
|
||||
{
|
||||
IRVariable variable;
|
||||
};
|
||||
struct Immutable
|
||||
{
|
||||
VariableDeclaration const* variable = nullptr;
|
||||
};
|
||||
struct Storage
|
||||
{
|
||||
std::string const slot;
|
||||
@@ -59,7 +63,7 @@ struct IRLValue
|
||||
{
|
||||
std::vector<std::optional<IRLValue>> components;
|
||||
};
|
||||
std::variant<Stack, Storage, Memory, Tuple> kind;
|
||||
std::variant<Stack, Immutable, Storage, Memory, Tuple> kind;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user