Coverage Report

Created: 2025-07-15 06:22

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