Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:gmcgarry:20081201074320

Diff

Diff from 1.92 to:

Annotations

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

Annotated File View

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