From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,571930b4ff0bc1ee X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-03-27 08:42:12 PST Newsgroups: comp.lang.ada Path: supernews.google.com!sn-xit-03!supernews.com!news-out.usenetserver.com!news-out-sjo.usenetserver.com!europa.netcrusader.net!208.184.7.66!newsfeed.skycache.com!Cidera!news.mv.net!world!bobduff From: Robert A Duff Subject: Re: Compile time executed functions Sender: bobduff@world.std.com (Robert A Duff) Message-ID: Date: Tue, 27 Mar 2001 16:33:40 GMT References: <3AC03CCE.70E3C2D5@mida.se> <132w6.3493$fy.5811@www.newsranger.com> Organization: The World Public Access UNIX, Brookline, MA X-Newsreader: Gnus v5.3/Emacs 19.34 Xref: supernews.google.com comp.lang.ada:6123 Date: 2001-03-27T16:33:40+00:00 List-Id: Ted Dennison writes: > In article , Robert A Duff says... > > > >Mats Karlssohn writes: > > > >> Sometimes, especially when doing embedded programming, I feel a great > >> need to execute a function to get literal initialization a constant. > >> That is I'd like to have the compiler to execute a function for me (at > >> compiletime!) and use the return of that function to initialize the > >> constant. > > > >If you inline the function, you are likely to get what you want, > >depending on how good the compiler's support for inlining is. > > He also wanted the result of the function to be put into the ROM area when the > executable is created by the compiler. Inlining wouldn't do that, > would it? Why not? If the value of the constant is known to the compiler, then it can go in ROM (presuming the compiler and linker and whatnot support ROM at all). >... It > just prevents the little extra subprogram call overhead (saving and restoring > the register context, etc) when the function is called at runtime. Inlining does that, but it also allows the compiler to optimize through the inlined code. Eg: function Factorial(X: Natural) return Positive is pragma Inline(Factorial); begin if X = 0 then return 1; else return Factorial(X-1) * X; end if; end Factorial; Y: constant Integer := Factorial(7); I have seen compilers that will generate the same code for the above that is generated for: Y: constant Integer := 5040; My point is that inlining is not just for removing the call overhead. It is also for letting the compiler reason about the code as if it were written in line at the call site. - Bob