Path: blob/main/contrib/llvm-project/llvm/lib/CodeGen/AsmPrinter/EHStreamer.h
35271 views
//===- EHStreamer.h - Exception Handling Directive Streamer -----*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file contains support for writing exception info into assembly files.9//10//===----------------------------------------------------------------------===//1112#ifndef LLVM_LIB_CODEGEN_ASMPRINTER_EHSTREAMER_H13#define LLVM_LIB_CODEGEN_ASMPRINTER_EHSTREAMER_H1415#include "llvm/ADT/DenseMap.h"16#include "llvm/CodeGen/AsmPrinterHandler.h"17#include "llvm/Support/Compiler.h"1819namespace llvm {2021class AsmPrinter;22struct LandingPadInfo;23class MachineInstr;24class MachineModuleInfo;25class MCSymbol;26template <typename T> class SmallVectorImpl;2728/// Emits exception handling directives.29class LLVM_LIBRARY_VISIBILITY EHStreamer : public AsmPrinterHandler {30protected:31/// Target of directive emission.32AsmPrinter *Asm;3334/// Collected machine module information.35MachineModuleInfo *MMI;3637/// How many leading type ids two landing pads have in common.38static unsigned sharedTypeIDs(const LandingPadInfo *L,39const LandingPadInfo *R);4041/// Structure holding a try-range and the associated landing pad.42struct PadRange {43// The index of the landing pad.44unsigned PadIndex;4546// The index of the begin and end labels in the landing pad's label lists.47unsigned RangeIndex;48};4950using RangeMapType = DenseMap<MCSymbol *, PadRange>;5152/// Structure describing an entry in the actions table.53struct ActionEntry {54int ValueForTypeID; // The value to write - may not be equal to the type id.55int NextAction;56unsigned Previous;57};5859/// Structure describing an entry in the call-site table.60struct CallSiteEntry {61// The 'try-range' is BeginLabel .. EndLabel.62MCSymbol *BeginLabel; // Null indicates the start of the function.63MCSymbol *EndLabel; // Null indicates the end of the function.6465// LPad contains the landing pad start labels.66const LandingPadInfo *LPad; // Null indicates that there is no landing pad.6768unsigned Action;69};7071/// Structure describing a contiguous range of call-sites which reside72/// in the same procedure fragment. With -fbasic-block-sections, there will73/// be one call site range per basic block section. Otherwise, we will have74/// one call site range containing all the call sites in the function.75struct CallSiteRange {76// Symbol marking the beginning of the precedure fragment.77MCSymbol *FragmentBeginLabel = nullptr;78// Symbol marking the end of the procedure fragment.79MCSymbol *FragmentEndLabel = nullptr;80// LSDA symbol for this call-site range.81MCSymbol *ExceptionLabel = nullptr;82// Index of the first call-site entry in the call-site table which83// belongs to this range.84size_t CallSiteBeginIdx = 0;85// Index just after the last call-site entry in the call-site table which86// belongs to this range.87size_t CallSiteEndIdx = 0;88// Whether this is the call-site range containing all the landing pads.89bool IsLPRange = false;90};9192/// Compute the actions table and gather the first action index for each93/// landing pad site.94void computeActionsTable(95const SmallVectorImpl<const LandingPadInfo *> &LandingPads,96SmallVectorImpl<ActionEntry> &Actions,97SmallVectorImpl<unsigned> &FirstActions);9899void computePadMap(const SmallVectorImpl<const LandingPadInfo *> &LandingPads,100RangeMapType &PadMap);101102/// Compute the call-site table and the call-site ranges. The entry for an103/// invoke has a try-range containing the call, a non-zero landing pad and an104/// appropriate action. The entry for an ordinary call has a try-range105/// containing the call and zero for the landing pad and the action. Calls106/// marked 'nounwind' have no entry and must not be contained in the try-range107/// of any entry - they form gaps in the table. Entries must be ordered by108/// try-range address. CallSiteRanges vector is only populated for Itanium109/// exception handling.110virtual void computeCallSiteTable(111SmallVectorImpl<CallSiteEntry> &CallSites,112SmallVectorImpl<CallSiteRange> &CallSiteRanges,113const SmallVectorImpl<const LandingPadInfo *> &LandingPads,114const SmallVectorImpl<unsigned> &FirstActions);115116/// Emit landing pads and actions.117///118/// The general organization of the table is complex, but the basic concepts119/// are easy. First there is a header which describes the location and120/// organization of the three components that follow.121/// 1. The landing pad site information describes the range of code covered122/// by the try. In our case it's an accumulation of the ranges covered123/// by the invokes in the try. There is also a reference to the landing124/// pad that handles the exception once processed. Finally an index into125/// the actions table.126/// 2. The action table, in our case, is composed of pairs of type ids127/// and next action offset. Starting with the action index from the128/// landing pad site, each type Id is checked for a match to the current129/// exception. If it matches then the exception and type id are passed130/// on to the landing pad. Otherwise the next action is looked up. This131/// chain is terminated with a next action of zero. If no type id is132/// found the frame is unwound and handling continues.133/// 3. Type id table contains references to all the C++ typeinfo for all134/// catches in the function. This tables is reversed indexed base 1.135///136/// Returns the starting symbol of an exception table.137MCSymbol *emitExceptionTable();138139virtual void emitTypeInfos(unsigned TTypeEncoding, MCSymbol *TTBaseLabel);140141// Helpers for identifying what kind of clause an EH typeid or selector142// corresponds to. Negative selectors are for filter clauses, the zero143// selector is for cleanups, and positive selectors are for catch clauses.144static bool isFilterEHSelector(int Selector) { return Selector < 0; }145static bool isCleanupEHSelector(int Selector) { return Selector == 0; }146static bool isCatchEHSelector(int Selector) { return Selector > 0; }147148public:149EHStreamer(AsmPrinter *A);150~EHStreamer() override;151152/// Return `true' if this is a call to a function marked `nounwind'. Return153/// `false' otherwise.154static bool callToNoUnwindFunction(const MachineInstr *MI);155};156157} // end namespace llvm158159#endif // LLVM_LIB_CODEGEN_ASMPRINTER_EHSTREAMER_H160161162