This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Static chains and pointers to functions


Hi,

I can't think of a good way to implement a call through a pointer
to a nested function, so I was wondering how gcc does it.  If the 
pointer can point to functions at different nesting levels, how is the 
static chain correctly maintained?  The only way I can think of doing
it is to use code that dynamically determines the appropriate nesting
level for the function pointer.  Is this what gcc does?

Thanks,
John Lu

P.S. The following code illustrates this situation.

void foo(int d) {
  void depth1(int c) {
    int a=1;
    void modify1() {
      a=1000;
    }
    void depth2() {
      int a=2;
      void modify2() {
	a=1000;
      }

      void end() {
	void (*f)();

	if (c==1) {
	  f=modify1;
	} else if (c==2) {
	  f=modify2;
	}
	f(); /* How is this implemented? */
      }
      end();
      printf("depth2 %d\n",a);
    }
    depth2();
    printf("depth1 %d\n",a);
  }
  
  depth1(d);
}

main() {
  foo(1);
  foo(2);
}



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]