ir

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EmitConstant

func EmitConstant(c *CodeBuilder, k Constant, reg Register, line int)

func EmitMove

func EmitMove(c *CodeBuilder, dst Register, src Register, line int)

func EmitMoveNoLine

func EmitMoveNoLine(c *CodeBuilder, dst Register, src Register)

func FoldMoveReg

func FoldMoveReg(i1, i2 Instruction, regs []RegData) (Instruction, Instruction)

FoldMoveReg folds the following

(r1 <- X; r2 <- r1) ==> r2 <- X

func FoldTakeRelease

func FoldTakeRelease(i1, i2 Instruction, regs []RegData) (Instruction, Instruction)

FoldTakeRelease folds the following

(take r1; release r1) ==> ()

Types

type AdvForLoop

type AdvForLoop struct {
	Start, Stop, Step Register
}

func (AdvForLoop) ProcessInstr

func (i AdvForLoop) ProcessInstr(p InstrProcessor)

func (AdvForLoop) String

func (i AdvForLoop) String() string

type Bool

type Bool bool

A Bool is a constant representing a boolean literal.

func (Bool) ProcessConstant

func (b Bool) ProcessConstant(p ConstantProcessor)

ProcessConstant uses the given ConstantProcessor to process the receiver.

type Call

type Call struct {
	Cont Register
	Tail bool
}

Call moves execution to the given continuation

func (Call) ProcessInstr

func (c Call) ProcessInstr(p InstrProcessor)

ProcessInstr makes the InstrProcessor process this instruction.

func (Call) String

func (c Call) String() string

type ClearReg

type ClearReg struct {
	Dst Register
}

ClearReg resets the given register to nil (if it contained a cell, this cell is removed).

func (ClearReg) ProcessInstr

func (i ClearReg) ProcessInstr(p InstrProcessor)

ProcessInstr makes the InstrProcessor process this instruction.

func (ClearReg) String

func (i ClearReg) String() string

type Code

type Code struct {
	Instructions []Instruction
	Lines        []int
	Constants    []Constant
	UpvalueDests []Register
	Registers    []RegData
	UpNames      []string
	Name         string
}

Code is the type of code literals (i.e. function definitions).

func FoldCode

func FoldCode(c Code, f FoldFunc) Code

FoldCode uses the given FoldFunc to fold the instructions in the given code. Returns a new Code instande with folded code.

func (Code) ProcessConstant

func (c Code) ProcessConstant(p ConstantProcessor)

ProcessConstant uses the given ConstantProcessor to process the receiver.

type CodeBuilder

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

func NewCodeBuilder

func NewCodeBuilder(chunkName string, constantPool *ConstantPool) *CodeBuilder

func (*CodeBuilder) Close

func (c *CodeBuilder) Close() (uint, []Register)

func (*CodeBuilder) DeclareGotoLabel

func (c *CodeBuilder) DeclareGotoLabel(name Name, line int) Label

func (*CodeBuilder) DeclareGotoLabelNoLine

func (c *CodeBuilder) DeclareGotoLabelNoLine(name Name) Label

func (*CodeBuilder) DeclareLocal

func (c *CodeBuilder) DeclareLocal(name Name, reg Register)

func (*CodeBuilder) DeclareUniqueGotoLabel

func (c *CodeBuilder) DeclareUniqueGotoLabel(name Name, line int) (Label, error)

func (*CodeBuilder) Dump

func (c *CodeBuilder) Dump()

func (*CodeBuilder) Emit

func (c *CodeBuilder) Emit(instr Instruction, line int)

func (*CodeBuilder) EmitGotoLabel

func (c *CodeBuilder) EmitGotoLabel(name Name) error

func (*CodeBuilder) EmitJump

func (c *CodeBuilder) EmitJump(lblName Name, line int) bool

func (*CodeBuilder) EmitLabel

func (c *CodeBuilder) EmitLabel(lbl Label, line int) error

func (*CodeBuilder) EmitLabelNoLine

func (c *CodeBuilder) EmitLabelNoLine(lbl Label) error

func (*CodeBuilder) EmitNoLine

func (c *CodeBuilder) EmitNoLine(instr Instruction)

func (*CodeBuilder) GetFreeRegister

