scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit 7a353b8e7e4bfa7b9f9ad26d33f9863489e4b84c
parent 3fdd8924237466b84005f844aaec040a6f5edc90
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue, 15 Apr 2014 08:08:42 +0200

Add + unary, - unary and bit complement

These are unary operators with similar behaviour (from
compiler point of view).

Diffstat:
Mcc.h | 3++-
Mcode.c | 4+++-
Mexpr.c | 42+++++++++++++++++++++++++++++++++++-------
3 files changed, 40 insertions(+), 9 deletions(-)

diff --git a/cc.h b/cc.h @@ -224,7 +224,8 @@ enum { OSHR, OLT, OGT, OGE, OLE, OEQ, ONE, OBAND, OBXOR, OBOR, OASSIGN, OA_MUL, OA_DIV, OA_MOD, OA_ADD, OA_SUB, OA_SHL, OA_SHR, - OA_AND, OA_XOR, OA_OR, OADDR, OAND, OOR + OA_AND, OA_XOR, OA_OR, OADDR, OAND, OOR, + ONEG, OCPL, }; extern void diff --git a/code.c b/code.c @@ -38,7 +38,9 @@ char *opcodes[] = { [OA_AND] = ":&", [OA_XOR] = ":^", [OA_OR] = ":|", - [OADDR] = "a" + [OADDR] = "a", + [ONEG] = "_", + [OCPL] = "~", }; Node * diff --git a/expr.c b/expr.c @@ -333,12 +333,15 @@ postfix(void) } } +static Node *cast(void); + static Node * unary(void) { register Node *np; Type *tp; - char paren; + char paren, op; + uint8_t t; switch (yytoken) { case SIZEOF: @@ -356,15 +359,40 @@ unary(void) } if (paren) expect(')'); - np = sizeofcode(tp); - break; + return sizeofcode(tp); case INC: case DEC: - np = incdec(unary(), (yytoken == INC) ? OINC : ODEC); + op = (yytoken == INC) ? OINC : ODEC; next(); - break; - default: - return postfix(); + return incdec(unary(), op); + /* TODO: case '!': */ + /* TODO: case '&': */ + /* TODO: case '*': */ + case '+': op = 0; break; + case '~': op = OCPL; break; + case '-': op = ONEG; break; + default: return postfix(); + } + + next(); + np = cast(); + t = BTYPE(np->type); + + switch (op) { + case OCPL: + if (t != INT) + goto bad_complement; + case ONEG: + if (!isarith(t)) + goto bad_arithm; + np = unarycode(op, np->type, np); + case 0: + return np; } + +bad_complement: + error("bad operand in bit-complement"); +bad_arithm: + error("bad arithmetic operand in unary expression"); } static Node *