1 lecture 19: control abstraction (section 8.1-8.2) csci 431 programming languages fall 2002 a...

17
1 Lecture 19: Lecture 19: Control Abstraction Control Abstraction (Section 8.1-8.2) (Section 8.1-8.2) CSCI 431 Programming Languages CSCI 431 Programming Languages Fall 2002 Fall 2002 A compilation of material A compilation of material developed by Felix Hernandez- developed by Felix Hernandez- Campos and Michael Scott Campos and Michael Scott

Upload: ezra-lawrence

Post on 19-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Lecture 19: Control Abstraction (Section 8.1-8.2) CSCI 431 Programming Languages Fall 2002 A compilation of material developed by Felix Hernandez-Campos

11

Lecture 19: Lecture 19: Control AbstractionControl Abstraction

(Section 8.1-8.2)(Section 8.1-8.2)

CSCI 431 Programming LanguagesCSCI 431 Programming Languages

Fall 2002Fall 2002

A compilation of material developed by Felix A compilation of material developed by Felix Hernandez-Campos and Michael ScottHernandez-Campos and Michael Scott

Page 2: 1 Lecture 19: Control Abstraction (Section 8.1-8.2) CSCI 431 Programming Languages Fall 2002 A compilation of material developed by Felix Hernandez-Campos

22

AbstractionAbstraction

• Programming languages support the binding of Programming languages support the binding of names with potentially complex program fragments names with potentially complex program fragments that can be used through an interfacethat can be used through an interface

– Programmers only need to know about the purpose of the Programmers only need to know about the purpose of the fragment rather than its implementation fragment rather than its implementation AbstractionAbstraction

• A A control abstractioncontrol abstraction performs a well-defined performs a well-defined operationoperation

– SubroutinesSubroutines

• A A data abstractiondata abstraction represents information represents information– Data structuresData structures– Most data structures include some number of control Most data structures include some number of control

abstractionsabstractions

Page 3: 1 Lecture 19: Control Abstraction (Section 8.1-8.2) CSCI 431 Programming Languages Fall 2002 A compilation of material developed by Felix Hernandez-Campos

33

SubroutinesSubroutines

• Execute an operation on behalf of a calling program Execute an operation on behalf of a calling program unitunit

• Subroutines can be parameterizedSubroutines can be parameterized– The parameters in the definition of the function are known The parameters in the definition of the function are known

as as formal parametersformal parameters– The parameters passed in the subroutine call are known as The parameters passed in the subroutine call are known as

actual parametersactual parameters or or argumentsarguments– At the time of the call, actual parameters are mapped to At the time of the call, actual parameters are mapped to

formal parametersformal parameters

• FunctionsFunctions are subroutines that return a value, while are subroutines that return a value, while proceduresprocedures are subroutines that do not return a value are subroutines that do not return a value

Page 4: 1 Lecture 19: Control Abstraction (Section 8.1-8.2) CSCI 431 Programming Languages Fall 2002 A compilation of material developed by Felix Hernandez-Campos

44

Subroutine FramesSubroutine Frames

• Each subroutines requires a Each subroutines requires a subroutine framesubroutine frame (a.k.a (a.k.a activation recordactivation record) to keep track of) to keep track of

– Arguments and return valuesArguments and return values– Local variables and temporariesLocal variables and temporaries– Bookkeeping informationBookkeeping information

• When a subroutine returns, its frame is removedWhen a subroutine returns, its frame is removed

1001: A(3)1001: A(3) … …2001: int A(int n) {2001: int A(int n) {

int m = n * n;int m = n * n;return m + A(n-1);return m + A(n-1);

}}

Actual ParametersActual Parameters

Formal ParametersFormal Parameters

Page 5: 1 Lecture 19: Control Abstraction (Section 8.1-8.2) CSCI 431 Programming Languages Fall 2002 A compilation of material developed by Felix Hernandez-Campos

55

Call StackCall StackRecursive SubroutineRecursive Subroutine

AA

AA

AA

AA

AA

AA

Page 6: 1 Lecture 19: Control Abstraction (Section 8.1-8.2) CSCI 431 Programming Languages Fall 2002 A compilation of material developed by Felix Hernandez-Campos

66

C ExampleC Example

• Stack pointer Stack pointer spsp– Top of the frame Top of the frame

stackstack

• Frame pointer Frame pointer fpfp– Access to Access to

arguments and arguments and locals via offset locals via offset of fpof fp

• They differ if They differ if temporary space temporary space is allocated in the is allocated in the stackstack

Page 7: 1 Lecture 19: Control Abstraction (Section 8.1-8.2) CSCI 431 Programming Languages Fall 2002 A compilation of material developed by Felix Hernandez-Campos

77

C ExampleC Example

• Stack pointer Stack pointer spsp– Top of the frame Top of the frame

stackstack

• Frame pointer Frame pointer fpfp– Access to Access to

arguments and arguments and locals via offset locals via offset of fpof fp

• They differ if They differ if temporary temporary space is space is allocated in the allocated in the stackstack

Page 8: 1 Lecture 19: Control Abstraction (Section 8.1-8.2) CSCI 431 Programming Languages Fall 2002 A compilation of material developed by Felix Hernandez-Campos

88

Stack MaintenanceStack Maintenance

• Calling sequence (by caller) and prologue (by callee) Calling sequence (by caller) and prologue (by callee) are executed in the way into the subroutine:are executed in the way into the subroutine:

