LLVM  5.0.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 
15 #include "Hexagon.h"
16 #include "HexagonTargetStreamer.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/BinaryFormat/ELF.h"
23 #include "llvm/MC/MCContext.h"
24 #include "llvm/MC/MCDwarf.h"
25 #include "llvm/MC/MCELFStreamer.h"
27 #include "llvm/MC/MCInstrInfo.h"
28 #include "llvm/MC/MCRegisterInfo.h"
29 #include "llvm/MC/MCStreamer.h"
34 #include <cassert>
35 #include <cstdint>
36 #include <new>
37 #include <string>
38 
39 using namespace llvm;
40 
41 #define GET_INSTRINFO_MC_DESC
42 #include "HexagonGenInstrInfo.inc"
43 
44 #define GET_SUBTARGETINFO_MC_DESC
45 #include "HexagonGenSubtargetInfo.inc"
46 
47 #define GET_REGINFO_MC_DESC
48 #include "HexagonGenRegisterInfo.inc"
49 
51  ("mno-compound",
52  cl::desc("Disable looking for compound instructions for Hexagon"));
53 
55  ("mno-pairing",
56  cl::desc("Disable looking for duplex instructions for Hexagon"));
57 
59  cl::desc("Build for Hexagon V4"));
60 
62  cl::desc("Build for Hexagon V5"));
63 
65  cl::desc("Build for Hexagon V55"));
66 
68  cl::desc("Build for Hexagon V60"));
69 
71  cl::desc("Build for Hexagon V62"));
72 
73 static cl::opt<bool> EnableHVX("mhvx", cl::Hidden, cl::init(false),
74  cl::desc("Enable Hexagon Vector Extension (HVX)"));
75 
76 static StringRef DefaultArch = "hexagonv60";
77 
80  return "hexagonv4";
82  return "hexagonv5";
84  return "hexagonv55";
86  return "hexagonv60";
88  return "hexagonv62";
89  return "";
90 }
91 
94  if (!ArchV.empty() && !CPU.empty()) {
95  if (ArchV != CPU)
96  report_fatal_error("conflicting architectures specified.");
97  return CPU;
98  }
99  if (ArchV.empty()) {
100  if (CPU.empty())
101  CPU = DefaultArch;
102  return CPU;
103  }
104  return ArchV;
105 }
106 
107 unsigned llvm::HexagonGetLastSlot() { return HexagonItinerariesV4FU::SLOT3; }
108 
109 namespace {
110 
111 class HexagonTargetAsmStreamer : public HexagonTargetStreamer {
112 public:
113  HexagonTargetAsmStreamer(MCStreamer &S,
115  bool isVerboseAsm,
116  MCInstPrinter &IP)
117  : HexagonTargetStreamer(S) {}
118 
119  void prettyPrintAsm(MCInstPrinter &InstPrinter, raw_ostream &OS,
120  const MCInst &Inst, const MCSubtargetInfo &STI) override {
123  std::string Buffer;
124  {
125  raw_string_ostream TempStream(Buffer);
126  InstPrinter.printInst(&Inst, TempStream, "", STI);
127  }
128  StringRef Contents(Buffer);
129  auto PacketBundle = Contents.rsplit('\n');
130  auto HeadTail = PacketBundle.first.split('\n');
131  StringRef Separator = "\n";
132  StringRef Indent = "\t\t";
133  OS << "\t{\n";
134  while (!HeadTail.first.empty()) {
135  StringRef InstTxt;
136  auto Duplex = HeadTail.first.split('\v');
137  if (!Duplex.second.empty()) {
138  OS << Indent << Duplex.first << Separator;
139  InstTxt = Duplex.second;
140  } else if (!HeadTail.first.trim().startswith("immext")) {
141  InstTxt = Duplex.first;
142  }
143  if (!InstTxt.empty())
144  OS << Indent << InstTxt << Separator;
145  HeadTail = HeadTail.second.split('\n');
146  }
147  OS << "\t}" << PacketBundle.second;
148  }
149 };
150 
151 class HexagonTargetELFStreamer : public HexagonTargetStreamer {
152 public:
153  MCELFStreamer &getStreamer() {
154  return static_cast<MCELFStreamer &>(Streamer);
155  }
156  HexagonTargetELFStreamer(MCStreamer &S, MCSubtargetInfo const &STI)
157  : HexagonTargetStreamer(S) {
158  MCAssembler &MCA = getStreamer().getAssembler();
160  }
161 
162 
163  void EmitCommonSymbolSorted(MCSymbol *Symbol, uint64_t Size,
164  unsigned ByteAlignment,
165  unsigned AccessSize) override {
166  HexagonMCELFStreamer &HexagonELFStreamer =
167  static_cast<HexagonMCELFStreamer &>(getStreamer());
168  HexagonELFStreamer.HexagonMCEmitCommonSymbol(Symbol, Size, ByteAlignment,
169  AccessSize);
170  }
171 
172  void EmitLocalCommonSymbolSorted(MCSymbol *Symbol, uint64_t Size,
173  unsigned ByteAlignment,
174  unsigned AccessSize) override {
175  HexagonMCELFStreamer &HexagonELFStreamer =
176  static_cast<HexagonMCELFStreamer &>(getStreamer());
177  HexagonELFStreamer.HexagonMCEmitLocalCommonSymbol(
178  Symbol, Size, ByteAlignment, AccessSize);
179  }
180 };
181 
182 } // end anonymous namespace
183 
185  MCInstrInfo *X = new MCInstrInfo();
186  InitHexagonMCInstrInfo(X);
187  return X;
188 }
189 
192  InitHexagonMCRegisterInfo(X, Hexagon::R31);
193  return X;
194 }
195 
197  const Triple &TT) {
198  MCAsmInfo *MAI = new HexagonMCAsmInfo(TT);
199 
200  // VirtualFP = (R30 + #0).
201  MCCFIInstruction Inst =
203  MRI.getDwarfRegNum(Hexagon::R30, true), 0);
204  MAI->addInitialFrameState(Inst);
205 
206  return MAI;
207 }
208 
210  unsigned SyntaxVariant,
211  const MCAsmInfo &MAI,
212  const MCInstrInfo &MII,
213  const MCRegisterInfo &MRI)
214 {
215  if (SyntaxVariant == 0)
216  return new HexagonInstPrinter(MAI, MII, MRI);
217  else
218  return nullptr;
219 }
220 
221 static MCTargetStreamer *
223  MCInstPrinter *IP, bool IsVerboseAsm) {
224  return new HexagonTargetAsmStreamer(S, OS, IsVerboseAsm, *IP);
225 }
226 
229  MCAsmBackend &MAB,
230  raw_pwrite_stream &OS,
231  MCCodeEmitter *Emitter,
232  bool RelaxAll) {
233  return createHexagonELFStreamer(T, Context, MAB, OS, Emitter);
234 }
235 
236 static MCTargetStreamer *
238  return new HexagonTargetELFStreamer(S, STI);
239 }
240 
242  uint64_t FB = STI->getFeatureBits().to_ullong();
243  if (FB & (1ULL << F))
244  STI->ToggleFeature(F);
245 }
246 
248  uint64_t FB = STI->getFeatureBits().to_ullong();
249  return (FB & (1ULL << F)) != 0;
250 }
251 
253  StringRef CPUName = Hexagon_MC::selectHexagonCPU(TT, CPU);
254  StringRef FS = "";
255  if (EnableHVX) {
256  if (CPUName.equals_lower("hexagonv60") ||
257  CPUName.equals_lower("hexagonv62"))
258  FS = "+hvx";
259  }
260  return FS;
261 }
262 
263 static bool isCPUValid(std::string CPU)
264 {
265  std::vector<std::string> table
266  {
267  "hexagonv4",
268  "hexagonv5",
269  "hexagonv55",
270  "hexagonv60",
271  "hexagonv62",
272  };
273 
274  return std::find(table.begin(), table.end(), CPU) != table.end();
275 }
276 
278  StringRef CPU,
279  StringRef FS) {
280  StringRef ArchFS = (FS.size()) ? FS : Hexagon_MC::ParseHexagonTriple(TT, CPU);
281  StringRef CPUName = Hexagon_MC::selectHexagonCPU(TT, CPU);
282  if (!isCPUValid(CPUName.str())) {
283  errs() << "error: invalid CPU \"" << CPUName.str().c_str()
284  << "\" specified\n";
285  return nullptr;
286  }
287 
288  MCSubtargetInfo *X = createHexagonMCSubtargetInfoImpl(TT, CPUName, ArchFS);
289  if (X->getFeatureBits()[Hexagon::ExtensionHVXDbl]) {
291  X->setFeatureBits(Features.set(Hexagon::ExtensionHVX));
292  }
293  return X;
294 }
295 
297  static std::map<StringRef,unsigned> ElfFlags = {
298  {"hexagonv4", ELF::EF_HEXAGON_MACH_V4},
299  {"hexagonv5", ELF::EF_HEXAGON_MACH_V5},
300  {"hexagonv55", ELF::EF_HEXAGON_MACH_V55},
301  {"hexagonv60", ELF::EF_HEXAGON_MACH_V60},
302  {"hexagonv62", ELF::EF_HEXAGON_MACH_V62},
303  };
304 
305  auto F = ElfFlags.find(STI.getCPU());
306  assert(F != ElfFlags.end() && "Unrecognized Architecture");
307  return F->second;
308 }
309 
310 namespace {
311 class HexagonMCInstrAnalysis : public MCInstrAnalysis {
312 public:
313  HexagonMCInstrAnalysis(MCInstrInfo const *Info) : MCInstrAnalysis(Info) {}
314 
315  bool isUnconditionalBranch(MCInst const &Inst) const override {
316  //assert(!HexagonMCInstrInfo::isBundle(Inst));
318  }
319 
320  bool isConditionalBranch(MCInst const &Inst) const override {
321  //assert(!HexagonMCInstrInfo::isBundle(Inst));
323  }
324 
325  bool evaluateBranch(MCInst const &Inst, uint64_t Addr,
326  uint64_t Size, uint64_t &Target) const override {
327  //assert(!HexagonMCInstrInfo::isBundle(Inst));
328  if(!HexagonMCInstrInfo::isExtendable(*Info, Inst))
329  return false;
330  auto const &Extended(HexagonMCInstrInfo::getExtendableOperand(*Info, Inst));
331  assert(Extended.isExpr());
332  int64_t Value;
333  if(!Extended.getExpr()->evaluateAsAbsolute(Value))
334  return false;
335  Target = Value;
336  return true;
337  }
338 };
339 }
340 
342  return new HexagonMCInstrAnalysis(Info);
343 }
344 
345 // Force static initialization.
346 extern "C" void LLVMInitializeHexagonTargetMC() {
347  // Register the MC asm info.
349 
350  // Register the MC instruction info.
353 
354  // Register the MC register info.
357 
358  // Register the MC subtarget info.
361 
362  // Register the MC Code Emitter
365 
366  // Register the asm backend
369 
370 
371  // Register the MC instruction analyzer.
374 
375  // Register the obj streamer
378 
379  // Register the obj target streamer
382 
383  // Register the asm streamer
386 
387  // Register the MC Inst Printer
390 }
void setELFHeaderEFlags(unsigned Flags)
Definition: MCAssembler.h:241
int getDwarfRegNum(unsigned RegNum, bool isEH) const
Map a target register to an equivalent dwarf register number.
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
static cl::opt< bool > HexagonV55ArchVariant("mv55", cl::Hidden, cl::init(false), cl::desc("Build for Hexagon V55"))
static cl::opt< bool > HexagonV5ArchVariant("mv5", cl::Hidden, cl::init(false), cl::desc("Build for Hexagon V5"))
LLVMContext & Context
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:103
void setFeatureBits(const FeatureBitset &FeatureBits_)
setFeatureBits - Set the feature bits.
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:42
formatted_raw_ostream - A raw_ostream that wraps another one and keeps track of line and column posit...
static void RegisterMCInstrAnalysis(Target &T, Target::MCInstrAnalysisCtorFnTy Fn)
RegisterMCInstrAnalysis - Register a MCInstrAnalysis implementation for the given target...
Target specific streamer interface.
Definition: MCStreamer.h:78
StringRef selectHexagonCPU(const Triple &TT, StringRef CPU)
LLVM_NODISCARD bool equals_lower(StringRef RHS) const
equals_lower - Check for string equality, ignoring case.
Definition: StringRef.h:176
bool isBundle(MCInst const &MCI)
unsigned HexagonGetLastSlot()
static cl::opt< bool > HexagonV62ArchVariant("mv62", cl::Hidden, cl::init(false), cl::desc("Build for Hexagon V62"))
static void RegisterMCInstPrinter(Target &T, Target::MCInstPrinterCtorTy Fn)
RegisterMCInstPrinter - Register a MCInstPrinter implementation for the given target.
MCAsmBackend * createHexagonAsmBackend(const Target &T, const MCRegisterInfo &MRI, const Triple &TT, StringRef CPU, const MCTargetOptions &Options)
void LLVMInitializeHexagonTargetMC()
static void RegisterAsmTargetStreamer(Target &T, Target::AsmTargetStreamerCtorTy Fn)
MCCodeEmitter * createHexagonMCCodeEmitter(const MCInstrInfo &MCII, const MCRegisterInfo &MRI, MCContext &MCT)
MCStreamer * createHexagonELFStreamer(Triple const &TT, MCContext &Context, MCAsmBackend &MAB, raw_pwrite_stream &OS, MCCodeEmitter *CE)
#define HEXAGON_PACKET_SIZE
Definition: Hexagon.h:33
StringRef getCPU() const
getCPU - Return the CPU string.
static cl::opt< bool > HexagonV4ArchVariant("mv4", cl::Hidden, cl::init(false), cl::desc("Build for Hexagon V4"))
Context object for machine code objects.
Definition: MCContext.h:59
#define F(x, y, z)
Definition: MD5.cpp:55
static MCInstrAnalysis * createHexagonMCInstrAnalysis(const MCInstrInfo *Info)
static StringRef DefaultArch
void addInitialFrameState(const MCCFIInstruction &Inst)
Definition: MCAsmInfo.h:582
MCInstrInfo * createHexagonMCInstrInfo()
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:159
virtual bool isUnconditionalBranch(const MCInst &Inst) const
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:56
FeatureBitset ToggleFeature(uint64_t FB)
ToggleFeature - Toggle a feature and returns the re-computed feature bits.
static void RegisterMCAsmBackend(Target &T, Target::MCAsmBackendCtorTy Fn)
RegisterMCAsmBackend - Register a MCAsmBackend implementation for the given target.
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:401
Streaming machine code generation interface.
Definition: MCStreamer.h:167
unsigned const MachineRegisterInfo * MRI
Container class for subtarget features.
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:377
static MCInstPrinter * createHexagonMCInstPrinter(const Triple &T, unsigned SyntaxVariant, const MCAsmInfo &MAI, const MCInstrInfo &MII, const MCRegisterInfo &MRI)
MCCodeEmitter - Generic instruction encoding interface.
Definition: MCCodeEmitter.h:22
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:24
cl::opt< bool > HexagonDisableCompound
MCSubtargetInfo * createHexagonMCSubtargetInfo(const Triple &TT, StringRef CPU, StringRef FS)
Create a Hexagon MCSubtargetInfo instance.
#define LLVM_ATTRIBUTE_UNUSED
Definition: Compiler.h:144
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
static MCTargetStreamer * createMCAsmTargetStreamer(MCStreamer &S, formatted_raw_ostream &OS, MCInstPrinter *IP, bool IsVerboseAsm)
static void LLVM_ATTRIBUTE_UNUSED clearFeature(MCSubtargetInfo *STI, uint64_t F)
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:228
static MCTargetStreamer * createHexagonObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI)
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)
unsigned GetELFFlags(const MCSubtargetInfo &STI)
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...
static cl::opt< bool > HexagonV60ArchVariant("mv60", cl::Hidden, cl::init(false), cl::desc("Build for Hexagon V60"))
void HexagonMCEmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size, unsigned ByteAlignment, unsigned AccessSize)
static bool isCPUValid(std::string CPU)
auto find(R &&Range, const T &Val) -> decltype(std::begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:839
bool isExtendable(MCInstrInfo const &MCII, MCInst const &MCI)
virtual bool isConditionalBranch(const MCInst &Inst) const
static const char * Separator
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.
Target - Wrapper for Target specific information.
LLVM_NODISCARD std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:727
static bool isUnconditionalBranch(TerminatorInst *Term)
Definition: ADCE.cpp:172
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
RegisterMCAsmInfoFn - Helper template for registering a target assembly info implementation.
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
static StringRef HexagonGetArchVariant()
This is an instance of a target assembly language printer that converts an MCInst to valid target ass...
Definition: MCInstPrinter.h:41
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:333
static bool LLVM_ATTRIBUTE_UNUSED checkFeature(MCSubtargetInfo *STI, uint64_t F)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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:463
LLVM Value Representation.
Definition: Value.h:73
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:35
static MCAsmInfo * createHexagonMCAsmInfo(const MCRegisterInfo &MRI, const Triple &TT)
const FeatureBitset Features
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
static cl::opt< bool > EnableHVX("mhvx", cl::Hidden, cl::init(false), cl::desc("Enable Hexagon Vector Extension (HVX)"))
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
static MCStreamer * createMCStreamer(Triple const &T, MCContext &Context, MCAsmBackend &MAB, raw_pwrite_stream &OS, MCCodeEmitter *Emitter, bool RelaxAll)
StringRef ParseHexagonTriple(const Triple &TT, StringRef CPU)
MCOperand const & getExtendableOperand(MCInstrInfo const &MCII, MCInst const &MCI)
MCRegisterInfo * createHexagonMCRegisterInfo(StringRef TT)
Target & getTheHexagonTarget()
cl::opt< bool > HexagonDisableDuplex
void HexagonMCEmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, unsigned ByteAlignment, unsigned AccessSize)