Skip to main content

Try it Live

Run Bytecode examples in the interactive playground

bytecode_get_next_pc(bytecode_t bytecode, size_t current_pc, size_t *next_pc) bool

Calculate the next program counter position after the instruction at current_pc.Parameters:
  • bytecode: Bytecode to analyze
  • current_pc: Current program counter position
  • next_pc: Output parameter for next PC
Returns: bool - true if next_pc is valid, false if at endExample:
Defined in: primitives.h

How It Works

getNextPc() handles the variable-width nature of EVM instructions:

Regular Opcodes (1 byte)

PUSH Instructions (1 + N bytes)

Example:

Usage Patterns

Manual Iteration

For iteration, prefer using bytecode.scan() which handles this automatically. Use getNextPc() for cases requiring manual PC control.

Jump Target Calculation

Instruction Boundary Validation

Disassembly with PC Tracking

Coverage Tracking

Edge Cases

At End of Bytecode

Invalid PC

Truncated PUSH

For malformed bytecode (truncated PUSH instructions), behavior is implementation-defined. Always validate bytecode before processing.

PC in Middle of PUSH Data

Only call getNextPc() from instruction start positions. Calling from within PUSH data is undefined behavior.

Performance

getNextPc() is O(1) - constant time:
  • Reads single byte at current PC
  • Calculates width based on opcode
  • Returns PC + 1 + width
No caching or pre-analysis required.

Comparison with scan()

Use getNextPc() when:
  • Building custom traversal logic
  • Implementing jump analysis
  • Need fine-grained PC control
  • Working with PC-based data structures
Use scan() when:
  • Standard iteration over instructions
  • Need instruction metadata (opcode, type, value)
  • Want fusion detection
  • Don’t need manual PC manipulation

See Also

  • scan - High-level iterator (recommended for most use cases)
  • getBlock - Get block containing a PC
  • parseInstructions - Parse all instructions to array
  • Opcode - Opcode utilities for determining instruction width