Skip to content

Commit

Permalink
Optimize Address Masking for Bytecode Size
Browse files Browse the repository at this point in the history
  • Loading branch information
krishpranav committed Jan 23, 2025
1 parent fe1794d commit 290f8c0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
18 changes: 18 additions & 0 deletions libsolidity/codegen/ExpressionCompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <libsolidity/ast/TypeProvider.h>

#include <libevmasm/GasMeter.h>
#include <libevmasm/Instruction.h>
#include <libsolutil/Common.h>
#include <libsolutil/FunctionSelector.h>
#include <libsolutil/Keccak256.h>
Expand Down Expand Up @@ -1526,6 +1527,23 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
return false;
}

void ExpressionCompiler::emitAddressMasking()
{
if (m_context.optimizeForBytecodeSize())
{
m_context << Instruction::PUSH0
<< Instruction::NOT
<< Instruction::PUSH1
<< u256(12)
<< Instruction::SHR;
}
else
{
m_context << Instruction::PUSH20
<< u256(0xffffffffffffffffffffffffffffffffffffffff);
}
}

bool ExpressionCompiler::visit(FunctionCallOptions const& _functionCallOptions)
{
_functionCallOptions.expression().accept(*this);
Expand Down
1 change: 1 addition & 0 deletions libsolidity/codegen/ExpressionCompiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class ExpressionCompiler: private ASTConstVisitor
bool visit(IndexRangeAccess const& _indexAccess) override;
void endVisit(Identifier const& _identifier) override;
void endVisit(Literal const& _literal) override;
void emitAddressMasking();

///@{
///@name Append code for various operator types
Expand Down

0 comments on commit 290f8c0

Please sign in to comment.