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

后端 未结 10 1122
陌清茗
陌清茗 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:10

    A simple way to circumvent this is if you'd have only few types for your structures that you are pointing to. Then you could just allocate big arrays for your data and do the indexing with uint32_t.

    So a "pointer" in such a model would be just an index in a global array. Usually addressing with that should be efficient enough with a decent compiler, and it would save you some space. You'd loose other things that you might be interested in, dynamic allocation for instance.

    Another way to achieve something similar is to encode a pointer with the difference to its actual location. If you can ensure that that difference always fits into 32 bit, you could gain too.

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

    On x86, no. On other processors, such as PowerPC it is quite common - 64 bit registers and instructions are available in 32 bit mode, whereas with x86 it tends to be "all or nothing".

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

    I'm afraid that if you are concerned about the size of pointers you might have bigger problems to deal with. If the number of pointers is going to be in the millions or billions, you will probably run into limitations within the Windows OS before you actually run out of physical or virtual memory.

    Mark Russinovich has written a great article relating to this, named Pushing the Limits of Windows: Virtual Memory.

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

    I think this would be similar to the MIPS n32 ABI: 64-bit registers with 32-bit pointers.

    In the n32 ABI, all registers are 64-bit (so requires a MIPS64 processor). But addresses and pointers are only 32-bit (when stored in memory), decreasing the memory footprint. When loading a 32-bit value (such as a pointer) into a register, it is sign-extended into 64-bits. When the processor uses the pointer/address for a load or store, all 64-bits are used (the processor is not aware of the n32-ess of the SW). If your OS supports n32 programs (maybe the OS also follows the n32 model or it may be a proper 64-bit OS with added n32 support), it can locate all memory used by the n32 application in suitable memory (e.g. the lower 2GB and the higher 2GB, virtual addresses). The only glitch with this model is that when registers are saved on the stack (function calls etc), all 64-bits are used, there is no 32-bit data model in the n32 ABI.

    Probably such an ABI could be implemented for x86-64 as well.

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

    Linux now has fairly comprehensive support for the X32 ABI which does exactly what the asker is asking, in fact it is partially supported as a configuration under the Gentoo operating system. I think this question needs to be reviewed in light of resent development.

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

    It's worth noting that there an ABI in development for linux, X32, that lets you build a x86_64 binary that uses 32 bit indices and addresses.

    Only relatively new, but interesting nonetheless.

    http://en.wikipedia.org/wiki/X32_ABI

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