mirjit.cjit

mirjit.cjit — an idiomatic D layer over the c2mir C11->MIR frontend.

Compile a translation unit of C source once with jit, then pull out typed, native function pointers with CModule.get:

auto m   = jit("int add(int a, int b) { return a + b; }");
auto add = m.get!(int function(int, int))("add");
assert(add(2, 3) == 5);

get takes the function-pointer type you want to retrieve, e.g. int function(int, int) or a named alias. Whatever linkage and attributes it carries are normalized to extern(C) ... nothrow @nogc. Only C-compatible scalar and pointer types are accepted; anything else is a compile-time error.

CModule owns the MIR context and all generated code: the returned function pointers are valid only while the CModule is alive (same contract as a dlopen handle). It is non-copyable and frees everything in its destructor.

This module is only compiled when the MirjitC2mir version is set, which the with-c2mir dub configuration does (it also links the c2mir frontend).

Members

Classes

CompileException
class CompileException

Thrown by jit when c2mir rejects the C source. The message carries the diagnostics c2mir produced.

SignatureException
class SignatureException

Thrown by CModule.get when the requested D signature is incompatible with the compiled C function's prototype.

SymbolException
class SymbolException

Thrown by CModule.get when no function of that name exists.

Functions

jit
CModule jit(string source, COptions opts)

Compile one translation unit of C source to MIR and return an owning CModule. C syntax/semantic errors surface here as CompileException.

Structs

CModule
struct CModule

A compiled C translation unit. Owns the MIR context; hands out typed native function pointers via get. Non-copyable; frees all code on destruction.

COptions
struct COptions

Options controlling a jit compilation.