Debian Codegraph @

arch/i386/local.c

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