Debian Codegraph @

arch/mips64/local2.c

raw symbols SCIP
1 /* $Id: local2.c,v 1.2 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 /*
30 * MIPS port by Jan Enoksson ([email protected]) and
31 * Simon Olsson ([email protected]) 2005.
32 *
33 * Extended to MIPS64 by Brian Callahan <[email protected]> 2016.
34 */
35
36 #include <ctype.h>
37 #include <string.h>
38 #include <stdlib.h>
39 #include <assert.h>
40
41 #include "pass1.h"
42 #include "pass2.h"
43
44 #ifdef TARGET_BIG_ENDIAN
45 int bigendian = 1;
46 #else
47 int bigendian = 0;
48 #endif
49
50 int nargregs = MIPS_N64_NARGREGS;
51
52 static int argsiz(NODE *p);
53
54 void
55 deflab(int label)
56 {
57 printf(LABFMT ":\n", label);
58 }
59
60 static int regoff[64];
61 static TWORD ftype;
62
63 /*
64 * calculate stack size and offsets
65 */
66 static int
67 offcalc(struct interpass_prolog * ipp)
68 {
69 int i, j, addto;
70
71 addto = p2maxautooff;
72
73 for (i = p2env.p_regs[0], j = 0; i; i >>= 1, j++) {
74 if (i & 1) {
75 addto += SZINT / SZCHAR * 2;
76 regoff[j] = addto;
77 }
78 }
79
80 /* round to 8-byte boundary */
81 addto += 7;
82 addto &= ~7;
83
84 return addto;
85 }
86
87 /*
88 * Print out the prolog assembler.
89 */
90 void
91 prologue(struct interpass_prolog * ipp)
92 {
93 int addto;
94 int i, j;
95
96 ftype = ipp->ipp_type;
97 printf("\t.align 2\n");
98 if (ipp->ipp_vis)
99 printf("\t.globl %s\n", ipp->ipp_name);
100 printf("\t.ent %s\n", ipp->ipp_name);
101 printf("%s:\n", ipp->ipp_name);
102
103 addto = offcalc(ipp);
104
105 /* emit PIC only if -fpic or -fPIC set */
106 if (kflag > 0) {
107 printf("\t.frame %s,%d,%s\n",
108 rnames[FP], ARGINIT/SZCHAR, rnames[RA]);
109 printf("\t.set noreorder\n");
110 printf("\t.cpload $25\t# pseudo-op to load GOT ptr into $25\n");
111 printf("\t.set reorder\n");
112 }
113
114 printf("\tdsubu %s,%s,%d\n", rnames[SP], rnames[SP], ARGINIT/SZCHAR);
115 /* emit PIC only if -fpic or -fPIC set */
116 if (kflag > 0)
117 printf("\t.cprestore 8\t# pseudo-op to store GOT ptr at 8(sp)\n");
118
119 printf("\tsd %s,8(%s)\n", rnames[RA], rnames[SP]);
120 printf("\tsd %s,(%s)\n", rnames[FP], rnames[SP]);
121 printf("\tmove %s,%s\n", rnames[FP], rnames[SP]);
122
123 #ifdef notyet
124 /* profiling */
125 if (pflag) {
126 printf("\t.set noat\n");
127 printf("\tmove %s,%s\t# save current return address\n",
128 rnames[AT], rnames[RA]);
129 printf("\tdsubu %s,%s,16\t# _mcount pops 2 dwords from stack\n",
130 rnames[SP], rnames[SP]);
131 printf("\tjal %s\n", exname("_mcount"));
132 printf("\tnop\n");
133 printf("\t.set at\n");
134 }
135 #endif
136
137 if (addto)
138 printf("\tdsubu %s,%s,%d\n", rnames[SP], rnames[SP], addto);
139
140 for (i = p2env.p_regs[0], j = 0; i; i >>= 1, j++)
141 if (i & 1)
142 printf("\tsd %s,-%d(%s) # save permanent\n",
143 rnames[j], regoff[j], rnames[FP]);
144
145 }
146
147 void
148 eoftn(struct interpass_prolog * ipp)
149 {
150 int i, j;
151
152 (void) offcalc(ipp);
153
154 if (ipp->ipp_ip.ip_lbl == 0)
155 return; /* no code needs to be generated */
156
157 /* return from function code */
158 for (i = p2env.p_regs[0], j = 0; i; i >>= 1, j++) {
159 if (i & 1)
160 printf("\tld %s,-%d(%s)\n\tnop\n",
161 rnames[j], regoff[j], rnames[FP]);
162 }
163
164 printf("\tdaddiu %s,%s,%d\n", rnames[SP], rnames[FP], ARGINIT/SZCHAR);
165 printf("\tld %s,%d(%s)\n", rnames[RA], 8-ARGINIT/SZCHAR, rnames[SP]);
166 printf("\tld %s,%d(%s)\n", rnames[FP], 0-ARGINIT/SZCHAR, rnames[SP]);
167
168 printf("\tjr %s\n", rnames[RA]);
169 printf("\tnop\n");
170
171 #ifdef USE_GAS
172 printf("\t.end %s\n", ipp->ipp_name);
173 printf("\t.size %s,.-%s\n", ipp->ipp_name, ipp->ipp_name);
174 #endif
175 }
176
177 /*
178 * add/sub/...
179 *
180 * Param given:
181 */
182 void
183 hopcode(int f, int o)
184 {
185 char *str;
186
187 switch (o) {
188 case EQ:
189 str = "beqz"; /* pseudo-op */
190 break;
191 case NE:
192 str = "bnez"; /* pseudo-op */
193 break;
194 case ULE:
195 case LE:
196 str = "blez";
197 break;
198 case ULT:
199 case LT:
200 str = "bltz";
201 break;
202 case UGE:
203 case GE:
204 str = "bgez";
205 break;
206 case UGT:
207 case GT:
208 str = "bgtz";
209 break;
210 case PLUS:
211 str = "add";
212 break;
213 case MINUS:
214 str = "sub";
215 break;
216 case AND:
217 str = "and";
218 break;
219 case OR:
220 str = "or";
221 break;
222 case ER:
223 str = "xor";
224 break;
225 default:
226 comperr("hopcode2: %d", o);
227 str = 0; /* XXX gcc */
228 }
229
230 printf("%s%c", str, f);
231 }
232
233 char *
234 rnames[] = {
235 #ifdef USE_GAS
236 /* gnu assembler */
237 "$zero", "$at", "$2", "$3", "$4", "$5", "$6", "$7",
238 "$8", "$9", "$10", "$11", "$12", "$13", "$14", "$15",
239 "$16", "$17", "$18", "$19", "$20", "$21", "$22", "$23",
240 "$24", "$25",
241 "$kt0", "$kt1", "$gp", "$sp", "$fp", "$ra",
242 "$2!!$3!!",
243 "$4!!$5!!", "$5!!$6!!", "$6!!$7!!", "$7!!$8!!",
244 "$8!!$9!!", "$9!!$10!", "$10!$11!", "$11!$12!",
245 "$12!$13!", "$13!$14!", "$14!$15!", "$15!$24!", "$24!$25!",
246 "$16!$17!", "$17!$18!", "$18!$19!", "$19!$20!",
247 "$20!$21!", "$21!$22!", "$22!$23!",
248 #else
249 /* mips assembler */
250 "$zero", "$at", "$v0", "$v1", "$a0", "$a1", "$a2", "$a3",
251 "$a4", "$a5", "$a6", "$a7", "$t4", "$t5", "$t6", "$t7",
252 "$s0", "$s1", "$s2", "$s3", "$s4", "$s5", "$s6", "$s7",
253 "$t8", "$t9",
254 "$k0", "$k1", "$gp", "$sp", "$fp", "$ra",
255 "$v0!$v1!",
256 "$a0!$a1!", "$a1!$a2!", "$a2!$a3!", "$a3!$t0!",
257 "$t0!$t1!", "$t1!$t2!", "$t2!$t3!", "$t3!$t4!",
258 "$t4!$t5!", "$t5!$t6!", "$t6!$t7!", "$t7!$t8!", "$t8!$t9!",
259 "$s0!$s1!", "$s1!$s2!", "$s2!$s3!", "$s3!$s4!",
260 "$s4!$s5!", "$s5!$s6!", "$s6!$s7!",
261 #endif
262 "$f0!$f1!", "$f2!$f3!", "$f4!$f5!", "$f6!$f7!",
263 "$f8!$f9!", "$f10$f11", "$f12$f13", "$f14$f15",
264 "$f16$f17", "$f18$f19", "$f20$f21", "$f22$f23",
265 "$f24$f25", "$f26$f27", "$f28$f29", "$f30$f31",
266 };
267
268 int
269 tlen(NODE *p)
270 {
271 switch (p->n_type) {
272 case CHAR:
273 case UCHAR:
274 return (1);
275
276 case SHORT:
277 case USHORT:
278 return (SZSHORT / SZCHAR);
279
280 case DOUBLE:
281 return (SZDOUBLE / SZCHAR);
282
283 case INT:
284 case UNSIGNED:
285 return (SZINT / SZCHAR);
286
287 case LONG:
288 case ULONG:
289 case LONGLONG:
290 case ULONGLONG:
291 return SZLONGLONG / SZCHAR;
292
293 default:
294 if (!ISPTR(p->n_type))
295 comperr("tlen type %d not pointer");
296 return SZPOINT(p->n_type) / SZCHAR;
297 }
298 }
299
300
301 /*
302 * Push a structure on stack as argument.
303 */
304 static void
305 starg(NODE *p)
306 {
307 int sz = attr_find(p->n_ap, ATTR_P2STRUCT)->iarg(0);
308 //assert(p->n_rval == A1);
309 printf("\tdsubu %s,%s,%d\n", rnames[SP], rnames[SP], sz);
310 /* A0 = dest, A1 = src, A2 = len */
311 printf("\tmove %s,%s\n", rnames[A0], rnames[SP]);
312 printf("\tdli %s,%d\t# structure size\n", rnames[A2], sz);
313 printf("\tdsubu %s,%s,32\n", rnames[SP], rnames[SP]);
314 printf("\tjal %s\t# structure copy\n", exname("memcpy"));
315 printf("\tnop\n");
316 printf("\tdaddiu %s,%s,32\n", rnames[SP], rnames[SP]);
317 }
318
319 /*
320 * Structure assignment.
321 */
322 static void
323 stasg(NODE *p)
324 {
325 assert(p->n_right->n_rval == A1);
326 /* A0 = dest, A1 = src, A2 = len */
327 printf("\tdli %s,%d\t# structure size\n", rnames[A2],
328 attr_find(p->n_ap, ATTR_P2STRUCT)->iarg(0));
329 if (p->n_left->n_op == OREG) {
330 printf("\tdaddiu %s,%s," CONFMT "\t# dest address\n",
331 rnames[A0], rnames[p->n_left->n_rval],
332 getlval(p->n_left));
333 } else if (p->n_left->n_op == NAME) {
334 printf("\tdla %s,", rnames[A0]);
335 adrput(stdout, p->n_left);
336 printf("\n");
337 }
338 printf("\tdsubu %s,%s,32\n", rnames[SP], rnames[SP]);
339 printf("\tjal %s\t# structure copy\n", exname("memcpy"));
340 printf("\tnop\n");
341 printf("\tdaddiu %s,%s,32\n", rnames[SP], rnames[SP]);
342 }
343
344 static void
345 shiftop(NODE *p)
346 {
347 NODE *r = p->n_right;
348 TWORD ty = p->n_type;
349
350 if (p->n_op == LS && r->n_op == ICON && getlval(r) < 32) {
351 expand(p, INBREG, "\tdsrl A1,AL,");
352 printf(CONFMT "\t# 64-bit left-shift\n", 32 - getlval(r));
353 expand(p, INBREG, "\tdsll U1,UL,AR\n");
354 expand(p, INBREG, "\tor U1,U1,A1\n");
355 expand(p, INBREG, "\tdsll A1,AL,AR\n");
356 } else if (p->n_op == LS && r->n_op == ICON && getlval(r) < 64) {
357 expand(p, INBREG, "\tdli A1,0\t# 64-bit left-shift\n");
358 expand(p, INBREG, "\tdsll U1,AL,");
359 printf(CONFMT "\n", getlval(r) - 32);
360 } else if (p->n_op == LS && r->n_op == ICON) {
361 expand(p, INBREG, "\tdli A1,0\t# 64-bit left-shift\n");
362 } else if (p->n_op == RS && r->n_op == ICON && getlval(r) < 32) {
363 expand(p, INBREG, "\tdsll U1,UL,");
364 printf(CONFMT "\t# 64-bit right-shift\n", 32 - getlval(r));
365 expand(p, INBREG, "\tdsrl A1,AL,AR\n");
366 expand(p, INBREG, "\tor A1,A1,U1\n");
367 if (ty == LONG || ty == LONGLONG)
368 expand(p, INBREG, "\tdsra U1,UL,AR\n");
369 else
370 expand(p, INBREG, "\tdsrl U1,UL,AR\n");
371 } else if (p->n_op == RS && r->n_op == ICON && getlval(r) < 64) {
372 if (ty == LONG || ty == LONGLONG) {
373 expand(p, INBREG, "\tdsra U1,UL,31\t# 64-bit right-shift\n");
374 expand(p, INBREG, "\tdsra A1,UL,");
375 }else {
376 expand(p, INBREG, "\tdli U1,0\t# 64-bit right-shift\n");
377 expand(p, INBREG, "\tdsrl A1,UL,");
378 }
379 printf(CONFMT "\n", getlval(r) - 32);
380 } else if (p->n_op == LS && r->n_op == ICON) {
381 expand(p, INBREG, "\tdli A1,0\t# 64-bit right-shift\n");
382 } else {
383 comperr("shiftop");
384 }
385 }
386
387 /*
388 * http://gcc.gnu.org/onlinedocs/gccint/Soft-float-library-routines.html#Soft-float-library-routines
389 */
390 static void
391 fpemulop(NODE *p)
392 {
393 NODE *l = p->n_left;
394 char *ch = NULL;
395
396 if (p->n_op == PLUS && p->n_type == FLOAT) ch = "addsf3";
397 else if (p->n_op == PLUS && p->n_type == DOUBLE) ch = "adddf3";
398 else if (p->n_op == PLUS && p->n_type == LDOUBLE) ch = "addtf3";
399
400 else if (p->n_op == MINUS && p->n_type == FLOAT) ch = "subsf3";
401 else if (p->n_op == MINUS && p->n_type == DOUBLE) ch = "subdf3";
402 else if (p->n_op == MINUS && p->n_type == LDOUBLE) ch = "subtf3";
403
404 else if (p->n_op == MUL && p->n_type == FLOAT) ch = "mulsf3";
405 else if (p->n_op == MUL && p->n_type == DOUBLE) ch = "muldf3";
406 else if (p->n_op == MUL && p->n_type == LDOUBLE) ch = "multf3";
407
408 else if (p->n_op == DIV && p->n_type == FLOAT) ch = "divsf3";
409 else if (p->n_op == DIV && p->n_type == DOUBLE) ch = "divdf3";
410 else if (p->n_op == DIV && p->n_type == LDOUBLE) ch = "divtf3";
411
412 else if (p->n_op == UMINUS && p->n_type == FLOAT) ch = "negsf2";
413 else if (p->n_op == UMINUS && p->n_type == DOUBLE) ch = "negdf2";
414 else if (p->n_op == UMINUS && p->n_type == LDOUBLE) ch = "negtf2";
415
416 else if (p->n_op == EQ && l->n_type == FLOAT) ch = "eqsf2";
417 else if (p->n_op == EQ && l->n_type == DOUBLE) ch = "eqdf2";
418 else if (p->n_op == EQ && l->n_type == LDOUBLE) ch = "eqtf2";
419
420 else if (p->n_op == NE && l->n_type == FLOAT) ch = "nesf2";
421 else if (p->n_op == NE && l->n_type == DOUBLE) ch = "nedf2";
422 else if (p->n_op == NE && l->n_type == LDOUBLE) ch = "netf2";
423
424 else if (p->n_op == GE && l->n_type == FLOAT) ch = "gesf2";
425 else if (p->n_op == GE && l->n_type == DOUBLE) ch = "gedf2";
426 else if (p->n_op == GE && l->n_type == LDOUBLE) ch = "getf2";
427
428 else if (p->n_op == LE && l->n_type == FLOAT) ch = "lesf2";
429 else if (p->n_op == LE && l->n_type == DOUBLE) ch = "ledf2";
430 else if (p->n_op == LE && l->n_type == LDOUBLE) ch = "letf2";
431
432 else if (p->n_op == GT && l->n_type == FLOAT) ch = "gtsf2";
433 else if (p->n_op == GT && l->n_type == DOUBLE) ch = "gtdf2";
434 else if (p->n_op == GT && l->n_type == LDOUBLE) ch = "gttf2";
435
436 else if (p->n_op == LT && l->n_type == FLOAT) ch = "ltsf2";
437 else if (p->n_op == LT && l->n_type == DOUBLE) ch = "ltdf2";
438 else if (p->n_op == LT && l->n_type == LDOUBLE) ch = "lttf2";
439
440 else if (p->n_op == SCONV && p->n_type == FLOAT) {
441 if (l->n_type == DOUBLE) ch = "truncdfsf2";
442 else if (l->n_type == LDOUBLE) ch = "trunctfsf2";
443 else if (l->n_type == ULONGLONG) ch = "floatdisf"; /**/
444 else if (l->n_type == LONGLONG) ch = "floatdisf";
445 else if (l->n_type == LONG) ch = "floatdisf";
446 else if (l->n_type == ULONG) ch = "floatdisf";
447 else if (l->n_type == INT) ch = "floatsisf";
448 else if (l->n_type == UNSIGNED) ch = "floatunsisf";
449 } else if (p->n_op == SCONV && p->n_type == DOUBLE) {
450 if (l->n_type == FLOAT) ch = "extendsfdf2";
451 else if (l->n_type == LDOUBLE) ch = "trunctfdf2";
452 else if (l->n_type == ULONGLONG) ch = "floatunsdidf";
453 else if (l->n_type == LONGLONG) ch = "floatdidf";
454 else if (l->n_type == LONG) ch = "floatdidf";
455 else if (l->n_type == ULONG) ch = "floatunsdidf";
456 else if (l->n_type == INT) ch = "floatsidf";
457 else if (l->n_type == UNSIGNED) ch = "floatunsidf";
458 } else if (p->n_op == SCONV && p->n_type == LDOUBLE) {
459 if (l->n_type == FLOAT) ch = "extendsftf2";
460 else if (l->n_type == DOUBLE) ch = "extenddfdf2";
461 else if (l->n_type == ULONGLONG) ch = "floatunsdidf";
462 else if (l->n_type == LONGLONG) ch = "floatdidf";
463 else if (l->n_type == LONG) ch = "floatdidf";
464 else if (l->n_type == ULONG) ch = "floatunsdidf";
465 else if (l->n_type == INT) ch = "floatsidf";
466 else if (l->n_type == UNSIGNED) ch = "floatunsidf";
467 } else if (p->n_op == SCONV && (p->n_type == ULONG || p->n_type == ULONGLONG)) {
468 if (l->n_type == FLOAT) ch = "fixunssfdi";
469 else if (l->n_type == DOUBLE) ch = "fixunsdfdi";
470 else if (l->n_type == LDOUBLE) ch = "fixunsdfdi";
471 } else if (p->n_op == SCONV && (p->n_type == LONG || p->n_type == LONGLONG)) {
472 if (l->n_type == FLOAT) ch = "fixsfdi";
473 else if (l->n_type == DOUBLE) ch = "fixdfdi";
474 else if (l->n_type == LDOUBLE) ch = "fixdfdi";
475 } else if (p->n_op == SCONV && p->n_type == INT) {
476 if (l->n_type == FLOAT) ch = "fixsfsi";
477 else if (l->n_type == DOUBLE) ch = "fixdfsi";
478 else if (l->n_type == LDOUBLE) ch = "fixdfsi";
479 } else if (p->n_op == SCONV && p->n_type == UNSIGNED) {
480 if (l->n_type == FLOAT) ch = "fixunssfsi";
481 else if (l->n_type == DOUBLE) ch = "fixunsdfsi";
482 else if (l->n_type == LDOUBLE) ch = "fixunsdfsi";
483 }
484
485 if (ch == NULL) comperr("ZF: op=0x%x (%d)\n", p->n_op, p->n_op);
486
487 if (p->n_op == SCONV) {
488 if (l->n_type == FLOAT) {
489 printf("\tdmfc1 %s,", rnames[A0]);
490 adrput(stdout, l);
491 printf("\n\tnop\n");
492 } else if (l->n_type == DOUBLE || l->n_type == LDOUBLE) {
493 printf("\tdmfc1 %s,", rnames[A1]);
494 upput(l, 0);
495 printf("\n\tnop\n");
496 printf("\tdmfc1 %s,", rnames[A0]);
497 adrput(stdout, l);
498 printf("\n\tnop\n");
499 }
500 } else {
501 comperr("ZF: incomplete softfloat - put args in registers");
502 }
503
504 printf("\tjal __%s\t# softfloat operation\n", exname(ch));
505 printf("\tnop\n");
506
507 if (p->n_op >= EQ && p->n_op <= GT)
508 printf("\tcmp %s,0\n", rnames[V0]);
509 }
510
511 /*
512 * http://gcc.gnu.org/onlinedocs/gccint/Integer-library-routines.html#Integer-library-routines
513 */
514 static void
515 emulop(NODE *p)
516 {
517 char *ch = NULL;
518
519 if (p->n_op == LS && (DEUNSIGN(p->n_type) == LONG ||
520 DEUNSIGN(p->n_type) == LONGLONG))
521 ch = "ashldi3";
522 else if (p->n_op == LS && DEUNSIGN(p->n_type) == INT)
523 ch = "ashlsi3";
524
525 else if (p->n_op == RS && (p->n_type == ULONG || p->n_type == ULONGLONG))
526 ch = "lshrdi3";
527 else if (p->n_op == RS && p->n_type == INT)
528 ch = "lshrsi3";
529
530 else if (p->n_op == RS && (p->n_type == LONG || p->n_type == LONGLONG))
531 ch = "ashrdi3";
532 else if (p->n_op == RS && p->n_type == INT)
533 ch = "ashrsi3";
534
535 else if (p->n_op == DIV && (p->n_type == LONG || p->n_type == LONGLONG))
536 ch = "divdi3";
537 else if (p->n_op == DIV && p->n_type == INT)
538 ch = "divsi3";
539
540 else if (p->n_op == DIV && (p->n_type == ULONG || p->n_type == ULONGLONG))
541 ch = "udivdi3";
542 else if (p->n_op == DIV && p->n_type == UNSIGNED)
543 ch = "udivsi3";
544
545 else if (p->n_op == MOD && (p->n_type == LONG || p->n_type == LONGLONG))
546 ch = "moddi3";
547 else if (p->n_op == MOD && p->n_type == INT)
548 ch = "modsi3";
549
550 else if (p->n_op == MOD && (p->n_type == ULONG || p->n_type == ULONGLONG))
551 ch = "umoddi3";
552 else if (p->n_op == MOD && p->n_type == UNSIGNED)
553 ch = "umodsi3";
554
555 else if (p->n_op == MUL && (p->n_type == LONG || p->n_type == LONGLONG))
556 ch = "muldi3";
557 else if (p->n_op == MUL && p->n_type == INT)
558 ch = "mulsi3";
559
560 else if (p->n_op == UMINUS && (p->n_type == LONG || p->n_type == LONGLONG))
561 ch = "negdi2";
562
563 else ch = 0, comperr("ZE");
564 printf("\tdsubu %s,%s,32\n", rnames[SP], rnames[SP]);
565 printf("\tjal __%s\t# emulated operation\n", exname(ch));
566 printf("\tnop\n");
567 printf("\tdaddiu %s,%s,32\n", rnames[SP], rnames[SP]);
568 }
569
570 /*
571 * Emit code to compare two longlong numbers.
572 */
573 static void
574 twollcomp(NODE *p)
575 {
576 int o = p->n_op;
577 int s = getlab2();
578 int e = p->n_label;
579 int cb1, cb2;
580
581 if (o >= ULE)
582 o -= (ULE-LE);
583 switch (o) {
584 case NE:
585 cb1 = 0;
586 cb2 = NE;
587 break;
588 case EQ:
589 cb1 = NE;
590 cb2 = 0;
591 break;
592 case LE:
593 case LT:
594 cb1 = GT;
595 cb2 = LT;
596 break;
597 case GE:
598 case GT:
599 cb1 = LT;
600 cb2 = GT;
601 break;
602
603 default:
604 cb1 = cb2 = 0; /* XXX gcc */
605 }
606 if (p->n_op >= ULE)
607 cb1 += 8, cb2 += 8;
608 expand(p, 0, "\tdsub A1,UL,UR\t# compare 64-bit values (upper)\n");
609 if (cb1) {
610 printf("\t");
611 hopcode(' ', cb1);
612 expand(p, 0, "A1");
613 printf("," LABFMT "\n", s);
614 printf("\tnop\n");
615 }
616 if (cb2) {
617 printf("\t");
618 hopcode(' ', cb2);
619 expand(p, 0, "A1");
620 printf("," LABFMT "\n", e);
621 printf("\tnop\n");
622 }
623 expand(p, 0, "\tdsub A1,AL,AR\t# (and lower)\n");
624 printf("\t");
625 hopcode(' ', o);
626 expand(p, 0, "A1");
627 printf("," LABFMT "\n", e);
628 printf("\tnop\n");
629 deflab(s);
630 }
631
632 static void
633 fpcmpops(NODE *p)
634 {
635 NODE *l = p->n_left;
636
637 switch (p->n_op) {
638 case EQ:
639 if (l->n_type == FLOAT)
640 expand(p, 0, "\tc.eq.s AL,AR\n");
641 else
642 expand(p, 0, "\tc.eq.d AL,AR\n");
643 expand(p, 0, "\tnop\n\tbc1t LC\n");
644 break;
645 case NE:
646 if (l->n_type == FLOAT)
647 expand(p, 0, "\tc.eq.s AL,AR\n");
648 else
649 expand(p, 0, "\tc.eq.d AL,AR\n");
650 expand(p, 0, "\tnop\n\tbc1f LC\n");
651 break;
652 case LT:
653 if (l->n_type == FLOAT)
654 expand(p, 0, "\tc.lt.s AL,AR\n");
655 else
656 expand(p, 0, "\tc.lt.d AL,AR\n");
657 expand(p, 0, "\tnop\n\tbc1t LC\n");
658 break;
659 case GE:
660 if (l->n_type == FLOAT)
661 expand(p, 0, "\tc.lt.s AL,AR\n");
662 else
663 expand(p, 0, "\tc.lt.d AL,AR\n");
664 expand(p, 0, "\tnop\n\tbc1f LC\n");
665 break;
666 case LE:
667 if (l->n_type == FLOAT)
668 expand(p, 0, "\tc.le.s AL,AR\n");
669 else
670 expand(p, 0, "\tc.le.d AL,AR\n");
671 expand(p, 0, "\tnop\n\tbc1t LC\n");
672 break;
673 case GT:
674 if (l->n_type == FLOAT)
675 expand(p, 0, "\tc.le.s AL,AR\n");
676 else
677 expand(p, 0, "\tc.le.d AL,AR\n");
678 expand(p, 0, "\tnop\n\tbc1f LC\n");
679 break;
680 }
681 printf("\tnop\n\tnop\n");
682 }
683
684 void
685 zzzcode(NODE * p, int c)
686 {
687 int sz;
688
689 switch (c) {
690
691 case 'C': /* remove arguments from stack after subroutine call */
692 sz = p->n_qual > 32 ? p->n_qual : 32;
693 printf("\tdaddiu %s,%s,%d\n", rnames[SP], rnames[SP], sz);
694 break;
695
696 case 'D': /* long long comparison */
697 twollcomp(p);
698 break;
699
700 case 'E': /* emit emulated ops */
701 emulop(p);
702 break;
703
704 case 'F': /* emit emulate floating point ops */
705 fpemulop(p);
706 break;
707
708 case 'G': /* emit hardware floating-point compare op */
709 fpcmpops(p);
710 break;
711
712 case 'H': /* structure argument */
713 starg(p);
714 break;
715
716 case 'I': /* high part of init constant */
717 if (p->n_name[0] != '\0')
718 comperr("named highword");
719 printf(CONFMT, (getlval(p) >> 32) & 0xffffffff);
720 break;
721
722 case 'O': /* 64-bit left and right shift operators */
723 shiftop(p);
724 break;
725
726 case 'Q': /* emit struct assign */
727 stasg(p);
728 break;
729
730 default:
731 comperr("zzzcode %c", c);
732 }
733 }
734
735 /* ARGSUSED */
736 int
737 rewfld(NODE * p)
738 {
739 return (1);
740 }
741
742 int
743 fldexpand(NODE *p, int cookie, char **cp)
744 {
745 CONSZ val;
746
747 if (p->n_op == ASSIGN)
748 p = p->n_left;
749 switch (**cp) {
750 case 'S':
751 printf("%d", UPKFSZ(p->n_rval));
752 break;
753 case 'H':
754 printf("%d", UPKFOFF(p->n_rval));
755 break;
756 case 'M':
757 case 'N':
758 val = (CONSZ)1 << UPKFSZ(p->n_rval);
759 --val;
760 val <<= UPKFOFF(p->n_rval);
761 printf("0x%llx", (**cp == 'M' ? val : ~val) & 0xffffffff);
762 break;
763 default:
764 comperr("fldexpand");
765 }
766 return 1;
767 }
768
769 /*
770 * Does the bitfield shape match?
771 */
772 int
773 flshape(NODE * p)
774 {
775 int o = p->n_op;
776
777 if (o == OREG || o == REG || o == NAME)
778 return SRDIR; /* Direct match */
779 if (o == UMUL && shumul(p->n_left, SOREG))
780 return SROREG; /* Convert into oreg */
781 return SRREG; /* put it into a register */
782 }
783
784 /* INTEMP shapes must not contain any temporary registers */
785 /* XXX should this go away now? */
786 int
787 shtemp(NODE * p)
788 {
789 return 0;
790 #if 0
791 int r;
792
793 if (p->n_op == STARG)
794 p = p->n_left;
795
796 switch (p->n_op) {
797 case REG:
798 return (!istreg(p->n_rval));
799
800 case OREG:
801 r = p->n_rval;
802 if (R2TEST(r)) {
803 if (istreg(R2UPK1(r)))
804 return (0);
805 r = R2UPK2(r);
806 }
807 return (!istreg(r));
808
809 case UMUL:
810 p = p->n_left;
811 return (p->n_op != UMUL && shtemp(p));
812 }
813
814 if (optype(p->n_op) != LTYPE)
815 return (0);
816 return (1);
817 #endif
818 }
819
820 void
821 adrcon(CONSZ val)
822 {
823 printf(CONFMT, val);
824 }
825
826 void
827 conput(FILE *fp, NODE *p)
828 {
829 int val = getlval(p);
830
831 switch (p->n_op) {
832 case ICON:
833 if (p->n_name[0] != '\0') {
834 fprintf(fp, "%s", p->n_name);
835 if (getlval(p))
836 fprintf(fp, "+%d", val);
837 } else
838 fprintf(fp, "%d", val);
839 return;
840
841 default:
842 comperr("illegal conput");
843 }
844 }
845
846 /* ARGSUSED */
847 void
848 insput(NODE * p)
849 {
850 comperr("insput");
851 }
852
853 /*
854 * Print lower or upper name of 64-bit register.
855 */
856 static void
857 print_reg64name(FILE *fp, int rval, int hi)
858 {
859 int off = 8 * (hi != 0);
860 char *regname = rnames[rval];
861
862 fprintf(fp, "%c%c",
863 regname[off],
864 regname[off + 1]);
865 if (regname[off + 2] != '!')
866 fputc(regname[off + 2], fp);
867 if (regname[off + 3] != '!')
868 fputc(regname[off + 3], fp);
869 }
870
871 /*
872 * Write out the upper address, like the upper register of a 2-register
873 * reference, or the next memory location.
874 */
875 void
876 upput(NODE * p, int size)
877 {
878
879 size /= SZCHAR;
880 switch (p->n_op) {
881 case REG:
882 if (GCLASS(p->n_rval) == CLASSB || GCLASS(p->n_rval) == CLASSC)
883 print_reg64name(stdout, p->n_rval, 1);
884 else
885 printf("%s", rnames[p->n_rval]);
886 break;
887
888 case NAME:
889 case OREG:
890 setlval(p, getlval(p) + size);
891 adrput(stdout, p);
892 setlval(p, getlval(p) - size);
893 break;
894 case ICON:
895 printf(CONFMT, getlval(p) >> 32);
896 break;
897 default:
898 comperr("upput bad op %d size %d", p->n_op, size);
899 }
900 }
901
902 void
903 adrput(FILE * io, NODE * p)
904 {
905 /* output an address, with offsets, from p */
906
907 if (p->n_op == FLD)
908 p = p->n_left;
909
910 switch (p->n_op) {
911
912 case NAME:
913 if (p->n_name[0] != '\0')
914 fputs(p->n_name, io);
915 if (getlval(p) != 0)
916 fprintf(io, "+" CONFMT, getlval(p));
917 return;
918
919 case OREG:
920 if (getlval(p))
921 fprintf(io, "%d", (int) getlval(p));
922 fprintf(io, "(%s)", rnames[p->n_rval]);
923 return;
924
925 case ICON:
926 /* addressable value of the constant */
927 conput(io, p);
928 return;
929
930 case REG:
931 if (GCLASS(p->n_rval) == CLASSB || GCLASS(p->n_rval) == CLASSC)
932 print_reg64name(io, p->n_rval, 0);
933 else
934 fputs(rnames[p->n_rval], io);
935 return;
936
937 default:
938 comperr("illegal address, op %d, node %p", p->n_op, p);
939 return;
940
941 }
942 }
943
944 /* printf conditional and unconditional branches */
945 void
946 cbgen(int o, int lab)
947 {
948 }
949
950 void
951 myreader(struct interpass * ipole)
952 {
953 }
954
955 #if 0
956 /*
957 * Calculate the stack size for arguments
958 */
959 static int stacksize;
960
961 static void
962 calcstacksize(NODE *p, void *arg)
963 {
964 int sz;
965
966 printf("op=%d\n", p->n_op);
967
968 if (p->n_op != CALL && p->n_op != STCALL)
969 return;
970
971 sz = argsiz(p->n_right);
972 if (sz > stacksize)
973 stacksize = sz;
974
975 #ifdef PCC_DEBUG
976 if (x2debug)
977 printf("stacksize: %d\n", stacksize);
978 #endif
979 }
980 #endif
981
982 /*
983 * If we're big endian, then all OREG loads of a type
984 * larger than the destination, must have the
985 * offset changed to point to the correct bytes in memory.
986 */
987 static void
988 offchg(NODE *p, void *arg)
989 {
990 NODE *l;
991
992 if (p->n_op != SCONV)
993 return;
994
995 l = p->n_left;
996
997 if (l->n_op != OREG)
998 return;
999
1000 switch (l->n_type) {
1001 case SHORT:
1002 case USHORT:
1003 if (DEUNSIGN(p->n_type) == CHAR)
1004 setlval(l, getlval(l) + 1);
1005 break;
1006 case INT:
1007 case UNSIGNED:
1008 if (DEUNSIGN(p->n_type) == CHAR)
1009 setlval(l, getlval(l + 3));
1010 else if (DEUNSIGN(p->n_type) == SHORT)
1011 setlval(l, getlval(l + 2));
1012 break;
1013 case LONG:
1014 case ULONG:
1015 case LONGLONG:
1016 case ULONGLONG:
1017 if (DEUNSIGN(p->n_type) == CHAR)
1018 setlval(l, getlval(l + 7));
1019 else if (DEUNSIGN(p->n_type) == SHORT)
1020 setlval(l, getlval(l + 6));
1021 else if (DEUNSIGN(p->n_type) == INT)
1022 setlval(l, getlval(l + 4));
1023 break;
1024 default:
1025 comperr("offchg: unknown type");
1026 break;
1027 }
1028 }
1029
1030 /*
1031 * Remove some PCONVs after OREGs are created.
1032 */
1033 static void
1034 pconv2(NODE * p, void *arg)
1035 {
1036 NODE *q;
1037
1038 if (p->n_op == PLUS) {
1039 if (p->n_type == (PTR | SHORT) || p->n_type == (PTR | USHORT)) {
1040 if (p->n_right->n_op != ICON)
1041 return;
1042 if (p->n_left->n_op != PCONV)
1043 return;
1044 if (p->n_left->n_left->n_op != OREG)
1045 return;
1046 q = p->n_left->n_left;
1047 nfree(p->n_left);
1048 p->n_left = q;
1049 /*
1050 * This will be converted to another OREG later.
1051 */
1052 }
1053 }
1054 }
1055
1056 void
1057 mycanon(NODE * p)
1058 {
1059 walkf(p, pconv2, 0);
1060 }
1061
1062 void
1063 myoptim(struct interpass * ipole)
1064 {
1065 struct interpass *ip;
1066
1067 #ifdef PCC_DEBUG
1068 if (x2debug)
1069 printf("myoptim:\n");
1070 #endif
1071
1072 #if 0
1073 stacksize = 0;
1074 #endif
1075
1076 DLIST_FOREACH(ip, ipole, qelem) {
1077 if (ip->type != IP_NODE)
1078 continue;
1079 if (bigendian)
1080 walkf(ip->ip_node, offchg, 0);
1081 #if 0
1082 walkf(ip->ip_node, calcstacksize, 0);
1083 #endif
1084 }
1085 }
1086
1087 /*
1088 * Move data between registers. While basic registers aren't a problem,
1089 * we have to handle the special case of overlapping composite registers.
1090 */
1091 void
1092 rmove(int s, int d, TWORD t)
1093 {
1094 switch (t) {
1095 case LONG:
1096 case ULONG:
1097 case LONGLONG:
1098 case ULONGLONG:
1099 if (s == d+1) {
1100 /* dh = sl, copy low word first */
1101 printf("\tmove ");
1102 print_reg64name(stdout, d, 0);
1103 printf(",");
1104 print_reg64name(stdout, s, 0);
1105 printf("\t# 64-bit rmove\n");
1106 printf("\tmove ");
1107 print_reg64name(stdout, d, 1);
1108 printf(",");
1109 print_reg64name(stdout, s, 1);
1110 printf("\n");
1111 } else {
1112 /* copy high word first */
1113 printf("\tmove ");
1114 print_reg64name(stdout, d, 1);
1115 printf(",");
1116 print_reg64name(stdout, s, 1);
1117 printf(" # 64-bit rmove\n");
1118 printf("\tmove ");
1119 print_reg64name(stdout, d, 0);
1120 printf(",");
1121 print_reg64name(stdout, s, 0);
1122 printf("\n");
1123 }
1124 break;
1125 case FLOAT:
1126 case DOUBLE:
1127 case LDOUBLE:
1128 if (t == FLOAT)
1129 printf("\tmov.s ");
1130 else
1131 printf("\tmov.d ");
1132 print_reg64name(stdout, d, 0);
1133 printf(",");
1134 print_reg64name(stdout, s, 0);
1135 printf("\t# float/double rmove\n");
1136 break;
1137 default:
1138 printf("\tmove %s,%s\t# default rmove\n", rnames[d], rnames[s]);
1139 }
1140 }
1141
1142
1143 /*
1144 * For class c, find worst-case displacement of the number of
1145 * registers in the array r[] indexed by class.
1146 *
1147 * On MIPS64, we have:
1148 *
1149 * 32 64-bit registers (8 reserved)
1150 * 26 64-bit pseudo registers (1 unavailable)
1151 * 32 floating-point registers
1152 */
1153 int
1154 COLORMAP(int c, int *r)
1155 {
1156 int num = 0;
1157
1158 switch (c) {
1159 case CLASSA:
1160 num += r[CLASSA];
1161 num += 2*r[CLASSB];
1162 return num < 24;
1163 case CLASSB:
1164 num += 2*r[CLASSB];
1165 num += r[CLASSA];
1166 return num < 25;
1167 case CLASSC:
1168 num += r[CLASSC];
1169 return num < 6;
1170 }
1171 comperr("COLORMAP");
1172 return 0; /* XXX gcc */
1173 }
1174
1175 /*
1176 * Return a class suitable for a specific type.
1177 */
1178 int
1179 gclass(TWORD t)
1180 {
1181 if (t == LONG || t == ULONG || t == LONGLONG || t == ULONGLONG)
1182 return CLASSB;
1183 if (t >= FLOAT && t <= LDOUBLE)
1184 return CLASSC;
1185 return CLASSA;
1186 }
1187
1188 /*
1189 * Calculate argument sizes.
1190 */
1191 void
1192 lastcall(NODE *p)
1193 {
1194 int sz;
1195
1196 #ifdef PCC_DEBUG
1197 if (x2debug)
1198 printf("lastcall:\n");
1199 #endif
1200
1201 p->n_qual = 0;
1202 if (p->n_op != CALL && p->n_op != FORTCALL && p->n_op != STCALL)
1203 return;
1204
1205 sz = argsiz(p->n_right);
1206
1207 if ((sz > 8*nargregs) && (sz & 7) != 0) {
1208 printf("\tdsubu %s,%s,8\t# align stack\n",
1209 rnames[SP], rnames[SP]);
1210 sz += 8;
1211 assert((sz & 7) == 0);
1212 }
1213
1214 p->n_qual = sz; /* XXX */
1215 }
1216
1217 static int
1218 argsiz(NODE *p)
1219 {
1220 TWORD t;
1221 int size = 0;
1222 int sz = 0;
1223
1224 if (p->n_op == CM) {
1225 size = argsiz(p->n_left);
1226 p = p->n_right;
1227 }
1228
1229 t = p->n_type;
1230 if (t < LONGLONG || t > BTMASK)
1231 sz = 4;
1232 else if (DEUNSIGN(t) == LONG || DEUNSIGN(t) == LONGLONG)
1233 sz = 8;
1234 else if (t == DOUBLE || t == LDOUBLE)
1235 sz = 8;
1236 else if (t == FLOAT)
1237 sz = 4;
1238 else if (t == STRTY || t == UNIONTY)
1239 sz = attr_find(p->n_ap, ATTR_P2STRUCT)->iarg(0);
1240
1241 if (p->n_type == STRTY || p->n_type == UNIONTY) {
1242 return (size + sz);
1243 }
1244
1245 /* alignment */
1246 if (sz == 8 && (size & 7) != 0)
1247 sz += 8;
1248
1249 // printf("size=%d, sz=%d -> %d\n", size, sz, size + sz);
1250 return (size + sz);
1251 }
1252
1253 /*
1254 * Special shapes.
1255 */
1256 int
1257 special(NODE *p, int shape)
1258 {
1259 int o = p->n_op;
1260
1261 if (o != ICON || p->n_name[0] != 0)
1262 return SRNOPE;
1263
1264 switch(shape) {
1265 case SPCON:
1266 if ((getlval(p) & ~0xffff) == 0)
1267 return SRDIR;
1268 break;
1269 }
1270
1271 return SRNOPE;
1272 }
1273
1274 /*
1275 * Target-dependent command-line options.
1276 */
1277 void
1278 mflags(char *str)
1279 {
1280 if (strcasecmp(str, "big-endian") == 0) {
1281 bigendian = 1;
1282 } else if (strcasecmp(str, "little-endian") == 0) {
1283 bigendian = 0;
1284 } else {
1285 fprintf(stderr, "unknown m option '%s'\n", str);
1286 exit(1);
1287 }
1288
1289 #if 0
1290 else if (strcasecmp(str, "ips2")) {
1291 } else if (strcasecmp(str, "ips2")) {
1292 } else if (strcasecmp(str, "ips3")) {
1293 } else if (strcasecmp(str, "ips4")) {
1294 } else if (strcasecmp(str, "hard-float")) {
1295 } else if (strcasecmp(str, "soft-float")) {
1296 } else if (strcasecmp(str, "abi=32")) {
1297 nargregs = MIPS_O32_NARGREGS;
1298 } else if (strcasecmp(str, "abi=n32")) {
1299 nargregs = MIPS_N32_NARGREGS;
1300 } else if (strcasecmp(str, "abi=64")) {
1301 nargregs = MIPS_N64_NARGREGS;
1302 }
1303 #endif
1304 }
1305 /*
1306 * Do something target-dependent for xasm arguments.
1307 * Supposed to find target-specific constraints and rewrite them.
1308 */
1309 int
1310 myxasm(struct interpass *ip, NODE *p)
1311 {
1312 return 0;
1313 }