XTRAN Example — Translate Fortran COMMONs & EQUIVALENCEs to C
The following example uses the standard set of XTRAN rules for parsing Fortran and translating it to C. NOTE that the translation shown below was done with default conditions. XTRAN provides many options for controlling the way it translates.
Process Flowchart
Here is a flowchart for this process, in which the elements are color coded:
- BLUE for XTRAN versions (runnable programs)
- ORANGE for XTRAN rules (text files)
- RED for
code
Input to XTRAN:
SUBROUTINE subr1 COMMON/abc/ i, j // y, z DIMENSION j(2) REAL j, k EQUIVALENCE (j, x) EQUIVALENCE (x, a) EQUIVALENCE (m, n) EQUIVALENCE (n, p) i = 1 j(2) = i k = 3.0 m = 1 p = 1.0 x = 1.0 y = 1.0 END SUBROUTINE subr2 INTEGER arr1(3), arr2(5), arr3(8), arr4(6), arr5(3), arr6(4) INTEGER var1, var2 EQUIVALENCE (arr1(2), arr2(3), arr3(4), arr4, var2, arr6(4)) EQUIVALENCE (arr2(2), var1, arr5(2)) arr1(1) = 1 arr2(1) = 1 arr3(1) = 1 arr4(1) = 1 arr5(1) = 1 arr6(1) = 1 var1 = 1 var2 = 1 END
Output from XTRAN:
extern struct { long i; union { float j[2]; float x; float a; } u1; } abc; extern struct { float y; float z; } blank_common; void subr1(void) { float k; union { long m; long n; float p; } u1; abc.i = 1; abc.u1.j[1] = abc.i; k = 3.0; u1.m = 1; u1.p = 1.0; u1.x = 1.0; blank_common.y = 1.0; return; } void subr2(void) { union { struct { Bytes filler[8]; union { long arr1[3]; long var1; } u1; } s1; struct { Bytes filler[4]; union { long arr2[5]; long arr5[3]; } u1; } s2; long arr3[8]; struct { Bytes filler[12]; union { long arr4[6]; long var2; } u1; } s3; long arr6[4]; } u1; u1.s1.u1.arr1[0] = 1; u1.s2.u1.arr2[0] = 1; u1.arr3[0] = 1; u1.s3.u1.arr4[0] = 1; u1.s2.u1.arr5[0] = 1; u1.arr6[0] = 1; u1.s1.u1.var1 = 1; u1.s3.u1.var2 = 1; return; }