golangasm

package module
v0.0.0-...-de85811 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2025 License: BSD-3-Clause Imports: 4 Imported by: 0

README ¶

golang-asm-v2

Enhanced Go Assembler Library with Go 1.25 Support and Extended RISC-V Instructions

golang-asm-v2 is an updated version of golang-asm that integrates the latest Go 1.25 assembler code, providing significantly enhanced RISC-V instruction support and modern assembler features.

Key Improvements

🚀 Massive RISC-V Instruction Set Expansion
  • 671 RISC-V instructions (vs 250 in original golang-asm)
  • 168% increase in RISC-V instruction support
  • Vector Extension (V): ~400 new vector instructions for high-performance computing
  • Bit Manipulation Extension (B): ~40 new bit operation instructions
  • Modern RISC-V Configurations: Support for rva20u64, rva22u64, rva23u64
🔧 Go 1.25 Integration
  • Updated from Go 1.15 (5 years old) to Go 1.25 (latest)
  • Enhanced build configuration system with internal/buildcfg
  • Modern assembler architecture and optimizations
  • Updated API compatibility with backward compatibility layer
🛠 API Compatibility Layer
  • ProgHelper class for handling Go 1.25 API changes
  • Seamless transition from []Addr to []AddrPos in Prog.RestArgs
  • Maintains backward compatibility while providing access to new features

Installation

go get github.com/alicemare/golang-asm-v1.25

Quick Start

Basic x86-64 Assembly
package main

import (
    golangasm "github.com/alicemare/golang-asm-v1.25"
    "github.com/alicemare/golang-asm-v1.25/obj"
    "github.com/alicemare/golang-asm-v1.25/obj/x86"
)

func main() {
    // Create builder for x86-64
    builder, err := golangasm.NewBuilder("amd64", 64)
    if err != nil {
        panic(err)
    }

    // MOV $42, AX
    movInstr := builder.NewProg()
    movInstr.As = x86.AMOVQ
    movInstr.From.Type = obj.TYPE_CONST
    movInstr.From.Offset = 42
    movInstr.To.Type = obj.TYPE_REG
    movInstr.To.Reg = x86.REG_AX
    builder.AddInstruction(movInstr)

    // Generate machine code
    machineCode := builder.Assemble()
    fmt.Printf("Generated: %x\n", machineCode)
}
Enhanced RISC-V Assembly
package main

import (
    golangasm "github.com/alicemare/golang-asm-v1.25"
    "github.com/alicemare/golang-asm-v1.25/obj"
    "github.com/alicemare/golang-asm-v1.25/obj/riscv"
)

func main() {
    // Create builder for RISC-V 64-bit
    builder, err := golangasm.NewBuilder("riscv64", 64)
    if err != nil {
        panic(err)
    }

    // ADDI x1, x0, 42  (load immediate 42 into x1)
    addiInstr := builder.NewProg()
    addiInstr.As = riscv.AADDI
    addiInstr.From.Type = obj.TYPE_CONST
    addiInstr.From.Offset = 42
    addiInstr.Reg = riscv.REG_ZERO
    addiInstr.To.Type = obj.TYPE_REG
    addiInstr.To.Reg = riscv.REG_X1
    builder.AddInstruction(addiInstr)

    // Generate machine code
    machineCode := builder.Assemble()
    fmt.Printf("Generated RISC-V: %x\n", machineCode)
}
API Compatibility Layer
// Handle Go 1.25 API changes seamlessly
prog := builder.NewProg()
helper := golangasm.NewProgHelper(prog)

// Set rest arguments (handles []Addr to []AddrPos conversion)
arg := obj.Addr{Type: obj.TYPE_REG, Reg: 1}
helper.SetRestArg(0, arg)
retrieved := helper.GetRestArg(0)

Supported Architectures

  • amd64 (x86-64)
  • arm64 (AArch64)
  • riscv64 (RISC-V 64-bit) - Significantly Enhanced
  • 386 (x86)
  • arm (ARM)
  • mips/mips64
  • ppc64
  • s390x
  • wasm

RISC-V Enhancements

New Instruction Extensions
Vector Extension (V)
  • Vector configuration instructions
  • Vector memory access instructions
  • Vector arithmetic and logical operations
  • Vector floating-point operations
  • Vector reduction operations
Bit Manipulation Extension (B)
  • Address generation instructions
  • Logical operations with immediate
  • Bit counting and manipulation
  • Rotation operations
Modern RISC-V Profiles
  • rva20u64: RISC-V Application Profile for 64-bit systems
  • rva22u64: Enhanced profile with additional extensions
  • rva23u64: Latest profile with cutting-edge features

Examples

See the examples/ directory for comprehensive usage examples:

cd examples
go run example_usage.go

This will demonstrate:

  1. x86-64 assembly generation
  2. Enhanced RISC-V instruction usage
  3. API compatibility layer features

Building from Source

# Clone the repository
git clone https://github.com/alicemare/golang-asm-v1.25
cd golang-asm-v2

# Build the library
go build .

# Run tests
cd test
go run minimal_demo.go

Migration from golang-asm

golang-asm-v2 maintains API compatibility with the original golang-asm while providing enhanced features:

  1. Drop-in replacement: Change import path from golang-asm to golang-asm-v2
  2. Enhanced RISC-V: Access to 671 RISC-V instructions vs 250 in original
  3. Modern Go support: Built on Go 1.25 vs Go 1.15
  4. API compatibility: Use ProgHelper for Go 1.25 API changes

Technical Details

