all groups > dotnet clr > july 2006 >
You're in the

dotnet clr

group:

Pointless IL Instructions?


Pointless IL Instructions? Ben R.
7/26/2006 12:07:02 PM
dotnet clr:
Thought this was a better forum for this question:

I've got a simple console app which just assigns 6 to an int and prints out
the value. The IL looks like:

..method public static void Main() cil managed
{
.entrypoint
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01
00 00 00 )
// Code size 12 (0xc)
.maxstack 1
.locals init ([0] int32 x)
IL_0000: nop
IL_0001: ldc.i4.6
IL_0002: stloc.0
IL_0003: ldloc.0
IL_0004: call void [mscorlib]System.Console::Write(int32)
IL_0009: nop
IL_000a: nop
IL_000b: ret
} // end of method Module1::Main

What is the point of these:
IL_0002: stloc.0
IL_0003: ldloc.0

Why store something in a variable just to put the exact thing right back on
the stack where it was? The proper value was on the stack before that anway,
right? Couldn't these two lines be taken out?

-Ben
Re: Pointless IL Instructions? Michael Nemtsev
7/26/2006 7:48:15 PM
Hello Ben R.,

Don't crosspost.
I've replied in framework group

B> Thought this was a better forum for this question:
B>
B> I've got a simple console app which just assigns 6 to an int and
B> prints out the value. The IL looks like:
B>
B> .method public static void Main() cil managed
B> {
B> .entrypoint
B> .custom instance void [mscorlib]System.STAThreadAttribute::.ctor()
B> = ( 01
B> 00 00 00 )
B> // Code size 12 (0xc)
B> .maxstack 1
B> .locals init ([0] int32 x)
B> IL_0000: nop
B> IL_0001: ldc.i4.6
B> IL_0002: stloc.0
B> IL_0003: ldloc.0
B> IL_0004: call void [mscorlib]System.Console::Write(int32)
B> IL_0009: nop
B> IL_000a: nop
B> IL_000b: ret
B> } // end of method Module1::Main
B> What is the point of these:
B> IL_0002: stloc.0
B> IL_0003: ldloc.0
B> Why store something in a variable just to put the exact thing right
B> back on the stack where it was? The proper value was on the stack
B> before that anway, right? Couldn't these two lines be taken out?
B>
B> -Ben
B>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsch

Re: Pointless IL Instructions? Barry Kelly
7/27/2006 12:24:06 AM
[quoted text, click to view]

The CLI isn't a machine - it's a serialization format for semantics.
When viewing the stack machine code, think 'serialized expression
graph', not 'CPU opcodes'.

-- Barry

--
Re: Pointless IL Instructions? Jon Shemitz
7/27/2006 8:33:55 AM
[quoted text, click to view]

Or, to translate that into language the OP may understand: CIL is a
program that the jitter will compile, and the jitter is perfectly
capable of optimizing out the unnecessary use of a local variable.

--

..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
Re: Pointless IL Instructions? Barry Kelly
7/27/2006 4:48:51 PM
[quoted text, click to view]

Is it that bad :) I am spending too much time in compiler-land...

-- Barry

--
AddThis Social Bookmark Button