Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20030103223222

Diff

Diff from 1.12 to:

Annotations

Annotate by Age | Author | Mixed | None
/fisheye/browse/pcc/pcc/cc/ccom/trees.c

Annotated File View

ragge
1.2
1 #if 0
ragge
1.1
2 static char *sccsid ="@(#)trees.c       4.37 (Berkeley) 6/18/90";
3 #endif
4
5 # include "pass1.h"
ragge
1.9
6 # include "pass2.h"     /* for NOPREF */
ragge
1.1
7
8 # include <setjmp.h>
9
10 int bdebug = 0;
11 int adebug = 0;
ragge
1.2
12 extern int ddebug;
ragge
1.1
13
ragge
1.2
14 void chkpun(NODE *p);
15 int opact(NODE *p);
16 int chkstr(intintTWORD);
17 int moditype(TWORD);
18
19 /* corrections when in violation of lint */
ragge
1.1
20
21 /*      some special actions, used in finding the type of nodes */
22 # define NCVT 01
23 # define PUN 02
24 # define TYPL 04
25 # define TYPR 010
26 # define TYMATCH 040
27 # define LVAL 0100
28 # define CVTO 0200
29 # define CVTL 0400
30 # define CVTR 01000
31 # define PTMATCH 02000
32 # define OTHER 04000
33 # define NCVTR 010000
34
35 #ifndef BUG1
ragge
1.2
36 static void
37 printact(NODE *tint acts)
ragge
1.1
38 {
39         static struct actions {
40                 int     a_bit;
41                 char    *a_name;
42         } actions[] = {
43                 { PUN,          "PUN" },
44                 { CVTL,         "CVTL" },
45                 { CVTR,         "CVTR" },
46                 { TYPL,         "TYPL" },
47                 { TYPR,         "TYPR" },
48                 { TYMATCH,      "TYMATCH" },
49                 { PTMATCH,      "PTMATCH" },
50                 { LVAL,         "LVAL" },
51                 { CVTO,         "CVTO" },
52                 { NCVT,         "NCVT" },
53                 { OTHER,        "OTHER" },
54                 { NCVTR,        "NCVTR" },
55                 { 0 }
56         };
57         register struct actions *p;
58         char *sep = " ";
59
60         printf("actions");
61         for (p = actionsp->a_namep++)
62                 if (p->a_bit & acts) {
63                         printf("%s%s"sepp->a_name);
64                         sep = "|";
65                 }
66         if (!bdebug) {
67                 printf(" for:\n");
68                 fwalk(teprint0);
69         } else
70                 putchar('\n');
71 }
72 #endif
73
74 /* node conventions:
75
76         NAME:   rval>0 is stab index for external
77                 rval<0 is -inlabel number
78                 lval is offset in bits
79         ICON:   lval has the value
80                 rval has the STAB index, or - label number,
81                         if a name whose address is in the constant
82                 rval = NONAME means no name
83         REG:    rval is reg. identification cookie
84
85         */
86
87 NODE *
ragge
1.2
88 buildtree(int oNODE *lNODE *r)
89 {
90         NODE *p, *q;
91         int actions;
92         int opty;
93         struct symtab *sp;
94         NODE *lr, *ll;
ragge
1.1
95         int i;
96
97 # ifndef BUG1
ragge
1.2
98         if (bdebug)
99                 printf("buildtree(%s, %p, %p)\n"opst[o], lr);
ragge
1.1
100 # endif
101         opty = optype(o);
102
103         /* check for constants */
104
105         ifopty == UTYPE && l->in.op == ICON ){
106
107                 switcho ){
108
109                 case NOT:
110                         ifhflag ) werror"constant argument to NOT" );
111                 case UNARY MINUS:
112                 case COMPL:
113                         ifconvallol ) ) return(l);
114                         break;
115
116                         }
117                 }
118
119         else ifopty == UTYPE && (l->in.op == FCON || l->in.op == DCON) ){
120
121                 switcho ){
122
123                 case NOT:
124                         ifhflag ) werror"constant argument to NOT" );
125                         ifl->in.op == FCON )
126                                 l->tn.lval = l->fpn.fval == 0.0;
127                         else
128                                 l->tn.lval = l->dpn.dval == 0.0;
129                         l->tn.rval = NONAME;
130                         l->in.op = ICON;
131                         l->fn.csiz = l->in.type = INT;
132                         l->fn.cdim = 0;
133                         return(l);
134                 case UNARY MINUS:
135                         ifl->in.op == FCON )
136                                 l->fpn.fval = -l->fpn.fval;
137                         else
138                                 l->dpn.dval = -l->dpn.dval;
139                         return(l);
140                         }
141                 }
142
143         else ifo==QUEST && l->in.op==ICON ) {
144                 l->in.op = FREE;
145                 r->in.op = FREE;
146                 ifl->tn.lval ){
147                         tfreer->in.right );
148                         returnr->in.left );
149                         }
150                 else {
151                         tfreer->in.left );
152                         returnr->in.right );
153                         }
154                 }
155
156         else if( (o==ANDAND || o==OROR) && (l->in.op==ICON||r->in.op==ICON) ) goto ccwarn;
157
158         else ifopty == BITYPE && l->in.op == ICON && r->in.op == ICON ){
159
160                 switcho ){
161
162                 case ULT:
163                 case UGT:
164                 case ULE:
165                 case UGE:
166                 case LT:
167                 case GT:
168                 case LE:
169                 case GE:
170                 case EQ:
171                 case NE:
172                         ifl->in.type == ENUMTY && r->in.type == ENUMTY ){
173                                 p = blockolrINT0INT );
174                                 chkpunp );
175                                 p->in.op = FREE;
176                                 }
177
178                 case ANDAND:
179                 case OROR:
180                 case CBRANCH:
181
182                 ccwarn:
183                         ifhflag ) werror"constant in conditional context" );
184
185                 case PLUS:
186                 case MINUS:
187                 case MUL:
188                 case DIV:
189                 case MOD:
190                 case AND:
191                 case OR:
192                 case ER:
193                 case LS:
194                 case RS:
195                         ifconvallor ) ) {
196                                 r->in.op = FREE;
197                                 return(l);
198                                 }
199                         break;
200                         }
201                 }
202         else if (opty == BITYPE &&
203                 (l->in.op == FCON || l->in.op == DCON || l->in.op == ICON) &&
204                 (r->in.op == FCON || r->in.op == DCON || r->in.op == ICON)) {
205                         if (o == PLUS || o == MINUS || o == MUL || o == DIV) {
206                                 extern int fpe_count;
207                                 extern jmp_buf gotfpe;
208
209                                 fpe_count = 0;
210                                 if (setjmp(gotfpe))
211                                         goto treatfpe;
212                                 if (l->in.op == ICON)
213                                         l->dpn.dval = l->tn.lval;
214                                 else if (l->in.op == FCON)
215                                         l->dpn.dval = l->fpn.fval;
216                                 if (r->in.op == ICON)
217                                         r->dpn.dval = r->tn.lval;
218                                 else if (r->in.op == FCON)
219                                         r->dpn.dval = r->fpn.fval;
220                                 switch (o) {
221
222                                 case PLUS:
223                                         l->dpn.dval += r->dpn.dval;
224                                         break;
225
226                                 case MINUS:
227                                         l->dpn.dval -= r->dpn.dval;
228                                         break;
229
230                                 case MUL:
231                                         l->dpn.dval *= r->dpn.dval;
232                                         break;
233
234                                 case DIV:
235                                         if (r->dpn.dval == 0)
236                                                 uerror("division by 0.");
237                                         else
238                                                 l->dpn.dval /= r->dpn.dval;
239                                         break;
240                                         }
241                         treatfpe:
242                                 if (fpe_count > 0) {
243                                         uerror("floating point exception in constant expression");
244                                         l->dpn.dval = 1.0/* Fairly harmless */
245                                         }
246                                 fpe_count = -1;
247                                 l->in.op = DCON;
248                                 l->in.type = l->fn.csiz = DOUBLE;
249                                 r->in.op = FREE;
250                                 return (l);
251                         }
252                 }
253
254         /* it's real; we must make a new node */
255
256         p = blockolrINT0INT );
257
258         actions = opact(p);
259 #ifndef BUG1
260         if (adebug)
261                 printact(pactions);
262 #endif
263
264         ifactions&LVAL ){ /* check left descendent */
265                 ifnotlval(p->in.left) ) {
266                         uerror"illegal lvalue operand of assignment operator" );
267                         }
268                 }
269
270         ifactions & NCVTR ){
271                 p->in.left = pconvertp->in.left );
272                 }
273         else if( !(actions & NCVT ) ){
274                 switchopty ){
275
276                 case BITYPE:
277                         p->in.right = pconvertp->in.right );
278                 case UTYPE:
279                         p->in.left = pconvertp->in.left );
280
281                         }
282                 }
283
ragge
1.7
284         if ((actions&PUN) && (o!=CAST||cflag))
ragge
1.1
285                 chkpun(p);
286
287         ifactions & (TYPL|TYPR) ){
288
289                 q = (actions&TYPL) ? p->in.left : p->in.right;
290
291                 p->in.type = q->in.type;
292                 p->fn.cdim = q->fn.cdim;
293                 p->fn.csiz = q->fn.csiz;
294                 }
295
296         ifactions & CVTL ) p = convertpCVTL );
297         ifactions & CVTR ) p = convertpCVTR );
298         ifactions & TYMATCH ) p = tymatch(p);
299         ifactions & PTMATCH ) p = ptmatch(p);
300
301         ifactions & OTHER ){
302                 l = p->in.left;
303                 r = p->in.right;
304
305                 switch(o){
306
307                 case NAME:
308                         sp = &stab[idname];
309                         ifsp->stype == UNDEF ){
310                                 uerror"%s undefined"sp->sname );
311                                 /* make p look reasonable */
312                                 p->in.type = p->fn.csiz = INT;
313                                 p->fn.cdim = 0;
314                                 p->tn.rval = idname;
315                                 p->tn.lval = 0;
316                                 defidpSNULL );
317                                 break;
318                                 }
319                         p->in.type = sp->stype;
320                         p->fn.cdim = sp->dimoff;
321                         p->fn.csiz = sp->sizoff;
322                         p->tn.lval = 0;
323                         p->tn.rval = idname;
324                         /* special case: MOETY is really an ICON... */
325                         ifp->in.type == MOETY ){
326                                 p->tn.rval = NONAME;
327                                 p->tn.lval = sp->offset;
328                                 p->fn.cdim = 0;
329                                 p->in.type = ENUMTY;
330                                 p->in.op = ICON;
331                                 }
332                         break;
333
334                 case ICON:
335                         p->in.type = INT;
336                         p->fn.cdim = 0;
337                         p->fn.csiz = INT;
338                         break;
339
340                 case STRING:
341                         p->in.op = NAME;
342                         p->in.type = CHAR+ARY;
343                         p->tn.lval = 0;
344                         p->tn.rval = NOLAB;
345                         p->fn.cdim = curdim;
346                         p->fn.csiz = CHAR;
347                         break;
348
349                 case FCON:
350                         p->tn.lval = 0;
351                         p->tn.rval = 0;
352                         p->in.type = FLOAT;
353                         p->fn.cdim = 0;
354                         p->fn.csiz = FLOAT;
355                         break;
356
357                 case DCON:
358                         p->tn.lval = 0;
359                         p->tn.rval = 0;
360                         p->in.type = DOUBLE;
361                         p->fn.cdim = 0;
362                         p->fn.csiz = DOUBLE;
363                         break;
364
365                 case STREF:
366                         /* p->x turned into *(p+offset) */
367                         /* rhs must be a name; check correctness */
368
369                         i = r->tn.rval;
370                         ifi<0 || ((sp= &stab[i])->sclass != MOS && sp->sclass != MOU && !(sp->sclass&FIELD)) ){
371                                 uerror"member of structure or union required" );
372                                 }else
373                         /* if this name is non-unique, find right one */
374                         ifstab[i].sflags & SNONUNIQ &&
375                                 (l->in.type==PTR+STRTY || l->in.type == PTR+UNIONTY) &&
376                                 (l->fn.csiz +1) >= 0 ){
377                                 /* nonunique name && structure defined */
378                                 char * memnam, * tabnam;
379                                 int j;
380                                 int memi;
381                                 j=dimtab[l->fn.csiz+1];
382                                 for( ; (memi=dimtab[j]) >= 0; ++j ){
383                                         tabnam = stab[memi].sname;
384                                         memnam = stab[i].sname;
385 # ifndef BUG1
386                                         ifddebug>1 ){
387                                                 printf("member %s==%s?\n",
388                                                         memnamtabnam);
389                                                 }
390 # endif
391                                         ifstab[memi].sflags & SNONUNIQ ){
392                                                 if (memnam != tabnam)
393                                                         goto next;
394                                                 r->tn.rval = i = memi;
395                                                 break;
396                                                 }
397                                         nextcontinue;
398                                         }
399                                 ifmemi < 0 )
400                                         uerror("illegal member use: %s",
401                                                 stab[i].sname);
402                                 }
403                         else {
ragge
1.2
404                                 int j;
ragge
1.1
405                                 ifl->in.type != PTR+STRTY && l->in.type != PTR+UNIONTY ){
406                                         ifstab[i].sflags & SNONUNIQ ){
407                                                 uerror"nonunique name demands struct/union or struct/union pointer" );
408                                                 }
409                                         else werror"struct/union or struct/union pointer required" );
410                                         }
411                                 else if( (j=l->fn.csiz+1)<0 ) cerror"undefined structure or union" );
412                                 else if( !chkstridimtab[j], DECREF(l->in.type) ) ){
413                                         werror"illegal member use: %s"stab[i].sname );
414                                         }
415                                 }
416
417                         p = strefp );
418                         break;
419
420                 case UNARY MUL:
421                         ifl->in.op == UNARY AND ){
422                                 p->in.op = l->in.op = FREE;
423                                 p = l->in.left;
424                                 }
425                         if( !ISPTR(l->in.type))uerror("illegal indirection");
426                         p->in.type = DECREF(l->in.type);
427                         p->fn.cdim = l->fn.cdim;
428                         p->fn.csiz = l->fn.csiz;
429                         break;
430
431                 case UNARY AND:
432                         switchl->in.op ){
433
434                         case UNARY MUL:
435                                 p->in.op = l->in.op = FREE;
436                                 p = l->in.left;
437                         case NAME:
438                                 p->in.type = INCREFl->in.type );
439                                 p->fn.cdim = l->fn.cdim;
440                                 p->fn.csiz = l->fn.csiz;
441                                 break;
442
443                         case COMOP:
444                                 lr = buildtreeUNARY ANDl->in.rightNIL );
445                                 p->in.op = l->in.op = FREE;
446                                 p = buildtreeCOMOPl->in.leftlr );
447                                 break;
448
449                         case QUEST:
450                                 lr = buildtreeUNARY ANDl->in.right->in.rightNIL );
451                                 ll = buildtreeUNARY ANDl->in.right->in.leftNIL );
452                                 p->in.op = l->in.op = l->in.right->in.op = FREE;
453                                 p = buildtreeQUESTl->in.leftbuildtreeCOLONlllr ) );
454                                 break;
455
456 # ifdef ADDROREG
457                         case OREG:
458                                 /* OREG was built in clocal()
459                                  * for an auto or formal parameter
460                                  * now its address is being taken
461                                  * local code must unwind it
462                                  * back to PLUS/MINUS REG ICON
463                                  * according to local conventions
464                                  */
465                                 p->in.op = FREE;
466                                 p = addroregl );
467                                 break;
468
469 # endif
470                         default:
471                                 uerror"unacceptable operand of &" );
472                                 break;
473                                 }
474                         break;
475
476                 case LS:
477                 case RS:
478                         ifl->in.type == CHAR || l->in.type == SHORT )
479                                 p->in.type = INT;
480                         else ifl->in.type == UCHAR || l->in.type == USHORT )
481                                 p->in.type = UNSIGNED;
482                         else
483                                 p->in.type = l->in.type;
484                 case ASG LS:
485                 case ASG RS:
486                         ifr->in.type != INT )
487                                 p->in.right = r = makety(rINT0INT );
488                         break;
489
490                 case RETURN:
491                 case ASSIGN:
492                 case CAST:
493                         /* structure assignment */
494                         /* take the addresses of the two sides; then make an
ragge
1.2
495                          * operator using STASG and
496                          * the addresses of left and right */
ragge
1.1
497
498                         {
ragge
1.2
499                                 TWORD t;
500                                 int ds;
ragge
1.1
501
502                                 ifl->fn.csiz != r->fn.csiz ) uerror"assignment of different structures" );
503
504                                 r = buildtreeUNARY ANDrNIL );
505                                 t = r->in.type;
506                                 d = r->fn.cdim;
507                                 s = r->fn.csiz;
508
509                                 l = blockSTASGlrtds );
510
511                                 ifo == RETURN ){
512                                         p->in.op = FREE;
513                                         p = l;
514                                         break;
515                                         }
516
517                                 p->in.op = UNARY MUL;
518                                 p->in.left = l;
519                                 p->in.right = NIL;
520                                 break;
521                                 }
522                 case COLON:
523                         /* structure colon */
524
525                         ifl->fn.csiz != r->fn.csiz ) uerror"type clash in conditional" );
526                         break;
527
528                 case CALL:
529                         p->in.right = r = fixargsp->in.right );
530                 case UNARY CALL:
531                         if( !ISPTR(l->in.type)) uerror("illegal function");
532                         p->in.type = DECREF(l->in.type);
533                         if( !ISFTN(p->in.type)) uerror("illegal function");
534                         p->in.type = DECREFp->in.type );
535                         p->fn.cdim = l->fn.cdim;
536                         p->fn.csiz = l->fn.csiz;
537                         ifl->in.op == UNARY AND && l->in.left->in.op == NAME &&
538                                 l->in.left->tn.rval >= 0 && l->in.left->tn.rval != NONAME &&
539                                 ( (i=stab[l->in.left->tn.rval].sclass) == FORTRAN || i==UFORTRAN ) ){
540                                 p->in.op += (FORTCALL-CALL);
541                                 }
542                         ifp->in.type == STRTY || p->in.type == UNIONTY ){
543                                 /* function returning structure */
544                                 /*  make function really return ptr to str., with * */
545
546                                 p->in.op += STCALL-CALL;
547                                 p->in.type = INCREFp->in.type );
548                                 p = buildtreeUNARY MULpNIL );
549
550                                 }
551                         break;
552
553                 default:
554                         cerror"other code %d"o );
555                         }
556
557                 }
558
ragge
1.11
559         /*
560          * Allow (void)0 casts.
561          * XXX - anything on the right side must be possible to cast.
562          * XXX - remove void types further on.
563          */
564         if (p->in.op == CAST && p->in.type == UNDEF &&
565             p->in.right->in.op == ICON)
566                 p->in.right->in.type = UNDEF;
567
ragge
1.1
568         ifactions & CVTO ) p = oconvert(p);
569         p = clocal(p);
570
571 # ifndef BUG1
572         ifbdebug ) fwalkpeprint0 );
573 # endif
574
575         return(p);
576
577         }
578
579 int fpe_count = -1;
580 jmp_buf gotfpe;
581
ragge
1.2
582 void fpe(int);
ragge
1.1
583 void
ragge
1.2
584 fpe(int a)
585 {
ragge
1.1
586         if (fpe_count < 0)
587                 cerror("floating point exception");
588         ++fpe_count;
589         longjmp(gotfpe1);
ragge
1.2
590 }
ragge
1.1
591
592 /*
593  * Rewrite arguments in a function call.
594  * Structure arguments are massaged, single
595  * precision floating point constants are
596  * cast to double (to eliminate convert code).
597  */
598 NODE *
599 fixargsp ) register NODE *p;  {
600         int o = p->in.op;
601
602         ifo == CM ){
603                 p->in.left = fixargsp->in.left );
604                 p->in.right = fixargsp->in.right );
605                 returnp );
606                 }
607
608         ifp->in.type == STRTY || p->in.type == UNIONTY ){
609                 p = blockSTARGpNILp->in.typep->fn.cdimp->fn.csiz );
610                 p->in.left = buildtreeUNARY ANDp->in.leftNIL );
611                 p = clocal(p);
612                 }
613         else ifo == FCON )
614                 p = makety(pDOUBLE00);
615         returnp );
ragge
1.2
616 }
ragge
1.1
617
ragge
1.2
618 /*
619  * is the MOS or MOU at stab[i] OK for strict reference by a ptr
620  * i has been checked to contain a MOS or MOU
621  * j is the index in dimtab of the members...
622  */
623 int
624 chkstr(int iint jTWORD type)
625 {
ragge
1.1
626         int kkk;
627
628         extern int ddebug;
629
630 # ifndef BUG1
631         ifddebug > 1 ) printf"chkstr( %s(%d), %d )\n"stab[i].snameij );
632 # endif
633         if( (k = j) < 0 ) uerror"undefined structure or union" );
634         else {
635                 for( ; (kk = dimtab[k] ) >= 0; ++k ){
636                         ifkk >= SYMTSZ ){
637                                 cerror"gummy structure" );
638                                 return(1);
639                                 }
640                         ifkk == i ) return1 );
641                         switchstab[kk].stype ){
642
643                         case STRTY:
644                         case UNIONTY:
645                                 iftype == STRTY ) continue;  /* no recursive looking for strs */
646                                 ifhflag && chkstridimtab[stab[kk].sizoff+1], stab[kk].stype ) ){
647                                         ifstab[kk].sname[0] == '$' ) return(0);  /* $FAKE */
648                                         werror(
649                                         "illegal member use: perhaps %s.%s?",
650                                         stab[kk].snamestab[i].sname );
651                                         return(1);
652                                         }
653                                 }
654                         }
655                 }
656         return0 );
ragge
1.2
657 }
ragge
1.1
658
ragge
1.2
659 /*
660  * apply the op o to the lval part of p; if binary, rhs is val
661  * works only on integer constants
662  */
663 int
664 conval(NODE *pint oNODE *q)
665 {
ragge
1.1
666         int iu;
667         CONSZ val;
668         TWORD utype;
669
670         val = q->tn.lval;
671         u = ISUNSIGNED(p->in.type) || ISUNSIGNED(q->in.type);
672         ifu && (o==LE||o==LT||o==GE||o==GT)) o += (UGE-GE);
673
674         ifp->tn.rval != NONAME && q->tn.rval != NONAME ) return(0);
675         ifq->tn.rval != NONAME && o!=PLUS ) return(0);
676         ifp->tn.rval != NONAME && o!=PLUS && o!=MINUS ) return(0);
677
678         /* usual type conversions -- handle casts of constants */
679 #define ISLONG(t)       ((t) == LONG || (t) == ULONG)
ragge
1.6
680 #define ISLONGLONG(t)   ((t) == LONGLONG || (t) == ULONGLONG)
ragge
1.1
681         if (ISLONG(p->in.type) || ISLONG(q->in.type))
682                 utype = u ? ULONG : LONG;
ragge
1.6
683         else if (ISLONGLONG(p->in.type) || ISLONGLONG(q->in.type))
684                 utype = u ? ULONGLONG : LONGLONG;
ragge
1.1
685         else
686                 utype = u ? UNSIGNED : INT;
687         if( !ISPTR(p->in.type) && p->in.type != utype )
688                 p = makety(putype0, (int)utype);
689         ifq->in.type != utype )
690                 q = makety(qutype0, (int)utype);
691
692         switcho ){
693
694         case PLUS:
695                 p->tn.lval += val;
696                 ifp->tn.rval == NONAME ){
697                         p->tn.rval = q->tn.rval;
698                         p->in.type = q->in.type;
699                         }
700                 break;
701         case MINUS:
702                 p->tn.lval -= val;
703                 break;
704         case MUL:
705                 p->tn.lval *= val;
706                 break;
707         case DIV:
708                 ifval == 0 ) uerror"division by 0" );
709                 else if ( u ) p->tn.lval = (unsignedp->tn.lval / val;
710                 else p->tn.lval /= val;
711                 break;
712         case MOD:
713                 ifval == 0 ) uerror"division by 0" );
714                 else if ( u ) p->tn.lval = (unsignedp->tn.lval % val;
715                 else p->tn.lval %= val;
716                 break;
717         case AND:
718                 p->tn.lval &= val;
719                 break;
720         case OR:
721                 p->tn.lval |= val;
722                 break;
723         case ER:
724                 p->tn.lval ^= val;
725                 break;
726         case LS:
727                 i = val;
728                 p->tn.lval = p->tn.lval << i;
729                 break;
730         case RS:
731                 i = val;
732                 if ( u ) p->tn.lval = (unsignedp->tn.lval >> i;
733                 else p->tn.lval = p->tn.lval >> i;
734                 break;
735
736         case UNARY MINUS:
737                 p->tn.lval = - p->tn.lval;
738                 break;
739         case COMPL:
740                 p->tn.lval = ~p->tn.lval;
741                 break;
742         case NOT:
743                 p->tn.lval = !p->tn.lval;
744                 break;
745         case LT:
746                 p->tn.lval = p->tn.lval < val;
747                 break;
748         case LE:
749                 p->tn.lval = p->tn.lval <= val;
750                 break;
751         case GT:
752                 p->tn.lval = p->tn.lval > val;
753                 break;
754         case GE:
755                 p->tn.lval = p->tn.lval >= val;
756                 break;
757         case ULT:
758                 p->tn.lval = p->tn.lval < (unsignedval;
759                 break;
760         case ULE:
761                 p->tn.lval = p->tn.lval <= (unsignedval;
762                 break;
763         case UGT:
764                 p->tn.lval = p->tn.lval > (unsignedval;
765                 break;
766         case UGE:
767                 p->tn.lval = p->tn.lval >= (unsignedval;
768                 break;
769         case EQ:
770                 p->tn.lval = p->tn.lval == val;
771                 break;
772         case NE:
773                 p->tn.lval = p->tn.lval != val;
774                 break;
775         default:
776                 return(0);
777                 }
778         return(1);
779         }
780
ragge
1.7
781 /*
782  * Checks p for the existance of a pun.  This is called when the op of p
783  * is ASSIGN, RETURN, CAST, COLON, or relational.
784  * One case is when enumerations are used: this applies only to lint.
785  * In the other case, one operand is a pointer, the other integer type
786  * we check that this integer is in fact a constant zero...
787  * in the case of ASSIGN, any assignment of pointer to integer is illegal
788  * this falls out, because the LHS is never 0.
789  */
ragge
1.2
790 void
ragge
1.7
791 chkpun(NODE *p)
792 {
ragge
1.2
793         NODE *q;
794         int t1t2d1d2ref1ref2;
ragge
1.1
795
796         t1 = p->in.left->in.type;
797         t2 = p->in.right->in.type;
798
ragge
1.7
799         /* check for enumerations */
800         if (t1==ENUMTY || t2==ENUMTY) {
ragge
1.1
801                 /* rob pike says this is obnoxious...
802                 if( logop( p->in.op ) && p->in.op != EQ && p->in.op != NE )
803                         werror( "comparison of enums" ); */
ragge
1.7
804                 if (t1==ENUMTY && t2==ENUMTY) {
805                         if (p->in.left->fn.csiz!=p->in.right->fn.csiz)
806                                 werror("enumeration type clash, "
807                                     "operator %s"opst[p->in.op]);
ragge
1.1
808                         return;
809                 }
ragge
1.7
810                 if (t1 == ENUMTY)
811                         t1 = INT;
812                 if (t2 == ENUMTY)
813                         t2 = INT;
814         }
ragge
1.1
815
816         ref1 = ISPTR(t1) || ISARY(t1);
817         ref2 = ISPTR(t2) || ISARY(t2);
818
ragge
1.7
819         if (ref1 ^ ref2) {
820                 if (ref1)
821                         q = p->in.right;
822                 else
823                         q = p->in.left;
824                 if (q->in.op != ICON || q->tn.lval != 0)
825                         werror("illegal combination of pointer "
826                             "and integer, op %s"opst[p->in.op]);
827         } else if (ref1) {
828                 if (t1 == t2) {
829                         if (p->in.left->fn.csiz != p->in.right->fn.csiz) {
ragge
1.5
830                                 werror("illegal structure pointer combination");
ragge
1.1
831                                 return;
ragge
1.5
832                         }
ragge
1.1
833                         d1 = p->in.left->fn.cdim;
834                         d2 = p->in.right->fn.cdim;
ragge
1.7
835                         for (;;) {
836                                 if (ISARY(t1)) {
837                                         if (dimtab[d1] != dimtab[d2]) {
838                                                 werror("illegal array "
839                                                     "size combination");
ragge
1.1
840                                                 return;
ragge
1.5
841                                         }
ragge
1.1
842                                         ++d1;
843                                         ++d2;
ragge
1.5
844                                 } else
ragge
1.7
845                                         if (!ISPTR(t1))
ragge
1.5
846                                                 break;
ragge
1.1
847                                 t1 = DECREF(t1);
848                         }
ragge
1.7
849                 } else if (t1 != INCREF(UNDEF) && t2 != INCREF(UNDEF))
850                         werror("illegal pointer combination");
ragge
1.1
851         }
ragge
1.5
852 }
ragge
1.1
853
854 NODE *
ragge
1.10
855 stref(NODE *p)
856 {
ragge
1.1
857         TWORD t;
858         int dsdscalign;
859         OFFSZ off;
860         register struct symtab *q;
861
862         /* make p->x */
863         /* this is also used to reference automatic variables */
864
865         q = &stab[p->in.right->tn.rval];
866         p->in.right->in.op = FREE;
867         p->in.op = FREE;
ragge
1.10
868         p = pconvert(p->in.left);
ragge
1.1
869
870         /* make p look like ptr to x */
871
ragge
1.10
872         if (!ISPTR(p->in.type))
ragge
1.1
873                 p->in.type = PTR+UNIONTY;
874
ragge
1.10
875         t = INCREF(q->stype);
ragge
1.1
876         d = q->dimoff;
877         s = q->sizoff;
878
ragge
1.10
879         p = makety(ptds);
ragge
1.1
880
881         /* compute the offset to be added */
882
883         off = q->offset;
884         dsc = q->sclass;
885
ragge
1.10
886         if (dsc & FIELD) {  /* normalize offset */
ragge
1.1
887                 align = ALINT;
888                 s = INT;
889                 off = (off/align)*align;
ragge
1.10
890         }
891         if (off != 0)
892                 p = clocal(block(PLUSpoffcon(offtds), tds));
ragge
1.1
893
ragge
1.10
894         p = buildtree(UNARY MULpNIL);
ragge
1.1
895
896         /* if field, build field info */
897
ragge
1.10
898         if (dsc & FIELD) {
899                 p = block(FLDpNILq->stype0q->sizoff);
900                 p->tn.rval = PKFIELD(dsc&FLDSIZq->offset%align);
901         }
ragge
1.1
902
ragge
1.10
903         return(clocal(p));
904 }
ragge
1.1
905
ragge
1.2
906 int
ragge
1.1
907 notlval(pregister NODE *p; {
908
909         /* return 0 if p an lvalue, 1 otherwise */
910
911         again:
912
913         switchp->in.op ){
914
915         case FLD:
916                 p = p->in.left;
917                 goto again;
918
919         case UNARY MUL:
920                 /* fix the &(a=b) bug, given that a and b are structures */
921                 ifp->in.left->in.op == STASG ) return1 );
922                 /* and the f().a bug, given that f returns a structure */
923                 ifp->in.left->in.op == UNARY STCALL ||
924                     p->in.left->in.op == STCALL ) return1 );
925         case NAME:
926         case OREG:
927                 ifISARY(p->in.type) || ISFTN(p->in.type) ) return(1);
928         case REG:
929                 return(0);
930
931         default:
932                 return(1);
933
934                 }
935
936         }
937
938 NODE *
939 bconi ){ /* make a constant node with value i */
940         register NODE *p;
941
942         p = blockICONNILNILINT0INT );
943         p->tn.lval = i;
944         p->tn.rval = NONAME;
945         returnclocal(p) );
946         }
947
948 NODE *
ragge
1.10
949 bpsize(NODE *p)
950 {
951         return(offcon(psize(p), p->in.typep->fn.cdimp->fn.csiz));
952 }
ragge
1.1
953
ragge
1.10
954 /*
955  * p is a node of type pointer; psize returns the
956  * size of the thing pointed to
957  */
ragge
1.1
958 OFFSZ
ragge
1.10
959 psize(NODE *p)
960 {
961
962         if (!ISPTR(p->in.type)) {
963                 uerror("pointer required");
964                 return(SZINT);
965         }
ragge
1.1
966         /* note: no pointers to fields */
ragge
1.10
967         return(tsize(DECREF(p->in.type), p->fn.cdimp->fn.csiz));
968 }
ragge
1.1
969
ragge
1.10
970 /*
971  * convert an operand of p
972  * f is either CVTL or CVTR
973  * operand has type int, and is converted by the size of the other side
974  */
ragge
1.1
975 NODE *
ragge
1.10
976 convert(NODE *pint f)
977 {
978         NODE *q, *r, *s;
ragge
1.1
979
ragge
1.10
980         q = (f == CVTL) ? p->in.left : p->in.right;
ragge
1.1
981
ragge
1.10
982         s = bpsize(f == CVTL ? p->in.right : p->in.left);
983         r = block(PMCONVqsINT0INT);
ragge
1.1
984         r = clocal(r);
ragge
1.10
985         if (f == CVTL)
ragge
1.1
986                 p->in.left = r;
987         else
988                 p->in.right = r;
989         return(p);
ragge
1.10
990 }
ragge
1.1
991
992 #ifndef econvert
ragge
1.2
993 void
ragge
1.1
994 econvertp ) register NODE *p; {
995
996         /* change enums to ints, or appropriate types */
997
998         register TWORD ty;
999
1000         if( (ty=BTYPE(p->in.type)) == ENUMTY || ty == MOETY ) {
ragge
1.6
1001                 if (dimtab[p->fn.csiz] == SZCHAR)
1002                         ty = CHAR;
1003                 else if (dimtab[p->fn.csiz] == SZINT)
1004                         ty = INT;
1005                 else if (dimtab[p->fn.csiz] == SZSHORT)
1006                         ty = SHORT;
1007                 else if (dimtab[p->fn.csiz] == SZLONGLONG)
1008                         ty = LONGLONG;
1009                 else
1010                         ty = LONG;
1011                 ty = ctype(ty);
ragge
1.1
1012                 p->fn.csiz = ty;
1013                 MODTYPE(p->in.type,ty);
ragge
1.6
1014                 if (p->in.op == ICON && ty != LONG && ty != LONGLONG)
1015                         p->in.type = p->fn.csiz = INT;
ragge