[hackers] [scc] [cc2] Add support for parsing structs and member definitions || Roberto E. Vargas Caballero

From: <git_AT_suckless.org>
Date: Tue, 26 Jan 2016 10:57:43 +0100 (CET)

commit eea0139149e4b53947fc3ba850a0b3f4713f3837
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
AuthorDate: Mon Jan 25 20:08:21 2016 +0100
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
CommitDate: Mon Jan 25 20:13:31 2016 +0100

    [cc2] Add support for parsing structs and member definitions
    
    We had to move type to the beginning of Symbol, because composed
    returns the type associated to the Symbol where the type is stored,
    but we need the symbol to store the name

diff --git a/cc2/cc2.h b/cc2/cc2.h
index 9ce3bd6..861babf 100644
--- a/cc2/cc2.h
+++ b/cc2/cc2.h
_AT_@ -113,11 +113,12 @@ struct type {
 };
 
 struct symbol {
+ Type type;
         unsigned short id;
         unsigned short numid;
         char *name;
- Type type;
         char kind;
+ TSIZE off;
         Symbol *next;
         Symbol *h_next;
 };
diff --git a/cc2/parser.c b/cc2/parser.c
index 5d84cfd..d700269 100644
--- a/cc2/parser.c
+++ b/cc2/parser.c
_AT_@ -32,11 +32,11 @@ union tokenop {
 
 typedef void parsefun(char *, union tokenop);
 static parsefun type, symbol, getname, unary, binary, ternary, call,
- parameter, constant, aggregate;
+ parameter, constant, composed;
 
 typedef void evalfun(void);
 static evalfun vardecl, beginfun, endfun, endpars, stmt, begininit,
- endinit, array;
+ endinit, array, aggregate, flddecl;
 
 static struct decoc {
         void (*eval)(void);
_AT_@ -49,8 +49,8 @@ static struct decoc {
         [EXTRN] = {vardecl, symbol, .u.op = MEM},
         [PRIVAT] = {vardecl, symbol, .u.op = MEM},
         [LOCAL] = {vardecl, symbol, .u.op = MEM},
- [MEMBER] = {NULL, symbol},
- [LABEL] = {NULL, symbol},
+ [MEMBER] = {flddecl, symbol},
+ [LABEL] = {flddecl, symbol},
 
         [INT8] = {NULL, type, .u.arg = &int8type},
         [INT16] = {NULL, type, .u.arg = &int16type},
_AT_@ -69,9 +69,9 @@ static struct decoc {
         [ELLIPSIS] = {NULL, type, .u.arg = &elipsistype},
 
         [FUNCTION] = {NULL, type, .u.arg = &funtype},
- [VECTOR] = {array, aggregate},
- [UNION] = {NULL, NULL},
- [STRUCT] = {NULL, NULL},
+ [VECTOR] = {array, composed},
+ [UNION] = {aggregate, composed},
+ [STRUCT] = {aggregate, composed},
 
         [ONAME] = {NULL, getname},
         ['{'] = {beginfun},
_AT_@ -122,7 +122,7 @@ static struct decoc {
 };
 
 static void *stack[STACKSIZ], **sp = stack;
-static Symbol *lastsym, *curfun;
+static Symbol *lastsym, *curfun, *lastaggreg;
 static Symbol *params[NR_FUNPARAM];
 static int funpars = -1, sclass, callpars, ininit;
 static Node *stmtp, *callp;
_AT_@ -150,7 +150,7 @@ type(char *token, union tokenop u)
 }
 
 static void
-aggregate(char *token, union tokenop u)
+composed(char *token, union tokenop u)
 {
         Symbol *sym;
 
_AT_@ -323,6 +323,36 @@ endpars(void)
 }
 
 static void
+aggregate(void)
+{
+ Node *align, *size;
+ char *name;
+ Type *tp;
+ Symbol *sym;
+
+ align = pop();
+ size = pop();
+ name = pop();
+ tp = pop();
+
+ tp->size = size->u.i;
+ tp->align = align->u.i;
+ /*
+ * type is the first field of Symbol so we can obtain the
+ * address of the symbol from the address of the type.
+ * We have to do this because composed returns the pointer
+ * to the type, but in this function we also need the
+ * symbol to store the name.
+ */
+ sym = (Symbol *) tp;
+ lastaggreg = sym;
+ sym->name = name;
+
+ delnode(align);
+ delnode(size);
+}
+
+static void
 array(void)
 {
         Type *tp, *base;
_AT_@ -374,6 +404,31 @@ vardecl(void)
 }
 
 static void
+flddecl(void)
+{
+ Node *off, *np;
+ char *name;
+ Type *tp;
+ Symbol *sym;
+
+ if (!lastaggreg)
+ error(ESYNTAX);
+
+ off = pop();
+ name = pop();
+ tp = pop();
+ np = pop();
+
+ sym = np->u.sym;
+ sym->off = off->u.i;
+ sym->name = name;
+ sym->type = *tp;
+
+ delnode(np);
+ delnode(off);
+}
+
+static void
 stmt(void)
 {
         static Node *lastp;
Received on Tue Jan 26 2016 - 10:57:43 CET

This archive was generated by hypermail 2.3.0 : Tue Jan 26 2016 - 11:00:36 CET