-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patharm_lower_branch.go
More file actions
228 lines (222 loc) · 6.75 KB
/
Copy patharm_lower_branch.go
File metadata and controls
228 lines (222 loc) · 6.75 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package plan9asm
import (
"fmt"
"strings"
)
type armEmitBr func(target string)
type armEmitCondBr func(cond string, target string, fall string) error
func (c *armCtx) lowerBranch(bi int, op, cond string, ins Instr, emitBr armEmitBr, emitCondBr armEmitCondBr) (ok bool, terminated bool, err error) {
switch op {
case "JMP":
op = "B"
}
switch op {
case "BL", "CALL":
if cond != "" {
return true, false, fmt.Errorf("arm %s.%s unsupported: %q", op, cond, ins.Raw)
}
if len(ins.Args) != 1 {
return true, false, fmt.Errorf("arm %s expects 1 operand: %q", op, ins.Raw)
}
switch ins.Args[0].Kind {
case OpReg:
addr, err := c.loadReg(ins.Args[0].Reg)
if err != nil {
return true, false, err
}
fmt.Fprintf(c.b, " call void asm sideeffect %q, %q(i32 %s)\n", "blx $0", "r,~{memory}", addr)
return true, false, nil
case OpMem:
addr, _, _, err := c.addrI32(ins.Args[0].Mem, false)
if err != nil {
return true, false, err
}
fmt.Fprintf(c.b, " call void asm sideeffect %q, %q(i32 %s)\n", "blx $0", "r,~{memory}", addr)
return true, false, nil
case OpSym:
return true, false, c.callSym(ins.Args[0])
default:
return true, false, fmt.Errorf("arm %s invalid target: %q", op, ins.Raw)
}
case "B":
if len(ins.Args) != 1 {
return true, false, fmt.Errorf("arm B expects 1 operand: %q", ins.Raw)
}
if cond != "" {
tgt, ok := armBranchTarget(ins.Args[0])
if !ok {
return true, false, fmt.Errorf("arm B.%s invalid target: %q", cond, ins.Raw)
}
if bi+1 >= len(c.blocks) {
return true, false, fmt.Errorf("arm B.%s needs fallthrough block: %q", cond, ins.Raw)
}
if err := emitCondBr(cond, tgt, c.blocks[bi+1].name); err != nil {
return true, false, err
}
return true, true, nil
}
if ins.Args[0].Kind == OpSym && strings.HasSuffix(ins.Args[0].Sym, "(SB)") {
return true, true, c.tailCallAndRet(ins.Args[0])
}
tgt, ok := armBranchTarget(ins.Args[0])
if !ok {
return true, false, fmt.Errorf("arm B invalid target: %q", ins.Raw)
}
emitBr(tgt)
return true, true, nil
case "BEQ", "BNE", "BLT", "BGE", "BGT", "BLE", "BHS", "BHI", "BLS", "BLO", "BCC", "BCS", "BMI":
if len(ins.Args) != 1 {
return true, false, fmt.Errorf("arm %s expects label: %q", op, ins.Raw)
}
tgt, ok := armBranchTarget(ins.Args[0])
if !ok {
return true, false, fmt.Errorf("arm %s invalid target: %q", op, ins.Raw)
}
if bi+1 >= len(c.blocks) {
return true, false, fmt.Errorf("arm %s needs fallthrough block: %q", op, ins.Raw)
}
branchCond := op[1:]
if branchCond == "CS" {
branchCond = "HS"
}
if branchCond == "CC" {
branchCond = "LO"
}
if err := emitCondBr(branchCond, tgt, c.blocks[bi+1].name); err != nil {
return true, false, err
}
return true, true, nil
}
return false, false, nil
}
func (c *armCtx) castI32RegToArg(v string, to LLVMType) (string, error) {
switch to {
case I32:
return v, nil
case I16, I8, I1:
t := c.newTmp()
fmt.Fprintf(c.b, " %%%s = trunc i32 %s to %s\n", t, v, to)
return "%" + t, nil
case Ptr:
t := c.newTmp()
fmt.Fprintf(c.b, " %%%s = inttoptr i32 %s to ptr\n", t, v)
return "%" + t, nil
case I64:
t := c.newTmp()
fmt.Fprintf(c.b, " %%%s = zext i32 %s to i64\n", t, v)
return "%" + t, nil
default:
return "", fmt.Errorf("unsupported arg type %s", to)
}
}
func (c *armCtx) tailCallAndRet(symOp Operand) error {
if symOp.Kind != OpSym {
return fmt.Errorf("arm tailcall expects sym operand, got %s", symOp.String())
}
s := strings.TrimSpace(symOp.Sym)
if !strings.HasSuffix(s, "(SB)") {
return fmt.Errorf("arm tailcall expects (SB) symbol, got %q", s)
}
callee := c.resolve(strings.TrimSuffix(s, "(SB)"))
csig, ok := c.sigs[callee]
if !ok {
// Cross-package trampoline (e.g. sync/atomic -> internal/runtime/atomic).
// If we don't have an explicit signature, fall back to caller signature.
csig = c.sig
csig.Name = callee
}
callee = funcSigSymbol(callee, csig)
args := make([]string, 0, len(csig.Args))
for i := 0; i < len(csig.Args); i++ {
r := Reg(fmt.Sprintf("R%d", i))
if i < len(csig.ArgRegs) {
r = csig.ArgRegs[i]
}
v, err := c.loadReg(r)
if err != nil {
return err
}
val, err := c.castI32RegToArg(v, csig.Args[i])
if err != nil {
return fmt.Errorf("arm tailcall %q unsupported arg type %s", callee, csig.Args[i])
}
args = append(args, fmt.Sprintf("%s %s", csig.Args[i], val))
}
if csig.Ret == Void {
fmt.Fprintf(c.b, " call void %s(%s)\n", llvmGlobal(callee), strings.Join(args, ", "))
if len(c.fpResults) > 0 {
return c.lowerRET()
}
if c.sig.Ret == Void {
c.b.WriteString(" ret void\n")
return nil
}
fmt.Fprintf(c.b, " ret %s %s\n", c.sig.Ret, llvmZeroValue(c.sig.Ret))
return nil
}
t := c.newTmp()
fmt.Fprintf(c.b, " %%%s = call %s %s(%s)\n", t, csig.Ret, llvmGlobal(callee), strings.Join(args, ", "))
if c.sig.Ret != csig.Ret {
return fmt.Errorf("arm tailcall return mismatch: caller %s callee %s", c.sig.Ret, csig.Ret)
}
fmt.Fprintf(c.b, " ret %s %%%s\n", c.sig.Ret, t)
return nil
}
func (c *armCtx) callSym(symOp Operand) error {
if symOp.Kind != OpSym {
return fmt.Errorf("arm call expects sym operand, got %s", symOp.String())
}
s := strings.TrimSpace(symOp.Sym)
if !strings.HasSuffix(s, "(SB)") {
return fmt.Errorf("arm call expects (SB) symbol, got %q", s)
}
callee := c.resolve(strings.TrimSuffix(s, "(SB)"))
if callee == "runtime.entersyscall" || callee == "runtime.exitsyscall" {
return nil
}
csig, ok := c.sigs[callee]
if !ok {
csig = FuncSig{Name: callee, Ret: Void}
}
callee = funcSigSymbol(callee, csig)
args := make([]string, 0, len(csig.Args))
for i := 0; i < len(csig.Args); i++ {
r := Reg(fmt.Sprintf("R%d", i))
if i < len(csig.ArgRegs) {
r = csig.ArgRegs[i]
}
v, err := c.loadReg(r)
if err != nil {
return err
}
val, err := c.castI32RegToArg(v, csig.Args[i])
if err != nil {
return fmt.Errorf("arm call %q unsupported arg type %s", callee, csig.Args[i])
}
args = append(args, fmt.Sprintf("%s %s", csig.Args[i], val))
}
if csig.Ret == Void {
fmt.Fprintf(c.b, " call void %s(%s)\n", llvmGlobal(callee), strings.Join(args, ", "))
return nil
}
t := c.newTmp()
fmt.Fprintf(c.b, " %%%s = call %s %s(%s)\n", t, csig.Ret, llvmGlobal(callee), strings.Join(args, ", "))
switch csig.Ret {
case I32:
return c.storeReg(Reg("R0"), "%"+t)
case I16, I8, I1:
z := c.newTmp()
fmt.Fprintf(c.b, " %%%s = zext %s %%%s to i32\n", z, csig.Ret, t)
return c.storeReg(Reg("R0"), "%"+z)
case Ptr:
p := c.newTmp()
fmt.Fprintf(c.b, " %%%s = ptrtoint ptr %%%s to i32\n", p, t)
return c.storeReg(Reg("R0"), "%"+p)
case I64:
tr := c.newTmp()
fmt.Fprintf(c.b, " %%%s = trunc i64 %%%s to i32\n", tr, t)
return c.storeReg(Reg("R0"), "%"+tr)
default:
return fmt.Errorf("arm call %q unsupported return type %s", callee, csig.Ret)
}
}