Architecture
  • Modular Design: Clean separation of architecture-specific code
  • Unified Interface: Consistent API across all supported architectures
  • Extensible: Easy to add new instruction sets and extensions
Build System
  • gen.sh: Automated extraction from Go 1.25 source
  • Constant Replacement: Smart handling of build-time constants
  • Import Rewriting: Automatic path rewriting for standalone usage
API Changes Handled
  • LSym.Func → LSym.Extra with FuncInfo
  • arch.Set(string) → arch.Set(string, bool)
  • Prog.RestArgs []Addr → Prog.RestArgs []AddrPos

Performance

golang-asm-v2 provides the same high-performance assembly generation as the Go compiler's internal assembler, with the added benefit of:

  • Modern optimizations from Go 1.25
  • Enhanced instruction scheduling for RISC-V
  • Improved code generation for vector operations

Contributing

We welcome contributions! Please see the original golang-asm project for contribution guidelines.

License

This project maintains the same license as the original golang-asm and Go source code.

Acknowledgments

  • Original golang-asm by twitchyliquid64
  • Go team for the excellent assembler architecture
  • RISC-V International for the instruction set specifications

golang-asm-v2 - Bringing modern Go assembler capabilities to your projects with enhanced RISC-V support! 🚀

Documentation ¶

Index ¶

Constants ¶

This section is empty.

Variables ¶

This section is empty.

Functions ¶

This section is empty.

Types ¶

type Builder ¶

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

Builder allows you to assemble a series of instructions.

func NewBuilder ¶

func NewBuilder(archStr string, cacheSize int) (*Builder, error)

NewBuilder constructs an assembler for the given architecture.

func (*Builder) AddInstruction ¶

func (b *Builder) AddInstruction(p *obj.Prog)

AddInstruction adds an instruction to the list of instructions to be assembled.

func (*Builder) Assemble ¶

func (b *Builder) Assemble() []byte

Assemble generates the machine code from the given instructions.

func (*Builder) NewProg ¶

func (b *Builder) NewProg() *obj.Prog

NewProg returns a new instruction structure.

func (*Builder) Root ¶

func (b *Builder) Root() *obj.Prog

Root returns the first instruction.

type ProgHelper ¶

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

ProgHelper provides helper methods for working with obj.Prog to maintain compatibility with the new Go 1.25 API changes, particularly the RestArgs field change from []Addr to []AddrPos.

func NewProgHelper ¶

func NewProgHelper(prog *obj.Prog) *ProgHelper

NewProgHelper creates a helper for the given prog.

func (*ProgHelper) GetRestArg ¶

func (ph *ProgHelper) GetRestArg(index int) obj.Addr

GetRestArg gets a rest argument at the given index with backward compatibility.

func (*ProgHelper) GetRestArgs ¶

func (ph *ProgHelper) GetRestArgs() []obj.Addr

GetRestArgs gets all rest arguments as a slice of Addr.

func (*ProgHelper) SetRestArg ¶

func (ph *ProgHelper) SetRestArg(index int, addr obj.Addr)

SetRestArg sets a rest argument at the given index with backward compatibility. This handles the API change from []Addr to []AddrPos in Go 1.25.

func (*ProgHelper) SetRestArgs ¶

func (ph *ProgHelper) SetRestArgs(addrs []obj.Addr)

SetRestArgs sets all rest arguments at once.

Directories ¶

Path Synopsis
asm
arch
Package arch defines architecture-specific information and support functions.
Package arch defines architecture-specific information and support functions.
Package bio implements common I/O abstractions used within the Go toolchain.
Package bio implements common I/O abstractions used within the Go toolchain.
Package bisect can be used by compilers and other programs to serve as a target for the bisect debugging tool.
Package bisect can be used by compilers and other programs to serve as a target for the bisect debugging tool.
Package buildcfg provides access to the build configuration described by the current environment.
Package buildcfg provides access to the build configuration described by the current environment.
Package dwarf generates DWARF debugging information.
Package dwarf generates DWARF debugging information.
package goarch contains GOARCH-specific constants.
package goarch contains GOARCH-specific constants.
Package goexperiment implements support for toolchain experiments.
Package goexperiment implements support for toolchain experiments.
Package hash implements hash functions used in the compiler toolchain.
Package hash implements hash functions used in the compiler toolchain.
obj
arm
arm64
Package arm64 implements an ARM64 assembler.
Package arm64 implements an ARM64 assembler.
ppc64
Package ppc64 implements a PPC64 assembler that assembles Go asm into the corresponding PPC64 instructions as defined by the Power ISA 3.0B.
Package ppc64 implements a PPC64 assembler that assembles Go asm into the corresponding PPC64 instructions as defined by the Power ISA 3.0B.
riscv
Code generated by ./parse.py -go rv64_a rv64_d rv64_f rv64_i rv64_m rv64_q rv64_zba rv64_zbb rv64_zbs rv_a rv_d rv_f rv_i rv_m rv_q rv_s rv_system rv_v rv_zba rv_zbb rv_zbs rv_zicsr; DO NOT EDIT.
Code generated by ./parse.py -go rv64_a rv64_d rv64_f rv64_i rv64_m rv64_q rv64_zba rv64_zbb rv64_zbs rv_a rv_d rv_f rv_i rv_m rv_q rv_s rv_system rv_v rv_zba rv_zbb rv_zbs rv_zicsr; DO NOT EDIT.
x86
Package unsafeheader contains header declarations for the Go runtime's slice and string implementations.
Package unsafeheader contains header declarations for the Go runtime's slice and string implementations.

Jump to

Keyboard shortcuts

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