1 //===- lib/MC/MCXCOFFStreamer.cpp - XCOFF Object Output -------------------===// 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 // This file assembles .s files and emits XCOFF .o object files. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/BinaryFormat/XCOFF.h" 14 #include "llvm/MC/MCAsmBackend.h" 15 #include "llvm/MC/MCCodeEmitter.h" 16 #include "llvm/MC/MCObjectWriter.h" 17 #include "llvm/MC/MCSymbolXCOFF.h" 18 #include "llvm/MC/MCXCOFFStreamer.h" 19 #include "llvm/Support/TargetRegistry.h" 20 21 using namespace llvm; 22 23 MCXCOFFStreamer::MCXCOFFStreamer(MCContext &Context, 24 std::unique_ptr<MCAsmBackend> MAB, 25 std::unique_ptr<MCObjectWriter> OW, 26 std::unique_ptr<MCCodeEmitter> Emitter) 27 : MCObjectStreamer(Context, std::move(MAB), std::move(OW), 28 std::move(Emitter)) {} 29 30 bool MCXCOFFStreamer::EmitSymbolAttribute(MCSymbol *Sym, 31 MCSymbolAttr Attribute) { 32 auto *Symbol = cast<MCSymbolXCOFF>(Sym); 33 getAssembler().registerSymbol(*Symbol); 34 35 switch (Attribute) { 36 case MCSA_Global: 37 Symbol->setStorageClass(XCOFF::C_EXT); 38 Symbol->setExternal(true); 39 break; 40 default: 41 report_fatal_error("Not implemented yet."); 42 } 43 return true; 44 } 45 46 void MCXCOFFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, 47 unsigned ByteAlignment) { 48 getAssembler().registerSymbol(*Symbol); 49 Symbol->setExternal(cast<MCSymbolXCOFF>(Symbol)->getStorageClass() != 50 XCOFF::C_HIDEXT); 51 Symbol->setCommon(Size, ByteAlignment); 52 53 // Emit the alignment and storage for the variable to the section. 54 EmitValueToAlignment(ByteAlignment); 55 EmitZeros(Size); 56 } 57 58 void MCXCOFFStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol, 59 uint64_t Size, unsigned ByteAlignment, 60 SMLoc Loc) { 61 report_fatal_error("Zero fill not implemented for XCOFF."); 62 } 63 64 void MCXCOFFStreamer::EmitInstToData(const MCInst &Inst, 65 const MCSubtargetInfo &STI) { 66 MCAssembler &Assembler = getAssembler(); 67 SmallVector<MCFixup, 4> Fixups; 68 SmallString<256> Code; 69 raw_svector_ostream VecOS(Code); 70 Assembler.getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI); 71 72 // TODO: Handle Fixups later 73 74 MCDataFragment *DF = getOrCreateDataFragment(&STI); 75 DF->setHasInstructions(STI); 76 DF->getContents().append(Code.begin(), Code.end()); 77 } 78 79 MCStreamer *llvm::createXCOFFStreamer(MCContext &Context, 80 std::unique_ptr<MCAsmBackend> &&MAB, 81 std::unique_ptr<MCObjectWriter> &&OW, 82 std::unique_ptr<MCCodeEmitter> &&CE, 83 bool RelaxAll) { 84 MCXCOFFStreamer *S = new MCXCOFFStreamer(Context, std::move(MAB), 85 std::move(OW), std::move(CE)); 86 if (RelaxAll) 87 S->getAssembler().setRelaxAll(true); 88 return S; 89 } 90 91 void MCXCOFFStreamer::EmitXCOFFLocalCommonSymbol(MCSymbol *LabelSym, 92 uint64_t Size, 93 MCSymbol *CsectSym, 94 unsigned ByteAlignment) { 95 EmitCommonSymbol(CsectSym, Size, ByteAlignment); 96 } 97