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

Skipping jump on jumptables inside macros #309

Open
Agusx1211 opened this issue Jan 18, 2024 · 0 comments
Open

Skipping jump on jumptables inside macros #309

Agusx1211 opened this issue Jan 18, 2024 · 0 comments

Comments

@Agusx1211
Copy link

Agusx1211 commented Jan 18, 2024

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant