Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:gmcgarry:20090907080633

Diff

Diff from 1.110 to:

Annotations

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

Annotated File View

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