LLVM  3.7.0
HexagonMCTargetDesc.cpp
Go to the documentation of this file.
1 //===-- HexagonMCTargetDesc.cpp - Hexagon 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 Hexagon specific target descriptions.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "HexagonMCTargetDesc.h"
15 #include "Hexagon.h"
16 #include "HexagonMCAsmInfo.h"
17 #include "HexagonMCELFStreamer.h"
19 #include "llvm/MC/MCCodeGenInfo.h"
20 #include "llvm/MC/MCContext.h"
21 #include "llvm/MC/MCELFStreamer.h"
22 #include "llvm/MC/MCInstrInfo.h"
24 #include "llvm/MC/MCRegisterInfo.h"
25 #include "llvm/MC/MCStreamer.h"
28 #include "llvm/Support/ELF.h"
31 
32 using namespace llvm;
33 
34 #define GET_INSTRINFO_MC_DESC
35 #include "HexagonGenInstrInfo.inc"
36 
37 #define GET_SUBTARGETINFO_MC_DESC
38 #include "HexagonGenSubtargetInfo.inc"
39 
40 #define GET_REGINFO_MC_DESC
41 #include "HexagonGenRegisterInfo.inc"
42 
44  MCInstrInfo *X = new MCInstrInfo();
45  InitHexagonMCInstrInfo(X);
46  return X;
47 }
48 
51  InitHexagonMCRegisterInfo(X, Hexagon::R0);
52  return X;
53 }
54 
55 static MCSubtargetInfo *
57  return createHexagonMCSubtargetInfoImpl(TT, CPU, FS);
58 }
59 
60 namespace {
61 class HexagonTargetAsmStreamer : public HexagonTargetStreamer {
62 public:
63  HexagonTargetAsmStreamer(MCStreamer &S,
64  formatted_raw_ostream &, bool,
65  MCInstPrinter &)
66  : HexagonTargetStreamer(S) {}
67  void prettyPrintAsm(MCInstPrinter &InstPrinter, raw_ostream &OS,
68  const MCInst &Inst, const MCSubtargetInfo &STI) override {
69  assert(HexagonMCInstrInfo::isBundle(Inst));
71  std::string Buffer;
72  {
73  raw_string_ostream TempStream(Buffer);
74  InstPrinter.printInst(&Inst, TempStream, "", STI);
75  }
76  StringRef Contents(Buffer);
77  auto PacketBundle = Contents.rsplit('\n');
78  auto HeadTail = PacketBundle.first.split('\n');
79  auto Preamble = "\t{\n\t\t";
80  auto Separator = "";
81  while(!HeadTail.first.empty()) {
82  OS << Separator;
83  StringRef Inst;
84  auto Duplex = HeadTail.first.split('\v');
85  if(!Duplex.second.empty()){
86  OS << Duplex.first << "\n";
87  Inst = Duplex.second;
88  }
89  else {
90  if(!HeadTail.first.startswith("immext"))
91  Inst = Duplex.first;
92  }
93  OS << Preamble;
94  OS << Inst;
95  HeadTail = HeadTail.second.split('\n');
96  Preamble = "";
97  Separator = "\n\t\t";
98  }
99  if(HexagonMCInstrInfo::bundleSize(Inst) != 0)
100  OS << "\n\t}" << PacketBundle.second;
101  }
102 };
103 }
104 
105 namespace {
106 class HexagonTargetELFStreamer : public HexagonTargetStreamer {
107 public:
108  MCELFStreamer &getStreamer() {
109  return static_cast<MCELFStreamer &>(Streamer);
110  }
111  HexagonTargetELFStreamer(MCStreamer &S, MCSubtargetInfo const &STI)
112  : HexagonTargetStreamer(S) {
113  auto Bits = STI.getFeatureBits();
114  unsigned Flags;
115  if (Bits.to_ullong() & llvm::Hexagon::ArchV5)
116  Flags = ELF::EF_HEXAGON_MACH_V5;
117  else
118  Flags = ELF::EF_HEXAGON_MACH_V4;
119  getStreamer().getAssembler().setELFHeaderEFlags(Flags);
120  }
121  void EmitCommonSymbolSorted(MCSymbol *Symbol, uint64_t Size,
122  unsigned ByteAlignment,
123  unsigned AccessSize) override {
124  HexagonMCELFStreamer &HexagonELFStreamer =
125  static_cast<HexagonMCELFStreamer &>(getStreamer());
126  HexagonELFStreamer.HexagonMCEmitCommonSymbol(Symbol, Size, ByteAlignment,
127  AccessSize);
128  }
129  void EmitLocalCommonSymbolSorted(MCSymbol *Symbol, uint64_t Size,
130  unsigned ByteAlignment,
131  unsigned AccessSize) override {
132  HexagonMCELFStreamer &HexagonELFStreamer =
133  static_cast<HexagonMCELFStreamer &>(getStreamer());
134  HexagonELFStreamer.HexagonMCEmitLocalCommonSymbol(
135  Symbol, Size, ByteAlignment, AccessSize);
136  }
137 };
138 }
139 
141  const Triple &TT) {
142  MCAsmInfo *MAI = new HexagonMCAsmInfo(TT);
143 
144  // VirtualFP = (R30 + #0).
145  MCCFIInstruction Inst =
146  MCCFIInstruction::createDefCfa(nullptr, Hexagon::R30, 0);
147  MAI->addInitialFrameState(Inst);
148 
149  return MAI;
150 }
151 
154  CodeModel::Model CM,
155  CodeGenOpt::Level OL) {
156  MCCodeGenInfo *X = new MCCodeGenInfo();
157  // For the time being, use static relocations, since there's really no
158  // support for PIC yet.
159  X->initMCCodeGenInfo(Reloc::Static, CM, OL);
160  return X;
161 }
162 
164  unsigned SyntaxVariant,
165  const MCAsmInfo &MAI,
166  const MCInstrInfo &MII,
167  const MCRegisterInfo &MRI) {
168  if (SyntaxVariant == 0)
169  return (new HexagonInstPrinter(MAI, MII, MRI));
170  else
171  return nullptr;
172 }
173 
176  MCInstPrinter *InstPrint,
177  bool IsVerboseAsm) {
178  return new HexagonTargetAsmStreamer(S, OS, IsVerboseAsm, *InstPrint);
179 }
180 
181 static MCStreamer *createMCStreamer(Triple const &T, MCContext &Context,
183  MCCodeEmitter *Emitter, bool RelaxAll) {
184  return createHexagonELFStreamer(Context, MAB, OS, Emitter);
185 }
186 
187 static MCTargetStreamer *
189  return new HexagonTargetELFStreamer(S, STI);
190 }
191 
192 // Force static initialization.
193 extern "C" void LLVMInitializeHexagonTargetMC() {
194  // Register the MC asm info.
196 
197  // Register the MC codegen info.
200 
201  // Register the MC instruction info.
204 
205  // Register the MC register info.
208 
209  // Register the MC subtarget info.
212 
213  // Register the MC Code Emitter
216 
217  // Register the asm backend
220 
221  // Register the obj streamer
223 
224  // Register the asm streamer
227 
228  // Register the MC Inst Printer
231 
234 }
virtual void printInst(const MCInst *MI, raw_ostream &OS, StringRef Annot, const MCSubtargetInfo &STI)=0
Print the specified MCInst to the specified raw_ostream.
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:39
static MCTargetStreamer * createMCAsmTargetStreamer(MCStreamer &S, formatted_raw_ostream &OS, MCInstPrinter *InstPrint, bool IsVerboseAsm)
formatted_raw_ostream - A raw_ostream that wraps another one and keeps track of line and column posit...
static MCRegisterInfo * createHexagonMCRegisterInfo(const Triple &TT)
Target specific streamer interface.
Definition: MCStreamer.h:73
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:450
bool isBundle(MCInst const &MCI)
static void RegisterMCInstPrinter(Target &T, Target::MCInstPrinterCtorTy Fn)
RegisterMCInstPrinter - Register a MCInstPrinter implementation for the given target.
void LLVMInitializeHexagonTargetMC()
static void RegisterAsmTargetStreamer(Target &T, Target::AsmTargetStreamerCtorTy Fn)
static MCTargetStreamer * createHexagonObjectTargetStreamer(MCStreamer &S, MCSubtargetInfo const &STI)
#define HEXAGON_PACKET_SIZE
Definition: Hexagon.h:33
MCAsmBackend * createHexagonAsmBackend(Target const &T, MCRegisterInfo const &, const Triple &TT, StringRef CPU)
Context object for machine code objects.
Definition: MCContext.h:48
void addInitialFrameState(const MCCFIInstruction &Inst)
Definition: MCAsmInfo.h:534
MCInstrInfo * createHexagonMCInstrInfo()
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:150
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:58
static void RegisterMCAsmBackend(Target &T, Target::MCAsmBackendCtorTy Fn)
RegisterMCAsmBackend - Register a MCAsmBackend implementation for the given target.
Streaming machine code generation interface.
Definition: MCStreamer.h:157
static MCCFIInstruction createDefCfa(MCSymbol *L, unsigned Register, int Offset)
.cfi_def_cfa defines a rule for computing CFA as: take address from Register and add Offset to it...
Definition: MCDwarf.h:354
static MCInstPrinter * createHexagonMCInstPrinter(const Triple &T, unsigned SyntaxVariant, const MCAsmInfo &MAI, const MCInstrInfo &MII, const MCRegisterInfo &MRI)
MCCodeEmitter * createHexagonMCCodeEmitter(MCInstrInfo const &MCII, MCRegisterInfo const &MRI, MCContext &MCT)
MCCodeEmitter - Generic instruction encoding interface.
Definition: MCCodeEmitter.h:23
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:24
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
static void RegisterMCCodeGenInfo(Target &T, Target::MCCodeGenInfoCtorFnTy Fn)
RegisterMCCodeGenInfo - Register a MCCodeGenInfo implementation for the given target.
static void RegisterMCSubtargetInfo(Target &T, Target::MCSubtargetInfoCtorFnTy Fn)
RegisterMCSubtargetInfo - Register a MCSubtargetInfo implementation for the given target...
static void RegisterObjectTargetStreamer(Target &T, Target::ObjectTargetStreamerCtorTy Fn)
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
Prints bundles as a newline separated list of individual instructions Duplexes are separated by a ver...
void HexagonMCEmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size, unsigned ByteAlignment, unsigned AccessSize)
static void RegisterMCCodeEmitter(Target &T, Target::MCCodeEmitterCtorTy Fn)
RegisterMCCodeEmitter - Register a MCCodeEmitter implementation for the given target.
const FeatureBitset & getFeatureBits() const
getFeatureBits - Return the feature bits.
static void RegisterMCRegInfo(Target &T, Target::MCRegInfoCtorFnTy Fn)
RegisterMCRegInfo - Register a MCRegisterInfo implementation for the given target.
MCStreamer * createHexagonELFStreamer(MCContext &Context, MCAsmBackend &MAB, raw_pwrite_stream &OS, MCCodeEmitter *CE)
RegisterMCAsmInfoFn - Helper template for registering a target assembly info implementation.
This is an instance of a target assembly language printer that converts an MCInst to valid target ass...
Definition: MCInstPrinter.h:38
void initMCCodeGenInfo(Reloc::Model RM=Reloc::Default, CodeModel::Model CM=CodeModel::Default, CodeGenOpt::Level OL=CodeGenOpt::Default)
static MCSubtargetInfo * createHexagonMCSubtargetInfo(const Triple &TT, StringRef CPU, StringRef FS)
static void RegisterMCInstrInfo(Target &T, Target::MCInstrInfoCtorFnTy Fn)
RegisterMCInstrInfo - Register a MCInstrInfo implementation for the given target. ...
MCSubtargetInfo - Generic base class for all target subtargets.
size_t bundleSize(MCInst const &MCI)
static void RegisterELFStreamer(Target &T, Target::ELFStreamerCtorTy Fn)
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:321
cl::opt< bool > RelaxAll("mc-relax-all", cl::desc("When used with filetype=obj, ""relax all fixups in the emitted object file"))
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:465
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:34
static MCAsmInfo * createHexagonMCAsmInfo(const MCRegisterInfo &MRI, const Triple &TT)
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:38
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
Target TheHexagonTarget
static MCStreamer * createMCStreamer(Triple const &T, MCContext &Context, MCAsmBackend &MAB, raw_pwrite_stream &OS, MCCodeEmitter *Emitter, bool RelaxAll)
void HexagonMCEmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, unsigned ByteAlignment, unsigned AccessSize)
static MCCodeGenInfo * createHexagonMCCodeGenInfo(const Triple &TT, Reloc::Model RM, CodeModel::Model CM, CodeGenOpt::Level OL)