Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:gmcgarry:20081223065121

Diff

Diff from 1.95 to:

Annotations

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

Annotated File View

gmcgarry
1.95
1 /*      $Id: local.c,v 1.95 2008/12/23 06:51:21 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.94
178         if (p->n_sp->slevel > 0) {
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
ragge
1.94
201         if (p->n_sp->slevel > 0) {
gmcgarry
1.83
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;
387                 }
ragge
1.1
388                 break;
389
ragge
1.65
390         case ADDROF:
391                 if (kflag == 0 || blevel == 0)
392                         break;
393                 /* char arrays may end up here */
394                 l = p->n_left;
395                 if (l->n_op != NAME ||
396                     (l->n_type != ARY+CHAR && l->n_type != ARY+WCHAR_TYPE))
397                         break;
398                 l = p;
399                 p = picstatic(p->n_left);
400                 nfree(l);
401                 if (p->n_op != UMUL)
402                         cerror("ADDROF error");
403                 l = p;
404                 p = p->n_left;
405                 nfree(l);
406                 break;
407
ragge
1.62
408         case UCALL:
409         case USTCALL:
ragge
1.60
410                 if (kflag == 0)
411                         break;
gmcgarry
1.86
412 #if defined(ELFABI)
ragge
1.62
413                 /* Change to CALL node with ebx as argument */
414                 l = block(REGNILNILINT0MKSUE(INT));
415                 l->n_rval = EBX;
416                 p->n_right = buildtree(ASSIGNl,
417                     tempnode(gotnrINT0MKSUE(INT)));
418                 p->n_op -= (UCALL-CALL);
gmcgarry
1.86
419 #endif
pantzer
1.89
420         
421         /* FALLTHROUGH */
422 #if defined(MACHOABI)
423         case CALL:
424         case STCALL:
425                 if (p->n_type == VOID)
426                         break;
427
428                 r = tempnode(0p->n_typep->n_dfp->n_sue);
429                 l = tcopy(r);
430                 p = buildtree(COMOPbuildtree(ASSIGNrp), l);
431 #endif
432                         
ragge
1.10
433                 break;
ragge
1.64
434
ragge
1.8
435         case CBRANCH:
436                 l = p->n_left;
437
438                 /*
ragge
1.77
439                  * Remove unnecessary conversion ops.
ragge
1.8
440                  */
ragge
1.10
441                 if (clogop(l->n_op) && l->n_left->n_op == SCONV) {
ragge
1.12
442                         if (coptype(l->n_op) != BITYPE)
443                                 break;
ragge
1.8
444                         if (l->n_right->n_op == ICON) {
445                                 r = l->n_left->n_left;
ragge
1.24
446                                 if (r->n_type >= FLOAT && r->n_type <= LDOUBLE)
447                                         break;
ragge
1.59
448                                 if (ISPTR(r->n_type))
449                                         break/* no opt for pointers */
ragge
1.57
450                                 if (toolarge(r->n_typel->n_right->n_lval))
451                                         break;
ragge
1.18
452                                 /* Type must be correct */
ragge
1.19
453                                 t = r->n_type;
ragge
1.8
454                                 nfree(l->n_left);
455                                 l->n_left = r;
ragge
1.19
456                                 l->n_type = t;
457                                 l->n_right->n_type = t;
ragge
1.17
458                         }
ragge
1.8
459                 }
460                 break;
461
ragge
1.1
462         case PCONV:
ragge
1.40
463                 /* Remove redundant PCONV's. Be careful */
ragge
1.1
464                 l = p->n_left;
ragge
1.40
465                 if (l->n_op == ICON) {
466                         l->n_lval = (unsigned)l->n_lval;
467                         goto delp;
468                 }
469                 if (l->n_type < INT || l->n_type == LONGLONG || 
470                     l->n_type == ULONGLONG) {
471                         /* float etc? */
472                         p->n_left = block(SCONVlNIL,
473                             UNSIGNED0MKSUE(UNSIGNED));
474                         break;
475                 }
476                 /* if left is SCONV, cannot remove */
477                 if (l->n_op == SCONV)
ragge
1.1
478                         break;
ragge
1.54
479
480                 /* avoid ADDROF TEMP */
481                 if (l->n_op == ADDROF && l->n_left->n_op == TEMP)
482                         break;
483
ragge
1.40
484                 /* if conversion to another pointer type, just remove */
485                 if (p->n_type > BTMASK && l->n_type > BTMASK)
486                         goto delp;
487                 break;
488
489         delp:   l->n_type = p->n_type;
ragge
1.1
490                 l->n_qual = p->n_qual;
491                 l->n_df = p->n_df;
492                 l->n_sue = p->n_sue;
493                 nfree(p);
494                 p = l;
495                 break;
ragge
1.40
496                 
ragge
1.1
497         case SCONV:
498                 l = p->n_left;
ragge
1.4
499
ragge
1.25
500                 if (p->n_type == l->n_type) {
501                         nfree(p);
502                         return l;
503                 }
504
ragge
1.4
505                 if ((p->n_type & TMASK) == 0 && (l->n_type & TMASK) == 0 &&
pj
1.30
506                     btdims[p->n_type].suesize == btdims[l->n_type].suesize) {
ragge
1.4
507                         if (p->n_type != FLOAT && p->n_type != DOUBLE &&
ragge
1.15
508                             l->n_type != FLOAT && l->n_type != DOUBLE &&
509                             l->n_type != LDOUBLE && p->n_type != LDOUBLE) {
ragge
1.41
510                                 if (l->n_op == NAME || l->n_op == UMUL ||
511                                     l->n_op == TEMP) {
ragge
1.15
512                                         l->n_type = p->n_type;
513                                         nfree(p);
514                                         return l;
515                                 }
ragge
1.4
516                         }
ragge
1.6
517                 }
518
ragge
1.42
519                 if (DEUNSIGN(p->n_type) == INT && DEUNSIGN(l->n_type) == INT &&
520                     coptype(l->n_op) == BITYPE) {
521                         l->n_type = p->n_type;
522                         nfree(p);
523                         return l;
524                 }
525
ragge
1.3
526                 o = l->n_op;
ragge
1.4
527                 m = p->n_type;
ragge
1.1
528
529                 if (o == ICON) {
530                         CONSZ val = l->n_lval;
531
ragge
1.29
532                         if (!ISPTR(m)) /* Pointers don't need to be conv'd */
533                             switch (m) {
ragge
1.52
534                         case BOOL:
535                                 l->n_lval = l->n_lval != 0;
536                                 break;
ragge
1.1
537                         case CHAR:
538                                 l->n_lval = (char)val;
539                                 break;
540                         case UCHAR:
541                                 l->n_lval = val & 0377;
542                                 break;
ragge
1.15
543                         case SHORT:
ragge
1.1
544                                 l->n_lval = (short)val;
545                                 break;
ragge
1.15
546                         case USHORT:
ragge
1.1
547                                 l->n_lval = val & 0177777;
548                                 break;
ragge
1.15
549                         case ULONG:
ragge
1.1
550                         case UNSIGNED:
551                                 l->n_lval = val & 0xffffffff;
552                                 break;
ragge
1.15
553                         case LONG:
ragge
1.1
554                         case INT:
555                                 l->n_lval = (int)val;
556                                 break;
557                         case LONGLONG:
558                                 l->n_lval = (long long)val;
ragge
1.15
559                                 break;
ragge
1.1
560                         case ULONGLONG:
561                                 l->n_lval = val;
562                                 break;
563                         case VOID:
564                                 break;
ragge
1.14
565                         case LDOUBLE:
ragge
1.1
566                         case DOUBLE:
567                         case FLOAT:
568                                 l->n_op = FCON;
569                                 l->n_dcon = val;
570                                 break;
571                         default:
572                                 cerror("unknown type %d"m);
573                         }
574                         l->n_type = m;
ragge
1.44
575                         l->n_sue = MKSUE(m);
ragge
1.1
576                         nfree(p);
577                         return l;
ragge
1.72
578                 } else if (l->n_op == FCON) {
579                         l->n_lval = l->n_dcon;
580                         l->n_sp = NULL;
581                         l->n_op = ICON;
582                         l->n_type = m;
583                         l->n_sue = MKSUE(m);
584                         nfree(p);
585                         return clocal(l);
ragge
1.1
586                 }
ragge
1.17
587                 if (DEUNSIGN(p->n_type) == SHORT &&
588                     DEUNSIGN(l->n_type) == SHORT) {
589                         nfree(p);
590                         p = l;
591                 }
ragge
1.36
592                 if ((p->n_type == CHAR || p->n_type == UCHAR ||
593                     p->n_type == SHORT || p->n_type == USHORT) &&
594                     (l->n_type == FLOAT || l->n_type == DOUBLE ||
595                     l->n_type == LDOUBLE)) {
596                         p = block(SCONVpNILp->n_typep->n_dfp->n_sue);
597                         p->n_left->n_type = INT;
598                         return p;
599                 }
ragge
1.1
600                 break;
601
ragge
1.15
602         case MOD:
603         case DIV:
604                 if (o == DIV && p->n_type != CHAR && p->n_type != SHORT)
605                         break;
ragge
1.16
606                 if (o == MOD && p->n_type != CHAR && p->n_type != SHORT)
ragge
1.15
607                         break;
608                 /* make it an int division by inserting conversions */
609                 p->n_left = block(SCONVp->n_leftNILINT0MKSUE(INT));
610                 p->n_right = block(SCONVp->n_rightNILINT0MKSUE(INT));
611                 p = block(SCONVpNILp->n_type0MKSUE(p->n_type));
612                 p->n_left->n_type = INT;
613                 break;
614
ragge
1.1
615         case PMCONV:
616         case PVCONV:
ragge
1.74
617                 r = p;
618                 p = buildtree(o == PMCONV ? MUL : DIVp->n_leftp->n_right);
619                 nfree(r);
620                 break;
ragge
1.1
621
ragge
1.31
622         case FORCE:
ragge
1.32
623                 /* put return value in return reg */
624                 p->n_op = ASSIGN;
625                 p->n_right = p->n_left;
626                 p->n_left = block(REGNILNILp->n_type0MKSUE(INT));
ragge
1.53
627                 p->n_left->n_rval = p->n_left->n_type == BOOL ? 
628                     RETREG(CHAR) : RETREG(p->n_type);
ragge
1.31
629                 break;
ragge
1.39
630
631         case LS:
632         case RS:
633                 /* shift count must be in a char
634                  * unless longlong, where it must be int */
635                 if (p->n_type == LONGLONG || p->n_type == ULONGLONG) {
636                         if (p->n_right->n_type != INT)
637                                 p->n_right = block(SCONVp->n_rightNIL,
638                                     INT0MKSUE(INT));
639                         break;
640                 }
641                 if (p->n_right->n_type == CHAR || p->n_right->n_type == UCHAR)
642                         break;
643                 p->n_right = block(SCONVp->n_rightNIL,
644                     CHAR0MKSUE(CHAR));
645                 break;
ragge
1.1
646         }
ragge
1.54
647 #ifdef PCC_DEBUG
648         if (xdebug) {
649                 printf("clocal end: %p\n"p);
650                 fwalk(peprint0);
651         }
652 #endif
ragge
1.1
653         return(p);
654 }
655
ragge
1.64
656 /*
657  * Change CALL references to either direct (static) or PLT.
658  */
659 static void
ragge
1.88
660 fixnames(NODE *pvoid *arg)
ragge
1.64
661 {
gmcgarry
1.86
662 #if !defined(PECOFFABI)
663
ragge
1.64
664         struct symtab *sp;
ragge
1.65
665         struct suedef *sue;
ragge
1.64
666         NODE *q;
667         char *c;
668         int isu;
669
670         if ((cdope(p->n_op) & CALLFLG) == 0)
671                 return;
672         isu = 0;
673         q = p->n_left;
ragge
1.65
674         sue = q->n_sue;
ragge
1.64
675         if (q->n_op == UMUL)
676                 q = q->n_leftisu = 1;
gmcgarry
1.83
677
ragge
1.64
678         if (q->n_op == PLUS && q->n_left->n_op == TEMP &&
679             q->n_right->n_op == ICON) {
680                 sp = q->n_right->n_sp;
681
ragge
1.65
682                 if (sp == NULL)
683                         return/* nothing to do */
ragge
1.64
684                 if (sp->sclass == STATIC && !ISFTN(sp->stype))
685                         return/* function pointer */
686
687                 if (sp->sclass != STATIC && sp->sclass != EXTERN &&
688                     sp->sclass != EXTDEF)
689                         cerror("fixnames");
690
gmcgarry
1.83
691 #if defined(ELFABI)
692
ragge
1.69
693                 if ((c = strstr(sp->soname"@GOT")) == NULL)
ragge
1.64
694                         cerror("fixnames2");
695                 if (isu) {
696                         memcpy(c"@PLT"sizeof("@PLT"));
697                 } else
698                         *c = 0;
gmcgarry
1.83
699
700 #elif defined(MACHOABI)
701
702                 if ((c = strstr(sp->soname"$non_lazy_ptr")) == NULL &&
703                     (c = strstr(sp->soname"-L")) == NULL)
704                                 cerror("fixnames2");
705                 if (isu) {
706                         *c = 0;
707                         addstub(&stublistsp->soname+1);
708                         strcpy(c"$stub");
709                 } else 
710                         *c = 0;
711
712 #endif
713
ragge
1.64
714                 nfree(q->n_left);
715                 q = q->n_right;
716                 if (isu)
717                         nfree(p->n_left->n_left);
718                 nfree(p->n_left);
719                 p->n_left = q;
ragge
1.65
720                 q->n_sue = sue;
ragge
1.64
721         }
gmcgarry
1.86
722 #endif
ragge
1.64
723 }
724
ragge
1.1
725 void
726 myp2tree(NODE *p)
727 {
ragge
1.67
728         struct symtab *sp;
729
ragge
1.64
730         if (kflag)
ragge
1.88
731                 walkf(pfixnames0); /* XXX walkf not needed */
ragge
1.67
732         if (p->n_op != FCON)
733                 return;
734
ragge
1.70
735 #if 0
ragge
1.67
736         /* put floating constants in memory */
737         setloc1(RDATA);
738         defalign(ALLDOUBLE);
739         deflab1(i = getlab());
740         ninval(0btdims[p->n_type].suesizep);
ragge
1.70
741 #endif
ragge
1.67
742
743         sp = IALLOC(sizeof(struct symtab));
744         sp->sclass = STATIC;
gmcgarry
1.75
745         sp->ssue = MKSUE(p->n_type);
ragge
1.67
746         sp->slevel = 1/* fake numeric label */
ragge
1.70
747         sp->soffset = getlab();
ragge
1.67
748         sp->sflags = 0;
ragge
1.70
749         sp->stype = p->n_type;
750         sp->squal = (CON >> TSHIFT);
751
752         defloc(sp);
gmcgarry
1.75
753         ninval(0sp->ssue->suesizep);
ragge
1.67
754
755         p->n_op = NAME;
756         p->n_lval = 0;
757         p->n_sp = sp;
ragge
1.1
758 }
759
760 /*ARGSUSED*/
761 int
762 andable(NODE *p)
763 {
ragge
1.64
764         return(1);      /* all names can have & taken on them */
ragge
1.1
765 }
766
767 /*
768  * at the end of the arguments of a ftn, set the automatic offset
769  */
770 void
771 cendarg()
772 {
773         autooff = AUTOINIT;
774 }
775
776 /*
ragge
1.36
777  * Return 1 if a variable of type type is OK to put in register.
778  */
779 int
780 cisreg(TWORD t)
781 {
782         if (t == FLOAT || t == DOUBLE || t == LDOUBLE)
783                 return 0/* not yet */
784         return 1;
785 }
786
787 /*
ragge
1.1
788  * return a node, for structure references, which is suitable for
789  * being added to a pointer of type t, in order to be off bits offset
790  * into a structure
791  * t, d, and s are the type, dimension offset, and sizeoffset
792  * For pdp10, return the type-specific index number which calculation
793  * is based on its size. For example, short a[3] would return 3.
794  * Be careful about only handling first-level pointers, the following
795  * indirections must be fullword.
796  */
797 NODE *
798 offcon(OFFSZ offTWORD tunion dimfun *dstruct suedef *sue)
799 {
800         register NODE *p;
801
802         if (xdebug)
803                 printf("offcon: OFFSZ %lld type %x dim %p siz %d\n",
804                     offtdsue->suesize);
805
806         p = bcon(0);
807         p->n_lval = off/SZCHAR/* Default */
808         return(p);
809 }
810
811 /*
812  * Allocate off bits on the stack.  p is a tree that when evaluated
ragge
1.45
813  * is the multiply count for off, t is a storeable node where to write
ragge
1.1
814  * the allocated address.
815  */
816 void
817 spalloc(NODE *tNODE *pOFFSZ off)
818 {
819         NODE *sp;
820
ragge
1.45
821         p = buildtree(MULpbcon(off/SZCHAR)); /* XXX word alignment? */
822
823         /* sub the size from sp */
ragge
1.55
824         sp = block(REGNILNILp->n_type0MKSUE(INT));
ragge
1.45
825         sp->n_lval = 0;
826         sp->n_rval = STKREG;
827         ecomp(buildtree(MINUSEQspp));
ragge
1.1
828
829         /* save the address of sp */
830         sp = block(REGNILNILPTR+INTt->n_dft->n_sue);
831         sp->n_lval = 0;
832         sp->n_rval = STKREG;
833         t->n_type = sp->n_type;
834         ecomp(buildtree(ASSIGNtsp)); /* Emit! */
835
836 }
837
838 /*
ragge
1.48
839  * Print out a string of characters.
840  * Assume that the assembler understands C-style escape
ragge
1.70
841  * sequences.
ragge
1.48
842  */
843 void
ragge
1.70
844 instring(struct symtab *sp)
ragge
1.48
845 {
gmcgarry
1.83
846         char *s, *str = sp->sname;
847
gmcgarry
1.86
848 #if defined(ELFABI) || defined(PECOFFABI)
ragge
1.70
849
850         defloc(sp);
gmcgarry
1.83
851
852 #elif defined(MACHOABI)
853
854         extern int lastloc;
855         if (lastloc != STRNG)
856                 printf("        .cstring\n");
857         lastloc = STRNG;
858         printf("\t.p2align 2\n");
859         printf(LABFMT ":\n"sp->soffset);
860
861 #endif
ragge
1.48
862
mickey
1.61
863         /* be kind to assemblers and avoid long strings */
ragge
1.48
864         printf("\t.ascii \"");
ragge
1.51
865         for (s = str; *s != 0; ) {
866                 if (*s++ == '\\') {
ragge
1.48
867                         (void)esccon(&s);
ragge
1.51
868                 }
ragge
1.70
869                 if (s - str > 60) {
ragge
1.48
870                         fwrite(str1s - strstdout);
871                         printf("\"\n\t.ascii \"");
872                         str = s;
873                 }
874         }
875         fwrite(str1s - strstdout);
876         printf("\\0\"\n");
877 }
878
ragge
1.70
879 /*
880  * Print out a wide string by calling ninval().
881  */
882 void
883 inwstring(struct symtab *sp)
884 {
885         char *s = sp->sname;
886         NODE *p;
887
888         defloc(sp);
gmcgarry
1.84
889         p = xbcon(0NULLWCHAR_TYPE);
ragge
1.70
890         do {
891                 if (*s++ == '\\')
892                         p->n_lval = esccon(&s);
893                 else
894                         p->n_lval = (unsigned char)s[-1];
895                 ninval(0, (MKSUE(WCHAR_TYPE))->suesizep);
896         } while (s[-1] != 0);
897         nfree(p);
898 }
899
900
ragge
1.49
901 static int inbitsinval;
902
903 /*
904  * set fsz bits in sequence to zero.
905  */
906 void
907 zbits(OFFSZ offint fsz)
908 {
909         int m;
910
911         if (idebug)
ragge
1.51
912                 printf("zbits off %lld, fsz %d inbits %d\n"offfszinbits);
ragge
1.49
913         if ((m = (inbits % SZCHAR))) {
914                 m = SZCHAR - m;
915                 if (fsz < m) {
ragge
1.51
916                         inbits += fsz;
ragge
1.49
917                         return;
918                 } else {
919                         fsz -= m;
920                         printf("\t.byte %d\n"inval);
921                         inval = inbits = 0;
922                 }
923         }
924         if (fsz >= SZCHAR) {
925                 printf("\t.zero %d\n"fsz/SZCHAR);
ragge
1.51
926                 fsz -= (fsz/SZCHAR) * SZCHAR;
ragge
1.49
927         }
928         if (fsz) {
929                 inval = 0;
930                 inbits = fsz;
931         }
932 }
933
934 /*
935  * Initialize a bitfield.
936  */
937 void
938 infld(CONSZ offint fszCONSZ val)
939 {
940         if (idebug)
ragge
1.51
941                 printf("infld off %lld, fsz %d, val %lld inbits %d\n",
942                     offfszvalinbits);
ragge
1.56
943         val &= ((CONSZ)1 << fsz)-1;
ragge
1.49
944         while (fsz + inbits >= SZCHAR) {
945                 inval |= (val << inbits);
946                 printf("\t.byte %d\n"inval & 255);
947                 fsz -= (SZCHAR - inbits);
948                 val >>= (SZCHAR - inbits);
949                 inval = inbits = 0;
950         }
ragge
1.51
951         if (fsz) {
952                 inval |= (val << inbits);
953                 inbits += fsz;
954         }
ragge
1.49
955 }
956
ragge
1.48
957 /*
ragge
1.46
958  * print out a constant node, may be associated with a label.
959  * Do not free the node after use.
ragge
1.49
960  * off is bit offset from the beginning of the aggregate
961  * fsz is the number of bits this is referring to
ragge
1.21
962  */
963 void
ragge
1.49
964 ninval(CONSZ offint fszNODE *p)
ragge
1.21
965 {
ragge
1.46
966         union { float fdouble dlong double lint i[3]; } u;
ragge
1.24
967         struct symtab *q;
gmcgarry
1.86
968 #if defined(ELFABI) || defined(MACHOABI)
ragge
1.64
969         char *c;
gmcgarry
1.86
970 #endif
ragge
1.23
971         TWORD t;
ragge
1.46
972         int i;
ragge
1.23
973
974         t = p->n_type;
975         if (t > BTMASK)
976                 t = INT/* pointer */
977
ragge
1.65
978         while (p->n_op == SCONV || p->n_op == PCONV) {
979                 NODE *l = p->n_left;
980                 l->n_type = p->n_type;
981