| 1 | <!--===- docs/Calls.md |
| 2 | |
| 3 | Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | See https://llvm.org/LICENSE.txt for license information. |
| 5 | SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | |
| 7 | --> |
| 8 | |
| 9 | # Representation of Fortran function calls |
| 10 | |
| 11 | ```{contents} |
| 12 | --- |
| 13 | local: |
| 14 | --- |
| 15 | ``` |
| 16 | |
| 17 | ## Procedure reference implementation protocol |
| 18 | |
| 19 | Fortran function and subroutine references are complicated. |
| 20 | This document attempts to collect the requirements imposed by the 2018 |
| 21 | standard (and legacy extensions) on programs and implementations, work |
| 22 | through the implications of the various features, and propose both a |
| 23 | runtime model and a compiler design. |
| 24 | |
| 25 | All section, requirement, and constraint numbers herein pertain to |
| 26 | the Fortran 2018 standard. |
| 27 | |
| 28 | This note does not consider calls to intrinsic procedures, statement |
| 29 | functions, or calls to internal runtime support library routines. |
| 30 | |
| 31 | ## Quick review of terminology |
| 32 | |
| 33 | * A *dummy argument* is a function or subroutine parameter. |
| 34 | It is *associated* with an *effective argument* at each call |
| 35 | to the procedure. |
| 36 | * The *shape* of an array is a vector containing its extent (size) |
| 37 | on each dimension; the *rank* of an array is the number of its |
| 38 | dimensions (i.e., the shape of its shape). |
| 39 | The absolute values of the lower and upper bounds of the dimensions |
| 40 | of an array are not part of its shape, just their difference (plus 1). |
| 41 | * An *explicit-shape* array has all of its bounds specified; lower |
| 42 | bounds default to 1. These can be passed by with a single address |
| 43 | and their contents are contiguous. |
| 44 | * An *assumed-size* array is an explicit-shape array with `*` as its |
| 45 | final dimension, which is the most-significant one in Fortran |
| 46 | and whose value does not affect indexed address calculations. |
| 47 | * A *deferred-shape* array (`DIMENSION::A(:)`) is a `POINTER` or `ALLOCATABLE`. |
| 48 | `POINTER` target data might not be contiguous. |
| 49 | * An *assumed-shape* (not size!) array (`DIMENSION::A(:)`) is a dummy argument |
| 50 | that is neither `POINTER` nor `ALLOCATABLE`; its lower bounds can be set |
| 51 | by the procedure that receives them (defaulting to 1), and its |
| 52 | upper bounds are functions of the lower bounds and the extents of |
| 53 | dimensions in the *shape* of the effective argument. |
| 54 | * An *assumed-length* `CHARACTER(*)` dummy argument |
| 55 | takes its length from the effective argument. |
| 56 | * An *assumed-length* `CHARACTER(*)` *result* of an external function (C721) |
| 57 | has its length determined by its eventual declaration in a calling scope. |
| 58 | * An *assumed-rank* `DIMENSION::A(..)` dummy argument array has an unknown |
| 59 | number of dimensions. |
| 60 | * A *polymorphic* `CLASS(t)` dummy argument, `ALLOCATABLE`, or `POINTER` |
| 61 | has a specific derived type or some extension of that type. |
| 62 | An *unlimited polymorphic* `CLASS(*)` object can have any |
| 63 | intrinsic or derived type. |
| 64 | * *Interoperable* `BIND(C)` procedures are written in C or callable from C. |
| 65 | |
| 66 | ## Interfaces |
| 67 | |
| 68 | Referenced procedures may or may not have declared interfaces |
| 69 | available to their call sites. |
| 70 | |
| 71 | Procedures with some post-Fortran '77 features *require* an |
| 72 | explicit interface to be called (15.4.2.2) or even passed (4.3.4(5)): |
| 73 | |
| 74 | * use of argument keywords in a call |
| 75 | * procedures that are `ELEMENTAL` or `BIND(C)` |
| 76 | * procedures that are required to be `PURE` due to the context of the call |
| 77 | (specification expression, `DO CONCURRENT`, `FORALL`) |
| 78 | * dummy arguments with these attributes: `ALLOCATABLE`, `POINTER`, |
| 79 | `VALUE`, `TARGET`, `OPTIONAL`, `ASYNCHRONOUS`, `VOLATILE`, |
| 80 | and, as a consequence of limitations on its use, `CONTIGUOUS`; |
| 81 | `INTENT()`, however, does *not* require an explicit interface |
| 82 | * dummy arguments that are coarrays |
| 83 | * dummy arguments that are assumed-shape or assumed-rank arrays |
| 84 | * dummy arguments with parameterized derived types |
| 85 | * dummy arguments that are polymorphic |
| 86 | * function result that is an array |
| 87 | * function result that is `ALLOCATABLE` or `POINTER` |
| 88 | * `CHARACTER` function result whose length is neither constant |
| 89 | nor assumed |
| 90 | * derived type function result with `LEN` type parameter value that is |
| 91 | not constant |
| 92 | (note that result derived type parameters cannot be assumed (C795)) |
| 93 | |
| 94 | Module procedures, internal procedures, procedure pointers, |
| 95 | type-bound procedures, and recursive references by a procedure to itself |
| 96 | always have explicit interfaces. |
| 97 | (Consequently, they cannot be assumed-length `CHARACTER(*)` functions; |
| 98 | conveniently, assumed-length `CHARACTER(*)` functions are prohibited from |
| 99 | recursion (15.6.2.1(3))). |
| 100 | |
| 101 | Other uses of procedures besides calls may also require explicit interfaces, |
| 102 | such as procedure pointer assignment, type-bound procedure bindings, &c. |
| 103 | |
| 104 | Note that non-parameterized monomorphic derived type arguments do |
| 105 | *not* by themselves require the use of an explicit interface. |
| 106 | However, dummy arguments with any derived type parameters *do* |
| 107 | require an explicit interface, even if they are all `KIND` type |
| 108 | parameters. |
| 109 | |
| 110 | 15.5.2.9(2) explicitly allows an assumed-length `CHARACTER(*)` function |
| 111 | to be passed as an actual argument to an explicit-length dummy; |
| 112 | this has implications for calls to character-valued dummy functions |
| 113 | and function pointers. |
| 114 | (In the scopes that reference `CHARACTER` functions, they must have |
| 115 | visible definitions with explicit result lengths.) |
| 116 | |
| 117 | ### Implicit interfaces |
| 118 | |
| 119 | In the absence of any characteristic or context that *requires* an |
| 120 | explicit interface (see above), an external function or subroutine (R503) |
| 121 | or `ENTRY` (R1541) can be called directly or indirectly via its implicit interface. |
| 122 | Each of the arguments can be passed as a simple address, including |
| 123 | dummy procedures. |
| 124 | Procedures that *can* be called via an implicit interface can |
| 125 | undergo more thorough checking |
| 126 | by semantics when an explicit interface for them exists, but they must be |
| 127 | compiled as if all calls to them were through the implicit interface. |
| 128 | This note will mention special handling for procedures that are exposed |
| 129 | to the possibility of being called with an implicit interface as *F77ish* procedures |
| 130 | below; this is of course not standard terminology. |
| 131 | |
| 132 | Internal and module subprograms that are ever passed as arguments &/or |
| 133 | assigned as targets of procedure pointers may be F77ish. |
| 134 | |
| 135 | Every F77ish procedure can and must be distinguished at compilation time. |
| 136 | Such procedures should respect the external naming conventions (when external) |
| 137 | and any legacy ABI used for Fortran '77 programs on the target architecture, |
| 138 | so that portable libraries can be compiled |
| 139 | and used by distinct implementations (and their versions) |
| 140 | of Fortran. |
| 141 | |
| 142 | Note that F77ish functions still have known result types, possibly by means |
| 143 | of implicit typing of their names. |
| 144 | They can also be `CHARACTER(*)` assumed-length character functions. |
| 145 | |
| 146 | In other words: these F77sh procedures that do not require the use of an explicit |
| 147 | interface and that can possibly be referenced, directly or indirectly, |
| 148 | with implicit interfaces are limited to argument lists that comprise |
| 149 | only the addresses of effective arguments and the length of a `CHARACTER` function result |
| 150 | (when there is one), and they can return only scalar values with constant |
| 151 | type parameter values. |
| 152 | None of their arguments or results need be (or can be) implemented |
| 153 | with descriptors, |
| 154 | and any internal procedures passed to them as arguments must be |
| 155 | simple addresses of non-internal subprograms or trampolines for |
| 156 | internal procedures. |
| 157 | |
| 158 | Note that the `INTENT` attribute does not, by itself, |
| 159 | require the use of explicit interface; neither does the use of a dummy |
| 160 | procedure (implicit or explicit in their interfaces). |
| 161 | So the analysis of calls to F77ish procedures must allow for the |
| 162 | invisible use of `INTENT(OUT)`. |
| 163 | |
| 164 | ## Protocol overview |
| 165 | |
| 166 | Here is a summary script of all of the actions that may need to be taken |
| 167 | by the calling procedure and its referenced procedure to effect |
| 168 | the call, entry, exit, and return steps of the procedure reference |
| 169 | protocol. |
| 170 | The order of these steps is not particularly strict, and we have |
| 171 | some design alternatives that are explored further below. |
| 172 | |
| 173 | ### Before the call: |
| 174 | |
| 175 | 1. Compute &/or copy into temporary storage the values of |
| 176 | some effective argument expressions and designators (see below). |
| 177 | 1. Create and populate descriptors for arguments that use them |
| 178 | (see below). |
| 179 | 1. Possibly allocate function result storage, |
| 180 | when its size can be known by all callers; function results that are |
| 181 | neither `POINTER` nor `ALLOCATABLE` must have explicit shapes (C816). |
| 182 | 1. Create and populate a descriptor for the function result, if it |
| 183 | needs one (deferred-shape/-length `POINTER`, any `ALLOCATABLE`, |
| 184 | derived type with non-constant length parameters, &c.). |
| 185 | 1. Capture the values of host-escaping local objects in memory; |
| 186 | package them into single address (for calls to internal procedures & |
| 187 | for calls that pass internal procedures as arguments). |
| 188 | 1. Resolve the target procedure's polymorphic binding, if any. |
| 189 | 1. Marshal effective argument addresses (or values for `%VAL()` and some |
| 190 | discretionary `VALUE` arguments) into registers. |
| 191 | 1. Marshal `CHARACTER` argument lengths in additional value arguments for |
| 192 | `CHARACTER` effective arguments not passed via descriptors. |
| 193 | These lengths must be 64-bit integers. |
| 194 | 1. Marshal an extra argument for the length of a `CHARACTER` function |
| 195 | result if the function is F77ish. |
| 196 | 1. Marshal an extra argument for the function result's descriptor, |
| 197 | if it needs one. |
| 198 | 1. Set the "host instance" (static link) register when calling an internal |
| 199 | procedure from its host or another internal procedure, a procedure pointer, |
| 200 | or dummy procedure (when it has a descriptor). |
| 201 | 1. Jump. |
| 202 | |
| 203 | ### On entry: |
| 204 | 1. For subprograms with alternate `ENTRY` points: shuffle `ENTRY` dummy arguments |
| 205 | set a compiler-generated variable to identify the alternate entry point, |
| 206 | and jump to the common entry point for common processing and a `switch()` |
| 207 | to the statement after the `ENTRY`. |
| 208 | 1. Capture `CHARACTER` argument &/or assumed-length result length values. |
| 209 | 1. Complete `VALUE` copying if this step will not always be done |
| 210 | by the caller (as I think it should be). |
| 211 | 1. Finalize &/or re-initialize `INTENT(OUT)` non-pointer |
| 212 | effective arguments (see below). |
| 213 | 1. For interoperable procedures called from C: compact discontiguous |
| 214 | dummy argument values when necessary (`CONTIGUOUS` &/or |
| 215 | explicit-shape/assumed-size arrays of assumed-length `CHARACTER(*)`). |
| 216 | 1. Optionally compact assumed-shape arguments for contiguity on one |
| 217 | or more leading dimensions to improve SIMD vectorization, if not |
| 218 | `TARGET` and not already sufficiently contiguous. |
| 219 | (PGI does this in the caller, whether the callee needs it or not.) |
| 220 | 1. Complete allocation of function result storage, if that has |
| 221 | not been done by the caller. |
| 222 | 1. Initialize components of derived type local variables, |
| 223 | including the function result. |
| 224 | |
| 225 | Execute the callee, populating the function result or selecting |
| 226 | the subroutine's alternate return. |
| 227 | |
| 228 | ### On exit: |
| 229 | 1. Clean up local scope (finalization, deallocation) |
| 230 | 1. Deallocate `VALUE` argument temporaries. |
| 231 | (But don't finalize them; see 7.5.6.3(3)). |
| 232 | 1. Replace any assumed-shape argument data that were compacted on |
| 233 | entry for contiguity when the data were possibly |
| 234 | modified across the call (never when `INTENT(IN)` or `VALUE`). |
| 235 | 1. Identify alternate `RETURN` to caller. |
| 236 | 1. Marshal results. |
| 237 | 1. Jump |
| 238 | |
| 239 | ### On return to the caller: |
| 240 | 1. Save the result registers, if any. |
| 241 | 1. Copy effective argument array designator data that was copied into |
| 242 | a temporary back into its original storage (see below). |
| 243 | 1. Complete deallocation of effective argument temporaries (not `VALUE`). |
| 244 | 1. Reload definable host-escaping local objects from memory, if they |
| 245 | were saved to memory by the host before the call. |
| 246 | 1. `GO TO` alternate return, if any. |
| 247 | 1. Use the function result in an expression. |
| 248 | 1. Eventually, finalize &/or deallocate the function result. |
| 249 | |
| 250 | (I've omitted some obvious steps, like preserving/restoring callee-saved |
| 251 | registers on entry/exit, dealing with caller-saved registers before/after |
| 252 | calls, and architecture-dependent ABI requirements.) |
| 253 | |
| 254 | ## The messy details |
| 255 | |
| 256 | ### Copying effective argument values into temporary storage |
| 257 | |
| 258 | There are several conditions that require the compiler to generate |
| 259 | code that allocates and populates temporary storage for an actual |
| 260 | argument. |
| 261 | |
| 262 | First, effective arguments that are expressions, not designators, obviously |
| 263 | need to be computed and captured into memory in order to be passed |
| 264 | by reference. |
| 265 | This includes parenthesized designators like `(X)`, which are |
| 266 | expressions in Fortran, as an important special case. |
| 267 | (This case also technically includes unparenthesized constants, |
| 268 | but those are better implemented by passing addresses in read-only |
| 269 | memory.) |
| 270 | The dummy argument cannot be known to have `INTENT(OUT)` or |
| 271 | `INTENT(IN OUT)`. |
| 272 | |
| 273 | Small scalar or elemental `VALUE` arguments may be passed in registers, |
| 274 | as should arguments wrapped in the legacy VMS `%VAL()` notation. |
| 275 | Multiple elemental `VALUE` arguments might be packed into SIMD registers. |
| 276 | |
| 277 | Effective arguments that are designators, not expressions, must also |
| 278 | be copied into temporaries in the following situations. |
| 279 | |
| 280 | 1. Coindexed objects need to be copied into the local image. |
| 281 | This can get very involved if they contain `ALLOCATABLE` |
| 282 | components, which also need to be copied, along with their |
| 283 | `ALLOCATABLE` components, and may be best implemented with a runtime |
| 284 | library routine working off a description of the type. |
| 285 | 1. Effective arguments associated with dummies with the `VALUE` |
| 286 | attribute need to be copied; this can be done on either |
| 287 | side of the call, but there are optimization opportunities |
| 288 | available when the caller's side bears the responsibility. |
| 289 | 1. In non-elemental calls, the values of array sections with |
| 290 | vector-valued subscripts need to be gathered into temporaries. |
| 291 | These effective arguments are not definable, and they are not allowed to |
| 292 | be associated with non-`VALUE` dummy arguments with the attributes |
| 293 | `INTENT(OUT)`, `INTENT(IN OUT)`, `ASYNCHRONOUS`, or `VOLATILE` |
| 294 | (15.5.2.4(21)); `INTENT()` can't always be checked. |
| 295 | 1. Non-simply-contiguous (9.5.4) arrays being passed to non-`POINTER` |
| 296 | dummy arguments that must be contiguous (due to a `CONTIGUOUS` |
| 297 | attribute, or not being assumed-shape or assumed-rank; this |
| 298 | is always the case for F77ish procedures). |
| 299 | This should be a runtime decision, so that effective arguments |
| 300 | that turn out to be contiguous can be passed cheaply. |
| 301 | This rule does not apply to coarray dummies, whose effective arguments |
| 302 | are required to be simply contiguous when this rule would otherwise |
| 303 | force the use of a temporary (15.5.2.8); neither does it apply |
| 304 | to `ASYNCHRONOUS` and `VOLATILE` effective arguments, which are |
| 305 | disallowed when copies would be necessary (C1538 - C1540). |
| 306 | *Only temporaries created by this contiguity requirement are |
| 307 | candidates for being copied back to the original variable after |
| 308 | the call* (see below). |
| 309 | |
| 310 | Fortran requires (18.3.6(5)) that calls to interoperable procedures |
| 311 | with dummy argument arrays with contiguity requirements |
| 312 | handle the compaction of discontiguous data *in the Fortran callee*, |
| 313 | at least when called from C. |
| 314 | And discontiguous data must be compacted on the *caller's* side |
| 315 | when passed from Fortran to C (18.3.6(6)). |
| 316 | |
| 317 | We could perform all argument compaction (discretionary or |
| 318 | required) in the callee, but there are many cases where the |
| 319 | compiler knows that the effective argument data are contiguous |
| 320 | when compiling the caller (a temporary is needed for other reasons, |
| 321 | or the effective argument is simply contiguous) and a run-time test for |
| 322 | discontiguity in the callee can be avoided by using a caller-compaction |
| 323 | convention when we have the freedom to choose. |
| 324 | |
| 325 | While we are unlikely to want to _needlessly_ use a temporary for |
| 326 | an effective argument that does not require one for any of these |
| 327 | reasons above, we are specifically disallowed from doing so |
| 328 | by the standard in cases where pointers to the original target |
| 329 | data are required to be valid across the call (15.5.2.4(9-10)). |
| 330 | In particular, compaction of assumed-shape arrays for discretionary |
| 331 | contiguity on the leading dimension to ease SIMD vectorization |
| 332 | cannot be done safely for `TARGET` dummies without `VALUE`. |
| 333 | |
| 334 | Effective arguments associated with known `INTENT(OUT)` dummies that |
| 335 | require allocation of a temporary -- and this can only be for reasons of |
| 336 | contiguity -- don't have to populate it, but they do have to perform |
| 337 | minimal initialization of any `ALLOCATABLE` components so that |
| 338 | the runtime doesn't crash when the callee finalizes and deallocates |
| 339 | them. |
| 340 | `ALLOCATABLE` coarrays are prohibited from being affected by `INTENT(OUT)` |
| 341 | (see C846). |
| 342 | Note that calls to implicit interfaces must conservatively allow |
| 343 | for the use of `INTENT(OUT)` by the callee. |
| 344 | |
| 345 | Except for `VALUE` and known `INTENT(IN)` dummy arguments, the original |
| 346 | contents of local designators that have been compacted into temporaries |
| 347 | could optionally have their `ALLOCATABLE` components invalidated |
| 348 | across the call as an aid to debugging. |
| 349 | |
| 350 | Except for `VALUE` and known `INTENT(IN)` dummy arguments, the contents of |
| 351 | the temporary storage will be copied back into the effective argument |
| 352 | designator after control returns from the procedure, and it may be necessary |
| 353 | to preserve addresses (or the values of subscripts and cosubscripts |
| 354 | needed to recalculate them) of the effective argument designator, or its |
| 355 | elements, in additional temporary storage if they can't be safely or |
| 356 | quickly recomputed after the call. |
| 357 | |
| 358 | ### `INTENT(OUT)` preparation |
| 359 | |
| 360 | Effective arguments that are associated with `INTENT(OUT)` |
| 361 | dummy arguments are required to be definable. |
| 362 | This cannot always be checked, as the use of `INTENT(OUT)` |
| 363 | does not by itself mandate the use of an explicit interface. |
| 364 | |
| 365 | `INTENT(OUT)` arguments are finalized (as if) on entry to the called |
| 366 | procedure. In particular, in calls to elemental procedures, |
| 367 | the elements of an array are finalized by a scalar or elemental |
| 368 | `FINAL` procedure (7.5.6.3(7)). |
| 369 | |
| 370 | Derived type components that are `ALLOCATABLE` are finalized |
| 371 | and deallocated; they are prohibited from being coarrays. |
| 372 | Components with initializers are (re)initialized. |
| 373 | |
| 374 | The preparation of effective arguments for `INTENT(OUT)` could be |
| 375 | done on either side of the call. If the preparation is |
| 376 | done by the caller, there is an optimization opportunity |
| 377 | in situations where unmodified incoming `INTENT(OUT)` dummy |
| 378 | arguments whose types lack `FINAL` procedures are being passed |
| 379 | onward as outgoing `INTENT(OUT)` arguments. |
| 380 | |
| 381 | ### Arguments and function results requiring descriptors |
| 382 | |
| 383 | Dummy arguments are represented with the addresses of new descriptors |
| 384 | when they have any of the following characteristics: |
| 385 | |
| 386 | 1. assumed-shape array (`DIMENSION::A(:)`) |
| 387 | 1. assumed-rank array (`DIMENSION::A(..)`) |
| 388 | 1. parameterized derived type with assumed `LEN` parameters |
| 389 | 1. polymorphic (`CLASS(T)`, `CLASS(*)`) |
| 390 | 1. assumed-type (`TYPE(*)`) |
| 391 | 1. coarray dummy argument |
| 392 | 1. `INTENT(IN) POINTER` argument (15.5.2.7, C.10.4) |
| 393 | |
| 394 | `ALLOCATABLE` and other `POINTER` arguments can be passed by simple |
| 395 | address. |
| 396 | |
| 397 | Non-F77ish procedures use descriptors to represent two further |
| 398 | kinds of dummy arguments: |
| 399 | |
| 400 | 1. assumed-length `CHARACTER(*)` |
| 401 | 1. dummy procedures |
| 402 | |
| 403 | F77ish procedures use other means to convey character length and host instance |
| 404 | links (respectively) for these arguments. |
| 405 | |
| 406 | Function results are described by the caller & callee in |
| 407 | a caller-supplied descriptor when they have any of the following |
| 408 | characteristics, some which necessitate an explicit interface: |
| 409 | |
| 410 | 1. deferred-shape array (so `ALLOCATABLE` or `POINTER`) |
| 411 | 1. derived type with any non-constant `LEN` parameter |
| 412 | (C795 prohibit assumed lengths) |
| 413 | 1. procedure pointer result (when the interface must be explicit) |
| 414 | |
| 415 | Storage for a function call's result is allocated by the caller when |
| 416 | possible: the result is neither `ALLOCATABLE` nor `POINTER`, |
| 417 | the shape is scalar or explicit, and the type has `LEN` parameters |
| 418 | that are constant expressions. |
| 419 | In other words, the result doesn't require the use of a descriptor |
| 420 | but can't be returned in registers. |
| 421 | This allows a function result to be written directly into a local |
| 422 | variable or temporary when it is safe to treat the variable as if |
| 423 | it were an additional `INTENT(OUT)` argument. |
| 424 | (Storage for `CHARACTER` results, assumed or explicit, is always |
| 425 | allocated by the caller, and the length is always passed so that |
| 426 | an assumed-length external function will work when eventually |
| 427 | called from a scope that declares the length that it will use |
| 428 | (15.5.2.9 (2)).) |
| 429 | |
| 430 | Note that the lower bounds of the dimensions of non-`POINTER` |
| 431 | non-`ALLOCATABLE` dummy argument arrays are determined by the |
| 432 | callee, not the caller. |
| 433 | (A Fortran pitfall: declaring `A(0:9)`, passing it to a dummy |
| 434 | array `D(:)`, and assuming that `LBOUND(D,1)` will be zero |
| 435 | in the callee.) |
| 436 | If the declaration of an assumed-shape dummy argument array |
| 437 | contains an explicit lower bound expression (R819), its value |
| 438 | needs to be computed by the callee; |
| 439 | it may be captured and saved in the incoming descriptor |
| 440 | as long as we assume that argument descriptors can be modified |
| 441 | by callees. |
| 442 | Callers should fill in all of the fields of outgoing |
| 443 | non-`POINTER` non-`ALLOCATABLE` argument |
| 444 | descriptors with the assumption that the callee will use 1 for |
| 445 | lower bound values, and callees can rely on them being 1 if |
| 446 | not modified. |
| 447 | |
| 448 | ### Copying temporary storage back into argument designators |
| 449 | |
| 450 | Except for `VALUE` and known `INTENT(IN)` dummy arguments and array sections |
| 451 | with vector-valued subscripts (15.5.2.4(21)), temporary storage into |
| 452 | which effective argument data were compacted for contiguity before the call |
| 453 | must be redistributed back to its original storage by the caller after |
| 454 | the return. |
| 455 | |
| 456 | In conjunction with saved cosubscript values, a standard descriptor |
| 457 | would suffice to represent a pointer to the original storage into which the |
| 458 | temporary data should be redistributed; |
| 459 | the descriptor need not be fully populated with type information. |
| 460 | |
| 461 | Note that coindexed objects with `ALLOCATABLE` ultimate components |
| 462 | are required to be associated only with dummy arguments with the |
| 463 | `VALUE` &/or `INTENT(IN)` attributes (15.6.2.4(6)), so there is no |
| 464 | requirement that the local image somehow reallocate remote storage |
| 465 | when copying the data back. |
| 466 | |
| 467 | ### Polymorphic bindings |
| 468 | |
| 469 | Calls to the type-bound procedures of monomorphic types are |
| 470 | resolved at compilation time, as are calls to `NON_OVERRIDABLE` |
| 471 | type-bound procedures. |
| 472 | The resolution of calls to overridable type-bound procedures of |
| 473 | polymorphic types must be completed at execution (generic resolution |
| 474 | of type-bound procedure bindings from effective argument types, kinds, |
| 475 | and ranks is always a compilation-time task (15.5.6, C.10.6)). |
| 476 | |
| 477 | Each derived type that declares or inherits any overridable |
| 478 | type-bound procedure bindings must correspond to a static constant |
| 479 | table of code addresses (or, more likely, a static constant type |
| 480 | description containing or pointing to such a table, along with |
| 481 | information used by the runtime support library for initialization, |
| 482 | copying, finalization, and I/O of type instances). Each overridable |
| 483 | type-bound procedure in the type corresponds to an index into this table. |
| 484 | |
| 485 | ### Host instance linkage |
| 486 | |
| 487 | Calls to dummy procedures and procedure pointers that resolve to |
| 488 | internal procedures need to pass an additional "host instance" argument that |
| 489 | addresses a block of storage in the stack frame of their |
| 490 | host subprogram that was active at the time they were passed as an |
| 491 | effective argument or associated with a procedure pointer. |
| 492 | This is similar to a static link in implementations of programming |
| 493 | languages with nested subprograms, although Fortran only allows |
| 494 | one level of nesting. |
| 495 | The 64-bit x86 and little-endian OpenPower ABIs reserve registers |
| 496 | for this purpose (`%r10` & `R11`); 64-bit ARM has a reserved register |
| 497 | that can be used (`x18`). |
| 498 | |
| 499 | The host subprogram objects that are visible to any of their internal |
| 500 | subprograms need to be resident in memory across any calls to them |
| 501 | (direct or not). Any host subprogram object that might be defined |
| 502 | during a call to an internal subprogram needs to be reloaded after |
| 503 | a call or reside permanently in memory. |
| 504 | A simple conservative analysis of the internal subprograms can |
| 505 | identify all of these escaping objects and their definable subset. |
| 506 | |
| 507 | The address of the host subprogram storage used to hold the escaping |
| 508 | objects needs to be saved alongside the code address(es) that |
| 509 | represent a procedure pointer. |
| 510 | It also needs to be conveyed alongside the text address for a |
| 511 | dummy procedure. |
| 512 | |
| 513 | For F77ish procedures, we cannot use a "procedure pointer descriptor" |
| 514 | to pass a procedure argument -- they expect to receive a single |
| 515 | address argument. |
| 516 | We will need to package the host instance link in a trampoline |
| 517 | that loads its address into the designated register. |
| 518 | |
| 519 | GNU Fortran and Intel Fortran construct trampolines by writing |
| 520 | a sequence of machine instructions to a block of storage in the |
| 521 | host's stack frame, which requires the stack to be executable, |
| 522 | which seems inadvisable for security reasons; |
| 523 | XLF manages trampolines in its runtime support library, which adds some overhead |
| 524 | to their construction and a reclamation obligation; |
| 525 | NAG Fortran manages a static fixed-sized stack of trampolines |
| 526 | per call site, imposing a hidden limit on recursion and foregoing |
| 527 | reentrancy; |
| 528 | PGI passes host instance links in descriptors in additional arguments |
| 529 | that are not always successfully forwarded across implicit interfaces, |
| 530 | sometimes leading to crashes when they turn out to be needed. |
| 531 | |
| 532 | F18 will manage a pool of trampolines in its runtime support library |
| 533 | that can be used to pass internal procedures as effective arguments |
| 534 | to F77ish procedures, so that |
| 535 | a bare code address can serve to represent the effective argument. |
| 536 | But targets that can only be called with an explicit interface |
| 537 | have the option of using a "fat pointer" (or additional argument) |
| 538 | to represent a dummy procedure closure so as |
| 539 | to avoid the overhead of constructing and reclaiming a trampoline. |
| 540 | Procedure descriptors can also support multiple code addresses. |
| 541 | |
| 542 | ### Naming |
| 543 | |
| 544 | External subroutines and functions (R503) and `ENTRY` points (R1541) |
| 545 | with `BIND(C)` (R808) have linker-visible names that are either explicitly |
| 546 | specified in the program or determined by straightforward rules. |
| 547 | The names of other F77ish external procedures should respect the conventions |
| 548 | of the target architecture for legacy Fortran '77 programs; this is typically |
| 549 | something like `foo_`. |
| 550 | |
| 551 | In other cases, however, we have fewer constraints on external naming, |
| 552 | as well as some additional requirements and goals. |
| 553 | |
| 554 | Module procedures need to be distinguished by the name of their module |
| 555 | and (when they have one) the submodule where their interface was |
| 556 | defined. |
| 557 | Note that submodule names are distinct in their modules, not hierarchical, |
| 558 | so at most two levels of qualification are needed. |
| 559 | |
| 560 | Pure `ELEMENTAL` functions (15.8) must use distinct names for any alternate |
| 561 | entry points used for packed SIMD arguments of various widths if we support |
| 562 | calls to these functions in SIMD parallel contexts. |
| 563 | There are already conventions for these names in `libpgmath`. |
| 564 | |
| 565 | The names of non-F77ish external procedures |
| 566 | should be distinguished as such so that incorrect attempts to call or pass |
| 567 | them with an implicit interface will fail to resolve at link time. |
| 568 | Fortran 2018 explicitly enables us to do this with a correction to Fortran |
| 569 | 2003 in 4.3.4(5). |
| 570 | |
| 571 | Last, there must be reasonably permanent naming conventions used |
| 572 | by the F18 runtime library for those unrestricted specific intrinsic |
| 573 | functions (table 16.2 in 16.8) and extensions that can be passed as |
| 574 | arguments. |
| 575 | |
| 576 | In these cases where external naming is at the discretion |
| 577 | of the implementation, we should use names that are not in the C language |
| 578 | user namespace, begin with something that identifies |
| 579 | the current incompatible version of F18, the module, the submodule, and |
| 580 | elemental SIMD width, and are followed by the external name. |
| 581 | The parts of the external name can be separated by some character that |
| 582 | is acceptable for use in LLVM IR and assembly language but not in user |
| 583 | Fortran or C code, or by switching case |
| 584 | (so long as there's a way to cope with extension names that don't begin |
| 585 | with letters). |
| 586 | |
| 587 | In particular, the period (`.`) seems safe to use as a separator character, |
| 588 | so a `Fa.` prefix can serve to isolate these discretionary names from |
| 589 | other uses and to identify the earliest link-compatible version. |
| 590 | For examples: `Fa.mod.foo`, `Fa.mod.submod.foo`, and (for an external |
| 591 | subprogram that requires an explicit interface) `Fa.foo`. |
| 592 | When the ABI changes in the future in an incompatible way, the |
| 593 | initial prefix becomes `Fb.`, `Fc.`, &c. |
| 594 | |
| 595 | ## Summary of checks to be enforced in semantics analysis |
| 596 | |
| 597 | 8.5.10 `INTENT` attributes |
| 598 | * (C846) An `INTENT(OUT)` argument shall not be associated with an |
| 599 | object that is or has an allocatable coarray. |
| 600 | * (C847) An `INTENT(OUT)` argument shall not have `LOCK_TYPE` or `EVENT_TYPE`. |
| 601 | |
| 602 | 8.5.18 `VALUE` attribute |
| 603 | * (C863) The argument cannot be assumed-size, a coarray, or have a coarray |
| 604 | ultimate component. |
| 605 | * (C864) The argument cannot be `ALLOCATABLE`, `POINTER`, `INTENT(OUT)`, |
| 606 | `INTENT(IN OUT)`, or `VOLATILE`. |
| 607 | * (C865) If the procedure is `BIND(C)`, the argument cannot be `OPTIONAL`. |
| 608 | |
| 609 | 15.5.1 procedure references: |
| 610 | * (C1533) can't pass non-intrinsic `ELEMENTAL` as argument |
| 611 | * (C1536) alternate return labels must be in the inclusive scope |
| 612 | * (C1537) coindexed argument cannot have a `POINTER` ultimate component |
| 613 | |
| 614 | 15.5.2.4 requirements for non-`POINTER` non-`ALLOCATABLE` dummies: |
| 615 | * (2) dummy must be monomorphic for coindexed polymorphic actual |
| 616 | * (2) dummy must be polymorphic for assumed-size polymorphic actual |
| 617 | * (2) dummy cannot be `TYPE(*)` if effective is PDT or has TBPs or `FINAL` |
| 618 | * (4) character length of effective cannot be less than dummy |
| 619 | * (6) coindexed effective with `ALLOCATABLE` ultimate component requires |
| 620 | `INTENT(IN)` &/or `VALUE` dummy |
| 621 | * (13) a coindexed scalar effective requires a scalar dummy |
| 622 | * (14) a non-conindexed scalar effective usually requires a scalar dummy, |
| 623 | but there are some exceptions that allow elements of storage sequences |
| 624 | to be passed and treated like explicit-shape or assumed-size arrays |
| 625 | (see 15.5.2.11) |
| 626 | * (16) array rank agreement |
| 627 | * (20) `INTENT(OUT)` & `INTENT(IN OUT)` dummies require definable actuals |
| 628 | * (21) array sections with vector subscripts can't be passed to definable dummies |
| 629 | (`INTENT(OUT)`, `INTENT(IN OUT)`, `ASYNCHRONOUS`, `VOLATILE`) |
| 630 | * (22) `VOLATILE` attributes must match when dummy has a coarray ultimate component |
| 631 | * (C1538 - C1540) checks for `ASYNCHRONOUS` and `VOLATILE` |
| 632 | |
| 633 | 15.5.2.5 requirements for `ALLOCATABLE` & `POINTER` arguments when both |
| 634 | the dummy and effective arguments have the same attributes: |
| 635 | * (2) both or neither can be polymorphic |
| 636 | * (2) both are unlimited polymorphic or both have the same declared type |
| 637 | * (3) rank compatibility |
| 638 | * (4) effective argument must have deferred the same type parameters as the dummy |
| 639 | |
| 640 | 15.5.2.6 `ALLOCATABLE` dummy arguments: |
| 641 | * (2) effective must be `ALLOCATABLE` |
| 642 | * (3) corank must match |
| 643 | * (4) coindexed effective requires `INTENT(IN)` dummy |
| 644 | * (7) `INTENT(OUT)` & `INTENT(IN OUT)` dummies require definable actuals |
| 645 | |
| 646 | 15.5.2.7 `POINTER` dummy arguments: |
| 647 | * (C1541) `CONTIGUOUS` dummy requires simply contiguous actual |
| 648 | * (C1542) effective argument cannot be coindexed unless procedure is intrinsic |
| 649 | * (2) effective argument must be `POINTER` unless dummy is `INTENT(IN)` and |
| 650 | effective could be the right-hand side of a pointer assignment statement |
| 651 | |
| 652 | 15.5.2.8 corray dummy arguments: |
| 653 | * (1) effective argument must be coarray |
| 654 | * (1) `VOLATILE` attributes must match |
| 655 | * (2) explicitly or implicitly contiguous dummy array requires a simply contiguous actual |
| 656 | |
| 657 | 15.5.2.9 dummy procedures: |
| 658 | * (1) explicit dummy procedure interface must have same characteristics as actual |
| 659 | * (5) dummy procedure `POINTER` requirements on effective arguments |
| 660 | |
| 661 | 15.6.2.1 procedure definitions: |
| 662 | * `NON_RECURSIVE` procedures cannot recurse. |
| 663 | * Assumed-length `CHARACTER(*)` functions cannot be declared as `RECURSIVE`, array-valued, |
| 664 | `POINTER`, `ELEMENTAL`, or `PURE' (C723), and cannot be called recursively (15.6.2.1(3)). |
| 665 | * (C823) A function result cannot be a coarray or contain a coarray ultimate component. |
| 666 | |
| 667 | `PURE` requirements (15.7): C1583 - C1599. |
| 668 | These also apply to `ELEMENTAL` procedures that are not `IMPURE`. |
| 669 | |
| 670 | `ELEMENTAL` requirements (15.8.1): C15100-C15103, |
| 671 | and C1533 (can't pass as effective argument unless intrinsic) |
| 672 | |
| 673 | For interoperable procedures and interfaces (18.3.6): |
| 674 | * C1552 - C1559 |
| 675 | * function result is scalar and of interoperable type (C1553, 18.3.1-3) |
| 676 | * `VALUE` arguments are scalar and of interoperable type |
| 677 | * `POINTER` dummies cannot be `CONTIGUOUS` (18.3.6 paragraph 2(5)) |
| 678 | * assumed-type dummies cannot be `ALLOCATABLE`, `POINTER`, assumed-shape, or assumed-rank (18.3.6 paragraph 2 (5)) |
| 679 | * `CHARACTER` dummies that are `ALLOCATABLE` or `POINTER` must be deferred-length |
| 680 | |
| 681 | ## Further topics to document |
| 682 | |
| 683 | * Alternate return specifiers |
| 684 | * `%VAL()`, `%REF()`, and `%DESCR()` legacy VMS interoperability extensions |
| 685 | * Unrestricted specific intrinsic functions as effective arguments |
| 686 | * SIMD variants of `ELEMENTAL` procedures (& unrestricted specific intrinsics) |
| 687 | * Elemental subroutine calls with array arguments |