Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20041230103629

Diff

Diff from 1.24 to:

Annotations

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

Annotated File View

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