Matheus Almeida | dac77fb | 2014-03-27 11:39:03 +0000 | [diff] [blame] | 1 | //===-------- 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 Zelenko | cd8ea02 | 2017-02-03 23:39:33 +0000 | [diff] [blame] | 11 | #include "MipsOptionRecord.h" |
Zoran Jovanovic | 9c65483 | 2014-11-05 16:35:20 +0000 | [diff] [blame] | 12 | #include "MipsTargetStreamer.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 13 | #include "llvm/BinaryFormat/ELF.h" |
Lang Hames | 02d3305 | 2017-10-11 01:57:21 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCAsmBackend.h" |
Eugene Zelenko | cd8ea02 | 2017-02-03 23:39:33 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCAssembler.h" |
Lang Hames | 2241ffa | 2017-10-11 23:34:47 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCCodeEmitter.h" |
Eugene Zelenko | cd8ea02 | 2017-02-03 23:39:33 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCContext.h" |
Aleksandar Beserminji | 81eb440 | 2018-10-15 14:39:12 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCDwarf.h" |
Daniel Sanders | 68c3747 | 2014-07-21 13:30:55 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCInst.h" |
Peter Collingbourne | f7b81db | 2018-05-18 18:26:45 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCObjectWriter.h" |
Rafael Espindola | 95fb9b9 | 2015-06-02 20:38:46 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCSymbolELF.h" |
Eugene Zelenko | cd8ea02 | 2017-02-03 23:39:33 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Casting.h" |
Zoran Jovanovic | 9c65483 | 2014-11-05 16:35:20 +0000 | [diff] [blame] | 23 | |
Benjamin Kramer | 89854eb | 2014-09-03 21:04:12 +0000 | [diff] [blame] | 24 | using namespace llvm; |
Daniel Sanders | 68c3747 | 2014-07-21 13:30:55 +0000 | [diff] [blame] | 25 | |
Lang Hames | 02d3305 | 2017-10-11 01:57:21 +0000 | [diff] [blame] | 26 | MipsELFStreamer::MipsELFStreamer(MCContext &Context, |
| 27 | std::unique_ptr<MCAsmBackend> MAB, |
Peter Collingbourne | f7b81db | 2018-05-18 18:26:45 +0000 | [diff] [blame] | 28 | std::unique_ptr<MCObjectWriter> OW, |
Lang Hames | 2241ffa | 2017-10-11 23:34:47 +0000 | [diff] [blame] | 29 | std::unique_ptr<MCCodeEmitter> Emitter) |
Peter Collingbourne | f7b81db | 2018-05-18 18:26:45 +0000 | [diff] [blame] | 30 | : MCELFStreamer(Context, std::move(MAB), std::move(OW), |
| 31 | std::move(Emitter)) { |
Lang Hames | 02d3305 | 2017-10-11 01:57:21 +0000 | [diff] [blame] | 32 | RegInfoRecord = new MipsRegInfoRecord(this, Context); |
| 33 | MipsOptionRecords.push_back( |
| 34 | std::unique_ptr<MipsRegInfoRecord>(RegInfoRecord)); |
| 35 | } |
| 36 | |
Daniel Sanders | 68c3747 | 2014-07-21 13:30:55 +0000 | [diff] [blame] | 37 | void MipsELFStreamer::EmitInstruction(const MCInst &Inst, |
Andrew V. Tischenko | 75745d0 | 2017-04-14 07:44:23 +0000 | [diff] [blame] | 38 | const MCSubtargetInfo &STI, bool) { |
Daniel Sanders | 68c3747 | 2014-07-21 13:30:55 +0000 | [diff] [blame] | 39 | 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 Jovanovic | 9c65483 | 2014-11-05 16:35:20 +0000 | [diff] [blame] | 53 | |
Toma Tabacu | 9ca5096 | 2015-04-16 09:53:47 +0000 | [diff] [blame] | 54 | createPendingLabelRelocs(); |
| 55 | } |
| 56 | |
Aleksandar Beserminji | 81eb440 | 2018-10-15 14:39:12 +0000 | [diff] [blame] | 57 | void MipsELFStreamer::EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame) { |
| 58 | Frame.Begin = getContext().createTempSymbol(); |
| 59 | MCELFStreamer::EmitLabel(Frame.Begin); |
| 60 | } |
| 61 | |
| 62 | MCSymbol *MipsELFStreamer::EmitCFILabel() { |
| 63 | MCSymbol *Label = getContext().createTempSymbol("cfi", true); |
| 64 | MCELFStreamer::EmitLabel(Label); |
| 65 | return Label; |
| 66 | } |
| 67 | |
| 68 | void MipsELFStreamer::EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame) { |
| 69 | Frame.End = getContext().createTempSymbol(); |
| 70 | MCELFStreamer::EmitLabel(Frame.End); |
| 71 | } |
| 72 | |
Toma Tabacu | 9ca5096 | 2015-04-16 09:53:47 +0000 | [diff] [blame] | 73 | void MipsELFStreamer::createPendingLabelRelocs() { |
| 74 | MipsTargetELFStreamer *ELFTargetStreamer = |
| 75 | static_cast<MipsTargetELFStreamer *>(getTargetStreamer()); |
| 76 | |
| 77 | // FIXME: Also mark labels when in MIPS16 mode. |
Zoran Jovanovic | 9c65483 | 2014-11-05 16:35:20 +0000 | [diff] [blame] | 78 | if (ELFTargetStreamer->isMicroMipsEnabled()) { |
Rafael Espindola | 95fb9b9 | 2015-06-02 20:38:46 +0000 | [diff] [blame] | 79 | for (auto *L : Labels) { |
| 80 | auto *Label = cast<MCSymbolELF>(L); |
Rafael Espindola | c73aed1 | 2015-06-03 19:03:11 +0000 | [diff] [blame] | 81 | getAssembler().registerSymbol(*Label); |
Rafael Espindola | 8c006ee | 2015-06-04 05:59:23 +0000 | [diff] [blame] | 82 | Label->setOther(ELF::STO_MIPS_MICROMIPS); |
Zoran Jovanovic | 9c65483 | 2014-11-05 16:35:20 +0000 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
| 86 | Labels.clear(); |
| 87 | } |
| 88 | |
Rafael Espindola | be99157 | 2017-02-10 15:13:12 +0000 | [diff] [blame] | 89 | void MipsELFStreamer::EmitLabel(MCSymbol *Symbol, SMLoc Loc) { |
Zoran Jovanovic | 9c65483 | 2014-11-05 16:35:20 +0000 | [diff] [blame] | 90 | MCELFStreamer::EmitLabel(Symbol); |
| 91 | Labels.push_back(Symbol); |
| 92 | } |
| 93 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 94 | void MipsELFStreamer::SwitchSection(MCSection *Section, |
Zoran Jovanovic | 9c65483 | 2014-11-05 16:35:20 +0000 | [diff] [blame] | 95 | const MCExpr *Subsection) { |
| 96 | MCELFStreamer::SwitchSection(Section, Subsection); |
| 97 | Labels.clear(); |
| 98 | } |
| 99 | |
| 100 | void MipsELFStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size, |
Craig Topper | 3c76c52 | 2015-09-20 23:35:59 +0000 | [diff] [blame] | 101 | SMLoc Loc) { |
Zoran Jovanovic | 9c65483 | 2014-11-05 16:35:20 +0000 | [diff] [blame] | 102 | MCELFStreamer::EmitValueImpl(Value, Size, Loc); |
| 103 | Labels.clear(); |
Daniel Sanders | 68c3747 | 2014-07-21 13:30:55 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Simon Atanasyan | b524459 | 2018-07-25 07:07:43 +0000 | [diff] [blame] | 106 | void MipsELFStreamer::EmitIntValue(uint64_t Value, unsigned Size) { |
| 107 | MCELFStreamer::EmitIntValue(Value, Size); |
| 108 | Labels.clear(); |
| 109 | } |
| 110 | |
Daniel Sanders | 68c3747 | 2014-07-21 13:30:55 +0000 | [diff] [blame] | 111 | void MipsELFStreamer::EmitMipsOptionRecords() { |
| 112 | for (const auto &I : MipsOptionRecords) |
| 113 | I->EmitMipsOptionRecord(); |
| 114 | } |
Matheus Almeida | dac77fb | 2014-03-27 11:39:03 +0000 | [diff] [blame] | 115 | |
Lang Hames | 2241ffa | 2017-10-11 23:34:47 +0000 | [diff] [blame] | 116 | MCELFStreamer *llvm::createMipsELFStreamer( |
| 117 | MCContext &Context, std::unique_ptr<MCAsmBackend> MAB, |
Peter Collingbourne | f7b81db | 2018-05-18 18:26:45 +0000 | [diff] [blame] | 118 | std::unique_ptr<MCObjectWriter> OW, std::unique_ptr<MCCodeEmitter> Emitter, |
Lang Hames | 2241ffa | 2017-10-11 23:34:47 +0000 | [diff] [blame] | 119 | bool RelaxAll) { |
Peter Collingbourne | f7b81db | 2018-05-18 18:26:45 +0000 | [diff] [blame] | 120 | return new MipsELFStreamer(Context, std::move(MAB), std::move(OW), |
| 121 | std::move(Emitter)); |
Matheus Almeida | dac77fb | 2014-03-27 11:39:03 +0000 | [diff] [blame] | 122 | } |