instructions

package
v0.0.0-...-56c637b Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInstructionNotImplemented = errors.New("instruction not implemented")
View Source
var ErrInvalidInstruction = errors.New("invalid instruction")
View Source
var ErrInvalidOpCode error = errors.New("invalid instruction opcode")

Functions

func LLVMImmediateType

func LLVMImmediateType(valueType types.ValueType) string

Returns the LLVM type name of a given immediate value type

func LLVMOperandPattern

func LLVMOperandPattern(op *OperandDescriptor) string

Returns the operand pattern for a LLVM instruction definition

func LLVMOperandType

func LLVMOperandType(op *OperandDescriptor) string

Returns the operand type for a LLVM instruction definition

func LLVMType

func LLVMType(valueType types.ValueType) string

Returns the LLVM type name of a given value type

Types

type Instruction

type Instruction struct {
	Descriptor    *InstructionDescriptor
	OperandValues []OperandValue
}

Stores a fully decoded instruction

func (*Instruction) Raw

func (i *Instruction) Raw() RawInstruction

func (*Instruction) String

func (i *Instruction) String() string

type InstructionDescriptor

type InstructionDescriptor struct {
	// Instruction opcode
	OpCode *OpCodeDescriptor
	// Instruction operands
	Operands []*OperandDescriptor
	// Instruction description (for documentation and debugging)
	Description string

	// LLVM instruction selection pattern template
	LLVM_PatternTemplate string
	// Flags controlling high level semantics of the instruction in LLVM instruction definition. See
	// class Instruction definition bit flags in LLVM's source llvm/include/Target/Target.td
	LLVM_InstructionFlags LLVMInstructionFlags
	// Set of non operand registers that are implicitly modified by the instruction
	LLVM_Defs []*registers.RegisterDescriptor
	// Set of non operand registers that are implicitly read by the instruction
	LLVM_Uses []*registers.RegisterDescriptor
	// LLVM instruction definition metadata
	LLVM *LLVMInstructionDescriptor
}

Contains information describing an instruction

func Add

func Add() *InstructionDescriptor

func Cmp

func Cmp() *InstructionDescriptor

func Div

func Div() *InstructionDescriptor

func Ld

func Mod

func Mod() *InstructionDescriptor

func Mov

func Mov() *InstructionDescriptor

func MovImm16H

func MovImm16H() *InstructionDescriptor

func MovImm16L

func MovImm16L() *InstructionDescriptor

func Mul

func Mul() *InstructionDescriptor

func Nop

func Nop() *InstructionDescriptor

func St

func Sub

func Sub() *InstructionDescriptor

func (*InstructionDescriptor) Documentation

func (d *InstructionDescriptor) Documentation(leftpad int) string

Returns full documentation for the instruction

func (*InstructionDescriptor) InstructionBits

func (d *InstructionDescriptor) InstructionBits() int

Returns the minimum bits required to encode the instruction

func (*InstructionDescriptor) String

func (d *InstructionDescriptor) String() string

Returns a human readable string representation of the instruction

type InstructionsDescriptor

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

Constains information about all implemented instructions

func NewInstructionsDescriptor

func NewInstructionsDescriptor(instructions []*InstructionDescriptor) InstructionsDescriptor

Initializes an instructions descriptor with all the given instructions

func (*InstructionsDescriptor) AllInstructions

func (d *InstructionsDescriptor) AllInstructions() []*InstructionDescriptor

Returns all implemented instructions

func (*InstructionsDescriptor) Decode

func (d *InstructionsDescriptor) Decode(binaryRepresentation uint32) (*Instruction, error)

Decode an instruction

func (*InstructionsDescriptor) Instruction

Returns the instruction corresponding to the given opcode

func (*InstructionsDescriptor) InstructionBits

func (d *InstructionsDescriptor) InstructionBits() int

Returns the number of bits required to encode a machine instruction

func (*InstructionsDescriptor) InstructionBytes

func (d *InstructionsDescriptor) InstructionBytes() int

Returns the number of bytes required to encode a machine instruction

type LLVMInstructionDescriptor

