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

Louis Krupp <lkrupp@nospam.pssw.com.invalid>
Tue, 13 Feb 2018 00:42:00 -0700

          From comp.compilers

Related articles
| List of all articles for this month |

From: Louis Krupp <lkrupp@nospam.pssw.com.invalid>
Newsgroups: comp.compilers
Date: Tue, 13 Feb 2018 00:42:00 -0700
Organization: Newshosting.com - Highest quality at a great price! www.newshosting.com
References: 18-02-009
Injection-Info: gal.iecc.com; posting-host="news.iecc.com:2001:470:1f07:1126:0:676f:7373:6970"; logging-data="85254"; mail-complaints-to="abuse@iecc.com"
Keywords: code, GCC
Posted-Date: 13 Feb 2018 11:14:30 EST

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


>Suppose I have a simple C-like programming language: ...
>Like you can see, it supports nested functions.


gcc supports nested functions as an extension to C. Compiling this
program with -O0 -fdump-tree-all and looking at the generated files
might give you an idea of one way to do it:


#include <stdio.h>


int main(void)
{
int k1 = 1;
int p1(void)
{
int k2 = 2;
int p2(void)
{
int k3 = 3;
int p3(void)
{
return k1 + k2 + k3;
}
return p3();
}
return p2();
}
int n;


n = p1();
printf("%d\n", n);


return 0;
}


Apparently, gcc chains stack frames, and accessing uplevel variables,
as when p3 uses k1 and k2, seems like it would take O(n) time, where n
is the nesting level.


The alternative, a display vector, seems like it would be easier to
implement unless you're doing this for a real machine with a real (and
therefore limited) set of registers.


Louis


Post a followup to this message

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