Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20160305153124

Diff

Diff from 1.200 to:

Annotations

Annotate by Age | Author | Mixed | None
/fisheye/browse/pcc/pcc/arch/i386/local.c

Annotated File View

ragge
1.200
1 /*      $Id: local.c,v 1.200 2016/03/05 15:31:24 ragge Exp $    */
ragge
1.1
2 /*
3  * Copyright (c) 2003 Anders Magnusson (ragge@ludd.luth.se).
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
ragge
1.48
28 #include "pass1.h"
ragge
1.1
29
ragge
1.193
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
ragge
1.60
40 /*      this file contains code which is dependent on the target machine */
41
ragge
1.130
42 #ifdef notyet
ragge
1.57
43 /*
44  * Check if a constant is too large for a type.
45  */
46 static int
47 toolarge(TWORD tCONSZ 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);
otto
1.58
59         case BOOL:
ragge
1.57
60         SCHK(CHAR);
61         UCHK(UNSIGNED);
62         UCHK(USHORT);
63         UCHK(UCHAR);
64         default:
65                 cerror("toolarge");
66         }
67         return 0;
68 }
ragge
1.130
69 #endif
ragge
1.57
70
ragge
1.194
71 static char *
72 getsoname(struct symtab *sp)
73 {
74         struct attr *ap;
75         return (ap = attr_find(sp->sapATTR_SONAME)) ?
76             ap->sarg(0) : sp->sname;
77         
78 }
79
gmcgarry
1.83
80 #if defined(MACHOABI)
81
82 /*
83  *  Keep track of PIC stubs.
84  */
85
86 void
87 addstub(struct stub *listchar *name)
88 {
89         struct stub *s;
90
91         DLIST_FOREACH(slistlink) {
92                 if (strcmp(s->namename) == 0)
93                         return;
94         }
95
96         s = permalloc(sizeof(struct stub));
plunky
1.166
97         s->name = newstring(namestrlen(name));
gmcgarry
1.83
98         DLIST_INSERT_BEFORE(listslink);
99 }
100
101 #endif
102
ragge
1.60
103 #define IALLOC(sz)      (isinlining ? permalloc(sz) : tmpalloc(sz))
gmcgarry
1.86
104
ragge
1.60
105 /*
106  * Make a symtab entry for PIC use.
107  */
108 static struct symtab *
gmcgarry
1.83
109 picsymtab(char *pchar *schar *s2)
ragge
1.60
110 {
111         struct symtab *sp = IALLOC(sizeof(struct symtab));
gmcgarry
1.83
112         size_t len = strlen(p) + strlen(s) + strlen(s2) + 1;
ragge
1.60
113         
ragge
1.194
114         sp->sname = IALLOC(len);
115         strlcpy(sp->snameplen);
116         strlcat(sp->snameslen);
117         strlcat(sp->snames2len);
118         sp->sap = attr_new(ATTR_SONAME1);
119         sp->sap->sarg(0) = sp->sname;
ragge
1.60
120         sp->sclass = EXTERN;
ragge
1.64
121         sp->sflags = sp->slevel = 0;
gmcgarry
1.158
122         sp->stype = 0xdeadbeef;
ragge
1.60
123         return sp;
124 }
gmcgarry
1.103
125
plunky
1.184
126 #ifdef PECOFFABI
ragge
1.193
127 static P1ND *
128 import(P1ND *p)
gmcgarry
1.103
129 {
ragge
1.194
130         struct attr *ap;
ragge
1.193
131         P1ND *q;
gmcgarry
1.103
132         char *name;
133         struct symtab *sp;
134
ragge
1.194
135         name = getexname(p->n_sp);
gmcgarry
1.103
136
137         sp = picsymtab("__imp_"name"");
138         q = xbcon(0spPTR+VOID);
ragge
1.138
139         q = block(UMULq0PTR|VOID00);
ragge
1.136
140         q = block(UMULq0p->n_typep->n_dfp->n_ap);
gmcgarry
1.103
141         q->n_sp = p->n_sp/* for init */
ragge
1.193
142         p1nfree(p);
gmcgarry
1.103
143
144         return q;
145 }
gmcgarry
1.86
146 #endif
ragge
1.60
147
148 int gotnr/* tempnum for GOT register */
gmcgarry
1.86
149 int argstacksize;
150
ragge
1.60
151 /*
152  * Create a reference for an extern variable.
153  */
ragge
1.193
154 static P1ND *
155 picext(P1ND *p)
ragge
1.60
156 {
ragge
1.194
157         struct attr *ap;
gmcgarry
1.86
158
159 #if defined(ELFABI)
ragge
1.193
160         P1ND *q, *r;
stefan
1.66
161         struct symtab *sp;
ragge
1.96
162         char *name;
ragge
1.60
163
ragge
1.138
164         q = tempnode(gotnrPTR|VOID00);
ragge
1.194
165         name = getexname(p->n_sp);
ragge
1.171
166
ragge
1.190
167 #ifdef GCC_COMPAT
ragge
1.194
168         if ((ap = attr_find(p->n_sp->sapGCC_ATYP_VISIBILITY)) &&
169             strcmp(ap->sarg(0), "hidden") == 0) {
ragge
1.171
170                 /* For hidden vars use GOTOFF */
171                 sp = picsymtab(""name"@GOTOFF");
172                 r = xbcon(0spINT);
173                 q = buildtree(PLUSqr);
174                 q = block(UMULq0p->n_typep->n_dfp->n_ap);
175                 q->n_sp = p->n_sp/* for init */
ragge
1.193
176                 p1nfree(p);
ragge
1.171
177                 return q;
178         }
ragge
1.190
179 #endif
ragge
1.171
180
ragge
1.96
181         sp = picsymtab(""name"@GOT");
ragge
1.117
182 #ifdef GCC_COMPAT
ragge
1.123
183         if (attr_find(p->n_sp->sapGCC_ATYP_STDCALL) != NULL)
ragge
1.117
184                 p->n_sp->sflags |= SSTDCALL;
185 #endif
ragge
1.118
186         sp->sflags = p->n_sp->sflags & SSTDCALL;
ragge
1.195
187         sp->sap = attr_add(p->n_sp->sapsp->sap);
stefan
1.66
188         r = xbcon(0spINT);
ragge
1.60
189         q = buildtree(PLUSqr);
ragge
1.138
190         q = block(UMULq0PTR|VOID00);
ragge
1.123
191         q = block(UMULq0p->n_typep->n_dfp->n_ap);
gmcgarry
1.86
192         q->n_sp = p->n_sp/* for init */
ragge
1.193
193         p1nfree(p);
gmcgarry
1.86
194         return q;
gmcgarry
1.83
195
196 #elif defined(MACHOABI)
197
ragge
1.193
198         P1ND *q, *r;
gmcgarry
1.86
199         struct symtab *sp;
gmcgarry
1.101
200         char buf2[256], *name, *pspn;
gmcgarry
1.83
201
ragge
1.194
202         name = getsoname(cftnsp);
203         pspn = getexname(p->n_sp);
204
gmcgarry
1.83
205         if (p->n_sp->sclass == EXTDEF) {
gmcgarry
1.101
206                 snprintf(buf2256"-L%s$pb"name);
pantzer
1.98
207                 sp = picsymtab(""pspnbuf2);
gmcgarry
1.83
208         } else {
gmcgarry
1.101
209                 snprintf(buf2256"$non_lazy_ptr-L%s$pb"name);
ragge
1.96
210                 sp = picsymtab("L"pspnbuf2);
211                 addstub(&nlplistpspn);
gmcgarry
1.83
212         }
gmcgarry
1.158
213
214         sp->stype = p->n_sp->stype;
215
ragge
1.138
216         q = tempnode(gotnrPTR+VOID00);
gmcgarry
1.83
217         r = xbcon(0spINT);
218         q = buildtree(PLUSqr);
219
220         if (p->n_sp->sclass != EXTDEF)
ragge
1.138
221                 q = block(UMULq0PTR+VOID00);
ragge
1.123
222         q = block(UMULq0p->n_typep->n_dfp->n_ap);
ragge
1.65
223         q->n_sp = p->n_sp/* for init */
ragge
1.193
224         p1nfree(p);
ragge
1.60
225         return q;
gmcgarry
1.86
226
ragge
1.137
227 #else /* defined(PECOFFABI) || defined(AOUTABI) */
gmcgarry
1.86
228
229         return p;
230
231 #endif
232
ragge
1.60
233 }
234
235 /*
236  * Create a reference for a static variable.
237  */
ragge
1.193
238 static P1ND *
239 picstatic(P1ND *p)
ragge
1.60
240 {
gmcgarry
1.86
241 #if defined(ELFABI)
242
ragge
1.193
243         P1ND *q, *r;
stefan
1.66
244         struct symtab *sp;
ragge
1.60
245
ragge
1.138
246         q = tempnode(gotnrPTR|VOID00);
ragge
1.94
247         if (p->n_sp->slevel > 0) {
ragge
1.60
248                 char buf[32];
ragge
1.189
249                 if ((p->n_sp->sflags & SMASK) == SSTRING)
250                         p->n_sp->sflags |= SASG;
ragge
1.60
251                 snprintf(buf32LABFMT, (int)p->n_sp->soffset);
gmcgarry
1.83
252                 sp = picsymtab(""buf"@GOTOFF");
ragge
1.194
253         } else
254                 sp = picsymtab(""getsoname(p->n_sp), "@GOTOFF");
ragge
1.96
255         
stefan
1.66
256         sp->sclass = STATIC;
257         sp->stype = p->n_sp->stype;
258         r = xbcon(0spINT);
ragge
1.60
259         q = buildtree(PLUSqr);
ragge
1.123
260         q = block(UMULq0p->n_typep->n_dfp->n_ap);
ragge
1.65
261         q->n_sp = p->n_sp/* for init */
ragge
1.193
262         p1nfree(p);
gmcgarry
1.86
263         return q;
gmcgarry
1.83
264
265 #elif defined(MACHOABI)
266
ragge
1.193
267         P1ND *q, *r;
gmcgarry
1.86
268         struct symtab *sp;
gmcgarry
1.101
269         char buf2[256];
gmcgarry
1.83
270
gmcgarry
1.101
271         snprintf(buf2256"-L%s$pb",
ragge
1.97
272             cftnsp->soname ? cftnsp->soname : cftnsp->sname);
gmcgarry
1.83
273
ragge
1.94
274         if (p->n_sp->slevel > 0) {
gmcgarry
1.101
275                 char buf1[32];
276                 snprintf(buf132LABFMT, (int)p->n_sp->soffset);
gmcgarry
1.83
277                 sp = picsymtab(""buf1buf2);
278         } else  {
ragge
1.194
279                 char *name = getexname(p->n_sp);
280                 sp = picsymtab(""namebuf2);
gmcgarry
1.83
281         }
282         sp->sclass = STATIC;
283         sp->stype = p->n_sp->stype;
ragge
1.138
284         q = tempnode(gotnrPTR+VOID00);
gmcgarry
1.83
285         r = xbcon(0spINT);
286         q = buildtree(PLUSqr);
ragge
1.123
287         q = block(UMULq0p->n_typep->n_dfp->n_ap);
gmcgarry
1.83
288         q->n_sp = p->n_sp;
ragge
1.193
289         p1nfree(p);
gmcgarry
1.86
290         return q;
291
ragge
1.137
292 #else /* defined(PECOFFABI) || defined(AOUTABI) */
gmcgarry
1.86
293
294         return p;
gmcgarry
1.83
295
296 #endif
297
ragge
1.60
298 }
ragge
1.1
299
ragge
1.73
300 #ifdef TLS
301 /*
302  * Create a reference for a TLS variable.
303  */
ragge
1.193
304 static P1ND *
305 tlspic(P1ND *p)
ragge
1.73
306 {
ragge
1.193
307         P1ND *q, *r;
ragge
1.85
308         struct symtab *sp, *sp2;
ragge
1.96
309         char *name;
ragge
1.73
310
311         /*
312          * creates:
313          *   leal var@TLSGD(%ebx),%eax
314          *   call ___tls_get_addr@PLT
315          */
316
317         /* calc address of var@TLSGD */
ragge
1.138
318         q = tempnode(gotnrPTR|VOID00);
ragge
1.194
319         name = getsoname(p->n_sp);
ragge
1.96
320         sp = picsymtab(""name"@TLSGD");
ragge
1.73
321         r = xbcon(0spINT);
322         q = buildtree(PLUSqr);
323
324         /* assign to %eax */
ragge
1.138
325         r = block(REGNILNILPTR|VOID00);
ragge
1.73
326         r->n_rval = EAX;
327         q = buildtree(ASSIGNrq);
328
329         /* call ___tls_get_addr */
ragge
1.85
330         sp2 = lookup("___tls_get_addr@PLT"0);
331         sp2->stype = EXTERN|INT|FTN;
332         r = nametree(sp2);
ragge
1.73
333         r = buildtree(ADDROFrNIL);
ragge
1.138
334         r = block(UCALLrNILINT00);
ragge
1.73
335
336         /* fusion both parts together */
337         q = buildtree(COMOPqr);
ragge
1.123
338         q = block(UMULq0p->n_typep->n_dfp->n_ap);
ragge
1.73
339         q->n_sp = p->n_sp/* for init */
340
ragge
1.193
341         p1nfree(p);
ragge
1.73
342         return q;
343 }
344
ragge
1.193
345 static P1ND *
346 tlsnonpic(P1ND *p)
ragge
1.73
347 {
ragge
1.193
348         P1ND *q, *r;
ragge
1.85
349         struct symtab *sp, *sp2;
ragge
1.73
350         int ext = p->n_sp->sclass;
ragge
1.96
351         char *name;
ragge
1.73
352
ragge
1.194
353         name = getsoname(p->n_sp);
ragge
1.96
354         sp = picsymtab(""name,
gmcgarry
1.83
355             ext == EXTERN ? "@INDNTPOFF" : "@NTPOFF");
ragge
1.73
356         q = xbcon(0spINT);
357         if (ext == EXTERN)
ragge
1.138
358                 q = block(UMULqNILPTR|VOID00);
ragge
1.73
359
ragge
1.85
360         sp2 = lookup("%gs:0"0);
361         sp2->stype = EXTERN|INT;
362         r = nametree(sp2);
ragge
1.73
363
364         q = buildtree(PLUSqr);
ragge
1.123
365         q = block(UMULq0p->n_typep->n_dfp->n_ap);
ragge
1.73
366         q->n_sp = p->n_sp/* for init */
367
ragge
1.193
368         p1nfree(p);
ragge
1.73
369         return q;
370 }
371
ragge
1.193
372 static P1ND *
373 tlsref(P1ND *p)
ragge
1.73
374 {
375         if (kflag)
376                 return (tlspic(p));
377         else
378                 return (tlsnonpic(p));
379 }
380 #endif
381
ragge
1.48
382 /* clocal() is called to do local transformations on
383  * an expression tree preparitory to its being
384  * written out in intermediate code.
385  *
386  * the major essential job is rewriting the
387  * automatic variables and arguments in terms of
388  * REG and OREG nodes
389  * conversion ops which are not necessary are also clobbered here
390  * in addition, any special features (such as rewriting
391  * exclusive or) are easily handled here as well
392  */
ragge
1.193
393 P1ND *
394 clocal(P1ND *p)
ragge
1.1
395 {
396
ragge
1.177
397         struct attr *ap;
ragge
1.1
398         register struct symtab *q;
ragge
1.193
399         register P1ND *r, *l, *n, *s;
ragge
1.1
400         register int o;
ragge
1.40
401         register int m;
ragge
1.1
402
ragge
1.54
403 #ifdef PCC_DEBUG
404         if (xdebug) {
405                 printf("clocal: %p\n"p);
ragge
1.193
406                 p1fwalk(peprint0);
ragge
1.54
407         }
408 #endif
ragge
1.1
409         switcho = p->n_op ){
410
411         case NAME:
412                 if ((q = p->n_sp) == NULL)
413                         return p/* Nothing to care about */
414
415                 switch (q->sclass) {
416
417                 case PARAM:
418                 case AUTO:
419                         /* fake up a structure reference */
420                         r = block(REGNILNILPTR+STRTY00);
ragge
1.199
421                         slval(r0);
ragge
1.1
422                         r->n_rval = FPREG;
423                         p = stref(block(STREFrp000));
424                         break;
425
ragge
1.65
426                 case USTATIC:
427                         if (kflag == 0)
428                                 break;
429                         /* FALLTHROUGH */
ragge
1.1
430                 case STATIC:
ragge
1.73
431 #ifdef TLS
432                         if (q->sflags & STLS) {
433                                 p = tlsref(p);
434                                 break;
435                         }
436 #endif
ragge
1.60
437                         if (kflag == 0) {
438                                 if (q->slevel == 0)
439                                         break;
ragge
1.178
440                         } else if (kflag && !statinit && blevel > 0 &&
ragge
1.190
441 #ifdef GCC_COMPAT
ragge
1.178
442                             attr_find(q->sapGCC_ATYP_WEAKREF)) {
ragge
1.190
443 #else
444                             0) {
445 #endif
ragge
1.178
446                                 /* extern call */
447                                 p = picext(p);
ragge
1.147
448                         } else if (blevel > 0 && !statinit)
ragge
1.60
449                                 p = picstatic(p);
ragge
1.1
450                         break;
451
452                 case REGISTER:
453                         p->n_op = REG;
ragge
1.199
454                         slval(p0);
ragge
1.1
455                         p->n_rval = q->soffset;
456                         break;
457
ragge
1.60
458                 case EXTERN:
459                 case EXTDEF:
ragge
1.73
460 #ifdef TLS
461                         if (q->sflags & STLS) {
462                                 p = tlsref(p);
463                                 break;
464                         }
465 #endif
gmcgarry
1.103
466
plunky
1.184
467 #ifdef PECOFFABI
gmcgarry
1.103
468                         if (q->sflags & SDLLINDIRECT)
469                                 p = import(p);
470 #endif
ragge
1.190
471 #ifdef GCC_COMPAT
ragge
1.177
472                         if ((ap = attr_find(q->sap,
473                             GCC_ATYP_VISIBILITY)) != NULL &&
ragge
1.194
474                             strcmp(ap->sarg(0), "hidden") == 0)
475                                 printf(PRTPREF "\t.hidden %s\n"getsoname(q));
ragge
1.190
476 #endif
ragge
1.60
477                         if (kflag == 0)
478                                 break;
ragge
1.147
479                         if (blevel > 0 && !statinit)
ragge
1.60
480                                 p = picext(p);
481                         break;
482                 }
ragge
1.1
483                 break;
484
ragge
1.65
485         case ADDROF:
ragge
1.175
486                 if (kflag == 0 || blevel == 0 || statinit)
ragge
1.65
487                         break;
488                 /* char arrays may end up here */
489                 l = p->n_left;
490                 if (l->n_op != NAME ||
491                     (l->n_type != ARY+CHAR && l->n_type != ARY+WCHAR_TYPE))
492                         break;
493                 l = p;
494                 p = picstatic(p->n_left);
ragge
1.193
495                 p1nfree(l);
ragge
1.65
496                 if (p->n_op != UMUL)
497                         cerror("ADDROF error");
498                 l = p;
499                 p = p->n_left;
ragge
1.193
500                 p1nfree(l);
ragge
1.65
501                 break;
502
ragge
1.62
503         case UCALL:
ragge
1.60
504                 if (kflag == 0)
505                         break;
ragge
1.138
506                 l = block(REGNILNILINT00);
ragge
1.62
507                 l->n_rval = EBX;
ragge
1.179
508                 p->n_right = buildtree(ASSIGNltempnode(gotnrINT00));
509                 p->n_op -= (UCALL-CALL);
510                 break;
511
512         case USTCALL:
ragge
1.182
513 #if defined(os_openbsd)
514                 ap = strattr(p->n_left->n_ap);
515                 if (ap->amsize == SZCHAR || ap->amsize == SZSHORT ||
516                     ap->amsize == SZINT || ap->amsize == SZLONGLONG)
517 #else
ragge
1.181
518                 if (attr_find(p->n_left->n_apATTR_COMPLEX) &&
519                     (ap = strattr(p->n_left->n_ap)) &&
ragge
1.182
520                     ap->amsize == SZLONGLONG)
521 #endif
522                 {
ragge
1.181
523                         /* float complex */
524                         /* fake one arg to make pass2 happy */
525                         p->n_right = block(FUNARGbcon(0), NILINT00);
526                         p->n_op -= (UCALL-CALL);
527                         break;
528                 }
ragge
1.182
529
ragge
1.179
530                 /* Add hidden arg0 */
531                 r = block(REGNILNILINCREF(VOID), 00);
532                 regno(r) = EBP;
ragge
1.190
533 #ifdef GCC_COMPAT
ragge
1.179
534                 if ((ap = attr_find(p->n_apGCC_ATYP_REGPARM)) != NULL &&
535                     ap->iarg(0) > 0) {
536                         l = block(REGNILNILINCREF(VOID), 00);
537                         regno(l) = EAX;
538                         p->n_right = buildtree(ASSIGNlr);
539                 } else
ragge
1.190
540 #endif
ragge
1.179
541                         p->n_right = block(FUNARGrNILINCREF(VOID), 00);
ragge
1.62
542                 p->n_op -= (UCALL-CALL);
ragge
1.179
543
544                 if (kflag == 0)
545                         break;
546                 l = block(REGNILNILINT00);
547                 regno(l) = EBX;
548                 r = buildtree(ASSIGNltempnode(gotnrINT00));
549                 p->n_right = block(CMrp->n_rightINT00);
550                 break;
pantzer
1.89
551         
552         /* FALLTHROUGH */
553 #if defined(MACHOABI)
554         case CALL:
555         case STCALL:
556                 if (p->n_type == VOID)
557                         break;
558
ragge
1.123
559                 r = tempnode(0p->n_typep->n_dfp->n_ap);
pantzer
1.89
560                 l = tcopy(r);
561                 p = buildtree(COMOPbuildtree(ASSIGNrp), l);
562 #endif
563                         
ragge
1.10
564                 break;
ragge
1.64
565
ragge
1.129
566 #ifdef notyet
567         /* XXX breaks sometimes */
ragge
1.8
568         case CBRANCH:
569                 l = p->n_left;
570
571                 /*
ragge
1.77
572                  * Remove unnecessary conversion ops.
ragge
1.8
573                  */
ragge
1.128
574                 if (!clogop(l->n_op) || l->n_left->n_op != SCONV)
575                         break;
576                 if (coptype(l->n_op) != BITYPE)
577                         break;
578                 if (l->n_right->n_op != ICON)
579                         break;
580                 r = l->n_left->n_left;
581                 if (r->n_type >= FLOAT)
582                         break;
583                 if (toolarge(r->n_typel->n_right->n_lval))
584                         break;
585                 l->n_right->n_type = r->n_type;
586                 if (l->n_op >= ULE && l->n_op <= UGT)
587                         l->n_op -= (UGT-ULE);
588                 p->n_left = buildtree(l->n_oprl->n_right);
ragge
1.193
589                 p1nfree(l->n_left);
590                 p1nfree(l);
ragge
1.8
591                 break;
ragge
1.129
592 #endif
ragge
1.8
593
ragge
1.1
594         case PCONV:
595                 l = p->n_left;
ragge
1.162
596
597                 /* Make int type before pointer */
ragge
1.40
598                 if (l->n_type < INT || l->n_type == LONGLONG || 
ragge
1.134
599                     l->n_type == ULONGLONG || l->n_type == BOOL) {
ragge
1.40
600                         /* float etc? */
ragge
1.162
601                         p->n_left = block(SCONVlNILUNSIGNED00);
ragge
1.40
602                 }
603                 break;
604
ragge
1.1
605         case SCONV:
ragge
1.109
606                 if (p->n_left->n_op == COMOP)
607                         break;  /* may propagate wrong type later */
ragge
1.1
608                 l = p->n_left;
ragge
1.4
609
ragge
1.25
610                 if (p->n_type == l->n_type) {
ragge
1.193
611                         p1nfree(p);
ragge
1.25
612                         return l;
613                 }
614
ragge
1.4
615                 if ((p->n_type & TMASK) == 0 && (l->n_type & TMASK) == 0 &&
ragge
1.139
616                     tsize(p->n_typep->n_dfp->n_ap) ==
617                     tsize(l->n_typel->n_dfl->n_ap)) {
ragge
1.4
618                         if (p->n_type != FLOAT && p->n_type != DOUBLE &&
ragge
1.15
619                             l->n_type != FLOAT && l->n_type != DOUBLE &&
620                             l->n_type != LDOUBLE && p->n_type != LDOUBLE) {
ragge
1.41
621                                 if (l->n_op == NAME || l->n_op == UMUL ||
622                                     l->n_op == TEMP) {
ragge
1.15
623                                         l->n_type = p->n_type;
ragge
1.193
624                                         p1nfree(p);
ragge
1.15
625                                         return l;
626                                 }
ragge
1.4
627                         }
ragge
1.6
628                 }
629
ragge
1.42
630                 if (DEUNSIGN(p->n_type) == INT && DEUNSIGN(l->n_type) == INT &&
ragge
1.131
631                     coptype(l->n_op) == BITYPE && l->n_op != COMOP &&
ragge
1.180
632                     l->n_op != QUEST && l->n_op != ASSIGN) {
ragge
1.42
633                         l->n_type = p->n_type;
ragge
1.193
634                         p1nfree(p);
ragge
1.42
635                         return l;
636                 }
637
ragge
1.3
638                 o = l->n_op;
ragge
1.4
639                 m = p->n_type;
ragge
1.1
640
641                 if (o == ICON) {
ragge
1.147
642                         /*
643                          * Can only end up here if o is an address,
644                          * and in that case the only compile-time conversion
645                          * possible is to int.
646                          */
ragge
1.156
647                         if ((TMASK & l->n_type) == 0 && l->n_sp == NULL)
ragge
1.147
648                                 cerror("SCONV ICON");
649                         if (l->n_sp == 0) {
650                                 p->n_type = UNSIGNED;
651                                 concast(lm);
652                         } else if (m != INT && m != UNSIGNED)
ragge
1.1
653                                 break;
654                         l->n_type = m;
ragge
1.138
655                         l->n_ap = 0;
ragge
1.193
656                         p1nfree(p);
ragge
1.1
657                         return l;
ragge
1.147
658                 } else if (l->n_op == FCON)
659                         cerror("SCONV FCON");
ragge
1.36
660                 if ((p->n_type == CHAR || p->n_type == UCHAR ||
661                     p->n_type == SHORT || p->n_type == USHORT) &&
662                     (l->n_type == FLOAT || l->n_type == DOUBLE ||
663                     l->n_type == LDOUBLE)) {
ragge
1.123
664                         p = block(SCONVpNILp->n_typep->n_dfp->n_ap);
ragge
1.36
665                         p->n_left->n_type = INT;
666                         return p;
667                 }
ragge
1.1
668                 break;
669
ragge
1.15
670         case MOD:
671         case DIV:
672                 if (o == DIV && p->n_type != CHAR && p->n_type != SHORT)
673                         break;
ragge
1.16
674                 if (o == MOD && p->n_type != CHAR && p->n_type != SHORT)
ragge
1.15
675                         break;
676                 /* make it an int division by inserting conversions */
ragge
1.147
677                 p->n_left = makety(p->n_leftINT000);
678                 p->n_right = makety(p->n_rightINT000);
679                 o = p->n_type;
680                 p->n_type = INT;
681                 p = makety(po000);
ragge
1.15
682                 break;
683
ragge
1.31
684         case FORCE:
ragge
1.32
685                 /* put return value in return reg */
686                 p->n_op = ASSIGN;
687                 p->n_right = p->n_left;
ragge
1.138
688                 p->n_left = block(REGNILNILp->n_type00);
ragge
1.53
689                 p->n_left->n_rval = p->n_left->n_type == BOOL ? 
690                     RETREG(CHAR) : RETREG(p->n_type);
ragge
1.31
691                 break;
ragge
1.39
692
693         case LS:
694         case RS:
ragge
1.127
695                 /* shift count must be in a char */
ragge
1.39
696                 if (p->n_right->n_type == CHAR || p->n_right->n_type == UCHAR)
697                         break;
ragge
1.138
698                 p->n_right = block(SCONVp->n_rightNILCHAR00);
ragge
1.39
699                 break;
ragge
1.181
700
ragge
1.108
701                 /* If not using pcc struct return */
702         case STASG:
703                 r = p->n_right;
704                 if (r->n_op != STCALL && r->n_op != USTCALL)
705                         break;
ragge
1.123
706                 m = tsize(BTYPE(r->n_type), r->n_dfr->n_ap);
ragge
1.108
707                 if (m == SZCHAR)
708                         m = CHAR;
709                 else if (m == SZSHORT)
710                         m = SHORT;
711                 else if (m == SZINT)
712                         m = INT;
713                 else if (m == SZLONGLONG)
714                         m = LONGLONG;
715                 else
716                         break;
ragge
1.181
717 #if !defined(os_openbsd)
718                 if (attr_find(r->n_apATTR_COMPLEX) == 0)
719                         break;  /* float _Complex always in regs */
720 #endif
ragge
1.108
721                 l = buildtree(ADDROFp->n_leftNIL);
ragge
1.193
722                 p1nfree(p);
ragge
1.108
723
724                 r->n_op -= (STCALL-CALL);
725                 r->n_type = m;
726
727                 /* r = long, l = &struct */
728
ragge
1.123
729                 n = tempnode(0mr->n_dfr->n_ap);
ragge
1.193
730                 r = buildtree(ASSIGNp1tcopy(n), r);
ragge
1.108
731
ragge
1.123
732                 s = tempnode(0l->n_typel->n_dfl->n_ap);
ragge
1.193
733                 l = buildtree(ASSIGNp1tcopy(s), l);
ragge
1.108
734
735                 p = buildtree(COMOPrl);
736
737                 l = buildtree(CAST,
ragge
1.193
738                     block(NAMENILNILm|PTR00), p1tcopy(s));
ragge
1.108
739                 r = l->n_right;
ragge
1.193
740                 p1nfree(l->n_left);
741                 p1nfree(l);
ragge
1.108
742
743                 r = buildtree(ASSIGNbuildtree(UMULrNIL), n);
744                 p = buildtree(COMOPpr);
745                 p = buildtree(COMOPps);
746                 break;
ragge
1.1
747         }
ragge
1.54
748 #ifdef PCC_DEBUG
749         if (xdebug) {
750                 printf("clocal end: %p\n"p);
ragge
1.193
751                 p1fwalk(peprint0);
ragge
1.54
752         }
753 #endif
ragge
1.1
754         return(p);
755 }
756
ragge
1.64
757 /*
758  * Change CALL references to either direct (static) or PLT.
759  */
760 static void
ragge
1.193
761 fixnames(P1ND *pvoid *arg)
ragge
1.64
762 {
ragge
1.137
763 #if defined(ELFABI) || defined(MACHOABI)
gmcgarry
1.86
764
ragge
1.64
765         struct symtab *sp;
ragge
1.196
766         struct attr *ap, *ap2;
ragge
1.193
767         P1ND *q;
ragge
1.64
768         char *c;
769         int isu;
770
771         if ((cdope(p->n_op) & CALLFLG) == 0)
772                 return;
773         isu = 0;
774         q = p->n_left;
ragge
1.123
775         ap = q->n_ap;
ragge
1.64
776         if (q->n_op == UMUL)
777                 q = q->n_leftisu = 1;
gmcgarry
1.83
778
ragge
1.64
779         if (q->n_op == PLUS && q->n_left->n_op == TEMP &&
780             q->n_right->n_op == ICON) {
781                 sp = q->n_right->n_sp;
782
ragge
1.65
783                 if (sp == NULL)
784                         return/* nothing to do */
ragge
1.64
785                 if (sp->sclass == STATIC && !ISFTN(sp->stype))
786                         return/* function pointer */
787
788                 if (sp->sclass != STATIC && sp->sclass != EXTERN &&
789                     sp->sclass != EXTDEF)
790                         cerror("fixnames");
ragge
1.96
791                 c = NULL;
gmcgarry
1.83
792 #if defined(ELFABI)
793
ragge
1.196
794                 if ((ap2 = attr_find(sp->sapATTR_SONAME)) == NULL ||
795                     (c = strstr(ap2->sarg(0), "@GOT")) == NULL)
796                         cerror("fixnames2: %p %s"ap2c);
ragge
1.64
797                 if (isu) {
798                         memcpy(c"@PLT"sizeof("@PLT"));
799                 } else
800                         *c = 0;
gmcgarry
1.83
801
802 #elif defined(MACHOABI)
803
ragge
1.96
804                 if (sp->soname == NULL ||
805                     ((c = strstr(sp->soname"$non_lazy_ptr")) == NULL &&
806                     (c = strstr(sp->soname"-L")) == NULL))
gmcgarry
1.83
807                                 cerror("fixnames2");
gmcgarry
1.158
808
809                 if (!ISFTN(sp->stype))
810                         return/* function pointer */
811
gmcgarry
1.83
812                 if (isu) {
gmcgarry
1.173
813                         *c = 0;
gmcgarry
1.83
814                         addstub(&stublistsp->soname+1);
plunky
1.166
815                         memcpy(c"$stub"sizeof("$stub"));
gmcgarry
1.83
816                 } else 
817                         *c = 0;
818
819 #endif
820
ragge
1.193
821                 p1nfree(q->n_left);
ragge
1.64
822                 q = q->n_right;
823                 if (isu)
ragge
1.193
824                         p1nfree(p->n_left->n_left);
825                 p1nfree(p->n_left);
ragge
1.64
826                 p->n_left = q;
ragge
1.123
827                 q->n_ap = ap;
ragge
1.64
828         }
gmcgarry
1.86
829 #endif
ragge
1.64
830 }
831
ragge
1.193
832 static void mangle(P1ND *p);
ragge
1.117
833
ragge
1.1
834 void
ragge
1.193
835 myp2tree(P1ND *p)
ragge
1.1
836 {
ragge
1.67
837         struct symtab *sp;
838
ragge
1.64
839         if (kflag)
ragge
1.117
840                 fixnames(p0);
841
842         mangle(p);
843
ragge
1.183
844         if ((p->n_op == STCALL || p->n_op == USTCALL) && 
ragge
1.187
845             attr_find(p->n_apATTR_COMPLEX) &&
846             strmemb(p->n_ap)->stype == FLOAT)
ragge
1.185
847                 p->n_ap = attr_add(p->n_apattr_new(ATTR_I386_FCMPLRET1));
ragge
1.183
848
ragge
1.67
849         if (p->n_op != FCON)
850                 return;
851
852         sp = IALLOC(sizeof(struct symtab));
853         sp->sclass = STATIC;
ragge
1.138
854         sp->sap = 0;
ragge
1.67
855         sp->slevel = 1/* fake numeric label */
ragge
1.70
856         sp->soffset = getlab();
ragge
1.67
857         sp->sflags = 0;
ragge
1.70
858         sp->stype = p->n_type;
859         sp->squal = (CON >> TSHIFT);
ragge
1.194
860         sp->sname = NULL;
ragge
1.70
861
ragge
1.154
862         locctr(DATAsp);
ragge
1.70
863         defloc(sp);
ragge
1.139
864         ninval(0tsize(sp->stypesp->sdfsp->sap), p);
ragge
1.67
865
866         p->n_op = NAME;
ragge
1.199
867         slval(p0);
ragge
1.67