[Vm-dev] Direct pointer vs pointerForOop()

David T. Lewis lewis at mail.msen.com
Sat Apr 7 21:04:14 UTC 2018


On Sat, Apr 07, 2018 at 11:28:38PM +0530, K K Subbu wrote:
> 
> On Saturday 07 April 2018 08:02 PM, David T. Lewis wrote:
> >I have long thought that it would be a good idea for purposes of clarity
> >to have a separate "sqObjectPointer" data type distinct from sqInt and
> >usqInt. This would make the intentions clear, and I think it would make
> >the VM code easier to read and understand. But it would probably be a
> >lot of work to do this throughout the VM.
> 
> While I realize OOP is strongly entrenched in literature, terms like 
> pointer, address have strong connotations of machine architecture. If 
> you are going to introduce a new type, I would suggest neutral terms 
> like sqObjectId, sqObjectIndex or sqObjectHandle.
> 
> How about using a union to pass them across Object and Memory layers and 
> avoid all those horrid casts?
> 
> typedef union {
> 	u32           asId;
> 	void         *asAddress;
> 	unsigned int  asWord;
> 	unsigned long asLongWord;
> 	unsigned char asBytes[sizeof(void *)];
> 	...
> } sqObjectId;
> 
> Object layer can access it as oop.asId and the Memory layer can use 
> asWord or asAddress after suitable conversion:
> 
> sqObjectId pointerForOop(sqObjectId oop)
> {return (oop.asAddress = oop.asId + sqMemoryBase, oop);}
> 
> sqObjectId oopFromPointer(sqObjectId oop)
> {return (oop.asId = sqMemoryBase - oop.asAddr, oop); }
> 

Hi Subbu,

I was unclear in my meaning when I said "data type" for object pointers.
I really only meant something like "#typedef usqInt sqObjectPointer" such
that "sqObjectPointer" could be used when the intent is an object pointer,
and "usqInt" could be used when the intent is an unsigned integer. Nothing
would actually change, but it might make the VM code a bit easier to read
and understand.

With respect to defining data types, I think that Nicolas has already
handled the most important case with the introduction of sqIntptr and
usqIntptr. This clarifies the usage a lot, and cleans up a lot of problems
across different compilers and platforms.

You are right that "object pointer" is a confusing term for people who may
be thinking in terms of C pointers. We also have a fair amount of confusion
in the use of term "word". But in a very real sense, an object pointer
really is a pointer (not just an identifier), even if that does mean
something different from the term "pointer" in C. I don't know, maybe
"object reference" instead of "object pointer"?

Dave



More information about the Vm-dev mailing list