Merge pull request #9243 from ethereum/jumpTypesForYul

Jump types for yul functions.
This commit is contained in:
chriseth
2020-07-07 12:21:14 +02:00
committed by GitHub
14 changed files with 151 additions and 35 deletions
+4 -3
View File
@@ -50,6 +50,7 @@ class AbstractAssembly
public:
using LabelID = size_t;
using SubID = size_t;
enum class JumpType { Ordinary, IntoFunction, OutOfFunction };
virtual ~AbstractAssembly() = default;
@@ -78,13 +79,13 @@ public:
/// Append a jump instruction.
/// @param _stackDiffAfter the stack adjustment after this instruction.
/// This is helpful to stack height analysis if there is no continuing control flow.
virtual void appendJump(int _stackDiffAfter) = 0;
virtual void appendJump(int _stackDiffAfter, JumpType _jumpType = JumpType::Ordinary) = 0;
/// Append a jump-to-immediate operation.
/// @param _stackDiffAfter the stack adjustment after this instruction.
virtual void appendJumpTo(LabelID _labelId, int _stackDiffAfter = 0) = 0;
virtual void appendJumpTo(LabelID _labelId, int _stackDiffAfter = 0, JumpType _jumpType = JumpType::Ordinary) = 0;
/// Append a jump-to-if-immediate operation.
virtual void appendJumpToIf(LabelID _labelId) = 0;
virtual void appendJumpToIf(LabelID _labelId, JumpType _jumpType = JumpType::Ordinary) = 0;
/// Start a subroutine identified by @a _labelId that takes @a _arguments
/// stack slots as arguments.
virtual void appendBeginsub(LabelID _labelId, int _arguments) = 0;
+25 -6
View File
@@ -98,22 +98,22 @@ void EthAssemblyAdapter::appendLinkerSymbol(std::string const& _linkerSymbol)
m_assembly.appendLibraryAddress(_linkerSymbol);
}
void EthAssemblyAdapter::appendJump(int _stackDiffAfter)
void EthAssemblyAdapter::appendJump(int _stackDiffAfter, JumpType _jumpType)
{
appendInstruction(evmasm::Instruction::JUMP);
appendJumpInstruction(evmasm::Instruction::JUMP, _jumpType);
m_assembly.adjustDeposit(_stackDiffAfter);
}
void EthAssemblyAdapter::appendJumpTo(LabelID _labelId, int _stackDiffAfter)
void EthAssemblyAdapter::appendJumpTo(LabelID _labelId, int _stackDiffAfter, JumpType _jumpType)
{
appendLabelReference(_labelId);
appendJump(_stackDiffAfter);
appendJump(_stackDiffAfter, _jumpType);
}
void EthAssemblyAdapter::appendJumpToIf(LabelID _labelId)
void EthAssemblyAdapter::appendJumpToIf(LabelID _labelId, JumpType _jumpType)
{
appendLabelReference(_labelId);
appendInstruction(evmasm::Instruction::JUMPI);
appendJumpInstruction(evmasm::Instruction::JUMPI, _jumpType);
}
void EthAssemblyAdapter::appendBeginsub(LabelID, int)
@@ -189,6 +189,25 @@ EthAssemblyAdapter::LabelID EthAssemblyAdapter::assemblyTagToIdentifier(evmasm::
return LabelID(id);
}
void EthAssemblyAdapter::appendJumpInstruction(evmasm::Instruction _instruction, JumpType _jumpType)
{
yulAssert(_instruction == evmasm::Instruction::JUMP || _instruction == evmasm::Instruction::JUMPI, "");
evmasm::AssemblyItem jump(_instruction);
switch (_jumpType)
{
case JumpType::Ordinary:
yulAssert(jump.getJumpType() == evmasm::AssemblyItem::JumpType::Ordinary, "");
break;
case JumpType::IntoFunction:
jump.setJumpType(evmasm::AssemblyItem::JumpType::IntoFunction);
break;
case JumpType::OutOfFunction:
jump.setJumpType(evmasm::AssemblyItem::JumpType::OutOfFunction);
break;
}
m_assembly.append(std::move(jump));
}
void CodeGenerator::assemble(
Block const& _parsedData,
AsmAnalysisInfo& _analysisInfo,
+4 -3
View File
@@ -49,9 +49,9 @@ public:
size_t newLabelId() override;
size_t namedLabel(std::string const& _name) override;
void appendLinkerSymbol(std::string const& _linkerSymbol) override;
void appendJump(int _stackDiffAfter) override;
void appendJumpTo(LabelID _labelId, int _stackDiffAfter) override;
void appendJumpToIf(LabelID _labelId) override;
void appendJump(int _stackDiffAfter, JumpType _jumpType) override;
void appendJumpTo(LabelID _labelId, int _stackDiffAfter, JumpType _jumpType) override;
void appendJumpToIf(LabelID _labelId, JumpType _jumpType) override;
void appendBeginsub(LabelID, int) override;
void appendJumpsub(LabelID, int, int) override;
void appendReturnsub(int, int) override;
@@ -66,6 +66,7 @@ public:
private:
static LabelID assemblyTagToIdentifier(evmasm::AssemblyItem const& _tag);
void appendJumpInstruction(evmasm::Instruction _instruction, JumpType _jumpType);
evmasm::Assembly& m_assembly;
std::map<SubID, u256> m_dataHashBySubId;
+4 -4
View File
@@ -91,14 +91,14 @@ void EVMAssembly::appendLinkerSymbol(string const&)
yulAssert(false, "Linker symbols not yet implemented.");
}
void EVMAssembly::appendJump(int _stackDiffAfter)
void EVMAssembly::appendJump(int _stackDiffAfter, JumpType)
{
yulAssert(!m_evm15, "Plain JUMP used for EVM 1.5");
appendInstruction(evmasm::Instruction::JUMP);
m_stackHeight += _stackDiffAfter;
}
void EVMAssembly::appendJumpTo(LabelID _labelId, int _stackDiffAfter)
void EVMAssembly::appendJumpTo(LabelID _labelId, int _stackDiffAfter, JumpType _jumpType)
{
if (m_evm15)
{
@@ -109,11 +109,11 @@ void EVMAssembly::appendJumpTo(LabelID _labelId, int _stackDiffAfter)
else
{
appendLabelReference(_labelId);
appendJump(_stackDiffAfter);
appendJump(_stackDiffAfter, _jumpType);
}
}
void EVMAssembly::appendJumpToIf(LabelID _labelId)
void EVMAssembly::appendJumpToIf(LabelID _labelId, JumpType)
{
if (m_evm15)
{
+3 -3
View File
@@ -64,11 +64,11 @@ public:
/// Append a jump instruction.
/// @param _stackDiffAfter the stack adjustment after this instruction.
void appendJump(int _stackDiffAfter) override;
void appendJump(int _stackDiffAfter, JumpType _jumpType) override;
/// Append a jump-to-immediate operation.
void appendJumpTo(LabelID _labelId, int _stackDiffAfter) override;
void appendJumpTo(LabelID _labelId, int _stackDiffAfter, JumpType _jumpType) override;
/// Append a jump-to-if-immediate operation.
void appendJumpToIf(LabelID _labelId) override;
void appendJumpToIf(LabelID _labelId, JumpType _jumpType) override;
/// Start a subroutine.
void appendBeginsub(LabelID _labelId, int _arguments) override;
/// Call a subroutine.
+6 -2
View File
@@ -291,7 +291,8 @@ void CodeTransform::operator()(FunctionCall const& _call)
{
m_assembly.appendJumpTo(
functionEntryID(_call.functionName.name, *function),
static_cast<int>(function->returns.size() - function->arguments.size()) - 1
static_cast<int>(function->returns.size() - function->arguments.size()) - 1,
AbstractAssembly::JumpType::IntoFunction
);
m_assembly.appendLabel(returnLabel);
}
@@ -511,7 +512,10 @@ void CodeTransform::operator()(FunctionDefinition const& _function)
if (m_evm15)
m_assembly.appendReturnsub(static_cast<int>(_function.returnVariables.size()), stackHeightBefore);
else
m_assembly.appendJump(stackHeightBefore - static_cast<int>(_function.returnVariables.size()));
m_assembly.appendJump(
stackHeightBefore - static_cast<int>(_function.returnVariables.size()),
AbstractAssembly::JumpType::OutOfFunction
);
m_assembly.setStackHeight(stackHeightBefore);
}
+4 -4
View File
@@ -70,25 +70,25 @@ void NoOutputAssembly::appendLinkerSymbol(string const&)
yulAssert(false, "Linker symbols not yet implemented.");
}
void NoOutputAssembly::appendJump(int _stackDiffAfter)
void NoOutputAssembly::appendJump(int _stackDiffAfter, JumpType)
{
yulAssert(!m_evm15, "Plain JUMP used for EVM 1.5");
appendInstruction(evmasm::Instruction::JUMP);
m_stackHeight += _stackDiffAfter;
}
void NoOutputAssembly::appendJumpTo(LabelID _labelId, int _stackDiffAfter)
void NoOutputAssembly::appendJumpTo(LabelID _labelId, int _stackDiffAfter, JumpType _jumpType)
{
if (m_evm15)
m_stackHeight += _stackDiffAfter;
else
{
appendLabelReference(_labelId);
appendJump(_stackDiffAfter);
appendJump(_stackDiffAfter, _jumpType);
}
}
void NoOutputAssembly::appendJumpToIf(LabelID _labelId)
void NoOutputAssembly::appendJumpToIf(LabelID _labelId, JumpType)
{
if (m_evm15)
m_stackHeight--;
+3 -3
View File
@@ -58,9 +58,9 @@ public:
LabelID namedLabel(std::string const& _name) override;
void appendLinkerSymbol(std::string const& _name) override;
void appendJump(int _stackDiffAfter) override;
void appendJumpTo(LabelID _labelId, int _stackDiffAfter) override;
void appendJumpToIf(LabelID _labelId) override;
void appendJump(int _stackDiffAfter, JumpType _jumpType) override;
void appendJumpTo(LabelID _labelId, int _stackDiffAfter, JumpType _jumpType) override;
void appendJumpToIf(LabelID _labelId, JumpType _jumpType) override;
void appendBeginsub(LabelID _labelId, int _arguments) override;
void appendJumpsub(LabelID _labelId, int _arguments, int _returns) override;
void appendReturnsub(int _returns, int _stackDiffAfter) override;