scc

Simple C99 Compiler
Log | Files | Refs | README | LICENSE

commit 6f2043e9eb7f2eb1a4ab6d4ac58d59f2e36f77a6
parent b9df40911c422cfbfddcda16321dab9736e9d1c1
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Thu, 29 May 2014 08:50:41 +0200

Merge common code in assign and incdec

This functions must check if the left value is a correct one, so
this part is common to them, so the best option is to create
a new function.

Diffstat:
cc1/expr.c | 27++++++++++++++-------------
cc1/stmt.c | 41++++++++++++++++++-----------------------
2 files changed, 32 insertions(+), 36 deletions(-)

diff --git a/cc1/expr.c b/cc1/expr.c @@ -58,6 +58,17 @@ typeconv(Node **p1, Node **p2) *p2 = np2; } +static void +chklvalue(Node *np, Type *tp) +{ + if (!np->b.lvalue) + error("lvalue required in operation"); + if (np->utype == voidtype) + error("invalid use of void expression"); + if (isconst(tp->op)) + error("const value modified"); +} + Node * eval(Node *np) { @@ -341,12 +352,7 @@ incdec(Node *np, char op) { Type *tp = np->utype; - if (!np->b.lvalue) - error("lvalue required in operation"); - if (np->utype == voidtype) - error("invalid use of void expression"); - if (isconst(tp->op)) - error("const value modified"); + chklvalue(np, np->utype); switch (np->typeop) { case PTR: @@ -533,7 +539,7 @@ mul(void) switch (yytoken) { case '*': op = OMUL; fun = arithmetic; break; case '/': op = ODIV; fun = arithmetic; break; - case '%': op = OMOD; fun = integerop; break; + case '%': op = OMOD; fun = integerop; break; default: return np; } next(); @@ -708,12 +714,7 @@ assign(void) case OR_EQ: op = OA_OR; fun = integerop; break; default: return np; } - if (!np->b.lvalue) - error("lvalue required as left operand of assignment"); - if (np->utype == voidtype) - error("invalid use of void expression"); - if (isconst(np->type->op)) - error("const value modified"); + chklvalue(np, np->type); next(); np = (fun)(op, np, eval(assign())); } diff --git a/cc1/stmt.c b/cc1/stmt.c @@ -29,16 +29,12 @@ label(char *s, char define) { Symbol *sym; - if (s) { - if ((sym = lookup(s, NS_LABEL)) != NULL) { - if (define && sym->s.isdefined) - error("label '%s' already defined", s); - else - sym->s.isdefined = 1; - return sym; - } - } else { - s = ""; + if ((sym = lookup(s, NS_LABEL)) != NULL) { + if (define && sym->s.isdefined) + error("label '%s' already defined", s); + else + sym->s.isdefined = 1; + return sym; } sym = install(s, NS_LABEL); @@ -72,9 +68,9 @@ While(Caselist *lswitch) Symbol *begin, *cond, *end; Node *np; - begin = label(NULL, 1); - end = label(NULL, 1); - cond = label(NULL, 1); + begin = label("", 1); + end = label("", 1); + cond = label("", 1); expect(WHILE); np = condition(); @@ -94,9 +90,9 @@ For(Caselist *lswitch) Symbol *begin, *cond, *end; Node *econd = NULL, *einc = NULL; - begin = label(NULL, 1); - end = label(NULL, 1); - cond = label(NULL, 1); + begin = label("", 1); + end = label("", 1); + cond = label("", 1); expect(FOR); expect('('); @@ -124,8 +120,7 @@ For(Caselist *lswitch) static void Dowhile(Caselist *lswitch) { - Symbol *begin= label(NULL, 1), *end = label(NULL, 1); - + Symbol *begin = label("", 1), *end = label("", 1); expect(DO); emitbloop(); @@ -206,7 +201,7 @@ Switch(Symbol *lcont) { Caselist lcase = {.nr = 0, .head = NULL, .deflabel = NULL}; struct scase *p; - Symbol *lbreak = label(NULL, 1), *lcond = label(NULL, 1); + Symbol *lbreak = label("", 1), *lcond = label("", 1); Node *cond; expect(SWITCH); @@ -230,7 +225,7 @@ static void Case(Symbol *lbreak, Symbol *lcont, Caselist *lswitch) { Node *np; - Symbol *lcase = label(NULL, 1); + Symbol *lcase = label("", 1); struct scase *pcase; if (!lswitch) @@ -253,7 +248,7 @@ Case(Symbol *lbreak, Symbol *lcont, Caselist *lswitch) static void Default(Symbol *lbreak, Symbol *lcont, Caselist *lswitch) { - Symbol *ldefault = label(NULL, 1); + Symbol *ldefault = label("", 1); expect(DEFAULT); expect(':'); @@ -264,7 +259,7 @@ Default(Symbol *lbreak, Symbol *lcont, Caselist *lswitch) static void If(Symbol *lbreak, Symbol *lcont, Caselist *lswitch) { - Symbol *end, *lelse = label(NULL, 1); + Symbol *end, *lelse = label("", 1); Node *np; expect(IF); @@ -273,7 +268,7 @@ If(Symbol *lbreak, Symbol *lcont, Caselist *lswitch) emitjump(lelse, np); stmt(lbreak, lcont, lswitch); if (accept(ELSE)) { - end = label(NULL, 1); + end = label("", 1); emitjump(end, NULL); emitlabel(lelse); stmt(lbreak, lcont, lswitch);