MCStreamer.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. //===- MCStreamer.h - High-level Streaming Machine Code Output --*- 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 declares the MCStreamer class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_MC_MCSTREAMER_H
  14. #define LLVM_MC_MCSTREAMER_H
  15. #include "llvm/ADT/ArrayRef.h"
  16. #include "llvm/ADT/SmallVector.h"
  17. #include "llvm/MC/MCDirectives.h"
  18. #include "llvm/MC/MCDwarf.h"
  19. #include "llvm/MC/MCLinkerOptimizationHint.h"
  20. #include "llvm/MC/MCWinEH.h"
  21. #include "llvm/Support/DataTypes.h"
  22. #include "llvm/Support/SMLoc.h"
  23. #include <string>
  24. namespace llvm {
  25. class MCAsmBackend;
  26. class MCCodeEmitter;
  27. class MCContext;
  28. class MCExpr;
  29. class MCInst;
  30. class MCInstPrinter;
  31. class MCSection;
  32. class MCStreamer;
  33. class MCSymbol;
  34. class MCSymbolELF;
  35. class MCSymbolRefExpr;
  36. class MCSubtargetInfo;
  37. class StringRef;
  38. class Twine;
  39. class raw_ostream;
  40. class formatted_raw_ostream;
  41. class AssemblerConstantPools;
  42. typedef std::pair<MCSection *, const MCExpr *> MCSectionSubPair;
  43. /// Target specific streamer interface. This is used so that targets can
  44. /// implement support for target specific assembly directives.
  45. ///
  46. /// If target foo wants to use this, it should implement 3 classes:
  47. /// * FooTargetStreamer : public MCTargetStreamer
  48. /// * FooTargetAsmStreamer : public FooTargetStreamer
  49. /// * FooTargetELFStreamer : public FooTargetStreamer
  50. ///
  51. /// FooTargetStreamer should have a pure virtual method for each directive. For
  52. /// example, for a ".bar symbol_name" directive, it should have
  53. /// virtual emitBar(const MCSymbol &Symbol) = 0;
  54. ///
  55. /// The FooTargetAsmStreamer and FooTargetELFStreamer classes implement the
  56. /// method. The assembly streamer just prints ".bar symbol_name". The object
  57. /// streamer does whatever is needed to implement .bar in the object file.
  58. ///
  59. /// In the assembly printer and parser the target streamer can be used by
  60. /// calling getTargetStreamer and casting it to FooTargetStreamer:
  61. ///
  62. /// MCTargetStreamer &TS = OutStreamer.getTargetStreamer();
  63. /// FooTargetStreamer &ATS = static_cast<FooTargetStreamer &>(TS);
  64. ///
  65. /// The base classes FooTargetAsmStreamer and FooTargetELFStreamer should
  66. /// *never* be treated differently. Callers should always talk to a
  67. /// FooTargetStreamer.
  68. class MCTargetStreamer {
  69. protected:
  70. MCStreamer &Streamer;
  71. public:
  72. MCTargetStreamer(MCStreamer &S);
  73. virtual ~MCTargetStreamer();
  74. MCStreamer &getStreamer() { return Streamer; }
  75. // Allow a target to add behavior to the EmitLabel of MCStreamer.
  76. virtual void emitLabel(MCSymbol *Symbol);
  77. // Allow a target to add behavior to the emitAssignment of MCStreamer.
  78. virtual void emitAssignment(MCSymbol *Symbol, const MCExpr *Value);
  79. virtual void prettyPrintAsm(MCInstPrinter &InstPrinter, raw_ostream &OS,
  80. const MCInst &Inst, const MCSubtargetInfo &STI);
  81. virtual void finish();
  82. };
  83. // FIXME: declared here because it is used from
  84. // lib/CodeGen/AsmPrinter/ARMException.cpp.
  85. class ARMTargetStreamer : public MCTargetStreamer {
  86. public:
  87. ARMTargetStreamer(MCStreamer &S);
  88. ~ARMTargetStreamer() override;
  89. virtual void emitFnStart();
  90. virtual void emitFnEnd();
  91. virtual void emitCantUnwind();
  92. virtual void emitPersonality(const MCSymbol *Personality);
  93. virtual void emitPersonalityIndex(unsigned Index);
  94. virtual void emitHandlerData();
  95. virtual void emitSetFP(unsigned FpReg, unsigned SpReg,
  96. int64_t Offset = 0);
  97. virtual void emitMovSP(unsigned Reg, int64_t Offset = 0);
  98. virtual void emitPad(int64_t Offset);
  99. virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
  100. bool isVector);
  101. virtual void emitUnwindRaw(int64_t StackOffset,
  102. const SmallVectorImpl<uint8_t> &Opcodes);
  103. virtual void switchVendor(StringRef Vendor);
  104. virtual void emitAttribute(unsigned Attribute, unsigned Value);
  105. virtual void emitTextAttribute(unsigned Attribute, StringRef String);
  106. virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
  107. StringRef StringValue = "");
  108. virtual void emitFPU(unsigned FPU);
  109. virtual void emitArch(unsigned Arch);
  110. virtual void emitArchExtension(unsigned ArchExt);
  111. virtual void emitObjectArch(unsigned Arch);
  112. virtual void finishAttributeSection();
  113. virtual void emitInst(uint32_t Inst, char Suffix = '\0');
  114. virtual void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE);
  115. virtual void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value);
  116. void finish() override;
  117. /// Callback used to implement the ldr= pseudo.
  118. /// Add a new entry to the constant pool for the current section and return an
  119. /// MCExpr that can be used to refer to the constant pool location.
  120. const MCExpr *addConstantPoolEntry(const MCExpr *);
  121. /// Callback used to implemnt the .ltorg directive.
  122. /// Emit contents of constant pool for the current section.
  123. void emitCurrentConstantPool();
  124. private:
  125. std::unique_ptr<AssemblerConstantPools> ConstantPools;
  126. };
  127. /// \brief Streaming machine code generation interface.
  128. ///
  129. /// This interface is intended to provide a programatic interface that is very
  130. /// similar to the level that an assembler .s file provides. It has callbacks
  131. /// to emit bytes, handle directives, etc. The implementation of this interface
  132. /// retains state to know what the current section is etc.
  133. ///
  134. /// There are multiple implementations of this interface: one for writing out
  135. /// a .s file, and implementations that write out .o files of various formats.
  136. ///
  137. class MCStreamer {
  138. MCContext &Context;
  139. std::unique_ptr<MCTargetStreamer> TargetStreamer;
  140. MCStreamer(const MCStreamer &) = delete;
  141. MCStreamer &operator=(const MCStreamer &) = delete;
  142. std::vector<MCDwarfFrameInfo> DwarfFrameInfos;
  143. MCDwarfFrameInfo *getCurrentDwarfFrameInfo();
  144. void EnsureValidDwarfFrame();
  145. MCSymbol *EmitCFICommon();
  146. std::vector<WinEH::FrameInfo *> WinFrameInfos;
  147. WinEH::FrameInfo *CurrentWinFrameInfo;
  148. void EnsureValidWinFrameInfo();
  149. /// \brief Tracks an index to represent the order a symbol was emitted in.
  150. /// Zero means we did not emit that symbol.
  151. DenseMap<const MCSymbol *, unsigned> SymbolOrdering;
  152. /// \brief This is stack of current and previous section values saved by
  153. /// PushSection.
  154. SmallVector<std::pair<MCSectionSubPair, MCSectionSubPair>, 4> SectionStack;
  155. protected:
  156. MCStreamer(MCContext &Ctx);
  157. virtual void EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame);
  158. virtual void EmitCFIEndProcImpl(MCDwarfFrameInfo &CurFrame);
  159. WinEH::FrameInfo *getCurrentWinFrameInfo() {
  160. return CurrentWinFrameInfo;
  161. }
  162. virtual void EmitWindowsUnwindTables();
  163. virtual void EmitRawTextImpl(StringRef String);
  164. public:
  165. virtual ~MCStreamer();
  166. void visitUsedExpr(const MCExpr &Expr);
  167. virtual void visitUsedSymbol(const MCSymbol &Sym);
  168. void setTargetStreamer(MCTargetStreamer *TS) {
  169. TargetStreamer.reset(TS);
  170. }
  171. /// State management
  172. ///
  173. virtual void reset();
  174. MCContext &getContext() const { return Context; }
  175. MCTargetStreamer *getTargetStreamer() {
  176. return TargetStreamer.get();
  177. }
  178. unsigned getNumFrameInfos() { return DwarfFrameInfos.size(); }
  179. ArrayRef<MCDwarfFrameInfo> getDwarfFrameInfos() const {
  180. return DwarfFrameInfos;
  181. }
  182. unsigned getNumWinFrameInfos() { return WinFrameInfos.size(); }
  183. ArrayRef<WinEH::FrameInfo *> getWinFrameInfos() const {
  184. return WinFrameInfos;
  185. }
  186. void generateCompactUnwindEncodings(MCAsmBackend *MAB);
  187. /// \name Assembly File Formatting.
  188. /// @{
  189. /// \brief Return true if this streamer supports verbose assembly and if it is
  190. /// enabled.
  191. virtual bool isVerboseAsm() const { return false; }
  192. /// \brief Return true if this asm streamer supports emitting unformatted text
  193. /// to the .s file with EmitRawText.
  194. virtual bool hasRawTextSupport() const { return false; }
  195. /// \brief Is the integrated assembler required for this streamer to function
  196. /// correctly?
  197. virtual bool isIntegratedAssemblerRequired() const { return false; }
  198. /// \brief Add a textual command.
  199. ///
  200. /// Typically for comments that can be emitted to the generated .s
  201. /// file if applicable as a QoI issue to make the output of the compiler
  202. /// more readable. This only affects the MCAsmStreamer, and only when
  203. /// verbose assembly output is enabled.
  204. ///
  205. /// If the comment includes embedded \n's, they will each get the comment
  206. /// prefix as appropriate. The added comment should not end with a \n.
  207. virtual void AddComment(const Twine &T) {}
  208. /// \brief Return a raw_ostream that comments can be written to. Unlike
  209. /// AddComment, you are required to terminate comments with \n if you use this
  210. /// method.
  211. virtual raw_ostream &GetCommentOS();
  212. /// \brief Print T and prefix it with the comment string (normally #) and
  213. /// optionally a tab. This prints the comment immediately, not at the end of
  214. /// the current line. It is basically a safe version of EmitRawText: since it
  215. /// only prints comments, the object streamer ignores it instead of asserting.
  216. virtual void emitRawComment(const Twine &T, bool TabPrefix = true);
  217. /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
  218. virtual void AddBlankLine() {}
  219. /// @}
  220. /// \name Symbol & Section Management
  221. /// @{
  222. /// \brief Return the current section that the streamer is emitting code to.
  223. MCSectionSubPair getCurrentSection() const {
  224. if (!SectionStack.empty())
  225. return SectionStack.back().first;
  226. return MCSectionSubPair();
  227. }
  228. MCSection *getCurrentSectionOnly() const { return getCurrentSection().first; }
  229. /// \brief Return the previous section that the streamer is emitting code to.
  230. MCSectionSubPair getPreviousSection() const {
  231. if (!SectionStack.empty())
  232. return SectionStack.back().second;
  233. return MCSectionSubPair();
  234. }
  235. /// \brief Returns an index to represent the order a symbol was emitted in.
  236. /// (zero if we did not emit that symbol)
  237. unsigned GetSymbolOrder(const MCSymbol *Sym) const {
  238. return SymbolOrdering.lookup(Sym);
  239. }
  240. /// \brief Update streamer for a new active section.
  241. ///
  242. /// This is called by PopSection and SwitchSection, if the current
  243. /// section changes.
  244. virtual void ChangeSection(MCSection *, const MCExpr *);
  245. /// \brief Save the current and previous section on the section stack.
  246. void PushSection() {
  247. SectionStack.push_back(
  248. std::make_pair(getCurrentSection(), getPreviousSection()));
  249. }
  250. /// \brief Restore the current and previous section from the section stack.
  251. /// Calls ChangeSection as needed.
  252. ///
  253. /// Returns false if the stack was empty.
  254. bool PopSection() {
  255. if (SectionStack.size() <= 1)
  256. return false;
  257. auto I = SectionStack.end();
  258. --I;
  259. MCSectionSubPair OldSection = I->first;
  260. --I;
  261. MCSectionSubPair NewSection = I->first;
  262. if (OldSection != NewSection)
  263. ChangeSection(NewSection.first, NewSection.second);
  264. SectionStack.pop_back();
  265. return true;
  266. }
  267. bool SubSection(const MCExpr *Subsection) {
  268. if (SectionStack.empty())
  269. return false;
  270. SwitchSection(SectionStack.back().first.first, Subsection);
  271. return true;
  272. }
  273. /// Set the current section where code is being emitted to \p Section. This
  274. /// is required to update CurSection.
  275. ///
  276. /// This corresponds to assembler directives like .section, .text, etc.
  277. virtual void SwitchSection(MCSection *Section,
  278. const MCExpr *Subsection = nullptr);
  279. /// \brief Set the current section where code is being emitted to \p Section.
  280. /// This is required to update CurSection. This version does not call
  281. /// ChangeSection.
  282. void SwitchSectionNoChange(MCSection *Section,
  283. const MCExpr *Subsection = nullptr) {
  284. assert(Section && "Cannot switch to a null section!");
  285. MCSectionSubPair curSection = SectionStack.back().first;
  286. SectionStack.back().second = curSection;
  287. if (MCSectionSubPair(Section, Subsection) != curSection)
  288. SectionStack.back().first = MCSectionSubPair(Section, Subsection);
  289. }
  290. /// \brief Create the default sections and set the initial one.
  291. virtual void InitSections(bool NoExecStack);
  292. MCSymbol *endSection(MCSection *Section);
  293. /// \brief Sets the symbol's section.
  294. ///
  295. /// Each emitted symbol will be tracked in the ordering table,
  296. /// so we can sort on them later.
  297. void AssignSection(MCSymbol *Symbol, MCSection *Section);
  298. /// \brief Emit a label for \p Symbol into the current section.
  299. ///
  300. /// This corresponds to an assembler statement such as:
  301. /// foo:
  302. ///
  303. /// \param Symbol - The symbol to emit. A given symbol should only be
  304. /// emitted as a label once, and symbols emitted as a label should never be
  305. /// used in an assignment.
  306. // FIXME: These emission are non-const because we mutate the symbol to
  307. // add the section we're emitting it to later.
  308. virtual void EmitLabel(MCSymbol *Symbol);
  309. virtual void EmitEHSymAttributes(const MCSymbol *Symbol, MCSymbol *EHSymbol);
  310. /// \brief Note in the output the specified \p Flag.
  311. virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
  312. /// \brief Emit the given list \p Options of strings as linker
  313. /// options into the output.
  314. virtual void EmitLinkerOptions(ArrayRef<std::string> Kind) {}
  315. /// \brief Note in the output the specified region \p Kind.
  316. virtual void EmitDataRegion(MCDataRegionType Kind) {}
  317. /// \brief Specify the MachO minimum deployment target version.
  318. virtual void EmitVersionMin(MCVersionMinType, unsigned Major, unsigned Minor,
  319. unsigned Update) {}
  320. /// \brief Note in the output that the specified \p Func is a Thumb mode
  321. /// function (ARM target only).
  322. virtual void EmitThumbFunc(MCSymbol *Func);
  323. /// \brief Emit an assignment of \p Value to \p Symbol.
  324. ///
  325. /// This corresponds to an assembler statement such as:
  326. /// symbol = value
  327. ///
  328. /// The assignment generates no code, but has the side effect of binding the
  329. /// value in the current context. For the assembly streamer, this prints the
  330. /// binding into the .s file.
  331. ///
  332. /// \param Symbol - The symbol being assigned to.
  333. /// \param Value - The value for the symbol.
  334. virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
  335. /// \brief Emit an weak reference from \p Alias to \p Symbol.
  336. ///
  337. /// This corresponds to an assembler statement such as:
  338. /// .weakref alias, symbol
  339. ///
  340. /// \param Alias - The alias that is being created.
  341. /// \param Symbol - The symbol being aliased.
  342. virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
  343. /// \brief Add the given \p Attribute to \p Symbol.
  344. virtual bool EmitSymbolAttribute(MCSymbol *Symbol,
  345. MCSymbolAttr Attribute) = 0;
  346. /// \brief Set the \p DescValue for the \p Symbol.
  347. ///
  348. /// \param Symbol - The symbol to have its n_desc field set.
  349. /// \param DescValue - The value to set into the n_desc field.
  350. virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
  351. /// \brief Start emitting COFF symbol definition
  352. ///
  353. /// \param Symbol - The symbol to have its External & Type fields set.
  354. virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol);
  355. /// \brief Emit the storage class of the symbol.
  356. ///
  357. /// \param StorageClass - The storage class the symbol should have.
  358. virtual void EmitCOFFSymbolStorageClass(int StorageClass);
  359. /// \brief Emit the type of the symbol.
  360. ///
  361. /// \param Type - A COFF type identifier (see COFF::SymbolType in X86COFF.h)
  362. virtual void EmitCOFFSymbolType(int Type);
  363. /// \brief Marks the end of the symbol definition.
  364. virtual void EndCOFFSymbolDef();
  365. virtual void EmitCOFFSafeSEH(MCSymbol const *Symbol);
  366. /// \brief Emits a COFF section index.
  367. ///
  368. /// \param Symbol - Symbol the section number relocation should point to.
  369. virtual void EmitCOFFSectionIndex(MCSymbol const *Symbol);
  370. /// \brief Emits a COFF section relative relocation.
  371. ///
  372. /// \param Symbol - Symbol the section relative relocation should point to.
  373. virtual void EmitCOFFSecRel32(MCSymbol const *Symbol);
  374. /// \brief Emit an ELF .size directive.
  375. ///
  376. /// This corresponds to an assembler statement such as:
  377. /// .size symbol, expression
  378. virtual void emitELFSize(MCSymbolELF *Symbol, const MCExpr *Value);
  379. /// \brief Emit a Linker Optimization Hint (LOH) directive.
  380. /// \param Args - Arguments of the LOH.
  381. virtual void EmitLOHDirective(MCLOHType Kind, const MCLOHArgs &Args) {}
  382. /// \brief Emit a common symbol.
  383. ///
  384. /// \param Symbol - The common symbol to emit.
  385. /// \param Size - The size of the common symbol.
  386. /// \param ByteAlignment - The alignment of the symbol if
  387. /// non-zero. This must be a power of 2.
  388. virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
  389. unsigned ByteAlignment) = 0;
  390. /// \brief Emit a local common (.lcomm) symbol.
  391. ///
  392. /// \param Symbol - The common symbol to emit.
  393. /// \param Size - The size of the common symbol.
  394. /// \param ByteAlignment - The alignment of the common symbol in bytes.
  395. virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
  396. unsigned ByteAlignment);
  397. /// \brief Emit the zerofill section and an optional symbol.
  398. ///
  399. /// \param Section - The zerofill section to create and or to put the symbol
  400. /// \param Symbol - The zerofill symbol to emit, if non-NULL.
  401. /// \param Size - The size of the zerofill symbol.
  402. /// \param ByteAlignment - The alignment of the zerofill symbol if
  403. /// non-zero. This must be a power of 2 on some targets.
  404. virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr,
  405. uint64_t Size = 0, unsigned ByteAlignment = 0) = 0;
  406. /// \brief Emit a thread local bss (.tbss) symbol.
  407. ///
  408. /// \param Section - The thread local common section.
  409. /// \param Symbol - The thread local common symbol to emit.
  410. /// \param Size - The size of the symbol.
  411. /// \param ByteAlignment - The alignment of the thread local common symbol
  412. /// if non-zero. This must be a power of 2 on some targets.
  413. virtual void EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
  414. uint64_t Size, unsigned ByteAlignment = 0);
  415. /// @}
  416. /// \name Generating Data
  417. /// @{
  418. /// \brief Emit the bytes in \p Data into the output.
  419. ///
  420. /// This is used to implement assembler directives such as .byte, .ascii,
  421. /// etc.
  422. virtual void EmitBytes(StringRef Data);
  423. /// \brief Emit the expression \p Value into the output as a native
  424. /// integer of the given \p Size bytes.
  425. ///
  426. /// This is used to implement assembler directives such as .word, .quad,
  427. /// etc.
  428. ///
  429. /// \param Value - The value to emit.
  430. /// \param Size - The size of the integer (in bytes) to emit. This must
  431. /// match a native machine width.
  432. /// \param Loc - The location of the expression for error reporting.
  433. virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
  434. const SMLoc &Loc = SMLoc());
  435. void EmitValue(const MCExpr *Value, unsigned Size,
  436. const SMLoc &Loc = SMLoc());
  437. /// \brief Special case of EmitValue that avoids the client having
  438. /// to pass in a MCExpr for constant integers.
  439. virtual void EmitIntValue(uint64_t Value, _In_range_(0, 8) unsigned Size);
  440. virtual void EmitULEB128Value(const MCExpr *Value);
  441. virtual void EmitSLEB128Value(const MCExpr *Value);
  442. /// \brief Special case of EmitULEB128Value that avoids the client having to
  443. /// pass in a MCExpr for constant integers.
  444. void EmitULEB128IntValue(uint64_t Value, unsigned Padding = 0);
  445. /// \brief Special case of EmitSLEB128Value that avoids the client having to
  446. /// pass in a MCExpr for constant integers.
  447. void EmitSLEB128IntValue(int64_t Value);
  448. /// \brief Special case of EmitValue that avoids the client having to pass in
  449. /// a MCExpr for MCSymbols.
  450. void EmitSymbolValue(const MCSymbol *Sym, unsigned Size,
  451. bool IsSectionRelative = false);
  452. /// \brief Emit the expression \p Value into the output as a gprel64 (64-bit
  453. /// GP relative) value.
  454. ///
  455. /// This is used to implement assembler directives such as .gpdword on
  456. /// targets that support them.
  457. virtual void EmitGPRel64Value(const MCExpr *Value);
  458. /// \brief Emit the expression \p Value into the output as a gprel32 (32-bit
  459. /// GP relative) value.
  460. ///
  461. /// This is used to implement assembler directives such as .gprel32 on
  462. /// targets that support them.
  463. virtual void EmitGPRel32Value(const MCExpr *Value);
  464. /// \brief Emit NumBytes bytes worth of the value specified by FillValue.
  465. /// This implements directives such as '.space'.
  466. virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue);
  467. /// \brief Emit NumBytes worth of zeros.
  468. /// This function properly handles data in virtual sections.
  469. virtual void EmitZeros(uint64_t NumBytes);
  470. /// \brief Emit some number of copies of \p Value until the byte alignment \p
  471. /// ByteAlignment is reached.
  472. ///
  473. /// If the number of bytes need to emit for the alignment is not a multiple
  474. /// of \p ValueSize, then the contents of the emitted fill bytes is
  475. /// undefined.
  476. ///
  477. /// This used to implement the .align assembler directive.
  478. ///
  479. /// \param ByteAlignment - The alignment to reach. This must be a power of
  480. /// two on some targets.
  481. /// \param Value - The value to use when filling bytes.
  482. /// \param ValueSize - The size of the integer (in bytes) to emit for
  483. /// \p Value. This must match a native machine width.
  484. /// \param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
  485. /// the alignment cannot be reached in this many bytes, no bytes are
  486. /// emitted.
  487. virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
  488. unsigned ValueSize = 1,
  489. unsigned MaxBytesToEmit = 0);
  490. /// \brief Emit nops until the byte alignment \p ByteAlignment is reached.
  491. ///
  492. /// This used to align code where the alignment bytes may be executed. This
  493. /// can emit different bytes for different sizes to optimize execution.
  494. ///
  495. /// \param ByteAlignment - The alignment to reach. This must be a power of
  496. /// two on some targets.
  497. /// \param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
  498. /// the alignment cannot be reached in this many bytes, no bytes are
  499. /// emitted.
  500. virtual void EmitCodeAlignment(unsigned ByteAlignment,
  501. unsigned MaxBytesToEmit = 0);
  502. /// \brief Emit some number of copies of \p Value until the byte offset \p
  503. /// Offset is reached.
  504. ///
  505. /// This is used to implement assembler directives such as .org.
  506. ///
  507. /// \param Offset - The offset to reach. This may be an expression, but the
  508. /// expression must be associated with the current section.
  509. /// \param Value - The value to use when filling bytes.
  510. /// \return false on success, true if the offset was invalid.
  511. virtual bool EmitValueToOffset(const MCExpr *Offset,
  512. unsigned char Value = 0);
  513. /// @}
  514. /// \brief Switch to a new logical file. This is used to implement the '.file
  515. /// "foo.c"' assembler directive.
  516. virtual void EmitFileDirective(StringRef Filename);
  517. /// \brief Emit the "identifiers" directive. This implements the
  518. /// '.ident "version foo"' assembler directive.
  519. virtual void EmitIdent(StringRef IdentString) {}
  520. /// \brief Associate a filename with a specified logical file number. This
  521. /// implements the DWARF2 '.file 4 "foo.c"' assembler directive.
  522. virtual unsigned EmitDwarfFileDirective(unsigned FileNo, StringRef Directory,
  523. StringRef Filename,
  524. unsigned CUID = 0);
  525. /// \brief This implements the DWARF2 '.loc fileno lineno ...' assembler
  526. /// directive.
  527. virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
  528. unsigned Column, unsigned Flags,
  529. unsigned Isa, unsigned Discriminator,
  530. StringRef FileName);
  531. /// Emit the absolute difference between two symbols.
  532. ///
  533. /// \pre Offset of \c Hi is greater than the offset \c Lo.
  534. virtual void emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo,
  535. unsigned Size);
  536. virtual MCSymbol *getDwarfLineTableSymbol(unsigned CUID);
  537. virtual void EmitCFISections(bool EH, bool Debug);
  538. void EmitCFIStartProc(bool IsSimple);
  539. void EmitCFIEndProc();
  540. virtual void EmitCFIDefCfa(int64_t Register, int64_t Offset);
  541. virtual void EmitCFIDefCfaOffset(int64_t Offset);
  542. virtual void EmitCFIDefCfaRegister(int64_t Register);
  543. virtual void EmitCFIOffset(int64_t Register, int64_t Offset);
  544. virtual void EmitCFIPersonality(const MCSymbol *Sym, unsigned Encoding);
  545. virtual void EmitCFILsda(const MCSymbol *Sym, unsigned Encoding);
  546. virtual void EmitCFIRememberState();
  547. virtual void EmitCFIRestoreState();
  548. virtual void EmitCFISameValue(int64_t Register);
  549. virtual void EmitCFIRestore(int64_t Register);
  550. virtual void EmitCFIRelOffset(int64_t Register, int64_t Offset);
  551. virtual void EmitCFIAdjustCfaOffset(int64_t Adjustment);
  552. virtual void EmitCFIEscape(StringRef Values);
  553. virtual void EmitCFISignalFrame();
  554. virtual void EmitCFIUndefined(int64_t Register);
  555. virtual void EmitCFIRegister(int64_t Register1, int64_t Register2);
  556. virtual void EmitCFIWindowSave();
  557. virtual void EmitWinCFIStartProc(const MCSymbol *Symbol);
  558. virtual void EmitWinCFIEndProc();
  559. virtual void EmitWinCFIStartChained();
  560. virtual void EmitWinCFIEndChained();
  561. virtual void EmitWinCFIPushReg(unsigned Register);
  562. virtual void EmitWinCFISetFrame(unsigned Register, unsigned Offset);
  563. virtual void EmitWinCFIAllocStack(unsigned Size);
  564. virtual void EmitWinCFISaveReg(unsigned Register, unsigned Offset);
  565. virtual void EmitWinCFISaveXMM(unsigned Register, unsigned Offset);
  566. virtual void EmitWinCFIPushFrame(bool Code);
  567. virtual void EmitWinCFIEndProlog();
  568. virtual void EmitWinEHHandler(const MCSymbol *Sym, bool Unwind, bool Except);
  569. virtual void EmitWinEHHandlerData();
  570. /// \brief Emit the given \p Instruction into the current section.
  571. virtual void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI);
  572. /// \brief Set the bundle alignment mode from now on in the section.
  573. /// The argument is the power of 2 to which the alignment is set. The
  574. /// value 0 means turn the bundle alignment off.
  575. virtual void EmitBundleAlignMode(unsigned AlignPow2);
  576. /// \brief The following instructions are a bundle-locked group.
  577. ///
  578. /// \param AlignToEnd - If true, the bundle-locked group will be aligned to
  579. /// the end of a bundle.
  580. virtual void EmitBundleLock(bool AlignToEnd);
  581. /// \brief Ends a bundle-locked group.
  582. virtual void EmitBundleUnlock();
  583. /// \brief If this file is backed by a assembly streamer, this dumps the
  584. /// specified string in the output .s file. This capability is indicated by
  585. /// the hasRawTextSupport() predicate. By default this aborts.
  586. void EmitRawText(const Twine &String);
  587. /// \brief Causes any cached state to be written out.
  588. virtual void Flush() {}
  589. /// \brief Streamer specific finalization.
  590. virtual void FinishImpl();
  591. /// \brief Finish emission of machine code.
  592. void Finish();
  593. virtual bool mayHaveInstructions(MCSection &Sec) const { return true; }
  594. };
  595. /// Create a dummy machine code streamer, which does nothing. This is useful for
  596. /// timing the assembler front end.
  597. MCStreamer *createNullStreamer(MCContext &Ctx);
  598. /// Create a machine code streamer which will print out assembly for the native
  599. /// target, suitable for compiling with a native assembler.
  600. ///
  601. /// \param InstPrint - If given, the instruction printer to use. If not given
  602. /// the MCInst representation will be printed. This method takes ownership of
  603. /// InstPrint.
  604. ///
  605. /// \param CE - If given, a code emitter to use to show the instruction
  606. /// encoding inline with the assembly. This method takes ownership of \p CE.
  607. ///
  608. /// \param TAB - If given, a target asm backend to use to show the fixup
  609. /// information in conjunction with encoding information. This method takes
  610. /// ownership of \p TAB.
  611. ///
  612. /// \param ShowInst - Whether to show the MCInst representation inline with
  613. /// the assembly.
  614. MCStreamer *createAsmStreamer(MCContext &Ctx,
  615. std::unique_ptr<formatted_raw_ostream> OS,
  616. bool isVerboseAsm, bool useDwarfDirectory,
  617. MCInstPrinter *InstPrint, MCCodeEmitter *CE,
  618. MCAsmBackend *TAB, bool ShowInst);
  619. } // end namespace llvm
  620. #endif