Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IL generics - what rules exist for when !T is used vs !0?

Tags:

.net

generics

cil

I'm writing an IL static analysis tool, and I'm having a hard time understanding the rules governing how generic type parameters are referenced:

take this IL (from the IList<T> interface):

.property instance !T Item(
    int32 index
)
{
    .get instance !0 System.Collections.Generic.IList`1::get_Item(int32)
    .set instance void System.Collections.Generic.IList`1::set_Item(int32, !0)
}

Why is the !0 there instead of !T? I presume they're equivalent as far as the VM goes, it just seems odd to use positional references at all when you're guaranteed to have the names.

Update: an additional case, from KeyedCollection.ctor:

IL_0037:  newobj instance void class System.Collections.Generic.Dictionary`2<!TKey,!TItem>::'.ctor'(class System.Collections.Generic.IEqualityComparer`1<!0>)
IL_003c:  stfld class System.Collections.Generic.Dictionary`2<!0,!1> class System.Collections.ObjectModel.KeyedCollection`2<!0,!1>::dictionary
like image 506
toshok Avatar asked Jul 13 '12 18:07

toshok


1 Answers

In the Common Language Infrastructure standard, Partition II - Metadata and File Format, clause 7.1 "Types" it states:

Type ::=        Description
--------        -----------
'!'             Generic parameter in a type definition, accessed by index from 0

So short answer: because it's in the spec.

Long answer: This is speculation on my part, but basically, most IL commands are stack-based and use positional references as parameters all the time. That said, it makes sense that positional references are used for generics, in order to maintain common patterns/usage mechanisms in IL.

like image 163
casperOne Avatar answered Sep 19 '22 11:09

casperOne