@@ -444,15 +444,21 @@ def RetCC_X86_64_OCaml : CallingConv<[
444444  // Promote all types to i64
445445  CCIfType<[i8, i16, i32], CCPromoteToType<i64>>,
446446
447-   // Every function preserves the necessary function arguments
448-   CCIfType<[i64], CCAssignToReg<[R14, RAX, RBX, RDI, RSI, RDX, RCX, R8, R9, R12, R13]>>,
447+   // Runtime registers (R14, R15) are threaded through function calls
448+   // See the argument calling conventions for OCaml for more details
449+   CCIfType<[i64], CCAssignToReg<[R14, R15, RAX, RBX, RDI, RSI, RDX, RCX, R8, R9, R12, R13]>>,
449450
450451  CCIfType<[f32, f64], CCAssignToReg<[
451452    XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6, XMM7,
452453    XMM8, XMM9, XMM10, XMM11, XMM12, XMM13, XMM14, XMM15
453454  ]>>
454455]>;
455456
457+ def RetCC_X86_64_OCaml_C_Call : CallingConv<[
458+   CCIfType<[i64], CCAssignToReg<[R14, R15]>>,
459+ 
460+   CCDelegateTo<RetCC_X86_64_C>
461+ ]>;
456462
457463defm X86_32_RegCall :
458464	 X86_RegCall_base<RC_X86_32_RegCall>;
@@ -509,6 +515,7 @@ def RetCC_X86_64 : CallingConv<[
509515
510516  // Handle OCaml calls
511517  CCIfCC<"CallingConv::OCaml", CCDelegateTo<RetCC_X86_64_OCaml>>,
518+   CCIfCC<"CallingConv::OCaml_C_Call", CCDelegateTo<RetCC_X86_64_OCaml_C_Call>>,
512519
513520  // Otherwise, drop to normal X86-64 CC
514521  CCDelegateTo<RetCC_X86_64_C>
@@ -725,9 +732,8 @@ def CC_X86_64_OCaml : CallingConv<[
725732  // (registers containing pointers to data structures maintained by the
726733  // runtime) must be preserved through the run of the program. To ensure LLVM
727734  // doesn't mess with them, they are explicitly threaded through function calls
728-   // and returns. These are R14 and R15. Note that R15 isn't listed since it is
729-   // not implemented yet.
730-   CCIfType<[i64], CCAssignToReg<[R14, RAX, RBX, RDI, RSI, RDX, RCX, R8, R9, R12, R13]>>,
735+   // and returns. These are R14 and R15.
736+   CCIfType<[i64], CCAssignToReg<[R14, R15, RAX, RBX, RDI, RSI, RDX, RCX, R8, R9, R12, R13]>>,
731737
732738  CCIfType<[f32, f64], CCAssignToReg<[
733739    XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6, XMM7,
@@ -738,7 +744,7 @@ def CC_X86_64_OCaml : CallingConv<[
738744def CC_X86_64_OCaml_C_Call : CallingConv<[
739745  // OCaml wraps C calls with another function that transfers stack arguments.
740746  // RAX contains the function address, and R12 contains the size of the stack arguments.
741-   CCIfType<[i64], CCAssignToReg<[RAX, R12]>>,
747+   CCIfType<[i64], CCAssignToReg<[R14, R15,  RAX, R12]>>,
742748
743749  // Follow C convention normally otherwise
744750  CCDelegateTo<CC_X86_64_C>
@@ -1173,6 +1179,9 @@ def CSR_NoRegs : CalleeSavedRegs<(add)>;
11731179def CSR_32 : CalleeSavedRegs<(add ESI, EDI, EBX, EBP)>;
11741180def CSR_64 : CalleeSavedRegs<(add RBX, R12, R13, R14, R15, RBP)>;
11751181
1182+ // R14 and R15 are used as return registers, so they aren't callee saved.
1183+ def CSR_64_OCaml_C_Call : CalleeSavedRegs<(sub CSR_64, R14, R15)>;
1184+ 
11761185def CSR_64_SwiftError : CalleeSavedRegs<(sub CSR_64, R12)>;
11771186def CSR_64_SwiftTail : CalleeSavedRegs<(sub CSR_64, R13, R14)>;
11781187
0 commit comments