Search code examples
keywordcil

What do "auto," "ansi," and "cil managed" do in CIL?


I'm learning CIL for various reasons, and it seems that classes are often defined similar to:

 .class public auto ansi beforefieldinit [...] extends [mscorlib]System.Object

and functions defined as:

 .method [...] (args) cil managed

I understand everything else, but I can't seem to find out what "auto" or "ansi" or "cil managed" do. The keywords are also too vague for me to get specific search results for (beforefieldinit came up nearly instantly).


Solution

  • ECMA-335 provides the information you seek,

    under II.10.1 Type header(ClassHeader) you will find:

    auto - Layout of fields is provided automatically. - §II.10.1.2

    ansi - Marshal strings to platform as ANSI. - §II.10.1.5

    Serge Lidin, in his book .Net IL Assembler writes in Chapter 1 under Class Declaration:

    The keyword auto in this context defines the class layout style (automatic, the default), directing the loader to lay out this class however it sees fit. Alternatives are sequential (which preserves the specified sequence of the fields) and explicit (which explicitly specifies the offset for each field, giving the loader exact instructions for laying out the class). The keyword ansi defines the mode of string conversion within the class when interoperating with the unmanaged code. This keyword, the default, specifies that the strings will be converted to and from “normal” C-style strings of bytes. Alternative keywords are unicode (strings are converted to and from UTF-16 Unicode) and autochar (the underlying platform determines the mode of string conversion).


    under II.23.1.11 Flags for methods [MethodImplAttributes] you can read:

    IL - 0x0000 - Method impl is CIL

    Managed - 0x0000 - Method impl is managed

    Serge Lidin, describes this in Chapter 1 under Method Declaration:

    The keywords cil and managed define so-called implementation flags of the MethodDef and indicate that the method body is represented in IL. A method represented in native code rather than in IL would carry the implementation flags native unmanaged.


    I recommend you to get a book about this topic, there are a few i think. Its a lot faster than digging around in the ECMA-335 specs.