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 array of argument values,
61 /// incorporating info about the formals into this state.
63 CCState::AnalyzeFormalArguments(const SmallVectorImpl
<ISD::InputArg
> &Ins
,
65 unsigned NumArgs
= Ins
.size();
67 for (unsigned i
= 0; i
!= NumArgs
; ++i
) {
68 MVT ArgVT
= Ins
[i
].VT
;
69 ISD::ArgFlagsTy ArgFlags
= Ins
[i
].Flags
;
70 if (Fn(i
, ArgVT
, ArgVT
, CCValAssign::Full
, ArgFlags
, *this)) {
72 cerr
<< "Formal argument #" << i
<< " has unhandled type "
73 << ArgVT
.getMVTString();
80 /// AnalyzeReturn - Analyze the returned values of a return,
81 /// incorporating info about the result values into this state.
82 void CCState::AnalyzeReturn(const SmallVectorImpl
<ISD::OutputArg
> &Outs
,
84 // Determine which register each value should be copied into.
85 for (unsigned i
= 0, e
= Outs
.size(); i
!= e
; ++i
) {
86 MVT VT
= Outs
[i
].Val
.getValueType();
87 ISD::ArgFlagsTy ArgFlags
= Outs
[i
].Flags
;
88 if (Fn(i
, VT
, VT
, CCValAssign::Full
, ArgFlags
, *this)) {
90 cerr
<< "Return operand #" << i
<< " has unhandled type "
99 /// AnalyzeCallOperands - Analyze the outgoing arguments to a call,
100 /// incorporating info about the passed values into this state.
101 void CCState::AnalyzeCallOperands(const SmallVectorImpl
<ISD::OutputArg
> &Outs
,
103 unsigned NumOps
= Outs
.size();
104 for (unsigned i
= 0; i
!= NumOps
; ++i
) {
105 MVT ArgVT
= Outs
[i
].Val
.getValueType();
106 ISD::ArgFlagsTy ArgFlags
= Outs
[i
].Flags
;
107 if (Fn(i
, ArgVT
, ArgVT
, CCValAssign::Full
, ArgFlags
, *this)) {
109 cerr
<< "Call operand #" << i
<< " has unhandled type "
110 << ArgVT
.getMVTString();
117 /// AnalyzeCallOperands - Same as above except it takes vectors of types
118 /// and argument flags.
119 void CCState::AnalyzeCallOperands(SmallVectorImpl
<MVT
> &ArgVTs
,
120 SmallVectorImpl
<ISD::ArgFlagsTy
> &Flags
,
122 unsigned NumOps
= ArgVTs
.size();
123 for (unsigned i
= 0; i
!= NumOps
; ++i
) {
124 MVT ArgVT
= ArgVTs
[i
];
125 ISD::ArgFlagsTy ArgFlags
= Flags
[i
];
126 if (Fn(i
, ArgVT
, ArgVT
, CCValAssign::Full
, ArgFlags
, *this)) {
128 cerr
<< "Call operand #" << i
<< " has unhandled type "
129 << ArgVT
.getMVTString();
136 /// AnalyzeCallResult - Analyze the return values of a call,
137 /// incorporating info about the passed values into this state.
138 void CCState::AnalyzeCallResult(const SmallVectorImpl
<ISD::InputArg
> &Ins
,
140 for (unsigned i
= 0, e
= Ins
.size(); i
!= e
; ++i
) {
142 ISD::ArgFlagsTy Flags
= Ins
[i
].Flags
;
143 if (Fn(i
, VT
, VT
, CCValAssign::Full
, Flags
, *this)) {
145 cerr
<< "Call result #" << i
<< " has unhandled type "
146 << VT
.getMVTString();
153 /// AnalyzeCallResult - Same as above except it's specialized for calls which
154 /// produce a single value.
155 void CCState::AnalyzeCallResult(MVT VT
, CCAssignFn Fn
) {
156 if (Fn(0, VT
, VT
, CCValAssign::Full
, ISD::ArgFlagsTy(), *this)) {
158 cerr
<< "Call result has unhandled type "
159 << VT
.getMVTString();