type LLVMInstructionDescriptor struct {
	Instruction *InstructionDescriptor
	Operands    []*LLVMInstructionOperandDescriptor
	Pattern     string
	Flags       map[LLVMInstructionFlags]bool
}

Contains metadata to generate LLVM instruction definitions

func NewLLVMInstructionDescriptor

func NewLLVMInstructionDescriptor(i *InstructionDescriptor) *LLVMInstructionDescriptor

Generates a descriptor for LLVM instruction generation from a cucaracha instruction descriptor

func (*LLVMInstructionDescriptor) Ins

func (d *LLVMInstructionDescriptor) Ins() []string

Returns the set of input operands declarations

func (*LLVMInstructionDescriptor) Outs

func (d *LLVMInstructionDescriptor) Outs() []string

Returns the set of output operands declarations

func (*LLVMInstructionDescriptor) Params

func (d *LLVMInstructionDescriptor) Params() []string

Returns all instruction operands in order in parameter form, that is, a $ followed by the operand name

type LLVMInstructionFlags

type LLVMInstructionFlags uint

Represents flags in the LLVM tablegen Instruction class that control the high level semantics of an instruction See class Instruction in LLVM's source code llvm/include/llvm/Target/Target.td for details

const (
	LLVMInstructionFlags_IsReturn LLVMInstructionFlags = 1 << iota
	LLVMInstructionFlags_IsBranch
	LLVMInstructionFlags_IsCompare
	LLVMInstructionFlags_IsMoveImm
	LLVMInstructionFlags_IsMoveReg
	LLVMInstructionFlags_IsBitcast
	LLVMInstructionFlags_IsCall
	LLVMInstructionFlags_MayLoad
	LLVMInstructionFlags_MayStore
	LLVMInstructionFlags_IsPseudo
)

func (LLVMInstructionFlags) Flags

Returns all flags with a corresponding boolean stating if the flag is set or not

func (LLVMInstructionFlags) FlagsSet

Returns all the flags enabled

func (LLVMInstructionFlags) String

func (f LLVMInstructionFlags) String() string

type LLVMInstructionOperandDescriptor

type LLVMInstructionOperandDescriptor struct {
	// Operand descriptor
	Operand *OperandDescriptor
	// Operand type
	Type string
	// Operand name
	Name string
	// Operand pattern in the DAG selection pattern of the instruction
	Pattern string
}

Contains metadata to generate correct operand descriptions for LLVM instruction definition

func (*LLVMInstructionOperandDescriptor) Bits

Returns the number of bits used to encode the operand

func (*LLVMInstructionOperandDescriptor) LeastSignificantBit

func (d *LLVMInstructionOperandDescriptor) LeastSignificantBit() int

Returns the least significant bit within the instruction used to encode the operand

func (*LLVMInstructionOperandDescriptor) MostSignificantBit

func (d *LLVMInstructionOperandDescriptor) MostSignificantBit() int

Returns the most significant bit within the instruction used to encode the operand

func (*LLVMInstructionOperandDescriptor) Param

Returns the operand paramter string, that is, the operand name prefixed with a $

func (*LLVMInstructionOperandDescriptor) ParamDeclaration

func (d *LLVMInstructionOperandDescriptor) ParamDeclaration() string

Returns the full operand paramter declaration, that is, the operand type followed by the operand name prefixed with a $

func (*LLVMInstructionOperandDescriptor) ParamPattern

func (d *LLVMInstructionOperandDescriptor) ParamPattern() string

Returns the full operand pattern declaration, that is, the operand pattern followed by the operand name prefixed with a $

type OpCode

type OpCode uint

Represents an instruction opcode

const (
	// No-Operation
	OpCode_NOP OpCode = iota
	// Copy 16 most significant bits of immediate value into register
	OpCode_MOV_IMM16H
	// Copy 16 least significant bits of immediate value into register
	OpCode_MOV_IMM16L
	// Copy value of one register into another
	OpCode_MOV
	// Load value from memory into register
	OpCode_LD
	// Save value of register into memory
	OpCode_ST
	// Add values of two registers, save result into third
	OpCode_ADD
	// Substract values of two registers, save result into third
	OpCode_SUB
	// Multiply values of two registers, save result into third
	OpCode_MUL
	// Divide values of two registers, save result into third
	OpCode_DIV
	// Compute register value modulo other register value, save result into third
	OpCode_MOD

	// Total opcodes implemented
	TOTAL_OPCODES
)

