Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20080620131903

Diff

Diff from 1.199 to:

Annotations

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

Annotated File View

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