Ayende @ Rahien

Hi!
My name is Ayende Rahien
Founder of Hibernating Rhinos LTD and RavenDB.
You can reach me by phone or email:

ayende@ayende.com

+972 52-548-6969

@

Posts: 5,947 | Comments: 44,540

filter by tags archive

IL Weaving - Mystery wrapped in an Enigma containing Frustration


Okay, here is another exampe of using Cecil, but I think that you can figure out what I think about having to deal with raw IL.

Feel free to figure out what this does, and why I would be interested in such a thing. BTW, in Boo to Boo, that sort of thing so painless. Having to deal with the raw IL takes all the fun out of it.

I know about Cecil.FlowControl, but as far as I can find, this is mostly (only?) for reading assemblies. I am looking for a way to have a reasonable AST over cecil. This is not scalable no matter how you cast it.

import Mono.Cecil

import Mono.Cecil.Cil

class DisposableAttribute(Attribute):

      def Process(t as TypeDefinition):

            disposed = FieldDefinition("___was_disposed",t.Module.Import(bool), FieldAttributes.Private)

            t.Fields.Add(disposed)

            t.Interfaces.Add( t.Module.Import(typeof(IDisposable)))

           

            for method as MethodDefinition in t.Methods:

                  cil = method.Body.CilWorker

                  ins = method.Body.Instructions[0];

                 

                  if method.Name=="Dispose":

                        cil.InsertBefore(ins,cil.Create(OpCodes.Ldarg_0))

                        cil.InsertBefore(ins,cil.Create(OpCodes.Ldc_I4_0))

                        cil.InsertBefore(ins,cil.Create(OpCodes.Stfld, disposed))

                        continue

                 

                  wasDisposedLoc = VariableDefinition(t.Module.Import(bool))

                  method.Body.Variables.Add( wasDisposedLoc )

                 

                  cil.InsertBefore(ins,cil.Create(OpCodes.Ldarg_0))

                  cil.InsertBefore(ins,cil.Create(OpCodes.Ldfld,disposed))

                  cil.InsertBefore(ins,cil.Create(OpCodes.Ldc_I4_0))

                  cil.InsertBefore(ins,cil.Create(OpCodes.Ceq))

                  cil.InsertBefore(ins,cil.Create(OpCodes.Stloc_0))

                  cil.InsertBefore(ins,cil.Create(OpCodes.Ldloc_0))

                  cil.InsertBefore(ins, cil.Create(OpCodes.Brtrue_S,ins))

                  cil.InsertBefore(ins,cil.Create(OpCodes.Ldstr, t.FullName))

                  cil.InsertBefore(ins,cil.Create(OpCodes.Newobj,
                                      t.Module.Import(typeof(ObjectDisposedException).GetConstructors()[0]) ))

                  cil.InsertBefore(ins,cil.Create(OpCodes.Throw))

Chris, compiler!


Comments

Jb Evain

Being able to get a complete AST from a Cecil assembly, being able to modify it and to save it back is one of the goal of the Milo [1] framework. The goal is to combine Cecil.FlowAnalysis, a modified Boo AST and Cecil.

Even if we're sort of busy, Rodrigo and I are working on that.

[1] http://code.google.com/p/milo

Max Bolingbroke

I've been trying to implement some of these macros for .NET too.. am I to take it you already have such a project on the go? In particular, have you been able to implement DefaultAttribute ala Boo? I couldn't work out how to reference the attribute constructor parameters from the generated IL..

BTW, your blogs "email" field does not accept some well formed emails, in particular those with a + in the first part of the address..

Paul Burns

Firstly, thanks! I've been reading the blog for a few month now after being slamdunked into the world of .NET, C#, Monorail and full time coding.

Have to say its been a good way to get on the bandwagon and keep up with anything and everything. Not that I understand alot of it, but I guess we all have to start somewhere.

Secondly, I have a request.. While I can always go away and read up about the topic of your posts if necessary, sometimes I'm confused by the acronyms. I don't know how useful it might be to everyone else, but as a suggestion some kind of automated text to link conversion for your acronyms might be good.

ie) 'Monorail' pointing to http://www.castleproject.org/

Anyway, just a suggestion, I know it's not going to be too high on your priority list right now :-)

Comment preview

Comments have been closed on this topic.

FUTURE POSTS

No future posts left, oh my!

RECENT SERIES

  1. RavenDB Sharding (2):
    21 May 2015 - Adding a new shard to an existing cluster, the easy way
  2. The RavenDB Comic Strip (2):
    20 May 2015 - Part II – a team in trouble!
  3. Challenge (45):
    28 Apr 2015 - What is the meaning of this change?
  4. Interview question (2):
    30 Mar 2015 - fix the index
  5. Excerpts from the RavenDB Performance team report (20):
    20 Feb 2015 - Optimizing Compare – The circle of life (a post-mortem)
View all series

RECENT COMMENTS

Syndication

Main feed Feed Stats
Comments feed   Comments Feed Stats