func (OpCode) String

func (op OpCode) String() string

Returns the mnemonic of the instruction opcode

type OpCodeDescriptor

type OpCodeDescriptor struct {
	OpCode               OpCode
	BinaryRepresentation uint64
	Mnemonic             string
}

Contains implementation information of an instruction opcode

func (*OpCodeDescriptor) EncodingBits

func (d *OpCodeDescriptor) EncodingBits() int

Returns the number of bits used to encode an instruction opcode

func (*OpCodeDescriptor) EncodingPosition

func (d *OpCodeDescriptor) EncodingPosition() int

Returns the first bit within an instruction used to encode the opcode

func (*OpCodeDescriptor) LeastSignificantBit

func (d *OpCodeDescriptor) LeastSignificantBit() int

Same as EncodingPosition()

func (*OpCodeDescriptor) MostSignificantBit

func (d *OpCodeDescriptor) MostSignificantBit() int

Returns the last bit within an instruction used to encode the opcode

func (*OpCodeDescriptor) String

func (d *OpCodeDescriptor) String() string

type OpCodesDescriptor

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

Returns information about the implemented opcodes

var Opcodes OpCodesDescriptor = NewOpCodesDescriptor(
	map[OpCode]string{
		OpCode_NOP:        "NOP",
		OpCode_MOV_IMM16H: "MOVIMM16H",
		OpCode_MOV_IMM16L: "MOVIMM16L",
		OpCode_MOV:        "MOV",
		OpCode_LD:         "LD",
		OpCode_ST:         "ST",
		OpCode_ADD:        "ADD",
		OpCode_SUB:        "SUB",
		OpCode_MUL:        "MUL",
		OpCode_DIV:        "DIV",
		OpCode_MOD:        "MOD",
	},
)

func NewOpCodesDescriptor

func NewOpCodesDescriptor(mnemonics map[OpCode]string) OpCodesDescriptor

Initialized an opcodes descriptor with all the opcodes in the given opcode -> mnemonic map

func (*OpCodesDescriptor) AllOpCodes

func (d *OpCodesDescriptor) AllOpCodes() []*OpCodeDescriptor

Returns the descriptors of all implemented opcodes

func (*OpCodesDescriptor) DecodeOpCode

func (d *OpCodesDescriptor) DecodeOpCode(binaryRepresentation uint64) (OpCode, error)

Decodes an opcode from its binary representation

func (*OpCodesDescriptor) Descriptor

func (d *OpCodesDescriptor) Descriptor(op OpCode) *OpCodeDescriptor

func (*OpCodesDescriptor) EncodeOpCode

func (d *OpCodesDescriptor) EncodeOpCode(op OpCode) uint64

Encodes an opcode into its binary representation

func (*OpCodesDescriptor) Mnemonic

func (d *OpCodesDescriptor) Mnemonic(op OpCode) string

Returns the mnemonic string representation of the opcode

func (*OpCodesDescriptor) OpCodeBits

func (d *OpCodesDescriptor) OpCodeBits() int

Miminum number of bits required to binary encode an opcode

func (*OpCodesDescriptor) ParseOpCode

func (d *OpCodesDescriptor) ParseOpCode(mnemonic string) (OpCode, error)

Returns the opcode corresponding to the given mnemonic

func (*OpCodesDescriptor) TotalOpCodes

func (d *OpCodesDescriptor) TotalOpCodes() int

Number of opcodes implemented

type OperandDescriptor

