Chris Lattner | c10132a | 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 | |
| 10 | #include "llvm/MC/MCSection.h" |
Rafael Espindola | 09266ba | 2015-05-25 22:57:48 +0000 | [diff] [blame] | 11 | #include "llvm/MC/MCAssembler.h" |
Chris Lattner | 7b26fce | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 12 | #include "llvm/MC/MCAsmInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCContext.h" |
Rafael Espindola | f2b408c | 2015-03-23 21:22:04 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCSymbol.h" |
Chris Lattner | 1cb9396 | 2009-08-08 22:41:53 +0000 | [diff] [blame] | 15 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | c10132a | 2009-07-31 17:02:00 +0000 | [diff] [blame] | 16 | using namespace llvm; |
| 17 | |
Chris Lattner | 1cb9396 | 2009-08-08 22:41:53 +0000 | [diff] [blame] | 18 | //===----------------------------------------------------------------------===// |
| 19 | // MCSection |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
Rafael Espindola | a554c05 | 2015-05-25 23:14:17 +0000 | [diff] [blame] | 22 | MCSection::MCSection(SectionVariant V, SectionKind K, MCSymbol *Begin) |
Rafael Espindola | a32d0e9 | 2015-05-27 15:14:11 +0000 | [diff] [blame] | 23 | : Begin(Begin), HasInstructions(false), Variant(V), Kind(K) {} |
Rafael Espindola | a554c05 | 2015-05-25 23:14:17 +0000 | [diff] [blame] | 24 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 25 | MCSymbol *MCSection::getEndSymbol(MCContext &Ctx) { |
Rafael Espindola | f2b408c | 2015-03-23 21:22:04 +0000 | [diff] [blame] | 26 | if (!End) |
| 27 | End = Ctx.createTempSymbol("sec_end", true); |
| 28 | return End; |
| 29 | } |
| 30 | |
| 31 | bool MCSection::hasEnded() const { return End && End->isInSection(); } |
| 32 | |
Chris Lattner | c10132a | 2009-07-31 17:02:00 +0000 | [diff] [blame] | 33 | MCSection::~MCSection() { |
| 34 | } |
| 35 | |
Rafael Espindola | b028cc8 | 2015-05-25 15:04:26 +0000 | [diff] [blame] | 36 | void 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 Espindola | 09266ba | 2015-05-25 22:57:48 +0000 | [diff] [blame] | 54 | |
Rafael Espindola | a32d0e9 | 2015-05-27 15:14:11 +0000 | [diff] [blame] | 55 | MCSection::iterator |
Rafael Espindola | 8c05c6e | 2015-05-27 13:37:28 +0000 | [diff] [blame] | 56 | MCSection::getSubsectionInsertionPoint(unsigned Subsection) { |
Rafael Espindola | a32d0e9 | 2015-05-27 15:14:11 +0000 | [diff] [blame] | 57 | if (Subsection == 0 && SubsectionFragmentMap.empty()) |
Rafael Espindola | 8c05c6e | 2015-05-27 13:37:28 +0000 | [diff] [blame] | 58 | return end(); |
| 59 | |
| 60 | SmallVectorImpl<std::pair<unsigned, MCFragment *>>::iterator MI = |
Rafael Espindola | a32d0e9 | 2015-05-27 15:14:11 +0000 | [diff] [blame] | 61 | std::lower_bound(SubsectionFragmentMap.begin(), |
| 62 | SubsectionFragmentMap.end(), |
Rafael Espindola | 8c05c6e | 2015-05-27 13:37:28 +0000 | [diff] [blame] | 63 | std::make_pair(Subsection, (MCFragment *)nullptr)); |
| 64 | bool ExactMatch = false; |
Rafael Espindola | a32d0e9 | 2015-05-27 15:14:11 +0000 | [diff] [blame] | 65 | if (MI != SubsectionFragmentMap.end()) { |
Rafael Espindola | 8c05c6e | 2015-05-27 13:37:28 +0000 | [diff] [blame] | 66 | ExactMatch = MI->first == Subsection; |
| 67 | if (ExactMatch) |
| 68 | ++MI; |
| 69 | } |
Rafael Espindola | a32d0e9 | 2015-05-27 15:14:11 +0000 | [diff] [blame] | 70 | iterator IP; |
| 71 | if (MI == SubsectionFragmentMap.end()) |
Rafael Espindola | 8c05c6e | 2015-05-27 13:37:28 +0000 | [diff] [blame] | 72 | 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 Espindola | a32d0e9 | 2015-05-27 15:14:11 +0000 | [diff] [blame] | 79 | SubsectionFragmentMap.insert(MI, std::make_pair(Subsection, F)); |
Rafael Espindola | 8c05c6e | 2015-05-27 13:37:28 +0000 | [diff] [blame] | 80 | getFragmentList().insert(IP, F); |
| 81 | F->setParent(this); |
| 82 | } |
| 83 | |
| 84 | return IP; |
| 85 | } |
| 86 | |
Rafael Espindola | 2e4be0a | 2015-05-27 15:18:34 +0000 | [diff] [blame^] | 87 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Rafael Espindola | a32d0e9 | 2015-05-27 15:14:11 +0000 | [diff] [blame] | 88 | void MCSection::dump() { |
| 89 | raw_ostream &OS = llvm::errs(); |
Rafael Espindola | a554c05 | 2015-05-25 23:14:17 +0000 | [diff] [blame] | 90 | |
Rafael Espindola | a32d0e9 | 2015-05-27 15:14:11 +0000 | [diff] [blame] | 91 | 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 Espindola | a554c05 | 2015-05-25 23:14:17 +0000 | [diff] [blame] | 99 | } |
Rafael Espindola | 2e4be0a | 2015-05-27 15:18:34 +0000 | [diff] [blame^] | 100 | #endif |
Rafael Espindola | a554c05 | 2015-05-25 23:14:17 +0000 | [diff] [blame] | 101 | |
Rafael Espindola | a32d0e9 | 2015-05-27 15:14:11 +0000 | [diff] [blame] | 102 | MCSection::iterator MCSection::begin() { return Fragments.begin(); } |
Rafael Espindola | 09266ba | 2015-05-25 22:57:48 +0000 | [diff] [blame] | 103 | |
Rafael Espindola | a32d0e9 | 2015-05-27 15:14:11 +0000 | [diff] [blame] | 104 | MCSection::iterator MCSection::end() { return Fragments.end(); } |
Rafael Espindola | 09266ba | 2015-05-25 22:57:48 +0000 | [diff] [blame] | 105 | |
Rafael Espindola | a32d0e9 | 2015-05-27 15:14:11 +0000 | [diff] [blame] | 106 | MCSection::reverse_iterator MCSection::rbegin() { return Fragments.rbegin(); } |
Rafael Espindola | 09266ba | 2015-05-25 22:57:48 +0000 | [diff] [blame] | 107 | |
Rafael Espindola | a32d0e9 | 2015-05-27 15:14:11 +0000 | [diff] [blame] | 108 | MCSection::reverse_iterator MCSection::rend() { return Fragments.rend(); } |