Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20120809162230

Diff

Diff from 1.161 to:

Annotations

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

Annotated File View

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