admin管理员组文章数量:1302278
I have looked for suggested question that answer mine but did not find any related ones.
I am studying compiler design and have come across the feature that a language supports nested lexical scopes. It means the outer variables are visible to inner functions defined within other functions. However, to do this the information at what stack frame and what offset the variable in question is found is needed. How can a compiler emit this information in the symbol table if we have recursion depth that depends on the user input?
Thanks!
I have looked for suggested question that answer mine but did not find any related ones.
I am studying compiler design and have come across the feature that a language supports nested lexical scopes. It means the outer variables are visible to inner functions defined within other functions. However, to do this the information at what stack frame and what offset the variable in question is found is needed. How can a compiler emit this information in the symbol table if we have recursion depth that depends on the user input?
Thanks!
Share asked Feb 10 at 14:28 Max SedluschMax Sedlusch 1155 bronze badges1 Answer
Reset to default 1Example: Scope A contains scope B which contains scope C. In scope C, there's a reference to variable va
declared in scope A.
When compiling scope C, the compiler knows that va
was declared in A. And although it doesn't know where A's stack frame will be at runtime, it does know that A is "two scopes out" from C. So it thinks of va
as "two scopes out, at offset 1" (say). The object code it generates for the reference to va
is "go 2 steps down the scope chain, then access offset 1".
So at runtime, each stack frame has both a pointer to its 'parent' frame on the stack (so it knows how to set the stack pointer when it returns), and also a pointer to the frame that is 'next out' on the scope chain (so it can resolve references to non-local variables). Setting up these pointers is part of the process of creating a new stack frame.
(Or at least, that's the mental model you should start with. There are optimizations that avoid traversing the scope chain.)
本文标签:
版权声明:本文标题:nested - if the symbol table is created at compile time how can it know the stack frame depth and offset in procedures depending 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741711668a2393878.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论