Re: The C Stack in interpreters - why?

anton@mips.complang.tuwien.ac.at (Anton Ertl)
14 May 2005 12:15:32 -0400

          From comp.compilers

Related articles
The C Stack in interpreters - why? clearm@comcast.net (2005-05-13)
Re: The C Stack in interpreters - why? Peter_Flass@Yahoo.com (Peter Flass) (2005-05-14)
Re: The C Stack in interpreters - why? haberg@math.su.se (2005-05-14)
Re: The C Stack in interpreters - why? clearm@comcast.net (2005-05-14)
Re: The C Stack in interpreters - why? nmm1@cus.cam.ac.uk (2005-05-14)
Re: The C Stack in interpreters - why? anton@mips.complang.tuwien.ac.at (2005-05-14)
Re: The C Stack in interpreters - why? haberg@math.su.se (2005-05-14)
Re: The C Stack in interpreters - why? nmm1@cus.cam.ac.uk (2005-05-14)
Re: The C Stack in interpreters - why? Marko.Makela@HUT.FI (Marko =?ISO-8859-1?Q?M=E4kel=E4?=) (2005-05-14)
Re: The C Stack in interpreters - why? marcov@stack.nl (Marco van de Voort) (2005-05-14)
Re: The C Stack in interpreters - why? gah@ugcs.caltech.edu (glen herrmannsfeldt) (2005-05-15)
Re: The C Stack in interpreters - why? gene@abhost.us (Gene Wirchenko) (2005-05-15)
[4 later articles]
| List of all articles for this month |
From: anton@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups: comp.compilers
Date: 14 May 2005 12:15:32 -0400
Organization: Institut fuer Computersprachen, Technische Universitaet Wien
References: 05-05-072
Keywords: code, C
Posted-Date: 14 May 2005 12:15:32 EDT

clearm@comcast.net writes:
>I am trying to understand why the C stack is used in interpreters
>rather than an explicity built stack on the heap?


You mean, for calling? I cannot tell you why they do it, because I
have used an explicit stack in all the interpreters I have written,
and I recommend that to everyone. My guess as to why they do it is:


It looks natural and obvious to map a call in the source language to a
C call (just like they usually map an addition in the source language
to an addition in C) rather than to decompose it into its parts. I
guess that in many cases not that much thought goes into that
decision.


The only possible advantage that I can see to this method is that it
may make the combination of callbacks and longjmps more
straightforward to implement.


The disadvantages to using C calls are:


- It restricts you in the kind of control flow you can implement.
Tail-call optimization? Backtracking? Coroutines? First-class
continuations? No. Well, maybe you can cobble something together,
but it will certainly be harder than with an explicit implementation
of calls. Essentially, using the C call restricts your source
langauge to C-like control flow.


- It is slower. In addition to the call overhead of your language,
you have to pay for the C call overhead, and that might be
considerable depending on the amount of state that your interpreter
has. Moreover, this choice may also have indirect costs, like putting
some virtual machine registers in global variables (i.e., in memory)
instead of in locals (typically registers), in order to be able to
access them across calls. However, I have not measured these
performance effects (and I don't know that anyone has).


- anton
--
M. Anton Ertl
anton@mips.complang.tuwien.ac.at
http://www.complang.tuwien.ac.at/anton/home.html


Post a followup to this message

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