1321369Sdim//===- RecordStreamer.h - Record asm defined and used symbols ---*- C++ -*-===//
2274955Ssvnmir//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6274955Ssvnmir//
7274955Ssvnmir//===----------------------------------------------------------------------===//
8274955Ssvnmir
9280031Sdim#ifndef LLVM_LIB_OBJECT_RECORDSTREAMER_H
10280031Sdim#define LLVM_LIB_OBJECT_RECORDSTREAMER_H
11274955Ssvnmir
12321369Sdim#include "llvm/ADT/DenseMap.h"
13321369Sdim#include "llvm/ADT/StringMap.h"
14321369Sdim#include "llvm/MC/MCDirectives.h"
15274955Ssvnmir#include "llvm/MC/MCStreamer.h"
16321369Sdim#include "llvm/MC/MCSymbol.h"
17321369Sdim#include "llvm/Support/SMLoc.h"
18321369Sdim#include <vector>
19274955Ssvnmir
20274955Ssvnmirnamespace llvm {
21321369Sdim
22331366Sdimclass GlobalValue;
23331366Sdimclass Module;
24331366Sdim
25274955Ssvnmirclass RecordStreamer : public MCStreamer {
26274955Ssvnmirpublic:
27314564Sdim  enum State { NeverSeen, Global, Defined, DefinedGlobal, DefinedWeak, Used,
28314564Sdim               UndefinedWeak};
29274955Ssvnmir
30274955Ssvnmirprivate:
31331366Sdim  const Module &M;
32274955Ssvnmir  StringMap<State> Symbols;
33321369Sdim  // Map of aliases created by .symver directives, saved so we can update
34321369Sdim  // their symbol binding after parsing complete. This maps from each
35321369Sdim  // aliasee to its list of aliases.
36331366Sdim  DenseMap<const MCSymbol *, std::vector<StringRef>> SymverAliasMap;
37321369Sdim
38331366Sdim  /// Get the state recorded for the given symbol.
39331366Sdim  State getSymbolState(const MCSymbol *Sym);
40331366Sdim
41274955Ssvnmir  void markDefined(const MCSymbol &Symbol);
42309124Sdim  void markGlobal(const MCSymbol &Symbol, MCSymbolAttr Attribute);
43274955Ssvnmir  void markUsed(const MCSymbol &Symbol);
44274955Ssvnmir  void visitUsedSymbol(const MCSymbol &Sym) override;
45274955Ssvnmir
46274955Ssvnmirpublic:
47331366Sdim  RecordStreamer(MCContext &Context, const Module &M);
48321369Sdim
49353358Sdim  void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override;
50321369Sdim  void EmitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc()) override;
51274955Ssvnmir  void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) override;
52274955Ssvnmir  bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override;
53288943Sdim  void EmitZerofill(MCSection *Section, MCSymbol *Symbol, uint64_t Size,
54341825Sdim                    unsigned ByteAlignment, SMLoc Loc = SMLoc()) override;
55274955Ssvnmir  void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
56274955Ssvnmir                        unsigned ByteAlignment) override;
57353358Sdim
58353358Sdim  // Ignore COFF-specific directives; we do not need any information from them,
59353358Sdim  // but the default implementation of these methods crashes, so we override
60353358Sdim  // them with versions that do nothing.
61353358Sdim  void BeginCOFFSymbolDef(const MCSymbol *Symbol) override {}
62353358Sdim  void EmitCOFFSymbolStorageClass(int StorageClass) override {}
63353358Sdim  void EmitCOFFSymbolType(int Type) override {}
64353358Sdim  void EndCOFFSymbolDef() override {}
65353358Sdim
66321369Sdim  /// Record .symver aliases for later processing.
67331366Sdim  void emitELFSymverDirective(StringRef AliasName,
68321369Sdim                              const MCSymbol *Aliasee) override;
69341825Sdim
70331366Sdim  // Emit ELF .symver aliases and ensure they have the same binding as the
71331366Sdim  // defined symbol they alias with.
72331366Sdim  void flushSymverDirectives();
73341825Sdim
74341825Sdim  // Symbols iterators
75341825Sdim  using const_iterator = StringMap<State>::const_iterator;
76341825Sdim  const_iterator begin();
77341825Sdim  const_iterator end();
78341825Sdim
79341825Sdim  // SymverAliasMap iterators
80341825Sdim  using const_symver_iterator = decltype(SymverAliasMap)::const_iterator;
81341825Sdim  iterator_range<const_symver_iterator> symverAliases();
82274955Ssvnmir};
83321369Sdim
84321369Sdim} // end namespace llvm
85321369Sdim
86321369Sdim#endif // LLVM_LIB_OBJECT_RECORDSTREAMER_H
87