1 / 27

CS 201 Computer Systems Programming Chapter 18 “ Parameter Passing ”

Herbert G. Mayer, PSU CS Status 1/23/2913. CS 201 Computer Systems Programming Chapter 18 “ Parameter Passing ”. Syllabus. Summary Aliasing Parameter Overview Value Parameter Reference Parameter Value-Result Parameter Name Parameter In-Out Parameter References. Summary.

Download Presentation

CS 201 Computer Systems Programming Chapter 18 “ Parameter Passing ”

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Herbert G. Mayer, PSU CS Status 1/23/2913 CS 201Computer Systems ProgrammingChapter 18“Parameter Passing”

  2. Syllabus • Summary • Aliasing • Parameter Overview • Value Parameter • Reference Parameter • Value-Result Parameter • Name Parameter • In-Out Parameter • References

  3. Summary Passing parameters is the key programming language tool to bind caller to callée The parameter on the calling side is the actual parameter (AP); the actual may be an expression or a named object (variable) depending on parameter The parameter on the called side (callée) is the formal parameter (FP); it is always named The actual is bound to the formal at place of call Binding lasts for the duration of the call Association actual-to-formal is generally by position Association may also be by naming the formal at the place of call, e.g. in Ada. In that case the order may be arbitrarily intermixed at the place of call

  4. Summary Types and numbers of actual and formal parameter (FP) generally must be compatible, subject to type compatibility- or type conversion rules C/C++ allow a lesser number of actual parameters (AP) to be passed than formally declared Typical implementation of C/C++ therefore pass actuals in reverse textual order; see detail in Compiler class CA 321/322 or similar Theoretically, APs are passed on the run-time stack Physically, good optimizers pass APs in registers Practically, compiler passes the first few parameters in registers and remaining ones on the stack FPs are generally located on one side off the stack marker, while locals are positioned on the opposite side of the stack marker, one kind requiring negative, the other positive offsets from a base pointer register

  5. Summary Value Parameter (VP): Actual parameter (AP) provides initial value via a type-compatible expression. If modifications to bound formal parameter (FP) occur, they do not impact the AP Reference Parameter(RP): AP must be an addressable object. If modifications to bound FP occur, they immediately impact the AP. Implemented by passing the address of the AP, which is a variable Value-Result Parameter (VRP): AP must be an addressable object. If modifications to bound FP occur, they do impact the AP, but such assignments to AP occur at the place of return. Implemented via copy-in copy-out Detail and other kinds of formal parameters below

  6. Aliasing Making one machine address accessible via two or more symbolic names (variables) is known as aliasing We say: object o2 is an alias of object o1, if they designate the same memory address Alias relation is reflexive: if o1 is alias of o2, then o2 is also an alias of o1 When o1 and o2 are aliases, and o1 is changed, then magically o2 also changes. Unless the programmer is aware of the alias relation, this magic is a surprise Aliasing is not always a sign of poor programming Software using aliasing is generally harder to comprehend, thus tends to be more error prone

  7. Aliasing Alias relations are typically established via reference parameters We can argue whether pointers cause aliasing as well; they surely allow programmers to access objects differently than via their name Example of aliasing: • int glob_i = 109; • . . . • voidfoo1( int & loc_i ) • { // foo1 • ASSERT( 109 == glob_i, "wrongvalue for glob_i" ); • ASSERT( 109 == loc_i, "wrongparameterpassing" ); • loc_i ++; // whatwillbechanged? • ASSERT( 110 == glob_i, "aliasingfailed." ); • } //endfoo1 • . . . • int main( void ) • { // main • foo1( glob_i ); // the onlycall to foo1() • } //end main

  8. Aliasing Pointers add another aliasing method Yet each time we change an object pointed to, we know: this may change memory anywhere Notice Golden Rule violation below! int glob_i = 110; . . . void foo2( void ) { // foo2 int * p_i = & glob_i; // set local pointer to glob_i ASSERT( 110 == glob_i, "error, glob_i unexpected" ); ASSERT( 110 == *p_i, "error, * to wrong 1 object" ); (*p_i)++; // *p_i is alias of glob_i ASSERT( 111 == glob_i, "error in * assignment" ); ASSERT( 111 == *p_i, "error, * to wrong 2 object" ); ++(*p_i); // another way to change ASSERT( 112 == glob_i, "error in pre-inc. via *" ); *p_i++; // this fails the ASSERT ASSERT( 113 == glob_i, "know your precedences" ); } //end foo2

  9. Parameter Overview

  10. Value Parameter (VP) The actual parameter (AP) to be bound to a formal value parameter (VP) must provide an initial value to the formal parameter (FP) Initial value is the first value of the FP; that initial actual value may be an expression or object During the call, assignments to the formal VP are allowed; exception: in-parameters in Ada Assignments to the formal VP have no impact on AP during or after the call Implementation: must make a copy of the initial value of the AP and pass it in register, or more generally on the stack At place of return, discard the copy, which immediately voids changes to the FP during the call

  11. Value Parameter in C C requires parameter passing by value Except for parameters of type array[] Arrays in C and C++ are passed by reference; generally implemented by passing the address of the actual array; in C and C++ that is also element [0] Not to be confused or equated with pointer types! More below! Pointer type parameters in C are also passed by value; of course the object pointed to may very well change during call, but that still keeps the pointer invariant C structs are passed by value, requiring a copy of the actual struct; that may be a large amount of data space How can a programmer pass an array[] by value in C? Pack array[] as a field into a struct, consumes identical amount of space as the original array[] modulo padding; will all be copied at place and moment of call

  12. Value Parameter in C++ C++ inherits C’s parameter passing methods Hence the default parameter passing method in C++ is also by value But C++ adds the explicit reference parameter type, using the optional & operator for formal reference parameters

  13. Value Parameter in C, Sample Set-Up #define ASSERT( condition, msg, value ) \ if ( ! ( condition ) ) { \ printf( "<> error: %s. Instead: %d\n", msg, value );\ } //end if typedef struct { int field; } struct_tp; typedef int * int_ptr; int global1 = 109; int global2 = 2012;

  14. Value int Parameter in C // assignment to formal VP has no impact on AP void change_int_val( int value ) { // change_int_val ASSERT( 109 == value, "in change_int_val should be 109”, value ); value++;// change formal parameter (FP) ASSERT( 110 == value, "in change_int_val should be 110”, value ); } //end change_int_val void int_val( void ) { // int_val int local1 = 109; change_int_val( local1 ); ASSERT( ( 109 == local1 ), should be 109", local1 ); } //end int_val

  15. Value struct Parameter in C // assign to formal value struct param no impact on actual void change_struct_val( struct_tp value ) { // change_struct_val ASSERT( 109 == value.field, "in change_struct_vale should be 109”, value.field ); value.field++; // change formal ASSERT( 110 == value.field, "in change_struct_val should be 110”, value.field ); } //end change_struct_val void struct_val( void ) { // struct_val struct_tp local1; local1.field = 109; change_struct_val( local1 ); ASSERT( ( 109 == local1.field ), "struct field should be 109", local1.field ); } //end struct_val

  16. Value pointer Parameter in C // assign to formal ptr value param has no impact on actual void change_ptr_val( int_ptr value ) { // change_ptr_val ASSERT( 109 == *value, "in change_ptr_val should be 109", *value ); value = & global2; // formal parameter change! ASSERT( *value == 2012, "pointed-to should be 2012", *value ); } //end change_ptr_val void ptr_val( void ) { // ptr_val int_ptr local1 = & global1; // init to 109 ASSERT( local1 == & global1, "before: should be & global1", & global1 ); change_ptr_val( local1 ); ASSERT( local1 == & global1, "Wrong pointer; points to:", *local1 ); ASSERT( 109 == *local1, "pointed-to value should be 109", *local1 ); } //end ptr_val

  17. Reference Parameter (RP) The AP for RP passing must be a named object; cannot be an expression AP does not have to be initialized, but the callée better abstain from referencing before assigning Goal is for callée eventually to compute and then return a new value in the AP Assignments to the formal RP have an immediate impact on the AP, since FP and AP are aliased to one another in RP passing Aliasing may be a source of hard to track errors Implement RP by passing the address of the AP; hence it is clear that the AP must be an addressable object; other implementations possible, specifically: passing address in register

  18. Reference Parameter in C++, Set-Up #include <iostream> #define ASSERT( condition, msg, value ) \ if ( ! ( condition ) ) { \ printf( "<> error: %s. Instead: %d\n", \ msg, ( value ) ); \ } #define MAX 10 typedef struct { // create struct w. 1 int field int field; // due to size = 4 -> no padding } struct_tp; typedef int arr_tp[ MAX ]; typedef int * int_ptr; // some globally visible variables int global1 = 109; int global2 = 2013;

  19. Reference int Parameter in C++ // assignment to formal impacts actual void change_int_ref( int & value ) { // change_int_ref ASSERT( 109 == value, "in change_int_ref should be 109”, value ); value++; // change formal, implied deref ASSERT( 110 == value, "in change_int_ref should be 110”, value ); } //end change_int_ref void int_ref( void ) { // int_ref int local1 = 109; change_int_ref( local1 ); ASSERT( ( 110 == local1 ), "int should be 110", local1 ); } //end int_ref

  20. Reference struct Parameter in C++ // assign to formal "ref struct param" impacts actual void change_struct_ref( struct_tp& value ) { // change_struct_ref ASSERT( 109 == value.field, // implied deref!!! "in change_struct_ref should be 109”, value.field ); value.field++; // change formal ASSERT( 110 == value.field, "in change_struct_ref should be 110”, value.field ); } //end change_struct_ref void struct_ref( void ) { // struct_ref struct_tp local1; local1.field = 109; change_struct_ref( local1 ); ASSERT( ( 110 == local1.field ), "struct field should be 110", local1.field ); } //end struct_ref

  21. Reference pointer Parameter in C++ // assign to formal "ref ptr param" impacts actual void change_ptr_ref( int_ptr& value ) { // change_ptr_ref ASSERT( 109 == *value, "pointer-to should be 109", *value ); value = & global2; // formal value changed ASSERT( *value == 2013, "pointed-to should be 2013", *value ); } //end change_ptr_ref void ptr_ref( void ) { // ptr_ref int_ptr local1 = & global1; ASSERT( 109 == *local1, "pointer-to should be 109 in ptr_ref", *local1 ); change_ptr_ref( local1 ); ASSERT( local1 == & global2, "Wrong pointer; points to:", *local1 ); ASSERT( 2013 == *local1, "pointed-to value should be 2013", *local1 ); } //end ptr_ref

  22. No Ref array[] Parameters in C++ // assign to formal "array param" impacts actual void change_arr_ref( arr_tp value ) // ref. is default!! { // change_arr_ref ASSERT( value[ 5 ] == 109, "value[ 5 ] should be 109", value[ 5 ] ); value[ 5 ]++; // formal value changed ASSERT( value[ 5 ] == 110, "value[5] should be 110 in change()”, value[5] ); } //end change_arr_ref void arr_ref( void ) { // arr_ref arr_tp value; for ( int i=0; i < MAX; i++ ) { value[i] = 109; } ASSERT( 109 == value[ 5 ], "value[5] should be 109 in arr_ref", value[5] ); change_arr_ref( value ); ASSERT( 110 == value[ 5 ], "value[ 5 ] should be 110", value[ 5 ] ); } //end arr_ref

  23. Value-Result Parameter in Fortran AP must be addressable object, to be bound to FP Assignments to FP are allowed in callée During the call, the AP is unchanged; note that this is quite different from RP passing! At moment of return –and there may be many places in the code– the last value assigned to the FP is copied back into the AP Not discussed further in CS 201 Should be covered in Fortran programming class, or compiler class

  24. Name Parameter in Algol60 Is default, quirky parameter passing method in Algo60 The text of the AP substitutes the corresponding FP for any particular call Can have strange side-effects Highly complicated to understand and implement, first done by Ingerman, using “thunks” Also not covered in CS 201; belongs into advanced Compiler class

  25. Name Parameter in Algol60 Comment: Algol60 sample; definition of confuse() procedure confuse( a, b ) integer a, b begin b := 4; a := 5; end; confuse( x[ i ], i ); comment: i = 33, x[33] = 10 Comment: as if confuse() had been coded like this: procedure confuse( x[i], i ) integer x[ i ], i begin i := 4; x[4] := 5; end; Comment: x[33] is unaffected, i = 4, and x[4] = 5

  26. In-Out Parameter in Ada Is one of the parameter passing methods used in Ada; close but not identical to value-result parameter passing Is a combination of the in-parameter and out-parameter passing methods Similar to value-result parameter passing, but it is left unspecified at which moment the value of the FP is copied back into the AP Will not be discussed in CS 201

  27. References Revised Algol60 report: http://www.masswerk.at/algol60/report.htm ADA reference manual: http://www.adaic.org/resources/add_content/standards/05rm/html/RM-TTL.html Ada report: http://www.sigada.org/ada_95/what_is_ada.html Fortran standard: http://www.j3-fortran.org Fast Interprocedural Alias Analysis: Keith D. Cooper, Ken Kennedy, Dept. of CS, Rice University, NSF grant CCR 86-19893

More Related