blob: 720e9a85b5d78c0e84c61845deb54d80620bf97f [file] [log] [blame]
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001//===-- MipsTargetMachine.cpp - Define TargetMachine for Mips -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00007//
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"
20using namespace llvm;
21
Oscar Fuentes92adc192008-11-15 21:36:30 +000022/// 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.
27extern "C" int MipsTargetMachineModule;
28int MipsTargetMachineModule = 0;
29
Dan Gohman844731a2008-05-13 00:00:25 +000030// Register the target.
Dan Gohmanb8cab922008-10-14 20:25:08 +000031static RegisterTarget<MipsTargetMachine> X("mips", "Mips");
32static RegisterTarget<MipselTargetMachine> Y("mipsel", "Mipsel");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000033
Douglas Gregor1555a232009-06-16 20:12:29 +000034// Force static initialization when called from llvm/InitializeAllTargets.h
35namespace llvm {
36 void InitializeMipsTarget() { }
37}
38
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000039const TargetAsmInfo *MipsTargetMachine::
40createTargetAsmInfo() const
41{
42 return new MipsTargetAsmInfo(*this);
43}
44
45// DataLayout --> Big-endian, 32-bit pointer/ABI/alignment
Bruno Cardoso Lopes51195af2007-08-28 05:13:42 +000046// The stack is always 8 byte aligned
47// On function prologue, the stack is created by decrementing
48// its pointer. Once decremented, all references are done with positive
Bruno Cardoso Lopesbbe51362008-08-06 06:14:43 +000049// offset from the stack/frame pointer, using StackGrowsUp enables
50// an easier handling.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000051// Using CodeModel::Large enables different CALL behavior.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000052MipsTargetMachine::
Bruno Cardoso Lopesd2947ee2008-06-04 01:45:25 +000053MipsTargetMachine(const Module &M, const std::string &FS, bool isLittle=false):
54 Subtarget(*this, M, FS, isLittle),
Bruno Cardoso Lopesb27cb552008-07-15 02:03:36 +000055 DataLayout(isLittle ? std::string("e-p:32:32:32-i8:8:32-i16:16:32") :
56 std::string("E-p:32:32:32-i8:8:32-i16:16:32")),
Bruno Cardoso Lopesd2947ee2008-06-04 01:45:25 +000057 InstrInfo(*this),
58 FrameInfo(TargetFrameInfo::StackGrowsUp, 8, 0),
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +000059 TLInfo(*this)
60{
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +000061 // Abicall enables PIC by default
Bruno Cardoso Lopesb3d72992008-09-15 21:06:55 +000062 if (Subtarget.hasABICall())
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +000063 setRelocationModel(Reloc::PIC_);
Bruno Cardoso Lopes43d526d2008-07-14 14:42:54 +000064
65 // TODO: create an option to enable long calls, like -mlong-calls,
66 // that would be our CodeModel::Large. It must not work with Abicall.
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +000067 if (getCodeModel() == CodeModel::Default)
68 setCodeModel(CodeModel::Small);
Bruno Cardoso Lopes0a604002007-10-09 03:01:19 +000069}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000070
Bruno Cardoso Lopesd2947ee2008-06-04 01:45:25 +000071MipselTargetMachine::
72MipselTargetMachine(const Module &M, const std::string &FS) :
73 MipsTargetMachine(M, FS, true) {}
74
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000075// return 0 and must specify -march to gen MIPS code.
76unsigned MipsTargetMachine::
Bruno Cardoso Lopes758dcca2007-07-11 23:17:41 +000077getModuleMatchQuality(const Module &M)
78{
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000079 // We strongly match "mips*-*".
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000080 std::string TT = M.getTargetTriple();
81 if (TT.size() >= 5 && std::string(TT.begin(), TT.begin()+5) == "mips-")
82 return 20;
Bruno Cardoso Lopes758dcca2007-07-11 23:17:41 +000083
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000084 if (TT.size() >= 13 && std::string(TT.begin(),
85 TT.begin()+13) == "mipsallegrex-")
86 return 20;
87
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000088 return 0;
89}
90
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000091// return 0 and must specify -march to gen MIPSEL code.
Bruno Cardoso Lopesd2947ee2008-06-04 01:45:25 +000092unsigned MipselTargetMachine::
93getModuleMatchQuality(const Module &M)
94{
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000095 // We strongly match "mips*el-*".
Bruno Cardoso Lopesd2947ee2008-06-04 01:45:25 +000096 std::string TT = M.getTargetTriple();
97 if (TT.size() >= 7 && std::string(TT.begin(), TT.begin()+7) == "mipsel-")
98 return 20;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000099
100 if (TT.size() >= 15 && std::string(TT.begin(),
101 TT.begin()+15) == "mipsallegrexel-")
102 return 20;
103
104 if (TT.size() == 3 && std::string(TT.begin(), TT.begin()+3) == "psp")
105 return 20;
Bruno Cardoso Lopesd2947ee2008-06-04 01:45:25 +0000106
107 return 0;
108}
109
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000110// Install an instruction selector pass using
111// the ISelDag to gen Mips code.
112bool MipsTargetMachine::
Bill Wendling98a366d2009-04-29 23:29:43 +0000113addInstSelector(PassManagerBase &PM, CodeGenOpt::Level OptLevel)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000114{
115 PM.add(createMipsISelDag(*this));
116 return false;
117}
118
119// Implemented by targets that want to run passes immediately before
120// machine code is emitted. return true if -print-machineinstrs should
121// print out the code after the passes.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000122bool MipsTargetMachine::
Bill Wendling98a366d2009-04-29 23:29:43 +0000123addPreEmitPass(PassManagerBase &PM, CodeGenOpt::Level OptLevel)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000124{
Bruno Cardoso Lopesaff42dc2007-08-18 01:58:15 +0000125 PM.add(createMipsDelaySlotFillerPass(*this));
126 return true;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000127}
128
129// Implements the AssemblyEmitter for the target. Must return
130// true if AssemblyEmitter is supported
131bool MipsTargetMachine::
Bill Wendling98a366d2009-04-29 23:29:43 +0000132addAssemblyEmitter(PassManagerBase &PM, CodeGenOpt::Level OptLevel,
Evan Cheng42bf74b2009-03-25 01:47:28 +0000133 bool Verbose, raw_ostream &Out)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000134{
135 // Output assembly language.
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000136 PM.add(createMipsCodePrinterPass(Out, *this, OptLevel, Verbose));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000137 return false;
138}