1 //===-------- MipsELFStreamer.cpp - ELF 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 #include "MipsELFStreamer.h"
10 #include "MipsOptionRecord.h"
11 #include "MipsTargetStreamer.h"
12 #include "llvm/BinaryFormat/ELF.h"
13 #include "llvm/MC/MCAsmBackend.h"
14 #include "llvm/MC/MCAssembler.h"
15 #include "llvm/MC/MCCodeEmitter.h"
16 #include "llvm/MC/MCContext.h"
17 #include "llvm/MC/MCDwarf.h"
18 #include "llvm/MC/MCInst.h"
19 #include "llvm/MC/MCObjectWriter.h"
20 #include "llvm/MC/MCSymbolELF.h"
21 #include "llvm/Support/Casting.h"
22 
23 using namespace llvm;
24 
MipsELFStreamer(MCContext & Context,std::unique_ptr<MCAsmBackend> MAB,std::unique_ptr<MCObjectWriter> OW,std::unique_ptr<MCCodeEmitter> Emitter)25 MipsELFStreamer::MipsELFStreamer(MCContext &Context,
26                                  std::unique_ptr<MCAsmBackend> MAB,
27                                  std::unique_ptr<MCObjectWriter> OW,
28                                  std::unique_ptr<MCCodeEmitter> Emitter)
29     : MCELFStreamer(Context, std::move(MAB), std::move(OW),
30                     std::move(Emitter)) {
31   RegInfoRecord = new MipsRegInfoRecord(this, Context);
32   MipsOptionRecords.push_back(
33       std::unique_ptr<MipsRegInfoRecord>(RegInfoRecord));
34 }
35 
emitInstruction(const MCInst & Inst,const MCSubtargetInfo & STI)36 void MipsELFStreamer::emitInstruction(const MCInst &Inst,
37                                       const MCSubtargetInfo &STI) {
38   MCELFStreamer::emitInstruction(Inst, STI);
39 
40   MCContext &Context = getContext();
41   const MCRegisterInfo *MCRegInfo = Context.getRegisterInfo();
42 
43   for (unsigned OpIndex = 0; OpIndex < Inst.getNumOperands(); ++OpIndex) {
44     const MCOperand &Op = Inst.getOperand(OpIndex);
45 
46     if (!Op.isReg())
47       continue;
48 
49     unsigned Reg = Op.getReg();
50     RegInfoRecord->SetPhysRegUsed(Reg, MCRegInfo);
51   }
52 
53   createPendingLabelRelocs();
54 }
55 
emitCFIStartProcImpl(MCDwarfFrameInfo & Frame)56 void MipsELFStreamer::emitCFIStartProcImpl(MCDwarfFrameInfo &Frame) {
57   Frame.Begin = getContext().createTempSymbol();
58   MCELFStreamer::emitLabel(Frame.Begin);
59 }
60 
emitCFILabel()61 MCSymbol *MipsELFStreamer::emitCFILabel() {
62   MCSymbol *Label = getContext().createTempSymbol("cfi", true);
63   MCELFStreamer::emitLabel(Label);
64   return Label;
65 }
66 
emitCFIEndProcImpl(MCDwarfFrameInfo & Frame)67 void MipsELFStreamer::emitCFIEndProcImpl(MCDwarfFrameInfo &Frame) {
68   Frame.End = getContext().createTempSymbol();
69   MCELFStreamer::emitLabel(Frame.End);
70 }
71 
createPendingLabelRelocs()72 void MipsELFStreamer::createPendingLabelRelocs() {
73   MipsTargetELFStreamer *ELFTargetStreamer =
74       static_cast<MipsTargetELFStreamer *>(getTargetStreamer());
75 
76   // FIXME: Also mark labels when in MIPS16 mode.
77   if (ELFTargetStreamer->isMicroMipsEnabled()) {
78     for (auto *L : Labels) {
79       auto *Label = cast<MCSymbolELF>(L);
80       getAssembler().registerSymbol(*Label);
81       Label->setOther(ELF::STO_MIPS_MICROMIPS);
82     }
83   }
84 
85   Labels.clear();
86 }
87 
emitLabel(MCSymbol * Symbol,SMLoc Loc)88 void MipsELFStreamer::emitLabel(MCSymbol *Symbol, SMLoc Loc) {
89   MCELFStreamer::emitLabel(Symbol);
90   Labels.push_back(Symbol);
91 }
92 
SwitchSection(MCSection * Section,const MCExpr * Subsection)93 void MipsELFStreamer::SwitchSection(MCSection *Section,
94                                     const MCExpr *Subsection) {
95   MCELFStreamer::SwitchSection(Section, Subsection);
96   Labels.clear();
97 }
98 
emitValueImpl(const MCExpr * Value,unsigned Size,SMLoc Loc)99 void MipsELFStreamer::emitValueImpl(const MCExpr *Value, unsigned Size,
100                                     SMLoc Loc) {
101   MCELFStreamer::emitValueImpl(Value, Size, Loc);
102   Labels.clear();
103 }
104 
emitIntValue(uint64_t Value,unsigned Size)105 void MipsELFStreamer::emitIntValue(uint64_t Value, unsigned Size) {
106   MCELFStreamer::emitIntValue(Value, Size);
107   Labels.clear();
108 }
109 
EmitMipsOptionRecords()110 void MipsELFStreamer::EmitMipsOptionRecords() {
111   for (const auto &I : MipsOptionRecords)
112     I->EmitMipsOptionRecord();
113 }
114 
createMipsELFStreamer(MCContext & Context,std::unique_ptr<MCAsmBackend> MAB,std::unique_ptr<MCObjectWriter> OW,std::unique_ptr<MCCodeEmitter> Emitter,bool RelaxAll)115 MCELFStreamer *llvm::createMipsELFStreamer(
116     MCContext &Context, std::unique_ptr<MCAsmBackend> MAB,
117     std::unique_ptr<MCObjectWriter> OW, std::unique_ptr<MCCodeEmitter> Emitter,
118     bool RelaxAll) {
119   return new MipsELFStreamer(Context, std::move(MAB), std::move(OW),
120                              std::move(Emitter));
121 }
122