Documentation
¶
Overview ¶
Package plan9asm provides an experimental translator from Go/Plan 9 assembly syntax (the `.s` files used in the Go standard library) into LLVM IR.
Scope (initial):
- Focus on a small subset sufficient for leaf functions used as building blocks (CRC/byte ops/syscall stubs), without depending on Go's assembler.
- The first implementation targets amd64-like integer ops and a simplified ABI model for tests. It is not yet a drop-in replacement for the full Go assembler.
This package is intentionally self-contained (no dependency on llgo's SSA pipeline) so it can be iterated independently and unit tested by compiling the produced `.ll` with llc/clang.
Index ¶
- Variables
- func ForeignARM64Functions(src []byte) map[string]bool
- func InstructionFamily(arch Arch, opcode string) string
- func ProbeInstruction(arch Arch, goarch string, ins Instr) error
- func Translate(file *File, opt Options) (string, error)
- func TranslateModule(file *File, opt Options) (llvm.Module, error)
- type Arch
- type DataStmt
- type ExtendOp
- type File
- type FrameLayout
- type FrameSlot
- type Func
- type FuncSig
- type GloblStmt
- type GoFunction
- type GoModuleOptions
- type GoModuleTranslation
- type GoPackage
- type Instr
- type InstructionDescriptor
- type LLVMType
- type MemRef
- type NativeData
- type Op
- type Operand
- type OperandKind
- type Options
- type Reg
- type ShiftOp
- type WASMABI
- type X87Mode
Constants ¶
This section is empty.
Variables ¶
var ErrProbeNeedsContext = errors.New("instruction probe needs function context")
ErrProbeNeedsContext means an instruction cannot be judged in isolation. The full-file corpus compiler remains authoritative for these forms.
Functions ¶
func ForeignARM64Functions ¶ added in v0.6.0
ForeignARM64Functions selects files made entirely of raw foreign-ABI callbacks/trampolines. Go-declared functions continue through typed LLVM lowering. NOSPLIT and zero Go frames are required; the callback may manage its own native frame explicitly with NOFRAME.
func InstructionFamily ¶ added in v0.5.2
InstructionFamily groups opcodes by architectural function. The family is deliberately independent of current plan9asm support so unsupported forms remain visible in the same family as their supported peers.
func ProbeInstruction ¶ added in v0.5.2
ProbeInstruction runs one instruction through the real lowering pipeline. It is intended for coverage tooling; executable conformance tests are still required before a form is considered semantically verified.
Types ¶
type DataStmt ¶
DataStmt models a minimal Plan 9 DATA directive:
DATA sym+off(SB)/width, $value
Width is in bytes. Integer values are encoded little-endian into the global. String payloads are copied byte-for-byte and zero-padded to Width, matching cmd/asm's DATA string semantics.
type ExtendOp ¶
type ExtendOp string
ExtendOp is an ARM64 register-extension modifier (for example UXTB, SXTW). The prefix U/S selects zero- or sign-extension; the suffix B/H/W/X selects the source width (8/16/32/64 bits).
type File ¶
type File struct {
Arch Arch
Funcs []Func
// Data and Globl capture a minimal subset of the Plan 9 DATA/GLOBL directives
// used by some stdlib asm (e.g. hash/crc32/crc32_amd64.s).
//
// These are emitted as LLVM globals by the translator so loads like:
// MOVOA r2r1<>+0(SB), X0
// can be translated without relying on an overlay/alt package.
Data []DataStmt
Globl []GloblStmt
}
File is a parsed Plan 9 asm source file (subset).
func Parse ¶
Parse parses a subset of Go/Plan 9 assembly syntax.
Currently supported:
- TEXT directives (function start)
- MOVQ/ADDQ/SUBQ/XORQ/MOVL, CPUID, XGETBV, BYTE, RET
- Operands: immediate ($imm), register (AX/BX/CX/DX), and name+off(FP)
Also supported at a minimal level:
- #include is ignored
- #define NAME <body> with optional single-line continuation via '\' and macro invocation when the entire statement is just NAME.
type FrameLayout ¶
type FrameSlot ¶
type FrameSlot struct {
Offset int64
Type LLVMType
Index int // index into LLVM function arguments (for Params) or results tuple (for Results)
// Field is the index of the extracted field within the argument aggregate.
// It is used for classic Go asm slots like b_base+0(FP) when the Go-level
// parameter is passed as a struct (string/slice header).
//
// When Field < 0, the slot refers directly to %arg(Index).
Field int
}
type Func ¶
type Func struct {
// Sym is the symbol name from the TEXT directive with (SB) trimmed.
// It may contain the Plan 9 middle dot (·).
Sym string
// FrameSize and ArgSize retain the numeric $frame-args values from TEXT.
// WebAssembly's Go ABI uses FrameSize to address FP operands through the
// linear-memory stack and to restore SP on return. A missing -args suffix
// leaves ArgSize at zero.
FrameSize int64
ArgSize int64
Instrs []Instr
}
type FuncSig ¶
type FuncSig struct {
Name string
Args []LLVMType
Ret LLVMType // use Void for void-return
Attrs string // optional function attributes group (e.g. "#0")
// WASMContext is the physical closure-environment parameter carried in
// WebAssembly's CTXT pseudo-register. It precedes Args in the LLVM entry but
// is not part of the Go function signature, so FrameSlot.Index continues to
// address Args without an offset. The zero value means that the entry does
// not consume an incoming CTXT value.
WASMContext LLVMType
// WASMNative keeps the function's declared WebAssembly signature when
// Options.WASMABI selects the official Go stack ABI. It is used for the
// small set of runtime exports and assembly helpers that intentionally do
// not use the resumable (PC_B) -> unwind convention.
WASMNative bool
// ArgRegs optionally specifies which architectural registers correspond to
// Args for arm64 asm that passes values in non-sequential registers.
//
// When empty, args are assumed to map to R0..R7 in order (ABIInternal-ish).
//
// This is used for intra-asm tailcalls like:
// B helper<>(SB)
// where helper expects inputs in a custom register assignment.
ArgRegs []Reg
// Frame provides a minimal stack-frame model for resolving name+off(FP)
// references in Go/Plan9 assembly into LLVM function args/returns.
//
// This is intentionally limited to the classic Go assembler convention used
// by stdlib .s files (e.g. internal/cpu/cpu_x86.s).
Frame FrameLayout
}
func InferWASMAssemblyFuncSig ¶ added in v0.5.2
InferWASMAssemblyFuncSig derives the native WebAssembly signature of a file-local helper from its operand-stack and virtual-register use. Official Go uses these helpers for bytealg routines such as memeqbody and memchr; they have no Go declaration, so their signature cannot come from go/types.
type GloblStmt ¶
GloblStmt models a minimal Plan 9 GLOBL directive:
GLOBL sym(SB), flags, $size
Flags are preserved as raw text for now (e.g. "RODATA").
type GoFunction ¶
GoFunction records the original TEXT symbol and its resolved LLVM symbol.
type GoModuleOptions ¶
type GoModuleOptions struct {
FileName string
GOOS string
GOARCH string
// Sizes is the type layout used to type-check the package. When nil,
// TranslateGoModule uses the standard gc layout for GOARCH. Callers with a
// custom data model, such as Emscripten wasm32 versus Memory64, must pass
// the same Sizes instance used by go/types.
Sizes types.Sizes
// FrameSizes describes the stack-frame layout encoded in FP offsets. It
// defaults to Sizes. This is separate because LLGo's C-compatible wasm32
// mode uses 32-bit values while translating assembly emitted for Go's
// 64-bit GOARCH=wasm frame layout.
FrameSizes types.Sizes
TargetTriple string
WASMABI WASMABI
X87Mode X87Mode
AnnotateSource bool
ResolveSym func(sym string) string
KeepFunc func(textSym, resolved string) bool
ManualSig func(resolved string) (FuncSig, bool)
}
GoModuleOptions configures TranslateGoModule.
GOARCH is required and currently accepts only "amd64", "386", and "arm64". If ResolveSym is nil, the default resolver only strips ABI suffixes.
type GoModuleTranslation ¶
type GoModuleTranslation struct {
Module llvm.Module
Signatures map[string]FuncSig
Functions []GoFunction
}
GoModuleTranslation is the result of TranslateGoModule.
Callers own Module and must call Module.Dispose when finished with it.
func TranslateGoModule ¶
func TranslateGoModule(pkg GoPackage, src []byte, opt GoModuleOptions) (*GoModuleTranslation, error)
TranslateGoModule binds Go declarations to a Plan 9 asm file and translates the result into an LLVM module in one call.
The package must provide go/types information for the declarations referenced by the assembly. Methods and variadic functions are not supported.
type GoPackage ¶
type GoPackage struct {
Path string
Types *types.Package
Imports map[string]*types.Package
Syntax []*ast.File
}
GoPackage provides the Go declarations and syntax needed to bind a Plan 9 asm file to real Go symbols.
type InstructionDescriptor ¶ added in v0.5.2
type InstructionDescriptor struct {
Arch Arch `json:"arch"`
Goarch string `json:"goarch"`
Family string `json:"family"`
Opcode string `json:"opcode"`
Operands []string `json:"operands"`
Form string `json:"form"`
}
InstructionDescriptor is the stable identity used by coverage reports. Opcode-only coverage is insufficient: an opcode can accept one operand form while rejecting another valid Go assembly form.
func DescribeInstruction ¶ added in v0.5.2
func DescribeInstruction(arch Arch, goarch string, ins Instr) InstructionDescriptor
DescribeInstruction classifies an instruction without claiming that its lowering is implemented. Use ProbeInstruction to check lowering support.
type MemRef ¶
type MemRef struct {
Base Reg
Sym string // optional symbol-based address, including the (SB) suffix
Off int64
OffRaw string // unresolved symbolic displacement, used by generated wasm go_asm.h offsets
Index Reg // optional; empty if not present
IndexExt ExtendOp // optional ARM64 index extension (UXTW, SXTW, UXTX, or SXTX)
Scale int64 // optional; defaults to 1 when Index is present
Segment Reg // optional x86 segment override (FS or GS)
}
type NativeData ¶ added in v0.6.0
NativeData describes a Go global whose storage is supplied by native assembly.
func TranslateNativeARM64Object ¶ added in v0.6.0
func TranslateNativeARM64Object(obj []byte, funcs map[string]bool, imports map[string]string, pkgPath string) (string, []NativeData, error)
TranslateNativeARM64Object preserves the machine code of foreign-ABI assembly. Unlike Go-declared functions, raw callbacks have no signature from which LLVM can reconstruct incoming registers. The Go assembler encodes those registers; this bridge only converts its address and branch relocations to Mach-O asm.
The input must be a darwin/arm64 cmd/asm object in go120ld format. Only explicitly selected functions, their data, and declared dynamic imports are accepted. Unknown formats, symbol references and relocations are errors.
type Op ¶
type Op string
const ( OpTEXT Op = "TEXT" OpMRS Op = "MRS" OpMOVQ Op = "MOVQ" OpMOVL Op = "MOVL" OpADDQ Op = "ADDQ" OpSUBQ Op = "SUBQ" OpXORQ Op = "XORQ" OpMOVD Op = "MOVD" OpCPUID Op = "CPUID" OpXGETBV Op = "XGETBV" OpBYTE Op = "BYTE" OpWORD Op = "WORD" OpCALL Op = "CALL" OpJMP Op = "JMP" OpRET Op = "RET" OpLABEL Op = "LABEL" )
type Operand ¶
type Operand struct {
Kind OperandKind
Imm int64 // OpImm
ImmRaw string // OpImm unresolved symbolic placeholder, including leading '$'
Reg Reg // OpReg
Ext ExtendOp // OpRegExtend
// OpRegShift
ShiftOp ShiftOp
ShiftAmount int64
ShiftReg Reg
FPName string // OpFP (e.g. "a", "ret")
FPOffset int64 // OpFP
Ident string // OpIdent (e.g. system register name in MRS)
Sym string // OpSym / OpLabel
// OpMem: a minimal memory addressing mode.
// Examples:
// (SI)
// 16(SI)
// -8(SI)(R8*1)
// (R0)(R6)
Mem MemRef
RegList []Reg // OpRegList (e.g. (R4, R8))
}
Operand models a minimal subset of Plan 9 asm operands.
Supported:
- Immediate: $123
- Register: AX, BX, ...
- FP slot: name+offset(FP) (used in classic Go stack ABI syntax)
type OperandKind ¶
type OperandKind int
const ( OpInvalid OperandKind = iota OpImm OpReg OpRegExtend OpRegShift OpFP OpFPAddr OpIdent OpSym OpLabel OpMem OpRegList )
type Options ¶
type Options struct {
TargetTriple string
// ResolveSym maps the TEXT symbol (with (SB) trimmed) into the final linker
// symbol name to emit in LLVM IR. If nil, the symbol is used as-is.
ResolveSym func(sym string) string
// Sigs maps resolved symbol name -> signature.
Sigs map[string]FuncSig
// Goarch is used for a few arch-specific translations (e.g. x86 CPUID).
Goarch string
// WASMABI selects the WebAssembly physical ABI. Its zero value preserves
// the direct ABI used by existing plan9asm callers.
WASMABI WASMABI
// X87Mode controls explicit x87 instruction lowering for GOARCH=386. Auto
// uses hardware, matching the Go 386 target contract. Software retains the
// portable LLVM fallback for custom targets without x87 hardware.
X87Mode X87Mode
// AnnotateSource emits source asm lines as IR comments before lowering each
// instruction, for translation debugging.
AnnotateSource bool
}
type WASMABI ¶ added in v0.5.2
type WASMABI uint8
WASMABI selects the physical ABI used for WebAssembly Plan 9 assembly. Direct is LLGo's existing native LLVM calling convention. Go uses the official linear-memory stack, mutable register globals, PC_B resume input, and an i32 unwind result.
type X87Mode ¶ added in v0.5.1
type X87Mode uint8
X87Mode selects how explicit x87 instructions in 386 Plan 9 assembly are lowered. Hand-written assembly may use x87 mnemonics regardless of GO386; the Go-supported Pentium MMX-or-newer baseline has an x87 unit, so the default emits hardware instructions. Software mode is reserved for custom targets that explicitly cannot execute x87 instructions.
Source Files
¶
- amd64_blocks.go
- amd64_ctx.go
- amd64_eval.go
- amd64_lower_arith.go
- amd64_lower_atomic.go
- amd64_lower_branch.go
- amd64_lower_cmpbt.go
- amd64_lower_crc32.go
- amd64_lower_fp.go
- amd64_lower_mov.go
- amd64_lower_string.go
- amd64_lower_syscall.go
- amd64_lower_vec.go
- amd64_lower_x87.go
- amd64_needed.go
- amd64_translate.go
- arm64_blocks.go
- arm64_ctx.go
- arm64_eval.go
- arm64_flags.go
- arm64_fp.go
- arm64_lower_arith.go
- arm64_lower_atomic.go
- arm64_lower_branch.go
- arm64_lower_cond.go
- arm64_lower_data.go
- arm64_lower_fpops.go
- arm64_lower_syscall.go
- arm64_lower_vec.go
- arm64_lower_word.go
- arm64_needed.go
- arm64_translate.go
- arm_blocks.go
- arm_ctx.go
- arm_eval.go
- arm_flags.go
- arm_fp.go
- arm_lower_arith.go
- arm_lower_atomic.go
- arm_lower_branch.go
- arm_lower_data.go
- arm_lower_movm.go
- arm_lower_syscall.go
- arm_needed.go
- arm_translate.go
- arm_translate_cfg.go
- doc.go
- feature_attrs.go
- floatlit.go
- go_translate.go
- instruction.go
- native_arm64_object.go
- parser.go
- preprocess.go
- source_comment.go
- translate.go
- translate_module.go
- translate_module_direct.go
- translate_prelude.go
- types.go
- wasm_infer.go
- wasm_ops.go
- wasm_translate.go
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
plan9asmcorpus
command
|
|
|
plan9asmdiscover
command
Command plan9asmdiscover finds public Go modules that contain Plan 9 assembly.
|
Command plan9asmdiscover finds public Go modules that contain Plan 9 assembly. |
|
plan9asmscan
command
|