Wednesday, June 26, 2013

Concepts of Programming Languages ---- Chapter 10 Implementing Subprograms

Concepts of Programming Languages ---- Chapter 10 Implementing Subprograms

Name : Fandy Limardi
NIM : 1601210713
Lecture : Tri Djoko Wahjono, Ir., M.Sc. (D0206)
Assignment : Concept of programming languages ---- Chapter 10 Implementing Subprograms


Review Questions
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
5.   What are the two reasons why implementing subprograms with stack-dynamic local variables is more difficult than implementing simple subprograms?
       ·         the compiler must generate code to cause the implicit allocation and deal-location of local 
       variables
       ·         recursion adds the possibility of multiple simultaneous activations of a subprogram which 
       means that there can be more than one instance at a given time.
10. Define static chain, static_depth, nesting depth and chain_offset
       ·         Static chain is a chain of static links that connect certain activation record instances in the 
       stack.
       ·         Static depth is an interger associated with a static scope that indicated how deeply it is 
       nested  in the outermost scope.
       ·         Nesting depth is the length of the static chain needed to reach the correct activation record 
       instance for a nonlocal reference to a variable X
       ·         Chain_offset is the length of static depth of the subprogram containing the reference to X 
       and  declaration for X.
11. What is an EP and what is its purpose?
EP(environment pointer) is used to access parameters and local variables during the execution of a subprogram. Its purpose is used as the base of the offset addressing of the data contents of the activation record instance - parameters and local variable or in other meaning, it only saved versions are stored in the activation record instances as the dynamic links.
15. Explain the two methods of implementing blocks
      ·         deep access
      ·         shallow access
16. Describe the deep-access method of implementing dynamic scoping
The dynamic chain links together all subprogram activation record instances in the reverse of the order in which they were activated. The dynamic chain is exactly what is needed to reference nonlocal variables in a dynamic-scoped language.
17. Describe the shallow-access method of implementing dynamic scoping
An alternative implementation method, not an alternative semantics. 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.
7.   It stated in this chapter that when nonlocal variables are accessed in a dynamic-scoped language using the dynamic chain, variable names must be stored in the activation records with the values. If this were actually done, every nonlocal access would require a sequence of costly string comparisons on names. Design an alternative to these string comparisons that would be faster.
One very simple alternative is to assign integer values to all variable names used in the program. Then the integer values could be used in the activation records, and the comparisons would be between integer values, which are much faster than string comparisons.
8.   Pascal allows gotos with nonlocal targets. How could such statements be handled if static chains were used for nonlocal variable access? Hint: Consider the way the correct activation record instance of the static parent of a newly enacted procedure is found (see Section 10.4.2).
Following the hint stated with the question, the target of every goto in a program could be  as an address and a nesting_depth, where the nesting_depth is the difference between the nesting level of the procedure that contains the goto and that of the procedure containing the target. Then, when a goto is executed, the static chain is followed by the number of links indicated in the nesting_depth of the goto target. The stack top pointer is reset to the top of the activation record at the end of the chain.
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