Force "static" variables to be in global memory (grischka-2005-09-25 bugfix 5). This patch was originally posted in grischka's 2005-09-25 email as a "required fix" 5 to compile gcc 2.95. It was tweaked by David A. Wheeler to merge the test case into the standard tcc test suite. The test case was enhanced to ensure that multiple different static variables with the same local name were mapped to different memory locations (if they originated from different scopes). diff -ur tinycc-rl-1.0.0-orig/tcc.c tinycc-rl-1.0.0-grischka5/tcc.c --- tinycc-rl-1.0.0-orig/tcc.c 2007-04-18 14:52:22.000000000 -0400 +++ tinycc-rl-1.0.0-grischka5/tcc.c 2007-05-02 16:27:30.000000000 -0400 @@ -8418,6 +8418,7 @@ extern */ external_sym(v, &type, r); } else { + type.t |= (btype.t & VT_STATIC); // Retain "static". if (type.t & VT_STATIC) r |= VT_CONST; else diff -ur tinycc-rl-1.0.0-orig/tests/tcctest.c tinycc-rl-1.0.0-grischka5/tests/tcctest.c --- tinycc-rl-1.0.0-orig/tests/tcctest.c 2007-04-18 14:52:22.000000000 -0400 +++ tinycc-rl-1.0.0-grischka5/tests/tcctest.c 2007-05-02 16:32:56.000000000 -0400 @@ -80,6 +80,7 @@ void statement_expr_test(void); void asm_test(void); void builtin_test(void); +void static_test(void); int fib(int n); void num(int n); @@ -532,6 +533,7 @@ local_label_test(); asm_test(); builtin_test(); + static_test(); return 0; } @@ -2011,6 +2013,43 @@ } +/* static_stub1 takes a pointer to a function, and returns a pointer to + * a function; that function must accept no parameters, and return nothing. */ +void ((*static_stub1(void ((*p)(void)))) (void)) +{ + static void (*pfn)(void); + void (*mytemp)(void); + mytemp = pfn; + pfn = p; + return mytemp; +} + +/* Another stub. The static "pfn" is in a different function, so it'd better + not be mapped to the same memory location. */ +void ((*static_stub2(void ((*p)(void)))) (void)) +{ + static void (*pfn)(void); + void (*mytemp)(void); + mytemp = pfn; + pfn = p; + return mytemp; +} + + +void static_test(void) +{ + void (*result)(void); + result = static_stub1(static_test); + printf("static_test Expect 1 -> %d\n", result == 0); + result = static_stub1(static_test); + printf("static_test Expect 1 -> %d\n", result == static_test); + result = static_stub2(static_test); + printf("static_test Expect 1 -> %d\n", result == 0); + result = static_stub2(static_test); + printf("static_test Expect 1 -> %d\n", result == static_test); +} + + void const_func(const int a) { }