module volt.interfaces

Code Map

module volt.interfaces;

public import volta.interfaces;


//! Used to determine the output of the backend.
enum TargetType
{
	DebugPrinting,
	LlvmBitcode,
	VoltCode,
	Object,
	CCode,
	Host,
}

//! Interface implemented by backends. Often the last stage of the compile
//! pipe that is implemented in this compiler, optimization and linking are
//! often done outside of the compiler, either invoked directly by us or a
//! build system.
interface Backend
{
public:
	//! Free resources.
	fn close();
	//! Return the supported target types.
	fn supported() TargetType[];
	//! Compile the given module to either a file or host result.
	fn compileFile(m: ir.Module, type: TargetType, ehPersonality: ir.Function, llvmTypeidFor: ir.Function, execDir: string, currentWorkingDir: string, identStr: string) BackendFileResult;
	//! Compile the given module to either a file or host result.
	fn compileHost(m: ir.Module, ehPersonality: ir.Function, llvmTypeidFor: ir.Function, execDir: string, currentWorkingDir: string, identStr: string) BackendHostResult;
}

//! A result from a backend compilation that can be saved onto disk.
interface BackendFileResult
{
public:
	//! Free resources.
	fn close();
	//! Save the result to disk.
	fn saveToFile(filename: string);
}

//! A JIT compiled a module that you can fetch functions from.
interface BackendHostResult
{
public:
	//! Return from getFunction method.
	alias CompiledDg = ir.Constant delegate(ir.Constant[]);


public:
	//! Free resources.
	fn close();
	//! Returns a delegate that runs the given function from the module that
	//! this object was compiled from.
	fn getFunction(ir.Function) CompiledDg;
}

//! Home to logic for tying Frontend, Pass and Backend together and
//! abstracts away several IO related functions. Such as looking up module
//! files and printing error messages.
class Driver
{
public:
	execDir: string;
	identStr: string;
	internalDebug: bool;


public:
	this() { }
	//! Free resources.
	fn close();
	//! Load a module source from file system.
	fn loadModule(name: ir.QualifiedName) ir.Module;
	//! Load a filename from the string import paths.
	fn stringImport(loc: const(Location), filename: string) string;
	//! Get the modules given on the command line.
	fn getCommandLineModules() ir.Module[];
	//! Returns a delegate that runs the given function from the given module.
	fn hostCompile(ir.Module) BackendHostResult;
}

