ir

package module
v0.0.0-...-da7ba2e Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2018 License: BSD-3-Clause Imports: 17 Imported by: 14

README

github.com/cznic/ir has moved to modernc.org/ir (vcs).

Please update your import paths to modernc.org/ir.

This repo is now archived.

Documentation

Overview

Package ir implements intermediate representation of compiled programs. (Work In Progress)

See: https://en.wikipedia.org/wiki/Intermediate_representation

Concepts

From the POV of this package, an IR is a slice of Objects. Object is either a DataDefinition or a FunctionDefinition. All objects are defined by Linkage, NameID and TypeID fields.

DataDefintions reserve global, static data storage and have an optional Value. If Value is nil the DataDefintion defines a zero value of its type.

FunctionDefinitions have a Body which is a slice of Operation. Operations are, for example, Add, Return, etc. The operation execution model is a zero register stack machine.

Verifying and linking

After generating a slice of Objects, its every item should be verified using Verify. One or more generated IRs can be turned into complete IRs using LinkMain or LinkLib. These functions will check and resolve external definitions by filling the respective Index fields of certain operations, for example Global. A properly linked IR should be suitable for back-end code generation of a program or a library.

Executing IR programs

cznic/virtual is an IR code generator for a virtual CPU and can also run the resulting binary. The virtual CPU is not very fast, it's best use is probably just to verify a particular IR generator or to provide an interpreter for scripts loaded/entered at run time. A "standard" back-end should normally produce machine code,

Index

Constants

This section is empty.

Variables

View Source
var (

	// Testing amends things for tests.
	Testing bool
)

Functions

func PrettyString

func PrettyString(v interface{}) string

PrettyString turns certain things, produced by this package, into neatly format text.

Types

type Add

type Add struct {
	TypeID TypeID // Operands type.
	token.Position
}

Add operation adds the top stack item (b) and the previous one (a) and replaces both operands with a + b.

func (*Add) Pos

func (o *Add) Pos() token.Position

Pos implements Operation.

func (*Add) String

func (o *Add) String() string

type AddressValue

type AddressValue struct {
	// A negative value or object index as resolved by the linker.
	Index int
	Label NameID
	Linkage
	NameID NameID
	Offset uintptr
	// contains filtered or unexported fields
}

AddressValue is a declaration initializer constant of type address. Its final value is determined by the linker/loader.

func (*AddressValue) String

func (v *AddressValue) String() string

type AllocResult

type AllocResult struct {
	TypeID   TypeID
	TypeName NameID
	token.Position
}

AllocResult operation reserves evaluation stack space for a result of type TypeID.

func (*AllocResult) Pos

func (o *AllocResult) Pos() token.Position

Pos implements Operation.

func (*AllocResult) String

func (o *AllocResult) String() string

type And

type And struct {
	TypeID TypeID // Operands type.
	token.Position
}

And operation replaces TOS with the bitwise and of the top two stack items.

func (*And) Pos

func (o *And) Pos() token.Position

Pos implements Operation.

func (*And) String

func (o *And) String() string

type Argument

type Argument struct {
	Address bool
	Index   int
	TypeID  TypeID
	token.Position
}

Argument pushes argument Index, or its address, to the evaluation stack.

func (*Argument) Pos

func (o *Argument) Pos() token.Position

Pos implements Operation.

func (*Argument) String

func (o *Argument) String() string

type Arguments

type Arguments struct {
	token.Position
	FunctionPointer bool // TOS contains a function pointer for a subsequent CallFP. Determined by linker.
}

Arguments operation annotates that function results, if any, are allocated and a function pointer is at TOS. Evaluation of any function arguments follows.

func (*Arguments) Pos

func (o *Arguments) Pos() token.Position

Pos implements Operation.

func (*Arguments) String

func (o *Arguments) String() string

type ArrayType

type ArrayType struct {
	TypeBase
	Item  Type
	Items int64
}

ArrayType represents a collection of items that can be selected by index.

func (*ArrayType) Pointer

func (t *ArrayType) Pointer() Type

Pointer implements Type.

type BeginScope

type BeginScope struct {
	// Evaluation stack may be non-empty on entering the scope. See
	// https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html
	Value bool
	token.Position
}

BeginScope operation annotates entering a block scope.

func (*BeginScope) Pos

func (o *BeginScope) Pos() token.Position

Pos implements Operation.

func (*BeginScope) String

func (o *BeginScope) String() string

type Bool

