Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20060616093031

Diff

Diff from 1.40 to:

Annotations

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

Annotated File View

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