Skip to content

CompiledIMT

Levente Santha edited this page May 11, 2026 · 1 revision

CompiledIMT (Compiled Interface Method Table)

Architecture-specific compiled IMT dispatch code for fast interface calls.

Overview

The CompiledIMT is the machine-code form of the IMT (Interface Method Table), generated by the JIT compiler to optimize interface method dispatch. Where the Java-side IMT uses hash-based array lookup with potential collision list traversal, the CompiledIMT compiles this logic into a direct jump table, eliminating the overhead of Java method calls and memory indirection.

Why Compile the IMT?

Interface dispatch via the IMT involves:

  1. Loading the IMT array from the TIB
  2. Computing selector % 64
  3. Branching on whether the slot holds a single method or collision list
  4. Potentially searching a collision list

Each step involves Java heap access and virtual method dispatch. The CompiledIMT collapses all of this into a single direct jump from method selector to method entry point.

Key Components

Class / File Role
core/src/core/org/jnode/vm/compiler/CompiledIMT.java Abstract base class defining getIMTAddress()
core/src/core/org/jnode/vm/compiler/IMTCompiler.java Abstract compiler that generates CompiledIMT bytecode
core/src/core/org/jnode/vm/x86/compiler/X86CompiledIMT.java x86-specific implementation holding compiled bytecode array
core/src/core/org/jnode/vm/x86/compiler/X86Assembler.java Bytecode emitter for x86 machine code generation

Architecture

CompiledIMT Interface

public abstract class CompiledIMT {
    public abstract Word getIMTAddress();
}

The abstract base provides the compiled IMT address. Each architecture implements this with its own bytecode array and address computation.

X86CompiledIMT

public final class X86CompiledIMT extends CompiledIMT {
    private final byte[] code;
    private final int address;

    public X86CompiledIMT(byte[] code, int address) {
        this.code = code;
        this.address = address;
    }

    @Override
    public Word getIMTAddress() {
        return Word.factory().create(address);
    }
}

IMTCompiler

public abstract class IMTCompiler {
    public abstract CompiledIMT compileIMT(IMTBuilder builder);

    protected byte[] generateJumpTable(IMTBuilder builder) {
        // For each of 64 slots, emit:
        //   - Direct jump to method if single method
        //   - Hash check + jump if collision list
    }
}

How It Works

Compilation Flow

  1. During class preparation, VmClassType.prepareTIB() builds the IMT via IMTBuilder
  2. If IMT compilation is enabled, the VM calls imtCompiler.compileIMT(imtBuilder)
  3. IMTCompiler analyzes each of the 64 IMT slots:
    • If single method: emit direct jmp to method entry
    • If collision list: emit selector check + indirect jump
  4. The generated bytecode array is wrapped in a CompiledIMT instance
  5. The compiled address is stored in TIB at COMPILED_IMT_INDEX

Dispatch Sequence

invokeinterface Foo.bar()
  → load this object's TIB[COMPILED_IMT_INDEX]
  → if non-null: jump to compiled IMT address
  → if null: fall back to Java-side IMT lookup

Jump Table Structure

The compiled form maps selector values directly to entry points:

IMT Entry (per selector):
  selector_low = selector & 0x3F  // selector % 64
  jmp [compiledIMT_base + selector_low * 4]  // 4-byte absolute address

This provides O(1) dispatch with a single memory load and indirect jump.

TIB Integration

See IMT and Object-Layout for the TIB layout:

TIB[index] Layout:
  [0] VmType reference
  [1] IMT (Object[])
  [2] IMT collision flags (boolean[])
  [3] CompiledIMT — this page
  [4] Superclasses array
  [5..N] vtable

The CompiledIMT at index 3 may be null if compilation failed or is disabled.

Gotchas & Non-Obvious Behavior

  • CompiledIMT is optional: If TIB[3] is null, the runtime falls back to Java-side IMT lookup. Some architectures or build configurations may not implement CompiledIMT.
  • Architecture-specific: CompiledIMT is generated per-target architecture (x86, x86_64, etc.). The bytecode format is architecture-dependent.
  • JIT must use it correctly: The JIT's visit_invokeinterface must check for CompiledIMT availability and emit appropriate dispatch code.
  • Collision handling inlined: The compiled form typically handles common cases (no collision) with direct jumps and falls back for collision lists.

Related Pages

  • IMT — Java-side interface method table with hash collision handling
  • Virtual-Methods — Overview of method dispatch including vtable, IMT, and CompiledIMT
  • TIB — Type Information Block containing IMT and CompiledIMT
  • JIT-Compilers — How compilers generate and use CompiledIMT
  • Object-Layout — TIB structure and memory layout

Clone this wiki locally