Ayende @ Rahien

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

ayende@ayende.com

+972 52-548-6969

, @ Q c

Posts: 5,968 | Comments: 44,489

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. Career planning (6):
    24 Jul 2015 - The immortal choices aren't
  2. Production postmortem (4):
    23 Jul 2015 - The case of the native memory leak
  3. API Design (7):
    20 Jul 2015 - We’ll let the users sort it out
  4. What is new in RavenDB 3.5 (3):
    15 Jul 2015 - Exploring data in the dark
  5. The RavenDB Comic Strip (3):
    28 May 2015 - Part III – High availability & sleeping soundly
View all series

RECENT COMMENTS

Syndication

Main feed Feed Stats
Comments feed   Comments Feed Stats