1//===- MC/MCAsmInfoXCOFF.cpp - XCOFF asm properties ------------ *- C++ -*-===//
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#include "llvm/MC/MCAsmInfoXCOFF.h"
10#include "MCCLOptions.h"
11#include "llvm/ADT/StringExtras.h"
12#include "llvm/MC/MCAsmInfo.h"
13#include "llvm/MC/MCSectionXCOFF.h"
14#include "llvm/Support/Format.h"
15#include "llvm/Support/raw_ostream.h"
16
17using namespace llvm;
18
19MCAsmInfoXCOFF::MCAsmInfoXCOFF(const MCTargetOptions &Options)
20 : MCAsmInfo(Options) {
21 IsAIX = true;
22 IsLittleEndian = false;
23
24 InternalSymbolPrefix = "L..";
25 SupportsQuotedNames = false;
26 if (getMCCLOptions(Options).use_leb128_directives == BoolOrDefault::Default)
27 HasLEB128Directives = false;
28 ZeroDirective = "\t.space\t";
29 AsciiDirective = nullptr; // not supported
30 AscizDirective = nullptr; // not supported
31 CharacterLiteralSyntax = ACLS_SingleQuotePrefix;
32
33 // Use .vbyte for data definition to avoid directives that apply an implicit
34 // alignment.
35 Data16bitsDirective = "\t.vbyte\t2, ";
36 Data32bitsDirective = "\t.vbyte\t4, ";
37
38 COMMDirectiveAlignmentIsInBytes = false;
39 LCOMMDirectiveAlignmentType = LCOMM::Log2Alignment;
40 HasDotTypeDotSizeDirective = false;
41 ParseInlineAsmUsingAsmParser = true;
42}
43
44bool MCAsmInfoXCOFF::useCodeAlign(const MCSection &Sec) const {
45 return static_cast<const MCSectionXCOFF &>(Sec).getKind().isText();
46}
47
48MCSectionXCOFF::~MCSectionXCOFF() = default;
49
50void MCSectionXCOFF::printCsectDirective(raw_ostream &OS) const {
51 OS << "\t.csect " << QualName->getName() << "," << Log2(A: getAlign()) << '\n';
52}
53
54void MCAsmInfoXCOFF::printSwitchToSection(const MCSection &Section, uint32_t,
55 const Triple &T,
56 raw_ostream &OS) const {
57 auto &Sec = static_cast<const MCSectionXCOFF &>(Section);
58 if (Sec.getKind().isText()) {
59 if (Sec.getMappingClass() != XCOFF::XMC_PR)
60 report_fatal_error(reason: "Unhandled storage-mapping class for .text csect");
61
62 Sec.printCsectDirective(OS);
63 return;
64 }
65
66 if (Sec.getKind().isReadOnly()) {
67 if (Sec.getMappingClass() != XCOFF::XMC_RO &&
68 Sec.getMappingClass() != XCOFF::XMC_TD)
69 report_fatal_error(reason: "Unhandled storage-mapping class for .rodata csect.");
70 Sec.printCsectDirective(OS);
71 return;
72 }
73
74 if (Sec.getKind().isReadOnlyWithRel()) {
75 if (Sec.getMappingClass() != XCOFF::XMC_RW &&
76 Sec.getMappingClass() != XCOFF::XMC_RO &&
77 Sec.getMappingClass() != XCOFF::XMC_TD)
78 report_fatal_error(
79 reason: "Unexepected storage-mapping class for ReadOnlyWithRel kind");
80 Sec.printCsectDirective(OS);
81 return;
82 }
83
84 // Initialized TLS data.
85 if (Sec.getKind().isThreadData()) {
86 // We only expect XMC_TL here for initialized TLS data.
87 if (Sec.getMappingClass() != XCOFF::XMC_TL)
88 report_fatal_error(reason: "Unhandled storage-mapping class for .tdata csect.");
89 Sec.printCsectDirective(OS);
90 return;
91 }
92
93 if (Sec.getKind().isData()) {
94 switch (Sec.getMappingClass()) {
95 case XCOFF::XMC_RW:
96 case XCOFF::XMC_DS:
97 case XCOFF::XMC_TD:
98 Sec.printCsectDirective(OS);
99 break;
100 case XCOFF::XMC_TC:
101 case XCOFF::XMC_TE:
102 break;
103 case XCOFF::XMC_TC0:
104 OS << "\t.toc\n";
105 break;
106 default:
107 report_fatal_error(reason: "Unhandled storage-mapping class for .data csect.");
108 }
109 return;
110 }
111
112 if (Sec.isCsect() && Sec.getMappingClass() == XCOFF::XMC_TD) {
113 // Common csect type (uninitialized storage) does not have to print
114 // csect directive for section switching unless it is local.
115 if (Sec.getKind().isCommon() && !Sec.getKind().isBSSLocal())
116 return;
117
118 assert(Sec.getKind().isBSS() && "Unexpected section kind for toc-data");
119 Sec.printCsectDirective(OS);
120 return;
121 }
122 // Common csect type (uninitialized storage) does not have to print csect
123 // directive for section switching.
124 if (Sec.isCsect() && Sec.getCSectType() == XCOFF::XTY_CM) {
125 assert((Sec.getMappingClass() == XCOFF::XMC_RW ||
126 Sec.getMappingClass() == XCOFF::XMC_BS ||
127 Sec.getMappingClass() == XCOFF::XMC_UL) &&
128 "Generated a storage-mapping class for a common/bss/tbss csect we "
129 "don't "
130 "understand how to switch to.");
131 // Common symbols and local zero-initialized symbols for TLS and Non-TLS are
132 // eligible for .bss/.tbss csect, getKind().isThreadBSS() is used to
133 // cover TLS common and zero-initialized local symbols since linkage type
134 // (in the GlobalVariable) is not accessible in this class.
135 assert((Sec.getKind().isBSSLocal() || Sec.getKind().isCommon() ||
136 Sec.getKind().isThreadBSS()) &&
137 "wrong symbol type for .bss/.tbss csect");
138 // Don't have to print a directive for switching to section for commons
139 // and zero-initialized TLS data. The '.comm' and '.lcomm' directives of the
140 // variable will create the needed csect.
141 return;
142 }
143
144 // Zero-initialized TLS data with weak or external linkage are not eligible to
145 // be put into common csect.
146 if (Sec.getKind().isThreadBSS()) {
147 Sec.printCsectDirective(OS);
148 return;
149 }
150
151 // XCOFF debug sections.
152 if (Sec.getKind().isMetadata() && Sec.isDwarfSect()) {
153 OS << "\n\t.dwsect " << format(Fmt: "0x%" PRIx32, Vals: *Sec.getDwarfSubtypeFlags())
154 << '\n';
155 OS << Sec.getName() << ':' << '\n';
156 return;
157 }
158
159 report_fatal_error(reason: "Printing for this SectionKind is unimplemented.");
160}
161