Skip to content

Commit 3547633

Browse files
committed
Support: split object format out of environment
This is a preliminary setup change to support a renaming of Windows target triples. Split the object file format information out of the environment into a separate entity. Unfortunately, file format was previously treated as an environment with an unknown OS. This is most obvious in the ARM subtarget where the handling for macho on an arbitrary platform switches to AAPCS rather than APCS (as per Apple's needs). llvm-svn: 203160
1 parent 0b55699 commit 3547633

File tree

9 files changed

+96
-26
lines changed

9 files changed

+96
-26
lines changed

llvm/include/llvm/ADT/Triple.h

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,14 @@ class Triple {
124124
CODE16,
125125
EABI,
126126
EABIHF,
127-
MachO,
128127
Android,
129-
ELF
128+
};
129+
enum ObjectFormatType {
130+
UnknownObjectFormat,
131+
132+
COFF,
133+
ELF,
134+
MachO,
130135
};
131136

132137
private:
@@ -144,13 +149,16 @@ class Triple {
144149
/// The parsed Environment type.
145150
EnvironmentType Environment;
146151

152+
/// The object format type.
153+
ObjectFormatType ObjectFormat;
154+
147155
public:
148156
/// @name Constructors
149157
/// @{
150158

151159
/// \brief Default constructor is the same as an empty string and leaves all
152160
/// triple fields unknown.
153-
Triple() : Data(), Arch(), Vendor(), OS(), Environment() {}
161+
Triple() : Data(), Arch(), Vendor(), OS(), Environment(), ObjectFormat() {}
154162

155163
explicit Triple(const Twine &Str);
156164
Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr);
@@ -189,6 +197,9 @@ class Triple {
189197
/// getEnvironment - Get the parsed environment type of this triple.
190198
EnvironmentType getEnvironment() const { return Environment; }
191199

200+
/// getFormat - Get the object format for this triple.
201+
ObjectFormatType getObjectFormat() const { return ObjectFormat; }
202+
192203
/// getOSVersion - Parse the version number from the OS name component of the
193204
/// triple, if present.
194205
///
@@ -344,18 +355,17 @@ class Triple {
344355

345356
/// \brief Tests whether the OS uses the ELF binary format.
346357
bool isOSBinFormatELF() const {
347-
return !isOSBinFormatMachO() && !isOSBinFormatCOFF();
358+
return getObjectFormat() == Triple::ELF;
348359
}
349360

350361
/// \brief Tests whether the OS uses the COFF binary format.
351362
bool isOSBinFormatCOFF() const {
352-
return getEnvironment() != Triple::ELF &&
353-
getEnvironment() != Triple::MachO && isOSWindows();
363+
return getObjectFormat() == Triple::COFF;
354364
}
355365

356366
/// \brief Tests whether the environment is MachO.
357367
bool isOSBinFormatMachO() const {
358-
return getEnvironment() == Triple::MachO || isOSDarwin();
368+
return getObjectFormat() == Triple::MachO;
359369
}
360370

361371
/// @}
@@ -378,6 +388,9 @@ class Triple {
378388
/// to a known type.
379389
void setEnvironment(EnvironmentType Kind);
380390

391+
/// setObjectFormat - Set the object file format
392+
void setObjectFormat(ObjectFormatType Kind);
393+
381394
/// setTriple - Set all components to the new triple \p Str.
382395
void setTriple(const Twine &Str);
383396

llvm/lib/MC/MCObjectFileInfo.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -736,10 +736,10 @@ void MCObjectFileInfo::InitMCObjectFileInfo(StringRef TT, Reloc::Model relocm,
736736
Arch == Triple::arm || Arch == Triple::thumb ||
737737
Arch == Triple::ppc || Arch == Triple::ppc64 ||
738738
Arch == Triple::UnknownArch) &&
739-
(T.isOSDarwin() || T.getEnvironment() == Triple::MachO)) {
739+
(T.isOSDarwin() || T.isOSBinFormatMachO())) {
740740
Env = IsMachO;
741741
InitMachOMCObjectFileInfo(T);
742-
} else if (T.isOSWindows() && T.getEnvironment() != Triple::ELF) {
742+
} else if (T.isOSWindows() && !T.isOSBinFormatELF()) {
743743
assert((Arch == Triple::x86 || Arch == Triple::x86_64) &&
744744
"expected x86 or x86_64");
745745
Env = IsCOFF;

llvm/lib/Support/Triple.cpp

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,7 @@ const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
154154
case CODE16: return "code16";
155155
case EABI: return "eabi";
156156
case EABIHF: return "eabihf";
157-
case MachO: return "macho";
158157
case Android: return "android";
159-
case ELF: return "elf";
160158
}
161159

162160
llvm_unreachable("Invalid EnvironmentType!");
@@ -310,12 +308,36 @@ static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
310308
.StartsWith("gnux32", Triple::GNUX32)
311309
.StartsWith("code16", Triple::CODE16)
312310
.StartsWith("gnu", Triple::GNU)
313-
.StartsWith("macho", Triple::MachO)
314311
.StartsWith("android", Triple::Android)
315-
.StartsWith("elf", Triple::ELF)
316312
.Default(Triple::UnknownEnvironment);
317313
}
318314

315+
static Triple::ObjectFormatType parseFormat(StringRef EnvironmentName) {
316+
return StringSwitch<Triple::ObjectFormatType>(EnvironmentName)
317+
.EndsWith("coff", Triple::COFF)
318+
.EndsWith("elf", Triple::ELF)
319+
.EndsWith("macho", Triple::MachO)
320+
.Default(Triple::UnknownObjectFormat);
321+
}
322+
323+
static const char *getObjectFormatTypeName(Triple::ObjectFormatType Kind) {
324+
switch (Kind) {
325+
case Triple::UnknownObjectFormat: return "";
326+
case Triple::COFF: return "coff";
327+
case Triple::ELF: return "elf";
328+
case Triple::MachO: return "macho";
329+
}
330+
llvm_unreachable("unknown object format type");
331+
}
332+
333+
static Triple::ObjectFormatType getDefaultFormat(const Triple &T) {
334+
if (T.isOSDarwin())
335+
return Triple::MachO;
336+
else if (T.isOSWindows())
337+
return Triple::COFF;
338+
return Triple::ELF;
339+
}
340+
319341
/// \brief Construct a triple from the string representation provided.
320342
///
321343
/// This stores the string representation and parses the various pieces into
@@ -325,7 +347,10 @@ Triple::Triple(const Twine &Str)
325347
Arch(parseArch(getArchName())),
326348
Vendor(parseVendor(getVendorName())),
327349
OS(parseOS(getOSName())),
328-
Environment(parseEnvironment(getEnvironmentName())) {
350+
Environment(parseEnvironment(getEnvironmentName())),
351+
ObjectFormat(parseFormat(getEnvironmentName())) {
352+
if (ObjectFormat == Triple::UnknownObjectFormat)
353+
ObjectFormat = getDefaultFormat(*this);
329354
}
330355

331356
/// \brief Construct a triple from string representations of the architecture,
@@ -339,7 +364,8 @@ Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
339364
Arch(parseArch(ArchStr.str())),
340365
Vendor(parseVendor(VendorStr.str())),
341366
OS(parseOS(OSStr.str())),
342-
Environment() {
367+
Environment(), ObjectFormat(Triple::UnknownObjectFormat) {
368+
ObjectFormat = getDefaultFormat(*this);
343369
}
344370

345371
/// \brief Construct a triple from string representations of the architecture,
@@ -354,7 +380,10 @@ Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
354380
Arch(parseArch(ArchStr.str())),
355381
Vendor(parseVendor(VendorStr.str())),
356382
OS(parseOS(OSStr.str())),
357-
Environment(parseEnvironment(EnvironmentStr.str())) {
383+
Environment(parseEnvironment(EnvironmentStr.str())),
384+
ObjectFormat(parseFormat(EnvironmentStr.str())) {
385+
if (ObjectFormat == Triple::UnknownObjectFormat)
386+
ObjectFormat = getDefaultFormat(*this);
358387
}
359388

360389
std::string Triple::normalize(StringRef Str) {
@@ -379,6 +408,7 @@ std::string Triple::normalize(StringRef Str) {
379408
EnvironmentType Environment = UnknownEnvironment;
380409
if (Components.size() > 3)
381410
Environment = parseEnvironment(Components[3]);
411+
ObjectFormatType ObjectFormat = UnknownObjectFormat;
382412

383413
// Note which components are already in their final position. These will not
384414
// be moved.
@@ -420,6 +450,10 @@ std::string Triple::normalize(StringRef Str) {
420450
case 3:
421451
Environment = parseEnvironment(Comp);
422452
Valid = Environment != UnknownEnvironment;
453+
if (!Valid) {
454+
ObjectFormat = parseFormat(Comp);
455+
Valid = ObjectFormat != UnknownObjectFormat;
456+
}
423457
break;
424458
}
425459
if (!Valid)
@@ -641,6 +675,10 @@ void Triple::setEnvironment(EnvironmentType Kind) {
641675
setEnvironmentName(getEnvironmentTypeName(Kind));
642676
}
643677

678+
void Triple::setObjectFormat(ObjectFormatType Kind) {
679+
setEnvironmentName(getObjectFormatTypeName(Kind));
680+
}
681+
644682
void Triple::setArchName(StringRef Str) {
645683
// Work around a miscompilation bug for Twines in gcc 4.0.3.
646684
SmallString<64> Triple;

llvm/lib/Target/ARM/ARMSubtarget.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,12 @@ void ARMSubtarget::resetSubtargetFeatures(StringRef CPU, StringRef FS) {
196196
case Triple::EABIHF:
197197
case Triple::GNUEABI:
198198
case Triple::GNUEABIHF:
199-
case Triple::MachO:
200199
TargetABI = ARM_ABI_AAPCS;
201200
break;
202201
default:
203-
if (isTargetIOS() && isMClass())
202+
if ((isTargetIOS() && isMClass()) ||
203+
(TargetTriple.isOSBinFormatMachO() &&
204+
TargetTriple.getOS() == Triple::UnknownOS))
204205
TargetABI = ARM_ABI_AAPCS;
205206
else
206207
TargetABI = ARM_ABI_APCS;

llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,7 @@ MCAsmBackend *llvm::createX86_32AsmBackend(const Target &T,
801801
TheTriple.isMacOSX() &&
802802
!TheTriple.isMacOSXVersionLT(10, 7));
803803

804-
if (TheTriple.isOSWindows() && TheTriple.getEnvironment() != Triple::ELF)
804+
if (TheTriple.isOSWindows() && !TheTriple.isOSBinFormatELF())
805805
return new WindowsX86AsmBackend(T, false, CPU);
806806

807807
uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TheTriple.getOS());
@@ -824,7 +824,7 @@ MCAsmBackend *llvm::createX86_64AsmBackend(const Target &T,
824824
!TheTriple.isMacOSXVersionLT(10, 7), CS);
825825
}
826826

827-
if (TheTriple.isOSWindows() && TheTriple.getEnvironment() != Triple::ELF)
827+
if (TheTriple.isOSWindows() && !TheTriple.isOSBinFormatELF())
828828
return new WindowsX86AsmBackend(T, true, CPU);
829829

830830
uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TheTriple.getOS());

llvm/lib/Target/X86/MCTargetDesc/X86MCTargetDesc.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ static MCAsmInfo *createX86MCAsmInfo(const MCRegisterInfo &MRI, StringRef TT) {
276276
MAI = new X86_64MCAsmInfoDarwin(TheTriple);
277277
else
278278
MAI = new X86MCAsmInfoDarwin(TheTriple);
279-
} else if (TheTriple.getEnvironment() == Triple::ELF) {
279+
} else if (TheTriple.isOSBinFormatELF()) {
280280
// Force the use of an ELF container.
281281
MAI = new X86ELFMCAsmInfo(TheTriple);
282282
} else if (TheTriple.getOS() == Triple::Win32) {
@@ -370,7 +370,7 @@ static MCStreamer *createMCStreamer(const Target &T, StringRef TT,
370370
if (TheTriple.isOSBinFormatMachO())
371371
return createMachOStreamer(Ctx, MAB, _OS, _Emitter, RelaxAll);
372372

373-
if (TheTriple.isOSWindows() && TheTriple.getEnvironment() != Triple::ELF)
373+
if (TheTriple.isOSWindows() && !TheTriple.isOSBinFormatELF())
374374
return createWinCOFFStreamer(Ctx, MAB, *_Emitter, _OS, RelaxAll);
375375

376376
return createELFStreamer(Ctx, MAB, _OS, _Emitter, RelaxAll, NoExecStack);

llvm/tools/llvm-jitlistener/llvm-jitlistener.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ class JitEventListenerTest {
138138
if (Tuple.getTriple().empty())
139139
Tuple.setTriple(sys::getProcessTriple());
140140

141-
if (Tuple.isOSWindows() && Triple::ELF != Tuple.getEnvironment()) {
142-
Tuple.setEnvironment(Triple::ELF);
141+
if (Tuple.isOSWindows() && !Tuple.isOSBinFormatELF()) {
142+
Tuple.setObjectFormat(Triple::ELF);
143143
TheModule->setTargetTriple(Tuple.getTriple());
144144
}
145145

llvm/tools/llvm-objdump/llvm-objdump.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ static const Target *getTarget(const ObjectFile *Obj = NULL) {
166166
// TheTriple defaults to ELF, and COFF doesn't have an environment:
167167
// the best we can do here is indicate that it is mach-o.
168168
if (Obj->isMachO())
169-
TheTriple.setEnvironment(Triple::MachO);
169+
TheTriple.setObjectFormat(Triple::MachO);
170170
}
171171
} else
172172
TheTriple.setTriple(Triple::normalize(TripleName));

llvm/unittests/ADT/TripleTest.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ TEST(TripleTest, Normalization) {
201201
EXPECT_EQ(E, Triple::normalize(Join(C[2], C[0], C[1])));
202202
EXPECT_EQ(E, Triple::normalize(Join(C[2], C[1], C[0])));
203203

204-
for (int Env = 1+Triple::UnknownEnvironment; Env <= Triple::MachO;
204+
for (int Env = 1 + Triple::UnknownEnvironment; Env <= Triple::Android;
205205
++Env) {
206206
C[3] = Triple::getEnvironmentTypeName(Triple::EnvironmentType(Env));
207207

@@ -497,4 +497,22 @@ TEST(TripleTest, getOSVersion) {
497497
EXPECT_EQ((unsigned)0, Micro);
498498
}
499499

500+
TEST(TripleTest, FileFormat) {
501+
EXPECT_EQ(Triple::ELF, Triple("i686-unknown-linux-gnu").getObjectFormat());
502+
EXPECT_EQ(Triple::ELF, Triple("i686-unknown-freebsd").getObjectFormat());
503+
EXPECT_EQ(Triple::ELF, Triple("i686-unknown-netbsd").getObjectFormat());
504+
EXPECT_EQ(Triple::ELF, Triple("i686--win32-elf").getObjectFormat());
505+
EXPECT_EQ(Triple::ELF, Triple("i686---elf").getObjectFormat());
506+
507+
EXPECT_EQ(Triple::MachO, Triple("i686-apple-macosx").getObjectFormat());
508+
EXPECT_EQ(Triple::MachO, Triple("i686-apple-ios").getObjectFormat());
509+
EXPECT_EQ(Triple::MachO, Triple("i686---macho").getObjectFormat());
510+
511+
EXPECT_EQ(Triple::COFF, Triple("i686--win32").getObjectFormat());
512+
513+
Triple T = Triple("");
514+
T.setObjectFormat(Triple::ELF);
515+
EXPECT_EQ(Triple::ELF, T.getObjectFormat());
516+
}
517+
500518
}

0 commit comments

Comments
 (0)