func (c *CodeBuilder) GetFreeRegister() Register

func (*CodeBuilder) GetNewLabel

func (c *CodeBuilder) GetNewLabel() Label

func (*CodeBuilder) GetRegister

func (c *CodeBuilder) GetRegister(name Name) (Register, bool)

func (*CodeBuilder) HasPendingCloseActions

func (c *CodeBuilder) HasPendingCloseActions() bool

HasPendingCloseActions returns true if there are close actions in the current context. In this case tail calls are disabled in order to allow the close actions to take place after the call.

func (*CodeBuilder) IsConstantReg

func (c *CodeBuilder) IsConstantReg(reg Register) bool

func (*CodeBuilder) MarkConstantReg

func (c *CodeBuilder) MarkConstantReg(reg Register)

func (*CodeBuilder) NewChild

func (c *CodeBuilder) NewChild(chunkName string) *CodeBuilder

func (*CodeBuilder) PopContext

func (c *CodeBuilder) PopContext()

func (*CodeBuilder) PushCloseAction

func (c *CodeBuilder) PushCloseAction(reg Register)

PushCloseAction emits a PushCloseStack instruction and updates the current lexical context accordingly

func (*CodeBuilder) PushContext

func (c *CodeBuilder) PushContext()

func (*CodeBuilder) ReleaseRegister

func (c *CodeBuilder) ReleaseRegister(reg Register)

func (*CodeBuilder) TakeRegister

func (c *CodeBuilder) TakeRegister(reg Register)

type Combine

type Combine struct {
	Op   ops.Op   // Operator to apply to Lsrc and Rsrc
	Dst  Register // Destination register
	Lsrc Register // Left operand register
	Rsrc Register // Right operand register
}

Combine applies the binary operator Op to Lsrc and Rsrc and stores the result in Dst.

func (Combine) DestReg

func (c Combine) DestReg() Register

DestReg returns the destination register of this instruction.

func (Combine) ProcessInstr

func (c Combine) ProcessInstr(p InstrProcessor)

ProcessInstr makes the InstrProcessor process this instruction.

func (Combine) String

func (c Combine) String() string

func (Combine) WithDestReg

func (c Combine) WithDestReg(r Register) Instruction

WithDestReg returns the same isntruction with a new destination register.

type Constant

type Constant interface {
	ProcessConstant(p ConstantProcessor)
}

Constant is the interface implemented by constant values (e.g. numbers, strings, but also code chunks).

func FoldConstants

func FoldConstants(consts []Constant, f FoldFunc) []Constant

FoldConstants uses the given FoldFunc to fold the code items in the given constant slice.

type ConstantPool

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

A ConstantPool accumulates constants and associates a stable integer with each constant.

func NewConstantPool

func NewConstantPool() *ConstantPool

func (*ConstantPool) Constants

func (c *ConstantPool) Constants() []Constant

Constants returns the list of all constants registered with this pool.

func (*ConstantPool) GetConstantIndex

func (c *ConstantPool) GetConstantIndex(k Constant) uint

GetConstantIndex returns the index associated with a given constant. If there is none, the constant is registered in the pool and the new index is returned.

type ConstantProcessor

type ConstantProcessor interface {
	ProcessFloat(Float)
	ProcessInt(Int)
	ProcessBool(Bool)
	ProcessString(String)
	ProcessNil(NilType)
	ProcessCode(Code)
}

A ConstantProcessor is able to process all the different types of constant using the various ProcessXXX methods.

type DeclareLabel

type DeclareLabel struct {
	Label Label
}

DeclareLabel is not a real IR instruction. It is a placeholder for the destination location of a jump label. Being an instruction makes it easier to perform IR code transformations and keep the labels in correct locations.

func (DeclareLabel) ProcessInstr

func (l DeclareLabel) ProcessInstr(p InstrProcessor)

ProcessInstr makes the InstrProcessor process this instruction.

func (DeclareLabel) String

func (l DeclareLabel) String() string

type EtcLookup

type EtcLookup struct {
	Etc Register
	Dst Register
	Idx int
}

EtcLookup finds the value at index Idx in the Etc register and puts it in Dst.

func (EtcLookup) DestReg

func (l EtcLookup) DestReg() Register

