Skip to content

Commit 15b866f

Browse files
MaskRaymahesh-attarde
authored andcommitted
MC: Merge MCSection*.cpp into MCAsmInfo*.cpp
To centralize assembly-related functions to MCAsmInfo and move toward making MCSection non-virtual. MCSection*.cpp files primarily define printSwitchToSection, which is tighly related to MCAsmInfo.
1 parent 798b530 commit 15b866f

11 files changed

+642
-707
lines changed

llvm/lib/MC/CMakeLists.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,7 @@ add_llvm_component_library(LLVMMC
4343
MCRegisterInfo.cpp
4444
MCSchedule.cpp
4545
MCSection.cpp
46-
MCSectionCOFF.cpp
47-
MCSectionELF.cpp
48-
MCSectionGOFF.cpp
4946
MCSectionMachO.cpp
50-
MCSectionWasm.cpp
51-
MCSectionXCOFF.cpp
5247
MCStreamer.cpp
5348
MCSPIRVStreamer.cpp
5449
MCSubtargetInfo.cpp

llvm/lib/MC/MCAsmInfoCOFF.cpp

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@
1212
//===----------------------------------------------------------------------===//
1313

1414
#include "llvm/MC/MCAsmInfoCOFF.h"
15+
#include "llvm/BinaryFormat/COFF.h"
1516
#include "llvm/MC/MCDirectives.h"
1617
#include "llvm/MC/MCSection.h"
18+
#include "llvm/MC/MCSectionCOFF.h"
19+
#include "llvm/MC/MCSymbol.h"
20+
#include "llvm/Support/raw_ostream.h"
21+
#include <cassert>
1722

1823
using namespace llvm;
1924

@@ -69,3 +74,103 @@ MCAsmInfoGNUCOFF::MCAsmInfoGNUCOFF() {
6974
// We don't create constants in comdat sections for MinGW.
7075
HasCOFFComdatConstants = false;
7176
}
77+
78+
// shouldOmitSectionDirective - Decides whether a '.section' directive
79+
// should be printed before the section name
80+
bool MCSectionCOFF::shouldOmitSectionDirective(StringRef Name,
81+
const MCAsmInfo &MAI) const {
82+
if (COMDATSymbol || isUnique())
83+
return false;
84+
85+
// FIXME: Does .section .bss/.data/.text work everywhere??
86+
if (Name == ".text" || Name == ".data" || Name == ".bss")
87+
return true;
88+
89+
return false;
90+
}
91+
92+
void MCSectionCOFF::setSelection(int Selection) const {
93+
assert(Selection != 0 && "invalid COMDAT selection type");
94+
this->Selection = Selection;
95+
Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
96+
}
97+
98+
void MCSectionCOFF::printSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
99+
raw_ostream &OS,
100+
uint32_t Subsection) const {
101+
// standard sections don't require the '.section'
102+
if (shouldOmitSectionDirective(getName(), MAI)) {
103+
OS << '\t' << getName() << '\n';
104+
return;
105+
}
106+
107+
OS << "\t.section\t" << getName() << ",\"";
108+
if (getCharacteristics() & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
109+
OS << 'd';
110+
if (getCharacteristics() & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
111+
OS << 'b';
112+
if (getCharacteristics() & COFF::IMAGE_SCN_MEM_EXECUTE)
113+
OS << 'x';
114+
if (getCharacteristics() & COFF::IMAGE_SCN_MEM_WRITE)
115+
OS << 'w';
116+
else if (getCharacteristics() & COFF::IMAGE_SCN_MEM_READ)
117+
OS << 'r';
118+
else
119+
OS << 'y';
120+
if (getCharacteristics() & COFF::IMAGE_SCN_LNK_REMOVE)
121+
OS << 'n';
122+
if (getCharacteristics() & COFF::IMAGE_SCN_MEM_SHARED)
123+
OS << 's';
124+
if ((getCharacteristics() & COFF::IMAGE_SCN_MEM_DISCARDABLE) &&
125+
!isImplicitlyDiscardable(getName()))
126+
OS << 'D';
127+
if (getCharacteristics() & COFF::IMAGE_SCN_LNK_INFO)
128+
OS << 'i';
129+
OS << '"';
130+
131+
// unique should be tail of .section directive.
132+
if (isUnique() && !COMDATSymbol)
133+
OS << ",unique," << UniqueID;
134+
135+
if (getCharacteristics() & COFF::IMAGE_SCN_LNK_COMDAT) {
136+
if (COMDATSymbol)
137+
OS << ",";
138+
else
139+
OS << "\n\t.linkonce\t";
140+
switch (Selection) {
141+
case COFF::IMAGE_COMDAT_SELECT_NODUPLICATES:
142+
OS << "one_only";
143+
break;
144+
case COFF::IMAGE_COMDAT_SELECT_ANY:
145+
OS << "discard";
146+
break;
147+
case COFF::IMAGE_COMDAT_SELECT_SAME_SIZE:
148+
OS << "same_size";
149+
break;
150+
case COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH:
151+
OS << "same_contents";
152+
break;
153+
case COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE:
154+
OS << "associative";
155+
break;
156+
case COFF::IMAGE_COMDAT_SELECT_LARGEST:
157+
OS << "largest";
158+
break;
159+
case COFF::IMAGE_COMDAT_SELECT_NEWEST:
160+
OS << "newest";
161+
break;
162+
default:
163+
assert(false && "unsupported COFF selection type");
164+
break;
165+
}
166+
if (COMDATSymbol) {
167+
OS << ",";
168+
COMDATSymbol->print(OS, &MAI);
169+
}
170+
}
171+
172+
if (isUnique() && COMDATSymbol)
173+
OS << ",unique," << UniqueID;
174+
175+
OS << '\n';
176+
}

llvm/lib/MC/MCAsmInfoELF.cpp

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,16 @@
1212
//===----------------------------------------------------------------------===//
1313

1414
#include "llvm/MC/MCAsmInfoELF.h"
15+
#include "llvm/ADT/Twine.h"
1516
#include "llvm/BinaryFormat/ELF.h"
17+
#include "llvm/MC/MCAsmInfo.h"
1618
#include "llvm/MC/MCContext.h"
19+
#include "llvm/MC/MCExpr.h"
1720
#include "llvm/MC/MCSectionELF.h"
21+
#include "llvm/Support/ErrorHandling.h"
22+
#include "llvm/Support/raw_ostream.h"
23+
#include "llvm/TargetParser/Triple.h"
24+
#include <cassert>
1825

1926
using namespace llvm;
2027

@@ -38,3 +45,196 @@ MCAsmInfoELF::MCAsmInfoELF() {
3845
PrivateGlobalPrefix = ".L";
3946
PrivateLabelPrefix = ".L";
4047
}
48+
49+
// Decides whether a '.section' directive
50+
// should be printed before the section name.
51+
bool MCSectionELF::shouldOmitSectionDirective(StringRef Name,
52+
const MCAsmInfo &MAI) const {
53+
if (isUnique())
54+
return false;
55+
56+
return MAI.shouldOmitSectionDirective(Name);
57+
}
58+
59+
static void printName(raw_ostream &OS, StringRef Name) {
60+
if (Name.find_first_not_of("0123456789_."
61+
"abcdefghijklmnopqrstuvwxyz"
62+
"ABCDEFGHIJKLMNOPQRSTUVWXYZ") == Name.npos) {
63+
OS << Name;
64+
return;
65+
}
66+
OS << '"';
67+
for (const char *B = Name.begin(), *E = Name.end(); B < E; ++B) {
68+
if (*B == '"') // Unquoted "
69+
OS << "\\\"";
70+
else if (*B != '\\') // Neither " or backslash
71+
OS << *B;
72+
else if (B + 1 == E) // Trailing backslash
73+
OS << "\\\\";
74+
else {
75+
OS << B[0] << B[1]; // Quoted character
76+
++B;
77+
}
78+
}
79+
OS << '"';
80+
}
81+
82+
void MCSectionELF::printSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
83+
raw_ostream &OS,
84+
uint32_t Subsection) const {
85+
if (shouldOmitSectionDirective(getName(), MAI)) {
86+
OS << '\t' << getName();
87+
if (Subsection)
88+
OS << '\t' << Subsection;
89+
OS << '\n';
90+
return;
91+
}
92+
93+
OS << "\t.section\t";
94+
printName(OS, getName());
95+
96+
// Handle the weird solaris syntax if desired.
97+
if (MAI.usesSunStyleELFSectionSwitchSyntax() && !(Flags & ELF::SHF_MERGE)) {
98+
if (Flags & ELF::SHF_ALLOC)
99+
OS << ",#alloc";
100+
if (Flags & ELF::SHF_EXECINSTR)
101+
OS << ",#execinstr";
102+
if (Flags & ELF::SHF_WRITE)
103+
OS << ",#write";
104+
if (Flags & ELF::SHF_EXCLUDE)
105+
OS << ",#exclude";
106+
if (Flags & ELF::SHF_TLS)
107+
OS << ",#tls";
108+
OS << '\n';
109+
return;
110+
}
111+
112+
OS << ",\"";
113+
if (Flags & ELF::SHF_ALLOC)
114+
OS << 'a';
115+
if (Flags & ELF::SHF_EXCLUDE)
116+
OS << 'e';
117+
if (Flags & ELF::SHF_EXECINSTR)
118+
OS << 'x';
119+
if (Flags & ELF::SHF_WRITE)
120+
OS << 'w';
121+
if (Flags & ELF::SHF_MERGE)
122+
OS << 'M';
123+
if (Flags & ELF::SHF_STRINGS)
124+
OS << 'S';
125+
if (Flags & ELF::SHF_TLS)
126+
OS << 'T';
127+
if (Flags & ELF::SHF_LINK_ORDER)
128+
OS << 'o';
129+
if (Flags & ELF::SHF_GROUP)
130+
OS << 'G';
131+
if (Flags & ELF::SHF_GNU_RETAIN)
132+
OS << 'R';
133+
134+
// If there are os-specific flags, print them.
135+
if (T.isOSSolaris())
136+
if (Flags & ELF::SHF_SUNW_NODISCARD)
137+
OS << 'R';
138+
139+
// If there are target-specific flags, print them.
140+
Triple::ArchType Arch = T.getArch();
141+
if (Arch == Triple::xcore) {
142+
if (Flags & ELF::XCORE_SHF_CP_SECTION)
143+
OS << 'c';
144+
if (Flags & ELF::XCORE_SHF_DP_SECTION)
145+
OS << 'd';
146+
} else if (T.isARM() || T.isThumb()) {
147+
if (Flags & ELF::SHF_ARM_PURECODE)
148+
OS << 'y';
149+
} else if (T.isAArch64()) {
150+
if (Flags & ELF::SHF_AARCH64_PURECODE)
151+
OS << 'y';
152+
} else if (Arch == Triple::hexagon) {
153+
if (Flags & ELF::SHF_HEX_GPREL)
154+
OS << 's';
155+
} else if (Arch == Triple::x86_64) {
156+
if (Flags & ELF::SHF_X86_64_LARGE)
157+
OS << 'l';
158+
}
159+
160+
OS << '"';
161+
162+
OS << ',';
163+
164+
// If comment string is '@', e.g. as on ARM - use '%' instead
165+
if (MAI.getCommentString()[0] == '@')
166+
OS << '%';
167+
else
168+
OS << '@';
169+
170+
if (Type == ELF::SHT_INIT_ARRAY)
171+
OS << "init_array";
172+
else if (Type == ELF::SHT_FINI_ARRAY)
173+
OS << "fini_array";
174+
else if (Type == ELF::SHT_PREINIT_ARRAY)
175+
OS << "preinit_array";
176+
else if (Type == ELF::SHT_NOBITS)
177+
OS << "nobits";
178+
else if (Type == ELF::SHT_NOTE)
179+
OS << "note";
180+
else if (Type == ELF::SHT_PROGBITS)
181+
OS << "progbits";
182+
else if (Type == ELF::SHT_X86_64_UNWIND)
183+
OS << "unwind";
184+
else if (Type == ELF::SHT_MIPS_DWARF)
185+
// Print hex value of the flag while we do not have
186+
// any standard symbolic representation of the flag.
187+
OS << "0x7000001e";
188+
else if (Type == ELF::SHT_LLVM_ODRTAB)
189+
OS << "llvm_odrtab";
190+
else if (Type == ELF::SHT_LLVM_LINKER_OPTIONS)
191+
OS << "llvm_linker_options";
192+
else if (Type == ELF::SHT_LLVM_CALL_GRAPH_PROFILE)
193+
OS << "llvm_call_graph_profile";
194+
else if (Type == ELF::SHT_LLVM_DEPENDENT_LIBRARIES)
195+
OS << "llvm_dependent_libraries";
196+
else if (Type == ELF::SHT_LLVM_SYMPART)
197+
OS << "llvm_sympart";
198+
else if (Type == ELF::SHT_LLVM_BB_ADDR_MAP)
199+
OS << "llvm_bb_addr_map";
200+
else if (Type == ELF::SHT_LLVM_OFFLOADING)
201+
OS << "llvm_offloading";
202+
else if (Type == ELF::SHT_LLVM_LTO)
203+
OS << "llvm_lto";
204+
else if (Type == ELF::SHT_LLVM_JT_SIZES)
205+
OS << "llvm_jt_sizes";
206+
else if (Type == ELF::SHT_LLVM_CFI_JUMP_TABLE)
207+
OS << "llvm_cfi_jump_table";
208+
else
209+
OS << "0x" << Twine::utohexstr(Type);
210+
211+
if (EntrySize) {
212+
assert((Flags & ELF::SHF_MERGE) || Type == ELF::SHT_LLVM_CFI_JUMP_TABLE);
213+
OS << "," << EntrySize;
214+
}
215+
216+
if (Flags & ELF::SHF_LINK_ORDER) {
217+
OS << ",";
218+
if (LinkedToSym)
219+
printName(OS, LinkedToSym->getName());
220+
else
221+
OS << '0';
222+
}
223+
224+
if (Flags & ELF::SHF_GROUP) {
225+
OS << ",";
226+
printName(OS, Group.getPointer()->getName());
227+
if (isComdat())
228+
OS << ",comdat";
229+
}
230+
231+
if (isUnique())
232+
OS << ",unique," << UniqueID;
233+
234+
OS << '\n';
235+
236+
if (Subsection) {
237+
OS << "\t.subsection\t" << Subsection;
238+
OS << '\n';
239+
}
240+
}

0 commit comments

Comments
 (0)