type Bool struct {
	TypeID TypeID // Operand type.
	token.Position
}

Bool operation converts TOS to a bool (ie. an int32) such that the result reflects if the operand was non zero.

func (*Bool) Pos

func (o *Bool) Pos() token.Position

Pos implements Operation.

func (*Bool) String

func (o *Bool) String() string

type Call

type Call struct {
	Arguments int    // Actual number of arguments passed to function.
	Comma     bool   // The call operation is produced by the C comma operator for a void function.
	Index     int    // A negative value or an function object index as resolved by the linker.
	TypeID    TypeID // Type of the function.
	token.Position
}

Call operation performs a static function call. The evaluation stack contains the space reseved for function results, if any, and any function arguments. On return all arguments are removed from the stack.

func (*Call) Pos

func (o *Call) Pos() token.Position

Pos implements Operation.

func (*Call) String

func (o *Call) String() string

type CallFP

type CallFP struct {
	Arguments int    // Actual number of arguments passed to function.
	Comma     bool   // The call FP operation is produced by the C comma operator for a void function.
	TypeID    TypeID // Type of the function pointer.
	token.Position
}

CallFP operation performs a function pointer call. The evaluation stack contains the space reseved for function results, if any, the function pointer and any function arguments. On return all arguments and the function pointer are removed from the stack.

func (*CallFP) Pos

func (o *CallFP) Pos() token.Position

Pos implements Operation.

func (*CallFP) String

func (o *CallFP) String() string

type Complex128Value

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

Complex128Value is a declaration initializer constant of type complex128.

func (*Complex128Value) String

func (v *Complex128Value) String() string

type Complex64Value

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

Complex64Value is a declaration initializer constant of type complex64.

func (*Complex64Value) String

func (v *Complex64Value) String() string

type CompositeValue

type CompositeValue struct {
	Values []Value
	// contains filtered or unexported fields
}

CompositeValue represents a constant array/struct initializer.

func (*CompositeValue) String

func (v *CompositeValue) String() string

type Const

type Const struct {
	TypeID TypeID
	Value  Value
	token.Position
}

Const operation pushes a constant value on the evaluation stack.

func (*Const) Pos

func (o *Const) Pos() token.Position

Pos implements Operation.

func (*Const) String

func (o *Const) String() string

type Const32

type Const32 struct {
	LOp    bool // This operation is an artifact of || or &&.
	TypeID TypeID
	Value  int32
	token.Position
}

Const32 operation pushes a 32 bit value on the evaluation stack.

func (*Const32) Pos

func (o *Const32) Pos() token.Position

Pos implements Operation.

func (*Const32) String

func (o *Const32) String() string

type Const64

type Const64 struct {
	TypeID TypeID
	Value  int64
	token.Position
}

Const64 operation pushes a 64 bit value on the evaluation stack.

func (*Const64) Pos

func (o *Const64) Pos() token.Position

Pos implements Operation.

func (*Const64) String

func (o *Const64) String() string

type ConstC128

type ConstC128 struct {
	TypeID TypeID
	Value  complex128
	token.Position
}

ConstC128 operation pushes a complex128 value on the evaluation stack.

func (*ConstC128) Pos

func (o *ConstC128) Pos() token.Position

Pos implements Operation.

func (*ConstC128) String

func (o *ConstC128) String() string

type Convert

type Convert struct {
	Result TypeID // Conversion type.
	TypeID TypeID // Operand type.
	token.Position
}

Convert operation converts TOS to the result type.

func (*Convert) Pos

func (o *Convert) Pos() token.Position

Pos implements Operation.

func (*Convert) String

func (o *Convert) String() string

type Copy

type Copy struct {
	TypeID TypeID // Operand type.
	token.Position
}

Copy assigns source, which address is at TOS, to dest, which address is the previous stack item. The source address is removed from the stack.

func (*Copy) Pos

func (o *Copy) Pos() token.Position

Pos implements Operation.

func (*Copy) String

func (o *Copy) String() string

type Cpl

type Cpl struct {
	TypeID TypeID // Operand type.
	token.Position
}

Cpl operation replaces TOS with ^TOS (bitwise complement).

func (*Cpl) Pos

func (o *Cpl) Pos() token.Position

Pos implements Operation.

func (*Cpl) String

func (o *Cpl) String() string

type DataDefinition

type DataDefinition struct {
	ObjectBase
	Value
}

DataDefinition represents a variable definition and an optional initializer value.

