Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20110604152202

Diff

Diff from 1.28 to:

Annotations

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

Annotated File View

ragge
1.28
1 /*      $Id: local.c,v 1.28 2011/06/04 15:22:03 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  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 /*
28  * MIPS port by Jan Enoksson (janeno-1@student.ltu.se) and
29  * Simon Olsson (simols-1@student.ltu.se) 2005.
30  */
31
gmcgarry
1.7
32 #include <assert.h>
gmcgarry
1.3
33 #include "pass1.h"
ragge
1.1
34
gmcgarry
1.15
35 #define IALLOC(sz) (isinlining ? permalloc(sz) : tmpalloc(sz))
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;
ragge
1.22
77                 r = tempnode(0p->n_typep->n_dfp->n_ap);
ragge
1.12
78                 tmpnr = regno(r);
ragge
1.22
79                 r = block(ASSIGNrpp->n_typep->n_dfp->n_ap);
gmcgarry
1.9
80
ragge
1.22
81                 p = tempnode(tmpnrr->n_typer->n_dfr->n_ap);
gmcgarry
1.8
82                 if (isptrvoid) {
ragge
1.25
83                         p = block(PCONVpNILPTR+VOIDp->n_df0);
gmcgarry
1.8
84                 }
gmcgarry
1.9
85                 p = buildtree(COMOPrp);
gmcgarry
1.8
86                 break;
87
ragge
1.1
88         case NAME:
89                 if ((q = p->n_sp) == NULL)
90                         return p/* Nothing to care about */
91
92                 switch (q->sclass) {
93
94                 case PARAM:
95                 case AUTO:
96                         /* fake up a structure reference */
97                         r = block(REGNILNILPTR+STRTY00);
98                         r->n_lval = 0;
gmcgarry
1.3
99                         r->n_rval = FP;
ragge
1.1
100                         p = stref(block(STREFrp000));
101                         break;
102
103                 case STATIC:
104                         if (q->slevel == 0)
105                                 break;
106                         p->n_lval = 0;
107                         p->n_sp = q;
108                         break;
109
110                 case REGISTER:
111                         p->n_op = REG;
112                         p->n_lval = 0;
113                         p->n_rval = q->soffset;
114                         break;
115
gmcgarry
1.7
116                 }
ragge
1.1
117                 break;
118
119         case FUNARG:
120                 /* Args smaller than int are given as int */
121                 if (p->n_type != CHAR && p->n_type != UCHAR && 
122                     p->n_type != SHORT && p->n_type != USHORT)
123                         break;
ragge
1.25
124                 p->n_left = block(SCONVp->n_leftNILINT00);
ragge
1.1
125                 p->n_type = INT;
ragge
1.25
126                 p->n_ap = 0;
ragge
1.1
127                 p->n_rval = SZINT;
128                 break;
129
130         case CBRANCH:
131                 l = p->n_left;
132
133                 /*
ragge
1.16
134                  * Remove unnecessary conversion ops.
ragge
1.1
135                  */
136                 if (clogop(l->n_op) && l->n_left->n_op == SCONV) {
137                         if (coptype(l->n_op) != BITYPE)
138                                 break;
139                         if (l->n_right->n_op == ICON) {
140                                 r = l->n_left->n_left;
141                                 if (r->n_type >= FLOAT && r->n_type <= LDOUBLE)
142                                         break;
143                                 /* Type must be correct */
gmcgarry
1.6
144                                 ty = r->n_type;
ragge
1.1
145                                 nfree(l->n_left);
146                                 l->n_left = r;
gmcgarry
1.6
147                                 l->n_type = ty;
148                                 l->n_right->n_type = ty;
ragge
1.1
149                         }
150 #if 0
151                           else if (l->n_right->n_op == SCONV &&
152                             l->n_left->n_type == l->n_right->n_type) {
153                                 r = l->n_left->n_left;
154                                 nfree(l->n_left);
155                                 l->n_left = r;
156                                 r = l->n_right->n_left;
157                                 nfree(l->n_right);
158                                 l->n_right = r;
159                         }
160 #endif
161                 }
162                 break;
163
164         case PCONV:
gmcgarry
1.7
165                 /* Remove redundant PCONV's. Be careful */
ragge
1.1
166                 l = p->n_left;
gmcgarry
1.7
167                 if (l->n_op == ICON) {
168                         l->n_lval = (unsigned)l->n_lval;
169                         goto delp;
170                 }
gmcgarry
1.8
171                 if (l->n_type < INT || DEUNSIGN(l->n_type) == LONGLONG) {
gmcgarry
1.7
172                         /* float etc? */
ragge
1.25
173                         p->n_left = block(SCONVlNILUNSIGNED00);
gmcgarry
1.7
174                         break;
175                 }
176                 /* if left is SCONV, cannot remove */
177                 if (l->n_op == SCONV)
178                         break;
179
180                 /* avoid ADDROF TEMP */
181                 if (l->n_op == ADDROF && l->n_left->n_op == TEMP)
ragge
1.1
182                         break;
gmcgarry
1.7
183
184                 /* if conversion to another pointer type, just remove */
185                 if (p->n_type > BTMASK && l->n_type > BTMASK)
186                         goto delp;
187                 break;
188
189         delp:   l->n_type = p->n_type;
ragge
1.1
190                 l->n_qual = p->n_qual;
191                 l->n_df = p->n_df;
ragge
1.22
192                 l->n_ap = p->n_ap;
ragge
1.1
193                 nfree(p);
194                 p = l;
195                 break;
196
197         case SCONV:
198                 l = p->n_left;
199
200                 if (p->n_type == l->n_type) {
201                         nfree(p);
gmcgarry
1.8
202                         p = l;
203                         break;
ragge
1.1
204                 }
205
206                 if ((p->n_type & TMASK) == 0 && (l->n_type & TMASK) == 0 &&
ragge
1.22
207                     btattr[p->n_type].atypsz == btattr[l->n_type].atypsz) {
ragge
1.1
208                         if (p->n_type != FLOAT && p->n_type != DOUBLE &&
209                             l->n_type != FLOAT && l->n_type != DOUBLE &&
210                             l->n_type != LDOUBLE && p->n_type != LDOUBLE) {
gmcgarry
1.8
211                                 if (l->n_op == NAME || l->n_op == UMUL ||
212                                     l->n_op == TEMP) {
ragge
1.1
213                                         l->n_type = p->n_type;
214                                         nfree(p);
gmcgarry
1.8
215                                         p = l;
216                                         break;
ragge
1.1
217                                 }
218                         }
219                 }
220
gmcgarry
1.8
221                 if (DEUNSIGN(p->n_type) == INT && DEUNSIGN(l->n_type) == INT &&
222                     coptype(l->n_op) == BITYPE) {
223                         l->n_type = p->n_type;
224                         nfree(p);
225                         p = l;
226                 }
227
228                 if (DEUNSIGN(p->n_type) == SHORT &&
229                     DEUNSIGN(l->n_type) == SHORT) {
ragge
1.1
230                         nfree(p);
gmcgarry
1.8
231                         p = l;
ragge
1.1
232                 }
233
gmcgarry
1.9
234                 /* convert float/double to int before to (u)char/(u)short */
gmcgarry
1.8
235                 if ((DEUNSIGN(p->n_type) == CHAR ||
236                     DEUNSIGN(p->n_type) == SHORT) &&
237                     (l->n_type == FLOAT || l->n_type == DOUBLE ||
238                     l->n_type == LDOUBLE)) {
ragge
1.22
239                         p = block(SCONVpNILp->n_typep->n_dfp->n_ap);
gmcgarry
1.8
240                         p->n_left->n_type = INT;
241                         break;
242                 }
243
gmcgarry
1.9
244                 /* convert (u)char/(u)short to int before float/double */
245                 if  ((p->n_type == FLOAT || p->n_type == DOUBLE ||
246                     p->n_type == LDOUBLE) && (DEUNSIGN(l->n_type) == CHAR ||
247                     DEUNSIGN(l->n_type) == SHORT)) {
ragge
1.22
248                         p = block(SCONVpNILp->n_typep->n_dfp->n_ap);
gmcgarry
1.9
249                         p->n_left->n_type = INT;
250                         break;
251                 }
gmcgarry
1.8
252
ragge
1.1
253                 o = l->n_op;
254                 m = p->n_type;
255
256                 if (o == ICON) {
257                         CONSZ val = l->n_lval;
258
259                         if (!ISPTR(m)) /* Pointers don't need to be conv'd */
260                             switch (m) {
gmcgarry
1.10
261                         case BOOL:
262                                 l->n_lval = l->n_lval != 0;
263                                 break;
ragge
1.1
264                         case CHAR:
265                                 l->n_lval = (char)val;
266                                 break;
267                         case UCHAR:
268                                 l->n_lval = val & 0377;
269                                 break;
270                         case SHORT:
271                                 l->n_lval = (short)val;
272                                 break;
273                         case USHORT:
274                                 l->n_lval = val & 0177777;
275                                 break;
276                         case ULONG:
277                         case UNSIGNED:
278                                 l->n_lval = val & 0xffffffff;
279                                 break;
280                         case LONG:
281                         case INT:
282                                 l->n_lval = (int)val;
283                                 break;
284                         case LONGLONG:
285                                 l->n_lval = (long long)val;
286                                 break;
287                         case ULONGLONG:
288                                 l->n_lval = val;
289                                 break;
290                         case VOID:
291                                 break;
292                         case LDOUBLE:
293                         case DOUBLE:
294                         case FLOAT:
295                                 l->n_op = FCON;
296                                 l->n_dcon = val;
297                                 break;
298                         default:
299                                 cerror("unknown type %d"m);
300                         }
301                         l->n_type = m;
302                         nfree(p);
303                         p = l;
gmcgarry
1.15
304                 } else if (o == FCON) {
305                         l->n_lval = l->n_dcon;
306                         l->n_sp = NULL;
307                         l->n_op = ICON;
308                         l->n_type = m;
ragge
1.25
309                         l->n_ap = 0;
gmcgarry
1.15
310                         nfree(p);
311                         p = clocal(l);
ragge
1.1
312                 }
313                 break;
314
315         case MOD:
316         case DIV:
317                 if (o == DIV && p->n_type != CHAR && p->n_type != SHORT)
318                         break;
319                 if (o == MOD && p->n_type != CHAR && p->n_type != SHORT)
320                         break;
321                 /* make it an int division by inserting conversions */
ragge
1.25
322                 p->n_left = block(SCONVp->n_leftNILINT00);
323                 p->n_right = block(SCONVp->n_rightNILINT00);
324                 p = block(SCONVpNILp->n_type00);
ragge
1.1
325                 p->n_left->n_type = INT;
326                 break;
327
328         case FORCE:
329                 /* put return value in return reg */
330                 p->n_op = ASSIGN;
331                 p->n_right = p->n_left;
ragge
1.25
332                 p->n_left = block(REGNILNILp->n_type00);
gmcgarry
1.3
333                 p->n_left->n_rval = RETREG(p->n_type);
ragge
1.1
334                 break;
335         }
gmcgarry
1.3
336
gmcgarry
1.8
337 #ifdef PCC_DEBUG
338         if (xdebug) {
339                 printf("clocal out: %p\n"p);
340                 fwalk(peprint0);
341         }
342 #endif
ragge
1.1
343
344         return(p);
345 }
346
347 void
348 myp2tree(NODE *p)
349 {
ragge
1.14
350         struct symtab *sp;
ragge
1.11
351
ragge
1.14
352         if (p->n_op != FCON
ragge
1.11
353                 return;
354
355         /* Write float constants to memory */
356  
gmcgarry
1.15
357         sp = IALLOC(sizeof(struct symtab));
ragge
1.14
358         sp->sclass = STATIC;
ragge
1.25
359         sp->sap = 0;
ragge
1.14
360         sp->slevel = 1/* fake numeric label */
361         sp->soffset = getlab();
362         sp->sflags = 0;
363         sp->stype = p->n_type;
364         sp->squal = (CON >> TSHIFT);
365
366         defloc(sp);
ragge
1.22
367         ninval(0sp->sap->atypszp);
ragge
1.14
368
ragge
1.11
369         p->n_op = NAME;
ragge
1.14
370         p->n_lval = 0;
371         p->n_sp = sp;
ragge
1.11
372
ragge
1.1
373 }
374
375 /*ARGSUSED*/
376 int
377 andable(NODE *p)
378 {
379         return(1);  /* all names can have & taken on them */
380 }
381
382 /*
383  * is an automatic variable of type t OK for a register variable
384  */
385 int
386 cisreg(TWORD t)
387 {
388         if (t == INT || t == UNSIGNED || t == LONG || t == ULONG)
389                 return(1);
390         return 0/* XXX - fix reg assignment in pftn.c */
391 }
392
393 /*
394  * Allocate off bits on the stack.  p is a tree that when evaluated
395  * is the multiply count for off, t is a NAME node where to write
396  * the allocated address.
397  */
398 void
399 spalloc(NODE *tNODE *pOFFSZ off)
400 {
401         NODE *sp;
gmcgarry
1.10
402         int nbytes = off / SZCHAR;
ragge
1.1
403
gmcgarry
1.10
404         p = buildtree(MULpbcon(nbytes));
405         p = buildtree(PLUSpbcon(7));
406         p = buildtree(ANDpbcon(~7));
407
408         /* subtract the size from sp */
409         sp = block(REGNILNILp->n_type00);
410         sp->n_lval = 0;
411         sp->n_rval = SP;
412         ecomp(buildtree(MINUSEQspp));
ragge
1.1
413
414         /* save the address of sp */
ragge
1.22
415         sp = block(REGNILNILPTR+INTt->n_dft->n_ap);
gmcgarry
1.6
416         sp->n_rval = SP;
ragge
1.1
417         t->n_type = sp->n_type;
418         ecomp(buildtree(ASSIGNtsp)); /* Emit! */
419 }
420
421 /*
422  * print out a constant node
423  * mat be associated with a label
424  */
ragge
1.28
425 int
gmcgarry
1.2
426 ninval(CONSZ offint fszNODE *p)
ragge
1.1
427 {
gmcgarry
1.10
428         union { float fdouble dint i[2]; } u;
gmcgarry
1.3
429         struct symtab *q;
430         TWORD t;
431 #ifndef USE_GAS
gmcgarry
1.5
432         int ij;
gmcgarry
1.3
433 #endif
ragge
1.1
434
gmcgarry
1.3
435         t = p->n_type;
436         if (t > BTMASK)
ragge
1.28
437                 p->n_type = t = INT/* pointer */
gmcgarry
1.3
438
439         if (p->n_op == ICON && p->n_sp != NULL && DEUNSIGN(t) != INT)
440                 uerror("element not constant");
441
442         switch (t) {
443         case LONGLONG:
444         case ULONGLONG:
445 #ifdef USE_GAS
gmcgarry
1.10
446                 printf("\t.dword %lld\n", (long long)p->n_lval);
gmcgarry
1.3
447 #else
gmcgarry
1.5
448                 i = p->n_lval >> 32;
449                 j = p->n_lval & 0xffffffff;
gmcgarry
1.3
450                 p->n_type = INT;
gmcgarry
1.5
451                 if (bigendian) {
452                         p->n_lval = j;
453                         ninval(off32p);
454                         p->n_lval = i;
455                         ninval(off+3232p);
456                 } else {
457                         p->n_lval = i;
458                         ninval(off32p);
459                         p->n_lval = j;
460                         ninval(off+3232p);
461                 }
gmcgarry
1.3
462 #endif
463                 break;
464         case INT:
465         case UNSIGNED:
gmcgarry
1.10
466                 printf("\t.word " CONFMT, (CONSZ)p->n_lval);
gmcgarry
1.3
467                 if ((q = p->n_sp) != NULL) {
ragge
1.18
468                         if ((q->sclass == STATIC && q->slevel > 0)) {
gmcgarry
1.3
469                                 printf("+" LABFMTq->soffset);
470                         } else
ragge
1.19
471                                 printf("+%s",
472                                     q->soname ? q->soname : exname(q->sname));
gmcgarry
1.3
473                 }
474                 printf("\n");
475                 break;
476         case SHORT:
477         case USHORT:
ragge
1.28
478                 astypnames[SHORT] = astypnames[USHORT] = "\t.half";
479                 return 0;
gmcgarry
1.3
480         case LDOUBLE:
481         case DOUBLE:
482                 u.d = (double)p->n_dcon;
gmcgarry
1.9
483                 if (bigendian) {
gmcgarry
1.10
484                         printf("\t.word\t%d\n"u.i[0]);
485                         printf("\t.word\t%d\n"u.i[1]);
gmcgarry
1.9
486                 } else {
gmcgarry
1.10
487                         printf("\t.word\t%d\n"u.i[1]);
488                         printf("\t.word\t%d\n"u.i[0]);
gmcgarry
1.9
489                 }
gmcgarry
1.3
490                 break;
491         case FLOAT:
492                 u.f = (float)p->n_dcon;
493                 printf("\t.word\t0x%x\n"u.i[0]);
494                 break;
495         default:
ragge
1.28
496                 return 0;
gmcgarry
1.3
497         }
ragge
1.28
498         return 1;
ragge
1.1
499 }
500
501 /* make a name look like an external name in the local machine */
502 char *
503 exname(char *p)
504 {
505         if (p == NULL)
506                 return "";
507         return p;
508 }
509
510 /*
511  * map types which are not defined on the local machine
512  */
513 TWORD
514 ctype(TWORD type)
515 {
516         switch (BTYPE(type)) {
517         case LONG:
518                 MODTYPE(type,INT);
519                 break;
520
521         case ULONG:
522                 MODTYPE(type,UNSIGNED);
523
524         }
525         return (type);
526 }
527
528 /* curid is a variable which is defined but
529  * is not initialized (and not a function );
530  * This routine returns the storage class for an uninitialized declaration
531  */
532 int
ragge
1.22
533 noinit(void)
ragge
1.1
534 {
535         return(EXTERN);
536 }
537
538 void
539 calldec(NODE *pNODE *q
540 {
541 }
542
543 void
544 extdec(struct symtab *q)
545 {
546 }
547
ragge
1.14
548 /*
549  * Print out a string of characters.
550  * Assume that the assembler understands C-style escape
551  * sequences.
552  */
553 void
554 instring(struct symtab *sp)
555 {
556         char *s, *str;
557
558         defloc(sp);
559         str = sp->sname;
560
561         /* be kind to assemblers and avoid long strings */
562         printf("\t.ascii \"");
563         for (s = str; *s != 0; ) {
564                 if (*s++ == '\\') {
565                         (void)esccon(&s);
566                 }
567                 if (s - str > 60) {
568                         fwrite(str1s - strstdout);
569                         printf("\"\n\t.ascii \"");
570                         str = s;
571                 }
572         }
573         fwrite(str1s - strstdout);
574         printf("\\0\"\n");
575 }
576
577 /* make a common declaration for id, if reasonable */
578 void
579 defzero(struct symtab *sp)
580 {
581         int off;
582
ragge
1.22
583         off = tsize(sp->stypesp->sdfsp->sap);
ragge
1.14
584         off = (off+(SZCHAR-1))/SZCHAR;
585         printf("        .%scomm "sp->sclass == STATIC ? "l" : "");
586         if (sp->slevel == 0)
ragge
1.19
587                 printf("%s,0%o\n"sp->soname ? sp->soname : exname(sp->sname), off);
ragge
1.14
588         else
589                 printf(LABFMT ",0%o\n"sp->soffsetoff);
590 }
591
592
593 #ifdef notdef
ragge
1.1
594 /* make a common declaration for id, if reasonable */
595 void
596 commdec(struct symtab *q)
597 {
598         int off;
599
600         off = tsize(q->stypeq->sdfq->ssue);
601         off = (off+(SZCHAR-1))/SZCHAR;
602
ragge
1.13
603         printf("        .comm %s,%d\n"exname(q->soname), off);
ragge
1.1
604 }
605
606 /* make a local common declaration for id, if reasonable */
607 void
608 lcommdec(struct symtab *q)
609 {
610         int off;
611
612         off = tsize(q->stypeq->sdfq->ssue);
613         off = (off+(SZCHAR-1))/SZCHAR;
614         if (q->slevel == 0)
ragge
1.13
615                 printf("\t.lcomm %s,%d\n"exname(q->soname), off);
ragge
1.1
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.15
637         if (locc == RDATA && lastloc == STRNG)
gmcgarry
1.10
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 }
ragge
1.14
648 #endif
gmcgarry
1.2
649
650 /*
651  * Initialize a bitfield.
652  */
653 void
654 infld(CONSZ offint fszCONSZ val)
655 {
656         if (idebug)
657                 printf("infld off %lld, fsz %d, val %lld inbits %d\n",
658                     offfszvalinbits);
659         val &= (1 << fsz)-1;
660         while (fsz + inbits >= SZCHAR) {
661                 inval |= (val << inbits);
662                 printf("\t.byte %d\n"inval & 255);
663                 fsz -= (SZCHAR - inbits);
664                 val >>= (SZCHAR - inbits);
665                 inval = inbits = 0;
666         }
667         if (fsz) {
668                 inval |= (val << inbits);
669                 inbits += fsz;
670         }
671 }
672
673 /*
674  * set fsz bits in sequence to zero.
675  */
676 void
677 zbits(OFFSZ offint fsz)
678 {
679         int m;
680
681         if (idebug)
682                 printf("zbits off %lld, fsz %d inbits %d\n"offfszinbits);
683         if ((m = (inbits % SZCHAR))) {
684                 m = SZCHAR - m;
685                 if (fsz < m) {
686                         inbits += fsz;
687                         return;
688                 } else {
689                         fsz -= m;
690                         printf("\t.byte %d\n"inval);
691                         inval = inbits = 0;
692                 }
693         }
694         if (fsz >= SZCHAR) {
695                 printf("\t.zero %d\n"fsz/SZCHAR);
696                 fsz -= (fsz/SZCHAR) * SZCHAR;
697         }
698         if (fsz) {
699                 inval = 0;
700                 inbits = fsz;
701         }
702 }
gmcgarry
1.6
703
704 /*
705  * va_start(ap, last) implementation.
706  *
707  * f is the NAME node for this builtin function.
708  * a is the argument list containing:
709  *         CM
710  *      ap   last
gmcgarry
1.7
711  *
712  * It turns out that this is easy on MIPS.  Just write the
713  * argument registers to the stack in va_arg_start() and
714  * use the traditional method of walking the stackframe.
gmcgarry
1.6
715  */
716 NODE *
ragge
1.23
717 mips_builtin_stdarg_start(NODE *fNODE *aTWORD t)
gmcgarry
1.6
718 {
gmcgarry
1.7
719         NODE *p, *q;
gmcgarry
1.6
720         int sz = 1;
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
727         /* must first deal with argument size; use int size */
728         p = a->n_right;
gmcgarry
1.7
729         if (p->n_type < INT) {
730                 /* round up to word */
ragge
1.22
731                 sz = SZINT / tsize(p->n_typep->n_dfp->n_ap);
gmcgarry
1.7
732         }
733
734         p = buildtree(ADDROFpNIL);  /* address of last arg */
735         p = optim(buildtree(PLUSpbcon(sz)));
736         q = block(NAMENILNILPTR+VOID00);
737         q = buildtree(CASTqp);
738         p = q->n_right;
739         nfree(q->n_left);
740         nfree(q);
741         p = buildtree(ASSIGNa->n_leftp);
742         tfree(f);
743         nfree(a);
744
745         return p;
746
gmcgarry
1.6
747 bad:
gmcgarry
1.7
748         uerror("bad argument to __builtin_stdarg_start");
749         return bcon(0);
gmcgarry
1.6
750 }
751
752 NODE *
ragge
1.23
753 mips_builtin_va_arg(NODE *fNODE *aTWORD t)
gmcgarry
1.6
754 {
gmcgarry
1.7
755         NODE *p, *q, *r;
756         int sztmpnr;
757
758         /* check num args and type */
759         if (a == NULL || a->n_op != CM || a->n_left->n_op == CM ||
760             !ISPTR(a->n_left->n_type) || a->n_right->n_op != TYPE)
761                 goto bad;
762
gmcgarry
1.15
763         r = a->n_right;
gmcgarry
1.7
764
gmcgarry
1.15
765         /* get type size */
ragge
1.22
766         sz = tsize(r->n_typer->n_dfr->n_ap) / SZCHAR;
gmcgarry
1.9
767         if (sz < SZINT/SZCHAR) {
768                 werror("%s%s promoted to int when passed through ...",
769                         r->n_type & 1 ? "unsigned " : "",
770                         DEUNSIGN(r->n_type) == SHORT ? "short" : "char");
771                 sz = SZINT/SZCHAR;
772         }
gmcgarry
1.15
773
774         /* alignment */
775         p = tcopy(a->n_left);
776         if (sz > SZINT/SZCHAR && r->n_type != UNIONTY && r->n_type != STRTY) {
777                 p = buildtree(PLUSpbcon(7));
ragge
1.22
778                 p = block(ANDpbcon(-8), p->n_typep->n_dfp->n_ap);
gmcgarry
1.15
779         }
780
781         /* create a copy to a temp node */
ragge
1.22
782         q = tempnode(0p->n_typep->n_dfp->n_ap);
gmcgarry
1.15
783         tmpnr = regno(q);
784         p = buildtree(ASSIGNqp);
785
ragge
1.22
786         q = tempnode(tmpnrp->n_typep->n_df,p->n_ap);
gmcgarry
1.15
787         q = buildtree(PLUSqbcon(sz));
788         q = buildtree(ASSIGNa->n_leftq);
789
gmcgarry
1.7
790         q = buildtree(COMOPpq);
791
792         nfree(a->n_right);
793         nfree(a);
794         nfree(f); 
795
ragge
1.22
796         p = tempnode(tmpnrINCREF(r->n_type), r->n_dfr->n_ap);
gmcgarry
1.7
797         p = buildtree(UMULpNIL);
798         p = buildtree(COMOPqp);
799
800         return p;
801
802 bad:
803         uerror("bad argument to __builtin_va_arg");
804         return bcon(0);
gmcgarry
1.6
805 }
806
807 NODE *
ragge
1.23
808 mips_builtin_va_end(NODE *fNODE *aTWORD t)
gmcgarry
1.6
809 {
gmcgarry
1.7
810         tfree(f);
811         tfree(a);
812         return bcon(0);
gmcgarry
1.6
813 }
814
815 NODE *
ragge
1.23
816 mips_builtin_va_copy(NODE *fNODE *aTWORD t)
gmcgarry
1.6
817 {
gmcgarry
1.7
818         if (a == NULL || a->n_op != CM || a->n_left->n_op == CM)
819                 goto bad;
820         tfree(f);
821         f = buildtree(ASSIGNa->n_lefta->n_right);
822         nfree(a);
gmcgarry
1.6
823         return f;
gmcgarry
1.7
824
825 bad:
826         uerror("bad argument to __buildtin_va_copy");
827         return bcon(0);
gmcgarry
1.6
828 }
gmcgarry
1.15
829
gmcgarry
1.17
830 static int constructor;
831 static int destructor;
832
ragge
1.13
833 /*
834  * Give target the opportunity of handling pragmas.
835  */
836 int
ragge
1.24
837 mypragma(char *str)
ragge
1.13
838 {
ragge
1.24
839
840         if (strcmp(str"tls") == 0) { 
gmcgarry
1.17
841                 uerror("thread-local storage not supported for this target");
842                 return 1;
843         } 
ragge
1.24
844         if (strcmp(str"constructor") == 0 || strcmp(str"init") == 0) {
gmcgarry
1.17
845                 constructor = 1;
846                 return 1;
847         }
ragge
1.24
848         if (strcmp(str"destructor") == 0 || strcmp(str"fini") == 0) {
gmcgarry
1.17
849                 destructor = 1;
850                 return 1;
851         }
852
gmcgarry
1.15
853         return 0;
854 }
ragge
1.13
855
856 /*
857  * Called when a identifier has been declared, to give target last word.
858  */
859 void
860 fixdef(struct symtab *sp)
861 {
gmcgarry
1.17
862         if ((constructor || destructor) && (sp->sclass != PARAM)) {
863                 printf("\t.section .%ctors,\"aw\",@progbits\n",
864                     constructor ? 'c' : 'd');
865                 printf("\t.p2align 2\n");
866                 printf("\t.long %s\n"exname(sp->sname));
gmcgarry
1.21
867                 printf("\t.previous\n");
gmcgarry
1.17
868                 constructor = destructor = 0;
869         }
870 }
871
872 void
873 pass1_lastchance(struct interpass *ip)
874 {
ragge
1.13
875 }
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-22 18:47 +0100