1932aae77SSourabh Singh Tomar<!--===- docs/RuntimeDescriptor.md 2932aae77SSourabh Singh Tomar 3932aae77SSourabh Singh Tomar Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4932aae77SSourabh Singh Tomar See https://llvm.org/LICENSE.txt for license information. 5932aae77SSourabh Singh Tomar SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6932aae77SSourabh Singh Tomar 7932aae77SSourabh Singh Tomar--> 8932aae77SSourabh Singh Tomar 9271a7bb1SRichard Barton# Runtime Descriptors 10271a7bb1SRichard Barton 11*b7ff0320Scor3ntin```{contents} 12*b7ff0320Scor3ntin--- 13*b7ff0320Scor3ntinlocal: 14*b7ff0320Scor3ntin--- 15271a7bb1SRichard Barton``` 16271a7bb1SRichard Barton 17eaff2004Ssameeran joshi## Concept 18eaff2004Ssameeran joshiThe properties that characterize data values and objects in Fortran 19eaff2004Ssameeran joshiprograms must sometimes be materialized when the program runs. 20eaff2004Ssameeran joshi 21eaff2004Ssameeran joshiSome properties are known during compilation and constant during 22eaff2004Ssameeran joshiexecution, yet must be reified anyway for execution in order to 23eaff2004Ssameeran joshidrive the interfaces of a language support library or the mandated 24eaff2004Ssameeran joshiinterfaces of interoperable (i.e., C) procedure calls. 25eaff2004Ssameeran joshi 26eaff2004Ssameeran joshiNote that many Fortran intrinsic subprograms have interfaces 27eaff2004Ssameeran joshithat are more flexible and generic than actual Fortran subprograms 28eaff2004Ssameeran joshican be, so properties that must be known during compilation and 29eaff2004Ssameeran joshiare constant during execution may still need to be materialized 30eaff2004Ssameeran joshifor calls to the library, even if only by modifying names to 31eaff2004Ssameeran joshidistinguish types or their kind specializations. 32eaff2004Ssameeran joshi 33eaff2004Ssameeran joshiOther properties are deferred to execution, and need to be represented 34eaff2004Ssameeran joshito serve the needs of compiled code and the run time support library. 35eaff2004Ssameeran joshi 36eaff2004Ssameeran joshiPrevious implementations of Fortran have typically defined a small 37eaff2004Ssameeran joshisheaf of _descriptor_ data structures for this purpose, and attached 38eaff2004Ssameeran joshithese descriptors as additional hidden arguments, type components, 39eaff2004Ssameeran joshiand local variables so as to convey dynamic characteristics between 40eaff2004Ssameeran joshisubprograms and between user code and the run-time support library. 41eaff2004Ssameeran joshi 42eaff2004Ssameeran joshi### References 43eaff2004Ssameeran joshiReferences are to the 12-2017 draft of the Fortran 2018 standard 44eaff2004Ssameeran joshi(N2146). 45eaff2004Ssameeran joshi 46eaff2004Ssameeran joshiSection 15.4.2.2 can be interpreted as a decent list of things that 47eaff2004Ssameeran joshimight need descriptors or other hidden state passed across a 48eaff2004Ssameeran joshisubprogram call, since such features (apart from assumed-length 49eaff2004Ssameeran joshi`CHARACTER` function results) trigger a requirement for the 50eaff2004Ssameeran joshisubprogram to have an explicit interface visible to their callers. 51eaff2004Ssameeran joshi 52eaff2004Ssameeran joshiSection 15.5.2 has good laundry lists of situations that can arise 53eaff2004Ssameeran joshiacross subprogram call boundaries. 54eaff2004Ssameeran joshi 55eaff2004Ssameeran joshi## A survey of dynamic characteristics 56eaff2004Ssameeran joshi 57eaff2004Ssameeran joshi### Length of assumed-length `CHARACTER` function results (B.3.6) 58eaff2004Ssameeran joshi``` 59eaff2004Ssameeran joshiCHARACTER*8 :: FOO 60eaff2004Ssameeran joshiPRINT *, FOO('abcdefghijklmnopqrstuvwxyz') 61eaff2004Ssameeran joshi... 62eaff2004Ssameeran joshiCHARACTER*(*) FUNCTION FOO(STR) 63eaff2004Ssameeran joshi CHARACTER*26 STR 64eaff2004Ssameeran joshi FOO=STR 65eaff2004Ssameeran joshiEND 66eaff2004Ssameeran joshi``` 67eaff2004Ssameeran joshi 68eaff2004Ssameeran joshiprints `abcdefgh` because the length parameter of the character type 69eaff2004Ssameeran joshiof the result of `FOO` is passed across the call -- even in the absence 70eaff2004Ssameeran joshiof an explicit interface! 71eaff2004Ssameeran joshi 72eaff2004Ssameeran joshi### Assumed length type parameters (7.2) 73eaff2004Ssameeran joshiDummy arguments and associate names for `SELECT TYPE` can have assumed length 74eaff2004Ssameeran joshitype parameters, which are denoted by asterisks (not colons). 75eaff2004Ssameeran joshiTheir values come from actual arguments or the associated expression (resp.). 76eaff2004Ssameeran joshi 77eaff2004Ssameeran joshi### Explicit-shape arrays (8.5.8.2) 78eaff2004Ssameeran joshiThe expressions used for lower and upper bounds must be captured and remain 79eaff2004Ssameeran joshiinvariant over the scope of an array, even if they contain references to 80eaff2004Ssameeran joshivariables that are later modified. 81eaff2004Ssameeran joshi 82eaff2004Ssameeran joshiExplicit-shape arrays can be dummy arguments, "adjustable" local variables, 83eaff2004Ssameeran joshiand components of derived type (using specification expressions in terms 84eaff2004Ssameeran joshiof constants and KIND type parameters). 85eaff2004Ssameeran joshi 86eaff2004Ssameeran joshi### Leading dimensions of assumed-size arrays (8.5.8.5) 87eaff2004Ssameeran joshi``` 88eaff2004Ssameeran joshiSUBROUTINE BAR(A) 89eaff2004Ssameeran joshi REAL A(2,3,*) 90eaff2004Ssameeran joshiEND 91eaff2004Ssameeran joshi``` 92eaff2004Ssameeran joshiThe total size and final dimension's extent do not constitute dynamic 93eaff2004Ssameeran joshiproperties. 94eaff2004Ssameeran joshiThe called subprogram has no means to extract the extent of the 95eaff2004Ssameeran joshilast (major) dimension, and may not depend upon it implicitly by using 96eaff2004Ssameeran joshithe array in any context that demands a known shape. 97eaff2004Ssameeran joshi 98eaff2004Ssameeran joshiThe values of the expressions used as the bounds of the dimensions 99eaff2004Ssameeran joshithat appear prior to 100eaff2004Ssameeran joshithe last dimension are, however, effectively captured on entry to the 101eaff2004Ssameeran joshisubprogram, and remain invariant even if the variables that appear in 102eaff2004Ssameeran joshithose expressions have their values modified later. 103eaff2004Ssameeran joshiThis is similar to the requirements for an explicit-shape array. 104eaff2004Ssameeran joshi 105eaff2004Ssameeran joshi### Some function results 106eaff2004Ssameeran joshi1. Deferred-shape 107eaff2004Ssameeran joshi2. Deferred length type parameter values 108eaff2004Ssameeran joshi3. Stride information for `POINTER` results 109eaff2004Ssameeran joshi 110eaff2004Ssameeran joshiNote that while function result variables can have the `ALLOCATABLE` 111eaff2004Ssameeran joshiattribute, the function itself and the value returned to the caller 112eaff2004Ssameeran joshido not possess the attribute. 113eaff2004Ssameeran joshi 114eaff2004Ssameeran joshi### Assumed-shape arrays 115eaff2004Ssameeran joshiThe extents of the dimensions of assumed-shape dummy argument arrays 116eaff2004Ssameeran joshiare conveyed from those of the actual effective arguments. 117eaff2004Ssameeran joshiThe bounds, however, are not. The called subprogram can define the 118eaff2004Ssameeran joshilower bound to be a value other than 1, but that is a local effect 119eaff2004Ssameeran joshionly. 120eaff2004Ssameeran joshi 121eaff2004Ssameeran joshi### Deferred-shape arrays 122eaff2004Ssameeran joshiThe extents and bounds of `POINTER` and `ALLOCATABLE` arrays are 123eaff2004Ssameeran joshiestablished by pointer assignments and `ALLOCATE` statements. 124eaff2004Ssameeran joshiNote that dummy arguments and function results that are `POINTER` 125eaff2004Ssameeran joshior `ALLOCATABLE` can be deferred-shape, not assumed-shape -- one cannot 126eaff2004Ssameeran joshisupply a lower bound expression as a local effect. 127eaff2004Ssameeran joshi 128eaff2004Ssameeran joshi### Strides 129eaff2004Ssameeran joshiSome arrays can have discontiguous (or negative) strides. 130eaff2004Ssameeran joshiThese include assumed-shape dummy arguments and deferred-shape 131eaff2004Ssameeran joshi`POINTER` variables, components, and function results. 132eaff2004Ssameeran joshi 133eaff2004Ssameeran joshiFortran disallows some conceivable cases that might otherwise 134eaff2004Ssameeran joshirequire implied strides, such as passing an array of an extended 135eaff2004Ssameeran joshiderived type as an actual argument that corresponds to a 136eaff2004Ssameeran joshinonpolymorphic dummy array of a base type, or the similar 137eaff2004Ssameeran joshicase of pointer assignment to a base of an extended derived type. 138eaff2004Ssameeran joshi 139eaff2004Ssameeran joshiOther arrays, including `ALLOCATABLE`, can be assured to 140eaff2004Ssameeran joshibe contiguous, and do not necessarily need to manage or 141eaff2004Ssameeran joshiconvey dynamic stride information. 142eaff2004Ssameeran joshi`CONTIGUOUS` dummy arguments and `POINTER` arrays need not 143eaff2004Ssameeran joshirecord stride information either. 144eaff2004Ssameeran joshi(The standard notes that a `CONTIGUOUS POINTER` occupies a 145eaff2004Ssameeran joshinumber of storage units that is distinct from that required 146eaff2004Ssameeran joshito hold a non-`CONTIGUOUS` pointer.) 147eaff2004Ssameeran joshi 148eaff2004Ssameeran joshiNote that Fortran distinguishes the `CONTIGUOUS` attribute from 149eaff2004Ssameeran joshithe concept of being known or required to be _simply contiguous_ (9.5.4), 150eaff2004Ssameeran joshiwhich includes `CONTIGUOUS` entities as well as many others, and 151eaff2004Ssameeran joshithe concept of actually _being_ contiguous (8.5.7) during execution. 152eaff2004Ssameeran joshiI believe that the property of being simply contiguous implies 153eaff2004Ssameeran joshithat an entity is known at compilation time to not require the 154eaff2004Ssameeran joshiuse or maintenance of hidden stride values. 155eaff2004Ssameeran joshi 156eaff2004Ssameeran joshi### Derived type component initializers 157eaff2004Ssameeran joshiFortran allows components of derived types to be declared with 158eaff2004Ssameeran joshiinitial values that are to be assigned to the components when an 159eaff2004Ssameeran joshiinstance of the derived type is created. 160eaff2004Ssameeran joshiThese include `ALLOCATABLE` components, which are always initialized 161eaff2004Ssameeran joshito a deallocated state. 162eaff2004Ssameeran joshi 163eaff2004Ssameeran joshiThese can be implemented with constructor subroutines, inline 164eaff2004Ssameeran joshistores or block copies from static initializer blocks, or a sequence 165eaff2004Ssameeran joshiof sparse offset/size/value component initializers to be emplaced 166eaff2004Ssameeran joshiby the run-time library. 167eaff2004Ssameeran joshi 168eaff2004Ssameeran joshiN.B. Fortran allows kind type parameters to appear in component 169eaff2004Ssameeran joshiinitialization constant expressions, but not length type parameters, 170eaff2004Ssameeran joshiso the initialization values are constants. 171eaff2004Ssameeran joshi 172eaff2004Ssameeran joshiN.B. Initialization is not assignment, and cannot be implemented 173eaff2004Ssameeran joshiwith assignments to uninitialized derived type instances from 174eaff2004Ssameeran joshistatic constant initializers. 175eaff2004Ssameeran joshi 176eaff2004Ssameeran joshi### Polymorphic `CLASS()`, `CLASS(*)`, and `TYPE(*)` 177eaff2004Ssameeran joshiType identification for `SELECT TYPE`. 178eaff2004Ssameeran joshiDefault initializers (see above). 179eaff2004Ssameeran joshiOffset locations of `ALLOCATABLE` and polymorphic components. 180eaff2004Ssameeran joshiPresence of `FINAL` procedures. 181eaff2004Ssameeran joshiMappings to overridable type-bound specific procedures. 182eaff2004Ssameeran joshi 183eaff2004Ssameeran joshi### Deferred length type parameters 184eaff2004Ssameeran joshiDerived types with length type parameters, and `CHARACTER`, may be used 185eaff2004Ssameeran joshiwith the values of those parameters deferred to execution. 186eaff2004Ssameeran joshiTheir actual values must be maintained as characteristics of the dynamic 187eaff2004Ssameeran joshitype that is associated with a value or object 188eaff2004Ssameeran joshi. 189eaff2004Ssameeran joshiA single copy of the deferred length type parameters suffices for 190eaff2004Ssameeran joshiall of the elements of an array of that parameterized derived type. 191eaff2004Ssameeran joshi 192eaff2004Ssameeran joshi### Components whose types and/or shape depends on length type parameters 193eaff2004Ssameeran joshiNon-pointer, non-allocatable components whose types or shapes are expressed 194eaff2004Ssameeran joshiin terms of length type parameters will probably have to be implemented as 195eaff2004Ssameeran joshiif they had deferred type and/or shape and were `ALLOCATABLE`. 196eaff2004Ssameeran joshiThe derived type instance constructor must allocate them and possibly 197eaff2004Ssameeran joshiinitialize them; the instance destructor must deallocate them. 198eaff2004Ssameeran joshi 199eaff2004Ssameeran joshi### Assumed rank arrays 200eaff2004Ssameeran joshiRank is almost always known at compilation time and would be redundant 201eaff2004Ssameeran joshiin most circumstances if also managed dynamically. 202eaff2004Ssameeran joshi`DIMENSION(..)` dummy arguments (8.5.8.7), however, are a recent feature 203eaff2004Ssameeran joshiwith which the rank of a whole array is dynamic outside the cases of 204eaff2004Ssameeran joshia `SELECT RANK` construct. 205eaff2004Ssameeran joshi 206eaff2004Ssameeran joshiThe lower bounds of the dimensions of assumed rank arrays 207eaff2004Ssameeran joshiare always 1. 208eaff2004Ssameeran joshi 209eaff2004Ssameeran joshi### Cached invariant subexpressions for addressing 210eaff2004Ssameeran joshiImplementations of Fortran have often maintained precalculated integer 211eaff2004Ssameeran joshivalues to accelerate subscript computations. 212eaff2004Ssameeran joshiFor example, given `REAL*8 :: A(2:4,3:5)`, the data reference `A(I,J)` 213eaff2004Ssameeran joshiresolves to something like `&A + 8*((I-2)+3*(J-3))`, and this can be 214eaff2004Ssameeran joshieffectively reassociated to `&A - 88 + 8*I + 24*J` 215eaff2004Ssameeran joshior `&A - 88 + 8*(I + 3*J)`. 216eaff2004Ssameeran joshiWhen the offset term and coefficients are not compile-time constants, 217eaff2004Ssameeran joshithey are at least invariant and can be precomputed. 218eaff2004Ssameeran joshi 219eaff2004Ssameeran joshiIn the cases of dummy argument arrays, `POINTER`, and `ALLOCATABLE`, 220eaff2004Ssameeran joshithese addressing invariants could be managed alongside other dynamic 221eaff2004Ssameeran joshiinformation like deferred extents and lower bounds to avoid their 222eaff2004Ssameeran joshirecalculation. 223eaff2004Ssameeran joshiIt's not clear that it's worth the trouble to do so, since the 224eaff2004Ssameeran joshiexpressions are invariant and cheap. 225eaff2004Ssameeran joshi 226eaff2004Ssameeran joshi### Coarray state (8.5.6) 227eaff2004Ssameeran joshiA _coarray_ is an `ALLOCATABLE` variable or component, or statically 228eaff2004Ssameeran joshiallocated variable (`SAVE` attribute explicit or implied), or dummy 229eaff2004Ssameeran joshiargument whose ultimate effective argument is one of such things. 230eaff2004Ssameeran joshi 231eaff2004Ssameeran joshiEach image in a team maintains its portion of each coarray and can 232eaff2004Ssameeran joshiaccess those portions of the coarray that are maintained by other images 233eaff2004Ssameeran joshiin the team. 234eaff2004Ssameeran joshiAllocations and deallocations are synchronization events at which 235eaff2004Ssameeran joshithe several images can exchange whatever information is needed by 236eaff2004Ssameeran joshithe underlying intercommunication interface to access the data 237eaff2004Ssameeran joshiof their peers. 238eaff2004Ssameeran joshi(Strictly speaking, an implementation could synchronize 239eaff2004Ssameeran joshiimages at allocations and deallocations with simple barriers, and defer 240eaff2004Ssameeran joshithe communication of remote access information until it is needed for a 241eaff2004Ssameeran joshigiven coarray on a given image, so long as it could be acquired in a 242eaff2004Ssameeran joshi"one-sided" fashion.) 243eaff2004Ssameeran joshi 244eaff2004Ssameeran joshi### Presence of `OPTIONAL` dummy arguments 245eaff2004Ssameeran joshiTypically indicated with null argument addresses. 246eaff2004Ssameeran joshiNote that `POINTER` and `ALLOCATABLE` objects can be passed to 247eaff2004Ssameeran joshinon-`POINTER` non-`ALLOCATABLE` dummy arguments, and their 248eaff2004Ssameeran joshiassociation or allocation status (resp.) determines the presence 249eaff2004Ssameeran joshiof the dummy argument. 250eaff2004Ssameeran joshi 251eaff2004Ssameeran joshi### Stronger contiguity enforcement or indication 252eaff2004Ssameeran joshiSome implementations of Fortran guarantee that dummy argument arrays 253eaff2004Ssameeran joshiare, or have been made to be, contiguous on one or more dimensions 254eaff2004Ssameeran joshiwhen the language does not require them to be so (8.5.7 p2). 255eaff2004Ssameeran joshiOthers pass a flag to identify contiguous arrays (or could pass the 256eaff2004Ssameeran joshinumber of contiguous leading dimensions, although I know of no such 257eaff2004Ssameeran joshiimplementation) so that optimizing transformations that depend on 258eaff2004Ssameeran joshicontiguity can be made conditional with multiple-version code generation 259eaff2004Ssameeran joshiand selected during execution. 260eaff2004Ssameeran joshi 261eaff2004Ssameeran joshiIn the absence of a contiguity guarantee or flag, the called side 262eaff2004Ssameeran joshiwould have to determine contiguity dynamically, if it cares, 263eaff2004Ssameeran joshiby calculating addresses of elements in the array whose subscripts 264eaff2004Ssameeran joshidiffer by exactly 1 on exactly 1 dimension of interest, and checking 265eaff2004Ssameeran joshiwhether that difference exactly matches the byte size of the type times 266eaff2004Ssameeran joshithe product of the extents of any prior dimensions. 267eaff2004Ssameeran joshi 268eaff2004Ssameeran joshi### Host instances for dummy procedures and procedure pointers 269eaff2004Ssameeran joshiA static link or other means of accessing the imported state of the 270eaff2004Ssameeran joshihost procedure must be available when an internal procedure is 271eaff2004Ssameeran joshiused as an actual argument or as a pointer assignment target. 272eaff2004Ssameeran joshi 273eaff2004Ssameeran joshi### Alternate returns 274eaff2004Ssameeran joshiSubroutines (only) with alternate return arguments need a 275eaff2004Ssameeran joshimeans, such as the otherwise unused function return value, by which 276eaff2004Ssameeran joshito distinguish and identify the use of an alternate `RETURN` statement. 277eaff2004Ssameeran joshiThe protocol can be a simple nonzero integer that drives a switch 278eaff2004Ssameeran joshiin the caller, or the caller can pass multiple return addresses as 279eaff2004Ssameeran joshiarguments for the callee to substitute on the stack for the original 280eaff2004Ssameeran joshireturn address in the event of an alternate `RETURN`. 281eaff2004Ssameeran joshi 282eaff2004Ssameeran joshi## Implementation options 283eaff2004Ssameeran joshi 284eaff2004Ssameeran joshi### A note on array descriptions 285eaff2004Ssameeran joshiSome arrays require dynamic management of distinct combinations of 286eaff2004Ssameeran joshivalues per dimension. 287eaff2004Ssameeran joshi 288eaff2004Ssameeran joshiOne can extract the extent on a dimension from its bounds, or extract 289eaff2004Ssameeran joshithe upper bound from the extent and the lower bound. Having distinct 290eaff2004Ssameeran joshiextent and upper bound would be redundant. 291eaff2004Ssameeran joshi 292eaff2004Ssameeran joshiContiguous arrays can assume a stride of 1 on each dimension. 293eaff2004Ssameeran joshi 294eaff2004Ssameeran joshiAssumed-shape and assumed-size dummy argument arrays need not convey 295eaff2004Ssameeran joshilower bounds. 296eaff2004Ssameeran joshi 297eaff2004Ssameeran joshiSo there are examples of dimensions with 298eaff2004Ssameeran joshi * extent only (== upper bound): `CONTIGUOUS` assumed-shape, explict shape and multidimensional assumed-size with constant lower bound 299eaff2004Ssameeran joshi * lower bound and either extent or upper bound: `ALLOCATABLE`, `CONTIGUOUS` `POINTER`, general explicit-shape and multidimensional assumed-size 300eaff2004Ssameeran joshi * extent (== upper bound) and stride: general (non-`CONTIGUOUS`) assumed-shape 301eaff2004Ssameeran joshi * lower bound, stride, and either extent or upper bound: general (non-`CONTIGUOUS`) `POINTER`, assumed-rank 302eaff2004Ssameeran joshi 303eaff2004Ssameeran joshiand these cases could be accompanied by precomputed invariant 304eaff2004Ssameeran joshiaddressing subexpressions to accelerate indexing calculations. 305eaff2004Ssameeran joshi 306eaff2004Ssameeran joshi### Interoperability requirements 307eaff2004Ssameeran joshi 308eaff2004Ssameeran joshiFortran 2018 requires that a Fortran implementation supply a header file 309eaff2004Ssameeran joshi`ISO_Fortran_binding.h` for use in C and C++ programs that defines and 310eaff2004Ssameeran joshiimplements an interface to Fortran objects from the _interoperable_ 311eaff2004Ssameeran joshisubset of Fortran objects and their types suitable for use when those 312eaff2004Ssameeran joshiobjects are passed to C functions. 313eaff2004Ssameeran joshiThis interface mandates a fat descriptor that is passed by address, 314eaff2004Ssameeran joshicontaining (at least) 315eaff2004Ssameeran joshi * a data base address 316eaff2004Ssameeran joshi * explicit rank and type 317eaff2004Ssameeran joshi * flags to distinguish `POINTER` and `ALLOCATABLE` 318eaff2004Ssameeran joshi * elemental byte size, and 319eaff2004Ssameeran joshi * (per-dimension) lower bound, extent, and byte stride 320eaff2004Ssameeran joshi 321eaff2004Ssameeran joshiThe requirements on the interoperability API do not mandate any 322eaff2004Ssameeran joshisupport for features like derived type component initialization, 323eaff2004Ssameeran joshiautomatic deallocation of `ALLOCATABLE` components, finalization, 324eaff2004Ssameeran joshiderived type parameters, data contiguity flags, &c. 325eaff2004Ssameeran joshiBut neither does the Standard preclude inclusion of additional 326eaff2004Ssameeran joshiinterfaces to describe and support such things. 327eaff2004Ssameeran joshi 328eaff2004Ssameeran joshiGiven a desire to fully support the Fortran 2018 language, we need 329eaff2004Ssameeran joshito either support the interoperability requirements as a distinct 330eaff2004Ssameeran joshispecialization of the procedure call protocol, or use the 331eaff2004Ssameeran joshi`ISO_Fortran_binding.h` header file requirements as a subset basis for a 332eaff2004Ssameeran joshicomplete implementation that adds representations for all the 333eaff2004Ssameeran joshimissing capabilities, which would be isolated and named so as 334eaff2004Ssameeran joshito prevent user C code from relying upon them. 335eaff2004Ssameeran joshi 336eaff2004Ssameeran joshi### Design space 337eaff2004Ssameeran joshiThere is a range of possible options for representing the 338eaff2004Ssameeran joshiproperties of values and objects during the execution of Fortran 339eaff2004Ssameeran joshiprograms. 340eaff2004Ssameeran joshi 341eaff2004Ssameeran joshiAt one extreme, the amount of dynamic information is minimized, 342eaff2004Ssameeran joshiand is packaged in custom data structures or additional arguments 343eaff2004Ssameeran joshifor each situation to convey only the values that are unknown at 344eaff2004Ssameeran joshicompilation time and actually needed at execution time. 345eaff2004Ssameeran joshi 346eaff2004Ssameeran joshiAt the other extreme, data values and objects are described completely, 347eaff2004Ssameeran joshiincluding even the values of properties are known at compilation time. 348eaff2004Ssameeran joshiThis is not as silly as it sounds -- e.g., Fortran array descriptors 349eaff2004Ssameeran joshihave historically materialized the number of dimensions they cover, even 350eaff2004Ssameeran joshithough rank will be (nearly) always be a known constant during compilation. 351eaff2004Ssameeran joshi 352eaff2004Ssameeran joshiWhen data are packaged, their containers can be self-describing to 353eaff2004Ssameeran joshisome degree. 354eaff2004Ssameeran joshiDescription records can have tag values or strings. 355eaff2004Ssameeran joshiTheir fields can have presence flags or identifying tags, and fields 356eaff2004Ssameeran joshineed not have fixed offsets or ordering. 357eaff2004Ssameeran joshiThis flexibility can increase binary compatibility across revisions 358eaff2004Ssameeran joshiof the run-time support library, and is convenient for debugging 359eaff2004Ssameeran joshithat library. 360eaff2004Ssameeran joshiHowever, it is not free. 361eaff2004Ssameeran joshi 362eaff2004Ssameeran joshiFurther, the requirements of the representation of dynamic 363eaff2004Ssameeran joshiproperties of values and objects depend on the execution model: 364eaff2004Ssameeran joshispecifically, are the complicated semantics of intrinsic assignment, 365eaff2004Ssameeran joshideallocation, and finalization of allocatables implemented entirely 366eaff2004Ssameeran joshiin the support library, in generated code for non-recursive cases, 367eaff2004Ssameeran joshior by means of a combination of the two approaches? 368eaff2004Ssameeran joshi 369eaff2004Ssameeran joshiConsider how to implement the following: 370eaff2004Ssameeran joshi``` 371eaff2004Ssameeran joshiTYPE :: LIST 372eaff2004Ssameeran joshi REAL :: HEAD 373eaff2004Ssameeran joshi TYPE(LIST), ALLOCATABLE :: REST 374eaff2004Ssameeran joshiEND TYPE LIST 375eaff2004Ssameeran joshiTYPE(LIST), ALLOCATABLE :: A, B 376eaff2004Ssameeran joshi... 377eaff2004Ssameeran joshiA = B 378eaff2004Ssameeran joshi``` 379eaff2004Ssameeran joshi 380eaff2004Ssameeran joshiFortran requires that `A`'s arbitrary-length linked list be deleted and 381eaff2004Ssameeran joshireplaced with a "deep copy" of `B`'s. 382eaff2004Ssameeran joshiSo either a complicated pair of loops must be generated by the compiler, 383eaff2004Ssameeran joshior a sophisticated run time support library needs to be driven with 384eaff2004Ssameeran joshian expressive representation of type information. 385eaff2004Ssameeran joshi 386eaff2004Ssameeran joshi## Proposal 387eaff2004Ssameeran joshiWe need to write `ISO_Fortran_binding.h` in any event. 388eaff2004Ssameeran joshiIt is a header that is published for use in user C code for interoperation 389eaff2004Ssameeran joshiwith compiled Fortran and the Fortran run time support library. 390eaff2004Ssameeran joshi 391eaff2004Ssameeran joshiThere is a sole descriptor structure defined in `ISO_Fortran_binding.h`. 392eaff2004Ssameeran joshiIt is suitable for characterizing scalars and array sections of intrinsic 393eaff2004Ssameeran joshitypes. 394eaff2004Ssameeran joshiIt is essentially a "fat" data pointer that encapsulates a raw data pointer, 395eaff2004Ssameeran joshia type code, rank, elemental byte size, and per-dimension bounds and stride. 396eaff2004Ssameeran joshi 397eaff2004Ssameeran joshiPlease note that the mandated interoperable descriptor includes the data 398eaff2004Ssameeran joshipointer. 399eaff2004Ssameeran joshiThis design in the Standard precludes the use of static descriptors that 400eaff2004Ssameeran joshicould be associated with dynamic base addresses. 401eaff2004Ssameeran joshi 402eaff2004Ssameeran joshiThe F18 runtime cannot use just the mandated interoperable 403eaff2004Ssameeran joshi`struct CFI_cdesc_t` argument descriptor structure as its 404eaff2004Ssameeran joshiall-purpose data descriptor. 405eaff2004Ssameeran joshiIt has no information about derived type components, overridable 406eaff2004Ssameeran joshitype-bound procedure bindings, type parameters, &c. 407eaff2004Ssameeran joshi 408eaff2004Ssameeran joshiHowever, we could extend the standard interoperable argument descriptor. 409eaff2004Ssameeran joshiThe `struct CFI_cdesc_t` structure is not of fixed size, but we 410eaff2004Ssameeran joshican efficiently locate the first address after an instance of the 411eaff2004Ssameeran joshistandard descriptor and attach our own data record there to 412eaff2004Ssameeran joshihold what we need. 413eaff2004Ssameeran joshiThere's at least one unused padding byte in the standard argument 414eaff2004Ssameeran joshidescriptor that can be used to hold a flag indicating the presence 415eaff2004Ssameeran joshiof the addenda. 416eaff2004Ssameeran joshi 417eaff2004Ssameeran joshiThe definitions of our additional run time data structures must 418eaff2004Ssameeran joshiappear in a header file that is distinct from `ISO_Fortran_binding.h`, 419eaff2004Ssameeran joshiand they should never be used by user applications. 420eaff2004Ssameeran joshi 421eaff2004Ssameeran joshiThis expanded descriptor structure can serve, at least initially for 422eaff2004Ssameeran joshisimplicity, as the sole representation of `POINTER` variables and 423eaff2004Ssameeran joshicomponents, `ALLOCATABLE` variables and components, and derived type 424eaff2004Ssameeran joshiinstances, including length parameter values. 425eaff2004Ssameeran joshi 426eaff2004Ssameeran joshiAn immediate concern with this concept is the amount of space and 427eaff2004Ssameeran joshiinitialization time that would be wasted when derived type components 428eaff2004Ssameeran joshineeding a descriptor would have to be accompanied by an instance 429eaff2004Ssameeran joshiof the general descriptor. 430eaff2004Ssameeran joshi(In the linked list example close above, what could be done with a 431eaff2004Ssameeran joshisingle pointer for the `REST` component would become at least 432eaff2004Ssameeran joshia four-word dynamic structure.) 433eaff2004Ssameeran joshiThis concern is amplified when derived type instances 434eaff2004Ssameeran joshiare allocated as arrays, since the overhead is per-element. 435eaff2004Ssameeran joshi 436eaff2004Ssameeran joshiWe can reduce this wastage in two ways. 437eaff2004Ssameeran joshiFirst, when the content of the component's descriptor is constant 438eaff2004Ssameeran joshiat compilation apart from its base address, a static descriptor 439eaff2004Ssameeran joshican be placed in read-only storage and attached to the description 440eaff2004Ssameeran joshiof the derived type's components. 441eaff2004Ssameeran joshiSecond, we could eventually optimize the storage requirements by 442eaff2004Ssameeran joshiomitting all static fields from the dynamic descriptor, and 443eaff2004Ssameeran joshiexpand the compressed dynamic descriptor during execution when 444eaff2004Ssameeran joshineeded. 445