1## Concept
2After a Fortran subprogram has been parsed, its names resolved, and all its
3semantic constraints successfully checked, the parse tree of its
4executable part is translated into another abstract representation,
5namely the _control flow graph_ described in this note.
6
7This second representation of the subprogram's executable part is
8suitable for analysis and incremental modification as the subprogram
9is readied for code generation.
10Many high-level Fortran features are implemented by rewriting portions
11of a subprogram's control flow graph in place.
12
13### Control Flow Graph
14A _control flow graph_ is a collection of simple (_i.e.,_ "non-extended")
15basic _blocks_ that comprise straight-line sequences of _actions_ with a
16single entry point and a single exit point, and a collection of
17directed flow _edges_ (or _arcs_) denoting all possible transitions of
18control flow that may take place during execution from the end of
19one basic block to the beginning of another (or itself).
20
21A block that has multiple distinct successors in the flow of control
22must end with an action that selects its successor.
23
24The sequence of actions that constitutes a basic block may
25include references to user and library procedures.
26Subprogram calls with implicit control flow afterwards, namely
27alternate returns and `END=`/`ERR=` labels on input/output,
28will be lowered in translation to a representation that materializes
29that control flow into something similar to a computed `GO TO` or
30C language `switch` statement.
31
32For convenience in optimization and to simplify the implementation of
33data flow confluence functions, we may choose to maintain the
34property that each flow arc is the sole outbound arc emanating from
35its originating block, the sole inbound arc arriving at its destination,
36or both.
37Empty blocks would inserted to "split" arcs when necessary to maintain this
38invariant property.
39
40Fortran subprograms (other than internal subprograms) can have multiple
41entry points by using the obsolescent `ENTRY` statement.
42We will implement such subprograms by constructing a union
43of their dummy argument lists and using it as part of the definition
44of a new subroutine or function that can be called by each of
45the entry points, which are then all converted into wrapper routines that
46pass a selector value as an additional argument to drive a `switch` on entry
47to the new subprogram.
48
49This transformation ensures that every subprogram's control
50flow graph has a well-defined `START` node.
51
52Statement labels can be used in Fortran on any statement, but only
53the labels that decorate legal destinations of `GO TO` statements
54need to be implemented in the control flow graph.
55Specifically, non-executable statements like `DATA`, `NAMELIST`, and
56`FORMAT` statements will be extracted into data initialization
57records before or during the construction of the control flow
58graph, and will survive only as synonyms for `CONTINUE`.
59
60Nests of multiple labeled `DO` loops that terminate on the same
61label will be have that label rewritten so that `GO TO` within
62the loop nest will arrive at the copy that most closely nests
63the context.
64The Fortran standard does not require us to do this, but XLF
65(at least) works this way.
66
67### Expressions and Statements (Operations and Actions)
68Expressions are trees, not DAGs, of intrinsic operations,
69resolved function references, constant literals, and
70data designators.
71
72Expression nodes are represented in the compiler in a type-safe manner.
73There is a distinct class or class template for every category of
74intrinsic type, templatized over its supported kind type parameter values.
75
76Operands are storage-owning indirections to other instances
77of `Expression`, instances of constant values, and to representations
78of data and function references.
79These indirections are not nullable apart from the situation in which
80the operands of an expression are being removed for use elsewhere before
81the expression is destructed.
82
83The ranks and the extents of the shapes of the results of expressions
84are explicit for constant arrays and recoverable by analysis otherwise.
85
86Parenthesized subexpressions are scrupulously preserved in accordance with
87the Fortran standard.
88
89The expression tree is meant to be a representation that is
90as equally well suited for use in the symbol table (e.g., for
91a bound of an explicit shape array) as it is for an action
92in a basic block of the control flow graph (e.g., the right
93hand side of an assignment statement).
94
95Each basic block comprises a linear sequence of _actions_.
96These are represented as a doubly-linked list so that insertion
97and deletion can be done in constant time.
98
99Only the last action in a basic block can represent a change
100to the flow of control.
101
102### Scope Transitions
103Some of the various scopes of the symbol table are visible in the control flow
104graph as `SCOPE ENTRY` and `SCOPE EXIT` actions.
105`SCOPE ENTRY` actions are unique for their corresponding scopes,
106while `SCOPE EXIT` actions need not be so.
107It must be the case that
108any flow of control within the subprogram will enter only scopes that are
109not yet active, and exit only the most recently entered scope that has not
110yet been deactivated; i.e., when modeled by a push-down stack that is
111pushed by each traversal of a `SCOPE ENTRY` action,
112the entries of the stack are always distinct, only the scope at
113the top of the stack is ever popped by `SCOPE EXIT`, and the stack is empty
114when the subprogram terminates.
115Further, any references to resolved symbols must be to symbols whose scopes
116are active.
117
118The `DEALLOCATE` actions and calls to `FINAL` procedures implied by scoped
119lifetimes will be explicit in the sequence of actions in the control flow
120graph.
121
122Parallel regions might be partially represented by scopes, or by explicit
123operations similar to the scope entry and exit operations.
124
125### Data Flow Representation
126The subprogram text will be in static single assignment form by the time the
127subprogram arrives at the bridge to the LLVM IR builder.
128Merge points are actions at the heads of basic blocks whose operands
129are definition points; definition points are actions at the ends of
130basic blocks whose operands are expression trees (which may refer to
131merge points).
132
133### Rewriting Transformations
134
135#### I/O
136#### Dynamic allocation
137#### Array constructors
138
139#### Derived type initialization, deallocation, and finalization
140The machinery behind the complicated semantics of Fortran's derived types
141and `ALLOCATABLE` objects will be implemented in large part by the run time
142support library.
143
144#### Actual argument temporaries
145#### Array assignments, `WHERE`, and `FORALL`
146
147Array operations have shape.
148
149`WHERE` masks have shape.
150Their effects on array operations are by means of explicit `MASK` operands that
151are part of array assignment operations.
152
153#### Intrinsic function and subroutine calls
154