Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20060204174346

Diff

Diff from 1.37 to:

Annotations

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

Annotated File View

ragge
1.37
1 /*      $Id: local.c,v 1.37 2006/02/04 17:43: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
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.37
90         case CALL:
91                 /* Fix function call arguments. On x86, just add funarg */
92                 for (r = p->n_rightr->n_op == CMr = r->n_left) {
93                         if (r->n_right->n_op != STARG)
94                                 r->n_right = block(FUNARGr->n_rightNIL
95                                     r->n_right->n_typer->n_right->n_df,
96                                     r->n_right->n_sue);
97                 }
98                 if (r->n_op != STARG) {
99                         l = talloc();
100                         *l = *r;
101                         r->n_op = FUNARGr->n_left = lr->n_type = l->n_type;
102                 }
ragge
1.10
103                 break;
ragge
1.37
104                 
ragge
1.8
105         case CBRANCH:
106                 l = p->n_left;
107
108                 /*
109                  * Remove unneccessary conversion ops.
110                  */
ragge
1.10
111                 if (clogop(l->n_op) && l->n_left->n_op == SCONV) {
ragge
1.12
112                         if (coptype(l->n_op) != BITYPE)
113                                 break;
ragge
1.8
114                         if (l->n_right->n_op == ICON) {
115                                 r = l->n_left->n_left;
ragge
1.24
116                                 if (r->n_type >= FLOAT && r->n_type <= LDOUBLE)
117                                         break;
ragge
1.18
118                                 /* Type must be correct */
ragge
1.19
119                                 t = r->n_type;
ragge
1.8
120                                 nfree(l->n_left);
121                                 l->n_left = r;
ragge
1.19
122                                 l->n_type = t;
123                                 l->n_right->n_type = t;
ragge
1.17
124                         }
ragge
1.8
125                 }
126                 break;
127
ragge
1.1
128         case PCONV:
ragge
1.2
129                 ml = p->n_left->n_type;
ragge
1.1
130                 l = p->n_left;
131                 if ((ml == CHAR || ml == UCHAR || ml == SHORT || ml == USHORT)
132                     && l->n_op != ICON)
133                         break;
134                 l->n_type = p->n_type;
135                 l->n_qual = p->n_qual;
136                 l->n_df = p->n_df;
137                 l->n_sue = p->n_sue;
138                 nfree(p);
139                 p = l;
140                 break;
141
142         case SCONV:
143                 l = p->n_left;
ragge
1.4
144
ragge
1.25
145                 if (p->n_type == l->n_type) {
146                         nfree(p);
147                         return l;
148                 }
149
ragge
1.4
150                 if ((p->n_type & TMASK) == 0 && (l->n_type & TMASK) == 0 &&
pj
1.30
151                     btdims[p->n_type].suesize == btdims[l->n_type].suesize) {
ragge
1.4
152                         if (p->n_type != FLOAT && p->n_type != DOUBLE &&
ragge
1.15
153                             l->n_type != FLOAT && l->n_type != DOUBLE &&
154                             l->n_type != LDOUBLE && p->n_type != LDOUBLE) {
155                                 if (l->n_op == NAME || l->n_op == UMUL) {
156                                         l->n_type = p->n_type;
157                                         nfree(p);
158                                         return l;
159                                 }
ragge
1.4
160                         }
ragge
1.6
161                 }
162
ragge
1.3
163                 o = l->n_op;
ragge
1.4
164                 m = p->n_type;
ragge
1.1
165
166                 if (o == ICON) {
167                         CONSZ val = l->n_lval;
168
ragge
1.29
169                         if (!ISPTR(m)) /* Pointers don't need to be conv'd */
170                             switch (m) {
ragge
1.1
171                         case CHAR:
172                                 l->n_lval = (char)val;
173                                 break;
174                         case UCHAR:
175                                 l->n_lval = val & 0377;
176                                 break;
ragge
1.15
177                         case SHORT:
ragge
1.1
178                                 l->n_lval = (short)val;
179                                 break;
ragge
1.15
180                         case USHORT:
ragge
1.1
181                                 l->n_lval = val & 0177777;
182                                 break;
ragge
1.15
183                         case ULONG:
ragge
1.1
184                         case UNSIGNED:
185                                 l->n_lval = val & 0xffffffff;
186                                 break;
187                         case ENUMTY:
188                         case MOETY:
ragge
1.15
189                         case LONG:
ragge
1.1
190                         case INT:
191                                 l->n_lval = (int)val;
192                                 break;
193                         case LONGLONG:
194                                 l->n_lval = (long long)val;
ragge
1.15
195                                 break;
ragge
1.1
196                         case ULONGLONG:
197                                 l->n_lval = val;
198                                 break;
199                         case VOID:
200                                 break;
ragge
1.14
201                         case LDOUBLE:
ragge
1.1
202                         case DOUBLE:
203                         case FLOAT:
204                                 l->n_op = FCON;
205                                 l->n_dcon = val;
206                                 break;
207                         default:
208                                 cerror("unknown type %d"m);
209                         }
210                         l->n_type = m;
211                         nfree(p);
212                         return l;
213                 }
ragge
1.17
214                 if (DEUNSIGN(p->n_type) == SHORT &&
215                     DEUNSIGN(l->n_type) == SHORT) {
216                         nfree(p);
217                         p = l;
218                 }
ragge
1.36
219                 if ((p->n_type == CHAR || p->n_type == UCHAR ||
220                     p->n_type == SHORT || p->n_type == USHORT) &&
221                     (l->n_type == FLOAT || l->n_type == DOUBLE ||
222                     l->n_type == LDOUBLE)) {
223                         p = block(SCONVpNILp->n_typep->n_dfp->n_sue);
224                         p->n_left->n_type = INT;
225                         return p;
226                 }
ragge
1.1
227                 break;
228
ragge
1.15
229         case MOD:
230         case DIV:
231                 if (o == DIV && p->n_type != CHAR && p->n_type != SHORT)
232                         break;
ragge
1.16
233                 if (o == MOD && p->n_type != CHAR && p->n_type != SHORT)
ragge
1.15
234                         break;
235                 /* make it an int division by inserting conversions */
236                 p->n_left = block(SCONVp->n_leftNILINT0MKSUE(INT));
237                 p->n_right = block(SCONVp->n_rightNILINT0MKSUE(INT));
238                 p = block(SCONVpNILp->n_type0MKSUE(p->n_type));
239                 p->n_left->n_type = INT;
240                 break;
241
ragge
1.1
242         case PMCONV:
243         case PVCONV:
244                 ifp->n_right->n_op != ICON ) cerror"bad conversion"0);
245                 nfree(p);
246                 return(buildtree(o==PMCONV?MUL:DIVp->n_leftp->n_right));
247
ragge
1.31
248         case FORCE:
ragge
1.32
249                 /* put return value in return reg */
250                 p->n_op = ASSIGN;
251                 p->n_right = p->n_left;
252                 p->n_left = block(REGNILNILp->n_type0MKSUE(INT));
ragge
1.35
253                 p->n_left->n_rval = RETREG(p->n_type);
ragge
1.31
254                 break;
ragge
1.1
255         }
ragge
1.24
256 //printf("ut:\n");
257 //fwalk(p, eprint, 0);
ragge
1.1
258
259         return(p);
260 }
261
262 void
263 myp2tree(NODE *p)
264 {
265 }
266
267 /*ARGSUSED*/
268 int
269 andable(NODE *p)
270 {
271         return(1);  /* all names can have & taken on them */
272 }
273
274 /*
275  * at the end of the arguments of a ftn, set the automatic offset
276  */
277 void
278 cendarg()
279 {
280         autooff = AUTOINIT;
281 }
282
283 /*
ragge
1.36
284  * Return 1 if a variable of type type is OK to put in register.
285  */
286 int
287 cisreg(TWORD t)
288 {
289         if (t == FLOAT || t == DOUBLE || t == LDOUBLE)
290                 return 0/* not yet */
291         return 1;
292 }
293
294 /*
ragge
1.1
295  * return a node, for structure references, which is suitable for
296  * being added to a pointer of type t, in order to be off bits offset
297  * into a structure
298  * t, d, and s are the type, dimension offset, and sizeoffset
299  * For pdp10, return the type-specific index number which calculation
300  * is based on its size. For example, short a[3] would return 3.
301  * Be careful about only handling first-level pointers, the following
302  * indirections must be fullword.
303  */
304 NODE *
305 offcon(OFFSZ offTWORD tunion dimfun *dstruct suedef *sue)
306 {
307         register NODE *p;
308
309         if (xdebug)
310                 printf("offcon: OFFSZ %lld type %x dim %p siz %d\n",
311                     offtdsue->suesize);
312
313         p = bcon(0);
314         p->n_lval = off/SZCHAR/* Default */
315         return(p);
316 }
317
318 /*
319  * Allocate off bits on the stack.  p is a tree that when evaluated
320  * is the multiply count for off, t is a NAME node where to write
321  * the allocated address.
322  */
323 void
324 spalloc(NODE *tNODE *pOFFSZ off)
325 {
326         NODE *sp;
327
328         if ((off % SZINT) == 0)
329                 p =  buildtree(MULpbcon(off/SZINT));
330         else if ((off % SZSHORT) == 0) {
331                 p = buildtree(MULpbcon(off/SZSHORT));
332                 p = buildtree(PLUSpbcon(1));
333                 p = buildtree(RSpbcon(1));
334         } else if ((off % SZCHAR) == 0) {
335                 p = buildtree(MULpbcon(off/SZCHAR));
336                 p = buildtree(PLUSpbcon(3));
337                 p = buildtree(RSpbcon(2));
338         } else
339                 cerror("roundsp");
340
341         /* save the address of sp */
342         sp = block(REGNILNILPTR+INTt->n_dft->n_sue);
343         sp->n_lval = 0;
344         sp->n_rval = STKREG;
345         t->n_type = sp->n_type;
346         ecomp(buildtree(ASSIGNtsp)); /* Emit! */
347
348         /* add the size to sp */
349         sp = block(REGNILNILp->n_type00);
350         sp->n_lval = 0;
351         sp->n_rval = STKREG;
352         ecomp(buildtree(PLUSEQspp));
353 }
354
355 /*
ragge
1.21
356  * print out a constant node
357  * mat be associated with a label
358  */
359 void
360 ninval(NODE *p)
361 {
ragge
1.24
362         struct symtab *q;
ragge
1.23
363         TWORD t;
364
ragge
1.21
365         p = p->n_left;
ragge
1.23
366         t = p->n_type;
367         if (t > BTMASK)
368                 t = INT/* pointer */
369
370         switch (t) {
ragge
1.21
371         case LONGLONG:
372         case ULONGLONG:
373                 inval(p->n_lval & 0xffffffff);
374                 inval(p->n_lval >> 32);
375                 break;
376         case INT:
377         case UNSIGNED:
378                 printf("\t.long 0x%x", (int)p->n_lval);
ragge
1.24
379                 if ((q = p->n_sp) != NULL) {
380                         if ((q->sclass == STATIC && q->slevel > 0) ||
381                             q->sclass == ILABEL) {
382                                 printf("+" LABFMTq->soffset);
ragge
1.21
383                         } else
ragge
1.24
384                                 printf("+%s"exname(q->sname));
ragge
1.21
385                 }
386                 printf("\n");
387                 break;
388         default:
389                 cerror("ninval");
390         }
391 }
392
393 /*
ragge
1.17
394  * print out an integer.
ragge
1.1
395  */
396 void
ragge
1.17
397 inval(CONSZ word)
ragge
1.1
398 {
ragge
1.17
399         word &= 0xffffffff;
400         printf("        .long 0x%llx\n"word);
ragge
1.1
401 }
402
ragge
1.17
403 /* output code to initialize a floating point value */
ragge
1.1
404 /* the proper alignment has been obtained */
405 void
ragge
1.17
406 finval(NODE *p)
ragge
1.1
407 {
ragge
1.17
408         switch (p->n_type) {
409         case LDOUBLE:
410                 printf("\t.tfloat\t0t%.20Le\n"p->n_dcon);
ragge
1.14
411                 break;
ragge
1.17
412         case DOUBLE:
413                 printf("\t.dfloat\t0d%.20e\n", (double)p->n_dcon);
ragge
1.14
414                 break;
ragge
1.17
415         case FLOAT:
416                 printf("\t.ffloat\t0f%.20e\n", (float)p->n_dcon);
ragge
1.14
417                 break;
418         }
ragge
1.1
419 }
420
421 /* make a name look like an external name in the local machine */
422 char *
423 exname(char *p)
424 {
425         if (p == NULL)
426                 return "";
427         return p;
428 }
429
430 /*
431  * map types which are not defined on the local machine
432  */
ragge
1.15
433 TWORD
ragge
1.1
434 ctype(TWORD type)
435 {
436         switch (BTYPE(type)) {
437         case LONG:
438                 MODTYPE(type,INT);
439                 break;
440
441         case ULONG:
442                 MODTYPE(type,UNSIGNED);
ragge
1.13
443
ragge
1.1
444         }
445         return (type);
446 }
447
448 /* curid is a variable which is defined but
449  * is not initialized (and not a function );
ragge
1.17
450  * This routine returns the storage class for an uninitialized declaration
ragge
1.1
451  */
452 int
453 noinit()
454 {
455         return(EXTERN);
456 }
457
pj
1.30
458 void
459 calldec(NODE *pNODE *q
460 {
461 }
462
463 void
464 extdec(struct symtab *q)
465 {
466 }
467
ragge
1.1
468 /* make a common declaration for id, if reasonable */
469 void
470 commdec(struct symtab *q)
471 {
472         int off;
473
474         off = tsize(q->stypeq->sdfq->ssue);
ragge
1.8
475         off = (off+(SZCHAR-1))/SZCHAR;
ragge
1.7
476 #ifdef GCC_COMPAT
477         printf("        .comm %s,0%o\n"gcc_findname(q), off);
478 #else
ragge
1.1
479         printf("        .comm %s,0%o\n"exname(q->sname), off);
ragge
1.7
480 #endif
ragge
1.1
481 }
482
483 /* make a local common declaration for id, if reasonable */
484 void
485 lcommdec(struct symtab *q)
486 {
487         int off;
488
489         off = tsize(q->stypeq->sdfq->ssue);
ragge
1.8
490         off = (off+(SZCHAR-1))/SZCHAR;
ragge
1.1
491         if (q->slevel == 0)
ragge
1.7
492 #ifdef GCC_COMPAT
493                 printf("        .lcomm %s,0%o\n"gcc_findname(q), off);
494 #else
ragge
1.1
495                 printf("        .lcomm %s,0%o\n"exname(q->sname), off);
ragge
1.7
496 #endif
ragge
1.1
497         else
498                 printf("        .lcomm " LABFMT ",0%o\n"q->soffsetoff);
499 }
ragge
1.22
500
501 /*
502  * print a (non-prog) label.
503  */
504 void
505 deflab1(int label)
506 {
507         printf(LABFMT ":\n"label);
508 }
509
ragge
1.28
510 static char *loctbl[] = { "text""data""section .rodata""section .rodata" };
ragge
1.27
511
ragge
1.23
512 void
513 setloc1(int locc)
514 {
515         if (locc == lastloc)
516                 return;
517         lastloc = locc;
ragge
1.27
518         printf("        .%s\n"loctbl[locc]);
ragge
1.23
519 }
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-23 04:19 +0200