Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20110530173610

Diff

Diff from 1.145 to:

Annotations

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

Annotated File View

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