Go to the first, previous, next, last section, table of contents.


Call-By-Reference Parameters

Procedure parameters in Fortran are passed by reference. SUIF uses call-by-value parameters and implements call-by-reference parameters by passing pointers to the actual arguments. When the formal parameters are used, the pointers must be dereferenced. However, one of the primary advantages of compiling Fortran programs is that there are no pointers in the source code. If not for the call-by-reference parameter pointers, most compiler passes that only deal with Fortran can be simplified by not having to deal with pointers. Thus the SUIF Fortran form automatically converts the code to make it appear that the parameters are passed by reference. The parameters are changed to call-by-reference types instead of pointers and the pointer dereferences are temporarily removed.

The make_ref_params function is used to convert call-by-reference parameters in a procedure to the Fortran form. It must be called before converting the procedure body to expression trees. This is done automatically by the read_proc method for a proc_sym if the use_fortran_form flag is set. Before writing a procedure to an output file, the call-by-reference parameters must be converted back to pointers using the undo_ref_params function. Since it is illegal to write out a procedure in the Fortran form, the write_proc method for the proc_sym always calls undo_ref_params; you do not need to call it directly. Both make_ref_params and undo_ref_params are defined in the file `callbyref.cc'.

The Fortran front-end must identify call-by-reference formal parameters by putting call_by_ref annotations (see section Call-By-Reference Annotations) on the pointer types used by those parameters. Before actually changing the type of a call-by-reference parameter and removing its dereferences, make_ref_params checks that the pointer parameter is neither addressed nor assigned within the procedure. If this check fails, the pointer is not a valid call-by-reference parameter, so make_ref_params prints a warning message and does not convert that parameter. Otherwise, it goes ahead and changes the type of the parameter from a pointer to a call-by-reference type (see section Modifier Types). Any dereferences of that pointer within the procedure are removed, and other references to the pointer are changed to ldc (load constant) instructions that take the address of the parameter. The end result of all this is that it appears that the parameters are passed by reference. Note that the call sites are not changed; the actual arguments for call-by-reference parameters are still passed as pointers.

The undo_ref_params function does as its name suggests and undoes the transformations applied by make_ref_params. The only potential complication is that while in the Fortran form the user may have used the call-by-reference parameters in ways that cannot be expressed outside of the Fortran form. Specifically, the call-by-reference parameters cannot be used as index variables of for loops (see section For Nodes), and they cannot be used as bounds in array types (see section Array Types). Both of those uses require direct references to variable symbols, and there is no place to insert the pointer dereferences required outside of Fortran form.


Go to the first, previous, next, last section, table of contents.