Try it Live
Run Bytecode examples in the interactive playground
- C
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 analyzecurrent_pc: Current program counter positionnext_pc: Output parameter for next PC
bool - true if next_pc is valid, false if at endExample:How It Works
getNextPc() handles the variable-width nature of EVM instructions:
Regular Opcodes (1 byte)
PUSH Instructions (1 + N bytes)
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
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
Comparison with scan()
Use
getNextPc() when:
- Building custom traversal logic
- Implementing jump analysis
- Need fine-grained PC control
- Working with PC-based data structures
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

