Some thoughts on temporaries for the front-end

Daniel Kraft d@domob.eu
Tue Aug 5 16:26:00 GMT 2008


Hi all,

for finalization of derived-types, the most puzzling pieces still 
missing are probably those two cases (already discussed in somewhat detail):

x = foo (x)
(CALL) foo (bar ())

In the first case, we need to evaluate the RHS, finalize x afterwards, 
and execute the assignment last.  For this we probably need to generate 
code like:

temp = foo (x)
finalize x
x = temp

In the second case, if the result of bar is of a finalizable 
derived-type, we need to finalize it after the value has been used. That 
means:

temp = bar ()
(CALL) foo (temp)
finalize temp

In any case, a reliable method of generating and using "temporaries" is 
needed in the front-end where finalization is handled; I already tried 
it by simply generating variables and this works except for some special 
cases where we need to have temporaries for arrays whose shape/size 
might not even be known at compile time and such.

The trans-code however already deals with such issues as it uses 
temporaries itself; I'd like to make the logic there accessible to the 
front-end to be used to resolve the two finalization cases and maybe 
also for further needs.  I was thinking about how to do this best, and 
here's what I'm thinking of at the moment; I tried to make this as 
"clean" and generally usable as possible, but of course my focus was on 
getting it capable for the two finalization points:

1.) We need a capability to tell the trans code a given expression 
should be converted using a temporary; I think this is currently done 
for parentheses like "(expr)", but maybe we could add a flag or 
something like that to make it even clearer and easier to use.  I 
believe adding parentheses is somewhat a "hack"...

2.) Add something equivalent to "post" and "pre" to gfc_expr's; maybe 
only for expressions marked as needing a temporary as in 1); that should 
suffice if it is easier to do.  These sections would consist of 
gfc_code's and could be translated into the pre and post blocks during 
trans (is this possible easily?).

The post-section could be used for finalizing the function result after 
it has been used while the pre-section (executed after the temporary is 
initialized and before it is actually used for x = temp) could be used 
to finalize the old value of x in the assignment.

3.) In order to reference the temporary variable itself in the 
finalizers, we could for instance introduce a new expression type 
EXPR_TEMPORARY that would point to the gfc_expr it is a temporary for 
and that would in the translation phase simply be converted using the 
correct temporary variable.  We could impose a restriction that such 
expressions might only be used within the mirrored expression's pre and 
post blocks to guard against abuses making things complicated.

What do you think about this ideas?  I admit that I don't have much 
knowledge about certain areas of gfortran and can't fully tell if 
something like that can be implemented and would be worth the effort. 
And of  course, this is only a very rough draft at the moment, outlining 
the points I believe are necessary.

Cheers,
Daniel

-- 
Done:     Arc-Bar-Sam-Val-Wiz, Dwa-Elf-Gno-Hum-Orc, Law-Neu-Cha, Fem-Mal
Underway: Cav-Dwa-Law-Fem
To go:    Cav-Hea-Kni-Mon-Pri-Ran-Rog-Tou



More information about the Fortran mailing list