Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20050905170716

Diff

Diff from 1.32 to:

Annotations

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

Annotated File View

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