Friday, June 28, 2013

Chapter 10 - Implementing Subprograms

REVIEW QUESTIONS 1. What is the definition used in this chapter for “simple subprograms”? Subprogram cannot be nested and all local variables are static. 4.What is the task of a linker? The task of a linker is to find the files that contain the translated subprograms referenced in that and load them into memory. 8. What kind of machines often use registers to pass parameters? RISC. 11. What is an EP, and what is its purpose? EP is a point or first address of the activation record instance of the main program. It is required to control the execution of a subprogram. 14. What are two potentialproblems with the static-chain method? - It is difficult for a programmer working on a time-critical program to estimate the costs of nonlocal references, because the cost of each reference depends on the depth of nesting between the reference and the scope of declaration. - Subsequent code modifications may change nesting depths, thereby changing the timing of some references, both in the changed code and possibly in code far from the changes. 17. Describe the shallow-access method of implementing dynamic scoping. Shallow access is an alternative implementation method, not an alternative semantics. The semantics of deep access and shallow access are identical. In the shallow-access method, variables declared in subprograms are not stored in the activation records of those subprograms. PROBLEM SET 6. Although local variables in Java methods are dynamically allocated at the beginning of each activation, under what circumstances could the value of a local variable in a particular activation retain the value of previous activation? If the variable is declared as static. Static modifier is a modifier that makes a variable history – sensitive. 8. Pascal allows gotos with nonlocal targets. How could such statements be handled if static chains were used for nonlocal variable access? Finding the correct activation record instance of a nonlocal variable using static links is relatively straightforward. When a reference is made to nonlocal variable, the activation record instance containing the variable can be found by searching the static chain until a static ancestor activation record instance is found that contains the variable. 9. The static-chain method could be expanded slightly by using two static links in each activation record instance where the second points to the static grandparent activation record instance. How would this approach affect the time required for subprogram linkage and nonlocal references? Including two static links would reduce the access time to nonlocals that are defined in scopes two steps away to be equal to that for nonlocals that are one step away. Overall, because most nonlocal references are relatively close, this could significantly increase the execution efficiency of many programs. 11. If a compiler uses the static chain approach to implementing blocks, which of the entries in the activation records for subprograms are needed in the activation records for blocks? There are two options for implementing blocks as parameterless subprograms: One way is to use the same activation record as a subprogram that has no parameters. This is the most simple way, because accesses to block variables will be exactly like accesses to local variables. Of course, the space for the static and dynamic links and the return address will be wasted. The alternative is to leave out the static and dynamic links and the return address, which saves space but makes accesses to block variables different from subprogram locals.

No comments:

Post a Comment