Related articles |
---|
Stack frames & static predecessors wollenberg@web.de (Till Wollenberg) (2007-08-24) |
Re: Stack frames & static predecessors torbenm@app-3.diku.dk (2007-08-24) |
Re: Stack frames & static predecessors gah@ugcs.caltech.edu (glen herrmannsfeldt) (2007-08-24) |
Re: Stack frames & static predecessors tk@ic.unicamp.br (Tomasz Kowaltowski) (2007-08-24) |
Re: Stack frames & static predecessors bonzini@gnu.org (Paolo Bonzini) (2007-08-24) |
Re: Stack frames & static predecessors gene.ressler@gmail.com (Gene) (2007-08-24) |
Re: Stack frames & static predecessors anton@mips.complang.tuwien.ac.at (2007-08-25) |
[4 later articles] |
From: | Till Wollenberg <wollenberg@web.de> |
Newsgroups: | comp.compilers |
Date: | Fri, 24 Aug 2007 02:10:32 +0200 |
Organization: | Compilers Central |
Keywords: | Pascal, code, question, comment |
Posted-Date: | 23 Aug 2007 20:47:53 EDT |
Hi!
I'm currently preparing for an exam in 'compiler construction' and
wondered how the following problem is solved in real world compilers:
if some code of an inner function ('bar') accesses a local variable
('x') of an outer function ('foo') how is the address (on the stack)
of this variable determined?
The variable is stored in the stack frame of the outer function
(static predecessor of bar), but as far as I can see the callee does
only have a pointer to the stack frame of its dynamic predecessor. Due
to the recursion bar cannot 'know' where the stack frame of foo
actually is.
(I choose Pascal for my example because it needs a language which
supports nested functions.)
| program example;
|
| procedure foo;
| var x: integer;
| procedure bar(n: integer);
| begin
| if (n < 5) then bar(n + 1) else writeln(x);
| end;
| begin
| x:=456;
| bar(1);
| end;
|
| begin.
| foo;
| end;
Best regards, Till.
--
real email: wollenberg (at) web (.dot.) de
[The calling procedure passes in pointers to the stack frames of all
of the lexically enclosing procedures, a structure known as a display.
The code to create the display at each call point is tedious but not
complex, and the techniques have been known since Algol 60, if not
longer. This used to be a standard topic in compiler texts, at least
until the academic world switched from Pascal to C. Take a look at the
x86 ENTER instruction, which is specifically designed to create stack
frames with displays. -John]
Return to the
comp.compilers page.
Search the
comp.compilers archives again.