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