Lec12-Subroutine and Control Abstraction-Mips

Lecture Overview

  • Topic: Subroutine and Control Abstraction

  • Instructor: Dr. Jianhui Yue

  • Course: CS 4121, Fall 2024, Michigan Tech University

  • Materials Adaptation: Based on Dr. Zhenlin Wang's slides

Outline

  • Introduction to Scope

  • Activation Records

  • Static Scope

    • Nested Subroutines

    • Handle Nested Scope

    • Access to Local Variables of Other Procedures

  • Dynamic Scope

  • Shallow and Deep Binding

  • Parameter Passing

  • Responsibility of Caller and Callee

    • Caller and Callee

    • MIPS Calling Convention

  • Aliasing and Overloading

Objectives

  • Understand bindings of variable references with nested scope using static and dynamic scope.

  • Determine storage methods for global, local, and parameter variables.

  • Describe methods for passing parameters during function or procedure calls.

  • Differentiate responsibilities of caller versus callee in a linkage convention.

  • Analyze the meaning of programs involving aliases and overloading.

Procedure Abstraction - Issues

  • Assigning storage for variables and compiler temporaries.

  • Generating code for computing addresses not known at compile time.

  • Interface with libraries, programs, languages, and the OS.

Abstractions Provided

  • Name Space:

    • Local variables are not visible to outer procedures, helping obscure non-locals.

    • Each invocation has its own set of local variables.

  • Control Abstraction:

    • A simple mechanism for procedure invocation and return.

    • Code for invocation can be generated without knowledge of the callee's source code.

  • External Interface:

    • Ensures the safety of data when calling external procedures.

Namespace: What’s in a Name?

  • Example in C to illustrate scope and variable bindings:

    • main() allocates memory, writes formatted output, and concatenates strings.

  • Understanding variable names and their meanings is context-dependent.

Static vs Dynamic Scoping

  • Static Scoping:

    • Also known as lexical scoping; determined at compile time.

    • Scope is resolved using the lexically closest definition.

  • Dynamic Scoping:

    • Scope determined at runtime based on the most recent definition.

    • Binding depends on the run-time path of execution.

Activation Records

  • Lifetimes of local variables are tied to the invocation duration.

  • Creation and Destruction:

    • Activation Records are created during procedure calls and destroyed on exit.

  • Storage Contents:

    • Include locals, parameters, compiler temporaries, return addresses, etc.

Example of Activation Records

  • A sample C code demonstrating activation records and inference of local and non-local variables.

Nested Subroutines

  • Languages supporting nested subroutines allow declarations to refer to the closest nested declaration.

  • Names declared are visible and accessible unless hidden by another declaration.

Establishing Addressability in Static Scope

  • Global/static variables reference a label in static data or an offset from a global pointer.

  • For locals, offsets are taken off a frame pointer.

Scoping Support Mechanisms

  • Combination of compile-time modeling of accessible names and runtime code generation for variable access using block-structured symbol tables.

Handling Nested Scopes

  • Operations include inserting names, looking up names, and deleting records at nested levels.

  • A stack of hash tables can manage scope depth and variable references.

Static and Dynamic Links

  • Static Link: Facilitates access to non-local variables by pointing to the parent activation record/frame.

  • Dynamic Link: Points to the activation record of the caller and is used to restore the stack during return sequences.

Parameter Passing Methodologies

  • Introduces various methods such as:

    • Call-by-value: Pass the evaluated result.

    • Call-by-reference: Pass the address of variables.

    • Call-by-value/result: Pass the result and store it back.

    • Call-by-name: Re-evaluate actual parameter each time it's referenced.

Aliasing and Overloading

  • Aliasing: Occurs when multiple names refer to the same memory location, leading to potential confusion in understanding code.

  • Overloading: Assignment of different meanings to a single name based on context, particularly in functions or methods in languages like C++.

Conclusion

  • Understanding subroutines, control abstraction, scopes, and parameter passing techniques is crucial for effective programming and debugging.