[hackers] [scc] Add initializers for non static local variables || Roberto E. Vargas Caballero

From: <git_AT_suckless.org>
Date: Tue, 8 Sep 2015 21:36:52 +0200 (CEST)

commit 3a225104759c18a70e8eb918d88a07111f3a128b
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
AuthorDate: Tue Sep 8 21:35:40 2015 +0200
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
CommitDate: Tue Sep 8 21:35:40 2015 +0200

    Add initializers for non static local variables
    
    This is the simpler case, because we only have to generate
    an assignation.

diff --git a/cc1/cc1.h b/cc1/cc1.h
index 1eae992..8e00760 100644
--- a/cc1/cc1.h
+++ b/cc1/cc1.h
_AT_@ -373,6 +373,7 @@ extern bool isnodecmp(int op);
 extern int negop(int op);
 extern bool cmpnode(Node *np, TUINT val);
 extern Node *decay(Node *np);
+extern Node *assignop(char op, Node *lp, Node *rp);
 
 /* cpp.c */
 extern void icpp(void);
diff --git a/cc1/decl.c b/cc1/decl.c
index 8f23e94..79cfc68 100644
--- a/cc1/decl.c
+++ b/cc1/decl.c
_AT_@ -360,18 +360,28 @@ return_type:
 
 /* TODO: check correctness of the initializator */
 /* TODO: emit initializer */
-static struct node *
+static void
 initializer(Symbol *sym)
 {
+ Type *tp = sym->type;
+ Node *np;
+
         if (accept('{')) {
                 initializer(sym);
                 expect('}');
- } else {
- do {
- expr();
- } while (accept(','));
+ return;
+ }
+ np = expr();
+ if ((np = convert(np, tp, 0)) == NULL)
+ goto bad_initializer;
+ if ((sym->flags & ISLOCAL) == 0) {
+ emit(OEXPR, assignop(OASSIGN, varnode(sym), np));
+ return;
         }
- return NULL;
+ return;
+
+bad_initializer:
+ errorp("invalid initializer");
 }
 
 static Symbol *
_AT_@ -683,13 +693,13 @@ identifier(struct decl *dcl)
                 sym->flags = flags;
         }
 
- if (accept('='))
- initializer(sym);
         /* TODO: disallow initializators in functions */
         /* TODO: check if typedef has initializer */
         /* TODO: check if the variable is extern and has initializer */
         if (sym->token == IDEN && sym->type->op != FTN)
                 emit(ODECL, sym);
+ if (accept('='))
+ initializer(sym);
         return sym;
 
 redeclaration:
diff --git a/cc1/expr.c b/cc1/expr.c
index 2e2c546..d26d919 100644
--- a/cc1/expr.c
+++ b/cc1/expr.c
_AT_@ -421,7 +421,7 @@ array(Node *lp, Node *rp)
         return content(OPTR, np);
 }
 
-static Node *
+Node *
 assignop(char op, Node *lp, Node *rp)
 {
         int force = 0;
Received on Tue Sep 08 2015 - 21:36:52 CEST

This archive was generated by hypermail 2.3.0 : Tue Sep 08 2015 - 21:48:11 CEST