Home | History | Annotate | Download | only in AsmPrinter
      1 //===-- CodeGen/AsmPrinter/Win64Exception.cpp - Dwarf Exception Impl ------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file contains support for writing Win64 exception info into asm files.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #include "DwarfException.h"
     15 #include "llvm/Module.h"
     16 #include "llvm/CodeGen/AsmPrinter.h"
     17 #include "llvm/CodeGen/MachineModuleInfo.h"
     18 #include "llvm/CodeGen/MachineFrameInfo.h"
     19 #include "llvm/CodeGen/MachineFunction.h"
     20 #include "llvm/MC/MCAsmInfo.h"
     21 #include "llvm/MC/MCContext.h"
     22 #include "llvm/MC/MCExpr.h"
     23 #include "llvm/MC/MCSection.h"
     24 #include "llvm/MC/MCStreamer.h"
     25 #include "llvm/MC/MCSymbol.h"
     26 #include "llvm/Target/Mangler.h"
     27 #include "llvm/Target/TargetData.h"
     28 #include "llvm/Target/TargetFrameLowering.h"
     29 #include "llvm/Target/TargetLoweringObjectFile.h"
     30 #include "llvm/Target/TargetMachine.h"
     31 #include "llvm/Target/TargetOptions.h"
     32 #include "llvm/Target/TargetRegisterInfo.h"
     33 #include "llvm/Support/Dwarf.h"
     34 #include "llvm/Support/ErrorHandling.h"
     35 #include "llvm/Support/FormattedStream.h"
     36 #include "llvm/ADT/SmallString.h"
     37 #include "llvm/ADT/StringExtras.h"
     38 #include "llvm/ADT/Twine.h"
     39 using namespace llvm;
     40 
     41 Win64Exception::Win64Exception(AsmPrinter *A)
     42   : DwarfException(A),
     43     shouldEmitPersonality(false), shouldEmitLSDA(false), shouldEmitMoves(false)
     44     {}
     45 
     46 Win64Exception::~Win64Exception() {}
     47 
     48 /// EndModule - Emit all exception information that should come after the
     49 /// content.
     50 void Win64Exception::EndModule() {
     51 }
     52 
     53 /// BeginFunction - Gather pre-function exception information. Assumes it's
     54 /// being emitted immediately after the function entry point.
     55 void Win64Exception::BeginFunction(const MachineFunction *MF) {
     56   shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false;
     57 
     58   // If any landing pads survive, we need an EH table.
     59   bool hasLandingPads = !MMI->getLandingPads().empty();
     60 
     61   shouldEmitMoves = Asm->needsSEHMoves();
     62 
     63   const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
     64   unsigned PerEncoding = TLOF.getPersonalityEncoding();
     65   const Function *Per = MMI->getPersonalities()[MMI->getPersonalityIndex()];
     66 
     67   shouldEmitPersonality = hasLandingPads &&
     68     PerEncoding != dwarf::DW_EH_PE_omit && Per;
     69 
     70   unsigned LSDAEncoding = TLOF.getLSDAEncoding();
     71   shouldEmitLSDA = shouldEmitPersonality &&
     72     LSDAEncoding != dwarf::DW_EH_PE_omit;
     73 
     74   if (!shouldEmitPersonality && !shouldEmitMoves)
     75     return;
     76 
     77   Asm->OutStreamer.EmitWin64EHStartProc(Asm->CurrentFnSym);
     78 
     79   if (!shouldEmitPersonality)
     80     return;
     81 
     82   MCSymbol *GCCHandlerSym =
     83     Asm->GetExternalSymbolSymbol("_GCC_specific_handler");
     84   Asm->OutStreamer.EmitWin64EHHandler(GCCHandlerSym, true, true);
     85 
     86   Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("eh_func_begin",
     87                                                 Asm->getFunctionNumber()));
     88 }
     89 
     90 /// EndFunction - Gather and emit post-function exception information.
     91 ///
     92 void Win64Exception::EndFunction() {
     93   if (!shouldEmitPersonality && !shouldEmitMoves)
     94     return;
     95 
     96   Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("eh_func_end",
     97                                                 Asm->getFunctionNumber()));
     98 
     99   // Map all labels and get rid of any dead landing pads.
    100   MMI->TidyLandingPads();
    101 
    102   if (shouldEmitPersonality) {
    103     const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
    104     const Function *Per = MMI->getPersonalities()[MMI->getPersonalityIndex()];
    105     const MCSymbol *Sym = TLOF.getCFIPersonalitySymbol(Per, Asm->Mang, MMI);
    106 
    107     Asm->OutStreamer.PushSection();
    108     Asm->OutStreamer.EmitWin64EHHandlerData();
    109     Asm->OutStreamer.EmitValue(MCSymbolRefExpr::Create(Sym, Asm->OutContext),
    110                                4);
    111     EmitExceptionTable();
    112     Asm->OutStreamer.PopSection();
    113   }
    114   Asm->OutStreamer.EmitWin64EHEndProc();
    115 }
    116