1 //===-- llvm/MC/MCSPIRVObjectWriter.h - SPIR-V Object Writer -----*- 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 #ifndef LLVM_MC_MCSPIRVOBJECTWRITER_H 10 #define LLVM_MC_MCSPIRVOBJECTWRITER_H 11 12 #include "llvm/MC/MCObjectWriter.h" 13 #include "llvm/MC/MCValue.h" 14 #include "llvm/Support/EndianStream.h" 15 #include "llvm/Support/raw_ostream.h" 16 #include <memory> 17 18 namespace llvm { 19 20 class MCSPIRVObjectTargetWriter : public MCObjectTargetWriter { 21 protected: MCSPIRVObjectTargetWriter()22 explicit MCSPIRVObjectTargetWriter() {} 23 24 public: getFormat()25 Triple::ObjectFormatType getFormat() const override { return Triple::SPIRV; } classof(const MCObjectTargetWriter * W)26 static bool classof(const MCObjectTargetWriter *W) { 27 return W->getFormat() == Triple::SPIRV; 28 } 29 }; 30 31 class SPIRVObjectWriter final : public MCObjectWriter { 32 support::endian::Writer W; 33 std::unique_ptr<MCSPIRVObjectTargetWriter> TargetObjectWriter; 34 35 struct VersionInfoType { 36 unsigned Major = 0; 37 unsigned Minor = 0; 38 unsigned Bound = 0; 39 } VersionInfo; 40 41 public: SPIRVObjectWriter(std::unique_ptr<MCSPIRVObjectTargetWriter> MOTW,raw_pwrite_stream & OS)42 SPIRVObjectWriter(std::unique_ptr<MCSPIRVObjectTargetWriter> MOTW, 43 raw_pwrite_stream &OS) 44 : W(OS, llvm::endianness::little), TargetObjectWriter(std::move(MOTW)) {} 45 46 void setBuildVersion(unsigned Major, unsigned Minor, unsigned Bound); 47 48 private: recordRelocation(MCAssembler & Asm,const MCFragment * Fragment,const MCFixup & Fixup,MCValue Target,uint64_t & FixedValue)49 void recordRelocation(MCAssembler &Asm, const MCFragment *Fragment, 50 const MCFixup &Fixup, MCValue Target, 51 uint64_t &FixedValue) override {} 52 53 uint64_t writeObject(MCAssembler &Asm) override; 54 void writeHeader(const MCAssembler &Asm); 55 }; 56 57 /// Construct a new SPIR-V writer instance. 58 /// 59 /// \param MOTW - The target specific SPIR-V writer subclass. 60 /// \param OS - The stream to write to. 61 /// \returns The constructed object writer. 62 std::unique_ptr<MCObjectWriter> 63 createSPIRVObjectWriter(std::unique_ptr<MCSPIRVObjectTargetWriter> MOTW, 64 raw_pwrite_stream &OS); 65 66 } // namespace llvm 67 68 #endif 69