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/>.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @author Alex Beregszaszi
|
|
|
|
* @date 2017
|
|
|
|
* Component that translates Solidity code into Yul.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libsolidity/codegen/ir/IRGenerator.h>
|
|
|
|
|
2019-03-18 10:21:41 +00:00
|
|
|
#include <libsolidity/codegen/ir/IRGeneratorForStatements.h>
|
|
|
|
|
2019-03-04 22:26:46 +00:00
|
|
|
#include <libsolidity/ast/AST.h>
|
2019-03-18 10:21:41 +00:00
|
|
|
#include <libsolidity/ast/ASTVisitor.h>
|
2019-03-04 22:26:46 +00:00
|
|
|
#include <libsolidity/codegen/ABIFunctions.h>
|
|
|
|
#include <libsolidity/codegen/CompilerUtils.h>
|
|
|
|
|
|
|
|
#include <libyul/AssemblyStack.h>
|
2019-06-21 15:33:35 +00:00
|
|
|
#include <libyul/Utilities.h>
|
2019-03-04 22:26:46 +00:00
|
|
|
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/CommonData.h>
|
|
|
|
#include <libsolutil/Whiskers.h>
|
|
|
|
#include <libsolutil/StringUtils.h>
|
2019-03-04 22:26:46 +00:00
|
|
|
|
|
|
|
#include <liblangutil/SourceReferenceFormatter.h>
|
|
|
|
|
2019-05-20 10:01:47 +00:00
|
|
|
#include <sstream>
|
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-09 19:59:17 +00:00
|
|
|
pair<string, string> IRGenerator::run(
|
|
|
|
ContractDefinition const& _contract,
|
|
|
|
map<ContractDefinition const*, string const> const& _otherYulSources
|
|
|
|
)
|
2019-03-04 22:26:46 +00:00
|
|
|
{
|
2020-04-09 19:59:17 +00:00
|
|
|
string const ir = yul::reindent(generate(_contract, _otherYulSources));
|
2019-03-04 22:26:46 +00:00
|
|
|
|
|
|
|
yul::AssemblyStack asmStack(m_evmVersion, yul::AssemblyStack::Language::StrictAssembly, m_optimiserSettings);
|
|
|
|
if (!asmStack.parseAndAnalyze("", ir))
|
|
|
|
{
|
|
|
|
string errorMessage;
|
|
|
|
for (auto const& error: asmStack.errors())
|
2019-04-05 15:49:39 +00:00
|
|
|
errorMessage += langutil::SourceReferenceFormatter::formatErrorInformation(*error);
|
2019-06-06 12:00:30 +00:00
|
|
|
solAssert(false, ir + "\n\nInvalid IR generated:\n" + errorMessage + "\n");
|
2019-03-04 22:26:46 +00:00
|
|
|
}
|
|
|
|
asmStack.optimize();
|
|
|
|
|
|
|
|
string warning =
|
|
|
|
"/*******************************************************\n"
|
|
|
|
" * WARNING *\n"
|
|
|
|
" * Solidity to Yul compilation is still EXPERIMENTAL *\n"
|
|
|
|
" * It can result in LOSS OF FUNDS or worse *\n"
|
|
|
|
" * !USE AT YOUR OWN RISK! *\n"
|
|
|
|
" *******************************************************/\n\n";
|
|
|
|
|
|
|
|
return {warning + ir, warning + asmStack.print()};
|
|
|
|
}
|
|
|
|
|
2020-04-09 19:59:17 +00:00
|
|
|
string IRGenerator::generate(
|
|
|
|
ContractDefinition const& _contract,
|
|
|
|
map<ContractDefinition const*, string const> const& _otherYulSources
|
|
|
|
)
|
2019-03-04 22:26:46 +00:00
|
|
|
{
|
2020-04-09 19:59:17 +00:00
|
|
|
auto subObjectSources = [&_otherYulSources](std::set<ContractDefinition const*, ASTNode::CompareByID> const& subObjects) -> string
|
|
|
|
{
|
|
|
|
std::string subObjectsSources;
|
|
|
|
for (ContractDefinition const* subObject: subObjects)
|
|
|
|
subObjectsSources += _otherYulSources.at(subObject);
|
|
|
|
return subObjectsSources;
|
|
|
|
};
|
|
|
|
|
2019-03-04 22:26:46 +00:00
|
|
|
Whiskers t(R"(
|
|
|
|
object "<CreationObject>" {
|
|
|
|
code {
|
|
|
|
<memoryInit>
|
2020-04-06 15:26:59 +00:00
|
|
|
<callValueCheck>
|
2020-04-20 14:22:39 +00:00
|
|
|
<?notLibrary>
|
2020-04-06 15:26:59 +00:00
|
|
|
<?constructorHasParams> let <constructorParams> := <copyConstructorArguments>() </constructorHasParams>
|
|
|
|
<implicitConstructor>(<constructorParams>)
|
2020-04-20 14:22:39 +00:00
|
|
|
</notLibrary>
|
2019-03-04 22:26:46 +00:00
|
|
|
<deploy>
|
|
|
|
<functions>
|
|
|
|
}
|
|
|
|
object "<RuntimeObject>" {
|
|
|
|
code {
|
|
|
|
<memoryInit>
|
|
|
|
<dispatch>
|
|
|
|
<runtimeFunctions>
|
|
|
|
}
|
2020-04-09 19:59:17 +00:00
|
|
|
<runtimeSubObjects>
|
2019-03-04 22:26:46 +00:00
|
|
|
}
|
2020-04-09 19:59:17 +00:00
|
|
|
<subObjects>
|
2019-03-04 22:26:46 +00:00
|
|
|
}
|
|
|
|
)");
|
|
|
|
|
2019-04-30 16:32:56 +00:00
|
|
|
resetContext(_contract);
|
2020-04-02 18:06:52 +00:00
|
|
|
for (VariableDeclaration const* var: ContractType(_contract).immutableVariables())
|
|
|
|
m_context.registerImmutableVariable(*var);
|
2019-05-09 12:42:35 +00:00
|
|
|
|
2020-05-13 17:48:31 +00:00
|
|
|
t("CreationObject", IRNames::creationObject(_contract));
|
2019-03-04 22:26:46 +00:00
|
|
|
t("memoryInit", memoryInit());
|
2020-04-20 14:22:39 +00:00
|
|
|
t("notLibrary", !_contract.isLibrary());
|
2020-04-06 15:26:59 +00:00
|
|
|
|
|
|
|
FunctionDefinition const* constructor = _contract.constructor();
|
|
|
|
t("callValueCheck", !constructor || !constructor->isPayable() ? callValueCheck() : "");
|
|
|
|
vector<string> constructorParams;
|
|
|
|
if (constructor && !constructor->parameters().empty())
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < constructor->parameters().size(); ++i)
|
|
|
|
constructorParams.emplace_back(m_context.newYulVariable());
|
|
|
|
t(
|
|
|
|
"copyConstructorArguments",
|
2020-05-13 17:48:31 +00:00
|
|
|
m_utils.copyConstructorArgumentsToMemoryFunction(_contract, IRNames::creationObject(_contract))
|
2020-04-06 15:26:59 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
t("constructorParams", joinHumanReadable(constructorParams));
|
|
|
|
t("constructorHasParams", !constructorParams.empty());
|
|
|
|
t("implicitConstructor", implicitConstructorName(_contract));
|
|
|
|
|
2019-03-04 22:26:46 +00:00
|
|
|
t("deploy", deployCode(_contract));
|
2020-04-06 15:26:59 +00:00
|
|
|
generateImplicitConstructors(_contract);
|
2020-04-10 17:16:44 +00:00
|
|
|
generateQueuedFunctions();
|
2020-03-02 17:08:19 +00:00
|
|
|
t("functions", m_context.functionCollector().requestedFunctions());
|
2020-04-09 19:59:17 +00:00
|
|
|
t("subObjects", subObjectSources(m_context.subObjectsCreated()));
|
2019-03-04 22:26:46 +00:00
|
|
|
|
2019-04-30 16:32:56 +00:00
|
|
|
resetContext(_contract);
|
2020-04-02 18:06:52 +00:00
|
|
|
// Do not register immutables to avoid assignment.
|
2020-05-13 17:48:31 +00:00
|
|
|
t("RuntimeObject", IRNames::runtimeObject(_contract));
|
2019-03-04 22:26:46 +00:00
|
|
|
t("dispatch", dispatchRoutine(_contract));
|
2020-04-10 17:16:44 +00:00
|
|
|
generateQueuedFunctions();
|
2020-03-02 17:08:19 +00:00
|
|
|
t("runtimeFunctions", m_context.functionCollector().requestedFunctions());
|
2020-04-09 19:59:17 +00:00
|
|
|
t("runtimeSubObjects", subObjectSources(m_context.subObjectsCreated()));
|
2019-03-04 22:26:46 +00:00
|
|
|
return t.render();
|
|
|
|
}
|
|
|
|
|
2019-03-18 10:21:41 +00:00
|
|
|
string IRGenerator::generate(Block const& _block)
|
|
|
|
{
|
|
|
|
IRGeneratorForStatements generator(m_context, m_utils);
|
|
|
|
_block.accept(generator);
|
|
|
|
return generator.code();
|
|
|
|
}
|
|
|
|
|
2020-04-10 17:16:44 +00:00
|
|
|
void IRGenerator::generateQueuedFunctions()
|
|
|
|
{
|
|
|
|
while (!m_context.functionGenerationQueueEmpty())
|
|
|
|
// NOTE: generateFunction() may modify function generation queue
|
|
|
|
generateFunction(*m_context.dequeueFunctionForCodeGeneration());
|
|
|
|
}
|
|
|
|
|
2019-03-18 10:21:41 +00:00
|
|
|
string IRGenerator::generateFunction(FunctionDefinition const& _function)
|
2019-03-04 22:26:46 +00:00
|
|
|
{
|
2020-05-13 17:48:31 +00:00
|
|
|
string functionName = IRNames::function(_function);
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_context.functionCollector().createFunction(functionName, [&]() {
|
2019-04-24 21:48:12 +00:00
|
|
|
Whiskers t(R"(
|
2020-05-07 15:29:42 +00:00
|
|
|
function <functionName>(<params>)<?+retParams> -> <retParams></+retParams> {
|
2020-03-02 20:42:46 +00:00
|
|
|
<initReturnVariables>
|
2019-10-24 17:23:56 +00:00
|
|
|
<body>
|
2019-04-24 21:48:12 +00:00
|
|
|
}
|
|
|
|
)");
|
2019-03-04 22:26:46 +00:00
|
|
|
t("functionName", functionName);
|
2020-05-07 15:29:42 +00:00
|
|
|
vector<string> params;
|
2019-03-04 22:26:46 +00:00
|
|
|
for (auto const& varDecl: _function.parameters())
|
2020-05-07 15:29:42 +00:00
|
|
|
params += m_context.addLocalVariable(*varDecl).stackSlots();
|
|
|
|
t("params", joinHumanReadable(params));
|
|
|
|
vector<string> retParams;
|
2020-03-02 20:42:46 +00:00
|
|
|
string retInit;
|
2019-03-04 22:26:46 +00:00
|
|
|
for (auto const& varDecl: _function.returnParameters())
|
2020-03-02 20:42:46 +00:00
|
|
|
{
|
2020-05-07 15:29:42 +00:00
|
|
|
retParams += m_context.addLocalVariable(*varDecl).stackSlots();
|
2020-03-02 20:42:46 +00:00
|
|
|
retInit += generateInitialAssignment(*varDecl);
|
|
|
|
}
|
2020-05-07 15:29:42 +00:00
|
|
|
t("retParams", joinHumanReadable(retParams));
|
2020-03-02 20:42:46 +00:00
|
|
|
t("initReturnVariables", retInit);
|
2019-03-18 10:21:41 +00:00
|
|
|
t("body", generate(_function.body()));
|
2019-03-04 22:26:46 +00:00
|
|
|
return t.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-08 20:15:59 +00:00
|
|
|
string IRGenerator::generateGetter(VariableDeclaration const& _varDecl)
|
|
|
|
{
|
2020-05-13 17:48:31 +00:00
|
|
|
string functionName = IRNames::function(_varDecl);
|
2019-07-08 20:15:59 +00:00
|
|
|
|
|
|
|
Type const* type = _varDecl.annotation().type;
|
|
|
|
|
|
|
|
solAssert(_varDecl.isStateVariable(), "");
|
|
|
|
|
2020-02-04 13:59:33 +00:00
|
|
|
if (auto const* mappingType = dynamic_cast<MappingType const*>(type))
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_context.functionCollector().createFunction(functionName, [&]() {
|
2020-05-07 16:18:40 +00:00
|
|
|
solAssert(!_varDecl.isConstant() && !_varDecl.immutable(), "");
|
2020-02-04 13:59:33 +00:00
|
|
|
pair<u256, unsigned> slot_offset = m_context.storageLocationOfVariable(_varDecl);
|
|
|
|
solAssert(slot_offset.second == 0, "");
|
|
|
|
FunctionType funType(_varDecl);
|
|
|
|
solUnimplementedAssert(funType.returnParameterTypes().size() == 1, "");
|
|
|
|
TypePointer returnType = funType.returnParameterTypes().front();
|
|
|
|
unsigned num_keys = 0;
|
|
|
|
stringstream indexAccesses;
|
|
|
|
string slot = m_context.newYulVariable();
|
|
|
|
do
|
|
|
|
{
|
|
|
|
solUnimplementedAssert(
|
|
|
|
mappingType->keyType()->sizeOnStack() == 1,
|
|
|
|
"Multi-slot mapping key unimplemented - might not be a problem"
|
|
|
|
);
|
|
|
|
indexAccesses <<
|
|
|
|
slot <<
|
|
|
|
" := " <<
|
|
|
|
m_utils.mappingIndexAccessFunction(*mappingType, *mappingType->keyType()) <<
|
|
|
|
"(" <<
|
|
|
|
slot;
|
|
|
|
if (mappingType->keyType()->sizeOnStack() > 0)
|
|
|
|
indexAccesses <<
|
|
|
|
", " <<
|
|
|
|
suffixedVariableNameList("key", num_keys, num_keys + mappingType->keyType()->sizeOnStack());
|
|
|
|
indexAccesses << ")\n";
|
|
|
|
num_keys += mappingType->keyType()->sizeOnStack();
|
|
|
|
}
|
|
|
|
while ((mappingType = dynamic_cast<MappingType const*>(mappingType->valueType())));
|
2019-07-08 20:15:59 +00:00
|
|
|
|
2020-02-04 13:59:33 +00:00
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>(<keys>) -> rval {
|
|
|
|
let <slot> := <base>
|
|
|
|
<indexAccesses>
|
|
|
|
rval := <readStorage>(<slot>)
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("keys", suffixedVariableNameList("key", 0, num_keys))
|
|
|
|
("readStorage", m_utils.readFromStorage(*returnType, 0, false))
|
|
|
|
("indexAccesses", indexAccesses.str())
|
|
|
|
("slot", slot)
|
|
|
|
("base", slot_offset.first.str())
|
|
|
|
.render();
|
|
|
|
});
|
|
|
|
else
|
|
|
|
{
|
|
|
|
solUnimplementedAssert(type->isValueType(), "");
|
2019-07-08 20:15:59 +00:00
|
|
|
|
2020-03-02 17:08:19 +00:00
|
|
|
return m_context.functionCollector().createFunction(functionName, [&]() {
|
2020-04-02 18:06:52 +00:00
|
|
|
if (_varDecl.immutable())
|
|
|
|
{
|
|
|
|
solUnimplementedAssert(type->sizeOnStack() == 1, "");
|
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>() -> rval {
|
|
|
|
rval := loadimmutable("<id>")
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("id", to_string(_varDecl.id()))
|
|
|
|
.render();
|
|
|
|
}
|
2020-04-20 21:05:14 +00:00
|
|
|
else if (_varDecl.isConstant())
|
|
|
|
return Whiskers(R"(
|
|
|
|
function <functionName>() -> <ret> {
|
|
|
|
<ret> := <constantValueFunction>()
|
|
|
|
}
|
|
|
|
)")
|
|
|
|
("functionName", functionName)
|
|
|
|
("constantValueFunction", IRGeneratorForStatements(m_context, m_utils).constantValueFunction(_varDecl))
|
|
|
|
("ret", suffixedVariableNameList("ret_", 0, _varDecl.type()->sizeOnStack()))
|
|
|
|
.render();
|
2020-04-02 18:06:52 +00:00
|
|
|
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();
|
|
|
|
}
|
2020-02-04 13:59:33 +00:00
|
|
|
});
|
|
|
|
}
|
2019-07-08 20:15:59 +00:00
|
|
|
}
|
|
|
|
|
2020-03-02 20:42:46 +00:00
|
|
|
string IRGenerator::generateInitialAssignment(VariableDeclaration const& _varDecl)
|
|
|
|
{
|
|
|
|
IRGeneratorForStatements generator(m_context, m_utils);
|
|
|
|
generator.initializeLocalVar(_varDecl);
|
|
|
|
return generator.code();
|
|
|
|
}
|
|
|
|
|
2020-05-07 15:29:42 +00:00
|
|
|
pair<string, map<ContractDefinition const*, vector<string>>> IRGenerator::evaluateConstructorArguments(
|
2020-04-06 15:26:59 +00:00
|
|
|
ContractDefinition const& _contract
|
|
|
|
)
|
2019-03-04 22:26:46 +00:00
|
|
|
{
|
2020-05-07 15:29:42 +00:00
|
|
|
map<ContractDefinition const*, vector<string>> constructorParams;
|
2020-04-06 15:26:59 +00:00
|
|
|
vector<pair<ContractDefinition const*, std::vector<ASTPointer<Expression>>const *>> baseConstructorArguments;
|
|
|
|
|
|
|
|
for (ASTPointer<InheritanceSpecifier> const& base: _contract.baseContracts())
|
|
|
|
if (FunctionDefinition const* baseConstructor = dynamic_cast<ContractDefinition const*>(
|
2020-05-04 18:38:22 +00:00
|
|
|
base->name().annotation().referencedDeclaration
|
2020-05-07 15:29:42 +00:00
|
|
|
)->constructor(); baseConstructor && base->arguments())
|
2020-04-06 15:26:59 +00:00
|
|
|
baseConstructorArguments.emplace_back(
|
2020-05-04 18:38:22 +00:00
|
|
|
dynamic_cast<ContractDefinition const*>(baseConstructor->scope()),
|
|
|
|
base->arguments()
|
2020-04-06 15:26:59 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if (FunctionDefinition const* constructor = _contract.constructor())
|
2020-05-04 18:38:22 +00:00
|
|
|
for (ASTPointer<ModifierInvocation> const& modifier: constructor->modifiers())
|
|
|
|
if (auto const* baseContract = dynamic_cast<ContractDefinition const*>(
|
2020-04-06 15:26:59 +00:00
|
|
|
modifier->name()->annotation().referencedDeclaration
|
2020-05-04 18:38:22 +00:00
|
|
|
))
|
|
|
|
if (
|
|
|
|
FunctionDefinition const* baseConstructor = baseContract->constructor();
|
|
|
|
baseConstructor && modifier->arguments()
|
|
|
|
)
|
|
|
|
baseConstructorArguments.emplace_back(
|
|
|
|
dynamic_cast<ContractDefinition const*>(baseConstructor->scope()),
|
|
|
|
modifier->arguments()
|
|
|
|
);
|
2019-05-20 10:01:47 +00:00
|
|
|
|
2020-04-06 15:26:59 +00:00
|
|
|
IRGeneratorForStatements generator{m_context, m_utils};
|
|
|
|
for (auto&& [baseContract, arguments]: baseConstructorArguments)
|
2019-05-20 10:01:47 +00:00
|
|
|
{
|
2020-04-06 15:26:59 +00:00
|
|
|
solAssert(baseContract && arguments, "");
|
|
|
|
if (baseContract->constructor() && !arguments->empty())
|
|
|
|
{
|
|
|
|
vector<string> params;
|
|
|
|
for (size_t i = 0; i < arguments->size(); ++i)
|
2020-05-07 15:29:42 +00:00
|
|
|
params += generator.evaluateExpression(
|
|
|
|
*(arguments->at(i)),
|
|
|
|
*(baseContract->constructor()->parameters()[i]->type())
|
|
|
|
).stackSlots();
|
|
|
|
constructorParams[baseContract] = std::move(params);
|
2020-04-06 15:26:59 +00:00
|
|
|
}
|
2019-05-20 10:01:47 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 15:26:59 +00:00
|
|
|
return {generator.code(), constructorParams};
|
|
|
|
}
|
2019-05-20 10:01:47 +00:00
|
|
|
|
2020-04-06 15:26:59 +00:00
|
|
|
string IRGenerator::initStateVariables(ContractDefinition const& _contract)
|
|
|
|
{
|
|
|
|
IRGeneratorForStatements generator{m_context, m_utils};
|
|
|
|
for (VariableDeclaration const* variable: _contract.stateVariables())
|
2020-04-02 18:06:52 +00:00
|
|
|
if (!variable->isConstant())
|
2020-04-06 15:26:59 +00:00
|
|
|
generator.initializeStateVar(*variable);
|
2020-03-23 15:04:36 +00:00
|
|
|
|
2020-04-06 15:26:59 +00:00
|
|
|
return generator.code();
|
|
|
|
}
|
2020-03-23 15:04:36 +00:00
|
|
|
|
2020-04-09 19:59:17 +00:00
|
|
|
|
2020-04-06 15:26:59 +00:00
|
|
|
void IRGenerator::generateImplicitConstructors(ContractDefinition const& _contract)
|
|
|
|
{
|
|
|
|
auto listAllParams = [&](
|
2020-05-07 15:29:42 +00:00
|
|
|
map<ContractDefinition const*, vector<string>> const& baseParams) -> vector<string>
|
2020-04-06 15:26:59 +00:00
|
|
|
{
|
|
|
|
vector<string> params;
|
|
|
|
for (ContractDefinition const* contract: _contract.annotation().linearizedBaseContracts)
|
|
|
|
if (baseParams.count(contract))
|
2020-05-07 15:29:42 +00:00
|
|
|
params += baseParams.at(contract);
|
|
|
|
return params;
|
2020-04-06 15:26:59 +00:00
|
|
|
};
|
|
|
|
|
2020-05-07 15:29:42 +00:00
|
|
|
map<ContractDefinition const*, vector<string>> baseConstructorParams;
|
2020-04-06 15:26:59 +00:00
|
|
|
for (size_t i = 0; i < _contract.annotation().linearizedBaseContracts.size(); ++i)
|
|
|
|
{
|
|
|
|
ContractDefinition const* contract = _contract.annotation().linearizedBaseContracts[i];
|
|
|
|
baseConstructorParams.erase(contract);
|
|
|
|
|
|
|
|
m_context.functionCollector().createFunction(implicitConstructorName(*contract), [&]() {
|
|
|
|
Whiskers t(R"(
|
|
|
|
function <functionName>(<params><comma><baseParams>) {
|
|
|
|
<evalBaseArguments>
|
|
|
|
<?hasNextConstructor> <nextConstructor>(<nextParams>) </hasNextConstructor>
|
|
|
|
<initStateVariables>
|
|
|
|
<userDefinedConstructorBody>
|
|
|
|
}
|
|
|
|
)");
|
2020-05-07 15:29:42 +00:00
|
|
|
vector<string> params;
|
2020-04-06 15:26:59 +00:00
|
|
|
if (contract->constructor())
|
|
|
|
for (ASTPointer<VariableDeclaration> const& varDecl: contract->constructor()->parameters())
|
2020-05-07 15:29:42 +00:00
|
|
|
params += m_context.addLocalVariable(*varDecl).stackSlots();
|
|
|
|
t("params", joinHumanReadable(params));
|
|
|
|
vector<string> baseParams = listAllParams(baseConstructorParams);
|
|
|
|
t("baseParams", joinHumanReadable(baseParams));
|
|
|
|
t("comma", !params.empty() && !baseParams.empty() ? ", " : "");
|
2020-04-06 15:26:59 +00:00
|
|
|
t("functionName", implicitConstructorName(*contract));
|
2020-05-07 15:29:42 +00:00
|
|
|
pair<string, map<ContractDefinition const*, vector<string>>> evaluatedArgs = evaluateConstructorArguments(*contract);
|
2020-04-06 15:26:59 +00:00
|
|
|
baseConstructorParams.insert(evaluatedArgs.second.begin(), evaluatedArgs.second.end());
|
|
|
|
t("evalBaseArguments", evaluatedArgs.first);
|
|
|
|
if (i < _contract.annotation().linearizedBaseContracts.size() - 1)
|
|
|
|
{
|
|
|
|
t("hasNextConstructor", true);
|
|
|
|
ContractDefinition const* nextContract = _contract.annotation().linearizedBaseContracts[i + 1];
|
|
|
|
t("nextConstructor", implicitConstructorName(*nextContract));
|
2020-05-07 15:29:42 +00:00
|
|
|
t("nextParams", joinHumanReadable(listAllParams(baseConstructorParams)));
|
2020-04-06 15:26:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
t("hasNextConstructor", false);
|
|
|
|
t("initStateVariables", initStateVariables(*contract));
|
|
|
|
t("userDefinedConstructorBody", contract->constructor() ? generate(contract->constructor()->body()) : "");
|
2020-03-23 15:04:36 +00:00
|
|
|
|
2020-04-06 15:26:59 +00:00
|
|
|
return t.render();
|
|
|
|
});
|
2019-05-09 12:42:35 +00:00
|
|
|
}
|
2019-03-04 22:26:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
string IRGenerator::deployCode(ContractDefinition const& _contract)
|
|
|
|
{
|
|
|
|
Whiskers t(R"X(
|
2020-04-02 18:06:52 +00:00
|
|
|
<#loadImmutables>
|
|
|
|
let <var> := mload(<memoryOffset>)
|
|
|
|
</loadImmutables>
|
|
|
|
|
2019-03-04 22:26:46 +00:00
|
|
|
codecopy(0, dataoffset("<object>"), datasize("<object>"))
|
2020-04-02 18:06:52 +00:00
|
|
|
|
|
|
|
<#storeImmutables>
|
|
|
|
setimmutable("<immutableName>", <var>)
|
|
|
|
</storeImmutables>
|
|
|
|
|
2019-03-04 22:26:46 +00:00
|
|
|
return(0, datasize("<object>"))
|
|
|
|
)X");
|
2020-05-13 17:48:31 +00:00
|
|
|
t("object", IRNames::runtimeObject(_contract));
|
2020-04-02 18:06:52 +00:00
|
|
|
|
|
|
|
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));
|
2019-03-04 22:26:46 +00:00
|
|
|
return t.render();
|
|
|
|
}
|
|
|
|
|
|
|
|
string IRGenerator::callValueCheck()
|
|
|
|
{
|
|
|
|
return "if callvalue() { revert(0, 0) }";
|
|
|
|
}
|
|
|
|
|
2020-04-06 15:26:59 +00:00
|
|
|
string IRGenerator::implicitConstructorName(ContractDefinition const& _contract)
|
|
|
|
{
|
|
|
|
return "constructor_" + _contract.name() + "_" + to_string(_contract.id());
|
|
|
|
}
|
|
|
|
|
2019-03-04 22:26:46 +00:00
|
|
|
string IRGenerator::dispatchRoutine(ContractDefinition const& _contract)
|
|
|
|
{
|
|
|
|
Whiskers t(R"X(
|
|
|
|
if iszero(lt(calldatasize(), 4))
|
|
|
|
{
|
|
|
|
let selector := <shr224>(calldataload(0))
|
|
|
|
switch selector
|
|
|
|
<#cases>
|
|
|
|
case <functionSelector>
|
|
|
|
{
|
|
|
|
// <functionName>
|
|
|
|
<callValueCheck>
|
2020-05-06 23:55:30 +00:00
|
|
|
<?+params>let <params> := </+params> <abiDecode>(4, calldatasize())
|
|
|
|
<?+retParams>let <retParams> := </+retParams> <function>(<params>)
|
2019-03-04 22:26:46 +00:00
|
|
|
let memPos := <allocate>(0)
|
2020-05-06 23:55:30 +00:00
|
|
|
let memEnd := <abiEncode>(memPos <?+retParams>,</+retParams> <retParams>)
|
2019-03-04 22:26:46 +00:00
|
|
|
return(memPos, sub(memEnd, memPos))
|
|
|
|
}
|
|
|
|
</cases>
|
|
|
|
default {}
|
|
|
|
}
|
2019-09-09 16:22:02 +00:00
|
|
|
if iszero(calldatasize()) { <receiveEther> }
|
2019-03-04 22:26:46 +00:00
|
|
|
<fallback>
|
|
|
|
)X");
|
|
|
|
t("shr224", m_utils.shiftRightFunction(224));
|
|
|
|
vector<map<string, string>> functions;
|
|
|
|
for (auto const& function: _contract.interfaceFunctions())
|
|
|
|
{
|
2020-04-01 02:39:38 +00:00
|
|
|
functions.emplace_back();
|
2019-03-04 22:26:46 +00:00
|
|
|
map<string, string>& templ = functions.back();
|
|
|
|
templ["functionSelector"] = "0x" + function.first.hex();
|
|
|
|
FunctionTypePointer const& type = function.second;
|
|
|
|
templ["functionName"] = type->externalSignature();
|
|
|
|
templ["callValueCheck"] = type->isPayable() ? "" : callValueCheck();
|
|
|
|
|
|
|
|
unsigned paramVars = make_shared<TupleType>(type->parameterTypes())->sizeOnStack();
|
|
|
|
unsigned retVars = make_shared<TupleType>(type->returnParameterTypes())->sizeOnStack();
|
|
|
|
|
2020-01-22 14:48:56 +00:00
|
|
|
ABIFunctions abiFunctions(m_evmVersion, m_context.revertStrings(), m_context.functionCollector());
|
2019-03-04 22:26:46 +00:00
|
|
|
templ["abiDecode"] = abiFunctions.tupleDecoder(type->parameterTypes());
|
2019-07-05 15:15:38 +00:00
|
|
|
templ["params"] = suffixedVariableNameList("param_", 0, paramVars);
|
2020-05-06 23:55:30 +00:00
|
|
|
templ["retParams"] = suffixedVariableNameList("ret_", 0, retVars);
|
2019-07-08 20:15:59 +00:00
|
|
|
|
|
|
|
if (FunctionDefinition const* funDef = dynamic_cast<FunctionDefinition const*>(&type->declaration()))
|
2020-04-10 17:16:44 +00:00
|
|
|
templ["function"] = m_context.enqueueFunctionForCodeGeneration(*funDef);
|
2019-07-08 20:15:59 +00:00
|
|
|
else if (VariableDeclaration const* varDecl = dynamic_cast<VariableDeclaration const*>(&type->declaration()))
|
|
|
|
templ["function"] = generateGetter(*varDecl);
|
|
|
|
else
|
|
|
|
solAssert(false, "Unexpected declaration for function!");
|
|
|
|
|
2019-03-04 22:26:46 +00:00
|
|
|
templ["allocate"] = m_utils.allocationFunction();
|
|
|
|
templ["abiEncode"] = abiFunctions.tupleEncoder(type->returnParameterTypes(), type->returnParameterTypes(), false);
|
|
|
|
}
|
|
|
|
t("cases", functions);
|
|
|
|
if (FunctionDefinition const* fallback = _contract.fallbackFunction())
|
|
|
|
{
|
|
|
|
string fallbackCode;
|
|
|
|
if (!fallback->isPayable())
|
|
|
|
fallbackCode += callValueCheck();
|
2020-04-10 17:16:44 +00:00
|
|
|
fallbackCode += m_context.enqueueFunctionForCodeGeneration(*fallback) + "() stop()";
|
2019-03-04 22:26:46 +00:00
|
|
|
|
|
|
|
t("fallback", fallbackCode);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
t("fallback", "revert(0, 0)");
|
2019-09-09 16:22:02 +00:00
|
|
|
if (FunctionDefinition const* etherReceiver = _contract.receiveFunction())
|
2020-04-10 17:16:44 +00:00
|
|
|
t("receiveEther", m_context.enqueueFunctionForCodeGeneration(*etherReceiver) + "() stop()");
|
2019-09-09 16:22:02 +00:00
|
|
|
else
|
|
|
|
t("receiveEther", "");
|
2019-03-04 22:26:46 +00:00
|
|
|
return t.render();
|
|
|
|
}
|
|
|
|
|
|
|
|
string IRGenerator::memoryInit()
|
|
|
|
{
|
|
|
|
// This function should be called at the beginning of the EVM call frame
|
|
|
|
// and thus can assume all memory to be zero, including the contents of
|
|
|
|
// the "zero memory area" (the position CompilerUtils::zeroPointer points to).
|
|
|
|
return
|
2020-04-02 18:06:52 +00:00
|
|
|
Whiskers{"mstore(<memPtr>, <freeMemoryStart>)"}
|
2019-03-04 22:26:46 +00:00
|
|
|
("memPtr", to_string(CompilerUtils::freeMemoryPointer))
|
2020-04-02 18:06:52 +00:00
|
|
|
("freeMemoryStart", to_string(CompilerUtils::generalPurposeMemoryStart + m_context.reservedMemory()))
|
2019-03-04 22:26:46 +00:00
|
|
|
.render();
|
|
|
|
}
|
|
|
|
|
2019-04-30 16:32:56 +00:00
|
|
|
void IRGenerator::resetContext(ContractDefinition const& _contract)
|
2019-03-04 22:26:46 +00:00
|
|
|
{
|
2020-04-10 17:16:44 +00:00
|
|
|
solAssert(
|
|
|
|
m_context.functionGenerationQueueEmpty(),
|
|
|
|
"Reset function generation queue while it still had functions."
|
|
|
|
);
|
2019-03-04 22:26:46 +00:00
|
|
|
solAssert(
|
2020-03-02 17:08:19 +00:00
|
|
|
m_context.functionCollector().requestedFunctions().empty(),
|
2019-03-04 22:26:46 +00:00
|
|
|
"Reset context while it still had functions."
|
|
|
|
);
|
2020-01-22 14:48:56 +00:00
|
|
|
m_context = IRGenerationContext(m_evmVersion, m_context.revertStrings(), m_optimiserSettings);
|
2019-04-30 16:32:56 +00:00
|
|
|
|
2020-03-24 17:25:59 +00:00
|
|
|
m_context.setMostDerivedContract(_contract);
|
2019-04-30 16:32:56 +00:00
|
|
|
for (auto const& var: ContractType(_contract).stateVariables())
|
|
|
|
m_context.addStateVariable(*get<0>(var), get<1>(var), get<2>(var));
|
2019-03-04 22:26:46 +00:00
|
|
|
}
|