Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:gmcgarry:20080627043719

Diff

Diff from 1.201 to:

Annotations

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

Annotated File View

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