]> granicus.if.org Git - llvm/commitdiff
[PowerPC][AIX] Adds support for writing the .data section in assembly files
authorXing Xue <xingxue@outlook.com>
Sun, 25 Aug 2019 15:17:25 +0000 (15:17 +0000)
committerXing Xue <xingxue@outlook.com>
Sun, 25 Aug 2019 15:17:25 +0000 (15:17 +0000)
Summary:
Adds support for generating the .data section in assembly files for global variables with a non-zero initialization. The support for writing the .data section in XCOFF object files will be added in a follow-on patch. Any relocations are not included in this patch.

Reviewers: hubert.reinterpretcast, sfertile, jasonliu, daltenty, Xiangling_L

Reviewed by: hubert.reinterpretcast

Subscribers: nemanjai, hiraditya, kbarton, MaskRay, jsji, wuzish, shchenz, DiggerLin, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D66154

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@369869 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/CodeGen/AsmPrinter.h
include/llvm/MC/MCAsmInfo.h
lib/CodeGen/AsmPrinter/AsmPrinter.cpp
lib/CodeGen/TargetLoweringObjectFileImpl.cpp
lib/MC/MCAsmInfoXCOFF.cpp
lib/MC/MCAsmStreamer.cpp
lib/MC/MCObjectFileInfo.cpp
lib/MC/MCSectionXCOFF.cpp
lib/Target/PowerPC/PPCAsmPrinter.cpp
test/CodeGen/PowerPC/aix-xcoff-data.ll [new file with mode: 0644]

index 78188ed0eff66e9dc80b1855674a574bd8cede1e..7ca0e508416be92236d08b06a788fa5ab744d824 100644 (file)
@@ -635,6 +635,11 @@ public:
   /// supported by the target.
   void EmitLinkage(const GlobalValue *GV, MCSymbol *GVSym) const;
 
+  /// Return the alignment in log2 form for the specified \p GV.
+  static unsigned getGVAlignmentLog2(const GlobalValue *GV,
+                                     const DataLayout &DL,
+                                     unsigned InBits = 0);
+
 private:
   /// Private state for PrintSpecial()
   // Assign a unique ID to this machine instruction.
index 971e9354da8c7f1a7745f78daa226db75c53e1f4..95c5662835a23f04baecf2ea573f1d2357c5819d 100644 (file)
@@ -165,6 +165,10 @@ protected:
   /// instead.
   bool UseDataRegionDirectives = false;
 
+  /// True if .align is to be used for alignment. Only power-of-two
+  /// alignment is supported.
+  bool UseDotAlignForAlignment = false;
+
   //===--- Data Emission Directives -------------------------------------===//
 
   /// This should be set to the directive used to get some number of zero bytes
@@ -520,6 +524,10 @@ public:
     return UseDataRegionDirectives;
   }
 
+  bool useDotAlignForAlignment() const {
+    return UseDotAlignForAlignment;
+  }
+
   const char *getZeroDirective() const { return ZeroDirective; }
   const char *getAsciiDirective() const { return AsciiDirective; }
   const char *getAscizDirective() const { return AscizDirective; }
