cpu

package
v0.0.0-...-46f7491 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2024 License: 0BSD Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CallFunc

func CallFunc()

func GrowStack

func GrowStack()

func Prepare

func Prepare()

func PushFunc

func PushFunc()

func Restore

func Restore()

Types

type ArchFunc

type ArchFunc uint8

type Args

type Args uint8

Args to a Mode

type Error

type Error int8

func (Error) Error

func (err Error) Error() string

type FloatingPointRegister

type FloatingPointRegister floatingPointRegister

FloatingPointRegister represents a handle to a floating-point optimised data register.

func (*FloatingPointRegister) Bool

func (r *FloatingPointRegister) Bool() bool

Bool returns true if the register is not equal to 0.

func (*FloatingPointRegister) Float32

func (r *FloatingPointRegister) Float32() float32

Float32 returns the register as a float32.

func (*FloatingPointRegister) Float64

func (r *FloatingPointRegister) Float64() float64

Float64 returns the register as a float64.

func (*FloatingPointRegister) Int

func (r *FloatingPointRegister) Int() int

Int returns the register as an int.

func (*FloatingPointRegister) Int32

func (r *FloatingPointRegister) Int32() int32

Int32 returns the register as an int32.

func (*FloatingPointRegister) Int64

func (r *FloatingPointRegister) Int64() int64

Int64 returns the register as an int64.

func (*FloatingPointRegister) Int8

func (r *FloatingPointRegister) Int8() int8

Int8 returns the register as an int8.

func (*FloatingPointRegister) SetBool

func (r *FloatingPointRegister) SetBool(b bool)

SetBool sets the register to 1 if b is true, otherwise 0.

func (*FloatingPointRegister) SetFloat32

func (r *FloatingPointRegister) SetFloat32(f float32)

SetFloat32 sets the register to f.

func (*FloatingPointRegister) SetFloat64

func (r *FloatingPointRegister) SetFloat64(f float64)

SetFloat64 sets the register to f.

func (*FloatingPointRegister) SetInt

func (r *FloatingPointRegister) SetInt(i int)

SetInt sets the register to i.

func (*FloatingPointRegister) SetInt32

func (r *FloatingPointRegister) SetInt32(i int32)

SetInt32 sets the register to i.

func (*FloatingPointRegister) SetInt64

func (r *FloatingPointRegister) SetInt64(i int64)

SetInt64 sets the register to i.

func (*FloatingPointRegister) SetInt8

func (r *FloatingPointRegister) SetInt8(i int8)

SetInt8 sets the register to i.

func (*FloatingPointRegister) SetUint

func (r *FloatingPointRegister) SetUint(i uint)

SetUint sets the register to i.

func (*FloatingPointRegister) SetUint16

func (r *FloatingPointRegister) SetUint16(i uint16)

SetUint16 sets the register to i.

func (*FloatingPointRegister) SetUint32

func (r *FloatingPointRegister) SetUint32(i uint32)

SetUint32 sets the register to i.

func (*FloatingPointRegister) SetUint64

func (r *FloatingPointRegister) SetUint64(i uint64)

SetUint64 sets the register to i.

func (*FloatingPointRegister) SetUint8

func (r *FloatingPointRegister) SetUint8(i uint8)

SetUint8 sets the register to i.

func (*FloatingPointRegister) SetUintptr

func (r *FloatingPointRegister) SetUintptr(i uintptr)

SetUintptr sets the register to i.

func (*FloatingPointRegister) SetUnsafePointer

func (r *FloatingPointRegister) SetUnsafePointer(i unsafe.Pointer)

SetUnsafePointer sets the register to i.

func (*FloatingPointRegister) Uint

func (r *FloatingPointRegister) Uint() uint

Uint returns the register as an uint.

func (*FloatingPointRegister) Uint16

func (r *FloatingPointRegister) Uint16() uint16

Uint16 returns the register as an uint16.

func (*FloatingPointRegister) Uint32

func (r *FloatingPointRegister) Uint32() uint32

Uint32 returns the register as an uint32.

func (*FloatingPointRegister) Uint64

func (r *FloatingPointRegister) Uint64() uint64

Uint64 returns the register as an uint64.

func (*FloatingPointRegister) Uint8

func (r *FloatingPointRegister) Uint8() uint8

Uint8 returns the register as an uint8.

func (*FloatingPointRegister) Uintptr

func (r *FloatingPointRegister) Uintptr() uintptr

Uintptr returns the register as an uintptr

func (*FloatingPointRegister) UnsafePointer

func (r *FloatingPointRegister) UnsafePointer() unsafe.Pointer

UnsafePointer returns the register as an unsafe.Pointer only valid when the pointer in this register is runtime pinned, static, or when it hasn't been allocated by Go.

