codegen

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: 8 Imported by: 13

Documentation

Overview

Package codegen provides an interface for code generation using LLVM.

codegen provides a higher-level and simpler interface for generating AOT or JIT code than using the LLVM go bindings directly.

Index

Constants

This section is empty.

Variables

View Source
var Variadic variadicTy

Variadic is a type that can be used as the last parameter of a function definition to indicate a variadic function.

Functions

func IsBool

func IsBool(ty Type) bool

IsBool returns true if ty is the boolean type.

func IsFloat

func IsFloat(ty Type) bool

IsFloat returns true if ty is a float type.

func IsInteger

func IsInteger(ty Type) bool

IsInteger returns true if ty is an integer type.

func IsPointer

func IsPointer(ty Type) bool

IsPointer returns true if ty is a pointer type.

func IsSignedInteger

func IsSignedInteger(ty Type) bool

IsSignedInteger returns true if ty is a signed integer type.

func IsStruct

func IsStruct(ty Type) bool

IsStruct returns true if ty is a struct type.

func IsUnsignedInteger

func IsUnsignedInteger(ty Type) bool

IsUnsignedInteger returns true if ty is an unsigned integer type.

func IsVector

func IsVector(ty Type) bool

IsVector returns true if ty is a vector type.

Types

type Array

type Array struct {
	Element Type // The type of the element the pointer points to.
	Size    int  // Number of elements in the array.
	// contains filtered or unexported fields
}

Array represents a static array type.

func (*Array) String

func (t *Array) String() string

func (*Array) TypeName

func (t *Array) TypeName() string

type Builder

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

Builder is provided to the callback of Build() for building a function's body.

func (*Builder) Add

func (b *Builder) Add(x, y *Value) *Value

Add returns x + y. The types of the two values must be equal.

func (*Builder) AddS

func (b *Builder) AddS(x *Value, y interface{}) *Value

AddS returns x + y. The types of the two values must be equal.

func (*Builder) AlignOf

func (b *Builder) AlignOf(ty Type) *Value

AlignOf returns the alignment of the type in bytes.

func (*Builder) And

func (b *Builder) And(x, y *Value) *Value

And performs a bitwise-and of the two integers. The types of the two values must be equal.

func (*Builder) Call

func (b *Builder) Call(f Function, args ...*Value) *Value

Call invokes the function f with the specified arguments

func (*Builder) Div

func (b *Builder) Div(x, y *Value) *Value

Div returns x / y. The types of the two values must be equal.

func (*Builder) DivS

func (b *Builder) DivS(x *Value, y interface{}) *Value

DivS returns x / y. The types of the two values must be equal.

func (*Builder) Equal

func (b *Builder) Equal(x, y *Value) *Value

Equal returns x == y. The types of the two values must be equal.

func (*Builder) ForN

func (b *Builder) ForN(n *Value, cb func(iterator *Value) (cont *Value))

ForN builds a logic block with the following psuedocode:

for it := 0; it < n; it++ {
  cont := cb()
  if cont == false { break; }
}

If cb returns nil then the loop will never exit early.

func (*Builder) GlobalString

func (b *Builder) GlobalString(s string) *Value

GlobalString returns a pointer to a global variable holidng the string data.

func (*Builder) GreaterOrEqualTo

func (b *Builder) GreaterOrEqualTo(x, y *Value) *Value

GreaterOrEqualTo returns x >= y. The types of the two values must be equal.

func (*Builder) GreaterThan

func (b *Builder) GreaterThan(x, y *Value) *Value

GreaterThan returns x > y. The types of the two values must be equal.

func (*Builder) If

func (b *Builder) If(cond *Value, onTrue func())

If builds an if statement.

func (*Builder) IfElse

func (b *Builder) IfElse(cond *Value, onTrue, onFalse func())

IfElse builds an if-else statement.

func (*Builder) Invert

func (b *Builder) Invert(x *Value) *Value

Invert returns ~x.

