Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to prohibit the use of certain registers for a small snippet of C++ code?

Tags:

c++

g++

I have looked at this question already, but neither of the two solutions work for me, for the following reasons.

  1. I am trying to prevent c++ code from touching registers, not assembly, so the clobber list will not work.
  2. I would like to do this locally, not globally, so global explicit register variables are too heavy-handed.

Is it possible to wrap a set of c++ statements in some way to tell the compiler to not use certain registers?

like image 637
merlin2011 Avatar asked Sep 26 '16 06:09

merlin2011


1 Answers

Not in a portable way of course. C++ semantic level knows nothing about this register thing (despite having a register keyword).

g++ for example however can allocate a register globally or locally to a variable and in this case the compiler will never touch that register. This can sometimes be useful (I've used this with serious performance gains in a VM for a Lisp implementation without having to write everything in assembly by hand).

I suspect of course that unless you recompile also all the standard library changing the standard headers to include the declaration that code in the library can touch the register (and depending on the ABI it's possible that the registers you want to use are declared "scratch" and therefore not saved and restored).

May be other compilers have this option too (clang however for example despite being almost g++-compatible does not support register allocation).

like image 106
6502 Avatar answered Nov 15 '22 06:11

6502