1 //===-- CallingConvLower.cpp - Calling Conventions ------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the CCState class, used for lowering and implementing
11 // calling conventions.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/CodeGen/CallingConvLower.h"
16 #include "llvm/Support/ErrorHandling.h"
17 #include "llvm/Support/raw_ostream.h"
18 #include "llvm/Target/TargetRegisterInfo.h"
19 #include "llvm/Target/TargetData.h"
20 #include "llvm/Target/TargetMachine.h"
23 CCState::CCState(unsigned CC, bool isVarArg, const TargetMachine &tm,
24 SmallVector<CCValAssign, 16> &locs, LLVMContext *C)
25 : CallingConv(CC), IsVarArg(isVarArg), TM(tm),
26 TRI(*TM.getRegisterInfo()), Locs(locs), Context(C) {
30 UsedRegs.resize((TRI.getNumRegs()+31)/32);
33 // HandleByVal - Allocate a stack slot large enough to pass an argument by
34 // value. The size and alignment information of the argument is encoded in its
35 // parameter attribute.
36 void CCState::HandleByVal(unsigned ValNo, MVT ValVT,
37 MVT LocVT, CCValAssign::LocInfo LocInfo,
38 int MinSize, int MinAlign,
39 ISD::ArgFlagsTy ArgFlags) {
40 unsigned Align = ArgFlags.getByValAlign();
41 unsigned Size = ArgFlags.getByValSize();
42 if (MinSize > (int)Size)
44 if (MinAlign > (int)Align)
46 unsigned Offset = AllocateStack(Size, Align);
48 addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
51 /// MarkAllocated - Mark a register and all of its aliases as allocated.
52 void CCState::MarkAllocated(unsigned Reg) {
53 UsedRegs[Reg/32] |= 1 << (Reg&31);
55 if (const unsigned *RegAliases = TRI.getAliasSet(Reg))
56 for (; (Reg = *RegAliases); ++RegAliases)
57 UsedRegs[Reg/32] |= 1 << (Reg&31);
60 /// AnalyzeFormalArguments - Analyze an ISD::FORMAL_ARGUMENTS node,
61 /// incorporating info about the formals into this state.
62 void CCState::AnalyzeFormalArguments(SDNode *TheArgs, CCAssignFn Fn) {
63 unsigned NumArgs = TheArgs->getNumValues()-1;
65 for (unsigned i = 0; i != NumArgs; ++i) {
66 MVT ArgVT = TheArgs->getValueType(i);
67 ISD::ArgFlagsTy ArgFlags =
68 cast<ARG_FLAGSSDNode>(TheArgs->getOperand(3+i))->getArgFlags();
69 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
71 raw_string_ostream Msg(msg);
72 Msg << "Formal argument #" << i << " has unhandled type "
73 << ArgVT.getMVTString();
74 llvm_report_error(Msg.str());
79 /// AnalyzeReturn - Analyze the returned values of an ISD::RET node,
80 /// incorporating info about the result values into this state.
81 void CCState::AnalyzeReturn(SDNode *TheRet, CCAssignFn Fn) {
82 // Determine which register each value should be copied into.
83 for (unsigned i = 0, e = TheRet->getNumOperands() / 2; i != e; ++i) {
84 MVT VT = TheRet->getOperand(i*2+1).getValueType();
85 ISD::ArgFlagsTy ArgFlags =
86 cast<ARG_FLAGSSDNode>(TheRet->getOperand(i*2+2))->getArgFlags();
87 if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this)){
89 raw_string_ostream Msg(msg);
90 Msg << "Return operand #" << i << " has unhandled type "
92 llvm_report_error(Msg.str());
98 /// AnalyzeCallOperands - Analyze an ISD::CALL node, incorporating info
99 /// about the passed values into this state.
100 void CCState::AnalyzeCallOperands(CallSDNode *TheCall, CCAssignFn Fn) {
101 unsigned NumOps = TheCall->getNumArgs();
102 for (unsigned i = 0; i != NumOps; ++i) {
103 MVT ArgVT = TheCall->getArg(i).getValueType();
104 ISD::ArgFlagsTy ArgFlags = TheCall->getArgFlags(i);
105 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
107 raw_string_ostream Msg(msg);
108 Msg << "Call operand #" << i << " has unhandled type "
109 << ArgVT.getMVTString();
110 llvm_report_error(Msg.str());
115 /// AnalyzeCallOperands - Same as above except it takes vectors of types
116 /// and argument flags.
117 void CCState::AnalyzeCallOperands(SmallVectorImpl<MVT> &ArgVTs,
118 SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
120 unsigned NumOps = ArgVTs.size();
121 for (unsigned i = 0; i != NumOps; ++i) {
122 MVT ArgVT = ArgVTs[i];
123 ISD::ArgFlagsTy ArgFlags = Flags[i];
124 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
126 raw_string_ostream Msg(msg);
127 Msg << "Call operand #" << i << " has unhandled type "
128 << ArgVT.getMVTString();
129 llvm_report_error(Msg.str());
134 /// AnalyzeCallResult - Analyze the return values of an ISD::CALL node,
135 /// incorporating info about the passed values into this state.
136 void CCState::AnalyzeCallResult(CallSDNode *TheCall, CCAssignFn Fn) {
137 for (unsigned i = 0, e = TheCall->getNumRetVals(); i != e; ++i) {
138 MVT VT = TheCall->getRetValType(i);
139 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
140 if (TheCall->isInreg())
142 if (Fn(i, VT, VT, CCValAssign::Full, Flags, *this)) {
144 raw_string_ostream Msg(msg);
145 Msg << "Call result #" << i << " has unhandled type "
146 << VT.getMVTString();
147 llvm_report_error(Msg.str());
152 /// AnalyzeCallResult - Same as above except it's specialized for calls which
153 /// produce a single value.
154 void CCState::AnalyzeCallResult(MVT VT, CCAssignFn Fn) {
155 if (Fn(0, VT, VT, CCValAssign::Full, ISD::ArgFlagsTy(), *this)) {
157 raw_string_ostream Msg(msg);
158 Msg << "Call result has unhandled type "
159 << VT.getMVTString();
160 llvm_report_error(Msg.str());