Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20110216172121

Diff

Diff from 1.128 to:

Annotations

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

Annotated File View

ragge
1.128
1 /*      $Id: local.c,v 1.128 2011/02/16 17:21:21 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.1
374
ragge
1.54
375 #ifdef PCC_DEBUG
376         if (xdebug) {
377                 printf("clocal: %p\n"p);
378                 fwalk(peprint0);
379         }
380 #endif
ragge
1.1
381         switcho = p->n_op ){
382
383         case NAME:
384                 if ((q = p->n_sp) == NULL)
385                         return p/* Nothing to care about */
386
387                 switch (q->sclass) {
388
389                 case PARAM:
390                 case AUTO:
391                         /* fake up a structure reference */
392                         r = block(REGNILNILPTR+STRTY00);
393                         r->n_lval = 0;
394                         r->n_rval = FPREG;
395                         p = stref(block(STREFrp000));
396                         break;
397
ragge
1.65
398                 case USTATIC:
399                         if (kflag == 0)
400                                 break;
401                         /* FALLTHROUGH */
ragge
1.1
402                 case STATIC:
ragge
1.73
403 #ifdef TLS
404                         if (q->sflags & STLS) {
405                                 p = tlsref(p);
406                                 break;
407                         }
408 #endif
ragge
1.60
409                         if (kflag == 0) {
410                                 if (q->slevel == 0)
411                                         break;
412                                 p->n_lval = 0;
ragge
1.64
413                         } else if (blevel > 0)
ragge
1.60
414                                 p = picstatic(p);
ragge
1.1
415                         break;
416
417                 case REGISTER:
418                         p->n_op = REG;
419                         p->n_lval = 0;
420                         p->n_rval = q->soffset;
421                         break;
422
ragge
1.60
423                 case EXTERN:
424                 case EXTDEF:
ragge
1.73
425 #ifdef TLS
426                         if (q->sflags & STLS) {
427                                 p = tlsref(p);
428                                 break;
429                         }
430 #endif
gmcgarry
1.103
431
432 #ifdef os_win32
433                         if (q->sflags & SDLLINDIRECT)
434                                 p = import(p);
435 #endif
ragge
1.60
436                         if (kflag == 0)
437                                 break;
ragge
1.64
438                         if (blevel > 0)
ragge
1.60
439                                 p = picext(p);
440                         break;
441                 }
ragge
1.1
442                 break;
443
ragge
1.65
444         case ADDROF:
445                 if (kflag == 0 || blevel == 0)
446                         break;
447                 /* char arrays may end up here */
448                 l = p->n_left;
449                 if (l->n_op != NAME ||
450                     (l->n_type != ARY+CHAR && l->n_type != ARY+WCHAR_TYPE))
451                         break;
452                 l = p;
453                 p = picstatic(p->n_left);
454                 nfree(l);
455                 if (p->n_op != UMUL)
456                         cerror("ADDROF error");
457                 l = p;
458                 p = p->n_left;
459                 nfree(l);
460                 break;
461
ragge
1.62
462         case UCALL:
463         case USTCALL:
ragge
1.60
464                 if (kflag == 0)
465                         break;
gmcgarry
1.86
466 #if defined(ELFABI)
ragge
1.62
467                 /* Change to CALL node with ebx as argument */
ragge
1.123
468                 l = block(REGNILNILINT0MKAP(INT));
ragge
1.62
469                 l->n_rval = EBX;
470                 p->n_right = buildtree(ASSIGNl,
ragge
1.123
471                     tempnode(gotnrINT0MKAP(INT)));
ragge
1.62
472                 p->n_op -= (UCALL-CALL);
gmcgarry
1.86
473 #endif
pantzer
1.89
474         
475         /* FALLTHROUGH */
476 #if defined(MACHOABI)
477         case CALL:
478         case STCALL:
479                 if (p->n_type == VOID)
480                         break;
481
ragge
1.123
482                 r = tempnode(0p->n_typep->n_dfp->n_ap);
pantzer
1.89
483                 l = tcopy(r);
484                 p = buildtree(COMOPbuildtree(ASSIGNrp), l);
485 #endif
486                         
ragge
1.10
487                 break;
ragge
1.64
488
ragge
1.8
489         case CBRANCH:
490                 l = p->n_left;
491
492                 /*
ragge
1.77
493                  * Remove unnecessary conversion ops.
ragge
1.8
494                  */
ragge
1.128
495                 if (!clogop(l->n_op) || l->n_left->n_op != SCONV)
496                         break;
497                 if (coptype(l->n_op) != BITYPE)
498                         break;
499                 if (l->n_right->n_op != ICON)
500                         break;
501                 r = l->n_left->n_left;
502                 if (r->n_type >= FLOAT)
503                         break;
504                 if (toolarge(r->n_typel->n_right->n_lval))
505                         break;
506                 l->n_right->n_type = r->n_type;
507                 if (l->n_op >= ULE && l->n_op <= UGT)
508                         l->n_op -= (UGT-ULE);
509                 p->n_left = buildtree(l->n_oprl->n_right);
510                 nfree(l->n_left);
511                 nfree(l);
ragge
1.8
512                 break;
513
ragge
1.1
514         case PCONV:
ragge
1.40
515                 /* Remove redundant PCONV's. Be careful */
ragge
1.1
516                 l = p->n_left;
ragge
1.40
517                 if (l->n_op == ICON) {
518                         l->n_lval = (unsigned)l->n_lval;
519                         goto delp;
520                 }
521                 if (l->n_type < INT || l->n_type == LONGLONG || 
522                     l->n_type == ULONGLONG) {
523                         /* float etc? */
524                         p->n_left = block(SCONVlNIL,
ragge
1.123
525                             UNSIGNED0MKAP(UNSIGNED));
ragge
1.40
526                         break;
527                 }
528                 /* if left is SCONV, cannot remove */
529                 if (l->n_op == SCONV)
ragge
1.1
530                         break;
ragge
1.54
531
532                 /* avoid ADDROF TEMP */
533                 if (l->n_op == ADDROF && l->n_left->n_op == TEMP)
534                         break;
535
ragge
1.40
536                 /* if conversion to another pointer type, just remove */
537                 if (p->n_type > BTMASK && l->n_type > BTMASK)
538                         goto delp;
539                 break;
540
541         delp:   l->n_type = p->n_type;
ragge
1.1
542                 l->n_qual = p->n_qual;
543                 l->n_df = p->n_df;
ragge
1.123
544                 l->n_ap = p->n_ap;
ragge
1.1
545                 nfree(p);
546                 p = l;
547                 break;
ragge
1.40
548                 
ragge
1.1
549         case SCONV:
ragge
1.109
550                 if (p->n_left->n_op == COMOP)
551                         break;  /* may propagate wrong type later */
ragge
1.1
552                 l = p->n_left;
ragge
1.4
553
ragge
1.25
554                 if (p->n_type == l->n_type) {
555                         nfree(p);
556                         return l;
557                 }
558
ragge
1.4
559                 if ((p->n_type & TMASK) == 0 && (l->n_type & TMASK) == 0 &&
ragge
1.123
560                     btattr[p->n_type].atypsz == btattr[l->n_type].atypsz) {
ragge
1.4
561                         if (p->n_type != FLOAT && p->n_type != DOUBLE &&
ragge
1.15
562                             l->n_type != FLOAT && l->n_type != DOUBLE &&
563                             l->n_type != LDOUBLE && p->n_type != LDOUBLE) {
ragge
1.41
564                                 if (l->n_op == NAME || l->n_op == UMUL ||
565                                     l->n_op == TEMP) {
ragge
1.15
566                                         l->n_type = p->n_type;
567                                         nfree(p);
568                                         return l;
569                                 }
ragge
1.4
570                         }
ragge
1.6
571                 }
572
ragge
1.42
573                 if (DEUNSIGN(p->n_type) == INT && DEUNSIGN(l->n_type) == INT &&
574                     coptype(l->n_op) == BITYPE) {
575                         l->n_type = p->n_type;
576                         nfree(p);
577                         return l;
578                 }
579
ragge
1.3
580                 o = l->n_op;
ragge
1.4
581                 m = p->n_type;
ragge
1.1
582
583                 if (o == ICON) {
584                         CONSZ val = l->n_lval;
585
ragge
1.29
586                         if (!ISPTR(m)) /* Pointers don't need to be conv'd */
587                             switch (m) {
ragge
1.52
588                         case BOOL:
ragge
1.125
589                                 l->n_lval = nncon(l) ? (l->n_lval != 0) : 1;
590                                 l->n_sp = NULL;
ragge
1.52
591                                 break;
ragge
1.1
592                         case CHAR:
593                                 l->n_lval = (char)val;
594                                 break;
595                         case UCHAR:
596                                 l->n_lval = val & 0377;
597                                 break;
ragge
1.15
598                         case SHORT:
ragge
1.1
599                                 l->n_lval = (short)val;
600                                 break;
ragge
1.15
601                         case USHORT:
ragge
1.1
602                                 l->n_lval = val & 0177777;
603                                 break;
ragge
1.15
604                         case ULONG:
ragge
1.1
605                         case UNSIGNED:
606                                 l->n_lval = val & 0xffffffff;
607                                 break;
ragge
1.15
608                         case LONG:
ragge
1.1
609                         case INT:
610                                 l->n_lval = (int)val;
611                                 break;
612                         case LONGLONG:
613                                 l->n_lval = (long long)val;
ragge
1.15
614                                 break;
ragge
1.1
615                         case ULONGLONG:
616                                 l->n_lval = val;
617                                 break;
618                         case VOID:
619                                 break;
ragge
1.14
620                         case LDOUBLE:
ragge
1.1
621                         case DOUBLE:
622                         case FLOAT:
623                                 l->n_op = FCON;
624                                 l->n_dcon = val;
625                                 break;
626                         default:
627                                 cerror("unknown type %d"m);
628                         }
629                         l->n_type = m;
ragge
1.123
630                         l->n_ap = MKAP(m);
ragge
1.1
631                         nfree(p);
632                         return l;
ragge
1.72
633                 } else if (l->n_op == FCON) {
gmcgarry
1.107
634                         l->n_lval = (CONSZ)l->n_dcon;
ragge
1.72
635                         l->n_sp = NULL;
636                         l->n_op = ICON;
637                         l->n_type = m;
ragge
1.123
638                         l->n_ap = MKAP(m);
ragge
1.72
639                         nfree(p);
640                         return clocal(l);
ragge
1.1
641                 }
ragge
1.17
642                 if (DEUNSIGN(p->n_type) == SHORT &&
643                     DEUNSIGN(l->n_type) == SHORT) {
644                         nfree(p);
645                         p = l;
646                 }
ragge
1.36
647                 if ((p->n_type == CHAR || p->n_type == UCHAR ||
648                     p->n_type == SHORT || p->n_type == USHORT) &&
649                     (l->n_type == FLOAT || l->n_type == DOUBLE ||
650                     l->n_type == LDOUBLE)) {
ragge
1.123
651                         p = block(SCONVpNILp->n_typep->n_dfp->n_ap);
ragge
1.36
652                         p->n_left->n_type = INT;
653                         return p;
654                 }
ragge
1.1
655                 break;
656
ragge
1.15
657         case MOD:
658         case DIV:
659                 if (o == DIV && p->n_type != CHAR && p->n_type != SHORT)
660                         break;
ragge
1.16
661                 if (o == MOD && p->n_type != CHAR && p->n_type != SHORT)
ragge
1.15
662                         break;
663                 /* make it an int division by inserting conversions */
ragge
1.123
664                 p->n_left = block(SCONVp->n_leftNILINT0MKAP(INT));
665                 p->n_right = block(SCONVp->n_rightNILINT0MKAP(INT));
666                 p = block(SCONVpNILp->n_type0MKAP(p->n_type));
ragge
1.15
667                 p->n_left->n_type = INT;
668                 break;
669
ragge
1.1
670         case PMCONV:
671         case PVCONV:
ragge
1.74
672                 r = p;
673                 p = buildtree(o == PMCONV ? MUL : DIVp->n_leftp->n_right);
674                 nfree(r);
675                 break;
ragge
1.1
676
ragge
1.31
677         case FORCE:
ragge
1.32
678                 /* put return value in return reg */
679                 p->n_op = ASSIGN;
680                 p->n_right = p->n_left;
ragge
1.123
681                 p->n_left = block(REGNILNILp->n_type0MKAP(INT));
ragge
1.53
682                 p->n_left->n_rval = p->n_left->n_type == BOOL ? 
683                     RETREG(CHAR) : RETREG(p->n_type);
ragge
1.31
684                 break;
ragge
1.39
685
686         case LS:
687         case RS:
ragge
1.127
688                 /* shift count must be in a char */
ragge
1.39
689                 if (p->n_right->n_type == CHAR || p->n_right->n_type == UCHAR)
690                         break;
ragge
1.127
691                 p->n_right = block(SCONVp->n_rightNILCHAR0MKAP(CHAR));
ragge
1.39
692                 break;
ragge
1.108
693 #if defined(os_openbsd)
694                 /* If not using pcc struct return */
695         case STASG:
696                 r = p->n_right;
697                 if (r->n_op != STCALL && r->n_op != USTCALL)
698                         break;
ragge
1.123
699                 m = tsize(BTYPE(r->n_type), r->n_dfr->n_ap);
ragge
1.108
700                 if (m == SZCHAR)
701                         m = CHAR;
702                 else if (m == SZSHORT)
703                         m = SHORT;
704                 else if (m == SZINT)
705                         m = INT;
706                 else if (m == SZLONGLONG)
707                         m = LONGLONG;
708                 else
709                         break;
710
711                 l = buildtree(ADDROFp->n_leftNIL);
712                 nfree(p);
713
714                 r->n_op -= (STCALL-CALL);
715                 r->n_type = m;
716
717                 /* r = long, l = &struct */
718
ragge
1.123
719                 n = tempnode(0mr->n_dfr->n_ap);
ragge
1.108
720                 r = buildtree(ASSIGNccopy(n), r);
721
ragge
1.123
722                 s = tempnode(0l->n_typel->n_dfl->n_ap);
ragge
1.108
723                 l = buildtree(ASSIGNccopy(s), l);
724
725                 p = buildtree(COMOPrl);
726
727                 l = buildtree(CAST,
ragge
1.123
728                     block(NAMENILNILm|PTR0MKAP(m)), ccopy(s));
ragge
1.108
729                 r = l->n_right;
730                 nfree(l->n_left);
731                 nfree(l);
732
733                 r = buildtree(ASSIGNbuildtree(UMULrNIL), n);
734                 p = buildtree(COMOPpr);
735                 p = buildtree(COMOPps);
736                 break;
737 #endif
ragge
1.1
738         }
ragge
1.54
739 #ifdef PCC_DEBUG
740         if (xdebug) {
741                 printf("clocal end: %p\n"p);
742                 fwalk(peprint0);
743         }
744 #endif
ragge
1.1
745         return(p);
746 }
747
ragge
1.64
748 /*
749  * Change CALL references to either direct (static) or PLT.
750  */
751 static void
ragge
1.88
752 fixnames(NODE *pvoid *arg)
ragge
1.64
753 {
gmcgarry
1.86
754 #if !defined(PECOFFABI)
755
ragge
1.64
756         struct symtab *sp;
ragge
1.123
757         struct attr *ap;
ragge
1.64
758         NODE *q;
759         char *c;
760         int isu;
761
762         if ((cdope(p->n_op) & CALLFLG) == 0)
763                 return;
764         isu = 0;
765         q = p->n_left;
ragge
1.123
766         ap = q->n_ap;
ragge
1.64
767         if (q->n_op == UMUL)
768                 q = q->n_leftisu = 1;
gmcgarry
1.83
769
ragge
1.64
770         if (q->n_op == PLUS && q->n_left->n_op == TEMP &&
771             q->n_right->n_op == ICON) {
772                 sp = q->n_right->n_sp;
773
ragge
1.65
774                 if (sp == NULL)
775                         return/* nothing to do */
ragge
1.64
776                 if (sp->sclass == STATIC && !ISFTN(sp->stype))
777                         return/* function pointer */
778
779                 if (sp->sclass != STATIC && sp->sclass != EXTERN &&
780                     sp->sclass != EXTDEF)
781                         cerror("fixnames");
ragge
1.96
782                 c = NULL;
gmcgarry
1.83
783 #if defined(ELFABI)
784
ragge
1.96
785                 if (sp->soname == NULL ||
786                     (c = strstr(sp->soname"@GOT")) == NULL)
ragge
1.64
787                         cerror("fixnames2");
788                 if (isu) {
789                         memcpy(c"@PLT"sizeof("@PLT"));
790                 } else
791                         *c = 0;
gmcgarry
1.83
792
793 #elif defined(MACHOABI)
794
ragge
1.96
795                 if (sp->soname == NULL ||
796                     ((c = strstr(sp->soname"$non_lazy_ptr")) == NULL &&
797                     (c = strstr(sp->soname"-L")) == NULL))
gmcgarry
1.83
798                                 cerror("fixnames2");
799                 if (isu) {
800                         *c = 0;
801                         addstub(&stublistsp->soname+1);
802                         strcpy(c"$stub");
803                 } else 
804                         *c = 0;
805
806 #endif
807
ragge
1.64
808                 nfree(q->n_left);
809                 q = q->n_right;
810                 if (isu)
811                         nfree(p->n_left->n_left);
812                 nfree(p->n_left);
813                 p->n_left = q;
ragge
1.123
814                 q->n_ap = ap;
ragge
1.64
815         }
gmcgarry
1.86
816 #endif
ragge
1.64
817 }
818
ragge
1.117
819 static void mangle(NODE *p);
820
ragge
1.1
821 void
822 myp2tree(NODE *p)
823 {
ragge
1.67
824         struct symtab *sp;
825
ragge
1.64
826         if (kflag)
ragge
1.117
827                 fixnames(p0);
828
829         mangle(p);
830
ragge
1.67
831         if (p->n_op != FCON)
832                 return;
833
834         sp = IALLOC(sizeof(struct symtab));
835         sp->sclass = STATIC;
ragge
1.123
836         sp->sap = MKAP(p->n_type);
ragge
1.67
837         sp->slevel = 1/* fake numeric label */
ragge
1.70
838         sp->soffset = getlab();
ragge
1.67
839         sp->sflags = 0;
ragge
1.70
840         sp->stype = p->n_type;
841         sp->squal = (CON >> TSHIFT);
842
843         defloc(sp);
ragge
1.123
844         ninval(0sp->sap->atypszp);
ragge
1.67
845
846         p->n_op = NAME;
847         p->n_lval = 0;
848         p->n_sp = sp;
ragge
1.1
849 }
850
851 /*ARGSUSED*/
852 int
853 andable(NODE *p)
854 {
ragge
1.64
855         return(1);      /* all names can have & taken on them */
ragge
1.1
856 }
857
858 /*
859  * at the end of the arguments of a ftn, set the automatic offset
860  */
861 void
862 cendarg()
863 {
864         autooff = AUTOINIT;
865 }
866
867 /*
ragge
1.36
868  * Return 1 if a variable of type type is OK to put in register.
869  */
870 int
871 cisreg(TWORD t)
872 {
873         if (t == FLOAT || t == DOUBLE || t == LDOUBLE)
874                 return 0/* not yet */
875         return 1;
876 }
877
878 /*
ragge
1.1
879  * return a node, for structure references, which is suitable for
880  * being added to a pointer of type t, in order to be off bits offset
881  * into a structure
882  * t, d, and s are the type, dimension offset, and sizeoffset
883  * For pdp10, return the type-specific index number which calculation
884  * is based on its size. For example, short a[3] would return 3.
885  * Be careful about only handling first-level pointers, the following
886  * indirections must be fullword.
887  */
888 NODE *
ragge
1.123
889 offcon(OFFSZ offTWORD tunion dimfun *dstruct attr *ap)
ragge
1.1
890 {
891         register NODE *p;
892
893         if (xdebug)
894                 printf("offcon: OFFSZ %lld type %x dim %p siz %d\n",
ragge
1.123
895                     offtd0);
ragge
1.1
896
897         p = bcon(0);
898         p->n_lval = off/SZCHAR/* Default */
899         return(p);
900 }
901
902 /*
903  * Allocate off bits on the stack.  p is a tree that when evaluated
ragge
1.45
904  * is the multiply count for off, t is a storeable node where to write
ragge
1.1
905  * the allocated address.
906  */
907 void
908 spalloc(NODE *tNODE *pOFFSZ off)
909 {
910         NODE *sp;
911
ragge
1.45
912         p = buildtree(MULpbcon(off/SZCHAR)); /* XXX word alignment? */
913
914         /* sub the size from sp */
ragge
1.123
915         sp = block(REGNILNILp->n_type0MKAP(INT));
ragge
1.45
916         sp->n_lval = 0;
917         sp->n_rval = STKREG;
918         ecomp(buildtree(MINUSEQspp));
ragge
1.1
919
pantzer
1.100
920 #ifdef MACHOABI 
921         /* align to 16 bytes */
ragge
1.123
922         sp = block(REGNILNILp->n_type0MKAP(INT));
pantzer
1.100
923         sp->n_lval = 0;
924         sp->n_rval = STKREG;
925         ecomp(buildtree(PLUSEQspbcon(15)));
926         
ragge
1.123
927         sp = block(REGNILNILp->n_type0MKAP(INT));
pantzer
1.100
928         sp->n_lval = 0;
929         sp->n_rval = STKREG;
930         ecomp(buildtree(RSEQspbcon(4)));
931         
ragge
1.123
932         sp = block(REGNILNILp->n_type0MKAP(INT));
pantzer
1.100
933         sp->n_lval = 0;
934         sp->n_rval = STKREG;
935         ecomp(buildtree(LSEQspbcon(4)));
936 #endif
937         
938
ragge
1.1
939         /* save the address of sp */
ragge
1.123
940         sp = block(REGNILNILPTR+INTt->n_dft->n_ap);
ragge
1.1
941         sp->n_lval = 0;
942         sp->n_rval = STKREG;
943         t->n_type = sp->n_type;
944         ecomp(buildtree(ASSIGNtsp)); /* Emit! */
945
946 }
947
948