type OperandDescriptor struct {
	// Type of operand
	Kind OperandKind
	// Role the operand takes in the instruction
	Role OperandRole
	// Register classes compatible with the operand in case the operand is a register, nil otherwise
	RegisterMetaClass *registers.RegisterMetaClass
	// Type of the value of the operand
	ValueType types.ValueType
	// First bit within the instruction used to encode this operand
	EncodingPosition int
	// Total bits used to encode the value into the instruction. The remaining significant bits left are truncated (ignored) from the value during encoding
	EncodingBits int
	// Operand description (for documentation and debugging)
	Description string
	// Position within the set of operands of the instruction, indexed from 0 to total operands - 1
	Index int

	// Custom operand name for LLVM instruction generation
	LLVM_CustomName string
	// Custom operand type for LLVM instruction generation
	LLVM_CustomType string
	// Custom operand pattern for LLVM instruction generation
	LLVM_CustomPattern string
}

Contains information about an instruction operand

func RegisterOperandDescriptor

func RegisterOperandDescriptor(rmc *registers.RegisterMetaClass, opd OperandDescriptor) *OperandDescriptor

Initializes a register operand descriptor

func (*OperandDescriptor) DecodeValue

func (o *OperandDescriptor) DecodeValue(value uint64) (OperandValue, error)

Decodes an operand value

func (*OperandDescriptor) IsImmediate

func (u *OperandDescriptor) IsImmediate() bool

Returns true if the operand is an immediate operand

func (*OperandDescriptor) IsRegister

func (u *OperandDescriptor) IsRegister() bool

Returns true if the operand is a register operand

func (*OperandDescriptor) ParseValue

func (o *OperandDescriptor) ParseValue(value string) (OperandValue, error)

Parses an operand value

func (*OperandDescriptor) String

func (o *OperandDescriptor) String() string

Returns an human readable string describing the operand (See [InstructionDescriptor.PrettyPrint])

type OperandKind

type OperandKind uint

Represents the kind of operand (Register, immediate, etc)

const (
	OperandKind_Immediate OperandKind = iota
	OperandKind_Register
)

func (OperandKind) String

func (o OperandKind) String() string

type OperandRole

type OperandRole uint

Represents the role an operand has within an instruction

const (
	OperandRole_Source OperandRole = iota
	OperandRole_Destination
)

func (OperandRole) String

func (o OperandRole) String() string

type OperandValue

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

Stores the value of an instruction operand

func DecodeImmediate

func DecodeImmediate(binaryRepresentation uint64, valueType types.ValueType) (OperandValue, error)

Decodes an immediate operand value

func DecodeInt32Immediate

func DecodeInt32Immediate(binaryRepresentation uint64) OperandValue

Decodes a 32 bit integer immediate operand value

func ImmediateValue

func ImmediateValue(value types.Value) OperandValue

Returns an immediate operand value

func ParseImmediate

func ParseImmediate(value string, valueType types.ValueType) (OperandValue, error)

Parses an string as an immediate operand value

func ParseInt32Immediate

func ParseInt32Immediate(value string) (OperandValue, error)

Parses an string as a 32 bit integer immediate operand value

func RegisterOperandValue

func RegisterOperandValue(register *registers.RegisterDescriptor) OperandValue

Returns a register operand value

func (OperandValue) Encode

func (v OperandValue) Encode() uint64

Returns the binary representation of the operand value

func (*OperandValue) Kind

func (v *OperandValue) Kind() OperandKind

Returns the kind of operand this value refers to

func (*OperandValue) String

func (v *OperandValue) String() string

Returns the string representation of the operand value

type RawInstruction

type RawInstruction struct {
	Descriptor    *InstructionDescriptor
	OperandValues []uint64
}

Stores a partially decoded instruction

Raw instructions are generated as a middle step in the instruction decoding process, when the instruction opcode has been decoded and identified (So we already have access to the instruction descriptor) but the instruction operands values have not been decoded yet

func (*RawInstruction) Decode

func (instr *RawInstruction) Decode() (*Instruction, error)

Returns the instruction with all its operands decoded

func (*RawInstruction) Encode

func (instr *RawInstruction) Encode() uint32

Returns the binary representation of the instruction, with the opcode and all operands encoded

func (RawInstruction) PrettyPrint

func (instr RawInstruction) PrettyPrint(leftpad int) string

Generates am ASCII frame representation of the instruction, showing all opcode and operand bits

func (*RawInstruction) String

func (instr *RawInstruction) String() string

Jump to

Keyboard shortcuts

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