goory

package module
v0.0.0-...-21fe308 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2017 License: MIT Imports: 4 Imported by: 2

README

goory

Build Status Coverage Status godocs

A go package for compiling syntax trees to LLVM ir and WebAssembly ir

Example usage

Adding to floats
double := goory.DoubleType()

module := goory.NewModule("test")

function := module.NewFunction("fpadd", double)
a := function.AddArgument(double, "a")
b := function.AddArgument(double, "b")

block := function.Entry()

result := block.Fadd(a, b)

block.Ret(result)
Gcd algorithum
integer := goory.IntType(32)

module := goory.NewModule("test")

function := module.NewFunction("gcd", integer)
x := function.AddArgument(integer, "x")
y := function.AddArgument(integer, "y")

block := function.Entry()

xyEqual := block.Icmp(goory.IntEq, x, y)

trueBlock := function.AddBlock()
trueBlock.Ret(x)

falseBlock := function.AddBlock()
xyLess := falseBlock.Icmp(goory.IntUlt, x, y)

elseIfBlock := function.AddBlock()
call1 := elseIfBlock.Call(function, x, elseIfBlock.Sub(y, x))
elseIfBlock.Ret(call1)

elseBlock := function.AddBlock()
call2 := elseBlock.Call(function, elseBlock.Sub(x, y), y)
elseBlock.Ret(call2)

block.CondBr(xyEqual, trueBlock, falseBlock)
falseBlock.CondBr(xyLess, elseIfBlock, elseBlock)
Fibonaci numbers
integer := goory.IntType(32)
module := goory.NewModule("test")

function := module.NewFunction("fib", integer)
n := function.AddArgument(integer, "n")

block := function.Entry()

trueBlock := function.AddBlock()
nEqualsZero := block.Icmp(goory.IntEq, n, goory.Constant(integer, 0))
nEqualsOne := block.Icmp(goory.IntEq, n, goory.Constant(integer, 1))
nOneOrZero := block.Or(nEqualsZero, nEqualsOne)
trueBlock.Ret(n)

falseBlock := function.AddBlock()
fib1 := falseBlock.Call(function, falseBlock.Sub(n, goory.Constant(integer, 1)))
fib2 := falseBlock.Call(function, falseBlock.Sub(n, goory.Constant(integer, 2)))
falseBlock.Ret(falseBlock.Add(fib1, fib2))

block.CondBr(nOneOrZero, trueBlock, falseBlock)

Todos

  • Instructions:
    • Terminator instructions:
      • ret ✔
      • br ✔
      • switch
      • indirectbr
      • invoke
      • resume
      • catchswitch
      • catchret
      • cleanupret
      • unreachable
    • Binary operations:
      • add ✔
      • fadd ✔
      • sub ✔
      • fsub ✔
      • mul ✔
      • fmul ✔
      • udiv
      • sdiv ~
      • fdiv ✔
      • urem
      • srem
      • frem
    • Bitwise binary operations:
      • shl
      • lshr
      • ashr
      • and ✔
      • org
      • xor ✔
    • Vector operations:
      • extractelement
      • insertelement
      • shufflevector
    • Aggregate operations:
      • extractvalue
      • insertvalue
    • Memory access and addressing operations:
      • alloca
      • load
      • store
      • fence
      • cmpxchg
      • atomicrmw
      • getelementptr
    • Conversion operations:
      • trunc .. to ✔
      • zext .. to ✔
      • sext .. to ✔
      • fptrunc .. to ✔
      • fpext .. to ✔
      • fptoui .. to ✔
      • fptosi .. to ✔
      • uitofp .. to
      • sitofp .. to
      • ptrtoint .. to
      • inttoptr .. to
      • bitcast .. to
      • addrspacecast .. to
    • Other operations:
      • icmp ✔
      • fcmp ✔
      • phi
      • select
      • call ✔
      • va_arg
      • landingpad
      • catchpad
      • cleanuppad

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// FloatOeq is a float ordered equal comparison
	FloatOeq = "oeq"

	// FloatOlt is a float ordered less than comparison
	FloatOlt = "olt"

	// Float One is a float ordered not equals comparison
	FloatOne = "one"

	// FloatOgt is a float ordered greater than comparison
	FloatOgt = "ogt"
)
View Source
var (
	// IntEq is an integer equals comparison
	IntEq = "eq"

	// IntNe is an integer not equals comparison
	IntNe = "ne"

	// IntUlt is an integer unsigned less than comparison
	IntUlt = "ult"

	// IntSlt is an integer signed less than comparison
	IntSlt = "slt"

	// IntUgt is an integer unsigned greater than comparison
	IntUgt = "ugt"

	// IntSgt is an integer signed greater than comparison
	IntSgt = "sgt"
)

Functions

func ArrayType

func ArrayType(base types.Type, count int) types.Type

func BoolType

func BoolType() types.Type

func Constant

func Constant(constantType types.Type, value interface{}) value.Value

Constant reperesents a literal value

func DoubleType

func DoubleType() types.Type

