Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20150720151558

Diff

Diff from 1.190 to:

Annotations

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

Annotated File View

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