Documentation ¶
Overview ¶
Package code provides functions and datastructures to encode / decode bytecode instructions.
Bytecode ¶
Constants ¶
Constants are blah di blah.
Index ¶
- type BinOp
- type Bool
- type Builder
- type ClStackOffset
- type Code
- type Constant
- type Flag
- type Float
- type Index8
- type Int
- type JumpOp
- type KIndex
- type Label
- type Lit16
- type Lit8
- type NilType
- type Offset
- type Opcode
- func AdvForLoop(rStart, rStop, rStep Reg) Opcode
- func Call(r Reg) Opcode
- func ClPush(r Reg) Opcode
- func ClTrunc(h uint16) Opcode
- func Clear(r Reg) Opcode
- func Combine(op BinOp, r1, r2, r3 Reg) Opcode
- func Cont(r1, r2 Reg) Opcode
- func FillTable(r1, r2 Reg, i int) Opcode
- func Jump(j Offset) Opcode
- func JumpIf(j Offset, r Reg) Opcode
- func JumpIfNot(j Offset, r Reg) Opcode
- func LoadBool(r Reg, b bool) Opcode
- func LoadClosure(r1 Reg, i KIndex) Opcode
- func LoadConst(r Reg, i KIndex) Opcode
- func LoadEmptyTable(r Reg) Opcode
- func LoadEtcLookup(r1, r2 Reg, i int) Opcode
- func LoadInt16(r Reg, n int16) Opcode
- func LoadLookup(r1, r2, r3 Reg) Opcode
- func LoadNil(r Reg) Opcode
- func LoadShortString(r Reg, b []byte) (Opcode, bool)
- func LoadSmallInt(r Reg, n int) (Opcode, bool)
- func LoadStr0(r Reg) Opcode
- func LoadStr1(r Reg, b []byte) Opcode
- func LoadStr2(r Reg, b []byte) Opcode
- func PrepForLoop(rStart, rStop, rStep Reg) Opcode
- func Push(r1, r2 Reg) Opcode
- func PushEtc(r1, r2 Reg) Opcode
- func Receive(r Reg) Opcode
- func ReceiveEtc(r Reg) Opcode
- func SetIndex(r1, r2, r3 Reg) Opcode
- func TailCall(r Reg) Opcode
- func TailCont(r1, r2 Reg) Opcode
- func Transform(op UnOp, r1, r2 Reg) Opcode
- func Upval(r1, r2 Reg) Opcode
- func (c Opcode) Disassemble(d OpcodeDisassembler, i int) string
- func (c Opcode) GetA() Reg
- func (c Opcode) GetB() Reg
- func (c Opcode) GetC() Reg
- func (c Opcode) GetClStackOffset() ClStackOffset
- func (c Opcode) GetF() bool
- func (c Opcode) GetJ() JumpOp
- func (c Opcode) GetKIndex() KIndex
- func (c Opcode) GetL() Lit8
- func (c Opcode) GetM() Index8
- func (c Opcode) GetN() Lit16
- func (c Opcode) GetOffset() Offset
- func (c Opcode) GetUnOp() UnOp
- func (c Opcode) GetUnOpK() UnOpK
- func (c Opcode) GetX() BinOp
- func (c Opcode) GetY() UnOpK16
- func (c Opcode) HasType0() bool
- func (c Opcode) HasType1() bool
- func (c Opcode) HasType4a() bool
- func (c Opcode) SetKIndex(i KIndex) Opcode
- func (c Opcode) SetOffset(n Offset) Opcode
- func (c Opcode) TypePfx() Opcode
- type OpcodeDisassembler
- type Reg
- type RegType
- type String
- type UnOp
- type UnOpK
- type UnOpK16
- type Unit
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Bool ¶
type Bool bool
A Bool is a boolean literal.
func (Bool) ShortString ¶
ShortString returns a short string describing this constant (e.g. for disassembly)
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
A Builder helps build a code Unit (in particular it calculates the offsets for jump instructions).
func NewBuilder ¶
NewBuilder returns an empty Builder for the given source.
func (*Builder) AddConstant ¶
AddConstant adds a constant.
func (*Builder) EmitJump ¶
EmitJump adds a jump opcode, jumping to the given label. The offset part of the opcode must be left as 0, it will be filled by the builder when the location of the label is known.
type ClStackOffset ¶
type ClStackOffset uint16
ClStackOffset is an offset from the bottom of the close stack
type Code ¶
type Code struct { Name string // Name of the function (if it has one) StartOffset, EndOffset uint // Where to find the opcode in the code Unit this belongs to UpvalueCount int16 // Number of upvalues CellCount int16 // Number of cell registers needed to run the code RegCount int16 // Number of registers needed to run the coee UpNames []string // Names of the upvalues }
Code is a constant representing a chunk of code. It doesn't contain any actual opcodes, but refers to a range in the code unit it belongs to.
func (Code) GetSpan ¶
GetSpan returns the name of the function this code is compiled from, and the range of instructions in the code Unit this code belongs to.
func (Code) ShortString ¶
ShortString returns a short string describing this constant (e.g. for disassembly)
type Constant ¶
type Constant interface {
ShortString() string
}
A Constant is a literal that can be loaded into a register (that include code chunks).
type Flag ¶
type Flag uint8
Flag is an On / Off switch that can be used in several types of opcodes.
type Float ¶
type Float float64
A Float is a floating point literal.
func (Float) ShortString ¶
ShortString returns a short string describing this constant (e.g. for disassembly)
type Index8 ¶
type Index8 uint8
Index8 is an 8 bit index (0 - 255).
func Index8FromInt ¶
Index8FromInt returns an Index8 encoding the given int, which must fit in an uint8.
type Int ¶
type Int int64
An Int is an integer literal.
func (Int) ShortString ¶
ShortString returns a short string describing this constant (e.g. for disassembly)
type KIndex ¶
type KIndex uint16
KIndex is an index into the constants table
func KIndexFromInt ¶
KIndexFromInt returns a KIndex encoding the given index i, panicking if out of range.
type Lit16 ¶
type Lit16 uint16
Lit16 is a 16 bit literal used in several opcode types, used to represent constant values, offsets or indexes into the constants table.
func Lit16FromInt16 ¶
Lit16FromInt16 converts an int16 into a Lit16.
func Lit16FromStr2 ¶
Lit16FromStr2 converts a slice into a Lit16. The slice must have length 2.
type Lit8 ¶
type Lit8 uint8
Lit8 is an 8 bit literal that can encode different types of constants inline.
func Lit8FromStr1 ¶
Lit8FromStr1 encodes a Lit8 from a byte string of length 1. Panics if b has length 0.
type NilType ¶
type NilType struct{}
NilType is the type of the nil literal.
func (NilType) ShortString ¶
ShortString returns a short string describing this constant (e.g. for disassembly)
type Opcode ¶
type Opcode uint32
Opcode is the type of opcodes
const ( Type1Pfx Opcode = 1 << 31 // 1...... Type2Pfx Opcode = 7 << 28 // 0111... Type3Pfx Opcode = 6 << 28 // 0110... Type4Pfx Opcode = 5 << 28 // 0101... Type5Pfx Opcode = 4 << 28 // 0100... Type6Pfx Opcode = 3 << 28 // 0011... Type7Pfx Opcode = 2 << 28 // 0010... Type0Pfx Opcode = 0 << 28 // 0000... )
Prefixes for the different types of opcodes. Note: there is an unused prefix (0001).
func AdvForLoop ¶
AdvForLoop increments rStart by rStep, making sure that it doesn't wrap around if it is an integer. If it wraps around then the loop should stop. If the loop should stop, rStart is set to nil
func ClPush ¶
ClPush encodes clpush r
r should contain either nil, false, or a value with a "__close" metamethod. This opcode is introduced to support Lua 5.4's "to-be-closed" variables.
func ClTrunc ¶
ClTrunc encodes cltrunc h
It truncates the close stack to the given height. Each value on the close stack which is removed should either be nil or false, or be a value with a "__close" metamethod, in which case this metamethod is called. This opcode is introduced to support Lua 5.4's "to-be-closed" variables.
func Clear ¶
Clear encodes clear r
This clears the register. If the register contains a cell, the cell is removed, so this is different from r <- nil
func Cont ¶
Cont encodes r1 <- cont(r2)
r2 must contain a closure, r1 then contains a new continuation for that closure, whose next continuation is the cc.
func FillTable ¶
FillTable encodes fill r1, i, r2
This fills the table r1 with all values from r2 (as an etc vector) starting from index i.
func LoadEtcLookup ¶
LoadEtcLookup encodes r1 <- etclookup(r2, i)
loads the (i + 1)-th element of r2 (as an etc vector) into r1.
func LoadShortString ¶
LoadShortString attempts to encode loading a short string. Returns the opcode and true if it did.
func LoadSmallInt ¶
LoadSmallInt attempts to load a small integer (atm it has to be representable as an int16).
func PrepForLoop ¶
PrepForLoop makes sure rStart, rStep, rStop are all numbers and converts rStart and rStep to the same numeric type. If the for loop should already stop then rStart is set to nil
func TailCall ¶
TailCall encodes tailcall r
r must contain a continuation that is ready to be called.
func TailCont ¶
TailCont encodes r1 <- tailcont(r2)
r2 must contain a closure, r1 then contains a new continuatino for that closure, whose next continuation is the cc's next continuation.
func Upval ¶
Upval encodes upval r1, r2
r1 must contain a closure. This appends the value of r2 to the list of upvalues of r1.
func (Opcode) Disassemble ¶
func (c Opcode) Disassemble(d OpcodeDisassembler, i int) string
Disassemble gets a human readable representation of an opcode.
func (Opcode) GetClStackOffset ¶
func (c Opcode) GetClStackOffset() ClStackOffset
func (Opcode) HasType4a ¶
HasType4a returns true if the opcode is Type4a, assuming that it is Type4.
type OpcodeDisassembler ¶
type OpcodeDisassembler interface { ShortKString(KIndex) string // Gets a string representation of a constant GetLabel(int) string // Gets a constistent label name for a code offset }
OpcodeDisassembler is an interface that helps disassemble an opcode.
type RegType ¶
type RegType uint8
RegType represents the type of a register
const ( ValueRegType RegType = 0 CellRegType = 1 )
ValueRegType types
type String ¶
type String string
A String is a string literal.
func (String) ShortString ¶
ShortString returns a short string describing this constant (e.g. for disassembly)
type UnOp ¶
type UnOp uint8
UnOp is the type of operators available in Type4a opcodes.
const ( OpNeg UnOp = iota // numerical negation OpBitNot // bitwise negation OpLen // length OpCont // make a continuation for the closure OpTailCont // make a "tail continuation" for the closure (its next is cc's next) OpId // identity OpTruth // Turn operand to boolean OpNot // Added afterwards - why did I not have it in the first place? OpUpvalue // get an upvalue OpEtcId // etc identity )
Available unary operators
type UnOpK16 ¶
type UnOpK16 uint8
UnOpK16 is the type of operator available in Type3 opcodes.
Here is the list of available UnOpK16 operators.
type Unit ¶
type Unit struct { Source string // Shows were the unit comes from (e.g. a filename) - only for information. Code []Opcode // The code Lines []int32 // Optional: source code line for the corresponding opcode Constants []Constant // All the constants required for running the code }
A Unit is a chunk of code with associated constants.
func (*Unit) Disassemble ¶
Disassemble outputs the disassembly of the unit code into the given io.Writer.