Skip to content

Commit

Permalink
Clang support for __attribute__((swapstack))
Browse files Browse the repository at this point in the history
  • Loading branch information
stedolan committed Aug 3, 2011
1 parent a783142 commit dbaa24d
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 3 deletions.
4 changes: 3 additions & 1 deletion include/clang/AST/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ enum CallingConv {
CC_X86FastCall, // __attribute__((fastcall))
CC_X86ThisCall, // __attribute__((thiscall))
CC_X86Pascal, // __attribute__((pascal))
CC_SwapStack, // __attribute__((swapstack))
CC_AAPCS, // __attribute__((pcs("aapcs")))
CC_AAPCS_VFP // __attribute__((pcs("aapcs-vfp")))
};
Expand Down Expand Up @@ -3202,7 +3203,8 @@ class AttributedType : public Type, public llvm::FoldingSetNode {
attr_fastcall,
attr_stdcall,
attr_thiscall,
attr_pascal
attr_pascal,
attr_swapstack
};

private:
Expand Down
4 changes: 4 additions & 0 deletions include/clang/Basic/Attr.td
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,10 @@ def ThisCall : InheritableAttr {
let Spellings = ["thiscall", "__thiscall"];
}

def SwapStack : InheritableAttr {
let Spellings = ["swapstack"];
}

def Pascal : InheritableAttr {
let Spellings = ["pascal", "__pascal"];
}
Expand Down
3 changes: 3 additions & 0 deletions include/clang/Basic/Builtins.def
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,9 @@ BUILTIN(__noop, "v.", "n")
BUILTIN(__debugbreak, "v", "n")


// NewStack
BUILTIN(__builtin_newstack, "v*v*iv*", "n")

