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.
To implement the calling convention, proceed with the following steps:
TOYCallingConv.td
file in the lib/Target/TOY
folder:$ vi TOYCallingConv.td
def RetCC_TOY : CallingConv<[ CCIfType<[i32], CCAssignToReg<[R0]>>, CCIfType<[i32], CCAssignToStack<4, 4>> ]>;
def CC_TOY : CallingConv<[ CCIfType<[i8, i16], CCPromoteToType<i32>>, CCIfType<[i32], CCAssignToReg<[R0, R1]>>, CCIfType<[i32], CCAssignToStack<4, 4>> ]>;
def CC_Save : CalleeSavedRegs<(add R2, R3)>;
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()
.
18.117.146.155