2

My question is very similar to: GCC: Prohibit use of some registers

However, I would like to limit the number of registers used in a C function. In other words, I would like to be able to use a directive just like clobber registers but applied at the C function level rather than within inline assembly. Also, each function may have a different set of registers to use, so it can't be a one-fits-all solution.

The only 2 possible solutions that I have thought of were:

  • Do everything in assembler. Not really a solution to my problem...
  • Compile each function separately using some kind of directive for conditional usage of registers in GCC. I would lose a lot of potential optimization by compiling each function separately and it would make the code virtually unreadable.

Since I am trying to do all this in C, is there any way that the pre-compiler could help here in any way? Are there any possible ways for achieving my intended goal?

Community
  • 1
  • 1
Oscar Wahltinez
  • 1,155
  • 3
  • 12
  • 24
  • 6
    Can I ask why you want to do this? – Wug Jun 20 '12 at 19:17
  • 2
    If you're trying to prohibit specific registers then you're no longer really at the level of C. – Ignacio Vazquez-Abrams Jun 20 '12 at 19:17
  • @Wug I am playing around with schedulers and task-switching and I was wondering if such a thing was possible, so it is not for practical applications at least for now – Oscar Wahltinez Jun 20 '12 at 19:19
  • 1
    You are likely to be much better off not fretting about it and letting the compiler use the registers as it sees best. If you are worried about it even so, then you are dealing with assembly language programming, not C programming really. – Jonathan Leffler Jun 20 '12 at 19:20
  • @IgnacioVazquez-Abrams I understand that, but C has some handy keywords that let you "step down" to a lower level like `Register` so I was wondering if there was something along those lines that could be applied here – Oscar Wahltinez Jun 20 '12 at 19:21
  • 2
    Even the register keyword is only just a suggestion. The compiler will try to keep variables in a register anyway, the register keyword instructs it to prefer one specific variable over others given the opportunity. – Wug Jun 20 '12 at 19:24
  • 1
    'Standard C' has no mechanisms to do what you want. Some specific compilers for the specific hardware you're working on may have extensions to help, but they are inherently platform (CPU type) and compiler specific. Since you've not specified platform and compiler, no-one can possibly advise you accurately yet. – Jonathan Leffler Jun 20 '12 at 19:24
  • 1
    The 'register' keyword does not allow you to "step down" to a lower level - at best it is intended to give the compiler a hint about the expected frequency of usage of a variable and "recommend" that it be allocated to a register. It does not guarantee that will be done, though. – twalberg Jun 20 '12 at 19:26
  • 2
    ["Never write `register`. It's exactly as meaningful as whitespace."](http://www.drdobbs.com/184403859) – Fred Larson Jun 20 '12 at 19:26

1 Answers1

4

No, there is not. You will need to use either inline assembly statements in your functions or assembly language.

Wug
  • 12,956
  • 4
  • 34
  • 54
  • I could at least use a per-function compilation or call each function within inline assembler using clobber registers, so I really can't set "No, there is not" as my accepted answer – Oscar Wahltinez Jun 20 '12 at 19:44
  • 1
    Register organization is pretty much exactly the sort of thing that using languages higher level than assembly language is supposed to abstract out of the process of writing programs. Your best bet would probably be to write the critical routines in assembly language, and call them from C. – Wug Jun 20 '12 at 19:48
  • Alternatively, you can use the -S compiler flag (works with gcc and probably others) to reduce your c source files to assembly language, and fiddle with the output before compiling them fully. – Wug Jun 20 '12 at 19:49