func NewDataDefinition

func NewDataDefinition(p token.Position, name, typeName NameID, typ TypeID, l Linkage, initializer Value) *DataDefinition

NewDataDefinition returns a newly created DataDefinition.

func (*DataDefinition) Verify

func (d *DataDefinition) Verify() error

Verify implements Object.

type DesignatedValue

type DesignatedValue struct {
	Index int // Array index or field index.
	Value
}

DesignatedValue represents the value of a particular array element or a particular struct field.

func (*DesignatedValue) String

func (v *DesignatedValue) String() string

type Div

type Div struct {
	TypeID TypeID // Operands type.
	token.Position
}

Div operation subtracts the top stack item (b) and the previous one (a) and replaces both operands with a / b. The operation panics if operands are integers and b == 0.

func (*Div) Pos

func (o *Div) Pos() token.Position

Pos implements Operation.

func (*Div) String

func (o *Div) String() string

type Drop

type Drop struct {
	Comma  bool // The drop operation is produced by the C comma operator.
	LOp    bool // This operation is an artifact of || or &&.
	TypeID TypeID
	token.Position
}

Drop operation removes one item from the evaluation stack.

func (*Drop) Pos

func (o *Drop) Pos() token.Position

Pos implements Operation.

func (*Drop) String

func (o *Drop) String() string

type Dup

type Dup struct {
	TypeID TypeID
	token.Position
}

Dup operation duplicates the top stack item.

func (*Dup) Pos

func (o *Dup) Pos() token.Position

Pos implements Operation.

func (*Dup) String

func (o *Dup) String() string

type Element

type Element struct {
	Address   bool
	IndexType TypeID
	Neg       bool   // Negate the index expression.
	TypeID    TypeID // The indexed type.
	token.Position
}

Element replaces a pointer and index with the indexed element or its address.

func (*Element) Pos

func (o *Element) Pos() token.Position

Pos implements Operation.

func (*Element) String

func (o *Element) String() string

type EndScope

type EndScope struct {
	// Leaving the scope may leave values on the evaluation stack. See
	// https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html
	Value bool
	token.Position
}

EndScope operation annotates leaving a block scope.

func (*EndScope) Pos

func (o *EndScope) Pos() token.Position

Pos implements Operation.

func (*EndScope) String

func (o *EndScope) String() string

type Eq

type Eq struct {
	TypeID TypeID // Operands type.
	token.Position
}

Eq operation compares the top stack item (b) and the previous one (a) and replaces both operands with a non zero int32 value if a == b or zero otherwise.

func (*Eq) Pos

func (o *Eq) Pos() token.Position

Pos implements Operation.

func (*Eq) String

func (o *Eq) String() string

type Field

type Field struct {
	Address bool
	Index   int
	TypeID  TypeID // Pointer to a struct/union.
	token.Position
}

Field replaces a struct/union pointer at TOS with its field by index, or its address.

func (*Field) Pos

func (o *Field) Pos() token.Position

Pos implements Operation.

func (*Field) String

func (o *Field) String() string

type FieldProperties

type FieldProperties struct {
	Offset  int64 // Relative to start of the struct/union.
	Size    int64 // Field size for copying.
	Padding int   // Adjustment to enforce proper alignment.
}

FieldProperties describe a struct/union field.

func (*FieldProperties) Sizeof

func (f *FieldProperties) Sizeof() int64

Sizeof returns the sum of f.Size and f.Padding.

type FieldValue

type FieldValue struct {
	Index  int
	TypeID TypeID // Struct/union type.
	token.Position
}

FieldValue replaces a struct/union at TOS with its field by index.

func (*FieldValue) Pos

func (o *FieldValue) Pos() token.Position

Pos implements Operation.

func (*FieldValue) String

func (o *FieldValue) String() string

type Float32Value

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

Float32Value is a declaration initializer constant of type float32.

func (*Float32Value) String

func (v *Float32Value) String() string

type Float64Value

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

Float64Value is a declaration initializer constant of type float64.

func (*Float64Value) String

func (v *Float64Value) String() string

type FunctionDefinition

type FunctionDefinition struct {
	Arguments []NameID // May be nil.
	Body      []Operation
	ObjectBase
	Results []NameID // May be nil.
}

FunctionDefinition represents a function definition.

func NewFunctionDefinition

func NewFunctionDefinition(p token.Position, name, typeName NameID, typ TypeID, l Linkage, argumnents, results []NameID) *FunctionDefinition

