img
Question:
Published on: 29 March, 2024

What is activation record? Explain clearly the components of an activation record.

Answer:

Activation Record: A data structure containing the important state information for a particular instance of a function call (or something that resembles a function call). May exist entirely in memory, or be partially stored in registers. Typically, an ActivationRecord contains the following:

  • A pointer to the ActivationRecord of the caller (commonly known as the DynamicChain), though in a language with ContinuationPassingStyle even this isn't necessary (when the function returns, it does so via the supplied continuation; rather than the DynamicChain).
  • In languages which allow nested LexicalScoping, a pointer to the ActivationRecord of the enclosing scope (the ReferencingEnvironment) - this pointer is called the StaticChain. In most such languages, it is generally a requirement that somewhere up the call chain, the enclosing function have been called (but in concurrent languages it may have been called in a different thread). In languages like C/C++ where nested LexicalScopes are disallowed (or restricted), a StaticChain is unnecessary. Java InnerClasses also seem to be designed in order to avoid a StaticChain.
  • Local variables (frequently cached in registers)
  • Actual parameters to the function (also frequently cached in registers)
  • Space for the (eventual) return value of the function.

In many (most) programming languages, ActivationRecords maintains a strict LastInFirstOut discipline and is thus stored on a stack (often referred to as TheStack). For this reason, ActivationRecords is also commonly known as stack frames. In the context of languages (like C) where a stack is always used; this terminology is correct. As other languages don't use stacks for this purpose (many Smalltalk implementations, ActivationRecord is a more correct term when talking about languages in general.

  • This scheme works well, unless you have the following features in your language (which violate LIFO discipline)
  • FirstClass LexicalClosures (a closure can outlive its referencing environment)
  • Continuations, excluding UpwardContinuation
  • CoRoutines, and a few other control structures that are found in various languages.

 

Each activation record is a separate heap object, chained together by a pointer to its caller. Continuation capture saves the pointer to the current record, which prevents it and all stack frames under it from being garbage collected. Continuation reinstatement restores the PC, registers, and current record. Stack frames are allocated normally, but when a continuation is captured, the whole stack is copied and saved in the continuation. Continuation reinstatement restores the register state and sets the stack and frame pointers to the continuation's stack. The old stack can be deallocated or freed by the garbage collector.

Random questions