Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are different types handled on the stack in CIL

Experimenting with ildasm to dive into CIL code it became obvious that CIL itself is working stack-based to support expressions like

IL_0001:    ldc.i4.s 13     ; 1f 0d
IL_0003:    stloc.0         ; 0a
IL_0004:    ldc.i4.s 31     ; 1f 1f
IL_0006:    stloc.1         ; 0b
IL_0007:    ldloc.0         ; 06
IL_0008:    ldloc.1         ; 07
IL_0009:    add             ; 58

Doing the same with float32 instead of int32 by using ldc.r4 <num> there is no difference in calling add thus making me wonder whether there are different stacks for different types or if there is only one stack which holds metadata which type a specific element has on the stack. Is there any information about the specific implementation in ECMA-335 or somewhere else?

like image 958
Christian Ivicevic Avatar asked Jun 02 '14 13:06

Christian Ivicevic


2 Answers

This is specifically addressed in Partition I, part 12 (from e.g. this pdf), which discusses the Virtual Execution System (VES):

As described below, CIL instructions do not specify their operand types. Instead, the CLI keeps track of operand types based on data flow and aided by a stack consistency requirement described below. For example, the single add instruction will add two integers or two floats from the stack.

And:

Most CIL instructions that deal with numbers take their operands from the evaluation stack (see §I.12.3.2.1), and these inputs have an associated type that is known to the VES. As a result, a single operation like add can have inputs of any numeric data type, although not all instructions can deal with all combinations of operand types.

Where I.12.1.4 goes into considerably more detail also.

like image 73
Damien_The_Unbeliever Avatar answered Nov 15 '22 11:11

Damien_The_Unbeliever


The JIT infers the types. It has to do this anyway to type-check your program. There is no need to parameterize operations for the types they operate on. The types and size of the stack are computable at any point in the IL instruction sequence. If they are not computable or ambiguous the program is unverifiable.

I believe Java IL does this differently but I might be mistaken.

like image 31
usr Avatar answered Nov 15 '22 12:11

usr