Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20071004165430

Diff

Diff from 1.166 to:

Annotations

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

Annotated File View

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