144
TSEAMCET(BiPC)-17 Inst_code INSTN_NAME branch_ code roll_no Rank CName Sex Region Caste_ Code Seat_Cat SNVM S N VANITHA PHARMACY MAHA VIDYALAYA, TARNAKA PDB 5414360 883 SAMYUKTA KACHAPUR F OU OC OC_GIRLS_UR GURU GURUNANAK INST.TECH CAMPUS, IBRAHIMPATAN PDB 6007148 1040 BHUKYA GOPINADH M OU ST OC_GEN_OU SUUP SULTAN UL-ULOOM COLLEGE OF PHARMACY, BANJARA HILLS PDB 5308291 1086 SANA FATIMA F OU BC_E MUS_GEN_UR GURU GURUNANAK INST.TECH CAMPUS, IBRAHIMPATAN PDB 6502103 1365 SHAHANAWAZKHAN M OU OC OC_GEN_OU BITL BHARAT INSTITUTE OF TECHNOLOGY, IBRAHIMPATNAM PDB 7412843 1389 BONAGIRI KARTHEEK M OU BC_D OC_GEN_UR PURD PULLA REDDY INST. OF PHARMACY DINDIGUL, DUNDIGAL PDB 7002044 1409 RAINI SAINATH M OU BC_B OC_GEN_UR GPRP G P R COLLEGE OF PHARMACY, MEHDIPATNAM PHB 5302042 1468 ZOHA HAFEEZ F OU BC_E OC_GIRLS_UR KUCP K U COLLEGE OF PHARMACEUTICAL SCIENCES, WARANGAL PHB 7410317 1494 VENGALDAS CHAITANYA F OU BC_B OC_GIRLS_UR DSOP DECCAN SCHOOL OF PHARMACY, NAMPALLY PDB 6207720 1641 M MEHARAZ BEGUM F OU BC_E MUS_GEN_UR DSOP DECCAN SCHOOL OF PHARMACY, NAMPALLY PHB 5308501 1670 UROOJ SHEREIN ANSARI F OU BC_E MUS_GEN_UR PURD PULLA REDDY INST. OF PHARMACY DINDIGUL, DUNDIGAL PDB 5802314 1717 VISSA SAI KRISHN M OU BC_D OC_GEN_OU KUCP K U COLLEGE OF PHARMACEUTICAL SCIENCES, WARANGAL PHB 5904008 1769 ILA SOWMYA F OU BC_D OC_GEN_UR MRCP MALLA REDDY COLLEGE OF PHARMACY, MEDCHAL PDB 5101503 1852 MOTTALA DEEKSHITH M OU BC_D OC_GEN_UR SUUP SULTAN UL-ULOOM COLLEGE OF PHARMACY, BANJARA HILLS PDB 5604003 2067 MD SAJJAD M OU BC_E MUS_GEN_UR VCOP SRI VENKATESWARA COLLEGE OF PHARMACY, MADHAPUR PDB 5104340 2214 MANAPATI RAKESH KUMAR YADAV M OU BC_D OC_GEN_UR DSOP DECCAN SCHOOL OF PHARMACY, NAMPALLY PDB 5603051 2256 MAJEDA ZAREEN F NL OC MUS_GEN_UR RBVW R B V R R WOMENS COLLEGE OF PHARMACY, NARAYANAGUDA PDB 5412423 2326 KAPA ANUSHA F OU OC OC_GIRLS_OU MRCP MALLA REDDY COLLEGE OF PHARMACY, MEDCHAL PDB 6102323 2345 PAIDIMARRY PUJITHA APARNA F OU OC OC_GIRLS_OU VCOP SRI VENKATESWARA COLLEGE OF PHARMACY, MADHAPUR PDB 5106654 2366 KANTIPUDI LIKHITHA F OU OC OC_GIRLS_OU SUUP SULTAN UL-ULOOM COLLEGE OF PHARMACY, BANJARA HILLS PHB 6404401 2433 AREESHA FIRDOUSE F OU BC_B MUS_GEN_UR VCOP SRI VENKATESWARA COLLEGE OF PHARMACY, MADHAPUR PDB 5409304 2485 GANDIKOTA SAI SRI LEKHA F OU OC OC_GIRLS_OU GURU GURUNANAK INST.TECH CAMPUS, IBRAHIMPATAN PDB 5405191 2496 KAVALI PRAKASH M OU BC_D BC_D_GEN_OU VCOP SRI VENKATESWARA COLLEGE OF PHARMACY, MADHAPUR PDB 6203156 2498 V RAJENDRA M OU BC_B BC_B_GEN_UR KUCP K U COLLEGE OF PHARMACEUTICAL SCIENCES, WARANGAL PHB 5802439 2585 THOTA PREETHI F OU BC_D OC_GEN_UR SVHU SATAVAHANA UNIVERSITY, KARIMNAGAR PHB 6102239 2628 KOTHAPALLY SAILASYA F OU OC OC_GIRLS_UR BPCP BHASKAR PHARMACY COLLEGE, YENKAPALLY PDB 7408746 2659 LAVUDYA AKSHAY NAIK M OU ST OC_GEN_OU DSOP DECCAN SCHOOL OF PHARMACY, NAMPALLY PDB 5304008 2685 HAFSA FATIMA F OU BC_E MUS_GEN_OU SWCP SHADHAN WOMENS COLL. OF PHARM., KHAIRATABAD PHB 5301169 2713 JAVERIA AFREEN SIDDIQUA F OU BC_E MUS_GIRLS_UR VCOP SRI VENKATESWARA COLLEGE OF PHARMACY, MADHAPUR PDB 5401395 2737 GURUBILLI DIVYA SRI F OU BC_D OC_GIRLS_OU CBIT CHAITANYA BHARATHI INSTITUTE OF TECHNOLOGY, GANDIPET BTB 5601300 2747 D LAHARI F OU SC OC_GEN_UR DSOP DECCAN SCHOOL OF PHARMACY, NAMPALLY PHB 5302443 2759 SYEDA MALIHA ARSHIYAN F OU OC MUS_GEN_UR GCPK GEETHANJALI COLL. OF PHARM, KEESARA PHB 5503253 2795 POOJITHA PINJALA F OU BC_B OC_GIRLS_UR DSOP DECCAN SCHOOL OF PHARMACY, NAMPALLY PDB 5603066 2816 MASARRATH UNNISA F OU BC_E MUS_GEN_OU DSOP DECCAN SCHOOL OF PHARMACY, NAMPALLY PDB 6606144 2861 HAFSA FIRDOSE F OU BC_E MUS_GEN_OU GRCP GOKARAJU RANGARAJU COLLEGE OF PHARMACY, BACHUPALLI PHB 5410446 2984 BOLLEPALLY SWETHA F OU OC OC_GIRLS_UR GPRP G P R COLLEGE OF PHARMACY, MEHDIPATNAM PHB 5409112 3063 MAMIDALA SRINIVAS M OU BC_B OC_GEN_UR SVHU SATAVAHANA UNIVERSITY, KARIMNAGAR PHB 7709001 3114 BOGASAMUDRAM SRIJA F AU OC OC_GEN_UR DSOP DECCAN SCHOOL OF PHARMACY, NAMPALLY PDB 5306436 3117 AMEENA FATIMA F OU BC_E MUS_GEN_OU SVHU SATAVAHANA UNIVERSITY, KARIMNAGAR PHB 6004133 3236 POTHURAJU RAMESH M OU BC_B BC_B_GEN_UR RBVW R B V R R WOMENS COLLEGE OF PHARMACY, NARAYANAGUDA PDB 5604403 3344 FATIMA TABASSUM F OU BC_E OC_GIRLS_OU KUCP K U COLLEGE OF PHARMACEUTICAL SCIENCES, WARANGAL PHB 5106092 3365 VANCHA KEERTHANA F OU OC OC_GIRLS_OU GPRP G P R COLLEGE OF PHARMACY, MEHDIPATNAM PDB 5603182 3449 EDULAKANTI AKSHITHA F OU BC_B BC_B_GEN_UR VPWL VAAGDEVI INST. OF PHARM. SCI., WARANGAL PDB 7411330 3455 BANOTH ANIL M OU ST OC_GEN_OU PURD PULLA REDDY INST. OF PHARMACY DINDIGUL, DUNDIGAL PDB 5605413 3456 PALASANI VISMITHA F OU OC OC_GIRLS_OU KUCP K U COLLEGE OF PHARMACEUTICAL SCIENCES, WARANGAL PHB 7412302 3499 DONTHULA PREETHI F OU OC OC_GIRLS_OU KUCP K U COLLEGE OF PHARMACEUTICAL SCIENCES, WARANGAL PHB 7416304 3500 VULLI ARAVIND M OU BC_B OC_GEN_OU VCOP SRI VENKATESWARA COLLEGE OF PHARMACY, MADHAPUR PHB 5101596 3527 MOVVA LALITHA HARSHINI F OU OC OC_GIRLS_UR SNVM S N VANITHA PHARMACY MAHA VIDYALAYA, TARNAKA PDB 5414065 3536 KUNDELLA MANJULA F OU BC_D BC_D_GIRLS_UR GURU GURUNANAK INST.TECH CAMPUS, IBRAHIMPATAN PDB 5407100 3544 PEDDI BHAVYA SRI F OU BC_B OC_GIRLS_UR DSOP DECCAN SCHOOL OF PHARMACY, NAMPALLY PDB 5604017 3550 SYEDA NEHA FATIMA F OU OC MUS_GEN_OU DSOP DECCAN SCHOOL OF PHARMACY, NAMPALLY PDB 5305304 3563 SYEDA SHAZIA ANWAR F OU OC MUS_GEN_OU VCOP SRI VENKATESWARA COLLEGE OF PHARMACY, MADHAPUR PDB 7403414 3567 ASHWITHA NALLA F OU OC OC_GEN_OU SSRP SMT SAROJINI RAMULAMMA COLLEGE OF PHARMACY, MAHABUBNAGAR PDB 6406265 3586 GUNTAPALLI SRAVANI F OU OC OC_GIRLS_OU DSOP DECCAN SCHOOL OF PHARMACY, NAMPALLY PDB 5603016 3642 RAMSHA FARHEEN F OU BC_E MUS_GEN_OU VCOP SRI VENKATESWARA COLLEGE OF PHARMACY, MADHAPUR PDB 5106255 3655 EEGA MADHURI F OU OC OC_GEN_OU DSOP DECCAN SCHOOL OF PHARMACY, NAMPALLY PHB 5411279 3703 SYEDA NAJMUNNISA FATIMA F OU OC MUS_GEN_UR GRCP GOKARAJU RANGARAJU COLLEGE OF PHARMACY, BACHUPALLI PHB 5409060 3755 N SAI SMRITHI F OU OC OC_GEN_UR DSOP DECCAN SCHOOL OF PHARMACY, NAMPALLY PDB 5603336 3771 MADIHA KHAN F OU OC MUS_GEN_OU KUCP K U COLLEGE OF PHARMACEUTICAL SCIENCES, WARANGAL PHB 5409336 3807 K PRABHA NIHARIKA F OU OC OC_GIRLS_OU MRPC MALLA REDDY PHARMACY COLLEGE, MAISAMMAGUDA PDB 7002459 3813 ASFIYA AZREEN F OU BC_E OC_GEN_UR 1 of 144

