9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: Digby Tarvin <digbyt@acm.org>
To: 9fans@cse.psu.edu
Subject: Re: [9fans] useful language extension, or no?
Date: Fri, 19 Jul 2002 19:09:47 +0100	[thread overview]
Message-ID: <200207191809.TAA08184@cthulhu.dircon.co.uk> (raw)
In-Reply-To: <200207191541.g6JFfXMN025116@ducky.net> from Mike Haertel at "Jul 19, 2002 08:41:33 am"

Ah - I see! It hadn't realised that the scope rules now included
that sort of nesting. When did that happen? I guess I am going to
have to trade in my much loved, dog eared K&R for a book on
standard C one of these days...

Presumably I need something along the lines of

  #include <stdio.h>
  int foo(int (*f)()) {
          (*f)();
  }
  
  int main() {
          int g(){; }
          foo(g);
  }

To see it used, producing:

foo:
        pushl %ebp
        movl %esp,%ebp
        subl $20,%esp
        pushl %ebx
        movl 8(%ebp),%ebx
        call *%ebx
.L2:
        movl -24(%ebp),%ebx
        movl %ebp,%esp
        popl %ebp
        ret

Presumably %esp is the stack pointer, %ebp is the frame pointer,
and %ebx is the a scratch register used for parameter passing
and jumping through to get to the trampoline. Makes me curious
about what the 'e' stands for - like I said, I am not an x86 expert...

This seems to require the somewhat less than transparent calling sequence:

main:
        pushl %ebp
        movl %esp,%ebp
        subl $40,%esp
        leal -28(%ebp),%edx
        leal 15(%edx),%eax
        andb $240,%al
        movl $g.6,%edx
        leal 10(%eax),%ecx
        subl %ecx,%edx
        movb $185,(%eax)
        movl %ebp,1(%eax)
        movb $233,5(%eax)
        movl %edx,6(%eax)
        addl $-12,%esp
        leal -28(%ebp),%edx
        leal 15(%edx),%eax
        andb $240,%al
        pushl %eax
        call foo
        addl $16,%esp
.L3:
        movl %ebp,%esp
        popl %ebp
        ret

Presumably $185 generates the instruction to load the frame pointer,
and $233 creates the jump to the actual function.

Thanks for the explanation. The level of knowledge on this list never
ceases to impress me...

Regards,
DigbyT

> 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.
> 


-- 
Digby R. S. Tarvin                                              digbyt@acm.org
http://www.cthulhu.dircon.co.uk


  reply	other threads:[~2002-07-19 18:09 UTC|newest]

Thread overview: 55+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-07-19  0:52 David Gordon Hogan
2002-07-19 12:53 ` Digby Tarvin
2002-07-19 15:41   ` Mike Haertel
2002-07-19 18:09     ` Digby Tarvin [this message]
2002-07-19 18:38       ` Scott Schwartz
2002-07-19 19:07         ` Digby Tarvin
2002-07-29 16:01     ` Ralph Corderoy
  -- strict thread matches above, loose matches on Subject: below --
2002-07-20  5:59 forsyth
2002-07-19 18:35 forsyth
2002-07-19 18:33 David Gordon Hogan
2002-07-19 18:22 David Gordon Hogan
2002-07-19 18:36 ` Digby Tarvin
2002-07-19 15:45 forsyth
2002-07-19 18:19 ` Digby Tarvin
2002-07-19  6:48 forsyth
2002-07-18 19:24 David Gordon Hogan
2002-07-19  4:22 ` Lucio De Re
2002-07-18 15:02 forsyth
2002-07-18 23:57 ` arisawa
2002-07-29 15:59   ` Douglas A. Gwyn
2002-07-18 12:54 Richard Miller
2002-07-17 13:36 rob pike, esq.
2002-07-16 17:31 presotto
2002-07-17  8:58 ` William Josephson
2002-07-17  8:58 ` Douglas A. Gwyn
2002-07-16 17:01 forsyth
2002-07-17  8:59 ` Douglas A. Gwyn
2002-07-16 16:50 rob pike, esq.
2002-07-16 15:59 ` Sam
2002-07-16 17:04   ` Howard Trickey
2002-07-16 16:20     ` Sam
2002-07-16 16:08 rob pike, esq.
2002-07-16 15:31 ` Sam
2002-07-16 15:53 rob pike, esq.
2002-07-16 15:05 ` Sam
2002-07-16 21:29   ` Steve Kilbane
2002-07-16 14:12 Sam
2002-07-16 16:30 ` Boyd Roberts
2002-07-17 12:49 ` Ian Broster
2002-07-17 12:12   ` Sam
2002-07-17 13:33     ` Lucio De Re
2002-07-17 16:12       ` Fariborz (Skip) Tavakkolian
2002-07-17 22:21         ` arisawa
2002-07-17 22:28           ` Ronald G Minnich
2002-07-18  9:51           ` Douglas A. Gwyn
2002-07-18 10:19             ` Lucio De Re
2002-07-18 10:28               ` Lucio De Re
2002-07-18 14:50                 ` Mike Haertel
2002-07-18 14:56                   ` Lucio De Re
2002-07-19  5:23                   ` arisawa
2002-07-18 14:21               ` Douglas A. Gwyn
2002-07-18 14:55                 ` Lucio De Re
     [not found] <20020616170512.14978199BB@mail.cse.psu.edu>
2002-06-16 21:12 ` [9fans] INVESTMENT & ASSISTANCE Digby Tarvin
2002-06-17  0:49   ` Scott Schwartz
2002-06-18  5:23     ` Lucio De Re

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200207191809.TAA08184@cthulhu.dircon.co.uk \
    --to=digbyt@acm.org \
    --cc=9fans@cse.psu.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).