Skip to content

Commit fd4f962

Browse files
committed
[Clang][M68k] Add Clang support for the new M68k_RTD CC
This patch adds `CC_M68kRTD`, which will be used on function if either `__attribute__((m68k_rtd))` is presented or `-mrtd` flag is given. Differential Revision: https://reviews.llvm.org/D149867
1 parent 42b707e commit fd4f962

File tree

25 files changed

+200
-31
lines changed

25 files changed

+200
-31
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ Modified Compiler Flags
205205
* ``-frewrite-includes`` now guards the original #include directives with
206206
``__CLANG_REWRITTEN_INCLUDES``, and ``__CLANG_REWRITTEN_SYSTEM_INCLUDES`` as
207207
appropriate.
208+
* Introducing a new default calling convention for ``-fdefault-calling-conv``:
209+
``rtdcall``. This new default CC only works for M68k and will use the new
210+
``m68k_rtdcc`` CC on every functions that are not variadic. The ``-mrtd``
211+
driver/frontend flag has the same effect when targeting M68k.
208212

209213
Removed Compiler Flags
210214
-------------------------

clang/include/clang-c/Index.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2980,6 +2980,7 @@ enum CXCallingConv {
29802980
CXCallingConv_AArch64VectorCall = 16,
29812981
CXCallingConv_SwiftAsync = 17,
29822982
CXCallingConv_AArch64SVEPCS = 18,
2983+
CXCallingConv_M68kRTD = 19,
29832984

29842985
CXCallingConv_Invalid = 100,
29852986
CXCallingConv_Unexposed = 200

clang/include/clang/Basic/Attr.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2805,6 +2805,11 @@ def PreserveAll : DeclOrTypeAttr {
28052805
let Documentation = [PreserveAllDocs];
28062806
}
28072807

2808+
def M68kRTD: DeclOrTypeAttr {
2809+
let Spellings = [Clang<"m68k_rtd">];
2810+
let Documentation = [M68kRTDDocs];
2811+
}
2812+
28082813
def Target : InheritableAttr {
28092814
let Spellings = [GCC<"target">];
28102815
let Args = [StringArgument<"featuresStr">];

clang/include/clang/Basic/AttrDocs.td

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2825,6 +2825,18 @@ See the documentation for `__vectorcall`_ on MSDN for more details.
28252825
}];
28262826
}
28272827

