LLVM 24.0.0git
MCAsmInfoCOFF.cpp
Go to the documentation of this file.
1//===- MCAsmInfoCOFF.cpp - COFF asm properties ----------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines target asm properties related what form asm statements
10// should take in general on COFF-based targets
11//
12//===----------------------------------------------------------------------===//
13
17#include "llvm/MC/MCSection.h"
19#include "llvm/MC/MCSymbol.h"
21#include <cassert>
22
23using namespace llvm;
24
25static void printSwitchToSection(const MCAsmInfo &MAI, const MCSection &Section,
26 uint32_t, const Triple &T, raw_ostream &OS) {
27 auto &Sec = static_cast<const MCSectionCOFF &>(Section);
28 // standard sections don't require the '.section'
29 if (!Sec.getCOMDATSymbol() && !Sec.isUnique() &&
30 MAI.isSectionDirective(Sec.getName())) {
31 OS << '\t' << Sec.getName() << '\n';
32 return;
33 }
34
35 OS << "\t.section\t" << Sec.getName() << ",\"";
36 if (Sec.getCharacteristics() & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
37 OS << 'd';
38 if (Sec.getCharacteristics() & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
39 OS << 'b';
40 if (Sec.getCharacteristics() & COFF::IMAGE_SCN_MEM_EXECUTE)
41 OS << 'x';
42 if (Sec.getCharacteristics() & COFF::IMAGE_SCN_MEM_WRITE)
43 OS << 'w';
44 else if (Sec.getCharacteristics() & COFF::IMAGE_SCN_MEM_READ)
45 OS << 'r';
46 else
47 OS << 'y';
48 if (Sec.getCharacteristics() & COFF::IMAGE_SCN_LNK_REMOVE)
49 OS << 'n';
50 if (Sec.getCharacteristics() & COFF::IMAGE_SCN_MEM_SHARED)
51 OS << 's';
52 if ((Sec.getCharacteristics() & COFF::IMAGE_SCN_MEM_DISCARDABLE) &&
53 !Sec.isImplicitlyDiscardable(Sec.getName()))
54 OS << 'D';
55 if (Sec.getCharacteristics() & COFF::IMAGE_SCN_LNK_INFO)
56 OS << 'i';
57 OS << '"';
58
59 // unique should be tail of .section directive.
60 if (Sec.isUnique() && !Sec.getCOMDATSymbol())
61 OS << ",unique," << Sec.getUniqueID();
62
63 if (Sec.getCharacteristics() & COFF::IMAGE_SCN_LNK_COMDAT) {
64 if (Sec.getCOMDATSymbol())
65 OS << ",";
66 else
67 OS << "\n\t.linkonce\t";
68 switch (Sec.getSelection()) {
70 OS << "one_only";
71 break;
73 OS << "discard";
74 break;
76 OS << "same_size";
77 break;
79 OS << "same_contents";
80 break;
82 OS << "associative";
83 break;
85 OS << "largest";
86 break;
88 OS << "newest";
89 break;
90 default:
91 assert(false && "unsupported COFF selection type");
92 break;
93 }
94 if (const MCSymbol *Sym = Sec.getCOMDATSymbol()) {
95 OS << ",";
96 Sym->print(OS, &MAI);
97 }
98 }
99
100 if (Sec.isUnique() && Sec.getCOMDATSymbol())
101 OS << ",unique," << Sec.getUniqueID();
102
103 OS << '\n';
104}
105
107 : MCAsmInfo(Options) {
108 // MingW 4.5 and later support .comm with log2 alignment, but .lcomm uses byte
109 // alignment.
114 WeakRefDirective = "\t.weak\t";
115 AvoidWeakIfComdat = true;
116
117 // Doesn't support visibility:
120
121 // Set up DWARF directives
124
125 // At least MSVC inline-asm does AShr.
126 UseLogicalShr = false;
127
128 // If this is a COFF target, assume that it supports associative comdats. It's
129 // part of the spec.
131
132 // We can generate constants in comdat sections that can be shared,
133 // but in order not to create null typed symbols, we actually need to
134 // make them global symbols as well.
136
138
139 // FIXME: Does .section .bss/.data/.text work everywhere??
140 SectionDirectives.insert(".text");
141 SectionDirectives.insert(".data");
142 SectionDirectives.insert(".bss");
143}
144
147
150 // If this is a GNU environment (mingw or cygwin), don't use associative
151 // comdats for jump tables, unwind information, and other data associated with
152 // a function.
154
155 // We don't create constants in comdat sections for MinGW.
157}
158
159void MCSectionCOFF::setSelection(int Selection) const {
160 assert(Selection != 0 && "invalid COMDAT selection type");
161 this->Selection = Selection;
162 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
163}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static LVOptions Options
Definition LVOptions.cpp:25
static void printSwitchToSection(const MCAsmInfo &MAI, const MCSection &Section, uint32_t, const Triple &T, raw_ostream &OS)
#define T
MCAsmInfoCOFF(const MCTargetOptions &Options)
MCAsmInfoGNUCOFF(const MCTargetOptions &Options)
MCAsmInfoMicrosoft(const MCTargetOptions &Options)
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition MCAsmInfo.h:69
bool NeedsDwarfSectionOffsetDirective
Definition MCAsmInfo.h:274
MCSymbolAttr ProtectedVisibilityAttr
This attribute, if not MCSA_Invalid, is used to declare a symbol as having protected visibility.
Definition MCAsmInfo.h:366
bool AvoidWeakIfComdat
True if we should mark symbols as global instead of weak, for weak*‍/linkonce*, if the symbol has a c...
Definition MCAsmInfo.h:350
MCAsmInfo(const MCTargetOptions &Options)
Definition MCAsmInfo.cpp:28
void printSwitchToSection(const MCSection &Sec, uint32_t Subsection, const Triple &T, raw_ostream &OS) const
Definition MCAsmInfo.h:555
bool UseLogicalShr
True if the integrated assembler should interpret 'a >> b' constant expressions as logical rather tha...
Definition MCAsmInfo.h:455
LCOMM::LCOMMType LCOMMDirectiveAlignmentType
Describes if the .lcomm directive for the target supports an alignment argument and how it is interpr...
Definition MCAsmInfo.h:311
bool HasCOFFComdatConstants
True if this is a non-GNU COFF target.
Definition MCAsmInfo.h:119
const char * WeakRefDirective
This directive, if non-null, is used to declare a global as being a weak undefined symbol.
Definition MCAsmInfo.h:341
SmallDenseSet< StringRef, 8 > SectionDirectives
Sections whose name also is supported as a directive without an explicit ".section" directive.
Definition MCAsmInfo.h:475
bool HasDotTypeDotSizeDirective
True if the target has .type and .size directives, this is true for most ELF targets.
Definition MCAsmInfo.h:322
bool HasCOFFAssociativeComdats
True if this is a non-GNU COFF target.
Definition MCAsmInfo.h:115
bool SupportsDebugInformation
True if target supports emission of debugging information.
Definition MCAsmInfo.h:372
MCSymbolAttr HiddenDeclarationVisibilityAttr
This attribute, if not MCSA_Invalid, is used to declare an undefined symbol as having hidden visibili...
Definition MCAsmInfo.h:362
bool HasSingleParameterDotFile
True if the target has a single parameter .file directive, this is true for ELF targets.
Definition MCAsmInfo.h:326
bool COMMDirectiveAlignmentIsInBytes
True is .comm's and .lcomms optional alignment is to be specified in bytes instead of log2(n).
Definition MCAsmInfo.h:307
void(* PrintSwitchToSectionFn)(const MCAsmInfo &, const MCSection &, uint32_t Subsection, const Triple &, raw_ostream &)
Definition MCAsmInfo.h:276
MCSymbolAttr HiddenVisibilityAttr
This attribute, if not MCSA_Invalid, is used to declare a symbol as having hidden visibility.
Definition MCAsmInfo.h:354
bool isSectionDirective(StringRef SecName) const
Definition MCAsmInfo.h:551
This represents a section on Windows.
LLVM_ABI void setSelection(int Selection) const
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition MCSection.h:577
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:48
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
@ IMAGE_SCN_MEM_SHARED
Definition COFF.h:334
@ IMAGE_SCN_LNK_REMOVE
Definition COFF.h:308
@ IMAGE_SCN_MEM_READ
Definition COFF.h:336
@ IMAGE_SCN_MEM_EXECUTE
Definition COFF.h:335
@ IMAGE_SCN_CNT_UNINITIALIZED_DATA
Definition COFF.h:305
@ IMAGE_SCN_MEM_DISCARDABLE
Definition COFF.h:331
@ IMAGE_SCN_LNK_INFO
Definition COFF.h:307
@ IMAGE_SCN_CNT_INITIALIZED_DATA
Definition COFF.h:304
@ IMAGE_SCN_LNK_COMDAT
Definition COFF.h:309
@ IMAGE_SCN_MEM_WRITE
Definition COFF.h:337
@ IMAGE_COMDAT_SELECT_NODUPLICATES
Definition COFF.h:455
@ IMAGE_COMDAT_SELECT_LARGEST
Definition COFF.h:460
@ IMAGE_COMDAT_SELECT_NEWEST
Definition COFF.h:461
@ IMAGE_COMDAT_SELECT_SAME_SIZE
Definition COFF.h:457
@ IMAGE_COMDAT_SELECT_ASSOCIATIVE
Definition COFF.h:459
@ IMAGE_COMDAT_SELECT_EXACT_MATCH
Definition COFF.h:458
@ IMAGE_COMDAT_SELECT_ANY
Definition COFF.h:456
This is an optimization pass for GlobalISel generic memory operations.
@ MCSA_Invalid
Not a valid directive.