MCAsmStreamer.cpp 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223
  1. //===- lib/MC/MCAsmStreamer.cpp - Text Assembly Output ----------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/ADT/Optional.h"
  9. #include "llvm/ADT/STLExtras.h"
  10. #include "llvm/ADT/SmallString.h"
  11. #include "llvm/ADT/StringExtras.h"
  12. #include "llvm/ADT/Twine.h"
  13. #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
  14. #include "llvm/MC/MCAsmBackend.h"
  15. #include "llvm/MC/MCAsmInfo.h"
  16. #include "llvm/MC/MCAssembler.h"
  17. #include "llvm/MC/MCCodeEmitter.h"
  18. #include "llvm/MC/MCCodeView.h"
  19. #include "llvm/MC/MCContext.h"
  20. #include "llvm/MC/MCExpr.h"
  21. #include "llvm/MC/MCFixupKindInfo.h"
  22. #include "llvm/MC/MCInst.h"
  23. #include "llvm/MC/MCInstPrinter.h"
  24. #include "llvm/MC/MCObjectFileInfo.h"
  25. #include "llvm/MC/MCObjectWriter.h"
  26. #include "llvm/MC/MCPseudoProbe.h"
  27. #include "llvm/MC/MCRegister.h"
  28. #include "llvm/MC/MCRegisterInfo.h"
  29. #include "llvm/MC/MCSectionMachO.h"
  30. #include "llvm/MC/MCStreamer.h"
  31. #include "llvm/MC/MCSymbolXCOFF.h"
  32. #include "llvm/Support/ErrorHandling.h"
  33. #include "llvm/Support/Format.h"
  34. #include "llvm/Support/FormattedStream.h"
  35. #include "llvm/Support/LEB128.h"
  36. #include "llvm/Support/MathExtras.h"
  37. #include "llvm/Support/Path.h"
  38. #include "llvm/Support/TargetRegistry.h"
  39. #include <cctype>
  40. using namespace llvm;
  41. namespace {
  42. class MCAsmStreamer final : public MCStreamer {
  43. std::unique_ptr<formatted_raw_ostream> OSOwner;
  44. formatted_raw_ostream &OS;
  45. const MCAsmInfo *MAI;
  46. std::unique_ptr<MCInstPrinter> InstPrinter;
  47. std::unique_ptr<MCAssembler> Assembler;
  48. SmallString<128> ExplicitCommentToEmit;
  49. SmallString<128> CommentToEmit;
  50. raw_svector_ostream CommentStream;
  51. raw_null_ostream NullStream;
  52. unsigned IsVerboseAsm : 1;
  53. unsigned ShowInst : 1;
  54. unsigned UseDwarfDirectory : 1;
  55. void EmitRegisterName(int64_t Register);
  56. void emitCFIStartProcImpl(MCDwarfFrameInfo &Frame) override;
  57. void emitCFIEndProcImpl(MCDwarfFrameInfo &Frame) override;
  58. public:
  59. MCAsmStreamer(MCContext &Context, std::unique_ptr<formatted_raw_ostream> os,
  60. bool isVerboseAsm, bool useDwarfDirectory,
  61. MCInstPrinter *printer, std::unique_ptr<MCCodeEmitter> emitter,
  62. std::unique_ptr<MCAsmBackend> asmbackend, bool showInst)
  63. : MCStreamer(Context), OSOwner(std::move(os)), OS(*OSOwner),
  64. MAI(Context.getAsmInfo()), InstPrinter(printer),
  65. Assembler(std::make_unique<MCAssembler>(
  66. Context, std::move(asmbackend), std::move(emitter),
  67. (asmbackend) ? asmbackend->createObjectWriter(NullStream)
  68. : nullptr)),
  69. CommentStream(CommentToEmit), IsVerboseAsm(isVerboseAsm),
  70. ShowInst(showInst), UseDwarfDirectory(useDwarfDirectory) {
  71. assert(InstPrinter);
  72. if (IsVerboseAsm)
  73. InstPrinter->setCommentStream(CommentStream);
  74. if (Assembler->getBackendPtr())
  75. setAllowAutoPadding(Assembler->getBackend().allowAutoPadding());
  76. Context.setUseNamesOnTempLabels(true);
  77. }
  78. MCAssembler &getAssembler() { return *Assembler; }
  79. MCAssembler *getAssemblerPtr() override { return nullptr; }
  80. inline void EmitEOL() {
  81. // Dump Explicit Comments here.
  82. emitExplicitComments();
  83. // If we don't have any comments, just emit a \n.
  84. if (!IsVerboseAsm) {
  85. OS << '\n';
  86. return;
  87. }
  88. EmitCommentsAndEOL();
  89. }
  90. void emitSyntaxDirective() override;
  91. void EmitCommentsAndEOL();
  92. /// Return true if this streamer supports verbose assembly at all.
  93. bool isVerboseAsm() const override { return IsVerboseAsm; }
  94. /// Do we support EmitRawText?
  95. bool hasRawTextSupport() const override { return true; }
  96. /// Add a comment that can be emitted to the generated .s file to make the
  97. /// output of the compiler more readable. This only affects the MCAsmStreamer
  98. /// and only when verbose assembly output is enabled.
  99. void AddComment(const Twine &T, bool EOL = true) override;
  100. /// Add a comment showing the encoding of an instruction.
  101. void AddEncodingComment(const MCInst &Inst, const MCSubtargetInfo &);
  102. /// Return a raw_ostream that comments can be written to.
  103. /// Unlike AddComment, you are required to terminate comments with \n if you
  104. /// use this method.
  105. raw_ostream &GetCommentOS() override {
  106. if (!IsVerboseAsm)
  107. return nulls(); // Discard comments unless in verbose asm mode.
  108. return CommentStream;
  109. }
  110. void emitRawComment(const Twine &T, bool TabPrefix = true) override;
  111. void addExplicitComment(const Twine &T) override;
  112. void emitExplicitComments() override;
  113. /// Emit a blank line to a .s file to pretty it up.
  114. void AddBlankLine() override {
  115. EmitEOL();
  116. }
  117. /// @name MCStreamer Interface
  118. /// @{
  119. void changeSection(MCSection *Section, const MCExpr *Subsection) override;
  120. void emitELFSymverDirective(StringRef AliasName,
  121. const MCSymbol *Aliasee) override;
  122. void emitLOHDirective(MCLOHType Kind, const MCLOHArgs &Args) override;
  123. StringRef getMnemonic(MCInst &MI) override {
  124. return InstPrinter->getMnemonic(&MI).first;
  125. }
  126. void emitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc()) override;
  127. void emitAssemblerFlag(MCAssemblerFlag Flag) override;
  128. void emitLinkerOptions(ArrayRef<std::string> Options) override;
  129. void emitDataRegion(MCDataRegionType Kind) override;
  130. void emitVersionMin(MCVersionMinType Kind, unsigned Major, unsigned Minor,
  131. unsigned Update, VersionTuple SDKVersion) override;
  132. void emitBuildVersion(unsigned Platform, unsigned Major, unsigned Minor,
  133. unsigned Update, VersionTuple SDKVersion) override;
  134. void emitThumbFunc(MCSymbol *Func) override;
  135. void emitAssignment(MCSymbol *Symbol, const MCExpr *Value) override;
  136. void emitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) override;
  137. bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override;
  138. void emitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) override;
  139. void BeginCOFFSymbolDef(const MCSymbol *Symbol) override;
  140. void EmitCOFFSymbolStorageClass(int StorageClass) override;
  141. void EmitCOFFSymbolType(int Type) override;
  142. void EndCOFFSymbolDef() override;
  143. void EmitCOFFSafeSEH(MCSymbol const *Symbol) override;
  144. void EmitCOFFSymbolIndex(MCSymbol const *Symbol) override;
  145. void EmitCOFFSectionIndex(MCSymbol const *Symbol) override;
  146. void EmitCOFFSecRel32(MCSymbol const *Symbol, uint64_t Offset) override;
  147. void EmitCOFFImgRel32(MCSymbol const *Symbol, int64_t Offset) override;
  148. void emitXCOFFLocalCommonSymbol(MCSymbol *LabelSym, uint64_t Size,
  149. MCSymbol *CsectSym,
  150. unsigned ByteAlign) override;
  151. void emitXCOFFSymbolLinkageWithVisibility(MCSymbol *Symbol,
  152. MCSymbolAttr Linakge,
  153. MCSymbolAttr Visibility) override;
  154. void emitXCOFFRenameDirective(const MCSymbol *Name,
  155. StringRef Rename) override;
  156. void emitELFSize(MCSymbol *Symbol, const MCExpr *Value) override;
  157. void emitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
  158. unsigned ByteAlignment) override;
  159. /// Emit a local common (.lcomm) symbol.
  160. ///
  161. /// @param Symbol - The common symbol to emit.
  162. /// @param Size - The size of the common symbol.
  163. /// @param ByteAlignment - The alignment of the common symbol in bytes.
  164. void emitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
  165. unsigned ByteAlignment) override;
  166. void emitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr,
  167. uint64_t Size = 0, unsigned ByteAlignment = 0,
  168. SMLoc Loc = SMLoc()) override;
  169. void emitTBSSSymbol(MCSection *Section, MCSymbol *Symbol, uint64_t Size,
  170. unsigned ByteAlignment = 0) override;
  171. void emitBinaryData(StringRef Data) override;
  172. void emitBytes(StringRef Data) override;
  173. void emitValueImpl(const MCExpr *Value, unsigned Size,
  174. SMLoc Loc = SMLoc()) override;
  175. void emitIntValue(uint64_t Value, unsigned Size) override;
  176. void emitIntValueInHex(uint64_t Value, unsigned Size) override;
  177. void emitIntValueInHexWithPadding(uint64_t Value, unsigned Size) override;
  178. void emitULEB128Value(const MCExpr *Value) override;
  179. void emitSLEB128Value(const MCExpr *Value) override;
  180. void emitDTPRel32Value(const MCExpr *Value) override;
  181. void emitDTPRel64Value(const MCExpr *Value) override;
  182. void emitTPRel32Value(const MCExpr *Value) override;
  183. void emitTPRel64Value(const MCExpr *Value) override;
  184. void emitGPRel64Value(const MCExpr *Value) override;
  185. void emitGPRel32Value(const MCExpr *Value) override;
  186. void emitFill(const MCExpr &NumBytes, uint64_t FillValue,
  187. SMLoc Loc = SMLoc()) override;
  188. void emitFill(const MCExpr &NumValues, int64_t Size, int64_t Expr,
  189. SMLoc Loc = SMLoc()) override;
  190. void emitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
  191. unsigned ValueSize = 1,
  192. unsigned MaxBytesToEmit = 0) override;
  193. void emitCodeAlignment(unsigned ByteAlignment,
  194. unsigned MaxBytesToEmit = 0) override;
  195. void emitValueToOffset(const MCExpr *Offset,
  196. unsigned char Value,
  197. SMLoc Loc) override;
  198. void emitFileDirective(StringRef Filename) override;
  199. Expected<unsigned> tryEmitDwarfFileDirective(unsigned FileNo,
  200. StringRef Directory,
  201. StringRef Filename,
  202. Optional<MD5::MD5Result> Checksum = None,
  203. Optional<StringRef> Source = None,
  204. unsigned CUID = 0) override;
  205. void emitDwarfFile0Directive(StringRef Directory, StringRef Filename,
  206. Optional<MD5::MD5Result> Checksum,
  207. Optional<StringRef> Source,
  208. unsigned CUID = 0) override;
  209. void emitDwarfLocDirective(unsigned FileNo, unsigned Line, unsigned Column,
  210. unsigned Flags, unsigned Isa,
  211. unsigned Discriminator,
  212. StringRef FileName) override;
  213. MCSymbol *getDwarfLineTableSymbol(unsigned CUID) override;
  214. bool EmitCVFileDirective(unsigned FileNo, StringRef Filename,
  215. ArrayRef<uint8_t> Checksum,
  216. unsigned ChecksumKind) override;
  217. bool EmitCVFuncIdDirective(unsigned FuncId) override;
  218. bool EmitCVInlineSiteIdDirective(unsigned FunctionId, unsigned IAFunc,
  219. unsigned IAFile, unsigned IALine,
  220. unsigned IACol, SMLoc Loc) override;
  221. void emitCVLocDirective(unsigned FunctionId, unsigned FileNo, unsigned Line,
  222. unsigned Column, bool PrologueEnd, bool IsStmt,
  223. StringRef FileName, SMLoc Loc) override;
  224. void emitCVLinetableDirective(unsigned FunctionId, const MCSymbol *FnStart,
  225. const MCSymbol *FnEnd) override;
  226. void emitCVInlineLinetableDirective(unsigned PrimaryFunctionId,
  227. unsigned SourceFileId,
  228. unsigned SourceLineNum,
  229. const MCSymbol *FnStartSym,
  230. const MCSymbol *FnEndSym) override;
  231. void PrintCVDefRangePrefix(
  232. ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges);
  233. void emitCVDefRangeDirective(
  234. ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
  235. codeview::DefRangeRegisterRelHeader DRHdr) override;
  236. void emitCVDefRangeDirective(
  237. ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
  238. codeview::DefRangeSubfieldRegisterHeader DRHdr) override;
  239. void emitCVDefRangeDirective(
  240. ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
  241. codeview::DefRangeRegisterHeader DRHdr) override;
  242. void emitCVDefRangeDirective(
  243. ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
  244. codeview::DefRangeFramePointerRelHeader DRHdr) override;
  245. void emitCVStringTableDirective() override;
  246. void emitCVFileChecksumsDirective() override;
  247. void emitCVFileChecksumOffsetDirective(unsigned FileNo) override;
  248. void EmitCVFPOData(const MCSymbol *ProcSym, SMLoc L) override;
  249. void emitIdent(StringRef IdentString) override;
  250. void emitCFIBKeyFrame() override;
  251. void emitCFISections(bool EH, bool Debug) override;
  252. void emitCFIDefCfa(int64_t Register, int64_t Offset) override;
  253. void emitCFIDefCfaOffset(int64_t Offset) override;
  254. void emitCFIDefCfaRegister(int64_t Register) override;
  255. void emitCFIOffset(int64_t Register, int64_t Offset) override;
  256. void emitCFIPersonality(const MCSymbol *Sym, unsigned Encoding) override;
  257. void emitCFILsda(const MCSymbol *Sym, unsigned Encoding) override;
  258. void emitCFIRememberState() override;
  259. void emitCFIRestoreState() override;
  260. void emitCFIRestore(int64_t Register) override;
  261. void emitCFISameValue(int64_t Register) override;
  262. void emitCFIRelOffset(int64_t Register, int64_t Offset) override;
  263. void emitCFIAdjustCfaOffset(int64_t Adjustment) override;
  264. void emitCFIEscape(StringRef Values) override;
  265. void emitCFIGnuArgsSize(int64_t Size) override;
  266. void emitCFISignalFrame() override;
  267. void emitCFIUndefined(int64_t Register) override;
  268. void emitCFIRegister(int64_t Register1, int64_t Register2) override;
  269. void emitCFIWindowSave() override;
  270. void emitCFINegateRAState() override;
  271. void emitCFIReturnColumn(int64_t Register) override;
  272. void EmitWinCFIStartProc(const MCSymbol *Symbol, SMLoc Loc) override;
  273. void EmitWinCFIEndProc(SMLoc Loc) override;
  274. void EmitWinCFIFuncletOrFuncEnd(SMLoc Loc) override;
  275. void EmitWinCFIStartChained(SMLoc Loc) override;
  276. void EmitWinCFIEndChained(SMLoc Loc) override;
  277. void EmitWinCFIPushReg(MCRegister Register, SMLoc Loc) override;
  278. void EmitWinCFISetFrame(MCRegister Register, unsigned Offset,
  279. SMLoc Loc) override;
  280. void EmitWinCFIAllocStack(unsigned Size, SMLoc Loc) override;
  281. void EmitWinCFISaveReg(MCRegister Register, unsigned Offset,
  282. SMLoc Loc) override;
  283. void EmitWinCFISaveXMM(MCRegister Register, unsigned Offset,
  284. SMLoc Loc) override;
  285. void EmitWinCFIPushFrame(bool Code, SMLoc Loc) override;
  286. void EmitWinCFIEndProlog(SMLoc Loc) override;
  287. void EmitWinEHHandler(const MCSymbol *Sym, bool Unwind, bool Except,
  288. SMLoc Loc) override;
  289. void EmitWinEHHandlerData(SMLoc Loc) override;
  290. void emitCGProfileEntry(const MCSymbolRefExpr *From,
  291. const MCSymbolRefExpr *To, uint64_t Count) override;
  292. void emitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override;
  293. void emitPseudoProbe(uint64_t Guid, uint64_t Index, uint64_t Type,
  294. uint64_t Attr,
  295. const MCPseudoProbeInlineStack &InlineStack) override;
  296. void emitBundleAlignMode(unsigned AlignPow2) override;
  297. void emitBundleLock(bool AlignToEnd) override;
  298. void emitBundleUnlock() override;
  299. Optional<std::pair<bool, std::string>>
  300. emitRelocDirective(const MCExpr &Offset, StringRef Name, const MCExpr *Expr,
  301. SMLoc Loc, const MCSubtargetInfo &STI) override;
  302. void emitAddrsig() override;
  303. void emitAddrsigSym(const MCSymbol *Sym) override;
  304. /// If this file is backed by an assembly streamer, this dumps the specified
  305. /// string in the output .s file. This capability is indicated by the
  306. /// hasRawTextSupport() predicate.
  307. void emitRawTextImpl(StringRef String) override;
  308. void finishImpl() override;
  309. };
  310. } // end anonymous namespace.
  311. void MCAsmStreamer::AddComment(const Twine &T, bool EOL) {
  312. if (!IsVerboseAsm) return;
  313. T.toVector(CommentToEmit);
  314. if (EOL)
  315. CommentToEmit.push_back('\n'); // Place comment in a new line.
  316. }
  317. void MCAsmStreamer::EmitCommentsAndEOL() {
  318. if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
  319. OS << '\n';
  320. return;
  321. }
  322. StringRef Comments = CommentToEmit;
  323. assert(Comments.back() == '\n' &&
  324. "Comment array not newline terminated");
  325. do {
  326. // Emit a line of comments.
  327. OS.PadToColumn(MAI->getCommentColumn());
  328. size_t Position = Comments.find('\n');
  329. OS << MAI->getCommentString() << ' ' << Comments.substr(0, Position) <<'\n';
  330. Comments = Comments.substr(Position+1);
  331. } while (!Comments.empty());
  332. CommentToEmit.clear();
  333. }
  334. static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
  335. assert(Bytes > 0 && Bytes <= 8 && "Invalid size!");
  336. return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
  337. }
  338. void MCAsmStreamer::emitRawComment(const Twine &T, bool TabPrefix) {
  339. if (TabPrefix)
  340. OS << '\t';
  341. OS << MAI->getCommentString() << T;
  342. EmitEOL();
  343. }
  344. void MCAsmStreamer::addExplicitComment(const Twine &T) {
  345. StringRef c = T.getSingleStringRef();
  346. if (c.equals(StringRef(MAI->getSeparatorString())))
  347. return;
  348. if (c.startswith(StringRef("//"))) {
  349. ExplicitCommentToEmit.append("\t");
  350. ExplicitCommentToEmit.append(MAI->getCommentString());
  351. // drop //
  352. ExplicitCommentToEmit.append(c.slice(2, c.size()).str());
  353. } else if (c.startswith(StringRef("/*"))) {
  354. size_t p = 2, len = c.size() - 2;
  355. // emit each line in comment as separate newline.
  356. do {
  357. size_t newp = std::min(len, c.find_first_of("\r\n", p));
  358. ExplicitCommentToEmit.append("\t");
  359. ExplicitCommentToEmit.append(MAI->getCommentString());
  360. ExplicitCommentToEmit.append(c.slice(p, newp).str());
  361. // If we have another line in this comment add line
  362. if (newp < len)
  363. ExplicitCommentToEmit.append("\n");
  364. p = newp + 1;
  365. } while (p < len);
  366. } else if (c.startswith(StringRef(MAI->getCommentString()))) {
  367. ExplicitCommentToEmit.append("\t");
  368. ExplicitCommentToEmit.append(c.str());
  369. } else if (c.front() == '#') {
  370. ExplicitCommentToEmit.append("\t");
  371. ExplicitCommentToEmit.append(MAI->getCommentString());
  372. ExplicitCommentToEmit.append(c.slice(1, c.size()).str());
  373. } else
  374. assert(false && "Unexpected Assembly Comment");
  375. // full line comments immediately output
  376. if (c.back() == '\n')
  377. emitExplicitComments();
  378. }
  379. void MCAsmStreamer::emitExplicitComments() {
  380. StringRef Comments = ExplicitCommentToEmit;
  381. if (!Comments.empty())
  382. OS << Comments;
  383. ExplicitCommentToEmit.clear();
  384. }
  385. void MCAsmStreamer::changeSection(MCSection *Section,
  386. const MCExpr *Subsection) {
  387. assert(Section && "Cannot switch to a null section!");
  388. if (MCTargetStreamer *TS = getTargetStreamer()) {
  389. TS->changeSection(getCurrentSectionOnly(), Section, Subsection, OS);
  390. } else {
  391. Section->PrintSwitchToSection(
  392. *MAI, getContext().getObjectFileInfo()->getTargetTriple(), OS,
  393. Subsection);
  394. }
  395. }
  396. void MCAsmStreamer::emitELFSymverDirective(StringRef AliasName,
  397. const MCSymbol *Aliasee) {
  398. OS << ".symver ";
  399. Aliasee->print(OS, MAI);
  400. OS << ", " << AliasName;
  401. EmitEOL();
  402. }
  403. void MCAsmStreamer::emitLabel(MCSymbol *Symbol, SMLoc Loc) {
  404. MCStreamer::emitLabel(Symbol, Loc);
  405. Symbol->print(OS, MAI);
  406. OS << MAI->getLabelSuffix();
  407. EmitEOL();
  408. }
  409. void MCAsmStreamer::emitLOHDirective(MCLOHType Kind, const MCLOHArgs &Args) {
  410. StringRef str = MCLOHIdToName(Kind);
  411. #ifndef NDEBUG
  412. int NbArgs = MCLOHIdToNbArgs(Kind);
  413. assert(NbArgs != -1 && ((size_t)NbArgs) == Args.size() && "Malformed LOH!");
  414. assert(str != "" && "Invalid LOH name");
  415. #endif
  416. OS << "\t" << MCLOHDirectiveName() << " " << str << "\t";
  417. bool IsFirst = true;
  418. for (const MCSymbol *Arg : Args) {
  419. if (!IsFirst)
  420. OS << ", ";
  421. IsFirst = false;
  422. Arg->print(OS, MAI);
  423. }
  424. EmitEOL();
  425. }
  426. void MCAsmStreamer::emitAssemblerFlag(MCAssemblerFlag Flag) {
  427. switch (Flag) {
  428. case MCAF_SyntaxUnified: OS << "\t.syntax unified"; break;
  429. case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
  430. case MCAF_Code16: OS << '\t'<< MAI->getCode16Directive();break;
  431. case MCAF_Code32: OS << '\t'<< MAI->getCode32Directive();break;
  432. case MCAF_Code64: OS << '\t'<< MAI->getCode64Directive();break;
  433. }
  434. EmitEOL();
  435. }
  436. void MCAsmStreamer::emitLinkerOptions(ArrayRef<std::string> Options) {
  437. assert(!Options.empty() && "At least one option is required!");
  438. OS << "\t.linker_option \"" << Options[0] << '"';
  439. for (ArrayRef<std::string>::iterator it = Options.begin() + 1,
  440. ie = Options.end(); it != ie; ++it) {
  441. OS << ", " << '"' << *it << '"';
  442. }
  443. EmitEOL();
  444. }
  445. void MCAsmStreamer::emitDataRegion(MCDataRegionType Kind) {
  446. if (!MAI->doesSupportDataRegionDirectives())
  447. return;
  448. switch (Kind) {
  449. case MCDR_DataRegion: OS << "\t.data_region"; break;
  450. case MCDR_DataRegionJT8: OS << "\t.data_region jt8"; break;
  451. case MCDR_DataRegionJT16: OS << "\t.data_region jt16"; break;
  452. case MCDR_DataRegionJT32: OS << "\t.data_region jt32"; break;
  453. case MCDR_DataRegionEnd: OS << "\t.end_data_region"; break;
  454. }
  455. EmitEOL();
  456. }
  457. static const char *getVersionMinDirective(MCVersionMinType Type) {
  458. switch (Type) {
  459. case MCVM_WatchOSVersionMin: return ".watchos_version_min";
  460. case MCVM_TvOSVersionMin: return ".tvos_version_min";
  461. case MCVM_IOSVersionMin: return ".ios_version_min";
  462. case MCVM_OSXVersionMin: return ".macosx_version_min";
  463. }
  464. llvm_unreachable("Invalid MC version min type");
  465. }
  466. static void EmitSDKVersionSuffix(raw_ostream &OS,
  467. const VersionTuple &SDKVersion) {
  468. if (SDKVersion.empty())
  469. return;
  470. OS << '\t' << "sdk_version " << SDKVersion.getMajor();
  471. if (auto Minor = SDKVersion.getMinor()) {
  472. OS << ", " << *Minor;
  473. if (auto Subminor = SDKVersion.getSubminor()) {
  474. OS << ", " << *Subminor;
  475. }
  476. }
  477. }
  478. void MCAsmStreamer::emitVersionMin(MCVersionMinType Type, unsigned Major,
  479. unsigned Minor, unsigned Update,
  480. VersionTuple SDKVersion) {
  481. OS << '\t' << getVersionMinDirective(Type) << ' ' << Major << ", " << Minor;
  482. if (Update)
  483. OS << ", " << Update;
  484. EmitSDKVersionSuffix(OS, SDKVersion);
  485. EmitEOL();
  486. }
  487. static const char *getPlatformName(MachO::PlatformType Type) {
  488. switch (Type) {
  489. case MachO::PLATFORM_MACOS: return "macos";
  490. case MachO::PLATFORM_IOS: return "ios";
  491. case MachO::PLATFORM_TVOS: return "tvos";
  492. case MachO::PLATFORM_WATCHOS: return "watchos";
  493. case MachO::PLATFORM_BRIDGEOS: return "bridgeos";
  494. case MachO::PLATFORM_MACCATALYST: return "macCatalyst";
  495. case MachO::PLATFORM_IOSSIMULATOR: return "iossimulator";
  496. case MachO::PLATFORM_TVOSSIMULATOR: return "tvossimulator";
  497. case MachO::PLATFORM_WATCHOSSIMULATOR: return "watchossimulator";
  498. case MachO::PLATFORM_DRIVERKIT: return "driverkit";
  499. }
  500. llvm_unreachable("Invalid Mach-O platform type");
  501. }
  502. void MCAsmStreamer::emitBuildVersion(unsigned Platform, unsigned Major,
  503. unsigned Minor, unsigned Update,
  504. VersionTuple SDKVersion) {
  505. const char *PlatformName = getPlatformName((MachO::PlatformType)Platform);
  506. OS << "\t.build_version " << PlatformName << ", " << Major << ", " << Minor;
  507. if (Update)
  508. OS << ", " << Update;
  509. EmitSDKVersionSuffix(OS, SDKVersion);
  510. EmitEOL();
  511. }
  512. void MCAsmStreamer::emitThumbFunc(MCSymbol *Func) {
  513. // This needs to emit to a temporary string to get properly quoted
  514. // MCSymbols when they have spaces in them.
  515. OS << "\t.thumb_func";
  516. // Only Mach-O hasSubsectionsViaSymbols()
  517. if (MAI->hasSubsectionsViaSymbols()) {
  518. OS << '\t';
  519. Func->print(OS, MAI);
  520. }
  521. EmitEOL();
  522. }
  523. void MCAsmStreamer::emitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
  524. // Do not emit a .set on inlined target assignments.
  525. bool EmitSet = true;
  526. if (auto *E = dyn_cast<MCTargetExpr>(Value))
  527. if (E->inlineAssignedExpr())
  528. EmitSet = false;
  529. if (EmitSet) {
  530. OS << ".set ";
  531. Symbol->print(OS, MAI);
  532. OS << ", ";
  533. Value->print(OS, MAI);
  534. EmitEOL();
  535. }
  536. MCStreamer::emitAssignment(Symbol, Value);
  537. }
  538. void MCAsmStreamer::emitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
  539. OS << ".weakref ";
  540. Alias->print(OS, MAI);
  541. OS << ", ";
  542. Symbol->print(OS, MAI);
  543. EmitEOL();
  544. }
  545. bool MCAsmStreamer::emitSymbolAttribute(MCSymbol *Symbol,
  546. MCSymbolAttr Attribute) {
  547. switch (Attribute) {
  548. case MCSA_Invalid: llvm_unreachable("Invalid symbol attribute");
  549. case MCSA_ELF_TypeFunction: /// .type _foo, STT_FUNC # aka @function
  550. case MCSA_ELF_TypeIndFunction: /// .type _foo, STT_GNU_IFUNC
  551. case MCSA_ELF_TypeObject: /// .type _foo, STT_OBJECT # aka @object
  552. case MCSA_ELF_TypeTLS: /// .type _foo, STT_TLS # aka @tls_object
  553. case MCSA_ELF_TypeCommon: /// .type _foo, STT_COMMON # aka @common
  554. case MCSA_ELF_TypeNoType: /// .type _foo, STT_NOTYPE # aka @notype
  555. case MCSA_ELF_TypeGnuUniqueObject: /// .type _foo, @gnu_unique_object
  556. if (!MAI->hasDotTypeDotSizeDirective())
  557. return false; // Symbol attribute not supported
  558. OS << "\t.type\t";
  559. Symbol->print(OS, MAI);
  560. OS << ',' << ((MAI->getCommentString()[0] != '@') ? '@' : '%');
  561. switch (Attribute) {
  562. default: return false;
  563. case MCSA_ELF_TypeFunction: OS << "function"; break;
  564. case MCSA_ELF_TypeIndFunction: OS << "gnu_indirect_function"; break;
  565. case MCSA_ELF_TypeObject: OS << "object"; break;
  566. case MCSA_ELF_TypeTLS: OS << "tls_object"; break;
  567. case MCSA_ELF_TypeCommon: OS << "common"; break;
  568. case MCSA_ELF_TypeNoType: OS << "notype"; break;
  569. case MCSA_ELF_TypeGnuUniqueObject: OS << "gnu_unique_object"; break;
  570. }
  571. EmitEOL();
  572. return true;
  573. case MCSA_Global: // .globl/.global
  574. OS << MAI->getGlobalDirective();
  575. break;
  576. case MCSA_LGlobal: OS << "\t.lglobl\t"; break;
  577. case MCSA_Hidden: OS << "\t.hidden\t"; break;
  578. case MCSA_IndirectSymbol: OS << "\t.indirect_symbol\t"; break;
  579. case MCSA_Internal: OS << "\t.internal\t"; break;
  580. case MCSA_LazyReference: OS << "\t.lazy_reference\t"; break;
  581. case MCSA_Local: OS << "\t.local\t"; break;
  582. case MCSA_NoDeadStrip:
  583. if (!MAI->hasNoDeadStrip())
  584. return false;
  585. OS << "\t.no_dead_strip\t";
  586. break;
  587. case MCSA_SymbolResolver: OS << "\t.symbol_resolver\t"; break;
  588. case MCSA_AltEntry: OS << "\t.alt_entry\t"; break;
  589. case MCSA_PrivateExtern:
  590. OS << "\t.private_extern\t";
  591. break;
  592. case MCSA_Protected: OS << "\t.protected\t"; break;
  593. case MCSA_Reference: OS << "\t.reference\t"; break;
  594. case MCSA_Extern:
  595. OS << "\t.extern\t";
  596. break;
  597. case MCSA_Weak: OS << MAI->getWeakDirective(); break;
  598. case MCSA_WeakDefinition:
  599. OS << "\t.weak_definition\t";
  600. break;
  601. // .weak_reference
  602. case MCSA_WeakReference: OS << MAI->getWeakRefDirective(); break;
  603. case MCSA_WeakDefAutoPrivate: OS << "\t.weak_def_can_be_hidden\t"; break;
  604. case MCSA_Cold:
  605. // Assemblers currently do not support a .cold directive.
  606. return false;
  607. }
  608. Symbol->print(OS, MAI);
  609. EmitEOL();
  610. return true;
  611. }
  612. void MCAsmStreamer::emitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
  613. OS << ".desc" << ' ';
  614. Symbol->print(OS, MAI);
  615. OS << ',' << DescValue;
  616. EmitEOL();
  617. }
  618. void MCAsmStreamer::emitSyntaxDirective() {
  619. if (MAI->getAssemblerDialect() == 1) {
  620. OS << "\t.intel_syntax noprefix";
  621. EmitEOL();
  622. }
  623. // FIXME: Currently emit unprefix'ed registers.
  624. // The intel_syntax directive has one optional argument
  625. // with may have a value of prefix or noprefix.
  626. }
  627. void MCAsmStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
  628. OS << "\t.def\t ";
  629. Symbol->print(OS, MAI);
  630. OS << ';';
  631. EmitEOL();
  632. }
  633. void MCAsmStreamer::EmitCOFFSymbolStorageClass (int StorageClass) {
  634. OS << "\t.scl\t" << StorageClass << ';';
  635. EmitEOL();
  636. }
  637. void MCAsmStreamer::EmitCOFFSymbolType (int Type) {
  638. OS << "\t.type\t" << Type << ';';
  639. EmitEOL();
  640. }
  641. void MCAsmStreamer::EndCOFFSymbolDef() {
  642. OS << "\t.endef";
  643. EmitEOL();
  644. }
  645. void MCAsmStreamer::EmitCOFFSafeSEH(MCSymbol const *Symbol) {
  646. OS << "\t.safeseh\t";
  647. Symbol->print(OS, MAI);
  648. EmitEOL();
  649. }
  650. void MCAsmStreamer::EmitCOFFSymbolIndex(MCSymbol const *Symbol) {
  651. OS << "\t.symidx\t";
  652. Symbol->print(OS, MAI);
  653. EmitEOL();
  654. }
  655. void MCAsmStreamer::EmitCOFFSectionIndex(MCSymbol const *Symbol) {
  656. OS << "\t.secidx\t";
  657. Symbol->print(OS, MAI);
  658. EmitEOL();
  659. }
  660. void MCAsmStreamer::EmitCOFFSecRel32(MCSymbol const *Symbol, uint64_t Offset) {
  661. OS << "\t.secrel32\t";
  662. Symbol->print(OS, MAI);
  663. if (Offset != 0)
  664. OS << '+' << Offset;
  665. EmitEOL();
  666. }
  667. void MCAsmStreamer::EmitCOFFImgRel32(MCSymbol const *Symbol, int64_t Offset) {
  668. OS << "\t.rva\t";
  669. Symbol->print(OS, MAI);
  670. if (Offset > 0)
  671. OS << '+' << Offset;
  672. else if (Offset < 0)
  673. OS << '-' << -Offset;
  674. EmitEOL();
  675. }
  676. // We need an XCOFF-specific version of this directive as the AIX syntax
  677. // requires a QualName argument identifying the csect name and storage mapping
  678. // class to appear before the alignment if we are specifying it.
  679. void MCAsmStreamer::emitXCOFFLocalCommonSymbol(MCSymbol *LabelSym,
  680. uint64_t Size,
  681. MCSymbol *CsectSym,
  682. unsigned ByteAlignment) {
  683. assert(MAI->getLCOMMDirectiveAlignmentType() == LCOMM::Log2Alignment &&
  684. "We only support writing log base-2 alignment format with XCOFF.");
  685. assert(isPowerOf2_32(ByteAlignment) && "Alignment must be a power of 2.");
  686. OS << "\t.lcomm\t";
  687. LabelSym->print(OS, MAI);
  688. OS << ',' << Size << ',';
  689. CsectSym->print(OS, MAI);
  690. OS << ',' << Log2_32(ByteAlignment);
  691. EmitEOL();
  692. // Print symbol's rename (original name contains invalid character(s)) if
  693. // there is one.
  694. MCSymbolXCOFF *XSym = cast<MCSymbolXCOFF>(CsectSym);
  695. if (XSym->hasRename())
  696. emitXCOFFRenameDirective(XSym, XSym->getSymbolTableName());
  697. }
  698. void MCAsmStreamer::emitXCOFFSymbolLinkageWithVisibility(
  699. MCSymbol *Symbol, MCSymbolAttr Linkage, MCSymbolAttr Visibility) {
  700. switch (Linkage) {
  701. case MCSA_Global:
  702. OS << MAI->getGlobalDirective();
  703. break;
  704. case MCSA_Weak:
  705. OS << MAI->getWeakDirective();
  706. break;
  707. case MCSA_Extern:
  708. OS << "\t.extern\t";
  709. break;
  710. case MCSA_LGlobal:
  711. OS << "\t.lglobl\t";
  712. break;
  713. default:
  714. report_fatal_error("unhandled linkage type");
  715. }
  716. Symbol->print(OS, MAI);
  717. switch (Visibility) {
  718. case MCSA_Invalid:
  719. // Nothing to do.
  720. break;
  721. case MCSA_Hidden:
  722. OS << ",hidden";
  723. break;
  724. case MCSA_Protected:
  725. OS << ",protected";
  726. break;
  727. default:
  728. report_fatal_error("unexpected value for Visibility type");
  729. }
  730. EmitEOL();
  731. // Print symbol's rename (original name contains invalid character(s)) if
  732. // there is one.
  733. if (cast<MCSymbolXCOFF>(Symbol)->hasRename())
  734. emitXCOFFRenameDirective(Symbol,
  735. cast<MCSymbolXCOFF>(Symbol)->getSymbolTableName());
  736. }
  737. void MCAsmStreamer::emitXCOFFRenameDirective(const MCSymbol *Name,
  738. StringRef Rename) {
  739. OS << "\t.rename\t";
  740. Name->print(OS, MAI);
  741. const char DQ = '"';
  742. OS << ',' << DQ;
  743. for (char C : Rename) {
  744. // To escape a double quote character, the character should be doubled.
  745. if (C == DQ)
  746. OS << DQ;
  747. OS << C;
  748. }
  749. OS << DQ;
  750. EmitEOL();
  751. }
  752. void MCAsmStreamer::emitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
  753. assert(MAI->hasDotTypeDotSizeDirective());
  754. OS << "\t.size\t";
  755. Symbol->print(OS, MAI);
  756. OS << ", ";
  757. Value->print(OS, MAI);
  758. EmitEOL();
  759. }
  760. void MCAsmStreamer::emitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
  761. unsigned ByteAlignment) {
  762. OS << "\t.comm\t";
  763. Symbol->print(OS, MAI);
  764. OS << ',' << Size;
  765. if (ByteAlignment != 0) {
  766. if (MAI->getCOMMDirectiveAlignmentIsInBytes())
  767. OS << ',' << ByteAlignment;
  768. else
  769. OS << ',' << Log2_32(ByteAlignment);
  770. }
  771. EmitEOL();
  772. // Print symbol's rename (original name contains invalid character(s)) if
  773. // there is one.
  774. MCSymbolXCOFF *XSym = dyn_cast<MCSymbolXCOFF>(Symbol);
  775. if (XSym && XSym->hasRename())
  776. emitXCOFFRenameDirective(XSym, XSym->getSymbolTableName());
  777. }
  778. void MCAsmStreamer::emitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
  779. unsigned ByteAlign) {
  780. OS << "\t.lcomm\t";
  781. Symbol->print(OS, MAI);
  782. OS << ',' << Size;
  783. if (ByteAlign > 1) {
  784. switch (MAI->getLCOMMDirectiveAlignmentType()) {
  785. case LCOMM::NoAlignment:
  786. llvm_unreachable("alignment not supported on .lcomm!");
  787. case LCOMM::ByteAlignment:
  788. OS << ',' << ByteAlign;
  789. break;
  790. case LCOMM::Log2Alignment:
  791. assert(isPowerOf2_32(ByteAlign) && "alignment must be a power of 2");
  792. OS << ',' << Log2_32(ByteAlign);
  793. break;
  794. }
  795. }
  796. EmitEOL();
  797. }
  798. void MCAsmStreamer::emitZerofill(MCSection *Section, MCSymbol *Symbol,
  799. uint64_t Size, unsigned ByteAlignment,
  800. SMLoc Loc) {
  801. if (Symbol)
  802. AssignFragment(Symbol, &Section->getDummyFragment());
  803. // Note: a .zerofill directive does not switch sections.
  804. OS << ".zerofill ";
  805. assert(Section->getVariant() == MCSection::SV_MachO &&
  806. ".zerofill is a Mach-O specific directive");
  807. // This is a mach-o specific directive.
  808. const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
  809. OS << MOSection->getSegmentName() << "," << MOSection->getName();
  810. if (Symbol) {
  811. OS << ',';
  812. Symbol->print(OS, MAI);
  813. OS << ',' << Size;
  814. if (ByteAlignment != 0)
  815. OS << ',' << Log2_32(ByteAlignment);
  816. }
  817. EmitEOL();
  818. }
  819. // .tbss sym, size, align
  820. // This depends that the symbol has already been mangled from the original,
  821. // e.g. _a.
  822. void MCAsmStreamer::emitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
  823. uint64_t Size, unsigned ByteAlignment) {
  824. AssignFragment(Symbol, &Section->getDummyFragment());
  825. assert(Symbol && "Symbol shouldn't be NULL!");
  826. // Instead of using the Section we'll just use the shortcut.
  827. assert(Section->getVariant() == MCSection::SV_MachO &&
  828. ".zerofill is a Mach-O specific directive");
  829. // This is a mach-o specific directive and section.
  830. OS << ".tbss ";
  831. Symbol->print(OS, MAI);
  832. OS << ", " << Size;
  833. // Output align if we have it. We default to 1 so don't bother printing
  834. // that.
  835. if (ByteAlignment > 1) OS << ", " << Log2_32(ByteAlignment);
  836. EmitEOL();
  837. }
  838. static inline char toOctal(int X) { return (X&7)+'0'; }
  839. static void PrintByteList(StringRef Data, raw_ostream &OS,
  840. MCAsmInfo::AsmCharLiteralSyntax ACLS) {
  841. assert(!Data.empty() && "Cannot generate an empty list.");
  842. const auto printCharacterInOctal = [&OS](unsigned char C) {
  843. OS << '0';
  844. OS << toOctal(C >> 6);
  845. OS << toOctal(C >> 3);
  846. OS << toOctal(C >> 0);
  847. };
  848. const auto printOneCharacterFor = [printCharacterInOctal](
  849. auto printOnePrintingCharacter) {
  850. return [printCharacterInOctal, printOnePrintingCharacter](unsigned char C) {
  851. if (isPrint(C)) {
  852. printOnePrintingCharacter(static_cast<char>(C));
  853. return;
  854. }
  855. printCharacterInOctal(C);
  856. };
  857. };
  858. const auto printCharacterList = [Data, &OS](const auto &printOneCharacter) {
  859. const auto BeginPtr = Data.begin(), EndPtr = Data.end();
  860. for (const unsigned char C : make_range(BeginPtr, EndPtr - 1)) {
  861. printOneCharacter(C);
  862. OS << ',';
  863. }
  864. printOneCharacter(*(EndPtr - 1));
  865. };
  866. switch (ACLS) {
  867. case MCAsmInfo::ACLS_Unknown:
  868. printCharacterList(printCharacterInOctal);
  869. return;
  870. case MCAsmInfo::ACLS_SingleQuotePrefix:
  871. printCharacterList(printOneCharacterFor([&OS](char C) {
  872. const char AsmCharLitBuf[2] = {'\'', C};
  873. OS << StringRef(AsmCharLitBuf, sizeof(AsmCharLitBuf));
  874. }));
  875. return;
  876. }
  877. llvm_unreachable("Invalid AsmCharLiteralSyntax value!");
  878. }
  879. static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
  880. OS << '"';
  881. for (unsigned i = 0, e = Data.size(); i != e; ++i) {
  882. unsigned char C = Data[i];
  883. if (C == '"' || C == '\\') {
  884. OS << '\\' << (char)C;
  885. continue;
  886. }
  887. if (isPrint((unsigned char)C)) {
  888. OS << (char)C;
  889. continue;
  890. }
  891. switch (C) {
  892. case '\b': OS << "\\b"; break;
  893. case '\f': OS << "\\f"; break;
  894. case '\n': OS << "\\n"; break;
  895. case '\r': OS << "\\r"; break;
  896. case '\t': OS << "\\t"; break;
  897. default:
  898. OS << '\\';
  899. OS << toOctal(C >> 6);
  900. OS << toOctal(C >> 3);
  901. OS << toOctal(C >> 0);
  902. break;
  903. }
  904. }
  905. OS << '"';
  906. }
  907. void MCAsmStreamer::emitBytes(StringRef Data) {
  908. assert(getCurrentSectionOnly() &&
  909. "Cannot emit contents before setting section!");
  910. if (Data.empty()) return;
  911. const auto emitAsString = [this](StringRef Data) {
  912. // If the data ends with 0 and the target supports .asciz, use it, otherwise
  913. // use .ascii or a byte-list directive
  914. if (MAI->getAscizDirective() && Data.back() == 0) {
  915. OS << MAI->getAscizDirective();
  916. Data = Data.substr(0, Data.size() - 1);
  917. } else if (LLVM_LIKELY(MAI->getAsciiDirective())) {
  918. OS << MAI->getAsciiDirective();
  919. } else if (MAI->getByteListDirective()) {
  920. OS << MAI->getByteListDirective();
  921. PrintByteList(Data, OS, MAI->characterLiteralSyntax());
  922. EmitEOL();
  923. return true;
  924. } else {
  925. return false;
  926. }
  927. PrintQuotedString(Data, OS);
  928. EmitEOL();
  929. return true;
  930. };
  931. if (Data.size() != 1 && emitAsString(Data))
  932. return;
  933. // Only single byte is provided or no ascii, asciz, or byte-list directives
  934. // are applicable. Emit as vector of individual 8bits data elements.
  935. if (MCTargetStreamer *TS = getTargetStreamer()) {
  936. TS->emitRawBytes(Data);
  937. return;
  938. }
  939. const char *Directive = MAI->getData8bitsDirective();
  940. for (const unsigned char C : Data.bytes()) {
  941. OS << Directive << (unsigned)C;
  942. EmitEOL();
  943. }
  944. }
  945. void MCAsmStreamer::emitBinaryData(StringRef Data) {
  946. // This is binary data. Print it in a grid of hex bytes for readability.
  947. const size_t Cols = 4;
  948. for (size_t I = 0, EI = alignTo(Data.size(), Cols); I < EI; I += Cols) {
  949. size_t J = I, EJ = std::min(I + Cols, Data.size());
  950. assert(EJ > 0);
  951. OS << MAI->getData8bitsDirective();
  952. for (; J < EJ - 1; ++J)
  953. OS << format("0x%02x", uint8_t(Data[J])) << ", ";
  954. OS << format("0x%02x", uint8_t(Data[J]));
  955. EmitEOL();
  956. }
  957. }
  958. void MCAsmStreamer::emitIntValue(uint64_t Value, unsigned Size) {
  959. emitValue(MCConstantExpr::create(Value, getContext()), Size);
  960. }
  961. void MCAsmStreamer::emitIntValueInHex(uint64_t Value, unsigned Size) {
  962. emitValue(MCConstantExpr::create(Value, getContext(), true), Size);
  963. }
  964. void MCAsmStreamer::emitIntValueInHexWithPadding(uint64_t Value,
  965. unsigned Size) {
  966. emitValue(MCConstantExpr::create(Value, getContext(), true, Size), Size);
  967. }
  968. void MCAsmStreamer::emitValueImpl(const MCExpr *Value, unsigned Size,
  969. SMLoc Loc) {
  970. assert(Size <= 8 && "Invalid size");
  971. assert(getCurrentSectionOnly() &&
  972. "Cannot emit contents before setting section!");
  973. const char *Directive = nullptr;
  974. switch (Size) {
  975. default: break;
  976. case 1: Directive = MAI->getData8bitsDirective(); break;
  977. case 2: Directive = MAI->getData16bitsDirective(); break;
  978. case 4: Directive = MAI->getData32bitsDirective(); break;
  979. case 8: Directive = MAI->getData64bitsDirective(); break;
  980. }
  981. if (!Directive) {
  982. int64_t IntValue;
  983. if (!Value->evaluateAsAbsolute(IntValue))
  984. report_fatal_error("Don't know how to emit this value.");
  985. // We couldn't handle the requested integer size so we fallback by breaking
  986. // the request down into several, smaller, integers.
  987. // Since sizes greater or equal to "Size" are invalid, we use the greatest
  988. // power of 2 that is less than "Size" as our largest piece of granularity.
  989. bool IsLittleEndian = MAI->isLittleEndian();
  990. for (unsigned Emitted = 0; Emitted != Size;) {
  991. unsigned Remaining = Size - Emitted;
  992. // The size of our partial emission must be a power of two less than
  993. // Size.
  994. unsigned EmissionSize = PowerOf2Floor(std::min(Remaining, Size - 1));
  995. // Calculate the byte offset of our partial emission taking into account
  996. // the endianness of the target.
  997. unsigned ByteOffset =
  998. IsLittleEndian ? Emitted : (Remaining - EmissionSize);
  999. uint64_t ValueToEmit = IntValue >> (ByteOffset * 8);
  1000. // We truncate our partial emission to fit within the bounds of the
  1001. // emission domain. This produces nicer output and silences potential
  1002. // truncation warnings when round tripping through another assembler.
  1003. uint64_t Shift = 64 - EmissionSize * 8;
  1004. assert(Shift < static_cast<uint64_t>(
  1005. std::numeric_limits<unsigned long long>::digits) &&
  1006. "undefined behavior");
  1007. ValueToEmit &= ~0ULL >> Shift;
  1008. emitIntValue(ValueToEmit, EmissionSize);
  1009. Emitted += EmissionSize;
  1010. }
  1011. return;
  1012. }
  1013. assert(Directive && "Invalid size for machine code value!");
  1014. OS << Directive;
  1015. if (MCTargetStreamer *TS = getTargetStreamer()) {
  1016. TS->emitValue(Value);
  1017. } else {
  1018. Value->print(OS, MAI);
  1019. EmitEOL();
  1020. }
  1021. }
  1022. void MCAsmStreamer::emitULEB128Value(const MCExpr *Value) {
  1023. int64_t IntValue;
  1024. if (Value->evaluateAsAbsolute(IntValue)) {
  1025. emitULEB128IntValue(IntValue);
  1026. return;
  1027. }
  1028. OS << "\t.uleb128 ";
  1029. Value->print(OS, MAI);
  1030. EmitEOL();
  1031. }
  1032. void MCAsmStreamer::emitSLEB128Value(const MCExpr *Value) {
  1033. int64_t IntValue;
  1034. if (Value->evaluateAsAbsolute(IntValue)) {
  1035. emitSLEB128IntValue(IntValue);
  1036. return;
  1037. }
  1038. OS << "\t.sleb128 ";
  1039. Value->print(OS, MAI);
  1040. EmitEOL();
  1041. }
  1042. void MCAsmStreamer::emitDTPRel64Value(const MCExpr *Value) {
  1043. assert(MAI->getDTPRel64Directive() != nullptr);
  1044. OS << MAI->getDTPRel64Directive();
  1045. Value->print(OS, MAI);
  1046. EmitEOL();
  1047. }
  1048. void MCAsmStreamer::emitDTPRel32Value(const MCExpr *Value) {
  1049. assert(MAI->getDTPRel32Directive() != nullptr);
  1050. OS << MAI->getDTPRel32Directive();
  1051. Value->print(OS, MAI);
  1052. EmitEOL();
  1053. }
  1054. void MCAsmStreamer::emitTPRel64Value(const MCExpr *Value) {
  1055. assert(MAI->getTPRel64Directive() != nullptr);
  1056. OS << MAI->getTPRel64Directive();
  1057. Value->print(OS, MAI);
  1058. EmitEOL();
  1059. }
  1060. void MCAsmStreamer::emitTPRel32Value(const MCExpr *Value) {
  1061. assert(MAI->getTPRel32Directive() != nullptr);
  1062. OS << MAI->getTPRel32Directive();
  1063. Value->print(OS, MAI);
  1064. EmitEOL();
  1065. }
  1066. void MCAsmStreamer::emitGPRel64Value(const MCExpr *Value) {
  1067. assert(MAI->getGPRel64Directive() != nullptr);
  1068. OS << MAI->getGPRel64Directive();
  1069. Value->print(OS, MAI);
  1070. EmitEOL();
  1071. }
  1072. void MCAsmStreamer::emitGPRel32Value(const MCExpr *Value) {
  1073. assert(MAI->getGPRel32Directive() != nullptr);
  1074. OS << MAI->getGPRel32Directive();
  1075. Value->print(OS, MAI);
  1076. EmitEOL();
  1077. }
  1078. void MCAsmStreamer::emitFill(const MCExpr &NumBytes, uint64_t FillValue,
  1079. SMLoc Loc) {
  1080. int64_t IntNumBytes;
  1081. const bool IsAbsolute = NumBytes.evaluateAsAbsolute(IntNumBytes);
  1082. if (IsAbsolute && IntNumBytes == 0)
  1083. return;
  1084. if (const char *ZeroDirective = MAI->getZeroDirective()) {
  1085. if (MAI->doesZeroDirectiveSupportNonZeroValue() || FillValue == 0) {
  1086. // FIXME: Emit location directives
  1087. OS << ZeroDirective;
  1088. NumBytes.print(OS, MAI);
  1089. if (FillValue != 0)
  1090. OS << ',' << (int)FillValue;
  1091. EmitEOL();
  1092. } else {
  1093. if (!IsAbsolute)
  1094. report_fatal_error(
  1095. "Cannot emit non-absolute expression lengths of fill.");
  1096. for (int i = 0; i < IntNumBytes; ++i) {
  1097. OS << MAI->getData8bitsDirective() << (int)FillValue;
  1098. EmitEOL();
  1099. }
  1100. }
  1101. return;
  1102. }
  1103. MCStreamer::emitFill(NumBytes, FillValue);
  1104. }
  1105. void MCAsmStreamer::emitFill(const MCExpr &NumValues, int64_t Size,
  1106. int64_t Expr, SMLoc Loc) {
  1107. // FIXME: Emit location directives
  1108. OS << "\t.fill\t";
  1109. NumValues.print(OS, MAI);
  1110. OS << ", " << Size << ", 0x";
  1111. OS.write_hex(truncateToSize(Expr, 4));
  1112. EmitEOL();
  1113. }
  1114. void MCAsmStreamer::emitValueToAlignment(unsigned ByteAlignment, int64_t Value,
  1115. unsigned ValueSize,
  1116. unsigned MaxBytesToEmit) {
  1117. if (MAI->useDotAlignForAlignment()) {
  1118. if (!isPowerOf2_32(ByteAlignment))
  1119. report_fatal_error("Only power-of-two alignments are supported "
  1120. "with .align.");
  1121. OS << "\t.align\t";
  1122. OS << Log2_32(ByteAlignment);
  1123. EmitEOL();
  1124. return;
  1125. }
  1126. // Some assemblers don't support non-power of two alignments, so we always
  1127. // emit alignments as a power of two if possible.
  1128. if (isPowerOf2_32(ByteAlignment)) {
  1129. switch (ValueSize) {
  1130. default:
  1131. llvm_unreachable("Invalid size for machine code value!");
  1132. case 1:
  1133. OS << "\t.p2align\t";
  1134. break;
  1135. case 2:
  1136. OS << ".p2alignw ";
  1137. break;
  1138. case 4:
  1139. OS << ".p2alignl ";
  1140. break;
  1141. case 8:
  1142. llvm_unreachable("Unsupported alignment size!");
  1143. }
  1144. OS << Log2_32(ByteAlignment);
  1145. if (Value || MaxBytesToEmit) {
  1146. OS << ", 0x";
  1147. OS.write_hex(truncateToSize(Value, ValueSize));
  1148. if (MaxBytesToEmit)
  1149. OS << ", " << MaxBytesToEmit;
  1150. }
  1151. EmitEOL();
  1152. return;
  1153. }
  1154. // Non-power of two alignment. This is not widely supported by assemblers.
  1155. // FIXME: Parameterize this based on MAI.
  1156. switch (ValueSize) {
  1157. default: llvm_unreachable("Invalid size for machine code value!");
  1158. case 1: OS << ".balign"; break;
  1159. case 2: OS << ".balignw"; break;
  1160. case 4: OS << ".balignl"; break;
  1161. case 8: llvm_unreachable("Unsupported alignment size!");
  1162. }
  1163. OS << ' ' << ByteAlignment;
  1164. OS << ", " << truncateToSize(Value, ValueSize);
  1165. if (MaxBytesToEmit)
  1166. OS << ", " << MaxBytesToEmit;
  1167. EmitEOL();
  1168. }
  1169. void MCAsmStreamer::emitCodeAlignment(unsigned ByteAlignment,
  1170. unsigned MaxBytesToEmit) {
  1171. // Emit with a text fill value.
  1172. emitValueToAlignment(ByteAlignment, MAI->getTextAlignFillValue(),
  1173. 1, MaxBytesToEmit);
  1174. }
  1175. void MCAsmStreamer::emitValueToOffset(const MCExpr *Offset,
  1176. unsigned char Value,
  1177. SMLoc Loc) {
  1178. // FIXME: Verify that Offset is associated with the current section.
  1179. OS << ".org ";
  1180. Offset->print(OS, MAI);
  1181. OS << ", " << (unsigned)Value;
  1182. EmitEOL();
  1183. }
  1184. void MCAsmStreamer::emitFileDirective(StringRef Filename) {
  1185. assert(MAI->hasSingleParameterDotFile());
  1186. OS << "\t.file\t";
  1187. PrintQuotedString(Filename, OS);
  1188. EmitEOL();
  1189. }
  1190. static void printDwarfFileDirective(unsigned FileNo, StringRef Directory,
  1191. StringRef Filename,
  1192. Optional<MD5::MD5Result> Checksum,
  1193. Optional<StringRef> Source,
  1194. bool UseDwarfDirectory,
  1195. raw_svector_ostream &OS) {
  1196. SmallString<128> FullPathName;
  1197. if (!UseDwarfDirectory && !Directory.empty()) {
  1198. if (sys::path::is_absolute(Filename))
  1199. Directory = "";
  1200. else {
  1201. FullPathName = Directory;
  1202. sys::path::append(FullPathName, Filename);
  1203. Directory = "";
  1204. Filename = FullPathName;
  1205. }
  1206. }
  1207. OS << "\t.file\t" << FileNo << ' ';
  1208. if (!Directory.empty()) {
  1209. PrintQuotedString(Directory, OS);
  1210. OS << ' ';
  1211. }
  1212. PrintQuotedString(Filename, OS);
  1213. if (Checksum)
  1214. OS << " md5 0x" << Checksum->digest();
  1215. if (Source) {
  1216. OS << " source ";
  1217. PrintQuotedString(*Source, OS);
  1218. }
  1219. }
  1220. Expected<unsigned> MCAsmStreamer::tryEmitDwarfFileDirective(
  1221. unsigned FileNo, StringRef Directory, StringRef Filename,
  1222. Optional<MD5::MD5Result> Checksum, Optional<StringRef> Source, unsigned CUID) {
  1223. assert(CUID == 0 && "multiple CUs not supported by MCAsmStreamer");
  1224. MCDwarfLineTable &Table = getContext().getMCDwarfLineTable(CUID);
  1225. unsigned NumFiles = Table.getMCDwarfFiles().size();
  1226. Expected<unsigned> FileNoOrErr =
  1227. Table.tryGetFile(Directory, Filename, Checksum, Source,
  1228. getContext().getDwarfVersion(), FileNo);
  1229. if (!FileNoOrErr)
  1230. return FileNoOrErr.takeError();
  1231. FileNo = FileNoOrErr.get();
  1232. if (NumFiles == Table.getMCDwarfFiles().size())
  1233. return FileNo;
  1234. SmallString<128> Str;
  1235. raw_svector_ostream OS1(Str);
  1236. printDwarfFileDirective(FileNo, Directory, Filename, Checksum, Source,
  1237. UseDwarfDirectory, OS1);
  1238. if (MCTargetStreamer *TS = getTargetStreamer())
  1239. TS->emitDwarfFileDirective(OS1.str());
  1240. else
  1241. emitRawText(OS1.str());
  1242. return FileNo;
  1243. }
  1244. void MCAsmStreamer::emitDwarfFile0Directive(StringRef Directory,
  1245. StringRef Filename,
  1246. Optional<MD5::MD5Result> Checksum,
  1247. Optional<StringRef> Source,
  1248. unsigned CUID) {
  1249. assert(CUID == 0);
  1250. // .file 0 is new for DWARF v5.
  1251. if (getContext().getDwarfVersion() < 5)
  1252. return;
  1253. // Inform MCDwarf about the root file.
  1254. getContext().setMCLineTableRootFile(CUID, Directory, Filename, Checksum,
  1255. Source);
  1256. SmallString<128> Str;
  1257. raw_svector_ostream OS1(Str);
  1258. printDwarfFileDirective(0, Directory, Filename, Checksum, Source,
  1259. UseDwarfDirectory, OS1);
  1260. if (MCTargetStreamer *TS = getTargetStreamer())
  1261. TS->emitDwarfFileDirective(OS1.str());
  1262. else
  1263. emitRawText(OS1.str());
  1264. }
  1265. void MCAsmStreamer::emitDwarfLocDirective(unsigned FileNo, unsigned Line,
  1266. unsigned Column, unsigned Flags,
  1267. unsigned Isa, unsigned Discriminator,
  1268. StringRef FileName) {
  1269. OS << "\t.loc\t" << FileNo << " " << Line << " " << Column;
  1270. if (MAI->supportsExtendedDwarfLocDirective()) {
  1271. if (Flags & DWARF2_FLAG_BASIC_BLOCK)
  1272. OS << " basic_block";
  1273. if (Flags & DWARF2_FLAG_PROLOGUE_END)
  1274. OS << " prologue_end";
  1275. if (Flags & DWARF2_FLAG_EPILOGUE_BEGIN)
  1276. OS << " epilogue_begin";
  1277. unsigned OldFlags = getContext().getCurrentDwarfLoc().getFlags();
  1278. if ((Flags & DWARF2_FLAG_IS_STMT) != (OldFlags & DWARF2_FLAG_IS_STMT)) {
  1279. OS << " is_stmt ";
  1280. if (Flags & DWARF2_FLAG_IS_STMT)
  1281. OS << "1";
  1282. else
  1283. OS << "0";
  1284. }
  1285. if (Isa)
  1286. OS << " isa " << Isa;
  1287. if (Discriminator)
  1288. OS << " discriminator " << Discriminator;
  1289. }
  1290. if (IsVerboseAsm) {
  1291. OS.PadToColumn(MAI->getCommentColumn());
  1292. OS << MAI->getCommentString() << ' ' << FileName << ':'
  1293. << Line << ':' << Column;
  1294. }
  1295. EmitEOL();
  1296. this->MCStreamer::emitDwarfLocDirective(FileNo, Line, Column, Flags, Isa,
  1297. Discriminator, FileName);
  1298. }
  1299. MCSymbol *MCAsmStreamer::getDwarfLineTableSymbol(unsigned CUID) {
  1300. // Always use the zeroth line table, since asm syntax only supports one line
  1301. // table for now.
  1302. return MCStreamer::getDwarfLineTableSymbol(0);
  1303. }
  1304. bool MCAsmStreamer::EmitCVFileDirective(unsigned FileNo, StringRef Filename,
  1305. ArrayRef<uint8_t> Checksum,
  1306. unsigned ChecksumKind) {
  1307. if (!getContext().getCVContext().addFile(*this, FileNo, Filename, Checksum,
  1308. ChecksumKind))
  1309. return false;
  1310. OS << "\t.cv_file\t" << FileNo << ' ';
  1311. PrintQuotedString(Filename, OS);
  1312. if (!ChecksumKind) {
  1313. EmitEOL();
  1314. return true;
  1315. }
  1316. OS << ' ';
  1317. PrintQuotedString(toHex(Checksum), OS);
  1318. OS << ' ' << ChecksumKind;
  1319. EmitEOL();
  1320. return true;
  1321. }
  1322. bool MCAsmStreamer::EmitCVFuncIdDirective(unsigned FuncId) {
  1323. OS << "\t.cv_func_id " << FuncId << '\n';
  1324. return MCStreamer::EmitCVFuncIdDirective(FuncId);
  1325. }
  1326. bool MCAsmStreamer::EmitCVInlineSiteIdDirective(unsigned FunctionId,
  1327. unsigned IAFunc,
  1328. unsigned IAFile,
  1329. unsigned IALine, unsigned IACol,
  1330. SMLoc Loc) {
  1331. OS << "\t.cv_inline_site_id " << FunctionId << " within " << IAFunc
  1332. << " inlined_at " << IAFile << ' ' << IALine << ' ' << IACol << '\n';
  1333. return MCStreamer::EmitCVInlineSiteIdDirective(FunctionId, IAFunc, IAFile,
  1334. IALine, IACol, Loc);
  1335. }
  1336. void MCAsmStreamer::emitCVLocDirective(unsigned FunctionId, unsigned FileNo,
  1337. unsigned Line, unsigned Column,
  1338. bool PrologueEnd, bool IsStmt,
  1339. StringRef FileName, SMLoc Loc) {
  1340. // Validate the directive.
  1341. if (!checkCVLocSection(FunctionId, FileNo, Loc))
  1342. return;
  1343. OS << "\t.cv_loc\t" << FunctionId << " " << FileNo << " " << Line << " "
  1344. << Column;
  1345. if (PrologueEnd)
  1346. OS << " prologue_end";
  1347. if (IsStmt)
  1348. OS << " is_stmt 1";
  1349. if (IsVerboseAsm) {
  1350. OS.PadToColumn(MAI->getCommentColumn());
  1351. OS << MAI->getCommentString() << ' ' << FileName << ':' << Line << ':'
  1352. << Column;
  1353. }
  1354. EmitEOL();
  1355. }
  1356. void MCAsmStreamer::emitCVLinetableDirective(unsigned FunctionId,
  1357. const MCSymbol *FnStart,
  1358. const MCSymbol *FnEnd) {
  1359. OS << "\t.cv_linetable\t" << FunctionId << ", ";
  1360. FnStart->print(OS, MAI);
  1361. OS << ", ";
  1362. FnEnd->print(OS, MAI);
  1363. EmitEOL();
  1364. this->MCStreamer::emitCVLinetableDirective(FunctionId, FnStart, FnEnd);
  1365. }
  1366. void MCAsmStreamer::emitCVInlineLinetableDirective(unsigned PrimaryFunctionId,
  1367. unsigned SourceFileId,
  1368. unsigned SourceLineNum,
  1369. const MCSymbol *FnStartSym,
  1370. const MCSymbol *FnEndSym) {
  1371. OS << "\t.cv_inline_linetable\t" << PrimaryFunctionId << ' ' << SourceFileId
  1372. << ' ' << SourceLineNum << ' ';
  1373. FnStartSym->print(OS, MAI);
  1374. OS << ' ';
  1375. FnEndSym->print(OS, MAI);
  1376. EmitEOL();
  1377. this->MCStreamer::emitCVInlineLinetableDirective(
  1378. PrimaryFunctionId, SourceFileId, SourceLineNum, FnStartSym, FnEndSym);
  1379. }
  1380. void MCAsmStreamer::PrintCVDefRangePrefix(
  1381. ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges) {
  1382. OS << "\t.cv_def_range\t";
  1383. for (std::pair<const MCSymbol *, const MCSymbol *> Range : Ranges) {
  1384. OS << ' ';
  1385. Range.first->print(OS, MAI);
  1386. OS << ' ';
  1387. Range.second->print(OS, MAI);
  1388. }
  1389. }
  1390. void MCAsmStreamer::emitCVDefRangeDirective(
  1391. ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
  1392. codeview::DefRangeRegisterRelHeader DRHdr) {
  1393. PrintCVDefRangePrefix(Ranges);
  1394. OS << ", reg_rel, ";
  1395. OS << DRHdr.Register << ", " << DRHdr.Flags << ", "
  1396. << DRHdr.BasePointerOffset;
  1397. EmitEOL();
  1398. }
  1399. void MCAsmStreamer::emitCVDefRangeDirective(
  1400. ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
  1401. codeview::DefRangeSubfieldRegisterHeader DRHdr) {
  1402. PrintCVDefRangePrefix(Ranges);
  1403. OS << ", subfield_reg, ";
  1404. OS << DRHdr.Register << ", " << DRHdr.OffsetInParent;
  1405. EmitEOL();
  1406. }
  1407. void MCAsmStreamer::emitCVDefRangeDirective(
  1408. ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
  1409. codeview::DefRangeRegisterHeader DRHdr) {
  1410. PrintCVDefRangePrefix(Ranges);
  1411. OS << ", reg, ";
  1412. OS << DRHdr.Register;
  1413. EmitEOL();
  1414. }
  1415. void MCAsmStreamer::emitCVDefRangeDirective(
  1416. ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
  1417. codeview::DefRangeFramePointerRelHeader DRHdr) {
  1418. PrintCVDefRangePrefix(Ranges);
  1419. OS << ", frame_ptr_rel, ";
  1420. OS << DRHdr.Offset;
  1421. EmitEOL();
  1422. }
  1423. void MCAsmStreamer::emitCVStringTableDirective() {
  1424. OS << "\t.cv_stringtable";
  1425. EmitEOL();
  1426. }
  1427. void MCAsmStreamer::emitCVFileChecksumsDirective() {
  1428. OS << "\t.cv_filechecksums";
  1429. EmitEOL();
  1430. }
  1431. void MCAsmStreamer::emitCVFileChecksumOffsetDirective(unsigned FileNo) {
  1432. OS << "\t.cv_filechecksumoffset\t" << FileNo;
  1433. EmitEOL();
  1434. }
  1435. void MCAsmStreamer::EmitCVFPOData(const MCSymbol *ProcSym, SMLoc L) {
  1436. OS << "\t.cv_fpo_data\t";
  1437. ProcSym->print(OS, MAI);
  1438. EmitEOL();
  1439. }
  1440. void MCAsmStreamer::emitIdent(StringRef IdentString) {
  1441. assert(MAI->hasIdentDirective() && ".ident directive not supported");
  1442. OS << "\t.ident\t";
  1443. PrintQuotedString(IdentString, OS);
  1444. EmitEOL();
  1445. }
  1446. void MCAsmStreamer::emitCFISections(bool EH, bool Debug) {
  1447. MCStreamer::emitCFISections(EH, Debug);
  1448. OS << "\t.cfi_sections ";
  1449. if (EH) {
  1450. OS << ".eh_frame";
  1451. if (Debug)
  1452. OS << ", .debug_frame";
  1453. } else if (Debug) {
  1454. OS << ".debug_frame";
  1455. }
  1456. EmitEOL();
  1457. }
  1458. void MCAsmStreamer::emitCFIStartProcImpl(MCDwarfFrameInfo &Frame) {
  1459. OS << "\t.cfi_startproc";
  1460. if (Frame.IsSimple)
  1461. OS << " simple";
  1462. EmitEOL();
  1463. }
  1464. void MCAsmStreamer::emitCFIEndProcImpl(MCDwarfFrameInfo &Frame) {
  1465. MCStreamer::emitCFIEndProcImpl(Frame);
  1466. OS << "\t.cfi_endproc";
  1467. EmitEOL();
  1468. }
  1469. void MCAsmStreamer::EmitRegisterName(int64_t Register) {
  1470. if (!MAI->useDwarfRegNumForCFI()) {
  1471. // User .cfi_* directives can use arbitrary DWARF register numbers, not
  1472. // just ones that map to LLVM register numbers and have known names.
  1473. // Fall back to using the original number directly if no name is known.
  1474. const MCRegisterInfo *MRI = getContext().getRegisterInfo();
  1475. if (Optional<unsigned> LLVMRegister = MRI->getLLVMRegNum(Register, true)) {
  1476. InstPrinter->printRegName(OS, *LLVMRegister);
  1477. return;
  1478. }
  1479. }
  1480. OS << Register;
  1481. }
  1482. void MCAsmStreamer::emitCFIDefCfa(int64_t Register, int64_t Offset) {
  1483. MCStreamer::emitCFIDefCfa(Register, Offset);
  1484. OS << "\t.cfi_def_cfa ";
  1485. EmitRegisterName(Register);
  1486. OS << ", " << Offset;
  1487. EmitEOL();
  1488. }
  1489. void MCAsmStreamer::emitCFIDefCfaOffset(int64_t Offset) {
  1490. MCStreamer::emitCFIDefCfaOffset(Offset);
  1491. OS << "\t.cfi_def_cfa_offset " << Offset;
  1492. EmitEOL();
  1493. }
  1494. static void PrintCFIEscape(llvm::formatted_raw_ostream &OS, StringRef Values) {
  1495. OS << "\t.cfi_escape ";
  1496. if (!Values.empty()) {
  1497. size_t e = Values.size() - 1;
  1498. for (size_t i = 0; i < e; ++i)
  1499. OS << format("0x%02x", uint8_t(Values[i])) << ", ";
  1500. OS << format("0x%02x", uint8_t(Values[e]));
  1501. }
  1502. }
  1503. void MCAsmStreamer::emitCFIEscape(StringRef Values) {
  1504. MCStreamer::emitCFIEscape(Values);
  1505. PrintCFIEscape(OS, Values);
  1506. EmitEOL();
  1507. }
  1508. void MCAsmStreamer::emitCFIGnuArgsSize(int64_t Size) {
  1509. MCStreamer::emitCFIGnuArgsSize(Size);
  1510. uint8_t Buffer[16] = { dwarf::DW_CFA_GNU_args_size };
  1511. unsigned Len = encodeULEB128(Size, Buffer + 1) + 1;
  1512. PrintCFIEscape(OS, StringRef((const char *)&Buffer[0], Len));
  1513. EmitEOL();
  1514. }
  1515. void MCAsmStreamer::emitCFIDefCfaRegister(int64_t Register) {
  1516. MCStreamer::emitCFIDefCfaRegister(Register);
  1517. OS << "\t.cfi_def_cfa_register ";
  1518. EmitRegisterName(Register);
  1519. EmitEOL();
  1520. }
  1521. void MCAsmStreamer::emitCFIOffset(int64_t Register, int64_t Offset) {
  1522. this->MCStreamer::emitCFIOffset(Register, Offset);
  1523. OS << "\t.cfi_offset ";
  1524. EmitRegisterName(Register);
  1525. OS << ", " << Offset;
  1526. EmitEOL();
  1527. }
  1528. void MCAsmStreamer::emitCFIPersonality(const MCSymbol *Sym,
  1529. unsigned Encoding) {
  1530. MCStreamer::emitCFIPersonality(Sym, Encoding);
  1531. OS << "\t.cfi_personality " << Encoding << ", ";
  1532. Sym->print(OS, MAI);
  1533. EmitEOL();
  1534. }
  1535. void MCAsmStreamer::emitCFILsda(const MCSymbol *Sym, unsigned Encoding) {
  1536. MCStreamer::emitCFILsda(Sym, Encoding);
  1537. OS << "\t.cfi_lsda " << Encoding << ", ";
  1538. Sym->print(OS, MAI);
  1539. EmitEOL();
  1540. }
  1541. void MCAsmStreamer::emitCFIRememberState() {
  1542. MCStreamer::emitCFIRememberState();
  1543. OS << "\t.cfi_remember_state";
  1544. EmitEOL();
  1545. }
  1546. void MCAsmStreamer::emitCFIRestoreState() {
  1547. MCStreamer::emitCFIRestoreState();
  1548. OS << "\t.cfi_restore_state";
  1549. EmitEOL();
  1550. }
  1551. void MCAsmStreamer::emitCFIRestore(int64_t Register) {
  1552. MCStreamer::emitCFIRestore(Register);
  1553. OS << "\t.cfi_restore ";
  1554. EmitRegisterName(Register);
  1555. EmitEOL();
  1556. }
  1557. void MCAsmStreamer::emitCFISameValue(int64_t Register) {
  1558. MCStreamer::emitCFISameValue(Register);
  1559. OS << "\t.cfi_same_value ";
  1560. EmitRegisterName(Register);
  1561. EmitEOL();
  1562. }
  1563. void MCAsmStreamer::emitCFIRelOffset(int64_t Register, int64_t Offset) {
  1564. MCStreamer::emitCFIRelOffset(Register, Offset);
  1565. OS << "\t.cfi_rel_offset ";
  1566. EmitRegisterName(Register);
  1567. OS << ", " << Offset;
  1568. EmitEOL();
  1569. }
  1570. void MCAsmStreamer::emitCFIAdjustCfaOffset(int64_t Adjustment) {
  1571. MCStreamer::emitCFIAdjustCfaOffset(Adjustment);
  1572. OS << "\t.cfi_adjust_cfa_offset " << Adjustment;
  1573. EmitEOL();
  1574. }
  1575. void MCAsmStreamer::emitCFISignalFrame() {
  1576. MCStreamer::emitCFISignalFrame();
  1577. OS << "\t.cfi_signal_frame";
  1578. EmitEOL();
  1579. }
  1580. void MCAsmStreamer::emitCFIUndefined(int64_t Register) {
  1581. MCStreamer::emitCFIUndefined(Register);
  1582. OS << "\t.cfi_undefined ";
  1583. EmitRegisterName(Register);
  1584. EmitEOL();
  1585. }
  1586. void MCAsmStreamer::emitCFIRegister(int64_t Register1, int64_t Register2) {
  1587. MCStreamer::emitCFIRegister(Register1, Register2);
  1588. OS << "\t.cfi_register ";
  1589. EmitRegisterName(Register1);
  1590. OS << ", ";
  1591. EmitRegisterName(Register2);
  1592. EmitEOL();
  1593. }
  1594. void MCAsmStreamer::emitCFIWindowSave() {
  1595. MCStreamer::emitCFIWindowSave();
  1596. OS << "\t.cfi_window_save";
  1597. EmitEOL();
  1598. }
  1599. void MCAsmStreamer::emitCFINegateRAState() {
  1600. MCStreamer::emitCFINegateRAState();
  1601. OS << "\t.cfi_negate_ra_state";
  1602. EmitEOL();
  1603. }
  1604. void MCAsmStreamer::emitCFIReturnColumn(int64_t Register) {
  1605. MCStreamer::emitCFIReturnColumn(Register);
  1606. OS << "\t.cfi_return_column ";
  1607. EmitRegisterName(Register);
  1608. EmitEOL();
  1609. }
  1610. void MCAsmStreamer::emitCFIBKeyFrame() {
  1611. MCStreamer::emitCFIBKeyFrame();
  1612. OS << "\t.cfi_b_key_frame";
  1613. EmitEOL();
  1614. }
  1615. void MCAsmStreamer::EmitWinCFIStartProc(const MCSymbol *Symbol, SMLoc Loc) {
  1616. MCStreamer::EmitWinCFIStartProc(Symbol, Loc);
  1617. OS << ".seh_proc ";
  1618. Symbol->print(OS, MAI);
  1619. EmitEOL();
  1620. }
  1621. void MCAsmStreamer::EmitWinCFIEndProc(SMLoc Loc) {
  1622. MCStreamer::EmitWinCFIEndProc(Loc);
  1623. OS << "\t.seh_endproc";
  1624. EmitEOL();
  1625. }
  1626. void MCAsmStreamer::EmitWinCFIFuncletOrFuncEnd(SMLoc Loc) {
  1627. MCStreamer::EmitWinCFIFuncletOrFuncEnd(Loc);
  1628. OS << "\t.seh_endfunclet";
  1629. EmitEOL();
  1630. }
  1631. void MCAsmStreamer::EmitWinCFIStartChained(SMLoc Loc) {
  1632. MCStreamer::EmitWinCFIStartChained(Loc);
  1633. OS << "\t.seh_startchained";
  1634. EmitEOL();
  1635. }
  1636. void MCAsmStreamer::EmitWinCFIEndChained(SMLoc Loc) {
  1637. MCStreamer::EmitWinCFIEndChained(Loc);
  1638. OS << "\t.seh_endchained";
  1639. EmitEOL();
  1640. }
  1641. void MCAsmStreamer::EmitWinEHHandler(const MCSymbol *Sym, bool Unwind,
  1642. bool Except, SMLoc Loc) {
  1643. MCStreamer::EmitWinEHHandler(Sym, Unwind, Except, Loc);
  1644. OS << "\t.seh_handler ";
  1645. Sym->print(OS, MAI);
  1646. if (Unwind)
  1647. OS << ", @unwind";
  1648. if (Except)
  1649. OS << ", @except";
  1650. EmitEOL();
  1651. }
  1652. void MCAsmStreamer::EmitWinEHHandlerData(SMLoc Loc) {
  1653. MCStreamer::EmitWinEHHandlerData(Loc);
  1654. // Switch sections. Don't call SwitchSection directly, because that will
  1655. // cause the section switch to be visible in the emitted assembly.
  1656. // We only do this so the section switch that terminates the handler
  1657. // data block is visible.
  1658. WinEH::FrameInfo *CurFrame = getCurrentWinFrameInfo();
  1659. // Do nothing if no frame is open. MCStreamer should've already reported an
  1660. // error.
  1661. if (!CurFrame)
  1662. return;
  1663. MCSection *TextSec = &CurFrame->Function->getSection();
  1664. MCSection *XData = getAssociatedXDataSection(TextSec);
  1665. SwitchSectionNoChange(XData);
  1666. OS << "\t.seh_handlerdata";
  1667. EmitEOL();
  1668. }
  1669. void MCAsmStreamer::EmitWinCFIPushReg(MCRegister Register, SMLoc Loc) {
  1670. MCStreamer::EmitWinCFIPushReg(Register, Loc);
  1671. OS << "\t.seh_pushreg ";
  1672. InstPrinter->printRegName(OS, Register);
  1673. EmitEOL();
  1674. }
  1675. void MCAsmStreamer::EmitWinCFISetFrame(MCRegister Register, unsigned Offset,
  1676. SMLoc Loc) {
  1677. MCStreamer::EmitWinCFISetFrame(Register, Offset, Loc);
  1678. OS << "\t.seh_setframe ";
  1679. InstPrinter->printRegName(OS, Register);
  1680. OS << ", " << Offset;
  1681. EmitEOL();
  1682. }
  1683. void MCAsmStreamer::EmitWinCFIAllocStack(unsigned Size, SMLoc Loc) {
  1684. MCStreamer::EmitWinCFIAllocStack(Size, Loc);
  1685. OS << "\t.seh_stackalloc " << Size;
  1686. EmitEOL();
  1687. }
  1688. void MCAsmStreamer::EmitWinCFISaveReg(MCRegister Register, unsigned Offset,
  1689. SMLoc Loc) {
  1690. MCStreamer::EmitWinCFISaveReg(Register, Offset, Loc);
  1691. OS << "\t.seh_savereg ";
  1692. InstPrinter->printRegName(OS, Register);
  1693. OS << ", " << Offset;
  1694. EmitEOL();
  1695. }
  1696. void MCAsmStreamer::EmitWinCFISaveXMM(MCRegister Register, unsigned Offset,
  1697. SMLoc Loc) {
  1698. MCStreamer::EmitWinCFISaveXMM(Register, Offset, Loc);
  1699. OS << "\t.seh_savexmm ";
  1700. InstPrinter->printRegName(OS, Register);
  1701. OS << ", " << Offset;
  1702. EmitEOL();
  1703. }
  1704. void MCAsmStreamer::EmitWinCFIPushFrame(bool Code, SMLoc Loc) {
  1705. MCStreamer::EmitWinCFIPushFrame(Code, Loc);
  1706. OS << "\t.seh_pushframe";
  1707. if (Code)
  1708. OS << " @code";
  1709. EmitEOL();
  1710. }
  1711. void MCAsmStreamer::EmitWinCFIEndProlog(SMLoc Loc) {
  1712. MCStreamer::EmitWinCFIEndProlog(Loc);
  1713. OS << "\t.seh_endprologue";
  1714. EmitEOL();
  1715. }
  1716. void MCAsmStreamer::emitCGProfileEntry(const MCSymbolRefExpr *From,
  1717. const MCSymbolRefExpr *To,
  1718. uint64_t Count) {
  1719. OS << "\t.cg_profile ";
  1720. From->getSymbol().print(OS, MAI);
  1721. OS << ", ";
  1722. To->getSymbol().print(OS, MAI);
  1723. OS << ", " << Count;
  1724. EmitEOL();
  1725. }
  1726. void MCAsmStreamer::AddEncodingComment(const MCInst &Inst,
  1727. const MCSubtargetInfo &STI) {
  1728. raw_ostream &OS = GetCommentOS();
  1729. SmallString<256> Code;
  1730. SmallVector<MCFixup, 4> Fixups;
  1731. raw_svector_ostream VecOS(Code);
  1732. // If we have no code emitter, don't emit code.
  1733. if (!getAssembler().getEmitterPtr())
  1734. return;
  1735. getAssembler().getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
  1736. // If we are showing fixups, create symbolic markers in the encoded
  1737. // representation. We do this by making a per-bit map to the fixup item index,
  1738. // then trying to display it as nicely as possible.
  1739. SmallVector<uint8_t, 64> FixupMap;
  1740. FixupMap.resize(Code.size() * 8);
  1741. for (unsigned i = 0, e = Code.size() * 8; i != e; ++i)
  1742. FixupMap[i] = 0;
  1743. for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
  1744. MCFixup &F = Fixups[i];
  1745. const MCFixupKindInfo &Info =
  1746. getAssembler().getBackend().getFixupKindInfo(F.getKind());
  1747. for (unsigned j = 0; j != Info.TargetSize; ++j) {
  1748. unsigned Index = F.getOffset() * 8 + Info.TargetOffset + j;
  1749. assert(Index < Code.size() * 8 && "Invalid offset in fixup!");
  1750. FixupMap[Index] = 1 + i;
  1751. }
  1752. }
  1753. // FIXME: Note the fixup comments for Thumb2 are completely bogus since the
  1754. // high order halfword of a 32-bit Thumb2 instruction is emitted first.
  1755. OS << "encoding: [";
  1756. for (unsigned i = 0, e = Code.size(); i != e; ++i) {
  1757. if (i)
  1758. OS << ',';
  1759. // See if all bits are the same map entry.
  1760. uint8_t MapEntry = FixupMap[i * 8 + 0];
  1761. for (unsigned j = 1; j != 8; ++j) {
  1762. if (FixupMap[i * 8 + j] == MapEntry)
  1763. continue;
  1764. MapEntry = uint8_t(~0U);
  1765. break;
  1766. }
  1767. if (MapEntry != uint8_t(~0U)) {
  1768. if (MapEntry == 0) {
  1769. OS << format("0x%02x", uint8_t(Code[i]));
  1770. } else {
  1771. if (Code[i]) {
  1772. // FIXME: Some of the 8 bits require fix up.
  1773. OS << format("0x%02x", uint8_t(Code[i])) << '\''
  1774. << char('A' + MapEntry - 1) << '\'';
  1775. } else
  1776. OS << char('A' + MapEntry - 1);
  1777. }
  1778. } else {
  1779. // Otherwise, write out in binary.
  1780. OS << "0b";
  1781. for (unsigned j = 8; j--;) {
  1782. unsigned Bit = (Code[i] >> j) & 1;
  1783. unsigned FixupBit;
  1784. if (MAI->isLittleEndian())
  1785. FixupBit = i * 8 + j;
  1786. else
  1787. FixupBit = i * 8 + (7-j);
  1788. if (uint8_t MapEntry = FixupMap[FixupBit]) {
  1789. assert(Bit == 0 && "Encoder wrote into fixed up bit!");
  1790. OS << char('A' + MapEntry - 1);
  1791. } else
  1792. OS << Bit;
  1793. }
  1794. }
  1795. }
  1796. OS << "]\n";
  1797. for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
  1798. MCFixup &F = Fixups[i];
  1799. const MCFixupKindInfo &Info =
  1800. getAssembler().getBackend().getFixupKindInfo(F.getKind());
  1801. OS << " fixup " << char('A' + i) << " - " << "offset: " << F.getOffset()
  1802. << ", value: " << *F.getValue() << ", kind: " << Info.Name << "\n";
  1803. }
  1804. }
  1805. void MCAsmStreamer::emitInstruction(const MCInst &Inst,
  1806. const MCSubtargetInfo &STI) {
  1807. assert(getCurrentSectionOnly() &&
  1808. "Cannot emit contents before setting section!");
  1809. // Show the encoding in a comment if we have a code emitter.
  1810. AddEncodingComment(Inst, STI);
  1811. // Show the MCInst if enabled.
  1812. if (ShowInst) {
  1813. Inst.dump_pretty(GetCommentOS(), InstPrinter.get(), "\n ");
  1814. GetCommentOS() << "\n";
  1815. }
  1816. if(getTargetStreamer())
  1817. getTargetStreamer()->prettyPrintAsm(*InstPrinter, 0, Inst, STI, OS);
  1818. else
  1819. InstPrinter->printInst(&Inst, 0, "", STI, OS);
  1820. StringRef Comments = CommentToEmit;
  1821. if (Comments.size() && Comments.back() != '\n')
  1822. GetCommentOS() << "\n";
  1823. EmitEOL();
  1824. }
  1825. void MCAsmStreamer::emitPseudoProbe(
  1826. uint64_t Guid, uint64_t Index, uint64_t Type, uint64_t Attr,
  1827. const MCPseudoProbeInlineStack &InlineStack) {
  1828. OS << "\t.pseudoprobe\t" << Guid << " " << Index << " " << Type << " "
  1829. << Attr;
  1830. // Emit inline stack like
  1831. // @ GUIDmain:3 @ GUIDCaller:1 @ GUIDDirectCaller:11
  1832. for (const auto &Site : InlineStack)
  1833. OS << " @ " << std::get<0>(Site) << ":" << std::get<1>(Site);
  1834. EmitEOL();
  1835. }
  1836. void MCAsmStreamer::emitBundleAlignMode(unsigned AlignPow2) {
  1837. OS << "\t.bundle_align_mode " << AlignPow2;
  1838. EmitEOL();
  1839. }
  1840. void MCAsmStreamer::emitBundleLock(bool AlignToEnd) {
  1841. OS << "\t.bundle_lock";
  1842. if (AlignToEnd)
  1843. OS << " align_to_end";
  1844. EmitEOL();
  1845. }
  1846. void MCAsmStreamer::emitBundleUnlock() {
  1847. OS << "\t.bundle_unlock";
  1848. EmitEOL();
  1849. }
  1850. Optional<std::pair<bool, std::string>>
  1851. MCAsmStreamer::emitRelocDirective(const MCExpr &Offset, StringRef Name,
  1852. const MCExpr *Expr, SMLoc,
  1853. const MCSubtargetInfo &STI) {
  1854. OS << "\t.reloc ";
  1855. Offset.print(OS, MAI);
  1856. OS << ", " << Name;
  1857. if (Expr) {
  1858. OS << ", ";
  1859. Expr->print(OS, MAI);
  1860. }
  1861. EmitEOL();
  1862. return None;
  1863. }
  1864. void MCAsmStreamer::emitAddrsig() {
  1865. OS << "\t.addrsig";
  1866. EmitEOL();
  1867. }
  1868. void MCAsmStreamer::emitAddrsigSym(const MCSymbol *Sym) {
  1869. OS << "\t.addrsig_sym ";
  1870. Sym->print(OS, MAI);
  1871. EmitEOL();
  1872. }
  1873. /// EmitRawText - If this file is backed by an assembly streamer, this dumps
  1874. /// the specified string in the output .s file. This capability is
  1875. /// indicated by the hasRawTextSupport() predicate.
  1876. void MCAsmStreamer::emitRawTextImpl(StringRef String) {
  1877. if (!String.empty() && String.back() == '\n')
  1878. String = String.substr(0, String.size()-1);
  1879. OS << String;
  1880. EmitEOL();
  1881. }
  1882. void MCAsmStreamer::finishImpl() {
  1883. // If we are generating dwarf for assembly source files dump out the sections.
  1884. if (getContext().getGenDwarfForAssembly())
  1885. MCGenDwarfInfo::Emit(this);
  1886. // Emit the label for the line table, if requested - since the rest of the
  1887. // line table will be defined by .loc/.file directives, and not emitted
  1888. // directly, the label is the only work required here.
  1889. const auto &Tables = getContext().getMCDwarfLineTables();
  1890. if (!Tables.empty()) {
  1891. assert(Tables.size() == 1 && "asm output only supports one line table");
  1892. if (auto *Label = Tables.begin()->second.getLabel()) {
  1893. SwitchSection(getContext().getObjectFileInfo()->getDwarfLineSection());
  1894. emitLabel(Label);
  1895. }
  1896. }
  1897. }
  1898. MCStreamer *llvm::createAsmStreamer(MCContext &Context,
  1899. std::unique_ptr<formatted_raw_ostream> OS,
  1900. bool isVerboseAsm, bool useDwarfDirectory,
  1901. MCInstPrinter *IP,
  1902. std::unique_ptr<MCCodeEmitter> &&CE,
  1903. std::unique_ptr<MCAsmBackend> &&MAB,
  1904. bool ShowInst) {
  1905. return new MCAsmStreamer(Context, std::move(OS), isVerboseAsm,
  1906. useDwarfDirectory, IP, std::move(CE), std::move(MAB),
  1907. ShowInst);
  1908. }