Line data Source code
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 :
14 : #ifndef LLVM_MC_MCSTREAMER_H
15 : #define LLVM_MC_MCSTREAMER_H
16 :
17 : #include "llvm/ADT/ArrayRef.h"
18 : #include "llvm/ADT/DenseMap.h"
19 : #include "llvm/ADT/Optional.h"
20 : #include "llvm/ADT/SmallVector.h"
21 : #include "llvm/ADT/StringRef.h"
22 : #include "llvm/MC/MCDirectives.h"
23 : #include "llvm/MC/MCDwarf.h"
24 : #include "llvm/MC/MCLinkerOptimizationHint.h"
25 : #include "llvm/MC/MCSymbol.h"
26 : #include "llvm/MC/MCWinEH.h"
27 : #include "llvm/Support/Error.h"
28 : #include "llvm/Support/MD5.h"
29 : #include "llvm/Support/SMLoc.h"
30 : #include "llvm/Support/TargetParser.h"
31 : #include <cassert>
32 : #include <cstdint>
33 : #include <memory>
34 : #include <string>
35 : #include <utility>
36 : #include <vector>
37 :
38 : namespace llvm {
39 :
40 : class AssemblerConstantPools;
41 : class formatted_raw_ostream;
42 : class MCAsmBackend;
43 : class MCCodeEmitter;
44 : struct MCCodePaddingContext;
45 : class MCContext;
46 : class MCExpr;
47 : class MCInst;
48 : class MCInstPrinter;
49 : class MCSection;
50 : class MCStreamer;
51 : class MCSymbolRefExpr;
52 : class MCSubtargetInfo;
53 : class raw_ostream;
54 : class Twine;
55 :
56 : using MCSectionSubPair = std::pair<MCSection *, const MCExpr *>;
57 :
58 : /// Target specific streamer interface. This is used so that targets can
59 : /// implement support for target specific assembly directives.
60 : ///
61 : /// If target foo wants to use this, it should implement 3 classes:
62 : /// * FooTargetStreamer : public MCTargetStreamer
63 : /// * FooTargetAsmStreamer : public FooTargetStreamer
64 : /// * FooTargetELFStreamer : public FooTargetStreamer
65 : ///
66 : /// FooTargetStreamer should have a pure virtual method for each directive. For
67 : /// example, for a ".bar symbol_name" directive, it should have
68 : /// virtual emitBar(const MCSymbol &Symbol) = 0;
69 : ///
70 : /// The FooTargetAsmStreamer and FooTargetELFStreamer classes implement the
71 : /// method. The assembly streamer just prints ".bar symbol_name". The object
72 : /// streamer does whatever is needed to implement .bar in the object file.
73 : ///
74 : /// In the assembly printer and parser the target streamer can be used by
75 : /// calling getTargetStreamer and casting it to FooTargetStreamer:
76 : ///
77 : /// MCTargetStreamer &TS = OutStreamer.getTargetStreamer();
78 : /// FooTargetStreamer &ATS = static_cast<FooTargetStreamer &>(TS);
79 : ///
80 : /// The base classes FooTargetAsmStreamer and FooTargetELFStreamer should
81 : /// *never* be treated differently. Callers should always talk to a
82 : /// FooTargetStreamer.
83 24610 : class MCTargetStreamer {
84 : protected:
85 : MCStreamer &Streamer;
86 :
87 : public:
88 : MCTargetStreamer(MCStreamer &S);
89 : virtual ~MCTargetStreamer();
90 :
91 0 : MCStreamer &getStreamer() { return Streamer; }
92 :
93 : // Allow a target to add behavior to the EmitLabel of MCStreamer.
94 : virtual void emitLabel(MCSymbol *Symbol);
95 : // Allow a target to add behavior to the emitAssignment of MCStreamer.
96 : virtual void emitAssignment(MCSymbol *Symbol, const MCExpr *Value);
97 :
98 : virtual void prettyPrintAsm(MCInstPrinter &InstPrinter, raw_ostream &OS,
99 : const MCInst &Inst, const MCSubtargetInfo &STI);
100 :
101 : virtual void emitDwarfFileDirective(StringRef Directive);
102 :
103 : /// Update streamer for a new active section.
104 : ///
105 : /// This is called by PopSection and SwitchSection, if the current
106 : /// section changes.
107 : virtual void changeSection(const MCSection *CurSection, MCSection *Section,
108 : const MCExpr *SubSection, raw_ostream &OS);
109 :
110 : virtual void emitValue(const MCExpr *Value);
111 :
112 : virtual void finish();
113 : };
114 :
115 : // FIXME: declared here because it is used from
116 : // lib/CodeGen/AsmPrinter/ARMException.cpp.
117 8163 : class ARMTargetStreamer : public MCTargetStreamer {
118 : public:
119 : ARMTargetStreamer(MCStreamer &S);
120 : ~ARMTargetStreamer() override;
121 :
122 : virtual void emitFnStart();
123 : virtual void emitFnEnd();
124 : virtual void emitCantUnwind();
125 : virtual void emitPersonality(const MCSymbol *Personality);
126 : virtual void emitPersonalityIndex(unsigned Index);
127 : virtual void emitHandlerData();
128 : virtual void emitSetFP(unsigned FpReg, unsigned SpReg,
129 : int64_t Offset = 0);
130 : virtual void emitMovSP(unsigned Reg, int64_t Offset = 0);
131 : virtual void emitPad(int64_t Offset);
132 : virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
133 : bool isVector);
134 : virtual void emitUnwindRaw(int64_t StackOffset,
135 : const SmallVectorImpl<uint8_t> &Opcodes);
136 :
137 : virtual void switchVendor(StringRef Vendor);
138 : virtual void emitAttribute(unsigned Attribute, unsigned Value);
139 : virtual void emitTextAttribute(unsigned Attribute, StringRef String);
140 : virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
141 : StringRef StringValue = "");
142 : virtual void emitFPU(unsigned FPU);
143 : virtual void emitArch(ARM::ArchKind Arch);
144 : virtual void emitArchExtension(unsigned ArchExt);
145 : virtual void emitObjectArch(ARM::ArchKind Arch);
146 : void emitTargetAttributes(const MCSubtargetInfo &STI);
147 : virtual void finishAttributeSection();
148 : virtual void emitInst(uint32_t Inst, char Suffix = '\0');
149 :
150 : virtual void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE);
151 :
152 : virtual void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value);
153 :
154 : void finish() override;
155 :
156 : /// Reset any state between object emissions, i.e. the equivalent of
157 : /// MCStreamer's reset method.
158 : virtual void reset();
159 :
160 : /// Callback used to implement the ldr= pseudo.
161 : /// Add a new entry to the constant pool for the current section and return an
162 : /// MCExpr that can be used to refer to the constant pool location.
163 : const MCExpr *addConstantPoolEntry(const MCExpr *, SMLoc Loc);
164 :
165 : /// Callback used to implemnt the .ltorg directive.
166 : /// Emit contents of constant pool for the current section.
167 : void emitCurrentConstantPool();
168 :
169 : private:
170 : std::unique_ptr<AssemblerConstantPools> ConstantPools;
171 : };
172 :
173 : /// Streaming machine code generation interface.
174 : ///
175 : /// This interface is intended to provide a programatic interface that is very
176 : /// similar to the level that an assembler .s file provides. It has callbacks
177 : /// to emit bytes, handle directives, etc. The implementation of this interface
178 : /// retains state to know what the current section is etc.
179 : ///
180 : /// There are multiple implementations of this interface: one for writing out
181 : /// a .s file, and implementations that write out .o files of various formats.
182 : ///
183 : class MCStreamer {
184 : MCContext &Context;
185 : std::unique_ptr<MCTargetStreamer> TargetStreamer;
186 :
187 : std::vector<MCDwarfFrameInfo> DwarfFrameInfos;
188 : MCDwarfFrameInfo *getCurrentDwarfFrameInfo();
189 :
190 : /// Similar to DwarfFrameInfos, but for SEH unwind info. Chained frames may
191 : /// refer to each other, so use std::unique_ptr to provide pointer stability.
192 : std::vector<std::unique_ptr<WinEH::FrameInfo>> WinFrameInfos;
193 :
194 : WinEH::FrameInfo *CurrentWinFrameInfo;
195 :
196 : /// Retreive the current frame info if one is available and it is not yet
197 : /// closed. Otherwise, issue an error and return null.
198 : WinEH::FrameInfo *EnsureValidWinFrameInfo(SMLoc Loc);
199 :
200 : /// Tracks an index to represent the order a symbol was emitted in.
201 : /// Zero means we did not emit that symbol.
202 : DenseMap<const MCSymbol *, unsigned> SymbolOrdering;
203 :
204 : /// This is stack of current and previous section values saved by
205 : /// PushSection.
206 : SmallVector<std::pair<MCSectionSubPair, MCSectionSubPair>, 4> SectionStack;
207 :
208 : /// The next unique ID to use when creating a WinCFI-related section (.pdata
209 : /// or .xdata). This ID ensures that we have a one-to-one mapping from
210 : /// code section to unwind info section, which MSVC's incremental linker
211 : /// requires.
212 : unsigned NextWinCFIID = 0;
213 :
214 : bool UseAssemblerInfoForParsing;
215 :
216 : protected:
217 : MCStreamer(MCContext &Ctx);
218 :
219 : virtual void EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame);
220 : virtual void EmitCFIEndProcImpl(MCDwarfFrameInfo &CurFrame);
221 :
222 : /// When emitting an object file, create and emit a real label. When emitting
223 : /// textual assembly, this should do nothing to avoid polluting our output.
224 : virtual MCSymbol *EmitCFILabel();
225 :
226 0 : WinEH::FrameInfo *getCurrentWinFrameInfo() {
227 0 : return CurrentWinFrameInfo;
228 : }
229 :
230 : virtual void EmitWindowsUnwindTables();
231 :
232 : virtual void EmitRawTextImpl(StringRef String);
233 :
234 : /// Returns true if the the .cv_loc directive is in the right section.
235 : bool checkCVLocSection(unsigned FuncId, unsigned FileNo, SMLoc Loc);
236 :
237 : public:
238 : MCStreamer(const MCStreamer &) = delete;
239 : MCStreamer &operator=(const MCStreamer &) = delete;
240 : virtual ~MCStreamer();
241 :
242 : void visitUsedExpr(const MCExpr &Expr);
243 : virtual void visitUsedSymbol(const MCSymbol &Sym);
244 :
245 : void setTargetStreamer(MCTargetStreamer *TS) {
246 : TargetStreamer.reset(TS);
247 : }
248 :
249 : /// State management
250 : ///
251 : virtual void reset();
252 :
253 0 : MCContext &getContext() const { return Context; }
254 :
255 862 : virtual MCAssembler *getAssemblerPtr() { return nullptr; }
256 :
257 18401 : void setUseAssemblerInfoForParsing(bool v) { UseAssemblerInfoForParsing = v; }
258 0 : bool getUseAssemblerInfoForParsing() { return UseAssemblerInfoForParsing; }
259 :
260 : MCTargetStreamer *getTargetStreamer() {
261 : return TargetStreamer.get();
262 : }
263 :
264 30946 : unsigned getNumFrameInfos() { return DwarfFrameInfos.size(); }
265 : ArrayRef<MCDwarfFrameInfo> getDwarfFrameInfos() const {
266 : return DwarfFrameInfos;
267 : }
268 :
269 : bool hasUnfinishedDwarfFrameInfo();
270 :
271 810 : unsigned getNumWinFrameInfos() { return WinFrameInfos.size(); }
272 : ArrayRef<std::unique_ptr<WinEH::FrameInfo>> getWinFrameInfos() const {
273 : return WinFrameInfos;
274 : }
275 :
276 : void generateCompactUnwindEncodings(MCAsmBackend *MAB);
277 :
278 : /// \name Assembly File Formatting.
279 : /// @{
280 :
281 : /// Return true if this streamer supports verbose assembly and if it is
282 : /// enabled.
283 244183 : virtual bool isVerboseAsm() const { return false; }
284 :
285 : /// Return true if this asm streamer supports emitting unformatted text
286 : /// to the .s file with EmitRawText.
287 899117 : virtual bool hasRawTextSupport() const { return false; }
288 :
289 : /// Is the integrated assembler required for this streamer to function
290 : /// correctly?
291 1909 : virtual bool isIntegratedAssemblerRequired() const { return false; }
292 :
293 : /// Add a textual comment.
294 : ///
295 : /// Typically for comments that can be emitted to the generated .s
296 : /// file if applicable as a QoI issue to make the output of the compiler
297 : /// more readable. This only affects the MCAsmStreamer, and only when
298 : /// verbose assembly output is enabled.
299 : ///
300 : /// If the comment includes embedded \n's, they will each get the comment
301 : /// prefix as appropriate. The added comment should not end with a \n.
302 : /// By default, each comment is terminated with an end of line, i.e. the
303 : /// EOL param is set to true by default. If one prefers not to end the
304 : /// comment with a new line then the EOL param should be passed
305 : /// with a false value.
306 515364 : virtual void AddComment(const Twine &T, bool EOL = true) {}
307 :
308 : /// Return a raw_ostream that comments can be written to. Unlike
309 : /// AddComment, you are required to terminate comments with \n if you use this
310 : /// method.
311 : virtual raw_ostream &GetCommentOS();
312 :
313 : /// Print T and prefix it with the comment string (normally #) and
314 : /// optionally a tab. This prints the comment immediately, not at the end of
315 : /// the current line. It is basically a safe version of EmitRawText: since it
316 : /// only prints comments, the object streamer ignores it instead of asserting.
317 : virtual void emitRawComment(const Twine &T, bool TabPrefix = true);
318 :
319 : /// Add explicit comment T. T is required to be a valid
320 : /// comment in the output and does not need to be escaped.
321 : virtual void addExplicitComment(const Twine &T);
322 :
323 : /// Emit added explicit comments.
324 : virtual void emitExplicitComments();
325 :
326 : /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
327 841653 : virtual void AddBlankLine() {}
328 :
329 : /// @}
330 :
331 : /// \name Symbol & Section Management
332 : /// @{
333 :
334 : /// Return the current section that the streamer is emitting code to.
335 : MCSectionSubPair getCurrentSection() const {
336 196508182 : if (!SectionStack.empty())
337 196508185 : return SectionStack.back().first;
338 : return MCSectionSubPair();
339 : }
340 : MCSection *getCurrentSectionOnly() const { return getCurrentSection().first; }
341 :
342 : /// Return the previous section that the streamer is emitting code to.
343 : MCSectionSubPair getPreviousSection() const {
344 9546 : if (!SectionStack.empty())
345 9577 : return SectionStack.back().second;
346 : return MCSectionSubPair();
347 : }
348 :
349 : /// Returns an index to represent the order a symbol was emitted in.
350 : /// (zero if we did not emit that symbol)
351 : unsigned GetSymbolOrder(const MCSymbol *Sym) const {
352 : return SymbolOrdering.lookup(Sym);
353 : }
354 :
355 : /// Update streamer for a new active section.
356 : ///
357 : /// This is called by PopSection and SwitchSection, if the current
358 : /// section changes.
359 : virtual void ChangeSection(MCSection *, const MCExpr *);
360 :
361 : /// Save the current and previous section on the section stack.
362 7856 : void PushSection() {
363 15712 : SectionStack.push_back(
364 7856 : std::make_pair(getCurrentSection(), getPreviousSection()));
365 7856 : }
366 :
367 : /// Restore the current and previous section from the section stack.
368 : /// Calls ChangeSection as needed.
369 : ///
370 : /// Returns false if the stack was empty.
371 7856 : bool PopSection() {
372 15712 : if (SectionStack.size() <= 1)
373 : return false;
374 : auto I = SectionStack.end();
375 : --I;
376 7856 : MCSectionSubPair OldSection = I->first;
377 : --I;
378 7856 : MCSectionSubPair NewSection = I->first;
379 :
380 : if (OldSection != NewSection)
381 7573 : ChangeSection(NewSection.first, NewSection.second);
382 : SectionStack.pop_back();
383 7856 : return true;
384 : }
385 :
386 : bool SubSection(const MCExpr *Subsection) {
387 0 : if (SectionStack.empty())
388 : return false;
389 :
390 0 : SwitchSection(SectionStack.back().first.first, Subsection);
391 : return true;
392 : }
393 :
394 : /// Set the current section where code is being emitted to \p Section. This
395 : /// is required to update CurSection.
396 : ///
397 : /// This corresponds to assembler directives like .section, .text, etc.
398 : virtual void SwitchSection(MCSection *Section,
399 : const MCExpr *Subsection = nullptr);
400 :
401 : /// Set the current section where code is being emitted to \p Section.
402 : /// This is required to update CurSection. This version does not call
403 : /// ChangeSection.
404 : void SwitchSectionNoChange(MCSection *Section,
405 : const MCExpr *Subsection = nullptr) {
406 : assert(Section && "Cannot switch to a null section!");
407 464 : MCSectionSubPair curSection = SectionStack.back().first;
408 : SectionStack.back().second = curSection;
409 : if (MCSectionSubPair(Section, Subsection) != curSection)
410 : SectionStack.back().first = MCSectionSubPair(Section, Subsection);
411 : }
412 :
413 : /// Create the default sections and set the initial one.
414 : virtual void InitSections(bool NoExecStack);
415 :
416 : MCSymbol *endSection(MCSection *Section);
417 :
418 : /// Sets the symbol's section.
419 : ///
420 : /// Each emitted symbol will be tracked in the ordering table,
421 : /// so we can sort on them later.
422 : void AssignFragment(MCSymbol *Symbol, MCFragment *Fragment);
423 :
424 : /// Emit a label for \p Symbol into the current section.
425 : ///
426 : /// This corresponds to an assembler statement such as:
427 : /// foo:
428 : ///
429 : /// \param Symbol - The symbol to emit. A given symbol should only be
430 : /// emitted as a label once, and symbols emitted as a label should never be
431 : /// used in an assignment.
432 : // FIXME: These emission are non-const because we mutate the symbol to
433 : // add the section we're emitting it to later.
434 : virtual void EmitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc());
435 :
436 : virtual void EmitEHSymAttributes(const MCSymbol *Symbol, MCSymbol *EHSymbol);
437 :
438 : /// Note in the output the specified \p Flag.
439 : virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
440 :
441 : /// Emit the given list \p Options of strings as linker
442 : /// options into the output.
443 0 : virtual void EmitLinkerOptions(ArrayRef<std::string> Kind) {}
444 :
445 : /// Note in the output the specified region \p Kind.
446 136 : virtual void EmitDataRegion(MCDataRegionType Kind) {}
447 :
448 : /// Specify the Mach-O minimum deployment target version.
449 0 : virtual void EmitVersionMin(MCVersionMinType Type, unsigned Major,
450 0 : unsigned Minor, unsigned Update) {}
451 :
452 : /// Emit/Specify Mach-O build version command.
453 : /// \p Platform should be one of MachO::PlatformType.
454 0 : virtual void EmitBuildVersion(unsigned Platform, unsigned Major,
455 0 : unsigned Minor, unsigned Update) {}
456 :
457 : void EmitVersionForTarget(const Triple &Target);
458 :
459 : /// Note in the output that the specified \p Func is a Thumb mode
460 : /// function (ARM target only).
461 : virtual void EmitThumbFunc(MCSymbol *Func);
462 :
463 : /// Emit an assignment of \p Value to \p Symbol.
464 : ///
465 : /// This corresponds to an assembler statement such as:
466 : /// symbol = value
467 : ///
468 : /// The assignment generates no code, but has the side effect of binding the
469 : /// value in the current context. For the assembly streamer, this prints the
470 : /// binding into the .s file.
471 : ///
472 : /// \param Symbol - The symbol being assigned to.
473 : /// \param Value - The value for the symbol.
474 : virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
475 :
476 : /// Emit an weak reference from \p Alias to \p Symbol.
477 : ///
478 : /// This corresponds to an assembler statement such as:
479 : /// .weakref alias, symbol
480 : ///
481 : /// \param Alias - The alias that is being created.
482 : /// \param Symbol - The symbol being aliased.
483 : virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
484 :
485 : /// Add the given \p Attribute to \p Symbol.
486 : virtual bool EmitSymbolAttribute(MCSymbol *Symbol,
487 : MCSymbolAttr Attribute) = 0;
488 :
489 : /// Set the \p DescValue for the \p Symbol.
490 : ///
491 : /// \param Symbol - The symbol to have its n_desc field set.
492 : /// \param DescValue - The value to set into the n_desc field.
493 : virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
494 :
495 : /// Start emitting COFF symbol definition
496 : ///
497 : /// \param Symbol - The symbol to have its External & Type fields set.
498 : virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol);
499 :
500 : /// Emit the storage class of the symbol.
501 : ///
502 : /// \param StorageClass - The storage class the symbol should have.
503 : virtual void EmitCOFFSymbolStorageClass(int StorageClass);
504 :
505 : /// Emit the type of the symbol.
506 : ///
507 : /// \param Type - A COFF type identifier (see COFF::SymbolType in X86COFF.h)
508 : virtual void EmitCOFFSymbolType(int Type);
509 :
510 : /// Marks the end of the symbol definition.
511 : virtual void EndCOFFSymbolDef();
512 :
513 : virtual void EmitCOFFSafeSEH(MCSymbol const *Symbol);
514 :
515 : /// Emits the symbol table index of a Symbol into the current section.
516 : virtual void EmitCOFFSymbolIndex(MCSymbol const *Symbol);
517 :
518 : /// Emits a COFF section index.
519 : ///
520 : /// \param Symbol - Symbol the section number relocation should point to.
521 : virtual void EmitCOFFSectionIndex(MCSymbol const *Symbol);
522 :
523 : /// Emits a COFF section relative relocation.
524 : ///
525 : /// \param Symbol - Symbol the section relative relocation should point to.
526 : virtual void EmitCOFFSecRel32(MCSymbol const *Symbol, uint64_t Offset);
527 :
528 : /// Emits a COFF image relative relocation.
529 : ///
530 : /// \param Symbol - Symbol the image relative relocation should point to.
531 : virtual void EmitCOFFImgRel32(MCSymbol const *Symbol, int64_t Offset);
532 :
533 : /// Emit an ELF .size directive.
534 : ///
535 : /// This corresponds to an assembler statement such as:
536 : /// .size symbol, expression
537 : virtual void emitELFSize(MCSymbol *Symbol, const MCExpr *Value);
538 :
539 : /// Emit an ELF .symver directive.
540 : ///
541 : /// This corresponds to an assembler statement such as:
542 : /// .symver _start, foo@@SOME_VERSION
543 : /// \param AliasName - The versioned alias (i.e. "foo@@SOME_VERSION")
544 : /// \param Aliasee - The aliased symbol (i.e. "_start")
545 : virtual void emitELFSymverDirective(StringRef AliasName,
546 : const MCSymbol *Aliasee);
547 :
548 : /// Emit a Linker Optimization Hint (LOH) directive.
549 : /// \param Args - Arguments of the LOH.
550 0 : virtual void EmitLOHDirective(MCLOHType Kind, const MCLOHArgs &Args) {}
551 :
552 : /// Emit a common symbol.
553 : ///
554 : /// \param Symbol - The common symbol to emit.
555 : /// \param Size - The size of the common symbol.
556 : /// \param ByteAlignment - The alignment of the symbol if
557 : /// non-zero. This must be a power of 2.
558 : virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
559 : unsigned ByteAlignment) = 0;
560 :
561 : /// Emit a local common (.lcomm) symbol.
562 : ///
563 : /// \param Symbol - The common symbol to emit.
564 : /// \param Size - The size of the common symbol.
565 : /// \param ByteAlignment - The alignment of the common symbol in bytes.
566 : virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
567 : unsigned ByteAlignment);
568 :
569 : /// Emit the zerofill section and an optional symbol.
570 : ///
571 : /// \param Section - The zerofill section to create and or to put the symbol
572 : /// \param Symbol - The zerofill symbol to emit, if non-NULL.
573 : /// \param Size - The size of the zerofill symbol.
574 : /// \param ByteAlignment - The alignment of the zerofill symbol if
575 : /// non-zero. This must be a power of 2 on some targets.
576 : virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr,
577 : uint64_t Size = 0, unsigned ByteAlignment = 0,
578 : SMLoc Loc = SMLoc()) = 0;
579 :
580 : /// Emit a thread local bss (.tbss) symbol.
581 : ///
582 : /// \param Section - The thread local common section.
583 : /// \param Symbol - The thread local common symbol to emit.
584 : /// \param Size - The size of the symbol.
585 : /// \param ByteAlignment - The alignment of the thread local common symbol
586 : /// if non-zero. This must be a power of 2 on some targets.
587 : virtual void EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
588 : uint64_t Size, unsigned ByteAlignment = 0);
589 :
590 : /// @}
591 : /// \name Generating Data
592 : /// @{
593 :
594 : /// Emit the bytes in \p Data into the output.
595 : ///
596 : /// This is used to implement assembler directives such as .byte, .ascii,
597 : /// etc.
598 : virtual void EmitBytes(StringRef Data);
599 :
600 : /// Functionally identical to EmitBytes. When emitting textual assembly, this
601 : /// method uses .byte directives instead of .ascii or .asciz for readability.
602 : virtual void EmitBinaryData(StringRef Data);
603 :
604 : /// Emit the expression \p Value into the output as a native
605 : /// integer of the given \p Size bytes.
606 : ///
607 : /// This is used to implement assembler directives such as .word, .quad,
608 : /// etc.
609 : ///
610 : /// \param Value - The value to emit.
611 : /// \param Size - The size of the integer (in bytes) to emit. This must
612 : /// match a native machine width.
613 : /// \param Loc - The location of the expression for error reporting.
614 : virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
615 : SMLoc Loc = SMLoc());
616 :
617 : void EmitValue(const MCExpr *Value, unsigned Size, SMLoc Loc = SMLoc());
618 :
619 : /// Special case of EmitValue that avoids the client having
620 : /// to pass in a MCExpr for constant integers.
621 : virtual void EmitIntValue(uint64_t Value, unsigned Size);
622 :
623 : virtual void EmitULEB128Value(const MCExpr *Value);
624 :
625 : virtual void EmitSLEB128Value(const MCExpr *Value);
626 :
627 : /// Special case of EmitULEB128Value that avoids the client having to
628 : /// pass in a MCExpr for constant integers.
629 : void EmitULEB128IntValue(uint64_t Value);
630 :
631 : /// Special case of EmitSLEB128Value that avoids the client having to
632 : /// pass in a MCExpr for constant integers.
633 : void EmitSLEB128IntValue(int64_t Value);
634 :
635 : /// Special case of EmitValue that avoids the client having to pass in
636 : /// a MCExpr for MCSymbols.
637 : void EmitSymbolValue(const MCSymbol *Sym, unsigned Size,
638 : bool IsSectionRelative = false);
639 :
640 : /// Emit the expression \p Value into the output as a dtprel
641 : /// (64-bit DTP relative) value.
642 : ///
643 : /// This is used to implement assembler directives such as .dtpreldword on
644 : /// targets that support them.
645 : virtual void EmitDTPRel64Value(const MCExpr *Value);
646 :
647 : /// Emit the expression \p Value into the output as a dtprel
648 : /// (32-bit DTP relative) value.
649 : ///
650 : /// This is used to implement assembler directives such as .dtprelword on
651 : /// targets that support them.
652 : virtual void EmitDTPRel32Value(const MCExpr *Value);
653 :
654 : /// Emit the expression \p Value into the output as a tprel
655 : /// (64-bit TP relative) value.
656 : ///
657 : /// This is used to implement assembler directives such as .tpreldword on
658 : /// targets that support them.
659 : virtual void EmitTPRel64Value(const MCExpr *Value);
660 :
661 : /// Emit the expression \p Value into the output as a tprel
662 : /// (32-bit TP relative) value.
663 : ///
664 : /// This is used to implement assembler directives such as .tprelword on
665 : /// targets that support them.
666 : virtual void EmitTPRel32Value(const MCExpr *Value);
667 :
668 : /// Emit the expression \p Value into the output as a gprel64 (64-bit
669 : /// GP relative) value.
670 : ///
671 : /// This is used to implement assembler directives such as .gpdword on
672 : /// targets that support them.
673 : virtual void EmitGPRel64Value(const MCExpr *Value);
674 :
675 : /// Emit the expression \p Value into the output as a gprel32 (32-bit
676 : /// GP relative) value.
677 : ///
678 : /// This is used to implement assembler directives such as .gprel32 on
679 : /// targets that support them.
680 : virtual void EmitGPRel32Value(const MCExpr *Value);
681 :
682 : /// Emit NumBytes bytes worth of the value specified by FillValue.
683 : /// This implements directives such as '.space'.
684 : void emitFill(uint64_t NumBytes, uint8_t FillValue);
685 :
686 : /// Emit \p Size bytes worth of the value specified by \p FillValue.
687 : ///
688 : /// This is used to implement assembler directives such as .space or .skip.
689 : ///
690 : /// \param NumBytes - The number of bytes to emit.
691 : /// \param FillValue - The value to use when filling bytes.
692 : /// \param Loc - The location of the expression for error reporting.
693 : virtual void emitFill(const MCExpr &NumBytes, uint64_t FillValue,
694 : SMLoc Loc = SMLoc());
695 :
696 : /// Emit \p NumValues copies of \p Size bytes. Each \p Size bytes is
697 : /// taken from the lowest order 4 bytes of \p Expr expression.
698 : ///
699 : /// This is used to implement assembler directives such as .fill.
700 : ///
701 : /// \param NumValues - The number of copies of \p Size bytes to emit.
702 : /// \param Size - The size (in bytes) of each repeated value.
703 : /// \param Expr - The expression from which \p Size bytes are used.
704 : virtual void emitFill(const MCExpr &NumValues, int64_t Size, int64_t Expr,
705 : SMLoc Loc = SMLoc());
706 :
707 : /// Emit NumBytes worth of zeros.
708 : /// This function properly handles data in virtual sections.
709 : void EmitZeros(uint64_t NumBytes);
710 :
711 : /// Emit some number of copies of \p Value until the byte alignment \p
712 : /// ByteAlignment is reached.
713 : ///
714 : /// If the number of bytes need to emit for the alignment is not a multiple
715 : /// of \p ValueSize, then the contents of the emitted fill bytes is
716 : /// undefined.
717 : ///
718 : /// This used to implement the .align assembler directive.
719 : ///
720 : /// \param ByteAlignment - The alignment to reach. This must be a power of
721 : /// two on some targets.
722 : /// \param Value - The value to use when filling bytes.
723 : /// \param ValueSize - The size of the integer (in bytes) to emit for
724 : /// \p Value. This must match a native machine width.
725 : /// \param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
726 : /// the alignment cannot be reached in this many bytes, no bytes are
727 : /// emitted.
728 : virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
729 : unsigned ValueSize = 1,
730 : unsigned MaxBytesToEmit = 0);
731 :
732 : /// Emit nops until the byte alignment \p ByteAlignment is reached.
733 : ///
734 : /// This used to align code where the alignment bytes may be executed. This
735 : /// can emit different bytes for different sizes to optimize execution.
736 : ///
737 : /// \param ByteAlignment - The alignment to reach. This must be a power of
738 : /// two on some targets.
739 : /// \param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
740 : /// the alignment cannot be reached in this many bytes, no bytes are
741 : /// emitted.
742 : virtual void EmitCodeAlignment(unsigned ByteAlignment,
743 : unsigned MaxBytesToEmit = 0);
744 :
745 : /// Emit some number of copies of \p Value until the byte offset \p
746 : /// Offset is reached.
747 : ///
748 : /// This is used to implement assembler directives such as .org.
749 : ///
750 : /// \param Offset - The offset to reach. This may be an expression, but the
751 : /// expression must be associated with the current section.
752 : /// \param Value - The value to use when filling bytes.
753 : virtual void emitValueToOffset(const MCExpr *Offset, unsigned char Value,
754 : SMLoc Loc);
755 :
756 : virtual void
757 233725 : EmitCodePaddingBasicBlockStart(const MCCodePaddingContext &Context) {}
758 :
759 : virtual void
760 233725 : EmitCodePaddingBasicBlockEnd(const MCCodePaddingContext &Context) {}
761 :
762 : /// @}
763 :
764 : /// Switch to a new logical file. This is used to implement the '.file
765 : /// "foo.c"' assembler directive.
766 : virtual void EmitFileDirective(StringRef Filename);
767 :
768 : /// Emit the "identifiers" directive. This implements the
769 : /// '.ident "version foo"' assembler directive.
770 3 : virtual void EmitIdent(StringRef IdentString) {}
771 :
772 : /// Associate a filename with a specified logical file number. This
773 : /// implements the DWARF2 '.file 4 "foo.c"' assembler directive.
774 : unsigned EmitDwarfFileDirective(unsigned FileNo, StringRef Directory,
775 : StringRef Filename,
776 : MD5::MD5Result *Checksum = nullptr,
777 : Optional<StringRef> Source = None,
778 : unsigned CUID = 0) {
779 : return cantFail(
780 1784803 : tryEmitDwarfFileDirective(FileNo, Directory, Filename, Checksum,
781 892401 : Source, CUID));
782 : }
783 :
784 : /// Associate a filename with a specified logical file number.
785 : /// Also associate a directory, optional checksum, and optional source
786 : /// text with the logical file. This implements the DWARF2
787 : /// '.file 4 "dir/foo.c"' assembler directive, and the DWARF5
788 : /// '.file 4 "dir/foo.c" md5 "..." source "..."' assembler directive.
789 : virtual Expected<unsigned> tryEmitDwarfFileDirective(
790 : unsigned FileNo, StringRef Directory, StringRef Filename,
791 : MD5::MD5Result *Checksum = nullptr, Optional<StringRef> Source = None,
792 : unsigned CUID = 0);
793 :
794 : /// Specify the "root" file of the compilation, using the ".file 0" extension.
795 : virtual void emitDwarfFile0Directive(StringRef Directory, StringRef Filename,
796 : MD5::MD5Result *Checksum,
797 : Optional<StringRef> Source,
798 : unsigned CUID = 0);
799 :
800 : /// This implements the DWARF2 '.loc fileno lineno ...' assembler
801 : /// directive.
802 : virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
803 : unsigned Column, unsigned Flags,
804 : unsigned Isa, unsigned Discriminator,
805 : StringRef FileName);
806 :
807 : /// Associate a filename with a specified logical file number, and also
808 : /// specify that file's checksum information. This implements the '.cv_file 4
809 : /// "foo.c"' assembler directive. Returns true on success.
810 : virtual bool EmitCVFileDirective(unsigned FileNo, StringRef Filename,
811 : ArrayRef<uint8_t> Checksum,
812 : unsigned ChecksumKind);
813 :
814 : /// Introduces a function id for use with .cv_loc.
815 : virtual bool EmitCVFuncIdDirective(unsigned FunctionId);
816 :
817 : /// Introduces an inline call site id for use with .cv_loc. Includes
818 : /// extra information for inline line table generation.
819 : virtual bool EmitCVInlineSiteIdDirective(unsigned FunctionId, unsigned IAFunc,
820 : unsigned IAFile, unsigned IALine,
821 : unsigned IACol, SMLoc Loc);
822 :
823 : /// This implements the CodeView '.cv_loc' assembler directive.
824 : virtual void EmitCVLocDirective(unsigned FunctionId, unsigned FileNo,
825 : unsigned Line, unsigned Column,
826 : bool PrologueEnd, bool IsStmt,
827 : StringRef FileName, SMLoc Loc);
828 :
829 : /// This implements the CodeView '.cv_linetable' assembler directive.
830 : virtual void EmitCVLinetableDirective(unsigned FunctionId,
831 : const MCSymbol *FnStart,
832 : const MCSymbol *FnEnd);
833 :
834 : /// This implements the CodeView '.cv_inline_linetable' assembler
835 : /// directive.
836 : virtual void EmitCVInlineLinetableDirective(unsigned PrimaryFunctionId,
837 : unsigned SourceFileId,
838 : unsigned SourceLineNum,
839 : const MCSymbol *FnStartSym,
840 : const MCSymbol *FnEndSym);
841 :
842 : /// This implements the CodeView '.cv_def_range' assembler
843 : /// directive.
844 : virtual void EmitCVDefRangeDirective(
845 : ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
846 : StringRef FixedSizePortion);
847 :
848 : /// This implements the CodeView '.cv_stringtable' assembler directive.
849 0 : virtual void EmitCVStringTableDirective() {}
850 :
851 : /// This implements the CodeView '.cv_filechecksums' assembler directive.
852 0 : virtual void EmitCVFileChecksumsDirective() {}
853 :
854 : /// This implements the CodeView '.cv_filechecksumoffset' assembler
855 : /// directive.
856 0 : virtual void EmitCVFileChecksumOffsetDirective(unsigned FileNo) {}
857 :
858 : /// This implements the CodeView '.cv_fpo_data' assembler directive.
859 0 : virtual void EmitCVFPOData(const MCSymbol *ProcSym, SMLoc Loc = {}) {}
860 :
861 : /// Emit the absolute difference between two symbols.
862 : ///
863 : /// \pre Offset of \c Hi is greater than the offset \c Lo.
864 : virtual void emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo,
865 : unsigned Size);
866 :
867 : /// Emit the absolute difference between two symbols encoded with ULEB128.
868 : virtual void emitAbsoluteSymbolDiffAsULEB128(const MCSymbol *Hi,
869 : const MCSymbol *Lo);
870 :
871 : virtual MCSymbol *getDwarfLineTableSymbol(unsigned CUID);
872 : virtual void EmitCFISections(bool EH, bool Debug);
873 : void EmitCFIStartProc(bool IsSimple);
874 : void EmitCFIEndProc();
875 : virtual void EmitCFIDefCfa(int64_t Register, int64_t Offset);
876 : virtual void EmitCFIDefCfaOffset(int64_t Offset);
877 : virtual void EmitCFIDefCfaRegister(int64_t Register);
878 : virtual void EmitCFIOffset(int64_t Register, int64_t Offset);
879 : virtual void EmitCFIPersonality(const MCSymbol *Sym, unsigned Encoding);
880 : virtual void EmitCFILsda(const MCSymbol *Sym, unsigned Encoding);
881 : virtual void EmitCFIRememberState();
882 : virtual void EmitCFIRestoreState();
883 : virtual void EmitCFISameValue(int64_t Register);
884 : virtual void EmitCFIRestore(int64_t Register);
885 : virtual void EmitCFIRelOffset(int64_t Register, int64_t Offset);
886 : virtual void EmitCFIAdjustCfaOffset(int64_t Adjustment);
887 : virtual void EmitCFIEscape(StringRef Values);
888 : virtual void EmitCFIReturnColumn(int64_t Register);
889 : virtual void EmitCFIGnuArgsSize(int64_t Size);
890 : virtual void EmitCFISignalFrame();
891 : virtual void EmitCFIUndefined(int64_t Register);
892 : virtual void EmitCFIRegister(int64_t Register1, int64_t Register2);
893 : virtual void EmitCFIWindowSave();
894 :
895 : virtual void EmitWinCFIStartProc(const MCSymbol *Symbol, SMLoc Loc = SMLoc());
896 : virtual void EmitWinCFIEndProc(SMLoc Loc = SMLoc());
897 : virtual void EmitWinCFIStartChained(SMLoc Loc = SMLoc());
898 : virtual void EmitWinCFIEndChained(SMLoc Loc = SMLoc());
899 : virtual void EmitWinCFIPushReg(unsigned Register, SMLoc Loc = SMLoc());
900 : virtual void EmitWinCFISetFrame(unsigned Register, unsigned Offset,
901 : SMLoc Loc = SMLoc());
902 : virtual void EmitWinCFIAllocStack(unsigned Size, SMLoc Loc = SMLoc());
903 : virtual void EmitWinCFISaveReg(unsigned Register, unsigned Offset,
904 : SMLoc Loc = SMLoc());
905 : virtual void EmitWinCFISaveXMM(unsigned Register, unsigned Offset,
906 : SMLoc Loc = SMLoc());
907 : virtual void EmitWinCFIPushFrame(bool Code, SMLoc Loc = SMLoc());
908 : virtual void EmitWinCFIEndProlog(SMLoc Loc = SMLoc());
909 : virtual void EmitWinEHHandler(const MCSymbol *Sym, bool Unwind, bool Except,
910 : SMLoc Loc = SMLoc());
911 : virtual void EmitWinEHHandlerData(SMLoc Loc = SMLoc());
912 :
913 : virtual void emitCGProfileEntry(const MCSymbolRefExpr *From,
914 : const MCSymbolRefExpr *To, uint64_t Count);
915 :
916 : /// Get the .pdata section used for the given section. Typically the given
917 : /// section is either the main .text section or some other COMDAT .text
918 : /// section, but it may be any section containing code.
919 : MCSection *getAssociatedPDataSection(const MCSection *TextSec);
920 :
921 : /// Get the .xdata section used for the given section.
922 : MCSection *getAssociatedXDataSection(const MCSection *TextSec);
923 :
924 : virtual void EmitSyntaxDirective();
925 :
926 : /// Emit a .reloc directive.
927 : /// Returns true if the relocation could not be emitted because Name is not
928 : /// known.
929 0 : virtual bool EmitRelocDirective(const MCExpr &Offset, StringRef Name,
930 : const MCExpr *Expr, SMLoc Loc,
931 : const MCSubtargetInfo &STI) {
932 0 : return true;
933 : }
934 :
935 0 : virtual void EmitAddrsig() {}
936 0 : virtual void EmitAddrsigSym(const MCSymbol *Sym) {}
937 :
938 : /// Emit the given \p Instruction into the current section.
939 : /// PrintSchedInfo == true then schedul comment should be added to output
940 : virtual void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
941 : bool PrintSchedInfo = false);
942 :
943 : /// Set the bundle alignment mode from now on in the section.
944 : /// The argument is the power of 2 to which the alignment is set. The
945 : /// value 0 means turn the bundle alignment off.
946 : virtual void EmitBundleAlignMode(unsigned AlignPow2);
947 :
948 : /// The following instructions are a bundle-locked group.
949 : ///
950 : /// \param AlignToEnd - If true, the bundle-locked group will be aligned to
951 : /// the end of a bundle.
952 : virtual void EmitBundleLock(bool AlignToEnd);
953 :
954 : /// Ends a bundle-locked group.
955 : virtual void EmitBundleUnlock();
956 :
957 : /// If this file is backed by a assembly streamer, this dumps the
958 : /// specified string in the output .s file. This capability is indicated by
959 : /// the hasRawTextSupport() predicate. By default this aborts.
960 : void EmitRawText(const Twine &String);
961 :
962 : /// Streamer specific finalization.
963 : virtual void FinishImpl();
964 : /// Finish emission of machine code.
965 : void Finish();
966 :
967 6 : virtual bool mayHaveInstructions(MCSection &Sec) const { return true; }
968 : };
969 :
970 : /// Create a dummy machine code streamer, which does nothing. This is useful for
971 : /// timing the assembler front end.
972 : MCStreamer *createNullStreamer(MCContext &Ctx);
973 :
974 : /// Create a machine code streamer which will print out assembly for the native
975 : /// target, suitable for compiling with a native assembler.
976 : ///
977 : /// \param InstPrint - If given, the instruction printer to use. If not given
978 : /// the MCInst representation will be printed. This method takes ownership of
979 : /// InstPrint.
980 : ///
981 : /// \param CE - If given, a code emitter to use to show the instruction
982 : /// encoding inline with the assembly. This method takes ownership of \p CE.
983 : ///
984 : /// \param TAB - If given, a target asm backend to use to show the fixup
985 : /// information in conjunction with encoding information. This method takes
986 : /// ownership of \p TAB.
987 : ///
988 : /// \param ShowInst - Whether to show the MCInst representation inline with
989 : /// the assembly.
990 : MCStreamer *createAsmStreamer(MCContext &Ctx,
991 : std::unique_ptr<formatted_raw_ostream> OS,
992 : bool isVerboseAsm, bool useDwarfDirectory,
993 : MCInstPrinter *InstPrint, MCCodeEmitter *CE,
994 : MCAsmBackend *TAB, bool ShowInst);
995 :
996 : } // end namespace llvm
997 :
998 : #endif // LLVM_MC_MCSTREAMER_H
|