Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20071226132613

Diff

Diff from 1.11 to:

Annotations

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

Annotated File View

ragge
1.11
1 /*      $Id: local.c,v 1.11 2007/12/26 13:26:14 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  * 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) {
gmcgarry
1.10
263                         case BOOL:
264                                 l->n_lval = l->n_lval != 0;
265                                 break;
ragge
1.1
266                         case CHAR:
267                                 l->n_lval = (char)val;
268                                 break;
269                         case UCHAR:
270                                 l->n_lval = val & 0377;
271                                 break;
272                         case SHORT:
273                                 l->n_lval = (short)val;
274                                 break;
275                         case USHORT:
276                                 l->n_lval = val & 0177777;
277                                 break;
278                         case ULONG:
279                         case UNSIGNED:
280                                 l->n_lval = val & 0xffffffff;
281                                 break;
282                         case LONG:
283                         case INT:
284                                 l->n_lval = (int)val;
285                                 break;
286                         case LONGLONG:
287                                 l->n_lval = (long long)val;
288                                 break;
289                         case ULONGLONG:
290                                 l->n_lval = val;
291                                 break;
292                         case VOID:
293                                 break;
294                         case LDOUBLE:
295                         case DOUBLE:
296                         case FLOAT:
297                                 l->n_op = FCON;
298                                 l->n_dcon = val;
299                                 break;
300                         default:
301                                 cerror("unknown type %d"m);
302                         }
303                         l->n_type = m;
304                         nfree(p);
305                         p = l;
306                 }
307                 break;
308
309         case MOD:
310         case DIV:
311                 if (o == DIV && p->n_type != CHAR && p->n_type != SHORT)
312                         break;
313                 if (o == MOD && p->n_type != CHAR && p->n_type != SHORT)
314                         break;
315                 /* make it an int division by inserting conversions */
316                 p->n_left = block(SCONVp->n_leftNILINT0MKSUE(INT));
317                 p->n_right = block(SCONVp->n_rightNILINT0MKSUE(INT));
318                 p = block(SCONVpNILp->n_type0MKSUE(p->n_type));
319                 p->n_left->n_type = INT;
320                 break;
321
322         case PMCONV:
323         case PVCONV:
324                 ifp->n_right->n_op != ICON ) cerror"bad conversion"0);
325                 nfree(p);
gmcgarry
1.8
326                 p = buildtree(o==PMCONV?MUL:DIVp->n_leftp->n_right);
327                 break;
ragge
1.1
328
329         case FORCE:
330                 /* put return value in return reg */
331                 p->n_op = ASSIGN;
332                 p->n_right = p->n_left;
gmcgarry
1.2
333                 p->n_left = block(REGNILNILp->n_type0MKSUE(INT));
gmcgarry
1.3
334                 p->n_left->n_rval = RETREG(p->n_type);
ragge
1.1
335                 break;
336         }
gmcgarry
1.3
337
gmcgarry
1.8
338 #ifdef PCC_DEBUG
339         if (xdebug) {
340                 printf("clocal out: %p\n"p);
341                 fwalk(peprint0);
342         }
343 #endif
ragge
1.1
344
345         return(p);
346 }
347
348 void
349 myp2tree(NODE *p)
350 {
ragge
1.11
351         int o = p->n_opi;
352
353         if (o != FCON
354                 return;
355
356         /* Write float constants to memory */
357         /* Should be volontary per architecture */
358  
359         setloc1(RDATA);
360         defalign(p->n_type == FLOAT ? ALFLOAT : p->n_type == DOUBLE ?
361             ALDOUBLE : ALLDOUBLE );
362         deflab1(i = getlab()); 
363         ninval(0btdims[p->n_type].suesizep);
364         p->n_op = NAME;
365         p->n_lval = 0;  
366         p->n_sp = tmpalloc(sizeof(struct symtab_hdr));
367         p->n_sp->sclass = ILABEL;
368         p->n_sp->soffset = i;
369         p->n_sp->sflags = 0;
370
ragge
1.1
371 }
372
373 /*ARGSUSED*/
374 int
375 andable(NODE *p)
376 {
377         return(1);  /* all names can have & taken on them */
378 }
379
380 /*
381  * at the end of the arguments of a ftn, set the automatic offset
382  */
383 void
384 cendarg()
385 {
386         autooff = AUTOINIT;
387 }
388
389 /*
390  * is an automatic variable of type t OK for a register variable
391  */
392 int
393 cisreg(TWORD t)
394 {
395         if (t == INT || t == UNSIGNED || t == LONG || t == ULONG)
396                 return(1);
397         return 0/* XXX - fix reg assignment in pftn.c */
398 }
399
400 /*
401  * return a node, for structure references, which is suitable for
402  * being added to a pointer of type t, in order to be off bits offset
403  * into a structure
404  * t, d, and s are the type, dimension offset, and sizeoffset
405  * Be careful about only handling first-level pointers, the following
406  * indirections must be fullword.
407  */
408 NODE *
409 offcon(OFFSZ offTWORD tunion dimfun *dstruct suedef *sue)
410 {
gmcgarry
1.3
411         NODE *p;
ragge
1.1
412
413         if (xdebug)
414                 printf("offcon: OFFSZ %lld type %x dim %p siz %d\n",
415                     offtdsue->suesize);
416
gmcgarry
1.3
417         p = bcon(off/SZCHAR);
418         return p;
ragge
1.1
419 }
420
421 /*
422  * Allocate off bits on the stack.  p is a tree that when evaluated
423  * is the multiply count for off, t is a NAME node where to write
424  * the allocated address.
425  */
426 void
427 spalloc(NODE *tNODE *pOFFSZ off)
428 {
429         NODE *sp;
gmcgarry
1.10
430         int nbytes = off / SZCHAR;
ragge
1.1
431
gmcgarry
1.10
432         p = buildtree(MULpbcon(nbytes));
433         p = buildtree(PLUSpbcon(7));
434         p = buildtree(ANDpbcon(~7));
435
436         /* subtract the size from sp */
437         sp = block(REGNILNILp->n_type00);
438         sp->n_lval = 0;
439         sp->n_rval = SP;
440         ecomp(buildtree(MINUSEQspp));
ragge
1.1
441
442         /* save the address of sp */
443         sp = block(REGNILNILPTR+INTt->n_dft->n_sue);
gmcgarry
1.6
444         sp->n_rval = SP;
ragge
1.1
445         t->n_type = sp->n_type;
446         ecomp(buildtree(ASSIGNtsp)); /* Emit! */
447 }
448
449 /*
450  * print out a constant node
451  * mat be associated with a label
452  */
453 void
gmcgarry
1.2
454 ninval(CONSZ offint fszNODE *p)
ragge
1.1
455 {
gmcgarry
1.10
456         union { float fdouble dint i[2]; } u;
gmcgarry
1.3
457         struct symtab *q;
458         TWORD t;
459 #ifndef USE_GAS
gmcgarry
1.5
460         int ij;
gmcgarry
1.3
461 #endif
ragge
1.1
462
gmcgarry
1.3
463         t = p->n_type;
464         if (t > BTMASK)
465                 t = INT/* pointer */
466
467         if (p->n_op != ICON && p->n_op != FCON)
468                 cerror("ninval: init node not constant");
469
470         if (p->n_op == ICON && p->n_sp != NULL && DEUNSIGN(t) != INT)
471                 uerror("element not constant");
472
473         switch (t) {
474         case LONGLONG:
475         case ULONGLONG:
476 #ifdef USE_GAS
gmcgarry
1.10
477                 printf("\t.dword %lld\n", (long long)p->n_lval);
gmcgarry
1.3
478 #else
gmcgarry
1.5
479                 i = p->n_lval >> 32;
480                 j = p->n_lval & 0xffffffff;
gmcgarry
1.3
481                 p->n_type = INT;
gmcgarry
1.5
482                 if (bigendian) {
483                         p->n_lval = j;
484                         ninval(off32p);
485                         p->n_lval = i;
486                         ninval(off+3232p);
487                 } else {
488                         p->n_lval = i;
489                         ninval(off32p);
490                         p->n_lval = j;
491                         ninval(off+3232p);
492                 }
gmcgarry
1.3
493 #endif
494                 break;
495         case BOOL:
496                 if (p->n_lval > 1)
497                         p->n_lval = p->n_lval != 0;
498                 /* FALLTHROUGH */
499         case INT:
500         case UNSIGNED:
gmcgarry
1.10
501                 printf("\t.word " CONFMT, (CONSZ)p->n_lval);
gmcgarry
1.3
502                 if ((q = p->n_sp) != NULL) {
503                         if ((q->sclass == STATIC && q->slevel > 0) ||
504                             q->sclass == ILABEL) {
505                                 printf("+" LABFMTq->soffset);
506                         } else
507                                 printf("+%s"exname(q->sname));
508                 }
509                 printf("\n");
510                 break;
511         case SHORT:
512         case USHORT:
gmcgarry
1.10
513                 printf("\t.half %d\n", (int)p->n_lval & 0xffff);
gmcgarry
1.3
514                 break;
515         case CHAR:
516         case UCHAR:
517                 printf("\t.byte %d\n", (int)p->n_lval & 0xff);
518                 break;
519         case LDOUBLE:
520         case DOUBLE:
521                 u.d = (double)p->n_dcon;
gmcgarry
1.9
522                 if (bigendian) {
gmcgarry
1.10
523                         printf("\t.word\t%d\n"u.i[0]);
524                         printf("\t.word\t%d\n"u.i[1]);
gmcgarry
1.9
525                 } else {
gmcgarry
1.10
526                         printf("\t.word\t%d\n"u.i[1]);
527                         printf("\t.word\t%d\n"u.i[0]);
gmcgarry
1.9
528                 }
gmcgarry
1.3
529                 break;
530         case FLOAT:
531                 u.f = (float)p->n_dcon;
532                 printf("\t.word\t0x%x\n"u.i[0]);
533                 break;
534         default:
535                 cerror("ninval");
536         }
ragge
1.1
537 }
538
539 /* make a name look like an external name in the local machine */
540 char *
541 exname(char *p)
542 {
543         if (p == NULL)
544                 return "";
545         return p;
546 }
547
548 /*
549  * map types which are not defined on the local machine
550  */
551 TWORD
552 ctype(TWORD type)
553 {
554         switch (BTYPE(type)) {
555         case LONG:
556                 MODTYPE(type,INT);
557                 break;
558
559         case ULONG:
560                 MODTYPE(type,UNSIGNED);
561
562         }
563         return (type);
564 }
565
566 /* curid is a variable which is defined but
567  * is not initialized (and not a function );
568  * This routine returns the storage class for an uninitialized declaration
569  */
570 int
571 noinit()
572 {
573         return(EXTERN);
574 }
575
576 void
577 calldec(NODE *pNODE *q
578 {
579 }
580
581 void
582 extdec(struct symtab *q)
583 {
584 }
585
586 /* make a common declaration for id, if reasonable */
587 void
588 commdec(struct symtab *q)
589 {
590         int off;
591
592         off = tsize(q->stypeq->sdfq->ssue);
593         off = (off+(SZCHAR-1))/SZCHAR;
594
595 #ifdef GCC_COMPAT
gmcgarry
1.9
596         printf("        .comm %s,%d\n"gcc_findname(q), off);
ragge
1.1
597 #else
gmcgarry
1.9
598         printf("        .comm %s,%d\n"exname(q->sname), off);
ragge
1.1
599 #endif
600 }
601
602 /* make a local common declaration for id, if reasonable */
603 void
604 lcommdec(struct symtab *q)
605 {
606         int off;
607
608         off = tsize(q->stypeq->sdfq->ssue);
609         off = (off+(SZCHAR-1))/SZCHAR;
610         if (q->slevel == 0)
611 #ifdef GCC_COMPAT
gmcgarry
1.9
612                 printf("\t.lcomm %s,%d\n"gcc_findname(q), off);
ragge
1.1
613 #else
gmcgarry
1.9
614                 printf("\t.lcomm %s,%d\n"exname(q->sname), off);
ragge
1.1
615 #endif
616         else
gmcgarry
1.9
617                 printf("\t.lcomm " LABFMT ",%d\n"q->soffsetoff);
ragge
1.1
618 }
619
620 /*
621  * print a (non-prog) label.
622  */
623 void
624 deflab1(int label)
625 {
626         printf(LABFMT ":\n"label);
627 }
628
gmcgarry
1.9
629 /* ro-text, rw-data, ro-data, ro-strings */
630 static char *loctbl[] = { "text""data""rdata""rdata" };
ragge
1.1
631
632 void
633 setloc1(int locc)
634 {
gmcgarry
1.10
635         if (locc == lastloc && locc != STRNG)
ragge
1.1
636                 return;
gmcgarry
1.10
637         if (locc == DATA && lastloc == STRNG)
638                 return;
639
640         if (locc != lastloc) {
641                 lastloc = locc;
642                 printf("\t.%s\n"loctbl[locc]);
643         }
644
645         if (locc == STRNG)
646                 printf("\t.align 2\n");
ragge
1.1
647 }
gmcgarry
1.2
648
649 /*
650  * Initialize a bitfield.
651  */
652 void
653 infld(CONSZ offint fszCONSZ val)
654 {
655         if (idebug)
656                 printf("infld off %lld, fsz %d, val %lld inbits %d\n",
657                     offfszvalinbits);
658         val &= (1 << fsz)-1;
659         while (fsz + inbits >= SZCHAR) {
660                 inval |= (val << inbits);
661                 printf("\t.byte %d\n"inval & 255);
662                 fsz -= (SZCHAR - inbits);
663                 val >>= (SZCHAR - inbits);
664                 inval = inbits = 0;
665         }
666         if (fsz) {
667                 inval |= (val << inbits);
668                 inbits += fsz;
669         }
670 }
671
672 /*
673  * set fsz bits in sequence to zero.
674  */
675 void
676 zbits(OFFSZ offint fsz)
677 {
678         int m;
679
680         if (idebug)
681                 printf("zbits off %lld, fsz %d inbits %d\n"offfszinbits);
682         if ((m = (inbits % SZCHAR))) {
683                 m = SZCHAR - m;
684                 if (fsz < m) {
685                         inbits += fsz;
686                         return;
687                 } else {
688                         fsz -= m;
689                         printf("\t.byte %d\n"inval);
690                         inval = inbits = 0;
691                 }
692         }
693         if (fsz >= SZCHAR) {
694                 printf("\t.zero %d\n"fsz/SZCHAR);
695                 fsz -= (fsz/SZCHAR) * SZCHAR;
696         }
697         if (fsz) {
698                 inval = 0;
699                 inbits = fsz;
700         }
701 }
gmcgarry
1.6
702
703 /*
704  * va_start(ap, last) implementation.
705  *
706  * f is the NAME node for this builtin function.
707  * a is the argument list containing:
708  *         CM
709  *      ap   last
gmcgarry
1.7
710  *
711  * It turns out that this is easy on MIPS.  Just write the
712  * argument registers to the stack in va_arg_start() and
713  * use the traditional method of walking the stackframe.
gmcgarry
1.6
714  */
715 NODE *
716 mips_builtin_stdarg_start(NODE *fNODE *a)
717 {
gmcgarry
1.7
718         NODE *p, *q;
gmcgarry
1.6
719         int sz = 1;
gmcgarry
1.7
720         int i;
gmcgarry
1.6
721
722         /* check num args and type */
723         if (a == NULL || a->n_op != CM || a->n_left->n_op == CM ||
724             !ISPTR(a->n_left->n_type))
725                 goto bad;
726
gmcgarry
1.7
727         /*
728          * look at the offset of the last org to calculate the
729          * number of remain registers that need to be written
730          * to the stack.
731          */
732         if (xtemps) {
733                 for (i = 0i < nargregsi++) {
734                         q = block(REGNILNILPTR+INT0MKSUE(INT));
735                         q->n_rval = A0 + i;
736                         p = block(REGNILNILPTR+INT0MKSUE(INT));
737                         p->n_rval = SP;
738                         p = block(PLUSpbcon(ARGINIT+i), PTR+INT0MKSUE(INT));
739                         p = buildtree(UMULpNIL);
740                         p = buildtree(ASSIGNpq);
741                         ecomp(p);
742                 }
743         }
744
gmcgarry
1.6
745         /* must first deal with argument size; use int size */
746         p = a->n_right;
gmcgarry
1.7
747         if (p->n_type < INT) {
748                 /* round up to word */
gmcgarry
1.6
749                 sz = SZINT / tsize(p->n_typep->n_dfp->n_sue);
gmcgarry
1.7
750         }
751
752         /*
753          * Once again, if xtemps, the register is written to a
754          * temp.  We cannot take the address of the temp and
755          * walk from there.
756          *
757          * No solution at the moment...
758          */
759         assert(!xtemps);
760
761         p = buildtree(ADDROFpNIL);  /* address of last arg */
762         p = optim(buildtree(PLUSpbcon(sz)));
763         q = block(NAMENILNILPTR+VOID00);
764         q = buildtree(CASTqp);
765         p = q->n_right;
766         nfree(q->n_left);
767         nfree(q);
768         p = buildtree(ASSIGNa->n_leftp);
769         tfree(f);
770         nfree(a);
771
772         return p;
773
gmcgarry
1.6
774 bad:
gmcgarry
1.7
775         uerror("bad argument to __builtin_stdarg_start");
776         return bcon(0);
gmcgarry
1.6
777 }
778
779 NODE *
780 mips_builtin_va_arg(NODE *fNODE *a)
781 {
gmcgarry
1.7
782         NODE *p, *q, *r;
783         int sztmpnr;
784
785         /* check num args and type */
786         if (a == NULL || a->n_op != CM || a->n_left->n_op == CM ||
787             !ISPTR(a->n_left->n_type) || a->n_right->n_op != TYPE)
788                 goto bad;
789
790         /* create a copy to a temp node */
791         p = tcopy(a->n_left);
792         q = tempnode(0p->n_typep->n_dfp->n_sue);
793         tmpnr = q->n_lval;
794         p = buildtree(ASSIGNqp);
795
796         r = a->n_right;
797         sz = tsize(r->n_typer->n_dfr->n_sue) / SZCHAR;
gmcgarry
1.9
798         if (sz < SZINT/SZCHAR) {
799                 werror("%s%s promoted to int when passed through ...",
800                         r->n_type & 1 ? "unsigned " : "",
801                         DEUNSIGN(r->n_type) == SHORT ? "short" : "char");
802                 sz = SZINT/SZCHAR;
803         }
gmcgarry
1.7
804         q = buildtree(PLUSEQa->n_leftbcon(sz));
805         q = buildtree(COMOPpq);
806
807         nfree(a->n_right);
808         nfree(a);
809         nfree(f); 
810
811         p = tempnode(tmpnrINCREF(r->n_type), r->n_dfr->n_sue);
812         p = buildtree(UMULpNIL);
813         p = buildtree(COMOPqp);
814
815         return p;
816
817 bad:
818         uerror("bad argument to __builtin_va_arg");
819         return bcon(0);
gmcgarry
1.6
820 }
821
822 NODE *
823 mips_builtin_va_end(NODE *fNODE *a)
824 {
gmcgarry
1.7
825         tfree(f);
826         tfree(a);
827         return bcon(0);
gmcgarry
1.6
828 }
829
830 NODE *
831 mips_builtin_va_copy(NODE *fNODE *a)
832 {
gmcgarry
1.7
833         if (a == NULL || a->n_op != CM || a->n_left->n_op == CM)
834                 goto bad;
835         tfree(f);
836         f = buildtree(ASSIGNa->n_lefta->n_right);
837         nfree(a);
gmcgarry
1.6
838         return f;
gmcgarry
1.7
839
840 bad:
841         uerror("bad argument to __buildtin_va_copy");
842         return bcon(0);
gmcgarry
1.6
843 }
FishEye: Open Source License registered to PCC.
Atlassian FishEye, CVS analysis. (Version:1.6.3 Build:build-336 2008-11-04) - Administration - Page generated 2014-12-21 02:57 +0100