-
Notifications
You must be signed in to change notification settings - Fork 0
/
assemble.go
165 lines (157 loc) · 3.97 KB
/
assemble.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
package arm64
import (
"errors"
)
var ErrInvalidOperand = errors.New("invalid operand")
// Assemble assembles an instruction.
func Assemble(i *Instruction) (uint32, error) {
switch m := i.Mnemonic; m {
case ADC, ADCS, SBC, SBCS:
rd, rn, rm, err := unpackRegister3(i)
if err != nil {
return 0, err
}
if i.Operands[3] != nil || !rn.Type().GP() {
return 0, ErrInvalidOperand
}
sf := flag(rd.Type() == X)
op := flag(m == SBC || m == SBCS)
s := flag(m == ADCS || m == SBCS)
return encode_addsub_carry(sf, op, s, rm.Index(), rn.Index(), rd.Index())
case ADD, ADDS, SUB, SUBS:
rd, rn, err := unpackRegister2(i)
if err != nil {
return 0, err
}
if i.Operands[3] != nil {
return 0, ErrInvalidOperand
}
sf := flag(rd.Type() == X)
op := flag(m == SUB || m == SUBS)
s := flag(m == ADDS || m == SUBS)
switch r := i.Operands[2].(type) {
case ExtShiftRegister:
if !rn.Type().GP() || r.Amount > 4 {
return 0, ErrInvalidOperand
}
option := r.ExtShift
if option == LSL {
option = UXTW
}
return encode_addsub_ext(sf, op, s, 0, r.Index(), uint32(option), uint32(r.Amount), rn.Index(), rd.Index())
case Immediate:
if r < 0 || r > 0xFFF {
return 0, ErrInvalidOperand
}
return encode_addsub_imm(sf, op, s, 0, uint32(r), rn.Index(), rd.Index())
case ExtShiftImmediate:
if r.Immediate < 0 || r.Immediate > 0xFFF {
return 0, ErrInvalidOperand
}
if r.ExtShift != LSL || r.Amount != 12 {
return 0, ErrInvalidOperand
}
return encode_addsub_imm(sf, op, s, 1, uint32(r.Immediate), rn.Index(), rd.Index())
default:
return 0, ErrInvalidOperand
}
case CMN, CMP:
rn, err := unpackRegister(i.Operands[0])
if err != nil {
return 0, err
}
sf := flag(rn.Type() == X)
op := flag(m == CMP)
switch r := i.Operands[1].(type) {
case ExtShiftRegister:
if !rn.Type().GP() || r.Amount > 4 {
return 0, ErrInvalidOperand
}
option := r.ExtShift
if option == LSL {
option = UXTW
}
return encode_addsub_ext(sf, op, 1, 0, r.Index(), uint32(option), uint32(r.Amount), rn.Index(), 31)
case Immediate:
if r < 0 || r > 0xFFF {
return 0, ErrInvalidOperand
}
return encode_addsub_imm(sf, op, 1, 0, uint32(r), rn.Index(), 31)
case ExtShiftImmediate:
if r.Immediate < 0 || r.Immediate > 0xFFF {
return 0, ErrInvalidOperand
}
if r.ExtShift != LSL || r.Amount != 12 {
return 0, ErrInvalidOperand
}
return encode_addsub_imm(sf, op, 1, 1, uint32(r.Immediate), rn.Index(), 31)
default:
return 0, ErrNotImplemented
}
case MOV:
rd, rn, err := unpackRegister2(i)
if err != nil {
return 0, err
}
sf := flag(rd.Type() == X)
if rd.Index() == 31 || rn.Index() == 31 {
return encode_addsub_imm(sf, 0, 0, 0, 0, rn.Index(), rd.Index())
}
return 0, ErrNotImplemented
case NGC, NGCS:
rn, rm, err := unpackRegister2(i)
if err != nil {
return 0, err
}
if i.Operands[2] != nil || !rn.Type().GP() {
return 0, ErrInvalidOperand
}
sf := flag(rn.Type() == X)
s := flag(m == NGCS)
return encode_addsub_carry(sf, 1, s, rm.Index(), rn.Index(), 31)
}
return 0, ErrNotImplemented
}
func flag(b bool) uint32 {
if b {
return 1
}
return 0
}
func unpackRegister(op Operand) (Register, error) {
switch r := op.(type) {
case Register:
return r, nil
case ExtShiftRegister:
return r.Register, nil
default:
return 0, ErrInvalidOperand
}
}
func unpackRegister2(i *Instruction) (a, b Register, err error) {
if a, err = unpackRegister(i.Operands[0]); err != nil {
return
}
if b, err = unpackRegister(i.Operands[1]); err != nil {
return
}
if a.Type() != b.Type() {
err = ErrInvalidOperand
}
return
}
func unpackRegister3(i *Instruction) (a, b, c Register, err error) {
if a, err = unpackRegister(i.Operands[0]); err != nil {
return
}
if b, err = unpackRegister(i.Operands[1]); err != nil {
return
}
if c, err = unpackRegister(i.Operands[2]); err != nil {
return
}
if a.Type() != b.Type() || b.Type() != c.Type() {
err = ErrInvalidOperand
}
return
}