[fpc-pascal] why fpc do not use a known return function value
leledumbo
leledumbo_cool at yahoo.co.id
Tue Feb 3 15:58:33 CET 2015
> int c ;
> int test( int p){
> int i;
> i = p;
> return (i+2+c+2+c+2+c);
> }
> int main(){
> c = test(128);
> }
Hmm....yes, from C perspective, there's nothing can modify c before it's
used the first time (unless you inject the startup code). Therefore, the
compiler is safe to assume that c is its initial global value (0) and is
able to use constant propagation, with c assumed as constant to reduce the
function call into a simple value.
The case with Pascal is not the same, however. Pascal has unit system with
initialization section that can inject code before the main block is
executed. A unit can extern-ize the variable and modify it in its
initialization section. So it's not safe to assume the same thing as C does
above. It's not impossible, but would be harder to implement, probably
requiring whole program analysis.
--
View this message in context: http://free-pascal-general.1045716.n5.nabble.com/why-fpc-do-not-use-a-known-return-function-value-tp5720901p5720918.html
Sent from the Free Pascal - General mailing list archive at Nabble.com.
More information about the fpc-pascal
mailing list