compiler

package
v0.0.0-...-15b4445 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 6, 2022 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package compiler implements the core gapil language compiler.

The compiler will generate types and command execution functions using LLVM for a resolved API. The compiler can be extended with Plugins for additional functionality.

Package compiler implements the core gapil language compiler.

The compiler will generate types and command execution functions using LLVM for a resolved API. The compiler can be extended with Plugins for additional functionality.

Index

Constants

View Source
const (
	// ErrSuccess is the error code for a success.
	ErrSuccess = ErrorCode(C.GAPIL_ERR_SUCCESS)

	// ErrAborted is the error code for a command that called abort().
	ErrAborted = ErrorCode(C.GAPIL_ERR_ABORTED)
)
View Source
const (
	ContextGlobals    = "globals"
	ContextArena      = "arena"
	ContextThread     = "thread"
	ContextCmdID      = "cmd_id"
	ContextCmdArgs    = "cmd_args"
	ContextNextPoolID = "next_pool_id"
)

Field names for the context_t runtime type.

View Source
const (
	SlicePool  = "pool"
	SliceRoot  = "root"
	SliceBase  = "base"
	SliceSize  = "size"
	SliceCount = "count"
)

Field names for the slice_t runtime type.

View Source
const (
	RTTIKind      = "kind"
	RTTIAPIIndex  = "api_index"
	RTTITypeIndex = "type_index"
	RTTITypeName  = "type_name"
	RTTIReference = "reference"
	RTTIRelease   = "release"
)

Field names for the gapil_rtti_t runtime type.

View Source
const (
	AnyRefCount = "ref_count"
	AnyArena    = "arena"
	AnyRTTI     = "rtti"
	AnyValue    = "value"
)

Field names for the gapil_any_t runtime type.

View Source
const (
	MsgArgName  = "name"
	MsgArgValue = "value"
)

Field names for the gapil_msg_arg_t runtime type.

View Source
const (
	MsgRefCount   = "ref_count"
	MsgArena      = "arena"
	MsgIdentifier = "identifier"
	MsgArgs       = "args"
)

Field names for the gapil_msg_t runtime type.

View Source
const (
	PoolRefCount = "ref_count"
	PoolID       = "id"
	PoolSize     = "size"
	PoolBuffer   = "buffer"
)

Field names for the pool_t runtime type.

View Source
const (
	MapRefCount = "ref_count"
	MapArena    = "arena"
	MapCount    = "count"
	MapCapacity = "capacity"
	MapElements = "elements"
)

Field names for the map_t runtime type.

View Source
const (
	StringRefCount = "ref_count"
	StringArena    = "arena"
	StringLength   = "length"
	StringData     = "data"
)

Field names for the string_t runtime type.

View Source
const (
	RefRefCount = "ref_count"
	RefArena    = "arena"
	RefValue    = "value"
)

Field names for the ref_t runtime type.

View Source
const (
	BufArena = "arena"
	BufData  = "data"
	BufCap   = "capacity"
	BufSize  = "size"
	BufAlign = "alignment"
)

Field names for the buffer_t runtime type.

View Source
const (
	Read  = C.GAPIL_READ
	Write = C.GAPIL_WRITE
)

gapil_data_access enumerator values.

View Source
const (
	KindBool      = uint32(C.GAPIL_KIND_BOOL)
	KindU8        = uint32(C.GAPIL_KIND_U8)
	KindS8        = uint32(C.GAPIL_KIND_S8)
	KindU16       = uint32(C.GAPIL_KIND_U16)
	KindS16       = uint32(C.GAPIL_KIND_S16)
	KindF32       = uint32(C.GAPIL_KIND_F32)
	KindU32       = uint32(C.GAPIL_KIND_U32)
	KindS32       = uint32(C.GAPIL_KIND_S32)
	KindF64       = uint32(C.GAPIL_KIND_F64)
	KindU64       = uint32(C.GAPIL_KIND_U64)
	KindS64       = uint32(C.GAPIL_KIND_S64)
	KindInt       = uint32(C.GAPIL_KIND_INT)
	KindUint      = uint32(C.GAPIL_KIND_UINT)
	KindSize      = uint32(C.GAPIL_KIND_SIZE)
	KindChar      = uint32(C.GAPIL_KIND_CHAR)
	KindArray     = uint32(C.GAPIL_KIND_ARRAY)
	KindClass     = uint32(C.GAPIL_KIND_CLASS)
	KindEnum      = uint32(C.GAPIL_KIND_ENUM)
	KindMap       = uint32(C.GAPIL_KIND_MAP)
	KindPointer   = uint32(C.GAPIL_KIND_POINTER)
	KindReference = uint32(C.GAPIL_KIND_REFERENCE)
	KindSlice     = uint32(C.GAPIL_KIND_SLICE)
	KindString    = uint32(C.GAPIL_KIND_STRING)
)

