[ORC] Add std::tuple support to SimplePackedSerialization.
[llvm-project.git] / llvm / lib / MC / MCSection.cpp
blob8342abacec091a58d257ef4997eab7d37d340444
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 //===----------------------------------------------------------------------===//
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 <algorithm>
19 #include <utility>
21 using namespace llvm;
23 MCSection::MCSection(SectionVariant V, StringRef Name, SectionKind K,
24 MCSymbol *Begin)
25 : Begin(Begin), BundleGroupBeforeFirstInst(false), HasInstructions(false),
26 IsRegistered(false), DummyFragment(this), Name(Name), Variant(V),
27 Kind(K) {}
29 MCSymbol *MCSection::getEndSymbol(MCContext &Ctx) {
30 if (!End)
31 End = Ctx.createTempSymbol("sec_end");
32 return End;
35 bool MCSection::hasEnded() const { return End && End->isInSection(); }
37 MCSection::~MCSection() = default;
39 void MCSection::setBundleLockState(BundleLockStateType NewState) {
40 if (NewState == NotBundleLocked) {
41 if (BundleLockNestingDepth == 0) {
42 report_fatal_error("Mismatched bundle_lock/unlock directives");
44 if (--BundleLockNestingDepth == 0) {
45 BundleLockState = NotBundleLocked;
47 return;
50 // If any of the directives is an align_to_end directive, the whole nested
51 // group is align_to_end. So don't downgrade from align_to_end to just locked.
52 if (BundleLockState != BundleLockedAlignToEnd) {
53 BundleLockState = NewState;
55 ++BundleLockNestingDepth;
58 MCSection::iterator
59 MCSection::getSubsectionInsertionPoint(unsigned Subsection) {
60 if (Subsection == 0 && SubsectionFragmentMap.empty())
61 return end();
63 SmallVectorImpl<std::pair<unsigned, MCFragment *>>::iterator MI = lower_bound(
64 SubsectionFragmentMap, 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;
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 F->setSubsectionNumber(Subsection);
86 return IP;
89 StringRef MCSection::getVirtualSectionKind() const { return "virtual"; }
91 void MCSection::addPendingLabel(MCSymbol *label, unsigned Subsection) {
92 PendingLabels.push_back(PendingLabel(label, Subsection));
95 void MCSection::flushPendingLabels(MCFragment *F, uint64_t FOffset,
96 unsigned Subsection) {
97 if (PendingLabels.empty())
98 return;
100 // Set the fragment and fragment offset for all pending symbols in the
101 // specified Subsection, and remove those symbols from the pending list.
102 for (auto It = PendingLabels.begin(); It != PendingLabels.end(); ++It) {
103 PendingLabel& Label = *It;
104 if (Label.Subsection == Subsection) {
105 Label.Sym->setFragment(F);
106 Label.Sym->setOffset(FOffset);
107 PendingLabels.erase(It--);
112 void MCSection::flushPendingLabels() {
113 // Make sure all remaining pending labels point to data fragments, by
114 // creating new empty data fragments for each Subsection with labels pending.
115 while (!PendingLabels.empty()) {
116 PendingLabel& Label = PendingLabels[0];
117 iterator CurInsertionPoint =
118 this->getSubsectionInsertionPoint(Label.Subsection);
119 MCFragment *F = new MCDataFragment();
120 getFragmentList().insert(CurInsertionPoint, F);
121 F->setParent(this);
122 flushPendingLabels(F, 0, Label.Subsection);
126 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
127 LLVM_DUMP_METHOD void MCSection::dump() const {
128 raw_ostream &OS = errs();
130 OS << "<MCSection";
131 OS << " Fragments:[\n ";
132 for (auto it = begin(), ie = end(); it != ie; ++it) {
133 if (it != begin())
134 OS << ",\n ";
135 it->dump();
137 OS << "]>";
139 #endif