commit 73173f33f7fe04ab610b0be20afef392eda59fed
Author:     Roberto E. Vargas Caballero <[email protected]>
AuthorDate: Fri Feb 3 11:03:28 2017 +0100
Commit:     Roberto E. Vargas Caballero <[email protected]>
CommitDate: Fri Feb 3 11:03:28 2017 +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.

diff --git a/cc1/cc1.h b/cc1/cc1.h
index e9eac6f..22c5c11 100644
--- 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
index 5497a26..8cd8b59 100644
--- 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 *

Reply via email to