-
Notifications
You must be signed in to change notification settings - Fork 39
/
cmp_ccmp.c
207 lines (187 loc) · 6.31 KB
/
cmp_ccmp.c
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
/*
* cmp/ccmp
*
* Copyright (c) 2015 xerub
*/
#include <assert.h>
#include <stdio.h>
#include <string.h>
/*
CMP A, B
CCMP C, D, NZCV, COND1
B.COND2 label
nzcv = cmp(A, B);
if (COND1(nzcv)) {
nzcv = cmp(C, D);
} else {
nzcv = NZCV
}
if (COND2(nzcv)) {
goto label
}
if (COND1(A, B)) {
if (COND2(C, D)) {
goto label
}
} else {
if (COND2(NZCV)) {
goto label
}
}
*/
#define FLAG_N (1 << 3)
#define FLAG_Z (1 << 2)
#define FLAG_C (1 << 1)
#define FLAG_V (1 << 0)
struct condition_t {
const char *name;
unsigned int mask;
unsigned int result1;
unsigned int result2;
int inverse;
char *templ;
} tab[] = {
/* flags tested (mask) (must equal this) (or this) */
{ "eq", FLAG_Z, FLAG_Z, FLAG_Z, 0, "%s == %s" },
{ "ne", FLAG_Z, FLAG_Z, FLAG_Z, 1, "%s != %s" },
{ "cs", FLAG_C, FLAG_C, FLAG_C, 0, "(unsigned)%s >= %s" },
{ "cc", FLAG_C, FLAG_C, FLAG_C, 1, "(unsigned)%s < %s" },
{ "hi", FLAG_Z | FLAG_C, FLAG_C, FLAG_C, 0, "(unsigned)%s > %s" },
{ "ls", FLAG_Z | FLAG_C, FLAG_C, FLAG_C, 1, "(unsigned)%s <= %s" },
{ "ge", FLAG_N | FLAG_V, FLAG_N|FLAG_V, 0, 0, "(signed)%s >= %s" },
{ "lt", FLAG_N | FLAG_V, FLAG_N|FLAG_V, 0, 1, "(signed)%s < %s" },
{ "gt", FLAG_N | FLAG_Z | FLAG_V, FLAG_N|FLAG_V, 0, 0, "(signed)%s > %s" },
{ "le", FLAG_N | FLAG_Z | FLAG_V, FLAG_N|FLAG_V, 0, 1, "(signed)%s <= %s" },
{ NULL, 0, 0, 0, 0, NULL }
};
/*
eq Equal. Z==1
ne Not equal. Z==0
cs/hs Unsigned higher or same (or carry set). C==1
cc/lo Unsigned lower (or carry clear). C==0
mi Negative. The mnemonic stands for "minus". N==1
pl Positive or zero. The mnemonic stands for "plus". N==0
vs Signed overflow. The mnemonic stands for "V set". V==1
vc No signed overflow. The mnemonic stands for "V clear". V==0
hi Unsigned higher. (C==1) && (Z==0)
ls Unsigned lower or same. (C==0) || (Z==1)
ge Signed greater than or equal. N==V
lt Signed less than. N!=V
gt Signed greater than. (Z==0) && (N==V)
le Signed less than or equal. (Z==1) || (N!=V)
*/
static const struct condition_t *
flipcond(const struct condition_t *c)
{
if (c->inverse) {
return --c;
}
return ++c;
}
static const struct condition_t *
getcond(const char *name)
{
struct condition_t *c;
for (c = tab; c->name; c++) {
if (!strcasecmp(c->name, name)) {
return c;
}
}
return NULL;
}
static int
evalcond(const char *name, unsigned int nzcv)
{
struct condition_t *c;
for (c = tab; c->name; c++) {
if (!strcasecmp(c->name, name)) {
/* found condition */
unsigned int r = nzcv & c->mask;
int result = (r == c->result1 || r == c->result2);
if (result ^ c->inverse) {
return 1;
}
return 0;
}
}
return -1;
}
static int
pr1(const char *A,
const char *B,
const char *C,
const char *D,
unsigned int NZCV,
const char *COND1,
const char *COND2,
const char *label)
{
const struct condition_t *c1 = getcond(COND1);
const struct condition_t *c2 = getcond(COND2);
int e = evalcond(COND2, NZCV);
assert(c1 && c2 && e >= 0);
printf("if (");
printf(c1->templ, A, B);
printf(") {\n");
printf("\tif (");
printf(c2->templ, C, D);
printf(") {\n");
printf("\t\tgoto %s;\n", label);
printf("\t}\n");
if (e) {
printf("} else {\n");
printf("\tgoto %s;\n", label);
}
printf("}\n");
return 0;
}
static int
qr1(const char *A,
const char *B,
const char *C,
const char *D,
unsigned int NZCV,
const char *COND1,
const char *COND2,
const char *label)
{
const struct condition_t *c1 = getcond(COND1);
const struct condition_t *c2 = getcond(COND2);
int e = evalcond(COND2, NZCV);
assert(c1 && c2 && e >= 0);
printf("if (");
if (e) {
c1 = flipcond(c1);
printf(c1->templ, A, B);
printf(" || ");
} else {
printf(c1->templ, A, B);
printf(" && ");
}
printf(c2->templ, C, D);
printf(") goto %s;\n", label);
return 0;
}
int
main(void)
{
/*
CMP W12, W9
CCMP W2, #0, #0, HI
B.EQ label
*/
pr1("W12", "W9", "W2", "0", 0, "HI", "EQ", "label");
printf("-\n");
qr1("W12", "W9", "W2", "0", 0, "HI", "EQ", "label");
printf("==\n");
/*
CMP W11, #0x80
CCMP W11, #0x1F, #0, NE
B.CC label
*/
pr1("W11", "0x80", "W11", "0x1F", 0, "NE", "CC", "label");
printf("-\n");
qr1("W11", "0x80", "W11", "0x1F", 0, "NE", "CC", "label");
printf("==\n");
return 0;
}