Is this possible to tell the compiler to put a certain global variable in a register? Thus effectively blocking this register from use everywhere else. I understand this might be difficult because even a simple call to malloc() will require spilling it temporarily.
I am thinking about it because I am looking for a way to efficiently implement a secondary stack for sort of VM and naturally it would be great to store the secondary stack pointer in another register.
In theory, this is possible: You could take any register that the calling conventions require to be preserved across function calls, and use that for your global variable.
However, there are some problems with this:
The effect is, that your functions will have one less register for local variables available. This means, more memory accesses on average.
Library functions will not preserve the registers value, they will save it on the stack as any other non-clobber register, use it as they please, and restore it before they return.
As such, it is impossible to pass a callback pointer to a library function, and access the register global from the callback. (But this is probably not your problem anyway.)
If you are implementing a VM stack as a global variable, you are doing something very wrong in the first place. A stack should be thread local by nature, it has no business being global.
Doing things right, keeping the stack pointer as a local variable within the VM emulator is likely to give you the best performance you can get.
Is this possible to tell the compiler to put a certain global variable in a register?
Not really. There is the register storage class, but this only means that the variable should be "as fast as possible". This keyword is mostly obsolete nowadays, it is from a time when compilers were trash.
Thus effectively blocking this register from use everywhere else
It's not possible to select a specific register. You will have to use inline assembler for that.
I am thinking about it because I am looking for a way to efficiently implement a secondary stack for sort of VM and naturally it would be great to store the secondary stack pointer in another register.
Sounds like you need to be writing assembler overall. It is not even possible to set the stack pointer from C. Or if by "stack" you don't mean the program memory, but rather some data type, don't go fiddle with pre-mature optimizations in the first place.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With