xref: /freebsd/contrib/llvm-project/llvm/lib/Target/X86/MCTargetDesc/X86WinCOFFStreamer.cpp (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 //===-- X86WinCOFFStreamer.cpp - X86 Target WinCOFF Streamer ----*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "X86MCTargetDesc.h"
10 #include "X86TargetStreamer.h"
11 #include "llvm/MC/MCAsmBackend.h"
12 #include "llvm/MC/MCAssembler.h"
13 #include "llvm/MC/MCCodeEmitter.h"
14 #include "llvm/MC/MCObjectWriter.h"
15 #include "llvm/MC/MCWin64EH.h"
16 #include "llvm/MC/MCWinCOFFStreamer.h"
17 
18 using namespace llvm;
19 
20 namespace {
21 class X86WinCOFFStreamer : public MCWinCOFFStreamer {
22   Win64EH::UnwindEmitter EHStreamer;
23 public:
24   X86WinCOFFStreamer(MCContext &C, std::unique_ptr<MCAsmBackend> AB,
25                      std::unique_ptr<MCCodeEmitter> CE,
26                      std::unique_ptr<MCObjectWriter> OW)
27       : MCWinCOFFStreamer(C, std::move(AB), std::move(CE), std::move(OW)) {}
28 
29   void emitWinEHHandlerData(SMLoc Loc) override;
30   void emitWindowsUnwindTables(WinEH::FrameInfo *Frame) override;
31   void emitWindowsUnwindTables() override;
32   void emitCVFPOData(const MCSymbol *ProcSym, SMLoc Loc) override;
33   void finishImpl() override;
34 };
35 
36 void X86WinCOFFStreamer::emitWinEHHandlerData(SMLoc Loc) {
37   MCStreamer::emitWinEHHandlerData(Loc);
38 
39   // We have to emit the unwind info now, because this directive
40   // actually switches to the .xdata section.
41   if (WinEH::FrameInfo *CurFrame = getCurrentWinFrameInfo())
42     EHStreamer.EmitUnwindInfo(*this, CurFrame, /* HandlerData = */ true);
43 }
44 
45 void X86WinCOFFStreamer::emitWindowsUnwindTables(WinEH::FrameInfo *Frame) {
46   EHStreamer.EmitUnwindInfo(*this, Frame, /* HandlerData = */ false);
47 }
48 
49 void X86WinCOFFStreamer::emitWindowsUnwindTables() {
50   if (!getNumWinFrameInfos())
51     return;
52   EHStreamer.Emit(*this);
53 }
54 
55 void X86WinCOFFStreamer::emitCVFPOData(const MCSymbol *ProcSym, SMLoc Loc) {
56   X86TargetStreamer *XTS =
57       static_cast<X86TargetStreamer *>(getTargetStreamer());
58   XTS->emitFPOData(ProcSym, Loc);
59 }
60 
61 void X86WinCOFFStreamer::finishImpl() {
62   emitFrames(nullptr);
63   emitWindowsUnwindTables();
64 
65   MCWinCOFFStreamer::finishImpl();
66 }
67 } // namespace
68 
69 MCStreamer *llvm::createX86WinCOFFStreamer(MCContext &C,
70                                            std::unique_ptr<MCAsmBackend> &&AB,
71                                            std::unique_ptr<MCObjectWriter> &&OW,
72                                            std::unique_ptr<MCCodeEmitter> &&CE,
73                                            bool RelaxAll,
74                                            bool IncrementalLinkerCompatible) {
75   X86WinCOFFStreamer *S =
76       new X86WinCOFFStreamer(C, std::move(AB), std::move(CE), std::move(OW));
77   S->getAssembler().setRelaxAll(RelaxAll);
78   S->getAssembler().setIncrementalLinkerCompatible(IncrementalLinkerCompatible);
79   return S;
80 }
81 
82