Defining registers and registers sets

This recipe shows you how to define registers and register sets in .td files. The tablegen function will convert this .td file into .inc files, which will be the #include declarative in our .cpp files and refer to registers.

Getting ready

We have defined our toy target machine to have four registers (r0-r3), a stack register (sp), and a link register (lr). These can be specified in the TOYRegisterInfo.td file. The tablegen function provides the Register class, which can be extended to specify the registers.

How to do it…

To define the backend architecture using target descriptor files, proceed with the following steps.

  1. Create a new folder in lib/Target named TOY:
    $ mkdir llvm_root_directory/lib/Target/TOY
    
  2. Create a new TOYRegisterInfo.td file in the new TOY folder:
    $ cd llvm_root_directory/lib/Target/TOY
    $ vi TOYRegisterInfo.td
    
  3. Define the hardware encoding, namespace, registers, and the register class:
    class TOYReg<bits<16> Enc, string n> : Register<n> {
      let HWEncoding = Enc;
      let Namespace = "TOY";
    }
    
    foreach i = 0-3 in {
        def R#i : R<i, "r"#i >;
    }
    
    def SP  : TOYReg<13, "sp">;
    def LR  : TOYReg<14, "lr">;
    
    def GRRegs : RegisterClass<"TOY", [i32], 32,
      (add R0, R1, R2, R3, SP)>;

How it works…

The tablegen function processes this .td file to generate the .inc file, which generally has enums generated for these registers. These enums can be used in the.cpp files, in which the registers can be referenced as TOY::R0. These .inc files will be generated when we build the LLVM project.

See also

  • To get more details about how registers are defined for more advanced architecture, such as ARM, refer to the lib/Target/ARM/ARMRegisterInfo.td file in the source code of LLVM.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.77.208