xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/AsmPrinter/AIXException.cpp (revision fe6060f10f634930ff71b7c50291ddc610da2475)
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 
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 
39e8d8bef9SDimitry Andric   Asm->OutStreamer->SwitchSection(
40e8d8bef9SDimitry Andric       Asm->getObjFileLowering().getCompactUnwindSection());
41e8d8bef9SDimitry Andric   MCSymbol *EHInfoLabel =
42e8d8bef9SDimitry Andric       TargetLoweringObjectFileXCOFF::getEHInfoTableSymbol(Asm->MF);
43e8d8bef9SDimitry Andric   Asm->OutStreamer->emitLabel(EHInfoLabel);
44e8d8bef9SDimitry Andric 
45e8d8bef9SDimitry Andric   // Version number.
46e8d8bef9SDimitry Andric   Asm->emitInt32(0);
47e8d8bef9SDimitry Andric 
48e8d8bef9SDimitry Andric   const DataLayout &DL = MMI->getModule()->getDataLayout();
49e8d8bef9SDimitry Andric   const unsigned PointerSize = DL.getPointerSize();
50e8d8bef9SDimitry Andric 
51e8d8bef9SDimitry Andric   // Add necessary paddings in 64 bit mode.
52e8d8bef9SDimitry Andric   Asm->OutStreamer->emitValueToAlignment(PointerSize);
53e8d8bef9SDimitry Andric 
54e8d8bef9SDimitry Andric   // LSDA location.
55e8d8bef9SDimitry Andric   Asm->OutStreamer->emitValue(MCSymbolRefExpr::create(LSDA, Asm->OutContext),
56e8d8bef9SDimitry Andric                               PointerSize);
57e8d8bef9SDimitry Andric 
58e8d8bef9SDimitry Andric   // Personality routine.
59e8d8bef9SDimitry Andric   Asm->OutStreamer->emitValue(MCSymbolRefExpr::create(PerSym, Asm->OutContext),
60e8d8bef9SDimitry Andric                               PointerSize);
61e8d8bef9SDimitry Andric }
62e8d8bef9SDimitry Andric 
63e8d8bef9SDimitry Andric void AIXException::endFunction(const MachineFunction *MF) {
64*fe6060f1SDimitry Andric   // There is no easy way to access register information in `AIXException`
65*fe6060f1SDimitry Andric   // class. when ShouldEmitEHBlock is false and VRs are saved, A dumy eh info
66*fe6060f1SDimitry Andric   // table are emitted in PPCAIXAsmPrinter::emitFunctionBodyEnd.
67e8d8bef9SDimitry Andric   if (!TargetLoweringObjectFileXCOFF::ShouldEmitEHBlock(MF))
68e8d8bef9SDimitry Andric     return;
69e8d8bef9SDimitry Andric 
70e8d8bef9SDimitry Andric   const MCSymbol *LSDALabel = emitExceptionTable();
71e8d8bef9SDimitry Andric 
72e8d8bef9SDimitry Andric   const Function &F = MF->getFunction();
73e8d8bef9SDimitry Andric   assert(F.hasPersonalityFn() &&
74e8d8bef9SDimitry Andric          "Landingpads are presented, but no personality routine is found.");
75*fe6060f1SDimitry Andric   const GlobalValue *Per =
76*fe6060f1SDimitry Andric       dyn_cast<GlobalValue>(F.getPersonalityFn()->stripPointerCasts());
77e8d8bef9SDimitry Andric   const MCSymbol *PerSym = Asm->TM.getSymbol(Per);
78e8d8bef9SDimitry Andric 
79e8d8bef9SDimitry Andric   emitExceptionInfoTable(LSDALabel, PerSym);
80e8d8bef9SDimitry Andric }
81e8d8bef9SDimitry Andric 
82e8d8bef9SDimitry Andric } // End of namespace llvm
83