Refactor PPC target to separate MC routines from Target routines.
[oota-llvm.git] / lib / Target / PowerPC / MCTargetDesc / PPCMCTargetDesc.cpp
1 //===-- PPCMCTargetDesc.cpp - PowerPC Target Descriptions -------*- C++ -*-===//
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 // This file provides PowerPC specific target descriptions.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "PPCMCTargetDesc.h"
15 #include "PPCMCAsmInfo.h"
16 #include "llvm/MC/MachineLocation.h"
17 #include "llvm/MC/MCInstrInfo.h"
18 #include "llvm/MC/MCRegisterInfo.h"
19 #include "llvm/MC/MCStreamer.h"
20 #include "llvm/MC/MCSubtargetInfo.h"
21 #include "llvm/Target/TargetRegistry.h"
22
23 #define GET_INSTRINFO_MC_DESC
24 #include "PPCGenInstrInfo.inc"
25
26 #define GET_SUBTARGETINFO_MC_DESC
27 #include "PPCGenSubtargetInfo.inc"
28
29 #define GET_REGINFO_MC_DESC
30 #include "PPCGenRegisterInfo.inc"
31
32 using namespace llvm;
33
34 static MCInstrInfo *createPPCMCInstrInfo() {
35   MCInstrInfo *X = new MCInstrInfo();
36   InitPPCMCInstrInfo(X);
37   return X;
38 }
39
40 static MCRegisterInfo *createPPCMCRegisterInfo(StringRef TT) {
41   Triple TheTriple(TT);
42   bool isPPC64 = (TheTriple.getArch() == Triple::ppc64);
43   unsigned Flavour = isPPC64 ? 0 : 1;
44   unsigned RA = isPPC64 ? PPC::LR8 : PPC::LR;
45
46   MCRegisterInfo *X = new MCRegisterInfo();
47   InitPPCMCRegisterInfo(X, RA, Flavour, Flavour);
48   return X;
49 }
50
51 static MCSubtargetInfo *createPPCMCSubtargetInfo(StringRef TT, StringRef CPU,
52                                                  StringRef FS) {
53   MCSubtargetInfo *X = new MCSubtargetInfo();
54   InitPPCMCSubtargetInfo(X, TT, CPU, FS);
55   return X;
56 }
57
58 static MCAsmInfo *createPPCMCAsmInfo(const Target &T, StringRef TT) {
59   Triple TheTriple(TT);
60   bool isPPC64 = TheTriple.getArch() == Triple::ppc64;
61
62   MCAsmInfo *MAI;
63   if (TheTriple.isOSDarwin())
64     MAI = new PPCMCAsmInfoDarwin(isPPC64);
65   else
66     MAI = new PPCLinuxMCAsmInfo(isPPC64);
67
68   // Initial state of the frame pointer is R1.
69   MachineLocation Dst(MachineLocation::VirtualFP);
70   MachineLocation Src(PPC::R1, 0);
71   MAI->addInitialFrameState(0, Dst, Src);
72
73   return MAI;
74 }
75
76 static MCCodeGenInfo *createPPCMCCodeGenInfo(StringRef TT, Reloc::Model RM,
77                                              CodeModel::Model CM) {
78   MCCodeGenInfo *X = new MCCodeGenInfo();
79
80   if (RM == Reloc::Default) {
81     Triple T(TT);
82     if (T.isOSDarwin())
83       RM = Reloc::DynamicNoPIC;
84     else
85       RM = Reloc::Static;
86   }
87   X->InitMCCodeGenInfo(RM, CM);
88   return X;
89 }
90
91 // This is duplicated code. Refactor this.
92 static MCStreamer *createMCStreamer(const Target &T, const std::string &TT,
93                                     MCContext &Ctx, TargetAsmBackend &TAB,
94                                     raw_ostream &OS,
95                                     MCCodeEmitter *Emitter,
96                                     bool RelaxAll,
97                                     bool NoExecStack) {
98   if (Triple(TT).isOSDarwin())
99     return createMachOStreamer(Ctx, TAB, OS, Emitter, RelaxAll);
100
101   return NULL;
102 }
103
104 extern "C" void LLVMInitializePowerPCTargetMC() {
105   // Register the MC asm info.
106   RegisterMCAsmInfoFn C(ThePPC32Target, createPPCMCAsmInfo);
107   RegisterMCAsmInfoFn D(ThePPC64Target, createPPCMCAsmInfo);  
108
109   // Register the MC codegen info.
110   TargetRegistry::RegisterMCCodeGenInfo(ThePPC32Target, createPPCMCCodeGenInfo);
111   TargetRegistry::RegisterMCCodeGenInfo(ThePPC64Target, createPPCMCCodeGenInfo);
112
113   // Register the MC instruction info.
114   TargetRegistry::RegisterMCInstrInfo(ThePPC32Target, createPPCMCInstrInfo);
115   TargetRegistry::RegisterMCInstrInfo(ThePPC64Target, createPPCMCInstrInfo);
116
117   // Register the MC register info.
118   TargetRegistry::RegisterMCRegInfo(ThePPC32Target, createPPCMCRegisterInfo);
119   TargetRegistry::RegisterMCRegInfo(ThePPC64Target, createPPCMCRegisterInfo);
120
121   // Register the MC subtarget info.
122   TargetRegistry::RegisterMCSubtargetInfo(ThePPC32Target,
123                                           createPPCMCSubtargetInfo);
124   TargetRegistry::RegisterMCSubtargetInfo(ThePPC64Target,
125                                           createPPCMCSubtargetInfo);
126
127   // Register the MC Code Emitter
128   TargetRegistry::RegisterCodeEmitter(ThePPC32Target, createPPCMCCodeEmitter);
129   TargetRegistry::RegisterCodeEmitter(ThePPC64Target, createPPCMCCodeEmitter);
130   
131     // Register the asm backend.
132   TargetRegistry::RegisterAsmBackend(ThePPC32Target, createPPCAsmBackend);
133   TargetRegistry::RegisterAsmBackend(ThePPC64Target, createPPCAsmBackend);
134   
135   // Register the object streamer.
136   TargetRegistry::RegisterObjectStreamer(ThePPC32Target, createMCStreamer);
137   TargetRegistry::RegisterObjectStreamer(ThePPC64Target, createMCStreamer);
138 }