3 ms·
I think where you win with the register machine is in compiling expressions where you have lots of common subexpressions. Once you have the value of a repeated
by kazinator 12y ago
I think where you win with the register machine is in compiling expressions where you have lots of common subexpressions.
Once you have the value of a repeated subexpression in a register, you can just refer to that register.
On a stack machine, your "register" is the top of the stack, and each time you refer to it with a pop, you lose it.
a = expr1 + expr2;
b = expr1 - expr2;
c = f(expr1, expr2);
Register code:
# code to evaluate expr1 to R1
# code to evaluate expr2 to R2
add R5, R1, R2
sub R6, R1, R2
call f # R1-R4 are used are also used for parm passing
store R5, a
store R6, b
store R1, c
Stack:
# code to evaluate expr1 to top of stack
# code to evaluate expr2 to top of stack
# now what? we want to keep expr1 and expr2 in the
# stack and use these values, too, while
# also leaving intermediate
# results in the stack. "dup, rot, swap hell".
The obvious thing is to create compiler-generated temporary local variables, and shunt out of the stack into these. But that's just registers in disguise!
Basically, register files have the undeniable advantage of random access, which works favorably when expressions are DAGs rather than trees.
- Dewie 12y agoWhile we're at it, maybe we might relate this to consuming values by using them, and creating explicit copies when we need to consume them more than once. This avoids having to deal with any kind of garbage collection or reference counting, but you have to do the copying of the whole data structure instead of copying the (sometimes/often) smaller pointers to the data itself (random access). There was some work on this by someone, called something like Linear Lisp. It looked like Forth with parenthesis.
- DougMerritt 12y agoThe illustrious Henry Baker, based on the slightly earlier (and extremely influential) Linear Logic. http://www.pipeline.com/~hbaker1/LinearLisp.html http://www.pipeline.com/~hbaker1/LinearLisp.html There's a conceptual relationship to SSA (static single assignment) http://en.wikipedia.org/wiki/Static_single_assignment_form http://en.wikipedia.org/wiki/Static_single_assignment_form
- agumonkey 12y agoThis is a wonderful read. Opens your eyes on how different things can be done under the hood.
- evincarofautumn 12y agoStack architectures (at least Forths) typically have multiple stacks, which tend to make both locals and stack shuffling hell unnecessary most of the time. In gforth for example, you can use the return stack to retain temporaries: : expr1 1 ; : expr2 2 ; : f * ; : test expr1 expr2 2>r \ retain both results temporarily 2r@ + \ copy both to data stack as needed 2r@ - 2r@ f 2rdrop \ dispose of temporaries ; test .s \ 3 -1 2
- pron 12y agoMost stack machines have a `dup` instruction that duplicates the value at the top of the stack.