blob: 2c892ab8160815a44cf081c9c9e118d552b9099d [file] [log] [blame]
Chris Lattnerc10132a2009-07-31 17:02:00 +00001//===- lib/MC/MCSection.cpp - Machine Code Section Representation ---------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Chris Lattnerc10132a2009-07-31 17:02:00 +00006//
7//===----------------------------------------------------------------------===//
8
Chandler Carruth6bda14b2017-06-06 11:49:48 +00009#include "llvm/MC/MCSection.h"
Eugene Zelenko3d8b0eb2017-02-08 22:23:19 +000010#include "llvm/ADT/SmallVector.h"
Nico Weber432a3882018-04-30 14:59:11 +000011#include "llvm/Config/llvm-config.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000012#include "llvm/MC/MCContext.h"
Eugene Zelenko3d8b0eb2017-02-08 22:23:19 +000013#include "llvm/MC/MCFragment.h"
Rafael Espindolaf2b408c2015-03-23 21:22:04 +000014#include "llvm/MC/MCSymbol.h"
Eugene Zelenko3d8b0eb2017-02-08 22:23:19 +000015#include "llvm/Support/Compiler.h"
16#include "llvm/Support/ErrorHandling.h"
Chris Lattner1cb93962009-08-08 22:41:53 +000017#include "llvm/Support/raw_ostream.h"
Eugene Zelenko3d8b0eb2017-02-08 22:23:19 +000018#include <algorithm>
19#include <utility>
Chris Lattnerc10132a2009-07-31 17:02:00 +000020
Eugene Zelenko3d8b0eb2017-02-08 22:23:19 +000021using namespace llvm;
Chris Lattner1cb93962009-08-08 22:41:53 +000022
Rafael Espindolaa554c052015-05-25 23:14:17 +000023MCSection::MCSection(SectionVariant V, SectionKind K, MCSymbol *Begin)
Rafael Espindolaa66395e2015-06-01 01:05:07 +000024 : Begin(Begin), BundleGroupBeforeFirstInst(false), HasInstructions(false),
Eric Christopherfe832702018-09-06 22:09:31 +000025 HasData(false), IsRegistered(false), DummyFragment(this), Variant(V),
26 Kind(K) {}
Rafael Espindolaa554c052015-05-25 23:14:17 +000027
Rafael Espindola0709a7b2015-05-21 19:20:38 +000028MCSymbol *MCSection::getEndSymbol(MCContext &Ctx) {
Rafael Espindolaf2b408c2015-03-23 21:22:04 +000029 if (!End)
30 End = Ctx.createTempSymbol("sec_end", true);
31 return End;
32}
33
34bool MCSection::hasEnded() const { return End && End->isInSection(); }
35
Eugene Zelenko3d8b0eb2017-02-08 22:23:19 +000036MCSection::~MCSection() = default;
Chris Lattnerc10132a2009-07-31 17:02:00 +000037
Rafael Espindolab028cc82015-05-25 15:04:26 +000038void MCSection::setBundleLockState(BundleLockStateType NewState) {
39 if (NewState == NotBundleLocked) {
40 if (BundleLockNestingDepth == 0) {
41 report_fatal_error("Mismatched bundle_lock/unlock directives");
42 }
43 if (--BundleLockNestingDepth == 0) {
44 BundleLockState = NotBundleLocked;
45 }
46 return;
47 }
48
49 // If any of the directives is an align_to_end directive, the whole nested
50 // group is align_to_end. So don't downgrade from align_to_end to just locked.
51 if (BundleLockState != BundleLockedAlignToEnd) {
52 BundleLockState = NewState;
53 }
54 ++BundleLockNestingDepth;
55}
Rafael Espindola09266ba2015-05-25 22:57:48 +000056
Rafael Espindolaa32d0e92015-05-27 15:14:11 +000057MCSection::iterator
Rafael Espindola8c05c6e2015-05-27 13:37:28 +000058MCSection::getSubsectionInsertionPoint(unsigned Subsection) {
Rafael Espindolaa32d0e92015-05-27 15:14:11 +000059 if (Subsection == 0 && SubsectionFragmentMap.empty())
Rafael Espindola8c05c6e2015-05-27 13:37:28 +000060 return end();
61
62 SmallVectorImpl<std::pair<unsigned, MCFragment *>>::iterator MI =
Rafael Espindolaa32d0e92015-05-27 15:14:11 +000063 std::lower_bound(SubsectionFragmentMap.begin(),
64 SubsectionFragmentMap.end(),
Rafael Espindola8c05c6e2015-05-27 13:37:28 +000065 std::make_pair(Subsection, (MCFragment *)nullptr));
66 bool ExactMatch = false;
Rafael Espindolaa32d0e92015-05-27 15:14:11 +000067 if (MI != SubsectionFragmentMap.end()) {
Rafael Espindola8c05c6e2015-05-27 13:37:28 +000068 ExactMatch = MI->first == Subsection;
69 if (ExactMatch)
70 ++MI;
71 }
Rafael Espindolaa32d0e92015-05-27 15:14:11 +000072 iterator IP;
73 if (MI == SubsectionFragmentMap.end())
Rafael Espindola8c05c6e2015-05-27 13:37:28 +000074 IP = end();
75 else
Duncan P. N. Exon Smitha5f45da2015-10-10 00:13:11 +000076 IP = MI->second->getIterator();
Rafael Espindola8c05c6e2015-05-27 13:37:28 +000077 if (!ExactMatch && Subsection != 0) {
78 // The GNU as documentation claims that subsections have an alignment of 4,
79 // although this appears not to be the case.
80 MCFragment *F = new MCDataFragment();
Rafael Espindolaa32d0e92015-05-27 15:14:11 +000081 SubsectionFragmentMap.insert(MI, std::make_pair(Subsection, F));
Rafael Espindola8c05c6e2015-05-27 13:37:28 +000082 getFragmentList().insert(IP, F);
83 F->setParent(this);
84 }
85
86 return IP;
87}
88
Aaron Ballman615eb472017-10-15 14:32:27 +000089#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Sam Clegg705f7982017-06-21 22:19:17 +000090LLVM_DUMP_METHOD void MCSection::dump() const {
Eugene Zelenko3d8b0eb2017-02-08 22:23:19 +000091 raw_ostream &OS = errs();
Rafael Espindolaa554c052015-05-25 23:14:17 +000092
Rafael Espindolaa32d0e92015-05-27 15:14:11 +000093 OS << "<MCSection";
94 OS << " Fragments:[\n ";
95 for (auto it = begin(), ie = end(); it != ie; ++it) {
96 if (it != begin())
97 OS << ",\n ";
98 it->dump();
99 }
100 OS << "]>";
Rafael Espindolaa554c052015-05-25 23:14:17 +0000101}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000102#endif