]> git.rm.cloudns.org Git - xonotic/gmqcc.git/commitdiff
let ast_node have a use-counter, helpful for the parser to delete unused fields which...
authorWolfgang (Blub) Bumiller <blub@speed.at>
Sat, 18 Aug 2012 14:16:43 +0000 (16:16 +0200)
committerWolfgang (Blub) Bumiller <blub@speed.at>
Sat, 18 Aug 2012 14:16:43 +0000 (16:16 +0200)
ast.c
ast.h
parser.c

diff --git a/ast.c b/ast.c
index e4d18592b8d0b6480194eefe1c1485b0d676c58a..d776e6e1e3635bad585882162fee500f89d1c7d4 100644 (file)
--- a/ast.c
+++ b/ast.c
@@ -57,6 +57,7 @@ static void ast_node_init(ast_node *self, lex_ctx ctx, int nodetype)
     self->node.context = ctx;
     self->node.destroy = &_ast_node_destroy;
     self->node.keep    = false;
+    self->node.uses    = 0;
     self->node.nodetype = nodetype;
 }
 
@@ -255,6 +256,9 @@ ast_binary* ast_binary_new(lex_ctx ctx, int op,
     else
         self->expression.vtype = left->expression.vtype;
 
+    ast_use(left);
+    ast_use(right);
+
     return self;
 }
 
@@ -288,6 +292,10 @@ ast_binstore* ast_binstore_new(lex_ctx ctx, int storop, int op,
     else
         self->expression.next = NULL;
 
+    ast_use(left);
+    ast_use(right);
+    ast_use(left);
+
     return self;
 }
 
@@ -308,6 +316,8 @@ ast_unary* ast_unary_new(lex_ctx ctx, int op,
     self->op = op;
     self->operand = expr;
 
+    ast_use(expr);
+
     return self;
 }
 
@@ -325,6 +335,8 @@ ast_return* ast_return_new(lex_ctx ctx, ast_expression *expr)
 
     self->operand = expr;
 
+    ast_use(expr);
+
     return self;
 }
 
@@ -361,6 +373,9 @@ ast_entfield* ast_entfield_new(lex_ctx ctx, ast_expression *entity, ast_expressi
     self->entity = entity;
     self->field  = field;
 
+    ast_use(entity);
+    ast_use(field);
+
     return self;
 }
 
@@ -400,6 +415,8 @@ ast_member* ast_member_new(lex_ctx ctx, ast_expression *owner, unsigned int fiel
     self->owner = owner;
     self->field = field;
 
+    ast_use(owner);
+
     return self;
 }
 
@@ -424,6 +441,10 @@ ast_ifthen* ast_ifthen_new(lex_ctx ctx, ast_expression *cond, ast_expression *on
     self->on_true  = ontrue;
     self->on_false = onfalse;
 
+    ast_use(cond);
+    if (ontrue)  ast_use(ontrue);
+    if (onfalse) ast_use(onfalse);
+
     return self;
 }
 
@@ -453,6 +474,10 @@ ast_ternary* ast_ternary_new(lex_ctx ctx, ast_expression *cond, ast_expression *
     self->on_false = onfalse;
     self->phi_out  = NULL;
 
+    ast_use(cond);
+    ast_use(ontrue);
+    ast_use(onfalse);
+
     return self;
 }
 
@@ -481,6 +506,12 @@ ast_loop* ast_loop_new(lex_ctx ctx,
     self->increment = increment;
     self->body      = body;
 
+    if (initexpr)  ast_use(initexpr);
+    if (precond)   ast_use(precond);
+    if (postcond)  ast_use(postcond);
+    if (increment) ast_use(increment);
+    if (body)      ast_use(body);
+
     return self;
 }
 
@@ -509,11 +540,20 @@ ast_call* ast_call_new(lex_ctx ctx,
     MEM_VECTOR_INIT(self, params);
 
     self->func = funcexpr;
+    ast_use(funcexpr);
 
     return self;
 }
 MEM_VEC_FUNCTIONS(ast_call, ast_expression*, params)
 
