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 | |
| 10 | #include "llvm/MC/MCSection.h" |
| 11 | #include "llvm/MC/MCContext.h" |
Chris Lattner | 892e182 | 2009-08-08 22:41:53 +0000 | [diff] [blame^] | 12 | #include "llvm/Target/TargetAsmInfo.h" |
| 13 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | ed47a04 | 2009-07-31 17:02:00 +0000 | [diff] [blame] | 14 | using namespace llvm; |
| 15 | |
Chris Lattner | 892e182 | 2009-08-08 22:41:53 +0000 | [diff] [blame^] | 16 | //===----------------------------------------------------------------------===// |
| 17 | // MCSection |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | |
Chris Lattner | ed47a04 | 2009-07-31 17:02:00 +0000 | [diff] [blame] | 20 | MCSection::~MCSection() { |
| 21 | } |
| 22 | |
Chris Lattner | 968ff11 | 2009-08-01 21:11:14 +0000 | [diff] [blame] | 23 | MCSection::MCSection(const StringRef &N, bool isDirective, SectionKind K, |
| 24 | MCContext &Ctx) |
| 25 | : Name(N), IsDirective(isDirective), Kind(K) { |
Chris Lattner | ed47a04 | 2009-07-31 17:02:00 +0000 | [diff] [blame] | 26 | MCSection *&Entry = Ctx.Sections[Name]; |
| 27 | assert(Entry == 0 && "Multiple sections with the same name created"); |
| 28 | Entry = this; |
| 29 | } |
| 30 | |
Chris Lattner | 892e182 | 2009-08-08 22:41:53 +0000 | [diff] [blame^] | 31 | //===----------------------------------------------------------------------===// |
| 32 | // MCSectionELF |
| 33 | //===----------------------------------------------------------------------===// |
Chris Lattner | a1c31b7 | 2009-08-08 20:50:49 +0000 | [diff] [blame] | 34 | |
| 35 | MCSectionELF *MCSectionELF:: |
| 36 | Create(const StringRef &Name, bool IsDirective, SectionKind K, MCContext &Ctx) { |
| 37 | return new (Ctx) MCSectionELF(Name, IsDirective, K, Ctx); |
| 38 | } |
| 39 | |
Chris Lattner | 892e182 | 2009-08-08 22:41:53 +0000 | [diff] [blame^] | 40 | void MCSectionELF::PrintSwitchToSection(const TargetAsmInfo &TAI, |
| 41 | raw_ostream &OS) const { |
| 42 | if (isDirective()) { |
| 43 | OS << getName() << '\n'; |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | OS << "\t.section\t" << getName(); |
| 48 | |
| 49 | // Handle the weird solaris syntax if desired. |
| 50 | if (TAI.usesSunStyleELFSectionSwitchSyntax() && |
| 51 | !getKind().isMergeableConst() && !getKind().isMergeableCString()) { |
| 52 | if (!getKind().isMetadata()) |
| 53 | OS << ",#alloc"; |
| 54 | if (getKind().isText()) |
| 55 | OS << ",#execinstr"; |
| 56 | if (getKind().isWriteable()) |
| 57 | OS << ",#write"; |
| 58 | if (getKind().isThreadLocal()) |
| 59 | OS << ",#tls"; |
| 60 | } else { |
| 61 | OS << ",\""; |
| 62 | |
| 63 | if (!getKind().isMetadata()) |
| 64 | OS << 'a'; |
| 65 | if (getKind().isText()) |
| 66 | OS << 'x'; |
| 67 | if (getKind().isWriteable()) |
| 68 | OS << 'w'; |
| 69 | if (getKind().isMergeable1ByteCString() || |
| 70 | getKind().isMergeable2ByteCString() || |
| 71 | getKind().isMergeable4ByteCString() || |
| 72 | getKind().isMergeableConst4() || |
| 73 | getKind().isMergeableConst8() || |
| 74 | getKind().isMergeableConst16()) |
| 75 | OS << 'M'; |
| 76 | if (getKind().isMergeable1ByteCString() || |
| 77 | getKind().isMergeable2ByteCString() || |
| 78 | getKind().isMergeable4ByteCString()) |
| 79 | OS << 'S'; |
| 80 | if (getKind().isThreadLocal()) |
| 81 | OS << 'T'; |
| 82 | |
| 83 | OS << "\","; |
| 84 | |
| 85 | // If comment string is '@', e.g. as on ARM - use '%' instead |
| 86 | if (TAI.getCommentString()[0] == '@') |
| 87 | OS << '%'; |
| 88 | else |
| 89 | OS << '@'; |
| 90 | |
| 91 | if (getKind().isBSS() || getKind().isThreadBSS()) |
| 92 | OS << "nobits"; |
| 93 | else |
| 94 | OS << "progbits"; |
| 95 | |
| 96 | if (getKind().isMergeable1ByteCString()) { |
| 97 | OS << ",1"; |
| 98 | } else if (getKind().isMergeable2ByteCString()) { |
| 99 | OS << ",2"; |
| 100 | } else if (getKind().isMergeable4ByteCString()) { |
| 101 | OS << ",4"; |
| 102 | } else if (getKind().isMergeableConst4()) { |
| 103 | OS << ",4"; |
| 104 | } else if (getKind().isMergeableConst8()) { |
| 105 | OS << ",8"; |
| 106 | } else if (getKind().isMergeableConst16()) { |
| 107 | OS << ",16"; |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | //===----------------------------------------------------------------------===// |
| 113 | // MCSectionMachO |
| 114 | //===----------------------------------------------------------------------===// |
Chris Lattner | a1c31b7 | 2009-08-08 20:50:49 +0000 | [diff] [blame] | 115 | |
| 116 | MCSectionMachO *MCSectionMachO:: |
| 117 | Create(const StringRef &Name, bool IsDirective, SectionKind K, MCContext &Ctx) { |
| 118 | return new (Ctx) MCSectionMachO(Name, IsDirective, K, Ctx); |
| 119 | } |
| 120 | |
Chris Lattner | 892e182 | 2009-08-08 22:41:53 +0000 | [diff] [blame^] | 121 | void MCSectionMachO::PrintSwitchToSection(const TargetAsmInfo &TAI, |
| 122 | raw_ostream &OS) const { |
| 123 | if (!isDirective()) |
| 124 | OS << "\t.section\t" << getName() << '\n'; |
| 125 | else |
| 126 | OS << getName() << '\n'; |
| 127 | } |
| 128 | |
| 129 | //===----------------------------------------------------------------------===// |
| 130 | // MCSectionCOFF |
| 131 | //===----------------------------------------------------------------------===// |
Chris Lattner | a1c31b7 | 2009-08-08 20:50:49 +0000 | [diff] [blame] | 132 | |
Chris Lattner | 7c599d0 | 2009-08-08 20:52:13 +0000 | [diff] [blame] | 133 | MCSectionCOFF *MCSectionCOFF:: |
Chris Lattner | a1c31b7 | 2009-08-08 20:50:49 +0000 | [diff] [blame] | 134 | Create(const StringRef &Name, bool IsDirective, SectionKind K, MCContext &Ctx) { |
Chris Lattner | 7c599d0 | 2009-08-08 20:52:13 +0000 | [diff] [blame] | 135 | return new (Ctx) MCSectionCOFF(Name, IsDirective, K, Ctx); |
Chris Lattner | a1c31b7 | 2009-08-08 20:50:49 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Chris Lattner | 892e182 | 2009-08-08 22:41:53 +0000 | [diff] [blame^] | 138 | void MCSectionCOFF::PrintSwitchToSection(const TargetAsmInfo &TAI, |
| 139 | raw_ostream &OS) const { |
| 140 | |
| 141 | if (isDirective()) { |
| 142 | OS << getName() << '\n'; |
| 143 | return; |
| 144 | } |
| 145 | OS << "\t.section\t" << getName() << ",\""; |
| 146 | if (getKind().isText()) |
| 147 | OS << 'x'; |
| 148 | if (getKind().isWriteable()) |
| 149 | OS << 'w'; |
| 150 | OS << "\"\n"; |
| 151 | } |