Homework # 5
due 2010/11/11

Exceptions for Cool

For this homework, you will add exceptions and exception handling to Cool. An instance of class Throwable or a subclass can be thrown by using the syntax throw $o$, an expression that does not return (and has any type, that is ``native'' type). An expression evaluation $e$ can be protected with handlers $h$ using the syntax: try { $e$ } catch { $h$ }. Each handler has the same syntax as a case branch. As with a match expression, the branch that most closely matches the thrown object will be executed. As is usual in languages with exception handling, the handlers are dynamically scoped. If no exception is thrown during the evaluation of $e$, the result of the protected evaluation is the same as the evaluation of $e$, otherwise if there is an exception thrown which is caught by a handler, the result of the handler is the result of the protected evaluation.

Errors that previously would terminate execution (such as dispatch on void, string index bad, etc) should now throw exceptions. Throwing null should cause a null pointer exception to be thrown instead. Furthermore, an uncaught exception should be printed to stdout and the program terminates. For more information on this requirement, see below.

The throw keyword should have lower precedence than any operator except the semicolon (;) operator.

Implementing an extension

This extension mainly affects the code generation and runtime system, but also has effects on the front end. You will first need to formalize the syntax and static semantics of the extension before you implement it. Write the grammar changes and new type axioms and turn them in for a milestone by November 4th.

Normally one would also need to change the intermediate representation. This task has already been done for you, there are two new ``constructors'' for the new constructs:

constructor handle(body : Expression; cases: Cases) : Expression;
constructor raise(exception : Expression) : Expression;
The semantic analyzer and code generator will need to define routines for these new classes of nodes.

Obviously, the code generator will need to be changed, but the run-time system will need to be changed as well to raise exceptions upon errors rather than aborting, and to catch and print uncaught exceptions (and abort execution). A string should be printed by calling the dynamic dispatch of toString on the exception object.

Files and Deliverables

Get copies of the required files from the homework's source directory:

weise.cs: make -f /afs/cs.uwm.edu/users/classes/cs854/src/homework5/Makefile
As well as the source code (which is editable), you also receive several links to shared files:
Makefile
The makefile to use to build your compiler and run it.
basic.cool
The updated predefined library, including classes Throwable and RuntimeException.
example.cool
The test case for the unextended compiler; this is used for regression tests.
exception.cool
An executable test case that tests exceptions. It includes the required output.
Please put an overview of your implementation in the provided README.

Here are the sizes of the diffs (diff oldX X | wc), counting both deleted and added lines (changed lines are counted twice) plus one for every deleted or added block, with changed blocks counting twice again:

Cool.lex
12
Cool.y
13
Semant.scala
91
CodeGen.scala
134
cool-runtime.s
522
Total
772
For instance Cool.y has two changed lines (counts 2+2+1+1), an inserted line (counted 1+1) and a block of four inserted lines (counted 4+1) for a grand total of 6+2+5 = 13.

The file frag.s in the $CLASSHOME/src/homework5 directory contains assembly code to help you generate exception strings. Part of the code is alternate code for Int.toString.



John Tang Boyland 2010-10-29