– Pass parametersPass parameters– Save return addressSave return address– Change program counterChange program counter– Change stack pointer to allocate stack spaceChange stack pointer to allocate stack space– Save registers (including frame pointer)Save registers (including frame pointer)– Change frame pointer to new frameChange frame pointer to new frame– Initialization codeInitialization code

• Separation of tasks?Separation of tasks?– As much as possible in the callee (only once in the As much as possible in the callee (only once in the

program)program)

Page 9: 1 Lecture 19: Control Abstraction (Section 8.1-8.2) CSCI 431 Programming Languages Fall 2002 A compilation of material developed by Felix Hernandez-Campos

99

Stack MaintenanceStack Maintenance

• Epilogue (by callee) is executed in the way out of the Epilogue (by callee) is executed in the way out of the subroutine:subroutine:

– Pass return valuePass return value– Execute finalization codeExecute finalization code– Deallocate stack frame (restore stack pointer)Deallocate stack frame (restore stack pointer)– Restore registersRestore registers

Page 10: 1 Lecture 19: Control Abstraction (Section 8.1-8.2) CSCI 431 Programming Languages Fall 2002 A compilation of material developed by Felix Hernandez-Campos

1010

C ExampleC Example

Calling sequence by Calling sequence by the caller:the caller:

1.1. Caller saves registers Caller saves registers in local variables and in local variables and temporaries spacetemporaries space

2.2. 4 scalar arguments in 4 scalar arguments in registersregisters

3.3. Rest of the arguments Rest of the arguments at the top of the current at the top of the current frameframe

4.4. Return address in Return address in register register rara and jump to and jump to target addresstarget address

Page 11: 1 Lecture 19: Control Abstraction (Section 8.1-8.2) CSCI 431 Programming Languages Fall 2002 A compilation of material developed by Felix Hernandez-Campos

1111

C ExampleC Example

Prologue by the callee:Prologue by the callee:

1.1. Subtract the frame size Subtract the frame size from the spfrom the sp

2.2. Save registers at the Save registers at the beginning of the new beginning of the new frame using sp as the frame using sp as the base for displacementbase for displacement

3.3. Copy the sp into the fpCopy the sp into the fp

Page 12: 1 Lecture 19: Control Abstraction (Section 8.1-8.2) CSCI 431 Programming Languages Fall 2002 A compilation of material developed by Felix Hernandez-Campos

1212

C ExampleC Example

Callee epilogue:Callee epilogue:

1.1. Set the function return Set the function return valuevalue

2.2. Copy fp to sp to Copy fp to sp to deallocate any deallocate any dynamically allocated dynamically allocated spacespace

3.3. Restore registers Restore registers including ra (based on including ra (based on sp)sp)

4.4. Add frame size to spAdd frame size to sp

5.5. Return (jump to ra)Return (jump to ra)

Page 13: 1 Lecture 19: Control Abstraction (Section 8.1-8.2) CSCI 431 Programming Languages Fall 2002 A compilation of material developed by Felix Hernandez-Campos

1313

Pascal ExamplePascal Example

Page 14: 1 Lecture 19: Control Abstraction (Section 8.1-8.2) CSCI 431 Programming Languages Fall 2002 A compilation of material developed by Felix Hernandez-Campos

1414

Parameter PassingParameter Passing

• Pass-by-valuePass-by-value– Input parameterInput parameter

• Pass-by-resultPass-by-result– Output parameterOutput parameter

• Pass-by-value-resultPass-by-value-result– Input/output parameterInput/output parameter

• Pass-by-referencePass-by-reference– Input/Output parameter, no copyInput/Output parameter, no copy

• Pass-by-namePass-by-name– Textual substitutionTextual substitution

Page 15: 1 Lecture 19: Control Abstraction (Section 8.1-8.2) CSCI 431 Programming Languages Fall 2002 A compilation of material developed by Felix Hernandez-Campos

1515

ClosureClosure

• Closure are subroutines that maintain all their Closure are subroutines that maintain all their referencing environmentreferencing environment

– This mechanism is also known as deep bindingThis mechanism is also known as deep binding

• This is significant when subroutines are passed as This is significant when subroutines are passed as argumentsarguments

Page 16: 1 Lecture 19: Control Abstraction (Section 8.1-8.2) CSCI 431 Programming Languages Fall 2002 A compilation of material developed by Felix Hernandez-Campos

1616

Exception HandlingException Handling

• An exception is an unexpected or unusual condition An exception is an unexpected or unusual condition that arises during program executionthat arises during program execution

– Raised by the program or detected by the language Raised by the program or detected by the language implementationimplementation

• Example: read a value after EOF reachedExample: read a value after EOF reached

• Alternatives:Alternatives:– InventInvent the value ( the value (e.g.e.g. –1) –1)– Always return the value and a status code (must be Always return the value and a status code (must be

checked every time)checked every time)– Pass a closure (if available) to handle errorsPass a closure (if available) to handle errors

Page 17: 1 Lecture 19: Control Abstraction (Section 8.1-8.2) CSCI 431 Programming Languages Fall 2002 A compilation of material developed by Felix Hernandez-Campos

1717

Exception HandlingException Handling

• Exception move error-checking out of the Exception move error-checking out of the normalnormal flow of the programflow of the program

– No special values to be returnedNo special values to be returned– No error checking after each callNo error checking after each call

• Exceptions in JavaExceptions in Java– http://java.sun.com/docs/books/tutorial/essential/exceptionhttp://java.sun.com/docs/books/tutorial/essential/exception

s/s/