Optimizing Stack Based Architectures? 31
An anonymous reader queries: "I'm currently writing a stack oriented interpreter (*ahem* Managed Code Environment) with complimentary compiler (that will be under MIT license), and was wondering if there have been any advances in stack architecture optimization? Some intense Googling turned up this paper, but it seems a bit dated, and focuses mainly on managing local variables, which is inapplicable to me because my interpreter directly supports local vars. Any thoughts or useful links on the topic would be appreciated."
Hi. (Score:3, Informative)
Re:Hi. (Score:5, Interesting)
You can even make this work if you've got a bunch of functions that call each other recursively with a tail call.
For more tips, I'd recommend reading the source code to another stack machine. Outside of some very hard to read dissertations on a dusty shelf, I doubt that the lore of stack machine optimization can be found anywhere else. This is just one of those cases where "did you Gooogle the thing?" just doesn't apply.
Re:Hi. (Score:1)
Re:Hi. (Score:2)
Re:Hi. (Score:4, Informative)
The algorithm will only operate in constant space correctly if the recursive call doesn't pass a variable by value on the stack that indirectly references another value currently on the stack. Meaning, don't pass a pointer to a stack allocated variable to a tail-call optimized recursive function that will overwrite the current functions stack activation frame. This (and pointer aliasing) are why C can't as a general rule tail-call optimize. A language such as Java can do it, but tail-call optimization sets the stack pointer to the original stack pointer of the preceeding call. Thus giving the current function to the last function's stack address space, which can be a security problem.
Re:Hi. (Score:1)
Indeed it is problematic to mix stack inspection as in Java with tail recursion (Here I'm refering to languages like Scheme which promise to give you tail recursion.) But they can actually work together. See the paper "A Tail-Recursive Machine with Stack Inspe
GNU Vmgen (Score:5, Informative)
It comes with a nice manual that is an interesting read even if you are writing your interpreter by hand: http://www.complang.tuwien.ac.at/anton/vmgen/html
Forth archives (Score:4, Interesting)
A german company even developed a microprocessor specifically designed for running forth.
Unfortunately there was not much internet in those days and no HTML, but I think that is a good timespan to look at in your research.
Look up Forth in MIT's library archives, it is more likely to throw up stuff than Google.
citeseer (Score:2, Informative)
-blue
Complimentary Compiler (Score:5, Funny)
Stack Folding (Score:4, Interesting)
Re:Stack Folding (Score:1)
use a register machine (Score:5, Interesting)
Don't get me wrong, stack machines are certainly the simplest and arguably the most elegant & compact format, and certainly nice to use if you final target is mostly JIT/native code.
If you are interpreting, your primary concern is _speed_. Speed in an interpreter on modern architectures (especially the P4!) is determined by almost constant branch mispredictions for every interpreted instruction (assuming a for(;;) switch(..) {..}; central interpreting loop, which sadly is still the fastest portable way of doing things). So your goal is to have the minimum amount of instructions generated for any particular language construct.
Generating code for a register machine can be just as easy as for a stack machine if you have the right infrastructure (assuming you don't work with a bounded set of registers, which you have no need to in an interpreter).
Register machines do much better here than stack machines, I would estimate about 1.5 times less instructions overal. Don't worry so much about the amount of work inside an instruction, aim to do as much as possible at once without branching as your VM design.
It may be even be worth it to integrate struct field and array indirection as part of your opcode, as once you switch to a register machine, "getfield" type instructions will become your most frequent ones. So having say an "add" instruction that can directly address struct fields for its 3 arguments is going to be a big win (i.e. compile a.b = c.d + e.f into a single instruction). By having a pointer in your stack frame that points to itself, you can even do this for single variables in the same instruction.
Sure - if you never call functions (Score:1, Interesting)
Look at Lua 4 vs. Lua 5 VM (Score:3, Informative)
The source code is very concise and reads nicely. The VM interpreter core loop is < 400 lines in src/lvm.c. Read this along with src/lopcodes.h.
Please read and understand both Lua 4 and Lua 5 VM core BEFORE desig
Barking up the wrong tree (Score:1, Informative)
It's not just about the VM (Score:1, Informative)
There's also the possibility to use a hybrid machine that's mostly
Re:It's not just about the VM (Score:2, Informative)
Re:It's not just about the VM (Score:2, Insightful)
IME if you develop and analyse your instruction set carefully, you can often reduce the amount of 'redundant' instructions necessary in a stack based solution. Let me give you a trivial example. In someth
searching papers: portal.acm.org (Score:3, Insightful)
- Hubert
Re:searching papers: citeseer.ist.psu.edu (Score:1, Informative)
Most paper are free for download.
IEEE is another website or ACM queue.
http://citeseer.ist.psu.edu/cis?cs=1&q=stack+base
Re:Et tu Brute? (Score:2, Informative)
Re:Et tu Brute? (Score:1)
When in doubt, et tu Brute force!
wakka wakka wakka! Is this thing on? Yes, I'll be here all week ladies and gentlemen!
Me too! (Score:1)
Stack Machines - Burroughs / Unisys A Series (Score:1)
From what I know today, the A Series emulator runs on big Intel clusters with very good performance.
You might want to extend your Google search to include the Burroughs/Unisys stack machines. A trip over to comp.sys.unisys with your question may