1 //===- lib/MC/MCSectionCOFF.cpp - COFF Code Section Representation --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/MC/MCSectionCOFF.h"
11 #include "llvm/MC/MCAsmInfo.h"
12 #include "llvm/MC/MCContext.h"
13 #include "llvm/MC/MCSymbol.h"
14 #include "llvm/Support/raw_ostream.h"
17 MCSectionCOFF::~MCSectionCOFF() {} // anchor.
19 // ShouldOmitSectionDirective - Decides whether a '.section' directive
20 // should be printed before the section name
21 bool MCSectionCOFF::ShouldOmitSectionDirective(StringRef Name,
22 const MCAsmInfo &MAI) const {
24 // FIXME: Does .section .bss/.data/.text work everywhere??
25 if (Name == ".text" || Name == ".data" || Name == ".bss")
31 void MCSectionCOFF::setSelection(int Selection,
32 const MCSectionCOFF *Assoc) const {
33 assert(Selection != 0 && "invalid COMDAT selection type");
34 assert((Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) ==
36 "associative COMDAT section must have an associated section");
37 this->Selection = Selection;
39 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
42 static bool isAcceptableSectionNameChar(char C) {
43 return (C >= 'a' && C <= 'z') ||
44 (C >= 'A' && C <= 'Z') ||
45 (C >= '0' && C <= '9') ||
46 C == '_' || C == '$' || C == '.';
49 /// NameNeedsQuoting - Return true if the identifier \p Str needs quotes to be
50 /// syntactically correct.
51 static bool sectionNameNeedsQuoting(StringRef Name) {
52 for (unsigned i = 0, e = Name.size(); i != e; ++i)
53 if (!isAcceptableSectionNameChar(Name[i]))
58 void MCSectionCOFF::PrintSwitchToSection(const MCAsmInfo &MAI,
60 const MCExpr *Subsection) const {
62 // standard sections don't require the '.section'
63 if (ShouldOmitSectionDirective(SectionName, MAI)) {
64 OS << '\t' << getSectionName() << '\n';
68 if (sectionNameNeedsQuoting(getSectionName()))
69 OS << "\t.section\t" << '"' << getSectionName() << '"' << ",\"";
71 OS << "\t.section\t" << getSectionName() << ",\"";
72 if (getKind().isText())
74 if (getKind().isWriteable())
78 if (getCharacteristics() & COFF::IMAGE_SCN_MEM_DISCARDABLE)
82 if (getCharacteristics() & COFF::IMAGE_SCN_LNK_COMDAT) {
84 case COFF::IMAGE_COMDAT_SELECT_NODUPLICATES:
85 OS << "\t.linkonce one_only\n";
87 case COFF::IMAGE_COMDAT_SELECT_ANY:
88 OS << "\t.linkonce discard\n";
90 case COFF::IMAGE_COMDAT_SELECT_SAME_SIZE:
91 OS << "\t.linkonce same_size\n";
93 case COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH:
94 OS << "\t.linkonce same_contents\n";
96 case COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE:
97 OS << "\t.linkonce associative " << Assoc->getSectionName() << "\n";
99 case COFF::IMAGE_COMDAT_SELECT_LARGEST:
100 OS << "\t.linkonce largest\n";
102 case COFF::IMAGE_COMDAT_SELECT_NEWEST:
103 OS << "\t.linkonce newest\n";
106 assert (0 && "unsupported COFF selection type");
112 bool MCSectionCOFF::UseCodeAlign() const {
113 return getKind().isText();
116 bool MCSectionCOFF::isVirtualSection() const {
117 return getCharacteristics() & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;