commit ea6f348b4e6b23fdbb93896aac72ea6ace1d8639
Author:     Roberto E. Vargas Caballero <[email protected]>
AuthorDate: Fri Aug 12 11:01:00 2016 +0200
Commit:     Roberto E. Vargas Caballero <[email protected]>
CommitDate: Fri Aug 12 11:01:00 2016 +0200

    [cc2] Add constnode()
    
    This function it is going to be extensively used
    because it is common to do some operation with some
    constant value.

diff --git a/cc2/arch/qbe/cgen.c b/cc2/arch/qbe/cgen.c
index 5144fd8..4f94dde 100644
--- a/cc2/arch/qbe/cgen.c
+++ b/cc2/arch/qbe/cgen.c
@@ -388,19 +388,13 @@ sethi(Node *np)
                break;
        case OCPL:
                np->op = OAND;
-               rp = newnode(OCONST);
-               rp->type = np->type;
-               rp->u.i = 0;
-               rp->u.i = ~np->u.i;
+               rp = constnode(~(TUINT) 0, &np->type);
                goto binary;
        case ONEG:
                np->op = OSUB;
                rp = lp;
-               lp = newnode(OCONST);
-               lp->type = np->type;
-               if (np->type.flags & INTF)
-                       lp->u.i = 0;
-               else
+               lp = constnode(0, &np->type);
+               if ((np->type.flags & INTF) == 0)
                        lp->u.f = 0.0;
        default:
        binary:
diff --git a/cc2/cc2.h b/cc2/cc2.h
index fccc1b3..28f89bf 100644
--- a/cc2/cc2.h
+++ b/cc2/cc2.h
@@ -216,7 +216,7 @@ extern void writeout(void), endinit(void), newfun(void);
 extern void code(int op, Node *to, Node *from1, Node *from2);
 extern void defvar(Symbol *), defpar(Symbol *), defglobal(Symbol *);
 extern void setlabel(Symbol *sym), getbblocks(void);
-extern Node *label2node(Symbol *sym);
+extern Node *label2node(Symbol *sym), *constnode(TUINT n, Type *tp);
 extern Symbol *newlabel(void);
 
 /* node.c */
diff --git a/cc2/code.c b/cc2/code.c
index 8b433c9..9ea9c56 100644
--- a/cc2/code.c
+++ b/cc2/code.c
@@ -77,6 +77,17 @@ label2node(Symbol *sym)
        return np;
 }
 
+Node *
+constnode(TUINT n, Type *tp)
+{
+       Node *np;
+
+       np = newnode(OCONST);
+       np->type = *tp;
+       np->u.i = n;
+       return np;
+}
+
 void
 setlabel(Symbol *sym)
 {

Reply via email to