6

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?

Community
  • 1
  • 1
merlin2011
  • 71,677
  • 44
  • 195
  • 329

2 Answers2

1

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).

6502
  • 112,025
  • 15
  • 165
  • 265
  • @merlin2011: "Too heavyhanded" is a meaningless remark, if it's the best possible option. – MSalters Sep 26 '16 at 06:54
  • @Msalters, The set of prohibited registers is sufficiently large that it would be detrimental to the performance of the rest of the program. – merlin2011 Sep 26 '16 at 07:10
0

No, C++ has a pretty simplistic model underneath.

If you think about it how would C++ compiler know about the registers it is to be compiled on?

Without delving into assembler you won't be able to do this.

Because you want to prevent certain registers from being used you made need to write the code from the point you want to prohibit those registers to the point where that requirement is no longer needed - which could be a lot of code.

Without more detail about why you want to prevent access to certain registers and how long the prevention is for, then all I can give is hazy generalistics.

graham.reeds
  • 16,230
  • 17
  • 74
  • 137
  • Considering the question is tagged "g++", it knows about the registers because every supported CPU model has an abstract description. But every compiler knows about the registers of the target machine, by definition. – MSalters Sep 26 '16 at 06:52
  • G++ may know, but C++ doesn't. – graham.reeds Sep 26 '16 at 07:26
  • "C++" describes the language, it doesn't even assume a compiler (interpreter is also possibilty). But as soon as you say "C++ compiler", it must know about its target environment. – MSalters Sep 26 '16 at 08:49
  • Agreed, my wording was pretty lax. – graham.reeds Sep 26 '16 at 10:52