[Mono-dev] Questions on Cecil

Jb Evain mono at evain.net
Sun Aug 13 06:08:21 EDT 2006

Hello Bjarke,

Answers inline.

Bjarke Hammersholt Roune wrote:
> 1. I'm using ildasm and the largely uncommented Cecil source as 
> documentation. Is there some actual documentation other than the FAQ?

No, sorry.

> 2. I need to know the types of the things on the stack. Can Cecil help 
> with that? If not, is there some nice way to get .NET to handle the 
> operations on the type lattice?

When you inspect the method body, you can deduce the stack. The OpCode 
has all the details. Check the OperandType and the StackBehaviour 

> 3. When emitting a Br (branch) instruction, Cecil insists on getting the 
> target instruction immediately. The trouble is that when I am writing 
> out CIL to Cecil from the CIL representation internal to my program, it 
> is often the case that the target of the branch has not been emitted 
> yet. Currently I get around this by passing it a dummy Nop instruction 
> and then correcting the target in a second pass. Is there some way to 
> avoid constructing a dummy instruction, such as by getting Cecil to 
> accept a null as the (preliminary) target? (When I try to do that the 
> obvious way I get an exception)

Well, I could eventually remove the null check, but if you forgot to 
patch the operand, it will point to the end of the CIL stream.

> 4. Cecil requires that the type of a local variable be specified as a 
> TypeReference, so I need to get those. Is there some way to get a 
> TypeReference for built in types like System.Int32 without loading an 
> assembly? The way I am doing it now is getting it as 
> assembly.MainModule.TypeReferences["System.Int32"].

assembly.MainModule.Import("System.Int64") will do.

> 5. I would like to emit the short form of the Br instruction when this 
> is possible. My initial idea is to always emit the short form, and then 
> expand to the long form where necessary. That makes it necessary to take 
> into account the possibility of a cascade where expanding one 
> instruction increases the distance to the target of another Br 
> instruction which then also needs to be expanded to the long form and so 
> on. I would like to avoid this trouble, so is there some way to get 
> Cecil to do this for me?

No, at least, not yet.

Feel free to ask if you have other questions,


More information about the Mono-devel-list mailing list