Skip to content

Commit

Permalink
[SQUASH] Remove stack implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
chriseth committed Oct 15, 2020
1 parent e2ffdb8 commit 71723aa
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 16 deletions.
7 changes: 3 additions & 4 deletions libsolidity/codegen/CompilerContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,8 @@ class CompilerContext
void setMostDerivedContract(ContractDefinition const& _contract) { m_mostDerivedContract = &_contract; }
ContractDefinition const& mostDerivedContract() const;

void pushArithmetic(Arithmetic _value) { m_arithmetic.push(_value); }
void popArithmetic() { m_arithmetic.pop(); }
Arithmetic arithmetic() const { return m_arithmetic.empty() ? Arithmetic::Checked : m_arithmetic.top(); }
void setArithmetic(Arithmetic _value) { m_arithmetic = _value; }
Arithmetic arithmetic() const { return m_arithmetic; }

/// @returns the next function in the queue of functions that are still to be compiled
/// (i.e. that were referenced during compilation but where we did not yet generate code for).
Expand Down Expand Up @@ -385,7 +384,7 @@ class CompilerContext
/// The contract currently being compiled. Virtual function lookup starts from this contarct.
ContractDefinition const* m_mostDerivedContract = nullptr;
/// Whether to use checked arithmetic.
std::stack<Arithmetic> m_arithmetic;
Arithmetic m_arithmetic = Arithmetic::Checked;
/// Stack of current visited AST nodes, used for location attachment
std::stack<ASTNode const*> m_visitedNodes;
/// The runtime context if in Creation mode, this is used for generating tags that would be stored into the storage and then used at runtime.
Expand Down
16 changes: 11 additions & 5 deletions libsolidity/codegen/ContractCompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1244,23 +1244,31 @@ bool ContractCompiler::visit(PlaceholderStatement const& _placeholderStatement)
{
StackHeightChecker checker(m_context);
CompilerContext::LocationSetter locationSetter(m_context, _placeholderStatement);
solAssert(m_context.arithmetic() == Arithmetic::Checked, "Placeholder cannot be used inside checked block.");
appendModifierOrFunctionCode();
solAssert(m_context.arithmetic() == Arithmetic::Checked, "Arithmetic not reset to 'checked'.");
checker.check();
return true;
}

bool ContractCompiler::visit(Block const& _block)
{
if (_block.unchecked())
m_context.pushArithmetic(Arithmetic::Wrapping);
{
solAssert(m_context.arithmetic() == Arithmetic::Checked, "");
m_context.setArithmetic(Arithmetic::Wrapping);
}
storeStackHeight(&_block);
return true;
}

void ContractCompiler::endVisit(Block const& _block)
{
if (_block.unchecked())
m_context.popArithmetic();
{
solAssert(m_context.arithmetic() == Arithmetic::Wrapping, "");
m_context.setArithmetic(Arithmetic::Checked);
}
// Frees local variables declared in the scope of this block.
popScopedVariables(&_block);
}
Expand Down Expand Up @@ -1328,7 +1336,7 @@ void ContractCompiler::appendModifierOrFunctionCode()

if (codeBlock)
{
m_context.pushArithmetic(Arithmetic::Checked);
m_context.setArithmetic(Arithmetic::Checked);

std::set<ExperimentalFeature> experimentalFeaturesOutside = m_context.experimentalFeaturesActive();
m_context.setExperimentalFeatures(codeBlock->sourceUnit().annotation().experimentalFeatures);
Expand All @@ -1345,8 +1353,6 @@ void ContractCompiler::appendModifierOrFunctionCode()
CompilerUtils(m_context).popStackSlots(stackSurplus);
for (auto var: addedVariables)
m_context.removeVariable(*var);

m_context.popArithmetic();
}
m_modifierDepth--;
m_context.setModifierDepth(m_modifierDepth);
Expand Down
7 changes: 3 additions & 4 deletions libsolidity/codegen/ir/IRGenerationContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,8 @@ class IRGenerationContext

langutil::EVMVersion evmVersion() const { return m_evmVersion; };

void pushArithmetic(Arithmetic _value) { m_arithmetic.push(_value); }
void popArithmetic() { m_arithmetic.pop(); }
Arithmetic arithmetic() const { return m_arithmetic.empty() ? Arithmetic::Checked : m_arithmetic.top(); }
void setArithmetic(Arithmetic _value) { m_arithmetic = _value; }
Arithmetic arithmetic() const { return m_arithmetic; }

ABIFunctions abiFunctions();

Expand Down Expand Up @@ -166,7 +165,7 @@ class IRGenerationContext
MultiUseYulFunctionCollector m_functions;
size_t m_varCounter = 0;
/// Whether to use checked or wrapping arithmetic.
std::stack<Arithmetic> m_arithmetic;
Arithmetic m_arithmetic = Arithmetic::Checked;

/// Flag indicating whether any inline assembly block was seen.
bool m_inlineAssemblySeen = false;
Expand Down
10 changes: 8 additions & 2 deletions libsolidity/codegen/ir/IRGeneratorForStatements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -477,14 +477,20 @@ bool IRGeneratorForStatements::visit(TupleExpression const& _tuple)
bool IRGeneratorForStatements::visit(Block const& _block)
{
if (_block.unchecked())
m_context.pushArithmetic(Arithmetic::Wrapping);
{
solAssert(m_context.arithmetic() == Arithmetic::Checked, "");
m_context.setArithmetic(Arithmetic::Wrapping);
}
return true;
}

void IRGeneratorForStatements::endVisit(Block const& _block)
{
if (_block.unchecked())
m_context.popArithmetic();
{
solAssert(m_context.arithmetic() == Arithmetic::Wrapping, "");
m_context.setArithmetic(Arithmetic::Checked);
}
}

bool IRGeneratorForStatements::visit(IfStatement const& _ifStatement)
Expand Down
2 changes: 1 addition & 1 deletion test/libsolidity/SolidityExpressionCompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ bytes compileFirstExpression(
);
context.resetVisitedNodes(contract);
context.setMostDerivedContract(*contract);
context.pushArithmetic(Arithmetic::Wrapping);
context.setArithmetic(Arithmetic::Wrapping);
size_t parametersSize = _localVariables.size(); // assume they are all one slot on the stack
context.adjustStackOffset(static_cast<int>(parametersSize));
for (vector<string> const& variable: _localVariables)
Expand Down

0 comments on commit 71723aa

Please sign in to comment.