Search code examples
c#.netvb.netcilcallstack

How are the call stack and the evaluation stack related?


A second attempt at phrasing this:

I am currently taking baby steps in getting to grips with some MSIL.

I keep hearing the 'Evaluation Stack' talked about as the stack that operations are pushed and poped as they are loaded, used etc.

So, given the following code:

public class Program
{
    public static void Main()
    {
        var message = GetMessage();

        Console.WriteLine(message);
    }

    public static string GetMessage()
    {
        return "Hello World!";
    }
}

The MSIL looks as so:

.class public auto ansi beforefieldinit Program
    extends [mscorlib]System.Object
{
    .method public hidebysig specialname rtspecialname instance void .ctor () cil managed 
    {
        IL_0000: ldarg.0
        IL_0001: call instance void [mscorlib]System.Object::.ctor()
        IL_0006: ret
    }

    .method public hidebysig static string GetMessage () cil managed 
    {
        .locals init (
            [0] string V_0
        )

        IL_0000: nop
        IL_0001: ldstr "Hello World!"
        IL_0006: stloc.0
        IL_0007: br.s IL_0009

        IL_0009: ldloc.0
        IL_000a: ret
    }

    .method public hidebysig static void Main () cil managed 
    {
        .entrypoint
        .locals init (
            [0] string V_0
        )

        IL_0000: nop
        IL_0001: call string Program::GetMessage()
        IL_0006: stloc.0
        IL_0007: ldloc.0
        IL_0008: call void [mscorlib]System.Console::WriteLine(string)
        IL_000d: nop
        IL_000e: ret
    }
}

I think that the lines which start with IL_xxxx are the evaluation stack (please correct me if I am wrong). So the line call string Program::GetMessage() is the line that calls another method.

So, I guess that question I am asking is this:

  1. Is each line starting with IL_0000 a 'new' evaluation stack?
  2. Is the call stack I see at runtime a joining\concatenation\filtering of this IL?

Solution

  • I think that the lines which start with IL_xxxx are the evaluation stack

    Is each line starting with IL_0000 a 'new' evaluation stack?

    No. There is one evaluation stack per method. Think of it as a Stack<object>. IL instructions operate on that stack by pushing and popping items. The MSDN documentation calls out the stack behavior for each IL opcode precisely.

    The call stack has nothing to do with the evaluation stack. The call stack is what the Call Stack Visual Studio window shows you.

    Is the call stack I see at runtime a joining\concatenation\filtering of this IL?

    No. There is no relationship.

    I feel that your thinking of the evaluation stack is too complicated. It is quite an easy concept. As long as you have something complicated in mind you have not grasped it. I'm not sure where exactly the misunderstanding lies, though.

    Note, that the evaulation stack is a logical concept used to define the meaning of IL programs. It does not exist at runtime anymore in contrast to the call stack which (barring inlining) does exist.