Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20150106032558

Diff

Diff from 1.188 to:

Annotations

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

Annotated File View

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