Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20071118172836

Diff

Diff from 1.4 to:

Annotations

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

Annotated File View

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