[cfe-dev] Need Help to Understand LLVM Source Code

Nikola Smiljanic via cfe-dev cfe-dev at lists.llvm.org
Wed Sep 2 00:08:04 PDT 2015


OK, now I see what confuses you. PointerUnion relies on the fact that
memory coming from the allocator is aligned. So you allocate a byte and get
an address X, then you allocate another byte and get the address Y. There's
no guarantee you'll get consecutive addresses but lets ignore that for
simplicity. The relative difference between the two addresses isn't one,
it's whatever the size of the word is. That means that lower bytes are
always empty and that's where PointerUnion stores the flag that tells it
what type is stored inside. The pointers that are stored inside all take
the same space, the size of a pointer, it's just that you have to clear the
lower bits to get to the actual value.

On Wed, Sep 2, 2015 at 10:50 AM, Feng Lu <lufeng1204 at gmail.com> wrote:

> Hi, Nikola,
>  Thanks for your help.
>   Is it assumed that pointer types are all have the same number of bits in
> pointer parts?
>   e.g. 30 bits in pointer part and 2 bits in flag part.
>   Otherwise I cannot understand why point types does not matter.
>   For example, if PT1 has 30 bit in pointer part, and PT2 has 31 bit in
> pointer part. It would be different for them to be NULL.
>
> Thanks
> Feng Lu
>
> On Tue, Sep 1, 2015 at 2:20 PM, Nikola Smiljanic <popizdeh at gmail.com>
> wrote:
>
>> You can't get a different result because it's not really important what
>> type you're getting. The type isn't exposed in the interface of this
>> method. It's just checking that pointer is null. The type doesn't matter
>> which is why the comment also says "one of the pointer types".
>>
>> On Wed, Sep 2, 2015 at 1:57 AM, Feng Lu via cfe-dev <
>> cfe-dev at lists.llvm.org> wrote:
>>
>>> Hi, All,
>>>  I am very interested in LLVM, and starting to learn from LLVM source
>>> code.
>>>  I encountered one problem that I cannot understand.
>>>
>>>  The part of the code is from  llvm/include/llvm/ADT/PointerUnion.h
>>>
>>>  class PointerUnion {PointerUnion.h
>>>   public:
>>>     typedef PointerIntPair<void*, 1, bool,
>>>                            PointerUnionUIntTraits<PT1,PT2> > ValTy;
>>>   private:
>>>     ValTy Val;
>>>
>>>     struct IsPT1 {
>>>       static const int Num = 0;
>>>     };
>>>     struct IsPT2 {
>>>       static const int Num = 1;
>>>     };
>>>     template <typename T>
>>>     struct UNION_DOESNT_CONTAIN_TYPE { };
>>>
>>>   public:
>>>     PointerUnion() {}
>>>
>>>     PointerUnion(PT1 V) : Val(
>>>       const_cast<void
>>> *>(PointerLikeTypeTraits<PT1>::getAsVoidPointer(V))) {
>>>     }
>>>     PointerUnion(PT2 V) : Val(
>>>       const_cast<void
>>> *>(PointerLikeTypeTraits<PT2>::getAsVoidPointer(V)), 1) {
>>>     }
>>>
>>>     /// isNull - Return true if the pointer held in the union is null,
>>>     /// regardless of which type it is.
>>>     bool isNull() const {
>>>       // Convert from the void* to one of the pointer types, to make
>>> sure that
>>>       // we recursively strip off low bits if we have a nested
>>> PointerUnion.
>>>       return
>>> !PointerLikeTypeTraits<PT1>::getFromVoidPointer(Val.getPointer());
>>>     }
>>>
>>> What confusing me is the  isNull() function.
>>> Would it be possible to get different result if I use
>>>   return !PointerLikeTypeTraits<PT2
>>> >::getFromVoidPointer(Val.getPointer());
>>> instead of
>>>   return !PointerLikeTypeTraits<PT1
>>> >::getFromVoidPointer(Val.getPointer());
>>>
>>> Thanks for your help.
>>> Feng Lu
>>>
>>>
>>> _______________________________________________
>>> cfe-dev mailing list
>>> cfe-dev at lists.llvm.org
>>> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>>>
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150902/50af9023/attachment.html>


More information about the cfe-dev mailing list