Debian Codegraph @

arch/mips/local.c

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