Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20081214211524

Diff

Diff from 1.223 to:

Annotations

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

Annotated File View

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