Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:gmcgarry:20090427225000

Diff

Diff from 1.105 to:

Annotations

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

Annotated File View

gmcgarry
1.105
1 /*      $Id: local.c,v 1.105 2009/04/27 22:50:00 gmcgarry 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  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29
ragge
1.48
30 #include "pass1.h"
ragge
1.1
31
ragge
1.60
32 /*      this file contains code which is dependent on the target machine */
33
ragge
1.57
34 /*
35  * Check if a constant is too large for a type.
36  */
37 static int
38 toolarge(TWORD tCONSZ con)
39 {
40         U_CONSZ ucon = con;
41
42         switch (t) {
43         case ULONGLONG:
44         case LONGLONG:
45                 break/* cannot be too large */
46 #define SCHK(i) case i: if (con > MAX_##i || con < MIN_##i) return 1; break
47 #define UCHK(i) case i: if (ucon > MAX_##i) return 1; break
48         SCHK(INT);
49         SCHK(SHORT);
otto
1.58
50         case BOOL:
ragge
1.57
51         SCHK(CHAR);
52         UCHK(UNSIGNED);
53         UCHK(USHORT);
54         UCHK(UCHAR);
55         default:
56                 cerror("toolarge");
57         }
58         return 0;
59 }
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.60
100         sp->sclass = EXTERN;
ragge
1.64
101         sp->sflags = sp->slevel = 0;
ragge
1.60
102         return sp;
103 }
gmcgarry
1.103
104
105 #ifdef os_win32
106 static NODE *
107 import(NODE *p)
108 {
109         NODE *q;
110         char *name;
111         struct symtab *sp;
112
113         if ((name = p->n_sp->soname) == NULL)
114                 name = exname(p->n_sp->sname);
115
116         sp = picsymtab("__imp_"name"");
117         q = xbcon(0spPTR+VOID);
118         q = block(UMULq0PTR|VOID0MKSUE(VOID));
119         q = block(UMULq0p->n_typep->n_dfp->n_sue);
120         q->n_sp = p->n_sp/* for init */
121         nfree(p);
122
123         return q;
124 }
gmcgarry
1.86
125 #endif
ragge
1.60
126
127 int gotnr/* tempnum for GOT register */
gmcgarry
1.86
128 int argstacksize;
129
ragge
1.60
130 /*
131  * Create a reference for an extern variable.
132  */
133 static NODE *
134 picext(NODE *p)
135 {
gmcgarry
1.86
136
137 #if defined(ELFABI)
138
ragge
1.60
139         NODE *q, *r;
stefan
1.66
140         struct symtab *sp;
ragge
1.96
141         char *name;
ragge
1.60
142
143         q = tempnode(gotnrPTR|VOID0MKSUE(VOID));
ragge
1.96
144         if ((name = p->n_sp->soname) == NULL)
145                 name = p->n_sp->sname;
146         sp = picsymtab(""name"@GOT");
stefan
1.66
147         r = xbcon(0spINT);
ragge
1.60
148         q = buildtree(PLUSqr);
149         q = block(UMULq0PTR|VOID0MKSUE(VOID));
gmcgarry
1.86
150         q = block(UMULq0p->n_typep->n_dfp->n_sue);
151         q->n_sp = p->n_sp/* for init */
152         nfree(p);
153         return q;
gmcgarry
1.83
154
155 #elif defined(MACHOABI)
156
gmcgarry
1.86
157         NODE *q, *r;
158         struct symtab *sp;
gmcgarry
1.101
159         char buf2[256], *name, *pspn;
gmcgarry
1.83
160
ragge
1.96
161         if ((name = cftnsp->soname) == NULL)
162                 name = cftnsp->sname;
163         if ((pspn = p->n_sp->soname) == NULL)
pantzer
1.98
164                 pspn = exname(p->n_sp->sname);
gmcgarry
1.83
165         if (p->n_sp->sclass == EXTDEF) {
gmcgarry
1.101
166                 snprintf(buf2256"-L%s$pb"name);
pantzer
1.98
167                 sp = picsymtab(""pspnbuf2);
gmcgarry
1.83
168         } else {
gmcgarry
1.101
169                 snprintf(buf2256"$non_lazy_ptr-L%s$pb"name);
ragge
1.96
170                 sp = picsymtab("L"pspnbuf2);
171                 addstub(&nlplistpspn);
gmcgarry
1.83
172         }
173         q = tempnode(gotnrPTR+VOID0MKSUE(VOID));
174         r = xbcon(0spINT);
175         q = buildtree(PLUSqr);
176
177         if (p->n_sp->sclass != EXTDEF)
178                 q = block(UMULq0PTR+VOID0MKSUE(VOID));
ragge
1.60
179         q = block(UMULq0p->n_typep->n_dfp->n_sue);
ragge
1.65
180         q->n_sp = p->n_sp/* for init */
ragge
1.60
181         nfree(p);
182         return q;
gmcgarry
1.86
183
184 #elif defined(PECOFFABI)
185
186         return p;
187
188 #endif
189
ragge
1.60
190 }
191
192 /*
193  * Create a reference for a static variable.
194  */
195 static NODE *
196 picstatic(NODE *p)
197 {
gmcgarry
1.86
198
199 #if defined(ELFABI)
200
ragge
1.60
201         NODE *q, *r;
stefan
1.66
202         struct symtab *sp;
ragge
1.60
203
204         q = tempnode(gotnrPTR|VOID0MKSUE(VOID));
ragge
1.94
205         if (p->n_sp->slevel > 0) {
ragge
1.60
206                 char buf[32];
207                 snprintf(buf32LABFMT, (int)p->n_sp->soffset);
gmcgarry
1.83
208                 sp = picsymtab(""buf"@GOTOFF");
ragge
1.96
209         } else {
210                 char *name;
211                 if ((name = p->n_sp->soname) == NULL)
212                         name = p->n_sp->sname;
213                 sp = picsymtab(""name"@GOTOFF");
214         }
215         
stefan
1.66
216         sp->sclass = STATIC;
217         sp->stype = p->n_sp->stype;
218         r = xbcon(0spINT);
ragge
1.60
219         q = buildtree(PLUSqr);
220         q = block(UMULq0p->n_typep->n_dfp->n_sue);
ragge
1.65
221         q->n_sp = p->n_sp/* for init */
ragge
1.60
222         nfree(p);
gmcgarry
1.86
223         return q;
gmcgarry
1.83
224
225 #elif defined(MACHOABI)
226
gmcgarry
1.86
227         NODE *q, *r;
228         struct symtab *sp;
gmcgarry
1.101
229         char buf2[256];
gmcgarry
1.83
230
gmcgarry
1.101
231         snprintf(buf2256"-L%s$pb",
ragge
1.97
232             cftnsp->soname ? cftnsp->soname : cftnsp->sname);
gmcgarry
1.83
233
ragge
1.94
234         if (p->n_sp->slevel > 0) {
gmcgarry
1.101
235                 char buf1[32];
236                 snprintf(buf132LABFMT, (int)p->n_sp->soffset);
gmcgarry
1.83
237                 sp = picsymtab(""buf1buf2);
238         } else  {
ragge
1.96
239                 char *name;
240                 if ((name = p->n_sp->soname) == NULL)
241                         name = p->n_sp->sname;
242                 sp = picsymtab(""exname(name), buf2);
gmcgarry
1.83
243         }
244         sp->sclass = STATIC;
245         sp->stype = p->n_sp->stype;
246         q = tempnode(gotnrPTR+VOID0MKSUE(VOID));
247         r = xbcon(0spINT);
248         q = buildtree(PLUSqr);
249         q = block(UMULq0p->n_typep->n_dfp->n_sue);
250         q->n_sp = p->n_sp;
251         nfree(p);
gmcgarry
1.86
252         return q;
253
254 #elif defined(PECOFFABI)
255
256         return p;
gmcgarry
1.83
257
258 #endif
259
ragge
1.60
260 }
ragge
1.1
261
ragge
1.73
262 #ifdef TLS
263 /*
264  * Create a reference for a TLS variable.
265  */
266 static NODE *
267 tlspic(NODE *p)
268 {
269         NODE *q, *r;
ragge
1.85
270         struct symtab *sp, *sp2;
ragge
1.96
271         char *name;
ragge
1.73
272
273         /*
274          * creates:
275          *   leal var@TLSGD(%ebx),%eax
276          *   call ___tls_get_addr@PLT
277          */
278
279         /* calc address of var@TLSGD */
280         q = tempnode(gotnrPTR|VOID0MKSUE(VOID));
ragge
1.96
281         if ((name = p->n_sp->soname) == NULL)
282                 name = p->n_sp->sname;
283         sp = picsymtab(""name"@TLSGD");
ragge
1.73
284         r = xbcon(0spINT);
285         q = buildtree(PLUSqr);
286
287         /* assign to %eax */
288         r = block(REGNILNILPTR|VOID0MKSUE(VOID));
289         r->n_rval = EAX;
290         q = buildtree(ASSIGNrq);
291
292         /* call ___tls_get_addr */
ragge
1.85
293         sp2 = lookup("___tls_get_addr@PLT"0);
294         sp2->stype = EXTERN|INT|FTN;
295         r = nametree(sp2);
ragge
1.73
296         r = buildtree(ADDROFrNIL);
297         r = block(UCALLrNILINT0MKSUE(INT));
298
299         /* fusion both parts together */
300         q = buildtree(COMOPqr);
301         q = block(UMULq0p->n_typep->n_dfp->n_sue);
302         q->n_sp = p->n_sp/* for init */
303
304         nfree(p);
305         return q;
306 }
307
308 static NODE *
309 tlsnonpic(NODE *p)
310 {
311         NODE *q, *r;
ragge
1.85
312         struct symtab *sp, *sp2;
ragge
1.73
313         int ext = p->n_sp->sclass;
ragge
1.96
314         char *name;
ragge
1.73
315
ragge
1.96
316         if ((name = p->n_sp->soname) == NULL)
317                 name = p->n_sp->sname;
318         sp = picsymtab(""name,
gmcgarry
1.83
319             ext == EXTERN ? "@INDNTPOFF" : "@NTPOFF");
ragge
1.73
320         q = xbcon(0spINT);
321         if (ext == EXTERN)
322                 q = block(UMULqNILPTR|VOID0MKSUE(VOID));
323
ragge
1.85
324         sp2 = lookup("%gs:0"0);
325         sp2->stype = EXTERN|INT;
326         r = nametree(sp2);
ragge
1.73
327
328         q = buildtree(PLUSqr);
329         q = block(UMULq0p->n_typep->n_dfp->n_sue);
330         q->n_sp = p->n_sp/* for init */
331
332         nfree(p);
333         return q;
334 }
335
336 static NODE *
337 tlsref(NODE *p)
338 {
339         if (kflag)
340                 return (tlspic(p));
341         else
342                 return (tlsnonpic(p));
343 }
344 #endif
345
ragge
1.48
346 /* clocal() is called to do local transformations on
347  * an expression tree preparitory to its being
348  * written out in intermediate code.
349  *
350  * the major essential job is rewriting the
351  * automatic variables and arguments in terms of
352  * REG and OREG nodes
353  * conversion ops which are not necessary are also clobbered here
354  * in addition, any special features (such as rewriting
355  * exclusive or) are easily handled here as well
356  */
ragge
1.1
357 NODE *
358 clocal(NODE *p)
359 {
360
361         register struct symtab *q;
ragge
1.2
362         register NODE *r, *l;
ragge
1.1
363         register int o;
ragge
1.40
364         register int m;
ragge
1.19
365         TWORD t;
ragge
1.1
366
ragge
1.54
367 #ifdef PCC_DEBUG
368         if (xdebug) {
369                 printf("clocal: %p\n"p);
370                 fwalk(peprint0);
371         }
372 #endif
ragge
1.1
373         switcho = p->n_op ){
374
375         case NAME:
376                 if ((q = p->n_sp) == NULL)
377                         return p/* Nothing to care about */
378
379                 switch (q->sclass) {
380
381                 case PARAM:
382                 case AUTO:
383                         /* fake up a structure reference */
384                         r = block(REGNILNILPTR+STRTY00);
385                         r->n_lval = 0;
386                         r->n_rval = FPREG;
387                         p = stref(block(STREFrp000));
388                         break;
389
ragge
1.65
390                 case USTATIC:
391                         if (kflag == 0)
392                                 break;
393                         /* FALLTHROUGH */
ragge
1.1
394                 case STATIC:
ragge
1.73
395 #ifdef TLS
396                         if (q->sflags & STLS) {
397                                 p = tlsref(p);
398                                 break;
399                         }
400 #endif
ragge
1.60
401                         if (kflag == 0) {
402                                 if (q->slevel == 0)
403                                         break;
404                                 p->n_lval = 0;
ragge
1.64
405                         } else if (blevel > 0)
ragge
1.60
406                                 p = picstatic(p);
ragge
1.1
407                         break;
408
409                 case REGISTER:
410                         p->n_op = REG;
411                         p->n_lval = 0;
412                         p->n_rval = q->soffset;
413                         break;
414
ragge
1.60
415                 case EXTERN:
416                 case EXTDEF:
ragge
1.73
417 #ifdef TLS
418                         if (q->sflags & STLS) {
419                                 p = tlsref(p);
420                                 break;
421                         }
422 #endif
gmcgarry
1.103
423
424 #ifdef os_win32
425                         if (q->sflags & SDLLINDIRECT)
426                                 p = import(p);
427 #endif
ragge
1.60
428                         if (kflag == 0)
429                                 break;
ragge
1.64
430                         if (blevel > 0)
ragge
1.60
431                                 p = picext(p);
432                         break;
433                 }
ragge
1.1
434                 break;
435
ragge
1.65
436         case ADDROF:
437                 if (kflag == 0 || blevel == 0)
438                         break;
439                 /* char arrays may end up here */
440                 l = p->n_left;
441                 if (l->n_op != NAME ||
442                     (l->n_type != ARY+CHAR && l->n_type != ARY+WCHAR_TYPE))
443                         break;
444                 l = p;
445                 p = picstatic(p->n_left);
446                 nfree(l);
447                 if (p->n_op != UMUL)
448                         cerror("ADDROF error");
449                 l = p;
450                 p = p->n_left;
451                 nfree(l);
452                 break;
453
ragge
1.62
454         case UCALL:
455         case USTCALL:
ragge
1.60
456                 if (kflag == 0)
457                         break;
gmcgarry
1.86
458 #if defined(ELFABI)
ragge
1.62
459                 /* Change to CALL node with ebx as argument */
460                 l = block(REGNILNILINT0MKSUE(INT));
461                 l->n_rval = EBX;
462                 p->n_right = buildtree(ASSIGNl,
463                     tempnode(gotnrINT0MKSUE(INT)));
464                 p->n_op -= (UCALL-CALL);
gmcgarry
1.86
465 #endif
pantzer
1.89
466         
467         /* FALLTHROUGH */
468 #if defined(MACHOABI)
469         case CALL:
470         case STCALL:
471                 if (p->n_type == VOID)
472                         break;
473
474                 r = tempnode(0p->n_typep->n_dfp->n_sue);
475                 l = tcopy(r);
476                 p = buildtree(COMOPbuildtree(ASSIGNrp), l);
477 #endif
478                         
ragge
1.10
479                 break;
ragge
1.64
480
ragge
1.8
481         case CBRANCH:
482                 l = p->n_left;
483
484                 /*
ragge
1.77
485                  * Remove unnecessary conversion ops.
ragge
1.8
486                  */
ragge
1.10
487                 if (clogop(l->n_op) && l->n_left->n_op == SCONV) {
ragge
1.12
488                         if (coptype(l->n_op) != BITYPE)
489                                 break;
ragge
1.8
490                         if (l->n_right->n_op == ICON) {
491                                 r = l->n_left->n_left;
ragge
1.24
492                                 if (r->n_type >= FLOAT && r->n_type <= LDOUBLE)
493                                         break;
ragge
1.59
494                                 if (ISPTR(r->n_type))
495                                         break/* no opt for pointers */
ragge
1.57
496                                 if (toolarge(r->n_typel->n_right->n_lval))
497                                         break;
ragge
1.18
498                                 /* Type must be correct */
ragge
1.19
499                                 t = r->n_type;
ragge
1.8
500                                 nfree(l->n_left);
501                                 l->n_left = r;
ragge
1.19
502                                 l->n_type = t;
503                                 l->n_right->n_type = t;
ragge
1.17
504                         }
ragge
1.8
505                 }
506                 break;
507
ragge
1.1
508         case PCONV:
ragge
1.40
509                 /* Remove redundant PCONV's. Be careful */
ragge
1.1
510                 l = p->n_left;
ragge
1.40
511                 if (l->n_op == ICON) {
512                         l->n_lval = (unsigned)l->n_lval;
513                         goto delp;
514                 }
515                 if (l->n_type < INT || l->n_type == LONGLONG || 
516                     l->n_type == ULONGLONG) {
517                         /* float etc? */
518                         p->n_left = block(SCONVlNIL,
519                             UNSIGNED0MKSUE(UNSIGNED));
520                         break;
521                 }
522                 /* if left is SCONV, cannot remove */
523                 if (l->n_op == SCONV)
ragge
1.1
524                         break;
ragge
1.54
525
526                 /* avoid ADDROF TEMP */
527                 if (l->n_op == ADDROF && l->n_left->n_op == TEMP)
528                         break;
529
ragge
1.40
530                 /* if conversion to another pointer type, just remove */
531                 if (p->n_type > BTMASK && l->n_type > BTMASK)
532                         goto delp;
533                 break;
534
535         delp:   l->n_type = p->n_type;
ragge
1.1
536                 l->n_qual = p->n_qual;
537                 l->n_df = p->n_df;
538                 l->n_sue = p->n_sue;
539                 nfree(p);
540                 p = l;
541                 break;
ragge
1.40
542                 
ragge
1.1
543         case SCONV:
544                 l = p->n_left;
ragge
1.4
545
ragge
1.25
546                 if (p->n_type == l->n_type) {
547                         nfree(p);
548                         return l;
549                 }
550
ragge
1.4
551                 if ((p->n_type & TMASK) == 0 && (l->n_type & TMASK) == 0 &&
pj
1.30
552                     btdims[p->n_type].suesize == btdims[l->n_type].suesize) {
ragge
1.4
553                         if (p->n_type != FLOAT && p->n_type != DOUBLE &&
ragge
1.15
554                             l->n_type != FLOAT && l->n_type != DOUBLE &&
555                             l->n_type != LDOUBLE && p->n_type != LDOUBLE) {
ragge
1.41
556                                 if (l->n_op == NAME || l->n_op == UMUL ||
557                                     l->n_op == TEMP) {
ragge
1.15
558                                         l->n_type = p->n_type;
559                                         nfree(p);
560                                         return l;
561                                 }
ragge
1.4
562                         }
ragge
1.6
563                 }
564
ragge
1.42
565                 if (DEUNSIGN(p->n_type) == INT && DEUNSIGN(l->n_type) == INT &&
566                     coptype(l->n_op) == BITYPE) {
567                         l->n_type = p->n_type;
568                         nfree(p);
569                         return l;
570                 }
571
ragge
1.3
572                 o = l->n_op;
ragge
1.4
573                 m = p->n_type;
ragge
1.1
574
575                 if (o == ICON) {
576                         CONSZ val = l->n_lval;
577
ragge
1.29
578                         if (!ISPTR(m)) /* Pointers don't need to be conv'd */
579                             switch (m) {
ragge
1.52
580                         case BOOL:
581                                 l->n_lval = l->n_lval != 0;
582                                 break;
ragge
1.1
583                         case CHAR:
584                                 l->n_lval = (char)val;
585                                 break;
586                         case UCHAR:
587                                 l->n_lval = val & 0377;
588                                 break;
ragge
1.15
589                         case SHORT:
ragge
1.1
590                                 l->n_lval = (short)val;
591                                 break;
ragge
1.15
592                         case USHORT:
ragge
1.1
593                                 l->n_lval = val & 0177777;
594                                 break;
ragge
1.15
595                         case ULONG:
ragge
1.1
596                         case UNSIGNED:
597                                 l->n_lval = val & 0xffffffff;
598                                 break;
ragge
1.15
599                         case LONG:
ragge
1.1
600                         case INT:
601                                 l->n_lval = (int)val;
602                                 break;
603                         case LONGLONG:
604                                 l->n_lval = (long long)val;
ragge
1.15
605                                 break;
ragge
1.1
606                         case ULONGLONG:
607                                 l->n_lval = val;
608                                 break;
609                         case VOID:
610                                 break;
ragge
1.14
611                         case LDOUBLE:
ragge
1.1
612                         case DOUBLE:
613                         case FLOAT:
614                                 l->n_op = FCON;
615                                 l->n_dcon = val;
616                                 break;
617                         default:
618                                 cerror("unknown type %d"m);
619                         }
620                         l->n_type = m;
ragge
1.44
621                         l->n_sue = MKSUE(m);
ragge
1.1
622                         nfree(p);
623                         return l;
ragge
1.72
624                 } else if (l->n_op == FCON) {
625                         l->n_lval = l->n_dcon;
626                         l->n_sp = NULL;
627                         l->n_op = ICON;
628                         l->n_type = m;
629                         l->n_sue = MKSUE(m);
630                         nfree(p);
631                         return clocal(l);
ragge
1.1
632                 }
ragge
1.17
633                 if (DEUNSIGN(p->n_type) == SHORT &&
634                     DEUNSIGN(l->n_type) == SHORT) {
635                         nfree(p);
636                         p = l;
637                 }
ragge
1.36
638                 if ((p->n_type == CHAR || p->n_type == UCHAR ||
639                     p->n_type == SHORT || p->n_type == USHORT) &&
640                     (l->n_type == FLOAT || l->n_type == DOUBLE ||
641                     l->n_type == LDOUBLE)) {
642                         p = block(SCONVpNILp->n_typep->n_dfp->n_sue);
643                         p->n_left->n_type = INT;
644                         return p;
645                 }
ragge
1.1
646                 break;
647
ragge
1.15
648         case MOD:
649         case DIV:
650                 if (o == DIV && p->n_type != CHAR && p->n_type != SHORT)
651                         break;
ragge
1.16
652                 if (o == MOD && p->n_type != CHAR && p->n_type != SHORT)
ragge
1.15
653                         break;
654                 /* make it an int division by inserting conversions */
655                 p->n_left = block(SCONVp->n_leftNILINT0MKSUE(INT));
656                 p->n_right = block(SCONVp->n_rightNILINT0MKSUE(INT));
657                 p = block(SCONVpNILp->n_type0MKSUE(p->n_type));
658                 p->n_left->n_type = INT;
659                 break;
660
ragge
1.1
661         case PMCONV:
662         case PVCONV:
ragge
1.74
663                 r = p;
664                 p = buildtree(o == PMCONV ? MUL : DIVp->n_leftp->n_right);
665                 nfree(r);
666                 break;
ragge
1.1
667
ragge
1.31
668         case FORCE:
ragge
1.32
669                 /* put return value in return reg */
670                 p->n_op = ASSIGN;
671                 p->n_right = p->n_left;
672                 p->n_left = block(REGNILNILp->n_type0MKSUE(INT));
ragge
1.53
673                 p->n_left->n_rval = p->n_left->n_type == BOOL ? 
674                     RETREG(CHAR) : RETREG(p->n_type);
ragge
1.31
675                 break;
ragge
1.39
676
677         case LS:
678         case RS:
679                 /* shift count must be in a char
680                  * unless longlong, where it must be int */
681                 if (p->n_type == LONGLONG || p->n_type == ULONGLONG) {
682                         if (p->n_right->n_type != INT)
683                                 p->n_right = block(SCONVp->n_rightNIL,
684                                     INT0MKSUE(INT));
685                         break;
686                 }
687                 if (p->n_right->n_type == CHAR || p->n_right->n_type == UCHAR)
688                         break;
689                 p->n_right = block(SCONVp->n_rightNIL,
690                     CHAR0MKSUE(CHAR));
691                 break;
ragge
1.1
692         }
ragge
1.54
693 #ifdef PCC_DEBUG
694         if (xdebug) {
695                 printf("clocal end: %p\n"p);
696                 fwalk(peprint0);
697         }
698 #endif
ragge
1.1
699         return(p);
700 }
701
ragge
1.64
702 /*
703  * Change CALL references to either direct (static) or PLT.
704  */
705 static void
ragge
1.88
706 fixnames(NODE *pvoid *arg)
ragge
1.64
707 {
gmcgarry
1.86
708 #if !defined(PECOFFABI)
709
ragge
1.64
710         struct symtab *sp;
ragge
1.65
711         struct suedef *sue;
ragge
1.64
712         NODE *q;
713         char *c;
714         int isu;
715
716         if ((cdope(p->n_op) & CALLFLG) == 0)
717                 return;
718         isu = 0;
719         q = p->n_left;
ragge
1.65
720         sue = q->n_sue;
ragge
1.64
721         if (q->n_op == UMUL)
722                 q = q->n_leftisu = 1;
gmcgarry
1.83
723
ragge
1.64
724         if (q->n_op == PLUS && q->n_left->n_op == TEMP &&
725             q->n_right->n_op == ICON) {
726                 sp = q->n_right->n_sp;
727
ragge
1.65
728                 if (sp == NULL)
729                         return/* nothing to do */
ragge
1.64
730                 if (sp->sclass == STATIC && !ISFTN(sp->stype))
731                         return/* function pointer */
732
733                 if (sp->sclass != STATIC && sp->sclass != EXTERN &&
734                     sp->sclass != EXTDEF)
735                         cerror("fixnames");
ragge
1.96
736                 c = NULL;
gmcgarry
1.83
737 #if defined(ELFABI)
738
ragge
1.96
739                 if (sp->soname == NULL ||
740                     (c = strstr(sp->soname"@GOT")) == NULL)
ragge
1.64
741                         cerror("fixnames2");
742                 if (isu) {
743                         memcpy(c"@PLT"sizeof("@PLT"));
744                 } else
745                         *c = 0;
gmcgarry
1.83
746
747 #elif defined(MACHOABI)
748
ragge
1.96
749                 if (sp->soname == NULL ||
750                     ((c = strstr(sp->soname"$non_lazy_ptr")) == NULL &&
751                     (c = strstr(sp->soname"-L")) == NULL))
gmcgarry
1.83
752                                 cerror("fixnames2");
753                 if (isu) {
754                         *c = 0;
755                         addstub(&stublistsp->soname+1);
756                         strcpy(c"$stub");
757                 } else 
758                         *c = 0;
759
760 #endif
761
ragge
1.64
762                 nfree(q->n_left);
763                 q = q->n_right;
764                 if (isu)
765                         nfree(p->n_left->n_left);
766                 nfree(p->n_left);
767                 p->n_left = q;
ragge
1.65
768                 q->n_sue = sue;
ragge
1.64
769         }
gmcgarry
1.86
770 #endif
ragge
1.64
771 }
772
ragge
1.1
773 void
774 myp2tree(NODE *p)
775 {
ragge
1.67
776         struct symtab *sp;
777
ragge
1.64
778         if (kflag)
ragge
1.88
779                 walkf(pfixnames0); /* XXX walkf not needed */
ragge
1.67
780         if (p->n_op != FCON)
781                 return;
782
ragge
1.70
783 #if 0
ragge
1.67
784         /* put floating constants in memory */
785         setloc1(RDATA);
786         defalign(ALLDOUBLE);
787         deflab1(i = getlab());
788         ninval(0btdims[p->n_type].suesizep);
ragge
1.70
789 #endif
ragge
1.67
790
791         sp = IALLOC(sizeof(struct symtab));
792         sp->sclass = STATIC;
gmcgarry
1.75
793         sp->ssue = MKSUE(p->n_type);
ragge
1.67
794         sp->slevel = 1/* fake numeric label */
ragge
1.70
795         sp->soffset = getlab();
ragge
1.67
796         sp->sflags = 0;
ragge
1.70
797         sp->stype = p->n_type;
798         sp->squal = (CON >> TSHIFT);
799
800         defloc(sp);
gmcgarry
1.75
801         ninval(0sp->ssue->suesizep);
ragge
1.67
802
803         p->n_op = NAME;
804         p->n_lval = 0;
805         p->n_sp = sp;
ragge
1.1
806 }
807
808 /*ARGSUSED*/
809 int
810 andable(NODE *p)
811 {
ragge
1.64
812         return(1);      /* all names can have & taken on them */
ragge
1.1
813 }
814
815 /*
816  * at the end of the arguments of a ftn, set the automatic offset
817  */
818 void
819 cendarg()
820 {
821         autooff = AUTOINIT;
822 }
823
824 /*
ragge
1.36
825  * Return 1 if a variable of type type is OK to put in register.
826  */
827 int
828 cisreg(TWORD t)
829 {
830         if (t == FLOAT || t == DOUBLE || t == LDOUBLE)
831                 return 0/* not yet */
832         return 1;
833 }
834
835 /*
ragge
1.1
836  * return a node, for structure references, which is suitable for
837  * being added to a pointer of type t, in order to be off bits offset
838  * into a structure
839  * t, d, and s are the type, dimension offset, and sizeoffset
840  * For pdp10, return the type-specific index number which calculation
841  * is based on its size. For example, short a[3] would return 3.
842  * Be careful about only handling first-level pointers, the following
843  * indirections must be fullword.
844  */
845 NODE *
846 offcon(OFFSZ offTWORD tunion dimfun *dstruct suedef *sue)
847 {
848         register NODE *p;
849
850         if (xdebug)
851                 printf("offcon: OFFSZ %lld type %x dim %p siz %d\n",
852                     offtdsue->suesize);
853
854         p = bcon(0);
855         p->n_lval = off/SZCHAR/* Default */
856         return(p);
857 }
858
859 /*
860  * Allocate off bits on the stack.  p is a tree that when evaluated
ragge
1.45
861  * is the multiply count for off, t is a storeable node where to write
ragge
1.1
862  * the allocated address.
863  */
864 void
865 spalloc(NODE *tNODE *pOFFSZ off)
866 {
867         NODE *sp;
868
ragge
1.45
869         p = buildtree(MULpbcon(off/SZCHAR)); /* XXX word alignment? */
870
871         /* sub the size from sp */
ragge
1.55
872         sp = block(REGNILNILp->n_type0MKSUE(INT));
ragge
1.45
873         sp->n_lval = 0;
874         sp->n_rval = STKREG;
875         ecomp(buildtree(MINUSEQspp));
ragge
1.1
876
pantzer
1.100
877 #ifdef MACHOABI 
878         /* align to 16 bytes */
879         sp = block(REGNILNILp->n_type0MKSUE(INT));
880         sp->n_lval = 0;
881         sp->n_rval = STKREG;
882         ecomp(buildtree(PLUSEQspbcon(15)));
883         
884         sp = block(REGNILNILp->n_type0MKSUE(INT));
885         sp->n_lval = 0;
886         sp->n_rval = STKREG;
887         ecomp(buildtree(RSEQspbcon(4)));
888         
889         sp = block(REGNILNILp->n_type0MKSUE(INT));
890         sp->n_lval = 0;
891         sp->n_rval = STKREG;
892         ecomp(buildtree(LSEQspbcon(4)));
893 #endif
894         
895
ragge
1.1
896         /* save the address of sp */
897         sp = block(REGNILNILPTR+INTt->n_dft->n_sue);
898         sp->n_lval = 0;
899         sp->n_rval = STKREG;
900         t->n_type = sp->n_type;
901         ecomp(buildtree(ASSIGNtsp)); /* Emit! */
902
903 }
904
905 /*
ragge
1.48
906  * Print out a string of characters.
907  * Assume that the assembler understands C-style escape
ragge
1.70
908  * sequences.
ragge
1.48
909  */
910 void
ragge
1.70
911 instring(struct symtab *sp)
ragge
1.48
912 {
gmcgarry
1.83
913         char *s, *str = sp->sname;
914
gmcgarry
1.86
915 #if defined(ELFABI) || defined(PECOFFABI)
ragge
1.70
916
917         defloc(sp);
gmcgarry
1.83
918
919 #elif defined(MACHOABI)
920
921         extern int lastloc;
922         if (lastloc != STRNG)
923                 printf("        .cstring\n");
924         lastloc = STRNG;
925         printf("\t.p2align 2\n");
926         printf(LABFMT ":\n"sp->soffset);
927
928 #endif
ragge
1.48
929
mickey
1.61
930         /* be kind to assemblers and avoid long strings */
ragge
1.48
931         printf("\t.ascii \"");
ragge
1.51
932         for (s = str; *s != 0; ) {
933                 if (*s++ == '\\') {
ragge
1.48
934                         (void)esccon(&s);
ragge
1.51
935                 }
ragge
1.70
936                 if (s - str > 60) {
ragge
1.48
937                         fwrite(str1s - strstdout);
938                         printf("\"\n\t.ascii \"");
939                         str = s;
940                 }
941         }
942         fwrite(str1s - strstdout);
943         printf("\\0\"\n");
944 }
945
ragge
1.70
946 /*
947  * Print out a wide string by calling ninval().
948  */
949 void
950 inwstring(struct symtab *sp)
951 {
952         char *s = sp->sname;
953         NODE *p;
954
955         defloc(sp);
gmcgarry
1.84
956         p = xbcon(0NULLWCHAR_TYPE);
ragge
1.70
957         do {
958                 if (*s++ == '\\')
959                         p->n_lval = esccon(&s);
960                 else
961                         p->n_lval = (unsigned char)s[-1];
962                 ninval(0, (MKSUE(WCHAR_TYPE))->suesizep);
963         } while (s[-1] != 0);
964         nfree(p);
965 }
966
967
ragge
1.49
968 static int inbitsinval;
969
970 /*
971  * set fsz bits in sequence to zero.
972  */
973 void
974 zbits(OFFSZ offint fsz)
975 {
976         int m;
977
978         if (idebug)
ragge
1.51