2014-10-20 10:41:56 +00:00
|
|
|
/*
|
|
|
|
This file is part of cpp-ethereum.
|
|
|
|
|
|
|
|
cpp-ethereum 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.
|
|
|
|
|
|
|
|
cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @author Christian <c@ethdev.com>
|
|
|
|
* @date 2014
|
2014-10-30 00:20:32 +00:00
|
|
|
* Solidity compiler.
|
2014-10-20 10:41:56 +00:00
|
|
|
*/
|
|
|
|
|
2014-10-30 00:20:32 +00:00
|
|
|
#include <algorithm>
|
2015-01-15 19:04:24 +00:00
|
|
|
#include <boost/range/adaptor/reversed.hpp>
|
2014-11-07 01:06:37 +00:00
|
|
|
#include <libevmcore/Instruction.h>
|
|
|
|
#include <libevmcore/Assembly.h>
|
2014-10-20 10:41:56 +00:00
|
|
|
#include <libsolidity/AST.h>
|
|
|
|
#include <libsolidity/Compiler.h>
|
2014-10-30 00:20:32 +00:00
|
|
|
#include <libsolidity/ExpressionCompiler.h>
|
2014-12-08 17:52:30 +00:00
|
|
|
#include <libsolidity/CompilerUtils.h>
|
2014-10-20 10:41:56 +00:00
|
|
|
|
2014-10-30 00:20:32 +00:00
|
|
|
using namespace std;
|
2014-10-20 10:41:56 +00:00
|
|
|
|
|
|
|
namespace dev {
|
|
|
|
namespace solidity {
|
|
|
|
|
2015-02-22 17:38:32 +00:00
|
|
|
/**
|
|
|
|
* Simple helper class to ensure that the stack height is the same at certain places in the code.
|
|
|
|
*/
|
|
|
|
class StackHeightChecker
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
StackHeightChecker(CompilerContext const& _context):
|
|
|
|
m_context(_context), stackHeight(m_context.getStackHeight()) {}
|
|
|
|
void check() { solAssert(m_context.getStackHeight() == stackHeight, "I sense a disturbance in the stack."); }
|
|
|
|
private:
|
|
|
|
CompilerContext const& m_context;
|
|
|
|
unsigned stackHeight;
|
|
|
|
};
|
|
|
|
|
2015-01-16 17:52:27 +00:00
|
|
|
void Compiler::compileContract(ContractDefinition const& _contract,
|
2014-12-12 15:49:26 +00:00
|
|
|
map<ContractDefinition const*, bytes const*> const& _contracts)
|
2014-10-20 10:41:56 +00:00
|
|
|
{
|
2014-10-30 00:20:32 +00:00
|
|
|
m_context = CompilerContext(); // clear it just in case
|
2015-01-16 17:52:27 +00:00
|
|
|
initializeContext(_contract, _contracts);
|
2014-11-19 09:24:22 +00:00
|
|
|
appendFunctionSelector(_contract);
|
2015-01-27 13:32:59 +00:00
|
|
|
set<Declaration const*> functions = m_context.getFunctionsWithoutCode();
|
|
|
|
while (!functions.empty())
|
2015-01-26 15:58:54 +00:00
|
|
|
{
|
2015-01-27 13:32:59 +00:00
|
|
|
for (Declaration const* function: functions)
|
2015-02-27 16:41:22 +00:00
|
|
|
{
|
|
|
|
m_context.setStackOffset(0);
|
2015-01-27 13:32:59 +00:00
|
|
|
function->accept(*this);
|
2015-02-27 16:41:22 +00:00
|
|
|
}
|
2015-01-27 13:32:59 +00:00
|
|
|
functions = m_context.getFunctionsWithoutCode();
|
2015-01-26 15:58:54 +00:00
|
|
|
}
|
|
|
|
|
2014-12-15 21:57:39 +00:00
|
|
|
// Swap the runtime context with the creation-time context
|
2015-01-13 14:59:42 +00:00
|
|
|
swap(m_context, m_runtimeContext);
|
2015-01-16 17:52:27 +00:00
|
|
|
initializeContext(_contract, _contracts);
|
2015-01-13 14:59:42 +00:00
|
|
|
packIntoContractCreator(_contract, m_runtimeContext);
|
2014-12-15 21:57:39 +00:00
|
|
|
}
|
2014-11-19 09:24:22 +00:00
|
|
|
|
2015-01-16 17:52:27 +00:00
|
|
|
void Compiler::initializeContext(ContractDefinition const& _contract,
|
2014-12-15 21:57:39 +00:00
|
|
|
map<ContractDefinition const*, bytes const*> const& _contracts)
|
|
|
|
{
|
|
|
|
m_context.setCompiledContracts(_contracts);
|
2015-01-27 13:32:59 +00:00
|
|
|
m_context.setInheritanceHierarchy(_contract.getLinearizedBaseContracts());
|
2014-11-19 09:24:22 +00:00
|
|
|
registerStateVariables(_contract);
|
2015-02-24 11:08:51 +00:00
|
|
|
m_context.resetVisitedNodes(&_contract);
|
2014-12-15 21:57:39 +00:00
|
|
|
}
|
2014-11-19 09:24:22 +00:00
|
|
|
|
2014-12-15 21:57:39 +00:00
|
|
|
void Compiler::packIntoContractCreator(ContractDefinition const& _contract, CompilerContext const& _runtimeContext)
|
|
|
|
{
|
2015-01-27 13:32:59 +00:00
|
|
|
// Determine the arguments that are used for the base constructors.
|
|
|
|
std::vector<ContractDefinition const*> const& bases = _contract.getLinearizedBaseContracts();
|
2015-01-19 22:08:48 +00:00
|
|
|
for (ContractDefinition const* contract: bases)
|
2015-02-27 16:41:22 +00:00
|
|
|
{
|
|
|
|
if (FunctionDefinition const* constructor = contract->getConstructor())
|
|
|
|
for (auto const& modifier: constructor->getModifiers())
|
|
|
|
{
|
|
|
|
auto baseContract = dynamic_cast<ContractDefinition const*>(
|
|
|
|
modifier->getName()->getReferencedDeclaration());
|
|
|
|
if (baseContract)
|
|
|
|
if (m_baseArguments.count(baseContract->getConstructor()) == 0)
|
|
|
|
m_baseArguments[baseContract->getConstructor()] = &modifier->getArguments();
|
|
|
|
}
|
|
|
|
|
2015-01-19 22:08:48 +00:00
|
|
|
for (ASTPointer<InheritanceSpecifier> const& base: contract->getBaseContracts())
|
|
|
|
{
|
|
|
|
ContractDefinition const* baseContract = dynamic_cast<ContractDefinition const*>(
|
2015-02-17 15:21:38 +00:00
|
|
|
base->getName()->getReferencedDeclaration());
|
2015-01-19 22:08:48 +00:00
|
|
|
solAssert(baseContract, "");
|
2014-11-19 09:24:22 +00:00
|
|
|
|
2015-02-27 16:41:22 +00:00
|
|
|
if (m_baseArguments.count(baseContract->getConstructor()) == 0)
|
|
|
|
m_baseArguments[baseContract->getConstructor()] = &base->getArguments();
|
|
|
|
}
|
2015-01-19 22:08:48 +00:00
|
|
|
}
|
2015-02-27 16:41:22 +00:00
|
|
|
// Initialization of state variables in base-to-derived order.
|
|
|
|
for (ContractDefinition const* contract: boost::adaptors::reverse(bases))
|
|
|
|
initializeStateVariables(*contract);
|
|
|
|
|
|
|
|
if (FunctionDefinition const* constructor = _contract.getConstructor())
|
|
|
|
appendConstructor(*constructor);
|
|
|
|
else if (auto c = m_context.getNextConstructor(_contract))
|
|
|
|
appendBaseConstructor(*c);
|
2014-12-15 21:57:39 +00:00
|
|
|
|
|
|
|
eth::AssemblyItem sub = m_context.addSubroutine(_runtimeContext.getAssembly());
|
|
|
|
// stack contains sub size
|
2014-11-19 09:24:22 +00:00
|
|
|
m_context << eth::Instruction::DUP1 << sub << u256(0) << eth::Instruction::CODECOPY;
|
|
|
|
m_context << u256(0) << eth::Instruction::RETURN;
|
2014-12-15 21:57:39 +00:00
|
|
|
|
2015-01-27 13:32:59 +00:00
|
|
|
// note that we have to include the functions again because of absolute jump labels
|
|
|
|
set<Declaration const*> functions = m_context.getFunctionsWithoutCode();
|
|
|
|
while (!functions.empty())
|
|
|
|
{
|
|
|
|
for (Declaration const* function: functions)
|
|
|
|
function->accept(*this);
|
|
|
|
functions = m_context.getFunctionsWithoutCode();
|
|
|
|
}
|
2014-12-15 21:57:39 +00:00
|
|
|
}
|
|
|
|
|
2015-02-27 16:41:22 +00:00
|
|
|
void Compiler::appendBaseConstructor(FunctionDefinition const& _constructor)
|
2015-01-19 22:08:48 +00:00
|
|
|
{
|
2015-03-07 22:08:39 +00:00
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _constructor);
|
2015-01-19 22:08:48 +00:00
|
|
|
FunctionType constructorType(_constructor);
|
2015-02-27 16:41:22 +00:00
|
|
|
if (!constructorType.getParameterTypes().empty())
|
|
|
|
{
|
|
|
|
std::vector<ASTPointer<Expression>> const* arguments = m_baseArguments[&_constructor];
|
|
|
|
solAssert(arguments, "");
|
|
|
|
for (unsigned i = 0; i < arguments->size(); ++i)
|
|
|
|
compileExpression(*(arguments->at(i)), constructorType.getParameterTypes()[i]);
|
|
|
|
}
|
|
|
|
_constructor.accept(*this);
|
2015-01-19 22:08:48 +00:00
|
|
|
}
|
|
|
|
|
2015-02-27 16:41:22 +00:00
|
|
|
void Compiler::appendConstructor(FunctionDefinition const& _constructor)
|
2014-12-15 21:57:39 +00:00
|
|
|
{
|
2015-03-07 22:08:39 +00:00
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _constructor);
|
2014-12-15 21:57:39 +00:00
|
|
|
// copy constructor arguments from code to memory and then to stack, they are supplied after the actual program
|
|
|
|
unsigned argumentSize = 0;
|
|
|
|
for (ASTPointer<VariableDeclaration> const& var: _constructor.getParameters())
|
2015-03-05 17:22:17 +00:00
|
|
|
argumentSize += var->getType()->getCalldataEncodedSize();
|
2015-01-23 15:37:06 +00:00
|
|
|
|
2014-12-15 21:57:39 +00:00
|
|
|
if (argumentSize > 0)
|
|
|
|
{
|
|
|
|
m_context << u256(argumentSize);
|
|
|
|
m_context.appendProgramSize();
|
2015-01-08 23:27:26 +00:00
|
|
|
m_context << u256(CompilerUtils::dataStartOffset); // copy it to byte four as expected for ABI calls
|
2014-12-15 21:57:39 +00:00
|
|
|
m_context << eth::Instruction::CODECOPY;
|
2015-01-23 15:37:06 +00:00
|
|
|
appendCalldataUnpacker(FunctionType(_constructor).getParameterTypes(), true);
|
2014-12-15 21:57:39 +00:00
|
|
|
}
|
2015-02-27 16:41:22 +00:00
|
|
|
_constructor.accept(*this);
|
2014-12-15 21:57:39 +00:00
|
|
|
}
|
|
|
|
|
2014-11-19 09:24:22 +00:00
|
|
|
void Compiler::appendFunctionSelector(ContractDefinition const& _contract)
|
2014-10-20 10:41:56 +00:00
|
|
|
{
|
2015-01-29 15:39:30 +00:00
|
|
|
map<FixedHash<4>, FunctionTypePointer> interfaceFunctions = _contract.getInterfaceFunctions();
|
2015-01-08 23:27:26 +00:00
|
|
|
map<FixedHash<4>, const eth::AssemblyItem> callDataUnpackerEntryPoints;
|
2014-10-30 17:15:25 +00:00
|
|
|
|
2015-01-07 23:19:19 +00:00
|
|
|
// retrieve the function signature hash from the calldata
|
2015-01-29 21:50:20 +00:00
|
|
|
if (!interfaceFunctions.empty())
|
2015-02-14 23:11:51 +00:00
|
|
|
CompilerUtils(m_context).loadFromMemory(0, IntegerType(CompilerUtils::dataStartOffset * 8), true);
|
2014-11-10 12:13:40 +00:00
|
|
|
|
2015-01-07 23:19:19 +00:00
|
|
|
// stack now is: 1 0 <funhash>
|
2015-01-08 23:27:26 +00:00
|
|
|
for (auto const& it: interfaceFunctions)
|
2014-11-10 12:13:40 +00:00
|
|
|
{
|
2015-01-08 23:27:26 +00:00
|
|
|
callDataUnpackerEntryPoints.insert(std::make_pair(it.first, m_context.newTag()));
|
|
|
|
m_context << eth::dupInstruction(1) << u256(FixedHash<4>::Arith(it.first)) << eth::Instruction::EQ;
|
2015-03-10 11:00:23 +00:00
|
|
|
m_context.appendConditionalJumpTo(callDataUnpackerEntryPoints.at(it.first));
|
2014-11-10 12:13:40 +00:00
|
|
|
}
|
2015-01-29 21:50:20 +00:00
|
|
|
if (FunctionDefinition const* fallback = _contract.getFallbackFunction())
|
|
|
|
{
|
|
|
|
eth::AssemblyItem returnTag = m_context.pushNewTag();
|
|
|
|
fallback->accept(*this);
|
|
|
|
m_context << returnTag;
|
|
|
|
appendReturnValuePacker(FunctionType(*fallback).getReturnParameterTypes());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
m_context << eth::Instruction::STOP; // function not found
|
2014-10-30 17:15:25 +00:00
|
|
|
|
2015-01-08 23:27:26 +00:00
|
|
|
for (auto const& it: interfaceFunctions)
|
2014-10-30 17:15:25 +00:00
|
|
|
{
|
2015-01-29 16:28:14 +00:00
|
|
|
FunctionTypePointer const& functionType = it.second;
|
2015-03-07 22:08:39 +00:00
|
|
|
solAssert(functionType->hasDeclaration(), "");
|
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, functionType->getDeclaration());
|
2015-01-08 23:27:26 +00:00
|
|
|
m_context << callDataUnpackerEntryPoints.at(it.first);
|
2014-10-31 16:47:43 +00:00
|
|
|
eth::AssemblyItem returnTag = m_context.pushNewTag();
|
2015-01-22 16:40:22 +00:00
|
|
|
appendCalldataUnpacker(functionType->getParameterTypes());
|
2015-03-10 11:00:23 +00:00
|
|
|
m_context.appendJumpTo(m_context.getFunctionEntryLabel(functionType->getDeclaration()));
|
2014-10-31 16:47:43 +00:00
|
|
|
m_context << returnTag;
|
2015-01-22 16:40:22 +00:00
|
|
|
appendReturnValuePacker(functionType->getReturnParameterTypes());
|
2014-10-31 16:47:43 +00:00
|
|
|
}
|
2014-10-30 17:15:25 +00:00
|
|
|
}
|
|
|
|
|
2015-02-15 01:00:33 +00:00
|
|
|
void Compiler::appendCalldataUnpacker(TypePointers const& _typeParameters, bool _fromMemory)
|
2014-10-30 17:15:25 +00:00
|
|
|
{
|
|
|
|
// We do not check the calldata size, everything is zero-padded.
|
2015-02-15 01:00:33 +00:00
|
|
|
unsigned offset(CompilerUtils::dataStartOffset);
|
|
|
|
|
2015-03-03 10:28:56 +00:00
|
|
|
bigint parameterHeadEnd = offset;
|
2015-02-15 01:00:33 +00:00
|
|
|
for (TypePointer const& type: _typeParameters)
|
2015-03-05 17:22:17 +00:00
|
|
|
parameterHeadEnd += type->isDynamicallySized() ? 32 : type->getCalldataEncodedSize();
|
2015-03-03 10:28:56 +00:00
|
|
|
solAssert(parameterHeadEnd <= numeric_limits<unsigned>::max(), "Arguments too large.");
|
|
|
|
|
|
|
|
unsigned stackHeightOfPreviousDynamicArgument = 0;
|
|
|
|
ArrayType const* previousDynamicType = nullptr;
|
2015-01-22 16:40:22 +00:00
|
|
|
for (TypePointer const& type: _typeParameters)
|
2015-03-03 10:28:56 +00:00
|
|
|
{
|
|
|
|
switch (type->getCategory())
|
2015-02-15 01:00:33 +00:00
|
|
|
{
|
2015-03-03 10:28:56 +00:00
|
|
|
case Type::Category::Array:
|
|
|
|
if (type->isDynamicallySized())
|
|
|
|
{
|
|
|
|
// put on stack: data_offset length
|
|
|
|
unsigned newStackHeight = m_context.getStackHeight();
|
|
|
|
if (previousDynamicType)
|
|
|
|
{
|
|
|
|
// Retrieve data start offset by adding length to start offset of previous dynamic type
|
|
|
|
unsigned stackDepth = m_context.getStackHeight() - stackHeightOfPreviousDynamicArgument;
|
2015-03-13 12:14:51 +00:00
|
|
|
solAssert(stackDepth <= 16, "Stack too deep.");
|
2015-03-03 10:28:56 +00:00
|
|
|
m_context << eth::dupInstruction(stackDepth) << eth::dupInstruction(stackDepth);
|
2015-03-05 17:22:17 +00:00
|
|
|
ArrayUtils(m_context).convertLengthToSize(*previousDynamicType, true);
|
|
|
|
m_context << eth::Instruction::ADD;
|
2015-03-03 10:28:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
m_context << u256(parameterHeadEnd);
|
|
|
|
stackHeightOfPreviousDynamicArgument = newStackHeight;
|
|
|
|
previousDynamicType = &dynamic_cast<ArrayType const&>(*type);
|
|
|
|
offset += CompilerUtils(m_context).loadFromMemory(offset, IntegerType(256), !_fromMemory);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-02-15 01:00:33 +00:00
|
|
|
m_context << u256(offset);
|
2015-03-05 17:22:17 +00:00
|
|
|
offset += type->getCalldataEncodedSize();
|
2015-03-03 10:28:56 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
solAssert(!type->isDynamicallySized(), "Unknown dynamically sized type: " + type->toString());
|
|
|
|
offset += CompilerUtils(m_context).loadFromMemory(offset, *type, !_fromMemory, true);
|
2015-02-15 01:00:33 +00:00
|
|
|
}
|
2015-03-03 10:28:56 +00:00
|
|
|
}
|
2014-10-30 17:15:25 +00:00
|
|
|
}
|
|
|
|
|
2015-01-22 16:40:22 +00:00
|
|
|
void Compiler::appendReturnValuePacker(TypePointers const& _typeParameters)
|
2014-10-30 17:15:25 +00:00
|
|
|
{
|
|
|
|
//@todo this can be also done more efficiently
|
|
|
|
unsigned dataOffset = 0;
|
2015-01-22 16:40:22 +00:00
|
|
|
unsigned stackDepth = 0;
|
|
|
|
for (TypePointer const& type: _typeParameters)
|
|
|
|
stackDepth += type->getSizeOnStack();
|
|
|
|
|
|
|
|
for (TypePointer const& type: _typeParameters)
|
2014-10-30 17:15:25 +00:00
|
|
|
{
|
2015-03-03 16:55:28 +00:00
|
|
|
CompilerUtils(m_context).copyToStackTop(stackDepth, type->getSizeOnStack());
|
2015-02-25 14:14:22 +00:00
|
|
|
ExpressionCompiler(m_context, m_optimize).appendTypeConversion(*type, *type, true);
|
2015-01-09 20:36:25 +00:00
|
|
|
bool const c_padToWords = true;
|
2015-02-10 16:53:43 +00:00
|
|
|
dataOffset += CompilerUtils(m_context).storeInMemory(dataOffset, *type, c_padToWords);
|
2015-01-22 16:40:22 +00:00
|
|
|
stackDepth -= type->getSizeOnStack();
|
2014-10-30 17:15:25 +00:00
|
|
|
}
|
|
|
|
// note that the stack is not cleaned up here
|
|
|
|
m_context << u256(dataOffset) << u256(0) << eth::Instruction::RETURN;
|
2014-10-20 10:41:56 +00:00
|
|
|
}
|
|
|
|
|
2014-11-19 09:24:22 +00:00
|
|
|
void Compiler::registerStateVariables(ContractDefinition const& _contract)
|
|
|
|
{
|
2015-01-15 19:04:24 +00:00
|
|
|
for (ContractDefinition const* contract: boost::adaptors::reverse(_contract.getLinearizedBaseContracts()))
|
|
|
|
for (ASTPointer<VariableDeclaration> const& variable: contract->getStateVariables())
|
|
|
|
m_context.addStateVariable(*variable);
|
2014-11-19 09:24:22 +00:00
|
|
|
}
|
|
|
|
|
2015-02-17 15:21:38 +00:00
|
|
|
void Compiler::initializeStateVariables(ContractDefinition const& _contract)
|
|
|
|
{
|
2015-02-20 10:57:42 +00:00
|
|
|
for (ASTPointer<VariableDeclaration> const& variable: _contract.getStateVariables())
|
2015-02-17 15:21:38 +00:00
|
|
|
if (variable->getValue())
|
2015-02-25 14:14:22 +00:00
|
|
|
ExpressionCompiler(m_context, m_optimize).appendStateVariableInitialization(*variable);
|
2015-02-17 15:21:38 +00:00
|
|
|
}
|
|
|
|
|
2015-01-27 13:32:59 +00:00
|
|
|
bool Compiler::visit(VariableDeclaration const& _variableDeclaration)
|
2015-01-26 15:58:54 +00:00
|
|
|
{
|
2015-01-27 13:32:59 +00:00
|
|
|
solAssert(_variableDeclaration.isStateVariable(), "Compiler visit to non-state variable declaration.");
|
2015-03-07 22:08:39 +00:00
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _variableDeclaration);
|
2015-01-27 13:32:59 +00:00
|
|
|
|
|
|
|
m_context.startFunction(_variableDeclaration);
|
2015-01-26 15:58:54 +00:00
|
|
|
m_breakTags.clear();
|
|
|
|
m_continueTags.clear();
|
|
|
|
|
2015-01-27 13:32:59 +00:00
|
|
|
m_context << m_context.getFunctionEntryLabel(_variableDeclaration);
|
2015-02-25 14:14:22 +00:00
|
|
|
ExpressionCompiler(m_context, m_optimize).appendStateVariableAccessor(_variableDeclaration);
|
2015-01-27 13:32:59 +00:00
|
|
|
|
|
|
|
return false;
|
2015-01-26 15:58:54 +00:00
|
|
|
}
|
|
|
|
|
2014-12-06 00:06:24 +00:00
|
|
|
bool Compiler::visit(FunctionDefinition const& _function)
|
2014-10-20 10:41:56 +00:00
|
|
|
{
|
2015-03-07 22:08:39 +00:00
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _function);
|
2014-10-30 17:15:25 +00:00
|
|
|
//@todo to simplify this, the calling convention could by changed such that
|
2014-10-30 00:20:32 +00:00
|
|
|
// caller puts: [retarg0] ... [retargm] [return address] [arg0] ... [argn]
|
|
|
|
// although note that this reduces the size of the visible stack
|
2014-10-25 14:52:22 +00:00
|
|
|
|
2015-01-27 13:32:59 +00:00
|
|
|
m_context.startFunction(_function);
|
2014-10-25 14:52:22 +00:00
|
|
|
|
2014-10-30 00:20:32 +00:00
|
|
|
// stack upon entry: [return address] [arg0] [arg1] ... [argn]
|
|
|
|
// reserve additional slots: [retarg0] ... [retargm] [localvar0] ... [localvarp]
|
2014-10-20 10:41:56 +00:00
|
|
|
|
2015-02-14 23:11:51 +00:00
|
|
|
unsigned parametersSize = CompilerUtils::getSizeOnStack(_function.getParameters());
|
2015-02-27 16:41:22 +00:00
|
|
|
if (!_function.isConstructor())
|
|
|
|
// adding 1 for return address.
|
|
|
|
m_context.adjustStackOffset(parametersSize + 1);
|
2015-02-14 23:11:51 +00:00
|
|
|
for (ASTPointer<VariableDeclaration const> const& variable: _function.getParameters())
|
2015-02-14 00:22:44 +00:00
|
|
|
{
|
2015-02-14 23:11:51 +00:00
|
|
|
m_context.addVariable(*variable, parametersSize);
|
|
|
|
parametersSize -= variable->getType()->getSizeOnStack();
|
2015-01-23 01:35:27 +00:00
|
|
|
}
|
2015-02-27 16:41:22 +00:00
|
|
|
|
2014-12-08 17:19:25 +00:00
|
|
|
for (ASTPointer<VariableDeclaration const> const& variable: _function.getReturnParameters())
|
2014-12-08 15:56:41 +00:00
|
|
|
m_context.addAndInitializeVariable(*variable);
|
2014-10-30 00:20:32 +00:00
|
|
|
for (VariableDeclaration const* localVariable: _function.getLocalVariables())
|
2014-12-08 15:56:41 +00:00
|
|
|
m_context.addAndInitializeVariable(*localVariable);
|
2014-10-20 10:41:56 +00:00
|
|
|
|
2015-02-27 16:41:22 +00:00
|
|
|
if (_function.isConstructor())
|
|
|
|
if (auto c = m_context.getNextConstructor(dynamic_cast<ContractDefinition const&>(*_function.getScope())))
|
|
|
|
appendBaseConstructor(*c);
|
|
|
|
|
|
|
|
m_returnTag = m_context.newTag();
|
|
|
|
m_breakTags.clear();
|
|
|
|
m_continueTags.clear();
|
|
|
|
m_stackCleanupForReturn = 0;
|
|
|
|
m_currentFunction = &_function;
|
|
|
|
m_modifierDepth = 0;
|
|
|
|
|
2015-01-23 01:35:27 +00:00
|
|
|
appendModifierOrFunctionCode();
|
2014-10-20 10:41:56 +00:00
|
|
|
|
2014-10-30 00:20:32 +00:00
|
|
|
m_context << m_returnTag;
|
2014-10-20 10:41:56 +00:00
|
|
|
|
2014-10-30 00:20:32 +00:00
|
|
|
// Now we need to re-shuffle the stack. For this we keep a record of the stack layout
|
|
|
|
// that shows the target positions of the elements, where "-1" denotes that this element needs
|
|
|
|
// to be removed from the stack.
|
|
|
|
// Note that the fact that the return arguments are of increasing index is vital for this
|
|
|
|
// algorithm to work.
|
2014-10-20 10:41:56 +00:00
|
|
|
|
2015-02-16 16:33:13 +00:00
|
|
|
unsigned const c_argumentsSize = CompilerUtils::getSizeOnStack(_function.getParameters());
|
2015-01-09 20:36:25 +00:00
|
|
|
unsigned const c_returnValuesSize = CompilerUtils::getSizeOnStack(_function.getReturnParameters());
|
|
|
|
unsigned const c_localVariablesSize = CompilerUtils::getSizeOnStack(_function.getLocalVariables());
|
2014-12-08 17:19:25 +00:00
|
|
|
|
2014-10-30 00:20:32 +00:00
|
|
|
vector<int> stackLayout;
|
2015-01-09 20:36:25 +00:00
|
|
|
stackLayout.push_back(c_returnValuesSize); // target of return address
|
|
|
|
stackLayout += vector<int>(c_argumentsSize, -1); // discard all arguments
|
|
|
|
for (unsigned i = 0; i < c_returnValuesSize; ++i)
|
2014-10-30 00:20:32 +00:00
|
|
|
stackLayout.push_back(i);
|
2015-01-09 20:36:25 +00:00
|
|
|
stackLayout += vector<int>(c_localVariablesSize, -1);
|
2014-10-20 10:41:56 +00:00
|
|
|
|
2015-03-13 12:14:51 +00:00
|
|
|
solAssert(stackLayout.size() <= 17, "Stack too deep.");
|
2014-10-30 00:20:32 +00:00
|
|
|
while (stackLayout.back() != int(stackLayout.size() - 1))
|
|
|
|
if (stackLayout.back() < 0)
|
|
|
|
{
|
|
|
|
m_context << eth::Instruction::POP;
|
|
|
|
stackLayout.pop_back();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_context << eth::swapInstruction(stackLayout.size() - stackLayout.back() - 1);
|
|
|
|
swap(stackLayout[stackLayout.back()], stackLayout.back());
|
|
|
|
}
|
2014-10-31 16:47:43 +00:00
|
|
|
//@todo assert that everything is in place now
|
2014-10-20 10:41:56 +00:00
|
|
|
|
2015-02-27 16:41:22 +00:00
|
|
|
for (ASTPointer<VariableDeclaration const> const& variable: _function.getParameters() + _function.getReturnParameters())
|
|
|
|
m_context.removeVariable(*variable);
|
|
|
|
for (VariableDeclaration const* localVariable: _function.getLocalVariables())
|
|
|
|
m_context.removeVariable(*localVariable);
|
2014-10-20 10:41:56 +00:00
|
|
|
|
2015-03-08 10:43:17 +00:00
|
|
|
m_context.adjustStackOffset(-(int)c_returnValuesSize);
|
2015-03-09 18:22:24 +00:00
|
|
|
|
2015-02-27 16:41:22 +00:00
|
|
|
if (!_function.isConstructor())
|
2015-03-09 18:22:24 +00:00
|
|
|
m_context.appendJump(eth::AssemblyItem::JumpType::OutOfFunction);
|
2014-10-30 00:20:32 +00:00
|
|
|
return false;
|
2014-10-20 10:41:56 +00:00
|
|
|
}
|
|
|
|
|
2014-12-06 00:06:24 +00:00
|
|
|
bool Compiler::visit(IfStatement const& _ifStatement)
|
2014-10-20 10:41:56 +00:00
|
|
|
{
|
2015-02-22 17:38:32 +00:00
|
|
|
StackHeightChecker checker(m_context);
|
2015-03-07 22:08:39 +00:00
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _ifStatement);
|
2014-12-11 16:35:23 +00:00
|
|
|
compileExpression(_ifStatement.getCondition());
|
2014-10-30 00:20:32 +00:00
|
|
|
eth::AssemblyItem trueTag = m_context.appendConditionalJump();
|
|
|
|
if (_ifStatement.getFalseStatement())
|
|
|
|
_ifStatement.getFalseStatement()->accept(*this);
|
2014-11-10 16:31:09 +00:00
|
|
|
eth::AssemblyItem endTag = m_context.appendJumpToNew();
|
2014-10-30 00:20:32 +00:00
|
|
|
m_context << trueTag;
|
|
|
|
_ifStatement.getTrueStatement().accept(*this);
|
|
|
|
m_context << endTag;
|
2015-02-22 17:38:32 +00:00
|
|
|
|
|
|
|
checker.check();
|
2014-10-30 00:20:32 +00:00
|
|
|
return false;
|
2014-10-20 10:41:56 +00:00
|
|
|
}
|
|
|
|
|
2014-12-06 00:06:24 +00:00
|
|
|
bool Compiler::visit(WhileStatement const& _whileStatement)
|
2014-10-20 10:41:56 +00:00
|
|
|
{
|
2015-02-22 17:38:32 +00:00
|
|
|
StackHeightChecker checker(m_context);
|
2015-03-07 22:08:39 +00:00
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _whileStatement);
|
2014-10-30 00:20:32 +00:00
|
|
|
eth::AssemblyItem loopStart = m_context.newTag();
|
|
|
|
eth::AssemblyItem loopEnd = m_context.newTag();
|
|
|
|
m_continueTags.push_back(loopStart);
|
|
|
|
m_breakTags.push_back(loopEnd);
|
2014-10-20 10:41:56 +00:00
|
|
|
|
2014-10-30 00:20:32 +00:00
|
|
|
m_context << loopStart;
|
2014-12-11 16:35:23 +00:00
|
|
|
compileExpression(_whileStatement.getCondition());
|
2014-10-31 16:20:27 +00:00
|
|
|
m_context << eth::Instruction::ISZERO;
|
2014-10-30 00:20:32 +00:00
|
|
|
m_context.appendConditionalJumpTo(loopEnd);
|
2014-10-20 10:41:56 +00:00
|
|
|
|
2014-10-30 00:20:32 +00:00
|
|
|
_whileStatement.getBody().accept(*this);
|
2014-10-20 10:41:56 +00:00
|
|
|
|
2014-10-30 00:20:32 +00:00
|
|
|
m_context.appendJumpTo(loopStart);
|
|
|
|
m_context << loopEnd;
|
2014-10-20 10:41:56 +00:00
|
|
|
|
2014-10-30 00:20:32 +00:00
|
|
|
m_continueTags.pop_back();
|
|
|
|
m_breakTags.pop_back();
|
2015-02-22 17:38:32 +00:00
|
|
|
|
|
|
|
checker.check();
|
2014-10-30 00:20:32 +00:00
|
|
|
return false;
|
2014-10-20 10:41:56 +00:00
|
|
|
}
|
|
|
|
|
2014-12-15 16:45:18 +00:00
|
|
|
bool Compiler::visit(ForStatement const& _forStatement)
|
|
|
|
{
|
2015-02-22 17:38:32 +00:00
|
|
|
StackHeightChecker checker(m_context);
|
2015-03-07 22:08:39 +00:00
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _forStatement);
|
2014-12-16 15:52:47 +00:00
|
|
|
eth::AssemblyItem loopStart = m_context.newTag();
|
|
|
|
eth::AssemblyItem loopEnd = m_context.newTag();
|
|
|
|
m_continueTags.push_back(loopStart);
|
|
|
|
m_breakTags.push_back(loopEnd);
|
|
|
|
|
|
|
|
if (_forStatement.getInitializationExpression())
|
|
|
|
_forStatement.getInitializationExpression()->accept(*this);
|
|
|
|
|
|
|
|
m_context << loopStart;
|
|
|
|
|
|
|
|
// if there is no terminating condition in for, default is to always be true
|
|
|
|
if (_forStatement.getCondition())
|
|
|
|
{
|
|
|
|
compileExpression(*_forStatement.getCondition());
|
|
|
|
m_context << eth::Instruction::ISZERO;
|
|
|
|
m_context.appendConditionalJumpTo(loopEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
_forStatement.getBody().accept(*this);
|
|
|
|
|
|
|
|
// for's loop expression if existing
|
|
|
|
if (_forStatement.getLoopExpression())
|
|
|
|
_forStatement.getLoopExpression()->accept(*this);
|
|
|
|
|
|
|
|
m_context.appendJumpTo(loopStart);
|
|
|
|
m_context << loopEnd;
|
|
|
|
|
|
|
|
m_continueTags.pop_back();
|
|
|
|
m_breakTags.pop_back();
|
2015-02-22 17:38:32 +00:00
|
|
|
|
|
|
|
checker.check();
|
2014-12-15 16:45:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-23 15:31:36 +00:00
|
|
|
bool Compiler::visit(Continue const& _continueStatement)
|
2014-10-20 10:41:56 +00:00
|
|
|
{
|
2015-03-07 22:08:39 +00:00
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _continueStatement);
|
2014-11-05 17:44:05 +00:00
|
|
|
if (!m_continueTags.empty())
|
|
|
|
m_context.appendJumpTo(m_continueTags.back());
|
2014-10-30 00:20:32 +00:00
|
|
|
return false;
|
2014-10-20 10:41:56 +00:00
|
|
|
}
|
|
|
|
|
2015-02-23 15:31:36 +00:00
|
|
|
bool Compiler::visit(Break const& _breakStatement)
|
2014-10-20 10:41:56 +00:00
|
|
|
{
|
2015-03-07 22:08:39 +00:00
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _breakStatement);
|
2014-11-05 17:44:05 +00:00
|
|
|
if (!m_breakTags.empty())
|
|
|
|
m_context.appendJumpTo(m_breakTags.back());
|
2014-10-30 00:20:32 +00:00
|
|
|
return false;
|
2014-10-20 10:41:56 +00:00
|
|
|
}
|
|
|
|
|
2014-12-06 00:06:24 +00:00
|
|
|
bool Compiler::visit(Return const& _return)
|
2014-10-20 10:41:56 +00:00
|
|
|
{
|
2015-03-07 22:08:39 +00:00
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _return);
|
2014-10-30 00:20:32 +00:00
|
|
|
//@todo modifications are needed to make this work with functions returning multiple values
|
2014-12-06 01:19:10 +00:00
|
|
|
if (Expression const* expression = _return.getExpression())
|
2014-10-20 10:41:56 +00:00
|
|
|
{
|
2015-01-23 01:35:27 +00:00
|
|
|
solAssert(_return.getFunctionReturnParameters(), "Invalid return parameters pointer.");
|
|
|
|
VariableDeclaration const& firstVariable = *_return.getFunctionReturnParameters()->getParameters().front();
|
|
|
|
compileExpression(*expression, firstVariable.getType());
|
2014-12-08 17:52:30 +00:00
|
|
|
CompilerUtils(m_context).moveToStackVariable(firstVariable);
|
2014-10-20 10:41:56 +00:00
|
|
|
}
|
2015-01-23 01:35:27 +00:00
|
|
|
for (unsigned i = 0; i < m_stackCleanupForReturn; ++i)
|
|
|
|
m_context << eth::Instruction::POP;
|
2014-10-30 00:20:32 +00:00
|
|
|
m_context.appendJumpTo(m_returnTag);
|
2015-01-23 01:35:27 +00:00
|
|
|
m_context.adjustStackOffset(m_stackCleanupForReturn);
|
2014-10-30 00:20:32 +00:00
|
|
|
return false;
|
2014-10-20 10:41:56 +00:00
|
|
|
}
|
|
|
|
|
2015-02-22 17:38:32 +00:00
|
|
|
bool Compiler::visit(VariableDeclarationStatement const& _variableDeclarationStatement)
|
2014-10-25 14:52:22 +00:00
|
|
|
{
|
2015-02-22 17:38:32 +00:00
|
|
|
StackHeightChecker checker(m_context);
|
2015-03-07 22:08:39 +00:00
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _variableDeclarationStatement);
|
2015-02-22 17:38:32 +00:00
|
|
|
if (Expression const* expression = _variableDeclarationStatement.getExpression())
|
2014-10-25 14:52:22 +00:00
|
|
|
{
|
2015-02-22 17:38:32 +00:00
|
|
|
compileExpression(*expression, _variableDeclarationStatement.getDeclaration().getType());
|
|
|
|
CompilerUtils(m_context).moveToStackVariable(_variableDeclarationStatement.getDeclaration());
|
2014-10-25 14:52:22 +00:00
|
|
|
}
|
2015-02-22 17:38:32 +00:00
|
|
|
checker.check();
|
2014-10-30 00:20:32 +00:00
|
|
|
return false;
|
2014-10-25 14:52:22 +00:00
|
|
|
}
|
|
|
|
|
2014-12-06 00:06:24 +00:00
|
|
|
bool Compiler::visit(ExpressionStatement const& _expressionStatement)
|
2014-10-25 14:52:22 +00:00
|
|
|
{
|
2015-02-22 17:38:32 +00:00
|
|
|
StackHeightChecker checker(m_context);
|
2015-03-07 22:08:39 +00:00
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _expressionStatement);
|
2014-12-06 01:19:10 +00:00
|
|
|
Expression const& expression = _expressionStatement.getExpression();
|
2014-12-11 16:35:23 +00:00
|
|
|
compileExpression(expression);
|
2014-12-08 17:52:30 +00:00
|
|
|
CompilerUtils(m_context).popStackElement(*expression.getType());
|
2015-02-22 17:38:32 +00:00
|
|
|
checker.check();
|
2014-10-30 00:20:32 +00:00
|
|
|
return false;
|
2014-10-25 14:52:22 +00:00
|
|
|
}
|
|
|
|
|
2015-02-23 15:31:36 +00:00
|
|
|
bool Compiler::visit(PlaceholderStatement const& _placeholderStatement)
|
2015-01-23 01:35:27 +00:00
|
|
|
{
|
2015-02-22 17:38:32 +00:00
|
|
|
StackHeightChecker checker(m_context);
|
2015-03-07 22:08:39 +00:00
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, _placeholderStatement);
|
2015-01-23 01:35:27 +00:00
|
|
|
++m_modifierDepth;
|
|
|
|
appendModifierOrFunctionCode();
|
|
|
|
--m_modifierDepth;
|
2015-02-22 17:38:32 +00:00
|
|
|
checker.check();
|
2015-01-26 09:13:34 +00:00
|
|
|
return true;
|
2015-01-23 01:35:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Compiler::appendModifierOrFunctionCode()
|
|
|
|
{
|
|
|
|
solAssert(m_currentFunction, "");
|
|
|
|
if (m_modifierDepth >= m_currentFunction->getModifiers().size())
|
|
|
|
m_currentFunction->getBody().accept(*this);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ASTPointer<ModifierInvocation> const& modifierInvocation = m_currentFunction->getModifiers()[m_modifierDepth];
|
2015-02-27 16:41:22 +00:00
|
|
|
|
|
|
|
// constructor call should be excluded
|
|
|
|
if (dynamic_cast<ContractDefinition const*>(modifierInvocation->getName()->getReferencedDeclaration()))
|
|
|
|
{
|
|
|
|
++m_modifierDepth;
|
|
|
|
appendModifierOrFunctionCode();
|
|
|
|
--m_modifierDepth;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-23 01:46:31 +00:00
|
|
|
ModifierDefinition const& modifier = m_context.getFunctionModifier(modifierInvocation->getName()->getName());
|
2015-03-07 22:08:39 +00:00
|
|
|
CompilerContext::LocationSetter locationSetter(m_context, modifier);
|
2015-01-23 01:46:31 +00:00
|
|
|
solAssert(modifier.getParameters().size() == modifierInvocation->getArguments().size(), "");
|
|
|
|
for (unsigned i = 0; i < modifier.getParameters().size(); ++i)
|
2015-01-23 01:35:27 +00:00
|
|
|
{
|
2015-01-23 01:46:31 +00:00
|
|
|
m_context.addVariable(*modifier.getParameters()[i]);
|
2015-01-23 01:35:27 +00:00
|
|
|
compileExpression(*modifierInvocation->getArguments()[i],
|
2015-01-23 01:46:31 +00:00
|
|
|
modifier.getParameters()[i]->getType());
|
2015-01-23 01:35:27 +00:00
|
|
|
}
|
2015-01-23 01:46:31 +00:00
|
|
|
for (VariableDeclaration const* localVariable: modifier.getLocalVariables())
|
2015-01-23 01:35:27 +00:00
|
|
|
m_context.addAndInitializeVariable(*localVariable);
|
|
|
|
|
2015-01-23 01:46:31 +00:00
|
|
|
unsigned const c_stackSurplus = CompilerUtils::getSizeOnStack(modifier.getParameters()) +
|
|
|
|
CompilerUtils::getSizeOnStack(modifier.getLocalVariables());
|
2015-01-23 01:35:27 +00:00
|
|
|
m_stackCleanupForReturn += c_stackSurplus;
|
|
|
|
|
2015-01-23 01:46:31 +00:00
|
|
|
modifier.getBody().accept(*this);
|
2015-01-23 01:35:27 +00:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < c_stackSurplus; ++i)
|
|
|
|
m_context << eth::Instruction::POP;
|
|
|
|
m_stackCleanupForReturn -= c_stackSurplus;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Compiler::compileExpression(Expression const& _expression, TypePointer const& _targetType)
|
2014-12-11 16:35:23 +00:00
|
|
|
{
|
2015-02-25 14:14:22 +00:00
|
|
|
ExpressionCompiler expressionCompiler(m_context, m_optimize);
|
|
|
|
expressionCompiler.compile(_expression);
|
2015-01-23 01:35:27 +00:00
|
|
|
if (_targetType)
|
2015-02-25 14:14:22 +00:00
|
|
|
expressionCompiler.appendTypeConversion(*_expression.getType(), *_targetType);
|
2014-12-11 16:35:23 +00:00
|
|
|
}
|
|
|
|
|
2014-10-20 10:41:56 +00:00
|
|
|
}
|
|
|
|
}
|