Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20110701143657

Diff

Diff from 1.155 to:

Annotations

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

Annotated File View

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