Implement support for varargs functions without any fixed

parameters in the CBE by implicitly adding a fixed argument.
This allows eliminating a work-around from DAE.  Patch by
Sylvere Teissier!


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@100944 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2010-04-10 19:12:44 +00:00
parent 195d2dec10
commit 5106dcd078
2 changed files with 23 additions and 31 deletions

View File

@ -473,8 +473,9 @@ void CWriter::printStructReturnPointerFunctionType(raw_ostream &Out,
PrintedType = true; PrintedType = true;
} }
if (FTy->isVarArg()) { if (FTy->isVarArg()) {
if (PrintedType) if (!PrintedType)
FunctionInnards << ", ..."; FunctionInnards << " int"; //dummy argument for empty vararg functs
FunctionInnards << ", ...";
} else if (!PrintedType) { } else if (!PrintedType) {
FunctionInnards << "void"; FunctionInnards << "void";
} }
@ -568,8 +569,9 @@ raw_ostream &CWriter::printType(raw_ostream &Out, const Type *Ty,
++Idx; ++Idx;
} }
if (FTy->isVarArg()) { if (FTy->isVarArg()) {
if (FTy->getNumParams()) if (!FTy->getNumParams())
FunctionInnards << ", ..."; FunctionInnards << " int"; //dummy argument for empty vaarg functs
FunctionInnards << ", ...";
} else if (!FTy->getNumParams()) { } else if (!FTy->getNumParams()) {
FunctionInnards << "void"; FunctionInnards << "void";
} }
@ -2237,12 +2239,16 @@ void CWriter::printFunctionSignature(const Function *F, bool Prototype) {
} }
} }
if (!PrintedArg && FT->isVarArg()) {
FunctionInnards << "int vararg_dummy_arg";
PrintedArg = true;
}
// Finish printing arguments... if this is a vararg function, print the ..., // Finish printing arguments... if this is a vararg function, print the ...,
// unless there are no known types, in which case, we just emit (). // unless there are no known types, in which case, we just emit ().
// //
if (FT->isVarArg() && PrintedArg) { if (FT->isVarArg() && PrintedArg) {
if (PrintedArg) FunctionInnards << ", "; FunctionInnards << ",..."; // Output varargs portion of signature!
FunctionInnards << "..."; // Output varargs portion of signature!
} else if (!FT->isVarArg() && !PrintedArg) { } else if (!FT->isVarArg() && !PrintedArg) {
FunctionInnards << "void"; // ret() -> ret(void) in C. FunctionInnards << "void"; // ret() -> ret(void) in C.
} }
@ -2928,6 +2934,12 @@ void CWriter::visitCallInst(CallInst &I) {
Out << '('; Out << '(';
bool PrintedArg = false;
if(FTy->isVarArg() && !FTy->getNumParams()) {
Out << "0 /*dummy arg*/";
PrintedArg = true;
}
unsigned NumDeclaredParams = FTy->getNumParams(); unsigned NumDeclaredParams = FTy->getNumParams();
CallSite::arg_iterator AI = I.op_begin()+1, AE = I.op_end(); CallSite::arg_iterator AI = I.op_begin()+1, AE = I.op_end();
@ -2937,7 +2949,7 @@ void CWriter::visitCallInst(CallInst &I) {
++ArgNo; ++ArgNo;
} }
bool PrintedArg = false;
for (; AI != AE; ++AI, ++ArgNo) { for (; AI != AE; ++AI, ++ArgNo) {
if (PrintedArg) Out << ", "; if (PrintedArg) Out << ", ";
if (ArgNo < NumDeclaredParams && if (ArgNo < NumDeclaredParams &&
@ -2987,15 +2999,10 @@ bool CWriter::visitBuiltinCall(CallInst &I, Intrinsic::ID ID,
writeOperand(I.getOperand(1)); writeOperand(I.getOperand(1));
Out << ", "; Out << ", ";
// Output the last argument to the enclosing function. // Output the last argument to the enclosing function.
if (I.getParent()->getParent()->arg_empty()) { if (I.getParent()->getParent()->arg_empty())
std::string msg; Out << "vararg_dummy_arg";
raw_string_ostream Msg(msg); else
Msg << "The C backend does not currently support zero " writeOperand(--I.getParent()->getParent()->arg_end());
<< "argument varargs functions, such as '"
<< I.getParent()->getParent()->getName() << "'!";
report_fatal_error(Msg.str());
}
writeOperand(--I.getParent()->getParent()->arg_end());
Out << ')'; Out << ')';
return true; return true;
case Intrinsic::vaend: case Intrinsic::vaend:

View File

@ -694,18 +694,6 @@ bool DAE::RemoveDeadStuffFromFunction(Function *F) {
AttrListPtr NewPAL = AttrListPtr::get(AttributesVec.begin(), AttrListPtr NewPAL = AttrListPtr::get(AttributesVec.begin(),
AttributesVec.end()); AttributesVec.end());
// Work around LLVM bug PR56: the CWriter cannot emit varargs functions which
// have zero fixed arguments.
//
// Note that we apply this hack for a vararg fuction that does not have any
// arguments anymore, but did have them before (so don't bother fixing
// functions that were already broken wrt CWriter).
bool ExtraArgHack = false;
if (Params.empty() && FTy->isVarArg() && FTy->getNumParams() != 0) {
ExtraArgHack = true;
Params.push_back(Type::getInt32Ty(F->getContext()));
}
// Create the new function type based on the recomputed parameters. // Create the new function type based on the recomputed parameters.
FunctionType *NFTy = FunctionType::get(NRetTy, Params, FTy->isVarArg()); FunctionType *NFTy = FunctionType::get(NRetTy, Params, FTy->isVarArg());
@ -755,9 +743,6 @@ bool DAE::RemoveDeadStuffFromFunction(Function *F) {
AttributesVec.push_back(AttributeWithIndex::get(Args.size(), Attrs)); AttributesVec.push_back(AttributeWithIndex::get(Args.size(), Attrs));
} }
if (ExtraArgHack)
Args.push_back(UndefValue::get(Type::getInt32Ty(F->getContext())));
// Push any varargs arguments on the list. Don't forget their attributes. // Push any varargs arguments on the list. Don't forget their attributes.
for (CallSite::arg_iterator E = CS.arg_end(); I != E; ++I, ++i) { for (CallSite::arg_iterator E = CS.arg_end(); I != E; ++I, ++i) {
Args.push_back(*I); Args.push_back(*I);