Dealing with Errors Signaling Errors - University Of Marylandatif/Teaching/Fall2007/lectures/class20... · – What about errors in a constructor? • What if client forgets to check

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Dealing with Errors Signaling Errors - University Of Marylandatif/Teaching/Fall2007/lectures/class20... · – What about errors in a constructor? • What if client forgets to check

1

CMSC 330: Organization of Programming Languages

ExceptionsParameter Passing

CMSC 330 2

Preconditions

• Functions often have requirements on their inputs// Return maximum element in A[i..j]

int findMax(int[] A, int i, int j) { ... }

– A is nonempty– A isn't null– i and j must be nonnegative– i and j must be less than A.length– i < j (maybe)

• These are called preconditions

CMSC 330 3

Dealing with Errors

• What do you do if a precondition isn’t met?

• What do you do if something unexpected happens?– Try to open a file that doesn’t exist– Try to write to a full disk

CMSC 330 4

Signaling Errors

• Style 1: Return invalid value

// Returns value key maps to, or null if no

// such key in map

Object get(Object key);

– Disadvantages?

CMSC 330 5

Signaling Errors (cont’d)• Style 2: Return an invalid value and status

static int lock_rdev(mdk_rdev_t *rdev) {

...

if (bdev == NULL)

return -ENOMEM;

...

}

