Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20060610150523

Diff

Diff from 1.39 to:

Annotations

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

Annotated File View

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