Mill Computing, Inc. Forums The Mill Tools Compilers Getting an int from a possible pointer


  • Author
  • Anonymous
    Post count: 1
    #3028 |

    I believe in the IPC talk I heard that you can’t access pointers on the stack which sounds great. I also remember in another talk Ivan said pointers are not ints and vice versa. Rarely I will use a pointer and want to double it as an int (for example error checking). Since I know the pointer is aligned to 4+bytes I know the low 2 bits will never be set. So in case of error or int I’ll set the lowest bit, return it then check in the caller code if it’s a int (which would get me 31/63bits) or if it’s pointer.

    Is this possible on the mill? Will the hardware accuse me of evil (which it sort of is) and not allow me to do it?

  • Ivan Godard
    Post count: 689

    I like your handle.

    Pointers are data like any other, and can be in the stack frame. What is not in the Mill stack frame is the state of the calling protocol, in particular the return address and down-stack link. These may be machine addresses (and usually are) but are not program-declared pointers.

    Mill conforms to C rules, which permit conversion between pointer types and intptr_t integral. However, the only thing you can legally do with the result of a conversion is to convert it back again. The facility exists to support data structures that need a hash or total ordering of pointers.

    You can legally modify the low bits either by converting to integer and doing integer arithmetic, or by casting through void* to char[4] and doing pointer arithmetic.

You must be logged in to reply to this topic.