Lecture 13 Slides - Suraj @ LUMS
Lecture 13 Slides - Suraj @ LUMS
Lecture 13 Slides - Suraj @ LUMS
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
procedure C; begin end;Pascal Code Fragmentprocedure A (P : procedure; i : integer);procedure B;begin Bwrite(i);end B;begin Aif i = 1 then A(B,2)else P;end A;begin mainA(C,1);end main.JavaScript Code Fragmentfunction sub1() {var x;function sub2() {alert(x);};function sub3() {var x;x = 3;sub4(sub2);};function sub4(subx) {var x;x = 4;subx();};x = 1;sub3();};Generic Subprograms in C++template Type max(Type first, Type second) {return first > second ? first :second;}int max(int first, int second){return first > second ? first : second;}import java.util.Hashtable;class TestJava without Generics{public static void main(String[] args) {Hashtable h = new Hashtable();h.put(new Integer(0), "value");String s = (String) h.get(new Integer(0));System.out.println(s); }}1
Java with GenericsCo-routinesimport java.util.Hashtable;class Test {public static void main(String[] args) {Hashtable h =new Hashtable();h.put(new Integer(0), "value");String s = h.get(new Integer(0));System.out.println(s);CoroutinesChapter 10 Topics• The General Semantics of Calls and Returns• Implementing “Simple” Subprograms• Implementing Subprograms with Stack-DynamicLocal Variables• Nested Subprograms• Blocks• Implementing Dynamic Scoping2
The General Semantics of Callsand Returns• Def: The subprogram call and returnoperations of a language are together calledits subprogram linkageImplementing “Simple”Subprograms• Call Semantics:1. Save the execution status of the caller2. Carry out the parameter-passing process3. Pass the return address to the callee4. Transfer control to the calleeImplementing “Simple”Subprograms• Return Semantics:1. If pass-by-value-result parameters are used,move the current values of those parameters totheir corresponding actual parameters2. If it is a function, move the functional value toa place the caller can get it3. Restore the execution status of the caller4. Transfer control back to the callerImplementing “Simple”Subprograms• Required Storage: Status information of thecaller, parameters, return address, andfunctional value (if it is a function)• The format, or layout, of the noncode partof an executing subprogram is called anactivation record• An activation record instance is a concreteexample of an activation record (thecollection of data for a particularsubprogram activation)3
An Activation Record for“Simple” SubprogramsCode andActivationRecords of aProgram with“Simple”SubprogramsImplementing Subprograms withStack-Dynamic Local Variables• More complicated because:– The compiler must generate code to causeimplicit allocation and deallocation of localvariables– Recursion must be supported (adds thepossibility of multiple simultaneous activationsof a subprogram)Typical Activation Record for aLanguage with Stack-DynamicLocal Variables4
Implementing Subprograms withStack-Dynamic Local Variables• The activation record format is static, butits size may be dynamic• The dynamic link points to the top of aninstance of the activation record of thecaller• An activation record instance isdynamically created when a subprogramis calledAn Example C Functionvoid sub(floattotal, int part){int list[4];float sum;…}[4][3][2][1][0]5