// Returns NULL if error and sets global

// variable errno

FILE *fopen(const char *path, const char *mode);

CMSC 330 6

Problems with These Approaches

• What if all possible return values are valid?– E.g., findMax from earlier slide– What about errors in a constructor?

• What if client forgets to check for error?– No compiler support

• What if client can’t handle error?– Needs to be dealt with at a higher level

• Poor modularity- exception handling code becomes scattered throughout program

• 1996 Ariane 5 failure classic example of this …

Page 2: Dealing with Errors Signaling Errors - University Of Marylandatif/Teaching/Fall2007/lectures/class20... · – What about errors in a constructor? • What if client forgets to check

2

CMSC 330 7

Ariane 5 failureDesign issues: In order to save funds and ensure reliability,

and since the French Ariane 4 was a successful rocket, the Inertial Reference System (SRI) from Ariane 4 was reused for the Ariane 5.

What happened?: On June 4, 1996 the Ariane 5 launch vehicle failed 39 seconds after liftoff causing the destruction of over $100 million in satellites.

Cause of failure: The SRI, which controls the attitude (direction) of the vehicle by sending aiming commands to the rocket nozzle, sent a bad command to the rocket causing the nozzle to move the rocket toward the horizontal.

The vehicle tried to switch to the backup SRI, but that failed for the same reason 72 millisec earlier.