func (*Builder) IsBlockTerminated

func (b *Builder) IsBlockTerminated() bool

IsBlockTerminated returns true if the last instruction is a terminator (unconditional jump). It is illegal to write another instruction after a terminator.

func (*Builder) LessOrEqualTo

func (b *Builder) LessOrEqualTo(x, y *Value) *Value

LessOrEqualTo returns x <= y. The types of the two values must be equal.

func (*Builder) LessThan

func (b *Builder) LessThan(x, y *Value) *Value

LessThan returns x < y. The types of the two values must be equal.

func (*Builder) Local

func (b *Builder) Local(name string, ty Type) *Value

Local returns a pointer to a new local variable with the specified name and type.

func (*Builder) LocalInit

func (b *Builder) LocalInit(name string, val *Value) *Value

LocalInit returns a new local variable with the specified name and initial value.

func (*Builder) Memcpy

func (b *Builder) Memcpy(dst, src, size *Value)

Memcpy performs an intrinsic memory copy of size bytes from src to dst. dst and src must be a void pointer (alias of u8*). size is the copy size in bytes. size must be of type uint32. align is the minimum pointer alignment of dst and src. align must of type uint32, or nil to represent no guaranteed alignment.

func (*Builder) Mul

func (b *Builder) Mul(x, y *Value) *Value

Mul returns x * y. The types of the two values must be equal.

func (*Builder) MulS

func (b *Builder) MulS(x *Value, y interface{}) *Value

MulS returns x * y. The types of the two values must be equal.

func (*Builder) Negate

func (b *Builder) Negate(x *Value) *Value

Negate returns -x. The type of x must be a signed integer or float.

func (*Builder) Not

func (b *Builder) Not(x *Value) *Value

Not returns !x. The type of x must be Bool.

func (*Builder) NotEqual

func (b *Builder) NotEqual(x, y *Value) *Value

NotEqual returns x != y. The types of the two values must be equal.

func (*Builder) One

func (b *Builder) One(ty Type) *Value

One returns a one-value of the specified bool, int or float type.

func (*Builder) Or

func (b *Builder) Or(x, y *Value) *Value

Or performs a bitwise-or of the two integers. The types of the two values must be equal.

func (*Builder) Parameter

func (b *Builder) Parameter(i int) *Value

Parameter returns i'th function parameter

func (*Builder) Rem

func (b *Builder) Rem(x, y *Value) *Value

Rem returns x % y. The types of the two values must be equal.

func (*Builder) Return

func (b *Builder) Return(val *Value)

Return returns execution of the function with the given value

func (*Builder) Scalar

func (b *Builder) Scalar(v interface{}) *Value

Scalar returns a constant scalar with the value v.

func (*Builder) Select

func (b *Builder) Select(cond, x, y *Value) *Value

Select returns (cond ? x : y). x and y must be of the same type.

func (*Builder) ShiftLeft

func (b *Builder) ShiftLeft(x, y *Value) *Value

ShiftLeft performs a bit-shift left by shift bits.

func (*Builder) ShiftRight

func (b *Builder) ShiftRight(x, y *Value) *Value

ShiftRight performs a bit-shift right by shift bits.

func (*Builder) Shuffle

func (b *Builder) Shuffle(x, y, indices *Value) *Value

Shuffle performs a vector-shuffle operation of the two vector values x and y with the specified indices.

func (*Builder) SizeOf

func (b *Builder) SizeOf(ty Type) *Value

SizeOf returns the size of the type in bytes as a uint64. If ty is void, a value of 1 is returned.

func (*Builder) Sub

func (b *Builder) Sub(x, y *Value) *Value

Sub returns x - y. The types of the two values must be equal.

func (*Builder) SubS

func (b *Builder) SubS(x *Value, y interface{}) *Value

SubS returns x - y. The types of the two values must be equal.

func (*Builder) Switch

func (b *Builder) Switch(cases []SwitchCase, defaultCase func())

