-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharm_flags.go
More file actions
174 lines (167 loc) · 4.72 KB
/
arm_flags.go
File metadata and controls
174 lines (167 loc) · 4.72 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
package plan9asm
import (
"fmt"
"strings"
)
func (c *armCtx) storeFlag(slot string, v string) {
fmt.Fprintf(c.b, " store i1 %s, ptr %s\n", v, slot)
}
func (c *armCtx) storeFlagCond(cond, slot, v string) error {
if cond == "" || strings.EqualFold(cond, "AL") {
c.storeFlag(slot, v)
return nil
}
cv, err := c.condValue(cond)
if err != nil {
return err
}
oldV := c.newTmp()
sel := c.newTmp()
fmt.Fprintf(c.b, " %%%s = load i1, ptr %s\n", oldV, slot)
fmt.Fprintf(c.b, " %%%s = select i1 %s, i1 %s, i1 %%%s\n", sel, cv, v, oldV)
c.storeFlag(slot, "%"+sel)
return nil
}
func (c *armCtx) setFlagsSub(cond, dst, src, res string) error {
c.flagsWritten = true
z := c.newTmp()
n := c.newTmp()
carry := c.newTmp()
fmt.Fprintf(c.b, " %%%s = icmp eq i32 %s, 0\n", z, res)
fmt.Fprintf(c.b, " %%%s = icmp slt i32 %s, 0\n", n, res)
fmt.Fprintf(c.b, " %%%s = icmp uge i32 %s, %s\n", carry, dst, src)
x1 := c.newTmp()
x2 := c.newTmp()
x3 := c.newTmp()
ov := c.newTmp()
fmt.Fprintf(c.b, " %%%s = xor i32 %s, %s\n", x1, dst, src)
fmt.Fprintf(c.b, " %%%s = xor i32 %s, %s\n", x2, dst, res)
fmt.Fprintf(c.b, " %%%s = and i32 %%%s, %%%s\n", x3, x1, x2)
fmt.Fprintf(c.b, " %%%s = icmp slt i32 %%%s, 0\n", ov, x3)
if err := c.storeFlagCond(cond, c.flagsZSlot, "%"+z); err != nil {
return err
}
if err := c.storeFlagCond(cond, c.flagsNSlot, "%"+n); err != nil {
return err
}
if err := c.storeFlagCond(cond, c.flagsCSlot, "%"+carry); err != nil {
return err
}
return c.storeFlagCond(cond, c.flagsVSlot, "%"+ov)
}
func (c *armCtx) setFlagsAdd(cond, dst, src, res string) error {
c.flagsWritten = true
z := c.newTmp()
n := c.newTmp()
carry := c.newTmp()
fmt.Fprintf(c.b, " %%%s = icmp eq i32 %s, 0\n", z, res)
fmt.Fprintf(c.b, " %%%s = icmp slt i32 %s, 0\n", n, res)
fmt.Fprintf(c.b, " %%%s = icmp ult i32 %s, %s\n", carry, res, dst)
x1 := c.newTmp()
nx1 := c.newTmp()
x2 := c.newTmp()
x3 := c.newTmp()
ov := c.newTmp()
fmt.Fprintf(c.b, " %%%s = xor i32 %s, %s\n", x1, dst, src)
fmt.Fprintf(c.b, " %%%s = xor i32 %%%s, -1\n", nx1, x1)
fmt.Fprintf(c.b, " %%%s = xor i32 %s, %s\n", x2, dst, res)
fmt.Fprintf(c.b, " %%%s = and i32 %%%s, %%%s\n", x3, nx1, x2)
fmt.Fprintf(c.b, " %%%s = icmp slt i32 %%%s, 0\n", ov, x3)
if err := c.storeFlagCond(cond, c.flagsZSlot, "%"+z); err != nil {
return err
}
if err := c.storeFlagCond(cond, c.flagsNSlot, "%"+n); err != nil {
return err
}
if err := c.storeFlagCond(cond, c.flagsCSlot, "%"+carry); err != nil {
return err
}
return c.storeFlagCond(cond, c.flagsVSlot, "%"+ov)
}
func (c *armCtx) setFlagsLogic(cond, res string) error {
c.flagsWritten = true
z := c.newTmp()
n := c.newTmp()
fmt.Fprintf(c.b, " %%%s = icmp eq i32 %s, 0\n", z, res)
fmt.Fprintf(c.b, " %%%s = icmp slt i32 %s, 0\n", n, res)
if err := c.storeFlagCond(cond, c.flagsZSlot, "%"+z); err != nil {
return err
}
return c.storeFlagCond(cond, c.flagsNSlot, "%"+n)
}
func (c *armCtx) condValue(cond string) (string, error) {
if !c.flagsWritten {
return "", fmt.Errorf("arm: condition %s without any prior flags write", cond)
}
ldN := c.newTmp()
ldZ := c.newTmp()
ldC := c.newTmp()
ldV := c.newTmp()
fmt.Fprintf(c.b, " %%%s = load i1, ptr %s\n", ldN, c.flagsNSlot)
fmt.Fprintf(c.b, " %%%s = load i1, ptr %s\n", ldZ, c.flagsZSlot)
fmt.Fprintf(c.b, " %%%s = load i1, ptr %s\n", ldC, c.flagsCSlot)
fmt.Fprintf(c.b, " %%%s = load i1, ptr %s\n", ldV, c.flagsVSlot)
n := "%" + ldN
z := "%" + ldZ
carry := "%" + ldC
v := "%" + ldV
not := func(x string) string {
t := c.newTmp()
fmt.Fprintf(c.b, " %%%s = xor i1 %s, true\n", t, x)
return "%" + t
}
and := func(a, b string) string {
t := c.newTmp()
fmt.Fprintf(c.b, " %%%s = and i1 %s, %s\n", t, a, b)
return "%" + t
}
or := func(a, b string) string {
t := c.newTmp()
fmt.Fprintf(c.b, " %%%s = or i1 %s, %s\n", t, a, b)
return "%" + t
}
xor := func(a, b string) string {
t := c.newTmp()
fmt.Fprintf(c.b, " %%%s = xor i1 %s, %s\n", t, a, b)
return "%" + t
}
eq := func(a, b string) string {
t := c.newTmp()
fmt.Fprintf(c.b, " %%%s = icmp eq i1 %s, %s\n", t, a, b)
return "%" + t
}
switch strings.ToUpper(cond) {
case "EQ":
return z, nil
case "NE":
return not(z), nil
case "CS", "HS":
return carry, nil
case "CC", "LO":
return not(carry), nil
case "HI":
return and(carry, not(z)), nil
case "LS":
return or(not(carry), z), nil
case "LT":
return xor(n, v), nil
case "GE":
return eq(n, v), nil
case "GT":
return and(not(z), eq(n, v)), nil
case "LE":
return or(z, xor(n, v)), nil
case "MI":
return n, nil
case "PL":
return not(n), nil
case "VS":
return v, nil
case "VC":
return not(v), nil
case "AL":
return "true", nil
default:
return "", fmt.Errorf("arm: unsupported condition %q", cond)
}
}