LLVM Europe 2012: A Portable C++/CLI Compiler

This afternoon I’m giving a brief talk on the Nuanti C++/CLI compiler built on Clang at the LLVM European Conference, London.

We started this project a couple of years ago to cover the language specification in ECMA-372, having found it a pain to integrate applications and SDK written in C and C++ with .NET. Since then, the compiler has developed into a self-hosting toolchain that’s been used to port entire applications to run on Mono, Silverlight, Mac OS X and Linux.

There are two codegen modes: (1) fully managed .NET bytecode, and (2) native optimized machine code with hooks into the Mono runtime for invocations. There’s some flexibility to mix and match the two. Sometimes it’s handy to call into an existing managed library from your C++ application and this use case is simple as adding an import header and calling the methods you need. No bindings or wrappers, and if using a runtime like Mono the stack is garbage collected behind the scenes.

The frontend has all the parser support you’d expect plus CLR metadata import. Meanwhile there’s a CIL backend using the LLVM codegen infrastructure. The two work together to generate native-looking SDK-quality assemblies out of all kinds of unmanaged code.

The implementation embraces and extends the ECMA spec with dynamic types from C# 4.0, LINQ for C++, expression trees and full-blown DLR syntax trees, going beyond the original specification in many ways. This is the real interesting part, since it’s what we use to bootstrap the compiler (so it can compile itself).

We were able to port User Mode Linux and WebKit to Silverlight using this with a certain amount of patching. There are other fascinating integration points like a pretty neat Objective-C++/CLI bridge.

Our main focus has been in-game ‘scripting’ and IOS applications. Going forward we’re concentrating more on open web standards and JavaScript for the platform but this continues to be a fun R&D project with often surprising uses, and it’ll be a fun story to tell.

See you there!