xref: /llvm-project/llvm/lib/MC/MCSection.cpp (revision d73d5c8c9b56fadcbd89dd1dab71dca2c8f5e38d)
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"
16 #include "llvm/Support/ErrorHandling.h"
17 #include "llvm/Support/raw_ostream.h"
18 #include <utility>
19 
20 using namespace llvm;
21 
22 MCSection::MCSection(SectionVariant V, StringRef Name, bool IsText,
23                      bool IsVirtual, MCSymbol *Begin)
24     : Begin(Begin), BundleGroupBeforeFirstInst(false), HasInstructions(false),
25       HasLayout(false), IsRegistered(false), IsText(IsText),
26       IsVirtual(IsVirtual), Name(Name), Variant(V) {
27   DummyFragment.setParent(this);
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 StringRef MCSection::getVirtualSectionKind() const { return "virtual"; }
69 
70 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
71 LLVM_DUMP_METHOD void MCSection::dump() const {
72   raw_ostream &OS = errs();
73 
74   OS << "<MCSection Name:" << getName();
75   OS << " Fragments:[\n      ";
76   bool First = true;
77   for (auto &F : *this) {
78     if (First)
79       First = false;
80     else
81       OS << ",\n      ";
82     F.dump();
83   }
84   OS << "]>";
85 }
86 #endif
87