Update comments.
[llvm/msp430.git] / lib / CodeGen / SelectionDAG / CallingConvLower.cpp
blob7cd2b73e8704d88d58071cee67c04decd00c3325
1 //===-- CallingConvLower.cpp - Calling Conventions ------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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/Target/TargetRegisterInfo.h"
17 #include "llvm/Target/TargetData.h"
18 #include "llvm/Target/TargetMachine.h"
19 using namespace llvm;
21 CCState::CCState(unsigned CC, bool isVarArg, const TargetMachine &tm,
22 SmallVector<CCValAssign, 16> &locs)
23 : CallingConv(CC), IsVarArg(isVarArg), TM(tm),
24 TRI(*TM.getRegisterInfo()), Locs(locs) {
25 // No stack is used.
26 StackOffset = 0;
28 UsedRegs.resize((TRI.getNumRegs()+31)/32);
31 // HandleByVal - Allocate a stack slot large enough to pass an argument by
32 // value. The size and alignment information of the argument is encoded in its
33 // parameter attribute.
34 void CCState::HandleByVal(unsigned ValNo, MVT ValVT,
35 MVT LocVT, CCValAssign::LocInfo LocInfo,
36 int MinSize, int MinAlign,
37 ISD::ArgFlagsTy ArgFlags) {
38 unsigned Align = ArgFlags.getByValAlign();
39 unsigned Size = ArgFlags.getByValSize();
40 if (MinSize > (int)Size)
41 Size = MinSize;
42 if (MinAlign > (int)Align)
43 Align = MinAlign;
44 unsigned Offset = AllocateStack(Size, Align);
46 addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
49 /// MarkAllocated - Mark a register and all of its aliases as allocated.
50 void CCState::MarkAllocated(unsigned Reg) {
51 UsedRegs[Reg/32] |= 1 << (Reg&31);
53 if (const unsigned *RegAliases = TRI.getAliasSet(Reg))
54 for (; (Reg = *RegAliases); ++RegAliases)
55 UsedRegs[Reg/32] |= 1 << (Reg&31);
58 /// AnalyzeFormalArguments - Analyze an ISD::FORMAL_ARGUMENTS node,
59 /// incorporating info about the formals into this state.
60 void CCState::AnalyzeFormalArguments(SDNode *TheArgs, CCAssignFn Fn) {
61 unsigned NumArgs = TheArgs->getNumValues()-1;
63 for (unsigned i = 0; i != NumArgs; ++i) {
64 MVT ArgVT = TheArgs->getValueType(i);
65 ISD::ArgFlagsTy ArgFlags =
66 cast<ARG_FLAGSSDNode>(TheArgs->getOperand(3+i))->getArgFlags();
67 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
68 cerr << "Formal argument #" << i << " has unhandled type "
69 << ArgVT.getMVTString() << "\n";
70 abort();
75 /// AnalyzeReturn - Analyze the returned values of an ISD::RET node,
76 /// incorporating info about the result values into this state.
77 void CCState::AnalyzeReturn(SDNode *TheRet, CCAssignFn Fn) {
78 // Determine which register each value should be copied into.
79 for (unsigned i = 0, e = TheRet->getNumOperands() / 2; i != e; ++i) {
80 MVT VT = TheRet->getOperand(i*2+1).getValueType();
81 ISD::ArgFlagsTy ArgFlags =
82 cast<ARG_FLAGSSDNode>(TheRet->getOperand(i*2+2))->getArgFlags();
83 if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this)){
84 cerr << "Return operand #" << i << " has unhandled type "
85 << VT.getMVTString() << "\n";
86 abort();
92 /// AnalyzeCallOperands - Analyze an ISD::CALL node, incorporating info
93 /// about the passed values into this state.
94 void CCState::AnalyzeCallOperands(CallSDNode *TheCall, CCAssignFn Fn) {
95 unsigned NumOps = TheCall->getNumArgs();
96 for (unsigned i = 0; i != NumOps; ++i) {
97 MVT ArgVT = TheCall->getArg(i).getValueType();
98 ISD::ArgFlagsTy ArgFlags = TheCall->getArgFlags(i);
99 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
100 cerr << "Call operand #" << i << " has unhandled type "
101 << ArgVT.getMVTString() << "\n";
102 abort();
107 /// AnalyzeCallOperands - Same as above except it takes vectors of types
108 /// and argument flags.
109 void CCState::AnalyzeCallOperands(SmallVectorImpl<MVT> &ArgVTs,
110 SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
111 CCAssignFn Fn) {
112 unsigned NumOps = ArgVTs.size();
113 for (unsigned i = 0; i != NumOps; ++i) {
114 MVT ArgVT = ArgVTs[i];
115 ISD::ArgFlagsTy ArgFlags = Flags[i];
116 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
117 cerr << "Call operand #" << i << " has unhandled type "
118 << ArgVT.getMVTString() << "\n";
119 abort();
124 /// AnalyzeCallResult - Analyze the return values of an ISD::CALL node,
125 /// incorporating info about the passed values into this state.
126 void CCState::AnalyzeCallResult(CallSDNode *TheCall, CCAssignFn Fn) {
127 for (unsigned i = 0, e = TheCall->getNumRetVals(); i != e; ++i) {
128 MVT VT = TheCall->getRetValType(i);
129 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
130 if (TheCall->isInreg())
131 Flags.setInReg();
132 if (Fn(i, VT, VT, CCValAssign::Full, Flags, *this)) {
133 cerr << "Call result #" << i << " has unhandled type "
134 << VT.getMVTString() << "\n";
135 abort();
140 /// AnalyzeCallResult - Same as above except it's specialized for calls which
141 /// produce a single value.
142 void CCState::AnalyzeCallResult(MVT VT, CCAssignFn Fn) {
143 if (Fn(0, VT, VT, CCValAssign::Full, ISD::ArgFlagsTy(), *this)) {
144 cerr << "Call result has unhandled type "
145 << VT.getMVTString() << "\n";
146 abort();