Skip to content

Yul codegen for break & continue statements #6241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Language Features:


Compiler Features:
* Yul: Adds break and continue keywords to for-loop syntax.


Bugfixes:
Expand Down
28 changes: 24 additions & 4 deletions libyul/backends/evm/EVMCodeTransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -634,14 +634,34 @@ void CodeTransform::operator()(ForLoop const& _forLoop)
checkStackHeight(&_forLoop);
}

void CodeTransform::operator()(Break const&)
int CodeTransform::appendPopUntil(int _targetDepth)
{
yulAssert(false, "Code generation for break statement in Yul is not implemented yet.");
int const stackDiffAfter = m_assembly.stackHeight() - _targetDepth;
for (int i = 0; i < stackDiffAfter; ++i)
m_assembly.appendInstruction(solidity::Instruction::POP);
return stackDiffAfter;
}

void CodeTransform::operator()(Break const& _break)
{
yulAssert(!m_context->forLoopStack.empty(), "Invalid break-statement. Requires surrounding for-loop in code generation.");
m_assembly.setSourceLocation(_break.location);

Context::JumpInfo const& jump = m_context->forLoopStack.top().done;
m_assembly.appendJumpTo(jump.label, appendPopUntil(jump.targetStackHeight));

checkStackHeight(&_break);
}

void CodeTransform::operator()(Continue const&)
void CodeTransform::operator()(Continue const& _continue)
{
yulAssert(false, "Code generation for continue statement in Yul is not implemented yet.");
yulAssert(!m_context->forLoopStack.empty(), "Invalid continue-statement. Requires surrounding for-loop in code generation.");
m_assembly.setSourceLocation(_continue.location);

Context::JumpInfo const& jump = m_context->forLoopStack.top().post;
m_assembly.appendJumpTo(jump.label, appendPopUntil(jump.targetStackHeight));

checkStackHeight(&_continue);
}

void CodeTransform::operator()(Block const& _block)
Expand Down
4 changes: 4 additions & 0 deletions libyul/backends/evm/EVMCodeTransform.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ class CodeTransform: public boost::static_visitor<>
/// and corrects the stack height to the target stack height.
void stackError(StackTooDeepError _error, int _targetStackSize);

/// Ensures stack height is down to @p _targetDepth by appending POP instructions to the output assembly.
/// Returns the number of POP statements that have been appended.
int appendPopUntil(int _targetDepth);

AbstractAssembly& m_assembly;
AsmAnalysisInfo& m_info;
Scope* m_scope = nullptr;
Expand Down
13 changes: 13 additions & 0 deletions test/libsolidity/semanticTests/asmForLoop/for_loop_break.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
contract C {
function f() public returns (uint i) {
assembly {
for {} lt(i, 10) { i := add(i, 1) }
{
if eq(i, 6) { break }
i := add(i, 1)
}
}
}
}
// ----
// f() -> 6
13 changes: 13 additions & 0 deletions test/libsolidity/semanticTests/asmForLoop/for_loop_continue.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
contract C {
function f() public returns (uint k) {
assembly {
for {let i := 0} lt(i, 10) { i := add(i, 1) }
{
if eq(mod(i, 2), 0) { continue }
k := add(k, 1)
}
}
}
}
// ----
// f() -> 5
20 changes: 20 additions & 0 deletions test/libsolidity/semanticTests/asmForLoop/for_loop_nested.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
contract C {
function f(uint x) public returns (uint i) {
assembly {
for {} lt(i, 10) { i := add(i, 1) }
{
if eq(x, 0) { i := 2 break }
for {} lt(x, 3) { i := 17 x := 9 } {
if eq(x, 1) { continue }
if eq(x, 2) { break }
}
if eq(x, 4) { i := 90 }
}
}
}
}
// ----
// f(uint256): 0 -> 2
// f(uint256): 1 -> 18
// f(uint256): 2 -> 10
// f(uint256): 4 -> 91