//! Centre point for all language passes.
class LanguagePass
{
public:
	alias DoneDg = void delegate();


public:
	errSink: ErrorSink;
	driver: Driver;
	ver: VersionSet;
	target: TargetInfo;
	settings: Settings;
	frontend: Frontend;
	postParse: PostParsePass;
	warningsEnabled: bool;
	//! Cached lookup items.
	objObject: ir.Class;
	//! Cached lookup items.
	vaStartFunc: ir.Function;
	//! Cached lookup items.
	vaEndFunc: ir.Function;
	//! Cached lookup items.
	tiTypeInfo: ir.Class;
	//! Cached lookup items.
	tiClassInfo: ir.Class;
	//! Cached lookup items.
	tiInterfaceInfo: ir.Class;
	//! Cached lookup items.
	exceptThrowable: ir.Class;
	//! Cached lookup items.
	gcAllocDgVariable: ir.Variable;
	//! Cached lookup items.
	aaNew: ir.Function;
	//! Cached lookup items.
	aaDup: ir.Function;
	//! Cached lookup items.
	aaRehash: ir.Function;
	//! Cached lookup items.
	aaGetKeys: ir.Function;
	//! Cached lookup items.
	aaGetValues: ir.Function;
	//! Cached lookup items.
	aaGetLength: ir.Function;
	//! Cached lookup items.
	aaInsertPrimitive: ir.Function;
	//! Cached lookup items.
	aaInsertArray: ir.Function;
	//! Cached lookup items.
	aaInsertPtr: ir.Function;
	//! Cached lookup items.
	aaDeletePrimitive: ir.Function;
	//! Cached lookup items.
	aaDeleteArray: ir.Function;
	//! Cached lookup items.
	aaDeletePtr: ir.Function;
	//! Cached lookup items.
	aaInPrimitive: ir.Function;
	//! Cached lookup items.
	aaInArray: ir.Function;
	//! Cached lookup items.
	aaInPtr: ir.Function;
	//! Cached lookup items.
	aaInBinopPrimitive: ir.Function;
	//! Cached lookup items.
	aaInBinopArray: ir.Function;
	//! Cached lookup items.
	aaInBinopPtr: ir.Function;
	//! Cached lookup items.
	hashFunc: ir.Function;
	//! Cached lookup items.
	castFunc: ir.Function;
	//! Cached lookup items.
	memcmpFunc: ir.Function;
	//! Cached lookup items.
	utfDecode_u8_d: ir.Function;
	//! Cached lookup items.
	utfReverseDecode_u8_d: ir.Function;
	//! Cached lookup items.
	ehThrowFunc: ir.Function;
	//! Cached lookup items.
	ehRethrowFunc: ir.Function;
	//! Cached lookup items.
	ehThrowSliceErrorFunc: ir.Function;
	//! Cached lookup items.
	ehPersonalityFunc: ir.Function;
	//! Cached lookup items.
	ehThrowAssertErrorFunc: ir.Function;
	//! Cached lookup items.
	ehThrowKeyNotFoundErrorFunc: ir.Function;
	//! Cached lookup items.
	runMainFunc: ir.Function;
	//! Cached lookup items.
	llvmTypeidFor: ir.Function;
	//! Cached lookup items.
	llvmMemmove32: ir.Function;
	//! Cached lookup items.
	llvmMemmove64: ir.Function;
	//! Cached lookup items.
	llvmMemcpy32: ir.Function;
	//! Cached lookup items.
	llvmMemcpy64: ir.Function;
	//! Cached lookup items.
	llvmMemset32: ir.Function;
	//! Cached lookup items.
	llvmMemset64: ir.Function;
	//! Cached lookup items.
	sinkType: ir.Type;
	//! Cached lookup items.
	sinkStore: ir.Type;
	//! Cached lookup items.
	sinkInit: ir.Function;
	//! Cached lookup items.
	sinkGetStr: ir.Function;
	//! Cached lookup items.
	formatHex: ir.Function;
	//! Cached lookup items.
	formatI64: ir.Function;
	//! Cached lookup items.
	formatU64: ir.Function;
	//! Cached lookup items.
	formatF32: ir.Function;
	//! Cached lookup items.
	formatF64: ir.Function;
	//! Cached lookup items.
	formatDchar: ir.Function;
	//! Type id constants for TypeInfo.
	TYPE_STRUCT: i32;
	//! Type id constants for TypeInfo.
	TYPE_CLASS: i32;
	//! Type id constants for TypeInfo.
	TYPE_INTERFACE: i32;
	//! Type id constants for TypeInfo.
	TYPE_UNION: i32;
	//! Type id constants for TypeInfo.
	TYPE_ENUM: i32;
	//! Type id constants for TypeInfo.
	TYPE_ATTRIBUTE: i32;
	//! Type id constants for TypeInfo.
	TYPE_ANNOTATION: i32;
	//! Type id constants for TypeInfo.
	TYPE_VOID: i32;
	//! Type id constants for TypeInfo.
	TYPE_UBYTE: i32;
	//! Type id constants for TypeInfo.
	TYPE_BYTE: i32;
	//! Type id constants for TypeInfo.
	TYPE_CHAR: i32;
	//! Type id constants for TypeInfo.
	TYPE_BOOL: i32;
	//! Type id constants for TypeInfo.
	TYPE_USHORT: i32;
	//! Type id constants for TypeInfo.
	TYPE_SHORT: i32;
	//! Type id constants for TypeInfo.
	TYPE_WCHAR: i32;
	//! Type id constants for TypeInfo.
	TYPE_UINT: i32;
	//! Type id constants for TypeInfo.
	TYPE_INT: i32;
	//! Type id constants for TypeInfo.
	TYPE_DCHAR: i32;
	//! Type id constants for TypeInfo.
	TYPE_FLOAT: i32;
	//! Type id constants for TypeInfo.
	TYPE_ULONG: i32;
	//! Type id constants for TypeInfo.
	TYPE_LONG: i32;
	//! Type id constants for TypeInfo.
	TYPE_DOUBLE: i32;
	//! Type id constants for TypeInfo.
	TYPE_REAL: i32;
	//! Type id constants for TypeInfo.
	TYPE_POINTER: i32;
	//! Type id constants for TypeInfo.
	TYPE_ARRAY: i32;
	//! Type id constants for TypeInfo.
	TYPE_STATIC_ARRAY: i32;
	//! Type id constants for TypeInfo.
	TYPE_AA: i32;
	//! Type id constants for TypeInfo.
	TYPE_FUNCTION: i32;
	//! Type id constants for TypeInfo.
	TYPE_DELEGATE: i32;


public:
	this(errSink: ErrorSink, drv: Driver, ver: VersionSet, target: TargetInfo, frontend: Frontend) { }
	//! Free resources.
	fn close();
	//! Used by the Driver to store classes it loads from arguments.
	fn addModule(mod: ir.Module);
	//! Returns a already loaded module or loads it from file.
	fn getModule(name: ir.QualifiedName) ir.Module;
	//! Retuns all currently loaded modules.
	fn getModules() ir.Module[];
	//! These functions are used to assure that no circular dependancies
	//! happens when resolving nodes like: Class, Function, Variables, etc.
	fn startResolving(n: ir.Node) DoneDg;
	//! These functions are used to assure that no circular dependancies
	//! happens when resolving nodes like: Class, Function, Variables, etc.
	fn startActualizing(n: ir.Node) DoneDg;
	//! Resolves an Attribute, for UserAttribute usages.
	fn resolve(current: ir.Scope, a: ir.Attribute);
	//! Resolve a set of user attributes.
	fn resolve(current: ir.Scope, userAttrs: ir.Attribute[]);
	//! Resolves an ExpReference, forwarding the decl appropriately.
	fn resolve(current: ir.Scope, eref: ir.ExpReference);
	//! Resolves an EnumDeclaration setting its value.
	fn resolve(current: ir.Scope, ed: ir.EnumDeclaration);
	//! Resolves a TemplateInstance, causing it to be instantiated.
	fn resolve(current: ir.Scope, ti: ir.TemplateInstance);
	//! Resolves an ir.Store that is of kind Merge. Afterwards the kind is
	//! changed to kind Function, since only functions can be merged.
	fn resolve(ir.Store);
	//! Resolves a Function making it usable externaly,
	fn resolve(current: ir.Scope, func: ir.Function) { }
	//! Resolves a Variable making it usable externaly.
	fn resolve(current: ir.Scope, v: ir.Variable) { }
	//! Resolves a unresolved alias store, the store can change type to Type,
	//! either the field myAlias or type is set.
	fn resolve(a: ir.Alias) { }
	//! Resolves an Enum making it usable externaly, done on lookup of it.
	fn resolveNamed(e: ir.Enum) { }
	//! Resolves a Struct, done on lookup of it.
	fn resolveNamed(s: ir.Struct) { }
	//! Resolves a Union, done on lookup of it.
	fn resolveNamed(u: ir.Union) { }
	//! Resolves a Class, making sure the parent class is populated.
	fn resolveNamed(c: ir.Class) { }
	//! Resolves an Interface.
	fn resolveNamed(i: ir._Interface) { }
	//! Actualize a Struct, making sure all its fields and methods are
	//! populated, and any embedded structs (not referenced via pointers) are
	//! actualized as well. In short makes sure that the struct size is fully
	//! known.
	fn actualize(s: ir.Struct) { }
	//! Actualize a Union, making sure all its fields and methods are
	//! populated, and any embedded structs (not referenced via pointers) are
	//! resolved as well.
	fn actualize(u: ir.Union) { }
	//! Actualize an Interface.
	fn actualize(i: ir._Interface) { }
	//! Actualize a Class, making sure all its fields and methods are
	//! populated, Any embedded structs (not referenced via pointers) are
	//! resolved as well. Parent classes are resolved to.
	fn actualize(c: ir.Class) { }
	//! Run all post parse passes on the given modules.
	fn phase1(m: ir.Module[]);
	//! Run all semantic passes on the given modules.
	fn phase2(m: ir.Module[]);
	//! Run all lowering passes on the given modules.
	fn phase3(m: ir.Module[]);


protected:
	fn doResolve(current: ir.Scope, v: ir.Variable);
	fn doResolve(current: ir.Scope, func: ir.Function);
	fn doResolve(a: ir.Alias);
	fn doResolve(e: ir.Enum);
	fn doResolve(i: ir._Interface);
	fn doResolve(c: ir.Class);
	fn doResolve(u: ir.Union);
	fn doResolve(c: ir.Struct);
	fn doActualize(i: ir._Interface);
	fn doActualize(s: ir.Struct);
	fn doActualize(u: ir.Union);
	fn doActualize(c: ir.Class);
}
class Driver

