blob: 667870d4521bc9f8fb6bc297fec8718fdf8167e9 [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"
Dan Gohman6f0d0242008-02-10 18:45:23 +000017#include "llvm/Target/TargetRegisterInfo.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),
Dan Gohman6f0d0242008-02-10 18:45:23 +000025 TRI(*TM.getRegisterInfo()), Locs(locs) {
Chris Lattner362e98a2007-02-27 04:43:02 +000026 // No stack is used.
27 StackOffset = 0;
28
Dan Gohman6f0d0242008-02-10 18:45:23 +000029 UsedRegs.resize(TRI.getNumRegs());
Chris Lattner362e98a2007-02-27 04:43:02 +000030}
31
Evan Cheng4c46fc62008-01-15 07:49:36 +000032// 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.
35void CCState::HandleByVal(unsigned ValNo, MVT::ValueType ValVT,
36 MVT::ValueType LocVT, CCValAssign::LocInfo LocInfo,
37 int MinSize, int MinAlign,
Duncan Sands276dcbd2008-03-21 09:14:45 +000038 ISD::ArgFlagsTy ArgFlags) {
39 unsigned Align = ArgFlags.getByValAlign();
40 unsigned Size = ArgFlags.getByValSize();
Evan Cheng4c46fc62008-01-15 07:49:36 +000041 if (MinSize > (int)Size)
42 Size = MinSize;
43 if (MinAlign > (int)Align)
44 Align = MinAlign;
45 unsigned Offset = AllocateStack(Size, Align);
Rafael Espindola594d37e2007-08-10 14:44:42 +000046
47 addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
48}
Chris Lattner362e98a2007-02-27 04:43:02 +000049
50/// MarkAllocated - Mark a register and all of its aliases as allocated.
51void CCState::MarkAllocated(unsigned Reg) {
52 UsedRegs[Reg/32] |= 1 << (Reg&31);
53
Dan Gohman6f0d0242008-02-10 18:45:23 +000054 if (const unsigned *RegAliases = TRI.getAliasSet(Reg))
Chris Lattner362e98a2007-02-27 04:43:02 +000055 for (; (Reg = *RegAliases); ++RegAliases)
56 UsedRegs[Reg/32] |= 1 << (Reg&31);
57}
Chris Lattnerfb39f992007-02-28 06:56:37 +000058
Chris Lattner66baf262007-02-28 07:09:40 +000059/// AnalyzeFormalArguments - Analyze an ISD::FORMAL_ARGUMENTS node,
60/// incorporating info about the formals into this state.
61void CCState::AnalyzeFormalArguments(SDNode *TheArgs, CCAssignFn Fn) {
62 unsigned NumArgs = TheArgs->getNumValues()-1;
63
64 for (unsigned i = 0; i != NumArgs; ++i) {
65 MVT::ValueType ArgVT = TheArgs->getValueType(i);
Duncan Sands276dcbd2008-03-21 09:14:45 +000066 ISD::ArgFlagsTy ArgFlags =
67 cast<ARG_FLAGSSDNode>(TheArgs->getOperand(3+i))->getArgFlags();
Chris Lattner66baf262007-02-28 07:09:40 +000068 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
69 cerr << "Formal argument #" << i << " has unhandled type "
70 << MVT::getValueTypeString(ArgVT) << "\n";
71 abort();
72 }
73 }
74}
75
76/// AnalyzeReturn - Analyze the returned values of an ISD::RET node,
77/// incorporating info about the result values into this state.
78void 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();
Duncan Sands276dcbd2008-03-21 09:14:45 +000082 ISD::ArgFlagsTy ArgFlags =
83 cast<ARG_FLAGSSDNode>(TheRet->getOperand(i*2+2))->getArgFlags();
84 if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this)){
Chris Lattner66baf262007-02-28 07:09:40 +000085 cerr << "Return operand #" << i << " has unhandled type "
86 << MVT::getValueTypeString(VT) << "\n";
87 abort();
88 }
89 }
90}
91
92
Chris Lattnerfb39f992007-02-28 06:56:37 +000093/// AnalyzeCallOperands - Analyze an ISD::CALL node, incorporating info
94/// about the passed values into this state.
95void 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();
Duncan Sands276dcbd2008-03-21 09:14:45 +000099 ISD::ArgFlagsTy ArgFlags =
100 cast<ARG_FLAGSSDNode>(TheCall->getOperand(5+2*i+1))->getArgFlags();
Chris Lattnerfb39f992007-02-28 06:56:37 +0000101 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
102 cerr << "Call operand #" << i << " has unhandled type "
103 << MVT::getValueTypeString(ArgVT) << "\n";
104 abort();
105 }
106 }
107}
108
Chris Lattner66baf262007-02-28 07:09:40 +0000109/// AnalyzeCallResult - Analyze the return values of an ISD::CALL node,
110/// incorporating info about the passed values into this state.
111void 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);
Duncan Sands276dcbd2008-03-21 09:14:45 +0000114 if (Fn(i, VT, VT, CCValAssign::Full, ISD::ArgFlagsTy(), *this)) {
Chris Lattner66baf262007-02-28 07:09:40 +0000115 cerr << "Call result #" << i << " has unhandled type "
116 << MVT::getValueTypeString(VT) << "\n";
Chris Lattnerfb39f992007-02-28 06:56:37 +0000117 abort();
118 }
119 }
120}