LLVM  10.0.0
MCSection.cpp
Go to the documentation of this file.
1 //===- lib/MC/MCSection.cpp - Machine Code Section Representation ---------===//
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 
9 #include "llvm/MC/MCSection.h"
10 #include "llvm/ADT/SmallVector.h"
11 #include "llvm/Config/llvm-config.h"
12 #include "llvm/MC/MCContext.h"
13 #include "llvm/MC/MCFragment.h"
14 #include "llvm/MC/MCSymbol.h"
15 #include "llvm/Support/Compiler.h"
18 #include <algorithm>
19 #include <utility>
20 
21 using namespace llvm;
22 
24  : Begin(Begin), BundleGroupBeforeFirstInst(false), HasInstructions(false),
25  IsRegistered(false), DummyFragment(this), Variant(V), Kind(K) {}
26 
28  if (!End)
29  End = Ctx.createTempSymbol("sec_end", true);
30  return End;
31 }
32 
33 bool MCSection::hasEnded() const { return End && End->isInSection(); }
34 
35 MCSection::~MCSection() = default;
36 
38  if (NewState == NotBundleLocked) {
39  if (BundleLockNestingDepth == 0) {
40  report_fatal_error("Mismatched bundle_lock/unlock directives");
41  }
42  if (--BundleLockNestingDepth == 0) {
43  BundleLockState = NotBundleLocked;
44  }
45  return;
46  }
47 
48  // If any of the directives is an align_to_end directive, the whole nested
49  // group is align_to_end. So don't downgrade from align_to_end to just locked.
50  if (BundleLockState != BundleLockedAlignToEnd) {
51  BundleLockState = NewState;
52  }
53  ++BundleLockNestingDepth;
54 }
55 
58  if (Subsection == 0 && SubsectionFragmentMap.empty())
59  return end();
60 
62  std::lower_bound(SubsectionFragmentMap.begin(),
63  SubsectionFragmentMap.end(),
64  std::make_pair(Subsection, (MCFragment *)nullptr));
65  bool ExactMatch = false;
66  if (MI != SubsectionFragmentMap.end()) {
67  ExactMatch = MI->first == Subsection;
68  if (ExactMatch)
69  ++MI;
70  }
71  iterator IP;
72  if (MI == SubsectionFragmentMap.end())
73  IP = end();
74  else
75  IP = MI->second->getIterator();
76  if (!ExactMatch && Subsection != 0) {
77  // The GNU as documentation claims that subsections have an alignment of 4,
78  // although this appears not to be the case.
79  MCFragment *F = new MCDataFragment();
80  SubsectionFragmentMap.insert(MI, std::make_pair(Subsection, F));
81  getFragmentList().insert(IP, F);
82  F->setParent(this);
83  }
84 
85  return IP;
86 }
87 
88 void MCSection::addPendingLabel(MCSymbol* label, unsigned Subsection) {
89  PendingLabels.push_back(PendingLabel(label, Subsection));
90 }
91 
92 void MCSection::flushPendingLabels(MCFragment *F, uint64_t FOffset,
93  unsigned Subsection) {
94  if (PendingLabels.empty())
95  return;
96 
97  // Set the fragment and fragment offset for all pending symbols in the
98  // specified Subsection, and remove those symbols from the pending list.
99  for (auto It = PendingLabels.begin(); It != PendingLabels.end(); ++It) {
100  PendingLabel& Label = *It;
101  if (Label.Subsection == Subsection) {
102  Label.Sym->setFragment(F);
103  Label.Sym->setOffset(FOffset);
104  PendingLabels.erase(It--);
105  }
106  }
107 }
108 
110  // Make sure all remaining pending labels point to data fragments, by
111  // creating new empty data fragments for each Subsection with labels pending.
112  while (!PendingLabels.empty()) {
113  PendingLabel& Label = PendingLabels[0];
114  iterator CurInsertionPoint =
115  this->getSubsectionInsertionPoint(Label.Subsection);
116  MCFragment *F = new MCDataFragment();
117  getFragmentList().insert(CurInsertionPoint, F);
118  F->setParent(this);
119  flushPendingLabels(F, 0, Label.Subsection);
120  }
121 }
122 
123 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
125  raw_ostream &OS = errs();
126 
127  OS << "<MCSection";
128  OS << " Fragments:[\n ";
129  for (auto it = begin(), ie = end(); it != ie; ++it) {
130  if (it != begin())
131  OS << ",\n ";
132  it->dump();
133  }
134  OS << "]>";
135 }
136 #endif
auto lower_bound(R &&Range, T &&Value) -> decltype(adl_begin(Range))
Provide wrappers to std::lower_bound which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1278
void addPendingLabel(MCSymbol *label, unsigned Subsection=0)
Add a pending label for the requested subsection.
Definition: MCSection.cpp:88
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:140
This class represents lattice values for constants.
Definition: AllocatorList.h:23
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Definition: Compiler.h:492
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
void push_back(const T &Elt)
Definition: SmallVector.h:211
MCSection(SectionVariant V, SectionKind K, MCSymbol *Begin)
Definition: MCSection.cpp:23
void setBundleLockState(BundleLockStateType NewState)
Definition: MCSection.cpp:37
F(f)
void dump() const
Definition: MCSection.cpp:124
BundleLockStateType
Express the state of bundle locked groups while emitting code.
Definition: MCSection.h:44
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
bool isInSection() const
isInSection - Check if this symbol is defined in some section (i.e., it is defined but not absolute)...
Definition: MCSymbol.h:245
MCSymbol * getEndSymbol(MCContext &Ctx)
Definition: MCSection.cpp:27
Context object for machine code objects.
Definition: MCContext.h:65
bool hasEnded() const
Definition: MCSection.cpp:33
void flushPendingLabels()
Associate all pending labels with empty data fragments.
Definition: MCSection.cpp:109
MCSymbol * createTempSymbol(bool CanBeUnnamed=true)
Create and return a new assembler temporary symbol with a unique but unspecified name.
Definition: MCContext.cpp:226
SectionKind - This is a simple POD value that classifies the properties of a section.
Definition: SectionKind.h:22
iterator erase(const_iterator CI)
Definition: SmallVector.h:434
Iterator for intrusive lists based on ilist_node.
iterator insert(iterator where, pointer New)
Definition: ilist.h:226
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:55
Fragment for data and encoded instructions.
Definition: MCFragment.h:229
MCSection::iterator getSubsectionInsertionPoint(unsigned Subsection)
Definition: MCSection.cpp:57
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
IRTranslator LLVM IR MI
iterator end()
Definition: MCSection.h:168
void setParent(MCSection *Value)
Definition: MCFragment.h:88
MCSection::FragmentListType & getFragmentList()
Definition: MCSection.h:152
iterator begin()
Definition: MCSection.h:165