index 8530174bb6cbd7f6f755a7edabc1ee022078a51c..220c47589564fc34d8b0f562e01409dfb3517a10 100644 (file)
@@ -162,8 +162,9 @@ static gcp_map_type &getGCMap(void *&P) {
 /// getGVAlignmentLog2 - Return the alignment to use for the specified global
 /// value in log2 form.  This rounds up to the preferred alignment if possible
 /// and legal.
-static unsigned getGVAlignmentLog2(const GlobalValue *GV, const DataLayout &DL,
-                                   unsigned InBits = 0) {
+unsigned AsmPrinter::getGVAlignmentLog2(const GlobalValue *GV,
+                                        const DataLayout &DL,
+                                        unsigned InBits) {
   unsigned NumBits = 0;
   if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
     NumBits = DL.getPreferredAlignmentLog(GVar);
index 1505c9d5e15baa6bda265c3476a3859c4524d4ce..653f30a12a23bede5f175c0c1fe39826920f5333 100644 (file)
@@ -1849,6 +1849,9 @@ MCSection *TargetLoweringObjectFileXCOFF::SelectSectionForGlobal(
   if (Kind.isText())
     return TextSection;
 
+  if (Kind.isData())
+    return DataSection;
+
   report_fatal_error("XCOFF other section types not yet implemented.");
 }
 
index 92dc505c0a1e178c9be7545904d46076b64518f0..13939f6a65f85caa491e4700acd04d3608563e44 100644 (file)
@@ -17,4 +17,8 @@ MCAsmInfoXCOFF::MCAsmInfoXCOFF() {
   HasDotTypeDotSizeDirective = false;
   COMMDirectiveAlignmentIsInBytes = false;
   LCOMMDirectiveAlignmentType = LCOMM::Log2Alignment;
+  UseDotAlignForAlignment = true;
+  AsciiDirective = nullptr; // not supported
+  AscizDirective = nullptr; // not supported
+  Data64bitsDirective = "\t.llong\t";
 }
index e20ba2f96e3bcd6eb721bb534f3c7ca32851a9da..e37bc4a4d706d4f2a79f84aa462ebd7658089394 100644 (file)
@@ -1119,6 +1119,16 @@ void MCAsmStreamer::emitFill(const MCExpr &NumValues, int64_t Size,
 void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
                                          unsigned ValueSize,
                                          unsigned MaxBytesToEmit) {
+  if (MAI->useDotAlignForAlignment()) {
+    if (!isPowerOf2_32(ByteAlignment))
+      report_fatal_error("Only power-of-two alignments are supported "
+                         "with .align.");
+    OS << "\t.align\t";
+    OS << Log2_32(ByteAlignment);
+    EmitEOL();
+    return;
+  }
+
   // Some assemblers don't support non-power of two alignments, so we always
   // emit alignments as a power of two if possible.
   if (isPowerOf2_32(ByteAlignment)) {
index d59453aa1cae1e7bb9c6d2cb6febc813ea3ad659..861e5013b6b2eb0da893dd6e271e5d90f0513a71 100644 (file)
@@ -770,6 +770,10 @@ void MCObjectFileInfo::initXCOFFMCObjectFileInfo(const Triple &T) {
   TextSection = Ctx->getXCOFFSection(
       ".text", XCOFF::StorageMappingClass::XMC_PR, XCOFF::XTY_SD,
       XCOFF::C_HIDEXT, SectionKind::getText());
+
+  DataSection = Ctx->getXCOFFSection(
+      ".data", XCOFF::StorageMappingClass::XMC_RW, XCOFF::XTY_SD,
+      XCOFF::C_HIDEXT, SectionKind::getData());
 }
 
 void MCObjectFileInfo::InitMCObjectFileInfo(const Triple &TheTriple, bool PIC,
index d00a435b6250ca601260cfd9e9a998f591c95dc0..db277521db2561f58f67a54001e186e333af34b2 100644 (file)
@@ -28,6 +28,16 @@ void MCSectionXCOFF::PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
     return;
   }
 
+  if (getKind().isData()) {
+    assert(getMappingClass() == XCOFF::XMC_RW &&
+           "Unhandled storage-mapping class for data section.");
+
+    OS << "\t.csect " << getSectionName() << "["
+       << "RW"
+       << "]" << '\n';
+    return;
+  }
+
   if (getKind().isBSSLocal() || getKind().isCommon()) {
     assert((getMappingClass() == XCOFF::XMC_RW ||
             getMappingClass() == XCOFF::XMC_BS) &&
index 8d23238e6159ef900988872dec89b8b63b462b8e..b1d988db83920598917bb51a13d5fa37017ed4d9 100644 (file)
@@ -1659,8 +1659,9 @@ void PPCAIXAsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
     report_fatal_error("COMDAT not yet supported by AIX.");
 
   SectionKind GVKind = getObjFileLowering().getKindForGlobal(GV, TM);
-  if (!GVKind.isCommon() && !GVKind.isBSSLocal())
-    report_fatal_error("Only common variables are supported on AIX for now.");
+  if (!GVKind.isCommon() && !GVKind.isBSSLocal() && !GVKind.isData())
+    report_fatal_error("Encountered a global variable kind that is "
+                       "not supported yet.");
 
   // Create the containing csect and switch to it.
   MCSectionXCOFF *CSect = cast<MCSectionXCOFF>(
@@ -1668,20 +1669,34 @@ void PPCAIXAsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
   OutStreamer->SwitchSection(CSect);
 
   // Create the symbol, set its storage class, and emit it.
-  MCSymbolXCOFF *XSym = cast<MCSymbolXCOFF>(getSymbol(GV));
-  XSym->setStorageClass(
+  MCSymbolXCOFF *GVSym = cast<MCSymbolXCOFF>(getSymbol(GV));
+  GVSym->setStorageClass(
       TargetLoweringObjectFileXCOFF::getStorageClassForGlobal(GV));
-  XSym->setContainingCsect(CSect);
+  GVSym->setContainingCsect(CSect);
 
   const DataLayout &DL = GV->getParent()->getDataLayout();
-  unsigned Align =
+
+  // Handle common symbols.
+  if (GVKind.isCommon() || GVKind.isBSSLocal()) {
+    unsigned Align =
       GV->getAlignment() ? GV->getAlignment() : DL.getPreferredAlignment(GV);
-  uint64_t Size = DL.getTypeAllocSize(GV->getType()->getElementType());
+    uint64_t Size = DL.getTypeAllocSize(GV->getType()->getElementType());
+
+    if (GVKind.isBSSLocal())
+      OutStreamer->EmitXCOFFLocalCommonSymbol(GVSym, Size, Align);
+    else
+      OutStreamer->EmitCommonSymbol(GVSym, Size, Align);
+    return;
+  }
+
+  // Get the alignment in the log2 form.
+  const unsigned AlignLog = getGVAlignmentLog2(GV, DL);
 
-  if (GVKind.isBSSLocal())
-    OutStreamer->EmitXCOFFLocalCommonSymbol(XSym, Size, Align);
-  else
-    OutStreamer->EmitCommonSymbol(XSym, Size, Align);
+  MCSymbol *EmittedInitSym = GVSym;
+  EmitLinkage(GV, EmittedInitSym);
+  EmitAlignment(AlignLog, GV);
+  OutStreamer->EmitLabel(EmittedInitSym);
+  EmitGlobalConstant(GV->getParent()->getDataLayout(), GV->getInitializer());
 }
 
 /// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
diff --git a/test/CodeGen/PowerPC/aix-xcoff-data.ll b/test/CodeGen/PowerPC/aix-xcoff-data.ll
new file mode 100644 (file)
index 0000000..fa1b70d
--- /dev/null
@@ -0,0 +1,57 @@
+; RUN: llc -mtriple powerpc-ibm-aix-xcoff < %s | FileCheck %s
+; RUN: llc -mtriple powerpc64-ibm-aix-xcoff < %s | FileCheck %s
+
+@ivar = local_unnamed_addr global i32 35, align 4
+@llvar = local_unnamed_addr global i64 36, align 8
+@svar = local_unnamed_addr global i16 37, align 2
+@fvar = local_unnamed_addr global float 8.000000e+02, align 4
+@dvar = local_unnamed_addr global double 9.000000e+02, align 8
+@over_aligned = local_unnamed_addr global double 9.000000e+02, align 32
+@charr = local_unnamed_addr global [4 x i8] c"abcd", align 1
+@dblarr = local_unnamed_addr global [4 x double] [double 1.000000e+00, double 2.000000e+00, double 3.000000e+00, double 4.000000e+00], align 8
+
+; CHECK:      .csect .data[RW]
+; CHECK-NEXT: .globl  ivar
+; CHECK-NEXT: .align  2
+; CHECK-NEXT: ivar:
+; CHECK-NEXT: .long   35
+
+; CHECK:      .globl  llvar
+; CHECK-NEXT: .align  3
+; CHECK-NEXT: llvar:
+; CHECK-NEXT: .llong  36
+
+; CHECK:      .globl  svar
+; CHECK-NEXT: .align  1
+; CHECK-NEXT: svar:
+; CHECK-NEXT: .short  37
+
+; CHECK:      .globl  fvar
+; CHECK-NEXT: .align  2
+; CHECK-NEXT: fvar:
+; CHECK-NEXT: .long   1145569280
+
+; CHECK:      .globl  dvar
+; CHECK-NEXT: .align  3
+; CHECK-NEXT: dvar:
+; CHECK-NEXT: .llong  4651127699538968576
+
+; CHECK:      .globl  over_aligned
+; CHECK-NEXT: .align  5
+; CHECK-NEXT: over_aligned:
+; CHECK-NEXT: .llong  4651127699538968576
+
+; CHECK:      .globl  charr
+; CHECK-NEXT: charr:
+; CHECK-NEXT: .byte   97
+; CHECK-NEXT: .byte   98
+; CHECK-NEXT: .byte   99
+; CHECK-NEXT: .byte   100
+
+; CHECK:      .globl  dblarr
+; CHECK-NEXT: .align  3
+; CHECK-NEXT: dblarr:
+; CHECK-NEXT: .llong  4607182418800017408
+; CHECK-NEXT: .llong  4611686018427387904
+; CHECK-NEXT: .llong  4613937818241073152
+; CHECK-NEXT: .llong  4616189618054758400