Class ScopeManager


  • public class ScopeManager
    extends java.lang.Object
    Keeps track of declared variables and maps them to their slots in the stack frames. A stack frame is just an array, with one slot for each variable. There are two kinds of stack frame: the global one, which has top-level variables plus those from the top level of modules. The second type is inside a function.

    When a variable is declared so that it shadows an outer variable those two get different slots, even though they have the same name.

    The slot number combines two values in one: which stack frame the variable resolves to, and its position in that frame. The first bit says which frame, and the rest of the bits are left for the slot number.

    Basically:

    • If first bit set: function frame
    • If first bit not set: global frame.
    • Constructor Detail

      • ScopeManager

        public ScopeManager()
    • Method Detail

      • getStackFrameSize

        public int getStackFrameSize()
      • getParameterSlots

        public java.util.Map<java.lang.String,​java.lang.Integer> getParameterSlots()
      • enterFunction

        public void enterFunction()
        Called when we enter a new function. A function is not just a new scope, because it needs its own stack frame.
      • leaveFunction

        public void leaveFunction()
      • enterScope

        public void enterScope()
        Called when we enter a new lexical scope in which variables can be declared, hiding those declared further out. Although the scopes are nested we flatten them into a single stack frame by simply giving the variables different slots in the same frame. Variable 'v' may map to different slots depending on where in the code it is used.
      • leaveScope

        public void leaveScope()
      • registerParameter

        public int registerParameter​(java.lang.String parameter,
                                     Location loc)
        Registers a parameter to a function.