aboutsummaryrefslogtreecommitdiffstats
path: root/lib/MC/MCSection.cpp
blob: a1f56648597dd29f72d81157a8aa7ef0228d2f94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//===- lib/MC/MCSection.cpp - Machine Code Section Representation ---------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "llvm/MC/MCSection.h"
#include "llvm/MC/MCContext.h"
#include "llvm/Target/TargetAsmInfo.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;

//===----------------------------------------------------------------------===//
// MCSection
//===----------------------------------------------------------------------===//

MCSection::~MCSection() {
}

MCSection::MCSection(const StringRef &N, bool isDirective, SectionKind K, 
                     MCContext &Ctx)
  : Name(N), IsDirective(isDirective), Kind(K) {
  MCSection *&Entry = Ctx.Sections[Name];
  assert(Entry == 0 && "Multiple sections with the same name created");
  Entry = this;
}

//===----------------------------------------------------------------------===//
// MCSectionELF
//===----------------------------------------------------------------------===//

MCSectionELF *MCSectionELF::
Create(const StringRef &Name, bool IsDirective, SectionKind K, MCContext &Ctx) {
  return new (Ctx) MCSectionELF(Name, IsDirective, K, Ctx);
}

void MCSectionELF::PrintSwitchToSection(const TargetAsmInfo &TAI,
                                        raw_ostream &OS) const {
  if (isDirective()) {
    OS << getName() << '\n';
    return;
  }

  OS << "\t.section\t" << getName();
  
  // Handle the weird solaris syntax if desired.
  if (TAI.usesSunStyleELFSectionSwitchSyntax() &&
      !getKind().isMergeableConst() && !getKind().isMergeableCString()) {
    if (!getKind().isMetadata())
      OS << ",#alloc";
    if (getKind().isText())
      OS << ",#execinstr";
    if (getKind().isWriteable())
      OS << ",#write";
    if (getKind().isThreadLocal())
      OS << ",#tls";
  } else {
    OS << ",\"";
  
    if (!getKind().isMetadata())
      OS << 'a';
    if (getKind().isText())
      OS << 'x';
    if (getKind().isWriteable())
      OS << 'w';
    if (getKind().isMergeable1ByteCString() ||
        getKind().isMergeable2ByteCString() ||
        getKind().isMergeable4ByteCString() ||
        getKind().isMergeableConst4() ||
        getKind().isMergeableConst8() ||
        getKind().isMergeableConst16())
      OS << 'M';
    if (getKind().isMergeable1ByteCString() ||
        getKind().isMergeable2ByteCString() ||
        getKind().isMergeable4ByteCString())
      OS << 'S';
    if (getKind().isThreadLocal())
      OS << 'T';
    
    OS << "\",";
    
    // If comment string is '@', e.g. as on ARM - use '%' instead
    if (TAI.getCommentString()[0] == '@')
      OS << '%';
    else
      OS << '@';
    
    if (getKind().isBSS() || getKind().isThreadBSS())
      OS << "nobits";
    else
      OS << "progbits";
    
    if (getKind().isMergeable1ByteCString()) {
      OS << ",1";
    } else if (getKind().isMergeable2ByteCString()) {
      OS << ",2";
    } else if (getKind().isMergeable4ByteCString()) {
      OS << ",4";
    } else if (getKind().isMergeableConst4()) {
      OS << ",4";
    } else if (getKind().isMergeableConst8()) {
      OS << ",8";
    } else if (getKind().isMergeableConst16()) {
      OS << ",16";
    }
  }
}

//===----------------------------------------------------------------------===//
// MCSectionMachO
//===----------------------------------------------------------------------===//

MCSectionMachO *MCSectionMachO::
Create(const StringRef &Name, bool IsDirective, SectionKind K, MCContext &Ctx) {
  return new (Ctx) MCSectionMachO(Name, IsDirective, K, Ctx);
}

void MCSectionMachO::PrintSwitchToSection(const TargetAsmInfo &TAI,
                                          raw_ostream &OS) const {
  if (!isDirective())
    OS << "\t.section\t" << getName() << '\n';
  else
    OS << getName() << '\n';
}

//===----------------------------------------------------------------------===//
// MCSectionCOFF
//===----------------------------------------------------------------------===//

MCSectionCOFF *MCSectionCOFF::
Create(const StringRef &Name, bool IsDirective, SectionKind K, MCContext &Ctx) {
  return new (Ctx) MCSectionCOFF(Name, IsDirective, K, Ctx);
}

void MCSectionCOFF::PrintSwitchToSection(const TargetAsmInfo &TAI,
                                         raw_ostream &OS) const {
  
  if (isDirective()) {
    OS << getName() << '\n';
    return;
  }
  OS << "\t.section\t" << getName() << ",\"";
  if (getKind().isText())
    OS << 'x';
  if (getKind().isWriteable())
    OS << 'w';
  OS << "\"\n";
}