fix(asm): encode amd64 frame adjustments above 127 bytes with imm32

This commit is contained in:
2026-09-14 18:22:00 +02:00
parent f5fc22d390
commit c6f0286732
2 changed files with 32 additions and 6 deletions
+8 -6
View File
@@ -22,6 +22,11 @@ import (
// FP/SP frame-relative operands, and local-label jumps. SB (global symbol)
// operands require relocations and are not yet supported; the SIMD (VEX/AVX2)
// integer and shuffle/extract/permute/move set is in.
//
// Like the other architectures, the stack-growth guard (the morestack check
// in the prologue and the call back into the runtime in the epilogue) is not
// emitted: the bytes match go tool asm only for NOSPLIT functions or
// zero-frame leaves, where the toolchain emits no guard either.
func Assemble(t *ast.Text) ([]byte, map[string]int, error) {
code, _, labels, _, _, err := assemble(t, nil)
return code, labels, err
@@ -31,7 +36,7 @@ func Assemble(t *ast.Text) ([]byte, map[string]int, error) {
// the set of static symbols a GLOBL in the same file defines. A nil link
// rejects SB operands outright (single-function assembly cannot resolve
// them). When allowExternal is set, a reference to a symbol no GLOBL in the
// file defines is recorded as an external relocation instead of failing —
// file defines is recorded as an external relocation instead of failing
// the object-file emitters resolve it at link time.
type linkInfo struct {
symbols map[string]bool
@@ -258,14 +263,11 @@ func epilogueBytes(size int) []byte {
}
func subSP(size int) []byte { // SUBQ $size, SP
// imm8 holds -128..127; anything larger takes the imm32 form, exactly as
// the Go assembler encodes it (verified for 8, 128, 200 and 255).
if size >= -128 && size <= 127 {
return []byte{0x48, 0x83, 0xEC, byte(int8(size))}
}
// 128..255 do not fit SUB's unsigned imm8, but the Go assembler
// switches to ADDQ $-size, SP whose sign-extended imm8 does.
if size >= -255 && size <= 255 {
return []byte{0x48, 0x83, 0xC4, byte(int8(-size))}
}
return append([]byte{0x48, 0x81, 0xEC}, le32(int64(size))...)
}