Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:gmcgarry:20090328223326

Diff

Diff from 1.104 to:

Annotations

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

Annotated File View

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