Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20090110145016

Diff

Diff from 1.225 to:

Annotations

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

Annotated File View

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