Mono.Cecil – Simplify and Optimize macros

You may have seen it, but what does it actually do? Do I need to add them?

When we write IL macros, we emit instruction after instruction to the method body. Instruction, is operation to do (e.g. math operations, load\store, invocations etc.) and it may have operand. The instruction is 1-byte or 2-bytes length.

Lets look on an example of loading method argument:

ldarg 1

ldarg is a 2-bytes instruction – FE 09 – that takes 2-bytes operand with the argument index to load.

But methods are rarely have more than a few arguments and we don’t really need a 2-bytes operand.

For this we have ldarg.s . meaning, a short form that takes only 1-byte for the argument index.

(actually there is a 1-byte ldarg.0, ldarg.1, ldarg.2, ldarg.3 beacuse they are used very often).

Cecil takes care for this optimization by calling OptimizeMacros

SimplifyMacros in the other hand, is the opposite. You need it when you rewrite an existing method. Because you may change the number of arguments\local variables\methods bytes, and it may affect the short form instruction.

So before you start to rewrite, call SimplifyMacros and when you finish, call OptimizeMacros.

That all for now 🙂

Advertisements
This entry was posted in .NET, c# and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s