DestReg returns the destination register of this instruction.

func (EtcLookup) ProcessInstr

func (l EtcLookup) ProcessInstr(p InstrProcessor)

ProcessInstr makes the InstrProcessor process this instruction.

func (EtcLookup) String

func (l EtcLookup) String() string

func (EtcLookup) WithDestReg

func (l EtcLookup) WithDestReg(r Register) Instruction

WithDestReg returns the same isntruction with a new destination register.

type FillTable

type FillTable struct {
	Etc Register
	Dst Register
	Idx int
}

FillTable fills Dst (which must contain a table) with the contents of Etc (which must be an etc value) starting from the given index.

func (FillTable) ProcessInstr

func (f FillTable) ProcessInstr(p InstrProcessor)

ProcessInstr makes the InstrProcessor process this instruction.

func (FillTable) String

func (f FillTable) String() string

type Float

type Float float64

A Float is a constant representing a literal floating point number.

func (Float) ProcessConstant

func (f Float) ProcessConstant(p ConstantProcessor)

ProcessConstant uses the given ConstantProcessor to process the receiver.

type FoldFunc

type FoldFunc func(i1, i2 Instruction, regs []RegData) (Instruction, Instruction)

A FoldFunc can turn 2 instructions into fewer instructions (in which case some of the returned instructions are nil).

var DefaultFold FoldFunc = nil

DefaultFold applies a few simple folds

func ComposeFolds

func ComposeFolds(fs ...FoldFunc) FoldFunc

ComposeFolds takes several fold functions are composes them into one single function applying all the folds.

type InstrProcessor

type InstrProcessor interface {

	// Real instructions
	ProcessCombineInstr(Combine)
	ProcessTransformInstr(Transform)
	ProcessLoadConstInstr(LoadConst)
	ProcessPushInstr(Push)
	ProcessJumpInstr(Jump)
	ProcessJumpIfInstr(JumpIf)
	ProcessCallInstr(Call)
	ProcessMkClosureInstr(MkClosure)
	ProcessMkContInstr(MkCont)
	ProcessClearRegInstr(ClearReg)
	ProcessMkTableInstr(MkTable)
	ProcessLookupInstr(Lookup)
	ProcessSetIndexInstr(SetIndex)
	ProcessReceiveInstr(Receive)
	ProcessReceiveEtcInstr(ReceiveEtc)
	ProcessEtcLookupInstr(EtcLookup)
	ProcessFillTableInstr(FillTable)
	ProcessTruncateCloseStackInstr(TruncateCloseStack)
	ProcessPushCloseStackInstr(PushCloseStack)
	ProcessPrepForLoopInstr(PrepForLoop)
	ProcessAdvForLoopInstr(AdvForLoop)

	// These are hints that a register is needed or no longer needed.
	ProcessTakeRegisterInstr(TakeRegister)
	ProcessReleaseRegisterInstr(ReleaseRegister)

	// A label (for jumping to)
	ProcessDeclareLabelInstr(DeclareLabel)
}

An InstrProcessor can process any instruction.

type Instruction

type Instruction interface {
	fmt.Stringer
	ProcessInstr(InstrProcessor)
}

Instruction is the interface that all ir instruction types must implement.

type Int

type Int int64

An Int is a constant representing an integer literal.

func (Int) ProcessConstant

func (n Int) ProcessConstant(p ConstantProcessor)

ProcessConstant uses the given ConstantProcessor to process the receiver.

type Jump

type Jump struct {
	Label Label
}

Jump jumps to the givel label.

func (Jump) ProcessInstr

func (j Jump) ProcessInstr(p InstrProcessor)

ProcessInstr makes the InstrProcessor process this instruction.

func (Jump) String

func (j Jump) String() string

type JumpIf

type JumpIf struct {
	Cond  Register
	Label Label
	Not   bool
}

JumpIf jumps to the given label if the boolean value in Cond is different from Not.

func (JumpIf) ProcessInstr

func (j JumpIf) ProcessInstr(p InstrProcessor)

ProcessInstr makes the InstrProcessor process this instruction.

func (JumpIf) String

func (j JumpIf) String() string

type Label

type Label uint

A Label is a location in the IR code.

func (Label) String

func (l Label) String() string

type LoadConst

