OSDN Git Service

MC/AsmPrinter: Reduce code duplication.
authorMatthias Braun <matze@braunis.de>
Thu, 14 Dec 2017 03:59:24 +0000 (03:59 +0000)
committerMatthias Braun <matze@braunis.de>
Thu, 14 Dec 2017 03:59:24 +0000 (03:59 +0000)
Factor out duplicated code emitting mach-o version-min specifiers.

This should be NFC but happens to fix a bug where the code in
MCMachoStreamer didn't take the version skew between darwin and macos
versions into account.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@320666 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/MC/MCStreamer.h
lib/CodeGen/AsmPrinter/AsmPrinter.cpp
lib/MC/MCMachOStreamer.cpp
lib/MC/MCStreamer.cpp
test/MC/MachO/diff-with-two-sections.s

index e4a969e..481d967 100644 (file)
@@ -430,6 +430,8 @@ public:
   virtual void EmitBuildVersion(unsigned Platform, unsigned Major,
                                 unsigned Minor, unsigned Update) {}
 
+  void EmitVersionForTarget(const Triple &Target);
+
   /// \brief Note in the output that the specified \p Func is a Thumb mode
   /// function (ARM target only).
   virtual void EmitThumbFunc(MCSymbol *Func);
index f1459d9..20381e4 100644 (file)
@@ -254,28 +254,8 @@ bool AsmPrinter::doInitialization(Module &M) {
   // alternative is duplicated code in each of the target asm printers that
   // use the directive, where it would need the same conditionalization
   // anyway.
-  const Triple &TT = TM.getTargetTriple();
-  // If there is a version specified, Major will be non-zero.
-  if (TT.isOSDarwin() && TT.getOSMajorVersion() != 0) {
-    unsigned Major, Minor, Update;
-    MCVersionMinType VersionType;
-    if (TT.isWatchOS()) {
-      VersionType = MCVM_WatchOSVersionMin;
-      TT.getWatchOSVersion(Major, Minor, Update);
-    } else if (TT.isTvOS()) {
-      VersionType = MCVM_TvOSVersionMin;
-      TT.getiOSVersion(Major, Minor, Update);
-    } else if (TT.isMacOSX()) {
-      VersionType = MCVM_OSXVersionMin;
-      if (!TT.getMacOSXVersion(Major, Minor, Update))
-        Major = 0;
-    } else {
-      VersionType = MCVM_IOSVersionMin;
-      TT.getiOSVersion(Major, Minor, Update);
-    }
-    if (Major != 0)
-      OutStreamer->EmitVersionMin(VersionType, Major, Minor, Update);
-  }
+  const Triple &Target = TM.getTargetTriple();
+  OutStreamer->EmitVersionForTarget(Target);
 
   // Allow the target to emit any magic that it wants at the start of the file.
   EmitStartOfAsmFile(M);
index 6e1dca8..82b75af 100644 (file)
@@ -502,26 +502,8 @@ MCStreamer *llvm::createMachOStreamer(MCContext &Context,
   MCMachOStreamer *S =
       new MCMachOStreamer(Context, std::move(MAB), OS, std::move(CE),
                           DWARFMustBeAtTheEnd, LabelSections);
-  const Triple &TT = Context.getObjectFileInfo()->getTargetTriple();
-  if (TT.isOSDarwin()) {
-    unsigned Major, Minor, Update;
-    TT.getOSVersion(Major, Minor, Update);
-    // If there is a version specified, Major will be non-zero.
-    if (Major) {
-      MCVersionMinType VersionType;
-      if (TT.isWatchOS())
-        VersionType = MCVM_WatchOSVersionMin;
-      else if (TT.isTvOS())
-        VersionType = MCVM_TvOSVersionMin;
-      else if (TT.isMacOSX())
-        VersionType = MCVM_OSXVersionMin;
-      else {
-        assert(TT.isiOS() && "Must only be iOS platform left");
-        VersionType = MCVM_IOSVersionMin;
-      }
-      S->EmitVersionMin(VersionType, Major, Minor, Update);
-    }
-  }
+  const Triple &Target = Context.getObjectFileInfo()->getTargetTriple();
+  S->EmitVersionForTarget(Target);
   if (RelaxAll)
     S->getAssembler().setRelaxAll(true);
   return S;
index 4067df0..6f3647d 100644 (file)
@@ -959,3 +959,32 @@ MCSymbol *MCStreamer::endSection(MCSection *Section) {
   EmitLabel(Sym);
   return Sym;
 }
+
+void MCStreamer::EmitVersionForTarget(const Triple &Target) {
+  if (!Target.isOSBinFormatMachO() || !Target.isOSDarwin())
+    return;
+  // Do we even know the version?
+  if (Target.getOSMajorVersion() == 0)
+    return;
+
+  unsigned Major;
+  unsigned Minor;
+  unsigned Update;
+  MCVersionMinType VersionType;
+  if (Target.isWatchOS()) {
+    VersionType = MCVM_WatchOSVersionMin;
+    Target.getWatchOSVersion(Major, Minor, Update);
+  } else if (Target.isTvOS()) {
+    VersionType = MCVM_TvOSVersionMin;
+    Target.getiOSVersion(Major, Minor, Update);
+  } else if (Target.isMacOSX()) {
+    VersionType = MCVM_OSXVersionMin;
+    if (!Target.getMacOSXVersion(Major, Minor, Update))
+      Major = 0;
+  } else {
+    VersionType = MCVM_IOSVersionMin;
+    Target.getiOSVersion(Major, Minor, Update);
+  }
+  if (Major != 0)
+    EmitVersionMin(VersionType, Major, Minor, Update);
+}
index 15784af..d63f4a0 100644 (file)
@@ -82,6 +82,6 @@ Ltmp4 = Leh_func_begin0-Ltmp3
 // CHECK-NEXT: MinVersion {
 // CHECK-NEXT:   Cmd: LC_VERSION_MIN_MACOSX
 // CHECK-NEXT:   Size: 16
-// CHECK-NEXT:   Version: 9.0
+// CHECK-NEXT:   Version: 10.5
 // CHECK-NEXT:   SDK: n/a
 // CHECK-NEXT: }