blob: 65e86d3b408834b51e9cb1bf49af02ffeee00958 [file] [log] [blame]
Chris Lattnered47a042009-07-31 17:02:00 +00001//===- 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 Lattner892e1822009-08-08 22:41:53 +000012#include "llvm/Target/TargetAsmInfo.h"
13#include "llvm/Support/raw_ostream.h"
Chris Lattnered47a042009-07-31 17:02:00 +000014using namespace llvm;
15
Chris Lattner892e1822009-08-08 22:41:53 +000016//===----------------------------------------------------------------------===//
17// MCSection
18//===----------------------------------------------------------------------===//
19
Chris Lattnered47a042009-07-31 17:02:00 +000020MCSection::~MCSection() {
21}
22
Chris Lattnered47a042009-07-31 17:02:00 +000023
Chris Lattner892e1822009-08-08 22:41:53 +000024//===----------------------------------------------------------------------===//
25// MCSectionELF
26//===----------------------------------------------------------------------===//
Chris Lattnera1c31b72009-08-08 20:50:49 +000027
28MCSectionELF *MCSectionELF::
29Create(const StringRef &Name, bool IsDirective, SectionKind K, MCContext &Ctx) {
30 return new (Ctx) MCSectionELF(Name, IsDirective, K, Ctx);
31}
32
Chris Lattner93b6db32009-08-08 23:39:42 +000033MCSectionELF::MCSectionELF(const StringRef &name, bool isDirective,
34 SectionKind K, MCContext &Ctx)
35 : MCSection(K), Name(name), IsDirective(isDirective) {
36 Ctx.SetSection(Name, this);
37}
38
39
Chris Lattner892e1822009-08-08 22:41:53 +000040void 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 }
Chris Lattner1f8e8db2009-08-09 15:31:10 +0000110
111 OS << '\n';
Chris Lattner892e1822009-08-08 22:41:53 +0000112}
113
114//===----------------------------------------------------------------------===//
115// MCSectionMachO
116//===----------------------------------------------------------------------===//
Chris Lattnera1c31b72009-08-08 20:50:49 +0000117
118MCSectionMachO *MCSectionMachO::
119Create(const StringRef &Name, bool IsDirective, SectionKind K, MCContext &Ctx) {
120 return new (Ctx) MCSectionMachO(Name, IsDirective, K, Ctx);
121}
122
Chris Lattner93b6db32009-08-08 23:39:42 +0000123MCSectionMachO::MCSectionMachO(const StringRef &name, bool isDirective,
124 SectionKind K, MCContext &Ctx)
125 : MCSection(K), Name(name), IsDirective(isDirective) {
126 Ctx.SetSection(Name, this);
127}
128
Chris Lattner892e1822009-08-08 22:41:53 +0000129void MCSectionMachO::PrintSwitchToSection(const TargetAsmInfo &TAI,
130 raw_ostream &OS) const {
131 if (!isDirective())
132 OS << "\t.section\t" << getName() << '\n';
133 else
134 OS << getName() << '\n';
135}
136
137//===----------------------------------------------------------------------===//
138// MCSectionCOFF
139//===----------------------------------------------------------------------===//
Chris Lattnera1c31b72009-08-08 20:50:49 +0000140
Chris Lattner7c599d02009-08-08 20:52:13 +0000141MCSectionCOFF *MCSectionCOFF::
Chris Lattnera1c31b72009-08-08 20:50:49 +0000142Create(const StringRef &Name, bool IsDirective, SectionKind K, MCContext &Ctx) {
Chris Lattner7c599d02009-08-08 20:52:13 +0000143 return new (Ctx) MCSectionCOFF(Name, IsDirective, K, Ctx);
Chris Lattnera1c31b72009-08-08 20:50:49 +0000144}
145
Chris Lattner93b6db32009-08-08 23:39:42 +0000146MCSectionCOFF::MCSectionCOFF(const StringRef &name, bool isDirective,
147 SectionKind K, MCContext &Ctx)
148 : MCSection(K), Name(name), IsDirective(isDirective) {
149 Ctx.SetSection(Name, this);
150}
151
152
Chris Lattner892e1822009-08-08 22:41:53 +0000153void MCSectionCOFF::PrintSwitchToSection(const TargetAsmInfo &TAI,
154 raw_ostream &OS) const {
155
156 if (isDirective()) {
157 OS << getName() << '\n';
158 return;
159 }
160 OS << "\t.section\t" << getName() << ",\"";
161 if (getKind().isText())
162 OS << 'x';
163 if (getKind().isWriteable())
164 OS << 'w';
165 OS << "\"\n";
166}