2828+
def M68kRTDDocs : Documentation {
2829+
let Category = DocCatCallingConvs;
2830+
let Content = [{
2831+
On M68k targets, this attribute changes the calling convention of a function
2832+
to clear parameters off the stack on return. In other words, callee is
2833+
responsible for cleaning out the stack space allocated for incoming paramters.
2834+
This convention does not support variadic calls or unprototyped functions in C.
2835+
When targeting M68010 or newer CPUs, this calling convention is implemented
2836+
using the `rtd` instruction.
2837+
}];
2838+
}
2839+
28282840
def DocCatConsumed : DocumentationCategory<"Consumed Annotation Checking"> {
28292841
let Content = [{
28302842
Clang supports additional attributes for checking basic resource management

clang/include/clang/Basic/LangOptions.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ class LangOptions : public LangOptionsBase {
134134
DCC_FastCall,
135135
DCC_StdCall,
136136
DCC_VectorCall,
137-
DCC_RegCall
137+
DCC_RegCall,
138+
DCC_RtdCall
138139
};
139140

140141
enum AddrSpaceMapMangling { ASMM_Target, ASMM_On, ASMM_Off };

clang/include/clang/Basic/Specifiers.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ namespace clang {
288288
CC_AArch64VectorCall, // __attribute__((aarch64_vector_pcs))
289289
CC_AArch64SVEPCS, // __attribute__((aarch64_sve_pcs))
290290
CC_AMDGPUKernelCall, // __attribute__((amdgpu_kernel))
291+
CC_M68kRTD, // __attribute__((m68k_rtd))
291292
};
292293

293294
/// Checks whether the given calling convention supports variadic
@@ -304,6 +305,7 @@ namespace clang {
304305
case CC_OpenCLKernel:
305306
case CC_Swift:
306307
case CC_SwiftAsync:
308+
case CC_M68kRTD:
307309
return false;
308310
default:
309311
return true;

clang/include/clang/Driver/Options.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7478,9 +7478,9 @@ def fnative_half_arguments_and_returns : Flag<["-"], "fnative-half-arguments-and
74787478
ImpliedByAnyOf<[open_cl.KeyPath, render_script.KeyPath, hlsl.KeyPath]>;
74797479
def fdefault_calling_conv_EQ : Joined<["-"], "fdefault-calling-conv=">,
74807480
HelpText<"Set default calling convention">,
7481-
Values<"cdecl,fastcall,stdcall,vectorcall,regcall">,
7481+
Values<"cdecl,fastcall,stdcall,vectorcall,regcall,rtdcall">,
74827482
NormalizedValuesScope<"LangOptions">,
7483-
NormalizedValues<["DCC_CDecl", "DCC_FastCall", "DCC_StdCall", "DCC_VectorCall", "DCC_RegCall"]>,
7483+
NormalizedValues<["DCC_CDecl", "DCC_FastCall", "DCC_StdCall", "DCC_VectorCall", "DCC_RegCall", "DCC_RtdCall"]>,
74847484
MarshallingInfoEnum<LangOpts<"DefaultCallingConv">, "DCC_None">;
74857485

74867486
// These options cannot be marshalled, because they are used to set up the LangOptions defaults.

clang/lib/AST/ASTContext.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12024,6 +12024,10 @@ CallingConv ASTContext::getDefaultCallingConvention(bool IsVariadic,
1202412024
if (!IsVariadic)
1202512025
return CC_X86RegCall;
1202612026
break;
12027+
case LangOptions::DCC_RtdCall:
12028+
if (!IsVariadic)
12029+
return CC_M68kRTD;
12030+
break;
1202712031
}
1202812032
}
1202912033
return Target->getDefaultCallingConv();

clang/lib/AST/ItaniumMangle.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3429,6 +3429,7 @@ StringRef CXXNameMangler::getCallingConvQualifierName(CallingConv CC) {
34293429
case CC_OpenCLKernel:
34303430
case CC_PreserveMost:
34313431
case CC_PreserveAll:
3432+
case CC_M68kRTD:
34323433
// FIXME: we should be mangling all of the above.
34333434
return "";
34343435

clang/lib/AST/Type.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3373,6 +3373,7 @@ StringRef FunctionType::getNameForCallConv(CallingConv CC) {
33733373
case CC_SwiftAsync: return "swiftasynccall";
33743374
case CC_PreserveMost: return "preserve_most";
33753375
case CC_PreserveAll: return "preserve_all";
3376+
case CC_M68kRTD: return "m68k_rtd";
33763377
}
33773378

33783379
llvm_unreachable("Invalid calling convention.");
@@ -3852,6 +3853,7 @@ bool AttributedType::isCallingConv() const {
38523853
case attr::IntelOclBicc:
38533854
case attr::PreserveMost:
38543855
case attr::PreserveAll:
3856+
case attr::M68kRTD:
38553857
return true;
38563858
}
38573859
llvm_unreachable("invalid attr kind");

clang/lib/AST/TypePrinter.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,6 +1044,9 @@ void TypePrinter::printFunctionAfter(const FunctionType::ExtInfo &Info,
10441044
case CC_PreserveAll:
10451045
OS << " __attribute__((preserve_all))";
10461046
break;
1047+
case CC_M68kRTD:
1048+
OS << " __attribute__((m68k_rtd))";
1049+
break;
10471050
}
10481051
}
10491052

@@ -1879,6 +1882,9 @@ void TypePrinter::printAttributedAfter(const AttributedType *T,
18791882
case attr::PreserveAll:
18801883
OS << "preserve_all";
18811884
break;
1885+
case attr::M68kRTD:
1886+
OS << "m68k_rtd";
1887+
break;
18821888
case attr::NoDeref:
18831889
OS << "noderef";
18841890
break;

clang/lib/Basic/Targets/M68k.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,5 +238,15 @@ TargetInfo::BuiltinVaListKind M68kTargetInfo::getBuiltinVaListKind() const {
238238
return TargetInfo::VoidPtrBuiltinVaList;
239239
}
240240

241+
TargetInfo::CallingConvCheckResult
242+
M68kTargetInfo::checkCallingConvention(CallingConv CC) const {
243+
switch (CC) {
244+
case CC_C:
245+
case CC_M68kRTD:
246+
return CCCR_OK;
247+
default:
248+
return TargetInfo::checkCallingConvention(CC);
249+
}
250+
}
241251
} // namespace targets
242252
} // namespace clang

clang/lib/Basic/Targets/M68k.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class LLVM_LIBRARY_VISIBILITY M68kTargetInfo : public TargetInfo {
5454
std::string_view getClobbers() const override;
5555
BuiltinVaListKind getBuiltinVaListKind() const override;
5656
bool setCPU(const std::string &Name) override;
57+
CallingConvCheckResult checkCallingConvention(CallingConv CC) const override;
5758
};
5859

5960
} // namespace targets

clang/lib/CodeGen/CGCall.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ unsigned CodeGenTypes::ClangCallConvToLLVMCallConv(CallingConv CC) {
7272
case CC_PreserveAll: return llvm::CallingConv::PreserveAll;
7373
case CC_Swift: return llvm::CallingConv::Swift;
7474
case CC_SwiftAsync: return llvm::CallingConv::SwiftTail;
75+
case CC_M68kRTD: return llvm::CallingConv::M68k_RTD;
7576
}
7677
}
7778

