blob: d4f11d10136a6af42b2db969fdd47b21142e40c1 [file] [log] [blame]
Chris Lattnered47a042009-07-31 17:02:00 +00001//===- lib/MC/MCSection.cpp - Machine Code Section Representation ---------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Chandler Carruthe3e43d92017-06-06 11:49:48 +000010#include "llvm/MC/MCSection.h"
Eugene Zelenko498b48e2017-02-08 22:23:19 +000011#include "llvm/ADT/SmallVector.h"
Nico Weber0f38c602018-04-30 14:59:11 +000012#include "llvm/Config/llvm-config.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000013#include "llvm/MC/MCContext.h"
Eugene Zelenko498b48e2017-02-08 22:23:19 +000014#include "llvm/MC/MCFragment.h"
Rafael Espindola79cd79b2015-03-23 21:22:04 +000015#include "llvm/MC/MCSymbol.h"
Eugene Zelenko498b48e2017-02-08 22:23:19 +000016#include "llvm/Support/Compiler.h"
17#include "llvm/Support/ErrorHandling.h"
Chris Lattner892e1822009-08-08 22:41:53 +000018#include "llvm/Support/raw_ostream.h"
Eugene Zelenko498b48e2017-02-08 22:23:19 +000019#include <algorithm>
20#include <utility>
Chris Lattnered47a042009-07-31 17:02:00 +000021
Eugene Zelenko498b48e2017-02-08 22:23:19 +000022using namespace llvm;
Chris Lattner892e1822009-08-08 22:41:53 +000023
Rafael Espindolae86bc462015-05-25 23:14:17 +000024MCSection::MCSection(SectionVariant V, SectionKind K, MCSymbol *Begin)
Rafael Espindola13950e52015-06-01 01:05:07 +000025 : Begin(Begin), BundleGroupBeforeFirstInst(false), HasInstructions(false),
Eric Christopher42eb0822018-09-06 22:09:31 +000026 HasData(false), IsRegistered(false), DummyFragment(this), Variant(V),
27 Kind(K) {}
Rafael Espindolae86bc462015-05-25 23:14:17 +000028
Rafael Espindola75219642015-05-21 19:20:38 +000029MCSymbol *MCSection::getEndSymbol(MCContext &Ctx) {
Rafael Espindola79cd79b2015-03-23 21:22:04 +000030 if (!End)
31 End = Ctx.createTempSymbol("sec_end", true);
32 return End;
33}
34
35bool MCSection::hasEnded() const { return End && End->isInSection(); }
36
Eugene Zelenko498b48e2017-02-08 22:23:19 +000037MCSection::~MCSection() = default;
Chris Lattnered47a042009-07-31 17:02:00 +000038
Rafael Espindola68c5b832015-05-25 15:04:26 +000039void MCSection::setBundleLockState(BundleLockStateType NewState) {
40 if (NewState == NotBundleLocked) {
41 if (BundleLockNestingDepth == 0) {
42 report_fatal_error("Mismatched bundle_lock/unlock directives");
43 }
44 if (--BundleLockNestingDepth == 0) {
45 BundleLockState = NotBundleLocked;
46 }
47 return;
48 }
49
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;
54 }
55 ++BundleLockNestingDepth;
56}
Rafael Espindola5e435842015-05-25 22:57:48 +000057
Rafael Espindola899cab62015-05-27 15:14:11 +000058MCSection::iterator
Rafael Espindola919ce812015-05-27 13:37:28 +000059MCSection::getSubsectionInsertionPoint(unsigned Subsection) {
Rafael Espindola899cab62015-05-27 15:14:11 +000060 if (Subsection == 0 && SubsectionFragmentMap.empty())
Rafael Espindola919ce812015-05-27 13:37:28 +000061 return end();
62
63 SmallVectorImpl<std::pair<unsigned, MCFragment *>>::iterator MI =
Rafael Espindola899cab62015-05-27 15:14:11 +000064 std::lower_bound(SubsectionFragmentMap.begin(),
65 SubsectionFragmentMap.end(),
Rafael Espindola919ce812015-05-27 13:37:28 +000066 std::make_pair(Subsection, (MCFragment *)nullptr));
67 bool ExactMatch = false;
Rafael Espindola899cab62015-05-27 15:14:11 +000068 if (MI != SubsectionFragmentMap.end()) {
Rafael Espindola919ce812015-05-27 13:37:28 +000069 ExactMatch = MI->first == Subsection;
70 if (ExactMatch)
71 ++MI;
72 }
Rafael Espindola899cab62015-05-27 15:14:11 +000073 iterator IP;
74 if (MI == SubsectionFragmentMap.end())
Rafael Espindola919ce812015-05-27 13:37:28 +000075 IP = end();
76 else
Duncan P. N. Exon Smith5d8fcb92015-10-10 00:13:11 +000077 IP = MI->second->getIterator();
Rafael Espindola919ce812015-05-27 13:37:28 +000078 if (!ExactMatch && Subsection != 0) {
79 // The GNU as documentation claims that subsections have an alignment of 4,
80 // although this appears not to be the case.
81 MCFragment *F = new MCDataFragment();
Rafael Espindola899cab62015-05-27 15:14:11 +000082 SubsectionFragmentMap.insert(MI, std::make_pair(Subsection, F));
Rafael Espindola919ce812015-05-27 13:37:28 +000083 getFragmentList().insert(IP, F);
84 F->setParent(this);
85 }
86
87 return IP;
88}
89
Aaron Ballman1d03d382017-10-15 14:32:27 +000090#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Sam Cleggb2092062017-06-21 22:19:17 +000091LLVM_DUMP_METHOD void MCSection::dump() const {
Eugene Zelenko498b48e2017-02-08 22:23:19 +000092 raw_ostream &OS = errs();
Rafael Espindolae86bc462015-05-25 23:14:17 +000093
Rafael Espindola899cab62015-05-27 15:14:11 +000094 OS << "<MCSection";
95 OS << " Fragments:[\n ";
96 for (auto it = begin(), ie = end(); it != ie; ++it) {
97 if (it != begin())
98 OS << ",\n ";
99 it->dump();
100 }
101 OS << "]>";
Rafael Espindolae86bc462015-05-25 23:14:17 +0000102}
Matthias Braun88d20752017-01-28 02:02:38 +0000103#endif