func FloatType

func FloatType() types.Type

func IntType

func IntType(bits int) types.Type

Types

type Block

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

Block is a seqential list of instructions ending with a terminator instruction

func (*Block) Add

func (b *Block) Add(lhs value.Value, rhs value.Value) *instructions.Add

Add creates a new add instruction. lhs and rhs must be integer types of the same size. Add returns the result of this instruction with the same type as lhs and rhs.

func (*Block) Alloca

func (b *Block) Alloca(base types.Type) *instructions.Alloca

Alloca creates a new alloca instruction. type is the type you want to allocate space for. Call SetNumber to set the number of elements to allocate for (default 1) Alloca returns the result of this instruction as a pointer with the same type as base type.

func (*Block) And

func (b *Block) And(lhs, rhs value.Value) *instructions.And

And create a new and instruction. lhs and rhs must be boolean types. And returns the result of the instruction as a boolean type.

func (*Block) Br

func (b *Block) Br(block value.Value) *instructions.Br

Br creates a new br instruction. block must be a block type.

func (*Block) Call

func (b *Block) Call(function value.Value, operands ...value.Value) *instructions.Call

Call creates a new call instruction. function must be a function type. operands must match the types of the function arguments. Call returns the result of the instruction with the same type as the function return type.

func (*Block) Cast

func (b *Block) Cast(value value.Value, cast types.Type) value.Value

Cast creates a cast for value to type cast

func (*Block) CondBr

func (b *Block) CondBr(condition, trueBlock, falseBlock value.Value) *instructions.CondBr

CondBr creates a new conditional branch instruction. condition must be a boolean type. trueBlock and falseBlock must be block types.

func (*Block) Div

func (b *Block) Div(lhs, rhs value.Value) *instructions.Div

Div creates a new integer division instruction. lhs and rhs must be integer types. Div returns the result of the instruction with the same type as lhs and rhs.

func (*Block) Empty

func (b *Block) Empty() bool

func (*Block) Extractvalue

func (b *Block) Extractvalue(location value.Value, position value.Value) *instructions.Extractvalue

Extractvalue creates a new extract value instruction location must be an aggregate type position is the index at which to extract the value from location Extractvalue returns the type specified in location at index position

func (*Block) Fadd

func (b *Block) Fadd(lhs value.Value, rhs value.Value) *instructions.Fadd

Fadd creates a new float add instruction. lhs and rhs must be float or double types. Fadd returns the result of the instruction with the same type as lhs and rhs.

func (*Block) Fcmp

func (b *Block) Fcmp(mode string, lhs, rhs value.Value) *instructions.Fcmp

Fcmp creates a new float add instruction. mode controls the behavior of the comparison, see https://godoc.org/github.com/bongo227/goory#pkg-variables. lhs and rhs must be float or double types. Fcmp returns the result of the instruction as a boolean type.

func (*Block) Fdiv

func (b *Block) Fdiv(lhs value.Value, rhs value.Value) *instructions.Fdiv

Fdiv creates a new float add instruction. lhs and rhs must be float or double types. Fdiv returns the result of the instruction with the same type as lhs and rhs.

func (*Block) Fmul

func (b *Block) Fmul(lhs value.Value, rhs value.Value) *instructions.Fmul

Fmul creates a new float add instruction. lhs and rhs must be float or double types. Fmul returns the result of the instruction with the same type as lhs and rhs.

func (*Block) Fpext

func (b *Block) Fpext(value value.Value, cast types.Type) *instructions.Fpext

Fpext creates a new float extention instruction. value must be a float or double type. cast must be a type larger than value. Fpext returns the result of the instruction with the same type as cast.

func (*Block) Fptosi

func (b *Block) Fptosi(value value.Value, cast types.Type) *instructions.Fptosi

Fptosi creates a new float to signed integer type. value must be a float of double type. cast must be an integer type. Fptosi returns the result of the instruction with the same type as cast.

func (*Block) Fptrunc

func (b *Block) Fptrunc(value value.Value, cast types.Type) *instructions.Fptrunc

Fptrunc creates a new float truncation. value must be a float or double type. cast must be a type smaller than value. Fptrunc returns the result of the instruction with the same type as cast.

func (*Block) Fsub

func (b *Block) Fsub(lhs value.Value, rhs value.Value) *instructions.Fsub

Fsub creates a new float subtraction instruction. lhs and rhs must be float or double types. Fsub returns the result of the instruction with the same type as lhs and rhs.

func (*Block) Function

func (b *Block) Function() *Function

Function returns the parent function

func (*Block) Getelementptr

func (b *Block) Getelementptr(typ types.Type, element value.Pointer, location ...value.Value) *instructions.Getelementptr

func (*Block) Icmp

func (b *Block) Icmp(mode string, lhs, rhs value.Value) *instructions.Icmp

Icmp creates a new integer comparison instruction. mode controls the behavior of the comparison, see: https://godoc.org/github.com/bongo227/goory#pkg-variables. lhs and rhs must be integer types of the the same size. Icmp returns the result of the instruction as a boolean type.

