Before we proceed ahead, let me add that IM dispatch relies on
Generic / Specific JIT trampolines and Magic trampolines. These
trampolines were introduced in one of my earlier posts1.

2 Motivation for IMT – Interface Methods and Virtual Slots

The main problem with an Interface Method is the following:

Classes that implement a given interface method, can implement the
same at potentially different VTable slots.

For the sake of illustration consider a simple class structure and
the corresponding VTable as laid out by the runtime. Specifically
note that the interface method IPrint.Print is assigned Slot 5 in
PrintHate’s VTable while it is assigned Slot 4 in PrintLove’s
VTable.

Given the above scenario, it is clear that IM dispatch has to
involve a lookup in a class-specific table that maps between the IM
Handle and it’s target VTable slot. This table is called as anInterface Method Table or IMT for short.

We now proceed to see how this IM Table is Lookup is done in the
Mono Runtime. At this point, it suffices to remark that the
specific choice of data structure, detailed below, is driven by a
requirement on the runtime to provide a optimal hosting environment
for it’s applications 2.

3 Example Assembly

We will reference the following assembly throughout the following
sections

VTable for PrintLove class has a total of 12 virtual method
entries, 4 inherited from System.Object and 8 implemented by the
class (as mandated by IPrint interface class). The slot assignment
chosen by the runtime is shown below.

The figure down below captures the state of the PrintLove class’s
VTable right after initial setup.

Note there are 19 IMT slots and 12 VTable slots in the VTable. Their
entry points is set to point to IMT and VTable Trampolines
respectively.

An IMT trampoline and VTable trampolines are Specific JIT
trampolines associated with no particular method and created in root
domain. We know from our previous discussions that a Specific JIT
trampoline ultimately calls a Magic Trampoline that does the
required magic. We will come to the details of the magic shortly.

6 IMT call Handling in Magic Trampoline

Note that the magic trampoline has access to all information it
needs to do the needed magic.

One would recall that the Magic Trampoline gets a snapshot of the
state of registers at the call site through a regs array arg. It
implies that Magic Trampoline has access to the Interface Method
Handle (EDX component of regs array) as well as the IMT slot (via
opcode probing at the call site)

The magic trampoline now has the responsibility of

Locating the VTable slot for the Interface Method.

Compiling the target IM method and get a native entry point for it.

Creating a IM Handle-to-Entry Point mapping

Doing some house work so that future invocation to the IM doesn’t
go through the trampoline. This involves replacing IMT slot
entries and VTable Slot entries.

Steps 3 and 4 are accomplished by what are known as IMT thunks.

7 IMT Thunks Explained

One can intuitively guess how and what goes on in to the IMT Thunks
by simply looking at the VTable of the PrintLove class right after
the three interface method invocations obj.Print5, obj.Print4 and
obj.Print6 and by looking at the IMT Thunks themselves.

After some deep consideration of the wealth of information captured
in the next section one make the following inferences –

IMT Thunks are generated only for colliding IMT slots

IMT Thunks compare the target interface method (which is in EDX)
and jump to the method entry point defined in the rightful VTable
slot.

IMT Thunks generated for slots with small collisions ( < 4)
employ a linear search while that with more entries employ binary
search

IM table entries are lazy filled i.e., only upon invocation. Only
exception to this rule are other Interface Methods that contend
for the same IM slot as the Target IM. Note that contending
methods continue to remain uncompiled. Only their map is set
eagerly.

IM Thunks introduce a small lookup overhead for some IM
dispatches. This overhead is always there throughout the lifetime
of the program.