Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20030801134444

Diff

Diff from 1.66 to:

Annotations

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

Annotated File View

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