Compiler Runtime

The compiler runtime is largely a black box containing support code specific to the compiler implementation. However, because the compiler is most aware of program memory layout, platform requirements, and so on, the compiler runtime is the optimal location for an API concerning such aspects of the application.

Interface Definition

This is a concise definition of the routines that every compiler runtime implementation must expose:

alias Throwable.TraceInfo function(void* ptr = null) TraceHandler;
alias void delegate(Exception) ExceptionHandler;
alias bool function(Object) CollectHandler;
alias void delegate(Object) DisposeEvent;

extern (C) bool  rt_init( ExceptionHandler dg = null );
extern (C) bool  rt_term( ExceptionHandler dg = null );
extern (C) bool  rt_trapExceptions = true;
extern (C) bool  rt_isHalting();

extern (C) void* rt_stackBottom();
extern (C) void* rt_stackTop();

extern (C) void  rt_finalize( void* p, bool det = true );

extern (C) void* rt_loadLibrary( in char[] name );
extern (C) bool  rt_unloadLibrary( void* ptr );

extern (C) void  rt_setTraceHandler( TraceHandler h );
extern (C) void  rt_setCollectHandler( CollectHandler h );
extern (C) void  rt_attachDisposeEvent( Object obj, DisposeEvent evt );
extern (C) void  rt_detachDisposeEvent( Object obj, DisposeEvent evt );

TraceInfo must be defined as follows:

class Throwable : Object
{
    interface TraceInfo
    {
        int opApply( int delegate(inout char[]) );
        char[] toString();
    }
}

Given the following interface:

    class Object
    {
        interface Monitor
        {
            void lock();
            void unlock();
        }
    }

Object monitors must be structured like so:

    alias Object.Monitor IMonitor;

    struct Monitor
    {
        IMonitor impl;
        /* data */
    }

If impl is null then the default monitor implementation will be used. Otherwise, monitor locking and unlocking will be performed via impl. Additionally, the use of rt_attachDisposeEvent and rt_detachDisposeEvent is only supported on objects without user-defined monitors. The reasoning is that objects with user-defined monitors may have special lifetime or storage requirements that invalidate any reason for the lifetime monitoring feature.

Implementation Requirements

The package name "rt" is reserved for use by the compiler runtime, and all modules defined should live within this namespace to avoid collisions with user code. More requirements coming soon.