Skip to content
This repository was archived by the owner on Oct 20, 2024. It is now read-only.

Skipping jump on jumptables inside macros #309

@Agusx1211

Description

@Agusx1211

When a jump table is used inside a macro, and the macro is called more than once, the first time the macro is called, it jumps to the jump table on the second macro instance.

In this example, it can be seen that only one increment is performed. This happens because the first time DOES_A_JUMP is called, it loads the jump table that belongs to the second instance.

#include "./Errors.huff"

#define jumptable FLAG_TABLE {
  jt1
  jt2
}

#define macro DOES_A_JUMP() = takes(1) returns (1) {
  0x01 add

  __tablesize(FLAG_TABLE)
  __tablestart(FLAG_TABLE)
  0x00
  codecopy

  0x20 mload jump

  jt1:
  jt2:
}

#define macro DOES_MANY_JUMPS() = takes(1) returns (1) {
  DOES_A_JUMP()
  DOES_A_JUMP()
}

#define test REPLICATE() = {
  0x00

  DOES_MANY_JUMPS()

  0x02 eq ASSERT()
}

#define test REPLICATE_NOT() = {
  0x00

  DOES_MANY_JUMPS()

  0x01 eq iszero ASSERT()
}

If we add an entry point, we can see it in the generated bytecode:

#define macro MAIN() = takes(1) returns (1) {
  0x00

  DOES_MANY_JUMPS()
}
5f60010160406100615f39602051565b5b60010160406100615f39602051565b5b000000000000000000000000000000000000000000000000000000000000001f0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001f0000000000000000000000000000000000000000000000000000000000000020

Both jumptables end up with the same jump destinations.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions