[hackers] [scc] Fix interger constants size || Roberto E. Vargas Caballero

From: <git_AT_suckless.org>
Date: Mon, 7 Sep 2015 16:55:29 +0200 (CEST)

commit 10faf603cace3ffda21153466dfbd9e672bcf656
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
AuthorDate: Mon Sep 7 11:37:24 2015 +0200
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
CommitDate: Mon Sep 7 11:50:27 2015 +0200

    Fix interger constants size
    
    These constant were incorrectly emitted, because the size of the target type
    was ignored, so negative number generated different quantities.
    There were also another problem related to the fact that comparisions were
    always done in the host type, that may be bigger than the target type..

diff --git a/cc1/cc1.h b/cc1/cc1.h
index 82ceff8..1eae992 100644
--- a/cc1/cc1.h
+++ b/cc1/cc1.h
_AT_@ -363,6 +363,7 @@ 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 */
 extern Node *expr(void), *negate(Node *np), *constexpr(void);
diff --git a/cc1/code.c b/cc1/code.c
index 492caa9..0be9d93 100644
--- a/cc1/code.c
+++ b/cc1/code.c
_AT_@ -192,7 +192,7 @@ emitconst(Node *np)
         case PTR:
         case INT:
                 u = (tp->sign) ? (TUINT) sym->u.i : sym->u.u;
- printf("#%c%lX", np->type->letter, sym->u.i);
+ printf("#%c%lX", np->type->letter, sym->u.i & ones(tp->size));
                 break;
         case ARY:
                 /*
diff --git a/cc1/expr.c b/cc1/expr.c
index 651d84c..96e8e56 100644
--- a/cc1/expr.c
+++ b/cc1/expr.c
_AT_@ -14,6 +14,7 @@ cmpnode(Node *np, TUINT val)
 {
         Symbol *sym;
         Type *tp;
+ TUINT mask, nodeval;
 
         if (!np || !np->constant)
                 return 0;
_AT_@ -23,7 +24,9 @@ cmpnode(Node *np, TUINT val)
         switch (tp->op) {
         case PTR:
         case INT:
- return ((tp->sign) ? sym->u.i : sym->u.u) == val;
+ mask = (val > 1) ? ones(np->type->size) : -1;
+ nodeval = (tp->sign) ? sym->u.i : sym->u.u;
+ return (nodeval & mask) == (val & mask);
         case FLOAT:
                 return sym->u.f == val;
         }
diff --git a/cc1/fold.c b/cc1/fold.c
index ab66987..a9e474e 100644
--- a/cc1/fold.c
+++ b/cc1/fold.c
_AT_@ -4,6 +4,19 @@
 #include "../inc/cc.h"
 #include "cc1.h"
 
+
+/* TODO: Add ENUM in the cases */
+
+TUINT
+ones(int nbytes)
+{
+ TUINT v;
+
+ for (v = 0; nbytes--; v |= 255)
+ v <<= 8;
+ return v;
+}
+
 static bool
 addi(TINT l, TINT r, Type *tp)
 {
_AT_@ -135,16 +148,6 @@ rshi(TINT l, TINT r, Type *tp)
         return 1;
 }
 
-static TUINT
-ones(int n)
-{
- TUINT v;
-
- for (v = 1; n--; v |= 1)
- v <<= 1;
- return v;
-}
-
 static bool
 foldint(int op, Symbol *res, TINT l, TINT r)
 {
_AT_@ -462,7 +465,7 @@ identity(int *op, Node *lp, Node *rp)
                 return NULL;
         case OBAND:
                 /* i & ~0 => i */
- if (cmpnode(rp, ~0))
+ if (cmpnode(rp, -1))
                         goto free_right;
                 return NULL;
         case OMOD:
diff --git a/cc1/tests/test019.c b/cc1/tests/test019.c
index e9fe384..8118f74 100644
--- a/cc1/tests/test019.c
+++ b/cc1/tests/test019.c
_AT_@ -22,8 +22,8 @@ A2 I i
         A2 #I4 :I
         A2 #IC :I
         A2 #I8 :I
- A2 #IFFFFFFFD :I
- A2 #IFFFFFFF3 :I
+ A2 #IFFFD :I
+ A2 #IFFF3 :I
         A2 #I1 :I
         A2 #I0 :I
         A2 #I0 :I
Received on Mon Sep 07 2015 - 16:55:29 CEST

This archive was generated by hypermail 2.3.0 : Mon Sep 07 2015 - 17:00:12 CEST