Giter VIP home page Giter VIP logo

Comments (1)

molly-ting avatar molly-ting commented on June 3, 2024

Another similar case is the external call.

Description

    function extcall(uint256 a) public returns(uint256) {
        uint256 res = 0;
        try l.lib(a) returns (uint256 tmp) {
            res=tmp*2;
        } catch {
            res = 1;
        }
        
        return res;
    }

The above function will generate the following opcode.

DUP1 ISZERO PUSH2 0xF5 JUMPI POP PUSH1 0x40 MLOAD ... JUMP JUMPDEST PUSH1 0x1 JUMPDEST PUSH2 0x102 JUMPI PUSH1 0x1 SWAP1 POP

The corresponding assembly code is:

...
tag_11:
      ...
      gas
      call
      swap3
      pop
      pop
      pop
      dup1
      iszero
      tag_12
      jumpi
      pop
      mload(0x40)
      …
      jump	// in
tag_13:
      0x01
tag_12:
        /* "extcall.sol":481:589  try l.lib(a) returns (uint256 tmp) {... */
      tag_15
      jumpi
        /* "extcall.sol":577:578  1 */
      0x01
        /* "extcall.sol":571:578  res = 1 */
      swap1
      pop
        /* "extcall.sol":481:589  try l.lib(a) returns (uint256 tmp) {... */
      jump(tag_19)
...

In tag_11, there is a 'POP' just after 'JUMPI', which means the original value of the DUP is not used. Although this value is used in the other branch(tag_12), this can also be optimized.

Optimization

Specifically, moving tag_12 after tag_15 jumpi would achieve this optimization. The resulting assembly code is as follows:

...
tag_11:
      ...
      gas
      call
      swap3
      pop
      pop
      pop
      iszero
      tag_12
      jumpi
      mload(0x40)
      …
      jump	// in
tag_13:
      0x01
        /* "extcall.sol":481:589  try l.lib(a) returns (uint256 tmp) {... */
      tag_15
      jumpi
tag_12:
        /* "extcall.sol":577:578  1 */
      0x01
        /* "extcall.sol":571:578  res = 1 */
      swap1
      pop
        /* "extcall.sol":481:589  try l.lib(a) returns (uint256 tmp) {... */
      jump(tag_19)
...

if (_tryCall)
{
m_context << Instruction::DUP1 << Instruction::ISZERO;
m_context.appendConditionalJumpTo(endTag);
m_context << Instruction::POP;
}

if (_tryCall)
{
// Success branch will reach this, failure branch will directly jump to endTag.
m_context << u256(1);
m_context << endTag;
}

In this function, endTag is tag_12 in this example.
bool ContractCompiler::visit(TryStatement const& _tryStatement)
{
StackHeightChecker checker(m_context);
CompilerContext::LocationSetter locationSetter(m_context, _tryStatement);
compileExpression(_tryStatement.externalCall());
int const returnSize = static_cast<int>(_tryStatement.externalCall().annotation().type->sizeOnStack());
// Stack: [ return values] <success flag>
evmasm::AssemblyItem successTag = m_context.appendConditionalJump();
// Catch case.
m_context.adjustStackOffset(-returnSize);

In this function, successTag is tag_15 in this example. Is it possible to move the conditionalJump in this function to the front of the endTag in the function appendExternalFunctionCall so that we can save one duplication and pop?

from solidity.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.