scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

commit a639f3f1bf3ee7a1645eddac929afc4a253e0c84
parent d02dde25b6faa53013cbbb9894f1539645093ee5
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Sat, 18 Jun 2022 09:01:22 +0200

cc1: Simplify unary() and cast()

A variable was passed to decay or not decay, making complex
to know when it had to happen. This new approach uses the NDECAY
flag in the node to detect the situations where the decay was
not needed.

Diffstat:
Msrc/cmd/cc/cc1/expr.c | 43++++++++++++++++++++++++-------------------
1 file changed, 24 insertions(+), 19 deletions(-)

diff --git a/src/cmd/cc/cc1/expr.c b/src/cmd/cc/cc1/expr.c @@ -540,9 +540,16 @@ static Node * address(int op, Node *np) { Node *new; - Type *tp = np->type; + Type *tp; Symbol *sym = np->sym; + if ((np->flags & NDECAY) != 0) { + new = np->left; + free(np); + np = new; + } + tp = np->type; + /* * ansi c accepts & applied to a function name, and it generates * a function pointer @@ -732,7 +739,7 @@ no_pars: return node(op, rettype, np, par); } -static Node *unary(int); +static Node *unary(void); static Type * typeof(Node *np) @@ -764,7 +771,7 @@ sizeexp(void) tp = typename(); break; default: - tp = typeof(unary(0)); + tp = typeof(unary()); break; } expect(')'); @@ -807,10 +814,10 @@ postfix(Node *lp) } } -static Node *cast(int); +static Node *cast(void); static Node * -unary(int needdecay) +unary(void) { Node *(*fun)(int, Node *), *np; int op; @@ -825,7 +832,7 @@ unary(int needdecay) case '*': op = OPTR; fun = content; break; case SIZEOF: next(); - tp = (yytoken == '(') ? sizeexp() : typeof(unary(0)); + tp = (yytoken == '(') ? sizeexp() : typeof(unary()); if (!(tp->prop & TDEFINED)) errorp("sizeof applied to an incomplete type"); return sizeofnode(tp); @@ -833,33 +840,31 @@ unary(int needdecay) case DEC: op = (yytoken == INC) ? OA_ADD : OA_SUB; next(); - np = incdec(unary(1), op); - goto chk_decay; + np = incdec(unary(), op); + goto decay; case DEFINED: return defined(); default: np = postfix(primary()); - goto chk_decay; + goto decay; } next(); - np = (*fun)(op, cast(op != OADDR)); + np = (*fun)(op, cast()); -chk_decay: - if (needdecay) - np = decay(np); - return np; +decay: + return decay(np); } static Node * -cast(int needdecay) +cast(void) { Node *tmp, *np; Type *tp; static int nested; if (!accept('(')) - return unary(needdecay); + return unary(); switch (yytoken) { case TQUALIFIER: @@ -875,7 +880,7 @@ cast(int needdecay) case ARY: error("cast specifies an array type"); default: - tmp = cast(needdecay); + tmp = cast(); if ((np = convert(tmp, tp, 1)) == NULL) error("bad type conversion requested"); np->flags &= ~NLVAL; @@ -901,7 +906,7 @@ mul(void) Node *np, *(*fun)(int, Node *, Node *); int op; - np = cast(1); + np = cast(); for (;;) { switch (yytoken) { case '*': op = OMUL; fun = arithmetic; break; @@ -910,7 +915,7 @@ mul(void) default: return np; } next(); - np = (*fun)(op, np, cast(1)); + np = (*fun)(op, np, cast()); } }