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 ¶
- Variables
- func DataLayout(abi *device.ABI) string
- func IsBool(ty Type) bool
- func IsEnum(ty Type) bool
- func IsFloat(ty Type) bool
- func IsFunction(ty Type) bool
- func IsInteger(ty Type) bool
- func IsIntegerOrEnum(ty Type) bool
- func IsPointer(ty Type) bool
- func IsSignedInteger(ty Type) bool
- func IsSignedIntegerOrEnum(ty Type) bool
- func IsStruct(ty Type) bool
- func IsUnsignedInteger(ty Type) bool
- func IsVector(ty Type) bool
- func StrideInBits(ty Type) int
- type Alias
- type Array
- type Builder
- func (b *Builder) Add(x, y *Value) *Value
- func (b *Builder) AddS(x *Value, y interface{}) *Value
- func (b *Builder) AlignOf(ty Type) *Value
- func (b *Builder) And(x, y *Value) *Value
- func (b *Builder) Call(f *Function, args ...*Value) *Value
- func (b *Builder) CallIndirect(f *Value, args ...*Value) *Value
- func (b *Builder) Div(x, y *Value) *Value
- func (b *Builder) DivS(x *Value, y interface{}) *Value
- func (b *Builder) Equal(x, y *Value) *Value
- func (b *Builder) ForN(n *Value, cb func(iterator *Value) (cont *Value))
- func (b *Builder) FuncAddr(f *Function) *Value
- func (b *Builder) GreaterOrEqualTo(x, y *Value) *Value
- func (b *Builder) GreaterThan(x, y *Value) *Value
- func (b *Builder) If(cond *Value, onTrue func())
- func (b *Builder) IfElse(cond *Value, onTrue, onFalse func())
- func (b *Builder) Invert(x *Value) *Value
- func (b *Builder) Invoke(f *Function, cleanup func(), args ...*Value) *Value
- func (b *Builder) IsBlockTerminated() bool
- func (b *Builder) LessOrEqualTo(x, y *Value) *Value
- func (b *Builder) LessThan(x, y *Value) *Value
- func (b *Builder) Local(name string, ty Type) *Value
- func (b *Builder) LocalInit(name string, val *Value) *Value
- func (b *Builder) Memcpy(dst, src, size *Value)
- func (b *Builder) Memset(dst, val, size *Value)
- func (b *Builder) Memzero(dst, size *Value)
- func (b *Builder) Mul(x, y *Value) *Value
- func (b *Builder) MulS(x *Value, y interface{}) *Value
- func (b *Builder) Negate(x *Value) *Value
- func (b *Builder) Not(x *Value) *Value
- func (b *Builder) NotEqual(x, y *Value) *Value
- func (b *Builder) One(ty Type) *Value
- func (b *Builder) Or(x, y *Value) *Value
- func (b *Builder) Parameter(i int) *Value
- func (b *Builder) PrintfSpecifier(v *Value) (string, []*Value)
- func (b *Builder) Rem(x, y *Value) *Value
- func (b *Builder) Return(val *Value)
- func (b *Builder) Scalar(v interface{}) *Value
- func (b *Builder) Select(cond, x, y *Value) *Value
- func (b *Builder) SetLocation(line, column int)
- func (b *Builder) ShiftLeft(x, y *Value) *Value
- func (b *Builder) ShiftRight(x, y *Value) *Value
- func (b *Builder) Shuffle(x, y, indices *Value) *Value
- func (b *Builder) SizeOf(ty Type) *Value
- func (b *Builder) StructOf(name string, v []*Value) *Value
- func (b *Builder) Sub(x, y *Value) *Value
- func (b *Builder) SubS(x *Value, y interface{}) *Value
- func (b *Builder) Switch(cases []SwitchCase, defaultCase func())
- func (b *Builder) Throw(v *Value)
- func (b *Builder) Undef(ty Type) *Value
- func (b *Builder) Vector(el0 interface{}, els ...interface{}) *Value
- func (b *Builder) VectorN(n int, v interface{}) *Value
- func (b *Builder) While(test func() *Value, loop func())
- func (b *Builder) Xor(x, y *Value) *Value
- func (b *Builder) Zero(ty Type) *Value
- type Const
- type Enum
- type Executor
- type Field
- type Float
- type Function
- func (f *Function) Build(cb func(*Builder)) (err error)
- func (f *Function) Inline() *Function
- func (f *Function) LinkInternal() *Function
- func (f *Function) LinkOnceODR() *Function
- func (f *Function) LinkPrivate() *Function
- func (f *Function) SetLocation(path string, line int) *Function
- func (f *Function) SetParameterNames(names ...string) *Function
- func (f Function) String() string
- type FunctionType
- type Global
- type IndexOrName
- type Integer
- type Module
- func (m *Module) AlignOf(ty Type) Const
- func (m *Module) Array(v interface{}, elTy Type) Const
- func (m *Module) ConstStruct(ty *Struct, fields map[string]interface{}) Const
- func (m *Module) Ctor(priority int32, cb func(*Builder))
- func (m *Module) EmitDebug()
- func (m *Module) Executor(optimize bool) (*Executor, error)
- func (m *Module) Extern(name string, ty Type) Global
- func (m *Module) Function(resTy Type, name string, paramTys ...Type) *Function
- func (m *Module) Global(name string, val Const) Global
- func (m *Module) Object(optimize bool) ([]byte, error)
- func (m *Module) OffsetOf(ty *Struct, name string) Const
- func (m *Module) Optimize()
- func (m *Module) ParseFunctionSignature(sig string) *Function
- func (m *Module) Scalar(v interface{}) Const
- func (m *Module) ScalarOfType(v interface{}, ty Type) Const
- func (m *Module) SizeOf(ty Type) Const
- func (m *Module) String() string
- func (m *Module) Verify() error
- func (m *Module) Zero(ty Type) Const
- func (m *Module) ZeroGlobal(name string, ty Type) Global
- type Pointer
- type Signature
- type Struct
- func (t *Struct) AlignInBits() int
- func (t *Struct) Field(name string) Field
- func (t *Struct) FieldIndex(name string) int
- func (t *Struct) FieldOffsetInBits(idx int) int
- func (t *Struct) Fields() []Field
- func (t *Struct) SetBody(packed bool, fields ...Field) *Struct
- func (t *Struct) SizeInBits() int
- func (t *Struct) String() string
- func (t *Struct) TypeName() string
- type SwitchCase
- type Triple
- type Type
- type TypeList
- type Types
- func (t *Types) Alias(name string, to Type) Alias
- func (t *Types) Array(el Type, n int) *Array
- func (t *Types) DeclarePackedStruct(name string) *Struct
- func (t *Types) DeclareStruct(name string) *Struct
- func (t *Types) Enum(name string) Enum
- func (t *Types) FieldsOf(v interface{}) []Field
- func (t *Types) Function(resTy Type, paramTys ...Type) *FunctionType
- func (t *Types) PackedStruct(name string, fields ...Field) *Struct
- func (t *Types) Pointer(el Type) Pointer
- func (t *Types) Struct(name string, fields ...Field) *Struct
- func (t *Types) TypeOf(v interface{}) Type
- func (t *Types) Vector(el Type, count int) Vector
- type Value
- func (v *Value) Bitcast(ty Type) *Value
- func (v *Value) Cast(ty Type) *Value
- func (v *Value) EmitDebug(name string) *Value
- func (v *Value) Extract(at IndexOrName) *Value
- func (v *Value) Index(path ...ValueIndexOrName) *Value
- func (v *Value) Insert(at ValueIndexOrName, val *Value) *Value
- func (v *Value) IsNull() *Value
- func (v *Value) Load() *Value
- func (v *Value) LoadUnaligned() *Value
- func (v *Value) Name() string
- func (v *Value) SetName(name string) *Value
- func (v *Value) Store(val *Value)
- func (v *Value) StoreUnaligned(val *Value)
- func (v *Value) Type() Type
- type ValueIndexOrName
- type Vector
Constants ¶
This section is empty.
Variables ¶
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 DataLayout ¶
DataLayout returns an LLVM data layout string for the given ABI, or an empty string if there is no known data layout for the given ABI. Reference: https://llvm.org/docs/LangRef.html#langref-datalayout
func IsIntegerOrEnum ¶
IsIntegerOrEnum returns true if ty is an integer or enum type.
func IsSignedInteger ¶
IsSignedInteger returns true if ty is a signed integer type.
func IsSignedIntegerOrEnum ¶
IsSignedIntegerOrEnum returns true if ty is a signed integer or enum type.
func IsUnsignedInteger ¶
IsUnsignedInteger returns true if ty is an unsigned integer type.
func StrideInBits ¶
StrideInBits returns the number of bits per element when held in an array.
Types ¶
type Alias ¶
type Alias struct {
// contains filtered or unexported fields
}
Alias is a named type that aliases another type.
func (Alias) AlignInBits ¶
func (Alias) SizeInBits ¶
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) AlignInBits ¶
func (Array) SizeInBits ¶
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) And ¶
And performs a bitwise-and of the two integers. The types of the two values must be equal.
func (*Builder) CallIndirect ¶
CallIndirect invokes the function pointer f with the specified arguments
func (*Builder) ForN ¶
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) GreaterOrEqualTo ¶
GreaterOrEqualTo returns x >= y. The types of the two values must be equal.
func (*Builder) GreaterThan ¶
GreaterThan returns x > y. The types of the two values must be equal.
func (*Builder) Invoke ¶
Invoke invokes the function f with the specified arguments. If an exception is thrown while calling f, then cleanup will be called before rethrowing the exception.
func (*Builder) IsBlockTerminated ¶
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 ¶
LessOrEqualTo returns x <= y. The types of the two values must be equal.
func (*Builder) Local ¶
Local returns a pointer to a new local variable with the specified name and type.
func (*Builder) LocalInit ¶
LocalInit returns a new local variable with the specified name and initial value.
func (*Builder) Memcpy ¶
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.
func (*Builder) Memset ¶
Memset performs an intrinsic memory set to the given value. dst must be a void pointer (alias of u8*). val is value that is to be repeated size times into dst. val must be of type uint8. size is the copy size in bytes. size must be of type uint32.
func (*Builder) Memzero ¶
Memzero performs an intrinsic memory clear to 0. dst must be a void pointer (alias of u8*). size is the copy size in bytes. size must be of type uint32.
func (*Builder) Or ¶
Or performs a bitwise-or of the two integers. The types of the two values must be equal.
func (*Builder) PrintfSpecifier ¶
PrintfSpecifier returns the string and values that can be used to print v with printf.
func (*Builder) SetLocation ¶
SetLocation sets the source location of the next instructions to be built.
func (*Builder) ShiftRight ¶
ShiftRight performs a bit-shift right by shift bits.
func (*Builder) Shuffle ¶
Shuffle performs a vector-shuffle operation of the two vector values x and y with the specified indices.
func (*Builder) SizeOf ¶
SizeOf returns the size of the type in bytes as a uint64. If ty is void, a value of 1 is returned.
func (*Builder) Switch ¶
func (b *Builder) Switch(cases []SwitchCase, defaultCase func())
Switch builds a switch statement.
func (*Builder) VectorN ¶
VectorN returns a constant vector of n elements with the same value v in each element.
func (*Builder) While ¶
While builds a logic block with the following psuedocode:
while test() { loop() }
type Const ¶
type Const struct { Type Type // contains filtered or unexported fields }
Const is an immutable value.
type Enum ¶
type Enum struct {
// contains filtered or unexported fields
}
Enum is an enumerator.
func (Enum) AlignInBits ¶
func (t Enum) AlignInBits() int
func (Enum) SizeInBits ¶
func (t Enum) SizeInBits() int
type Executor ¶
type Executor struct {
// contains filtered or unexported fields
}
Executor executes module functions.
func (*Executor) AlignOf ¶
AlignOf returns the preferred stack/global alignment for the specified type.
func (*Executor) FieldOffsets ¶
func (*Executor) FunctionAddress ¶
FunctionAddress returns the address of the function f.
func (*Executor) GlobalAddress ¶
GlobalAddress returns the address of the global g.
func (*Executor) SizeOf ¶
SizeOf returns the offset in bytes between successive objects of the specified type, including alignment padding.
func (*Executor) StructLayout ¶
type Float ¶
type Float struct {
// contains filtered or unexported fields
}
Float represents a floating-point type.
func (Float) AlignInBits ¶
func (t Float) AlignInBits() int
func (Float) SizeInBits ¶
func (t Float) SizeInBits() int
type Function ¶
type Function struct { Name string Type *FunctionType // contains filtered or unexported fields }
Function represents a callable function.
func (*Function) LinkInternal ¶
LinkInternal makes this function use internal linkage.
func (*Function) LinkOnceODR ¶
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 ¶
LinkPrivate makes this function use private linkage.
func (*Function) SetLocation ¶
SetLocation sets the source location of the given function. SetLocation returns f so it can be called fluently.
func (*Function) SetParameterNames ¶
SetParameterNames assigns debug names to each of the function parameters.
type FunctionType ¶
type FunctionType struct { Signature Signature // contains filtered or unexported fields }
FunctionType is the type of a function
func (FunctionType) AlignInBits ¶
func (t FunctionType) AlignInBits() int
func (FunctionType) SizeInBits ¶
func (t FunctionType) SizeInBits() int
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 ¶
LinkPrivate makes this global use private linkage.
func (Global) LinkPublic ¶
LinkPublic makes this global use public linkage.
func (Global) SetConstant ¶
SetConstant makes this global constant.
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) AlignInBits ¶
func (t Integer) AlignInBits() int
func (Integer) SizeInBits ¶
func (t Integer) SizeInBits() int
type Module ¶
type Module struct { Types Types // contains filtered or unexported fields }
Module is a JIT module.
func (*Module) ConstStruct ¶
ConstStruct returns a constant struct with the value v.
func (*Module) EmitDebug ¶
func (m *Module) EmitDebug()
EmitDebug enables debug info generation for the module.
func (*Module) Extern ¶
Extern returns a global variable declared externally with the given name and type.
func (*Module) Function ¶
Function creates a new function with the given name, result type and parameters.
func (*Module) Global ¶
Global returns a new global variable intiailized with the specified constant value.
func (*Module) ParseFunctionSignature ¶
ParseFunctionSignature returns a function parsed from a C-style signature. Example:
"void* Foo(uint8_t i, bool b)"
func (*Module) ScalarOfType ¶
ScalarOfType returns a constant scalar with the value v with the given 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) AlignInBits ¶
func (t Pointer) AlignInBits() int
func (Pointer) SizeInBits ¶
func (t Pointer) SizeInBits() int
type Struct ¶
type Struct struct {
// contains filtered or unexported fields
}
Struct is the type of a structure.
func (*Struct) AlignInBits ¶
func (*Struct) Field ¶
Field returns the field with the given name. Field panics if the struct does not contain the given field.
func (*Struct) FieldIndex ¶
FieldIndex returns the index of the field with the given name, or -1 if the struct does not have a field with the given name.
func (*Struct) FieldOffsetInBits ¶
FieldOffsetInBits returns the field offset in bits from the start of the struct.
func (*Struct) SizeInBits ¶
type SwitchCase ¶
type SwitchCase struct { Conditions func() []*Value Block func() }
SwitchCase is a single condition and block used as a case statement in a switch.
type Triple ¶
type Triple struct {
// contains filtered or unexported fields
}
Triple represents an LLVM target triple.
func TargetTriple ¶
TargetTriple returns an LLVM target triple for the given ABI in the form:
<arch><sub>-<vendor>-<os>-<abi>
References: https://github.com/llvm-mirror/llvm/blob/master/lib/Support/Triple.cpp https://clang.llvm.org/docs/CrossCompilation.html
type Type ¶
type Type interface { String() string TypeName() string SizeInBits() int AlignInBits() int // contains filtered or unexported methods }
Type represents a codegen type.
func Underlying ¶
Underlying returns the underlying non-aliased type for ty.
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) DeclarePackedStruct ¶
DeclarePackedStruct creates a new, packed empty struct type.
func (*Types) DeclareStruct ¶
DeclareStruct creates a new, empty struct type.
func (*Types) FieldsOf ¶
FieldsOf returns the codegen fields of the given struct type. FieldsOf may also accept a reflect.Type.
func (*Types) Function ¶
func (t *Types) Function(resTy Type, paramTys ...Type) *FunctionType
Function returns a type representing the given function signature.
func (*Types) PackedStruct ¶
PackedStruct creates a new packed struct type.
type Value ¶
type Value struct {
// contains filtered or unexported fields
}
Value represents a value.
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) LoadUnaligned ¶
LoadUnaligned loads the element from the pointer value, it allows the loaded value to be unaligned
func (*Value) StoreUnaligned ¶
StoreUnaligned stores val to the pointer ptr. It assumes that the destination address may be unaligned
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) AlignInBits ¶
func (t Vector) AlignInBits() int
func (Vector) SizeInBits ¶
func (t Vector) SizeInBits() int