dotgnu-libjit
[Top][All Lists]
Advanced

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

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


From: kmeaw
Subject: [Libjit-developers] how do the nested functions work?
Date: Sun, 17 Jun 2007 19:44:32 +0400
User-agent: Thunderbird 2.0.0.4 (X11/20070616)

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



reply via email to

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