The vehicle had to then be destroyed.

CMSC 330 8

Why Ariane 5 failed• SRI tried to convert a floating point number out of range to

integer. Therefore it issued an error message (as a 16 bit number). This 16 bit number was interpreted as an integer by the guidance system and caused the nozzle to move accordingly.– The backup SRI performed according to specifications and

failed for the same reason.• Ada range checking was disabled since the SRI was

supposedly processing at 80% load and the extra time needed for range checking was deemed unnecessary since the Ariane 4 software worked well.

• The ultimate cause of the problem was that the Ariane 5 has a more pronounced angle of attack and can move horizontally sooner after launch. The “bad value” was actually the appropriate horizontal speed of the vehicle.

CMSC 330 9

Better approaches: Exceptions in Java

• On an error condition, we throw an exception

• At some point up the call chain, the exception is caught and the error is handled

• Separates normal from error-handling code

• A form of non-local control-flow– Like goto, but structured

CMSC 330 10

Throwing an Exception

• Create a new object of the class Exception, and throw it

if (i >= 0 && i < a.length )

return a[i];

throw new ArrayIndexOutOfBounds();

• Exceptions thrown are part of the return type in Java– When overriding method in superclass, cannot throw

any more exceptions than parent’s version

CMSC 330 11

Method throws declarations

• A method declares the exceptions it might throw– public void openNext() throws

UnknownHostException, EmptyStackException{ … }

• Must declare any exception the method might throw– Unless it is caught in (masked by) the method– Includes exceptions thrown by called methods– Certain kinds of exceptions excluded

CMSC 330 12

Exception Hierarchy

Throwable

Error Exception

RuntimeException

Checked

Unchecked

Page 3: Dealing with Errors Signaling Errors - University Of Marylandatif/Teaching/Fall2007/lectures/class20... · – What about errors in a constructor? • What if client forgets to check

3

CMSC 330 13

• Subclasses of RuntimeException and Error are unchecked– Need not be listed in method specifications

• Currently used for things like– NullPointerException– IndexOutOfBoundsException– VirtualMachineError

• Is this a good design?

Unchecked Exceptions

CMSC 330 14

• First catch with supertype of the exception catches it

• finally is always executedtry { if (i == 0) return; myMethod(a[i]); }catch (ArrayIndexOutOfBounds e) {

System.out.println(“a[] out of bounds”); }catch (MyOwnException e) {

System.out.println(“Caught my error”); }catch (Exception e) {

System.out.println(“Caught” + e.toString()); throw e; }finally { /* stuff to do regardless of whether an exception */

/* was thrown or a return taken */ }

Exception Handling

CMSC 330 15

Implementing Exceptions in Java

• JVM knows about exceptions, and has built-in mechanism to handle them

public class A {void foo() {

try {Object f = null;f.hashCode();

}catch (NullPointerException e) {

System.out.println("caught");}

}}

CMSC 330 16

Implementing Exns in Java

• Exception table tells JVM what handlers there are for which region of code– Notice that putting this “off to the side” keeps it out of the main

code path• (Though less important for an interpreted language)

