diff --git a/clang/include/clang/Basic/LangOptions.def b/clang/include/clang/Basic/LangOptions.def --- a/clang/include/clang/Basic/LangOptions.def +++ b/clang/include/clang/Basic/LangOptions.def @@ -88,6 +88,7 @@ LANGOPT(C17 , 1, 0, "C17") LANGOPT(C2x , 1, 0, "C2x") LANGOPT(MSVCCompat , 1, 0, "Microsoft Visual C++ full compatibility mode") +LANGOPT(Kernel , 1, 0, "Kernel mode") LANGOPT(MicrosoftExt , 1, 0, "Microsoft C++ extensions") LANGOPT(AsmBlocks , 1, 0, "Microsoft inline asm blocks") LANGOPT(Borland , 1, 0, "Borland extensions") diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -2178,6 +2178,8 @@ NormalizedValues<["PPTMK_FullGeneralitySingleInheritance", "PPTMK_FullGeneralityMultipleInheritance", "PPTMK_FullGeneralityVirtualInheritance"]>, MarshallingInfoEnum, "PPTMK_BestCase">; +def fms_kernel : Flag<["-"], "fms-kernel">, Group, Flags<[CC1Option, NoDriverOption]>, + MarshallingInfoFlag>; // __declspec is enabled by default for the PS4 by the driver, and also // enabled for Microsoft Extensions or Borland Extensions, here. // diff --git a/clang/lib/Basic/Targets/OSTargets.cpp b/clang/lib/Basic/Targets/OSTargets.cpp --- a/clang/lib/Basic/Targets/OSTargets.cpp +++ b/clang/lib/Basic/Targets/OSTargets.cpp @@ -215,6 +215,9 @@ } } + if (Opts.Kernel) + Builder.defineMacro("_KERNEL_MODE"); + Builder.defineMacro("_INTEGRAL_MAX_BITS", "64"); Builder.defineMacro("__STDC_NO_THREADS__"); diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -38,6 +38,7 @@ #include "clang/Driver/SanitizerArgs.h" #include "clang/Driver/Types.h" #include "clang/Driver/XRayArgs.h" +#include "llvm/ADT/SmallSet.h" #include "llvm/ADT/StringExtras.h" #include "llvm/Config/llvm-config.h" #include "llvm/Option/ArgList.h" @@ -7458,6 +7459,12 @@ EH.NoUnwindC = true; } + if (Args.hasArg(options::OPT__SLASH_kernel)) { + EH.Synch = false; + EH.NoUnwindC = false; + EH.Asynch = false; + } + return EH; } @@ -7602,6 +7609,26 @@ CmdArgs.push_back("-fno-wchar"); } + if (Args.hasArg(options::OPT__SLASH_kernel)) { + llvm::Triple::ArchType Arch = getToolChain().getArch(); + std::vector Values = + Args.getAllArgValues(options::OPT__SLASH_arch); + if (!Values.empty()) { + llvm::SmallSet SupportedArches; + if (Arch == llvm::Triple::x86) { + std::vector Arches = {"IA32"}; + SupportedArches.insert(Arches.begin(), Arches.end()); + } + + for (auto &V : Values) + if (!SupportedArches.contains(V)) + D.Diag(diag::err_drv_argument_not_allowed_with) + << std::string("/arch:").append(V) << "/kernel"; + } + + CmdArgs.push_back("-fno-rtti"); + } + Arg *MostGeneralArg = Args.getLastArg(options::OPT__SLASH_vmg); Arg *BestCaseArg = Args.getLastArg(options::OPT__SLASH_vmb); if (MostGeneralArg && BestCaseArg) @@ -7671,6 +7698,9 @@ CmdArgs.push_back("msvc"); } + if (Args.hasArg(options::OPT__SLASH_kernel)) + CmdArgs.push_back("-fms-kernel"); + if (Arg *A = Args.getLastArg(options::OPT__SLASH_guard)) { StringRef GuardArgs = A->getValue(); // The only valid options are "cf", "cf,nochecks", "cf-", "ehcont" and diff --git a/clang/test/Driver/cl-zc.cpp b/clang/test/Driver/cl-zc.cpp --- a/clang/test/Driver/cl-zc.cpp +++ b/clang/test/Driver/cl-zc.cpp @@ -24,6 +24,28 @@ // RUN: %clang_cl /c /std:c++17 -### /Zc:alignedNew- -- %s 2>&1 | FileCheck -check-prefix=ALIGNED-NEW-OFF %s // ALIGNED-NEW-OFF-NOT: "-faligned-allocation" +// RUN: %clang_cl /c -### /kernel -- %s 2>&1 | FileCheck -check-prefixes=KERNEL-NO-RTTI,KERNEL-NO-EXCEPTIONS %s +// KERNEL-NO-RTTI: "-fno-rtti" +// KERNEL-NO-EXCEPTIONS-NOT: "-fcxx-exceptions" "-fexceptions" + +// RUN: %clang_cl /c -### --target=i686-pc-windows-msvc /kernel /arch:SSE -- %s 2>&1 | FileCheck -check-prefixes=KERNEL-SSE %s +// RUN: %clang_cl /c -### --target=i686-pc-windows-msvc /kernel /arch:SSE2 -- %s 2>&1 | FileCheck -check-prefixes=KERNEL-SSE2 %s +// RUN: %clang_cl /c -### --target=i686-pc-windows-msvc /kernel /arch:AVX -- %s 2>&1 | FileCheck -check-prefixes=KERNEL-AVX %s +// RUN: %clang_cl /c -### --target=i686-pc-windows-msvc /kernel /arch:AVX2 -- %s 2>&1 | FileCheck -check-prefixes=KERNEL-AVX2 %s +// RUN: %clang_cl /c -### --target=i686-pc-windows-msvc /kernel /arch:AVX512 -- %s 2>&1 | FileCheck -check-prefixes=KERNEL-AVX512 %s +// KERNEL-SSE: error: invalid argument '/arch:SSE' not allowed with '/kernel' +// KERNEL-SSE2: error: invalid argument '/arch:SSE2' not allowed with '/kernel' +// KERNEL-AVX: error: invalid argument '/arch:AVX' not allowed with '/kernel' +// KERNEL-AVX2: error: invalid argument '/arch:AVX2' not allowed with '/kernel' +// KERNEL-AVX512: error: invalid argument '/arch:AVX512' not allowed with '/kernel' + +// RUN: %clang_cl /c -### --target=x86_64-pc-windows-msvc /kernel /arch:AVX -- %s 2>&1 | FileCheck -check-prefixes=KERNEL-X64-AVX %s +// RUN: %clang_cl /c -### --target=x86_64-pc-windows-msvc /kernel /arch:AVX2 -- %s 2>&1 | FileCheck -check-prefixes=KERNEL-X64-AVX2 %s +// RUN: %clang_cl /c -### --target=x86_64-pc-windows-msvc /kernel /arch:AVX512 -- %s 2>&1 | FileCheck -check-prefixes=KERNEL-X64-AVX512 %s +// KERNEL-X64-AVX: error: invalid argument '/arch:AVX' not allowed with '/kernel' +// KERNEL-X64-AVX2: error: invalid argument '/arch:AVX2' not allowed with '/kernel' +// KERNEL-X64-AVX512: error: invalid argument '/arch:AVX512' not allowed with '/kernel' + // RUN: %clang_cl /c -### -- %s 2>&1 | FileCheck -check-prefix=STRICTSTRINGS-DEFAULT %s // STRICTSTRINGS-DEFAULT-NOT: -Werror=c++11-compat-deprecated-writable-strings // RUN: %clang_cl /c -### /Zc:strictStrings -- %s 2>&1 | FileCheck -check-prefix=STRICTSTRINGS-ON %s