www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How do I check if a function got CTFE?

reply "AsmMan" <jckj33 gmail.com> writes:
I'd like to check if a function got CTFE, ie, the compiler was 
able to replace my foo(s); by the computed value at compile-time.

I'm trying to convert the binary executable to assembly by using 
objconv tool but I'm finding it very diffucult to find anything 
in there, since some converters I've used which does ELF to ASM 
keep the function name, e.g, foo() function is a foo label 
somewhere in the file but this convert doesn't and use some 
numbers instead of. I don't know if it's related how is the 
windows object file format designed.
Oct 02 2014
next sibling parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Thursday, 2 October 2014 at 17:56:29 UTC, AsmMan wrote:
 I'd like to check if a function got CTFE, ie, the compiler was 
 able to replace my foo(s); by the computed value at 
 compile-time.
You have to explicitly force ctfe with context, it is never done automatically, and if it fails, the build will fail and you get a compile time error. So if you write enum f = foo(); or static f = foo(); or similar initializations and the build succeeds, you know it got ctfe'd. Otherwise, it wasn't.
Oct 02 2014
parent "AsmMan" <jckj33 gmail.com> writes:
On Thursday, 2 October 2014 at 18:02:30 UTC, Adam D. Ruppe wrote:
 On Thursday, 2 October 2014 at 17:56:29 UTC, AsmMan wrote:
 I'd like to check if a function got CTFE, ie, the compiler was 
 able to replace my foo(s); by the computed value at 
 compile-time.
You have to explicitly force ctfe with context, it is never done automatically, and if it fails, the build will fail and you get a compile time error.
That's the point. I thought the compiler did it by checking things like constant arguments + function purity or so. This was exactly my issue. Thanks!
Oct 02 2014
prev sibling next sibling parent reply "anonymous" <anonymous example.com> writes:
On Thursday, 2 October 2014 at 17:56:29 UTC, AsmMan wrote:
 I'd like to check if a function got CTFE, ie, the compiler was 
 able to replace my foo(s); by the computed value at 
 compile-time.

 I'm trying to convert the binary executable to assembly by 
 using objconv tool but I'm finding it very diffucult to find 
 anything in there, since some converters I've used which does 
 ELF to ASM keep the function name, e.g, foo() function is a foo 
 label somewhere in the file but this convert doesn't and use 
 some numbers instead of. I don't know if it's related how is 
 the windows object file format designed.
Sorry, can't help with you objconv, COFF, ELF, etc. A little something about terminology though: "CTFE" in the narrower sense refers to those occurrences of compile time evaluation that are specified to happen. Static initializers are CTFE-ed, for example. CTFE is not an optimization that a compiler may or may not do. The term for the optimization would be "constant folding", I think. Your question, as I understand it, is about constant folding, not about CTFE.
Oct 02 2014
parent reply "AsmMan" <jckj33 gmail.com> writes:
On Thursday, 2 October 2014 at 18:17:12 UTC, anonymous wrote:
 On Thursday, 2 October 2014 at 17:56:29 UTC, AsmMan wrote:
 I'd like to check if a function got CTFE, ie, the compiler was 
 able to replace my foo(s); by the computed value at 
 compile-time.

 I'm trying to convert the binary executable to assembly by 
 using objconv tool but I'm finding it very diffucult to find 
 anything in there, since some converters I've used which does 
 ELF to ASM keep the function name, e.g, foo() function is a 
 foo label somewhere in the file but this convert doesn't and 
 use some numbers
I was thiking the dmd compiler did CTFE without someone ask for this, in the way as I've mentioned, checking for constant arguments + function's purity and if all this is true, it did the CTFE rather than generate code to compute it at run-time. In the case of it did happen, I just wanted to know. It was my misunderstsooding how it does works in dmd.
Oct 02 2014
next sibling parent reply "anonymous" <anonymous example.com> writes:
On Thursday, 2 October 2014 at 18:42:56 UTC, AsmMan wrote:
 I was thiking the dmd compiler did CTFE without someone ask for 
 this, in the way as I've mentioned, checking for constant 
 arguments + function's purity and if all this is true, it did 
 the CTFE rather than generate code to compute it at run-time. 
 In the case of it did happen, I just wanted to know. It was my 
 misunderstsooding how it does works in dmd.
Yeah, that would be constant folding. The compiler is free to do that. It's just not called "CTFE" then.
Oct 02 2014
parent "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
On Thu, Oct 02, 2014 at 07:04:56PM +0000, anonymous via Digitalmars-d-learn
wrote:
 On Thursday, 2 October 2014 at 18:42:56 UTC, AsmMan wrote:
I was thiking the dmd compiler did CTFE without someone ask for this,
in the way as I've mentioned, checking for constant arguments +
function's purity and if all this is true, it did the CTFE rather
than generate code to compute it at run-time. In the case of it did
happen, I just wanted to know. It was my misunderstsooding how it
does works in dmd.
Yeah, that would be constant folding. The compiler is free to do that. It's just not called "CTFE" then.
CTFE grew out of constant-folding in dmd. In the dmd code, it is essentially still just a constant-folder, albeit a superpowered one. :-P T -- It is of the new things that men tire --- of fashions and proposals and improvements and change. It is the old things that startle and intoxicate. It is the old things that are young. -- G.K. Chesterton
Oct 02 2014
prev sibling parent "monarch_dodra" <monarchdodra gmail.com> writes:
On Thursday, 2 October 2014 at 18:42:56 UTC, AsmMan wrote:
 I was thiking the dmd compiler did CTFE without someone ask for 
 this, in the way as I've mentioned, checking for constant 
 arguments + function's purity and if all this is true, it did 
 the CTFE rather than generate code to compute it at run-time. 
 In the case of it did happen, I just wanted to know. It was my 
 misunderstsooding how it does works in dmd.
A convenient way to force ctfe is "eval": http://dlang.org/function.html (search for "eval!") Though you'd change "const" for "enum". Unfortunately, it's not in Phobos, but it should be! https://issues.dlang.org/show_bug.cgi?id=11811
Oct 02 2014
prev sibling parent "Gary Willoughby" <dev nomad.so> writes:
On Thursday, 2 October 2014 at 17:56:29 UTC, AsmMan wrote:
 I'd like to check if a function got CTFE, ie, the compiler was 
 able to replace my foo(s); by the computed value at 
 compile-time.

 I'm trying to convert the binary executable to assembly by 
 using objconv tool but I'm finding it very diffucult to find 
 anything in there, since some converters I've used which does 
 ELF to ASM keep the function name, e.g, foo() function is a foo 
 label somewhere in the file but this convert doesn't and use 
 some numbers instead of. I don't know if it's related how is 
 the windows object file format designed.
You could use __ctfe http://forum.dlang.org/thread/yzioyjhiqedktswkweop forum.dlang.org
Oct 02 2014