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: Aleksey Demakov
Subject: Re: [Libjit-developers] how do the nested functions work?
Date: Mon, 18 Jun 2007 16:08:36 +0700

Hi,

I never tried nested functions in libjit so I am not sure if they now work
correctly. But the idea is that you cannot access directly the values
that belong to the parent function from within the nested function.

When a nested function is called an additional parameter is automatically
passed to it. This parameter is the pointer to its parent's stack frame.
The nested functions makes access to parent's variables through this
additional parameter.

When one nested function calls another nested function it has to find
the frame pointer of their closest common parent and pass this pointer
to the function being called.

Regards,
Aleksey

On 6/17/07, kmeaw <address@hidden> wrote:
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?

Thanks in advance.

--
kmeaw

_______________________________________________
Libjit-developers mailing list
address@hidden
http://dotgnu.org/mailman/listinfo/libjit-developers



reply via email to

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