// C99 library functions
// C99 stdlib.h
LIBBUILTIN(abort, "v", "fr", "stdlib.h", ALL_LANGUAGES)
Expand Down
1 change: 1 addition & 0 deletions include/clang/Sema/AttributeList.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ class AttributeList { // TODO: This should really be called ParsedAttribute
AT_shared,
AT_stdcall,
AT_thiscall,
AT_swapstack, // Clang-specific
AT_transparent_union,
AT_unavailable,
AT_unused,
Expand Down
1 change: 1 addition & 0 deletions lib/AST/DumpXML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,7 @@ struct XMLDumper : public XMLDeclVisitor<XMLDumper>,
case CC_X86StdCall: return set("cc", "x86_stdcall");
case CC_X86ThisCall: return set("cc", "x86_thiscall");
case CC_X86Pascal: return set("cc", "x86_pascal");
case CC_SwapStack: return set("cc", "swapstack");
case CC_AAPCS: return set("cc", "aapcs");
case CC_AAPCS_VFP: return set("cc", "aapcs_vfp");
}
Expand Down
1 change: 1 addition & 0 deletions lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1492,6 +1492,7 @@ StringRef FunctionType::getNameForCallConv(CallingConv CC) {
case CC_X86FastCall: return "fastcall";
case CC_X86ThisCall: return "thiscall";
case CC_X86Pascal: return "pascal";
case CC_SwapStack: return "swapstack";
case CC_AAPCS: return "aapcs";
case CC_AAPCS_VFP: return "aapcs-vfp";
}
Expand Down
4 changes: 4 additions & 0 deletions lib/AST/TypePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,9 @@ void TypePrinter::printFunctionProto(const FunctionProtoType *T,
case CC_X86Pascal:
S += " __attribute__((pascal))";
break;
case CC_SwapStack:
S += " __attribute__((swapstack))";
break;
case CC_AAPCS:
S += " __attribute__((pcs(\"aapcs\")))";
break;
Expand Down Expand Up @@ -934,6 +937,7 @@ void TypePrinter::printAttributed(const AttributedType *T,
case AttributedType::attr_stdcall: S += "stdcall"; break;
case AttributedType::attr_thiscall: S += "thiscall"; break;
case AttributedType::attr_pascal: S += "pascal"; break;
case AttributedType::attr_swapstack: S += "swapstack"; break;
case AttributedType::attr_pcs: {
S += "pcs(";
QualType t = T->getEquivalentType();
Expand Down
11 changes: 11 additions & 0 deletions lib/CodeGen/CGBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,17 @@ RValue CodeGenFunction::EmitBuiltinExpr(const FunctionDecl *FD,

return RValue::get(0);
}
case Builtin::BI__builtin_newstack: {
Value* Mem = Builder.CreateBitCast(EmitScalarExpr(E->getArg(0)),
Int8PtrTy);
Value* Len = Builder.CreateIntCast(EmitScalarExpr(E->getArg(1)),
Int32Ty, false, "tmp");
Value* Func = Builder.CreateBitCast(EmitScalarExpr(E->getArg(2)),
Int8PtrTy);

Value* f = CGM.getIntrinsic(Intrinsic::newstack);
return RValue::get(Builder.CreateCall3(f, Mem, Len, Func));
}
case Builtin::BI__sync_fetch_and_add:
case Builtin::BI__sync_fetch_and_sub:
case Builtin::BI__sync_fetch_and_or:
Expand Down
21 changes: 19 additions & 2 deletions lib/CodeGen/CGCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ static unsigned ClangCallConvToLLVMCallConv(CallingConv CC) {
case CC_X86StdCall: return llvm::CallingConv::X86_StdCall;
case CC_X86FastCall: return llvm::CallingConv::X86_FastCall;
case CC_X86ThisCall: return llvm::CallingConv::X86_ThisCall;
case CC_SwapStack: return llvm::CallingConv::SwapStack;
case CC_AAPCS: return llvm::CallingConv::ARM_AAPCS;
case CC_AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
// TODO: add support for CC_X86Pascal to llvm
Expand Down Expand Up @@ -105,6 +106,9 @@ static CallingConv getCallingConventionForDecl(const Decl *D) {
if (D->hasAttr<PascalAttr>())
return CC_X86Pascal;

if (D->hasAttr<SwapStackAttr>())
return CC_SwapStack;

if (PcsAttr *PCS = D->getAttr<PcsAttr>())
return (PCS->getPCS() == PcsAttr::AAPCS ? CC_AAPCS : CC_AAPCS_VFP);

Expand Down Expand Up @@ -268,8 +272,21 @@ const CGFunctionInfo &CodeGenTypes::getFunctionInfo(CanQualType ResTy,
bool Inserted = FunctionsBeingProcessed.insert(FI); (void)Inserted;
assert(Inserted && "Recursively being processed?");

// Compute ABI information.
getABIInfo().computeInfo(*FI);
if (Info.getCC() == CC_SwapStack){
QualType RetTy = FI->getReturnType();
if (RetTy->isVoidType())
FI->getReturnInfo() = ABIArgInfo::getIgnore();
else
FI->getReturnInfo() = ABIArgInfo::getDirect();

for (CGFunctionInfo::arg_iterator it = FI->arg_begin(), ie = FI->arg_end();
it != ie; ++it)
it->info = ABIArgInfo::getDirect();
}else{
// Compute ABI information.
getABIInfo().computeInfo(*FI);
}


// Loop over all of the computed argument and return value info. If any of
// them are direct or extend without a specified coerce type, specify the
Expand Down
1 change: 1 addition & 0 deletions lib/Sema/AttributeList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ AttributeList::Kind AttributeList::getKind(const IdentifierInfo *Name) {
.Case("thiscall", AT_thiscall)
.Case("bounded", IgnoredAttribute) // OpenBSD
.Case("pascal", AT_pascal)
.Case("swapstack", AT_swapstack)
.Case("__cdecl", AT_cdecl)
.Case("__stdcall", AT_stdcall)
.Case("__fastcall", AT_fastcall)
Expand Down
5 changes: 5 additions & 0 deletions lib/Sema/SemaDeclAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2461,6 +2461,9 @@ static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
case AttributeList::AT_pascal:
D->addAttr(::new (S.Context) PascalAttr(Attr.getLoc(), S.Context));
return;
case AttributeList::AT_swapstack:
D->addAttr(::new (S.Context) SwapStackAttr(Attr.getLoc(), S.Context));
return;
case AttributeList::AT_pcs: {
Expr *Arg = Attr.getArg(0);
StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Expand Down Expand Up @@ -2516,6 +2519,7 @@ bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) {
case AttributeList::AT_stdcall: CC = CC_X86StdCall; break;
case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break;
case AttributeList::AT_pascal: CC = CC_X86Pascal; break;
case AttributeList::AT_swapstack: CC = CC_SwapStack; break;
case AttributeList::AT_pcs: {
Expr *Arg = attr.getArg(0);
StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Expand Down Expand Up @@ -3062,6 +3066,7 @@ static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
case AttributeList::AT_fastcall:
case AttributeList::AT_thiscall:
case AttributeList::AT_pascal:
case AttributeList::AT_swapstack:
case AttributeList::AT_pcs:
handleCallConvAttr(S, D, Attr);
break;
Expand Down
3 changes: 3 additions & 0 deletions lib/Sema/SemaType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ static void diagnoseBadTypeAttribute(Sema &S, const AttributeList &attr,
case AttributeList::AT_stdcall: \
case AttributeList::AT_thiscall: \
case AttributeList::AT_pascal: \
case AttributeList::AT_swapstack: \
case AttributeList::AT_regparm: \
case AttributeList::AT_pcs \

Expand Down Expand Up @@ -2651,6 +2652,8 @@ static AttributeList::Kind getAttrListKind(AttributedType::Kind kind) {
return AttributeList::AT_thiscall;
case AttributedType::attr_pascal:
return AttributeList::AT_pascal;
case AttributedType::attr_swapstack:
return AttributeList::AT_swapstack;
case AttributedType::attr_pcs:
return AttributeList::AT_pcs;
}
Expand Down

0 comments on commit dbaa24d

Please sign in to comment.