Switch builds a switch statement.

func (*Builder) Undef

func (b *Builder) Undef(ty Type) *Value

Undef returns a new undefined value of the specified type.

func (*Builder) Vector

func (b *Builder) Vector(el0 interface{}, els ...interface{}) *Value

Vector returns a constant vector with the specified values.

func (*Builder) VectorN

func (b *Builder) VectorN(n int, v interface{}) *Value

VectorN returns a constant vector of n elements with the same value v in each element.

func (*Builder) While

func (b *Builder) While(test func() *Value, loop func())

While builds a logic block with the following psuedocode:

while test() {
  loop()
}

func (*Builder) Xor

func (b *Builder) Xor(x, y *Value) *Value

Xor performs a bitwise-xor of the two integers. The types of the two values must be equal.

func (*Builder) Zero

func (b *Builder) Zero(ty Type) *Value

Zero returns a zero-value of the specified type.

type Const

type Const struct {
	Type Type
	// contains filtered or unexported fields
}

Const is an immutable value.

func (Const) Cast

func (v Const) Cast(ty Type) Const

Cast casts the constant v to the type ty.

func (Const) Value

func (c Const) Value(b *Builder) *Value

Value returns a Value for the constant.

type Executor

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

Executor executes module functions.

func (*Executor) AlignOf

func (e *Executor) AlignOf(t Type) int

AlignOf returns the preferred stack/global alignment for the specified type.

func (*Executor) FieldOffsets

func (e *Executor) FieldOffsets(s *Struct) []int

func (*Executor) FunctionAddress

func (e *Executor) FunctionAddress(f Function) unsafe.Pointer

func (*Executor) SizeOf

func (e *Executor) SizeOf(t Type) int

SizeOf returns the offset in bytes between successive objects of the specified type, including alignment padding.

func (*Executor) StructLayout

func (e *Executor) StructLayout(s *Struct) string

type Field

type Field struct {
	Name string
	Type Type
}

Field is a single field in a struct.

type Float

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

Float represents a floating-point type.

func (Float) String

func (t Float) String() string

func (Float) TypeName

func (t Float) TypeName() string

type Function

type Function struct {
	Name string
	Type FunctionType
	// contains filtered or unexported fields
}

Function represents a callable function.

func (Function) Build

func (f Function) Build(cb func(*Builder)) (err error)

Build calls cb with a Builder that can construct the function body.

func (Function) Inline

func (f Function) Inline() Function

Inline makes this function prefer inlining

func (Function) LinkOnceODR

func (f Function) LinkOnceODR() Function

LinkOnceODR sets this function's linkage to "linkonce_odr". This lets the function be merged with other global symbols with the same name, with the assumption their implementation is identical. Unlike "linkonce" this also preserves inlining.

func (Function) LinkPrivate

func (f Function) LinkPrivate() Function

LinkPrivate makes this function use private linkage.

func (Function) String

func (f Function) String() string

type FunctionType

type FunctionType struct {
	Signature Signature
	// contains filtered or unexported fields
}

FunctionType is the type of a function

func (FunctionType) String

func (t FunctionType) String() string

func (FunctionType) TypeName

func (t FunctionType) TypeName() string

type Global

type Global struct {
	Type Type
	// contains filtered or unexported fields
}

Global is a global value.

func (Global) LinkPrivate

func (g Global) LinkPrivate() Global

LinkPrivate makes this global use private linkage.

func (Global) Value

func (g Global) Value(b *Builder) *Value

Value returns a Value for the global.

type IndexOrName

type IndexOrName interface{}

IndexOrName can be an index (int) or field name (string).

type Integer

type Integer struct {
	Signed bool // Is this integer type signed?
	// contains filtered or unexported fields
}

Integer represents an integer type.

func (Integer) String

func (t Integer) String() string

func (Integer) TypeName

func (t Integer) TypeName() string

type Module

