Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:gmcgarry:20071213042014

Diff

Diff from 1.9 to:

Annotations

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

Annotated File View

gmcgarry
1.9
1 /*      $Id: local.c,v 1.9 2007/12/13 04:20:14 gmcgarry 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  * MIPS port by Jan Enoksson (janeno-1@student.ltu.se) and
31  * Simon Olsson (simols-1@student.ltu.se) 2005.
32  */
33
gmcgarry
1.7
34 #include <assert.h>
gmcgarry
1.3
35 #include "pass1.h"
ragge
1.1
36
gmcgarry
1.2
37 static int inbitsinval;
38
gmcgarry
1.3
39 /* this is called to do local transformations on
40  * an expression tree preparitory to its being
41  * written out in intermediate code.
42  */
ragge
1.1
43 NODE *
44 clocal(NODE *p)
45 {
gmcgarry
1.3
46         struct symtab *q;
47         NODE *r, *l;
48         int o;
gmcgarry
1.7
49         int m;
gmcgarry
1.6
50         TWORD ty;
gmcgarry
1.8
51         int tmpnrisptrvoid = 0;
ragge
1.1
52
gmcgarry
1.7
53 #ifdef PCC_DEBUG
54         if (xdebug) {
gmcgarry
1.8
55                 printf("clocal in: %p\n"p);
gmcgarry
1.7
56                 fwalk(peprint0);
57         }
58 #endif
gmcgarry
1.3
59
gmcgarry
1.6
60         switch (o = p->n_op) {
ragge
1.1
61
gmcgarry
1.8
62         case UCALL:
63         case CALL:
64         case STCALL:
65         case USTCALL:
66                 if (p->n_type == VOID)
67                         break;
68                 /*
69                  * if the function returns void*, ecode() invokes
70                  * delvoid() to convert it to uchar*.
71                  * We just let this happen on the ASSIGN to the temp,
72                  * and cast the pointer back to void* on access
73                  * from the temp.
74                  */
75                 if (p->n_type == PTR+VOID)
76                         isptrvoid = 1;
77                 r = tempnode(0p->n_typep->n_dfp->n_sue);
78                 tmpnr = r->n_lval;
79                 r = block(ASSIGNrpp->n_typep->n_dfp->n_sue);
gmcgarry
1.9
80
gmcgarry
1.8
81                 p = tempnode(tmpnrr->n_typer->n_dfr->n_sue);
82                 if (isptrvoid) {
83                         p = block(PCONVpNILPTR+VOID,
84                             p->n_dfMKSUE(PTR+VOID));
85                 }
gmcgarry
1.9
86                 p = buildtree(COMOPrp);
gmcgarry
1.8
87                 break;
88
ragge
1.1
89         case NAME:
90                 if ((q = p->n_sp) == NULL)
91                         return p/* Nothing to care about */
92
93                 switch (q->sclass) {
94
95                 case PARAM:
96                 case AUTO:
97                         /* fake up a structure reference */
98                         r = block(REGNILNILPTR+STRTY00);
99                         r->n_lval = 0;
gmcgarry
1.3
100                         r->n_rval = FP;
ragge
1.1
101                         p = stref(block(STREFrp000));
102                         break;
103
104                 case STATIC:
105                         if (q->slevel == 0)
106                                 break;
107                         p->n_lval = 0;
108                         p->n_sp = q;
109                         break;
110
111                 case REGISTER:
112                         p->n_op = REG;
113                         p->n_lval = 0;
114                         p->n_rval = q->soffset;
115                         break;
116
gmcgarry
1.7
117                 }
ragge
1.1
118                 break;
119
120         case FUNARG:
121                 /* Args smaller than int are given as int */
122                 if (p->n_type != CHAR && p->n_type != UCHAR && 
123                     p->n_type != SHORT && p->n_type != USHORT)
124                         break;
125                 p->n_left = block(SCONVp->n_leftNILINT0MKSUE(INT));
126                 p->n_type = INT;
127                 p->n_sue = MKSUE(INT);
128                 p->n_rval = SZINT;
129                 break;
130
131         case CBRANCH:
132                 l = p->n_left;
133
134                 /*
135                  * Remove unneccessary conversion ops.
136                  */
137                 if (clogop(l->n_op) && l->n_left->n_op == SCONV) {
138                         if (coptype(l->n_op) != BITYPE)
139                                 break;
140                         if (l->n_right->n_op == ICON) {
141                                 r = l->n_left->n_left;
142                                 if (r->n_type >= FLOAT && r->n_type <= LDOUBLE)
143                                         break;
144                                 /* Type must be correct */
gmcgarry
1.6
145                                 ty = r->n_type;
ragge
1.1
146                                 nfree(l->n_left);
147                                 l->n_left = r;
gmcgarry
1.6
148                                 l->n_type = ty;
149                                 l->n_right->n_type = ty;
ragge
1.1
150                         }
151 #if 0
152                           else if (l->n_right->n_op == SCONV &&
153                             l->n_left->n_type == l->n_right->n_type) {
154                                 r = l->n_left->n_left;
155                                 nfree(l->n_left);
156                                 l->n_left = r;
157                                 r = l->n_right->n_left;
158                                 nfree(l->n_right);
159                                 l->n_right = r;
160                         }
161 #endif
162                 }
163                 break;
164
165         case PCONV:
gmcgarry
1.7
166                 /* Remove redundant PCONV's. Be careful */
ragge
1.1
167                 l = p->n_left;
gmcgarry
1.7
168                 if (l->n_op == ICON) {
169                         l->n_lval = (unsigned)l->n_lval;
170                         goto delp;
171                 }
gmcgarry
1.8
172                 if (l->n_type < INT || DEUNSIGN(l->n_type) == LONGLONG) {
gmcgarry
1.7
173                         /* float etc? */
174                         p->n_left = block(SCONVlNIL,
175                             UNSIGNED0MKSUE(UNSIGNED));
176                         break;
177                 }
178                 /* if left is SCONV, cannot remove */
179                 if (l->n_op == SCONV)
180                         break;
181
182                 /* avoid ADDROF TEMP */
183                 if (l->n_op == ADDROF && l->n_left->n_op == TEMP)
ragge
1.1
184                         break;
gmcgarry
1.7
185
186                 /* if conversion to another pointer type, just remove */
187                 if (p->n_type > BTMASK && l->n_type > BTMASK)
188                         goto delp;
189                 break;
190
191         delp:   l->n_type = p->n_type;
ragge
1.1
192                 l->n_qual = p->n_qual;
193                 l->n_df = p->n_df;
194                 l->n_sue = p->n_sue;
195                 nfree(p);
196                 p = l;
197                 break;
198
199         case SCONV:
200                 l = p->n_left;
201
202                 if (p->n_type == l->n_type) {
203                         nfree(p);
gmcgarry
1.8
204                         p = l;
205                         break;
ragge
1.1
206                 }
207
208                 if ((p->n_type & TMASK) == 0 && (l->n_type & TMASK) == 0 &&
209                     btdims[p->n_type].suesize == btdims[l->n_type].suesize) {
210                         if (p->n_type != FLOAT && p->n_type != DOUBLE &&
211                             l->n_type != FLOAT && l->n_type != DOUBLE &&
212                             l->n_type != LDOUBLE && p->n_type != LDOUBLE) {
gmcgarry
1.8
213                                 if (l->n_op == NAME || l->n_op == UMUL ||
214                                     l->n_op == TEMP) {
ragge
1.1
215                                         l->n_type = p->n_type;
216                                         nfree(p);
gmcgarry
1.8
217                                         p = l;
218                                         break;
ragge
1.1
219                                 }
220                         }
221                 }
222
gmcgarry
1.8
223                 if (DEUNSIGN(p->n_type) == INT && DEUNSIGN(l->n_type) == INT &&
224                     coptype(l->n_op) == BITYPE) {
225                         l->n_type = p->n_type;
226                         nfree(p);
227                         p = l;
228                 }
229
230                 if (DEUNSIGN(p->n_type) == SHORT &&
231                     DEUNSIGN(l->n_type) == SHORT) {
ragge
1.1
232                         nfree(p);
gmcgarry
1.8
233                         p = l;
ragge
1.1
234                 }
235
gmcgarry
1.9
236                 /* convert float/double to int before to (u)char/(u)short */
gmcgarry
1.8
237                 if ((DEUNSIGN(p->n_type) == CHAR ||
238                     DEUNSIGN(p->n_type) == SHORT) &&
239                     (l->n_type == FLOAT || l->n_type == DOUBLE ||
240                     l->n_type == LDOUBLE)) {
241                         p = block(SCONVpNILp->n_typep->n_dfp->n_sue);
242                         p->n_left->n_type = INT;
243                         break;
244                 }
245
gmcgarry
1.9
246                 /* convert (u)char/(u)short to int before float/double */
247                 if  ((p->n_type == FLOAT || p->n_type == DOUBLE ||
248                     p->n_type == LDOUBLE) && (DEUNSIGN(l->n_type) == CHAR ||
249                     DEUNSIGN(l->n_type) == SHORT)) {
250                         p = block(SCONVpNILp->n_typep->n_dfp->n_sue);
251                         p->n_left->n_type = INT;
252                         break;
253                 }
gmcgarry
1.8
254
ragge
1.1
255                 o = l->n_op;
256                 m = p->n_type;
257
258                 if (o == ICON) {
259                         CONSZ val = l->n_lval;
260
261                         if (!ISPTR(m)) /* Pointers don't need to be conv'd */
262                             switch (m) {
263                         case CHAR:
264                                 l->n_lval = (char)val;
265                                 break;
266                         case UCHAR:
267                                 l->n_lval = val & 0377;
268                                 break;
269                         case SHORT:
270                                 l->n_lval = (short)val;
271                                 break;
272                         case USHORT:
273                                 l->n_lval = val & 0177777;
274                                 break;
275                         case ULONG:
276                         case UNSIGNED:
277                                 l->n_lval = val & 0xffffffff;
278                                 break;
279                         case LONG:
280                         case INT:
281                                 l->n_lval = (int)val;
282                                 break;
283                         case LONGLONG:
284                                 l->n_lval = (long long)val;
285                                 break;
286                         case ULONGLONG:
287                                 l->n_lval = val;
288                                 break;
289                         case VOID:
290                                 break;
291                         case LDOUBLE:
292                         case DOUBLE:
293                         case FLOAT:
294                                 l->n_op = FCON;
295                                 l->n_dcon = val;
296                                 break;
297                         default:
298                                 cerror("unknown type %d"m);
299                         }
300                         l->n_type = m;
301                         nfree(p);
302                         p = l;
303                 }
304                 break;
305
306         case MOD:
307         case DIV:
308                 if (o == DIV && p->n_type != CHAR && p->n_type != SHORT)
309                         break;
310                 if (o == MOD && p->n_type != CHAR && p->n_type != SHORT)
311                         break;
312                 /* make it an int division by inserting conversions */
313                 p->n_left = block(SCONVp->n_leftNILINT0MKSUE(INT));
314                 p->n_right = block(SCONVp->n_rightNILINT0MKSUE(INT));
315                 p = block(SCONVpNILp->n_type0MKSUE(p->n_type));
316                 p->n_left->n_type = INT;
317                 break;
318
319         case PMCONV:
320         case PVCONV:
321                 ifp->n_right->n_op != ICON ) cerror"bad conversion"0);
322                 nfree(p);
gmcgarry
1.8
323                 p = buildtree(o==PMCONV?MUL:DIVp->n_leftp->n_right);
324                 break;
ragge
1.1
325
326         case FORCE:
327                 /* put return value in return reg */
328                 p->n_op = ASSIGN;
329                 p->n_right = p->n_left;
gmcgarry
1.2
330                 p->n_left = block(REGNILNILp->n_type0MKSUE(INT));
gmcgarry
1.3
331                 p->n_left->n_rval = RETREG(p->n_type);
ragge
1.1
332                 break;
333         }
gmcgarry
1.3
334
gmcgarry
1.8
335 #ifdef PCC_DEBUG
336         if (xdebug) {
337                 printf("clocal out: %p\n"p);
338                 fwalk(peprint0);
339         }
340 #endif
ragge
1.1
341
342         return(p);
343 }
344
345 void
346 myp2tree(NODE *p)
347 {
348 }
349
350 /*ARGSUSED*/
351 int
352 andable(NODE *p)
353 {
354         return(1);  /* all names can have & taken on them */
355 }
356
357 /*
358  * at the end of the arguments of a ftn, set the automatic offset
359  */
360 void
361 cendarg()
362 {
363         autooff = AUTOINIT;
364 }
365
366 /*
367  * is an automatic variable of type t OK for a register variable
368  */
369 int
370 cisreg(TWORD t)
371 {
372         if (t == INT || t == UNSIGNED || t == LONG || t == ULONG)
373                 return(1);
374         return 0/* XXX - fix reg assignment in pftn.c */
375 }
376
377 /*
378  * return a node, for structure references, which is suitable for
379  * being added to a pointer of type t, in order to be off bits offset
380  * into a structure
381  * t, d, and s are the type, dimension offset, and sizeoffset
382  * Be careful about only handling first-level pointers, the following
383  * indirections must be fullword.
384  */
385 NODE *
386 offcon(OFFSZ offTWORD tunion dimfun *dstruct suedef *sue)
387 {
gmcgarry
1.3
388         NODE *p;
ragge
1.1
389
390         if (xdebug)
391                 printf("offcon: OFFSZ %lld type %x dim %p siz %d\n",
392                     offtdsue->suesize);
393
gmcgarry
1.3
394         p = bcon(off/SZCHAR);
395         return p;
ragge
1.1
396 }
397
398 /*
399  * Allocate off bits on the stack.  p is a tree that when evaluated
400  * is the multiply count for off, t is a NAME node where to write
401  * the allocated address.
402  */
403 void
404 spalloc(NODE *tNODE *pOFFSZ off)
405 {
406         NODE *sp;
407
408         if ((off % SZINT) == 0)
409                 p =  buildtree(MULpbcon(off/SZINT));
410         else if ((off % SZSHORT) == 0) {
411                 p = buildtree(MULpbcon(off/SZSHORT));
412                 p = buildtree(PLUSpbcon(1));
413                 p = buildtree(RSpbcon(1));
414         } else if ((off % SZCHAR) == 0) {
415                 p = buildtree(MULpbcon(off/SZCHAR));
416                 p = buildtree(PLUSpbcon(3));
417                 p = buildtree(RSpbcon(2));
418         } else
419                 cerror("roundsp");
420
421         /* save the address of sp */
422         sp = block(REGNILNILPTR+INTt->n_dft->n_sue);
gmcgarry
1.6
423         sp->n_rval = SP;
ragge
1.1
424         t->n_type = sp->n_type;
425         ecomp(buildtree(ASSIGNtsp)); /* Emit! */
426
gmcgarry
1.9
427         /* subtract the size from sp */
ragge
1.1
428         sp = block(REGNILNILp->n_type00);
429         sp->n_lval = 0;
gmcgarry
1.6
430         sp->n_rval = SP;
gmcgarry
1.9
431         ecomp(buildtree(MINUSEQspp));
ragge
1.1
432 }
433
434 /*
435  * print out a constant node
436  * mat be associated with a label
437  */
438 void
gmcgarry
1.2
439 ninval(CONSZ offint fszNODE *p)
ragge
1.1
440 {
gmcgarry
1.3
441         union { float fdouble dlong double lint i[3]; } u;
442         struct symtab *q;
443         TWORD t;
444 #ifndef USE_GAS
gmcgarry
1.5
445         int ij;
gmcgarry
1.3
446 #endif
ragge
1.1
447
gmcgarry
1.3
448         t = p->n_type;
449         if (t > BTMASK)
450                 t = INT/* pointer */
451
452         if (p->n_op != ICON && p->n_op != FCON)
453                 cerror("ninval: init node not constant");
454
455         if (p->n_op == ICON && p->n_sp != NULL && DEUNSIGN(t) != INT)
456                 uerror("element not constant");
457
458         switch (t) {
459         case LONGLONG:
460         case ULONGLONG:
461 #ifdef USE_GAS
462                 printf("\t.dword 0x%llx\n", (long long)p->n_lval);
463 #else
gmcgarry
1.5
464                 i = p->n_lval >> 32;
465                 j = p->n_lval & 0xffffffff;
gmcgarry
1.3
466                 p->n_type = INT;
gmcgarry
1.5
467                 if (bigendian) {
468                         p->n_lval = j;
469                         ninval(off32p);
470                         p->n_lval = i;
471                         ninval(off+3232p);
472                 } else {
473                         p->n_lval = i;
474                         ninval(off32p);
475                         p->n_lval = j;
476                         ninval(off+3232p);
477                 }
gmcgarry
1.3
478 #endif
479                 break;
480         case BOOL:
481                 if (p->n_lval > 1)
482                         p->n_lval = p->n_lval != 0;
483                 /* FALLTHROUGH */
484         case INT:
485         case UNSIGNED:
486                 printf("\t.word 0x%x", (int)p->n_lval);
487                 if ((q = p->n_sp) != NULL) {
488                         if ((q->sclass == STATIC && q->slevel > 0) ||
489                             q->sclass == ILABEL) {
490                                 printf("+" LABFMTq->soffset);
491                         } else
492                                 printf("+%s"exname(q->sname));
493                 }
494                 printf("\n");
495                 break;
496         case SHORT:
497         case USHORT:
498                 printf("\t.half 0x%x\n", (int)p->n_lval & 0xffff);
499                 break;
500         case CHAR:
501         case UCHAR:
502                 printf("\t.byte %d\n", (int)p->n_lval & 0xff);
503                 break;
504         case LDOUBLE:
505         case DOUBLE:
506                 u.d = (double)p->n_dcon;
gmcgarry
1.9
507                 if (bigendian) {
508                         printf("\t.word\t0x%x\n"u.i[0]);
509                         printf("\t.word\t0x%x\n"u.i[1]);
510                 } else {
511                         printf("\t.word\t0x%x\n"u.i[1]);
512                         printf("\t.word\t0x%x\n"u.i[0]);
513                 }
gmcgarry
1.3
514                 break;
515         case FLOAT:
516                 u.f = (float)p->n_dcon;
517                 printf("\t.word\t0x%x\n"u.i[0]);
518                 break;
519         default:
520                 cerror("ninval");
521         }
ragge
1.1
522 }
523
524 /* make a name look like an external name in the local machine */
525 char *
526 exname(char *p)
527 {
528         if (p == NULL)
529                 return "";
530         return p;
531 }
532
533 /*
534  * map types which are not defined on the local machine
535  */
536 TWORD
537 ctype(TWORD type)
538 {
539         switch (BTYPE(type)) {
540         case LONG:
541                 MODTYPE(type,INT);
542                 break;
543
544         case ULONG:
545                 MODTYPE(type,UNSIGNED);
546
547         }
548         return (type);
549 }
550
551 /* curid is a variable which is defined but
552  * is not initialized (and not a function );
553  * This routine returns the storage class for an uninitialized declaration
554  */
555 int
556 noinit()
557 {
558         return(EXTERN);
559 }
560
561 void
562 calldec(NODE *pNODE *q
563 {
564 }
565
566 void
567 extdec(struct symtab *q)
568 {
569 }
570
571 /* make a common declaration for id, if reasonable */
572 void
573 commdec(struct symtab *q)
574 {
575         int off;
576
577         off = tsize(q->stypeq->sdfq->ssue);
578         off = (off+(SZCHAR-1))/SZCHAR;
579
580 #ifdef GCC_COMPAT
gmcgarry
1.9
581         printf("        .comm %s,%d\n"gcc_findname(q), off);
ragge
1.1
582 #else
gmcgarry
1.9
583         printf("        .comm %s,%d\n"exname(q->sname), off);
ragge
1.1
584 #endif
585 }
586
587 /* make a local common declaration for id, if reasonable */
588 void
589 lcommdec(struct symtab *q)
590 {
591         int off;
592
593         off = tsize(q->stypeq->sdfq->ssue);
594         off = (off+(SZCHAR-1))/SZCHAR;
595         if (q->slevel == 0)
596 #ifdef GCC_COMPAT
gmcgarry
1.9
597                 printf("\t.lcomm %s,%d\n"gcc_findname(q), off);
ragge
1.1
598 #else
gmcgarry
1.9
599                 printf("\t.lcomm %s,%d\n"exname(q->sname), off);
ragge
1.1
600 #endif
601         else
gmcgarry
1.9
602                 printf("\t.lcomm " LABFMT ",%d\n"q->soffsetoff);
ragge
1.1
603 }
604
605 /*
606  * print a (non-prog) label.
607  */
608 void
609 deflab1(int label)
610 {
611         printf(LABFMT ":\n"label);
612 }
613
gmcgarry
1.9
614 /* ro-text, rw-data, ro-data, ro-strings */
615 static char *loctbl[] = { "text""data""rdata""rdata" };
ragge
1.1
616
617 void
618 setloc1(int locc)
619 {
gmcgarry
1.6
620         if ((locc == lastloc) || (lastloc == DATA && locc == STRNG) ||
621             (locc == STRNG && lastloc == DATA))
ragge
1.1
622                 return;
623         lastloc = locc;
gmcgarry
1.3
624         printf("\t.%s\n"loctbl[locc]);
ragge
1.1
625 }
gmcgarry
1.2
626
627 /*
628  * Initialize a bitfield.
629  */
630 void
631 infld(CONSZ offint fszCONSZ val)
632 {
633         if (idebug)
634                 printf("infld off %lld, fsz %d, val %lld inbits %d\n",
635                     offfszvalinbits);
636         val &= (1 << fsz)-1;
637         while (fsz + inbits >= SZCHAR) {
638                 inval |= (val << inbits);
639                 printf("\t.byte %d\n"inval & 255);
640                 fsz -= (SZCHAR - inbits);
641                 val >>= (SZCHAR - inbits);
642                 inval = inbits = 0;
643         }
644         if (fsz) {
645                 inval |= (val << inbits);
646                 inbits += fsz;
647         }
648 }
649
650 /*
651  * set fsz bits in sequence to zero.
652  */
653 void
654 zbits(OFFSZ offint fsz)
655 {
656         int m;
657
658         if (idebug)
659                 printf("zbits off %lld, fsz %d inbits %d\n"offfszinbits);
660         if ((m = (inbits % SZCHAR))) {
661                 m = SZCHAR - m;
662                 if (fsz < m) {
663                         inbits += fsz;
664                         return;
665                 } else {
666                         fsz -= m;
667                         printf("\t.byte %d\n"inval);
668                         inval = inbits = 0;
669                 }
670         }
671         if (fsz >= SZCHAR) {
672                 printf("\t.zero %d\n"fsz/SZCHAR);
673                 fsz -= (fsz/SZCHAR) * SZCHAR;
674         }
675         if (fsz) {
676                 inval = 0;
677                 inbits = fsz;
678         }
679 }
gmcgarry
1.6
680
681 /*
682  * va_start(ap, last) implementation.
683  *
684  * f is the NAME node for this builtin function.
685  * a is the argument list containing:
686  *         CM
687  *      ap   last
gmcgarry
1.7
688  *
689  * It turns out that this is easy on MIPS.  Just write the
690  * argument registers to the stack in va_arg_start() and
691  * use the traditional method of walking the stackframe.
gmcgarry
1.6
692  */
693 NODE *
694 mips_builtin_stdarg_start(NODE *fNODE *a)
695 {
gmcgarry
1.7
696         NODE *p, *q;
gmcgarry
1.6
697         int sz = 1;
gmcgarry
1.7
698         int i;
gmcgarry
1.6
699
700         /* check num args and type */
701         if (a == NULL || a->n_op != CM || a->n_left->n_op == CM ||
702             !ISPTR(a->n_left->n_type))
703                 goto bad;
704
gmcgarry
1.7
705         /*
706          * look at the offset of the last org to calculate the
707          * number of remain registers that need to be written
708          * to the stack.
709          */
710         if (xtemps) {
711                 for (i = 0i < nargregsi++) {
712                         q = block(REGNILNILPTR+INT0MKSUE(INT));
713                         q->n_rval = A0 + i;
714                         p = block(REGNILNILPTR+INT0MKSUE(INT));
715                         p->n_rval = SP;
716                         p = block(PLUSpbcon(ARGINIT+i), PTR+INT0MKSUE(INT));
717                         p = buildtree(UMULpNIL);
718                         p = buildtree(ASSIGNpq);
719                         ecomp(p);
720                 }
721         }
722
gmcgarry
1.6
723         /* must first deal with argument size; use int size */
724         p = a->n_right;
gmcgarry
1.7
725         if (p->n_type < INT) {
726                 /* round up to word */
gmcgarry
1.6
727                 sz = SZINT / tsize(p->n_typep->n_dfp->n_sue);
gmcgarry
1.7
728         }
729
730         /*
731          * Once again, if xtemps, the register is written to a
732          * temp.  We cannot take the address of the temp and
733          * walk from there.
734          *
735          * No solution at the moment...
736          */
737         assert(!xtemps);
738
739         p = buildtree(ADDROFpNIL);  /* address of last arg */
740         p = optim(buildtree(PLUSpbcon(sz)));
741         q = block(NAMENILNILPTR+VOID00);
742         q = buildtree(CASTqp);
743         p = q->n_right;
744         nfree(q->n_left);
745         nfree(q);
746         p = buildtree(ASSIGNa->n_leftp);
747         tfree(f);
748         nfree(a);
749
750         return p;
751
gmcgarry
1.6
752 bad:
gmcgarry
1.7
753         uerror("bad argument to __builtin_stdarg_start");
754         return bcon(0);
gmcgarry
1.6
755 }
756
757 NODE *
758 mips_builtin_va_arg(NODE *fNODE *a)
759 {
gmcgarry
1.7
760         NODE *p, *q, *r;
761         int sztmpnr;
762
763         /* check num args and type */
764         if (a == NULL || a->n_op != CM || a->n_left->n_op == CM ||
765             !ISPTR(a->n_left->n_type) || a->n_right->n_op != TYPE)
766                 goto bad;
767
768         /* create a copy to a temp node */
769         p = tcopy(a->n_left);
770         q = tempnode(0p->n_typep->n_dfp->n_sue);
771         tmpnr = q->n_lval;
772         p = buildtree(ASSIGNqp);
773
774         r = a->n_right;
775         sz = tsize(r->n_typer->n_dfr->n_sue) / SZCHAR;
gmcgarry
1.9
776         if (sz < SZINT/SZCHAR) {
777                 werror("%s%s promoted to int when passed through ...",
778                         r->n_type & 1 ? "unsigned " : "",
779                         DEUNSIGN(r->n_type) == SHORT ? "short" : "char");
780                 sz = SZINT/SZCHAR;
781         }
gmcgarry
1.7
782         q = buildtree(PLUSEQa->n_leftbcon(sz));
783         q = buildtree(COMOPpq);
784
785         nfree(a->n_right);
786         nfree(a);
787         nfree(f); 
788
789         p = tempnode(tmpnrINCREF(r->n_type), r->n_dfr->n_sue);
790         p = buildtree(UMULpNIL);
791         p = buildtree(COMOPqp);
792
793         return p;
794
795 bad:
796         uerror("bad argument to __builtin_va_arg");
797         return bcon(0);
gmcgarry
1.6
798 }
799
800 NODE *
801 mips_builtin_va_end(NODE *fNODE *a)
802 {
gmcgarry
1.7
803         tfree(f);
804         tfree(a);
805         return bcon(0);
gmcgarry
1.6
806 }
807
808 NODE *
809 mips_builtin_va_copy(NODE *fNODE *a)
810 {
gmcgarry
1.7
811         if (a == NULL || a->n_op != CM || a->n_left->n_op == CM)
812                 goto bad;
813         tfree(f);
814         f = buildtree(ASSIGNa->n_lefta->n_right);
815         nfree(a);
gmcgarry
1.6
816         return f;
gmcgarry
1.7
817
818 bad:
819         uerror("bad argument to __buildtin_va_copy");
820         return bcon(0);
gmcgarry
1.6
821 }
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-10-30 16:14 +0100