Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:gmcgarry:20071105145703

Diff

Diff from 1.2 to:

Annotations

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

Annotated File View

gmcgarry
1.2
1 /*      $Id: local.c,v 1.2 2007/11/05 14:57:03 gmcgarry 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  * MIPS port by Jan Enoksson (janeno-1@student.ltu.se) and
31  * Simon Olsson (simols-1@student.ltu.se) 2005.
32  */
33
34 # include "pass1.h"
35
36 /*      this file contains code which is dependent on the target machine */
37
gmcgarry
1.2
38 static int inbitsinval;
39
40 static void
41 fixupfuncargs(NODE *rint *reg)
42 {
43 #ifdef PCC_DEBUG
44         if (xdebug)
45                 printf("fixupfuncargs(): r=%p\n"r);
46 #endif
47
48         if (r->n_op == CM) {
49                 /* recurse to the bottom of the tree */
50                 fixupfuncargs(r->n_leftreg);
51
52                 if (*reg > A3) {
53                         r->n_right = block(FUNARGr->n_rightNIL
54                             r->n_right->n_typer->n_right->n_df,
55                             r->n_right->n_sue);
56                         (*reg)++;
57                         return;
58                 }
59
60                 r->n_right = block(ASSIGNNILr->n_right
61                             r->n_right->n_typer->n_right->n_df,
62                             r->n_right->n_sue);
63                 r->n_right->n_left = block(REGNILNILr->n_right->n_type0MKSUE(INT));
64
65                 if (r->n_right->n_type == ULONGLONG || r->n_right->n_type == LONGLONG) {
66                         r->n_right->n_left->n_rval = A0A1 + (*reg) - A0;
67                         (*reg) += 2;
68                 } else {
69                         r->n_right->n_left->n_rval = (*reg);
70                         (*reg)++;
71                 }
72
73         } else {
74                 NODE *l = talloc();
75                 *l = *r;
76                 r->n_op = ASSIGN;
77                 r->n_left = block(REGNILNILl->n_type0MKSUE(INT));
78                 r->n_right = l;
79                 if (r->n_right->n_type == ULONGLONG || r->n_right->n_type == LONGLONG) {
80                         r->n_left->n_rval = A0A1 + (*reg) - A0;
81                         (*reg) += 2;
82                 } else {
83                         r->n_left->n_rval = (*reg);
84                         (*reg)++;
85                 }
86         }
87 }
88
89
ragge
1.1
90 NODE *
91 clocal(NODE *p)
92 {
93         /* this is called to do local transformations on
94            an expression tree preparitory to its being
95            written out in intermediate code.
96         */
97
98         /* the major essential job is rewriting the
99            automatic variables and arguments in terms of
100            REG and OREG nodes */
101         /* conversion ops which are not necessary are also clobbered here */
102         /* in addition, any special features (such as rewriting
103            exclusive or) are easily handled here as well */
104
105         register struct symtab *q;
106         register NODE *r, *l;
107         register int o;
108         register int mml;
109         TWORD t;
110
111 //printf("in:\n");
112 //fwalk(p, eprint, 0);
113         switcho = p->n_op ){
114
gmcgarry
1.2
115         case CALL:
116
117                 {
118                 int reg = A0;
119                 fixupfuncargs(p->n_right, &reg);
120                 }
121
122 #if 0
123                 for (r = p->n_rightr->n_op == CMr = r->n_left) {
124                         if (r->n_right->n_op != STARG &&
125                             r->n_right->n_op != FUNARG) {
126                                 r->n_right = block(FUNARGr->n_rightNIL
127                                     r->n_right->n_typer->n_right->n_df,
128                                     r->n_right->n_sue);
129                                 }
130                 }
131                 if (r->n_op != STARG && r->n_op != FUNARG) {
132                         l = talloc();
133                         *l = *r;
134                         r->n_op = FUNARGr->n_left = lr->n_type = l->n_type;
135                 }
136
137                 break;
138 #endif
139
ragge
1.1
140         case NAME:
141                 if ((q = p->n_sp) == NULL)
142                         return p/* Nothing to care about */
143
144                 switch (q->sclass) {
145
146                 case PARAM:
147                 case AUTO:
148                         /* fake up a structure reference */
149                         r = block(REGNILNILPTR+STRTY00);
150                         r->n_lval = 0;
151                         r->n_rval = FPREG;
152                         p = stref(block(STREFrp000));
153                         break;
154
155                 case STATIC:
156                         if (q->slevel == 0)
157                                 break;
158                         p->n_lval = 0;
159                         p->n_sp = q;
160                         break;
161
162                 case REGISTER:
163                         p->n_op = REG;
164                         p->n_lval = 0;
165                         p->n_rval = q->soffset;
166                         break;
167
168                         }
169                 break;
170
171         case FUNARG:
172                 /* Args smaller than int are given as int */
173                 if (p->n_type != CHAR && p->n_type != UCHAR && 
174                     p->n_type != SHORT && p->n_type != USHORT)
175                         break;
176                 p->n_left = block(SCONVp->n_leftNILINT0MKSUE(INT));
177                 p->n_type = INT;
178                 p->n_sue = MKSUE(INT);
179                 p->n_rval = SZINT;
180                 break;
181
182         case CBRANCH:
183                 l = p->n_left;
184
185                 /*
186                  * Remove unneccessary conversion ops.
187                  */
188                 if (clogop(l->n_op) && l->n_left->n_op == SCONV) {
189                         if (coptype(l->n_op) != BITYPE)
190                                 break;
191                         if (l->n_right->n_op == ICON) {
192                                 r = l->n_left->n_left;
193                                 if (r->n_type >= FLOAT && r->n_type <= LDOUBLE)
194                                         break;
195                                 /* Type must be correct */
196                                 t = r->n_type;
197                                 nfree(l->n_left);
198                                 l->n_left = r;
199                                 l->n_type = t;
200                                 l->n_right->n_type = t;
201                         }
202 #if 0
203                           else if (l->n_right->n_op == SCONV &&
204                             l->n_left->n_type == l->n_right->n_type) {
205                                 r = l->n_left->n_left;
206                                 nfree(l->n_left);
207                                 l->n_left = r;
208                                 r = l->n_right->n_left;
209                                 nfree(l->n_right);
210                                 l->n_right = r;
211                         }
212 #endif
213                 }
214                 break;
215
216         case PCONV:
217                 ml = p->n_left->n_type;
218                 l = p->n_left;
219                 if ((ml == CHAR || ml == UCHAR || ml == SHORT || ml == USHORT)
220                     && l->n_op != ICON)
221                         break;
222                 l->n_type = p->n_type;
223                 l->n_qual = p->n_qual;
224                 l->n_df = p->n_df;
225                 l->n_sue = p->n_sue;
226                 nfree(p);
227                 p = l;
228                 break;
229
230         case SCONV:
231                 l = p->n_left;
232
233                 if (p->n_type == l->n_type) {
234                         nfree(p);
235                         return l;
236                 }
237
238                 if ((p->n_type & TMASK) == 0 && (l->n_type & TMASK) == 0 &&
239                     btdims[p->n_type].suesize == btdims[l->n_type].suesize) {
240                         if (p->n_type != FLOAT && p->n_type != DOUBLE &&
241                             l->n_type != FLOAT && l->n_type != DOUBLE &&
242                             l->n_type != LDOUBLE && p->n_type != LDOUBLE) {
243                                 if (l->n_op == NAME || l->n_op == UMUL) {
244                                         l->n_type = p->n_type;
245                                         nfree(p);
246                                         return l;
247                                 }
248                         }
249                 }
250
251 #if 0
252                 if ((p->n_type == INT || p->n_type == UNSIGNED) &&
253                     ISPTR(l->n_type)) {
254                         nfree(p);
255                         return l;
256                 }
257 #endif
258
259                 o = l->n_op;
260                 m = p->n_type;
261
262                 if (o == ICON) {
263                         CONSZ val = l->n_lval;
264
265                         if (!ISPTR(m)) /* Pointers don't need to be conv'd */
266                             switch (m) {
267                         case CHAR:
268                                 l->n_lval = (char)val;
269                                 break;
270                         case UCHAR:
271                                 l->n_lval = val & 0377;
272                                 break;
273                         case SHORT:
274                                 l->n_lval = (short)val;
275                                 break;
276                         case USHORT:
277                                 l->n_lval = val & 0177777;
278                                 break;
279                         case ULONG:
280                         case UNSIGNED:
281                                 l->n_lval = val & 0xffffffff;
282                                 break;
283                         case ENUMTY:
284                         case MOETY:
285                         case LONG:
286                         case INT:
287                                 l->n_lval = (int)val;
288                                 break;
289                         case LONGLONG:
290                                 l->n_lval = (long long)val;
291                                 break;
292                         case ULONGLONG:
293                                 l->n_lval = val;
294                                 break;
295                         case VOID:
296                                 break;
297                         case LDOUBLE:
298                         case DOUBLE:
299                         case FLOAT:
300                                 l->n_op = FCON;
301                                 l->n_dcon = val;
302                                 break;
303                         default:
304                                 cerror("unknown type %d"m);
305                         }
306                         l->n_type = m;
307                         nfree(p);
308                         return l;
309                 }
310                 if (DEUNSIGN(p->n_type) == SHORT &&
311                     DEUNSIGN(l->n_type) == SHORT) {
312                         nfree(p);
313                         p = l;
314                 }
315                 break;
316
317         case MOD:
318         case DIV:
319                 if (o == DIV && p->n_type != CHAR && p->n_type != SHORT)
320                         break;
321                 if (o == MOD && p->n_type != CHAR && p->n_type != SHORT)
322                         break;
323                 /* make it an int division by inserting conversions */
324                 p->n_left = block(SCONVp->n_leftNILINT0MKSUE(INT));
325                 p->n_right = block(SCONVp->n_rightNILINT0MKSUE(INT));
326                 p = block(SCONVpNILp->n_type0MKSUE(p->n_type));
327                 p->n_left->n_type = INT;
328                 break;
329
330         case PMCONV:
331         case PVCONV:
332                 ifp->n_right->n_op != ICON ) cerror"bad conversion"0);
333                 nfree(p);
334                 return(buildtree(o==PMCONV?MUL:DIVp->n_leftp->n_right));
335
336         case FORCE:
337                 /* put return value in return reg */
338                 p->n_op = ASSIGN;
339                 p->n_right = p->n_left;
gmcgarry
1.2
340                 p->n_left = block(REGNILNILp->n_type0MKSUE(INT));
341                 p->n_left->n_rval = RETREG(p->n_type);;
ragge
1.1
342                 break;
343         }
344 //printf("ut:\n");
345 //fwalk(p, eprint, 0);
346
347
348         return(p);
349 }
350
351 void
352 myp2tree(NODE *p)
353 {
354 }
355
356 /*ARGSUSED*/
357 int
358 andable(NODE *p)
359 {
360         return(1);  /* all names can have & taken on them */
361 }
362
363 /*
364  * at the end of the arguments of a ftn, set the automatic offset
365  */
366 void
367 cendarg()
368 {
369         autooff = AUTOINIT;
370 }
371
372 /*
373  * is an automatic variable of type t OK for a register variable
374  */
375 int
376 cisreg(TWORD t)
377 {
378         if (t == INT || t == UNSIGNED || t == LONG || t == ULONG)
379                 return(1);
380         return 0/* XXX - fix reg assignment in pftn.c */
381 }
382
383 /*
384  * return a node, for structure references, which is suitable for
385  * being added to a pointer of type t, in order to be off bits offset
386  * into a structure
387  * t, d, and s are the type, dimension offset, and sizeoffset
388  * For pdp10, return the type-specific index number which calculation
389  * is based on its size. For example, short a[3] would return 3.
390  * Be careful about only handling first-level pointers, the following
391  * indirections must be fullword.
392  */
393 NODE *
394 offcon(OFFSZ offTWORD tunion dimfun *dstruct suedef *sue)
395 {
396         register NODE *p;
397
398         if (xdebug)
399                 printf("offcon: OFFSZ %lld type %x dim %p siz %d\n",
400                     offtdsue->suesize);
401
402         p = bcon(0);
403         p->n_lval = off/SZCHAR/* Default */
404         return(p);
405 }
406
407 /*
408  * Allocate off bits on the stack.  p is a tree that when evaluated
409  * is the multiply count for off, t is a NAME node where to write
410  * the allocated address.
411  */
412 void
413 spalloc(NODE *tNODE *pOFFSZ off)
414 {
415         NODE *sp;
416
417         if ((off % SZINT) == 0)
418                 p =  buildtree(MULpbcon(off/SZINT));
419         else if ((off % SZSHORT) == 0) {
420                 p = buildtree(MULpbcon(off/SZSHORT));
421                 p = buildtree(PLUSpbcon(1));
422                 p = buildtree(RSpbcon(1));
423         } else if ((off % SZCHAR) == 0) {
424                 p = buildtree(MULpbcon(off/SZCHAR));
425                 p = buildtree(PLUSpbcon(3));
426                 p = buildtree(RSpbcon(2));
427         } else
428                 cerror("roundsp");
429
430         /* save the address of sp */
431         sp = block(REGNILNILPTR+INTt->n_dft->n_sue);
432         sp->n_lval = 0;
433         sp->n_rval = STKREG;
434         t->n_type = sp->n_type;
435         ecomp(buildtree(ASSIGNtsp)); /* Emit! */
436
437         /* add the size to sp */
438         sp = block(REGNILNILp->n_type00);
439         sp->n_lval = 0;
440         sp->n_rval = STKREG;
441         ecomp(buildtree(PLUSEQspp));
442 }
443
444 /*
445  * print out a constant node
446  * mat be associated with a label
447  */
448 void
gmcgarry
1.2
449 ninval(CONSZ offint fszNODE *p)
ragge
1.1
450 {
451         struct symtab *q;
452         TWORD t;
gmcgarry
1.2
453         int i;
ragge
1.1
454
455         p = p->n_left;
456         t = p->n_type;
457         if (t > BTMASK)
458                 t = INT/* pointer */
459
460         switch (t) {
461         case LONGLONG:
462         case ULONGLONG:
gmcgarry
1.2
463                 i = (p->n_lval >> 32);
464                 p->n_lval &= 0xffffffff;
465                 p->n_type = INT;
466                 ninval(off32p);
467                 p->n_lval = i;
468                 ninval(off+3232p);
ragge
1.1
469                 break;
470         case INT:
471         case UNSIGNED:
472                 printf("\t.long 0x%x", (int)p->n_lval);
473                 if ((q = p->n_sp) != NULL) {
474                         if ((q->sclass == STATIC && q->slevel > 0) ||
475                             q->sclass == ILABEL) {
476                                 printf("+" LABFMTq->soffset);
477                         } else
478                                 printf("+%s"exname(q->sname));
479                 }
480                 printf("\n");
481                 break;
482         default:
483                 cerror("ninval");
484         }
485 }
486
gmcgarry
1.2
487 #if 0
ragge
1.1
488 /*
489  * print out an integer.
490  */
491 void
492 inval(CONSZ word)
493 {
494         word &= 0xffffffff;
495         printf("        .long 0x%llx\n"word);
496 }
497
498 /* output code to initialize a floating point value */
499 /* the proper alignment has been obtained */
500 void
501 finval(NODE *p)
502 {
503         switch (p->n_type) {
504         case LDOUBLE:
505                 printf("\t.tfloat\t0t%.20Le\n"p->n_dcon);
506                 break;
507         case DOUBLE:
508                 printf("\t.dfloat\t0d%.20e\n", (double)p->n_dcon);
509                 break;
510         case FLOAT:
511                 printf("\t.ffloat\t0f%.20e\n", (float)p->n_dcon);
512                 break;
513         }
514 }
gmcgarry
1.2
515 #endif
ragge
1.1
516
517 /* make a name look like an external name in the local machine */
518 char *
519 exname(char *p)
520 {
521         if (p == NULL)
522                 return "";
523         return p;
524 }
525
526 /*
527  * map types which are not defined on the local machine
528  */
529 TWORD
530 ctype(TWORD type)
531 {
532         switch (BTYPE(type)) {
533         case LONG:
534                 MODTYPE(type,INT);
535                 break;
536
537         case ULONG:
538                 MODTYPE(type,UNSIGNED);
539
540         }
541         return (type);
542 }
543
544 /* curid is a variable which is defined but
545  * is not initialized (and not a function );
546  * This routine returns the storage class for an uninitialized declaration
547  */
548 int
549 noinit()
550 {
551         return(EXTERN);
552 }
553
554 void
555 calldec(NODE *pNODE *q
556 {
557 }
558
559 void
560 extdec(struct symtab *q)
561 {
562 }
563
564 /* make a common declaration for id, if reasonable */
565 void
566 commdec(struct symtab *q)
567 {
568         int off;
569
570         off = tsize(q->stypeq->sdfq->ssue);
571         off = (off+(SZCHAR-1))/SZCHAR;
572
573 #ifdef GCC_COMPAT
574         printf("        .comm %s,0%o\n"gcc_findname(q), off);
575 #else
576         printf("        .comm %s,0%o\n"exname(q->sname), off);
577 #endif
578 }
579
580 /* make a local common declaration for id, if reasonable */
581 void
582 lcommdec(struct symtab *q)
583 {
584         int off;
585
586         off = tsize(q->stypeq->sdfq->ssue);
587         off = (off+(SZCHAR-1))/SZCHAR;
588         if (q->slevel == 0)
589 #ifdef GCC_COMPAT
590                 printf("        .lcomm %s,0%o\n"gcc_findname(q), off);
591 #else
592                 printf("        .lcomm %s,0%o\n"exname(q->sname), off);
593 #endif
594         else
595                 printf("        .lcomm " LABFMT ",0%o\n"q->soffsetoff);
596 }
597
598 /*
599  * print a (non-prog) label.
600  */
601 void
602 deflab1(int label)
603 {
604         printf(LABFMT ":\n"label);
605 }
606
gmcgarry
1.2
607 static char *loctbl[] = { "text""data""data""data" };
ragge
1.1
608
609 void
610 setloc1(int locc)
611 {
612         if (locc == lastloc)
613                 return;
614         lastloc = locc;
615         printf("        .%s\n"loctbl[locc]);
616 }
gmcgarry
1.2
617
618 /*
619  * Initialize a bitfield.
620  */
621 void
622 infld(CONSZ offint fszCONSZ val)
623 {
624         if (idebug)
625                 printf("infld off %lld, fsz %d, val %lld inbits %d\n",
626                     offfszvalinbits);
627         val &= (1 << fsz)-1;
628         while (fsz + inbits >= SZCHAR) {
629                 inval |= (val << inbits);
630                 printf("\t.byte %d\n"inval & 255);
631                 fsz -= (SZCHAR - inbits);
632                 val >>= (SZCHAR - inbits);
633                 inval = inbits = 0;
634         }
635         if (fsz) {
636                 inval |= (val << inbits);
637                 inbits += fsz;
638         }
639 }
640
641 /*
642  * set fsz bits in sequence to zero.
643  */
644 void
645 zbits(OFFSZ offint fsz)
646 {
647         int m;
648
649         if (idebug)
650                 printf("zbits off %lld, fsz %d inbits %d\n"offfszinbits);
651         if ((m = (inbits % SZCHAR))) {
652                 m = SZCHAR - m;
653                 if (fsz < m) {
654                         inbits += fsz;
655                         return;
656                 } else {
657                         fsz -= m;
658                         printf("\t.byte %d\n"inval);
659                         inval = inbits = 0;
660                 }
661         }
662         if (fsz >= SZCHAR) {
663                 printf("\t.zero %d\n"fsz/SZCHAR);
664                 fsz -= (fsz/SZCHAR) * SZCHAR;
665         }
666         if (fsz) {
667                 inval = 0;
668                 inbits = fsz;
669         }
670 }
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-21 02:04 +0200