Documentation ¶
Overview ¶
Package ir implements Nebula IR instructions and stack.
Nebula IR is an intermediate language between Whitespace and LLVM IR.
Index ¶
- type AccessHandler
- type AccessStackStmt
- type BasicBlock
- type BinaryExpr
- type BinaryOp
- type Builder
- func (b *Builder) Block(n int) *BasicBlock
- func (b *Builder) Blocks() []*BasicBlock
- func (b *Builder) CreateAccessStackStmt(stackSize uint, pos token.Pos) *AccessStackStmt
- func (b *Builder) CreateBinaryExpr(op BinaryOp, lhs, rhs Value, pos token.Pos) *BinaryExpr
- func (b *Builder) CreateBlock() *BasicBlock
- func (b *Builder) CreateCallTerm(callee, next *BasicBlock, pos token.Pos) *CallTerm
- func (b *Builder) CreateExitTerm(pos token.Pos) *ExitTerm
- func (b *Builder) CreateFlushStmt(pos token.Pos) *FlushStmt
- func (b *Builder) CreateJmpCondTerm(op JmpCondOp, val Value, trueBlock, falseBlock *BasicBlock, pos token.Pos) *JmpCondTerm
- func (b *Builder) CreateJmpTerm(op JmpOp, jumpee *BasicBlock, pos token.Pos) *JmpTerm
- func (b *Builder) CreateLoadHeapExpr(addr Value, pos token.Pos) *LoadHeapExpr
- func (b *Builder) CreateLoadStackExpr(stackPos uint, pos token.Pos) *LoadStackExpr
- func (b *Builder) CreateOffsetStackStmt(offset int, pos token.Pos) *OffsetStackStmt
- func (b *Builder) CreatePrintStmt(op PrintOp, val Value, pos token.Pos) *PrintStmt
- func (b *Builder) CreateReadExpr(op ReadOp, pos token.Pos) *ReadExpr
- func (b *Builder) CreateRetTerm(pos token.Pos) *RetTerm
- func (b *Builder) CreateStoreHeapStmt(addr, val Value, pos token.Pos) *StoreHeapStmt
- func (b *Builder) CreateStoreStackStmt(stackPos uint, val Value, pos token.Pos) *StoreStackStmt
- func (b *Builder) CreateUnaryExpr(op UnaryOp, val Value, pos token.Pos) *UnaryExpr
- func (b *Builder) CurrentBlock() *BasicBlock
- func (b *Builder) InitBlocks(n int)
- func (b *Builder) Program() (*Program, error)
- func (b *Builder) SetCurrentBlock(block *BasicBlock)
- type CallTerm
- type ExitTerm
- type FlushStmt
- type Formatter
- type Inst
- type IntConst
- type JmpCondOp
- type JmpCondTerm
- type JmpOp
- type JmpTerm
- type Label
- type LoadHandler
- type LoadHeapExpr
- type LoadStackExpr
- type OffsetStackStmt
- type PhiExpr
- type PhiValue
- type PosBase
- type PrintOp
- type PrintStmt
- type Program
- type ReadExpr
- type ReadOp
- type RetTerm
- type RetUnderflowError
- type Stack
- func (s *Stack) Access(n uint, pos token.Pos)
- func (s *Stack) Accesses() uint
- func (s *Stack) At(n uint, pos token.Pos) (nth Value)
- func (s *Stack) Clear()
- func (s *Stack) Copy(n uint, pos token.Pos) (nth Value)
- func (s *Stack) Drop(pos token.Pos)
- func (s *Stack) DropN(n uint, pos token.Pos)
- func (s *Stack) Dup(pos token.Pos) (top Value)
- func (s *Stack) Get(n uint) (nth Value, ok bool)
- func (s *Stack) Len() uint
- func (s *Stack) Pop(pos token.Pos) (top Value)
- func (s *Stack) Pop2(pos token.Pos) (val1, val0 Value)
- func (s *Stack) Pops() uint
- func (s *Stack) Push(val Value)
- func (s *Stack) Slide(n uint, pos token.Pos)
- func (s *Stack) String() string
- func (s *Stack) Swap(pos token.Pos)
- func (s *Stack) Top(pos token.Pos) (top Value)
- func (s *Stack) Values() []Value
- type StoreHeapStmt
- type StoreStackStmt
- type TermBase
- type TermInst
- type UnaryExpr
- type UnaryOp
- type User
- type UserBase
- type Value
- type ValueBase
- type ValueUse
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AccessHandler ¶
AccessHandler watches accesses of values under stack frame.
type AccessStackStmt ¶
AccessStackStmt is a statement that asserts the stack length.
func NewAccessStackStmt ¶
func NewAccessStackStmt(stackSize uint, pos token.Pos) *AccessStackStmt
NewAccessStackStmt constructs a AccessStackStmt.
func (*AccessStackStmt) OpString ¶
func (*AccessStackStmt) OpString() string
OpString pretty prints the op kind.
type BasicBlock ¶
type BasicBlock struct { ID int // Unique block ID for printing LabelName string // Name derived from label Labels []Label // Labels for this block in source Nodes []Inst // Non-branching non-stack instructions Terminator TermInst // Terminator control flow instruction Entries []*BasicBlock // Entry blocks; blocks immediately preceding this block in flow Callers []*BasicBlock // Calling blocks; blocks calling this block or its parents Returns []*BasicBlock // Returning blocks; blocks returning to this block Prev *BasicBlock // Predecessor block in source Next *BasicBlock // Successor block in source }
BasicBlock is a list of consecutive non-branching instructions in a program followed by a branch.
func (*BasicBlock) AppendInst ¶
func (block *BasicBlock) AppendInst(inst Inst)
AppendInst appends an instruction to the block.
func (*BasicBlock) Disconnect ¶
func (block *BasicBlock) Disconnect()
Disconnect removes incoming edges to a basic block. The block is not removed from the program block slice and callers are not updated.
func (*BasicBlock) Name ¶
func (block *BasicBlock) Name() string
Name returns the name of the basic block from either the first label or the block address.
func (*BasicBlock) SetTerminator ¶
func (block *BasicBlock) SetTerminator(term TermInst)
SetTerminator sets the terminator instruction of the block.
func (*BasicBlock) String ¶
func (block *BasicBlock) String() string
func (*BasicBlock) Succs ¶
func (block *BasicBlock) Succs() []*BasicBlock
Succs returns all outgoing edges of the block.
type BinaryExpr ¶
BinaryExpr is an arithmetic expression with two operands.
func NewBinaryExpr ¶
func NewBinaryExpr(op BinaryOp, lhs, rhs Value, pos token.Pos) *BinaryExpr
NewBinaryExpr constructs a BinaryExpr.
func (*BinaryExpr) OpString ¶
func (bin *BinaryExpr) OpString() string
OpString pretty prints the op kind.
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder assists in IR construction.
func NewBuilder ¶
NewBuilder constructs a builder with a given number of basic blocks.
func (*Builder) CreateAccessStackStmt ¶
func (b *Builder) CreateAccessStackStmt(stackSize uint, pos token.Pos) *AccessStackStmt
CreateAccessStackStmt constructs a AccessStackStmt and appends it to the current block.
func (*Builder) CreateBinaryExpr ¶
CreateBinaryExpr constructs a BinaryExpr and appends it to the current block.
func (*Builder) CreateBlock ¶
func (b *Builder) CreateBlock() *BasicBlock
CreateBlock creates a block.
func (*Builder) CreateCallTerm ¶
func (b *Builder) CreateCallTerm(callee, next *BasicBlock, pos token.Pos) *CallTerm
CreateCallTerm constructs a CallTerm and appends it to the current block.
func (*Builder) CreateExitTerm ¶
CreateExitTerm constructs a ExitTerm and appends it to the current block.
func (*Builder) CreateFlushStmt ¶
CreateFlushStmt constructs a FlushStmt and appends it to the current block.
func (*Builder) CreateJmpCondTerm ¶
func (b *Builder) CreateJmpCondTerm(op JmpCondOp, val Value, trueBlock, falseBlock *BasicBlock, pos token.Pos) *JmpCondTerm
CreateJmpCondTerm constructs a JmpCondTerm and appends it to the current block.
func (*Builder) CreateJmpTerm ¶
CreateJmpTerm constructs a JmpTerm and appends it to the current block.
func (*Builder) CreateLoadHeapExpr ¶
func (b *Builder) CreateLoadHeapExpr(addr Value, pos token.Pos) *LoadHeapExpr
CreateLoadHeapExpr constructs a LoadHeapExpr and appends it to the current block.
func (*Builder) CreateLoadStackExpr ¶
func (b *Builder) CreateLoadStackExpr(stackPos uint, pos token.Pos) *LoadStackExpr
CreateLoadStackExpr constructs a LoadStackExpr and appends it to the current block.
func (*Builder) CreateOffsetStackStmt ¶
func (b *Builder) CreateOffsetStackStmt(offset int, pos token.Pos) *OffsetStackStmt
CreateOffsetStackStmt constructs a OffsetStackStmt and appends it to the current block.
func (*Builder) CreatePrintStmt ¶
CreatePrintStmt constructs a PrintStmt and appends it to the current block.
func (*Builder) CreateReadExpr ¶
CreateReadExpr constructs a ReadExpr and appends it to the current block.
func (*Builder) CreateRetTerm ¶
CreateRetTerm constructs a RetTerm and appends it to the current block.
func (*Builder) CreateStoreHeapStmt ¶
func (b *Builder) CreateStoreHeapStmt(addr, val Value, pos token.Pos) *StoreHeapStmt
CreateStoreHeapStmt constructs a StoreHeapStmt and appends it to the current block.
func (*Builder) CreateStoreStackStmt ¶
CreateStoreStackStmt constructs a StoreStackStmt and appends it to the current block.
func (*Builder) CreateUnaryExpr ¶
CreateUnaryExpr constructs a UnaryExpr and appends it to the current block.
func (*Builder) CurrentBlock ¶
func (b *Builder) CurrentBlock() *BasicBlock
CurrentBlock returns the currently selected block.
func (*Builder) InitBlocks ¶
InitBlocks creates n empty blocks and sets the first to be the current block.
func (*Builder) SetCurrentBlock ¶
func (b *Builder) SetCurrentBlock(block *BasicBlock)
SetCurrentBlock sets the currently selected block.
type CallTerm ¶
CallTerm is terminator that pushes the current location to the call stack, then jumps to the callee.
func NewCallTerm ¶
func NewCallTerm(callee, next *BasicBlock, pos token.Pos) *CallTerm
NewCallTerm constructs a CallTerm.
type FlushStmt ¶
type FlushStmt struct {
PosBase
}
FlushStmt is a statement that flushes stdout.
func NewFlushStmt ¶
NewFlushStmt constructs a FlushStmt.
type Formatter ¶
type Formatter struct {
// contains filtered or unexported fields
}
Formatter pretty prints Nebula IR.
func (*Formatter) FormatBlock ¶
func (f *Formatter) FormatBlock(block *BasicBlock) string
FormatBlock pretty prints a BasicBlock.
func (*Formatter) FormatInst ¶
FormatInst pretty prints an Inst.
func (*Formatter) FormatProgram ¶
FormatProgram pretty prints a Program.
func (*Formatter) FormatStack ¶
FormatStack pretty prints a stack.
func (*Formatter) FormatValue ¶
FormatValue pretty prints a value.
type IntConst ¶
IntConst is a constant integer value. The contained ints can be compared for pointer equality.
func NewIntConst ¶
NewIntConst constructs an IntConst.
type JmpCondOp ¶
type JmpCondOp uint8
JmpCondOp is the kind of operator for a conditional jump terminator.
type JmpCondTerm ¶
type JmpCondTerm struct { Op JmpCondOp UserBase TermBase PosBase // contains filtered or unexported fields }
JmpCondTerm is a terminator that conditionally jumps to one of two blocks.
func NewJmpCondTerm ¶
func NewJmpCondTerm(op JmpCondOp, val Value, trueBlock, falseBlock *BasicBlock, pos token.Pos) *JmpCondTerm
NewJmpCondTerm constructs a JmpCondTerm.
func (*JmpCondTerm) OpString ¶
func (jc *JmpCondTerm) OpString() string
OpString pretty prints the op kind.
type JmpTerm ¶
JmpTerm is a terminator that unconditionally jumps to a block.
func NewJmpTerm ¶
func NewJmpTerm(op JmpOp, jumpee *BasicBlock, pos token.Pos) *JmpTerm
NewJmpTerm constructs a JmpTerm.
type LoadHandler ¶
LoadHandler watches loads of values under stack frame.
type LoadHeapExpr ¶
LoadHeapExpr is an expression that loads a value at an address from the heap.
func NewLoadHeapExpr ¶
func NewLoadHeapExpr(addr Value, pos token.Pos) *LoadHeapExpr
NewLoadHeapExpr constructs a LoadHeapExpr.
func (*LoadHeapExpr) OpString ¶
func (*LoadHeapExpr) OpString() string
OpString pretty prints the op kind.
type LoadStackExpr ¶
LoadStackExpr is an expression that loads a value from under the current stack frame. A position of 1 is the top of the stack.
func NewLoadStackExpr ¶
func NewLoadStackExpr(stackPos uint, pos token.Pos) *LoadStackExpr
NewLoadStackExpr constructs a LoadStackExpr.
func (*LoadStackExpr) OpString ¶
func (*LoadStackExpr) OpString() string
OpString pretty prints the op kind.
type OffsetStackStmt ¶
OffsetStackStmt is a statement that changes the stack length relatively.
func NewOffsetStackStmt ¶
func NewOffsetStackStmt(offset int, pos token.Pos) *OffsetStackStmt
NewOffsetStackStmt constructs a OffsetStackStmt.
func (*OffsetStackStmt) OpString ¶
func (*OffsetStackStmt) OpString() string
OpString pretty prints the op kind.
type PhiExpr ¶
PhiExpr is an SSA Φ function with pairs of values and predecessor blocks.
func (*PhiExpr) AddIncoming ¶
func (phi *PhiExpr) AddIncoming(val Value, block *BasicBlock)
AddIncoming adds a val for an incoming edge to the phi expression.
type PhiValue ¶
type PhiValue struct { Value Value Block *BasicBlock }
PhiValue is a value and predecessor block.
type PosBase ¶
type PosBase struct {
// contains filtered or unexported fields
}
PosBase stores source position information.
type PrintStmt ¶
PrintStmt is an expression that prints a value to stdout.
func NewPrintStmt ¶
NewPrintStmt constructs a PrintStmt.
type Program ¶
type Program struct { Name string Blocks []*BasicBlock Entry *BasicBlock NextBlockID int File *token.File }
Program is a set of interconnected basic blocks.
func (*Program) DotDigraph ¶
DotDigraph creates a control flow graph in the Graphviz DOT format.
func (*Program) RenumberBlockIDs ¶
func (p *Program) RenumberBlockIDs()
RenumberBlockIDs cleans up block IDs to match the block index.
func (*Program) TrimUnreachable ¶
func (p *Program) TrimUnreachable()
TrimUnreachable removes uncalled blocks.
type ReadExpr ¶
ReadExpr is an expression that reads a value from stdin.
func NewReadExpr ¶
NewReadExpr constructs a ReadExpr.
type RetUnderflowError ¶
type RetUnderflowError struct {
Traces [][]*BasicBlock
}
RetUnderflowError is an error given when ret is executed without a caller.
func (*RetUnderflowError) Error ¶
func (err *RetUnderflowError) Error() string
type Stack ¶
type Stack struct { HandleAccess AccessHandler // Executed on access HandleLoad LoadHandler // Executed on load // contains filtered or unexported fields }
Stack models a stack frame for converting Whitespace stack-oriented operations to SSA form. When accessing a position under the stack frame, the load handler is invoked to provide that value.
type StoreHeapStmt ¶
StoreHeapStmt is a statement that stores a value at an address in the heap.
func NewStoreHeapStmt ¶
func NewStoreHeapStmt(addr, val Value, pos token.Pos) *StoreHeapStmt
NewStoreHeapStmt constructs a StoreHeapStmt.
func (*StoreHeapStmt) OpString ¶
func (*StoreHeapStmt) OpString() string
OpString pretty prints the op kind.
type StoreStackStmt ¶
StoreStackStmt is a statement that stores a value at a position in the stack.
func NewStoreStackStmt ¶
func NewStoreStackStmt(stackPos uint, val Value, pos token.Pos) *StoreStackStmt
NewStoreStackStmt constructs a StoreStackStmt.
func (*StoreStackStmt) OpString ¶
func (*StoreStackStmt) OpString() string
OpString pretty prints the op kind.
type TermBase ¶
type TermBase struct { PosBase // contains filtered or unexported fields }
TermBase implements the TermInst interface.
func (*TermBase) SetSucc ¶
func (term *TermBase) SetSucc(n int, block *BasicBlock)
SetSucc sets the specified successor block to a given block.
func (*TermBase) Succ ¶
func (term *TermBase) Succ(n int) *BasicBlock
Succ returns the specified successor block.
func (*TermBase) Succs ¶
func (term *TermBase) Succs() []*BasicBlock
Succs returns the terminator's successor blocks.
type TermInst ¶
type TermInst interface { Succs() []*BasicBlock NSuccs() int Succ(n int) *BasicBlock SetSucc(n int, block *BasicBlock) Inst }
TermInst is a branching instruction that terminates a basic block.
type UnaryExpr ¶
UnaryExpr is an arithmetic expression with one operand.
func NewUnaryExpr ¶
NewUnaryExpr constructs a UnaryExpr.
type User ¶
type User interface { Operands() []*ValueUse NOperands() int Operand(n int) *ValueUse SetOperand(n int, val Value) ClearOperands() UsesValue(val Value) bool Inst }
User is an instruction that uses values.
type UserBase ¶
type UserBase struct { PosBase // contains filtered or unexported fields }
UserBase implements the User interface.
func (*UserBase) ClearOperands ¶
func (user *UserBase) ClearOperands()
ClearOperands clears all operands and removes the uses.
func (*UserBase) SetOperand ¶
SetOperand sets the specified operand to a value and updates the use lists.
type Value ¶
type Value interface { Uses() []*ValueUse NUses() int AddUse(use *ValueUse) RemoveUse(use *ValueUse) bool ReplaceUsesWith(other Value) Pos() token.Pos }
Value is an expression or constant with a set of uses.
type ValueBase ¶
type ValueBase struct {
// contains filtered or unexported fields
}
ValueBase implements the Value interface.
func (*ValueBase) ReplaceUsesWith ¶
ReplaceUsesWith replaces all uses of def with newDef.