Factor out asmprinter out of ppc
[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 is distributed under the University of Illinois Open Source
6 // 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 "PPCTargetAsmInfo.h"
16 #include "PPCTargetMachine.h"
17 #include "llvm/Module.h"
18 #include "llvm/PassManager.h"
19 #include "llvm/Target/TargetMachineRegistry.h"
20 #include "llvm/Target/TargetOptions.h"
21 using namespace llvm;
22
23 // Register the targets
24 static RegisterTarget<PPC32TargetMachine>
25 X("ppc32", "  PowerPC 32");
26 static RegisterTarget<PPC64TargetMachine>
27 Y("ppc64", "  PowerPC 64");
28
29 // No assembler printer by default
30 PPCTargetMachine::AsmPrinterCtorFn PPCTargetMachine::AsmPrinterCtor = 0;
31
32 const TargetAsmInfo *PPCTargetMachine::createTargetAsmInfo() const {
33   if (Subtarget.isDarwin())
34     return new PPCDarwinTargetAsmInfo(*this);
35   else
36     return new PPCLinuxTargetAsmInfo(*this);
37 }
38
39 unsigned PPC32TargetMachine::getJITMatchQuality() {
40 #if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER) || defined(__PPC__)
41   if (sizeof(void*) == 4)
42     return 10;
43 #endif
44   return 0;
45 }
46 unsigned PPC64TargetMachine::getJITMatchQuality() {
47 #if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER) || defined(__PPC__)
48   if (sizeof(void*) == 8)
49     return 10;
50 #endif
51   return 0;
52 }
53
54 unsigned PPC32TargetMachine::getModuleMatchQuality(const Module &M) {
55   // We strongly match "powerpc-*".
56   std::string TT = M.getTargetTriple();
57   if (TT.size() >= 8 && std::string(TT.begin(), TT.begin()+8) == "powerpc-")
58     return 20;
59   
60   // If the target triple is something non-powerpc, we don't match.
61   if (!TT.empty()) return 0;
62   
63   if (M.getEndianness()  == Module::BigEndian &&
64       M.getPointerSize() == Module::Pointer32)
65     return 10;                                   // Weak match
66   else if (M.getEndianness() != Module::AnyEndianness ||
67            M.getPointerSize() != Module::AnyPointerSize)
68     return 0;                                    // Match for some other target
69   
70   return getJITMatchQuality()/2;
71 }
72
73 unsigned PPC64TargetMachine::getModuleMatchQuality(const Module &M) {
74   // We strongly match "powerpc64-*".
75   std::string TT = M.getTargetTriple();
76   if (TT.size() >= 10 && std::string(TT.begin(), TT.begin()+10) == "powerpc64-")
77     return 20;
78   
79   if (M.getEndianness()  == Module::BigEndian &&
80       M.getPointerSize() == Module::Pointer64)
81     return 10;                                   // Weak match
82   else if (M.getEndianness() != Module::AnyEndianness ||
83            M.getPointerSize() != Module::AnyPointerSize)
84     return 0;                                    // Match for some other target
85   
86   return getJITMatchQuality()/2;
87 }
88
89
90 PPCTargetMachine::PPCTargetMachine(const Module &M, const std::string &FS,
91                                    bool is64Bit)
92   : Subtarget(*this, M, FS, is64Bit),
93     DataLayout(Subtarget.getTargetDataString()), InstrInfo(*this),
94     FrameInfo(*this, is64Bit), JITInfo(*this, is64Bit), TLInfo(*this),
95     InstrItins(Subtarget.getInstrItineraryData()), MachOWriterInfo(*this) {
96
97   if (getRelocationModel() == Reloc::Default) {
98     if (Subtarget.isDarwin())
99       setRelocationModel(Reloc::DynamicNoPIC);
100     else
101       setRelocationModel(Reloc::Static);
102   }
103 }
104
105 /// Override this for PowerPC.  Tail merging happily breaks up instruction issue
106 /// groups, which typically degrades performance.
107 bool PPCTargetMachine::getEnableTailMergeDefault() const { return false; }
108
109 PPC32TargetMachine::PPC32TargetMachine(const Module &M, const std::string &FS) 
110   : PPCTargetMachine(M, FS, false) {
111 }
112
113
114 PPC64TargetMachine::PPC64TargetMachine(const Module &M, const std::string &FS)
115   : PPCTargetMachine(M, FS, true) {
116 }
117
118
119 //===----------------------------------------------------------------------===//
120 // Pass Pipeline Configuration
121 //===----------------------------------------------------------------------===//
122
123 bool PPCTargetMachine::addInstSelector(PassManagerBase &PM, bool Fast) {
124   // Install an instruction selector.
125   PM.add(createPPCISelDag(*this));
126   return false;
127 }
128
129 bool PPCTargetMachine::addPreEmitPass(PassManagerBase &PM, bool Fast) {
130   
131   // Must run branch selection immediately preceding the asm printer.
132   PM.add(createPPCBranchSelectionPass());
133   return false;
134 }
135
136 bool PPCTargetMachine::addAssemblyEmitter(PassManagerBase &PM, bool Fast, 
137                                           std::ostream &Out) {
138   assert(AsmPrinterCtor && "AsmPrinter was not linked in");
139   if (AsmPrinterCtor)
140     PM.add(AsmPrinterCtor(Out, *this));
141
142   return false;
143 }
144
145 bool PPCTargetMachine::addCodeEmitter(PassManagerBase &PM, bool Fast,
146                                       bool DumpAsm, MachineCodeEmitter &MCE) {
147   // The JIT should use the static relocation model in ppc32 mode, PIC in ppc64.
148   // FIXME: This should be moved to TargetJITInfo!!
149   if (Subtarget.isPPC64()) {
150     // We use PIC codegen in ppc64 mode, because otherwise we'd have to use many
151     // instructions to materialize arbitrary global variable + function +
152     // constant pool addresses.
153     setRelocationModel(Reloc::PIC_);
154     // Temporary workaround for the inability of PPC64 JIT to handle jump
155     // tables.
156     DisableJumpTables = true;      
157   } else {
158     setRelocationModel(Reloc::Static);
159   }
160   
161   // Inform the subtarget that we are in JIT mode.  FIXME: does this break macho
162   // writing?
163   Subtarget.SetJITMode();
164   
165   // Machine code emitter pass for PowerPC.
166   PM.add(createPPCCodeEmitterPass(*this, MCE));
167   if (DumpAsm) {
168     assert(AsmPrinterCtor && "AsmPrinter was not linked in");
169     if (AsmPrinterCtor)
170       PM.add(AsmPrinterCtor(*cerr.stream(), *this));
171   }
172
173   return false;
174 }
175
176 bool PPCTargetMachine::addSimpleCodeEmitter(PassManagerBase &PM, bool Fast,
177                                             bool DumpAsm, MachineCodeEmitter &MCE) {
178   // Machine code emitter pass for PowerPC.
179   PM.add(createPPCCodeEmitterPass(*this, MCE));
180   if (DumpAsm) {
181     assert(AsmPrinterCtor && "AsmPrinter was not linked in");
182     if (AsmPrinterCtor)
183       PM.add(AsmPrinterCtor(*cerr.stream(), *this));
184   }
185
186   return false;
187 }