LLVM API Documentation

CallingConvLower.cpp

Go to the documentation of this file.
00001 //===-- llvm/CallingConvLower.cpp - Calling Conventions -------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the CCState class, used for lowering and implementing
00011 // calling conventions.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "llvm/CodeGen/CallingConvLower.h"
00016 #include "llvm/Target/TargetRegisterInfo.h"
00017 #include "llvm/Target/TargetData.h"
00018 #include "llvm/Target/TargetMachine.h"
00019 using namespace llvm;
00020 
00021 CCState::CCState(unsigned CC, bool isVarArg, const TargetMachine &tm,
00022                  SmallVector<CCValAssign, 16> &locs)
00023   : CallingConv(CC), IsVarArg(isVarArg), TM(tm),
00024     TRI(*TM.getRegisterInfo()), Locs(locs) {
00025   // No stack is used.
00026   StackOffset = 0;
00027   
00028   UsedRegs.resize((TRI.getNumRegs()+31)/32);
00029 }
00030 
00031 // HandleByVal - Allocate a stack slot large enough to pass an argument by
00032 // value. The size and alignment information of the argument is encoded in its
00033 // parameter attribute.
00034 void CCState::HandleByVal(unsigned ValNo, MVT ValVT,
00035                           MVT LocVT, CCValAssign::LocInfo LocInfo,
00036                           int MinSize, int MinAlign,
00037                           ISD::ArgFlagsTy ArgFlags) {
00038   unsigned Align = ArgFlags.getByValAlign();
00039   unsigned Size  = ArgFlags.getByValSize();
00040   if (MinSize > (int)Size)
00041     Size = MinSize;
00042   if (MinAlign > (int)Align)
00043     Align = MinAlign;
00044   unsigned Offset = AllocateStack(Size, Align);
00045 
00046   addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
00047 }
00048 
00049 /// MarkAllocated - Mark a register and all of its aliases as allocated.
00050 void CCState::MarkAllocated(unsigned Reg) {
00051   UsedRegs[Reg/32] |= 1 << (Reg&31);
00052   
00053   if (const unsigned *RegAliases = TRI.getAliasSet(Reg))
00054     for (; (Reg = *RegAliases); ++RegAliases)
00055       UsedRegs[Reg/32] |= 1 << (Reg&31);
00056 }
00057 
00058 /// AnalyzeFormalArguments - Analyze an ISD::FORMAL_ARGUMENTS node,
00059 /// incorporating info about the formals into this state.
00060 void CCState::AnalyzeFormalArguments(SDNode *TheArgs, CCAssignFn Fn) {
00061   unsigned NumArgs = TheArgs->getNumValues()-1;
00062   
00063   for (unsigned i = 0; i != NumArgs; ++i) {
00064     MVT ArgVT = TheArgs->getValueType(i);
00065     ISD::ArgFlagsTy ArgFlags =
00066       cast<ARG_FLAGSSDNode>(TheArgs->getOperand(3+i))->getArgFlags();
00067     if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
00068       cerr << "Formal argument #" << i << " has unhandled type "
00069            << ArgVT.getMVTString() << "\n";
00070       abort();
00071     }
00072   }
00073 }
00074 
00075 /// AnalyzeReturn - Analyze the returned values of an ISD::RET node,
00076 /// incorporating info about the result values into this state.
00077 void CCState::AnalyzeReturn(SDNode *TheRet, CCAssignFn Fn) {
00078   // Determine which register each value should be copied into.
00079   for (unsigned i = 0, e = TheRet->getNumOperands() / 2; i != e; ++i) {
00080     MVT VT = TheRet->getOperand(i*2+1).getValueType();
00081     ISD::ArgFlagsTy ArgFlags =
00082       cast<ARG_FLAGSSDNode>(TheRet->getOperand(i*2+2))->getArgFlags();
00083     if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this)){
00084       cerr << "Return operand #" << i << " has unhandled type "
00085            << VT.getMVTString() << "\n";
00086       abort();
00087     }
00088   }
00089 }
00090 
00091 
00092 /// AnalyzeCallOperands - Analyze an ISD::CALL node, incorporating info
00093 /// about the passed values into this state.
00094 void CCState::AnalyzeCallOperands(CallSDNode *TheCall, CCAssignFn Fn) {
00095   unsigned NumOps = TheCall->getNumArgs();
00096   for (unsigned i = 0; i != NumOps; ++i) {
00097     MVT ArgVT = TheCall->getArg(i).getValueType();
00098     ISD::ArgFlagsTy ArgFlags = TheCall->getArgFlags(i);
00099     if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
00100       cerr << "Call operand #" << i << " has unhandled type "
00101            << ArgVT.getMVTString() << "\n";
00102       abort();
00103     }
00104   }
00105 }
00106 
00107 /// AnalyzeCallOperands - Same as above except it takes vectors of types
00108 /// and argument flags.
00109 void CCState::AnalyzeCallOperands(SmallVectorImpl<MVT> &ArgVTs,
00110                                   SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
00111                                   CCAssignFn Fn) {
00112   unsigned NumOps = ArgVTs.size();
00113   for (unsigned i = 0; i != NumOps; ++i) {
00114     MVT ArgVT = ArgVTs[i];
00115     ISD::ArgFlagsTy ArgFlags = Flags[i];
00116     if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
00117       cerr << "Call operand #" << i << " has unhandled type "
00118            << ArgVT.getMVTString() << "\n";
00119       abort();
00120     }
00121   }
00122 }
00123 
00124 /// AnalyzeCallResult - Analyze the return values of an ISD::CALL node,
00125 /// incorporating info about the passed values into this state.
00126 void CCState::AnalyzeCallResult(CallSDNode *TheCall, CCAssignFn Fn) {
00127   for (unsigned i = 0, e = TheCall->getNumRetVals(); i != e; ++i) {
00128     MVT VT = TheCall->getRetValType(i);
00129     ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
00130     if (TheCall->isInreg())
00131       Flags.setInReg();
00132     if (Fn(i, VT, VT, CCValAssign::Full, Flags, *this)) {
00133       cerr << "Call result #" << i << " has unhandled type "
00134            << VT.getMVTString() << "\n";
00135       abort();
00136     }
00137   }
00138 }
00139 
00140 /// AnalyzeCallResult - Same as above except it's specialized for calls which
00141 /// produce a single value.
00142 void CCState::AnalyzeCallResult(MVT VT, CCAssignFn Fn) {
00143   if (Fn(0, VT, VT, CCValAssign::Full, ISD::ArgFlagsTy(), *this)) {
00144     cerr << "Call result has unhandled type "
00145          << VT.getMVTString() << "\n";
00146     abort();
00147   }
00148 }