ed445f15d48d926ee1b844607445d76b83d88f8f
[oota-llvm.git] / lib / Target / Mips / MipsTargetMachine.cpp
1 //===-- MipsTargetMachine.cpp - Define TargetMachine for Mips -------------===//
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 // Implements the info about Mips target spec.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "Mips.h"
15 #include "MipsTargetAsmInfo.h"
16 #include "MipsTargetMachine.h"
17 #include "llvm/Module.h"
18 #include "llvm/PassManager.h"
19 #include "llvm/Target/TargetMachineRegistry.h"
20 using namespace llvm;
21
22 /// MipsTargetMachineModule - Note that this is used on hosts that
23 /// cannot link in a library unless there are references into the
24 /// library.  In particular, it seems that it is not possible to get
25 /// things to work on Win32 without this.  Though it is unused, do not
26 /// remove it.
27 extern "C" int MipsTargetMachineModule;
28 int MipsTargetMachineModule = 0;
29
30 // Register the target.
31 extern Target TheMipsTarget;
32 static RegisterTarget<MipsTargetMachine>    X(TheMipsTarget, "mips", "Mips");
33
34 extern Target TheMipselTarget;
35 static RegisterTarget<MipselTargetMachine>  Y(TheMipselTarget, "mipsel", 
36                                               "Mipsel");
37
38 MipsTargetMachine::AsmPrinterCtorFn MipsTargetMachine::AsmPrinterCtor = 0;
39
40
41 // Force static initialization.
42 extern "C" void LLVMInitializeMipsTarget() { }
43
44 const TargetAsmInfo *MipsTargetMachine::
45 createTargetAsmInfo() const 
46 {
47   return new MipsTargetAsmInfo(*this);
48 }
49
50 // DataLayout --> Big-endian, 32-bit pointer/ABI/alignment
51 // The stack is always 8 byte aligned
52 // On function prologue, the stack is created by decrementing
53 // its pointer. Once decremented, all references are done with positive
54 // offset from the stack/frame pointer, using StackGrowsUp enables 
55 // an easier handling.
56 // Using CodeModel::Large enables different CALL behavior.
57 MipsTargetMachine::
58 MipsTargetMachine(const Target &T, const Module &M, const std::string &FS, 
59                   bool isLittle=false):
60   LLVMTargetMachine(T),
61   Subtarget(*this, M, FS, isLittle), 
62   DataLayout(isLittle ? std::string("e-p:32:32:32-i8:8:32-i16:16:32") :
63                         std::string("E-p:32:32:32-i8:8:32-i16:16:32")), 
64   InstrInfo(*this), 
65   FrameInfo(TargetFrameInfo::StackGrowsUp, 8, 0),
66   TLInfo(*this) 
67 {
68   // Abicall enables PIC by default
69   if (Subtarget.hasABICall())
70     setRelocationModel(Reloc::PIC_);  
71
72   // TODO: create an option to enable long calls, like -mlong-calls, 
73   // that would be our CodeModel::Large. It must not work with Abicall.
74   if (getCodeModel() == CodeModel::Default)
75     setCodeModel(CodeModel::Small);
76 }
77
78 MipselTargetMachine::
79 MipselTargetMachine(const Target &T, const Module &M, const std::string &FS) :
80   MipsTargetMachine(T, M, FS, true) {}
81
82 // return 0 and must specify -march to gen MIPS code.
83 unsigned MipsTargetMachine::
84 getModuleMatchQuality(const Module &M) 
85 {
86   // We strongly match "mips*-*".
87   std::string TT = M.getTargetTriple();
88   if (TT.size() >= 5 && std::string(TT.begin(), TT.begin()+5) == "mips-")
89     return 20;
90   
91   if (TT.size() >= 13 && std::string(TT.begin(), 
92       TT.begin()+13) == "mipsallegrex-")
93     return 20;
94
95   return 0;
96 }
97
98 // return 0 and must specify -march to gen MIPSEL code.
99 unsigned MipselTargetMachine::
100 getModuleMatchQuality(const Module &M) 
101 {
102   // We strongly match "mips*el-*".
103   std::string TT = M.getTargetTriple();
104   if (TT.size() >= 7 && std::string(TT.begin(), TT.begin()+7) == "mipsel-")
105     return 20;
106
107   if (TT.size() >= 15 && std::string(TT.begin(), 
108       TT.begin()+15) == "mipsallegrexel-")
109     return 20;
110
111   if (TT.size() == 3 && std::string(TT.begin(), TT.begin()+3) == "psp")
112     return 20;
113   
114   return 0;
115 }
116
117 // Install an instruction selector pass using 
118 // the ISelDag to gen Mips code.
119 bool MipsTargetMachine::
120 addInstSelector(PassManagerBase &PM, CodeGenOpt::Level OptLevel) 
121 {
122   PM.add(createMipsISelDag(*this));
123   return false;
124 }
125
126 // Implemented by targets that want to run passes immediately before 
127 // machine code is emitted. return true if -print-machineinstrs should 
128 // print out the code after the passes.
129 bool MipsTargetMachine::
130 addPreEmitPass(PassManagerBase &PM, CodeGenOpt::Level OptLevel) 
131 {
132   PM.add(createMipsDelaySlotFillerPass(*this));
133   return true;
134 }
135
136 // Implements the AssemblyEmitter for the target. Must return
137 // true if AssemblyEmitter is supported
138 bool MipsTargetMachine::
139 addAssemblyEmitter(PassManagerBase &PM, CodeGenOpt::Level OptLevel, 
140                    bool Verbose, formatted_raw_ostream &Out)  {
141   // Output assembly language.
142   assert(AsmPrinterCtor && "AsmPrinter was not linked in");
143   PM.add(AsmPrinterCtor(Out, *this, Verbose));
144   return false;
145 }