Provide TargetMachine implementations with reference to Target they were created
[oota-llvm.git] / lib / Target / Sparc / SparcTargetMachine.cpp
1 //===-- SparcTargetMachine.cpp - Define TargetMachine for Sparc -----------===//
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 //
11 //===----------------------------------------------------------------------===//
12
13 #include "SparcTargetAsmInfo.h"
14 #include "SparcTargetMachine.h"
15 #include "Sparc.h"
16 #include "llvm/Module.h"
17 #include "llvm/PassManager.h"
18 #include "llvm/Target/TargetMachineRegistry.h"
19 using namespace llvm;
20
21 // Register the target.
22 extern Target TheSparcTarget;
23 static RegisterTarget<SparcTargetMachine> X(TheSparcTarget, "sparc", "SPARC");
24
25 // No assembler printer by default
26 SparcTargetMachine::AsmPrinterCtorFn SparcTargetMachine::AsmPrinterCtor = 0;
27
28
29 // Force static initialization.
30 extern "C" void LLVMInitializeSparcTarget() { }
31
32 const TargetAsmInfo *SparcTargetMachine::createTargetAsmInfo() const {
33   // FIXME: Handle Solaris subtarget someday :)
34   return new SparcELFTargetAsmInfo(*this);
35 }
36
37 /// SparcTargetMachine ctor - Create an ILP32 architecture model
38 ///
39 SparcTargetMachine::SparcTargetMachine(const Target &T, const Module &M, 
40                                        const std::string &FS)
41   : LLVMTargetMachine(T),
42     DataLayout("E-p:32:32-f128:128:128"),
43     Subtarget(M, FS), TLInfo(*this), InstrInfo(Subtarget),
44     FrameInfo(TargetFrameInfo::StackGrowsDown, 8, 0) {
45 }
46
47 unsigned SparcTargetMachine::getModuleMatchQuality(const Module &M) {
48   std::string TT = M.getTargetTriple();
49   if (TT.size() >= 6 && std::string(TT.begin(), TT.begin()+6) == "sparc-")
50     return 20;
51   
52   // If the target triple is something non-sparc, we don't match.
53   if (!TT.empty()) return 0;
54
55   if (M.getEndianness()  == Module::BigEndian &&
56       M.getPointerSize() == Module::Pointer32)
57 #ifdef __sparc__
58     return 20;   // BE/32 ==> Prefer sparc on sparc
59 #else
60     return 5;    // BE/32 ==> Prefer ppc elsewhere
61 #endif
62   else if (M.getEndianness() != Module::AnyEndianness ||
63            M.getPointerSize() != Module::AnyPointerSize)
64     return 0;                                    // Match for some other target
65
66 #if defined(__sparc__)
67   return 10;
68 #else
69   return 0;
70 #endif
71 }
72
73 bool SparcTargetMachine::addInstSelector(PassManagerBase &PM,
74                                          CodeGenOpt::Level OptLevel) {
75   PM.add(createSparcISelDag(*this));
76   return false;
77 }
78
79 /// addPreEmitPass - This pass may be implemented by targets that want to run
80 /// passes immediately before machine code is emitted.  This should return
81 /// true if -print-machineinstrs should print out the code after the passes.
82 bool SparcTargetMachine::addPreEmitPass(PassManagerBase &PM,
83                                         CodeGenOpt::Level OptLevel){
84   PM.add(createSparcFPMoverPass(*this));
85   PM.add(createSparcDelaySlotFillerPass(*this));
86   return true;
87 }
88
89 bool SparcTargetMachine::addAssemblyEmitter(PassManagerBase &PM,
90                                             CodeGenOpt::Level OptLevel,
91                                             bool Verbose,
92                                             formatted_raw_ostream &Out) {
93   // Output assembly language.
94   assert(AsmPrinterCtor && "AsmPrinter was not linked in");
95   if (AsmPrinterCtor)
96     PM.add(AsmPrinterCtor(Out, *this, Verbose));
97   return false;
98 }