Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inform GCC to not use a particular register

Assume I have a very big source code and intend to make the rdx register totally unused during the execution, i.e., while generating the assembly code, all I want is to inform my compiler (GCC) that it should not use rdx at all.

NOTE: register rdx is just an example. I am OK with any available Intel x86 register.

I am even happy to update the source code of the compiler and use my custom GCC. But which changes to the source code are needed?

like image 639
Sandhya Kumar Avatar asked Dec 08 '22 01:12

Sandhya Kumar


2 Answers

You can put some global variable to this register. For ARM CPU you can do it this way:

register volatile type *global_ptr asm ("r8")

This instruction uses general purpose register "r8" to hold the value of global_ptr pointer.

See the source in U-Boot for real-life example:

http://git.denx.de/?p=u-boot.git;a=blob;f=arch/arm/include/asm/global_data.h;h=4e3ea55e290a19c766017b59241615f7723531d5;hb=HEAD#l83

File arch/arm/include/asm/global_data.h (line ~83).

#define DECLARE_GLOBAL_DATA_PTR register volatile gd_t *gd asm ("r8")

like image 27
KostaZ Avatar answered Dec 29 '22 14:12

KostaZ


You tell GCC not to allocate a register via the -ffixed-reg option (gcc docs).

  • -ffixed-reg

    Treat the register named reg as a fixed register; generated code should never refer to it (except perhaps as a stack pointer, frame pointer or in some other fixed role). reg must be the name of a register. The register names accepted are machine-specific and are defined in the REGISTER_NAMES macro in the machine description macro file.

For example, gcc -ffixed-r13 will make gcc leave it alone entirely. Using registers that are part of the calling convention, or required for certain instructions, may be problematic.

like image 101
chill Avatar answered Dec 29 '22 13:12

chill