type FuncName

type FuncName uint8
const (
	Noop FuncName = iota

	Jump // to instruction $normal if $assert is not zero.
	Call // call the function loaded into the context.
	Wrap // replace $normal with p.Func[$length]($normal)
	Data // replace $normal with p.Data[$normal]

	Bool // convert $normal to a boolean in $normal.

	SwapLength
	SwapAssert
	SwapResult

	HeapMake
	HeapPush8
	HeapPush16
	HeapPush32
	HeapPush64
	HeapCopy
	HeapLoad

	StackCaller // switch to caller stack pointer
	StackCallee // switch to callee stack pointer
	Stack       // reset stack pointer
	Stack8
	Stack16
	Stack32
	Stack64

	PointerMake // make $normal pointer a [Pointer].
	PointerFree
	PointerKeep
	PointerLoad

	ErrorMake // make $result into an error in $normal and $assert.

	StringSize // null-terminated string size of $normal
	StringCopy // copy $normal as a null-terminated string and store in $normal.
	StringMake // copy $normal + $length as a null-terminated string if needed.

	AssertArgs
)

Func names.

type Instruction

type Instruction uint8

Instruction is a single instruction that can be executed by the cpu inside of a [MakeFunc] function.

func NewArch

func NewArch(args ArchFunc) Instruction

func NewBits

func NewBits(args uint8) Instruction

func NewFunc

func NewFunc(args FuncName) Instruction

func NewLoad

func NewLoad(args Location) Instruction

func NewMath

func NewMath(args MathFunc) Instruction

func NewMove

func NewMove(args Location) Instruction

func NewSlow

func NewSlow(args SlowFunc) Instruction

func (Instruction) Decode

func (op Instruction) Decode() (Mode, Args)

Decode returns the mode and arguments of the instruction.

func (Instruction) String

func (op Instruction) String() (s string)

String implements fmt.Stringer and returns a readable representation of the instruction.

type Location

type Location uint8
const (
	R0 Location = iota
	R1
	R2
	R3
	R4
	R5
	R6
	R7
	R8
	R9
	R10
	R11
	R12
	R13
	R14
	R15
	X0
	X1
	X2
	X3
	X4
	X5
	X6
	X7
	X8
	X9
	X10
	X11
	X12
	X13
	X14
	X15
)

Registers

type MathFunc

type MathFunc uint8
const (
	Flip MathFunc = iota // invert $result as a boolean

	// assertions
	Less // write 1 to $result if $normal !< $assert
	More // write 1 to $result if $normal !> $assert
	Same // write 1 to $result if $normal != $assert
	Diff // write 1 to $result if $normal = $assert

	Add // add $normal and $assert
	Sub // subtract $assert from $normal
	Mul // multiply $normal and $assert
	Div // divide $normal by $assert
	Mod // modulo $normal by $assert

	Addi // signed add $normal and $assert
	Subi // signed subtract $assert from $normal
	Muli // signed multiply $normal and $assert
	Divi // signed divide $normal by $assert
	Modi // signed modulo $normal by $assert

	And // bitwise and $normal and $assert
	Or  // bitwise or $normal and $assert
	Xor // bitwise xor $normal and $assert
	Shl // shift $normal left by $assert
	Shr // shift $normal right by $assert

	Addf // add $normal and $assert as floating point
	Subf // subtract $assert from $normal as floating point
	Mulf // multiply $normal and $assert as floating point
	Divf // divide $normal by $assert as floating point

	Conv  // cast integer $normal to $result as a float
	Convf // cast float $normal to $result as an integer
)

Math operations, operate on $normal and $assert, result is stored in $result.

type Mode

type Mode uint8

Mode is the highest 3-bits of an instruction that defined how to interpret the remaining lower 5 bits of the instruction.

const (
	Func Mode = iota
	Arch      // architecture-specific registers and instructions.
	Load      // register N into the $normal register.
	Slow      // ABI compatible functions.
	Move      // the $normal register into write-only output register N.
	Math      // math operations.
	Bits      // load lowest five bits of $normal into register N.
)

Modes supported.

type Pointer

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

type Program

type Program struct {
	Call unsafe.Pointer // unsafe pointer to the function that will be called by the [Call] instruction.
	Size int            // stack/frame size, zero if unknown.
	Text []Instruction
	Pins []Location

	Data []uint
	Func []func(Register) Register

	Dump func() // debug dumper
	// contains filtered or unexported fields
}

Program can be converted into any Go function at runtime. The virtual machine Instruction is geared for writing calling convention converters, although it is turing complete and can be used for general purpose computation (with limited access to the system).

func (*Program) Add

func (program *Program) Add(ins ...Instruction)

Add the given instructions to the program.

