MC/AsmPrinter: Reduce code duplication.

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.

llvm-svn: 320666
This commit is contained in:
Matthias Braun 2017-12-14 03:59:24 +00:00
parent 123adb5073
commit 5dd72adbec
5 changed files with 36 additions and 43 deletions

View 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);

View 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);

View 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;

View 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);
}

View 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: }