Com S 342 meeting -*- Outline -*- * Call by Value-Result and call-by-result (6.3) ** informal overview *** motivation ------------------------------------------ MOTIVATION FOR CALL BY VALUE-RESULT How would a compiler access a formal using: - call by value? - call by reference? Which is more efficient? Which works over a network? What can be done with call-by-reference that can't be with call-by-value? ------------------------------------------ ...offset into local stack frame ... indirectly through pointer in local stack frame this is only a small efficiency difference, but anything that affects procedure calls is worth looking into. ... call by value for small values, call by reference works for bigger things, but even there there's no win in the indirect model for call-by-reference, only in the direct model *** examples ------------------------------------------ CALL BY REFERENCE VS. CALL BY VALUE-RESULT EXAMPLE define swap2 = proc (x, y) begin x := +(x, y); y := -(x, y); x := -(x, y) end; let b = 5; c = 7 in begin swap2(b, c); print(b); print(c) end; ------------------------------------------ Q: What does this do in call by reference? value-result? Q: What if we execute swap(b,b)? Then get 0 as the final value of b with reference The problem with the aliasing goes away in value-result, because there is another copy. ------------------------------------------ PASSING AN ARRAY BY VALUE-RESULT let one = 1 in let p = proc(b,x) begin x := 2; b[x] := one end in letarray a[3] in begin a[0] := 7; a[1] := 8; a[2] := 9; p(a, one); list(a,one) end ------------------------------------------ Q: Is there any complication for passing an array element? need to use the array element (ae) mechanism, just as in reference ------------------------------------------ PASSING ARRAY ELEMENTS BY VALUE-RESULT let swap = proc(x,y) let temp = 0 in begin temp := x; x := y; y := temp end in letarray a[3] in begin a[0] := 3; a[1] := 4; a[2] := 2; let i = 0 in begin swap(i, a[i]); list(i, a[0], a[1], a[2]) end ------------------------------------------ Q: What about in the direct model? none here either. Q: How would you implement call by value-result? homework problem... Q: How does call by value-result relate to call-by-value? Q: What do you think call-by-result should mean? ** summary ------------------------------------------ LANGUAGES AND MECHANISMS array language model mechanism(s) C direct value C++ direct value (default) reference (&) Pascal direct value (default) reference (var) MS Visual direct reference (default) Basic 6.0 value (ByVal) Ada direct value (in) result (out) value-result (in out) Java indirect value Scheme Smalltalk ------------------------------------------ ------------------------------------------ SUMMARY OF CALLING MECHANISMS action at call at return mechanism value copy value - reference copy address - value-result result name/need make closure - pass address of closure variable reference is direct to local for call-by: variable reference is indirect in: ------------------------------------------ ... value-result copy value copy result + copy address result copy address copy result ... value, value-result ... reference, name (first time with need)