@@ -29,7 +29,7 @@ func Xreg2num(arg interface{}) (int, bool) {
2929 if ! ok {
3030 return 0 , false
3131 }
32- ndx = aa . Reg ( n )
32+ ndx = n
3333 default :
3434 return 0 , false
3535 }
@@ -46,7 +46,9 @@ func Xreg2num(arg interface{}) (int, bool) {
4646
4747// DecodeRegister converts the result of calling Reg.String()
4848// into the initial register's value.
49- func DecodeRegister (reg string ) (uint64 , bool ) {
49+ func DecodeRegister (reg string ) (aa.Reg , bool ) {
50+ const maxRegister = uint64 (aa .V31 )
51+
5052 // This function is essentially just the inverse
5153 // of https://cs.opensource.google/go/x/arch/+/fc48f9fe:arm64/arm64asm/inst.go;l=335
5254 length := len (reg )
@@ -65,7 +67,10 @@ func DecodeRegister(reg string) (uint64, bool) {
6567 if err != nil {
6668 return 0 , false
6769 }
68- return val , true
70+ if val > maxRegister {
71+ return 0 , false
72+ }
73+ return aa .Reg (val ), true
6974 }
7075
7176 // Otherwise, we want to strip out the
@@ -98,16 +103,20 @@ func DecodeRegister(reg string) (uint64, bool) {
98103 return 0 , false
99104 }
100105
101- return val + regOffset , true
106+ res := val + regOffset
107+ if res > maxRegister {
108+ return 0 , false
109+ }
110+ return aa .Reg (res ), true
102111}
103112
104113// DecodeImmediate converts an arm64asm Arg of immediate type to it's value.
105- func DecodeImmediate (arg aa.Arg ) (uint64 , bool ) {
114+ func DecodeImmediate (arg aa.Arg ) (int64 , bool ) {
106115 switch val := arg .(type ) {
107116 case aa.Imm :
108- return uint64 (val .Imm ), true
117+ return int64 (val .Imm ), true
109118 case aa.PCRel :
110- return uint64 (val ), true
119+ return int64 (val ), true
111120 case aa.MemImmediate :
112121 // The MemImmediate layout changes quite
113122 // a bit depending on its mode.
@@ -139,13 +148,13 @@ func DecodeImmediate(arg aa.Arg) (uint64, bool) {
139148 // Note that the second %s here is the print
140149 // format from a register. Annoyingly this isn't a
141150 // register type, so we have to unwind it manually
142- val , err := DecodeRegister (fields [1 ])
143- if ! err {
151+ reg , ok := DecodeRegister (fields [1 ])
152+ if ! ok {
144153 return 0 , false
145154 }
146155 // The Go disassembler always adds X0 here.
147156 // See https://cs.opensource.google/go/x/arch/+/fc48f9fe:arm64/arm64asm/inst.go;l=526
148- return val - uint64 ( aa .X0 ), true
157+ return int64 ( reg - aa .X0 ), true
149158 }
150159
151160 // Otherwise all of the strings end with a ], so we just parse
@@ -156,12 +165,12 @@ func DecodeImmediate(arg aa.Arg) (uint64, bool) {
156165 if err != nil {
157166 return 0 , false
158167 }
159- return uint64 ( out ) , true
168+ return out , true
160169
161170 case aa.ImmShift :
162171 // Sadly, ImmShift{} does not have public fields.
163172 // https://github.com/golang/go/issues/51517
164- var imm uint64
173+ var imm int64
165174 n , err := fmt .Sscanf (val .String (), "#%v" , & imm )
166175 if err != nil || n != 1 {
167176 return 0 , false
0 commit comments