Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20140524151953

Diff

Diff from 1.179 to:

Annotations

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

Annotated File View

ragge
1.179
1 /*      $Id: local.c,v 1.179 2014/05/24 15:19:53 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  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27
ragge
1.48
28 #include "pass1.h"
ragge
1.1
29
ragge
1.60
30 /*      this file contains code which is dependent on the target machine */
31
ragge
1.130
32 #ifdef notyet
ragge
1.57
33 /*
34  * Check if a constant is too large for a type.
35  */
36 static int
37 toolarge(TWORD tCONSZ con)
38 {
39         U_CONSZ ucon = con;
40
41         switch (t) {
42         case ULONGLONG:
43         case LONGLONG:
44                 break/* cannot be too large */
45 #define SCHK(i) case i: if (con > MAX_##i || con < MIN_##i) return 1; break
46 #define UCHK(i) case i: if (ucon > MAX_##i) return 1; break
47         SCHK(INT);
48         SCHK(SHORT);
otto
1.58
49         case BOOL:
ragge
1.57
50         SCHK(CHAR);
51         UCHK(UNSIGNED);
52         UCHK(USHORT);
53         UCHK(UCHAR);
54         default:
55                 cerror("toolarge");
56         }
57         return 0;
58 }
ragge
1.130
59 #endif
ragge
1.57
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));
plunky
1.166
78         s->name = newstring(namestrlen(name));
gmcgarry
1.83
79         DLIST_INSERT_BEFORE(listslink);
80 }
81
82 #endif
83
ragge
1.60
84 #define IALLOC(sz)      (isinlining ? permalloc(sz) : tmpalloc(sz))
gmcgarry
1.86
85
ragge
1.60
86 /*
87  * Make a symtab entry for PIC use.
88  */
89 static struct symtab *
gmcgarry
1.83
90 picsymtab(char *pchar *schar *s2)
ragge
1.60
91 {
92         struct symtab *sp = IALLOC(sizeof(struct symtab));
gmcgarry
1.83
93         size_t len = strlen(p) + strlen(s) + strlen(s2) + 1;
ragge
1.60
94         
ragge
1.69
95         sp->sname = sp->soname = IALLOC(len);
gmcgarry
1.83
96         strlcpy(sp->sonameplen);
97         strlcat(sp->sonameslen);
ragge
1.69
98         strlcat(sp->sonames2len);
ragge
1.123
99         sp->sap = NULL;
ragge
1.60
100         sp->sclass = EXTERN;
ragge
1.64
101         sp->sflags = sp->slevel = 0;
gmcgarry
1.158
102         sp->stype = 0xdeadbeef;
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);
ragge
1.138
119         q = block(UMULq0PTR|VOID00);
ragge
1.136
120         q = block(UMULq0p->n_typep->n_dfp->n_ap);
gmcgarry
1.103
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.168
140         struct attr *ga;
ragge
1.60
141         NODE *q, *r;
stefan
1.66
142         struct symtab *sp;
ragge
1.96
143         char *name;
ragge
1.60
144
ragge
1.138
145         q = tempnode(gotnrPTR|VOID00);
ragge
1.96
146         if ((name = p->n_sp->soname) == NULL)
147                 name = p->n_sp->sname;
ragge
1.171
148
149         if ((ga = attr_find(p->n_sp->sapGCC_ATYP_VISIBILITY)) &&
150             strcmp(ga->sarg(0), "hidden") == 0) {
151                 /* For hidden vars use GOTOFF */
152                 sp = picsymtab(""name"@GOTOFF");
153                 r = xbcon(0spINT);
154                 q = buildtree(PLUSqr);
155                 q = block(UMULq0p->n_typep->n_dfp->n_ap);
156                 q->n_sp = p->n_sp/* for init */
157                 nfree(p);
158                 return q;
159         }
160
ragge
1.96
161         sp = picsymtab(""name"@GOT");
ragge
1.117
162 #ifdef GCC_COMPAT
ragge
1.123
163         if (attr_find(p->n_sp->sapGCC_ATYP_STDCALL) != NULL)
ragge
1.117
164                 p->n_sp->sflags |= SSTDCALL;
165 #endif
ragge
1.118
166         sp->sflags = p->n_sp->sflags & SSTDCALL;
stefan
1.66
167         r = xbcon(0spINT);
ragge
1.60
168         q = buildtree(PLUSqr);
ragge
1.138
169         q = block(UMULq0PTR|VOID00);
ragge
1.123
170         q = block(UMULq0p->n_typep->n_dfp->n_ap);
gmcgarry
1.86
171         q->n_sp = p->n_sp/* for init */
172         nfree(p);
173         return q;
gmcgarry
1.83
174
175 #elif defined(MACHOABI)
176
gmcgarry
1.86
177         NODE *q, *r;
178         struct symtab *sp;
gmcgarry
1.101
179         char buf2[256], *name, *pspn;
gmcgarry
1.83
180
ragge
1.96
181         if ((name = cftnsp->soname) == NULL)
182                 name = cftnsp->sname;
183         if ((pspn = p->n_sp->soname) == NULL)
pantzer
1.98
184                 pspn = exname(p->n_sp->sname);
gmcgarry
1.83
185         if (p->n_sp->sclass == EXTDEF) {
gmcgarry
1.101
186                 snprintf(buf2256"-L%s$pb"name);
pantzer
1.98
187                 sp = picsymtab(""pspnbuf2);
gmcgarry
1.83
188         } else {
gmcgarry
1.101
189                 snprintf(buf2256"$non_lazy_ptr-L%s$pb"name);
ragge
1.96
190                 sp = picsymtab("L"pspnbuf2);
191                 addstub(&nlplistpspn);
gmcgarry
1.83
192         }
gmcgarry
1.158
193
194         sp->stype = p->n_sp->stype;
195
ragge
1.138
196         q = tempnode(gotnrPTR+VOID00);
gmcgarry
1.83
197         r = xbcon(0spINT);
198         q = buildtree(PLUSqr);
199
200         if (p->n_sp->sclass != EXTDEF)
ragge
1.138
201                 q = block(UMULq0PTR+VOID00);
ragge
1.123
202         q = block(UMULq0p->n_typep->n_dfp->n_ap);
ragge
1.65
203         q->n_sp = p->n_sp/* for init */
ragge
1.60
204         nfree(p);
205         return q;
gmcgarry
1.86
206
ragge
1.137
207 #else /* defined(PECOFFABI) || defined(AOUTABI) */
gmcgarry
1.86
208
209         return p;
210
211 #endif
212
ragge
1.60
213 }
214
215 /*
216  * Create a reference for a static variable.
217  */
218 static NODE *
219 picstatic(NODE *p)
220 {
gmcgarry
1.86
221
222 #if defined(ELFABI)
223
ragge
1.60
224         NODE *q, *r;
stefan
1.66
225         struct symtab *sp;
ragge
1.60
226
ragge
1.138
227         q = tempnode(gotnrPTR|VOID00);
ragge
1.94
228         if (p->n_sp->slevel > 0) {
ragge
1.60
229                 char buf[32];
230                 snprintf(buf32LABFMT, (int)p->n_sp->soffset);
gmcgarry
1.83
231                 sp = picsymtab(""buf"@GOTOFF");
ragge
1.96
232         } else {
233                 char *name;
234                 if ((name = p->n_sp->soname) == NULL)
235                         name = p->n_sp->sname;
236                 sp = picsymtab(""name"@GOTOFF");
237         }
238         
stefan
1.66
239         sp->sclass = STATIC;
240         sp->stype = p->n_sp->stype;
241         r = xbcon(0spINT);
ragge
1.60
242         q = buildtree(PLUSqr);
ragge
1.123
243         q = block(UMULq0p->n_typep->n_dfp->n_ap);
ragge
1.65
244         q->n_sp = p->n_sp/* for init */
ragge
1.60
245         nfree(p);
gmcgarry
1.86
246         return q;
gmcgarry
1.83
247
248 #elif defined(MACHOABI)
249
gmcgarry
1.86
250         NODE *q, *r;
251         struct symtab *sp;
gmcgarry
1.101
252         char buf2[256];
gmcgarry
1.83
253
gmcgarry
1.101
254         snprintf(buf2256"-L%s$pb",
ragge
1.97
255             cftnsp->soname ? cftnsp->soname : cftnsp->sname);
gmcgarry
1.83
256
ragge
1.94
257         if (p->n_sp->slevel > 0) {
gmcgarry
1.101
258                 char buf1[32];
259                 snprintf(buf132LABFMT, (int)p->n_sp->soffset);
gmcgarry
1.83
260                 sp = picsymtab(""buf1buf2);
261         } else  {
ragge
1.96
262                 char *name;
263                 if ((name = p->n_sp->soname) == NULL)
264                         name = p->n_sp->sname;
265                 sp = picsymtab(""exname(name), buf2);
gmcgarry
1.83
266         }
267         sp->sclass = STATIC;
268         sp->stype = p->n_sp->stype;
ragge
1.138
269         q = tempnode(gotnrPTR+VOID00);
gmcgarry
1.83
270         r = xbcon(0spINT);
271         q = buildtree(PLUSqr);
ragge
1.123
272         q = block(UMULq0p->n_typep->n_dfp->n_ap);
gmcgarry
1.83
273         q->n_sp = p->n_sp;
274         nfree(p);
gmcgarry
1.86
275         return q;
276
ragge
1.137
277 #else /* defined(PECOFFABI) || defined(AOUTABI) */
gmcgarry
1.86
278
279         return p;
gmcgarry
1.83
280
281 #endif
282
ragge
1.60
283 }
ragge
1.1
284
ragge
1.73
285 #ifdef TLS
286 /*
287  * Create a reference for a TLS variable.
288  */
289 static NODE *
290 tlspic(NODE *p)
291 {
292         NODE *q, *r;
ragge
1.85
293         struct symtab *sp, *sp2;
ragge
1.96
294         char *name;
ragge
1.73
295
296         /*
297          * creates:
298          *   leal var@TLSGD(%ebx),%eax
299          *   call ___tls_get_addr@PLT
300          */
301
302         /* calc address of var@TLSGD */
ragge
1.138
303         q = tempnode(gotnrPTR|VOID00);
ragge
1.96
304         if ((name = p->n_sp->soname) == NULL)
305                 name = p->n_sp->sname;
306         sp = picsymtab(""name"@TLSGD");
ragge
1.73
307         r = xbcon(0spINT);
308         q = buildtree(PLUSqr);
309
310         /* assign to %eax */
ragge
1.138
311         r = block(REGNILNILPTR|VOID00);
ragge
1.73
312         r->n_rval = EAX;
313         q = buildtree(ASSIGNrq);
314
315         /* call ___tls_get_addr */
ragge
1.85
316         sp2 = lookup("___tls_get_addr@PLT"0);
317         sp2->stype = EXTERN|INT|FTN;
318         r = nametree(sp2);
ragge
1.73
319         r = buildtree(ADDROFrNIL);
ragge
1.138
320         r = block(UCALLrNILINT00);
ragge
1.73
321
322         /* fusion both parts together */
323         q = buildtree(COMOPqr);
ragge
1.123
324         q = block(UMULq0p->n_typep->n_dfp->n_ap);
ragge
1.73
325         q->n_sp = p->n_sp/* for init */
326
327         nfree(p);
328         return q;
329 }
330
331 static NODE *
332 tlsnonpic(NODE *p)
333 {
334         NODE *q, *r;
ragge
1.85
335         struct symtab *sp, *sp2;
ragge
1.73
336         int ext = p->n_sp->sclass;
ragge
1.96
337         char *name;
ragge
1.73
338
ragge
1.96
339         if ((name = p->n_sp->soname) == NULL)
340                 name = p->n_sp->sname;
341         sp = picsymtab(""name,
gmcgarry
1.83
342             ext == EXTERN ? "@INDNTPOFF" : "@NTPOFF");
ragge
1.73
343         q = xbcon(0spINT);
344         if (ext == EXTERN)
ragge
1.138
345                 q = block(UMULqNILPTR|VOID00);
ragge
1.73
346
ragge
1.85
347         sp2 = lookup("%gs:0"0);
348         sp2->stype = EXTERN|INT;
349         r = nametree(sp2);
ragge
1.73
350
351         q = buildtree(PLUSqr);
ragge
1.123
352         q = block(UMULq0p->n_typep->n_dfp->n_ap);
ragge
1.73
353         q->n_sp = p->n_sp/* for init */
354
355         nfree(p);
356         return q;
357 }
358
359 static NODE *
360 tlsref(NODE *p)
361 {
362         if (kflag)
363                 return (tlspic(p));
364         else
365                 return (tlsnonpic(p));
366 }
367 #endif
368
ragge
1.48
369 /* clocal() is called to do local transformations on
370  * an expression tree preparitory to its being
371  * written out in intermediate code.
372  *
373  * the major essential job is rewriting the
374  * automatic variables and arguments in terms of
375  * REG and OREG nodes
376  * conversion ops which are not necessary are also clobbered here
377  * in addition, any special features (such as rewriting
378  * exclusive or) are easily handled here as well
379  */
ragge
1.1
380 NODE *
381 clocal(NODE *p)
382 {
383
ragge
1.177
384         struct attr *ap;
ragge
1.1
385         register struct symtab *q;
gmcgarry
1.113
386         register NODE *r, *l;
387 #if defined(os_openbsd)
388         register NODE *s, *n;
389 #endif
ragge
1.1
390         register int o;
ragge
1.40
391         register int m;
ragge
1.1
392
ragge
1.54
393 #ifdef PCC_DEBUG
394         if (xdebug) {
395                 printf("clocal: %p\n"p);
396                 fwalk(peprint0);
397         }
398 #endif
ragge
1.1
399         switcho = p->n_op ){
400
401         case NAME:
402                 if ((q = p->n_sp) == NULL)
403                         return p/* Nothing to care about */
404
405                 switch (q->sclass) {
406
407                 case PARAM:
408                 case AUTO:
409                         /* fake up a structure reference */
410                         r = block(REGNILNILPTR+STRTY00);
411                         r->n_lval = 0;
412                         r->n_rval = FPREG;
413                         p = stref(block(STREFrp000));
414                         break;
415
ragge
1.65
416                 case USTATIC:
417                         if (kflag == 0)
418                                 break;
419                         /* FALLTHROUGH */
ragge
1.1
420                 case STATIC:
ragge
1.73
421 #ifdef TLS
422                         if (q->sflags & STLS) {
423                                 p = tlsref(p);
424                                 break;
425                         }
426 #endif
ragge
1.60
427                         if (kflag == 0) {
428                                 if (q->slevel == 0)
429                                         break;
ragge
1.178
430                         } else if (kflag && !statinit && blevel > 0 &&
431                             attr_find(q->sapGCC_ATYP_WEAKREF)) {
432                                 /* extern call */
433                                 p = picext(p);
ragge
1.147
434                         } else if (blevel > 0 && !statinit)
ragge
1.60
435                                 p = picstatic(p);
ragge
1.1
436                         break;
437
438                 case REGISTER:
439                         p->n_op = REG;
440                         p->n_lval = 0;
441                         p->n_rval = q->soffset;
442                         break;
443
ragge
1.60
444                 case EXTERN:
445                 case EXTDEF:
ragge
1.73
446 #ifdef TLS
447                         if (q->sflags & STLS) {
448                                 p = tlsref(p);
449                                 break;
450                         }
451 #endif
gmcgarry
1.103
452
453 #ifdef os_win32
454                         if (q->sflags & SDLLINDIRECT)
455                                 p = import(p);
456 #endif
ragge
1.177
457                         if ((ap = attr_find(q->sap,
458                             GCC_ATYP_VISIBILITY)) != NULL &&
459                             strcmp(ap->sarg(0), "hidden") == 0) {
460                                 char *sn = q->soname ? q->soname : q->sname
461                                 printf("\t.hidden %s\n"sn);
462                         }
ragge
1.60
463                         if (kflag == 0)
464                                 break;
ragge
1.147
465                         if (blevel > 0 && !statinit)
ragge
1.60
466                                 p = picext(p);
467                         break;
468                 }
ragge
1.1
469                 break;
470
ragge
1.65
471         case ADDROF:
ragge
1.175
472                 if (kflag == 0 || blevel == 0 || statinit)
ragge
1.65
473                         break;
474                 /* char arrays may end up here */
475                 l = p->n_left;
476                 if (l->n_op != NAME ||
477                     (l->n_type != ARY+CHAR && l->n_type != ARY+WCHAR_TYPE))
478                         break;
479                 l = p;
480                 p = picstatic(p->n_left);
481                 nfree(l);
482                 if (p->n_op != UMUL)
483                         cerror("ADDROF error");
484                 l = p;
485                 p = p->n_left;
486                 nfree(l);
487                 break;
488
ragge
1.62
489         case UCALL:
ragge
1.60
490                 if (kflag == 0)
491                         break;
ragge
1.138
492                 l = block(REGNILNILINT00);
ragge
1.62
493                 l->n_rval = EBX;
ragge
1.179
494                 p->n_right = buildtree(ASSIGNltempnode(gotnrINT00));
495                 p->n_op -= (UCALL-CALL);
496                 break;
497
498         case USTCALL:
499                 /* Add hidden arg0 */
500                 r = block(REGNILNILINCREF(VOID), 00);
501                 regno(r) = EBP;
502                 if ((ap = attr_find(p->n_apGCC_ATYP_REGPARM)) != NULL &&
503                     ap->iarg(0) > 0) {
504                         l = block(REGNILNILINCREF(VOID), 00);
505                         regno(l) = EAX;
506                         p->n_right = buildtree(ASSIGNlr);
507                 } else
508                         p->n_right = block(FUNARGrNILINCREF(VOID), 00);
ragge
1.62
509                 p->n_op -= (UCALL-CALL);
ragge
1.179
510
511                 if (kflag == 0)
512                         break;
513                 l = block(REGNILNILINT00);
514                 regno(l) = EBX;
515                 r = buildtree(ASSIGNltempnode(gotnrINT00));
516                 p->n_right = block(CMrp->n_rightINT00);
517                 break;
pantzer
1.89
518         
519         /* FALLTHROUGH */
520 #if defined(MACHOABI)
521         case CALL:
522         case STCALL:
523                 if (p->n_type == VOID)
524                         break;
525
ragge
1.123
526                 r = tempnode(0p->n_typep->n_dfp->n_ap);
pantzer
1.89
527                 l = tcopy(r);
528                 p = buildtree(COMOPbuildtree(ASSIGNrp), l);
529 #endif
530                         
ragge
1.10
531                 break;
ragge
1.64
532
ragge
1.159
533 #if defined(os_openbsd)
534 /*
535  * The called function returns structures according to their aligned size:
536  *  Structures 1, 2 or 4 bytes in size are placed in EAX.
537  *  Structures 8 bytes in size are placed in: EAX and EDX.
538  */
539         case UMUL:
540                 o = 0;
541                 l = p->n_left;
542                 if (l->n_op == PLUS)
543                         l = l->n_lefto++;
544                 if (l->n_op != PCONV)
545                         break;
546                 l = l->n_left;
547                 if (l->n_op != STCALL && l->n_op != USTCALL)
548                         break;
549                 m = tsize(DECREF(l->n_type), l->n_dfl->n_ap);
550                 if (m != SZCHAR && m != SZSHORT &&
551                     m != SZINT && m != SZLONGLONG)
552                         break;
553                 /* This is a direct reference to a small struct return */
554                 l->n_op -= (STCALL-CALL);
555                 if (o == 0) {
556                         /* first element in struct */
557                         p->n_left = nfree(p->n_left); /* free PCONV */
558                         l->n_ap = p->n_ap;
559                         l->n_df = p->n_df;
560                         l->n_type = p->n_type;
561                         if (p->n_type < INT) {
562                                 /* Need to add SCONV */
563                                 p->n_op = SCONV;
564                         } else
565                                 p = nfree(p); /* no casts needed */
566                 } else {
567                         /* convert PLUS to RS */
568                         o = p->n_left->n_right->n_lval;
569                         l->n_type = (o > 3 ? ULONGLONG : UNSIGNED);
570                         nfree(p->n_left->n_right);
571                         p->n_left = nfree(p->n_left);
572                         p->n_left = nfree(p->n_left);
573                         p->n_left = buildtree(RSp->n_leftbcon(o*8));
574                         p->n_left = makety(p->n_leftp->n_typep->n_qual,
575                             p->n_dfp->n_ap);
576                         p = nfree(p);
577                 }
578                 break;
579 #endif
580
ragge
1.129
581 #ifdef notyet
582         /* XXX breaks sometimes */
ragge
1.8
583         case CBRANCH:
584                 l = p->n_left;
585
586                 /*
ragge
1.77
587                  * Remove unnecessary conversion ops.
ragge
1.8
588                  */
ragge
1.128
589                 if (!clogop(l->n_op) || l->n_left->n_op != SCONV)
590                         break;
591                 if (coptype(l->n_op) != BITYPE)
592                         break;
593                 if (l->n_right->n_op != ICON)
594                         break;
595                 r = l->n_left->n_left;
596                 if (r->n_type >= FLOAT)
597                         break;
598                 if (toolarge(r->n_typel->n_right->n_lval))
599                         break;
600                 l->n_right->n_type = r->n_type;
601                 if (l->n_op >= ULE && l->n_op <= UGT)
602                         l->n_op -= (UGT-ULE);
603                 p->n_left = buildtree(l->n_oprl->n_right);
604                 nfree(l->n_left);
605                 nfree(l);
ragge
1.8
606                 break;
ragge
1.129
607 #endif
ragge
1.8
608
ragge
1.1
609         case PCONV:
610                 l = p->n_left;
ragge
1.162
611
612                 /* Make int type before pointer */
ragge
1.40
613                 if (l->n_type < INT || l->n_type == LONGLONG || 
ragge
1.134
614                     l->n_type == ULONGLONG || l->n_type == BOOL) {
ragge
1.40
615                         /* float etc? */
ragge
1.162
616                         p->n_left = block(SCONVlNILUNSIGNED00);
ragge
1.40
617                 }
618                 break;
619
ragge
1.1
620         case SCONV:
ragge
1.109
621                 if (p->n_left->n_op == COMOP)
622                         break;  /* may propagate wrong type later */
ragge
1.1
623                 l = p->n_left;
ragge
1.4
624
ragge
1.25
625                 if (p->n_type == l->n_type) {
626                         nfree(p);
627                         return l;
628                 }
629
ragge
1.4
630                 if ((p->n_type & TMASK) == 0 && (l->n_type & TMASK) == 0 &&
ragge
1.139
631                     tsize(p->n_typep->n_dfp->n_ap) ==
632                     tsize(l->n_typel->n_dfl->n_ap)) {
ragge
1.4
633                         if (p->n_type != FLOAT && p->n_type != DOUBLE &&
ragge
1.15
634                             l->n_type != FLOAT && l->n_type != DOUBLE &&
635                             l->n_type != LDOUBLE && p->n_type != LDOUBLE) {
ragge
1.41
636                                 if (l->n_op == NAME || l->n_op == UMUL ||
637                                     l->n_op == TEMP) {
ragge
1.15
638                                         l->n_type = p->n_type;
639                                         nfree(p);
640                                         return l;
641                                 }
ragge
1.4
642                         }
ragge
1.6
643                 }
644
ragge
1.42
645                 if (DEUNSIGN(p->n_type) == INT && DEUNSIGN(l->n_type) == INT &&
ragge
1.131
646                     coptype(l->n_op) == BITYPE && l->n_op != COMOP &&
647                     l->n_op != QUEST) {
ragge
1.42
648                         l->n_type = p->n_type;
649                         nfree(p);
650                         return l;
651                 }
652
ragge
1.3
653                 o = l->n_op;
ragge
1.4
654                 m = p->n_type;
ragge
1.1
655
656                 if (o == ICON) {
ragge
1.147
657                         /*
658                          * Can only end up here if o is an address,
659                          * and in that case the only compile-time conversion
660                          * possible is to int.
661                          */
ragge
1.156
662                         if ((TMASK & l->n_type) == 0 && l->n_sp == NULL)
ragge
1.147
663                                 cerror("SCONV ICON");
664                         if (l->n_sp == 0) {
665                                 p->n_type = UNSIGNED;
666                                 concast(lm);
667                         } else if (m != INT && m != UNSIGNED)
ragge
1.1
668                                 break;
669                         l->n_type = m;
ragge
1.138
670                         l->n_ap = 0;
ragge
1.1
671                         nfree(p);
672                         return l;
ragge
1.147
673                 } else if (l->n_op == FCON)
674                         cerror("SCONV FCON");
ragge
1.36
675                 if ((p->n_type == CHAR || p->n_type == UCHAR ||
676                     p->n_type == SHORT || p->n_type == USHORT) &&
677                     (l->n_type == FLOAT || l->n_type == DOUBLE ||
678                     l->n_type == LDOUBLE)) {
ragge
1.123
679                         p = block(SCONVpNILp->n_typep->n_dfp->n_ap);
ragge
1.36
680                         p->n_left->n_type = INT;
681                         return p;
682                 }
ragge
1.1
683                 break;
684
ragge
1.15
685         case MOD:
686         case DIV:
687                 if (o == DIV && p->n_type != CHAR && p->n_type != SHORT)
688                         break;
ragge
1.16
689                 if (o == MOD && p->n_type != CHAR && p->n_type != SHORT)
ragge
1.15
690                         break;
691                 /* make it an int division by inserting conversions */
ragge
1.147
692                 p->n_left = makety(p->n_leftINT000);
693                 p->n_right = makety(p->n_rightINT000);
694                 o = p->n_type;
695                 p->n_type = INT;
696                 p = makety(po000);
ragge
1.15
697                 break;
698
ragge
1.31
699         case FORCE:
ragge
1.32
700                 /* put return value in return reg */
701                 p->n_op = ASSIGN;
702                 p->n_right = p->n_left;
ragge
1.138
703                 p->n_left = block(REGNILNILp->n_type00);
ragge
1.53
704                 p->n_left->n_rval = p->n_left->n_type == BOOL ? 
705                     RETREG(CHAR) : RETREG(p->n_type);
ragge
1.31
706                 break;
ragge
1.39
707
708         case LS:
709         case RS:
ragge
1.127
710                 /* shift count must be in a char */
ragge
1.39
711                 if (p->n_right->n_type == CHAR || p->n_right->n_type == UCHAR)
712                         break;
ragge
1.138
713                 p->n_right = block(SCONVp->n_rightNILCHAR00);
ragge
1.39
714                 break;
ragge
1.108
715 #if defined(os_openbsd)
716                 /* If not using pcc struct return */
717         case STASG:
718                 r = p->n_right;
719                 if (r->n_op != STCALL && r->n_op != USTCALL)
720                         break;
ragge
1.123
721                 m = tsize(BTYPE(r->n_type), r->n_dfr->n_ap);
ragge
1.108
722                 if (m == SZCHAR)
723                         m = CHAR;
724                 else if (m == SZSHORT)
725                         m = SHORT;
726                 else if (m == SZINT)
727                         m = INT;
728                 else if (m == SZLONGLONG)
729                         m = LONGLONG;
730                 else
731                         break;
732
733                 l = buildtree(ADDROFp->n_leftNIL);
734                 nfree(p);
735
736                 r->n_op -= (STCALL-CALL);
737                 r->n_type = m;
738
739                 /* r = long, l = &struct */
740
ragge
1.123
741                 n = tempnode(0mr->n_dfr->n_ap);
ragge
1.108
742                 r = buildtree(ASSIGNccopy(n), r);
743
ragge
1.123
744                 s = tempnode(0l->n_typel->n_dfl->n_ap);
ragge
1.108
745                 l = buildtree(ASSIGNccopy(s), l);
746
747                 p = buildtree(COMOPrl);
748
749                 l = buildtree(CAST,
ragge
1.138
750                     block(NAMENILNILm|PTR00), ccopy(s));
ragge
1.108
751                 r = l->n_right;
752                 nfree(l->n_left);
753                 nfree(l);
754
755                 r = buildtree(ASSIGNbuildtree(UMULrNIL), n);
756                 p = buildtree(COMOPpr);
757                 p = buildtree(COMOPps);
758                 break;
759 #endif
ragge
1.1
760         }
ragge
1.54
761 #ifdef PCC_DEBUG
762         if (xdebug) {
763                 printf("clocal end: %p\n"p);
764                 fwalk(peprint0);
765         }
766 #endif
ragge
1.1
767         return(p);
768 }
769
ragge
1.64
770 /*
771  * Change CALL references to either direct (static) or PLT.
772  */
773 static void
ragge
1.88
774 fixnames(NODE *pvoid *arg)
ragge
1.64
775 {
ragge
1.137
776 #if defined(ELFABI) || defined(MACHOABI)
gmcgarry
1.86
777
ragge
1.64
778         struct symtab *sp;
ragge
1.123
779         struct attr *ap;
ragge
1.64
780         NODE *q;
781         char *c;
782         int isu;
783
784         if ((cdope(p->n_op) & CALLFLG) == 0)
785                 return;
786         isu = 0;
787         q = p->n_left;
ragge
1.123
788         ap = q->n_ap;
ragge
1.64
789         if (q->n_op == UMUL)
790                 q = q->n_leftisu = 1;
gmcgarry
1.83
791
ragge
1.64
792         if (q->n_op == PLUS && q->n_left->n_op == TEMP &&
793             q->n_right->n_op == ICON) {
794                 sp = q->n_right->n_sp;
795
ragge
1.65
796                 if (sp == NULL)
797                         return/* nothing to do */
ragge
1.64
798                 if (sp->sclass == STATIC && !ISFTN(sp->stype))
799                         return/* function pointer */
800
801                 if (sp->sclass != STATIC && sp->sclass != EXTERN &&
802                     sp->sclass != EXTDEF)
803                         cerror("fixnames");
ragge
1.96
804                 c = NULL;
gmcgarry
1.83
805 #if defined(ELFABI)
806
ragge
1.96
807                 if (sp->soname == NULL ||
808                     (c = strstr(sp->soname"@GOT")) == NULL)
ragge
1.64
809                         cerror("fixnames2");
810                 if (isu) {
811                         memcpy(c"@PLT"sizeof("@PLT"));
812                 } else
813                         *c = 0;
gmcgarry
1.83
814
815 #elif defined(MACHOABI)
816
ragge
1.96
817                 if (sp->soname == NULL ||
818                     ((c = strstr(sp->soname"$non_lazy_ptr")) == NULL &&
819                     (c = strstr(sp->soname"-L")) == NULL))
gmcgarry
1.83
820                                 cerror("fixnames2");
gmcgarry
1.158
821
822                 if (!ISFTN(sp->stype))
823                         return/* function pointer */
824
gmcgarry
1.83
825                 if (isu) {
gmcgarry
1.173
826                         *c = 0;
gmcgarry
1.83
827                         addstub(&stublistsp->soname+1);
plunky
1.166
828                         memcpy(c"$stub"sizeof("$stub"));
gmcgarry
1.83
829                 } else 
830                         *c = 0;
831
832 #endif
833
ragge
1.64
834                 nfree(q->n_left);
835                 q = q->n_right;
836                 if (isu)
837                         nfree(p->n_left->n_left);
838                 nfree(p->n_left);
839                 p->n_left = q;
ragge
1.123
840                 q->n_ap = ap;
ragge
1.64
841         }
gmcgarry
1.86
842 #endif
ragge
1.64
843 }
844
ragge
1.117
845 static void mangle(NODE *p);
846
ragge
1.1
847 void
848 myp2tree(NODE *p)
849 {
ragge
1.67
850         struct symtab *sp;
851
ragge
1.64
852         if (kflag)
ragge
1.117
853                 fixnames(p0);
854
855         mangle(p);
856
ragge
1.67
857         if (p->n_op != FCON)
858                 return;
859
860         sp = IALLOC(sizeof(struct symtab));
861         sp->sclass = STATIC;
ragge
1.138
862         sp->sap = 0;
ragge
1.67
863         sp->slevel = 1/* fake numeric label */
ragge
1.70
864         sp->soffset = getlab();
ragge
1.67
865         sp->sflags = 0;
ragge
1.70
866         sp->stype = p->n_type;
867         sp->squal = (CON >> TSHIFT);
gmcgarry
1.144
868         sp->sname = sp->soname = NULL;
ragge
1.70
869
ragge
1.154
870         locctr(DATAsp);
ragge
1.70
871         defloc(sp);
ragge
1.139
872         ninval(0tsize(sp->stypesp->sdfsp->sap), p);
ragge
1.67
873
874         p->n_op = NAME;
875         p->n_lval = 0;
876         p->n_sp = sp;
ragge
1.161
877
878         if (kflag) {
879                 NODE *q = optim(picstatic(tcopy(p)));
880                 *p = *q;
881                 nfree(q);
882         }
ragge
1.1
883 }
884
885 /*ARGSUSED*/
886 int
887 andable(NODE *p)
888 {
ragge
1.64
889         return(1);      /* all names can have & taken on them */
ragge
1.1
890 }
891
892 /*
ragge
1.36
893  * Return 1 if a variable of type type is OK to put in register.
894  */
895 int
896 cisreg(TWORD t)
897 {
898         if (t == FLOAT || t == DOUBLE || t == LDOUBLE)
899                 return 0/* not yet */
900         return 1;
901 }
902
903 /*
ragge
1.1
904  * Allocate off bits on the stack.  p is a tree that when evaluated
ragge
1.45
905  * is the multiply count for off, t is a storeable node where to write
ragge
1.1
906  * the allocated address.
907  */
908 void
909 spalloc(NODE *tNODE *pOFFSZ off)
910 {
911         NODE *sp;
912
ragge
1.45
913         p = buildtree(MULpbcon(off/SZCHAR)); /* XXX word alignment? */
914
915         /* sub the size from sp */
ragge
1.138
916         sp = block(REGNILNILp->n_type00);
ragge
1.45
917         sp->n_lval = 0;
918         sp->n_rval = STKREG;
919         ecomp(buildtree(MINUSEQspp));
ragge
1.1
920
pantzer
1.100
921 #ifdef MACHOABI 
922         /* align to 16 bytes */
ragge
1.138
923         sp = block(REGNILNILp->n_type00);
pantzer
1.100
924         sp->n_lval = 0;
925         sp->n_rval = STKREG;
926         ecomp(buildtree(PLUSEQspbcon(15)));
927         
ragge
1.138
928         sp = block(REGNILNILp->n_type00);
pantzer
1.100
929         sp->n_lval = 0;
930         sp->n_rval = STKREG;
931         ecomp(buildtree(RSEQspbcon(4)));
932         
ragge
1.138