blob: 21b01e8509678ffac52fd1626143667b9edc4c21 [file] [log] [blame]
Matheus Almeidadac77fb2014-03-27 11:39:03 +00001//===-------- MipsELFStreamer.cpp - ELF Object Output ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "MipsELFStreamer.h"
Eugene Zelenkocd8ea022017-02-03 23:39:33 +000011#include "MipsOptionRecord.h"
Zoran Jovanovic9c654832014-11-05 16:35:20 +000012#include "MipsTargetStreamer.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000013#include "llvm/BinaryFormat/ELF.h"
Lang Hames02d33052017-10-11 01:57:21 +000014#include "llvm/MC/MCAsmBackend.h"
Eugene Zelenkocd8ea022017-02-03 23:39:33 +000015#include "llvm/MC/MCAssembler.h"
Lang Hames2241ffa2017-10-11 23:34:47 +000016#include "llvm/MC/MCCodeEmitter.h"
Eugene Zelenkocd8ea022017-02-03 23:39:33 +000017#include "llvm/MC/MCContext.h"
Aleksandar Beserminji81eb4402018-10-15 14:39:12 +000018#include "llvm/MC/MCDwarf.h"
Daniel Sanders68c37472014-07-21 13:30:55 +000019#include "llvm/MC/MCInst.h"
Peter Collingbournef7b81db2018-05-18 18:26:45 +000020#include "llvm/MC/MCObjectWriter.h"
Rafael Espindola95fb9b92015-06-02 20:38:46 +000021#include "llvm/MC/MCSymbolELF.h"
Eugene Zelenkocd8ea022017-02-03 23:39:33 +000022#include "llvm/Support/Casting.h"
Zoran Jovanovic9c654832014-11-05 16:35:20 +000023
Benjamin Kramer89854eb2014-09-03 21:04:12 +000024using namespace llvm;
Daniel Sanders68c37472014-07-21 13:30:55 +000025
Lang Hames02d33052017-10-11 01:57:21 +000026MipsELFStreamer::MipsELFStreamer(MCContext &Context,
27 std::unique_ptr<MCAsmBackend> MAB,
Peter Collingbournef7b81db2018-05-18 18:26:45 +000028 std::unique_ptr<MCObjectWriter> OW,
Lang Hames2241ffa2017-10-11 23:34:47 +000029 std::unique_ptr<MCCodeEmitter> Emitter)
Peter Collingbournef7b81db2018-05-18 18:26:45 +000030 : MCELFStreamer(Context, std::move(MAB), std::move(OW),
31 std::move(Emitter)) {
Lang Hames02d33052017-10-11 01:57:21 +000032 RegInfoRecord = new MipsRegInfoRecord(this, Context);
33 MipsOptionRecords.push_back(
34 std::unique_ptr<MipsRegInfoRecord>(RegInfoRecord));
35}
36
Daniel Sanders68c37472014-07-21 13:30:55 +000037void MipsELFStreamer::EmitInstruction(const MCInst &Inst,
Andrew V. Tischenko75745d02017-04-14 07:44:23 +000038 const MCSubtargetInfo &STI, bool) {
Daniel Sanders68c37472014-07-21 13:30:55 +000039 MCELFStreamer::EmitInstruction(Inst, STI);
40
41 MCContext &Context = getContext();
42 const MCRegisterInfo *MCRegInfo = Context.getRegisterInfo();
43
44 for (unsigned OpIndex = 0; OpIndex < Inst.getNumOperands(); ++OpIndex) {
45 const MCOperand &Op = Inst.getOperand(OpIndex);
46
47 if (!Op.isReg())
48 continue;
49
50 unsigned Reg = Op.getReg();
51 RegInfoRecord->SetPhysRegUsed(Reg, MCRegInfo);
52 }
Zoran Jovanovic9c654832014-11-05 16:35:20 +000053
Toma Tabacu9ca50962015-04-16 09:53:47 +000054 createPendingLabelRelocs();
55}
56
Aleksandar Beserminji81eb4402018-10-15 14:39:12 +000057void MipsELFStreamer::EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame) {
58 Frame.Begin = getContext().createTempSymbol();
59 MCELFStreamer::EmitLabel(Frame.Begin);
60}
61
62MCSymbol *MipsELFStreamer::EmitCFILabel() {
63 MCSymbol *Label = getContext().createTempSymbol("cfi", true);
64 MCELFStreamer::EmitLabel(Label);
65 return Label;
66}
67
68void MipsELFStreamer::EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame) {
69 Frame.End = getContext().createTempSymbol();
70 MCELFStreamer::EmitLabel(Frame.End);
71}
72
Toma Tabacu9ca50962015-04-16 09:53:47 +000073void MipsELFStreamer::createPendingLabelRelocs() {
74 MipsTargetELFStreamer *ELFTargetStreamer =
75 static_cast<MipsTargetELFStreamer *>(getTargetStreamer());
76
77 // FIXME: Also mark labels when in MIPS16 mode.
Zoran Jovanovic9c654832014-11-05 16:35:20 +000078 if (ELFTargetStreamer->isMicroMipsEnabled()) {
Rafael Espindola95fb9b92015-06-02 20:38:46 +000079 for (auto *L : Labels) {
80 auto *Label = cast<MCSymbolELF>(L);
Rafael Espindolac73aed12015-06-03 19:03:11 +000081 getAssembler().registerSymbol(*Label);
Rafael Espindola8c006ee2015-06-04 05:59:23 +000082 Label->setOther(ELF::STO_MIPS_MICROMIPS);
Zoran Jovanovic9c654832014-11-05 16:35:20 +000083 }
84 }
85
86 Labels.clear();
87}
88
Rafael Espindolabe991572017-02-10 15:13:12 +000089void MipsELFStreamer::EmitLabel(MCSymbol *Symbol, SMLoc Loc) {
Zoran Jovanovic9c654832014-11-05 16:35:20 +000090 MCELFStreamer::EmitLabel(Symbol);
91 Labels.push_back(Symbol);
92}
93
Rafael Espindola0709a7b2015-05-21 19:20:38 +000094void MipsELFStreamer::SwitchSection(MCSection *Section,
Zoran Jovanovic9c654832014-11-05 16:35:20 +000095 const MCExpr *Subsection) {
96 MCELFStreamer::SwitchSection(Section, Subsection);
97 Labels.clear();
98}
99
100void MipsELFStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
Craig Topper3c76c522015-09-20 23:35:59 +0000101 SMLoc Loc) {
Zoran Jovanovic9c654832014-11-05 16:35:20 +0000102 MCELFStreamer::EmitValueImpl(Value, Size, Loc);
103 Labels.clear();
Daniel Sanders68c37472014-07-21 13:30:55 +0000104}
105
Simon Atanasyanb5244592018-07-25 07:07:43 +0000106void MipsELFStreamer::EmitIntValue(uint64_t Value, unsigned Size) {
107 MCELFStreamer::EmitIntValue(Value, Size);
108 Labels.clear();
109}
110
Daniel Sanders68c37472014-07-21 13:30:55 +0000111void MipsELFStreamer::EmitMipsOptionRecords() {
112 for (const auto &I : MipsOptionRecords)
113 I->EmitMipsOptionRecord();
114}
Matheus Almeidadac77fb2014-03-27 11:39:03 +0000115
Lang Hames2241ffa2017-10-11 23:34:47 +0000116MCELFStreamer *llvm::createMipsELFStreamer(
117 MCContext &Context, std::unique_ptr<MCAsmBackend> MAB,
Peter Collingbournef7b81db2018-05-18 18:26:45 +0000118 std::unique_ptr<MCObjectWriter> OW, std::unique_ptr<MCCodeEmitter> Emitter,
Lang Hames2241ffa2017-10-11 23:34:47 +0000119 bool RelaxAll) {
Peter Collingbournef7b81db2018-05-18 18:26:45 +0000120 return new MipsELFStreamer(Context, std::move(MAB), std::move(OW),
121 std::move(Emitter));
Matheus Almeidadac77fb2014-03-27 11:39:03 +0000122}