Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:gmcgarry:20080808122645

Diff

Diff from 1.205 to:

Annotations

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

Annotated File View

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