Home to logic for tying Frontend, Pass and Backend together and abstracts away several IO related functions. Such as looking up module files and printing error messages.

fn close()

Free resources.

fn loadModule(name: ir.QualifiedName) ir.Module

Load a module source from file system.

fn stringImport(loc: const(Location), filename: string) string

Load a filename from the string import paths.

fn getCommandLineModules() ir.Module[]

Get the modules given on the command line.

fn hostCompile(ir.Module) BackendHostResult

Returns a delegate that runs the given function from the given module.

May be called multiple times with the same arguments, or the same Module but with a different function from the given Module.

The driver should do caching of the module and function. Once a module has been given to it or any children of it may not be changed, doing so will cause undefined behaviour.

class LanguagePass

Centre point for all language passes.

objObject: ir.Class

Cached lookup items.

vaStartFunc: ir.Function

Cached lookup items.

vaEndFunc: ir.Function

Cached lookup items.

tiTypeInfo: ir.Class

Cached lookup items.

tiClassInfo: ir.Class

Cached lookup items.

tiInterfaceInfo: ir.Class

Cached lookup items.

exceptThrowable: ir.Class

Cached lookup items.

gcAllocDgVariable: ir.Variable

Cached lookup items.

aaNew: ir.Function

Cached lookup items.

aaDup: ir.Function

