Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20110419200510

Diff

Diff from 1.141 to:

Annotations

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

Annotated File View

ragge
1.141
1 /*      $Id: local.c,v 1.141 2011/04/19 20:05: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;
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.138
470                 l = block(REGNILNILINT00);
ragge
1.62
471                 l->n_rval = EBX;
472                 p->n_right = buildtree(ASSIGNl,
ragge
1.138
473                     tempnode(gotnrINT00));
ragge
1.62
474                 p->n_op -= (UCALL-CALL);
gmcgarry
1.86
475 #endif
pantzer
1.89
476         
477         /* FALLTHROUGH */
478 #if defined(MACHOABI)
479         case CALL:
480         case STCALL:
481                 if (p->n_type == VOID)
482                         break;
483
ragge
1.123
484                 r = tempnode(0p->n_typep->n_dfp->n_ap);
pantzer
1.89
485                 l = tcopy(r);
486                 p = buildtree(COMOPbuildtree(ASSIGNrp), l);
487 #endif
488                         
ragge
1.10
489                 break;
ragge
1.64
490
ragge
1.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 || 
ragge
1.134
527                     l->n_type == ULONGLONG || l->n_type == BOOL) {
ragge
1.40
528                         /* float etc? */
529                         p->n_left = block(SCONVlNIL,
ragge
1.138
530                             UNSIGNED00);
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.139
565                     tsize(p->n_typep->n_dfp->n_ap) ==
566                     tsize(l->n_typel->n_dfl->n_ap)) {
ragge
1.4
567                         if (p->n_type != FLOAT && p->n_type != DOUBLE &&
ragge
1.15
568                             l->n_type != FLOAT && l->n_type != DOUBLE &&
569                             l->n_type != LDOUBLE && p->n_type != LDOUBLE) {
ragge
1.41
570                                 if (l->n_op == NAME || l->n_op == UMUL ||
571                                     l->n_op == TEMP) {
ragge
1.15
572                                         l->n_type = p->n_type;
573                                         nfree(p);
574                                         return l;
575                                 }
ragge
1.4
576                         }
ragge
1.6
577                 }
578
ragge
1.42
579                 if (DEUNSIGN(p->n_type) == INT && DEUNSIGN(l->n_type) == INT &&
ragge
1.131
580                     coptype(l->n_op) == BITYPE && l->n_op != COMOP &&
581                     l->n_op != QUEST) {
ragge
1.42
582                         l->n_type = p->n_type;
583                         nfree(p);
584                         return l;
585                 }
586
ragge
1.3
587                 o = l->n_op;
ragge
1.4
588                 m = p->n_type;
ragge
1.1
589
590                 if (o == ICON) {
591                         CONSZ val = l->n_lval;
592
ragge
1.29
593                         if (!ISPTR(m)) /* Pointers don't need to be conv'd */
594                             switch (m) {
ragge
1.52
595                         case BOOL:
ragge
1.125
596                                 l->n_lval = nncon(l) ? (l->n_lval != 0) : 1;
597                                 l->n_sp = NULL;
ragge
1.52
598                                 break;
ragge
1.1
599                         case CHAR:
600                                 l->n_lval = (char)val;
601                                 break;
602                         case UCHAR:
603                                 l->n_lval = val & 0377;
604                                 break;
ragge
1.15
605                         case SHORT:
ragge
1.1
606                                 l->n_lval = (short)val;
607                                 break;
ragge
1.15
608                         case USHORT:
ragge
1.1
609                                 l->n_lval = val & 0177777;
610                                 break;
ragge
1.15
611                         case ULONG:
ragge
1.1
612                         case UNSIGNED:
613                                 l->n_lval = val & 0xffffffff;
614                                 break;
ragge
1.15
615                         case LONG:
ragge
1.1
616                         case INT:
617                                 l->n_lval = (int)val;
618                                 break;
619                         case LONGLONG:
620                                 l->n_lval = (long long)val;
ragge
1.15
621                                 break;
ragge
1.1
622                         case ULONGLONG:
623                                 l->n_lval = val;
624                                 break;
625                         case VOID:
626                                 break;
ragge
1.14
627                         case LDOUBLE:
ragge
1.1
628                         case DOUBLE:
629                         case FLOAT:
630                                 l->n_op = FCON;
631                                 l->n_dcon = val;
632                                 break;
633                         default:
634                                 cerror("unknown type %d"m);
635                         }
636                         l->n_type = m;
ragge
1.138
637                         l->n_ap = 0;
ragge
1.1
638                         nfree(p);
639                         return l;
ragge
1.72
640                 } else if (l->n_op == FCON) {
gmcgarry
1.107
641                         l->n_lval = (CONSZ)l->n_dcon;
ragge
1.72
642                         l->n_sp = NULL;
643                         l->n_op = ICON;
644                         l->n_type = m;
ragge
1.138
645                         l->n_ap = 0;
ragge
1.72
646                         nfree(p);
647                         return clocal(l);
ragge
1.1
648                 }
ragge
1.36
649                 if ((p->n_type == CHAR || p->n_type == UCHAR ||
650                     p->n_type == SHORT || p->n_type == USHORT) &&
651                     (l->n_type == FLOAT || l->n_type == DOUBLE ||
652                     l->n_type == LDOUBLE)) {
ragge
1.123
653                         p = block(SCONVpNILp->n_typep->n_dfp->n_ap);
ragge
1.36
654                         p->n_left->n_type = INT;
655                         return p;
656                 }
ragge
1.1
657                 break;
658
ragge
1.15
659         case MOD:
660         case DIV:
661                 if (o == DIV && p->n_type != CHAR && p->n_type != SHORT)
662                         break;
ragge
1.16
663                 if (o == MOD && p->n_type != CHAR && p->n_type != SHORT)
ragge
1.15
664                         break;
665                 /* make it an int division by inserting conversions */
ragge
1.138
666                 p->n_left = block(SCONVp->n_leftNILINT00);
667                 p->n_right = block(SCONVp->n_rightNILINT00);
668                 p = block(SCONVpNILp->n_type00);
ragge
1.15
669                 p->n_left->n_type = INT;
670                 break;
671
ragge
1.1
672         case PMCONV:
673         case PVCONV:
ragge
1.74
674                 r = p;
675                 p = buildtree(o == PMCONV ? MUL : DIVp->n_leftp->n_right);
676                 nfree(r);
677                 break;
ragge
1.1
678
ragge
1.31
679         case FORCE:
ragge
1.32
680                 /* put return value in return reg */
681                 p->n_op = ASSIGN;
682                 p->n_right = p->n_left;
ragge
1.138
683                 p->n_left = block(REGNILNILp->n_type00);
ragge
1.53
684                 p->n_left->n_rval = p->n_left->n_type == BOOL ? 
685                     RETREG(CHAR) : RETREG(p->n_type);
ragge
1.31
686                 break;
ragge
1.39
687
688         case LS:
689         case RS:
ragge
1.127
690                 /* shift count must be in a char */
ragge
1.39
691                 if (p->n_right->n_type == CHAR || p->n_right->n_type == UCHAR)
692                         break;
ragge
1.138
693                 p->n_right = block(SCONVp->n_rightNILCHAR00);
ragge
1.39
694                 break;
ragge
1.108
695 #if defined(os_openbsd)
696                 /* If not using pcc struct return */
697         case STASG:
698                 r = p->n_right;
699                 if (r->n_op != STCALL && r->n_op != USTCALL)
700                         break;
ragge
1.123
701                 m = tsize(BTYPE(r->n_type), r->n_dfr->n_ap);
ragge
1.108
702                 if (m == SZCHAR)
703                         m = CHAR;
704                 else if (m == SZSHORT)
705                         m = SHORT;
706                 else if (m == SZINT)
707                         m = INT;
708                 else if (m == SZLONGLONG)
709                         m = LONGLONG;
710                 else
711                         break;
712
713                 l = buildtree(ADDROFp->n_leftNIL);
714                 nfree(p);
715
716                 r->n_op -= (STCALL-CALL);
717                 r->n_type = m;
718
719                 /* r = long, l = &struct */
720
ragge
1.123
721                 n = tempnode(0mr->n_dfr->n_ap);
ragge
1.108
722                 r = buildtree(ASSIGNccopy(n), r);
723
ragge
1.123
724                 s = tempnode(0l->n_typel->n_dfl->n_ap);
ragge
1.108
725                 l = buildtree(ASSIGNccopy(s), l);
726
727                 p = buildtree(COMOPrl);
728
729                 l = buildtree(CAST,
ragge
1.138
730                     block(NAMENILNILm|PTR00), ccopy(s));
ragge
1.108
731                 r = l->n_right;
732                 nfree(l->n_left);
733                 nfree(l);
734
735                 r = buildtree(ASSIGNbuildtree(UMULrNIL), n);
736                 p = buildtree(COMOPpr);
737                 p = buildtree(COMOPps);
738                 break;
739 #endif
ragge
1.1
740         }
ragge
1.54
741 #ifdef PCC_DEBUG
742         if (xdebug) {
743                 printf("clocal end: %p\n"p);
744                 fwalk(peprint0);
745         }
746 #endif
ragge
1.1
747         return(p);
748 }
749
ragge
1.64
750 /*
751  * Change CALL references to either direct (static) or PLT.
752  */
753 static void
ragge
1.88
754 fixnames(NODE *pvoid *arg)
ragge
1.64
755 {
ragge
1.137
756 #if defined(ELFABI) || defined(MACHOABI)
gmcgarry
1.86
757
ragge
1.64
758         struct symtab *sp;
ragge
1.123
759         struct attr *ap;
ragge
1.64
760         NODE *q;
761         char *c;
762         int isu;
763
764         if ((cdope(p->n_op) & CALLFLG) == 0)
765                 return;
766         isu = 0;
767         q = p->n_left;
ragge
1.123
768         ap = q->n_ap;
ragge
1.64
769         if (q->n_op == UMUL)
770                 q = q->n_leftisu = 1;
gmcgarry
1.83
771
ragge
1.64
772         if (q->n_op == PLUS && q->n_left->n_op == TEMP &&
773             q->n_right->n_op == ICON) {
774                 sp = q->n_right->n_sp;
775
ragge
1.65
776                 if (sp == NULL)
777                         return/* nothing to do */
ragge
1.64
778                 if (sp->sclass == STATIC && !ISFTN(sp->stype))
779                         return/* function pointer */
780
781                 if (sp->sclass != STATIC && sp->sclass != EXTERN &&
782                     sp->sclass != EXTDEF)
783                         cerror("fixnames");
ragge
1.96
784                 c = NULL;
gmcgarry
1.83
785 #if defined(ELFABI)
786
ragge
1.96
787                 if (sp->soname == NULL ||
788                     (c = strstr(sp->soname"@GOT")) == NULL)
ragge
1.64
789                         cerror("fixnames2");
790                 if (isu) {
791                         memcpy(c"@PLT"sizeof("@PLT"));
792                 } else
793                         *c = 0;
gmcgarry
1.83
794
795 #elif defined(MACHOABI)
796
ragge
1.96
797                 if (sp->soname == NULL ||
798                     ((c = strstr(sp->soname"$non_lazy_ptr")) == NULL &&
799                     (c = strstr(sp->soname"-L")) == NULL))
gmcgarry
1.83
800                                 cerror("fixnames2");
801                 if (isu) {
802                         *c = 0;
803                         addstub(&stublistsp->soname+1);
804                         strcpy(c"$stub");
805                 } else 
806                         *c = 0;
807
808 #endif
809
ragge
1.64
810                 nfree(q->n_left);
811                 q = q->n_right;
812                 if (isu)
813                         nfree(p->n_left->n_left);
814                 nfree(p->n_left);
815                 p->n_left = q;
ragge
1.123
816                 q->n_ap = ap;
ragge
1.64
817         }
gmcgarry
1.86
818 #endif
ragge
1.64
819 }
820
ragge
1.117
821 static void mangle(NODE *p);
822
ragge
1.1
823 void
824 myp2tree(NODE *p)
825 {
ragge
1.67
826         struct symtab *sp;
827
ragge
1.64
828         if (kflag)
ragge
1.117
829                 fixnames(p0);
830
831         mangle(p);
832
ragge
1.67
833         if (p->n_op != FCON)
834                 return;
835
836         sp = IALLOC(sizeof(struct symtab));
837         sp->sclass = STATIC;
ragge
1.138
838         sp->sap = 0;
ragge
1.67
839         sp->slevel = 1/* fake numeric label */
ragge
1.70
840         sp->soffset = getlab();
ragge
1.67
841         sp->sflags = 0;
ragge
1.70
842         sp->stype = p->n_type;
843         sp->squal = (CON >> TSHIFT);
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) {
849                 sp->sname = sp->soname = inlalloc(32);
850                 snprintf(sp->sname32LABFMT "@GOTOFF", (int)sp->soffset);
851                 sp->sclass = EXTERN;
852                 sp->sflags = sp->slevel = 0;
853         }
ragge
1.67
854         p->n_op = NAME;
855         p->n_lval = 0;
856         p->n_sp = sp;
ragge
1.1
857 }
858
859 /*ARGSUSED*/
860 int
861 andable(NODE *p)
862 {
ragge
1.64
863         return(1);      /* all names can have & taken on them */
ragge
1.1
864 }
865
866 /*
867  * at the end of the arguments of a ftn, set the automatic offset
868  */
869 void
870 cendarg()
871 {
872         autooff = AUTOINIT;
873 }
874
875 /*
ragge
1.36
876  * Return 1 if a variable of type type is OK to put in register.
877  */
878 int
879 cisreg(TWORD t)
880 {
881         if (t == FLOAT || t == DOUBLE || t == LDOUBLE)
882                 return 0/* not yet */
883         return 1;
884 }
885
886 /*
ragge
1.1
887  * return a node, for structure references, which is suitable for
888  * being added to a pointer of type t, in order to be off bits offset
889  * into a structure
890  * t, d, and s are the type, dimension offset, and sizeoffset
891  * For pdp10, return the type-specific index number which calculation
892  * is based on its size. For example, short a[3] would return 3.
893  * Be careful about only handling first-level pointers, the following
894  * indirections must be fullword.
895  */
896 NODE *
ragge
1.123
897 offcon(OFFSZ offTWORD tunion dimfun *dstruct attr *ap)
ragge
1.1
898 {
899         register NODE *p;
900
901         if (xdebug)
902                 printf("offcon: OFFSZ %lld type %x dim %p siz %d\n",
ragge
1.123
903                     offtd0);
ragge
1.1
904
905         p = bcon(0);
906         p->n_lval = off/SZCHAR/* Default */
907         return(p);
908 }
909
910 /*
911  * Allocate off bits on the stack.  p is a tree that when evaluated
ragge
1.45
912  * is the multiply count for off, t is a storeable node where to write
ragge
1.1
913  * the allocated address.
914  */
915 void
916 spalloc(NODE *tNODE *pOFFSZ off)
917 {
918         NODE *sp;
919
ragge
1.45
920         p = buildtree(MULpbcon(off/SZCHAR)); /* XXX word alignment? */
921
922         /* sub the size from sp */
ragge
1.138
923         sp = block(REGNILNILp->n_type00);
ragge
1.45
924         sp->n_lval = 0;
925         sp->n_rval = STKREG;
926         ecomp(buildtree(MINUSEQspp));
ragge
1.1
927
pantzer
1.100
928 #ifdef MACHOABI 
929         /* align to 16 bytes */
ragge
1.138
930         sp = block(REGNILNILp->n_type00);
pantzer
1.100
931