Skip to main content

Try it Live

Run Bytecode examples in the interactive playground
The Bytecode iterator returns instructions as a discriminated union (OpcodeData) representing different instruction categories and fusion patterns.

Base Instruction Type

All instruction objects share common fields:

Instruction Categories

Regular Instructions

Standard single-byte opcodes (ADD, MUL, SWAP1, etc.):
Example:

PUSH Instructions

PUSH1 through PUSH32 with immediate data:
Example:

JUMPDEST Instructions

Valid jump destinations with block metadata:
Example:
JUMPDEST metadata enables efficient block-level gas estimation and stack validation without re-analyzing.

JUMP Instructions

Dynamic unconditional jump (target determined at runtime):
Example:
jump type indicates dynamic JUMP (not fused with PUSH). Target validation occurs at runtime.

JUMPI Instructions

Dynamic conditional jump (target and condition determined at runtime):
Example:

PC Instructions

PC opcode returning current program counter:
Example:

STOP Instructions

Halts execution:

INVALID Instructions

Invalid opcodes or undefined instruction bytes:

Fusion Instructions

Optimizable multi-instruction patterns detected during analysis.

Arithmetic Fusions

PUSH + ADD

Represents: PUSH value + ADD → Add immediate value to stack top.

PUSH + MUL

Represents: PUSH value + MUL → Multiply stack top by immediate.

PUSH + SUB

Represents: PUSH value + SUB → Subtract immediate from stack top.

PUSH + DIV

Represents: PUSH value + DIV → Divide stack top by immediate.

Bitwise Fusions

PUSH + AND

Represents: PUSH value + AND → Bitwise AND with immediate.

PUSH + OR

Represents: PUSH value + OR → Bitwise OR with immediate.

PUSH + XOR

Represents: PUSH value + XOR → Bitwise XOR with immediate.

Memory Fusions

PUSH + MLOAD

Represents: PUSH offset + MLOAD → Load from immediate memory address.

PUSH + MSTORE

Represents: PUSH offset + MSTORE → Store to immediate memory address.

PUSH + MSTORE8

Represents: PUSH offset + MSTORE8 → Store byte to immediate memory address.

Control Flow Fusions

PUSH + JUMP

Represents: PUSH target + JUMPStatic jump to known destination.
Static jumps can be validated during bytecode analysis. If value points to invalid JUMPDEST, bytecode is malformed.

PUSH + JUMPI

Represents: PUSH target + JUMPI → Conditional jump to known destination (condition still runtime).

ISZERO + PUSH + JUMPI

Represents: ISZERO + PUSH target + JUMPI → Inverted conditional jump pattern. Common in Solidity:
Compiles to: ISZERO + PUSH <skip_offset> + JUMPI

Advanced Stack Fusions

DUP2 + MSTORE + PUSH

Represents: DUP2 + MSTORE + PUSH value → Common memory write pattern.

DUP3 + ADD + MSTORE

Represents: DUP3 + ADD + MSTORE → Offset calculation + store.

SWAP1 + DUP2 + ADD

Represents: SWAP1 + DUP2 + ADD → Stack manipulation + arithmetic.

PUSH + DUP3 + ADD

Represents: PUSH value + DUP3 + ADD → Immediate addition with stack duplication.

PUSH + ADD + DUP1

Represents: PUSH value + ADD + DUP1 → Add immediate and duplicate result.

MLOAD + SWAP1 + DUP2

Represents: MLOAD + SWAP1 + DUP2 → Memory load with stack rearrangement.

Multi-Instruction Fusions

MULTI_PUSH

Represents: Multiple consecutive PUSH instructions (2 or 3). Example:
{ type: 'multi_push', count: 3, values: [1n, 2n, 3n] }

MULTI_POP

Represents: Multiple consecutive POP instructions (2 or 3).

Solidity-Specific Patterns

FUNCTION_DISPATCH

Represents: PUSH4 selector + EQ + PUSH target + JUMPI → Function selector matching. Solidity pattern:
Generates function dispatch checking msg.sig == 0x12345678.
Detect all functions by collecting function_dispatch patterns. Extract function selectors for ABI reconstruction.

CALLVALUE_CHECK

Represents: CALLVALUE + DUP1 + ISZERO → Check if ETH sent (non-payable modifier). Solidity pattern:
Compiler inserts check: revert if msg.value > 0.

PUSH0 + PUSH0 + REVERT

Represents: PUSH0 + PUSH0 + REVERT → Empty revert (no error message). Solidity pattern:
Or failed require() with no message.

Union Type Definition

Complete TypeScript union:

Type Guards

Use TypeScript discriminated union for type-safe handling:

Pattern Detection

Detect specific patterns during iteration:

See Also