Skip to main content

Try it Live

Run Opcode examples in the interactive playground

Opcode.isPush()

Check if opcode is PUSH0-PUSH32.
import { isPush, PUSH1, ADD } from 'tevm/Opcode'

isPush(PUSH1)  // true
isPush(ADD)    // false

Parameters

  • opcode: BrandedOpcode - Opcode to check

Returns

boolean - True if PUSH0-PUSH32 (0x5F-0x7F)

PUSH Opcodes

  • PUSH0 (0x5F): Push 0 onto stack (no immediate data)
  • PUSH1 (0x60): Push 1 byte
  • PUSH2-PUSH31 (0x61-0x7E): Push 2-31 bytes
  • PUSH32 (0x7F): Push 32 bytes

Use Cases

Extract Constants

function extractPushValues(bytecode: Uint8Array): bigint[] {
  const instructions = Opcode.parse(bytecode)
  const values: bigint[] = []

  for (const inst of instructions) {
    if (Opcode.isPush(inst.opcode) && inst.immediate) {
      const hex = '0x' + Array(inst.immediate)
        .map(b => b.toString(16).padStart(2, '0'))
        .join('')
      values.push(BigInt(hex))
    }
  }

  return values
}

Count PUSH Operations

function countPushOps(bytecode: Uint8Array): number {
  const instructions = Opcode.parse(bytecode)
  return instructions.filter(inst => Opcode.isPush(inst.opcode)).length
}