@@ -440,6 +440,19 @@ def RetCC_X86_64_HHVM: CallingConv<[
440440 RAX, R10, R11, R13, R14, R15]>>
441441]>;
442442
443+ def RetCC_X86_64_OCaml : CallingConv<[
444+ // Promote all types to i64
445+ CCIfType<[i8, i16, i32], CCPromoteToType<i64>>,
446+
447+ // Every function preserves the necessary function arguments
448+ CCIfType<[i64], CCAssignToReg<[R14, RAX, RBX, RDI, RSI, RDX, RCX, R8, R9, R12, R13]>>,
449+
450+ CCIfType<[f32, f64], CCAssignToReg<[
451+ XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6, XMM7,
452+ XMM8, XMM9, XMM10, XMM11, XMM12, XMM13, XMM14, XMM15
453+ ]>>
454+ ]>;
455+
443456
444457defm X86_32_RegCall :
445458 X86_RegCall_base<RC_X86_32_RegCall>;
@@ -494,6 +507,9 @@ def RetCC_X86_64 : CallingConv<[
494507 // Mingw64 and native Win64 use Win64 CC
495508 CCIfSubtarget<"isTargetWin64()", CCDelegateTo<RetCC_X86_Win64_C>>,
496509
510+ // Handle OCaml calls
511+ CCIfCC<"CallingConv::OCaml", CCDelegateTo<RetCC_X86_64_OCaml>>,
512+
497513 // Otherwise, drop to normal X86-64 CC
498514 CCDelegateTo<RetCC_X86_64_C>
499515]>;
@@ -700,6 +716,24 @@ def CC_X86_Win64_VectorCall : CallingConv<[
700716 CCDelegateTo<CC_X86_Win64_C>
701717]>;
702718
719+ def CC_X86_64_OCaml : CallingConv<[
720+ // Promote i8/i16/i32 arguments to i64.
721+ CCIfType<[i8, i16, i32], CCPromoteToType<i64>>,
722+
723+ // See https://github.com/oxcaml/oxcaml/blob/main/backend/amd64/proc.ml#L34
724+ // for further details on the registers and their meaning. Runtime registers
725+ // (registers containing pointers to data structures maintained by the
726+ // runtime) must be preserved through the run of the program. To ensure LLVM
727+ // 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]>>,
731+
732+ CCIfType<[f32, f64], CCAssignToReg<[
733+ XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6, XMM7,
734+ XMM8, XMM9, XMM10, XMM11, XMM12, XMM13, XMM14, XMM15
735+ ]>>
736+ ]>;
703737
704738def CC_X86_64_GHC : CallingConv<[
705739 // Promote i8/i16/i32 arguments to i64.
@@ -1103,6 +1137,7 @@ def CC_X86_64 : CallingConv<[
11031137 CCIfSubtarget<"isTargetWin64()", CCDelegateTo<CC_X86_Win64_RegCall>>>,
11041138 CCIfCC<"CallingConv::X86_RegCall", CCDelegateTo<CC_X86_SysV64_RegCall>>,
11051139 CCIfCC<"CallingConv::X86_INTR", CCCustom<"CC_X86_Intr">>,
1140+ CCIfCC<"CallingConv::OCaml", CCDelegateTo<CC_X86_64_OCaml>>,
11061141
11071142 // Mingw64 and native Win64 use Win64 CC
11081143 CCIfSubtarget<"isTargetWin64()", CCDelegateTo<CC_X86_Win64_C>>,
0 commit comments