From mboxrd@z Thu Jan 1 00:00:00 1970 From: Mike Haertel Message-Id: <200207191541.g6JFfXMN025116@ducky.net> To: 9fans@cse.psu.edu Subject: Re: [9fans] useful language extension, or no? In-Reply-To: <200207191253.NAA06845@cthulhu.dircon.co.uk> Date: Fri, 19 Jul 2002 08:41:33 -0700 Topicbox-Message-UUID: d223767a-eaca-11e9-9e20-41e7f4b1d025 >I don't really understand the need to have a pointer to the function >on the stack, but I am sure there is a good reason for it. Thre restricted >scope of the function name, which I guess could be useful, does not seem >to depend on it. In order to call a lexically scoped nested function, a caller needs two pieces of information: 1. the actual address address of the code 2. a pointer to the stack frame for the containing function, sometimes called the static chain pointer. A traditional C "pointer to function" is just (1) since there is no containing function. In order to make "pointer to nested function" have the same representation as "pointer to top level function", GCC creates a small bit of trampline code on the stack that sets up (2), then jumps to (1). Then when a pointer to the nested function is actually needed, GCC passes out the pointer to the trampoline code. The trampoline isn't needed when the call to the nested function is a direct call within the scope of the parent function--it's only needed when the nested function is being called via a function pointer from an unknown scope. So if you never take the address of a nested function, GCC shouldn't generate any trampoline code. The alternative to the trampoline would be for "pointer to function" to become a two-word object, or a pointer to a two-word object, with a changed calling sequence in either case. This would be fine, the C standard would certainly allow it, but it would be binary incompatible with existing calling conventions in most systems.