[hackers] [scc] Remove sizeof nodes || Roberto E. Vargas Caballero

From: <git_AT_suckless.org>
Date: Mon, 20 Jul 2015 19:28:02 +0200 (CEST)

commit 63284fa474d0474e85fd63a0b2fb00878f7efeee
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
AuthorDate: Mon Jul 20 19:23:42 2015 +0200
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
CommitDate: Mon Jul 20 19:23:42 2015 +0200

    Remove sizeof nodes
    
    We need calculate sizeof expressions in cc1, because in other case
    we will not be able of calculate types like:
    
    int a[sizeof(int)];

diff --git a/cc1/cc1.h b/cc1/cc1.h
index f60053f..328343f 100644
--- a/cc1/cc1.h
+++ b/cc1/cc1.h
_AT_@ -13,11 +13,18 @@ typedef struct caselist Caselist;
 typedef struct node Node;
 typedef struct input Input;
 
+/*
+ * TODO: Some of the data stored in type is shared with
+ * cc2, so it should be stored in a table shared
+ * between both programs, and dependant of the target.
+ */
 struct type {
         unsigned char op; /* type builder operator */
         unsigned char ns;
         char letter; /* letter of the type */
         bool defined; /* type defined */
+ size_t size; /* sizeof the type */
+ size_t align; /* align of the type */
         Type *type; /* base type */
         Type *next; /* next element in the hash */
         Type **pars; /* type parameters */
_AT_@ -211,7 +218,6 @@ enum tokens {
 enum {
         OPTR,
         OADD,
- OSIZE,
         OMUL,
         OSUB,
         OINC,
_AT_@ -337,7 +343,7 @@ extern int lexmode;
 
 extern Type *voidtype, *pvoidtype, *booltype,
             *uchartype, *chartype,
- *uinttype, *inttype,
+ *uinttype, *inttype, *sizetp,
             *ushortype, *shortype,
             *longtype, *ulongtype,
             *ullongtype, *llongtype,
diff --git a/cc1/code.c b/cc1/code.c
index c4e946b..e22908e 100644
--- a/cc1/code.c
+++ b/cc1/code.c
_AT_@ -10,7 +10,7 @@
 static void emitbin(unsigned, void *),
             emitcast(unsigned, void *), emitswitch(unsigned, void *),
             emitsym(unsigned, void *), emitfield(unsigned, void *),
- emitsizeof(unsigned, void *), emitexp(unsigned, void *),
+ emitexp(unsigned, void *),
             emitsymid(unsigned, void *), emittext(unsigned, void *),
             emitprint(unsigned, void *), emitfun(unsigned, void *),
             emitret(unsigned, void *), emitdcl(unsigned, void *);
_AT_@ -21,7 +21,6 @@ char *optxt[] = {
         [OMUL] = "*",
         [OINC] = ";+",
         [ODEC] = ";-",
- [OSIZE] = "#",
         [OPTR] = "_AT_",
         [OMOD] = "%",
         [ODIV] = "/",
_AT_@ -72,7 +71,6 @@ void (*opcode[])(unsigned, void *) = {
         [OMUL] = emitbin,
         [OINC] = emitbin,
         [ODEC] = emitbin,
- [OSIZE] = emitsizeof,
         [OPTR] = emitbin,
         [OMOD] = emitbin,
         [ODIV] = emitbin,
_AT_@ -235,13 +233,6 @@ emitbin(unsigned op, void *arg)
 }
 
 static void
-emitsizeof(unsigned op, void *arg)
-{
- Node *np = arg;
- printf("\t#%c", np->left->type->letter);
-}
-
-static void
 emitexp(unsigned op, void *arg)
 {
         Node *np = arg;
_AT_@ -351,7 +342,10 @@ Node *
 sizeofnode(Type *tp)
 {
         Node *np;
+ Symbol *sym;
 
- np = node(0, tp, NULL, NULL);
- return node(OSIZE, inttype, np, NULL);
+ sym = newsym(NS_IDEN);
+ sym->type = sizetp;
+ sym->u.i = tp->size;
+ return constnode(sym);
 }
diff --git a/cc1/types.c b/cc1/types.c
index 5aef90d..5e2011c 100644
--- a/cc1/types.c
+++ b/cc1/types.c
_AT_@ -24,96 +24,128 @@ static Type types[] = {
         },
         { /* 1 = pvoidtype */
                 .op = PTR,
- .letter = L_POINTER
+ .letter = L_POINTER,
+ .size = 2,
+ .align = 2
         },
         { /* 2 = booltype */
                 .op = INT,
                 .letter = L_BOOL,
                 .defined = 1,
+ .size = 1,
+ .align = 1,
                 .n.rank = RANK_BOOL
         },
         { /* 3 = schartype */
                 .op = INT,
                 .letter = L_SCHAR,
                 .defined = 1,
+ .size = 1,
+ .align = 1,
                 .n.rank = RANK_SCHAR
         },
         { /* 4 = uchartype */
                 .op = INT,
                 .letter = L_UCHAR,
                 .defined = 1,
+ .size = 1,
+ .align = 1,
                 .n.rank = RANK_UCHAR
         },
         { /* 5 = chartype */
                 .op = INT,
                 .letter = L_CHAR,
                 .defined = 1,
+ .size = 1,
+ .align = 1,
                 .n.rank = RANK_CHAR
         },
         { /* 6 = ushortype */
                 .op = INT,
                 .letter = L_USHORT,
                 .defined = 1,
+ .size = 2,
+ .align = 1,
                 .n.rank = RANK_USHORT
         },
         { /* 7 = shortype */
                 .op = INT,
                 .letter = L_SHORT,
                 .defined = 1,
+ .size = 2,
+ .align = 1,
                 .n.rank = RANK_SHORT
         },
         { /* 8 = uinttype */
                 .op = INT,
                 .letter = L_UINT,
                 .defined = 1,
+ .size = 2,
+ .align = 1,
                 .n.rank = RANK_UINT
         },
         { /* 9 = inttype */
                 .op = INT,
                 .letter = L_INT,
                 .defined = 1,
+ .size = 2,
+ .align = 1,
                 .n.rank = RANK_INT
         },
         { /* 10 = longtype */
                 .op = INT,
                 .letter = L_LONG,
                 .defined = 1,
+ .size = 4,
+ .align = 1,
                 .n.rank = RANK_LONG
         },
         { /* 11 = ulongtype */
                 .op = INT,
                 .letter = L_ULONG,
                 .defined = 1,
+ .size = 4,
+ .align = 1,
                 .n.rank = RANK_ULONG
         },
         { /* 12 = ullongtype */
                 .op = INT,
                 .letter = L_ULLONG,
                 .defined = 1,
+ .size = 8,
+ .align = 1,
                 .n.rank = RANK_ULLONG
         },
         { /* 13 = llongtype */
                 .op = INT,
                 .letter = L_LLONG,
                 .defined = 1,
+ .size = 8,
+ .align = 1,
                 .n.rank = RANK_LLONG
         },
         { /* 14 = floattype */
                 .op = FLOAT,
                 .letter = L_FLOAT,
                 .defined = 1,
+ .size = 4,
+ .align = 1,
                 .n.rank = RANK_FLOAT
         },
         { /* 15 = doubletype */
                 .op = FLOAT,
                 .letter = L_DOUBLE,
                 .defined = 1,
+ .size = 8,
+ .align = 1,
                 .n.rank = RANK_DOUBLE
         },
         { /* 16 = ldoubletype */
                 .op = FLOAT,
                 .letter = L_LDOUBLE,
                 .defined = 1,
+ .size = 16,
+ .align = 1,
                 .n.rank = RANK_LDOUBLE
         }
 };
_AT_@ -128,6 +160,8 @@ Type *voidtype = &types[0], *pvoidtype = &types[1],
         *floattype = &types[14], *doubletype = &types[15],
         *ldoubletype = &types[16];
 
+Type *sizetp = &types[8]; /* TODO: This depend of the target */
+
 static Symbol dummy0 = {.u.i = 0, .type = &types[9]},
               dummy1 = {.u.i = 1, .type = &types[9]};
 Symbol *zero = &dummy0, *one = &dummy1;
Received on Mon Jul 20 2015 - 19:28:02 CEST

This archive was generated by hypermail 2.3.0 : Mon Jul 20 2015 - 19:36:15 CEST