-
Notifications
You must be signed in to change notification settings - Fork 9
/
operand.go
189 lines (177 loc) · 4.77 KB
/
operand.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package insts
import (
"fmt"
"math"
"strings"
)
// Operand types
const (
InvalidOperantType = iota
RegOperand
FloatOperand
IntOperand
LiteralConstant
)
// OperandType represents the type of an operand. It can be Reg, Float, Int ...
type OperandType int
// An Operand is an operand
type Operand struct {
Code int
OperandType OperandType
Register *Reg
RegCount int // for cases like v[0:3]
FloatValue float64
IntValue int64
LiteralConstant uint32
}
// NewRegOperand returns a new operand of register type
func NewRegOperand(code int, reg RegType, count int) *Operand {
o := new(Operand)
o.Code = code
o.OperandType = RegOperand
o.Register = Regs[reg]
o.RegCount = count
return o
}
// NewSRegOperand returns a new operand of s register type
func NewSRegOperand(code int, index int, count int) *Operand {
o := new(Operand)
o.Code = code
o.OperandType = RegOperand
o.Register = Regs[S0+RegType(index)]
o.RegCount = count
return o
}
// NewVRegOperand returns a new operand of v register type
func NewVRegOperand(code int, index int, count int) *Operand {
o := new(Operand)
o.Code = code
o.OperandType = RegOperand
o.Register = Regs[V0+RegType(index)]
o.RegCount = count
return o
}
// NewIntOperand returns a new operand of an integer type
func NewIntOperand(code int, value int64) *Operand {
o := new(Operand)
o.Code = code
o.OperandType = IntOperand
o.IntValue = value
return o
}
// NewFloatOperand returns a new operand of an floating point type
func NewFloatOperand(code int, value float64) *Operand {
o := new(Operand)
o.Code = code
o.OperandType = FloatOperand
o.FloatValue = value
return o
}
func (o Operand) String() string {
switch o.OperandType {
case RegOperand:
return o.regOperandToString()
case IntOperand:
return fmt.Sprintf("%d", o.IntValue)
case FloatOperand:
switch o.FloatValue {
case 1.0:
return "1.0"
case 0.5:
return "0.5"
case -1.0:
return "-1.0"
default:
return fmt.Sprintf("%.06f", o.FloatValue)
}
case LiteralConstant:
return fmt.Sprintf("0x%x", o.LiteralConstant)
default:
return ""
}
}
func (o Operand) regOperandToString() string {
if o.RegCount > 1 {
if o.Register.IsSReg() {
return fmt.Sprintf("s[%d:%d]",
o.Register.RegIndex(), o.Register.RegIndex()+o.RegCount-1)
} else if o.Register.IsVReg() {
return fmt.Sprintf("v[%d:%d]",
o.Register.RegIndex(), o.Register.RegIndex()+o.RegCount-1)
} else if strings.Contains(o.Register.Name, "lo") {
return o.Register.Name[:len(o.Register.Name)-2]
}
return fmt.Sprintf("<unknown: %+v>", o.Register)
}
return o.Register.Name
}
//nolint:gocyclo,funlen
func getOperand(num uint16) (*Operand, error) {
code := int(num)
switch {
case num >= 0 && num <= 101:
return NewSRegOperand(code, code, 0), nil
case num == 102:
return NewRegOperand(code, FlatSratchLo, 0), nil
case num == 103:
return NewRegOperand(code, FlatSratchHi, 0), nil
case num == 104:
return NewRegOperand(code, XnackMaskLo, 0), nil
case num == 105:
return NewRegOperand(code, XnackMaskHi, 0), nil
case num == 106:
return NewRegOperand(code, VCCLO, 0), nil
case num == 107:
return NewRegOperand(code, VCCHI, 0), nil
case num == 108:
return NewRegOperand(code, TbaLo, 0), nil
case num == 109:
return NewRegOperand(code, TbaHi, 0), nil
case num == 110:
return NewRegOperand(code, TmaLo, 0), nil
case num == 111:
return NewRegOperand(code, TmaHi, 0), nil
case num >= 112 && num < 123:
return NewRegOperand(code, Timp0+RegType(num-112), 0), nil
case num == 124:
return NewRegOperand(code, M0, 0), nil
case num == 126:
return NewRegOperand(code, EXECLO, 0), nil
case num == 127:
return NewRegOperand(code, EXECHI, 0), nil
case num >= 128 && num <= 192:
return NewIntOperand(code, int64(num)-128), nil
case num >= 193 && num <= 208:
return NewIntOperand(code, 192-int64(num)), nil
case num == 240:
return NewFloatOperand(code, 0.5), nil
case num == 241:
return NewFloatOperand(code, -0.5), nil
case num == 242:
return NewFloatOperand(code, 1.0), nil
case num == 243:
return NewFloatOperand(code, -1.0), nil
case num == 244:
return NewFloatOperand(code, 2.0), nil
case num == 245:
return NewFloatOperand(code, -2.0), nil
case num == 246:
return NewFloatOperand(code, 4.0), nil
case num == 247:
return NewFloatOperand(code, -4.0), nil
case num == 248:
return NewFloatOperand(code, 1.0/(2.0*math.Pi)), nil
case num == 251:
return NewRegOperand(code, VCCZ, 0), nil
case num == 252:
return NewRegOperand(code, EXECZ, 0), nil
case num == 253:
return NewRegOperand(code, SCC, 0), nil
case num == 255:
return &Operand{code, LiteralConstant, nil, 0, 0, 0, 0}, nil
case num >= 256 && num <= 511:
return NewVRegOperand(code, int(num)-256, 0), nil
default:
return nil, fmt.Errorf("cannot find Operand %d", num)
}
}