void foo();Code:0: aconst_null1: astore_12: aload_13: invokevirtual #2;

//hashCode6: pop7: goto 19

10: astore_111: getstatic #4; //System.out14: ldc #5; //String caught16: invokevirtual #6; //println19: return

Exception table:from to target type0 7 10 NullPointerExn

CMSC 330 17

Implementing Exns in C++

• Design battle: resumption vs. termination– Resumption: an exception handler can resume

computation at the place where the exception was thrown

– Termination: throwing an exception terminates execution at the point of the exception

• C++ settled on termination– What do you think?

CMSC 330: Organization of Programming Languages

Parameter Passingand More on Scoping

Page 4: Dealing with Errors Signaling Errors - University Of Marylandatif/Teaching/Fall2007/lectures/class20... · – What about errors in a constructor? • What if client forgets to check

4

CMSC 330 19

Order of Evaluation

• Will OCaml raise a Division_by_zero exception?

– No: && and || are short-circuiting in OCaml• e1 && e2 evaluates e1. If false, it returns false. Otherwise,

it returns the result of evaluating e2• e1 || e2 evaluates e1. If true, it returns true. Otherwise, it

returns the result of evaluating e2

let x = 0

if x != 0 && (y / x) > 100 thenprint_string "OCaml sure is fun"

if x == 0 || (y / x) > 100 thenprint_string "OCaml sure is fun"

CMSC 330 20

Order of Evaluation (cont’d)

• C, C++, Java, and Ruby all short-circuit &&, ||• But some languages don’t, like Pascal:

– So this would need to be written as

x := 0;...if (x <> 0) and (y / x > 100) then

writeln('Sure OCaml is fun');

x := 0;...if x <> 0 then

if y / x > 100 thenwriteln('Sure OCaml is fun');

CMSC 330 21

Call-by-Value

• In call-by-value (cbv), arguments to functions are fully evaluated before the function is invoked– Also in OCaml, in let x = e1 in e2, the expression e1

is fully evaluated before e2 is evaluated• C, C++, and Java also use call-by-value

int r = 0;

int add(int x, int y) { return r + x + y; }

int set_r(void) {r = 3;return 1;

}

add(set_r(), 2);

CMSC 330 22

Call-by-Value in Imperative Languages• In C, C++, and Java, call-by-value has another

feature– What does this program print?

– Prints 0

void f(int x) {x = 3;

}

int main() {int x = 0;f(x);printf("%d\n", x);

}

CMSC 330 23

Call-by-Value in Imperative Languages, con't.

• Actual parameter is copied to stack location of formal parameter

int main() {int x = 0;f(x);printf("%d\n", x);

}

x 0void f(int x) {x = 3;

}x 03

CMSC 330 24

Call-by-Reference

• Alternative idea: Implicitly pass a pointer or reference to the actual parameter– If the function writes to it the actual parameter is modified

int main() {int x = 0;f(x);printf("%d\n", x);

}

void f(int x) {x = 3;

}

x 0x

3

Page 5: Dealing with Errors Signaling Errors - University Of Marylandatif/Teaching/Fall2007/lectures/class20... · – What about errors in a constructor? • What if client forgets to check

5

CMSC 330 25

Call-by-Reference (cont’d)

• Advantages– The entire argument doesn't have to be copied to the

called function• It's more efficient if you’re passing a large (multi-word)

argument• Can do this without explicit pointer manipulation

– Allows easy multiple return values• Disadvantages

– Can you pass a non-variable (e.g., constant, function result) by reference?

– It may be hard to tell if a function modifies an argument– What if you have aliasing?

CMSC 330 26

Aliasing

• We say that two names are aliased if they refer to the same object in memory– C examples (this is what makes optimizing C hard)

int x;int *p, *q; /*Note that C uses pointers to

simulate call by reference */p = &x; /* *p and x are aliased */q = p; /* *q, *p, and x are aliased */

struct list { int x; struct list *next; }struct list *p, *q;...q = p; /* *q and *p are aliased */

/* so are p->x and q->x *//* and p->next->x and q->next->x... */

CMSC 330 27

Call-by-Reference (cont’d)

• Call-by-reference is still around (e.g., C++), but seems to be less popular in newer languages– Older languages (e.g., Fortran, Ada, C with pointers)

still use it– Possible efficiency gains not worth the confusion– “The hardware” is basically call-by-value