func (*Program) MakeFunc

func (p *Program) MakeFunc(rtype reflect.Type) reflect.Value

MakeFunc is the safest way to convert a Program into a Go function (still very unsafe!) as it garuntees that the Program will have access to the full set of registers. The resulting call-overhead is the highest. This can be optimized by calling the variants of MakeFunc that leverage a more restricted set of registers.

func (*Program) Pin

func (program *Program) Pin(reg ...Location)

Pin the given pointer to the program.

type Register

type Register register

Register represents a handle to a general purpose data register.

func (*Register) Bool

func (r *Register) Bool() bool

Bool returns true if the register is not equal to 0.

func (*Register) Float32

func (r *Register) Float32() float32

Float32 returns the register as an float32.

func (*Register) Float64

func (r *Register) Float64() float64

Float64 returns the register as a float64.

func (*Register) Int

func (r *Register) Int() int

Int returns the register as an int.

func (*Register) Int16

func (r *Register) Int16() int16

Int16 returns the register as an int16.

func (*Register) Int32

func (r *Register) Int32() int32

Int32 returns the register as an int32.

func (*Register) Int64

func (r *Register) Int64() int64

Int64 returns the register as an int64.

func (*Register) Int8

func (r *Register) Int8() int8

Int8 returns the register as an int8.

func (*Register) SetBool

func (r *Register) SetBool(b bool)

SetBool sets the register to 1 if b is true, otherwise 0.

func (*Register) SetFloat32

func (r *Register) SetFloat32(i float32)

SetFloat32 sets the register to i.

func (*Register) SetFloat64

func (r *Register) SetFloat64(f float64)

SetFloat64 sets the register to f.

func (*Register) SetInt

func (r *Register) SetInt(i int)

SetInt sets the register to i.

func (*Register) SetInt16

func (r *Register) SetInt16(i int16)

SetInt16 sets the register to i.

func (*Register) SetInt32

func (r *Register) SetInt32(i int32)

SetInt32 sets the register to i.

func (*Register) SetInt64

func (r *Register) SetInt64(i int64)

SetInt64 sets the register to i.

func (*Register) SetInt8

func (r *Register) SetInt8(i int8)

SetInt8 sets the register to i.

func (*Register) SetUint

func (r *Register) SetUint(i uint)

SetUint sets the register to i.

func (*Register) SetUint16

func (r *Register) SetUint16(i uint16)

SetUint16 sets the register to i.

func (*Register) SetUint32

func (r *Register) SetUint32(i uint32)

SetUint32 sets the register to i.

func (*Register) SetUint64

func (r *Register) SetUint64(i uint64)

SetUint64 sets the register to i.

func (*Register) SetUint8

func (r *Register) SetUint8(i uint8)

SetUint8 sets the register to i.

func (*Register) SetUintptr

func (r *Register) SetUintptr(i uintptr)

SetUintptr sets the register to i.

func (*Register) SetUnsafePointer

func (r *Register) SetUnsafePointer(i unsafe.Pointer)

SetUnsafePointer sets the register to i.

func (*Register) Uint

func (r *Register) Uint() uint

Uint returns the register as an uint.

func (*Register) Uint16

func (r *Register) Uint16() uint16

Uint16 returns the register as an uint16.

func (*Register) Uint32

func (r *Register) Uint32() uint32

Uint32 returns the register as an uint32.

func (*Register) Uint64

func (r *Register) Uint64() uint64

Uint64 returns the register as an uint64.

func (*Register) Uint8

func (r *Register) Uint8() uint8

Uint8 returns the register as an uint8.

func (*Register) Uintptr

func (r *Register) Uintptr() uintptr

Uintptr returns the register as an uintptr.

func (*Register) UnsafePointer

func (r *Register) UnsafePointer() unsafe.Pointer

UnsafePointer returns the register as an unsafe.Pointer only valid when the pointer in this register is runtime pinned, static, or when it hasn't been allocated by Go.

type RegistersArch

type RegistersArch = registers

Registers represent the full set of calling registers available on the current architecture for handling the slowest but most general calls.

type RegistersFast

type RegistersFast struct {
	RegistersZero
	// contains filtered or unexported fields
}

RegistersFast represent the smallest set of useful calling registers available on the current architecture for the fastest calls (R0, R1, and X0).

func (*RegistersFast) R0

func (r *RegistersFast) R0() *Register

func (*RegistersFast) R1

func (r *RegistersFast) R1() *Register

func (*RegistersFast) X0

type RegistersFull

type RegistersFull struct {
	RegistersMany
	// contains filtered or unexported fields
}

RegistersFull represent the full set of calling registers, even if they are not available on the current architecture (undefined behaviour).

func (*RegistersFull) R10

