commit 73173f33f7fe04ab610b0be20afef392eda59fed
parent 9312f29f82dc8833bffdfa6eef5577865056d20c
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Fri, 3 Feb 2017 11:03:28 +0100
[cc1] Move castcode() from fold.c to expr.c
This function was only used in expr.c and it is not related
to the code in fold.c.
Diffstat:
2 files changed, 58 insertions(+), 1 deletion(-)
diff --git a/cc1/cc1.h b/cc1/cc1.h
@@ -420,7 +420,6 @@ extern void freetree(Node *np);
/* fold.c */
extern Node *simplify(int op, Type *tp, Node *lp, Node *rp);
-extern Node *castcode(Node *np, Type *newtp);
extern TUINT ones(int nbytes);
/* expr.c */
diff --git a/cc1/expr.c b/cc1/expr.c
@@ -240,6 +240,64 @@ numericaluop(int op, Node *np)
if (op == OADD)
return np;
return simplify(op, np->type, np, NULL);
+ return node(op, np->type, np, NULL);
+}
+
+/* TODO: check validity of types */
+static Node *
+castcode(Node *np, Type *newtp)
+{
+ TUINT negmask, mask, u;
+ Type *oldtp = np->type;
+ Symbol aux, *sym, *osym = np->sym;
+
+ if (!(np->flags & NCONST))
+ goto noconstant;
+
+ switch (newtp->op) {
+ case PTR:
+ case INT:
+ case ENUM:
+ switch (oldtp->op) {
+ case PTR:
+ case INT:
+ case ENUM:
+ u = (oldtp->prop & TSIGNED) ? osym->u.i : osym->u.u;
+ break;
+ case FLOAT:
+ oldtp = newtp;
+ u = osym->u.f;
+ break;
+ default:
+ goto noconstant;
+ }
+ mask = ones(newtp->size);
+ if (newtp->prop & TSIGNED) {
+ negmask = ~mask;
+ if (u & (negmask >> 1) & mask)
+ u |= negmask;
+ aux.u.i = u;
+ } else {
+ aux.u.u = u & mask;
+ }
+ break;
+ case FLOAT:
+ /* FIXME: The cast can be from another float type */
+ aux.u.f = (oldtp->prop & TSIGNED) ? osym->u.i : osym->u.u;
+ break;
+ default:
+ goto noconstant;
+ }
+
+ sym = newsym(NS_IDEN, NULL);
+ np->type = sym->type = newtp;
+ np->sym = sym;
+ sym->u = aux.u;
+
+ return np;
+
+noconstant:
+ return node(OCAST, newtp, np, NULL);
}
Node *