Question

In the Fortran 77 code , there is a combination use of common block and equivalence statement. For example,

    common  /X/ a,b,c,d
    dimension arr(4)
    equivalent(arr(1),a)

Can anybody provide a feasible solution that convert this kind of Fortran 77 code into Fortran 90 code?

Was it helpful?

Solution

I assument that you mean an equivalence statement. This causes the named variables to share storage.

It might not be easy. There are several possible approaches depending on how the variables are used.

With the equivalence statement the various variables are automatically updated when their corresponding variable is changed since they occupy the same storage. Whenever b is changed a(2) automatically changes, etc. Whenever a(3) is changed, c automatically changes, etc.

You could make all of the variables into module variables -- the Fortran 90 way of having global variables. Omitting the equivalence statement they no longer share storage. Then you could simply write a(2) = b after any change to b, b = a(2) after any change to a(2), etc. But this requires that you identify every place in the program in which any of these variables change, and that one continue to follow this method when the program is modified. Which might not be easy.
Writing a(2) = b, etc., supposes that the variables are of the same type. If they are not, the Fortran 90 equivalent of equivalence is the transfer intrinsic function.

There are two better solutions depending on what the code is doing with the variables. If the program is really transferring info with the equivalence statement, then see if you can rewrite the code to use only one set of variable names. Replace the other name where ever it is used. This would only possible only if the types are the same.

If the program is using the equivalence to overlay variables that are not simultaneously used to conserve memory, then it is easier. In this case you can eliminate the equivalence statement and make the variables module variables instead of common and don't have additional work to do. And maybe they don't have to be global variables anymore. If one subroutine was using the other set, and another the other, and the common and equivalence was only a way of saving storage, you could make them into local variables.

The best solution depends on understanding the particular program.

I consider equivalence pernicious ... its worth a good amount of effort to get rid of. Still, if too deeply embedded into a large program, it might be very difficult.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top