Subterranean IL: ThreadLocal revisited

0
0
Last year, I looked at the ThreadLocal type as it exists in .NET 4. In .NET 4.5, this type has been completely rewritten. In this post, I’ll be looking at how the new ThreadLocal works in .NET 4.5. I won’t be looking at all the implementation details, but concentrating on how this type works. Again, … Read more

Subterranean IL: The ThreadLocal type

0
1
I came across ThreadLocal<T> while I was researching ConcurrentBag. To look at it, it doesn’t really make much sense. What’s all those extra Cn classes doing in there? Why is there a GenericHolder<T,U,V,W> class? What’s going on? However, digging deeper, it’s a rather ingenious solution to a tricky problem. Thread statics Declaring that a variable … Read more

Subterranean IL: Explicit overrides

0
0
Normally, virtual method overrides in .NET are done implicitly; if a subclass has a virtual method with the same name and signature as a virtual method in a base class, then the method in the subclass overrides the method in the base class: This is in contrast to C# and VB, where a ‘base’ virtual … Read more

Subterranean IL: Exception handler semantics

0
0
In my blog posts on fault and filter exception handlers, I said that the same behaviour could be replicated using normal catch blocks. Well, that isn’t entirely true… Changing the handler semantics Consider the following: If the filter handler is engaged (true is inserted into the exception dictionary), then the following gets printed to the … Read more

Subterranean IL: Filter exception handlers

0
0
Filter handlers are the second type of exception handler that aren’t accessible from C#. Unlike the other handler types, which have defined conditions for when the handlers execute, filter lets you use custom logic to determine whether the handler should be run. However, similar to a catch block, the filter block does not get run … Read more

Subterranean IL: Fault exception handlers

0
0
Fault event handlers are one of the two handler types that aren’t available in C#. It behaves exactly like a finally, except it is only run if control flow exits the block due to an exception being thrown. As an example, take the following method: If we pass true to this method the following gets … Read more

Subterranean IL: Compiling C# exception handlers

0
0
An exception handler in C# combines the IL catch and finally exception handling clauses into a single try statement: How does this get compiled into IL? Initial implementation If you remember from my earlier post, finally clauses must be specified with their own .try clause. So, for the initial implementation, we take the try/catch/finally, and … Read more

Subterranean IL: Exception handling control flow

0
0
Control flow in and around exception handlers is tightly controlled, due to the various ways the handler blocks can be executed. To start off with, I’ll describe what SEH does when an exception is thrown. Handling exceptions When an exception is thrown, the CLR stops program execution at the throw statement and searches up the … Read more

Subterranean IL: Exception handling

0
0
Today, I’ll be starting a look at the Structured Exception Handling mechanism within the CLR. Exception handling is quite a complicated business, and, as a result, the rules governing exception handling clauses in IL are quite strict; you need to be careful when writing exception clauses in IL. Exception handlers Exception handlers are specified using … Read more

Subterranean IL: Pseudo custom attributes

0
0
Custom attributes were designed to make the .NET framework extensible; if a .NET language needs to store additional metadata on an item that isn’t expressible in IL, then an attribute could be applied to the IL item to represent this metadata. For instance, the C# compiler uses DecimalConstantAttribute and DateTimeConstantAttribute to represent compile-time decimal or … Read more

Subterranean IL: Custom modifiers

0
0
In IL, volatile is an instruction prefix used to set a memory barrier at that instruction. However, in C#, volatile is applied to a field to indicate that all accesses on that field should be prefixed with volatile. As I mentioned in my previous post, this means that the field definition needs to store this … Read more

Subterranean IL: Volatile

0
0
This time, we’ll be having a look at the volatile. prefix instruction, and one of the differences between volatile in IL and C#. The volatile. prefix volatile is a tricky one, as there’s varying levels of documentation on it. From what I can see, it has two effects: It prevents caching of the load or … Read more

Subterranean IL: Constructor constraints

0
0
The constructor generic constraint is a slightly wierd one. The ECMA specification simply states that it: constrains [the type] to being a concrete reference type (i.e., not abstract) that has a public constructor taking no arguments (the default constructor), or to being a value type. There seems to be no reference within the spec to … Read more

Subterranean IL: Generics and array covariance

0
0
Arrays in .NET are curious beasts. They are the only built-in collection types in the CLR, and SZ-arrays (single dimension, zero-indexed) have their own commands and IL syntax. One of their stranger properties is they have a kind of built-in covariance long before generic variance was added in .NET 4. However, this causes a subtle … Read more

Subterranean IL: Callvirt and generic types

0
0
In this post we finally get on to how basic generic methods are implemented in IL. First of all, we should briefly cover how a generic method is declared in IL. Declaring a generic method The basic syntax for a generic method is fairly straightfoward. The following C# generic method definition: corresponds to the following … Read more

Subterranean IL: Callvirt and virtual methods

0
0
Next up is a look at the details of callvirt and what happens when you call a virtual method. However, in order to do that, we first need to understand some of the low-level CLR datastructures, and what exactly an object instance is. Object instances Object instances are actually suprisingly small and lightweight things – … Read more

Subterranean IL: Calling methods

0
0
Next in my ‘Subterranean IL’ series, I’ll be looking at method calls, and in particular, the difference between reference and value types. To help, I’ll be using the following types (definitions given in C# for brevity)1: Reference types We’ll be looking at how you call the Increment method on instances of these types in IL. … Read more

Subterranean IL: Introduction

0
0
Today, I’ll be starting a new series of blog posts on ‘Subterranean IL’ – a look at the low-level IL commands available to .NET compilers, what each command does (or at least the more interesting commands) and why each command does what it does. One of the first things I’ll be looking at are the … Read more