Debian Codegraph @

arch/mips/code.c

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