Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20040509182657

Diff

Diff from 1.100 to:

Annotations

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

Annotated File View

ragge
1.100
1 /*      $Id: trees.c,v 1.100 2004/05/09 18:26:58 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
ragge
1.77
80 int lastloc = -1;
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:
ragge
1.94
135                 case UMINUS:
ragge
1.1
136                 case COMPL:
137                         ifconvallol ) ) return(l);
138                         break;
139                 }
ragge
1.71
140         } else if (o == NOT && l->n_op == FCON) {
141                 l = clocal(block(SCONVlNILINT0MKSUE(INT)));
ragge
1.94
142         } else ifo == UMINUS && l->n_op == FCON ){
ragge
1.71
143                         l->n_dcon = -l->n_dcon;
ragge
1.1
144                         return(l);
145
ragge
1.44
146         } else ifo==QUEST && l->n_op==ICON ) {
ragge
1.41
147                 CONSZ c = l->n_lval;
148                 nfree(l);
149                 if (c) {
150                         tfree(r->n_right);
151                         l = r->n_left;
152                         nfree(r);
153                         return(l);
154                 } else {
155                         tfree(r->n_left);
156                         l = r->n_right;
157                         nfree(r);
158                         return(l);
ragge
1.1
159                 }
ragge
1.44
160         } else if( (o==ANDAND || o==OROR) && (l->n_op==ICON||r->n_op==ICON) )
161                 goto ccwarn;
ragge
1.1
162
ragge
1.22
163         else ifopty == BITYPE && l->n_op == ICON && r->n_op == ICON ){
ragge
1.1
164
165                 switcho ){
166
167                 case ULT:
168                 case UGT:
169                 case ULE:
170                 case UGE:
171                 case LT:
172                 case GT:
173                 case LE:
174                 case GE:
175                 case EQ:
ragge
1.48
176                 case NE:
ragge
1.1
177                 case ANDAND:
178                 case OROR:
179
180                 ccwarn:
181
182                 case PLUS:
183                 case MINUS:
184                 case MUL:
185                 case DIV:
186                 case MOD:
187                 case AND:
188                 case OR:
189                 case ER:
190                 case LS:
191                 case RS:
192                         ifconvallor ) ) {
ragge
1.41
193                                 nfree(r);
ragge
1.1
194                                 return(l);
ragge
1.44
195                         }
ragge
1.1
196                         break;
197                 }
ragge
1.44
198         } else if (opty == BITYPE && (l->n_op == FCON || l->n_op == ICON) &&
199             (r->n_op == FCON || r->n_op == ICON) && (o == PLUS || o == MINUS ||
200             o == MUL || o == DIV)) {
201                 switch(o){
202                 case PLUS:
203                 case MINUS:
204                 case MUL:
205                 case DIV:
206                         if (l->n_op == ICON)
207                                 l->n_dcon = l->n_lval;
208                         if (r->n_op == ICON)
209                                 r->n_dcon = r->n_lval;
210                         switch (o) {
211                         case PLUS:
212                                 l->n_dcon += r->n_dconbreak;
213                         case MINUS:
214                                 l->n_dcon -= r->n_dconbreak;
215                         case MUL:
216                                 l->n_dcon *= r->n_dconbreak;
217                         case DIV:
218                                 if (r->n_dcon == 0)
219                                         uerror("division by 0.");
220                                 else
221                                         l->n_dcon /= r->n_dcon;
222                         }
223                         l->n_op = FCON;
224                         l->n_type = DOUBLE;
225                         l->n_sue = MKSUE(DOUBLE);
226                         nfree(r);
227                         return(l);
ragge
1.1
228                 }
ragge
1.44
229         }
ragge
1.1
230
ragge
1.30
231         /* its real; we must make a new node */
ragge
1.1
232
ragge
1.26
233         p = block(olrINT0MKSUE(INT));
ragge
1.1
234
235         actions = opact(p);
236
ragge
1.66
237         if (actions & LVAL) { /* check left descendent */
238                 if (notlval(p->n_left)) {
239                         uerror("lvalue required");
ragge
1.68
240 #ifdef notyet
ragge
1.67
241                 } else {
242                         if ((l->n_type > BTMASK && ISCON(l->n_qual)) ||
243                             (l->n_type <= BTMASK && ISCON(l->n_qual << TSHIFT)))
244                                 if (blevel > 0)
245                                         uerror("lvalue is declared const");
ragge
1.68
246 #endif
ragge
1.67
247                 }
ragge
1.66
248         }
ragge
1.1
249
250         ifactions & NCVTR ){
ragge
1.22
251                 p->n_left = pconvertp->n_left );
ragge
1.1
252                 }
253         else if( !(actions & NCVT ) ){
254                 switchopty ){
255
256                 case BITYPE:
ragge
1.22
257                         p->n_right = pconvertp->n_right );
ragge
1.1
258                 case UTYPE:
ragge
1.22
259                         p->n_left = pconvertp->n_left );
ragge
1.1
260
261                         }
262                 }
263
ragge
1.64
264         if ((actions&PUN) && (o!=CAST))
ragge
1.1
265                 chkpun(p);
266
267         ifactions & (TYPL|TYPR) ){
268
ragge
1.22
269                 q = (actions&TYPL) ? p->n_left : p->n_right;
ragge
1.1
270
ragge
1.22
271                 p->n_type = q->n_type;
ragge
1.66
272                 p->n_qual = q->n_qual;
ragge
1.29
273                 p->n_df = q->n_df;
ragge
1.26
274                 p->n_sue = q->n_sue;
ragge
1.1
275                 }
276
277         ifactions & CVTL ) p = convertpCVTL );
278         ifactions & CVTR ) p = convertpCVTR );
279         ifactions & TYMATCH ) p = tymatch(p);
280         ifactions & PTMATCH ) p = ptmatch(p);
281
282         ifactions & OTHER ){
ragge
1.22
283                 l = p->n_left;
284                 r = p->n_right;
ragge
1.1
285
286                 switch(o){
287
288                 case NAME:
ragge
1.23
289                         sp = spname;
290                         if (sp->stype == UNDEF) {
291                                 uerror("%s undefined"sp->sname);
ragge
1.1
292                                 /* make p look reasonable */
ragge
1.26
293                                 p->n_type = INT;
294                                 p->n_sue = MKSUE(INT);
ragge
1.29
295                                 p->n_df = NULL;
ragge
1.23
296                                 p->n_sp = sp;
ragge
1.22
297                                 p->n_lval = 0;
ragge
1.23
298                                 defid(pSNULL);
ragge
1.1
299                                 break;
ragge
1.23
300                         }
ragge
1.22
301                         p->n_type = sp->stype;
ragge
1.66
302                         p->n_qual = sp->squal;
ragge
1.29
303                         p->n_df = sp->sdf;
ragge
1.26
304                         p->n_sue = sp->ssue;
ragge
1.22
305                         p->n_lval = 0;
ragge
1.23
306                         p->n_sp = sp;
ragge
1.1
307                         /* special case: MOETY is really an ICON... */
ragge
1.23
308                         if (p->n_type == MOETY) {
309                                 p->n_sp = NULL;
310                                 p->n_lval = sp->soffset;
ragge
1.29
311                                 p->n_df = NULL;
ragge
1.22
312                                 p->n_type = ENUMTY;
313                                 p->n_op = ICON;
ragge
1.23
314                         }
ragge
1.89
315                         if (sp->sflags & STNODE) {
316                                 p->n_lval = sp->soffset;
317                                 p->n_op = TEMP;
318                         }
ragge
1.1
319                         break;
320
321                 case STRING:
ragge
1.22
322                         p->n_op = NAME;
ragge
1.17
323 #ifdef CHAR_UNSIGNED
ragge
1.22
324                         p->n_type = UCHAR+ARY;
ragge
1.26
325                         p->n_sue = MKSUE(UCHAR);
ragge
1.17
326 #else
ragge
1.22
327                         p->n_type = CHAR+ARY;
ragge
1.26
328                         p->n_sue = MKSUE(CHAR);
ragge
1.17
329 #endif
ragge
1.22
330                         p->n_lval = 0;
ragge
1.23
331                         p->n_sp = NULL;
ragge
1.1
332                         break;
333
334                 case STREF:
335                         /* p->x turned into *(p+offset) */
336                         /* rhs must be a name; check correctness */
337
ragge
1.23
338                         /* Find member symbol struct */
339                         if (l->n_type != PTR+STRTY && l->n_type != PTR+UNIONTY){
340                                 uerror("struct or union required");
341                                 break;
342                         }
343
ragge
1.26
344                         if ((elem = l->n_sue->suelem) == NULL)
ragge
1.23
345                                 uerror("undefined struct or union");
346
347                         name = r->n_name;
ragge
1.26
348                         for (; *elem != NULLelem++) {
349                                 sp = *elem;
ragge
1.23
350                                 if (sp->sname == name)
351                                         break;
352                         }
ragge
1.26
353                         if (*elem == NULL)
ragge
1.23
354                                 uerror("member '%s' not declared"name);
355
356                         r->n_sp = sp;
357                         p = stref(p);
358                         break;
359
ragge
1.94
360                 case UMUL:
ragge
1.93
361                         if (l->n_op == ADDROF) {
ragge
1.41
362                                 nfree(p);
ragge
1.22
363                                 p = l->n_left;
ragge
1.41
364                                 nfree(l);
365                         }
ragge
1.22
366                         if( !ISPTR(l->n_type))uerror("illegal indirection");
367                         p->n_type = DECREF(l->n_type);
ragge
1.67
368                         p->n_qual = DECREF(l->n_qual);
ragge
1.29
369                         p->n_df = l->n_df;
ragge
1.26
370                         p->n_sue = l->n_sue;
ragge
1.1
371                         break;
372
ragge
1.93
373                 case ADDROF:
ragge
1.22
374                         switchl->n_op ){
ragge
1.1
375
ragge
1.94
376                         case UMUL:
ragge
1.41
377                                 nfree(p);
ragge
1.22
378                                 p = l->n_left;
ragge
1.41
379                                 nfree(l);
ragge
1.100
380                         case TEMP:
ragge
1.1
381                         case NAME:
ragge
1.68
382                                 p->n_type = INCREF(l->n_type);
383                                 p->n_qual = INCQAL(l->n_qual);
ragge
1.29
384                                 p->n_df = l->n_df;
ragge
1.26
385                                 p->n_sue = l->n_sue;
ragge
1.1
386                                 break;
387
388                         case COMOP:
ragge
1.41
389                                 nfree(p);
ragge
1.93
390                                 lr = buildtree(ADDROFl->n_rightNIL);
ragge
1.22
391                                 p = buildtreeCOMOPl->n_leftlr );
ragge
1.41
392                                 nfree(l);
ragge
1.1
393                                 break;
394
395                         case QUEST:
ragge
1.93
396                                 lr = buildtreeADDROFl->n_right->n_rightNIL );
397                                 ll = buildtreeADDROFl->n_right->n_leftNIL );
ragge
1.41
398                                 nfree(p); nfree(l->n_right);
ragge
1.22
399                                 p = buildtreeQUESTl->n_leftbuildtreeCOLONlllr ) );
ragge
1.41
400                                 nfree(l);
ragge
1.1
401                                 break;
402
403                         default:
ragge
1.22
404                                 uerror("unacceptable operand of &: %d"l->n_op );
ragge
1.1
405                                 break;
406                                 }
407                         break;
408
409                 case LS:
ragge
1.50
410                 case RS:
ragge
1.95
411                 case LSEQ:
412                 case RSEQ:
ragge
1.50
413                         if(tsize(p->n_right->n_typep->n_right->n_dfp->n_right->n_sue) > SZINT)
ragge
1.68
414                                 p->n_right = r = makety(rINT00MKSUE(INT));
ragge
1.1
415                         break;
416
417                 case RETURN:
418                 case ASSIGN:
419                 case CAST:
420                         /* structure assignment */
421                         /* take the addresses of the two sides; then make an
ragge
1.2
422                          * operator using STASG and
423                          * the addresses of left and right */
ragge
1.1
424
425                         {
ragge
1.26
426                                 struct suedef *sue;
ragge
1.2
427                                 TWORD t;
ragge
1.29
428                                 union dimfun *d;
ragge
1.1
429
ragge
1.26
430                                 if (l->n_sue != r->n_sue)
431                                         uerror("assignment of different structures");
ragge
1.1
432
ragge
1.93
433                                 r = buildtree(ADDROFrNIL);
ragge
1.22
434                                 t = r->n_type;
ragge
1.29
435                                 d = r->n_df;
ragge
1.26
436                                 sue = r->n_sue;
ragge
1.1
437
ragge
1.28
438                                 l = block(STASGlrtdsue);
ragge
1.1
439
440                                 ifo == RETURN ){
ragge
1.41
441                                         nfree(p);
ragge
1.1
442                                         p = l;
443                                         break;
ragge
1.28
444                                 }
ragge
1.1
445
ragge
1.94
446                                 p->n_op = UMUL;
ragge
1.22
447                                 p->n_left = l;
448                                 p->n_right = NIL;
ragge
1.1
449                                 break;
450                                 }
451                 case COLON:
452                         /* structure colon */
453
ragge
1.26
454                         if (l->n_sue != r->n_sue)
455                                 uerror"type clash in conditional" );
ragge
1.1
456                         break;
457
458                 case CALL:
ragge
1.45
459                         p->n_right = r = strargs(p->n_right);
ragge
1.95
460                 case UCALL:
ragge
1.23
461                         if (!ISPTR(l->n_type))
462                                 uerror("illegal function");
ragge
1.22
463                         p->n_type = DECREF(l->n_type);
ragge
1.23
464                         if (!ISFTN(p->n_type))
465                                 uerror("illegal function");
466                         p->n_type = DECREF(p->n_type);
ragge
1.29
467                         p->n_df = l->n_df;
ragge
1.26
468                         p->n_sue = l->n_sue;
ragge
1.93
469                         if (l->n_op == ADDROF && l->n_left->n_op == NAME &&
ragge
1.23
470                             l->n_left->n_sp != NULL && l->n_left->n_sp != NULL &&
471                             (l->n_left->n_sp->sclass == FORTRAN ||
472                             l->n_left->n_sp->sclass == UFORTRAN)) {
ragge
1.22
473                                 p->n_op += (FORTCALL-CALL);
ragge
1.23
474                         }
475                         if (p->n_type == STRTY || p->n_type == UNIONTY) {
ragge
1.1
476                                 /* function returning structure */
477                                 /*  make function really return ptr to str., with * */
478
ragge
1.22
479                                 p->n_op += STCALL-CALL;
ragge
1.23
480                                 p->n_type = INCREF(p->n_type);
ragge
1.94
481                                 p = buildtree(UMULpNIL);
ragge
1.1
482
483                                 }
484                         break;
485
486                 default:
487                         cerror"other code %d"o );
488                         }
489
490                 }
491
ragge
1.11
492         /*
493          * Allow (void)0 casts.
494          * XXX - anything on the right side must be possible to cast.
495          * XXX - remove void types further on.
496          */
ragge
1.50
497         if (p->n_op == CAST && p->n_type == VOID &&
ragge
1.22
498             p->n_right->n_op == ICON)
ragge
1.50
499                 p->n_right->n_type = VOID;
ragge
1.11
500
ragge
1.68
501         if (actions & CVTO)
502                 p = oconvert(p);
ragge
1.1
503         p = clocal(p);
504
ragge
1.30
505 #ifdef PCC_DEBUG
ragge
1.74
506         if (bdebug) {
507                 printf("End of buildtree:\n");
ragge
1.68
508                 fwalk(peprint0);
ragge
1.74
509         }
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.93
527                 p->n_left = buildtreeADDROFp->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
ragge
1.94
598         case UMINUS:
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.79
690                         werror"comparison of enums" );
ragge
1.47
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.87
699                 if ((t1 == ENUMTY && t2 <= BTMASK) ||
700                     (t2 == ENUMTY && t1 <= BTMASK))
701                         return;
ragge
1.7
702         }
ragge
1.1
703
ragge
1.52
704         if (ISPTR(t1) || ISARY(t1))
705                 q = p->n_right;
706         else
707                 q = p->n_left;
ragge
1.1
708
ragge
1.52
709         if (!ISPTR(q->n_type) && !ISARY(q->n_type)) {
ragge
1.22
710                 if (q->n_op != ICON || q->n_lval != 0)
ragge
1.47
711                         werror("illegal combination of pointer and integer");
712         } else {
713                 d1 = p->n_left->n_df;
714                 d2 = p->n_right->n_df;
ragge
1.52
715                 if (t1 == t2) {
716                         if (p->n_left->n_sue != p->n_right->n_sue)
717                                 werror("illegal structure pointer combination");
718                         return;
719                 }
ragge
1.47
720                 for (;;) {
ragge
1.52
721                         if (ISARY(t1) || ISPTR(t1)) {
722                                 if (!ISARY(t2) && !ISPTR(t2))
723                                         break;
ragge
1.47
724                                 if (ISARY(t1) && ISARY(t2) && d1->ddim != d2->ddim) {
725                                         werror("illegal array size combination");
726                                         return;
727                                 }
ragge
1.52
728                                 if (ISARY(t1))
729                                         ++d1;
730                                 if (ISARY(t2))
731                                         ++d2;
ragge
1.47
732                         } else if (ISFTN(t1)) {
733                                 if (chkftn(d1->dfund2->dfun)) {
734                                         werror("illegal function "
735                                             "pointer combination");
736                                         return;
737                                 }
738                                 ++d1;
739                                 ++d2;
ragge
1.52
740                         } else
741                                 break;
ragge
1.47
742                         t1 = DECREF(t1);
743                         t2 = DECREF(t2);
744                 }
745                 werror("illegal pointer combination");
ragge
1.1
746         }
ragge
1.5
747 }
ragge
1.1
748
749 NODE *
ragge
1.10
750 stref(NODE *p)
751 {
ragge
1.41
752         NODE *r;
ragge
1.26
753         struct suedef *sue;
ragge
1.29
754         union dimfun *d;
ragge
1.68
755         TWORD tq;
ragge
1.49
756         int dsc;
ragge
1.1
757         OFFSZ off;
ragge
1.68
758         struct symtab *s;
ragge
1.1
759
760         /* make p->x */
761         /* this is also used to reference automatic variables */
762
ragge
1.68
763         s = p->n_right->n_sp;
ragge
1.41
764         nfree(p->n_right);
765         r = p->n_left;
766         nfree(p);
767         p = pconvert(r);
ragge
1.1
768
769         /* make p look like ptr to x */
770
ragge
1.22
771         if (!ISPTR(p->n_type))
772                 p->n_type = PTR+UNIONTY;
ragge
1.1
773
ragge
1.68
774         t = INCREF(s->stype);
775         q = INCQAL(s->squal);
776         d = s->sdf;
777         sue = s->ssue;
ragge
1.1
778
ragge
1.68
779         p = makety(ptqdsue);
ragge
1.1
780
781         /* compute the offset to be added */
782
ragge
1.68
783         off = s->soffset;
784         dsc = s->sclass;
ragge
1.1
785
ragge
1.49
786         if (dsc & FIELD) {  /* make fields look like ints */
787                 off = (off/ALINT)*ALINT;
ragge
1.26
788                 sue = MKSUE(INT);
ragge
1.10
789         }
ragge
1.68
790         if (off != 0) {
791                 p = block(PLUSpoffcon(offtdsue), tdsue);
792                 p->n_qual = q;
ragge
1.81
793                 p = optim(p);
ragge
1.68
794         }
ragge
1.1
795
ragge
1.94
796         p = buildtree(UMULpNIL);
ragge
1.1
797
798         /* if field, build field info */
799
ragge
1.10
800         if (dsc & FIELD) {
ragge
1.68
801                 p = block(FLDpNILs->stype0s->ssue);
802                 p->n_qual = q;
803                 p->n_rval = PKFIELD(dsc&FLDSIZs->soffset%ALINT);
ragge
1.10
804         }
ragge
1.1
805
ragge
1.19
806         p = clocal(p);
807         return p;
ragge
1.10
808 }
ragge
1.1
809
ragge
1.2
810 int
ragge
1.1
811 notlval(pregister NODE *p; {
812
813         /* return 0 if p an lvalue, 1 otherwise */
814
815         again:
816
ragge
1.22
817         switchp->n_op ){
ragge
1.1
818
819         case FLD:
ragge
1.22
820                 p = p->n_left;
ragge
1.1
821                 goto again;
822
823         case NAME:
824         case OREG:
ragge
1.94
825         case UMUL:
ragge
1.22
826                 ifISARY(p->n_type) || ISFTN(p->n_type) ) return(1);
ragge
1.55
827         case TEMP:
ragge
1.1
828         case REG:
829                 return(0);
830
831         default:
832                 return(1);
833
834                 }
835
836         }
ragge
1.23
837 /* make a constant node with value i */
ragge
1.1
838 NODE *
ragge
1.23
839 bcon(int i)
840 {
ragge
1.1
841         register NODE *p;
842
ragge
1.26
843         p = block(ICONNILNILINT0MKSUE(INT));
ragge
1.22
844         p->n_lval = i;
ragge
1.23
845         p->n_sp = NULL;
846         return(clocal(p));
847 }
ragge
1.1
848
849 NODE *
ragge
1.10
850 bpsize(NODE *p)
851 {
ragge
1.29
852         return(offcon(psize(p), p->n_type</