Skip to content
This repository
Browse code

Take advantage of Triple and TargetData.

A lot of system specific knowledge is already present in LLVM. This is used to populate several fields in global.params instead of hard coded values in main(). Ensures that the frontend and LLVM have always the same values.
  • Loading branch information...
commit 940d6e907ffc5b1eeffeb80d5ad8c425c8af39a9 1 parent 98f9742
Kai Nacke redstar authored
36 dmd/mars.h
@@ -71,6 +71,10 @@ the target object file format:
71 71 #include <stddef.h>
72 72 #include <stdarg.h>
73 73
  74 +#if IN_LLVM
  75 +#include "llvm/ADT/Triple.h"
  76 +#endif
  77 +
74 78 #ifdef __DMC__
75 79 #ifdef DEBUG
76 80 #undef assert
@@ -140,14 +144,15 @@ struct OutBuffer;
140 144 // LDC
141 145 enum ARCH
142 146 {
143   - ARCHinvalid,
144   - ARCHx86,
145   - ARCHx86_64,
146   - ARCHppc,
147   - ARCHppc_64,
148   - ARCHarm,
149   - ARCHthumb
  147 + ARCHinvalid = llvm::Triple::UnknownArch,
  148 + ARCHx86 = llvm::Triple::x86,
  149 + ARCHx86_64 = llvm::Triple::x86_64,
  150 + ARCHppc = llvm::Triple::ppc,
  151 + ARCHppc_64 = llvm::Triple::ppc64,
  152 + ARCHarm = llvm::Triple::arm,
  153 + ARCHthumb = llvm::Triple::thumb,
150 154 };
  155 +