NewFunctionDefinition returns a newly created FunctionDefinition.

func (*FunctionDefinition) Verify

func (f *FunctionDefinition) Verify() (err error)

Verify implements Object.

type FunctionType

type FunctionType struct {
	TypeBase
	Arguments []Type
	Results   []Type
	Variadic  bool // C-variadic.
}

FunctionType represents a function, its possibly variadic, optional arguments and results.

func (*FunctionType) Pointer

func (t *FunctionType) Pointer() Type

Pointer implements Type.

type Geq

type Geq struct {
	TypeID TypeID // Operands type.
	token.Position
}

Geq operation compares the top stack item (b) and the previous one (a) and replaces both operands with a non zero int32 value if a >= b or zero otherwise.

func (*Geq) Pos

func (o *Geq) Pos() token.Position

Pos implements Operation.

func (*Geq) String

func (o *Geq) String() string

type Global

type Global struct {
	Address bool
	Index   int // A negative value or an object index as resolved by the linker.
	Linkage
	NameID   NameID
	TypeID   TypeID
	TypeName NameID
	token.Position
}

Global operation pushes a global variable, or its address, to the evaluation stack.

func (*Global) Pos

func (o *Global) Pos() token.Position

Pos implements Operation.

func (*Global) String

func (o *Global) String() string

type Gt

type Gt struct {
	TypeID TypeID // Operands type.
	token.Position
}

Gt operation compares the top stack item (b) and the previous one (a) and replaces both operands with a non zero int32 value if a > b or zero otherwise.

func (*Gt) Pos

func (o *Gt) Pos() token.Position

Pos implements Operation.

func (*Gt) String

func (o *Gt) String() string

type Int32Value

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

Int32Value is a declaration initializer constant of type int32.

func (*Int32Value) String

func (v *Int32Value) String() string

type Int64Value

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

Int64Value is a declaration initializer constant of type int64.

func (*Int64Value) String

func (v *Int64Value) String() string

type Jmp

type Jmp struct {
	Cond   bool // This operation is an artifact of the conditional operator.
	NameID NameID
	Number int
	token.Position
}

Jmp operation performs a branch to a named or numbered label.

func (*Jmp) Pos

func (o *Jmp) Pos() token.Position

Pos implements Operation.

func (*Jmp) String

func (o *Jmp) String() string

type JmpP

type JmpP struct {
	token.Position
}

JmpP operation performs a branch to pointer at TOS.

func (*JmpP) Pos

func (o *JmpP) Pos() token.Position

Pos implements Operation.

func (*JmpP) String

func (o *JmpP) String() string

type Jnz

type Jnz struct {
	LOp    bool // This operation is an artifact of || or &&.
	NameID NameID
	Number int
	token.Position
}

Jnz operation performs a branch to a named or numbered label if the top of the stack is non zero. The TOS type must be int32 and the operation removes TOS.

func (*Jnz) Pos

func (o *Jnz) Pos() token.Position

Pos implements Operation.

func (*Jnz) String

func (o *Jnz) String() string

type Jz

type Jz struct {
	LOp    bool // This operation is an artifact of || or && or the conditional operator.
	NameID NameID
	Number int
	token.Position
}

Jz operation performs a branch to a named or numbered label if the top of the stack is zero. The TOS type must be int32 and the operation removes TOS.

func (*Jz) Pos

func (o *Jz) Pos() token.Position

Pos implements Operation.

func (*Jz) String

func (o *Jz) String() string

type Label

type Label struct {
	Cond   bool // This operation is an artifact of the conditional operator.
	LAnd   bool // This operation is an artifact of &&.
	LOr    bool // This operation is an artifact of ||.
	NameID NameID
	Nop    bool // This operation is an artifact of the conditional operator.
	Number int
	token.Position
}

Label operation declares a named or numbered branch target. A valid Label must have a non zero NameID or non negative Number.

func (*Label) IsValid

func (o *Label) IsValid() bool

IsValid reports whether o has a positive NameID or a non negative Number.

func (*Label) Pos

func (o *Label) Pos() token.Position

Pos implements Operation.

func (*Label) String

func (o *Label) String() string

type Leq

type Leq struct {
	TypeID TypeID // Operands type.
	token.Position
}

Leq operation compares the top stack item (b) and the previous one (a) and replaces both operands with a non zero int32 value if a <= b or zero otherwise.

func (*Leq) Pos

