Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.compilers > #3015

Re: Fortran to C/C++ translation: a running example.

From Thomas Koenig <tkoenig@netcologne.de>
Newsgroups comp.compilers
Subject Re: Fortran to C/C++ translation: a running example.
Date 2022-05-21 17:24 +0000
Organization news.netcologne.de
Message-ID <22-05-042@comp.compilers> (permalink)
References <22-05-032@comp.compilers> <22-05-038@comp.compilers>

Show all headers | View raw


Lydia Marie Williamson <lydiamariewilliamson@gmail.com> schrieb:
> On Monday, May 16, 2022 at 2:53:09 PM UTC-5, Rock Brentwood wrote:
>> Another key issue the aliasing that goes on with "equivalence" constructs.
>> There is no good uniform translation for this into C ... it actually better
>> fits C++, where you have reference types available. There's really no good
>> reason why those have been left out of C, when other things which appeared
>> first in C++, like "const", "bool" or function prototypes, found their way
>> into C.
>>
>> However, a substantial chunk of use-cases for equivalence constructs can be
>> carved out as "enum" types, so there was a strength reduction step for this,
>> too.
>
> This is not exactly correct. It's "common blocks" that were handled in this way.
>
> In the Fortran source of Zork/dungeon, the "equivalence" statements and
> "common blocks" were used together, so it's easy to get the issue confused. I
> don't know if their being used together is something that always happened in
> Fortran, or if it was just particular to this program.

Fortran has the concept of storage association - under certain
circumstances, the ordering of variables is prescribed by the
standard.

COMMON blocks are one example of this.  Taking an example from the
original Fortran source code:

	COMMON /SYNTAX/ VFLAG,DOBJ,DFL1,DFL2,DFW1,DFW2,
     & IOBJ,IFL1,IFL2,IFW1,IFW2

This declares a common block /SYNTAX/ with 11 named variables
(all of them integers due to an IMPLICIT INTEGER (A-Z) earlier in
all files), which have to be contiguous in memory.

The next line

	INTEGER SYN(11)

declares an integer array with 11 elements.

Finally, the statement

	EQUIVALENCE (VFLAG, SYN)

tells the compiler that the address of the (first element of) SYN
and VFLAG are the same.

So, you can now use SYN(1) to refer to VFLAG, SYN(2) to DOBJ and so on.

Why is this done?  I see only one use case, in np3.for

	DO 10 I=1,11
C						!CLEAR SYNTAX.
	  SYN(I)=0
10	CONTINUE

simply to create a shortcut for clearing the syntax.

This is a benign (and standard-conforming) way of using COMMON
and EQUIVALENCE.  Equivalent C code might create a 'struct syntax'
and clear it with a memset, or have 11 individual variables and
zero them individually.

Back to comp.compilers | Previous | NextPrevious in thread | Find similar


Thread

Fortran to C/C++ translation: a running example. Rock Brentwood <rockbrentwood@gmail.com> - 2022-05-16 12:27 -0700
  Re: Fortran to C/C++ translation: a running example. Thomas Koenig <tkoenig@netcologne.de> - 2022-05-17 14:59 +0000
  Re: Fortran to C/C++ translation: a running example. Lydia Marie Williamson <lydiamariewilliamson@gmail.com> - 2022-05-20 16:34 -0700
    Re: Fortran to C/C++ translation: a running example. gah4 <gah4@u.washington.edu> - 2022-05-21 09:31 -0700
    Re: Fortran to C/C++ translation: a running example. Thomas Koenig <tkoenig@netcologne.de> - 2022-05-21 17:24 +0000

csiph-web