Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20030202145022

Diff

Diff from 1.16 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>
ragge
1.16
9 # include <stdarg.h>
10 # include <string.h>
ragge
1.1
11
12 int bdebug = 0;
13 int adebug = 0;
ragge
1.2
14 extern int ddebug;
ragge
1.1
15
ragge
1.2
16 void chkpun(NODE *p);
17 int opact(NODE *p);
18 int chkstr(intintTWORD);
19 int moditype(TWORD);
ragge
1.16
20 static void to_pass2(NODE *p);
ragge
1.2
21
22 /* corrections when in violation of lint */
ragge
1.1
23
24 /*      some special actions, used in finding the type of nodes */
25 # define NCVT 01
26 # define PUN 02
27 # define TYPL 04
28 # define TYPR 010
29 # define TYMATCH 040
30 # define LVAL 0100
31 # define CVTO 0200
32 # define CVTL 0400
33 # define CVTR 01000
34 # define PTMATCH 02000
35 # define OTHER 04000
36 # define NCVTR 010000
37
38 #ifndef BUG1
ragge
1.2
39 static void
40 printact(NODE *tint acts)
ragge
1.1
41 {
42         static struct actions {
43                 int     a_bit;
44                 char    *a_name;
45         } actions[] = {
46                 { PUN,          "PUN" },
47                 { CVTL,         "CVTL" },
48                 { CVTR,         "CVTR" },
49                 { TYPL,         "TYPL" },
50                 { TYPR,         "TYPR" },
51                 { TYMATCH,      "TYMATCH" },
52                 { PTMATCH,      "PTMATCH" },
53                 { LVAL,         "LVAL" },
54                 { CVTO,         "CVTO" },
55                 { NCVT,         "NCVT" },
56                 { OTHER,        "OTHER" },
57                 { NCVTR,        "NCVTR" },
58                 { 0 }
59         };
60         register struct actions *p;
61         char *sep = " ";
62
63         printf("actions");
64         for (p = actionsp->a_namep++)
65                 if (p->a_bit & acts) {
66                         printf("%s%s"sepp->a_name);
67                         sep = "|";
68                 }
69         if (!bdebug) {
70                 printf(" for:\n");
71                 fwalk(teprint0);
72         } else
73                 putchar('\n');
74 }
75 #endif
76
77 /* node conventions:
78
79         NAME:   rval>0 is stab index for external
80                 rval<0 is -inlabel number
81                 lval is offset in bits
82         ICON:   lval has the value
83                 rval has the STAB index, or - label number,
84                         if a name whose address is in the constant
85                 rval = NONAME means no name
86         REG:    rval is reg. identification cookie
87
88         */
89
90 NODE *
ragge
1.2
91 buildtree(int oNODE *lNODE *r)
92 {
93         NODE *p, *q;
94         int actions;
95         int opty;
96         struct symtab *sp;
97         NODE *lr, *ll;
ragge
1.1
98         int i;
99
100 # ifndef BUG1
ragge
1.2
101         if (bdebug)
102                 printf("buildtree(%s, %p, %p)\n"opst[o], lr);
ragge
1.1
103 # endif
104         opty = optype(o);
105
106         /* check for constants */
107
108         ifopty == UTYPE && l->in.op == ICON ){
109
110                 switcho ){
111
112                 case NOT:
113                 case UNARY MINUS:
114                 case COMPL:
115                         ifconvallol ) ) return(l);
116                         break;
117
118                         }
119                 }
120
121         else ifopty == UTYPE && (l->in.op == FCON || l->in.op == DCON) ){
122
123                 switcho ){
124
125                 case NOT:
126                         ifl->in.op == FCON )
127                                 l->tn.lval = l->fpn.fval == 0.0;
128                         else
129                                 l->tn.lval = l->dpn.dval == 0.0;
130                         l->tn.rval = NONAME;
131                         l->in.op = ICON;
132                         l->fn.csiz = l->in.type = INT;
133                         l->fn.cdim = 0;
134                         return(l);
135                 case UNARY MINUS:
136                         ifl->in.op == FCON )
137                                 l->fpn.fval = -l->fpn.fval;
138                         else
139                                 l->dpn.dval = -l->dpn.dval;
140                         return(l);
141                         }
142                 }
143
144         else ifo==QUEST && l->in.op==ICON ) {
145                 l->in.op = FREE;
146                 r->in.op = FREE;
147                 ifl->tn.lval ){
148                         tfreer->in.right );
149                         returnr->in.left );
150                         }
151                 else {
152                         tfreer->in.left );
153                         returnr->in.right );
154                         }
155                 }
156
157         else if( (o==ANDAND || o==OROR) && (l->in.op==ICON||r->in.op==ICON) ) goto ccwarn;
158
159         else ifopty == BITYPE && l->in.op == ICON && r->in.op == ICON ){
160
161                 switcho ){
162
163                 case ULT:
164                 case UGT:
165                 case ULE:
166                 case UGE:
167                 case LT:
168                 case GT:
169                 case LE:
170                 case GE:
171                 case EQ:
172                 case NE:
173                         ifl->in.type == ENUMTY && r->in.type == ENUMTY ){
174                                 p = blockolrINT0INT );
175                                 chkpunp );
176                                 p->in.op = FREE;
177                                 }
178
179                 case ANDAND:
180                 case OROR:
181                 case CBRANCH:
182
183                 ccwarn:
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
ragge
1.15
631         if (ddebug > 1)
632                 printf("chkstr( %s(%d), %d )\n"stab[i].snameij);
ragge
1.1
633 # endif
ragge
1.15
634         if ((k = j) < 0)
635                 uerror("undefined structure or union");
ragge
1.1
636         else {
ragge
1.15
637                 for (; (kk = dimtab[k] ) >= 0; ++k) {
638                         if (kk >= SYMTSZ) {
ragge
1.1
639                                 cerror"gummy structure" );
640                                 return(1);
ragge
1.15
641                         }
642                         if (kk == i)
643                                 return(1);
644                         switch(stab[kk].stype) {
ragge
1.1
645
646                         case STRTY:
647                         case UNIONTY:
ragge
1.15
648                                 if (type == STRTY)
649                                         continue;  /* no recursive looking for strs */
ragge
1.1
650                         }
651                 }
ragge
1.15
652         }
653         return(0);
ragge
1.2
654 }
ragge
1.1
655
ragge
1.2
656 /*
657  * apply the op o to the lval part of p; if binary, rhs is val
658  * works only on integer constants
659  */
660 int
661 conval(NODE *pint oNODE *q)
662 {
ragge
1.1
663         int iu;
664         CONSZ val;
665         TWORD utype;
666
667         val = q->tn.lval;
668         u = ISUNSIGNED(p->in.type) || ISUNSIGNED(q->in.type);
669         ifu && (o==LE||o==LT||o==GE||o==GT)) o += (UGE-GE);
670
671         ifp->tn.rval != NONAME && q->tn.rval != NONAME ) return(0);
672         ifq->tn.rval != NONAME && o!=PLUS ) return(0);
673         ifp->tn.rval != NONAME && o!=PLUS && o!=MINUS ) return(0);
674
675         /* usual type conversions -- handle casts of constants */
676 #define ISLONG(t)       ((t) == LONG || (t) == ULONG)
ragge
1.6
677 #define ISLONGLONG(t)   ((t) == LONGLONG || (t) == ULONGLONG)
ragge
1.1
678         if (ISLONG(p->in.type) || ISLONG(q->in.type))
679                 utype = u ? ULONG : LONG;
ragge
1.6
680         else if (ISLONGLONG(p->in.type) || ISLONGLONG(q->in.type))
681                 utype = u ? ULONGLONG : LONGLONG;
ragge
1.1
682         else
683                 utype = u ? UNSIGNED : INT;
684         if( !ISPTR(p->in.type) && p->in.type != utype )
685                 p = makety(putype0, (int)utype);
686         ifq->in.type != utype )
687                 q = makety(qutype0, (int)utype);
688
689         switcho ){
690
691         case PLUS:
692                 p->tn.lval += val;
693                 ifp->tn.rval == NONAME ){
694                         p->tn.rval = q->tn.rval;
695                         p->in.type = q->in.type;
696                         }
697                 break;
698         case MINUS:
699                 p->tn.lval -= val;
700                 break;
701         case MUL:
702                 p->tn.lval *= val;
703                 break;
704         case DIV:
705                 ifval == 0 ) uerror"division by 0" );
706                 else if ( u ) p->tn.lval = (unsignedp->tn.lval / val;
707                 else p->tn.lval /= val;
708                 break;
709         case MOD:
710                 ifval == 0 ) uerror"division by 0" );
711                 else if ( u ) p->tn.lval = (unsignedp->tn.lval % val;
712                 else p->tn.lval %= val;
713                 break;
714         case AND:
715                 p->tn.lval &= val;
716                 break;
717         case OR:
718                 p->tn.lval |= val;
719                 break;
720         case ER:
721                 p->tn.lval ^= val;
722                 break;
723         case LS:
724                 i = val;
725                 p->tn.lval = p->tn.lval << i;
726                 break;
727         case RS:
728                 i = val;
729                 if ( u ) p->tn.lval = (unsignedp->tn.lval >> i;
730                 else p->tn.lval = p->tn.lval >> i;
731                 break;
732
733         case UNARY MINUS:
734                 p->tn.lval = - p->tn.lval;
735                 break;
736         case COMPL:
737                 p->tn.lval = ~p->tn.lval;
738                 break;
739         case NOT:
740                 p->tn.lval = !p->tn.lval;
741                 break;
742         case LT:
743                 p->tn.lval = p->tn.lval < val;
744                 break;
745         case LE:
746                 p->tn.lval = p->tn.lval <= val;
747                 break;
748         case GT:
749                 p->tn.lval = p->tn.lval > val;
750                 break;
751         case GE:
752                 p->tn.lval = p->tn.lval >= val;
753                 break;
754         case ULT:
755                 p->tn.lval = p->tn.lval < (unsignedval;
756                 break;
757         case ULE:
758                 p->tn.lval = p->tn.lval <= (unsignedval;
759                 break;
760         case UGT:
761                 p->tn.lval = p->tn.lval > (unsignedval;
762                 break;
763         case UGE:
764                 p->tn.lval = p->tn.lval >= (unsignedval;
765                 break;
766         case EQ:
767                 p->tn.lval = p->tn.lval == val;
768                 break;
769         case NE:
770                 p->tn.lval = p->tn.lval != val;
771                 break;
772         default:
773                 return(0);
774                 }
775         return(1);
776         }
777
ragge
1.7
778 /*
779  * Checks p for the existance of a pun.  This is called when the op of p
780  * is ASSIGN, RETURN, CAST, COLON, or relational.
781  * One case is when enumerations are used: this applies only to lint.
782  * In the other case, one operand is a pointer, the other integer type
783  * we check that this integer is in fact a constant zero...
784  * in the case of ASSIGN, any assignment of pointer to integer is illegal
785  * this falls out, because the LHS is never 0.
786  */
ragge
1.2
787 void
ragge
1.7
788 chkpun(NODE *p)
789 {
ragge
1.2
790         NODE *q;
791         int t1t2d1d2ref1ref2;
ragge
1.1
792
793         t1 = p->in.left->in.type;
794         t2 = p->in.right->in.type;
795
ragge
1.7
796         /* check for enumerations */
797         if (t1==ENUMTY || t2==ENUMTY) {
ragge
1.1
798                 /* rob pike says this is obnoxious...
799                 if( logop( p->in.op ) && p->in.op != EQ && p->in.op != NE )
800                         werror( "comparison of enums" ); */
ragge
1.7
801                 if (t1==ENUMTY && t2==ENUMTY) {
802                         if (p->in.left->fn.csiz!=p->in.right->fn.csiz)
803                                 werror("enumeration type clash, "
804                                     "operator %s"opst[p->in.op]);
ragge
1.1
805                         return;
806                 }
ragge
1.7
807                 if (t1 == ENUMTY)
808                         t1 = INT;
809                 if (t2 == ENUMTY)
810                         t2 = INT;
811         }
ragge
1.1
812
813         ref1 = ISPTR(t1) || ISARY(t1);
814         ref2 = ISPTR(t2) || ISARY(t2);
815
ragge
1.7
816         if (ref1 ^ ref2) {
817                 if (ref1)
818                         q = p->in.right;
819                 else
820                         q = p->in.left;
821                 if (q->in.op != ICON || q->tn.lval != 0)
822                         werror("illegal combination of pointer "
823                             "and integer, op %s"opst[p->in.op]);
824         } else if (ref1) {
825                 if (t1 == t2) {
826                         if (p->in.left->fn.csiz != p->in.right->fn.csiz) {
ragge
1.5
827                                 werror("illegal structure pointer combination");
ragge
1.1
828                                 return;
ragge
1.5
829                         }
ragge
1.1
830                         d1 = p->in.left->fn.cdim;
831                         d2 = p->in.right->fn.cdim;
ragge
1.7
832                         for (;;) {
833                                 if (ISARY(t1)) {
834                                         if (dimtab[d1] != dimtab[d2]) {
835                                                 werror("illegal array "
836                                                     "size combination");
ragge
1.1
837                                                 return;
ragge
1.5
838                                         }
ragge
1.1
839                                         ++d1;
840                                         ++d2;
ragge
1.5
841                                 } else
ragge
1.7
842                                         if (!ISPTR(t1))
ragge
1.5
843                                                 break;
ragge
1.1
844                                 t1 = DECREF(t1);
845                         }
ragge
1.7
846                 } else if (t1 != INCREF(UNDEF) && t2 != INCREF(UNDEF))
847                         werror("illegal pointer combination");
ragge
1.1
848         }
ragge
1.5
849 }
ragge
1.1
850
851 NODE *
ragge
1.10
852 stref(NODE *p)
853 {
ragge
1.1
854         TWORD t;
855         int dsdscalign;
856         OFFSZ off;
857         register struct symtab *q;
858
859         /* make p->x */
860         /* this is also used to reference automatic variables */
861
862         q = &stab[p->in.right->tn.rval];
863         p->in.right->in.op = FREE;
864         p->in.op = FREE;
ragge
1.10
865         p = pconvert(p->in.left);
ragge
1.1
866
867         /* make p look like ptr to x */
868
ragge
1.10
869         if (!ISPTR(p->in.type))
ragge
1.1
870                 p->in.type = PTR+UNIONTY;
871
ragge
1.10
872         t = INCREF(q->stype);
ragge
1.1
873         d = q->dimoff;
874         s = q->sizoff;
875
ragge
1.10
876         p = makety(ptds);
ragge
1.1
877
878         /* compute the offset to be added */
879
880         off = q->offset;
881         dsc = q->sclass;
882
ragge
1.10
883         if (dsc & FIELD) {  /* normalize offset */
ragge
1.1
884                 align = ALINT;
885                 s = INT;
886                 off = (off/align)*align;
ragge
1.10
887         }
888         if (off != 0)
889                 p = clocal(block(PLUSpoffcon(offtds), tds));
ragge
1.1
890
ragge
1.10
891         p = buildtree(UNARY MULpNIL);
ragge
1.1
892
893         /* if field, build field info */
894
ragge
1.10
895         if (dsc & FIELD) {
896                 p = block(FLDpNILq->stype0q->sizoff);
897                 p->tn.rval = PKFIELD(dsc&FLDSIZq->offset%align);
898         }
ragge
1.1
899
ragge
1.10
900         return(clocal(p));
901 }
ragge
1.1
902
ragge
1.2
903 int
ragge
1.1
904 notlval(pregister NODE *p; {
905
906         /* return 0 if p an lvalue, 1 otherwise */
907
908         again:
909
910         switchp->in.op ){
911
912         case FLD:
913                 p = p->in.left;
914                 goto again;
915
916         case UNARY MUL:
917                 /* fix the &(a=b) bug, given that a and b are structures */
918                 ifp->in.left->in.op == STASG ) return1 );
919                 /* and the f().a bug, given that f returns a structure */
920                 ifp->in.left->in.op == UNARY STCALL ||
921                     p->in.left->in.op == STCALL ) return1 );
922         case NAME:
923         case OREG:
924                 ifISARY(p->in.type) || ISFTN(p->in.type) ) return(1);
925         case REG:
926                 return(0);
927
928         default:
929                 return(1);
930
931                 }
932
933         }
934
935 NODE *
936 bconi ){ /* make a constant node with value i */
937         register NODE *p;
938
939         p = blockICONNILNILINT0INT );
940         p->tn.lval = i;
941         p->tn.rval = NONAME;
942         returnclocal(p) );
943         }
944
945 NODE *
ragge
1.10
946 bpsize(NODE *p)
947 {
948         return(offcon(psize(p), p->in.typep->fn.cdimp->fn.csiz));
949 }
ragge
1.1
950
ragge
1.10
951 /*
952  * p is a node of type pointer; psize returns the
953  * size of the thing pointed to
954  */
ragge
1.1
955 OFFSZ
ragge
1.10
956 psize(NODE *p)
957 {
958
959         if (!ISPTR(p->in.type)) {
960                 uerror("pointer required");
961                 return(SZINT);
962         }
ragge
1.1
963         /* note: no pointers to fields */
ragge
1.10
964         return(tsize(DECREF(p->in.type), p->fn.cdimp->fn.csiz));
965 }
ragge
1.1
966
ragge
1.10
967 /*
968  * convert an operand of p
969  * f is either CVTL or CVTR
970  * operand has type int, and is converted by the size of the other side
971  */
ragge
1.1
972 NODE *
ragge
1.10
973 convert(NODE *pint f)
974 {
975         NODE *q, *r, *s;
ragge
1.1
976
ragge
1.10
977         q = (f == CVTL) ? p->in.left : p->in.right;
ragge
1.1
978
ragge
1.10
979         s = bpsize(f == CVTL ? p->in.right : p->in.left);
980         r = block(PMCONVqsINT0INT);
ragge
1.1
981         r = clocal(r);
ragge
1.13
982         /*
983          * Indexing is only allowed with integer arguments, so insert
984          * SCONV here if arg is not an integer.
985          * XXX - complain?
986          */
987         if (r->in.type != INT)
988                 r = clocal(block(SCONVrNILINT0INT));
ragge
1.10
989         if (f == CVTL)
ragge
1.1
990                 p->in.left = r;
991         else
992                 p->in.right = r;
993         return(p);
ragge
1.10
994 }
ragge
1.1
995
996 #ifndef econvert
ragge
1.2
997 void
ragge
1.1
998 econvertp ) register NODE *p; {
999
1000         /* change enums to ints, or appropriate types */
1001
1002         register TWORD ty;
1003
1004         if( (ty=BTYPE(p->in.type)) == ENUMTY || ty == MOETY ) {
ragge
1.6
1005                 if (dimtab[p->fn.csiz] == SZCHAR)
1006                         ty = CHAR;
1007                 else if (dimtab[p->fn.csiz] == SZINT)
1008                         ty = INT;
1009                 else if (dimtab[p->fn.csiz] == SZSHORT)