Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20140929161538

Diff

Diff from 1.183 to:

Annotations

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

Annotated File View

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