Add nested-function support in a language the based on a stack-machine

dror.openu@gmail.com
Mon, 12 Feb 2018 11:25:36 -0800 (PST)

          From comp.compilers

Related articles
Add nested-function support in a language the based on a stack-machine dror.openu@gmail.com (2018-02-12)
| List of all articles for this month |
From: dror.openu@gmail.com
Newsgroups: comp.compilers
Date: Mon, 12 Feb 2018 11:25:36 -0800 (PST)
Organization: Compilers Central
Injection-Info: gal.iecc.com; posting-host="news.iecc.com:2001:470:1f07:1126:0:676f:7373:6970"; logging-data="60035"; mail-complaints-to="abuse@iecc.com"
Keywords: code, question, comment
Posted-Date: 12 Feb 2018 15:51:16 EST

Suppose I have a simple C-like programming language:


        int foo() {
                int x = 10;
                int bar(y int) {
                        return y * 2
                }
                return bar() + x
        }


Like you can see, it supports nested functions.


I already implemented the lexing-parsing phases, and now I'm working
on the code generation and the stack-machine. most of the operations,
and the simple flow-control already implemented but I need some
help/ideas how to solve the nested-function task.


The stack-machine has two registers, accumulator, and a temporary register.


Each frame contains: `[arguments, local variables, return address,
caller frame and stack pointer, temporaries and data operations]`. I
read about two ways to implement the nested-function. one, using an
activation-link (also called static-link), and display. Is there any
better idea to handle this? If I'll choose one of these idea, is it
compile time calculation or do I need to store something in runtime?
if it's a runtime thing, what's the time complexity for it? is it O(1)
operation, or something like O(k) where k is the depth of the
function.
[This at least used to be a standard topic in compiler texts. You should
be able to do either in O(1). -John]


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.