@@ -252,6 +253,9 @@ static CallingConv getCallingConventionForDecl(const ObjCMethodDecl *D,
252253
if (D->hasAttr<PreserveAllAttr>())
253254
return CC_PreserveAll;
254255

256+
if (D->hasAttr<M68kRTDAttr>())
257+
return CC_M68kRTD;
258+
255259
return CC_C;
256260
}
257261

clang/lib/CodeGen/CGDebugInfo.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,6 +1445,8 @@ static unsigned getDwarfCC(CallingConv CC) {
14451445
return llvm::dwarf::DW_CC_LLVM_PreserveAll;
14461446
case CC_X86RegCall:
14471447
return llvm::dwarf::DW_CC_LLVM_X86RegCall;
1448+
case CC_M68kRTD:
1449+
return llvm::dwarf::DW_CC_LLVM_M68kRTD;
14481450
}
14491451
return 0;
14501452
}

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5466,8 +5466,12 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
54665466
}
54675467
}
54685468

5469-
if (Args.hasFlag(options::OPT_mrtd, options::OPT_mno_rtd, false))
5470-
CmdArgs.push_back("-fdefault-calling-conv=stdcall");
5469+
if (Args.hasFlag(options::OPT_mrtd, options::OPT_mno_rtd, false)) {
5470+
if (Triple.getArch() == llvm::Triple::m68k)
5471+
CmdArgs.push_back("-fdefault-calling-conv=rtdcall");
5472+
else
5473+
CmdArgs.push_back("-fdefault-calling-conv=stdcall");
5474+
}
54715475

54725476
if (Args.hasArg(options::OPT_fenable_matrix)) {
54735477
// enable-matrix is needed by both the LangOpts and by LLVM.

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,7 @@ static bool FixupInvocation(CompilerInvocation &Invocation,
648648
emitError |= (DefaultCC == LangOptions::DCC_VectorCall ||
649649
DefaultCC == LangOptions::DCC_RegCall) &&
650650
!T.isX86();
651+
emitError |= DefaultCC == LangOptions::DCC_RtdCall && Arch != llvm::Triple::m68k;
651652
if (emitError)
652653
Diags.Report(diag::err_drv_argument_not_allowed_with)
653654
<< A->getSpelling() << T.getTriple();
@@ -3865,11 +3866,17 @@ bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args,
38653866
Diags.Report(diag::err_drv_argument_not_allowed_with)
38663867
<< A->getSpelling() << "-fdefault-calling-conv";
38673868
else {
3868-
if (T.getArch() != llvm::Triple::x86)
3869+
switch (T.getArch()) {
3870+
case llvm::Triple::x86:
3871+
Opts.setDefaultCallingConv(LangOptions::DCC_StdCall);
3872+
break;
3873+
case llvm::Triple::m68k:
3874+
Opts.setDefaultCallingConv(LangOptions::DCC_RtdCall);
3875+
break;
3876+
default:
38693877
Diags.Report(diag::err_drv_argument_not_allowed_with)
38703878
<< A->getSpelling() << T.getTriple();
3871-
else
3872-
Opts.setDefaultCallingConv(LangOptions::DCC_StdCall);
3879+
}
38733880
}
38743881
}
38753882

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5211,6 +5211,9 @@ static void handleCallConvAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
52115211
case ParsedAttr::AT_PreserveAll:
52125212
D->addAttr(::new (S.Context) PreserveAllAttr(S.Context, AL));
52135213
return;
5214+
case ParsedAttr::AT_M68kRTD:
5215+
D->addAttr(::new (S.Context) M68kRTDAttr(S.Context, AL));
5216+
return;
52145217
default:
52155218
llvm_unreachable("unexpected attribute kind");
52165219
}
@@ -5408,6 +5411,9 @@ bool Sema::CheckCallingConvAttr(const ParsedAttr &Attrs, CallingConv &CC,
54085411
case ParsedAttr::AT_PreserveAll:
54095412
CC = CC_PreserveAll;
54105413
break;
5414+
case ParsedAttr::AT_M68kRTD:
5415+
CC = CC_M68kRTD;
5416+
break;
54115417
default: llvm_unreachable("unexpected attribute kind");
54125418
}
54135419

