Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20071226133150

Diff

Diff from 1.180 to:

Annotations

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

Annotated File View

ragge
1.180
1 /*      $Id: trees.c,v 1.180 2007/12/26 13:31:50 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>
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.2
79
ragge
1.77
80 int lastloc = -1;
ragge
1.1
81
82 /*      some special actions, used in finding the type of nodes */
83 # define NCVT 01
84 # define PUN 02
85 # define TYPL 04
86 # define TYPR 010
87 # define TYMATCH 040
88 # define LVAL 0100
89 # define CVTO 0200
90 # define CVTL 0400
91 # define CVTR 01000
92 # define PTMATCH 02000
93 # define OTHER 04000
94 # define NCVTR 010000
95
96 /* node conventions:
97
98         NAME:   rval>0 is stab index for external
99                 rval<0 is -inlabel number
100                 lval is offset in bits
101         ICON:   lval has the value
102                 rval has the STAB index, or - label number,
103                         if a name whose address is in the constant
104                 rval = NONAME means no name
105         REG:    rval is reg. identification cookie
106
107         */
108
ragge
1.30
109 int bdebug = 0;
110
ragge
1.1
111 NODE *
ragge
1.2
112 buildtree(int oNODE *lNODE *r)
113 {
114         NODE *p, *q;
115         int actions;
116         int opty;
ragge
1.119
117         struct symtab *sp = NULL/* XXX gcc */
ragge
1.2
118         NODE *lr, *ll;
ragge
1.23
119         char *name;
ragge
1.26
120         struct symtab **elem;
ragge
1.1
121
ragge
1.68
122 #ifdef PCC_DEBUG
ragge
1.128
123         if (bdebug) {
ragge
1.58
124                 printf("buildtree(%s, %p, %p)\n"copst(o), lr);
ragge
1.128
125                 if (lfwalk(leprint0);
126                 if (rfwalk(reprint0);
127         }
ragge
1.68
128 #endif
ragge
1.58
129         opty = coptype(o);
ragge
1.1
130
131         /* check for constants */
132
ragge
1.22
133         ifopty == UTYPE && l->n_op == ICON ){
ragge
1.1
134
135                 switcho ){
136
137                 case NOT:
ragge
1.94
138                 case UMINUS:
ragge
1.1
139                 case COMPL:
140                         ifconvallol ) ) return(l);
141                         break;
142                 }
ragge
1.71
143         } else if (o == NOT && l->n_op == FCON) {
144                 l = clocal(block(SCONVlNILINT0MKSUE(INT)));
ragge
1.94
145         } else ifo == UMINUS && l->n_op == FCON ){
ragge
1.71
146                         l->n_dcon = -l->n_dcon;
ragge
1.1
147                         return(l);
148
ragge
1.44
149         } else ifo==QUEST && l->n_op==ICON ) {
ragge
1.41
150                 CONSZ c = l->n_lval;
151                 nfree(l);
152                 if (c) {
153                         tfree(r->n_right);
154                         l = r->n_left;
155                         nfree(r);
156                         return(l);
157                 } else {
158                         tfree(r->n_left);
159                         l = r->n_right;
160                         nfree(r);
161                         return(l);
ragge
1.1
162                 }
ragge
1.155
163         } else ifopty == BITYPE && l->n_op == ICON && r->n_op == ICON ){
ragge
1.1
164
165                 switcho ){
166
ragge
1.159
167                 case PLUS:
168                 case MINUS:
169                 case MUL:
170                 case DIV:
171                 case MOD:
172                         /*
173                          * Do type propagation for simple types here.
174                          * The constant value is correct anyway.
175                          * Maybe this op shortcut should be removed?
176                          */
177                         if (l->n_sp == NULL && r->n_sp == NULL &&
178                             l->n_type < BTMASK && r->n_type < BTMASK) {
179                                 if (l->n_type > r->n_type)
180                                         r->n_type = l->n_type;
181                                 else
182                                         l->n_type = r->n_type;
183                         }
184                         /* FALLTHROUGH */
ragge
1.1
185                 case ULT:
186                 case UGT:
187                 case ULE:
188                 case UGE:
189                 case LT:
190                 case GT:
191                 case LE:
192                 case GE:
193                 case EQ:
ragge
1.48
194                 case NE:
ragge
1.1
195                 case ANDAND:
196                 case OROR:
197                 case AND:
198                 case OR:
199                 case ER:
200                 case LS:
201                 case RS:
202                         ifconvallor ) ) {
ragge
1.41
203                                 nfree(r);
ragge
1.1
204                                 return(l);
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");
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;
306                                 p->n_lval = sp->soffset;
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
ragge
1.26
355                         if ((elem = l->n_sue->suelem) == NULL)
ragge
1.23
356                                 uerror("undefined struct or union");
357
358                         name = r->n_name;
ragge
1.26
359                         for (; *elem != NULLelem++) {
360                                 sp = *elem;
ragge
1.23
361                                 if (sp->sname == name)
362                                         break;
363                         }
ragge
1.26
364                         if (*elem == NULL)
ragge
1.23
365                                 uerror("member '%s' not declared"name);
366
367                         r->n_sp = sp;
368                         p = stref(p);
369                         break;
370
ragge
1.94
371                 case UMUL:
ragge
1.93
372                         if (l->n_op == ADDROF) {
ragge
1.41
373                                 nfree(p);
ragge
1.22
374                                 p = l->n_left;
ragge
1.41
375                                 nfree(l);
376                         }
ragge
1.22
377                         if( !ISPTR(l->n_type))uerror("illegal indirection");
378                         p->n_type = DECREF(l->n_type);
ragge
1.67
379                         p->n_qual = DECREF(l->n_qual);
ragge
1.29
380                         p->n_df = l->n_df;
ragge
1.26
381                         p->n_sue = l->n_sue;
ragge
1.1
382                         break;
383
ragge
1.93
384                 case ADDROF:
ragge
1.22
385                         switchl->n_op ){
ragge
1.1
386
ragge
1.94
387                         case UMUL:
ragge
1.41
388                                 nfree(p);
ragge
1.22
389                                 p = l->n_left;
ragge
1.41
390                                 nfree(l);
ragge
1.130
391                         case TEMP:
ragge
1.1
392                         case NAME:
ragge
1.68
393                                 p->n_type = INCREF(l->n_type);
394                                 p->n_qual = INCQAL(l->n_qual);
ragge
1.29
395                                 p->n_df = l->n_df;
ragge
1.26
396                                 p->n_sue = l->n_sue;
ragge
1.1
397                                 break;
398
399                         case COMOP:
ragge
1.41
400                                 nfree(p);
ragge
1.93
401                                 lr = buildtree(ADDROFl->n_rightNIL);
ragge
1.22
402                                 p = buildtreeCOMOPl->n_leftlr );
ragge
1.41
403                                 nfree(l);
ragge
1.1
404                                 break;
405
406                         case QUEST:
ragge
1.93
407                                 lr = buildtreeADDROFl->n_right->n_rightNIL );
408                                 ll = buildtreeADDROFl->n_right->n_leftNIL );
ragge
1.41
409                                 nfree(p); nfree(l->n_right);
ragge
1.22
410                                 p = buildtreeQUESTl->n_leftbuildtreeCOLONlllr ) );
ragge
1.41
411                                 nfree(l);
ragge
1.1
412                                 break;
413
414                         default:
ragge
1.22
415                                 uerror("unacceptable operand of &: %d"l->n_op );
ragge
1.1
416                                 break;
417                                 }
418                         break;
419
420                 case LS:
ragge
1.150
421                 case RS/* must make type size at least int... */
422                         if (p->n_type == CHAR || p->n_type == SHORT) {
423                                 p->n_left = makety(lINT00MKSUE(INT));
424                         } else if (p->n_type == UCHAR || p->n_type == USHORT) {
425                                 p->n_left = makety(lUNSIGNED00,
426                                     MKSUE(UNSIGNED));
427                         }
428                         l = p->n_left;
429                         p->n_type = l->n_type;
430                         p->n_qual = l->n_qual;
431                         p->n_df = l->n_df;
432                         p->n_sue = l->n_sue;
433
434                         /* FALLTHROUGH */
ragge
1.95
435                 case LSEQ:
ragge
1.150
436                 case RSEQ/* ...but not for assigned types */
437                         if(tsize(r->n_typer->n_dfr->n_sue) > SZINT)
438                                 p->n_right = makety(rINT00MKSUE(INT));
ragge
1.1
439                         break;
440
441                 case RETURN:
442                 case ASSIGN:
443                 case CAST:
444                         /* structure assignment */
445                         /* take the addresses of the two sides; then make an
ragge
1.2
446                          * operator using STASG and
447                          * the addresses of left and right */
ragge
1.1
448
449                         {
ragge
1.26
450                                 struct suedef *sue;
ragge
1.2
451                                 TWORD t;
ragge
1.29
452                                 union dimfun *d;
ragge
1.1
453
ragge
1.26
454                                 if (l->n_sue != r->n_sue)
455                                         uerror("assignment of different structures");
ragge
1.1
456
ragge
1.93
457                                 r = buildtree(ADDROFrNIL);
ragge
1.22
458                                 t = r->n_type;
ragge
1.29
459                                 d = r->n_df;
ragge
1.26
460                                 sue = r->n_sue;
ragge
1.1
461
ragge
1.28
462                                 l = block(STASGlrtdsue);
gmcgarry
1.177
463                                 l = clocal(l);
ragge
1.1
464
465                                 ifo == RETURN ){
ragge
1.41
466                                         nfree(p);
ragge
1.1
467                                         p = l;
468                                         break;
ragge
1.28
469                                 }
ragge
1.1
470
ragge
1.94
471                                 p->n_op = UMUL;
ragge
1.22
472                                 p->n_left = l;
473                                 p->n_right = NIL;
ragge
1.1
474                                 break;
475                                 }
476                 case COLON:
477                         /* structure colon */
478
ragge
1.26
479                         if (l->n_sue != r->n_sue)
480                                 uerror"type clash in conditional" );
ragge
1.1
481                         break;
482
483                 case CALL:
ragge
1.45
484                         p->n_right = r = strargs(p->n_right);
ragge
1.171
485                         p = funcode(p);
486                         /* FALLTHROUGH */
ragge
1.95
487                 case UCALL:
ragge
1.23
488                         if (!ISPTR(l->n_type))
489                                 uerror("illegal function");
ragge
1.22
490                         p->n_type = DECREF(l->n_type);
ragge
1.23
491                         if (!ISFTN(p->n_type))
492                                 uerror("illegal function");
493                         p->n_type = DECREF(p->n_type);
ragge
1.29
494                         p->n_df = l->n_df;
ragge
1.26
495                         p->n_sue = l->n_sue;
ragge
1.93
496                         if (l->n_op == ADDROF && l->n_left->n_op == NAME &&
ragge
1.23
497                             l->n_left->n_sp != NULL && l->n_left->n_sp != NULL &&
498                             (l->n_left->n_sp->sclass == FORTRAN ||
499                             l->n_left->n_sp->sclass == UFORTRAN)) {
ragge
1.22
500                                 p->n_op += (FORTCALL-CALL);
ragge
1.23
501                         }
502                         if (p->n_type == STRTY || p->n_type == UNIONTY) {
ragge
1.1
503                                 /* function returning structure */
504                                 /*  make function really return ptr to str., with * */
505
ragge
1.22
506                                 p->n_op += STCALL-CALL;
ragge
1.23
507                                 p->n_type = INCREF(p->n_type);
ragge
1.149
508                                 p = clocal(p); /* before recursing */
ragge
1.94
509                                 p = buildtree(UMULpNIL);
ragge
1.1
510
511                                 }
512                         break;
513
514                 default:
515                         cerror"other code %d"o );
516                         }
517
518                 }
519
ragge
1.11
520         /*
521          * Allow (void)0 casts.
522          * XXX - anything on the right side must be possible to cast.
523          * XXX - remove void types further on.
524          */
ragge
1.50
525         if (p->n_op == CAST && p->n_type == VOID &&
ragge
1.22
526             p->n_right->n_op == ICON)
ragge
1.50
527                 p->n_right->n_type = VOID;
ragge
1.11
528
ragge
1.68
529         if (actions & CVTO)
530                 p = oconvert(p);
ragge
1.1
531         p = clocal(p);
532
ragge
1.30
533 #ifdef PCC_DEBUG
ragge
1.74
534         if (bdebug) {
535                 printf("End of buildtree:\n");
ragge
1.68
536                 fwalk(peprint0);
ragge
1.74
537         }
ragge
1.30
538 #endif
ragge
1.1
539
540         return(p);
541
542         }
543
ragge
1.107
544 /*
545  * Do a conditional branch.
546  */
547 void
548 cbranch(NODE *pNODE *q)
549 {
550         p = buildtree(CBRANCHpq);
551         if (p->n_left->n_op == ICON) {
552                 if (p->n_left->n_lval != 0)
553                         branch(q->n_lval); /* branch always */
554                 tfree(p);
555                 tfree(q);
556                 return;
557         }
558         ecomp(p);
559 }
560
ragge
1.1
561 NODE *
ragge
1.45
562 strargsp ) register NODE *p;  { /* rewrite structure flavored arguments */
ragge
1.1
563
ragge
1.45
564         ifp->n_op == CM ){
565                 p->n_left = strargsp->n_left );
566                 p->n_right = strargsp->n_right );
ragge
1.1
567                 returnp );
568                 }
569
ragge
1.22
570         ifp->n_type == STRTY || p->n_type == UNIONTY ){
ragge
1.29
571                 p = block(STARGpNILp->n_typep->n_dfp->n_sue);
ragge
1.93
572                 p->n_left = buildtreeADDROFp->n_leftNIL );
ragge
1.1
573                 p = clocal(p);
574                 }
575         returnp );
ragge
1.2
576 }
ragge
1.1
577
ragge
1.2
578 /*
579  * apply the op o to the lval part of p; if binary, rhs is val
580  */
581 int
582 conval(NODE *pint oNODE *q)
583 {
ragge
1.1
584         int iu;
585         CONSZ val;
ragge
1.159
586         U_CONSZ v1v2;
ragge
1.1
587
ragge
1.22
588         val = q->n_lval;
589         u = ISUNSIGNED(p->n_type) || ISUNSIGNED(q->n_type);
ragge
1.1
590         ifu && (o==LE||o==LT||o==GE||o==GT)) o += (UGE-GE);
591
ragge
1.23
592         if (p->n_sp != NULL && q->n_sp != NULL)
593                 return(0);
594         if (q->n_sp != NULL && o != PLUS)
595                 return(0);
596         if (p->n_sp != NULL && o != PLUS && o != MINUS)
597                 return(0);
ragge
1.161
598         v1 = p->n_lval;
599         v2 = q->n_lval;
ragge
1.1
600         switcho ){
601
602         case PLUS:
ragge
1.22
603                 p->n_lval += val;
ragge
1.23
604                 if (p->n_sp == NULL) {
ragge
1.22
605                         p->n_rval = q->n_rval;
606                         p->n_type = q->n_type;
ragge
1.23
607                 }
ragge
1.1
608                 break;
609         case MINUS:
ragge
1.22
610                 p->n_lval -= val;
ragge
1.1
611                 break;
612         case MUL:
ragge
1.22
613                 p->n_lval *= val;
ragge
1.1
614                 break;
615         case DIV:
ragge
1.46
616                 if (val == 0)
617                         uerror("division by 0");
ragge
1.159
618                 else  {
619                         if (u) {
620                                 v1 /= v2;
621                                 p->n_lval = v1;
622                         } else
623                                 p->n_lval /= val;
624                 }
ragge
1.1
625                 break;
626         case MOD:
ragge
1.46
627                 if (val == 0)
628                         uerror("division by 0");
ragge
1.159
629                 else  {
630                         if (u) {
631                                 v1 %= v2;
632                                 p->n_lval = v1;
633                         } else
634                                 p->n_lval %= val;
635                 }
ragge
1.1
636                 break;
637         case AND:
ragge
1.22
638                 p->n_lval &= val;
ragge
1.1
639                 break;
640         case OR:
ragge
1.22
641                 p->n_lval |= val;
ragge
1.1
642                 break;
643         case ER:
ragge
1.22
644                 p->n_lval ^= val;
ragge
1.1
645                 break;
646         case LS:
647                 i = val;
ragge
1.22
648                 p->n_lval = p->n_lval << i;
ragge
1.1
649                 break;
650         case RS:
651                 i = val;
ragge
1.159
652                 if (u) {
653                         v1 = v1 >> i;
654                         p->n_lval = v1;
655                 } else
656                         p->n_lval = p->n_lval >> i;
ragge
1.1
657                 break;
658
ragge
1.94
659         case UMINUS:
ragge
1.22
660                 p->n_lval = - p->n_lval;
ragge
1.1
661                 break;
662         case COMPL:
ragge
1.22
663                 p->n_lval = ~p->n_lval;
ragge
1.1
664                 break;
665         case NOT:
ragge
1.22
666                 p->n_lval = !p->n_lval;
ragge
1.1
667                 break;
668         case LT:
ragge
1.22
669                 p->n_lval = p->n_lval < val;
ragge
1.1
670                 break;
671         case LE:
ragge
1.22
672                 p->n_lval = p->n_lval <= val;
ragge
1.1
673                 break;
674         case GT:
ragge
1.22
675                 p->n_lval = p->n_lval > val;
ragge
1.1
676                 break;
677         case GE:
ragge
1.22
678                 p->n_lval = p->n_lval >= val;
ragge
1.1
679                 break;
680         case ULT:
ragge
1.159
681                 p->n_lval = v1 < v2;
ragge
1.1
682                 break;
683         case ULE:
ragge
1.159
684                 p->n_lval = v1 <= v2;
ragge
1.1
685                 break;
686         case UGT:
ragge
1.159
687                 p->n_lval = v1 > v2;
ragge
1.1
688                 break;
689         case UGE:
ragge
1.159
690                 p->n_lval = v1 >= v2;
ragge
1.1
691                 break;
692         case EQ:
ragge
1.22
693                 p->n_lval = p->n_lval == val;
ragge
1.1
694                 break;
695         case NE:
ragge
1.22
696                 p->n_lval = p->n_lval != val;
ragge
1.1
697                 break;
ragge
1.155
698         case ANDAND:
699                 p->n_lval = p->n_lval && val;
700                 break;
ragge
1.154
701         case OROR:
702                 p->n_lval = p->n_lval || val;
703                 break;
ragge
1.1
704         default:
705                 return(0);
706                 }
707         return(1);
708         }
709
ragge
1.7
710 /*
711  * Checks p for the existance of a pun.  This is called when the op of p
712  * is ASSIGN, RETURN, CAST, COLON, or relational.
713  * One case is when enumerations are used: this applies only to lint.
714  * In the other case, one operand is a pointer, the other integer type
715  * we check that this integer is in fact a constant zero...
716  * in the case of ASSIGN, any assignment of pointer to integer is illegal
717  * this falls out, because the LHS is never 0.
718  */
ragge
1.2
719 void
ragge
1.7
720 chkpun(NODE *p)
721 {
ragge
1.29
722         union dimfun *d1, *d2;
ragge
1.2
723         NODE *q;
ragge
1.47
724         int t1t2;
ragge
1.1
725
ragge
1.22
726         t1 = p->n_left->n_type;
727         t2 = p->n_right->n_type;
ragge
1.1
728
ragge
1.56
729         switch (p->n_op) {
730         case RETURN:
731                 /* return of void allowed but nothing else */
ragge
1.52
732                 if (t1 == VOID && t2 == VOID)
733                         return;
ragge
1.163
734                 if (t1 == VOID) {
735                         werror("returning value from void function");
736                         return;
737                 }
738                 if (t2 == VOID) {
739                         uerror("using void value");
740                         return;
741                 }
ragge
1.56
742         case COLON:
743                 if (t1 == VOID && t2 == VOID)
744                         return;
745                 break;
746         default:
otto
1.165
747                 if ((t1 == VOID && t2 != VOID) || (t1 != VOID && t2 == VOID)) {
748                         uerror("value of void expression used");
749                         return;
750                 }
ragge
1.56
751                 break;
ragge
1.52
752         }
753
754         /* allow void pointer assignments in any direction */
755         if (BTYPE(t1) == VOID && (t2 & TMASK))
756                 return;
757         if (BTYPE(t2) == VOID && (t1 & TMASK))
758                 return;
759
760         if (ISPTR(t1) || ISARY(t1))
761                 q = p->n_right;
762         else
763                 q = p->n_left;
ragge
1.1
764
ragge
1.52
765         if (!ISPTR(q->n_type) && !ISARY(q->n_type)) {
ragge
1.22
766                 if (q->n_op != ICON || q->n_lval != 0)
ragge
1.47
767                         werror("illegal combination of pointer and integer");
768         } else {
769                 d1 = p->n_left->n_df;
770                 d2 = p->n_right->n_df;
ragge
1.52
771                 if (t1 == t2) {
772                         if (p->n_left->n_sue != p->n_right->n_sue)
773                                 werror("illegal structure pointer combination");
774                         return;
775                 }
ragge
1.47
776                 for (;;) {
ragge
1.52
777                         if (ISARY(t1) || ISPTR(t1)) {
778                                 if (!ISARY(t2) && !ISPTR(t2))
779                                         break;
ragge
1.47
780                                 if (ISARY(t1) && ISARY(t2) && d1->ddim != d2->ddim) {
781                                         werror("illegal array size combination");
782                                         return;
783                                 }
ragge
1.52
784                                 if (ISARY(t1))
785                                         ++d1;
786                                 if (ISARY(t2))
787                                         ++d2;
ragge
1.47
788                         } else if (ISFTN(t1)) {
789                                 if (chkftn(d1->dfund2->dfun)) {
790                                         werror("illegal function "
791                                             "pointer combination");
792                                         return;
793                                 }
794                                 ++d1;
795                                 ++d2;
ragge
1.52
796                         } else
797                                 break;
ragge
1.47
798                         t1 = DECREF(t1);
799                         t2 = DECREF(t2);
800                 }
801                 werror("illegal pointer combination");
ragge
1.1
802         }
ragge
1.5
803 }
ragge
1.1
804
805 NODE *
ragge
1.10
806 stref(NODE *p)
807 {
ragge
1.41
808         NODE *r;
ragge
1.26
809         struct suedef *sue;
ragge
1.29
810         union dimfun *d;
ragge
1.68
811         TWORD tq;
ragge
1.49
812         int dsc;
ragge
1.1
813         OFFSZ off;
ragge
1.68
814         struct symtab *s;
ragge
1.1
815
816         /* make p->x */
817         /* this is also used to reference automatic variables */
818
ragge
1.68
819         s = p->n_right->n_sp;
ragge
1.41
820         nfree(p->n_right);
821         r = p->n_left;
822         nfree(p);
823         p = pconvert(r);
ragge
1.1
824
825         /* make p look like ptr to x */
826
ragge
1.22
827         if (!ISPTR(p->n_type))
828                 p->n_type = PTR+UNIONTY;
ragge
1.1
829
ragge
1.68
830         t = INCREF(s->stype);
831         q = INCQAL(s->squal);
832         d = s->sdf;
833         sue = s->ssue;
ragge
1.1
834
ragge
1.68
835         p = makety(ptqdsue);
ragge
1.1
836
837         /* compute the offset to be added */
838
ragge
1.68
839         off = s->soffset;
840         dsc = s->sclass;
ragge
1.1
841
ragge
1.168
842 #ifndef CAN_UNALIGN
843         /*
844          * If its a packed struct, and the target cannot do unaligned
845          * accesses, then split it up in two bitfield operations.
846          * LHS and RHS accesses are different, so must delay
847          * it until we know.  Do the bitfield construct here though.
848          */
849         if ((dsc & FIELD) == 0 && (off % talign(s->stypes->ssue))) {
850 //              int sz = tsize(s->stype, s->sdf, s->ssue);
851 //              int al = talign(s->stype, s->ssue);
852 //              int sz1 = al - (off % al);
853                 
854         }
855 #endif
856
ragge
1.49
857         if (dsc & FIELD) {  /* make fields look like ints */
858                 off = (off/ALINT)*ALINT;
ragge
1.26
859                 sue = MKSUE(INT);
ragge
1.10
860         }
ragge
1.68
861         if (off != 0) {
862                 p = block(PLUSpoffcon(offtdsue), tdsue);
863                 p->n_qual = q;
ragge
1.81
864                 p = optim(p);
ragge
1.68
865         }
ragge
1.1
866
ragge