cil

Virtual Machine Optimization

I am messing around with a toy interpreter in Java and I was considering trying to write a simple compiler that can generate bytecode for the Java Virtual Machine. Which got me thinking, how much optimization needs to be done by compilers that target virtual machines such as JVM and CLI? Do Just In Time (JIT) compilers do constant foldi...

Learning CIL

Does anybody know any good resources for learning how to program CIL with in-depth descriptions of commands, etc.? I have looked around but not found anything particularly good. ...

Using the DLR for (primarily) static language compilation...

I'm building a compiler that targets .NET and I've previously generated CIL directly, but generating DLR trees will make my life a fair amount easier. I'm supporting a few dynamic features, namely runtime function creation and ducktyping, but the vast majority of the code is completely static. So now that that's been explained, I have ...

Is it possible to indirectly load a value type on the stack

In Microsoft IL, to call a method on a value type you need an indirect reference. Lets say we have an ILGenerator named "il" and that currently we have a Nullable on top of the stack, if we want to check whether it has a value then we could emit the following: var local = il.DeclareLocal(typeof(Nullable<int>)); il.Emit(OpCodes.Stloc, lo...

Is there a way to compile C++ code to Microsoft's CIL "bytecode"?

i.e., a web browser client would be written in C++ !!! ...

Differences between MSIL and Java bytecode?

I'm new to .Net and I'm trying to understand the basics first. What is the difference between MSIL and Java bytecode? ...

How Do VB.Net Optional Parameters work 'Under the hood'? Are they CLS Compliant?

Let's say we have the following method declaration: Public Function MyMethod(ByVal param1 As Integer, _ Optional ByVal param2 As Integer = 0, _ Optional ByVal param3 As Integer = 1) As Integer Return param1 + param2 + param3 End Function How does VB.NET make the optional parameters work within the confines of the CLR? Are...

Modifying Existing .NET Assemblies

Is there a way to modify existing .NET assemblies without resorting to 3rd party tools? I know that PostSharp makes this possible but I find it incredibly wasteful that the developler of PostSharp basically had to rewrite the functionality of the whole System.Reflection namespace in order to make existing assemblies modifiable. System.R...

Is there a CLR that runs on the CLR ?

I was wondering if there was a .NET-compatible CLR that was implemented using the CLI (common language infrastructure), e.g., using .NET itself, or at least if there were any resources that would help with building one. Basically, something like a .NET program that loads assemblies as MemoryStreams, parses the bytecode, constructs the t...

What are the best resources for learning CIL (MSIL)

I'm an expert C# 3 / .NET 3.5 programmer looking to start doing some runtime codegen using System.Reflection.Emit.DynamicMethod. I'd love to move up to the next level by becoming intimately familiar with IL. Any pointers (pun intended)? ...

Is there a way to count the number of IL instructions executed?

I want to do some benchmarking of a C# process, but I don't want to use time as my vector - I want to count the number of IL instructions that get executed in a particular method call. Is this possible? Edit I don't mean static analysis of a method body - I'm referring to the actual number of instructions that are executed - so if, for ...

CIL stack exchange instruction

Is there CIL instruction to exchange the first two elements in the stack? ...

Call and Callvirt

What is the difference between the CIL instructions "Call" and "Callvirt"? ...

Is there a good wrapper around ILGenerator ?

I'm using System.Reflection.Emit for a while now, and find it (who don't?) as painful as bug prone. Do you know if there is a good wrapper around the IL Generator, something that I can rely on to emit IL in a more safe and easier manner than with playing directly with SRE? Edit: I know that manipulating expression trees is definitivel...

Reasons to learn MSIL

Hi, Learning MSIL is fun and all that. Understanding what is going on "under the hood" can in many ways improve how you write your code performance-wise. However, the IL that is produced by the compiler is quite verbose and does not tell the whole story since JIT will optimize away a lot of the code. I, personally, have had good use of...

If events are implemented as delegates in .NET, what is the point of the .event IL section?

I've seen some very good questions on Stack Overflow concerning delegates, events, and the .NET implementation of these two features. One question in particular, "How do C# Events work behind the scenes?", produced a great answer that explains some subtle points very well. The answer to the above question makes this point: When y...

What's the purpose of the nop opcode?

I'm going through MSIL and noticing there are a lot of nop instructions. The MSDN article says they take no action and are used to fill space if the opcode is patched. They're used a lot more in debug builds than release builds. I know that these kinds of statements are used in assembly languages to make sure an opcode fits on a word bou...

Would an automatic MSIL to JavaScript conversion be useful?

I've been working on a project called Axial that converts MSIL (compiled C# or VB.NET) to JavaScript. There are a few samples of working code, but some common situations don't work properly. (The current release doesn't work in production mode and the SVN code doesn't work in debug mode but is much cleaner.) I've heard from quite a few p...

What's the MSIL to call a base class's event handler?

I have a class called EventConsumer which defines an event EventConsumed and a method OnEventConsumed as follows: public event EventHandler EventConsumed; public virtual void OnEventConsumed(object sender, EventArgs e) { if (EventConsumed != null) EventConsumed(this, e); } I need to add attributes to the at OnEventConsume...

Why is it necessary to call :this() on a struct to use automatic properties in c#?

If I define a struct in C# using automatic properties like this: public struct Address { public Address(string line1, string line2, string city, string state, string zip) { Line1 = line1; Line2 = line2; City = city; State = state; Zip = zip; } public string Line1 { get; protected ...