Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20080326200319

Diff

Diff from 1.189 to:

Annotations

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

Annotated File View

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