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