Commit 30abbeca authored by Árpád Lukács's avatar Árpád Lukács
Browse files

Update RunOld.Rmd

parent fb94fbdb
......@@ -586,3 +586,237 @@ like **R**, it uses doubles as its default numeric format.
# References
# Some notes on translation and compilation on Linux
## Introduction
In this note, I would like to give some short notes on running codes written in various (current and old) programming languages on Linux.
It is not meant to be complete, neither is it addressed at programmers. As an advice: only look at the parts detailing languages you are
not a user of, except if you want to expand it. What you'll see is rudimentary instructions to get an executable and run it, without much
detail or any advanced material.
Once a compilation procedure is worked out for a given code, it is recommended to use `make`. Rules can be defined from one file type to
another (`%.o : %.c` means a rule where an object file is made from, and in rules `$^` can be used for the prerequisites, and `$@` for the
target). An example:
% : %.alw
awe $^ -o $@
my_executable: my_algol_source.alw
Note that GNU make has many built-in pattern rules, e.g., object from C (`%.o:%.c`), object from Fortran (`%.o:%.f`).
## C
C is the most often used programming language in unix-like systems.
Compiling a source:
gcc <options> <source file(s)> -o <output file> -l<library name>
Typical options are `-s` (strip executable of symbol table and relocation information), `-On`, n=1,2,... (optimization). Running it:
./<executable file>
Or compiling into object code
gcc <options> -c <source file> -o <object file>
and linking
gcc <options> <object files> -l<library name> -o <executable file>
For older codes, the `-std=` option can be useful, to set an older C standard: `-std=c90` (available: `c90`, `c99`, `c11`).
## C++
Also modern, often used. Just replace `gcc` with `g++` in the above. Available standard versoions are: `c++98`, `c++03`, `c++11`, `c++14`, `c++1z` (proposed 2017 standard).
## Fortran
Fortran has been the language of most scientific computing for decades. It is still alive and going well, with the standard being updated regularly.
Under Linux, the standard compilers are `f2c`, the Fortran to C translator by Bell Labs, and `gfortran`, the GNU fortran compiler. Other compilers
available are Intel, NAG, and Watcom Fortran, flang, g95, and maybe others.
### f2c
Covered in Prof. Nash's notes. It shall be remarked, that with the default options, intrinsic function evaluations are performed with double precision
variables, even for real results. This may result in different results, than obtained with other compilers.
### gfortran
A full-fledged compiler, part of the GNU compiler collection (with, among others, `gcc`, `g++`, `gnat`, `gpc`). Supports the Fortran 77, 90, 95 and later standards
(to a large extent, if maybe not fully). Also accepts a number of extensions, some of them GNU, some from other compilers (e.g., DEC).
The compilation of a source in a single file is done with the command
gfortran <options> <fortran source file> -o <executble file name>
e.g.,
gfortran test.f -o test
and then the executable is run with the command
./<executable file name>
e.g.,
./test
Options that one may use are `-s` (strip the executable of symbol table and relocation information), `-On`, n=1,2,... (optimization levels), and `-l<library name>`, if a subroutine
library, which is installed on the system is used, e.g.,
gfortran my_calculation.f -o my_calculation -llapack
may be used to compile some calculation that calls lapack subroutines.
If the code is split into separate files, it is possible to compile them one-by-one, and then link the result together. A source file is compiled into
object code with the command
gfortran -c <options> <fortran source file> -o <object file name>
E.g.,
gfortran -c my_subroutines.f -o my_subroutines.o
And then it is possible to link together the full program whith the linker `ld` or with `gfortran`. As the latter will already link the library of fortran intrinsics,
it is easier. The syntax is
fortran <options> <object files> -o <executable>
E.g.,
gfortran my_subroutines.o my_mainprogram.o -o my_program
and run
./my_program
I would like to draw attention to some options that might come in handy when working with old code:
* `-fdefault-real-8` will use `real(kind=8)` for all `real` variables, i.e., promote reals to double precision. It allows one to test how the same code would run
with double precision. Also useful if one gets different results from a code translated with `f2c`.
* `-finit-local-zero` will initialize local variables to zero. Many codes have assumed that variables have the value zero unless changed.
* `-funconstrained-commons` (newer `gfortran`) or `-fno-aggressive-loop-optimizations` tell `gfortran` not to use the size of common blocks to guess how many times a
loop would run. This may be important because of the common, but not really standard conforming practice of declaring variables in common blocks with size 1, e.g.,
for a work array in subroutines, and specifying the actual size (that depends on the size of the problem solved) in the main program. In such a case, with optimization
turned on during the compilation of subroutine files, gfortran would optimize away the loop. An example may be:
a common block used in a subroutine:
SUBROUTINE sub1
DOUBLE PRECISION A
COMMON /work/ A(1)
...
DO I=1,N
do something with A(N)
END DO
and then having a main program
PROGRAM pro1
DOUBLE PRECISION A
COMMON /work/ A(1024)
...
such programs produce errors if compiled with optimization without the `-funconstrained-commons` option.
## Algol 60
Algol 60 is supported under linux with Algol 60 to C translators, `marst`, `jff-algol`, and maybe others.
### MARST
Marst is an Algol 60 to C translator that is part of the GNU project. The authors claim that it is rather standard conformant. It can translate to C, which can be
compiled then with `gcc`.
The syntax is
marst <algol source file> -o <c file>
gcc <options> <c file> -o <executable file> -lalgol
where options may include `-s` or `-On`, n=1,2,.... The option `-lalgol` lets the `libalgol.a` library be linked with the code. After that, the resulting executable can be run
./<executable name>
Marst includes some example programs (some of which were one time considered to be hard). It also accepts the examples that came with `jff-algol`. On the down-side,
the C output is quite unreadable, algol procedures are not translated into C functions. I do not think it is possible to use procedures written in algol, translated
with marst, in another language (e.g., C or R).
Documentation is via the GNU info, or in `/usr/share/doc/marst/marst.pdf.gz`.
Quite importantly, marst comes with a converter of old source formats to the one used by marst,
macvt <options> <oldfilename> -o <algol file name.alg>
where options may be `-c` (classic source format), `-f` (free coding, keywords not enclosed in apostrophes), `-i` (ignore case),
`-m` (more free), `-s` (old semicolon representation `.,`), and `-t` (old ten symbol representation).
### JFF-ALGOL
Jff-algol is another Algol 60 to C translator. It is possible to translate an Algol 60 code to C, or, in one step, to executable:
jff-algol <algol source.alg>
generates an output executable of the same name, a c file (with extension `.c`) and a header file (with extension `.h`). Translation only to
C (without compilation) can be achieved by
jff-a2c -c <tmpfile> -f <header file.h> -o /usr/share/jff-a2c/operator -p /usr/share/jff-a2c/prelude -h /usr/include/jff_header.h <algol file.alg>
At compilation of such C files, the file `/usr/lib/x86_64-linux-gnu/lib_jff.a` has to be linked to the executable (e.g., `-l_jff`), and the standard C
math library is also needed (-lm)
gcc <options> <c source.c> -o<executable name> -l_jff -lm
As for the language, `jff-algol` seems to be more picky than marst. It does not translate marst's examples, but, e.g., stops at variable names containing a
space. I have not checked what the standard prescribes.
The pro: in the case of `jff-algol`, it has been one of the goals of its author to translate algol procedures to C functions, and retain the program structure.
A code translated by `jff-algol` is readable, and probably, linkable to other languages.
Documentation is in `/usr/share/doc/jff-manual.pdf.gz`.
## Algol W
Algol W is also supported with an Algol W to C translator, awe.
### AWE
Awe is an Algol W to C translator. Its author claims full standard conformance. The code is either saved in C, or compiled in one step (with gcc) to an executable:
awe <algol w source.alw>
compiles to an executable of the same name, without the `.alw` extension.
awe -c <algol w source.alw>
only translates to C, and
awe -p <algol w source.alw>
compiles a single algol procedure.
Documentation is available via the unix `man` (as well as in html), and there is an additional Algol W pretty printer available, `awnest`.
One downside is, that the Algol W library only works if compiled without compiler optimizations, which may render the resulting code rather slow, especially
because Algol W operators are translated into function calls, to implement the quite flexible exception handling of Algol W (e.g., division by zero).
## Algol 68
There is a compiler/interpreter, `algol68genie`, which is available in Ubuntu.
## Pascal
Available compilers are GNU pascal, `gpc`, and the Free pascal, `fpc`. Translators are available to C (`p2c`) and C/C++ (`ptoc`).
## PL/M
There is `plm2c`, but as far as I know, no implementation of library functions. The user has to write them, e.g., in C.
## PL/1
There is Iron Springs PL/1 for Linux, free (no costs) but no source. A PL/1 compiler source has been posted online called `pli-32`. The target is windows, the license is unknown.
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment