Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20060205183051

Diff

Diff from 1.38 to:

Annotations

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

Annotated File View

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