1*d415bd75Srobert //===-- DirectXAsmPrinter.cpp - DirectX assembly writer --------*- C++ -*--===//
2*d415bd75Srobert //
3*d415bd75Srobert // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*d415bd75Srobert // See https://llvm.org/LICENSE.txt for license information.
5*d415bd75Srobert // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*d415bd75Srobert //
7*d415bd75Srobert //===----------------------------------------------------------------------===//
8*d415bd75Srobert //
9*d415bd75Srobert // This file contains AsmPrinters for the DirectX backend.
10*d415bd75Srobert //
11*d415bd75Srobert //===----------------------------------------------------------------------===//
12*d415bd75Srobert 
13*d415bd75Srobert #include "TargetInfo/DirectXTargetInfo.h"
14*d415bd75Srobert #include "llvm/CodeGen/AsmPrinter.h"
15*d415bd75Srobert #include "llvm/IR/GlobalVariable.h"
16*d415bd75Srobert #include "llvm/IR/Module.h"
17*d415bd75Srobert #include "llvm/MC/MCStreamer.h"
18*d415bd75Srobert #include "llvm/MC/SectionKind.h"
19*d415bd75Srobert #include "llvm/MC/TargetRegistry.h"
20*d415bd75Srobert #include "llvm/Target/TargetLoweringObjectFile.h"
21*d415bd75Srobert 
22*d415bd75Srobert using namespace llvm;
23*d415bd75Srobert 
24*d415bd75Srobert #define DEBUG_TYPE "asm-printer"
25*d415bd75Srobert 
26*d415bd75Srobert namespace {
27*d415bd75Srobert 
28*d415bd75Srobert // The DXILAsmPrinter is mostly a stub because DXIL is just LLVM bitcode which
29*d415bd75Srobert // gets embedded into a DXContainer file.
30*d415bd75Srobert class DXILAsmPrinter : public AsmPrinter {
31*d415bd75Srobert public:
DXILAsmPrinter(TargetMachine & TM,std::unique_ptr<MCStreamer> Streamer)32*d415bd75Srobert   explicit DXILAsmPrinter(TargetMachine &TM,
33*d415bd75Srobert                           std::unique_ptr<MCStreamer> Streamer)
34*d415bd75Srobert       : AsmPrinter(TM, std::move(Streamer)) {}
35*d415bd75Srobert 
getPassName() const36*d415bd75Srobert   StringRef getPassName() const override { return "DXIL Assembly Printer"; }
37*d415bd75Srobert   void emitGlobalVariable(const GlobalVariable *GV) override;
runOnMachineFunction(MachineFunction & MF)38*d415bd75Srobert   bool runOnMachineFunction(MachineFunction &MF) override { return false; }
39*d415bd75Srobert };
40*d415bd75Srobert } // namespace
41*d415bd75Srobert 
emitGlobalVariable(const GlobalVariable * GV)42*d415bd75Srobert void DXILAsmPrinter::emitGlobalVariable(const GlobalVariable *GV) {
43*d415bd75Srobert   // If there is no initializer, or no explicit section do nothing
44*d415bd75Srobert   if (!GV->hasInitializer() || GV->hasImplicitSection() || !GV->hasSection())
45*d415bd75Srobert     return;
46*d415bd75Srobert   // Skip the LLVM metadata
47*d415bd75Srobert   if (GV->getSection() == "llvm.metadata")
48*d415bd75Srobert     return;
49*d415bd75Srobert   SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, TM);
50*d415bd75Srobert   MCSection *TheSection = getObjFileLowering().SectionForGlobal(GV, GVKind, TM);
51*d415bd75Srobert   OutStreamer->switchSection(TheSection);
52*d415bd75Srobert   emitGlobalConstant(GV->getParent()->getDataLayout(), GV->getInitializer());
53*d415bd75Srobert }
54*d415bd75Srobert 
LLVMInitializeDirectXAsmPrinter()55*d415bd75Srobert extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeDirectXAsmPrinter() {
56*d415bd75Srobert   RegisterAsmPrinter<DXILAsmPrinter> X(getTheDirectXTarget());
57*d415bd75Srobert }
58