Debian Codegraph @

arch/powerpc/local.c

raw symbols SCIP
1 /* $Id: local.c,v 1.37 2020/10/30 16:47:53 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 #include <assert.h>
28 #include "pass1.h"
29
30 #undef NIL
31 #define NIL NULL
32
33 #ifdef LANG_CXX
34 #define P1ND NODE
35 #define p1nfree nfree
36 #define p1tfree tfree
37 #define p1fwalk fwalk
38 #define p1walkf walkf
39 #define p1tcopy tcopy
40 #else
41 #define NODE P1ND
42 #define nfree p1nfree
43 #endif
44
45 #define IALLOC(sz) (isinlining ? permalloc(sz) : tmpalloc(sz))
46
47 extern int kflag;
48
49 static void simmod(NODE *p);
50
51 /* this file contains code which is dependent on the target machine */
52
53 #if defined(MACHOABI)
54
55 /*
56 * Keep track of PIC stubs.
57 */
58
59 void
60 addstub(struct stub *list, char *name)
61 {
62 struct stub *s;
63
64 DLIST_FOREACH(s, list, link) {
65 if (strcmp(s->name, name) == 0)
66 return;
67 }
68
69 s = permalloc(sizeof(struct stub));
70 s->name = newstring(name, strlen(name));
71 DLIST_INSERT_BEFORE(list, s, link);
72 }
73
74 #endif
75
76
77 /*
78 * Make a symtab entry for PIC use.
79 */
80 static struct symtab *
81 picsymtab(char *p, char *s, char *s2)
82 {
83 struct symtab *sp = IALLOC(sizeof(struct symtab));
84 size_t len = strlen(p) + strlen(s) + strlen(s2) + 1;
85
86 sp->sname = IALLOC(len);
87 strlcpy(sp->sname, p, len);
88 strlcat(sp->sname, s, len);
89 strlcat(sp->sname, s2, len);
90 sp->sclass = EXTERN;
91 sp->sflags = sp->slevel = 0;
92
93 return sp;
94 }
95
96 int gotnr; /* tempnum for GOT register */
97
98 /*
99 * Create a reference for an extern variable.
100 */
101 static NODE *
102 picext(NODE *p)
103 {
104 NODE *q;
105 struct symtab *sp;
106 char *name;
107
108 name = getexname(p->n_sp);
109
110 if (strncmp(name, "__builtin", 9) == 0)
111 return p;
112
113 #if defined(ELFABI)
114
115 sp = picsymtab("", name, "@got(31)");
116 q = xbcon(0, sp, PTR+VOID);
117 q = block(UMUL, q, 0, PTR+VOID, 0, 0);
118
119 #elif defined(MACHOABI)
120
121 char buf2[64];
122 NODE *r;
123 char *fname;
124
125 fname = cftnsp->soname ? cftnsp->soname : cftnsp->sname;
126
127 if (p->n_sp->sclass == EXTDEF) {
128 snprintf(buf2, 64, "-L%s$pb", fname);
129 sp = picsymtab("", name, buf2);
130 } else {
131 snprintf(buf2, 64, "$non_lazy_ptr-L%s$pb", fname);
132 sp = picsymtab("L", name, buf2);
133 addstub(&nlplist, name);
134 }
135 #if USE_GOTNR
136 q = tempnode(gotnr, PTR+VOID, 0, 0);
137 #else
138 q = block(REG, NIL, NIL, PTR+VOID, 0, 0);
139 regno(q) = GOTREG;
140 #endif
141 r = xbcon(0, sp, INT);
142 q = buildtree(PLUS, q, r);
143
144 if (p->n_sp->sclass != EXTDEF)
145 q = block(UMUL, q, 0, PTR+VOID, 0, 0);
146
147 #endif
148
149 q = block(UMUL, q, 0, p->n_type, p->n_df, p->n_ap);
150 q->n_sp = p->n_sp; /* for init */
151 p1nfree(p);
152
153 return q;
154 }
155
156 /*
157 * Create a reference for a static variable
158 */
159
160 static NODE *
161 picstatic(NODE *p)
162 {
163 NODE *q;
164 struct symtab *sp;
165
166 #if defined(ELFABI)
167 char *n;
168
169 if (p->n_sp->slevel > 0) {
170 char buf[64];
171 snprintf(buf, 64, LABFMT, (int)p->n_sp->soffset);
172 sp = picsymtab("", buf, "@got(31)");
173 } else {
174 n = getexname(p->n_sp);
175 sp = picsymtab("", n, "@got(31)");
176 }
177 sp->sclass = STATIC;
178 sp->stype = p->n_sp->stype;
179 q = xbcon(0, sp, PTR+VOID);
180 q = block(UMUL, q, 0, p->n_type, p->n_df, p->n_ap);
181 q->n_sp = p->n_sp;
182 p1nfree(p);
183
184 #elif defined(MACHOABI)
185
186 char buf2[64];
187 NODE *r;
188
189 snprintf(buf2, 64, "-L%s$pb",
190 cftnsp->soname ? cftnsp->soname : cftnsp->sname);
191
192 if (p->n_sp->slevel > 0) {
193 char buf1[64];
194 snprintf(buf1, 64, LABFMT, (int)p->n_sp->soffset);
195 sp = picsymtab("", buf1, buf2);
196 } else {
197 char *name = p->n_sp->soname ? p->n_sp->soname : exname(p->n_sp->sname);
198 sp = picsymtab("", name, buf2);
199 }
200 sp->sclass = STATIC;
201 sp->stype = p->n_sp->stype;
202 #if USE_GOTNR
203 q = tempnode(gotnr, PTR+VOID, 0, 0);
204 #else
205 q = block(REG, NIL, NIL, PTR+VOID, 0, 0);
206 regno(q) = GOTREG;
207 #endif
208 r = xbcon(0, sp, INT);
209 q = buildtree(PLUS, q, r);
210 q = block(UMUL, q, 0, p->n_type, p->n_df, p->n_ap);
211 q->n_sp = p->n_sp;
212 nfree(p);
213
214 #endif
215
216 return q;
217 }
218
219 #if 0
220 static NODE *
221 convert_ulltof(NODE *p)
222 {
223 NODE *q, *r, *l, *t;
224 int ty;
225 int tmpnr;
226
227 ty = p->n_type;
228 l = p->n_left;
229 nfree(p);
230
231 q = tempnode(0, ULONGLONG, 0, 0);
232 tmpnr = regno(q);
233 t = buildtree(ASSIGN, q, l);
234 ecomp(t);
235
236 #if 0
237 q = tempnode(tmpnr, ULONGLONG, 0, 0);
238 q = block(SCONV, q, NIL, LONGLONG, 0, 0);
239 #endif
240 q = tempnode(tmpnr, LONGLONG, 0, 0);
241 r = block(SCONV, q, NIL, ty, 0, 0);
242
243 q = tempnode(tmpnr, ULONGLONG, 0, 0);
244 q = block(RS, q, bcon(1), ULONGLONG, 0, 0);
245 q = block(SCONV, q, NIL, LONGLONG, 0, 0);
246 q = block(SCONV, q, NIL, ty, 0, 0);
247 t = block(FCON, NIL, NIL, ty, 0, 0);
248 #ifndef LANG_CXX
249 t->n_dcon = fltallo();
250 FLOAT_INT2FP(p->n_dcon, 2, INT);
251 #else
252 cerror("convert_ulltof");
253 #endif
254 l = block(MUL, q, t, ty, 0, 0);
255
256 r = buildtree(COLON, l, r);
257
258 q = tempnode(tmpnr, ULONGLONG, 0, 0);
259 q = block(SCONV, q, NIL, LONGLONG, 0, 0);
260 l = block(LE, q, xbcon(0, NULL, LONGLONG), INT, 0, 0);
261
262 return clocal(buildtree(QUEST, l, r));
263
264 }
265 #endif
266
267
268 /* clocal() is called to do local transformations on
269 * an expression tree preparitory to its being
270 * written out in intermediate code.
271 *
272 * the major essential job is rewriting the
273 * automatic variables and arguments in terms of
274 * REG and OREG nodes
275 * conversion ops which are not necessary are also clobbered here
276 * in addition, any special features (such as rewriting
277 * exclusive or) are easily handled here as well
278 */
279 NODE *
280 clocal(NODE *p)
281 {
282
283 struct symtab *q;
284 NODE *r, *l;
285 int o;
286 int m;
287 TWORD t;
288 int isptrvoid = 0;
289 int tmpnr;
290
291 #ifdef PCC_DEBUG
292 if (xdebug) {
293 printf("clocal: %p\n", p);
294 p1fwalk(p, eprint, 0);
295 }
296 #endif
297 switch (o = p->n_op) {
298
299 case ADDROF:
300 #ifdef PCC_DEBUG
301 if (xdebug) {
302 printf("clocal(): ADDROF\n");
303 printf("type: 0x%x\n", p->n_type);
304 }
305 #endif
306 /* XXX cannot takes addresses of PARAMs */
307
308 if (kflag == 0 || blevel == 0)
309 break;
310 /* char arrays may end up here */
311 l = p->n_left;
312 if (l->n_op != NAME ||
313 (l->n_type != ARY+CHAR && l->n_type != ARY+WCHAR_TYPE))
314 break;
315 l = p;
316 p = picstatic(p->n_left);
317 nfree(l);
318 if (p->n_op != UMUL)
319 cerror("ADDROF error");
320 l = p;
321 p = p->n_left;
322 nfree(l);
323 break;
324
325 case NAME:
326 if ((q = p->n_sp) == NULL)
327 return p; /* Nothing to care about */
328
329 switch (q->sclass) {
330
331 case PARAM:
332 case AUTO:
333 /* fake up a structure reference */
334 r = block(REG, NIL, NIL, PTR+STRTY, 0, 0);
335 slval(r, 0);
336 r->n_rval = FPREG;
337 p = stref(block(STREF, r, p, 0, 0, 0));
338 break;
339
340 case USTATIC:
341 if (kflag == 0)
342 break;
343 /* FALLTHROUGH */
344
345 case STATIC:
346 if (kflag == 0) {
347 if (q->slevel == 0)
348 break;
349 slval(p, 0);
350 } else if (blevel > 0) {
351 p = picstatic(p);
352 }
353 break;
354
355 case REGISTER:
356 p->n_op = REG;
357 slval(p, 0);
358 p->n_rval = q->soffset;
359 break;
360
361 case EXTERN:
362 case EXTDEF:
363 if (kflag == 0)
364 break;
365 if (blevel > 0)
366 p = picext(p);
367 break;
368 }
369 break;
370
371 case UCALL:
372 case CALL:
373 case USTCALL:
374 case STCALL:
375 if (p->n_type == VOID)
376 break;
377 /*
378 * if the function returns void*, ecode() invokes
379 * delvoid() to convert it to uchar*.
380 * We just let this happen on the ASSIGN to the temp,
381 * and cast the pointer back to void* on access
382 * from the temp.
383 */
384 if (p->n_type == PTR+VOID)
385 isptrvoid = 1;
386 r = tempnode(0, p->n_type, p->n_df, p->n_ap);
387 tmpnr = regno(r);
388 r = buildtree(ASSIGN, r, p);
389
390 p = tempnode(tmpnr, r->n_type, r->n_df, r->n_ap);
391 if (isptrvoid) {
392 p = block(PCONV, p, NIL, PTR+VOID,
393 p->n_df, 0);
394 }
395 #if 1
396 p = buildtree(COMOP, r, p);
397 #else
398 /* XXX this doesn't work if the call is already in a COMOP */
399 r = clocal(r);
400 ecomp(r);
401 #endif
402 break;
403
404 case CBRANCH:
405 l = p->n_left;
406
407 /*
408 * Remove unnecessary conversion ops.
409 */
410 if (clogop(l->n_op) && l->n_left->n_op == SCONV) {
411 if (coptype(l->n_op) != BITYPE)
412 break;
413 if (l->n_right->n_op == ICON) {
414 r = l->n_left->n_left;
415 if (r->n_type >= FLOAT && r->n_type <= LDOUBLE)
416 break;
417 /* Type must be correct */
418 t = r->n_type;
419 nfree(l->n_left);
420 l->n_left = r;
421 l->n_type = t;
422 l->n_right->n_type = t;
423 }
424 }
425 break;
426
427 case PCONV:
428 /* Remove redundant PCONV's. Be careful */
429 l = p->n_left;
430 if (l->n_op == ICON) {
431 slval(l, (unsigned)glval(l));
432 goto delp;
433 }
434 if (l->n_type < INT || DEUNSIGN(l->n_type) == LONGLONG) {
435 /* float etc? */
436 p->n_left = block(SCONV, l, NIL,
437 UNSIGNED, 0, 0);
438 break;
439 }
440 /* if left is SCONV, cannot remove */
441 if (l->n_op == SCONV)
442 break;
443
444 /* avoid ADDROF TEMP */
445 if (l->n_op == ADDROF && l->n_left->n_op == TEMP)
446 break;
447
448 /* if conversion to another pointer type, just remove */
449 if (p->n_type > BTMASK && l->n_type > BTMASK)
450 goto delp;
451 break;
452
453 delp: l->n_type = p->n_type;
454 l->n_qual = p->n_qual;
455 l->n_df = p->n_df;
456 l->n_ap = p->n_ap;
457 nfree(p);
458 p = l;
459 break;
460
461 case SCONV:
462 l = p->n_left;
463
464 if (p->n_type == l->n_type) {
465 nfree(p);
466 return l;
467 }
468
469 if ((p->n_type & TMASK) == 0 && (l->n_type & TMASK) == 0 &&
470 tsize(p->n_type, p->n_df, p->n_ap) ==
471 tsize(l->n_type, l->n_df, l->n_ap)) {
472 if (p->n_type != FLOAT && p->n_type != DOUBLE &&
473 l->n_type != FLOAT && l->n_type != DOUBLE &&
474 l->n_type != LDOUBLE && p->n_type != LDOUBLE) {
475 if (l->n_op == NAME || l->n_op == UMUL ||
476 l->n_op == TEMP) {
477 l->n_type = p->n_type;
478 nfree(p);
479 return l;
480 }
481 }
482 }
483
484 if (DEUNSIGN(p->n_type) == INT && DEUNSIGN(l->n_type) == INT &&
485 coptype(l->n_op) == BITYPE) {
486 l->n_type = p->n_type;
487 nfree(p);
488 return l;
489 }
490
491 #if 0
492 /*
493 * if converting ULONGLONG to FLOAT/(L)DOUBLE,
494 * replace ___floatunsdidf() with ___floatdidf()
495 */
496 if (l->n_type == ULONGLONG && p->n_type >= FLOAT &&
497 p->n_type <= LDOUBLE) {
498 return convert_ulltof(p);
499 }
500 #endif
501
502 o = l->n_op;
503 m = p->n_type;
504
505 if (DEUNSIGN(p->n_type) == SHORT &&
506 DEUNSIGN(l->n_type) == SHORT) {
507 nfree(p);
508 p = l;
509 }
510 if ((DEUNSIGN(p->n_type) == CHAR ||
511 DEUNSIGN(p->n_type) == SHORT) &&
512 (l->n_type == FLOAT || l->n_type == DOUBLE ||
513 l->n_type == LDOUBLE)) {
514 p = block(SCONV, p, NIL, p->n_type, p->n_df, p->n_ap);
515 p->n_left->n_type = INT;
516 return p;
517 }
518 if ((DEUNSIGN(l->n_type) == CHAR ||
519 DEUNSIGN(l->n_type) == SHORT) &&
520 (p->n_type == FLOAT || p->n_type == DOUBLE ||
521 p->n_type == LDOUBLE)) {
522 p = block(SCONV, p, NIL, p->n_type, p->n_df, p->n_ap);
523 p->n_left->n_type = INT;
524 return p;
525 }
526 break;
527
528 case MOD:
529 simmod(p);
530 break;
531
532 case DIV:
533 if (o == DIV && p->n_type != CHAR && p->n_type != SHORT)
534 break;
535 /* make it an int division by inserting conversions */
536 p->n_left = block(SCONV, p->n_left, NIL, INT, 0, 0);
537 p->n_right = block(SCONV, p->n_right, NIL, INT, 0, 0);
538 p = block(SCONV, p, NIL, p->n_type, 0, 0);
539 p->n_left->n_type = INT;
540 break;
541
542 case STNAME:
543 if ((q = p->n_sp) == NULL)
544 return p;
545 if (q->sclass != STNAME)
546 return p;
547 t = p->n_type;
548 p = block(ADDROF, p, NIL, INCREF(t), p->n_df, p->n_ap);
549 p = block(UMUL, p, NIL, t, p->n_df, p->n_ap);
550 break;
551
552 case FORCE:
553 /* put return value in return reg */
554 p->n_op = ASSIGN;
555 p->n_right = p->n_left;
556 p->n_left = block(REG, NIL, NIL, p->n_type, 0, 0);
557 p->n_left->n_rval = p->n_left->n_type == BOOL ?
558 RETREG(BOOL_TYPE) : RETREG(p->n_type);
559 break;
560
561 case LS:
562 case RS:
563 if (p->n_right->n_op == ICON)
564 break; /* do not do anything */
565 if (DEUNSIGN(p->n_right->n_type) == INT)
566 break;
567 p->n_right = block(SCONV, p->n_right, NIL, INT, 0, 0);
568 break;
569 }
570
571 #ifdef PCC_DEBUG
572 if (xdebug) {
573 printf("clocal end: %p\n", p);
574 p1fwalk(p, eprint, 0);
575 }
576 #endif
577 return(p);
578 }
579
580 /*
581 * Change CALL references to either direct (static) or PLT.
582 */
583 static void
584 fixnames(NODE *p, void *arg)
585 {
586 struct symtab *sp;
587 struct attr *ap, *ap2;
588 NODE *q;
589 char *c;
590 int isu;
591
592 if ((cdope(p->n_op) & CALLFLG) == 0)
593 return;
594
595 isu = 0;
596 q = p->n_left;
597 ap = q->n_ap;
598 if (q->n_op == UMUL)
599 q = q->n_left, isu = 1;
600
601 #if defined(ELFABI)
602
603 if (q->n_op == ICON) {
604 sp = q->n_sp;
605
606 #elif defined(MACHOABI)
607
608 #ifdef USE_GOTNR
609 if (q->n_op == PLUS && q->n_left->n_op == TEMP &&
610 #else
611 if (q->n_op == PLUS && q->n_left->n_op == REG &&
612 #endif
613 q->n_right->n_op == ICON) {
614 sp = q->n_right->n_sp;
615 #endif
616
617 if (sp == NULL)
618 return; /* nothing to do */
619 if (sp->sclass == STATIC && !ISFTN(sp->stype))
620 return; /* function pointer */
621
622 if (sp->sclass != STATIC && sp->sclass != EXTERN &&
623 sp->sclass != EXTDEF)
624 cerror("fixnames");
625 c = NULL;
626 #if defined(ELFABI)
627
628 if ((ap2 = attr_find(sp->sap, ATTR_SONAME)) == NULL ||
629 (c = strstr(ap2->sarg(0), "@got(31)")) == NULL)
630 cerror("fixnames2");
631 if (isu) {
632 memcpy(c, "@plt", sizeof("@plt"));
633 } else
634 *c = 0;
635
636 #elif defined(MACHOABI)
637
638 if (sp->soname == NULL ||
639 ((c = strstr(sp->soname, "$non_lazy_ptr")) == NULL &&
640 (c = strstr(sp->soname, "-L")) == NULL))
641 cerror("fixnames2");
642 if (isu) {
643 addstub(&stublist, sp->soname+1);
644 memcpy(c, "$stub", sizeof("$stub"));
645 } else
646 *c = 0;
647
648 nfree(q->n_left);
649 q = q->n_right;
650 if (isu)
651 nfree(p->n_left->n_left);
652 nfree(p->n_left);
653 p->n_left = q;
654 q->n_ap = ap;
655
656 #endif
657 }
658 }
659
660 void
661 myp2tree(NODE *p)
662 {
663 int o = p->n_op;
664 struct symtab *sp;
665
666 if (kflag)
667 p1walkf(p, fixnames, 0);
668 if (o != FCON)
669 return;
670
671 /* Write float constants to memory */
672 /* Should be voluntary per architecture */
673
674 sp = IALLOC(sizeof(struct symtab));
675 sp->sclass = STATIC;
676 sp->sap = 0;
677 sp->slevel = 1; /* fake numeric label */
678 sp->soffset = getlab();
679 sp->sflags = 0;
680 sp->stype = p->n_type;
681 sp->squal = (CON >> TSHIFT);
682
683 defloc(sp);
684 ninval(0, tsize(sp->stype, sp->sdf, sp->sap), p);
685
686 p->n_op = NAME;
687 slval(p, 0);
688 p->n_sp = sp;
689 }
690
691 /*ARGSUSED*/
692 int
693 andable(NODE *p)
694 {
695 return(1); /* all names can have & taken on them */
696 }
697
698 /*
699 * Return 1 if a variable of type type is OK to put in register.
700 */
701 int
702 cisreg(TWORD t)
703 {
704 return 1;
705 }
706
707 /*
708 * Allocate bits on the stack.
709 * 'off' is the number of bits to allocate
710 * 'p' is a tree that when evaluated is the multiply count for 'off'
711 * 't' is a storeable node where to write the allocated address
712 */
713 void
714 spalloc(NODE *t, NODE *p, OFFSZ off)
715 {
716 NODE *q, *r;
717 int nbytes = off / SZCHAR;
718 int stacksize = 24+40; /* this should be p2stacksize */
719
720 /*
721 * After we subtract the requisite bytes
722 * off the stack, we need to step back over
723 * the 40 bytes for the arguments registers
724 * *and* any other parameters which will get
725 * saved to the stack. Unfortunately, we
726 * don't have that information in pass1 and
727 * the parameters will stomp on the allocated
728 * space for alloca().
729 *
730 * No fix yet.
731 */
732 werror("parameters may stomp on alloca()");
733
734 /* compute size */
735 p = buildtree(MUL, p, bcon(nbytes));
736 p = buildtree(PLUS, p, bcon(ALSTACK/SZCHAR));
737
738 /* load the top-of-stack */
739 q = block(REG, NIL, NIL, PTR+INT, 0, 0);
740 regno(q) = SPREG;
741 q = block(UMUL, q, NIL, INT, 0, 0);
742
743 /* save old top-of-stack value to new top-of-stack position */
744 r = block(REG, NIL, NIL, PTR+INT, 0, 0);
745 regno(r) = SPREG;
746 r = block(MINUSEQ, r, p, INT, 0, 0);
747 r = block(UMUL, r, NIL, INT, 0, 0);
748 ecomp(buildtree(ASSIGN, r, q));
749
750 r = block(REG, NIL, NIL, PTR+INT, 0, 0);
751 regno(r) = SPREG;
752
753 /* skip over the arguments space and align to 16 bytes */
754 r = block(PLUS, r, bcon(stacksize + 15), INT, 0, 0);
755 r = block(RS, r, bcon(4), INT, 0, 0);
756 r = block(LS, r, bcon(4), INT, 0, 0);
757
758 t->n_type = p->n_type;
759 ecomp(buildtree(ASSIGN, t, r));
760 }
761
762 /*
763 * Print out a string of characters.
764 * Unfortunately, this code assumes that the assembler understands
765 * C-style escape sequences. (which it doesn't!)
766 * Location is already set.
767 */
768 void
769 instring(struct symtab *sp)
770 {
771 char *s, *str = sp->sname;
772
773 #if defined(ELFABI)
774
775 defloc(sp);
776
777 #elif defined(MACHOABI)
778
779 extern int lastloc;
780 if (lastloc != STRNG)
781 printf(" .cstring\n");
782 lastloc = STRNG;
783 printf("\t.p2align 2\n");
784 printf(LABFMT ":\n", sp->soffset);
785
786 #endif
787
788 /* be kind to assemblers and avoid long strings */
789 printf("\t.ascii \"");
790 for (s = str; *s != 0; ) {
791 if (*s++ == '\\') {
792 (void)esccon(&s);
793 }
794 if (s - str > 64) {
795 fwrite(str, 1, s - str, stdout);
796 printf("\"\n\t.ascii \"");
797 str = s;
798 }
799 }
800 fwrite(str, 1, s - str, stdout);
801 printf("\\0\"\n");
802 }
803
804 /*
805 * print out a constant node, may be associated with a label.
806 * Do not free the node after use.
807 * off is bit offset from the beginning of the aggregate
808 * fsz is the number of bits this is referring to
809 */
810 int
811 ninval(CONSZ off, int fsz, NODE *p)
812 {
813 struct symtab *q;
814 TWORD t;
815 int i;
816
817 t = p->n_type;
818 if (t > BTMASK)
819 p->n_type = t = INT; /* pointer */
820
821 #if 0 /* not needed anymore */
822 if (kflag && (p->n_op == PLUS || p->n_op == UMUL)) {
823 char *c;
824 if (p->n_op == UMUL)
825 p = p->n_left;
826 p = p->n_right;
827 q = p->n_sp;
828
829 #if defined(ELFABI)
830
831 if (q->soname && (c = strstr(q->soname, "@got(31)")) != NULL)
832 *c = 0; /* ignore GOT ref here */
833
834 #elif defined(MACHOABI)
835
836 if ((c = strstr(q->soname, "$non_lazy_ptr")) != NULL) {
837 q->soname++; /* skip "L" */
838 *c = 0; /* ignore GOT ref here */
839 }
840 else if ((c = strstr(q->soname, "-L")) != NULL)
841 *c = 0; /* ignore GOT ref here */
842
843 #endif
844
845 }
846 #endif
847
848 if (p->n_op == ICON && p->n_sp != NULL && DEUNSIGN(t) != INT)
849 uerror("element not constant");
850
851 switch (t) {
852 case LONGLONG:
853 case ULONGLONG:
854 #if 0
855 /* little-endian */
856 i = (glval(p) >> 32);
857 slval(p, glval(p) & 0xffffffff);
858 p->n_type = INT;
859 ninval(off, 32, p);
860 slval(p, i);
861 ninval(off+32, 32, p);
862 #endif
863 /* big-endian */
864 i = (glval(p) & 0xffffffff);
865 slval(p, glval(p) >> 32);
866 p->n_type = INT;
867 ninval(off, 32, p);
868 slval(p, i);
869 ninval(off+32, 32, p);
870
871 break;
872 case INT:
873 case UNSIGNED:
874 printf("\t.long %d", (int)glval(p));
875 if ((q = p->n_sp) != NULL) {
876 if ((q->sclass == STATIC && q->slevel > 0)) {
877 printf("+" LABFMT, q->soffset);
878 } else {
879 char *name = getexname(q);
880 printf("+%s", name);
881 }
882 }
883 printf("\n");
884 break;
885 default:
886 return 0;
887 }
888 return 1;
889 }
890
891 /* make a name look like an external name in the local machine */
892 char *
893 exname(char *p)
894 {
895 #if defined(ELFABI)
896
897 return (p == NULL ? "" : p);
898
899 #elif defined(MACHOABI)
900
901 #define NCHNAM 256
902 static char text[NCHNAM+1];
903 int i;
904
905 if (p == NULL)
906 return "";
907
908 text[0] = '_';
909 for (i=1; *p && i<NCHNAM; ++i)
910 text[i] = *p++;
911
912 text[i] = '\0';
913 text[NCHNAM] = '\0'; /* truncate */
914
915 return (text);
916
917 #endif
918 }
919
920 /*
921 * map types which are not defined on the local machine
922 */
923 TWORD
924 ctype(TWORD type)
925 {
926 switch (BTYPE(type)) {
927 case LONG:
928 MODTYPE(type,INT);
929 break;
930
931 case ULONG:
932 MODTYPE(type,UNSIGNED);
933
934 }
935 return (type);
936 }
937
938 void
939 calldec(NODE *p, NODE *q)
940 {
941 #ifdef PCC_DEBUG
942 if (xdebug)
943 printf("calldec:\n");
944 #endif
945 }
946
947 void
948 extdec(struct symtab *q)
949 {
950 #ifdef PCC_DEBUG
951 if (xdebug)
952 printf("extdec:\n");
953 #endif
954 }
955
956 /* make a common declaration for id, if reasonable */
957 void
958 defzero(struct symtab *sp)
959 {
960 char *n;
961 int off;
962
963 off = tsize(sp->stype, sp->sdf, sp->sap);
964 off = (off+(SZCHAR-1))/SZCHAR;
965 printf("\t.%scomm ", sp->sclass == STATIC ? "l" : "");
966 n = getexname(sp);
967 if (sp->slevel == 0)
968 printf("%s,%d\n", n, off);
969 else
970 printf(LABFMT ",%d\n", sp->soffset, off);
971 }
972
973
974 #ifdef notdef
975 /* make a common declaration for id, if reasonable */
976 void
977 commdec(struct symtab *q)
978 {
979 int off;
980
981 off = tsize(q->stype, q->sdf, q->ssue);
982 off = (off+(SZCHAR-1))/SZCHAR;
983 printf("\t.comm %s,0%o\n", getexname(q), off);
984 }
985
986 /* make a local common declaration for id, if reasonable */
987 void
988 lcommdec(struct symtab *q)
989 {
990 int off;
991
992 off = tsize(q->stype, q->sdf, q->ssue);
993 off = (off+(SZCHAR-1))/SZCHAR;
994 if (q->slevel == 0)
995 printf("\t.lcomm %s,%d\n", getexname(q), off);
996 else
997 printf("\t.lcomm " LABFMT ",%d\n", q->soffset, off);
998 }
999
1000 /*
1001 * print a (non-prog) label.
1002 */
1003 void
1004 deflab1(int label)
1005 {
1006 printf(LABFMT ":\n", label);
1007 }
1008
1009 #if defined(ELFABI)
1010
1011 static char *loctbl[] = { "text", "data", "section .rodata,",
1012 "section .rodata" };
1013
1014 #elif defined(MACHOABI)
1015
1016 static char *loctbl[] = { "text", "data", "section .rodata,", "cstring" };
1017
1018 #endif
1019
1020 void
1021 setloc1(int locc)
1022 {
1023 #ifdef PCC_DEBUG
1024 if (xdebug)
1025 printf("setloc1: locc=%d, lastloc=%d\n", locc, lastloc);
1026 #endif
1027
1028 if (locc == lastloc)
1029 return;
1030 lastloc = locc;
1031 printf(" .%s\n", loctbl[locc]);
1032 }
1033 #endif
1034
1035 /* simulate and optimise the MOD opcode */
1036 static void
1037 simmod(NODE *p)
1038 {
1039 NODE *r = p->n_right;
1040
1041 assert(p->n_op == MOD);
1042
1043 if (!ISUNSIGNED(p->n_type))
1044 return;
1045
1046 #define ISPOW2(n) ((n) && (((n)&((n)-1)) == 0))
1047
1048 /* if the right is a constant power of two, then replace with AND */
1049 if (r->n_op == ICON && ISPOW2(glval(r))) {
1050 p->n_op = AND;
1051 slval(r, glval(r) - 1);
1052 return;
1053 }
1054
1055 #undef ISPOW2
1056
1057 /* other optimizations can go here */
1058 }
1059
1060 static int constructor;
1061 static int destructor;
1062
1063 /*
1064 * Give target the opportunity of handling pragmas.
1065 */
1066 int
1067 mypragma(char *str)
1068 {
1069 if (strcmp(str, "tls") == 0) {
1070 uerror("thread-local storage not supported for this target");
1071 return 1;
1072 }
1073 if (strcmp(str, "constructor") == 0 || strcmp(str, "init") == 0) {
1074 constructor = 1;
1075 return 1;
1076 }
1077 if (strcmp(str, "destructor") == 0 || strcmp(str, "fini") == 0) {
1078 destructor = 1;
1079 return 1;
1080 }
1081
1082 return 0;
1083 }
1084
1085 /*
1086 * Called when a identifier has been declared, to give target last word.
1087 */
1088 void
1089 fixdef(struct symtab *sp)
1090 {
1091 /* may have sanity checks here */
1092 if ((constructor || destructor) && (sp->sclass != PARAM)) {
1093 #ifdef MACHOABI
1094 if (kflag) {
1095 if (constructor)
1096 printf("\t.mod_init_func\n");
1097 else
1098 printf("\t.mod_term_func\n");
1099 } else {
1100 if (constructor)
1101 printf("\t.constructor\n");
1102 else
1103 printf("\t.destructor\n");
1104 }
1105 printf("\t.p2align 2\n");
1106 printf("\t.long %s\n", exname(sp->sname));
1107 printf("\t.text\n");
1108 constructor = destructor = 0;
1109 #endif
1110 }
1111 }
1112
1113 /*
1114 * There is very little different here to the standard builtins.
1115 * It basically handles promotion of types smaller than INT.
1116 */
1117
1118 NODE *
1119 powerpc_builtin_stdarg_start(const struct bitable *bi, NODE *a)
1120 {
1121 NODE *p, *q;
1122 int sz = 1;
1123
1124 /* must first deal with argument size; use int size */
1125 p = a->n_right;
1126 if (p->n_type < INT) {
1127 /* round up to word */
1128 sz = SZINT / tsize(p->n_type, p->n_df, p->n_ap);
1129 }
1130
1131 p = buildtree(ADDROF, p, NIL); /* address of last arg */
1132 p = optim(buildtree(PLUS, p, bcon(sz)));
1133 q = block(NAME, NIL, NIL, PTR+VOID, 0, 0);
1134 q = buildtree(CAST, q, p);
1135 p = q->n_right;
1136 nfree(q->n_left);
1137 nfree(q);
1138 p = buildtree(ASSIGN, a->n_left, p);
1139 nfree(a);
1140
1141 return p;
1142 }
1143
1144 NODE *
1145 powerpc_builtin_va_arg(const struct bitable *bi, NODE *a)
1146 {
1147 NODE *p, *q, *r;
1148 int sz, tmpnr;
1149
1150 r = a->n_right;
1151
1152 /* get type size */
1153 sz = tsize(r->n_type, r->n_df, r->n_ap) / SZCHAR;
1154 if (sz < SZINT/SZCHAR) {
1155 werror("%s%s promoted to int when passed through ...",
1156 ISUNSIGNED(r->n_type) ? "unsigned " : "",
1157 DEUNSIGN(r->n_type) == SHORT ? "short" : "char");
1158 sz = SZINT/SZCHAR;
1159 r->n_type = INT;
1160 r->n_ap = 0;
1161 }
1162
1163 p = p1tcopy(a->n_left);
1164
1165 #if defined(ELFABI)
1166
1167 /* alignment */
1168 if (SZINT/SZCHAR && r->n_type != UNIONTY && r->n_type != STRTY) {
1169 p = buildtree(PLUS, p, bcon(ALSTACK/8 - 1));
1170 p = block(AND, p, bcon(-ALSTACK/8), p->n_type, p->n_df, p->n_ap);
1171 }
1172
1173 #endif
1174
1175 /* create a copy to a temp node */
1176 q = tempnode(0, p->n_type, p->n_df, p->n_ap);
1177 tmpnr = regno(q);
1178 p = buildtree(ASSIGN, q, p);
1179
1180 q = tempnode(tmpnr, p->n_type, p->n_df, p->n_ap);
1181 q = buildtree(PLUS, q, bcon(sz));
1182 q = buildtree(ASSIGN, a->n_left, q);
1183
1184 q = buildtree(COMOP, p, q);
1185
1186 nfree(a->n_right);
1187 nfree(a);
1188
1189 p = tempnode(tmpnr, INCREF(r->n_type), r->n_df, r->n_ap);
1190 p = buildtree(UMUL, p, NIL);
1191 p = buildtree(COMOP, q, p);
1192
1193 return p;
1194 }
1195
1196 NODE *
1197 powerpc_builtin_va_end(const struct bitable *bi, NODE *a)
1198 {
1199 p1tfree(a);
1200 return bcon(0);
1201 }
1202
1203 NODE *
1204 powerpc_builtin_va_copy(const struct bitable *bi, NODE *a)
1205 {
1206 P1ND *f = buildtree(ASSIGN, a->n_left, a->n_right);
1207 p1nfree(a);
1208 return f;
1209 }
1210
1211 NODE *
1212 builtin_return_address(const struct bitable *bt, NODE *a)
1213 {
1214 P1ND *f;
1215 int nframes;
1216 int i = 0;
1217
1218 nframes = glval(a);
1219
1220 p1tfree(a);
1221
1222 f = block(REG, NIL, NIL, PTR+VOID, 0, 0);
1223 regno(f) = SPREG;
1224
1225 do {
1226 f = block(UMUL, f, NIL, PTR+VOID, 0, 0);
1227 } while (i++ < nframes);
1228
1229 f = block(PLUS, f, bcon(8), INCREF(PTR+VOID), 0, 0);
1230 f = buildtree(UMUL, f, NIL);
1231
1232 return f;
1233 }
1234
1235 NODE *
1236 builtin_frame_address(const struct bitable *bt, NODE *a)
1237 {
1238 P1ND *f;
1239 int nframes;
1240 int i = 0;
1241
1242 nframes = glval(a);
1243
1244 p1tfree(a);
1245
1246 if (nframes == 0) {
1247 f = block(REG, NIL, NIL, PTR+VOID, 0, 0);
1248 regno(f) = FPREG;
1249 } else {
1250 f = block(REG, NIL, NIL, PTR+VOID, 0, 0);
1251 regno(f) = SPREG;
1252 do {
1253 f = block(UMUL, f, NIL, PTR+VOID, 0, 0);
1254 } while (i++ < nframes);
1255 f = block(PLUS, f, bcon(24), INCREF(PTR+VOID), 0, 0);
1256 f = buildtree(UMUL, f, NIL);
1257 }
1258
1259 return f;
1260 }
1261
1262 /*
1263 * Return "canonical frame address".
1264 */
1265 NODE *
1266 builtin_cfa(const struct bitable *bt, NODE *a)
1267 {
1268 cerror("builtin_cfa");
1269 return bcon(0);
1270 }
1271
1272
1273 void
1274 pass1_lastchance(struct interpass *ip)
1275 {
1276 }