type Module struct {
	Types Types
	// contains filtered or unexported fields
}

Module is a JIT module.

func NewModule

func NewModule(name string, target *device.ABI) *Module

NewModule returns a new module with the specified name.

func (*Module) ConstStruct

func (m *Module) ConstStruct(ty *Struct, fields map[string]interface{}) Const

ConstStruct returns a constant struct with the value v.

func (*Module) Executor

func (m *Module) Executor(optimize bool) (*Executor, error)

Executor constructs an executor.

func (*Module) Extern

func (m *Module) Extern(name string, ty Type) Global

Extern returns a global variable declared externally with the given name and type.

func (*Module) Function

func (m *Module) Function(resTy Type, name string, paramTys ...Type) Function

Function creates a new function with the given name, result type and parameters.

func (*Module) Global

func (m *Module) Global(name string, val Const) Global

Global returns a new global variable intiailized with the specified constant value.

func (*Module) Object

func (m *Module) Object(optimize bool) ([]byte, error)

Object compiles the module down to an object file.

func (*Module) Optimize

func (m *Module) Optimize()

Optimize optimizes the module.

func (*Module) ParseFunctionSignature

func (m *Module) ParseFunctionSignature(sig string) Function

ParseFunctionSignature returns a function parsed from a C-style signature. Example:

"void* Foo(uint8_t i, bool b)"

func (*Module) Scalar

func (m *Module) Scalar(v interface{}) Const

Scalar returns a constant scalar with the value v.

func (*Module) String

func (m *Module) String() string

func (*Module) Verify

func (m *Module) Verify() error

Verify checks correctness of the module.

func (*Module) ZeroGlobal

func (m *Module) ZeroGlobal(name string, ty Type) Global

ZeroGlobal returns a zero-initialized new global variable with the specified name and type.

type Pointer

type Pointer struct {
	Element Type // The type of the element the pointer points to.
	// contains filtered or unexported fields
}

Pointer represents a pointer type.

func (Pointer) String

func (t Pointer) String() string

func (Pointer) TypeName

func (t Pointer) TypeName() string

type Signature

type Signature struct {
	Parameters TypeList
	Result     Type
	Variadic   bool
}

Signature holds signature information about a function.

type Struct

type Struct struct {
	Name   string
	Fields []Field
	// contains filtered or unexported fields
}

Struct is the type of a structure.

func (*Struct) Field

func (t *Struct) Field(name string) Field

func (*Struct) SetBody

func (t *Struct) SetBody(packed bool, fields ...Field) *Struct

SetBody sets the fields of the declared struct.

func (*Struct) String

func (t *Struct) String() string

func (*Struct) TypeName

func (t *Struct) TypeName() string

type SwitchCase

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

type Type

type Type interface {
	String() string
	TypeName() string
	// contains filtered or unexported methods
}

Type represents a JIT type.

func Scalar

func Scalar(ty Type) Type

Scalar returns the element type if ty is a vector, otherwise it returns ty.

type TypeList

type TypeList []Type

TypeList is a slice of types.

func (TypeList) String

func (l TypeList) String() string

type Types

type Types struct {
	Void    Type // Void is the void type.
	Bool    Type // Bool is a one-bit integer type.
	Int     Type // Int is a signed 32-bit or 64-bit integer type.
	Int8    Type // Int8 is a signed 8-bit integer type.
	Int16   Type // Int16 is a signed 16-bit integer type.
	Int32   Type // Int32 is a signed 32-bit integer type.
	Int64   Type // Int64 is a signed 64-bit integer type.
	Uint    Type // Uint is an unsigned 32-bit or 64-bit integer type.
	Uint8   Type // Uint8 is an unsigned 8-bit integer type.
	Uint16  Type // Uint16 is an unigned 16-bit integer type.
	Uint32  Type // Uint32 is an unsigned 32-bit integer type.
	Uint64  Type // Uint64 is an unsigned 64-bit integer type.
	Uintptr Type // Uinptr is an unsigned integer type of the same width as a pointer.
	Size    Type // Size is an unsigned integer of native bit-width.
	Float32 Type // Float32 is a 32-bit floating-point number type.
	Float64 Type // Float64 is a 64-bit floating-point number type.
	// contains filtered or unexported fields
}