• Although call by reference is not hard to implement and there may be some support for it

CMSC 330 28

Call-by-Value Discussion

• Call-by-value is the standard for languages with side effects – When we have side effects, we need to know the

order in which things are evaluated, otherwise programs have unpredictable behavior

– Call-by-reference can sometimes give different results

– Call-by-value specifies the order at function calls• But there are alternatives to call by value and

call by reference ...

CMSC 330 29

Call-by-Name• Call-by-name (cbn)

– First described in description of Algol (1960)– Generalization of Lambda expressions (to be

discussed later)– Idea simple: In a function:

Let add x y = x+yadd (a*b) (c*d)

Then each use of x and y in the function definition is just a literal substitution of the actual arguments, (a*b) and (c*d), respectively

– But implementation: Highly complex, inefficient, and provides little improvement over other mechanisms, as later slides demonstrate

Example:add (a*b) (c*d) =

(a*b) + (c*d) � executed function

CMSC 330 30

Call-by-Name (cont’d)

• In call-by-name (cbn), arguments to functions are evaluated at the last possible moment, just before they're needed

let add x y = x + y

let z = add (add 3 1) (add 4 1)

OCaml; cbv; argumentsevaluated here

add x y = x + y

z = add (add 3 1) (add 4 1)

Haskell; cbn; argumentsevaluated here

Page 6: Dealing with Errors Signaling Errors - University Of Marylandatif/Teaching/Fall2007/lectures/class20... · – What about errors in a constructor? • What if client forgets to check

6

CMSC 330 31

Call-by-Name (cont’d)

• What would be an example where this difference matters?

let cond p x y = if p then x else ylet rec loop n = loop nlet z = cond true 42 (loop 0)

cond p x y = if p then x else yloop n = loop nz = cond True 42 (loop 0)

OCaml; cbv; infinite recursionat call

Haskell; cbn; never evaluatedbecause parameter is never used

CMSC 330 32

Call by Name Examples1. P(x) {x = x + x;}

Y = 2;P(Y); write(Y)

2. But if F(m) {m = m + 1; return m;}What is:

int A[10]; m = 1;P(A[F(m)])

P(A[F(m)]) � A[F(m)] = A[F(m)]+A[F(m)] � A[m++] = A[m++]+A[m++]� A[2] = A[3]+A[4]

� means Y = Y+Y = 4

CMSC 330 33

But: Call by Name Anomalies3. Write a program to exchange values of X and Y: (e.g.,

swap(X,Y))Usual way: swap(x,y) {t=x; x=y; y=t;}– Cannot do it with call by name. Cannot handle both

of following: swap(m, A[m]) swap(A[m],m)– One of these must fail. Why?

CMSC 330 34

Two Cool Things to Do with CBN

• CBN is also called lazy evaluation– (CBV is also known as eager evaluation)

• Build control structures with functions

• Build “infinite” data structures

let cond p x y = if p then x else y

integers n = n::(integers (n+1))take 10 (integers 0) (* infinite loop in cbv *)

CMSC 330 35

Three-Way Comparison• Consider the following program under the three

calling conventions– For each, determine i's value and which a[i] (if any) is

modifiedint i = 1;

void p(int f, int g) {g++;f = 5 * i;

}

int main() {int a[] = {0, 1, 2};p(a[i], i);printf("%d %d %d %d\n",i, a[0], a[1], a[2]);

}CMSC 330 36

Example: Call-by-Value

int i = 1;

void p(int f, int g) {g++;f = 5 * i;

}

int main() {int a[] = {0, 1, 2};p(a[i], i);printf("%d %d %d %d\n",i, a[0], a[1], a[2]);

}

2

1

g

5

1

f

2101

a[2]a[1]a[0]i

Page 7: Dealing with Errors Signaling Errors - University Of Marylandatif/Teaching/Fall2007/lectures/class20... · – What about errors in a constructor? • What if client forgets to check

7

CMSC 330 37

Example: Call-by-Reference

int i = 1;

void p(int f, int g) {g++;f = 5 * i;

}

int main() {int a[] = {0, 1, 2};p(a[i], i);printf("%d %d %d %d\n",i, a[0], a[1], a[2]);

}

102

2101

a[2]a[1]a[0]i /f/g

102

CMSC 330 38

Example: Call-by-Name

int i = 1;

