Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:gmcgarry:20080516021136

Diff

Diff from 1.192 to:

Annotations

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

Annotated File View

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