repair prepare_main_collect2 hack
authorMatthias Braun <matze@braunis.de>
Thu, 20 Oct 2011 12:58:06 +0000 (14:58 +0200)
committerMatthias Braun <matze@braunis.de>
Thu, 20 Oct 2011 12:58:06 +0000 (14:58 +0200)
ast2firm.c
parser.c

index 483b53f..4394f5d 100644 (file)
@@ -5612,10 +5612,6 @@ static void create_function(entity_t *entity)
        if (entity->function.statement == NULL)
                return;
 
-       if (is_main(entity) && enable_main_collect2_hack) {
-               prepare_main_collect2(entity);
-       }
-
        inner_functions     = NULL;
        current_trampolines = NULL;
 
index 92f21d1..23d6405 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -5321,6 +5321,22 @@ warn_unreachable:
        }
 }
 
+static bool is_main(entity_t *entity)
+{
+       static symbol_t *sym_main = NULL;
+       if (sym_main == NULL) {
+               sym_main = symbol_table_insert("main");
+       }
+
+       if (entity->base.symbol != sym_main)
+               return false;
+       /* must be in outermost scope */
+       if (entity->base.parent_scope != file_scope)
+               return false;
+
+       return true;
+}
+
 static void parse_external_declaration(void)
 {
        /* function-definitions and declarations both start with declaration
@@ -5470,6 +5486,9 @@ static void parse_external_declaration(void)
                        }
                }
 
+               if (is_main(entity) && enable_main_collect2_hack)
+                       prepare_main_collect2(entity);
+
                POP_PARENT();
                assert(current_function == function);
                assert(current_entity   == entity);
@@ -10573,6 +10592,8 @@ static void complete_incomplete_arrays(void)
 
 void prepare_main_collect2(entity_t *entity)
 {
+       PUSH_SCOPE(&entity->function.statement->compound.scope);
+
        // create call to __main
        symbol_t *symbol         = symbol_table_insert("__main");
        entity_t *subsubmain_ent
@@ -10599,6 +10620,8 @@ void prepare_main_collect2(entity_t *entity)
 
        expr_statement->base.next = compounds->statements;
        compounds->statements     = expr_statement;
+
+       POP_SCOPE();
 }
 
 void parse(void)