Debian Codegraph @

arch/powerpc/local2.c

raw symbols SCIP
1 /* $Id: local2.c,v 1.30 2016/09/26 16:45:42 ragge Exp $ */
2 /*
3 * Copyright (c) 2003 Anders Magnusson ([email protected]).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "pass1.h" /* for cftnsp */
30 #include "pass2.h"
31 #include <ctype.h>
32 #include <string.h>
33 #include <stdlib.h>
34
35 #if defined(MACHOABI)
36 #define EXPREFIX "_"
37 #else
38 #define EXPREFIX ""
39 #endif
40
41 #define LOWREG 0
42 #define HIREG 1
43
44 char *rnames[] = {
45 REGPREFIX "r0", REGPREFIX "r1",
46 REGPREFIX "r2", REGPREFIX "r3",
47 REGPREFIX "r4", REGPREFIX "r5",
48 REGPREFIX "r6", REGPREFIX "r7",
49 REGPREFIX "r8", REGPREFIX "r9",
50 REGPREFIX "r10", REGPREFIX "r11",
51 REGPREFIX "r12", REGPREFIX "r13",
52 REGPREFIX "r14", REGPREFIX "r15",
53 REGPREFIX "r16", REGPREFIX "r17",
54 REGPREFIX "r18", REGPREFIX "r19",
55 REGPREFIX "r20", REGPREFIX "r21",
56 REGPREFIX "r22", REGPREFIX "r23",
57 REGPREFIX "r24", REGPREFIX "r25",
58 REGPREFIX "r26", REGPREFIX "r27",
59 REGPREFIX "r28", REGPREFIX "r29",
60 REGPREFIX "r30", REGPREFIX "r31",
61 "r4\0r3\0", "r5\0r4\0", "r6\0r5\0", "r7\0r6\0",
62 "r8\0r7\0", "r9\0r8\0", "r10r9\0", "r15r14", "r17r16",
63 "r19r18", "r21r20", "r23r22", "r25r24", "r27r26",
64 "r29r28", "r31r30",
65 REGPREFIX "f0", REGPREFIX "f1",
66 REGPREFIX "f2", REGPREFIX "f3",
67 REGPREFIX "f4", REGPREFIX "f5",
68 REGPREFIX "f6", REGPREFIX "f7",
69 REGPREFIX "f8", REGPREFIX "f9",
70 REGPREFIX "f10", REGPREFIX "f11",
71 REGPREFIX "f12", REGPREFIX "f13",
72 REGPREFIX "f14", REGPREFIX "f15",
73 REGPREFIX "f16", REGPREFIX "f17",
74 REGPREFIX "f18", REGPREFIX "f19",
75 REGPREFIX "f20", REGPREFIX "f21",
76 REGPREFIX "f22", REGPREFIX "f23",
77 REGPREFIX "f24", REGPREFIX "f25",
78 REGPREFIX "f26", REGPREFIX "f27",
79 REGPREFIX "f28", REGPREFIX "f29",
80 REGPREFIX "f30", REGPREFIX "f31",
81 };
82
83 static int argsize(NODE *p);
84
85 static int p2calls;
86 static int p2temps; /* TEMPs which aren't autos yet */
87 static int p2framesize;
88 static int p2maxstacksize;
89
90 void
91 deflab(int label)
92 {
93 printf(LABFMT ":\n", label);
94 }
95
96 static TWORD ftype;
97
98 /*
99 * Print out the prolog assembler.
100 */
101 void
102 prologue(struct interpass_prolog *ipp)
103 {
104 int addto;
105
106 #ifdef PCC_DEBUG
107 if (x2debug)
108 printf("prologue: type=%d, lineno=%d, name=%s, vis=%d, ipptype=%d, autos=%d, tmpnum=%d, lblnum=%d\n",
109 ipp->ipp_ip.type,
110 ipp->ipp_ip.lineno,
111 ipp->ipp_name,
112 ipp->ipp_vis,
113 ipp->ipp_type,
114 ipp->ipp_autos,
115 ipp->ip_tmpnum,
116 ipp->ip_lblnum);
117 #endif
118
119 ftype = ipp->ipp_type;
120
121 addto = p2framesize;
122
123 if (p2calls != 0 || kflag) {
124 /* get return address (not required for leaf function) */
125 printf("\tmflr %s\n", rnames[R0]);
126 printf("\tstw %s,8(%s)\n", rnames[R0], rnames[R1]);
127 }
128 /* save registers R30 and R31 */
129 printf("\tstmw %s,-8(%s)\n", rnames[R30], rnames[R1]);
130 #ifdef FPREG
131 printf("\tmr %s,%s\n", rnames[FPREG], rnames[R1]);
132 #endif
133 /* create the new stack frame */
134 if (addto > 32767) {
135 printf("\tlis %s,%d\n", rnames[R0], (-addto) >> 16);
136 printf("\tori %s,%s,%d\n", rnames[R0],
137 rnames[R0], (-addto) & 0xffff);
138 printf("\tstwux %s,%s,%s\n", rnames[R1],
139 rnames[R1], rnames[R0]);
140 } else {
141 printf("\tstwu %s,-%d(%s)\n", rnames[R1], addto, rnames[R1]);
142 }
143
144 if (kflag) {
145 #if defined(ELFABI)
146 printf("\tbl _GLOBAL_OFFSET_TABLE_@local-4\n");
147 printf("\tmflr %s\n", rnames[GOTREG]);
148 #elif defined(MACHOABI)
149 printf("\tbcl 20,31,L%s$pb\n", ipp->ipp_name + 1);
150 printf("L%s$pb:\n", ipp->ipp_name + 1);
151 printf("\tmflr %s\n", rnames[GOTREG]);
152 #endif
153 }
154
155 }
156
157
158 void
159 eoftn(struct interpass_prolog *ipp)
160 {
161
162 #ifdef PCC_DEBUG
163 if (x2debug)
164 printf("eoftn:\n");
165 #endif
166
167 if (ipp->ipp_ip.ip_lbl == 0)
168 return; /* no code needs to be generated */
169
170 /* struct return needs special treatment */
171 if (ftype == STRTY || ftype == UNIONTY)
172 cerror("eoftn");
173
174 /* unwind stack frame */
175 printf("\tlwz %s,0(%s)\n", rnames[R1], rnames[R1]);
176 if (p2calls != 0 || kflag) {
177 printf("\tlwz %s,8(%s)\n", rnames[R0], rnames[R1]);
178 printf("\tmtlr %s\n", rnames[R0]);
179 }
180 printf("\tlmw %s,-8(%s)\n", rnames[R30], rnames[R1]);
181 printf("\tblr\n");
182 }
183
184 /*
185 * add/sub/...
186 *
187 * Param given:
188 */
189 void
190 hopcode(int f, int o)
191 {
192 char *str;
193
194 switch (o) {
195 case PLUS:
196 str = "addw";
197 break;
198 case MINUS:
199 str = "subw";
200 break;
201 case AND:
202 str = "and";
203 break;
204 case OR:
205 str = "or";
206 break;
207 case ER:
208 str = "xor";
209 break;
210 default:
211 comperr("hopcode2: %d", o);
212 str = 0; /* XXX gcc */
213 }
214 printf("%s%c", str, f);
215 }
216
217 /*
218 * Return type size in bytes. Used by R2REGS, arg 2 to offset().
219 */
220 int
221 tlen(NODE *p)
222 {
223 switch(p->n_type) {
224 case CHAR:
225 case UCHAR:
226 return(1);
227
228 case SHORT:
229 case USHORT:
230 return(SZSHORT/SZCHAR);
231
232 case DOUBLE:
233 return(SZDOUBLE/SZCHAR);
234
235 case INT:
236 case UNSIGNED:
237 case LONG:
238 case ULONG:
239 return(SZINT/SZCHAR);
240
241 case LONGLONG:
242 case ULONGLONG:
243 return SZLONGLONG/SZCHAR;
244
245 default:
246 if (!ISPTR(p->n_type))
247 comperr("tlen type %d not pointer");
248 return SZPOINT(p->n_type)/SZCHAR;
249 }
250 }
251
252 /*
253 * Emit code to compare two longlong numbers.
254 */
255 static void
256 twollcomp(NODE *p)
257 {
258 int o = p->n_op;
259 int s = getlab2();
260 int e = p->n_label;
261 int cb1, cb2;
262
263 if (o >= ULE)
264 o -= (ULE-LE);
265 switch (o) {
266 case NE:
267 cb1 = 0;
268 cb2 = NE;
269 break;
270 case EQ:
271 cb1 = NE;
272 cb2 = 0;
273 break;
274 case LE:
275 case LT:
276 cb1 = GT;
277 cb2 = LT;
278 break;
279 case GE:
280 case GT:
281 cb1 = LT;
282 cb2 = GT;
283 break;
284
285 default:
286 cb1 = cb2 = 0; /* XXX gcc */
287 }
288 if (p->n_op >= ULE)
289 cb1 += 4, cb2 += 4;
290 if (p->n_op >= ULE)
291 expand(p, 0, "\tcmplw UL,UR" COM "compare 64-bit values (upper)\n");
292 else
293 expand(p, 0, "\tcmpw UL,UR" COM "compare 64-bit values (upper)\n");
294 if (cb1) cbgen(cb1, s);
295 if (cb2) cbgen(cb2, e);
296 if (p->n_op >= ULE)
297 expand(p, 0, "\tcmplw AL,AR" COM "(and lower)\n");
298 else
299 expand(p, 0, "\tcmpw AL,AR" COM "(and lower)\n");
300 cbgen(p->n_op, e);
301 deflab(s);
302 }
303
304 static void
305 shiftop(NODE *p)
306 {
307 NODE *r = p->n_right;
308 TWORD ty = p->n_type;
309
310 if (p->n_op == LS && r->n_op == ICON && getlval(r) < 32) {
311 expand(p, INBREG, "\tsrwi A1,AL,32-AR" COM "64-bit left-shift\n");
312 expand(p, INBREG, "\tslwi U1,UL,AR\n");
313 expand(p, INBREG, "\tor U1,U1,A1\n");
314 expand(p, INBREG, "\tslwi A1,AL,AR\n");
315 } else if (p->n_op == LS && r->n_op == ICON && getlval(r) < 64) {
316 expand(p, INBREG, "\tli A1,0" COM "64-bit left-shift\n");
317 if (getlval(r) == 32)
318 expand(p, INBREG, "\tmr U1,AL\n");
319 else
320 expand(p, INBREG, "\tslwi U1,AL,AR-32\n");
321 } else if (p->n_op == LS && r->n_op == ICON) {
322 expand(p, INBREG, "\tli A1,0" COM "64-bit left-shift\n");
323 expand(p, INBREG, "\tli U1,0\n");
324 } else if (p->n_op == RS && r->n_op == ICON && getlval(r) < 32) {
325 expand(p, INBREG, "\tslwi U1,UL,32-AR" COM "64-bit right-shift\n");
326 expand(p, INBREG, "\tsrwi A1,AL,AR\n");
327 expand(p, INBREG, "\tor A1,A1,U1\n");
328 if (ty == LONGLONG)
329 expand(p, INBREG, "\tsrawi U1,UL,AR\n");
330 else
331 expand(p, INBREG, "\tsrwi U1,UL,AR\n");
332 } else if (p->n_op == RS && r->n_op == ICON && getlval(r) < 64) {
333 if (ty == LONGLONG)
334 expand(p, INBREG, "\tli U1,-1" COM "64-bit right-shift\n");
335 else
336 expand(p, INBREG, "\tli U1,0" COM "64-bit right-shift\n");
337 if (getlval(r) == 32)
338 expand(p, INBREG, "\tmr A1,UL\n");
339 else if (ty == LONGLONG)
340 expand(p, INBREG, "\tsrawi A1,UL,AR-32\n");
341 else
342 expand(p, INBREG, "\tsrwi A1,UL,AR-32\n");
343 } else if (p->n_op == RS && r->n_op == ICON) {
344 expand(p, INBREG, "\tli A1,0" COM "64-bit right-shift\n");
345 expand(p, INBREG, "\tli U1,0\n");
346 }
347 }
348
349 /*
350 * Structure assignment.
351 */
352 static void
353 stasg(NODE *p)
354 {
355 NODE *l = p->n_left;
356 int val = getlval(l);
357
358 /* R3 = dest, R4 = src, R5 = len */
359 printf("\tli %s,%d\n", rnames[R5], attr_find(p->n_ap, ATTR_P2STRUCT)->iarg(0));
360 if (l->n_op == OREG) {
361 printf("\taddi %s,%s,%d\n", rnames[R3], rnames[regno(l)], val);
362 } else if (l->n_op == NAME) {
363 #if defined(ELFABI)
364 printf("\tli %s,", rnames[R3]);
365 adrput(stdout, l);
366 printf("@ha\n");
367 printf("\taddi %s,%s,", rnames[R3], rnames[R3]);
368 adrput(stdout, l);
369 printf("@l\n");
370 #elif defined(MACHOABI)
371 printf("\tli %s,ha16(", rnames[R3]);
372 adrput(stdout, l);
373 printf(")\n");
374 printf("\taddi %s,%s,lo16(", rnames[R3], rnames[R3]);
375 adrput(stdout, l);
376 printf(")\n");
377 #endif
378 }
379 if (kflag) {
380 #if defined(ELFABI)
381 printf("\tbl %s@got(30)\n", EXPREFIX "memcpy");
382 #elif defined(MACHOABI)
383 printf("\tbl L%s$stub\n", EXPREFIX "memcpy");
384 addstub(&stublist, EXPREFIX "memcpy");
385 #endif
386 } else {
387 printf("\tbl %s\n", EXPREFIX "memcpy");
388 }
389 }
390
391 static void
392 fpemul(NODE *p)
393 {
394 NODE *l = p->n_left;
395 char *ch = NULL;
396
397 if (p->n_op == PLUS && p->n_type == FLOAT) ch = "addsf3";
398 else if (p->n_op == PLUS && p->n_type == DOUBLE) ch = "adddf3";
399 else if (p->n_op == PLUS && p->n_type == LDOUBLE) ch = "addtf3";
400
401 else if (p->n_op == MINUS && p->n_type == FLOAT) ch = "subsf3";
402 else if (p->n_op == MINUS && p->n_type == DOUBLE) ch = "subdf3";
403 else if (p->n_op == MINUS && p->n_type == LDOUBLE) ch = "subtf3";
404
405 else if (p->n_op == MUL && p->n_type == FLOAT) ch = "mulsf3";
406 else if (p->n_op == MUL && p->n_type == DOUBLE) ch = "muldf3";
407 else if (p->n_op == MUL && p->n_type == LDOUBLE) ch = "multf3";
408
409 else if (p->n_op == DIV && p->n_type == FLOAT) ch = "divsf3";
410 else if (p->n_op == DIV && p->n_type == DOUBLE) ch = "divdf3";
411 else if (p->n_op == DIV && p->n_type == LDOUBLE) ch = "divtf3";
412
413 else if (p->n_op == UMINUS && p->n_type == FLOAT) ch = "negsf2";
414 else if (p->n_op == UMINUS && p->n_type == DOUBLE) ch = "negdf2";
415 else if (p->n_op == UMINUS && p->n_type == LDOUBLE) ch = "negtf2";
416
417 else if (p->n_op == EQ && l->n_type == FLOAT) ch = "eqsf2";
418 else if (p->n_op == EQ && l->n_type == DOUBLE) ch = "eqdf2";
419 else if (p->n_op == EQ && l->n_type == LDOUBLE) ch = "eqtf2";
420
421 else if (p->n_op == NE && l->n_type == FLOAT) ch = "nesf2";
422 else if (p->n_op == NE && l->n_type == DOUBLE) ch = "nedf2";
423 else if (p->n_op == NE && l->n_type == LDOUBLE) ch = "netf2";
424
425 else if (p->n_op == GE && l->n_type == FLOAT) ch = "gesf2";
426 else if (p->n_op == GE && l->n_type == DOUBLE) ch = "gedf2";
427 else if (p->n_op == GE && l->n_type == LDOUBLE) ch = "getf2";
428
429 else if (p->n_op == LE && l->n_type == FLOAT) ch = "lesf2";
430 else if (p->n_op == LE && l->n_type == DOUBLE) ch = "ledf2";
431 else if (p->n_op == LE && l->n_type == LDOUBLE) ch = "letf2";
432
433 else if (p->n_op == GT && l->n_type == FLOAT) ch = "gtsf2";
434 else if (p->n_op == GT && l->n_type == DOUBLE) ch = "gtdf2";
435 else if (p->n_op == GT && l->n_type == LDOUBLE) ch = "gttf2";
436
437 else if (p->n_op == LT && l->n_type == FLOAT) ch = "ltsf2";
438 else if (p->n_op == LT && l->n_type == DOUBLE) ch = "ltdf2";
439 else if (p->n_op == LT && l->n_type == LDOUBLE) ch = "lttf2";
440
441 else if (p->n_op == SCONV && p->n_type == FLOAT) {
442 if (l->n_type == DOUBLE) ch = "truncdfsf2";
443 else if (l->n_type == LDOUBLE) ch = "truncdfsf2";
444 else if (l->n_type == ULONGLONG) ch = "floatunsdisf";
445 else if (l->n_type == LONGLONG) ch = "floatdisf";
446 else if (l->n_type == LONG) ch = "floatsisf";
447 else if (l->n_type == ULONG) ch = "floatunsisf";
448 else if (l->n_type == INT) ch = "floatsisf";
449 else if (l->n_type == UNSIGNED) ch = "floatunsisf";
450 } else if (p->n_op == SCONV && p->n_type == DOUBLE) {
451 if (l->n_type == FLOAT) ch = "extendsfdf2";
452 else if (l->n_type == LDOUBLE) ch = "truncdfdf2";
453 else if (l->n_type == ULONGLONG) ch = "floatunsdidf";
454 else if (l->n_type == LONGLONG) ch = "floatdidf";
455 else if (l->n_type == LONG) ch = "floatsidf";
456 else if (l->n_type == ULONG) ch = "floatunssidf";
457 else if (l->n_type == INT) ch = "floatsidf";
458 else if (l->n_type == UNSIGNED) ch = "floatunssidf";
459 } else if (p->n_op == SCONV && p->n_type == LDOUBLE) {
460 if (l->n_type == FLOAT) ch = "extendsfdf2";
461 else if (l->n_type == DOUBLE) ch = "extenddfdf2";
462 else if (l->n_type == ULONGLONG) ch = "floatunsdidf";
463 else if (l->n_type == LONGLONG) ch = "floatdidf";
464 else if (l->n_type == LONG) ch = "floatsidf";
465 else if (l->n_type == ULONG) ch = "floatunssidf";
466 else if (l->n_type == INT) ch = "floatsidf";
467 else if (l->n_type == UNSIGNED) ch = "floatunsidf";
468 } else if (p->n_op == SCONV && p->n_type == ULONGLONG) {
469 if (l->n_type == FLOAT) ch = "fixunssfdi";
470 else if (l->n_type == DOUBLE) ch = "fixunsdfdi";
471 else if (l->n_type == LDOUBLE) ch = "fixunsdfdi";
472 } else if (p->n_op == SCONV && p->n_type == LONGLONG) {
473 if (l->n_type == FLOAT) ch = "fixsfdi";
474 else if (l->n_type == DOUBLE) ch = "fixdfdi";
475 else if (l->n_type == LDOUBLE) ch = "fixdfdi";
476 } else if (p->n_op == SCONV && p->n_type == LONG) {
477 if (l->n_type == FLOAT) ch = "fixsfdi";
478 else if (l->n_type == DOUBLE) ch = "fixdfdi";
479 else if (l->n_type == LDOUBLE) ch = "fixdfdi";
480 } else if (p->n_op == SCONV && p->n_type == ULONG) {
481 if (l->n_type == FLOAT) ch = "fixunssfdi";
482 else if (l->n_type == DOUBLE) ch = "fixunsdfdi";
483 else if (l->n_type == LDOUBLE) ch = "fixunsdfdi";
484 } else if (p->n_op == SCONV && p->n_type == INT) {
485 if (l->n_type == FLOAT) ch = "fixsfsi";
486 else if (l->n_type == DOUBLE) ch = "fixdfsi";
487 else if (l->n_type == LDOUBLE) ch = "fixdfsi";
488 } else if (p->n_op == SCONV && p->n_type == UNSIGNED) {
489 if (l->n_type == FLOAT) ch = "fixunssfsi";
490 else if (l->n_type == DOUBLE) ch = "fixunsdfsi";
491 else if (l->n_type == LDOUBLE) ch = "fixunsdfsi";
492 }
493
494 if (ch == NULL) comperr("ZF: op=0x%x (%d)\n", p->n_op, p->n_op);
495
496 if (kflag) {
497 #if defined(ELFABI)
498 printf("\tbl __%s%s@got(30)" COM "soft-float\n", EXPREFIX, ch);
499 #elif defined(MACHOABI)
500 char buf[32];
501 printf("\tbl L__%s%s$stub" COM "soft-float\n", EXPREFIX, ch);
502 snprintf(buf, 32, "__%s%s", EXPREFIX, ch);
503 addstub(&stublist, buf);
504 #endif
505 } else {
506 printf("\tbl __%s%s" COM "soft-float\n", EXPREFIX, ch);
507 }
508
509 if (p->n_op >= EQ && p->n_op <= GT)
510 printf("\tcmpwi %s,0\n", rnames[R3]);
511 }
512
513 /*
514 * http://gcc.gnu.org/onlinedocs/gccint/Integer-library-routines.html#Integer-library-routines
515 */
516
517 static void
518 emul(NODE *p)
519 {
520 char *ch = NULL;
521
522 if (p->n_op == LS && DEUNSIGN(p->n_type) == LONGLONG) ch = "ashldi3";
523 else if (p->n_op == LS && (DEUNSIGN(p->n_type) == LONG ||
524 DEUNSIGN(p->n_type) == INT))
525 ch = "ashlsi3";
526
527 else if (p->n_op == RS && p->n_type == ULONGLONG) ch = "lshrdi3";
528 else if (p->n_op == RS && (p->n_type == ULONG || p->n_type == INT))
529 ch = "lshrsi3";
530
531 else if (p->n_op == RS && p->n_type == LONGLONG) ch = "ashrdi3";
532 else if (p->n_op == RS && (p->n_type == LONG || p->n_type == INT))
533 ch = "ashrsi3";
534
535 else if (p->n_op == DIV && p->n_type == LONGLONG) ch = "divdi3";
536 else if (p->n_op == DIV && (p->n_type == LONG || p->n_type == INT))
537 ch = "divsi3";
538
539 else if (p->n_op == DIV && p->n_type == ULONGLONG) ch = "udivdi3";
540 else if (p->n_op == DIV && (p->n_type == ULONG ||
541 p->n_type == UNSIGNED))
542 ch = "udivsi3";
543
544 else if (p->n_op == MOD && p->n_type == LONGLONG) ch = "moddi3";
545 else if (p->n_op == MOD && (p->n_type == LONG || p->n_type == INT))
546 ch = "modsi3";
547
548 else if (p->n_op == MOD && p->n_type == ULONGLONG) ch = "umoddi3";
549 else if (p->n_op == MOD && (p->n_type == ULONG ||
550 p->n_type == UNSIGNED))
551 ch = "umodsi3";
552
553 else if (p->n_op == MUL && p->n_type == LONGLONG) ch = "muldi3";
554 else if (p->n_op == MUL && (p->n_type == LONG || p->n_type == INT))
555 ch = "mulsi3";
556
557 else if (p->n_op == UMINUS && p->n_type == LONGLONG) ch = "negdi2";
558 else if (p->n_op == UMINUS && p->n_type == LONG) ch = "negsi2";
559
560 else ch = 0, comperr("ZE");
561 if (kflag) {
562 #if defined(ELFABI)
563 printf("\tbl __%s%s@got(30)" COM "emulated op\n", EXPREFIX, ch);
564 #elif defined(MACHOABI)
565 char buf[32];
566 printf("\tbl L__%s%s$stub" COM "emulated op\n", EXPREFIX, ch);
567 snprintf(buf, 32, "__%s%s", EXPREFIX, ch);
568 addstub(&stublist, buf);
569 #endif
570 } else {
571 printf("\tbl __%s%s" COM "emulated operation\n", EXPREFIX, ch);
572 }
573 }
574
575 /*
576 * Floating-point conversions (int -> float/double & float/double -> int)
577 */
578
579 static void
580 ftoi(NODE *p)
581 {
582 NODE *l = p->n_left;
583
584 printf(COM "start conversion float/(l)double to int\n");
585
586 if (l->n_op != OREG) {
587 expand(p, 0, "\tstw AL,-4");
588 printf("(%s)\n", rnames[SPREG]);
589 if (l->n_type == FLOAT)
590 expand(p, 0, "\tlfs A2,");
591 else
592 expand(p, 0, "\tlfd A2,\n");
593 printf("-4(%s)\n", rnames[SPREG]);
594 } else {
595 if (l->n_type == FLOAT)
596 expand(p, 0, "\tlfs A2,AL\n");
597 else
598 expand(p, 0, "\tlfd A2,AL\n");
599 }
600
601 expand(p, 0, "\tfctiwz A2,A2\n");
602 expand(p, 0, "\tstfd A2,");
603 printf("-8(%s)\n", rnames[SPREG]);
604 expand(p, 0, "\tlwz A1,");
605 printf("-4(%s)\n", rnames[SPREG]);
606
607 printf(COM "end conversion\n");
608 }
609
610 static void
611 ftou(NODE *p)
612 {
613 static int lab = 0;
614 NODE *l = p->n_left;
615 int lab1 = getlab2();
616 int lab2 = getlab2();
617
618 printf(COM "start conversion of float/(l)double to unsigned\n");
619
620 if (lab == 0) {
621 lab = getlab2();
622 expand(p, 0, "\t.data\n");
623 printf(LABFMT ":\t.long 0x41e00000\n\t.long 0\n", lab);
624 expand(p, 0, "\t.text\n");
625 }
626
627 if (l->n_op != OREG) {
628 expand(p, 0, "\tstw AL,");
629 printf("-4(%s)\n", rnames[SPREG]);
630 if (l->n_type == FLOAT)
631 expand(p, 0, "\tlfs A3,");
632 else
633 expand(p, 0, "\tlfd A3,");
634 printf("-4(%s)\n", rnames[SPREG]);
635
636 } else {
637 if (l->n_type == FLOAT)
638 expand(p, 0, "\tlfs A3,AL\n");
639 else
640 expand(p, 0, "\tlfd A3,AL\n");
641 }
642
643 #if 0
644 if (kflag) {
645 expand(p, 0, "\taddis A1,");
646 printf("%s,ha16(", rnames[R31]);
647 printf(LABFMT, lab);
648 printf("-L%s$pb)\n", cftnsp->soname ? cftnsp->soname : exname(cftnsp->sname));
649 expand(p, 0, "\tlfd A2,lo16(");
650 printf(LABFMT, lab);
651 printf("-L%s$pb)\n", cftnsp->soname ? cftnsp->soname : exname(cftnsp->sname));
652 expand(p, 0, "(A1)\n");
653 } else {
654 expand(p, 0, "\tlfd A2,");
655 printf(LABFMT "\n", lab);
656 }
657 #endif
658
659 #if defined(ELFABI)
660
661 expand(p, 0, "\taddis A1,");
662 printf("%s," LABFMT "@ha\n", rnames[R31], lab);
663 expand(p, 0, "\tlfd A2,");
664 printf(LABFMT "@l", lab);
665 expand(p, 0, "(A1)\n");
666
667 #elif defined(MACHOABI)
668
669 expand(p, 0, "\taddis A1,");
670 printf("%s,ha16(", rnames[R31]);
671 printf(LABFMT, lab);
672 if (kflag)
673 printf("-L%s$pb", cftnsp->soname ? cftnsp->soname : exname(cftnsp->sname));
674 printf(")\n");
675 expand(p, 0, "\tlfd A2,lo16(");
676 printf(LABFMT, lab);
677 if (kflag)
678 printf("-L%s$pb", cftnsp->soname ? cftnsp->soname : exname(cftnsp->sname));
679 expand(p, 0, ")(A1)\n");
680
681 #endif
682
683 expand(p, 0, "\tfcmpu cr7,A3,A2\n");
684 printf("\tcror 30,29,30\n");
685 printf("\tbeq cr7,"LABFMT "\n", lab1);
686
687 expand(p, 0, "\tfctiwz A2,A3\n");
688 expand(p, 0, "\tstfd A2,");
689 printf("-8(%s)\n", rnames[SPREG]);
690 expand(p, 0, "\tlwz A1,");
691 printf("-4(%s)\n", rnames[SPREG]);
692 printf("\tba " LABFMT "\n", lab2);
693
694 deflab(lab1);
695
696 expand(p, 0, "\tfsub A2,A3,A2\n");
697 expand(p, 0, "\tfctiwz A2,A2\n");
698 expand(p, 0, "\tstfd A2,");
699 printf("-8(%s)\n", rnames[SPREG]);
700 expand(p, 0, "\tlwz A1,");
701 printf("-4(%s)\n", rnames[SPREG]);
702 expand(p, 0, "\txoris A1,A1,0x8000\n");
703
704 deflab(lab2);
705
706 printf(COM "end conversion\n");
707 }
708
709 static void
710 itof(NODE *p)
711 {
712 static int labu = 0;
713 static int labi = 0;
714 int lab;
715 NODE *l = p->n_left;
716
717 printf(COM "start conversion (u)int to float/(l)double\n");
718
719 if (labi == 0 && l->n_type == INT) {
720 labi = getlab2();
721 expand(p, 0, "\t.data\n");
722 printf(LABFMT ":\t.long 0x43300000\n\t.long 0x80000000\n", labi);
723 expand(p, 0, "\t.text\n");
724 } else if (labu == 0 && l->n_type == UNSIGNED) {
725 labu = getlab2();
726 expand(p, 0, "\t.data\n");
727 printf(LABFMT ":\t.long 0x43300000\n\t.long 0x00000000\n", labu);
728 expand(p, 0, "\t.text\n");
729 }
730
731 if (l->n_type == INT) {
732 expand(p, 0, "\txoris A1,AL,0x8000\n");
733 lab = labi;
734 } else {
735 lab = labu;
736 }
737 expand(p, 0, "\tstw A1,");
738 printf("-4(%s)\n", rnames[SPREG]);
739 expand(p, 0, "\tlis A1,0x4330\n");
740 expand(p, 0, "\tstw A1,");
741 printf("-8(%s)\n", rnames[SPREG]);
742 expand(p, 0, "\tlfd A3,");
743 printf("-8(%s)\n", rnames[SPREG]);
744
745 #if 0
746 if (kflag) {
747 expand(p, 0, "\taddis A1,");
748 printf("%s,ha16(", rnames[R31]);
749 printf(LABFMT, lab);
750 printf("-L%s$pb)\n", cftnsp->soname ? cftnsp->soname : exname(cftnsp->sname));
751 expand(p, 0, "\tlfd A2,lo16(");
752 printf(LABFMT, lab);
753 printf("-L%s$pb)\n", cftnsp->soname ? cftnsp->soname : exname(cftnsp->sname));
754 expand(p, 0, "(A1)\n");
755 } else {
756 expand(p, 0, "\tlfd A2,");
757 printf(LABFMT "\n", lab);
758 }
759 #endif
760
761 #if defined(ELFABI)
762
763 expand(p, 0, "\taddis A1,");
764 printf("%s," LABFMT "@ha\n", rnames[R31], lab);
765 expand(p, 0, "\tlfd A2,");
766 printf(LABFMT "@l", lab);
767 expand(p, 0, "(A1)\n");
768
769 #elif defined(MACHOABI)
770
771 expand(p, 0, "\taddis A1,");
772 printf("%s,ha16(", rnames[R31]);
773 printf(LABFMT, lab);
774 if (kflag)
775 printf("-L%s$pb", cftnsp->soname ? cftnsp->soname : exname(cftnsp->sname));
776 printf(")\n");
777 expand(p, 0, "\tlfd A2,lo16(");
778 printf(LABFMT, lab);
779 if (kflag)
780 printf("-L%s$pb", cftnsp->soname ? cftnsp->soname : exname(cftnsp->sname));
781 expand(p, 0, ")(A1)\n");
782
783 #endif
784
785 expand(p, 0, "\tfsub A3,A3,A2\n");
786 if (p->n_type == FLOAT)
787 expand(p, 0, "\tfrsp A3,A3\n");
788
789 printf(COM "end conversion\n");
790 }
791
792
793 static void
794 fpconv(NODE *p)
795 {
796 NODE *l = p->n_left;
797
798 #ifdef PCC_DEBUG
799 if (p->n_op != SCONV)
800 cerror("fpconv 1");
801 #endif
802
803 if (DEUNSIGN(l->n_type) == INT)
804 itof(p);
805 else if (p->n_type == INT)
806 ftoi(p);
807 else if (p->n_type == UNSIGNED)
808 ftou(p);
809 else
810 cerror("unhandled floating-point conversion");
811
812 }
813
814 void
815 zzzcode(NODE *p, int c)
816 {
817 switch (c) {
818
819 case 'C': /* floating-point conversions */
820 fpconv(p);
821 break;
822
823 case 'D': /* long long comparision */
824 twollcomp(p);
825 break;
826
827 case 'E': /* print out emulated ops */
828 emul(p);
829 break;
830
831 case 'F': /* print out emulate floating-point ops */
832 fpemul(p);
833 break;
834
835 case 'O': /* 64-bit left and right shift operators */
836 shiftop(p);
837 break;
838
839 case 'Q': /* emit struct assign */
840 stasg(p);
841 break;
842
843 default:
844 comperr("zzzcode %c", c);
845 }
846 }
847
848 /*ARGSUSED*/
849 int
850 rewfld(NODE *p)
851 {
852 return(1);
853 }
854
855 int canaddr(NODE *);
856 int
857 canaddr(NODE *p)
858 {
859 int o = p->n_op;
860
861 if (o == NAME || o == REG || o == ICON || o == OREG ||
862 (o == UMUL && shumul(p->n_left, SOREG)))
863 return(1);
864 return 0;
865 }
866
867 int
868 fldexpand(NODE *p, int cookie, char **cp)
869 {
870 CONSZ val;
871 int shft;
872
873 if (p->n_op == ASSIGN)
874 p = p->n_left;
875
876 if (features(FEATURE_BIGENDIAN))
877 shft = SZINT - UPKFSZ(p->n_rval) - UPKFOFF(p->n_rval);
878 else
879 shft = UPKFOFF(p->n_rval);
880
881 switch (**cp) {
882 case 'S':
883 printf("%d", UPKFSZ(p->n_rval));
884 break;
885 case 'H':
886 printf("%d", shft);
887 break;
888 case 'M':
889 case 'N':
890 val = (CONSZ)1 << UPKFSZ(p->n_rval);
891 --val;
892 val <<= shft;
893 printf(CONFMT, (**cp == 'M' ? val : ~val) & 0xffffffff);
894 break;
895 default:
896 comperr("fldexpand");
897 }
898 return 1;
899 }
900
901 /*
902 * Does the bitfield shape match?
903 */
904 int
905 flshape(NODE *p)
906 {
907 int o = p->n_op;
908
909 if (o == OREG || o == REG || o == NAME)
910 return SRDIR; /* Direct match */
911 if (o == UMUL && shumul(p->n_left, SOREG))
912 return SROREG; /* Convert into oreg */
913 return SRREG; /* put it into a register */
914 }
915
916 /* INTEMP shapes must not contain any temporary registers */
917 /* XXX should this go away now? */
918 int
919 shtemp(NODE *p)
920 {
921 printf("; shtemp\n");
922 return 0;
923 #if 0
924 int r;
925
926 if (p->n_op == STARG )
927 p = p->n_left;
928
929 switch (p->n_op) {
930 case REG:
931 return (!istreg(regno(p)));
932
933 case OREG:
934 r = regno(p);
935 if (R2TEST(r)) {
936 if (istreg(R2UPK1(r)))
937 return(0);
938 r = R2UPK2(r);
939 }
940 return (!istreg(r));
941
942 case UMUL:
943 p = p->n_left;
944 return (p->n_op != UMUL && shtemp(p));
945 }
946
947 if (optype(p->n_op) != LTYPE)
948 return(0);
949 return(1);
950 #endif
951 }
952
953 void
954 adrcon(CONSZ val)
955 {
956 printf( CONFMT, val);
957 }
958
959 void
960 conput(FILE *fp, NODE *p)
961 {
962 int val = getlval(p);
963
964 switch (p->n_op) {
965 case ICON:
966 if (p->n_name[0] != '\0') {
967 fprintf(fp, "%s", p->n_name);
968 if (val)
969 fprintf(fp, "+%d", val);
970 } else {
971 if (GCLASS(p->n_type) == CLASSB)
972 fprintf(fp, CONFMT, getlval(p) >> 32);
973 else
974 fprintf(fp, "%d", val);
975 }
976 return;
977
978 default:
979 comperr("illegal conput, p %p", p);
980 }
981 }
982
983 /*ARGSUSED*/
984 void
985 insput(NODE *p)
986 {
987 comperr("insput");
988 }
989
990 /*
991 * Print lower or upper name of 64-bit register.
992 */
993 static void
994 reg64name(int reg, int hi)
995 {
996 int idx;
997 int off = 0;
998
999 idx = (reg > R14R15 ? (2*(reg - R14R15) + R14) : (reg - R3R4 + R3));
1000
1001 if ((hi == HIREG && !features(FEATURE_BIGENDIAN)) ||
1002 (hi == LOWREG && features(FEATURE_BIGENDIAN)))
1003 off = 1;
1004
1005 printf("%s" , rnames[idx + off]);
1006 }
1007
1008 /*
1009 * Write out the upper address, like the upper register of a 2-register
1010 * reference, or the next memory location.
1011 */
1012 void
1013 upput(NODE *p, int size)
1014 {
1015 size /= SZCHAR;
1016 switch (p->n_op) {
1017 case REG:
1018 reg64name(regno(p), HIREG);
1019 break;
1020
1021 case NAME:
1022 case OREG:
1023 if (features(FEATURE_BIGENDIAN))
1024 printf("%d", (int)getlval(p));
1025 else
1026 printf("%d", (int)(getlval(p) + 4));
1027 printf("(%s)", rnames[regno(p)]);
1028 break;
1029
1030 case ICON:
1031 printf(CONFMT, getlval(p) >> 32);
1032 break;
1033
1034 default:
1035 comperr("upput bad op %d size %d", p->n_op, size);
1036 }
1037 }
1038
1039 void
1040 adrput(FILE *io, NODE *p)
1041 {
1042 /* output an address, with offsets, from p */
1043
1044 if (p->n_op == FLD)
1045 p = p->n_left;
1046
1047 switch (p->n_op) {
1048
1049 case NAME:
1050 if (p->n_name[0] != '\0') {
1051 fputs(p->n_name, io);
1052 if (getlval(p) != 0)
1053 fprintf(io, "+" CONFMT, getlval(p));
1054 } else
1055 fprintf(io, CONFMT, getlval(p));
1056 return;
1057
1058 case OREG:
1059 if (DEUNSIGN(p->n_type) == LONGLONG &&
1060 features(FEATURE_BIGENDIAN))
1061 fprintf(io, "%d", (int)getlval(p) + 4);
1062 else
1063 fprintf(io, "%d", (int)getlval(p));
1064 fprintf(io, "(%s)", rnames[regno(p)]);
1065 return;
1066
1067 case ICON:
1068 /* addressable value of the constant */
1069 conput(io, p);
1070 return;
1071
1072 case REG:
1073 if (GCLASS(regno(p)) == CLASSB)
1074 reg64name(regno(p), LOWREG);
1075 else
1076 fprintf(io, "%s", rnames[regno(p)]);
1077 #if 0
1078 switch (p->n_type) {
1079 case DOUBLE:
1080 case LDOUBLE:
1081 if (features(FEATURE_HARDFLOAT)) {
1082 fprintf(io, "%s", rnames[regno(p)]);
1083 break;
1084 }
1085 /* FALL-THROUGH */
1086 case LONGLONG:
1087 case ULONGLONG:
1088 reg64name(regno(p), LOWREG);
1089 break;
1090 default:
1091 fprintf(io, "%s", rnames[regno(p)]);
1092 }
1093 #endif
1094 return;
1095
1096 default:
1097 comperr("illegal address, op %d, node %p", p->n_op, p);
1098 return;
1099
1100 }
1101 }
1102
1103 /*
1104 * these mnemonics match the order of the preprocessor decls
1105 * EQ, NE, LE, LT, GE, GT, ULE, ULT, UGE, UGT
1106 */
1107
1108 static char *
1109 ccbranches[] = {
1110 "beq", /* branch if equal */
1111 "bne", /* branch if not-equal */
1112 "ble", /* branch if less-than-or-equal */
1113 "blt", /* branch if less-than */
1114 "bge", /* branch if greater-than-or-equal */
1115 "bgt", /* branch if greater-than */
1116 /* what should these be ? */
1117 "ble", /* branch if less-than-or-equal */
1118 "blt", /* branch if less-than */
1119 "bge", /* branch if greater-than-or-equal */
1120 "bgt", /* branch if greater-than */
1121
1122 };
1123
1124
1125 /* printf conditional and unconditional branches */
1126 void
1127 cbgen(int o, int lab)
1128 {
1129 if (o < EQ || o > UGT)
1130 comperr("bad conditional branch: %s", opst[o]);
1131 printf("\t%s " LABFMT "\n", ccbranches[o-EQ], lab);
1132 }
1133
1134 static int
1135 argsize(NODE *p)
1136 {
1137 TWORD t = p->n_type;
1138
1139 if (t < LONGLONG || t == FLOAT || t > BTMASK)
1140 return 4;
1141 if (t == LONGLONG || t == ULONGLONG)
1142 return 8;
1143 if (t == DOUBLE || t == LDOUBLE)
1144 return 8;
1145 if (t == STRTY || t == UNIONTY)
1146 return attr_find(p->n_ap, ATTR_P2STRUCT)->iarg(0);
1147 comperr("argsize");
1148 return 0;
1149 }
1150
1151 static int
1152 calc_args_size(NODE *p)
1153 {
1154 int n = 0;
1155
1156 if (p->n_op == CM) {
1157 n += calc_args_size(p->n_left);
1158 n += calc_args_size(p->n_right);
1159 return n;
1160 }
1161
1162 n += argsize(p);
1163
1164 return n;
1165 }
1166
1167
1168 static void
1169 fixcalls(NODE *p, void *arg)
1170 {
1171 int n = 0;
1172
1173 switch (p->n_op) {
1174 case STCALL:
1175 case CALL:
1176 n = calc_args_size(p->n_right);
1177 if (n > p2maxstacksize)
1178 p2maxstacksize = n;
1179 /* FALLTHROUGH */
1180 case USTCALL:
1181 case UCALL:
1182 ++p2calls;
1183 break;
1184 case TEMP:
1185 p2temps += argsize(p);
1186 break;
1187 }
1188 }
1189
1190 /*
1191 * Must store floats in memory if there are two function calls involved.
1192 */
1193 static int
1194 storefloat(struct interpass *ip, NODE *p)
1195 {
1196 int l, r;
1197
1198 switch (optype(p->n_op)) {
1199 case BITYPE:
1200 l = storefloat(ip, p->n_left);
1201 r = storefloat(ip, p->n_right);
1202 if (p->n_op == CM)
1203 return 0; /* arguments, don't care */
1204 if (callop(p->n_op))
1205 return 1; /* found one */
1206 #define ISF(p) ((p)->n_type == FLOAT || (p)->n_type == DOUBLE || \
1207 (p)->n_type == LDOUBLE)
1208 if (ISF(p->n_left) && ISF(p->n_right) && l && r) {
1209 /* must store one. store left */
1210 struct interpass *nip;
1211 TWORD t = p->n_left->n_type;
1212 NODE *ll;
1213 int off;
1214
1215 off = (freetemp(szty(t)));
1216 ll = mklnode(OREG, off, SPREG, t);
1217 nip = ipnode(mkbinode(ASSIGN, ll, p->n_left, t));
1218 p->n_left = mklnode(OREG, off, SPREG, t);
1219 DLIST_INSERT_BEFORE(ip, nip, qelem);
1220 }
1221 return l|r;
1222
1223 case UTYPE:
1224 l = storefloat(ip, p->n_left);
1225 if (callop(p->n_op))
1226 l = 1;
1227 return l;
1228 default:
1229 return 0;
1230 }
1231 }
1232
1233 void
1234 myreader(struct interpass *ipole)
1235 {
1236 struct interpass *ip;
1237
1238 p2calls = 0;
1239 p2temps = 0;
1240 p2maxstacksize = 0;
1241
1242 DLIST_FOREACH(ip, ipole, qelem) {
1243 if (ip->type != IP_NODE)
1244 continue;
1245 walkf(ip->ip_node, fixcalls, 0);
1246 storefloat(ip, ip->ip_node);
1247 }
1248
1249 if (p2maxstacksize < NARGREGS*SZINT/SZCHAR)
1250 p2maxstacksize = NARGREGS*SZINT/SZCHAR;
1251
1252 p2framesize = ARGINIT/SZCHAR; /* stack ptr / return addr */
1253 p2framesize += 8; /* for R31 and R30 */
1254 p2framesize += p2maxautooff; /* autos */
1255 p2framesize += p2temps; /* TEMPs that aren't autos */
1256 if (p2calls != 0)
1257 p2framesize += p2maxstacksize; /* arguments to functions */
1258 p2framesize += (ALSTACK/SZCHAR - 1); /* round to 16-byte boundary */
1259 p2framesize &= ~(ALSTACK/SZCHAR - 1);
1260
1261 #if 0
1262 printf("!!! MYREADER\n");
1263 printf("!!! p2maxautooff = %d\n", p2maxautooff);
1264 printf("!!! p2autooff = %d\n", p2autooff);
1265 printf("!!! p2temps = %d\n", p2temps);
1266 printf("!!! p2calls = %d\n", p2calls);
1267 printf("!!! p2maxstacksize = %d\n", p2maxstacksize);
1268 #endif
1269
1270 if (x2debug)
1271 printip(ipole);
1272 }
1273
1274 /*
1275 * Remove some PCONVs after OREGs are created.
1276 */
1277 static void
1278 pconv2(NODE *p, void *arg)
1279 {
1280 NODE *q;
1281
1282 if (p->n_op == PLUS) {
1283 if (p->n_type == (PTR|SHORT) || p->n_type == (PTR|USHORT)) {
1284 if (p->n_right->n_op != ICON)
1285 return;
1286 if (p->n_left->n_op != PCONV)
1287 return;
1288 if (p->n_left->n_left->n_op != OREG)
1289 return;
1290 q = p->n_left->n_left;
1291 nfree(p->n_left);
1292 p->n_left = q;
1293 /*
1294 * This will be converted to another OREG later.
1295 */
1296 }
1297 }
1298 }
1299
1300 void
1301 mycanon(NODE *p)
1302 {
1303 walkf(p, pconv2, 0);
1304 }
1305
1306 void
1307 myoptim(struct interpass *ip)
1308 {
1309 #ifdef PCC_DEBUG
1310 if (x2debug) {
1311 printf("myoptim\n");
1312 }
1313 #endif
1314 }
1315
1316 /*
1317 * Move data between registers. While basic registers aren't a problem,
1318 * we have to handle the special case of overlapping composite registers.
1319 * It might just be easier to modify the register allocator so that
1320 * moves between overlapping registers isn't possible.
1321 */
1322 void
1323 rmove(int s, int d, TWORD t)
1324 {
1325 switch (t) {
1326 case LDOUBLE:
1327 case DOUBLE:
1328 if (features(FEATURE_HARDFLOAT)) {
1329 printf("\tfmr %s,%s" COM "rmove\n",
1330 rnames[d], rnames[s]);
1331 break;
1332 }
1333 /* FALL-THROUGH */
1334 case LONGLONG:
1335 case ULONGLONG:
1336 if (s == d+1) {
1337 /* dh = sl, copy low word first */
1338 printf("\tmr ");
1339 reg64name(d, LOWREG);
1340 printf(",");
1341 reg64name(s, LOWREG);
1342 printf("\n");
1343 printf("\tmr ");
1344 reg64name(d, HIREG);
1345 printf(",");
1346 reg64name(s, HIREG);
1347 printf("\n");
1348 } else {
1349 /* copy high word first */
1350 printf("\tmr ");
1351 reg64name(d, HIREG);
1352 printf(",");
1353 reg64name(s, HIREG);
1354 printf("\n");
1355 printf("\tmr ");
1356 reg64name(d, LOWREG);
1357 printf(",");
1358 reg64name(s, LOWREG);
1359 printf("\n");
1360 }
1361 break;
1362 case FLOAT:
1363 if (features(FEATURE_HARDFLOAT)) {
1364 printf("\tfmr %s,%s" COM "rmove\n",
1365 rnames[d], rnames[s]);
1366 break;
1367 }
1368 /* FALL-THROUGH */
1369 default:
1370 printf("\tmr %s,%s" COM "rmove\n", rnames[d], rnames[s]);
1371 }
1372 }
1373
1374 /*
1375 * For class c, find worst-case displacement of the number of
1376 * registers in the array r[] indexed by class.
1377 *
1378 * On PowerPC, we have:
1379 *
1380 * 32 32-bit registers (2 reserved)
1381 * 16 64-bit pseudo registers
1382 * 32 floating-point registers
1383 */
1384 int
1385 COLORMAP(int c, int *r)
1386 {
1387 int num = 0;
1388
1389 switch (c) {
1390 case CLASSA:
1391 num += r[CLASSA];
1392 num += 2*r[CLASSB];
1393 return num < 30;
1394 case CLASSB:
1395 num += 2*r[CLASSB];
1396 num += r[CLASSA];
1397 return num < 16;
1398 case CLASSC:
1399 return num < 32;
1400 case CLASSD:
1401 return r[CLASSD] < DREGCNT;
1402 }
1403 return 0; /* XXX gcc */
1404 }
1405
1406 /*
1407 * Return a class suitable for a specific type.
1408 */
1409 int
1410 gclass(TWORD t)
1411 {
1412 if (t == LONGLONG || t == ULONGLONG)
1413 return CLASSB;
1414 if (t == FLOAT || t == DOUBLE || t == LDOUBLE) {
1415 if (features(FEATURE_HARDFLOAT))
1416 return CLASSC;
1417 if (t == FLOAT)
1418 return CLASSA;
1419 else
1420 return CLASSB;
1421 }
1422 return CLASSA;
1423 }
1424
1425 int
1426 retreg(int t)
1427 {
1428 int c = gclass(t);
1429 if (c == CLASSB)
1430 return R3R4;
1431 else if (c == CLASSC)
1432 return F1;
1433 return R3;
1434 }
1435
1436 /*
1437 * Calculate argument sizes.
1438 */
1439 void
1440 lastcall(NODE *p)
1441 {
1442 NODE *op = p;
1443 int size = 0;
1444
1445 #ifdef PCC_DEBUG
1446 if (x2debug)
1447 printf("lastcall:\n");
1448 #endif
1449
1450 p->n_qual = 0;
1451 if (p->n_op != CALL && p->n_op != FORTCALL && p->n_op != STCALL)
1452 return;
1453
1454 for (p = p->n_right; p->n_op == CM; p = p->n_left)
1455 size += argsize(p->n_right);
1456 size += argsize(p);
1457 op->n_qual = size; /* XXX */
1458 }
1459
1460 /*
1461 * Special shapes.
1462 */
1463 int
1464 special(NODE *p, int shape)
1465 {
1466 int o = p->n_op;
1467
1468 switch (shape) {
1469 case SFUNCALL:
1470 if (o == STCALL || o == USTCALL)
1471 return SRREG;
1472 break;
1473 case SPCON:
1474 if (o == ICON && p->n_name[0] == 0 && (getlval(p) & ~0x7fff) == 0)
1475 return SRDIR;
1476 break;
1477 }
1478 return SRNOPE;
1479 }
1480
1481 static int fset = FEATURE_BIGENDIAN | FEATURE_HARDFLOAT;
1482
1483 /*
1484 * Target-dependent command-line options.
1485 */
1486 void
1487 mflags(char *str)
1488 {
1489 if (strcasecmp(str, "big-endian") == 0) {
1490 fset |= FEATURE_BIGENDIAN;
1491 } else if (strcasecmp(str, "little-endian") == 0) {
1492 fset &= ~FEATURE_BIGENDIAN;
1493 } else if (strcasecmp(str, "soft-float") == 0) {
1494 fset &= ~FEATURE_HARDFLOAT;
1495 } else if (strcasecmp(str, "hard-float") == 0) {
1496 fset |= FEATURE_HARDFLOAT;
1497 } else {
1498 fprintf(stderr, "unknown m option '%s'\n", str);
1499 exit(1);
1500 }
1501 }
1502
1503 int
1504 features(int mask)
1505 {
1506 return ((fset & mask) == mask);
1507 }
1508 /*
1509 * Do something target-dependent for xasm arguments.
1510 * Supposed to find target-specific constraints and rewrite them.
1511 */
1512 int
1513 myxasm(struct interpass *ip, NODE *p)
1514 {
1515 return 0;
1516 }