Can a C compiler generate an executable 64-bits where pointers are 32-bits?

后端 未结 10 1124
陌清茗
陌清茗 2021-02-12 16:51

Most programs fits well on <4GB address space but needs to use new features just available on x64 architecture.

Are there compilers/platforms where I can use x64 regi

相关标签:
10条回答
  • 2021-02-12 17:28

    It depends on the platform. On Mac OS X, the first 4 GB of a 64-bit process' address space is reserved and unmapped, presumably as a safety feature so no 32-bit value is ever mistaken for a pointer. If you try, there may be a way to defeat this. I worked around it once by writing a C++ "pointer" class which adds 0x100000000 to the stored value. (This was significantly faster than indexing into an array, which also requires finding the array-base address and multiplying before the addition.)

    On the ISA level, you can certainly choose to load and zero-extend a 32-bit value and then use it as a 64-bit pointer. It's a good feature for a platform to have.

    No change should be necessary to a program unless you wish to use 64-bit and 32-bit pointers simultaneously. In that case you are back to the bad old days of having near and far pointers.

    Also, you will certainly break ABI compatibility with APIs that take pointers to pointers.

    0 讨论(0)
  • 2021-02-12 17:29

    Technically, it is possible for a compiler to do so. AFAIK, in practice it isn't done. It has been proposed for gcc (even with a patch here: http://gcc.gnu.org/ml/gcc/2007-10/msg00156.html) but never integrated (at least, it was not documented the last time I checked). My understanding is that it needs also support from the kernel and standard library to work (i.e. the kernel would need to set up things in a way not currently possible and using the existing 32 or 64 bit ABI to communicate with the kernel would not be possible).

    0 讨论(0)
  • 2021-02-12 17:30

    What exactly are the "64-bit features" you need, isn't that a little vague?

    Found this while searching myself for an answer: http://www.codeproject.com/KB/cpp/smallptr.aspx

    Also pick up the discussion at the bottom...

    Never had any need to think about this, but it is interesting to realize that one can be concerned with how much space pointers need...

    0 讨论(0)
  • 2021-02-12 17:31

    The second part of your question is easily answered. It is very possible, in fact many C implementations have support, for 64-bit operations using 32-bit code. The C type often used for this is long long (but check with your compiler and architecture).

    As far as I know it is not possible to have 32-bit pointers in 64-bit native code.

    0 讨论(0)
提交回复
热议问题