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-Thread: 103376,c6acbb9f2027b8c9 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local01.nntp.dca.giganews.com!nntp.scarlet.biz!news.scarlet.biz.POSTED!not-for-mail NNTP-Posting-Date: Wed, 05 Oct 2005 13:21:10 -0500 From: Ludovic Brenta Newsgroups: comp.lang.ada Subject: Re: volatile vs aliased References: <1128525722.605730.281980@g43g2000cwa.googlegroups.com> Date: Wed, 05 Oct 2005 20:22:29 +0200 Message-ID: <87mzlnomca.fsf@ludovic-brenta.org> User-Agent: Gnus/5.110004 (No Gnus v0.4) Emacs/21.4 (gnu/linux) Cancel-Lock: sha1:RBC2KcrDLDBo4urR6g8FMjEygyg= MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii NNTP-Posting-Host: 83.134.241.83 X-Trace: sv3-K1nejqrLa7V9Pwoo8lAZg3uNEtyD1NNzIzCSLDIN8foZzMKQyaL0eIZ8OUgfIRQd+qJBX0U4vKebMd0!YrvSjUmWWzONoN0zIev3B6I3s0JypIr7K4MQVeYWoTFa0SBliQewo+64hbdghdtUqyMd/tsgvg== X-Complaints-To: abuse@scarlet.be X-DMCA-Complaints-To: abuse@scarlet.biz X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.32 Xref: g2news1.google.com comp.lang.ada:5415 Date: 2005-10-05T20:22:29+02:00 List-Id: "REH" writes: > I'm converting some code written for Gnat to compile with Apex. The > software has bindings to C functions (OS calls). Some of these take > System.Address as parameters. Apex complains about the variables used > in these calls (via 'Address) because they not volatile or aliased (or > imported, exported, etc.). My question is: which should I use? Is > there any difference whether a variable is volatile or aliased? > > I am assuming the compiler is concerned about the variable possibly > being changed in the call (which, of course, is true). Is there a way > to satisfy the compiler without stopping optimizations of the variable > after the call, or do I need to use a second variable to do this? I like this warning given by Apex; I think it would be nice if GNAT would also warn in this situation. pragma Volatile (Variable) says the compiler must not optimise away any reads or writes to that variable, and that it may not add extra reads or writes beyond those you explicitly request in your program text. You want to use that for hardware registers, where a "read" operation may have a side effect such as changing the device's state. Whenever you access a variable through an address value, the variable is aliased, so you should say that explicitly. Otherwise, the compiler may be tempted to place the variable in a processor register, which has no address. In addition, pragma Import (Ada, Variable) says that the compiler may not insert default initialisations for that variable, or assume anything about the value of the variable, before you write to it. But the compiler may do optimisations, because it is allowed to assume that no side effects occur when you read from or write to the variable (that is, side effects not explicitly requested from the program text). In your situation, it seems that you want something like this (which, incidentally, I use very often): type T is ...; procedure Read_Variable (At_Address : in System.Address; Into : out T) is V : aliased T; -- suppress default initialisation, I know what I'm doing. pragma Import (Ada, V); for V'Address use At_Address; begin -- perform my own explicit validation of the variable, perhaps -- using 'Valid; then, copy into Into: Into := V; end Read_Variable; -- Ludovic Brenta.