[m-dev.] Why do strings in Mercury need to be word aligned?

Paul Bone pbone at csse.unimelb.edu.au
Wed May 6 18:57:51 AEST 2009


On Wed, May 06, 2009 at 06:31:26PM +1000, Peter Ross wrote:
> Hi,
> 
> Why does a MR_String need to be word-aligned?  What code will ever
> look at the tag bits of a string and hence do the wrong thing?  I'm
> sure that there is code, but I just can't think of what it is.

I think that if you have:

:- type maybe(T)
    --->    yes(T)
    ;       no.

And then use the value yes("fish") the word-aligned string is packed with the
tag bits for maybe so that it uses only two memory cells one for the actual
string and one for maybe.

  +-----+---------+        +--------+
  | tag | pointer |------> | String |
  +-----+---------+        +--------+

If we couldn't use the lower two bits of the string pointer this structure
would have to be:

  +-----+---------------------------+     +----------------+     +--------+
  | tag | pointer to string pointer |---> | String Pointer |---> | String |
  +-----+---------------------------+     +----------------+     +--------+

Other examples with other structures will also do similar things.  It also
doesn't require use of the ploymorphic type as above.

Hope this helps.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 197 bytes
Desc: Digital signature
URL: <http://lists.mercurylang.org/archives/developers/attachments/20090506/061160a1/attachment.sig>


More information about the developers mailing list