Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20100428145354

Diff

Diff from 1.115 to:

Annotations

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

Annotated File View

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