1 //===-- CodeGen/AsmPrinter/AIXException.cpp - AIX Exception Impl ----------===//
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 // This file contains support for writing AIX exception info into asm files.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "DwarfException.h"
14 #include "llvm/CodeGen/AsmPrinter.h"
15 #include "llvm/CodeGen/MachineModuleInfo.h"
16 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
17 #include "llvm/MC/MCStreamer.h"
18 #include "llvm/Target/TargetLoweringObjectFile.h"
19 #include "llvm/Target/TargetMachine.h"
20 
21 namespace llvm {
22 
23 AIXException::AIXException(AsmPrinter *A) : DwarfCFIExceptionBase(A) {}
24 
25 void AIXException::markFunctionEnd() { endFragment(); }
26 
27 void AIXException::emitExceptionInfoTable(const MCSymbol *LSDA,
28                                           const MCSymbol *PerSym) {
29   // Generate EH Info Table.
30   // The EH Info Table, aka, 'compat unwind section' on AIX, have the following
31   // format: struct eh_info_t {
32   //   unsigned version;           /* EH info verion 0 */
33   // #if defined(__64BIT__)
34   //   char _pad[4];               /* padding */
35   // #endif
36   //   unsigned long lsda;         /* Pointer to LSDA */
37   //   unsigned long personality;  /* Pointer to the personality routine */
38   //   }
39 
40   Asm->OutStreamer->SwitchSection(
41       Asm->getObjFileLowering().getCompactUnwindSection());
42   MCSymbol *EHInfoLabel =
43       TargetLoweringObjectFileXCOFF::getEHInfoTableSymbol(Asm->MF);
44   Asm->OutStreamer->emitLabel(EHInfoLabel);
45 
46   // Version number.
47   Asm->emitInt32(0);
48 
49   const DataLayout &DL = MMI->getModule()->getDataLayout();
50   const unsigned PointerSize = DL.getPointerSize();
51 
52   // Add necessary paddings in 64 bit mode.
53   Asm->OutStreamer->emitValueToAlignment(PointerSize);
54 
55   // LSDA location.
56   Asm->OutStreamer->emitValue(MCSymbolRefExpr::create(LSDA, Asm->OutContext),
57                               PointerSize);
58 
59   // Personality routine.
60   Asm->OutStreamer->emitValue(MCSymbolRefExpr::create(PerSym, Asm->OutContext),
61                               PointerSize);
62 }
63 
64 void AIXException::endFunction(const MachineFunction *MF) {
65   // There is no easy way to access register information in `AIXException`
66   // class. when ShouldEmitEHBlock is false and VRs are saved, A dumy eh info
67   // table are emitted in PPCAIXAsmPrinter::emitFunctionBodyEnd.
68   if (!TargetLoweringObjectFileXCOFF::ShouldEmitEHBlock(MF))
69     return;
70 
71   const MCSymbol *LSDALabel = emitExceptionTable();
72 
73   const Function &F = MF->getFunction();
74   assert(F.hasPersonalityFn() &&
75          "Landingpads are presented, but no personality routine is found.");
76   const auto *Per =
77       cast<GlobalValue>(F.getPersonalityFn()->stripPointerCasts());
78   const MCSymbol *PerSym = Asm->TM.getSymbol(Per);
79 
80   emitExceptionInfoTable(LSDALabel, PerSym);
81 }
82 
83 } // End of namespace llvm
84