PPCMCTargetDesc.cpp revision daa65f5e08e19b1fac5f476cef45223b1b4e5a8e
1//===-- PPCMCTargetDesc.cpp - PowerPC Target Descriptions -----------------===//
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 "InstPrinter/PPCInstPrinter.h"
17#include "llvm/MC/MachineLocation.h"
18#include "llvm/MC/MCCodeGenInfo.h"
19#include "llvm/MC/MCInstrInfo.h"
20#include "llvm/MC/MCRegisterInfo.h"
21#include "llvm/MC/MCStreamer.h"
22#include "llvm/MC/MCSubtargetInfo.h"
23#include "llvm/Support/ErrorHandling.h"
24#include "llvm/Support/TargetRegistry.h"
25
26#define GET_INSTRINFO_MC_DESC
27#include "PPCGenInstrInfo.inc"
28
29#define GET_SUBTARGETINFO_MC_DESC
30#include "PPCGenSubtargetInfo.inc"
31
32#define GET_REGINFO_MC_DESC
33#include "PPCGenRegisterInfo.inc"
34
35using namespace llvm;
36
37static MCInstrInfo *createPPCMCInstrInfo() {
38  MCInstrInfo *X = new MCInstrInfo();
39  InitPPCMCInstrInfo(X);
40  return X;
41}
42
43static MCRegisterInfo *createPPCMCRegisterInfo(StringRef TT) {
44  Triple TheTriple(TT);
45  bool isPPC64 = (TheTriple.getArch() == Triple::ppc64);
46  unsigned Flavour = isPPC64 ? 0 : 1;
47  unsigned RA = isPPC64 ? PPC::LR8 : PPC::LR;
48
49  MCRegisterInfo *X = new MCRegisterInfo();
50  InitPPCMCRegisterInfo(X, RA, Flavour, Flavour);
51  return X;
52}
53
54static MCSubtargetInfo *createPPCMCSubtargetInfo(StringRef TT, StringRef CPU,
55                                                 StringRef FS) {
56  MCSubtargetInfo *X = new MCSubtargetInfo();
57  InitPPCMCSubtargetInfo(X, TT, CPU, FS);
58  return X;
59}
60
61static MCAsmInfo *createPPCMCAsmInfo(const Target &T, StringRef TT) {
62  Triple TheTriple(TT);
63  bool isPPC64 = TheTriple.getArch() == Triple::ppc64;
64
65  MCAsmInfo *MAI;
66  if (TheTriple.isOSDarwin())
67    MAI = new PPCMCAsmInfoDarwin(isPPC64);
68  else
69    MAI = new PPCLinuxMCAsmInfo(isPPC64);
70
71  // Initial state of the frame pointer is R1.
72  MachineLocation Dst(MachineLocation::VirtualFP);
73  MachineLocation Src(PPC::R1, 0);
74  MAI->addInitialFrameState(0, Dst, Src);
75
76  return MAI;
77}
78
79static MCCodeGenInfo *createPPCMCCodeGenInfo(StringRef TT, Reloc::Model RM,
80                                             CodeModel::Model CM,
81                                             CodeGenOpt::Level OL) {
82  MCCodeGenInfo *X = new MCCodeGenInfo();
83
84  if (RM == Reloc::Default) {
85    Triple T(TT);
86    if (T.isOSDarwin())
87      RM = Reloc::DynamicNoPIC;
88    else
89      RM = Reloc::Static;
90  }
91  if (CM == CodeModel::Default) {
92    Triple T(TT);
93    if (!T.isOSDarwin() && T.getArch() == Triple::ppc64)
94      CM = CodeModel::Medium;
95  }
96  X->InitMCCodeGenInfo(RM, CM, OL);
97  return X;
98}
99
100// This is duplicated code. Refactor this.
101static MCStreamer *createMCStreamer(const Target &T, StringRef TT,
102                                    MCContext &Ctx, MCAsmBackend &MAB,
103                                    raw_ostream &OS,
104                                    MCCodeEmitter *Emitter,
105                                    bool RelaxAll,
106                                    bool NoExecStack) {
107  if (Triple(TT).isOSDarwin())
108    return createMachOStreamer(Ctx, MAB, OS, Emitter, RelaxAll);
109
110  return createELFStreamer(Ctx, MAB, OS, Emitter, RelaxAll, NoExecStack);
111}
112
113static MCInstPrinter *createPPCMCInstPrinter(const Target &T,
114                                             unsigned SyntaxVariant,
115                                             const MCAsmInfo &MAI,
116                                             const MCInstrInfo &MII,
117                                             const MCRegisterInfo &MRI,
118                                             const MCSubtargetInfo &STI) {
119  return new PPCInstPrinter(MAI, MII, MRI, SyntaxVariant);
120}
121
122extern "C" void LLVMInitializePowerPCTargetMC() {
123  // Register the MC asm info.
124  RegisterMCAsmInfoFn C(ThePPC32Target, createPPCMCAsmInfo);
125  RegisterMCAsmInfoFn D(ThePPC64Target, createPPCMCAsmInfo);
126
127  // Register the MC codegen info.
128  TargetRegistry::RegisterMCCodeGenInfo(ThePPC32Target, createPPCMCCodeGenInfo);
129  TargetRegistry::RegisterMCCodeGenInfo(ThePPC64Target, createPPCMCCodeGenInfo);
130
131  // Register the MC instruction info.
132  TargetRegistry::RegisterMCInstrInfo(ThePPC32Target, createPPCMCInstrInfo);
133  TargetRegistry::RegisterMCInstrInfo(ThePPC64Target, createPPCMCInstrInfo);
134
135  // Register the MC register info.
136  TargetRegistry::RegisterMCRegInfo(ThePPC32Target, createPPCMCRegisterInfo);
137  TargetRegistry::RegisterMCRegInfo(ThePPC64Target, createPPCMCRegisterInfo);
138
139  // Register the MC subtarget info.
140  TargetRegistry::RegisterMCSubtargetInfo(ThePPC32Target,
141                                          createPPCMCSubtargetInfo);
142  TargetRegistry::RegisterMCSubtargetInfo(ThePPC64Target,
143                                          createPPCMCSubtargetInfo);
144
145  // Register the MC Code Emitter
146  TargetRegistry::RegisterMCCodeEmitter(ThePPC32Target, createPPCMCCodeEmitter);
147  TargetRegistry::RegisterMCCodeEmitter(ThePPC64Target, createPPCMCCodeEmitter);
148
149    // Register the asm backend.
150  TargetRegistry::RegisterMCAsmBackend(ThePPC32Target, createPPCAsmBackend);
151  TargetRegistry::RegisterMCAsmBackend(ThePPC64Target, createPPCAsmBackend);
152
153  // Register the object streamer.
154  TargetRegistry::RegisterMCObjectStreamer(ThePPC32Target, createMCStreamer);
155  TargetRegistry::RegisterMCObjectStreamer(ThePPC64Target, createMCStreamer);
156
157  // Register the MCInstPrinter.
158  TargetRegistry::RegisterMCInstPrinter(ThePPC32Target, createPPCMCInstPrinter);
159  TargetRegistry::RegisterMCInstPrinter(ThePPC64Target, createPPCMCInstPrinter);
160}
161