Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20081116133016

Diff

Diff from 1.88 to:

Annotations

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

Annotated File View

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