56 lines
1.7 KiB
Go
56 lines
1.7 KiB
Go
// Copyright (c) 2026 Petr Balvín <opensource@petrbalvin.org> (https://petrbalvin.org)
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
package asm
|
|
|
|
// Operand is an instruction operand: a Reg, a Mem reference or an Imm value.
|
|
type Operand interface {
|
|
isOperand()
|
|
}
|
|
|
|
// Imm is an immediate value. Its encoded width is chosen by the instruction
|
|
// (sign-extended imm8 where possible, otherwise imm32, imm64 for MOV).
|
|
type Imm int64
|
|
|
|
func (Imm) isOperand() {}
|
|
|
|
// Mem is a memory operand of the form disp(base)(index*scale).
|
|
type Mem struct {
|
|
Base Reg
|
|
Index Reg
|
|
Scale int // 1, 2, 4 or 8; 0 means no index
|
|
Disp int64
|
|
Size int // operand width in bytes
|
|
HasBase bool
|
|
HasIndex bool
|
|
}
|
|
|
|
func (Mem) isOperand() {}
|
|
|
|
// Ptr builds a plain displaced memory operand (base)+disp of the given size.
|
|
func Ptr(base Reg, disp int64, size int) Mem {
|
|
return Mem{Base: base, Disp: disp, Size: size, HasBase: true}
|
|
}
|
|
|
|
// Idx builds an indexed memory operand disp(base)(index*scale).
|
|
func Idx(base, index Reg, scale int, disp int64, size int) Mem {
|
|
return Mem{Base: base, Index: index, Scale: scale, Disp: disp, Size: size, HasBase: true, HasIndex: true}
|
|
}
|
|
|
|
// Rip builds a RIP-relative memory operand (RIP)+disp.
|
|
func Rip(disp int64, size int) Mem {
|
|
return Mem{Disp: disp, Size: size}
|
|
}
|
|
|
|
// sbMem is a memory operand that references a static (SB) symbol. It encodes
|
|
// as a RIP-relative reference with a placeholder displacement; the encoder
|
|
// records a patch site so the file-level layout can fill in the true rel32
|
|
// once the symbol's address is known.
|
|
type sbMem struct {
|
|
size int
|
|
name string // static symbol name (the GLOBL identifier)
|
|
addend int64 // byte offset within the symbol
|
|
}
|
|
|
|
func (sbMem) isOperand() {}
|