blob: b8bc484e3bcd1537baecdec72ac15910d986abf8 [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"
Chris Lattner13513b72007-02-27 05:13:54 +000018#include "llvm/Target/TargetMachine.h"
Chris Lattner362e98a2007-02-27 04:43:02 +000019using namespace llvm;
20
Chris Lattneraeeccfc2007-06-19 00:11:09 +000021CCState::CCState(unsigned CC, bool isVarArg, const TargetMachine &tm,
Chris Lattner13513b72007-02-27 05:13:54 +000022 SmallVector<CCValAssign, 16> &locs)
Chris Lattneraeeccfc2007-06-19 00:11:09 +000023 : CallingConv(CC), IsVarArg(isVarArg), TM(tm),
24 MRI(*TM.getRegisterInfo()), Locs(locs) {
Chris Lattner362e98a2007-02-27 04:43:02 +000025 // No stack is used.
26 StackOffset = 0;
27
28 UsedRegs.resize(MRI.getNumRegs());
29}
30
Rafael Espindola594d37e2007-08-10 14:44:42 +000031void CCState::HandleStruct(unsigned ValNo, MVT::ValueType ValVT,
32 MVT::ValueType LocVT, CCValAssign::LocInfo LocInfo,
33 unsigned ArgFlags) {
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);
39
40 addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
41}
Chris Lattner362e98a2007-02-27 04:43:02 +000042
43/// MarkAllocated - Mark a register and all of its aliases as allocated.
44void CCState::MarkAllocated(unsigned Reg) {
45 UsedRegs[Reg/32] |= 1 << (Reg&31);
46
47 if (const unsigned *RegAliases = MRI.getAliasSet(Reg))
48 for (; (Reg = *RegAliases); ++RegAliases)
49 UsedRegs[Reg/32] |= 1 << (Reg&31);
50}
Chris Lattnerfb39f992007-02-28 06:56:37 +000051
Chris Lattner66baf262007-02-28 07:09:40 +000052/// AnalyzeFormalArguments - Analyze an ISD::FORMAL_ARGUMENTS node,
53/// incorporating info about the formals into this state.
54void CCState::AnalyzeFormalArguments(SDNode *TheArgs, CCAssignFn Fn) {
55 unsigned NumArgs = TheArgs->getNumValues()-1;
56
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";
64 abort();
65 }
66 }
67}
68
69/// AnalyzeReturn - Analyze the returned values of an ISD::RET node,
70/// incorporating info about the result values into this state.
71void 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";
79 abort();
80 }
81 }
82}
83
84
Chris Lattnerfb39f992007-02-28 06:56:37 +000085/// AnalyzeCallOperands - Analyze an ISD::CALL node, incorporating info
86/// about the passed values into this state.
87void 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";
96 abort();
97 }
98 }
99}
100
Chris Lattner66baf262007-02-28 07:09:40 +0000101/// AnalyzeCallResult - Analyze the return values of an ISD::CALL node,
102/// incorporating info about the passed values into this state.
103void 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";
Chris Lattnerfb39f992007-02-28 06:56:37 +0000109 abort();
110 }
111 }
112}