DOC PREVIEW
Berkeley COMPSCI 164 - Lecture Notes

This preview shows page 1-2-3-26-27-28 out of 28 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Lecture #19: More Special Effects---Exceptions and OOPExceptions and ContinuationsApproach I: Do NothingApproach II: Non-Standard ReturnApproach III: Stack manipulationApproach III: DiscussionApproach IV: PC tablesNew Topic: Dynamic Method Selection and OOPI. Fully Dynamic ApproachCharacteristics of Dynamic ApproachImplementing the Dynamic ApproachPros and Cons of Dynamic ApproachII. Straight Single Inheritance, Dynamic TypingImplementing the Smalltalk-like ApproachPros and Cons of Smalltalk ApproachSingle Inheritance with Static TypesImplementation of Simple Static Single InheritanceInterfacesInterface Implementation I: Brute ForceInterface Implementation II: Make Interface Values DifferentInterface Implementation II, IllustratedImproving Interface Implementation IIFull Multiple InteritanceImplementing Full Multiple Inheritance IImplementing Full Multiple Inheritance I (contd.)Implementing Full Multiple Inheritance IILecture #19: More Special Effects—Exceptions andOOPAdministrivia• First autograder ran Wednesday night. I’ll add more tests for thefinal run.• Test #2 on Tuesday, 14 April (in class).Last modified: Thu Apr 2 12:27:02 2009 CS164: Lecture #19 1Exceptions and Continuations• Exception-handling in programming languages is a very limited formof continuation.• Execution continues after a function call that is still active whenexception raised.• Java provides mechanism to return a value with the exception, butthis adds no new complexity.Last modified: Thu Apr 2 12:27:02 2009 CS164: Lecture #19 2Approach I: Do Nothing• Some say keep it simple; don’t bother with exceptions.• Use return code convention:Example: C library functions often return either 0 for OK or non-zero for various degrees of badness.• Problems:Last modified: Thu Apr 2 12:27:02 2009 CS164: Lecture #19 3Approach I: Do Nothing• Some say keep it simple; don’t bother with exceptions.• Use return code convention:Example: C library functions often return either 0 for OK or non-zero for various degrees of badness.• Problems:– Forgetting to check.– Code clutter.– Clumsiness: makes value-returning functions less useful.– Slight cost in always checking return codes.Last modified: Thu Apr 2 12:27:02 2009 CS164: Lecture #19 3Approach II: Non-Standard Return• First idea is to modify calls so that they look like this:call _fjmp OKcode to handle exceptionOK:code for normal return• To throw exception:– Put type of exception in some standard register or memory loca-tion.– Return to instructionafternormal return.• Awkward for the ia32 (above). Easier on machines that allow return-ing to a register+constant offset address [why?].• Exception-handling code decides whether it can handle the excep-tion, and does another exception return if not.• Problem: Requires small distributed overhead for every functioncall.Last modified: Thu Apr 2 12:27:02 2009 CS164: Lecture #19 4Approach III: Stack manipulation• C does not have an exception mechanism built into its syntax, butuses library routines:jmp_buf catch_point;void Caller () {if (setjmp (catch_point) == 0) {normal case, which eventuallygets down to Callee} else {handle exception}}void Callee () {...// Throw exception:longjmp (catch_point, 42);...}...Caller’sframe...otherframes...Callee’sframeCaller’sFP, SP,addr ofsetjmp call& otherscatch point:Last modified: Thu Apr 2 12:27:02 2009 CS164: Lecture #19 5Approach III: Stack manipulation• C does not have an exception mechanism built into its syntax, butuses library routines:jmp_buf catch_point;void Caller () {if (setjmp (catch_point) == 0) {normal case, which eventuallygets down to Callee} else {handle exception}}void Callee () {...// Throw exception:longjmp (catch_point, 42);...}...Caller’sframeCaller’sFP, SP,addr ofsetjmp call& otherscatch point:When longjmp called, re-store stack as indicated bycatch point and return tothe end of the setjmp call.Last modified: Thu Apr 2 12:27:02 2009 CS164: Lecture #19 5Approach III: Discussion• On exception, call to setjmp appears to return twice, with two dif-ferent values.• Does not require help from compiler,• But implementation is architecture-specific.• Overhead imposed on every setjmp call.• If used to implement try and catch, therefore, would impose coston every try.• Subtle problems involving variables that are stored in registers:– The jmpbuf typically has to store such registers, but– That means the value of some local variables may revert unpre-dictably upon a longjmp.Last modified: Thu Apr 2 12:27:02 2009 CS164: Lecture #19 6Approach IV: PC tables• Sun’s Java implementation uses a different approach.• Compiler generates a table mapping instruction addresses (programcounter (PC) values) to exception handlers for each function.• If needed, compiler also leaves behind information necessary to re-turn from a function (“unwind the stack”) when exception thrown.• To throw exception E:while (current PC doesn’t map to handler for E)unwind stack to last caller• Under this approach, a try-catch incurs no cost unless there is anexception, but• Throwing and handling the exception more expensive than other ap-proaches, and• Tables add space.Last modified: Thu Apr 2 12:27:02 2009 CS164: Lecture #19 7New Topic: Dynamic Method Selection and OOP• “Interesting” language feature introduced by Simula 67, Smalltalk,C++, Java: thevirtual function(to use C++ terminology).• Problem:– Arrange classes in a hierarchy of types.– Instance of subtype “is an” instance of its supertype(s).– In particular, inherits their methods, but can override them.– Adynamiceffect: Cannot in general tell from program text whatbody of code executed by a given call.• Implementation difficulty (as usual) depends on details of a lan-guage’s semantics.• Some things still static:– Names of functions, numbers of arguments are (usually) known– Compiler can handle overloading by inventing new names for func-tions. E.g., G++ encodes a function f(int x) in class Q asZN1Q1fEi,and f(int x, int y) as ZN1Q1fEii.Last modified: Thu Apr 2 12:27:02 2009 CS164: Lecture #19 8I. Fully Dynamic Approach• Regular Python has a completely dynamic approach to the problem:class A:x = 2; def f (self): return 42a = A (); b = A ()print a.x, a.f() # Prints 2 42a.x = lambda (self, z): self.w * za.f = 13; a.w = 5print a.x(3), a.f, a.w # Prints 15 13 5print b.x(3), b.f, b.w # Errorprint A.x # Prints 2A.x = lambda (self): 19A.f = 2A.v


View Full Document

Berkeley COMPSCI 164 - Lecture Notes

Documents in this Course
Lecture 8

Lecture 8

40 pages

Load more
Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?