blob: 522ad7399e2c54be078466269a422057c322cc94 [file] [log] [blame]
Chris Lattner362e98a2007-02-27 04:43:02 +00001//===-- llvm/CallingConvLower.cpp - Calling Conventions -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner362e98a2007-02-27 04:43:02 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the CCState class, used for lowering and implementing
11// calling conventions.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattnerfb39f992007-02-28 06:56:37 +000016#include "llvm/CodeGen/SelectionDAGNodes.h"
Chris Lattner362e98a2007-02-27 04:43:02 +000017#include "llvm/Target/MRegisterInfo.h"
Evan Chengc67e6e82008-01-15 03:14:05 +000018#include "llvm/Target/TargetData.h"
Chris Lattner13513b72007-02-27 05:13:54 +000019#include "llvm/Target/TargetMachine.h"
Chris Lattner362e98a2007-02-27 04:43:02 +000020using namespace llvm;
21
Chris Lattneraeeccfc2007-06-19 00:11:09 +000022CCState::CCState(unsigned CC, bool isVarArg, const TargetMachine &tm,
Chris Lattner13513b72007-02-27 05:13:54 +000023 SmallVector<CCValAssign, 16> &locs)
Chris Lattneraeeccfc2007-06-19 00:11:09 +000024 : CallingConv(CC), IsVarArg(isVarArg), TM(tm),
25 MRI(*TM.getRegisterInfo()), Locs(locs) {
Chris Lattner362e98a2007-02-27 04:43:02 +000026 // No stack is used.
27 StackOffset = 0;
28
29 UsedRegs.resize(MRI.getNumRegs());
30}
31
Rafael Espindola594d37e2007-08-10 14:44:42 +000032void CCState::HandleStruct(unsigned ValNo, MVT::ValueType ValVT,
33 MVT::ValueType LocVT, CCValAssign::LocInfo LocInfo,
34 unsigned ArgFlags) {
Evan Chengc67e6e82008-01-15 03:14:05 +000035 unsigned MinAlign = TM.getTargetData()->getPointerABIAlignment();
Rafael Espindola594d37e2007-08-10 14:44:42 +000036 unsigned Align = 1 << ((ArgFlags & ISD::ParamFlags::ByValAlign) >>
37 ISD::ParamFlags::ByValAlignOffs);
38 unsigned Size = (ArgFlags & ISD::ParamFlags::ByValSize) >>
39 ISD::ParamFlags::ByValSizeOffs;
Evan Chengc67e6e82008-01-15 03:14:05 +000040 unsigned Offset = AllocateStack(Size, std::max(MinAlign, Align));
Rafael Espindola594d37e2007-08-10 14:44:42 +000041
42 addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
43}
Chris Lattner362e98a2007-02-27 04:43:02 +000044
45/// MarkAllocated - Mark a register and all of its aliases as allocated.
46void CCState::MarkAllocated(unsigned Reg) {
47 UsedRegs[Reg/32] |= 1 << (Reg&31);
48
49 if (const unsigned *RegAliases = MRI.getAliasSet(Reg))
50 for (; (Reg = *RegAliases); ++RegAliases)
51 UsedRegs[Reg/32] |= 1 << (Reg&31);
52}
Chris Lattnerfb39f992007-02-28 06:56:37 +000053
Chris Lattner66baf262007-02-28 07:09:40 +000054/// AnalyzeFormalArguments - Analyze an ISD::FORMAL_ARGUMENTS node,
55/// incorporating info about the formals into this state.
56void CCState::AnalyzeFormalArguments(SDNode *TheArgs, CCAssignFn Fn) {
57 unsigned NumArgs = TheArgs->getNumValues()-1;
58
59 for (unsigned i = 0; i != NumArgs; ++i) {
60 MVT::ValueType ArgVT = TheArgs->getValueType(i);
61 SDOperand FlagOp = TheArgs->getOperand(3+i);
62 unsigned ArgFlags = cast<ConstantSDNode>(FlagOp)->getValue();
63 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
64 cerr << "Formal argument #" << i << " has unhandled type "
65 << MVT::getValueTypeString(ArgVT) << "\n";
66 abort();
67 }
68 }
69}
70
71/// AnalyzeReturn - Analyze the returned values of an ISD::RET node,
72/// incorporating info about the result values into this state.
73void CCState::AnalyzeReturn(SDNode *TheRet, CCAssignFn Fn) {
74 // Determine which register each value should be copied into.
75 for (unsigned i = 0, e = TheRet->getNumOperands() / 2; i != e; ++i) {
76 MVT::ValueType VT = TheRet->getOperand(i*2+1).getValueType();
77 if (Fn(i, VT, VT, CCValAssign::Full,
78 cast<ConstantSDNode>(TheRet->getOperand(i*2+2))->getValue(), *this)){
79 cerr << "Return operand #" << i << " has unhandled type "
80 << MVT::getValueTypeString(VT) << "\n";
81 abort();
82 }
83 }
84}
85
86
Chris Lattnerfb39f992007-02-28 06:56:37 +000087/// AnalyzeCallOperands - Analyze an ISD::CALL node, incorporating info
88/// about the passed values into this state.
89void CCState::AnalyzeCallOperands(SDNode *TheCall, CCAssignFn Fn) {
90 unsigned NumOps = (TheCall->getNumOperands() - 5) / 2;
91 for (unsigned i = 0; i != NumOps; ++i) {
92 MVT::ValueType ArgVT = TheCall->getOperand(5+2*i).getValueType();
93 SDOperand FlagOp = TheCall->getOperand(5+2*i+1);
94 unsigned ArgFlags =cast<ConstantSDNode>(FlagOp)->getValue();
95 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
96 cerr << "Call operand #" << i << " has unhandled type "
97 << MVT::getValueTypeString(ArgVT) << "\n";
98 abort();
99 }
100 }
101}
102
Chris Lattner66baf262007-02-28 07:09:40 +0000103/// AnalyzeCallResult - Analyze the return values of an ISD::CALL node,
104/// incorporating info about the passed values into this state.
105void CCState::AnalyzeCallResult(SDNode *TheCall, CCAssignFn Fn) {
106 for (unsigned i = 0, e = TheCall->getNumValues() - 1; i != e; ++i) {
107 MVT::ValueType VT = TheCall->getValueType(i);
108 if (Fn(i, VT, VT, CCValAssign::Full, 0, *this)) {
109 cerr << "Call result #" << i << " has unhandled type "
110 << MVT::getValueTypeString(VT) << "\n";
Chris Lattnerfb39f992007-02-28 06:56:37 +0000111 abort();
112 }
113 }
114}