Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20060617163538

Diff

Diff from 1.41 to:

Annotations

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

Annotated File View

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