blob: d141dd6627c466a5f5eab8f4890151a5c9fd08aa [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
Chandler Carruth6bda14b2017-06-06 11:49:48 +000010#include "llvm/MC/MCSection.h"
Eugene Zelenko3d8b0eb2017-02-08 22:23:19 +000011#include "llvm/ADT/SmallVector.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),
Rafael Espindolae3a20f52015-10-05 12:07:05 +000025 IsRegistered(false), DummyFragment(this), Variant(V), Kind(K) {}
Rafael Espindolaa554c052015-05-25 23:14:17 +000026
Rafael Espindola0709a7b2015-05-21 19:20:38 +000027MCSymbol *MCSection::getEndSymbol(MCContext &Ctx) {
Rafael Espindolaf2b408c2015-03-23 21:22:04 +000028 if (!End)
29 End = Ctx.createTempSymbol("sec_end", true);
30 return End;
31}
32
33bool MCSection::hasEnded() const { return End && End->isInSection(); }
34
Eugene Zelenko3d8b0eb2017-02-08 22:23:19 +000035MCSection::~MCSection() = default;
Chris Lattnerc10132a2009-07-31 17:02:00 +000036
Rafael Espindolab028cc82015-05-25 15:04:26 +000037void MCSection::setBundleLockState(BundleLockStateType NewState) {
38 if (NewState == NotBundleLocked) {
39 if (BundleLockNestingDepth == 0) {
40 report_fatal_error("Mismatched bundle_lock/unlock directives");
41 }
42 if (--BundleLockNestingDepth == 0) {
43 BundleLockState = NotBundleLocked;
44 }
45 return;
46 }
47
48 // If any of the directives is an align_to_end directive, the whole nested
49 // group is align_to_end. So don't downgrade from align_to_end to just locked.
50 if (BundleLockState != BundleLockedAlignToEnd) {
51 BundleLockState = NewState;
52 }
53 ++BundleLockNestingDepth;
54}
Rafael Espindola09266ba2015-05-25 22:57:48 +000055
Rafael Espindolaa32d0e92015-05-27 15:14:11 +000056MCSection::iterator
Rafael Espindola8c05c6e2015-05-27 13:37:28 +000057MCSection::getSubsectionInsertionPoint(unsigned Subsection) {
Rafael Espindolaa32d0e92015-05-27 15:14:11 +000058 if (Subsection == 0 && SubsectionFragmentMap.empty())
Rafael Espindola8c05c6e2015-05-27 13:37:28 +000059 return end();
60
61 SmallVectorImpl<std::pair<unsigned, MCFragment *>>::iterator MI =
Rafael Espindolaa32d0e92015-05-27 15:14:11 +000062 std::lower_bound(SubsectionFragmentMap.begin(),
63 SubsectionFragmentMap.end(),
Rafael Espindola8c05c6e2015-05-27 13:37:28 +000064 std::make_pair(Subsection, (MCFragment *)nullptr));
65 bool ExactMatch = false;
Rafael Espindolaa32d0e92015-05-27 15:14:11 +000066 if (MI != SubsectionFragmentMap.end()) {
Rafael Espindola8c05c6e2015-05-27 13:37:28 +000067 ExactMatch = MI->first == Subsection;
68 if (ExactMatch)
69 ++MI;
70 }
Rafael Espindolaa32d0e92015-05-27 15:14:11 +000071 iterator IP;
72 if (MI == SubsectionFragmentMap.end())
Rafael Espindola8c05c6e2015-05-27 13:37:28 +000073 IP = end();
74 else
Duncan P. N. Exon Smitha5f45da2015-10-10 00:13:11 +000075 IP = MI->second->getIterator();
Rafael Espindola8c05c6e2015-05-27 13:37:28 +000076 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();
Rafael Espindolaa32d0e92015-05-27 15:14:11 +000080 SubsectionFragmentMap.insert(MI, std::make_pair(Subsection, F));
Rafael Espindola8c05c6e2015-05-27 13:37:28 +000081 getFragmentList().insert(IP, F);
82 F->setParent(this);
83 }
84
85 return IP;
86}
87
Aaron Ballman615eb472017-10-15 14:32:27 +000088#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Sam Clegg705f7982017-06-21 22:19:17 +000089LLVM_DUMP_METHOD void MCSection::dump() const {
Eugene Zelenko3d8b0eb2017-02-08 22:23:19 +000090 raw_ostream &OS = errs();
Rafael Espindolaa554c052015-05-25 23:14:17 +000091
Rafael Espindolaa32d0e92015-05-27 15:14:11 +000092 OS << "<MCSection";
93 OS << " Fragments:[\n ";
94 for (auto it = begin(), ie = end(); it != ie; ++it) {
95 if (it != begin())
96 OS << ",\n ";
97 it->dump();
98 }
99 OS << "]>";
Rafael Espindolaa554c052015-05-25 23:14:17 +0000100}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000101#endif