Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:pantzer:20090209202945

Diff

Diff from 1.98 to:

Annotations

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

Annotated File View

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