This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Static chains and pointers to functions
- From: John Lu <jlu at lsil dot com>
- To: gcc at gcc dot gnu dot org
- Date: Thu, 20 Feb 2003 14:08:53 -0600 (CST)
- Subject: Static chains and pointers to functions
- Reply-to: John Lu <jlu at lsil dot com>
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);
}