Separate target specific asm properties from the asm printers.
[oota-llvm.git] / lib / Target / PowerPC / PPCTargetMachine.cpp
1 //===-- PPCTargetMachine.cpp - Define TargetMachine for PowerPC -----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Top-level implementation for the PowerPC target.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "PPC.h"
15 #include "PPCTargetMachine.h"
16 #include "llvm/Module.h"
17 #include "llvm/PassManager.h"
18 #include "llvm/Target/TargetMachineRegistry.h"
19 using namespace llvm;
20
21 namespace {
22   // Register the targets
23   RegisterTarget<PPC32TargetMachine>
24   X("ppc32", "  PowerPC 32");
25   RegisterTarget<PPC64TargetMachine>
26   Y("ppc64", "  PowerPC 64");
27 }
28
29 unsigned PPC32TargetMachine::getJITMatchQuality() {
30 #if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER)
31   if (sizeof(void*) == 4)
32     return 10;
33 #endif
34   return 0;
35 }
36 unsigned PPC64TargetMachine::getJITMatchQuality() {
37 #if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER)
38   if (sizeof(void*) == 8)
39     return 10;
40 #endif
41   return 0;
42 }
43
44 unsigned PPC32TargetMachine::getModuleMatchQuality(const Module &M) {
45   // We strongly match "powerpc-*".
46   std::string TT = M.getTargetTriple();
47   if (TT.size() >= 8 && std::string(TT.begin(), TT.begin()+8) == "powerpc-")
48     return 20;
49   
50   if (M.getEndianness()  == Module::BigEndian &&
51       M.getPointerSize() == Module::Pointer32)
52     return 10;                                   // Weak match
53   else if (M.getEndianness() != Module::AnyEndianness ||
54            M.getPointerSize() != Module::AnyPointerSize)
55     return 0;                                    // Match for some other target
56   
57   return getJITMatchQuality()/2;
58 }
59
60 unsigned PPC64TargetMachine::getModuleMatchQuality(const Module &M) {
61   // We strongly match "powerpc64-*".
62   std::string TT = M.getTargetTriple();
63   if (TT.size() >= 10 && std::string(TT.begin(), TT.begin()+10) == "powerpc64-")
64     return 20;
65   
66   if (M.getEndianness()  == Module::BigEndian &&
67       M.getPointerSize() == Module::Pointer64)
68     return 10;                                   // Weak match
69   else if (M.getEndianness() != Module::AnyEndianness ||
70            M.getPointerSize() != Module::AnyPointerSize)
71     return 0;                                    // Match for some other target
72   
73   return getJITMatchQuality()/2;
74 }
75
76
77 PPCTargetMachine::PPCTargetMachine(const Module &M, const std::string &FS,
78                                    bool is64Bit)
79   : Subtarget(M, FS, is64Bit),
80     DataLayout(Subtarget.getTargetDataString()), InstrInfo(*this),
81     FrameInfo(*this, false), JITInfo(*this, is64Bit), TLInfo(*this),
82     InstrItins(Subtarget.getInstrItineraryData()) {
83
84   if (getRelocationModel() == Reloc::Default)
85     if (Subtarget.isDarwin())
86       setRelocationModel(Reloc::DynamicNoPIC);
87     else
88       setRelocationModel(Reloc::PIC_);
89 }
90
91 PPC32TargetMachine::PPC32TargetMachine(const Module &M, const std::string &FS) 
92   : PPCTargetMachine(M, FS, false) {
93 }
94
95
96 PPC64TargetMachine::PPC64TargetMachine(const Module &M, const std::string &FS)
97   : PPCTargetMachine(M, FS, true) {
98 }
99
100
101 //===----------------------------------------------------------------------===//
102 // Pass Pipeline Configuration
103 //===----------------------------------------------------------------------===//
104
105 bool PPCTargetMachine::addInstSelector(FunctionPassManager &PM, bool Fast) {
106   // Install an instruction selector.
107   PM.add(createPPCISelDag(*this));
108   return false;
109 }
110
111 bool PPCTargetMachine::addPreEmitPass(FunctionPassManager &PM, bool Fast) {
112   
113   // Must run branch selection immediately preceding the asm printer.
114   PM.add(createPPCBranchSelectionPass());
115   return false;
116 }
117
118 bool PPCTargetMachine::addAssemblyEmitter(FunctionPassManager &PM, bool Fast, 
119                                           std::ostream &Out) {
120   PM.add(createDarwinCodePrinterPass(Out, *this));
121   return false;
122 }
123
124 bool PPCTargetMachine::addObjectWriter(FunctionPassManager &PM, bool Fast,
125                                        std::ostream &Out) {
126   // FIXME: support PPC ELF files at some point
127   addPPCMachOObjectWriterPass(PM, Out, *this);
128   return true;
129 }
130
131 bool PPCTargetMachine::addCodeEmitter(FunctionPassManager &PM, bool Fast,
132                                       MachineCodeEmitter &MCE) {
133   // The JIT should use the static relocation model.
134   // FIXME: This should be moved to TargetJITInfo!!
135   setRelocationModel(Reloc::Static);
136
137   
138   
139   // Machine code emitter pass for PowerPC.
140   PM.add(createPPCCodeEmitterPass(*this, MCE));
141   return false;
142 }
143