Colin LeMahieu | be99a02 | 2015-06-17 03:06:16 +0000 | [diff] [blame] | 1 | //=== HexagonMCELFStreamer.cpp - Hexagon subclass of MCELFStreamer -------===// |
| 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 | // This file is a stub that parses a MCInst bundle and passes the |
| 11 | // instructions on to the real streamer. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | #define DEBUG_TYPE "hexagonmcelfstreamer" |
| 15 | |
| 16 | #include "Hexagon.h" |
| 17 | #include "HexagonMCELFStreamer.h" |
| 18 | #include "MCTargetDesc/HexagonBaseInfo.h" |
| 19 | #include "MCTargetDesc/HexagonMCShuffler.h" |
| 20 | #include "llvm/ADT/StringExtras.h" |
| 21 | #include "llvm/MC/MCAssembler.h" |
| 22 | #include "llvm/MC/MCContext.h" |
| 23 | #include "llvm/MC/MCSectionELF.h" |
| 24 | #include "llvm/MC/MCStreamer.h" |
| 25 | #include "llvm/MC/MCSymbol.h" |
| 26 | #include "llvm/MC/MCSymbolELF.h" |
| 27 | #include "llvm/Support/CommandLine.h" |
| 28 | #include "llvm/Support/Debug.h" |
| 29 | #include "llvm/Support/raw_ostream.h" |
| 30 | |
| 31 | using namespace llvm; |
| 32 | |
| 33 | static cl::opt<unsigned> |
| 34 | GPSize("gpsize", cl::NotHidden, |
| 35 | cl::desc("Global Pointer Addressing Size. The default size is 8."), |
| 36 | cl::Prefix, cl::init(8)); |
| 37 | |
| 38 | void HexagonMCELFStreamer::EmitInstruction(const MCInst &MCK, |
| 39 | const MCSubtargetInfo &STI) { |
| 40 | MCInst HMI; |
| 41 | HMI.setOpcode(Hexagon::BUNDLE); |
| 42 | HMI.addOperand(MCOperand::createImm(0)); |
| 43 | MCInst *MCB; |
| 44 | |
| 45 | if (MCK.getOpcode() != Hexagon::BUNDLE) { |
| 46 | HMI.addOperand(MCOperand::createInst(&MCK)); |
| 47 | MCB = &HMI; |
| 48 | } else |
| 49 | MCB = const_cast<MCInst *>(&MCK); |
| 50 | |
| 51 | // Examines packet and pad the packet, if needed, when an |
| 52 | // end-loop is in the bundle. |
Colin LeMahieu | b3c9727 | 2015-11-13 07:58:06 +0000 | [diff] [blame] | 53 | HexagonMCInstrInfo::padEndloop(getContext(), *MCB); |
Colin LeMahieu | be99a02 | 2015-06-17 03:06:16 +0000 | [diff] [blame] | 54 | HexagonMCShuffle(*MCII, STI, *MCB); |
| 55 | |
| 56 | assert(HexagonMCInstrInfo::bundleSize(*MCB) <= HEXAGON_PACKET_SIZE); |
| 57 | bool Extended = false; |
| 58 | for (auto &I : HexagonMCInstrInfo::bundleInstructions(*MCB)) { |
| 59 | MCInst *MCI = const_cast<MCInst *>(I.getInst()); |
| 60 | if (Extended) { |
| 61 | if (HexagonMCInstrInfo::isDuplex(*MCII, *MCI)) { |
| 62 | MCInst *SubInst = const_cast<MCInst *>(MCI->getOperand(1).getInst()); |
Colin LeMahieu | 8ab7e8e | 2015-11-10 00:02:27 +0000 | [diff] [blame] | 63 | HexagonMCInstrInfo::clampExtended(*MCII, getContext(), *SubInst); |
Colin LeMahieu | be99a02 | 2015-06-17 03:06:16 +0000 | [diff] [blame] | 64 | } else { |
Colin LeMahieu | 8ab7e8e | 2015-11-10 00:02:27 +0000 | [diff] [blame] | 65 | HexagonMCInstrInfo::clampExtended(*MCII, getContext(), *MCI); |
Colin LeMahieu | be99a02 | 2015-06-17 03:06:16 +0000 | [diff] [blame] | 66 | } |
| 67 | Extended = false; |
| 68 | } else { |
| 69 | Extended = HexagonMCInstrInfo::isImmext(*MCI); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // At this point, MCB is a bundle |
| 74 | // Iterate through the bundle and assign addends for the instructions |
| 75 | for (auto const &I : HexagonMCInstrInfo::bundleInstructions(*MCB)) { |
| 76 | MCInst *MCI = const_cast<MCInst *>(I.getInst()); |
| 77 | EmitSymbol(*MCI); |
| 78 | } |
| 79 | MCObjectStreamer::EmitInstruction(*MCB, STI); |
| 80 | } |
| 81 | |
| 82 | void HexagonMCELFStreamer::EmitSymbol(const MCInst &Inst) { |
| 83 | // Scan for values. |
| 84 | for (unsigned i = Inst.getNumOperands(); i--;) |
| 85 | if (Inst.getOperand(i).isExpr()) |
| 86 | visitUsedExpr(*Inst.getOperand(i).getExpr()); |
| 87 | } |
| 88 | |
| 89 | // EmitCommonSymbol and EmitLocalCommonSymbol are extended versions of the |
| 90 | // functions found in MCELFStreamer.cpp taking AccessSize as an additional |
| 91 | // parameter. |
| 92 | void HexagonMCELFStreamer::HexagonMCEmitCommonSymbol(MCSymbol *Symbol, |
| 93 | uint64_t Size, |
| 94 | unsigned ByteAlignment, |
| 95 | unsigned AccessSize) { |
| 96 | getAssembler().registerSymbol(*Symbol); |
| 97 | StringRef sbss[4] = {".sbss.1", ".sbss.2", ".sbss.4", ".sbss.8"}; |
| 98 | |
| 99 | auto ELFSymbol = cast<MCSymbolELF>(Symbol); |
| 100 | if (!ELFSymbol->isBindingSet()) { |
| 101 | ELFSymbol->setBinding(ELF::STB_GLOBAL); |
| 102 | ELFSymbol->setExternal(true); |
| 103 | } |
| 104 | |
| 105 | ELFSymbol->setType(ELF::STT_OBJECT); |
| 106 | |
| 107 | if (ELFSymbol->getBinding() == ELF::STB_LOCAL) { |
| 108 | StringRef SectionName = |
| 109 | ((AccessSize == 0) || (Size == 0) || (Size > GPSize)) |
| 110 | ? ".bss" |
| 111 | : sbss[(Log2_64(AccessSize))]; |
| 112 | |
| 113 | MCSection *CrntSection = getCurrentSection().first; |
| 114 | MCSection *Section = getAssembler().getContext().getELFSection( |
| 115 | SectionName, ELF::SHT_NOBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC); |
| 116 | SwitchSection(Section); |
Rafael Espindola | e3a20f5 | 2015-10-05 12:07:05 +0000 | [diff] [blame] | 117 | AssignFragment(Symbol, getCurrentFragment()); |
Colin LeMahieu | be99a02 | 2015-06-17 03:06:16 +0000 | [diff] [blame] | 118 | |
| 119 | MCELFStreamer::EmitCommonSymbol(Symbol, Size, ByteAlignment); |
| 120 | SwitchSection(CrntSection); |
| 121 | } else { |
| 122 | if (ELFSymbol->declareCommon(Size, ByteAlignment)) |
| 123 | report_fatal_error("Symbol: " + Symbol->getName() + |
| 124 | " redeclared as different type"); |
| 125 | if ((AccessSize) && (Size <= GPSize)) { |
| 126 | uint64_t SectionIndex = |
| 127 | (AccessSize <= GPSize) |
| 128 | ? ELF::SHN_HEXAGON_SCOMMON + (Log2_64(AccessSize) + 1) |
| 129 | : (unsigned)ELF::SHN_HEXAGON_SCOMMON; |
| 130 | ELFSymbol->setIndex(SectionIndex); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | ELFSymbol->setSize(MCConstantExpr::create(Size, getContext())); |
| 135 | } |
| 136 | |
| 137 | void HexagonMCELFStreamer::HexagonMCEmitLocalCommonSymbol( |
| 138 | MCSymbol *Symbol, uint64_t Size, unsigned ByteAlignment, |
| 139 | unsigned AccessSize) { |
| 140 | getAssembler().registerSymbol(*Symbol); |
| 141 | auto ELFSymbol = cast<MCSymbolELF>(Symbol); |
| 142 | ELFSymbol->setBinding(ELF::STB_LOCAL); |
| 143 | ELFSymbol->setExternal(false); |
| 144 | HexagonMCEmitCommonSymbol(Symbol, Size, ByteAlignment, AccessSize); |
| 145 | } |
| 146 | |
| 147 | namespace llvm { |
| 148 | MCStreamer *createHexagonELFStreamer(MCContext &Context, MCAsmBackend &MAB, |
| 149 | raw_pwrite_stream &OS, MCCodeEmitter *CE) { |
| 150 | return new HexagonMCELFStreamer(Context, MAB, OS, CE); |
| 151 | } |
| 152 | } |