Quick Search:

View

Revision:
Expand:  
Changeset: r-1-0-0:ragge:20110330163536

Diff

Diff from 1.130.2.3 to:

Annotations

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

Annotated File View

ragge
1.130.2.3
1 /*      $Id: local.c,v 1.130.2.3 2011/03/30 16:35:36 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.130.2.2
121         q = block(UMULq0PTR|VOID0MKAP(VOID));
ragge
1.130.2.3
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.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 &&
ragge
1.130.2.1
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.123
636                         l->n_ap = MKAP(m);
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.123
644                         l->n_ap = MKAP(m);
ragge
1.72
645                         nfree(p);
646                         return clocal(l);
ragge
1.1
647                 }
ragge
1.17
648                 if (DEUNSIGN(p->n_type) == SHORT &&
649                     DEUNSIGN(l->n_type) == SHORT) {
650                         nfree(p);
651                         p = l;
652                 }
ragge
1.36
653                 if ((p->n_type == CHAR || p->n_type == UCHAR ||
654                     p->n_type == SHORT || p->n_type == USHORT) &&
655                     (l->n_type == FLOAT || l->n_type == DOUBLE ||
656                     l->n_type == LDOUBLE)) {
ragge
1.123
657                         p = block(SCONVpNILp->n_typep->n_dfp->n_ap);
ragge
1.36
658                         p->n_left->n_type = INT;
659                         return p;
660                 }
ragge
1.1
661                 break;
662
ragge
1.15
663         case MOD:
664         case DIV:
665                 if (o == DIV && p->n_type != CHAR && p->n_type != SHORT)
666                         break;
ragge
1.16
667                 if (o == MOD && p->n_type != CHAR && p->n_type != SHORT)
ragge
1.15
668                         break;
669                 /* make it an int division by inserting conversions */
ragge
1.123
670                 p->n_left = block(SCONVp->n_leftNILINT0MKAP(INT));
671                 p->n_right = block(SCONVp->n_rightNILINT0MKAP(INT));
672                 p = block(SCONVpNILp->n_type0MKAP(p->n_type));
ragge
1.15
673                 p->n_left->n_type = INT;
674                 break;
675
ragge
1.1
676         case PMCONV:
677         case PVCONV:
ragge
1.74
678                 r = p;
679                 p = buildtree(o == PMCONV ? MUL : DIVp->n_leftp->n_right);
680                 nfree(r);
681                 break;
ragge
1.1
682
ragge
1.31
683         case FORCE:
ragge
1.32
684                 /* put return value in return reg */
685                 p->n_op = ASSIGN;
686                 p->n_right = p->n_left;
ragge
1.123
687                 p->n_left = block(REGNILNILp->n_type0MKAP(INT));
ragge
1.53
688                 p->n_left->n_rval = p->n_left->n_type == BOOL ? 
689                     RETREG(CHAR) : RETREG(p->n_type);
ragge
1.31
690                 break;
ragge
1.39
691
692         case LS:
693         case RS:
ragge
1.127
694                 /* shift count must be in a char */
ragge
1.39
695                 if (p->n_right->n_type == CHAR || p->n_right->n_type == UCHAR)
696                         break;
ragge
1.127
697                 p->n_right = block(SCONVp->n_rightNILCHAR0MKAP(CHAR));
ragge
1.39
698                 break;
ragge
1.108
699 #if defined(os_openbsd)
700                 /* If not using pcc struct return */
701         case STASG:
702                 r = p->n_right;
703                 if (r->n_op != STCALL && r->n_op != USTCALL)
704                         break;
ragge
1.123
705                 m = tsize(BTYPE(r->n_type), r->n_dfr->n_ap);
ragge
1.108
706                 if (m == SZCHAR)
707                         m = CHAR;
708                 else if (m == SZSHORT)
709                         m = SHORT;
710                 else if (m == SZINT)
711                         m = INT;
712                 else if (m == SZLONGLONG)
713                         m = LONGLONG;
714                 else
715                         break;
716
717                 l = buildtree(ADDROFp->n_leftNIL);
718                 nfree(p);
719
720                 r->n_op -= (STCALL-CALL);
721                 r->n_type = m;
722
723                 /* r = long, l = &struct */
724
ragge
1.123
725                 n = tempnode(0mr->n_dfr->n_ap);
ragge
1.108
726                 r = buildtree(ASSIGNccopy(n), r);
727
ragge
1.123
728                 s = tempnode(0l->n_typel->n_dfl->n_ap);
ragge
1.108
729                 l = buildtree(ASSIGNccopy(s), l);
730
731                 p = buildtree(COMOPrl);
732
733                 l = buildtree(CAST,
ragge
1.123
734                     block(NAMENILNILm|PTR0MKAP(m)), ccopy(s));
ragge
1.108
735                 r = l->n_right;
736                 nfree(l->n_left);
737                 nfree(l);
738
739                 r = buildtree(ASSIGNbuildtree(UMULrNIL), n);
740                 p = buildtree(COMOPpr);
741                 p = buildtree(COMOPps);
742                 break;
743 #endif
ragge
1.1
744         }
ragge
1.54
745 #ifdef PCC_DEBUG
746         if (xdebug) {
747                 printf("clocal end: %p\n"p);
748                 fwalk(peprint0);
749         }
750 #endif
ragge
1.1
751         return(p);
752 }
753
ragge
1.64
754 /*
755  * Change CALL references to either direct (static) or PLT.
756  */
757 static void
ragge
1.88
758 fixnames(NODE *pvoid *arg)
ragge
1.64
759 {
gmcgarry
1.86
760 #if !defined(PECOFFABI)
761
ragge
1.64
762         struct symtab *sp;
ragge
1.123
763         struct attr *ap;
ragge
1.64
764         NODE *q;
765         char *c;
766         int isu;
767
768         if ((cdope(p->n_op) & CALLFLG) == 0)
769                 return;
770         isu = 0;
771         q = p->n_left;
ragge
1.123
772         ap = q->n_ap;
ragge
1.64
773         if (q->n_op == UMUL)
774                 q = q->n_leftisu = 1;
gmcgarry
1.83
775
ragge
1.64
776         if (q->n_op == PLUS && q->n_left->n_op == TEMP &&
777             q->n_right->n_op == ICON) {
778                 sp = q->n_right->n_sp;
779
ragge
1.65
780                 if (sp == NULL)
781                         return/* nothing to do */
ragge
1.64
782                 if (sp->sclass == STATIC && !ISFTN(sp->stype))
783                         return/* function pointer */
784
785                 if (sp->sclass != STATIC && sp->sclass != EXTERN &&
786                     sp->sclass != EXTDEF)
787                         cerror("fixnames");
ragge
1.96
788                 c = NULL;
gmcgarry
1.83
789 #if defined(ELFABI)
790
ragge
1.96
791                 if (sp->soname == NULL ||
792                     (c = strstr(sp->soname"@GOT")) == NULL)
ragge
1.64
793                         cerror("fixnames2");
794                 if (isu) {
795                         memcpy(c"@PLT"sizeof("@PLT"));
796                 } else
797                         *c = 0;
gmcgarry
1.83
798
799 #elif defined(MACHOABI)
800
ragge
1.96
801                 if (sp->soname == NULL ||
802                     ((c = strstr(sp->soname"$non_lazy_ptr")) == NULL &&
803                     (c = strstr(sp->soname"-L")) == NULL))
gmcgarry
1.83
804                                 cerror("fixnames2");
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.123
842         sp->sap = MKAP(p->n_type);
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);
848
849         defloc(sp);
ragge
1.123
850         ninval(0sp->sap->atypszp);
ragge
1.67
851
852         p->n_op = NAME;
853         p->n_lval = 0;
854         p->n_sp = sp;
ragge
1.1
855 }
856
857 /*ARGSUSED*/
858 int
859 andable(NODE *p)
860 {
ragge
1.64
861         return(1);      /* all names can have & taken on them */
ragge
1.1
862 }
863
864 /*
865  * at the end of the arguments of a ftn, set the automatic offset
866  */
867 void
868 cendarg()
869 {
870         autooff = AUTOINIT;
871 }
872
873 /*
ragge
1.36
874  * Return 1 if a variable of type type is OK to put in register.
875  */
876 int
877 cisreg(TWORD t)
878 {
879         if (t == FLOAT || t == DOUBLE || t == LDOUBLE)
880                 return 0/* not yet */
881         return 1;
882 }
883
884 /*
ragge
1.1
885  * return a node, for structure references, which is suitable for
886  * being added to a pointer of type t, in order to be off bits offset
887  * into a structure
888  * t, d, and s are the type, dimension offset, and sizeoffset
889  * For pdp10, return the type-specific index number which calculation
890  * is based on its size. For example, short a[3] would return 3.
891  * Be careful about only handling first-level pointers, the following
892  * indirections must be fullword.
893  */
894 NODE *
ragge
1.123
895 offcon(OFFSZ offTWORD tunion dimfun *dstruct attr *ap)
ragge
1.1
896 {
897         register NODE *p;
898
899         if (xdebug)
900                 printf("offcon: OFFSZ %lld type %x dim %p siz %d\n",
ragge
1.123
901                     offtd0);
ragge
1.1
902
903         p = bcon(0);
904         p->n_lval = off/SZCHAR/* Default */
905         return(p);
906 }
907
908 /*
909  * Allocate off bits on the stack.  p is a tree that when evaluated
ragge
1.45
910  * is the multiply count for off, t is a storeable node where to write
ragge
1.1
911  * the allocated address.
912  */
913 void
914 spalloc(NODE *tNODE *pOFFSZ off)
915 {
916         NODE *sp;
917
ragge
1.45
918         p = buildtree(MULpbcon(off/SZCHAR)); /* XXX word alignment? */
919
920         /* sub the size from sp */
ragge
1.123
921         sp = block(REGNILNILp->n_type0MKAP(INT));
ragge
1.45
922         sp->n_lval = 0;
923         sp->n_rval = STKREG;
924         ecomp(buildtree(MINUSEQspp));
ragge
1.1
925
pantzer
1.100
926 #ifdef MACHOABI 
927         /* align to 16 bytes */
ragge
1.123
928         sp = block(REGNILNILp->n_type0MKAP(INT));
pantzer
1.100
929         sp->n_lval = 0;
930         sp->n_rval = STKREG;
931         ecomp(buildtree(PLUSEQspbcon(15)));
932         
ragge
1.123
933         sp = block(REGNILNILp->n_type0MKAP(INT));
pantzer
1.100
934         sp->n_lval = 0;
935         sp->n_rval = STKREG;
936         ecomp(buildtree(RSEQspbcon(4)));