Cached lookup items.

aaRehash: ir.Function

Cached lookup items.

aaGetKeys: ir.Function

Cached lookup items.

aaGetValues: ir.Function

Cached lookup items.

aaGetLength: ir.Function

Cached lookup items.

aaInsertPrimitive: ir.Function

Cached lookup items.

aaInsertArray: ir.Function

Cached lookup items.

aaInsertPtr: ir.Function

Cached lookup items.

aaDeletePrimitive: ir.Function

Cached lookup items.

aaDeleteArray: ir.Function

Cached lookup items.

aaDeletePtr: ir.Function

Cached lookup items.

aaInPrimitive: ir.Function

Cached lookup items.

aaInArray: ir.Function

Cached lookup items.

aaInPtr: ir.Function

Cached lookup items.

aaInBinopPrimitive: ir.Function

Cached lookup items.

aaInBinopArray: ir.Function

Cached lookup items.

aaInBinopPtr: ir.Function

Cached lookup items.

hashFunc: ir.Function

Cached lookup items.

castFunc: ir.Function

Cached lookup items.

memcmpFunc: ir.Function

Cached lookup items.

utfDecode_u8_d: ir.Function

Cached lookup items.

utfReverseDecode_u8_d: ir.Function

Cached lookup items.

ehThrowFunc: ir.Function

Cached lookup items.

ehRethrowFunc: ir.Function

Cached lookup items.

ehThrowSliceErrorFunc: ir.Function

Cached lookup items.

ehPersonalityFunc: ir.Function

Cached lookup items.

ehThrowAssertErrorFunc: ir.Function

Cached lookup items.

ehThrowKeyNotFoundErrorFunc: ir.Function

Cached lookup items.

