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.1 required=5.0 tests=BAYES_00, PP_MIME_FAKE_ASCII_TEXT autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII X-Google-Thread: 103376,d2cba5965c7bfcd5 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-03-03 19:16:07 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!newsfeed.cwix.com!newscon01.news.prodigy.com!newsmst01.news.prodigy.com!prodigy.com!postmaster.news.prodigy.com!newssvr30.news.prodigy.com.POSTED!not-for-mail From: "Pat Rogers" Newsgroups: comp.lang.ada References: <3C823A1A.6030006@users.sf.net> Subject: Re: 64bit access to an array of 8bit values X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2600.0000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 Message-ID: NNTP-Posting-Host: 208.191.180.40 X-Complaints-To: abuse@prodigy.net X-Trace: newssvr30.news.prodigy.com 1015211725 ST000 208.191.180.40 (Sun, 03 Mar 2002 22:15:25 EST) NNTP-Posting-Date: Sun, 03 Mar 2002 22:15:25 EST Organization: Prodigy Internet http://www.prodigy.com X-UserInfo1: FKPO@SJGTZYQBQXYQ[OD]_HBWB]^PCPDLXUNNHLIWIWTEPIB_NVUAH_[BL[\IRKIANGGJBFNJF_DOLSCENSY^U@FRFUEXR@KFXYDBPWBCDQJA@X_DCBHXR[C@\EOKCJLED_SZ@RMWYXYWE_P@\\GOIW^@SYFFSWHFIXMADO@^[ADPRPETLBJ]RDGENSKQQZN Date: Mon, 04 Mar 2002 03:15:25 GMT Xref: archiver1.google.com comp.lang.ada:20743 Date: 2002-03-04T03:15:25+00:00 List-Id: "Dave Poirier" wrote in message news:3C823A1A.6030006@users.sf.net... > I'm trying to modelize a small virtual machine, and I'm stuck on the > memory model. I'm defining the memory as an array of 8bit values, so > that I am able to access every byte individually. The problem comes > when I try to access them with other data size, like 16/32/64bit. GNAT > simply refuses to do the pointer conversion (yeah, probably a bad habit). > > So, what would be a "clean" way to do it? here's what I tried The predefined way to manipulate values of arbitrary types at arbitrary locations is to use the generic package System.Address_to_Access_Conversions: generic type Object (<>) is limited private; package System.Address_To_Access_Conversions is type Object_Pointer is access all Object; function To_Pointer( Value : Address ) return Object_Pointer; function To_Address( Value : Object_Pointer ) return Address; pragma Convention( Intrinsic, To_Pointer ); pragma Convention( Intrinsic, To_Address ); end System.Address_To_Access_Conversions; For example, if you wanted to swap the two bytes at an arbitrary address (assuming that was the reasonable thing to do), you could do it like this: with Interfaces; with System.Address_To_Access_Conversions; package body Byte_Swapping is type Word is new Interfaces.Unsigned_16; package Word_Ops is new System.Address_To_Access_Conversions( Word ); use Word_Ops; procedure Swap2( Location : in System.Address ) is X : Word renames To_Pointer(Location).all; begin X := ( Shift_Left(X,8) and 16#FF00# ) or ( Shift_Right(X,8) and 16#00FF# ); end Swap2; � end Byte_Swapping; There are other ways, of course, but this illustrates the idea. Note that there is no guarantee that the address you provide to To_Pointer is actually a meaningful address for a value of a given type. You have to be concerned with issues such as padding and alignment, for example. Having described the above, let me also say that you can do unchecked conversions from addresses into access values if you are knowledgeable about how access values are implemented for your given machine/vendor. For example, there is no reason to think that an access value is simply a single address -- it might be an address pair, and it depends upon the type designated. By the way, depending on exactly what you are doing, if you want to model contiguous memory you should consider using the type System.Storage_Elements.Storage_Array -- it is guaranteed contiguous, unlike other array types. That may not be an issue for your model, but if you are doing address calculations it is worth thinking about. Hope that helps, --- Patrick Rogers Consulting and Training in: http://www.classwide.com Real-Time/OO Languages progers@classwide.com Hard Deadline Schedulability Analysis (281)648-3165 Software Fault Tolerance