func (o *Leq) Pos() token.Position

Pos implements Operation.

func (*Leq) String

func (o *Leq) String() string

type Linkage

type Linkage int

Linkage represents a linkage type.

const (
	ExternalLinkage Linkage
	InternalLinkage
)

Linkage values.

func (Linkage) String

func (i Linkage) String() string

type Load

type Load struct {
	TypeID TypeID // Pointer type.
	token.Position
}

Load replaces a pointer at TOS by its pointee.

func (*Load) Pos

func (o *Load) Pos() token.Position

Pos implements Operation.

func (*Load) String

func (o *Load) String() string

type Lsh

type Lsh struct {
	TypeID TypeID // Operand (a) type.
	token.Position
}

Lsh operation uses the top stack item (b), which must be an int32, and the previous one (a), which must be an integral type and replaces both operands with a << b.

func (*Lsh) Pos

func (o *Lsh) Pos() token.Position

Pos implements Operation.

func (*Lsh) String

func (o *Lsh) String() string

type Lt

type Lt struct {
	TypeID TypeID // Operands type.
	token.Position
}

Lt operation compares the top stack item (b) and the previous one (a) and replaces both operands with a non zero int32 value if a < b or zero otherwise.

func (*Lt) Pos

func (o *Lt) Pos() token.Position

Pos implements Operation.

func (*Lt) String

func (o *Lt) String() string

type MemoryModel

type MemoryModel map[TypeKind]MemoryModelItem

MemoryModel defines properties of types. A valid memory model must provide model items for all type kinds except Array, Struct and Union. Methods of invalid models may panic. Memory model instances are not modified by this package and safe for concurrent use by multiple goroutines as long as any of them does not modify them either.

func NewMemoryModel

func NewMemoryModel() (MemoryModel, error)

NewMemoryModel returns a new MemoryModel for the current architecture and platform or an error, if any.

func (MemoryModel) Alignof

func (m MemoryModel) Alignof(t Type) int

Alignof computes the memory alignment requirements of t. Zero is returned for a struct/union type with no fields.

func (MemoryModel) Layout

Layout computes the memory layout of t.

func (MemoryModel) Sizeof

func (m MemoryModel) Sizeof(t Type) int64

Sizeof computes the memory size of t.

func (MemoryModel) StructAlignof

func (m MemoryModel) StructAlignof(t Type) int

StructAlignof computes the memory alignment requirements of t when its instance is a struct field. Zero is returned for a struct/union type with no fields.

type MemoryModelItem

type MemoryModelItem struct {
	Size        uint
	Align       uint
	StructAlign uint
}

MemoryModelItem describes memory properties of a particular type kind.

type Mul

type Mul struct {
	TypeID TypeID // Operands type.
	token.Position
}

Mul operation subtracts the top stack item (b) and the previous one (a) and replaces both operands with a * b.

func (*Mul) Pos

func (o *Mul) Pos() token.Position

Pos implements Operation.

func (*Mul) String

func (o *Mul) String() string

type NameID

type NameID int

NameID is a numeric identifier of an identifier as registered in a global dictionary[0].

[0]: https://godoc.org/github.com/cznic/xc#pkg-variables

func (*NameID) GobDecode

func (t *NameID) GobDecode(b []byte) error

GobDecode implements GobDecoder.

func (NameID) GobEncode

func (t NameID) GobEncode() ([]byte, error)

GobEncode implements GobEncoder.

func (NameID) String

func (t NameID) String() string

String implements fmt.Stringer.

type Neg

type Neg struct {
	TypeID TypeID // Operand type.
	token.Position
}

Neg operation replaces TOS with 0-TOS.

func (*Neg) Pos

func (o *Neg) Pos() token.Position

Pos implements Operation.

func (*Neg) String

func (o *Neg) String() string

type Neq

type Neq struct {
	TypeID TypeID // Operands type.
	token.Position
}

Neq operation compares the top stack item (b) and the previous one (a) and replaces both operands with a non zero int32 value if a != b or zero otherwise.

func (*Neq) Pos

func (o *Neq) Pos() token.Position

Pos implements Operation.

func (*Neq) String

func (o *Neq) String() string

type Nil

type Nil struct {
	TypeID TypeID // Pointer type.
	token.Position
}

Nil pushes a typed nil to TOS.

func (*Nil) Pos

func (o *Nil) Pos() token.Position

Pos implements Operation.

func (*Nil) String

