Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20040905114414

Diff

Diff from 1.116 to:

Annotations

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

Annotated File View

ragge
1.116
1 /*      $Id: trees.c,v 1.116 2004/09/05 11:44:14 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;
ragge
1.115
290 #ifdef GCC_COMPAT
291                         /* Get a label name */
292                         if (sp->sflags == SLBLNAME) {
293                                 p->n_type = VOID;
294                                 p->n_sue = MKSUE(VOID);
295                                 p->n_lval = 0;
296                                 p->n_sp = sp;
297                                 break;
298                         } else
299 #endif
ragge
1.23
300                         if (sp->stype == UNDEF) {
301                                 uerror("%s undefined"sp->sname);
ragge
1.1
302                                 /* make p look reasonable */
ragge
1.26
303                                 p->n_type = INT;
304                                 p->n_sue = MKSUE(INT);
ragge
1.29
305                                 p->n_df = NULL;
ragge
1.23
306                                 p->n_sp = sp;
ragge
1.22
307                                 p->n_lval = 0;
ragge
1.23
308                                 defid(pSNULL);
ragge
1.1
309                                 break;
ragge
1.23
310                         }
ragge
1.22
311                         p->n_type = sp->stype;
ragge
1.66
312                         p->n_qual = sp->squal;
ragge
1.29
313                         p->n_df = sp->sdf;
ragge
1.26
314                         p->n_sue = sp->ssue;
ragge
1.22
315                         p->n_lval = 0;
ragge
1.23
316                         p->n_sp = sp;
ragge
1.1
317                         /* special case: MOETY is really an ICON... */
ragge
1.23
318                         if (p->n_type == MOETY) {
319                                 p->n_sp = NULL;
320                                 p->n_lval = sp->soffset;
ragge
1.29
321                                 p->n_df = NULL;
ragge
1.22
322                                 p->n_type = ENUMTY;
323                                 p->n_op = ICON;
ragge
1.23
324                         }
ragge
1.1
325                         break;
326
327                 case STRING:
ragge
1.22
328                         p->n_op = NAME;
ragge
1.17
329 #ifdef CHAR_UNSIGNED
ragge
1.22
330                         p->n_type = UCHAR+ARY;
ragge
1.26
331                         p->n_sue = MKSUE(UCHAR);
ragge
1.17
332 #else
ragge
1.22
333                         p->n_type = CHAR+ARY;
ragge
1.26
334                         p->n_sue = MKSUE(CHAR);
ragge
1.17
335 #endif
ragge
1.22
336                         p->n_lval = 0;
ragge
1.23
337                         p->n_sp = NULL;
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.1
386                         case NAME:
ragge
1.68
387                                 p->n_type = INCREF(l->n_type);
388                                 p->n_qual = INCQAL(l->n_qual);
ragge
1.29
389                                 p->n_df = l->n_df;
ragge
1.26
390                                 p->n_sue = l->n_sue;
ragge
1.1
391                                 break;
392
393                         case COMOP:
ragge
1.41
394                                 nfree(p);
ragge
1.93
395                                 lr = buildtree(ADDROFl->n_rightNIL);
ragge
1.22
396                                 p = buildtreeCOMOPl->n_leftlr );
ragge
1.41
397                                 nfree(l);
ragge
1.1
398                                 break;
399
400                         case QUEST:
ragge
1.93
401                                 lr = buildtreeADDROFl->n_right->n_rightNIL );
402                                 ll = buildtreeADDROFl->n_right->n_leftNIL );
ragge
1.41
403                                 nfree(p); nfree(l->n_right);
ragge
1.22
404                                 p = buildtreeQUESTl->n_leftbuildtreeCOLONlllr ) );
ragge
1.41
405                                 nfree(l);
ragge
1.1
406                                 break;
407
408                         default:
ragge
1.22
409                                 uerror("unacceptable operand of &: %d"l->n_op );
ragge
1.1
410                                 break;
411                                 }
412                         break;
413
414                 case LS:
ragge
1.50
415                 case RS:
ragge
1.95
416                 case LSEQ:
417                 case RSEQ:
ragge
1.50
418                         if(tsize(p->n_right->n_typep->n_right->n_dfp->n_right->n_sue) > SZINT)
ragge
1.68
419                                 p->n_right = r = makety(rINT00MKSUE(INT));
ragge
1.1
420                         break;
421
422                 case RETURN:
423                 case ASSIGN:
424                 case CAST:
425                         /* structure assignment */
426                         /* take the addresses of the two sides; then make an
ragge
1.2
427                          * operator using STASG and
428                          * the addresses of left and right */
ragge
1.1
429
430                         {
ragge
1.26
431                                 struct suedef *sue;
ragge
1.2
432                                 TWORD t;
ragge
1.29
433                                 union dimfun *d;
ragge
1.1
434
ragge
1.26
435                                 if (l->n_sue != r->n_sue)
436                                         uerror("assignment of different structures");
ragge
1.1
437
ragge
1.93
438                                 r = buildtree(ADDROFrNIL);
ragge
1.22
439                                 t = r->n_type;
ragge
1.29
440                                 d = r->n_df;
ragge
1.26
441                                 sue = r->n_sue;
ragge
1.1
442
ragge
1.28
443                                 l = block(STASGlrtdsue);
ragge
1.1
444
445                                 ifo == RETURN ){
ragge
1.41
446                                         nfree(p);
ragge
1.1
447                                         p = l;
448                                         break;
ragge
1.28
449                                 }
ragge
1.1
450
ragge
1.94
451                                 p->n_op = UMUL;
ragge
1.22
452                                 p->n_left = l;
453                                 p->n_right = NIL;
ragge
1.1
454                                 break;
455                                 }
456                 case COLON:
457                         /* structure colon */
458
ragge
1.26
459                         if (l->n_sue != r->n_sue)
460                                 uerror"type clash in conditional" );
ragge
1.1
461                         break;
462
463                 case CALL:
ragge
1.45
464                         p->n_right = r = strargs(p->n_right);
ragge
1.95
465                 case UCALL:
ragge
1.23
466                         if (!ISPTR(l->n_type))
467                                 uerror("illegal function");
ragge
1.22
468                         p->n_type = DECREF(l->n_type);
ragge
1.23
469                         if (!ISFTN(p->n_type))
470                                 uerror("illegal function");
471                         p->n_type = DECREF(p->n_type);
ragge
1.29
472                         p->n_df = l->n_df;
ragge
1.26
473                         p->n_sue = l->n_sue;
ragge
1.93
474                         if (l->n_op == ADDROF && l->n_left->n_op == NAME &&
ragge
1.23
475                             l->n_left->n_sp != NULL && l->n_left->n_sp != NULL &&
476                             (l->n_left->n_sp->sclass == FORTRAN ||
477                             l->n_left->n_sp->sclass == UFORTRAN)) {
ragge
1.22
478                                 p->n_op += (FORTCALL-CALL);
ragge
1.23
479                         }
480                         if (p->n_type == STRTY || p->n_type == UNIONTY) {
ragge
1.1
481                                 /* function returning structure */
482                                 /*  make function really return ptr to str., with * */
483
ragge
1.22
484                                 p->n_op += STCALL-CALL;
ragge
1.23
485                                 p->n_type = INCREF(p->n_type);
ragge
1.94
486                                 p = buildtree(UMULpNIL);
ragge
1.1
487
488                                 }
489                         break;
490
491                 default:
492                         cerror"other code %d"o );
493                         }
494
495                 }
496
ragge
1.11
497         /*
498          * Allow (void)0 casts.
499          * XXX - anything on the right side must be possible to cast.
500          * XXX - remove void types further on.
501          */
ragge
1.50
502         if (p->n_op == CAST && p->n_type == VOID &&
ragge
1.22
503             p->n_right->n_op == ICON)
ragge
1.50
504                 p->n_right->n_type = VOID;
ragge
1.11
505
ragge
1.68
506         if (actions & CVTO)
507                 p = oconvert(p);
ragge
1.1
508         p = clocal(p);
509
ragge
1.30
510 #ifdef PCC_DEBUG
ragge
1.74
511         if (bdebug) {
512                 printf("End of buildtree:\n");
ragge
1.68
513                 fwalk(peprint0);
ragge
1.74
514         }
ragge
1.30
515 #endif
ragge
1.1
516
517         return(p);
518
519         }
520
ragge
1.107
521 /*
522  * Do a conditional branch.
523  */
524 void
525 cbranch(NODE *pNODE *q)
526 {
527         p = buildtree(CBRANCHpq);
528         if (p->n_left->n_op == ICON) {
529                 if (p->n_left->n_lval != 0)
530                         branch(q->n_lval); /* branch always */
531                 tfree(p);
532                 tfree(q);
533                 return;
534         }
535         ecomp(p);
536 }
537
ragge
1.1
538 NODE *
ragge
1.45
539 strargsp ) register NODE *p;  { /* rewrite structure flavored arguments */
ragge
1.1
540
ragge
1.45
541         ifp->n_op == CM ){
542                 p->n_left = strargsp->n_left );
543                 p->n_right = strargsp->n_right );
ragge
1.1
544                 returnp );
545                 }
546
ragge
1.22
547         ifp->n_type == STRTY || p->n_type == UNIONTY ){
ragge
1.29
548                 p = block(STARGpNILp->n_typep->n_dfp->n_sue);
ragge
1.93
549                 p->n_left = buildtreeADDROFp->n_leftNIL );
ragge
1.1
550                 p = clocal(p);
551                 }
552         returnp );
ragge
1.2
553 }
ragge
1.1
554
ragge
1.2
555 /*
556  * apply the op o to the lval part of p; if binary, rhs is val
557  */
558 int
559 conval(NODE *pint oNODE *q)
560 {
ragge
1.1
561         int iu;
562         CONSZ val;
563
ragge
1.22
564         val = q->n_lval;
565         u = ISUNSIGNED(p->n_type) || ISUNSIGNED(q->n_type);
ragge
1.1
566         ifu && (o==LE||o==LT||o==GE||o==GT)) o += (UGE-GE);
567
ragge
1.23
568         if (p->n_sp != NULL && q->n_sp != NULL)
569                 return(0);
570         if (q->n_sp != NULL && o != PLUS)
571                 return(0);
572         if (p->n_sp != NULL && o != PLUS && o != MINUS)
573                 return(0);
ragge
1.1
574
575         switcho ){
576
577         case PLUS:
ragge
1.22
578                 p->n_lval += val;
ragge
1.23
579                 if (p->n_sp == NULL) {
ragge
1.22
580                         p->n_rval = q->n_rval;
581                         p->n_type = q->n_type;
ragge
1.23
582                 }
ragge
1.1
583                 break;
584         case MINUS:
ragge
1.22
585                 p->n_lval -= val;
ragge
1.1
586                 break;
587         case MUL:
ragge
1.22
588                 p->n_lval *= val;
ragge
1.1
589                 break;
590         case DIV:
ragge
1.46
591                 if (val == 0)
592                         uerror("division by 0");
593                 else 
594                         p->n_lval /= val;
ragge
1.1
595                 break;
596         case MOD:
ragge
1.46
597                 if (val == 0)
598                         uerror("division by 0");
599                 else 
600                         p->n_lval %= val;
ragge
1.1
601                 break;
602         case AND:
ragge
1.22
603                 p->n_lval &= val;
ragge
1.1
604                 break;
605         case OR:
ragge
1.22
606                 p->n_lval |= val;
ragge
1.1
607                 break;
608         case ER:
ragge
1.22
609                 p->n_lval ^= val;
ragge
1.1
610                 break;
611         case LS:
612                 i = val;
ragge
1.22
613                 p->n_lval = p->n_lval << i;
ragge
1.1
614                 break;
615         case RS:
616                 i = val;
ragge
1.46
617                 p->n_lval = p->n_lval >> i;
ragge
1.1
618                 break;
619
ragge
1.94
620         case UMINUS:
ragge
1.22
621                 p->n_lval = - p->n_lval;
ragge
1.1
622                 break;
623         case COMPL:
ragge
1.22
624                 p->n_lval = ~p->n_lval;
ragge
1.1
625                 break;
626         case NOT:
ragge
1.22
627                 p->n_lval = !p->n_lval;
ragge
1.1
628                 break;
629         case LT:
ragge
1.22
630                 p->n_lval = p->n_lval < val;
ragge
1.1
631                 break;
632         case LE:
ragge
1.22
633                 p->n_lval = p->n_lval <= val;
ragge
1.1
634                 break;
635         case GT:
ragge
1.22
636                 p->n_lval = p->n_lval > val;
ragge
1.1
637                 break;
638         case GE:
ragge
1.22
639                 p->n_lval = p->n_lval >= val;
ragge
1.1
640                 break;
641         case ULT:
ragge
1.46
642                 p->n_lval = (p->n_lval-val)<0;
ragge
1.1
643                 break;
644         case ULE:
ragge
1.46
645                 p->n_lval = (p->n_lval-val)<=0;
ragge
1.1
646                 break;
647         case UGT:
ragge
1.46
648                 p->n_lval = (p->n_lval-val)>=0;
ragge
1.1
649                 break;
650         case UGE:
ragge
1.46
651                 p->n_lval = (p->n_lval-val)>0;
ragge
1.1
652                 break;
653         case EQ:
ragge
1.22
654                 p->n_lval = p->n_lval == val;
ragge
1.1
655                 break;
656         case NE:
ragge
1.22
657                 p->n_lval = p->n_lval != val;
ragge
1.1
658                 break;
659         default:
660                 return(0);
661                 }
662         return(1);
663         }
664
ragge
1.7
665 /*
666  * Checks p for the existance of a pun.  This is called when the op of p
667  * is ASSIGN, RETURN, CAST, COLON, or relational.
668  * One case is when enumerations are used: this applies only to lint.
669  * In the other case, one operand is a pointer, the other integer type
670  * we check that this integer is in fact a constant zero...
671  * in the case of ASSIGN, any assignment of pointer to integer is illegal
672  * this falls out, because the LHS is never 0.
673  */
ragge
1.2
674 void
ragge
1.7
675 chkpun(NODE *p)
676 {
ragge
1.29
677         union dimfun *d1, *d2;
ragge
1.2
678         NODE *q;
ragge
1.47
679         int t1t2;
ragge
1.1
680
ragge
1.22
681         t1 = p->n_left->n_type;
682         t2 = p->n_right->n_type;
ragge
1.1
683
ragge
1.56
684         switch (p->n_op) {
685         case RETURN:
686                 /* return of void allowed but nothing else */
ragge
1.52
687                 if (t1 == VOID && t2 == VOID)
688                         return;
689                 if (t1 == VOID)
ragge
1.113
690                         return werror("returning value from void function");
ragge
1.52
691                 if (t2 == VOID)
692                         return uerror("using void value");
ragge
1.56
693         case COLON:
694                 if (t1 == VOID && t2 == VOID)
695                         return;
696                 break;
697         default:
698                 if ((t1 == VOID && t2 != VOID) || (t1 != VOID && t2 == VOID))
699                         return uerror("value of void expression used");
700                 break;
ragge
1.52
701         }
702
703         /* allow void pointer assignments in any direction */
704         if (BTYPE(t1) == VOID && (t2 & TMASK))
705                 return;
706         if (BTYPE(t2) == VOID && (t1 & TMASK))
707                 return;
708
ragge
1.111
709 #ifdef notdef
710         /* C99 says that enums always should be handled as ints */
ragge
1.7
711         /* check for enumerations */
712         if (t1==ENUMTY || t2==ENUMTY) {
ragge
1.58
713                 ifclogopp->n_op ) && p->n_op != EQ && p->n_op != NE ) {
ragge
1.79
714                         werror"comparison of enums" );
ragge
1.47
715                         return;
716                         }
ragge
1.7
717                 if (t1==ENUMTY && t2==ENUMTY) {
ragge
1.26
718                         if (p->n_left->n_sue!=p->n_right->n_sue)
ragge
1.7
719                                 werror("enumeration type clash, "
ragge
1.58
720                                     "operator %s"copst(p->n_op));
ragge
1.1
721                         return;
722                 }
ragge
1.87
723                 if ((t1 == ENUMTY && t2 <= BTMASK) ||
724                     (t2 == ENUMTY && t1 <= BTMASK))
725                         return;
ragge
1.7
726         }
ragge
1.111
727 #endif
ragge
1.1
728
ragge
1.52
729         if (ISPTR(t1) || ISARY(t1))
730                 q = p->n_right;
731         else
732                 q = p->n_left;
ragge
1.1
733
ragge
1.52
734         if (!ISPTR(q->n_type) && !ISARY(q->n_type)) {
ragge
1.22
735                 if (q->n_op != ICON || q->n_lval != 0)
ragge
1.47
736                         werror("illegal combination of pointer and integer");
737         } else {
738                 d1 = p->n_left->n_df;
739                 d2 = p->n_right->n_df;
ragge
1.52
740                 if (t1 == t2) {
741                         if (p->n_left->n_sue != p->n_right->n_sue)
742                                 werror("illegal structure pointer combination");
743                         return;
744                 }
ragge
1.47
745                 for (;;) {
ragge
1.52
746                         if (ISARY(t1) || ISPTR(t1)) {
747                                 if (!ISARY(t2) && !ISPTR(t2))
748                                         break;
ragge
1.47
749                                 if (ISARY(t1) && ISARY(t2) && d1->ddim != d2->ddim) {
750                                         werror("illegal array size combination");
751                                         return;
752                                 }
ragge
1.52
753                                 if (ISARY(t1))
754                                         ++d1;
755                                 if (ISARY(t2))
756                                         ++d2;
ragge
1.47
757                         } else if (ISFTN(t1)) {
758                                 if (chkftn(d1->dfund2->dfun)) {
759                                         werror("illegal function "
760                                             "pointer combination");
761                                         return;
762                                 }
763                                 ++d1;
764                                 ++d2;
ragge
1.52
765                         } else
766                                 break;
ragge
1.47
767                         t1 = DECREF(t1);
768                         t2 = DECREF(t2);
769                 }
770                 werror("illegal pointer combination");
ragge
1.1
771         }
ragge
1.5
772 }
ragge
1.1
773
774 NODE *
ragge
1.10
775 stref(NODE *p)
776 {
ragge
1.41
777         NODE *r;
ragge
1.26
778         struct suedef *sue;
ragge
1.29
779         union dimfun *d;
ragge
1.68
780         TWORD tq;
ragge
1.49
781         int dsc;
ragge
1.1
782         OFFSZ off;
ragge
1.68
783         struct symtab *s;
ragge
1.1
784
785         /* make p->x */
786         /* this is also used to reference automatic variables */
787
ragge
1.68
788         s = p->n_right->n_sp;
ragge
1.41
789         nfree(p->n_right);
790         r = p->n_left;
791         nfree(p);
792         p = pconvert(r);
ragge
1.1
793
794         /* make p look like ptr to x */
795
ragge
1.22
796         if (!ISPTR(p->n_type))
797                 p->n_type = PTR+UNIONTY;
ragge
1.1
798
ragge
1.68
799         t = INCREF(s->stype);
800         q = INCQAL(s->squal);
801         d = s->sdf;
802         sue = s->ssue;
ragge
1.1
803
ragge
1.68
804         p = makety(ptqdsue);
ragge
1.1
805
806         /* compute the offset to be added */
807
ragge
1.68
808         off = s->soffset;
809         dsc = s->sclass;
ragge
1.1
810
ragge
1.49
811         if (dsc & FIELD) {  /* make fields look like ints */
812                 off = (off/ALINT)*ALINT;
ragge
1.26
813                 sue = MKSUE(INT);
ragge
1.10
814         }
ragge
1.68
815         if (off != 0) {
816                 p = block(PLUSpoffcon(offtdsue), tdsue);
817                 p->n_qual = q;
ragge
1.81
818                 p = optim(p);
ragge
1.68
819         }
ragge
1.1
820
ragge
1.94
821         p = buildtree(UMULpNIL);
ragge
1.1
822
823         /* if field, build field info */
824
ragge
1.10
825         if (dsc & FIELD) {
ragge
1.68
826                 p = block(FLDpNILs->stype0s->ssue);
827                 p->n_qual = q;
828                 p->n_rval = PKFIELD(dsc&FLDSIZs->soffset%ALINT);
ragge
1.10
829         }
ragge
1.1
830
ragge
1.19
831         p = clocal(p);
832         return p;
ragge
1.10
833 }
ragge
1.1
834
ragge
1.2
835 int
ragge
1.1
836 notlval(pregister NODE *p; {
837
838         /* return 0 if p an lvalue, 1 otherwise */
839
840         again:
841
ragge
1.22
842         switchp->n_op ){
ragge
1.1
843
844         case FLD:
ragge
1.22
845                 p = p->n_left;
ragge
1.1
846                 goto again;
847
848         case NAME:
849         case OREG:
ragge
1.94
850         case UMUL:
ragge
1.22
851                 ifISARY(p->n_type) || ISFTN(p->n_type) ) return(1);
ragge
1.1
852         case REG:
853