Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20050905170716

Diff

Diff from 1.139 to:

Annotations

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

Annotated File View

ragge
1.139
1 /*      $Id: trees.c,v 1.139 2005/09/05 17:07:16 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.94
487                                 p = buildtree(UMULpNIL);
ragge
1.1
488
489                                 }
490                         break;
491
492                 default:
493                         cerror"other code %d"o );
494                         }
495
496                 }
497
ragge
1.11
498         /*
499          * Allow (void)0 casts.
500          * XXX - anything on the right side must be possible to cast.
501          * XXX - remove void types further on.
502          */
ragge
1.50
503         if (p->n_op == CAST && p->n_type == VOID &&
ragge
1.22
504             p->n_right->n_op == ICON)
ragge
1.50
505                 p->n_right->n_type = VOID;
ragge
1.11
506
ragge
1.68
507         if (actions & CVTO)
508                 p = oconvert(p);
ragge
1.1
509         p = clocal(p);
510
ragge
1.30
511 #ifdef PCC_DEBUG
ragge
1.74
512         if (bdebug) {
513                 printf("End of buildtree:\n");
ragge
1.68
514                 fwalk(peprint0);
ragge
1.74
515         }
ragge
1.30
516 #endif
ragge
1.1
517
518         return(p);
519
520         }
521
ragge
1.107
522 /*
523  * Do a conditional branch.
524  */
525 void
526 cbranch(NODE *pNODE *q)
527 {
528         p = buildtree(CBRANCHpq);
529         if (p->n_left->n_op == ICON) {
530                 if (p->n_left->n_lval != 0)
531                         branch(q->n_lval); /* branch always */
532                 tfree(p);
533                 tfree(q);
534                 return;
535         }
536         ecomp(p);
537 }
538
ragge
1.1
539 NODE *
ragge
1.45
540 strargsp ) register NODE *p;  { /* rewrite structure flavored arguments */
ragge
1.1
541
ragge
1.45
542         ifp->n_op == CM ){
543                 p->n_left = strargsp->n_left );
544                 p->n_right = strargsp->n_right );
ragge
1.1
545                 returnp );
546                 }
547
ragge
1.22
548         ifp->n_type == STRTY || p->n_type == UNIONTY ){
ragge
1.29
549                 p = block(STARGpNILp->n_typep->n_dfp->n_sue);
ragge
1.93
550                 p->n_left = buildtreeADDROFp->n_leftNIL );
ragge
1.1
551                 p = clocal(p);
552                 }
553         returnp );
ragge
1.2
554 }
ragge
1.1
555
ragge
1.2
556 /*
557  * apply the op o to the lval part of p; if binary, rhs is val
558  */
559 int
560 conval(NODE *pint oNODE *q)
561 {
ragge
1.1
562         int iu;
563         CONSZ val;
564
ragge
1.22
565         val = q->n_lval;
566         u = ISUNSIGNED(p->n_type) || ISUNSIGNED(q->n_type);
ragge
1.1
567         ifu && (o==LE||o==LT||o==GE||o==GT)) o += (UGE-GE);
568
ragge
1.23
569         if (p->n_sp != NULL && q->n_sp != NULL)
570                 return(0);
571         if (q->n_sp != NULL && o != PLUS)
572                 return(0);
573         if (p->n_sp != NULL && o != PLUS && o != MINUS)
574                 return(0);
ragge
1.1
575
576         switcho ){
577
578         case PLUS:
ragge
1.22
579                 p->n_lval += val;
ragge
1.23
580                 if (p->n_sp == NULL) {
ragge
1.22
581                         p->n_rval = q->n_rval;
582                         p->n_type = q->n_type;
ragge
1.23
583                 }
ragge
1.1
584                 break;
585         case MINUS:
ragge
1.22
586                 p->n_lval -= val;
ragge
1.1
587                 break;
588         case MUL:
ragge
1.22
589                 p->n_lval *= val;
ragge
1.1
590                 break;
591         case DIV:
ragge
1.46
592                 if (val == 0)
593                         uerror("division by 0");
594                 else 
595                         p->n_lval /= val;
ragge
1.1
596                 break;
597         case MOD:
ragge
1.46
598                 if (val == 0)
599                         uerror("division by 0");
600                 else 
601                         p->n_lval %= val;
ragge
1.1
602                 break;
603         case AND:
ragge
1.22
604                 p->n_lval &= val;
ragge
1.1
605                 break;
606         case OR:
ragge
1.22
607                 p->n_lval |= val;
ragge
1.1
608                 break;
609         case ER:
ragge
1.22
610                 p->n_lval ^= val;
ragge
1.1
611                 break;
612         case LS:
613                 i = val;
ragge
1.22
614                 p->n_lval = p->n_lval << i;
ragge
1.1
615                 break;
616         case RS:
617                 i = val;
ragge
1.46
618                 p->n_lval = p->n_lval >> i;
ragge
1.1
619                 break;
620
ragge
1.94
621         case UMINUS:
ragge
1.22
622                 p->n_lval = - p->n_lval;
ragge
1.1
623                 break;
624         case COMPL:
ragge
1.22
625                 p->n_lval = ~p->n_lval;
ragge
1.1
626                 break;
627         case NOT:
ragge
1.22
628                 p->n_lval = !p->n_lval;
ragge
1.1
629                 break;
630         case LT:
ragge
1.22
631                 p->n_lval = p->n_lval < val;
ragge
1.1
632                 break;
633         case LE:
ragge
1.22
634                 p->n_lval = p->n_lval <= val;
ragge
1.1
635                 break;
636         case GT:
ragge
1.22
637                 p->n_lval = p->n_lval > val;
ragge
1.1
638                 break;
639         case GE:
ragge
1.22
640                 p->n_lval = p->n_lval >= val;
ragge
1.1
641                 break;
642         case ULT:
ragge
1.46
643                 p->n_lval = (p->n_lval-val)<0;
ragge
1.1
644                 break;
645         case ULE:
ragge
1.46
646                 p->n_lval = (p->n_lval-val)<=0;
ragge
1.1
647                 break;
648         case UGT:
ragge
1.46
649                 p->n_lval = (p->n_lval-val)>=0;
ragge
1.1
650                 break;
651         case UGE:
ragge
1.46
652                 p->n_lval = (p->n_lval-val)>0;
ragge
1.1
653                 break;
654         case EQ:
ragge
1.22
655                 p->n_lval = p->n_lval == val;
ragge
1.1
656                 break;
657         case NE:
ragge
1.22
658                 p->n_lval = p->n_lval != val;
ragge
1.1
659                 break;
660         default:
661                 return(0);
662                 }
663         return(1);
664         }
665
ragge
1.7
666 /*
667  * Checks p for the existance of a pun.  This is called when the op of p
668  * is ASSIGN, RETURN, CAST, COLON, or relational.
669  * One case is when enumerations are used: this applies only to lint.
670  * In the other case, one operand is a pointer, the other integer type
671  * we check that this integer is in fact a constant zero...
672  * in the case of ASSIGN, any assignment of pointer to integer is illegal
673  * this falls out, because the LHS is never 0.
674  */
ragge
1.2
675 void
ragge
1.7
676 chkpun(NODE *p)
677 {
ragge
1.29
678         union dimfun *d1, *d2;
ragge
1.2
679         NODE *q;
ragge
1.47
680         int t1t2;
ragge
1.1
681
ragge
1.22
682         t1 = p->n_left->n_type;
683         t2 = p->n_right->n_type;
ragge
1.1
684
ragge
1.56
685         switch (p->n_op) {
686         case RETURN:
687                 /* return of void allowed but nothing else */
ragge
1.52
688                 if (t1 == VOID && t2 == VOID)
689                         return;
690                 if (t1 == VOID)
ragge
1.113
691                         return werror("returning value from void function");
ragge
1.52
692                 if (t2 == VOID)
693                         return uerror("using void value");
ragge
1.56
694         case COLON:
695                 if (t1 == VOID && t2 == VOID)
696                         return;
697                 break;
698         default:
699                 if ((t1 == VOID && t2 != VOID) || (t1 != VOID && t2 == VOID))
700                         return uerror("value of void expression used");
701                 break;
ragge
1.52
702         }
703
704         /* allow void pointer assignments in any direction */
705         if (BTYPE(t1) == VOID && (t2 & TMASK))
706                 return;
707         if (BTYPE(t2) == VOID && (t1 & TMASK))
708                 return;
709
ragge
1.111
710 #ifdef notdef
711         /* C99 says that enums always should be handled as ints */
ragge
1.7
712         /* check for enumerations */
713         if (t1==ENUMTY || t2==ENUMTY) {
ragge
1.58
714                 ifclogopp->n_op ) && p->n_op != EQ && p->n_op != NE ) {
ragge
1.79
715                         werror"comparison of enums" );
ragge
1.47
716                         return;
717                         }
ragge
1.7
718                 if (t1==ENUMTY && t2==ENUMTY) {
ragge
1.26
719                         if (p->n_left->n_sue!=p->n_right->n_sue)
ragge
1.7
720                                 werror("enumeration type clash, "
ragge
1.58
721                                     "operator %s"copst(p->n_op));
ragge
1.1
722                         return;
723                 }
ragge
1.87
724                 if ((t1 == ENUMTY && t2 <= BTMASK) ||
725                     (t2 == ENUMTY && t1 <= BTMASK))
726                         return;
ragge
1.7
727         }
ragge
1.111
728 #endif
ragge
1.1
729
ragge
1.52
730         if (ISPTR(t1) || ISARY(t1))
731                 q = p->n_right;
732         else
733                 q = p->n_left;
ragge
1.1
734
ragge
1.52
735         if (!ISPTR(q->n_type) && !ISARY(q->n_type)) {
ragge
1.22
736                 if (q->n_op != ICON || q->n_lval != 0)
ragge
1.47
737                         werror("illegal combination of pointer and integer");
738         } else {
739                 d1 = p->n_left->n_df;
740                 d2 = p->n_right->n_df;
ragge
1.52
741                 if (t1 == t2) {
742                         if (p->n_left->n_sue != p->n_right->n_sue)
743                                 werror("illegal structure pointer combination");
744                         return;
745                 }
ragge
1.47
746                 for (;;) {
ragge
1.52
747                         if (ISARY(t1) || ISPTR(t1)) {
748                                 if (!ISARY(t2) && !ISPTR(t2))
749                                         break;
ragge
1.47
750                                 if (ISARY(t1) && ISARY(t2) && d1->ddim != d2->ddim) {
751                                         werror("illegal array size combination");
752                                         return;
753                                 }
ragge
1.52
754                                 if (ISARY(t1))
755                                         ++d1;
756                                 if (ISARY(t2))
757                                         ++d2;
ragge
1.47
758                         } else if (ISFTN(t1)) {
759                                 if (chkftn(d1->dfund2->dfun)) {
760                                         werror("illegal function "
761                                             "pointer combination");
762                                         return;
763                                 }
764                                 ++d1;
765                                 ++d2;
ragge
1.52
766                         } else
767                                 break;
ragge
1.47
768                         t1 = DECREF(t1);
769                         t2 = DECREF(t2);
770                 }
771                 werror("illegal pointer combination");
ragge
1.1
772         }
ragge
1.5
773 }
ragge
1.1
774
775 NODE *
ragge
1.10
776 stref(NODE *p)
777 {
ragge
1.41
778         NODE *r;
ragge
1.26
779         struct suedef *sue;
ragge
1.29
780         union dimfun *d;
ragge
1.68
781         TWORD tq;
ragge
1.49
782         int dsc;
ragge
1.1
783         OFFSZ off;
ragge
1.68
784         struct symtab *s;
ragge
1.1
785
786         /* make p->x */
787         /* this is also used to reference automatic variables */
788
ragge
1.68
789         s = p->n_right->n_sp;
ragge
1.41
790         nfree(p->n_right);
791         r = p->n_left;
792         nfree(p);
793         p = pconvert(r);
ragge
1.1
794
795         /* make p look like ptr to x */
796
ragge
1.22
797         if (!ISPTR(p->n_type))
798                 p->n_type = PTR+UNIONTY;
ragge
1.1
799
ragge
1.68
800         t = INCREF(s->stype);
801         q = INCQAL(s->squal);
802         d = s->sdf;
803         sue = s->ssue;
ragge
1.1
804
ragge
1.68
805         p = makety(ptqdsue);
ragge
1.1
806
807         /* compute the offset to be added */
808
ragge
1.68
809         off = s->soffset;
810         dsc = s->sclass;
ragge
1.1
811
ragge
1.49
812         if (dsc & FIELD) {  /* make fields look like ints */
813                 off = (off/ALINT)*ALINT;
ragge
1.26
814                 sue = MKSUE(INT);
ragge
1.10
815         }
ragge
1.68
816         if (off != 0) {
817                 p = block(PLUSpoffcon(offtdsue), tdsue);
818                 p->n_qual = q;
ragge
1.81
819                 p = optim(p);
ragge
1.68
820         }
ragge
1.1
821
ragge
1.94
822         p = buildtree(UMULpNIL);
ragge
1.1
823
824         /* if field, build field info */
825
ragge
1.10
826         if (dsc & FIELD) {
ragge
1.68
827                 p = block(FLDpNILs->stype0s->ssue);
828                 p->n_qual = q;
829                 p->n_rval = PKFIELD(dsc&FLDSIZs->soffset%ALINT);
ragge
1.10
830         }
ragge
1.1
831
ragge
1.19
832         p = clocal(p);
833         return p;
ragge
1.10
834 }
ragge
1.1
835
ragge
1.2
836 int
ragge
1.1
837 notlval(pregister NODE *p; {
838
839         /* return 0 if p an lvalue, 1 otherwise */
840
841         again:
842
ragge
1.22
843         switchp->n_op ){
ragge
1.1
844
845         case FLD:
ragge
1.22
846                 p = p->n_left;
ragge
1.1
847                 goto again;
848
849         case NAME:
850         case OREG:
ragge
1.94
851         case UMUL:
ragge
1.22
852                 ifISARY(p->n_type) || ISFTN(p->n_type) ) return(1);
ragge
1.129
853         case TEMP:
ragge
1.1
854         case REG:
855                 return(0);
856
857         default:
858                 return(1);
859