Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20030802112243

Diff

Diff from 1.67 to:

Annotations

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

Annotated File View

ragge
1.67
1 /*      $Id: trees.c,v 1.67 2003/08/02 11:22:44 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
122 # ifndef BUG1
ragge
1.2
123         if (bdebug)
ragge
1.58
124                 printf("buildtree(%s, %p, %p)\n"copst(o), lr);
ragge
1.1
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.67
247                 } else {
248                         if ((l->n_type > BTMASK && ISCON(l->n_qual)) ||
249                             (l->n_type <= BTMASK && ISCON(l->n_qual << TSHIFT)))
250                                 if (blevel > 0)
251                                         uerror("lvalue is declared const");
252                 }
ragge
1.66
253         }
ragge
1.1
254
255         ifactions & NCVTR ){
ragge
1.22
256                 p->n_left = pconvertp->n_left );
ragge
1.1
257                 }
258         else if( !(actions & NCVT ) ){
259                 switchopty ){
260
261                 case BITYPE:
ragge
1.22
262                         p->n_right = pconvertp->n_right );
ragge
1.1
263                 case UTYPE:
ragge
1.22
264                         p->n_left = pconvertp->n_left );
ragge
1.1
265
266                         }
267                 }
268
ragge
1.64
269         if ((actions&PUN) && (o!=CAST))
ragge
1.1
270                 chkpun(p);
271
272         ifactions & (TYPL|TYPR) ){
273
ragge
1.22
274                 q = (actions&TYPL) ? p->n_left : p->n_right;
ragge
1.1
275
ragge
1.22
276                 p->n_type = q->n_type;
ragge
1.66
277                 p->n_qual = q->n_qual;
ragge
1.29
278                 p->n_df = q->n_df;
ragge
1.26
279                 p->n_sue = q->n_sue;
ragge
1.1
280                 }
281
282         ifactions & CVTL ) p = convertpCVTL );
283         ifactions & CVTR ) p = convertpCVTR );
284         ifactions & TYMATCH ) p = tymatch(p);
285         ifactions & PTMATCH ) p = ptmatch(p);
286
287         ifactions & OTHER ){
ragge
1.22
288                 l = p->n_left;
289                 r = p->n_right;
ragge
1.1
290
291                 switch(o){
292
293                 case NAME:
ragge
1.23
294                         sp = spname;
295                         if (sp->stype == UNDEF) {
296                                 uerror("%s undefined"sp->sname);
ragge
1.1
297                                 /* make p look reasonable */
ragge
1.26
298                                 p->n_type = INT;
299                                 p->n_sue = MKSUE(INT);
ragge
1.29
300                                 p->n_df = NULL;
ragge
1.23
301                                 p->n_sp = sp;
ragge
1.22
302                                 p->n_lval = 0;
ragge
1.23
303                                 defid(pSNULL);
ragge
1.1
304                                 break;
ragge
1.23
305                         }
ragge
1.22
306                         p->n_type = sp->stype;
ragge
1.66
307                         p->n_qual = sp->squal;
ragge
1.29
308                         p->n_df = sp->sdf;
ragge
1.26
309                         p->n_sue = sp->ssue;
ragge
1.22
310                         p->n_lval = 0;
ragge
1.23
311                         p->n_sp = sp;
ragge
1.1
312                         /* special case: MOETY is really an ICON... */
ragge
1.23
313                         if (p->n_type == MOETY) {
314                                 p->n_sp = NULL;
315                                 p->n_lval = sp->soffset;
ragge
1.29
316                                 p->n_df = NULL;
ragge
1.22
317                                 p->n_type = ENUMTY;
318                                 p->n_op = ICON;
ragge
1.23
319                         }
ragge
1.1
320                         break;
321
322                 case STRING:
ragge
1.22
323                         p->n_op = NAME;
ragge
1.17
324 #ifdef CHAR_UNSIGNED
ragge
1.22
325                         p->n_type = UCHAR+ARY;
ragge
1.26
326                         p->n_sue = MKSUE(UCHAR);
ragge
1.17
327 #else
ragge
1.22
328                         p->n_type = CHAR+ARY;
ragge
1.26
329                         p->n_sue = MKSUE(CHAR);
ragge
1.17
330 #endif
ragge
1.22
331                         p->n_lval = 0;
ragge
1.23
332                         p->n_sp = NULL;
ragge
1.1
333                         break;
334
335                 case STREF:
336                         /* p->x turned into *(p+offset) */
337                         /* rhs must be a name; check correctness */
338
ragge
1.23
339                         /* Find member symbol struct */
340                         if (l->n_type != PTR+STRTY && l->n_type != PTR+UNIONTY){
341                                 uerror("struct or union required");
342                                 break;
343                         }
344
ragge
1.26
345                         if ((elem = l->n_sue->suelem) == NULL)
ragge
1.23
346                                 uerror("undefined struct or union");
347
348                         name = r->n_name;
ragge
1.26
349                         for (; *elem != NULLelem++) {
350                                 sp = *elem;
ragge
1.23
351                                 if (sp->sname == name)
352                                         break;
353                         }
ragge
1.26
354                         if (*elem == NULL)
ragge
1.23
355                                 uerror("member '%s' not declared"name);
356
357                         r->n_sp = sp;
358                         p = stref(p);
359                         break;
360
ragge
1.1
361                 case UNARY MUL:
ragge
1.41
362                         if (l->n_op == UNARY AND) {
363                                 nfree(p);
ragge
1.22
364                                 p = l->n_left;
ragge
1.41
365                                 nfree(l);
366                         }
ragge
1.22
367                         if( !ISPTR(l->n_type))uerror("illegal indirection");
368                         p->n_type = DECREF(l->n_type);
ragge
1.67
369                         p->n_qual = DECREF(l->n_qual);
ragge
1.29
370                         p->n_df = l->n_df;
ragge
1.26
371                         p->n_sue = l->n_sue;
ragge
1.1
372                         break;
373
374                 case UNARY AND:
ragge
1.22
375                         switchl->n_op ){
ragge
1.1
376
377                         case UNARY MUL:
ragge
1.41
378                                 nfree(p);
ragge
1.22
379                                 p = l->n_left;
ragge
1.41
380                                 nfree(l);
ragge
1.1
381                         case NAME:
ragge
1.22
382                                 p->n_type = INCREFl->n_type );
ragge
1.29
383                                 p->n_df = l->n_df;
ragge
1.26
384                                 p->n_sue = l->n_sue;
ragge
1.1
385                                 break;
386
387                         case COMOP:
ragge
1.41
388                                 nfree(p);
ragge
1.22
389                                 lr = buildtreeUNARY ANDl->n_rightNIL );
390                                 p = buildtreeCOMOPl->n_leftlr );
ragge
1.41
391                                 nfree(l);
ragge
1.1
392                                 break;
393
394                         case QUEST:
ragge
1.22
395                                 lr = buildtreeUNARY ANDl->n_right->n_rightNIL );
396                                 ll = buildtreeUNARY ANDl->n_right->n_leftNIL );
ragge
1.41
397                                 nfree(p); nfree(l->n_right);
ragge
1.22
398                                 p = buildtreeQUESTl->n_leftbuildtreeCOLONlllr ) );
ragge
1.41
399                                 nfree(l);
ragge
1.1
400                                 break;
401
402                         default:
ragge
1.22
403                                 uerror("unacceptable operand of &: %d"l->n_op );
ragge
1.1
404                                 break;
405                                 }
406                         break;
407
408                 case LS:
ragge
1.50
409                 case RS:
ragge
1.1
410                 case ASG LS:
411                 case ASG RS:
ragge
1.50
412                         if(tsize(p->n_right->n_typep->n_right->n_dfp->n_right->n_sue) > SZINT)
ragge
1.26
413                                 p->n_right = r = makety(rINT0MKSUE(INT));
ragge
1.1
414                         break;
415
416                 case RETURN:
417                 case ASSIGN:
418                 case CAST:
419                         /* structure assignment */
420                         /* take the addresses of the two sides; then make an
ragge
1.2
421                          * operator using STASG and
422                          * the addresses of left and right */
ragge
1.1
423
424                         {
ragge
1.26
425                                 struct suedef *sue;
ragge
1.2
426                                 TWORD t;
ragge
1.29
427                                 union dimfun *d;
ragge
1.1
428
ragge
1.26
429                                 if (l->n_sue != r->n_sue)
430                                         uerror("assignment of different structures");
ragge
1.1
431
ragge
1.26
432                                 r = buildtree(UNARY ANDrNIL);
ragge
1.22
433                                 t = r->n_type;
ragge
1.29
434                                 d = r->n_df;
ragge
1.26
435                                 sue = r->n_sue;
ragge
1.1
436
ragge
1.28
437                                 l = block(STASGlrtdsue);
ragge
1.1
438
439                                 ifo == RETURN ){
ragge
1.41
440                                         nfree(p);
ragge
1.1
441                                         p = l;
442                                         break;
ragge
1.28
443                                 }
ragge
1.1
444
ragge
1.22
445                                 p->n_op = UNARY MUL;
446                                 p->n_left = l;
447                                 p->n_right = NIL;
ragge
1.1
448                                 break;
449                                 }
450                 case COLON:
451                         /* structure colon */
452
ragge
1.26
453                         if (l->n_sue != r->n_sue)
454                                 uerror"type clash in conditional" );
ragge
1.1
455                         break;
456
457                 case CALL:
ragge
1.45
458                         p->n_right = r = strargs(p->n_right);
ragge
1.1
459                 case UNARY CALL:
ragge
1.23
460                         if (!ISPTR(l->n_type))
461                                 uerror("illegal function");
ragge
1.22
462                         p->n_type = DECREF(l->n_type);
ragge
1.23
463                         if (!ISFTN(p->n_type))
464                                 uerror("illegal function");
465                         p->n_type = DECREF(p->n_type);
ragge
1.29
466                         p->n_df = l->n_df;
ragge
1.26
467                         p->n_sue = l->n_sue;
ragge
1.23
468                         if (l->n_op == UNARY AND && l->n_left->n_op == NAME &&
469                             l->n_left->n_sp != NULL && l->n_left->n_sp != NULL &&
470                             (l->n_left->n_sp->sclass == FORTRAN ||
471                             l->n_left->n_sp->sclass == UFORTRAN)) {
ragge
1.22
472                                 p->n_op += (FORTCALL-CALL);
ragge
1.23
473                         }
474                         if (p->n_type == STRTY || p->n_type == UNIONTY) {
ragge
1.1
475                                 /* function returning structure */
476                                 /*  make function really return ptr to str., with * */
477
ragge
1.22
478                                 p->n_op += STCALL-CALL;
ragge
1.23
479                                 p->n_type = INCREF(p->n_type);
480                                 p = buildtree(UNARY MULpNIL);
ragge
1.1
481
482                                 }
483                         break;
484
485                 default:
486                         cerror"other code %d"o );
487                         }
488
489                 }
490
ragge
1.11
491         /*
492          * Allow (void)0 casts.
493          * XXX - anything on the right side must be possible to cast.
494          * XXX - remove void types further on.
495          */
ragge
1.50
496         if (p->n_op == CAST && p->n_type == VOID &&
ragge
1.22
497             p->n_right->n_op == ICON)
ragge
1.50
498                 p->n_right->n_type = VOID;
ragge
1.11
499
ragge
1.56
500 ifbdebug ) fwalkpeprint0 );
ragge
1.1
501         ifactions & CVTO ) p = oconvert(p);
502         p = clocal(p);
503
ragge
1.30
504 #ifdef PCC_DEBUG
ragge
1.1
505         ifbdebug ) fwalkpeprint0 );
ragge
1.30
506 #endif
ragge
1.1
507
508         return(p);
509
510         }
511
512 NODE *
ragge
1.45
513 strargsp ) register NODE *p;  { /* rewrite structure flavored arguments */
ragge
1.1
514
ragge
1.45
515         ifp->n_op == CM ){
516                 p->n_left = strargsp->n_left );
517                 p->n_right = strargsp->n_right );
ragge
1.1
518                 returnp );
519                 }
520
ragge
1.22
521         ifp->n_type == STRTY || p->n_type == UNIONTY ){
ragge
1.29
522                 p = block(STARGpNILp->n_typep->n_dfp->n_sue);
ragge
1.22
523                 p->n_left = buildtreeUNARY ANDp->n_leftNIL );
ragge
1.1
524                 p = clocal(p);
525                 }
526         returnp );
ragge
1.2
527 }
ragge
1.1
528
ragge
1.2
529 /*
530  * apply the op o to the lval part of p; if binary, rhs is val
531  */
532 int
533 conval(NODE *pint oNODE *q)
534 {
ragge
1.1
535         int iu;
536         CONSZ val;
537
ragge
1.22
538         val = q->n_lval;
539         u = ISUNSIGNED(p->n_type) || ISUNSIGNED(q->n_type);
ragge
1.1
540         ifu && (o==LE||o==LT||o==GE||o==GT)) o += (UGE-GE);
541
ragge
1.23
542         if (p->n_sp != NULL && q->n_sp != NULL)
543                 return(0);
544         if (q->n_sp != NULL && o != PLUS)
545                 return(0);
546         if (p->n_sp != NULL && o != PLUS && o != MINUS)
547                 return(0);
ragge
1.1
548
549         switcho ){
550
551         case PLUS:
ragge
1.22
552                 p->n_lval += val;
ragge
1.23
553                 if (p->n_sp == NULL) {
ragge
1.22
554                         p->n_rval = q->n_rval;
555                         p->n_type = q->n_type;
ragge
1.23
556                 }
ragge
1.1
557                 break;
558         case MINUS:
ragge
1.22
559                 p->n_lval -= val;
ragge
1.1
560                 break;
561         case MUL:
ragge
1.22
562                 p->n_lval *= val;
ragge
1.1
563                 break;
564         case DIV:
ragge
1.46
565                 if (val == 0)
566                         uerror("division by 0");
567                 else 
568                         p->n_lval /= val;
ragge
1.1
569                 break;
570         case MOD:
ragge
1.46
571                 if (val == 0)
572                         uerror("division by 0");
573                 else 
574                         p->n_lval %= val;
ragge
1.1
575                 break;
576         case AND:
ragge
1.22
577                 p->n_lval &= val;
ragge
1.1
578                 break;
579         case OR:
ragge
1.22
580                 p->n_lval |= val;
ragge
1.1
581                 break;
582         case ER:
ragge
1.22
583                 p->n_lval ^= val;
ragge
1.1
584                 break;
585         case LS:
586                 i = val;
ragge
1.22
587                 p->n_lval = p->n_lval << i;
ragge
1.1
588                 break;
589         case RS:
590                 i = val;
ragge
1.46
591                 p->n_lval = p->n_lval >> i;
ragge
1.1
592                 break;
593
594         case UNARY MINUS:
ragge
1.22
595                 p->n_lval = - p->n_lval;
ragge
1.1
596                 break;
597         case COMPL:
ragge
1.22
598                 p->n_lval = ~p->n_lval;
ragge
1.1
599                 break;
600         case NOT:
ragge
1.22
601                 p->n_lval = !p->n_lval;
ragge
1.1
602                 break;
603         case LT:
ragge
1.22
604                 p->n_lval = p->n_lval < val;
ragge
1.1
605                 break;
606         case LE:
ragge
1.22
607                 p->n_lval = p->n_lval <= val;
ragge
1.1
608                 break;
609         case GT:
ragge
1.22
610                 p->n_lval = p->n_lval > val;
ragge
1.1
611                 break;
612         case GE:
ragge
1.22
613                 p->n_lval = p->n_lval >= val;
ragge
1.1
614                 break;
615         case ULT:
ragge
1.46
616                 p->n_lval = (p->n_lval-val)<0;
ragge
1.1
617                 break;
618         case ULE:
ragge
1.46
619                 p->n_lval = (p->n_lval-val)<=0;
ragge
1.1
620                 break;
621         case UGT:
ragge
1.46
622                 p->n_lval = (p->n_lval-val)>=0;
ragge
1.1
623                 break;
624         case UGE:
ragge
1.46
625                 p->n_lval = (p->n_lval-val)>0;
ragge
1.1
626                 break;
627         case EQ:
ragge
1.22
628                 p->n_lval = p->n_lval == val;
ragge
1.1
629                 break;
630         case NE:
ragge
1.22
631                 p->n_lval = p->n_lval != val;
ragge
1.1
632                 break;
633         default:
634                 return(0);
635                 }
636         return(1);
637         }
638
ragge
1.7
639 /*
640  * Checks p for the existance of a pun.  This is called when the op of p
641  * is ASSIGN, RETURN, CAST, COLON, or relational.
642  * One case is when enumerations are used: this applies only to lint.
643  * In the other case, one operand is a pointer, the other integer type
644  * we check that this integer is in fact a constant zero...
645  * in the case of ASSIGN, any assignment of pointer to integer is illegal
646  * this falls out, because the LHS is never 0.
647  */
ragge
1.2
648 void
ragge
1.7
649 chkpun(NODE *p)
650 {
ragge
1.29
651         union dimfun *d1, *d2;
ragge
1.2
652         NODE *q;
ragge
1.47
653         int t1t2;
ragge
1.1
654
ragge
1.22
655         t1 = p->n_left->n_type;
656         t2 = p->n_right->n_type;
ragge
1.1
657
ragge
1.56
658         switch (p->n_op) {
659         case RETURN:
660                 /* return of void allowed but nothing else */
ragge
1.52
661                 if (t1 == VOID && t2 == VOID)
662                         return;
663                 if (t1 == VOID)
664                         return uerror("returning value from void function");
665                 if (t2 == VOID)
666                         return uerror("using void value");
ragge
1.56
667         case COLON:
668                 if (t1 == VOID && t2 == VOID)
669                         return;
670                 break;
671         default:
672                 if ((t1 == VOID && t2 != VOID) || (t1 != VOID && t2 == VOID))
673                         return uerror("value of void expression used");
674                 break;
ragge
1.52
675         }
676
677         /* allow void pointer assignments in any direction */
678         if (BTYPE(t1) == VOID && (t2 & TMASK))
679                 return;
680         if (BTYPE(t2) == VOID && (t1 & TMASK))
681                 return;
682
ragge
1.7
683         /* check for enumerations */
684         if (t1==ENUMTY || t2==ENUMTY) {
ragge
1.58
685                 ifclogopp->n_op ) && p->n_op != EQ && p->n_op != NE ) {
ragge
1.47
686                         uerror"comparison of enums" );
687                         return;
688                         }
ragge
1.7
689                 if (t1==ENUMTY && t2==ENUMTY) {
ragge
1.26
690                         if (p->n_left->n_sue!=p->n_right->n_sue)
ragge
1.7
691                                 werror("enumeration type clash, "
ragge
1.58
692                                     "operator %s"copst(p->n_op));
ragge
1.1
693                         return;
694                 }
ragge
1.7
695         }
ragge
1.1
696
ragge
1.52
697         if (ISPTR(t1) || ISARY(t1))
698                 q = p->n_right;
699         else
700                 q = p->n_left;
ragge
1.1
701
ragge
1.52
702         if (!ISPTR(q->n_type) && !ISARY(q->n_type)) {
ragge
1.22
703                 if (q->n_op != ICON || q->n_lval != 0)
ragge
1.47
704                         werror("illegal combination of pointer and integer");
705         } else {
706                 d1 = p->n_left->n_df;
707                 d2 = p->n_right->n_df;
ragge
1.52
708                 if (t1 == t2) {
709                         if (p->n_left->n_sue != p->n_right->n_sue)
710                                 werror("illegal structure pointer combination");
711                         return;
712                 }
ragge
1.47
713                 for (;;) {
ragge
1.52
714                         if (ISARY(t1) || ISPTR(t1)) {
715                                 if (!ISARY(t2) && !ISPTR(t2))
716                                         break;
ragge
1.47
717                                 if (ISARY(t1) && ISARY(t2) && d1->ddim != d2->ddim) {
718                                         werror("illegal array size combination");
719                                         return;
720                                 }
ragge
1.52
721                                 if (ISARY(t1))
722                                         ++d1;
723                                 if (ISARY(t2))
724                                         ++d2;
ragge
1.47
725                         } else if (ISFTN(t1)) {
726                                 if (chkftn(d1->dfund2->dfun)) {
727                                         werror("illegal function "
728                                             "pointer combination");
729                                         return;
730                                 }
731                                 ++d1;
732                                 ++d2;
ragge
1.52
733                         } else
734                                 break;
ragge
1.47
735                         t1 = DECREF(t1);
736                         t2 = DECREF(t2);
737                 }
738                 werror("illegal pointer combination");
ragge
1.1
739         }
ragge
1.5
740 }
ragge
1.1
741
742 NODE *
ragge
1.10
743 stref(NODE *p)
744 {
ragge
1.41
745         NODE *r;
ragge
1.26
746         struct suedef *sue;
ragge
1.29
747         union dimfun *d;
ragge
1.1
748         TWORD t;
ragge
1.49
749         int dsc;
ragge
1.1
750         OFFSZ off;
751         register struct symtab *q;
752
753         /* make p->x */
754         /* this is also used to reference automatic variables */
755
ragge
1.23
756         q = p->n_right->n_sp;
ragge
1.41
757         nfree(p->n_right);
758         r = p->n_left;
759         nfree(p);
760         p = pconvert(r);
ragge
1.1
761
762         /* make p look like ptr to x */
763
ragge
1.22
764         if (!ISPTR(p->n_type))
765                 p->n_type = PTR+UNIONTY;
ragge
1.1
766
ragge
1.10
767         t = INCREF(q->stype);
ragge
1.29
768         d = q->sdf;
ragge
1.26
769         sue = q->ssue;
ragge
1.1
770
ragge
1.26
771         p = makety(ptdsue);
ragge
1.1
772
773         /* compute the offset to be added */
774
ragge
1.23
775         off = q->soffset;
ragge
1.1
776         dsc = q->sclass;
777
ragge
1.49
778         if (dsc & FIELD) {  /* make fields look like ints */
779                 off = (off/ALINT)*ALINT;
ragge
1.26
780                 sue = MKSUE(INT);
ragge
1.10
781         }
782         if (off != 0)
ragge
1.26
783                 p = clocal(block(PLUSpoffcon(offtdsue), tdsue));
ragge
1.1
784
ragge
1.10
785         p = buildtree(UNARY MULpNIL);
ragge
1.1
786
787         /* if field, build field info */
788
ragge
1.10
789         if (dsc & FIELD) {
ragge
1.26
790                 p = block(FLDpNILq->stype0q->ssue);
ragge
1.49
791                 p->n_rval = PKFIELD(dsc&FLDSIZq->soffset%ALINT);
ragge
1.10
792         }
ragge
1.1
793
ragge
1.19
794         p = clocal(p);
795         return p;
ragge
1.10
796 }
ragge
1.1
797
ragge
1.2
798 int
ragge
1.1
799 notlval(pregister NODE *p; {
800
801         /* return 0 if p an lvalue, 1 otherwise */
802
803         again:
804
ragge
1.22
805         switchp->n_op ){
ragge
1.1
806
807         case FLD:
ragge
1.22
808                 p = p->n_left;
ragge
1.1
809                 goto again;
810
811         case NAME:
812         case OREG:
ragge
1.49
813         case UNARY MUL:
ragge
1.22
814                 ifISARY(p->n_type) || ISFTN(p->n_type) ) return(1);
ragge
1.55
815         case TEMP:
ragge
1.1
816         case REG:
817                 return(0);
818
819         default:
820                 return(1);
821
822                 }
823
824         }
ragge
1.23
825 /* make a constant node with value i */
ragge
1.1
826 NODE *
ragge
1.23
827 bcon(int i)
828 {
ragge
1.1
829         register NODE *p;
830
ragge
1.26
831         p = block(ICONNILNILINT0MKSUE(INT));
ragge
1.22
832         p->n_lval = i;
ragge
1.23
833         p->n_sp = NULL;
834         return(clocal(p));
835 }
ragge
1.1
836
837 NODE *
ragge
1.10
838 bpsize(NODE *p)
839 {
ragge
1.29
840         return(offcon(psize(p), p->n_typep->n_dfp->n_sue));
ragge
1.10
841 }
ragge
1.1
842
ragge
1.10
843 /*
844  * p is a node of type pointer; psize returns the
845  * size of the thing pointed to
846  */
ragge
1.1
847 OFFSZ
ragge
1.10
848 psize(NODE *p)
849 {
850
ragge
1.22
851         if (!ISPTR(p->n_type)) {
ragge
1.10
852                 uerror("pointer required");
853                 return(SZINT);
854         }
ragge
1.1
855         /* note: no pointers to fields */
ragge
1.29
856         return(tsize(DECREF(p->n_type), p->n_dfp->n_sue));
ragge
1.10
857 }
ragge
1.1
858
ragge