Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20090521110740

Diff

Diff from 1.236 to:

Annotations

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

Annotated File View

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