Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20041002145427

Diff

Diff from 1.18 to:

Annotations

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

Annotated File View

ragge
1.18
1 /*      $Id: local.c,v 1.18 2004/10/02 14:54:27 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;
53
54         switcho = p->n_op ){
55
56         case NAME:
57                 if ((q = p->n_sp) == NULL)
58                         return p/* Nothing to care about */
59
60                 switch (q->sclass) {
61
62                 case PARAM:
63                 case AUTO:
64                         /* fake up a structure reference */
65                         r = block(REGNILNILPTR+STRTY00);
66                         r->n_lval = 0;
67                         r->n_rval = FPREG;
68                         p = stref(block(STREFrp000));
69                         break;
70
71                 case STATIC:
72                         if (q->slevel == 0)
73                                 break;
74                         p->n_lval = 0;
75                         p->n_sp = q;
76                         if ((q->sflags & SLABEL) == 0)
77                                 cerror("STATIC");
78                         break;
79
80                 case REGISTER:
81                         p->n_op = REG;
82                         p->n_lval = 0;
83                         p->n_rval = q->soffset;
84                         break;
85
86                         }
87                 break;
88
ragge
1.10
89         case FUNARG:
90                 /* Args smaller than int are given as int */
91                 if (p->n_type != CHAR && p->n_type != UCHAR && 
92                     p->n_type != SHORT && p->n_type != USHORT)
93                         break;
94                 p->n_left = block(SCONVp->n_leftNILINT0MKSUE(INT));
95                 p->n_type = INT;
96                 p->n_sue = MKSUE(INT);
97                 p->n_rval = SZINT;
98                 break;
99
ragge
1.8
100         case CBRANCH:
101                 l = p->n_left;
102
103                 /*
104                  * Remove unneccessary conversion ops.
105                  */
ragge
1.10
106                 if (clogop(l->n_op) && l->n_left->n_op == SCONV) {
ragge
1.12
107                         if (coptype(l->n_op) != BITYPE)
108                                 break;
ragge
1.8
109                         if (l->n_right->n_op == ICON) {
110                                 r = l->n_left->n_left;
ragge
1.18
111                                 /* Type must be correct */
112                                 r->n_type = l->n_left->n_type;
ragge
1.8
113                                 nfree(l->n_left);
114                                 l->n_left = r;
ragge
1.17
115                         }
116 #if 0
117                           else if (l->n_right->n_op == SCONV &&
ragge
1.8
118                             l->n_left->n_type == l->n_right->n_type) {
119                                 r = l->n_left->n_left;
120                                 nfree(l->n_left);
121                                 l->n_left = r;
122                                 r = l->n_right->n_left;
123                                 nfree(l->n_right);
124                                 l->n_right = r;
125                         }
ragge
1.17
126 #endif
ragge
1.8
127                 }
128                 break;
129
ragge
1.1
130         case PCONV:
ragge
1.2
131                 ml = p->n_left->n_type;
ragge
1.1
132                 l = p->n_left;
133                 if ((ml == CHAR || ml == UCHAR || ml == SHORT || ml == USHORT)
134                     && l->n_op != ICON)
135                         break;
136                 l->n_type = p->n_type;
137                 l->n_qual = p->n_qual;
138                 l->n_df = p->n_df;
139                 l->n_sue = p->n_sue;
140                 nfree(p);
141                 p = l;
142                 break;
143
144         case SCONV:
145                 l = p->n_left;
ragge
1.4
146
147                 if ((p->n_type & TMASK) == 0 && (l->n_type & TMASK) == 0 &&
148                     btdim[p->n_type] == btdim[l->n_type]) {
149                         if (p->n_type != FLOAT && p->n_type != DOUBLE &&
ragge
1.15
150                             l->n_type != FLOAT && l->n_type != DOUBLE &&
151                             l->n_type != LDOUBLE && p->n_type != LDOUBLE) {
152                                 if (l->n_op == NAME || l->n_op == UMUL) {
153                                         l->n_type = p->n_type;
154                                         nfree(p);
155                                         return l;
156                                 }
ragge
1.4
157                         }
ragge
1.6
158                 }
159
ragge
1.11
160 #if 0
ragge
1.6
161                 if ((p->n_type == INT || p->n_type == UNSIGNED) &&
162                     ISPTR(l->n_type)) {
163                         nfree(p);
164                         return l;
ragge
1.4
165                 }
ragge
1.11
166 #endif
ragge
1.4
167
ragge
1.3
168                 o = l->n_op;
ragge
1.4
169                 m = p->n_type;
ragge
1.1
170
171                 if (o == ICON) {
172                         CONSZ val = l->n_lval;
173
174                         switch (m) {
175                         case CHAR:
176                                 l->n_lval = (char)val;
177                                 break;
178                         case UCHAR:
179                                 l->n_lval = val & 0377;
180                                 break;
ragge
1.15
181                         case SHORT:
ragge
1.1
182                                 l->n_lval = (short)val;
183                                 break;
ragge
1.15
184                         case USHORT:
ragge
1.1
185                                 l->n_lval = val & 0177777;
186                                 break;
ragge
1.15
187                         case ULONG:
ragge
1.1
188                         case UNSIGNED:
189                                 l->n_lval = val & 0xffffffff;
190                                 break;
191                         case ENUMTY:
192                         case MOETY:
ragge
1.15
193                         case LONG:
ragge
1.1
194                         case INT:
195                                 l->n_lval = (int)val;
196                                 break;
197                         case LONGLONG:
198                                 l->n_lval = (long long)val;
ragge
1.15
199                                 break;
ragge
1.1
200                         case ULONGLONG:
201                                 l->n_lval = val;
202                                 break;
203                         case VOID:
204                                 break;
ragge
1.14
205                         case LDOUBLE:
ragge
1.1
206                         case DOUBLE:
207                         case FLOAT:
208                                 l->n_op = FCON;
209                                 l->n_dcon = val;
210                                 break;
211                         default:
212                                 cerror("unknown type %d"m);
213                         }
214                         l->n_type = m;
215                         nfree(p);
216                         return l;
217                 }
ragge
1.17
218                 if (DEUNSIGN(p->n_type) == SHORT &&
219                     DEUNSIGN(l->n_type) == SHORT) {
220                         nfree(p);
221                         p = l;
222                 }
ragge
1.1
223                 break;
224
ragge
1.15
225         case MOD:
226         case DIV:
227                 if (o == DIV && p->n_type != CHAR && p->n_type != SHORT)
228                         break;
ragge
1.16
229                 if (o == MOD && p->n_type != CHAR && p->n_type != SHORT)
ragge
1.15
230                         break;
231                 /* make it an int division by inserting conversions */
232                 p->n_left = block(SCONVp->n_leftNILINT0MKSUE(INT));
233                 p->n_right = block(SCONVp->n_rightNILINT0MKSUE(INT));
234                 p = block(SCONVpNILp->n_type0MKSUE(p->n_type));
235                 p->n_left->n_type = INT;
236                 break;
237
ragge
1.1
238         case PMCONV:
239         case PVCONV:
240                 ifp->n_right->n_op != ICON ) cerror"bad conversion"0);
241                 nfree(p);
242                 return(buildtree(o==PMCONV?MUL:DIVp->n_leftp->n_right));
243
244         }
245
246         return(p);
247 }
248
249 void
250 myp2tree(NODE *p)
251 {
252 }
253
254 /*ARGSUSED*/
255 int
256 andable(NODE *p)
257 {
258         return(1);  /* all names can have & taken on them */
259 }
260
261 /*
262  * at the end of the arguments of a ftn, set the automatic offset
263  */
264 void
265 cendarg()
266 {
267         autooff = AUTOINIT;
268 }
269
270 /*
271  * is an automatic variable of type t OK for a register variable
272  */
273 int
274 cisreg(TWORD t)
275 {
ragge
1.13
276         if (t == INT || t == UNSIGNED || t == LONG || t == ULONG)
277                 return(1);
278         return 0/* XXX - fix reg assignment in pftn.c */
ragge
1.1
279 }
280
281 /*
282  * return a node, for structure references, which is suitable for
283  * being added to a pointer of type t, in order to be off bits offset
284  * into a structure
285  * t, d, and s are the type, dimension offset, and sizeoffset
286  * For pdp10, return the type-specific index number which calculation
287  * is based on its size. For example, short a[3] would return 3.
288  * Be careful about only handling first-level pointers, the following
289  * indirections must be fullword.
290  */
291 NODE *
292 offcon(OFFSZ offTWORD tunion dimfun *dstruct suedef *sue)
293 {
294         register NODE *p;
295
296         if (xdebug)
297                 printf("offcon: OFFSZ %lld type %x dim %p siz %d\n",
298                     offtdsue->suesize);
299
300         p = bcon(0);
301         p->n_lval = off/SZCHAR/* Default */
302         return(p);
303 }
304
305 /*
306  * Allocate off bits on the stack.  p is a tree that when evaluated
307  * is the multiply count for off, t is a NAME node where to write
308  * the allocated address.
309  */
310 void
311 spalloc(NODE *tNODE *pOFFSZ off)
312 {
313         NODE *sp;
314
315         if ((off % SZINT) == 0)
316                 p =  buildtree(MULpbcon(off/SZINT));
317         else if ((off % SZSHORT) == 0) {
318                 p = buildtree(MULpbcon(off/SZSHORT));
319                 p = buildtree(PLUSpbcon(1));
320                 p = buildtree(RSpbcon(1));
321         } else if ((off % SZCHAR) == 0) {
322                 p = buildtree(MULpbcon(off/SZCHAR));
323                 p = buildtree(PLUSpbcon(3));
324                 p = buildtree(RSpbcon(2));
325         } else
326                 cerror("roundsp");
327
328         /* save the address of sp */
329         sp = block(REGNILNILPTR+INTt->n_dft->n_sue);
330         sp->n_lval = 0;
331         sp->n_rval = STKREG;
332         t->n_type = sp->n_type;
333         ecomp(buildtree(ASSIGNtsp)); /* Emit! */
334
335         /* add the size to sp */
336         sp = block(REGNILNILp->n_type00);
337         sp->n_lval = 0;
338         sp->n_rval = STKREG;
339         ecomp(buildtree(PLUSEQspp));
340 }
341
342 /*
ragge
1.17
343  * print out an integer.
ragge
1.1
344  */
345 void
ragge
1.17
346 inval(CONSZ word)
ragge
1.1
347 {
ragge
1.17
348         word &= 0xffffffff;
349         printf("        .long 0x%llx\n"word);
ragge
1.1
350 }
351
ragge
1.17
352 /* output code to initialize a floating point value */
ragge
1.1
353 /* the proper alignment has been obtained */
354 void
ragge
1.17
355 finval(NODE *p)
ragge
1.1
356 {
ragge
1.17
357         switch (p->n_type) {
358         case LDOUBLE:
359                 printf("\t.tfloat\t0t%.20Le\n"p->n_dcon);
ragge
1.14
360                 break;
ragge
1.17
361         case DOUBLE:
362                 printf("\t.dfloat\t0d%.20e\n", (double)p->n_dcon);
ragge
1.14
363                 break;
ragge
1.17
364         case FLOAT:
365                 printf("\t.ffloat\t0f%.20e\n", (float)p->n_dcon);
ragge
1.14
366                 break;
367         }
ragge
1.1
368 }
369
370 /* make a name look like an external name in the local machine */
371 char *
372 exname(char *p)
373 {
374         if (p == NULL)
375                 return "";
376         return p;
377 }
378
379 /*
380  * map types which are not defined on the local machine
381  */
ragge
1.15
382 TWORD
ragge
1.1
383 ctype(TWORD type)
384 {
385         switch (BTYPE(type)) {
386         case LONG:
387                 MODTYPE(type,INT);
388                 break;
389
390         case ULONG:
391                 MODTYPE(type,UNSIGNED);
ragge
1.13
392
ragge
1.1
393         }
394         return (type);
395 }
396
397 /* curid is a variable which is defined but
398  * is not initialized (and not a function );
ragge
1.17
399  * This routine returns the storage class for an uninitialized declaration
ragge
1.1
400  */
401 int
402 noinit()
403 {
404         return(EXTERN);
405 }
406
407 /* make a common declaration for id, if reasonable */
408 void
409 commdec(struct symtab *q)
410 {
411         int off;
412
413         off = tsize(q->stypeq->sdfq->ssue);
ragge
1.8
414         off = (off+(SZCHAR-1))/SZCHAR;
ragge
1.7
415 #ifdef GCC_COMPAT
416         printf("        .comm %s,0%o\n"gcc_findname(q), off);
417 #else
ragge
1.1
418         printf("        .comm %s,0%o\n"exname(q->sname), off);
ragge
1.7
419 #endif
ragge
1.1
420 }
421
422 /* make a local common declaration for id, if reasonable */
423 void
424 lcommdec(struct symtab *q)
425 {
426         int off;
427
428         off = tsize(q->stypeq->sdfq->ssue);
ragge
1.8
429         off = (off+(SZCHAR-1))/SZCHAR;
ragge
1.1
430         if (q->slevel == 0)
ragge
1.7
431 #ifdef GCC_COMPAT
432                 printf("        .lcomm %s,0%o\n"gcc_findname(q), off);
433 #else
ragge
1.1
434                 printf("        .lcomm %s,0%o\n"exname(q->sname), off);
ragge
1.7
435 #endif
ragge
1.1
436         else
437                 printf("        .lcomm " LABFMT ",0%o\n"q->soffsetoff);
438 }
439
440 /*
441  * Debugger code - ignore.
442  */
443 void
444 prcstab(int a)
445 {
446 }
447
448 void
449 pfstab(char *a)
450 {
451 }
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-11-01 06:33 +0100