-
Notifications
You must be signed in to change notification settings - Fork 6
Virtual Registers
Dibyendu Majumdar edited this page May 31, 2025
·
23 revisions
Compilers often use named slots to represent locations where a value will be stored. These named slots are virtual in the sense that they may be mapped in the end to a CPU register or a function's stack frame. In our compiler we use the term Register to denote such a virtual slot.
Instructions can define a Register, or use one or more Registers.
There are several requirements we need to meet when deciding how to represent these virtual registers.
- The first requirement is that every variable declared in a function must get a unique slot. In the source language a variable name may be reused in multiple scopes, but from a compiler's perspective, each such variable is unique, because each has a unique lifetime and type.
Consider this example:
func foo(x: Int)
{
if (x == 0)
{
var i = 0
}
if (x == 1)
{
var i = 1
}
}
The variable i has two defined instances, each has a different lifetime. From a compiler's standpoint, each instance of i must be mapped to a distinct virtual register.