Common Type System—Locals
Locals
Two types of data are local to a method’s activation frame: arguments and locals. Both occupy space on the physical stack; they are allocated when a method call is made and deallocated when a method exits, either because of an ordinary return or an unhandled error. We’ve already seen the use of arguments above. Arguments are locations into which callers copy data they wish to pass to the callee. We’ll examine the different argument passing styles later, which can involve copying values or passing references to a caller’s local data structures, for example, depending on the scenario.
A method can also use locals. Locals are also allocated on the stack, but are entirely transparent to
callers. They are an implementation detail of the method. When the stack space is allocated, locals are
initialized by zeroing them out (resulting in 0 for scalars, 0.0 for floating points, false for Booleans,
and null for references), and each is assigned a conceptual slot. Slots are typed so that the CLR can allocate
the correct amount of space and so that the verifier can ensure they are used in a type-safe manner.
For example:
class Example { public void Foo() { int x = 0; double y = 5.5; string s = “Hello, World”; // ... } }
This code defines three locals, x, y, and s, which get numbered when they are emitted to the IL. Most
compilers would turn these into 0, 1, and 2, respectively, but a compiler is free to perform optimizations
that would alter this numbering. Additional locals may of course be defined further in the method in the
C# language (unlike C, where all variables must be introduced at the beginning of a function), although
this is language specific and each will still ordinarily get its own slot (although compilers are free to
reuse slots as well).
|

