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/CodeGen/SelectionDAGNodes.h"
17 #include "llvm/Target/TargetRegisterInfo.h"
18 #include "llvm/Target/TargetData.h"
19 #include "llvm/Target/TargetMachine.h"
22 CCState::CCState(unsigned CC, bool isVarArg, const TargetMachine &tm,
23 SmallVector<CCValAssign, 16> &locs)
24 : CallingConv(CC), IsVarArg(isVarArg), TM(tm),
25 TRI(*TM.getRegisterInfo()), Locs(locs) {
29 UsedRegs.resize(TRI.getNumRegs());
32 // HandleByVal - Allocate a stack slot large enough to pass an argument by
33 // value. The size and alignment information of the argument is encoded in its
34 // parameter attribute.
35 void CCState::HandleByVal(unsigned ValNo, MVT::ValueType ValVT,
36 MVT::ValueType LocVT, CCValAssign::LocInfo LocInfo,
37 int MinSize, int MinAlign,
38 ISD::ArgFlagsTy ArgFlags) {
39 unsigned Align = ArgFlags.getByValAlign();
40 unsigned Size = ArgFlags.getByValSize();
41 if (MinSize > (int)Size)
43 if (MinAlign > (int)Align)
45 unsigned Offset = AllocateStack(Size, Align);
47 addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
50 /// MarkAllocated - Mark a register and all of its aliases as allocated.
51 void CCState::MarkAllocated(unsigned Reg) {
52 UsedRegs[Reg/32] |= 1 << (Reg&31);
54 if (const unsigned *RegAliases = TRI.getAliasSet(Reg))
55 for (; (Reg = *RegAliases); ++RegAliases)
56 UsedRegs[Reg/32] |= 1 << (Reg&31);
59 /// AnalyzeFormalArguments - Analyze an ISD::FORMAL_ARGUMENTS node,
60 /// incorporating info about the formals into this state.
61 void CCState::AnalyzeFormalArguments(SDNode *TheArgs, CCAssignFn Fn) {
62 unsigned NumArgs = TheArgs->getNumValues()-1;
64 for (unsigned i = 0; i != NumArgs; ++i) {
65 MVT::ValueType ArgVT = TheArgs->getValueType(i);
66 ISD::ArgFlagsTy ArgFlags =
67 cast<ARG_FLAGSSDNode>(TheArgs->getOperand(3+i))->getArgFlags();
68 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
69 cerr << "Formal argument #" << i << " has unhandled type "
70 << MVT::getValueTypeString(ArgVT) << "\n";
76 /// AnalyzeReturn - Analyze the returned values of an ISD::RET node,
77 /// incorporating info about the result values into this state.
78 void CCState::AnalyzeReturn(SDNode *TheRet, CCAssignFn Fn) {
79 // Determine which register each value should be copied into.
80 for (unsigned i = 0, e = TheRet->getNumOperands() / 2; i != e; ++i) {
81 MVT::ValueType VT = TheRet->getOperand(i*2+1).getValueType();
82 ISD::ArgFlagsTy ArgFlags =
83 cast<ARG_FLAGSSDNode>(TheRet->getOperand(i*2+2))->getArgFlags();
84 if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this)){
85 cerr << "Return operand #" << i << " has unhandled type "
86 << MVT::getValueTypeString(VT) << "\n";
93 /// AnalyzeCallOperands - Analyze an ISD::CALL node, incorporating info
94 /// about the passed values into this state.
95 void CCState::AnalyzeCallOperands(SDNode *TheCall, CCAssignFn Fn) {
96 unsigned NumOps = (TheCall->getNumOperands() - 5) / 2;
97 for (unsigned i = 0; i != NumOps; ++i) {
98 MVT::ValueType ArgVT = TheCall->getOperand(5+2*i).getValueType();
99 ISD::ArgFlagsTy ArgFlags =
100 cast<ARG_FLAGSSDNode>(TheCall->getOperand(5+2*i+1))->getArgFlags();
101 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
102 cerr << "Call operand #" << i << " has unhandled type "
103 << MVT::getValueTypeString(ArgVT) << "\n";
109 /// AnalyzeCallResult - Analyze the return values of an ISD::CALL node,
110 /// incorporating info about the passed values into this state.
111 void CCState::AnalyzeCallResult(SDNode *TheCall, CCAssignFn Fn) {
112 for (unsigned i = 0, e = TheCall->getNumValues() - 1; i != e; ++i) {
113 MVT::ValueType VT = TheCall->getValueType(i);
114 if (Fn(i, VT, VT, CCValAssign::Full, ISD::ArgFlagsTy(), *this)) {
115 cerr << "Call result #" << i << " has unhandled type "
116 << MVT::getValueTypeString(VT) << "\n";