1 //===-- llvm/CallingConvLower.cpp - Calling Conventions -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source 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/MRegisterInfo.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 MRI(*TM.getRegisterInfo()), Locs(locs) {
28 UsedRegs.resize(MRI.getNumRegs());
31 void CCState::HandleStruct(unsigned ValNo, MVT::ValueType ValVT,
32 MVT::ValueType LocVT, CCValAssign::LocInfo LocInfo,
34 unsigned Align = 1 << ((ArgFlags & ISD::ParamFlags::ByValAlign) >>
35 ISD::ParamFlags::ByValAlignOffs);
36 unsigned Size = (ArgFlags & ISD::ParamFlags::ByValSize) >>
37 ISD::ParamFlags::ByValSizeOffs;
38 unsigned Offset = AllocateStack(Size, Align);
40 addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
43 /// MarkAllocated - Mark a register and all of its aliases as allocated.
44 void CCState::MarkAllocated(unsigned Reg) {
45 UsedRegs[Reg/32] |= 1 << (Reg&31);
47 if (const unsigned *RegAliases = MRI.getAliasSet(Reg))
48 for (; (Reg = *RegAliases); ++RegAliases)
49 UsedRegs[Reg/32] |= 1 << (Reg&31);
52 /// AnalyzeFormalArguments - Analyze an ISD::FORMAL_ARGUMENTS node,
53 /// incorporating info about the formals into this state.
54 void CCState::AnalyzeFormalArguments(SDNode *TheArgs, CCAssignFn Fn) {
55 unsigned NumArgs = TheArgs->getNumValues()-1;
57 for (unsigned i = 0; i != NumArgs; ++i) {
58 MVT::ValueType ArgVT = TheArgs->getValueType(i);
59 SDOperand FlagOp = TheArgs->getOperand(3+i);
60 unsigned ArgFlags = cast<ConstantSDNode>(FlagOp)->getValue();
61 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
62 cerr << "Formal argument #" << i << " has unhandled type "
63 << MVT::getValueTypeString(ArgVT) << "\n";
69 /// AnalyzeReturn - Analyze the returned values of an ISD::RET node,
70 /// incorporating info about the result values into this state.
71 void CCState::AnalyzeReturn(SDNode *TheRet, CCAssignFn Fn) {
72 // Determine which register each value should be copied into.
73 for (unsigned i = 0, e = TheRet->getNumOperands() / 2; i != e; ++i) {
74 MVT::ValueType VT = TheRet->getOperand(i*2+1).getValueType();
75 if (Fn(i, VT, VT, CCValAssign::Full,
76 cast<ConstantSDNode>(TheRet->getOperand(i*2+2))->getValue(), *this)){
77 cerr << "Return operand #" << i << " has unhandled type "
78 << MVT::getValueTypeString(VT) << "\n";
85 /// AnalyzeCallOperands - Analyze an ISD::CALL node, incorporating info
86 /// about the passed values into this state.
87 void CCState::AnalyzeCallOperands(SDNode *TheCall, CCAssignFn Fn) {
88 unsigned NumOps = (TheCall->getNumOperands() - 5) / 2;
89 for (unsigned i = 0; i != NumOps; ++i) {
90 MVT::ValueType ArgVT = TheCall->getOperand(5+2*i).getValueType();
91 SDOperand FlagOp = TheCall->getOperand(5+2*i+1);
92 unsigned ArgFlags =cast<ConstantSDNode>(FlagOp)->getValue();
93 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
94 cerr << "Call operand #" << i << " has unhandled type "
95 << MVT::getValueTypeString(ArgVT) << "\n";
101 /// AnalyzeCallResult - Analyze the return values of an ISD::CALL node,
102 /// incorporating info about the passed values into this state.
103 void CCState::AnalyzeCallResult(SDNode *TheCall, CCAssignFn Fn) {
104 for (unsigned i = 0, e = TheCall->getNumValues() - 1; i != e; ++i) {
105 MVT::ValueType VT = TheCall->getValueType(i);
106 if (Fn(i, VT, VT, CCValAssign::Full, 0, *this)) {
107 cerr << "Call result #" << i << " has unhandled type "
108 << MVT::getValueTypeString(VT) << "\n";