func (r *RegistersFull) R10() *Register

func (*RegistersFull) R11

func (r *RegistersFull) R11() *Register

func (*RegistersFull) R12

func (r *RegistersFull) R12() *Register

func (*RegistersFull) R13

func (r *RegistersFull) R13() *Register

func (*RegistersFull) R14

func (r *RegistersFull) R14() *Register

func (*RegistersFull) R15

func (r *RegistersFull) R15() *Register

func (*RegistersFull) R8

func (r *RegistersFull) R8() *Register

func (*RegistersFull) R9

func (r *RegistersFull) R9() *Register

func (*RegistersFull) X10

func (*RegistersFull) X11

func (*RegistersFull) X12

func (*RegistersFull) X13

func (*RegistersFull) X14

func (*RegistersFull) X15

func (*RegistersFull) X4

func (*RegistersFull) X5

func (*RegistersFull) X6

func (*RegistersFull) X7

func (*RegistersFull) X8

func (*RegistersFull) X9

type RegistersLong

type RegistersLong struct {
	RegistersSome
	// contains filtered or unexported fields
}

RegistersLong represent a suitable set of calling registers available on the current architecture for most calls (R0, R1, R2, R3, R4, R5, X0, X1 and X2).

func (*RegistersLong) R4

func (r *RegistersLong) R4() *Register

func (*RegistersLong) R5

func (r *RegistersLong) R5() *Register

func (*RegistersLong) X2

type RegistersMany

type RegistersMany struct {
	RegistersLong
	// contains filtered or unexported fields
}

RegistersMany represent even more registers that are available on the current architecture for calls that should still be slightly faster than the full register set (R0, R1, R2, R3, R4, R5, R6, R7, X0, X1, X2 and X3).

func (*RegistersMany) R6

func (r *RegistersMany) R6() *Register

func (*RegistersMany) R7

func (r *RegistersMany) R7() *Register

func (*RegistersMany) X3

type RegistersSome

type RegistersSome struct {
	RegistersFast
	// contains filtered or unexported fields
}

RegistersSome represent a useful set of calling registers available on the current architecture for faster calls (R0, R1, R2, R3, X0 and X1).

func (*RegistersSome) R2

func (r *RegistersSome) R2() *Register

func (*RegistersSome) R3

func (r *RegistersSome) R3() *Register

func (*RegistersSome) X1

type RegistersZero

type RegistersZero struct{}

RegistersZero represents access to no calling registers. Accessing any register will result in a panic.

func (*RegistersZero) R0

func (r *RegistersZero) R0() *Register

func (*RegistersZero) R1

func (r *RegistersZero) R1() *Register

func (*RegistersZero) R10

func (r *RegistersZero) R10() *Register

func (*RegistersZero) R11

func (r *RegistersZero) R11() *Register

func (*RegistersZero) R12

func (r *RegistersZero) R12() *Register

func (*RegistersZero) R13

func (r *RegistersZero) R13() *Register

func (*RegistersZero) R14

func (r *RegistersZero) R14() *Register

func (*RegistersZero) R15

func (r *RegistersZero) R15() *Register

func (*RegistersZero) R2

func (r *RegistersZero) R2() *Register

func (*RegistersZero) R3

func (r *RegistersZero) R3() *Register

func (*RegistersZero) R4

func (r *RegistersZero) R4() *Register

func (*RegistersZero) R5

func (r *RegistersZero) R5() *Register

func (*RegistersZero) R6

func (r *RegistersZero) R6() *Register

func (*RegistersZero) R7

func (r *RegistersZero) R7() *Register

func (*RegistersZero) R8

func (r *RegistersZero) R8() *Register

func (*RegistersZero) R9

func (r *RegistersZero) R9() *Register

func (*RegistersZero) X0

func (*RegistersZero) X1

func (*RegistersZero) X10

func (*RegistersZero) X11

func (*RegistersZero) X12

func (*RegistersZero) X13

func (*RegistersZero) X14

func (*RegistersZero) X15

func (*RegistersZero) X2

func (*RegistersZero) X3

func (*RegistersZero) X4

func (*RegistersZero) X5

func (*RegistersZero) X6

func (*RegistersZero) X7

func (*RegistersZero) X8

func (*RegistersZero) X9

type SlowFunc

type SlowFunc uint8
const (
	InitStruct SlowFunc = iota // required for CallStruct

	PushStruct
	PushBytes1
	PushBytes2
	PushBytes4
	PushBytes8
	PushMemory
	PushSizing

	PushFloat4
	PushFloat8

	CallStruct
	CallBytes1
	CallBytes2
	CallBytes4
	CallBytes8
	CallFloat4
	CallFloat8
	CallMemory
	CallSizing
	CallIgnore
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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