B
Brian Tyler
I have seen a very strange piece of code being generated by the C# compiler
and was hoping someone might be able to shed some light on it. I've reduced
the example down to a bare minimum and I am using the .NET 1.1 framework.
Here is the C# code:
using System;
namespace Example
{
class SwitchTest
{
static void Main(string[] args)
{
switch(args[0])
{
case "A":
break;
case "B":
break;
}
}
}
}
And this is the IL generated (pretty much the same for debug and release):
..method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 43 (0x2b)
.maxstack 2
.locals init (string V_0)
IL_0000: ldstr "A"
IL_0005: ldstr "B"
IL_000a: leave.s IL_000c
IL_000c: ldarg.0
IL_000d: ldc.i4.0
IL_000e: ldelem.ref
IL_000f: dup
IL_0010: stloc.0
IL_0011: brfalse.s IL_002a
IL_0013: ldloc.0
IL_0014: call string [mscorlib]System.String::IsInterned(string)
IL_0019: stloc.0
IL_001a: ldloc.0
IL_001b: ldstr "A"
IL_0020: beq.s IL_002a
IL_0022: ldloc.0
IL_0023: ldstr "B"
IL_0028: beq.s IL_002a
IL_002a: ret
} // end of method SwitchTest::Main
QUESTION: What is the purpose of the two ldstr for the case statement
values? The LEAVE instruction clears out the eval stack so they aren't used
elsewhere. I thought at first that it might have something to do with the
Interned string cache, but these are constants and so should already be in
the cache.
Any ideas?
Brian
(e-mail address removed)
and was hoping someone might be able to shed some light on it. I've reduced
the example down to a bare minimum and I am using the .NET 1.1 framework.
Here is the C# code:
using System;
namespace Example
{
class SwitchTest
{
static void Main(string[] args)
{
switch(args[0])
{
case "A":
break;
case "B":
break;
}
}
}
}
And this is the IL generated (pretty much the same for debug and release):
..method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 43 (0x2b)
.maxstack 2
.locals init (string V_0)
IL_0000: ldstr "A"
IL_0005: ldstr "B"
IL_000a: leave.s IL_000c
IL_000c: ldarg.0
IL_000d: ldc.i4.0
IL_000e: ldelem.ref
IL_000f: dup
IL_0010: stloc.0
IL_0011: brfalse.s IL_002a
IL_0013: ldloc.0
IL_0014: call string [mscorlib]System.String::IsInterned(string)
IL_0019: stloc.0
IL_001a: ldloc.0
IL_001b: ldstr "A"
IL_0020: beq.s IL_002a
IL_0022: ldloc.0
IL_0023: ldstr "B"
IL_0028: beq.s IL_002a
IL_002a: ret
} // end of method SwitchTest::Main
QUESTION: What is the purpose of the two ldstr for the case statement
values? The LEAVE instruction clears out the eval stack so they aren't used
elsewhere. I thought at first that it might have something to do with the
Interned string cache, but these are constants and so should already be in
the cache.
Any ideas?
Brian
(e-mail address removed)