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