151 156 enum OUTPUTFLAG
152 157 {
153 158 OUTPUTFLAGno,
@@ -157,13 +162,13 @@ enum OUTPUTFLAG
157 162
158 163 enum OS
159 164 {
160   - OSinvalid,
161   - OSLinux,
162   - OSHaiku,
163   - OSWindows,
164   - OSMacOSX,
165   - OSFreeBSD,
166   - OSSolaris,
  165 + OSinvalid = llvm::Triple::UnknownOS,
  166 + OSLinux = llvm::Triple::Linux,
  167 + OSHaiku = llvm::Triple::Haiku,
  168 + OSWindows = llvm::Triple::Win32,
  169 + OSMacOSX = llvm::Triple::MacOSX,
  170 + OSFreeBSD = llvm::Triple::FreeBSD,
  171 + OSSolaris = llvm::Triple::Solaris,
167 172 };
168 173
169 174 typedef unsigned char ubyte;
@@ -303,8 +308,7 @@ struct Param
303 308 bool useAvailableExternally;
304 309
305 310 // target stuff
306   - const char* llvmArch;
307   - const char *targetTriple;
  311 + llvm::Triple *targetTriple;
308 312
309 313 // Codegen cl options
310 314 bool singleObj;
36 dmd2/mars.h
@@ -71,6 +71,10 @@ the target object file format:
71 71 #include <stddef.h>
72 72 #include <stdarg.h>
73 73
  74 +#if IN_LLVM
  75 +#include "llvm/ADT/Triple.h"
  76 +#endif
  77 +
74 78 #ifdef __DMC__
75 79 #ifdef DEBUG
76 80 #undef assert
@@ -145,14 +149,15 @@ typedef ArrayBase<char> Strings;
145 149 #if IN_LLVM
146 150 enum ARCH
147 151 {
148   - ARCHinvalid,
149   - ARCHx86,
150   - ARCHx86_64,
151   - ARCHppc,
152   - ARCHppc_64,
153   - ARCHarm,
154   - ARCHthumb
  152 + ARCHinvalid = llvm::Triple::UnknownArch,
  153 + ARCHx86 = llvm::Triple::x86,
  154 + ARCHx86_64 = llvm::Triple::x86_64,
  155 + ARCHppc = llvm::Triple::ppc,
  156 + ARCHppc_64 = llvm::Triple::ppc64,
  157 + ARCHarm = llvm::Triple::arm,
  158 + ARCHthumb = llvm::Triple::thumb,
155 159 };
  160 +
156 161 enum OUTPUTFLAG
157 162 {
158 163 OUTPUTFLAGno,
@@ -162,13 +167,13 @@ enum OUTPUTFLAG
162 167
163 168 enum OS
164 169 {
165   - OSinvalid,
166   - OSLinux,
167   - OSHaiku,
168   - OSWindows,
169   - OSMacOSX,
170   - OSFreeBSD,
171   - OSSolaris,
  170 + OSinvalid = llvm::Triple::UnknownOS,
  171 + OSLinux = llvm::Triple::Linux,
  172 + OSHaiku = llvm::Triple::Haiku,
  173 + OSWindows = llvm::Triple::Win32,
  174 + OSMacOSX = llvm::Triple::MacOSX,
  175 + OSFreeBSD = llvm::Triple::FreeBSD,
  176 + OSSolaris = llvm::Triple::Solaris,
172 177 };
173 178
174 179 typedef unsigned char ubyte;
@@ -271,8 +276,7 @@ struct Param
271 276 bool useAvailableExternally;
272 277
273 278 // target stuff
274   - const char* llvmArch;
275   - const char *targetTriple;
  279 + llvm::Triple targetTriple;
276 280
277 281 // Codegen cl options
278 282 bool singleObj;
4 driver/linker.cpp
@@ -382,7 +382,7 @@ int linkObjToBinaryWin(bool sharedLib)
382 382 int linkObjToBinary(bool sharedLib)
383 383 {
384 384 int status;
385   - if (llvm::Triple(global.params.targetTriple).isOSWindows())
  385 + if (global.params.targetTriple.getOS() == llvm::Triple::Win32)
386 386 status = linkObjToBinaryWin(sharedLib);
387 387 else
388 388 status = linkObjToBinaryGcc(sharedLib);
@@ -395,7 +395,7 @@ void createStaticLibrary()
395 395 {
396 396 Logger::println("*** Creating static library ***");
397 397
398   - const bool isTargetWindows = llvm::Triple(global.params.targetTriple).isOSWindows();
  398 + const bool isTargetWindows = global.params.targetTriple.getOS() == llvm::Triple::Win32;
399 399
400 400 // find archiver
401 401 llvm::sys::Path tool = isTargetWindows ? getLib() : getArchiver();
96 driver/main.cpp
@@ -498,7 +498,7 @@ int main(int argc, char** argv)
498 498 triple = llvm::Triple::normalize(mTargetTriple);
499 499 }
500 500
501   - global.params.targetTriple = triple.c_str();
  501 + global.params.targetTriple = llvm::Triple(triple);
502 502
503 503 // Allocate target machine.
504 504 const llvm::Target *theTarget = NULL;
@@ -566,54 +566,41 @@ int main(int argc, char** argv)
566 566
567 567 gTargetData = target->getTargetData();
568 568
569   - global.params.llvmArch = theTarget->getName();
  569 + global.params.isLE = gTargetData->isLittleEndian();
  570 + // Starting with LLVM 3.1 we could also use global.params.targetTriple.isArch64Bit();
  571 + global.params.is64bit = gTargetData->getPointerSizeInBits() == 64;
  572 + global.params.cpu = static_cast<ARCH>(global.params.targetTriple.getArch());
  573 + global.params.os = static_cast<OS>(global.params.targetTriple.getOS());
570 574
571   - if (strcmp(global.params.llvmArch,"x86")==0) {
572   - VersionCondition::addPredefinedGlobalIdent("X86");
573   - global.params.isLE = true;
574   - global.params.is64bit = false;
575   - global.params.cpu = ARCHx86;
576   - if (global.params.useInlineAsm) {
577   - VersionCondition::addPredefinedGlobalIdent("D_InlineAsm_X86");
578   - }
579   - }
580   - else if (strcmp(global.params.llvmArch,"x86-64")==0) {
581   - VersionCondition::addPredefinedGlobalIdent("X86_64");
582   - global.params.isLE = true;
583   - global.params.is64bit = true;
584   - global.params.cpu = ARCHx86_64;
585   - if (global.params.useInlineAsm) {
586   - VersionCondition::addPredefinedGlobalIdent("D_InlineAsm_X86_64");
587   - }
588   - }
589   - else if (strcmp(global.params.llvmArch,"ppc32")==0) {
590   - VersionCondition::addPredefinedGlobalIdent("PPC");
591   - global.params.isLE = false;
592   - global.params.is64bit = false;
593   - global.params.cpu = ARCHppc;
594   - }
595   - else if (strcmp(global.params.llvmArch,"ppc64")==0) {
596   - VersionCondition::addPredefinedGlobalIdent("PPC64");
597   - global.params.isLE = false;
598   - global.params.is64bit = true;
599   - global.params.cpu = ARCHppc_64;
600   - }
601   - else if (strcmp(global.params.llvmArch,"arm")==0) {
602   - VersionCondition::addPredefinedGlobalIdent("ARM");
603   - global.params.isLE = true;
604   - global.params.is64bit = false;
605   - global.params.cpu = ARCHarm;
606   - }
607   - else if (strcmp(global.params.llvmArch,"thumb")==0) {
608   - VersionCondition::addPredefinedGlobalIdent("ARM");
609   - VersionCondition::addPredefinedGlobalIdent("Thumb");
610   - global.params.isLE = true;
611   - global.params.is64bit = false;
612   - global.params.cpu = ARCHthumb;
613   - }
614   - else {
615   - error("invalid cpu architecture specified: %s", global.params.llvmArch);
616   - fatal();
  575 + switch (global.params.targetTriple.getArch())
  576 + {
  577 + case llvm::Triple::x86:
  578 + VersionCondition::addPredefinedGlobalIdent("X86");
  579 + if (global.params.useInlineAsm) {
  580 + VersionCondition::addPredefinedGlobalIdent("D_InlineAsm_X86");
  581 + }
  582 + break;
  583 + case llvm::Triple::x86_64:
  584 + VersionCondition::addPredefinedGlobalIdent("X86_64");
  585 + if (global.params.useInlineAsm) {
  586 + VersionCondition::addPredefinedGlobalIdent("D_InlineAsm_X86_64");
  587 + }
  588 + break;
  589 + case llvm::Triple::ppc:
  590 + VersionCondition::addPredefinedGlobalIdent("PPC");
  591 + break;
  592 + case llvm::Triple::ppc64:
  593 + VersionCondition::addPredefinedGlobalIdent("PPC64");
  594 + break;
  595 + case llvm::Triple::arm:
  596 + VersionCondition::addPredefinedGlobalIdent("ARM");
  597 + case llvm::Triple::thumb:
  598 + VersionCondition::addPredefinedGlobalIdent("ARM");
  599 + VersionCondition::addPredefinedGlobalIdent("Thumb");
  600 + break;
  601 + default:
  602 + error("invalid cpu architecture specified: %s", global.params.targetTriple.getArchName().str().c_str());
  603 + fatal();
617 604 }
618 605
619 606 // endianness
@@ -638,10 +625,9 @@ int main(int argc, char** argv)
638 625 // parse the OS out of the target triple
639 626 // see http://gcc.gnu.org/install/specific.html for details
640 627 // also llvm's different SubTargets have useful information
641   - switch (llvm::Triple(triple).getOS())
  628 + switch (global.params.targetTriple.getOS())
642 629 {
643 630 case llvm::Triple::Win32:
644   - global.params.os = OSWindows;
645 631 VersionCondition::addPredefinedGlobalIdent("Windows");
646 632 VersionCondition::addPredefinedGlobalIdent("Win32");
647 633 if (global.params.is64bit) {
@@ -649,7 +635,7 @@ int main(int argc, char** argv)
649 635 }
650 636 break;
651 637 case llvm::Triple::MinGW32:
652   - global.params.os = OSWindows;
  638 + global.params.os = OSWindows; // FIXME: Check source for uses of MinGW32
653 639 VersionCondition::addPredefinedGlobalIdent("Windows");
654 640 VersionCondition::addPredefinedGlobalIdent("Win32");
655 641 VersionCondition::addPredefinedGlobalIdent("mingw32");
@@ -663,35 +649,31 @@ int main(int argc, char** argv)
663 649 fatal();
664 650 break;
665 651 case llvm::Triple::Linux:
666   - global.params.os = OSLinux;
667 652 VersionCondition::addPredefinedGlobalIdent("linux");
668 653 VersionCondition::addPredefinedGlobalIdent("Posix");
669 654 break;
670 655 case llvm::Triple::Haiku:
671   - global.params.os = OSHaiku;
672 656 VersionCondition::addPredefinedGlobalIdent("Haiku");
673 657 VersionCondition::addPredefinedGlobalIdent("Posix");
674 658 break;
675 659 case llvm::Triple::Darwin:
676   - global.params.os = OSMacOSX;
  660 + global.params.os = OSMacOSX; // FIXME: Do we need to distinguish between Darwin and MacOSX?
677 661 VersionCondition::addPredefinedGlobalIdent("OSX");
678 662 VersionCondition::addPredefinedGlobalIdent("darwin");
679 663 VersionCondition::addPredefinedGlobalIdent("Posix");
680 664 break;
681 665 case llvm::Triple::FreeBSD:
682   - global.params.os = OSFreeBSD;
683 666 VersionCondition::addPredefinedGlobalIdent("freebsd");
684 667 VersionCondition::addPredefinedGlobalIdent("FreeBSD");
685 668 VersionCondition::addPredefinedGlobalIdent("Posix");
686 669 break;
687 670 case llvm::Triple::Solaris:
688   - global.params.os = OSSolaris;
689 671 VersionCondition::addPredefinedGlobalIdent("solaris");
690 672 VersionCondition::addPredefinedGlobalIdent("Solaris");
691 673 VersionCondition::addPredefinedGlobalIdent("Posix");
692 674 break;
693 675 default:
694   - error("target '%s' is not yet supported", global.params.targetTriple);
  676 + error("target '%s' is not yet supported", global.params.targetTriple.str().c_str());
695 677 fatal();
696 678 }
697 679
2  gen/asmstmt.cpp
@@ -149,7 +149,7 @@ Statement *AsmStatement::semantic(Scope *sc)
149 149 bool err = false;
150 150 if ((global.params.cpu != ARCHx86) && (global.params.cpu != ARCHx86_64))
151 151 {
152   - error("inline asm is not supported for the \"%s\" architecture", global.params.llvmArch);
  152 + error("inline asm is not supported for the \"%s\" architecture", global.params.targetTriple.getArchName().str().c_str());
153 153 err = true;
154 154 }
155 155 if (!global.params.useInlineAsm)
2  gen/module.cpp
@@ -242,7 +242,7 @@ llvm::Module* Module::genLLVMModule(llvm::LLVMContext& context, Ir* sir)
242 242 sir->setState(&ir);
243 243
244 244 // set target triple
245   - ir.module->setTargetTriple(global.params.targetTriple);
  245 + ir.module->setTargetTriple(global.params.targetTriple.str());
246 246
247 247 // set final data layout
248 248 ir.module->setDataLayout(gTargetData->getStringRepresentation());
2  gen/naked.cpp
@@ -344,7 +344,7 @@ void emitABIReturnAsmStmt(IRAsmBlock* asmblock, Loc loc, FuncDeclaration* fdecl)
344 344 // unsupported
345 345 else
346 346 {
347   - error(loc, "this target (%s) does not implement inline asm falling off the end of the function", global.params.targetTriple);
  347 + error(loc, "this target (%s) does not implement inline asm falling off the end of the function", global.params.targetTriple.str().c_str());
348 348 fatal();
349 349 }
350 350

0 comments on commit 940d6e9

Please sign in to comment.
Something went wrong with that request. Please try again.