xref: /freebsd-src/contrib/llvm-project/llvm/lib/CodeGen/AsmPrinter/AIXException.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
1e8d8bef9SDimitry Andric //===-- CodeGen/AsmPrinter/AIXException.cpp - AIX Exception Impl ----------===//
2e8d8bef9SDimitry Andric //
3e8d8bef9SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e8d8bef9SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e8d8bef9SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e8d8bef9SDimitry Andric //
7e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
8e8d8bef9SDimitry Andric //
9e8d8bef9SDimitry Andric // This file contains support for writing AIX exception info into asm files.
10e8d8bef9SDimitry Andric //
11e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
12e8d8bef9SDimitry Andric 
13e8d8bef9SDimitry Andric #include "DwarfException.h"
14e8d8bef9SDimitry Andric #include "llvm/CodeGen/AsmPrinter.h"
15e8d8bef9SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h"
16e8d8bef9SDimitry Andric #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
17e8d8bef9SDimitry Andric #include "llvm/MC/MCSectionXCOFF.h"
18e8d8bef9SDimitry Andric #include "llvm/MC/MCStreamer.h"
19e8d8bef9SDimitry Andric #include "llvm/Target/TargetLoweringObjectFile.h"
20e8d8bef9SDimitry Andric #include "llvm/Target/TargetMachine.h"
21e8d8bef9SDimitry Andric 
22e8d8bef9SDimitry Andric namespace llvm {
23e8d8bef9SDimitry Andric 
24*bdd1243dSDimitry Andric AIXException::AIXException(AsmPrinter *A) : EHStreamer(A) {}
2504eeddc0SDimitry Andric 
26e8d8bef9SDimitry Andric void AIXException::emitExceptionInfoTable(const MCSymbol *LSDA,
27e8d8bef9SDimitry Andric                                           const MCSymbol *PerSym) {
28e8d8bef9SDimitry Andric   // Generate EH Info Table.
29e8d8bef9SDimitry Andric   // The EH Info Table, aka, 'compat unwind section' on AIX, have the following
30e8d8bef9SDimitry Andric   // format: struct eh_info_t {
31e8d8bef9SDimitry Andric   //   unsigned version;           /* EH info verion 0 */
32e8d8bef9SDimitry Andric   // #if defined(__64BIT__)
33e8d8bef9SDimitry Andric   //   char _pad[4];               /* padding */
34e8d8bef9SDimitry Andric   // #endif
35e8d8bef9SDimitry Andric   //   unsigned long lsda;         /* Pointer to LSDA */
36e8d8bef9SDimitry Andric   //   unsigned long personality;  /* Pointer to the personality routine */
37e8d8bef9SDimitry Andric   //   }
38e8d8bef9SDimitry Andric 
3981ad6265SDimitry Andric   auto *EHInfo =
4081ad6265SDimitry Andric       cast<MCSectionXCOFF>(Asm->getObjFileLowering().getCompactUnwindSection());
4181ad6265SDimitry Andric   if (Asm->TM.getFunctionSections()) {
4281ad6265SDimitry Andric     // If option -ffunction-sections is on, append the function name to the
4381ad6265SDimitry Andric     // name of EH Info Table csect so that each function has its own EH Info
4481ad6265SDimitry Andric     // Table csect. This helps the linker to garbage-collect EH info of unused
4581ad6265SDimitry Andric     // functions.
4681ad6265SDimitry Andric     SmallString<128> NameStr = EHInfo->getName();
4781ad6265SDimitry Andric     raw_svector_ostream(NameStr) << '.' << Asm->MF->getFunction().getName();
4881ad6265SDimitry Andric     EHInfo = Asm->OutContext.getXCOFFSection(NameStr, EHInfo->getKind(),
4981ad6265SDimitry Andric                                              EHInfo->getCsectProp());
5081ad6265SDimitry Andric   }
5181ad6265SDimitry Andric   Asm->OutStreamer->switchSection(EHInfo);
52e8d8bef9SDimitry Andric   MCSymbol *EHInfoLabel =
53e8d8bef9SDimitry Andric       TargetLoweringObjectFileXCOFF::getEHInfoTableSymbol(Asm->MF);
54e8d8bef9SDimitry Andric   Asm->OutStreamer->emitLabel(EHInfoLabel);
55e8d8bef9SDimitry Andric 
56e8d8bef9SDimitry Andric   // Version number.
57e8d8bef9SDimitry Andric   Asm->emitInt32(0);
58e8d8bef9SDimitry Andric 
59e8d8bef9SDimitry Andric   const DataLayout &DL = MMI->getModule()->getDataLayout();
60e8d8bef9SDimitry Andric   const unsigned PointerSize = DL.getPointerSize();
61e8d8bef9SDimitry Andric 
62e8d8bef9SDimitry Andric   // Add necessary paddings in 64 bit mode.
63*bdd1243dSDimitry Andric   Asm->OutStreamer->emitValueToAlignment(Align(PointerSize));
64e8d8bef9SDimitry Andric 
65e8d8bef9SDimitry Andric   // LSDA location.
66e8d8bef9SDimitry Andric   Asm->OutStreamer->emitValue(MCSymbolRefExpr::create(LSDA, Asm->OutContext),
67e8d8bef9SDimitry Andric                               PointerSize);
68e8d8bef9SDimitry Andric 
69e8d8bef9SDimitry Andric   // Personality routine.
70e8d8bef9SDimitry Andric   Asm->OutStreamer->emitValue(MCSymbolRefExpr::create(PerSym, Asm->OutContext),
71e8d8bef9SDimitry Andric                               PointerSize);
72e8d8bef9SDimitry Andric }
73e8d8bef9SDimitry Andric 
74e8d8bef9SDimitry Andric void AIXException::endFunction(const MachineFunction *MF) {
75fe6060f1SDimitry Andric   // There is no easy way to access register information in `AIXException`
76fe6060f1SDimitry Andric   // class. when ShouldEmitEHBlock is false and VRs are saved, A dumy eh info
77fe6060f1SDimitry Andric   // table are emitted in PPCAIXAsmPrinter::emitFunctionBodyEnd.
78e8d8bef9SDimitry Andric   if (!TargetLoweringObjectFileXCOFF::ShouldEmitEHBlock(MF))
79e8d8bef9SDimitry Andric     return;
80e8d8bef9SDimitry Andric 
81e8d8bef9SDimitry Andric   const MCSymbol *LSDALabel = emitExceptionTable();
82e8d8bef9SDimitry Andric 
83e8d8bef9SDimitry Andric   const Function &F = MF->getFunction();
84e8d8bef9SDimitry Andric   assert(F.hasPersonalityFn() &&
85e8d8bef9SDimitry Andric          "Landingpads are presented, but no personality routine is found.");
8681ad6265SDimitry Andric   const auto *Per =
8781ad6265SDimitry Andric       cast<GlobalValue>(F.getPersonalityFn()->stripPointerCasts());
88e8d8bef9SDimitry Andric   const MCSymbol *PerSym = Asm->TM.getSymbol(Per);
89e8d8bef9SDimitry Andric 
90e8d8bef9SDimitry Andric   emitExceptionInfoTable(LSDALabel, PerSym);
91e8d8bef9SDimitry Andric }
92e8d8bef9SDimitry Andric 
93e8d8bef9SDimitry Andric } // End of namespace llvm
94