type LoadConst struct {
	Dst  Register // Destination register
	Kidx uint     // Index of the constant to load
}

LoadConst loads a constant into a register.

func (LoadConst) DestReg

func (l LoadConst) DestReg() Register

DestReg returns the destination register of this instruction.

func (LoadConst) ProcessInstr

func (l LoadConst) ProcessInstr(p InstrProcessor)

ProcessInstr makes the InstrProcessor process this instruction.

func (LoadConst) String

func (l LoadConst) String() string

func (LoadConst) WithDestReg

func (l LoadConst) WithDestReg(r Register) Instruction

WithDestReg returns the same isntruction with a new destination register.

type Lookup

type Lookup struct {
	Dst   Register
	Table Register
	Index Register
}

Lookup finds the value associated with the key Index in Table and puts it in Dst.

func (Lookup) DestReg

func (s Lookup) DestReg() Register

DestReg returns the destination register of this instruction.

func (Lookup) ProcessInstr

func (s Lookup) ProcessInstr(p InstrProcessor)

ProcessInstr makes the InstrProcessor process this instruction.

func (Lookup) String

func (s Lookup) String() string

func (Lookup) WithDestReg

func (s Lookup) WithDestReg(r Register) Instruction

WithDestReg returns the same isntruction with a new destination register.

type MkClosure

type MkClosure struct {
	Dst      Register
	Code     uint
	Upvalues []Register
}

MkClosure creates a new closure with the given code and upvalues and puts it in Dst.

func (MkClosure) DestReg

func (m MkClosure) DestReg() Register

DestReg returns the destination register of this instruction.

func (MkClosure) ProcessInstr

func (m MkClosure) ProcessInstr(p InstrProcessor)

ProcessInstr makes the InstrProcessor process this instruction.

func (MkClosure) String

func (m MkClosure) String() string

func (MkClosure) WithDestReg

func (m MkClosure) WithDestReg(r Register) Instruction

WithDestReg returns the same isntruction with a new destination register.

type MkCont

type MkCont struct {
	Dst     Register
	Closure Register
	Tail    bool
}

MkCont creates a new continuation for the given closure and puts it in Dst.

func (MkCont) DestReg

func (m MkCont) DestReg() Register

DestReg returns the destination register of this instruction.

func (MkCont) ProcessInstr

func (m MkCont) ProcessInstr(p InstrProcessor)

ProcessInstr makes the InstrProcessor process this instruction.

func (MkCont) String

func (m MkCont) String() string

func (MkCont) WithDestReg

func (m MkCont) WithDestReg(r Register) Instruction

WithDestReg returns the same isntruction with a new destination register.

type MkTable

type MkTable struct {
	Dst Register
}

MkTable creates a new empty table and puts it i Dst.

func (MkTable) ProcessInstr

func (m MkTable) ProcessInstr(p InstrProcessor)

ProcessInstr makes the InstrProcessor process this instruction.

func (MkTable) String

func (m MkTable) String() string

type Name

type Name string

type NilType

type NilType struct{}

NilType is the type of the nil literal.

func (NilType) ProcessConstant

func (n NilType) ProcessConstant(p ConstantProcessor)

ProcessConstant uses the given ConstantProcessor to process the receiver.

type PrepForLoop

type PrepForLoop struct {
	Start, Stop, Step Register
}

PrepForLoop prepares a for loop

func (PrepForLoop) ProcessInstr

func (i PrepForLoop) ProcessInstr(p InstrProcessor)

func (PrepForLoop) String

func (i PrepForLoop) String() string

type Push

type Push struct {
	Cont Register // Destination register (should contain a continuation)
	Item Register // Register containing item to push
	Etc  bool     // True if the Item is an etc value.
}

Push pushes the contents of a register into a continuation.

func (Push) ProcessInstr

func (p Push) ProcessInstr(ip InstrProcessor)

ProcessInstr makes the InstrProcessor process this instruction.

func (Push) String

func (p Push) String() string

type PushCloseStack

type PushCloseStack struct {
	Src Register
}

PushCloseStack truncates pushes the value Src to the close stack. This instruction was introduced to support to-be-closed variables which are part of Lua 5.4

func (PushCloseStack) ProcessInstr