@@ -9353,6 +9359,7 @@ ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, const ParsedAttr &AL,
93539359
case ParsedAttr::AT_AArch64VectorPcs:
93549360
case ParsedAttr::AT_AArch64SVEPcs:
93559361
case ParsedAttr::AT_AMDGPUKernelCall:
9362+
case ParsedAttr::AT_M68kRTD:
93569363
handleCallConvAttr(S, D, AL);
93579364
break;
93589365
case ParsedAttr::AT_Suppress:

clang/lib/Sema/SemaType.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ static void diagnoseBadTypeAttribute(Sema &S, const ParsedAttr &attr,
136136
case ParsedAttr::AT_Pcs: \
137137
case ParsedAttr::AT_IntelOclBicc: \
138138
case ParsedAttr::AT_PreserveMost: \
139-
case ParsedAttr::AT_PreserveAll
139+
case ParsedAttr::AT_PreserveAll: \
140+
case ParsedAttr::AT_M68kRTD
140141

141142
// Function type attributes.
142143
#define FUNCTION_TYPE_ATTRS_CASELIST \
@@ -7802,6 +7803,8 @@ static Attr *getCCTypeAttr(ASTContext &Ctx, ParsedAttr &Attr) {
78027803
return createSimpleAttr<PreserveMostAttr>(Ctx, Attr);
78037804
case ParsedAttr::AT_PreserveAll:
78047805
return createSimpleAttr<PreserveAllAttr>(Ctx, Attr);
7806+
case ParsedAttr::AT_M68kRTD:
7807+
return createSimpleAttr<M68kRTDAttr>(Ctx, Attr);
78057808
}
78067809
llvm_unreachable("unexpected attribute kind!");
78077810
}

clang/test/CodeGen/mrtd.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
1-
// RUN: %clang_cc1 -mrtd -triple i386-unknown-unknown -std=c89 -emit-llvm -o - %s 2>&1 | FileCheck %s
2-
3-
// CHECK: mrtd.c:10:3: warning: function with no prototype cannot use the stdcall calling convention
1+
// RUN: %clang_cc1 -mrtd -triple i386-unknown-unknown -std=c89 -emit-llvm -o - %s 2>&1 | FileCheck --check-prefixes=CHECK,X86 %s
2+
// RUN: %clang_cc1 -mrtd -triple m68k-unknown-unknown -std=c89 -emit-llvm -o - %s 2>&1 | FileCheck --check-prefixes=CHECK,M68K %s
43

54
void baz(int arg);
65

7-
// CHECK: define{{.*}} x86_stdcallcc void @foo(i32 noundef %arg) [[NUW:#[0-9]+]]
6+
// X86: define{{.*}} x86_stdcallcc void @foo(i32 noundef %arg) [[NUW:#[0-9]+]]
7+
// M68K: define{{.*}} m68k_rtdcc void @foo(i32 noundef %arg)
88
void foo(int arg) {
9-
// CHECK: call x86_stdcallcc i32 @bar(
9+
// X86: call x86_stdcallcc i32 @bar(
10+
#ifndef __mc68000__
1011
bar(arg);
11-
// CHECK: call x86_stdcallcc void @baz(i32
12+
#endif
13+
// X86: call x86_stdcallcc void @baz(i32
14+
// M68K: call m68k_rtdcc void @baz(i32
1215
baz(arg);
1316
}
1417

15-
// CHECK: declare x86_stdcallcc i32 @bar(...)
18+
// X86: declare x86_stdcallcc i32 @bar(...)
1619

17-
// CHECK: declare x86_stdcallcc void @baz(i32 noundef)
20+
// X86: declare x86_stdcallcc void @baz(i32 noundef)
21+
// M68K: declare m68k_rtdcc void @baz(i32 noundef)
1822

1923
void qux(int arg, ...) { }
2024
// CHECK: define{{.*}} void @qux(i32 noundef %arg, ...)
2125

2226
void quux(int a1, int a2, int a3) {
2327
qux(a1, a2, a3);
2428
}
25-
// CHECK-LABEL: define{{.*}} x86_stdcallcc void @quux
29+
// X86-LABEL: define{{.*}} x86_stdcallcc void @quux
30+
// M68K-LABEL: define{{.*}} m68k_rtdcc void @quux
2631
// CHECK: call void (i32, ...) @qux
2732

28-
// CHECK: attributes [[NUW]] = { noinline nounwind{{.*}} }
33+
// X86: attributes [[NUW]] = { noinline nounwind{{.*}} }

0 commit comments

Comments
 (0)