// Copyright (c) 2026 Petr Balvín (https://petrbalvin.org) // SPDX-License-Identifier: BSD-3-Clause //go:build integration // Package asm integration tests against the production go-libraries kernels. // These are excluded from the default test run (go test ./...) so that the // coverage numbers are identical locally and in CI, where go-libraries is // not checked out. Run them explicitly with: go test -tags=integration ./asm/ package asm import ( "bytes" "os" "testing" "golang.org/x/arch/x86/x86asm" "sourcedock.dev/petrbalvin/gasm-devkit/parser" ) // TestAssembleGoFlacAVX2Kernel assembles the whole production AVX2 kernel; // all functions plus the file-local mask24 constant; and checks that every // static-symbol load resolves to the right bytes in the image. func TestAssembleGoFlacAVX2Kernel(t *testing.T) { path := "../../go-libraries/go-flac/avx2_amd64.s" if _, err := os.Stat(path); err != nil { t.Skip("go-libraries repository not present next to gasm-devkit") } src, err := os.ReadFile(path) if err != nil { t.Fatal(err) } f, errs := parser.Parse(path, string(src)) if len(errs) > 0 { t.Fatalf("parse: %v", errs) } img, err := AssembleFile(f) if err != nil { t.Fatalf("AssembleFile: %v", err) } if len(img.Funcs) != 17 { t.Errorf("functions = %d, want 17", len(img.Funcs)) } // mask24 as the DATA directives define it. mask := []byte{ 0x00, 0x01, 0x02, 0x80, 0x03, 0x04, 0x05, 0x80, 0x06, 0x07, 0x08, 0x80, 0x09, 0x0a, 0x0b, 0x80, } image := img.Bytes() if got := image[img.Symbols["mask24"] : img.Symbols["mask24"]+16]; !bytes.Equal(got, mask) { t.Errorf("mask24 contents %x, want %x", got, mask) } // Every VMOVDQU mask24<>(SB), X15 (c5 7a 6f 3d + rel32, i.e. a VMOVDQU // with a RIP-relative r/m) must land on the mask bytes within the image. loads := 0 for _, fn := range img.Funcs { code := img.Code[fn.Offset : fn.Offset+fn.Size] for pc := 0; pc < len(code); { inst, err := x86asm.Decode(code[pc:], 64) if err != nil { t.Fatalf("%s: decode at +%d: %v", fn.Name, pc, err) } // mod=00, rm=101 → RIP-relative. if inst.Op == x86asm.VMOVDQU && inst.Len == 8 && code[pc+3]&0xC7 == 0x05 { rel := int32(uint32(code[pc+4]) | uint32(code[pc+5])<<8 | uint32(code[pc+6])<<16 | uint32(code[pc+7])<<24) target := fn.Offset + pc + 8 + int(rel) if !bytes.Equal(image[target:target+16], mask) { t.Errorf("%s: mask load at +%d lands on %x, want %x", fn.Name, pc, image[target:target+16], mask) } loads++ } pc += inst.Len } } if loads != 2 { t.Errorf("mask loads found = %d, want 2", loads) } } // TestAssembleGoFlacAVX512Kernel assembles the whole production AVX-512 // kernel, all functions plus the file-global idx16 constant, and checks // that the static-symbol load resolves to the right bytes in the image. func TestAssembleGoFlacAVX512Kernel(t *testing.T) { path := "../../go-libraries/go-flac/avx512_amd64.s" if _, err := os.Stat(path); err != nil { t.Skip("go-libraries repository not present next to gasm-devkit") } src, err := os.ReadFile(path) if err != nil { t.Fatal(err) } f, errs := parser.Parse(path, string(src)) if len(errs) > 0 { t.Fatalf("parse: %v", errs) } img, err := AssembleFile(f) if err != nil { t.Fatalf("AssembleFile: %v", err) } if len(img.Funcs) != 10 { t.Errorf("functions = %d, want 10", len(img.Funcs)) } // idx16 as the DATA directives define it: dwords 1..16. idx := make([]byte, 0, 64) for i := 1; i <= 16; i++ { idx = append(idx, byte(i), 0, 0, 0) } image := img.Bytes() base := img.Symbols["idx16"] if base == 0 { t.Fatal("idx16 not laid out") } if got := image[base : base+64]; hexCompact(got) != hexCompact(idx) { t.Errorf("idx16 contents %x, want %x", got, idx) } // The VMOVDQU32 idx16(SB), Z13 load (62 71 7e 48 6f 2d + rel32) must // resolve to idx16 within the image. loads := 0 for _, fn := range img.Funcs { code := img.Code[fn.Offset : fn.Offset+fn.Size] pat := []byte{0x62, 0x71, 0x7e, 0x48, 0x6f, 0x2d} for pos := 0; ; { i := indexOf(code[pos:], pat) if i < 0 { break } i += pos rel := int32(uint32(code[i+6]) | uint32(code[i+7])<<8 | uint32(code[i+8])<<16 | uint32(code[i+9])<<24) target := fn.Offset + i + 10 + int(rel) if target != base { t.Errorf("%s: idx16 load at +%d targets 0x%x, want 0x%x", fn.Name, i, target, base) } loads++ pos = i + 10 } } if loads != 1 { t.Errorf("idx16 loads found = %d, want 1", loads) } } // indexOf returns the index of the first occurrence of pat in b, or -1. func indexOf(b, pat []byte) int { for i := 0; i+len(pat) <= len(b); i++ { j := 0 for j < len(pat) && b[i+j] == pat[j] { j++ } if j == len(pat) { return i } } return -1 }