Debian Codegraph @

arch/mips64/local.c

raw symbols SCIP
1 /* $Id: local.c,v 1.2 2018/12/02 20:25:55 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 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 /*
28 * MIPS port by Jan Enoksson ([email protected]) and
29 * Simon Olsson ([email protected]) 2005.
30 *
31 * Extended to MIPS64 by Brian Callahan <[email protected]> 2016.
32 */
33
34 #include "pass1.h"
35
36 #ifndef LANG_CXX
37 #define NODE P1ND
38 #define ccopy p1tcopy
39 #define tcopy p1tcopy
40 #define tfree p1tfree
41 #define nfree p1nfree
42 #define fwalk p1fwalk
43 #define talloc p1alloc
44 #endif
45
46 #define IALLOC(sz) (isinlining ? permalloc(sz) : tmpalloc(sz))
47
48 /* this is called to do local transformations on
49 * an expression tree preparitory to its being
50 * written out in intermediate code.
51 */
52 NODE *
53 clocal(NODE *p)
54 {
55 struct symtab *q;
56 NODE *r, *l;
57 int o;
58 int m;
59 TWORD ty;
60 int tmpnr, isptrvoid = 0;
61
62 #ifdef PCC_DEBUG
63 if (xdebug) {
64 printf("clocal in: %p\n", p);
65 fwalk(p, eprint, 0);
66 }
67 #endif
68
69 switch (o = p->n_op) {
70
71 case UCALL:
72 case CALL:
73 case STCALL:
74 case USTCALL:
75 if (p->n_type == VOID)
76 break;
77 /*
78 * if the function returns void*, ecode() invokes
79 * delvoid() to convert it to uchar*.
80 * We just let this happen on the ASSIGN to the temp,
81 * and cast the pointer back to void* on access
82 * from the temp.
83 */
84 if (p->n_type == PTR+VOID)
85 isptrvoid = 1;
86 r = tempnode(0, p->n_type, p->n_df, p->n_ap);
87 tmpnr = regno(r);
88 r = block(ASSIGN, r, p, p->n_type, p->n_df, p->n_ap);
89
90 p = tempnode(tmpnr, r->n_type, r->n_df, r->n_ap);
91 if (isptrvoid) {
92 p = block(PCONV, p, NIL, PTR+VOID, p->n_df, 0);
93 }
94 p = buildtree(COMOP, r, p);
95 break;
96
97 case NAME:
98 if ((q = p->n_sp) == NULL)
99 return p; /* Nothing to care about */
100
101 switch (q->sclass) {
102
103 case PARAM:
104 case AUTO:
105 /* fake up a structure reference */
106 r = block(REG, NIL, NIL, PTR+STRTY, 0, 0);
107 slval(r, 0);
108 r->n_rval = FP;
109 p = stref(block(STREF, r, p, 0, 0, 0));
110 break;
111
112 case STATIC:
113 if (q->slevel == 0)
114 break;
115 slval(p, 0);
116 p->n_sp = q;
117 break;
118
119 case REGISTER:
120 p->n_op = REG;
121 slval(p, 0);
122 p->n_rval = q->soffset;
123 break;
124
125 }
126 break;
127
128 case FUNARG:
129 /* Args smaller than int are given as int */
130 if (p->n_type != CHAR && p->n_type != UCHAR &&
131 p->n_type != SHORT && p->n_type != USHORT)
132 break;
133 p->n_left = block(SCONV, p->n_left, NIL, INT, 0, 0);
134 p->n_type = INT;
135 p->n_ap = 0;
136 p->n_rval = SZINT;
137 break;
138
139 case CBRANCH:
140 l = p->n_left;
141
142 /*
143 * Remove unnecessary conversion ops.
144 */
145 if (clogop(l->n_op) && l->n_left->n_op == SCONV) {
146 if (coptype(l->n_op) != BITYPE)
147 break;
148 if (l->n_right->n_op == ICON) {
149 r = l->n_left->n_left;
150 if (r->n_type >= FLOAT && r->n_type <= LDOUBLE)
151 break;
152 /* Type must be correct */
153 ty = r->n_type;
154 nfree(l->n_left);
155 l->n_left = r;
156 l->n_type = ty;
157 l->n_right->n_type = ty;
158 }
159 #if 0
160 else if (l->n_right->n_op == SCONV &&
161 l->n_left->n_type == l->n_right->n_type) {
162 r = l->n_left->n_left;
163 nfree(l->n_left);
164 l->n_left = r;
165 r = l->n_right->n_left;
166 nfree(l->n_right);
167 l->n_right = r;
168 }
169 #endif
170 }
171 break;
172
173 case PCONV:
174 /* Remove redundant PCONV's. Be careful */
175 l = p->n_left;
176 if (l->n_op == ICON) {
177 slval(l, (unsigned)glval(l));
178 goto delp;
179 }
180 if (l->n_type < INT || DEUNSIGN(l->n_type) == LONG ||
181 DEUNSIGN(l->n_type) == LONGLONG) {
182 /* float etc? */
183 p->n_left = block(SCONV, l, NIL, UNSIGNED, 0, 0);
184 break;
185 }
186 /* if left is SCONV, cannot remove */
187 if (l->n_op == SCONV)
188 break;
189
190 /* avoid ADDROF TEMP */
191 if (l->n_op == ADDROF && l->n_left->n_op == TEMP)
192 break;
193
194 /* if conversion to another pointer type, just remove */
195 if (p->n_type > BTMASK && l->n_type > BTMASK)
196 goto delp;
197 break;
198
199 delp: l->n_type = p->n_type;
200 l->n_qual = p->n_qual;
201 l->n_df = p->n_df;
202 l->n_ap = p->n_ap;
203 nfree(p);
204 p = l;
205 break;
206
207 case SCONV:
208 l = p->n_left;
209
210 if (p->n_type == l->n_type) {
211 nfree(p);
212 p = l;
213 break;
214 }
215
216 if ((p->n_type & TMASK) == 0 && (l->n_type & TMASK) == 0 &&
217 tsize(p->n_type, p->n_df, p->n_ap) ==
218 tsize(l->n_type, l->n_df, l->n_ap)) {
219 if (p->n_type != FLOAT && p->n_type != DOUBLE &&
220 l->n_type != FLOAT && l->n_type != DOUBLE &&
221 l->n_type != LDOUBLE && p->n_type != LDOUBLE) {
222 if (l->n_op == NAME || l->n_op == UMUL ||
223 l->n_op == TEMP) {
224 l->n_type = p->n_type;
225 nfree(p);
226 p = l;
227 break;
228 }
229 }
230 }
231
232 if (DEUNSIGN(p->n_type) == INT && DEUNSIGN(l->n_type) == INT &&
233 coptype(l->n_op) == BITYPE) {
234 l->n_type = p->n_type;
235 nfree(p);
236 p = l;
237 }
238
239 if (DEUNSIGN(p->n_type) == SHORT &&
240 DEUNSIGN(l->n_type) == SHORT) {
241 nfree(p);
242 p = l;
243 }
244
245 /* convert float/double to int before to (u)char/(u)short */
246 if ((DEUNSIGN(p->n_type) == CHAR ||
247 DEUNSIGN(p->n_type) == SHORT) &&
248 (l->n_type == FLOAT || l->n_type == DOUBLE ||
249 l->n_type == LDOUBLE)) {
250 p = block(SCONV, p, NIL, p->n_type, p->n_df, p->n_ap);
251 p->n_left->n_type = INT;
252 break;
253 }
254
255 /* convert (u)char/(u)short to int before float/double */
256 if ((p->n_type == FLOAT || p->n_type == DOUBLE ||
257 p->n_type == LDOUBLE) && (DEUNSIGN(l->n_type) == CHAR ||
258 DEUNSIGN(l->n_type) == SHORT)) {
259 p = block(SCONV, p, NIL, p->n_type, p->n_df, p->n_ap);
260 p->n_left->n_type = INT;
261 break;
262 }
263 break;
264
265 case MOD:
266 case DIV:
267 if (o == DIV && p->n_type != CHAR && p->n_type != SHORT)
268 break;
269 if (o == MOD && p->n_type != CHAR && p->n_type != SHORT)
270 break;
271 /* make it an int division by inserting conversions */
272 p->n_left = block(SCONV, p->n_left, NIL, INT, 0, 0);
273 p->n_right = block(SCONV, p->n_right, NIL, INT, 0, 0);
274 p = block(SCONV, p, NIL, p->n_type, 0, 0);
275 p->n_left->n_type = INT;
276 break;
277
278 case FORCE:
279 /* put return value in return reg */
280 p->n_op = ASSIGN;
281 p->n_right = p->n_left;
282 p->n_left = block(REG, NIL, NIL, p->n_type, 0, 0);
283 p->n_left->n_rval = RETREG(p->n_type);
284 break;
285 }
286
287 #ifdef PCC_DEBUG
288 if (xdebug) {
289 printf("clocal out: %p\n", p);
290 fwalk(p, eprint, 0);
291 }
292 #endif
293
294 return(p);
295 }
296
297 void
298 myp2tree(NODE *p)
299 {
300 struct symtab *sp;
301
302 if (p->n_op != FCON)
303 return;
304
305 /* Write float constants to memory */
306
307 sp = IALLOC(sizeof(struct symtab));
308 sp->sclass = STATIC;
309 sp->sap = 0;
310 sp->slevel = 1; /* fake numeric label */
311 sp->soffset = getlab();
312 sp->sflags = 0;
313 sp->stype = p->n_type;
314 sp->squal = (CON >> TSHIFT);
315
316 defloc(sp);
317 ninval(0, tsize(sp->stype, sp->sdf, sp->sap), p);
318
319 p->n_op = NAME;
320 slval(p, 0);
321 p->n_sp = sp;
322
323 }
324
325 /*ARGSUSED*/
326 int
327 andable(NODE *p)
328 {
329 return(1); /* all names can have & taken on them */
330 }
331
332 /*
333 * is an automatic variable of type t OK for a register variable
334 */
335 int
336 cisreg(TWORD t)
337 {
338 if (t == INT || t == UNSIGNED)
339 return(1);
340 return 0; /* XXX - fix reg assignment in pftn.c */
341 }
342
343 /*
344 * Allocate off bits on the stack. p is a tree that when evaluated
345 * is the multiply count for off, t is a NAME node where to write
346 * the allocated address.
347 */
348 void
349 spalloc(NODE *t, NODE *p, OFFSZ off)
350 {
351 NODE *sp;
352 int nbytes = off / SZCHAR;
353
354 p = buildtree(MUL, p, bcon(nbytes));
355 p = buildtree(PLUS, p, bcon(7));
356 p = buildtree(AND, p, bcon(~7));
357
358 /* subtract the size from sp */
359 sp = block(REG, NIL, NIL, p->n_type, 0, 0);
360 slval(sp, 0);
361 sp->n_rval = SP;
362 ecomp(buildtree(MINUSEQ, sp, p));
363
364 /* save the address of sp */
365 sp = block(REG, NIL, NIL, PTR+INT, t->n_df, t->n_ap);
366 sp->n_rval = SP;
367 t->n_type = sp->n_type;
368 ecomp(buildtree(ASSIGN, t, sp)); /* Emit! */
369 }
370
371 /*
372 * print out a constant node
373 * mat be associated with a label
374 */
375 int
376 ninval(CONSZ off, int fsz, NODE *p)
377 {
378 struct symtab *q;
379 TWORD t;
380 #ifndef USE_GAS
381 int i, j;
382 #endif
383
384 t = p->n_type;
385 if (t > BTMASK)
386 p->n_type = t = LONG; /* pointer */
387
388 if (p->n_op == ICON && p->n_sp != NULL && DEUNSIGN(t) != LONG)
389 uerror("element not constant");
390
391 switch (t) {
392 case LONGLONG:
393 case ULONGLONG:
394 #ifdef USE_GAS
395 printf("\t.dword %lld\n", (long long)glval(p));
396 #else
397 i = glval(p) >> 32;
398 j = glval(p) & 0xffffffff;
399 p->n_type = INT;
400 if (bigendian) {
401 slval(p, j);
402 ninval(off, 32, p);
403 slval(p, i);
404 ninval(off+32, 32, p);
405 } else {
406 slval(p, i);
407 ninval(off, 32, p);
408 slval(p, j);
409 ninval(off+32, 32, p);
410 }
411 #endif
412 break;
413 case INT:
414 case UNSIGNED:
415 printf("\t.word " CONFMT, (CONSZ)glval(p));
416 if ((q = p->n_sp) != NULL) {
417 if ((q->sclass == STATIC && q->slevel > 0)) {
418 printf("+" LABFMT, q->soffset);
419 } else
420 printf("+%s", getexname(q));
421 }
422 printf("\n");
423 break;
424 case SHORT:
425 case USHORT:
426 astypnames[SHORT] = astypnames[USHORT] = "\t.half";
427 return 0;
428 default:
429 return 0;
430 }
431 return 1;
432 }
433
434 /* make a name look like an external name in the local machine */
435 char *
436 exname(char *p)
437 {
438 if (p == NULL)
439 return "";
440 return p;
441 }
442
443 /*
444 * map types which are not defined on the local machine
445 */
446 TWORD
447 ctype(TWORD type)
448 {
449 switch (BTYPE(type)) {
450 case LONG:
451 MODTYPE(type,LONGLONG);
452 break;
453
454 case ULONG:
455 MODTYPE(type,ULONGLONG);
456
457 }
458 return (type);
459 }
460
461 void
462 calldec(NODE *p, NODE *q)
463 {
464 }
465
466 void
467 extdec(struct symtab *q)
468 {
469 }
470
471 /* make a common declaration for id, if reasonable */
472 void
473 defzero(struct symtab *sp)
474 {
475 int off;
476
477 off = tsize(sp->stype, sp->sdf, sp->sap);
478 off = (off+(SZCHAR-1))/SZCHAR;
479 printf(" .%scomm ", sp->sclass == STATIC ? "l" : "");
480 if (sp->slevel == 0)
481 printf("%s,0%o\n", getexname(sp), off);
482 else
483 printf(LABFMT ",0%o\n", sp->soffset, off);
484 }
485
486
487 #ifdef notdef
488 /* make a common declaration for id, if reasonable */
489 void
490 commdec(struct symtab *q)
491 {
492 int off;
493
494 off = tsize(q->stype, q->sdf, q->ssue);
495 off = (off+(SZCHAR-1))/SZCHAR;
496
497 printf(" .comm %s,%d\n", exname(q->soname), off);
498 }
499
500 /* make a local common declaration for id, if reasonable */
501 void
502 lcommdec(struct symtab *q)
503 {
504 int off;
505
506 off = tsize(q->stype, q->sdf, q->ssue);
507 off = (off+(SZCHAR-1))/SZCHAR;
508 if (q->slevel == 0)
509 printf("\t.lcomm %s,%d\n", exname(q->soname), off);
510 else
511 printf("\t.lcomm " LABFMT ",%d\n", q->soffset, off);
512 }
513
514 /*
515 * print a (non-prog) label.
516 */
517 void
518 deflab1(int label)
519 {
520 printf(LABFMT ":\n", label);
521 }
522
523 /* ro-text, rw-data, ro-data, ro-strings */
524 static char *loctbl[] = { "text", "data", "rdata", "rdata" };
525
526 void
527 setloc1(int locc)
528 {
529 if (locc == lastloc && locc != STRNG)
530 return;
531 if (locc == RDATA && lastloc == STRNG)
532 return;
533
534 if (locc != lastloc) {
535 lastloc = locc;
536 printf("\t.%s\n", loctbl[locc]);
537 }
538
539 if (locc == STRNG)
540 printf("\t.align 2\n");
541 }
542 #endif
543
544 /*
545 * va_start(ap, last) implementation.
546 *
547 * f is the NAME node for this builtin function.
548 * a is the argument list containing:
549 * CM
550 * ap last
551 *
552 * It turns out that this is easy on MIPS. Just write the
553 * argument registers to the stack in va_arg_start() and
554 * use the traditional method of walking the stackframe.
555 */
556 NODE *
557 mips64_builtin_stdarg_start(const struct bitable *bt, NODE *a)
558 {
559 NODE *p, *q;
560 int sz = 1;
561
562 /* check num args and type */
563 if (a == NULL || a->n_op != CM || a->n_left->n_op == CM ||
564 !ISPTR(a->n_left->n_type))
565 goto bad;
566
567 /* must first deal with argument size; use int size */
568 p = a->n_right;
569 if (p->n_type < INT) {
570 /* round up to word */
571 sz = SZINT / tsize(p->n_type, p->n_df, p->n_ap);
572 }
573
574 p = buildtree(ADDROF, p, NIL); /* address of last arg */
575 p = optim(buildtree(PLUS, p, bcon(sz)));
576 q = block(NAME, NIL, NIL, PTR+VOID, 0, 0);
577 q = buildtree(CAST, q, p);
578 p = q->n_right;
579 nfree(q->n_left);
580 nfree(q);
581 p = buildtree(ASSIGN, a->n_left, p);
582 nfree(a);
583
584 return p;
585
586 bad:
587 uerror("bad argument to __builtin_stdarg_start");
588 return bcon(0);
589 }
590
591 NODE *
592 mips64_builtin_va_arg(const struct bitable *bt, NODE *a)
593 {
594 NODE *p, *q, *r;
595 int sz, tmpnr;
596
597 /* check num args and type */
598 if (a == NULL || a->n_op != CM || a->n_left->n_op == CM ||
599 !ISPTR(a->n_left->n_type) || a->n_right->n_op != TYPE)
600 goto bad;
601
602 r = a->n_right;
603
604 /* get type size */
605 sz = tsize(r->n_type, r->n_df, r->n_ap) / SZCHAR;
606 if (sz < SZINT/SZCHAR) {
607 werror("%s%s promoted to int when passed through ...",
608 r->n_type & 1 ? "unsigned " : "",
609 DEUNSIGN(r->n_type) == SHORT ? "short" : "char");
610 sz = SZINT/SZCHAR;
611 }
612
613 /* alignment */
614 p = tcopy(a->n_left);
615 if (sz > SZINT/SZCHAR && r->n_type != UNIONTY && r->n_type != STRTY) {
616 p = buildtree(PLUS, p, bcon(7));
617 p = block(AND, p, bcon(-8), p->n_type, p->n_df, p->n_ap);
618 }
619
620 /* create a copy to a temp node */
621 q = tempnode(0, p->n_type, p->n_df, p->n_ap);
622 tmpnr = regno(q);
623 p = buildtree(ASSIGN, q, p);
624
625 q = tempnode(tmpnr, p->n_type, p->n_df,p->n_ap);
626 q = buildtree(PLUS, q, bcon(sz));
627 q = buildtree(ASSIGN, a->n_left, q);
628
629 q = buildtree(COMOP, p, q);
630
631 nfree(a->n_right);
632 nfree(a);
633
634 p = tempnode(tmpnr, INCREF(r->n_type), r->n_df, r->n_ap);
635 p = buildtree(UMUL, p, NIL);
636 p = buildtree(COMOP, q, p);
637
638 return p;
639
640 bad:
641 uerror("bad argument to __builtin_va_arg");
642 return bcon(0);
643 }
644
645 NODE *
646 mips64_builtin_va_end(const struct bitable *bt, NODE *a)
647 {
648 tfree(a);
649 return bcon(0);
650 }
651
652 NODE *
653 mips64_builtin_va_copy(const struct bitable *bt, NODE *a)
654 {
655 NODE *f;
656
657 if (a == NULL || a->n_op != CM || a->n_left->n_op == CM)
658 goto bad;
659 f = buildtree(ASSIGN, a->n_left, a->n_right);
660 nfree(a);
661 return f;
662
663 bad:
664 uerror("bad argument to __builtin_va_copy");
665 return bcon(0);
666 }
667
668 static int constructor;
669 static int destructor;
670
671 /*
672 * Give target the opportunity of handling pragmas.
673 */
674 int
675 mypragma(char *str)
676 {
677
678 if (strcmp(str, "tls") == 0) {
679 uerror("thread-local storage not supported for this target");
680 return 1;
681 }
682 if (strcmp(str, "constructor") == 0 || strcmp(str, "init") == 0) {
683 constructor = 1;
684 return 1;
685 }
686 if (strcmp(str, "destructor") == 0 || strcmp(str, "fini") == 0) {
687 destructor = 1;
688 return 1;
689 }
690
691 return 0;
692 }
693
694 /*
695 * Called when a identifier has been declared, to give target last word.
696 */
697 void
698 fixdef(struct symtab *sp)
699 {
700 if ((constructor || destructor) && (sp->sclass != PARAM)) {
701 printf("\t.section .%ctors,\"aw\",@progbits\n",
702 constructor ? 'c' : 'd');
703 printf("\t.p2align 2\n");
704 printf("\t.long %s\n", exname(sp->sname));
705 printf("\t.previous\n");
706 constructor = destructor = 0;
707 }
708 }
709
710 void
711 pass1_lastchance(struct interpass *ip)
712 {
713 }