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