Chris Lattner | ed47a04 | 2009-07-31 17:02:00 +0000 | [diff] [blame] | 1 | //===- 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 Carruth | e3e43d9 | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 10 | #include "llvm/MC/MCSection.h" |
Eugene Zelenko | 498b48e | 2017-02-08 22:23:19 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/SmallVector.h" |
Nico Weber | 0f38c60 | 2018-04-30 14:59:11 +0000 | [diff] [blame] | 12 | #include "llvm/Config/llvm-config.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCContext.h" |
Eugene Zelenko | 498b48e | 2017-02-08 22:23:19 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCFragment.h" |
Rafael Espindola | 79cd79b | 2015-03-23 21:22:04 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCSymbol.h" |
Eugene Zelenko | 498b48e | 2017-02-08 22:23:19 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Compiler.h" |
| 17 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | 892e182 | 2009-08-08 22:41:53 +0000 | [diff] [blame] | 18 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 498b48e | 2017-02-08 22:23:19 +0000 | [diff] [blame] | 19 | #include <algorithm> |
| 20 | #include <utility> |
Chris Lattner | ed47a04 | 2009-07-31 17:02:00 +0000 | [diff] [blame] | 21 | |
Eugene Zelenko | 498b48e | 2017-02-08 22:23:19 +0000 | [diff] [blame] | 22 | using namespace llvm; |
Chris Lattner | 892e182 | 2009-08-08 22:41:53 +0000 | [diff] [blame] | 23 | |
Rafael Espindola | e86bc46 | 2015-05-25 23:14:17 +0000 | [diff] [blame] | 24 | MCSection::MCSection(SectionVariant V, SectionKind K, MCSymbol *Begin) |
Rafael Espindola | 13950e5 | 2015-06-01 01:05:07 +0000 | [diff] [blame] | 25 | : Begin(Begin), BundleGroupBeforeFirstInst(false), HasInstructions(false), |
Eric Christopher | 42eb082 | 2018-09-06 22:09:31 +0000 | [diff] [blame] | 26 | HasData(false), IsRegistered(false), DummyFragment(this), Variant(V), |
| 27 | Kind(K) {} |
Rafael Espindola | e86bc46 | 2015-05-25 23:14:17 +0000 | [diff] [blame] | 28 | |
Rafael Espindola | 7521964 | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 29 | MCSymbol *MCSection::getEndSymbol(MCContext &Ctx) { |
Rafael Espindola | 79cd79b | 2015-03-23 21:22:04 +0000 | [diff] [blame] | 30 | if (!End) |
| 31 | End = Ctx.createTempSymbol("sec_end", true); |
| 32 | return End; |
| 33 | } |
| 34 | |
| 35 | bool MCSection::hasEnded() const { return End && End->isInSection(); } |
| 36 | |
Eugene Zelenko | 498b48e | 2017-02-08 22:23:19 +0000 | [diff] [blame] | 37 | MCSection::~MCSection() = default; |
Chris Lattner | ed47a04 | 2009-07-31 17:02:00 +0000 | [diff] [blame] | 38 | |
Rafael Espindola | 68c5b83 | 2015-05-25 15:04:26 +0000 | [diff] [blame] | 39 | void 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 Espindola | 5e43584 | 2015-05-25 22:57:48 +0000 | [diff] [blame] | 57 | |
Rafael Espindola | 899cab6 | 2015-05-27 15:14:11 +0000 | [diff] [blame] | 58 | MCSection::iterator |
Rafael Espindola | 919ce81 | 2015-05-27 13:37:28 +0000 | [diff] [blame] | 59 | MCSection::getSubsectionInsertionPoint(unsigned Subsection) { |
Rafael Espindola | 899cab6 | 2015-05-27 15:14:11 +0000 | [diff] [blame] | 60 | if (Subsection == 0 && SubsectionFragmentMap.empty()) |
Rafael Espindola | 919ce81 | 2015-05-27 13:37:28 +0000 | [diff] [blame] | 61 | return end(); |
| 62 | |
| 63 | SmallVectorImpl<std::pair<unsigned, MCFragment *>>::iterator MI = |
Rafael Espindola | 899cab6 | 2015-05-27 15:14:11 +0000 | [diff] [blame] | 64 | std::lower_bound(SubsectionFragmentMap.begin(), |
| 65 | SubsectionFragmentMap.end(), |
Rafael Espindola | 919ce81 | 2015-05-27 13:37:28 +0000 | [diff] [blame] | 66 | std::make_pair(Subsection, (MCFragment *)nullptr)); |
| 67 | bool ExactMatch = false; |
Rafael Espindola | 899cab6 | 2015-05-27 15:14:11 +0000 | [diff] [blame] | 68 | if (MI != SubsectionFragmentMap.end()) { |
Rafael Espindola | 919ce81 | 2015-05-27 13:37:28 +0000 | [diff] [blame] | 69 | ExactMatch = MI->first == Subsection; |
| 70 | if (ExactMatch) |
| 71 | ++MI; |
| 72 | } |
Rafael Espindola | 899cab6 | 2015-05-27 15:14:11 +0000 | [diff] [blame] | 73 | iterator IP; |
| 74 | if (MI == SubsectionFragmentMap.end()) |
Rafael Espindola | 919ce81 | 2015-05-27 13:37:28 +0000 | [diff] [blame] | 75 | IP = end(); |
| 76 | else |
Duncan P. N. Exon Smith | 5d8fcb9 | 2015-10-10 00:13:11 +0000 | [diff] [blame] | 77 | IP = MI->second->getIterator(); |
Rafael Espindola | 919ce81 | 2015-05-27 13:37:28 +0000 | [diff] [blame] | 78 | 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 Espindola | 899cab6 | 2015-05-27 15:14:11 +0000 | [diff] [blame] | 82 | SubsectionFragmentMap.insert(MI, std::make_pair(Subsection, F)); |
Rafael Espindola | 919ce81 | 2015-05-27 13:37:28 +0000 | [diff] [blame] | 83 | getFragmentList().insert(IP, F); |
| 84 | F->setParent(this); |
| 85 | } |
| 86 | |
| 87 | return IP; |
| 88 | } |
| 89 | |
Aaron Ballman | 1d03d38 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 90 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Sam Clegg | b209206 | 2017-06-21 22:19:17 +0000 | [diff] [blame] | 91 | LLVM_DUMP_METHOD void MCSection::dump() const { |
Eugene Zelenko | 498b48e | 2017-02-08 22:23:19 +0000 | [diff] [blame] | 92 | raw_ostream &OS = errs(); |
Rafael Espindola | e86bc46 | 2015-05-25 23:14:17 +0000 | [diff] [blame] | 93 | |
Rafael Espindola | 899cab6 | 2015-05-27 15:14:11 +0000 | [diff] [blame] | 94 | 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 Espindola | e86bc46 | 2015-05-25 23:14:17 +0000 | [diff] [blame] | 102 | } |
Matthias Braun | 88d2075 | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 103 | #endif |