-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patharm64_flags.go
More file actions
167 lines (150 loc) · 4.39 KB
/
Copy patharm64_flags.go
File metadata and controls
167 lines (150 loc) · 4.39 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
package plan9asm
import (
"fmt"
"strings"
)
func (c *arm64Ctx) storeFlag(slot string, v string) {
fmt.Fprintf(c.b, " store i1 %s, ptr %s\n", v, slot)
}
func (c *arm64Ctx) setFlagsSub(dst, src, res string) {
// NZCV for subtraction:
// Z: res==0
// N: res<0 (signed)
// C: dst >= src (unsigned, no borrow)
// V: signed overflow for dst - src
c.flagsWritten = true
z := c.newTmp()
fmt.Fprintf(c.b, " %%%s = icmp eq i64 %s, 0\n", z, res)
n := c.newTmp()
fmt.Fprintf(c.b, " %%%s = icmp slt i64 %s, 0\n", n, res)
carry := c.newTmp()
fmt.Fprintf(c.b, " %%%s = icmp uge i64 %s, %s\n", carry, dst, src)
// overflow = ((dst ^ src) & (dst ^ res)) < 0
x1 := c.newTmp()
fmt.Fprintf(c.b, " %%%s = xor i64 %s, %s\n", x1, dst, src)
x2 := c.newTmp()
fmt.Fprintf(c.b, " %%%s = xor i64 %s, %s\n", x2, dst, res)
x3 := c.newTmp()
fmt.Fprintf(c.b, " %%%s = and i64 %%%s, %%%s\n", x3, x1, x2)
ov := c.newTmp()
fmt.Fprintf(c.b, " %%%s = icmp slt i64 %%%s, 0\n", ov, x3)
c.storeFlag(c.flagsZSlot, "%"+z)
c.storeFlag(c.flagsNSlot, "%"+n)
c.storeFlag(c.flagsCSlot, "%"+carry)
c.storeFlag(c.flagsVSlot, "%"+ov)
}
func (c *arm64Ctx) setFlagsAdd(dst, src, res string) {
// NZCV for addition:
// Z: res==0
// N: res<0 (signed)
// C: carry out (unsigned overflow) => res < dst
// V: signed overflow for dst + src
c.flagsWritten = true
z := c.newTmp()
fmt.Fprintf(c.b, " %%%s = icmp eq i64 %s, 0\n", z, res)
n := c.newTmp()
fmt.Fprintf(c.b, " %%%s = icmp slt i64 %s, 0\n", n, res)
carry := c.newTmp()
fmt.Fprintf(c.b, " %%%s = icmp ult i64 %s, %s\n", carry, res, dst)
// overflow = (~(dst ^ src) & (dst ^ res)) < 0
x1 := c.newTmp()
fmt.Fprintf(c.b, " %%%s = xor i64 %s, %s\n", x1, dst, src)
nx1 := c.newTmp()
fmt.Fprintf(c.b, " %%%s = xor i64 %%%s, -1\n", nx1, x1)
x2 := c.newTmp()
fmt.Fprintf(c.b, " %%%s = xor i64 %s, %s\n", x2, dst, res)
x3 := c.newTmp()
fmt.Fprintf(c.b, " %%%s = and i64 %%%s, %%%s\n", x3, nx1, x2)
ov := c.newTmp()
fmt.Fprintf(c.b, " %%%s = icmp slt i64 %%%s, 0\n", ov, x3)
c.storeFlag(c.flagsZSlot, "%"+z)
c.storeFlag(c.flagsNSlot, "%"+n)
c.storeFlag(c.flagsCSlot, "%"+carry)
c.storeFlag(c.flagsVSlot, "%"+ov)
}
func (c *arm64Ctx) setFlagsLogic(res string) {
// ANDS-like: update N/Z; set C/V to 0 (good enough for current corpus).
c.flagsWritten = true
z := c.newTmp()
fmt.Fprintf(c.b, " %%%s = icmp eq i64 %s, 0\n", z, res)
n := c.newTmp()
fmt.Fprintf(c.b, " %%%s = icmp slt i64 %s, 0\n", n, res)
c.storeFlag(c.flagsZSlot, "%"+z)
c.storeFlag(c.flagsNSlot, "%"+n)
c.storeFlag(c.flagsCSlot, "false")
c.storeFlag(c.flagsVSlot, "false")
}
func (c *arm64Ctx) condValue(cond string) (string, error) {
if !c.flagsWritten {
return "", fmt.Errorf("%w: arm64 condition %s has no prior flags write", ErrProbeNeedsContext, 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":
return carry, nil
case "HS":
return carry, nil
case "LO":
return not(carry), nil
case "CC":
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
default:
return "", fmt.Errorf("arm64: unsupported condition %q", cond)
}
}