func (o *Nil) String() string

type Not

type Not struct {
	token.Position
}

Not replaces the boolean value at TOS with !value. The TOS type must be int32.

func (*Not) Pos

func (o *Not) Pos() token.Position

Pos implements Operation.

func (*Not) String

func (o *Not) String() string

type Object

type Object interface {
	// Verify checks if the object is well-formed. Verify may mutate the
	// object. For example, Verify may remove provably unreachable code of
	// a FunctionDefinition.Body.
	Verify() error
	Base() *ObjectBase
}

Object represents a declarations or definitions of static data and functions.

func LinkLib

func LinkLib(translationUnits ...[]Object) (_ []Object, err error)

LinkLib returns all objects with external linkage defined in translationUnits. Linking may mutate passed objects. It's the caller responsibility to ensure all translationUnits were produced for the same architecture and platform.

LinkLib panics when passed no data.

func LinkMain

func LinkMain(translationUnits ...[]Object) (_ []Object, err error)

LinkMain returns all objects transitively referenced from function _start or an error, if any. Linking may mutate passed objects. It's the caller responsibility to ensure all translationUnits were produced for the same architecture and platform.

LinkMain panics when passed no data.

type ObjectBase

type ObjectBase struct {
	Comment NameID
	Linkage
	NameID   NameID
	Package  NameID
	TypeID   TypeID
	TypeName NameID
	token.Position
}

ObjectBase collects fields common to all objects.

func (*ObjectBase) Base

func (o *ObjectBase) Base() *ObjectBase

Base implements Object.

type Objects

type Objects [][]Object

Objects represent []Object implementing io.ReaderFrom and io.WriterTo.

func (*Objects) ReadFrom

func (o *Objects) ReadFrom(r io.Reader) (n int64, err error)

ReadFrom reads o from r.

func (Objects) WriteTo

func (o Objects) WriteTo(w io.Writer) (n int64, err error)

WriteTo writes o to w.

type Operation

type Operation interface {
	Pos() token.Position
	// contains filtered or unexported methods
}

Operation is a unit of execution.

type Or

type Or struct {
	TypeID TypeID // Operands type.
	token.Position
}

Or operation replaces TOS with the bitwise or of the top two stack items.

func (*Or) Pos

func (o *Or) Pos() token.Position

Pos implements Operation.

func (*Or) String

func (o *Or) String() string

type Panic

type Panic struct {
	token.Position
}

Panic operation aborts execution with a stack trace.

func (*Panic) Pos

func (o *Panic) Pos() token.Position

Pos implements Operation.

func (*Panic) String

func (o *Panic) String() string

type PointerType

type PointerType struct {
	TypeBase
	Element Type
}

PointerType represents a pointer to an element, an instance of another type.

func (*PointerType) Pointer

func (t *PointerType) Pointer() Type

Pointer implements Type.

type PostIncrement

type PostIncrement struct {
	BitFieldType TypeID
	BitOffset    int
	Bits         int
	Delta        int
	TypeID       TypeID // Operand type.
	token.Position
}

PostIncrement operation adds Delta to the value pointed to by address at TOS and replaces TOS by the value pointee had before the increment. If Bits is non zero then the effective operand type is BitFieldType and the bit field starts at bit BitOffset.

func (*PostIncrement) Pos

func (o *PostIncrement) Pos() token.Position

Pos implements Operation.

func (*PostIncrement) String

func (o *PostIncrement) String() string

type PreIncrement

type PreIncrement struct {
	BitFieldType TypeID
	BitOffset    int
	Bits         int
	Delta        int
	TypeID       TypeID // Operand type.
	token.Position
}

PreIncrement operation adds Delta to the value pointed to by address at TOS and replaces TOS by the new value of the pointee. If Bits is non zero then the effective operand type is BitFieldType and the bit field starts at bit BitOffset.

func (*PreIncrement) Pos

func (o *PreIncrement) Pos() token.Position

Pos implements Operation.

func (*PreIncrement) String

func (o *PreIncrement) String() string

type PtrDiff

type PtrDiff struct {
	PtrType TypeID
	TypeID  TypeID // Operands type.
	token.Position
}

PtrDiff operation subtracts the top stack item (b) and the previous one (a) and replaces both operands with a - b of type TypeID.

func (*PtrDiff) Pos

func (o *PtrDiff) Pos() token.Position

Pos implements Operation.

func (*PtrDiff) String

func (o *PtrDiff) String() string

