compiler

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: May 8, 2018 License: Apache-2.0 Imports: 18 Imported by: 13

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.

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 (
	ContextLocation = "location"
	ContextGlobals  = "globals"
	ContextArena    = "arena"
)

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 (
	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.

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

	// API is the api that is being compiled.
	API *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
	// 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) 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, Location, 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) 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) 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) 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) 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, dstPtr *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) 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.

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 ErrorCode

type ErrorCode uint32

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

func (ErrorCode) Err

func (c ErrorCode) Err() error

type Location

type Location struct {
	File   string
	Line   int
	Column int
}

Location defines a single .api source location.

func (Location) String

func (l Location) String() string

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
	Contains codegen.Function // bool(ctx*, M*, K)
	Index    codegen.Function //   V*(ctx*, M*, K, addIfNotFound)
	Lookup   codegen.Function //   V(ctx*, M*, K)
	Remove   codegen.Function // void(ctx*, M*, K)
	Clear    codegen.Function // void(ctx*, M*)
}

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

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

	// 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

	// Locations is the array of source locations.
	// Only generated if Settings.CodeLocations is true.
	Locations []Location

	// Module is the generated code module.
	Module *codegen.Module

	// Initializer is a function that initializes the globals with defaults.
	// The function has the signature: void (ctx* ctx)
	Initializer codegen.Function
}

Program is the output of a compilation.

func Compile

func Compile(api *semantic.API, mappings *resolver.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
	// contains filtered or unexported fields
}

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

type Settings

type Settings struct {
	// TargetABI is the ABI used by the device running the compiled code.
	TargetABI *device.ABI

	// StorageABI is the ABI of the device used to generate data in serialized
	// buffers.
	StorageABI *device.ABI

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

	// Prefix for mangler
	Namespaces []string

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

	// CodeLocations is true if the compiler should emit writes to the context's
	// location field with the currently executing statement.
	CodeLocations 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 StructInfo

type StructInfo struct {
	Type *codegen.Struct
}

StructInfo holds the generated structure for a given structure type.

type Types

type Types struct {
	codegen.Types
	Ctx        codegen.Type                        // 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*
	Uint8Ptr   codegen.Type                        // uint8_t*
	VoidPtr    codegen.Type                        // void* (aliased of uint8_t*)
	Globals    *codegen.Struct                     // API global variables structure.
	GlobalsPtr codegen.Type                        // Pointer to Globals.
	CmdParams  map[*semantic.Function]codegen.Type // struct holding all command parameters and return value.
	Maps       map[*semantic.Map]*MapInfo
	// 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) Storage

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

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

func (*Types) StorageABIAlignment

func (t *Types) StorageABIAlignment(ty semantic.Type) int32

StorageABIAlignment returns the alignment of this type in bytes when stored.

func (*Types) StorageAllocaSize

func (t *Types) StorageAllocaSize(ty semantic.Type) int32

StorageAllocaSize returns the number of bytes per object if you were to store two next to each other in memory.

func (*Types) StorageSize

func (t *Types) StorageSize(ty semantic.Type) int32

StorageSize returns the number of bytes needed to store this type.

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.
Package testexterns implements some extern functions used for compiler tests.
Package testexterns implements some extern functions used for compiler tests.

Jump to

Keyboard shortcuts

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