1 //===-- CodeGen/AsmPrinter/AIXException.cpp - AIX Exception Impl ----------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file contains support for writing AIX exception info into asm files.
11 //===----------------------------------------------------------------------===//
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/IR/Module.h"
18 #include "llvm/MC/MCSectionXCOFF.h"
19 #include "llvm/MC/MCStreamer.h"
20 #include "llvm/Target/TargetLoweringObjectFile.h"
21 #include "llvm/Target/TargetMachine.h"
25 AIXException::AIXException(AsmPrinter
*A
) : EHStreamer(A
) {}
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 */
36 // unsigned long lsda; /* Pointer to LSDA */
37 // unsigned long personality; /* Pointer to the personality routine */
41 cast
<MCSectionXCOFF
>(Asm
->getObjFileLowering().getCompactUnwindSection());
42 if (Asm
->TM
.getFunctionSections()) {
43 // If option -ffunction-sections is on, append the function name to the
44 // name of EH Info Table csect so that each function has its own EH Info
45 // Table csect. This helps the linker to garbage-collect EH info of unused
47 SmallString
<128> NameStr
= EHInfo
->getName();
48 raw_svector_ostream(NameStr
) << '.' << Asm
->MF
->getFunction().getName();
49 EHInfo
= Asm
->OutContext
.getXCOFFSection(NameStr
, EHInfo
->getKind(),
50 EHInfo
->getCsectProp());
52 Asm
->OutStreamer
->switchSection(EHInfo
);
53 MCSymbol
*EHInfoLabel
=
54 TargetLoweringObjectFileXCOFF::getEHInfoTableSymbol(Asm
->MF
);
55 Asm
->OutStreamer
->emitLabel(EHInfoLabel
);
60 const DataLayout
&DL
= MMI
->getModule()->getDataLayout();
61 const unsigned PointerSize
= DL
.getPointerSize();
63 // Add necessary paddings in 64 bit mode.
64 Asm
->OutStreamer
->emitValueToAlignment(Align(PointerSize
));
67 Asm
->OutStreamer
->emitValue(MCSymbolRefExpr::create(LSDA
, Asm
->OutContext
),
70 // Personality routine.
71 Asm
->OutStreamer
->emitValue(MCSymbolRefExpr::create(PerSym
, Asm
->OutContext
),
75 void AIXException::endFunction(const MachineFunction
*MF
) {
76 // There is no easy way to access register information in `AIXException`
77 // class. when ShouldEmitEHBlock is false and VRs are saved, A dumy eh info
78 // table are emitted in PPCAIXAsmPrinter::emitFunctionBodyEnd.
79 if (!TargetLoweringObjectFileXCOFF::ShouldEmitEHBlock(MF
))
82 const MCSymbol
*LSDALabel
= emitExceptionTable();
84 const Function
&F
= MF
->getFunction();
85 assert(F
.hasPersonalityFn() &&
86 "Landingpads are presented, but no personality routine is found.");
88 cast
<GlobalValue
>(F
.getPersonalityFn()->stripPointerCasts());
89 const MCSymbol
*PerSym
= Asm
->TM
.getSymbol(Per
);
91 emitExceptionInfoTable(LSDALabel
, PerSym
);
94 } // End of namespace llvm