Skip to content

Commit

Permalink
loops are very cheap
Browse files Browse the repository at this point in the history
  • Loading branch information
vadixidav committed Aug 27, 2018
1 parent fc6a7f6 commit 3a79dca
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion base_instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ One of those things could be to push a set of registers onto the stack at the be

If we use a dedicated loop instruction, we can make it so any time we move back to the beginning of the loop that the loop puts the registers on the stack. This is especially helpful because the first iteration of the loop gets the initial inputs, just as in the above solution, but even more useful because now we can automatically return to the beginning without specifically requiring the PC, which is more useful for branching paths conditionally exiting the loop or doing other things. This does make returning take an extra instruction to `unloop` the loop, which is not otherwise necessary since no loop stack or any special loop state exists.

Another solution is to use tail-call recursion to implement all loops. In this case, the loop control flow is controlled using existing control-flow instructions. To break the loop, one only needs to return. To continue the loop, one only needs to jump to the beginning. In this case we only need a special jump instruction that copies registers onto the stack to avoid manual register reading on every iteration. This solution is the most practical as it avoids adding new instructions and calls in stack machines are cheap.
Another solution is to use tail-call recursion to implement all loops. In this case, the loop control flow is controlled using existing control-flow instructions. To break the loop, one only needs to return. To continue the loop, one only needs to jump to the beginning. In this case we only need a special jump instruction that copies registers onto the stack to avoid manual register reading on every iteration. This solution is the most practical as it avoids adding new instructions and calls in stack machines are very cheap.

- jump load
- Jump and load `n` non-pc registers to the stack.
Expand Down

0 comments on commit 3a79dca

Please sign in to comment.