Chris Lattner | c10132a | 2009-07-31 17:02:00 +0000 | [diff] [blame] | 1 | //===- lib/MC/MCSection.cpp - Machine Code Section Representation ---------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 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 |
Chris Lattner | c10132a | 2009-07-31 17:02:00 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 9 | #include "llvm/MC/MCSection.h" |
Eugene Zelenko | 3d8b0eb | 2017-02-08 22:23:19 +0000 | [diff] [blame] | 10 | #include "llvm/ADT/SmallVector.h" |
Nico Weber | 432a388 | 2018-04-30 14:59:11 +0000 | [diff] [blame] | 11 | #include "llvm/Config/llvm-config.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 12 | #include "llvm/MC/MCContext.h" |
Eugene Zelenko | 3d8b0eb | 2017-02-08 22:23:19 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCFragment.h" |
Rafael Espindola | f2b408c | 2015-03-23 21:22:04 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCSymbol.h" |
Eugene Zelenko | 3d8b0eb | 2017-02-08 22:23:19 +0000 | [diff] [blame] | 15 | #include "llvm/Support/Compiler.h" |
| 16 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | 1cb9396 | 2009-08-08 22:41:53 +0000 | [diff] [blame] | 17 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 3d8b0eb | 2017-02-08 22:23:19 +0000 | [diff] [blame] | 18 | #include <algorithm> |
| 19 | #include <utility> |
Chris Lattner | c10132a | 2009-07-31 17:02:00 +0000 | [diff] [blame] | 20 | |
Eugene Zelenko | 3d8b0eb | 2017-02-08 22:23:19 +0000 | [diff] [blame] | 21 | using namespace llvm; |
Chris Lattner | 1cb9396 | 2009-08-08 22:41:53 +0000 | [diff] [blame] | 22 | |
Rafael Espindola | a554c05 | 2015-05-25 23:14:17 +0000 | [diff] [blame] | 23 | MCSection::MCSection(SectionVariant V, SectionKind K, MCSymbol *Begin) |
Rafael Espindola | a66395e | 2015-06-01 01:05:07 +0000 | [diff] [blame] | 24 | : Begin(Begin), BundleGroupBeforeFirstInst(false), HasInstructions(false), |
Eric Christopher | fe83270 | 2018-09-06 22:09:31 +0000 | [diff] [blame] | 25 | HasData(false), IsRegistered(false), DummyFragment(this), Variant(V), |
| 26 | Kind(K) {} |
Rafael Espindola | a554c05 | 2015-05-25 23:14:17 +0000 | [diff] [blame] | 27 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 28 | MCSymbol *MCSection::getEndSymbol(MCContext &Ctx) { |
Rafael Espindola | f2b408c | 2015-03-23 21:22:04 +0000 | [diff] [blame] | 29 | if (!End) |
| 30 | End = Ctx.createTempSymbol("sec_end", true); |
| 31 | return End; |
| 32 | } |
| 33 | |
| 34 | bool MCSection::hasEnded() const { return End && End->isInSection(); } |
| 35 | |
Eugene Zelenko | 3d8b0eb | 2017-02-08 22:23:19 +0000 | [diff] [blame] | 36 | MCSection::~MCSection() = default; |
Chris Lattner | c10132a | 2009-07-31 17:02:00 +0000 | [diff] [blame] | 37 | |
Rafael Espindola | b028cc8 | 2015-05-25 15:04:26 +0000 | [diff] [blame] | 38 | void 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 Espindola | 09266ba | 2015-05-25 22:57:48 +0000 | [diff] [blame] | 56 | |
Rafael Espindola | a32d0e9 | 2015-05-27 15:14:11 +0000 | [diff] [blame] | 57 | MCSection::iterator |
Rafael Espindola | 8c05c6e | 2015-05-27 13:37:28 +0000 | [diff] [blame] | 58 | MCSection::getSubsectionInsertionPoint(unsigned Subsection) { |
Rafael Espindola | a32d0e9 | 2015-05-27 15:14:11 +0000 | [diff] [blame] | 59 | if (Subsection == 0 && SubsectionFragmentMap.empty()) |
Rafael Espindola | 8c05c6e | 2015-05-27 13:37:28 +0000 | [diff] [blame] | 60 | return end(); |
| 61 | |
| 62 | SmallVectorImpl<std::pair<unsigned, MCFragment *>>::iterator MI = |
Rafael Espindola | a32d0e9 | 2015-05-27 15:14:11 +0000 | [diff] [blame] | 63 | std::lower_bound(SubsectionFragmentMap.begin(), |
| 64 | SubsectionFragmentMap.end(), |
Rafael Espindola | 8c05c6e | 2015-05-27 13:37:28 +0000 | [diff] [blame] | 65 | std::make_pair(Subsection, (MCFragment *)nullptr)); |
| 66 | bool ExactMatch = false; |
Rafael Espindola | a32d0e9 | 2015-05-27 15:14:11 +0000 | [diff] [blame] | 67 | if (MI != SubsectionFragmentMap.end()) { |
Rafael Espindola | 8c05c6e | 2015-05-27 13:37:28 +0000 | [diff] [blame] | 68 | ExactMatch = MI->first == Subsection; |
| 69 | if (ExactMatch) |
| 70 | ++MI; |
| 71 | } |
Rafael Espindola | a32d0e9 | 2015-05-27 15:14:11 +0000 | [diff] [blame] | 72 | iterator IP; |
| 73 | if (MI == SubsectionFragmentMap.end()) |
Rafael Espindola | 8c05c6e | 2015-05-27 13:37:28 +0000 | [diff] [blame] | 74 | IP = end(); |
| 75 | else |
Duncan P. N. Exon Smith | a5f45da | 2015-10-10 00:13:11 +0000 | [diff] [blame] | 76 | IP = MI->second->getIterator(); |
Rafael Espindola | 8c05c6e | 2015-05-27 13:37:28 +0000 | [diff] [blame] | 77 | 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 Espindola | a32d0e9 | 2015-05-27 15:14:11 +0000 | [diff] [blame] | 81 | SubsectionFragmentMap.insert(MI, std::make_pair(Subsection, F)); |
Rafael Espindola | 8c05c6e | 2015-05-27 13:37:28 +0000 | [diff] [blame] | 82 | getFragmentList().insert(IP, F); |
| 83 | F->setParent(this); |
| 84 | } |
| 85 | |
| 86 | return IP; |
| 87 | } |
| 88 | |
Aaron Ballman | 615eb47 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 89 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Sam Clegg | 705f798 | 2017-06-21 22:19:17 +0000 | [diff] [blame] | 90 | LLVM_DUMP_METHOD void MCSection::dump() const { |
Eugene Zelenko | 3d8b0eb | 2017-02-08 22:23:19 +0000 | [diff] [blame] | 91 | raw_ostream &OS = errs(); |
Rafael Espindola | a554c05 | 2015-05-25 23:14:17 +0000 | [diff] [blame] | 92 | |
Rafael Espindola | a32d0e9 | 2015-05-27 15:14:11 +0000 | [diff] [blame] | 93 | 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 Espindola | a554c05 | 2015-05-25 23:14:17 +0000 | [diff] [blame] | 101 | } |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 102 | #endif |