scc

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

commit a512be02d9c534110deafcce1e73f452a0fa9068
parent 9e7c2e2a2707f5db37322bf67f7eccf778d42136
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Mon, 14 Apr 2014 09:43:30 +0200

Add ary2ptr()

Each time the name of an array is present in some expression
that is equivalent to a pointer to the first element of the array,
so this expression is going to happen in different places. So
it is a good candidate for being a function.

Diffstat:
cc.h | 2+-
code.c | 3++-
expr.c | 23++++++++++++++++++-----
3 files changed, 21 insertions(+), 7 deletions(-)

diff --git a/cc.h b/cc.h @@ -223,7 +223,7 @@ 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 + OA_AND, OA_XOR, OA_OR, OADDR }; extern void diff --git a/code.c b/code.c @@ -37,7 +37,8 @@ char *opcodes[] = { [OA_SHR] = ":r", [OA_AND] = ":&", [OA_XOR] = ":^", - [OA_OR] = ":|" + [OA_OR] = ":|", + [OADDR] = "a" }; Node * diff --git a/expr.c b/expr.c @@ -37,6 +37,16 @@ bitlogic(char op, Node *np1, Node *np2) return bincode(op, np1->type, np1, np2); } +static Node * +ary2ptr(Node *np) +{ + Type *tp; + + tp = UNQUAL(np->type); + tp = mktype(tp->type, PTR, NULL, 0); + return unarycode(OADDR, tp, np); +} + /* * Convert a Node to a type */ @@ -109,15 +119,16 @@ arithmetic(char op, Node *np1, Node *np2) goto incorrect; } break; - case PTR: case ARY: + case ARY: + np1 = ary2ptr(np1); + tp1 = np1->type; + case PTR: pointer: switch (op) { case OADD: case OSUB: - tp3 = tp1->type; /* TODO: I think tp3 is not needed */ - if (!tp1->defined) + tp3 = tp1->type; + if (!tp3->defined) goto nocomplete; - if (t1 == ARY) - tp1 = mktype(tp1->type, PTR, NULL, 0); if (t2 != INT) goto incorrect; np2 = bincode(OMUL, tp1, @@ -178,6 +189,8 @@ compare(char op, Node *np1, Node *np2) goto incompatibles; } case ARY: + np1 = ary2ptr(np1); + break; case FTN: /* TODO: cover this cases */ case PTR: