xref: /llvm-project/llvm/lib/Target/DirectX/DirectXAsmPrinter.cpp (revision 9df71d7673b5c98e1032d01be83724a45b42fafc)
13942f8e4SChris Bieneman //===-- DirectXAsmPrinter.cpp - DirectX assembly writer --------*- C++ -*--===//
23942f8e4SChris Bieneman //
33942f8e4SChris Bieneman // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
43942f8e4SChris Bieneman // See https://llvm.org/LICENSE.txt for license information.
53942f8e4SChris Bieneman // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
63942f8e4SChris Bieneman //
73942f8e4SChris Bieneman //===----------------------------------------------------------------------===//
83942f8e4SChris Bieneman //
93942f8e4SChris Bieneman // This file contains AsmPrinters for the DirectX backend.
103942f8e4SChris Bieneman //
113942f8e4SChris Bieneman //===----------------------------------------------------------------------===//
123942f8e4SChris Bieneman 
133942f8e4SChris Bieneman #include "TargetInfo/DirectXTargetInfo.h"
143942f8e4SChris Bieneman #include "llvm/CodeGen/AsmPrinter.h"
153942f8e4SChris Bieneman #include "llvm/IR/GlobalVariable.h"
163942f8e4SChris Bieneman #include "llvm/IR/Module.h"
173942f8e4SChris Bieneman #include "llvm/MC/MCStreamer.h"
183942f8e4SChris Bieneman #include "llvm/MC/SectionKind.h"
193942f8e4SChris Bieneman #include "llvm/MC/TargetRegistry.h"
203942f8e4SChris Bieneman #include "llvm/Target/TargetLoweringObjectFile.h"
213942f8e4SChris Bieneman 
223942f8e4SChris Bieneman using namespace llvm;
233942f8e4SChris Bieneman 
243942f8e4SChris Bieneman #define DEBUG_TYPE "asm-printer"
253942f8e4SChris Bieneman 
263942f8e4SChris Bieneman namespace {
273942f8e4SChris Bieneman 
283942f8e4SChris Bieneman // The DXILAsmPrinter is mostly a stub because DXIL is just LLVM bitcode which
293942f8e4SChris Bieneman // gets embedded into a DXContainer file.
303942f8e4SChris Bieneman class DXILAsmPrinter : public AsmPrinter {
313942f8e4SChris Bieneman public:
DXILAsmPrinter(TargetMachine & TM,std::unique_ptr<MCStreamer> Streamer)323942f8e4SChris Bieneman   explicit DXILAsmPrinter(TargetMachine &TM,
333942f8e4SChris Bieneman                           std::unique_ptr<MCStreamer> Streamer)
343942f8e4SChris Bieneman       : AsmPrinter(TM, std::move(Streamer)) {}
353942f8e4SChris Bieneman 
getPassName() const363942f8e4SChris Bieneman   StringRef getPassName() const override { return "DXIL Assembly Printer"; }
373942f8e4SChris Bieneman   void emitGlobalVariable(const GlobalVariable *GV) override;
runOnMachineFunction(MachineFunction & MF)383942f8e4SChris Bieneman   bool runOnMachineFunction(MachineFunction &MF) override { return false; }
393942f8e4SChris Bieneman };
403942f8e4SChris Bieneman } // namespace
413942f8e4SChris Bieneman 
emitGlobalVariable(const GlobalVariable * GV)423942f8e4SChris Bieneman void DXILAsmPrinter::emitGlobalVariable(const GlobalVariable *GV) {
43ce0bb316SChris Bieneman   // If there is no initializer, or no explicit section do nothing
44ce0bb316SChris Bieneman   if (!GV->hasInitializer() || GV->hasImplicitSection() || !GV->hasSection())
453942f8e4SChris Bieneman     return;
463942f8e4SChris Bieneman   // Skip the LLVM metadata
473942f8e4SChris Bieneman   if (GV->getSection() == "llvm.metadata")
483942f8e4SChris Bieneman     return;
493942f8e4SChris Bieneman   SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, TM);
503942f8e4SChris Bieneman   MCSection *TheSection = getObjFileLowering().SectionForGlobal(GV, GVKind, TM);
513942f8e4SChris Bieneman   OutStreamer->switchSection(TheSection);
52*9df71d76SNikita Popov   emitGlobalConstant(GV->getDataLayout(), GV->getInitializer());
533942f8e4SChris Bieneman }
543942f8e4SChris Bieneman 
LLVMInitializeDirectXAsmPrinter()553942f8e4SChris Bieneman extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeDirectXAsmPrinter() {
563942f8e4SChris Bieneman   RegisterAsmPrinter<DXILAsmPrinter> X(getTheDirectXTarget());
573942f8e4SChris Bieneman }
58