Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20090127171203

Diff

Diff from 1.229 to:

Annotations

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

Annotated File View

ragge
1.229
1 /*      $Id: trees.c,v 1.229 2009/01/27 17:12:03 ragge Exp $    */
ragge
1.51
2 /*
3  * Copyright (c) 2003 Anders Magnusson (ragge@ludd.luth.se).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 /*
30  * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
31  *
32  * Redistribution and use in source and binary forms, with or without
33  * modification, are permitted provided that the following conditions
34  * are met:
35  *
36  * Redistributions of source code and documentation must retain the above
37  * copyright notice, this list of conditions and the following disclaimer.
38  * Redistributions in binary form must reproduce the above copyright
39  * notice, this list of conditionsand the following disclaimer in the
40  * documentation and/or other materials provided with the distribution.
41  * All advertising materials mentioning features or use of this software
42  * must display the following acknowledgement:
43  *      This product includes software developed or owned by Caldera
44  *      International, Inc.
45  * Neither the name of Caldera International, Inc. nor the names of other
46  * contributors may be used to endorse or promote products derived from
47  * this software without specific prior written permission.
48  *
49  * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
50  * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
51  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
52  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
53  * DISCLAIMED.  IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE
54  * FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57  * HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT,
58  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
59  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
60  * POSSIBILITY OF SUCH DAMAGE.
61  */
62 /*
63  * Some of the changes from 32V include:
64  * - Understand "void" as type.
65  * - Handle enums as ints everywhere.
ragge
1.57
66  * - Convert some C-specific ops into branches.
ragge
1.51
67  */
68
ragge
1.1
69 # include "pass1.h"
ragge
1.51
70 # include "pass2.h"
ragge
1.1
71
ragge
1.16
72 # include <stdarg.h>
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.226
302                 struct suedef *sue2;
ragge
1.225
303                 struct suedef *sue;
304
ragge
1.22
305                 l = p->n_left;
306                 r = p->n_right;
ragge
1.1
307
308                 switch(o){
309
310                 case NAME:
ragge
1.203
311                         cerror("buildtree NAME");
ragge
1.1
312
313                 case STREF:
314                         /* p->x turned into *(p+offset) */
315                         /* rhs must be a name; check correctness */
316
ragge
1.23
317                         /* Find member symbol struct */
318                         if (l->n_type != PTR+STRTY && l->n_type != PTR+UNIONTY){
319                                 uerror("struct or union required");
320                                 break;
321                         }
322
ragge
1.225
323 #define GETSUE(x, y)    for (x = y; x->suep; x = x->suep)
324
325                         /* find type sue */
326                         GETSUE(suel->n_sue);
327                         if ((sp = sue->suem) == NULL) {
ragge
1.23
328                                 uerror("undefined struct or union");
stefan
1.190
329                                 break;
330                         }
ragge
1.23
331
ragge
1.218
332                         sp = findmember(spr->n_name);
333 #ifdef notdef
ragge
1.23
334                         name = r->n_name;
ragge
1.187
335                         for (; sp != NULLsp = sp->snext) {
ragge
1.23
336                                 if (sp->sname == name)
337                                         break;
338                         }
ragge
1.218
339 #endif
stefan
1.190
340                         if (sp == NULL) {
ragge
1.218
341                                 uerror("member '%s' not declared"r->n_name);
stefan
1.190
342                                 break;
343                         }
ragge
1.23
344
345                         r->n_sp = sp;
346                         p = stref(p);
347                         break;
348
ragge
1.94
349                 case UMUL:
ragge
1.93
350                         if (l->n_op == ADDROF) {
ragge
1.41
351                                 nfree(p);
ragge
1.22
352                                 p = l->n_left;
ragge
1.41
353                                 nfree(l);
354                         }
ragge
1.22
355                         if( !ISPTR(l->n_type))uerror("illegal indirection");
356                         p->n_type = DECREF(l->n_type);
ragge
1.67
357                         p->n_qual = DECREF(l->n_qual);
ragge
1.29
358                         p->n_df = l->n_df;
ragge
1.26
359                         p->n_sue = l->n_sue;
ragge
1.1
360                         break;
361
ragge
1.93
362                 case ADDROF:
ragge
1.22
363                         switchl->n_op ){
ragge
1.1
364
ragge
1.94
365                         case UMUL:
ragge
1.41
366                                 nfree(p);
ragge
1.22
367                                 p = l->n_left;
ragge
1.41
368                                 nfree(l);
ragge
1.130
369                         case TEMP:
ragge
1.1
370                         case NAME:
ragge
1.68
371                                 p->n_type = INCREF(l->n_type);
372                                 p->n_qual = INCQAL(l->n_qual);
ragge
1.29
373                                 p->n_df = l->n_df;
ragge
1.26
374                                 p->n_sue = l->n_sue;
ragge
1.1
375                                 break;
376
377                         case COMOP:
ragge
1.41
378                                 nfree(p);
ragge
1.93
379                                 lr = buildtree(ADDROFl->n_rightNIL);
ragge
1.22
380                                 p = buildtreeCOMOPl->n_leftlr );
ragge
1.41
381                                 nfree(l);
ragge
1.1
382                                 break;
383
384                         case QUEST:
ragge
1.93
385                                 lr = buildtreeADDROFl->n_right->n_rightNIL );
386                                 ll = buildtreeADDROFl->n_right->n_leftNIL );
ragge
1.41
387                                 nfree(p); nfree(l->n_right);
ragge
1.22
388                                 p = buildtreeQUESTl->n_leftbuildtreeCOLONlllr ) );
ragge
1.41
389                                 nfree(l);
ragge
1.1
390                                 break;
391
392                         default:
ragge
1.22
393                                 uerror("unacceptable operand of &: %d"l->n_op );
ragge
1.1
394                                 break;
395                                 }
396                         break;
397
398                 case LS:
ragge
1.150
399                 case RS/* must make type size at least int... */
400                         if (p->n_type == CHAR || p->n_type == SHORT) {
401                                 p->n_left = makety(lINT00MKSUE(INT));
402                         } else if (p->n_type == UCHAR || p->n_type == USHORT) {
403                                 p->n_left = makety(lUNSIGNED00,
404                                     MKSUE(UNSIGNED));
405                         }
406                         l = p->n_left;
407                         p->n_type = l->n_type;
408                         p->n_qual = l->n_qual;
409                         p->n_df = l->n_df;
410                         p->n_sue = l->n_sue;
411
412                         /* FALLTHROUGH */
ragge
1.95
413                 case LSEQ:
ragge
1.150
414                 case RSEQ/* ...but not for assigned types */
415                         if(tsize(r->n_typer->n_dfr->n_sue) > SZINT)
416                                 p->n_right = makety(rINT00MKSUE(INT));
ragge
1.1
417                         break;
418
419                 case RETURN:
420                 case ASSIGN:
421                 case CAST:
422                         /* structure assignment */
423                         /* take the addresses of the two sides; then make an
ragge
1.2
424                          * operator using STASG and
425                          * the addresses of left and right */
ragge
1.1
426
427                         {
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.226
458                         GETSUE(suel->n_sue);
459                         GETSUE(sue2r->n_sue);
460                         if (sue->suem != sue2->suem)
ragge
1.26
461                                 uerror"type clash in conditional" );
ragge
1.1
462                         break;
463
464                 case CALL:
ragge
1.45
465                         p->n_right = r = strargs(p->n_right);
ragge
1.171
466                         p = funcode(p);
467                         /* FALLTHROUGH */
ragge
1.95
468                 case UCALL:
ragge
1.23
469                         if (!ISPTR(l->n_type))
470                                 uerror("illegal function");
ragge
1.22
471                         p->n_type = DECREF(l->n_type);
ragge
1.23
472                         if (!ISFTN(p->n_type))
473                                 uerror("illegal function");
474                         p->n_type = DECREF(p->n_type);
ragge
1.29
475                         p->n_df = l->n_df;
ragge
1.26
476                         p->n_sue = l->n_sue;
ragge
1.93
477                         if (l->n_op == ADDROF && l->n_left->n_op == NAME &&
ragge
1.23
478                             l->n_left->n_sp != NULL && l->n_left->n_sp != NULL &&
479                             (l->n_left->n_sp->sclass == FORTRAN ||
480                             l->n_left->n_sp->sclass == UFORTRAN)) {
ragge
1.22
481                                 p->n_op += (FORTCALL-CALL);
ragge
1.23
482                         }
483                         if (p->n_type == STRTY || p->n_type == UNIONTY) {
ragge
1.1
484                                 /* function returning structure */
485                                 /*  make function really return ptr to str., with * */
486
ragge
1.22
487                                 p->n_op += STCALL-CALL;
ragge
1.23
488                                 p->n_type = INCREF(p->n_type);
ragge
1.149
489                                 p = clocal(p); /* before recursing */
ragge
1.94
490                                 p = buildtree(UMULpNIL);
ragge
1.1
491
492                                 }
493                         break;
494
495                 default:
496                         cerror"other code %d"o );
497                         }
498
499                 }
500
ragge
1.11
501         /*
502          * Allow (void)0 casts.
503          * XXX - anything on the right side must be possible to cast.
504          * XXX - remove void types further on.
505          */
ragge
1.50
506         if (p->n_op == CAST && p->n_type == VOID &&
ragge
1.22
507             p->n_right->n_op == ICON)
ragge
1.50
508                 p->n_right->n_type = VOID;
ragge
1.11
509
ragge
1.68
510         if (actions & CVTO)
511                 p = oconvert(p);
ragge
1.1
512         p = clocal(p);
513
ragge
1.30
514 #ifdef PCC_DEBUG
ragge
1.74
515         if (bdebug) {
516                 printf("End of buildtree:\n");
ragge
1.68
517                 fwalk(peprint0);
ragge
1.74
518         }
ragge
1.30
519 #endif
ragge
1.1
520
521         return(p);
522
523         }
524
ragge
1.218
525 /* Find a member in a struct or union.  May be an unnamed member */
526 static struct symtab *
527 findmember(struct symtab *spchar *s)
528 {
529         struct symtab *sp2, *sp3;
530
531         for (; sp != NULLsp = sp->snext) {
532                 if (sp->sname[0] == '*') {
533                         /* unnamed member, recurse down */
ragge
1.225
534                         if ((sp2 = findmember(sp->ssue->suems))) {
ragge
1.218
535                                 sp3 = tmpalloc(sizeof (struct symtab));
536                                 *sp3 = *sp2;
537                                 sp3->soffset += sp->soffset;
538                                 return sp3;
539                         }
540                 } else if (sp->sname == s)
541                         return sp;
542         }
543         return NULL;
544 }
545
546
ragge
1.107
547 /*
ragge
1.214
548  * Check if there will be a lost label destination inside of a ?:
549  * It cannot be reached so just print it out.
550  */
551 static void
ragge
1.215
552 putjops(NODE *pvoid *arg)
ragge
1.214
553 {
554         if (p->n_op == COMOP && p->n_left->n_op == GOTO)
555                 plabel(p->n_left->n_left->n_lval+1);
556 }
557
558 /*
ragge
1.199
559  * Build a name node based on a symtab entry.
560  * broken out from buildtree().
561  */
562 NODE *
563 nametree(struct symtab *sp)
564 {
565         NODE *p;
566
567         p = block(NAMENILNILsp->stypesp->sdfsp->ssue);
568         p->n_qual = sp->squal;
569         p->n_sp = sp;
570
571 #ifndef NO_C_BUILTINS
572         if (sp->sname[0] == '_' && strncmp(sp->sname"__builtin_"10) == 0)
573                 return p;  /* do not touch builtins here */
574         
575 #endif
576
577         if (sp->sflags & STNODE) {
578                 /* Generated for optimizer */
579                 p->n_op = TEMP;
580                 p->n_rval = sp->soffset;
581         }
582
583 #ifdef GCC_COMPAT
584         /* Get a label name */
585         if (sp->sflags == SLBLNAME) {
586                 p->n_type = VOID;
587                 p->n_sue = MKSUE(VOID);
588         }
589 #endif
590         if (sp->stype == UNDEF) {
591                 uerror("%s undefined"sp->sname);
592                 /* make p look reasonable */
593                 p->n_type = INT;
594                 p->n_sue = MKSUE(INT);
595                 p->n_df = NULL;
596                 defid(pSNULL);
597         }
598         if (sp->sclass == MOE) {
599                 p->n_op = ICON;
600                 p->n_lval = sp->soffset;
601                 p->n_df = NULL;
602                 p->n_sp = NULL;
603         }
604         return clocal(p);
605 }
606
607 /*
ragge
1.107
608  * Do a conditional branch.
609  */
610 void
611 cbranch(NODE *pNODE *q)
612 {
613         p = buildtree(CBRANCHpq);
614         if (p->n_left->n_op == ICON) {
ragge
1.208
615                 if (p->n_left->n_lval != 0) {
ragge
1.107
616                         branch(q->n_lval); /* branch always */
ragge
1.208
617                         reached = 0;
618                 }
ragge
1.107
619                 tfree(p);
620                 tfree(q);
621                 return;
622         }
623         ecomp(p);
624 }
625
ragge
1.1
626 NODE *
ragge
1.45
627 strargsp ) register NODE *p;  { /* rewrite structure flavored arguments */
ragge
1.1
628
ragge
1.45
629         ifp->n_op == CM ){
630                 p->n_left = strargsp->n_left );
631                 p->n_right = strargsp->n_right );
ragge
1.1
632                 returnp );
633                 }
634
ragge
1.22
635         ifp->n_type == STRTY || p->n_type == UNIONTY ){
ragge
1.29
636                 p = block(STARGpNILp->n_typep->n_dfp->n_sue);
ragge
1.93
637                 p->n_left = buildtreeADDROFp->n_leftNIL );
ragge
1.1
638                 p = clocal(p);
639                 }
640         returnp );
ragge
1.2
641 }
ragge
1.1
642
ragge
1.2
643 /*
644  * apply the op o to the lval part of p; if binary, rhs is val
645  */
646 int
647 conval(NODE *pint oNODE *q)
648 {
ragge
1.1
649         int iu;
650         CONSZ val;
ragge
1.159
651         U_CONSZ v1v2;
ragge
1.1
652
ragge
1.22
653         val = q->n_lval;
654         u = ISUNSIGNED(p->n_type) || ISUNSIGNED(q->n_type);
ragge
1.1
655         ifu && (o==LE||o==LT||o==GE||o==GT)) o += (UGE-GE);
656
ragge
1.23
657         if (p->n_sp != NULL && q->n_sp != NULL)
658                 return(0);
659         if (q->n_sp != NULL && o != PLUS)
660                 return(0);
661         if (p->n_sp != NULL && o != PLUS && o != MINUS)
662                 return(0);
ragge
1.161
663         v1 = p->n_lval;
664         v2 = q->n_lval;
ragge
1.1
665         switcho ){
666
667         case PLUS:
ragge
1.22
668                 p->n_lval += val;
ragge
1.23
669                 if (p->n_sp == NULL) {
ragge
1.22
670                         p->n_rval = q->n_rval;
671                         p->n_type = q->n_type;
ragge
1.23
672                 }
ragge
1.1
673                 break;
674         case MINUS:
ragge
1.22
675                 p->n_lval -= val;
ragge
1.1
676                 break;
677         case MUL:
ragge
1.22
678                 p->n_lval *= val;
ragge
1.1
679                 break;
680         case DIV:
ragge
1.46
681                 if (val == 0)
682                         uerror("division by 0");
ragge
1.159
683                 else  {
684                         if (u) {
685                                 v1 /= v2;
686                                 p->n_lval = v1;
687                         } else
688                                 p->n_lval /= val;
689                 }
ragge
1.1
690                 break;
691         case MOD:
ragge
1.46
692                 if (val == 0)
693                         uerror("division by 0");
ragge
1.159
694                 else  {
695                         if (u) {
696                                 v1 %= v2;
697                                 p->n_lval = v1;
698                         } else
699                                 p->n_lval %= val;
700                 }
ragge
1.1
701                 break;
702         case AND:
ragge
1.22
703                 p->n_lval &= val;
ragge
1.1
704                 break;
705         case OR:
ragge
1.22
706                 p->n_lval |= val;
ragge
1.1
707                 break;
708         case ER:
ragge
1.22
709                 p->n_lval ^= val;
ragge
1.1
710                 break;
711         case LS:
712                 i = val;
ragge
1.22
713                 p->n_lval = p->n_lval << i;
ragge
1.1
714                 break;
715         case RS:
716                 i = val;
ragge
1.159
717                 if (u) {
718                         v1 = v1 >> i;
719                         p->n_lval = v1;
720                 } else
721                         p->n_lval = p->n_lval >> i;
ragge
1.1
722                 break;
723
ragge
1.94
724         case UMINUS:
ragge
1.22
725                 p->n_lval = - p->n_lval;
ragge
1.1
726                 break;
727         case COMPL:
ragge
1.22
728                 p->n_lval = ~p->n_lval;
ragge
1.1
729                 break;
730         case NOT:
ragge
1.22
731                 p->n_lval = !p->n_lval;
ragge
1.1
732                 break;
733         case LT:
ragge
1.22
734                 p->n_lval = p->n_lval < val;
ragge
1.1
735                 break;
736         case LE:
ragge
1.22
737                 p->n_lval = p->n_lval <= val;
ragge
1.1
738                 break;
739         case GT:
ragge
1.22
740                 p->n_lval = p->n_lval > val;
ragge
1.1
741                 break;
742         case GE:
ragge
1.22
743                 p->n_lval = p->n_lval >= val;
ragge
1.1
744                 break;
745         case ULT:
ragge
1.159
746                 p->n_lval = v1 < v2;
ragge
1.1
747                 break;
748         case ULE:
ragge
1.159
749                 p->n_lval = v1 <= v2;
ragge
1.1
750                 break;
751         case UGT:
ragge
1.159
752                 p->n_lval = v1 > v2;
ragge
1.1
753                 break;
754         case UGE:
ragge
1.159
755                 p->n_lval = v1 >= v2;
ragge
1.1
756                 break;
757         case EQ:
ragge
1.22
758                 p->n_lval = p->n_lval == val;
ragge
1.1
759                 break;
760         case NE:
ragge
1.22
761                 p->n_lval = p->n_lval != val;
ragge
1.1
762                 break;
ragge
1.155
763         case ANDAND:
764                 p->n_lval = p->n_lval && val;
765                 break;
ragge
1.154
766         case OROR:
767                 p->n_lval = p->n_lval || val;
768                 break;
ragge
1.1
769         default:
770                 return(0);
771                 }
772         return(1);
773         }
774
ragge
1.7
775 /*
776  * Checks p for the existance of a pun.  This is called when the op of p
777  * is ASSIGN, RETURN, CAST, COLON, or relational.
778  * One case is when enumerations are used: this applies only to lint.
779  * In the other case, one operand is a pointer, the other integer type
780  * we check that this integer is in fact a constant zero...
781  * in the case of ASSIGN, any assignment of pointer to integer is illegal
782  * this falls out, because the LHS is never 0.
ragge
1.212
783  * XXX - check for COMOPs in assignment RHS?
ragge
1.7
784  */
ragge
1.2
785 void
ragge
1.7
786 chkpun(NODE *p)
787 {
ragge
1.29
788         union dimfun *d1, *d2;
ragge
1.2
789         NODE *q;
ragge
1.47
790         int t1t2;
ragge
1.1
791
ragge
1.22
792         t1 = p->n_left->n_type;
793         t2 = p->n_right->n_type;
ragge
1.1
794
ragge
1.56
795         switch (p->n_op) {
796         case RETURN:
797                 /* return of void allowed but nothing else */
ragge
1.52
798                 if (t1 == VOID && t2 == VOID)
799                         return;
ragge
1.163
800                 if (t1 == VOID) {
801                         werror("returning value from void function");
802                         return;
803                 }
804                 if (t2 == VOID) {
805                         uerror("using void value");
806                         return;
807                 }
ragge
1.56
808         case COLON:
809                 if (t1 == VOID && t2 == VOID)
810                         return;
811                 break;
812         default:
otto
1.165
813                 if ((t1 == VOID && t2 != VOID) || (t1 != VOID && t2 == VOID)) {
814                         uerror("value of void expression used");
815                         return;
816                 }
ragge
1.56
817                 break;
ragge
1.52
818         }
819
820         /* allow void pointer assignments in any direction */
821         if (BTYPE(t1) == VOID && (t2 & TMASK))
822                 return;
823         if (BTYPE(t2) == VOID && (t1 & TMASK))
824                 return;
825
826         if (ISPTR(t1) || ISARY(t1))
827                 q = p->n_right;
828         else
829                 q = p->n_left;
ragge
1.1
830
ragge
1.52
831         if (!ISPTR(q->n_type) && !ISARY(q->n_type)) {
ragge
1.22
832                 if (q->n_op != ICON || q->n_lval != 0)
ragge
1.47
833                         werror("illegal combination of pointer and integer");
834         } else {
835                 d1 = p->n_left->n_df;
836                 d2 = p->n_right->n_df;
ragge
1.52
837                 if (t1 == t2) {
ragge
1.225
838                         if (p->n_left->n_sue->suem != p->n_right->n_sue->suem)
ragge
1.52
839                                 werror("illegal structure pointer combination");
840                         return;
841                 }
ragge
1.47
842                 for (;;) {
ragge
1.52
843                         if (ISARY(t1) || ISPTR(t1)) {
844                                 if (!ISARY(t2) && !ISPTR(t2))
845                                         break;
ragge
1.47
846                                 if (ISARY(t1) && ISARY(t2) && d1->ddim != d2->ddim) {
847                                         werror("illegal array size combination");
848                                         return;
849                                 }
ragge
1.52
850                                 if (ISARY(t1))
851                                         ++d1;
852                                 if (ISARY(t2))
853                                         ++d2;
ragge
1.47
854                         } else if (ISFTN(t1)) {
855                                 if (chkftn(d1->dfund2->dfun)) {
856                                         werror("illegal function "
857                                             "pointer combination");
858                                         return;
859                                 }
860                                 ++d1;
861                                 ++d2;
ragge
1.52
862                         } else
863                                 break;
ragge
1.47
864                         t1 = DECREF(t1);
865                         t2 = DECREF(t2);
866                 }
gmcgarry
1.197
867                 if (Wpointer_sign)
868                         werror("illegal pointer combination");
ragge
1.1
869         }
ragge
1.5
870 }
ragge
1.1
871
872 NODE *
ragge