gapil_kind enumerator values.

Variables

This section is empty.

Functions

This section is empty.

Types

type C

type C struct {
	// T holds the compiler types.
	T Types

	// M is the codegen module for the program.
	M *codegen.Module

	// APIs are the apis that are being compiled.
	APIs []*semantic.API

	// Mangler is the symbol mangler in use.
	Mangler mangling.Mangler

	// Root is the namespace in which generated symbols should be placed.
	// This excludes gapil runtime symbols which have the prefix 'gapil_'.
	Root mangling.Scope

	// Settings are the configuration values used for this compile.
	Settings Settings
	// contains filtered or unexported fields
}

C is the compiler context used to build the program.

func (*C) Alloc

func (c *C) Alloc(s *S, count *codegen.Value, ty codegen.Type) *codegen.Value

Alloc calls gapil_alloc to allocate a buffer big enough to hold count elements of type ty.

func (*C) AppendBuffer

func (c *C) AppendBuffer(s *S, bufPtr, size, bytes *codegen.Value)

AppendBuffer appends bytes to the buffer. size must be of type uint32. bytes must be of type void* (alias of uint8*).

func (*C) AppendBufferData

func (c *C) AppendBufferData(s *S, bufPtr, ptr *codegen.Value)

AppendBufferData appends a data to the buffer from the given pointer.

func (*C) Build

func (c *C) Build(f *codegen.Function, do func(*S))

Build implements the function f by creating a new scope and calling do to emit the function body. If the function has a parameter of type context_t* then the Ctx, Globals and Arena scope fields are automatically assigned.

func (*C) CopySlice

func (c *C) CopySlice(s *S, dst, src *codegen.Value)

CopySlice copies the contents of slice src to dst.

func (*C) CopySliceByPtr

func (c *C) CopySliceByPtr(s *S, dst, src *codegen.Value)

CopySliceByPtr copies the contents of slice at *src to the slice at *dst.

func (*C) CurrentAPI

func (c *C) CurrentAPI() *semantic.API

CurrentAPI returns the API that is currently being built.

func (*C) CurrentExpression

func (c *C) CurrentExpression() semantic.Expression

CurrentExpression returns the expression that is currently being built.

func (*C) CurrentStatement

func (c *C) CurrentStatement() semantic.Statement

CurrentStatement returns the statement that is currently being built.

func (*C) Delegate

func (c *C) Delegate(from, to *codegen.Function)

Delegate builds the function from with a simple body that calls to, with implicit casts for each of the parameters. If the function to returns a value, this is cast to the from return type and returned. Delegate can be used to produce stub functions that have equivalent signatures when lowered to LLVM types.

func (*C) ExpressionStack

func (c *C) ExpressionStack() []semantic.Expression

ExpressionStack returns the current build stack of expressions.

func (*C) Fail

func (c *C) Fail(msg string, args ...interface{})

Fail is used to immediately terminate compilation due to an internal compiler error.

func (*C) Free

func (c *C) Free(s *S, ptr *codegen.Value)

Free calls gapil_free to release a buffer that was previously allocated with Alloc or Realloc.

func (*C) InitBuffer

func (c *C) InitBuffer(s *S, bufPtr, initialCapacity *codegen.Value)

InitBuffer initializes a buffer with the initial capacity. initialCapacity must be of type uint32.

func (*C) IterateMap

func (c *C) IterateMap(s *S, mapPtr *codegen.Value, idxTy semantic.Type, cb func(i, k, v *codegen.Value))

