Skip to content

Adding a new intrinsic function

Jeff Bush edited this page Mar 30, 2015 · 3 revisions

Adding a new intrinsic/built-in function that is accessible from C/C++ requires four steps:

  1. Add a definition for the function to NyuziToolchain/tools/clang/include/clang/Basic/BuiltinsNyuzi.def, for example:

     BUILTIN(__builtin_nyuzi_read_control_reg, "ii", "n")
    
  2. Open NyuziToolchain/tools/clang/lib/CodeGen/CGBuiltin.cpp and look at the function CodeGenFunction::EmitNyuziBuiltinExpr. Add an entry to the switch statement:

     switch (BuiltinID) {
         ...
         case Nyuzi::BI__builtin_nyuzi_read_control_reg:
    
  3. Edit NyuziToolchain/include/llvm/IR/IntrinsicsNyuzi.td and add a definition of the intrinsic to the LLVM backend

     def int_nyuzi_read_control_reg : Intrinsic<[llvm_i32_ty], [llvm_i32_ty], [],
         "llvm.nyuzi.__builtin_nyuzi_read_control_reg">;
    
  4. Add a pattern to the instruction table NyuziToolchain/lib/Target/Nyuzi/NyuziInstrInfo.td

     def READ_CONTROL_REG : FormatMInst<
         (outs GPR32:$dest),
         (ins i32imm:$cr),
         "getcr $dest, $cr",
         [(set i32:$dest, (int_nyuzi_read_control_reg imm:$cr))],
         ...