section 3: fortran evolution of software languagessoft.vub.ac.be/~tjdhondt/esl/fortran_files/lecture...
TRANSCRIPT
Evolution of Software Languages 1
Evolution of
Software Languages
Theo D'Hondt
Bachelor of Computer Science Faculty of Sciences and Bio-Engineering Sciences
Vrije Universiteit Brussel Academic Year 2015-2016
Section 3: Fortran
3: Fortran
Evolution of Software Languages 2 3: Fortran
John Backus(1924-2007)
IBM (fellow) Speedcoding FORTRAN ('54) BNF Algol FP Turing award (acm)
https://en.wikipedia.org/wiki/John_Backus
Evolution of Software Languages 3 3: Fortran
FORTRAN: the first high-level programming language
• developed (by John Backus) in 1954 • designed for the IBM 704 • designed for floating-point applications • provided with sophisticated code-optimisation • fairly efficient • much more readable than machine code • standardised by ANSI in 1966 (FTN IV) • extended ANSI in 1977 (FTN V) with "modern
control- and data structures" • continuously revised • served as a basis (via ALGOL influences) for PL/I
Evolution of Software Languages 4 3: Fortran
DIMENSION DTA(900) SUM = 0.0 READ 10,N 10 FORMAT(I3) DO 20 I=1,N READ 30,DTA(I) 30 FORMAT(F10.6) IF(DTA(I)) 25,20,20 25 DTA(I) = -DTA(I) 20 CONTINUE DO 40 I=1,N SUM = SUM + DTA(I) 40 CONTINUE AVG = SUM / FLOAT(N) PRINT 50, AVG 50 FORMAT(1H , F10.6) STOP
Example
columns 1-5 label field column 6 continuation field columns 7-72 statement field columns 73-80 card sequence field
Evolution of Software Languages 5 3: Fortran
• A program consists of commands • Commands are:
• declarative : interpreted at compile time and contain information about:
DIMENSION DTA(900) DATA DTA, SUM / 901*0.0
• imperative : translated into code and belonging to a category: • arithmetic • control-flow • input-output
Programs1
namebinding
storage allocation
initialisation
Evolution of Software Languages 6 3: Fortran
Programs2
A program consists of:
• one main program • zero or more subroutines and/or functions
A compilation:
• translates main program, subroutines and functions into modules of machine code
• allows for references to other subroutines and functions
Evolution of Software Languages 7 3: Fortran
Linkage
A linkage:
• adds all the necessary code modules of subroutines and functions (transitively) referenced in the main program to the main modules
• substitutes absolute addresses for relocatable addresses
• substitutes absolute addresses for references to subroutines and functions
Evolution of Software Languages 8 3: Fortran
Load
A load:
• loads absolute code into storage • launches the main program starting
with the first imperative command
Evolution of Software Languages 9 3: Fortran
Compilation
Compilation is performed in three steps
• parsing ⇾ verification of lexical, syntactic and semantic structure
• analysis of the imperative structure and detection of optimisable components
• synthesis of relocatable machine code • code generation is optimised for the IBM709
Evolution of Software Languages 10 3: Fortran
The if-statement
The basic selection control structure is the IF command:
IF (expression) labelnegative, labelzero, labelpositive
expression
labelnegative labelzero labelpositive
expression<0 expression>0
expression=0
Evolution of Software Languages 11 3: Fortran
The if-statement
The basic selection control structure is the IF command:
IF (expression) labelnegative, labelzero, labelpositive
expression
labelnegative labelzero labelpositive
expression<0 expression>0
expression=0
The structure principle:
The dynamic structure of a program must be reflected in a simple fashion
in the static structure
Evolution of Software Languages 12 3: Fortran
The if-statement
The basic selection control structure is the IF command:
IF (expression) labelnegative, labelzero, labelpositive
expression
labelnegative labelzero labelpositive
expression<0 expression>0
expression=0The lexical order must correspond to the order in which
code segments are executed
The structure principle:
The dynamic structure of a program must be reflected in a simple fashion
in the static structure
Evolution of Software Languages 13 3: Fortran
The goto-statement
The entire control-flow structure of FORTRAN is based on the GOTO command, closely following the underlying machine architecture
GOTO ( 1, 2, 3), k 1 ... GOTO 9 2 ... GOTO 9 3 ... 9 ...
ASSIGN 2 TO label ... GOTO label,( 1, 2, 3) 1 ... GOTO 9 2 ... GOTO 9 3 ... 9 ... computed gotoassigned goto
Evolution of Software Languages 14 3: Fortran
The goto-statement
The entire control-flow structure of FORTRAN is based on the GOTO command, closely following the underlying machine architecture
GOTO ( 1, 2, 3), k 1 ... GOTO 9 2 ... GOTO 9 3 ... 9 ...
ASSIGN 2 TO label ... GOTO label,( 1, 2, 3) 1 ... GOTO 9 2 ... GOTO 9 3 ... 9 ... computed gotoassigned goto
The consistency principle:
Language constructs with similar appearances need to have similar a
meaning and vice versa
Evolution of Software Languages 15 3: Fortran
The goto-statement
The entire control-flow structure of FORTRAN is based on the GOTO command, closely following the underlying machine architecture
GOTO ( 1, 2, 3), k 1 ... GOTO 9 2 ... GOTO 9 3 ... 9 ...
ASSIGN 2 TO label ... GOTO label,( 1, 2, 3) 1 ... GOTO 9 2 ... GOTO 9 3 ... 9 ... computed gotoassigned goto
The consistency principle:
Language constructs with similar appearances need to have similar a
meaning and vice versa
Constructions such as loops, selections, … must be recognisable
as such in a simple way
Evolution of Software Languages 16 3: Fortran
Iterationa “while” iteration
10 IF(end) GOTO 20 ... GOTO 10 20 ...
a “repeat” iteration
10 ... IF(.NOT.end)GOTO 10
a “loop/exit” iteration
10 ... IF(end)GOTO 20 ... GOTO 10 20 ...
Evolution of Software Languages 17 3: Fortran
Iterationa “while” iteration
10 IF(end) GOTO 20 ... GOTO 10 20 ...
a “repeat” iteration
10 ... IF(.NOT.end)GOTO 10
a “loop/exit” iteration
10 ... IF(end)GOTO 20 ... GOTO 10 20 ...
Principle of defence:
errors that remain undetected at one particular line of defence, should be
detected at a following one
Evolution of Software Languages 18 3: Fortran
Iterationa “while” iteration
10 IF(end) GOTO 20 ... GOTO 10 20 ...
a “repeat” iteration
10 ... IF(.NOT.end)GOTO 10
a “loop/exit” iteration
10 ... IF(end)GOTO 20 ... GOTO 10 20 ...
Principle of defence:
errors that remain undetected at one particular line of defence, should be
detected at a following one
Avoid constructs that become identical due to a
simple typing error
Evolution of Software Languages 19 3: Fortran
Types
FORTRAN is weakly typed:
• the type INTEGER is ‘overworked’
• a ‘label’ for a GOTO can be stored in an INTEGER variable
• there are no ‘range constraints’, and therefore no real validation of array index values
Evolution of Software Languages 20 3: Fortran
Defense
• first line defence ⇾ syntactic control
• second line defence ⇾ static ‘constraint’ control
• third line defence ⇾ dynamic ‘constraint’ control
Evolution of Software Languages 21 3: Fortran
The do-statement1
read-only index stored in register invariant sub-expression
computed ahead of time
invariant index addressed ahead of time
DO 10 I = 1,N A(I) = A(I+2*K)+A(K) 10 CONTINUE
Evolution of Software Languages 22 3: Fortran
The do-statement2
A DO-loop is very important to numerical applications:
• it is (the only) ‘structured’ command
• it is high-level
• it can be nested
• it is a candidate for optimisation
Evolution of Software Languages 23 3: Fortran
The do-statement2
A DO-loop is very important to numerical applications:
• it is (the only) ‘structured’ command
• it is high-level
• it can be nested
• it is a candidate for optimisation
Principle of conservation of information
A language must allow the registration of explicit information which may be
useful to the compiler
Evolution of Software Languages 24 3: Fortran
Subroutines1
Subroutines are ...
• procedural abstractions • essential for modular programming
(‘programming in the large ’ versus ‘programming in the small’)
• essential for library structures • save storage at the cost of a slight
performance loss
Evolution of Software Languages 25 3: Fortran
Subroutines2
Communication between subroutines
• via parameters: systematically ‘call by reference’ • always the most efficient solution
FUNCTION AVGE(ARR, N) DIMENSION ARR(N) SUM = 0.0 DO 10 I = 1,N 10 SUM = SUM + ARR(I) AVGE = SUM/FLOAT(N) RETURN END
Evolution of Software Languages 26 3: Fortran
Subroutines3
Potentially a cause for errors
CALL BUG(0) ... SUBROUTINE BUG(N) N = 1 RETURN END
Evolution of Software Languages 27 3: Fortran
Data types1
• Inspired by mathematics
• Scalar primitives:
- INTEGER
- REAL
- LOGICAL
- DOUBLE (PRECISION)
- COMPLEX
• Mapped onto a word-based memory architecture
Evolution of Software Languages 28 3: Fortran
Data types2
Example CDC 6400 (Cray):
- 60 bits words for REAL with 1 bit sign, 11 bits exponent, 48 bits mantissa
- 60/48 bits for INTEGER for +,- resp. *, /
- 2*60 bits for COMPLEX, DOUBLE
- 60 bits for LOGICAL
Evolution of Software Languages 29 3: Fortran
Operations• Operations are ‘overloaded’ +,-,*,/,** : INTEGER x INTEGER ---> INTEGER +,-,*,/,** : INTEGER x REAL ---> REAL +,-,*,/,** : REAL x INTEGER ---> REAL +,-,*,/,** : REAL x REAL ---> REAL +,-,*,/,** : REAL x COMPLEX ---> COMPLEX ... • ‘Coercion’ : automatic from e.g. INTEGER to
REAL, the inverse via IFIX(...) • Strings are special representations of
INTEGER: on CDC6400 ---> 10Habcde12345 on IBM70x(x) ---> 6Habc123
Evolution of Software Languages 30 3: Fortran
Compound data1
• only via ‘arrays’ • corresponds with most numerical mathematics needs (not e.g. sparse matrices) • implementation of arrays is very efficient
DIMENSION ABC(10,20,30)
@ABC(I,J,K) = @ABC+20*(J-1+10*(I-1))+K-1
• index expressions are limited to:
( [const *] var [ (+|-) const] | const)
Evolution of Software Languages 31 3: Fortran
Compound data2
‘arrays’ allow several kinds of optimisation
DIMENSION A(10,10) ... SUM = 0 DO 1 I = 1,10 1 SUM = SUM + A(I,I) MOVE 0,R1
MOVE @A,R2 MOVE 10,R3 LOOP LOAD R4,R2 ADD R4,R1 ADDC 11,R2 SUBC 1,R3 POS R3,LOOP STOR R1,SUM
Evolution of Software Languages 32 3: Fortran
Visibility
• names of subroutines/functions are globally defined and need be unique
• names of variables are local to a program, subroutine or function and need only be unique within its body
• the name of a variable and e.g. a subroutine may be the same (distinguished by CALL)
• there are no reserved names!
Evolution of Software Languages 33 3: Fortran
Data sharing1
Is done by type-unsafe parameter passing
DIMENSION A(10,10) CALL TRACE(A, 10, S)
...
SUBROUTINE TRACE(X, N, S) DIMENSION X(1) S = 0 J = 1 DO 1 I=1 TO N S = S + X(J) 1 J = J + N + 1 RETURN END
Evolution of Software Languages 34 3: Fortran
Data sharing2
Subprograms can share data structures • a COMMON block is a named area of storage that is
shared by the program, subroutines and/or functions • each of the participating modules interprets this storage
area using (possibly different) declarations • COMMON blocks encourage the use of aliasing:
COMMON /XYZ/ C(10),D(5,5) COMPLEX C DOUBLE D
...
COMMON /XYZ/ R(10,2), X(50)
with possibly unforeseen consequences...
Evolution of Software Languages 35 3: Fortran
Data sharing3Data can be aliased within the body of one routine:
• the EQUIVALENCE statement “synchronises” the address of several data structures
DIMENSION A(2,10), B(10), C(5) EQUIVALENCE (A(1,1),B(1)) EQUIVALENCE (B(6), C(1))
• this declarations stops the automatic address allocation used by the compiler
• is “normally” used to save storage • is very machine dependent: depends on the
internal representation of the different primitive types (e.g. redefining INTEGER as LOGICAL in order to apply .AND. , .OR. and .NOT.
Evolution of Software Languages 36 3: Fortran
Dynamic data1
• The ‘blank COMMON‘ is used because it was originally loaded at the end of the storage, giving access to the remainder of unused memory
• Via ‘loader’-commands the size of a “heap” is regulated
COMMON HEAP(1)
• Using EQUIVALENCE a partition can be made:
DIMENSION VAL(1000),NEXT(1000) EQUIVALENCE (VAL(1), HEAP(1)) EQUIVALENCE (NEXT(1), HEAP(1001))
Evolution of Software Languages 37 3: Fortran
Dynamic data2
Operations can be defined on these structures:
FUNCTION NEW NEW = FREE FREE = NEXT(FREE) RETURN END
SUBROUTINE STORE(V,P) INTEGER P VAL(P) = V RETURN END
FUNCTION FETCH(P) INTEGER P FETCH = VAL(P) RETURN END
Evolution of Software Languages 38 3: Fortran
Parsing• lexical ‘punched card’-format :
interaction between lexing and scanning
• spaces are fully redundant COMPLEXFUNCTIONABCDEF
is the same as COMPLEX FUNCTION ABCDEF
• grammatical disasters occur: DO 999 IJK = 1. 5
IF(IJK) = GOTO12
DIMENSION FORMAT(100) ... 1 FORMAT(11H) = 10*(I-J)