...

Package loader

import "github.com/bytedance/sonic/loader"
Overview
Index

Overview ▾

Constants

Note: This list must match the list in runtime/symtab.go.

const (
    FuncFlag_TOPFRAME = 1 << iota
    FuncFlag_SPWRITE
    FuncFlag_ASM
)
const (
    // PCDATA_UnsafePoint values.
    PCDATA_UnsafePointSafe   = -1 // Safe for async preemption
    PCDATA_UnsafePointUnsafe = -2 // Unsafe for async preemption

    // PCDATA_Restart1(2) apply on a sequence of instructions, within
    // which if an async preemption happens, we should back off the PC
    // to the start of the sequence when resume.
    // We need two so we can distinguish the start/end of the sequence
    // in case that two sequences are next to each other.
    PCDATA_Restart1 = -3
    PCDATA_Restart2 = -4

    // Like PCDATA_RestartAtEntry, but back to function entry if async
    // preempted.
    PCDATA_RestartAtEntry = -5
)

PCDATA and FUNCDATA table indexes.

See funcdata.h and $GROOT/src/cmd/internal/objabi/funcdata.go.

const (

    // ArgsSizeUnknown is set in Func.argsize to mark all functions
    // whose argument size is unknown (C vararg functions, and
    // assembly code without an explicit specification).
    // This value is generated by the compiler, assembler, or linker.
    ArgsSizeUnknown = -0x80000000
)

func WrapGoC

func WrapGoC(text []byte, natives []CFunc, stubs []GoC, modulename string, filename string)

WrapGoC wraps C functions and loader it into Go stubs

type CFunc

CFunc is a function information for C func

type CFunc struct {
    // C function name
    Name string

    // entry pc relative to entire text segment
    EntryOff uint32

    // function text size in bytes
    TextSize uint32

    // maximum stack depth of the function
    MaxStack uintptr

    // PC->SP delta lists of the function
    Pcsp [][2]uint32
}

type Func

Func contains information about a function.

type Func struct {
    ID          uint8  // see runtime/symtab.go
    Flag        uint8  // see runtime/symtab.go
    ArgsSize    int32  // args byte size
    EntryOff    uint32 // start pc, offset to moduledata.text
    TextSize    uint32 // size of func text
    DeferReturn uint32 // offset of start of a deferreturn call instruction from entry, if any.
    FileIndex   uint32 // index into filetab
    Name        string // name of function

    // PC data
    Pcsp            *Pcdata // PC -> SP delta
    Pcfile          *Pcdata // PC -> file index
    Pcline          *Pcdata // PC -> line number
    PcUnsafePoint   *Pcdata // PC -> unsafe point, must be PCDATA_UnsafePointSafe or PCDATA_UnsafePointUnsafe
    PcStackMapIndex *Pcdata // PC -> stack map index, relative to ArgsPointerMaps and LocalsPointerMaps
    PcInlTreeIndex  *Pcdata // PC -> inlining tree index, relative to InlTree
    PcArgLiveIndex  *Pcdata // PC -> arg live index, relative to ArgLiveInfo

    // Func data, must implement encoding.BinaryMarshaler
    ArgsPointerMaps    encoding.BinaryMarshaler // concrete type: *StackMap
    LocalsPointerMaps  encoding.BinaryMarshaler // concrete type: *StackMap
    StackObjects       encoding.BinaryMarshaler
    InlTree            encoding.BinaryMarshaler
    OpenCodedDeferInfo encoding.BinaryMarshaler
    ArgInfo            encoding.BinaryMarshaler
    ArgLiveInfo        encoding.BinaryMarshaler
    WrapInfo           encoding.BinaryMarshaler
}

type Function

Function is a function pointer

type Function unsafe.Pointer

func Load

func Load(text []byte, funcs []Func, modulename string, filenames []string) (out []Function)

Load loads given machine codes and corresponding function information into go moduledata and returns runnable function pointer WARN: this API is experimental, use it carefully

type GoC

GoC is the wrapper for Go calls to C

type GoC struct {
    // CName is the name of corresponding C function
    CName string

    // CEntry points out where to store the entry address of corresponding C function.
    // It won't be set if nil
    CEntry *uintptr

    // GoFunc is the POINTER of corresponding go stub function.
    // It is used to generate Go-C ABI conversion wrapper and receive the wrapper's address
    //   eg. &func(a int, b int) int
    //     FOR
    //     int add(int a, int b)
    // It won't be set if nil
    GoFunc interface{}
}

type Loader

Loader is a helper used to load a module simply

type Loader struct {
    Name string // module name
    File string // file name
    Options
}

func (Loader) LoadOne

func (self Loader) LoadOne(text []byte, funcName string, frameSize int, argSize int, argPtrs []bool, localPtrs []bool) Function

LoadFuncs loads only one function as module, and returns the function pointer

WARN:

type Options

Options used to load a module

type Options struct {
    // NoPreempt is used to disable async preemption for this module
    NoPreempt bool
}

type Pcdata

Pcdata represents pc->value mapping table.

WARN: we use ** [Pcdata[i].PC, Pcdata[i+1].PC) **
as the range where the Pcdata[i].Val is effective.
type Pcdata []Pcvalue

func (Pcdata) MarshalBinary

func (self Pcdata) MarshalBinary() (data []byte, err error)

see https://docs.google.com/document/d/1lyPIbmsYbXnpNj57a261hgOYVpNRcgydurVQIyZOz_o/pub

type Pcvalue

Pcvalue is the program count corresponding to the value Val

WARN: we use relative value here (to function entry)
type Pcvalue struct {
    PC  uint32 // program count relative to function entry
    Val int32  // value relative to the value in function entry
}