Difference between revisions of "The register allocator"

From Free Pascal wiki
Jump to navigationJump to search
Line 54: Line 54:
  
  
Tips<\br>
+
Tips</ br>
 
You can compile your project by using the -sr switch. This will leave the immaginary register names in the generated .s file
 
You can compile your project by using the -sr switch. This will leave the immaginary register names in the generated .s file

Revision as of 18:08, 16 December 2013

Registry allocation in FPC

1. Introduction

Main unit for the registry allocation is rgobj.pas Main class for the registry allocation is TRgObj located in rgobj.pas

Registry allocator provides imaginary registers for the assembler instructions during the code generation. Then it calculates the real registers on the place of the imanginary ones.

The fpc registry allocator uses the Registry coloring for determining the real registers. Also uses registry spilling technique - when there is not enough registers it uses the memory.


2. How to use the registry allocator This topic describes how to use the registry allocator during the code generation. Described like a black box with the public methods you can call to get job done.

2.1. Creating the Registry allocator Creating registry allocator is the first step we make before we can use its functionality.

The low level code generator creates instances of TRgObj class. They are created when the code generation of specific routine begin. Code generator works on subroutine level. The registry allocator also allocates registers for specific method, procedure, function.

One TRgObj instance allocates registers of certain type. For example Integer registers. Thats way we have a few TRgObj instances, one for every type of register that the cpu supports.


2.2. Using registers in the code generation


Code generator uses TRgObj.getRegister to get register for some assembler instruction. This way an imaginary register is allocation and can be used after that in specific assembler instruction. There is another method if more than one imaginary register needs to be allocated - TRgObj.allocCpuRegisters

After allocation the register we can use it in some assembler instructions. For evry instruction that generates, the code generator notifies the registry allocator. It passes the instruction, also the imaginary register as parameters to the following method.

         TRgObj.add_reg_instruction(instr, r, cg.executionweight);

But for MOV instruction there is specific method that is used

         TRgObj.add_move_instruction(instr:Taicpu);

If an imaginary register is allocated, but later it becomes useless it can be deallocated by using the following methods

 ungetcpuregister - Free register specified.
 dealloccpuregisters - Free multiple registers specified.


2.3. Generating of real registers At the end when all the assemble instructions are generated we call

       do_register_allocation(list: TAsmList; headerTai: TAi)

It calculates real registers for the immaginary ones.


Tips</ br> You can compile your project by using the -sr switch. This will leave the immaginary register names in the generated .s file