+bool ast_call_add_param(ast_call *self, ast_expression *expr)
+{
+    if (!ast_call_params_add(self, expr))
+        return false;
+    ast_use(expr);
+    return true;
+}
+
 void ast_call_delete(ast_call *self)
 {
     size_t i;
@@ -538,6 +578,9 @@ ast_store* ast_store_new(lex_ctx ctx, int op,
     self->dest = dest;
     self->source = source;
 
+    ast_use(dest);
+    ast_use(source);
+
     return self;
 }
 
@@ -563,6 +606,14 @@ ast_block* ast_block_new(lex_ctx ctx)
 MEM_VEC_FUNCTIONS(ast_block, ast_value*, locals)
 MEM_VEC_FUNCTIONS(ast_block, ast_expression*, exprs)
 
+bool ast_block_add_expr(ast_block *self, ast_expression *expr)
+{
+    if (!ast_block_exprs_add(self, expr))
+        return false;
+    ast_use(expr);
+    return true;
+}
+
 void ast_block_delete(ast_block *self)
 {
     size_t i;
diff --git a/ast.h b/ast.h
index 0059e823fa303c0bc895ea3e7d0a7db4c68e8ce1..4f26742ba50b7d59bbccf51d2b59b43a4c6acfa6 100644 (file)
--- a/ast.h
+++ b/ast.h
@@ -81,8 +81,13 @@ typedef struct
      * prevents its dtor from destroying this node as well.
      */
     bool             keep;
+    /* usecount - so we can delete unused _x,_y and _z nodes... */
+    size_t           uses;
 } ast_node_common;
 
+#define ast_use(n) ((ast_node*)(n))->node.uses++
+#define ast_unuse(n) ((ast_node*)(n))->node.uses--
+
 #define ast_delete(x) ( ( (ast_node*)(x) ) -> node.destroy )((ast_node*)(x))
 #define ast_unref(x) do                     \
 {                                           \
@@ -418,6 +423,8 @@ bool ast_call_codegen(ast_call*, ast_function*, bool lvalue, ir_value**);
 
 MEM_VECTOR_PROTO(ast_call, ast_expression*, params);
 
+bool ast_call_add_param(ast_call*, ast_expression*);
+
 /* Blocks
  *
  */
@@ -435,6 +442,8 @@ bool ast_block_set_type(ast_block*, ast_expression *from);
 MEM_VECTOR_PROTO(ast_block, ast_value*, locals);
 MEM_VECTOR_PROTO(ast_block, ast_expression*, exprs);
 
+bool ast_block_add_expr(ast_block*, ast_expression *expr);
+
 bool ast_block_codegen(ast_block*, ast_function*, bool lvalue, ir_value**);
 
 /* Function
index 24be6ec1dfe1a5a1c6a4e308d171d2393484582b..1ee5cadf2cb030d47fb50ef8808c10ad34c57c0d 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -517,12 +517,12 @@ static bool parser_sy_pop(parser_t *parser, shunt *sy)
 
         case opid1(','):
             if (blocks[0]) {
-                if (!ast_block_exprs_add(blocks[0], exprs[1]))
+                if (!ast_block_add_expr(blocks[0], exprs[1]))
                     return false;
             } else {
                 blocks[0] = ast_block_new(ctx);
-                if (!ast_block_exprs_add(blocks[0], exprs[0]) ||
-                    !ast_block_exprs_add(blocks[0], exprs[1]))
+                if (!ast_block_add_expr(blocks[0], exprs[0]) ||
+                    !ast_block_add_expr(blocks[0], exprs[1]))
                 {
                     return false;
                 }
@@ -900,7 +900,7 @@ static bool parser_close_call(parser_t *parser, shunt *sy)
         if (!params) {
             /* 1 param */
             paramcount = 1;
-            if (!ast_call_params_add(call, sy->out[sy->out_count].out)) {
+            if (!ast_call_add_param(call, sy->out[sy->out_count].out)) {
                 ast_delete(sy->out[sy->out_count].out);
                 parseerror(parser, "out of memory");
                 return false;
@@ -1698,7 +1698,7 @@ static ast_block* parser_parse_block(parser_t *parser)
         }
         if (!expr)
             continue;
-        if (!ast_block_exprs_add(block, expr)) {
+        if (!ast_block_add_expr(block, expr)) {
             ast_delete(expr);
             ast_block_delete(block);
             block = NULL;