1 //===- llvm/MC/MCSPIRVObjectWriter.cpp - 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 #include "llvm/MC/MCAssembler.h" 10 #include "llvm/MC/MCSPIRVObjectWriter.h" 11 #include "llvm/MC/MCSection.h" 12 #include "llvm/MC/MCValue.h" 13 #include "llvm/Support/EndianStream.h" 14 15 using namespace llvm; 16 17 void SPIRVObjectWriter::writeHeader(const MCAssembler &Asm) { 18 constexpr uint32_t MagicNumber = 0x07230203; 19 constexpr uint32_t GeneratorID = 43; 20 constexpr uint32_t GeneratorMagicNumber = 21 (GeneratorID << 16) | (LLVM_VERSION_MAJOR); 22 constexpr uint32_t Schema = 0; 23 24 W.write<uint32_t>(MagicNumber); 25 W.write<uint32_t>((VersionInfo.Major << 16) | (VersionInfo.Minor << 8)); 26 W.write<uint32_t>(GeneratorMagicNumber); 27 W.write<uint32_t>(VersionInfo.Bound); 28 W.write<uint32_t>(Schema); 29 } 30 31 void SPIRVObjectWriter::setBuildVersion(unsigned Major, unsigned Minor, 32 unsigned Bound) { 33 VersionInfo.Major = Major; 34 VersionInfo.Minor = Minor; 35 VersionInfo.Bound = Bound; 36 } 37 38 uint64_t SPIRVObjectWriter::writeObject(MCAssembler &Asm) { 39 uint64_t StartOffset = W.OS.tell(); 40 writeHeader(Asm); 41 for (const MCSection &S : Asm) 42 Asm.writeSectionData(W.OS, &S); 43 return W.OS.tell() - StartOffset; 44 } 45 46 std::unique_ptr<MCObjectWriter> 47 llvm::createSPIRVObjectWriter(std::unique_ptr<MCSPIRVObjectTargetWriter> MOTW, 48 raw_pwrite_stream &OS) { 49 return std::make_unique<SPIRVObjectWriter>(std::move(MOTW), OS); 50 } 51