func (i PushCloseStack) ProcessInstr(p InstrProcessor)

ProcessInstr makes the InstrProcessor process this instruction.

func (PushCloseStack) String

func (i PushCloseStack) String() string

type Receive

type Receive struct {
	Dst []Register
}

Receive will put the result of pushes in the given registers.

func (Receive) ProcessInstr

func (r Receive) ProcessInstr(p InstrProcessor)

ProcessInstr makes the InstrProcessor process this instruction.

func (Receive) String

func (r Receive) String() string

type ReceiveEtc

type ReceiveEtc struct {
	Dst []Register
	Etc Register
}

ReceiveEtc will put the result of pushes into the given registers. Extra pushes will be accumulated into the Etc register.

func (ReceiveEtc) ProcessInstr

func (r ReceiveEtc) ProcessInstr(p InstrProcessor)

ProcessInstr makes the InstrProcessor process this instruction.

func (ReceiveEtc) String

func (r ReceiveEtc) String() string

type RegData

type RegData struct {
	IsCell     bool
	IsConstant bool
	// contains filtered or unexported fields
}

type Register

type Register uint

A Register is an IR register. The number of IR registers is not bounded (other than the bounds of the underling type).

func (Register) String

func (r Register) String() string

type ReleaseRegister

type ReleaseRegister struct {
	Reg Register
}

ReleaseRegister is not a real instruction. It is a hint to the next stage that the value in this register no longer needs to be used, so can be overwritten. It should be preceded by a TakeRegister for the same register.

func (ReleaseRegister) ProcessInstr

func (r ReleaseRegister) ProcessInstr(p InstrProcessor)

ProcessInstr makes the InstrProcessor process this instruction.

func (ReleaseRegister) String

func (r ReleaseRegister) String() string

type SetIndex

type SetIndex struct {
	Table Register
	Index Register
	Src   Register
}

SetIndex associates Index with Src in the table Table.

func (SetIndex) ProcessInstr

func (s SetIndex) ProcessInstr(p InstrProcessor)

ProcessInstr makes the InstrProcessor process this instruction.

func (SetIndex) String

func (s SetIndex) String() string

type SetRegInstruction

type SetRegInstruction interface {
	DestReg() Register
	WithDestReg(Register) Instruction
}

SetRegInstruction is the interface that instructions that set the value of a register must implement.

type String

type String string

A String is a constant representing a string literal.

func (String) ProcessConstant

func (s String) ProcessConstant(p ConstantProcessor)

ProcessConstant uses the given ConstantProcessor to process the receiver.

type TakeRegister

type TakeRegister struct {
	Reg Register
}

TakeRegister is not a real instruction. It is a hint to the next stage that the value in this register will need to be used, so not to overwrite it. A ReleaseRegister for the same register should be emitted some time later.

func (TakeRegister) ProcessInstr

func (t TakeRegister) ProcessInstr(p InstrProcessor)

ProcessInstr makes the InstrProcessor process this instruction.

func (TakeRegister) String

func (t TakeRegister) String() string

type Transform

type Transform struct {
	Op  ops.Op   // Operator to apply to Src
	Dst Register // Destination register
	Src Register // Operand register
}

Transform applies a unary operator Op to Src and stores the result in Dst.

func (Transform) DestReg

func (t Transform) DestReg() Register

DestReg returns the destination register of this instruction.

func (Transform) ProcessInstr

func (t Transform) ProcessInstr(p InstrProcessor)

ProcessInstr makes the InstrProcessor process this instruction.

func (Transform) String

func (t Transform) String() string

func (Transform) WithDestReg

func (t Transform) WithDestReg(r Register) Instruction

WithDestReg returns the same isntruction with a new destination register.

type TruncateCloseStack

type TruncateCloseStack struct {
	Height int
}

TruncateCloseStack truncates the close stack to Height (which should be >= 0). This instruction was introduced to support to-be-closed variables which are part of Lua 5.4

func (TruncateCloseStack) ProcessInstr

func (t TruncateCloseStack) ProcessInstr(p InstrProcessor)

ProcessInstr makes the InstrProcessor process this instruction.

func (TruncateCloseStack) String

func (t TruncateCloseStack) String() string

Jump to

Keyboard shortcuts

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