Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20110219100226

Diff

Diff from 1.130 to:

Annotations

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

Annotated File View

ragge
1.130
1 /*      $Id: local.c,v 1.130 2011/02/19 10:02:26 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);
121         q = block(UMULq0PTR|VOID0MKSUE(VOID));
122         q = block(UMULq0p->n_typep->n_dfp->n_sue);
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.123
146         q = tempnode(gotnrPTR|VOID0MKAP(VOID));
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.123
157         q = block(UMULq0PTR|VOID0MKAP(VOID));
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.123
181         q = tempnode(gotnrPTR+VOID0MKAP(VOID));
gmcgarry
1.83
182         r = xbcon(0spINT);
183         q = buildtree(PLUSqr);
184
185         if (p->n_sp->sclass != EXTDEF)
ragge
1.123
186                 q = block(UMULq0PTR+VOID0MKAP(VOID));
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
192 #elif defined(PECOFFABI)
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.123
212         q = tempnode(gotnrPTR|VOID0MKAP(VOID));
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.123
254         q = tempnode(gotnrPTR+VOID0MKAP(VOID));
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
262 #elif defined(PECOFFABI)
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.123
288         q = tempnode(gotnrPTR|VOID0MKAP(VOID));
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.123
296         r = block(REGNILNILPTR|VOID0MKAP(VOID));
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.123
305         r = block(UCALLrNILINT0MKAP(INT));
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.123
330                 q = block(UMULqNILPTR|VOID0MKAP(VOID));
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;
414                                 p->n_lval = 0;
ragge
1.64
415                         } else if (blevel > 0)
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.64
440                         if (blevel > 0)
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.123
470                 l = block(REGNILNILINT0MKAP(INT));
ragge
1.62
471                 l->n_rval = EBX;
472                 p->n_right = buildtree(ASSIGNl,
ragge
1.123
473                     tempnode(gotnrINT0MKAP(INT)));
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.129
491 #ifdef notyet
492         /* XXX breaks sometimes */
ragge
1.8
493         case CBRANCH:
494                 l = p->n_left;
495
496                 /*
ragge
1.77
497                  * Remove unnecessary conversion ops.
ragge
1.8
498                  */
ragge
1.128
499                 if (!clogop(l->n_op) || l->n_left->n_op != SCONV)
500                         break;
501                 if (coptype(l->n_op) != BITYPE)
502                         break;
503                 if (l->n_right->n_op != ICON)
504                         break;
505                 r = l->n_left->n_left;
506                 if (r->n_type >= FLOAT)
507                         break;
508                 if (toolarge(r->n_typel->n_right->n_lval))
509                         break;
510                 l->n_right->n_type = r->n_type;
511                 if (l->n_op >= ULE && l->n_op <= UGT)
512                         l->n_op -= (UGT-ULE);
513                 p->n_left = buildtree(l->n_oprl->n_right);
514                 nfree(l->n_left);
515                 nfree(l);
ragge
1.8
516                 break;
ragge
1.129
517 #endif
ragge
1.8
518
ragge
1.1
519         case PCONV:
ragge
1.40
520                 /* Remove redundant PCONV's. Be careful */
ragge
1.1
521                 l = p->n_left;
ragge
1.40
522                 if (l->n_op == ICON) {
523                         l->n_lval = (unsigned)l->n_lval;
524                         goto delp;
525                 }
526                 if (l->n_type < INT || l->n_type == LONGLONG || 
527                     l->n_type == ULONGLONG) {
528                         /* float etc? */
529                         p->n_left = block(SCONVlNIL,
ragge
1.123
530                             UNSIGNED0MKAP(UNSIGNED));
ragge
1.40
531                         break;
532                 }
533                 /* if left is SCONV, cannot remove */
534                 if (l->n_op == SCONV)
ragge
1.1
535                         break;
ragge
1.54
536
537                 /* avoid ADDROF TEMP */
538                 if (l->n_op == ADDROF && l->n_left->n_op == TEMP)
539                         break;
540
ragge
1.40
541                 /* if conversion to another pointer type, just remove */
542                 if (p->n_type > BTMASK && l->n_type > BTMASK)
543                         goto delp;
544                 break;
545
546         delp:   l->n_type = p->n_type;
ragge
1.1
547                 l->n_qual = p->n_qual;
548                 l->n_df = p->n_df;
ragge
1.123
549                 l->n_ap = p->n_ap;
ragge
1.1
550                 nfree(p);
551                 p = l;
552                 break;
ragge
1.40
553                 
ragge
1.1
554         case SCONV:
ragge
1.109
555                 if (p->n_left->n_op == COMOP)
556                         break;  /* may propagate wrong type later */
ragge
1.1
557                 l = p->n_left;
ragge
1.4
558
ragge
1.25
559                 if (p->n_type == l->n_type) {
560                         nfree(p);
561                         return l;
562                 }
563
ragge
1.4
564                 if ((p->n_type & TMASK) == 0 && (l->n_type & TMASK) == 0 &&
ragge
1.123
565                     btattr[p->n_type].atypsz == btattr[l->n_type].atypsz) {
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 &&
579                     coptype(l->n_op) == BITYPE) {
580                         l->n_type = p->n_type;
581                         nfree(p);
582                         return l;
583                 }
584
ragge
1.3
585                 o = l->n_op;
ragge
1.4
586                 m = p->n_type;
ragge
1.1
587
588                 if (o == ICON) {
589                         CONSZ val = l->n_lval;
590
ragge
1.29
591                         if (!ISPTR(m)) /* Pointers don't need to be conv'd */
592                             switch (m) {
ragge
1.52
593                         case BOOL:
ragge
1.125
594                                 l->n_lval = nncon(l) ? (l->n_lval != 0) : 1;
595                                 l->n_sp = NULL;
ragge
1.52
596                                 break;
ragge
1.1
597                         case CHAR:
598                                 l->n_lval = (char)val;
599                                 break;
600                         case UCHAR:
601                                 l->n_lval = val & 0377;
602                                 break;
ragge
1.15
603                         case SHORT:
ragge
1.1
604                                 l->n_lval = (short)val;
605                                 break;
ragge
1.15
606                         case USHORT:
ragge
1.1
607                                 l->n_lval = val & 0177777;
608                                 break;
ragge
1.15
609                         case ULONG:
ragge
1.1
610                         case UNSIGNED:
611                                 l->n_lval = val & 0xffffffff;
612                                 break;
ragge
1.15
613                         case LONG:
ragge
1.1
614                         case INT:
615                                 l->n_lval = (int)val;
616                                 break;
617                         case LONGLONG:
618                                 l->n_lval = (long long)val;
ragge
1.15
619                                 break;
ragge
1.1
620                         case ULONGLONG:
621                                 l->n_lval = val;
622                                 break;
623                         case VOID:
624                                 break;
ragge
1.14
625                         case LDOUBLE:
ragge
1.1
626                         case DOUBLE:
627                         case FLOAT:
628                                 l->n_op = FCON;
629                                 l->n_dcon = val;
630                                 break;
631                         default:
632                                 cerror("unknown type %d"m);
633                         }
634                         l->n_type = m;
ragge
1.123
635                         l->n_ap = MKAP(m);
ragge
1.1
636                         nfree(p);
637                         return l;
ragge
1.72
638                 } else if (l->n_op == FCON) {
gmcgarry
1.107
639                         l->n_lval = (CONSZ)l->n_dcon;
ragge
1.72
640                         l->n_sp = NULL;
641                         l->n_op = ICON;
642                         l->n_type = m;
ragge
1.123
643                         l->n_ap = MKAP(m);
ragge
1.72
644                         nfree(p);
645                         return clocal(l);
ragge
1.1
646                 }
ragge
1.17
647                 if (DEUNSIGN(p->n_type) == SHORT &&
648                     DEUNSIGN(l->n_type) == SHORT) {
649                         nfree(p);
650                         p = l;
651                 }
ragge
1.36
652                 if ((p->n_type == CHAR || p->n_type == UCHAR ||
653                     p->n_type == SHORT || p->n_type == USHORT) &&
654                     (l->n_type == FLOAT || l->n_type == DOUBLE ||
655                     l->n_type == LDOUBLE)) {
ragge
1.123
656                         p = block(SCONVpNILp->n_typep->n_dfp->n_ap);
ragge
1.36
657                         p->n_left->n_type = INT;
658                         return p;
659                 }
ragge
1.1
660                 break;
661
ragge
1.15
662         case MOD:
663         case DIV:
664                 if (o == DIV && p->n_type != CHAR && p->n_type != SHORT)
665                         break;
ragge
1.16
666                 if (o == MOD && p->n_type != CHAR && p->n_type != SHORT)
ragge
1.15
667                         break;
668                 /* make it an int division by inserting conversions */
ragge
1.123
669                 p->n_left = block(SCONVp->n_leftNILINT0MKAP(INT));
670                 p->n_right = block(SCONVp->n_rightNILINT0MKAP(INT));
671                 p = block(SCONVpNILp->n_type0MKAP(p->n_type));
ragge
1.15
672                 p->n_left->n_type = INT;
673                 break;
674
ragge
1.1
675         case PMCONV:
676         case PVCONV:
ragge
1.74
677                 r = p;
678                 p = buildtree(o == PMCONV ? MUL : DIVp->n_leftp->n_right);
679                 nfree(r);
680                 break;
ragge
1.1
681
ragge
1.31
682         case FORCE:
ragge
1.32
683                 /* put return value in return reg */
684                 p->n_op = ASSIGN;
685                 p->n_right = p->n_left;
ragge
1.123
686                 p->n_left = block(REGNILNILp->n_type0MKAP(INT));
ragge
1.53
687                 p->n_left->n_rval = p->n_left->n_type == BOOL ? 
688                     RETREG(CHAR) : RETREG(p->n_type);
ragge
1.31
689                 break;
ragge
1.39
690
691         case LS:
692         case RS:
ragge
1.127
693                 /* shift count must be in a char */
ragge
1.39
694                 if (p->n_right->n_type == CHAR || p->n_right->n_type == UCHAR)
695                         break;
ragge
1.127
696                 p->n_right = block(SCONVp->n_rightNILCHAR0MKAP(CHAR));
ragge
1.39
697                 break;
ragge
1.108
698 #if defined(os_openbsd)
699                 /* If not using pcc struct return */
700         case STASG:
701                 r = p->n_right;
702                 if (r->n_op != STCALL && r->n_op != USTCALL)
703                         break;
ragge
1.123
704                 m = tsize(BTYPE(r->n_type), r->n_dfr->n_ap);
ragge
1.108
705                 if (m == SZCHAR)
706                         m = CHAR;
707                 else if (m == SZSHORT)
708                         m = SHORT;
709                 else if (m == SZINT)
710                         m = INT;
711                 else if (m == SZLONGLONG)
712                         m = LONGLONG;
713                 else
714                         break;
715
716                 l = buildtree(ADDROFp->n_leftNIL);
717                 nfree(p);
718
719                 r->n_op -= (STCALL-CALL);
720                 r->n_type = m;
721
722                 /* r = long, l = &struct */
723
ragge
1.123
724                 n = tempnode(0mr->n_dfr->n_ap);
ragge
1.108
725                 r = buildtree(ASSIGNccopy(n), r);
726
ragge
1.123
727                 s = tempnode(0l->n_typel->n_dfl->n_ap);
ragge
1.108
728                 l = buildtree(ASSIGNccopy(s), l);
729
730                 p = buildtree(COMOPrl);
731
732                 l = buildtree(CAST,
ragge
1.123
733                     block(NAMENILNILm|PTR0MKAP(m)), ccopy(s));
ragge
1.108
734                 r = l->n_right;
735                 nfree(l->n_left);
736                 nfree(l);
737
738                 r = buildtree(ASSIGNbuildtree(UMULrNIL), n);
739                 p = buildtree(COMOPpr);
740                 p = buildtree(COMOPps);
741                 break;
742 #endif
ragge
1.1
743         }
ragge
1.54
744 #ifdef PCC_DEBUG
745         if (xdebug) {
746                 printf("clocal end: %p\n"p);
747                 fwalk(peprint0);
748         }
749 #endif
ragge
1.1
750         return(p);
751 }
752
ragge
1.64
753 /*
754  * Change CALL references to either direct (static) or PLT.
755  */
756 static void
ragge
1.88
757 fixnames(NODE *pvoid *arg)
ragge
1.64
758 {
gmcgarry
1.86
759 #if !defined(PECOFFABI)
760
ragge
1.64
761         struct symtab *sp;
ragge
1.123
762         struct attr *ap;
ragge
1.64
763         NODE *q;
764         char *c;
765         int isu;
766
767         if ((cdope(p->n_op) & CALLFLG) == 0)
768                 return;
769         isu = 0;
770         q = p->n_left;
ragge
1.123
771         ap = q->n_ap;
ragge
1.64
772         if (q->n_op == UMUL)
773                 q = q->n_leftisu = 1;
gmcgarry
1.83
774
ragge
1.64
775         if (q->n_op == PLUS && q->n_left->n_op == TEMP &&
776             q->n_right->n_op == ICON) {
777                 sp = q->n_right->n_sp;
778
ragge
1.65
779                 if (sp == NULL)
780                         return/* nothing to do */
ragge
1.64
781                 if (sp->sclass == STATIC && !ISFTN(sp->stype))
782                         return/* function pointer */
783
784                 if (sp->sclass != STATIC && sp->sclass != EXTERN &&
785                     sp->sclass != EXTDEF)
786                         cerror("fixnames");
ragge
1.96
787                 c = NULL;
gmcgarry
1.83
788 #if defined(ELFABI)
789
ragge
1.96
790                 if (sp->soname == NULL ||
791                     (c = strstr(sp->soname"@GOT")) == NULL)
ragge
1.64
792                         cerror("fixnames2");
793                 if (isu) {
794                         memcpy(c"@PLT"sizeof("@PLT"));
795                 } else
796                         *c = 0;
gmcgarry
1.83
797
798 #elif defined(MACHOABI)
799
ragge
1.96
800                 if (sp->soname == NULL ||
801                     ((c = strstr(sp->soname"$non_lazy_ptr")) == NULL &&
802                     (c = strstr(sp->soname"-L")) == NULL))
gmcgarry
1.83
803                                 cerror("fixnames2");
804                 if (isu) {
805                         *c = 0;
806                         addstub(&stublistsp->soname+1);
807                         strcpy(c"$stub");
808                 } else 
809                         *c = 0;
810
811 #endif
812
ragge
1.64
813                 nfree(q->n_left);
814                 q = q->n_right;
815                 if (isu)
816                         nfree(p->n_left->n_left);
817                 nfree(p->n_left);
818                 p->n_left = q;
ragge
1.123
819                 q->n_ap = ap;
ragge
1.64
820         }
gmcgarry
1.86
821 #endif
ragge
1.64
822 }
823
ragge
1.117
824 static void mangle(NODE *p);
825
ragge
1.1
826 void
827 myp2tree(NODE *p)
828 {
ragge
1.67
829         struct symtab *sp;
830
ragge
1.64
831         if (kflag)
ragge
1.117
832                 fixnames(p0);
833
834         mangle(p);
835
ragge
1.67
836         if (p->n_op != FCON)
837                 return;
838
839         sp = IALLOC(sizeof(struct symtab));
840         sp->sclass = STATIC;
ragge
1.123
841         sp->sap = MKAP(p->n_type);
ragge
1.67
842         sp->slevel = 1/* fake numeric label */
ragge
1.70
843         sp->soffset = getlab();
ragge
1.67
844         sp->sflags = 0;
ragge
1.70
845         sp->stype = p->n_type;
846         sp->squal = (CON >> TSHIFT);
847
848         defloc(sp);
ragge
1.123
849         ninval(0sp->sap->atypszp);
ragge
1.67
850
851         p->n_op = NAME;
852         p->n_lval = 0;
853         p->n_sp = sp;
ragge
1.1
854 }
855
856 /*ARGSUSED*/
857 int
858 andable(NODE *p)
859 {
ragge
1.64
860         return(1);      /* all names can have & taken on them */
ragge
1.1
861 }
862
863 /*
864  * at the end of the arguments of a ftn, set the automatic offset
865  */
866 void
867 cendarg()
868 {
869         autooff = AUTOINIT;
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  * return a node, for structure references, which is suitable for
885  * being added to a pointer of type t, in order to be off bits offset
886  * into a structure
887  * t, d, and s are the type, dimension offset, and sizeoffset
888  * For pdp10, return the type-specific index number which calculation
889  * is based on its size. For example, short a[3] would return 3.
890  * Be careful about only handling first-level pointers, the following
891  * indirections must be fullword.
892  */
893 NODE *
ragge
1.123
894 offcon(OFFSZ offTWORD tunion dimfun *dstruct attr *ap)
ragge
1.1
895 {
896         register NODE *p;
897
898         if (xdebug)
899                 printf("offcon: OFFSZ %lld type %x dim %p siz %d\n",
ragge
1.123
900                     offtd0);
ragge
1.1
901
902         p = bcon(0);
903         p->n_lval = off/SZCHAR/* Default */
904         return(p);
905 }
906
907 /*
908  * Allocate off bits on the stack.  p is a tree that when evaluated
ragge
1.45
909  * is the multiply count for off, t is a storeable node where to write
ragge
1.1
910  * the allocated address.
911  */
912 void
913 spalloc(NODE *tNODE *pOFFSZ off)
914 {
915         NODE *sp;
916
ragge
1.45
917         p = buildtree(MULpbcon(off/SZCHAR)); /* XXX word alignment? */
918
919         /* sub the size from sp */
ragge
1.123
920         sp = block(REGNILNILp->n_type0MKAP(INT));
ragge
1.45
921         sp->n_lval = 0;
922         sp->n_rval = STKREG;
923         ecomp(buildtree(MINUSEQspp));
ragge
1.1
924
pantzer
1.100
925 #ifdef MACHOABI 
926         /* align to 16 bytes */
ragge
1.123
927         sp = block(REGNILNILp->n_type0MKAP(INT));
pantzer
1.100
928         sp->n_lval = 0;
929         sp->n_rval = STKREG;
930         ecomp(buildtree(PLUSEQspbcon(15)));
931         
ragge
1.123
932         sp = block(REGNILNILp->n_type0MKAP(INT));
pantzer
1.100
933         sp->n_lval = 0;
934         sp->n_rval = STKREG;
935         ecomp(buildtree(RSEQspbcon(4)));
936         
ragge
1.123
937         sp = block(REGNILNILp->n_type0MKAP(INT));
pantzer
1.100
938         sp->n_lval = 0;
939         sp->