1 //===-- llvm/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/Target/TargetRegisterInfo.h"
17 #include "llvm/Target/TargetData.h"
18 #include "llvm/Target/TargetMachine.h"
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) {
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)
42 if (MinAlign > (int)Align)
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";
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";
92 /// AnalyzeCallOperands - Analyze an ISD::CALL node, incorporating info
93 /// about the passed values into this state.
94 void CCState::AnalyzeCallOperands(SDNode *TheCall, CCAssignFn Fn) {
95 unsigned NumOps = (TheCall->getNumOperands() - 5) / 2;
96 for (unsigned i = 0; i != NumOps; ++i) {
97 MVT ArgVT = TheCall->getOperand(5+2*i).getValueType();
98 ISD::ArgFlagsTy ArgFlags =
99 cast<ARG_FLAGSSDNode>(TheCall->getOperand(5+2*i+1))->getArgFlags();
100 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
101 cerr << "Call operand #" << i << " has unhandled type "
102 << ArgVT.getMVTString() << "\n";
108 /// AnalyzeCallResult - Analyze the return values of an ISD::CALL node,
109 /// incorporating info about the passed values into this state.
110 void CCState::AnalyzeCallResult(SDNode *TheCall, CCAssignFn Fn) {
111 for (unsigned i = 0, e = TheCall->getNumValues() - 1; i != e; ++i) {
112 MVT VT = TheCall->getValueType(i);
113 if (Fn(i, VT, VT, CCValAssign::Full, ISD::ArgFlagsTy(), *this)) {
114 cerr << "Call result #" << i << " has unhandled type "
115 << VT.getMVTString() << "\n";