The Evaluation Stack
The evaluation stack is used for managing state information in .NET pro-
grams. It is used by IL code in a way that is similar to how IA-32 instructions
use registers—for storing immediate information such as the input and output
data for instructions. Probably the most important thing to realize about the
evaluation stack is that it doesn’t really exist! Because IL code is never inter-
preted in runtime and is always compiled into native code before being exe-
cuted, the evaluation stack only exists during the JIT process. It has no
meaning during runtime.
Unlike the IA-32 stacks you’ve gotten so used to, the evaluation stack isn’t
made up of 32-bit entries, or any other fixed-size entries. A single entry in the
stack can contain any data type, including whole data structures. Many instruc-
tions in the IL instruction set are polymorphic, meaning that they can take dif-
ferent data types and properly deal with a variety of types. This means that
arithmetic instructions, for instance, can operate correctly on either floating-
point or integer operands. There is no need to explicitly tell instructions which
data types to expect—the JIT will perform the necessary data-flow analysis and
determine the data types of the operands passed to each instruction.
To properly grasp the philosophy of IL, you must get used to the idea that
the CLR is a stack machine, meaning that IL instructions use the evaluation
stack just like IA-32 assembly language instruction use registers. Practically
every instruction either pops a value off of the stack or it pushes some kind of
value back onto it—that’s how IL instructions access their operands.
Activation Records
Activation records are data elements that represent the state of the currently
running function, much like a stack frame in native programs. An activation
record contains the parameters passed to the current function along with all
the local variables in that function. For each function call a new activation
record is allocated and initialized. In most cases, the CLR allocates activation
records on the stack, which means that they are essentially the same thing as
the stack frames you’ve worked with in native assembly language code. The IL
instruction set includes special instructions that access the current activation
record for both function parameters and local variables (see below). Activation
records are automatically allocated by the IL instruction call.
IL Instructions
Let’s go over the most common and interesting IL instructions, just to get an
idea of the language and what it looks like. Table 12.1 provides descriptions for
some of the most popular instructions in the IL instruction set. Note that the
instruction set contains over 200 instructions and that this is nowhere near a
430 Chapter 12