1 //===-- M68kCallingConv.h - M68k Custom CC Routines ---------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// This file contains the custom routines for the M68k Calling Convention
11 /// that aren't done by tablegen.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_LIB_TARGET_M68K_M68KCALLINGCONV_H
16 #define LLVM_LIB_TARGET_M68K_M68KCALLINGCONV_H
17 
18 #include "MCTargetDesc/M68kMCTargetDesc.h"
19 
20 #include "llvm/CodeGen/CallingConvLower.h"
21 #include "llvm/IR/CallingConv.h"
22 #include "llvm/IR/Function.h"
23 
24 namespace llvm {
25 
26 /// Custom state to propagate llvm type info to register CC assigner
27 class M68kCCState : public CCState {
28 public:
29   const llvm::Function &F;
30 
31   M68kCCState(const llvm::Function &F, CallingConv::ID CC, bool IsVarArg,
32               MachineFunction &MF, SmallVectorImpl<CCValAssign> &Locs,
33               LLVMContext &C)
34       : CCState(CC, IsVarArg, MF, Locs, C), F(F) {}
35 };
36 
37 /// NOTE this function is used to select registers for formal arguments and call
38 /// FIXME: Handling on pointer arguments is not complete
39 inline bool CC_M68k_Any_AssignToReg(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
40                                     CCValAssign::LocInfo &LocInfo,
41                                     ISD::ArgFlagsTy &ArgFlags, CCState &State) {
42   M68kCCState CCInfo = static_cast<M68kCCState &>(State);
43 
44   static const MCPhysReg DataRegList[] = {M68k::D0, M68k::D1, M68k::A0,
45                                           M68k::A1};
46 
47   // Address registers have %a register priority
48   static const MCPhysReg AddrRegList[] = {
49       M68k::A0,
50       M68k::A1,
51       M68k::D0,
52       M68k::D1,
53   };
54 
55   auto I = CCInfo.F.arg_begin();
56   int No = ValNo;
57   while (No > 0) {
58     No -= I->getType()->isIntegerTy(64) ? 2 : 1;
59     I++;
60   }
61 
62   bool IsPtr = I != CCInfo.F.arg_end() && I->getType()->isPointerTy();
63 
64   unsigned Reg =
65       IsPtr ? State.AllocateReg(AddrRegList) : State.AllocateReg(DataRegList);
66 
67   if (Reg) {
68     State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
69     return true;
70   }
71 
72   return false;
73 }
74 
75 } // namespace llvm
76 
77 #endif
78