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
|
|
|
|
|
|
|
#include <libdevcore/CommonData.h>
|
|
|
|
#include <libdevcore/Whiskers.h>
|
|
|
|
#include <libdevcore/StringUtils.h>
|
|
|
|
|
|
|
|
#include <liblangutil/SourceReferenceFormatter.h>
|
|
|
|
|
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
2019-05-20 10:01:47 +00:00
|
|
|
#include <boost/range/adaptor/reversed.hpp>
|
|
|
|
|
|
|
|
#include <sstream>
|
2019-03-04 22:26:46 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace dev;
|
|
|
|
using namespace dev::solidity;
|
|
|
|
|
|
|
|
pair<string, string> IRGenerator::run(ContractDefinition const& _contract)
|
|
|
|
{
|
2019-06-21 15:33:35 +00:00
|
|
|
string const ir = yul::reindent(generate(_contract));
|
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()};
|
|
|
|
}
|
|
|
|
|
2019-03-18 10:21:41 +00:00
|
|
|
string IRGenerator::generate(ContractDefinition const& _contract)
|
2019-03-04 22:26:46 +00:00
|
|
|
{
|
2019-05-09 12:42:35 +00:00
|
|
|
solUnimplementedAssert(!_contract.isLibrary(), "Libraries not yet implemented.");
|
|
|
|
|
2019-03-04 22:26:46 +00:00
|
|
|
Whiskers t(R"(
|
|
|
|
object "<CreationObject>" {
|
|
|
|
code {
|
|
|
|
<memoryInit>
|
|
|
|
<constructor>
|
|
|
|
<deploy>
|
|
|
|
<functions>
|
|
|
|
}
|
|
|
|
object "<RuntimeObject>" {
|
|
|
|
code {
|
|
|
|
<memoryInit>
|
|
|
|
<dispatch>
|
|
|
|
<runtimeFunctions>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)");
|
|
|
|
|
2019-04-30 16:32:56 +00:00
|
|
|
resetContext(_contract);
|
2019-05-09 12:42:35 +00:00
|
|
|
|
2019-03-04 22:26:46 +00:00
|
|
|
t("CreationObject", creationObjectName(_contract));
|
|
|
|
t("memoryInit", memoryInit());
|
2019-05-09 12:42:35 +00:00
|
|
|
t("constructor", constructorCode(_contract));
|
2019-03-04 22:26:46 +00:00
|
|
|
t("deploy", deployCode(_contract));
|
2019-05-09 10:00:23 +00:00
|
|
|
// We generate code for all functions and rely on the optimizer to remove them again
|
|
|
|
// TODO it would probably be better to only generate functions when internalDispatch or
|
|
|
|
// virtualFunctionName is called - same below.
|
|
|
|
for (auto const* contract: _contract.annotation().linearizedBaseContracts)
|
|
|
|
for (auto const* fun: contract->definedFunctions())
|
|
|
|
generateFunction(*fun);
|
2019-03-04 22:26:46 +00:00
|
|
|
t("functions", m_context.functionCollector()->requestedFunctions());
|
|
|
|
|
2019-04-30 16:32:56 +00:00
|
|
|
resetContext(_contract);
|
2019-04-02 10:37:48 +00:00
|
|
|
m_context.setInheritanceHierarchy(_contract.annotation().linearizedBaseContracts);
|
2019-03-04 22:26:46 +00:00
|
|
|
t("RuntimeObject", runtimeObjectName(_contract));
|
|
|
|
t("dispatch", dispatchRoutine(_contract));
|
2019-05-09 10:00:23 +00:00
|
|
|
for (auto const* contract: _contract.annotation().linearizedBaseContracts)
|
|
|
|
for (auto const* fun: contract->definedFunctions())
|
|
|
|
generateFunction(*fun);
|
2019-03-04 22:26:46 +00:00
|
|
|
t("runtimeFunctions", m_context.functionCollector()->requestedFunctions());
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
string IRGenerator::generateFunction(FunctionDefinition const& _function)
|
2019-03-04 22:26:46 +00:00
|
|
|
{
|
2019-04-02 10:37:48 +00:00
|
|
|
string functionName = m_context.functionName(_function);
|
2019-03-04 22:26:46 +00:00
|
|
|
return m_context.functionCollector()->createFunction(functionName, [&]() {
|
2019-04-24 21:48:12 +00:00
|
|
|
Whiskers t(R"(
|
|
|
|
function <functionName>(<params>) <returns> {
|
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);
|
|
|
|
string params;
|
|
|
|
for (auto const& varDecl: _function.parameters())
|
|
|
|
params += (params.empty() ? "" : ", ") + m_context.addLocalVariable(*varDecl);
|
|
|
|
t("params", params);
|
|
|
|
string retParams;
|
|
|
|
for (auto const& varDecl: _function.returnParameters())
|
|
|
|
retParams += (retParams.empty() ? "" : ", ") + m_context.addLocalVariable(*varDecl);
|
|
|
|
t("returns", retParams.empty() ? "" : " -> " + retParams);
|
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)
|
|
|
|
{
|
|
|
|
string functionName = m_context.functionName(_varDecl);
|
|
|
|
|
|
|
|
Type const* type = _varDecl.annotation().type;
|
|
|
|
|
|
|
|
solAssert(!_varDecl.isConstant(), "");
|
|
|
|
solAssert(_varDecl.isStateVariable(), "");
|
|
|
|
|
|
|
|
solUnimplementedAssert(type->isValueType(), "");
|
|
|
|
|
|
|
|
return m_context.functionCollector()->createFunction(functionName, [&]() {
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-09 12:42:35 +00:00
|
|
|
string IRGenerator::constructorCode(ContractDefinition const& _contract)
|
2019-03-04 22:26:46 +00:00
|
|
|
{
|
2019-05-20 10:01:47 +00:00
|
|
|
// Initialization of state variables in base-to-derived order.
|
|
|
|
solAssert(!_contract.isLibrary(), "Tried to initialize state variables of library.");
|
|
|
|
|
|
|
|
using boost::adaptors::reverse;
|
|
|
|
|
|
|
|
ostringstream out;
|
|
|
|
|
|
|
|
FunctionDefinition const* constructor = _contract.constructor();
|
|
|
|
if (constructor && !constructor->isPayable())
|
|
|
|
out << callValueCheck();
|
|
|
|
|
|
|
|
for (ContractDefinition const* contract: reverse(_contract.annotation().linearizedBaseContracts))
|
|
|
|
{
|
|
|
|
out <<
|
|
|
|
"\n// Begin state variable initialization for contract \"" <<
|
|
|
|
contract->name() <<
|
|
|
|
"\" (" <<
|
|
|
|
contract->stateVariables().size() <<
|
|
|
|
" variables)\n";
|
|
|
|
|
|
|
|
IRGeneratorForStatements generator{m_context, m_utils};
|
|
|
|
for (VariableDeclaration const* variable: contract->stateVariables())
|
|
|
|
if (!variable->isConstant())
|
|
|
|
generator.initializeStateVar(*variable);
|
|
|
|
out << generator.code();
|
|
|
|
|
|
|
|
out << "// End state variable initialization for contract \"" << contract->name() << "\".\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (constructor)
|
2019-05-09 12:42:35 +00:00
|
|
|
{
|
|
|
|
solUnimplementedAssert(constructor->parameters().empty(), "");
|
2019-05-20 10:01:47 +00:00
|
|
|
|
|
|
|
// TODO base constructors
|
|
|
|
|
|
|
|
out << m_context.functionName(*constructor) + "()\n";
|
2019-05-09 12:42:35 +00:00
|
|
|
}
|
2019-03-04 22:26:46 +00:00
|
|
|
|
2019-05-20 10:01:47 +00:00
|
|
|
return out.str();
|
2019-03-04 22:26:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
string IRGenerator::deployCode(ContractDefinition const& _contract)
|
|
|
|
{
|
|
|
|
Whiskers t(R"X(
|
|
|
|
codecopy(0, dataoffset("<object>"), datasize("<object>"))
|
|
|
|
return(0, datasize("<object>"))
|
|
|
|
)X");
|
|
|
|
t("object", runtimeObjectName(_contract));
|
|
|
|
return t.render();
|
|
|
|
}
|
|
|
|
|
|
|
|
string IRGenerator::callValueCheck()
|
|
|
|
{
|
|
|
|
return "if callvalue() { revert(0, 0) }";
|
|
|
|
}
|
|
|
|
|
|
|
|
string IRGenerator::creationObjectName(ContractDefinition const& _contract)
|
|
|
|
{
|
|
|
|
return _contract.name() + "_" + to_string(_contract.id());
|
|
|
|
}
|
|
|
|
|
|
|
|
string IRGenerator::runtimeObjectName(ContractDefinition const& _contract)
|
|
|
|
{
|
|
|
|
return _contract.name() + "_" + to_string(_contract.id()) + "_deployed";
|
|
|
|
}
|
|
|
|
|
|
|
|
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>
|
|
|
|
<assignToParams> <abiDecode>(4, calldatasize())
|
|
|
|
<assignToRetParams> <function>(<params>)
|
|
|
|
let memPos := <allocate>(0)
|
|
|
|
let memEnd := <abiEncode>(memPos <comma> <retParams>)
|
|
|
|
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())
|
|
|
|
{
|
|
|
|
functions.push_back({});
|
|
|
|
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();
|
2019-07-05 15:15:38 +00:00
|
|
|
templ["assignToParams"] = paramVars == 0 ? "" : "let " + suffixedVariableNameList("param_", 0, paramVars) + " := ";
|
|
|
|
templ["assignToRetParams"] = retVars == 0 ? "" : "let " + suffixedVariableNameList("ret_", 0, retVars) + " := ";
|
2019-03-04 22:26:46 +00:00
|
|
|
|
|
|
|
ABIFunctions abiFunctions(m_evmVersion, m_context.functionCollector());
|
|
|
|
templ["abiDecode"] = abiFunctions.tupleDecoder(type->parameterTypes());
|
2019-07-05 15:15:38 +00:00
|
|
|
templ["params"] = suffixedVariableNameList("param_", 0, paramVars);
|
|
|
|
templ["retParams"] = suffixedVariableNameList("ret_", retVars, 0);
|
2019-07-08 20:15:59 +00:00
|
|
|
|
|
|
|
if (FunctionDefinition const* funDef = dynamic_cast<FunctionDefinition const*>(&type->declaration()))
|
|
|
|
templ["function"] = generateFunction(*funDef);
|
|
|
|
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);
|
|
|
|
templ["comma"] = retVars == 0 ? "" : ", ";
|
|
|
|
}
|
|
|
|
t("cases", functions);
|
|
|
|
if (FunctionDefinition const* fallback = _contract.fallbackFunction())
|
|
|
|
{
|
|
|
|
string fallbackCode;
|
|
|
|
if (!fallback->isPayable())
|
|
|
|
fallbackCode += callValueCheck();
|
2019-03-18 10:21:41 +00:00
|
|
|
fallbackCode += generateFunction(*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())
|
|
|
|
t("receiveEther", generateFunction(*etherReceiver) + "() stop()");
|
|
|
|
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
|
|
|
|
Whiskers{"mstore(<memPtr>, <generalPurposeStart>)"}
|
|
|
|
("memPtr", to_string(CompilerUtils::freeMemoryPointer))
|
|
|
|
("generalPurposeStart", to_string(CompilerUtils::generalPurposeMemoryStart))
|
|
|
|
.render();
|
|
|
|
}
|
|
|
|
|
2019-04-30 16:32:56 +00:00
|
|
|
void IRGenerator::resetContext(ContractDefinition const& _contract)
|
2019-03-04 22:26:46 +00:00
|
|
|
{
|
|
|
|
solAssert(
|
|
|
|
m_context.functionCollector()->requestedFunctions().empty(),
|
|
|
|
"Reset context while it still had functions."
|
|
|
|
);
|
|
|
|
m_context = IRGenerationContext(m_evmVersion, m_optimiserSettings);
|
|
|
|
m_utils = YulUtilFunctions(m_evmVersion, m_context.functionCollector());
|
2019-04-30 16:32:56 +00:00
|
|
|
|
|
|
|
m_context.setInheritanceHierarchy(_contract.annotation().linearizedBaseContracts);
|
|
|
|
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
|
|
|
}
|