1 //===-- AArch64MCTargetDesc.cpp - AArch64 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 AArch64 specific target descriptions.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "AArch64MCTargetDesc.h"
15 #include "AArch64ELFStreamer.h"
16 #include "AArch64MCAsmInfo.h"
17 #include "InstPrinter/AArch64InstPrinter.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 using namespace llvm;
27 
28 #define GET_INSTRINFO_MC_DESC
29 #include "AArch64GenInstrInfo.inc"
30 
31 #define GET_SUBTARGETINFO_MC_DESC
32 #include "AArch64GenSubtargetInfo.inc"
33 
34 #define GET_REGINFO_MC_DESC
35 #include "AArch64GenRegisterInfo.inc"
36 
37 static MCInstrInfo *createAArch64MCInstrInfo() {
38   MCInstrInfo *X = new MCInstrInfo();
39   InitAArch64MCInstrInfo(X);
40   return X;
41 }
42 
43 static MCSubtargetInfo *
44 createAArch64MCSubtargetInfo(const Triple &TT, StringRef CPU, StringRef FS) {
45   if (CPU.empty())
46     CPU = "generic";
47 
48   return createAArch64MCSubtargetInfoImpl(TT, CPU, FS);
49 }
50 
51 static MCRegisterInfo *createAArch64MCRegisterInfo(const Triple &Triple) {
52   MCRegisterInfo *X = new MCRegisterInfo();
53   InitAArch64MCRegisterInfo(X, AArch64::LR);
54   return X;
55 }
56 
57 static MCAsmInfo *createAArch64MCAsmInfo(const MCRegisterInfo &MRI,
58                                          const Triple &TheTriple) {
59   MCAsmInfo *MAI;
60   if (TheTriple.isOSBinFormatMachO())
61     MAI = new AArch64MCAsmInfoDarwin();
62   else {
63     assert(TheTriple.isOSBinFormatELF() && "Only expect Darwin or ELF");
64     MAI = new AArch64MCAsmInfoELF(TheTriple);
65   }
66 
67   // Initial state of the frame pointer is SP.
68   unsigned Reg = MRI.getDwarfRegNum(AArch64::SP, true);
69   MCCFIInstruction Inst = MCCFIInstruction::createDefCfa(nullptr, Reg, 0);
70   MAI->addInitialFrameState(Inst);
71 
72   return MAI;
73 }
74 
75 static MCCodeGenInfo *createAArch64MCCodeGenInfo(const Triple &TT,
76                                                  Reloc::Model RM,
77                                                  CodeModel::Model CM,
78                                                  CodeGenOpt::Level OL) {
79   assert((TT.isOSBinFormatELF() || TT.isOSBinFormatMachO()) &&
80          "Only expect Darwin and ELF targets");
81 
82   if (CM == CodeModel::Default)
83     CM = CodeModel::Small;
84   // The default MCJIT memory managers make no guarantees about where they can
85   // find an executable page; JITed code needs to be able to refer to globals
86   // no matter how far away they are.
87   else if (CM == CodeModel::JITDefault)
88     CM = CodeModel::Large;
89   else if (CM != CodeModel::Small && CM != CodeModel::Large)
90     report_fatal_error(
91         "Only small and large code models are allowed on AArch64");
92 
93   MCCodeGenInfo *X = new MCCodeGenInfo();
94   X->initMCCodeGenInfo(RM, CM, OL);
95   return X;
96 }
97 
98 static MCInstPrinter *createAArch64MCInstPrinter(const Triple &T,
99                                                  unsigned SyntaxVariant,
100                                                  const MCAsmInfo &MAI,
101                                                  const MCInstrInfo &MII,
102                                                  const MCRegisterInfo &MRI) {
103   if (SyntaxVariant == 0)
104     return new AArch64InstPrinter(MAI, MII, MRI);
105   if (SyntaxVariant == 1)
106     return new AArch64AppleInstPrinter(MAI, MII, MRI);
107 
108   return nullptr;
109 }
110 
111 static MCStreamer *createELFStreamer(const Triple &T, MCContext &Ctx,
112                                      MCAsmBackend &TAB, raw_pwrite_stream &OS,
113                                      MCCodeEmitter *Emitter, bool RelaxAll) {
114   return createAArch64ELFStreamer(Ctx, TAB, OS, Emitter, RelaxAll);
115 }
116 
117 static MCStreamer *createMachOStreamer(MCContext &Ctx, MCAsmBackend &TAB,
118                                        raw_pwrite_stream &OS,
119                                        MCCodeEmitter *Emitter, bool RelaxAll,
120                                        bool DWARFMustBeAtTheEnd) {
121   return createMachOStreamer(Ctx, TAB, OS, Emitter, RelaxAll,
122                              DWARFMustBeAtTheEnd,
123                              /*LabelSections*/ true);
124 }
125 
126 // Force static initialization.
127 extern "C" void LLVMInitializeAArch64TargetMC() {
128   for (Target *T :
129        {&TheAArch64leTarget, &TheAArch64beTarget, &TheARM64Target}) {
130     // Register the MC asm info.
131     RegisterMCAsmInfoFn X(*T, createAArch64MCAsmInfo);
132 
133     // Register the MC codegen info.
134     TargetRegistry::RegisterMCCodeGenInfo(*T, createAArch64MCCodeGenInfo);
135 
136     // Register the MC instruction info.
137     TargetRegistry::RegisterMCInstrInfo(*T, createAArch64MCInstrInfo);
138 
139     // Register the MC register info.
140     TargetRegistry::RegisterMCRegInfo(*T, createAArch64MCRegisterInfo);
141 
142     // Register the MC subtarget info.
143     TargetRegistry::RegisterMCSubtargetInfo(*T, createAArch64MCSubtargetInfo);
144 
145     // Register the MC Code Emitter
146     TargetRegistry::RegisterMCCodeEmitter(*T, createAArch64MCCodeEmitter);
147 
148     // Register the obj streamers.
149     TargetRegistry::RegisterELFStreamer(*T, createELFStreamer);
150     TargetRegistry::RegisterMachOStreamer(*T, createMachOStreamer);
151 
152     // Register the obj target streamer.
153     TargetRegistry::RegisterObjectTargetStreamer(
154         *T, createAArch64ObjectTargetStreamer);
155 
156     // Register the asm streamer.
157     TargetRegistry::RegisterAsmTargetStreamer(*T,
158                                               createAArch64AsmTargetStreamer);
159     // Register the MCInstPrinter.
160     TargetRegistry::RegisterMCInstPrinter(*T, createAArch64MCInstPrinter);
161   }
162 
163   // Register the asm backend.
164   for (Target *T : {&TheAArch64leTarget, &TheARM64Target})
165     TargetRegistry::RegisterMCAsmBackend(*T, createAArch64leAsmBackend);
166   TargetRegistry::RegisterMCAsmBackend(TheAArch64beTarget,
167                                        createAArch64beAsmBackend);
168 }
169