This page is a placeholder. All examples on this page are currently AI-generated and are not correct. This documentation will be completed in the future with accurate, tested examples.
Overview
Opcode:0x58Introduced: Frontier (EVM genesis)PC pushes the current program counter value onto the stack. The program counter represents the position of the currently executing instruction in the bytecode.This opcode enables position-aware bytecode, dynamic jump calculations, and self-referential code patterns.
Specification
Stack Input: NoneStack Output:
pc (top) - Current program counter value
Gas Cost: 2 (GasQuickStep)Operation:
1. Push current PC onto stack2. Increment PC by 1
Behavior
PC provides the current execution position:
Consumes 2 gas (GasQuickStep)
Pushes current PC value onto stack
Increments PC to next instruction
Important: The value pushed is the PC of the PC instruction itself, NOT the next instruction.Example:
Position Opcode-------- ------0x00 PUSH1 0x050x02 PC ← PC = 0x020x03 ADD
When PC executes at position 0x02, it pushes 0x02 onto the stack.
Examples
Basic PC Usage
import { createFrame } from '@tevm/voltaire/evm/Frame';import { handler_0x58_PC } from '@tevm/voltaire/evm/control';const bytecode = new Uint8Array([ 0x60, 0x05, // PUSH1 5 (positions 0-1) 0x58, // PC (position 2) 0x01, // ADD (position 3)]);const frame = createFrame({ bytecode, stack: [5n], pc: 2 // At PC instruction});const err = handler_0x58_PC(frame);console.log(err); // null (success)console.log(frame.stack); // [5n, 2n] (pushed PC value 2)console.log(frame.pc); // 3 (incremented)
Calculate Relative Position
assembly { let currentPos := pc() let targetPos := add(currentPos, 10) // 10 bytes ahead // Use for relative jumps or calculations}
assembly { let base := pc() // Jump table based on current position let offset := mul(selector, 0x20) let dest := add(base, offset) jump(dest)}
Gas Cost
Cost: 2 gas (GasQuickStep)Comparison:
PC: 2 gas (read position)
PUSH1: 3 gas (push constant)
JUMP: 8 gas
JUMPI: 10 gas
Efficiency:
PC is cheaper than PUSH for getting current position, but PUSH is still preferred for constants.
Edge Cases
PC at Start
// PC at position 0const bytecode = new Uint8Array([0x58, 0x00]);const frame = createFrame({ bytecode, pc: 0 });handler_0x58_PC(frame);console.log(frame.stack); // [0n]
PC at End
// PC at last instructionconst bytecode = new Uint8Array([0x00, 0x00, 0x58]);const frame = createFrame({ bytecode, pc: 2 });handler_0x58_PC(frame);console.log(frame.stack); // [2n]console.log(frame.pc); // 3 (past bytecode - will stop)
Stack Overflow
// Stack at max capacity (1024 items)const frame = createFrame({ stack: new Array(1024).fill(0n), pc: 0});const err = handler_0x58_PC(frame);console.log(err); // { type: "StackOverflow" }
Multiple PC Calls
const bytecode = new Uint8Array([ 0x58, // PC → 0 0x58, // PC → 1 0x58, // PC → 2 0x58, // PC → 3]);const frame = createFrame({ bytecode, pc: 0 });// Execute all PC instructionsfor (let i = 0; i < 4; i++) { handler_0x58_PC(frame);}console.log(frame.stack); // [0n, 1n, 2n, 3n]
Common Usage
Relative Addressing
Calculate addresses relative to current position:
assembly { let here := pc() let data_offset := add(here, 0x20) // 32 bytes ahead // Load data from relative position let data := mload(data_offset)}
Position Verification
assembly { let expected := 0x1234 let actual := pc() // Verify we're at expected position if iszero(eq(actual, expected)) { revert(0, 0) }}
Code Size Calculation
assembly { let start := pc() // ... code block ... let end := pc() let size := sub(end, start)}
Dynamic Dispatch Base
assembly { // Get base address for jump table let base := pc() // Calculate jump destination switch selector case 0 { jump(add(base, 0x10)) } case 1 { jump(add(base, 0x30)) } case 2 { jump(add(base, 0x50)) }}
Implementation
TypeScript
import { consumeGas } from "../Frame/consumeGas.js";import { pushStack } from "../Frame/pushStack.js";import { QuickStep } from "../../primitives/GasConstants/constants.js";/** * PC opcode (0x58) - Get program counter * * @param frame - Frame instance * @returns Error if operation fails */export function handler_0x58_PC(frame: FrameType): EvmError | null { const gasErr = consumeGas(frame, QuickStep); if (gasErr) return gasErr; const pushErr = pushStack(frame, BigInt(frame.pc)); if (pushErr) return pushErr; frame.pc += 1; return null;}