blob: 04f932bfeec07903c7786f185a4c7edb50f07c2b [file] [log] [blame]
Chris Lattnerc10132a2009-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
10#include "llvm/MC/MCSection.h"
Rafael Espindola09266ba2015-05-25 22:57:48 +000011#include "llvm/MC/MCAssembler.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000012#include "llvm/MC/MCAsmInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000013#include "llvm/MC/MCContext.h"
Rafael Espindolaf2b408c2015-03-23 21:22:04 +000014#include "llvm/MC/MCSymbol.h"
Chris Lattner1cb93962009-08-08 22:41:53 +000015#include "llvm/Support/raw_ostream.h"
Chris Lattnerc10132a2009-07-31 17:02:00 +000016using namespace llvm;
17
Chris Lattner1cb93962009-08-08 22:41:53 +000018//===----------------------------------------------------------------------===//
19// MCSection
20//===----------------------------------------------------------------------===//
21
Rafael Espindolaa554c052015-05-25 23:14:17 +000022MCSection::MCSection(SectionVariant V, SectionKind K, MCSymbol *Begin)
Rafael Espindolaa32d0e92015-05-27 15:14:11 +000023 : Begin(Begin), HasInstructions(false), Variant(V), Kind(K) {}
Rafael Espindolaa554c052015-05-25 23:14:17 +000024
Rafael Espindola0709a7b2015-05-21 19:20:38 +000025MCSymbol *MCSection::getEndSymbol(MCContext &Ctx) {
Rafael Espindolaf2b408c2015-03-23 21:22:04 +000026 if (!End)
27 End = Ctx.createTempSymbol("sec_end", true);
28 return End;
29}
30
31bool MCSection::hasEnded() const { return End && End->isInSection(); }
32
Chris Lattnerc10132a2009-07-31 17:02:00 +000033MCSection::~MCSection() {
34}
35
Rafael Espindolab028cc82015-05-25 15:04:26 +000036void MCSection::setBundleLockState(BundleLockStateType NewState) {
37 if (NewState == NotBundleLocked) {
38 if (BundleLockNestingDepth == 0) {
39 report_fatal_error("Mismatched bundle_lock/unlock directives");
40 }
41 if (--BundleLockNestingDepth == 0) {
42 BundleLockState = NotBundleLocked;
43 }
44 return;
45 }
46
47 // If any of the directives is an align_to_end directive, the whole nested
48 // group is align_to_end. So don't downgrade from align_to_end to just locked.
49 if (BundleLockState != BundleLockedAlignToEnd) {
50 BundleLockState = NewState;
51 }
52 ++BundleLockNestingDepth;
53}
Rafael Espindola09266ba2015-05-25 22:57:48 +000054
Rafael Espindolaa32d0e92015-05-27 15:14:11 +000055MCSection::iterator
Rafael Espindola8c05c6e2015-05-27 13:37:28 +000056MCSection::getSubsectionInsertionPoint(unsigned Subsection) {
Rafael Espindolaa32d0e92015-05-27 15:14:11 +000057 if (Subsection == 0 && SubsectionFragmentMap.empty())
Rafael Espindola8c05c6e2015-05-27 13:37:28 +000058 return end();
59
60 SmallVectorImpl<std::pair<unsigned, MCFragment *>>::iterator MI =
Rafael Espindolaa32d0e92015-05-27 15:14:11 +000061 std::lower_bound(SubsectionFragmentMap.begin(),
62 SubsectionFragmentMap.end(),
Rafael Espindola8c05c6e2015-05-27 13:37:28 +000063 std::make_pair(Subsection, (MCFragment *)nullptr));
64 bool ExactMatch = false;
Rafael Espindolaa32d0e92015-05-27 15:14:11 +000065 if (MI != SubsectionFragmentMap.end()) {
Rafael Espindola8c05c6e2015-05-27 13:37:28 +000066 ExactMatch = MI->first == Subsection;
67 if (ExactMatch)
68 ++MI;
69 }
Rafael Espindolaa32d0e92015-05-27 15:14:11 +000070 iterator IP;
71 if (MI == SubsectionFragmentMap.end())
Rafael Espindola8c05c6e2015-05-27 13:37:28 +000072 IP = end();
73 else
74 IP = MI->second;
75 if (!ExactMatch && Subsection != 0) {
76 // The GNU as documentation claims that subsections have an alignment of 4,
77 // although this appears not to be the case.
78 MCFragment *F = new MCDataFragment();
Rafael Espindolaa32d0e92015-05-27 15:14:11 +000079 SubsectionFragmentMap.insert(MI, std::make_pair(Subsection, F));
Rafael Espindola8c05c6e2015-05-27 13:37:28 +000080 getFragmentList().insert(IP, F);
81 F->setParent(this);
82 }
83
84 return IP;
85}
86
Rafael Espindola2e4be0a2015-05-27 15:18:34 +000087#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Rafael Espindolaa32d0e92015-05-27 15:14:11 +000088void MCSection::dump() {
89 raw_ostream &OS = llvm::errs();
Rafael Espindolaa554c052015-05-25 23:14:17 +000090
Rafael Espindolaa32d0e92015-05-27 15:14:11 +000091 OS << "<MCSection";
92 OS << " Fragments:[\n ";
93 for (auto it = begin(), ie = end(); it != ie; ++it) {
94 if (it != begin())
95 OS << ",\n ";
96 it->dump();
97 }
98 OS << "]>";
Rafael Espindolaa554c052015-05-25 23:14:17 +000099}
Rafael Espindola2e4be0a2015-05-27 15:18:34 +0000100#endif
Rafael Espindolaa554c052015-05-25 23:14:17 +0000101
Rafael Espindolaa32d0e92015-05-27 15:14:11 +0000102MCSection::iterator MCSection::begin() { return Fragments.begin(); }
Rafael Espindola09266ba2015-05-25 22:57:48 +0000103
Rafael Espindolaa32d0e92015-05-27 15:14:11 +0000104MCSection::iterator MCSection::end() { return Fragments.end(); }
Rafael Espindola09266ba2015-05-25 22:57:48 +0000105
Rafael Espindolaa32d0e92015-05-27 15:14:11 +0000106MCSection::reverse_iterator MCSection::rbegin() { return Fragments.rbegin(); }
Rafael Espindola09266ba2015-05-25 22:57:48 +0000107
Rafael Espindolaa32d0e92015-05-27 15:14:11 +0000108MCSection::reverse_iterator MCSection::rend() { return Fragments.rend(); }