Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20060616093031

Diff

Diff from 1.149 to:

Annotations

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

Annotated File View

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