Defining the calling convention

The calling convention specifies how values are passed to and from a function call. Our TOY architecture specifies that two arguments are passed in two registers, r0 and r1, while the remaining ones are passed to the stack. This recipe shows you how to define the calling convention, which will be used in ISelLowering (the instruction selection lowering phase discussed in Chapter 6, Target Independent Code Generator) via function pointers.

The calling convention will be defined in the TOYCallingConv.td file, which will have primarily two sections—one for defining the return value convention, and the other for defining the argument passing convention. The return value convention specifies how the return values will reside and in which registers. The argument passing convention will specify how the arguments passed will reside and in which registers. The CallingConv class is inherited while defining the calling convention of the toy architecture.

How to do it…

To implement the calling convention, proceed with the following steps:

  1. Create a new TOYCallingConv.td file in the lib/Target/TOY folder:
    $ vi TOYCallingConv.td
    
  2. In that file, define the return value convention, as follows:
    def RetCC_TOY : CallingConv<[
      CCIfType<[i32], CCAssignToReg<[R0]>>,
      CCIfType<[i32], CCAssignToStack<4, 4>>
    ]>;
    
  3. Also, define the argument passing convention, like this:
    def CC_TOY : CallingConv<[
      CCIfType<[i8, i16], CCPromoteToType<i32>>,
      CCIfType<[i32], CCAssignToReg<[R0, R1]>>,
      CCIfType<[i32], CCAssignToStack<4, 4>>
    ]>;
    
  4. Define the callee saved register set:
    def CC_Save : CalleeSavedRegs<(add R2, R3)>;
    

How it works…

In the .td file you just read about, it has been specified that the return values of the integer type of 32 bits are stored in the r0 register. Whenever arguments are passed to a function, the first two arguments will be stored in the r0 and r1 registers. It is also specified that whenever any data type, such as an integer of 8 bits or 16 bits, will be encountered, it will be promoted to the 32-bit integer type.

The tablegen function generates a TOYCallingConv.inc file, which will be referred to in the TOYISelLowering.cpp file. The two target hook functions used to define argument handling are LowerFormalArguments() and LowerReturn().

See also

  • To see a detailed implementation of advanced architectures, such as ARM, look into the lib/Target/ARM/ARMCallingConv.td file
..................Content has been hidden....................

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