IterateMap emits a map iteration calling cb for each element in the map where: i is a pointer to the sequential index of the element starting from 0 of type

idxTy.

k is a pointer to the element key. v is a pointer to the element value.

func (*C) LoadParameters

func (c *C) LoadParameters(s *S, f *semantic.Function)

LoadParameters loads the command's parameters from the context's arguments field and stores them into s.Parameters.

func (*C) Log

func (c *C) Log(s *S, severity log.Severity, msg string, args ...interface{})

Log emits a call to gapil_logf with the given printf-style arguments. args can be a mix of codegen.Values or simple data types (which are automatically passed to codegen.Builder.Scalar).

func (*C) LogF

func (c *C) LogF(s *S, msg string, args ...interface{})

LogF is short hand for Log(s, log.Fatal, msg, args...)

func (*C) LogI

func (c *C) LogI(s *S, msg string, args ...interface{})

LogI is short hand for Log(s, log.Info, msg, args...)

func (*C) MakeSlice

func (c *C) MakeSlice(s *S, size, count *codegen.Value) *codegen.Value

MakeSlice creates a new slice of the given size in bytes.

func (*C) MakeSliceAt

func (c *C) MakeSliceAt(s *S, size, count, slice *codegen.Value)

MakeSliceAt creates a new slice of the given size in bytes at the given slice pointer.

func (*C) MakeString

func (c *C) MakeString(s *S, length, data *codegen.Value) *codegen.Value

MakeString creates a new string from the specified data and length in bytes.

func (*C) Mangle

func (c *C) Mangle(ty codegen.Type) mangling.Type

Mangle returns the mangling type for the given codegen type.

func (*C) Method

func (c *C) Method(
	isConst bool,
	owner codegen.Type,
	retTy codegen.Type,
	name string,
	params ...codegen.Type) *codegen.Function

Method declares a function as a member of owner using the compiler's mangler.

func (*C) Parameter

func (c *C) Parameter(s *S, e *semantic.Parameter) *codegen.Value

Parameter returns the loaded parameter value, failing if the parameter cannot be found.

func (*C) Realloc

func (c *C) Realloc(s *S, old, count *codegen.Value) *codegen.Value

Realloc calls gapil_realloc to reallocate a buffer that was previously allocated with Alloc or Realloc.

func (*C) SliceDataForRead

func (c *C) SliceDataForRead(s *S, slice *codegen.Value, elType codegen.Type) *codegen.Value

SliceDataForRead returns a pointer to an array of slice elements. This pointer should be used to read (not write) from the slice. The pointer is only valid until the slice is touched again.

func (*C) SliceDataForWrite

func (c *C) SliceDataForWrite(s *S, slice *codegen.Value, elType codegen.Type) *codegen.Value

SliceDataForWrite returns a pointer to an array of slice elements. This pointer should be used to write (not read) to the slice. The pointer is only valid until the slice is touched again.

func (*C) SourceLocation

func (c *C) SourceLocation() SourceLocation

SourceLocation returns the SoureLocation for the currently built expression, statement or function.

func (*C) SourceLocationFor

func (c *C) SourceLocationFor(n semantic.Node) SourceLocation

SourceLocationFor returns a string describing the source location of the given semantic node.

func (*C) SourceLocationForCST

func (c *C) SourceLocationForCST(n cst.Node) SourceLocation

SourceLocationForCST returns a string describing the source location of the given CST node.

func (*C) StatementStack

func (c *C) StatementStack() []semantic.Statement

StatementStack returns the current build stack of statements.

func (*C) StorageTypes

func (c *C) StorageTypes(layout *device.MemoryLayout, prefix string) *StorageTypes

StorageTypes returns a StorageTypes built for the given ABI.

func (*C) TermBuffer

func (c *C) TermBuffer(s *S, bufPtr *codegen.Value)

TermBuffer frees a buffer's data without freeing the buffer itself.

type CommandInfo

type CommandInfo struct {
	// The execute function for the given command.
	// The function has the signature: void (ctx*, Params*)
	Execute *codegen.Function

	// The Params structure that is passed to Execute.
	Parameters *codegen.Struct
}

CommandInfo holds the generated execute function for a given command.