func (*Block) Ident

func (b *Block) Ident() string

Ident gets the identifyer of the block

func (*Block) Insertvalue

func (b *Block) Insertvalue(location, value value.Value, position value.Value) *instructions.Insertvalue

Insertvalue creates a new insert value instruction. location must be an aggregate type the value is to be inserted into. value must match the type of the location at index position. position spesifys the index in which to insert value in location. Insertvalue returns the result of the instruction with the same type as location.

func (*Block) Llvm

func (b *Block) Llvm() string

Llvm returns the llvm ir representation of the block

func (*Block) Load

func (b *Block) Load(element value.Pointer) *instructions.Load

Load creates a new load instruction. Load returns the result of the instruction with the same type as the allocation.

func (*Block) Mul

func (b *Block) Mul(lhs value.Value, rhs value.Value) *instructions.Mul

Mul creates a new integer multiplication instruction. lhs and rhs must be integer types of the the same size. Mul returns the result of the instruction with the same type as lhs and rhs.

func (*Block) Name

func (b *Block) Name() string

Name gets the name of the block

func (*Block) Or

func (b *Block) Or(lhs value.Value, rhs value.Value) *instructions.Or

Or creates a new bitwise or instruction. lhs and rhs must be boolean types. Or returns the result of the instruction as a boolean type.

func (*Block) Phi

func (b *Block) Phi() *instructions.Phi

Phi creates a new phi instruction. Phi returns the phi instruction to which you add all incoming blocks.

func (*Block) Ret

func (b *Block) Ret(value value.Value) *instructions.Ret

Ret creates a new return instruction. value must be the same type as the function return type.

func (*Block) Sitofp

func (b *Block) Sitofp(value value.Value, cast types.Type) *instructions.Sitofp

Sitofp creates a new signed integer to float instruction. value must be an integer type. cast must be a float type. Sitofp returns the result of the instruction with the same type as cast.

func (*Block) Srem

func (b *Block) Srem(lhs value.Value, rhs value.Value) *instructions.Srem

Srem creates a new srem instruction. lhs and rhs must be integer types of the same size. Srem returns the result of this instruction with the same type as lhs and rhs.

func (*Block) Store

func (b *Block) Store(element value.Pointer, value value.Value)

Store creates a new store instruction. value must be the same type as the allocation. Store retruns the result of the instruction with the same type as lhs and rhs.

func (*Block) Sub

func (b *Block) Sub(lhs value.Value, rhs value.Value) *instructions.Sub

Sub creates a new float sub instruction. lhs and rhs must be float or double types. Sub retruns the result of the instruction with the same type as lhs and rhs.

func (*Block) Terminated

func (b *Block) Terminated() bool

Terminated returns true if the block is terminated (ends in branch or return)

func (*Block) Trunc

func (b *Block) Trunc(value value.Value, cast types.Type) *instructions.Trunc

Trunc creates a new integer truncation instruction. value must be an integer type. cast must be a integer type smaller than value. Trunc returns the result of the instruction with the same type as cast.

func (*Block) Type

func (b *Block) Type() types.Type

Type returns new block type

func (*Block) Xor

func (b *Block) Xor(lhs value.Value, rhs value.Value) *instructions.Xor

Xor creates a new float exclusive or instruction. lhs and rhs must be boolean types. Xor returns the result of the instruction as a boolean type.

func (*Block) Zext

func (b *Block) Zext(value value.Value, cast types.Type) *instructions.Zext

Zext creates a new zero extention instruction. value must be an integer type. cast must be a integer type larger than value. Zext returns the result of the instruction with the same type as cast.

type Function

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

Function is a group of instructions that are executed is a new stack frame

func (*Function) AddArgument

func (f *Function) AddArgument(argType types.Type, name string) types.Arg

AddArgument adds a new parameter to the function

func (*Function) AddBlock

func (f *Function) AddBlock() *Block

AddBlock adds a new block to the function

func (*Function) Arguments

func (f *Function) Arguments() []types.Arg

Arguments returns the values of function arguments

func (*Function) Entry

func (f *Function) Entry() *Block

Entry returns the entry block of the function

func (*Function) Ident

func (f *Function) Ident() string

Ident returns the function identifier

func (*Function) Llvm

func (f *Function) Llvm() string

Llvm returns the llvm ir representation of the function

func (*Function) Module

func (f *Function) Module() *Module

Module returns the module the function is in

func (*Function) Name

func (f *Function) Name() string

Name returns the function name

func (*Function) Type

func (f *Function) Type() types.Type

Type returns the function type

type Module

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

Module is a single compilation unit

func NewModule

func NewModule(name string) *Module

NewModule creates a new module with a name

func (*Module) LLVM

func (m *Module) LLVM() string

LLVM returns the module as llvm ir

func (*Module) Name

func (m *Module) Name() string

Name gets the name of the module

func (*Module) NewFunction

func (m *Module) NewFunction(name string, returnType types.Type) *Function

NewFunction adds a new function to module

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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