5 #define xx(a,b,c,d,e,f,g) c,
6 #define yy(a,b,c,d,e,f,g) c,
10 #define xx(a,b,c,d,e,f,g) d,
11 #define yy(a,b,c,d,e,f,g) d,
15 static Tree
expr2(void);
16 static Tree
expr3(int);
17 static Tree
nullcheck(Tree
);
18 static Tree
postfix(Tree
);
19 static Tree
unary(void);
20 static Tree
primary(void);
21 static Type
super(Type ty
);
23 static Type
super(Type ty
) {
26 if (ty
->size
< inttype
->size
)
30 if (ty
->size
< unsignedtype
->size
)
39 static char stop
[] = { IF
, ID
, '}', 0 };
45 q
= pointer(expr1(0));
46 p
= tree(RIGHT
, q
->type
, root(value(p
)), q
);
53 return root(expr(tok
));
56 static char stop
[] = { IF
, ID
, 0 };
60 || (prec
[t
] >= 6 && prec
[t
] <= 8)
61 || (prec
[t
] >= 11 && prec
[t
] <= 13)) {
65 p
= asgntree(ASGN
, p
, value(expr1(0)));
69 p
= incr(op
, p
, expr1(0));
76 Tree
incr(int op
, Tree v
, Tree e
) {
77 return asgntree(ASGN
, v
, (*optree
[op
])(oper
[op
], v
, e
));
79 static Tree
expr2(void) {
85 if (Aflag
> 1 && isfunc(p
->type
))
86 warning("%s used in a conditional expression\n",
91 l
= pointer(expr(':'));
96 apply(events
.points
, &pts
[0], &l
);
97 apply(events
.points
, &pts
[1], &r
);
99 p
= condtree(p
, l
, r
);
104 int op
= generic(rightkid(p
)->op
);
106 if (p
->type
!= voidtype
107 && (op
==AND
|| op
==OR
|| op
==NOT
|| op
==EQ
|| op
==NE
108 || op
== LE
|| op
==LT
|| op
== GE
|| op
==GT
))
109 p
= condtree(p
, consttree(1, inttype
),
110 consttree(0, inttype
));
113 static Tree
expr3(int k
) {
117 for (k1
= prec
[t
]; k1
>= k
; k1
--)
118 while (prec
[t
] == k1
&& *cp
!= '=') {
125 if (op
== ANDAND
|| op
== OROR
) {
126 r
= pointer(expr3(k1
));
128 apply(events
.points
, &pt
, &r
);
130 r
= pointer(expr3(k1
+ 1));
131 p
= (*optree
[op
])(oper
[op
], p
, r
);
135 static Tree
unary(void) {
139 case '*': t
= gettok(); p
= unary(); p
= pointer(p
);
141 && (isfunc(p
->type
->type
) || isarray(p
->type
->type
)))
142 p
= retype(p
, p
->type
->type
);
148 case '&': t
= gettok(); p
= unary(); if (isarray(p
->type
) || isfunc(p
->type
))
149 p
= retype(p
, ptr(p
->type
));
152 if (isaddrop(p
->op
) && p
->u
.sym
->sclass
== REGISTER
)
153 error("invalid operand of unary &; `%s' is declared register\n", p
->u
.sym
->name
);
155 else if (isaddrop(p
->op
))
156 p
->u
.sym
->addressed
= 1;
158 case '+': t
= gettok(); p
= unary(); p
= pointer(p
);
159 if (isarith(p
->type
))
160 p
= cast(p
, promote(p
->type
));
162 typeerror(ADD
, p
, NULL
); break;
163 case '-': t
= gettok(); p
= unary(); p
= pointer(p
);
164 if (isarith(p
->type
)) {
165 Type ty
= promote(p
->type
);
167 if (isunsigned(ty
)) {
168 warning("unsigned operand of unary -\n");
169 p
= simplify(ADD
, ty
, simplify(BCOM
, ty
, p
, NULL
), cnsttree(ty
, 1UL));
171 p
= simplify(NEG
, ty
, p
, NULL
);
173 typeerror(SUB
, p
, NULL
); break;
174 case '~': t
= gettok(); p
= unary(); p
= pointer(p
);
175 if (isint(p
->type
)) {
176 Type ty
= promote(p
->type
);
177 p
= simplify(BCOM
, ty
, cast(p
, ty
), NULL
);
179 typeerror(BCOM
, p
, NULL
); break;
180 case '!': t
= gettok(); p
= unary(); p
= pointer(p
);
181 if (isscalar(p
->type
))
182 p
= simplify(NOT
, inttype
, cond(p
), NULL
);
184 typeerror(NOT
, p
, NULL
); break;
185 case INCR
: t
= gettok(); p
= unary(); p
= incr(INCR
, pointer(p
), consttree(1, inttype
)); break;
186 case DECR
: t
= gettok(); p
= unary(); p
= incr(DECR
, pointer(p
), consttree(1, inttype
)); break;
187 case TYPECODE
: case SIZEOF
: { int op
= t
;
193 if (istypename(t
, tsym
)) {
197 p
= postfix(expr(')'));
206 p
= cnsttree(inttype
, (long)ty
->op
);
208 if (isfunc(ty
) || ty
->size
== 0)
209 error("invalid type argument `%t' to `sizeof'\n", ty
);
210 else if (p
&& rightkid(p
)->op
== FIELD
)
211 error("`sizeof' applied to a bit field\n");
212 p
= cnsttree(unsignedlong
, (unsigned long)ty
->size
);
216 if (istypename(t
, tsym
)) {
217 Type ty
, ty1
= typename(), pty
;
223 ty2
= qual(CONST
, ty2
);
225 ty2
= qual(VOLATILE
, ty2
);
229 p
= pointer(unary());
233 if ((isarith(pty
) && isarith(ty
))
234 || (isptr(pty
) && isptr(ty
))) {
238 } else if ((isptr(pty
) && isint(ty
))
239 || (isint(pty
) && isptr(ty
))) {
240 if (Aflag
>= 1 && ty
->size
< pty
->size
)
241 warning("conversion from `%t' to `%t' is compiler dependent\n", p
->type
, ty
);
244 } else if (ty
!= voidtype
) {
245 error("cast from `%t' to `%t' is illegal\n",
249 if (generic(p
->op
) == INDIR
|| ty
->size
== 0)
250 p
= tree(RIGHT
, ty1
, NULL
, p
);
254 p
= postfix(expr(')'));
257 p
= postfix(primary());
262 static Tree
postfix(Tree p
) {
265 case INCR
: p
= tree(RIGHT
, p
->type
,
268 incr(t
, p
, consttree(1, inttype
))),
271 case DECR
: p
= tree(RIGHT
, p
->type
,
274 incr(t
, p
, consttree(1, inttype
))),
284 else if (isptr(q
->type
))
287 p
= (*optree
['+'])(ADD
, pointer(p
), pointer(q
));
288 if (isptr(p
->type
) && isarray(p
->type
->type
))
289 p
= retype(p
, p
->type
->type
);
297 if (isptr(p
->type
) && isfunc(p
->type
->type
))
300 error("found `%t' expected a function\n", p
->type
);
301 ty
= func(voidtype
, NULL
, 1);
302 p
= retype(p
, ptr(ty
));
308 case '.': t
= gettok();
310 if (isstruct(p
->type
)) {
314 if (isaddrop(q
->op
) && q
->u
.sym
->temporary
)
315 p
= tree(RIGHT
, p
->type
, p
, NULL
);
317 error("left operand of . has incompatible type `%t'\n",
321 error("field name expected\n"); break;
322 case DEREF
: t
= gettok();
325 if (isptr(p
->type
) && isstruct(p
->type
->type
)) {
330 error("left operand of -> has incompatible type `%t'\n", p
->type
);
334 error("field name expected\n"); break;
339 static Tree
primary(void) {
345 case FCON
: p
= tree(mkop(CNST
,tsym
->type
), tsym
->type
, NULL
, NULL
);
346 p
->u
.v
= tsym
->u
.c
.v
;
348 case SCON
: if (ischar(tsym
->type
->type
))
349 tsym
->u
.c
.v
.p
= stringn(tsym
->u
.c
.v
.p
, tsym
->type
->size
);
351 tsym
->u
.c
.v
.p
= memcpy(allocate(tsym
->type
->size
, PERM
), tsym
->u
.c
.v
.p
, tsym
->type
->size
);
352 tsym
= constant(tsym
->type
, tsym
->u
.c
.v
);
353 if (tsym
->u
.c
.loc
== NULL
)
354 tsym
->u
.c
.loc
= genident(STATIC
, tsym
->type
, GLOBAL
);
355 p
= idtree(tsym
->u
.c
.loc
); break;
356 case ID
: if (tsym
== NULL
)
358 Symbol p
= install(token
, &identifiers
, level
, FUNC
);
360 if (getchr() == '(') {
361 Symbol q
= lookup(token
, externals
);
362 p
->type
= func(inttype
, NULL
, 1);
365 warning("missing prototype\n");
366 if (q
&& !eqtype(q
->type
, p
->type
, 1))
367 warning("implicit declaration of `%s' does not match previous declaration at %w\n", q
->name
, &q
->src
);
370 q
= install(p
->name
, &externals
, GLOBAL
, PERM
);
378 error("undeclared identifier `%s'\n", p
->name
);
381 if (p
->scope
== GLOBAL
)
393 if (tsym
->sclass
== ENUM
)
394 p
= consttree(tsym
->u
.value
, inttype
);
396 if (tsym
->sclass
== TYPEDEF
)
397 error("illegal use of type name `%s'\n", tsym
->name
);
401 if (level
> PARAM
&& cfunc
&& cfunc
->u
.f
.callee
[0])
402 p
= idtree(cfunc
->u
.f
.callee
[0]);
404 error("illegal use of `%k'\n", FIRSTARG
);
405 p
= cnsttree(inttype
, 0L);
409 error("illegal expression\n");
410 p
= cnsttree(inttype
, 0L);
415 Tree
idtree(Symbol p
) {
418 Type ty
= p
->type
? unqual(p
->type
) : voidptype
;
420 if (p
->scope
== GLOBAL
|| p
->sclass
== STATIC
)
422 else if (p
->scope
== PARAM
) {
424 if (isstruct(p
->type
) && !IR
->wants_argb
)
426 e
= tree(mkop(op
,voidptype
), ptr(ptr(p
->type
)), NULL
, NULL
);
428 return rvalue(rvalue(e
));
430 } else if (p
->sclass
== EXTERN
) {
438 e
= tree(mkop(op
,voidptype
), p
->type
, NULL
, NULL
);
440 e
= tree(mkop(op
,funcptype
), p
->type
, NULL
, NULL
);
442 e
= tree(mkop(op
,voidptype
), ptr(p
->type
), NULL
, NULL
);
449 Tree
rvalue(Tree p
) {
450 Type ty
= deref(p
->type
);
453 return tree(mkop(INDIR
,ty
), ty
, p
, NULL
);
455 Tree
lvalue(Tree p
) {
456 if (generic(p
->op
) != INDIR
) {
457 error("lvalue required\n");
459 } else if (unqual(p
->type
) == voidtype
)
460 warning("`%t' used as an lvalue\n", p
->type
);
463 Tree
retype(Tree p
, Type ty
) {
468 q
= tree(p
->op
, ty
, p
->kids
[0], p
->kids
[1]);
473 Tree
rightkid(Tree p
) {
474 while (p
&& p
->op
== RIGHT
)
484 int hascall(Tree p
) {
487 if (generic(p
->op
) == CALL
|| (IR
->mulops_calls
&&
488 (p
->op
== DIV
+I
|| p
->op
== MOD
+I
|| p
->op
== MUL
+I
489 || p
->op
== DIV
+U
|| p
->op
== MOD
+U
|| p
->op
== MUL
+U
)))
491 return hascall(p
->kids
[0]) || hascall(p
->kids
[1]);
493 Type
binary(Type xty
, Type yty
) {
494 #define xx(t) if (xty == t || yty == t) return t
498 xx(unsignedlonglong
);
501 if ((xty
== longtype
&& yty
== unsignedtype
)
502 || (xty
== unsignedtype
&& yty
== longtype
)) {
503 if (longtype
->size
> unsignedtype
->size
)
513 Tree
pointer(Tree p
) {
514 if (isarray(p
->type
))
515 /* assert(p->op != RIGHT || p->u.sym == NULL), */
516 p
= retype(p
, atop(p
->type
));
517 else if (isfunc(p
->type
))
518 p
= retype(p
, ptr(p
->type
));
522 int op
= generic(rightkid(p
)->op
);
524 if (op
== AND
|| op
== OR
|| op
== NOT
525 || op
== EQ
|| op
== NE
526 || op
== LE
|| op
== LT
|| op
== GE
|| op
== GT
)
529 return (*optree
[NEQ
])(NE
, p
, consttree(0, inttype
));
531 Tree
cast(Tree p
, Type type
) {
538 src
= unqual(p
->type
);
539 if (src
->op
!= dst
->op
|| src
->size
!= dst
->size
) {
542 if (src
->size
< inttype
->size
)
543 p
= simplify(CVI
, inttype
, p
, NULL
);
546 if (src
->size
< inttype
->size
)
547 p
= simplify(CVU
, inttype
, p
, NULL
);
548 else if (src
->size
< unsignedtype
->size
)
549 p
= simplify(CVU
, unsignedtype
, p
, NULL
);
552 p
= retype(p
, inttype
);
555 if (isint(dst
) && src
->size
> dst
->size
)
556 warning("conversion from `%t' to `%t' is undefined\n", p
->type
, type
);
557 p
= simplify(CVP
, super(src
), p
, NULL
);
564 src
= unqual(p
->type
);
566 if (src
->op
!= dst
->op
)
569 p
= simplify(CVI
, dst
, p
, NULL
);
573 Type ssrc
= signedint(src
);
574 Tree two
= cnsttree(longdouble
, (double)2.0);
575 p
= (*optree
['+'])(ADD
,
580 p
, consttree(1, inttype
)), NULL
)),
583 p
, consttree(1, unsignedtype
)), NULL
));
585 p
= simplify(CVU
, dst
, p
, NULL
);
588 if (isunsigned(dst
)) {
589 Type sdst
= signedint(dst
);
590 Tree c
= cast(cnsttree(longdouble
, (double)sdst
->u
.sym
->u
.limits
.max
.i
+ 1), src
);
592 simplify(GE
, src
, p
, c
),
594 cast(cast(simplify(SUB
, src
, p
, c
), sdst
), dst
),
595 cast(cnsttree(unsignedlong
, (unsigned long)sdst
->u
.sym
->u
.limits
.max
.i
+ 1), dst
)),
596 simplify(CVF
, sdst
, p
, NULL
));
598 p
= simplify(CVF
, dst
, p
, NULL
);
605 src
= unqual(p
->type
);
608 if (src
->op
!= dst
->op
|| src
->size
!= dst
->size
)
609 p
= simplify(CVI
, dst
, p
, NULL
);
612 if (src
->op
!= dst
->op
|| src
->size
!= dst
->size
)
613 p
= simplify(CVU
, dst
, p
, NULL
);
616 if (src
->op
!= dst
->op
|| src
->size
!= dst
->size
)
617 p
= simplify(CVF
, dst
, p
, NULL
);
620 if (src
->op
!= dst
->op
)
621 p
= simplify(CVP
, dst
, p
, NULL
);
623 if ((isfunc(src
->type
) && !isfunc(dst
->type
))
624 || (!isfunc(src
->type
) && isfunc(dst
->type
)))
625 warning("conversion from `%t' to `%t' is compiler dependent\n", p
->type
, type
);
627 if (src
->size
!= dst
->size
)
628 p
= simplify(CVP
, dst
, p
, NULL
);
633 return retype(p
, type
);
635 Tree
field(Tree p
, const char *name
) {
637 Type ty1
, ty
= p
->type
;
643 if ((q
= fieldref(name
, ty
)) != NULL
) {
644 if (isarray(q
->type
)) {
646 if (isconst(ty1
) && !isconst(ty
))
647 ty
= qual(CONST
, ty
);
648 if (isvolatile(ty1
) && !isvolatile(ty
))
649 ty
= qual(VOLATILE
, ty
);
650 ty
= array(ty
, q
->type
->size
/ty
->size
, q
->type
->align
);
653 if (isconst(ty1
) && !isconst(ty
))
654 ty
= qual(CONST
, ty
);
655 if (isvolatile(ty1
) && !isvolatile(ty
))
656 ty
= qual(VOLATILE
, ty
);
659 if (YYcheck
&& !isaddrop(p
->op
) && q
->offset
> 0) /* omit */
660 p
= nullcall(ty
, YYcheck
, p
, consttree(q
->offset
, inttype
)); /* omit */
662 p
= simplify(ADD
+P
, ty
, p
, consttree(q
->offset
, inttype
));
665 p
= tree(FIELD
, ty
->type
, rvalue(p
), NULL
);
667 } else if (!isarray(q
->type
))
671 error("unknown field `%s' of `%t'\n", name
, ty
);
672 p
= rvalue(retype(p
, ptr(inttype
)));
676 /* funcname - return name of function f or a function' */
677 char *funcname(Tree f
) {
679 return stringf("`%s'", f
->u
.sym
->name
);
682 static Tree
nullcheck(Tree p
) {
683 if (!needconst
&& YYnull
&& isptr(p
->type
)) {
685 if (strcmp(YYnull
->name
, "_YYnull") == 0) {
686 Symbol t1
= temporary(REGISTER
, voidptype
);
687 p
= tree(RIGHT
, p
->type
,
689 cond(asgn(t1
, cast(p
, voidptype
))),
690 vcall(YYnull
, voidtype
, (file
&& *file
? pointer(idtree(mkstr(file
)->u
.c
.loc
)) : cnsttree(voidptype
, NULL
)), cnsttree(inttype
, (long)lineno
) , NULL
)),
695 p
= nullcall(p
->type
, YYnull
, p
, cnsttree(inttype
, 0L));
700 Tree
nullcall(Type pty
, Symbol f
, Tree p
, Tree e
) {
704 return retype(nullcall(atop(pty
), f
, p
, e
), pty
);
705 ty
= unqual(unqual(p
->type
)->type
);
708 cnsttree(inttype
, (long)ty
->size
),
709 cnsttree(inttype
, (long)ty
->align
),
710 (file
&& *file
? pointer(idtree(mkstr(file
)->u
.c
.loc
)) : cnsttree(voidptype
, NULL
)), cnsttree(inttype
, (long)lineno
) , NULL
);