Linux86-64 Fortran Supplement. STMicroelectronics PGI
Advertentie
Advertentie
Chapter 21. Run-time Environment
For calls that use varargs
or stdargs
, the register %rax acts as a hidden argument whose value is the number of XMM registers used in the call.
C Parameter Conversion
In C, for a called prototyped function, the parameter type in the called function must match the argument type in the calling function. If the called function is not prototyped, the calling convention uses the types of the arguments but promotes char or short to int, and unsigned char or unsigned short to unsigned int and promotes float to double, unless you use the
–Msingle
option. For more information on the
–Msingle option, refer to Chapter 3.
Calling Assembly Language Programs
The following example shows a C program calling an assembly-language routine sum_3
.
Example 21.3. C Program Calling an Assembly-language Routine
/* File: testmain.c */
#include <stdio.h> int main() {
long l_para1 = 2;
float f_para2 = 1.0;
double d_para3 = 0.5;
float f_return;
extern float sum_3(long para1, float para2, double para3);
f_return = sum_3(l_para1, f_para2, d_para3);
printf("Parameter one, type long = %ld\n", l_para1);
printf("Parameter two, type float = %f\n", f_para2);
printf("Parameter three, type double = %f\n", d_para3);
printf("The sum after conversion = %f\n", f_return);
return 0;
}
# File: sum_3.s
# Computes ( para1 + para2 ) + para3
.text
.align 16
.globl sum_3 sum_3:
pushq %rbp
movq %rsp, %rbp
cvtsi2ssq %rdi, %xmm2
addss %xmm0, %xmm2
cvtss2sd %xmm2,%xmm2
addsd %xmm1, %xmm2
cvtsd2ss %xmm2, %xmm2
movaps %xmm2, %xmm0
popq %rbp
ret
.type sum_3, @function
.size sum_3,.-sum_3
Linux86-64 Fortran Supplement
Sections A2.4.1 through A2.4.4 of the ABI for x64 Linux and Mac OS X define the Fortran supplement. The register usage conventions set forth in that document remain the same for Fortran.
367
Linux86-64 Programming Model
Fortran Fundamental Types
INTEGER
INTEGER*1
INTEGER*2
INTEGER*4
INTEGER*8
LOGICAL
LOGICAL*1
LOGICAL*2
LOGICAL*4
LOGICAL*8
BYTE
CHARACTER*n
REAL
REAL*4
REAL*8
DOUBLE PRECISION
COMPLEX
COMPLEX*8
COMPLEX*16
DOUBLE COMPLEX
Table 21.10. Linux86-64 Fortran Fundamental Types
Fortran Type
8
8
16
16
4
8
8
1 n
4
2
4
8
8
4
1
1
2
4
Size
(bytes)
4
8
8
4
4
4
8
8
1
1
4
2
4
8
8
4
1
1
2
4
Alignment
(bytes)
4
A logical constant is one of:
• .TRUE.
• .FALSE.
The logical constants .TRUE. and .FALSE. are defined to be the four-byte values -1 and 0 respectively. A logical expression is defined to be .TRUE. if its least significant bit is 1 and .FALSE. otherwise.
Note that the value of a character is not automatically NULL-terminated.
Naming Conventions
By default, all globally visible Fortran symbol names (subroutines, functions, common blocks) are converted to lower-case. In addition, an underscore is appended to Fortran global names to distinguish the Fortran name space from the C/C++ name space.
368
Chapter 21. Run-time Environment
Argument Passing and Return Conventions
Arguments are passed by reference (i.e. the address of the argument is passed, rather than the argument itself). In contrast, C/C++ arguments are passed by value.
When passing an argument declared as Fortran type CHARACTER, an argument representing the length of the
CHARACTER argument is also passed to the function. This length argument is a four-byte integer passed by value, and is passed at the end of the parameter list following the other formal arguments. A length argument is passed for each CHARACTER argument; the length arguments are passed in the same order as their respective
CHARACTER arguments.
A Fortran function, returning a value of type CHARACTER, adds two arguments to the beginning of its argument list. The first additional argument is the address of the area created by the caller for the return value; the second additional argument is the length of the return value. If a Fortran function is declared to return a character value of constant length, for example CHARACTER*4 FUNCTION CHF(), the second extra parameter representing the length of the return value must still be supplied.
A Fortran complex function returns its value in memory. The caller provides space for the return value and passes the address of this storage as if it were the first argument to the function.
Alternate return specifiers of a Fortran function are not passed as arguments by the caller. The alternate return function passes the appropriate return value back to the caller in %rax.
The handling of the following Fortran 90 features is implementation-defined: internal procedures, pointer arguments, assumed-shape arguments, functions returning arrays, and functions returning derived types.
Inter-language Calling
Inter-language calling between Fortran and C/C++ is possible if function/subroutine parameters and return values match types. If a C/C++ function returns a value, call it from Fortran as a function, otherwise, call it as a subroutine. If a Fortran function has type CHARACTER or COMPLEX, call it from C/C++ as a void function.
If a Fortran subroutine has alternate returns, call it from C/C++ as a function returning int; the value of such a subroutine is the value of the integer expression specified in the alternate RETURN statement. If a Fortran subroutine does not contain alternate returns, call it from C/C++ as a void function.
provides the C/C++ data type corresponding to each Fortran data type.
Table 21.11. Fortran and C/C++ Data Type Compatibility
Fortran Type
CHARACTER*n x
REAL x
REAL*4 x
REAL*8 x
DOUBLE PRECISION x
INTEGER x
INTEGER*1 x
C/C++ Type
char x[n] float x float x double x double x int x signed char x
8
4
1
4
4
8
Size (bytes)
n
369
Linux86-64 Programming Model
Fortran Type
INTEGER*2 x
INTEGER*4 x
INTEGER*8 x
LOGICAL x
LOGICAL*1 x
LOGICAL*2 x
LOGICAL*4 x
LOGICAL*8 x
C/C++ Type
short x int x long x, or long long x int x char x short x int x long x, or long long x
2
4
8
8
4
1
Size (bytes)
2
4
Table 21.12. Fortran and C/C++ Representation of the COMPLEX Type
Fortran Type (lower case)
complex x complex*8 x double complex x complex *16 x
C/C++ Type
struct {float r,i;} x; float complex x; struct {float r,i;} x; float complex x; struct {double dr,di;} x; double complex x; struct {double dr,di;} x; double complex x;
Size (bytes)
16
16
16
16
8
8
8
Note
For C/C++, the complex
type implies C99 or later.
Arrays
C/C++ arrays and Fortran arrays use different default initial array index values. By default, C/C++ arrays start at 0 and Fortran arrays start at 1. A Fortran array can be declared to start at zero.
Another difference between Fortran and C/C++ arrays is the storage method used. Fortran uses columnmajor order and C/C++ use row-major order. For one-dimensional arrays, this poses no problems. For two-dimensional arrays, where there are an equal number of rows and columns, row and column indexes can simply be reversed. Inter-language function mixing is not recommended for arrays other than single dimensional arrays and square two-dimensional arrays.
Structures, Unions, Maps, and Derived Types
Fields within Fortran structures and derived types, and multiple map declarations within a Fortran union, conform to the same alignment requirements used by C structures.
370
Chapter 21. Run-time Environment
Common Blocks
A named Fortran common block can be represented in C/C++ by a structure whose members correspond to the members of the common block. The name of the structure in C/C++ must have the added underscore.
For example, the Fortran common block:
INTEGER I, J
COMPLEX C
DOUBLE COMPLEX CD
DOUBLE PRECISION D
COMMON /COM/ i, j, c, cd, d is represented in C with the following equivalent:
extern struct {
int i;
int j;
struct {float real, imag;} c;
struct {double real, imag;} cd;
double d;
} com_; and in C++ with the following equivalent:
extern "C" struct {
int i;
int j;
struct {float real, imag;} c;
struct {double real, imag;} cd;
double d;
} com_;
Note
The compiler-provided name of the BLANK COMMON block is implementation specific.
Calling Fortran COMPLEX and CHARACTER functions from C/C++ is not as straightforward as calling other types of Fortran functions. Additional arguments must be passed to the Fortran function by the C/C++ caller. A
Fortran COMPLEX function returns its value in memory; the first argument passed to the function must contain the address of the storage for this value. A Fortran CHARACTER function adds two arguments to the beginning of its argument list. The following example of calling a Fortran CHARACTER function from C/C++ illustrates these caller-provided extra parameters:
CHARACTER*(*) FUNCTION CHF(C1, I)
CHARACTER*(*) C1
INTEGER I
END
extern void chf_();
char tmp[10];
char c1[9];
int i;
chf_(tmp, 10, c1, &i, 9);
The extra parameters tmp and 10 are supplied for the return value, while 9 is supplied as the length of c1.
Refer to Section 2.8, Argument Passing and Return Conventions, for additional information.
371
Downloaden
Advertentie