Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20110121214758

Diff

Diff from 1.126 to:

Annotations

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

Annotated File View

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