Debian Codegraph @

arch/mips64/code.c

raw symbols SCIP
1 /* $Id: code.c,v 1.1 2016/07/06 07:49:48 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 /*
31 * MIPS port by Jan Enoksson ([email protected]) and
32 * Simon Olsson ([email protected]) 2005.
33 *
34 * Extended to MIPS64 by Brian Callahan <[email protected]> 2016.
35 */
36
37 #include <assert.h>
38 #include "pass1.h"
39
40 #ifndef LANG_CXX
41 #undef NIL
42 #define NIL NULL
43 #define NODE P1ND
44 #define nfree p1nfree
45 #define ccopy p1tcopy
46 #define tfree p1tfree
47 #endif
48
49 /*
50 * Print out assembler segment name.
51 */
52 void
53 setseg(int seg, char *name)
54 {
55 switch (seg) {
56 case PROG: name = ".text"; break;
57 case DATA:
58 case LDATA: name = ".data"; break;
59 case STRNG:
60 case RDATA: name = ".section .rodata"; break;
61 case UDATA: break;
62 case PICLDATA:
63 case PICDATA: name = ".section .data.rel.rw,\"aw\",@progbits"; break;
64 case PICRDATA: name = ".section .data.rel.ro,\"aw\",@progbits"; break;
65 case TLSDATA: name = ".section .tdata,\"awT\",@progbits"; break;
66 case TLSUDATA: name = ".section .tbss,\"awT\",@nobits"; break;
67 case CTORS: name = ".section\t.ctors,\"aw\",@progbits"; break;
68 case DTORS: name = ".section\t.dtors,\"aw\",@progbits"; break;
69 case NMSEG:
70 printf("\t.section %s,\"a%c\",@progbits\n", name,
71 cftnsp ? 'x' : 'w');
72 return;
73 }
74 printf("\t%s\n", name);
75 }
76
77 /*
78 * Define everything needed to print out some data (or text).
79 * This means segment, alignment, visibility, etc.
80 */
81 void
82 defloc(struct symtab *sp)
83 {
84 char *n;
85
86 if (ISFTN(sp->stype))
87 return; /* XXX until fixed */
88
89 n = getexname(sp);
90
91 if (sp->sclass == EXTDEF)
92 printf(" .globl %s\n", n);
93 if (sp->slevel == 0) {
94 #ifdef USE_GAS
95 printf("\t.type %s,@%s\n", n,
96 ISFTN(sp->stype) ? "function" : "object");
97 if (!ISFTN(sp->stype))
98 printf("\t.size %s," CONFMT "\n", n,
99 tsize(sp->stype, sp->sdf, sp->sap));
100 #endif
101 printf("%s:\n", n);
102 } else
103 printf(LABFMT ":\n", sp->soffset);
104 }
105
106
107 /*
108 * cause the alignment to become a multiple of n
109 */
110 void
111 defalign(int n)
112 {
113 n = ispow2(n / SZCHAR);
114 if (n == -1)
115 cerror("defalign: n != 2^i");
116 printf("\t.p2align %d\n", n);
117 }
118
119 static int rvnr;
120
121 /*
122 * code for the end of a function
123 * deals with struct return here
124 */
125 void
126 efcode(void)
127 {
128 NODE *p, *q;
129 int tempnr;
130 int ty;
131
132 if (cftnsp->stype != STRTY+FTN && cftnsp->stype != UNIONTY+FTN)
133 return;
134
135 ty = cftnsp->stype - FTN;
136
137 q = block(REG, NIL, NIL, INCREF(ty), 0, cftnsp->sap);
138 q->n_rval = V0;
139 p = tempnode(0, INCREF(ty), 0, cftnsp->sap);
140 tempnr = regno(p);
141 p = buildtree(ASSIGN, p, q);
142 ecomp(p);
143
144 q = tempnode(tempnr, INCREF(ty), 0, cftnsp->sap);
145 q = buildtree(UMUL, q, NIL);
146
147 p = tempnode(rvnr, INCREF(ty), 0, cftnsp->sap);
148 p = buildtree(UMUL, p, NIL);
149
150 p = buildtree(ASSIGN, p, q);
151 ecomp(p);
152
153 q = tempnode(rvnr, INCREF(ty), 0, cftnsp->sap);
154 p = block(REG, NIL, NIL, INCREF(ty), 0, cftnsp->sap);
155 p->n_rval = V0;
156 p = buildtree(ASSIGN, p, q);
157 ecomp(p);
158 }
159
160 /* Put a symbol in a temporary
161 * used by bfcode() and its helpers */
162 static void
163 putintemp(struct symtab *sym)
164 {
165 NODE *p;
166 p = tempnode(0, sym->stype, sym->sdf, sym->sap);
167 p = buildtree(ASSIGN, p, nametree(sym));
168 sym->soffset = regno(p->n_left);
169 sym->sflags |= STNODE;
170 ecomp(p);
171 }
172
173 /* setup the hidden pointer to struct return parameter
174 * used by bfcode() */
175 static void
176 param_retptr(void)
177 {
178 NODE *p, *q;
179
180 p = tempnode(0, PTR+STRTY, 0, cftnsp->sap);
181 rvnr = regno(p);
182 q = block(REG, NIL, NIL, PTR+STRTY, 0, cftnsp->sap);
183 q->n_rval = A0;
184 p = buildtree(ASSIGN, p, q);
185 ecomp(p);
186 }
187
188 /* setup struct parameter
189 * push the registers out to memory
190 * used by bfcode() */
191 static void
192 param_struct(struct symtab *sym, int *regp)
193 {
194 int reg = *regp;
195 NODE *p, *q;
196 int navail;
197 int sz;
198 int off;
199 int num;
200 int i;
201
202 navail = nargregs - (reg - A0);
203 sz = tsize(sym->stype, sym->sdf, sym->sap) / SZINT;
204 off = (ARGINIT/SZINT * 2) + (reg - A0);
205 num = sz > navail ? navail : sz;
206 for (i = 0; i < num; i++) {
207 q = block(REG, NIL, NIL, INT, 0, 0);
208 q->n_rval = reg++;
209 p = block(REG, NIL, NIL, INT, 0, 0);
210 p->n_rval = FP;
211 p = block(PLUS, p, bcon(8*off++), INT, 0, 0);
212 p = block(UMUL, p, NIL, INT, 0, 0);
213 p = buildtree(ASSIGN, p, q);
214 ecomp(p);
215 }
216
217 *regp = reg;
218 }
219
220 /* setup a 64-bit parameter (double/ldouble/longlong)
221 * used by bfcode() */
222 static void
223 param_64bit(struct symtab *sym, int *regp, int dotemps)
224 {
225 int reg = *regp;
226 NODE *p, *q;
227 int navail;
228
229 /* alignment */
230 ++reg;
231 reg &= ~1;
232
233 navail = nargregs - (reg - A0);
234
235 if (navail < 2) {
236 /* would have appeared half in registers/half
237 * on the stack, but alignment ensures it
238 * appears on the stack */
239 if (dotemps)
240 putintemp(sym);
241 *regp = reg;
242 return;
243 }
244
245 q = block(REG, NIL, NIL, sym->stype, sym->sdf, sym->sap);
246 q->n_rval = A0A1 + (reg - A0);
247 if (dotemps) {
248 p = tempnode(0, sym->stype, sym->sdf, sym->sap);
249 sym->soffset = regno(p);
250 sym->sflags |= STNODE;
251 } else {
252 p = nametree(sym);
253 }
254 p = buildtree(ASSIGN, p, q);
255 ecomp(p);
256 *regp = reg + 2;
257 }
258
259 /* setup a 32-bit param on the stack
260 * used by bfcode() */
261 static void
262 param_32bit(struct symtab *sym, int *regp, int dotemps)
263 {
264 NODE *p, *q;
265
266 q = block(REG, NIL, NIL, sym->stype, sym->sdf, sym->sap);
267 q->n_rval = (*regp)++;
268 if (dotemps) {
269 p = tempnode(0, sym->stype, sym->sdf, sym->sap);
270 sym->soffset = regno(p);
271 sym->sflags |= STNODE;
272 } else {
273 p = nametree(sym);
274 }
275 p = buildtree(ASSIGN, p, q);
276 ecomp(p);
277 }
278
279 /*
280 * XXX This is a hack. We cannot have (l)doubles in more than one
281 * register class. So we bounce them in and out of temps to
282 * move them in and out of the right registers.
283 */
284 static void
285 param_double(struct symtab *sym, int *regp, int dotemps)
286 {
287 int reg = *regp;
288 NODE *p, *q, *t;
289 int navail;
290 int tmpnr;
291
292 /* alignment */
293 ++reg;
294 reg &= ~1;
295
296 navail = nargregs - (reg - A0);
297
298 if (navail < 2) {
299 /* would have appeared half in registers/half
300 * on the stack, but alignment ensures it
301 * appears on the stack */
302 if (dotemps)
303 putintemp(sym);
304 *regp = reg;
305 return;
306 }
307
308 t = tempnode(0, LONGLONG, 0, 0);
309 tmpnr = regno(t);
310 q = block(REG, NIL, NIL, LONGLONG, 0, 0);
311 q->n_rval = A0A1 + (reg - A0);
312 p = buildtree(ASSIGN, t, q);
313 ecomp(p);
314
315 if (dotemps) {
316 sym->soffset = tmpnr;
317 sym->sflags |= STNODE;
318 } else {
319 q = tempnode(tmpnr, sym->stype, sym->sdf, sym->sap);
320 p = nametree(sym);
321 p = buildtree(ASSIGN, p, q);
322 ecomp(p);
323 }
324 *regp = reg + 2;
325 }
326
327 /*
328 * XXX This is a hack. We cannot have floats in more than one
329 * register class. So we bounce them in and out of temps to
330 * move them in and out of the right registers.
331 */
332 static void
333 param_float(struct symtab *sym, int *regp, int dotemps)
334 {
335 NODE *p, *q, *t;
336 int tmpnr;
337
338 t = tempnode(0, INT, 0, 0);
339 tmpnr = regno(t);
340 q = block(REG, NIL, NIL, INT, 0, 0);
341 q->n_rval = (*regp)++;
342 p = buildtree(ASSIGN, t, q);
343 ecomp(p);
344
345 if (dotemps) {
346 sym->soffset = tmpnr;
347 sym->sflags |= STNODE;
348 } else {
349 q = tempnode(tmpnr, sym->stype, sym->sdf, sym->sap);
350 p = nametree(sym);
351 p = buildtree(ASSIGN, p, q);
352 ecomp(p);
353 }
354 }
355
356 /*
357 * code for the beginning of a function; a is an array of
358 * indices in symtab for the arguments; n is the number
359 */
360 void
361 bfcode(struct symtab **sp, int cnt)
362 {
363 union arglist *usym;
364 int lastreg = A0 + nargregs - 1;
365 int saveallargs = 0;
366 int i, reg;
367
368 /*
369 * Detect if this function has ellipses and save all
370 * argument register onto stack.
371 */
372 usym = cftnsp->sdf->dfun;
373 while (usym && usym->type != TNULL) {
374 if (usym->type == TELLIPSIS) {
375 saveallargs = 1;
376 break;
377 }
378 ++usym;
379 }
380
381 reg = A0;
382
383 /* assign hidden return structure to temporary */
384 if (cftnsp->stype == STRTY+FTN || cftnsp->stype == UNIONTY+FTN) {
385 param_retptr();
386 ++reg;
387 }
388
389 /* recalculate the arg offset and create TEMP moves */
390 for (i = 0; i < cnt; i++) {
391
392 if ((reg > lastreg) && !xtemps)
393 break;
394 else if (reg > lastreg)
395 putintemp(sp[i]);
396 else if (sp[i]->stype == STRTY || sp[i]->stype == UNIONTY)
397 param_struct(sp[i], &reg);
398 else if ((DEUNSIGN(sp[i]->stype) == LONG) ||
399 (DEUNSIGN(sp[i]->stype) == LONGLONG))
400 param_64bit(sp[i], &reg, xtemps && !saveallargs);
401 else if (sp[i]->stype == DOUBLE || sp[i]->stype == LDOUBLE)
402 param_double(sp[i], &reg, xtemps && !saveallargs);
403 else if (sp[i]->stype == FLOAT)
404 param_float(sp[i], &reg, xtemps && !saveallargs);
405 else
406 param_32bit(sp[i], &reg, xtemps && !saveallargs);
407 }
408
409 /* if saveallargs, save the rest of the args onto the stack */
410 if (!saveallargs)
411 return;
412 while (reg <= lastreg) {
413 NODE *p, *q;
414 int off = ARGINIT/SZINT + (reg - A0);
415 q = block(REG, NIL, NIL, INT, 0, 0);
416 q->n_rval = reg++;
417 p = block(REG, NIL, NIL, INT, 0, 0);
418 p->n_rval = FP;
419 p = block(PLUS, p, bcon(8*off), INT, 0, 0);
420 p = block(UMUL, p, NIL, INT, 0, 0);
421 p = buildtree(ASSIGN, p, q);
422 ecomp(p);
423 }
424
425 }
426
427
428 /* called just before final exit */
429 /* flag is 1 if errors, 0 if none */
430 void
431 ejobcode(int flag)
432 {
433 }
434
435 void
436 bjobcode(void)
437 {
438 printf("\t.section .mdebug.abi64\n");
439 printf("\t.previous\n");
440
441 /* only if -fpic or -fPIC */
442 if (kflag > 0)
443 printf("\t.abicalls\n");
444 }
445
446 #ifdef notdef
447 /*
448 * Print character t at position i in one string, until t == -1.
449 * Locctr & label is already defined.
450 */
451 void
452 bycode(int t, int i)
453 {
454 static int lastoctal = 0;
455
456 /* put byte i+1 in a string */
457
458 if (t < 0) {
459 if (i != 0)
460 puts("\\000\"");
461 } else {
462 if (i == 0)
463 printf("\t.ascii \"");
464 if (t == 0)
465 return;
466 else if (t == '\\' || t == '"') {
467 lastoctal = 0;
468 putchar('\\');
469 putchar(t);
470 } else if (t == 011) {
471 printf("\\t");
472 } else if (t == 012) {
473 printf("\\n");
474 } else if (t < 040 || t >= 0177) {
475 lastoctal++;
476 printf("\\%o",t);
477 } else if (lastoctal && '0' <= t && t <= '9') {
478 lastoctal = 0;
479 printf("\"\n\t.ascii \"%c", t);
480 } else {
481 lastoctal = 0;
482 putchar(t);
483 }
484 }
485 }
486 #endif
487
488 /* fix up type of field p */
489 void
490 fldty(struct symtab *p)
491 {
492 }
493
494 /*
495 * XXX - fix genswitch.
496 */
497 int
498 mygenswitch(int num, TWORD type, struct swents **p, int n)
499 {
500 return 0;
501 }
502
503
504 /* setup call stack with a structure */
505 /* called from moveargs() */
506 static NODE *
507 movearg_struct(NODE *p, NODE *parent, int *regp)
508 {
509 int reg = *regp;
510 NODE *l, *q, *t, *r;
511 int tmpnr;
512 int navail;
513 int off;
514 int num;
515 int sz;
516 int ty;
517 int i;
518
519 navail = nargregs - (reg - A0);
520 sz = tsize(p->n_type, p->n_df, p->n_ap) / SZINT;
521 num = sz > navail ? navail : sz;
522
523 l = p->n_left;
524 nfree(p);
525 ty = l->n_type;
526 t = tempnode(0, l->n_type, l->n_df, l->n_ap);
527 tmpnr = regno(t);
528 l = buildtree(ASSIGN, t, l);
529
530 if (p != parent) {
531 q = parent->n_left;
532 } else
533 q = NULL;
534
535 /* copy structure into registers */
536 for (i = 0; i < num; i++) {
537 t = tempnode(tmpnr, ty, 0, 0);
538 t = block(SCONV, t, NIL, PTR+INT, 0, 0);
539 t = block(PLUS, t, bcon(8*i), PTR+INT, 0, 0);
540 t = buildtree(UMUL, t, NIL);
541
542 r = block(REG, NIL, NIL, INT, 0, 0);
543 r->n_rval = reg++;
544
545 r = buildtree(ASSIGN, r, t);
546 if (q == NULL)
547 q = r;
548 else
549 q = block(CM, q, r, INT, 0, 0);
550 }
551 off = ARGINIT/SZINT + nargregs;
552 for (i = num; i < sz; i++) {
553 t = tempnode(tmpnr, ty, 0, 0);
554 t = block(SCONV, t, NIL, PTR+INT, 0, 0);
555 t = block(PLUS, t, bcon(8*i), PTR+INT, 0, 0);
556 t = buildtree(UMUL, t, NIL);
557
558 r = block(REG, NIL, NIL, INT, 0, 0);
559 r->n_rval = FP;
560 r = block(PLUS, r, bcon(8*off++), INT, 0, 0);
561 r = block(UMUL, r, NIL, INT, 0, 0);
562
563 r = buildtree(ASSIGN, r, t);
564 if (q == NULL)
565 q = r;
566 else
567 q = block(CM, q, r, INT, 0, 0);
568 }
569
570 if (parent->n_op == CM) {
571 parent->n_left = q;
572 q = l;
573 } else {
574 q = block(CM, q, l, INT, 0, 0);
575 }
576
577 *regp = reg;
578 return q;
579 }
580
581 /* setup call stack with 64-bit argument */
582 /* called from moveargs() */
583 static NODE *
584 movearg_64bit(NODE *p, int *regp)
585 {
586 int reg = *regp;
587 NODE *q;
588 int lastarg;
589
590 /* alignment */
591 ++reg;
592 reg &= ~1;
593
594 lastarg = A0 + nargregs - 1;
595 if (reg > lastarg) {
596 *regp = reg;
597 return block(FUNARG, p, NIL, p->n_type, p->n_df, p->n_ap);
598 }
599
600 q = block(REG, NIL, NIL, p->n_type, p->n_df, p->n_ap);
601 q->n_rval = A0A1 + (reg - A0);
602 q = buildtree(ASSIGN, q, p);
603
604 *regp = reg + 2;
605 return q;
606 }
607
608 /* setup call stack with 32-bit argument */
609 /* called from moveargs() */
610 static NODE *
611 movearg_32bit(NODE *p, int *regp)
612 {
613 int reg = *regp;
614 NODE *q;
615
616 q = block(REG, NIL, NIL, p->n_type, p->n_df, p->n_ap);
617 q->n_rval = reg++;
618 q = buildtree(ASSIGN, q, p);
619
620 *regp = reg;
621 return q;
622 }
623
624 static NODE *
625 moveargs(NODE *p, int *regp)
626 {
627 NODE *r, **rp;
628 int lastreg;
629 int reg;
630
631 if (p->n_op == CM) {
632 p->n_left = moveargs(p->n_left, regp);
633 r = p->n_right;
634 rp = &p->n_right;
635 } else {
636 r = p;
637 rp = &p;
638 }
639
640 lastreg = A0 + nargregs - 1;
641 reg = *regp;
642
643 if (reg > lastreg && r->n_op != STARG)
644 *rp = block(FUNARG, r, NIL, r->n_type, r->n_df, r->n_ap);
645 else if (r->n_op == STARG) {
646 *rp = movearg_struct(r, p, regp);
647 } else if ((DEUNSIGN(r->n_type) == LONG) ||
648 (DEUNSIGN(r->n_type) == LONGLONG)) {
649 *rp = movearg_64bit(r, regp);
650 } else if (r->n_type == DOUBLE || r->n_type == LDOUBLE) {
651 /* XXX bounce in and out of temporary to change to longlong */
652 NODE *t1 = tempnode(0, LONGLONG, 0, 0);
653 int tmpnr = regno(t1);
654 NODE *t2 = tempnode(tmpnr, r->n_type, r->n_df, r->n_ap);
655 t1 = movearg_64bit(t1, regp);
656 r = block(ASSIGN, t2, r, r->n_type, r->n_df, r->n_ap);
657 if (p->n_op == CM) {
658 p->n_left = buildtree(CM, p->n_left, t1);
659 p->n_right = r;
660 } else {
661 p = buildtree(CM, t1, r);
662 }
663 } else if (r->n_type == FLOAT) {
664 /* XXX bounce in and out of temporary to change to int */
665 NODE *t1 = tempnode(0, INT, 0, 0);
666 int tmpnr = regno(t1);
667 NODE *t2 = tempnode(tmpnr, r->n_type, r->n_df, r->n_ap);
668 t1 = movearg_32bit(t1, regp);
669 r = block(ASSIGN, t2, r, r->n_type, r->n_df, r->n_ap);
670 if (p->n_op == CM) {
671 p->n_left = buildtree(CM, p->n_left, t1);
672 p->n_right = r;
673 } else {
674 p = buildtree(CM, t1, r);
675 }
676 } else {
677 *rp = movearg_32bit(r, regp);
678 }
679
680 return p;
681 }
682
683 /*
684 * Called with a function call with arguments as argument.
685 * This is done early in buildtree() and only done once.
686 */
687 NODE *
688 funcode(NODE *p)
689 {
690 int regnum = A0;
691 NODE *l, *r, *t, *q;
692 int ty;
693
694 l = p->n_left;
695 r = p->n_right;
696
697 /*
698 * if returning a structure, make the first argument
699 * a hidden pointer to return structure.
700 */
701 ty = DECREF(l->n_type);
702 if (ty == STRTY+FTN || ty == UNIONTY+FTN) {
703 ty = DECREF(l->n_type) - FTN;
704 q = tempnode(0, ty, l->n_df, l->n_ap);
705 q = buildtree(ADDROF, q, NIL);
706 if (r->n_op != CM) {
707 p->n_right = block(CM, q, r, INCREF(ty),
708 l->n_df, l->n_ap);
709 } else {
710 for (t = r; t->n_left->n_op == CM; t = t->n_left)
711 ;
712 t->n_left = block(CM, q, t->n_left, INCREF(ty),
713 l->n_df, l->n_ap);
714 }
715 }
716
717 p->n_right = moveargs(p->n_right, &regnum);
718
719 return p;
720 }
721
722 NODE *
723 builtin_cfa(const struct bitable *bt, NODE *a)
724 {
725 uerror("missing builtin_cfa");
726 return bcon(0);
727 }
728
729 NODE *
730 builtin_frame_address(const struct bitable *bt, NODE *a)
731 {
732 uerror("missing builtin_frame_address");
733 return bcon(0);
734 }
735
736 NODE *
737 builtin_return_address(const struct bitable *bt, NODE *a)
738 {
739 uerror("missing builtin_return_address");
740 return bcon(0);
741 }
742