MCStreamer.h 28 KB

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