2019-03-04 22:26:46 +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/>.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Class that contains contextual information during IR generation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libsolidity/codegen/ir/IRGenerationContext.h>
|
|
|
|
|
2019-04-02 10:37:48 +00:00
|
|
|
#include <libsolidity/codegen/YulUtilFunctions.h>
|
2020-04-09 19:59:17 +00:00
|
|
|
#include <libsolidity/codegen/ABIFunctions.h>
|
2020-04-02 18:06:52 +00:00
|
|
|
#include <libsolidity/codegen/CompilerUtils.h>
|
2019-03-04 22:26:46 +00:00
|
|
|
#include <libsolidity/ast/AST.h>
|
2020-03-02 20:42:46 +00:00
|
|
|
#include <libsolidity/ast/TypeProvider.h>
|
2019-03-04 22:26:46 +00:00
|
|
|
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/Whiskers.h>
|
|
|
|
#include <libsolutil/StringUtils.h>
|
2019-04-02 10:37:48 +00:00
|
|
|
|
2019-03-04 22:26:46 +00:00
|
|
|
using namespace std;
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::util;
|
|
|
|
using namespace solidity::frontend;
|
2019-03-04 22:26:46 +00:00
|
|
|
|
2020-04-10 17:16:44 +00:00
|
|
|
string IRGenerationContext::enqueueFunctionForCodeGeneration(FunctionDefinition const& _function)
|
|
|
|
{
|
2020-05-13 17:48:31 +00:00
|
|
|
string name = IRNames::function(_function);
|
2020-04-10 17:16:44 +00:00
|
|
|
|
|
|
|
if (!m_functions.contains(name))
|
|
|
|
m_functionGenerationQueue.insert(&_function);
|
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
FunctionDefinition const* IRGenerationContext::dequeueFunctionForCodeGeneration()
|
|
|
|
{
|
|
|
|
solAssert(!m_functionGenerationQueue.empty(), "");
|
|
|
|
|
|
|
|
FunctionDefinition const* result = *m_functionGenerationQueue.begin();
|
|
|
|
m_functionGenerationQueue.erase(m_functionGenerationQueue.begin());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-03-24 17:25:59 +00:00
|
|
|
ContractDefinition const& IRGenerationContext::mostDerivedContract() const
|
|
|
|
{
|
|
|
|
solAssert(m_mostDerivedContract, "Most derived contract requested but not set.");
|
|
|
|
return *m_mostDerivedContract;
|
|
|
|
}
|
|
|
|
|
2020-02-06 13:05:25 +00:00
|
|
|
IRVariable const& IRGenerationContext::addLocalVariable(VariableDeclaration const& _varDecl)
|
2019-03-04 22:26:46 +00:00
|
|
|
{
|
2020-02-06 13:05:25 +00:00
|
|
|
auto const& [it, didInsert] = m_localVariables.emplace(
|
|
|
|
std::make_pair(&_varDecl, IRVariable{_varDecl})
|
2019-03-04 22:26:46 +00:00
|
|
|
);
|
2020-02-06 13:05:25 +00:00
|
|
|
solAssert(didInsert, "Local variable added multiple times.");
|
|
|
|
return it->second;
|
2019-03-04 22:26:46 +00:00
|
|
|
}
|
2019-03-18 10:21:41 +00:00
|
|
|
|
2020-02-06 13:05:25 +00:00
|
|
|
IRVariable const& IRGenerationContext::localVariable(VariableDeclaration const& _varDecl)
|
2019-03-18 10:21:41 +00:00
|
|
|
{
|
|
|
|
solAssert(
|
|
|
|
m_localVariables.count(&_varDecl),
|
|
|
|
"Unknown variable: " + _varDecl.name()
|
|
|
|
);
|
2020-02-06 13:05:25 +00:00
|
|
|
return m_localVariables.at(&_varDecl);
|
2019-03-18 10:21:41 +00:00
|
|
|
}
|
|
|
|
|
2020-04-02 18:06:52 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-04-30 16:32:56 +00:00
|
|
|
void IRGenerationContext::addStateVariable(
|
|
|
|
VariableDeclaration const& _declaration,
|
|
|
|
u256 _storageOffset,
|
|
|
|
unsigned _byteOffset
|
|
|
|
)
|
|
|
|
{
|
|
|
|
m_stateVariables[&_declaration] = make_pair(move(_storageOffset), _byteOffset);
|
|
|
|
}
|
|
|
|
|
2019-03-18 10:21:41 +00:00
|
|
|
string IRGenerationContext::newYulVariable()
|
|
|
|
{
|
|
|
|
return "_" + to_string(++m_varCounter);
|
|
|
|
}
|
|
|
|
|
2019-04-02 10:37:48 +00:00
|
|
|
string IRGenerationContext::internalDispatch(size_t _in, size_t _out)
|
|
|
|
{
|
|
|
|
string funName = "dispatch_internal_in_" + to_string(_in) + "_out_" + to_string(_out);
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_functions.createFunction(funName, [&]() {
|
2019-04-02 10:37:48 +00:00
|
|
|
Whiskers templ(R"(
|
2019-05-22 10:25:00 +00:00
|
|
|
function <functionName>(fun <comma> <in>) <arrow> <out> {
|
2019-04-02 10:37:48 +00:00
|
|
|
switch fun
|
|
|
|
<#cases>
|
|
|
|
case <funID>
|
|
|
|
{
|
2020-04-23 19:16:41 +00:00
|
|
|
<out> <assignment_op> <name>(<in>)
|
2019-04-02 10:37:48 +00:00
|
|
|
}
|
|
|
|
</cases>
|
|
|
|
default { invalid() }
|
|
|
|
}
|
|
|
|
)");
|
|
|
|
templ("functionName", funName);
|
|
|
|
templ("comma", _in > 0 ? "," : "");
|
2020-01-22 14:48:56 +00:00
|
|
|
YulUtilFunctions utils(m_evmVersion, m_revertStrings, m_functions);
|
2019-07-05 15:15:38 +00:00
|
|
|
templ("in", suffixedVariableNameList("in_", 0, _in));
|
2019-05-22 10:25:00 +00:00
|
|
|
templ("arrow", _out > 0 ? "->" : "");
|
2020-04-23 19:16:41 +00:00
|
|
|
templ("assignment_op", _out > 0 ? ":=" : "");
|
2019-07-05 15:15:38 +00:00
|
|
|
templ("out", suffixedVariableNameList("out_", 0, _out));
|
2020-04-23 13:59:42 +00:00
|
|
|
|
|
|
|
// UNIMPLEMENTED: Internal library calls via pointers are not implemented yet.
|
|
|
|
// We're not generating code for internal library functions here even though it's possible
|
|
|
|
// to call them via pointers. Right now such calls end up triggering the `default` case in
|
|
|
|
// the switch above.
|
2019-04-02 10:37:48 +00:00
|
|
|
vector<map<string, string>> functions;
|
2020-03-24 17:25:59 +00:00
|
|
|
for (auto const& contract: mostDerivedContract().annotation().linearizedBaseContracts)
|
2019-04-02 10:37:48 +00:00
|
|
|
for (FunctionDefinition const* function: contract->definedFunctions())
|
|
|
|
if (
|
2020-05-19 16:00:50 +00:00
|
|
|
FunctionType const* functionType = TypeProvider::function(*function, FunctionType::Kind::Internal);
|
2019-04-02 10:37:48 +00:00
|
|
|
!function->isConstructor() &&
|
2020-03-02 20:42:46 +00:00
|
|
|
TupleType(functionType->parameterTypes()).sizeOnStack() == _in &&
|
|
|
|
TupleType(functionType->returnParameterTypes()).sizeOnStack() == _out
|
2019-04-02 10:37:48 +00:00
|
|
|
)
|
2019-05-22 10:25:00 +00:00
|
|
|
{
|
|
|
|
// 0 is reserved for uninitialized function pointers
|
|
|
|
solAssert(function->id() != 0, "Unexpected function ID: 0");
|
|
|
|
|
2019-04-02 10:37:48 +00:00
|
|
|
functions.emplace_back(map<string, string> {
|
|
|
|
{ "funID", to_string(function->id()) },
|
2020-05-13 17:48:31 +00:00
|
|
|
{ "name", IRNames::function(*function)}
|
2019-04-02 10:37:48 +00:00
|
|
|
});
|
2020-04-10 17:16:44 +00:00
|
|
|
|
|
|
|
enqueueFunctionForCodeGeneration(*function);
|
2019-05-22 10:25:00 +00:00
|
|
|
}
|
2019-04-02 10:37:48 +00:00
|
|
|
templ("cases", move(functions));
|
|
|
|
return templ.render();
|
|
|
|
});
|
|
|
|
}
|
2019-04-30 16:32:56 +00:00
|
|
|
|
|
|
|
YulUtilFunctions IRGenerationContext::utils()
|
|
|
|
{
|
2020-01-22 14:48:56 +00:00
|
|
|
return YulUtilFunctions(m_evmVersion, m_revertStrings, m_functions);
|
|
|
|
}
|
|
|
|
|
2020-04-09 19:59:17 +00:00
|
|
|
ABIFunctions IRGenerationContext::abiFunctions()
|
|
|
|
{
|
|
|
|
return ABIFunctions(m_evmVersion, m_revertStrings, m_functions);
|
|
|
|
}
|
|
|
|
|
2020-01-22 14:48:56 +00:00
|
|
|
std::string IRGenerationContext::revertReasonIfDebug(std::string const& _message)
|
|
|
|
{
|
|
|
|
return YulUtilFunctions::revertReasonIfDebug(m_revertStrings, _message);
|
2019-04-30 16:32:56 +00:00
|
|
|
}
|
2020-02-10 10:58:36 +00:00
|
|
|
|