Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:plunky:20120906130728

Diff

Diff from 1.166 to:

Annotations

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

Annotated File View

plunky
1.166
1 /*      $Id: local.c,v 1.166 2012/09/06 13:07:28 plunky 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  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27
ragge
1.48
28 #include "pass1.h"
ragge
1.1
29
ragge
1.60
30 /*      this file contains code which is dependent on the target machine */
31
ragge
1.130
32 #ifdef notyet
ragge
1.57
33 /*
34  * Check if a constant is too large for a type.
35  */
36 static int
37 toolarge(TWORD tCONSZ con)
38 {
39         U_CONSZ ucon = con;
40
41         switch (t) {
42         case ULONGLONG:
43         case LONGLONG:
44                 break/* cannot be too large */
45 #define SCHK(i) case i: if (con > MAX_##i || con < MIN_##i) return 1; break
46 #define UCHK(i) case i: if (ucon > MAX_##i) return 1; break
47         SCHK(INT);
48         SCHK(SHORT);
otto
1.58
49         case BOOL:
ragge
1.57
50         SCHK(CHAR);
51         UCHK(UNSIGNED);
52         UCHK(USHORT);
53         UCHK(UCHAR);
54         default:
55                 cerror("toolarge");
56         }
57         return 0;
58 }
ragge
1.130
59 #endif
ragge
1.57
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));
plunky
1.166
78         s->name = newstring(namestrlen(name));
gmcgarry
1.83
79         DLIST_INSERT_BEFORE(listslink);
80 }
81
82 #endif
83
ragge
1.60
84 #define IALLOC(sz)      (isinlining ? permalloc(sz) : tmpalloc(sz))
gmcgarry
1.86
85
ragge
1.60
86 /*
87  * Make a symtab entry for PIC use.
88  */
89 static struct symtab *
gmcgarry
1.83
90 picsymtab(char *pchar *schar *s2)
ragge
1.60
91 {
92         struct symtab *sp = IALLOC(sizeof(struct symtab));
gmcgarry
1.83
93         size_t len = strlen(p) + strlen(s) + strlen(s2) + 1;
ragge
1.60
94         
ragge
1.69
95         sp->sname = sp->soname = IALLOC(len);
gmcgarry
1.83
96         strlcpy(sp->sonameplen);
97         strlcat(sp->sonameslen);
ragge
1.69
98         strlcat(sp->sonames2len);
ragge
1.123
99         sp->sap = NULL;
ragge
1.60
100         sp->sclass = EXTERN;
ragge
1.64
101         sp->sflags = sp->slevel = 0;
gmcgarry
1.158
102         sp->stype = 0xdeadbeef;
ragge
1.60
103         return sp;
104 }
gmcgarry
1.103
105
106 #ifdef os_win32
107 static NODE *
108 import(NODE *p)
109 {
110         NODE *q;
111         char *name;
112         struct symtab *sp;
113
114         if ((name = p->n_sp->soname) == NULL)
115                 name = exname(p->n_sp->sname);
116
117         sp = picsymtab("__imp_"name"");
118         q = xbcon(0spPTR+VOID);
ragge
1.138
119         q = block(UMULq0PTR|VOID00);
ragge
1.136
120         q = block(UMULq0p->n_typep->n_dfp->n_ap);
gmcgarry
1.103
121         q->n_sp = p->n_sp/* for init */
122         nfree(p);
123
124         return q;
125 }
gmcgarry
1.86
126 #endif
ragge
1.60
127
128 int gotnr/* tempnum for GOT register */
gmcgarry
1.86
129 int argstacksize;
130
ragge
1.60
131 /*
132  * Create a reference for an extern variable.
133  */
134 static NODE *
135 picext(NODE *p)
136 {
gmcgarry
1.86
137
138 #if defined(ELFABI)
139
ragge
1.60
140         NODE *q, *r;
stefan
1.66
141         struct symtab *sp;
ragge
1.96
142         char *name;
ragge
1.60
143
ragge
1.138
144         q = tempnode(gotnrPTR|VOID00);
ragge
1.96
145         if ((name = p->n_sp->soname) == NULL)
146                 name = p->n_sp->sname;
147         sp = picsymtab(""name"@GOT");
ragge
1.117
148 #ifdef GCC_COMPAT
ragge
1.123
149         if (attr_find(p->n_sp->sapGCC_ATYP_STDCALL) != NULL)
ragge
1.117
150                 p->n_sp->sflags |= SSTDCALL;
151 #endif
ragge
1.118
152         sp->sflags = p->n_sp->sflags & SSTDCALL;
stefan
1.66
153         r = xbcon(0spINT);
ragge
1.60
154         q = buildtree(PLUSqr);
ragge
1.138
155         q = block(UMULq0PTR|VOID00);
ragge
1.123
156         q = block(UMULq0p->n_typep->n_dfp->n_ap);
gmcgarry
1.86
157         q->n_sp = p->n_sp/* for init */
158         nfree(p);
159         return q;
gmcgarry
1.83
160
161 #elif defined(MACHOABI)
162
gmcgarry
1.86
163         NODE *q, *r;
164         struct symtab *sp;
gmcgarry
1.101
165         char buf2[256], *name, *pspn;
gmcgarry
1.83
166
ragge
1.96
167         if ((name = cftnsp->soname) == NULL)
168                 name = cftnsp->sname;
169         if ((pspn = p->n_sp->soname) == NULL)
pantzer
1.98
170                 pspn = exname(p->n_sp->sname);
gmcgarry
1.83
171         if (p->n_sp->sclass == EXTDEF) {
gmcgarry
1.101
172                 snprintf(buf2256"-L%s$pb"name);
pantzer
1.98
173                 sp = picsymtab(""pspnbuf2);
gmcgarry
1.83
174         } else {
gmcgarry
1.101
175                 snprintf(buf2256"$non_lazy_ptr-L%s$pb"name);
ragge
1.96
176                 sp = picsymtab("L"pspnbuf2);
177                 addstub(&nlplistpspn);
gmcgarry
1.83
178         }
gmcgarry
1.158
179
180         sp->stype = p->n_sp->stype;
181
ragge
1.138
182         q = tempnode(gotnrPTR+VOID00);
gmcgarry
1.83
183         r = xbcon(0spINT);
184         q = buildtree(PLUSqr);
185
186         if (p->n_sp->sclass != EXTDEF)
ragge
1.138
187                 q = block(UMULq0PTR+VOID00);
ragge
1.123
188         q = block(UMULq0p->n_typep->n_dfp->n_ap);
ragge
1.65
189         q->n_sp = p->n_sp/* for init */
ragge
1.60
190         nfree(p);
191         return q;
gmcgarry
1.86
192
ragge
1.137
193 #else /* defined(PECOFFABI) || defined(AOUTABI) */
gmcgarry
1.86
194
195         return p;
196
197 #endif
198
ragge
1.60
199 }
200
201 /*
202  * Create a reference for a static variable.
203  */
204 static NODE *
205 picstatic(NODE *p)
206 {
gmcgarry
1.86
207
208 #if defined(ELFABI)
209
ragge
1.60
210         NODE *q, *r;
stefan
1.66
211         struct symtab *sp;
ragge
1.60
212
ragge
1.138
213         q = tempnode(gotnrPTR|VOID00);
ragge
1.94
214         if (p->n_sp->slevel > 0) {
ragge
1.60
215                 char buf[32];
216                 snprintf(buf32LABFMT, (int)p->n_sp->soffset);
gmcgarry
1.83
217                 sp = picsymtab(""buf"@GOTOFF");
ragge
1.96
218         } else {
219                 char *name;
220                 if ((name = p->n_sp->soname) == NULL)
221                         name = p->n_sp->sname;
222                 sp = picsymtab(""name"@GOTOFF");
223         }
224         
stefan
1.66
225         sp->sclass = STATIC;
226         sp->stype = p->n_sp->stype;
227         r = xbcon(0spINT);
ragge
1.60
228         q = buildtree(PLUSqr);
ragge
1.123
229         q = block(UMULq0p->n_typep->n_dfp->n_ap);
ragge
1.65
230         q->n_sp = p->n_sp/* for init */
ragge
1.60
231         nfree(p);
gmcgarry
1.86
232         return q;
gmcgarry
1.83
233
234 #elif defined(MACHOABI)
235
gmcgarry
1.86
236         NODE *q, *r;
237         struct symtab *sp;
gmcgarry
1.101
238         char buf2[256];
gmcgarry
1.83
239
gmcgarry
1.101
240         snprintf(buf2256"-L%s$pb",
ragge
1.97
241             cftnsp->soname ? cftnsp->soname : cftnsp->sname);
gmcgarry
1.83
242
ragge
1.94
243         if (p->n_sp->slevel > 0) {
gmcgarry
1.101
244                 char buf1[32];
245                 snprintf(buf132LABFMT, (int)p->n_sp->soffset);
gmcgarry
1.83
246                 sp = picsymtab(""buf1buf2);
247         } else  {
ragge
1.96
248                 char *name;
249                 if ((name = p->n_sp->soname) == NULL)
250                         name = p->n_sp->sname;
251                 sp = picsymtab(""exname(name), buf2);
gmcgarry
1.83
252         }
253         sp->sclass = STATIC;
254         sp->stype = p->n_sp->stype;
ragge
1.138
255         q = tempnode(gotnrPTR+VOID00);
gmcgarry
1.83
256         r = xbcon(0spINT);
257         q = buildtree(PLUSqr);
ragge
1.123
258         q = block(UMULq0p->n_typep->n_dfp->n_ap);
gmcgarry
1.83
259         q->n_sp = p->n_sp;
260         nfree(p);
gmcgarry
1.86
261         return q;
262
ragge
1.137
263 #else /* defined(PECOFFABI) || defined(AOUTABI) */
gmcgarry
1.86
264
265         return p;
gmcgarry
1.83
266
267 #endif
268
ragge
1.60
269 }
ragge
1.1
270
ragge
1.73
271 #ifdef TLS
272 /*
273  * Create a reference for a TLS variable.
274  */
275 static NODE *
276 tlspic(NODE *p)
277 {
278         NODE *q, *r;
ragge
1.85
279         struct symtab *sp, *sp2;
ragge
1.96
280         char *name;
ragge
1.73
281
282         /*
283          * creates:
284          *   leal var@TLSGD(%ebx),%eax
285          *   call ___tls_get_addr@PLT
286          */
287
288         /* calc address of var@TLSGD */
ragge
1.138
289         q = tempnode(gotnrPTR|VOID00);
ragge
1.96
290         if ((name = p->n_sp->soname) == NULL)
291                 name = p->n_sp->sname;
292         sp = picsymtab(""name"@TLSGD");
ragge
1.73
293         r = xbcon(0spINT);
294         q = buildtree(PLUSqr);
295
296         /* assign to %eax */
ragge
1.138
297         r = block(REGNILNILPTR|VOID00);
ragge
1.73
298         r->n_rval = EAX;
299         q = buildtree(ASSIGNrq);
300
301         /* call ___tls_get_addr */
ragge
1.85
302         sp2 = lookup("___tls_get_addr@PLT"0);
303         sp2->stype = EXTERN|INT|FTN;
304         r = nametree(sp2);
ragge
1.73
305         r = buildtree(ADDROFrNIL);
ragge
1.138
306         r = block(UCALLrNILINT00);
ragge
1.73
307
308         /* fusion both parts together */
309         q = buildtree(COMOPqr);
ragge
1.123
310         q = block(UMULq0p->n_typep->n_dfp->n_ap);
ragge
1.73
311         q->n_sp = p->n_sp/* for init */
312
313         nfree(p);
314         return q;
315 }
316
317 static NODE *
318 tlsnonpic(NODE *p)
319 {
320         NODE *q, *r;
ragge
1.85
321         struct symtab *sp, *sp2;
ragge
1.73
322         int ext = p->n_sp->sclass;
ragge
1.96
323         char *name;
ragge
1.73
324
ragge
1.96
325         if ((name = p->n_sp->soname) == NULL)
326                 name = p->n_sp->sname;
327         sp = picsymtab(""name,
gmcgarry
1.83
328             ext == EXTERN ? "@INDNTPOFF" : "@NTPOFF");
ragge
1.73
329         q = xbcon(0spINT);
330         if (ext == EXTERN)
ragge
1.138
331                 q = block(UMULqNILPTR|VOID00);
ragge
1.73
332
ragge
1.85
333         sp2 = lookup("%gs:0"0);
334         sp2->stype = EXTERN|INT;
335         r = nametree(sp2);
ragge
1.73
336
337         q = buildtree(PLUSqr);
ragge
1.123
338         q = block(UMULq0p->n_typep->n_dfp->n_ap);
ragge
1.73
339         q->n_sp = p->n_sp/* for init */
340
341         nfree(p);
342         return q;
343 }
344
345 static NODE *
346 tlsref(NODE *p)
347 {
348         if (kflag)
349                 return (tlspic(p));
350         else
351                 return (tlsnonpic(p));
352 }
353 #endif
354
ragge
1.48
355 /* clocal() is called to do local transformations on
356  * an expression tree preparitory to its being
357  * written out in intermediate code.
358  *
359  * the major essential job is rewriting the
360  * automatic variables and arguments in terms of
361  * REG and OREG nodes
362  * conversion ops which are not necessary are also clobbered here
363  * in addition, any special features (such as rewriting
364  * exclusive or) are easily handled here as well
365  */
ragge
1.1
366 NODE *
367 clocal(NODE *p)
368 {
369
370         register struct symtab *q;
gmcgarry
1.113
371         register NODE *r, *l;
372 #if defined(os_openbsd)
373         register NODE *s, *n;
374 #endif
ragge
1.1
375         register int o;
ragge
1.40
376         register int m;
ragge
1.1
377
ragge
1.54
378 #ifdef PCC_DEBUG
379         if (xdebug) {
380                 printf("clocal: %p\n"p);
381                 fwalk(peprint0);
382         }
383 #endif
ragge
1.1
384         switcho = p->n_op ){
385
386         case NAME:
387                 if ((q = p->n_sp) == NULL)
388                         return p/* Nothing to care about */
389
390                 switch (q->sclass) {
391
392                 case PARAM:
393                 case AUTO:
394                         /* fake up a structure reference */
395                         r = block(REGNILNILPTR+STRTY00);
396                         r->n_lval = 0;
397                         r->n_rval = FPREG;
398                         p = stref(block(STREFrp000));
399                         break;
400
ragge
1.65
401                 case USTATIC:
402                         if (kflag == 0)
403                                 break;
404                         /* FALLTHROUGH */
ragge
1.1
405                 case STATIC:
ragge
1.73
406 #ifdef TLS
407                         if (q->sflags & STLS) {
408                                 p = tlsref(p);
409                                 break;
410                         }
411 #endif
ragge
1.60
412                         if (kflag == 0) {
413                                 if (q->slevel == 0)
414                                         break;
ragge
1.147
415                         } else if (blevel > 0 && !statinit)
ragge
1.60
416                                 p = picstatic(p);
ragge
1.1
417                         break;
418
419                 case REGISTER:
420                         p->n_op = REG;
421                         p->n_lval = 0;
422                         p->n_rval = q->soffset;
423                         break;
424
ragge
1.60
425                 case EXTERN:
426                 case EXTDEF:
ragge
1.73
427 #ifdef TLS
428                         if (q->sflags & STLS) {
429                                 p = tlsref(p);
430                                 break;
431                         }
432 #endif
gmcgarry
1.103
433
434 #ifdef os_win32
435                         if (q->sflags & SDLLINDIRECT)
436                                 p = import(p);
437 #endif
ragge
1.60
438                         if (kflag == 0)
439                                 break;
ragge
1.147
440                         if (blevel > 0 && !statinit)
ragge
1.60
441                                 p = picext(p);
442                         break;
443                 }
ragge
1.1
444                 break;
445
ragge
1.65
446         case ADDROF:
447                 if (kflag == 0 || blevel == 0)
448                         break;
449                 /* char arrays may end up here */
450                 l = p->n_left;
451                 if (l->n_op != NAME ||
452                     (l->n_type != ARY+CHAR && l->n_type != ARY+WCHAR_TYPE))
453                         break;
454                 l = p;
455                 p = picstatic(p->n_left);
456                 nfree(l);
457                 if (p->n_op != UMUL)
458                         cerror("ADDROF error");
459                 l = p;
460                 p = p->n_left;
461                 nfree(l);
462                 break;
463
ragge
1.62
464         case UCALL:
465         case USTCALL:
ragge
1.60
466                 if (kflag == 0)
467                         break;
gmcgarry
1.86
468 #if defined(ELFABI)
ragge
1.62
469                 /* Change to CALL node with ebx as argument */
ragge
1.138
470                 l = block(REGNILNILINT00);
ragge
1.62
471                 l->n_rval = EBX;
472                 p->n_right = buildtree(ASSIGNl,
ragge
1.138
473                     tempnode(gotnrINT00));
ragge
1.62
474                 p->n_op -= (UCALL-CALL);
gmcgarry
1.86
475 #endif
pantzer
1.89
476         
477         /* FALLTHROUGH */
478 #if defined(MACHOABI)
479         case CALL:
480         case STCALL:
481                 if (p->n_type == VOID)
482                         break;
483
ragge
1.123
484                 r = tempnode(0p->n_typep->n_dfp->n_ap);
pantzer
1.89
485                 l = tcopy(r);
486                 p = buildtree(COMOPbuildtree(ASSIGNrp), l);
487 #endif
488                         
ragge
1.10
489                 break;
ragge
1.64
490
ragge
1.159
491 #if defined(os_openbsd)
492 /*
493  * The called function returns structures according to their aligned size:
494  *  Structures 1, 2 or 4 bytes in size are placed in EAX.
495  *  Structures 8 bytes in size are placed in: EAX and EDX.
496  */
497         case UMUL:
498                 o = 0;
499                 l = p->n_left;
500                 if (l->n_op == PLUS)
501                         l = l->n_lefto++;
502                 if (l->n_op != PCONV)
503                         break;
504                 l = l->n_left;
505                 if (l->n_op != STCALL && l->n_op != USTCALL)
506                         break;
507                 m = tsize(DECREF(l->n_type), l->n_dfl->n_ap);
508                 if (m != SZCHAR && m != SZSHORT &&
509                     m != SZINT && m != SZLONGLONG)
510                         break;
511                 /* This is a direct reference to a small struct return */
512                 l->n_op -= (STCALL-CALL);
513                 if (o == 0) {
514                         /* first element in struct */
515                         p->n_left = nfree(p->n_left); /* free PCONV */
516                         l->n_ap = p->n_ap;
517                         l->n_df = p->n_df;
518                         l->n_type = p->n_type;
519                         if (p->n_type < INT) {
520                                 /* Need to add SCONV */
521                                 p->n_op = SCONV;
522                         } else
523                                 p = nfree(p); /* no casts needed */
524                 } else {
525                         /* convert PLUS to RS */
526                         o = p->n_left->n_right->n_lval;
527                         l->n_type = (o > 3 ? ULONGLONG : UNSIGNED);
528                         nfree(p->n_left->n_right);
529                         p->n_left = nfree(p->n_left);
530                         p->n_left = nfree(p->n_left);
531                         p->n_left = buildtree(RSp->n_leftbcon(o*8));
532                         p->n_left = makety(p->n_leftp->n_typep->n_qual,
533                             p->n_dfp->n_ap);
534                         p = nfree(p);
535                 }
536                 break;
537 #endif
538
ragge
1.129
539 #ifdef notyet
540         /* XXX breaks sometimes */
ragge
1.8
541         case CBRANCH:
542                 l = p->n_left;
543
544                 /*
ragge
1.77
545                  * Remove unnecessary conversion ops.
ragge
1.8
546                  */
ragge
1.128
547                 if (!clogop(l->n_op) || l->n_left->n_op != SCONV)
548                         break;
549                 if (coptype(l->n_op) != BITYPE)
550                         break;
551                 if (l->n_right->n_op != ICON)
552                         break;
553                 r = l->n_left->n_left;
554                 if (r->n_type >= FLOAT)
555                         break;
556                 if (toolarge(r->n_typel->n_right->n_lval))
557                         break;
558                 l->n_right->n_type = r->n_type;
559                 if (l->n_op >= ULE && l->n_op <= UGT)
560                         l->n_op -= (UGT-ULE);
561                 p->n_left = buildtree(l->n_oprl->n_right);
562                 nfree(l->n_left);
563                 nfree(l);
ragge
1.8
564                 break;
ragge
1.129
565 #endif
ragge
1.8
566
ragge
1.1
567         case PCONV:
568                 l = p->n_left;
ragge
1.162
569
570                 /* Make int type before pointer */
ragge
1.40
571                 if (l->n_type < INT || l->n_type == LONGLONG || 
ragge
1.134
572                     l->n_type == ULONGLONG || l->n_type == BOOL) {
ragge
1.40
573                         /* float etc? */
ragge
1.162
574                         p->n_left = block(SCONVlNILUNSIGNED00);
ragge
1.40
575                 }
576                 break;
577
ragge
1.1
578         case SCONV:
ragge
1.109
579                 if (p->n_left->n_op == COMOP)
580                         break;  /* may propagate wrong type later */
ragge
1.1
581                 l = p->n_left;
ragge
1.4
582
ragge
1.25
583                 if (p->n_type == l->n_type) {
584                         nfree(p);
585                         return l;
586                 }
587
ragge
1.4
588                 if ((p->n_type & TMASK) == 0 && (l->n_type & TMASK) == 0 &&
ragge
1.139
589                     tsize(p->n_typep->n_dfp->n_ap) ==
590                     tsize(l->n_typel->n_dfl->n_ap)) {
ragge
1.4
591                         if (p->n_type != FLOAT && p->n_type != DOUBLE &&
ragge
1.15
592                             l->n_type != FLOAT && l->n_type != DOUBLE &&
593                             l->n_type != LDOUBLE && p->n_type != LDOUBLE) {
ragge
1.41
594                                 if (l->n_op == NAME || l->n_op == UMUL ||
595                                     l->n_op == TEMP) {
ragge
1.15
596                                         l->n_type = p->n_type;
597                                         nfree(p);
598                                         return l;
599                                 }
ragge
1.4
600                         }
ragge
1.6
601                 }
602
ragge
1.42
603                 if (DEUNSIGN(p->n_type) == INT && DEUNSIGN(l->n_type) == INT &&
ragge
1.131
604                     coptype(l->n_op) == BITYPE && l->n_op != COMOP &&
605                     l->n_op != QUEST) {
ragge
1.42
606                         l->n_type = p->n_type;
607                         nfree(p);
608                         return l;
609                 }
610
ragge
1.3
611                 o = l->n_op;
ragge
1.4
612                 m = p->n_type;
ragge
1.1
613
614                 if (o == ICON) {
ragge
1.147
615                         /*
616                          * Can only end up here if o is an address,
617                          * and in that case the only compile-time conversion
618                          * possible is to int.
619                          */
ragge
1.156
620                         if ((TMASK & l->n_type) == 0 && l->n_sp == NULL)
ragge
1.147
621                                 cerror("SCONV ICON");
622                         if (l->n_sp == 0) {
623                                 p->n_type = UNSIGNED;
624                                 concast(lm);
625                         } else if (m != INT && m != UNSIGNED)
ragge
1.1
626                                 break;
627                         l->n_type = m;
ragge
1.138
628                         l->n_ap = 0;
ragge
1.1
629                         nfree(p);
630                         return l;
ragge
1.147
631                 } else if (l->n_op == FCON)
632                         cerror("SCONV FCON");
ragge
1.36
633                 if ((p->n_type == CHAR || p->n_type == UCHAR ||
634                     p->n_type == SHORT || p->n_type == USHORT) &&
635                     (l->n_type == FLOAT || l->n_type == DOUBLE ||
636                     l->n_type == LDOUBLE)) {
ragge
1.123
637                         p = block(SCONVpNILp->n_typep->n_dfp->n_ap);
ragge
1.36
638                         p->n_left->n_type = INT;
639                         return p;
640                 }
ragge
1.1
641                 break;
642
ragge
1.15
643         case MOD:
644         case DIV:
645                 if (o == DIV && p->n_type != CHAR && p->n_type != SHORT)
646                         break;
ragge
1.16
647                 if (o == MOD && p->n_type != CHAR && p->n_type != SHORT)
ragge
1.15
648                         break;
649                 /* make it an int division by inserting conversions */
ragge
1.147
650                 p->n_left = makety(p->n_leftINT000);
651                 p->n_right = makety(p->n_rightINT000);
652                 o = p->n_type;
653                 p->n_type = INT;
654                 p = makety(po000);
ragge
1.15
655                 break;
656
ragge
1.31
657         case FORCE:
ragge
1.32
658                 /* put return value in return reg */
659                 p->n_op = ASSIGN;
660                 p->n_right = p->n_left;
ragge
1.138
661                 p->n_left = block(REGNILNILp->n_type00);
ragge
1.53
662                 p->n_left->n_rval = p->n_left->n_type == BOOL ? 
663                     RETREG(CHAR) : RETREG(p->n_type);
ragge
1.31
664                 break;
ragge
1.39
665
666         case LS:
667         case RS:
ragge
1.127
668                 /* shift count must be in a char */
ragge
1.39
669                 if (p->n_right->n_type == CHAR || p->n_right->n_type == UCHAR)
670                         break;
ragge
1.138
671                 p->n_right = block(SCONVp->n_rightNILCHAR00);
ragge
1.39
672                 break;
ragge
1.108
673 #if defined(os_openbsd)
674                 /* If not using pcc struct return */
675         case STASG:
676                 r = p->n_right;
677                 if (r->n_op != STCALL && r->n_op != USTCALL)
678                         break;
ragge
1.123
679                 m = tsize(BTYPE(r->n_type), r->n_dfr->n_ap);
ragge
1.108
680                 if (m == SZCHAR)
681                         m = CHAR;
682                 else if (m == SZSHORT)
683                         m = SHORT;
684                 else if (m == SZINT)
685                         m = INT;
686                 else if (m == SZLONGLONG)
687                         m = LONGLONG;
688                 else
689                         break;
690
691                 l = buildtree(ADDROFp->n_leftNIL);
692                 nfree(p);
693
694                 r->n_op -= (STCALL-CALL);
695                 r->n_type = m;
696
697                 /* r = long, l = &struct */
698
ragge
1.123
699                 n = tempnode(0mr->n_dfr->n_ap);
ragge
1.108
700                 r = buildtree(ASSIGNccopy(n), r);
701
ragge
1.123
702                 s = tempnode(0l->n_typel->n_dfl->n_ap);
ragge
1.108
703                 l = buildtree(ASSIGNccopy(s), l);
704
705                 p = buildtree(COMOPrl);
706
707                 l = buildtree(CAST,
ragge
1.138
708                     block(NAMENILNILm|PTR00), ccopy(s));
ragge
1.108
709                 r = l->n_right;
710                 nfree(l->n_left);
711                 nfree(l);
712
713                 r = buildtree(ASSIGNbuildtree(UMULrNIL), n);
714                 p = buildtree(COMOPpr);
715                 p = buildtree(COMOPps);
716                 break;
717 #endif
ragge
1.1
718         }
ragge
1.54
719 #ifdef PCC_DEBUG
720         if (xdebug) {
721                 printf("clocal end: %p\n"p);
722                 fwalk(peprint0);
723         }
724 #endif
ragge
1.1
725         return(p);
726 }
727
ragge
1.64
728 /*
729  * Change CALL references to either direct (static) or PLT.
730  */
731 static void
ragge
1.88
732 fixnames(NODE *pvoid *arg)
ragge
1.64
733 {
ragge
1.137
734 #if defined(ELFABI) || defined(MACHOABI)
gmcgarry
1.86
735
ragge
1.64
736         struct symtab *sp;
ragge
1.123
737         struct attr *ap;
ragge
1.64
738         NODE *q;
739         char *c;
740         int isu;
741
742         if ((cdope(p->n_op) & CALLFLG) == 0)
743                 return;
744         isu = 0;
745         q = p->n_left;
ragge
1.123
746         ap = q->n_ap;
ragge
1.64
747         if (q->n_op == UMUL)
748                 q = q->n_leftisu = 1;
gmcgarry
1.83
749
ragge
1.64
750         if (q->n_op == PLUS && q->n_left->n_op == TEMP &&
751             q->n_right->n_op == ICON) {
752                 sp = q->n_right->n_sp;
753
ragge
1.65
754                 if (sp == NULL)
755                         return/* nothing to do */
ragge
1.64
756                 if (sp->sclass == STATIC && !ISFTN(sp->stype))
757                         return/* function pointer */
758
759                 if (sp->sclass != STATIC && sp->sclass != EXTERN &&
760                     sp->sclass != EXTDEF)
761                         cerror("fixnames");
ragge
1.96
762                 c = NULL;
gmcgarry
1.83
763 #if defined(ELFABI)
764
ragge
1.96
765                 if (sp->soname == NULL ||
766                     (c = strstr(sp->soname"@GOT")) == NULL)
ragge
1.64
767                         cerror("fixnames2");
768                 if (isu) {
769                         memcpy(c"@PLT"sizeof("@PLT"));
770                 } else
771                         *c = 0;
gmcgarry
1.83
772
773 #elif defined(MACHOABI)
774
ragge
1.96
775                 if (sp->soname == NULL ||
776                     ((c = strstr(sp->soname"$non_lazy_ptr")) == NULL &&
777                     (c = strstr(sp->soname"-L")) == NULL))
gmcgarry
1.83
778                                 cerror("fixnames2");
gmcgarry
1.158
779
780                 if (!ISFTN(sp->stype))
781                         return/* function pointer */
782
gmcgarry
1.83
783                 if (isu) {
784                         addstub(&stublistsp->soname+1);
plunky
1.166
785                         memcpy(c"$stub"sizeof("$stub"));
gmcgarry
1.83
786                 } else 
787                         *c = 0;
788
789 #endif
790
ragge
1.64
791                 nfree(q->n_left);
792                 q = q->n_right;
793                 if (isu)
794                         nfree(p->n_left->n_left);
795                 nfree(p->n_left);
796                 p->n_left = q;
ragge
1.123
797                 q->n_ap = ap;
ragge
1.64
798         }
gmcgarry
1.86
799 #endif
ragge
1.64
800 }
801
ragge
1.117
802 static void mangle(NODE *p);
803
ragge
1.1
804 void
805 myp2tree(NODE *p)
806 {
ragge
1.67
807         struct symtab *sp;
808
ragge
1.64
809         if (kflag)
ragge
1.117
810                 fixnames(p0);
811
812         mangle(p);
813
ragge
1.67
814         if (p->n_op != FCON)
815                 return;
816
817         sp = IALLOC(sizeof(struct symtab));
818         sp->sclass = STATIC;
ragge
1.138
819         sp->sap = 0;
ragge
1.67
820         sp->slevel = 1/* fake numeric label */
ragge
1.70
821         sp->soffset = getlab();
ragge
1.67
822         sp->sflags = 0;
ragge
1.70
823         sp->stype = p->n_type;
824         sp->squal = (CON >> TSHIFT);
gmcgarry
1.144
825         sp->sname = sp->soname = NULL;
ragge
1.70
826
ragge
1.154
827         locctr(DATAsp);
ragge
1.70
828         defloc(sp);
ragge
1.139
829         ninval(0tsize(sp->stypesp->sdfsp->sap), p);
ragge
1.67
830
831         p->n_op = NAME;
832         p->n_lval = 0;
833         p->n_sp = sp;
ragge
1.161
834
835         if (kflag) {
836                 NODE *q = optim(picstatic(tcopy(p)));
837                 *p = *q;
838                 nfree(q);
839         }
ragge
1.1
840 }
841
842 /*ARGSUSED*/
843 int
844 andable(NODE *p)
845 {
ragge
1.64
846         return(1);      /* all names can have & taken on them */
ragge
1.1
847 }
848
849 /*
ragge
1.36
850  * Return 1 if a variable of type type is OK to put in register.
851  */
852 int
853 cisreg(TWORD t)
854 {
855         if (t == FLOAT || t == DOUBLE || t == LDOUBLE)
856                 return 0/* not yet */
857         return 1;
858 }
859
860 /*
ragge
1.1
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.138
873         sp = block(REGNILNILp->n_type00);
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 */
ragge
1.138
880         sp = block(REGNILNILp->n_type00);
pantzer
1.100
881         sp->n_lval = 0;
882         sp->n_rval = STKREG;
883         ecomp(buildtree(PLUSEQspbcon(15)));
884         
ragge
1.138
885         sp = block(REGNILNILp->n_type00);
pantzer
1.100
886         sp->n_lval = 0;
887         sp->n_rval = STKREG;
888         ecomp(buildtree(RSEQspbcon(4)));
889         
ragge
1.138
890         sp = block(REGNILNILp->n_type00);
pantzer
1.100
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 */
ragge
1.123
898         sp = block(REGNILNILPTR+INTt->n_dft->n_ap);
ragge
1.1
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.46
907  * print out a constant node, may be associated with a label.
908  * Do not free the node after use.
ragge
1.49
909  * off is bit offset from the beginning of the aggregate
910  * fsz is the number of bits this is referring to
ragge
1.21
911  */
ragge
1.150
912 int
ragge
1.49
913 ninval(CONSZ offint fszNODE *p)
ragge
1.21
914 {
ragge
1.46
915         union { float fdouble dlong double lint i[3]; } u;
916         int i;
ragge
1.23
917
ragge
1.151
918         switch (p->n_type) {
ragge
1.21
919         case LONGLONG:
920         case ULONGLONG:
gmcgarry
1.107
921                 i = (int)(p->n_lval >> 32);
ragge
1.46
922                 p->n_lval &= 0xffffffff;
ragge
1.51
923                 p->n_type = INT;
ragge
1.151
924                 inval(off32p);
ragge
1.46
925                 p->n_lval = i;
ragge
1.151
926                 inval(off+3232p);
ragge
1.21
927                 break;
ragge
1.46
928         case LDOUBLE:
929                 u.i[2] = 0;
930                 u.l = (long double)p->n_dcon;
gmcgarry
1.87
931 #if defined(HOST_BIG_ENDIAN)
932                 /