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!postnews.google.com!g14g2000cwa.googlegroups.com!not-for-mail From: "REH" Newsgroups: comp.lang.ada Subject: Re: volatile vs aliased Date: 5 Oct 2005 11:39:26 -0700 Organization: http://groups.google.com Message-ID: <1128537566.929419.121660@g14g2000cwa.googlegroups.com> References: <1128525722.605730.281980@g43g2000cwa.googlegroups.com> <87mzlnomca.fsf@ludovic-brenta.org> NNTP-Posting-Host: 192.35.35.35 Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" X-Trace: posting.google.com 1128537572 7342 127.0.0.1 (5 Oct 2005 18:39:32 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Wed, 5 Oct 2005 18:39:32 +0000 (UTC) In-Reply-To: <87mzlnomca.fsf@ludovic-brenta.org> User-Agent: G2/0.2 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915,gzip(gfe),gzip(gfe) Complaints-To: groups-abuse@google.com Injection-Info: g14g2000cwa.googlegroups.com; posting-host=192.35.35.35; posting-account=lnUIyw0AAACoRB2fMF2SFTIilm8F10q2 Xref: g2news1.google.com comp.lang.ada:5420 Date: 2005-10-05T11:39:26-07:00 List-Id: Ludovic Brenta wrote: > 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. VERY helpful! Thank you so much. I'm dealing with a situation where I have to do symbol table lookups from the OS which return an address to procedure or function. This is done in several places in the code. The reason is I have to support different hardware dynamically. For example, if I am on board A, I call Foo. The comparible subprogram on B is Bar. So, I cannot just make pragma exports for them both as one will be undefined. Anyways, to make it easier, I wanted to make a generic which just returned an access type without having to do the pragma Import and for X'address in every case. Then calling the subprogram is simple. My first pass is: generic type Data_Type is private; type Data_Ptr is access all Data_Type; function Address_To_Access(Addr : System.Address) return Data_Ptr; function Address_To_Access(Addr : System.Address) return Data_Ptr is begin if Addr = System.Null_Address then return null; else declare Data: aliased Data_Type; for Data'Address use Addr; begin return Data'Unchecked_Access; end; end if; end Address_To_Access; Two questions: 1) Is this even a smart thing to do? 2) How would I define a generic like the above for an subprogram with an unknown signature? Thanks again for the help. REH