
A Guide to RISC Microprocessors / Register File Management - peter_d_sherman
https://books.google.com/books?id=shqJUaKB9RMC&pg=PA37
======
peter_d_sherman
>"To manage the register windows, SPARC has three special instructions and two
special registers. The instructions are SAVE. RESTORE, and RETT (return from
trap). The registers are the CWP (current window pointer) and the W1M (window
invalid mask).

 _The CWP serves the same purpose for the register windows that the stack
pointer serves for memory-based stack frames._

The CWP determines which window is current (and, by implication, which windows
are previous and next). The WIM determines which of the windows in the
register file are "valid" or available. For the register windows scheme to
function properly, there must always be one invalid window so that overflow
and underflow can be detected. Overflow (trying to allocate a new window when
the register file is "full") and underflow (trying to deallocate a window when
the register file is "empty") can occur because there are only a few windows
on-chip (typically seven or so) that are trying to contain an arbitrarily long
run-time stack. By always having at least one window marked invalid, a program
is guaranteed to bump into the invalid window as the CWP wraps around the
circular window file. When a program tries to use the window marked as
invalid, a hardware trap is taken to a routine that either flushes some of the
register file contents to memory or restores some of the register file
contents from memory.

In essence, only a portion of the top of the memory-based run-time stack is
always contained in the register file. Another reason to always have one
window marked invalid is to support fast trap processing. SPARC traps
automatically decrement the CWP. In the case when all but one of the windows
is valid, the trap-handling routine will be guaranteed free access to the
window marked as invalid; there is no danger of the trap routine clobbering
valid data in the register file. The RETT instruction automatically increments
the CWP."

