Docsity

Control Flow1 - Programming Languages and Compilers | CS 421, Study notes of Computer Science

Partial preview of the text

Download Control Flow1 - Programming Languages and Compilers | CS 421 and more Study notes Computer Science in PDF only on Docsity!

Overview Continuations Exceptions Coroutines

CS421 Lecture 20: Control Flow

Mark Hills

[email protected]

University of Illinois at Urbana-Champaign

July 25, 2006

Based on slides by Mattox Beckman, as updated by Vikram Adve, Gul

Agha, and Elsa Gunter

Overview Continuations Exceptions Coroutines

Overview

Continuations

Exceptions

Coroutines

Overview Continuations Exceptions Coroutines

Classes of Control Flow

We can break control flow up into a number of categories, based

on the purpose of the constructs.

◮ Invocation

◮ Direct calls: functions, subroutines

◮ Indirect calls: function pointers, class methods, closures

◮ Termination of Scope

◮ Structured: break, break to a label, exceptions, CPS

◮ Unstructured: goto, setjmp/longjmp, exit

◮ Selection

◮ Structured: if/then/else, continue, switch, case

◮ Unstructured: goto, computed goto, labeled entries

Overview Continuations Exceptions Coroutines

Classes of Control Flow, cont.

◮ Iteration

◮ Precomputed iteration space: do, foreach

◮ Dynamic iteration space: for, while, recursion

◮ Concurrency

◮ Manual: processes, threads, futures, coroutines

◮ Automatic: constructs in concurrent/parallel frameworks for

reductions

◮ Communication and synchronization techniques are critical

◮ You should have come across many of these already:

if/then/else, switch/case, do, for, while, break, continue,

probably foreach, goto, exit

Overview Continuations Exceptions Coroutines

Continuations Scheme and Call/CC Examples Summary

Call/CC

To do something interesting with a continuation, we first need to

get ahold of one. In Scheme, this is done using the call/cc

function.

◮ The continuation is represented as a function expecting one

argument

◮ The call/cc call also expects a function that takes one

argument as its argument

◮ When call/cc is called, the current control context is saved

and passed to the function argument of call/cc

◮ Invoking this argument then returns control to where we were

when we invoked call/cc

Overview Continuations Exceptions Coroutines

Continuations Scheme and Call/CC Examples Summary

Call/CC Example 1

1 (define call/cc call-with-current-continuation)

The full name is a bit cumbersome, but some Schemes don’t define the

abbreviation...

1 (+ 1 (call/cc

2 (lambda (k)

3 (+ 2 (k 3)))))

◮ The continuation is (+ 1 []), where [] represents the

“hole” where the continuation result will be placed

◮ The final result is just 4 (1 + 3), since we never add 2 to the

result.

Overview Continuations Exceptions Coroutines

Continuations Scheme and Call/CC Examples Summary

Continuations, Overall

◮ The good: Using continuations with call/cc gives us a

flexible way to encode jumps, meaning we could build up

features like exceptions.

◮ The bad: This could get cumbersome, though – if we have a

dedicated exception mechanism, even just syntactic sugar, it

would make programs clearer.

Overview Continuations Exceptions Coroutines

Error Handling Techniques Exceptions Exceptions in Java Evaluation

Error Handling

Historically, languages and language APIs have handled errors in

several different ways:

◮ Have functions return values, outside of the normal range of

return values, that represent errors;

◮ Return a success or error code from the function, requiring

actual return values to be in pointers or reference parameters;

◮ Pass an error handling routine (like a continuation) that the

normal routine can invoke when errors occur.

Overview Continuations Exceptions Coroutines

Error Handling Techniques Exceptions Exceptions in Java Evaluation

Success or Error Returns

1 pthread_mutex_t theLock = PTHREAD_MUTEX_INITIALIZER;

3 if (pthread_mutex_lock(&theLock) != 0) {

4 perror("Could not acquire lock");

5 return OTHERERROR;

8 if (pthread_mutex_unlock(&theLock) != 0) {

9 perror("Could not release lock");

10 return OTHERERROR;

Overview Continuations Exceptions Coroutines

Error Handling Techniques Exceptions Exceptions in Java Evaluation

Are these good enough?

These methods work, but all tend to be clumsy.

◮ Using return codes require picking values that can never be

returned from the function, and are easy to forget to check

◮ Having explicit return codes for success or failure again means

these have to be checked, which clutters up the code and

leads to “unnatural” use of functions

◮ Error handlers again clutter up function calls, and can be

expensive

Are there any better solutions?

Overview Continuations Exceptions Coroutines

Error Handling Techniques Exceptions Exceptions in Java Evaluation

Exceptions

◮ Provide for “out-of-band” error handling, with error handlers

moved into special sections of code

◮ Eliminates the need to check for errors with each step of

computation

◮ Often allows for exception mechanisms that fit well with the

strengths of the language (exception objects in Java,

constructor-based exceptions in OCaml, etc)

Overview Continuations Exceptions Coroutines

Error Handling Techniques Exceptions Exceptions in Java Evaluation

Exception History

Exceptions were first introduced in PL/1, or “Programming

Language 1”, an IBM language from the 1960’s.

◮ Used an exception handler of the form ON condition

statement, which would execute the handler statement if the

error condition matched.

◮ For very serious errors, would terminate the program after

handling the error

◮ For less serious errors, would pick up execution at the

statement following the one that triggered the exception

(similar to mechanisms in Visual Basic)

◮ Wound up being confusing – hard to tell, from looking at

code, what would execute after an error occurred

Overview Continuations Exceptions Coroutines

Error Handling Techniques Exceptions Exceptions in Java Evaluation

Exceptions in Java

In Java, exceptions are objects like other objects in the system, and

are joined into the same inheritance hierarchy.

◮ Exceptions all extend class Throwable

◮ Most exceptions extend Exception, which is for checked

exceptions

◮ System exceptions extend RuntimeException or Error,

which are unchecked (do not need to be caught)

◮ Exceptions can be manually thrown with the throw statement

Overview Continuations Exceptions Coroutines

Error Handling Techniques Exceptions Exceptions in Java Evaluation

Example: Java Exception Handler

1 ParallelTerm currentTerm = null;

2 try {

3 currentTerm = (ParallelTerm)definition.clone();

4 } catch (Exception e) {

5 System.err.println("Clone exception caught!");

6 e.printStackTrace();

7 System.exit(-1);