runMainFunc: ir.Function

Cached lookup items.

llvmTypeidFor: ir.Function

Cached lookup items.

llvmMemmove32: ir.Function

Cached lookup items.

llvmMemmove64: ir.Function

Cached lookup items.

llvmMemcpy32: ir.Function

Cached lookup items.

llvmMemcpy64: ir.Function

Cached lookup items.

llvmMemset32: ir.Function

Cached lookup items.

llvmMemset64: ir.Function

Cached lookup items.

sinkType: ir.Type

Cached lookup items.

sinkStore: ir.Type

Cached lookup items.

sinkInit: ir.Function

Cached lookup items.

sinkGetStr: ir.Function

Cached lookup items.

formatHex: ir.Function

Cached lookup items.

formatI64: ir.Function

Cached lookup items.

formatU64: ir.Function

Cached lookup items.

formatF32: ir.Function

Cached lookup items.

formatF64: ir.Function

Cached lookup items.

formatDchar: ir.Function

Cached lookup items.

TYPE_STRUCT: i32

Type id constants for TypeInfo.

TYPE_CLASS: i32

Type id constants for TypeInfo.

TYPE_INTERFACE: i32

Type id constants for TypeInfo.

TYPE_UNION: i32

Type id constants for TypeInfo.

TYPE_ENUM: i32

Type id constants for TypeInfo.

TYPE_ATTRIBUTE: i32

Type id constants for TypeInfo.

TYPE_ANNOTATION: i32

Type id constants for TypeInfo.

TYPE_VOID: i32

Type id constants for TypeInfo.

TYPE_UBYTE: i32

Type id constants for TypeInfo.

TYPE_BYTE: i32

Type id constants for TypeInfo.

TYPE_CHAR: i32

Type id constants for TypeInfo.

TYPE_BOOL: i32

Type id constants for TypeInfo.

TYPE_USHORT: i32

Type id constants for TypeInfo.

TYPE_SHORT: i32

Type id constants for TypeInfo.

TYPE_WCHAR: i32

Type id constants for TypeInfo.

TYPE_UINT: i32

Type id constants for TypeInfo.

TYPE_INT: i32

Type id constants for TypeInfo.

TYPE_DCHAR: i32

Type id constants for TypeInfo.

TYPE_FLOAT: i32

Type id constants for TypeInfo.

TYPE_ULONG: i32

Type id constants for TypeInfo.

TYPE_LONG: i32

Type id constants for TypeInfo.

TYPE_DOUBLE: i32

Type id constants for TypeInfo.

TYPE_REAL: i32

Type id constants for TypeInfo.

TYPE_POINTER: i32

Type id constants for TypeInfo.

TYPE_ARRAY: i32

Type id constants for TypeInfo.

TYPE_STATIC_ARRAY: i32

Type id constants for TypeInfo.

TYPE_AA: i32

Type id constants for TypeInfo.

TYPE_FUNCTION: i32

Type id constants for TypeInfo.

TYPE_DELEGATE: i32

Type id constants for TypeInfo.

fn close()

Free resources.

fn addModule(mod: ir.Module)

Used by the Driver to store classes it loads from arguments.

The controller does not need to call addModule when it has loaded a module via its loadModule function.

fn getModule(name: ir.QualifiedName) ir.Module

Returns a already loaded module or loads it from file.

The expected behavior of the langauge pass is to call the Driver to load the module.

fn getModules() ir.Module[]

Retuns all currently loaded modules.

fn startResolving(n: ir.Node) DoneDg

These functions are used to assure that no circular dependancies happens when resolving nodes like: Class, Function, Variables, etc.

fn startActualizing(n: ir.Node) DoneDg

These functions are used to assure that no circular dependancies happens when resolving nodes like: Class, Function, Variables, etc.

fn resolve(current: ir.Scope, a: ir.Attribute)

Resolves an Attribute, for UserAttribute usages.

fn resolve(current: ir.Scope, userAttrs: ir.Attribute[])

Resolve a set of user attributes.

fn resolve(current: ir.Scope, eref: ir.ExpReference)

