Reputation: 326
Part of the FORTRAN Syntax is FORMAT for example:
WRITE(*,10)'this is a string', 'this is a second string', 'this is a third string'
10 FORMAT(1x,a10,1x,a10,1x,a20)
What I want to know is when the code is formatted, is it compile-time or execution-time? How could I test this? Checking the differences in assembly?
Upvotes: 2
Views: 347
Reputation: 60058
The format strings are typically interpreted at runtime. After all those edits it is not that typical after all, see Steve's answer. The answer describes gfortran which might even be less typical, but is important and widespread. Especially when the format string is a character variable (that can be anything), but also the FORMAT statement. You often get an error message for incorrect format only at runtime. For example:
WRITE(*,10) x
10 FORMAT(i0)
END
gives
> ./a.out
At line 8 of file format3.f90 (unit = 6, file = 'stdout')
Fortran runtime error: Expected INTEGER for item 1 in formatted transfer, got REAL
(i0)
^
In your case:
gfortran -O3 format3.f90 -fdump-tree-optimized
gives
dt_parm.0.common.filename = &"format3.f90"[1]{lb: 1 sz: 1};
dt_parm.0.common.line = 1;
dt_parm.0.format = &"(1x,a10,1x,a10,1x,a20)"[1]{lb: 1 sz: 1};
dt_parm.0.format_len = 22;
dt_parm.0.common.flags = 4096;
dt_parm.0.common.unit = 6;
_gfortran_st_write (&dt_parm.0);
_gfortran_transfer_character_write (&dt_parm.0, &"this is a string"[1]{lb: 1 sz: 1}, 16);
_gfortran_transfer_character_write (&dt_parm.0, &"this is a second string"[1]{lb: 1 sz: 1}, 23);
_gfortran_transfer_character_write (&dt_parm.0, &"this is a third string"[1]{lb: 1 sz: 1}, 22);
_gfortran_st_write_done (&dt_parm.0);
dt_parm.0 ={v} {CLOBBER};
The _gfortran_st_write (&dt_parm.0);
sets the writing mode and that includes the format string.
You can notice that the format is stored in a character variable and could be interpreted at runtime if necessary, but it is not actually used in the code generated by the compiler. It is potentially used inside _gfortran_transfer_character_write
that are part of the libgfortran runtime library.
The above is for gfortran. One can imagine a lot can be actually optimized away and compiled, but I am not aware that compilers do that.
If you are curious, the gfortran format interpretting code is in https://github.com/gcc-mirror/gcc/blob/trunk/libgfortran/io/format.c
Upvotes: 3
Reputation: 7267
The compilers I have worked on have always translated the format to an internal representation at compile time, other than formats stored in a character variable. The run-time system still has to have the ability to "compile" formats at run-time, but the compiler can check for errors. The actual formatting of the output always happens at run-time.
Upvotes: 3