type ContextDataPlugin

type ContextDataPlugin interface {
	// OnPreBuildContext is called just before the context structure is built.
	// This can be used to build any additional types that are returned by
	// ContextData().
	OnPreBuildContext(*C)

	// ContextData returns a slice of additional ContextFields that will be
	// augmented to the context structure.
	ContextData(*C) []ContextField
}

ContextDataPlugin is the interface implemented by plugins that require additional data to be stored in the runtime context.

type ContextField

type ContextField struct {
	Name string                              // Name of the field
	Type codegen.Type                        // Type of the field
	Init func(s *S, fieldPtr *codegen.Value) // Optional initializer
	Term func(s *S, fieldPtr *codegen.Value) // Optional terminator
}

ContextField represents a single additional context field added by a ContextDataPlugin.

type ErrorCode

type ErrorCode uint32

ErrorCode is an error code returned (possibly in a <value, errcode> pair) by a command or subroutine.

type FunctionExposerPlugin

type FunctionExposerPlugin interface {
	Functions() map[string]*codegen.Function
}

FunctionExposerPlugin is the interface implemented by plugins that build public functions. These functions will be exposed on the output Program.

type MapInfo

type MapInfo struct {
	Type     *codegen.Struct // Maps are held as pointers to these structs
	Elements *codegen.Struct
	Key      codegen.Type
	Val      codegen.Type
	Element  codegen.Type
	MapMethods
}

MapInfo holds the generated map info for a given semantic map type.

type MapMethods

type MapMethods struct {
	Contains  *codegen.Function // bool(M*, ctx*, K)
	Index     *codegen.Function //   V*(M*, ctx*, K, addIfNotFound)
	Remove    *codegen.Function // void(M*, ctx*, K)
	Clear     *codegen.Function // void(M*, ctx*)
	ClearKeep *codegen.Function // void(M*, ctx*)
}

MapMethods are the functions that operate on a map.

type OnBeginCommandListener

type OnBeginCommandListener interface {
	OnBeginCommand(s *S, cmd *semantic.Function)
}

OnBeginCommandListener is the interface implemented by plugins that generate custom logic at the start of the command.

type OnEndCommandListener

type OnEndCommandListener interface {
	OnEndCommand(s *S, cmd *semantic.Function)
}

OnEndCommandListener is the interface implemented by plugins that generate custom logic at the end of the command.

type OnFenceListener

type OnFenceListener interface {
	OnFence(s *S)
}

OnFenceListener is the interface implemented by plugins that generate custom logic at the fence of the command.

type OnReadListener

type OnReadListener interface {
	OnRead(s *S, slice *codegen.Value, ty *semantic.Slice)
}

OnReadListener is the interface implemented by plugins that generate custom logic when slices are read.

type OnWriteListener

type OnWriteListener interface {
	OnWrite(s *S, slice *codegen.Value, ty *semantic.Slice)
}

OnWriteListener is the interface implemented by plugins that generate custom logic when slices are written.

type Plugin

type Plugin interface {
	Build(*C)
}

Plugin is a extension for the compiler.

type Program

type Program struct {
	// Settings used to compile the program.
	Settings Settings

	// APIs compiled for this program.
	APIs []*semantic.API

	// Commands is a map of command name to CommandInfo.
	Commands map[string]*CommandInfo

	// Structs is a map of struct name to StructInfo.
	Structs map[string]*StructInfo

	// Maps is a map of map name to MapInfo.
	Maps map[string]*MapInfo

	// Globals is the StructInfo of all the globals.
	Globals *StructInfo

	// Functions is a map of function name to plugin implemented functions.
	Functions map[string]*codegen.Function

	// Codegen is the codegen module.
	Codegen *codegen.Module

	// Module is the global that holds the generated gapil_module structure.
	Module codegen.Global
}

Program is the output of a compilation.

func Compile

func Compile(apis []*semantic.API, mappings *semantic.Mappings, s Settings) (*Program, error)

Compile compiles the given API semantic tree to a program using the given settings.

func (*Program) Dump

func (p *Program) Dump() string

Dump returns the full LLVM IR for the program.

func (*Program) IR

func (p *Program) IR() map[string]string

IR returns a map of function to IR.

type S

type S struct {
	// The scope can emit any instructions into the current scope block.
	*codegen.Builder

	// Ctx is a pointer to the active context (context_t*).
	Ctx *codegen.Value

	// Location is a pointer to the current source code location (uint32_t*).
	Location *codegen.Value

	// Globals is a pointer to the current API's global state variables.
	Globals *codegen.Value

	// Arena is a pointer to the current memory arena (arena*).
	Arena *codegen.Value

	// Parameters is the current function's parameters.
	Parameters map[*semantic.Parameter]*codegen.Value

	// The identifier of the currently executing thread.
	CurrentThread *codegen.Value
	// contains filtered or unexported fields
}

S is a nestable compiler scope. A scope holds parameters and local variables.

func (*S) ForN

func (s *S) ForN(n *codegen.Value, cb func(s *S, iterator *codegen.Value) (cont *codegen.Value))

ForN overrides codegen.Builder.ForN to ensure all the scopes are popped after cb reaches its last instruction.

func (*S) If

func (s *S) If(cond *codegen.Value, onTrue func(s *S))

If overrides codegen.Builder.If to ensure all the scopes are popped after onTrue reaches its last instruction.

func (*S) IfElse

func (s *S) IfElse(cond *codegen.Value, onTrue, onFalse func(s *S))

IfElse overrides codegen.Builder.IfElse to ensure all the scopes are popped after onTrue and onFalse reach their last instruction.

func (*S) Return

func (s *S) Return(val *codegen.Value)

Return overrides codegen.Builder.Return to ensure all the scopes are popped before emitting the terminating instruction.

func (*S) Switch

func (s *S) Switch(cases []SwitchCase, defaultCase func(s *S))

Switch overrides codegen.Builder.Switch to ensure all the scopes are popped after each condition and block reach their last instruction.

type Settings

type Settings struct {
	// The name of the module to produce.
	// This will be a global of the gapil_module type.
	Module string

	// TargetABI is the ABI used by the device running the compiled code.
	TargetABI *device.ABI

	// CaptureABI is the ABI of the device used to generate the capture data.
	CaptureABI *device.ABI

	// The mangler used for global functions and types.
	Mangler mangling.Mangler

	// Prefix for mangler
	Namespaces []string

	// EmitDebug is true if the compiler should emit DWARF debug info.
	EmitDebug bool

	// EmitContext is true if the compiler should generate context creation and
	// destruction functions.
	EmitContext bool

	// EmitExec is true if the compiler should generate execution functions for
	// each API command. Implies EmitContext.
	EmitExec bool

	// WriteToApplicationPool is true if writes to the application pool should
	// be performed.
	WriteToApplicationPool bool

	// Plugins are the list of extensions to include in the compilation.
	Plugins []Plugin
}

Settings describe the options used to compile an API file.

type SourceLocation

type SourceLocation struct {
	Node   semantic.Node
	File   string
	Line   int
	Column int
}

SourceLocation associates a semantic node with its location in a source file.

func (SourceLocation) IsValid

func (l SourceLocation) IsValid() bool

IsValid returns true if the source location is valid.

func (SourceLocation) String

func (l SourceLocation) String() string

type StorageTypes

type StorageTypes struct {
	// contains filtered or unexported fields
}

StorageTypes are types that can be persisted in buffers, with possibly a different ABI to the target.

func (*StorageTypes) AlignOf

func (s *StorageTypes) AlignOf(ty semantic.Type) uint64

AlignOf returns the alignment of this type in bytes.

func (*StorageTypes) Get

func (s *StorageTypes) Get(ty semantic.Type) codegen.Type

Get returns the codegen type used to store ty in a buffer.

func (*StorageTypes) SizeOf

func (s *StorageTypes) SizeOf(ty semantic.Type) uint64

SizeOf returns size of the type.

func (*StorageTypes) StrideOf

func (s *StorageTypes) StrideOf(ty semantic.Type) uint64

StrideOf returns the number of bytes per element when held in an array.

type StructInfo

type StructInfo struct {
	Type *codegen.Struct
}

StructInfo holds the generated structure for a given structure type.

type SwitchCase

