Path: utzoo!mnetor!uunet!seismo!sundc!pitstop!sun!decwrl!decvax!mcnc!gatech!udel!princeton!phoenix!rjchen From: rjchen@phoenix.Princeton.EDU (Raymond Juimong Chen) Newsgroups: comp.lang.c Subject: Another 'D' proposal (sigh) Message-ID: <1893@phoenix.Princeton.EDU> Date: 28 Feb 88 20:05:15 GMT Organization: Princeton University, NJ Lines: 67 I don't have a copy of dpANS so I don't know if this is addressed in it. I also don't know if this has been discussed and rejected by the Committee, so here goes... Propose that there be a way to declare "true functions", ie, functions whose return values depend on and are solely determined by the arguments passed to it. They also have no side-effects. Simple examples: min() max() abs() pow() time_diff() etc. This would be a hint to the compiler that such function calls have no side-effects and therefore can 1. be optimized away in expressions such as if (0 * min(a,b)) ... 2. be calculated only once if the arguments are identical in different statements: foo = (time_diff(start, end) + bar) * snerg; /* ... statements which do not alter start or end */ oog = time_diff(start, end); 3. be calculated at any time provided the arguments are known. This also allows you to use such "true functions" in DEBUG macros without having the compiler generate idle code. for example, DEBUG(("x^2 is now %f\n", pow(x,2))); (Normally, the compiler would have to generate a call to pow(), since it doesn't know that pow() has no side-effects--even when DEBUG if #define'd to a null string.) Implementation: I can think of two ways this could be implemented. Let foo() be a "true function". register int foo(...) const int foo(...) The first method is consistent with the notion of "register" as a "hint to the compiler". The second is consistent with the notion of "const" as "unchanging". (Observe that the use of "const" does not conflict with soon-to-be-existing usage. const char * foo(...) and char * const foo(...) would mean different things. One says that foo() returns a pointer to a character in ROM, the other says that foo() returns a pointer to a character which depends purely on the arguments. I don't know which is which, however.) The compiler can even check whether you're lying or not: If inside your function you refer to anything other than an argument or a local variable, or if you call a "non-true" function, then the "trueness" of your function is suspect and can be flagged by a suitably picky compiler (or lint). Comments welcome. -- Raymond Chen UUCP: ...allegra!princeton!{phoenix|pucc}!rjchen BITNET: rjchen@phoenix.UUCP, rjchen@pucc ARPA: rjchen@phoenix.PRINCETON.EDU "Say something, please! ('Yes' would be best.)" - The Doctor -- Raymond Chen UUCP: ...allegra!princeton!{phoenix|pucc}!rjchen BITNET: rjchen@phoenix.UUCP, rjchen@pucc ARPA: rjchen@phoenix.PRINCETON.EDU "Say something, please! ('Yes' would be best.)" - The Doctor