dotgnu-libjit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Libjit-developers] how do the nested functions work?


From: ktreichel
Subject: Re: [Libjit-developers] how do the nested functions work?
Date: Sun, 17 Jun 2007 21:33:22 +0200

Hi,

i haven't used thm by now but i'm thinking to use them for cleanup functions.


> -----Ursprüngliche Nachricht-----
> Von: kmeaw <address@hidden>
> Gesendet: 17.06.07 17:47:36
> An: address@hidden
> Betreff: [Libjit-developers] how do the nested functions work?


> 
> Hello. I am trying to figure out, how nested functions are implemented
> in libjit. I have read the online documentation
> (www.southern-storm.com.au/doc/libjit) and still do not understand it.
> 
> For example:
> 
> /* The ANSI C does not allow nested functions, *
>  * I just use C-like syntax to express my      *
>  * problem more clearly.                       */
> 
> void f()
> {
>   int x; /* (1) */
>   void g()
>   {
>     printf ("%d\n", x); /* (2) */
>     /* Nested function references parent variable */
>   }
>   void h()
>   {
>     g(); /* (3) */
>   }
>   g(); /* (4) */
>   h();
> }
> 
> void i()
> {
>   f(); /* (5) */
> }
> 
> When function f gets compiled, x (1) is pushed onto the stack on each
> call. So the address of x is (STACK_POINTER) just after its invocation.
> When function g gets compiled, x is referenced (2) as (STACK_POINTER +
> 1), because *(STACK_POINTER) holds the return address.
> 
> The problem arises, when h is invoked. On the invocation of g from h
> (3), the stack looks like:
> 
> +-------------------------+
> | return address (from g) | <-- *(STACK_POINTER)
> | return address (from h) | <-- *(STACK_POINTER + 1)
> |           x             | <-- *(STACK_POINTER + 2)
> +-------------------------+
> 
> Function g still expects variable x to be located at (STACK_POINTER +
> 1). I have tried to solve this problem by referencing local variables
> and function arguments, using an offset value, relative to the stack
> top, while referencing all other variables (parent variables) by using
> an offset value, relative to the stack bottom. It failes, when one
> calls function f as a nested function from another function i (5).
> 
> Could one tell me, how was this problem solved in libjit? Are there any
> limitations in this solution?

My guess on the implementation is that for nested functions there is no new 
stackframe.
On x86 that means that %ebp is still the same as for the paremt function and 
the locals are accessed %ebp relative.
So only the return address is pushed onto the stack.

> 
> Thanks in advance.
> 
> --
> kmeaw
> 

Cheers,

Klaus


__________________________________________________________________________
Erweitern Sie FreeMail zu einem noch leistungsstärkeren E-Mail-Postfach!        
        
Mehr Infos unter http://produkte.web.de/club/?mc=021131



reply via email to

[Prev in Thread] Current Thread [Next in Thread]