type Rem

type Rem struct {
	TypeID TypeID // Operands type.
	token.Position
}

Rem operation divides the top stack item (b) and the previous one (a) and replaces both operands with a % b. The operation panics if b == 0.

func (*Rem) Pos

func (o *Rem) Pos() token.Position

Pos implements Operation.

func (*Rem) String

func (o *Rem) String() string

type Result

type Result struct {
	Address bool
	Index   int
	TypeID  TypeID
	token.Position
}

Result pushes a function result by index, or its address, to the evaluation stack.

func (*Result) Pos

func (o *Result) Pos() token.Position

Pos implements Operation.

func (*Result) String

func (o *Result) String() string

type Return

type Return struct {
	token.Position
}

Return operation removes all function call arguments from the evaluation stack as well as the function pointer used in the call, if any.

func (*Return) Pos

func (o *Return) Pos() token.Position

Pos implements Operation.

func (*Return) String

func (o *Return) String() string

type Rsh

type Rsh struct {
	TypeID TypeID // Operand (a) type.
	token.Position
}

Rsh operation uses the top stack item (b), which must be an int32, and the previous one (a), which must be an integral type and replaces both operands with a >> b.

func (*Rsh) Pos

func (o *Rsh) Pos() token.Position

Pos implements Operation.

func (*Rsh) String

func (o *Rsh) String() string

type Store

type Store struct {
	BitOffset int
	Bits      int
	TypeID    TypeID // Type of the value.
	token.Position
}

Store operation stores a TOS value at address in the preceding stack position. The address is removed from the evaluation stack. If Bits is non zero then the destination is a bit field starting at bit BitOffset.

func (*Store) Pos

func (o *Store) Pos() token.Position

Pos implements Operation.

func (*Store) String

func (o *Store) String() string

type StringConst

type StringConst struct {
	Value  StringID
	TypeID TypeID // Type of the pointer to the string value.
	token.Position
}

StringConst operation pushes a string value on the evaluation stack.

func (*StringConst) Pos

func (o *StringConst) Pos() token.Position

Pos implements Operation.

func (*StringConst) String

func (o *StringConst) String() string

type StringID

type StringID int

StringID is a numeric identifier of a string literal as registered in a global dictionary[0].

[0]: https://godoc.org/github.com/cznic/xc#pkg-variables

func (*StringID) GobDecode

func (t *StringID) GobDecode(b []byte) error

GobDecode implements GobDecoder.

func (StringID) GobEncode

func (t StringID) GobEncode() ([]byte, error)

GobEncode implements GobEncoder.

func (StringID) String

func (t StringID) String() string

String implements fmt.Stringer.

type StringValue

type StringValue struct {
	Offset   uintptr
	StringID StringID
	// contains filtered or unexported fields
}

StringValue is a declaration initializer constant of type string.

func (*StringValue) String

func (v *StringValue) String() string

type StructOrUnionType

type StructOrUnionType struct {
	Fields []Type
	Names  []NameID
	TypeBase
}

StructOrUnionType represents a collection of fields that can be selected by name.

func (*StructOrUnionType) Pointer

func (t *StructOrUnionType) Pointer() Type

Pointer implements Type.

type Sub

type Sub struct {
	TypeID TypeID // Operands type.
	token.Position
}

Sub operation subtracts the top stack item (b) and the previous one (a) and replaces both operands with a - b.

func (*Sub) Pos

func (o *Sub) Pos() token.Position

Pos implements Operation.

func (*Sub) String

func (o *Sub) String() string

type Switch

type Switch struct {
	Default Label
	Labels  []Label
	TypeID  TypeID // Operand type.
	Values  []Value
	token.Position
}

Switch jumps to a label according to a value at TOS or to a default label. The value at TOS is removed from the evaluation stack.

func (*Switch) Pos

func (o *Switch) Pos() token.Position

Pos implements Operation.

func (*Switch) String

func (o *Switch) String() string

type Type

type Type interface {
	Equal(Type) bool
	ID() TypeID
	Kind() TypeKind
	Pointer() Type
	Signed() bool
}

Type represents an IR type.

The type specifier syntax is defined using Extended Backus-Naur Form (EBNF[0]):