Types contains all the types for a module.

func (*Types) Array

func (t *Types) Array(el Type, n int) *Array

Array returns an n-element array type of el.

func (*Types) DeclarePackedStruct

func (t *Types) DeclarePackedStruct(name string) *Struct

DeclarePackedStruct creates a new, packed empty struct type.

func (*Types) DeclareStruct

func (t *Types) DeclareStruct(name string) *Struct

DeclareStruct creates a new, empty struct type.

func (*Types) PackedStruct

func (t *Types) PackedStruct(name string, fields ...Field) *Struct

PackedStruct creates a new packed struct type.

func (*Types) Pointer

func (t *Types) Pointer(el Type) Pointer

Pointer returns a pointer type of el.

func (*Types) Struct

func (t *Types) Struct(name string, fields ...Field) *Struct

Struct creates a new unpacked struct type.

func (*Types) TypeOf

func (t *Types) TypeOf(v interface{}) Type

TypeOf returns the corresponding JIT type for the type of value v. TypeOf may also accept a reflect.Type.

func (*Types) Vector

func (t *Types) Vector(el Type, count int) Vector

Vector returns a pointer type of el.

type Value

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

Value represents a value.

func (*Value) Bitcast

func (v *Value) Bitcast(ty Type) *Value

Bitcast performs a bitwise cast of the value v to the type ty.

func (*Value) Cast

func (v *Value) Cast(ty Type) *Value

Cast casts the value v to the type ty.

func (*Value) Extract

func (v *Value) Extract(at IndexOrName) *Value

Extract returns the field at extracted from the struct or array v.

func (*Value) Index

func (v *Value) Index(path ...ValueIndexOrName) *Value

Index returns a new pointer to the array or field element found by following the list of indices as specified by path.

func (*Value) Insert

func (v *Value) Insert(at ValueIndexOrName, val *Value) *Value

Insert creates a copy of the struct or array v with the field/element at changed to val.

func (*Value) IsNull

func (v *Value) IsNull() *Value

IsNull returns true if the pointer value v is null.

func (*Value) Load

func (v *Value) Load() *Value

Load loads the element from the pointer value.

func (*Value) LoadUnaligned

func (v *Value) LoadUnaligned() *Value

LoadUnaligned loads the element from the pointer value, it allows the loaded value to be unaligned

func (*Value) Name

func (v *Value) Name() string

Name returns the value's name.

func (*Value) SetName

func (v *Value) SetName(name string) *Value

SetName assigns a name to the value.

func (*Value) SetNameIfUnset

func (v *Value) SetNameIfUnset(name string) *Value

SetNameIfUnset assigns a name to the value if it isn't already set.

func (*Value) Store

func (v *Value) Store(val *Value)

Store stores val to the pointer ptr.

func (*Value) StoreUnaligned

func (v *Value) StoreUnaligned(val *Value)

StoreUnaligned stores val to the pointer ptr. It assumes that the destination address may be unaligned

func (*Value) Type

func (v *Value) Type() Type

Type returns the type of the value.

type ValueIndexOrName

type ValueIndexOrName interface{}

ValueIndexOrName can be an index (Value or int) or field name (string).

type Vector

type Vector struct {
	Element Type // The type of the vector elements.
	Count   int  // Number of elements in a vector.
	// contains filtered or unexported fields
}

Vector represents a vector type.

func (Vector) String

func (t Vector) String() string

func (Vector) TypeName

func (t Vector) TypeName() string

Directories

Path Synopsis
Package call provides methods for invoking C function pointers.
Package call provides methods for invoking C function pointers.

Jump to

Keyboard shortcuts

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