Debian Codegraph @

arch/amd64/order.c

raw symbols SCIP
1 /* $Id: order.c,v 1.19 2017/12/03 17:34:15 ragge Exp $ */
2 /*
3 * Copyright (c) 2008 Michael Shalayeff
4 * Copyright (c) 2003 Anders Magnusson ([email protected]).
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30
31 # include "pass2.h"
32
33 #include <string.h>
34
35 int canaddr(NODE *);
36
37 /* is it legal to make an OREG or NAME entry which has an
38 * offset of off, (from a register of r), if the
39 * resulting thing had type t */
40 int
41 notoff(TWORD t, int r, CONSZ off, char *cp)
42 {
43 if (off > MAX_INT || off < MIN_INT)
44 return 1; /* max signed 32-bit offset */
45 return(0); /* YES */
46 }
47
48 /*
49 * Check if LS and try to make it indexable.
50 * Ignore SCONV to long.
51 * Return 0 if failed.
52 */
53 static int
54 findls(NODE *p, int check)
55 {
56 CONSZ c;
57
58 if (p->n_op == SCONV && p->n_type == LONG && p->n_left->n_type == INT)
59 p = p->n_left; /* Ignore pointless SCONVs here */
60 if (p->n_op != LS || p->n_right->n_op != ICON)
61 return 0;
62 if ((c = getlval(p->n_right)) != 1 && c != 2 && c != 3)
63 return 0;
64 if (check == 1 && p->n_left->n_op != REG)
65 return 0;
66 if (!isreg(p->n_left))
67 (void)geninsn(p->n_left, INAREG);
68 return 1;
69 }
70
71 /*
72 * Turn a UMUL-referenced node into OREG.
73 * Be careful about register classes, this is a place where classes change.
74 *
75 * AMD64 (and i386) have a quite powerful addressing scheme:
76 * : 4(%rax) 4 + %rax
77 * : 4(%rbx,%rax,8) 4 + %rbx + %rax * 8
78 * : 4(,%rax) 4 + %rax * 8
79 * The 8 above can be 1,2,4 or 8.
80 */
81 void
82 offstar(NODE *p, int shape)
83 {
84 NODE *l;
85
86 if (x2debug) {
87 printf("offstar(%p)\n", p);
88 fwalk(p, e2print, 0);
89 }
90
91 if (isreg(p))
92 return; /* Matched (%rax) */
93
94 if (findls(p, 0))
95 return; /* Matched (,%rax,8) */
96
97 if ((p->n_op == PLUS || p->n_op == MINUS) &&
98 p->n_left->n_op == ICON &&
99 p->n_left->n_name[0] == '\0' &&
100 notoff(0, 0, getlval(p->n_left), 0) == 0) {
101 l = p->n_right;
102 if (isreg(l))
103 return; /* Matched 4(%rax) */
104 if (findls(l, 0))
105 return; /* Matched 4(,%rax,8) */
106 if (l->n_op == PLUS && isreg(l->n_right)) {
107 if (findls(l->n_left, 0))
108 return; /* Matched 4(%rbx,%rax,8) */
109 (void)geninsn(l->n_left, INAREG);
110 return; /* Generate 4(%rbx,%rax) */
111 }
112 (void)geninsn(l, INAREG);
113 return; /* Generate 4(%rbx) */
114 }
115
116 if (p->n_op == PLUS) {
117 if (!isreg(p->n_left)) /* ensure right is REG */
118 (void)geninsn(p->n_left, INAREG);
119 if (isreg(p->n_right))
120 return; /* Matched (%rax,%rbx) */
121 if (findls(p->n_right, 0))
122 return; /* Matched (%rax,%rbx,4) */
123 (void)geninsn(p->n_right, INAREG);
124 return; /* Generate (%rbx,%rax) */
125 }
126
127 (void)geninsn(p, INAREG);
128 }
129
130 /*
131 * Do the actual conversion of offstar-found OREGs into real OREGs.
132 * For simple OREGs conversion should already be done.
133 */
134 void
135 myormake(NODE *q)
136 {
137 static int shtbl[] = { 1,2,4,8 };
138 NODE *p, *r;
139 CONSZ c = 0;
140 int r1, r2, sh;
141 int mkconv = 0;
142 char *n = "";
143
144 #define risreg(p) (p->n_op == REG)
145 if (x2debug) {
146 printf("myormake(%p)\n", q);
147 fwalk(q, e2print, 0);
148 }
149 r1 = r2 = MAXREGS;
150 sh = 1;
151
152 r = p = q->n_left;
153
154 if ((p->n_op == PLUS || p->n_op == MINUS) && p->n_left->n_op == ICON) {
155 c = getlval(p->n_left);
156 n = p->n_left->n_name;
157 p = p->n_right;
158 }
159
160 if (p->n_op == PLUS && risreg(p->n_left)) {
161 r1 = regno(p->n_left);
162 p = p->n_right;
163 }
164
165 if (findls(p, 1)) {
166 if (p->n_op == SCONV)
167 p = p->n_left;
168 sh = shtbl[(int)getlval(p->n_right)];
169 r2 = regno(p->n_left);
170 mkconv = 1;
171 } else if (risreg(p)) {
172 r2 = regno(p);
173 mkconv = 1;
174 } //else
175 // comperr("bad myormake tree");
176
177 if (mkconv == 0)
178 return;
179
180 q->n_op = OREG;
181 setlval(q, c);
182 q->n_rval = R2PACK(r1, r2, sh);
183 q->n_name = n;
184 tfree(r);
185 if (x2debug) {
186 printf("myormake converted %p\n", q);
187 fwalk(q, e2print, 0);
188 }
189 }
190
191 /*
192 * Shape matches for UMUL. Cooperates with offstar().
193 */
194 int
195 shumul(NODE *p, int shape)
196 {
197
198 if (x2debug)
199 printf("shumul(%p)\n", p);
200
201 /* Turns currently anything into OREG on x86 */
202 if (shape & SOREG)
203 return SROREG;
204 return SRNOPE;
205 }
206
207 /*
208 * Rewrite operations on binary operators (like +, -, etc...).
209 * Called as a result of table lookup.
210 */
211 int
212 setbin(NODE *p)
213 {
214
215 if (x2debug)
216 printf("setbin(%p)\n", p);
217 return 0;
218
219 }
220
221 /* setup for assignment operator */
222 int
223 setasg(NODE *p, int cookie)
224 {
225 if (x2debug)
226 printf("setasg(%p)\n", p);
227 return(0);
228 }
229
230 /* setup for unary operator */
231 int
232 setuni(NODE *p, int cookie)
233 {
234 return 0;
235 }
236
237 /*
238 * Special handling of some instruction register allocation.
239 */
240 struct rspecial *
241 nspecial(struct optab *q)
242 {
243 switch (q->op) {
244 case SCONV:
245 if ((q->ltype & TINT) &&
246 q->rtype == (TLONGLONG|TULONGLONG|TLONG|TULONG)) {
247 static struct rspecial s[] = {
248 { NLEFT, RAX }, { NRES, RAX }, { 0 } };
249 return s;
250 }
251 break;
252
253 case DIV:
254 {
255 static struct rspecial s[] = {
256 { NEVER, RAX }, { NEVER, RDX },
257 { NLEFT, RAX }, { NRES, RAX },
258 { NORIGHT, RDX }, { NORIGHT, RAX }, { 0 } };
259 return s;
260 }
261 break;
262
263 case MOD:
264 if (q->ltype & TUCHAR) {
265 static struct rspecial s[] = {
266 { NEVER, RAX },
267 { NLEFT, RAX }, { NRES, RAX },
268 { NORIGHT, RAX }, { 0 } };
269 return s;
270 } else {
271 static struct rspecial s[] = {
272 { NEVER, RAX }, { NEVER, RDX },
273 { NLEFT, RAX }, { NRES, RDX },
274 { NORIGHT, RDX }, { NORIGHT, RAX }, { 0 } };
275 return s;
276 }
277 break;
278
279 case STARG:
280 {
281 static struct rspecial s[] = {
282 { NEVER, RDI },
283 { NLEFT, RSI },
284 { NEVER, RCX }, { 0 } };
285 return s;
286 }
287
288 case STASG:
289 {
290 static struct rspecial s[] = {
291 { NEVER, RDI },
292 { NRIGHT, RSI }, { NOLEFT, RSI },
293 { NOLEFT, RCX }, { NORIGHT, RCX },
294 { NEVER, RCX }, { 0 } };
295 return s;
296 }
297
298 case MUL:
299 if (q->lshape == SAREG) {
300 static struct rspecial s[] = {
301 { NEVER, RAX },
302 { NLEFT, RAX }, { NRES, RAX }, { 0 } };
303 return s;
304 }
305 break;
306
307 case LS:
308 case RS:
309 {
310 static struct rspecial s[] = {
311 { NRIGHT, RCX }, { NOLEFT, RCX }, { 0 } };
312 return s;
313 }
314 break;
315
316 default:
317 break;
318 }
319 comperr("nspecial entry %d", q - table);
320 return 0; /* XXX gcc */
321 }
322
323 /*
324 * Set evaluation order of a binary node if it differs from default.
325 */
326 int
327 setorder(NODE *p)
328 {
329 return 0; /* nothing differs on x86 */
330 }
331
332 /*
333 * set registers in calling conventions live.
334 */
335 int *
336 livecall(NODE *p)
337 {
338 static int r[NTEMPREG+1];
339 NODE *q;
340 int cr = 0;
341
342 if (optype(p->n_op) != BITYPE)
343 return r[0] = -1, r;
344
345 for (q = p->n_right; q->n_op == CM; q = q->n_left) {
346 if (q->n_right->n_op == ASSIGN &&
347 q->n_right->n_left->n_op == REG)
348 r[cr++] = regno(q->n_right->n_left);
349 }
350 if (q->n_op == ASSIGN && q->n_left->n_op == REG)
351 r[cr++] = regno(q->n_left);
352 r[cr++] = -1;
353 return r;
354 }
355
356 /*
357 * Signal whether the instruction is acceptable for this target.
358 */
359 int
360 acceptable(struct optab *op)
361 {
362 if (op->visit & MCALL)
363 return (op->visit & mcmodel) != 0;
364 return 1;
365 }