[mono-cecil] cecil and local variables

791 views
Skip to first unread message

Jan

unread,
Apr 26, 2010, 8:04:59 AM4/26/10
to mono-cecil
Hi JB,

I am adding a bunch of new functions to existing classes. Because I am
porting from System.Emit I can compare both APIs.

I am adding local variables to the new function with:

VariableDefinition loc0 = new VariableDefinition("loc0",
ModuleDefinition.Import(typeof(System.Reflection.FieldInfo[])));
md.Body.Variables.Add(loc0);

If I now create a usage of the variable with (OpCodes.Stloc, loc0) I
always get a OpCodes.Stloc, with Emit I get a OpCodes.Stloc.0 or a
OpCodes.Stloc.1 for the first and second defined variable.
With Cecil it looks for me I have to do this by hand, where I have to
be sure that the ordering of the variables is stable
(OpCodes.Stloc_0). How can I be sure that I am refering to the right
slot, or is there a way to do it like with Emit?

Thanks

Jan

--
--
mono-cecil

Subscription settings: http://groups.google.com/group/mono-cecil/subscribe?hl=en

Jb Evain

unread,
Apr 26, 2010, 9:16:59 AM4/26/10
to mono-...@googlegroups.com
Hey Jan,

On Mon, Apr 26, 2010 at 2:04 PM, Jan <jan.ble...@gmail.com> wrote:
> If I now create a usage of the variable with (OpCodes.Stloc, loc0) I
> always get a OpCodes.Stloc, with Emit I get a OpCodes.Stloc.0 or a
> OpCodes.Stloc.1 for the first and second defined variable.
> With Cecil it looks for me I have to do this by hand, where I have to
> be sure that the ordering of the variables is stable
> (OpCodes.Stloc_0). How can I be sure that I am refering to the right
> slot, or is there a way to do it like with Emit?

What you can do is to emit Stloc and Ldoc and pass the
VariableDefinition. When you're done with the method, you can call

body.Optimize ()

And it will turn the ldloc into ldloc.0.

Optimize is an extension method in Mono.Cecil.Rocks.

--
Jb Evain <j...@nurv.fr>

Jan

unread,
Apr 26, 2010, 10:20:12 AM4/26/10
to mono-cecil
Hi JB

On Apr 26, 3:16 pm, Jb Evain <j...@nurv.fr> wrote:
> body.Optimize ()
>
> And it will turn the ldloc into ldloc.0.
>
> Optimize is an extension method in Mono.Cecil.Rocks.
>

I only can see OptimizeMacros in 0.9.1.34. It looks like the one you
mean.

Jan

Jb Evain

unread,
Apr 26, 2010, 10:28:21 AM4/26/10
to mono-...@googlegroups.com
Hey,

On Mon, Apr 26, 2010 at 4:20 PM, Jan <jan.ble...@gmail.com> wrote:
> I only can see OptimizeMacros in 0.9.1.34. It looks like the one you
> mean.

Yeah, sorry, it's the old name. SimplifyMacros and OptimizeMacros are
better worded.

--
Jb Evain <j...@nurv.fr>
Reply all
Reply to author
Forward
0 new messages