xref: /llvm-project/llvm/lib/MC/MCSection.cpp (revision b1932b8483011c2bfebbea1ef56a565634570e6b)
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/STLExtras.h"
11 #include "llvm/ADT/SmallVector.h"
12 #include "llvm/Config/llvm-config.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCFragment.h"
15 #include "llvm/MC/MCSymbol.h"
16 #include "llvm/Support/Compiler.h"
17 #include "llvm/Support/ErrorHandling.h"
18 #include "llvm/Support/raw_ostream.h"
19 #include <utility>
20 
21 using namespace llvm;
22 
23 MCSection::MCSection(SectionVariant V, StringRef Name, SectionKind K,
24                      MCSymbol *Begin)
25     : Begin(Begin), BundleGroupBeforeFirstInst(false), HasInstructions(false),
26       HasLayout(false), IsRegistered(false), DummyFragment(this), Name(Name),
27       Variant(V), Kind(K) {
28   // The initial subsection number is 0. Create a fragment list.
29   CurFragList = &Subsections.emplace_back(0u, FragList{}).second;
30 }
31 
32 MCSymbol *MCSection::getEndSymbol(MCContext &Ctx) {
33   if (!End)
34     End = Ctx.createTempSymbol("sec_end");
35   return End;
36 }
37 
38 bool MCSection::hasEnded() const { return End && End->isInSection(); }
39 
40 MCSection::~MCSection() {
41   for (auto &[_, Chain] : Subsections) {
42     for (MCFragment *X = Chain.Head, *Y; X; X = Y) {
43       Y = X->Next;
44       X->destroy();
45     }
46   }
47 }
48 
49 void MCSection::setBundleLockState(BundleLockStateType NewState) {
50   if (NewState == NotBundleLocked) {
51     if (BundleLockNestingDepth == 0) {
52       report_fatal_error("Mismatched bundle_lock/unlock directives");
53     }
54     if (--BundleLockNestingDepth == 0) {
55       BundleLockState = NotBundleLocked;
56     }
57     return;
58   }
59 
60   // If any of the directives is an align_to_end directive, the whole nested
61   // group is align_to_end. So don't downgrade from align_to_end to just locked.
62   if (BundleLockState != BundleLockedAlignToEnd) {
63     BundleLockState = NewState;
64   }
65   ++BundleLockNestingDepth;
66 }
67 
68 void MCSection::switchSubsection(unsigned Subsection) {
69   size_t I = 0, E = Subsections.size();
70   while (I != E && Subsections[I].first < Subsection)
71     ++I;
72   // If the subsection number is not in the sorted Subsections list, create a
73   // new fragment list.
74   if (I == E || Subsections[I].first != Subsection)
75     Subsections.insert(Subsections.begin() + I, {Subsection, FragList{}});
76   CurFragList = &Subsections[I].second;
77 }
78 
79 StringRef MCSection::getVirtualSectionKind() const { return "virtual"; }
80 
81 void MCSection::addPendingLabel(MCSymbol *label, unsigned Subsection) {
82   PendingLabels.push_back(PendingLabel(label, Subsection));
83 }
84 
85 void MCSection::flushPendingLabels(MCFragment *F, unsigned Subsection) {
86   // Set the fragment and fragment offset for all pending symbols in the
87   // specified Subsection, and remove those symbols from the pending list.
88   for (auto It = PendingLabels.begin(); It != PendingLabels.end(); ++It) {
89     PendingLabel& Label = *It;
90     if (Label.Subsection == Subsection) {
91       Label.Sym->setFragment(F);
92       assert(Label.Sym->getOffset() == 0);
93       PendingLabels.erase(It--);
94     }
95   }
96 }
97 
98 void MCSection::flushPendingLabels() {
99   // Make sure all remaining pending labels point to data fragments, by
100   // creating new empty data fragments for each Subsection with labels pending.
101   while (!PendingLabels.empty()) {
102     PendingLabel& Label = PendingLabels[0];
103     switchSubsection(Label.Subsection);
104     MCFragment *F = new MCDataFragment();
105     addFragment(*F);
106     F->setParent(this);
107     flushPendingLabels(F, Label.Subsection);
108   }
109 }
110 
111 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
112 LLVM_DUMP_METHOD void MCSection::dump() const {
113   raw_ostream &OS = errs();
114 
115   OS << "<MCSection Name:" << getName();
116   OS << " Fragments:[\n      ";
117   bool First = true;
118   for (auto &F : *this) {
119     if (First)
120       First = false;
121     else
122       OS << ",\n      ";
123     F.dump();
124   }
125   OS << "]>";
126 }
127 #endif
128