Debian Codegraph @

arch/mips/local2.c

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