void p(int f, int g) {g++;f = 5 * i;

}

int main() {int a[] = {0, 1, 2};p(a[i], i);printf("%d %d %d %d\n",i, a[0], a[1], a[2]);

}

102

2101

a[2]a[1]a[0]i

i++;a[i] = 5*i;

The expression a[i] isn'tevaluated until needed, inthis case after i haschanged.

2 10

CMSC 330 39

Other Calling Mechanisms• Call-by-result

– Actual argument passed by reference, but not initialized– Written to in function body (and since passed by

reference, affects actual argument)• Call-by-value-result

– Actual argument copied in on call (like cbv)– Mutated within function, but does not affect actual yet– At end of function body, copied back out to actual

• These calling mechanisms didn't really catch on– They can be confusing in cases– Recent languages don’t use them

CMSC 330 40

CBV versus CBN• CBN is flexible- strictly more programs terminate

– E.g., where we might have an infinite loop with cbv, we might avoid it with cbn by waiting to evaluate

• Order of evaluation is really hard to see in CBN– Call-by-name doesn't mix well with side effects

(assignments, print statements, etc.)• Call-by-name is more expensive since:

– Functions have to be passed around– If you use a parameter twice in a function body, its

thunk (the unevaluated argument) will be called twice• Haskell actually uses call-by-need (each formal parameter is

evaluated only once, where it's first used in a function)

CMSC 330 41

CBV versus CBN (cont’d)

• Call-by-name isn't very “mainstream”– Haskell solves these issues by not having side

effects– But then someone invented “monads” (constructed

values that simulate side effects) so you can have side effects in a lazy language

• Call-by-name's benefits may not be worth its cost

CMSC 330 42

How Function Calls Really Work

• Function calls are so important they usually have direct instruction support on the hardware

• We won’t go into the details of assembly language programming– See CMSC 212, 311, 412, or 430

• But we will discuss just enough to know how functions are called

Page 8: Dealing with Errors Signaling Errors - University Of Marylandatif/Teaching/Fall2007/lectures/class20... · – What about errors in a constructor? • What if client forgets to check

8

CMSC 330 43

Machine Model (Generic UNIX)• The text segment

contains the program's source code

• The data segmentcontains global variables, static data (data that exists for the entire execution and whose size is known), and the heap

• The stack segment contains the activation records for functions

(text segment)

(data segment)

CMSC 330 44

Machine Model (x86)

• The CPU has a fixed number of registers– Think of these as memory that’s really fast to access– For a 32-bit machine, each can hold a 32-bit word

• Important x86 registers– eax generic register for computing values– esp pointer to the top of the stack– ebp pointer to start of current stack frame– eip the program counter (points to next instruction in

text segment to execute)

CMSC 330 45

The x86 Stack Frame/Activation Record• The stack just after f transfers control to g

return instr ptr (eip)

f’s locals, saves

previous frames

parameters for g

frame boundary

frame boundary

ebp

esp

Based on Fig 6-1 in Intel ia-32 manual

saved ebp of f

return instruction ptr

ebp for caller of f

CMSC 330 46

x86 Calling Convention• To call a function

– Push parameters for function onto stack– Invoke CALL instruction to

• Push current value of eip onto stack– I.e., save the program counter

• Start executing code for called function– Callee pushes ebp onto stack to save it

• When a function returns– Put return value in eax– Invoke LEAVE to pop stack frame

• Set esp to ebp• Restore ebp that was saved on stack and pop it off the stack

– Invoke RET instruction to load return address into eip• I.e., start executing code where we left off at call

CMSC 330 47

Exampleint f(int a, int b) {

return a + b;}

int main(void) {int x;

x = f(3, 4);}

f:pushl %ebpmovl %esp, %ebpmovl 12(%ebp), %eaxaddl 8(%ebp), %eaxleaveret

main:...subl $8, %esppushl $4pushl $3call f

l: addl $16, %espmovl %eax, -4(%ebp)leaveret

gcc -S a.c

CMSC 330 48

Lots More Details• There’s a whole lot more to say about calling

functions– Local variables are allocated on stack by the callee

as needed• This is usually the first thing a called function does

– Saving registers• If the callee is going to use eax itself, you’d better save it to

the stack before you call– Passing parameters in registers

• More efficient than pushing/popping from the stack– Etc...

• See other courses for more details