Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20100527194546

Diff

Diff from 1.121 to:

Annotations

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

Annotated File View

ragge
1.121
1 /*      $Id: local.c,v 1.121 2010/05/27 19:45:46 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.120
100         sp->ssue = 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
144         q = tempnode(gotnrPTR|VOID0MKSUE(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
149         if (gcc_get_attr(p->n_sp->ssueGCC_ATYP_STDCALL) != NULL)
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);
155         q = block(UMULq0PTR|VOID0MKSUE(VOID));
gmcgarry
1.86
156         q = block(UMULq0p->n_typep->n_dfp->n_sue);
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         }
179         q = tempnode(gotnrPTR+VOID0MKSUE(VOID));
180         r = xbcon(0spINT);
181         q = buildtree(PLUSqr);
182
183         if (p->n_sp->sclass != EXTDEF)
184                 q = block(UMULq0PTR+VOID0MKSUE(VOID));
ragge
1.60
185         q = block(UMULq0p->n_typep->n_dfp->n_sue);
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
210         q = tempnode(gotnrPTR|VOID0MKSUE(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);
226         q = block(UMULq0p->n_typep->n_dfp->n_sue);
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;
252         q = tempnode(gotnrPTR+VOID0MKSUE(VOID));
253         r = xbcon(0spINT);
254         q = buildtree(PLUSqr);
255         q = block(UMULq0p->n_typep->n_dfp->n_sue);
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 */
286         q = tempnode(gotnrPTR|VOID0MKSUE(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 */
294         r = block(REGNILNILPTR|VOID0MKSUE(VOID));
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);
303         r = block(UCALLrNILINT0MKSUE(INT));
304
305         /* fusion both parts together */
306         q = buildtree(COMOPqr);
307         q = block(UMULq0p->n_typep->n_dfp->n_sue);
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)
328                 q = block(UMULqNILPTR|VOID0MKSUE(VOID));
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);
335         q = block(UMULq0p->n_typep->n_dfp->n_sue);
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 */
469                 l = block(REGNILNILINT0MKSUE(INT));
470                 l->n_rval = EBX;
471                 p->n_right = buildtree(ASSIGNl,
472                     tempnode(gotnrINT0MKSUE(INT)));
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
483                 r = tempnode(0p->n_typep->n_dfp->n_sue);
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,
528                             UNSIGNED0MKSUE(UNSIGNED));
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;
547                 l->n_sue = p->n_sue;
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 &&
pj
1.30
563                     btdims[p->n_type].suesize == btdims[l->n_type].suesize) {
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:
592                                 l->n_lval = l->n_lval != 0;
593                                 break;
ragge
1.1
594                         case CHAR:
595                                 l->n_lval = (char)val;
596                                 break;
597                         case UCHAR:
598                                 l->n_lval = val & 0377;
599                                 break;
ragge
1.15
600                         case SHORT:
ragge
1.1
601                                 l->n_lval = (short)val;
602                                 break;
ragge
1.15
603                         case USHORT:
ragge
1.1
604                                 l->n_lval = val & 0177777;
605                                 break;
ragge
1.15
606                         case ULONG:
ragge
1.1
607                         case UNSIGNED:
608                                 l->n_lval = val & 0xffffffff;
609                                 break;
ragge
1.15
610                         case LONG:
ragge
1.1
611                         case INT:
612                                 l->n_lval = (int)val;
613                                 break;
614                         case LONGLONG:
615                                 l->n_lval = (long long)val;
ragge
1.15
616                                 break;
ragge
1.1
617                         case ULONGLONG:
618                                 l->n_lval = val;
619                                 break;
620                         case VOID:
621                                 break;
ragge
1.14
622                         case LDOUBLE:
ragge
1.1
623                         case DOUBLE:
624                         case FLOAT:
625                                 l->n_op = FCON;
626                                 l->n_dcon = val;
627                                 break;
628                         default:
629                                 cerror("unknown type %d"m);
630                         }
631                         l->n_type = m;
ragge
1.44
632                         l->n_sue = MKSUE(m);
ragge
1.1
633                         nfree(p);
634                         return l;
ragge
1.72
635                 } else if (l->n_op == FCON) {
gmcgarry
1.107
636                         l->n_lval = (CONSZ)l->n_dcon;
ragge
1.72
637                         l->n_sp = NULL;
638                         l->n_op = ICON;
639                         l->n_type = m;
640                         l->n_sue = MKSUE(m);
641                         nfree(p);
642                         return clocal(l);
ragge
1.1
643                 }
ragge
1.17
644                 if (DEUNSIGN(p->n_type) == SHORT &&
645                     DEUNSIGN(l->n_type) == SHORT) {
646                         nfree(p);
647                         p = l;
648                 }
ragge
1.36
649                 if ((p->n_type == CHAR || p->n_type == UCHAR ||
650                     p->n_type == SHORT || p->n_type == USHORT) &&
651                     (l->n_type == FLOAT || l->n_type == DOUBLE ||
652                     l->n_type == LDOUBLE)) {
653                         p = block(SCONVpNILp->n_typep->n_dfp->n_sue);
654                         p->n_left->n_type = INT;
655                         return p;
656                 }
ragge
1.1
657                 break;
658
ragge
1.15
659         case MOD:
660         case DIV:
661                 if (o == DIV && p->n_type != CHAR && p->n_type != SHORT)
662                         break;
ragge
1.16
663                 if (o == MOD && p->n_type != CHAR && p->n_type != SHORT)
ragge
1.15
664                         break;
665                 /* make it an int division by inserting conversions */
666                 p->n_left = block(SCONVp->n_leftNILINT0MKSUE(INT));
667                 p->n_right = block(SCONVp->n_rightNILINT0MKSUE(INT));
668                 p = block(SCONVpNILp->n_type0MKSUE(p->n_type));
669                 p->n_left->n_type = INT;
670                 break;
671
ragge
1.1
672         case PMCONV:
673         case PVCONV:
ragge
1.74
674                 r = p;
675                 p = buildtree(o == PMCONV ? MUL : DIVp->n_leftp->n_right);
676                 nfree(r);
677                 break;
ragge
1.1
678
ragge
1.31
679         case FORCE:
ragge
1.32
680                 /* put return value in return reg */
681                 p->n_op = ASSIGN;
682                 p->n_right = p->n_left;
683                 p->n_left = block(REGNILNILp->n_type0MKSUE(INT));
ragge
1.53
684                 p->n_left->n_rval = p->n_left->n_type == BOOL ? 
685                     RETREG(CHAR) : RETREG(p->n_type);
ragge
1.31
686                 break;
ragge
1.39
687
688         case LS:
689         case RS:
690                 /* shift count must be in a char
691                  * unless longlong, where it must be int */
692                 if (p->n_type == LONGLONG || p->n_type == ULONGLONG) {
693                         if (p->n_right->n_type != INT)
694                                 p->n_right = block(SCONVp->n_rightNIL,
695                                     INT0MKSUE(INT));
696                         break;
697                 }
698                 if (p->n_right->n_type == CHAR || p->n_right->n_type == UCHAR)
699                         break;
700                 p->n_right = block(SCONVp->n_rightNIL,
701                     CHAR0MKSUE(CHAR));
702                 break;
ragge
1.108
703 #if defined(os_openbsd)
704                 /* If not using pcc struct return */
705         case STASG:
706                 r = p->n_right;
707                 if (r->n_op != STCALL && r->n_op != USTCALL)
708                         break;
709                 m = tsize(BTYPE(r->n_type), r->n_dfr->n_sue);
710                 if (m == SZCHAR)
711                         m = CHAR;
712                 else if (m == SZSHORT)
713                         m = SHORT;
714                 else if (m == SZINT)
715                         m = INT;
716                 else if (m == SZLONGLONG)
717                         m = LONGLONG;
718                 else
719                         break;
720
721                 l = buildtree(ADDROFp->n_leftNIL);
722                 nfree(p);
723
724                 r->n_op -= (STCALL-CALL);
725                 r->n_type = m;
726
727                 /* r = long, l = &struct */
728
729                 n = tempnode(0mr->n_dfr->n_sue);
730                 r = buildtree(ASSIGNccopy(n), r);
731
732                 s = tempnode(0l->n_typel->n_dfl->n_sue);
733                 l = buildtree(ASSIGNccopy(s), l);
734
735                 p = buildtree(COMOPrl);
736
737                 l = buildtree(CAST,
738                     block(NAMENILNILm|PTR0MKSUE(m)), ccopy(s));
739                 r = l->n_right;
740                 nfree(l->n_left);
741                 nfree(l);
742
743                 r = buildtree(ASSIGNbuildtree(UMULrNIL), n);
744                 p = buildtree(COMOPpr);
745                 p = buildtree(COMOPps);
746                 break;
747 #endif
ragge
1.1
748         }
ragge
1.54
749 #ifdef PCC_DEBUG
750         if (xdebug) {
751                 printf("clocal end: %p\n"p);
752                 fwalk(peprint0);
753         }
754 #endif
ragge
1.1
755         return(p);
756 }
757
ragge
1.64
758 /*
759  * Change CALL references to either direct (static) or PLT.
760  */
761 static void
ragge
1.88
762 fixnames(NODE *pvoid *arg)
ragge
1.64
763 {
gmcgarry
1.86
764 #if !defined(PECOFFABI)
765
ragge
1.64
766         struct symtab *sp;
ragge
1.65
767         struct suedef *sue;
ragge
1.64
768         NODE *q;
769         char *c;
770         int isu;
771
772         if ((cdope(p->n_op) & CALLFLG) == 0)
773                 return;
774         isu = 0;
775         q = p->n_left;
ragge
1.65
776         sue = q->n_sue;
ragge
1.64
777         if (q->n_op == UMUL)
778                 q = q->n_leftisu = 1;
gmcgarry
1.83
779
ragge
1.64
780         if (q->n_op == PLUS && q->n_left->n_op == TEMP &&
781             q->n_right->n_op == ICON) {
782                 sp = q->n_right->n_sp;
783
ragge
1.65
784                 if (sp == NULL)
785                         return/* nothing to do */
ragge
1.64
786                 if (sp->sclass == STATIC && !ISFTN(sp->stype))
787                         return/* function pointer */
788
789                 if (sp->sclass != STATIC && sp->sclass != EXTERN &&
790                     sp->sclass != EXTDEF)
791                         cerror("fixnames");
ragge
1.96
792                 c = NULL;
gmcgarry
1.83
793 #if defined(ELFABI)
794
ragge
1.96
795                 if (sp->soname == NULL ||
796                     (c = strstr(sp->soname"@GOT")) == NULL)
ragge
1.64
797                         cerror("fixnames2");
798                 if (isu) {
799                         memcpy(c"@PLT"sizeof("@PLT"));
800                 } else
801                         *c = 0;
gmcgarry
1.83
802
803 #elif defined(MACHOABI)
804
ragge
1.96
805                 if (sp->soname == NULL ||
806                     ((c = strstr(sp->soname"$non_lazy_ptr")) == NULL &&
807                     (c = strstr(sp->soname"-L")) == NULL))
gmcgarry
1.83
808                                 cerror("fixnames2");
809                 if (isu) {
810                         *c = 0;
811                         addstub(&stublistsp->soname+1);
812                         strcpy(c"$stub");
813                 } else 
814                         *c = 0;
815
816 #endif
817
ragge
1.64
818                 nfree(q->n_left);
819                 q = q->n_right;
820                 if (isu)
821                         nfree(p->n_left->n_left);
822                 nfree(p->n_left);
823                 p->n_left = q;
ragge
1.65
824                 q->n_sue = sue;
ragge
1.64
825         }
gmcgarry
1.86
826 #endif
ragge
1.64
827 }
828
ragge
1.117
829 static void mangle(NODE *p);
830
ragge
1.1
831 void
832 myp2tree(NODE *p)
833 {
ragge
1.67
834         struct symtab *sp;
835
ragge
1.64
836         if (kflag)
ragge
1.117
837                 fixnames(p0);
838
839         mangle(p);
840
ragge
1.67
841         if (p->n_op != FCON)
842                 return;
843
844         sp = IALLOC(sizeof(struct symtab));
845         sp->sclass = STATIC;
gmcgarry
1.75
846         sp->ssue = MKSUE(p->n_type);
ragge
1.67
847         sp->slevel = 1/* fake numeric label */
ragge
1.70
848         sp->soffset = getlab();
ragge
1.67
849         sp->sflags = 0;
ragge
1.70
850         sp->stype = p->n_type;
851         sp->squal = (CON >> TSHIFT);
852
853         defloc(sp);
gmcgarry
1.75
854         ninval(0sp->ssue->suesizep);
ragge
1.67
855
856         p->n_op = NAME;
857         p->n_lval = 0;
858         p->n_sp = sp;
ragge
1.1
859 }
860
861 /*ARGSUSED*/
862 int
863 andable(NODE *p)
864 {
ragge
1.64
865         return(1);      /* all names can have & taken on them */
ragge
1.1
866 }
867
868 /*
869  * at the end of the arguments of a ftn, set the automatic offset
870  */
871 void
872 cendarg()
873 {
874         autooff = AUTOINIT;
875 }
876
877 /*
ragge
1.36
878  * Return 1 if a variable of type type is OK to put in register.
879  */
880 int
881 cisreg(TWORD t)
882 {
883         if (t == FLOAT || t == DOUBLE || t == LDOUBLE)
884                 return 0/* not yet */
885         return 1;
886 }
887
888 /*
ragge
1.1
889  * return a node, for structure references, which is suitable for
890  * being added to a pointer of type t, in order to be off bits offset
891  * into a structure
892  * t, d, and s are the type, dimension offset, and sizeoffset
893  * For pdp10, return the type-specific index number which calculation
894  * is based on its size. For example, short a[3] would return 3.
895  * Be careful about only handling first-level pointers, the following
896  * indirections must be fullword.
897  */
898 NODE *
899 offcon(OFFSZ offTWORD tunion dimfun *dstruct suedef *sue)
900 {
901         register NODE *p;
902
903         if (xdebug)
904                 printf("offcon: OFFSZ %lld type %x dim %p siz %d\n",
905                     offtdsue->suesize);
906
907         p = bcon(0);
908         p->n_lval = off/SZCHAR/* Default */
909         return(p);
910 }
911
912 /*
913  * Allocate off bits on the stack.  p is a tree that when evaluated
ragge
1.45
914  * is the multiply count for off, t is a storeable node where to write
ragge
1.1
915  * the allocated address.
916  */
917 void
918 spalloc(NODE *tNODE *pOFFSZ off)
919 {
920         NODE *sp;
921
ragge
1.45
922         p = buildtree(MULpbcon(off/SZCHAR)); /* XXX word alignment? */
923
924         /* sub the size from sp */
ragge
1.55
925         sp = block(REGNILNILp->n_type0MKSUE(INT));
ragge
1.45
926         sp->n_lval = 0;
927         sp->n_rval = STKREG;
928         ecomp(buildtree(MINUSEQspp));
ragge
1.1
929
pantzer
1.100
930 #ifdef MACHOABI 
931         /* align to 16 bytes */
932         sp = block(REGNILNILp->n_type0MKSUE(INT));
933         sp->n_lval = 0;
934         sp->n_rval = STKREG;
935         ecomp(buildtree(PLUSEQspbcon(15)));
936         
937         sp = block(REGNILNILp->n_type0MKSUE(INT));
938         sp->n_lval = 0;
939         sp->n_rval = STKREG;
940         ecomp(buildtree(RSEQspbcon(4)));
941         
942         sp = block(REGNILNILp->n_type0MKSUE(INT));
943         sp->n_lval = 0;
944         sp->n_rval = STKREG;
945         ecomp(buildtree(LSEQspbcon(4)));
946 #endif
947         
948
ragge
1.1
949         /* save the address of sp */
950         sp = block(REGNILNILPTR+INTt->n_dft->n_sue);
951         sp->n_lval = 0;
952         sp->n_rval = STKREG;
953         t->n_type = sp->n_type;
954         ecomp(buildtree(ASSIGNtsp)); /* Emit! */
955
956 }
957
958 /*
ragge
1.48
959  * Print out a string of characters.
960  * Assume that the assembler understands C-style escape
ragge
1.70
961  * sequences.
ragge
1.48
962  */
963 void
ragge
1.70
964 instring(struct symtab *sp)
ragge
1.48
965 {
gmcgarry
1.83
966         char *s, *str = sp->sname;
967
gmcgarry
1.86
968 #if defined(ELFABI) || defined(PECOFFABI)
ragge
1.70
969
970         defloc(sp);
gmcgarry
1.83
971
972 #elif defined(MACHOABI)
973
974         extern int lastloc;
975         if (lastloc != STRNG)
976                 printf("        .cstring\n");