Resolves an ExpReference, forwarding the decl appropriately.

fn resolve(current: ir.Scope, ed: ir.EnumDeclaration)

Resolves an EnumDeclaration setting its value.

Throws

  • CompilerError on failure to resolve the enum value.

fn resolve(current: ir.Scope, ti: ir.TemplateInstance)

Resolves a TemplateInstance, causing it to be instantiated.

fn resolve(ir.Store)

Resolves an ir.Store that is of kind Merge. Afterwards the kind is changed to kind Function, since only functions can be merged.

fn resolve(current: ir.Scope, func: ir.Function)

Resolves a Function making it usable externaly,

Throws

  • CompilerError on failure to resolve function.

fn resolve(current: ir.Scope, v: ir.Variable)

Resolves a Variable making it usable externaly.

Throws

  • CompilerError on failure to resolve variable.

fn resolve(a: ir.Alias)

Resolves a unresolved alias store, the store can change type to Type, either the field myAlias or type is set.

Throws

  • CompilerError on failure to resolve alias.

fn resolveNamed(e: ir.Enum)

Resolves an Enum making it usable externaly, done on lookup of it.

Throws

  • CompilerError on failure to resolve the enum.

fn resolveNamed(s: ir.Struct)

Resolves a Struct, done on lookup of it.

fn resolveNamed(u: ir.Union)

Resolves a Union, done on lookup of it.

fn resolveNamed(c: ir.Class)

Resolves a Class, making sure the parent class is populated.

fn resolveNamed(i: ir._Interface)

Resolves an Interface.

fn actualize(s: ir.Struct)

Actualize a Struct, making sure all its fields and methods are populated, and any embedded structs (not referenced via pointers) are actualized as well. In short makes sure that the struct size is fully known.

fn actualize(u: ir.Union)

Actualize a Union, making sure all its fields and methods are populated, and any embedded structs (not referenced via pointers) are resolved as well.

fn actualize(i: ir._Interface)

Actualize an Interface.

fn actualize(c: ir.Class)

Actualize a Class, making sure all its fields and methods are populated, Any embedded structs (not referenced via pointers) are resolved as well. Parent classes are resolved to.

Any lowering structs and internal variables are also generated by this function.

fn phase1(m: ir.Module[])

Run all post parse passes on the given modules.

Parameters

m

The modules.

fn phase2(m: ir.Module[])

Run all semantic passes on the given modules.

Parameters

m

The modules.

fn phase3(m: ir.Module[])

Run all lowering passes on the given modules.

Parameters

m

The modules.

enum TargetType

Used to determine the output of the backend.

interface Backend

Interface implemented by backends. Often the last stage of the compile pipe that is implemented in this compiler, optimization and linking are often done outside of the compiler, either invoked directly by us or a build system.

See also

fn close()

Free resources.

fn supported() TargetType[]

Return the supported target types.

fn compileFile(m: ir.Module, type: TargetType, ehPersonality: ir.Function, llvmTypeidFor: ir.Function, execDir: string, currentWorkingDir: string, identStr: string) BackendFileResult

Compile the given module to either a file or host result.

See the corresponding fields on LanguagePass and Driver for what the non-Module arguments mean.

fn compileHost(m: ir.Module, ehPersonality: ir.Function, llvmTypeidFor: ir.Function, execDir: string, currentWorkingDir: string, identStr: string) BackendHostResult

Compile the given module to either a file or host result.

See the corresponding fields on LanguagePass and Driver for what the non-Module arguments mean.

interface BackendFileResult

A result from a backend compilation that can be saved onto disk.

fn close()

Free resources.

fn saveToFile(filename: string)

Save the result to disk.

interface BackendHostResult

A JIT compiled a module that you can fetch functions from.

fn close()

Free resources.

alias CompiledDg

Return from getFunction method.

fn getFunction(ir.Function) CompiledDg

Returns a delegate that runs the given function from the module that this object was compiled from.

The function must have been inside of the module. May be called multiple times with the same function, or different function.

This object should do caching of the function. Once a module has been given to the driver any children of it may not be changed, doing so will cause undefined behaviour.