Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20050218165058

Diff

Diff from 1.27 to:

Annotations

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

Annotated File View

ragge
1.27
1 /*      $Id: local.c,v 1.27 2005/02/18 16:50:58 ragge Exp $     */
ragge
1.1
2 /*
3  * Copyright (c) 2003 Anders Magnusson (ragge@ludd.luth.se).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29
30 # include "pass1.h"
31
32 /*      this file contains code which is dependent on the target machine */
33
34 NODE *
35 clocal(NODE *p)
36 {
37         /* this is called to do local transformations on
38            an expression tree preparitory to its being
39            written out in intermediate code.
40         */
41
42         /* the major essential job is rewriting the
43            automatic variables and arguments in terms of
44            REG and OREG nodes */
45         /* conversion ops which are not necessary are also clobbered here */
46         /* in addition, any special features (such as rewriting
47            exclusive or) are easily handled here as well */
48
49         register struct symtab *q;
ragge
1.2
50         register NODE *r, *l;
ragge
1.1
51         register int o;
52         register int mml;
ragge
1.19
53         TWORD t;
ragge
1.1
54
ragge
1.24
55 //printf("in:\n");
56 //fwalk(p, eprint, 0);
ragge
1.1
57         switcho = p->n_op ){
58
59         case NAME:
60                 if ((q = p->n_sp) == NULL)
61                         return p/* Nothing to care about */
62
63                 switch (q->sclass) {
64
65                 case PARAM:
66                 case AUTO:
67                         /* fake up a structure reference */
68                         r = block(REGNILNILPTR+STRTY00);
69                         r->n_lval = 0;
70                         r->n_rval = FPREG;
71                         p = stref(block(STREFrp000));
72                         break;
73
74                 case STATIC:
75                         if (q->slevel == 0)
76                                 break;
77                         p->n_lval = 0;
78                         p->n_sp = q;
79                         break;
80
81                 case REGISTER:
82                         p->n_op = REG;
83                         p->n_lval = 0;
84                         p->n_rval = q->soffset;
85                         break;
86
87                         }
88                 break;
89
ragge
1.10
90         case FUNARG:
91                 /* Args smaller than int are given as int */
92                 if (p->n_type != CHAR && p->n_type != UCHAR && 
93                     p->n_type != SHORT && p->n_type != USHORT)
94                         break;
95                 p->n_left = block(SCONVp->n_leftNILINT0MKSUE(INT));
96                 p->n_type = INT;
97                 p->n_sue = MKSUE(INT);
98                 p->n_rval = SZINT;
99                 break;
100
ragge
1.8
101         case CBRANCH:
102                 l = p->n_left;
103
104                 /*
105                  * Remove unneccessary conversion ops.
106                  */
ragge
1.10
107                 if (clogop(l->n_op) && l->n_left->n_op == SCONV) {
ragge
1.12
108                         if (coptype(l->n_op) != BITYPE)
109                                 break;
ragge
1.8
110                         if (l->n_right->n_op == ICON) {
111                                 r = l->n_left->n_left;
ragge
1.24
112                                 if (r->n_type >= FLOAT && r->n_type <= LDOUBLE)
113                                         break;
ragge
1.18
114                                 /* Type must be correct */
ragge
1.19
115                                 t = r->n_type;
ragge
1.8
116                                 nfree(l->n_left);
117                                 l->n_left = r;
ragge
1.19
118                                 l->n_type = t;
119                                 l->n_right->n_type = t;
ragge
1.17
120                         }
121 #if 0
122                           else if (l->n_right->n_op == SCONV &&
ragge
1.8
123                             l->n_left->n_type == l->n_right->n_type) {
124                                 r = l->n_left->n_left;
125                                 nfree(l->n_left);
126                                 l->n_left = r;
127                                 r = l->n_right->n_left;
128                                 nfree(l->n_right);
129                                 l->n_right = r;
130                         }
ragge
1.17
131 #endif
ragge
1.8
132                 }
133                 break;
134
ragge
1.1
135         case PCONV:
ragge
1.2
136                 ml = p->n_left->n_type;
ragge
1.1
137                 l = p->n_left;
138                 if ((ml == CHAR || ml == UCHAR || ml == SHORT || ml == USHORT)
139                     && l->n_op != ICON)
140                         break;
141                 l->n_type = p->n_type;
142                 l->n_qual = p->n_qual;
143                 l->n_df = p->n_df;
144                 l->n_sue = p->n_sue;
145                 nfree(p);
146                 p = l;
147                 break;
148
149         case SCONV:
150                 l = p->n_left;
ragge
1.4
151
ragge
1.25
152                 if (p->n_type == l->n_type) {
153                         nfree(p);
154                         return l;
155                 }
156
ragge
1.4
157                 if ((p->n_type & TMASK) == 0 && (l->n_type & TMASK) == 0 &&
158                     btdim[p->n_type] == btdim[l->n_type]) {
159                         if (p->n_type != FLOAT && p->n_type != DOUBLE &&
ragge
1.15
160                             l->n_type != FLOAT && l->n_type != DOUBLE &&
161                             l->n_type != LDOUBLE && p->n_type != LDOUBLE) {
162                                 if (l->n_op == NAME || l->n_op == UMUL) {
163                                         l->n_type = p->n_type;
164                                         nfree(p);
165                                         return l;
166                                 }
ragge
1.4
167                         }
ragge
1.6
168                 }
169
ragge
1.11
170 #if 0
ragge
1.6
171                 if ((p->n_type == INT || p->n_type == UNSIGNED) &&
172                     ISPTR(l->n_type)) {
173                         nfree(p);
174                         return l;
ragge
1.4
175                 }
ragge
1.11
176 #endif
ragge
1.4
177
ragge
1.3
178                 o = l->n_op;
ragge
1.4
179                 m = p->n_type;
ragge
1.1
180
181                 if (o == ICON) {
182                         CONSZ val = l->n_lval;
183
184                         switch (m) {
185                         case CHAR:
186                                 l->n_lval = (char)val;
187                                 break;
188                         case UCHAR:
189                                 l->n_lval = val & 0377;
190                                 break;
ragge
1.15
191                         case SHORT:
ragge
1.1
192                                 l->n_lval = (short)val;
193                                 break;
ragge
1.15
194                         case USHORT:
ragge
1.1
195                                 l->n_lval = val & 0177777;
196                                 break;
ragge
1.15
197                         case ULONG:
ragge
1.1
198                         case UNSIGNED:
199                                 l->n_lval = val & 0xffffffff;
200                                 break;
201                         case ENUMTY:
202                         case MOETY:
ragge
1.15
203                         case LONG:
ragge
1.1
204                         case INT:
205                                 l->n_lval = (int)val;
206                                 break;
207                         case LONGLONG:
208                                 l->n_lval = (long long)val;
ragge
1.15
209                                 break;
ragge
1.1
210                         case ULONGLONG:
211                                 l->n_lval = val;
212                                 break;
213                         case VOID:
214                                 break;
ragge
1.14
215                         case LDOUBLE:
ragge
1.1
216                         case DOUBLE:
217                         case FLOAT:
218                                 l->n_op = FCON;
219                                 l->n_dcon = val;
220                                 break;
221                         default:
222                                 cerror("unknown type %d"m);
223                         }
224                         l->n_type = m;
225                         nfree(p);
226                         return l;
227                 }
ragge
1.17
228                 if (DEUNSIGN(p->n_type) == SHORT &&
229                     DEUNSIGN(l->n_type) == SHORT) {
230                         nfree(p);
231                         p = l;
232                 }
ragge
1.1
233                 break;
234
ragge
1.15
235         case MOD:
236         case DIV:
237                 if (o == DIV && p->n_type != CHAR && p->n_type != SHORT)
238                         break;
ragge
1.16
239                 if (o == MOD && p->n_type != CHAR && p->n_type != SHORT)
ragge
1.15
240                         break;
241                 /* make it an int division by inserting conversions */
242                 p->n_left = block(SCONVp->n_leftNILINT0MKSUE(INT));
243                 p->n_right = block(SCONVp->n_rightNILINT0MKSUE(INT));
244                 p = block(SCONVpNILp->n_type0MKSUE(p->n_type));
245                 p->n_left->n_type = INT;
246                 break;
247
ragge
1.1
248         case PMCONV:
249         case PVCONV:
250                 ifp->n_right->n_op != ICON ) cerror"bad conversion"0);
251                 nfree(p);
252                 return(buildtree(o==PMCONV?MUL:DIVp->n_leftp->n_right));
253
254         }
ragge
1.24
255 //printf("ut:\n");
256 //fwalk(p, eprint, 0);
ragge
1.1
257
258         return(p);
259 }
260
261 void
262 myp2tree(NODE *p)
263 {
264 }
265
266 /*ARGSUSED*/
267 int
268 andable(NODE *p)
269 {
270         return(1);  /* all names can have & taken on them */
271 }
272
273 /*
274  * at the end of the arguments of a ftn, set the automatic offset
275  */
276 void
277 cendarg()
278 {
279         autooff = AUTOINIT;
280 }
281
282 /*
283  * is an automatic variable of type t OK for a register variable
284  */
285 int
286 cisreg(TWORD t)
287 {
ragge
1.13
288         if (t == INT || t == UNSIGNED || t == LONG || t == ULONG)
289                 return(1);
290         return 0/* XXX - fix reg assignment in pftn.c */
ragge
1.1
291 }
292
293 /*
294  * return a node, for structure references, which is suitable for
295  * being added to a pointer of type t, in order to be off bits offset
296  * into a structure
297  * t, d, and s are the type, dimension offset, and sizeoffset
298  * For pdp10, return the type-specific index number which calculation
299  * is based on its size. For example, short a[3] would return 3.
300  * Be careful about only handling first-level pointers, the following
301  * indirections must be fullword.
302  */
303 NODE *
304 offcon(OFFSZ offTWORD tunion dimfun *dstruct suedef *sue)
305 {
306         register NODE *p;
307
308         if (xdebug)
309                 printf("offcon: OFFSZ %lld type %x dim %p siz %d\n",
310                     offtdsue->suesize);
311
312         p = bcon(0);
313         p->n_lval = off/SZCHAR/* Default */
314         return(p);
315 }
316
317 /*
318  * Allocate off bits on the stack.  p is a tree that when evaluated
319  * is the multiply count for off, t is a NAME node where to write
320  * the allocated address.
321  */
322 void
323 spalloc(NODE *tNODE *pOFFSZ off)
324 {
325         NODE *sp;
326
327         if ((off % SZINT) == 0)
328                 p =  buildtree(MULpbcon(off/SZINT));
329         else if ((off % SZSHORT) == 0) {
330                 p = buildtree(MULpbcon(off/SZSHORT));
331                 p = buildtree(PLUSpbcon(1));
332                 p = buildtree(RSpbcon(1));
333         } else if ((off % SZCHAR) == 0) {
334                 p = buildtree(MULpbcon(off/SZCHAR));
335                 p = buildtree(PLUSpbcon(3));
336                 p = buildtree(RSpbcon(2));
337         } else
338                 cerror("roundsp");
339
340         /* save the address of sp */
341         sp = block(REGNILNILPTR+INTt->n_dft->n_sue);
342         sp->n_lval = 0;
343         sp->n_rval = STKREG;
344         t->n_type = sp->n_type;
345         ecomp(buildtree(ASSIGNtsp)); /* Emit! */
346
347         /* add the size to sp */
348         sp = block(REGNILNILp->n_type00);
349         sp->n_lval = 0;
350         sp->n_rval = STKREG;
351         ecomp(buildtree(PLUSEQspp));
352 }
353
354 /*
ragge
1.21
355  * print out a constant node
356  * mat be associated with a label
357  */
358 void
359 ninval(NODE *p)
360 {
ragge
1.24
361         struct symtab *q;
ragge
1.23
362         TWORD t;
363
ragge
1.21
364         p = p->n_left;
ragge
1.23
365         t = p->n_type;
366         if (t > BTMASK)
367                 t = INT/* pointer */
368
369         switch (t) {
ragge
1.21
370         case LONGLONG:
371         case ULONGLONG:
372                 inval(p->n_lval & 0xffffffff);
373                 inval(p->n_lval >> 32);
374                 break;
375         case INT:
376         case UNSIGNED:
377                 printf("\t.long 0x%x", (int)p->n_lval);
ragge
1.24
378                 if ((q = p->n_sp) != NULL) {
379                         if ((q->sclass == STATIC && q->slevel > 0) ||
380                             q->sclass == ILABEL) {
381                                 printf("+" LABFMTq->soffset);
ragge
1.21
382                         } else
ragge
1.24
383                                 printf("+%s"exname(q->sname));
ragge
1.21
384                 }
385                 printf("\n");
386                 break;
387         default:
388                 cerror("ninval");
389         }
390 }
391
392 /*
ragge
1.17
393  * print out an integer.
ragge
1.1
394  */
395 void
ragge
1.17
396 inval(CONSZ word)
ragge
1.1
397 {
ragge
1.17
398         word &= 0xffffffff;
399         printf("        .long 0x%llx\n"word);
ragge
1.1
400 }
401
ragge
1.17
402 /* output code to initialize a floating point value */
ragge
1.1
403 /* the proper alignment has been obtained */
404 void
ragge
1.17
405 finval(NODE *p)
ragge
1.1
406 {
ragge
1.17
407         switch (p->n_type) {
408         case LDOUBLE:
409                 printf("\t.tfloat\t0t%.20Le\n"p->n_dcon);
ragge
1.14
410                 break;
ragge
1.17
411         case DOUBLE:
412                 printf("\t.dfloat\t0d%.20e\n", (double)p->n_dcon);
ragge
1.14
413                 break;
ragge
1.17
414         case FLOAT:
415                 printf("\t.ffloat\t0f%.20e\n", (float)p->n_dcon);
ragge
1.14
416                 break;
417         }
ragge
1.1
418 }
419
420 /* make a name look like an external name in the local machine */
421 char *
422 exname(char *p)
423 {
424         if (p == NULL)
425                 return "";
426         return p;
427 }
428
429 /*
430  * map types which are not defined on the local machine
431  */
ragge
1.15
432 TWORD
ragge
1.1
433 ctype(TWORD type)
434 {
435         switch (BTYPE(type)) {
436         case LONG:
437                 MODTYPE(type,INT);
438                 break;
439
440         case ULONG:
441                 MODTYPE(type,UNSIGNED);
ragge
1.13
442
ragge
1.1
443         }
444         return (type);
445 }
446
447 /* curid is a variable which is defined but
448  * is not initialized (and not a function );
ragge
1.17
449  * This routine returns the storage class for an uninitialized declaration
ragge
1.1
450  */
451 int
452 noinit()
453 {
454         return(EXTERN);
455 }
456
457 /* make a common declaration for id, if reasonable */
458 void
459 commdec(struct symtab *q)
460 {
461         int off;
462
463         off = tsize(q->stypeq->sdfq->ssue);
ragge
1.8
464         off = (off+(SZCHAR-1))/SZCHAR;
ragge
1.7
465 #ifdef GCC_COMPAT
466         printf("        .comm %s,0%o\n"gcc_findname(q), off);
467 #else
ragge
1.1
468         printf("        .comm %s,0%o\n"exname(q->sname), off);
ragge
1.7
469 #endif
ragge
1.1
470 }
471
472 /* make a local common declaration for id, if reasonable */
473 void
474 lcommdec(struct symtab *q)
475 {
476         int off;
477
478         off = tsize(q->stypeq->sdfq->ssue);
ragge
1.8
479         off = (off+(SZCHAR-1))/SZCHAR;
ragge
1.1
480         if (q->slevel == 0)
ragge
1.7
481 #ifdef GCC_COMPAT
482                 printf("        .lcomm %s,0%o\n"gcc_findname(q), off);
483 #else
ragge
1.1
484                 printf("        .lcomm %s,0%o\n"exname(q->sname), off);
ragge
1.7
485 #endif
ragge
1.1
486         else
487                 printf("        .lcomm " LABFMT ",0%o\n"q->soffsetoff);
488 }
ragge
1.22
489
490 /*
491  * print a (non-prog) label.
492  */
493 void
494 deflab1(int label)
495 {
496         printf(LABFMT ":\n"label);
497 }
498
ragge
1.27
499 static char *loctbl[] = { "text""data""rodata""rodata" };
500
ragge
1.23
501 void
502 setloc1(int locc)
503 {
504         if (locc == lastloc)
505                 return;
506         lastloc = locc;
ragge
1.27
507         printf("        .%s\n"loctbl[locc]);
ragge
1.23
508 }
FishEye: Open Source License registered to PCC.
Your maintenance has expired. You can renew your license at http://www.atlassian.com/fisheye/renew
Atlassian FishEye, CVS analysis. (Version:1.6.3 Build:build-336 2008-11-04) - Administration - Page generated 2014-09-30 12:16 +0200