8b7fcd2815c251a0dc31f2726ab9ea41d7b2280e
[oota-llvm.git] / lib / MC / MCSection.cpp
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"
12 #include "llvm/Target/TargetAsmInfo.h"
13 #include "llvm/Support/raw_ostream.h"
14 using namespace llvm;
15
16 //===----------------------------------------------------------------------===//
17 // MCSection
18 //===----------------------------------------------------------------------===//
19
20 MCSection::~MCSection() {
21 }
22
23
24 //===----------------------------------------------------------------------===//
25 // MCSectionELF
26 //===----------------------------------------------------------------------===//
27
28 MCSectionELF *MCSectionELF::
29 Create(const StringRef &Name, bool IsDirective, SectionKind K, MCContext &Ctx) {
30   return new (Ctx) MCSectionELF(Name, IsDirective, K);
31 }
32
33 void MCSectionELF::PrintSwitchToSection(const TargetAsmInfo &TAI,
34                                         raw_ostream &OS) const {
35   if (isDirective()) {
36     OS << getName() << '\n';
37     return;
38   }
39
40   OS << "\t.section\t" << getName();
41   
42   // Handle the weird solaris syntax if desired.
43   if (TAI.usesSunStyleELFSectionSwitchSyntax() &&
44       !getKind().isMergeableConst() && !getKind().isMergeableCString()) {
45     if (!getKind().isMetadata())
46       OS << ",#alloc";
47     if (getKind().isText())
48       OS << ",#execinstr";
49     if (getKind().isWriteable())
50       OS << ",#write";
51     if (getKind().isThreadLocal())
52       OS << ",#tls";
53   } else {
54     OS << ",\"";
55   
56     if (!getKind().isMetadata())
57       OS << 'a';
58     if (getKind().isText())
59       OS << 'x';
60     if (getKind().isWriteable())
61       OS << 'w';
62     if (getKind().isMergeable1ByteCString() ||
63         getKind().isMergeable2ByteCString() ||
64         getKind().isMergeable4ByteCString() ||
65         getKind().isMergeableConst4() ||
66         getKind().isMergeableConst8() ||
67         getKind().isMergeableConst16())
68       OS << 'M';
69     if (getKind().isMergeable1ByteCString() ||
70         getKind().isMergeable2ByteCString() ||
71         getKind().isMergeable4ByteCString())
72       OS << 'S';
73     if (getKind().isThreadLocal())
74       OS << 'T';
75     
76     OS << "\",";
77     
78     // If comment string is '@', e.g. as on ARM - use '%' instead
79     if (TAI.getCommentString()[0] == '@')
80       OS << '%';
81     else
82       OS << '@';
83     
84     if (getKind().isBSS() || getKind().isThreadBSS())
85       OS << "nobits";
86     else
87       OS << "progbits";
88     
89     if (getKind().isMergeable1ByteCString()) {
90       OS << ",1";
91     } else if (getKind().isMergeable2ByteCString()) {
92       OS << ",2";
93     } else if (getKind().isMergeable4ByteCString()) {
94       OS << ",4";
95     } else if (getKind().isMergeableConst4()) {
96       OS << ",4";
97     } else if (getKind().isMergeableConst8()) {
98       OS << ",8";
99     } else if (getKind().isMergeableConst16()) {
100       OS << ",16";
101     }
102   }
103   
104   OS << '\n';
105 }
106
107
108 //===----------------------------------------------------------------------===//
109 // MCSectionCOFF
110 //===----------------------------------------------------------------------===//
111
112 MCSectionCOFF *MCSectionCOFF::
113 Create(const StringRef &Name, bool IsDirective, SectionKind K, MCContext &Ctx) {
114   return new (Ctx) MCSectionCOFF(Name, IsDirective, K);
115 }
116
117 void MCSectionCOFF::PrintSwitchToSection(const TargetAsmInfo &TAI,
118                                          raw_ostream &OS) const {
119   
120   if (isDirective()) {
121     OS << getName() << '\n';
122     return;
123   }
124   OS << "\t.section\t" << getName() << ",\"";
125   if (getKind().isText())
126     OS << 'x';
127   if (getKind().isWriteable())
128     OS << 'w';
129   OS << "\"\n";
130 }