ast: {Input,Output}Registers()

This commit is contained in:
Michael McLoughlin
2018-12-02 22:29:30 -08:00
parent 59548ee9f6
commit 7d4e18f4f4
4 changed files with 84 additions and 3 deletions

View File

@@ -12,8 +12,8 @@ type Op interface {
type Mem struct {
Disp int
Base reg.Physical
Index reg.Physical
Base reg.Register
Index reg.Register
Scale uint8
}
@@ -50,3 +50,23 @@ type LabelRef string
func (l LabelRef) Asm() string {
return string(l)
}
// Registers returns the list of all operands involved in the given operand.
func Registers(op Op) []reg.Register {
switch op := op.(type) {
case reg.Register:
return []reg.Register{op}
case Mem:
var r []reg.Register
if op.Base != nil {
r = append(r, op.Base)
}
if op.Index != nil {
r = append(r, op.Index)
}
return r
case Imm, Rel, LabelRef:
return nil
}
panic("unknown operand type")
}