Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20030803104938

Diff

Diff from 1.69 to:

Annotations

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

Annotated File View

ragge
1.69
1 /*      $Id: trees.c,v 1.69 2003/08/03 10:49:39 ragge Exp $     */
ragge
1.51
2 /*
3  * Copyright (c) 2003 Anders Magnusson (ragge@ludd.luth.se).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 /*
30  * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
31  *
32  * Redistribution and use in source and binary forms, with or without
33  * modification, are permitted provided that the following conditions
34  * are met:
35  *
36  * Redistributions of source code and documentation must retain the above
37  * copyright notice, this list of conditions and the following disclaimer.
38  * Redistributions in binary form must reproduce the above copyright
39  * notice, this list of conditionsand the following disclaimer in the
40  * documentation and/or other materials provided with the distribution.
41  * All advertising materials mentioning features or use of this software
42  * must display the following acknowledgement:
43  *      This product includes software developed or owned by Caldera
44  *      International, Inc.
45  * Neither the name of Caldera International, Inc. nor the names of other
46  * contributors may be used to endorse or promote products derived from
47  * this software without specific prior written permission.
48  *
49  * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
50  * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
51  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
52  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
53  * DISCLAIMED.  IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE
54  * FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57  * HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT,
58  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
59  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
60  * POSSIBILITY OF SUCH DAMAGE.
61  */
62 /*
63  * Some of the changes from 32V include:
64  * - Understand "void" as type.
65  * - Handle enums as ints everywhere.
ragge
1.57
66  * - Convert some C-specific ops into branches.
ragge
1.51
67  */
68
ragge
1.1
69 # include "pass1.h"
ragge
1.51
70 # include "pass2.h"
ragge
1.1
71
ragge
1.16
72 # include <stdarg.h>
ragge
1.1
73
ragge
1.51
74 static void chkpun(NODE *p);
75 static int opact(NODE *p);
76 static int moditype(TWORD);
ragge
1.45
77 static NODE *strargs(NODE *);
ragge
1.57
78 static void rmcops(NODE *p);
ragge
1.2
79
80 /* corrections when in violation of lint */
ragge
1.1
81
82 /*      some special actions, used in finding the type of nodes */
83 # define NCVT 01
84 # define PUN 02
85 # define TYPL 04
86 # define TYPR 010
87 # define TYMATCH 040
88 # define LVAL 0100
89 # define CVTO 0200
90 # define CVTL 0400
91 # define CVTR 01000
92 # define PTMATCH 02000
93 # define OTHER 04000
94 # define NCVTR 010000
95
96 /* node conventions:
97
98         NAME:   rval>0 is stab index for external
99                 rval<0 is -inlabel number
100                 lval is offset in bits
101         ICON:   lval has the value
102                 rval has the STAB index, or - label number,
103                         if a name whose address is in the constant
104                 rval = NONAME means no name
105         REG:    rval is reg. identification cookie
106
107         */
108
ragge
1.30
109 int bdebug = 0;
110
ragge
1.1
111 NODE *
ragge
1.2
112 buildtree(int oNODE *lNODE *r)
113 {
114         NODE *p, *q;
115         int actions;
116         int opty;
117         struct symtab *sp;
118         NODE *lr, *ll;
ragge
1.23
119         char *name;
ragge
1.26
120         struct symtab **elem;
ragge
1.1
121
ragge
1.68
122 #ifdef PCC_DEBUG
ragge
1.2
123         if (bdebug)
ragge
1.58
124                 printf("buildtree(%s, %p, %p)\n"copst(o), lr);
ragge
1.68
125 #endif
ragge
1.58
126         opty = coptype(o);
ragge
1.1
127
128         /* check for constants */
129
ragge
1.22
130         ifopty == UTYPE && l->n_op == ICON ){
ragge
1.1
131
132                 switcho ){
133
134                 case NOT:
135                 case UNARY MINUS:
136                 case COMPL:
137                         ifconvallol ) ) return(l);
138                         break;
139
140                 }
ragge
1.44
141         } else ifo == UNARY MINUS && l->n_op == FCON ){
142                         ifl->n_type == FLOAT )
ragge
1.22
143                                 l->n_fcon = -l->n_fcon;
ragge
1.1
144                         else
ragge
1.22
145                                 l->n_dcon = -l->n_dcon;
ragge
1.1
146                         return(l);
147
ragge
1.44
148         } else ifo==QUEST && l->n_op==ICON ) {
ragge
1.41
149                 CONSZ c = l->n_lval;
150                 nfree(l);
151                 if (c) {
152                         tfree(r->n_right);
153                         l = r->n_left;
154                         nfree(r);
155                         return(l);
156                 } else {
157                         tfree(r->n_left);
158                         l = r->n_right;
159                         nfree(r);
160                         return(l);
ragge
1.1
161                 }
ragge
1.44
162         } else if( (o==ANDAND || o==OROR) && (l->n_op==ICON||r->n_op==ICON) )
163                 goto ccwarn;
ragge
1.1
164
ragge
1.22
165         else ifopty == BITYPE && l->n_op == ICON && r->n_op == ICON ){
ragge
1.1
166
167                 switcho ){
168
169                 case ULT:
170                 case UGT:
171                 case ULE:
172                 case UGE:
173                 case LT:
174                 case GT:
175                 case LE:
176                 case GE:
177                 case EQ:
ragge
1.48
178                 case NE:
ragge
1.1
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 ) ) {
ragge
1.41
196                                 nfree(r);
ragge
1.1
197                                 return(l);
ragge
1.44
198                         }
ragge
1.1
199                         break;
200                 }
ragge
1.44
201         } else if (opty == BITYPE && (l->n_op == FCON || l->n_op == ICON) &&
202             (r->n_op == FCON || r->n_op == ICON) && (o == PLUS || o == MINUS ||
203             o == MUL || o == DIV)) {
204                 switch(o){
205                 case PLUS:
206                 case MINUS:
207                 case MUL:
208                 case DIV:
209                         if (l->n_op == ICON)
210                                 l->n_dcon = l->n_lval;
211                         else if (l->n_type == FLOAT)
212                                 l->n_dcon = l->n_fcon;
213                         if (r->n_op == ICON)
214                                 r->n_dcon = r->n_lval;
215                         else if (r->n_type == FLOAT)
216                                 r->n_dcon = r->n_fcon;
217                         switch (o) {
218                         case PLUS:
219                                 l->n_dcon += r->n_dconbreak;
220                         case MINUS:
221                                 l->n_dcon -= r->n_dconbreak;
222                         case MUL:
223                                 l->n_dcon *= r->n_dconbreak;
224                         case DIV:
225                                 if (r->n_dcon == 0)
226                                         uerror("division by 0.");
227                                 else
228                                         l->n_dcon /= r->n_dcon;
229                         }
230                         l->n_op = FCON;
231                         l->n_type = DOUBLE;
232                         l->n_sue = MKSUE(DOUBLE);
233                         nfree(r);
234                         return(l);
ragge
1.1
235                 }
ragge
1.44
236         }
ragge
1.1
237
ragge
1.30
238         /* its real; we must make a new node */
ragge
1.1
239
ragge
1.26
240         p = block(olrINT0MKSUE(INT));
ragge
1.1
241
242         actions = opact(p);
243
ragge
1.66
244         if (actions & LVAL) { /* check left descendent */
245                 if (notlval(p->n_left)) {
246                         uerror("lvalue required");
ragge
1.68
247 #ifdef notyet
ragge
1.67
248                 } else {
249                         if ((l->n_type > BTMASK && ISCON(l->n_qual)) ||
250                             (l->n_type <= BTMASK && ISCON(l->n_qual << TSHIFT)))
251                                 if (blevel > 0)
252                                         uerror("lvalue is declared const");
ragge
1.68
253 #endif
ragge
1.67
254                 }
ragge
1.66
255         }
ragge
1.1
256
257         ifactions & NCVTR ){
ragge
1.22
258                 p->n_left = pconvertp->n_left );
ragge
1.1
259                 }
260         else if( !(actions & NCVT ) ){
261                 switchopty ){
262
263                 case BITYPE:
ragge
1.22
264                         p->n_right = pconvertp->n_right );
ragge
1.1
265                 case UTYPE:
ragge
1.22
266                         p->n_left = pconvertp->n_left );
ragge
1.1
267
268                         }
269                 }
270
ragge
1.64
271         if ((actions&PUN) && (o!=CAST))
ragge
1.1
272                 chkpun(p);
273
274         ifactions & (TYPL|TYPR) ){
275
ragge
1.22
276                 q = (actions&TYPL) ? p->n_left : p->n_right;
ragge
1.1
277
ragge
1.22
278                 p->n_type = q->n_type;
ragge
1.66
279                 p->n_qual = q->n_qual;
ragge
1.29
280                 p->n_df = q->n_df;
ragge
1.26
281                 p->n_sue = q->n_sue;
ragge
1.1
282                 }
283
284         ifactions & CVTL ) p = convertpCVTL );
285         ifactions & CVTR ) p = convertpCVTR );
286         ifactions & TYMATCH ) p = tymatch(p);
287         ifactions & PTMATCH ) p = ptmatch(p);
288
289         ifactions & OTHER ){
ragge
1.22
290                 l = p->n_left;
291                 r = p->n_right;
ragge
1.1
292
293                 switch(o){
294
295                 case NAME:
ragge
1.23
296                         sp = spname;
297                         if (sp->stype == UNDEF) {
298                                 uerror("%s undefined"sp->sname);
ragge
1.1
299                                 /* make p look reasonable */
ragge
1.26
300                                 p->n_type = INT;
301                                 p->n_sue = MKSUE(INT);
ragge
1.29
302                                 p->n_df = NULL;
ragge
1.23
303                                 p->n_sp = sp;
ragge
1.22
304                                 p->n_lval = 0;
ragge
1.23
305                                 defid(pSNULL);
ragge
1.1
306                                 break;
ragge
1.23
307                         }
ragge
1.22
308                         p->n_type = sp->stype;
ragge
1.66
309                         p->n_qual = sp->squal;
ragge
1.29
310                         p->n_df = sp->sdf;
ragge
1.26
311                         p->n_sue = sp->ssue;
ragge
1.22
312                         p->n_lval = 0;
ragge
1.23
313                         p->n_sp = sp;
ragge
1.1
314                         /* special case: MOETY is really an ICON... */
ragge
1.23
315                         if (p->n_type == MOETY) {
316                                 p->n_sp = NULL;
317                                 p->n_lval = sp->soffset;
ragge
1.29
318                                 p->n_df = NULL;
ragge
1.22
319                                 p->n_type = ENUMTY;
320                                 p->n_op = ICON;
ragge
1.23
321                         }
ragge
1.1
322                         break;
323
324                 case STRING:
ragge
1.22
325                         p->n_op = NAME;
ragge
1.17
326 #ifdef CHAR_UNSIGNED
ragge
1.22
327                         p->n_type = UCHAR+ARY;
ragge
1.26
328                         p->n_sue = MKSUE(UCHAR);
ragge
1.17
329 #else
ragge
1.22
330                         p->n_type = CHAR+ARY;
ragge
1.26
331                         p->n_sue = MKSUE(CHAR);
ragge
1.17
332 #endif
ragge
1.22
333                         p->n_lval = 0;
ragge
1.23
334                         p->n_sp = NULL;
ragge
1.1
335                         break;
336
337                 case STREF:
338                         /* p->x turned into *(p+offset) */
339                         /* rhs must be a name; check correctness */
340
ragge
1.23
341                         /* Find member symbol struct */
342                         if (l->n_type != PTR+STRTY && l->n_type != PTR+UNIONTY){
343                                 uerror("struct or union required");
344                                 break;
345                         }
346
ragge
1.26
347                         if ((elem = l->n_sue->suelem) == NULL)
ragge
1.23
348                                 uerror("undefined struct or union");
349
350                         name = r->n_name;
ragge
1.26
351                         for (; *elem != NULLelem++) {
352                                 sp = *elem;
ragge
1.23
353                                 if (sp->sname == name)
354                                         break;
355                         }
ragge
1.26
356                         if (*elem == NULL)
ragge
1.23
357                                 uerror("member '%s' not declared"name);
358
359                         r->n_sp = sp;
360                         p = stref(p);
361                         break;
362
ragge
1.1
363                 case UNARY MUL:
ragge
1.41
364                         if (l->n_op == UNARY AND) {
365                                 nfree(p);
ragge
1.22
366                                 p = l->n_left;
ragge
1.41
367                                 nfree(l);
368                         }
ragge
1.22
369                         if( !ISPTR(l->n_type))uerror("illegal indirection");
370                         p->n_type = DECREF(l->n_type);
ragge
1.67
371                         p->n_qual = DECREF(l->n_qual);
ragge
1.29
372                         p->n_df = l->n_df;
ragge
1.26
373                         p->n_sue = l->n_sue;
ragge
1.1
374                         break;
375
376                 case UNARY AND:
ragge
1.22
377                         switchl->n_op ){
ragge
1.1
378
379                         case UNARY MUL:
ragge
1.41
380                                 nfree(p);
ragge
1.22
381                                 p = l->n_left;
ragge
1.41
382                                 nfree(l);
ragge
1.1
383                         case NAME:
ragge
1.68
384                                 p->n_type = INCREF(l->n_type);
385                                 p->n_qual = INCQAL(l->n_qual);
ragge
1.29
386                                 p->n_df = l->n_df;
ragge
1.26
387                                 p->n_sue = l->n_sue;
ragge
1.1
388                                 break;
389
390                         case COMOP:
ragge
1.41
391                                 nfree(p);
ragge
1.68
392                                 lr = buildtree(UNARY ANDl->n_rightNIL);
ragge
1.22
393                                 p = buildtreeCOMOPl->n_leftlr );
ragge
1.41
394                                 nfree(l);
ragge
1.1
395                                 break;
396
397                         case QUEST:
ragge
1.22
398                                 lr = buildtreeUNARY ANDl->n_right->n_rightNIL );
399                                 ll = buildtreeUNARY ANDl->n_right->n_leftNIL );
ragge
1.41
400                                 nfree(p); nfree(l->n_right);
ragge
1.22
401                                 p = buildtreeQUESTl->n_leftbuildtreeCOLONlllr ) );
ragge
1.41
402                                 nfree(l);
ragge
1.1
403                                 break;
404
405                         default:
ragge
1.22
406                                 uerror("unacceptable operand of &: %d"l->n_op );
ragge
1.1
407                                 break;
408                                 }
409                         break;
410
411                 case LS:
ragge
1.50
412                 case RS:
ragge
1.1
413                 case ASG LS:
414                 case ASG RS:
ragge
1.50
415                         if(tsize(p->n_right->n_typep->n_right->n_dfp->n_right->n_sue) > SZINT)
ragge
1.68
416                                 p->n_right = r = makety(rINT00MKSUE(INT));
ragge
1.1
417                         break;
418
419                 case RETURN:
420                 case ASSIGN:
421                 case CAST:
422                         /* structure assignment */
423                         /* take the addresses of the two sides; then make an
ragge
1.2
424                          * operator using STASG and
425                          * the addresses of left and right */
ragge
1.1
426
427                         {
ragge
1.26
428                                 struct suedef *sue;
ragge
1.2
429                                 TWORD t;
ragge
1.29
430                                 union dimfun *d;
ragge
1.1
431
ragge
1.26
432                                 if (l->n_sue != r->n_sue)
433                                         uerror("assignment of different structures");
ragge
1.1
434
ragge
1.26
435                                 r = buildtree(UNARY ANDrNIL);
ragge
1.22
436                                 t = r->n_type;
ragge
1.29
437                                 d = r->n_df;
ragge
1.26
438                                 sue = r->n_sue;
ragge
1.1
439
ragge
1.28
440                                 l = block(STASGlrtdsue);
ragge
1.1
441
442                                 ifo == RETURN ){
ragge
1.41
443                                         nfree(p);
ragge
1.1
444                                         p = l;
445                                         break;
ragge
1.28
446                                 }
ragge
1.1
447
ragge
1.22
448                                 p->n_op = UNARY MUL;
449                                 p->n_left = l;
450                                 p->n_right = NIL;
ragge
1.1
451                                 break;
452                                 }
453                 case COLON:
454                         /* structure colon */
455
ragge
1.26
456                         if (l->n_sue != r->n_sue)
457                                 uerror"type clash in conditional" );
ragge
1.1
458                         break;
459
460                 case CALL:
ragge
1.45
461                         p->n_right = r = strargs(p->n_right);
ragge
1.1
462                 case UNARY CALL:
ragge
1.23
463                         if (!ISPTR(l->n_type))
464                                 uerror("illegal function");
ragge
1.22
465                         p->n_type = DECREF(l->n_type);
ragge
1.23
466                         if (!ISFTN(p->n_type))
467                                 uerror("illegal function");
468                         p->n_type = DECREF(p->n_type);
ragge
1.29
469                         p->n_df = l->n_df;
ragge
1.26
470                         p->n_sue = l->n_sue;
ragge
1.23
471                         if (l->n_op == UNARY AND && l->n_left->n_op == NAME &&
472                             l->n_left->n_sp != NULL && l->n_left->n_sp != NULL &&
473                             (l->n_left->n_sp->sclass == FORTRAN ||
474                             l->n_left->n_sp->sclass == UFORTRAN)) {
ragge
1.22
475                                 p->n_op += (FORTCALL-CALL);
ragge
1.23
476                         }
477                         if (p->n_type == STRTY || p->n_type == UNIONTY) {
ragge
1.1
478                                 /* function returning structure */
479                                 /*  make function really return ptr to str., with * */
480
ragge
1.22
481                                 p->n_op += STCALL-CALL;
ragge
1.23
482                                 p->n_type = INCREF(p->n_type);
483                                 p = buildtree(UNARY MULpNIL);
ragge
1.1
484
485                                 }
486                         break;
487
488                 default:
489                         cerror"other code %d"o );
490                         }
491
492                 }
493
ragge
1.11
494         /*
495          * Allow (void)0 casts.
496          * XXX - anything on the right side must be possible to cast.
497          * XXX - remove void types further on.
498          */
ragge
1.50
499         if (p->n_op == CAST && p->n_type == VOID &&
ragge
1.22
500             p->n_right->n_op == ICON)
ragge
1.50
501                 p->n_right->n_type = VOID;
ragge
1.11
502
ragge
1.68
503         if (actions & CVTO)
504                 p = oconvert(p);
ragge
1.1
505         p = clocal(p);
506
ragge
1.30
507 #ifdef PCC_DEBUG
ragge
1.68
508         if (bdebug)
509                 fwalk(peprint0);
ragge
1.30
510 #endif
ragge
1.1
511
512         return(p);
513
514         }
515
516 NODE *
ragge
1.45
517 strargsp ) register NODE *p;  { /* rewrite structure flavored arguments */
ragge
1.1
518
ragge
1.45
519         ifp->n_op == CM ){
520                 p->n_left = strargsp->n_left );
521                 p->n_right = strargsp->n_right );
ragge
1.1
522                 returnp );
523                 }
524
ragge
1.22
525         ifp->n_type == STRTY || p->n_type == UNIONTY ){
ragge
1.29
526                 p = block(STARGpNILp->n_typep->n_dfp->n_sue);
ragge
1.22
527                 p->n_left = buildtreeUNARY ANDp->n_leftNIL );
ragge
1.1
528                 p = clocal(p);
529                 }
530         returnp );
ragge
1.2
531 }
ragge
1.1
532
ragge
1.2
533 /*
534  * apply the op o to the lval part of p; if binary, rhs is val
535  */
536 int
537 conval(NODE *pint oNODE *q)
538 {
ragge
1.1
539         int iu;
540         CONSZ val;
541
ragge
1.22
542         val = q->n_lval;
543         u = ISUNSIGNED(p->n_type) || ISUNSIGNED(q->n_type);
ragge
1.1
544         ifu && (o==LE||o==LT||o==GE||o==GT)) o += (UGE-GE);
545
ragge
1.23
546         if (p->n_sp != NULL && q->n_sp != NULL)
547                 return(0);
548         if (q->n_sp != NULL && o != PLUS)
549                 return(0);
550         if (p->n_sp != NULL && o != PLUS && o != MINUS)
551                 return(0);
ragge
1.1
552
553         switcho ){
554
555         case PLUS:
ragge
1.22
556                 p->n_lval += val;
ragge
1.23
557                 if (p->n_sp == NULL) {
ragge
1.22
558                         p->n_rval = q->n_rval;
559                         p->n_type = q->n_type;
ragge
1.23
560                 }
ragge
1.1
561                 break;
562         case MINUS:
ragge
1.22
563                 p->n_lval -= val;
ragge
1.1
564                 break;
565         case MUL:
ragge
1.22
566                 p->n_lval *= val;
ragge
1.1
567                 break;
568         case DIV:
ragge
1.46
569                 if (val == 0)
570                         uerror("division by 0");
571                 else 
572                         p->n_lval /= val;
ragge
1.1
573                 break;
574         case MOD:
ragge
1.46
575                 if (val == 0)
576                         uerror("division by 0");
577                 else 
578                         p->n_lval %= val;
ragge
1.1
579                 break;
580         case AND:
ragge
1.22
581                 p->n_lval &= val;
ragge
1.1
582                 break;
583         case OR:
ragge
1.22
584                 p->n_lval |= val;
ragge
1.1
585                 break;
586         case ER:
ragge
1.22
587                 p->n_lval ^= val;
ragge
1.1
588                 break;
589         case LS:
590                 i = val;
ragge
1.22
591                 p->n_lval = p->n_lval << i;
ragge
1.1
592                 break;
593         case RS:
594                 i = val;
ragge
1.46
595                 p->n_lval = p->n_lval >> i;
ragge
1.1
596                 break;
597
598         case UNARY MINUS:
ragge
1.22
599                 p->n_lval = - p->n_lval;
ragge
1.1
600                 break;
601         case COMPL:
ragge
1.22
602                 p->n_lval = ~p->n_lval;
ragge
1.1
603                 break;
604         case NOT:
ragge
1.22
605                 p->n_lval = !p->n_lval;
ragge
1.1
606                 break;
607         case LT:
ragge
1.22
608                 p->n_lval = p->n_lval < val;
ragge
1.1
609                 break;
610         case LE:
ragge
1.22
611                 p->n_lval = p->n_lval <= val;
ragge
1.1
612                 break;
613         case GT:
ragge
1.22
614                 p->n_lval = p->n_lval > val;
ragge
1.1
615                 break;
616         case GE:
ragge
1.22
617                 p->n_lval = p->n_lval >= val;
ragge
1.1
618                 break;
619         case ULT:
ragge
1.46
620                 p->n_lval = (p->n_lval-val)<0;
ragge
1.1
621                 break;
622         case ULE:
ragge
1.46
623                 p->n_lval = (p->n_lval-val)<=0;
ragge
1.1
624                 break;
625         case UGT:
ragge
1.46
626                 p->n_lval = (p->n_lval-val)>=0;
ragge
1.1
627                 break;
628         case UGE:
ragge
1.46
629                 p->n_lval = (p->n_lval-val)>0;
ragge
1.1
630                 break;
631         case EQ:
ragge
1.22
632                 p->n_lval = p->n_lval == val;
ragge
1.1
633                 break;
634         case NE:
ragge
1.22
635                 p->n_lval = p->n_lval != val;
ragge
1.1
636                 break;
637         default:
638                 return(0);
639                 }
640         return(1);
641         }
642
ragge
1.7
643 /*
644  * Checks p for the existance of a pun.  This is called when the op of p
645  * is ASSIGN, RETURN, CAST, COLON, or relational.
646  * One case is when enumerations are used: this applies only to lint.
647  * In the other case, one operand is a pointer, the other integer type
648  * we check that this integer is in fact a constant zero...
649  * in the case of ASSIGN, any assignment of pointer to integer is illegal
650  * this falls out, because the LHS is never 0.
651  */
ragge
1.2
652 void
ragge
1.7
653 chkpun(NODE *p)
654 {
ragge
1.29
655         union dimfun *d1, *d2;
ragge
1.2
656         NODE *q;
ragge
1.47
657         int t1t2;
ragge
1.1
658
ragge
1.22
659         t1 = p->n_left->n_type;
660         t2 = p->n_right->n_type;
ragge
1.1
661
ragge
1.56
662         switch (p->n_op) {
663         case RETURN:
664                 /* return of void allowed but nothing else */
ragge
1.52
665                 if (t1 == VOID && t2 == VOID)
666                         return;
667                 if (t1 == VOID)
668                         return uerror("returning value from void function");
669                 if (t2 == VOID)
670                         return uerror("using void value");
ragge
1.56
671         case COLON:
672                 if (t1 == VOID && t2 == VOID)
673                         return;
674                 break;
675         default:
676                 if ((t1 == VOID && t2 != VOID) || (t1 != VOID && t2 == VOID))
677                         return uerror("value of void expression used");
678                 break;
ragge
1.52
679         }
680
681         /* allow void pointer assignments in any direction */
682         if (BTYPE(t1) == VOID && (t2 & TMASK))
683                 return;
684         if (BTYPE(t2) == VOID && (t1 & TMASK))
685                 return;
686
ragge
1.7
687         /* check for enumerations */
688         if (t1==ENUMTY || t2==ENUMTY) {
ragge
1.58
689                 ifclogopp->n_op ) && p->n_op != EQ && p->n_op != NE ) {
ragge
1.47
690                         uerror"comparison of enums" );
691                         return;
692                         }
ragge
1.7
693                 if (t1==ENUMTY && t2==ENUMTY) {
ragge
1.26
694                         if (p->n_left->n_sue!=p->n_right->n_sue)
ragge
1.7
695                                 werror("enumeration type clash, "
ragge
1.58
696                                     "operator %s"copst(p->n_op));
ragge
1.1
697                         return;
698                 }
ragge
1.7
699         }
ragge
1.1
700
ragge
1.52
701         if (ISPTR(t1) || ISARY(t1))
702                 q = p->n_right;
703         else
704                 q = p->n_left;
ragge
1.1
705
ragge
1.52
706         if (!ISPTR(q->n_type) && !ISARY(q->n_type)) {
ragge
1.22
707                 if (q->n_op != ICON || q->n_lval != 0)
ragge
1.47
708                         werror("illegal combination of pointer and integer");
709         } else {
710                 d1 = p->n_left->n_df;
711                 d2 = p->n_right->n_df;
ragge
1.52
712                 if (t1 == t2) {
713                         if (p->n_left->n_sue != p->n_right->n_sue)
714                                 werror("illegal structure pointer combination");
715                         return;
716                 }
ragge
1.47
717                 for (;;) {
ragge
1.52
718                         if (ISARY(t1) || ISPTR(t1)) {
719                                 if (!ISARY(t2) && !ISPTR(t2))
720                                         break;
ragge
1.47
721                                 if (ISARY(t1) && ISARY(t2) && d1->ddim != d2->ddim) {
722                                         werror("illegal array size combination");
723                                         return;
724                                 }
ragge
1.52
725                                 if (ISARY(t1))
726                                         ++d1;
727                                 if (ISARY(t2))
728                                         ++d2;
ragge
1.47
729                         } else if (ISFTN(t1)) {
730                                 if (chkftn(d1->dfund2->dfun)) {
731                                         werror("illegal function "
732                                             "pointer combination");
733                                         return;
734                                 }
735                                 ++d1;
736                                 ++d2;
ragge
1.52
737                         } else
738                                 break;
ragge
1.47
739                         t1 = DECREF(t1);
740                         t2 = DECREF(t2);
741                 }
742                 werror("illegal pointer combination");
ragge
1.1
743         }
ragge
1.5
744 }
ragge
1.1
745
746 NODE *
ragge
1.10
747 stref(NODE *p)
748 {
ragge
1.41
749         NODE *r;
ragge
1.26
750         struct suedef *sue;
ragge
1.29
751         union dimfun *d;
ragge
1.68
752         TWORD tq;
ragge
1.49
753         int dsc;
ragge
1.1
754         OFFSZ off;
ragge
1.68
755         struct symtab *s;
ragge
1.1
756
757         /* make p->x */
758         /* this is also used to reference automatic variables */
759
ragge
1.68
760         s = p->n_right->n_sp;
ragge
1.41
761         nfree(p->n_right);
762         r = p->n_left;
763         nfree(p);
764         p = pconvert(r);
ragge
1.1
765
766         /* make p look like ptr to x */
767
ragge
1.22
768         if (!ISPTR(p->n_type))
769                 p->n_type = PTR+UNIONTY;
ragge
1.1
770
ragge
1.68
771         t = INCREF(s->stype);
772         q = INCQAL(s->squal);
773         d = s->sdf;
774         sue = s->ssue;
ragge
1.1
775
ragge
1.68
776         p = makety(ptqdsue);
ragge
1.1
777
778         /* compute the offset to be added */
779
ragge
1.68
780         off = s->soffset;
781         dsc = s->sclass;
ragge
1.1
782
ragge
1.49
783         if (dsc & FIELD) {  /* make fields look like ints */
784                 off = (off/ALINT)*ALINT;
ragge
1.26
785                 sue = MKSUE(INT);
ragge
1.10
786         }
ragge
1.68
787         if (off != 0) {
788                 p = block(PLUSpoffcon(offtdsue), tdsue);
789                 p->n_qual = q;
790                 p = clocal(p);
791         }
ragge
1.1
792
ragge
1.10
793         p = buildtree(UNARY MULpNIL);
ragge
1.1
794
795         /* if field, build field info */
796
ragge
1.10
797         if (dsc & FIELD) {
ragge
1.68
798                 p = block(FLDpNILs->stype0s->ssue);
799                 p->n_qual = q;
800                 p->n_rval = PKFIELD(dsc&FLDSIZs->soffset%ALINT);
ragge
1.10
801         }
ragge
1.1
802
ragge
1.19
803         p = clocal(p);
804         return p;
ragge
1.10
805 }
ragge
1.1
806
ragge
1.2
807 int
ragge
1.1
808 notlval(pregister NODE *p; {
809
810         /* return 0 if p an lvalue, 1 otherwise */
811
812         again:
813
ragge
1.22
814         switchp->n_op ){
ragge
1.1
815
816         case FLD:
ragge
1.22
817                 p = p->n_left;
ragge
1.1
818                 goto again;
819
820         case NAME:
821         case OREG:
ragge
1.49
822         case UNARY MUL:
ragge
1.22
823                 ifISARY(p->n_type) || ISFTN(p->n_type) ) return(1);
ragge
1.55
824         case TEMP:
ragge
1.1
825         case REG:
826                 return(0);
827
828         default:
829                 return(1);
830
831                 }
832
833         }
ragge
1.23
834 /* make a constant node with value i */
ragge
1.1
835 NODE *
ragge
1.23
836 bcon(int i)
837 {
ragge
1.1
838         register NODE *p;
839
ragge
1.26
840         p = block(ICONNILNILINT0MKSUE(INT));
ragge
1.22
841         p->n_lval = i;
ragge
1.23
842         p->n_sp = NULL;
843         return(clocal(p));
844 }
ragge
1.1
845
846 NODE *
ragge
1.10
847 bpsize(NODE *p)
848 {
ragge
1.29
849         return(offcon(psize(p), p->n_typep->n_dfp->n_sue));
ragge
1.10
850 }
ragge
1.1
851
ragge
1.10
852 /*
853  * p is a node of type pointer; psize returns the
854  * size of the thing pointed to
855  */
ragge
1.1
856 OFFSZ
ragge
1.10
857 psize(NODE *p)
858 {
859
ragge
1.22
860         if (!ISPTR(p->n_type)) {
ragge
1.10
861                 uerror("pointer required");
862                 return(SZINT);