Type		= ArrayType | FunctionType | PointerType | StructType | TypeName | UnionType .
ArrayType	= "[" "0"..."9" { "0"..."9" } "]" Type .
FunctionType	= "func" "(" [ TypeList ] [ "..." ] ")" [ Type | "(" TypeList ")" ] .
PointerType	= "*" Type .
StructType	= "struct" "{" [ FieldList ] "}" .
Fieldist	= name " " Type { "," name " " Type } .
TypeList	= Type { "," Type } .
TypeName	= "uint8" | "uint16" | "uint32" | "uint64"
		| "int8" | "int16" | "int32" | "int64"
		| "float32" | "float64" | "float128"
		| "complex64" | "complex128" | complex256
		| "uint0" | "uint8" | "uint16" | "uint32" | "uint64" .
UnionType	= "union" "{" [ FieldList ] "}" .

No whitespace is allowed in type specifiers except as the name Type separator.

[0]: https://golang.org/ref/spec#Notation

Type identity

Two types are identical if their type specifiers are equivalent.

type TypeBase

type TypeBase struct {
	TypeKind
	TypeID
}

TypeBase collects fields common to all types.

func (*TypeBase) Pointer

func (t *TypeBase) Pointer() Type

Pointer implements Type.

func (*TypeBase) String

func (t *TypeBase) String() string

String implements fmt.Stringer.

type TypeCache

type TypeCache map[TypeID]Type

TypeCache maps TypeIDs to Types. Use TypeCache{} to create a ready to use TypeCache value.

func (TypeCache) MustType

func (c TypeCache) MustType(id TypeID) Type

MustType is like Type but panics on error.

func (TypeCache) Type

func (c TypeCache) Type(id TypeID) (Type, error)

Type returns the type identified by id or an error, if any. If the cache has already a value for id, it is returned. Otherwise the type specifier denoted by id is parsed.

type TypeID

type TypeID int

TypeID is a numeric identifier of a type specifier as registered in a global dictionary[0].

[0]: https://godoc.org/github.com/cznic/xc#pkg-variables

func (TypeID) Equal

func (t TypeID) Equal(u Type) bool

Equal implements Type.

func (*TypeID) GobDecode

func (t *TypeID) GobDecode(b []byte) error

GobDecode implements GobDecoder.

func (TypeID) GobEncode

func (t TypeID) GobEncode() ([]byte, error)

GobEncode implements GobEncoder.

func (TypeID) ID

func (t TypeID) ID() TypeID

ID implements Type.

func (TypeID) Signed

func (t TypeID) Signed() bool

Signed implements Type.

func (TypeID) String

func (t TypeID) String() string

String implements fmt.Stringer.

type TypeKind

type TypeKind int

TypeKind represents a particular type kind.

const (
	Int8 TypeKind
	Int16
	Int32
	Int64

	Uint8
	Uint16
	Uint32
	Uint64

	Float32
	Float64
	Float128

	Complex64
	Complex128
	Complex256

	Array
	Union
	Struct
	Pointer
	Function
)

TypeKind values.

func (TypeKind) Kind

func (k TypeKind) Kind() TypeKind

Kind implements Type.

func (TypeKind) String

func (i TypeKind) String() string

type Value

type Value interface {
	// contains filtered or unexported methods
}

Value represents a constant expression used for initializing static data or function variables.

type Variable

type Variable struct {
	Address bool
	Index   int
	TypeID  TypeID
	token.Position
}

Variable pushes a function local variable by index, or its address, to the evaluation stack.

func (*Variable) Pos

func (o *Variable) Pos() token.Position

Pos implements Operation.

func (*Variable) String

func (o *Variable) String() string

type VariableDeclaration

type VariableDeclaration struct {
	Index    int // 0-based index within a function.
	NameID   NameID
	TypeID   TypeID
	TypeName NameID
	Value
	token.Position
}

VariableDeclaration operation declares a function local variable. NameID, TypeName and Value are all optional.

func (*VariableDeclaration) Pos

Pos implements Operation.

func (*VariableDeclaration) String

func (o *VariableDeclaration) String() string

type WideStringValue

type WideStringValue struct {
	Value []rune
	// contains filtered or unexported fields
}

WideStringValue is a declaration initializer constant of type wide string.

func (*WideStringValue) String

func (v *WideStringValue) String() string

type Xor

type Xor struct {
	TypeID TypeID // Operands type.
	token.Position
}

Xor operation replaces TOS with the bitwise xor of the top two stack items.

func (*Xor) Pos

func (o *Xor) Pos() token.Position

Pos implements Operation.

func (*Xor) String

func (o *Xor) String() string

Jump to

Keyboard shortcuts

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