| 1 | package operand |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | |
| 6 | "github.com/mmcloughlin/avo/reg" |
| 7 | ) |
| 8 | |
| 9 | // Op is an operand. |
| 10 | type Op interface { |
| 11 | Asm() string |
| 12 | } |
| 13 | |
| 14 | // Symbol represents a symbol name. |
| 15 | type Symbol struct { |
| 16 | Name string |
| 17 | Static bool // only visible in current source file |
| 18 | } |
| 19 | |
| 20 | // NewStaticSymbol builds a static Symbol. Static symbols are only visible in the current source file. |
| 21 | func NewStaticSymbol(name string) Symbol { |
| 22 | return Symbol{Name: name, Static: true} |
| 23 | } |
| 24 | |
| 25 | func (s Symbol) String() string { |
| 26 | n := s.Name |
| 27 | if s.Static { |
| 28 | n += "<>" |
| 29 | } |
| 30 | return n |
| 31 | } |
| 32 | |
| 33 | // Mem represents a memory reference. |
| 34 | type Mem struct { |
| 35 | Symbol Symbol |
| 36 | Disp int |
| 37 | Base reg.Register |
| 38 | Index reg.Register |
| 39 | Scale uint8 |
| 40 | } |
| 41 | |
| 42 | // NewParamAddr is a convenience to build a Mem operand pointing to a function |
| 43 | // parameter, which is a named offset from the frame pointer pseudo register. |
| 44 | func NewParamAddr(name string, offset int) Mem { |
| 45 | return Mem{ |
| 46 | Symbol: Symbol{ |
| 47 | Name: name, |
| 48 | Static: false, |
| 49 | }, |
| 50 | Disp: offset, |
| 51 | Base: reg.FramePointer, |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // NewStackAddr returns a memory reference relative to the stack pointer. |
| 56 | func NewStackAddr(offset int) Mem { |
| 57 | return Mem{ |
| 58 | Disp: offset, |
| 59 | Base: reg.StackPointer, |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | // NewDataAddr returns a memory reference relative to the named data symbol. |
| 64 | func NewDataAddr(sym Symbol, offset int) Mem { |
| 65 | return Mem{ |
| 66 | Symbol: sym, |
| 67 | Disp: offset, |
| 68 | Base: reg.StaticBase, |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // Offset returns a reference to m plus idx bytes. |
| 73 | func (m Mem) Offset(idx int) Mem { |
| 74 | a := m |
| 75 | a.Disp += idx |
| 76 | return a |
| 77 | } |
| 78 | |
| 79 | // Idx returns a new memory reference with (Index, Scale) set to (r, s). |
| 80 | func (m Mem) Idx(r reg.Register, s uint8) Mem { |
| 81 | a := m |
| 82 | a.Index = r |
| 83 | a.Scale = s |
| 84 | return a |
| 85 | } |
| 86 | |
| 87 | // Asm returns an assembly syntax representation of m. |
| 88 | func (m Mem) Asm() string { |
| 89 | a := m.Symbol.String() |
| 90 | if a != "" { |
| 91 | a += fmt.Sprintf("%+d", m.Disp) |
| 92 | } else if m.Disp != 0 { |
| 93 | a += fmt.Sprintf("%d", m.Disp) |
| 94 | } |
| 95 | if m.Base != nil { |
| 96 | a += fmt.Sprintf("(%s)", m.Base.Asm()) |
| 97 | } |
| 98 | if m.Index != nil && m.Scale != 0 { |
| 99 | a += fmt.Sprintf("(%s*%d)", m.Index.Asm(), m.Scale) |
| 100 | } |
| 101 | return a |
| 102 | } |
| 103 | |
| 104 | // Rel is an offset relative to the instruction pointer. |
| 105 | type Rel int32 |
| 106 | |
| 107 | // Asm returns an assembly syntax representation of r. |
| 108 | func (r Rel) Asm() string { |
| 109 | return fmt.Sprintf(".%+d", r) |
| 110 | } |
| 111 | |
| 112 | // LabelRef is a reference to a label. |
| 113 | type LabelRef string |
| 114 | |
| 115 | // Asm returns an assembly syntax representation of l. |
| 116 | func (l LabelRef) Asm() string { |
| 117 | return string(l) |
| 118 | } |
| 119 | |
| 120 | // Registers returns the list of all operands involved in the given operand. |
| 121 | func Registers(op Op) []reg.Register { |
| 122 | switch op := op.(type) { |
| 123 | case reg.Register: |
| 124 | return []reg.Register{op} |
| 125 | case Mem: |
| 126 | var r []reg.Register |
| 127 | if op.Base != nil { |
| 128 | r = append(r, op.Base) |
| 129 | } |
| 130 | if op.Index != nil { |
| 131 | r = append(r, op.Index) |
| 132 | } |
| 133 | return r |
| 134 | case Constant, Rel, LabelRef: |
| 135 | return nil |
| 136 | } |
| 137 | panic("unknown operand type") |
| 138 | } |
| 139 | |
| 140 | // ApplyAllocation returns an operand with allocated registers replaced. Registers missing from the allocation are left alone. |
| 141 | func ApplyAllocation(op Op, a reg.Allocation) Op { |
| 142 | switch op := op.(type) { |
| 143 | case reg.Register: |
| 144 | return a.LookupRegisterDefault(op) |
| 145 | case Mem: |
| 146 | op.Base = a.LookupRegisterDefault(op.Base) |
| 147 | op.Index = a.LookupRegisterDefault(op.Index) |
| 148 | return op |
| 149 | } |
| 150 | return op |
| 151 | } |