type SwitchCase struct {
	Conditions func(*S) []*codegen.Value
	Block      func(*S)
}

SwitchCase is a single condition and block used as a case statement in a switch.

type Types

type Types struct {
	codegen.Types
	Ctx        *codegen.Struct                        // context_t
	CtxPtr     codegen.Type                           // context_t*
	Pool       codegen.Type                           // pool_t
	PoolPtr    codegen.Type                           // pool_t*
	Sli        codegen.Type                           // slice_t
	Str        *codegen.Struct                        // string_t
	StrPtr     codegen.Type                           // string_t*
	Arena      *codegen.Struct                        // arena_t
	ArenaPtr   codegen.Type                           // arena_t*
	Any        *codegen.Struct                        // gapil_any_t
	AnyPtr     codegen.Type                           // gapil_any_t*
	Msg        *codegen.Struct                        // gapil_msg_t
	MsgPtr     codegen.Type                           // gapil_msg_t*
	MsgArg     *codegen.Struct                        // gapil_msg_arg_t
	MsgArgPtr  codegen.Type                           // gapil_msg_arg_t*
	RTTI       *codegen.Struct                        // gapil_rtti
	Uint8Ptr   codegen.Type                           // uint8_t*
	VoidPtr    codegen.Type                           // void* (aliased of uint8_t*)
	VoidPtrPtr codegen.Type                           // void** (aliased of uint8_t**)
	Globals    *codegen.Struct                        // API global variables structure.
	GlobalsPtr codegen.Type                           // Pointer to Globals.
	Buf        codegen.Type                           // buffer_t
	BufPtr     codegen.Type                           // buffer_t*
	CmdParams  map[*semantic.Function]*codegen.Struct // struct holding all command parameters and return value.
	DataAccess codegen.Type
	Maps       map[*semantic.Map]*MapInfo

	CaptureTypes *StorageTypes
	// contains filtered or unexported fields
}

Types holds all the codegen Types for semantic types and the runtime. Types augments the codegen.Types structure.

func (*Types) AlignOf

func (t *Types) AlignOf(layout *device.MemoryLayout, ty semantic.Type) uint64

AlignOf returns the alignment of this type in bytes for the given memory layout.

func (*Types) Capture

func (t *Types) Capture(ty semantic.Type) codegen.Type

Capture returns the codegen type used to represent ty when stored in a capture's buffer.

func (*Types) SizeOf

func (t *Types) SizeOf(layout *device.MemoryLayout, ty semantic.Type) uint64

SizeOf returns size of the type for the given memory layout.

func (*Types) StrideOf

func (t *Types) StrideOf(layout *device.MemoryLayout, ty semantic.Type) uint64

StrideOf returns the number of bytes per element when held in an array for the given memory layout.

func (*Types) Target

func (t *Types) Target(ty semantic.Type) codegen.Type

Target returns the codegen type used to represent ty in the target-preferred form.

Directories

Path Synopsis
Package mangling exposes a minimal collection of types used to build mangled strings using any of the sub-packages.
Package mangling exposes a minimal collection of types used to build mangled strings using any of the sub-packages.
c
Package c implements a basic symbol mangling for that is compatible with C.
Package c implements a basic symbol mangling for that is compatible with C.
ia64
Package ia64 implements a subset of the symbol mangling for the itanium ABI (standard for GCC).
Package ia64 implements a subset of the symbol mangling for the itanium ABI (standard for GCC).
plugins
cloner
Package cloner is a plugin for the gapil compiler to generate deep clone functions for reference types, maps and commands.
Package cloner is a plugin for the gapil compiler to generate deep clone functions for reference types, maps and commands.
encoder
Package encoder is a plugin for the gapil compiler to generate command and state encode functions.
Package encoder is a plugin for the gapil compiler to generate command and state encode functions.
encoder/test/encoder_pb
Package encoder_pb describes the serialization format for the serialzation test api.
Package encoder_pb describes the serialization format for the serialzation test api.
replay
Package replay is a plugin for the gapil compiler to generate replay streams for commands.
Package replay is a plugin for the gapil compiler to generate replay streams for commands.
Package testutils implements reusable helpers for writing compiler tests.
Package testutils implements reusable helpers for writing compiler tests.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL