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 ¶
- 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 LLVMType
- type MemRef
- type Op
- type Operand
- type OperandKind
- type Options
- type Reg
- type ShiftOp
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type DataStmt ¶
DataStmt models a minimal Plan 9 DATA directive:
DATA sym+off(SB)/width, $value
Width is in bytes. Value is encoded little-endian into the global.
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 FuncSig ¶
type FuncSig struct {
Name string
Args []LLVMType
Ret LLVMType // use Void for void-return
Attrs string // optional function attributes group (e.g. "#0")
// 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
}
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
TargetTriple string
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 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
// AnnotateSource emits source asm lines as IR comments before lowering each
// instruction, for translation debugging.
AnnotateSource bool
}
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_syscall.go
- amd64_lower_vec.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
- parser.go
- preprocess.go
- source_comment.go
- translate.go
- translate_module.go
- translate_module_direct.go
- translate_prelude.go
- types.go